OSDN Git Service

6d676b22ad228a58b653a5f8587f0425f7393c0e
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-codes.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "except.h"
38 #include "function.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "gimple.h"
51 #include "dwarf2.h"
52 #include "df.h"
53 #include "tm-constrs.h"
54 #include "params.h"
55 #include "cselib.h"
56 #include "debug.h"
57 #include "dwarf2out.h"
58
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
416      (we ensure the alignment).  For small blocks inline loop is still a
417      noticeable win, for bigger blocks either rep movsl or rep movsb is
418      way to go.  Rep movsb has apparently more expensive startup time in CPU,
419      but after 4K the difference is down in the noise.  */
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
715      small blocks it is better to use loop. For large blocks, libcall can
716      do 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 struct processor_costs bdver1_cost = {
823   COSTS_N_INSNS (1),                    /* cost of an add instruction */
824   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
825   COSTS_N_INSNS (1),                    /* variable shift costs */
826   COSTS_N_INSNS (1),                    /* constant shift costs */
827   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
828    COSTS_N_INSNS (4),                   /*                               HI */
829    COSTS_N_INSNS (3),                   /*                               SI */
830    COSTS_N_INSNS (4),                   /*                               DI */
831    COSTS_N_INSNS (5)},                  /*                            other */
832   0,                                    /* cost of multiply per each bit set */
833   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
834    COSTS_N_INSNS (35),                  /*                          HI */
835    COSTS_N_INSNS (51),                  /*                          SI */
836    COSTS_N_INSNS (83),                  /*                          DI */
837    COSTS_N_INSNS (83)},                 /*                          other */
838   COSTS_N_INSNS (1),                    /* cost of movsx */
839   COSTS_N_INSNS (1),                    /* cost of movzx */
840   8,                                    /* "large" insn */
841   9,                                    /* MOVE_RATIO */
842   4,                                 /* cost for loading QImode using movzbl */
843   {3, 4, 3},                            /* cost of loading integer registers
844                                            in QImode, HImode and SImode.
845                                            Relative to reg-reg move (2).  */
846   {3, 4, 3},                            /* cost of storing integer registers */
847   4,                                    /* cost of reg,reg fld/fst */
848   {4, 4, 12},                           /* cost of loading fp registers
849                                            in SFmode, DFmode and XFmode */
850   {6, 6, 8},                            /* cost of storing fp registers
851                                            in SFmode, DFmode and XFmode */
852   2,                                    /* cost of moving MMX register */
853   {3, 3},                               /* cost of loading MMX registers
854                                            in SImode and DImode */
855   {4, 4},                               /* cost of storing MMX registers
856                                            in SImode and DImode */
857   2,                                    /* cost of moving SSE register */
858   {4, 4, 3},                            /* cost of loading SSE registers
859                                            in SImode, DImode and TImode */
860   {4, 4, 5},                            /* cost of storing SSE registers
861                                            in SImode, DImode and TImode */
862   3,                                    /* MMX or SSE register to integer */
863                                         /* On K8:
864                                             MOVD reg64, xmmreg Double FSTORE 4
865                                             MOVD reg32, xmmreg Double FSTORE 4
866                                            On AMDFAM10:
867                                             MOVD reg64, xmmreg Double FADD 3
868                                                                1/1  1/1
869                                             MOVD reg32, xmmreg Double FADD 3
870                                                                1/1  1/1 */
871   64,                                   /* size of l1 cache.  */
872   1024,                                 /* size of l2 cache.  */
873   64,                                   /* size of prefetch block */
874   /* New AMD processors never drop prefetches; if they cannot be performed
875      immediately, they are queued.  We set number of simultaneous prefetches
876      to a large constant to reflect this (it probably is not a good idea not
877      to limit number of prefetches at all, as their execution also takes some
878      time).  */
879   100,                                  /* number of parallel prefetches */
880   2,                                    /* Branch cost */
881   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
882   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
883   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
884   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
885   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
886   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
887
888   /*  BDVER1 has optimized REP instruction for medium sized blocks, but for
889       very small blocks it is better to use loop. For large blocks, libcall
890       can do nontemporary accesses and beat inline considerably.  */
891   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
892    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
893   {{libcall, {{8, loop}, {24, unrolled_loop},
894               {2048, rep_prefix_4_byte}, {-1, libcall}}},
895    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
896   4,                                    /* scalar_stmt_cost.  */
897   2,                                    /* scalar load_cost.  */
898   2,                                    /* scalar_store_cost.  */
899   6,                                    /* vec_stmt_cost.  */
900   0,                                    /* vec_to_scalar_cost.  */
901   2,                                    /* scalar_to_vec_cost.  */
902   2,                                    /* vec_align_load_cost.  */
903   2,                                    /* vec_unalign_load_cost.  */
904   2,                                    /* vec_store_cost.  */
905   2,                                    /* cond_taken_branch_cost.  */
906   1,                                    /* cond_not_taken_branch_cost.  */
907 };
908
909 static const
910 struct processor_costs pentium4_cost = {
911   COSTS_N_INSNS (1),                    /* cost of an add instruction */
912   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
913   COSTS_N_INSNS (4),                    /* variable shift costs */
914   COSTS_N_INSNS (4),                    /* constant shift costs */
915   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
916    COSTS_N_INSNS (15),                  /*                               HI */
917    COSTS_N_INSNS (15),                  /*                               SI */
918    COSTS_N_INSNS (15),                  /*                               DI */
919    COSTS_N_INSNS (15)},                 /*                            other */
920   0,                                    /* cost of multiply per each bit set */
921   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
922    COSTS_N_INSNS (56),                  /*                          HI */
923    COSTS_N_INSNS (56),                  /*                          SI */
924    COSTS_N_INSNS (56),                  /*                          DI */
925    COSTS_N_INSNS (56)},                 /*                          other */
926   COSTS_N_INSNS (1),                    /* cost of movsx */
927   COSTS_N_INSNS (1),                    /* cost of movzx */
928   16,                                   /* "large" insn */
929   6,                                    /* MOVE_RATIO */
930   2,                                 /* cost for loading QImode using movzbl */
931   {4, 5, 4},                            /* cost of loading integer registers
932                                            in QImode, HImode and SImode.
933                                            Relative to reg-reg move (2).  */
934   {2, 3, 2},                            /* cost of storing integer registers */
935   2,                                    /* cost of reg,reg fld/fst */
936   {2, 2, 6},                            /* cost of loading fp registers
937                                            in SFmode, DFmode and XFmode */
938   {4, 4, 6},                            /* cost of storing fp registers
939                                            in SFmode, DFmode and XFmode */
940   2,                                    /* cost of moving MMX register */
941   {2, 2},                               /* cost of loading MMX registers
942                                            in SImode and DImode */
943   {2, 2},                               /* cost of storing MMX registers
944                                            in SImode and DImode */
945   12,                                   /* cost of moving SSE register */
946   {12, 12, 12},                         /* cost of loading SSE registers
947                                            in SImode, DImode and TImode */
948   {2, 2, 8},                            /* cost of storing SSE registers
949                                            in SImode, DImode and TImode */
950   10,                                   /* MMX or SSE register to integer */
951   8,                                    /* size of l1 cache.  */
952   256,                                  /* size of l2 cache.  */
953   64,                                   /* size of prefetch block */
954   6,                                    /* number of parallel prefetches */
955   2,                                    /* Branch cost */
956   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
957   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
958   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
959   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
960   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
961   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
962   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
963    DUMMY_STRINGOP_ALGS},
964   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
965    {-1, libcall}}},
966    DUMMY_STRINGOP_ALGS},
967   1,                                    /* scalar_stmt_cost.  */
968   1,                                    /* scalar load_cost.  */
969   1,                                    /* scalar_store_cost.  */
970   1,                                    /* vec_stmt_cost.  */
971   1,                                    /* vec_to_scalar_cost.  */
972   1,                                    /* scalar_to_vec_cost.  */
973   1,                                    /* vec_align_load_cost.  */
974   2,                                    /* vec_unalign_load_cost.  */
975   1,                                    /* vec_store_cost.  */
976   3,                                    /* cond_taken_branch_cost.  */
977   1,                                    /* cond_not_taken_branch_cost.  */
978 };
979
980 static const
981 struct processor_costs nocona_cost = {
982   COSTS_N_INSNS (1),                    /* cost of an add instruction */
983   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
984   COSTS_N_INSNS (1),                    /* variable shift costs */
985   COSTS_N_INSNS (1),                    /* constant shift costs */
986   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
987    COSTS_N_INSNS (10),                  /*                               HI */
988    COSTS_N_INSNS (10),                  /*                               SI */
989    COSTS_N_INSNS (10),                  /*                               DI */
990    COSTS_N_INSNS (10)},                 /*                            other */
991   0,                                    /* cost of multiply per each bit set */
992   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
993    COSTS_N_INSNS (66),                  /*                          HI */
994    COSTS_N_INSNS (66),                  /*                          SI */
995    COSTS_N_INSNS (66),                  /*                          DI */
996    COSTS_N_INSNS (66)},                 /*                          other */
997   COSTS_N_INSNS (1),                    /* cost of movsx */
998   COSTS_N_INSNS (1),                    /* cost of movzx */
999   16,                                   /* "large" insn */
1000   17,                                   /* MOVE_RATIO */
1001   4,                                 /* cost for loading QImode using movzbl */
1002   {4, 4, 4},                            /* cost of loading integer registers
1003                                            in QImode, HImode and SImode.
1004                                            Relative to reg-reg move (2).  */
1005   {4, 4, 4},                            /* cost of storing integer registers */
1006   3,                                    /* cost of reg,reg fld/fst */
1007   {12, 12, 12},                         /* cost of loading fp registers
1008                                            in SFmode, DFmode and XFmode */
1009   {4, 4, 4},                            /* cost of storing fp registers
1010                                            in SFmode, DFmode and XFmode */
1011   6,                                    /* cost of moving MMX register */
1012   {12, 12},                             /* cost of loading MMX registers
1013                                            in SImode and DImode */
1014   {12, 12},                             /* cost of storing MMX registers
1015                                            in SImode and DImode */
1016   6,                                    /* cost of moving SSE register */
1017   {12, 12, 12},                         /* cost of loading SSE registers
1018                                            in SImode, DImode and TImode */
1019   {12, 12, 12},                         /* cost of storing SSE registers
1020                                            in SImode, DImode and TImode */
1021   8,                                    /* MMX or SSE register to integer */
1022   8,                                    /* size of l1 cache.  */
1023   1024,                                 /* size of l2 cache.  */
1024   128,                                  /* size of prefetch block */
1025   8,                                    /* number of parallel prefetches */
1026   1,                                    /* Branch cost */
1027   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
1028   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1029   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
1030   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
1031   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
1032   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
1033   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
1034    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
1035               {100000, unrolled_loop}, {-1, libcall}}}},
1036   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
1037    {-1, libcall}}},
1038    {libcall, {{24, loop}, {64, unrolled_loop},
1039               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1040   1,                                    /* scalar_stmt_cost.  */
1041   1,                                    /* scalar load_cost.  */
1042   1,                                    /* scalar_store_cost.  */
1043   1,                                    /* vec_stmt_cost.  */
1044   1,                                    /* vec_to_scalar_cost.  */
1045   1,                                    /* scalar_to_vec_cost.  */
1046   1,                                    /* vec_align_load_cost.  */
1047   2,                                    /* vec_unalign_load_cost.  */
1048   1,                                    /* vec_store_cost.  */
1049   3,                                    /* cond_taken_branch_cost.  */
1050   1,                                    /* cond_not_taken_branch_cost.  */
1051 };
1052
1053 static const
1054 struct processor_costs core2_cost = {
1055   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1056   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1057   COSTS_N_INSNS (1),                    /* variable shift costs */
1058   COSTS_N_INSNS (1),                    /* constant shift costs */
1059   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1060    COSTS_N_INSNS (3),                   /*                               HI */
1061    COSTS_N_INSNS (3),                   /*                               SI */
1062    COSTS_N_INSNS (3),                   /*                               DI */
1063    COSTS_N_INSNS (3)},                  /*                            other */
1064   0,                                    /* cost of multiply per each bit set */
1065   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
1066    COSTS_N_INSNS (22),                  /*                          HI */
1067    COSTS_N_INSNS (22),                  /*                          SI */
1068    COSTS_N_INSNS (22),                  /*                          DI */
1069    COSTS_N_INSNS (22)},                 /*                          other */
1070   COSTS_N_INSNS (1),                    /* cost of movsx */
1071   COSTS_N_INSNS (1),                    /* cost of movzx */
1072   8,                                    /* "large" insn */
1073   16,                                   /* MOVE_RATIO */
1074   2,                                 /* cost for loading QImode using movzbl */
1075   {6, 6, 6},                            /* cost of loading integer registers
1076                                            in QImode, HImode and SImode.
1077                                            Relative to reg-reg move (2).  */
1078   {4, 4, 4},                            /* cost of storing integer registers */
1079   2,                                    /* cost of reg,reg fld/fst */
1080   {6, 6, 6},                            /* cost of loading fp registers
1081                                            in SFmode, DFmode and XFmode */
1082   {4, 4, 4},                            /* cost of storing fp registers
1083                                            in SFmode, DFmode and XFmode */
1084   2,                                    /* cost of moving MMX register */
1085   {6, 6},                               /* cost of loading MMX registers
1086                                            in SImode and DImode */
1087   {4, 4},                               /* cost of storing MMX registers
1088                                            in SImode and DImode */
1089   2,                                    /* cost of moving SSE register */
1090   {6, 6, 6},                            /* cost of loading SSE registers
1091                                            in SImode, DImode and TImode */
1092   {4, 4, 4},                            /* cost of storing SSE registers
1093                                            in SImode, DImode and TImode */
1094   2,                                    /* MMX or SSE register to integer */
1095   32,                                   /* size of l1 cache.  */
1096   2048,                                 /* size of l2 cache.  */
1097   128,                                  /* size of prefetch block */
1098   8,                                    /* number of parallel prefetches */
1099   3,                                    /* Branch cost */
1100   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
1101   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
1102   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
1103   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
1104   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
1105   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
1106   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1107    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1108               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1109   {{libcall, {{8, loop}, {15, unrolled_loop},
1110               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1111    {libcall, {{24, loop}, {32, unrolled_loop},
1112               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1113   1,                                    /* scalar_stmt_cost.  */
1114   1,                                    /* scalar load_cost.  */
1115   1,                                    /* scalar_store_cost.  */
1116   1,                                    /* vec_stmt_cost.  */
1117   1,                                    /* vec_to_scalar_cost.  */
1118   1,                                    /* scalar_to_vec_cost.  */
1119   1,                                    /* vec_align_load_cost.  */
1120   2,                                    /* vec_unalign_load_cost.  */
1121   1,                                    /* vec_store_cost.  */
1122   3,                                    /* cond_taken_branch_cost.  */
1123   1,                                    /* cond_not_taken_branch_cost.  */
1124 };
1125
1126 static const
1127 struct processor_costs atom_cost = {
1128   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1129   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1130   COSTS_N_INSNS (1),                    /* variable shift costs */
1131   COSTS_N_INSNS (1),                    /* constant shift costs */
1132   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1133    COSTS_N_INSNS (4),                   /*                               HI */
1134    COSTS_N_INSNS (3),                   /*                               SI */
1135    COSTS_N_INSNS (4),                   /*                               DI */
1136    COSTS_N_INSNS (2)},                  /*                            other */
1137   0,                                    /* cost of multiply per each bit set */
1138   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1139    COSTS_N_INSNS (26),                  /*                          HI */
1140    COSTS_N_INSNS (42),                  /*                          SI */
1141    COSTS_N_INSNS (74),                  /*                          DI */
1142    COSTS_N_INSNS (74)},                 /*                          other */
1143   COSTS_N_INSNS (1),                    /* cost of movsx */
1144   COSTS_N_INSNS (1),                    /* cost of movzx */
1145   8,                                    /* "large" insn */
1146   17,                                   /* MOVE_RATIO */
1147   2,                                 /* cost for loading QImode using movzbl */
1148   {4, 4, 4},                            /* cost of loading integer registers
1149                                            in QImode, HImode and SImode.
1150                                            Relative to reg-reg move (2).  */
1151   {4, 4, 4},                            /* cost of storing integer registers */
1152   4,                                    /* cost of reg,reg fld/fst */
1153   {12, 12, 12},                         /* cost of loading fp registers
1154                                            in SFmode, DFmode and XFmode */
1155   {6, 6, 8},                            /* cost of storing fp registers
1156                                            in SFmode, DFmode and XFmode */
1157   2,                                    /* cost of moving MMX register */
1158   {8, 8},                               /* cost of loading MMX registers
1159                                            in SImode and DImode */
1160   {8, 8},                               /* cost of storing MMX registers
1161                                            in SImode and DImode */
1162   2,                                    /* cost of moving SSE register */
1163   {8, 8, 8},                            /* cost of loading SSE registers
1164                                            in SImode, DImode and TImode */
1165   {8, 8, 8},                            /* cost of storing SSE registers
1166                                            in SImode, DImode and TImode */
1167   5,                                    /* MMX or SSE register to integer */
1168   32,                                   /* size of l1 cache.  */
1169   256,                                  /* size of l2 cache.  */
1170   64,                                   /* size of prefetch block */
1171   6,                                    /* number of parallel prefetches */
1172   3,                                    /* Branch cost */
1173   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1174   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1175   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1176   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1177   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1178   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1179   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1180    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1181           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1182   {{libcall, {{8, loop}, {15, unrolled_loop},
1183           {2048, rep_prefix_4_byte}, {-1, libcall}}},
1184    {libcall, {{24, loop}, {32, unrolled_loop},
1185           {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1186   1,                                    /* scalar_stmt_cost.  */
1187   1,                                    /* scalar load_cost.  */
1188   1,                                    /* scalar_store_cost.  */
1189   1,                                    /* vec_stmt_cost.  */
1190   1,                                    /* vec_to_scalar_cost.  */
1191   1,                                    /* scalar_to_vec_cost.  */
1192   1,                                    /* vec_align_load_cost.  */
1193   2,                                    /* vec_unalign_load_cost.  */
1194   1,                                    /* vec_store_cost.  */
1195   3,                                    /* cond_taken_branch_cost.  */
1196   1,                                    /* cond_not_taken_branch_cost.  */
1197 };
1198
1199 /* Generic64 should produce code tuned for Nocona and K8.  */
1200 static const
1201 struct processor_costs generic64_cost = {
1202   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1203   /* On all chips taken into consideration lea is 2 cycles and more.  With
1204      this cost however our current implementation of synth_mult results in
1205      use of unnecessary temporary registers causing regression on several
1206      SPECfp benchmarks.  */
1207   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1208   COSTS_N_INSNS (1),                    /* variable shift costs */
1209   COSTS_N_INSNS (1),                    /* constant shift costs */
1210   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1211    COSTS_N_INSNS (4),                   /*                               HI */
1212    COSTS_N_INSNS (3),                   /*                               SI */
1213    COSTS_N_INSNS (4),                   /*                               DI */
1214    COSTS_N_INSNS (2)},                  /*                            other */
1215   0,                                    /* cost of multiply per each bit set */
1216   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1217    COSTS_N_INSNS (26),                  /*                          HI */
1218    COSTS_N_INSNS (42),                  /*                          SI */
1219    COSTS_N_INSNS (74),                  /*                          DI */
1220    COSTS_N_INSNS (74)},                 /*                          other */
1221   COSTS_N_INSNS (1),                    /* cost of movsx */
1222   COSTS_N_INSNS (1),                    /* cost of movzx */
1223   8,                                    /* "large" insn */
1224   17,                                   /* MOVE_RATIO */
1225   4,                                 /* cost for loading QImode using movzbl */
1226   {4, 4, 4},                            /* cost of loading integer registers
1227                                            in QImode, HImode and SImode.
1228                                            Relative to reg-reg move (2).  */
1229   {4, 4, 4},                            /* cost of storing integer registers */
1230   4,                                    /* cost of reg,reg fld/fst */
1231   {12, 12, 12},                         /* cost of loading fp registers
1232                                            in SFmode, DFmode and XFmode */
1233   {6, 6, 8},                            /* cost of storing fp registers
1234                                            in SFmode, DFmode and XFmode */
1235   2,                                    /* cost of moving MMX register */
1236   {8, 8},                               /* cost of loading MMX registers
1237                                            in SImode and DImode */
1238   {8, 8},                               /* cost of storing MMX registers
1239                                            in SImode and DImode */
1240   2,                                    /* cost of moving SSE register */
1241   {8, 8, 8},                            /* cost of loading SSE registers
1242                                            in SImode, DImode and TImode */
1243   {8, 8, 8},                            /* cost of storing SSE registers
1244                                            in SImode, DImode and TImode */
1245   5,                                    /* MMX or SSE register to integer */
1246   32,                                   /* size of l1 cache.  */
1247   512,                                  /* size of l2 cache.  */
1248   64,                                   /* size of prefetch block */
1249   6,                                    /* number of parallel prefetches */
1250   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this
1251      value is increased to perhaps more appropriate value of 5.  */
1252   3,                                    /* Branch cost */
1253   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1254   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1255   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1256   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1257   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1258   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1259   {DUMMY_STRINGOP_ALGS,
1260    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1261   {DUMMY_STRINGOP_ALGS,
1262    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1263   1,                                    /* scalar_stmt_cost.  */
1264   1,                                    /* scalar load_cost.  */
1265   1,                                    /* scalar_store_cost.  */
1266   1,                                    /* vec_stmt_cost.  */
1267   1,                                    /* vec_to_scalar_cost.  */
1268   1,                                    /* scalar_to_vec_cost.  */
1269   1,                                    /* vec_align_load_cost.  */
1270   2,                                    /* vec_unalign_load_cost.  */
1271   1,                                    /* vec_store_cost.  */
1272   3,                                    /* cond_taken_branch_cost.  */
1273   1,                                    /* cond_not_taken_branch_cost.  */
1274 };
1275
1276 /* Generic32 should produce code tuned for PPro, Pentium4, Nocona,
1277    Athlon and K8.  */
1278 static const
1279 struct processor_costs generic32_cost = {
1280   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1281   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1282   COSTS_N_INSNS (1),                    /* variable shift costs */
1283   COSTS_N_INSNS (1),                    /* constant shift costs */
1284   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1285    COSTS_N_INSNS (4),                   /*                               HI */
1286    COSTS_N_INSNS (3),                   /*                               SI */
1287    COSTS_N_INSNS (4),                   /*                               DI */
1288    COSTS_N_INSNS (2)},                  /*                            other */
1289   0,                                    /* cost of multiply per each bit set */
1290   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1291    COSTS_N_INSNS (26),                  /*                          HI */
1292    COSTS_N_INSNS (42),                  /*                          SI */
1293    COSTS_N_INSNS (74),                  /*                          DI */
1294    COSTS_N_INSNS (74)},                 /*                          other */
1295   COSTS_N_INSNS (1),                    /* cost of movsx */
1296   COSTS_N_INSNS (1),                    /* cost of movzx */
1297   8,                                    /* "large" insn */
1298   17,                                   /* MOVE_RATIO */
1299   4,                                 /* cost for loading QImode using movzbl */
1300   {4, 4, 4},                            /* cost of loading integer registers
1301                                            in QImode, HImode and SImode.
1302                                            Relative to reg-reg move (2).  */
1303   {4, 4, 4},                            /* cost of storing integer registers */
1304   4,                                    /* cost of reg,reg fld/fst */
1305   {12, 12, 12},                         /* cost of loading fp registers
1306                                            in SFmode, DFmode and XFmode */
1307   {6, 6, 8},                            /* cost of storing fp registers
1308                                            in SFmode, DFmode and XFmode */
1309   2,                                    /* cost of moving MMX register */
1310   {8, 8},                               /* cost of loading MMX registers
1311                                            in SImode and DImode */
1312   {8, 8},                               /* cost of storing MMX registers
1313                                            in SImode and DImode */
1314   2,                                    /* cost of moving SSE register */
1315   {8, 8, 8},                            /* cost of loading SSE registers
1316                                            in SImode, DImode and TImode */
1317   {8, 8, 8},                            /* cost of storing SSE registers
1318                                            in SImode, DImode and TImode */
1319   5,                                    /* MMX or SSE register to integer */
1320   32,                                   /* size of l1 cache.  */
1321   256,                                  /* size of l2 cache.  */
1322   64,                                   /* size of prefetch block */
1323   6,                                    /* number of parallel prefetches */
1324   3,                                    /* Branch cost */
1325   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1326   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1327   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1328   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1329   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1330   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1331   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1332    DUMMY_STRINGOP_ALGS},
1333   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1334    DUMMY_STRINGOP_ALGS},
1335   1,                                    /* scalar_stmt_cost.  */
1336   1,                                    /* scalar load_cost.  */
1337   1,                                    /* scalar_store_cost.  */
1338   1,                                    /* vec_stmt_cost.  */
1339   1,                                    /* vec_to_scalar_cost.  */
1340   1,                                    /* scalar_to_vec_cost.  */
1341   1,                                    /* vec_align_load_cost.  */
1342   2,                                    /* vec_unalign_load_cost.  */
1343   1,                                    /* vec_store_cost.  */
1344   3,                                    /* cond_taken_branch_cost.  */
1345   1,                                    /* cond_not_taken_branch_cost.  */
1346 };
1347
1348 const struct processor_costs *ix86_cost = &pentium_cost;
1349
1350 /* Processor feature/optimization bitmasks.  */
1351 #define m_386 (1<<PROCESSOR_I386)
1352 #define m_486 (1<<PROCESSOR_I486)
1353 #define m_PENT (1<<PROCESSOR_PENTIUM)
1354 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1355 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
1356 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
1357 #define m_CORE2  (1<<PROCESSOR_CORE2)
1358 #define m_ATOM  (1<<PROCESSOR_ATOM)
1359
1360 #define m_GEODE  (1<<PROCESSOR_GEODE)
1361 #define m_K6  (1<<PROCESSOR_K6)
1362 #define m_K6_GEODE  (m_K6 | m_GEODE)
1363 #define m_K8  (1<<PROCESSOR_K8)
1364 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
1365 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
1366 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
1367 #define m_BDVER1  (1<<PROCESSOR_BDVER1)
1368 #define m_AMD_MULTIPLE  (m_K8 | m_ATHLON | m_AMDFAM10 | m_BDVER1)
1369
1370 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1371 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1372
1373 /* Generic instruction choice should be common subset of supported CPUs
1374    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1375 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1376
1377 /* Feature tests against the various tunings.  */
1378 unsigned char ix86_tune_features[X86_TUNE_LAST];
1379
1380 /* Feature tests against the various tunings used to create ix86_tune_features
1381    based on the processor mask.  */
1382 static unsigned int initial_ix86_tune_features[X86_TUNE_LAST] = {
1383   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1384      negatively, so enabling for Generic64 seems like good code size
1385      tradeoff.  We can't enable it for 32bit generic because it does not
1386      work well with PPro base chips.  */
1387   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_CORE2 | m_GENERIC64,
1388
1389   /* X86_TUNE_PUSH_MEMORY */
1390   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1391   | m_NOCONA | m_CORE2 | m_GENERIC,
1392
1393   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1394   m_486 | m_PENT,
1395
1396   /* X86_TUNE_UNROLL_STRLEN */
1397   m_486 | m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_K6
1398   | m_CORE2 | m_GENERIC,
1399
1400   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1401   m_ATOM | m_PPRO | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4 | m_GENERIC,
1402
1403   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1404      on simulation result. But after P4 was made, no performance benefit
1405      was observed with branch hints.  It also increases the code size.
1406      As a result, icc never generates branch hints.  */
1407   0,
1408
1409   /* X86_TUNE_DOUBLE_WITH_ADD */
1410   ~m_386,
1411
1412   /* X86_TUNE_USE_SAHF */
1413   m_ATOM | m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_BDVER1 | m_PENT4
1414   | m_NOCONA | m_CORE2 | m_GENERIC,
1415
1416   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1417      partial dependencies.  */
1418   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA
1419   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1420
1421   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1422      register stalls on Generic32 compilation setting as well.  However
1423      in current implementation the partial register stalls are not eliminated
1424      very well - they can be introduced via subregs synthesized by combine
1425      and can happen in caller/callee saving sequences.  Because this option
1426      pays back little on PPro based chips and is in conflict with partial reg
1427      dependencies used by Athlon/P4 based chips, it is better to leave it off
1428      for generic32 for now.  */
1429   m_PPRO,
1430
1431   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1432   m_CORE2 | m_GENERIC,
1433
1434   /* X86_TUNE_USE_HIMODE_FIOP */
1435   m_386 | m_486 | m_K6_GEODE,
1436
1437   /* X86_TUNE_USE_SIMODE_FIOP */
1438   ~(m_PPRO | m_AMD_MULTIPLE | m_PENT | m_ATOM | m_CORE2 | m_GENERIC),
1439
1440   /* X86_TUNE_USE_MOV0 */
1441   m_K6,
1442
1443   /* X86_TUNE_USE_CLTD */
1444   ~(m_PENT | m_ATOM | m_K6 | m_CORE2 | m_GENERIC),
1445
1446   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1447   m_PENT4,
1448
1449   /* X86_TUNE_SPLIT_LONG_MOVES */
1450   m_PPRO,
1451
1452   /* X86_TUNE_READ_MODIFY_WRITE */
1453   ~m_PENT,
1454
1455   /* X86_TUNE_READ_MODIFY */
1456   ~(m_PENT | m_PPRO),
1457
1458   /* X86_TUNE_PROMOTE_QIMODE */
1459   m_K6_GEODE | m_PENT | m_ATOM | m_386 | m_486 | m_AMD_MULTIPLE
1460   | m_CORE2 | m_GENERIC /* | m_PENT4 ? */,
1461
1462   /* X86_TUNE_FAST_PREFIX */
1463   ~(m_PENT | m_486 | m_386),
1464
1465   /* X86_TUNE_SINGLE_STRINGOP */
1466   m_386 | m_PENT4 | m_NOCONA,
1467
1468   /* X86_TUNE_QIMODE_MATH */
1469   ~0,
1470
1471   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1472      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1473      might be considered for Generic32 if our scheme for avoiding partial
1474      stalls was more effective.  */
1475   ~m_PPRO,
1476
1477   /* X86_TUNE_PROMOTE_QI_REGS */
1478   0,
1479
1480   /* X86_TUNE_PROMOTE_HI_REGS */
1481   m_PPRO,
1482
1483   /* X86_TUNE_ADD_ESP_4: Enable if add/sub is preferred over 1/2 push/pop.  */
1484   m_ATOM | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT4 | m_NOCONA
1485   | m_CORE2 | m_GENERIC,
1486
1487   /* X86_TUNE_ADD_ESP_8 */
1488   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_K6_GEODE | m_386
1489   | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1490
1491   /* X86_TUNE_SUB_ESP_4 */
1492   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2
1493   | m_GENERIC,
1494
1495   /* X86_TUNE_SUB_ESP_8 */
1496   m_AMD_MULTIPLE | m_ATOM | m_PPRO | m_386 | m_486
1497   | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1498
1499   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1500      for DFmode copies */
1501   ~(m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1502     | m_GENERIC | m_GEODE),
1503
1504   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1505   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1506
1507   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1508      conflict here in between PPro/Pentium4 based chips that thread 128bit
1509      SSE registers as single units versus K8 based chips that divide SSE
1510      registers to two 64bit halves.  This knob promotes all store destinations
1511      to be 128bit to allow register renaming on 128bit SSE units, but usually
1512      results in one extra microop on 64bit SSE units.  Experimental results
1513      shows that disabling this option on P4 brings over 20% SPECfp regression,
1514      while enabling it on K8 brings roughly 2.4% regression that can be partly
1515      masked by careful scheduling of moves.  */
1516   m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC
1517   | m_AMDFAM10 | m_BDVER1,
1518
1519   /* X86_TUNE_SSE_UNALIGNED_LOAD_OPTIMAL */
1520   m_AMDFAM10 | m_BDVER1,
1521
1522   /* X86_TUNE_SSE_UNALIGNED_STORE_OPTIMAL */
1523   m_BDVER1,
1524
1525   /* X86_TUNE_SSE_PACKED_SINGLE_INSN_OPTIMAL */
1526   m_BDVER1,
1527
1528   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1529      are resolved on SSE register parts instead of whole registers, so we may
1530      maintain just lower part of scalar values in proper format leaving the
1531      upper part undefined.  */
1532   m_ATHLON_K8,
1533
1534   /* X86_TUNE_SSE_TYPELESS_STORES */
1535   m_AMD_MULTIPLE,
1536
1537   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1538   m_PPRO | m_PENT4 | m_NOCONA,
1539
1540   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1541   m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1542
1543   /* X86_TUNE_PROLOGUE_USING_MOVE */
1544   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1545
1546   /* X86_TUNE_EPILOGUE_USING_MOVE */
1547   m_ATHLON_K8 | m_ATOM | m_PPRO | m_CORE2 | m_GENERIC,
1548
1549   /* X86_TUNE_SHIFT1 */
1550   ~m_486,
1551
1552   /* X86_TUNE_USE_FFREEP */
1553   m_AMD_MULTIPLE,
1554
1555   /* X86_TUNE_INTER_UNIT_MOVES */
1556   ~(m_AMD_MULTIPLE | m_GENERIC),
1557
1558   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
1559   ~(m_AMDFAM10 | m_BDVER1),
1560
1561   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1562      than 4 branch instructions in the 16 byte window.  */
1563   m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2
1564   | m_GENERIC,
1565
1566   /* X86_TUNE_SCHEDULE */
1567   m_PPRO | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT | m_ATOM | m_CORE2
1568   | m_GENERIC,
1569
1570   /* X86_TUNE_USE_BT */
1571   m_AMD_MULTIPLE | m_ATOM | m_CORE2 | m_GENERIC,
1572
1573   /* X86_TUNE_USE_INCDEC */
1574   ~(m_PENT4 | m_NOCONA | m_GENERIC | m_ATOM),
1575
1576   /* X86_TUNE_PAD_RETURNS */
1577   m_AMD_MULTIPLE | m_CORE2 | m_GENERIC,
1578
1579   /* X86_TUNE_EXT_80387_CONSTANTS */
1580   m_K6_GEODE | m_ATHLON_K8 | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO
1581   | m_CORE2 | m_GENERIC,
1582
1583   /* X86_TUNE_SHORTEN_X87_SSE */
1584   ~m_K8,
1585
1586   /* X86_TUNE_AVOID_VECTOR_DECODE */
1587   m_K8 | m_GENERIC64,
1588
1589   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1590      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1591   ~(m_386 | m_486),
1592
1593   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1594      vector path on AMD machines.  */
1595   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1596
1597   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1598      machines.  */
1599   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1600
1601   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1602      than a MOV.  */
1603   m_PENT,
1604
1605   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1606      but one byte longer.  */
1607   m_PENT,
1608
1609   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1610      operand that cannot be represented using a modRM byte.  The XOR
1611      replacement is long decoded, so this split helps here as well.  */
1612   m_K6,
1613
1614   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
1615      from FP to FP. */
1616   m_AMDFAM10 | m_GENERIC,
1617
1618   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1619      from integer to FP. */
1620   m_AMDFAM10,
1621
1622   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
1623      with a subsequent conditional jump instruction into a single
1624      compare-and-branch uop.  */
1625   m_CORE2 | m_BDVER1,
1626
1627   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
1628      will impact LEA instruction selection. */
1629   m_ATOM,
1630 };
1631
1632 /* Feature tests against the various architecture variations.  */
1633 unsigned char ix86_arch_features[X86_ARCH_LAST];
1634
1635 /* Feature tests against the various architecture variations, used to create
1636    ix86_arch_features based on the processor mask.  */
1637 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
1638   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1639   ~(m_386 | m_486 | m_PENT | m_K6),
1640
1641   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1642   ~m_386,
1643
1644   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1645   ~(m_386 | m_486),
1646
1647   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1648   ~m_386,
1649
1650   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1651   ~m_386,
1652 };
1653
1654 static const unsigned int x86_accumulate_outgoing_args
1655   = m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1656     | m_GENERIC;
1657
1658 static const unsigned int x86_arch_always_fancy_math_387
1659   = m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4
1660     | m_NOCONA | m_CORE2 | m_GENERIC;
1661
1662 static enum stringop_alg stringop_alg = no_stringop;
1663
1664 /* In case the average insn count for single function invocation is
1665    lower than this constant, emit fast (but longer) prologue and
1666    epilogue code.  */
1667 #define FAST_PROLOGUE_INSN_COUNT 20
1668
1669 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1670 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1671 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1672 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1673
1674 /* Array of the smallest class containing reg number REGNO, indexed by
1675    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1676
1677 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1678 {
1679   /* ax, dx, cx, bx */
1680   AREG, DREG, CREG, BREG,
1681   /* si, di, bp, sp */
1682   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1683   /* FP registers */
1684   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1685   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1686   /* arg pointer */
1687   NON_Q_REGS,
1688   /* flags, fpsr, fpcr, frame */
1689   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1690   /* SSE registers */
1691   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1692   SSE_REGS, SSE_REGS,
1693   /* MMX registers */
1694   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1695   MMX_REGS, MMX_REGS,
1696   /* REX registers */
1697   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1698   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1699   /* SSE REX registers */
1700   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1701   SSE_REGS, SSE_REGS,
1702 };
1703
1704 /* The "default" register map used in 32bit mode.  */
1705
1706 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1707 {
1708   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1709   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1710   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1711   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1712   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1713   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1714   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1715 };
1716
1717 /* The "default" register map used in 64bit mode.  */
1718
1719 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1720 {
1721   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1722   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1723   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1724   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1725   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1726   8,9,10,11,12,13,14,15,                /* extended integer registers */
1727   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1728 };
1729
1730 /* Define the register numbers to be used in Dwarf debugging information.
1731    The SVR4 reference port C compiler uses the following register numbers
1732    in its Dwarf output code:
1733         0 for %eax (gcc regno = 0)
1734         1 for %ecx (gcc regno = 2)
1735         2 for %edx (gcc regno = 1)
1736         3 for %ebx (gcc regno = 3)
1737         4 for %esp (gcc regno = 7)
1738         5 for %ebp (gcc regno = 6)
1739         6 for %esi (gcc regno = 4)
1740         7 for %edi (gcc regno = 5)
1741    The following three DWARF register numbers are never generated by
1742    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1743    believes these numbers have these meanings.
1744         8  for %eip    (no gcc equivalent)
1745         9  for %eflags (gcc regno = 17)
1746         10 for %trapno (no gcc equivalent)
1747    It is not at all clear how we should number the FP stack registers
1748    for the x86 architecture.  If the version of SDB on x86/svr4 were
1749    a bit less brain dead with respect to floating-point then we would
1750    have a precedent to follow with respect to DWARF register numbers
1751    for x86 FP registers, but the SDB on x86/svr4 is so completely
1752    broken with respect to FP registers that it is hardly worth thinking
1753    of it as something to strive for compatibility with.
1754    The version of x86/svr4 SDB I have at the moment does (partially)
1755    seem to believe that DWARF register number 11 is associated with
1756    the x86 register %st(0), but that's about all.  Higher DWARF
1757    register numbers don't seem to be associated with anything in
1758    particular, and even for DWARF regno 11, SDB only seems to under-
1759    stand that it should say that a variable lives in %st(0) (when
1760    asked via an `=' command) if we said it was in DWARF regno 11,
1761    but SDB still prints garbage when asked for the value of the
1762    variable in question (via a `/' command).
1763    (Also note that the labels SDB prints for various FP stack regs
1764    when doing an `x' command are all wrong.)
1765    Note that these problems generally don't affect the native SVR4
1766    C compiler because it doesn't allow the use of -O with -g and
1767    because when it is *not* optimizing, it allocates a memory
1768    location for each floating-point variable, and the memory
1769    location is what gets described in the DWARF AT_location
1770    attribute for the variable in question.
1771    Regardless of the severe mental illness of the x86/svr4 SDB, we
1772    do something sensible here and we use the following DWARF
1773    register numbers.  Note that these are all stack-top-relative
1774    numbers.
1775         11 for %st(0) (gcc regno = 8)
1776         12 for %st(1) (gcc regno = 9)
1777         13 for %st(2) (gcc regno = 10)
1778         14 for %st(3) (gcc regno = 11)
1779         15 for %st(4) (gcc regno = 12)
1780         16 for %st(5) (gcc regno = 13)
1781         17 for %st(6) (gcc regno = 14)
1782         18 for %st(7) (gcc regno = 15)
1783 */
1784 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1785 {
1786   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1787   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1788   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1789   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1790   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1791   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1792   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1793 };
1794
1795 /* Define parameter passing and return registers.  */
1796
1797 static int const x86_64_int_parameter_registers[6] =
1798 {
1799   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
1800 };
1801
1802 static int const x86_64_ms_abi_int_parameter_registers[4] =
1803 {
1804   CX_REG, DX_REG, R8_REG, R9_REG
1805 };
1806
1807 static int const x86_64_int_return_registers[4] =
1808 {
1809   AX_REG, DX_REG, DI_REG, SI_REG
1810 };
1811
1812 /* Define the structure for the machine field in struct function.  */
1813
1814 struct GTY(()) stack_local_entry {
1815   unsigned short mode;
1816   unsigned short n;
1817   rtx rtl;
1818   struct stack_local_entry *next;
1819 };
1820
1821 /* Structure describing stack frame layout.
1822    Stack grows downward:
1823
1824    [arguments]
1825                                               <- ARG_POINTER
1826    saved pc
1827
1828    saved frame pointer if frame_pointer_needed
1829                                               <- HARD_FRAME_POINTER
1830    [saved regs]
1831
1832    [padding0]
1833
1834    [saved SSE regs]
1835
1836    [padding1]          \
1837                         )
1838    [va_arg registers]  (
1839                         > to_allocate         <- FRAME_POINTER
1840    [frame]             (
1841                         )
1842    [padding2]          /
1843   */
1844 struct ix86_frame
1845 {
1846   int padding0;
1847   int nsseregs;
1848   int nregs;
1849   int padding1;
1850   int va_arg_size;
1851   int red_zone_size;
1852   HOST_WIDE_INT frame;
1853   int padding2;
1854   int outgoing_arguments_size;
1855
1856   HOST_WIDE_INT to_allocate;
1857   /* The offsets relative to ARG_POINTER.  */
1858   HOST_WIDE_INT frame_pointer_offset;
1859   HOST_WIDE_INT hard_frame_pointer_offset;
1860   HOST_WIDE_INT stack_pointer_offset;
1861
1862   /* When save_regs_using_mov is set, emit prologue using
1863      move instead of push instructions.  */
1864   bool save_regs_using_mov;
1865 };
1866
1867 /* Code model option.  */
1868 enum cmodel ix86_cmodel;
1869 /* Asm dialect.  */
1870 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1871 /* TLS dialects.  */
1872 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1873
1874 /* Which unit we are generating floating point math for.  */
1875 enum fpmath_unit ix86_fpmath;
1876
1877 /* Which cpu are we scheduling for.  */
1878 enum attr_cpu ix86_schedule;
1879
1880 /* Which cpu are we optimizing for.  */
1881 enum processor_type ix86_tune;
1882
1883 /* Which instruction set architecture to use.  */
1884 enum processor_type ix86_arch;
1885
1886 /* true if sse prefetch instruction is not NOOP.  */
1887 int x86_prefetch_sse;
1888
1889 /* ix86_regparm_string as a number */
1890 static int ix86_regparm;
1891
1892 /* -mstackrealign option */
1893 extern int ix86_force_align_arg_pointer;
1894 static const char ix86_force_align_arg_pointer_string[]
1895   = "force_align_arg_pointer";
1896
1897 static rtx (*ix86_gen_leave) (void);
1898 static rtx (*ix86_gen_pop1) (rtx);
1899 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
1900 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
1901 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
1902 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
1903 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
1904 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
1905 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
1906 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
1907 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
1908
1909 /* Preferred alignment for stack boundary in bits.  */
1910 unsigned int ix86_preferred_stack_boundary;
1911
1912 /* Alignment for incoming stack boundary in bits specified at
1913    command line.  */
1914 static unsigned int ix86_user_incoming_stack_boundary;
1915
1916 /* Default alignment for incoming stack boundary in bits.  */
1917 static unsigned int ix86_default_incoming_stack_boundary;
1918
1919 /* Alignment for incoming stack boundary in bits.  */
1920 unsigned int ix86_incoming_stack_boundary;
1921
1922 /* The abi used by target.  */
1923 enum calling_abi ix86_abi;
1924
1925 /* Values 1-5: see jump.c */
1926 int ix86_branch_cost;
1927
1928 /* Calling abi specific va_list type nodes.  */
1929 static GTY(()) tree sysv_va_list_type_node;
1930 static GTY(()) tree ms_va_list_type_node;
1931
1932 /* Variables which are this size or smaller are put in the data/bss
1933    or ldata/lbss sections.  */
1934
1935 int ix86_section_threshold = 65536;
1936
1937 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1938 char internal_label_prefix[16];
1939 int internal_label_prefix_len;
1940
1941 /* Fence to use after loop using movnt.  */
1942 tree x86_mfence;
1943
1944 /* Register class used for passing given 64bit part of the argument.
1945    These represent classes as documented by the PS ABI, with the exception
1946    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1947    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1948
1949    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1950    whenever possible (upper half does contain padding).  */
1951 enum x86_64_reg_class
1952   {
1953     X86_64_NO_CLASS,
1954     X86_64_INTEGER_CLASS,
1955     X86_64_INTEGERSI_CLASS,
1956     X86_64_SSE_CLASS,
1957     X86_64_SSESF_CLASS,
1958     X86_64_SSEDF_CLASS,
1959     X86_64_SSEUP_CLASS,
1960     X86_64_X87_CLASS,
1961     X86_64_X87UP_CLASS,
1962     X86_64_COMPLEX_X87_CLASS,
1963     X86_64_MEMORY_CLASS
1964   };
1965
1966 #define MAX_CLASSES 4
1967
1968 /* Table of constants used by fldpi, fldln2, etc....  */
1969 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1970 static bool ext_80387_constants_init = 0;
1971
1972 \f
1973 static struct machine_function * ix86_init_machine_status (void);
1974 static rtx ix86_function_value (const_tree, const_tree, bool);
1975 static bool ix86_function_value_regno_p (const unsigned int);
1976 static rtx ix86_static_chain (const_tree, bool);
1977 static int ix86_function_regparm (const_tree, const_tree);
1978 static void ix86_compute_frame_layout (struct ix86_frame *);
1979 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1980                                                  rtx, rtx, int);
1981 static void ix86_add_new_builtins (int);
1982 static rtx ix86_expand_vec_perm_builtin (tree);
1983 static tree ix86_canonical_va_list_type (tree);
1984
1985 enum ix86_function_specific_strings
1986 {
1987   IX86_FUNCTION_SPECIFIC_ARCH,
1988   IX86_FUNCTION_SPECIFIC_TUNE,
1989   IX86_FUNCTION_SPECIFIC_FPMATH,
1990   IX86_FUNCTION_SPECIFIC_MAX
1991 };
1992
1993 static char *ix86_target_string (int, int, const char *, const char *,
1994                                  const char *, bool);
1995 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
1996 static void ix86_function_specific_save (struct cl_target_option *);
1997 static void ix86_function_specific_restore (struct cl_target_option *);
1998 static void ix86_function_specific_print (FILE *, int,
1999                                           struct cl_target_option *);
2000 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2001 static bool ix86_valid_target_attribute_inner_p (tree, char *[]);
2002 static bool ix86_can_inline_p (tree, tree);
2003 static void ix86_set_current_function (tree);
2004 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2005
2006 static enum calling_abi ix86_function_abi (const_tree);
2007
2008 \f
2009 #ifndef SUBTARGET32_DEFAULT_CPU
2010 #define SUBTARGET32_DEFAULT_CPU "i386"
2011 #endif
2012
2013 /* The svr4 ABI for the i386 says that records and unions are returned
2014    in memory.  */
2015 #ifndef DEFAULT_PCC_STRUCT_RETURN
2016 #define DEFAULT_PCC_STRUCT_RETURN 1
2017 #endif
2018
2019 /* Whether -mtune= or -march= were specified */
2020 static int ix86_tune_defaulted;
2021 static int ix86_arch_specified;
2022
2023 /* Bit flags that specify the ISA we are compiling for.  */
2024 int ix86_isa_flags = TARGET_64BIT_DEFAULT | TARGET_SUBTARGET_ISA_DEFAULT;
2025
2026 /* A mask of ix86_isa_flags that includes bit X if X
2027    was set or cleared on the command line.  */
2028 static int ix86_isa_flags_explicit;
2029
2030 /* Define a set of ISAs which are available when a given ISA is
2031    enabled.  MMX and SSE ISAs are handled separately.  */
2032
2033 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
2034 #define OPTION_MASK_ISA_3DNOW_SET \
2035   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
2036
2037 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
2038 #define OPTION_MASK_ISA_SSE2_SET \
2039   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
2040 #define OPTION_MASK_ISA_SSE3_SET \
2041   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
2042 #define OPTION_MASK_ISA_SSSE3_SET \
2043   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
2044 #define OPTION_MASK_ISA_SSE4_1_SET \
2045   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
2046 #define OPTION_MASK_ISA_SSE4_2_SET \
2047   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
2048 #define OPTION_MASK_ISA_AVX_SET \
2049   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_SSE4_2_SET)
2050 #define OPTION_MASK_ISA_FMA_SET \
2051   (OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_AVX_SET)
2052
2053 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
2054    as -msse4.2.  */
2055 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
2056
2057 #define OPTION_MASK_ISA_SSE4A_SET \
2058   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
2059 #define OPTION_MASK_ISA_FMA4_SET \
2060   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_SSE4A_SET \
2061    | OPTION_MASK_ISA_AVX_SET)
2062 #define OPTION_MASK_ISA_XOP_SET \
2063   (OPTION_MASK_ISA_XOP | OPTION_MASK_ISA_FMA4_SET)
2064 #define OPTION_MASK_ISA_LWP_SET \
2065   OPTION_MASK_ISA_LWP
2066
2067 /* AES and PCLMUL need SSE2 because they use xmm registers */
2068 #define OPTION_MASK_ISA_AES_SET \
2069   (OPTION_MASK_ISA_AES | OPTION_MASK_ISA_SSE2_SET)
2070 #define OPTION_MASK_ISA_PCLMUL_SET \
2071   (OPTION_MASK_ISA_PCLMUL | OPTION_MASK_ISA_SSE2_SET)
2072
2073 #define OPTION_MASK_ISA_ABM_SET \
2074   (OPTION_MASK_ISA_ABM | OPTION_MASK_ISA_POPCNT)
2075
2076 #define OPTION_MASK_ISA_POPCNT_SET OPTION_MASK_ISA_POPCNT
2077 #define OPTION_MASK_ISA_CX16_SET OPTION_MASK_ISA_CX16
2078 #define OPTION_MASK_ISA_SAHF_SET OPTION_MASK_ISA_SAHF
2079 #define OPTION_MASK_ISA_MOVBE_SET OPTION_MASK_ISA_MOVBE
2080 #define OPTION_MASK_ISA_CRC32_SET OPTION_MASK_ISA_CRC32
2081
2082 #define OPTION_MASK_ISA_FSGSBASE_SET OPTION_MASK_ISA_FSGSBASE
2083 #define OPTION_MASK_ISA_RDRND_SET OPTION_MASK_ISA_RDRND
2084 #define OPTION_MASK_ISA_F16C_SET \
2085   (OPTION_MASK_ISA_F16C | OPTION_MASK_ISA_AVX_SET)
2086
2087 /* Define a set of ISAs which aren't available when a given ISA is
2088    disabled.  MMX and SSE ISAs are handled separately.  */
2089
2090 #define OPTION_MASK_ISA_MMX_UNSET \
2091   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
2092 #define OPTION_MASK_ISA_3DNOW_UNSET \
2093   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
2094 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
2095
2096 #define OPTION_MASK_ISA_SSE_UNSET \
2097   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
2098 #define OPTION_MASK_ISA_SSE2_UNSET \
2099   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
2100 #define OPTION_MASK_ISA_SSE3_UNSET \
2101   (OPTION_MASK_ISA_SSE3 \
2102    | OPTION_MASK_ISA_SSSE3_UNSET \
2103    | OPTION_MASK_ISA_SSE4A_UNSET )
2104 #define OPTION_MASK_ISA_SSSE3_UNSET \
2105   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
2106 #define OPTION_MASK_ISA_SSE4_1_UNSET \
2107   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
2108 #define OPTION_MASK_ISA_SSE4_2_UNSET \
2109   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_AVX_UNSET )
2110 #define OPTION_MASK_ISA_AVX_UNSET \
2111   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_FMA_UNSET \
2112    | OPTION_MASK_ISA_FMA4_UNSET | OPTION_MASK_ISA_F16C_UNSET)
2113 #define OPTION_MASK_ISA_FMA_UNSET OPTION_MASK_ISA_FMA
2114
2115 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
2116    as -mno-sse4.1. */
2117 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
2118
2119 #define OPTION_MASK_ISA_SSE4A_UNSET \
2120   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_FMA4_UNSET)
2121
2122 #define OPTION_MASK_ISA_FMA4_UNSET \
2123   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_XOP_UNSET)
2124 #define OPTION_MASK_ISA_XOP_UNSET OPTION_MASK_ISA_XOP
2125 #define OPTION_MASK_ISA_LWP_UNSET OPTION_MASK_ISA_LWP
2126
2127 #define OPTION_MASK_ISA_AES_UNSET OPTION_MASK_ISA_AES
2128 #define OPTION_MASK_ISA_PCLMUL_UNSET OPTION_MASK_ISA_PCLMUL
2129 #define OPTION_MASK_ISA_ABM_UNSET OPTION_MASK_ISA_ABM
2130 #define OPTION_MASK_ISA_POPCNT_UNSET OPTION_MASK_ISA_POPCNT
2131 #define OPTION_MASK_ISA_CX16_UNSET OPTION_MASK_ISA_CX16
2132 #define OPTION_MASK_ISA_SAHF_UNSET OPTION_MASK_ISA_SAHF
2133 #define OPTION_MASK_ISA_MOVBE_UNSET OPTION_MASK_ISA_MOVBE
2134 #define OPTION_MASK_ISA_CRC32_UNSET OPTION_MASK_ISA_CRC32
2135
2136 #define OPTION_MASK_ISA_FSGSBASE_UNSET OPTION_MASK_ISA_FSGSBASE
2137 #define OPTION_MASK_ISA_RDRND_UNSET OPTION_MASK_ISA_RDRND
2138 #define OPTION_MASK_ISA_F16C_UNSET OPTION_MASK_ISA_F16C
2139
2140 /* Vectorization library interface and handlers.  */
2141 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2142
2143 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2144 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2145
2146 /* Processor target table, indexed by processor number */
2147 struct ptt
2148 {
2149   const struct processor_costs *cost;           /* Processor costs */
2150   const int align_loop;                         /* Default alignments.  */
2151   const int align_loop_max_skip;
2152   const int align_jump;
2153   const int align_jump_max_skip;
2154   const int align_func;
2155 };
2156
2157 static const struct ptt processor_target_table[PROCESSOR_max] =
2158 {
2159   {&i386_cost, 4, 3, 4, 3, 4},
2160   {&i486_cost, 16, 15, 16, 15, 16},
2161   {&pentium_cost, 16, 7, 16, 7, 16},
2162   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2163   {&geode_cost, 0, 0, 0, 0, 0},
2164   {&k6_cost, 32, 7, 32, 7, 32},
2165   {&athlon_cost, 16, 7, 16, 7, 16},
2166   {&pentium4_cost, 0, 0, 0, 0, 0},
2167   {&k8_cost, 16, 7, 16, 7, 16},
2168   {&nocona_cost, 0, 0, 0, 0, 0},
2169   {&core2_cost, 16, 10, 16, 10, 16},
2170   {&generic32_cost, 16, 7, 16, 7, 16},
2171   {&generic64_cost, 16, 10, 16, 10, 16},
2172   {&amdfam10_cost, 32, 24, 32, 7, 32},
2173   {&bdver1_cost, 32, 24, 32, 7, 32},
2174   {&atom_cost, 16, 7, 16, 7, 16}
2175 };
2176
2177 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2178 {
2179   "generic",
2180   "i386",
2181   "i486",
2182   "pentium",
2183   "pentium-mmx",
2184   "pentiumpro",
2185   "pentium2",
2186   "pentium3",
2187   "pentium4",
2188   "pentium-m",
2189   "prescott",
2190   "nocona",
2191   "core2",
2192   "atom",
2193   "geode",
2194   "k6",
2195   "k6-2",
2196   "k6-3",
2197   "athlon",
2198   "athlon-4",
2199   "k8",
2200   "amdfam10",
2201   "bdver1"
2202 };
2203 \f
2204 /* Implement TARGET_HANDLE_OPTION.  */
2205
2206 static bool
2207 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
2208 {
2209   switch (code)
2210     {
2211     case OPT_mmmx:
2212       if (value)
2213         {
2214           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
2215           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
2216         }
2217       else
2218         {
2219           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
2220           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
2221         }
2222       return true;
2223
2224     case OPT_m3dnow:
2225       if (value)
2226         {
2227           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
2228           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
2229         }
2230       else
2231         {
2232           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
2233           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
2234         }
2235       return true;
2236
2237     case OPT_m3dnowa:
2238       return false;
2239
2240     case OPT_msse:
2241       if (value)
2242         {
2243           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
2244           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
2245         }
2246       else
2247         {
2248           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
2249           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
2250         }
2251       return true;
2252
2253     case OPT_msse2:
2254       if (value)
2255         {
2256           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
2257           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
2258         }
2259       else
2260         {
2261           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
2262           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
2263         }
2264       return true;
2265
2266     case OPT_msse3:
2267       if (value)
2268         {
2269           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
2270           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
2271         }
2272       else
2273         {
2274           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
2275           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
2276         }
2277       return true;
2278
2279     case OPT_mssse3:
2280       if (value)
2281         {
2282           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
2283           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
2284         }
2285       else
2286         {
2287           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
2288           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
2289         }
2290       return true;
2291
2292     case OPT_msse4_1:
2293       if (value)
2294         {
2295           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
2296           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
2297         }
2298       else
2299         {
2300           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
2301           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
2302         }
2303       return true;
2304
2305     case OPT_msse4_2:
2306       if (value)
2307         {
2308           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
2309           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
2310         }
2311       else
2312         {
2313           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
2314           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
2315         }
2316       return true;
2317
2318     case OPT_mavx:
2319       if (value)
2320         {
2321           ix86_isa_flags |= OPTION_MASK_ISA_AVX_SET;
2322           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_SET;
2323         }
2324       else
2325         {
2326           ix86_isa_flags &= ~OPTION_MASK_ISA_AVX_UNSET;
2327           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_UNSET;
2328         }
2329       return true;
2330
2331     case OPT_mfma:
2332       if (value)
2333         {
2334           ix86_isa_flags |= OPTION_MASK_ISA_FMA_SET;
2335           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_SET;
2336         }
2337       else
2338         {
2339           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA_UNSET;
2340           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_UNSET;
2341         }
2342       return true;
2343
2344     case OPT_msse4:
2345       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
2346       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
2347       return true;
2348
2349     case OPT_mno_sse4:
2350       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
2351       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
2352       return true;
2353
2354     case OPT_msse4a:
2355       if (value)
2356         {
2357           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
2358           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
2359         }
2360       else
2361         {
2362           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
2363           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
2364         }
2365       return true;
2366
2367     case OPT_mfma4:
2368       if (value)
2369         {
2370           ix86_isa_flags |= OPTION_MASK_ISA_FMA4_SET;
2371           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_SET;
2372         }
2373       else
2374         {
2375           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA4_UNSET;
2376           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_UNSET;
2377         }
2378       return true;
2379
2380    case OPT_mxop:
2381       if (value)
2382         {
2383           ix86_isa_flags |= OPTION_MASK_ISA_XOP_SET;
2384           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_SET;
2385         }
2386       else
2387         {
2388           ix86_isa_flags &= ~OPTION_MASK_ISA_XOP_UNSET;
2389           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_UNSET;
2390         }
2391       return true;
2392
2393    case OPT_mlwp:
2394       if (value)
2395         {
2396           ix86_isa_flags |= OPTION_MASK_ISA_LWP_SET;
2397           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_SET;
2398         }
2399       else
2400         {
2401           ix86_isa_flags &= ~OPTION_MASK_ISA_LWP_UNSET;
2402           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_UNSET;
2403         }
2404       return true;
2405
2406     case OPT_mabm:
2407       if (value)
2408         {
2409           ix86_isa_flags |= OPTION_MASK_ISA_ABM_SET;
2410           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_SET;
2411         }
2412       else
2413         {
2414           ix86_isa_flags &= ~OPTION_MASK_ISA_ABM_UNSET;
2415           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_UNSET;
2416         }
2417       return true;
2418
2419     case OPT_mpopcnt:
2420       if (value)
2421         {
2422           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT_SET;
2423           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_SET;
2424         }
2425       else
2426         {
2427           ix86_isa_flags &= ~OPTION_MASK_ISA_POPCNT_UNSET;
2428           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_UNSET;
2429         }
2430       return true;
2431
2432     case OPT_msahf:
2433       if (value)
2434         {
2435           ix86_isa_flags |= OPTION_MASK_ISA_SAHF_SET;
2436           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_SET;
2437         }
2438       else
2439         {
2440           ix86_isa_flags &= ~OPTION_MASK_ISA_SAHF_UNSET;
2441           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_UNSET;
2442         }
2443       return true;
2444
2445     case OPT_mcx16:
2446       if (value)
2447         {
2448           ix86_isa_flags |= OPTION_MASK_ISA_CX16_SET;
2449           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_SET;
2450         }
2451       else
2452         {
2453           ix86_isa_flags &= ~OPTION_MASK_ISA_CX16_UNSET;
2454           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_UNSET;
2455         }
2456       return true;
2457
2458     case OPT_mmovbe:
2459       if (value)
2460         {
2461           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE_SET;
2462           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_SET;
2463         }
2464       else
2465         {
2466           ix86_isa_flags &= ~OPTION_MASK_ISA_MOVBE_UNSET;
2467           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_UNSET;
2468         }
2469       return true;
2470
2471     case OPT_mcrc32:
2472       if (value)
2473         {
2474           ix86_isa_flags |= OPTION_MASK_ISA_CRC32_SET;
2475           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_SET;
2476         }
2477       else
2478         {
2479           ix86_isa_flags &= ~OPTION_MASK_ISA_CRC32_UNSET;
2480           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_UNSET;
2481         }
2482       return true;
2483
2484     case OPT_maes:
2485       if (value)
2486         {
2487           ix86_isa_flags |= OPTION_MASK_ISA_AES_SET;
2488           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_SET;
2489         }
2490       else
2491         {
2492           ix86_isa_flags &= ~OPTION_MASK_ISA_AES_UNSET;
2493           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_UNSET;
2494         }
2495       return true;
2496
2497     case OPT_mpclmul:
2498       if (value)
2499         {
2500           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL_SET;
2501           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_SET;
2502         }
2503       else
2504         {
2505           ix86_isa_flags &= ~OPTION_MASK_ISA_PCLMUL_UNSET;
2506           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_UNSET;
2507         }
2508       return true;
2509
2510     case OPT_mfsgsbase:
2511       if (value)
2512         {
2513           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE_SET;
2514           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_SET;
2515         }
2516       else
2517         {
2518           ix86_isa_flags &= ~OPTION_MASK_ISA_FSGSBASE_UNSET;
2519           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_UNSET;
2520         }
2521       return true;
2522
2523     case OPT_mrdrnd:
2524       if (value)
2525         {
2526           ix86_isa_flags |= OPTION_MASK_ISA_RDRND_SET;
2527           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_SET;
2528         }
2529       else
2530         {
2531           ix86_isa_flags &= ~OPTION_MASK_ISA_RDRND_UNSET;
2532           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_UNSET;
2533         }
2534       return true;
2535
2536     case OPT_mf16c:
2537       if (value)
2538         {
2539           ix86_isa_flags |= OPTION_MASK_ISA_F16C_SET;
2540           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_SET;
2541         }
2542       else
2543         {
2544           ix86_isa_flags &= ~OPTION_MASK_ISA_F16C_UNSET;
2545           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_UNSET;
2546         }
2547       return true;
2548
2549     default:
2550       return true;
2551     }
2552 }
2553 \f
2554 /* Return a string that documents the current -m options.  The caller is
2555    responsible for freeing the string.  */
2556
2557 static char *
2558 ix86_target_string (int isa, int flags, const char *arch, const char *tune,
2559                     const char *fpmath, bool add_nl_p)
2560 {
2561   struct ix86_target_opts
2562   {
2563     const char *option;         /* option string */
2564     int mask;                   /* isa mask options */
2565   };
2566
2567   /* This table is ordered so that options like -msse4.2 that imply
2568      preceding options while match those first.  */
2569   static struct ix86_target_opts isa_opts[] =
2570   {
2571     { "-m64",           OPTION_MASK_ISA_64BIT },
2572     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2573     { "-mfma",          OPTION_MASK_ISA_FMA },
2574     { "-mxop",          OPTION_MASK_ISA_XOP },
2575     { "-mlwp",          OPTION_MASK_ISA_LWP },
2576     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2577     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2578     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2579     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2580     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2581     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2582     { "-msse",          OPTION_MASK_ISA_SSE },
2583     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2584     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2585     { "-mmmx",          OPTION_MASK_ISA_MMX },
2586     { "-mabm",          OPTION_MASK_ISA_ABM },
2587     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2588     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2589     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2590     { "-maes",          OPTION_MASK_ISA_AES },
2591     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2592     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2593     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2594     { "-mf16c",         OPTION_MASK_ISA_F16C },
2595   };
2596
2597   /* Flag options.  */
2598   static struct ix86_target_opts flag_opts[] =
2599   {
2600     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2601     { "-m80387",                        MASK_80387 },
2602     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2603     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2604     { "-mcld",                          MASK_CLD },
2605     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2606     { "-mieee-fp",                      MASK_IEEE_FP },
2607     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2608     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2609     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2610     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2611     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2612     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2613     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2614     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2615     { "-mrecip",                        MASK_RECIP },
2616     { "-mrtd",                          MASK_RTD },
2617     { "-msseregparm",                   MASK_SSEREGPARM },
2618     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2619     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2620   };
2621
2622   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2623
2624   char isa_other[40];
2625   char target_other[40];
2626   unsigned num = 0;
2627   unsigned i, j;
2628   char *ret;
2629   char *ptr;
2630   size_t len;
2631   size_t line_len;
2632   size_t sep_len;
2633
2634   memset (opts, '\0', sizeof (opts));
2635
2636   /* Add -march= option.  */
2637   if (arch)
2638     {
2639       opts[num][0] = "-march=";
2640       opts[num++][1] = arch;
2641     }
2642
2643   /* Add -mtune= option.  */
2644   if (tune)
2645     {
2646       opts[num][0] = "-mtune=";
2647       opts[num++][1] = tune;
2648     }
2649
2650   /* Pick out the options in isa options.  */
2651   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2652     {
2653       if ((isa & isa_opts[i].mask) != 0)
2654         {
2655           opts[num++][0] = isa_opts[i].option;
2656           isa &= ~ isa_opts[i].mask;
2657         }
2658     }
2659
2660   if (isa && add_nl_p)
2661     {
2662       opts[num++][0] = isa_other;
2663       sprintf (isa_other, "(other isa: %#x)", isa);
2664     }
2665
2666   /* Add flag options.  */
2667   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2668     {
2669       if ((flags & flag_opts[i].mask) != 0)
2670         {
2671           opts[num++][0] = flag_opts[i].option;
2672           flags &= ~ flag_opts[i].mask;
2673         }
2674     }
2675
2676   if (flags && add_nl_p)
2677     {
2678       opts[num++][0] = target_other;
2679       sprintf (target_other, "(other flags: %#x)", flags);
2680     }
2681
2682   /* Add -fpmath= option.  */
2683   if (fpmath)
2684     {
2685       opts[num][0] = "-mfpmath=";
2686       opts[num++][1] = fpmath;
2687     }
2688
2689   /* Any options?  */
2690   if (num == 0)
2691     return NULL;
2692
2693   gcc_assert (num < ARRAY_SIZE (opts));
2694
2695   /* Size the string.  */
2696   len = 0;
2697   sep_len = (add_nl_p) ? 3 : 1;
2698   for (i = 0; i < num; i++)
2699     {
2700       len += sep_len;
2701       for (j = 0; j < 2; j++)
2702         if (opts[i][j])
2703           len += strlen (opts[i][j]);
2704     }
2705
2706   /* Build the string.  */
2707   ret = ptr = (char *) xmalloc (len);
2708   line_len = 0;
2709
2710   for (i = 0; i < num; i++)
2711     {
2712       size_t len2[2];
2713
2714       for (j = 0; j < 2; j++)
2715         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
2716
2717       if (i != 0)
2718         {
2719           *ptr++ = ' ';
2720           line_len++;
2721
2722           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
2723             {
2724               *ptr++ = '\\';
2725               *ptr++ = '\n';
2726               line_len = 0;
2727             }
2728         }
2729
2730       for (j = 0; j < 2; j++)
2731         if (opts[i][j])
2732           {
2733             memcpy (ptr, opts[i][j], len2[j]);
2734             ptr += len2[j];
2735             line_len += len2[j];
2736           }
2737     }
2738
2739   *ptr = '\0';
2740   gcc_assert (ret + len >= ptr);
2741
2742   return ret;
2743 }
2744
2745 /* Return TRUE if software prefetching is beneficial for the
2746    given CPU. */
2747
2748 static bool
2749 software_prefetching_beneficial_p (void)
2750 {
2751   switch (ix86_tune)
2752     {
2753     case PROCESSOR_GEODE:
2754     case PROCESSOR_K6:
2755     case PROCESSOR_ATHLON:
2756     case PROCESSOR_K8:
2757     case PROCESSOR_AMDFAM10:
2758       return true;
2759
2760     default:
2761       return false;
2762     }
2763 }
2764
2765 /* Return true, if profiling code should be emitted before
2766    prologue. Otherwise it returns false.
2767    Note: For x86 with "hotfix" it is sorried.  */
2768 static bool
2769 ix86_profile_before_prologue (void)
2770 {
2771   return flag_fentry != 0;
2772 }
2773
2774 /* Function that is callable from the debugger to print the current
2775    options.  */
2776 void
2777 ix86_debug_options (void)
2778 {
2779   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
2780                                    ix86_arch_string, ix86_tune_string,
2781                                    ix86_fpmath_string, true);
2782
2783   if (opts)
2784     {
2785       fprintf (stderr, "%s\n\n", opts);
2786       free (opts);
2787     }
2788   else
2789     fputs ("<no options>\n\n", stderr);
2790
2791   return;
2792 }
2793 \f
2794 /* Sometimes certain combinations of command options do not make
2795    sense on a particular target machine.  You can define a macro
2796    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
2797    defined, is executed once just after all the command options have
2798    been parsed.
2799
2800    Don't use this macro to turn on various extra optimizations for
2801    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
2802
2803 void
2804 override_options (bool main_args_p)
2805 {
2806   int i;
2807   unsigned int ix86_arch_mask, ix86_tune_mask;
2808   const bool ix86_tune_specified = (ix86_tune_string != NULL);
2809   const char *prefix;
2810   const char *suffix;
2811   const char *sw;
2812
2813   /* Comes from final.c -- no real reason to change it.  */
2814 #define MAX_CODE_ALIGN 16
2815
2816   enum pta_flags
2817     {
2818       PTA_SSE = 1 << 0,
2819       PTA_SSE2 = 1 << 1,
2820       PTA_SSE3 = 1 << 2,
2821       PTA_MMX = 1 << 3,
2822       PTA_PREFETCH_SSE = 1 << 4,
2823       PTA_3DNOW = 1 << 5,
2824       PTA_3DNOW_A = 1 << 6,
2825       PTA_64BIT = 1 << 7,
2826       PTA_SSSE3 = 1 << 8,
2827       PTA_CX16 = 1 << 9,
2828       PTA_POPCNT = 1 << 10,
2829       PTA_ABM = 1 << 11,
2830       PTA_SSE4A = 1 << 12,
2831       PTA_NO_SAHF = 1 << 13,
2832       PTA_SSE4_1 = 1 << 14,
2833       PTA_SSE4_2 = 1 << 15,
2834       PTA_AES = 1 << 16,
2835       PTA_PCLMUL = 1 << 17,
2836       PTA_AVX = 1 << 18,
2837       PTA_FMA = 1 << 19,
2838       PTA_MOVBE = 1 << 20,
2839       PTA_FMA4 = 1 << 21,
2840       PTA_XOP = 1 << 22,
2841       PTA_LWP = 1 << 23,
2842       PTA_FSGSBASE = 1 << 24,
2843       PTA_RDRND = 1 << 25,
2844       PTA_F16C = 1 << 26
2845     };
2846
2847   static struct pta
2848     {
2849       const char *const name;           /* processor name or nickname.  */
2850       const enum processor_type processor;
2851       const enum attr_cpu schedule;
2852       const unsigned /*enum pta_flags*/ flags;
2853     }
2854   const processor_alias_table[] =
2855     {
2856       {"i386", PROCESSOR_I386, CPU_NONE, 0},
2857       {"i486", PROCESSOR_I486, CPU_NONE, 0},
2858       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2859       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2860       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
2861       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
2862       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2863       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2864       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
2865       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2866       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2867       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
2868       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2869         PTA_MMX | PTA_SSE},
2870       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2871         PTA_MMX | PTA_SSE},
2872       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2873         PTA_MMX | PTA_SSE | PTA_SSE2},
2874       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
2875         PTA_MMX |PTA_SSE | PTA_SSE2},
2876       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
2877         PTA_MMX | PTA_SSE | PTA_SSE2},
2878       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
2879         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2880       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
2881         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2882         | PTA_CX16 | PTA_NO_SAHF},
2883       {"core2", PROCESSOR_CORE2, CPU_CORE2,
2884         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2885         | PTA_SSSE3 | PTA_CX16},
2886       {"atom", PROCESSOR_ATOM, CPU_ATOM,
2887         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2888         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
2889       {"geode", PROCESSOR_GEODE, CPU_GEODE,
2890         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
2891       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
2892       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2893       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2894       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
2895         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2896       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
2897         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2898       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
2899         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2900       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
2901         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2902       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
2903         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2904       {"x86-64", PROCESSOR_K8, CPU_K8,
2905         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
2906       {"k8", PROCESSOR_K8, CPU_K8,
2907         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2908         | PTA_SSE2 | PTA_NO_SAHF},
2909       {"k8-sse3", PROCESSOR_K8, CPU_K8,
2910         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2911         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2912       {"opteron", PROCESSOR_K8, CPU_K8,
2913         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2914         | PTA_SSE2 | PTA_NO_SAHF},
2915       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
2916         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2917         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2918       {"athlon64", PROCESSOR_K8, CPU_K8,
2919         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2920         | PTA_SSE2 | PTA_NO_SAHF},
2921       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
2922         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2923         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2924       {"athlon-fx", PROCESSOR_K8, CPU_K8,
2925         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2926         | PTA_SSE2 | PTA_NO_SAHF},
2927       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2928         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2929         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2930       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2931         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2932         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2933       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
2934         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2935         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM
2936         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AES
2937         | PTA_PCLMUL | PTA_AVX | PTA_FMA4 | PTA_XOP | PTA_LWP},
2938       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
2939         0 /* flags are only used for -march switch.  */ },
2940       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
2941         PTA_64BIT /* flags are only used for -march switch.  */ },
2942     };
2943
2944   int const pta_size = ARRAY_SIZE (processor_alias_table);
2945
2946   /* Set up prefix/suffix so the error messages refer to either the command
2947      line argument, or the attribute(target).  */
2948   if (main_args_p)
2949     {
2950       prefix = "-m";
2951       suffix = "";
2952       sw = "switch";
2953     }
2954   else
2955     {
2956       prefix = "option(\"";
2957       suffix = "\")";
2958       sw = "attribute";
2959     }
2960
2961 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2962   SUBTARGET_OVERRIDE_OPTIONS;
2963 #endif
2964
2965 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2966   SUBSUBTARGET_OVERRIDE_OPTIONS;
2967 #endif
2968
2969   /* -fPIC is the default for x86_64.  */
2970   if (TARGET_MACHO && TARGET_64BIT)
2971     flag_pic = 2;
2972
2973   /* Set the default values for switches whose default depends on TARGET_64BIT
2974      in case they weren't overwritten by command line options.  */
2975   if (TARGET_64BIT)
2976     {
2977       if (flag_zee == 2)
2978         flag_zee = 1;
2979       /* Mach-O doesn't support omitting the frame pointer for now.  */
2980       if (flag_omit_frame_pointer == 2)
2981         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
2982       if (flag_asynchronous_unwind_tables == 2)
2983         flag_asynchronous_unwind_tables = 1;
2984       if (flag_pcc_struct_return == 2)
2985         flag_pcc_struct_return = 0;
2986     }
2987   else
2988     {
2989       if (flag_zee == 2)
2990         flag_zee = 0;
2991       if (flag_omit_frame_pointer == 2)
2992         flag_omit_frame_pointer = 0;
2993       if (flag_asynchronous_unwind_tables == 2)
2994         flag_asynchronous_unwind_tables = 0;
2995       if (flag_pcc_struct_return == 2)
2996         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
2997     }
2998
2999   /* Need to check -mtune=generic first.  */
3000   if (ix86_tune_string)
3001     {
3002       if (!strcmp (ix86_tune_string, "generic")
3003           || !strcmp (ix86_tune_string, "i686")
3004           /* As special support for cross compilers we read -mtune=native
3005              as -mtune=generic.  With native compilers we won't see the
3006              -mtune=native, as it was changed by the driver.  */
3007           || !strcmp (ix86_tune_string, "native"))
3008         {
3009           if (TARGET_64BIT)
3010             ix86_tune_string = "generic64";
3011           else
3012             ix86_tune_string = "generic32";
3013         }
3014       /* If this call is for setting the option attribute, allow the
3015          generic32/generic64 that was previously set.  */
3016       else if (!main_args_p
3017                && (!strcmp (ix86_tune_string, "generic32")
3018                    || !strcmp (ix86_tune_string, "generic64")))
3019         ;
3020       else if (!strncmp (ix86_tune_string, "generic", 7))
3021         error ("bad value (%s) for %stune=%s %s",
3022                ix86_tune_string, prefix, suffix, sw);
3023       else if (!strcmp (ix86_tune_string, "x86-64"))
3024         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated.  Use "
3025                  "%stune=k8%s or %stune=generic%s instead as appropriate.",
3026                  prefix, suffix, prefix, suffix, prefix, suffix);
3027     }
3028   else
3029     {
3030       if (ix86_arch_string)
3031         ix86_tune_string = ix86_arch_string;
3032       if (!ix86_tune_string)
3033         {
3034           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3035           ix86_tune_defaulted = 1;
3036         }
3037
3038       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3039          need to use a sensible tune option.  */
3040       if (!strcmp (ix86_tune_string, "generic")
3041           || !strcmp (ix86_tune_string, "x86-64")
3042           || !strcmp (ix86_tune_string, "i686"))
3043         {
3044           if (TARGET_64BIT)
3045             ix86_tune_string = "generic64";
3046           else
3047             ix86_tune_string = "generic32";
3048         }
3049     }
3050
3051   if (ix86_stringop_string)
3052     {
3053       if (!strcmp (ix86_stringop_string, "rep_byte"))
3054         stringop_alg = rep_prefix_1_byte;
3055       else if (!strcmp (ix86_stringop_string, "libcall"))
3056         stringop_alg = libcall;
3057       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
3058         stringop_alg = rep_prefix_4_byte;
3059       else if (!strcmp (ix86_stringop_string, "rep_8byte")
3060                && TARGET_64BIT)
3061         /* rep; movq isn't available in 32-bit code.  */
3062         stringop_alg = rep_prefix_8_byte;
3063       else if (!strcmp (ix86_stringop_string, "byte_loop"))
3064         stringop_alg = loop_1_byte;
3065       else if (!strcmp (ix86_stringop_string, "loop"))
3066         stringop_alg = loop;
3067       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
3068         stringop_alg = unrolled_loop;
3069       else
3070         error ("bad value (%s) for %sstringop-strategy=%s %s",
3071                ix86_stringop_string, prefix, suffix, sw);
3072     }
3073
3074   if (!ix86_arch_string)
3075     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3076   else
3077     ix86_arch_specified = 1;
3078
3079   /* Validate -mabi= value.  */
3080   if (ix86_abi_string)
3081     {
3082       if (strcmp (ix86_abi_string, "sysv") == 0)
3083         ix86_abi = SYSV_ABI;
3084       else if (strcmp (ix86_abi_string, "ms") == 0)
3085         ix86_abi = MS_ABI;
3086       else
3087         error ("unknown ABI (%s) for %sabi=%s %s",
3088                ix86_abi_string, prefix, suffix, sw);
3089     }
3090   else
3091     ix86_abi = DEFAULT_ABI;
3092
3093   if (ix86_cmodel_string != 0)
3094     {
3095       if (!strcmp (ix86_cmodel_string, "small"))
3096         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3097       else if (!strcmp (ix86_cmodel_string, "medium"))
3098         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
3099       else if (!strcmp (ix86_cmodel_string, "large"))
3100         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
3101       else if (flag_pic)
3102         error ("code model %s does not support PIC mode", ix86_cmodel_string);
3103       else if (!strcmp (ix86_cmodel_string, "32"))
3104         ix86_cmodel = CM_32;
3105       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
3106         ix86_cmodel = CM_KERNEL;
3107       else
3108         error ("bad value (%s) for %scmodel=%s %s",
3109                ix86_cmodel_string, prefix, suffix, sw);
3110     }
3111   else
3112     {
3113       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3114          use of rip-relative addressing.  This eliminates fixups that
3115          would otherwise be needed if this object is to be placed in a
3116          DLL, and is essentially just as efficient as direct addressing.  */
3117       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3118         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3119       else if (TARGET_64BIT)
3120         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3121       else
3122         ix86_cmodel = CM_32;
3123     }
3124   if (ix86_asm_string != 0)
3125     {
3126       if (! TARGET_MACHO
3127           && !strcmp (ix86_asm_string, "intel"))
3128         ix86_asm_dialect = ASM_INTEL;
3129       else if (!strcmp (ix86_asm_string, "att"))
3130         ix86_asm_dialect = ASM_ATT;
3131       else
3132         error ("bad value (%s) for %sasm=%s %s",
3133                ix86_asm_string, prefix, suffix, sw);
3134     }
3135   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
3136     error ("code model %qs not supported in the %s bit mode",
3137            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
3138   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3139     sorry ("%i-bit mode not compiled in",
3140            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3141
3142   for (i = 0; i < pta_size; i++)
3143     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3144       {
3145         ix86_schedule = processor_alias_table[i].schedule;
3146         ix86_arch = processor_alias_table[i].processor;
3147         /* Default cpu tuning to the architecture.  */
3148         ix86_tune = ix86_arch;
3149
3150         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3151           error ("CPU you selected does not support x86-64 "
3152                  "instruction set");
3153
3154         if (processor_alias_table[i].flags & PTA_MMX
3155             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3156           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3157         if (processor_alias_table[i].flags & PTA_3DNOW
3158             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3159           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3160         if (processor_alias_table[i].flags & PTA_3DNOW_A
3161             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3162           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3163         if (processor_alias_table[i].flags & PTA_SSE
3164             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3165           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3166         if (processor_alias_table[i].flags & PTA_SSE2
3167             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3168           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3169         if (processor_alias_table[i].flags & PTA_SSE3
3170             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3171           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3172         if (processor_alias_table[i].flags & PTA_SSSE3
3173             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3174           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3175         if (processor_alias_table[i].flags & PTA_SSE4_1
3176             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3177           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3178         if (processor_alias_table[i].flags & PTA_SSE4_2
3179             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3180           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3181         if (processor_alias_table[i].flags & PTA_AVX
3182             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3183           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3184         if (processor_alias_table[i].flags & PTA_FMA
3185             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3186           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3187         if (processor_alias_table[i].flags & PTA_SSE4A
3188             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3189           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3190         if (processor_alias_table[i].flags & PTA_FMA4
3191             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3192           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3193         if (processor_alias_table[i].flags & PTA_XOP
3194             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3195           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3196         if (processor_alias_table[i].flags & PTA_LWP
3197             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3198           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3199         if (processor_alias_table[i].flags & PTA_ABM
3200             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3201           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3202         if (processor_alias_table[i].flags & PTA_CX16
3203             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3204           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3205         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3206             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3207           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3208         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3209             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3210           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3211         if (processor_alias_table[i].flags & PTA_MOVBE
3212             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3213           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3214         if (processor_alias_table[i].flags & PTA_AES
3215             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3216           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3217         if (processor_alias_table[i].flags & PTA_PCLMUL
3218             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3219           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3220         if (processor_alias_table[i].flags & PTA_FSGSBASE
3221             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3222           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3223         if (processor_alias_table[i].flags & PTA_RDRND
3224             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3225           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3226         if (processor_alias_table[i].flags & PTA_F16C
3227             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3228           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3229         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3230           x86_prefetch_sse = true;
3231
3232         break;
3233       }
3234
3235   if (!strcmp (ix86_arch_string, "generic"))
3236     error ("generic CPU can be used only for %stune=%s %s",
3237            prefix, suffix, sw);
3238   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3239     error ("bad value (%s) for %sarch=%s %s",
3240            ix86_arch_string, prefix, suffix, sw);
3241
3242   ix86_arch_mask = 1u << ix86_arch;
3243   for (i = 0; i < X86_ARCH_LAST; ++i)
3244     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3245
3246   for (i = 0; i < pta_size; i++)
3247     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3248       {
3249         ix86_schedule = processor_alias_table[i].schedule;
3250         ix86_tune = processor_alias_table[i].processor;
3251         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3252           {
3253             if (ix86_tune_defaulted)
3254               {
3255                 ix86_tune_string = "x86-64";
3256                 for (i = 0; i < pta_size; i++)
3257                   if (! strcmp (ix86_tune_string,
3258                                 processor_alias_table[i].name))
3259                     break;
3260                 ix86_schedule = processor_alias_table[i].schedule;
3261                 ix86_tune = processor_alias_table[i].processor;
3262               }
3263             else
3264               error ("CPU you selected does not support x86-64 "
3265                      "instruction set");
3266           }
3267         /* Intel CPUs have always interpreted SSE prefetch instructions as
3268            NOPs; so, we can enable SSE prefetch instructions even when
3269            -mtune (rather than -march) points us to a processor that has them.
3270            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3271            higher processors.  */
3272         if (TARGET_CMOVE
3273             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3274           x86_prefetch_sse = true;
3275         break;
3276       }
3277
3278   if (ix86_tune_specified && i == pta_size)
3279     error ("bad value (%s) for %stune=%s %s",
3280            ix86_tune_string, prefix, suffix, sw);
3281
3282   ix86_tune_mask = 1u << ix86_tune;
3283   for (i = 0; i < X86_TUNE_LAST; ++i)
3284     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3285
3286   if (optimize_size)
3287     ix86_cost = &ix86_size_cost;
3288   else
3289     ix86_cost = processor_target_table[ix86_tune].cost;
3290
3291   /* Arrange to set up i386_stack_locals for all functions.  */
3292   init_machine_status = ix86_init_machine_status;
3293
3294   /* Validate -mregparm= value.  */
3295   if (ix86_regparm_string)
3296     {
3297       if (TARGET_64BIT)
3298         warning (0, "%sregparm%s is ignored in 64-bit mode", prefix, suffix);
3299       i = atoi (ix86_regparm_string);
3300       if (i < 0 || i > REGPARM_MAX)
3301         error ("%sregparm=%d%s is not between 0 and %d",
3302                prefix, i, suffix, REGPARM_MAX);
3303       else
3304         ix86_regparm = i;
3305     }
3306   if (TARGET_64BIT)
3307     ix86_regparm = REGPARM_MAX;
3308
3309   /* If the user has provided any of the -malign-* options,
3310      warn and use that value only if -falign-* is not set.
3311      Remove this code in GCC 3.2 or later.  */
3312   if (ix86_align_loops_string)
3313     {
3314       warning (0, "%salign-loops%s is obsolete, use -falign-loops%s",
3315                prefix, suffix, suffix);
3316       if (align_loops == 0)
3317         {
3318           i = atoi (ix86_align_loops_string);
3319           if (i < 0 || i > MAX_CODE_ALIGN)
3320             error ("%salign-loops=%d%s is not between 0 and %d",
3321                    prefix, i, suffix, MAX_CODE_ALIGN);
3322           else
3323             align_loops = 1 << i;
3324         }
3325     }
3326
3327   if (ix86_align_jumps_string)
3328     {
3329       warning (0, "%salign-jumps%s is obsolete, use -falign-jumps%s",
3330                prefix, suffix, suffix);
3331       if (align_jumps == 0)
3332         {
3333           i = atoi (ix86_align_jumps_string);
3334           if (i < 0 || i > MAX_CODE_ALIGN)
3335             error ("%salign-loops=%d%s is not between 0 and %d",
3336                    prefix, i, suffix, MAX_CODE_ALIGN);
3337           else
3338             align_jumps = 1 << i;
3339         }
3340     }
3341
3342   if (ix86_align_funcs_string)
3343     {
3344       warning (0, "%salign-functions%s is obsolete, use -falign-functions%s",
3345                prefix, suffix, suffix);
3346       if (align_functions == 0)
3347         {
3348           i = atoi (ix86_align_funcs_string);
3349           if (i < 0 || i > MAX_CODE_ALIGN)
3350             error ("%salign-loops=%d%s is not between 0 and %d",
3351                    prefix, i, suffix, MAX_CODE_ALIGN);
3352           else
3353             align_functions = 1 << i;
3354         }
3355     }
3356
3357   /* Default align_* from the processor table.  */
3358   if (align_loops == 0)
3359     {
3360       align_loops = processor_target_table[ix86_tune].align_loop;
3361       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3362     }
3363   if (align_jumps == 0)
3364     {
3365       align_jumps = processor_target_table[ix86_tune].align_jump;
3366       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3367     }
3368   if (align_functions == 0)
3369     {
3370       align_functions = processor_target_table[ix86_tune].align_func;
3371     }
3372
3373   /* Validate -mbranch-cost= value, or provide default.  */
3374   ix86_branch_cost = ix86_cost->branch_cost;
3375   if (ix86_branch_cost_string)
3376     {
3377       i = atoi (ix86_branch_cost_string);
3378       if (i < 0 || i > 5)
3379         error ("%sbranch-cost=%d%s is not between 0 and 5", prefix, i, suffix);
3380       else
3381         ix86_branch_cost = i;
3382     }
3383   if (ix86_section_threshold_string)
3384     {
3385       i = atoi (ix86_section_threshold_string);
3386       if (i < 0)
3387         error ("%slarge-data-threshold=%d%s is negative", prefix, i, suffix);
3388       else
3389         ix86_section_threshold = i;
3390     }
3391
3392   if (ix86_tls_dialect_string)
3393     {
3394       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
3395         ix86_tls_dialect = TLS_DIALECT_GNU;
3396       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
3397         ix86_tls_dialect = TLS_DIALECT_GNU2;
3398       else
3399         error ("bad value (%s) for %stls-dialect=%s %s",
3400                ix86_tls_dialect_string, prefix, suffix, sw);
3401     }
3402
3403   if (ix87_precision_string)
3404     {
3405       i = atoi (ix87_precision_string);
3406       if (i != 32 && i != 64 && i != 80)
3407         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
3408     }
3409
3410   if (TARGET_64BIT)
3411     {
3412       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3413
3414       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3415          explicitly disable any of these.  In particular, disabling SSE and
3416          MMX for kernel code is extremely useful.  */
3417       if (!ix86_arch_specified)
3418       ix86_isa_flags
3419         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3420              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3421
3422       if (TARGET_RTD)
3423         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3424     }
3425   else
3426     {
3427       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3428
3429       if (!ix86_arch_specified)
3430       ix86_isa_flags
3431         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3432
3433       /* i386 ABI does not specify red zone.  It still makes sense to use it
3434          when programmer takes care to stack from being destroyed.  */
3435       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3436         target_flags |= MASK_NO_RED_ZONE;
3437     }
3438
3439   /* Keep nonleaf frame pointers.  */
3440   if (flag_omit_frame_pointer)
3441     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3442   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3443     flag_omit_frame_pointer = 1;
3444
3445   /* If we're doing fast math, we don't care about comparison order
3446      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3447   if (flag_finite_math_only)
3448     target_flags &= ~MASK_IEEE_FP;
3449
3450   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3451      since the insns won't need emulation.  */
3452   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3453     target_flags &= ~MASK_NO_FANCY_MATH_387;
3454
3455   /* Likewise, if the target doesn't have a 387, or we've specified
3456      software floating point, don't use 387 inline intrinsics.  */
3457   if (!TARGET_80387)
3458     target_flags |= MASK_NO_FANCY_MATH_387;
3459
3460   /* Turn on MMX builtins for -msse.  */
3461   if (TARGET_SSE)
3462     {
3463       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3464       x86_prefetch_sse = true;
3465     }
3466
3467   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3468   if (TARGET_SSE4_2 || TARGET_ABM)
3469     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3470
3471   /* Validate -mpreferred-stack-boundary= value or default it to
3472      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3473   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3474   if (ix86_preferred_stack_boundary_string)
3475     {
3476       i = atoi (ix86_preferred_stack_boundary_string);
3477       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3478         error ("%spreferred-stack-boundary=%d%s is not between %d and 12",
3479                prefix, i, suffix, TARGET_64BIT ? 4 : 2);
3480       else
3481         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
3482     }
3483
3484   /* Set the default value for -mstackrealign.  */
3485   if (ix86_force_align_arg_pointer == -1)
3486     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3487
3488   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3489
3490   /* Validate -mincoming-stack-boundary= value or default it to
3491      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3492   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3493   if (ix86_incoming_stack_boundary_string)
3494     {
3495       i = atoi (ix86_incoming_stack_boundary_string);
3496       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3497         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3498                i, TARGET_64BIT ? 4 : 2);
3499       else
3500         {
3501           ix86_user_incoming_stack_boundary = (1 << i) * BITS_PER_UNIT;
3502           ix86_incoming_stack_boundary
3503             = ix86_user_incoming_stack_boundary;
3504         }
3505     }
3506
3507   /* Accept -msseregparm only if at least SSE support is enabled.  */
3508   if (TARGET_SSEREGPARM
3509       && ! TARGET_SSE)
3510     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3511
3512   ix86_fpmath = TARGET_FPMATH_DEFAULT;
3513   if (ix86_fpmath_string != 0)
3514     {
3515       if (! strcmp (ix86_fpmath_string, "387"))
3516         ix86_fpmath = FPMATH_387;
3517       else if (! strcmp (ix86_fpmath_string, "sse"))
3518         {
3519           if (!TARGET_SSE)
3520             {
3521               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3522               ix86_fpmath = FPMATH_387;
3523             }
3524           else
3525             ix86_fpmath = FPMATH_SSE;
3526         }
3527       else if (! strcmp (ix86_fpmath_string, "387,sse")
3528                || ! strcmp (ix86_fpmath_string, "387+sse")
3529                || ! strcmp (ix86_fpmath_string, "sse,387")
3530                || ! strcmp (ix86_fpmath_string, "sse+387")
3531                || ! strcmp (ix86_fpmath_string, "both"))
3532         {
3533           if (!TARGET_SSE)
3534             {
3535               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3536               ix86_fpmath = FPMATH_387;
3537             }
3538           else if (!TARGET_80387)
3539             {
3540               warning (0, "387 instruction set disabled, using SSE arithmetics");
3541               ix86_fpmath = FPMATH_SSE;
3542             }
3543           else
3544             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
3545         }
3546       else
3547         error ("bad value (%s) for %sfpmath=%s %s",
3548                ix86_fpmath_string, prefix, suffix, sw);
3549     }
3550
3551   /* If the i387 is disabled, then do not return values in it. */
3552   if (!TARGET_80387)
3553     target_flags &= ~MASK_FLOAT_RETURNS;
3554
3555   /* Use external vectorized library in vectorizing intrinsics.  */
3556   if (ix86_veclibabi_string)
3557     {
3558       if (strcmp (ix86_veclibabi_string, "svml") == 0)
3559         ix86_veclib_handler = ix86_veclibabi_svml;
3560       else if (strcmp (ix86_veclibabi_string, "acml") == 0)
3561         ix86_veclib_handler = ix86_veclibabi_acml;
3562       else
3563         error ("unknown vectorization library ABI type (%s) for "
3564                "%sveclibabi=%s %s", ix86_veclibabi_string,
3565                prefix, suffix, sw);
3566     }
3567
3568   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
3569       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3570       && !optimize_size)
3571     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3572
3573   /* ??? Unwind info is not correct around the CFG unless either a frame
3574      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3575      unwind info generation to be aware of the CFG and propagating states
3576      around edges.  */
3577   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3578        || flag_exceptions || flag_non_call_exceptions)
3579       && flag_omit_frame_pointer
3580       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3581     {
3582       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3583         warning (0, "unwind tables currently require either a frame pointer "
3584                  "or %saccumulate-outgoing-args%s for correctness",
3585                  prefix, suffix);
3586       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3587     }
3588
3589   /* If stack probes are required, the space used for large function
3590      arguments on the stack must also be probed, so enable
3591      -maccumulate-outgoing-args so this happens in the prologue.  */
3592   if (TARGET_STACK_PROBE
3593       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3594     {
3595       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3596         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3597                  "for correctness", prefix, suffix);
3598       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3599     }
3600
3601   /* For sane SSE instruction set generation we need fcomi instruction.
3602      It is safe to enable all CMOVE instructions.  */
3603   if (TARGET_SSE)
3604     TARGET_CMOVE = 1;
3605
3606   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3607   {
3608     char *p;
3609     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3610     p = strchr (internal_label_prefix, 'X');
3611     internal_label_prefix_len = p - internal_label_prefix;
3612     *p = '\0';
3613   }
3614
3615   /* When scheduling description is not available, disable scheduler pass
3616      so it won't slow down the compilation and make x87 code slower.  */
3617   if (!TARGET_SCHEDULE)
3618     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3619
3620   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
3621     set_param_value ("simultaneous-prefetches",
3622                      ix86_cost->simultaneous_prefetches);
3623   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
3624     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
3625   if (!PARAM_SET_P (PARAM_L1_CACHE_SIZE))
3626     set_param_value ("l1-cache-size", ix86_cost->l1_cache_size);
3627   if (!PARAM_SET_P (PARAM_L2_CACHE_SIZE))
3628     set_param_value ("l2-cache-size", ix86_cost->l2_cache_size);
3629
3630   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
3631   if (flag_prefetch_loop_arrays < 0
3632       && HAVE_prefetch
3633       && optimize >= 3
3634       && software_prefetching_beneficial_p ())
3635     flag_prefetch_loop_arrays = 1;
3636
3637   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3638      can be optimized to ap = __builtin_next_arg (0).  */
3639   if (!TARGET_64BIT)
3640     targetm.expand_builtin_va_start = NULL;
3641
3642   if (TARGET_64BIT)
3643     {
3644       ix86_gen_leave = gen_leave_rex64;
3645       ix86_gen_pop1 = gen_popdi1;
3646       ix86_gen_add3 = gen_adddi3;
3647       ix86_gen_sub3 = gen_subdi3;
3648       ix86_gen_sub3_carry = gen_subdi3_carry;
3649       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
3650       ix86_gen_monitor = gen_sse3_monitor64;
3651       ix86_gen_andsp = gen_anddi3;
3652       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_64;
3653       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
3654       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
3655     }
3656   else
3657     {
3658       ix86_gen_leave = gen_leave;
3659       ix86_gen_pop1 = gen_popsi1;
3660       ix86_gen_add3 = gen_addsi3;
3661       ix86_gen_sub3 = gen_subsi3;
3662       ix86_gen_sub3_carry = gen_subsi3_carry;
3663       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
3664       ix86_gen_monitor = gen_sse3_monitor;
3665       ix86_gen_andsp = gen_andsi3;
3666       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_32;
3667       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
3668       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
3669     }
3670
3671 #ifdef USE_IX86_CLD
3672   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
3673   if (!TARGET_64BIT)
3674     target_flags |= MASK_CLD & ~target_flags_explicit;
3675 #endif
3676
3677   if (!TARGET_64BIT && flag_pic)
3678     {
3679       if (flag_fentry > 0)
3680         sorry ("-mfentry isn't supported for 32-bit in combination with -fpic");
3681       flag_fentry = 0;
3682     }
3683   if (flag_fentry < 0)
3684    {
3685 #if defined(PROFILE_BEFORE_PROLOGUE)
3686      flag_fentry = 1;
3687 #else
3688      flag_fentry = 0;
3689 #endif
3690    }
3691
3692   /* Save the initial options in case the user does function specific options */
3693   if (main_args_p)
3694     target_option_default_node = target_option_current_node
3695       = build_target_option_node ();
3696 }
3697
3698 /* Update register usage after having seen the compiler flags.  */
3699
3700 void
3701 ix86_conditional_register_usage (void)
3702 {
3703   int i;
3704   unsigned int j;
3705
3706   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3707     {
3708       if (fixed_regs[i] > 1)
3709         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
3710       if (call_used_regs[i] > 1)
3711         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
3712     }
3713
3714   /* The PIC register, if it exists, is fixed.  */
3715   j = PIC_OFFSET_TABLE_REGNUM;
3716   if (j != INVALID_REGNUM)
3717     fixed_regs[j] = call_used_regs[j] = 1;
3718
3719   /* The MS_ABI changes the set of call-used registers.  */
3720   if (TARGET_64BIT && ix86_cfun_abi () == MS_ABI)
3721     {
3722       call_used_regs[SI_REG] = 0;
3723       call_used_regs[DI_REG] = 0;
3724       call_used_regs[XMM6_REG] = 0;
3725       call_used_regs[XMM7_REG] = 0;
3726       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3727         call_used_regs[i] = 0;
3728     }
3729
3730   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
3731      other call-clobbered regs for 64-bit.  */
3732   if (TARGET_64BIT)
3733     {
3734       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
3735
3736       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3737         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
3738             && call_used_regs[i])
3739           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
3740     }
3741
3742   /* If MMX is disabled, squash the registers.  */
3743   if (! TARGET_MMX)
3744     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3745       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
3746         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3747
3748   /* If SSE is disabled, squash the registers.  */
3749   if (! TARGET_SSE)
3750     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3751       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
3752         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3753
3754   /* If the FPU is disabled, squash the registers.  */
3755   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
3756     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3757       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
3758         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3759
3760   /* If 32-bit, squash the 64-bit registers.  */
3761   if (! TARGET_64BIT)
3762     {
3763       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
3764         reg_names[i] = "";
3765       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3766         reg_names[i] = "";
3767     }
3768 }
3769
3770 \f
3771 /* Save the current options */
3772
3773 static void
3774 ix86_function_specific_save (struct cl_target_option *ptr)
3775 {
3776   ptr->arch = ix86_arch;
3777   ptr->schedule = ix86_schedule;
3778   ptr->tune = ix86_tune;
3779   ptr->fpmath = ix86_fpmath;
3780   ptr->branch_cost = ix86_branch_cost;
3781   ptr->tune_defaulted = ix86_tune_defaulted;
3782   ptr->arch_specified = ix86_arch_specified;
3783   ptr->ix86_isa_flags_explicit = ix86_isa_flags_explicit;
3784   ptr->target_flags_explicit = target_flags_explicit;
3785
3786   /* The fields are char but the variables are not; make sure the
3787      values fit in the fields.  */
3788   gcc_assert (ptr->arch == ix86_arch);
3789   gcc_assert (ptr->schedule == ix86_schedule);
3790   gcc_assert (ptr->tune == ix86_tune);
3791   gcc_assert (ptr->fpmath == ix86_fpmath);
3792   gcc_assert (ptr->branch_cost == ix86_branch_cost);
3793 }
3794
3795 /* Restore the current options */
3796
3797 static void
3798 ix86_function_specific_restore (struct cl_target_option *ptr)
3799 {
3800   enum processor_type old_tune = ix86_tune;
3801   enum processor_type old_arch = ix86_arch;
3802   unsigned int ix86_arch_mask, ix86_tune_mask;
3803   int i;
3804
3805   ix86_arch = (enum processor_type) ptr->arch;
3806   ix86_schedule = (enum attr_cpu) ptr->schedule;
3807   ix86_tune = (enum processor_type) ptr->tune;
3808   ix86_fpmath = (enum fpmath_unit) ptr->fpmath;
3809   ix86_branch_cost = ptr->branch_cost;
3810   ix86_tune_defaulted = ptr->tune_defaulted;
3811   ix86_arch_specified = ptr->arch_specified;
3812   ix86_isa_flags_explicit = ptr->ix86_isa_flags_explicit;
3813   target_flags_explicit = ptr->target_flags_explicit;
3814
3815   /* Recreate the arch feature tests if the arch changed */
3816   if (old_arch != ix86_arch)
3817     {
3818       ix86_arch_mask = 1u << ix86_arch;
3819       for (i = 0; i < X86_ARCH_LAST; ++i)
3820         ix86_arch_features[i]
3821           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3822     }
3823
3824   /* Recreate the tune optimization tests */
3825   if (old_tune != ix86_tune)
3826     {
3827       ix86_tune_mask = 1u << ix86_tune;
3828       for (i = 0; i < X86_TUNE_LAST; ++i)
3829         ix86_tune_features[i]
3830           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3831     }
3832 }
3833
3834 /* Print the current options */
3835
3836 static void
3837 ix86_function_specific_print (FILE *file, int indent,
3838                               struct cl_target_option *ptr)
3839 {
3840   char *target_string
3841     = ix86_target_string (ptr->ix86_isa_flags, ptr->target_flags,
3842                           NULL, NULL, NULL, false);
3843
3844   fprintf (file, "%*sarch = %d (%s)\n",
3845            indent, "",
3846            ptr->arch,
3847            ((ptr->arch < TARGET_CPU_DEFAULT_max)
3848             ? cpu_names[ptr->arch]
3849             : "<unknown>"));
3850
3851   fprintf (file, "%*stune = %d (%s)\n",
3852            indent, "",
3853            ptr->tune,
3854            ((ptr->tune < TARGET_CPU_DEFAULT_max)
3855             ? cpu_names[ptr->tune]
3856             : "<unknown>"));
3857
3858   fprintf (file, "%*sfpmath = %d%s%s\n", indent, "", ptr->fpmath,
3859            (ptr->fpmath & FPMATH_387) ? ", 387" : "",
3860            (ptr->fpmath & FPMATH_SSE) ? ", sse" : "");
3861   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
3862
3863   if (target_string)
3864     {
3865       fprintf (file, "%*s%s\n", indent, "", target_string);
3866       free (target_string);
3867     }
3868 }
3869
3870 \f
3871 /* Inner function to process the attribute((target(...))), take an argument and
3872    set the current options from the argument. If we have a list, recursively go
3873    over the list.  */
3874
3875 static bool
3876 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[])
3877 {
3878   char *next_optstr;
3879   bool ret = true;
3880
3881 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
3882 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
3883 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
3884 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
3885
3886   enum ix86_opt_type
3887   {
3888     ix86_opt_unknown,
3889     ix86_opt_yes,
3890     ix86_opt_no,
3891     ix86_opt_str,
3892     ix86_opt_isa
3893   };
3894
3895   static const struct
3896   {
3897     const char *string;
3898     size_t len;
3899     enum ix86_opt_type type;
3900     int opt;
3901     int mask;
3902   } attrs[] = {
3903     /* isa options */
3904     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
3905     IX86_ATTR_ISA ("abm",       OPT_mabm),
3906     IX86_ATTR_ISA ("aes",       OPT_maes),
3907     IX86_ATTR_ISA ("avx",       OPT_mavx),
3908     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
3909     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
3910     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
3911     IX86_ATTR_ISA ("sse",       OPT_msse),
3912     IX86_ATTR_ISA ("sse2",      OPT_msse2),
3913     IX86_ATTR_ISA ("sse3",      OPT_msse3),
3914     IX86_ATTR_ISA ("sse4",      OPT_msse4),
3915     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
3916     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
3917     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
3918     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
3919     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
3920     IX86_ATTR_ISA ("xop",       OPT_mxop),
3921     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
3922     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
3923     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
3924     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
3925
3926     /* string options */
3927     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
3928     IX86_ATTR_STR ("fpmath=",   IX86_FUNCTION_SPECIFIC_FPMATH),
3929     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
3930
3931     /* flag options */
3932     IX86_ATTR_YES ("cld",
3933                    OPT_mcld,
3934                    MASK_CLD),
3935
3936     IX86_ATTR_NO ("fancy-math-387",
3937                   OPT_mfancy_math_387,
3938                   MASK_NO_FANCY_MATH_387),
3939
3940     IX86_ATTR_YES ("ieee-fp",
3941                    OPT_mieee_fp,
3942                    MASK_IEEE_FP),
3943
3944     IX86_ATTR_YES ("inline-all-stringops",
3945                    OPT_minline_all_stringops,
3946                    MASK_INLINE_ALL_STRINGOPS),
3947
3948     IX86_ATTR_YES ("inline-stringops-dynamically",
3949                    OPT_minline_stringops_dynamically,
3950                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
3951
3952     IX86_ATTR_NO ("align-stringops",
3953                   OPT_mno_align_stringops,
3954                   MASK_NO_ALIGN_STRINGOPS),
3955
3956     IX86_ATTR_YES ("recip",
3957                    OPT_mrecip,
3958                    MASK_RECIP),
3959
3960   };
3961
3962   /* If this is a list, recurse to get the options.  */
3963   if (TREE_CODE (args) == TREE_LIST)
3964     {
3965       bool ret = true;
3966
3967       for (; args; args = TREE_CHAIN (args))
3968         if (TREE_VALUE (args)
3969             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args), p_strings))
3970           ret = false;
3971
3972       return ret;
3973     }
3974
3975   else if (TREE_CODE (args) != STRING_CST)
3976     gcc_unreachable ();
3977
3978   /* Handle multiple arguments separated by commas.  */
3979   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
3980
3981   while (next_optstr && *next_optstr != '\0')
3982     {
3983       char *p = next_optstr;
3984       char *orig_p = p;
3985       char *comma = strchr (next_optstr, ',');
3986       const char *opt_string;
3987       size_t len, opt_len;
3988       int opt;
3989       bool opt_set_p;
3990       char ch;
3991       unsigned i;
3992       enum ix86_opt_type type = ix86_opt_unknown;
3993       int mask = 0;
3994
3995       if (comma)
3996         {
3997           *comma = '\0';
3998           len = comma - next_optstr;
3999           next_optstr = comma + 1;
4000         }
4001       else
4002         {
4003           len = strlen (p);
4004           next_optstr = NULL;
4005         }
4006
4007       /* Recognize no-xxx.  */
4008       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4009         {
4010           opt_set_p = false;
4011           p += 3;
4012           len -= 3;
4013         }
4014       else
4015         opt_set_p = true;
4016
4017       /* Find the option.  */
4018       ch = *p;
4019       opt = N_OPTS;
4020       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4021         {
4022           type = attrs[i].type;
4023           opt_len = attrs[i].len;
4024           if (ch == attrs[i].string[0]
4025               && ((type != ix86_opt_str) ? len == opt_len : len > opt_len)
4026               && memcmp (p, attrs[i].string, opt_len) == 0)
4027             {
4028               opt = attrs[i].opt;
4029               mask = attrs[i].mask;
4030               opt_string = attrs[i].string;
4031               break;
4032             }
4033         }
4034
4035       /* Process the option.  */
4036       if (opt == N_OPTS)
4037         {
4038           error ("attribute(target(\"%s\")) is unknown", orig_p);
4039           ret = false;
4040         }
4041
4042       else if (type == ix86_opt_isa)
4043         ix86_handle_option (opt, p, opt_set_p);
4044
4045       else if (type == ix86_opt_yes || type == ix86_opt_no)
4046         {
4047           if (type == ix86_opt_no)
4048             opt_set_p = !opt_set_p;
4049
4050           if (opt_set_p)
4051             target_flags |= mask;
4052           else
4053             target_flags &= ~mask;
4054         }
4055
4056       else if (type == ix86_opt_str)
4057         {
4058           if (p_strings[opt])
4059             {
4060               error ("option(\"%s\") was already specified", opt_string);
4061               ret = false;
4062             }
4063           else
4064             p_strings[opt] = xstrdup (p + opt_len);
4065         }
4066
4067       else
4068         gcc_unreachable ();
4069     }
4070
4071   return ret;
4072 }
4073
4074 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4075
4076 tree
4077 ix86_valid_target_attribute_tree (tree args)
4078 {
4079   const char *orig_arch_string = ix86_arch_string;
4080   const char *orig_tune_string = ix86_tune_string;
4081   const char *orig_fpmath_string = ix86_fpmath_string;
4082   int orig_tune_defaulted = ix86_tune_defaulted;
4083   int orig_arch_specified = ix86_arch_specified;
4084   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL, NULL };
4085   tree t = NULL_TREE;
4086   int i;
4087   struct cl_target_option *def
4088     = TREE_TARGET_OPTION (target_option_default_node);
4089
4090   /* Process each of the options on the chain.  */
4091   if (! ix86_valid_target_attribute_inner_p (args, option_strings))
4092     return NULL_TREE;
4093
4094   /* If the changed options are different from the default, rerun override_options,
4095      and then save the options away.  The string options are are attribute options,
4096      and will be undone when we copy the save structure.  */
4097   if (ix86_isa_flags != def->ix86_isa_flags
4098       || target_flags != def->target_flags
4099       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4100       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4101       || option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4102     {
4103       /* If we are using the default tune= or arch=, undo the string assigned,
4104          and use the default.  */
4105       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4106         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4107       else if (!orig_arch_specified)
4108         ix86_arch_string = NULL;
4109
4110       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4111         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4112       else if (orig_tune_defaulted)
4113         ix86_tune_string = NULL;
4114
4115       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4116       if (option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4117         ix86_fpmath_string = option_strings[IX86_FUNCTION_SPECIFIC_FPMATH];
4118       else if (!TARGET_64BIT && TARGET_SSE)
4119         ix86_fpmath_string = "sse,387";
4120
4121       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4122       override_options (false);
4123
4124       /* Add any builtin functions with the new isa if any.  */
4125       ix86_add_new_builtins (ix86_isa_flags);
4126
4127       /* Save the current options unless we are validating options for
4128          #pragma.  */
4129       t = build_target_option_node ();
4130
4131       ix86_arch_string = orig_arch_string;
4132       ix86_tune_string = orig_tune_string;
4133       ix86_fpmath_string = orig_fpmath_string;
4134
4135       /* Free up memory allocated to hold the strings */
4136       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4137         if (option_strings[i])
4138           free (option_strings[i]);
4139     }
4140
4141   return t;
4142 }
4143
4144 /* Hook to validate attribute((target("string"))).  */
4145
4146 static bool
4147 ix86_valid_target_attribute_p (tree fndecl,
4148                                tree ARG_UNUSED (name),
4149                                tree args,
4150                                int ARG_UNUSED (flags))
4151 {
4152   struct cl_target_option cur_target;
4153   bool ret = true;
4154   tree old_optimize = build_optimization_node ();
4155   tree new_target, new_optimize;
4156   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4157
4158   /* If the function changed the optimization levels as well as setting target
4159      options, start with the optimizations specified.  */
4160   if (func_optimize && func_optimize != old_optimize)
4161     cl_optimization_restore (TREE_OPTIMIZATION (func_optimize));
4162
4163   /* The target attributes may also change some optimization flags, so update
4164      the optimization options if necessary.  */
4165   cl_target_option_save (&cur_target);
4166   new_target = ix86_valid_target_attribute_tree (args);
4167   new_optimize = build_optimization_node ();
4168
4169   if (!new_target)
4170     ret = false;
4171
4172   else if (fndecl)
4173     {
4174       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4175
4176       if (old_optimize != new_optimize)
4177         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4178     }
4179
4180   cl_target_option_restore (&cur_target);
4181
4182   if (old_optimize != new_optimize)
4183     cl_optimization_restore (TREE_OPTIMIZATION (old_optimize));
4184
4185   return ret;
4186 }
4187
4188 \f
4189 /* Hook to determine if one function can safely inline another.  */
4190
4191 static bool
4192 ix86_can_inline_p (tree caller, tree callee)
4193 {
4194   bool ret = false;
4195   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4196   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4197
4198   /* If callee has no option attributes, then it is ok to inline.  */
4199   if (!callee_tree)
4200     ret = true;
4201
4202   /* If caller has no option attributes, but callee does then it is not ok to
4203      inline.  */
4204   else if (!caller_tree)
4205     ret = false;
4206
4207   else
4208     {
4209       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4210       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4211
4212       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4213          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4214          function.  */
4215       if ((caller_opts->ix86_isa_flags & callee_opts->ix86_isa_flags)
4216           != callee_opts->ix86_isa_flags)
4217         ret = false;
4218
4219       /* See if we have the same non-isa options.  */
4220       else if (caller_opts->target_flags != callee_opts->target_flags)
4221         ret = false;
4222
4223       /* See if arch, tune, etc. are the same.  */
4224       else if (caller_opts->arch != callee_opts->arch)
4225         ret = false;
4226
4227       else if (caller_opts->tune != callee_opts->tune)
4228         ret = false;
4229
4230       else if (caller_opts->fpmath != callee_opts->fpmath)
4231         ret = false;
4232
4233       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4234         ret = false;
4235
4236       else
4237         ret = true;
4238     }
4239
4240   return ret;
4241 }
4242
4243 \f
4244 /* Remember the last target of ix86_set_current_function.  */
4245 static GTY(()) tree ix86_previous_fndecl;
4246
4247 /* Establish appropriate back-end context for processing the function
4248    FNDECL.  The argument might be NULL to indicate processing at top
4249    level, outside of any function scope.  */
4250 static void
4251 ix86_set_current_function (tree fndecl)
4252 {
4253   /* Only change the context if the function changes.  This hook is called
4254      several times in the course of compiling a function, and we don't want to
4255      slow things down too much or call target_reinit when it isn't safe.  */
4256   if (fndecl && fndecl != ix86_previous_fndecl)
4257     {
4258       tree old_tree = (ix86_previous_fndecl
4259                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4260                        : NULL_TREE);
4261
4262       tree new_tree = (fndecl
4263                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4264                        : NULL_TREE);
4265
4266       ix86_previous_fndecl = fndecl;
4267       if (old_tree == new_tree)
4268         ;
4269
4270       else if (new_tree)
4271         {
4272           cl_target_option_restore (TREE_TARGET_OPTION (new_tree));
4273           target_reinit ();
4274         }
4275
4276       else if (old_tree)
4277         {
4278           struct cl_target_option *def
4279             = TREE_TARGET_OPTION (target_option_current_node);
4280
4281           cl_target_option_restore (def);
4282           target_reinit ();
4283         }
4284     }
4285 }
4286
4287 \f
4288 /* Return true if this goes in large data/bss.  */
4289
4290 static bool
4291 ix86_in_large_data_p (tree exp)
4292 {
4293   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4294     return false;
4295
4296   /* Functions are never large data.  */
4297   if (TREE_CODE (exp) == FUNCTION_DECL)
4298     return false;
4299
4300   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4301     {
4302       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4303       if (strcmp (section, ".ldata") == 0
4304           || strcmp (section, ".lbss") == 0)
4305         return true;
4306       return false;
4307     }
4308   else
4309     {
4310       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4311
4312       /* If this is an incomplete type with size 0, then we can't put it
4313          in data because it might be too big when completed.  */
4314       if (!size || size > ix86_section_threshold)
4315         return true;
4316     }
4317
4318   return false;
4319 }
4320
4321 /* Switch to the appropriate section for output of DECL.
4322    DECL is either a `VAR_DECL' node or a constant of some sort.
4323    RELOC indicates whether forming the initial value of DECL requires
4324    link-time relocations.  */
4325
4326 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4327         ATTRIBUTE_UNUSED;
4328
4329 static section *
4330 x86_64_elf_select_section (tree decl, int reloc,
4331                            unsigned HOST_WIDE_INT align)
4332 {
4333   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4334       && ix86_in_large_data_p (decl))
4335     {
4336       const char *sname = NULL;
4337       unsigned int flags = SECTION_WRITE;
4338       switch (categorize_decl_for_section (decl, reloc))
4339         {
4340         case SECCAT_DATA:
4341           sname = ".ldata";
4342           break;
4343         case SECCAT_DATA_REL:
4344           sname = ".ldata.rel";
4345           break;
4346         case SECCAT_DATA_REL_LOCAL:
4347           sname = ".ldata.rel.local";
4348           break;
4349         case SECCAT_DATA_REL_RO:
4350           sname = ".ldata.rel.ro";
4351           break;
4352         case SECCAT_DATA_REL_RO_LOCAL:
4353           sname = ".ldata.rel.ro.local";
4354           break;
4355         case SECCAT_BSS:
4356           sname = ".lbss";
4357           flags |= SECTION_BSS;
4358           break;
4359         case SECCAT_RODATA:
4360         case SECCAT_RODATA_MERGE_STR:
4361         case SECCAT_RODATA_MERGE_STR_INIT:
4362         case SECCAT_RODATA_MERGE_CONST:
4363           sname = ".lrodata";
4364           flags = 0;
4365           break;
4366         case SECCAT_SRODATA:
4367         case SECCAT_SDATA:
4368         case SECCAT_SBSS:
4369           gcc_unreachable ();
4370         case SECCAT_TEXT:
4371         case SECCAT_TDATA:
4372         case SECCAT_TBSS:
4373           /* We don't split these for medium model.  Place them into
4374              default sections and hope for best.  */
4375           break;
4376         }
4377       if (sname)
4378         {
4379           /* We might get called with string constants, but get_named_section
4380              doesn't like them as they are not DECLs.  Also, we need to set
4381              flags in that case.  */
4382           if (!DECL_P (decl))
4383             return get_section (sname, flags, NULL);
4384           return get_named_section (decl, sname, reloc);
4385         }
4386     }
4387   return default_elf_select_section (decl, reloc, align);
4388 }
4389
4390 /* Build up a unique section name, expressed as a
4391    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4392    RELOC indicates whether the initial value of EXP requires
4393    link-time relocations.  */
4394
4395 static void ATTRIBUTE_UNUSED
4396 x86_64_elf_unique_section (tree decl, int reloc)
4397 {
4398   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4399       && ix86_in_large_data_p (decl))
4400     {
4401       const char *prefix = NULL;
4402       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4403       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4404
4405       switch (categorize_decl_for_section (decl, reloc))
4406         {
4407         case SECCAT_DATA:
4408         case SECCAT_DATA_REL:
4409         case SECCAT_DATA_REL_LOCAL:
4410         case SECCAT_DATA_REL_RO:
4411         case SECCAT_DATA_REL_RO_LOCAL:
4412           prefix = one_only ? ".ld" : ".ldata";
4413           break;
4414         case SECCAT_BSS:
4415           prefix = one_only ? ".lb" : ".lbss";
4416           break;
4417         case SECCAT_RODATA:
4418         case SECCAT_RODATA_MERGE_STR:
4419         case SECCAT_RODATA_MERGE_STR_INIT:
4420         case SECCAT_RODATA_MERGE_CONST:
4421           prefix = one_only ? ".lr" : ".lrodata";
4422           break;
4423         case SECCAT_SRODATA:
4424         case SECCAT_SDATA:
4425         case SECCAT_SBSS:
4426           gcc_unreachable ();
4427         case SECCAT_TEXT:
4428         case SECCAT_TDATA:
4429         case SECCAT_TBSS:
4430           /* We don't split these for medium model.  Place them into
4431              default sections and hope for best.  */
4432           break;
4433         }
4434       if (prefix)
4435         {
4436           const char *name, *linkonce;
4437           char *string;
4438
4439           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4440           name = targetm.strip_name_encoding (name);
4441
4442           /* If we're using one_only, then there needs to be a .gnu.linkonce
4443              prefix to the section name.  */
4444           linkonce = one_only ? ".gnu.linkonce" : "";
4445
4446           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4447
4448           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4449           return;
4450         }
4451     }
4452   default_unique_section (decl, reloc);
4453 }
4454
4455 #ifdef COMMON_ASM_OP
4456 /* This says how to output assembler code to declare an
4457    uninitialized external linkage data object.
4458
4459    For medium model x86-64 we need to use .largecomm opcode for
4460    large objects.  */
4461 void
4462 x86_elf_aligned_common (FILE *file,
4463                         const char *name, unsigned HOST_WIDE_INT size,
4464                         int align)
4465 {
4466   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4467       && size > (unsigned int)ix86_section_threshold)
4468     fputs (".largecomm\t", file);
4469   else
4470     fputs (COMMON_ASM_OP, file);
4471   assemble_name (file, name);
4472   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4473            size, align / BITS_PER_UNIT);
4474 }
4475 #endif
4476
4477 /* Utility function for targets to use in implementing
4478    ASM_OUTPUT_ALIGNED_BSS.  */
4479
4480 void
4481 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4482                         const char *name, unsigned HOST_WIDE_INT size,
4483                         int align)
4484 {
4485   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4486       && size > (unsigned int)ix86_section_threshold)
4487     switch_to_section (get_named_section (decl, ".lbss", 0));
4488   else
4489     switch_to_section (bss_section);
4490   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4491 #ifdef ASM_DECLARE_OBJECT_NAME
4492   last_assemble_variable_decl = decl;
4493   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4494 #else
4495   /* Standard thing is just output label for the object.  */
4496   ASM_OUTPUT_LABEL (file, name);
4497 #endif /* ASM_DECLARE_OBJECT_NAME */
4498   ASM_OUTPUT_SKIP (file, size ? size : 1);
4499 }
4500 \f
4501 void
4502 optimization_options (int level, int size ATTRIBUTE_UNUSED)
4503 {
4504   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
4505      make the problem with not enough registers even worse.  */
4506 #ifdef INSN_SCHEDULING
4507   if (level > 1)
4508     flag_schedule_insns = 0;
4509 #endif
4510
4511   if (TARGET_MACHO)
4512     /* The Darwin libraries never set errno, so we might as well
4513        avoid calling them when that's the only reason we would.  */
4514     flag_errno_math = 0;
4515
4516   /* The default values of these switches depend on the TARGET_64BIT
4517      that is not known at this moment.  Mark these values with 2 and
4518      let user the to override these.  In case there is no command line option
4519      specifying them, we will set the defaults in override_options.  */
4520   if (optimize >= 1)
4521     flag_omit_frame_pointer = 2;
4522
4523   /* For -O2 and beyond, turn on -fzee for x86_64 target. */
4524   if (level > 1)
4525     flag_zee = 2;
4526
4527   flag_pcc_struct_return = 2;
4528   flag_asynchronous_unwind_tables = 2;
4529   flag_vect_cost_model = 1;
4530 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
4531   SUBTARGET_OPTIMIZATION_OPTIONS;
4532 #endif
4533 }
4534
4535 /* Decide whether we must probe the stack before any space allocation
4536    on this target.  It's essentially TARGET_STACK_PROBE except when
4537    -fstack-check causes the stack to be already probed differently.  */
4538
4539 bool
4540 ix86_target_stack_probe (void)
4541 {
4542   /* Do not probe the stack twice if static stack checking is enabled.  */
4543   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
4544     return false;
4545
4546   return TARGET_STACK_PROBE;
4547 }
4548 \f
4549 /* Decide whether we can make a sibling call to a function.  DECL is the
4550    declaration of the function being targeted by the call and EXP is the
4551    CALL_EXPR representing the call.  */
4552
4553 static bool
4554 ix86_function_ok_for_sibcall (tree decl, tree exp)
4555 {
4556   tree type, decl_or_type;
4557   rtx a, b;
4558
4559   /* If we are generating position-independent code, we cannot sibcall
4560      optimize any indirect call, or a direct call to a global function,
4561      as the PLT requires %ebx be live.  */
4562   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
4563     return false;
4564
4565   /* If we need to align the outgoing stack, then sibcalling would
4566      unalign the stack, which may break the called function.  */
4567   if (ix86_minimum_incoming_stack_boundary (true)
4568       < PREFERRED_STACK_BOUNDARY)
4569     return false;
4570
4571   if (decl)
4572     {
4573       decl_or_type = decl;
4574       type = TREE_TYPE (decl);
4575     }
4576   else
4577     {
4578       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4579       type = CALL_EXPR_FN (exp);                /* pointer expression */
4580       type = TREE_TYPE (type);                  /* pointer type */
4581       type = TREE_TYPE (type);                  /* function type */
4582       decl_or_type = type;
4583     }
4584
4585   /* Check that the return value locations are the same.  Like
4586      if we are returning floats on the 80387 register stack, we cannot
4587      make a sibcall from a function that doesn't return a float to a
4588      function that does or, conversely, from a function that does return
4589      a float to a function that doesn't; the necessary stack adjustment
4590      would not be executed.  This is also the place we notice
4591      differences in the return value ABI.  Note that it is ok for one
4592      of the functions to have void return type as long as the return
4593      value of the other is passed in a register.  */
4594   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
4595   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4596                            cfun->decl, false);
4597   if (STACK_REG_P (a) || STACK_REG_P (b))
4598     {
4599       if (!rtx_equal_p (a, b))
4600         return false;
4601     }
4602   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4603     ;
4604   else if (!rtx_equal_p (a, b))
4605     return false;
4606
4607   if (TARGET_64BIT)
4608     {
4609       /* The SYSV ABI has more call-clobbered registers;
4610          disallow sibcalls from MS to SYSV.  */
4611       if (cfun->machine->call_abi == MS_ABI
4612           && ix86_function_type_abi (type) == SYSV_ABI)
4613         return false;
4614     }
4615   else
4616     {
4617       /* If this call is indirect, we'll need to be able to use a
4618          call-clobbered register for the address of the target function.
4619          Make sure that all such registers are not used for passing
4620          parameters.  Note that DLLIMPORT functions are indirect.  */
4621       if (!decl
4622           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
4623         {
4624           if (ix86_function_regparm (type, NULL) >= 3)
4625             {
4626               /* ??? Need to count the actual number of registers to be used,
4627                  not the possible number of registers.  Fix later.  */
4628               return false;
4629             }
4630         }
4631     }
4632
4633   /* Otherwise okay.  That also includes certain types of indirect calls.  */
4634   return true;
4635 }
4636
4637 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
4638    and "sseregparm" calling convention attributes;
4639    arguments as in struct attribute_spec.handler.  */
4640
4641 static tree
4642 ix86_handle_cconv_attribute (tree *node, tree name,
4643                                    tree args,
4644                                    int flags ATTRIBUTE_UNUSED,
4645                                    bool *no_add_attrs)
4646 {
4647   if (TREE_CODE (*node) != FUNCTION_TYPE
4648       && TREE_CODE (*node) != METHOD_TYPE
4649       && TREE_CODE (*node) != FIELD_DECL
4650       && TREE_CODE (*node) != TYPE_DECL)
4651     {
4652       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4653                name);
4654       *no_add_attrs = true;
4655       return NULL_TREE;
4656     }
4657
4658   /* Can combine regparm with all attributes but fastcall.  */
4659   if (is_attribute_p ("regparm", name))
4660     {
4661       tree cst;
4662
4663       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4664         {
4665           error ("fastcall and regparm attributes are not compatible");
4666         }
4667
4668       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4669         {
4670           error ("regparam and thiscall attributes are not compatible");
4671         }
4672
4673       cst = TREE_VALUE (args);
4674       if (TREE_CODE (cst) != INTEGER_CST)
4675         {
4676           warning (OPT_Wattributes,
4677                    "%qE attribute requires an integer constant argument",
4678                    name);
4679           *no_add_attrs = true;
4680         }
4681       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
4682         {
4683           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
4684                    name, REGPARM_MAX);
4685           *no_add_attrs = true;
4686         }
4687
4688       return NULL_TREE;
4689     }
4690
4691   if (TARGET_64BIT)
4692     {
4693       /* Do not warn when emulating the MS ABI.  */
4694       if ((TREE_CODE (*node) != FUNCTION_TYPE
4695            && TREE_CODE (*node) != METHOD_TYPE)
4696           || ix86_function_type_abi (*node) != MS_ABI)
4697         warning (OPT_Wattributes, "%qE attribute ignored",
4698                  name);
4699       *no_add_attrs = true;
4700       return NULL_TREE;
4701     }
4702
4703   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
4704   if (is_attribute_p ("fastcall", name))
4705     {
4706       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4707         {
4708           error ("fastcall and cdecl attributes are not compatible");
4709         }
4710       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4711         {
4712           error ("fastcall and stdcall attributes are not compatible");
4713         }
4714       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
4715         {
4716           error ("fastcall and regparm attributes are not compatible");
4717         }
4718       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4719         {
4720           error ("fastcall and thiscall attributes are not compatible");
4721         }
4722     }
4723
4724   /* Can combine stdcall with fastcall (redundant), regparm and
4725      sseregparm.  */
4726   else if (is_attribute_p ("stdcall", name))
4727     {
4728       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4729         {
4730           error ("stdcall and cdecl attributes are not compatible");
4731         }
4732       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4733         {
4734           error ("stdcall and fastcall attributes are not compatible");
4735         }
4736       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4737         {
4738           error ("stdcall and thiscall attributes are not compatible");
4739         }
4740     }
4741
4742   /* Can combine cdecl with regparm and sseregparm.  */
4743   else if (is_attribute_p ("cdecl", name))
4744     {
4745       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4746         {
4747           error ("stdcall and cdecl attributes are not compatible");
4748         }
4749       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4750         {
4751           error ("fastcall and cdecl attributes are not compatible");
4752         }
4753       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4754         {
4755           error ("cdecl and thiscall attributes are not compatible");
4756         }
4757     }
4758   else if (is_attribute_p ("thiscall", name))
4759     {
4760       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
4761         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
4762                  name);
4763       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4764         {
4765           error ("stdcall and thiscall attributes are not compatible");
4766         }
4767       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4768         {
4769           error ("fastcall and thiscall attributes are not compatible");
4770         }
4771       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4772         {
4773           error ("cdecl and thiscall attributes are not compatible");
4774         }
4775     }
4776
4777   /* Can combine sseregparm with all attributes.  */
4778
4779   return NULL_TREE;
4780 }
4781
4782 /* Return 0 if the attributes for two types are incompatible, 1 if they
4783    are compatible, and 2 if they are nearly compatible (which causes a
4784    warning to be generated).  */
4785
4786 static int
4787 ix86_comp_type_attributes (const_tree type1, const_tree type2)
4788 {
4789   /* Check for mismatch of non-default calling convention.  */
4790   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
4791
4792   if (TREE_CODE (type1) != FUNCTION_TYPE
4793       && TREE_CODE (type1) != METHOD_TYPE)
4794     return 1;
4795
4796   /* Check for mismatched fastcall/regparm types.  */
4797   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
4798        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
4799       || (ix86_function_regparm (type1, NULL)
4800           != ix86_function_regparm (type2, NULL)))
4801     return 0;
4802
4803   /* Check for mismatched sseregparm types.  */
4804   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
4805       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
4806     return 0;
4807
4808   /* Check for mismatched thiscall types.  */
4809   if (!lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type1))
4810       != !lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type2)))
4811     return 0;
4812
4813   /* Check for mismatched return types (cdecl vs stdcall).  */
4814   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
4815       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
4816     return 0;
4817
4818   return 1;
4819 }
4820 \f
4821 /* Return the regparm value for a function with the indicated TYPE and DECL.
4822    DECL may be NULL when calling function indirectly
4823    or considering a libcall.  */
4824
4825 static int
4826 ix86_function_regparm (const_tree type, const_tree decl)
4827 {
4828   tree attr;
4829   int regparm;
4830
4831   if (TARGET_64BIT)
4832     return (ix86_function_type_abi (type) == SYSV_ABI
4833             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
4834
4835   regparm = ix86_regparm;
4836   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
4837   if (attr)
4838     {
4839       regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
4840       return regparm;
4841     }
4842
4843   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
4844     return 2;
4845
4846   if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
4847     return 1;
4848
4849   /* Use register calling convention for local functions when possible.  */
4850   if (decl
4851       && TREE_CODE (decl) == FUNCTION_DECL
4852       && optimize
4853       && !(profile_flag && !flag_fentry))
4854     {
4855       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4856       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
4857       if (i && i->local)
4858         {
4859           int local_regparm, globals = 0, regno;
4860
4861           /* Make sure no regparm register is taken by a
4862              fixed register variable.  */
4863           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
4864             if (fixed_regs[local_regparm])
4865               break;
4866
4867           /* We don't want to use regparm(3) for nested functions as
4868              these use a static chain pointer in the third argument.  */
4869           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
4870             local_regparm = 2;
4871
4872           /* Each fixed register usage increases register pressure,
4873              so less registers should be used for argument passing.
4874              This functionality can be overriden by an explicit
4875              regparm value.  */
4876           for (regno = 0; regno <= DI_REG; regno++)
4877             if (fixed_regs[regno])
4878               globals++;
4879
4880           local_regparm
4881             = globals < local_regparm ? local_regparm - globals : 0;
4882
4883           if (local_regparm > regparm)
4884             regparm = local_regparm;
4885         }
4886     }
4887
4888   return regparm;
4889 }
4890
4891 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
4892    DFmode (2) arguments in SSE registers for a function with the
4893    indicated TYPE and DECL.  DECL may be NULL when calling function
4894    indirectly or considering a libcall.  Otherwise return 0.  */
4895
4896 static int
4897 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
4898 {
4899   gcc_assert (!TARGET_64BIT);
4900
4901   /* Use SSE registers to pass SFmode and DFmode arguments if requested
4902      by the sseregparm attribute.  */
4903   if (TARGET_SSEREGPARM
4904       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
4905     {
4906       if (!TARGET_SSE)
4907         {
4908           if (warn)
4909             {
4910               if (decl)
4911                 error ("Calling %qD with attribute sseregparm without "
4912                        "SSE/SSE2 enabled", decl);
4913               else
4914                 error ("Calling %qT with attribute sseregparm without "
4915                        "SSE/SSE2 enabled", type);
4916             }
4917           return 0;
4918         }
4919
4920       return 2;
4921     }
4922
4923   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
4924      (and DFmode for SSE2) arguments in SSE registers.  */
4925   if (decl && TARGET_SSE_MATH && optimize
4926       && !(profile_flag && !flag_fentry))
4927     {
4928       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4929       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
4930       if (i && i->local)
4931         return TARGET_SSE2 ? 2 : 1;
4932     }
4933
4934   return 0;
4935 }
4936
4937 /* Return true if EAX is live at the start of the function.  Used by
4938    ix86_expand_prologue to determine if we need special help before
4939    calling allocate_stack_worker.  */
4940
4941 static bool
4942 ix86_eax_live_at_start_p (void)
4943 {
4944   /* Cheat.  Don't bother working forward from ix86_function_regparm
4945      to the function type to whether an actual argument is located in
4946      eax.  Instead just look at cfg info, which is still close enough
4947      to correct at this point.  This gives false positives for broken
4948      functions that might use uninitialized data that happens to be
4949      allocated in eax, but who cares?  */
4950   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
4951 }
4952
4953 /* Value is the number of bytes of arguments automatically
4954    popped when returning from a subroutine call.
4955    FUNDECL is the declaration node of the function (as a tree),
4956    FUNTYPE is the data type of the function (as a tree),
4957    or for a library call it is an identifier node for the subroutine name.
4958    SIZE is the number of bytes of arguments passed on the stack.
4959
4960    On the 80386, the RTD insn may be used to pop them if the number
4961      of args is fixed, but if the number is variable then the caller
4962      must pop them all.  RTD can't be used for library calls now
4963      because the library is compiled with the Unix compiler.
4964    Use of RTD is a selectable option, since it is incompatible with
4965    standard Unix calling sequences.  If the option is not selected,
4966    the caller must always pop the args.
4967
4968    The attribute stdcall is equivalent to RTD on a per module basis.  */
4969
4970 static int
4971 ix86_return_pops_args (tree fundecl, tree funtype, int size)
4972 {
4973   int rtd;
4974
4975   /* None of the 64-bit ABIs pop arguments.  */
4976   if (TARGET_64BIT)
4977     return 0;
4978
4979   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
4980
4981   /* Cdecl functions override -mrtd, and never pop the stack.  */
4982   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
4983     {
4984       /* Stdcall and fastcall functions will pop the stack if not
4985          variable args.  */
4986       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
4987           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype))
4988           || lookup_attribute ("thiscall", TYPE_ATTRIBUTES (funtype)))
4989         rtd = 1;
4990
4991       if (rtd && ! stdarg_p (funtype))
4992         return size;
4993     }
4994
4995   /* Lose any fake structure return argument if it is passed on the stack.  */
4996   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
4997       && !KEEP_AGGREGATE_RETURN_POINTER)
4998     {
4999       int nregs = ix86_function_regparm (funtype, fundecl);
5000       if (nregs == 0)
5001         return GET_MODE_SIZE (Pmode);
5002     }
5003
5004   return 0;
5005 }
5006 \f
5007 /* Argument support functions.  */
5008
5009 /* Return true when register may be used to pass function parameters.  */
5010 bool
5011 ix86_function_arg_regno_p (int regno)
5012 {
5013   int i;
5014   const int *parm_regs;
5015
5016   if (!TARGET_64BIT)
5017     {
5018       if (TARGET_MACHO)
5019         return (regno < REGPARM_MAX
5020                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5021       else
5022         return (regno < REGPARM_MAX
5023                 || (TARGET_MMX && MMX_REGNO_P (regno)
5024                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5025                 || (TARGET_SSE && SSE_REGNO_P (regno)
5026                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5027     }
5028
5029   if (TARGET_MACHO)
5030     {
5031       if (SSE_REGNO_P (regno) && TARGET_SSE)
5032         return true;
5033     }
5034   else
5035     {
5036       if (TARGET_SSE && SSE_REGNO_P (regno)
5037           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5038         return true;
5039     }
5040
5041   /* TODO: The function should depend on current function ABI but
5042      builtins.c would need updating then. Therefore we use the
5043      default ABI.  */
5044
5045   /* RAX is used as hidden argument to va_arg functions.  */
5046   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5047     return true;
5048
5049   if (ix86_abi == MS_ABI)
5050     parm_regs = x86_64_ms_abi_int_parameter_registers;
5051   else
5052     parm_regs = x86_64_int_parameter_registers;
5053   for (i = 0; i < (ix86_abi == MS_ABI
5054                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5055     if (regno == parm_regs[i])
5056       return true;
5057   return false;
5058 }
5059
5060 /* Return if we do not know how to pass TYPE solely in registers.  */
5061
5062 static bool
5063 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5064 {
5065   if (must_pass_in_stack_var_size_or_pad (mode, type))
5066     return true;
5067
5068   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5069      The layout_type routine is crafty and tries to trick us into passing
5070      currently unsupported vector types on the stack by using TImode.  */
5071   return (!TARGET_64BIT && mode == TImode
5072           && type && TREE_CODE (type) != VECTOR_TYPE);
5073 }
5074
5075 /* It returns the size, in bytes, of the area reserved for arguments passed
5076    in registers for the function represented by fndecl dependent to the used
5077    abi format.  */
5078 int
5079 ix86_reg_parm_stack_space (const_tree fndecl)
5080 {
5081   enum calling_abi call_abi = SYSV_ABI;
5082   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5083     call_abi = ix86_function_abi (fndecl);
5084   else
5085     call_abi = ix86_function_type_abi (fndecl);
5086   if (call_abi == MS_ABI)
5087     return 32;
5088   return 0;
5089 }
5090
5091 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5092    call abi used.  */
5093 enum calling_abi
5094 ix86_function_type_abi (const_tree fntype)
5095 {
5096   if (TARGET_64BIT && fntype != NULL)
5097     {
5098       enum calling_abi abi = ix86_abi;
5099       if (abi == SYSV_ABI)
5100         {
5101           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5102             abi = MS_ABI;
5103         }
5104       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5105         abi = SYSV_ABI;
5106       return abi;
5107     }
5108   return ix86_abi;
5109 }
5110
5111 static bool
5112 ix86_function_ms_hook_prologue (const_tree fntype)
5113 {
5114   if (fntype && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fntype)))
5115     {
5116       if (decl_function_context (fntype) != NULL_TREE)
5117       {
5118         error_at (DECL_SOURCE_LOCATION (fntype),
5119             "ms_hook_prologue is not compatible with nested function");
5120       }
5121
5122       return true;
5123     }
5124   return false;
5125 }
5126
5127 static enum calling_abi
5128 ix86_function_abi (const_tree fndecl)
5129 {
5130   if (! fndecl)
5131     return ix86_abi;
5132   return ix86_function_type_abi (TREE_TYPE (fndecl));
5133 }
5134
5135 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5136    call abi used.  */
5137 enum calling_abi
5138 ix86_cfun_abi (void)
5139 {
5140   if (! cfun || ! TARGET_64BIT)
5141     return ix86_abi;
5142   return cfun->machine->call_abi;
5143 }
5144
5145 /* Write the extra assembler code needed to declare a function properly.  */
5146
5147 void
5148 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5149                                 tree decl)
5150 {
5151   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5152
5153   if (is_ms_hook)
5154     {
5155       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5156       unsigned int filler_cc = 0xcccccccc;
5157
5158       for (i = 0; i < filler_count; i += 4)
5159         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5160     }
5161
5162   ASM_OUTPUT_LABEL (asm_out_file, fname);
5163
5164   /* Output magic byte marker, if hot-patch attribute is set.
5165      For x86 case frame-pointer prologue will be emitted in
5166      expand_prologue.  */
5167   if (is_ms_hook)
5168     {
5169       if (TARGET_64BIT)
5170         /* leaq [%rsp + 0], %rsp  */
5171         asm_fprintf (asm_out_file, ASM_BYTE
5172                      "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5173       else
5174         /* movl.s %edi, %edi.  */
5175         asm_fprintf (asm_out_file, ASM_BYTE "0x8b, 0xff\n");
5176     }
5177 }
5178
5179 /* regclass.c  */
5180 extern void init_regs (void);
5181
5182 /* Implementation of call abi switching target hook. Specific to FNDECL
5183    the specific call register sets are set. See also CONDITIONAL_REGISTER_USAGE
5184    for more details.  */
5185 void
5186 ix86_call_abi_override (const_tree fndecl)
5187 {
5188   if (fndecl == NULL_TREE)
5189     cfun->machine->call_abi = ix86_abi;
5190   else
5191     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5192 }
5193
5194 /* MS and SYSV ABI have different set of call used registers.  Avoid expensive
5195    re-initialization of init_regs each time we switch function context since
5196    this is needed only during RTL expansion.  */
5197 static void
5198 ix86_maybe_switch_abi (void)
5199 {
5200   if (TARGET_64BIT &&
5201       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5202     reinit_regs ();
5203 }
5204
5205 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5206    for a call to a function whose data type is FNTYPE.
5207    For a library call, FNTYPE is 0.  */
5208
5209 void
5210 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5211                       tree fntype,      /* tree ptr for function decl */
5212                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5213                       tree fndecl)
5214 {
5215   struct cgraph_local_info *i = fndecl ? cgraph_local_info (fndecl) : NULL;
5216   memset (cum, 0, sizeof (*cum));
5217
5218   if (fndecl)
5219    cum->call_abi = ix86_function_abi (fndecl);
5220   else
5221    cum->call_abi = ix86_function_type_abi (fntype);
5222   /* Set up the number of registers to use for passing arguments.  */
5223
5224   if (cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5225     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5226            "or subtarget optimization implying it");
5227   cum->nregs = ix86_regparm;
5228   if (TARGET_64BIT)
5229     {
5230       cum->nregs = (cum->call_abi == SYSV_ABI
5231                    ? X86_64_REGPARM_MAX
5232                    : X86_64_MS_REGPARM_MAX);
5233     }
5234   if (TARGET_SSE)
5235     {
5236       cum->sse_nregs = SSE_REGPARM_MAX;
5237       if (TARGET_64BIT)
5238         {
5239           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5240                            ? X86_64_SSE_REGPARM_MAX
5241                            : X86_64_MS_SSE_REGPARM_MAX);
5242         }
5243     }
5244   if (TARGET_MMX)
5245     cum->mmx_nregs = MMX_REGPARM_MAX;
5246   cum->warn_avx = true;
5247   cum->warn_sse = true;
5248   cum->warn_mmx = true;
5249
5250   /* Because type might mismatch in between caller and callee, we need to
5251      use actual type of function for local calls.
5252      FIXME: cgraph_analyze can be told to actually record if function uses
5253      va_start so for local functions maybe_vaarg can be made aggressive
5254      helping K&R code.
5255      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5256   if (i && i->local)
5257     fntype = TREE_TYPE (fndecl);
5258   cum->maybe_vaarg = (fntype
5259                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5260                       : !libname);
5261
5262   if (!TARGET_64BIT)
5263     {
5264       /* If there are variable arguments, then we won't pass anything
5265          in registers in 32-bit mode. */
5266       if (stdarg_p (fntype))
5267         {
5268           cum->nregs = 0;
5269           cum->sse_nregs = 0;
5270           cum->mmx_nregs = 0;
5271           cum->warn_avx = 0;
5272           cum->warn_sse = 0;
5273           cum->warn_mmx = 0;
5274           return;
5275         }
5276
5277       /* Use ecx and edx registers if function has fastcall attribute,
5278          else look for regparm information.  */
5279       if (fntype)
5280         {
5281           if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
5282             {
5283               cum->nregs = 1;
5284               cum->fastcall = 1; /* Same first register as in fastcall.  */
5285             }
5286           else if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
5287             {
5288               cum->nregs = 2;
5289               cum->fastcall = 1;
5290             }
5291           else
5292             cum->nregs = ix86_function_regparm (fntype, fndecl);
5293         }
5294
5295       /* Set up the number of SSE registers used for passing SFmode
5296          and DFmode arguments.  Warn for mismatching ABI.  */
5297       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5298     }
5299 }
5300
5301 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5302    But in the case of vector types, it is some vector mode.
5303
5304    When we have only some of our vector isa extensions enabled, then there
5305    are some modes for which vector_mode_supported_p is false.  For these
5306    modes, the generic vector support in gcc will choose some non-vector mode
5307    in order to implement the type.  By computing the natural mode, we'll
5308    select the proper ABI location for the operand and not depend on whatever
5309    the middle-end decides to do with these vector types.
5310
5311    The midde-end can't deal with the vector types > 16 bytes.  In this
5312    case, we return the original mode and warn ABI change if CUM isn't
5313    NULL.  */
5314
5315 static enum machine_mode
5316 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5317 {
5318   enum machine_mode mode = TYPE_MODE (type);
5319
5320   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5321     {
5322       HOST_WIDE_INT size = int_size_in_bytes (type);
5323       if ((size == 8 || size == 16 || size == 32)
5324           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5325           && TYPE_VECTOR_SUBPARTS (type) > 1)
5326         {
5327           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5328
5329           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5330             mode = MIN_MODE_VECTOR_FLOAT;
5331           else
5332             mode = MIN_MODE_VECTOR_INT;
5333
5334           /* Get the mode which has this inner mode and number of units.  */
5335           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5336             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5337                 && GET_MODE_INNER (mode) == innermode)
5338               {
5339                 if (size == 32 && !TARGET_AVX)
5340                   {
5341                     static bool warnedavx;
5342
5343                     if (cum
5344                         && !warnedavx
5345                         && cum->warn_avx)
5346                       {
5347                         warnedavx = true;
5348                         warning (0, "AVX vector argument without AVX "
5349                                  "enabled changes the ABI");
5350                       }
5351                     return TYPE_MODE (type);
5352                   }
5353                 else
5354                   return mode;
5355               }
5356
5357           gcc_unreachable ();
5358         }
5359     }
5360
5361   return mode;
5362 }
5363
5364 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5365    this may not agree with the mode that the type system has chosen for the
5366    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5367    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5368
5369 static rtx
5370 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5371                      unsigned int regno)
5372 {
5373   rtx tmp;
5374
5375   if (orig_mode != BLKmode)
5376     tmp = gen_rtx_REG (orig_mode, regno);
5377   else
5378     {
5379       tmp = gen_rtx_REG (mode, regno);
5380       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5381       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5382     }
5383
5384   return tmp;
5385 }
5386
5387 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5388    of this code is to classify each 8bytes of incoming argument by the register
5389    class and assign registers accordingly.  */
5390
5391 /* Return the union class of CLASS1 and CLASS2.
5392    See the x86-64 PS ABI for details.  */
5393
5394 static enum x86_64_reg_class
5395 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5396 {
5397   /* Rule #1: If both classes are equal, this is the resulting class.  */
5398   if (class1 == class2)
5399     return class1;
5400
5401   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5402      the other class.  */
5403   if (class1 == X86_64_NO_CLASS)
5404     return class2;
5405   if (class2 == X86_64_NO_CLASS)
5406     return class1;
5407
5408   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5409   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5410     return X86_64_MEMORY_CLASS;
5411
5412   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5413   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5414       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5415     return X86_64_INTEGERSI_CLASS;
5416   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5417       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5418     return X86_64_INTEGER_CLASS;
5419
5420   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5421      MEMORY is used.  */
5422   if (class1 == X86_64_X87_CLASS
5423       || class1 == X86_64_X87UP_CLASS
5424       || class1 == X86_64_COMPLEX_X87_CLASS
5425       || class2 == X86_64_X87_CLASS
5426       || class2 == X86_64_X87UP_CLASS
5427       || class2 == X86_64_COMPLEX_X87_CLASS)
5428     return X86_64_MEMORY_CLASS;
5429
5430   /* Rule #6: Otherwise class SSE is used.  */
5431   return X86_64_SSE_CLASS;
5432 }
5433
5434 /* Classify the argument of type TYPE and mode MODE.
5435    CLASSES will be filled by the register class used to pass each word
5436    of the operand.  The number of words is returned.  In case the parameter
5437    should be passed in memory, 0 is returned. As a special case for zero
5438    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5439
5440    BIT_OFFSET is used internally for handling records and specifies offset
5441    of the offset in bits modulo 256 to avoid overflow cases.
5442
5443    See the x86-64 PS ABI for details.
5444 */
5445
5446 static int
5447 classify_argument (enum machine_mode mode, const_tree type,
5448                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5449 {
5450   HOST_WIDE_INT bytes =
5451     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5452   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5453
5454   /* Variable sized entities are always passed/returned in memory.  */
5455   if (bytes < 0)
5456     return 0;
5457
5458   if (mode != VOIDmode
5459       && targetm.calls.must_pass_in_stack (mode, type))
5460     return 0;
5461
5462   if (type && AGGREGATE_TYPE_P (type))
5463     {
5464       int i;
5465       tree field;
5466       enum x86_64_reg_class subclasses[MAX_CLASSES];
5467
5468       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5469       if (bytes > 32)
5470         return 0;
5471
5472       for (i = 0; i < words; i++)
5473         classes[i] = X86_64_NO_CLASS;
5474
5475       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5476          signalize memory class, so handle it as special case.  */
5477       if (!words)
5478         {
5479           classes[0] = X86_64_NO_CLASS;
5480           return 1;
5481         }
5482
5483       /* Classify each field of record and merge classes.  */
5484       switch (TREE_CODE (type))
5485         {
5486         case RECORD_TYPE:
5487           /* And now merge the fields of structure.  */
5488           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5489             {
5490               if (TREE_CODE (field) == FIELD_DECL)
5491                 {
5492                   int num;
5493
5494                   if (TREE_TYPE (field) == error_mark_node)
5495                     continue;
5496
5497                   /* Bitfields are always classified as integer.  Handle them
5498                      early, since later code would consider them to be
5499                      misaligned integers.  */
5500                   if (DECL_BIT_FIELD (field))
5501                     {
5502                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5503                            i < ((int_bit_position (field) + (bit_offset % 64))
5504                                 + tree_low_cst (DECL_SIZE (field), 0)
5505                                 + 63) / 8 / 8; i++)
5506                         classes[i] =
5507                           merge_classes (X86_64_INTEGER_CLASS,
5508                                          classes[i]);
5509                     }
5510                   else
5511                     {
5512                       int pos;
5513
5514                       type = TREE_TYPE (field);
5515
5516                       /* Flexible array member is ignored.  */
5517                       if (TYPE_MODE (type) == BLKmode
5518                           && TREE_CODE (type) == ARRAY_TYPE
5519                           && TYPE_SIZE (type) == NULL_TREE
5520                           && TYPE_DOMAIN (type) != NULL_TREE
5521                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5522                               == NULL_TREE))
5523                         {
5524                           static bool warned;
5525
5526                           if (!warned && warn_psabi)
5527                             {
5528                               warned = true;
5529                               inform (input_location,
5530                                       "The ABI of passing struct with"
5531                                       " a flexible array member has"
5532                                       " changed in GCC 4.4");
5533                             }
5534                           continue;
5535                         }
5536                       num = classify_argument (TYPE_MODE (type), type,
5537                                                subclasses,
5538                                                (int_bit_position (field)
5539                                                 + bit_offset) % 256);
5540                       if (!num)
5541                         return 0;
5542                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5543                       for (i = 0; i < num && (i + pos) < words; i++)
5544                         classes[i + pos] =
5545                           merge_classes (subclasses[i], classes[i + pos]);
5546                     }
5547                 }
5548             }
5549           break;
5550
5551         case ARRAY_TYPE:
5552           /* Arrays are handled as small records.  */
5553           {
5554             int num;
5555             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
5556                                      TREE_TYPE (type), subclasses, bit_offset);
5557             if (!num)
5558               return 0;
5559
5560             /* The partial classes are now full classes.  */
5561             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
5562               subclasses[0] = X86_64_SSE_CLASS;
5563             if (subclasses[0] == X86_64_INTEGERSI_CLASS
5564                 && !((bit_offset % 64) == 0 && bytes == 4))
5565               subclasses[0] = X86_64_INTEGER_CLASS;
5566
5567             for (i = 0; i < words; i++)
5568               classes[i] = subclasses[i % num];
5569
5570             break;
5571           }
5572         case UNION_TYPE:
5573         case QUAL_UNION_TYPE:
5574           /* Unions are similar to RECORD_TYPE but offset is always 0.
5575              */
5576           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5577             {
5578               if (TREE_CODE (field) == FIELD_DECL)
5579                 {
5580                   int num;
5581
5582                   if (TREE_TYPE (field) == error_mark_node)
5583                     continue;
5584
5585                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
5586                                            TREE_TYPE (field), subclasses,
5587                                            bit_offset);
5588                   if (!num)
5589                     return 0;
5590                   for (i = 0; i < num; i++)
5591                     classes[i] = merge_classes (subclasses[i], classes[i]);
5592                 }
5593             }
5594           break;
5595
5596         default:
5597           gcc_unreachable ();
5598         }
5599
5600       if (words > 2)
5601         {
5602           /* When size > 16 bytes, if the first one isn't
5603              X86_64_SSE_CLASS or any other ones aren't
5604              X86_64_SSEUP_CLASS, everything should be passed in
5605              memory.  */
5606           if (classes[0] != X86_64_SSE_CLASS)
5607               return 0;
5608
5609           for (i = 1; i < words; i++)
5610             if (classes[i] != X86_64_SSEUP_CLASS)
5611               return 0;
5612         }
5613
5614       /* Final merger cleanup.  */
5615       for (i = 0; i < words; i++)
5616         {
5617           /* If one class is MEMORY, everything should be passed in
5618              memory.  */
5619           if (classes[i] == X86_64_MEMORY_CLASS)
5620             return 0;
5621
5622           /* The X86_64_SSEUP_CLASS should be always preceded by
5623              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
5624           if (classes[i] == X86_64_SSEUP_CLASS
5625               && classes[i - 1] != X86_64_SSE_CLASS
5626               && classes[i - 1] != X86_64_SSEUP_CLASS)
5627             {
5628               /* The first one should never be X86_64_SSEUP_CLASS.  */
5629               gcc_assert (i != 0);
5630               classes[i] = X86_64_SSE_CLASS;
5631             }
5632
5633           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
5634                everything should be passed in memory.  */
5635           if (classes[i] == X86_64_X87UP_CLASS
5636               && (classes[i - 1] != X86_64_X87_CLASS))
5637             {
5638               static bool warned;
5639
5640               /* The first one should never be X86_64_X87UP_CLASS.  */
5641               gcc_assert (i != 0);
5642               if (!warned && warn_psabi)
5643                 {
5644                   warned = true;
5645                   inform (input_location,
5646                           "The ABI of passing union with long double"
5647                           " has changed in GCC 4.4");
5648                 }
5649               return 0;
5650             }
5651         }
5652       return words;
5653     }
5654
5655   /* Compute alignment needed.  We align all types to natural boundaries with
5656      exception of XFmode that is aligned to 64bits.  */
5657   if (mode != VOIDmode && mode != BLKmode)
5658     {
5659       int mode_alignment = GET_MODE_BITSIZE (mode);
5660
5661       if (mode == XFmode)
5662         mode_alignment = 128;
5663       else if (mode == XCmode)
5664         mode_alignment = 256;
5665       if (COMPLEX_MODE_P (mode))
5666         mode_alignment /= 2;
5667       /* Misaligned fields are always returned in memory.  */
5668       if (bit_offset % mode_alignment)
5669         return 0;
5670     }
5671
5672   /* for V1xx modes, just use the base mode */
5673   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
5674       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
5675     mode = GET_MODE_INNER (mode);
5676
5677   /* Classification of atomic types.  */
5678   switch (mode)
5679     {
5680     case SDmode:
5681     case DDmode:
5682       classes[0] = X86_64_SSE_CLASS;
5683       return 1;
5684     case TDmode:
5685       classes[0] = X86_64_SSE_CLASS;
5686       classes[1] = X86_64_SSEUP_CLASS;
5687       return 2;
5688     case DImode:
5689     case SImode:
5690     case HImode:
5691     case QImode:
5692     case CSImode:
5693     case CHImode:
5694     case CQImode:
5695       {
5696         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
5697
5698         if (size <= 32)
5699           {
5700             classes[0] = X86_64_INTEGERSI_CLASS;
5701             return 1;
5702           }
5703         else if (size <= 64)
5704           {
5705             classes[0] = X86_64_INTEGER_CLASS;
5706             return 1;
5707           }
5708         else if (size <= 64+32)
5709           {
5710             classes[0] = X86_64_INTEGER_CLASS;
5711             classes[1] = X86_64_INTEGERSI_CLASS;
5712             return 2;
5713           }
5714         else if (size <= 64+64)
5715           {
5716             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5717             return 2;
5718           }
5719         else
5720           gcc_unreachable ();
5721       }
5722     case CDImode:
5723     case TImode:
5724       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5725       return 2;
5726     case COImode:
5727     case OImode:
5728       /* OImode shouldn't be used directly.  */
5729       gcc_unreachable ();
5730     case CTImode:
5731       return 0;
5732     case SFmode:
5733       if (!(bit_offset % 64))
5734         classes[0] = X86_64_SSESF_CLASS;
5735       else
5736         classes[0] = X86_64_SSE_CLASS;
5737       return 1;
5738     case DFmode:
5739       classes[0] = X86_64_SSEDF_CLASS;
5740       return 1;
5741     case XFmode:
5742       classes[0] = X86_64_X87_CLASS;
5743       classes[1] = X86_64_X87UP_CLASS;
5744       return 2;
5745     case TFmode:
5746       classes[0] = X86_64_SSE_CLASS;
5747       classes[1] = X86_64_SSEUP_CLASS;
5748       return 2;
5749     case SCmode:
5750       classes[0] = X86_64_SSE_CLASS;
5751       if (!(bit_offset % 64))
5752         return 1;
5753       else
5754         {
5755           static bool warned;
5756
5757           if (!warned && warn_psabi)
5758             {
5759               warned = true;
5760               inform (input_location,
5761                       "The ABI of passing structure with complex float"
5762                       " member has changed in GCC 4.4");
5763             }
5764           classes[1] = X86_64_SSESF_CLASS;
5765           return 2;
5766         }
5767     case DCmode:
5768       classes[0] = X86_64_SSEDF_CLASS;
5769       classes[1] = X86_64_SSEDF_CLASS;
5770       return 2;
5771     case XCmode:
5772       classes[0] = X86_64_COMPLEX_X87_CLASS;
5773       return 1;
5774     case TCmode:
5775       /* This modes is larger than 16 bytes.  */
5776       return 0;
5777     case V8SFmode:
5778     case V8SImode:
5779     case V32QImode:
5780     case V16HImode:
5781     case V4DFmode:
5782     case V4DImode:
5783       classes[0] = X86_64_SSE_CLASS;
5784       classes[1] = X86_64_SSEUP_CLASS;
5785       classes[2] = X86_64_SSEUP_CLASS;
5786       classes[3] = X86_64_SSEUP_CLASS;
5787       return 4;
5788     case V4SFmode:
5789     case V4SImode:
5790     case V16QImode:
5791     case V8HImode:
5792     case V2DFmode:
5793     case V2DImode:
5794       classes[0] = X86_64_SSE_CLASS;
5795       classes[1] = X86_64_SSEUP_CLASS;
5796       return 2;
5797     case V1TImode:
5798     case V1DImode:
5799     case V2SFmode:
5800     case V2SImode:
5801     case V4HImode:
5802     case V8QImode:
5803       classes[0] = X86_64_SSE_CLASS;
5804       return 1;
5805     case BLKmode:
5806     case VOIDmode:
5807       return 0;
5808     default:
5809       gcc_assert (VECTOR_MODE_P (mode));
5810
5811       if (bytes > 16)
5812         return 0;
5813
5814       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
5815
5816       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
5817         classes[0] = X86_64_INTEGERSI_CLASS;
5818       else
5819         classes[0] = X86_64_INTEGER_CLASS;
5820       classes[1] = X86_64_INTEGER_CLASS;
5821       return 1 + (bytes > 8);
5822     }
5823 }
5824
5825 /* Examine the argument and return set number of register required in each
5826    class.  Return 0 iff parameter should be passed in memory.  */
5827 static int
5828 examine_argument (enum machine_mode mode, const_tree type, int in_return,
5829                   int *int_nregs, int *sse_nregs)
5830 {
5831   enum x86_64_reg_class regclass[MAX_CLASSES];
5832   int n = classify_argument (mode, type, regclass, 0);
5833
5834   *int_nregs = 0;
5835   *sse_nregs = 0;
5836   if (!n)
5837     return 0;
5838   for (n--; n >= 0; n--)
5839     switch (regclass[n])
5840       {
5841       case X86_64_INTEGER_CLASS:
5842       case X86_64_INTEGERSI_CLASS:
5843         (*int_nregs)++;
5844         break;
5845       case X86_64_SSE_CLASS:
5846       case X86_64_SSESF_CLASS:
5847       case X86_64_SSEDF_CLASS:
5848         (*sse_nregs)++;
5849         break;
5850       case X86_64_NO_CLASS:
5851       case X86_64_SSEUP_CLASS:
5852         break;
5853       case X86_64_X87_CLASS:
5854       case X86_64_X87UP_CLASS:
5855         if (!in_return)
5856           return 0;
5857         break;
5858       case X86_64_COMPLEX_X87_CLASS:
5859         return in_return ? 2 : 0;
5860       case X86_64_MEMORY_CLASS:
5861         gcc_unreachable ();
5862       }
5863   return 1;
5864 }
5865
5866 /* Construct container for the argument used by GCC interface.  See
5867    FUNCTION_ARG for the detailed description.  */
5868
5869 static rtx
5870 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
5871                      const_tree type, int in_return, int nintregs, int nsseregs,
5872                      const int *intreg, int sse_regno)
5873 {
5874   /* The following variables hold the static issued_error state.  */
5875   static bool issued_sse_arg_error;
5876   static bool issued_sse_ret_error;
5877   static bool issued_x87_ret_error;
5878
5879   enum machine_mode tmpmode;
5880   int bytes =
5881     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5882   enum x86_64_reg_class regclass[MAX_CLASSES];
5883   int n;
5884   int i;
5885   int nexps = 0;
5886   int needed_sseregs, needed_intregs;
5887   rtx exp[MAX_CLASSES];
5888   rtx ret;
5889
5890   n = classify_argument (mode, type, regclass, 0);
5891   if (!n)
5892     return NULL;
5893   if (!examine_argument (mode, type, in_return, &needed_intregs,
5894                          &needed_sseregs))
5895     return NULL;
5896   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
5897     return NULL;
5898
5899   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
5900      some less clueful developer tries to use floating-point anyway.  */
5901   if (needed_sseregs && !TARGET_SSE)
5902     {
5903       if (in_return)
5904         {
5905           if (!issued_sse_ret_error)
5906             {
5907               error ("SSE register return with SSE disabled");
5908               issued_sse_ret_error = true;
5909             }
5910         }
5911       else if (!issued_sse_arg_error)
5912         {
5913           error ("SSE register argument with SSE disabled");
5914           issued_sse_arg_error = true;
5915         }
5916       return NULL;
5917     }
5918
5919   /* Likewise, error if the ABI requires us to return values in the
5920      x87 registers and the user specified -mno-80387.  */
5921   if (!TARGET_80387 && in_return)
5922     for (i = 0; i < n; i++)
5923       if (regclass[i] == X86_64_X87_CLASS
5924           || regclass[i] == X86_64_X87UP_CLASS
5925           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
5926         {
5927           if (!issued_x87_ret_error)
5928             {
5929               error ("x87 register return with x87 disabled");
5930               issued_x87_ret_error = true;
5931             }
5932           return NULL;
5933         }
5934
5935   /* First construct simple cases.  Avoid SCmode, since we want to use
5936      single register to pass this type.  */
5937   if (n == 1 && mode != SCmode)
5938     switch (regclass[0])
5939       {
5940       case X86_64_INTEGER_CLASS:
5941       case X86_64_INTEGERSI_CLASS:
5942         return gen_rtx_REG (mode, intreg[0]);
5943       case X86_64_SSE_CLASS:
5944       case X86_64_SSESF_CLASS:
5945       case X86_64_SSEDF_CLASS:
5946         if (mode != BLKmode)
5947           return gen_reg_or_parallel (mode, orig_mode,
5948                                       SSE_REGNO (sse_regno));
5949         break;
5950       case X86_64_X87_CLASS:
5951       case X86_64_COMPLEX_X87_CLASS:
5952         return gen_rtx_REG (mode, FIRST_STACK_REG);
5953       case X86_64_NO_CLASS:
5954         /* Zero sized array, struct or class.  */
5955         return NULL;
5956       default:
5957         gcc_unreachable ();
5958       }
5959   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
5960       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
5961     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
5962   if (n == 4
5963       && regclass[0] == X86_64_SSE_CLASS
5964       && regclass[1] == X86_64_SSEUP_CLASS
5965       && regclass[2] == X86_64_SSEUP_CLASS
5966       && regclass[3] == X86_64_SSEUP_CLASS
5967       && mode != BLKmode)
5968     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
5969
5970   if (n == 2
5971       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
5972     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
5973   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
5974       && regclass[1] == X86_64_INTEGER_CLASS
5975       && (mode == CDImode || mode == TImode || mode == TFmode)
5976       && intreg[0] + 1 == intreg[1])
5977     return gen_rtx_REG (mode, intreg[0]);
5978
5979   /* Otherwise figure out the entries of the PARALLEL.  */
5980   for (i = 0; i < n; i++)
5981     {
5982       int pos;
5983
5984       switch (regclass[i])
5985         {
5986           case X86_64_NO_CLASS:
5987             break;
5988           case X86_64_INTEGER_CLASS:
5989           case X86_64_INTEGERSI_CLASS:
5990             /* Merge TImodes on aligned occasions here too.  */
5991             if (i * 8 + 8 > bytes)
5992               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
5993             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
5994               tmpmode = SImode;
5995             else
5996               tmpmode = DImode;
5997             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
5998             if (tmpmode == BLKmode)
5999               tmpmode = DImode;
6000             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6001                                                gen_rtx_REG (tmpmode, *intreg),
6002                                                GEN_INT (i*8));
6003             intreg++;
6004             break;
6005           case X86_64_SSESF_CLASS:
6006             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6007                                                gen_rtx_REG (SFmode,
6008                                                             SSE_REGNO (sse_regno)),
6009                                                GEN_INT (i*8));
6010             sse_regno++;
6011             break;
6012           case X86_64_SSEDF_CLASS:
6013             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6014                                                gen_rtx_REG (DFmode,
6015                                                             SSE_REGNO (sse_regno)),
6016                                                GEN_INT (i*8));
6017             sse_regno++;
6018             break;
6019           case X86_64_SSE_CLASS:
6020             pos = i;
6021             switch (n)
6022               {
6023               case 1:
6024                 tmpmode = DImode;
6025                 break;
6026               case 2:
6027                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6028                   {
6029                     tmpmode = TImode;
6030                     i++;
6031                   }
6032                 else
6033                   tmpmode = DImode;
6034                 break;
6035               case 4:
6036                 gcc_assert (i == 0
6037                             && regclass[1] == X86_64_SSEUP_CLASS
6038                             && regclass[2] == X86_64_SSEUP_CLASS
6039                             && regclass[3] == X86_64_SSEUP_CLASS);
6040                 tmpmode = OImode;
6041                 i += 3;
6042                 break;
6043               default:
6044                 gcc_unreachable ();
6045               }
6046             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6047                                                gen_rtx_REG (tmpmode,
6048                                                             SSE_REGNO (sse_regno)),
6049                                                GEN_INT (pos*8));
6050             sse_regno++;
6051             break;
6052           default:
6053             gcc_unreachable ();
6054         }
6055     }
6056
6057   /* Empty aligned struct, union or class.  */
6058   if (nexps == 0)
6059     return NULL;
6060
6061   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6062   for (i = 0; i < nexps; i++)
6063     XVECEXP (ret, 0, i) = exp [i];
6064   return ret;
6065 }
6066
6067 /* Update the data in CUM to advance over an argument of mode MODE
6068    and data type TYPE.  (TYPE is null for libcalls where that information
6069    may not be available.)  */
6070
6071 static void
6072 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6073                          const_tree type, HOST_WIDE_INT bytes,
6074                          HOST_WIDE_INT words)
6075 {
6076   switch (mode)
6077     {
6078     default:
6079       break;
6080
6081     case BLKmode:
6082       if (bytes < 0)
6083         break;
6084       /* FALLTHRU */
6085
6086     case DImode:
6087     case SImode:
6088     case HImode:
6089     case QImode:
6090       cum->words += words;
6091       cum->nregs -= words;
6092       cum->regno += words;
6093
6094       if (cum->nregs <= 0)
6095         {
6096           cum->nregs = 0;
6097           cum->regno = 0;
6098         }
6099       break;
6100
6101     case OImode:
6102       /* OImode shouldn't be used directly.  */
6103       gcc_unreachable ();
6104
6105     case DFmode:
6106       if (cum->float_in_sse < 2)
6107         break;
6108     case SFmode:
6109       if (cum->float_in_sse < 1)
6110         break;
6111       /* FALLTHRU */
6112
6113     case V8SFmode:
6114     case V8SImode:
6115     case V32QImode:
6116     case V16HImode:
6117     case V4DFmode:
6118     case V4DImode:
6119     case TImode:
6120     case V16QImode:
6121     case V8HImode:
6122     case V4SImode:
6123     case V2DImode:
6124     case V4SFmode:
6125     case V2DFmode:
6126       if (!type || !AGGREGATE_TYPE_P (type))
6127         {
6128           cum->sse_words += words;
6129           cum->sse_nregs -= 1;
6130           cum->sse_regno += 1;
6131           if (cum->sse_nregs <= 0)
6132             {
6133               cum->sse_nregs = 0;
6134               cum->sse_regno = 0;
6135             }
6136         }
6137       break;
6138
6139     case V8QImode:
6140     case V4HImode:
6141     case V2SImode:
6142     case V2SFmode:
6143     case V1TImode:
6144     case V1DImode:
6145       if (!type || !AGGREGATE_TYPE_P (type))
6146         {
6147           cum->mmx_words += words;
6148           cum->mmx_nregs -= 1;
6149           cum->mmx_regno += 1;
6150           if (cum->mmx_nregs <= 0)
6151             {
6152               cum->mmx_nregs = 0;
6153               cum->mmx_regno = 0;
6154             }
6155         }
6156       break;
6157     }
6158 }
6159
6160 static void
6161 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6162                          const_tree type, HOST_WIDE_INT words, bool named)
6163 {
6164   int int_nregs, sse_nregs;
6165
6166   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6167   if (!named && VALID_AVX256_REG_MODE (mode))
6168     return;
6169
6170   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6171       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6172     {
6173       cum->nregs -= int_nregs;
6174       cum->sse_nregs -= sse_nregs;
6175       cum->regno += int_nregs;
6176       cum->sse_regno += sse_nregs;
6177     }
6178   else
6179     {
6180       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6181       cum->words = (cum->words + align - 1) & ~(align - 1);
6182       cum->words += words;
6183     }
6184 }
6185
6186 static void
6187 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6188                             HOST_WIDE_INT words)
6189 {
6190   /* Otherwise, this should be passed indirect.  */
6191   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6192
6193   cum->words += words;
6194   if (cum->nregs > 0)
6195     {
6196       cum->nregs -= 1;
6197       cum->regno += 1;
6198     }
6199 }
6200
6201 /* Update the data in CUM to advance over an argument of mode MODE and
6202    data type TYPE.  (TYPE is null for libcalls where that information
6203    may not be available.)  */
6204
6205 static void
6206 ix86_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6207                            const_tree type, bool named)
6208 {
6209   HOST_WIDE_INT bytes, words;
6210
6211   if (mode == BLKmode)
6212     bytes = int_size_in_bytes (type);
6213   else
6214     bytes = GET_MODE_SIZE (mode);
6215   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6216
6217   if (type)
6218     mode = type_natural_mode (type, NULL);
6219
6220   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6221     function_arg_advance_ms_64 (cum, bytes, words);
6222   else if (TARGET_64BIT)
6223     function_arg_advance_64 (cum, mode, type, words, named);
6224   else
6225     function_arg_advance_32 (cum, mode, type, bytes, words);
6226 }
6227
6228 /* Define where to put the arguments to a function.
6229    Value is zero to push the argument on the stack,
6230    or a hard register in which to store the argument.
6231
6232    MODE is the argument's machine mode.
6233    TYPE is the data type of the argument (as a tree).
6234     This is null for libcalls where that information may
6235     not be available.
6236    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6237     the preceding args and about the function being called.
6238    NAMED is nonzero if this argument is a named parameter
6239     (otherwise it is an extra parameter matching an ellipsis).  */
6240
6241 static rtx
6242 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6243                  enum machine_mode orig_mode, const_tree type,
6244                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6245 {
6246   static bool warnedsse, warnedmmx;
6247
6248   /* Avoid the AL settings for the Unix64 ABI.  */
6249   if (mode == VOIDmode)
6250     return constm1_rtx;
6251
6252   switch (mode)
6253     {
6254     default:
6255       break;
6256
6257     case BLKmode:
6258       if (bytes < 0)
6259         break;
6260       /* FALLTHRU */
6261     case DImode:
6262     case SImode:
6263     case HImode:
6264     case QImode:
6265       if (words <= cum->nregs)
6266         {
6267           int regno = cum->regno;
6268
6269           /* Fastcall allocates the first two DWORD (SImode) or
6270             smaller arguments to ECX and EDX if it isn't an
6271             aggregate type .  */
6272           if (cum->fastcall)
6273             {
6274               if (mode == BLKmode
6275                   || mode == DImode
6276                   || (type && AGGREGATE_TYPE_P (type)))
6277                 break;
6278
6279               /* ECX not EAX is the first allocated register.  */
6280               if (regno == AX_REG)
6281                 regno = CX_REG;
6282             }
6283           return gen_rtx_REG (mode, regno);
6284         }
6285       break;
6286
6287     case DFmode:
6288       if (cum->float_in_sse < 2)
6289         break;
6290     case SFmode:
6291       if (cum->float_in_sse < 1)
6292         break;
6293       /* FALLTHRU */
6294     case TImode:
6295       /* In 32bit, we pass TImode in xmm registers.  */
6296     case V16QImode:
6297     case V8HImode:
6298     case V4SImode:
6299     case V2DImode:
6300     case V4SFmode:
6301     case V2DFmode:
6302       if (!type || !AGGREGATE_TYPE_P (type))
6303         {
6304           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6305             {
6306               warnedsse = true;
6307               warning (0, "SSE vector argument without SSE enabled "
6308                        "changes the ABI");
6309             }
6310           if (cum->sse_nregs)
6311             return gen_reg_or_parallel (mode, orig_mode,
6312                                         cum->sse_regno + FIRST_SSE_REG);
6313         }
6314       break;
6315
6316     case OImode:
6317       /* OImode shouldn't be used directly.  */
6318       gcc_unreachable ();
6319
6320     case V8SFmode:
6321     case V8SImode:
6322     case V32QImode:
6323     case V16HImode:
6324     case V4DFmode:
6325     case V4DImode:
6326       if (!type || !AGGREGATE_TYPE_P (type))
6327         {
6328           if (cum->sse_nregs)
6329             return gen_reg_or_parallel (mode, orig_mode,
6330                                         cum->sse_regno + FIRST_SSE_REG);
6331         }
6332       break;
6333
6334     case V8QImode:
6335     case V4HImode:
6336     case V2SImode:
6337     case V2SFmode:
6338     case V1TImode:
6339     case V1DImode:
6340       if (!type || !AGGREGATE_TYPE_P (type))
6341         {
6342           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6343             {
6344               warnedmmx = true;
6345               warning (0, "MMX vector argument without MMX enabled "
6346                        "changes the ABI");
6347             }
6348           if (cum->mmx_nregs)
6349             return gen_reg_or_parallel (mode, orig_mode,
6350                                         cum->mmx_regno + FIRST_MMX_REG);
6351         }
6352       break;
6353     }
6354
6355   return NULL_RTX;
6356 }
6357
6358 static rtx
6359 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6360                  enum machine_mode orig_mode, const_tree type, bool named)
6361 {
6362   /* Handle a hidden AL argument containing number of registers
6363      for varargs x86-64 functions.  */
6364   if (mode == VOIDmode)
6365     return GEN_INT (cum->maybe_vaarg
6366                     ? (cum->sse_nregs < 0
6367                        ? X86_64_SSE_REGPARM_MAX
6368                        : cum->sse_regno)
6369                     : -1);
6370
6371   switch (mode)
6372     {
6373     default:
6374       break;
6375
6376     case V8SFmode:
6377     case V8SImode:
6378     case V32QImode:
6379     case V16HImode:
6380     case V4DFmode:
6381     case V4DImode:
6382       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6383       if (!named)
6384         return NULL;
6385       break;
6386     }
6387
6388   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6389                               cum->sse_nregs,
6390                               &x86_64_int_parameter_registers [cum->regno],
6391                               cum->sse_regno);
6392 }
6393
6394 static rtx
6395 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6396                     enum machine_mode orig_mode, bool named,
6397                     HOST_WIDE_INT bytes)
6398 {
6399   unsigned int regno;
6400
6401   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6402      We use value of -2 to specify that current function call is MSABI.  */
6403   if (mode == VOIDmode)
6404     return GEN_INT (-2);
6405
6406   /* If we've run out of registers, it goes on the stack.  */
6407   if (cum->nregs == 0)
6408     return NULL_RTX;
6409
6410   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6411
6412   /* Only floating point modes are passed in anything but integer regs.  */
6413   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6414     {
6415       if (named)
6416         regno = cum->regno + FIRST_SSE_REG;
6417       else
6418         {
6419           rtx t1, t2;
6420
6421           /* Unnamed floating parameters are passed in both the
6422              SSE and integer registers.  */
6423           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6424           t2 = gen_rtx_REG (mode, regno);
6425           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6426           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6427           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6428         }
6429     }
6430   /* Handle aggregated types passed in register.  */
6431   if (orig_mode == BLKmode)
6432     {
6433       if (bytes > 0 && bytes <= 8)
6434         mode = (bytes > 4 ? DImode : SImode);
6435       if (mode == BLKmode)
6436         mode = DImode;
6437     }
6438
6439   return gen_reg_or_parallel (mode, orig_mode, regno);
6440 }
6441
6442 /* Return where to put the arguments to a function.
6443    Return zero to push the argument on the stack, or a hard register in which to store the argument.
6444
6445    MODE is the argument's machine mode.  TYPE is the data type of the
6446    argument.  It is null for libcalls where that information may not be
6447    available.  CUM gives information about the preceding args and about
6448    the function being called.  NAMED is nonzero if this argument is a
6449    named parameter (otherwise it is an extra parameter matching an
6450    ellipsis).  */
6451
6452 static rtx
6453 ix86_function_arg (const CUMULATIVE_ARGS *cum, enum machine_mode omode,
6454                    const_tree type, bool named)
6455 {
6456   enum machine_mode mode = omode;
6457   HOST_WIDE_INT bytes, words;
6458
6459   if (mode == BLKmode)
6460     bytes = int_size_in_bytes (type);
6461   else
6462     bytes = GET_MODE_SIZE (mode);
6463   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6464
6465   /* To simplify the code below, represent vector types with a vector mode
6466      even if MMX/SSE are not active.  */
6467   if (type && TREE_CODE (type) == VECTOR_TYPE)
6468     mode = type_natural_mode (type, cum);
6469
6470   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6471     return function_arg_ms_64 (cum, mode, omode, named, bytes);
6472   else if (TARGET_64BIT)
6473     return function_arg_64 (cum, mode, omode, type, named);
6474   else
6475     return function_arg_32 (cum, mode, omode, type, bytes, words);
6476 }
6477
6478 /* A C expression that indicates when an argument must be passed by
6479    reference.  If nonzero for an argument, a copy of that argument is
6480    made in memory and a pointer to the argument is passed instead of
6481    the argument itself.  The pointer is passed in whatever way is
6482    appropriate for passing a pointer to that type.  */
6483
6484 static bool
6485 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
6486                         enum machine_mode mode ATTRIBUTE_UNUSED,
6487                         const_tree type, bool named ATTRIBUTE_UNUSED)
6488 {
6489   /* See Windows x64 Software Convention.  */
6490   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6491     {
6492       int msize = (int) GET_MODE_SIZE (mode);
6493       if (type)
6494         {
6495           /* Arrays are passed by reference.  */
6496           if (TREE_CODE (type) == ARRAY_TYPE)
6497             return true;
6498
6499           if (AGGREGATE_TYPE_P (type))
6500             {
6501               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6502                  are passed by reference.  */
6503               msize = int_size_in_bytes (type);
6504             }
6505         }
6506
6507       /* __m128 is passed by reference.  */
6508       switch (msize) {
6509       case 1: case 2: case 4: case 8:
6510         break;
6511       default:
6512         return true;
6513       }
6514     }
6515   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
6516     return 1;
6517
6518   return 0;
6519 }
6520
6521 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
6522    ABI.  */
6523 static bool
6524 contains_aligned_value_p (const_tree type)
6525 {
6526   enum machine_mode mode = TYPE_MODE (type);
6527   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
6528        || mode == TDmode
6529        || mode == TFmode
6530        || mode == TCmode)
6531       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
6532     return true;
6533   if (TYPE_ALIGN (type) < 128)
6534     return false;
6535
6536   if (AGGREGATE_TYPE_P (type))
6537     {
6538       /* Walk the aggregates recursively.  */
6539       switch (TREE_CODE (type))
6540         {
6541         case RECORD_TYPE:
6542         case UNION_TYPE:
6543         case QUAL_UNION_TYPE:
6544           {
6545             tree field;
6546
6547             /* Walk all the structure fields.  */
6548             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6549               {
6550                 if (TREE_CODE (field) == FIELD_DECL
6551                     && contains_aligned_value_p (TREE_TYPE (field)))
6552                   return true;
6553               }
6554             break;
6555           }
6556
6557         case ARRAY_TYPE:
6558           /* Just for use if some languages passes arrays by value.  */
6559           if (contains_aligned_value_p (TREE_TYPE (type)))
6560             return true;
6561           break;
6562
6563         default:
6564           gcc_unreachable ();
6565         }
6566     }
6567   return false;
6568 }
6569
6570 /* Gives the alignment boundary, in bits, of an argument with the
6571    specified mode and type.  */
6572
6573 int
6574 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
6575 {
6576   int align;
6577   if (type)
6578     {
6579       /* Since the main variant type is used for call, we convert it to
6580          the main variant type.  */
6581       type = TYPE_MAIN_VARIANT (type);
6582       align = TYPE_ALIGN (type);
6583     }
6584   else
6585     align = GET_MODE_ALIGNMENT (mode);
6586   if (align < PARM_BOUNDARY)
6587     align = PARM_BOUNDARY;
6588   /* In 32bit, only _Decimal128 and __float128 are aligned to their
6589      natural boundaries.  */
6590   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
6591     {
6592       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
6593          make an exception for SSE modes since these require 128bit
6594          alignment.
6595
6596          The handling here differs from field_alignment.  ICC aligns MMX
6597          arguments to 4 byte boundaries, while structure fields are aligned
6598          to 8 byte boundaries.  */
6599       if (!type)
6600         {
6601           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
6602             align = PARM_BOUNDARY;
6603         }
6604       else
6605         {
6606           if (!contains_aligned_value_p (type))
6607             align = PARM_BOUNDARY;
6608         }
6609     }
6610   if (align > BIGGEST_ALIGNMENT)
6611     align = BIGGEST_ALIGNMENT;
6612   return align;
6613 }
6614
6615 /* Return true if N is a possible register number of function value.  */
6616
6617 static bool
6618 ix86_function_value_regno_p (const unsigned int regno)
6619 {
6620   switch (regno)
6621     {
6622     case 0:
6623       return true;
6624
6625     case FIRST_FLOAT_REG:
6626       /* TODO: The function should depend on current function ABI but
6627        builtins.c would need updating then. Therefore we use the
6628        default ABI.  */
6629       if (TARGET_64BIT && ix86_abi == MS_ABI)
6630         return false;
6631       return TARGET_FLOAT_RETURNS_IN_80387;
6632
6633     case FIRST_SSE_REG:
6634       return TARGET_SSE;
6635
6636     case FIRST_MMX_REG:
6637       if (TARGET_MACHO || TARGET_64BIT)
6638         return false;
6639       return TARGET_MMX;
6640     }
6641
6642   return false;
6643 }
6644
6645 /* Define how to find the value returned by a function.
6646    VALTYPE is the data type of the value (as a tree).
6647    If the precise function being called is known, FUNC is its FUNCTION_DECL;
6648    otherwise, FUNC is 0.  */
6649
6650 static rtx
6651 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
6652                    const_tree fntype, const_tree fn)
6653 {
6654   unsigned int regno;
6655
6656   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
6657      we normally prevent this case when mmx is not available.  However
6658      some ABIs may require the result to be returned like DImode.  */
6659   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6660     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
6661
6662   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
6663      we prevent this case when sse is not available.  However some ABIs
6664      may require the result to be returned like integer TImode.  */
6665   else if (mode == TImode
6666            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6667     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
6668
6669   /* 32-byte vector modes in %ymm0.   */
6670   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
6671     regno = TARGET_AVX ? FIRST_SSE_REG : 0;
6672
6673   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
6674   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
6675     regno = FIRST_FLOAT_REG;
6676   else
6677     /* Most things go in %eax.  */
6678     regno = AX_REG;
6679
6680   /* Override FP return register with %xmm0 for local functions when
6681      SSE math is enabled or for functions with sseregparm attribute.  */
6682   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
6683     {
6684       int sse_level = ix86_function_sseregparm (fntype, fn, false);
6685       if ((sse_level >= 1 && mode == SFmode)
6686           || (sse_level == 2 && mode == DFmode))
6687         regno = FIRST_SSE_REG;
6688     }
6689
6690   /* OImode shouldn't be used directly.  */
6691   gcc_assert (mode != OImode);
6692
6693   return gen_rtx_REG (orig_mode, regno);
6694 }
6695
6696 static rtx
6697 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
6698                    const_tree valtype)
6699 {
6700   rtx ret;
6701
6702   /* Handle libcalls, which don't provide a type node.  */
6703   if (valtype == NULL)
6704     {
6705       switch (mode)
6706         {
6707         case SFmode:
6708         case SCmode:
6709         case DFmode:
6710         case DCmode:
6711         case TFmode:
6712         case SDmode:
6713         case DDmode:
6714         case TDmode:
6715           return gen_rtx_REG (mode, FIRST_SSE_REG);
6716         case XFmode:
6717         case XCmode:
6718           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
6719         case TCmode:
6720           return NULL;
6721         default:
6722           return gen_rtx_REG (mode, AX_REG);
6723         }
6724     }
6725
6726   ret = construct_container (mode, orig_mode, valtype, 1,
6727                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
6728                              x86_64_int_return_registers, 0);
6729
6730   /* For zero sized structures, construct_container returns NULL, but we
6731      need to keep rest of compiler happy by returning meaningful value.  */
6732   if (!ret)
6733     ret = gen_rtx_REG (orig_mode, AX_REG);
6734
6735   return ret;
6736 }
6737
6738 static rtx
6739 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
6740 {
6741   unsigned int regno = AX_REG;
6742
6743   if (TARGET_SSE)
6744     {
6745       switch (GET_MODE_SIZE (mode))
6746         {
6747         case 16:
6748           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6749              && !COMPLEX_MODE_P (mode))
6750             regno = FIRST_SSE_REG;
6751           break;
6752         case 8:
6753         case 4:
6754           if (mode == SFmode || mode == DFmode)
6755             regno = FIRST_SSE_REG;
6756           break;
6757         default:
6758           break;
6759         }
6760     }
6761   return gen_rtx_REG (orig_mode, regno);
6762 }
6763
6764 static rtx
6765 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
6766                        enum machine_mode orig_mode, enum machine_mode mode)
6767 {
6768   const_tree fn, fntype;
6769
6770   fn = NULL_TREE;
6771   if (fntype_or_decl && DECL_P (fntype_or_decl))
6772     fn = fntype_or_decl;
6773   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
6774
6775   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
6776     return function_value_ms_64 (orig_mode, mode);
6777   else if (TARGET_64BIT)
6778     return function_value_64 (orig_mode, mode, valtype);
6779   else
6780     return function_value_32 (orig_mode, mode, fntype, fn);
6781 }
6782
6783 static rtx
6784 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
6785                      bool outgoing ATTRIBUTE_UNUSED)
6786 {
6787   enum machine_mode mode, orig_mode;
6788
6789   orig_mode = TYPE_MODE (valtype);
6790   mode = type_natural_mode (valtype, NULL);
6791   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
6792 }
6793
6794 rtx
6795 ix86_libcall_value (enum machine_mode mode)
6796 {
6797   return ix86_function_value_1 (NULL, NULL, mode, mode);
6798 }
6799
6800 /* Return true iff type is returned in memory.  */
6801
6802 static int ATTRIBUTE_UNUSED
6803 return_in_memory_32 (const_tree type, enum machine_mode mode)
6804 {
6805   HOST_WIDE_INT size;
6806
6807   if (mode == BLKmode)
6808     return 1;
6809
6810   size = int_size_in_bytes (type);
6811
6812   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
6813     return 0;
6814
6815   if (VECTOR_MODE_P (mode) || mode == TImode)
6816     {
6817       /* User-created vectors small enough to fit in EAX.  */
6818       if (size < 8)
6819         return 0;
6820
6821       /* MMX/3dNow values are returned in MM0,
6822          except when it doesn't exits.  */
6823       if (size == 8)
6824         return (TARGET_MMX ? 0 : 1);
6825
6826       /* SSE values are returned in XMM0, except when it doesn't exist.  */
6827       if (size == 16)
6828         return (TARGET_SSE ? 0 : 1);
6829
6830       /* AVX values are returned in YMM0, except when it doesn't exist.  */
6831       if (size == 32)
6832         return TARGET_AVX ? 0 : 1;
6833     }
6834
6835   if (mode == XFmode)
6836     return 0;
6837
6838   if (size > 12)
6839     return 1;
6840
6841   /* OImode shouldn't be used directly.  */
6842   gcc_assert (mode != OImode);
6843
6844   return 0;
6845 }
6846
6847 static int ATTRIBUTE_UNUSED
6848 return_in_memory_64 (const_tree type, enum machine_mode mode)
6849 {
6850   int needed_intregs, needed_sseregs;
6851   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
6852 }
6853
6854 static int ATTRIBUTE_UNUSED
6855 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
6856 {
6857   HOST_WIDE_INT size = int_size_in_bytes (type);
6858
6859   /* __m128 is returned in xmm0.  */
6860   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6861       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
6862     return 0;
6863
6864   /* Otherwise, the size must be exactly in [1248]. */
6865   return (size != 1 && size != 2 && size != 4 && size != 8);
6866 }
6867
6868 static bool
6869 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6870 {
6871 #ifdef SUBTARGET_RETURN_IN_MEMORY
6872   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
6873 #else
6874   const enum machine_mode mode = type_natural_mode (type, NULL);
6875
6876   if (TARGET_64BIT)
6877     {
6878       if (ix86_function_type_abi (fntype) == MS_ABI)
6879         return return_in_memory_ms_64 (type, mode);
6880       else
6881         return return_in_memory_64 (type, mode);
6882     }
6883   else
6884     return return_in_memory_32 (type, mode);
6885 #endif
6886 }
6887
6888 /* Return false iff TYPE is returned in memory.  This version is used
6889    on Solaris 2.  It is similar to the generic ix86_return_in_memory,
6890    but differs notably in that when MMX is available, 8-byte vectors
6891    are returned in memory, rather than in MMX registers.  */
6892
6893 bool
6894 ix86_solaris_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6895 {
6896   int size;
6897   enum machine_mode mode = type_natural_mode (type, NULL);
6898
6899   if (TARGET_64BIT)
6900     return return_in_memory_64 (type, mode);
6901
6902   if (mode == BLKmode)
6903     return 1;
6904
6905   size = int_size_in_bytes (type);
6906
6907   if (VECTOR_MODE_P (mode))
6908     {
6909       /* Return in memory only if MMX registers *are* available.  This
6910          seems backwards, but it is consistent with the existing
6911          Solaris x86 ABI.  */
6912       if (size == 8)
6913         return TARGET_MMX;
6914       if (size == 16)
6915         return !TARGET_SSE;
6916     }
6917   else if (mode == TImode)
6918     return !TARGET_SSE;
6919   else if (mode == XFmode)
6920     return 0;
6921
6922   return size > 12;
6923 }
6924
6925 /* When returning SSE vector types, we have a choice of either
6926      (1) being abi incompatible with a -march switch, or
6927      (2) generating an error.
6928    Given no good solution, I think the safest thing is one warning.
6929    The user won't be able to use -Werror, but....
6930
6931    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
6932    called in response to actually generating a caller or callee that
6933    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
6934    via aggregate_value_p for general type probing from tree-ssa.  */
6935
6936 static rtx
6937 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
6938 {
6939   static bool warnedsse, warnedmmx;
6940
6941   if (!TARGET_64BIT && type)
6942     {
6943       /* Look at the return type of the function, not the function type.  */
6944       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
6945
6946       if (!TARGET_SSE && !warnedsse)
6947         {
6948           if (mode == TImode
6949               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6950             {
6951               warnedsse = true;
6952               warning (0, "SSE vector return without SSE enabled "
6953                        "changes the ABI");
6954             }
6955         }
6956
6957       if (!TARGET_MMX && !warnedmmx)
6958         {
6959           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6960             {
6961               warnedmmx = true;
6962               warning (0, "MMX vector return without MMX enabled "
6963                        "changes the ABI");
6964             }
6965         }
6966     }
6967
6968   return NULL;
6969 }
6970
6971 \f
6972 /* Create the va_list data type.  */
6973
6974 /* Returns the calling convention specific va_list date type.
6975    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
6976
6977 static tree
6978 ix86_build_builtin_va_list_abi (enum calling_abi abi)
6979 {
6980   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
6981
6982   /* For i386 we use plain pointer to argument area.  */
6983   if (!TARGET_64BIT || abi == MS_ABI)
6984     return build_pointer_type (char_type_node);
6985
6986   record = lang_hooks.types.make_type (RECORD_TYPE);
6987   type_decl = build_decl (BUILTINS_LOCATION,
6988                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
6989
6990   f_gpr = build_decl (BUILTINS_LOCATION,
6991                       FIELD_DECL, get_identifier ("gp_offset"),
6992                       unsigned_type_node);
6993   f_fpr = build_decl (BUILTINS_LOCATION,
6994                       FIELD_DECL, get_identifier ("fp_offset"),
6995                       unsigned_type_node);
6996   f_ovf = build_decl (BUILTINS_LOCATION,
6997                       FIELD_DECL, get_identifier ("overflow_arg_area"),
6998                       ptr_type_node);
6999   f_sav = build_decl (BUILTINS_LOCATION,
7000                       FIELD_DECL, get_identifier ("reg_save_area"),
7001                       ptr_type_node);
7002
7003   va_list_gpr_counter_field = f_gpr;
7004   va_list_fpr_counter_field = f_fpr;
7005
7006   DECL_FIELD_CONTEXT (f_gpr) = record;
7007   DECL_FIELD_CONTEXT (f_fpr) = record;
7008   DECL_FIELD_CONTEXT (f_ovf) = record;
7009   DECL_FIELD_CONTEXT (f_sav) = record;
7010
7011   TREE_CHAIN (record) = type_decl;
7012   TYPE_NAME (record) = type_decl;
7013   TYPE_FIELDS (record) = f_gpr;
7014   DECL_CHAIN (f_gpr) = f_fpr;
7015   DECL_CHAIN (f_fpr) = f_ovf;
7016   DECL_CHAIN (f_ovf) = f_sav;
7017
7018   layout_type (record);
7019
7020   /* The correct type is an array type of one element.  */
7021   return build_array_type (record, build_index_type (size_zero_node));
7022 }
7023
7024 /* Setup the builtin va_list data type and for 64-bit the additional
7025    calling convention specific va_list data types.  */
7026
7027 static tree
7028 ix86_build_builtin_va_list (void)
7029 {
7030   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7031
7032   /* Initialize abi specific va_list builtin types.  */
7033   if (TARGET_64BIT)
7034     {
7035       tree t;
7036       if (ix86_abi == MS_ABI)
7037         {
7038           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7039           if (TREE_CODE (t) != RECORD_TYPE)
7040             t = build_variant_type_copy (t);
7041           sysv_va_list_type_node = t;
7042         }
7043       else
7044         {
7045           t = ret;
7046           if (TREE_CODE (t) != RECORD_TYPE)
7047             t = build_variant_type_copy (t);
7048           sysv_va_list_type_node = t;
7049         }
7050       if (ix86_abi != MS_ABI)
7051         {
7052           t = ix86_build_builtin_va_list_abi (MS_ABI);
7053           if (TREE_CODE (t) != RECORD_TYPE)
7054             t = build_variant_type_copy (t);
7055           ms_va_list_type_node = t;
7056         }
7057       else
7058         {
7059           t = ret;
7060           if (TREE_CODE (t) != RECORD_TYPE)
7061             t = build_variant_type_copy (t);
7062           ms_va_list_type_node = t;
7063         }
7064     }
7065
7066   return ret;
7067 }
7068
7069 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7070
7071 static void
7072 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7073 {
7074   rtx save_area, mem;
7075   alias_set_type set;
7076   int i, max;
7077
7078   /* GPR size of varargs save area.  */
7079   if (cfun->va_list_gpr_size)
7080     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7081   else
7082     ix86_varargs_gpr_size = 0;
7083
7084   /* FPR size of varargs save area.  We don't need it if we don't pass
7085      anything in SSE registers.  */
7086   if (TARGET_SSE && cfun->va_list_fpr_size)
7087     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7088   else
7089     ix86_varargs_fpr_size = 0;
7090
7091   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7092     return;
7093
7094   save_area = frame_pointer_rtx;
7095   set = get_varargs_alias_set ();
7096
7097   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7098   if (max > X86_64_REGPARM_MAX)
7099     max = X86_64_REGPARM_MAX;
7100
7101   for (i = cum->regno; i < max; i++)
7102     {
7103       mem = gen_rtx_MEM (Pmode,
7104                          plus_constant (save_area, i * UNITS_PER_WORD));
7105       MEM_NOTRAP_P (mem) = 1;
7106       set_mem_alias_set (mem, set);
7107       emit_move_insn (mem, gen_rtx_REG (Pmode,
7108                                         x86_64_int_parameter_registers[i]));
7109     }
7110
7111   if (ix86_varargs_fpr_size)
7112     {
7113       enum machine_mode smode;
7114       rtx label, test;
7115
7116       /* Now emit code to save SSE registers.  The AX parameter contains number
7117          of SSE parameter registers used to call this function, though all we
7118          actually check here is the zero/non-zero status.  */
7119
7120       label = gen_label_rtx ();
7121       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7122       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7123                                       label));
7124
7125       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7126          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7127          be if we could determine the real mode of the data, via a hook
7128          into pass_stdarg.  Ignore all that for now.  */
7129       smode = V4SFmode;
7130       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7131         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7132
7133       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7134       if (max > X86_64_SSE_REGPARM_MAX)
7135         max = X86_64_SSE_REGPARM_MAX;
7136
7137       for (i = cum->sse_regno; i < max; ++i)
7138         {
7139           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7140           mem = gen_rtx_MEM (smode, mem);
7141           MEM_NOTRAP_P (mem) = 1;
7142           set_mem_alias_set (mem, set);
7143           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7144
7145           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7146         }
7147
7148       emit_label (label);
7149     }
7150 }
7151
7152 static void
7153 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7154 {
7155   alias_set_type set = get_varargs_alias_set ();
7156   int i;
7157
7158   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7159     {
7160       rtx reg, mem;
7161
7162       mem = gen_rtx_MEM (Pmode,
7163                          plus_constant (virtual_incoming_args_rtx,
7164                                         i * UNITS_PER_WORD));
7165       MEM_NOTRAP_P (mem) = 1;
7166       set_mem_alias_set (mem, set);
7167
7168       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7169       emit_move_insn (mem, reg);
7170     }
7171 }
7172
7173 static void
7174 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7175                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7176                              int no_rtl)
7177 {
7178   CUMULATIVE_ARGS next_cum;
7179   tree fntype;
7180
7181   /* This argument doesn't appear to be used anymore.  Which is good,
7182      because the old code here didn't suppress rtl generation.  */
7183   gcc_assert (!no_rtl);
7184
7185   if (!TARGET_64BIT)
7186     return;
7187
7188   fntype = TREE_TYPE (current_function_decl);
7189
7190   /* For varargs, we do not want to skip the dummy va_dcl argument.
7191      For stdargs, we do want to skip the last named argument.  */
7192   next_cum = *cum;
7193   if (stdarg_p (fntype))
7194     ix86_function_arg_advance (&next_cum, mode, type, true);
7195
7196   if (cum->call_abi == MS_ABI)
7197     setup_incoming_varargs_ms_64 (&next_cum);
7198   else
7199     setup_incoming_varargs_64 (&next_cum);
7200 }
7201
7202 /* Checks if TYPE is of kind va_list char *.  */
7203
7204 static bool
7205 is_va_list_char_pointer (tree type)
7206 {
7207   tree canonic;
7208
7209   /* For 32-bit it is always true.  */
7210   if (!TARGET_64BIT)
7211     return true;
7212   canonic = ix86_canonical_va_list_type (type);
7213   return (canonic == ms_va_list_type_node
7214           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7215 }
7216
7217 /* Implement va_start.  */
7218
7219 static void
7220 ix86_va_start (tree valist, rtx nextarg)
7221 {
7222   HOST_WIDE_INT words, n_gpr, n_fpr;
7223   tree f_gpr, f_fpr, f_ovf, f_sav;
7224   tree gpr, fpr, ovf, sav, t;
7225   tree type;
7226
7227   /* Only 64bit target needs something special.  */
7228   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7229     {
7230       std_expand_builtin_va_start (valist, nextarg);
7231       return;
7232     }
7233
7234   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7235   f_fpr = DECL_CHAIN (f_gpr);
7236   f_ovf = DECL_CHAIN (f_fpr);
7237   f_sav = DECL_CHAIN (f_ovf);
7238
7239   valist = build_simple_mem_ref (valist);
7240   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7241   /* The following should be folded into the MEM_REF offset.  */
7242   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7243                 f_gpr, NULL_TREE);
7244   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7245                 f_fpr, NULL_TREE);
7246   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7247                 f_ovf, NULL_TREE);
7248   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7249                 f_sav, NULL_TREE);
7250
7251   /* Count number of gp and fp argument registers used.  */
7252   words = crtl->args.info.words;
7253   n_gpr = crtl->args.info.regno;
7254   n_fpr = crtl->args.info.sse_regno;
7255
7256   if (cfun->va_list_gpr_size)
7257     {
7258       type = TREE_TYPE (gpr);
7259       t = build2 (MODIFY_EXPR, type,
7260                   gpr, build_int_cst (type, n_gpr * 8));
7261       TREE_SIDE_EFFECTS (t) = 1;
7262       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7263     }
7264
7265   if (TARGET_SSE && cfun->va_list_fpr_size)
7266     {
7267       type = TREE_TYPE (fpr);
7268       t = build2 (MODIFY_EXPR, type, fpr,
7269                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7270       TREE_SIDE_EFFECTS (t) = 1;
7271       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7272     }
7273
7274   /* Find the overflow area.  */
7275   type = TREE_TYPE (ovf);
7276   t = make_tree (type, crtl->args.internal_arg_pointer);
7277   if (words != 0)
7278     t = build2 (POINTER_PLUS_EXPR, type, t,
7279                 size_int (words * UNITS_PER_WORD));
7280   t = build2 (MODIFY_EXPR, type, ovf, t);
7281   TREE_SIDE_EFFECTS (t) = 1;
7282   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7283
7284   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
7285     {
7286       /* Find the register save area.
7287          Prologue of the function save it right above stack frame.  */
7288       type = TREE_TYPE (sav);
7289       t = make_tree (type, frame_pointer_rtx);
7290       if (!ix86_varargs_gpr_size)
7291         t = build2 (POINTER_PLUS_EXPR, type, t,
7292                     size_int (-8 * X86_64_REGPARM_MAX));
7293       t = build2 (MODIFY_EXPR, type, sav, t);
7294       TREE_SIDE_EFFECTS (t) = 1;
7295       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7296     }
7297 }
7298
7299 /* Implement va_arg.  */
7300
7301 static tree
7302 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7303                       gimple_seq *post_p)
7304 {
7305   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
7306   tree f_gpr, f_fpr, f_ovf, f_sav;
7307   tree gpr, fpr, ovf, sav, t;
7308   int size, rsize;
7309   tree lab_false, lab_over = NULL_TREE;
7310   tree addr, t2;
7311   rtx container;
7312   int indirect_p = 0;
7313   tree ptrtype;
7314   enum machine_mode nat_mode;
7315   unsigned int arg_boundary;
7316
7317   /* Only 64bit target needs something special.  */
7318   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7319     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7320
7321   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7322   f_fpr = DECL_CHAIN (f_gpr);
7323   f_ovf = DECL_CHAIN (f_fpr);
7324   f_sav = DECL_CHAIN (f_ovf);
7325
7326   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7327                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7328   valist = build_va_arg_indirect_ref (valist);
7329   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7330   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7331   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7332
7333   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7334   if (indirect_p)
7335     type = build_pointer_type (type);
7336   size = int_size_in_bytes (type);
7337   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7338
7339   nat_mode = type_natural_mode (type, NULL);
7340   switch (nat_mode)
7341     {
7342     case V8SFmode:
7343     case V8SImode:
7344     case V32QImode:
7345     case V16HImode:
7346     case V4DFmode:
7347     case V4DImode:
7348       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7349       if (ix86_cfun_abi () == SYSV_ABI)
7350         {
7351           container = NULL;
7352           break;
7353         }
7354
7355     default:
7356       container = construct_container (nat_mode, TYPE_MODE (type),
7357                                        type, 0, X86_64_REGPARM_MAX,
7358                                        X86_64_SSE_REGPARM_MAX, intreg,
7359                                        0);
7360       break;
7361     }
7362
7363   /* Pull the value out of the saved registers.  */
7364
7365   addr = create_tmp_var (ptr_type_node, "addr");
7366
7367   if (container)
7368     {
7369       int needed_intregs, needed_sseregs;
7370       bool need_temp;
7371       tree int_addr, sse_addr;
7372
7373       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7374       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7375
7376       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7377
7378       need_temp = (!REG_P (container)
7379                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7380                        || TYPE_ALIGN (type) > 128));
7381
7382       /* In case we are passing structure, verify that it is consecutive block
7383          on the register save area.  If not we need to do moves.  */
7384       if (!need_temp && !REG_P (container))
7385         {
7386           /* Verify that all registers are strictly consecutive  */
7387           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7388             {
7389               int i;
7390
7391               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7392                 {
7393                   rtx slot = XVECEXP (container, 0, i);
7394                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7395                       || INTVAL (XEXP (slot, 1)) != i * 16)
7396                     need_temp = 1;
7397                 }
7398             }
7399           else
7400             {
7401               int i;
7402
7403               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7404                 {
7405                   rtx slot = XVECEXP (container, 0, i);
7406                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
7407                       || INTVAL (XEXP (slot, 1)) != i * 8)
7408                     need_temp = 1;
7409                 }
7410             }
7411         }
7412       if (!need_temp)
7413         {
7414           int_addr = addr;
7415           sse_addr = addr;
7416         }
7417       else
7418         {
7419           int_addr = create_tmp_var (ptr_type_node, "int_addr");
7420           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
7421         }
7422
7423       /* First ensure that we fit completely in registers.  */
7424       if (needed_intregs)
7425         {
7426           t = build_int_cst (TREE_TYPE (gpr),
7427                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
7428           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
7429           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7430           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7431           gimplify_and_add (t, pre_p);
7432         }
7433       if (needed_sseregs)
7434         {
7435           t = build_int_cst (TREE_TYPE (fpr),
7436                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
7437                              + X86_64_REGPARM_MAX * 8);
7438           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
7439           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7440           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7441           gimplify_and_add (t, pre_p);
7442         }
7443
7444       /* Compute index to start of area used for integer regs.  */
7445       if (needed_intregs)
7446         {
7447           /* int_addr = gpr + sav; */
7448           t = fold_convert (sizetype, gpr);
7449           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7450           gimplify_assign (int_addr, t, pre_p);
7451         }
7452       if (needed_sseregs)
7453         {
7454           /* sse_addr = fpr + sav; */
7455           t = fold_convert (sizetype, fpr);
7456           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7457           gimplify_assign (sse_addr, t, pre_p);
7458         }
7459       if (need_temp)
7460         {
7461           int i, prev_size = 0;
7462           tree temp = create_tmp_var (type, "va_arg_tmp");
7463
7464           /* addr = &temp; */
7465           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
7466           gimplify_assign (addr, t, pre_p);
7467
7468           for (i = 0; i < XVECLEN (container, 0); i++)
7469             {
7470               rtx slot = XVECEXP (container, 0, i);
7471               rtx reg = XEXP (slot, 0);
7472               enum machine_mode mode = GET_MODE (reg);
7473               tree piece_type;
7474               tree addr_type;
7475               tree daddr_type;
7476               tree src_addr, src;
7477               int src_offset;
7478               tree dest_addr, dest;
7479               int cur_size = GET_MODE_SIZE (mode);
7480
7481               if (prev_size + cur_size > size)
7482                 {
7483                   cur_size = size - prev_size;
7484                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
7485                   if (mode == BLKmode)
7486                     mode = QImode;
7487                 }
7488               piece_type = lang_hooks.types.type_for_mode (mode, 1);
7489               if (mode == GET_MODE (reg))
7490                 addr_type = build_pointer_type (piece_type);
7491               else
7492                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
7493                                                          true);
7494               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
7495                                                         true);
7496
7497               if (SSE_REGNO_P (REGNO (reg)))
7498                 {
7499                   src_addr = sse_addr;
7500                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
7501                 }
7502               else
7503                 {
7504                   src_addr = int_addr;
7505                   src_offset = REGNO (reg) * 8;
7506                 }
7507               src_addr = fold_convert (addr_type, src_addr);
7508               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
7509                                       size_int (src_offset));
7510
7511               dest_addr = fold_convert (daddr_type, addr);
7512               dest_addr = fold_build2 (POINTER_PLUS_EXPR, daddr_type, dest_addr,
7513                                        size_int (INTVAL (XEXP (slot, 1))));
7514               if (cur_size == GET_MODE_SIZE (mode))
7515                 {
7516                   src = build_va_arg_indirect_ref (src_addr);
7517                   dest = build_va_arg_indirect_ref (dest_addr);
7518
7519                   gimplify_assign (dest, src, pre_p);
7520                 }
7521               else
7522                 {
7523                   tree copy
7524                     = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
7525                                        3, dest_addr, src_addr,
7526                                        size_int (cur_size));
7527                   gimplify_and_add (copy, pre_p);
7528                 }
7529               prev_size += cur_size;
7530             }
7531         }
7532
7533       if (needed_intregs)
7534         {
7535           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
7536                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
7537           gimplify_assign (gpr, t, pre_p);
7538         }
7539
7540       if (needed_sseregs)
7541         {
7542           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
7543                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
7544           gimplify_assign (fpr, t, pre_p);
7545         }
7546
7547       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
7548
7549       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
7550     }
7551
7552   /* ... otherwise out of the overflow area.  */
7553
7554   /* When we align parameter on stack for caller, if the parameter
7555      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
7556      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
7557      here with caller.  */
7558   arg_boundary = FUNCTION_ARG_BOUNDARY (VOIDmode, type);
7559   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
7560     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
7561
7562   /* Care for on-stack alignment if needed.  */
7563   if (arg_boundary <= 64 || size == 0)
7564     t = ovf;
7565  else
7566     {
7567       HOST_WIDE_INT align = arg_boundary / 8;
7568       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
7569                   size_int (align - 1));
7570       t = fold_convert (sizetype, t);
7571       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
7572                   size_int (-align));
7573       t = fold_convert (TREE_TYPE (ovf), t);
7574     }
7575
7576   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
7577   gimplify_assign (addr, t, pre_p);
7578
7579   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
7580               size_int (rsize * UNITS_PER_WORD));
7581   gimplify_assign (unshare_expr (ovf), t, pre_p);
7582
7583   if (container)
7584     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
7585
7586   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
7587   addr = fold_convert (ptrtype, addr);
7588
7589   if (indirect_p)
7590     addr = build_va_arg_indirect_ref (addr);
7591   return build_va_arg_indirect_ref (addr);
7592 }
7593 \f
7594 /* Return nonzero if OPNUM's MEM should be matched
7595    in movabs* patterns.  */
7596
7597 int
7598 ix86_check_movabs (rtx insn, int opnum)
7599 {
7600   rtx set, mem;
7601
7602   set = PATTERN (insn);
7603   if (GET_CODE (set) == PARALLEL)
7604     set = XVECEXP (set, 0, 0);
7605   gcc_assert (GET_CODE (set) == SET);
7606   mem = XEXP (set, opnum);
7607   while (GET_CODE (mem) == SUBREG)
7608     mem = SUBREG_REG (mem);
7609   gcc_assert (MEM_P (mem));
7610   return (volatile_ok || !MEM_VOLATILE_P (mem));
7611 }
7612 \f
7613 /* Initialize the table of extra 80387 mathematical constants.  */
7614
7615 static void
7616 init_ext_80387_constants (void)
7617 {
7618   static const char * cst[5] =
7619   {
7620     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
7621     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
7622     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
7623     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
7624     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
7625   };
7626   int i;
7627
7628   for (i = 0; i < 5; i++)
7629     {
7630       real_from_string (&ext_80387_constants_table[i], cst[i]);
7631       /* Ensure each constant is rounded to XFmode precision.  */
7632       real_convert (&ext_80387_constants_table[i],
7633                     XFmode, &ext_80387_constants_table[i]);
7634     }
7635
7636   ext_80387_constants_init = 1;
7637 }
7638
7639 /* Return true if the constant is something that can be loaded with
7640    a special instruction.  */
7641
7642 int
7643 standard_80387_constant_p (rtx x)
7644 {
7645   enum machine_mode mode = GET_MODE (x);
7646
7647   REAL_VALUE_TYPE r;
7648
7649   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
7650     return -1;
7651
7652   if (x == CONST0_RTX (mode))
7653     return 1;
7654   if (x == CONST1_RTX (mode))
7655     return 2;
7656
7657   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7658
7659   /* For XFmode constants, try to find a special 80387 instruction when
7660      optimizing for size or on those CPUs that benefit from them.  */
7661   if (mode == XFmode
7662       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
7663     {
7664       int i;
7665
7666       if (! ext_80387_constants_init)
7667         init_ext_80387_constants ();
7668
7669       for (i = 0; i < 5; i++)
7670         if (real_identical (&r, &ext_80387_constants_table[i]))
7671           return i + 3;
7672     }
7673
7674   /* Load of the constant -0.0 or -1.0 will be split as
7675      fldz;fchs or fld1;fchs sequence.  */
7676   if (real_isnegzero (&r))
7677     return 8;
7678   if (real_identical (&r, &dconstm1))
7679     return 9;
7680
7681   return 0;
7682 }
7683
7684 /* Return the opcode of the special instruction to be used to load
7685    the constant X.  */
7686
7687 const char *
7688 standard_80387_constant_opcode (rtx x)
7689 {
7690   switch (standard_80387_constant_p (x))
7691     {
7692     case 1:
7693       return "fldz";
7694     case 2:
7695       return "fld1";
7696     case 3:
7697       return "fldlg2";
7698     case 4:
7699       return "fldln2";
7700     case 5:
7701       return "fldl2e";
7702     case 6:
7703       return "fldl2t";
7704     case 7:
7705       return "fldpi";
7706     case 8:
7707     case 9:
7708       return "#";
7709     default:
7710       gcc_unreachable ();
7711     }
7712 }
7713
7714 /* Return the CONST_DOUBLE representing the 80387 constant that is
7715    loaded by the specified special instruction.  The argument IDX
7716    matches the return value from standard_80387_constant_p.  */
7717
7718 rtx
7719 standard_80387_constant_rtx (int idx)
7720 {
7721   int i;
7722
7723   if (! ext_80387_constants_init)
7724     init_ext_80387_constants ();
7725
7726   switch (idx)
7727     {
7728     case 3:
7729     case 4:
7730     case 5:
7731     case 6:
7732     case 7:
7733       i = idx - 3;
7734       break;
7735
7736     default:
7737       gcc_unreachable ();
7738     }
7739
7740   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
7741                                        XFmode);
7742 }
7743
7744 /* Return 1 if X is all 0s and 2 if x is all 1s
7745    in supported SSE vector mode.  */
7746
7747 int
7748 standard_sse_constant_p (rtx x)
7749 {
7750   enum machine_mode mode = GET_MODE (x);
7751
7752   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
7753     return 1;
7754   if (vector_all_ones_operand (x, mode))
7755     switch (mode)
7756       {
7757       case V16QImode:
7758       case V8HImode:
7759       case V4SImode:
7760       case V2DImode:
7761         if (TARGET_SSE2)
7762           return 2;
7763       default:
7764         break;
7765       }
7766
7767   return 0;
7768 }
7769
7770 /* Return the opcode of the special instruction to be used to load
7771    the constant X.  */
7772
7773 const char *
7774 standard_sse_constant_opcode (rtx insn, rtx x)
7775 {
7776   switch (standard_sse_constant_p (x))
7777     {
7778     case 1:
7779       switch (get_attr_mode (insn))
7780         {
7781         case MODE_V4SF:
7782           return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7783         case MODE_V2DF:
7784           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7785             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7786           else
7787             return TARGET_AVX ? "vxorpd\t%0, %0, %0" : "xorpd\t%0, %0";
7788         case MODE_TI:
7789           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7790             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7791           else
7792             return TARGET_AVX ? "vpxor\t%0, %0, %0" : "pxor\t%0, %0";
7793         case MODE_V8SF:
7794           return "vxorps\t%x0, %x0, %x0";
7795         case MODE_V4DF:
7796           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7797             return "vxorps\t%x0, %x0, %x0";
7798           else
7799             return "vxorpd\t%x0, %x0, %x0";
7800         case MODE_OI:
7801           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7802             return "vxorps\t%x0, %x0, %x0";
7803           else
7804             return "vpxor\t%x0, %x0, %x0";
7805         default:
7806           break;
7807         }
7808     case 2:
7809       return TARGET_AVX ? "vpcmpeqd\t%0, %0, %0" : "pcmpeqd\t%0, %0";
7810     default:
7811       break;
7812     }
7813   gcc_unreachable ();
7814 }
7815
7816 /* Returns 1 if OP contains a symbol reference */
7817
7818 int
7819 symbolic_reference_mentioned_p (rtx op)
7820 {
7821   const char *fmt;
7822   int i;
7823
7824   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
7825     return 1;
7826
7827   fmt = GET_RTX_FORMAT (GET_CODE (op));
7828   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
7829     {
7830       if (fmt[i] == 'E')
7831         {
7832           int j;
7833
7834           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
7835             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
7836               return 1;
7837         }
7838
7839       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
7840         return 1;
7841     }
7842
7843   return 0;
7844 }
7845
7846 /* Return 1 if it is appropriate to emit `ret' instructions in the
7847    body of a function.  Do this only if the epilogue is simple, needing a
7848    couple of insns.  Prior to reloading, we can't tell how many registers
7849    must be saved, so return 0 then.  Return 0 if there is no frame
7850    marker to de-allocate.  */
7851
7852 int
7853 ix86_can_use_return_insn_p (void)
7854 {
7855   struct ix86_frame frame;
7856
7857   if (! reload_completed || frame_pointer_needed)
7858     return 0;
7859
7860   /* Don't allow more than 32 pop, since that's all we can do
7861      with one instruction.  */
7862   if (crtl->args.pops_args
7863       && crtl->args.size >= 32768)
7864     return 0;
7865
7866   ix86_compute_frame_layout (&frame);
7867   return frame.to_allocate == 0 && frame.padding0 == 0
7868          && (frame.nregs + frame.nsseregs) == 0;
7869 }
7870 \f
7871 /* Value should be nonzero if functions must have frame pointers.
7872    Zero means the frame pointer need not be set up (and parms may
7873    be accessed via the stack pointer) in functions that seem suitable.  */
7874
7875 static bool
7876 ix86_frame_pointer_required (void)
7877 {
7878   /* If we accessed previous frames, then the generated code expects
7879      to be able to access the saved ebp value in our frame.  */
7880   if (cfun->machine->accesses_prev_frame)
7881     return true;
7882
7883   /* Several x86 os'es need a frame pointer for other reasons,
7884      usually pertaining to setjmp.  */
7885   if (SUBTARGET_FRAME_POINTER_REQUIRED)
7886     return true;
7887
7888   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
7889      the frame pointer by default.  Turn it back on now if we've not
7890      got a leaf function.  */
7891   if (TARGET_OMIT_LEAF_FRAME_POINTER
7892       && (!current_function_is_leaf
7893           || ix86_current_function_calls_tls_descriptor))
7894     return true;
7895
7896   if (crtl->profile && !flag_fentry)
7897     return true;
7898
7899   return false;
7900 }
7901
7902 /* Record that the current function accesses previous call frames.  */
7903
7904 void
7905 ix86_setup_frame_addresses (void)
7906 {
7907   cfun->machine->accesses_prev_frame = 1;
7908 }
7909 \f
7910 #ifndef USE_HIDDEN_LINKONCE
7911 # if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
7912 #  define USE_HIDDEN_LINKONCE 1
7913 # else
7914 #  define USE_HIDDEN_LINKONCE 0
7915 # endif
7916 #endif
7917
7918 static int pic_labels_used;
7919
7920 /* Fills in the label name that should be used for a pc thunk for
7921    the given register.  */
7922
7923 static void
7924 get_pc_thunk_name (char name[32], unsigned int regno)
7925 {
7926   gcc_assert (!TARGET_64BIT);
7927
7928   if (USE_HIDDEN_LINKONCE)
7929     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
7930   else
7931     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
7932 }
7933
7934
7935 /* This function generates code for -fpic that loads %ebx with
7936    the return address of the caller and then returns.  */
7937
7938 static void
7939 ix86_code_end (void)
7940 {
7941   rtx xops[2];
7942   int regno;
7943
7944   for (regno = 0; regno < 8; ++regno)
7945     {
7946       char name[32];
7947       tree decl;
7948
7949       if (! ((pic_labels_used >> regno) & 1))
7950         continue;
7951
7952       get_pc_thunk_name (name, regno);
7953
7954       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
7955                          get_identifier (name),
7956                          build_function_type (void_type_node, void_list_node));
7957       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
7958                                        NULL_TREE, void_type_node);
7959       TREE_PUBLIC (decl) = 1;
7960       TREE_STATIC (decl) = 1;
7961
7962 #if TARGET_MACHO
7963       if (TARGET_MACHO)
7964         {
7965           switch_to_section (darwin_sections[text_coal_section]);
7966           fputs ("\t.weak_definition\t", asm_out_file);
7967           assemble_name (asm_out_file, name);
7968           fputs ("\n\t.private_extern\t", asm_out_file);
7969           assemble_name (asm_out_file, name);
7970           putc ('\n', asm_out_file);
7971           ASM_OUTPUT_LABEL (asm_out_file, name);
7972           DECL_WEAK (decl) = 1;
7973         }
7974       else
7975 #endif
7976       if (USE_HIDDEN_LINKONCE)
7977         {
7978           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
7979
7980           targetm.asm_out.unique_section (decl, 0);
7981           switch_to_section (get_named_section (decl, NULL, 0));
7982
7983           targetm.asm_out.globalize_label (asm_out_file, name);
7984           fputs ("\t.hidden\t", asm_out_file);
7985           assemble_name (asm_out_file, name);
7986           putc ('\n', asm_out_file);
7987           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
7988         }
7989       else
7990         {
7991           switch_to_section (text_section);
7992           ASM_OUTPUT_LABEL (asm_out_file, name);
7993         }
7994
7995       DECL_INITIAL (decl) = make_node (BLOCK);
7996       current_function_decl = decl;
7997       init_function_start (decl);
7998       first_function_block_is_cold = false;
7999       /* Make sure unwind info is emitted for the thunk if needed.  */
8000       final_start_function (emit_barrier (), asm_out_file, 1);
8001
8002       xops[0] = gen_rtx_REG (Pmode, regno);
8003       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8004       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8005       output_asm_insn ("ret", xops);
8006       final_end_function ();
8007       init_insn_lengths ();
8008       free_after_compilation (cfun);
8009       set_cfun (NULL);
8010       current_function_decl = NULL;
8011     }
8012 }
8013
8014 /* Emit code for the SET_GOT patterns.  */
8015
8016 const char *
8017 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8018 {
8019   rtx xops[3];
8020
8021   xops[0] = dest;
8022
8023   if (TARGET_VXWORKS_RTP && flag_pic)
8024     {
8025       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8026       xops[2] = gen_rtx_MEM (Pmode,
8027                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8028       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8029
8030       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8031          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8032          an unadorned address.  */
8033       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8034       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8035       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8036       return "";
8037     }
8038
8039   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8040
8041   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
8042     {
8043       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8044
8045       if (!flag_pic)
8046         output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8047       else
8048         {
8049           output_asm_insn ("call\t%a2", xops);
8050 #ifdef DWARF2_UNWIND_INFO
8051           /* The call to next label acts as a push.  */
8052           if (dwarf2out_do_frame ())
8053             {
8054               rtx insn;
8055               start_sequence ();
8056               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8057                                              gen_rtx_PLUS (Pmode,
8058                                                            stack_pointer_rtx,
8059                                                            GEN_INT (-4))));
8060               RTX_FRAME_RELATED_P (insn) = 1;
8061               dwarf2out_frame_debug (insn, true);
8062               end_sequence ();
8063             }
8064 #endif
8065         }
8066
8067 #if TARGET_MACHO
8068       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8069          is what will be referenced by the Mach-O PIC subsystem.  */
8070       if (!label)
8071         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8072 #endif
8073
8074       targetm.asm_out.internal_label (asm_out_file, "L",
8075                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8076
8077       if (flag_pic)
8078         {
8079           output_asm_insn ("pop%z0\t%0", xops);
8080 #ifdef DWARF2_UNWIND_INFO
8081           /* The pop is a pop and clobbers dest, but doesn't restore it
8082              for unwind info purposes.  */
8083           if (dwarf2out_do_frame ())
8084             {
8085               rtx insn;
8086               start_sequence ();
8087               insn = emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
8088               dwarf2out_frame_debug (insn, true);
8089               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8090                                              gen_rtx_PLUS (Pmode,
8091                                                            stack_pointer_rtx,
8092                                                            GEN_INT (4))));
8093               RTX_FRAME_RELATED_P (insn) = 1;
8094               dwarf2out_frame_debug (insn, true);
8095               end_sequence ();
8096             }
8097 #endif
8098         }
8099     }
8100   else
8101     {
8102       char name[32];
8103       get_pc_thunk_name (name, REGNO (dest));
8104       pic_labels_used |= 1 << REGNO (dest);
8105
8106 #ifdef DWARF2_UNWIND_INFO
8107       /* Ensure all queued register saves are flushed before the
8108          call.  */
8109       if (dwarf2out_do_frame ())
8110         {
8111           rtx insn;
8112           start_sequence ();
8113           insn = emit_barrier ();
8114           end_sequence ();
8115           dwarf2out_frame_debug (insn, false);
8116         }
8117 #endif
8118       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8119       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8120       output_asm_insn ("call\t%X2", xops);
8121       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8122          is what will be referenced by the Mach-O PIC subsystem.  */
8123 #if TARGET_MACHO
8124       if (!label)
8125         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8126       else
8127         targetm.asm_out.internal_label (asm_out_file, "L",
8128                                            CODE_LABEL_NUMBER (label));
8129 #endif
8130     }
8131
8132   if (TARGET_MACHO)
8133     return "";
8134
8135   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
8136     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8137   else
8138     output_asm_insn ("add%z0\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
8139
8140   return "";
8141 }
8142
8143 /* Generate an "push" pattern for input ARG.  */
8144
8145 static rtx
8146 gen_push (rtx arg)
8147 {
8148   if (ix86_cfa_state->reg == stack_pointer_rtx)
8149     ix86_cfa_state->offset += UNITS_PER_WORD;
8150
8151   return gen_rtx_SET (VOIDmode,
8152                       gen_rtx_MEM (Pmode,
8153                                    gen_rtx_PRE_DEC (Pmode,
8154                                                     stack_pointer_rtx)),
8155                       arg);
8156 }
8157
8158 /* Return >= 0 if there is an unused call-clobbered register available
8159    for the entire function.  */
8160
8161 static unsigned int
8162 ix86_select_alt_pic_regnum (void)
8163 {
8164   if (current_function_is_leaf
8165       && !crtl->profile
8166       && !ix86_current_function_calls_tls_descriptor)
8167     {
8168       int i, drap;
8169       /* Can't use the same register for both PIC and DRAP.  */
8170       if (crtl->drap_reg)
8171         drap = REGNO (crtl->drap_reg);
8172       else
8173         drap = -1;
8174       for (i = 2; i >= 0; --i)
8175         if (i != drap && !df_regs_ever_live_p (i))
8176           return i;
8177     }
8178
8179   return INVALID_REGNUM;
8180 }
8181
8182 /* Return 1 if we need to save REGNO.  */
8183 static int
8184 ix86_save_reg (unsigned int regno, int maybe_eh_return)
8185 {
8186   if (pic_offset_table_rtx
8187       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8188       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8189           || crtl->profile
8190           || crtl->calls_eh_return
8191           || crtl->uses_const_pool))
8192     {
8193       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
8194         return 0;
8195       return 1;
8196     }
8197
8198   if (crtl->calls_eh_return && maybe_eh_return)
8199     {
8200       unsigned i;
8201       for (i = 0; ; i++)
8202         {
8203           unsigned test = EH_RETURN_DATA_REGNO (i);
8204           if (test == INVALID_REGNUM)
8205             break;
8206           if (test == regno)
8207             return 1;
8208         }
8209     }
8210
8211   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8212     return 1;
8213
8214   return (df_regs_ever_live_p (regno)
8215           && !call_used_regs[regno]
8216           && !fixed_regs[regno]
8217           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8218 }
8219
8220 /* Return number of saved general prupose registers.  */
8221
8222 static int
8223 ix86_nsaved_regs (void)
8224 {
8225   int nregs = 0;
8226   int regno;
8227
8228   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8229     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8230       nregs ++;
8231   return nregs;
8232 }
8233
8234 /* Return number of saved SSE registrers.  */
8235
8236 static int
8237 ix86_nsaved_sseregs (void)
8238 {
8239   int nregs = 0;
8240   int regno;
8241
8242   if (ix86_cfun_abi () != MS_ABI)
8243     return 0;
8244   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8245     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8246       nregs ++;
8247   return nregs;
8248 }
8249
8250 /* Given FROM and TO register numbers, say whether this elimination is
8251    allowed.  If stack alignment is needed, we can only replace argument
8252    pointer with hard frame pointer, or replace frame pointer with stack
8253    pointer.  Otherwise, frame pointer elimination is automatically
8254    handled and all other eliminations are valid.  */
8255
8256 static bool
8257 ix86_can_eliminate (const int from, const int to)
8258 {
8259   if (stack_realign_fp)
8260     return ((from == ARG_POINTER_REGNUM
8261              && to == HARD_FRAME_POINTER_REGNUM)
8262             || (from == FRAME_POINTER_REGNUM
8263                 && to == STACK_POINTER_REGNUM));
8264   else
8265     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
8266 }
8267
8268 /* Return the offset between two registers, one to be eliminated, and the other
8269    its replacement, at the start of a routine.  */
8270
8271 HOST_WIDE_INT
8272 ix86_initial_elimination_offset (int from, int to)
8273 {
8274   struct ix86_frame frame;
8275   ix86_compute_frame_layout (&frame);
8276
8277   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
8278     return frame.hard_frame_pointer_offset;
8279   else if (from == FRAME_POINTER_REGNUM
8280            && to == HARD_FRAME_POINTER_REGNUM)
8281     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
8282   else
8283     {
8284       gcc_assert (to == STACK_POINTER_REGNUM);
8285
8286       if (from == ARG_POINTER_REGNUM)
8287         return frame.stack_pointer_offset;
8288
8289       gcc_assert (from == FRAME_POINTER_REGNUM);
8290       return frame.stack_pointer_offset - frame.frame_pointer_offset;
8291     }
8292 }
8293
8294 /* In a dynamically-aligned function, we can't know the offset from
8295    stack pointer to frame pointer, so we must ensure that setjmp
8296    eliminates fp against the hard fp (%ebp) rather than trying to
8297    index from %esp up to the top of the frame across a gap that is
8298    of unknown (at compile-time) size.  */
8299 static rtx
8300 ix86_builtin_setjmp_frame_value (void)
8301 {
8302   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
8303 }
8304
8305 /* Fill structure ix86_frame about frame of currently computed function.  */
8306
8307 static void
8308 ix86_compute_frame_layout (struct ix86_frame *frame)
8309 {
8310   unsigned int stack_alignment_needed;
8311   HOST_WIDE_INT offset;
8312   unsigned int preferred_alignment;
8313   HOST_WIDE_INT size = get_frame_size ();
8314
8315   frame->nregs = ix86_nsaved_regs ();
8316   frame->nsseregs = ix86_nsaved_sseregs ();
8317
8318   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
8319   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
8320
8321   /* MS ABI seem to require stack alignment to be always 16 except for function
8322      prologues and leaf.  */
8323   if ((ix86_cfun_abi () == MS_ABI && preferred_alignment < 16)
8324       && (!current_function_is_leaf || cfun->calls_alloca != 0
8325           || ix86_current_function_calls_tls_descriptor))
8326     {
8327       preferred_alignment = 16;
8328       stack_alignment_needed = 16;
8329       crtl->preferred_stack_boundary = 128;
8330       crtl->stack_alignment_needed = 128;
8331     }
8332
8333   gcc_assert (!size || stack_alignment_needed);
8334   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
8335   gcc_assert (preferred_alignment <= stack_alignment_needed);
8336
8337   /* During reload iteration the amount of registers saved can change.
8338      Recompute the value as needed.  Do not recompute when amount of registers
8339      didn't change as reload does multiple calls to the function and does not
8340      expect the decision to change within single iteration.  */
8341   if (!optimize_function_for_size_p (cfun)
8342       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
8343     {
8344       int count = frame->nregs;
8345       struct cgraph_node *node = cgraph_node (current_function_decl);
8346
8347       cfun->machine->use_fast_prologue_epilogue_nregs = count;
8348       /* The fast prologue uses move instead of push to save registers.  This
8349          is significantly longer, but also executes faster as modern hardware
8350          can execute the moves in parallel, but can't do that for push/pop.
8351
8352          Be careful about choosing what prologue to emit:  When function takes
8353          many instructions to execute we may use slow version as well as in
8354          case function is known to be outside hot spot (this is known with
8355          feedback only).  Weight the size of function by number of registers
8356          to save as it is cheap to use one or two push instructions but very
8357          slow to use many of them.  */
8358       if (count)
8359         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
8360       if (node->frequency < NODE_FREQUENCY_NORMAL
8361           || (flag_branch_probabilities
8362               && node->frequency < NODE_FREQUENCY_HOT))
8363         cfun->machine->use_fast_prologue_epilogue = false;
8364       else
8365         cfun->machine->use_fast_prologue_epilogue
8366            = !expensive_function_p (count);
8367     }
8368   if (TARGET_PROLOGUE_USING_MOVE
8369       && cfun->machine->use_fast_prologue_epilogue)
8370     frame->save_regs_using_mov = true;
8371   else
8372     frame->save_regs_using_mov = false;
8373
8374   /* If static stack checking is enabled and done with probes, the registers
8375      need to be saved before allocating the frame.  */
8376   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
8377     frame->save_regs_using_mov = false;
8378
8379   /* Skip return address.  */
8380   offset = UNITS_PER_WORD;
8381
8382   /* Skip pushed static chain.  */
8383   if (ix86_static_chain_on_stack)
8384     offset += UNITS_PER_WORD;
8385
8386   /* Skip saved base pointer.  */
8387   if (frame_pointer_needed)
8388     offset += UNITS_PER_WORD;
8389
8390   frame->hard_frame_pointer_offset = offset;
8391
8392   /* Set offset to aligned because the realigned frame starts from
8393      here.  */
8394   if (stack_realign_fp)
8395     offset = (offset + stack_alignment_needed -1) & -stack_alignment_needed;
8396
8397   /* Register save area */
8398   offset += frame->nregs * UNITS_PER_WORD;
8399
8400   /* Align SSE reg save area.  */
8401   if (frame->nsseregs)
8402     frame->padding0 = ((offset + 16 - 1) & -16) - offset;
8403   else
8404     frame->padding0 = 0;
8405
8406   /* SSE register save area.  */
8407   offset += frame->padding0 + frame->nsseregs * 16;
8408
8409   /* Va-arg area */
8410   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
8411   offset += frame->va_arg_size;
8412
8413   /* Align start of frame for local function.  */
8414   frame->padding1 = ((offset + stack_alignment_needed - 1)
8415                      & -stack_alignment_needed) - offset;
8416
8417   offset += frame->padding1;
8418
8419   /* Frame pointer points here.  */
8420   frame->frame_pointer_offset = offset;
8421
8422   offset += size;
8423
8424   /* Add outgoing arguments area.  Can be skipped if we eliminated
8425      all the function calls as dead code.
8426      Skipping is however impossible when function calls alloca.  Alloca
8427      expander assumes that last crtl->outgoing_args_size
8428      of stack frame are unused.  */
8429   if (ACCUMULATE_OUTGOING_ARGS
8430       && (!current_function_is_leaf || cfun->calls_alloca
8431           || ix86_current_function_calls_tls_descriptor))
8432     {
8433       offset += crtl->outgoing_args_size;
8434       frame->outgoing_arguments_size = crtl->outgoing_args_size;
8435     }
8436   else
8437     frame->outgoing_arguments_size = 0;
8438
8439   /* Align stack boundary.  Only needed if we're calling another function
8440      or using alloca.  */
8441   if (!current_function_is_leaf || cfun->calls_alloca
8442       || ix86_current_function_calls_tls_descriptor)
8443     frame->padding2 = ((offset + preferred_alignment - 1)
8444                        & -preferred_alignment) - offset;
8445   else
8446     frame->padding2 = 0;
8447
8448   offset += frame->padding2;
8449
8450   /* We've reached end of stack frame.  */
8451   frame->stack_pointer_offset = offset;
8452
8453   /* Size prologue needs to allocate.  */
8454   frame->to_allocate =
8455     (size + frame->padding1 + frame->padding2
8456      + frame->outgoing_arguments_size + frame->va_arg_size);
8457
8458   if ((!frame->to_allocate && frame->nregs <= 1)
8459       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
8460     frame->save_regs_using_mov = false;
8461
8462   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE
8463       && current_function_sp_is_unchanging
8464       && current_function_is_leaf
8465       && !ix86_current_function_calls_tls_descriptor)
8466     {
8467       frame->red_zone_size = frame->to_allocate;
8468       if (frame->save_regs_using_mov)
8469         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
8470       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
8471         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
8472     }
8473   else
8474     frame->red_zone_size = 0;
8475   frame->to_allocate -= frame->red_zone_size;
8476   frame->stack_pointer_offset -= frame->red_zone_size;
8477 }
8478
8479 /* Emit code to save registers in the prologue.  */
8480
8481 static void
8482 ix86_emit_save_regs (void)
8483 {
8484   unsigned int regno;
8485   rtx insn;
8486
8487   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
8488     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8489       {
8490         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
8491         RTX_FRAME_RELATED_P (insn) = 1;
8492       }
8493 }
8494
8495 /* Emit code to save registers using MOV insns.  First register
8496    is restored from POINTER + OFFSET.  */
8497 static void
8498 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
8499 {
8500   unsigned int regno;
8501   rtx insn;
8502
8503   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8504     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8505       {
8506         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
8507                                                Pmode, offset),
8508                                gen_rtx_REG (Pmode, regno));
8509         RTX_FRAME_RELATED_P (insn) = 1;
8510         offset += UNITS_PER_WORD;
8511       }
8512 }
8513
8514 /* Emit code to save registers using MOV insns.  First register
8515    is restored from POINTER + OFFSET.  */
8516 static void
8517 ix86_emit_save_sse_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
8518 {
8519   unsigned int regno;
8520   rtx insn;
8521   rtx mem;
8522
8523   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8524     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8525       {
8526         mem = adjust_address (gen_rtx_MEM (TImode, pointer), TImode, offset);
8527         set_mem_align (mem, 128);
8528         insn = emit_move_insn (mem, gen_rtx_REG (TImode, regno));
8529         RTX_FRAME_RELATED_P (insn) = 1;
8530         offset += 16;
8531       }
8532 }
8533
8534 static GTY(()) rtx queued_cfa_restores;
8535
8536 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
8537    manipulation insn.  Don't add it if the previously
8538    saved value will be left untouched within stack red-zone till return,
8539    as unwinders can find the same value in the register and
8540    on the stack.  */
8541
8542 static void
8543 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT red_offset)
8544 {
8545   if (TARGET_RED_ZONE
8546       && !TARGET_64BIT_MS_ABI
8547       && red_offset + RED_ZONE_SIZE >= 0
8548       && crtl->args.pops_args < 65536)
8549     return;
8550
8551   if (insn)
8552     {
8553       add_reg_note (insn, REG_CFA_RESTORE, reg);
8554       RTX_FRAME_RELATED_P (insn) = 1;
8555     }
8556   else
8557     queued_cfa_restores
8558       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
8559 }
8560
8561 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
8562
8563 static void
8564 ix86_add_queued_cfa_restore_notes (rtx insn)
8565 {
8566   rtx last;
8567   if (!queued_cfa_restores)
8568     return;
8569   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
8570     ;
8571   XEXP (last, 1) = REG_NOTES (insn);
8572   REG_NOTES (insn) = queued_cfa_restores;
8573   queued_cfa_restores = NULL_RTX;
8574   RTX_FRAME_RELATED_P (insn) = 1;
8575 }
8576
8577 /* Expand prologue or epilogue stack adjustment.
8578    The pattern exist to put a dependency on all ebp-based memory accesses.
8579    STYLE should be negative if instructions should be marked as frame related,
8580    zero if %r11 register is live and cannot be freely used and positive
8581    otherwise.  */
8582
8583 static void
8584 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
8585                            int style, bool set_cfa)
8586 {
8587   rtx insn;
8588
8589   if (! TARGET_64BIT)
8590     insn = emit_insn (gen_pro_epilogue_adjust_stack_si_1 (dest, src, offset));
8591   else if (x86_64_immediate_operand (offset, DImode))
8592     insn = emit_insn (gen_pro_epilogue_adjust_stack_di_1 (dest, src, offset));
8593   else
8594     {
8595       rtx tmp;
8596       /* r11 is used by indirect sibcall return as well, set before the
8597          epilogue and used after the epilogue.  */
8598       if (style)
8599         tmp = gen_rtx_REG (DImode, R11_REG);
8600       else
8601         {
8602           gcc_assert (src != hard_frame_pointer_rtx
8603                       && dest != hard_frame_pointer_rtx);
8604           tmp = hard_frame_pointer_rtx;
8605         }
8606       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
8607       if (style < 0)
8608         RTX_FRAME_RELATED_P (insn) = 1;
8609       insn = emit_insn (gen_pro_epilogue_adjust_stack_di_2 (dest, src, tmp,
8610                                                             offset));
8611     }
8612
8613   if (style >= 0)
8614     ix86_add_queued_cfa_restore_notes (insn);
8615
8616   if (set_cfa)
8617     {
8618       rtx r;
8619
8620       gcc_assert (ix86_cfa_state->reg == src);
8621       ix86_cfa_state->offset += INTVAL (offset);
8622       ix86_cfa_state->reg = dest;
8623
8624       r = gen_rtx_PLUS (Pmode, src, offset);
8625       r = gen_rtx_SET (VOIDmode, dest, r);
8626       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
8627       RTX_FRAME_RELATED_P (insn) = 1;
8628     }
8629   else if (style < 0)
8630     RTX_FRAME_RELATED_P (insn) = 1;
8631 }
8632
8633 /* Find an available register to be used as dynamic realign argument
8634    pointer regsiter.  Such a register will be written in prologue and
8635    used in begin of body, so it must not be
8636         1. parameter passing register.
8637         2. GOT pointer.
8638    We reuse static-chain register if it is available.  Otherwise, we
8639    use DI for i386 and R13 for x86-64.  We chose R13 since it has
8640    shorter encoding.
8641
8642    Return: the regno of chosen register.  */
8643
8644 static unsigned int
8645 find_drap_reg (void)
8646 {
8647   tree decl = cfun->decl;
8648
8649   if (TARGET_64BIT)
8650     {
8651       /* Use R13 for nested function or function need static chain.
8652          Since function with tail call may use any caller-saved
8653          registers in epilogue, DRAP must not use caller-saved
8654          register in such case.  */
8655       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8656         return R13_REG;
8657
8658       return R10_REG;
8659     }
8660   else
8661     {
8662       /* Use DI for nested function or function need static chain.
8663          Since function with tail call may use any caller-saved
8664          registers in epilogue, DRAP must not use caller-saved
8665          register in such case.  */
8666       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8667         return DI_REG;
8668
8669       /* Reuse static chain register if it isn't used for parameter
8670          passing.  */
8671       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2
8672           && !lookup_attribute ("fastcall",
8673                                 TYPE_ATTRIBUTES (TREE_TYPE (decl)))
8674           && !lookup_attribute ("thiscall",
8675                                 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
8676         return CX_REG;
8677       else
8678         return DI_REG;
8679     }
8680 }
8681
8682 /* Return minimum incoming stack alignment.  */
8683
8684 static unsigned int
8685 ix86_minimum_incoming_stack_boundary (bool sibcall)
8686 {
8687   unsigned int incoming_stack_boundary;
8688
8689   /* Prefer the one specified at command line. */
8690   if (ix86_user_incoming_stack_boundary)
8691     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
8692   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
8693      if -mstackrealign is used, it isn't used for sibcall check and
8694      estimated stack alignment is 128bit.  */
8695   else if (!sibcall
8696            && !TARGET_64BIT
8697            && ix86_force_align_arg_pointer
8698            && crtl->stack_alignment_estimated == 128)
8699     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8700   else
8701     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
8702
8703   /* Incoming stack alignment can be changed on individual functions
8704      via force_align_arg_pointer attribute.  We use the smallest
8705      incoming stack boundary.  */
8706   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
8707       && lookup_attribute (ix86_force_align_arg_pointer_string,
8708                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
8709     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8710
8711   /* The incoming stack frame has to be aligned at least at
8712      parm_stack_boundary.  */
8713   if (incoming_stack_boundary < crtl->parm_stack_boundary)
8714     incoming_stack_boundary = crtl->parm_stack_boundary;
8715
8716   /* Stack at entrance of main is aligned by runtime.  We use the
8717      smallest incoming stack boundary. */
8718   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
8719       && DECL_NAME (current_function_decl)
8720       && MAIN_NAME_P (DECL_NAME (current_function_decl))
8721       && DECL_FILE_SCOPE_P (current_function_decl))
8722     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
8723
8724   return incoming_stack_boundary;
8725 }
8726
8727 /* Update incoming stack boundary and estimated stack alignment.  */
8728
8729 static void
8730 ix86_update_stack_boundary (void)
8731 {
8732   ix86_incoming_stack_boundary
8733     = ix86_minimum_incoming_stack_boundary (false);
8734
8735   /* x86_64 vararg needs 16byte stack alignment for register save
8736      area.  */
8737   if (TARGET_64BIT
8738       && cfun->stdarg
8739       && crtl->stack_alignment_estimated < 128)
8740     crtl->stack_alignment_estimated = 128;
8741 }
8742
8743 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
8744    needed or an rtx for DRAP otherwise.  */
8745
8746 static rtx
8747 ix86_get_drap_rtx (void)
8748 {
8749   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
8750     crtl->need_drap = true;
8751
8752   if (stack_realign_drap)
8753     {
8754       /* Assign DRAP to vDRAP and returns vDRAP */
8755       unsigned int regno = find_drap_reg ();
8756       rtx drap_vreg;
8757       rtx arg_ptr;
8758       rtx seq, insn;
8759
8760       arg_ptr = gen_rtx_REG (Pmode, regno);
8761       crtl->drap_reg = arg_ptr;
8762
8763       start_sequence ();
8764       drap_vreg = copy_to_reg (arg_ptr);
8765       seq = get_insns ();
8766       end_sequence ();
8767
8768       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
8769       if (!optimize)
8770         {
8771           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
8772           RTX_FRAME_RELATED_P (insn) = 1;
8773         }
8774       return drap_vreg;
8775     }
8776   else
8777     return NULL;
8778 }
8779
8780 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
8781
8782 static rtx
8783 ix86_internal_arg_pointer (void)
8784 {
8785   return virtual_incoming_args_rtx;
8786 }
8787
8788 struct scratch_reg {
8789   rtx reg;
8790   bool saved;
8791 };
8792
8793 /* Return a short-lived scratch register for use on function entry.
8794    In 32-bit mode, it is valid only after the registers are saved
8795    in the prologue.  This register must be released by means of
8796    release_scratch_register_on_entry once it is dead.  */
8797
8798 static void
8799 get_scratch_register_on_entry (struct scratch_reg *sr)
8800 {
8801   int regno;
8802
8803   sr->saved = false;
8804
8805   if (TARGET_64BIT)
8806     {
8807       /* We always use R11 in 64-bit mode.  */
8808       regno = R11_REG;
8809     }
8810   else
8811     {
8812       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
8813       bool fastcall_p
8814         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
8815       bool static_chain_p = DECL_STATIC_CHAIN (decl);
8816       int regparm = ix86_function_regparm (fntype, decl);
8817       int drap_regno
8818         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
8819
8820       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
8821           for the static chain register.  */
8822       if ((regparm < 1 || (fastcall_p && !static_chain_p))
8823           && drap_regno != AX_REG)
8824         regno = AX_REG;
8825       else if (regparm < 2 && drap_regno != DX_REG)
8826         regno = DX_REG;
8827       /* ecx is the static chain register.  */
8828       else if (regparm < 3 && !fastcall_p && !static_chain_p
8829                && drap_regno != CX_REG)
8830         regno = CX_REG;
8831       else if (ix86_save_reg (BX_REG, true))
8832         regno = BX_REG;
8833       /* esi is the static chain register.  */
8834       else if (!(regparm == 3 && static_chain_p)
8835                && ix86_save_reg (SI_REG, true))
8836         regno = SI_REG;
8837       else if (ix86_save_reg (DI_REG, true))
8838         regno = DI_REG;
8839       else
8840         {
8841           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
8842           sr->saved = true;
8843         }
8844     }
8845
8846   sr->reg = gen_rtx_REG (Pmode, regno);
8847   if (sr->saved)
8848     {
8849       rtx insn = emit_insn (gen_push (sr->reg));
8850       RTX_FRAME_RELATED_P (insn) = 1;
8851     }
8852 }
8853
8854 /* Release a scratch register obtained from the preceding function.  */
8855
8856 static void
8857 release_scratch_register_on_entry (struct scratch_reg *sr)
8858 {
8859   if (sr->saved)
8860     {
8861       rtx x, insn = emit_insn (ix86_gen_pop1 (sr->reg));
8862
8863       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
8864       RTX_FRAME_RELATED_P (insn) = 1;
8865       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
8866       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
8867       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
8868     }
8869 }
8870
8871 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
8872
8873 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
8874
8875 static void
8876 ix86_adjust_stack_and_probe (HOST_WIDE_INT size)
8877 {
8878   /* We skip the probe for the first interval + a small dope of 4 words and
8879      probe that many bytes past the specified size to maintain a protection
8880      area at the botton of the stack.  */
8881   const int dope = 4 * UNITS_PER_WORD;
8882   rtx size_rtx = GEN_INT (size);
8883
8884   /* See if we have a constant small number of probes to generate.  If so,
8885      that's the easy case.  The run-time loop is made up of 11 insns in the
8886      generic case while the compile-time loop is made up of 3+2*(n-1) insns
8887      for n # of intervals.  */
8888   if (size <= 5 * PROBE_INTERVAL)
8889     {
8890       HOST_WIDE_INT i, adjust;
8891       bool first_probe = true;
8892
8893       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
8894          values of N from 1 until it exceeds SIZE.  If only one probe is
8895          needed, this will not generate any code.  Then adjust and probe
8896          to PROBE_INTERVAL + SIZE.  */
8897       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
8898         {
8899           if (first_probe)
8900             {
8901               adjust = 2 * PROBE_INTERVAL + dope;
8902               first_probe = false;
8903             }
8904           else
8905             adjust = PROBE_INTERVAL;
8906
8907           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8908                                   plus_constant (stack_pointer_rtx, -adjust)));
8909           emit_stack_probe (stack_pointer_rtx);
8910         }
8911
8912       if (first_probe)
8913         adjust = size + PROBE_INTERVAL + dope;
8914       else
8915         adjust = size + PROBE_INTERVAL - i;
8916
8917       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8918                               plus_constant (stack_pointer_rtx, -adjust)));
8919       emit_stack_probe (stack_pointer_rtx);
8920
8921       /* Adjust back to account for the additional first interval.  */
8922       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8923                               plus_constant (stack_pointer_rtx,
8924                                              PROBE_INTERVAL + dope)));
8925     }
8926
8927   /* Otherwise, do the same as above, but in a loop.  Note that we must be
8928      extra careful with variables wrapping around because we might be at
8929      the very top (or the very bottom) of the address space and we have
8930      to be able to handle this case properly; in particular, we use an
8931      equality test for the loop condition.  */
8932   else
8933     {
8934       HOST_WIDE_INT rounded_size;
8935       struct scratch_reg sr;
8936
8937       get_scratch_register_on_entry (&sr);
8938
8939
8940       /* Step 1: round SIZE to the previous multiple of the interval.  */
8941
8942       rounded_size = size & -PROBE_INTERVAL;
8943
8944
8945       /* Step 2: compute initial and final value of the loop counter.  */
8946
8947       /* SP = SP_0 + PROBE_INTERVAL.  */
8948       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8949                               plus_constant (stack_pointer_rtx,
8950                                              - (PROBE_INTERVAL + dope))));
8951
8952       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
8953       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
8954       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
8955                               gen_rtx_PLUS (Pmode, sr.reg,
8956                                             stack_pointer_rtx)));
8957
8958
8959       /* Step 3: the loop
8960
8961          while (SP != LAST_ADDR)
8962            {
8963              SP = SP + PROBE_INTERVAL
8964              probe at SP
8965            }
8966
8967          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
8968          values of N from 1 until it is equal to ROUNDED_SIZE.  */
8969
8970       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
8971
8972
8973       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
8974          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
8975
8976       if (size != rounded_size)
8977         {
8978           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8979                                   plus_constant (stack_pointer_rtx,
8980                                                  rounded_size - size)));
8981           emit_stack_probe (stack_pointer_rtx);
8982         }
8983
8984       /* Adjust back to account for the additional first interval.  */
8985       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8986                               plus_constant (stack_pointer_rtx,
8987                                              PROBE_INTERVAL + dope)));
8988
8989       release_scratch_register_on_entry (&sr);
8990     }
8991
8992   gcc_assert (ix86_cfa_state->reg != stack_pointer_rtx);
8993
8994   /* Make sure nothing is scheduled before we are done.  */
8995   emit_insn (gen_blockage ());
8996 }
8997
8998 /* Adjust the stack pointer up to REG while probing it.  */
8999
9000 const char *
9001 output_adjust_stack_and_probe (rtx reg)
9002 {
9003   static int labelno = 0;
9004   char loop_lab[32], end_lab[32];
9005   rtx xops[2];
9006
9007   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9008   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9009
9010   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9011
9012   /* Jump to END_LAB if SP == LAST_ADDR.  */
9013   xops[0] = stack_pointer_rtx;
9014   xops[1] = reg;
9015   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9016   fputs ("\tje\t", asm_out_file);
9017   assemble_name_raw (asm_out_file, end_lab);
9018   fputc ('\n', asm_out_file);
9019
9020   /* SP = SP + PROBE_INTERVAL.  */
9021   xops[1] = GEN_INT (PROBE_INTERVAL);
9022   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9023
9024   /* Probe at SP.  */
9025   xops[1] = const0_rtx;
9026   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
9027
9028   fprintf (asm_out_file, "\tjmp\t");
9029   assemble_name_raw (asm_out_file, loop_lab);
9030   fputc ('\n', asm_out_file);
9031
9032   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9033
9034   return "";
9035 }
9036
9037 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
9038    inclusive.  These are offsets from the current stack pointer.  */
9039
9040 static void
9041 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
9042 {
9043   /* See if we have a constant small number of probes to generate.  If so,
9044      that's the easy case.  The run-time loop is made up of 7 insns in the
9045      generic case while the compile-time loop is made up of n insns for n #
9046      of intervals.  */
9047   if (size <= 7 * PROBE_INTERVAL)
9048     {
9049       HOST_WIDE_INT i;
9050
9051       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
9052          it exceeds SIZE.  If only one probe is needed, this will not
9053          generate any code.  Then probe at FIRST + SIZE.  */
9054       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9055         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
9056
9057       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
9058     }
9059
9060   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9061      extra careful with variables wrapping around because we might be at
9062      the very top (or the very bottom) of the address space and we have
9063      to be able to handle this case properly; in particular, we use an
9064      equality test for the loop condition.  */
9065   else
9066     {
9067       HOST_WIDE_INT rounded_size, last;
9068       struct scratch_reg sr;
9069
9070       get_scratch_register_on_entry (&sr);
9071
9072
9073       /* Step 1: round SIZE to the previous multiple of the interval.  */
9074
9075       rounded_size = size & -PROBE_INTERVAL;
9076
9077
9078       /* Step 2: compute initial and final value of the loop counter.  */
9079
9080       /* TEST_OFFSET = FIRST.  */
9081       emit_move_insn (sr.reg, GEN_INT (-first));
9082
9083       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
9084       last = first + rounded_size;
9085
9086
9087       /* Step 3: the loop
9088
9089          while (TEST_ADDR != LAST_ADDR)
9090            {
9091              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
9092              probe at TEST_ADDR
9093            }
9094
9095          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
9096          until it is equal to ROUNDED_SIZE.  */
9097
9098       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
9099
9100
9101       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
9102          that SIZE is equal to ROUNDED_SIZE.  */
9103
9104       if (size != rounded_size)
9105         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
9106                                                        stack_pointer_rtx,
9107                                                        sr.reg),
9108                                          rounded_size - size));
9109
9110       release_scratch_register_on_entry (&sr);
9111     }
9112
9113   /* Make sure nothing is scheduled before we are done.  */
9114   emit_insn (gen_blockage ());
9115 }
9116
9117 /* Probe a range of stack addresses from REG to END, inclusive.  These are
9118    offsets from the current stack pointer.  */
9119
9120 const char *
9121 output_probe_stack_range (rtx reg, rtx end)
9122 {
9123   static int labelno = 0;
9124   char loop_lab[32], end_lab[32];
9125   rtx xops[3];
9126
9127   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9128   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9129
9130   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9131
9132   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
9133   xops[0] = reg;
9134   xops[1] = end;
9135   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9136   fputs ("\tje\t", asm_out_file);
9137   assemble_name_raw (asm_out_file, end_lab);
9138   fputc ('\n', asm_out_file);
9139
9140   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
9141   xops[1] = GEN_INT (PROBE_INTERVAL);
9142   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9143
9144   /* Probe at TEST_ADDR.  */
9145   xops[0] = stack_pointer_rtx;
9146   xops[1] = reg;
9147   xops[2] = const0_rtx;
9148   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
9149
9150   fprintf (asm_out_file, "\tjmp\t");
9151   assemble_name_raw (asm_out_file, loop_lab);
9152   fputc ('\n', asm_out_file);
9153
9154   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9155
9156   return "";
9157 }
9158
9159 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
9160    to be generated in correct form.  */
9161 static void
9162 ix86_finalize_stack_realign_flags (void)
9163 {
9164   /* Check if stack realign is really needed after reload, and
9165      stores result in cfun */
9166   unsigned int incoming_stack_boundary
9167     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
9168        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
9169   unsigned int stack_realign = (incoming_stack_boundary
9170                                 < (current_function_is_leaf
9171                                    ? crtl->max_used_stack_slot_alignment
9172                                    : crtl->stack_alignment_needed));
9173
9174   if (crtl->stack_realign_finalized)
9175     {
9176       /* After stack_realign_needed is finalized, we can't no longer
9177          change it.  */
9178       gcc_assert (crtl->stack_realign_needed == stack_realign);
9179     }
9180   else
9181     {
9182       crtl->stack_realign_needed = stack_realign;
9183       crtl->stack_realign_finalized = true;
9184     }
9185 }
9186
9187 /* Expand the prologue into a bunch of separate insns.  */
9188
9189 void
9190 ix86_expand_prologue (void)
9191 {
9192   rtx insn;
9193   bool pic_reg_used;
9194   struct ix86_frame frame;
9195   HOST_WIDE_INT allocate;
9196   int gen_frame_pointer = frame_pointer_needed;
9197
9198   ix86_finalize_stack_realign_flags ();
9199
9200   /* DRAP should not coexist with stack_realign_fp */
9201   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
9202
9203   /* Initialize CFA state for before the prologue.  */
9204   ix86_cfa_state->reg = stack_pointer_rtx;
9205   ix86_cfa_state->offset = INCOMING_FRAME_SP_OFFSET;
9206
9207   ix86_compute_frame_layout (&frame);
9208
9209   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
9210     {
9211       rtx push, mov;
9212
9213       /* Check if profiling is active and we shall use profiling before
9214          prologue variant. If so sorry.  */
9215       if (crtl->profile && flag_fentry != 0)
9216         sorry ("ms_hook_prologue attribute isn't compatible with -mfentry for 32-bit");
9217
9218       /* Make sure the function starts with
9219          8b ff     movl.s %edi,%edi (emited by ix86_asm_output_function_label)
9220          55        push   %ebp
9221          8b ec     movl.s %esp,%ebp
9222
9223          This matches the hookable function prologue in Win32 API
9224          functions in Microsoft Windows XP Service Pack 2 and newer.
9225          Wine uses this to enable Windows apps to hook the Win32 API
9226          functions provided by Wine.  */
9227       push = emit_insn (gen_push (hard_frame_pointer_rtx));
9228       mov = emit_insn (gen_vswapmov (hard_frame_pointer_rtx,
9229                                      stack_pointer_rtx));
9230
9231       if (frame_pointer_needed && !(crtl->drap_reg
9232                                     && crtl->stack_realign_needed))
9233         {
9234           /* The push %ebp and movl.s %esp, %ebp already set up
9235              the frame pointer.  No need to do this again. */
9236           gen_frame_pointer = 0;
9237           RTX_FRAME_RELATED_P (push) = 1;
9238           RTX_FRAME_RELATED_P (mov) = 1;
9239           if (ix86_cfa_state->reg == stack_pointer_rtx)
9240             ix86_cfa_state->reg = hard_frame_pointer_rtx;
9241         }
9242       else
9243         /* If the frame pointer is not needed, pop %ebp again. This
9244            could be optimized for cases where ebp needs to be backed up
9245            for some other reason.  If stack realignment is needed, pop
9246            the base pointer again, align the stack, and later regenerate
9247            the frame pointer setup.  The frame pointer generated by the
9248            hook prologue is not aligned, so it can't be used.  */
9249         insn = emit_insn (ix86_gen_pop1 (hard_frame_pointer_rtx));
9250     }
9251
9252   /* The first insn of a function that accepts its static chain on the
9253      stack is to push the register that would be filled in by a direct
9254      call.  This insn will be skipped by the trampoline.  */
9255   if (ix86_static_chain_on_stack)
9256     {
9257       rtx t;
9258
9259       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
9260       emit_insn (gen_blockage ());
9261
9262       /* We don't want to interpret this push insn as a register save,
9263          only as a stack adjustment.  The real copy of the register as
9264          a save will be done later, if needed.  */
9265       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
9266       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
9267       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
9268       RTX_FRAME_RELATED_P (insn) = 1;
9269     }
9270
9271   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
9272      of DRAP is needed and stack realignment is really needed after reload */
9273   if (crtl->drap_reg && crtl->stack_realign_needed)
9274     {
9275       rtx x, y;
9276       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
9277       int param_ptr_offset = UNITS_PER_WORD;
9278
9279       if (ix86_static_chain_on_stack)
9280         param_ptr_offset += UNITS_PER_WORD;
9281       if (!call_used_regs[REGNO (crtl->drap_reg)])
9282         param_ptr_offset += UNITS_PER_WORD;
9283
9284       gcc_assert (stack_realign_drap);
9285
9286       /* Grab the argument pointer.  */
9287       x = plus_constant (stack_pointer_rtx, param_ptr_offset);
9288       y = crtl->drap_reg;
9289
9290       /* Only need to push parameter pointer reg if it is caller
9291          saved reg */
9292       if (!call_used_regs[REGNO (crtl->drap_reg)])
9293         {
9294           /* Push arg pointer reg */
9295           insn = emit_insn (gen_push (y));
9296           RTX_FRAME_RELATED_P (insn) = 1;
9297         }
9298
9299       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
9300       RTX_FRAME_RELATED_P (insn) = 1;
9301       ix86_cfa_state->reg = crtl->drap_reg;
9302
9303       /* Align the stack.  */
9304       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
9305                                         stack_pointer_rtx,
9306                                         GEN_INT (-align_bytes)));
9307       RTX_FRAME_RELATED_P (insn) = 1;
9308
9309       /* Replicate the return address on the stack so that return
9310          address can be reached via (argp - 1) slot.  This is needed
9311          to implement macro RETURN_ADDR_RTX and intrinsic function
9312          expand_builtin_return_addr etc.  */
9313       x = crtl->drap_reg;
9314       x = gen_frame_mem (Pmode,
9315                          plus_constant (x, -UNITS_PER_WORD));
9316       insn = emit_insn (gen_push (x));
9317       RTX_FRAME_RELATED_P (insn) = 1;
9318     }
9319
9320   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
9321      slower on all targets.  Also sdb doesn't like it.  */
9322
9323   if (gen_frame_pointer)
9324     {
9325       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
9326       RTX_FRAME_RELATED_P (insn) = 1;
9327
9328       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
9329       RTX_FRAME_RELATED_P (insn) = 1;
9330
9331       if (ix86_cfa_state->reg == stack_pointer_rtx)
9332         ix86_cfa_state->reg = hard_frame_pointer_rtx;
9333     }
9334
9335   if (stack_realign_fp)
9336     {
9337       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
9338       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
9339
9340       /* Align the stack.  */
9341       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
9342                                         stack_pointer_rtx,
9343                                         GEN_INT (-align_bytes)));
9344       RTX_FRAME_RELATED_P (insn) = 1;
9345     }
9346
9347   allocate = frame.to_allocate + frame.nsseregs * 16 + frame.padding0;
9348
9349   if (!frame.save_regs_using_mov)
9350     ix86_emit_save_regs ();
9351   else
9352     allocate += frame.nregs * UNITS_PER_WORD;
9353
9354   /* The stack has already been decremented by the instruction calling us
9355      so we need to probe unconditionally to preserve the protection area.  */
9356   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
9357     {
9358       /* We expect the registers to be saved when probes are used.  */
9359       gcc_assert (!frame.save_regs_using_mov);
9360
9361       if (STACK_CHECK_MOVING_SP)
9362         {
9363           ix86_adjust_stack_and_probe (allocate);
9364           allocate = 0;
9365         }
9366       else
9367         {
9368           HOST_WIDE_INT size = allocate;
9369
9370           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
9371             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
9372
9373           if (TARGET_STACK_PROBE)
9374             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
9375           else
9376             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
9377         }
9378     }
9379
9380   /* When using red zone we may start register saving before allocating
9381      the stack frame saving one cycle of the prologue. However I will
9382      avoid doing this if I am going to have to probe the stack since
9383      at least on x86_64 the stack probe can turn into a call that clobbers
9384      a red zone location */
9385   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE && frame.save_regs_using_mov
9386       && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT))
9387     ix86_emit_save_regs_using_mov ((frame_pointer_needed
9388                                      && !crtl->stack_realign_needed)
9389                                    ? hard_frame_pointer_rtx
9390                                    : stack_pointer_rtx,
9391                                    -frame.nregs * UNITS_PER_WORD);
9392
9393   if (allocate == 0)
9394     ;
9395   else if (!ix86_target_stack_probe () || allocate < CHECK_STACK_LIMIT)
9396     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9397                                GEN_INT (-allocate), -1,
9398                                ix86_cfa_state->reg == stack_pointer_rtx);
9399   else
9400     {
9401       rtx eax = gen_rtx_REG (Pmode, AX_REG);
9402       bool eax_live;
9403       rtx t;
9404
9405       if (cfun->machine->call_abi == MS_ABI)
9406         eax_live = false;
9407       else
9408         eax_live = ix86_eax_live_at_start_p ();
9409
9410       if (eax_live)
9411         {
9412           emit_insn (gen_push (eax));
9413           allocate -= UNITS_PER_WORD;
9414         }
9415
9416       emit_move_insn (eax, GEN_INT (allocate));
9417
9418       insn = emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
9419
9420       if (ix86_cfa_state->reg == stack_pointer_rtx)
9421         {
9422           ix86_cfa_state->offset += allocate;
9423           t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
9424           t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
9425           add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
9426           RTX_FRAME_RELATED_P (insn) = 1;
9427         }
9428
9429       if (eax_live)
9430         {
9431           if (frame_pointer_needed)
9432             t = plus_constant (hard_frame_pointer_rtx,
9433                                allocate
9434                                - frame.to_allocate
9435                                - frame.nregs * UNITS_PER_WORD);
9436           else
9437             t = plus_constant (stack_pointer_rtx, allocate);
9438           emit_move_insn (eax, gen_rtx_MEM (Pmode, t));
9439         }
9440     }
9441
9442   if (frame.save_regs_using_mov
9443       && !(!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE
9444          && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)))
9445     {
9446       if (!frame_pointer_needed
9447           || !(frame.to_allocate + frame.padding0)
9448           || crtl->stack_realign_needed)
9449         ix86_emit_save_regs_using_mov (stack_pointer_rtx,
9450                                        frame.to_allocate
9451                                        + frame.nsseregs * 16 + frame.padding0);
9452       else
9453         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
9454                                        -frame.nregs * UNITS_PER_WORD);
9455     }
9456   if (!frame_pointer_needed
9457       || !(frame.to_allocate + frame.padding0)
9458       || crtl->stack_realign_needed)
9459     ix86_emit_save_sse_regs_using_mov (stack_pointer_rtx,
9460                                        frame.to_allocate);
9461   else
9462     ix86_emit_save_sse_regs_using_mov (hard_frame_pointer_rtx,
9463                                        - frame.nregs * UNITS_PER_WORD
9464                                        - frame.nsseregs * 16
9465                                        - frame.padding0);
9466
9467   pic_reg_used = false;
9468   if (pic_offset_table_rtx
9469       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
9470           || crtl->profile))
9471     {
9472       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
9473
9474       if (alt_pic_reg_used != INVALID_REGNUM)
9475         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
9476
9477       pic_reg_used = true;
9478     }
9479
9480   if (pic_reg_used)
9481     {
9482       if (TARGET_64BIT)
9483         {
9484           if (ix86_cmodel == CM_LARGE_PIC)
9485             {
9486               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
9487               rtx label = gen_label_rtx ();
9488               emit_label (label);
9489               LABEL_PRESERVE_P (label) = 1;
9490               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
9491               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
9492               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
9493               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
9494                                             pic_offset_table_rtx, tmp_reg));
9495             }
9496           else
9497             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
9498         }
9499       else
9500         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
9501     }
9502
9503   /* In the pic_reg_used case, make sure that the got load isn't deleted
9504      when mcount needs it.  Blockage to avoid call movement across mcount
9505      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
9506      note.  */
9507   if (crtl->profile && !flag_fentry && pic_reg_used)
9508     emit_insn (gen_prologue_use (pic_offset_table_rtx));
9509
9510   if (crtl->drap_reg && !crtl->stack_realign_needed)
9511     {
9512       /* vDRAP is setup but after reload it turns out stack realign
9513          isn't necessary, here we will emit prologue to setup DRAP
9514          without stack realign adjustment */
9515       rtx x;
9516       int drap_bp_offset = UNITS_PER_WORD * 2;
9517
9518       if (ix86_static_chain_on_stack)
9519         drap_bp_offset += UNITS_PER_WORD;
9520       x = plus_constant (hard_frame_pointer_rtx, drap_bp_offset);
9521       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, x));
9522     }
9523
9524   /* Prevent instructions from being scheduled into register save push
9525      sequence when access to the redzone area is done through frame pointer.
9526      The offset between the frame pointer and the stack pointer is calculated
9527      relative to the value of the stack pointer at the end of the function
9528      prologue, and moving instructions that access redzone area via frame
9529      pointer inside push sequence violates this assumption.  */
9530   if (frame_pointer_needed && frame.red_zone_size)
9531     emit_insn (gen_memory_blockage ());
9532
9533   /* Emit cld instruction if stringops are used in the function.  */
9534   if (TARGET_CLD && ix86_current_function_needs_cld)
9535     emit_insn (gen_cld ());
9536 }
9537
9538 /* Emit code to restore REG using a POP insn.  */
9539
9540 static void
9541 ix86_emit_restore_reg_using_pop (rtx reg, HOST_WIDE_INT red_offset)
9542 {
9543   rtx insn = emit_insn (ix86_gen_pop1 (reg));
9544
9545   if (ix86_cfa_state->reg == crtl->drap_reg
9546       && REGNO (reg) == REGNO (crtl->drap_reg))
9547     {
9548       /* Previously we'd represented the CFA as an expression
9549          like *(%ebp - 8).  We've just popped that value from
9550          the stack, which means we need to reset the CFA to
9551          the drap register.  This will remain until we restore
9552          the stack pointer.  */
9553       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
9554       RTX_FRAME_RELATED_P (insn) = 1;
9555       return;
9556     }
9557
9558   if (ix86_cfa_state->reg == stack_pointer_rtx)
9559     {
9560       ix86_cfa_state->offset -= UNITS_PER_WORD;
9561       add_reg_note (insn, REG_CFA_ADJUST_CFA,
9562                     copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
9563       RTX_FRAME_RELATED_P (insn) = 1;
9564     }
9565
9566   /* When the frame pointer is the CFA, and we pop it, we are
9567      swapping back to the stack pointer as the CFA.  This happens
9568      for stack frames that don't allocate other data, so we assume
9569      the stack pointer is now pointing at the return address, i.e.
9570      the function entry state, which makes the offset be 1 word.  */
9571   else if (ix86_cfa_state->reg == hard_frame_pointer_rtx
9572            && reg == hard_frame_pointer_rtx)
9573     {
9574       ix86_cfa_state->reg = stack_pointer_rtx;
9575       ix86_cfa_state->offset -= UNITS_PER_WORD;
9576
9577       add_reg_note (insn, REG_CFA_DEF_CFA,
9578                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
9579                                   GEN_INT (ix86_cfa_state->offset)));
9580       RTX_FRAME_RELATED_P (insn) = 1;
9581     }
9582
9583   ix86_add_cfa_restore_note (insn, reg, red_offset);
9584 }
9585
9586 /* Emit code to restore saved registers using POP insns.  */
9587
9588 static void
9589 ix86_emit_restore_regs_using_pop (HOST_WIDE_INT red_offset)
9590 {
9591   int regno;
9592
9593   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9594     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
9595       {
9596         ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno),
9597                                          red_offset);
9598         red_offset += UNITS_PER_WORD;
9599       }
9600 }
9601
9602 /* Emit code and notes for the LEAVE instruction.  */
9603
9604 static void
9605 ix86_emit_leave (HOST_WIDE_INT red_offset)
9606 {
9607   rtx insn = emit_insn (ix86_gen_leave ());
9608
9609   ix86_add_queued_cfa_restore_notes (insn);
9610
9611   if (ix86_cfa_state->reg == hard_frame_pointer_rtx)
9612     {
9613       ix86_cfa_state->reg = stack_pointer_rtx;
9614       ix86_cfa_state->offset -= UNITS_PER_WORD;
9615
9616       add_reg_note (insn, REG_CFA_ADJUST_CFA,
9617                     copy_rtx (XVECEXP (PATTERN (insn), 0, 0)));
9618       RTX_FRAME_RELATED_P (insn) = 1;
9619       ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx, red_offset);
9620     }
9621 }
9622
9623 /* Emit code to restore saved registers using MOV insns.  First register
9624    is restored from POINTER + OFFSET.  */
9625 static void
9626 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
9627                                   HOST_WIDE_INT red_offset,
9628                                   int maybe_eh_return)
9629 {
9630   unsigned int regno;
9631   rtx base_address = gen_rtx_MEM (Pmode, pointer);
9632   rtx insn;
9633
9634   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9635     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
9636       {
9637         rtx reg = gen_rtx_REG (Pmode, regno);
9638
9639         /* Ensure that adjust_address won't be forced to produce pointer
9640            out of range allowed by x86-64 instruction set.  */
9641         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
9642           {
9643             rtx r11;
9644
9645             r11 = gen_rtx_REG (DImode, R11_REG);
9646             emit_move_insn (r11, GEN_INT (offset));
9647             emit_insn (gen_adddi3 (r11, r11, pointer));
9648             base_address = gen_rtx_MEM (Pmode, r11);
9649             offset = 0;
9650           }
9651         insn = emit_move_insn (reg,
9652                                adjust_address (base_address, Pmode, offset));
9653         offset += UNITS_PER_WORD;
9654
9655         if (ix86_cfa_state->reg == crtl->drap_reg
9656             && regno == REGNO (crtl->drap_reg))
9657           {
9658             /* Previously we'd represented the CFA as an expression
9659                like *(%ebp - 8).  We've just popped that value from
9660                the stack, which means we need to reset the CFA to
9661                the drap register.  This will remain until we restore
9662                the stack pointer.  */
9663             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
9664             RTX_FRAME_RELATED_P (insn) = 1;
9665           }
9666         else
9667           ix86_add_cfa_restore_note (NULL_RTX, reg, red_offset);
9668
9669         red_offset += UNITS_PER_WORD;
9670       }
9671 }
9672
9673 /* Emit code to restore saved registers using MOV insns.  First register
9674    is restored from POINTER + OFFSET.  */
9675 static void
9676 ix86_emit_restore_sse_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
9677                                       HOST_WIDE_INT red_offset,
9678                                       int maybe_eh_return)
9679 {
9680   int regno;
9681   rtx base_address = gen_rtx_MEM (TImode, pointer);
9682   rtx mem;
9683
9684   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9685     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
9686       {
9687         rtx reg = gen_rtx_REG (TImode, regno);
9688
9689         /* Ensure that adjust_address won't be forced to produce pointer
9690            out of range allowed by x86-64 instruction set.  */
9691         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
9692           {
9693             rtx r11;
9694
9695             r11 = gen_rtx_REG (DImode, R11_REG);
9696             emit_move_insn (r11, GEN_INT (offset));
9697             emit_insn (gen_adddi3 (r11, r11, pointer));
9698             base_address = gen_rtx_MEM (TImode, r11);
9699             offset = 0;
9700           }
9701         mem = adjust_address (base_address, TImode, offset);
9702         set_mem_align (mem, 128);
9703         emit_move_insn (reg, mem);
9704         offset += 16;
9705
9706         ix86_add_cfa_restore_note (NULL_RTX, reg, red_offset);
9707
9708         red_offset += 16;
9709       }
9710 }
9711
9712 /* Restore function stack, frame, and registers.  */
9713
9714 void
9715 ix86_expand_epilogue (int style)
9716 {
9717   int sp_valid;
9718   struct ix86_frame frame;
9719   HOST_WIDE_INT offset, red_offset;
9720   struct machine_cfa_state cfa_state_save = *ix86_cfa_state;
9721   bool using_drap;
9722
9723   ix86_finalize_stack_realign_flags ();
9724
9725  /* When stack is realigned, SP must be valid.  */
9726   sp_valid = (!frame_pointer_needed
9727               || current_function_sp_is_unchanging
9728               || stack_realign_fp);
9729
9730   ix86_compute_frame_layout (&frame);
9731
9732   /* See the comment about red zone and frame
9733      pointer usage in ix86_expand_prologue.  */
9734   if (frame_pointer_needed && frame.red_zone_size)
9735     emit_insn (gen_memory_blockage ());
9736
9737   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
9738   gcc_assert (!using_drap || ix86_cfa_state->reg == crtl->drap_reg);
9739
9740   /* Calculate start of saved registers relative to ebp.  Special care
9741      must be taken for the normal return case of a function using
9742      eh_return: the eax and edx registers are marked as saved, but not
9743      restored along this path.  */
9744   offset = frame.nregs;
9745   if (crtl->calls_eh_return && style != 2)
9746     offset -= 2;
9747   offset *= -UNITS_PER_WORD;
9748   offset -= frame.nsseregs * 16 + frame.padding0;
9749
9750   /* Calculate start of saved registers relative to esp on entry of the
9751      function.  When realigning stack, this needs to be the most negative
9752      value possible at runtime.  */
9753   red_offset = offset;
9754   if (using_drap)
9755     red_offset -= crtl->stack_alignment_needed / BITS_PER_UNIT
9756                   + UNITS_PER_WORD;
9757   else if (stack_realign_fp)
9758     red_offset -= crtl->stack_alignment_needed / BITS_PER_UNIT
9759                   - UNITS_PER_WORD;
9760   if (ix86_static_chain_on_stack)
9761     red_offset -= UNITS_PER_WORD;
9762   if (frame_pointer_needed)
9763     red_offset -= UNITS_PER_WORD;
9764
9765   /* If we're only restoring one register and sp is not valid then
9766      using a move instruction to restore the register since it's
9767      less work than reloading sp and popping the register.
9768
9769      The default code result in stack adjustment using add/lea instruction,
9770      while this code results in LEAVE instruction (or discrete equivalent),
9771      so it is profitable in some other cases as well.  Especially when there
9772      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
9773      and there is exactly one register to pop. This heuristic may need some
9774      tuning in future.  */
9775   if ((!sp_valid && (frame.nregs + frame.nsseregs) <= 1)
9776       || (TARGET_EPILOGUE_USING_MOVE
9777           && cfun->machine->use_fast_prologue_epilogue
9778           && ((frame.nregs + frame.nsseregs) > 1
9779               || (frame.to_allocate + frame.padding0) != 0))
9780       || (frame_pointer_needed && !(frame.nregs + frame.nsseregs)
9781           && (frame.to_allocate + frame.padding0) != 0)
9782       || (frame_pointer_needed && TARGET_USE_LEAVE
9783           && cfun->machine->use_fast_prologue_epilogue
9784           && (frame.nregs + frame.nsseregs) == 1)
9785       || crtl->calls_eh_return)
9786     {
9787       /* Restore registers.  We can use ebp or esp to address the memory
9788          locations.  If both are available, default to ebp, since offsets
9789          are known to be small.  Only exception is esp pointing directly
9790          to the end of block of saved registers, where we may simplify
9791          addressing mode.
9792
9793          If we are realigning stack with bp and sp, regs restore can't
9794          be addressed by bp. sp must be used instead.  */
9795
9796       if (!frame_pointer_needed
9797           || (sp_valid && !(frame.to_allocate + frame.padding0))
9798           || stack_realign_fp)
9799         {
9800           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9801                                                 frame.to_allocate, red_offset,
9802                                                 style == 2);
9803           ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
9804                                             frame.to_allocate
9805                                             + frame.nsseregs * 16
9806                                             + frame.padding0,
9807                                             red_offset
9808                                             + frame.nsseregs * 16
9809                                             + frame.padding0, style == 2);
9810         }
9811       else
9812         {
9813           ix86_emit_restore_sse_regs_using_mov (hard_frame_pointer_rtx,
9814                                                 offset, red_offset,
9815                                                 style == 2);
9816           ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
9817                                             offset
9818                                             + frame.nsseregs * 16
9819                                             + frame.padding0,
9820                                             red_offset
9821                                             + frame.nsseregs * 16
9822                                             + frame.padding0, style == 2);
9823         }
9824
9825       red_offset -= offset;
9826
9827       /* eh_return epilogues need %ecx added to the stack pointer.  */
9828       if (style == 2)
9829         {
9830           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
9831
9832           /* Stack align doesn't work with eh_return.  */
9833           gcc_assert (!crtl->stack_realign_needed);
9834           /* Neither does regparm nested functions.  */
9835           gcc_assert (!ix86_static_chain_on_stack);
9836
9837           if (frame_pointer_needed)
9838             {
9839               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
9840               tmp = plus_constant (tmp, UNITS_PER_WORD);
9841               tmp = emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
9842
9843               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
9844               tmp = emit_move_insn (hard_frame_pointer_rtx, tmp);
9845
9846               /* Note that we use SA as a temporary CFA, as the return
9847                  address is at the proper place relative to it.  We
9848                  pretend this happens at the FP restore insn because
9849                  prior to this insn the FP would be stored at the wrong
9850                  offset relative to SA, and after this insn we have no
9851                  other reasonable register to use for the CFA.  We don't
9852                  bother resetting the CFA to the SP for the duration of
9853                  the return insn.  */
9854               add_reg_note (tmp, REG_CFA_DEF_CFA,
9855                             plus_constant (sa, UNITS_PER_WORD));
9856               ix86_add_queued_cfa_restore_notes (tmp);
9857               add_reg_note (tmp, REG_CFA_RESTORE, hard_frame_pointer_rtx);
9858               RTX_FRAME_RELATED_P (tmp) = 1;
9859               ix86_cfa_state->reg = sa;
9860               ix86_cfa_state->offset = UNITS_PER_WORD;
9861
9862               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
9863                                          const0_rtx, style, false);
9864             }
9865           else
9866             {
9867               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
9868               tmp = plus_constant (tmp, (frame.to_allocate
9869                                          + frame.nregs * UNITS_PER_WORD
9870                                          + frame.nsseregs * 16
9871                                          + frame.padding0));
9872               tmp = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
9873               ix86_add_queued_cfa_restore_notes (tmp);
9874
9875               gcc_assert (ix86_cfa_state->reg == stack_pointer_rtx);
9876               if (ix86_cfa_state->offset != UNITS_PER_WORD)
9877                 {
9878                   ix86_cfa_state->offset = UNITS_PER_WORD;
9879                   add_reg_note (tmp, REG_CFA_DEF_CFA,
9880                                 plus_constant (stack_pointer_rtx,
9881                                                UNITS_PER_WORD));
9882                   RTX_FRAME_RELATED_P (tmp) = 1;
9883                 }
9884             }
9885         }
9886       else if (!frame_pointer_needed)
9887         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9888                                    GEN_INT (frame.to_allocate
9889                                             + frame.nregs * UNITS_PER_WORD
9890                                             + frame.nsseregs * 16
9891                                             + frame.padding0),
9892                                    style, !using_drap);
9893       /* If not an i386, mov & pop is faster than "leave".  */
9894       else if (TARGET_USE_LEAVE || optimize_function_for_size_p (cfun)
9895                || !cfun->machine->use_fast_prologue_epilogue)
9896         ix86_emit_leave (red_offset);
9897       else
9898         {
9899           pro_epilogue_adjust_stack (stack_pointer_rtx,
9900                                      hard_frame_pointer_rtx,
9901                                      const0_rtx, style, !using_drap);
9902
9903           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx, red_offset);
9904         }
9905     }
9906   else
9907     {
9908       /* First step is to deallocate the stack frame so that we can
9909          pop the registers.
9910
9911          If we realign stack with frame pointer, then stack pointer
9912          won't be able to recover via lea $offset(%bp), %sp, because
9913          there is a padding area between bp and sp for realign.
9914          "add $to_allocate, %sp" must be used instead.  */
9915       if (!sp_valid)
9916         {
9917           gcc_assert (frame_pointer_needed);
9918           gcc_assert (!stack_realign_fp);
9919           pro_epilogue_adjust_stack (stack_pointer_rtx,
9920                                      hard_frame_pointer_rtx,
9921                                      GEN_INT (offset), style, false);
9922           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9923                                                 0, red_offset,
9924                                                 style == 2);
9925           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9926                                      GEN_INT (frame.nsseregs * 16
9927                                               + frame.padding0),
9928                                      style, false);
9929         }
9930       else if (frame.to_allocate || frame.padding0 || frame.nsseregs)
9931         {
9932           ix86_emit_restore_sse_regs_using_mov (stack_pointer_rtx,
9933                                                 frame.to_allocate, red_offset,
9934                                                 style == 2);
9935           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9936                                      GEN_INT (frame.to_allocate
9937                                               + frame.nsseregs * 16
9938                                               + frame.padding0), style,
9939                                      !using_drap && !frame_pointer_needed);
9940         }
9941
9942       ix86_emit_restore_regs_using_pop (red_offset + frame.nsseregs * 16
9943                                         + frame.padding0);
9944       red_offset -= offset;
9945
9946       if (frame_pointer_needed)
9947         {
9948           /* Leave results in shorter dependency chains on CPUs that are
9949              able to grok it fast.  */
9950           if (TARGET_USE_LEAVE)
9951             ix86_emit_leave (red_offset);
9952           else
9953             {
9954               /* For stack realigned really happens, recover stack
9955                  pointer to hard frame pointer is a must, if not using
9956                  leave.  */
9957               if (stack_realign_fp)
9958                 pro_epilogue_adjust_stack (stack_pointer_rtx,
9959                                            hard_frame_pointer_rtx,
9960                                            const0_rtx, style, !using_drap);
9961               ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx,
9962                                                red_offset);
9963             }
9964         }
9965     }
9966
9967   if (using_drap)
9968     {
9969       int param_ptr_offset = UNITS_PER_WORD;
9970       rtx insn;
9971
9972       gcc_assert (stack_realign_drap);
9973
9974       if (ix86_static_chain_on_stack)
9975         param_ptr_offset += UNITS_PER_WORD;
9976       if (!call_used_regs[REGNO (crtl->drap_reg)])
9977         param_ptr_offset += UNITS_PER_WORD;
9978
9979       insn = emit_insn (gen_rtx_SET
9980                         (VOIDmode, stack_pointer_rtx,
9981                          gen_rtx_PLUS (Pmode,
9982                                        crtl->drap_reg,
9983                                        GEN_INT (-param_ptr_offset))));
9984       ix86_cfa_state->reg = stack_pointer_rtx;
9985       ix86_cfa_state->offset = param_ptr_offset;
9986
9987       add_reg_note (insn, REG_CFA_DEF_CFA,
9988                     gen_rtx_PLUS (Pmode, ix86_cfa_state->reg,
9989                                   GEN_INT (ix86_cfa_state->offset)));
9990       RTX_FRAME_RELATED_P (insn) = 1;
9991
9992       if (!call_used_regs[REGNO (crtl->drap_reg)])
9993         ix86_emit_restore_reg_using_pop (crtl->drap_reg, -UNITS_PER_WORD);
9994     }
9995
9996   /* Remove the saved static chain from the stack.  The use of ECX is
9997      merely as a scratch register, not as the actual static chain.  */
9998   if (ix86_static_chain_on_stack)
9999     {
10000       rtx r, insn;
10001
10002       gcc_assert (ix86_cfa_state->reg == stack_pointer_rtx);
10003       ix86_cfa_state->offset += UNITS_PER_WORD;
10004
10005       r = gen_rtx_REG (Pmode, CX_REG);
10006       insn = emit_insn (ix86_gen_pop1 (r));
10007
10008       r = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
10009       r = gen_rtx_SET (VOIDmode, stack_pointer_rtx, r);
10010       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
10011       RTX_FRAME_RELATED_P (insn) = 1;
10012     }
10013
10014   /* Sibcall epilogues don't want a return instruction.  */
10015   if (style == 0)
10016     {
10017       *ix86_cfa_state = cfa_state_save;
10018       return;
10019     }
10020
10021   if (crtl->args.pops_args && crtl->args.size)
10022     {
10023       rtx popc = GEN_INT (crtl->args.pops_args);
10024
10025       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
10026          address, do explicit add, and jump indirectly to the caller.  */
10027
10028       if (crtl->args.pops_args >= 65536)
10029         {
10030           rtx ecx = gen_rtx_REG (SImode, CX_REG);
10031           rtx insn;
10032
10033           /* There is no "pascal" calling convention in any 64bit ABI.  */
10034           gcc_assert (!TARGET_64BIT);
10035
10036           insn = emit_insn (gen_popsi1 (ecx));
10037           ix86_cfa_state->offset -= UNITS_PER_WORD;
10038
10039           add_reg_note (insn, REG_CFA_ADJUST_CFA,
10040                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
10041           add_reg_note (insn, REG_CFA_REGISTER,
10042                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
10043           RTX_FRAME_RELATED_P (insn) = 1;
10044
10045           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10046                                      popc, -1, true);
10047           emit_jump_insn (gen_return_indirect_internal (ecx));
10048         }
10049       else
10050         emit_jump_insn (gen_return_pop_internal (popc));
10051     }
10052   else
10053     emit_jump_insn (gen_return_internal ());
10054
10055   /* Restore the state back to the state from the prologue,
10056      so that it's correct for the next epilogue.  */
10057   *ix86_cfa_state = cfa_state_save;
10058 }
10059
10060 /* Reset from the function's potential modifications.  */
10061
10062 static void
10063 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10064                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10065 {
10066   if (pic_offset_table_rtx)
10067     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
10068 #if TARGET_MACHO
10069   /* Mach-O doesn't support labels at the end of objects, so if
10070      it looks like we might want one, insert a NOP.  */
10071   {
10072     rtx insn = get_last_insn ();
10073     while (insn
10074            && NOTE_P (insn)
10075            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
10076       insn = PREV_INSN (insn);
10077     if (insn
10078         && (LABEL_P (insn)
10079             || (NOTE_P (insn)
10080                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
10081       fputs ("\tnop\n", file);
10082   }
10083 #endif
10084
10085 }
10086 \f
10087 /* Extract the parts of an RTL expression that is a valid memory address
10088    for an instruction.  Return 0 if the structure of the address is
10089    grossly off.  Return -1 if the address contains ASHIFT, so it is not
10090    strictly valid, but still used for computing length of lea instruction.  */
10091
10092 int
10093 ix86_decompose_address (rtx addr, struct ix86_address *out)
10094 {
10095   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
10096   rtx base_reg, index_reg;
10097   HOST_WIDE_INT scale = 1;
10098   rtx scale_rtx = NULL_RTX;
10099   rtx tmp;
10100   int retval = 1;
10101   enum ix86_address_seg seg = SEG_DEFAULT;
10102
10103   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
10104     base = addr;
10105   else if (GET_CODE (addr) == PLUS)
10106     {
10107       rtx addends[4], op;
10108       int n = 0, i;
10109
10110       op = addr;
10111       do
10112         {
10113           if (n >= 4)
10114             return 0;
10115           addends[n++] = XEXP (op, 1);
10116           op = XEXP (op, 0);
10117         }
10118       while (GET_CODE (op) == PLUS);
10119       if (n >= 4)
10120         return 0;
10121       addends[n] = op;
10122
10123       for (i = n; i >= 0; --i)
10124         {
10125           op = addends[i];
10126           switch (GET_CODE (op))
10127             {
10128             case MULT:
10129               if (index)
10130                 return 0;
10131               index = XEXP (op, 0);
10132               scale_rtx = XEXP (op, 1);
10133               break;
10134
10135             case ASHIFT:
10136               if (index)
10137                 return 0;
10138               index = XEXP (op, 0);
10139               tmp = XEXP (op, 1);
10140               if (!CONST_INT_P (tmp))
10141                 return 0;
10142               scale = INTVAL (tmp);
10143               if ((unsigned HOST_WIDE_INT) scale > 3)
10144                 return 0;
10145               scale = 1 << scale;
10146               break;
10147
10148             case UNSPEC:
10149               if (XINT (op, 1) == UNSPEC_TP
10150                   && TARGET_TLS_DIRECT_SEG_REFS
10151                   && seg == SEG_DEFAULT)
10152                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
10153               else
10154                 return 0;
10155               break;
10156
10157             case REG:
10158             case SUBREG:
10159               if (!base)
10160                 base = op;
10161               else if (!index)
10162                 index = op;
10163               else
10164                 return 0;
10165               break;
10166
10167             case CONST:
10168             case CONST_INT:
10169             case SYMBOL_REF:
10170             case LABEL_REF:
10171               if (disp)
10172                 return 0;
10173               disp = op;
10174               break;
10175
10176             default:
10177               return 0;
10178             }
10179         }
10180     }
10181   else if (GET_CODE (addr) == MULT)
10182     {
10183       index = XEXP (addr, 0);           /* index*scale */
10184       scale_rtx = XEXP (addr, 1);
10185     }
10186   else if (GET_CODE (addr) == ASHIFT)
10187     {
10188       /* We're called for lea too, which implements ashift on occasion.  */
10189       index = XEXP (addr, 0);
10190       tmp = XEXP (addr, 1);
10191       if (!CONST_INT_P (tmp))
10192         return 0;
10193       scale = INTVAL (tmp);
10194       if ((unsigned HOST_WIDE_INT) scale > 3)
10195         return 0;
10196       scale = 1 << scale;
10197       retval = -1;
10198     }
10199   else
10200     disp = addr;                        /* displacement */
10201
10202   /* Extract the integral value of scale.  */
10203   if (scale_rtx)
10204     {
10205       if (!CONST_INT_P (scale_rtx))
10206         return 0;
10207       scale = INTVAL (scale_rtx);
10208     }
10209
10210   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
10211   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
10212
10213   /* Avoid useless 0 displacement.  */
10214   if (disp == const0_rtx && (base || index))
10215     disp = NULL_RTX;
10216
10217   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
10218   if (base_reg && index_reg && scale == 1
10219       && (index_reg == arg_pointer_rtx
10220           || index_reg == frame_pointer_rtx
10221           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
10222     {
10223       rtx tmp;
10224       tmp = base, base = index, index = tmp;
10225       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
10226     }
10227
10228   /* Special case: %ebp cannot be encoded as a base without a displacement.
10229      Similarly %r13.  */
10230   if (!disp
10231       && base_reg
10232       && (base_reg == hard_frame_pointer_rtx
10233           || base_reg == frame_pointer_rtx
10234           || base_reg == arg_pointer_rtx
10235           || (REG_P (base_reg)
10236               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
10237                   || REGNO (base_reg) == R13_REG))))
10238     disp = const0_rtx;
10239
10240   /* Special case: on K6, [%esi] makes the instruction vector decoded.
10241      Avoid this by transforming to [%esi+0].
10242      Reload calls address legitimization without cfun defined, so we need
10243      to test cfun for being non-NULL. */
10244   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
10245       && base_reg && !index_reg && !disp
10246       && REG_P (base_reg)
10247       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
10248     disp = const0_rtx;
10249
10250   /* Special case: encode reg+reg instead of reg*2.  */
10251   if (!base && index && scale == 2)
10252     base = index, base_reg = index_reg, scale = 1;
10253
10254   /* Special case: scaling cannot be encoded without base or displacement.  */
10255   if (!base && !disp && index && scale != 1)
10256     disp = const0_rtx;
10257
10258   out->base = base;
10259   out->index = index;
10260   out->disp = disp;
10261   out->scale = scale;
10262   out->seg = seg;
10263
10264   return retval;
10265 }
10266 \f
10267 /* Return cost of the memory address x.
10268    For i386, it is better to use a complex address than let gcc copy
10269    the address into a reg and make a new pseudo.  But not if the address
10270    requires to two regs - that would mean more pseudos with longer
10271    lifetimes.  */
10272 static int
10273 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
10274 {
10275   struct ix86_address parts;
10276   int cost = 1;
10277   int ok = ix86_decompose_address (x, &parts);
10278
10279   gcc_assert (ok);
10280
10281   if (parts.base && GET_CODE (parts.base) == SUBREG)
10282     parts.base = SUBREG_REG (parts.base);
10283   if (parts.index && GET_CODE (parts.index) == SUBREG)
10284     parts.index = SUBREG_REG (parts.index);
10285
10286   /* Attempt to minimize number of registers in the address.  */
10287   if ((parts.base
10288        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
10289       || (parts.index
10290           && (!REG_P (parts.index)
10291               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
10292     cost++;
10293
10294   if (parts.base
10295       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
10296       && parts.index
10297       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
10298       && parts.base != parts.index)
10299     cost++;
10300
10301   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
10302      since it's predecode logic can't detect the length of instructions
10303      and it degenerates to vector decoded.  Increase cost of such
10304      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
10305      to split such addresses or even refuse such addresses at all.
10306
10307      Following addressing modes are affected:
10308       [base+scale*index]
10309       [scale*index+disp]
10310       [base+index]
10311
10312      The first and last case  may be avoidable by explicitly coding the zero in
10313      memory address, but I don't have AMD-K6 machine handy to check this
10314      theory.  */
10315
10316   if (TARGET_K6
10317       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
10318           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
10319           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
10320     cost += 10;
10321
10322   return cost;
10323 }
10324 \f
10325 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
10326    this is used for to form addresses to local data when -fPIC is in
10327    use.  */
10328
10329 static bool
10330 darwin_local_data_pic (rtx disp)
10331 {
10332   return (GET_CODE (disp) == UNSPEC
10333           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
10334 }
10335
10336 /* Determine if a given RTX is a valid constant.  We already know this
10337    satisfies CONSTANT_P.  */
10338
10339 bool
10340 legitimate_constant_p (rtx x)
10341 {
10342   switch (GET_CODE (x))
10343     {
10344     case CONST:
10345       x = XEXP (x, 0);
10346
10347       if (GET_CODE (x) == PLUS)
10348         {
10349           if (!CONST_INT_P (XEXP (x, 1)))
10350             return false;
10351           x = XEXP (x, 0);
10352         }
10353
10354       if (TARGET_MACHO && darwin_local_data_pic (x))
10355         return true;
10356
10357       /* Only some unspecs are valid as "constants".  */
10358       if (GET_CODE (x) == UNSPEC)
10359         switch (XINT (x, 1))
10360           {
10361           case UNSPEC_GOT:
10362           case UNSPEC_GOTOFF:
10363           case UNSPEC_PLTOFF:
10364             return TARGET_64BIT;
10365           case UNSPEC_TPOFF:
10366           case UNSPEC_NTPOFF:
10367             x = XVECEXP (x, 0, 0);
10368             return (GET_CODE (x) == SYMBOL_REF
10369                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
10370           case UNSPEC_DTPOFF:
10371             x = XVECEXP (x, 0, 0);
10372             return (GET_CODE (x) == SYMBOL_REF
10373                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
10374           default:
10375             return false;
10376           }
10377
10378       /* We must have drilled down to a symbol.  */
10379       if (GET_CODE (x) == LABEL_REF)
10380         return true;
10381       if (GET_CODE (x) != SYMBOL_REF)
10382         return false;
10383       /* FALLTHRU */
10384
10385     case SYMBOL_REF:
10386       /* TLS symbols are never valid.  */
10387       if (SYMBOL_REF_TLS_MODEL (x))
10388         return false;
10389
10390       /* DLLIMPORT symbols are never valid.  */
10391       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
10392           && SYMBOL_REF_DLLIMPORT_P (x))
10393         return false;
10394       break;
10395
10396     case CONST_DOUBLE:
10397       if (GET_MODE (x) == TImode
10398           && x != CONST0_RTX (TImode)
10399           && !TARGET_64BIT)
10400         return false;
10401       break;
10402
10403     case CONST_VECTOR:
10404       if (!standard_sse_constant_p (x))
10405         return false;
10406
10407     default:
10408       break;
10409     }
10410
10411   /* Otherwise we handle everything else in the move patterns.  */
10412   return true;
10413 }
10414
10415 /* Determine if it's legal to put X into the constant pool.  This
10416    is not possible for the address of thread-local symbols, which
10417    is checked above.  */
10418
10419 static bool
10420 ix86_cannot_force_const_mem (rtx x)
10421 {
10422   /* We can always put integral constants and vectors in memory.  */
10423   switch (GET_CODE (x))
10424     {
10425     case CONST_INT:
10426     case CONST_DOUBLE:
10427     case CONST_VECTOR:
10428       return false;
10429
10430     default:
10431       break;
10432     }
10433   return !legitimate_constant_p (x);
10434 }
10435
10436
10437 /* Nonzero if the constant value X is a legitimate general operand
10438    when generating PIC code.  It is given that flag_pic is on and
10439    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
10440
10441 bool
10442 legitimate_pic_operand_p (rtx x)
10443 {
10444   rtx inner;
10445
10446   switch (GET_CODE (x))
10447     {
10448     case CONST:
10449       inner = XEXP (x, 0);
10450       if (GET_CODE (inner) == PLUS
10451           && CONST_INT_P (XEXP (inner, 1)))
10452         inner = XEXP (inner, 0);
10453
10454       /* Only some unspecs are valid as "constants".  */
10455       if (GET_CODE (inner) == UNSPEC)
10456         switch (XINT (inner, 1))
10457           {
10458           case UNSPEC_GOT:
10459           case UNSPEC_GOTOFF:
10460           case UNSPEC_PLTOFF:
10461             return TARGET_64BIT;
10462           case UNSPEC_TPOFF:
10463             x = XVECEXP (inner, 0, 0);
10464             return (GET_CODE (x) == SYMBOL_REF
10465                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
10466           case UNSPEC_MACHOPIC_OFFSET:
10467             return legitimate_pic_address_disp_p (x);
10468           default:
10469             return false;
10470           }
10471       /* FALLTHRU */
10472
10473     case SYMBOL_REF:
10474     case LABEL_REF:
10475       return legitimate_pic_address_disp_p (x);
10476
10477     default:
10478       return true;
10479     }
10480 }
10481
10482 /* Determine if a given CONST RTX is a valid memory displacement
10483    in PIC mode.  */
10484
10485 int
10486 legitimate_pic_address_disp_p (rtx disp)
10487 {
10488   bool saw_plus;
10489
10490   /* In 64bit mode we can allow direct addresses of symbols and labels
10491      when they are not dynamic symbols.  */
10492   if (TARGET_64BIT)
10493     {
10494       rtx op0 = disp, op1;
10495
10496       switch (GET_CODE (disp))
10497         {
10498         case LABEL_REF:
10499           return true;
10500
10501         case CONST:
10502           if (GET_CODE (XEXP (disp, 0)) != PLUS)
10503             break;
10504           op0 = XEXP (XEXP (disp, 0), 0);
10505           op1 = XEXP (XEXP (disp, 0), 1);
10506           if (!CONST_INT_P (op1)
10507               || INTVAL (op1) >= 16*1024*1024
10508               || INTVAL (op1) < -16*1024*1024)
10509             break;
10510           if (GET_CODE (op0) == LABEL_REF)
10511             return true;
10512           if (GET_CODE (op0) != SYMBOL_REF)
10513             break;
10514           /* FALLTHRU */
10515
10516         case SYMBOL_REF:
10517           /* TLS references should always be enclosed in UNSPEC.  */
10518           if (SYMBOL_REF_TLS_MODEL (op0))
10519             return false;
10520           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
10521               && ix86_cmodel != CM_LARGE_PIC)
10522             return true;
10523           break;
10524
10525         default:
10526           break;
10527         }
10528     }
10529   if (GET_CODE (disp) != CONST)
10530     return 0;
10531   disp = XEXP (disp, 0);
10532
10533   if (TARGET_64BIT)
10534     {
10535       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
10536          of GOT tables.  We should not need these anyway.  */
10537       if (GET_CODE (disp) != UNSPEC
10538           || (XINT (disp, 1) != UNSPEC_GOTPCREL
10539               && XINT (disp, 1) != UNSPEC_GOTOFF
10540               && XINT (disp, 1) != UNSPEC_PLTOFF))
10541         return 0;
10542
10543       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
10544           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
10545         return 0;
10546       return 1;
10547     }
10548
10549   saw_plus = false;
10550   if (GET_CODE (disp) == PLUS)
10551     {
10552       if (!CONST_INT_P (XEXP (disp, 1)))
10553         return 0;
10554       disp = XEXP (disp, 0);
10555       saw_plus = true;
10556     }
10557
10558   if (TARGET_MACHO && darwin_local_data_pic (disp))
10559     return 1;
10560
10561   if (GET_CODE (disp) != UNSPEC)
10562     return 0;
10563
10564   switch (XINT (disp, 1))
10565     {
10566     case UNSPEC_GOT:
10567       if (saw_plus)
10568         return false;
10569       /* We need to check for both symbols and labels because VxWorks loads
10570          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
10571          details.  */
10572       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
10573               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
10574     case UNSPEC_GOTOFF:
10575       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
10576          While ABI specify also 32bit relocation but we don't produce it in
10577          small PIC model at all.  */
10578       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
10579            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
10580           && !TARGET_64BIT)
10581         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
10582       return false;
10583     case UNSPEC_GOTTPOFF:
10584     case UNSPEC_GOTNTPOFF:
10585     case UNSPEC_INDNTPOFF:
10586       if (saw_plus)
10587         return false;
10588       disp = XVECEXP (disp, 0, 0);
10589       return (GET_CODE (disp) == SYMBOL_REF
10590               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
10591     case UNSPEC_NTPOFF:
10592       disp = XVECEXP (disp, 0, 0);
10593       return (GET_CODE (disp) == SYMBOL_REF
10594               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
10595     case UNSPEC_DTPOFF:
10596       disp = XVECEXP (disp, 0, 0);
10597       return (GET_CODE (disp) == SYMBOL_REF
10598               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
10599     }
10600
10601   return 0;
10602 }
10603
10604 /* Recognizes RTL expressions that are valid memory addresses for an
10605    instruction.  The MODE argument is the machine mode for the MEM
10606    expression that wants to use this address.
10607
10608    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
10609    convert common non-canonical forms to canonical form so that they will
10610    be recognized.  */
10611
10612 static bool
10613 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
10614                            rtx addr, bool strict)
10615 {
10616   struct ix86_address parts;
10617   rtx base, index, disp;
10618   HOST_WIDE_INT scale;
10619
10620   if (ix86_decompose_address (addr, &parts) <= 0)
10621     /* Decomposition failed.  */
10622     return false;
10623
10624   base = parts.base;
10625   index = parts.index;
10626   disp = parts.disp;
10627   scale = parts.scale;
10628
10629   /* Validate base register.
10630
10631      Don't allow SUBREG's that span more than a word here.  It can lead to spill
10632      failures when the base is one word out of a two word structure, which is
10633      represented internally as a DImode int.  */
10634
10635   if (base)
10636     {
10637       rtx reg;
10638
10639       if (REG_P (base))
10640         reg = base;
10641       else if (GET_CODE (base) == SUBREG
10642                && REG_P (SUBREG_REG (base))
10643                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
10644                   <= UNITS_PER_WORD)
10645         reg = SUBREG_REG (base);
10646       else
10647         /* Base is not a register.  */
10648         return false;
10649
10650       if (GET_MODE (base) != Pmode)
10651         /* Base is not in Pmode.  */
10652         return false;
10653
10654       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
10655           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
10656         /* Base is not valid.  */
10657         return false;
10658     }
10659
10660   /* Validate index register.
10661
10662      Don't allow SUBREG's that span more than a word here -- same as above.  */
10663
10664   if (index)
10665     {
10666       rtx reg;
10667
10668       if (REG_P (index))
10669         reg = index;
10670       else if (GET_CODE (index) == SUBREG
10671                && REG_P (SUBREG_REG (index))
10672                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
10673                   <= UNITS_PER_WORD)
10674         reg = SUBREG_REG (index);
10675       else
10676         /* Index is not a register.  */
10677         return false;
10678
10679       if (GET_MODE (index) != Pmode)
10680         /* Index is not in Pmode.  */
10681         return false;
10682
10683       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
10684           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
10685         /* Index is not valid.  */
10686         return false;
10687     }
10688
10689   /* Validate scale factor.  */
10690   if (scale != 1)
10691     {
10692       if (!index)
10693         /* Scale without index.  */
10694         return false;
10695
10696       if (scale != 2 && scale != 4 && scale != 8)
10697         /* Scale is not a valid multiplier.  */
10698         return false;
10699     }
10700
10701   /* Validate displacement.  */
10702   if (disp)
10703     {
10704       if (GET_CODE (disp) == CONST
10705           && GET_CODE (XEXP (disp, 0)) == UNSPEC
10706           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
10707         switch (XINT (XEXP (disp, 0), 1))
10708           {
10709           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
10710              used.  While ABI specify also 32bit relocations, we don't produce
10711              them at all and use IP relative instead.  */
10712           case UNSPEC_GOT:
10713           case UNSPEC_GOTOFF:
10714             gcc_assert (flag_pic);
10715             if (!TARGET_64BIT)
10716               goto is_legitimate_pic;
10717
10718             /* 64bit address unspec.  */
10719             return false;
10720
10721           case UNSPEC_GOTPCREL:
10722             gcc_assert (flag_pic);
10723             goto is_legitimate_pic;
10724
10725           case UNSPEC_GOTTPOFF:
10726           case UNSPEC_GOTNTPOFF:
10727           case UNSPEC_INDNTPOFF:
10728           case UNSPEC_NTPOFF:
10729           case UNSPEC_DTPOFF:
10730             break;
10731
10732           default:
10733             /* Invalid address unspec.  */
10734             return false;
10735           }
10736
10737       else if (SYMBOLIC_CONST (disp)
10738                && (flag_pic
10739                    || (TARGET_MACHO
10740 #if TARGET_MACHO
10741                        && MACHOPIC_INDIRECT
10742                        && !machopic_operand_p (disp)
10743 #endif
10744                )))
10745         {
10746
10747         is_legitimate_pic:
10748           if (TARGET_64BIT && (index || base))
10749             {
10750               /* foo@dtpoff(%rX) is ok.  */
10751               if (GET_CODE (disp) != CONST
10752                   || GET_CODE (XEXP (disp, 0)) != PLUS
10753                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
10754                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
10755                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
10756                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
10757                 /* Non-constant pic memory reference.  */
10758                 return false;
10759             }
10760           else if (! legitimate_pic_address_disp_p (disp))
10761             /* Displacement is an invalid pic construct.  */
10762             return false;
10763
10764           /* This code used to verify that a symbolic pic displacement
10765              includes the pic_offset_table_rtx register.
10766
10767              While this is good idea, unfortunately these constructs may
10768              be created by "adds using lea" optimization for incorrect
10769              code like:
10770
10771              int a;
10772              int foo(int i)
10773                {
10774                  return *(&a+i);
10775                }
10776
10777              This code is nonsensical, but results in addressing
10778              GOT table with pic_offset_table_rtx base.  We can't
10779              just refuse it easily, since it gets matched by
10780              "addsi3" pattern, that later gets split to lea in the
10781              case output register differs from input.  While this
10782              can be handled by separate addsi pattern for this case
10783              that never results in lea, this seems to be easier and
10784              correct fix for crash to disable this test.  */
10785         }
10786       else if (GET_CODE (disp) != LABEL_REF
10787                && !CONST_INT_P (disp)
10788                && (GET_CODE (disp) != CONST
10789                    || !legitimate_constant_p (disp))
10790                && (GET_CODE (disp) != SYMBOL_REF
10791                    || !legitimate_constant_p (disp)))
10792         /* Displacement is not constant.  */
10793         return false;
10794       else if (TARGET_64BIT
10795                && !x86_64_immediate_operand (disp, VOIDmode))
10796         /* Displacement is out of range.  */
10797         return false;
10798     }
10799
10800   /* Everything looks valid.  */
10801   return true;
10802 }
10803
10804 /* Determine if a given RTX is a valid constant address.  */
10805
10806 bool
10807 constant_address_p (rtx x)
10808 {
10809   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
10810 }
10811 \f
10812 /* Return a unique alias set for the GOT.  */
10813
10814 static alias_set_type
10815 ix86_GOT_alias_set (void)
10816 {
10817   static alias_set_type set = -1;
10818   if (set == -1)
10819     set = new_alias_set ();
10820   return set;
10821 }
10822
10823 /* Return a legitimate reference for ORIG (an address) using the
10824    register REG.  If REG is 0, a new pseudo is generated.
10825
10826    There are two types of references that must be handled:
10827
10828    1. Global data references must load the address from the GOT, via
10829       the PIC reg.  An insn is emitted to do this load, and the reg is
10830       returned.
10831
10832    2. Static data references, constant pool addresses, and code labels
10833       compute the address as an offset from the GOT, whose base is in
10834       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
10835       differentiate them from global data objects.  The returned
10836       address is the PIC reg + an unspec constant.
10837
10838    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
10839    reg also appears in the address.  */
10840
10841 static rtx
10842 legitimize_pic_address (rtx orig, rtx reg)
10843 {
10844   rtx addr = orig;
10845   rtx new_rtx = orig;
10846   rtx base;
10847
10848 #if TARGET_MACHO
10849   if (TARGET_MACHO && !TARGET_64BIT)
10850     {
10851       if (reg == 0)
10852         reg = gen_reg_rtx (Pmode);
10853       /* Use the generic Mach-O PIC machinery.  */
10854       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
10855     }
10856 #endif
10857
10858   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
10859     new_rtx = addr;
10860   else if (TARGET_64BIT
10861            && ix86_cmodel != CM_SMALL_PIC
10862            && gotoff_operand (addr, Pmode))
10863     {
10864       rtx tmpreg;
10865       /* This symbol may be referenced via a displacement from the PIC
10866          base address (@GOTOFF).  */
10867
10868       if (reload_in_progress)
10869         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10870       if (GET_CODE (addr) == CONST)
10871         addr = XEXP (addr, 0);
10872       if (GET_CODE (addr) == PLUS)
10873           {
10874             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
10875                                       UNSPEC_GOTOFF);
10876             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
10877           }
10878         else
10879           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
10880       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10881       if (!reg)
10882         tmpreg = gen_reg_rtx (Pmode);
10883       else
10884         tmpreg = reg;
10885       emit_move_insn (tmpreg, new_rtx);
10886
10887       if (reg != 0)
10888         {
10889           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
10890                                          tmpreg, 1, OPTAB_DIRECT);
10891           new_rtx = reg;
10892         }
10893       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
10894     }
10895   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
10896     {
10897       /* This symbol may be referenced via a displacement from the PIC
10898          base address (@GOTOFF).  */
10899
10900       if (reload_in_progress)
10901         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10902       if (GET_CODE (addr) == CONST)
10903         addr = XEXP (addr, 0);
10904       if (GET_CODE (addr) == PLUS)
10905           {
10906             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
10907                                       UNSPEC_GOTOFF);
10908             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
10909           }
10910         else
10911           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
10912       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10913       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
10914
10915       if (reg != 0)
10916         {
10917           emit_move_insn (reg, new_rtx);
10918           new_rtx = reg;
10919         }
10920     }
10921   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
10922            /* We can't use @GOTOFF for text labels on VxWorks;
10923               see gotoff_operand.  */
10924            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
10925     {
10926       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
10927         {
10928           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
10929             return legitimize_dllimport_symbol (addr, true);
10930           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
10931               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
10932               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
10933             {
10934               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
10935               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
10936             }
10937         }
10938
10939       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
10940         {
10941           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
10942           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10943           new_rtx = gen_const_mem (Pmode, new_rtx);
10944           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
10945
10946           if (reg == 0)
10947             reg = gen_reg_rtx (Pmode);
10948           /* Use directly gen_movsi, otherwise the address is loaded
10949              into register for CSE.  We don't want to CSE this addresses,
10950              instead we CSE addresses from the GOT table, so skip this.  */
10951           emit_insn (gen_movsi (reg, new_rtx));
10952           new_rtx = reg;
10953         }
10954       else
10955         {
10956           /* This symbol must be referenced via a load from the
10957              Global Offset Table (@GOT).  */
10958
10959           if (reload_in_progress)
10960             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
10961           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
10962           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
10963           if (TARGET_64BIT)
10964             new_rtx = force_reg (Pmode, new_rtx);
10965           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
10966           new_rtx = gen_const_mem (Pmode, new_rtx);
10967           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
10968
10969           if (reg == 0)
10970             reg = gen_reg_rtx (Pmode);
10971           emit_move_insn (reg, new_rtx);
10972           new_rtx = reg;
10973         }
10974     }
10975   else
10976     {
10977       if (CONST_INT_P (addr)
10978           && !x86_64_immediate_operand (addr, VOIDmode))
10979         {
10980           if (reg)
10981             {
10982               emit_move_insn (reg, addr);
10983               new_rtx = reg;
10984             }
10985           else
10986             new_rtx = force_reg (Pmode, addr);
10987         }
10988       else if (GET_CODE (addr) == CONST)
10989         {
10990           addr = XEXP (addr, 0);
10991
10992           /* We must match stuff we generate before.  Assume the only
10993              unspecs that can get here are ours.  Not that we could do
10994              anything with them anyway....  */
10995           if (GET_CODE (addr) == UNSPEC
10996               || (GET_CODE (addr) == PLUS
10997                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
10998             return orig;
10999           gcc_assert (GET_CODE (addr) == PLUS);
11000         }
11001       if (GET_CODE (addr) == PLUS)
11002         {
11003           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
11004
11005           /* Check first to see if this is a constant offset from a @GOTOFF
11006              symbol reference.  */
11007           if (gotoff_operand (op0, Pmode)
11008               && CONST_INT_P (op1))
11009             {
11010               if (!TARGET_64BIT)
11011                 {
11012                   if (reload_in_progress)
11013                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11014                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
11015                                             UNSPEC_GOTOFF);
11016                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
11017                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11018                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
11019
11020                   if (reg != 0)
11021                     {
11022                       emit_move_insn (reg, new_rtx);
11023                       new_rtx = reg;
11024                     }
11025                 }
11026               else
11027                 {
11028                   if (INTVAL (op1) < -16*1024*1024
11029                       || INTVAL (op1) >= 16*1024*1024)
11030                     {
11031                       if (!x86_64_immediate_operand (op1, Pmode))
11032                         op1 = force_reg (Pmode, op1);
11033                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
11034                     }
11035                 }
11036             }
11037           else
11038             {
11039               base = legitimize_pic_address (XEXP (addr, 0), reg);
11040               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
11041                                                  base == reg ? NULL_RTX : reg);
11042
11043               if (CONST_INT_P (new_rtx))
11044                 new_rtx = plus_constant (base, INTVAL (new_rtx));
11045               else
11046                 {
11047                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
11048                     {
11049                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
11050                       new_rtx = XEXP (new_rtx, 1);
11051                     }
11052                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
11053                 }
11054             }
11055         }
11056     }
11057   return new_rtx;
11058 }
11059 \f
11060 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
11061
11062 static rtx
11063 get_thread_pointer (int to_reg)
11064 {
11065   rtx tp, reg, insn;
11066
11067   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
11068   if (!to_reg)
11069     return tp;
11070
11071   reg = gen_reg_rtx (Pmode);
11072   insn = gen_rtx_SET (VOIDmode, reg, tp);
11073   insn = emit_insn (insn);
11074
11075   return reg;
11076 }
11077
11078 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
11079    false if we expect this to be used for a memory address and true if
11080    we expect to load the address into a register.  */
11081
11082 static rtx
11083 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
11084 {
11085   rtx dest, base, off, pic, tp;
11086   int type;
11087
11088   switch (model)
11089     {
11090     case TLS_MODEL_GLOBAL_DYNAMIC:
11091       dest = gen_reg_rtx (Pmode);
11092       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
11093
11094       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
11095         {
11096           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
11097
11098           start_sequence ();
11099           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
11100           insns = get_insns ();
11101           end_sequence ();
11102
11103           RTL_CONST_CALL_P (insns) = 1;
11104           emit_libcall_block (insns, dest, rax, x);
11105         }
11106       else if (TARGET_64BIT && TARGET_GNU2_TLS)
11107         emit_insn (gen_tls_global_dynamic_64 (dest, x));
11108       else
11109         emit_insn (gen_tls_global_dynamic_32 (dest, x));
11110
11111       if (TARGET_GNU2_TLS)
11112         {
11113           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
11114
11115           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
11116         }
11117       break;
11118
11119     case TLS_MODEL_LOCAL_DYNAMIC:
11120       base = gen_reg_rtx (Pmode);
11121       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
11122
11123       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
11124         {
11125           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
11126
11127           start_sequence ();
11128           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
11129           insns = get_insns ();
11130           end_sequence ();
11131
11132           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
11133           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
11134           RTL_CONST_CALL_P (insns) = 1;
11135           emit_libcall_block (insns, base, rax, note);
11136         }
11137       else if (TARGET_64BIT && TARGET_GNU2_TLS)
11138         emit_insn (gen_tls_local_dynamic_base_64 (base));
11139       else
11140         emit_insn (gen_tls_local_dynamic_base_32 (base));
11141
11142       if (TARGET_GNU2_TLS)
11143         {
11144           rtx x = ix86_tls_module_base ();
11145
11146           set_unique_reg_note (get_last_insn (), REG_EQUIV,
11147                                gen_rtx_MINUS (Pmode, x, tp));
11148         }
11149
11150       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
11151       off = gen_rtx_CONST (Pmode, off);
11152
11153       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
11154
11155       if (TARGET_GNU2_TLS)
11156         {
11157           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
11158
11159           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
11160         }
11161
11162       break;
11163
11164     case TLS_MODEL_INITIAL_EXEC:
11165       if (TARGET_64BIT)
11166         {
11167           pic = NULL;
11168           type = UNSPEC_GOTNTPOFF;
11169         }
11170       else if (flag_pic)
11171         {
11172           if (reload_in_progress)
11173             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11174           pic = pic_offset_table_rtx;
11175           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
11176         }
11177       else if (!TARGET_ANY_GNU_TLS)
11178         {
11179           pic = gen_reg_rtx (Pmode);
11180           emit_insn (gen_set_got (pic));
11181           type = UNSPEC_GOTTPOFF;
11182         }
11183       else
11184         {
11185           pic = NULL;
11186           type = UNSPEC_INDNTPOFF;
11187         }
11188
11189       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
11190       off = gen_rtx_CONST (Pmode, off);
11191       if (pic)
11192         off = gen_rtx_PLUS (Pmode, pic, off);
11193       off = gen_const_mem (Pmode, off);
11194       set_mem_alias_set (off, ix86_GOT_alias_set ());
11195
11196       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11197         {
11198           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
11199           off = force_reg (Pmode, off);
11200           return gen_rtx_PLUS (Pmode, base, off);
11201         }
11202       else
11203         {
11204           base = get_thread_pointer (true);
11205           dest = gen_reg_rtx (Pmode);
11206           emit_insn (gen_subsi3 (dest, base, off));
11207         }
11208       break;
11209
11210     case TLS_MODEL_LOCAL_EXEC:
11211       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
11212                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11213                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
11214       off = gen_rtx_CONST (Pmode, off);
11215
11216       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11217         {
11218           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
11219           return gen_rtx_PLUS (Pmode, base, off);
11220         }
11221       else
11222         {
11223           base = get_thread_pointer (true);
11224           dest = gen_reg_rtx (Pmode);
11225           emit_insn (gen_subsi3 (dest, base, off));
11226         }
11227       break;
11228
11229     default:
11230       gcc_unreachable ();
11231     }
11232
11233   return dest;
11234 }
11235
11236 /* Create or return the unique __imp_DECL dllimport symbol corresponding
11237    to symbol DECL.  */
11238
11239 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
11240   htab_t dllimport_map;
11241
11242 static tree
11243 get_dllimport_decl (tree decl)
11244 {
11245   struct tree_map *h, in;
11246   void **loc;
11247   const char *name;
11248   const char *prefix;
11249   size_t namelen, prefixlen;
11250   char *imp_name;
11251   tree to;
11252   rtx rtl;
11253
11254   if (!dllimport_map)
11255     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
11256
11257   in.hash = htab_hash_pointer (decl);
11258   in.base.from = decl;
11259   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
11260   h = (struct tree_map *) *loc;
11261   if (h)
11262     return h->to;
11263
11264   *loc = h = ggc_alloc_tree_map ();
11265   h->hash = in.hash;
11266   h->base.from = decl;
11267   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
11268                            VAR_DECL, NULL, ptr_type_node);
11269   DECL_ARTIFICIAL (to) = 1;
11270   DECL_IGNORED_P (to) = 1;
11271   DECL_EXTERNAL (to) = 1;
11272   TREE_READONLY (to) = 1;
11273
11274   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11275   name = targetm.strip_name_encoding (name);
11276   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
11277     ? "*__imp_" : "*__imp__";
11278   namelen = strlen (name);
11279   prefixlen = strlen (prefix);
11280   imp_name = (char *) alloca (namelen + prefixlen + 1);
11281   memcpy (imp_name, prefix, prefixlen);
11282   memcpy (imp_name + prefixlen, name, namelen + 1);
11283
11284   name = ggc_alloc_string (imp_name, namelen + prefixlen);
11285   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
11286   SET_SYMBOL_REF_DECL (rtl, to);
11287   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
11288
11289   rtl = gen_const_mem (Pmode, rtl);
11290   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
11291
11292   SET_DECL_RTL (to, rtl);
11293   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
11294
11295   return to;
11296 }
11297
11298 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
11299    true if we require the result be a register.  */
11300
11301 static rtx
11302 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
11303 {
11304   tree imp_decl;
11305   rtx x;
11306
11307   gcc_assert (SYMBOL_REF_DECL (symbol));
11308   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
11309
11310   x = DECL_RTL (imp_decl);
11311   if (want_reg)
11312     x = force_reg (Pmode, x);
11313   return x;
11314 }
11315
11316 /* Try machine-dependent ways of modifying an illegitimate address
11317    to be legitimate.  If we find one, return the new, valid address.
11318    This macro is used in only one place: `memory_address' in explow.c.
11319
11320    OLDX is the address as it was before break_out_memory_refs was called.
11321    In some cases it is useful to look at this to decide what needs to be done.
11322
11323    It is always safe for this macro to do nothing.  It exists to recognize
11324    opportunities to optimize the output.
11325
11326    For the 80386, we handle X+REG by loading X into a register R and
11327    using R+REG.  R will go in a general reg and indexing will be used.
11328    However, if REG is a broken-out memory address or multiplication,
11329    nothing needs to be done because REG can certainly go in a general reg.
11330
11331    When -fpic is used, special handling is needed for symbolic references.
11332    See comments by legitimize_pic_address in i386.c for details.  */
11333
11334 static rtx
11335 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
11336                          enum machine_mode mode)
11337 {
11338   int changed = 0;
11339   unsigned log;
11340
11341   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
11342   if (log)
11343     return legitimize_tls_address (x, (enum tls_model) log, false);
11344   if (GET_CODE (x) == CONST
11345       && GET_CODE (XEXP (x, 0)) == PLUS
11346       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
11347       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
11348     {
11349       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
11350                                       (enum tls_model) log, false);
11351       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
11352     }
11353
11354   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
11355     {
11356       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
11357         return legitimize_dllimport_symbol (x, true);
11358       if (GET_CODE (x) == CONST
11359           && GET_CODE (XEXP (x, 0)) == PLUS
11360           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
11361           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
11362         {
11363           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
11364           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
11365         }
11366     }
11367
11368   if (flag_pic && SYMBOLIC_CONST (x))
11369     return legitimize_pic_address (x, 0);
11370
11371   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
11372   if (GET_CODE (x) == ASHIFT
11373       && CONST_INT_P (XEXP (x, 1))
11374       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
11375     {
11376       changed = 1;
11377       log = INTVAL (XEXP (x, 1));
11378       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
11379                         GEN_INT (1 << log));
11380     }
11381
11382   if (GET_CODE (x) == PLUS)
11383     {
11384       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
11385
11386       if (GET_CODE (XEXP (x, 0)) == ASHIFT
11387           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
11388           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
11389         {
11390           changed = 1;
11391           log = INTVAL (XEXP (XEXP (x, 0), 1));
11392           XEXP (x, 0) = gen_rtx_MULT (Pmode,
11393                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
11394                                       GEN_INT (1 << log));
11395         }
11396
11397       if (GET_CODE (XEXP (x, 1)) == ASHIFT
11398           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
11399           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
11400         {
11401           changed = 1;
11402           log = INTVAL (XEXP (XEXP (x, 1), 1));
11403           XEXP (x, 1) = gen_rtx_MULT (Pmode,
11404                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
11405                                       GEN_INT (1 << log));
11406         }
11407
11408       /* Put multiply first if it isn't already.  */
11409       if (GET_CODE (XEXP (x, 1)) == MULT)
11410         {
11411           rtx tmp = XEXP (x, 0);
11412           XEXP (x, 0) = XEXP (x, 1);
11413           XEXP (x, 1) = tmp;
11414           changed = 1;
11415         }
11416
11417       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
11418          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
11419          created by virtual register instantiation, register elimination, and
11420          similar optimizations.  */
11421       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
11422         {
11423           changed = 1;
11424           x = gen_rtx_PLUS (Pmode,
11425                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
11426                                           XEXP (XEXP (x, 1), 0)),
11427                             XEXP (XEXP (x, 1), 1));
11428         }
11429
11430       /* Canonicalize
11431          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
11432          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
11433       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
11434                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
11435                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
11436                && CONSTANT_P (XEXP (x, 1)))
11437         {
11438           rtx constant;
11439           rtx other = NULL_RTX;
11440
11441           if (CONST_INT_P (XEXP (x, 1)))
11442             {
11443               constant = XEXP (x, 1);
11444               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
11445             }
11446           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
11447             {
11448               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
11449               other = XEXP (x, 1);
11450             }
11451           else
11452             constant = 0;
11453
11454           if (constant)
11455             {
11456               changed = 1;
11457               x = gen_rtx_PLUS (Pmode,
11458                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
11459                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
11460                                 plus_constant (other, INTVAL (constant)));
11461             }
11462         }
11463
11464       if (changed && ix86_legitimate_address_p (mode, x, FALSE))
11465         return x;
11466
11467       if (GET_CODE (XEXP (x, 0)) == MULT)
11468         {
11469           changed = 1;
11470           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
11471         }
11472
11473       if (GET_CODE (XEXP (x, 1)) == MULT)
11474         {
11475           changed = 1;
11476           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
11477         }
11478
11479       if (changed
11480           && REG_P (XEXP (x, 1))
11481           && REG_P (XEXP (x, 0)))
11482         return x;
11483
11484       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
11485         {
11486           changed = 1;
11487           x = legitimize_pic_address (x, 0);
11488         }
11489
11490       if (changed && ix86_legitimate_address_p (mode, x, FALSE))
11491         return x;
11492
11493       if (REG_P (XEXP (x, 0)))
11494         {
11495           rtx temp = gen_reg_rtx (Pmode);
11496           rtx val  = force_operand (XEXP (x, 1), temp);
11497           if (val != temp)
11498             emit_move_insn (temp, val);
11499
11500           XEXP (x, 1) = temp;
11501           return x;
11502         }
11503
11504       else if (REG_P (XEXP (x, 1)))
11505         {
11506           rtx temp = gen_reg_rtx (Pmode);
11507           rtx val  = force_operand (XEXP (x, 0), temp);
11508           if (val != temp)
11509             emit_move_insn (temp, val);
11510
11511           XEXP (x, 0) = temp;
11512           return x;
11513         }
11514     }
11515
11516   return x;
11517 }
11518 \f
11519 /* Print an integer constant expression in assembler syntax.  Addition
11520    and subtraction are the only arithmetic that may appear in these
11521    expressions.  FILE is the stdio stream to write to, X is the rtx, and
11522    CODE is the operand print code from the output string.  */
11523
11524 static void
11525 output_pic_addr_const (FILE *file, rtx x, int code)
11526 {
11527   char buf[256];
11528
11529   switch (GET_CODE (x))
11530     {
11531     case PC:
11532       gcc_assert (flag_pic);
11533       putc ('.', file);
11534       break;
11535
11536     case SYMBOL_REF:
11537       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
11538         output_addr_const (file, x);
11539       else
11540         {
11541           const char *name = XSTR (x, 0);
11542
11543           /* Mark the decl as referenced so that cgraph will
11544              output the function.  */
11545           if (SYMBOL_REF_DECL (x))
11546             mark_decl_referenced (SYMBOL_REF_DECL (x));
11547
11548 #if TARGET_MACHO
11549           if (MACHOPIC_INDIRECT
11550               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
11551             name = machopic_indirection_name (x, /*stub_p=*/true);
11552 #endif
11553           assemble_name (file, name);
11554         }
11555       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
11556           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
11557         fputs ("@PLT", file);
11558       break;
11559
11560     case LABEL_REF:
11561       x = XEXP (x, 0);
11562       /* FALLTHRU */
11563     case CODE_LABEL:
11564       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
11565       assemble_name (asm_out_file, buf);
11566       break;
11567
11568     case CONST_INT:
11569       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
11570       break;
11571
11572     case CONST:
11573       /* This used to output parentheses around the expression,
11574          but that does not work on the 386 (either ATT or BSD assembler).  */
11575       output_pic_addr_const (file, XEXP (x, 0), code);
11576       break;
11577
11578     case CONST_DOUBLE:
11579       if (GET_MODE (x) == VOIDmode)
11580         {
11581           /* We can use %d if the number is <32 bits and positive.  */
11582           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
11583             fprintf (file, "0x%lx%08lx",
11584                      (unsigned long) CONST_DOUBLE_HIGH (x),
11585                      (unsigned long) CONST_DOUBLE_LOW (x));
11586           else
11587             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
11588         }
11589       else
11590         /* We can't handle floating point constants;
11591            TARGET_PRINT_OPERAND must handle them.  */
11592         output_operand_lossage ("floating constant misused");
11593       break;
11594
11595     case PLUS:
11596       /* Some assemblers need integer constants to appear first.  */
11597       if (CONST_INT_P (XEXP (x, 0)))
11598         {
11599           output_pic_addr_const (file, XEXP (x, 0), code);
11600           putc ('+', file);
11601           output_pic_addr_const (file, XEXP (x, 1), code);
11602         }
11603       else
11604         {
11605           gcc_assert (CONST_INT_P (XEXP (x, 1)));
11606           output_pic_addr_const (file, XEXP (x, 1), code);
11607           putc ('+', file);
11608           output_pic_addr_const (file, XEXP (x, 0), code);
11609         }
11610       break;
11611
11612     case MINUS:
11613       if (!TARGET_MACHO)
11614         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
11615       output_pic_addr_const (file, XEXP (x, 0), code);
11616       putc ('-', file);
11617       output_pic_addr_const (file, XEXP (x, 1), code);
11618       if (!TARGET_MACHO)
11619         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
11620       break;
11621
11622      case UNSPEC:
11623        gcc_assert (XVECLEN (x, 0) == 1);
11624        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
11625        switch (XINT (x, 1))
11626         {
11627         case UNSPEC_GOT:
11628           fputs ("@GOT", file);
11629           break;
11630         case UNSPEC_GOTOFF:
11631           fputs ("@GOTOFF", file);
11632           break;
11633         case UNSPEC_PLTOFF:
11634           fputs ("@PLTOFF", file);
11635           break;
11636         case UNSPEC_GOTPCREL:
11637           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
11638                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
11639           break;
11640         case UNSPEC_GOTTPOFF:
11641           /* FIXME: This might be @TPOFF in Sun ld too.  */
11642           fputs ("@gottpoff", file);
11643           break;
11644         case UNSPEC_TPOFF:
11645           fputs ("@tpoff", file);
11646           break;
11647         case UNSPEC_NTPOFF:
11648           if (TARGET_64BIT)
11649             fputs ("@tpoff", file);
11650           else
11651             fputs ("@ntpoff", file);
11652           break;
11653         case UNSPEC_DTPOFF:
11654           fputs ("@dtpoff", file);
11655           break;
11656         case UNSPEC_GOTNTPOFF:
11657           if (TARGET_64BIT)
11658             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
11659                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
11660           else
11661             fputs ("@gotntpoff", file);
11662           break;
11663         case UNSPEC_INDNTPOFF:
11664           fputs ("@indntpoff", file);
11665           break;
11666 #if TARGET_MACHO
11667         case UNSPEC_MACHOPIC_OFFSET:
11668           putc ('-', file);
11669           machopic_output_function_base_name (file);
11670           break;
11671 #endif
11672         default:
11673           output_operand_lossage ("invalid UNSPEC as operand");
11674           break;
11675         }
11676        break;
11677
11678     default:
11679       output_operand_lossage ("invalid expression as operand");
11680     }
11681 }
11682
11683 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
11684    We need to emit DTP-relative relocations.  */
11685
11686 static void ATTRIBUTE_UNUSED
11687 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
11688 {
11689   fputs (ASM_LONG, file);
11690   output_addr_const (file, x);
11691   fputs ("@dtpoff", file);
11692   switch (size)
11693     {
11694     case 4:
11695       break;
11696     case 8:
11697       fputs (", 0", file);
11698       break;
11699     default:
11700       gcc_unreachable ();
11701    }
11702 }
11703
11704 /* Return true if X is a representation of the PIC register.  This copes
11705    with calls from ix86_find_base_term, where the register might have
11706    been replaced by a cselib value.  */
11707
11708 static bool
11709 ix86_pic_register_p (rtx x)
11710 {
11711   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
11712     return (pic_offset_table_rtx
11713             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
11714   else
11715     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
11716 }
11717
11718 /* In the name of slightly smaller debug output, and to cater to
11719    general assembler lossage, recognize PIC+GOTOFF and turn it back
11720    into a direct symbol reference.
11721
11722    On Darwin, this is necessary to avoid a crash, because Darwin
11723    has a different PIC label for each routine but the DWARF debugging
11724    information is not associated with any particular routine, so it's
11725    necessary to remove references to the PIC label from RTL stored by
11726    the DWARF output code.  */
11727
11728 static rtx
11729 ix86_delegitimize_address (rtx x)
11730 {
11731   rtx orig_x = delegitimize_mem_from_attrs (x);
11732   /* addend is NULL or some rtx if x is something+GOTOFF where
11733      something doesn't include the PIC register.  */
11734   rtx addend = NULL_RTX;
11735   /* reg_addend is NULL or a multiple of some register.  */
11736   rtx reg_addend = NULL_RTX;
11737   /* const_addend is NULL or a const_int.  */
11738   rtx const_addend = NULL_RTX;
11739   /* This is the result, or NULL.  */
11740   rtx result = NULL_RTX;
11741
11742   x = orig_x;
11743
11744   if (MEM_P (x))
11745     x = XEXP (x, 0);
11746
11747   if (TARGET_64BIT)
11748     {
11749       if (GET_CODE (x) != CONST
11750           || GET_CODE (XEXP (x, 0)) != UNSPEC
11751           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
11752           || !MEM_P (orig_x))
11753         return orig_x;
11754       x = XVECEXP (XEXP (x, 0), 0, 0);
11755       if (GET_MODE (orig_x) != Pmode)
11756         return simplify_gen_subreg (GET_MODE (orig_x), x, Pmode, 0);
11757       return x;
11758     }
11759
11760   if (GET_CODE (x) != PLUS
11761       || GET_CODE (XEXP (x, 1)) != CONST)
11762     return orig_x;
11763
11764   if (ix86_pic_register_p (XEXP (x, 0)))
11765     /* %ebx + GOT/GOTOFF */
11766     ;
11767   else if (GET_CODE (XEXP (x, 0)) == PLUS)
11768     {
11769       /* %ebx + %reg * scale + GOT/GOTOFF */
11770       reg_addend = XEXP (x, 0);
11771       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
11772         reg_addend = XEXP (reg_addend, 1);
11773       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
11774         reg_addend = XEXP (reg_addend, 0);
11775       else
11776         {
11777           reg_addend = NULL_RTX;
11778           addend = XEXP (x, 0);
11779         }
11780     }
11781   else
11782     addend = XEXP (x, 0);
11783
11784   x = XEXP (XEXP (x, 1), 0);
11785   if (GET_CODE (x) == PLUS
11786       && CONST_INT_P (XEXP (x, 1)))
11787     {
11788       const_addend = XEXP (x, 1);
11789       x = XEXP (x, 0);
11790     }
11791
11792   if (GET_CODE (x) == UNSPEC
11793       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
11794           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
11795     result = XVECEXP (x, 0, 0);
11796
11797   if (TARGET_MACHO && darwin_local_data_pic (x)
11798       && !MEM_P (orig_x))
11799     result = XVECEXP (x, 0, 0);
11800
11801   if (! result)
11802     return orig_x;
11803
11804   if (const_addend)
11805     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
11806   if (reg_addend)
11807     result = gen_rtx_PLUS (Pmode, reg_addend, result);
11808   if (addend)
11809     {
11810       /* If the rest of original X doesn't involve the PIC register, add
11811          addend and subtract pic_offset_table_rtx.  This can happen e.g.
11812          for code like:
11813          leal (%ebx, %ecx, 4), %ecx
11814          ...
11815          movl foo@GOTOFF(%ecx), %edx
11816          in which case we return (%ecx - %ebx) + foo.  */
11817       if (pic_offset_table_rtx)
11818         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
11819                                                      pic_offset_table_rtx),
11820                                result);
11821       else
11822         return orig_x;
11823     }
11824   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
11825     return simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
11826   return result;
11827 }
11828
11829 /* If X is a machine specific address (i.e. a symbol or label being
11830    referenced as a displacement from the GOT implemented using an
11831    UNSPEC), then return the base term.  Otherwise return X.  */
11832
11833 rtx
11834 ix86_find_base_term (rtx x)
11835 {
11836   rtx term;
11837
11838   if (TARGET_64BIT)
11839     {
11840       if (GET_CODE (x) != CONST)
11841         return x;
11842       term = XEXP (x, 0);
11843       if (GET_CODE (term) == PLUS
11844           && (CONST_INT_P (XEXP (term, 1))
11845               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
11846         term = XEXP (term, 0);
11847       if (GET_CODE (term) != UNSPEC
11848           || XINT (term, 1) != UNSPEC_GOTPCREL)
11849         return x;
11850
11851       return XVECEXP (term, 0, 0);
11852     }
11853
11854   return ix86_delegitimize_address (x);
11855 }
11856 \f
11857 static void
11858 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
11859                     int fp, FILE *file)
11860 {
11861   const char *suffix;
11862
11863   if (mode == CCFPmode || mode == CCFPUmode)
11864     {
11865       code = ix86_fp_compare_code_to_integer (code);
11866       mode = CCmode;
11867     }
11868   if (reverse)
11869     code = reverse_condition (code);
11870
11871   switch (code)
11872     {
11873     case EQ:
11874       switch (mode)
11875         {
11876         case CCAmode:
11877           suffix = "a";
11878           break;
11879
11880         case CCCmode:
11881           suffix = "c";
11882           break;
11883
11884         case CCOmode:
11885           suffix = "o";
11886           break;
11887
11888         case CCSmode:
11889           suffix = "s";
11890           break;
11891
11892         default:
11893           suffix = "e";
11894         }
11895       break;
11896     case NE:
11897       switch (mode)
11898         {
11899         case CCAmode:
11900           suffix = "na";
11901           break;
11902
11903         case CCCmode:
11904           suffix = "nc";
11905           break;
11906
11907         case CCOmode:
11908           suffix = "no";
11909           break;
11910
11911         case CCSmode:
11912           suffix = "ns";
11913           break;
11914
11915         default:
11916           suffix = "ne";
11917         }
11918       break;
11919     case GT:
11920       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
11921       suffix = "g";
11922       break;
11923     case GTU:
11924       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
11925          Those same assemblers have the same but opposite lossage on cmov.  */
11926       if (mode == CCmode)
11927         suffix = fp ? "nbe" : "a";
11928       else if (mode == CCCmode)
11929         suffix = "b";
11930       else
11931         gcc_unreachable ();
11932       break;
11933     case LT:
11934       switch (mode)
11935         {
11936         case CCNOmode:
11937         case CCGOCmode:
11938           suffix = "s";
11939           break;
11940
11941         case CCmode:
11942         case CCGCmode:
11943           suffix = "l";
11944           break;
11945
11946         default:
11947           gcc_unreachable ();
11948         }
11949       break;
11950     case LTU:
11951       gcc_assert (mode == CCmode || mode == CCCmode);
11952       suffix = "b";
11953       break;
11954     case GE:
11955       switch (mode)
11956         {
11957         case CCNOmode:
11958         case CCGOCmode:
11959           suffix = "ns";
11960           break;
11961
11962         case CCmode:
11963         case CCGCmode:
11964           suffix = "ge";
11965           break;
11966
11967         default:
11968           gcc_unreachable ();
11969         }
11970       break;
11971     case GEU:
11972       /* ??? As above.  */
11973       gcc_assert (mode == CCmode || mode == CCCmode);
11974       suffix = fp ? "nb" : "ae";
11975       break;
11976     case LE:
11977       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
11978       suffix = "le";
11979       break;
11980     case LEU:
11981       /* ??? As above.  */
11982       if (mode == CCmode)
11983         suffix = "be";
11984       else if (mode == CCCmode)
11985         suffix = fp ? "nb" : "ae";
11986       else
11987         gcc_unreachable ();
11988       break;
11989     case UNORDERED:
11990       suffix = fp ? "u" : "p";
11991       break;
11992     case ORDERED:
11993       suffix = fp ? "nu" : "np";
11994       break;
11995     default:
11996       gcc_unreachable ();
11997     }
11998   fputs (suffix, file);
11999 }
12000
12001 /* Print the name of register X to FILE based on its machine mode and number.
12002    If CODE is 'w', pretend the mode is HImode.
12003    If CODE is 'b', pretend the mode is QImode.
12004    If CODE is 'k', pretend the mode is SImode.
12005    If CODE is 'q', pretend the mode is DImode.
12006    If CODE is 'x', pretend the mode is V4SFmode.
12007    If CODE is 't', pretend the mode is V8SFmode.
12008    If CODE is 'h', pretend the reg is the 'high' byte register.
12009    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
12010    If CODE is 'd', duplicate the operand for AVX instruction.
12011  */
12012
12013 void
12014 print_reg (rtx x, int code, FILE *file)
12015 {
12016   const char *reg;
12017   bool duplicated = code == 'd' && TARGET_AVX;
12018
12019   gcc_assert (x == pc_rtx
12020               || (REGNO (x) != ARG_POINTER_REGNUM
12021                   && REGNO (x) != FRAME_POINTER_REGNUM
12022                   && REGNO (x) != FLAGS_REG
12023                   && REGNO (x) != FPSR_REG
12024                   && REGNO (x) != FPCR_REG));
12025
12026   if (ASSEMBLER_DIALECT == ASM_ATT)
12027     putc ('%', file);
12028
12029   if (x == pc_rtx)
12030     {
12031       gcc_assert (TARGET_64BIT);
12032       fputs ("rip", file);
12033       return;
12034     }
12035
12036   if (code == 'w' || MMX_REG_P (x))
12037     code = 2;
12038   else if (code == 'b')
12039     code = 1;
12040   else if (code == 'k')
12041     code = 4;
12042   else if (code == 'q')
12043     code = 8;
12044   else if (code == 'y')
12045     code = 3;
12046   else if (code == 'h')
12047     code = 0;
12048   else if (code == 'x')
12049     code = 16;
12050   else if (code == 't')
12051     code = 32;
12052   else
12053     code = GET_MODE_SIZE (GET_MODE (x));
12054
12055   /* Irritatingly, AMD extended registers use different naming convention
12056      from the normal registers.  */
12057   if (REX_INT_REG_P (x))
12058     {
12059       gcc_assert (TARGET_64BIT);
12060       switch (code)
12061         {
12062           case 0:
12063             error ("extended registers have no high halves");
12064             break;
12065           case 1:
12066             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
12067             break;
12068           case 2:
12069             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
12070             break;
12071           case 4:
12072             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
12073             break;
12074           case 8:
12075             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
12076             break;
12077           default:
12078             error ("unsupported operand size for extended register");
12079             break;
12080         }
12081       return;
12082     }
12083
12084   reg = NULL;
12085   switch (code)
12086     {
12087     case 3:
12088       if (STACK_TOP_P (x))
12089         {
12090           reg = "st(0)";
12091           break;
12092         }
12093       /* FALLTHRU */
12094     case 8:
12095     case 4:
12096     case 12:
12097       if (! ANY_FP_REG_P (x))
12098         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
12099       /* FALLTHRU */
12100     case 16:
12101     case 2:
12102     normal:
12103       reg = hi_reg_name[REGNO (x)];
12104       break;
12105     case 1:
12106       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
12107         goto normal;
12108       reg = qi_reg_name[REGNO (x)];
12109       break;
12110     case 0:
12111       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
12112         goto normal;
12113       reg = qi_high_reg_name[REGNO (x)];
12114       break;
12115     case 32:
12116       if (SSE_REG_P (x))
12117         {
12118           gcc_assert (!duplicated);
12119           putc ('y', file);
12120           fputs (hi_reg_name[REGNO (x)] + 1, file);
12121           return;
12122         }
12123       break;
12124     default:
12125       gcc_unreachable ();
12126     }
12127
12128   fputs (reg, file);
12129   if (duplicated)
12130     {
12131       if (ASSEMBLER_DIALECT == ASM_ATT)
12132         fprintf (file, ", %%%s", reg);
12133       else
12134         fprintf (file, ", %s", reg);
12135     }
12136 }
12137
12138 /* Locate some local-dynamic symbol still in use by this function
12139    so that we can print its name in some tls_local_dynamic_base
12140    pattern.  */
12141
12142 static int
12143 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
12144 {
12145   rtx x = *px;
12146
12147   if (GET_CODE (x) == SYMBOL_REF
12148       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
12149     {
12150       cfun->machine->some_ld_name = XSTR (x, 0);
12151       return 1;
12152     }
12153
12154   return 0;
12155 }
12156
12157 static const char *
12158 get_some_local_dynamic_name (void)
12159 {
12160   rtx insn;
12161
12162   if (cfun->machine->some_ld_name)
12163     return cfun->machine->some_ld_name;
12164
12165   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
12166     if (NONDEBUG_INSN_P (insn)
12167         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
12168       return cfun->machine->some_ld_name;
12169
12170   return NULL;
12171 }
12172
12173 /* Meaning of CODE:
12174    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
12175    C -- print opcode suffix for set/cmov insn.
12176    c -- like C, but print reversed condition
12177    F,f -- likewise, but for floating-point.
12178    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
12179         otherwise nothing
12180    R -- print the prefix for register names.
12181    z -- print the opcode suffix for the size of the current operand.
12182    Z -- likewise, with special suffixes for x87 instructions.
12183    * -- print a star (in certain assembler syntax)
12184    A -- print an absolute memory reference.
12185    w -- print the operand as if it's a "word" (HImode) even if it isn't.
12186    s -- print a shift double count, followed by the assemblers argument
12187         delimiter.
12188    b -- print the QImode name of the register for the indicated operand.
12189         %b0 would print %al if operands[0] is reg 0.
12190    w --  likewise, print the HImode name of the register.
12191    k --  likewise, print the SImode name of the register.
12192    q --  likewise, print the DImode name of the register.
12193    x --  likewise, print the V4SFmode name of the register.
12194    t --  likewise, print the V8SFmode name of the register.
12195    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
12196    y -- print "st(0)" instead of "st" as a register.
12197    d -- print duplicated register operand for AVX instruction.
12198    D -- print condition for SSE cmp instruction.
12199    P -- if PIC, print an @PLT suffix.
12200    X -- don't print any sort of PIC '@' suffix for a symbol.
12201    & -- print some in-use local-dynamic symbol name.
12202    H -- print a memory address offset by 8; used for sse high-parts
12203    Y -- print condition for XOP pcom* instruction.
12204    + -- print a branch hint as 'cs' or 'ds' prefix
12205    ; -- print a semicolon (after prefixes due to bug in older gas).
12206  */
12207
12208 void
12209 ix86_print_operand (FILE *file, rtx x, int code)
12210 {
12211   if (code)
12212     {
12213       switch (code)
12214         {
12215         case '*':
12216           if (ASSEMBLER_DIALECT == ASM_ATT)
12217             putc ('*', file);
12218           return;
12219
12220         case '&':
12221           {
12222             const char *name = get_some_local_dynamic_name ();
12223             if (name == NULL)
12224               output_operand_lossage ("'%%&' used without any "
12225                                       "local dynamic TLS references");
12226             else
12227               assemble_name (file, name);
12228             return;
12229           }
12230
12231         case 'A':
12232           switch (ASSEMBLER_DIALECT)
12233             {
12234             case ASM_ATT:
12235               putc ('*', file);
12236               break;
12237
12238             case ASM_INTEL:
12239               /* Intel syntax. For absolute addresses, registers should not
12240                  be surrounded by braces.  */
12241               if (!REG_P (x))
12242                 {
12243                   putc ('[', file);
12244                   ix86_print_operand (file, x, 0);
12245                   putc (']', file);
12246                   return;
12247                 }
12248               break;
12249
12250             default:
12251               gcc_unreachable ();
12252             }
12253
12254           ix86_print_operand (file, x, 0);
12255           return;
12256
12257
12258         case 'L':
12259           if (ASSEMBLER_DIALECT == ASM_ATT)
12260             putc ('l', file);
12261           return;
12262
12263         case 'W':
12264           if (ASSEMBLER_DIALECT == ASM_ATT)
12265             putc ('w', file);
12266           return;
12267
12268         case 'B':
12269           if (ASSEMBLER_DIALECT == ASM_ATT)
12270             putc ('b', file);
12271           return;
12272
12273         case 'Q':
12274           if (ASSEMBLER_DIALECT == ASM_ATT)
12275             putc ('l', file);
12276           return;
12277
12278         case 'S':
12279           if (ASSEMBLER_DIALECT == ASM_ATT)
12280             putc ('s', file);
12281           return;
12282
12283         case 'T':
12284           if (ASSEMBLER_DIALECT == ASM_ATT)
12285             putc ('t', file);
12286           return;
12287
12288         case 'z':
12289           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
12290             {
12291               /* Opcodes don't get size suffixes if using Intel opcodes.  */
12292               if (ASSEMBLER_DIALECT == ASM_INTEL)
12293                 return;
12294
12295               switch (GET_MODE_SIZE (GET_MODE (x)))
12296                 {
12297                 case 1:
12298                   putc ('b', file);
12299                   return;
12300
12301                 case 2:
12302                   putc ('w', file);
12303                   return;
12304
12305                 case 4:
12306                   putc ('l', file);
12307                   return;
12308
12309                 case 8:
12310                   putc ('q', file);
12311                   return;
12312
12313                 default:
12314                   output_operand_lossage
12315                     ("invalid operand size for operand code '%c'", code);
12316                   return;
12317                 }
12318             }
12319
12320           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
12321             warning
12322               (0, "non-integer operand used with operand code '%c'", code);
12323           /* FALLTHRU */
12324
12325         case 'Z':
12326           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
12327           if (ASSEMBLER_DIALECT == ASM_INTEL)
12328             return;
12329
12330           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
12331             {
12332               switch (GET_MODE_SIZE (GET_MODE (x)))
12333                 {
12334                 case 2:
12335 #ifdef HAVE_AS_IX86_FILDS
12336                   putc ('s', file);
12337 #endif
12338                   return;
12339
12340                 case 4:
12341                   putc ('l', file);
12342                   return;
12343
12344                 case 8:
12345 #ifdef HAVE_AS_IX86_FILDQ
12346                   putc ('q', file);
12347 #else
12348                   fputs ("ll", file);
12349 #endif
12350                   return;
12351
12352                 default:
12353                   break;
12354                 }
12355             }
12356           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
12357             {
12358               /* 387 opcodes don't get size suffixes
12359                  if the operands are registers.  */
12360               if (STACK_REG_P (x))
12361                 return;
12362
12363               switch (GET_MODE_SIZE (GET_MODE (x)))
12364                 {
12365                 case 4:
12366                   putc ('s', file);
12367                   return;
12368
12369                 case 8:
12370                   putc ('l', file);
12371                   return;
12372
12373                 case 12:
12374                 case 16:
12375                   putc ('t', file);
12376                   return;
12377
12378                 default:
12379                   break;
12380                 }
12381             }
12382           else
12383             {
12384               output_operand_lossage
12385                 ("invalid operand type used with operand code '%c'", code);
12386               return;
12387             }
12388
12389           output_operand_lossage
12390             ("invalid operand size for operand code '%c'", code);
12391           return;
12392
12393         case 'd':
12394         case 'b':
12395         case 'w':
12396         case 'k':
12397         case 'q':
12398         case 'h':
12399         case 't':
12400         case 'y':
12401         case 'x':
12402         case 'X':
12403         case 'P':
12404           break;
12405
12406         case 's':
12407           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
12408             {
12409               ix86_print_operand (file, x, 0);
12410               fputs (", ", file);
12411             }
12412           return;
12413
12414         case 'D':
12415           /* Little bit of braindamage here.  The SSE compare instructions
12416              does use completely different names for the comparisons that the
12417              fp conditional moves.  */
12418           if (TARGET_AVX)
12419             {
12420               switch (GET_CODE (x))
12421                 {
12422                 case EQ:
12423                   fputs ("eq", file);
12424                   break;
12425                 case UNEQ:
12426                   fputs ("eq_us", file);
12427                   break;
12428                 case LT:
12429                   fputs ("lt", file);
12430                   break;
12431                 case UNLT:
12432                   fputs ("nge", file);
12433                   break;
12434                 case LE:
12435                   fputs ("le", file);
12436                   break;
12437                 case UNLE:
12438                   fputs ("ngt", file);
12439                   break;
12440                 case UNORDERED:
12441                   fputs ("unord", file);
12442                   break;
12443                 case NE:
12444                   fputs ("neq", file);
12445                   break;
12446                 case LTGT:
12447                   fputs ("neq_oq", file);
12448                   break;
12449                 case GE:
12450                   fputs ("ge", file);
12451                   break;
12452                 case UNGE:
12453                   fputs ("nlt", file);
12454                   break;
12455                 case GT:
12456                   fputs ("gt", file);
12457                   break;
12458                 case UNGT:
12459                   fputs ("nle", file);
12460                   break;
12461                 case ORDERED:
12462                   fputs ("ord", file);
12463                   break;
12464                 default:
12465                   output_operand_lossage ("operand is not a condition code, "
12466                                           "invalid operand code 'D'");
12467                   return;
12468                 }
12469             }
12470           else
12471             {
12472               switch (GET_CODE (x))
12473                 {
12474                 case EQ:
12475                 case UNEQ:
12476                   fputs ("eq", file);
12477                   break;
12478                 case LT:
12479                 case UNLT:
12480                   fputs ("lt", file);
12481                   break;
12482                 case LE:
12483                 case UNLE:
12484                   fputs ("le", file);
12485                   break;
12486                 case UNORDERED:
12487                   fputs ("unord", file);
12488                   break;
12489                 case NE:
12490                 case LTGT:
12491                   fputs ("neq", file);
12492                   break;
12493                 case UNGE:
12494                 case GE:
12495                   fputs ("nlt", file);
12496                   break;
12497                 case UNGT:
12498                 case GT:
12499                   fputs ("nle", file);
12500                   break;
12501                 case ORDERED:
12502                   fputs ("ord", file);
12503                   break;
12504                 default:
12505                   output_operand_lossage ("operand is not a condition code, "
12506                                           "invalid operand code 'D'");
12507                   return;
12508                 }
12509             }
12510           return;
12511         case 'O':
12512 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
12513           if (ASSEMBLER_DIALECT == ASM_ATT)
12514             {
12515               switch (GET_MODE (x))
12516                 {
12517                 case HImode: putc ('w', file); break;
12518                 case SImode:
12519                 case SFmode: putc ('l', file); break;
12520                 case DImode:
12521                 case DFmode: putc ('q', file); break;
12522                 default: gcc_unreachable ();
12523                 }
12524               putc ('.', file);
12525             }
12526 #endif
12527           return;
12528         case 'C':
12529           if (!COMPARISON_P (x))
12530             {
12531               output_operand_lossage ("operand is neither a constant nor a "
12532                                       "condition code, invalid operand code "
12533                                       "'C'");
12534               return;
12535             }
12536           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
12537           return;
12538         case 'F':
12539           if (!COMPARISON_P (x))
12540             {
12541               output_operand_lossage ("operand is neither a constant nor a "
12542                                       "condition code, invalid operand code "
12543                                       "'F'");
12544               return;
12545             }
12546 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
12547           if (ASSEMBLER_DIALECT == ASM_ATT)
12548             putc ('.', file);
12549 #endif
12550           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
12551           return;
12552
12553           /* Like above, but reverse condition */
12554         case 'c':
12555           /* Check to see if argument to %c is really a constant
12556              and not a condition code which needs to be reversed.  */
12557           if (!COMPARISON_P (x))
12558             {
12559               output_operand_lossage ("operand is neither a constant nor a "
12560                                       "condition code, invalid operand "
12561                                       "code 'c'");
12562               return;
12563             }
12564           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
12565           return;
12566         case 'f':
12567           if (!COMPARISON_P (x))
12568             {
12569               output_operand_lossage ("operand is neither a constant nor a "
12570                                       "condition code, invalid operand "
12571                                       "code 'f'");
12572               return;
12573             }
12574 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
12575           if (ASSEMBLER_DIALECT == ASM_ATT)
12576             putc ('.', file);
12577 #endif
12578           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
12579           return;
12580
12581         case 'H':
12582           /* It doesn't actually matter what mode we use here, as we're
12583              only going to use this for printing.  */
12584           x = adjust_address_nv (x, DImode, 8);
12585           break;
12586
12587         case '+':
12588           {
12589             rtx x;
12590
12591             if (!optimize
12592                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
12593               return;
12594
12595             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
12596             if (x)
12597               {
12598                 int pred_val = INTVAL (XEXP (x, 0));
12599
12600                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
12601                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
12602                   {
12603                     int taken = pred_val > REG_BR_PROB_BASE / 2;
12604                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
12605
12606                     /* Emit hints only in the case default branch prediction
12607                        heuristics would fail.  */
12608                     if (taken != cputaken)
12609                       {
12610                         /* We use 3e (DS) prefix for taken branches and
12611                            2e (CS) prefix for not taken branches.  */
12612                         if (taken)
12613                           fputs ("ds ; ", file);
12614                         else
12615                           fputs ("cs ; ", file);
12616                       }
12617                   }
12618               }
12619             return;
12620           }
12621
12622         case 'Y':
12623           switch (GET_CODE (x))
12624             {
12625             case NE:
12626               fputs ("neq", file);
12627               break;
12628             case EQ:
12629               fputs ("eq", file);
12630               break;
12631             case GE:
12632             case GEU:
12633               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
12634               break;
12635             case GT:
12636             case GTU:
12637               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
12638               break;
12639             case LE:
12640             case LEU:
12641               fputs ("le", file);
12642               break;
12643             case LT:
12644             case LTU:
12645               fputs ("lt", file);
12646               break;
12647             case UNORDERED:
12648               fputs ("unord", file);
12649               break;
12650             case ORDERED:
12651               fputs ("ord", file);
12652               break;
12653             case UNEQ:
12654               fputs ("ueq", file);
12655               break;
12656             case UNGE:
12657               fputs ("nlt", file);
12658               break;
12659             case UNGT:
12660               fputs ("nle", file);
12661               break;
12662             case UNLE:
12663               fputs ("ule", file);
12664               break;
12665             case UNLT:
12666               fputs ("ult", file);
12667               break;
12668             case LTGT:
12669               fputs ("une", file);
12670               break;
12671             default:
12672               output_operand_lossage ("operand is not a condition code, "
12673                                       "invalid operand code 'Y'");
12674               return;
12675             }
12676           return;
12677
12678         case ';':
12679 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
12680           putc (';', file);
12681 #endif
12682           return;
12683
12684         default:
12685             output_operand_lossage ("invalid operand code '%c'", code);
12686         }
12687     }
12688
12689   if (REG_P (x))
12690     print_reg (x, code, file);
12691
12692   else if (MEM_P (x))
12693     {
12694       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
12695       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
12696           && GET_MODE (x) != BLKmode)
12697         {
12698           const char * size;
12699           switch (GET_MODE_SIZE (GET_MODE (x)))
12700             {
12701             case 1: size = "BYTE"; break;
12702             case 2: size = "WORD"; break;
12703             case 4: size = "DWORD"; break;
12704             case 8: size = "QWORD"; break;
12705             case 12: size = "TBYTE"; break;
12706             case 16:
12707               if (GET_MODE (x) == XFmode)
12708                 size = "TBYTE";
12709               else
12710                 size = "XMMWORD";
12711               break;
12712             case 32: size = "YMMWORD"; break;
12713             default:
12714               gcc_unreachable ();
12715             }
12716
12717           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
12718           if (code == 'b')
12719             size = "BYTE";
12720           else if (code == 'w')
12721             size = "WORD";
12722           else if (code == 'k')
12723             size = "DWORD";
12724
12725           fputs (size, file);
12726           fputs (" PTR ", file);
12727         }
12728
12729       x = XEXP (x, 0);
12730       /* Avoid (%rip) for call operands.  */
12731       if (CONSTANT_ADDRESS_P (x) && code == 'P'
12732           && !CONST_INT_P (x))
12733         output_addr_const (file, x);
12734       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
12735         output_operand_lossage ("invalid constraints for operand");
12736       else
12737         output_address (x);
12738     }
12739
12740   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
12741     {
12742       REAL_VALUE_TYPE r;
12743       long l;
12744
12745       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
12746       REAL_VALUE_TO_TARGET_SINGLE (r, l);
12747
12748       if (ASSEMBLER_DIALECT == ASM_ATT)
12749         putc ('$', file);
12750       fprintf (file, "0x%08lx", (long unsigned int) l);
12751     }
12752
12753   /* These float cases don't actually occur as immediate operands.  */
12754   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
12755     {
12756       char dstr[30];
12757
12758       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
12759       fputs (dstr, file);
12760     }
12761
12762   else if (GET_CODE (x) == CONST_DOUBLE
12763            && GET_MODE (x) == XFmode)
12764     {
12765       char dstr[30];
12766
12767       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
12768       fputs (dstr, file);
12769     }
12770
12771   else
12772     {
12773       /* We have patterns that allow zero sets of memory, for instance.
12774          In 64-bit mode, we should probably support all 8-byte vectors,
12775          since we can in fact encode that into an immediate.  */
12776       if (GET_CODE (x) == CONST_VECTOR)
12777         {
12778           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
12779           x = const0_rtx;
12780         }
12781
12782       if (code != 'P')
12783         {
12784           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
12785             {
12786               if (ASSEMBLER_DIALECT == ASM_ATT)
12787                 putc ('$', file);
12788             }
12789           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
12790                    || GET_CODE (x) == LABEL_REF)
12791             {
12792               if (ASSEMBLER_DIALECT == ASM_ATT)
12793                 putc ('$', file);
12794               else
12795                 fputs ("OFFSET FLAT:", file);
12796             }
12797         }
12798       if (CONST_INT_P (x))
12799         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12800       else if (flag_pic)
12801         output_pic_addr_const (file, x, code);
12802       else
12803         output_addr_const (file, x);
12804     }
12805 }
12806
12807 static bool
12808 ix86_print_operand_punct_valid_p (unsigned char code)
12809 {
12810   return (code == '*' || code == '+' || code == '&' || code == ';');
12811 }
12812 \f
12813 /* Print a memory operand whose address is ADDR.  */
12814
12815 static void
12816 ix86_print_operand_address (FILE *file, rtx addr)
12817 {
12818   struct ix86_address parts;
12819   rtx base, index, disp;
12820   int scale;
12821   int ok = ix86_decompose_address (addr, &parts);
12822
12823   gcc_assert (ok);
12824
12825   base = parts.base;
12826   index = parts.index;
12827   disp = parts.disp;
12828   scale = parts.scale;
12829
12830   switch (parts.seg)
12831     {
12832     case SEG_DEFAULT:
12833       break;
12834     case SEG_FS:
12835     case SEG_GS:
12836       if (ASSEMBLER_DIALECT == ASM_ATT)
12837         putc ('%', file);
12838       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
12839       break;
12840     default:
12841       gcc_unreachable ();
12842     }
12843
12844   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
12845   if (TARGET_64BIT && !base && !index)
12846     {
12847       rtx symbol = disp;
12848
12849       if (GET_CODE (disp) == CONST
12850           && GET_CODE (XEXP (disp, 0)) == PLUS
12851           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
12852         symbol = XEXP (XEXP (disp, 0), 0);
12853
12854       if (GET_CODE (symbol) == LABEL_REF
12855           || (GET_CODE (symbol) == SYMBOL_REF
12856               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
12857         base = pc_rtx;
12858     }
12859   if (!base && !index)
12860     {
12861       /* Displacement only requires special attention.  */
12862
12863       if (CONST_INT_P (disp))
12864         {
12865           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
12866             fputs ("ds:", file);
12867           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
12868         }
12869       else if (flag_pic)
12870         output_pic_addr_const (file, disp, 0);
12871       else
12872         output_addr_const (file, disp);
12873     }
12874   else
12875     {
12876       if (ASSEMBLER_DIALECT == ASM_ATT)
12877         {
12878           if (disp)
12879             {
12880               if (flag_pic)
12881                 output_pic_addr_const (file, disp, 0);
12882               else if (GET_CODE (disp) == LABEL_REF)
12883                 output_asm_label (disp);
12884               else
12885                 output_addr_const (file, disp);
12886             }
12887
12888           putc ('(', file);
12889           if (base)
12890             print_reg (base, 0, file);
12891           if (index)
12892             {
12893               putc (',', file);
12894               print_reg (index, 0, file);
12895               if (scale != 1)
12896                 fprintf (file, ",%d", scale);
12897             }
12898           putc (')', file);
12899         }
12900       else
12901         {
12902           rtx offset = NULL_RTX;
12903
12904           if (disp)
12905             {
12906               /* Pull out the offset of a symbol; print any symbol itself.  */
12907               if (GET_CODE (disp) == CONST
12908                   && GET_CODE (XEXP (disp, 0)) == PLUS
12909                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
12910                 {
12911                   offset = XEXP (XEXP (disp, 0), 1);
12912                   disp = gen_rtx_CONST (VOIDmode,
12913                                         XEXP (XEXP (disp, 0), 0));
12914                 }
12915
12916               if (flag_pic)
12917                 output_pic_addr_const (file, disp, 0);
12918               else if (GET_CODE (disp) == LABEL_REF)
12919                 output_asm_label (disp);
12920               else if (CONST_INT_P (disp))
12921                 offset = disp;
12922               else
12923                 output_addr_const (file, disp);
12924             }
12925
12926           putc ('[', file);
12927           if (base)
12928             {
12929               print_reg (base, 0, file);
12930               if (offset)
12931                 {
12932                   if (INTVAL (offset) >= 0)
12933                     putc ('+', file);
12934                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
12935                 }
12936             }
12937           else if (offset)
12938             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
12939           else
12940             putc ('0', file);
12941
12942           if (index)
12943             {
12944               putc ('+', file);
12945               print_reg (index, 0, file);
12946               if (scale != 1)
12947                 fprintf (file, "*%d", scale);
12948             }
12949           putc (']', file);
12950         }
12951     }
12952 }
12953
12954 bool
12955 output_addr_const_extra (FILE *file, rtx x)
12956 {
12957   rtx op;
12958
12959   if (GET_CODE (x) != UNSPEC)
12960     return false;
12961
12962   op = XVECEXP (x, 0, 0);
12963   switch (XINT (x, 1))
12964     {
12965     case UNSPEC_GOTTPOFF:
12966       output_addr_const (file, op);
12967       /* FIXME: This might be @TPOFF in Sun ld.  */
12968       fputs ("@gottpoff", file);
12969       break;
12970     case UNSPEC_TPOFF:
12971       output_addr_const (file, op);
12972       fputs ("@tpoff", file);
12973       break;
12974     case UNSPEC_NTPOFF:
12975       output_addr_const (file, op);
12976       if (TARGET_64BIT)
12977         fputs ("@tpoff", file);
12978       else
12979         fputs ("@ntpoff", file);
12980       break;
12981     case UNSPEC_DTPOFF:
12982       output_addr_const (file, op);
12983       fputs ("@dtpoff", file);
12984       break;
12985     case UNSPEC_GOTNTPOFF:
12986       output_addr_const (file, op);
12987       if (TARGET_64BIT)
12988         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12989                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
12990       else
12991         fputs ("@gotntpoff", file);
12992       break;
12993     case UNSPEC_INDNTPOFF:
12994       output_addr_const (file, op);
12995       fputs ("@indntpoff", file);
12996       break;
12997 #if TARGET_MACHO
12998     case UNSPEC_MACHOPIC_OFFSET:
12999       output_addr_const (file, op);
13000       putc ('-', file);
13001       machopic_output_function_base_name (file);
13002       break;
13003 #endif
13004
13005     default:
13006       return false;
13007     }
13008
13009   return true;
13010 }
13011 \f
13012 /* Split one or more DImode RTL references into pairs of SImode
13013    references.  The RTL can be REG, offsettable MEM, integer constant, or
13014    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
13015    split and "num" is its length.  lo_half and hi_half are output arrays
13016    that parallel "operands".  */
13017
13018 void
13019 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
13020 {
13021   while (num--)
13022     {
13023       rtx op = operands[num];
13024
13025       /* simplify_subreg refuse to split volatile memory addresses,
13026          but we still have to handle it.  */
13027       if (MEM_P (op))
13028         {
13029           lo_half[num] = adjust_address (op, SImode, 0);
13030           hi_half[num] = adjust_address (op, SImode, 4);
13031         }
13032       else
13033         {
13034           lo_half[num] = simplify_gen_subreg (SImode, op,
13035                                               GET_MODE (op) == VOIDmode
13036                                               ? DImode : GET_MODE (op), 0);
13037           hi_half[num] = simplify_gen_subreg (SImode, op,
13038                                               GET_MODE (op) == VOIDmode
13039                                               ? DImode : GET_MODE (op), 4);
13040         }
13041     }
13042 }
13043 /* Split one or more TImode RTL references into pairs of DImode
13044    references.  The RTL can be REG, offsettable MEM, integer constant, or
13045    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
13046    split and "num" is its length.  lo_half and hi_half are output arrays
13047    that parallel "operands".  */
13048
13049 void
13050 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
13051 {
13052   while (num--)
13053     {
13054       rtx op = operands[num];
13055
13056       /* simplify_subreg refuse to split volatile memory addresses, but we
13057          still have to handle it.  */
13058       if (MEM_P (op))
13059         {
13060           lo_half[num] = adjust_address (op, DImode, 0);
13061           hi_half[num] = adjust_address (op, DImode, 8);
13062         }
13063       else
13064         {
13065           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
13066           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
13067         }
13068     }
13069 }
13070 \f
13071 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
13072    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
13073    is the expression of the binary operation.  The output may either be
13074    emitted here, or returned to the caller, like all output_* functions.
13075
13076    There is no guarantee that the operands are the same mode, as they
13077    might be within FLOAT or FLOAT_EXTEND expressions.  */
13078
13079 #ifndef SYSV386_COMPAT
13080 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
13081    wants to fix the assemblers because that causes incompatibility
13082    with gcc.  No-one wants to fix gcc because that causes
13083    incompatibility with assemblers...  You can use the option of
13084    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
13085 #define SYSV386_COMPAT 1
13086 #endif
13087
13088 const char *
13089 output_387_binary_op (rtx insn, rtx *operands)
13090 {
13091   static char buf[40];
13092   const char *p;
13093   const char *ssep;
13094   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
13095
13096 #ifdef ENABLE_CHECKING
13097   /* Even if we do not want to check the inputs, this documents input
13098      constraints.  Which helps in understanding the following code.  */
13099   if (STACK_REG_P (operands[0])
13100       && ((REG_P (operands[1])
13101            && REGNO (operands[0]) == REGNO (operands[1])
13102            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
13103           || (REG_P (operands[2])
13104               && REGNO (operands[0]) == REGNO (operands[2])
13105               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
13106       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
13107     ; /* ok */
13108   else
13109     gcc_assert (is_sse);
13110 #endif
13111
13112   switch (GET_CODE (operands[3]))
13113     {
13114     case PLUS:
13115       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13116           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13117         p = "fiadd";
13118       else
13119         p = "fadd";
13120       ssep = "vadd";
13121       break;
13122
13123     case MINUS:
13124       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13125           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13126         p = "fisub";
13127       else
13128         p = "fsub";
13129       ssep = "vsub";
13130       break;
13131
13132     case MULT:
13133       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13134           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13135         p = "fimul";
13136       else
13137         p = "fmul";
13138       ssep = "vmul";
13139       break;
13140
13141     case DIV:
13142       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13143           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13144         p = "fidiv";
13145       else
13146         p = "fdiv";
13147       ssep = "vdiv";
13148       break;
13149
13150     default:
13151       gcc_unreachable ();
13152     }
13153
13154   if (is_sse)
13155    {
13156      if (TARGET_AVX)
13157        {
13158          strcpy (buf, ssep);
13159          if (GET_MODE (operands[0]) == SFmode)
13160            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
13161          else
13162            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
13163        }
13164      else
13165        {
13166          strcpy (buf, ssep + 1);
13167          if (GET_MODE (operands[0]) == SFmode)
13168            strcat (buf, "ss\t{%2, %0|%0, %2}");
13169          else
13170            strcat (buf, "sd\t{%2, %0|%0, %2}");
13171        }
13172       return buf;
13173    }
13174   strcpy (buf, p);
13175
13176   switch (GET_CODE (operands[3]))
13177     {
13178     case MULT:
13179     case PLUS:
13180       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
13181         {
13182           rtx temp = operands[2];
13183           operands[2] = operands[1];
13184           operands[1] = temp;
13185         }
13186
13187       /* know operands[0] == operands[1].  */
13188
13189       if (MEM_P (operands[2]))
13190         {
13191           p = "%Z2\t%2";
13192           break;
13193         }
13194
13195       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
13196         {
13197           if (STACK_TOP_P (operands[0]))
13198             /* How is it that we are storing to a dead operand[2]?
13199                Well, presumably operands[1] is dead too.  We can't
13200                store the result to st(0) as st(0) gets popped on this
13201                instruction.  Instead store to operands[2] (which I
13202                think has to be st(1)).  st(1) will be popped later.
13203                gcc <= 2.8.1 didn't have this check and generated
13204                assembly code that the Unixware assembler rejected.  */
13205             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
13206           else
13207             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
13208           break;
13209         }
13210
13211       if (STACK_TOP_P (operands[0]))
13212         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
13213       else
13214         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
13215       break;
13216
13217     case MINUS:
13218     case DIV:
13219       if (MEM_P (operands[1]))
13220         {
13221           p = "r%Z1\t%1";
13222           break;
13223         }
13224
13225       if (MEM_P (operands[2]))
13226         {
13227           p = "%Z2\t%2";
13228           break;
13229         }
13230
13231       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
13232         {
13233 #if SYSV386_COMPAT
13234           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
13235              derived assemblers, confusingly reverse the direction of
13236              the operation for fsub{r} and fdiv{r} when the
13237              destination register is not st(0).  The Intel assembler
13238              doesn't have this brain damage.  Read !SYSV386_COMPAT to
13239              figure out what the hardware really does.  */
13240           if (STACK_TOP_P (operands[0]))
13241             p = "{p\t%0, %2|rp\t%2, %0}";
13242           else
13243             p = "{rp\t%2, %0|p\t%0, %2}";
13244 #else
13245           if (STACK_TOP_P (operands[0]))
13246             /* As above for fmul/fadd, we can't store to st(0).  */
13247             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
13248           else
13249             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
13250 #endif
13251           break;
13252         }
13253
13254       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
13255         {
13256 #if SYSV386_COMPAT
13257           if (STACK_TOP_P (operands[0]))
13258             p = "{rp\t%0, %1|p\t%1, %0}";
13259           else
13260             p = "{p\t%1, %0|rp\t%0, %1}";
13261 #else
13262           if (STACK_TOP_P (operands[0]))
13263             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
13264           else
13265             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
13266 #endif
13267           break;
13268         }
13269
13270       if (STACK_TOP_P (operands[0]))
13271         {
13272           if (STACK_TOP_P (operands[1]))
13273             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
13274           else
13275             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
13276           break;
13277         }
13278       else if (STACK_TOP_P (operands[1]))
13279         {
13280 #if SYSV386_COMPAT
13281           p = "{\t%1, %0|r\t%0, %1}";
13282 #else
13283           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
13284 #endif
13285         }
13286       else
13287         {
13288 #if SYSV386_COMPAT
13289           p = "{r\t%2, %0|\t%0, %2}";
13290 #else
13291           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
13292 #endif
13293         }
13294       break;
13295
13296     default:
13297       gcc_unreachable ();
13298     }
13299
13300   strcat (buf, p);
13301   return buf;
13302 }
13303
13304 /* Return needed mode for entity in optimize_mode_switching pass.  */
13305
13306 int
13307 ix86_mode_needed (int entity, rtx insn)
13308 {
13309   enum attr_i387_cw mode;
13310
13311   /* The mode UNINITIALIZED is used to store control word after a
13312      function call or ASM pattern.  The mode ANY specify that function
13313      has no requirements on the control word and make no changes in the
13314      bits we are interested in.  */
13315
13316   if (CALL_P (insn)
13317       || (NONJUMP_INSN_P (insn)
13318           && (asm_noperands (PATTERN (insn)) >= 0
13319               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
13320     return I387_CW_UNINITIALIZED;
13321
13322   if (recog_memoized (insn) < 0)
13323     return I387_CW_ANY;
13324
13325   mode = get_attr_i387_cw (insn);
13326
13327   switch (entity)
13328     {
13329     case I387_TRUNC:
13330       if (mode == I387_CW_TRUNC)
13331         return mode;
13332       break;
13333
13334     case I387_FLOOR:
13335       if (mode == I387_CW_FLOOR)
13336         return mode;
13337       break;
13338
13339     case I387_CEIL:
13340       if (mode == I387_CW_CEIL)
13341         return mode;
13342       break;
13343
13344     case I387_MASK_PM:
13345       if (mode == I387_CW_MASK_PM)
13346         return mode;
13347       break;
13348
13349     default:
13350       gcc_unreachable ();
13351     }
13352
13353   return I387_CW_ANY;
13354 }
13355
13356 /* Output code to initialize control word copies used by trunc?f?i and
13357    rounding patterns.  CURRENT_MODE is set to current control word,
13358    while NEW_MODE is set to new control word.  */
13359
13360 void
13361 emit_i387_cw_initialization (int mode)
13362 {
13363   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
13364   rtx new_mode;
13365
13366   enum ix86_stack_slot slot;
13367
13368   rtx reg = gen_reg_rtx (HImode);
13369
13370   emit_insn (gen_x86_fnstcw_1 (stored_mode));
13371   emit_move_insn (reg, copy_rtx (stored_mode));
13372
13373   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
13374       || optimize_function_for_size_p (cfun))
13375     {
13376       switch (mode)
13377         {
13378         case I387_CW_TRUNC:
13379           /* round toward zero (truncate) */
13380           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
13381           slot = SLOT_CW_TRUNC;
13382           break;
13383
13384         case I387_CW_FLOOR:
13385           /* round down toward -oo */
13386           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
13387           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
13388           slot = SLOT_CW_FLOOR;
13389           break;
13390
13391         case I387_CW_CEIL:
13392           /* round up toward +oo */
13393           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
13394           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
13395           slot = SLOT_CW_CEIL;
13396           break;
13397
13398         case I387_CW_MASK_PM:
13399           /* mask precision exception for nearbyint() */
13400           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
13401           slot = SLOT_CW_MASK_PM;
13402           break;
13403
13404         default:
13405           gcc_unreachable ();
13406         }
13407     }
13408   else
13409     {
13410       switch (mode)
13411         {
13412         case I387_CW_TRUNC:
13413           /* round toward zero (truncate) */
13414           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
13415           slot = SLOT_CW_TRUNC;
13416           break;
13417
13418         case I387_CW_FLOOR:
13419           /* round down toward -oo */
13420           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
13421           slot = SLOT_CW_FLOOR;
13422           break;
13423
13424         case I387_CW_CEIL:
13425           /* round up toward +oo */
13426           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
13427           slot = SLOT_CW_CEIL;
13428           break;
13429
13430         case I387_CW_MASK_PM:
13431           /* mask precision exception for nearbyint() */
13432           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
13433           slot = SLOT_CW_MASK_PM;
13434           break;
13435
13436         default:
13437           gcc_unreachable ();
13438         }
13439     }
13440
13441   gcc_assert (slot < MAX_386_STACK_LOCALS);
13442
13443   new_mode = assign_386_stack_local (HImode, slot);
13444   emit_move_insn (new_mode, reg);
13445 }
13446
13447 /* Output code for INSN to convert a float to a signed int.  OPERANDS
13448    are the insn operands.  The output may be [HSD]Imode and the input
13449    operand may be [SDX]Fmode.  */
13450
13451 const char *
13452 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
13453 {
13454   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
13455   int dimode_p = GET_MODE (operands[0]) == DImode;
13456   int round_mode = get_attr_i387_cw (insn);
13457
13458   /* Jump through a hoop or two for DImode, since the hardware has no
13459      non-popping instruction.  We used to do this a different way, but
13460      that was somewhat fragile and broke with post-reload splitters.  */
13461   if ((dimode_p || fisttp) && !stack_top_dies)
13462     output_asm_insn ("fld\t%y1", operands);
13463
13464   gcc_assert (STACK_TOP_P (operands[1]));
13465   gcc_assert (MEM_P (operands[0]));
13466   gcc_assert (GET_MODE (operands[1]) != TFmode);
13467
13468   if (fisttp)
13469       output_asm_insn ("fisttp%Z0\t%0", operands);
13470   else
13471     {
13472       if (round_mode != I387_CW_ANY)
13473         output_asm_insn ("fldcw\t%3", operands);
13474       if (stack_top_dies || dimode_p)
13475         output_asm_insn ("fistp%Z0\t%0", operands);
13476       else
13477         output_asm_insn ("fist%Z0\t%0", operands);
13478       if (round_mode != I387_CW_ANY)
13479         output_asm_insn ("fldcw\t%2", operands);
13480     }
13481
13482   return "";
13483 }
13484
13485 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
13486    have the values zero or one, indicates the ffreep insn's operand
13487    from the OPERANDS array.  */
13488
13489 static const char *
13490 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
13491 {
13492   if (TARGET_USE_FFREEP)
13493 #ifdef HAVE_AS_IX86_FFREEP
13494     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
13495 #else
13496     {
13497       static char retval[32];
13498       int regno = REGNO (operands[opno]);
13499
13500       gcc_assert (FP_REGNO_P (regno));
13501
13502       regno -= FIRST_STACK_REG;
13503
13504       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
13505       return retval;
13506     }
13507 #endif
13508
13509   return opno ? "fstp\t%y1" : "fstp\t%y0";
13510 }
13511
13512
13513 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
13514    should be used.  UNORDERED_P is true when fucom should be used.  */
13515
13516 const char *
13517 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
13518 {
13519   int stack_top_dies;
13520   rtx cmp_op0, cmp_op1;
13521   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
13522
13523   if (eflags_p)
13524     {
13525       cmp_op0 = operands[0];
13526       cmp_op1 = operands[1];
13527     }
13528   else
13529     {
13530       cmp_op0 = operands[1];
13531       cmp_op1 = operands[2];
13532     }
13533
13534   if (is_sse)
13535     {
13536       static const char ucomiss[] = "vucomiss\t{%1, %0|%0, %1}";
13537       static const char ucomisd[] = "vucomisd\t{%1, %0|%0, %1}";
13538       static const char comiss[] = "vcomiss\t{%1, %0|%0, %1}";
13539       static const char comisd[] = "vcomisd\t{%1, %0|%0, %1}";
13540
13541       if (GET_MODE (operands[0]) == SFmode)
13542         if (unordered_p)
13543           return &ucomiss[TARGET_AVX ? 0 : 1];
13544         else
13545           return &comiss[TARGET_AVX ? 0 : 1];
13546       else
13547         if (unordered_p)
13548           return &ucomisd[TARGET_AVX ? 0 : 1];
13549         else
13550           return &comisd[TARGET_AVX ? 0 : 1];
13551     }
13552
13553   gcc_assert (STACK_TOP_P (cmp_op0));
13554
13555   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
13556
13557   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
13558     {
13559       if (stack_top_dies)
13560         {
13561           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
13562           return output_387_ffreep (operands, 1);
13563         }
13564       else
13565         return "ftst\n\tfnstsw\t%0";
13566     }
13567
13568   if (STACK_REG_P (cmp_op1)
13569       && stack_top_dies
13570       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
13571       && REGNO (cmp_op1) != FIRST_STACK_REG)
13572     {
13573       /* If both the top of the 387 stack dies, and the other operand
13574          is also a stack register that dies, then this must be a
13575          `fcompp' float compare */
13576
13577       if (eflags_p)
13578         {
13579           /* There is no double popping fcomi variant.  Fortunately,
13580              eflags is immune from the fstp's cc clobbering.  */
13581           if (unordered_p)
13582             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
13583           else
13584             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
13585           return output_387_ffreep (operands, 0);
13586         }
13587       else
13588         {
13589           if (unordered_p)
13590             return "fucompp\n\tfnstsw\t%0";
13591           else
13592             return "fcompp\n\tfnstsw\t%0";
13593         }
13594     }
13595   else
13596     {
13597       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
13598
13599       static const char * const alt[16] =
13600       {
13601         "fcom%Z2\t%y2\n\tfnstsw\t%0",
13602         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
13603         "fucom%Z2\t%y2\n\tfnstsw\t%0",
13604         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
13605
13606         "ficom%Z2\t%y2\n\tfnstsw\t%0",
13607         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
13608         NULL,
13609         NULL,
13610
13611         "fcomi\t{%y1, %0|%0, %y1}",
13612         "fcomip\t{%y1, %0|%0, %y1}",
13613         "fucomi\t{%y1, %0|%0, %y1}",
13614         "fucomip\t{%y1, %0|%0, %y1}",
13615
13616         NULL,
13617         NULL,
13618         NULL,
13619         NULL
13620       };
13621
13622       int mask;
13623       const char *ret;
13624
13625       mask  = eflags_p << 3;
13626       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
13627       mask |= unordered_p << 1;
13628       mask |= stack_top_dies;
13629
13630       gcc_assert (mask < 16);
13631       ret = alt[mask];
13632       gcc_assert (ret);
13633
13634       return ret;
13635     }
13636 }
13637
13638 void
13639 ix86_output_addr_vec_elt (FILE *file, int value)
13640 {
13641   const char *directive = ASM_LONG;
13642
13643 #ifdef ASM_QUAD
13644   if (TARGET_64BIT)
13645     directive = ASM_QUAD;
13646 #else
13647   gcc_assert (!TARGET_64BIT);
13648 #endif
13649
13650   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
13651 }
13652
13653 void
13654 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
13655 {
13656   const char *directive = ASM_LONG;
13657
13658 #ifdef ASM_QUAD
13659   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
13660     directive = ASM_QUAD;
13661 #else
13662   gcc_assert (!TARGET_64BIT);
13663 #endif
13664   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
13665   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
13666     fprintf (file, "%s%s%d-%s%d\n",
13667              directive, LPREFIX, value, LPREFIX, rel);
13668   else if (HAVE_AS_GOTOFF_IN_DATA)
13669     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
13670 #if TARGET_MACHO
13671   else if (TARGET_MACHO)
13672     {
13673       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
13674       machopic_output_function_base_name (file);
13675       putc ('\n', file);
13676     }
13677 #endif
13678   else
13679     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
13680                  GOT_SYMBOL_NAME, LPREFIX, value);
13681 }
13682 \f
13683 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
13684    for the target.  */
13685
13686 void
13687 ix86_expand_clear (rtx dest)
13688 {
13689   rtx tmp;
13690
13691   /* We play register width games, which are only valid after reload.  */
13692   gcc_assert (reload_completed);
13693
13694   /* Avoid HImode and its attendant prefix byte.  */
13695   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
13696     dest = gen_rtx_REG (SImode, REGNO (dest));
13697   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
13698
13699   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
13700   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
13701     {
13702       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
13703       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
13704     }
13705
13706   emit_insn (tmp);
13707 }
13708
13709 /* X is an unchanging MEM.  If it is a constant pool reference, return
13710    the constant pool rtx, else NULL.  */
13711
13712 rtx
13713 maybe_get_pool_constant (rtx x)
13714 {
13715   x = ix86_delegitimize_address (XEXP (x, 0));
13716
13717   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
13718     return get_pool_constant (x);
13719
13720   return NULL_RTX;
13721 }
13722
13723 void
13724 ix86_expand_move (enum machine_mode mode, rtx operands[])
13725 {
13726   rtx op0, op1;
13727   enum tls_model model;
13728
13729   op0 = operands[0];
13730   op1 = operands[1];
13731
13732   if (GET_CODE (op1) == SYMBOL_REF)
13733     {
13734       model = SYMBOL_REF_TLS_MODEL (op1);
13735       if (model)
13736         {
13737           op1 = legitimize_tls_address (op1, model, true);
13738           op1 = force_operand (op1, op0);
13739           if (op1 == op0)
13740             return;
13741         }
13742       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
13743                && SYMBOL_REF_DLLIMPORT_P (op1))
13744         op1 = legitimize_dllimport_symbol (op1, false);
13745     }
13746   else if (GET_CODE (op1) == CONST
13747            && GET_CODE (XEXP (op1, 0)) == PLUS
13748            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
13749     {
13750       rtx addend = XEXP (XEXP (op1, 0), 1);
13751       rtx symbol = XEXP (XEXP (op1, 0), 0);
13752       rtx tmp = NULL;
13753
13754       model = SYMBOL_REF_TLS_MODEL (symbol);
13755       if (model)
13756         tmp = legitimize_tls_address (symbol, model, true);
13757       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
13758                && SYMBOL_REF_DLLIMPORT_P (symbol))
13759         tmp = legitimize_dllimport_symbol (symbol, true);
13760
13761       if (tmp)
13762         {
13763           tmp = force_operand (tmp, NULL);
13764           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
13765                                      op0, 1, OPTAB_DIRECT);
13766           if (tmp == op0)
13767             return;
13768         }
13769     }
13770
13771   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
13772     {
13773       if (TARGET_MACHO && !TARGET_64BIT)
13774         {
13775 #if TARGET_MACHO
13776           if (MACHOPIC_PURE)
13777             {
13778               rtx temp = ((reload_in_progress
13779                            || ((op0 && REG_P (op0))
13780                                && mode == Pmode))
13781                           ? op0 : gen_reg_rtx (Pmode));
13782               op1 = machopic_indirect_data_reference (op1, temp);
13783               op1 = machopic_legitimize_pic_address (op1, mode,
13784                                                      temp == op1 ? 0 : temp);
13785             }
13786           else if (MACHOPIC_INDIRECT)
13787             op1 = machopic_indirect_data_reference (op1, 0);
13788           if (op0 == op1)
13789             return;
13790 #endif
13791         }
13792       else
13793         {
13794           if (MEM_P (op0))
13795             op1 = force_reg (Pmode, op1);
13796           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
13797             {
13798               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
13799               op1 = legitimize_pic_address (op1, reg);
13800               if (op0 == op1)
13801                 return;
13802             }
13803         }
13804     }
13805   else
13806     {
13807       if (MEM_P (op0)
13808           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
13809               || !push_operand (op0, mode))
13810           && MEM_P (op1))
13811         op1 = force_reg (mode, op1);
13812
13813       if (push_operand (op0, mode)
13814           && ! general_no_elim_operand (op1, mode))
13815         op1 = copy_to_mode_reg (mode, op1);
13816
13817       /* Force large constants in 64bit compilation into register
13818          to get them CSEed.  */
13819       if (can_create_pseudo_p ()
13820           && (mode == DImode) && TARGET_64BIT
13821           && immediate_operand (op1, mode)
13822           && !x86_64_zext_immediate_operand (op1, VOIDmode)
13823           && !register_operand (op0, mode)
13824           && optimize)
13825         op1 = copy_to_mode_reg (mode, op1);
13826
13827       if (can_create_pseudo_p ()
13828           && FLOAT_MODE_P (mode)
13829           && GET_CODE (op1) == CONST_DOUBLE)
13830         {
13831           /* If we are loading a floating point constant to a register,
13832              force the value to memory now, since we'll get better code
13833              out the back end.  */
13834
13835           op1 = validize_mem (force_const_mem (mode, op1));
13836           if (!register_operand (op0, mode))
13837             {
13838               rtx temp = gen_reg_rtx (mode);
13839               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
13840               emit_move_insn (op0, temp);
13841               return;
13842             }
13843         }
13844     }
13845
13846   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
13847 }
13848
13849 void
13850 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
13851 {
13852   rtx op0 = operands[0], op1 = operands[1];
13853   unsigned int align = GET_MODE_ALIGNMENT (mode);
13854
13855   /* Force constants other than zero into memory.  We do not know how
13856      the instructions used to build constants modify the upper 64 bits
13857      of the register, once we have that information we may be able
13858      to handle some of them more efficiently.  */
13859   if (can_create_pseudo_p ()
13860       && register_operand (op0, mode)
13861       && (CONSTANT_P (op1)
13862           || (GET_CODE (op1) == SUBREG
13863               && CONSTANT_P (SUBREG_REG (op1))))
13864       && !standard_sse_constant_p (op1))
13865     op1 = validize_mem (force_const_mem (mode, op1));
13866
13867   /* We need to check memory alignment for SSE mode since attribute
13868      can make operands unaligned.  */
13869   if (can_create_pseudo_p ()
13870       && SSE_REG_MODE_P (mode)
13871       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
13872           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
13873     {
13874       rtx tmp[2];
13875
13876       /* ix86_expand_vector_move_misalign() does not like constants ... */
13877       if (CONSTANT_P (op1)
13878           || (GET_CODE (op1) == SUBREG
13879               && CONSTANT_P (SUBREG_REG (op1))))
13880         op1 = validize_mem (force_const_mem (mode, op1));
13881
13882       /* ... nor both arguments in memory.  */
13883       if (!register_operand (op0, mode)
13884           && !register_operand (op1, mode))
13885         op1 = force_reg (mode, op1);
13886
13887       tmp[0] = op0; tmp[1] = op1;
13888       ix86_expand_vector_move_misalign (mode, tmp);
13889       return;
13890     }
13891
13892   /* Make operand1 a register if it isn't already.  */
13893   if (can_create_pseudo_p ()
13894       && !register_operand (op0, mode)
13895       && !register_operand (op1, mode))
13896     {
13897       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
13898       return;
13899     }
13900
13901   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
13902 }
13903
13904 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
13905    straight to ix86_expand_vector_move.  */
13906 /* Code generation for scalar reg-reg moves of single and double precision data:
13907      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
13908        movaps reg, reg
13909      else
13910        movss reg, reg
13911      if (x86_sse_partial_reg_dependency == true)
13912        movapd reg, reg
13913      else
13914        movsd reg, reg
13915
13916    Code generation for scalar loads of double precision data:
13917      if (x86_sse_split_regs == true)
13918        movlpd mem, reg      (gas syntax)
13919      else
13920        movsd mem, reg
13921
13922    Code generation for unaligned packed loads of single precision data
13923    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
13924      if (x86_sse_unaligned_move_optimal)
13925        movups mem, reg
13926
13927      if (x86_sse_partial_reg_dependency == true)
13928        {
13929          xorps  reg, reg
13930          movlps mem, reg
13931          movhps mem+8, reg
13932        }
13933      else
13934        {
13935          movlps mem, reg
13936          movhps mem+8, reg
13937        }
13938
13939    Code generation for unaligned packed loads of double precision data
13940    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
13941      if (x86_sse_unaligned_move_optimal)
13942        movupd mem, reg
13943
13944      if (x86_sse_split_regs == true)
13945        {
13946          movlpd mem, reg
13947          movhpd mem+8, reg
13948        }
13949      else
13950        {
13951          movsd  mem, reg
13952          movhpd mem+8, reg
13953        }
13954  */
13955
13956 void
13957 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
13958 {
13959   rtx op0, op1, m;
13960
13961   op0 = operands[0];
13962   op1 = operands[1];
13963
13964   if (TARGET_AVX)
13965     {
13966       switch (GET_MODE_CLASS (mode))
13967         {
13968         case MODE_VECTOR_INT:
13969         case MODE_INT:
13970           switch (GET_MODE_SIZE (mode))
13971             {
13972             case 16:
13973               /*  If we're optimizing for size, movups is the smallest.  */
13974               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
13975                 {
13976                   op0 = gen_lowpart (V4SFmode, op0);
13977                   op1 = gen_lowpart (V4SFmode, op1);
13978                   emit_insn (gen_avx_movups (op0, op1));
13979                   return;
13980                 }
13981               op0 = gen_lowpart (V16QImode, op0);
13982               op1 = gen_lowpart (V16QImode, op1);
13983               emit_insn (gen_avx_movdqu (op0, op1));
13984               break;
13985             case 32:
13986               op0 = gen_lowpart (V32QImode, op0);
13987               op1 = gen_lowpart (V32QImode, op1);
13988               emit_insn (gen_avx_movdqu256 (op0, op1));
13989               break;
13990             default:
13991               gcc_unreachable ();
13992             }
13993           break;
13994         case MODE_VECTOR_FLOAT:
13995           op0 = gen_lowpart (mode, op0);
13996           op1 = gen_lowpart (mode, op1);
13997
13998           switch (mode)
13999             {
14000             case V4SFmode:
14001               emit_insn (gen_avx_movups (op0, op1));
14002               break;
14003             case V8SFmode:
14004               emit_insn (gen_avx_movups256 (op0, op1));
14005               break;
14006             case V2DFmode:
14007               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14008                 {
14009                   op0 = gen_lowpart (V4SFmode, op0);
14010                   op1 = gen_lowpart (V4SFmode, op1);
14011                   emit_insn (gen_avx_movups (op0, op1));
14012                   return;
14013                 }
14014               emit_insn (gen_avx_movupd (op0, op1));
14015               break;
14016             case V4DFmode:
14017               emit_insn (gen_avx_movupd256 (op0, op1));
14018               break;
14019             default:
14020               gcc_unreachable ();
14021             }
14022           break;
14023
14024         default:
14025           gcc_unreachable ();
14026         }
14027
14028       return;
14029     }
14030
14031   if (MEM_P (op1))
14032     {
14033       /* If we're optimizing for size, movups is the smallest.  */
14034       if (optimize_insn_for_size_p ()
14035           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14036         {
14037           op0 = gen_lowpart (V4SFmode, op0);
14038           op1 = gen_lowpart (V4SFmode, op1);
14039           emit_insn (gen_sse_movups (op0, op1));
14040           return;
14041         }
14042
14043       /* ??? If we have typed data, then it would appear that using
14044          movdqu is the only way to get unaligned data loaded with
14045          integer type.  */
14046       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14047         {
14048           op0 = gen_lowpart (V16QImode, op0);
14049           op1 = gen_lowpart (V16QImode, op1);
14050           emit_insn (gen_sse2_movdqu (op0, op1));
14051           return;
14052         }
14053
14054       if (TARGET_SSE2 && mode == V2DFmode)
14055         {
14056           rtx zero;
14057
14058           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
14059             {
14060               op0 = gen_lowpart (V2DFmode, op0);
14061               op1 = gen_lowpart (V2DFmode, op1);
14062               emit_insn (gen_sse2_movupd (op0, op1));
14063               return;
14064             }
14065
14066           /* When SSE registers are split into halves, we can avoid
14067              writing to the top half twice.  */
14068           if (TARGET_SSE_SPLIT_REGS)
14069             {
14070               emit_clobber (op0);
14071               zero = op0;
14072             }
14073           else
14074             {
14075               /* ??? Not sure about the best option for the Intel chips.
14076                  The following would seem to satisfy; the register is
14077                  entirely cleared, breaking the dependency chain.  We
14078                  then store to the upper half, with a dependency depth
14079                  of one.  A rumor has it that Intel recommends two movsd
14080                  followed by an unpacklpd, but this is unconfirmed.  And
14081                  given that the dependency depth of the unpacklpd would
14082                  still be one, I'm not sure why this would be better.  */
14083               zero = CONST0_RTX (V2DFmode);
14084             }
14085
14086           m = adjust_address (op1, DFmode, 0);
14087           emit_insn (gen_sse2_loadlpd (op0, zero, m));
14088           m = adjust_address (op1, DFmode, 8);
14089           emit_insn (gen_sse2_loadhpd (op0, op0, m));
14090         }
14091       else
14092         {
14093           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
14094             {
14095               op0 = gen_lowpart (V4SFmode, op0);
14096               op1 = gen_lowpart (V4SFmode, op1);
14097               emit_insn (gen_sse_movups (op0, op1));
14098               return;
14099             }
14100
14101           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
14102             emit_move_insn (op0, CONST0_RTX (mode));
14103           else
14104             emit_clobber (op0);
14105
14106           if (mode != V4SFmode)
14107             op0 = gen_lowpart (V4SFmode, op0);
14108           m = adjust_address (op1, V2SFmode, 0);
14109           emit_insn (gen_sse_loadlps (op0, op0, m));
14110           m = adjust_address (op1, V2SFmode, 8);
14111           emit_insn (gen_sse_loadhps (op0, op0, m));
14112         }
14113     }
14114   else if (MEM_P (op0))
14115     {
14116       /* If we're optimizing for size, movups is the smallest.  */
14117       if (optimize_insn_for_size_p ()
14118           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14119         {
14120           op0 = gen_lowpart (V4SFmode, op0);
14121           op1 = gen_lowpart (V4SFmode, op1);
14122           emit_insn (gen_sse_movups (op0, op1));
14123           return;
14124         }
14125
14126       /* ??? Similar to above, only less clear because of quote
14127          typeless stores unquote.  */
14128       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
14129           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14130         {
14131           op0 = gen_lowpart (V16QImode, op0);
14132           op1 = gen_lowpart (V16QImode, op1);
14133           emit_insn (gen_sse2_movdqu (op0, op1));
14134           return;
14135         }
14136
14137       if (TARGET_SSE2 && mode == V2DFmode)
14138         {
14139           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
14140             {
14141               op0 = gen_lowpart (V2DFmode, op0);
14142               op1 = gen_lowpart (V2DFmode, op1);
14143               emit_insn (gen_sse2_movupd (op0, op1));
14144             }
14145           else
14146             {
14147               m = adjust_address (op0, DFmode, 0);
14148               emit_insn (gen_sse2_storelpd (m, op1));
14149               m = adjust_address (op0, DFmode, 8);
14150               emit_insn (gen_sse2_storehpd (m, op1));
14151             }
14152         }
14153       else
14154         {
14155           if (mode != V4SFmode)
14156             op1 = gen_lowpart (V4SFmode, op1);
14157
14158           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
14159             {
14160               op0 = gen_lowpart (V4SFmode, op0);
14161               emit_insn (gen_sse_movups (op0, op1));
14162             }
14163           else
14164             {
14165               m = adjust_address (op0, V2SFmode, 0);
14166               emit_insn (gen_sse_storelps (m, op1));
14167               m = adjust_address (op0, V2SFmode, 8);
14168               emit_insn (gen_sse_storehps (m, op1));
14169             }
14170         }
14171     }
14172   else
14173     gcc_unreachable ();
14174 }
14175
14176 /* Expand a push in MODE.  This is some mode for which we do not support
14177    proper push instructions, at least from the registers that we expect
14178    the value to live in.  */
14179
14180 void
14181 ix86_expand_push (enum machine_mode mode, rtx x)
14182 {
14183   rtx tmp;
14184
14185   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
14186                              GEN_INT (-GET_MODE_SIZE (mode)),
14187                              stack_pointer_rtx, 1, OPTAB_DIRECT);
14188   if (tmp != stack_pointer_rtx)
14189     emit_move_insn (stack_pointer_rtx, tmp);
14190
14191   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
14192
14193   /* When we push an operand onto stack, it has to be aligned at least
14194      at the function argument boundary.  However since we don't have
14195      the argument type, we can't determine the actual argument
14196      boundary.  */
14197   emit_move_insn (tmp, x);
14198 }
14199
14200 /* Helper function of ix86_fixup_binary_operands to canonicalize
14201    operand order.  Returns true if the operands should be swapped.  */
14202
14203 static bool
14204 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
14205                              rtx operands[])
14206 {
14207   rtx dst = operands[0];
14208   rtx src1 = operands[1];
14209   rtx src2 = operands[2];
14210
14211   /* If the operation is not commutative, we can't do anything.  */
14212   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
14213     return false;
14214
14215   /* Highest priority is that src1 should match dst.  */
14216   if (rtx_equal_p (dst, src1))
14217     return false;
14218   if (rtx_equal_p (dst, src2))
14219     return true;
14220
14221   /* Next highest priority is that immediate constants come second.  */
14222   if (immediate_operand (src2, mode))
14223     return false;
14224   if (immediate_operand (src1, mode))
14225     return true;
14226
14227   /* Lowest priority is that memory references should come second.  */
14228   if (MEM_P (src2))
14229     return false;
14230   if (MEM_P (src1))
14231     return true;
14232
14233   return false;
14234 }
14235
14236
14237 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
14238    destination to use for the operation.  If different from the true
14239    destination in operands[0], a copy operation will be required.  */
14240
14241 rtx
14242 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
14243                             rtx operands[])
14244 {
14245   rtx dst = operands[0];
14246   rtx src1 = operands[1];
14247   rtx src2 = operands[2];
14248
14249   /* Canonicalize operand order.  */
14250   if (ix86_swap_binary_operands_p (code, mode, operands))
14251     {
14252       rtx temp;
14253
14254       /* It is invalid to swap operands of different modes.  */
14255       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
14256
14257       temp = src1;
14258       src1 = src2;
14259       src2 = temp;
14260     }
14261
14262   /* Both source operands cannot be in memory.  */
14263   if (MEM_P (src1) && MEM_P (src2))
14264     {
14265       /* Optimization: Only read from memory once.  */
14266       if (rtx_equal_p (src1, src2))
14267         {
14268           src2 = force_reg (mode, src2);
14269           src1 = src2;
14270         }
14271       else
14272         src2 = force_reg (mode, src2);
14273     }
14274
14275   /* If the destination is memory, and we do not have matching source
14276      operands, do things in registers.  */
14277   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
14278     dst = gen_reg_rtx (mode);
14279
14280   /* Source 1 cannot be a constant.  */
14281   if (CONSTANT_P (src1))
14282     src1 = force_reg (mode, src1);
14283
14284   /* Source 1 cannot be a non-matching memory.  */
14285   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
14286     src1 = force_reg (mode, src1);
14287
14288   operands[1] = src1;
14289   operands[2] = src2;
14290   return dst;
14291 }
14292
14293 /* Similarly, but assume that the destination has already been
14294    set up properly.  */
14295
14296 void
14297 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
14298                                     enum machine_mode mode, rtx operands[])
14299 {
14300   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
14301   gcc_assert (dst == operands[0]);
14302 }
14303
14304 /* Attempt to expand a binary operator.  Make the expansion closer to the
14305    actual machine, then just general_operand, which will allow 3 separate
14306    memory references (one output, two input) in a single insn.  */
14307
14308 void
14309 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
14310                              rtx operands[])
14311 {
14312   rtx src1, src2, dst, op, clob;
14313
14314   dst = ix86_fixup_binary_operands (code, mode, operands);
14315   src1 = operands[1];
14316   src2 = operands[2];
14317
14318  /* Emit the instruction.  */
14319
14320   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
14321   if (reload_in_progress)
14322     {
14323       /* Reload doesn't know about the flags register, and doesn't know that
14324          it doesn't want to clobber it.  We can only do this with PLUS.  */
14325       gcc_assert (code == PLUS);
14326       emit_insn (op);
14327     }
14328   else
14329     {
14330       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
14331       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
14332     }
14333
14334   /* Fix up the destination if needed.  */
14335   if (dst != operands[0])
14336     emit_move_insn (operands[0], dst);
14337 }
14338
14339 /* Return TRUE or FALSE depending on whether the binary operator meets the
14340    appropriate constraints.  */
14341
14342 int
14343 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
14344                          rtx operands[3])
14345 {
14346   rtx dst = operands[0];
14347   rtx src1 = operands[1];
14348   rtx src2 = operands[2];
14349
14350   /* Both source operands cannot be in memory.  */
14351   if (MEM_P (src1) && MEM_P (src2))
14352     return 0;
14353
14354   /* Canonicalize operand order for commutative operators.  */
14355   if (ix86_swap_binary_operands_p (code, mode, operands))
14356     {
14357       rtx temp = src1;
14358       src1 = src2;
14359       src2 = temp;
14360     }
14361
14362   /* If the destination is memory, we must have a matching source operand.  */
14363   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
14364       return 0;
14365
14366   /* Source 1 cannot be a constant.  */
14367   if (CONSTANT_P (src1))
14368     return 0;
14369
14370   /* Source 1 cannot be a non-matching memory.  */
14371   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
14372     return 0;
14373
14374   return 1;
14375 }
14376
14377 /* Attempt to expand a unary operator.  Make the expansion closer to the
14378    actual machine, then just general_operand, which will allow 2 separate
14379    memory references (one output, one input) in a single insn.  */
14380
14381 void
14382 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
14383                             rtx operands[])
14384 {
14385   int matching_memory;
14386   rtx src, dst, op, clob;
14387
14388   dst = operands[0];
14389   src = operands[1];
14390
14391   /* If the destination is memory, and we do not have matching source
14392      operands, do things in registers.  */
14393   matching_memory = 0;
14394   if (MEM_P (dst))
14395     {
14396       if (rtx_equal_p (dst, src))
14397         matching_memory = 1;
14398       else
14399         dst = gen_reg_rtx (mode);
14400     }
14401
14402   /* When source operand is memory, destination must match.  */
14403   if (MEM_P (src) && !matching_memory)
14404     src = force_reg (mode, src);
14405
14406   /* Emit the instruction.  */
14407
14408   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
14409   if (reload_in_progress || code == NOT)
14410     {
14411       /* Reload doesn't know about the flags register, and doesn't know that
14412          it doesn't want to clobber it.  */
14413       gcc_assert (code == NOT);
14414       emit_insn (op);
14415     }
14416   else
14417     {
14418       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
14419       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
14420     }
14421
14422   /* Fix up the destination if needed.  */
14423   if (dst != operands[0])
14424     emit_move_insn (operands[0], dst);
14425 }
14426
14427 #define LEA_SEARCH_THRESHOLD 12
14428
14429 /* Search backward for non-agu definition of register number REGNO1
14430    or register number REGNO2 in INSN's basic block until
14431    1. Pass LEA_SEARCH_THRESHOLD instructions, or
14432    2. Reach BB boundary, or
14433    3. Reach agu definition.
14434    Returns the distance between the non-agu definition point and INSN.
14435    If no definition point, returns -1.  */
14436
14437 static int
14438 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
14439                          rtx insn)
14440 {
14441   basic_block bb = BLOCK_FOR_INSN (insn);
14442   int distance = 0;
14443   df_ref *def_rec;
14444   enum attr_type insn_type;
14445
14446   if (insn != BB_HEAD (bb))
14447     {
14448       rtx prev = PREV_INSN (insn);
14449       while (prev && distance < LEA_SEARCH_THRESHOLD)
14450         {
14451           if (NONDEBUG_INSN_P (prev))
14452             {
14453               distance++;
14454               for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
14455                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
14456                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
14457                     && (regno1 == DF_REF_REGNO (*def_rec)
14458                         || regno2 == DF_REF_REGNO (*def_rec)))
14459                   {
14460                     insn_type = get_attr_type (prev);
14461                     if (insn_type != TYPE_LEA)
14462                       goto done;
14463                   }
14464             }
14465           if (prev == BB_HEAD (bb))
14466             break;
14467           prev = PREV_INSN (prev);
14468         }
14469     }
14470
14471   if (distance < LEA_SEARCH_THRESHOLD)
14472     {
14473       edge e;
14474       edge_iterator ei;
14475       bool simple_loop = false;
14476
14477       FOR_EACH_EDGE (e, ei, bb->preds)
14478         if (e->src == bb)
14479           {
14480             simple_loop = true;
14481             break;
14482           }
14483
14484       if (simple_loop)
14485         {
14486           rtx prev = BB_END (bb);
14487           while (prev
14488                  && prev != insn
14489                  && distance < LEA_SEARCH_THRESHOLD)
14490             {
14491               if (NONDEBUG_INSN_P (prev))
14492                 {
14493                   distance++;
14494                   for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
14495                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
14496                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
14497                         && (regno1 == DF_REF_REGNO (*def_rec)
14498                             || regno2 == DF_REF_REGNO (*def_rec)))
14499                       {
14500                         insn_type = get_attr_type (prev);
14501                         if (insn_type != TYPE_LEA)
14502                           goto done;
14503                       }
14504                 }
14505               prev = PREV_INSN (prev);
14506             }
14507         }
14508     }
14509
14510   distance = -1;
14511
14512 done:
14513   /* get_attr_type may modify recog data.  We want to make sure
14514      that recog data is valid for instruction INSN, on which
14515      distance_non_agu_define is called.  INSN is unchanged here.  */
14516   extract_insn_cached (insn);
14517   return distance;
14518 }
14519
14520 /* Return the distance between INSN and the next insn that uses
14521    register number REGNO0 in memory address.  Return -1 if no such
14522    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
14523
14524 static int
14525 distance_agu_use (unsigned int regno0, rtx insn)
14526 {
14527   basic_block bb = BLOCK_FOR_INSN (insn);
14528   int distance = 0;
14529   df_ref *def_rec;
14530   df_ref *use_rec;
14531
14532   if (insn != BB_END (bb))
14533     {
14534       rtx next = NEXT_INSN (insn);
14535       while (next && distance < LEA_SEARCH_THRESHOLD)
14536         {
14537           if (NONDEBUG_INSN_P (next))
14538             {
14539               distance++;
14540
14541               for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
14542                 if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
14543                      || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
14544                     && regno0 == DF_REF_REGNO (*use_rec))
14545                   {
14546                     /* Return DISTANCE if OP0 is used in memory
14547                        address in NEXT.  */
14548                     return distance;
14549                   }
14550
14551               for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
14552                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
14553                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
14554                     && regno0 == DF_REF_REGNO (*def_rec))
14555                   {
14556                     /* Return -1 if OP0 is set in NEXT.  */
14557                     return -1;
14558                   }
14559             }
14560           if (next == BB_END (bb))
14561             break;
14562           next = NEXT_INSN (next);
14563         }
14564     }
14565
14566   if (distance < LEA_SEARCH_THRESHOLD)
14567     {
14568       edge e;
14569       edge_iterator ei;
14570       bool simple_loop = false;
14571
14572       FOR_EACH_EDGE (e, ei, bb->succs)
14573         if (e->dest == bb)
14574           {
14575             simple_loop = true;
14576             break;
14577           }
14578
14579       if (simple_loop)
14580         {
14581           rtx next = BB_HEAD (bb);
14582           while (next
14583                  && next != insn
14584                  && distance < LEA_SEARCH_THRESHOLD)
14585             {
14586               if (NONDEBUG_INSN_P (next))
14587                 {
14588                   distance++;
14589
14590                   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
14591                     if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
14592                          || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
14593                         && regno0 == DF_REF_REGNO (*use_rec))
14594                       {
14595                         /* Return DISTANCE if OP0 is used in memory
14596                            address in NEXT.  */
14597                         return distance;
14598                       }
14599
14600                   for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
14601                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
14602                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
14603                         && regno0 == DF_REF_REGNO (*def_rec))
14604                       {
14605                         /* Return -1 if OP0 is set in NEXT.  */
14606                         return -1;
14607                       }
14608
14609                 }
14610               next = NEXT_INSN (next);
14611             }
14612         }
14613     }
14614
14615   return -1;
14616 }
14617
14618 /* Define this macro to tune LEA priority vs ADD, it take effect when
14619    there is a dilemma of choicing LEA or ADD
14620    Negative value: ADD is more preferred than LEA
14621    Zero: Netrual
14622    Positive value: LEA is more preferred than ADD*/
14623 #define IX86_LEA_PRIORITY 2
14624
14625 /* Return true if it is ok to optimize an ADD operation to LEA
14626    operation to avoid flag register consumation.  For the processors
14627    like ATOM, if the destination register of LEA holds an actual
14628    address which will be used soon, LEA is better and otherwise ADD
14629    is better.  */
14630
14631 bool
14632 ix86_lea_for_add_ok (enum rtx_code code ATTRIBUTE_UNUSED,
14633                      rtx insn, rtx operands[])
14634 {
14635   unsigned int regno0 = true_regnum (operands[0]);
14636   unsigned int regno1 = true_regnum (operands[1]);
14637   unsigned int regno2;
14638
14639   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
14640     return regno0 != regno1;
14641
14642   regno2 = true_regnum (operands[2]);
14643
14644   /* If a = b + c, (a!=b && a!=c), must use lea form. */
14645   if (regno0 != regno1 && regno0 != regno2)
14646     return true;
14647   else
14648     {
14649       int dist_define, dist_use;
14650       dist_define = distance_non_agu_define (regno1, regno2, insn);
14651       if (dist_define <= 0)
14652         return true;
14653
14654       /* If this insn has both backward non-agu dependence and forward
14655          agu dependence, the one with short distance take effect. */
14656       dist_use = distance_agu_use (regno0, insn);
14657       if (dist_use <= 0
14658           || (dist_define + IX86_LEA_PRIORITY) < dist_use)
14659         return false;
14660
14661       return true;
14662     }
14663 }
14664
14665 /* Return true if destination reg of SET_BODY is shift count of
14666    USE_BODY.  */
14667
14668 static bool
14669 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
14670 {
14671   rtx set_dest;
14672   rtx shift_rtx;
14673   int i;
14674
14675   /* Retrieve destination of SET_BODY.  */
14676   switch (GET_CODE (set_body))
14677     {
14678     case SET:
14679       set_dest = SET_DEST (set_body);
14680       if (!set_dest || !REG_P (set_dest))
14681         return false;
14682       break;
14683     case PARALLEL:
14684       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
14685         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
14686                                           use_body))
14687           return true;
14688     default:
14689       return false;
14690       break;
14691     }
14692
14693   /* Retrieve shift count of USE_BODY.  */
14694   switch (GET_CODE (use_body))
14695     {
14696     case SET:
14697       shift_rtx = XEXP (use_body, 1);
14698       break;
14699     case PARALLEL:
14700       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
14701         if (ix86_dep_by_shift_count_body (set_body,
14702                                           XVECEXP (use_body, 0, i)))
14703           return true;
14704     default:
14705       return false;
14706       break;
14707     }
14708
14709   if (shift_rtx
14710       && (GET_CODE (shift_rtx) == ASHIFT
14711           || GET_CODE (shift_rtx) == LSHIFTRT
14712           || GET_CODE (shift_rtx) == ASHIFTRT
14713           || GET_CODE (shift_rtx) == ROTATE
14714           || GET_CODE (shift_rtx) == ROTATERT))
14715     {
14716       rtx shift_count = XEXP (shift_rtx, 1);
14717
14718       /* Return true if shift count is dest of SET_BODY.  */
14719       if (REG_P (shift_count)
14720           && true_regnum (set_dest) == true_regnum (shift_count))
14721         return true;
14722     }
14723
14724   return false;
14725 }
14726
14727 /* Return true if destination reg of SET_INSN is shift count of
14728    USE_INSN.  */
14729
14730 bool
14731 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
14732 {
14733   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
14734                                        PATTERN (use_insn));
14735 }
14736
14737 /* Return TRUE or FALSE depending on whether the unary operator meets the
14738    appropriate constraints.  */
14739
14740 int
14741 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
14742                         enum machine_mode mode ATTRIBUTE_UNUSED,
14743                         rtx operands[2] ATTRIBUTE_UNUSED)
14744 {
14745   /* If one of operands is memory, source and destination must match.  */
14746   if ((MEM_P (operands[0])
14747        || MEM_P (operands[1]))
14748       && ! rtx_equal_p (operands[0], operands[1]))
14749     return FALSE;
14750   return TRUE;
14751 }
14752
14753 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
14754    are ok, keeping in mind the possible movddup alternative.  */
14755
14756 bool
14757 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
14758 {
14759   if (MEM_P (operands[0]))
14760     return rtx_equal_p (operands[0], operands[1 + high]);
14761   if (MEM_P (operands[1]) && MEM_P (operands[2]))
14762     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
14763   return true;
14764 }
14765
14766 /* Post-reload splitter for converting an SF or DFmode value in an
14767    SSE register into an unsigned SImode.  */
14768
14769 void
14770 ix86_split_convert_uns_si_sse (rtx operands[])
14771 {
14772   enum machine_mode vecmode;
14773   rtx value, large, zero_or_two31, input, two31, x;
14774
14775   large = operands[1];
14776   zero_or_two31 = operands[2];
14777   input = operands[3];
14778   two31 = operands[4];
14779   vecmode = GET_MODE (large);
14780   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
14781
14782   /* Load up the value into the low element.  We must ensure that the other
14783      elements are valid floats -- zero is the easiest such value.  */
14784   if (MEM_P (input))
14785     {
14786       if (vecmode == V4SFmode)
14787         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
14788       else
14789         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
14790     }
14791   else
14792     {
14793       input = gen_rtx_REG (vecmode, REGNO (input));
14794       emit_move_insn (value, CONST0_RTX (vecmode));
14795       if (vecmode == V4SFmode)
14796         emit_insn (gen_sse_movss (value, value, input));
14797       else
14798         emit_insn (gen_sse2_movsd (value, value, input));
14799     }
14800
14801   emit_move_insn (large, two31);
14802   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
14803
14804   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
14805   emit_insn (gen_rtx_SET (VOIDmode, large, x));
14806
14807   x = gen_rtx_AND (vecmode, zero_or_two31, large);
14808   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
14809
14810   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
14811   emit_insn (gen_rtx_SET (VOIDmode, value, x));
14812
14813   large = gen_rtx_REG (V4SImode, REGNO (large));
14814   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
14815
14816   x = gen_rtx_REG (V4SImode, REGNO (value));
14817   if (vecmode == V4SFmode)
14818     emit_insn (gen_sse2_cvttps2dq (x, value));
14819   else
14820     emit_insn (gen_sse2_cvttpd2dq (x, value));
14821   value = x;
14822
14823   emit_insn (gen_xorv4si3 (value, value, large));
14824 }
14825
14826 /* Convert an unsigned DImode value into a DFmode, using only SSE.
14827    Expects the 64-bit DImode to be supplied in a pair of integral
14828    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
14829    -mfpmath=sse, !optimize_size only.  */
14830
14831 void
14832 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
14833 {
14834   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
14835   rtx int_xmm, fp_xmm;
14836   rtx biases, exponents;
14837   rtx x;
14838
14839   int_xmm = gen_reg_rtx (V4SImode);
14840   if (TARGET_INTER_UNIT_MOVES)
14841     emit_insn (gen_movdi_to_sse (int_xmm, input));
14842   else if (TARGET_SSE_SPLIT_REGS)
14843     {
14844       emit_clobber (int_xmm);
14845       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
14846     }
14847   else
14848     {
14849       x = gen_reg_rtx (V2DImode);
14850       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
14851       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
14852     }
14853
14854   x = gen_rtx_CONST_VECTOR (V4SImode,
14855                             gen_rtvec (4, GEN_INT (0x43300000UL),
14856                                        GEN_INT (0x45300000UL),
14857                                        const0_rtx, const0_rtx));
14858   exponents = validize_mem (force_const_mem (V4SImode, x));
14859
14860   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
14861   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
14862
14863   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
14864      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
14865      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
14866      (0x1.0p84 + double(fp_value_hi_xmm)).
14867      Note these exponents differ by 32.  */
14868
14869   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
14870
14871   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
14872      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
14873   real_ldexp (&bias_lo_rvt, &dconst1, 52);
14874   real_ldexp (&bias_hi_rvt, &dconst1, 84);
14875   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
14876   x = const_double_from_real_value (bias_hi_rvt, DFmode);
14877   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
14878   biases = validize_mem (force_const_mem (V2DFmode, biases));
14879   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
14880
14881   /* Add the upper and lower DFmode values together.  */
14882   if (TARGET_SSE3)
14883     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
14884   else
14885     {
14886       x = copy_to_mode_reg (V2DFmode, fp_xmm);
14887       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
14888       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
14889     }
14890
14891   ix86_expand_vector_extract (false, target, fp_xmm, 0);
14892 }
14893
14894 /* Not used, but eases macroization of patterns.  */
14895 void
14896 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
14897                                   rtx input ATTRIBUTE_UNUSED)
14898 {
14899   gcc_unreachable ();
14900 }
14901
14902 /* Convert an unsigned SImode value into a DFmode.  Only currently used
14903    for SSE, but applicable anywhere.  */
14904
14905 void
14906 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
14907 {
14908   REAL_VALUE_TYPE TWO31r;
14909   rtx x, fp;
14910
14911   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
14912                            NULL, 1, OPTAB_DIRECT);
14913
14914   fp = gen_reg_rtx (DFmode);
14915   emit_insn (gen_floatsidf2 (fp, x));
14916
14917   real_ldexp (&TWO31r, &dconst1, 31);
14918   x = const_double_from_real_value (TWO31r, DFmode);
14919
14920   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
14921   if (x != target)
14922     emit_move_insn (target, x);
14923 }
14924
14925 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
14926    32-bit mode; otherwise we have a direct convert instruction.  */
14927
14928 void
14929 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
14930 {
14931   REAL_VALUE_TYPE TWO32r;
14932   rtx fp_lo, fp_hi, x;
14933
14934   fp_lo = gen_reg_rtx (DFmode);
14935   fp_hi = gen_reg_rtx (DFmode);
14936
14937   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
14938
14939   real_ldexp (&TWO32r, &dconst1, 32);
14940   x = const_double_from_real_value (TWO32r, DFmode);
14941   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
14942
14943   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
14944
14945   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
14946                            0, OPTAB_DIRECT);
14947   if (x != target)
14948     emit_move_insn (target, x);
14949 }
14950
14951 /* Convert an unsigned SImode value into a SFmode, using only SSE.
14952    For x86_32, -mfpmath=sse, !optimize_size only.  */
14953 void
14954 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
14955 {
14956   REAL_VALUE_TYPE ONE16r;
14957   rtx fp_hi, fp_lo, int_hi, int_lo, x;
14958
14959   real_ldexp (&ONE16r, &dconst1, 16);
14960   x = const_double_from_real_value (ONE16r, SFmode);
14961   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
14962                                       NULL, 0, OPTAB_DIRECT);
14963   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
14964                                       NULL, 0, OPTAB_DIRECT);
14965   fp_hi = gen_reg_rtx (SFmode);
14966   fp_lo = gen_reg_rtx (SFmode);
14967   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
14968   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
14969   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
14970                                0, OPTAB_DIRECT);
14971   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
14972                                0, OPTAB_DIRECT);
14973   if (!rtx_equal_p (target, fp_hi))
14974     emit_move_insn (target, fp_hi);
14975 }
14976
14977 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
14978    then replicate the value for all elements of the vector
14979    register.  */
14980
14981 rtx
14982 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
14983 {
14984   rtvec v;
14985   switch (mode)
14986     {
14987     case SImode:
14988       gcc_assert (vect);
14989       v = gen_rtvec (4, value, value, value, value);
14990       return gen_rtx_CONST_VECTOR (V4SImode, v);
14991
14992     case DImode:
14993       gcc_assert (vect);
14994       v = gen_rtvec (2, value, value);
14995       return gen_rtx_CONST_VECTOR (V2DImode, v);
14996
14997     case SFmode:
14998       if (vect)
14999         v = gen_rtvec (4, value, value, value, value);
15000       else
15001         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
15002                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
15003       return gen_rtx_CONST_VECTOR (V4SFmode, v);
15004
15005     case DFmode:
15006       if (vect)
15007         v = gen_rtvec (2, value, value);
15008       else
15009         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
15010       return gen_rtx_CONST_VECTOR (V2DFmode, v);
15011
15012     default:
15013       gcc_unreachable ();
15014     }
15015 }
15016
15017 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
15018    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
15019    for an SSE register.  If VECT is true, then replicate the mask for
15020    all elements of the vector register.  If INVERT is true, then create
15021    a mask excluding the sign bit.  */
15022
15023 rtx
15024 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
15025 {
15026   enum machine_mode vec_mode, imode;
15027   HOST_WIDE_INT hi, lo;
15028   int shift = 63;
15029   rtx v;
15030   rtx mask;
15031
15032   /* Find the sign bit, sign extended to 2*HWI.  */
15033   switch (mode)
15034     {
15035     case SImode:
15036     case SFmode:
15037       imode = SImode;
15038       vec_mode = (mode == SImode) ? V4SImode : V4SFmode;
15039       lo = 0x80000000, hi = lo < 0;
15040       break;
15041
15042     case DImode:
15043     case DFmode:
15044       imode = DImode;
15045       vec_mode = (mode == DImode) ? V2DImode : V2DFmode;
15046       if (HOST_BITS_PER_WIDE_INT >= 64)
15047         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
15048       else
15049         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
15050       break;
15051
15052     case TImode:
15053     case TFmode:
15054       vec_mode = VOIDmode;
15055       if (HOST_BITS_PER_WIDE_INT >= 64)
15056         {
15057           imode = TImode;
15058           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
15059         }
15060       else
15061         {
15062           rtvec vec;
15063
15064           imode = DImode;
15065           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
15066
15067           if (invert)
15068             {
15069               lo = ~lo, hi = ~hi;
15070               v = constm1_rtx;
15071             }
15072           else
15073             v = const0_rtx;
15074
15075           mask = immed_double_const (lo, hi, imode);
15076
15077           vec = gen_rtvec (2, v, mask);
15078           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
15079           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
15080
15081           return v;
15082         }
15083      break;
15084
15085     default:
15086       gcc_unreachable ();
15087     }
15088
15089   if (invert)
15090     lo = ~lo, hi = ~hi;
15091
15092   /* Force this value into the low part of a fp vector constant.  */
15093   mask = immed_double_const (lo, hi, imode);
15094   mask = gen_lowpart (mode, mask);
15095
15096   if (vec_mode == VOIDmode)
15097     return force_reg (mode, mask);
15098
15099   v = ix86_build_const_vector (mode, vect, mask);
15100   return force_reg (vec_mode, v);
15101 }
15102
15103 /* Generate code for floating point ABS or NEG.  */
15104
15105 void
15106 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
15107                                 rtx operands[])
15108 {
15109   rtx mask, set, use, clob, dst, src;
15110   bool use_sse = false;
15111   bool vector_mode = VECTOR_MODE_P (mode);
15112   enum machine_mode elt_mode = mode;
15113
15114   if (vector_mode)
15115     {
15116       elt_mode = GET_MODE_INNER (mode);
15117       use_sse = true;
15118     }
15119   else if (mode == TFmode)
15120     use_sse = true;
15121   else if (TARGET_SSE_MATH)
15122     use_sse = SSE_FLOAT_MODE_P (mode);
15123
15124   /* NEG and ABS performed with SSE use bitwise mask operations.
15125      Create the appropriate mask now.  */
15126   if (use_sse)
15127     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
15128   else
15129     mask = NULL_RTX;
15130
15131   dst = operands[0];
15132   src = operands[1];
15133
15134   if (vector_mode)
15135     {
15136       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
15137       set = gen_rtx_SET (VOIDmode, dst, set);
15138       emit_insn (set);
15139     }
15140   else
15141     {
15142       set = gen_rtx_fmt_e (code, mode, src);
15143       set = gen_rtx_SET (VOIDmode, dst, set);
15144       if (mask)
15145         {
15146           use = gen_rtx_USE (VOIDmode, mask);
15147           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15148           emit_insn (gen_rtx_PARALLEL (VOIDmode,
15149                                        gen_rtvec (3, set, use, clob)));
15150         }
15151       else
15152         emit_insn (set);
15153     }
15154 }
15155
15156 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
15157
15158 void
15159 ix86_expand_copysign (rtx operands[])
15160 {
15161   enum machine_mode mode;
15162   rtx dest, op0, op1, mask, nmask;
15163
15164   dest = operands[0];
15165   op0 = operands[1];
15166   op1 = operands[2];
15167
15168   mode = GET_MODE (dest);
15169
15170   if (GET_CODE (op0) == CONST_DOUBLE)
15171     {
15172       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
15173
15174       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
15175         op0 = simplify_unary_operation (ABS, mode, op0, mode);
15176
15177       if (mode == SFmode || mode == DFmode)
15178         {
15179           enum machine_mode vmode;
15180
15181           vmode = mode == SFmode ? V4SFmode : V2DFmode;
15182
15183           if (op0 == CONST0_RTX (mode))
15184             op0 = CONST0_RTX (vmode);
15185           else
15186             {
15187               rtx v = ix86_build_const_vector (mode, false, op0);
15188
15189               op0 = force_reg (vmode, v);
15190             }
15191         }
15192       else if (op0 != CONST0_RTX (mode))
15193         op0 = force_reg (mode, op0);
15194
15195       mask = ix86_build_signbit_mask (mode, 0, 0);
15196
15197       if (mode == SFmode)
15198         copysign_insn = gen_copysignsf3_const;
15199       else if (mode == DFmode)
15200         copysign_insn = gen_copysigndf3_const;
15201       else
15202         copysign_insn = gen_copysigntf3_const;
15203
15204         emit_insn (copysign_insn (dest, op0, op1, mask));
15205     }
15206   else
15207     {
15208       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
15209
15210       nmask = ix86_build_signbit_mask (mode, 0, 1);
15211       mask = ix86_build_signbit_mask (mode, 0, 0);
15212
15213       if (mode == SFmode)
15214         copysign_insn = gen_copysignsf3_var;
15215       else if (mode == DFmode)
15216         copysign_insn = gen_copysigndf3_var;
15217       else
15218         copysign_insn = gen_copysigntf3_var;
15219
15220       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
15221     }
15222 }
15223
15224 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
15225    be a constant, and so has already been expanded into a vector constant.  */
15226
15227 void
15228 ix86_split_copysign_const (rtx operands[])
15229 {
15230   enum machine_mode mode, vmode;
15231   rtx dest, op0, mask, x;
15232
15233   dest = operands[0];
15234   op0 = operands[1];
15235   mask = operands[3];
15236
15237   mode = GET_MODE (dest);
15238   vmode = GET_MODE (mask);
15239
15240   dest = simplify_gen_subreg (vmode, dest, mode, 0);
15241   x = gen_rtx_AND (vmode, dest, mask);
15242   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
15243
15244   if (op0 != CONST0_RTX (vmode))
15245     {
15246       x = gen_rtx_IOR (vmode, dest, op0);
15247       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
15248     }
15249 }
15250
15251 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
15252    so we have to do two masks.  */
15253
15254 void
15255 ix86_split_copysign_var (rtx operands[])
15256 {
15257   enum machine_mode mode, vmode;
15258   rtx dest, scratch, op0, op1, mask, nmask, x;
15259
15260   dest = operands[0];
15261   scratch = operands[1];
15262   op0 = operands[2];
15263   op1 = operands[3];
15264   nmask = operands[4];
15265   mask = operands[5];
15266
15267   mode = GET_MODE (dest);
15268   vmode = GET_MODE (mask);
15269
15270   if (rtx_equal_p (op0, op1))
15271     {
15272       /* Shouldn't happen often (it's useless, obviously), but when it does
15273          we'd generate incorrect code if we continue below.  */
15274       emit_move_insn (dest, op0);
15275       return;
15276     }
15277
15278   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
15279     {
15280       gcc_assert (REGNO (op1) == REGNO (scratch));
15281
15282       x = gen_rtx_AND (vmode, scratch, mask);
15283       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
15284
15285       dest = mask;
15286       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
15287       x = gen_rtx_NOT (vmode, dest);
15288       x = gen_rtx_AND (vmode, x, op0);
15289       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
15290     }
15291   else
15292     {
15293       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
15294         {
15295           x = gen_rtx_AND (vmode, scratch, mask);
15296         }
15297       else                                              /* alternative 2,4 */
15298         {
15299           gcc_assert (REGNO (mask) == REGNO (scratch));
15300           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
15301           x = gen_rtx_AND (vmode, scratch, op1);
15302         }
15303       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
15304
15305       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
15306         {
15307           dest = simplify_gen_subreg (vmode, op0, mode, 0);
15308           x = gen_rtx_AND (vmode, dest, nmask);
15309         }
15310       else                                              /* alternative 3,4 */
15311         {
15312           gcc_assert (REGNO (nmask) == REGNO (dest));
15313           dest = nmask;
15314           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
15315           x = gen_rtx_AND (vmode, dest, op0);
15316         }
15317       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
15318     }
15319
15320   x = gen_rtx_IOR (vmode, dest, scratch);
15321   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
15322 }
15323
15324 /* Return TRUE or FALSE depending on whether the first SET in INSN
15325    has source and destination with matching CC modes, and that the
15326    CC mode is at least as constrained as REQ_MODE.  */
15327
15328 int
15329 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
15330 {
15331   rtx set;
15332   enum machine_mode set_mode;
15333
15334   set = PATTERN (insn);
15335   if (GET_CODE (set) == PARALLEL)
15336     set = XVECEXP (set, 0, 0);
15337   gcc_assert (GET_CODE (set) == SET);
15338   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
15339
15340   set_mode = GET_MODE (SET_DEST (set));
15341   switch (set_mode)
15342     {
15343     case CCNOmode:
15344       if (req_mode != CCNOmode
15345           && (req_mode != CCmode
15346               || XEXP (SET_SRC (set), 1) != const0_rtx))
15347         return 0;
15348       break;
15349     case CCmode:
15350       if (req_mode == CCGCmode)
15351         return 0;
15352       /* FALLTHRU */
15353     case CCGCmode:
15354       if (req_mode == CCGOCmode || req_mode == CCNOmode)
15355         return 0;
15356       /* FALLTHRU */
15357     case CCGOCmode:
15358       if (req_mode == CCZmode)
15359         return 0;
15360       /* FALLTHRU */
15361     case CCAmode:
15362     case CCCmode:
15363     case CCOmode:
15364     case CCSmode:
15365     case CCZmode:
15366       break;
15367
15368     default:
15369       gcc_unreachable ();
15370     }
15371
15372   return (GET_MODE (SET_SRC (set)) == set_mode);
15373 }
15374
15375 /* Generate insn patterns to do an integer compare of OPERANDS.  */
15376
15377 static rtx
15378 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
15379 {
15380   enum machine_mode cmpmode;
15381   rtx tmp, flags;
15382
15383   cmpmode = SELECT_CC_MODE (code, op0, op1);
15384   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
15385
15386   /* This is very simple, but making the interface the same as in the
15387      FP case makes the rest of the code easier.  */
15388   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
15389   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
15390
15391   /* Return the test that should be put into the flags user, i.e.
15392      the bcc, scc, or cmov instruction.  */
15393   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
15394 }
15395
15396 /* Figure out whether to use ordered or unordered fp comparisons.
15397    Return the appropriate mode to use.  */
15398
15399 enum machine_mode
15400 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
15401 {
15402   /* ??? In order to make all comparisons reversible, we do all comparisons
15403      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
15404      all forms trapping and nontrapping comparisons, we can make inequality
15405      comparisons trapping again, since it results in better code when using
15406      FCOM based compares.  */
15407   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
15408 }
15409
15410 enum machine_mode
15411 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
15412 {
15413   enum machine_mode mode = GET_MODE (op0);
15414
15415   if (SCALAR_FLOAT_MODE_P (mode))
15416     {
15417       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
15418       return ix86_fp_compare_mode (code);
15419     }
15420
15421   switch (code)
15422     {
15423       /* Only zero flag is needed.  */
15424     case EQ:                    /* ZF=0 */
15425     case NE:                    /* ZF!=0 */
15426       return CCZmode;
15427       /* Codes needing carry flag.  */
15428     case GEU:                   /* CF=0 */
15429     case LTU:                   /* CF=1 */
15430       /* Detect overflow checks.  They need just the carry flag.  */
15431       if (GET_CODE (op0) == PLUS
15432           && rtx_equal_p (op1, XEXP (op0, 0)))
15433         return CCCmode;
15434       else
15435         return CCmode;
15436     case GTU:                   /* CF=0 & ZF=0 */
15437     case LEU:                   /* CF=1 | ZF=1 */
15438       /* Detect overflow checks.  They need just the carry flag.  */
15439       if (GET_CODE (op0) == MINUS
15440           && rtx_equal_p (op1, XEXP (op0, 0)))
15441         return CCCmode;
15442       else
15443         return CCmode;
15444       /* Codes possibly doable only with sign flag when
15445          comparing against zero.  */
15446     case GE:                    /* SF=OF   or   SF=0 */
15447     case LT:                    /* SF<>OF  or   SF=1 */
15448       if (op1 == const0_rtx)
15449         return CCGOCmode;
15450       else
15451         /* For other cases Carry flag is not required.  */
15452         return CCGCmode;
15453       /* Codes doable only with sign flag when comparing
15454          against zero, but we miss jump instruction for it
15455          so we need to use relational tests against overflow
15456          that thus needs to be zero.  */
15457     case GT:                    /* ZF=0 & SF=OF */
15458     case LE:                    /* ZF=1 | SF<>OF */
15459       if (op1 == const0_rtx)
15460         return CCNOmode;
15461       else
15462         return CCGCmode;
15463       /* strcmp pattern do (use flags) and combine may ask us for proper
15464          mode.  */
15465     case USE:
15466       return CCmode;
15467     default:
15468       gcc_unreachable ();
15469     }
15470 }
15471
15472 /* Return the fixed registers used for condition codes.  */
15473
15474 static bool
15475 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
15476 {
15477   *p1 = FLAGS_REG;
15478   *p2 = FPSR_REG;
15479   return true;
15480 }
15481
15482 /* If two condition code modes are compatible, return a condition code
15483    mode which is compatible with both.  Otherwise, return
15484    VOIDmode.  */
15485
15486 static enum machine_mode
15487 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
15488 {
15489   if (m1 == m2)
15490     return m1;
15491
15492   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
15493     return VOIDmode;
15494
15495   if ((m1 == CCGCmode && m2 == CCGOCmode)
15496       || (m1 == CCGOCmode && m2 == CCGCmode))
15497     return CCGCmode;
15498
15499   switch (m1)
15500     {
15501     default:
15502       gcc_unreachable ();
15503
15504     case CCmode:
15505     case CCGCmode:
15506     case CCGOCmode:
15507     case CCNOmode:
15508     case CCAmode:
15509     case CCCmode:
15510     case CCOmode:
15511     case CCSmode:
15512     case CCZmode:
15513       switch (m2)
15514         {
15515         default:
15516           return VOIDmode;
15517
15518         case CCmode:
15519         case CCGCmode:
15520         case CCGOCmode:
15521         case CCNOmode:
15522         case CCAmode:
15523         case CCCmode:
15524         case CCOmode:
15525         case CCSmode:
15526         case CCZmode:
15527           return CCmode;
15528         }
15529
15530     case CCFPmode:
15531     case CCFPUmode:
15532       /* These are only compatible with themselves, which we already
15533          checked above.  */
15534       return VOIDmode;
15535     }
15536 }
15537
15538
15539 /* Return a comparison we can do and that it is equivalent to
15540    swap_condition (code) apart possibly from orderedness.
15541    But, never change orderedness if TARGET_IEEE_FP, returning
15542    UNKNOWN in that case if necessary.  */
15543
15544 static enum rtx_code
15545 ix86_fp_swap_condition (enum rtx_code code)
15546 {
15547   switch (code)
15548     {
15549     case GT:                   /* GTU - CF=0 & ZF=0 */
15550       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
15551     case GE:                   /* GEU - CF=0 */
15552       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
15553     case UNLT:                 /* LTU - CF=1 */
15554       return TARGET_IEEE_FP ? UNKNOWN : GT;
15555     case UNLE:                 /* LEU - CF=1 | ZF=1 */
15556       return TARGET_IEEE_FP ? UNKNOWN : GE;
15557     default:
15558       return swap_condition (code);
15559     }
15560 }
15561
15562 /* Return cost of comparison CODE using the best strategy for performance.
15563    All following functions do use number of instructions as a cost metrics.
15564    In future this should be tweaked to compute bytes for optimize_size and
15565    take into account performance of various instructions on various CPUs.  */
15566
15567 static int
15568 ix86_fp_comparison_cost (enum rtx_code code)
15569 {
15570   int arith_cost;
15571
15572   /* The cost of code using bit-twiddling on %ah.  */
15573   switch (code)
15574     {
15575     case UNLE:
15576     case UNLT:
15577     case LTGT:
15578     case GT:
15579     case GE:
15580     case UNORDERED:
15581     case ORDERED:
15582     case UNEQ:
15583       arith_cost = 4;
15584       break;
15585     case LT:
15586     case NE:
15587     case EQ:
15588     case UNGE:
15589       arith_cost = TARGET_IEEE_FP ? 5 : 4;
15590       break;
15591     case LE:
15592     case UNGT:
15593       arith_cost = TARGET_IEEE_FP ? 6 : 4;
15594       break;
15595     default:
15596       gcc_unreachable ();
15597     }
15598
15599   switch (ix86_fp_comparison_strategy (code))
15600     {
15601     case IX86_FPCMP_COMI:
15602       return arith_cost > 4 ? 3 : 2;
15603     case IX86_FPCMP_SAHF:
15604       return arith_cost > 4 ? 4 : 3;
15605     default:
15606       return arith_cost;
15607     }
15608 }
15609
15610 /* Return strategy to use for floating-point.  We assume that fcomi is always
15611    preferrable where available, since that is also true when looking at size
15612    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
15613
15614 enum ix86_fpcmp_strategy
15615 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
15616 {
15617   /* Do fcomi/sahf based test when profitable.  */
15618
15619   if (TARGET_CMOVE)
15620     return IX86_FPCMP_COMI;
15621
15622   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
15623     return IX86_FPCMP_SAHF;
15624
15625   return IX86_FPCMP_ARITH;
15626 }
15627
15628 /* Swap, force into registers, or otherwise massage the two operands
15629    to a fp comparison.  The operands are updated in place; the new
15630    comparison code is returned.  */
15631
15632 static enum rtx_code
15633 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
15634 {
15635   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
15636   rtx op0 = *pop0, op1 = *pop1;
15637   enum machine_mode op_mode = GET_MODE (op0);
15638   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
15639
15640   /* All of the unordered compare instructions only work on registers.
15641      The same is true of the fcomi compare instructions.  The XFmode
15642      compare instructions require registers except when comparing
15643      against zero or when converting operand 1 from fixed point to
15644      floating point.  */
15645
15646   if (!is_sse
15647       && (fpcmp_mode == CCFPUmode
15648           || (op_mode == XFmode
15649               && ! (standard_80387_constant_p (op0) == 1
15650                     || standard_80387_constant_p (op1) == 1)
15651               && GET_CODE (op1) != FLOAT)
15652           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
15653     {
15654       op0 = force_reg (op_mode, op0);
15655       op1 = force_reg (op_mode, op1);
15656     }
15657   else
15658     {
15659       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
15660          things around if they appear profitable, otherwise force op0
15661          into a register.  */
15662
15663       if (standard_80387_constant_p (op0) == 0
15664           || (MEM_P (op0)
15665               && ! (standard_80387_constant_p (op1) == 0
15666                     || MEM_P (op1))))
15667         {
15668           enum rtx_code new_code = ix86_fp_swap_condition (code);
15669           if (new_code != UNKNOWN)
15670             {
15671               rtx tmp;
15672               tmp = op0, op0 = op1, op1 = tmp;
15673               code = new_code;
15674             }
15675         }
15676
15677       if (!REG_P (op0))
15678         op0 = force_reg (op_mode, op0);
15679
15680       if (CONSTANT_P (op1))
15681         {
15682           int tmp = standard_80387_constant_p (op1);
15683           if (tmp == 0)
15684             op1 = validize_mem (force_const_mem (op_mode, op1));
15685           else if (tmp == 1)
15686             {
15687               if (TARGET_CMOVE)
15688                 op1 = force_reg (op_mode, op1);
15689             }
15690           else
15691             op1 = force_reg (op_mode, op1);
15692         }
15693     }
15694
15695   /* Try to rearrange the comparison to make it cheaper.  */
15696   if (ix86_fp_comparison_cost (code)
15697       > ix86_fp_comparison_cost (swap_condition (code))
15698       && (REG_P (op1) || can_create_pseudo_p ()))
15699     {
15700       rtx tmp;
15701       tmp = op0, op0 = op1, op1 = tmp;
15702       code = swap_condition (code);
15703       if (!REG_P (op0))
15704         op0 = force_reg (op_mode, op0);
15705     }
15706
15707   *pop0 = op0;
15708   *pop1 = op1;
15709   return code;
15710 }
15711
15712 /* Convert comparison codes we use to represent FP comparison to integer
15713    code that will result in proper branch.  Return UNKNOWN if no such code
15714    is available.  */
15715
15716 enum rtx_code
15717 ix86_fp_compare_code_to_integer (enum rtx_code code)
15718 {
15719   switch (code)
15720     {
15721     case GT:
15722       return GTU;
15723     case GE:
15724       return GEU;
15725     case ORDERED:
15726     case UNORDERED:
15727       return code;
15728       break;
15729     case UNEQ:
15730       return EQ;
15731       break;
15732     case UNLT:
15733       return LTU;
15734       break;
15735     case UNLE:
15736       return LEU;
15737       break;
15738     case LTGT:
15739       return NE;
15740       break;
15741     default:
15742       return UNKNOWN;
15743     }
15744 }
15745
15746 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
15747
15748 static rtx
15749 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
15750 {
15751   enum machine_mode fpcmp_mode, intcmp_mode;
15752   rtx tmp, tmp2;
15753
15754   fpcmp_mode = ix86_fp_compare_mode (code);
15755   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
15756
15757   /* Do fcomi/sahf based test when profitable.  */
15758   switch (ix86_fp_comparison_strategy (code))
15759     {
15760     case IX86_FPCMP_COMI:
15761       intcmp_mode = fpcmp_mode;
15762       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
15763       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
15764                          tmp);
15765       emit_insn (tmp);
15766       break;
15767
15768     case IX86_FPCMP_SAHF:
15769       intcmp_mode = fpcmp_mode;
15770       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
15771       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
15772                          tmp);
15773
15774       if (!scratch)
15775         scratch = gen_reg_rtx (HImode);
15776       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
15777       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
15778       break;
15779
15780     case IX86_FPCMP_ARITH:
15781       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
15782       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
15783       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
15784       if (!scratch)
15785         scratch = gen_reg_rtx (HImode);
15786       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
15787
15788       /* In the unordered case, we have to check C2 for NaN's, which
15789          doesn't happen to work out to anything nice combination-wise.
15790          So do some bit twiddling on the value we've got in AH to come
15791          up with an appropriate set of condition codes.  */
15792
15793       intcmp_mode = CCNOmode;
15794       switch (code)
15795         {
15796         case GT:
15797         case UNGT:
15798           if (code == GT || !TARGET_IEEE_FP)
15799             {
15800               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
15801               code = EQ;
15802             }
15803           else
15804             {
15805               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15806               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
15807               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
15808               intcmp_mode = CCmode;
15809               code = GEU;
15810             }
15811           break;
15812         case LT:
15813         case UNLT:
15814           if (code == LT && TARGET_IEEE_FP)
15815             {
15816               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15817               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
15818               intcmp_mode = CCmode;
15819               code = EQ;
15820             }
15821           else
15822             {
15823               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
15824               code = NE;
15825             }
15826           break;
15827         case GE:
15828         case UNGE:
15829           if (code == GE || !TARGET_IEEE_FP)
15830             {
15831               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
15832               code = EQ;
15833             }
15834           else
15835             {
15836               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15837               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
15838               code = NE;
15839             }
15840           break;
15841         case LE:
15842         case UNLE:
15843           if (code == LE && TARGET_IEEE_FP)
15844             {
15845               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15846               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
15847               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
15848               intcmp_mode = CCmode;
15849               code = LTU;
15850             }
15851           else
15852             {
15853               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
15854               code = NE;
15855             }
15856           break;
15857         case EQ:
15858         case UNEQ:
15859           if (code == EQ && TARGET_IEEE_FP)
15860             {
15861               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15862               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
15863               intcmp_mode = CCmode;
15864               code = EQ;
15865             }
15866           else
15867             {
15868               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
15869               code = NE;
15870             }
15871           break;
15872         case NE:
15873         case LTGT:
15874           if (code == NE && TARGET_IEEE_FP)
15875             {
15876               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
15877               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
15878                                              GEN_INT (0x40)));
15879               code = NE;
15880             }
15881           else
15882             {
15883               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
15884               code = EQ;
15885             }
15886           break;
15887
15888         case UNORDERED:
15889           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
15890           code = NE;
15891           break;
15892         case ORDERED:
15893           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
15894           code = EQ;
15895           break;
15896
15897         default:
15898           gcc_unreachable ();
15899         }
15900         break;
15901
15902     default:
15903       gcc_unreachable();
15904     }
15905
15906   /* Return the test that should be put into the flags user, i.e.
15907      the bcc, scc, or cmov instruction.  */
15908   return gen_rtx_fmt_ee (code, VOIDmode,
15909                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
15910                          const0_rtx);
15911 }
15912
15913 static rtx
15914 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
15915 {
15916   rtx ret;
15917
15918   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
15919     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
15920
15921   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
15922     {
15923       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
15924       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
15925     }
15926   else
15927     ret = ix86_expand_int_compare (code, op0, op1);
15928
15929   return ret;
15930 }
15931
15932 void
15933 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
15934 {
15935   rtx tmp;
15936
15937   switch (GET_MODE (op0))
15938     {
15939     case SFmode:
15940     case DFmode:
15941     case XFmode:
15942     case QImode:
15943     case HImode:
15944     case SImode:
15945       simple:
15946       tmp = ix86_expand_compare (code, op0, op1);
15947       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
15948                                   gen_rtx_LABEL_REF (VOIDmode, label),
15949                                   pc_rtx);
15950       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
15951       return;
15952
15953     case DImode:
15954       if (TARGET_64BIT)
15955         goto simple;
15956     case TImode:
15957       /* Expand DImode branch into multiple compare+branch.  */
15958       {
15959         rtx lo[2], hi[2], label2;
15960         enum rtx_code code1, code2, code3;
15961         enum machine_mode submode;
15962
15963         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
15964           {
15965             tmp = op0, op0 = op1, op1 = tmp;
15966             code = swap_condition (code);
15967           }
15968         if (GET_MODE (op0) == DImode)
15969           {
15970             split_di (&op0, 1, lo+0, hi+0);
15971             split_di (&op1, 1, lo+1, hi+1);
15972             submode = SImode;
15973           }
15974         else
15975           {
15976             split_ti (&op0, 1, lo+0, hi+0);
15977             split_ti (&op1, 1, lo+1, hi+1);
15978             submode = DImode;
15979           }
15980
15981         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
15982            avoid two branches.  This costs one extra insn, so disable when
15983            optimizing for size.  */
15984
15985         if ((code == EQ || code == NE)
15986             && (!optimize_insn_for_size_p ()
15987                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
15988           {
15989             rtx xor0, xor1;
15990
15991             xor1 = hi[0];
15992             if (hi[1] != const0_rtx)
15993               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
15994                                    NULL_RTX, 0, OPTAB_WIDEN);
15995
15996             xor0 = lo[0];
15997             if (lo[1] != const0_rtx)
15998               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
15999                                    NULL_RTX, 0, OPTAB_WIDEN);
16000
16001             tmp = expand_binop (submode, ior_optab, xor1, xor0,
16002                                 NULL_RTX, 0, OPTAB_WIDEN);
16003
16004             ix86_expand_branch (code, tmp, const0_rtx, label);
16005             return;
16006           }
16007
16008         /* Otherwise, if we are doing less-than or greater-or-equal-than,
16009            op1 is a constant and the low word is zero, then we can just
16010            examine the high word.  Similarly for low word -1 and
16011            less-or-equal-than or greater-than.  */
16012
16013         if (CONST_INT_P (hi[1]))
16014           switch (code)
16015             {
16016             case LT: case LTU: case GE: case GEU:
16017               if (lo[1] == const0_rtx)
16018                 {
16019                   ix86_expand_branch (code, hi[0], hi[1], label);
16020                   return;
16021                 }
16022               break;
16023             case LE: case LEU: case GT: case GTU:
16024               if (lo[1] == constm1_rtx)
16025                 {
16026                   ix86_expand_branch (code, hi[0], hi[1], label);
16027                   return;
16028                 }
16029               break;
16030             default:
16031               break;
16032             }
16033
16034         /* Otherwise, we need two or three jumps.  */
16035
16036         label2 = gen_label_rtx ();
16037
16038         code1 = code;
16039         code2 = swap_condition (code);
16040         code3 = unsigned_condition (code);
16041
16042         switch (code)
16043           {
16044           case LT: case GT: case LTU: case GTU:
16045             break;
16046
16047           case LE:   code1 = LT;  code2 = GT;  break;
16048           case GE:   code1 = GT;  code2 = LT;  break;
16049           case LEU:  code1 = LTU; code2 = GTU; break;
16050           case GEU:  code1 = GTU; code2 = LTU; break;
16051
16052           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
16053           case NE:   code2 = UNKNOWN; break;
16054
16055           default:
16056             gcc_unreachable ();
16057           }
16058
16059         /*
16060          * a < b =>
16061          *    if (hi(a) < hi(b)) goto true;
16062          *    if (hi(a) > hi(b)) goto false;
16063          *    if (lo(a) < lo(b)) goto true;
16064          *  false:
16065          */
16066
16067         if (code1 != UNKNOWN)
16068           ix86_expand_branch (code1, hi[0], hi[1], label);
16069         if (code2 != UNKNOWN)
16070           ix86_expand_branch (code2, hi[0], hi[1], label2);
16071
16072         ix86_expand_branch (code3, lo[0], lo[1], label);
16073
16074         if (code2 != UNKNOWN)
16075           emit_label (label2);
16076         return;
16077       }
16078
16079     default:
16080       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
16081       goto simple;
16082     }
16083 }
16084
16085 /* Split branch based on floating point condition.  */
16086 void
16087 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
16088                       rtx target1, rtx target2, rtx tmp, rtx pushed)
16089 {
16090   rtx condition;
16091   rtx i;
16092
16093   if (target2 != pc_rtx)
16094     {
16095       rtx tmp = target2;
16096       code = reverse_condition_maybe_unordered (code);
16097       target2 = target1;
16098       target1 = tmp;
16099     }
16100
16101   condition = ix86_expand_fp_compare (code, op1, op2,
16102                                       tmp);
16103
16104   /* Remove pushed operand from stack.  */
16105   if (pushed)
16106     ix86_free_from_memory (GET_MODE (pushed));
16107
16108   i = emit_jump_insn (gen_rtx_SET
16109                       (VOIDmode, pc_rtx,
16110                        gen_rtx_IF_THEN_ELSE (VOIDmode,
16111                                              condition, target1, target2)));
16112   if (split_branch_probability >= 0)
16113     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
16114 }
16115
16116 void
16117 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16118 {
16119   rtx ret;
16120
16121   gcc_assert (GET_MODE (dest) == QImode);
16122
16123   ret = ix86_expand_compare (code, op0, op1);
16124   PUT_MODE (ret, QImode);
16125   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
16126 }
16127
16128 /* Expand comparison setting or clearing carry flag.  Return true when
16129    successful and set pop for the operation.  */
16130 static bool
16131 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
16132 {
16133   enum machine_mode mode =
16134     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
16135
16136   /* Do not handle DImode compares that go through special path.  */
16137   if (mode == (TARGET_64BIT ? TImode : DImode))
16138     return false;
16139
16140   if (SCALAR_FLOAT_MODE_P (mode))
16141     {
16142       rtx compare_op, compare_seq;
16143
16144       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
16145
16146       /* Shortcut:  following common codes never translate
16147          into carry flag compares.  */
16148       if (code == EQ || code == NE || code == UNEQ || code == LTGT
16149           || code == ORDERED || code == UNORDERED)
16150         return false;
16151
16152       /* These comparisons require zero flag; swap operands so they won't.  */
16153       if ((code == GT || code == UNLE || code == LE || code == UNGT)
16154           && !TARGET_IEEE_FP)
16155         {
16156           rtx tmp = op0;
16157           op0 = op1;
16158           op1 = tmp;
16159           code = swap_condition (code);
16160         }
16161
16162       /* Try to expand the comparison and verify that we end up with
16163          carry flag based comparison.  This fails to be true only when
16164          we decide to expand comparison using arithmetic that is not
16165          too common scenario.  */
16166       start_sequence ();
16167       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
16168       compare_seq = get_insns ();
16169       end_sequence ();
16170
16171       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
16172           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
16173         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
16174       else
16175         code = GET_CODE (compare_op);
16176
16177       if (code != LTU && code != GEU)
16178         return false;
16179
16180       emit_insn (compare_seq);
16181       *pop = compare_op;
16182       return true;
16183     }
16184
16185   if (!INTEGRAL_MODE_P (mode))
16186     return false;
16187
16188   switch (code)
16189     {
16190     case LTU:
16191     case GEU:
16192       break;
16193
16194     /* Convert a==0 into (unsigned)a<1.  */
16195     case EQ:
16196     case NE:
16197       if (op1 != const0_rtx)
16198         return false;
16199       op1 = const1_rtx;
16200       code = (code == EQ ? LTU : GEU);
16201       break;
16202
16203     /* Convert a>b into b<a or a>=b-1.  */
16204     case GTU:
16205     case LEU:
16206       if (CONST_INT_P (op1))
16207         {
16208           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
16209           /* Bail out on overflow.  We still can swap operands but that
16210              would force loading of the constant into register.  */
16211           if (op1 == const0_rtx
16212               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
16213             return false;
16214           code = (code == GTU ? GEU : LTU);
16215         }
16216       else
16217         {
16218           rtx tmp = op1;
16219           op1 = op0;
16220           op0 = tmp;
16221           code = (code == GTU ? LTU : GEU);
16222         }
16223       break;
16224
16225     /* Convert a>=0 into (unsigned)a<0x80000000.  */
16226     case LT:
16227     case GE:
16228       if (mode == DImode || op1 != const0_rtx)
16229         return false;
16230       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
16231       code = (code == LT ? GEU : LTU);
16232       break;
16233     case LE:
16234     case GT:
16235       if (mode == DImode || op1 != constm1_rtx)
16236         return false;
16237       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
16238       code = (code == LE ? GEU : LTU);
16239       break;
16240
16241     default:
16242       return false;
16243     }
16244   /* Swapping operands may cause constant to appear as first operand.  */
16245   if (!nonimmediate_operand (op0, VOIDmode))
16246     {
16247       if (!can_create_pseudo_p ())
16248         return false;
16249       op0 = force_reg (mode, op0);
16250     }
16251   *pop = ix86_expand_compare (code, op0, op1);
16252   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
16253   return true;
16254 }
16255
16256 int
16257 ix86_expand_int_movcc (rtx operands[])
16258 {
16259   enum rtx_code code = GET_CODE (operands[1]), compare_code;
16260   rtx compare_seq, compare_op;
16261   enum machine_mode mode = GET_MODE (operands[0]);
16262   bool sign_bit_compare_p = false;
16263   rtx op0 = XEXP (operands[1], 0);
16264   rtx op1 = XEXP (operands[1], 1);
16265
16266   start_sequence ();
16267   compare_op = ix86_expand_compare (code, op0, op1);
16268   compare_seq = get_insns ();
16269   end_sequence ();
16270
16271   compare_code = GET_CODE (compare_op);
16272
16273   if ((op1 == const0_rtx && (code == GE || code == LT))
16274       || (op1 == constm1_rtx && (code == GT || code == LE)))
16275     sign_bit_compare_p = true;
16276
16277   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
16278      HImode insns, we'd be swallowed in word prefix ops.  */
16279
16280   if ((mode != HImode || TARGET_FAST_PREFIX)
16281       && (mode != (TARGET_64BIT ? TImode : DImode))
16282       && CONST_INT_P (operands[2])
16283       && CONST_INT_P (operands[3]))
16284     {
16285       rtx out = operands[0];
16286       HOST_WIDE_INT ct = INTVAL (operands[2]);
16287       HOST_WIDE_INT cf = INTVAL (operands[3]);
16288       HOST_WIDE_INT diff;
16289
16290       diff = ct - cf;
16291       /*  Sign bit compares are better done using shifts than we do by using
16292           sbb.  */
16293       if (sign_bit_compare_p
16294           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
16295         {
16296           /* Detect overlap between destination and compare sources.  */
16297           rtx tmp = out;
16298
16299           if (!sign_bit_compare_p)
16300             {
16301               rtx flags;
16302               bool fpcmp = false;
16303
16304               compare_code = GET_CODE (compare_op);
16305
16306               flags = XEXP (compare_op, 0);
16307
16308               if (GET_MODE (flags) == CCFPmode
16309                   || GET_MODE (flags) == CCFPUmode)
16310                 {
16311                   fpcmp = true;
16312                   compare_code
16313                     = ix86_fp_compare_code_to_integer (compare_code);
16314                 }
16315
16316               /* To simplify rest of code, restrict to the GEU case.  */
16317               if (compare_code == LTU)
16318                 {
16319                   HOST_WIDE_INT tmp = ct;
16320                   ct = cf;
16321                   cf = tmp;
16322                   compare_code = reverse_condition (compare_code);
16323                   code = reverse_condition (code);
16324                 }
16325               else
16326                 {
16327                   if (fpcmp)
16328                     PUT_CODE (compare_op,
16329                               reverse_condition_maybe_unordered
16330                                 (GET_CODE (compare_op)));
16331                   else
16332                     PUT_CODE (compare_op,
16333                               reverse_condition (GET_CODE (compare_op)));
16334                 }
16335               diff = ct - cf;
16336
16337               if (reg_overlap_mentioned_p (out, op0)
16338                   || reg_overlap_mentioned_p (out, op1))
16339                 tmp = gen_reg_rtx (mode);
16340
16341               if (mode == DImode)
16342                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
16343               else
16344                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
16345                                                  flags, compare_op));
16346             }
16347           else
16348             {
16349               if (code == GT || code == GE)
16350                 code = reverse_condition (code);
16351               else
16352                 {
16353                   HOST_WIDE_INT tmp = ct;
16354                   ct = cf;
16355                   cf = tmp;
16356                   diff = ct - cf;
16357                 }
16358               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
16359             }
16360
16361           if (diff == 1)
16362             {
16363               /*
16364                * cmpl op0,op1
16365                * sbbl dest,dest
16366                * [addl dest, ct]
16367                *
16368                * Size 5 - 8.
16369                */
16370               if (ct)
16371                 tmp = expand_simple_binop (mode, PLUS,
16372                                            tmp, GEN_INT (ct),
16373                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
16374             }
16375           else if (cf == -1)
16376             {
16377               /*
16378                * cmpl op0,op1
16379                * sbbl dest,dest
16380                * orl $ct, dest
16381                *
16382                * Size 8.
16383                */
16384               tmp = expand_simple_binop (mode, IOR,
16385                                          tmp, GEN_INT (ct),
16386                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
16387             }
16388           else if (diff == -1 && ct)
16389             {
16390               /*
16391                * cmpl op0,op1
16392                * sbbl dest,dest
16393                * notl dest
16394                * [addl dest, cf]
16395                *
16396                * Size 8 - 11.
16397                */
16398               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
16399               if (cf)
16400                 tmp = expand_simple_binop (mode, PLUS,
16401                                            copy_rtx (tmp), GEN_INT (cf),
16402                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
16403             }
16404           else
16405             {
16406               /*
16407                * cmpl op0,op1
16408                * sbbl dest,dest
16409                * [notl dest]
16410                * andl cf - ct, dest
16411                * [addl dest, ct]
16412                *
16413                * Size 8 - 11.
16414                */
16415
16416               if (cf == 0)
16417                 {
16418                   cf = ct;
16419                   ct = 0;
16420                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
16421                 }
16422
16423               tmp = expand_simple_binop (mode, AND,
16424                                          copy_rtx (tmp),
16425                                          gen_int_mode (cf - ct, mode),
16426                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
16427               if (ct)
16428                 tmp = expand_simple_binop (mode, PLUS,
16429                                            copy_rtx (tmp), GEN_INT (ct),
16430                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
16431             }
16432
16433           if (!rtx_equal_p (tmp, out))
16434             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
16435
16436           return 1; /* DONE */
16437         }
16438
16439       if (diff < 0)
16440         {
16441           enum machine_mode cmp_mode = GET_MODE (op0);
16442
16443           HOST_WIDE_INT tmp;
16444           tmp = ct, ct = cf, cf = tmp;
16445           diff = -diff;
16446
16447           if (SCALAR_FLOAT_MODE_P (cmp_mode))
16448             {
16449               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
16450
16451               /* We may be reversing unordered compare to normal compare, that
16452                  is not valid in general (we may convert non-trapping condition
16453                  to trapping one), however on i386 we currently emit all
16454                  comparisons unordered.  */
16455               compare_code = reverse_condition_maybe_unordered (compare_code);
16456               code = reverse_condition_maybe_unordered (code);
16457             }
16458           else
16459             {
16460               compare_code = reverse_condition (compare_code);
16461               code = reverse_condition (code);
16462             }
16463         }
16464
16465       compare_code = UNKNOWN;
16466       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
16467           && CONST_INT_P (op1))
16468         {
16469           if (op1 == const0_rtx
16470               && (code == LT || code == GE))
16471             compare_code = code;
16472           else if (op1 == constm1_rtx)
16473             {
16474               if (code == LE)
16475                 compare_code = LT;
16476               else if (code == GT)
16477                 compare_code = GE;
16478             }
16479         }
16480
16481       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
16482       if (compare_code != UNKNOWN
16483           && GET_MODE (op0) == GET_MODE (out)
16484           && (cf == -1 || ct == -1))
16485         {
16486           /* If lea code below could be used, only optimize
16487              if it results in a 2 insn sequence.  */
16488
16489           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
16490                  || diff == 3 || diff == 5 || diff == 9)
16491               || (compare_code == LT && ct == -1)
16492               || (compare_code == GE && cf == -1))
16493             {
16494               /*
16495                * notl op1       (if necessary)
16496                * sarl $31, op1
16497                * orl cf, op1
16498                */
16499               if (ct != -1)
16500                 {
16501                   cf = ct;
16502                   ct = -1;
16503                   code = reverse_condition (code);
16504                 }
16505
16506               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
16507
16508               out = expand_simple_binop (mode, IOR,
16509                                          out, GEN_INT (cf),
16510                                          out, 1, OPTAB_DIRECT);
16511               if (out != operands[0])
16512                 emit_move_insn (operands[0], out);
16513
16514               return 1; /* DONE */
16515             }
16516         }
16517
16518
16519       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
16520            || diff == 3 || diff == 5 || diff == 9)
16521           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
16522           && (mode != DImode
16523               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
16524         {
16525           /*
16526            * xorl dest,dest
16527            * cmpl op1,op2
16528            * setcc dest
16529            * lea cf(dest*(ct-cf)),dest
16530            *
16531            * Size 14.
16532            *
16533            * This also catches the degenerate setcc-only case.
16534            */
16535
16536           rtx tmp;
16537           int nops;
16538
16539           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
16540
16541           nops = 0;
16542           /* On x86_64 the lea instruction operates on Pmode, so we need
16543              to get arithmetics done in proper mode to match.  */
16544           if (diff == 1)
16545             tmp = copy_rtx (out);
16546           else
16547             {
16548               rtx out1;
16549               out1 = copy_rtx (out);
16550               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
16551               nops++;
16552               if (diff & 1)
16553                 {
16554                   tmp = gen_rtx_PLUS (mode, tmp, out1);
16555                   nops++;
16556                 }
16557             }
16558           if (cf != 0)
16559             {
16560               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
16561               nops++;
16562             }
16563           if (!rtx_equal_p (tmp, out))
16564             {
16565               if (nops == 1)
16566                 out = force_operand (tmp, copy_rtx (out));
16567               else
16568                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
16569             }
16570           if (!rtx_equal_p (out, operands[0]))
16571             emit_move_insn (operands[0], copy_rtx (out));
16572
16573           return 1; /* DONE */
16574         }
16575
16576       /*
16577        * General case:                  Jumpful:
16578        *   xorl dest,dest               cmpl op1, op2
16579        *   cmpl op1, op2                movl ct, dest
16580        *   setcc dest                   jcc 1f
16581        *   decl dest                    movl cf, dest
16582        *   andl (cf-ct),dest            1:
16583        *   addl ct,dest
16584        *
16585        * Size 20.                       Size 14.
16586        *
16587        * This is reasonably steep, but branch mispredict costs are
16588        * high on modern cpus, so consider failing only if optimizing
16589        * for space.
16590        */
16591
16592       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
16593           && BRANCH_COST (optimize_insn_for_speed_p (),
16594                           false) >= 2)
16595         {
16596           if (cf == 0)
16597             {
16598               enum machine_mode cmp_mode = GET_MODE (op0);
16599
16600               cf = ct;
16601               ct = 0;
16602
16603               if (SCALAR_FLOAT_MODE_P (cmp_mode))
16604                 {
16605                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
16606
16607                   /* We may be reversing unordered compare to normal compare,
16608                      that is not valid in general (we may convert non-trapping
16609                      condition to trapping one), however on i386 we currently
16610                      emit all comparisons unordered.  */
16611                   code = reverse_condition_maybe_unordered (code);
16612                 }
16613               else
16614                 {
16615                   code = reverse_condition (code);
16616                   if (compare_code != UNKNOWN)
16617                     compare_code = reverse_condition (compare_code);
16618                 }
16619             }
16620
16621           if (compare_code != UNKNOWN)
16622             {
16623               /* notl op1       (if needed)
16624                  sarl $31, op1
16625                  andl (cf-ct), op1
16626                  addl ct, op1
16627
16628                  For x < 0 (resp. x <= -1) there will be no notl,
16629                  so if possible swap the constants to get rid of the
16630                  complement.
16631                  True/false will be -1/0 while code below (store flag
16632                  followed by decrement) is 0/-1, so the constants need
16633                  to be exchanged once more.  */
16634
16635               if (compare_code == GE || !cf)
16636                 {
16637                   code = reverse_condition (code);
16638                   compare_code = LT;
16639                 }
16640               else
16641                 {
16642                   HOST_WIDE_INT tmp = cf;
16643                   cf = ct;
16644                   ct = tmp;
16645                 }
16646
16647               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
16648             }
16649           else
16650             {
16651               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
16652
16653               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
16654                                          constm1_rtx,
16655                                          copy_rtx (out), 1, OPTAB_DIRECT);
16656             }
16657
16658           out = expand_simple_binop (mode, AND, copy_rtx (out),
16659                                      gen_int_mode (cf - ct, mode),
16660                                      copy_rtx (out), 1, OPTAB_DIRECT);
16661           if (ct)
16662             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
16663                                        copy_rtx (out), 1, OPTAB_DIRECT);
16664           if (!rtx_equal_p (out, operands[0]))
16665             emit_move_insn (operands[0], copy_rtx (out));
16666
16667           return 1; /* DONE */
16668         }
16669     }
16670
16671   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
16672     {
16673       /* Try a few things more with specific constants and a variable.  */
16674
16675       optab op;
16676       rtx var, orig_out, out, tmp;
16677
16678       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
16679         return 0; /* FAIL */
16680
16681       /* If one of the two operands is an interesting constant, load a
16682          constant with the above and mask it in with a logical operation.  */
16683
16684       if (CONST_INT_P (operands[2]))
16685         {
16686           var = operands[3];
16687           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
16688             operands[3] = constm1_rtx, op = and_optab;
16689           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
16690             operands[3] = const0_rtx, op = ior_optab;
16691           else
16692             return 0; /* FAIL */
16693         }
16694       else if (CONST_INT_P (operands[3]))
16695         {
16696           var = operands[2];
16697           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
16698             operands[2] = constm1_rtx, op = and_optab;
16699           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
16700             operands[2] = const0_rtx, op = ior_optab;
16701           else
16702             return 0; /* FAIL */
16703         }
16704       else
16705         return 0; /* FAIL */
16706
16707       orig_out = operands[0];
16708       tmp = gen_reg_rtx (mode);
16709       operands[0] = tmp;
16710
16711       /* Recurse to get the constant loaded.  */
16712       if (ix86_expand_int_movcc (operands) == 0)
16713         return 0; /* FAIL */
16714
16715       /* Mask in the interesting variable.  */
16716       out = expand_binop (mode, op, var, tmp, orig_out, 0,
16717                           OPTAB_WIDEN);
16718       if (!rtx_equal_p (out, orig_out))
16719         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
16720
16721       return 1; /* DONE */
16722     }
16723
16724   /*
16725    * For comparison with above,
16726    *
16727    * movl cf,dest
16728    * movl ct,tmp
16729    * cmpl op1,op2
16730    * cmovcc tmp,dest
16731    *
16732    * Size 15.
16733    */
16734
16735   if (! nonimmediate_operand (operands[2], mode))
16736     operands[2] = force_reg (mode, operands[2]);
16737   if (! nonimmediate_operand (operands[3], mode))
16738     operands[3] = force_reg (mode, operands[3]);
16739
16740   if (! register_operand (operands[2], VOIDmode)
16741       && (mode == QImode
16742           || ! register_operand (operands[3], VOIDmode)))
16743     operands[2] = force_reg (mode, operands[2]);
16744
16745   if (mode == QImode
16746       && ! register_operand (operands[3], VOIDmode))
16747     operands[3] = force_reg (mode, operands[3]);
16748
16749   emit_insn (compare_seq);
16750   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
16751                           gen_rtx_IF_THEN_ELSE (mode,
16752                                                 compare_op, operands[2],
16753                                                 operands[3])));
16754
16755   return 1; /* DONE */
16756 }
16757
16758 /* Swap, force into registers, or otherwise massage the two operands
16759    to an sse comparison with a mask result.  Thus we differ a bit from
16760    ix86_prepare_fp_compare_args which expects to produce a flags result.
16761
16762    The DEST operand exists to help determine whether to commute commutative
16763    operators.  The POP0/POP1 operands are updated in place.  The new
16764    comparison code is returned, or UNKNOWN if not implementable.  */
16765
16766 static enum rtx_code
16767 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
16768                                   rtx *pop0, rtx *pop1)
16769 {
16770   rtx tmp;
16771
16772   switch (code)
16773     {
16774     case LTGT:
16775     case UNEQ:
16776       /* We have no LTGT as an operator.  We could implement it with
16777          NE & ORDERED, but this requires an extra temporary.  It's
16778          not clear that it's worth it.  */
16779       return UNKNOWN;
16780
16781     case LT:
16782     case LE:
16783     case UNGT:
16784     case UNGE:
16785       /* These are supported directly.  */
16786       break;
16787
16788     case EQ:
16789     case NE:
16790     case UNORDERED:
16791     case ORDERED:
16792       /* For commutative operators, try to canonicalize the destination
16793          operand to be first in the comparison - this helps reload to
16794          avoid extra moves.  */
16795       if (!dest || !rtx_equal_p (dest, *pop1))
16796         break;
16797       /* FALLTHRU */
16798
16799     case GE:
16800     case GT:
16801     case UNLE:
16802     case UNLT:
16803       /* These are not supported directly.  Swap the comparison operands
16804          to transform into something that is supported.  */
16805       tmp = *pop0;
16806       *pop0 = *pop1;
16807       *pop1 = tmp;
16808       code = swap_condition (code);
16809       break;
16810
16811     default:
16812       gcc_unreachable ();
16813     }
16814
16815   return code;
16816 }
16817
16818 /* Detect conditional moves that exactly match min/max operational
16819    semantics.  Note that this is IEEE safe, as long as we don't
16820    interchange the operands.
16821
16822    Returns FALSE if this conditional move doesn't match a MIN/MAX,
16823    and TRUE if the operation is successful and instructions are emitted.  */
16824
16825 static bool
16826 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
16827                            rtx cmp_op1, rtx if_true, rtx if_false)
16828 {
16829   enum machine_mode mode;
16830   bool is_min;
16831   rtx tmp;
16832
16833   if (code == LT)
16834     ;
16835   else if (code == UNGE)
16836     {
16837       tmp = if_true;
16838       if_true = if_false;
16839       if_false = tmp;
16840     }
16841   else
16842     return false;
16843
16844   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
16845     is_min = true;
16846   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
16847     is_min = false;
16848   else
16849     return false;
16850
16851   mode = GET_MODE (dest);
16852
16853   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
16854      but MODE may be a vector mode and thus not appropriate.  */
16855   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
16856     {
16857       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
16858       rtvec v;
16859
16860       if_true = force_reg (mode, if_true);
16861       v = gen_rtvec (2, if_true, if_false);
16862       tmp = gen_rtx_UNSPEC (mode, v, u);
16863     }
16864   else
16865     {
16866       code = is_min ? SMIN : SMAX;
16867       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
16868     }
16869
16870   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
16871   return true;
16872 }
16873
16874 /* Expand an sse vector comparison.  Return the register with the result.  */
16875
16876 static rtx
16877 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
16878                      rtx op_true, rtx op_false)
16879 {
16880   enum machine_mode mode = GET_MODE (dest);
16881   rtx x;
16882
16883   cmp_op0 = force_reg (mode, cmp_op0);
16884   if (!nonimmediate_operand (cmp_op1, mode))
16885     cmp_op1 = force_reg (mode, cmp_op1);
16886
16887   if (optimize
16888       || reg_overlap_mentioned_p (dest, op_true)
16889       || reg_overlap_mentioned_p (dest, op_false))
16890     dest = gen_reg_rtx (mode);
16891
16892   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
16893   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16894
16895   return dest;
16896 }
16897
16898 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
16899    operations.  This is used for both scalar and vector conditional moves.  */
16900
16901 static void
16902 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
16903 {
16904   enum machine_mode mode = GET_MODE (dest);
16905   rtx t2, t3, x;
16906
16907   if (op_false == CONST0_RTX (mode))
16908     {
16909       op_true = force_reg (mode, op_true);
16910       x = gen_rtx_AND (mode, cmp, op_true);
16911       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16912     }
16913   else if (op_true == CONST0_RTX (mode))
16914     {
16915       op_false = force_reg (mode, op_false);
16916       x = gen_rtx_NOT (mode, cmp);
16917       x = gen_rtx_AND (mode, x, op_false);
16918       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16919     }
16920   else if (TARGET_XOP)
16921     {
16922       rtx pcmov = gen_rtx_SET (mode, dest,
16923                                gen_rtx_IF_THEN_ELSE (mode, cmp,
16924                                                      op_true,
16925                                                      op_false));
16926       emit_insn (pcmov);
16927     }
16928   else
16929     {
16930       op_true = force_reg (mode, op_true);
16931       op_false = force_reg (mode, op_false);
16932
16933       t2 = gen_reg_rtx (mode);
16934       if (optimize)
16935         t3 = gen_reg_rtx (mode);
16936       else
16937         t3 = dest;
16938
16939       x = gen_rtx_AND (mode, op_true, cmp);
16940       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
16941
16942       x = gen_rtx_NOT (mode, cmp);
16943       x = gen_rtx_AND (mode, x, op_false);
16944       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
16945
16946       x = gen_rtx_IOR (mode, t3, t2);
16947       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16948     }
16949 }
16950
16951 /* Expand a floating-point conditional move.  Return true if successful.  */
16952
16953 int
16954 ix86_expand_fp_movcc (rtx operands[])
16955 {
16956   enum machine_mode mode = GET_MODE (operands[0]);
16957   enum rtx_code code = GET_CODE (operands[1]);
16958   rtx tmp, compare_op;
16959   rtx op0 = XEXP (operands[1], 0);
16960   rtx op1 = XEXP (operands[1], 1);
16961
16962   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
16963     {
16964       enum machine_mode cmode;
16965
16966       /* Since we've no cmove for sse registers, don't force bad register
16967          allocation just to gain access to it.  Deny movcc when the
16968          comparison mode doesn't match the move mode.  */
16969       cmode = GET_MODE (op0);
16970       if (cmode == VOIDmode)
16971         cmode = GET_MODE (op1);
16972       if (cmode != mode)
16973         return 0;
16974
16975       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
16976       if (code == UNKNOWN)
16977         return 0;
16978
16979       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
16980                                      operands[2], operands[3]))
16981         return 1;
16982
16983       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
16984                                  operands[2], operands[3]);
16985       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
16986       return 1;
16987     }
16988
16989   /* The floating point conditional move instructions don't directly
16990      support conditions resulting from a signed integer comparison.  */
16991
16992   compare_op = ix86_expand_compare (code, op0, op1);
16993   if (!fcmov_comparison_operator (compare_op, VOIDmode))
16994     {
16995       tmp = gen_reg_rtx (QImode);
16996       ix86_expand_setcc (tmp, code, op0, op1);
16997
16998       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
16999     }
17000
17001   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
17002                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
17003                                                 operands[2], operands[3])));
17004
17005   return 1;
17006 }
17007
17008 /* Expand a floating-point vector conditional move; a vcond operation
17009    rather than a movcc operation.  */
17010
17011 bool
17012 ix86_expand_fp_vcond (rtx operands[])
17013 {
17014   enum rtx_code code = GET_CODE (operands[3]);
17015   rtx cmp;
17016
17017   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
17018                                            &operands[4], &operands[5]);
17019   if (code == UNKNOWN)
17020     return false;
17021
17022   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
17023                                  operands[5], operands[1], operands[2]))
17024     return true;
17025
17026   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
17027                              operands[1], operands[2]);
17028   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
17029   return true;
17030 }
17031
17032 /* Expand a signed/unsigned integral vector conditional move.  */
17033
17034 bool
17035 ix86_expand_int_vcond (rtx operands[])
17036 {
17037   enum machine_mode mode = GET_MODE (operands[0]);
17038   enum rtx_code code = GET_CODE (operands[3]);
17039   bool negate = false;
17040   rtx x, cop0, cop1;
17041
17042   cop0 = operands[4];
17043   cop1 = operands[5];
17044
17045   /* XOP supports all of the comparisons on all vector int types.  */
17046   if (!TARGET_XOP)
17047     {
17048       /* Canonicalize the comparison to EQ, GT, GTU.  */
17049       switch (code)
17050         {
17051         case EQ:
17052         case GT:
17053         case GTU:
17054           break;
17055
17056         case NE:
17057         case LE:
17058         case LEU:
17059           code = reverse_condition (code);
17060           negate = true;
17061           break;
17062
17063         case GE:
17064         case GEU:
17065           code = reverse_condition (code);
17066           negate = true;
17067           /* FALLTHRU */
17068
17069         case LT:
17070         case LTU:
17071           code = swap_condition (code);
17072           x = cop0, cop0 = cop1, cop1 = x;
17073           break;
17074
17075         default:
17076           gcc_unreachable ();
17077         }
17078
17079       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
17080       if (mode == V2DImode)
17081         {
17082           switch (code)
17083             {
17084             case EQ:
17085               /* SSE4.1 supports EQ.  */
17086               if (!TARGET_SSE4_1)
17087                 return false;
17088               break;
17089
17090             case GT:
17091             case GTU:
17092               /* SSE4.2 supports GT/GTU.  */
17093               if (!TARGET_SSE4_2)
17094                 return false;
17095               break;
17096
17097             default:
17098               gcc_unreachable ();
17099             }
17100         }
17101
17102       /* Unsigned parallel compare is not supported by the hardware.
17103          Play some tricks to turn this into a signed comparison
17104          against 0.  */
17105       if (code == GTU)
17106         {
17107           cop0 = force_reg (mode, cop0);
17108
17109           switch (mode)
17110             {
17111             case V4SImode:
17112             case V2DImode:
17113                 {
17114                   rtx t1, t2, mask;
17115                   rtx (*gen_sub3) (rtx, rtx, rtx);
17116
17117                   /* Subtract (-(INT MAX) - 1) from both operands to make
17118                      them signed.  */
17119                   mask = ix86_build_signbit_mask (GET_MODE_INNER (mode),
17120                                                   true, false);
17121                   gen_sub3 = (mode == V4SImode
17122                               ? gen_subv4si3 : gen_subv2di3);
17123                   t1 = gen_reg_rtx (mode);
17124                   emit_insn (gen_sub3 (t1, cop0, mask));
17125
17126                   t2 = gen_reg_rtx (mode);
17127                   emit_insn (gen_sub3 (t2, cop1, mask));
17128
17129                   cop0 = t1;
17130                   cop1 = t2;
17131                   code = GT;
17132                 }
17133               break;
17134
17135             case V16QImode:
17136             case V8HImode:
17137               /* Perform a parallel unsigned saturating subtraction.  */
17138               x = gen_reg_rtx (mode);
17139               emit_insn (gen_rtx_SET (VOIDmode, x,
17140                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
17141
17142               cop0 = x;
17143               cop1 = CONST0_RTX (mode);
17144               code = EQ;
17145               negate = !negate;
17146               break;
17147
17148             default:
17149               gcc_unreachable ();
17150             }
17151         }
17152     }
17153
17154   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
17155                            operands[1+negate], operands[2-negate]);
17156
17157   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
17158                          operands[2-negate]);
17159   return true;
17160 }
17161
17162 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
17163    true if we should do zero extension, else sign extension.  HIGH_P is
17164    true if we want the N/2 high elements, else the low elements.  */
17165
17166 void
17167 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
17168 {
17169   enum machine_mode imode = GET_MODE (operands[1]);
17170   rtx (*unpack)(rtx, rtx, rtx);
17171   rtx se, dest;
17172
17173   switch (imode)
17174     {
17175     case V16QImode:
17176       if (high_p)
17177         unpack = gen_vec_interleave_highv16qi;
17178       else
17179         unpack = gen_vec_interleave_lowv16qi;
17180       break;
17181     case V8HImode:
17182       if (high_p)
17183         unpack = gen_vec_interleave_highv8hi;
17184       else
17185         unpack = gen_vec_interleave_lowv8hi;
17186       break;
17187     case V4SImode:
17188       if (high_p)
17189         unpack = gen_vec_interleave_highv4si;
17190       else
17191         unpack = gen_vec_interleave_lowv4si;
17192       break;
17193     default:
17194       gcc_unreachable ();
17195     }
17196
17197   dest = gen_lowpart (imode, operands[0]);
17198
17199   if (unsigned_p)
17200     se = force_reg (imode, CONST0_RTX (imode));
17201   else
17202     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
17203                               operands[1], pc_rtx, pc_rtx);
17204
17205   emit_insn (unpack (dest, operands[1], se));
17206 }
17207
17208 /* This function performs the same task as ix86_expand_sse_unpack,
17209    but with SSE4.1 instructions.  */
17210
17211 void
17212 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
17213 {
17214   enum machine_mode imode = GET_MODE (operands[1]);
17215   rtx (*unpack)(rtx, rtx);
17216   rtx src, dest;
17217
17218   switch (imode)
17219     {
17220     case V16QImode:
17221       if (unsigned_p)
17222         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
17223       else
17224         unpack = gen_sse4_1_extendv8qiv8hi2;
17225       break;
17226     case V8HImode:
17227       if (unsigned_p)
17228         unpack = gen_sse4_1_zero_extendv4hiv4si2;
17229       else
17230         unpack = gen_sse4_1_extendv4hiv4si2;
17231       break;
17232     case V4SImode:
17233       if (unsigned_p)
17234         unpack = gen_sse4_1_zero_extendv2siv2di2;
17235       else
17236         unpack = gen_sse4_1_extendv2siv2di2;
17237       break;
17238     default:
17239       gcc_unreachable ();
17240     }
17241
17242   dest = operands[0];
17243   if (high_p)
17244     {
17245       /* Shift higher 8 bytes to lower 8 bytes.  */
17246       src = gen_reg_rtx (imode);
17247       emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, src),
17248                                      gen_lowpart (V1TImode, operands[1]),
17249                                      GEN_INT (64)));
17250     }
17251   else
17252     src = operands[1];
17253
17254   emit_insn (unpack (dest, src));
17255 }
17256
17257 /* Expand conditional increment or decrement using adb/sbb instructions.
17258    The default case using setcc followed by the conditional move can be
17259    done by generic code.  */
17260 int
17261 ix86_expand_int_addcc (rtx operands[])
17262 {
17263   enum rtx_code code = GET_CODE (operands[1]);
17264   rtx flags;
17265   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
17266   rtx compare_op;
17267   rtx val = const0_rtx;
17268   bool fpcmp = false;
17269   enum machine_mode mode;
17270   rtx op0 = XEXP (operands[1], 0);
17271   rtx op1 = XEXP (operands[1], 1);
17272
17273   if (operands[3] != const1_rtx
17274       && operands[3] != constm1_rtx)
17275     return 0;
17276   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
17277      return 0;
17278   code = GET_CODE (compare_op);
17279
17280   flags = XEXP (compare_op, 0);
17281
17282   if (GET_MODE (flags) == CCFPmode
17283       || GET_MODE (flags) == CCFPUmode)
17284     {
17285       fpcmp = true;
17286       code = ix86_fp_compare_code_to_integer (code);
17287     }
17288
17289   if (code != LTU)
17290     {
17291       val = constm1_rtx;
17292       if (fpcmp)
17293         PUT_CODE (compare_op,
17294                   reverse_condition_maybe_unordered
17295                     (GET_CODE (compare_op)));
17296       else
17297         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
17298     }
17299
17300   mode = GET_MODE (operands[0]);
17301
17302   /* Construct either adc or sbb insn.  */
17303   if ((code == LTU) == (operands[3] == constm1_rtx))
17304     {
17305       switch (mode)
17306         {
17307           case QImode:
17308             insn = gen_subqi3_carry;
17309             break;
17310           case HImode:
17311             insn = gen_subhi3_carry;
17312             break;
17313           case SImode:
17314             insn = gen_subsi3_carry;
17315             break;
17316           case DImode:
17317             insn = gen_subdi3_carry;
17318             break;
17319           default:
17320             gcc_unreachable ();
17321         }
17322     }
17323   else
17324     {
17325       switch (mode)
17326         {
17327           case QImode:
17328             insn = gen_addqi3_carry;
17329             break;
17330           case HImode:
17331             insn = gen_addhi3_carry;
17332             break;
17333           case SImode:
17334             insn = gen_addsi3_carry;
17335             break;
17336           case DImode:
17337             insn = gen_adddi3_carry;
17338             break;
17339           default:
17340             gcc_unreachable ();
17341         }
17342     }
17343   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
17344
17345   return 1; /* DONE */
17346 }
17347
17348
17349 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
17350    works for floating pointer parameters and nonoffsetable memories.
17351    For pushes, it returns just stack offsets; the values will be saved
17352    in the right order.  Maximally three parts are generated.  */
17353
17354 static int
17355 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
17356 {
17357   int size;
17358
17359   if (!TARGET_64BIT)
17360     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
17361   else
17362     size = (GET_MODE_SIZE (mode) + 4) / 8;
17363
17364   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
17365   gcc_assert (size >= 2 && size <= 4);
17366
17367   /* Optimize constant pool reference to immediates.  This is used by fp
17368      moves, that force all constants to memory to allow combining.  */
17369   if (MEM_P (operand) && MEM_READONLY_P (operand))
17370     {
17371       rtx tmp = maybe_get_pool_constant (operand);
17372       if (tmp)
17373         operand = tmp;
17374     }
17375
17376   if (MEM_P (operand) && !offsettable_memref_p (operand))
17377     {
17378       /* The only non-offsetable memories we handle are pushes.  */
17379       int ok = push_operand (operand, VOIDmode);
17380
17381       gcc_assert (ok);
17382
17383       operand = copy_rtx (operand);
17384       PUT_MODE (operand, Pmode);
17385       parts[0] = parts[1] = parts[2] = parts[3] = operand;
17386       return size;
17387     }
17388
17389   if (GET_CODE (operand) == CONST_VECTOR)
17390     {
17391       enum machine_mode imode = int_mode_for_mode (mode);
17392       /* Caution: if we looked through a constant pool memory above,
17393          the operand may actually have a different mode now.  That's
17394          ok, since we want to pun this all the way back to an integer.  */
17395       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
17396       gcc_assert (operand != NULL);
17397       mode = imode;
17398     }
17399
17400   if (!TARGET_64BIT)
17401     {
17402       if (mode == DImode)
17403         split_di (&operand, 1, &parts[0], &parts[1]);
17404       else
17405         {
17406           int i;
17407
17408           if (REG_P (operand))
17409             {
17410               gcc_assert (reload_completed);
17411               for (i = 0; i < size; i++)
17412                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
17413             }
17414           else if (offsettable_memref_p (operand))
17415             {
17416               operand = adjust_address (operand, SImode, 0);
17417               parts[0] = operand;
17418               for (i = 1; i < size; i++)
17419                 parts[i] = adjust_address (operand, SImode, 4 * i);
17420             }
17421           else if (GET_CODE (operand) == CONST_DOUBLE)
17422             {
17423               REAL_VALUE_TYPE r;
17424               long l[4];
17425
17426               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
17427               switch (mode)
17428                 {
17429                 case TFmode:
17430                   real_to_target (l, &r, mode);
17431                   parts[3] = gen_int_mode (l[3], SImode);
17432                   parts[2] = gen_int_mode (l[2], SImode);
17433                   break;
17434                 case XFmode:
17435                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
17436                   parts[2] = gen_int_mode (l[2], SImode);
17437                   break;
17438                 case DFmode:
17439                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
17440                   break;
17441                 default:
17442                   gcc_unreachable ();
17443                 }
17444               parts[1] = gen_int_mode (l[1], SImode);
17445               parts[0] = gen_int_mode (l[0], SImode);
17446             }
17447           else
17448             gcc_unreachable ();
17449         }
17450     }
17451   else
17452     {
17453       if (mode == TImode)
17454         split_ti (&operand, 1, &parts[0], &parts[1]);
17455       if (mode == XFmode || mode == TFmode)
17456         {
17457           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
17458           if (REG_P (operand))
17459             {
17460               gcc_assert (reload_completed);
17461               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
17462               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
17463             }
17464           else if (offsettable_memref_p (operand))
17465             {
17466               operand = adjust_address (operand, DImode, 0);
17467               parts[0] = operand;
17468               parts[1] = adjust_address (operand, upper_mode, 8);
17469             }
17470           else if (GET_CODE (operand) == CONST_DOUBLE)
17471             {
17472               REAL_VALUE_TYPE r;
17473               long l[4];
17474
17475               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
17476               real_to_target (l, &r, mode);
17477
17478               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
17479               if (HOST_BITS_PER_WIDE_INT >= 64)
17480                 parts[0]
17481                   = gen_int_mode
17482                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
17483                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
17484                        DImode);
17485               else
17486                 parts[0] = immed_double_const (l[0], l[1], DImode);
17487
17488               if (upper_mode == SImode)
17489                 parts[1] = gen_int_mode (l[2], SImode);
17490               else if (HOST_BITS_PER_WIDE_INT >= 64)
17491                 parts[1]
17492                   = gen_int_mode
17493                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
17494                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
17495                        DImode);
17496               else
17497                 parts[1] = immed_double_const (l[2], l[3], DImode);
17498             }
17499           else
17500             gcc_unreachable ();
17501         }
17502     }
17503
17504   return size;
17505 }
17506
17507 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
17508    Return false when normal moves are needed; true when all required
17509    insns have been emitted.  Operands 2-4 contain the input values
17510    int the correct order; operands 5-7 contain the output values.  */
17511
17512 void
17513 ix86_split_long_move (rtx operands[])
17514 {
17515   rtx part[2][4];
17516   int nparts, i, j;
17517   int push = 0;
17518   int collisions = 0;
17519   enum machine_mode mode = GET_MODE (operands[0]);
17520   bool collisionparts[4];
17521
17522   /* The DFmode expanders may ask us to move double.
17523      For 64bit target this is single move.  By hiding the fact
17524      here we simplify i386.md splitters.  */
17525   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
17526     {
17527       /* Optimize constant pool reference to immediates.  This is used by
17528          fp moves, that force all constants to memory to allow combining.  */
17529
17530       if (MEM_P (operands[1])
17531           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
17532           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
17533         operands[1] = get_pool_constant (XEXP (operands[1], 0));
17534       if (push_operand (operands[0], VOIDmode))
17535         {
17536           operands[0] = copy_rtx (operands[0]);
17537           PUT_MODE (operands[0], Pmode);
17538         }
17539       else
17540         operands[0] = gen_lowpart (DImode, operands[0]);
17541       operands[1] = gen_lowpart (DImode, operands[1]);
17542       emit_move_insn (operands[0], operands[1]);
17543       return;
17544     }
17545
17546   /* The only non-offsettable memory we handle is push.  */
17547   if (push_operand (operands[0], VOIDmode))
17548     push = 1;
17549   else
17550     gcc_assert (!MEM_P (operands[0])
17551                 || offsettable_memref_p (operands[0]));
17552
17553   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
17554   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
17555
17556   /* When emitting push, take care for source operands on the stack.  */
17557   if (push && MEM_P (operands[1])
17558       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
17559     {
17560       rtx src_base = XEXP (part[1][nparts - 1], 0);
17561
17562       /* Compensate for the stack decrement by 4.  */
17563       if (!TARGET_64BIT && nparts == 3
17564           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
17565         src_base = plus_constant (src_base, 4);
17566
17567       /* src_base refers to the stack pointer and is
17568          automatically decreased by emitted push.  */
17569       for (i = 0; i < nparts; i++)
17570         part[1][i] = change_address (part[1][i],
17571                                      GET_MODE (part[1][i]), src_base);
17572     }
17573
17574   /* We need to do copy in the right order in case an address register
17575      of the source overlaps the destination.  */
17576   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
17577     {
17578       rtx tmp;
17579
17580       for (i = 0; i < nparts; i++)
17581         {
17582           collisionparts[i]
17583             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
17584           if (collisionparts[i])
17585             collisions++;
17586         }
17587
17588       /* Collision in the middle part can be handled by reordering.  */
17589       if (collisions == 1 && nparts == 3 && collisionparts [1])
17590         {
17591           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
17592           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
17593         }
17594       else if (collisions == 1
17595                && nparts == 4
17596                && (collisionparts [1] || collisionparts [2]))
17597         {
17598           if (collisionparts [1])
17599             {
17600               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
17601               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
17602             }
17603           else
17604             {
17605               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
17606               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
17607             }
17608         }
17609
17610       /* If there are more collisions, we can't handle it by reordering.
17611          Do an lea to the last part and use only one colliding move.  */
17612       else if (collisions > 1)
17613         {
17614           rtx base;
17615
17616           collisions = 1;
17617
17618           base = part[0][nparts - 1];
17619
17620           /* Handle the case when the last part isn't valid for lea.
17621              Happens in 64-bit mode storing the 12-byte XFmode.  */
17622           if (GET_MODE (base) != Pmode)
17623             base = gen_rtx_REG (Pmode, REGNO (base));
17624
17625           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
17626           part[1][0] = replace_equiv_address (part[1][0], base);
17627           for (i = 1; i < nparts; i++)
17628             {
17629               tmp = plus_constant (base, UNITS_PER_WORD * i);
17630               part[1][i] = replace_equiv_address (part[1][i], tmp);
17631             }
17632         }
17633     }
17634
17635   if (push)
17636     {
17637       if (!TARGET_64BIT)
17638         {
17639           if (nparts == 3)
17640             {
17641               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
17642                 emit_insn (gen_addsi3 (stack_pointer_rtx,
17643                                        stack_pointer_rtx, GEN_INT (-4)));
17644               emit_move_insn (part[0][2], part[1][2]);
17645             }
17646           else if (nparts == 4)
17647             {
17648               emit_move_insn (part[0][3], part[1][3]);
17649               emit_move_insn (part[0][2], part[1][2]);
17650             }
17651         }
17652       else
17653         {
17654           /* In 64bit mode we don't have 32bit push available.  In case this is
17655              register, it is OK - we will just use larger counterpart.  We also
17656              retype memory - these comes from attempt to avoid REX prefix on
17657              moving of second half of TFmode value.  */
17658           if (GET_MODE (part[1][1]) == SImode)
17659             {
17660               switch (GET_CODE (part[1][1]))
17661                 {
17662                 case MEM:
17663                   part[1][1] = adjust_address (part[1][1], DImode, 0);
17664                   break;
17665
17666                 case REG:
17667                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
17668                   break;
17669
17670                 default:
17671                   gcc_unreachable ();
17672                 }
17673
17674               if (GET_MODE (part[1][0]) == SImode)
17675                 part[1][0] = part[1][1];
17676             }
17677         }
17678       emit_move_insn (part[0][1], part[1][1]);
17679       emit_move_insn (part[0][0], part[1][0]);
17680       return;
17681     }
17682
17683   /* Choose correct order to not overwrite the source before it is copied.  */
17684   if ((REG_P (part[0][0])
17685        && REG_P (part[1][1])
17686        && (REGNO (part[0][0]) == REGNO (part[1][1])
17687            || (nparts == 3
17688                && REGNO (part[0][0]) == REGNO (part[1][2]))
17689            || (nparts == 4
17690                && REGNO (part[0][0]) == REGNO (part[1][3]))))
17691       || (collisions > 0
17692           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
17693     {
17694       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
17695         {
17696           operands[2 + i] = part[0][j];
17697           operands[6 + i] = part[1][j];
17698         }
17699     }
17700   else
17701     {
17702       for (i = 0; i < nparts; i++)
17703         {
17704           operands[2 + i] = part[0][i];
17705           operands[6 + i] = part[1][i];
17706         }
17707     }
17708
17709   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
17710   if (optimize_insn_for_size_p ())
17711     {
17712       for (j = 0; j < nparts - 1; j++)
17713         if (CONST_INT_P (operands[6 + j])
17714             && operands[6 + j] != const0_rtx
17715             && REG_P (operands[2 + j]))
17716           for (i = j; i < nparts - 1; i++)
17717             if (CONST_INT_P (operands[7 + i])
17718                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
17719               operands[7 + i] = operands[2 + j];
17720     }
17721
17722   for (i = 0; i < nparts; i++)
17723     emit_move_insn (operands[2 + i], operands[6 + i]);
17724
17725   return;
17726 }
17727
17728 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
17729    left shift by a constant, either using a single shift or
17730    a sequence of add instructions.  */
17731
17732 static void
17733 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
17734 {
17735   if (count == 1)
17736     {
17737       emit_insn ((mode == DImode
17738                   ? gen_addsi3
17739                   : gen_adddi3) (operand, operand, operand));
17740     }
17741   else if (!optimize_insn_for_size_p ()
17742            && count * ix86_cost->add <= ix86_cost->shift_const)
17743     {
17744       int i;
17745       for (i=0; i<count; i++)
17746         {
17747           emit_insn ((mode == DImode
17748                       ? gen_addsi3
17749                       : gen_adddi3) (operand, operand, operand));
17750         }
17751     }
17752   else
17753     emit_insn ((mode == DImode
17754                 ? gen_ashlsi3
17755                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
17756 }
17757
17758 void
17759 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
17760 {
17761   rtx low[2], high[2];
17762   int count;
17763   const int single_width = mode == DImode ? 32 : 64;
17764
17765   if (CONST_INT_P (operands[2]))
17766     {
17767       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
17768       count = INTVAL (operands[2]) & (single_width * 2 - 1);
17769
17770       if (count >= single_width)
17771         {
17772           emit_move_insn (high[0], low[1]);
17773           emit_move_insn (low[0], const0_rtx);
17774
17775           if (count > single_width)
17776             ix86_expand_ashl_const (high[0], count - single_width, mode);
17777         }
17778       else
17779         {
17780           if (!rtx_equal_p (operands[0], operands[1]))
17781             emit_move_insn (operands[0], operands[1]);
17782           emit_insn ((mode == DImode
17783                      ? gen_x86_shld
17784                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
17785           ix86_expand_ashl_const (low[0], count, mode);
17786         }
17787       return;
17788     }
17789
17790   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17791
17792   if (operands[1] == const1_rtx)
17793     {
17794       /* Assuming we've chosen a QImode capable registers, then 1 << N
17795          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
17796       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
17797         {
17798           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
17799
17800           ix86_expand_clear (low[0]);
17801           ix86_expand_clear (high[0]);
17802           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
17803
17804           d = gen_lowpart (QImode, low[0]);
17805           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
17806           s = gen_rtx_EQ (QImode, flags, const0_rtx);
17807           emit_insn (gen_rtx_SET (VOIDmode, d, s));
17808
17809           d = gen_lowpart (QImode, high[0]);
17810           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
17811           s = gen_rtx_NE (QImode, flags, const0_rtx);
17812           emit_insn (gen_rtx_SET (VOIDmode, d, s));
17813         }
17814
17815       /* Otherwise, we can get the same results by manually performing
17816          a bit extract operation on bit 5/6, and then performing the two
17817          shifts.  The two methods of getting 0/1 into low/high are exactly
17818          the same size.  Avoiding the shift in the bit extract case helps
17819          pentium4 a bit; no one else seems to care much either way.  */
17820       else
17821         {
17822           rtx x;
17823
17824           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
17825             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
17826           else
17827             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
17828           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
17829
17830           emit_insn ((mode == DImode
17831                       ? gen_lshrsi3
17832                       : gen_lshrdi3) (high[0], high[0],
17833                                       GEN_INT (mode == DImode ? 5 : 6)));
17834           emit_insn ((mode == DImode
17835                       ? gen_andsi3
17836                       : gen_anddi3) (high[0], high[0], const1_rtx));
17837           emit_move_insn (low[0], high[0]);
17838           emit_insn ((mode == DImode
17839                       ? gen_xorsi3
17840                       : gen_xordi3) (low[0], low[0], const1_rtx));
17841         }
17842
17843       emit_insn ((mode == DImode
17844                     ? gen_ashlsi3
17845                     : gen_ashldi3) (low[0], low[0], operands[2]));
17846       emit_insn ((mode == DImode
17847                     ? gen_ashlsi3
17848                     : gen_ashldi3) (high[0], high[0], operands[2]));
17849       return;
17850     }
17851
17852   if (operands[1] == constm1_rtx)
17853     {
17854       /* For -1 << N, we can avoid the shld instruction, because we
17855          know that we're shifting 0...31/63 ones into a -1.  */
17856       emit_move_insn (low[0], constm1_rtx);
17857       if (optimize_insn_for_size_p ())
17858         emit_move_insn (high[0], low[0]);
17859       else
17860         emit_move_insn (high[0], constm1_rtx);
17861     }
17862   else
17863     {
17864       if (!rtx_equal_p (operands[0], operands[1]))
17865         emit_move_insn (operands[0], operands[1]);
17866
17867       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17868       emit_insn ((mode == DImode
17869                   ? gen_x86_shld
17870                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
17871     }
17872
17873   emit_insn ((mode == DImode
17874               ? gen_ashlsi3
17875               : gen_ashldi3) (low[0], low[0], operands[2]));
17876
17877   if (TARGET_CMOVE && scratch)
17878     {
17879       ix86_expand_clear (scratch);
17880       emit_insn ((mode == DImode
17881                   ? gen_x86_shiftsi_adj_1
17882                   : gen_x86_shiftdi_adj_1) (high[0], low[0], operands[2],
17883                                             scratch));
17884     }
17885   else
17886     emit_insn ((mode == DImode
17887                 ? gen_x86_shiftsi_adj_2
17888                 : gen_x86_shiftdi_adj_2) (high[0], low[0], operands[2]));
17889 }
17890
17891 void
17892 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
17893 {
17894   rtx low[2], high[2];
17895   int count;
17896   const int single_width = mode == DImode ? 32 : 64;
17897
17898   if (CONST_INT_P (operands[2]))
17899     {
17900       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
17901       count = INTVAL (operands[2]) & (single_width * 2 - 1);
17902
17903       if (count == single_width * 2 - 1)
17904         {
17905           emit_move_insn (high[0], high[1]);
17906           emit_insn ((mode == DImode
17907                       ? gen_ashrsi3
17908                       : gen_ashrdi3) (high[0], high[0],
17909                                       GEN_INT (single_width - 1)));
17910           emit_move_insn (low[0], high[0]);
17911
17912         }
17913       else if (count >= single_width)
17914         {
17915           emit_move_insn (low[0], high[1]);
17916           emit_move_insn (high[0], low[0]);
17917           emit_insn ((mode == DImode
17918                       ? gen_ashrsi3
17919                       : gen_ashrdi3) (high[0], high[0],
17920                                       GEN_INT (single_width - 1)));
17921           if (count > single_width)
17922             emit_insn ((mode == DImode
17923                         ? gen_ashrsi3
17924                         : gen_ashrdi3) (low[0], low[0],
17925                                         GEN_INT (count - single_width)));
17926         }
17927       else
17928         {
17929           if (!rtx_equal_p (operands[0], operands[1]))
17930             emit_move_insn (operands[0], operands[1]);
17931           emit_insn ((mode == DImode
17932                       ? gen_x86_shrd
17933                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
17934           emit_insn ((mode == DImode
17935                       ? gen_ashrsi3
17936                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
17937         }
17938     }
17939   else
17940     {
17941       if (!rtx_equal_p (operands[0], operands[1]))
17942         emit_move_insn (operands[0], operands[1]);
17943
17944       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
17945
17946       emit_insn ((mode == DImode
17947                   ? gen_x86_shrd
17948                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
17949       emit_insn ((mode == DImode
17950                   ? gen_ashrsi3
17951                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
17952
17953       if (TARGET_CMOVE && scratch)
17954         {
17955           emit_move_insn (scratch, high[0]);
17956           emit_insn ((mode == DImode
17957                       ? gen_ashrsi3
17958                       : gen_ashrdi3) (scratch, scratch,
17959                                       GEN_INT (single_width - 1)));
17960           emit_insn ((mode == DImode
17961                       ? gen_x86_shiftsi_adj_1
17962                       : gen_x86_shiftdi_adj_1) (low[0], high[0], operands[2],
17963                                                 scratch));
17964         }
17965       else
17966         emit_insn ((mode == DImode
17967                     ? gen_x86_shiftsi_adj_3
17968                     : gen_x86_shiftdi_adj_3) (low[0], high[0], operands[2]));
17969     }
17970 }
17971
17972 void
17973 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
17974 {
17975   rtx low[2], high[2];
17976   int count;
17977   const int single_width = mode == DImode ? 32 : 64;
17978
17979   if (CONST_INT_P (operands[2]))
17980     {
17981       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
17982       count = INTVAL (operands[2]) & (single_width * 2 - 1);
17983
17984       if (count >= single_width)
17985         {
17986           emit_move_insn (low[0], high[1]);
17987           ix86_expand_clear (high[0]);
17988
17989           if (count > single_width)
17990             emit_insn ((mode == DImode
17991                         ? gen_lshrsi3
17992                         : gen_lshrdi3) (low[0], low[0],
17993                                         GEN_INT (count - single_width)));
17994         }
17995       else
17996         {
17997           if (!rtx_equal_p (operands[0], operands[1]))
17998             emit_move_insn (operands[0], operands[1]);
17999           emit_insn ((mode == DImode
18000                       ? gen_x86_shrd
18001                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
18002           emit_insn ((mode == DImode
18003                       ? gen_lshrsi3
18004                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
18005         }
18006     }
18007   else
18008     {
18009       if (!rtx_equal_p (operands[0], operands[1]))
18010         emit_move_insn (operands[0], operands[1]);
18011
18012       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
18013
18014       emit_insn ((mode == DImode
18015                   ? gen_x86_shrd
18016                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
18017       emit_insn ((mode == DImode
18018                   ? gen_lshrsi3
18019                   : gen_lshrdi3) (high[0], high[0], operands[2]));
18020
18021       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
18022       if (TARGET_CMOVE && scratch)
18023         {
18024           ix86_expand_clear (scratch);
18025           emit_insn ((mode == DImode
18026                       ? gen_x86_shiftsi_adj_1
18027                       : gen_x86_shiftdi_adj_1) (low[0], high[0], operands[2],
18028                                                 scratch));
18029         }
18030       else
18031         emit_insn ((mode == DImode
18032                     ? gen_x86_shiftsi_adj_2
18033                     : gen_x86_shiftdi_adj_2) (low[0], high[0], operands[2]));
18034     }
18035 }
18036
18037 /* Predict just emitted jump instruction to be taken with probability PROB.  */
18038 static void
18039 predict_jump (int prob)
18040 {
18041   rtx insn = get_last_insn ();
18042   gcc_assert (JUMP_P (insn));
18043   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
18044 }
18045
18046 /* Helper function for the string operations below.  Dest VARIABLE whether
18047    it is aligned to VALUE bytes.  If true, jump to the label.  */
18048 static rtx
18049 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
18050 {
18051   rtx label = gen_label_rtx ();
18052   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
18053   if (GET_MODE (variable) == DImode)
18054     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
18055   else
18056     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
18057   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
18058                            1, label);
18059   if (epilogue)
18060     predict_jump (REG_BR_PROB_BASE * 50 / 100);
18061   else
18062     predict_jump (REG_BR_PROB_BASE * 90 / 100);
18063   return label;
18064 }
18065
18066 /* Adjust COUNTER by the VALUE.  */
18067 static void
18068 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
18069 {
18070   if (GET_MODE (countreg) == DImode)
18071     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
18072   else
18073     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
18074 }
18075
18076 /* Zero extend possibly SImode EXP to Pmode register.  */
18077 rtx
18078 ix86_zero_extend_to_Pmode (rtx exp)
18079 {
18080   rtx r;
18081   if (GET_MODE (exp) == VOIDmode)
18082     return force_reg (Pmode, exp);
18083   if (GET_MODE (exp) == Pmode)
18084     return copy_to_mode_reg (Pmode, exp);
18085   r = gen_reg_rtx (Pmode);
18086   emit_insn (gen_zero_extendsidi2 (r, exp));
18087   return r;
18088 }
18089
18090 /* Divide COUNTREG by SCALE.  */
18091 static rtx
18092 scale_counter (rtx countreg, int scale)
18093 {
18094   rtx sc;
18095
18096   if (scale == 1)
18097     return countreg;
18098   if (CONST_INT_P (countreg))
18099     return GEN_INT (INTVAL (countreg) / scale);
18100   gcc_assert (REG_P (countreg));
18101
18102   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
18103                             GEN_INT (exact_log2 (scale)),
18104                             NULL, 1, OPTAB_DIRECT);
18105   return sc;
18106 }
18107
18108 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
18109    DImode for constant loop counts.  */
18110
18111 static enum machine_mode
18112 counter_mode (rtx count_exp)
18113 {
18114   if (GET_MODE (count_exp) != VOIDmode)
18115     return GET_MODE (count_exp);
18116   if (!CONST_INT_P (count_exp))
18117     return Pmode;
18118   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
18119     return DImode;
18120   return SImode;
18121 }
18122
18123 /* When SRCPTR is non-NULL, output simple loop to move memory
18124    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
18125    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
18126    equivalent loop to set memory by VALUE (supposed to be in MODE).
18127
18128    The size is rounded down to whole number of chunk size moved at once.
18129    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
18130
18131
18132 static void
18133 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
18134                                rtx destptr, rtx srcptr, rtx value,
18135                                rtx count, enum machine_mode mode, int unroll,
18136                                int expected_size)
18137 {
18138   rtx out_label, top_label, iter, tmp;
18139   enum machine_mode iter_mode = counter_mode (count);
18140   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
18141   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
18142   rtx size;
18143   rtx x_addr;
18144   rtx y_addr;
18145   int i;
18146
18147   top_label = gen_label_rtx ();
18148   out_label = gen_label_rtx ();
18149   iter = gen_reg_rtx (iter_mode);
18150
18151   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
18152                               NULL, 1, OPTAB_DIRECT);
18153   /* Those two should combine.  */
18154   if (piece_size == const1_rtx)
18155     {
18156       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
18157                                true, out_label);
18158       predict_jump (REG_BR_PROB_BASE * 10 / 100);
18159     }
18160   emit_move_insn (iter, const0_rtx);
18161
18162   emit_label (top_label);
18163
18164   tmp = convert_modes (Pmode, iter_mode, iter, true);
18165   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
18166   destmem = change_address (destmem, mode, x_addr);
18167
18168   if (srcmem)
18169     {
18170       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
18171       srcmem = change_address (srcmem, mode, y_addr);
18172
18173       /* When unrolling for chips that reorder memory reads and writes,
18174          we can save registers by using single temporary.
18175          Also using 4 temporaries is overkill in 32bit mode.  */
18176       if (!TARGET_64BIT && 0)
18177         {
18178           for (i = 0; i < unroll; i++)
18179             {
18180               if (i)
18181                 {
18182                   destmem =
18183                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
18184                   srcmem =
18185                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
18186                 }
18187               emit_move_insn (destmem, srcmem);
18188             }
18189         }
18190       else
18191         {
18192           rtx tmpreg[4];
18193           gcc_assert (unroll <= 4);
18194           for (i = 0; i < unroll; i++)
18195             {
18196               tmpreg[i] = gen_reg_rtx (mode);
18197               if (i)
18198                 {
18199                   srcmem =
18200                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
18201                 }
18202               emit_move_insn (tmpreg[i], srcmem);
18203             }
18204           for (i = 0; i < unroll; i++)
18205             {
18206               if (i)
18207                 {
18208                   destmem =
18209                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
18210                 }
18211               emit_move_insn (destmem, tmpreg[i]);
18212             }
18213         }
18214     }
18215   else
18216     for (i = 0; i < unroll; i++)
18217       {
18218         if (i)
18219           destmem =
18220             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
18221         emit_move_insn (destmem, value);
18222       }
18223
18224   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
18225                              true, OPTAB_LIB_WIDEN);
18226   if (tmp != iter)
18227     emit_move_insn (iter, tmp);
18228
18229   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
18230                            true, top_label);
18231   if (expected_size != -1)
18232     {
18233       expected_size /= GET_MODE_SIZE (mode) * unroll;
18234       if (expected_size == 0)
18235         predict_jump (0);
18236       else if (expected_size > REG_BR_PROB_BASE)
18237         predict_jump (REG_BR_PROB_BASE - 1);
18238       else
18239         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
18240     }
18241   else
18242     predict_jump (REG_BR_PROB_BASE * 80 / 100);
18243   iter = ix86_zero_extend_to_Pmode (iter);
18244   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
18245                              true, OPTAB_LIB_WIDEN);
18246   if (tmp != destptr)
18247     emit_move_insn (destptr, tmp);
18248   if (srcptr)
18249     {
18250       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
18251                                  true, OPTAB_LIB_WIDEN);
18252       if (tmp != srcptr)
18253         emit_move_insn (srcptr, tmp);
18254     }
18255   emit_label (out_label);
18256 }
18257
18258 /* Output "rep; mov" instruction.
18259    Arguments have same meaning as for previous function */
18260 static void
18261 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
18262                            rtx destptr, rtx srcptr,
18263                            rtx count,
18264                            enum machine_mode mode)
18265 {
18266   rtx destexp;
18267   rtx srcexp;
18268   rtx countreg;
18269
18270   /* If the size is known, it is shorter to use rep movs.  */
18271   if (mode == QImode && CONST_INT_P (count)
18272       && !(INTVAL (count) & 3))
18273     mode = SImode;
18274
18275   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
18276     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
18277   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
18278     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
18279   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
18280   if (mode != QImode)
18281     {
18282       destexp = gen_rtx_ASHIFT (Pmode, countreg,
18283                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
18284       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
18285       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
18286                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
18287       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
18288     }
18289   else
18290     {
18291       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
18292       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
18293     }
18294   if (CONST_INT_P (count))
18295     {
18296       count = GEN_INT (INTVAL (count)
18297                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
18298       destmem = shallow_copy_rtx (destmem);
18299       srcmem = shallow_copy_rtx (srcmem);
18300       set_mem_size (destmem, count);
18301       set_mem_size (srcmem, count);
18302     }
18303   else
18304     {
18305       if (MEM_SIZE (destmem))
18306         set_mem_size (destmem, NULL_RTX);
18307       if (MEM_SIZE (srcmem))
18308         set_mem_size (srcmem, NULL_RTX);
18309     }
18310   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
18311                           destexp, srcexp));
18312 }
18313
18314 /* Output "rep; stos" instruction.
18315    Arguments have same meaning as for previous function */
18316 static void
18317 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
18318                             rtx count, enum machine_mode mode,
18319                             rtx orig_value)
18320 {
18321   rtx destexp;
18322   rtx countreg;
18323
18324   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
18325     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
18326   value = force_reg (mode, gen_lowpart (mode, value));
18327   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
18328   if (mode != QImode)
18329     {
18330       destexp = gen_rtx_ASHIFT (Pmode, countreg,
18331                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
18332       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
18333     }
18334   else
18335     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
18336   if (orig_value == const0_rtx && CONST_INT_P (count))
18337     {
18338       count = GEN_INT (INTVAL (count)
18339                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
18340       destmem = shallow_copy_rtx (destmem);
18341       set_mem_size (destmem, count);
18342     }
18343   else if (MEM_SIZE (destmem))
18344     set_mem_size (destmem, NULL_RTX);
18345   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
18346 }
18347
18348 static void
18349 emit_strmov (rtx destmem, rtx srcmem,
18350              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
18351 {
18352   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
18353   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
18354   emit_insn (gen_strmov (destptr, dest, srcptr, src));
18355 }
18356
18357 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
18358 static void
18359 expand_movmem_epilogue (rtx destmem, rtx srcmem,
18360                         rtx destptr, rtx srcptr, rtx count, int max_size)
18361 {
18362   rtx src, dest;
18363   if (CONST_INT_P (count))
18364     {
18365       HOST_WIDE_INT countval = INTVAL (count);
18366       int offset = 0;
18367
18368       if ((countval & 0x10) && max_size > 16)
18369         {
18370           if (TARGET_64BIT)
18371             {
18372               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
18373               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
18374             }
18375           else
18376             gcc_unreachable ();
18377           offset += 16;
18378         }
18379       if ((countval & 0x08) && max_size > 8)
18380         {
18381           if (TARGET_64BIT)
18382             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
18383           else
18384             {
18385               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
18386               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
18387             }
18388           offset += 8;
18389         }
18390       if ((countval & 0x04) && max_size > 4)
18391         {
18392           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
18393           offset += 4;
18394         }
18395       if ((countval & 0x02) && max_size > 2)
18396         {
18397           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
18398           offset += 2;
18399         }
18400       if ((countval & 0x01) && max_size > 1)
18401         {
18402           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
18403           offset += 1;
18404         }
18405       return;
18406     }
18407   if (max_size > 8)
18408     {
18409       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
18410                                     count, 1, OPTAB_DIRECT);
18411       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
18412                                      count, QImode, 1, 4);
18413       return;
18414     }
18415
18416   /* When there are stringops, we can cheaply increase dest and src pointers.
18417      Otherwise we save code size by maintaining offset (zero is readily
18418      available from preceding rep operation) and using x86 addressing modes.
18419    */
18420   if (TARGET_SINGLE_STRINGOP)
18421     {
18422       if (max_size > 4)
18423         {
18424           rtx label = ix86_expand_aligntest (count, 4, true);
18425           src = change_address (srcmem, SImode, srcptr);
18426           dest = change_address (destmem, SImode, destptr);
18427           emit_insn (gen_strmov (destptr, dest, srcptr, src));
18428           emit_label (label);
18429           LABEL_NUSES (label) = 1;
18430         }
18431       if (max_size > 2)
18432         {
18433           rtx label = ix86_expand_aligntest (count, 2, true);
18434           src = change_address (srcmem, HImode, srcptr);
18435           dest = change_address (destmem, HImode, destptr);
18436           emit_insn (gen_strmov (destptr, dest, srcptr, src));
18437           emit_label (label);
18438           LABEL_NUSES (label) = 1;
18439         }
18440       if (max_size > 1)
18441         {
18442           rtx label = ix86_expand_aligntest (count, 1, true);
18443           src = change_address (srcmem, QImode, srcptr);
18444           dest = change_address (destmem, QImode, destptr);
18445           emit_insn (gen_strmov (destptr, dest, srcptr, src));
18446           emit_label (label);
18447           LABEL_NUSES (label) = 1;
18448         }
18449     }
18450   else
18451     {
18452       rtx offset = force_reg (Pmode, const0_rtx);
18453       rtx tmp;
18454
18455       if (max_size > 4)
18456         {
18457           rtx label = ix86_expand_aligntest (count, 4, true);
18458           src = change_address (srcmem, SImode, srcptr);
18459           dest = change_address (destmem, SImode, destptr);
18460           emit_move_insn (dest, src);
18461           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
18462                                      true, OPTAB_LIB_WIDEN);
18463           if (tmp != offset)
18464             emit_move_insn (offset, tmp);
18465           emit_label (label);
18466           LABEL_NUSES (label) = 1;
18467         }
18468       if (max_size > 2)
18469         {
18470           rtx label = ix86_expand_aligntest (count, 2, true);
18471           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
18472           src = change_address (srcmem, HImode, tmp);
18473           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
18474           dest = change_address (destmem, HImode, tmp);
18475           emit_move_insn (dest, src);
18476           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
18477                                      true, OPTAB_LIB_WIDEN);
18478           if (tmp != offset)
18479             emit_move_insn (offset, tmp);
18480           emit_label (label);
18481           LABEL_NUSES (label) = 1;
18482         }
18483       if (max_size > 1)
18484         {
18485           rtx label = ix86_expand_aligntest (count, 1, true);
18486           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
18487           src = change_address (srcmem, QImode, tmp);
18488           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
18489           dest = change_address (destmem, QImode, tmp);
18490           emit_move_insn (dest, src);
18491           emit_label (label);
18492           LABEL_NUSES (label) = 1;
18493         }
18494     }
18495 }
18496
18497 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
18498 static void
18499 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
18500                                  rtx count, int max_size)
18501 {
18502   count =
18503     expand_simple_binop (counter_mode (count), AND, count,
18504                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
18505   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
18506                                  gen_lowpart (QImode, value), count, QImode,
18507                                  1, max_size / 2);
18508 }
18509
18510 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
18511 static void
18512 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
18513 {
18514   rtx dest;
18515
18516   if (CONST_INT_P (count))
18517     {
18518       HOST_WIDE_INT countval = INTVAL (count);
18519       int offset = 0;
18520
18521       if ((countval & 0x10) && max_size > 16)
18522         {
18523           if (TARGET_64BIT)
18524             {
18525               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
18526               emit_insn (gen_strset (destptr, dest, value));
18527               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
18528               emit_insn (gen_strset (destptr, dest, value));
18529             }
18530           else
18531             gcc_unreachable ();
18532           offset += 16;
18533         }
18534       if ((countval & 0x08) && max_size > 8)
18535         {
18536           if (TARGET_64BIT)
18537             {
18538               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
18539               emit_insn (gen_strset (destptr, dest, value));
18540             }
18541           else
18542             {
18543               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
18544               emit_insn (gen_strset (destptr, dest, value));
18545               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
18546               emit_insn (gen_strset (destptr, dest, value));
18547             }
18548           offset += 8;
18549         }
18550       if ((countval & 0x04) && max_size > 4)
18551         {
18552           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
18553           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
18554           offset += 4;
18555         }
18556       if ((countval & 0x02) && max_size > 2)
18557         {
18558           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
18559           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
18560           offset += 2;
18561         }
18562       if ((countval & 0x01) && max_size > 1)
18563         {
18564           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
18565           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
18566           offset += 1;
18567         }
18568       return;
18569     }
18570   if (max_size > 32)
18571     {
18572       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
18573       return;
18574     }
18575   if (max_size > 16)
18576     {
18577       rtx label = ix86_expand_aligntest (count, 16, true);
18578       if (TARGET_64BIT)
18579         {
18580           dest = change_address (destmem, DImode, destptr);
18581           emit_insn (gen_strset (destptr, dest, value));
18582           emit_insn (gen_strset (destptr, dest, value));
18583         }
18584       else
18585         {
18586           dest = change_address (destmem, SImode, destptr);
18587           emit_insn (gen_strset (destptr, dest, value));
18588           emit_insn (gen_strset (destptr, dest, value));
18589           emit_insn (gen_strset (destptr, dest, value));
18590           emit_insn (gen_strset (destptr, dest, value));
18591         }
18592       emit_label (label);
18593       LABEL_NUSES (label) = 1;
18594     }
18595   if (max_size > 8)
18596     {
18597       rtx label = ix86_expand_aligntest (count, 8, true);
18598       if (TARGET_64BIT)
18599         {
18600           dest = change_address (destmem, DImode, destptr);
18601           emit_insn (gen_strset (destptr, dest, value));
18602         }
18603       else
18604         {
18605           dest = change_address (destmem, SImode, destptr);
18606           emit_insn (gen_strset (destptr, dest, value));
18607           emit_insn (gen_strset (destptr, dest, value));
18608         }
18609       emit_label (label);
18610       LABEL_NUSES (label) = 1;
18611     }
18612   if (max_size > 4)
18613     {
18614       rtx label = ix86_expand_aligntest (count, 4, true);
18615       dest = change_address (destmem, SImode, destptr);
18616       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
18617       emit_label (label);
18618       LABEL_NUSES (label) = 1;
18619     }
18620   if (max_size > 2)
18621     {
18622       rtx label = ix86_expand_aligntest (count, 2, true);
18623       dest = change_address (destmem, HImode, destptr);
18624       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
18625       emit_label (label);
18626       LABEL_NUSES (label) = 1;
18627     }
18628   if (max_size > 1)
18629     {
18630       rtx label = ix86_expand_aligntest (count, 1, true);
18631       dest = change_address (destmem, QImode, destptr);
18632       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
18633       emit_label (label);
18634       LABEL_NUSES (label) = 1;
18635     }
18636 }
18637
18638 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
18639    DESIRED_ALIGNMENT.  */
18640 static void
18641 expand_movmem_prologue (rtx destmem, rtx srcmem,
18642                         rtx destptr, rtx srcptr, rtx count,
18643                         int align, int desired_alignment)
18644 {
18645   if (align <= 1 && desired_alignment > 1)
18646     {
18647       rtx label = ix86_expand_aligntest (destptr, 1, false);
18648       srcmem = change_address (srcmem, QImode, srcptr);
18649       destmem = change_address (destmem, QImode, destptr);
18650       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
18651       ix86_adjust_counter (count, 1);
18652       emit_label (label);
18653       LABEL_NUSES (label) = 1;
18654     }
18655   if (align <= 2 && desired_alignment > 2)
18656     {
18657       rtx label = ix86_expand_aligntest (destptr, 2, false);
18658       srcmem = change_address (srcmem, HImode, srcptr);
18659       destmem = change_address (destmem, HImode, destptr);
18660       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
18661       ix86_adjust_counter (count, 2);
18662       emit_label (label);
18663       LABEL_NUSES (label) = 1;
18664     }
18665   if (align <= 4 && desired_alignment > 4)
18666     {
18667       rtx label = ix86_expand_aligntest (destptr, 4, false);
18668       srcmem = change_address (srcmem, SImode, srcptr);
18669       destmem = change_address (destmem, SImode, destptr);
18670       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
18671       ix86_adjust_counter (count, 4);
18672       emit_label (label);
18673       LABEL_NUSES (label) = 1;
18674     }
18675   gcc_assert (desired_alignment <= 8);
18676 }
18677
18678 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
18679    ALIGN_BYTES is how many bytes need to be copied.  */
18680 static rtx
18681 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
18682                                  int desired_align, int align_bytes)
18683 {
18684   rtx src = *srcp;
18685   rtx src_size, dst_size;
18686   int off = 0;
18687   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
18688   if (src_align_bytes >= 0)
18689     src_align_bytes = desired_align - src_align_bytes;
18690   src_size = MEM_SIZE (src);
18691   dst_size = MEM_SIZE (dst);
18692   if (align_bytes & 1)
18693     {
18694       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
18695       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
18696       off = 1;
18697       emit_insn (gen_strmov (destreg, dst, srcreg, src));
18698     }
18699   if (align_bytes & 2)
18700     {
18701       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
18702       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
18703       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
18704         set_mem_align (dst, 2 * BITS_PER_UNIT);
18705       if (src_align_bytes >= 0
18706           && (src_align_bytes & 1) == (align_bytes & 1)
18707           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
18708         set_mem_align (src, 2 * BITS_PER_UNIT);
18709       off = 2;
18710       emit_insn (gen_strmov (destreg, dst, srcreg, src));
18711     }
18712   if (align_bytes & 4)
18713     {
18714       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
18715       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
18716       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
18717         set_mem_align (dst, 4 * BITS_PER_UNIT);
18718       if (src_align_bytes >= 0)
18719         {
18720           unsigned int src_align = 0;
18721           if ((src_align_bytes & 3) == (align_bytes & 3))
18722             src_align = 4;
18723           else if ((src_align_bytes & 1) == (align_bytes & 1))
18724             src_align = 2;
18725           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
18726             set_mem_align (src, src_align * BITS_PER_UNIT);
18727         }
18728       off = 4;
18729       emit_insn (gen_strmov (destreg, dst, srcreg, src));
18730     }
18731   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
18732   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
18733   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
18734     set_mem_align (dst, desired_align * BITS_PER_UNIT);
18735   if (src_align_bytes >= 0)
18736     {
18737       unsigned int src_align = 0;
18738       if ((src_align_bytes & 7) == (align_bytes & 7))
18739         src_align = 8;
18740       else if ((src_align_bytes & 3) == (align_bytes & 3))
18741         src_align = 4;
18742       else if ((src_align_bytes & 1) == (align_bytes & 1))
18743         src_align = 2;
18744       if (src_align > (unsigned int) desired_align)
18745         src_align = desired_align;
18746       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
18747         set_mem_align (src, src_align * BITS_PER_UNIT);
18748     }
18749   if (dst_size)
18750     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
18751   if (src_size)
18752     set_mem_size (dst, GEN_INT (INTVAL (src_size) - align_bytes));
18753   *srcp = src;
18754   return dst;
18755 }
18756
18757 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
18758    DESIRED_ALIGNMENT.  */
18759 static void
18760 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
18761                         int align, int desired_alignment)
18762 {
18763   if (align <= 1 && desired_alignment > 1)
18764     {
18765       rtx label = ix86_expand_aligntest (destptr, 1, false);
18766       destmem = change_address (destmem, QImode, destptr);
18767       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
18768       ix86_adjust_counter (count, 1);
18769       emit_label (label);
18770       LABEL_NUSES (label) = 1;
18771     }
18772   if (align <= 2 && desired_alignment > 2)
18773     {
18774       rtx label = ix86_expand_aligntest (destptr, 2, false);
18775       destmem = change_address (destmem, HImode, destptr);
18776       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
18777       ix86_adjust_counter (count, 2);
18778       emit_label (label);
18779       LABEL_NUSES (label) = 1;
18780     }
18781   if (align <= 4 && desired_alignment > 4)
18782     {
18783       rtx label = ix86_expand_aligntest (destptr, 4, false);
18784       destmem = change_address (destmem, SImode, destptr);
18785       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
18786       ix86_adjust_counter (count, 4);
18787       emit_label (label);
18788       LABEL_NUSES (label) = 1;
18789     }
18790   gcc_assert (desired_alignment <= 8);
18791 }
18792
18793 /* Set enough from DST to align DST known to by aligned by ALIGN to
18794    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
18795 static rtx
18796 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
18797                                  int desired_align, int align_bytes)
18798 {
18799   int off = 0;
18800   rtx dst_size = MEM_SIZE (dst);
18801   if (align_bytes & 1)
18802     {
18803       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
18804       off = 1;
18805       emit_insn (gen_strset (destreg, dst,
18806                              gen_lowpart (QImode, value)));
18807     }
18808   if (align_bytes & 2)
18809     {
18810       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
18811       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
18812         set_mem_align (dst, 2 * BITS_PER_UNIT);
18813       off = 2;
18814       emit_insn (gen_strset (destreg, dst,
18815                              gen_lowpart (HImode, value)));
18816     }
18817   if (align_bytes & 4)
18818     {
18819       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
18820       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
18821         set_mem_align (dst, 4 * BITS_PER_UNIT);
18822       off = 4;
18823       emit_insn (gen_strset (destreg, dst,
18824                              gen_lowpart (SImode, value)));
18825     }
18826   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
18827   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
18828     set_mem_align (dst, desired_align * BITS_PER_UNIT);
18829   if (dst_size)
18830     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
18831   return dst;
18832 }
18833
18834 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
18835 static enum stringop_alg
18836 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
18837             int *dynamic_check)
18838 {
18839   const struct stringop_algs * algs;
18840   bool optimize_for_speed;
18841   /* Algorithms using the rep prefix want at least edi and ecx;
18842      additionally, memset wants eax and memcpy wants esi.  Don't
18843      consider such algorithms if the user has appropriated those
18844      registers for their own purposes.  */
18845   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
18846                              || (memset
18847                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
18848
18849 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
18850                            || (alg != rep_prefix_1_byte         \
18851                                && alg != rep_prefix_4_byte      \
18852                                && alg != rep_prefix_8_byte))
18853   const struct processor_costs *cost;
18854
18855   /* Even if the string operation call is cold, we still might spend a lot
18856      of time processing large blocks.  */
18857   if (optimize_function_for_size_p (cfun)
18858       || (optimize_insn_for_size_p ()
18859           && expected_size != -1 && expected_size < 256))
18860     optimize_for_speed = false;
18861   else
18862     optimize_for_speed = true;
18863
18864   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
18865
18866   *dynamic_check = -1;
18867   if (memset)
18868     algs = &cost->memset[TARGET_64BIT != 0];
18869   else
18870     algs = &cost->memcpy[TARGET_64BIT != 0];
18871   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
18872     return stringop_alg;
18873   /* rep; movq or rep; movl is the smallest variant.  */
18874   else if (!optimize_for_speed)
18875     {
18876       if (!count || (count & 3))
18877         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
18878       else
18879         return rep_prefix_usable ? rep_prefix_4_byte : loop;
18880     }
18881   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
18882    */
18883   else if (expected_size != -1 && expected_size < 4)
18884     return loop_1_byte;
18885   else if (expected_size != -1)
18886     {
18887       unsigned int i;
18888       enum stringop_alg alg = libcall;
18889       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
18890         {
18891           /* We get here if the algorithms that were not libcall-based
18892              were rep-prefix based and we are unable to use rep prefixes
18893              based on global register usage.  Break out of the loop and
18894              use the heuristic below.  */
18895           if (algs->size[i].max == 0)
18896             break;
18897           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
18898             {
18899               enum stringop_alg candidate = algs->size[i].alg;
18900
18901               if (candidate != libcall && ALG_USABLE_P (candidate))
18902                 alg = candidate;
18903               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
18904                  last non-libcall inline algorithm.  */
18905               if (TARGET_INLINE_ALL_STRINGOPS)
18906                 {
18907                   /* When the current size is best to be copied by a libcall,
18908                      but we are still forced to inline, run the heuristic below
18909                      that will pick code for medium sized blocks.  */
18910                   if (alg != libcall)
18911                     return alg;
18912                   break;
18913                 }
18914               else if (ALG_USABLE_P (candidate))
18915                 return candidate;
18916             }
18917         }
18918       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
18919     }
18920   /* When asked to inline the call anyway, try to pick meaningful choice.
18921      We look for maximal size of block that is faster to copy by hand and
18922      take blocks of at most of that size guessing that average size will
18923      be roughly half of the block.
18924
18925      If this turns out to be bad, we might simply specify the preferred
18926      choice in ix86_costs.  */
18927   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18928       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
18929     {
18930       int max = -1;
18931       enum stringop_alg alg;
18932       int i;
18933       bool any_alg_usable_p = true;
18934
18935       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
18936         {
18937           enum stringop_alg candidate = algs->size[i].alg;
18938           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
18939
18940           if (candidate != libcall && candidate
18941               && ALG_USABLE_P (candidate))
18942               max = algs->size[i].max;
18943         }
18944       /* If there aren't any usable algorithms, then recursing on
18945          smaller sizes isn't going to find anything.  Just return the
18946          simple byte-at-a-time copy loop.  */
18947       if (!any_alg_usable_p)
18948         {
18949           /* Pick something reasonable.  */
18950           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18951             *dynamic_check = 128;
18952           return loop_1_byte;
18953         }
18954       if (max == -1)
18955         max = 4096;
18956       alg = decide_alg (count, max / 2, memset, dynamic_check);
18957       gcc_assert (*dynamic_check == -1);
18958       gcc_assert (alg != libcall);
18959       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
18960         *dynamic_check = max;
18961       return alg;
18962     }
18963   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
18964 #undef ALG_USABLE_P
18965 }
18966
18967 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
18968    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
18969 static int
18970 decide_alignment (int align,
18971                   enum stringop_alg alg,
18972                   int expected_size)
18973 {
18974   int desired_align = 0;
18975   switch (alg)
18976     {
18977       case no_stringop:
18978         gcc_unreachable ();
18979       case loop:
18980       case unrolled_loop:
18981         desired_align = GET_MODE_SIZE (Pmode);
18982         break;
18983       case rep_prefix_8_byte:
18984         desired_align = 8;
18985         break;
18986       case rep_prefix_4_byte:
18987         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
18988            copying whole cacheline at once.  */
18989         if (TARGET_PENTIUMPRO)
18990           desired_align = 8;
18991         else
18992           desired_align = 4;
18993         break;
18994       case rep_prefix_1_byte:
18995         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
18996            copying whole cacheline at once.  */
18997         if (TARGET_PENTIUMPRO)
18998           desired_align = 8;
18999         else
19000           desired_align = 1;
19001         break;
19002       case loop_1_byte:
19003         desired_align = 1;
19004         break;
19005       case libcall:
19006         return 0;
19007     }
19008
19009   if (optimize_size)
19010     desired_align = 1;
19011   if (desired_align < align)
19012     desired_align = align;
19013   if (expected_size != -1 && expected_size < 4)
19014     desired_align = align;
19015   return desired_align;
19016 }
19017
19018 /* Return the smallest power of 2 greater than VAL.  */
19019 static int
19020 smallest_pow2_greater_than (int val)
19021 {
19022   int ret = 1;
19023   while (ret <= val)
19024     ret <<= 1;
19025   return ret;
19026 }
19027
19028 /* Expand string move (memcpy) operation.  Use i386 string operations when
19029    profitable.  expand_setmem contains similar code.  The code depends upon
19030    architecture, block size and alignment, but always has the same
19031    overall structure:
19032
19033    1) Prologue guard: Conditional that jumps up to epilogues for small
19034       blocks that can be handled by epilogue alone.  This is faster but
19035       also needed for correctness, since prologue assume the block is larger
19036       than the desired alignment.
19037
19038       Optional dynamic check for size and libcall for large
19039       blocks is emitted here too, with -minline-stringops-dynamically.
19040
19041    2) Prologue: copy first few bytes in order to get destination aligned
19042       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
19043       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
19044       We emit either a jump tree on power of two sized blocks, or a byte loop.
19045
19046    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
19047       with specified algorithm.
19048
19049    4) Epilogue: code copying tail of the block that is too small to be
19050       handled by main body (or up to size guarded by prologue guard).  */
19051
19052 int
19053 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
19054                     rtx expected_align_exp, rtx expected_size_exp)
19055 {
19056   rtx destreg;
19057   rtx srcreg;
19058   rtx label = NULL;
19059   rtx tmp;
19060   rtx jump_around_label = NULL;
19061   HOST_WIDE_INT align = 1;
19062   unsigned HOST_WIDE_INT count = 0;
19063   HOST_WIDE_INT expected_size = -1;
19064   int size_needed = 0, epilogue_size_needed;
19065   int desired_align = 0, align_bytes = 0;
19066   enum stringop_alg alg;
19067   int dynamic_check;
19068   bool need_zero_guard = false;
19069
19070   if (CONST_INT_P (align_exp))
19071     align = INTVAL (align_exp);
19072   /* i386 can do misaligned access on reasonably increased cost.  */
19073   if (CONST_INT_P (expected_align_exp)
19074       && INTVAL (expected_align_exp) > align)
19075     align = INTVAL (expected_align_exp);
19076   /* ALIGN is the minimum of destination and source alignment, but we care here
19077      just about destination alignment.  */
19078   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
19079     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
19080
19081   if (CONST_INT_P (count_exp))
19082     count = expected_size = INTVAL (count_exp);
19083   if (CONST_INT_P (expected_size_exp) && count == 0)
19084     expected_size = INTVAL (expected_size_exp);
19085
19086   /* Make sure we don't need to care about overflow later on.  */
19087   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
19088     return 0;
19089
19090   /* Step 0: Decide on preferred algorithm, desired alignment and
19091      size of chunks to be copied by main loop.  */
19092
19093   alg = decide_alg (count, expected_size, false, &dynamic_check);
19094   desired_align = decide_alignment (align, alg, expected_size);
19095
19096   if (!TARGET_ALIGN_STRINGOPS)
19097     align = desired_align;
19098
19099   if (alg == libcall)
19100     return 0;
19101   gcc_assert (alg != no_stringop);
19102   if (!count)
19103     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
19104   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
19105   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
19106   switch (alg)
19107     {
19108     case libcall:
19109     case no_stringop:
19110       gcc_unreachable ();
19111     case loop:
19112       need_zero_guard = true;
19113       size_needed = GET_MODE_SIZE (Pmode);
19114       break;
19115     case unrolled_loop:
19116       need_zero_guard = true;
19117       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
19118       break;
19119     case rep_prefix_8_byte:
19120       size_needed = 8;
19121       break;
19122     case rep_prefix_4_byte:
19123       size_needed = 4;
19124       break;
19125     case rep_prefix_1_byte:
19126       size_needed = 1;
19127       break;
19128     case loop_1_byte:
19129       need_zero_guard = true;
19130       size_needed = 1;
19131       break;
19132     }
19133
19134   epilogue_size_needed = size_needed;
19135
19136   /* Step 1: Prologue guard.  */
19137
19138   /* Alignment code needs count to be in register.  */
19139   if (CONST_INT_P (count_exp) && desired_align > align)
19140     {
19141       if (INTVAL (count_exp) > desired_align
19142           && INTVAL (count_exp) > size_needed)
19143         {
19144           align_bytes
19145             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
19146           if (align_bytes <= 0)
19147             align_bytes = 0;
19148           else
19149             align_bytes = desired_align - align_bytes;
19150         }
19151       if (align_bytes == 0)
19152         count_exp = force_reg (counter_mode (count_exp), count_exp);
19153     }
19154   gcc_assert (desired_align >= 1 && align >= 1);
19155
19156   /* Ensure that alignment prologue won't copy past end of block.  */
19157   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
19158     {
19159       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
19160       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
19161          Make sure it is power of 2.  */
19162       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
19163
19164       if (count)
19165         {
19166           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
19167             {
19168               /* If main algorithm works on QImode, no epilogue is needed.
19169                  For small sizes just don't align anything.  */
19170               if (size_needed == 1)
19171                 desired_align = align;
19172               else
19173                 goto epilogue;
19174             }
19175         }
19176       else
19177         {
19178           label = gen_label_rtx ();
19179           emit_cmp_and_jump_insns (count_exp,
19180                                    GEN_INT (epilogue_size_needed),
19181                                    LTU, 0, counter_mode (count_exp), 1, label);
19182           if (expected_size == -1 || expected_size < epilogue_size_needed)
19183             predict_jump (REG_BR_PROB_BASE * 60 / 100);
19184           else
19185             predict_jump (REG_BR_PROB_BASE * 20 / 100);
19186         }
19187     }
19188
19189   /* Emit code to decide on runtime whether library call or inline should be
19190      used.  */
19191   if (dynamic_check != -1)
19192     {
19193       if (CONST_INT_P (count_exp))
19194         {
19195           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
19196             {
19197               emit_block_move_via_libcall (dst, src, count_exp, false);
19198               count_exp = const0_rtx;
19199               goto epilogue;
19200             }
19201         }
19202       else
19203         {
19204           rtx hot_label = gen_label_rtx ();
19205           jump_around_label = gen_label_rtx ();
19206           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
19207                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
19208           predict_jump (REG_BR_PROB_BASE * 90 / 100);
19209           emit_block_move_via_libcall (dst, src, count_exp, false);
19210           emit_jump (jump_around_label);
19211           emit_label (hot_label);
19212         }
19213     }
19214
19215   /* Step 2: Alignment prologue.  */
19216
19217   if (desired_align > align)
19218     {
19219       if (align_bytes == 0)
19220         {
19221           /* Except for the first move in epilogue, we no longer know
19222              constant offset in aliasing info.  It don't seems to worth
19223              the pain to maintain it for the first move, so throw away
19224              the info early.  */
19225           src = change_address (src, BLKmode, srcreg);
19226           dst = change_address (dst, BLKmode, destreg);
19227           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
19228                                   desired_align);
19229         }
19230       else
19231         {
19232           /* If we know how many bytes need to be stored before dst is
19233              sufficiently aligned, maintain aliasing info accurately.  */
19234           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
19235                                                  desired_align, align_bytes);
19236           count_exp = plus_constant (count_exp, -align_bytes);
19237           count -= align_bytes;
19238         }
19239       if (need_zero_guard
19240           && (count < (unsigned HOST_WIDE_INT) size_needed
19241               || (align_bytes == 0
19242                   && count < ((unsigned HOST_WIDE_INT) size_needed
19243                               + desired_align - align))))
19244         {
19245           /* It is possible that we copied enough so the main loop will not
19246              execute.  */
19247           gcc_assert (size_needed > 1);
19248           if (label == NULL_RTX)
19249             label = gen_label_rtx ();
19250           emit_cmp_and_jump_insns (count_exp,
19251                                    GEN_INT (size_needed),
19252                                    LTU, 0, counter_mode (count_exp), 1, label);
19253           if (expected_size == -1
19254               || expected_size < (desired_align - align) / 2 + size_needed)
19255             predict_jump (REG_BR_PROB_BASE * 20 / 100);
19256           else
19257             predict_jump (REG_BR_PROB_BASE * 60 / 100);
19258         }
19259     }
19260   if (label && size_needed == 1)
19261     {
19262       emit_label (label);
19263       LABEL_NUSES (label) = 1;
19264       label = NULL;
19265       epilogue_size_needed = 1;
19266     }
19267   else if (label == NULL_RTX)
19268     epilogue_size_needed = size_needed;
19269
19270   /* Step 3: Main loop.  */
19271
19272   switch (alg)
19273     {
19274     case libcall:
19275     case no_stringop:
19276       gcc_unreachable ();
19277     case loop_1_byte:
19278       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
19279                                      count_exp, QImode, 1, expected_size);
19280       break;
19281     case loop:
19282       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
19283                                      count_exp, Pmode, 1, expected_size);
19284       break;
19285     case unrolled_loop:
19286       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
19287          registers for 4 temporaries anyway.  */
19288       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
19289                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
19290                                      expected_size);
19291       break;
19292     case rep_prefix_8_byte:
19293       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
19294                                  DImode);
19295       break;
19296     case rep_prefix_4_byte:
19297       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
19298                                  SImode);
19299       break;
19300     case rep_prefix_1_byte:
19301       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
19302                                  QImode);
19303       break;
19304     }
19305   /* Adjust properly the offset of src and dest memory for aliasing.  */
19306   if (CONST_INT_P (count_exp))
19307     {
19308       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
19309                                           (count / size_needed) * size_needed);
19310       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
19311                                           (count / size_needed) * size_needed);
19312     }
19313   else
19314     {
19315       src = change_address (src, BLKmode, srcreg);
19316       dst = change_address (dst, BLKmode, destreg);
19317     }
19318
19319   /* Step 4: Epilogue to copy the remaining bytes.  */
19320  epilogue:
19321   if (label)
19322     {
19323       /* When the main loop is done, COUNT_EXP might hold original count,
19324          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
19325          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
19326          bytes. Compensate if needed.  */
19327
19328       if (size_needed < epilogue_size_needed)
19329         {
19330           tmp =
19331             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
19332                                  GEN_INT (size_needed - 1), count_exp, 1,
19333                                  OPTAB_DIRECT);
19334           if (tmp != count_exp)
19335             emit_move_insn (count_exp, tmp);
19336         }
19337       emit_label (label);
19338       LABEL_NUSES (label) = 1;
19339     }
19340
19341   if (count_exp != const0_rtx && epilogue_size_needed > 1)
19342     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
19343                             epilogue_size_needed);
19344   if (jump_around_label)
19345     emit_label (jump_around_label);
19346   return 1;
19347 }
19348
19349 /* Helper function for memcpy.  For QImode value 0xXY produce
19350    0xXYXYXYXY of wide specified by MODE.  This is essentially
19351    a * 0x10101010, but we can do slightly better than
19352    synth_mult by unwinding the sequence by hand on CPUs with
19353    slow multiply.  */
19354 static rtx
19355 promote_duplicated_reg (enum machine_mode mode, rtx val)
19356 {
19357   enum machine_mode valmode = GET_MODE (val);
19358   rtx tmp;
19359   int nops = mode == DImode ? 3 : 2;
19360
19361   gcc_assert (mode == SImode || mode == DImode);
19362   if (val == const0_rtx)
19363     return copy_to_mode_reg (mode, const0_rtx);
19364   if (CONST_INT_P (val))
19365     {
19366       HOST_WIDE_INT v = INTVAL (val) & 255;
19367
19368       v |= v << 8;
19369       v |= v << 16;
19370       if (mode == DImode)
19371         v |= (v << 16) << 16;
19372       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
19373     }
19374
19375   if (valmode == VOIDmode)
19376     valmode = QImode;
19377   if (valmode != QImode)
19378     val = gen_lowpart (QImode, val);
19379   if (mode == QImode)
19380     return val;
19381   if (!TARGET_PARTIAL_REG_STALL)
19382     nops--;
19383   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
19384       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
19385       <= (ix86_cost->shift_const + ix86_cost->add) * nops
19386           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
19387     {
19388       rtx reg = convert_modes (mode, QImode, val, true);
19389       tmp = promote_duplicated_reg (mode, const1_rtx);
19390       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
19391                                   OPTAB_DIRECT);
19392     }
19393   else
19394     {
19395       rtx reg = convert_modes (mode, QImode, val, true);
19396
19397       if (!TARGET_PARTIAL_REG_STALL)
19398         if (mode == SImode)
19399           emit_insn (gen_movsi_insv_1 (reg, reg));
19400         else
19401           emit_insn (gen_movdi_insv_1 (reg, reg));
19402       else
19403         {
19404           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
19405                                      NULL, 1, OPTAB_DIRECT);
19406           reg =
19407             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
19408         }
19409       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
19410                                  NULL, 1, OPTAB_DIRECT);
19411       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
19412       if (mode == SImode)
19413         return reg;
19414       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
19415                                  NULL, 1, OPTAB_DIRECT);
19416       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
19417       return reg;
19418     }
19419 }
19420
19421 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
19422    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
19423    alignment from ALIGN to DESIRED_ALIGN.  */
19424 static rtx
19425 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
19426 {
19427   rtx promoted_val;
19428
19429   if (TARGET_64BIT
19430       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
19431     promoted_val = promote_duplicated_reg (DImode, val);
19432   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
19433     promoted_val = promote_duplicated_reg (SImode, val);
19434   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
19435     promoted_val = promote_duplicated_reg (HImode, val);
19436   else
19437     promoted_val = val;
19438
19439   return promoted_val;
19440 }
19441
19442 /* Expand string clear operation (bzero).  Use i386 string operations when
19443    profitable.  See expand_movmem comment for explanation of individual
19444    steps performed.  */
19445 int
19446 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
19447                     rtx expected_align_exp, rtx expected_size_exp)
19448 {
19449   rtx destreg;
19450   rtx label = NULL;
19451   rtx tmp;
19452   rtx jump_around_label = NULL;
19453   HOST_WIDE_INT align = 1;
19454   unsigned HOST_WIDE_INT count = 0;
19455   HOST_WIDE_INT expected_size = -1;
19456   int size_needed = 0, epilogue_size_needed;
19457   int desired_align = 0, align_bytes = 0;
19458   enum stringop_alg alg;
19459   rtx promoted_val = NULL;
19460   bool force_loopy_epilogue = false;
19461   int dynamic_check;
19462   bool need_zero_guard = false;
19463
19464   if (CONST_INT_P (align_exp))
19465     align = INTVAL (align_exp);
19466   /* i386 can do misaligned access on reasonably increased cost.  */
19467   if (CONST_INT_P (expected_align_exp)
19468       && INTVAL (expected_align_exp) > align)
19469     align = INTVAL (expected_align_exp);
19470   if (CONST_INT_P (count_exp))
19471     count = expected_size = INTVAL (count_exp);
19472   if (CONST_INT_P (expected_size_exp) && count == 0)
19473     expected_size = INTVAL (expected_size_exp);
19474
19475   /* Make sure we don't need to care about overflow later on.  */
19476   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
19477     return 0;
19478
19479   /* Step 0: Decide on preferred algorithm, desired alignment and
19480      size of chunks to be copied by main loop.  */
19481
19482   alg = decide_alg (count, expected_size, true, &dynamic_check);
19483   desired_align = decide_alignment (align, alg, expected_size);
19484
19485   if (!TARGET_ALIGN_STRINGOPS)
19486     align = desired_align;
19487
19488   if (alg == libcall)
19489     return 0;
19490   gcc_assert (alg != no_stringop);
19491   if (!count)
19492     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
19493   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
19494   switch (alg)
19495     {
19496     case libcall:
19497     case no_stringop:
19498       gcc_unreachable ();
19499     case loop:
19500       need_zero_guard = true;
19501       size_needed = GET_MODE_SIZE (Pmode);
19502       break;
19503     case unrolled_loop:
19504       need_zero_guard = true;
19505       size_needed = GET_MODE_SIZE (Pmode) * 4;
19506       break;
19507     case rep_prefix_8_byte:
19508       size_needed = 8;
19509       break;
19510     case rep_prefix_4_byte:
19511       size_needed = 4;
19512       break;
19513     case rep_prefix_1_byte:
19514       size_needed = 1;
19515       break;
19516     case loop_1_byte:
19517       need_zero_guard = true;
19518       size_needed = 1;
19519       break;
19520     }
19521   epilogue_size_needed = size_needed;
19522
19523   /* Step 1: Prologue guard.  */
19524
19525   /* Alignment code needs count to be in register.  */
19526   if (CONST_INT_P (count_exp) && desired_align > align)
19527     {
19528       if (INTVAL (count_exp) > desired_align
19529           && INTVAL (count_exp) > size_needed)
19530         {
19531           align_bytes
19532             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
19533           if (align_bytes <= 0)
19534             align_bytes = 0;
19535           else
19536             align_bytes = desired_align - align_bytes;
19537         }
19538       if (align_bytes == 0)
19539         {
19540           enum machine_mode mode = SImode;
19541           if (TARGET_64BIT && (count & ~0xffffffff))
19542             mode = DImode;
19543           count_exp = force_reg (mode, count_exp);
19544         }
19545     }
19546   /* Do the cheap promotion to allow better CSE across the
19547      main loop and epilogue (ie one load of the big constant in the
19548      front of all code.  */
19549   if (CONST_INT_P (val_exp))
19550     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
19551                                                    desired_align, align);
19552   /* Ensure that alignment prologue won't copy past end of block.  */
19553   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
19554     {
19555       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
19556       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
19557          Make sure it is power of 2.  */
19558       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
19559
19560       /* To improve performance of small blocks, we jump around the VAL
19561          promoting mode.  This mean that if the promoted VAL is not constant,
19562          we might not use it in the epilogue and have to use byte
19563          loop variant.  */
19564       if (epilogue_size_needed > 2 && !promoted_val)
19565         force_loopy_epilogue = true;
19566       if (count)
19567         {
19568           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
19569             {
19570               /* If main algorithm works on QImode, no epilogue is needed.
19571                  For small sizes just don't align anything.  */
19572               if (size_needed == 1)
19573                 desired_align = align;
19574               else
19575                 goto epilogue;
19576             }
19577         }
19578       else
19579         {
19580           label = gen_label_rtx ();
19581           emit_cmp_and_jump_insns (count_exp,
19582                                    GEN_INT (epilogue_size_needed),
19583                                    LTU, 0, counter_mode (count_exp), 1, label);
19584           if (expected_size == -1 || expected_size <= epilogue_size_needed)
19585             predict_jump (REG_BR_PROB_BASE * 60 / 100);
19586           else
19587             predict_jump (REG_BR_PROB_BASE * 20 / 100);
19588         }
19589     }
19590   if (dynamic_check != -1)
19591     {
19592       rtx hot_label = gen_label_rtx ();
19593       jump_around_label = gen_label_rtx ();
19594       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
19595                                LEU, 0, counter_mode (count_exp), 1, hot_label);
19596       predict_jump (REG_BR_PROB_BASE * 90 / 100);
19597       set_storage_via_libcall (dst, count_exp, val_exp, false);
19598       emit_jump (jump_around_label);
19599       emit_label (hot_label);
19600     }
19601
19602   /* Step 2: Alignment prologue.  */
19603
19604   /* Do the expensive promotion once we branched off the small blocks.  */
19605   if (!promoted_val)
19606     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
19607                                                    desired_align, align);
19608   gcc_assert (desired_align >= 1 && align >= 1);
19609
19610   if (desired_align > align)
19611     {
19612       if (align_bytes == 0)
19613         {
19614           /* Except for the first move in epilogue, we no longer know
19615              constant offset in aliasing info.  It don't seems to worth
19616              the pain to maintain it for the first move, so throw away
19617              the info early.  */
19618           dst = change_address (dst, BLKmode, destreg);
19619           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
19620                                   desired_align);
19621         }
19622       else
19623         {
19624           /* If we know how many bytes need to be stored before dst is
19625              sufficiently aligned, maintain aliasing info accurately.  */
19626           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
19627                                                  desired_align, align_bytes);
19628           count_exp = plus_constant (count_exp, -align_bytes);
19629           count -= align_bytes;
19630         }
19631       if (need_zero_guard
19632           && (count < (unsigned HOST_WIDE_INT) size_needed
19633               || (align_bytes == 0
19634                   && count < ((unsigned HOST_WIDE_INT) size_needed
19635                               + desired_align - align))))
19636         {
19637           /* It is possible that we copied enough so the main loop will not
19638              execute.  */
19639           gcc_assert (size_needed > 1);
19640           if (label == NULL_RTX)
19641             label = gen_label_rtx ();
19642           emit_cmp_and_jump_insns (count_exp,
19643                                    GEN_INT (size_needed),
19644                                    LTU, 0, counter_mode (count_exp), 1, label);
19645           if (expected_size == -1
19646               || expected_size < (desired_align - align) / 2 + size_needed)
19647             predict_jump (REG_BR_PROB_BASE * 20 / 100);
19648           else
19649             predict_jump (REG_BR_PROB_BASE * 60 / 100);
19650         }
19651     }
19652   if (label && size_needed == 1)
19653     {
19654       emit_label (label);
19655       LABEL_NUSES (label) = 1;
19656       label = NULL;
19657       promoted_val = val_exp;
19658       epilogue_size_needed = 1;
19659     }
19660   else if (label == NULL_RTX)
19661     epilogue_size_needed = size_needed;
19662
19663   /* Step 3: Main loop.  */
19664
19665   switch (alg)
19666     {
19667     case libcall:
19668     case no_stringop:
19669       gcc_unreachable ();
19670     case loop_1_byte:
19671       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
19672                                      count_exp, QImode, 1, expected_size);
19673       break;
19674     case loop:
19675       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
19676                                      count_exp, Pmode, 1, expected_size);
19677       break;
19678     case unrolled_loop:
19679       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
19680                                      count_exp, Pmode, 4, expected_size);
19681       break;
19682     case rep_prefix_8_byte:
19683       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
19684                                   DImode, val_exp);
19685       break;
19686     case rep_prefix_4_byte:
19687       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
19688                                   SImode, val_exp);
19689       break;
19690     case rep_prefix_1_byte:
19691       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
19692                                   QImode, val_exp);
19693       break;
19694     }
19695   /* Adjust properly the offset of src and dest memory for aliasing.  */
19696   if (CONST_INT_P (count_exp))
19697     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
19698                                         (count / size_needed) * size_needed);
19699   else
19700     dst = change_address (dst, BLKmode, destreg);
19701
19702   /* Step 4: Epilogue to copy the remaining bytes.  */
19703
19704   if (label)
19705     {
19706       /* When the main loop is done, COUNT_EXP might hold original count,
19707          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
19708          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
19709          bytes. Compensate if needed.  */
19710
19711       if (size_needed < epilogue_size_needed)
19712         {
19713           tmp =
19714             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
19715                                  GEN_INT (size_needed - 1), count_exp, 1,
19716                                  OPTAB_DIRECT);
19717           if (tmp != count_exp)
19718             emit_move_insn (count_exp, tmp);
19719         }
19720       emit_label (label);
19721       LABEL_NUSES (label) = 1;
19722     }
19723  epilogue:
19724   if (count_exp != const0_rtx && epilogue_size_needed > 1)
19725     {
19726       if (force_loopy_epilogue)
19727         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
19728                                          epilogue_size_needed);
19729       else
19730         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
19731                                 epilogue_size_needed);
19732     }
19733   if (jump_around_label)
19734     emit_label (jump_around_label);
19735   return 1;
19736 }
19737
19738 /* Expand the appropriate insns for doing strlen if not just doing
19739    repnz; scasb
19740
19741    out = result, initialized with the start address
19742    align_rtx = alignment of the address.
19743    scratch = scratch register, initialized with the startaddress when
19744         not aligned, otherwise undefined
19745
19746    This is just the body. It needs the initializations mentioned above and
19747    some address computing at the end.  These things are done in i386.md.  */
19748
19749 static void
19750 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
19751 {
19752   int align;
19753   rtx tmp;
19754   rtx align_2_label = NULL_RTX;
19755   rtx align_3_label = NULL_RTX;
19756   rtx align_4_label = gen_label_rtx ();
19757   rtx end_0_label = gen_label_rtx ();
19758   rtx mem;
19759   rtx tmpreg = gen_reg_rtx (SImode);
19760   rtx scratch = gen_reg_rtx (SImode);
19761   rtx cmp;
19762
19763   align = 0;
19764   if (CONST_INT_P (align_rtx))
19765     align = INTVAL (align_rtx);
19766
19767   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
19768
19769   /* Is there a known alignment and is it less than 4?  */
19770   if (align < 4)
19771     {
19772       rtx scratch1 = gen_reg_rtx (Pmode);
19773       emit_move_insn (scratch1, out);
19774       /* Is there a known alignment and is it not 2? */
19775       if (align != 2)
19776         {
19777           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
19778           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
19779
19780           /* Leave just the 3 lower bits.  */
19781           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
19782                                     NULL_RTX, 0, OPTAB_WIDEN);
19783
19784           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
19785                                    Pmode, 1, align_4_label);
19786           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
19787                                    Pmode, 1, align_2_label);
19788           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
19789                                    Pmode, 1, align_3_label);
19790         }
19791       else
19792         {
19793           /* Since the alignment is 2, we have to check 2 or 0 bytes;
19794              check if is aligned to 4 - byte.  */
19795
19796           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
19797                                     NULL_RTX, 0, OPTAB_WIDEN);
19798
19799           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
19800                                    Pmode, 1, align_4_label);
19801         }
19802
19803       mem = change_address (src, QImode, out);
19804
19805       /* Now compare the bytes.  */
19806
19807       /* Compare the first n unaligned byte on a byte per byte basis.  */
19808       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
19809                                QImode, 1, end_0_label);
19810
19811       /* Increment the address.  */
19812       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
19813
19814       /* Not needed with an alignment of 2 */
19815       if (align != 2)
19816         {
19817           emit_label (align_2_label);
19818
19819           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
19820                                    end_0_label);
19821
19822           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
19823
19824           emit_label (align_3_label);
19825         }
19826
19827       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
19828                                end_0_label);
19829
19830       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
19831     }
19832
19833   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
19834      align this loop.  It gives only huge programs, but does not help to
19835      speed up.  */
19836   emit_label (align_4_label);
19837
19838   mem = change_address (src, SImode, out);
19839   emit_move_insn (scratch, mem);
19840   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
19841
19842   /* This formula yields a nonzero result iff one of the bytes is zero.
19843      This saves three branches inside loop and many cycles.  */
19844
19845   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
19846   emit_insn (gen_one_cmplsi2 (scratch, scratch));
19847   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
19848   emit_insn (gen_andsi3 (tmpreg, tmpreg,
19849                          gen_int_mode (0x80808080, SImode)));
19850   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
19851                            align_4_label);
19852
19853   if (TARGET_CMOVE)
19854     {
19855        rtx reg = gen_reg_rtx (SImode);
19856        rtx reg2 = gen_reg_rtx (Pmode);
19857        emit_move_insn (reg, tmpreg);
19858        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
19859
19860        /* If zero is not in the first two bytes, move two bytes forward.  */
19861        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
19862        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19863        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
19864        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
19865                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
19866                                                      reg,
19867                                                      tmpreg)));
19868        /* Emit lea manually to avoid clobbering of flags.  */
19869        emit_insn (gen_rtx_SET (SImode, reg2,
19870                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
19871
19872        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19873        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
19874        emit_insn (gen_rtx_SET (VOIDmode, out,
19875                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
19876                                                      reg2,
19877                                                      out)));
19878     }
19879   else
19880     {
19881        rtx end_2_label = gen_label_rtx ();
19882        /* Is zero in the first two bytes? */
19883
19884        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
19885        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19886        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
19887        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
19888                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
19889                             pc_rtx);
19890        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
19891        JUMP_LABEL (tmp) = end_2_label;
19892
19893        /* Not in the first two.  Move two bytes forward.  */
19894        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
19895        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
19896
19897        emit_label (end_2_label);
19898
19899     }
19900
19901   /* Avoid branch in fixing the byte.  */
19902   tmpreg = gen_lowpart (QImode, tmpreg);
19903   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
19904   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
19905   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
19906   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
19907
19908   emit_label (end_0_label);
19909 }
19910
19911 /* Expand strlen.  */
19912
19913 int
19914 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
19915 {
19916   rtx addr, scratch1, scratch2, scratch3, scratch4;
19917
19918   /* The generic case of strlen expander is long.  Avoid it's
19919      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
19920
19921   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
19922       && !TARGET_INLINE_ALL_STRINGOPS
19923       && !optimize_insn_for_size_p ()
19924       && (!CONST_INT_P (align) || INTVAL (align) < 4))
19925     return 0;
19926
19927   addr = force_reg (Pmode, XEXP (src, 0));
19928   scratch1 = gen_reg_rtx (Pmode);
19929
19930   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
19931       && !optimize_insn_for_size_p ())
19932     {
19933       /* Well it seems that some optimizer does not combine a call like
19934          foo(strlen(bar), strlen(bar));
19935          when the move and the subtraction is done here.  It does calculate
19936          the length just once when these instructions are done inside of
19937          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
19938          often used and I use one fewer register for the lifetime of
19939          output_strlen_unroll() this is better.  */
19940
19941       emit_move_insn (out, addr);
19942
19943       ix86_expand_strlensi_unroll_1 (out, src, align);
19944
19945       /* strlensi_unroll_1 returns the address of the zero at the end of
19946          the string, like memchr(), so compute the length by subtracting
19947          the start address.  */
19948       emit_insn (ix86_gen_sub3 (out, out, addr));
19949     }
19950   else
19951     {
19952       rtx unspec;
19953
19954       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
19955       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
19956         return false;
19957
19958       scratch2 = gen_reg_rtx (Pmode);
19959       scratch3 = gen_reg_rtx (Pmode);
19960       scratch4 = force_reg (Pmode, constm1_rtx);
19961
19962       emit_move_insn (scratch3, addr);
19963       eoschar = force_reg (QImode, eoschar);
19964
19965       src = replace_equiv_address_nv (src, scratch3);
19966
19967       /* If .md starts supporting :P, this can be done in .md.  */
19968       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
19969                                                  scratch4), UNSPEC_SCAS);
19970       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
19971       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
19972       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
19973     }
19974   return 1;
19975 }
19976
19977 /* For given symbol (function) construct code to compute address of it's PLT
19978    entry in large x86-64 PIC model.  */
19979 rtx
19980 construct_plt_address (rtx symbol)
19981 {
19982   rtx tmp = gen_reg_rtx (Pmode);
19983   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
19984
19985   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
19986   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
19987
19988   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
19989   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
19990   return tmp;
19991 }
19992
19993 void
19994 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
19995                   rtx callarg2,
19996                   rtx pop, int sibcall)
19997 {
19998   rtx use = NULL, call;
19999
20000   if (pop == const0_rtx)
20001     pop = NULL;
20002   gcc_assert (!TARGET_64BIT || !pop);
20003
20004   if (TARGET_MACHO && !TARGET_64BIT)
20005     {
20006 #if TARGET_MACHO
20007       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
20008         fnaddr = machopic_indirect_call_target (fnaddr);
20009 #endif
20010     }
20011   else
20012     {
20013       /* Static functions and indirect calls don't need the pic register.  */
20014       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
20015           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
20016           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
20017         use_reg (&use, pic_offset_table_rtx);
20018     }
20019
20020   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
20021     {
20022       rtx al = gen_rtx_REG (QImode, AX_REG);
20023       emit_move_insn (al, callarg2);
20024       use_reg (&use, al);
20025     }
20026
20027   if (ix86_cmodel == CM_LARGE_PIC
20028       && MEM_P (fnaddr)
20029       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
20030       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
20031     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
20032   else if (sibcall
20033            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
20034            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
20035     {
20036       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
20037       fnaddr = gen_rtx_MEM (QImode, fnaddr);
20038     }
20039
20040   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
20041   if (retval)
20042     call = gen_rtx_SET (VOIDmode, retval, call);
20043   if (pop)
20044     {
20045       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
20046       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
20047       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
20048     }
20049   if (TARGET_64BIT
20050       && ix86_cfun_abi () == MS_ABI
20051       && (!callarg2 || INTVAL (callarg2) != -2))
20052     {
20053       /* We need to represent that SI and DI registers are clobbered
20054          by SYSV calls.  */
20055       static int clobbered_registers[] = {
20056         XMM6_REG, XMM7_REG, XMM8_REG,
20057         XMM9_REG, XMM10_REG, XMM11_REG,
20058         XMM12_REG, XMM13_REG, XMM14_REG,
20059         XMM15_REG, SI_REG, DI_REG
20060       };
20061       unsigned int i;
20062       rtx vec[ARRAY_SIZE (clobbered_registers) + 2];
20063       rtx unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
20064                                    UNSPEC_MS_TO_SYSV_CALL);
20065
20066       vec[0] = call;
20067       vec[1] = unspec;
20068       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
20069         vec[i + 2] = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
20070                                       ? TImode : DImode,
20071                                       gen_rtx_REG
20072                                         (SSE_REGNO_P (clobbered_registers[i])
20073                                                       ? TImode : DImode,
20074                                          clobbered_registers[i]));
20075
20076       call = gen_rtx_PARALLEL (VOIDmode,
20077                                gen_rtvec_v (ARRAY_SIZE (clobbered_registers)
20078                                + 2, vec));
20079     }
20080
20081   call = emit_call_insn (call);
20082   if (use)
20083     CALL_INSN_FUNCTION_USAGE (call) = use;
20084 }
20085
20086 \f
20087 /* Clear stack slot assignments remembered from previous functions.
20088    This is called from INIT_EXPANDERS once before RTL is emitted for each
20089    function.  */
20090
20091 static struct machine_function *
20092 ix86_init_machine_status (void)
20093 {
20094   struct machine_function *f;
20095
20096   f = ggc_alloc_cleared_machine_function ();
20097   f->use_fast_prologue_epilogue_nregs = -1;
20098   f->tls_descriptor_call_expanded_p = 0;
20099   f->call_abi = ix86_abi;
20100
20101   return f;
20102 }
20103
20104 /* Return a MEM corresponding to a stack slot with mode MODE.
20105    Allocate a new slot if necessary.
20106
20107    The RTL for a function can have several slots available: N is
20108    which slot to use.  */
20109
20110 rtx
20111 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
20112 {
20113   struct stack_local_entry *s;
20114
20115   gcc_assert (n < MAX_386_STACK_LOCALS);
20116
20117   /* Virtual slot is valid only before vregs are instantiated.  */
20118   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
20119
20120   for (s = ix86_stack_locals; s; s = s->next)
20121     if (s->mode == mode && s->n == n)
20122       return copy_rtx (s->rtl);
20123
20124   s = ggc_alloc_stack_local_entry ();
20125   s->n = n;
20126   s->mode = mode;
20127   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
20128
20129   s->next = ix86_stack_locals;
20130   ix86_stack_locals = s;
20131   return s->rtl;
20132 }
20133
20134 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
20135
20136 static GTY(()) rtx ix86_tls_symbol;
20137 rtx
20138 ix86_tls_get_addr (void)
20139 {
20140
20141   if (!ix86_tls_symbol)
20142     {
20143       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
20144                                             (TARGET_ANY_GNU_TLS
20145                                              && !TARGET_64BIT)
20146                                             ? "___tls_get_addr"
20147                                             : "__tls_get_addr");
20148     }
20149
20150   return ix86_tls_symbol;
20151 }
20152
20153 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
20154
20155 static GTY(()) rtx ix86_tls_module_base_symbol;
20156 rtx
20157 ix86_tls_module_base (void)
20158 {
20159
20160   if (!ix86_tls_module_base_symbol)
20161     {
20162       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
20163                                                         "_TLS_MODULE_BASE_");
20164       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
20165         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
20166     }
20167
20168   return ix86_tls_module_base_symbol;
20169 }
20170 \f
20171 /* Calculate the length of the memory address in the instruction
20172    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
20173
20174 int
20175 memory_address_length (rtx addr)
20176 {
20177   struct ix86_address parts;
20178   rtx base, index, disp;
20179   int len;
20180   int ok;
20181
20182   if (GET_CODE (addr) == PRE_DEC
20183       || GET_CODE (addr) == POST_INC
20184       || GET_CODE (addr) == PRE_MODIFY
20185       || GET_CODE (addr) == POST_MODIFY)
20186     return 0;
20187
20188   ok = ix86_decompose_address (addr, &parts);
20189   gcc_assert (ok);
20190
20191   if (parts.base && GET_CODE (parts.base) == SUBREG)
20192     parts.base = SUBREG_REG (parts.base);
20193   if (parts.index && GET_CODE (parts.index) == SUBREG)
20194     parts.index = SUBREG_REG (parts.index);
20195
20196   base = parts.base;
20197   index = parts.index;
20198   disp = parts.disp;
20199   len = 0;
20200
20201   /* Rule of thumb:
20202        - esp as the base always wants an index,
20203        - ebp as the base always wants a displacement,
20204        - r12 as the base always wants an index,
20205        - r13 as the base always wants a displacement.  */
20206
20207   /* Register Indirect.  */
20208   if (base && !index && !disp)
20209     {
20210       /* esp (for its index) and ebp (for its displacement) need
20211          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
20212          code.  */
20213       if (REG_P (addr)
20214           && (addr == arg_pointer_rtx
20215               || addr == frame_pointer_rtx
20216               || REGNO (addr) == SP_REG
20217               || REGNO (addr) == BP_REG
20218               || REGNO (addr) == R12_REG
20219               || REGNO (addr) == R13_REG))
20220         len = 1;
20221     }
20222
20223   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
20224      is not disp32, but disp32(%rip), so for disp32
20225      SIB byte is needed, unless print_operand_address
20226      optimizes it into disp32(%rip) or (%rip) is implied
20227      by UNSPEC.  */
20228   else if (disp && !base && !index)
20229     {
20230       len = 4;
20231       if (TARGET_64BIT)
20232         {
20233           rtx symbol = disp;
20234
20235           if (GET_CODE (disp) == CONST)
20236             symbol = XEXP (disp, 0);
20237           if (GET_CODE (symbol) == PLUS
20238               && CONST_INT_P (XEXP (symbol, 1)))
20239             symbol = XEXP (symbol, 0);
20240
20241           if (GET_CODE (symbol) != LABEL_REF
20242               && (GET_CODE (symbol) != SYMBOL_REF
20243                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
20244               && (GET_CODE (symbol) != UNSPEC
20245                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
20246                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
20247             len += 1;
20248         }
20249     }
20250
20251   else
20252     {
20253       /* Find the length of the displacement constant.  */
20254       if (disp)
20255         {
20256           if (base && satisfies_constraint_K (disp))
20257             len = 1;
20258           else
20259             len = 4;
20260         }
20261       /* ebp always wants a displacement.  Similarly r13.  */
20262       else if (base && REG_P (base)
20263                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
20264         len = 1;
20265
20266       /* An index requires the two-byte modrm form....  */
20267       if (index
20268           /* ...like esp (or r12), which always wants an index.  */
20269           || base == arg_pointer_rtx
20270           || base == frame_pointer_rtx
20271           || (base && REG_P (base)
20272               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
20273         len += 1;
20274     }
20275
20276   switch (parts.seg)
20277     {
20278     case SEG_FS:
20279     case SEG_GS:
20280       len += 1;
20281       break;
20282     default:
20283       break;
20284     }
20285
20286   return len;
20287 }
20288
20289 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
20290    is set, expect that insn have 8bit immediate alternative.  */
20291 int
20292 ix86_attr_length_immediate_default (rtx insn, int shortform)
20293 {
20294   int len = 0;
20295   int i;
20296   extract_insn_cached (insn);
20297   for (i = recog_data.n_operands - 1; i >= 0; --i)
20298     if (CONSTANT_P (recog_data.operand[i]))
20299       {
20300         enum attr_mode mode = get_attr_mode (insn);
20301
20302         gcc_assert (!len);
20303         if (shortform && CONST_INT_P (recog_data.operand[i]))
20304           {
20305             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
20306             switch (mode)
20307               {
20308               case MODE_QI:
20309                 len = 1;
20310                 continue;
20311               case MODE_HI:
20312                 ival = trunc_int_for_mode (ival, HImode);
20313                 break;
20314               case MODE_SI:
20315                 ival = trunc_int_for_mode (ival, SImode);
20316                 break;
20317               default:
20318                 break;
20319               }
20320             if (IN_RANGE (ival, -128, 127))
20321               {
20322                 len = 1;
20323                 continue;
20324               }
20325           }
20326         switch (mode)
20327           {
20328           case MODE_QI:
20329             len = 1;
20330             break;
20331           case MODE_HI:
20332             len = 2;
20333             break;
20334           case MODE_SI:
20335             len = 4;
20336             break;
20337           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
20338           case MODE_DI:
20339             len = 4;
20340             break;
20341           default:
20342             fatal_insn ("unknown insn mode", insn);
20343         }
20344       }
20345   return len;
20346 }
20347 /* Compute default value for "length_address" attribute.  */
20348 int
20349 ix86_attr_length_address_default (rtx insn)
20350 {
20351   int i;
20352
20353   if (get_attr_type (insn) == TYPE_LEA)
20354     {
20355       rtx set = PATTERN (insn), addr;
20356
20357       if (GET_CODE (set) == PARALLEL)
20358         set = XVECEXP (set, 0, 0);
20359
20360       gcc_assert (GET_CODE (set) == SET);
20361
20362       addr = SET_SRC (set);
20363       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
20364         {
20365           if (GET_CODE (addr) == ZERO_EXTEND)
20366             addr = XEXP (addr, 0);
20367           if (GET_CODE (addr) == SUBREG)
20368             addr = SUBREG_REG (addr);
20369         }
20370
20371       return memory_address_length (addr);
20372     }
20373
20374   extract_insn_cached (insn);
20375   for (i = recog_data.n_operands - 1; i >= 0; --i)
20376     if (MEM_P (recog_data.operand[i]))
20377       {
20378         constrain_operands_cached (reload_completed);
20379         if (which_alternative != -1)
20380           {
20381             const char *constraints = recog_data.constraints[i];
20382             int alt = which_alternative;
20383
20384             while (*constraints == '=' || *constraints == '+')
20385               constraints++;
20386             while (alt-- > 0)
20387               while (*constraints++ != ',')
20388                 ;
20389             /* Skip ignored operands.  */
20390             if (*constraints == 'X')
20391               continue;
20392           }
20393         return memory_address_length (XEXP (recog_data.operand[i], 0));
20394       }
20395   return 0;
20396 }
20397
20398 /* Compute default value for "length_vex" attribute. It includes
20399    2 or 3 byte VEX prefix and 1 opcode byte.  */
20400
20401 int
20402 ix86_attr_length_vex_default (rtx insn, int has_0f_opcode,
20403                               int has_vex_w)
20404 {
20405   int i;
20406
20407   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
20408      byte VEX prefix.  */
20409   if (!has_0f_opcode || has_vex_w)
20410     return 3 + 1;
20411
20412  /* We can always use 2 byte VEX prefix in 32bit.  */
20413   if (!TARGET_64BIT)
20414     return 2 + 1;
20415
20416   extract_insn_cached (insn);
20417
20418   for (i = recog_data.n_operands - 1; i >= 0; --i)
20419     if (REG_P (recog_data.operand[i]))
20420       {
20421         /* REX.W bit uses 3 byte VEX prefix.  */
20422         if (GET_MODE (recog_data.operand[i]) == DImode
20423             && GENERAL_REG_P (recog_data.operand[i]))
20424           return 3 + 1;
20425       }
20426     else
20427       {
20428         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
20429         if (MEM_P (recog_data.operand[i])
20430             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
20431           return 3 + 1;
20432       }
20433
20434   return 2 + 1;
20435 }
20436 \f
20437 /* Return the maximum number of instructions a cpu can issue.  */
20438
20439 static int
20440 ix86_issue_rate (void)
20441 {
20442   switch (ix86_tune)
20443     {
20444     case PROCESSOR_PENTIUM:
20445     case PROCESSOR_ATOM:
20446     case PROCESSOR_K6:
20447       return 2;
20448
20449     case PROCESSOR_PENTIUMPRO:
20450     case PROCESSOR_PENTIUM4:
20451     case PROCESSOR_ATHLON:
20452     case PROCESSOR_K8:
20453     case PROCESSOR_AMDFAM10:
20454     case PROCESSOR_NOCONA:
20455     case PROCESSOR_GENERIC32:
20456     case PROCESSOR_GENERIC64:
20457     case PROCESSOR_BDVER1:
20458       return 3;
20459
20460     case PROCESSOR_CORE2:
20461       return 4;
20462
20463     default:
20464       return 1;
20465     }
20466 }
20467
20468 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
20469    by DEP_INSN and nothing set by DEP_INSN.  */
20470
20471 static int
20472 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
20473 {
20474   rtx set, set2;
20475
20476   /* Simplify the test for uninteresting insns.  */
20477   if (insn_type != TYPE_SETCC
20478       && insn_type != TYPE_ICMOV
20479       && insn_type != TYPE_FCMOV
20480       && insn_type != TYPE_IBR)
20481     return 0;
20482
20483   if ((set = single_set (dep_insn)) != 0)
20484     {
20485       set = SET_DEST (set);
20486       set2 = NULL_RTX;
20487     }
20488   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
20489            && XVECLEN (PATTERN (dep_insn), 0) == 2
20490            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
20491            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
20492     {
20493       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
20494       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
20495     }
20496   else
20497     return 0;
20498
20499   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
20500     return 0;
20501
20502   /* This test is true if the dependent insn reads the flags but
20503      not any other potentially set register.  */
20504   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
20505     return 0;
20506
20507   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
20508     return 0;
20509
20510   return 1;
20511 }
20512
20513 /* Return true iff USE_INSN has a memory address with operands set by
20514    SET_INSN.  */
20515
20516 bool
20517 ix86_agi_dependent (rtx set_insn, rtx use_insn)
20518 {
20519   int i;
20520   extract_insn_cached (use_insn);
20521   for (i = recog_data.n_operands - 1; i >= 0; --i)
20522     if (MEM_P (recog_data.operand[i]))
20523       {
20524         rtx addr = XEXP (recog_data.operand[i], 0);
20525         return modified_in_p (addr, set_insn) != 0;
20526       }
20527   return false;
20528 }
20529
20530 static int
20531 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
20532 {
20533   enum attr_type insn_type, dep_insn_type;
20534   enum attr_memory memory;
20535   rtx set, set2;
20536   int dep_insn_code_number;
20537
20538   /* Anti and output dependencies have zero cost on all CPUs.  */
20539   if (REG_NOTE_KIND (link) != 0)
20540     return 0;
20541
20542   dep_insn_code_number = recog_memoized (dep_insn);
20543
20544   /* If we can't recognize the insns, we can't really do anything.  */
20545   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
20546     return cost;
20547
20548   insn_type = get_attr_type (insn);
20549   dep_insn_type = get_attr_type (dep_insn);
20550
20551   switch (ix86_tune)
20552     {
20553     case PROCESSOR_PENTIUM:
20554       /* Address Generation Interlock adds a cycle of latency.  */
20555       if (insn_type == TYPE_LEA)
20556         {
20557           rtx addr = PATTERN (insn);
20558
20559           if (GET_CODE (addr) == PARALLEL)
20560             addr = XVECEXP (addr, 0, 0);
20561
20562           gcc_assert (GET_CODE (addr) == SET);
20563
20564           addr = SET_SRC (addr);
20565           if (modified_in_p (addr, dep_insn))
20566             cost += 1;
20567         }
20568       else if (ix86_agi_dependent (dep_insn, insn))
20569         cost += 1;
20570
20571       /* ??? Compares pair with jump/setcc.  */
20572       if (ix86_flags_dependent (insn, dep_insn, insn_type))
20573         cost = 0;
20574
20575       /* Floating point stores require value to be ready one cycle earlier.  */
20576       if (insn_type == TYPE_FMOV
20577           && get_attr_memory (insn) == MEMORY_STORE
20578           && !ix86_agi_dependent (dep_insn, insn))
20579         cost += 1;
20580       break;
20581
20582     case PROCESSOR_PENTIUMPRO:
20583       memory = get_attr_memory (insn);
20584
20585       /* INT->FP conversion is expensive.  */
20586       if (get_attr_fp_int_src (dep_insn))
20587         cost += 5;
20588
20589       /* There is one cycle extra latency between an FP op and a store.  */
20590       if (insn_type == TYPE_FMOV
20591           && (set = single_set (dep_insn)) != NULL_RTX
20592           && (set2 = single_set (insn)) != NULL_RTX
20593           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
20594           && MEM_P (SET_DEST (set2)))
20595         cost += 1;
20596
20597       /* Show ability of reorder buffer to hide latency of load by executing
20598          in parallel with previous instruction in case
20599          previous instruction is not needed to compute the address.  */
20600       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
20601           && !ix86_agi_dependent (dep_insn, insn))
20602         {
20603           /* Claim moves to take one cycle, as core can issue one load
20604              at time and the next load can start cycle later.  */
20605           if (dep_insn_type == TYPE_IMOV
20606               || dep_insn_type == TYPE_FMOV)
20607             cost = 1;
20608           else if (cost > 1)
20609             cost--;
20610         }
20611       break;
20612
20613     case PROCESSOR_K6:
20614       memory = get_attr_memory (insn);
20615
20616       /* The esp dependency is resolved before the instruction is really
20617          finished.  */
20618       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
20619           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
20620         return 1;
20621
20622       /* INT->FP conversion is expensive.  */
20623       if (get_attr_fp_int_src (dep_insn))
20624         cost += 5;
20625
20626       /* Show ability of reorder buffer to hide latency of load by executing
20627          in parallel with previous instruction in case
20628          previous instruction is not needed to compute the address.  */
20629       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
20630           && !ix86_agi_dependent (dep_insn, insn))
20631         {
20632           /* Claim moves to take one cycle, as core can issue one load
20633              at time and the next load can start cycle later.  */
20634           if (dep_insn_type == TYPE_IMOV
20635               || dep_insn_type == TYPE_FMOV)
20636             cost = 1;
20637           else if (cost > 2)
20638             cost -= 2;
20639           else
20640             cost = 1;
20641         }
20642       break;
20643
20644     case PROCESSOR_ATHLON:
20645     case PROCESSOR_K8:
20646     case PROCESSOR_AMDFAM10:
20647     case PROCESSOR_BDVER1:
20648     case PROCESSOR_ATOM:
20649     case PROCESSOR_GENERIC32:
20650     case PROCESSOR_GENERIC64:
20651       memory = get_attr_memory (insn);
20652
20653       /* Show ability of reorder buffer to hide latency of load by executing
20654          in parallel with previous instruction in case
20655          previous instruction is not needed to compute the address.  */
20656       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
20657           && !ix86_agi_dependent (dep_insn, insn))
20658         {
20659           enum attr_unit unit = get_attr_unit (insn);
20660           int loadcost = 3;
20661
20662           /* Because of the difference between the length of integer and
20663              floating unit pipeline preparation stages, the memory operands
20664              for floating point are cheaper.
20665
20666              ??? For Athlon it the difference is most probably 2.  */
20667           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
20668             loadcost = 3;
20669           else
20670             loadcost = TARGET_ATHLON ? 2 : 0;
20671
20672           if (cost >= loadcost)
20673             cost -= loadcost;
20674           else
20675             cost = 0;
20676         }
20677
20678     default:
20679       break;
20680     }
20681
20682   return cost;
20683 }
20684
20685 /* How many alternative schedules to try.  This should be as wide as the
20686    scheduling freedom in the DFA, but no wider.  Making this value too
20687    large results extra work for the scheduler.  */
20688
20689 static int
20690 ia32_multipass_dfa_lookahead (void)
20691 {
20692   switch (ix86_tune)
20693     {
20694     case PROCESSOR_PENTIUM:
20695       return 2;
20696
20697     case PROCESSOR_PENTIUMPRO:
20698     case PROCESSOR_K6:
20699       return 1;
20700
20701     default:
20702       return 0;
20703     }
20704 }
20705
20706 \f
20707 /* Compute the alignment given to a constant that is being placed in memory.
20708    EXP is the constant and ALIGN is the alignment that the object would
20709    ordinarily have.
20710    The value of this function is used instead of that alignment to align
20711    the object.  */
20712
20713 int
20714 ix86_constant_alignment (tree exp, int align)
20715 {
20716   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
20717       || TREE_CODE (exp) == INTEGER_CST)
20718     {
20719       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
20720         return 64;
20721       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
20722         return 128;
20723     }
20724   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
20725            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
20726     return BITS_PER_WORD;
20727
20728   return align;
20729 }
20730
20731 /* Compute the alignment for a static variable.
20732    TYPE is the data type, and ALIGN is the alignment that
20733    the object would ordinarily have.  The value of this function is used
20734    instead of that alignment to align the object.  */
20735
20736 int
20737 ix86_data_alignment (tree type, int align)
20738 {
20739   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
20740
20741   if (AGGREGATE_TYPE_P (type)
20742       && TYPE_SIZE (type)
20743       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
20744       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
20745           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
20746       && align < max_align)
20747     align = max_align;
20748
20749   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
20750      to 16byte boundary.  */
20751   if (TARGET_64BIT)
20752     {
20753       if (AGGREGATE_TYPE_P (type)
20754            && TYPE_SIZE (type)
20755            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
20756            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
20757                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
20758         return 128;
20759     }
20760
20761   if (TREE_CODE (type) == ARRAY_TYPE)
20762     {
20763       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
20764         return 64;
20765       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
20766         return 128;
20767     }
20768   else if (TREE_CODE (type) == COMPLEX_TYPE)
20769     {
20770
20771       if (TYPE_MODE (type) == DCmode && align < 64)
20772         return 64;
20773       if ((TYPE_MODE (type) == XCmode
20774            || TYPE_MODE (type) == TCmode) && align < 128)
20775         return 128;
20776     }
20777   else if ((TREE_CODE (type) == RECORD_TYPE
20778             || TREE_CODE (type) == UNION_TYPE
20779             || TREE_CODE (type) == QUAL_UNION_TYPE)
20780            && TYPE_FIELDS (type))
20781     {
20782       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
20783         return 64;
20784       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
20785         return 128;
20786     }
20787   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
20788            || TREE_CODE (type) == INTEGER_TYPE)
20789     {
20790       if (TYPE_MODE (type) == DFmode && align < 64)
20791         return 64;
20792       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
20793         return 128;
20794     }
20795
20796   return align;
20797 }
20798
20799 /* Compute the alignment for a local variable or a stack slot.  EXP is
20800    the data type or decl itself, MODE is the widest mode available and
20801    ALIGN is the alignment that the object would ordinarily have.  The
20802    value of this macro is used instead of that alignment to align the
20803    object.  */
20804
20805 unsigned int
20806 ix86_local_alignment (tree exp, enum machine_mode mode,
20807                       unsigned int align)
20808 {
20809   tree type, decl;
20810
20811   if (exp && DECL_P (exp))
20812     {
20813       type = TREE_TYPE (exp);
20814       decl = exp;
20815     }
20816   else
20817     {
20818       type = exp;
20819       decl = NULL;
20820     }
20821
20822   /* Don't do dynamic stack realignment for long long objects with
20823      -mpreferred-stack-boundary=2.  */
20824   if (!TARGET_64BIT
20825       && align == 64
20826       && ix86_preferred_stack_boundary < 64
20827       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
20828       && (!type || !TYPE_USER_ALIGN (type))
20829       && (!decl || !DECL_USER_ALIGN (decl)))
20830     align = 32;
20831
20832   /* If TYPE is NULL, we are allocating a stack slot for caller-save
20833      register in MODE.  We will return the largest alignment of XF
20834      and DF.  */
20835   if (!type)
20836     {
20837       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
20838         align = GET_MODE_ALIGNMENT (DFmode);
20839       return align;
20840     }
20841
20842   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
20843      to 16byte boundary.  Exact wording is:
20844
20845      An array uses the same alignment as its elements, except that a local or
20846      global array variable of length at least 16 bytes or
20847      a C99 variable-length array variable always has alignment of at least 16 bytes.
20848
20849      This was added to allow use of aligned SSE instructions at arrays.  This
20850      rule is meant for static storage (where compiler can not do the analysis
20851      by itself).  We follow it for automatic variables only when convenient.
20852      We fully control everything in the function compiled and functions from
20853      other unit can not rely on the alignment.
20854
20855      Exclude va_list type.  It is the common case of local array where
20856      we can not benefit from the alignment.  */
20857   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
20858       && TARGET_SSE)
20859     {
20860       if (AGGREGATE_TYPE_P (type)
20861            && (TYPE_MAIN_VARIANT (type)
20862                != TYPE_MAIN_VARIANT (va_list_type_node))
20863            && TYPE_SIZE (type)
20864            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
20865            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
20866                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
20867         return 128;
20868     }
20869   if (TREE_CODE (type) == ARRAY_TYPE)
20870     {
20871       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
20872         return 64;
20873       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
20874         return 128;
20875     }
20876   else if (TREE_CODE (type) == COMPLEX_TYPE)
20877     {
20878       if (TYPE_MODE (type) == DCmode && align < 64)
20879         return 64;
20880       if ((TYPE_MODE (type) == XCmode
20881            || TYPE_MODE (type) == TCmode) && align < 128)
20882         return 128;
20883     }
20884   else if ((TREE_CODE (type) == RECORD_TYPE
20885             || TREE_CODE (type) == UNION_TYPE
20886             || TREE_CODE (type) == QUAL_UNION_TYPE)
20887            && TYPE_FIELDS (type))
20888     {
20889       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
20890         return 64;
20891       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
20892         return 128;
20893     }
20894   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
20895            || TREE_CODE (type) == INTEGER_TYPE)
20896     {
20897
20898       if (TYPE_MODE (type) == DFmode && align < 64)
20899         return 64;
20900       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
20901         return 128;
20902     }
20903   return align;
20904 }
20905
20906 /* Compute the minimum required alignment for dynamic stack realignment
20907    purposes for a local variable, parameter or a stack slot.  EXP is
20908    the data type or decl itself, MODE is its mode and ALIGN is the
20909    alignment that the object would ordinarily have.  */
20910
20911 unsigned int
20912 ix86_minimum_alignment (tree exp, enum machine_mode mode,
20913                         unsigned int align)
20914 {
20915   tree type, decl;
20916
20917   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
20918     return align;
20919
20920   if (exp && DECL_P (exp))
20921     {
20922       type = TREE_TYPE (exp);
20923       decl = exp;
20924     }
20925   else
20926     {
20927       type = exp;
20928       decl = NULL;
20929     }
20930
20931   /* Don't do dynamic stack realignment for long long objects with
20932      -mpreferred-stack-boundary=2.  */
20933   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
20934       && (!type || !TYPE_USER_ALIGN (type))
20935       && (!decl || !DECL_USER_ALIGN (decl)))
20936     return 32;
20937
20938   return align;
20939 }
20940 \f
20941 /* Find a location for the static chain incoming to a nested function.
20942    This is a register, unless all free registers are used by arguments.  */
20943
20944 static rtx
20945 ix86_static_chain (const_tree fndecl, bool incoming_p)
20946 {
20947   unsigned regno;
20948
20949   if (!DECL_STATIC_CHAIN (fndecl))
20950     return NULL;
20951
20952   if (TARGET_64BIT)
20953     {
20954       /* We always use R10 in 64-bit mode.  */
20955       regno = R10_REG;
20956     }
20957   else
20958     {
20959       tree fntype;
20960       /* By default in 32-bit mode we use ECX to pass the static chain.  */
20961       regno = CX_REG;
20962
20963       fntype = TREE_TYPE (fndecl);
20964       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
20965         {
20966           /* Fastcall functions use ecx/edx for arguments, which leaves
20967              us with EAX for the static chain.  */
20968           regno = AX_REG;
20969         }
20970       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
20971         {
20972           /* Thiscall functions use ecx for arguments, which leaves
20973              us with EAX for the static chain.  */
20974           regno = AX_REG;
20975         }
20976       else if (ix86_function_regparm (fntype, fndecl) == 3)
20977         {
20978           /* For regparm 3, we have no free call-clobbered registers in
20979              which to store the static chain.  In order to implement this,
20980              we have the trampoline push the static chain to the stack.
20981              However, we can't push a value below the return address when
20982              we call the nested function directly, so we have to use an
20983              alternate entry point.  For this we use ESI, and have the
20984              alternate entry point push ESI, so that things appear the
20985              same once we're executing the nested function.  */
20986           if (incoming_p)
20987             {
20988               if (fndecl == current_function_decl)
20989                 ix86_static_chain_on_stack = true;
20990               return gen_frame_mem (SImode,
20991                                     plus_constant (arg_pointer_rtx, -8));
20992             }
20993           regno = SI_REG;
20994         }
20995     }
20996
20997   return gen_rtx_REG (Pmode, regno);
20998 }
20999
21000 /* Emit RTL insns to initialize the variable parts of a trampoline.
21001    FNDECL is the decl of the target address; M_TRAMP is a MEM for
21002    the trampoline, and CHAIN_VALUE is an RTX for the static chain
21003    to be passed to the target function.  */
21004
21005 static void
21006 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
21007 {
21008   rtx mem, fnaddr;
21009
21010   fnaddr = XEXP (DECL_RTL (fndecl), 0);
21011
21012   if (!TARGET_64BIT)
21013     {
21014       rtx disp, chain;
21015       int opcode;
21016
21017       /* Depending on the static chain location, either load a register
21018          with a constant, or push the constant to the stack.  All of the
21019          instructions are the same size.  */
21020       chain = ix86_static_chain (fndecl, true);
21021       if (REG_P (chain))
21022         {
21023           if (REGNO (chain) == CX_REG)
21024             opcode = 0xb9;
21025           else if (REGNO (chain) == AX_REG)
21026             opcode = 0xb8;
21027           else
21028             gcc_unreachable ();
21029         }
21030       else
21031         opcode = 0x68;
21032
21033       mem = adjust_address (m_tramp, QImode, 0);
21034       emit_move_insn (mem, gen_int_mode (opcode, QImode));
21035
21036       mem = adjust_address (m_tramp, SImode, 1);
21037       emit_move_insn (mem, chain_value);
21038
21039       /* Compute offset from the end of the jmp to the target function.
21040          In the case in which the trampoline stores the static chain on
21041          the stack, we need to skip the first insn which pushes the
21042          (call-saved) register static chain; this push is 1 byte.  */
21043       disp = expand_binop (SImode, sub_optab, fnaddr,
21044                            plus_constant (XEXP (m_tramp, 0),
21045                                           MEM_P (chain) ? 9 : 10),
21046                            NULL_RTX, 1, OPTAB_DIRECT);
21047
21048       mem = adjust_address (m_tramp, QImode, 5);
21049       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
21050
21051       mem = adjust_address (m_tramp, SImode, 6);
21052       emit_move_insn (mem, disp);
21053     }
21054   else
21055     {
21056       int offset = 0;
21057
21058       /* Load the function address to r11.  Try to load address using
21059          the shorter movl instead of movabs.  We may want to support
21060          movq for kernel mode, but kernel does not use trampolines at
21061          the moment.  */
21062       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
21063         {
21064           fnaddr = copy_to_mode_reg (DImode, fnaddr);
21065
21066           mem = adjust_address (m_tramp, HImode, offset);
21067           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
21068
21069           mem = adjust_address (m_tramp, SImode, offset + 2);
21070           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
21071           offset += 6;
21072         }
21073       else
21074         {
21075           mem = adjust_address (m_tramp, HImode, offset);
21076           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
21077
21078           mem = adjust_address (m_tramp, DImode, offset + 2);
21079           emit_move_insn (mem, fnaddr);
21080           offset += 10;
21081         }
21082
21083       /* Load static chain using movabs to r10.  */
21084       mem = adjust_address (m_tramp, HImode, offset);
21085       emit_move_insn (mem, gen_int_mode (0xba49, HImode));
21086
21087       mem = adjust_address (m_tramp, DImode, offset + 2);
21088       emit_move_insn (mem, chain_value);
21089       offset += 10;
21090
21091       /* Jump to r11; the last (unused) byte is a nop, only there to
21092          pad the write out to a single 32-bit store.  */
21093       mem = adjust_address (m_tramp, SImode, offset);
21094       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
21095       offset += 4;
21096
21097       gcc_assert (offset <= TRAMPOLINE_SIZE);
21098     }
21099
21100 #ifdef ENABLE_EXECUTE_STACK
21101 #ifdef CHECK_EXECUTE_STACK_ENABLED
21102   if (CHECK_EXECUTE_STACK_ENABLED)
21103 #endif
21104   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
21105                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
21106 #endif
21107 }
21108 \f
21109 /* The following file contains several enumerations and data structures
21110    built from the definitions in i386-builtin-types.def.  */
21111
21112 #include "i386-builtin-types.inc"
21113
21114 /* Table for the ix86 builtin non-function types.  */
21115 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
21116
21117 /* Retrieve an element from the above table, building some of
21118    the types lazily.  */
21119
21120 static tree
21121 ix86_get_builtin_type (enum ix86_builtin_type tcode)
21122 {
21123   unsigned int index;
21124   tree type, itype;
21125
21126   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
21127
21128   type = ix86_builtin_type_tab[(int) tcode];
21129   if (type != NULL)
21130     return type;
21131
21132   gcc_assert (tcode > IX86_BT_LAST_PRIM);
21133   if (tcode <= IX86_BT_LAST_VECT)
21134     {
21135       enum machine_mode mode;
21136
21137       index = tcode - IX86_BT_LAST_PRIM - 1;
21138       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
21139       mode = ix86_builtin_type_vect_mode[index];
21140
21141       type = build_vector_type_for_mode (itype, mode);
21142     }
21143   else
21144     {
21145       int quals;
21146
21147       index = tcode - IX86_BT_LAST_VECT - 1;
21148       if (tcode <= IX86_BT_LAST_PTR)
21149         quals = TYPE_UNQUALIFIED;
21150       else
21151         quals = TYPE_QUAL_CONST;
21152
21153       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
21154       if (quals != TYPE_UNQUALIFIED)
21155         itype = build_qualified_type (itype, quals);
21156
21157       type = build_pointer_type (itype);
21158     }
21159
21160   ix86_builtin_type_tab[(int) tcode] = type;
21161   return type;
21162 }
21163
21164 /* Table for the ix86 builtin function types.  */
21165 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
21166
21167 /* Retrieve an element from the above table, building some of
21168    the types lazily.  */
21169
21170 static tree
21171 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
21172 {
21173   tree type;
21174
21175   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
21176
21177   type = ix86_builtin_func_type_tab[(int) tcode];
21178   if (type != NULL)
21179     return type;
21180
21181   if (tcode <= IX86_BT_LAST_FUNC)
21182     {
21183       unsigned start = ix86_builtin_func_start[(int) tcode];
21184       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
21185       tree rtype, atype, args = void_list_node;
21186       unsigned i;
21187
21188       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
21189       for (i = after - 1; i > start; --i)
21190         {
21191           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
21192           args = tree_cons (NULL, atype, args);
21193         }
21194
21195       type = build_function_type (rtype, args);
21196     }
21197   else
21198     {
21199       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
21200       enum ix86_builtin_func_type icode;
21201
21202       icode = ix86_builtin_func_alias_base[index];
21203       type = ix86_get_builtin_func_type (icode);
21204     }
21205
21206   ix86_builtin_func_type_tab[(int) tcode] = type;
21207   return type;
21208 }
21209
21210
21211 /* Codes for all the SSE/MMX builtins.  */
21212 enum ix86_builtins
21213 {
21214   IX86_BUILTIN_ADDPS,
21215   IX86_BUILTIN_ADDSS,
21216   IX86_BUILTIN_DIVPS,
21217   IX86_BUILTIN_DIVSS,
21218   IX86_BUILTIN_MULPS,
21219   IX86_BUILTIN_MULSS,
21220   IX86_BUILTIN_SUBPS,
21221   IX86_BUILTIN_SUBSS,
21222
21223   IX86_BUILTIN_CMPEQPS,
21224   IX86_BUILTIN_CMPLTPS,
21225   IX86_BUILTIN_CMPLEPS,
21226   IX86_BUILTIN_CMPGTPS,
21227   IX86_BUILTIN_CMPGEPS,
21228   IX86_BUILTIN_CMPNEQPS,
21229   IX86_BUILTIN_CMPNLTPS,
21230   IX86_BUILTIN_CMPNLEPS,
21231   IX86_BUILTIN_CMPNGTPS,
21232   IX86_BUILTIN_CMPNGEPS,
21233   IX86_BUILTIN_CMPORDPS,
21234   IX86_BUILTIN_CMPUNORDPS,
21235   IX86_BUILTIN_CMPEQSS,
21236   IX86_BUILTIN_CMPLTSS,
21237   IX86_BUILTIN_CMPLESS,
21238   IX86_BUILTIN_CMPNEQSS,
21239   IX86_BUILTIN_CMPNLTSS,
21240   IX86_BUILTIN_CMPNLESS,
21241   IX86_BUILTIN_CMPNGTSS,
21242   IX86_BUILTIN_CMPNGESS,
21243   IX86_BUILTIN_CMPORDSS,
21244   IX86_BUILTIN_CMPUNORDSS,
21245
21246   IX86_BUILTIN_COMIEQSS,
21247   IX86_BUILTIN_COMILTSS,
21248   IX86_BUILTIN_COMILESS,
21249   IX86_BUILTIN_COMIGTSS,
21250   IX86_BUILTIN_COMIGESS,
21251   IX86_BUILTIN_COMINEQSS,
21252   IX86_BUILTIN_UCOMIEQSS,
21253   IX86_BUILTIN_UCOMILTSS,
21254   IX86_BUILTIN_UCOMILESS,
21255   IX86_BUILTIN_UCOMIGTSS,
21256   IX86_BUILTIN_UCOMIGESS,
21257   IX86_BUILTIN_UCOMINEQSS,
21258
21259   IX86_BUILTIN_CVTPI2PS,
21260   IX86_BUILTIN_CVTPS2PI,
21261   IX86_BUILTIN_CVTSI2SS,
21262   IX86_BUILTIN_CVTSI642SS,
21263   IX86_BUILTIN_CVTSS2SI,
21264   IX86_BUILTIN_CVTSS2SI64,
21265   IX86_BUILTIN_CVTTPS2PI,
21266   IX86_BUILTIN_CVTTSS2SI,
21267   IX86_BUILTIN_CVTTSS2SI64,
21268
21269   IX86_BUILTIN_MAXPS,
21270   IX86_BUILTIN_MAXSS,
21271   IX86_BUILTIN_MINPS,
21272   IX86_BUILTIN_MINSS,
21273
21274   IX86_BUILTIN_LOADUPS,
21275   IX86_BUILTIN_STOREUPS,
21276   IX86_BUILTIN_MOVSS,
21277
21278   IX86_BUILTIN_MOVHLPS,
21279   IX86_BUILTIN_MOVLHPS,
21280   IX86_BUILTIN_LOADHPS,
21281   IX86_BUILTIN_LOADLPS,
21282   IX86_BUILTIN_STOREHPS,
21283   IX86_BUILTIN_STORELPS,
21284
21285   IX86_BUILTIN_MASKMOVQ,
21286   IX86_BUILTIN_MOVMSKPS,
21287   IX86_BUILTIN_PMOVMSKB,
21288
21289   IX86_BUILTIN_MOVNTPS,
21290   IX86_BUILTIN_MOVNTQ,
21291
21292   IX86_BUILTIN_LOADDQU,
21293   IX86_BUILTIN_STOREDQU,
21294
21295   IX86_BUILTIN_PACKSSWB,
21296   IX86_BUILTIN_PACKSSDW,
21297   IX86_BUILTIN_PACKUSWB,
21298
21299   IX86_BUILTIN_PADDB,
21300   IX86_BUILTIN_PADDW,
21301   IX86_BUILTIN_PADDD,
21302   IX86_BUILTIN_PADDQ,
21303   IX86_BUILTIN_PADDSB,
21304   IX86_BUILTIN_PADDSW,
21305   IX86_BUILTIN_PADDUSB,
21306   IX86_BUILTIN_PADDUSW,
21307   IX86_BUILTIN_PSUBB,
21308   IX86_BUILTIN_PSUBW,
21309   IX86_BUILTIN_PSUBD,
21310   IX86_BUILTIN_PSUBQ,
21311   IX86_BUILTIN_PSUBSB,
21312   IX86_BUILTIN_PSUBSW,
21313   IX86_BUILTIN_PSUBUSB,
21314   IX86_BUILTIN_PSUBUSW,
21315
21316   IX86_BUILTIN_PAND,
21317   IX86_BUILTIN_PANDN,
21318   IX86_BUILTIN_POR,
21319   IX86_BUILTIN_PXOR,
21320
21321   IX86_BUILTIN_PAVGB,
21322   IX86_BUILTIN_PAVGW,
21323
21324   IX86_BUILTIN_PCMPEQB,
21325   IX86_BUILTIN_PCMPEQW,
21326   IX86_BUILTIN_PCMPEQD,
21327   IX86_BUILTIN_PCMPGTB,
21328   IX86_BUILTIN_PCMPGTW,
21329   IX86_BUILTIN_PCMPGTD,
21330
21331   IX86_BUILTIN_PMADDWD,
21332
21333   IX86_BUILTIN_PMAXSW,
21334   IX86_BUILTIN_PMAXUB,
21335   IX86_BUILTIN_PMINSW,
21336   IX86_BUILTIN_PMINUB,
21337
21338   IX86_BUILTIN_PMULHUW,
21339   IX86_BUILTIN_PMULHW,
21340   IX86_BUILTIN_PMULLW,
21341
21342   IX86_BUILTIN_PSADBW,
21343   IX86_BUILTIN_PSHUFW,
21344
21345   IX86_BUILTIN_PSLLW,
21346   IX86_BUILTIN_PSLLD,
21347   IX86_BUILTIN_PSLLQ,
21348   IX86_BUILTIN_PSRAW,
21349   IX86_BUILTIN_PSRAD,
21350   IX86_BUILTIN_PSRLW,
21351   IX86_BUILTIN_PSRLD,
21352   IX86_BUILTIN_PSRLQ,
21353   IX86_BUILTIN_PSLLWI,
21354   IX86_BUILTIN_PSLLDI,
21355   IX86_BUILTIN_PSLLQI,
21356   IX86_BUILTIN_PSRAWI,
21357   IX86_BUILTIN_PSRADI,
21358   IX86_BUILTIN_PSRLWI,
21359   IX86_BUILTIN_PSRLDI,
21360   IX86_BUILTIN_PSRLQI,
21361
21362   IX86_BUILTIN_PUNPCKHBW,
21363   IX86_BUILTIN_PUNPCKHWD,
21364   IX86_BUILTIN_PUNPCKHDQ,
21365   IX86_BUILTIN_PUNPCKLBW,
21366   IX86_BUILTIN_PUNPCKLWD,
21367   IX86_BUILTIN_PUNPCKLDQ,
21368
21369   IX86_BUILTIN_SHUFPS,
21370
21371   IX86_BUILTIN_RCPPS,
21372   IX86_BUILTIN_RCPSS,
21373   IX86_BUILTIN_RSQRTPS,
21374   IX86_BUILTIN_RSQRTPS_NR,
21375   IX86_BUILTIN_RSQRTSS,
21376   IX86_BUILTIN_RSQRTF,
21377   IX86_BUILTIN_SQRTPS,
21378   IX86_BUILTIN_SQRTPS_NR,
21379   IX86_BUILTIN_SQRTSS,
21380
21381   IX86_BUILTIN_UNPCKHPS,
21382   IX86_BUILTIN_UNPCKLPS,
21383
21384   IX86_BUILTIN_ANDPS,
21385   IX86_BUILTIN_ANDNPS,
21386   IX86_BUILTIN_ORPS,
21387   IX86_BUILTIN_XORPS,
21388
21389   IX86_BUILTIN_EMMS,
21390   IX86_BUILTIN_LDMXCSR,
21391   IX86_BUILTIN_STMXCSR,
21392   IX86_BUILTIN_SFENCE,
21393
21394   /* 3DNow! Original */
21395   IX86_BUILTIN_FEMMS,
21396   IX86_BUILTIN_PAVGUSB,
21397   IX86_BUILTIN_PF2ID,
21398   IX86_BUILTIN_PFACC,
21399   IX86_BUILTIN_PFADD,
21400   IX86_BUILTIN_PFCMPEQ,
21401   IX86_BUILTIN_PFCMPGE,
21402   IX86_BUILTIN_PFCMPGT,
21403   IX86_BUILTIN_PFMAX,
21404   IX86_BUILTIN_PFMIN,
21405   IX86_BUILTIN_PFMUL,
21406   IX86_BUILTIN_PFRCP,
21407   IX86_BUILTIN_PFRCPIT1,
21408   IX86_BUILTIN_PFRCPIT2,
21409   IX86_BUILTIN_PFRSQIT1,
21410   IX86_BUILTIN_PFRSQRT,
21411   IX86_BUILTIN_PFSUB,
21412   IX86_BUILTIN_PFSUBR,
21413   IX86_BUILTIN_PI2FD,
21414   IX86_BUILTIN_PMULHRW,
21415
21416   /* 3DNow! Athlon Extensions */
21417   IX86_BUILTIN_PF2IW,
21418   IX86_BUILTIN_PFNACC,
21419   IX86_BUILTIN_PFPNACC,
21420   IX86_BUILTIN_PI2FW,
21421   IX86_BUILTIN_PSWAPDSI,
21422   IX86_BUILTIN_PSWAPDSF,
21423
21424   /* SSE2 */
21425   IX86_BUILTIN_ADDPD,
21426   IX86_BUILTIN_ADDSD,
21427   IX86_BUILTIN_DIVPD,
21428   IX86_BUILTIN_DIVSD,
21429   IX86_BUILTIN_MULPD,
21430   IX86_BUILTIN_MULSD,
21431   IX86_BUILTIN_SUBPD,
21432   IX86_BUILTIN_SUBSD,
21433
21434   IX86_BUILTIN_CMPEQPD,
21435   IX86_BUILTIN_CMPLTPD,
21436   IX86_BUILTIN_CMPLEPD,
21437   IX86_BUILTIN_CMPGTPD,
21438   IX86_BUILTIN_CMPGEPD,
21439   IX86_BUILTIN_CMPNEQPD,
21440   IX86_BUILTIN_CMPNLTPD,
21441   IX86_BUILTIN_CMPNLEPD,
21442   IX86_BUILTIN_CMPNGTPD,
21443   IX86_BUILTIN_CMPNGEPD,
21444   IX86_BUILTIN_CMPORDPD,
21445   IX86_BUILTIN_CMPUNORDPD,
21446   IX86_BUILTIN_CMPEQSD,
21447   IX86_BUILTIN_CMPLTSD,
21448   IX86_BUILTIN_CMPLESD,
21449   IX86_BUILTIN_CMPNEQSD,
21450   IX86_BUILTIN_CMPNLTSD,
21451   IX86_BUILTIN_CMPNLESD,
21452   IX86_BUILTIN_CMPORDSD,
21453   IX86_BUILTIN_CMPUNORDSD,
21454
21455   IX86_BUILTIN_COMIEQSD,
21456   IX86_BUILTIN_COMILTSD,
21457   IX86_BUILTIN_COMILESD,
21458   IX86_BUILTIN_COMIGTSD,
21459   IX86_BUILTIN_COMIGESD,
21460   IX86_BUILTIN_COMINEQSD,
21461   IX86_BUILTIN_UCOMIEQSD,
21462   IX86_BUILTIN_UCOMILTSD,
21463   IX86_BUILTIN_UCOMILESD,
21464   IX86_BUILTIN_UCOMIGTSD,
21465   IX86_BUILTIN_UCOMIGESD,
21466   IX86_BUILTIN_UCOMINEQSD,
21467
21468   IX86_BUILTIN_MAXPD,
21469   IX86_BUILTIN_MAXSD,
21470   IX86_BUILTIN_MINPD,
21471   IX86_BUILTIN_MINSD,
21472
21473   IX86_BUILTIN_ANDPD,
21474   IX86_BUILTIN_ANDNPD,
21475   IX86_BUILTIN_ORPD,
21476   IX86_BUILTIN_XORPD,
21477
21478   IX86_BUILTIN_SQRTPD,
21479   IX86_BUILTIN_SQRTSD,
21480
21481   IX86_BUILTIN_UNPCKHPD,
21482   IX86_BUILTIN_UNPCKLPD,
21483
21484   IX86_BUILTIN_SHUFPD,
21485
21486   IX86_BUILTIN_LOADUPD,
21487   IX86_BUILTIN_STOREUPD,
21488   IX86_BUILTIN_MOVSD,
21489
21490   IX86_BUILTIN_LOADHPD,
21491   IX86_BUILTIN_LOADLPD,
21492
21493   IX86_BUILTIN_CVTDQ2PD,
21494   IX86_BUILTIN_CVTDQ2PS,
21495
21496   IX86_BUILTIN_CVTPD2DQ,
21497   IX86_BUILTIN_CVTPD2PI,
21498   IX86_BUILTIN_CVTPD2PS,
21499   IX86_BUILTIN_CVTTPD2DQ,
21500   IX86_BUILTIN_CVTTPD2PI,
21501
21502   IX86_BUILTIN_CVTPI2PD,
21503   IX86_BUILTIN_CVTSI2SD,
21504   IX86_BUILTIN_CVTSI642SD,
21505
21506   IX86_BUILTIN_CVTSD2SI,
21507   IX86_BUILTIN_CVTSD2SI64,
21508   IX86_BUILTIN_CVTSD2SS,
21509   IX86_BUILTIN_CVTSS2SD,
21510   IX86_BUILTIN_CVTTSD2SI,
21511   IX86_BUILTIN_CVTTSD2SI64,
21512
21513   IX86_BUILTIN_CVTPS2DQ,
21514   IX86_BUILTIN_CVTPS2PD,
21515   IX86_BUILTIN_CVTTPS2DQ,
21516
21517   IX86_BUILTIN_MOVNTI,
21518   IX86_BUILTIN_MOVNTPD,
21519   IX86_BUILTIN_MOVNTDQ,
21520
21521   IX86_BUILTIN_MOVQ128,
21522
21523   /* SSE2 MMX */
21524   IX86_BUILTIN_MASKMOVDQU,
21525   IX86_BUILTIN_MOVMSKPD,
21526   IX86_BUILTIN_PMOVMSKB128,
21527
21528   IX86_BUILTIN_PACKSSWB128,
21529   IX86_BUILTIN_PACKSSDW128,
21530   IX86_BUILTIN_PACKUSWB128,
21531
21532   IX86_BUILTIN_PADDB128,
21533   IX86_BUILTIN_PADDW128,
21534   IX86_BUILTIN_PADDD128,
21535   IX86_BUILTIN_PADDQ128,
21536   IX86_BUILTIN_PADDSB128,
21537   IX86_BUILTIN_PADDSW128,
21538   IX86_BUILTIN_PADDUSB128,
21539   IX86_BUILTIN_PADDUSW128,
21540   IX86_BUILTIN_PSUBB128,
21541   IX86_BUILTIN_PSUBW128,
21542   IX86_BUILTIN_PSUBD128,
21543   IX86_BUILTIN_PSUBQ128,
21544   IX86_BUILTIN_PSUBSB128,
21545   IX86_BUILTIN_PSUBSW128,
21546   IX86_BUILTIN_PSUBUSB128,
21547   IX86_BUILTIN_PSUBUSW128,
21548
21549   IX86_BUILTIN_PAND128,
21550   IX86_BUILTIN_PANDN128,
21551   IX86_BUILTIN_POR128,
21552   IX86_BUILTIN_PXOR128,
21553
21554   IX86_BUILTIN_PAVGB128,
21555   IX86_BUILTIN_PAVGW128,
21556
21557   IX86_BUILTIN_PCMPEQB128,
21558   IX86_BUILTIN_PCMPEQW128,
21559   IX86_BUILTIN_PCMPEQD128,
21560   IX86_BUILTIN_PCMPGTB128,
21561   IX86_BUILTIN_PCMPGTW128,
21562   IX86_BUILTIN_PCMPGTD128,
21563
21564   IX86_BUILTIN_PMADDWD128,
21565
21566   IX86_BUILTIN_PMAXSW128,
21567   IX86_BUILTIN_PMAXUB128,
21568   IX86_BUILTIN_PMINSW128,
21569   IX86_BUILTIN_PMINUB128,
21570
21571   IX86_BUILTIN_PMULUDQ,
21572   IX86_BUILTIN_PMULUDQ128,
21573   IX86_BUILTIN_PMULHUW128,
21574   IX86_BUILTIN_PMULHW128,
21575   IX86_BUILTIN_PMULLW128,
21576
21577   IX86_BUILTIN_PSADBW128,
21578   IX86_BUILTIN_PSHUFHW,
21579   IX86_BUILTIN_PSHUFLW,
21580   IX86_BUILTIN_PSHUFD,
21581
21582   IX86_BUILTIN_PSLLDQI128,
21583   IX86_BUILTIN_PSLLWI128,
21584   IX86_BUILTIN_PSLLDI128,
21585   IX86_BUILTIN_PSLLQI128,
21586   IX86_BUILTIN_PSRAWI128,
21587   IX86_BUILTIN_PSRADI128,
21588   IX86_BUILTIN_PSRLDQI128,
21589   IX86_BUILTIN_PSRLWI128,
21590   IX86_BUILTIN_PSRLDI128,
21591   IX86_BUILTIN_PSRLQI128,
21592
21593   IX86_BUILTIN_PSLLDQ128,
21594   IX86_BUILTIN_PSLLW128,
21595   IX86_BUILTIN_PSLLD128,
21596   IX86_BUILTIN_PSLLQ128,
21597   IX86_BUILTIN_PSRAW128,
21598   IX86_BUILTIN_PSRAD128,
21599   IX86_BUILTIN_PSRLW128,
21600   IX86_BUILTIN_PSRLD128,
21601   IX86_BUILTIN_PSRLQ128,
21602
21603   IX86_BUILTIN_PUNPCKHBW128,
21604   IX86_BUILTIN_PUNPCKHWD128,
21605   IX86_BUILTIN_PUNPCKHDQ128,
21606   IX86_BUILTIN_PUNPCKHQDQ128,
21607   IX86_BUILTIN_PUNPCKLBW128,
21608   IX86_BUILTIN_PUNPCKLWD128,
21609   IX86_BUILTIN_PUNPCKLDQ128,
21610   IX86_BUILTIN_PUNPCKLQDQ128,
21611
21612   IX86_BUILTIN_CLFLUSH,
21613   IX86_BUILTIN_MFENCE,
21614   IX86_BUILTIN_LFENCE,
21615
21616   IX86_BUILTIN_BSRSI,
21617   IX86_BUILTIN_BSRDI,
21618   IX86_BUILTIN_RDPMC,
21619   IX86_BUILTIN_RDTSC,
21620   IX86_BUILTIN_RDTSCP,
21621   IX86_BUILTIN_ROLQI,
21622   IX86_BUILTIN_ROLHI,
21623   IX86_BUILTIN_RORQI,
21624   IX86_BUILTIN_RORHI,
21625
21626   /* SSE3.  */
21627   IX86_BUILTIN_ADDSUBPS,
21628   IX86_BUILTIN_HADDPS,
21629   IX86_BUILTIN_HSUBPS,
21630   IX86_BUILTIN_MOVSHDUP,
21631   IX86_BUILTIN_MOVSLDUP,
21632   IX86_BUILTIN_ADDSUBPD,
21633   IX86_BUILTIN_HADDPD,
21634   IX86_BUILTIN_HSUBPD,
21635   IX86_BUILTIN_LDDQU,
21636
21637   IX86_BUILTIN_MONITOR,
21638   IX86_BUILTIN_MWAIT,
21639
21640   /* SSSE3.  */
21641   IX86_BUILTIN_PHADDW,
21642   IX86_BUILTIN_PHADDD,
21643   IX86_BUILTIN_PHADDSW,
21644   IX86_BUILTIN_PHSUBW,
21645   IX86_BUILTIN_PHSUBD,
21646   IX86_BUILTIN_PHSUBSW,
21647   IX86_BUILTIN_PMADDUBSW,
21648   IX86_BUILTIN_PMULHRSW,
21649   IX86_BUILTIN_PSHUFB,
21650   IX86_BUILTIN_PSIGNB,
21651   IX86_BUILTIN_PSIGNW,
21652   IX86_BUILTIN_PSIGND,
21653   IX86_BUILTIN_PALIGNR,
21654   IX86_BUILTIN_PABSB,
21655   IX86_BUILTIN_PABSW,
21656   IX86_BUILTIN_PABSD,
21657
21658   IX86_BUILTIN_PHADDW128,
21659   IX86_BUILTIN_PHADDD128,
21660   IX86_BUILTIN_PHADDSW128,
21661   IX86_BUILTIN_PHSUBW128,
21662   IX86_BUILTIN_PHSUBD128,
21663   IX86_BUILTIN_PHSUBSW128,
21664   IX86_BUILTIN_PMADDUBSW128,
21665   IX86_BUILTIN_PMULHRSW128,
21666   IX86_BUILTIN_PSHUFB128,
21667   IX86_BUILTIN_PSIGNB128,
21668   IX86_BUILTIN_PSIGNW128,
21669   IX86_BUILTIN_PSIGND128,
21670   IX86_BUILTIN_PALIGNR128,
21671   IX86_BUILTIN_PABSB128,
21672   IX86_BUILTIN_PABSW128,
21673   IX86_BUILTIN_PABSD128,
21674
21675   /* AMDFAM10 - SSE4A New Instructions.  */
21676   IX86_BUILTIN_MOVNTSD,
21677   IX86_BUILTIN_MOVNTSS,
21678   IX86_BUILTIN_EXTRQI,
21679   IX86_BUILTIN_EXTRQ,
21680   IX86_BUILTIN_INSERTQI,
21681   IX86_BUILTIN_INSERTQ,
21682
21683   /* SSE4.1.  */
21684   IX86_BUILTIN_BLENDPD,
21685   IX86_BUILTIN_BLENDPS,
21686   IX86_BUILTIN_BLENDVPD,
21687   IX86_BUILTIN_BLENDVPS,
21688   IX86_BUILTIN_PBLENDVB128,
21689   IX86_BUILTIN_PBLENDW128,
21690
21691   IX86_BUILTIN_DPPD,
21692   IX86_BUILTIN_DPPS,
21693
21694   IX86_BUILTIN_INSERTPS128,
21695
21696   IX86_BUILTIN_MOVNTDQA,
21697   IX86_BUILTIN_MPSADBW128,
21698   IX86_BUILTIN_PACKUSDW128,
21699   IX86_BUILTIN_PCMPEQQ,
21700   IX86_BUILTIN_PHMINPOSUW128,
21701
21702   IX86_BUILTIN_PMAXSB128,
21703   IX86_BUILTIN_PMAXSD128,
21704   IX86_BUILTIN_PMAXUD128,
21705   IX86_BUILTIN_PMAXUW128,
21706
21707   IX86_BUILTIN_PMINSB128,
21708   IX86_BUILTIN_PMINSD128,
21709   IX86_BUILTIN_PMINUD128,
21710   IX86_BUILTIN_PMINUW128,
21711
21712   IX86_BUILTIN_PMOVSXBW128,
21713   IX86_BUILTIN_PMOVSXBD128,
21714   IX86_BUILTIN_PMOVSXBQ128,
21715   IX86_BUILTIN_PMOVSXWD128,
21716   IX86_BUILTIN_PMOVSXWQ128,
21717   IX86_BUILTIN_PMOVSXDQ128,
21718
21719   IX86_BUILTIN_PMOVZXBW128,
21720   IX86_BUILTIN_PMOVZXBD128,
21721   IX86_BUILTIN_PMOVZXBQ128,
21722   IX86_BUILTIN_PMOVZXWD128,
21723   IX86_BUILTIN_PMOVZXWQ128,
21724   IX86_BUILTIN_PMOVZXDQ128,
21725
21726   IX86_BUILTIN_PMULDQ128,
21727   IX86_BUILTIN_PMULLD128,
21728
21729   IX86_BUILTIN_ROUNDPD,
21730   IX86_BUILTIN_ROUNDPS,
21731   IX86_BUILTIN_ROUNDSD,
21732   IX86_BUILTIN_ROUNDSS,
21733
21734   IX86_BUILTIN_PTESTZ,
21735   IX86_BUILTIN_PTESTC,
21736   IX86_BUILTIN_PTESTNZC,
21737
21738   IX86_BUILTIN_VEC_INIT_V2SI,
21739   IX86_BUILTIN_VEC_INIT_V4HI,
21740   IX86_BUILTIN_VEC_INIT_V8QI,
21741   IX86_BUILTIN_VEC_EXT_V2DF,
21742   IX86_BUILTIN_VEC_EXT_V2DI,
21743   IX86_BUILTIN_VEC_EXT_V4SF,
21744   IX86_BUILTIN_VEC_EXT_V4SI,
21745   IX86_BUILTIN_VEC_EXT_V8HI,
21746   IX86_BUILTIN_VEC_EXT_V2SI,
21747   IX86_BUILTIN_VEC_EXT_V4HI,
21748   IX86_BUILTIN_VEC_EXT_V16QI,
21749   IX86_BUILTIN_VEC_SET_V2DI,
21750   IX86_BUILTIN_VEC_SET_V4SF,
21751   IX86_BUILTIN_VEC_SET_V4SI,
21752   IX86_BUILTIN_VEC_SET_V8HI,
21753   IX86_BUILTIN_VEC_SET_V4HI,
21754   IX86_BUILTIN_VEC_SET_V16QI,
21755
21756   IX86_BUILTIN_VEC_PACK_SFIX,
21757
21758   /* SSE4.2.  */
21759   IX86_BUILTIN_CRC32QI,
21760   IX86_BUILTIN_CRC32HI,
21761   IX86_BUILTIN_CRC32SI,
21762   IX86_BUILTIN_CRC32DI,
21763
21764   IX86_BUILTIN_PCMPESTRI128,
21765   IX86_BUILTIN_PCMPESTRM128,
21766   IX86_BUILTIN_PCMPESTRA128,
21767   IX86_BUILTIN_PCMPESTRC128,
21768   IX86_BUILTIN_PCMPESTRO128,
21769   IX86_BUILTIN_PCMPESTRS128,
21770   IX86_BUILTIN_PCMPESTRZ128,
21771   IX86_BUILTIN_PCMPISTRI128,
21772   IX86_BUILTIN_PCMPISTRM128,
21773   IX86_BUILTIN_PCMPISTRA128,
21774   IX86_BUILTIN_PCMPISTRC128,
21775   IX86_BUILTIN_PCMPISTRO128,
21776   IX86_BUILTIN_PCMPISTRS128,
21777   IX86_BUILTIN_PCMPISTRZ128,
21778
21779   IX86_BUILTIN_PCMPGTQ,
21780
21781   /* AES instructions */
21782   IX86_BUILTIN_AESENC128,
21783   IX86_BUILTIN_AESENCLAST128,
21784   IX86_BUILTIN_AESDEC128,
21785   IX86_BUILTIN_AESDECLAST128,
21786   IX86_BUILTIN_AESIMC128,
21787   IX86_BUILTIN_AESKEYGENASSIST128,
21788
21789   /* PCLMUL instruction */
21790   IX86_BUILTIN_PCLMULQDQ128,
21791
21792   /* AVX */
21793   IX86_BUILTIN_ADDPD256,
21794   IX86_BUILTIN_ADDPS256,
21795   IX86_BUILTIN_ADDSUBPD256,
21796   IX86_BUILTIN_ADDSUBPS256,
21797   IX86_BUILTIN_ANDPD256,
21798   IX86_BUILTIN_ANDPS256,
21799   IX86_BUILTIN_ANDNPD256,
21800   IX86_BUILTIN_ANDNPS256,
21801   IX86_BUILTIN_BLENDPD256,
21802   IX86_BUILTIN_BLENDPS256,
21803   IX86_BUILTIN_BLENDVPD256,
21804   IX86_BUILTIN_BLENDVPS256,
21805   IX86_BUILTIN_DIVPD256,
21806   IX86_BUILTIN_DIVPS256,
21807   IX86_BUILTIN_DPPS256,
21808   IX86_BUILTIN_HADDPD256,
21809   IX86_BUILTIN_HADDPS256,
21810   IX86_BUILTIN_HSUBPD256,
21811   IX86_BUILTIN_HSUBPS256,
21812   IX86_BUILTIN_MAXPD256,
21813   IX86_BUILTIN_MAXPS256,
21814   IX86_BUILTIN_MINPD256,
21815   IX86_BUILTIN_MINPS256,
21816   IX86_BUILTIN_MULPD256,
21817   IX86_BUILTIN_MULPS256,
21818   IX86_BUILTIN_ORPD256,
21819   IX86_BUILTIN_ORPS256,
21820   IX86_BUILTIN_SHUFPD256,
21821   IX86_BUILTIN_SHUFPS256,
21822   IX86_BUILTIN_SUBPD256,
21823   IX86_BUILTIN_SUBPS256,
21824   IX86_BUILTIN_XORPD256,
21825   IX86_BUILTIN_XORPS256,
21826   IX86_BUILTIN_CMPSD,
21827   IX86_BUILTIN_CMPSS,
21828   IX86_BUILTIN_CMPPD,
21829   IX86_BUILTIN_CMPPS,
21830   IX86_BUILTIN_CMPPD256,
21831   IX86_BUILTIN_CMPPS256,
21832   IX86_BUILTIN_CVTDQ2PD256,
21833   IX86_BUILTIN_CVTDQ2PS256,
21834   IX86_BUILTIN_CVTPD2PS256,
21835   IX86_BUILTIN_CVTPS2DQ256,
21836   IX86_BUILTIN_CVTPS2PD256,
21837   IX86_BUILTIN_CVTTPD2DQ256,
21838   IX86_BUILTIN_CVTPD2DQ256,
21839   IX86_BUILTIN_CVTTPS2DQ256,
21840   IX86_BUILTIN_EXTRACTF128PD256,
21841   IX86_BUILTIN_EXTRACTF128PS256,
21842   IX86_BUILTIN_EXTRACTF128SI256,
21843   IX86_BUILTIN_VZEROALL,
21844   IX86_BUILTIN_VZEROUPPER,
21845   IX86_BUILTIN_VPERMILVARPD,
21846   IX86_BUILTIN_VPERMILVARPS,
21847   IX86_BUILTIN_VPERMILVARPD256,
21848   IX86_BUILTIN_VPERMILVARPS256,
21849   IX86_BUILTIN_VPERMILPD,
21850   IX86_BUILTIN_VPERMILPS,
21851   IX86_BUILTIN_VPERMILPD256,
21852   IX86_BUILTIN_VPERMILPS256,
21853   IX86_BUILTIN_VPERMIL2PD,
21854   IX86_BUILTIN_VPERMIL2PS,
21855   IX86_BUILTIN_VPERMIL2PD256,
21856   IX86_BUILTIN_VPERMIL2PS256,
21857   IX86_BUILTIN_VPERM2F128PD256,
21858   IX86_BUILTIN_VPERM2F128PS256,
21859   IX86_BUILTIN_VPERM2F128SI256,
21860   IX86_BUILTIN_VBROADCASTSS,
21861   IX86_BUILTIN_VBROADCASTSD256,
21862   IX86_BUILTIN_VBROADCASTSS256,
21863   IX86_BUILTIN_VBROADCASTPD256,
21864   IX86_BUILTIN_VBROADCASTPS256,
21865   IX86_BUILTIN_VINSERTF128PD256,
21866   IX86_BUILTIN_VINSERTF128PS256,
21867   IX86_BUILTIN_VINSERTF128SI256,
21868   IX86_BUILTIN_LOADUPD256,
21869   IX86_BUILTIN_LOADUPS256,
21870   IX86_BUILTIN_STOREUPD256,
21871   IX86_BUILTIN_STOREUPS256,
21872   IX86_BUILTIN_LDDQU256,
21873   IX86_BUILTIN_MOVNTDQ256,
21874   IX86_BUILTIN_MOVNTPD256,
21875   IX86_BUILTIN_MOVNTPS256,
21876   IX86_BUILTIN_LOADDQU256,
21877   IX86_BUILTIN_STOREDQU256,
21878   IX86_BUILTIN_MASKLOADPD,
21879   IX86_BUILTIN_MASKLOADPS,
21880   IX86_BUILTIN_MASKSTOREPD,
21881   IX86_BUILTIN_MASKSTOREPS,
21882   IX86_BUILTIN_MASKLOADPD256,
21883   IX86_BUILTIN_MASKLOADPS256,
21884   IX86_BUILTIN_MASKSTOREPD256,
21885   IX86_BUILTIN_MASKSTOREPS256,
21886   IX86_BUILTIN_MOVSHDUP256,
21887   IX86_BUILTIN_MOVSLDUP256,
21888   IX86_BUILTIN_MOVDDUP256,
21889
21890   IX86_BUILTIN_SQRTPD256,
21891   IX86_BUILTIN_SQRTPS256,
21892   IX86_BUILTIN_SQRTPS_NR256,
21893   IX86_BUILTIN_RSQRTPS256,
21894   IX86_BUILTIN_RSQRTPS_NR256,
21895
21896   IX86_BUILTIN_RCPPS256,
21897
21898   IX86_BUILTIN_ROUNDPD256,
21899   IX86_BUILTIN_ROUNDPS256,
21900
21901   IX86_BUILTIN_UNPCKHPD256,
21902   IX86_BUILTIN_UNPCKLPD256,
21903   IX86_BUILTIN_UNPCKHPS256,
21904   IX86_BUILTIN_UNPCKLPS256,
21905
21906   IX86_BUILTIN_SI256_SI,
21907   IX86_BUILTIN_PS256_PS,
21908   IX86_BUILTIN_PD256_PD,
21909   IX86_BUILTIN_SI_SI256,
21910   IX86_BUILTIN_PS_PS256,
21911   IX86_BUILTIN_PD_PD256,
21912
21913   IX86_BUILTIN_VTESTZPD,
21914   IX86_BUILTIN_VTESTCPD,
21915   IX86_BUILTIN_VTESTNZCPD,
21916   IX86_BUILTIN_VTESTZPS,
21917   IX86_BUILTIN_VTESTCPS,
21918   IX86_BUILTIN_VTESTNZCPS,
21919   IX86_BUILTIN_VTESTZPD256,
21920   IX86_BUILTIN_VTESTCPD256,
21921   IX86_BUILTIN_VTESTNZCPD256,
21922   IX86_BUILTIN_VTESTZPS256,
21923   IX86_BUILTIN_VTESTCPS256,
21924   IX86_BUILTIN_VTESTNZCPS256,
21925   IX86_BUILTIN_PTESTZ256,
21926   IX86_BUILTIN_PTESTC256,
21927   IX86_BUILTIN_PTESTNZC256,
21928
21929   IX86_BUILTIN_MOVMSKPD256,
21930   IX86_BUILTIN_MOVMSKPS256,
21931
21932   /* TFmode support builtins.  */
21933   IX86_BUILTIN_INFQ,
21934   IX86_BUILTIN_HUGE_VALQ,
21935   IX86_BUILTIN_FABSQ,
21936   IX86_BUILTIN_COPYSIGNQ,
21937
21938   /* Vectorizer support builtins.  */
21939   IX86_BUILTIN_CPYSGNPS,
21940   IX86_BUILTIN_CPYSGNPD,
21941
21942   IX86_BUILTIN_CVTUDQ2PS,
21943
21944   IX86_BUILTIN_VEC_PERM_V2DF,
21945   IX86_BUILTIN_VEC_PERM_V4SF,
21946   IX86_BUILTIN_VEC_PERM_V2DI,
21947   IX86_BUILTIN_VEC_PERM_V4SI,
21948   IX86_BUILTIN_VEC_PERM_V8HI,
21949   IX86_BUILTIN_VEC_PERM_V16QI,
21950   IX86_BUILTIN_VEC_PERM_V2DI_U,
21951   IX86_BUILTIN_VEC_PERM_V4SI_U,
21952   IX86_BUILTIN_VEC_PERM_V8HI_U,
21953   IX86_BUILTIN_VEC_PERM_V16QI_U,
21954   IX86_BUILTIN_VEC_PERM_V4DF,
21955   IX86_BUILTIN_VEC_PERM_V8SF,
21956
21957   /* FMA4 and XOP instructions.  */
21958   IX86_BUILTIN_VFMADDSS,
21959   IX86_BUILTIN_VFMADDSD,
21960   IX86_BUILTIN_VFMADDPS,
21961   IX86_BUILTIN_VFMADDPD,
21962   IX86_BUILTIN_VFMSUBSS,
21963   IX86_BUILTIN_VFMSUBSD,
21964   IX86_BUILTIN_VFMSUBPS,
21965   IX86_BUILTIN_VFMSUBPD,
21966   IX86_BUILTIN_VFMADDSUBPS,
21967   IX86_BUILTIN_VFMADDSUBPD,
21968   IX86_BUILTIN_VFMSUBADDPS,
21969   IX86_BUILTIN_VFMSUBADDPD,
21970   IX86_BUILTIN_VFNMADDSS,
21971   IX86_BUILTIN_VFNMADDSD,
21972   IX86_BUILTIN_VFNMADDPS,
21973   IX86_BUILTIN_VFNMADDPD,
21974   IX86_BUILTIN_VFNMSUBSS,
21975   IX86_BUILTIN_VFNMSUBSD,
21976   IX86_BUILTIN_VFNMSUBPS,
21977   IX86_BUILTIN_VFNMSUBPD,
21978   IX86_BUILTIN_VFMADDPS256,
21979   IX86_BUILTIN_VFMADDPD256,
21980   IX86_BUILTIN_VFMSUBPS256,
21981   IX86_BUILTIN_VFMSUBPD256,
21982   IX86_BUILTIN_VFMADDSUBPS256,
21983   IX86_BUILTIN_VFMADDSUBPD256,
21984   IX86_BUILTIN_VFMSUBADDPS256,
21985   IX86_BUILTIN_VFMSUBADDPD256,
21986   IX86_BUILTIN_VFNMADDPS256,
21987   IX86_BUILTIN_VFNMADDPD256,
21988   IX86_BUILTIN_VFNMSUBPS256,
21989   IX86_BUILTIN_VFNMSUBPD256,
21990
21991   IX86_BUILTIN_VPCMOV,
21992   IX86_BUILTIN_VPCMOV_V2DI,
21993   IX86_BUILTIN_VPCMOV_V4SI,
21994   IX86_BUILTIN_VPCMOV_V8HI,
21995   IX86_BUILTIN_VPCMOV_V16QI,
21996   IX86_BUILTIN_VPCMOV_V4SF,
21997   IX86_BUILTIN_VPCMOV_V2DF,
21998   IX86_BUILTIN_VPCMOV256,
21999   IX86_BUILTIN_VPCMOV_V4DI256,
22000   IX86_BUILTIN_VPCMOV_V8SI256,
22001   IX86_BUILTIN_VPCMOV_V16HI256,
22002   IX86_BUILTIN_VPCMOV_V32QI256,
22003   IX86_BUILTIN_VPCMOV_V8SF256,
22004   IX86_BUILTIN_VPCMOV_V4DF256,
22005
22006   IX86_BUILTIN_VPPERM,
22007
22008   IX86_BUILTIN_VPMACSSWW,
22009   IX86_BUILTIN_VPMACSWW,
22010   IX86_BUILTIN_VPMACSSWD,
22011   IX86_BUILTIN_VPMACSWD,
22012   IX86_BUILTIN_VPMACSSDD,
22013   IX86_BUILTIN_VPMACSDD,
22014   IX86_BUILTIN_VPMACSSDQL,
22015   IX86_BUILTIN_VPMACSSDQH,
22016   IX86_BUILTIN_VPMACSDQL,
22017   IX86_BUILTIN_VPMACSDQH,
22018   IX86_BUILTIN_VPMADCSSWD,
22019   IX86_BUILTIN_VPMADCSWD,
22020
22021   IX86_BUILTIN_VPHADDBW,
22022   IX86_BUILTIN_VPHADDBD,
22023   IX86_BUILTIN_VPHADDBQ,
22024   IX86_BUILTIN_VPHADDWD,
22025   IX86_BUILTIN_VPHADDWQ,
22026   IX86_BUILTIN_VPHADDDQ,
22027   IX86_BUILTIN_VPHADDUBW,
22028   IX86_BUILTIN_VPHADDUBD,
22029   IX86_BUILTIN_VPHADDUBQ,
22030   IX86_BUILTIN_VPHADDUWD,
22031   IX86_BUILTIN_VPHADDUWQ,
22032   IX86_BUILTIN_VPHADDUDQ,
22033   IX86_BUILTIN_VPHSUBBW,
22034   IX86_BUILTIN_VPHSUBWD,
22035   IX86_BUILTIN_VPHSUBDQ,
22036
22037   IX86_BUILTIN_VPROTB,
22038   IX86_BUILTIN_VPROTW,
22039   IX86_BUILTIN_VPROTD,
22040   IX86_BUILTIN_VPROTQ,
22041   IX86_BUILTIN_VPROTB_IMM,
22042   IX86_BUILTIN_VPROTW_IMM,
22043   IX86_BUILTIN_VPROTD_IMM,
22044   IX86_BUILTIN_VPROTQ_IMM,
22045
22046   IX86_BUILTIN_VPSHLB,
22047   IX86_BUILTIN_VPSHLW,
22048   IX86_BUILTIN_VPSHLD,
22049   IX86_BUILTIN_VPSHLQ,
22050   IX86_BUILTIN_VPSHAB,
22051   IX86_BUILTIN_VPSHAW,
22052   IX86_BUILTIN_VPSHAD,
22053   IX86_BUILTIN_VPSHAQ,
22054
22055   IX86_BUILTIN_VFRCZSS,
22056   IX86_BUILTIN_VFRCZSD,
22057   IX86_BUILTIN_VFRCZPS,
22058   IX86_BUILTIN_VFRCZPD,
22059   IX86_BUILTIN_VFRCZPS256,
22060   IX86_BUILTIN_VFRCZPD256,
22061
22062   IX86_BUILTIN_VPCOMEQUB,
22063   IX86_BUILTIN_VPCOMNEUB,
22064   IX86_BUILTIN_VPCOMLTUB,
22065   IX86_BUILTIN_VPCOMLEUB,
22066   IX86_BUILTIN_VPCOMGTUB,
22067   IX86_BUILTIN_VPCOMGEUB,
22068   IX86_BUILTIN_VPCOMFALSEUB,
22069   IX86_BUILTIN_VPCOMTRUEUB,
22070
22071   IX86_BUILTIN_VPCOMEQUW,
22072   IX86_BUILTIN_VPCOMNEUW,
22073   IX86_BUILTIN_VPCOMLTUW,
22074   IX86_BUILTIN_VPCOMLEUW,
22075   IX86_BUILTIN_VPCOMGTUW,
22076   IX86_BUILTIN_VPCOMGEUW,
22077   IX86_BUILTIN_VPCOMFALSEUW,
22078   IX86_BUILTIN_VPCOMTRUEUW,
22079
22080   IX86_BUILTIN_VPCOMEQUD,
22081   IX86_BUILTIN_VPCOMNEUD,
22082   IX86_BUILTIN_VPCOMLTUD,
22083   IX86_BUILTIN_VPCOMLEUD,
22084   IX86_BUILTIN_VPCOMGTUD,
22085   IX86_BUILTIN_VPCOMGEUD,
22086   IX86_BUILTIN_VPCOMFALSEUD,
22087   IX86_BUILTIN_VPCOMTRUEUD,
22088
22089   IX86_BUILTIN_VPCOMEQUQ,
22090   IX86_BUILTIN_VPCOMNEUQ,
22091   IX86_BUILTIN_VPCOMLTUQ,
22092   IX86_BUILTIN_VPCOMLEUQ,
22093   IX86_BUILTIN_VPCOMGTUQ,
22094   IX86_BUILTIN_VPCOMGEUQ,
22095   IX86_BUILTIN_VPCOMFALSEUQ,
22096   IX86_BUILTIN_VPCOMTRUEUQ,
22097
22098   IX86_BUILTIN_VPCOMEQB,
22099   IX86_BUILTIN_VPCOMNEB,
22100   IX86_BUILTIN_VPCOMLTB,
22101   IX86_BUILTIN_VPCOMLEB,
22102   IX86_BUILTIN_VPCOMGTB,
22103   IX86_BUILTIN_VPCOMGEB,
22104   IX86_BUILTIN_VPCOMFALSEB,
22105   IX86_BUILTIN_VPCOMTRUEB,
22106
22107   IX86_BUILTIN_VPCOMEQW,
22108   IX86_BUILTIN_VPCOMNEW,
22109   IX86_BUILTIN_VPCOMLTW,
22110   IX86_BUILTIN_VPCOMLEW,
22111   IX86_BUILTIN_VPCOMGTW,
22112   IX86_BUILTIN_VPCOMGEW,
22113   IX86_BUILTIN_VPCOMFALSEW,
22114   IX86_BUILTIN_VPCOMTRUEW,
22115
22116   IX86_BUILTIN_VPCOMEQD,
22117   IX86_BUILTIN_VPCOMNED,
22118   IX86_BUILTIN_VPCOMLTD,
22119   IX86_BUILTIN_VPCOMLED,
22120   IX86_BUILTIN_VPCOMGTD,
22121   IX86_BUILTIN_VPCOMGED,
22122   IX86_BUILTIN_VPCOMFALSED,
22123   IX86_BUILTIN_VPCOMTRUED,
22124
22125   IX86_BUILTIN_VPCOMEQQ,
22126   IX86_BUILTIN_VPCOMNEQ,
22127   IX86_BUILTIN_VPCOMLTQ,
22128   IX86_BUILTIN_VPCOMLEQ,
22129   IX86_BUILTIN_VPCOMGTQ,
22130   IX86_BUILTIN_VPCOMGEQ,
22131   IX86_BUILTIN_VPCOMFALSEQ,
22132   IX86_BUILTIN_VPCOMTRUEQ,
22133
22134   /* LWP instructions.  */
22135   IX86_BUILTIN_LLWPCB,
22136   IX86_BUILTIN_SLWPCB,
22137   IX86_BUILTIN_LWPVAL32,
22138   IX86_BUILTIN_LWPVAL64,
22139   IX86_BUILTIN_LWPINS32,
22140   IX86_BUILTIN_LWPINS64,
22141
22142   IX86_BUILTIN_CLZS,
22143
22144   /* FSGSBASE instructions.  */
22145   IX86_BUILTIN_RDFSBASE32,
22146   IX86_BUILTIN_RDFSBASE64,
22147   IX86_BUILTIN_RDGSBASE32,
22148   IX86_BUILTIN_RDGSBASE64,
22149   IX86_BUILTIN_WRFSBASE32,
22150   IX86_BUILTIN_WRFSBASE64,
22151   IX86_BUILTIN_WRGSBASE32,
22152   IX86_BUILTIN_WRGSBASE64,
22153
22154   /* RDRND instructions.  */
22155   IX86_BUILTIN_RDRAND16,
22156   IX86_BUILTIN_RDRAND32,
22157   IX86_BUILTIN_RDRAND64,
22158
22159   /* F16C instructions.  */
22160   IX86_BUILTIN_CVTPH2PS,
22161   IX86_BUILTIN_CVTPH2PS256,
22162   IX86_BUILTIN_CVTPS2PH,
22163   IX86_BUILTIN_CVTPS2PH256,
22164
22165   IX86_BUILTIN_MAX
22166 };
22167
22168 /* Table for the ix86 builtin decls.  */
22169 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
22170
22171 /* Table of all of the builtin functions that are possible with different ISA's
22172    but are waiting to be built until a function is declared to use that
22173    ISA.  */
22174 struct builtin_isa {
22175   const char *name;             /* function name */
22176   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
22177   int isa;                      /* isa_flags this builtin is defined for */
22178   bool const_p;                 /* true if the declaration is constant */
22179   bool set_and_not_built_p;
22180 };
22181
22182 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
22183
22184
22185 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
22186    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
22187    function decl in the ix86_builtins array.  Returns the function decl or
22188    NULL_TREE, if the builtin was not added.
22189
22190    If the front end has a special hook for builtin functions, delay adding
22191    builtin functions that aren't in the current ISA until the ISA is changed
22192    with function specific optimization.  Doing so, can save about 300K for the
22193    default compiler.  When the builtin is expanded, check at that time whether
22194    it is valid.
22195
22196    If the front end doesn't have a special hook, record all builtins, even if
22197    it isn't an instruction set in the current ISA in case the user uses
22198    function specific options for a different ISA, so that we don't get scope
22199    errors if a builtin is added in the middle of a function scope.  */
22200
22201 static inline tree
22202 def_builtin (int mask, const char *name, enum ix86_builtin_func_type tcode,
22203              enum ix86_builtins code)
22204 {
22205   tree decl = NULL_TREE;
22206
22207   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
22208     {
22209       ix86_builtins_isa[(int) code].isa = mask;
22210
22211       mask &= ~OPTION_MASK_ISA_64BIT;
22212       if (mask == 0
22213           || (mask & ix86_isa_flags) != 0
22214           || (lang_hooks.builtin_function
22215               == lang_hooks.builtin_function_ext_scope))
22216
22217         {
22218           tree type = ix86_get_builtin_func_type (tcode);
22219           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
22220                                        NULL, NULL_TREE);
22221           ix86_builtins[(int) code] = decl;
22222           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
22223         }
22224       else
22225         {
22226           ix86_builtins[(int) code] = NULL_TREE;
22227           ix86_builtins_isa[(int) code].tcode = tcode;
22228           ix86_builtins_isa[(int) code].name = name;
22229           ix86_builtins_isa[(int) code].const_p = false;
22230           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
22231         }
22232     }
22233
22234   return decl;
22235 }
22236
22237 /* Like def_builtin, but also marks the function decl "const".  */
22238
22239 static inline tree
22240 def_builtin_const (int mask, const char *name,
22241                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
22242 {
22243   tree decl = def_builtin (mask, name, tcode, code);
22244   if (decl)
22245     TREE_READONLY (decl) = 1;
22246   else
22247     ix86_builtins_isa[(int) code].const_p = true;
22248
22249   return decl;
22250 }
22251
22252 /* Add any new builtin functions for a given ISA that may not have been
22253    declared.  This saves a bit of space compared to adding all of the
22254    declarations to the tree, even if we didn't use them.  */
22255
22256 static void
22257 ix86_add_new_builtins (int isa)
22258 {
22259   int i;
22260
22261   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
22262     {
22263       if ((ix86_builtins_isa[i].isa & isa) != 0
22264           && ix86_builtins_isa[i].set_and_not_built_p)
22265         {
22266           tree decl, type;
22267
22268           /* Don't define the builtin again.  */
22269           ix86_builtins_isa[i].set_and_not_built_p = false;
22270
22271           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
22272           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
22273                                                  type, i, BUILT_IN_MD, NULL,
22274                                                  NULL_TREE);
22275
22276           ix86_builtins[i] = decl;
22277           if (ix86_builtins_isa[i].const_p)
22278             TREE_READONLY (decl) = 1;
22279         }
22280     }
22281 }
22282
22283 /* Bits for builtin_description.flag.  */
22284
22285 /* Set when we don't support the comparison natively, and should
22286    swap_comparison in order to support it.  */
22287 #define BUILTIN_DESC_SWAP_OPERANDS      1
22288
22289 struct builtin_description
22290 {
22291   const unsigned int mask;
22292   const enum insn_code icode;
22293   const char *const name;
22294   const enum ix86_builtins code;
22295   const enum rtx_code comparison;
22296   const int flag;
22297 };
22298
22299 static const struct builtin_description bdesc_comi[] =
22300 {
22301   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
22302   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
22303   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
22304   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
22305   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
22306   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
22307   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
22308   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
22309   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
22310   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
22311   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
22312   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
22313   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
22314   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
22315   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
22316   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
22317   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
22318   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
22319   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
22320   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
22321   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
22322   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
22323   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
22324   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
22325 };
22326
22327 static const struct builtin_description bdesc_pcmpestr[] =
22328 {
22329   /* SSE4.2 */
22330   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
22331   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
22332   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
22333   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
22334   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
22335   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
22336   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
22337 };
22338
22339 static const struct builtin_description bdesc_pcmpistr[] =
22340 {
22341   /* SSE4.2 */
22342   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
22343   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
22344   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
22345   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
22346   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
22347   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
22348   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
22349 };
22350
22351 /* Special builtins with variable number of arguments.  */
22352 static const struct builtin_description bdesc_special_args[] =
22353 {
22354   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
22355   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
22356
22357   /* MMX */
22358   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
22359
22360   /* 3DNow! */
22361   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
22362
22363   /* SSE */
22364   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
22365   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
22366   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
22367
22368   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
22369   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
22370   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
22371   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
22372
22373   /* SSE or 3DNow!A  */
22374   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
22375   { 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 },
22376
22377   /* SSE2 */
22378   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
22379   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
22380   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
22381   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
22382   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
22383   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
22384   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntsi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
22385   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
22386   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
22387
22388   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
22389   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
22390
22391   /* SSE3 */
22392   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
22393
22394   /* SSE4.1 */
22395   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
22396
22397   /* SSE4A */
22398   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
22399   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
22400
22401   /* AVX */
22402   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
22403   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
22404
22405   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
22406   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
22407   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
22408   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
22409   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
22410
22411   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
22412   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
22413   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
22414   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
22415   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
22416   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
22417   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
22418
22419   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
22420   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
22421   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
22422
22423   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DF },
22424   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SF },
22425   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DF },
22426   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SF },
22427   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DF_V2DF },
22428   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SF_V4SF },
22429   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DF_V4DF },
22430   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SF_V8SF },
22431
22432   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
22433   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
22434   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
22435   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
22436   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
22437   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
22438
22439   /* FSGSBASE */
22440   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
22441   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
22442   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
22443   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
22444   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
22445   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
22446   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
22447   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
22448
22449   /* RDRND */
22450   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandhi, "__builtin_ia32_rdrand16", IX86_BUILTIN_RDRAND16, UNKNOWN, (int) UINT16_FTYPE_VOID },
22451   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandsi, "__builtin_ia32_rdrand32", IX86_BUILTIN_RDRAND32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
22452   { OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT, CODE_FOR_rdranddi, "__builtin_ia32_rdrand64", IX86_BUILTIN_RDRAND64, UNKNOWN, (int) UINT64_FTYPE_VOID },
22453 };
22454
22455 /* Builtins with variable number of arguments.  */
22456 static const struct builtin_description bdesc_args[] =
22457 {
22458   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
22459   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
22460   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
22461   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
22462   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
22463   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
22464   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
22465
22466   /* MMX */
22467   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22468   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22469   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22470   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22471   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22472   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22473
22474   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22475   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22476   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22477   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22478   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22479   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22480   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22481   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22482
22483   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22484   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22485
22486   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22487   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22488   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22489   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22490
22491   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22492   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22493   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22494   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22495   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22496   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22497
22498   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22499   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22500   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22501   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22502   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
22503   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
22504
22505   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
22506   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
22507   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
22508
22509   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
22510
22511   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
22512   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
22513   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
22514   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
22515   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
22516   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
22517
22518   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
22519   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
22520   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
22521   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
22522   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
22523   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
22524
22525   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
22526   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
22527   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
22528   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
22529
22530   /* 3DNow! */
22531   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
22532   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
22533   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
22534   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
22535
22536   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22537   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22538   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22539   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
22540   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
22541   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
22542   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22543   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22544   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22545   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22546   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22547   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22548   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22549   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22550   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22551
22552   /* 3DNow!A */
22553   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
22554   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
22555   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
22556   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
22557   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22558   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
22559
22560   /* SSE */
22561   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
22562   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22563   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22564   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22565   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22566   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22567   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
22568   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
22569   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
22570   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
22571   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
22572   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
22573
22574   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22575
22576   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22577   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22578   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22579   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22580   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22581   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22582   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22583   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22584
22585   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
22586   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
22587   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
22588   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
22589   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
22590   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
22591   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
22592   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
22593   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
22594   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
22595   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
22596   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
22597   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
22598   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
22599   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
22600   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
22601   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
22602   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
22603   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
22604   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
22605   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
22606   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
22607
22608   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22609   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22610   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22611   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22612
22613   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22614   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22615   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22616   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22617
22618   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22619
22620   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22621   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22622   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22623   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22624   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22625
22626   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
22627   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
22628   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
22629
22630   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
22631
22632   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
22633   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
22634   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
22635
22636   /* SSE MMX or 3Dnow!A */
22637   { 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 },
22638   { 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 },
22639   { 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 },
22640
22641   { 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 },
22642   { 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 },
22643   { 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 },
22644   { 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 },
22645
22646   { 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 },
22647   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
22648
22649   { 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 },
22650
22651   /* SSE2 */
22652   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22653
22654   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2df", IX86_BUILTIN_VEC_PERM_V2DF, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DI },
22655   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4sf", IX86_BUILTIN_VEC_PERM_V4SF, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SI },
22656   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di", IX86_BUILTIN_VEC_PERM_V2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_V2DI },
22657   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si", IX86_BUILTIN_VEC_PERM_V4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_V4SI },
22658   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi", IX86_BUILTIN_VEC_PERM_V8HI, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_V8HI },
22659   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi", IX86_BUILTIN_VEC_PERM_V16QI, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
22660   { 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 },
22661   { 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 },
22662   { 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 },
22663   { 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 },
22664   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4df", IX86_BUILTIN_VEC_PERM_V4DF, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DI },
22665   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8sf", IX86_BUILTIN_VEC_PERM_V8SF, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SI },
22666
22667   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
22668   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
22669   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
22670   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
22671   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
22672   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtudq2ps, "__builtin_ia32_cvtudq2ps", IX86_BUILTIN_CVTUDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
22673
22674   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
22675   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
22676   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
22677   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
22678   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
22679
22680   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
22681
22682   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
22683   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
22684   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
22685   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
22686
22687   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
22688   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
22689   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
22690
22691   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22692   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22693   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22694   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22695   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22696   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22697   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22698   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22699
22700   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
22701   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
22702   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
22703   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
22704   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
22705   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
22706   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
22707   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
22708   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
22709   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
22710   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
22711   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
22712   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
22713   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
22714   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
22715   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
22716   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
22717   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
22718   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
22719   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
22720
22721   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22722   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22723   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22724   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22725
22726   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22727   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22728   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22729   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22730
22731   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22732
22733   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22734   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22735   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22736
22737   { 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 },
22738
22739   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22740   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22741   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22742   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22743   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22744   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22745   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22746   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22747
22748   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22749   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22750   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22751   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22752   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22753   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22754   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22755   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22756
22757   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22758   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
22759
22760   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22761   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22762   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22763   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22764
22765   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22766   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22767
22768   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22769   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22770   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
22771   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22772   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22773   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
22774
22775   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22776   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22777   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22778   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22779
22780   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22781   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
22782   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
22783   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22784   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22785   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22786   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22787   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22788
22789   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
22790   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
22791   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
22792
22793   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22794   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
22795
22796   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
22797   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
22798
22799   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
22800
22801   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
22802   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
22803   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
22804   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
22805
22806   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
22807   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
22808   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
22809   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
22810   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
22811   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
22812   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
22813
22814   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
22815   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
22816   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
22817   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
22818   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
22819   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
22820   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
22821
22822   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
22823   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
22824   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
22825   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
22826
22827   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
22828   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
22829   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
22830
22831   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
22832
22833   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
22834   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
22835
22836   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
22837
22838   /* SSE2 MMX */
22839   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
22840   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
22841
22842   /* SSE3 */
22843   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
22844   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
22845
22846   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22847   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22848   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22849   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22850   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
22851   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
22852
22853   /* SSSE3 */
22854   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
22855   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
22856   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
22857   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
22858   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
22859   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
22860
22861   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22862   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22863   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22864   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22865   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22866   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22867   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22868   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22869   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22870   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22871   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22872   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22873   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
22874   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
22875   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22876   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22877   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22878   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22879   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22880   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
22881   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22882   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
22883   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22884   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
22885
22886   /* SSSE3.  */
22887   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
22888   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
22889
22890   /* SSE4.1 */
22891   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22892   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22893   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
22894   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
22895   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22896   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22897   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22898   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
22899   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
22900   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
22901
22902   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
22903   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
22904   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
22905   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
22906   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
22907   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
22908   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
22909   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
22910   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
22911   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
22912   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
22913   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
22914   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
22915
22916   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
22917   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22918   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22919   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22920   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22921   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22922   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
22923   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22924   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22925   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
22926   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
22927   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
22928
22929   /* SSE4.1 */
22930   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
22931   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
22932   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
22933   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
22934
22935   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22936   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22937   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
22938
22939   /* SSE4.2 */
22940   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22941   { 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 },
22942   { 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 },
22943   { 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 },
22944   { 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 },
22945
22946   /* SSE4A */
22947   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
22948   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
22949   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
22950   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22951
22952   /* AES */
22953   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
22954   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
22955
22956   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22957   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22958   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22959   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
22960
22961   /* PCLMUL */
22962   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
22963
22964   /* AVX */
22965   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22966   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22967   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22968   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22969   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22970   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22971   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22972   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22973   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22974   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22975   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22976   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22977   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22978   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22979   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22980   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22981   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22982   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22983   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22984   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22985   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22986   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22987   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22988   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22989   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
22990   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
22991
22992   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
22993   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
22994   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
22995   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
22996
22997   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
22998   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
22999   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
23000   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
23001   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23002   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23003   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23004   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpsdv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23005   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpssv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23006   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23007   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23008   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23009   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23010   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
23011   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
23012   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
23013   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2pd256, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
23014   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2ps256, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
23015   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
23016   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
23017   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
23018   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttpd2dq256, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
23019   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
23020   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttps2dq256, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
23021   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23022   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23023   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
23024   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
23025   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
23026   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
23027   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
23028   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
23029   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
23030   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
23031
23032   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23033   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23034   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
23035
23036   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
23037   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23038   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23039   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23040   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23041
23042   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23043
23044   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
23045   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
23046
23047   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23048   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23049   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23050   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23051
23052   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
23053   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
23054   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
23055   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
23056   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
23057   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
23058
23059   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23060   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23061   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23062   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23063   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23064   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23065   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23066   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23067   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23068   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23069   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23070   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23071   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23072   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23073   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23074
23075   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
23076   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
23077
23078   { OPTION_MASK_ISA_ABM, CODE_FOR_clzhi2_abm,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
23079
23080   /* F16C */
23081   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
23082   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
23083   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
23084   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
23085 };
23086
23087 /* FMA4 and XOP.  */
23088 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
23089 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
23090 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
23091 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
23092 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
23093 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
23094 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
23095 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
23096 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
23097 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
23098 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
23099 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
23100 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
23101 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
23102 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
23103 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
23104 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
23105 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
23106 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
23107 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
23108 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
23109 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
23110 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
23111 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
23112 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
23113 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
23114 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
23115 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
23116 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
23117 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
23118 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
23119 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
23120 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
23121 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
23122 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
23123 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
23124 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
23125 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
23126 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
23127 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
23128 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
23129 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
23130 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
23131 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
23132 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
23133 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
23134 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
23135 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
23136 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
23137 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
23138 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
23139 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
23140
23141 static const struct builtin_description bdesc_multi_arg[] =
23142 {
23143   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmaddv4sf4,     "__builtin_ia32_vfmaddss",    IX86_BUILTIN_VFMADDSS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23144   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmaddv2df4,     "__builtin_ia32_vfmaddsd",    IX86_BUILTIN_VFMADDSD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23145   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv4sf4,       "__builtin_ia32_vfmaddps",    IX86_BUILTIN_VFMADDPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23146   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv2df4,       "__builtin_ia32_vfmaddpd",    IX86_BUILTIN_VFMADDPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23147   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmsubv4sf4,     "__builtin_ia32_vfmsubss",    IX86_BUILTIN_VFMSUBSS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23148   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmsubv2df4,     "__builtin_ia32_vfmsubsd",    IX86_BUILTIN_VFMSUBSD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23149   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv4sf4,       "__builtin_ia32_vfmsubps",    IX86_BUILTIN_VFMSUBPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23150   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv2df4,       "__builtin_ia32_vfmsubpd",    IX86_BUILTIN_VFMSUBPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23151
23152   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmaddv4sf4,    "__builtin_ia32_vfnmaddss",   IX86_BUILTIN_VFNMADDSS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
23153   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmaddv2df4,    "__builtin_ia32_vfnmaddsd",   IX86_BUILTIN_VFNMADDSD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
23154   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv4sf4,      "__builtin_ia32_vfnmaddps",   IX86_BUILTIN_VFNMADDPS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
23155   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv2df4,      "__builtin_ia32_vfnmaddpd",   IX86_BUILTIN_VFNMADDPD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
23156   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmsubv4sf4,    "__builtin_ia32_vfnmsubss",   IX86_BUILTIN_VFNMSUBSS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
23157   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfnmsubv2df4,    "__builtin_ia32_vfnmsubsd",   IX86_BUILTIN_VFNMSUBSD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
23158   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv4sf4,      "__builtin_ia32_vfnmsubps",   IX86_BUILTIN_VFNMSUBPS,   UNKNOWN,      (int)MULTI_ARG_3_SF },
23159   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv2df4,      "__builtin_ia32_vfnmsubpd",   IX86_BUILTIN_VFNMSUBPD,   UNKNOWN,      (int)MULTI_ARG_3_DF },
23160
23161   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv4sf4,    "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23162   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv2df4,    "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23163   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv4sf4,    "__builtin_ia32_vfmsubaddps", IX86_BUILTIN_VFMSUBADDPS,    UNKNOWN,      (int)MULTI_ARG_3_SF },
23164   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv2df4,    "__builtin_ia32_vfmsubaddpd", IX86_BUILTIN_VFMSUBADDPD,    UNKNOWN,      (int)MULTI_ARG_3_DF },
23165
23166   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv8sf4256,       "__builtin_ia32_vfmaddps256",    IX86_BUILTIN_VFMADDPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23167   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddv4df4256,       "__builtin_ia32_vfmaddpd256",    IX86_BUILTIN_VFMADDPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23168   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv8sf4256,       "__builtin_ia32_vfmsubps256",    IX86_BUILTIN_VFMSUBPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23169   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubv4df4256,       "__builtin_ia32_vfmsubpd256",    IX86_BUILTIN_VFMSUBPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23170
23171   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv8sf4256,      "__builtin_ia32_vfnmaddps256",   IX86_BUILTIN_VFNMADDPS256,   UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23172   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmaddv4df4256,      "__builtin_ia32_vfnmaddpd256",   IX86_BUILTIN_VFNMADDPD256,   UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23173   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv8sf4256,      "__builtin_ia32_vfnmsubps256",   IX86_BUILTIN_VFNMSUBPS256,   UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23174   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fnmsubv4df4256,      "__builtin_ia32_vfnmsubpd256",   IX86_BUILTIN_VFNMSUBPD256,   UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23175
23176   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv8sf4,    "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23177   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmaddsubv4df4,    "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23178   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv8sf4,    "__builtin_ia32_vfmsubaddps256", IX86_BUILTIN_VFMSUBADDPS256,    UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23179   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmsubaddv4df4,    "__builtin_ia32_vfmsubaddpd256", IX86_BUILTIN_VFMSUBADDPD256,    UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23180
23181   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
23182   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
23183   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
23184   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
23185   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
23186   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
23187   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
23188
23189   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
23190   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
23191   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
23192   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
23193   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
23194   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23195   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23196
23197   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
23198
23199   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
23200   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
23201   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23202   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23203   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
23204   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
23205   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23206   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23207   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23208   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23209   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23210   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23211
23212   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
23213   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
23214   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
23215   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
23216   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
23217   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
23218   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
23219   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
23220   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
23221   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
23222   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
23223   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
23224   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
23225   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
23226   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
23227   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
23228
23229   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
23230   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
23231   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
23232   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
23233   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2256,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
23234   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2256,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
23235
23236   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
23237   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
23238   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
23239   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
23240   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
23241   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
23242   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
23243   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
23244   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
23245   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
23246   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
23247   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
23248   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
23249   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
23250   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
23251
23252   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
23253   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
23254   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
23255   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
23256   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
23257   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
23258   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
23259
23260   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
23261   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
23262   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
23263   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
23264   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
23265   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
23266   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
23267
23268   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
23269   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
23270   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
23271   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
23272   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
23273   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
23274   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
23275
23276   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
23277   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
23278   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
23279   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
23280   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
23281   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
23282   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
23283
23284   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
23285   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
23286   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
23287   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
23288   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
23289   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
23290   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
23291
23292   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
23293   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
23294   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
23295   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
23296   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
23297   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
23298   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
23299
23300   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
23301   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
23302   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
23303   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
23304   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
23305   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
23306   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
23307
23308   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
23309   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
23310   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
23311   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
23312   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
23313   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
23314   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
23315
23316   { 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 },
23317   { 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 },
23318   { 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 },
23319   { 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 },
23320   { 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 },
23321   { 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 },
23322   { 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 },
23323   { 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 },
23324
23325   { 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 },
23326   { 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 },
23327   { 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 },
23328   { 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 },
23329   { 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 },
23330   { 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 },
23331   { 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 },
23332   { 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 },
23333
23334   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
23335   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
23336   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
23337   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
23338
23339 };
23340
23341 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
23342    in the current target ISA to allow the user to compile particular modules
23343    with different target specific options that differ from the command line
23344    options.  */
23345 static void
23346 ix86_init_mmx_sse_builtins (void)
23347 {
23348   const struct builtin_description * d;
23349   enum ix86_builtin_func_type ftype;
23350   size_t i;
23351
23352   /* Add all special builtins with variable number of operands.  */
23353   for (i = 0, d = bdesc_special_args;
23354        i < ARRAY_SIZE (bdesc_special_args);
23355        i++, d++)
23356     {
23357       if (d->name == 0)
23358         continue;
23359
23360       ftype = (enum ix86_builtin_func_type) d->flag;
23361       def_builtin (d->mask, d->name, ftype, d->code);
23362     }
23363
23364   /* Add all builtins with variable number of operands.  */
23365   for (i = 0, d = bdesc_args;
23366        i < ARRAY_SIZE (bdesc_args);
23367        i++, d++)
23368     {
23369       if (d->name == 0)
23370         continue;
23371
23372       ftype = (enum ix86_builtin_func_type) d->flag;
23373       def_builtin_const (d->mask, d->name, ftype, d->code);
23374     }
23375
23376   /* pcmpestr[im] insns.  */
23377   for (i = 0, d = bdesc_pcmpestr;
23378        i < ARRAY_SIZE (bdesc_pcmpestr);
23379        i++, d++)
23380     {
23381       if (d->code == IX86_BUILTIN_PCMPESTRM128)
23382         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
23383       else
23384         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
23385       def_builtin_const (d->mask, d->name, ftype, d->code);
23386     }
23387
23388   /* pcmpistr[im] insns.  */
23389   for (i = 0, d = bdesc_pcmpistr;
23390        i < ARRAY_SIZE (bdesc_pcmpistr);
23391        i++, d++)
23392     {
23393       if (d->code == IX86_BUILTIN_PCMPISTRM128)
23394         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
23395       else
23396         ftype = INT_FTYPE_V16QI_V16QI_INT;
23397       def_builtin_const (d->mask, d->name, ftype, d->code);
23398     }
23399
23400   /* comi/ucomi insns.  */
23401   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
23402     {
23403       if (d->mask == OPTION_MASK_ISA_SSE2)
23404         ftype = INT_FTYPE_V2DF_V2DF;
23405       else
23406         ftype = INT_FTYPE_V4SF_V4SF;
23407       def_builtin_const (d->mask, d->name, ftype, d->code);
23408     }
23409
23410   /* SSE */
23411   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
23412                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
23413   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
23414                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
23415
23416   /* SSE or 3DNow!A */
23417   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
23418                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
23419                IX86_BUILTIN_MASKMOVQ);
23420
23421   /* SSE2 */
23422   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
23423                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
23424
23425   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
23426                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
23427   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
23428                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
23429
23430   /* SSE3.  */
23431   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
23432                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
23433   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
23434                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
23435
23436   /* AES */
23437   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
23438                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
23439   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
23440                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
23441   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
23442                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
23443   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
23444                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
23445   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
23446                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
23447   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
23448                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
23449
23450   /* PCLMUL */
23451   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
23452                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
23453
23454   /* MMX access to the vec_init patterns.  */
23455   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
23456                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
23457
23458   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
23459                      V4HI_FTYPE_HI_HI_HI_HI,
23460                      IX86_BUILTIN_VEC_INIT_V4HI);
23461
23462   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
23463                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
23464                      IX86_BUILTIN_VEC_INIT_V8QI);
23465
23466   /* Access to the vec_extract patterns.  */
23467   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
23468                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
23469   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
23470                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
23471   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
23472                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
23473   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
23474                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
23475   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
23476                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
23477
23478   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
23479                      "__builtin_ia32_vec_ext_v4hi",
23480                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
23481
23482   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
23483                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
23484
23485   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
23486                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
23487
23488   /* Access to the vec_set patterns.  */
23489   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
23490                      "__builtin_ia32_vec_set_v2di",
23491                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
23492
23493   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
23494                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
23495
23496   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
23497                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
23498
23499   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
23500                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
23501
23502   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
23503                      "__builtin_ia32_vec_set_v4hi",
23504                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
23505
23506   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
23507                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
23508
23509   /* Add FMA4 multi-arg argument instructions */
23510   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
23511     {
23512       if (d->name == 0)
23513         continue;
23514
23515       ftype = (enum ix86_builtin_func_type) d->flag;
23516       def_builtin_const (d->mask, d->name, ftype, d->code);
23517     }
23518 }
23519
23520 /* Internal method for ix86_init_builtins.  */
23521
23522 static void
23523 ix86_init_builtins_va_builtins_abi (void)
23524 {
23525   tree ms_va_ref, sysv_va_ref;
23526   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
23527   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
23528   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
23529   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
23530
23531   if (!TARGET_64BIT)
23532     return;
23533   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
23534   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
23535   ms_va_ref = build_reference_type (ms_va_list_type_node);
23536   sysv_va_ref =
23537     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
23538
23539   fnvoid_va_end_ms =
23540     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
23541   fnvoid_va_start_ms =
23542     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
23543   fnvoid_va_end_sysv =
23544     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
23545   fnvoid_va_start_sysv =
23546     build_varargs_function_type_list (void_type_node, sysv_va_ref,
23547                                        NULL_TREE);
23548   fnvoid_va_copy_ms =
23549     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
23550                               NULL_TREE);
23551   fnvoid_va_copy_sysv =
23552     build_function_type_list (void_type_node, sysv_va_ref,
23553                               sysv_va_ref, NULL_TREE);
23554
23555   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
23556                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
23557   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
23558                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
23559   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
23560                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
23561   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
23562                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
23563   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
23564                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
23565   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
23566                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
23567 }
23568
23569 static void
23570 ix86_init_builtin_types (void)
23571 {
23572   tree float128_type_node, float80_type_node;
23573
23574   /* The __float80 type.  */
23575   float80_type_node = long_double_type_node;
23576   if (TYPE_MODE (float80_type_node) != XFmode)
23577     {
23578       /* The __float80 type.  */
23579       float80_type_node = make_node (REAL_TYPE);
23580
23581       TYPE_PRECISION (float80_type_node) = 80;
23582       layout_type (float80_type_node);
23583     }
23584   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
23585
23586   /* The __float128 type.  */
23587   float128_type_node = make_node (REAL_TYPE);
23588   TYPE_PRECISION (float128_type_node) = 128;
23589   layout_type (float128_type_node);
23590   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
23591
23592   /* This macro is built by i386-builtin-types.awk.  */
23593   DEFINE_BUILTIN_PRIMITIVE_TYPES;
23594 }
23595
23596 static void
23597 ix86_init_builtins (void)
23598 {
23599   tree t;
23600
23601   ix86_init_builtin_types ();
23602
23603   /* TFmode support builtins.  */
23604   def_builtin_const (0, "__builtin_infq",
23605                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
23606   def_builtin_const (0, "__builtin_huge_valq",
23607                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
23608
23609   /* We will expand them to normal call if SSE2 isn't available since
23610      they are used by libgcc. */
23611   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
23612   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
23613                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
23614   TREE_READONLY (t) = 1;
23615   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
23616
23617   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
23618   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
23619                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
23620   TREE_READONLY (t) = 1;
23621   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
23622
23623   ix86_init_mmx_sse_builtins ();
23624
23625   if (TARGET_64BIT)
23626     ix86_init_builtins_va_builtins_abi ();
23627 }
23628
23629 /* Return the ix86 builtin for CODE.  */
23630
23631 static tree
23632 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
23633 {
23634   if (code >= IX86_BUILTIN_MAX)
23635     return error_mark_node;
23636
23637   return ix86_builtins[code];
23638 }
23639
23640 /* Errors in the source file can cause expand_expr to return const0_rtx
23641    where we expect a vector.  To avoid crashing, use one of the vector
23642    clear instructions.  */
23643 static rtx
23644 safe_vector_operand (rtx x, enum machine_mode mode)
23645 {
23646   if (x == const0_rtx)
23647     x = CONST0_RTX (mode);
23648   return x;
23649 }
23650
23651 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
23652
23653 static rtx
23654 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
23655 {
23656   rtx pat;
23657   tree arg0 = CALL_EXPR_ARG (exp, 0);
23658   tree arg1 = CALL_EXPR_ARG (exp, 1);
23659   rtx op0 = expand_normal (arg0);
23660   rtx op1 = expand_normal (arg1);
23661   enum machine_mode tmode = insn_data[icode].operand[0].mode;
23662   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
23663   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
23664
23665   if (VECTOR_MODE_P (mode0))
23666     op0 = safe_vector_operand (op0, mode0);
23667   if (VECTOR_MODE_P (mode1))
23668     op1 = safe_vector_operand (op1, mode1);
23669
23670   if (optimize || !target
23671       || GET_MODE (target) != tmode
23672       || !insn_data[icode].operand[0].predicate (target, tmode))
23673     target = gen_reg_rtx (tmode);
23674
23675   if (GET_MODE (op1) == SImode && mode1 == TImode)
23676     {
23677       rtx x = gen_reg_rtx (V4SImode);
23678       emit_insn (gen_sse2_loadd (x, op1));
23679       op1 = gen_lowpart (TImode, x);
23680     }
23681
23682   if (!insn_data[icode].operand[1].predicate (op0, mode0))
23683     op0 = copy_to_mode_reg (mode0, op0);
23684   if (!insn_data[icode].operand[2].predicate (op1, mode1))
23685     op1 = copy_to_mode_reg (mode1, op1);
23686
23687   pat = GEN_FCN (icode) (target, op0, op1);
23688   if (! pat)
23689     return 0;
23690
23691   emit_insn (pat);
23692
23693   return target;
23694 }
23695
23696 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
23697
23698 static rtx
23699 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
23700                                enum ix86_builtin_func_type m_type,
23701                                enum rtx_code sub_code)
23702 {
23703   rtx pat;
23704   int i;
23705   int nargs;
23706   bool comparison_p = false;
23707   bool tf_p = false;
23708   bool last_arg_constant = false;
23709   int num_memory = 0;
23710   struct {
23711     rtx op;
23712     enum machine_mode mode;
23713   } args[4];
23714
23715   enum machine_mode tmode = insn_data[icode].operand[0].mode;
23716
23717   switch (m_type)
23718     {
23719     case MULTI_ARG_4_DF2_DI_I:
23720     case MULTI_ARG_4_DF2_DI_I1:
23721     case MULTI_ARG_4_SF2_SI_I:
23722     case MULTI_ARG_4_SF2_SI_I1:
23723       nargs = 4;
23724       last_arg_constant = true;
23725       break;
23726
23727     case MULTI_ARG_3_SF:
23728     case MULTI_ARG_3_DF:
23729     case MULTI_ARG_3_SF2:
23730     case MULTI_ARG_3_DF2:
23731     case MULTI_ARG_3_DI:
23732     case MULTI_ARG_3_SI:
23733     case MULTI_ARG_3_SI_DI:
23734     case MULTI_ARG_3_HI:
23735     case MULTI_ARG_3_HI_SI:
23736     case MULTI_ARG_3_QI:
23737     case MULTI_ARG_3_DI2:
23738     case MULTI_ARG_3_SI2:
23739     case MULTI_ARG_3_HI2:
23740     case MULTI_ARG_3_QI2:
23741       nargs = 3;
23742       break;
23743
23744     case MULTI_ARG_2_SF:
23745     case MULTI_ARG_2_DF:
23746     case MULTI_ARG_2_DI:
23747     case MULTI_ARG_2_SI:
23748     case MULTI_ARG_2_HI:
23749     case MULTI_ARG_2_QI:
23750       nargs = 2;
23751       break;
23752
23753     case MULTI_ARG_2_DI_IMM:
23754     case MULTI_ARG_2_SI_IMM:
23755     case MULTI_ARG_2_HI_IMM:
23756     case MULTI_ARG_2_QI_IMM:
23757       nargs = 2;
23758       last_arg_constant = true;
23759       break;
23760
23761     case MULTI_ARG_1_SF:
23762     case MULTI_ARG_1_DF:
23763     case MULTI_ARG_1_SF2:
23764     case MULTI_ARG_1_DF2:
23765     case MULTI_ARG_1_DI:
23766     case MULTI_ARG_1_SI:
23767     case MULTI_ARG_1_HI:
23768     case MULTI_ARG_1_QI:
23769     case MULTI_ARG_1_SI_DI:
23770     case MULTI_ARG_1_HI_DI:
23771     case MULTI_ARG_1_HI_SI:
23772     case MULTI_ARG_1_QI_DI:
23773     case MULTI_ARG_1_QI_SI:
23774     case MULTI_ARG_1_QI_HI:
23775       nargs = 1;
23776       break;
23777
23778     case MULTI_ARG_2_DI_CMP:
23779     case MULTI_ARG_2_SI_CMP:
23780     case MULTI_ARG_2_HI_CMP:
23781     case MULTI_ARG_2_QI_CMP:
23782       nargs = 2;
23783       comparison_p = true;
23784       break;
23785
23786     case MULTI_ARG_2_SF_TF:
23787     case MULTI_ARG_2_DF_TF:
23788     case MULTI_ARG_2_DI_TF:
23789     case MULTI_ARG_2_SI_TF:
23790     case MULTI_ARG_2_HI_TF:
23791     case MULTI_ARG_2_QI_TF:
23792       nargs = 2;
23793       tf_p = true;
23794       break;
23795
23796     default:
23797       gcc_unreachable ();
23798     }
23799
23800   if (optimize || !target
23801       || GET_MODE (target) != tmode
23802       || !insn_data[icode].operand[0].predicate (target, tmode))
23803     target = gen_reg_rtx (tmode);
23804
23805   gcc_assert (nargs <= 4);
23806
23807   for (i = 0; i < nargs; i++)
23808     {
23809       tree arg = CALL_EXPR_ARG (exp, i);
23810       rtx op = expand_normal (arg);
23811       int adjust = (comparison_p) ? 1 : 0;
23812       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
23813
23814       if (last_arg_constant && i == nargs-1)
23815         {
23816           if (!CONST_INT_P (op))
23817             {
23818               error ("last argument must be an immediate");
23819               return gen_reg_rtx (tmode);
23820             }
23821         }
23822       else
23823         {
23824           if (VECTOR_MODE_P (mode))
23825             op = safe_vector_operand (op, mode);
23826
23827           /* If we aren't optimizing, only allow one memory operand to be
23828              generated.  */
23829           if (memory_operand (op, mode))
23830             num_memory++;
23831
23832           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
23833
23834           if (optimize
23835               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
23836               || num_memory > 1)
23837             op = force_reg (mode, op);
23838         }
23839
23840       args[i].op = op;
23841       args[i].mode = mode;
23842     }
23843
23844   switch (nargs)
23845     {
23846     case 1:
23847       pat = GEN_FCN (icode) (target, args[0].op);
23848       break;
23849
23850     case 2:
23851       if (tf_p)
23852         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
23853                                GEN_INT ((int)sub_code));
23854       else if (! comparison_p)
23855         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
23856       else
23857         {
23858           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
23859                                        args[0].op,
23860                                        args[1].op);
23861
23862           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
23863         }
23864       break;
23865
23866     case 3:
23867       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
23868       break;
23869
23870     case 4:
23871       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
23872       break;
23873
23874     default:
23875       gcc_unreachable ();
23876     }
23877
23878   if (! pat)
23879     return 0;
23880
23881   emit_insn (pat);
23882   return target;
23883 }
23884
23885 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
23886    insns with vec_merge.  */
23887
23888 static rtx
23889 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
23890                                     rtx target)
23891 {
23892   rtx pat;
23893   tree arg0 = CALL_EXPR_ARG (exp, 0);
23894   rtx op1, op0 = expand_normal (arg0);
23895   enum machine_mode tmode = insn_data[icode].operand[0].mode;
23896   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
23897
23898   if (optimize || !target
23899       || GET_MODE (target) != tmode
23900       || !insn_data[icode].operand[0].predicate (target, tmode))
23901     target = gen_reg_rtx (tmode);
23902
23903   if (VECTOR_MODE_P (mode0))
23904     op0 = safe_vector_operand (op0, mode0);
23905
23906   if ((optimize && !register_operand (op0, mode0))
23907       || !insn_data[icode].operand[1].predicate (op0, mode0))
23908     op0 = copy_to_mode_reg (mode0, op0);
23909
23910   op1 = op0;
23911   if (!insn_data[icode].operand[2].predicate (op1, mode0))
23912     op1 = copy_to_mode_reg (mode0, op1);
23913
23914   pat = GEN_FCN (icode) (target, op0, op1);
23915   if (! pat)
23916     return 0;
23917   emit_insn (pat);
23918   return target;
23919 }
23920
23921 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
23922
23923 static rtx
23924 ix86_expand_sse_compare (const struct builtin_description *d,
23925                          tree exp, rtx target, bool swap)
23926 {
23927   rtx pat;
23928   tree arg0 = CALL_EXPR_ARG (exp, 0);
23929   tree arg1 = CALL_EXPR_ARG (exp, 1);
23930   rtx op0 = expand_normal (arg0);
23931   rtx op1 = expand_normal (arg1);
23932   rtx op2;
23933   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
23934   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
23935   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
23936   enum rtx_code comparison = d->comparison;
23937
23938   if (VECTOR_MODE_P (mode0))
23939     op0 = safe_vector_operand (op0, mode0);
23940   if (VECTOR_MODE_P (mode1))
23941     op1 = safe_vector_operand (op1, mode1);
23942
23943   /* Swap operands if we have a comparison that isn't available in
23944      hardware.  */
23945   if (swap)
23946     {
23947       rtx tmp = gen_reg_rtx (mode1);
23948       emit_move_insn (tmp, op1);
23949       op1 = op0;
23950       op0 = tmp;
23951     }
23952
23953   if (optimize || !target
23954       || GET_MODE (target) != tmode
23955       || !insn_data[d->icode].operand[0].predicate (target, tmode))
23956     target = gen_reg_rtx (tmode);
23957
23958   if ((optimize && !register_operand (op0, mode0))
23959       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
23960     op0 = copy_to_mode_reg (mode0, op0);
23961   if ((optimize && !register_operand (op1, mode1))
23962       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
23963     op1 = copy_to_mode_reg (mode1, op1);
23964
23965   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
23966   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
23967   if (! pat)
23968     return 0;
23969   emit_insn (pat);
23970   return target;
23971 }
23972
23973 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
23974
23975 static rtx
23976 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
23977                       rtx target)
23978 {
23979   rtx pat;
23980   tree arg0 = CALL_EXPR_ARG (exp, 0);
23981   tree arg1 = CALL_EXPR_ARG (exp, 1);
23982   rtx op0 = expand_normal (arg0);
23983   rtx op1 = expand_normal (arg1);
23984   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
23985   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
23986   enum rtx_code comparison = d->comparison;
23987
23988   if (VECTOR_MODE_P (mode0))
23989     op0 = safe_vector_operand (op0, mode0);
23990   if (VECTOR_MODE_P (mode1))
23991     op1 = safe_vector_operand (op1, mode1);
23992
23993   /* Swap operands if we have a comparison that isn't available in
23994      hardware.  */
23995   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
23996     {
23997       rtx tmp = op1;
23998       op1 = op0;
23999       op0 = tmp;
24000     }
24001
24002   target = gen_reg_rtx (SImode);
24003   emit_move_insn (target, const0_rtx);
24004   target = gen_rtx_SUBREG (QImode, target, 0);
24005
24006   if ((optimize && !register_operand (op0, mode0))
24007       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
24008     op0 = copy_to_mode_reg (mode0, op0);
24009   if ((optimize && !register_operand (op1, mode1))
24010       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
24011     op1 = copy_to_mode_reg (mode1, op1);
24012
24013   pat = GEN_FCN (d->icode) (op0, op1);
24014   if (! pat)
24015     return 0;
24016   emit_insn (pat);
24017   emit_insn (gen_rtx_SET (VOIDmode,
24018                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24019                           gen_rtx_fmt_ee (comparison, QImode,
24020                                           SET_DEST (pat),
24021                                           const0_rtx)));
24022
24023   return SUBREG_REG (target);
24024 }
24025
24026 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
24027
24028 static rtx
24029 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
24030                        rtx target)
24031 {
24032   rtx pat;
24033   tree arg0 = CALL_EXPR_ARG (exp, 0);
24034   tree arg1 = CALL_EXPR_ARG (exp, 1);
24035   rtx op0 = expand_normal (arg0);
24036   rtx op1 = expand_normal (arg1);
24037   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
24038   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
24039   enum rtx_code comparison = d->comparison;
24040
24041   if (VECTOR_MODE_P (mode0))
24042     op0 = safe_vector_operand (op0, mode0);
24043   if (VECTOR_MODE_P (mode1))
24044     op1 = safe_vector_operand (op1, mode1);
24045
24046   target = gen_reg_rtx (SImode);
24047   emit_move_insn (target, const0_rtx);
24048   target = gen_rtx_SUBREG (QImode, target, 0);
24049
24050   if ((optimize && !register_operand (op0, mode0))
24051       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
24052     op0 = copy_to_mode_reg (mode0, op0);
24053   if ((optimize && !register_operand (op1, mode1))
24054       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
24055     op1 = copy_to_mode_reg (mode1, op1);
24056
24057   pat = GEN_FCN (d->icode) (op0, op1);
24058   if (! pat)
24059     return 0;
24060   emit_insn (pat);
24061   emit_insn (gen_rtx_SET (VOIDmode,
24062                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24063                           gen_rtx_fmt_ee (comparison, QImode,
24064                                           SET_DEST (pat),
24065                                           const0_rtx)));
24066
24067   return SUBREG_REG (target);
24068 }
24069
24070 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
24071
24072 static rtx
24073 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
24074                           tree exp, rtx target)
24075 {
24076   rtx pat;
24077   tree arg0 = CALL_EXPR_ARG (exp, 0);
24078   tree arg1 = CALL_EXPR_ARG (exp, 1);
24079   tree arg2 = CALL_EXPR_ARG (exp, 2);
24080   tree arg3 = CALL_EXPR_ARG (exp, 3);
24081   tree arg4 = CALL_EXPR_ARG (exp, 4);
24082   rtx scratch0, scratch1;
24083   rtx op0 = expand_normal (arg0);
24084   rtx op1 = expand_normal (arg1);
24085   rtx op2 = expand_normal (arg2);
24086   rtx op3 = expand_normal (arg3);
24087   rtx op4 = expand_normal (arg4);
24088   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
24089
24090   tmode0 = insn_data[d->icode].operand[0].mode;
24091   tmode1 = insn_data[d->icode].operand[1].mode;
24092   modev2 = insn_data[d->icode].operand[2].mode;
24093   modei3 = insn_data[d->icode].operand[3].mode;
24094   modev4 = insn_data[d->icode].operand[4].mode;
24095   modei5 = insn_data[d->icode].operand[5].mode;
24096   modeimm = insn_data[d->icode].operand[6].mode;
24097
24098   if (VECTOR_MODE_P (modev2))
24099     op0 = safe_vector_operand (op0, modev2);
24100   if (VECTOR_MODE_P (modev4))
24101     op2 = safe_vector_operand (op2, modev4);
24102
24103   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
24104     op0 = copy_to_mode_reg (modev2, op0);
24105   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
24106     op1 = copy_to_mode_reg (modei3, op1);
24107   if ((optimize && !register_operand (op2, modev4))
24108       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
24109     op2 = copy_to_mode_reg (modev4, op2);
24110   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
24111     op3 = copy_to_mode_reg (modei5, op3);
24112
24113   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
24114     {
24115       error ("the fifth argument must be a 8-bit immediate");
24116       return const0_rtx;
24117     }
24118
24119   if (d->code == IX86_BUILTIN_PCMPESTRI128)
24120     {
24121       if (optimize || !target
24122           || GET_MODE (target) != tmode0
24123           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
24124         target = gen_reg_rtx (tmode0);
24125
24126       scratch1 = gen_reg_rtx (tmode1);
24127
24128       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
24129     }
24130   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
24131     {
24132       if (optimize || !target
24133           || GET_MODE (target) != tmode1
24134           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
24135         target = gen_reg_rtx (tmode1);
24136
24137       scratch0 = gen_reg_rtx (tmode0);
24138
24139       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
24140     }
24141   else
24142     {
24143       gcc_assert (d->flag);
24144
24145       scratch0 = gen_reg_rtx (tmode0);
24146       scratch1 = gen_reg_rtx (tmode1);
24147
24148       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
24149     }
24150
24151   if (! pat)
24152     return 0;
24153
24154   emit_insn (pat);
24155
24156   if (d->flag)
24157     {
24158       target = gen_reg_rtx (SImode);
24159       emit_move_insn (target, const0_rtx);
24160       target = gen_rtx_SUBREG (QImode, target, 0);
24161
24162       emit_insn
24163         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24164                       gen_rtx_fmt_ee (EQ, QImode,
24165                                       gen_rtx_REG ((enum machine_mode) d->flag,
24166                                                    FLAGS_REG),
24167                                       const0_rtx)));
24168       return SUBREG_REG (target);
24169     }
24170   else
24171     return target;
24172 }
24173
24174
24175 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
24176
24177 static rtx
24178 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
24179                           tree exp, rtx target)
24180 {
24181   rtx pat;
24182   tree arg0 = CALL_EXPR_ARG (exp, 0);
24183   tree arg1 = CALL_EXPR_ARG (exp, 1);
24184   tree arg2 = CALL_EXPR_ARG (exp, 2);
24185   rtx scratch0, scratch1;
24186   rtx op0 = expand_normal (arg0);
24187   rtx op1 = expand_normal (arg1);
24188   rtx op2 = expand_normal (arg2);
24189   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
24190
24191   tmode0 = insn_data[d->icode].operand[0].mode;
24192   tmode1 = insn_data[d->icode].operand[1].mode;
24193   modev2 = insn_data[d->icode].operand[2].mode;
24194   modev3 = insn_data[d->icode].operand[3].mode;
24195   modeimm = insn_data[d->icode].operand[4].mode;
24196
24197   if (VECTOR_MODE_P (modev2))
24198     op0 = safe_vector_operand (op0, modev2);
24199   if (VECTOR_MODE_P (modev3))
24200     op1 = safe_vector_operand (op1, modev3);
24201
24202   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
24203     op0 = copy_to_mode_reg (modev2, op0);
24204   if ((optimize && !register_operand (op1, modev3))
24205       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
24206     op1 = copy_to_mode_reg (modev3, op1);
24207
24208   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
24209     {
24210       error ("the third argument must be a 8-bit immediate");
24211       return const0_rtx;
24212     }
24213
24214   if (d->code == IX86_BUILTIN_PCMPISTRI128)
24215     {
24216       if (optimize || !target
24217           || GET_MODE (target) != tmode0
24218           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
24219         target = gen_reg_rtx (tmode0);
24220
24221       scratch1 = gen_reg_rtx (tmode1);
24222
24223       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
24224     }
24225   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
24226     {
24227       if (optimize || !target
24228           || GET_MODE (target) != tmode1
24229           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
24230         target = gen_reg_rtx (tmode1);
24231
24232       scratch0 = gen_reg_rtx (tmode0);
24233
24234       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
24235     }
24236   else
24237     {
24238       gcc_assert (d->flag);
24239
24240       scratch0 = gen_reg_rtx (tmode0);
24241       scratch1 = gen_reg_rtx (tmode1);
24242
24243       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
24244     }
24245
24246   if (! pat)
24247     return 0;
24248
24249   emit_insn (pat);
24250
24251   if (d->flag)
24252     {
24253       target = gen_reg_rtx (SImode);
24254       emit_move_insn (target, const0_rtx);
24255       target = gen_rtx_SUBREG (QImode, target, 0);
24256
24257       emit_insn
24258         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24259                       gen_rtx_fmt_ee (EQ, QImode,
24260                                       gen_rtx_REG ((enum machine_mode) d->flag,
24261                                                    FLAGS_REG),
24262                                       const0_rtx)));
24263       return SUBREG_REG (target);
24264     }
24265   else
24266     return target;
24267 }
24268
24269 /* Subroutine of ix86_expand_builtin to take care of insns with
24270    variable number of operands.  */
24271
24272 static rtx
24273 ix86_expand_args_builtin (const struct builtin_description *d,
24274                           tree exp, rtx target)
24275 {
24276   rtx pat, real_target;
24277   unsigned int i, nargs;
24278   unsigned int nargs_constant = 0;
24279   int num_memory = 0;
24280   struct
24281     {
24282       rtx op;
24283       enum machine_mode mode;
24284     } args[4];
24285   bool last_arg_count = false;
24286   enum insn_code icode = d->icode;
24287   const struct insn_data_d *insn_p = &insn_data[icode];
24288   enum machine_mode tmode = insn_p->operand[0].mode;
24289   enum machine_mode rmode = VOIDmode;
24290   bool swap = false;
24291   enum rtx_code comparison = d->comparison;
24292
24293   switch ((enum ix86_builtin_func_type) d->flag)
24294     {
24295     case INT_FTYPE_V8SF_V8SF_PTEST:
24296     case INT_FTYPE_V4DI_V4DI_PTEST:
24297     case INT_FTYPE_V4DF_V4DF_PTEST:
24298     case INT_FTYPE_V4SF_V4SF_PTEST:
24299     case INT_FTYPE_V2DI_V2DI_PTEST:
24300     case INT_FTYPE_V2DF_V2DF_PTEST:
24301       return ix86_expand_sse_ptest (d, exp, target);
24302     case FLOAT128_FTYPE_FLOAT128:
24303     case FLOAT_FTYPE_FLOAT:
24304     case INT_FTYPE_INT:
24305     case UINT64_FTYPE_INT:
24306     case UINT16_FTYPE_UINT16:
24307     case INT64_FTYPE_INT64:
24308     case INT64_FTYPE_V4SF:
24309     case INT64_FTYPE_V2DF:
24310     case INT_FTYPE_V16QI:
24311     case INT_FTYPE_V8QI:
24312     case INT_FTYPE_V8SF:
24313     case INT_FTYPE_V4DF:
24314     case INT_FTYPE_V4SF:
24315     case INT_FTYPE_V2DF:
24316     case V16QI_FTYPE_V16QI:
24317     case V8SI_FTYPE_V8SF:
24318     case V8SI_FTYPE_V4SI:
24319     case V8HI_FTYPE_V8HI:
24320     case V8HI_FTYPE_V16QI:
24321     case V8QI_FTYPE_V8QI:
24322     case V8SF_FTYPE_V8SF:
24323     case V8SF_FTYPE_V8SI:
24324     case V8SF_FTYPE_V4SF:
24325     case V8SF_FTYPE_V8HI:
24326     case V4SI_FTYPE_V4SI:
24327     case V4SI_FTYPE_V16QI:
24328     case V4SI_FTYPE_V4SF:
24329     case V4SI_FTYPE_V8SI:
24330     case V4SI_FTYPE_V8HI:
24331     case V4SI_FTYPE_V4DF:
24332     case V4SI_FTYPE_V2DF:
24333     case V4HI_FTYPE_V4HI:
24334     case V4DF_FTYPE_V4DF:
24335     case V4DF_FTYPE_V4SI:
24336     case V4DF_FTYPE_V4SF:
24337     case V4DF_FTYPE_V2DF:
24338     case V4SF_FTYPE_V4SF:
24339     case V4SF_FTYPE_V4SI:
24340     case V4SF_FTYPE_V8SF:
24341     case V4SF_FTYPE_V4DF:
24342     case V4SF_FTYPE_V8HI:
24343     case V4SF_FTYPE_V2DF:
24344     case V2DI_FTYPE_V2DI:
24345     case V2DI_FTYPE_V16QI:
24346     case V2DI_FTYPE_V8HI:
24347     case V2DI_FTYPE_V4SI:
24348     case V2DF_FTYPE_V2DF:
24349     case V2DF_FTYPE_V4SI:
24350     case V2DF_FTYPE_V4DF:
24351     case V2DF_FTYPE_V4SF:
24352     case V2DF_FTYPE_V2SI:
24353     case V2SI_FTYPE_V2SI:
24354     case V2SI_FTYPE_V4SF:
24355     case V2SI_FTYPE_V2SF:
24356     case V2SI_FTYPE_V2DF:
24357     case V2SF_FTYPE_V2SF:
24358     case V2SF_FTYPE_V2SI:
24359       nargs = 1;
24360       break;
24361     case V4SF_FTYPE_V4SF_VEC_MERGE:
24362     case V2DF_FTYPE_V2DF_VEC_MERGE:
24363       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
24364     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
24365     case V16QI_FTYPE_V16QI_V16QI:
24366     case V16QI_FTYPE_V8HI_V8HI:
24367     case V8QI_FTYPE_V8QI_V8QI:
24368     case V8QI_FTYPE_V4HI_V4HI:
24369     case V8HI_FTYPE_V8HI_V8HI:
24370     case V8HI_FTYPE_V16QI_V16QI:
24371     case V8HI_FTYPE_V4SI_V4SI:
24372     case V8SF_FTYPE_V8SF_V8SF:
24373     case V8SF_FTYPE_V8SF_V8SI:
24374     case V4SI_FTYPE_V4SI_V4SI:
24375     case V4SI_FTYPE_V8HI_V8HI:
24376     case V4SI_FTYPE_V4SF_V4SF:
24377     case V4SI_FTYPE_V2DF_V2DF:
24378     case V4HI_FTYPE_V4HI_V4HI:
24379     case V4HI_FTYPE_V8QI_V8QI:
24380     case V4HI_FTYPE_V2SI_V2SI:
24381     case V4DF_FTYPE_V4DF_V4DF:
24382     case V4DF_FTYPE_V4DF_V4DI:
24383     case V4SF_FTYPE_V4SF_V4SF:
24384     case V4SF_FTYPE_V4SF_V4SI:
24385     case V4SF_FTYPE_V4SF_V2SI:
24386     case V4SF_FTYPE_V4SF_V2DF:
24387     case V4SF_FTYPE_V4SF_DI:
24388     case V4SF_FTYPE_V4SF_SI:
24389     case V2DI_FTYPE_V2DI_V2DI:
24390     case V2DI_FTYPE_V16QI_V16QI:
24391     case V2DI_FTYPE_V4SI_V4SI:
24392     case V2DI_FTYPE_V2DI_V16QI:
24393     case V2DI_FTYPE_V2DF_V2DF:
24394     case V2SI_FTYPE_V2SI_V2SI:
24395     case V2SI_FTYPE_V4HI_V4HI:
24396     case V2SI_FTYPE_V2SF_V2SF:
24397     case V2DF_FTYPE_V2DF_V2DF:
24398     case V2DF_FTYPE_V2DF_V4SF:
24399     case V2DF_FTYPE_V2DF_V2DI:
24400     case V2DF_FTYPE_V2DF_DI:
24401     case V2DF_FTYPE_V2DF_SI:
24402     case V2SF_FTYPE_V2SF_V2SF:
24403     case V1DI_FTYPE_V1DI_V1DI:
24404     case V1DI_FTYPE_V8QI_V8QI:
24405     case V1DI_FTYPE_V2SI_V2SI:
24406       if (comparison == UNKNOWN)
24407         return ix86_expand_binop_builtin (icode, exp, target);
24408       nargs = 2;
24409       break;
24410     case V4SF_FTYPE_V4SF_V4SF_SWAP:
24411     case V2DF_FTYPE_V2DF_V2DF_SWAP:
24412       gcc_assert (comparison != UNKNOWN);
24413       nargs = 2;
24414       swap = true;
24415       break;
24416     case V8HI_FTYPE_V8HI_V8HI_COUNT:
24417     case V8HI_FTYPE_V8HI_SI_COUNT:
24418     case V4SI_FTYPE_V4SI_V4SI_COUNT:
24419     case V4SI_FTYPE_V4SI_SI_COUNT:
24420     case V4HI_FTYPE_V4HI_V4HI_COUNT:
24421     case V4HI_FTYPE_V4HI_SI_COUNT:
24422     case V2DI_FTYPE_V2DI_V2DI_COUNT:
24423     case V2DI_FTYPE_V2DI_SI_COUNT:
24424     case V2SI_FTYPE_V2SI_V2SI_COUNT:
24425     case V2SI_FTYPE_V2SI_SI_COUNT:
24426     case V1DI_FTYPE_V1DI_V1DI_COUNT:
24427     case V1DI_FTYPE_V1DI_SI_COUNT:
24428       nargs = 2;
24429       last_arg_count = true;
24430       break;
24431     case UINT64_FTYPE_UINT64_UINT64:
24432     case UINT_FTYPE_UINT_UINT:
24433     case UINT_FTYPE_UINT_USHORT:
24434     case UINT_FTYPE_UINT_UCHAR:
24435     case UINT16_FTYPE_UINT16_INT:
24436     case UINT8_FTYPE_UINT8_INT:
24437       nargs = 2;
24438       break;
24439     case V2DI_FTYPE_V2DI_INT_CONVERT:
24440       nargs = 2;
24441       rmode = V1TImode;
24442       nargs_constant = 1;
24443       break;
24444     case V8HI_FTYPE_V8HI_INT:
24445     case V8HI_FTYPE_V8SF_INT:
24446     case V8HI_FTYPE_V4SF_INT:
24447     case V8SF_FTYPE_V8SF_INT:
24448     case V4SI_FTYPE_V4SI_INT:
24449     case V4SI_FTYPE_V8SI_INT:
24450     case V4HI_FTYPE_V4HI_INT:
24451     case V4DF_FTYPE_V4DF_INT:
24452     case V4SF_FTYPE_V4SF_INT:
24453     case V4SF_FTYPE_V8SF_INT:
24454     case V2DI_FTYPE_V2DI_INT:
24455     case V2DF_FTYPE_V2DF_INT:
24456     case V2DF_FTYPE_V4DF_INT:
24457       nargs = 2;
24458       nargs_constant = 1;
24459       break;
24460     case V16QI_FTYPE_V16QI_V16QI_V16QI:
24461     case V8SF_FTYPE_V8SF_V8SF_V8SF:
24462     case V4DF_FTYPE_V4DF_V4DF_V4DF:
24463     case V4SF_FTYPE_V4SF_V4SF_V4SF:
24464     case V2DF_FTYPE_V2DF_V2DF_V2DF:
24465       nargs = 3;
24466       break;
24467     case V16QI_FTYPE_V16QI_V16QI_INT:
24468     case V8HI_FTYPE_V8HI_V8HI_INT:
24469     case V8SI_FTYPE_V8SI_V8SI_INT:
24470     case V8SI_FTYPE_V8SI_V4SI_INT:
24471     case V8SF_FTYPE_V8SF_V8SF_INT:
24472     case V8SF_FTYPE_V8SF_V4SF_INT:
24473     case V4SI_FTYPE_V4SI_V4SI_INT:
24474     case V4DF_FTYPE_V4DF_V4DF_INT:
24475     case V4DF_FTYPE_V4DF_V2DF_INT:
24476     case V4SF_FTYPE_V4SF_V4SF_INT:
24477     case V2DI_FTYPE_V2DI_V2DI_INT:
24478     case V2DF_FTYPE_V2DF_V2DF_INT:
24479       nargs = 3;
24480       nargs_constant = 1;
24481       break;
24482     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
24483       nargs = 3;
24484       rmode = V2DImode;
24485       nargs_constant = 1;
24486       break;
24487     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
24488       nargs = 3;
24489       rmode = DImode;
24490       nargs_constant = 1;
24491       break;
24492     case V2DI_FTYPE_V2DI_UINT_UINT:
24493       nargs = 3;
24494       nargs_constant = 2;
24495       break;
24496     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
24497     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
24498     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
24499     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
24500       nargs = 4;
24501       nargs_constant = 1;
24502       break;
24503     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
24504       nargs = 4;
24505       nargs_constant = 2;
24506       break;
24507     default:
24508       gcc_unreachable ();
24509     }
24510
24511   gcc_assert (nargs <= ARRAY_SIZE (args));
24512
24513   if (comparison != UNKNOWN)
24514     {
24515       gcc_assert (nargs == 2);
24516       return ix86_expand_sse_compare (d, exp, target, swap);
24517     }
24518
24519   if (rmode == VOIDmode || rmode == tmode)
24520     {
24521       if (optimize
24522           || target == 0
24523           || GET_MODE (target) != tmode
24524           || !insn_p->operand[0].predicate (target, tmode))
24525         target = gen_reg_rtx (tmode);
24526       real_target = target;
24527     }
24528   else
24529     {
24530       target = gen_reg_rtx (rmode);
24531       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
24532     }
24533
24534   for (i = 0; i < nargs; i++)
24535     {
24536       tree arg = CALL_EXPR_ARG (exp, i);
24537       rtx op = expand_normal (arg);
24538       enum machine_mode mode = insn_p->operand[i + 1].mode;
24539       bool match = insn_p->operand[i + 1].predicate (op, mode);
24540
24541       if (last_arg_count && (i + 1) == nargs)
24542         {
24543           /* SIMD shift insns take either an 8-bit immediate or
24544              register as count.  But builtin functions take int as
24545              count.  If count doesn't match, we put it in register.  */
24546           if (!match)
24547             {
24548               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
24549               if (!insn_p->operand[i + 1].predicate (op, mode))
24550                 op = copy_to_reg (op);
24551             }
24552         }
24553       else if ((nargs - i) <= nargs_constant)
24554         {
24555           if (!match)
24556             switch (icode)
24557               {
24558               case CODE_FOR_sse4_1_roundpd:
24559               case CODE_FOR_sse4_1_roundps:
24560               case CODE_FOR_sse4_1_roundsd:
24561               case CODE_FOR_sse4_1_roundss:
24562               case CODE_FOR_sse4_1_blendps:
24563               case CODE_FOR_avx_blendpd256:
24564               case CODE_FOR_avx_vpermilv4df:
24565               case CODE_FOR_avx_roundpd256:
24566               case CODE_FOR_avx_roundps256:
24567                 error ("the last argument must be a 4-bit immediate");
24568                 return const0_rtx;
24569
24570               case CODE_FOR_sse4_1_blendpd:
24571               case CODE_FOR_avx_vpermilv2df:
24572               case CODE_FOR_xop_vpermil2v2df3:
24573               case CODE_FOR_xop_vpermil2v4sf3:
24574               case CODE_FOR_xop_vpermil2v4df3:
24575               case CODE_FOR_xop_vpermil2v8sf3:
24576                 error ("the last argument must be a 2-bit immediate");
24577                 return const0_rtx;
24578
24579               case CODE_FOR_avx_vextractf128v4df:
24580               case CODE_FOR_avx_vextractf128v8sf:
24581               case CODE_FOR_avx_vextractf128v8si:
24582               case CODE_FOR_avx_vinsertf128v4df:
24583               case CODE_FOR_avx_vinsertf128v8sf:
24584               case CODE_FOR_avx_vinsertf128v8si:
24585                 error ("the last argument must be a 1-bit immediate");
24586                 return const0_rtx;
24587
24588               case CODE_FOR_avx_cmpsdv2df3:
24589               case CODE_FOR_avx_cmpssv4sf3:
24590               case CODE_FOR_avx_cmppdv2df3:
24591               case CODE_FOR_avx_cmppsv4sf3:
24592               case CODE_FOR_avx_cmppdv4df3:
24593               case CODE_FOR_avx_cmppsv8sf3:
24594                 error ("the last argument must be a 5-bit immediate");
24595                 return const0_rtx;
24596
24597              default:
24598                 switch (nargs_constant)
24599                   {
24600                   case 2:
24601                     if ((nargs - i) == nargs_constant)
24602                       {
24603                         error ("the next to last argument must be an 8-bit immediate");
24604                         break;
24605                       }
24606                   case 1:
24607                     error ("the last argument must be an 8-bit immediate");
24608                     break;
24609                   default:
24610                     gcc_unreachable ();
24611                   }
24612                 return const0_rtx;
24613               }
24614         }
24615       else
24616         {
24617           if (VECTOR_MODE_P (mode))
24618             op = safe_vector_operand (op, mode);
24619
24620           /* If we aren't optimizing, only allow one memory operand to
24621              be generated.  */
24622           if (memory_operand (op, mode))
24623             num_memory++;
24624
24625           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
24626             {
24627               if (optimize || !match || num_memory > 1)
24628                 op = copy_to_mode_reg (mode, op);
24629             }
24630           else
24631             {
24632               op = copy_to_reg (op);
24633               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
24634             }
24635         }
24636
24637       args[i].op = op;
24638       args[i].mode = mode;
24639     }
24640
24641   switch (nargs)
24642     {
24643     case 1:
24644       pat = GEN_FCN (icode) (real_target, args[0].op);
24645       break;
24646     case 2:
24647       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
24648       break;
24649     case 3:
24650       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
24651                              args[2].op);
24652       break;
24653     case 4:
24654       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
24655                              args[2].op, args[3].op);
24656       break;
24657     default:
24658       gcc_unreachable ();
24659     }
24660
24661   if (! pat)
24662     return 0;
24663
24664   emit_insn (pat);
24665   return target;
24666 }
24667
24668 /* Subroutine of ix86_expand_builtin to take care of special insns
24669    with variable number of operands.  */
24670
24671 static rtx
24672 ix86_expand_special_args_builtin (const struct builtin_description *d,
24673                                     tree exp, rtx target)
24674 {
24675   tree arg;
24676   rtx pat, op;
24677   unsigned int i, nargs, arg_adjust, memory;
24678   struct
24679     {
24680       rtx op;
24681       enum machine_mode mode;
24682     } args[3];
24683   enum insn_code icode = d->icode;
24684   bool last_arg_constant = false;
24685   const struct insn_data_d *insn_p = &insn_data[icode];
24686   enum machine_mode tmode = insn_p->operand[0].mode;
24687   enum { load, store } klass;
24688
24689   switch ((enum ix86_builtin_func_type) d->flag)
24690     {
24691     case VOID_FTYPE_VOID:
24692       emit_insn (GEN_FCN (icode) (target));
24693       return 0;
24694     case VOID_FTYPE_UINT64:
24695     case VOID_FTYPE_UNSIGNED:
24696       nargs = 0;
24697       klass = store;
24698       memory = 0;
24699       break;
24700       break;
24701     case UINT64_FTYPE_VOID:
24702     case UNSIGNED_FTYPE_VOID:
24703     case UINT16_FTYPE_VOID:
24704       nargs = 0;
24705       klass = load;
24706       memory = 0;
24707       break;
24708     case UINT64_FTYPE_PUNSIGNED:
24709     case V2DI_FTYPE_PV2DI:
24710     case V32QI_FTYPE_PCCHAR:
24711     case V16QI_FTYPE_PCCHAR:
24712     case V8SF_FTYPE_PCV4SF:
24713     case V8SF_FTYPE_PCFLOAT:
24714     case V4SF_FTYPE_PCFLOAT:
24715     case V4DF_FTYPE_PCV2DF:
24716     case V4DF_FTYPE_PCDOUBLE:
24717     case V2DF_FTYPE_PCDOUBLE:
24718     case VOID_FTYPE_PVOID:
24719       nargs = 1;
24720       klass = load;
24721       memory = 0;
24722       break;
24723     case VOID_FTYPE_PV2SF_V4SF:
24724     case VOID_FTYPE_PV4DI_V4DI:
24725     case VOID_FTYPE_PV2DI_V2DI:
24726     case VOID_FTYPE_PCHAR_V32QI:
24727     case VOID_FTYPE_PCHAR_V16QI:
24728     case VOID_FTYPE_PFLOAT_V8SF:
24729     case VOID_FTYPE_PFLOAT_V4SF:
24730     case VOID_FTYPE_PDOUBLE_V4DF:
24731     case VOID_FTYPE_PDOUBLE_V2DF:
24732     case VOID_FTYPE_PULONGLONG_ULONGLONG:
24733     case VOID_FTYPE_PINT_INT:
24734       nargs = 1;
24735       klass = store;
24736       /* Reserve memory operand for target.  */
24737       memory = ARRAY_SIZE (args);
24738       break;
24739     case V4SF_FTYPE_V4SF_PCV2SF:
24740     case V2DF_FTYPE_V2DF_PCDOUBLE:
24741       nargs = 2;
24742       klass = load;
24743       memory = 1;
24744       break;
24745     case V8SF_FTYPE_PCV8SF_V8SF:
24746     case V4DF_FTYPE_PCV4DF_V4DF:
24747     case V4SF_FTYPE_PCV4SF_V4SF:
24748     case V2DF_FTYPE_PCV2DF_V2DF:
24749       nargs = 2;
24750       klass = load;
24751       memory = 0;
24752       break;
24753     case VOID_FTYPE_PV8SF_V8SF_V8SF:
24754     case VOID_FTYPE_PV4DF_V4DF_V4DF:
24755     case VOID_FTYPE_PV4SF_V4SF_V4SF:
24756     case VOID_FTYPE_PV2DF_V2DF_V2DF:
24757       nargs = 2;
24758       klass = store;
24759       /* Reserve memory operand for target.  */
24760       memory = ARRAY_SIZE (args);
24761       break;
24762     case VOID_FTYPE_UINT_UINT_UINT:
24763     case VOID_FTYPE_UINT64_UINT_UINT:
24764     case UCHAR_FTYPE_UINT_UINT_UINT:
24765     case UCHAR_FTYPE_UINT64_UINT_UINT:
24766       nargs = 3;
24767       klass = load;
24768       memory = ARRAY_SIZE (args);
24769       last_arg_constant = true;
24770       break;
24771     default:
24772       gcc_unreachable ();
24773     }
24774
24775   gcc_assert (nargs <= ARRAY_SIZE (args));
24776
24777   if (klass == store)
24778     {
24779       arg = CALL_EXPR_ARG (exp, 0);
24780       op = expand_normal (arg);
24781       gcc_assert (target == 0);
24782       if (memory)
24783         target = gen_rtx_MEM (tmode, copy_to_mode_reg (Pmode, op));
24784       else
24785         target = force_reg (tmode, op);
24786       arg_adjust = 1;
24787     }
24788   else
24789     {
24790       arg_adjust = 0;
24791       if (optimize
24792           || target == 0
24793           || GET_MODE (target) != tmode
24794           || !insn_p->operand[0].predicate (target, tmode))
24795         target = gen_reg_rtx (tmode);
24796     }
24797
24798   for (i = 0; i < nargs; i++)
24799     {
24800       enum machine_mode mode = insn_p->operand[i + 1].mode;
24801       bool match;
24802
24803       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
24804       op = expand_normal (arg);
24805       match = insn_p->operand[i + 1].predicate (op, mode);
24806
24807       if (last_arg_constant && (i + 1) == nargs)
24808         {
24809           if (!match)
24810             {
24811               if (icode == CODE_FOR_lwp_lwpvalsi3
24812                   || icode == CODE_FOR_lwp_lwpinssi3
24813                   || icode == CODE_FOR_lwp_lwpvaldi3
24814                   || icode == CODE_FOR_lwp_lwpinsdi3)
24815                 error ("the last argument must be a 32-bit immediate");
24816               else
24817                 error ("the last argument must be an 8-bit immediate");
24818               return const0_rtx;
24819             }
24820         }
24821       else
24822         {
24823           if (i == memory)
24824             {
24825               /* This must be the memory operand.  */
24826               op = gen_rtx_MEM (mode, copy_to_mode_reg (Pmode, op));
24827               gcc_assert (GET_MODE (op) == mode
24828                           || GET_MODE (op) == VOIDmode);
24829             }
24830           else
24831             {
24832               /* This must be register.  */
24833               if (VECTOR_MODE_P (mode))
24834                 op = safe_vector_operand (op, mode);
24835
24836               gcc_assert (GET_MODE (op) == mode
24837                           || GET_MODE (op) == VOIDmode);
24838               op = copy_to_mode_reg (mode, op);
24839             }
24840         }
24841
24842       args[i].op = op;
24843       args[i].mode = mode;
24844     }
24845
24846   switch (nargs)
24847     {
24848     case 0:
24849       pat = GEN_FCN (icode) (target);
24850       break;
24851     case 1:
24852       pat = GEN_FCN (icode) (target, args[0].op);
24853       break;
24854     case 2:
24855       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
24856       break;
24857     case 3:
24858       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
24859       break;
24860     default:
24861       gcc_unreachable ();
24862     }
24863
24864   if (! pat)
24865     return 0;
24866   emit_insn (pat);
24867   return klass == store ? 0 : target;
24868 }
24869
24870 /* Return the integer constant in ARG.  Constrain it to be in the range
24871    of the subparts of VEC_TYPE; issue an error if not.  */
24872
24873 static int
24874 get_element_number (tree vec_type, tree arg)
24875 {
24876   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
24877
24878   if (!host_integerp (arg, 1)
24879       || (elt = tree_low_cst (arg, 1), elt > max))
24880     {
24881       error ("selector must be an integer constant in the range 0..%wi", max);
24882       return 0;
24883     }
24884
24885   return elt;
24886 }
24887
24888 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24889    ix86_expand_vector_init.  We DO have language-level syntax for this, in
24890    the form of  (type){ init-list }.  Except that since we can't place emms
24891    instructions from inside the compiler, we can't allow the use of MMX
24892    registers unless the user explicitly asks for it.  So we do *not* define
24893    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
24894    we have builtins invoked by mmintrin.h that gives us license to emit
24895    these sorts of instructions.  */
24896
24897 static rtx
24898 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
24899 {
24900   enum machine_mode tmode = TYPE_MODE (type);
24901   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
24902   int i, n_elt = GET_MODE_NUNITS (tmode);
24903   rtvec v = rtvec_alloc (n_elt);
24904
24905   gcc_assert (VECTOR_MODE_P (tmode));
24906   gcc_assert (call_expr_nargs (exp) == n_elt);
24907
24908   for (i = 0; i < n_elt; ++i)
24909     {
24910       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
24911       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
24912     }
24913
24914   if (!target || !register_operand (target, tmode))
24915     target = gen_reg_rtx (tmode);
24916
24917   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
24918   return target;
24919 }
24920
24921 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24922    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
24923    had a language-level syntax for referencing vector elements.  */
24924
24925 static rtx
24926 ix86_expand_vec_ext_builtin (tree exp, rtx target)
24927 {
24928   enum machine_mode tmode, mode0;
24929   tree arg0, arg1;
24930   int elt;
24931   rtx op0;
24932
24933   arg0 = CALL_EXPR_ARG (exp, 0);
24934   arg1 = CALL_EXPR_ARG (exp, 1);
24935
24936   op0 = expand_normal (arg0);
24937   elt = get_element_number (TREE_TYPE (arg0), arg1);
24938
24939   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
24940   mode0 = TYPE_MODE (TREE_TYPE (arg0));
24941   gcc_assert (VECTOR_MODE_P (mode0));
24942
24943   op0 = force_reg (mode0, op0);
24944
24945   if (optimize || !target || !register_operand (target, tmode))
24946     target = gen_reg_rtx (tmode);
24947
24948   ix86_expand_vector_extract (true, target, op0, elt);
24949
24950   return target;
24951 }
24952
24953 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
24954    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
24955    a language-level syntax for referencing vector elements.  */
24956
24957 static rtx
24958 ix86_expand_vec_set_builtin (tree exp)
24959 {
24960   enum machine_mode tmode, mode1;
24961   tree arg0, arg1, arg2;
24962   int elt;
24963   rtx op0, op1, target;
24964
24965   arg0 = CALL_EXPR_ARG (exp, 0);
24966   arg1 = CALL_EXPR_ARG (exp, 1);
24967   arg2 = CALL_EXPR_ARG (exp, 2);
24968
24969   tmode = TYPE_MODE (TREE_TYPE (arg0));
24970   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
24971   gcc_assert (VECTOR_MODE_P (tmode));
24972
24973   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
24974   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
24975   elt = get_element_number (TREE_TYPE (arg0), arg2);
24976
24977   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
24978     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
24979
24980   op0 = force_reg (tmode, op0);
24981   op1 = force_reg (mode1, op1);
24982
24983   /* OP0 is the source of these builtin functions and shouldn't be
24984      modified.  Create a copy, use it and return it as target.  */
24985   target = gen_reg_rtx (tmode);
24986   emit_move_insn (target, op0);
24987   ix86_expand_vector_set (true, target, op1, elt);
24988
24989   return target;
24990 }
24991
24992 /* Expand an expression EXP that calls a built-in function,
24993    with result going to TARGET if that's convenient
24994    (and in mode MODE if that's convenient).
24995    SUBTARGET may be used as the target for computing one of EXP's operands.
24996    IGNORE is nonzero if the value is to be ignored.  */
24997
24998 static rtx
24999 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
25000                      enum machine_mode mode ATTRIBUTE_UNUSED,
25001                      int ignore ATTRIBUTE_UNUSED)
25002 {
25003   const struct builtin_description *d;
25004   size_t i;
25005   enum insn_code icode;
25006   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
25007   tree arg0, arg1, arg2;
25008   rtx op0, op1, op2, pat;
25009   enum machine_mode mode0, mode1, mode2;
25010   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
25011
25012   /* Determine whether the builtin function is available under the current ISA.
25013      Originally the builtin was not created if it wasn't applicable to the
25014      current ISA based on the command line switches.  With function specific
25015      options, we need to check in the context of the function making the call
25016      whether it is supported.  */
25017   if (ix86_builtins_isa[fcode].isa
25018       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
25019     {
25020       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
25021                                        NULL, NULL, false);
25022
25023       if (!opts)
25024         error ("%qE needs unknown isa option", fndecl);
25025       else
25026         {
25027           gcc_assert (opts != NULL);
25028           error ("%qE needs isa option %s", fndecl, opts);
25029           free (opts);
25030         }
25031       return const0_rtx;
25032     }
25033
25034   switch (fcode)
25035     {
25036     case IX86_BUILTIN_MASKMOVQ:
25037     case IX86_BUILTIN_MASKMOVDQU:
25038       icode = (fcode == IX86_BUILTIN_MASKMOVQ
25039                ? CODE_FOR_mmx_maskmovq
25040                : CODE_FOR_sse2_maskmovdqu);
25041       /* Note the arg order is different from the operand order.  */
25042       arg1 = CALL_EXPR_ARG (exp, 0);
25043       arg2 = CALL_EXPR_ARG (exp, 1);
25044       arg0 = CALL_EXPR_ARG (exp, 2);
25045       op0 = expand_normal (arg0);
25046       op1 = expand_normal (arg1);
25047       op2 = expand_normal (arg2);
25048       mode0 = insn_data[icode].operand[0].mode;
25049       mode1 = insn_data[icode].operand[1].mode;
25050       mode2 = insn_data[icode].operand[2].mode;
25051
25052       op0 = force_reg (Pmode, op0);
25053       op0 = gen_rtx_MEM (mode1, op0);
25054
25055       if (!insn_data[icode].operand[0].predicate (op0, mode0))
25056         op0 = copy_to_mode_reg (mode0, op0);
25057       if (!insn_data[icode].operand[1].predicate (op1, mode1))
25058         op1 = copy_to_mode_reg (mode1, op1);
25059       if (!insn_data[icode].operand[2].predicate (op2, mode2))
25060         op2 = copy_to_mode_reg (mode2, op2);
25061       pat = GEN_FCN (icode) (op0, op1, op2);
25062       if (! pat)
25063         return 0;
25064       emit_insn (pat);
25065       return 0;
25066
25067     case IX86_BUILTIN_LDMXCSR:
25068       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
25069       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
25070       emit_move_insn (target, op0);
25071       emit_insn (gen_sse_ldmxcsr (target));
25072       return 0;
25073
25074     case IX86_BUILTIN_STMXCSR:
25075       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
25076       emit_insn (gen_sse_stmxcsr (target));
25077       return copy_to_mode_reg (SImode, target);
25078
25079     case IX86_BUILTIN_CLFLUSH:
25080         arg0 = CALL_EXPR_ARG (exp, 0);
25081         op0 = expand_normal (arg0);
25082         icode = CODE_FOR_sse2_clflush;
25083         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
25084             op0 = copy_to_mode_reg (Pmode, op0);
25085
25086         emit_insn (gen_sse2_clflush (op0));
25087         return 0;
25088
25089     case IX86_BUILTIN_MONITOR:
25090       arg0 = CALL_EXPR_ARG (exp, 0);
25091       arg1 = CALL_EXPR_ARG (exp, 1);
25092       arg2 = CALL_EXPR_ARG (exp, 2);
25093       op0 = expand_normal (arg0);
25094       op1 = expand_normal (arg1);
25095       op2 = expand_normal (arg2);
25096       if (!REG_P (op0))
25097         op0 = copy_to_mode_reg (Pmode, op0);
25098       if (!REG_P (op1))
25099         op1 = copy_to_mode_reg (SImode, op1);
25100       if (!REG_P (op2))
25101         op2 = copy_to_mode_reg (SImode, op2);
25102       emit_insn (ix86_gen_monitor (op0, op1, op2));
25103       return 0;
25104
25105     case IX86_BUILTIN_MWAIT:
25106       arg0 = CALL_EXPR_ARG (exp, 0);
25107       arg1 = CALL_EXPR_ARG (exp, 1);
25108       op0 = expand_normal (arg0);
25109       op1 = expand_normal (arg1);
25110       if (!REG_P (op0))
25111         op0 = copy_to_mode_reg (SImode, op0);
25112       if (!REG_P (op1))
25113         op1 = copy_to_mode_reg (SImode, op1);
25114       emit_insn (gen_sse3_mwait (op0, op1));
25115       return 0;
25116
25117     case IX86_BUILTIN_VEC_INIT_V2SI:
25118     case IX86_BUILTIN_VEC_INIT_V4HI:
25119     case IX86_BUILTIN_VEC_INIT_V8QI:
25120       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
25121
25122     case IX86_BUILTIN_VEC_EXT_V2DF:
25123     case IX86_BUILTIN_VEC_EXT_V2DI:
25124     case IX86_BUILTIN_VEC_EXT_V4SF:
25125     case IX86_BUILTIN_VEC_EXT_V4SI:
25126     case IX86_BUILTIN_VEC_EXT_V8HI:
25127     case IX86_BUILTIN_VEC_EXT_V2SI:
25128     case IX86_BUILTIN_VEC_EXT_V4HI:
25129     case IX86_BUILTIN_VEC_EXT_V16QI:
25130       return ix86_expand_vec_ext_builtin (exp, target);
25131
25132     case IX86_BUILTIN_VEC_SET_V2DI:
25133     case IX86_BUILTIN_VEC_SET_V4SF:
25134     case IX86_BUILTIN_VEC_SET_V4SI:
25135     case IX86_BUILTIN_VEC_SET_V8HI:
25136     case IX86_BUILTIN_VEC_SET_V4HI:
25137     case IX86_BUILTIN_VEC_SET_V16QI:
25138       return ix86_expand_vec_set_builtin (exp);
25139
25140     case IX86_BUILTIN_VEC_PERM_V2DF:
25141     case IX86_BUILTIN_VEC_PERM_V4SF:
25142     case IX86_BUILTIN_VEC_PERM_V2DI:
25143     case IX86_BUILTIN_VEC_PERM_V4SI:
25144     case IX86_BUILTIN_VEC_PERM_V8HI:
25145     case IX86_BUILTIN_VEC_PERM_V16QI:
25146     case IX86_BUILTIN_VEC_PERM_V2DI_U:
25147     case IX86_BUILTIN_VEC_PERM_V4SI_U:
25148     case IX86_BUILTIN_VEC_PERM_V8HI_U:
25149     case IX86_BUILTIN_VEC_PERM_V16QI_U:
25150     case IX86_BUILTIN_VEC_PERM_V4DF:
25151     case IX86_BUILTIN_VEC_PERM_V8SF:
25152       return ix86_expand_vec_perm_builtin (exp);
25153
25154     case IX86_BUILTIN_INFQ:
25155     case IX86_BUILTIN_HUGE_VALQ:
25156       {
25157         REAL_VALUE_TYPE inf;
25158         rtx tmp;
25159
25160         real_inf (&inf);
25161         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
25162
25163         tmp = validize_mem (force_const_mem (mode, tmp));
25164
25165         if (target == 0)
25166           target = gen_reg_rtx (mode);
25167
25168         emit_move_insn (target, tmp);
25169         return target;
25170       }
25171
25172     case IX86_BUILTIN_LLWPCB:
25173       arg0 = CALL_EXPR_ARG (exp, 0);
25174       op0 = expand_normal (arg0);
25175       icode = CODE_FOR_lwp_llwpcb;
25176       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
25177         op0 = copy_to_mode_reg (Pmode, op0);
25178       emit_insn (gen_lwp_llwpcb (op0));
25179       return 0;
25180
25181     case IX86_BUILTIN_SLWPCB:
25182       icode = CODE_FOR_lwp_slwpcb;
25183       if (!target
25184           || !insn_data[icode].operand[0].predicate (target, Pmode))
25185         target = gen_reg_rtx (Pmode);
25186       emit_insn (gen_lwp_slwpcb (target));
25187       return target;
25188
25189     default:
25190       break;
25191     }
25192
25193   for (i = 0, d = bdesc_special_args;
25194        i < ARRAY_SIZE (bdesc_special_args);
25195        i++, d++)
25196     if (d->code == fcode)
25197       return ix86_expand_special_args_builtin (d, exp, target);
25198
25199   for (i = 0, d = bdesc_args;
25200        i < ARRAY_SIZE (bdesc_args);
25201        i++, d++)
25202     if (d->code == fcode)
25203       switch (fcode)
25204         {
25205         case IX86_BUILTIN_FABSQ:
25206         case IX86_BUILTIN_COPYSIGNQ:
25207           if (!TARGET_SSE2)
25208             /* Emit a normal call if SSE2 isn't available.  */
25209             return expand_call (exp, target, ignore);
25210         default:
25211           return ix86_expand_args_builtin (d, exp, target);
25212         }
25213
25214   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
25215     if (d->code == fcode)
25216       return ix86_expand_sse_comi (d, exp, target);
25217
25218   for (i = 0, d = bdesc_pcmpestr;
25219        i < ARRAY_SIZE (bdesc_pcmpestr);
25220        i++, d++)
25221     if (d->code == fcode)
25222       return ix86_expand_sse_pcmpestr (d, exp, target);
25223
25224   for (i = 0, d = bdesc_pcmpistr;
25225        i < ARRAY_SIZE (bdesc_pcmpistr);
25226        i++, d++)
25227     if (d->code == fcode)
25228       return ix86_expand_sse_pcmpistr (d, exp, target);
25229
25230   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
25231     if (d->code == fcode)
25232       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
25233                                             (enum ix86_builtin_func_type)
25234                                             d->flag, d->comparison);
25235
25236   gcc_unreachable ();
25237 }
25238
25239 /* Returns a function decl for a vectorized version of the builtin function
25240    with builtin function code FN and the result vector type TYPE, or NULL_TREE
25241    if it is not available.  */
25242
25243 static tree
25244 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
25245                                   tree type_in)
25246 {
25247   enum machine_mode in_mode, out_mode;
25248   int in_n, out_n;
25249   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
25250
25251   if (TREE_CODE (type_out) != VECTOR_TYPE
25252       || TREE_CODE (type_in) != VECTOR_TYPE
25253       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
25254     return NULL_TREE;
25255
25256   out_mode = TYPE_MODE (TREE_TYPE (type_out));
25257   out_n = TYPE_VECTOR_SUBPARTS (type_out);
25258   in_mode = TYPE_MODE (TREE_TYPE (type_in));
25259   in_n = TYPE_VECTOR_SUBPARTS (type_in);
25260
25261   switch (fn)
25262     {
25263     case BUILT_IN_SQRT:
25264       if (out_mode == DFmode && out_n == 2
25265           && in_mode == DFmode && in_n == 2)
25266         return ix86_builtins[IX86_BUILTIN_SQRTPD];
25267       break;
25268
25269     case BUILT_IN_SQRTF:
25270       if (out_mode == SFmode && out_n == 4
25271           && in_mode == SFmode && in_n == 4)
25272         return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
25273       break;
25274
25275     case BUILT_IN_LRINT:
25276       if (out_mode == SImode && out_n == 4
25277           && in_mode == DFmode && in_n == 2)
25278         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
25279       break;
25280
25281     case BUILT_IN_LRINTF:
25282       if (out_mode == SImode && out_n == 4
25283           && in_mode == SFmode && in_n == 4)
25284         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
25285       break;
25286
25287     case BUILT_IN_COPYSIGN:
25288       if (out_mode == DFmode && out_n == 2
25289           && in_mode == DFmode && in_n == 2)
25290         return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
25291       break;
25292
25293     case BUILT_IN_COPYSIGNF:
25294       if (out_mode == SFmode && out_n == 4
25295           && in_mode == SFmode && in_n == 4)
25296         return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
25297       break;
25298
25299     default:
25300       ;
25301     }
25302
25303   /* Dispatch to a handler for a vectorization library.  */
25304   if (ix86_veclib_handler)
25305     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
25306                                 type_in);
25307
25308   return NULL_TREE;
25309 }
25310
25311 /* Handler for an SVML-style interface to
25312    a library with vectorized intrinsics.  */
25313
25314 static tree
25315 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
25316 {
25317   char name[20];
25318   tree fntype, new_fndecl, args;
25319   unsigned arity;
25320   const char *bname;
25321   enum machine_mode el_mode, in_mode;
25322   int n, in_n;
25323
25324   /* The SVML is suitable for unsafe math only.  */
25325   if (!flag_unsafe_math_optimizations)
25326     return NULL_TREE;
25327
25328   el_mode = TYPE_MODE (TREE_TYPE (type_out));
25329   n = TYPE_VECTOR_SUBPARTS (type_out);
25330   in_mode = TYPE_MODE (TREE_TYPE (type_in));
25331   in_n = TYPE_VECTOR_SUBPARTS (type_in);
25332   if (el_mode != in_mode
25333       || n != in_n)
25334     return NULL_TREE;
25335
25336   switch (fn)
25337     {
25338     case BUILT_IN_EXP:
25339     case BUILT_IN_LOG:
25340     case BUILT_IN_LOG10:
25341     case BUILT_IN_POW:
25342     case BUILT_IN_TANH:
25343     case BUILT_IN_TAN:
25344     case BUILT_IN_ATAN:
25345     case BUILT_IN_ATAN2:
25346     case BUILT_IN_ATANH:
25347     case BUILT_IN_CBRT:
25348     case BUILT_IN_SINH:
25349     case BUILT_IN_SIN:
25350     case BUILT_IN_ASINH:
25351     case BUILT_IN_ASIN:
25352     case BUILT_IN_COSH:
25353     case BUILT_IN_COS:
25354     case BUILT_IN_ACOSH:
25355     case BUILT_IN_ACOS:
25356       if (el_mode != DFmode || n != 2)
25357         return NULL_TREE;
25358       break;
25359
25360     case BUILT_IN_EXPF:
25361     case BUILT_IN_LOGF:
25362     case BUILT_IN_LOG10F:
25363     case BUILT_IN_POWF:
25364     case BUILT_IN_TANHF:
25365     case BUILT_IN_TANF:
25366     case BUILT_IN_ATANF:
25367     case BUILT_IN_ATAN2F:
25368     case BUILT_IN_ATANHF:
25369     case BUILT_IN_CBRTF:
25370     case BUILT_IN_SINHF:
25371     case BUILT_IN_SINF:
25372     case BUILT_IN_ASINHF:
25373     case BUILT_IN_ASINF:
25374     case BUILT_IN_COSHF:
25375     case BUILT_IN_COSF:
25376     case BUILT_IN_ACOSHF:
25377     case BUILT_IN_ACOSF:
25378       if (el_mode != SFmode || n != 4)
25379         return NULL_TREE;
25380       break;
25381
25382     default:
25383       return NULL_TREE;
25384     }
25385
25386   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
25387
25388   if (fn == BUILT_IN_LOGF)
25389     strcpy (name, "vmlsLn4");
25390   else if (fn == BUILT_IN_LOG)
25391     strcpy (name, "vmldLn2");
25392   else if (n == 4)
25393     {
25394       sprintf (name, "vmls%s", bname+10);
25395       name[strlen (name)-1] = '4';
25396     }
25397   else
25398     sprintf (name, "vmld%s2", bname+10);
25399
25400   /* Convert to uppercase. */
25401   name[4] &= ~0x20;
25402
25403   arity = 0;
25404   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
25405        args = TREE_CHAIN (args))
25406     arity++;
25407
25408   if (arity == 1)
25409     fntype = build_function_type_list (type_out, type_in, NULL);
25410   else
25411     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
25412
25413   /* Build a function declaration for the vectorized function.  */
25414   new_fndecl = build_decl (BUILTINS_LOCATION,
25415                            FUNCTION_DECL, get_identifier (name), fntype);
25416   TREE_PUBLIC (new_fndecl) = 1;
25417   DECL_EXTERNAL (new_fndecl) = 1;
25418   DECL_IS_NOVOPS (new_fndecl) = 1;
25419   TREE_READONLY (new_fndecl) = 1;
25420
25421   return new_fndecl;
25422 }
25423
25424 /* Handler for an ACML-style interface to
25425    a library with vectorized intrinsics.  */
25426
25427 static tree
25428 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
25429 {
25430   char name[20] = "__vr.._";
25431   tree fntype, new_fndecl, args;
25432   unsigned arity;
25433   const char *bname;
25434   enum machine_mode el_mode, in_mode;
25435   int n, in_n;
25436
25437   /* The ACML is 64bits only and suitable for unsafe math only as
25438      it does not correctly support parts of IEEE with the required
25439      precision such as denormals.  */
25440   if (!TARGET_64BIT
25441       || !flag_unsafe_math_optimizations)
25442     return NULL_TREE;
25443
25444   el_mode = TYPE_MODE (TREE_TYPE (type_out));
25445   n = TYPE_VECTOR_SUBPARTS (type_out);
25446   in_mode = TYPE_MODE (TREE_TYPE (type_in));
25447   in_n = TYPE_VECTOR_SUBPARTS (type_in);
25448   if (el_mode != in_mode
25449       || n != in_n)
25450     return NULL_TREE;
25451
25452   switch (fn)
25453     {
25454     case BUILT_IN_SIN:
25455     case BUILT_IN_COS:
25456     case BUILT_IN_EXP:
25457     case BUILT_IN_LOG:
25458     case BUILT_IN_LOG2:
25459     case BUILT_IN_LOG10:
25460       name[4] = 'd';
25461       name[5] = '2';
25462       if (el_mode != DFmode
25463           || n != 2)
25464         return NULL_TREE;
25465       break;
25466
25467     case BUILT_IN_SINF:
25468     case BUILT_IN_COSF:
25469     case BUILT_IN_EXPF:
25470     case BUILT_IN_POWF:
25471     case BUILT_IN_LOGF:
25472     case BUILT_IN_LOG2F:
25473     case BUILT_IN_LOG10F:
25474       name[4] = 's';
25475       name[5] = '4';
25476       if (el_mode != SFmode
25477           || n != 4)
25478         return NULL_TREE;
25479       break;
25480
25481     default:
25482       return NULL_TREE;
25483     }
25484
25485   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
25486   sprintf (name + 7, "%s", bname+10);
25487
25488   arity = 0;
25489   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
25490        args = TREE_CHAIN (args))
25491     arity++;
25492
25493   if (arity == 1)
25494     fntype = build_function_type_list (type_out, type_in, NULL);
25495   else
25496     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
25497
25498   /* Build a function declaration for the vectorized function.  */
25499   new_fndecl = build_decl (BUILTINS_LOCATION,
25500                            FUNCTION_DECL, get_identifier (name), fntype);
25501   TREE_PUBLIC (new_fndecl) = 1;
25502   DECL_EXTERNAL (new_fndecl) = 1;
25503   DECL_IS_NOVOPS (new_fndecl) = 1;
25504   TREE_READONLY (new_fndecl) = 1;
25505
25506   return new_fndecl;
25507 }
25508
25509
25510 /* Returns a decl of a function that implements conversion of an integer vector
25511    into a floating-point vector, or vice-versa.  DEST_TYPE and SRC_TYPE
25512    are the types involved when converting according to CODE.
25513    Return NULL_TREE if it is not available.  */
25514
25515 static tree
25516 ix86_vectorize_builtin_conversion (unsigned int code,
25517                                    tree dest_type, tree src_type)
25518 {
25519   if (! TARGET_SSE2)
25520     return NULL_TREE;
25521
25522   switch (code)
25523     {
25524     case FLOAT_EXPR:
25525       switch (TYPE_MODE (src_type))
25526         {
25527         case V4SImode:
25528           switch (TYPE_MODE (dest_type))
25529             {
25530             case V4SFmode:
25531               return (TYPE_UNSIGNED (src_type)
25532                       ? ix86_builtins[IX86_BUILTIN_CVTUDQ2PS]
25533                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS]);
25534             case V4DFmode:
25535               return (TYPE_UNSIGNED (src_type)
25536                       ? NULL_TREE
25537                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PD256]);
25538             default:
25539               return NULL_TREE;
25540             }
25541           break;
25542         case V8SImode:
25543           switch (TYPE_MODE (dest_type))
25544             {
25545             case V8SFmode:
25546               return (TYPE_UNSIGNED (src_type)
25547                       ? NULL_TREE
25548                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS]);
25549             default:
25550               return NULL_TREE;
25551             }
25552           break;
25553         default:
25554           return NULL_TREE;
25555         }
25556
25557     case FIX_TRUNC_EXPR:
25558       switch (TYPE_MODE (dest_type))
25559         {
25560         case V4SImode:
25561           switch (TYPE_MODE (src_type))
25562             {
25563             case V4SFmode:
25564               return (TYPE_UNSIGNED (dest_type)
25565                       ? NULL_TREE
25566                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ]);
25567             case V4DFmode:
25568               return (TYPE_UNSIGNED (dest_type)
25569                       ? NULL_TREE
25570                       : ix86_builtins[IX86_BUILTIN_CVTTPD2DQ256]);
25571             default:
25572               return NULL_TREE;
25573             }
25574           break;
25575
25576         case V8SImode:
25577           switch (TYPE_MODE (src_type))
25578             {
25579             case V8SFmode:
25580               return (TYPE_UNSIGNED (dest_type)
25581                       ? NULL_TREE
25582                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ256]);
25583             default:
25584               return NULL_TREE;
25585             }
25586           break;
25587
25588         default:
25589           return NULL_TREE;
25590         }
25591
25592     default:
25593       return NULL_TREE;
25594     }
25595
25596   return NULL_TREE;
25597 }
25598
25599 /* Returns a code for a target-specific builtin that implements
25600    reciprocal of the function, or NULL_TREE if not available.  */
25601
25602 static tree
25603 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
25604                          bool sqrt ATTRIBUTE_UNUSED)
25605 {
25606   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
25607          && flag_finite_math_only && !flag_trapping_math
25608          && flag_unsafe_math_optimizations))
25609     return NULL_TREE;
25610
25611   if (md_fn)
25612     /* Machine dependent builtins.  */
25613     switch (fn)
25614       {
25615         /* Vectorized version of sqrt to rsqrt conversion.  */
25616       case IX86_BUILTIN_SQRTPS_NR:
25617         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
25618
25619       default:
25620         return NULL_TREE;
25621       }
25622   else
25623     /* Normal builtins.  */
25624     switch (fn)
25625       {
25626         /* Sqrt to rsqrt conversion.  */
25627       case BUILT_IN_SQRTF:
25628         return ix86_builtins[IX86_BUILTIN_RSQRTF];
25629
25630       default:
25631         return NULL_TREE;
25632       }
25633 }
25634 \f
25635 /* Helper for avx_vpermilps256_operand et al.  This is also used by
25636    the expansion functions to turn the parallel back into a mask.
25637    The return value is 0 for no match and the imm8+1 for a match.  */
25638
25639 int
25640 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
25641 {
25642   unsigned i, nelt = GET_MODE_NUNITS (mode);
25643   unsigned mask = 0;
25644   unsigned char ipar[8];
25645
25646   if (XVECLEN (par, 0) != (int) nelt)
25647     return 0;
25648
25649   /* Validate that all of the elements are constants, and not totally
25650      out of range.  Copy the data into an integral array to make the
25651      subsequent checks easier.  */
25652   for (i = 0; i < nelt; ++i)
25653     {
25654       rtx er = XVECEXP (par, 0, i);
25655       unsigned HOST_WIDE_INT ei;
25656
25657       if (!CONST_INT_P (er))
25658         return 0;
25659       ei = INTVAL (er);
25660       if (ei >= nelt)
25661         return 0;
25662       ipar[i] = ei;
25663     }
25664
25665   switch (mode)
25666     {
25667     case V4DFmode:
25668       /* In the 256-bit DFmode case, we can only move elements within
25669          a 128-bit lane.  */
25670       for (i = 0; i < 2; ++i)
25671         {
25672           if (ipar[i] >= 2)
25673             return 0;
25674           mask |= ipar[i] << i;
25675         }
25676       for (i = 2; i < 4; ++i)
25677         {
25678           if (ipar[i] < 2)
25679             return 0;
25680           mask |= (ipar[i] - 2) << i;
25681         }
25682       break;
25683
25684     case V8SFmode:
25685       /* In the 256-bit SFmode case, we have full freedom of movement
25686          within the low 128-bit lane, but the high 128-bit lane must
25687          mirror the exact same pattern.  */
25688       for (i = 0; i < 4; ++i)
25689         if (ipar[i] + 4 != ipar[i + 4])
25690           return 0;
25691       nelt = 4;
25692       /* FALLTHRU */
25693
25694     case V2DFmode:
25695     case V4SFmode:
25696       /* In the 128-bit case, we've full freedom in the placement of
25697          the elements from the source operand.  */
25698       for (i = 0; i < nelt; ++i)
25699         mask |= ipar[i] << (i * (nelt / 2));
25700       break;
25701
25702     default:
25703       gcc_unreachable ();
25704     }
25705
25706   /* Make sure success has a non-zero value by adding one.  */
25707   return mask + 1;
25708 }
25709
25710 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
25711    the expansion functions to turn the parallel back into a mask.
25712    The return value is 0 for no match and the imm8+1 for a match.  */
25713
25714 int
25715 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
25716 {
25717   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
25718   unsigned mask = 0;
25719   unsigned char ipar[8];
25720
25721   if (XVECLEN (par, 0) != (int) nelt)
25722     return 0;
25723
25724   /* Validate that all of the elements are constants, and not totally
25725      out of range.  Copy the data into an integral array to make the
25726      subsequent checks easier.  */
25727   for (i = 0; i < nelt; ++i)
25728     {
25729       rtx er = XVECEXP (par, 0, i);
25730       unsigned HOST_WIDE_INT ei;
25731
25732       if (!CONST_INT_P (er))
25733         return 0;
25734       ei = INTVAL (er);
25735       if (ei >= 2 * nelt)
25736         return 0;
25737       ipar[i] = ei;
25738     }
25739
25740   /* Validate that the halves of the permute are halves.  */
25741   for (i = 0; i < nelt2 - 1; ++i)
25742     if (ipar[i] + 1 != ipar[i + 1])
25743       return 0;
25744   for (i = nelt2; i < nelt - 1; ++i)
25745     if (ipar[i] + 1 != ipar[i + 1])
25746       return 0;
25747
25748   /* Reconstruct the mask.  */
25749   for (i = 0; i < 2; ++i)
25750     {
25751       unsigned e = ipar[i * nelt2];
25752       if (e % nelt2)
25753         return 0;
25754       e /= nelt2;
25755       mask |= e << (i * 4);
25756     }
25757
25758   /* Make sure success has a non-zero value by adding one.  */
25759   return mask + 1;
25760 }
25761 \f
25762
25763 /* Store OPERAND to the memory after reload is completed.  This means
25764    that we can't easily use assign_stack_local.  */
25765 rtx
25766 ix86_force_to_memory (enum machine_mode mode, rtx operand)
25767 {
25768   rtx result;
25769
25770   gcc_assert (reload_completed);
25771   if (!TARGET_64BIT_MS_ABI && TARGET_RED_ZONE)
25772     {
25773       result = gen_rtx_MEM (mode,
25774                             gen_rtx_PLUS (Pmode,
25775                                           stack_pointer_rtx,
25776                                           GEN_INT (-RED_ZONE_SIZE)));
25777       emit_move_insn (result, operand);
25778     }
25779   else if ((TARGET_64BIT_MS_ABI || !TARGET_RED_ZONE) && TARGET_64BIT)
25780     {
25781       switch (mode)
25782         {
25783         case HImode:
25784         case SImode:
25785           operand = gen_lowpart (DImode, operand);
25786           /* FALLTHRU */
25787         case DImode:
25788           emit_insn (
25789                       gen_rtx_SET (VOIDmode,
25790                                    gen_rtx_MEM (DImode,
25791                                                 gen_rtx_PRE_DEC (DImode,
25792                                                         stack_pointer_rtx)),
25793                                    operand));
25794           break;
25795         default:
25796           gcc_unreachable ();
25797         }
25798       result = gen_rtx_MEM (mode, stack_pointer_rtx);
25799     }
25800   else
25801     {
25802       switch (mode)
25803         {
25804         case DImode:
25805           {
25806             rtx operands[2];
25807             split_di (&operand, 1, operands, operands + 1);
25808             emit_insn (
25809                         gen_rtx_SET (VOIDmode,
25810                                      gen_rtx_MEM (SImode,
25811                                                   gen_rtx_PRE_DEC (Pmode,
25812                                                         stack_pointer_rtx)),
25813                                      operands[1]));
25814             emit_insn (
25815                         gen_rtx_SET (VOIDmode,
25816                                      gen_rtx_MEM (SImode,
25817                                                   gen_rtx_PRE_DEC (Pmode,
25818                                                         stack_pointer_rtx)),
25819                                      operands[0]));
25820           }
25821           break;
25822         case HImode:
25823           /* Store HImodes as SImodes.  */
25824           operand = gen_lowpart (SImode, operand);
25825           /* FALLTHRU */
25826         case SImode:
25827           emit_insn (
25828                       gen_rtx_SET (VOIDmode,
25829                                    gen_rtx_MEM (GET_MODE (operand),
25830                                                 gen_rtx_PRE_DEC (SImode,
25831                                                         stack_pointer_rtx)),
25832                                    operand));
25833           break;
25834         default:
25835           gcc_unreachable ();
25836         }
25837       result = gen_rtx_MEM (mode, stack_pointer_rtx);
25838     }
25839   return result;
25840 }
25841
25842 /* Free operand from the memory.  */
25843 void
25844 ix86_free_from_memory (enum machine_mode mode)
25845 {
25846   if (!TARGET_RED_ZONE || TARGET_64BIT_MS_ABI)
25847     {
25848       int size;
25849
25850       if (mode == DImode || TARGET_64BIT)
25851         size = 8;
25852       else
25853         size = 4;
25854       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
25855          to pop or add instruction if registers are available.  */
25856       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
25857                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
25858                                             GEN_INT (size))));
25859     }
25860 }
25861
25862 /* Implement TARGET_IRA_COVER_CLASSES.  If -mfpmath=sse, we prefer
25863    SSE_REGS to FLOAT_REGS if their costs for a pseudo are the
25864    same.  */
25865 static const reg_class_t *
25866 i386_ira_cover_classes (void)
25867 {
25868   static const reg_class_t sse_fpmath_classes[] = {
25869     GENERAL_REGS, SSE_REGS, MMX_REGS, FLOAT_REGS, LIM_REG_CLASSES
25870   };
25871   static const reg_class_t no_sse_fpmath_classes[] = {
25872     GENERAL_REGS, FLOAT_REGS, MMX_REGS, SSE_REGS, LIM_REG_CLASSES
25873   };
25874
25875  return TARGET_SSE_MATH ? sse_fpmath_classes : no_sse_fpmath_classes;
25876 }
25877
25878 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
25879    QImode must go into class Q_REGS.
25880    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
25881    movdf to do mem-to-mem moves through integer regs.  */
25882 enum reg_class
25883 ix86_preferred_reload_class (rtx x, enum reg_class regclass)
25884 {
25885   enum machine_mode mode = GET_MODE (x);
25886
25887   /* We're only allowed to return a subclass of CLASS.  Many of the
25888      following checks fail for NO_REGS, so eliminate that early.  */
25889   if (regclass == NO_REGS)
25890     return NO_REGS;
25891
25892   /* All classes can load zeros.  */
25893   if (x == CONST0_RTX (mode))
25894     return regclass;
25895
25896   /* Force constants into memory if we are loading a (nonzero) constant into
25897      an MMX or SSE register.  This is because there are no MMX/SSE instructions
25898      to load from a constant.  */
25899   if (CONSTANT_P (x)
25900       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
25901     return NO_REGS;
25902
25903   /* Prefer SSE regs only, if we can use them for math.  */
25904   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
25905     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
25906
25907   /* Floating-point constants need more complex checks.  */
25908   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
25909     {
25910       /* General regs can load everything.  */
25911       if (reg_class_subset_p (regclass, GENERAL_REGS))
25912         return regclass;
25913
25914       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
25915          zero above.  We only want to wind up preferring 80387 registers if
25916          we plan on doing computation with them.  */
25917       if (TARGET_80387
25918           && standard_80387_constant_p (x))
25919         {
25920           /* Limit class to non-sse.  */
25921           if (regclass == FLOAT_SSE_REGS)
25922             return FLOAT_REGS;
25923           if (regclass == FP_TOP_SSE_REGS)
25924             return FP_TOP_REG;
25925           if (regclass == FP_SECOND_SSE_REGS)
25926             return FP_SECOND_REG;
25927           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
25928             return regclass;
25929         }
25930
25931       return NO_REGS;
25932     }
25933
25934   /* Generally when we see PLUS here, it's the function invariant
25935      (plus soft-fp const_int).  Which can only be computed into general
25936      regs.  */
25937   if (GET_CODE (x) == PLUS)
25938     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
25939
25940   /* QImode constants are easy to load, but non-constant QImode data
25941      must go into Q_REGS.  */
25942   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
25943     {
25944       if (reg_class_subset_p (regclass, Q_REGS))
25945         return regclass;
25946       if (reg_class_subset_p (Q_REGS, regclass))
25947         return Q_REGS;
25948       return NO_REGS;
25949     }
25950
25951   return regclass;
25952 }
25953
25954 /* Discourage putting floating-point values in SSE registers unless
25955    SSE math is being used, and likewise for the 387 registers.  */
25956 enum reg_class
25957 ix86_preferred_output_reload_class (rtx x, enum reg_class regclass)
25958 {
25959   enum machine_mode mode = GET_MODE (x);
25960
25961   /* Restrict the output reload class to the register bank that we are doing
25962      math on.  If we would like not to return a subset of CLASS, reject this
25963      alternative: if reload cannot do this, it will still use its choice.  */
25964   mode = GET_MODE (x);
25965   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
25966     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
25967
25968   if (X87_FLOAT_MODE_P (mode))
25969     {
25970       if (regclass == FP_TOP_SSE_REGS)
25971         return FP_TOP_REG;
25972       else if (regclass == FP_SECOND_SSE_REGS)
25973         return FP_SECOND_REG;
25974       else
25975         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
25976     }
25977
25978   return regclass;
25979 }
25980
25981 static reg_class_t
25982 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
25983                        enum machine_mode mode,
25984                        secondary_reload_info *sri ATTRIBUTE_UNUSED)
25985 {
25986   /* QImode spills from non-QI registers require
25987      intermediate register on 32bit targets.  */
25988   if (!in_p && mode == QImode && !TARGET_64BIT
25989       && (rclass == GENERAL_REGS
25990           || rclass == LEGACY_REGS
25991           || rclass == INDEX_REGS))
25992     {
25993       int regno;
25994
25995       if (REG_P (x))
25996         regno = REGNO (x);
25997       else
25998         regno = -1;
25999
26000       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
26001         regno = true_regnum (x);
26002
26003       /* Return Q_REGS if the operand is in memory.  */
26004       if (regno == -1)
26005         return Q_REGS;
26006     }
26007
26008   return NO_REGS;
26009 }
26010
26011 /* If we are copying between general and FP registers, we need a memory
26012    location. The same is true for SSE and MMX registers.
26013
26014    To optimize register_move_cost performance, allow inline variant.
26015
26016    The macro can't work reliably when one of the CLASSES is class containing
26017    registers from multiple units (SSE, MMX, integer).  We avoid this by never
26018    combining those units in single alternative in the machine description.
26019    Ensure that this constraint holds to avoid unexpected surprises.
26020
26021    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
26022    enforce these sanity checks.  */
26023
26024 static inline int
26025 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
26026                               enum machine_mode mode, int strict)
26027 {
26028   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
26029       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
26030       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
26031       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
26032       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
26033       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
26034     {
26035       gcc_assert (!strict);
26036       return true;
26037     }
26038
26039   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
26040     return true;
26041
26042   /* ??? This is a lie.  We do have moves between mmx/general, and for
26043      mmx/sse2.  But by saying we need secondary memory we discourage the
26044      register allocator from using the mmx registers unless needed.  */
26045   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
26046     return true;
26047
26048   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
26049     {
26050       /* SSE1 doesn't have any direct moves from other classes.  */
26051       if (!TARGET_SSE2)
26052         return true;
26053
26054       /* If the target says that inter-unit moves are more expensive
26055          than moving through memory, then don't generate them.  */
26056       if (!TARGET_INTER_UNIT_MOVES)
26057         return true;
26058
26059       /* Between SSE and general, we have moves no larger than word size.  */
26060       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
26061         return true;
26062     }
26063
26064   return false;
26065 }
26066
26067 int
26068 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
26069                               enum machine_mode mode, int strict)
26070 {
26071   return inline_secondary_memory_needed (class1, class2, mode, strict);
26072 }
26073
26074 /* Return true if the registers in CLASS cannot represent the change from
26075    modes FROM to TO.  */
26076
26077 bool
26078 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
26079                                enum reg_class regclass)
26080 {
26081   if (from == to)
26082     return false;
26083
26084   /* x87 registers can't do subreg at all, as all values are reformatted
26085      to extended precision.  */
26086   if (MAYBE_FLOAT_CLASS_P (regclass))
26087     return true;
26088
26089   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
26090     {
26091       /* Vector registers do not support QI or HImode loads.  If we don't
26092          disallow a change to these modes, reload will assume it's ok to
26093          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
26094          the vec_dupv4hi pattern.  */
26095       if (GET_MODE_SIZE (from) < 4)
26096         return true;
26097
26098       /* Vector registers do not support subreg with nonzero offsets, which
26099          are otherwise valid for integer registers.  Since we can't see
26100          whether we have a nonzero offset from here, prohibit all
26101          nonparadoxical subregs changing size.  */
26102       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
26103         return true;
26104     }
26105
26106   return false;
26107 }
26108
26109 /* Return the cost of moving data of mode M between a
26110    register and memory.  A value of 2 is the default; this cost is
26111    relative to those in `REGISTER_MOVE_COST'.
26112
26113    This function is used extensively by register_move_cost that is used to
26114    build tables at startup.  Make it inline in this case.
26115    When IN is 2, return maximum of in and out move cost.
26116
26117    If moving between registers and memory is more expensive than
26118    between two registers, you should define this macro to express the
26119    relative cost.
26120
26121    Model also increased moving costs of QImode registers in non
26122    Q_REGS classes.
26123  */
26124 static inline int
26125 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
26126                          int in)
26127 {
26128   int cost;
26129   if (FLOAT_CLASS_P (regclass))
26130     {
26131       int index;
26132       switch (mode)
26133         {
26134           case SFmode:
26135             index = 0;
26136             break;
26137           case DFmode:
26138             index = 1;
26139             break;
26140           case XFmode:
26141             index = 2;
26142             break;
26143           default:
26144             return 100;
26145         }
26146       if (in == 2)
26147         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
26148       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
26149     }
26150   if (SSE_CLASS_P (regclass))
26151     {
26152       int index;
26153       switch (GET_MODE_SIZE (mode))
26154         {
26155           case 4:
26156             index = 0;
26157             break;
26158           case 8:
26159             index = 1;
26160             break;
26161           case 16:
26162             index = 2;
26163             break;
26164           default:
26165             return 100;
26166         }
26167       if (in == 2)
26168         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
26169       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
26170     }
26171   if (MMX_CLASS_P (regclass))
26172     {
26173       int index;
26174       switch (GET_MODE_SIZE (mode))
26175         {
26176           case 4:
26177             index = 0;
26178             break;
26179           case 8:
26180             index = 1;
26181             break;
26182           default:
26183             return 100;
26184         }
26185       if (in)
26186         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
26187       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
26188     }
26189   switch (GET_MODE_SIZE (mode))
26190     {
26191       case 1:
26192         if (Q_CLASS_P (regclass) || TARGET_64BIT)
26193           {
26194             if (!in)
26195               return ix86_cost->int_store[0];
26196             if (TARGET_PARTIAL_REG_DEPENDENCY
26197                 && optimize_function_for_speed_p (cfun))
26198               cost = ix86_cost->movzbl_load;
26199             else
26200               cost = ix86_cost->int_load[0];
26201             if (in == 2)
26202               return MAX (cost, ix86_cost->int_store[0]);
26203             return cost;
26204           }
26205         else
26206           {
26207            if (in == 2)
26208              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
26209            if (in)
26210              return ix86_cost->movzbl_load;
26211            else
26212              return ix86_cost->int_store[0] + 4;
26213           }
26214         break;
26215       case 2:
26216         if (in == 2)
26217           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
26218         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
26219       default:
26220         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
26221         if (mode == TFmode)
26222           mode = XFmode;
26223         if (in == 2)
26224           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
26225         else if (in)
26226           cost = ix86_cost->int_load[2];
26227         else
26228           cost = ix86_cost->int_store[2];
26229         return (cost * (((int) GET_MODE_SIZE (mode)
26230                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
26231     }
26232 }
26233
26234 static int
26235 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
26236                        bool in)
26237 {
26238   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
26239 }
26240
26241
26242 /* Return the cost of moving data from a register in class CLASS1 to
26243    one in class CLASS2.
26244
26245    It is not required that the cost always equal 2 when FROM is the same as TO;
26246    on some machines it is expensive to move between registers if they are not
26247    general registers.  */
26248
26249 static int
26250 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
26251                          reg_class_t class2_i)
26252 {
26253   enum reg_class class1 = (enum reg_class) class1_i;
26254   enum reg_class class2 = (enum reg_class) class2_i;
26255
26256   /* In case we require secondary memory, compute cost of the store followed
26257      by load.  In order to avoid bad register allocation choices, we need
26258      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
26259
26260   if (inline_secondary_memory_needed (class1, class2, mode, 0))
26261     {
26262       int cost = 1;
26263
26264       cost += inline_memory_move_cost (mode, class1, 2);
26265       cost += inline_memory_move_cost (mode, class2, 2);
26266
26267       /* In case of copying from general_purpose_register we may emit multiple
26268          stores followed by single load causing memory size mismatch stall.
26269          Count this as arbitrarily high cost of 20.  */
26270       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
26271         cost += 20;
26272
26273       /* In the case of FP/MMX moves, the registers actually overlap, and we
26274          have to switch modes in order to treat them differently.  */
26275       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
26276           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
26277         cost += 20;
26278
26279       return cost;
26280     }
26281
26282   /* Moves between SSE/MMX and integer unit are expensive.  */
26283   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
26284       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
26285
26286     /* ??? By keeping returned value relatively high, we limit the number
26287        of moves between integer and MMX/SSE registers for all targets.
26288        Additionally, high value prevents problem with x86_modes_tieable_p(),
26289        where integer modes in MMX/SSE registers are not tieable
26290        because of missing QImode and HImode moves to, from or between
26291        MMX/SSE registers.  */
26292     return MAX (8, ix86_cost->mmxsse_to_integer);
26293
26294   if (MAYBE_FLOAT_CLASS_P (class1))
26295     return ix86_cost->fp_move;
26296   if (MAYBE_SSE_CLASS_P (class1))
26297     return ix86_cost->sse_move;
26298   if (MAYBE_MMX_CLASS_P (class1))
26299     return ix86_cost->mmx_move;
26300   return 2;
26301 }
26302
26303 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
26304
26305 bool
26306 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
26307 {
26308   /* Flags and only flags can only hold CCmode values.  */
26309   if (CC_REGNO_P (regno))
26310     return GET_MODE_CLASS (mode) == MODE_CC;
26311   if (GET_MODE_CLASS (mode) == MODE_CC
26312       || GET_MODE_CLASS (mode) == MODE_RANDOM
26313       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
26314     return 0;
26315   if (FP_REGNO_P (regno))
26316     return VALID_FP_MODE_P (mode);
26317   if (SSE_REGNO_P (regno))
26318     {
26319       /* We implement the move patterns for all vector modes into and
26320          out of SSE registers, even when no operation instructions
26321          are available.  OImode move is available only when AVX is
26322          enabled.  */
26323       return ((TARGET_AVX && mode == OImode)
26324               || VALID_AVX256_REG_MODE (mode)
26325               || VALID_SSE_REG_MODE (mode)
26326               || VALID_SSE2_REG_MODE (mode)
26327               || VALID_MMX_REG_MODE (mode)
26328               || VALID_MMX_REG_MODE_3DNOW (mode));
26329     }
26330   if (MMX_REGNO_P (regno))
26331     {
26332       /* We implement the move patterns for 3DNOW modes even in MMX mode,
26333          so if the register is available at all, then we can move data of
26334          the given mode into or out of it.  */
26335       return (VALID_MMX_REG_MODE (mode)
26336               || VALID_MMX_REG_MODE_3DNOW (mode));
26337     }
26338
26339   if (mode == QImode)
26340     {
26341       /* Take care for QImode values - they can be in non-QI regs,
26342          but then they do cause partial register stalls.  */
26343       if (regno <= BX_REG || TARGET_64BIT)
26344         return 1;
26345       if (!TARGET_PARTIAL_REG_STALL)
26346         return 1;
26347       return reload_in_progress || reload_completed;
26348     }
26349   /* We handle both integer and floats in the general purpose registers.  */
26350   else if (VALID_INT_MODE_P (mode))
26351     return 1;
26352   else if (VALID_FP_MODE_P (mode))
26353     return 1;
26354   else if (VALID_DFP_MODE_P (mode))
26355     return 1;
26356   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
26357      on to use that value in smaller contexts, this can easily force a
26358      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
26359      supporting DImode, allow it.  */
26360   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
26361     return 1;
26362
26363   return 0;
26364 }
26365
26366 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
26367    tieable integer mode.  */
26368
26369 static bool
26370 ix86_tieable_integer_mode_p (enum machine_mode mode)
26371 {
26372   switch (mode)
26373     {
26374     case HImode:
26375     case SImode:
26376       return true;
26377
26378     case QImode:
26379       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
26380
26381     case DImode:
26382       return TARGET_64BIT;
26383
26384     default:
26385       return false;
26386     }
26387 }
26388
26389 /* Return true if MODE1 is accessible in a register that can hold MODE2
26390    without copying.  That is, all register classes that can hold MODE2
26391    can also hold MODE1.  */
26392
26393 bool
26394 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
26395 {
26396   if (mode1 == mode2)
26397     return true;
26398
26399   if (ix86_tieable_integer_mode_p (mode1)
26400       && ix86_tieable_integer_mode_p (mode2))
26401     return true;
26402
26403   /* MODE2 being XFmode implies fp stack or general regs, which means we
26404      can tie any smaller floating point modes to it.  Note that we do not
26405      tie this with TFmode.  */
26406   if (mode2 == XFmode)
26407     return mode1 == SFmode || mode1 == DFmode;
26408
26409   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
26410      that we can tie it with SFmode.  */
26411   if (mode2 == DFmode)
26412     return mode1 == SFmode;
26413
26414   /* If MODE2 is only appropriate for an SSE register, then tie with
26415      any other mode acceptable to SSE registers.  */
26416   if (GET_MODE_SIZE (mode2) == 16
26417       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
26418     return (GET_MODE_SIZE (mode1) == 16
26419             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
26420
26421   /* If MODE2 is appropriate for an MMX register, then tie
26422      with any other mode acceptable to MMX registers.  */
26423   if (GET_MODE_SIZE (mode2) == 8
26424       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
26425     return (GET_MODE_SIZE (mode1) == 8
26426             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
26427
26428   return false;
26429 }
26430
26431 /* Compute a (partial) cost for rtx X.  Return true if the complete
26432    cost has been computed, and false if subexpressions should be
26433    scanned.  In either case, *TOTAL contains the cost result.  */
26434
26435 static bool
26436 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total, bool speed)
26437 {
26438   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
26439   enum machine_mode mode = GET_MODE (x);
26440   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
26441
26442   switch (code)
26443     {
26444     case CONST_INT:
26445     case CONST:
26446     case LABEL_REF:
26447     case SYMBOL_REF:
26448       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
26449         *total = 3;
26450       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
26451         *total = 2;
26452       else if (flag_pic && SYMBOLIC_CONST (x)
26453                && (!TARGET_64BIT
26454                    || (!GET_CODE (x) != LABEL_REF
26455                        && (GET_CODE (x) != SYMBOL_REF
26456                            || !SYMBOL_REF_LOCAL_P (x)))))
26457         *total = 1;
26458       else
26459         *total = 0;
26460       return true;
26461
26462     case CONST_DOUBLE:
26463       if (mode == VOIDmode)
26464         *total = 0;
26465       else
26466         switch (standard_80387_constant_p (x))
26467           {
26468           case 1: /* 0.0 */
26469             *total = 1;
26470             break;
26471           default: /* Other constants */
26472             *total = 2;
26473             break;
26474           case 0:
26475           case -1:
26476             /* Start with (MEM (SYMBOL_REF)), since that's where
26477                it'll probably end up.  Add a penalty for size.  */
26478             *total = (COSTS_N_INSNS (1)
26479                       + (flag_pic != 0 && !TARGET_64BIT)
26480                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
26481             break;
26482           }
26483       return true;
26484
26485     case ZERO_EXTEND:
26486       /* The zero extensions is often completely free on x86_64, so make
26487          it as cheap as possible.  */
26488       if (TARGET_64BIT && mode == DImode
26489           && GET_MODE (XEXP (x, 0)) == SImode)
26490         *total = 1;
26491       else if (TARGET_ZERO_EXTEND_WITH_AND)
26492         *total = cost->add;
26493       else
26494         *total = cost->movzx;
26495       return false;
26496
26497     case SIGN_EXTEND:
26498       *total = cost->movsx;
26499       return false;
26500
26501     case ASHIFT:
26502       if (CONST_INT_P (XEXP (x, 1))
26503           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
26504         {
26505           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
26506           if (value == 1)
26507             {
26508               *total = cost->add;
26509               return false;
26510             }
26511           if ((value == 2 || value == 3)
26512               && cost->lea <= cost->shift_const)
26513             {
26514               *total = cost->lea;
26515               return false;
26516             }
26517         }
26518       /* FALLTHRU */
26519
26520     case ROTATE:
26521     case ASHIFTRT:
26522     case LSHIFTRT:
26523     case ROTATERT:
26524       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
26525         {
26526           if (CONST_INT_P (XEXP (x, 1)))
26527             {
26528               if (INTVAL (XEXP (x, 1)) > 32)
26529                 *total = cost->shift_const + COSTS_N_INSNS (2);
26530               else
26531                 *total = cost->shift_const * 2;
26532             }
26533           else
26534             {
26535               if (GET_CODE (XEXP (x, 1)) == AND)
26536                 *total = cost->shift_var * 2;
26537               else
26538                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
26539             }
26540         }
26541       else
26542         {
26543           if (CONST_INT_P (XEXP (x, 1)))
26544             *total = cost->shift_const;
26545           else
26546             *total = cost->shift_var;
26547         }
26548       return false;
26549
26550     case MULT:
26551       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26552         {
26553           /* ??? SSE scalar cost should be used here.  */
26554           *total = cost->fmul;
26555           return false;
26556         }
26557       else if (X87_FLOAT_MODE_P (mode))
26558         {
26559           *total = cost->fmul;
26560           return false;
26561         }
26562       else if (FLOAT_MODE_P (mode))
26563         {
26564           /* ??? SSE vector cost should be used here.  */
26565           *total = cost->fmul;
26566           return false;
26567         }
26568       else
26569         {
26570           rtx op0 = XEXP (x, 0);
26571           rtx op1 = XEXP (x, 1);
26572           int nbits;
26573           if (CONST_INT_P (XEXP (x, 1)))
26574             {
26575               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
26576               for (nbits = 0; value != 0; value &= value - 1)
26577                 nbits++;
26578             }
26579           else
26580             /* This is arbitrary.  */
26581             nbits = 7;
26582
26583           /* Compute costs correctly for widening multiplication.  */
26584           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
26585               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
26586                  == GET_MODE_SIZE (mode))
26587             {
26588               int is_mulwiden = 0;
26589               enum machine_mode inner_mode = GET_MODE (op0);
26590
26591               if (GET_CODE (op0) == GET_CODE (op1))
26592                 is_mulwiden = 1, op1 = XEXP (op1, 0);
26593               else if (CONST_INT_P (op1))
26594                 {
26595                   if (GET_CODE (op0) == SIGN_EXTEND)
26596                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
26597                                   == INTVAL (op1);
26598                   else
26599                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
26600                 }
26601
26602               if (is_mulwiden)
26603                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
26604             }
26605
26606           *total = (cost->mult_init[MODE_INDEX (mode)]
26607                     + nbits * cost->mult_bit
26608                     + rtx_cost (op0, outer_code, speed) + rtx_cost (op1, outer_code, speed));
26609
26610           return true;
26611         }
26612
26613     case DIV:
26614     case UDIV:
26615     case MOD:
26616     case UMOD:
26617       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26618         /* ??? SSE cost should be used here.  */
26619         *total = cost->fdiv;
26620       else if (X87_FLOAT_MODE_P (mode))
26621         *total = cost->fdiv;
26622       else if (FLOAT_MODE_P (mode))
26623         /* ??? SSE vector cost should be used here.  */
26624         *total = cost->fdiv;
26625       else
26626         *total = cost->divide[MODE_INDEX (mode)];
26627       return false;
26628
26629     case PLUS:
26630       if (GET_MODE_CLASS (mode) == MODE_INT
26631                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
26632         {
26633           if (GET_CODE (XEXP (x, 0)) == PLUS
26634               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
26635               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
26636               && CONSTANT_P (XEXP (x, 1)))
26637             {
26638               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
26639               if (val == 2 || val == 4 || val == 8)
26640                 {
26641                   *total = cost->lea;
26642                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
26643                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
26644                                       outer_code, speed);
26645                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
26646                   return true;
26647                 }
26648             }
26649           else if (GET_CODE (XEXP (x, 0)) == MULT
26650                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
26651             {
26652               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
26653               if (val == 2 || val == 4 || val == 8)
26654                 {
26655                   *total = cost->lea;
26656                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
26657                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
26658                   return true;
26659                 }
26660             }
26661           else if (GET_CODE (XEXP (x, 0)) == PLUS)
26662             {
26663               *total = cost->lea;
26664               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
26665               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
26666               *total += rtx_cost (XEXP (x, 1), outer_code, speed);
26667               return true;
26668             }
26669         }
26670       /* FALLTHRU */
26671
26672     case MINUS:
26673       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26674         {
26675           /* ??? SSE cost should be used here.  */
26676           *total = cost->fadd;
26677           return false;
26678         }
26679       else if (X87_FLOAT_MODE_P (mode))
26680         {
26681           *total = cost->fadd;
26682           return false;
26683         }
26684       else if (FLOAT_MODE_P (mode))
26685         {
26686           /* ??? SSE vector cost should be used here.  */
26687           *total = cost->fadd;
26688           return false;
26689         }
26690       /* FALLTHRU */
26691
26692     case AND:
26693     case IOR:
26694     case XOR:
26695       if (!TARGET_64BIT && mode == DImode)
26696         {
26697           *total = (cost->add * 2
26698                     + (rtx_cost (XEXP (x, 0), outer_code, speed)
26699                        << (GET_MODE (XEXP (x, 0)) != DImode))
26700                     + (rtx_cost (XEXP (x, 1), outer_code, speed)
26701                        << (GET_MODE (XEXP (x, 1)) != DImode)));
26702           return true;
26703         }
26704       /* FALLTHRU */
26705
26706     case NEG:
26707       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26708         {
26709           /* ??? SSE cost should be used here.  */
26710           *total = cost->fchs;
26711           return false;
26712         }
26713       else if (X87_FLOAT_MODE_P (mode))
26714         {
26715           *total = cost->fchs;
26716           return false;
26717         }
26718       else if (FLOAT_MODE_P (mode))
26719         {
26720           /* ??? SSE vector cost should be used here.  */
26721           *total = cost->fchs;
26722           return false;
26723         }
26724       /* FALLTHRU */
26725
26726     case NOT:
26727       if (!TARGET_64BIT && mode == DImode)
26728         *total = cost->add * 2;
26729       else
26730         *total = cost->add;
26731       return false;
26732
26733     case COMPARE:
26734       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
26735           && XEXP (XEXP (x, 0), 1) == const1_rtx
26736           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
26737           && XEXP (x, 1) == const0_rtx)
26738         {
26739           /* This kind of construct is implemented using test[bwl].
26740              Treat it as if we had an AND.  */
26741           *total = (cost->add
26742                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed)
26743                     + rtx_cost (const1_rtx, outer_code, speed));
26744           return true;
26745         }
26746       return false;
26747
26748     case FLOAT_EXTEND:
26749       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
26750         *total = 0;
26751       return false;
26752
26753     case ABS:
26754       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26755         /* ??? SSE cost should be used here.  */
26756         *total = cost->fabs;
26757       else if (X87_FLOAT_MODE_P (mode))
26758         *total = cost->fabs;
26759       else if (FLOAT_MODE_P (mode))
26760         /* ??? SSE vector cost should be used here.  */
26761         *total = cost->fabs;
26762       return false;
26763
26764     case SQRT:
26765       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
26766         /* ??? SSE cost should be used here.  */
26767         *total = cost->fsqrt;
26768       else if (X87_FLOAT_MODE_P (mode))
26769         *total = cost->fsqrt;
26770       else if (FLOAT_MODE_P (mode))
26771         /* ??? SSE vector cost should be used here.  */
26772         *total = cost->fsqrt;
26773       return false;
26774
26775     case UNSPEC:
26776       if (XINT (x, 1) == UNSPEC_TP)
26777         *total = 0;
26778       return false;
26779
26780     case VEC_SELECT:
26781     case VEC_CONCAT:
26782     case VEC_MERGE:
26783     case VEC_DUPLICATE:
26784       /* ??? Assume all of these vector manipulation patterns are
26785          recognizable.  In which case they all pretty much have the
26786          same cost.  */
26787      *total = COSTS_N_INSNS (1);
26788      return true;
26789
26790     default:
26791       return false;
26792     }
26793 }
26794
26795 #if TARGET_MACHO
26796
26797 static int current_machopic_label_num;
26798
26799 /* Given a symbol name and its associated stub, write out the
26800    definition of the stub.  */
26801
26802 void
26803 machopic_output_stub (FILE *file, const char *symb, const char *stub)
26804 {
26805   unsigned int length;
26806   char *binder_name, *symbol_name, lazy_ptr_name[32];
26807   int label = ++current_machopic_label_num;
26808
26809   /* For 64-bit we shouldn't get here.  */
26810   gcc_assert (!TARGET_64BIT);
26811
26812   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
26813   symb = targetm.strip_name_encoding (symb);
26814
26815   length = strlen (stub);
26816   binder_name = XALLOCAVEC (char, length + 32);
26817   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
26818
26819   length = strlen (symb);
26820   symbol_name = XALLOCAVEC (char, length + 32);
26821   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
26822
26823   sprintf (lazy_ptr_name, "L%d$lz", label);
26824
26825   if (MACHOPIC_PURE)
26826     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
26827   else
26828     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
26829
26830   fprintf (file, "%s:\n", stub);
26831   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26832
26833   if (MACHOPIC_PURE)
26834     {
26835       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
26836       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
26837       fprintf (file, "\tjmp\t*%%edx\n");
26838     }
26839   else
26840     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
26841
26842   fprintf (file, "%s:\n", binder_name);
26843
26844   if (MACHOPIC_PURE)
26845     {
26846       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
26847       fputs ("\tpushl\t%eax\n", file);
26848     }
26849   else
26850     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
26851
26852   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
26853
26854   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
26855   fprintf (file, "%s:\n", lazy_ptr_name);
26856   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
26857   fprintf (file, ASM_LONG "%s\n", binder_name);
26858 }
26859 #endif /* TARGET_MACHO */
26860
26861 /* Order the registers for register allocator.  */
26862
26863 void
26864 x86_order_regs_for_local_alloc (void)
26865 {
26866    int pos = 0;
26867    int i;
26868
26869    /* First allocate the local general purpose registers.  */
26870    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
26871      if (GENERAL_REGNO_P (i) && call_used_regs[i])
26872         reg_alloc_order [pos++] = i;
26873
26874    /* Global general purpose registers.  */
26875    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
26876      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
26877         reg_alloc_order [pos++] = i;
26878
26879    /* x87 registers come first in case we are doing FP math
26880       using them.  */
26881    if (!TARGET_SSE_MATH)
26882      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
26883        reg_alloc_order [pos++] = i;
26884
26885    /* SSE registers.  */
26886    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
26887      reg_alloc_order [pos++] = i;
26888    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
26889      reg_alloc_order [pos++] = i;
26890
26891    /* x87 registers.  */
26892    if (TARGET_SSE_MATH)
26893      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
26894        reg_alloc_order [pos++] = i;
26895
26896    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
26897      reg_alloc_order [pos++] = i;
26898
26899    /* Initialize the rest of array as we do not allocate some registers
26900       at all.  */
26901    while (pos < FIRST_PSEUDO_REGISTER)
26902      reg_alloc_order [pos++] = 0;
26903 }
26904
26905 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
26906    struct attribute_spec.handler.  */
26907 static tree
26908 ix86_handle_abi_attribute (tree *node, tree name,
26909                               tree args ATTRIBUTE_UNUSED,
26910                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26911 {
26912   if (TREE_CODE (*node) != FUNCTION_TYPE
26913       && TREE_CODE (*node) != METHOD_TYPE
26914       && TREE_CODE (*node) != FIELD_DECL
26915       && TREE_CODE (*node) != TYPE_DECL)
26916     {
26917       warning (OPT_Wattributes, "%qE attribute only applies to functions",
26918                name);
26919       *no_add_attrs = true;
26920       return NULL_TREE;
26921     }
26922   if (!TARGET_64BIT)
26923     {
26924       warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
26925                name);
26926       *no_add_attrs = true;
26927       return NULL_TREE;
26928     }
26929
26930   /* Can combine regparm with all attributes but fastcall.  */
26931   if (is_attribute_p ("ms_abi", name))
26932     {
26933       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
26934         {
26935           error ("ms_abi and sysv_abi attributes are not compatible");
26936         }
26937
26938       return NULL_TREE;
26939     }
26940   else if (is_attribute_p ("sysv_abi", name))
26941     {
26942       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
26943         {
26944           error ("ms_abi and sysv_abi attributes are not compatible");
26945         }
26946
26947       return NULL_TREE;
26948     }
26949
26950   return NULL_TREE;
26951 }
26952
26953 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
26954    struct attribute_spec.handler.  */
26955 static tree
26956 ix86_handle_struct_attribute (tree *node, tree name,
26957                               tree args ATTRIBUTE_UNUSED,
26958                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26959 {
26960   tree *type = NULL;
26961   if (DECL_P (*node))
26962     {
26963       if (TREE_CODE (*node) == TYPE_DECL)
26964         type = &TREE_TYPE (*node);
26965     }
26966   else
26967     type = node;
26968
26969   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
26970                  || TREE_CODE (*type) == UNION_TYPE)))
26971     {
26972       warning (OPT_Wattributes, "%qE attribute ignored",
26973                name);
26974       *no_add_attrs = true;
26975     }
26976
26977   else if ((is_attribute_p ("ms_struct", name)
26978             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
26979            || ((is_attribute_p ("gcc_struct", name)
26980                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
26981     {
26982       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
26983                name);
26984       *no_add_attrs = true;
26985     }
26986
26987   return NULL_TREE;
26988 }
26989
26990 static tree
26991 ix86_handle_fndecl_attribute (tree *node, tree name,
26992                               tree args ATTRIBUTE_UNUSED,
26993                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
26994 {
26995   if (TREE_CODE (*node) != FUNCTION_DECL)
26996     {
26997       warning (OPT_Wattributes, "%qE attribute only applies to functions",
26998                name);
26999       *no_add_attrs = true;
27000       return NULL_TREE;
27001     }
27002
27003 #ifndef HAVE_AS_IX86_SWAP
27004   if (!TARGET_64BIT)
27005     sorry ("ms_hook_prologue attribute needs assembler swap suffix support");
27006 #endif
27007
27008     return NULL_TREE;
27009 }
27010
27011 static bool
27012 ix86_ms_bitfield_layout_p (const_tree record_type)
27013 {
27014   return (TARGET_MS_BITFIELD_LAYOUT &&
27015           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
27016     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
27017 }
27018
27019 /* Returns an expression indicating where the this parameter is
27020    located on entry to the FUNCTION.  */
27021
27022 static rtx
27023 x86_this_parameter (tree function)
27024 {
27025   tree type = TREE_TYPE (function);
27026   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
27027   int nregs;
27028
27029   if (TARGET_64BIT)
27030     {
27031       const int *parm_regs;
27032
27033       if (ix86_function_type_abi (type) == MS_ABI)
27034         parm_regs = x86_64_ms_abi_int_parameter_registers;
27035       else
27036         parm_regs = x86_64_int_parameter_registers;
27037       return gen_rtx_REG (DImode, parm_regs[aggr]);
27038     }
27039
27040   nregs = ix86_function_regparm (type, function);
27041
27042   if (nregs > 0 && !stdarg_p (type))
27043     {
27044       int regno;
27045
27046       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
27047         regno = aggr ? DX_REG : CX_REG;
27048       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
27049         {
27050           regno = CX_REG;
27051           if (aggr)
27052             return gen_rtx_MEM (SImode,
27053                                 plus_constant (stack_pointer_rtx, 4));
27054         }
27055       else
27056         {
27057           regno = AX_REG;
27058           if (aggr)
27059             {
27060               regno = DX_REG;
27061               if (nregs == 1)
27062                 return gen_rtx_MEM (SImode,
27063                                     plus_constant (stack_pointer_rtx, 4));
27064             }
27065         }
27066       return gen_rtx_REG (SImode, regno);
27067     }
27068
27069   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
27070 }
27071
27072 /* Determine whether x86_output_mi_thunk can succeed.  */
27073
27074 static bool
27075 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
27076                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
27077                          HOST_WIDE_INT vcall_offset, const_tree function)
27078 {
27079   /* 64-bit can handle anything.  */
27080   if (TARGET_64BIT)
27081     return true;
27082
27083   /* For 32-bit, everything's fine if we have one free register.  */
27084   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
27085     return true;
27086
27087   /* Need a free register for vcall_offset.  */
27088   if (vcall_offset)
27089     return false;
27090
27091   /* Need a free register for GOT references.  */
27092   if (flag_pic && !targetm.binds_local_p (function))
27093     return false;
27094
27095   /* Otherwise ok.  */
27096   return true;
27097 }
27098
27099 /* Output the assembler code for a thunk function.  THUNK_DECL is the
27100    declaration for the thunk function itself, FUNCTION is the decl for
27101    the target function.  DELTA is an immediate constant offset to be
27102    added to THIS.  If VCALL_OFFSET is nonzero, the word at
27103    *(*this + vcall_offset) should be added to THIS.  */
27104
27105 static void
27106 x86_output_mi_thunk (FILE *file,
27107                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
27108                      HOST_WIDE_INT vcall_offset, tree function)
27109 {
27110   rtx xops[3];
27111   rtx this_param = x86_this_parameter (function);
27112   rtx this_reg, tmp;
27113
27114   /* Make sure unwind info is emitted for the thunk if needed.  */
27115   final_start_function (emit_barrier (), file, 1);
27116
27117   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
27118      pull it in now and let DELTA benefit.  */
27119   if (REG_P (this_param))
27120     this_reg = this_param;
27121   else if (vcall_offset)
27122     {
27123       /* Put the this parameter into %eax.  */
27124       xops[0] = this_param;
27125       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
27126       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
27127     }
27128   else
27129     this_reg = NULL_RTX;
27130
27131   /* Adjust the this parameter by a fixed constant.  */
27132   if (delta)
27133     {
27134       xops[0] = GEN_INT (delta);
27135       xops[1] = this_reg ? this_reg : this_param;
27136       if (TARGET_64BIT)
27137         {
27138           if (!x86_64_general_operand (xops[0], DImode))
27139             {
27140               tmp = gen_rtx_REG (DImode, R10_REG);
27141               xops[1] = tmp;
27142               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
27143               xops[0] = tmp;
27144               xops[1] = this_param;
27145             }
27146           if (x86_maybe_negate_const_int (&xops[0], DImode))
27147             output_asm_insn ("sub{q}\t{%0, %1|%1, %0}", xops);
27148           else
27149             output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
27150         }
27151       else if (x86_maybe_negate_const_int (&xops[0], SImode))
27152         output_asm_insn ("sub{l}\t{%0, %1|%1, %0}", xops);
27153       else
27154         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
27155     }
27156
27157   /* Adjust the this parameter by a value stored in the vtable.  */
27158   if (vcall_offset)
27159     {
27160       if (TARGET_64BIT)
27161         tmp = gen_rtx_REG (DImode, R10_REG);
27162       else
27163         {
27164           int tmp_regno = CX_REG;
27165           if (lookup_attribute ("fastcall",
27166                                 TYPE_ATTRIBUTES (TREE_TYPE (function)))
27167               || lookup_attribute ("thiscall",
27168                                    TYPE_ATTRIBUTES (TREE_TYPE (function))))
27169             tmp_regno = AX_REG;
27170           tmp = gen_rtx_REG (SImode, tmp_regno);
27171         }
27172
27173       xops[0] = gen_rtx_MEM (Pmode, this_reg);
27174       xops[1] = tmp;
27175       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
27176
27177       /* Adjust the this parameter.  */
27178       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
27179       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
27180         {
27181           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
27182           xops[0] = GEN_INT (vcall_offset);
27183           xops[1] = tmp2;
27184           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
27185           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
27186         }
27187       xops[1] = this_reg;
27188       output_asm_insn ("add%z1\t{%0, %1|%1, %0}", xops);
27189     }
27190
27191   /* If necessary, drop THIS back to its stack slot.  */
27192   if (this_reg && this_reg != this_param)
27193     {
27194       xops[0] = this_reg;
27195       xops[1] = this_param;
27196       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
27197     }
27198
27199   xops[0] = XEXP (DECL_RTL (function), 0);
27200   if (TARGET_64BIT)
27201     {
27202       if (!flag_pic || targetm.binds_local_p (function))
27203         output_asm_insn ("jmp\t%P0", xops);
27204       /* All thunks should be in the same object as their target,
27205          and thus binds_local_p should be true.  */
27206       else if (TARGET_64BIT && cfun->machine->call_abi == MS_ABI)
27207         gcc_unreachable ();
27208       else
27209         {
27210           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
27211           tmp = gen_rtx_CONST (Pmode, tmp);
27212           tmp = gen_rtx_MEM (QImode, tmp);
27213           xops[0] = tmp;
27214           output_asm_insn ("jmp\t%A0", xops);
27215         }
27216     }
27217   else
27218     {
27219       if (!flag_pic || targetm.binds_local_p (function))
27220         output_asm_insn ("jmp\t%P0", xops);
27221       else
27222 #if TARGET_MACHO
27223         if (TARGET_MACHO)
27224           {
27225             rtx sym_ref = XEXP (DECL_RTL (function), 0);
27226             if (TARGET_MACHO_BRANCH_ISLANDS)
27227               sym_ref = (gen_rtx_SYMBOL_REF
27228                    (Pmode,
27229                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
27230             tmp = gen_rtx_MEM (QImode, sym_ref);
27231             xops[0] = tmp;
27232             output_asm_insn ("jmp\t%0", xops);
27233           }
27234         else
27235 #endif /* TARGET_MACHO */
27236         {
27237           tmp = gen_rtx_REG (SImode, CX_REG);
27238           output_set_got (tmp, NULL_RTX);
27239
27240           xops[1] = tmp;
27241           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
27242           output_asm_insn ("jmp\t{*}%1", xops);
27243         }
27244     }
27245   final_end_function ();
27246 }
27247
27248 static void
27249 x86_file_start (void)
27250 {
27251   default_file_start ();
27252 #if TARGET_MACHO
27253   darwin_file_start ();
27254 #endif
27255   if (X86_FILE_START_VERSION_DIRECTIVE)
27256     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
27257   if (X86_FILE_START_FLTUSED)
27258     fputs ("\t.global\t__fltused\n", asm_out_file);
27259   if (ix86_asm_dialect == ASM_INTEL)
27260     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
27261 }
27262
27263 int
27264 x86_field_alignment (tree field, int computed)
27265 {
27266   enum machine_mode mode;
27267   tree type = TREE_TYPE (field);
27268
27269   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
27270     return computed;
27271   mode = TYPE_MODE (strip_array_types (type));
27272   if (mode == DFmode || mode == DCmode
27273       || GET_MODE_CLASS (mode) == MODE_INT
27274       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
27275     return MIN (32, computed);
27276   return computed;
27277 }
27278
27279 /* Output assembler code to FILE to increment profiler label # LABELNO
27280    for profiling a function entry.  */
27281 void
27282 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
27283 {
27284   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
27285                                          : MCOUNT_NAME);
27286
27287   if (TARGET_64BIT)
27288     {
27289 #ifndef NO_PROFILE_COUNTERS
27290       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
27291 #endif
27292
27293       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
27294         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
27295       else
27296         fprintf (file, "\tcall\t%s\n", mcount_name);
27297     }
27298   else if (flag_pic)
27299     {
27300 #ifndef NO_PROFILE_COUNTERS
27301       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
27302                LPREFIX, labelno);
27303 #endif
27304       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
27305     }
27306   else
27307     {
27308 #ifndef NO_PROFILE_COUNTERS
27309       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
27310                LPREFIX, labelno);
27311 #endif
27312       fprintf (file, "\tcall\t%s\n", mcount_name);
27313     }
27314 }
27315
27316 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
27317 /* We don't have exact information about the insn sizes, but we may assume
27318    quite safely that we are informed about all 1 byte insns and memory
27319    address sizes.  This is enough to eliminate unnecessary padding in
27320    99% of cases.  */
27321
27322 static int
27323 min_insn_size (rtx insn)
27324 {
27325   int l = 0, len;
27326
27327   if (!INSN_P (insn) || !active_insn_p (insn))
27328     return 0;
27329
27330   /* Discard alignments we've emit and jump instructions.  */
27331   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
27332       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
27333     return 0;
27334   if (JUMP_TABLE_DATA_P (insn))
27335     return 0;
27336
27337   /* Important case - calls are always 5 bytes.
27338      It is common to have many calls in the row.  */
27339   if (CALL_P (insn)
27340       && symbolic_reference_mentioned_p (PATTERN (insn))
27341       && !SIBLING_CALL_P (insn))
27342     return 5;
27343   len = get_attr_length (insn);
27344   if (len <= 1)
27345     return 1;
27346
27347   /* For normal instructions we rely on get_attr_length being exact,
27348      with a few exceptions.  */
27349   if (!JUMP_P (insn))
27350     {
27351       enum attr_type type = get_attr_type (insn);
27352
27353       switch (type)
27354         {
27355         case TYPE_MULTI:
27356           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
27357               || asm_noperands (PATTERN (insn)) >= 0)
27358             return 0;
27359           break;
27360         case TYPE_OTHER:
27361         case TYPE_FCMP:
27362           break;
27363         default:
27364           /* Otherwise trust get_attr_length.  */
27365           return len;
27366         }
27367
27368       l = get_attr_length_address (insn);
27369       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
27370         l = 4;
27371     }
27372   if (l)
27373     return 1+l;
27374   else
27375     return 2;
27376 }
27377
27378 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
27379    window.  */
27380
27381 static void
27382 ix86_avoid_jump_mispredicts (void)
27383 {
27384   rtx insn, start = get_insns ();
27385   int nbytes = 0, njumps = 0;
27386   int isjump = 0;
27387
27388   /* Look for all minimal intervals of instructions containing 4 jumps.
27389      The intervals are bounded by START and INSN.  NBYTES is the total
27390      size of instructions in the interval including INSN and not including
27391      START.  When the NBYTES is smaller than 16 bytes, it is possible
27392      that the end of START and INSN ends up in the same 16byte page.
27393
27394      The smallest offset in the page INSN can start is the case where START
27395      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
27396      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
27397      */
27398   for (insn = start; insn; insn = NEXT_INSN (insn))
27399     {
27400       int min_size;
27401
27402       if (LABEL_P (insn))
27403         {
27404           int align = label_to_alignment (insn);
27405           int max_skip = label_to_max_skip (insn);
27406
27407           if (max_skip > 15)
27408             max_skip = 15;
27409           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
27410              already in the current 16 byte page, because otherwise
27411              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
27412              bytes to reach 16 byte boundary.  */
27413           if (align <= 0
27414               || (align <= 3 && max_skip != (1 << align) - 1))
27415             max_skip = 0;
27416           if (dump_file)
27417             fprintf (dump_file, "Label %i with max_skip %i\n",
27418                      INSN_UID (insn), max_skip);
27419           if (max_skip)
27420             {
27421               while (nbytes + max_skip >= 16)
27422                 {
27423                   start = NEXT_INSN (start);
27424                   if ((JUMP_P (start)
27425                        && GET_CODE (PATTERN (start)) != ADDR_VEC
27426                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
27427                       || CALL_P (start))
27428                     njumps--, isjump = 1;
27429                   else
27430                     isjump = 0;
27431                   nbytes -= min_insn_size (start);
27432                 }
27433             }
27434           continue;
27435         }
27436
27437       min_size = min_insn_size (insn);
27438       nbytes += min_size;
27439       if (dump_file)
27440         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
27441                  INSN_UID (insn), min_size);
27442       if ((JUMP_P (insn)
27443            && GET_CODE (PATTERN (insn)) != ADDR_VEC
27444            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
27445           || CALL_P (insn))
27446         njumps++;
27447       else
27448         continue;
27449
27450       while (njumps > 3)
27451         {
27452           start = NEXT_INSN (start);
27453           if ((JUMP_P (start)
27454                && GET_CODE (PATTERN (start)) != ADDR_VEC
27455                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
27456               || CALL_P (start))
27457             njumps--, isjump = 1;
27458           else
27459             isjump = 0;
27460           nbytes -= min_insn_size (start);
27461         }
27462       gcc_assert (njumps >= 0);
27463       if (dump_file)
27464         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
27465                  INSN_UID (start), INSN_UID (insn), nbytes);
27466
27467       if (njumps == 3 && isjump && nbytes < 16)
27468         {
27469           int padsize = 15 - nbytes + min_insn_size (insn);
27470
27471           if (dump_file)
27472             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
27473                      INSN_UID (insn), padsize);
27474           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
27475         }
27476     }
27477 }
27478 #endif
27479
27480 /* AMD Athlon works faster
27481    when RET is not destination of conditional jump or directly preceded
27482    by other jump instruction.  We avoid the penalty by inserting NOP just
27483    before the RET instructions in such cases.  */
27484 static void
27485 ix86_pad_returns (void)
27486 {
27487   edge e;
27488   edge_iterator ei;
27489
27490   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
27491     {
27492       basic_block bb = e->src;
27493       rtx ret = BB_END (bb);
27494       rtx prev;
27495       bool replace = false;
27496
27497       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
27498           || optimize_bb_for_size_p (bb))
27499         continue;
27500       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
27501         if (active_insn_p (prev) || LABEL_P (prev))
27502           break;
27503       if (prev && LABEL_P (prev))
27504         {
27505           edge e;
27506           edge_iterator ei;
27507
27508           FOR_EACH_EDGE (e, ei, bb->preds)
27509             if (EDGE_FREQUENCY (e) && e->src->index >= 0
27510                 && !(e->flags & EDGE_FALLTHRU))
27511               replace = true;
27512         }
27513       if (!replace)
27514         {
27515           prev = prev_active_insn (ret);
27516           if (prev
27517               && ((JUMP_P (prev) && any_condjump_p (prev))
27518                   || CALL_P (prev)))
27519             replace = true;
27520           /* Empty functions get branch mispredict even when the jump destination
27521              is not visible to us.  */
27522           if (!prev && !optimize_function_for_size_p (cfun))
27523             replace = true;
27524         }
27525       if (replace)
27526         {
27527           emit_jump_insn_before (gen_return_internal_long (), ret);
27528           delete_insn (ret);
27529         }
27530     }
27531 }
27532
27533 /* Implement machine specific optimizations.  We implement padding of returns
27534    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
27535 static void
27536 ix86_reorg (void)
27537 {
27538   if (optimize && optimize_function_for_speed_p (cfun))
27539     {
27540       if (TARGET_PAD_RETURNS)
27541         ix86_pad_returns ();
27542 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
27543       if (TARGET_FOUR_JUMP_LIMIT)
27544         ix86_avoid_jump_mispredicts ();
27545 #endif
27546     }
27547 }
27548
27549 /* Return nonzero when QImode register that must be represented via REX prefix
27550    is used.  */
27551 bool
27552 x86_extended_QIreg_mentioned_p (rtx insn)
27553 {
27554   int i;
27555   extract_insn_cached (insn);
27556   for (i = 0; i < recog_data.n_operands; i++)
27557     if (REG_P (recog_data.operand[i])
27558         && REGNO (recog_data.operand[i]) > BX_REG)
27559        return true;
27560   return false;
27561 }
27562
27563 /* Return nonzero when P points to register encoded via REX prefix.
27564    Called via for_each_rtx.  */
27565 static int
27566 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
27567 {
27568    unsigned int regno;
27569    if (!REG_P (*p))
27570      return 0;
27571    regno = REGNO (*p);
27572    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
27573 }
27574
27575 /* Return true when INSN mentions register that must be encoded using REX
27576    prefix.  */
27577 bool
27578 x86_extended_reg_mentioned_p (rtx insn)
27579 {
27580   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
27581                        extended_reg_mentioned_1, NULL);
27582 }
27583
27584 /* If profitable, negate (without causing overflow) integer constant
27585    of mode MODE at location LOC.  Return true in this case.  */
27586 bool
27587 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
27588 {
27589   HOST_WIDE_INT val;
27590
27591   if (!CONST_INT_P (*loc))
27592     return false;
27593
27594   switch (mode)
27595     {
27596     case DImode:
27597       /* DImode x86_64 constants must fit in 32 bits.  */
27598       gcc_assert (x86_64_immediate_operand (*loc, mode));
27599
27600       mode = SImode;
27601       break;
27602
27603     case SImode:
27604     case HImode:
27605     case QImode:
27606       break;
27607
27608     default:
27609       gcc_unreachable ();
27610     }
27611
27612   /* Avoid overflows.  */
27613   if (mode_signbit_p (mode, *loc))
27614     return false;
27615
27616   val = INTVAL (*loc);
27617
27618   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
27619      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
27620   if ((val < 0 && val != -128)
27621       || val == 128)
27622     {
27623       *loc = GEN_INT (-val);
27624       return true;
27625     }
27626
27627   return false;
27628 }
27629
27630 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
27631    optabs would emit if we didn't have TFmode patterns.  */
27632
27633 void
27634 x86_emit_floatuns (rtx operands[2])
27635 {
27636   rtx neglab, donelab, i0, i1, f0, in, out;
27637   enum machine_mode mode, inmode;
27638
27639   inmode = GET_MODE (operands[1]);
27640   gcc_assert (inmode == SImode || inmode == DImode);
27641
27642   out = operands[0];
27643   in = force_reg (inmode, operands[1]);
27644   mode = GET_MODE (out);
27645   neglab = gen_label_rtx ();
27646   donelab = gen_label_rtx ();
27647   f0 = gen_reg_rtx (mode);
27648
27649   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
27650
27651   expand_float (out, in, 0);
27652
27653   emit_jump_insn (gen_jump (donelab));
27654   emit_barrier ();
27655
27656   emit_label (neglab);
27657
27658   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
27659                             1, OPTAB_DIRECT);
27660   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
27661                             1, OPTAB_DIRECT);
27662   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
27663
27664   expand_float (f0, i0, 0);
27665
27666   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
27667
27668   emit_label (donelab);
27669 }
27670 \f
27671 /* AVX does not support 32-byte integer vector operations,
27672    thus the longest vector we are faced with is V16QImode.  */
27673 #define MAX_VECT_LEN    16
27674
27675 struct expand_vec_perm_d
27676 {
27677   rtx target, op0, op1;
27678   unsigned char perm[MAX_VECT_LEN];
27679   enum machine_mode vmode;
27680   unsigned char nelt;
27681   bool testing_p;
27682 };
27683
27684 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
27685 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
27686
27687 /* Get a vector mode of the same size as the original but with elements
27688    twice as wide.  This is only guaranteed to apply to integral vectors.  */
27689
27690 static inline enum machine_mode
27691 get_mode_wider_vector (enum machine_mode o)
27692 {
27693   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
27694   enum machine_mode n = GET_MODE_WIDER_MODE (o);
27695   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
27696   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
27697   return n;
27698 }
27699
27700 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
27701    with all elements equal to VAR.  Return true if successful.  */
27702
27703 static bool
27704 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
27705                                    rtx target, rtx val)
27706 {
27707   bool ok;
27708
27709   switch (mode)
27710     {
27711     case V2SImode:
27712     case V2SFmode:
27713       if (!mmx_ok)
27714         return false;
27715       /* FALLTHRU */
27716
27717     case V4DFmode:
27718     case V4DImode:
27719     case V8SFmode:
27720     case V8SImode:
27721     case V2DFmode:
27722     case V2DImode:
27723     case V4SFmode:
27724     case V4SImode:
27725       {
27726         rtx insn, dup;
27727
27728         /* First attempt to recognize VAL as-is.  */
27729         dup = gen_rtx_VEC_DUPLICATE (mode, val);
27730         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
27731         if (recog_memoized (insn) < 0)
27732           {
27733             rtx seq;
27734             /* If that fails, force VAL into a register.  */
27735
27736             start_sequence ();
27737             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
27738             seq = get_insns ();
27739             end_sequence ();
27740             if (seq)
27741               emit_insn_before (seq, insn);
27742
27743             ok = recog_memoized (insn) >= 0;
27744             gcc_assert (ok);
27745           }
27746       }
27747       return true;
27748
27749     case V4HImode:
27750       if (!mmx_ok)
27751         return false;
27752       if (TARGET_SSE || TARGET_3DNOW_A)
27753         {
27754           rtx x;
27755
27756           val = gen_lowpart (SImode, val);
27757           x = gen_rtx_TRUNCATE (HImode, val);
27758           x = gen_rtx_VEC_DUPLICATE (mode, x);
27759           emit_insn (gen_rtx_SET (VOIDmode, target, x));
27760           return true;
27761         }
27762       goto widen;
27763
27764     case V8QImode:
27765       if (!mmx_ok)
27766         return false;
27767       goto widen;
27768
27769     case V8HImode:
27770       if (TARGET_SSE2)
27771         {
27772           struct expand_vec_perm_d dperm;
27773           rtx tmp1, tmp2;
27774
27775         permute:
27776           memset (&dperm, 0, sizeof (dperm));
27777           dperm.target = target;
27778           dperm.vmode = mode;
27779           dperm.nelt = GET_MODE_NUNITS (mode);
27780           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
27781
27782           /* Extend to SImode using a paradoxical SUBREG.  */
27783           tmp1 = gen_reg_rtx (SImode);
27784           emit_move_insn (tmp1, gen_lowpart (SImode, val));
27785
27786           /* Insert the SImode value as low element of a V4SImode vector. */
27787           tmp2 = gen_lowpart (V4SImode, dperm.op0);
27788           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
27789
27790           ok = (expand_vec_perm_1 (&dperm)
27791                 || expand_vec_perm_broadcast_1 (&dperm));
27792           gcc_assert (ok);
27793           return ok;
27794         }
27795       goto widen;
27796
27797     case V16QImode:
27798       if (TARGET_SSE2)
27799         goto permute;
27800       goto widen;
27801
27802     widen:
27803       /* Replicate the value once into the next wider mode and recurse.  */
27804       {
27805         enum machine_mode smode, wsmode, wvmode;
27806         rtx x;
27807
27808         smode = GET_MODE_INNER (mode);
27809         wvmode = get_mode_wider_vector (mode);
27810         wsmode = GET_MODE_INNER (wvmode);
27811
27812         val = convert_modes (wsmode, smode, val, true);
27813         x = expand_simple_binop (wsmode, ASHIFT, val,
27814                                  GEN_INT (GET_MODE_BITSIZE (smode)),
27815                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
27816         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
27817
27818         x = gen_lowpart (wvmode, target);
27819         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
27820         gcc_assert (ok);
27821         return ok;
27822       }
27823
27824     case V16HImode:
27825     case V32QImode:
27826       {
27827         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
27828         rtx x = gen_reg_rtx (hvmode);
27829
27830         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
27831         gcc_assert (ok);
27832
27833         x = gen_rtx_VEC_CONCAT (mode, x, x);
27834         emit_insn (gen_rtx_SET (VOIDmode, target, x));
27835       }
27836       return true;
27837
27838     default:
27839       return false;
27840     }
27841 }
27842
27843 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
27844    whose ONE_VAR element is VAR, and other elements are zero.  Return true
27845    if successful.  */
27846
27847 static bool
27848 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
27849                                      rtx target, rtx var, int one_var)
27850 {
27851   enum machine_mode vsimode;
27852   rtx new_target;
27853   rtx x, tmp;
27854   bool use_vector_set = false;
27855
27856   switch (mode)
27857     {
27858     case V2DImode:
27859       /* For SSE4.1, we normally use vector set.  But if the second
27860          element is zero and inter-unit moves are OK, we use movq
27861          instead.  */
27862       use_vector_set = (TARGET_64BIT
27863                         && TARGET_SSE4_1
27864                         && !(TARGET_INTER_UNIT_MOVES
27865                              && one_var == 0));
27866       break;
27867     case V16QImode:
27868     case V4SImode:
27869     case V4SFmode:
27870       use_vector_set = TARGET_SSE4_1;
27871       break;
27872     case V8HImode:
27873       use_vector_set = TARGET_SSE2;
27874       break;
27875     case V4HImode:
27876       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
27877       break;
27878     case V32QImode:
27879     case V16HImode:
27880     case V8SImode:
27881     case V8SFmode:
27882     case V4DFmode:
27883       use_vector_set = TARGET_AVX;
27884       break;
27885     case V4DImode:
27886       /* Use ix86_expand_vector_set in 64bit mode only.  */
27887       use_vector_set = TARGET_AVX && TARGET_64BIT;
27888       break;
27889     default:
27890       break;
27891     }
27892
27893   if (use_vector_set)
27894     {
27895       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
27896       var = force_reg (GET_MODE_INNER (mode), var);
27897       ix86_expand_vector_set (mmx_ok, target, var, one_var);
27898       return true;
27899     }
27900
27901   switch (mode)
27902     {
27903     case V2SFmode:
27904     case V2SImode:
27905       if (!mmx_ok)
27906         return false;
27907       /* FALLTHRU */
27908
27909     case V2DFmode:
27910     case V2DImode:
27911       if (one_var != 0)
27912         return false;
27913       var = force_reg (GET_MODE_INNER (mode), var);
27914       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
27915       emit_insn (gen_rtx_SET (VOIDmode, target, x));
27916       return true;
27917
27918     case V4SFmode:
27919     case V4SImode:
27920       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
27921         new_target = gen_reg_rtx (mode);
27922       else
27923         new_target = target;
27924       var = force_reg (GET_MODE_INNER (mode), var);
27925       x = gen_rtx_VEC_DUPLICATE (mode, var);
27926       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
27927       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
27928       if (one_var != 0)
27929         {
27930           /* We need to shuffle the value to the correct position, so
27931              create a new pseudo to store the intermediate result.  */
27932
27933           /* With SSE2, we can use the integer shuffle insns.  */
27934           if (mode != V4SFmode && TARGET_SSE2)
27935             {
27936               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
27937                                             const1_rtx,
27938                                             GEN_INT (one_var == 1 ? 0 : 1),
27939                                             GEN_INT (one_var == 2 ? 0 : 1),
27940                                             GEN_INT (one_var == 3 ? 0 : 1)));
27941               if (target != new_target)
27942                 emit_move_insn (target, new_target);
27943               return true;
27944             }
27945
27946           /* Otherwise convert the intermediate result to V4SFmode and
27947              use the SSE1 shuffle instructions.  */
27948           if (mode != V4SFmode)
27949             {
27950               tmp = gen_reg_rtx (V4SFmode);
27951               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
27952             }
27953           else
27954             tmp = new_target;
27955
27956           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
27957                                        const1_rtx,
27958                                        GEN_INT (one_var == 1 ? 0 : 1),
27959                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
27960                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
27961
27962           if (mode != V4SFmode)
27963             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
27964           else if (tmp != target)
27965             emit_move_insn (target, tmp);
27966         }
27967       else if (target != new_target)
27968         emit_move_insn (target, new_target);
27969       return true;
27970
27971     case V8HImode:
27972     case V16QImode:
27973       vsimode = V4SImode;
27974       goto widen;
27975     case V4HImode:
27976     case V8QImode:
27977       if (!mmx_ok)
27978         return false;
27979       vsimode = V2SImode;
27980       goto widen;
27981     widen:
27982       if (one_var != 0)
27983         return false;
27984
27985       /* Zero extend the variable element to SImode and recurse.  */
27986       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
27987
27988       x = gen_reg_rtx (vsimode);
27989       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
27990                                                 var, one_var))
27991         gcc_unreachable ();
27992
27993       emit_move_insn (target, gen_lowpart (mode, x));
27994       return true;
27995
27996     default:
27997       return false;
27998     }
27999 }
28000
28001 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
28002    consisting of the values in VALS.  It is known that all elements
28003    except ONE_VAR are constants.  Return true if successful.  */
28004
28005 static bool
28006 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
28007                                  rtx target, rtx vals, int one_var)
28008 {
28009   rtx var = XVECEXP (vals, 0, one_var);
28010   enum machine_mode wmode;
28011   rtx const_vec, x;
28012
28013   const_vec = copy_rtx (vals);
28014   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
28015   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
28016
28017   switch (mode)
28018     {
28019     case V2DFmode:
28020     case V2DImode:
28021     case V2SFmode:
28022     case V2SImode:
28023       /* For the two element vectors, it's just as easy to use
28024          the general case.  */
28025       return false;
28026
28027     case V4DImode:
28028       /* Use ix86_expand_vector_set in 64bit mode only.  */
28029       if (!TARGET_64BIT)
28030         return false;
28031     case V4DFmode:
28032     case V8SFmode:
28033     case V8SImode:
28034     case V16HImode:
28035     case V32QImode:
28036     case V4SFmode:
28037     case V4SImode:
28038     case V8HImode:
28039     case V4HImode:
28040       break;
28041
28042     case V16QImode:
28043       if (TARGET_SSE4_1)
28044         break;
28045       wmode = V8HImode;
28046       goto widen;
28047     case V8QImode:
28048       wmode = V4HImode;
28049       goto widen;
28050     widen:
28051       /* There's no way to set one QImode entry easily.  Combine
28052          the variable value with its adjacent constant value, and
28053          promote to an HImode set.  */
28054       x = XVECEXP (vals, 0, one_var ^ 1);
28055       if (one_var & 1)
28056         {
28057           var = convert_modes (HImode, QImode, var, true);
28058           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
28059                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
28060           x = GEN_INT (INTVAL (x) & 0xff);
28061         }
28062       else
28063         {
28064           var = convert_modes (HImode, QImode, var, true);
28065           x = gen_int_mode (INTVAL (x) << 8, HImode);
28066         }
28067       if (x != const0_rtx)
28068         var = expand_simple_binop (HImode, IOR, var, x, var,
28069                                    1, OPTAB_LIB_WIDEN);
28070
28071       x = gen_reg_rtx (wmode);
28072       emit_move_insn (x, gen_lowpart (wmode, const_vec));
28073       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
28074
28075       emit_move_insn (target, gen_lowpart (mode, x));
28076       return true;
28077
28078     default:
28079       return false;
28080     }
28081
28082   emit_move_insn (target, const_vec);
28083   ix86_expand_vector_set (mmx_ok, target, var, one_var);
28084   return true;
28085 }
28086
28087 /* A subroutine of ix86_expand_vector_init_general.  Use vector
28088    concatenate to handle the most general case: all values variable,
28089    and none identical.  */
28090
28091 static void
28092 ix86_expand_vector_init_concat (enum machine_mode mode,
28093                                 rtx target, rtx *ops, int n)
28094 {
28095   enum machine_mode cmode, hmode = VOIDmode;
28096   rtx first[8], second[4];
28097   rtvec v;
28098   int i, j;
28099
28100   switch (n)
28101     {
28102     case 2:
28103       switch (mode)
28104         {
28105         case V8SImode:
28106           cmode = V4SImode;
28107           break;
28108         case V8SFmode:
28109           cmode = V4SFmode;
28110           break;
28111         case V4DImode:
28112           cmode = V2DImode;
28113           break;
28114         case V4DFmode:
28115           cmode = V2DFmode;
28116           break;
28117         case V4SImode:
28118           cmode = V2SImode;
28119           break;
28120         case V4SFmode:
28121           cmode = V2SFmode;
28122           break;
28123         case V2DImode:
28124           cmode = DImode;
28125           break;
28126         case V2SImode:
28127           cmode = SImode;
28128           break;
28129         case V2DFmode:
28130           cmode = DFmode;
28131           break;
28132         case V2SFmode:
28133           cmode = SFmode;
28134           break;
28135         default:
28136           gcc_unreachable ();
28137         }
28138
28139       if (!register_operand (ops[1], cmode))
28140         ops[1] = force_reg (cmode, ops[1]);
28141       if (!register_operand (ops[0], cmode))
28142         ops[0] = force_reg (cmode, ops[0]);
28143       emit_insn (gen_rtx_SET (VOIDmode, target,
28144                               gen_rtx_VEC_CONCAT (mode, ops[0],
28145                                                   ops[1])));
28146       break;
28147
28148     case 4:
28149       switch (mode)
28150         {
28151         case V4DImode:
28152           cmode = V2DImode;
28153           break;
28154         case V4DFmode:
28155           cmode = V2DFmode;
28156           break;
28157         case V4SImode:
28158           cmode = V2SImode;
28159           break;
28160         case V4SFmode:
28161           cmode = V2SFmode;
28162           break;
28163         default:
28164           gcc_unreachable ();
28165         }
28166       goto half;
28167
28168     case 8:
28169       switch (mode)
28170         {
28171         case V8SImode:
28172           cmode = V2SImode;
28173           hmode = V4SImode;
28174           break;
28175         case V8SFmode:
28176           cmode = V2SFmode;
28177           hmode = V4SFmode;
28178           break;
28179         default:
28180           gcc_unreachable ();
28181         }
28182       goto half;
28183
28184 half:
28185       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
28186       i = n - 1;
28187       j = (n >> 1) - 1;
28188       for (; i > 0; i -= 2, j--)
28189         {
28190           first[j] = gen_reg_rtx (cmode);
28191           v = gen_rtvec (2, ops[i - 1], ops[i]);
28192           ix86_expand_vector_init (false, first[j],
28193                                    gen_rtx_PARALLEL (cmode, v));
28194         }
28195
28196       n >>= 1;
28197       if (n > 2)
28198         {
28199           gcc_assert (hmode != VOIDmode);
28200           for (i = j = 0; i < n; i += 2, j++)
28201             {
28202               second[j] = gen_reg_rtx (hmode);
28203               ix86_expand_vector_init_concat (hmode, second [j],
28204                                               &first [i], 2);
28205             }
28206           n >>= 1;
28207           ix86_expand_vector_init_concat (mode, target, second, n);
28208         }
28209       else
28210         ix86_expand_vector_init_concat (mode, target, first, n);
28211       break;
28212
28213     default:
28214       gcc_unreachable ();
28215     }
28216 }
28217
28218 /* A subroutine of ix86_expand_vector_init_general.  Use vector
28219    interleave to handle the most general case: all values variable,
28220    and none identical.  */
28221
28222 static void
28223 ix86_expand_vector_init_interleave (enum machine_mode mode,
28224                                     rtx target, rtx *ops, int n)
28225 {
28226   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
28227   int i, j;
28228   rtx op0, op1;
28229   rtx (*gen_load_even) (rtx, rtx, rtx);
28230   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
28231   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
28232
28233   switch (mode)
28234     {
28235     case V8HImode:
28236       gen_load_even = gen_vec_setv8hi;
28237       gen_interleave_first_low = gen_vec_interleave_lowv4si;
28238       gen_interleave_second_low = gen_vec_interleave_lowv2di;
28239       inner_mode = HImode;
28240       first_imode = V4SImode;
28241       second_imode = V2DImode;
28242       third_imode = VOIDmode;
28243       break;
28244     case V16QImode:
28245       gen_load_even = gen_vec_setv16qi;
28246       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
28247       gen_interleave_second_low = gen_vec_interleave_lowv4si;
28248       inner_mode = QImode;
28249       first_imode = V8HImode;
28250       second_imode = V4SImode;
28251       third_imode = V2DImode;
28252       break;
28253     default:
28254       gcc_unreachable ();
28255     }
28256
28257   for (i = 0; i < n; i++)
28258     {
28259       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
28260       op0 = gen_reg_rtx (SImode);
28261       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
28262
28263       /* Insert the SImode value as low element of V4SImode vector. */
28264       op1 = gen_reg_rtx (V4SImode);
28265       op0 = gen_rtx_VEC_MERGE (V4SImode,
28266                                gen_rtx_VEC_DUPLICATE (V4SImode,
28267                                                       op0),
28268                                CONST0_RTX (V4SImode),
28269                                const1_rtx);
28270       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
28271
28272       /* Cast the V4SImode vector back to a vector in orignal mode.  */
28273       op0 = gen_reg_rtx (mode);
28274       emit_move_insn (op0, gen_lowpart (mode, op1));
28275
28276       /* Load even elements into the second positon.  */
28277       emit_insn (gen_load_even (op0,
28278                                 force_reg (inner_mode,
28279                                            ops [i + i + 1]),
28280                                 const1_rtx));
28281
28282       /* Cast vector to FIRST_IMODE vector.  */
28283       ops[i] = gen_reg_rtx (first_imode);
28284       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
28285     }
28286
28287   /* Interleave low FIRST_IMODE vectors.  */
28288   for (i = j = 0; i < n; i += 2, j++)
28289     {
28290       op0 = gen_reg_rtx (first_imode);
28291       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
28292
28293       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
28294       ops[j] = gen_reg_rtx (second_imode);
28295       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
28296     }
28297
28298   /* Interleave low SECOND_IMODE vectors.  */
28299   switch (second_imode)
28300     {
28301     case V4SImode:
28302       for (i = j = 0; i < n / 2; i += 2, j++)
28303         {
28304           op0 = gen_reg_rtx (second_imode);
28305           emit_insn (gen_interleave_second_low (op0, ops[i],
28306                                                 ops[i + 1]));
28307
28308           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
28309              vector.  */
28310           ops[j] = gen_reg_rtx (third_imode);
28311           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
28312         }
28313       second_imode = V2DImode;
28314       gen_interleave_second_low = gen_vec_interleave_lowv2di;
28315       /* FALLTHRU */
28316
28317     case V2DImode:
28318       op0 = gen_reg_rtx (second_imode);
28319       emit_insn (gen_interleave_second_low (op0, ops[0],
28320                                             ops[1]));
28321
28322       /* Cast the SECOND_IMODE vector back to a vector on original
28323          mode.  */
28324       emit_insn (gen_rtx_SET (VOIDmode, target,
28325                               gen_lowpart (mode, op0)));
28326       break;
28327
28328     default:
28329       gcc_unreachable ();
28330     }
28331 }
28332
28333 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
28334    all values variable, and none identical.  */
28335
28336 static void
28337 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
28338                                  rtx target, rtx vals)
28339 {
28340   rtx ops[32], op0, op1;
28341   enum machine_mode half_mode = VOIDmode;
28342   int n, i;
28343
28344   switch (mode)
28345     {
28346     case V2SFmode:
28347     case V2SImode:
28348       if (!mmx_ok && !TARGET_SSE)
28349         break;
28350       /* FALLTHRU */
28351
28352     case V8SFmode:
28353     case V8SImode:
28354     case V4DFmode:
28355     case V4DImode:
28356     case V4SFmode:
28357     case V4SImode:
28358     case V2DFmode:
28359     case V2DImode:
28360       n = GET_MODE_NUNITS (mode);
28361       for (i = 0; i < n; i++)
28362         ops[i] = XVECEXP (vals, 0, i);
28363       ix86_expand_vector_init_concat (mode, target, ops, n);
28364       return;
28365
28366     case V32QImode:
28367       half_mode = V16QImode;
28368       goto half;
28369
28370     case V16HImode:
28371       half_mode = V8HImode;
28372       goto half;
28373
28374 half:
28375       n = GET_MODE_NUNITS (mode);
28376       for (i = 0; i < n; i++)
28377         ops[i] = XVECEXP (vals, 0, i);
28378       op0 = gen_reg_rtx (half_mode);
28379       op1 = gen_reg_rtx (half_mode);
28380       ix86_expand_vector_init_interleave (half_mode, op0, ops,
28381                                           n >> 2);
28382       ix86_expand_vector_init_interleave (half_mode, op1,
28383                                           &ops [n >> 1], n >> 2);
28384       emit_insn (gen_rtx_SET (VOIDmode, target,
28385                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
28386       return;
28387
28388     case V16QImode:
28389       if (!TARGET_SSE4_1)
28390         break;
28391       /* FALLTHRU */
28392
28393     case V8HImode:
28394       if (!TARGET_SSE2)
28395         break;
28396
28397       /* Don't use ix86_expand_vector_init_interleave if we can't
28398          move from GPR to SSE register directly.  */
28399       if (!TARGET_INTER_UNIT_MOVES)
28400         break;
28401
28402       n = GET_MODE_NUNITS (mode);
28403       for (i = 0; i < n; i++)
28404         ops[i] = XVECEXP (vals, 0, i);
28405       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
28406       return;
28407
28408     case V4HImode:
28409     case V8QImode:
28410       break;
28411
28412     default:
28413       gcc_unreachable ();
28414     }
28415
28416     {
28417       int i, j, n_elts, n_words, n_elt_per_word;
28418       enum machine_mode inner_mode;
28419       rtx words[4], shift;
28420
28421       inner_mode = GET_MODE_INNER (mode);
28422       n_elts = GET_MODE_NUNITS (mode);
28423       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
28424       n_elt_per_word = n_elts / n_words;
28425       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
28426
28427       for (i = 0; i < n_words; ++i)
28428         {
28429           rtx word = NULL_RTX;
28430
28431           for (j = 0; j < n_elt_per_word; ++j)
28432             {
28433               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
28434               elt = convert_modes (word_mode, inner_mode, elt, true);
28435
28436               if (j == 0)
28437                 word = elt;
28438               else
28439                 {
28440                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
28441                                               word, 1, OPTAB_LIB_WIDEN);
28442                   word = expand_simple_binop (word_mode, IOR, word, elt,
28443                                               word, 1, OPTAB_LIB_WIDEN);
28444                 }
28445             }
28446
28447           words[i] = word;
28448         }
28449
28450       if (n_words == 1)
28451         emit_move_insn (target, gen_lowpart (mode, words[0]));
28452       else if (n_words == 2)
28453         {
28454           rtx tmp = gen_reg_rtx (mode);
28455           emit_clobber (tmp);
28456           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
28457           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
28458           emit_move_insn (target, tmp);
28459         }
28460       else if (n_words == 4)
28461         {
28462           rtx tmp = gen_reg_rtx (V4SImode);
28463           gcc_assert (word_mode == SImode);
28464           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
28465           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
28466           emit_move_insn (target, gen_lowpart (mode, tmp));
28467         }
28468       else
28469         gcc_unreachable ();
28470     }
28471 }
28472
28473 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
28474    instructions unless MMX_OK is true.  */
28475
28476 void
28477 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
28478 {
28479   enum machine_mode mode = GET_MODE (target);
28480   enum machine_mode inner_mode = GET_MODE_INNER (mode);
28481   int n_elts = GET_MODE_NUNITS (mode);
28482   int n_var = 0, one_var = -1;
28483   bool all_same = true, all_const_zero = true;
28484   int i;
28485   rtx x;
28486
28487   for (i = 0; i < n_elts; ++i)
28488     {
28489       x = XVECEXP (vals, 0, i);
28490       if (!(CONST_INT_P (x)
28491             || GET_CODE (x) == CONST_DOUBLE
28492             || GET_CODE (x) == CONST_FIXED))
28493         n_var++, one_var = i;
28494       else if (x != CONST0_RTX (inner_mode))
28495         all_const_zero = false;
28496       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
28497         all_same = false;
28498     }
28499
28500   /* Constants are best loaded from the constant pool.  */
28501   if (n_var == 0)
28502     {
28503       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
28504       return;
28505     }
28506
28507   /* If all values are identical, broadcast the value.  */
28508   if (all_same
28509       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
28510                                             XVECEXP (vals, 0, 0)))
28511     return;
28512
28513   /* Values where only one field is non-constant are best loaded from
28514      the pool and overwritten via move later.  */
28515   if (n_var == 1)
28516     {
28517       if (all_const_zero
28518           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
28519                                                   XVECEXP (vals, 0, one_var),
28520                                                   one_var))
28521         return;
28522
28523       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
28524         return;
28525     }
28526
28527   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
28528 }
28529
28530 void
28531 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
28532 {
28533   enum machine_mode mode = GET_MODE (target);
28534   enum machine_mode inner_mode = GET_MODE_INNER (mode);
28535   enum machine_mode half_mode;
28536   bool use_vec_merge = false;
28537   rtx tmp;
28538   static rtx (*gen_extract[6][2]) (rtx, rtx)
28539     = {
28540         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
28541         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
28542         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
28543         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
28544         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
28545         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
28546       };
28547   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
28548     = {
28549         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
28550         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
28551         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
28552         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
28553         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
28554         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
28555       };
28556   int i, j, n;
28557
28558   switch (mode)
28559     {
28560     case V2SFmode:
28561     case V2SImode:
28562       if (mmx_ok)
28563         {
28564           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
28565           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
28566           if (elt == 0)
28567             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
28568           else
28569             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
28570           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
28571           return;
28572         }
28573       break;
28574
28575     case V2DImode:
28576       use_vec_merge = TARGET_SSE4_1;
28577       if (use_vec_merge)
28578         break;
28579
28580     case V2DFmode:
28581       {
28582         rtx op0, op1;
28583
28584         /* For the two element vectors, we implement a VEC_CONCAT with
28585            the extraction of the other element.  */
28586
28587         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
28588         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
28589
28590         if (elt == 0)
28591           op0 = val, op1 = tmp;
28592         else
28593           op0 = tmp, op1 = val;
28594
28595         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
28596         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
28597       }
28598       return;
28599
28600     case V4SFmode:
28601       use_vec_merge = TARGET_SSE4_1;
28602       if (use_vec_merge)
28603         break;
28604
28605       switch (elt)
28606         {
28607         case 0:
28608           use_vec_merge = true;
28609           break;
28610
28611         case 1:
28612           /* tmp = target = A B C D */
28613           tmp = copy_to_reg (target);
28614           /* target = A A B B */
28615           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
28616           /* target = X A B B */
28617           ix86_expand_vector_set (false, target, val, 0);
28618           /* target = A X C D  */
28619           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
28620                                           const1_rtx, const0_rtx,
28621                                           GEN_INT (2+4), GEN_INT (3+4)));
28622           return;
28623
28624         case 2:
28625           /* tmp = target = A B C D */
28626           tmp = copy_to_reg (target);
28627           /* tmp = X B C D */
28628           ix86_expand_vector_set (false, tmp, val, 0);
28629           /* target = A B X D */
28630           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
28631                                           const0_rtx, const1_rtx,
28632                                           GEN_INT (0+4), GEN_INT (3+4)));
28633           return;
28634
28635         case 3:
28636           /* tmp = target = A B C D */
28637           tmp = copy_to_reg (target);
28638           /* tmp = X B C D */
28639           ix86_expand_vector_set (false, tmp, val, 0);
28640           /* target = A B X D */
28641           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
28642                                           const0_rtx, const1_rtx,
28643                                           GEN_INT (2+4), GEN_INT (0+4)));
28644           return;
28645
28646         default:
28647           gcc_unreachable ();
28648         }
28649       break;
28650
28651     case V4SImode:
28652       use_vec_merge = TARGET_SSE4_1;
28653       if (use_vec_merge)
28654         break;
28655
28656       /* Element 0 handled by vec_merge below.  */
28657       if (elt == 0)
28658         {
28659           use_vec_merge = true;
28660           break;
28661         }
28662
28663       if (TARGET_SSE2)
28664         {
28665           /* With SSE2, use integer shuffles to swap element 0 and ELT,
28666              store into element 0, then shuffle them back.  */
28667
28668           rtx order[4];
28669
28670           order[0] = GEN_INT (elt);
28671           order[1] = const1_rtx;
28672           order[2] = const2_rtx;
28673           order[3] = GEN_INT (3);
28674           order[elt] = const0_rtx;
28675
28676           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
28677                                         order[1], order[2], order[3]));
28678
28679           ix86_expand_vector_set (false, target, val, 0);
28680
28681           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
28682                                         order[1], order[2], order[3]));
28683         }
28684       else
28685         {
28686           /* For SSE1, we have to reuse the V4SF code.  */
28687           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
28688                                   gen_lowpart (SFmode, val), elt);
28689         }
28690       return;
28691
28692     case V8HImode:
28693       use_vec_merge = TARGET_SSE2;
28694       break;
28695     case V4HImode:
28696       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
28697       break;
28698
28699     case V16QImode:
28700       use_vec_merge = TARGET_SSE4_1;
28701       break;
28702
28703     case V8QImode:
28704       break;
28705
28706     case V32QImode:
28707       half_mode = V16QImode;
28708       j = 0;
28709       n = 16;
28710       goto half;
28711
28712     case V16HImode:
28713       half_mode = V8HImode;
28714       j = 1;
28715       n = 8;
28716       goto half;
28717
28718     case V8SImode:
28719       half_mode = V4SImode;
28720       j = 2;
28721       n = 4;
28722       goto half;
28723
28724     case V4DImode:
28725       half_mode = V2DImode;
28726       j = 3;
28727       n = 2;
28728       goto half;
28729
28730     case V8SFmode:
28731       half_mode = V4SFmode;
28732       j = 4;
28733       n = 4;
28734       goto half;
28735
28736     case V4DFmode:
28737       half_mode = V2DFmode;
28738       j = 5;
28739       n = 2;
28740       goto half;
28741
28742 half:
28743       /* Compute offset.  */
28744       i = elt / n;
28745       elt %= n;
28746
28747       gcc_assert (i <= 1);
28748
28749       /* Extract the half.  */
28750       tmp = gen_reg_rtx (half_mode);
28751       emit_insn (gen_extract[j][i] (tmp, target));
28752
28753       /* Put val in tmp at elt.  */
28754       ix86_expand_vector_set (false, tmp, val, elt);
28755
28756       /* Put it back.  */
28757       emit_insn (gen_insert[j][i] (target, target, tmp));
28758       return;
28759
28760     default:
28761       break;
28762     }
28763
28764   if (use_vec_merge)
28765     {
28766       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
28767       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
28768       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
28769     }
28770   else
28771     {
28772       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
28773
28774       emit_move_insn (mem, target);
28775
28776       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
28777       emit_move_insn (tmp, val);
28778
28779       emit_move_insn (target, mem);
28780     }
28781 }
28782
28783 void
28784 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
28785 {
28786   enum machine_mode mode = GET_MODE (vec);
28787   enum machine_mode inner_mode = GET_MODE_INNER (mode);
28788   bool use_vec_extr = false;
28789   rtx tmp;
28790
28791   switch (mode)
28792     {
28793     case V2SImode:
28794     case V2SFmode:
28795       if (!mmx_ok)
28796         break;
28797       /* FALLTHRU */
28798
28799     case V2DFmode:
28800     case V2DImode:
28801       use_vec_extr = true;
28802       break;
28803
28804     case V4SFmode:
28805       use_vec_extr = TARGET_SSE4_1;
28806       if (use_vec_extr)
28807         break;
28808
28809       switch (elt)
28810         {
28811         case 0:
28812           tmp = vec;
28813           break;
28814
28815         case 1:
28816         case 3:
28817           tmp = gen_reg_rtx (mode);
28818           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
28819                                        GEN_INT (elt), GEN_INT (elt),
28820                                        GEN_INT (elt+4), GEN_INT (elt+4)));
28821           break;
28822
28823         case 2:
28824           tmp = gen_reg_rtx (mode);
28825           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
28826           break;
28827
28828         default:
28829           gcc_unreachable ();
28830         }
28831       vec = tmp;
28832       use_vec_extr = true;
28833       elt = 0;
28834       break;
28835
28836     case V4SImode:
28837       use_vec_extr = TARGET_SSE4_1;
28838       if (use_vec_extr)
28839         break;
28840
28841       if (TARGET_SSE2)
28842         {
28843           switch (elt)
28844             {
28845             case 0:
28846               tmp = vec;
28847               break;
28848
28849             case 1:
28850             case 3:
28851               tmp = gen_reg_rtx (mode);
28852               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
28853                                             GEN_INT (elt), GEN_INT (elt),
28854                                             GEN_INT (elt), GEN_INT (elt)));
28855               break;
28856
28857             case 2:
28858               tmp = gen_reg_rtx (mode);
28859               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
28860               break;
28861
28862             default:
28863               gcc_unreachable ();
28864             }
28865           vec = tmp;
28866           use_vec_extr = true;
28867           elt = 0;
28868         }
28869       else
28870         {
28871           /* For SSE1, we have to reuse the V4SF code.  */
28872           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
28873                                       gen_lowpart (V4SFmode, vec), elt);
28874           return;
28875         }
28876       break;
28877
28878     case V8HImode:
28879       use_vec_extr = TARGET_SSE2;
28880       break;
28881     case V4HImode:
28882       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
28883       break;
28884
28885     case V16QImode:
28886       use_vec_extr = TARGET_SSE4_1;
28887       break;
28888
28889     case V8QImode:
28890       /* ??? Could extract the appropriate HImode element and shift.  */
28891     default:
28892       break;
28893     }
28894
28895   if (use_vec_extr)
28896     {
28897       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
28898       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
28899
28900       /* Let the rtl optimizers know about the zero extension performed.  */
28901       if (inner_mode == QImode || inner_mode == HImode)
28902         {
28903           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
28904           target = gen_lowpart (SImode, target);
28905         }
28906
28907       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
28908     }
28909   else
28910     {
28911       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
28912
28913       emit_move_insn (mem, vec);
28914
28915       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
28916       emit_move_insn (target, tmp);
28917     }
28918 }
28919
28920 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
28921    pattern to reduce; DEST is the destination; IN is the input vector.  */
28922
28923 void
28924 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
28925 {
28926   rtx tmp1, tmp2, tmp3;
28927
28928   tmp1 = gen_reg_rtx (V4SFmode);
28929   tmp2 = gen_reg_rtx (V4SFmode);
28930   tmp3 = gen_reg_rtx (V4SFmode);
28931
28932   emit_insn (gen_sse_movhlps (tmp1, in, in));
28933   emit_insn (fn (tmp2, tmp1, in));
28934
28935   emit_insn (gen_sse_shufps_v4sf (tmp3, tmp2, tmp2,
28936                                   const1_rtx, const1_rtx,
28937                                   GEN_INT (1+4), GEN_INT (1+4)));
28938   emit_insn (fn (dest, tmp2, tmp3));
28939 }
28940 \f
28941 /* Target hook for scalar_mode_supported_p.  */
28942 static bool
28943 ix86_scalar_mode_supported_p (enum machine_mode mode)
28944 {
28945   if (DECIMAL_FLOAT_MODE_P (mode))
28946     return default_decimal_float_supported_p ();
28947   else if (mode == TFmode)
28948     return true;
28949   else
28950     return default_scalar_mode_supported_p (mode);
28951 }
28952
28953 /* Implements target hook vector_mode_supported_p.  */
28954 static bool
28955 ix86_vector_mode_supported_p (enum machine_mode mode)
28956 {
28957   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
28958     return true;
28959   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
28960     return true;
28961   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
28962     return true;
28963   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
28964     return true;
28965   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
28966     return true;
28967   return false;
28968 }
28969
28970 /* Target hook for c_mode_for_suffix.  */
28971 static enum machine_mode
28972 ix86_c_mode_for_suffix (char suffix)
28973 {
28974   if (suffix == 'q')
28975     return TFmode;
28976   if (suffix == 'w')
28977     return XFmode;
28978
28979   return VOIDmode;
28980 }
28981
28982 /* Worker function for TARGET_MD_ASM_CLOBBERS.
28983
28984    We do this in the new i386 backend to maintain source compatibility
28985    with the old cc0-based compiler.  */
28986
28987 static tree
28988 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
28989                       tree inputs ATTRIBUTE_UNUSED,
28990                       tree clobbers)
28991 {
28992   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
28993                         clobbers);
28994   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
28995                         clobbers);
28996   return clobbers;
28997 }
28998
28999 /* Implements target vector targetm.asm.encode_section_info.  This
29000    is not used by netware.  */
29001
29002 static void ATTRIBUTE_UNUSED
29003 ix86_encode_section_info (tree decl, rtx rtl, int first)
29004 {
29005   default_encode_section_info (decl, rtl, first);
29006
29007   if (TREE_CODE (decl) == VAR_DECL
29008       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
29009       && ix86_in_large_data_p (decl))
29010     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
29011 }
29012
29013 /* Worker function for REVERSE_CONDITION.  */
29014
29015 enum rtx_code
29016 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
29017 {
29018   return (mode != CCFPmode && mode != CCFPUmode
29019           ? reverse_condition (code)
29020           : reverse_condition_maybe_unordered (code));
29021 }
29022
29023 /* Output code to perform an x87 FP register move, from OPERANDS[1]
29024    to OPERANDS[0].  */
29025
29026 const char *
29027 output_387_reg_move (rtx insn, rtx *operands)
29028 {
29029   if (REG_P (operands[0]))
29030     {
29031       if (REG_P (operands[1])
29032           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
29033         {
29034           if (REGNO (operands[0]) == FIRST_STACK_REG)
29035             return output_387_ffreep (operands, 0);
29036           return "fstp\t%y0";
29037         }
29038       if (STACK_TOP_P (operands[0]))
29039         return "fld%Z1\t%y1";
29040       return "fst\t%y0";
29041     }
29042   else if (MEM_P (operands[0]))
29043     {
29044       gcc_assert (REG_P (operands[1]));
29045       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
29046         return "fstp%Z0\t%y0";
29047       else
29048         {
29049           /* There is no non-popping store to memory for XFmode.
29050              So if we need one, follow the store with a load.  */
29051           if (GET_MODE (operands[0]) == XFmode)
29052             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
29053           else
29054             return "fst%Z0\t%y0";
29055         }
29056     }
29057   else
29058     gcc_unreachable();
29059 }
29060
29061 /* Output code to perform a conditional jump to LABEL, if C2 flag in
29062    FP status register is set.  */
29063
29064 void
29065 ix86_emit_fp_unordered_jump (rtx label)
29066 {
29067   rtx reg = gen_reg_rtx (HImode);
29068   rtx temp;
29069
29070   emit_insn (gen_x86_fnstsw_1 (reg));
29071
29072   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
29073     {
29074       emit_insn (gen_x86_sahf_1 (reg));
29075
29076       temp = gen_rtx_REG (CCmode, FLAGS_REG);
29077       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
29078     }
29079   else
29080     {
29081       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
29082
29083       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
29084       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
29085     }
29086
29087   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
29088                               gen_rtx_LABEL_REF (VOIDmode, label),
29089                               pc_rtx);
29090   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
29091
29092   emit_jump_insn (temp);
29093   predict_jump (REG_BR_PROB_BASE * 10 / 100);
29094 }
29095
29096 /* Output code to perform a log1p XFmode calculation.  */
29097
29098 void ix86_emit_i387_log1p (rtx op0, rtx op1)
29099 {
29100   rtx label1 = gen_label_rtx ();
29101   rtx label2 = gen_label_rtx ();
29102
29103   rtx tmp = gen_reg_rtx (XFmode);
29104   rtx tmp2 = gen_reg_rtx (XFmode);
29105   rtx test;
29106
29107   emit_insn (gen_absxf2 (tmp, op1));
29108   test = gen_rtx_GE (VOIDmode, tmp,
29109     CONST_DOUBLE_FROM_REAL_VALUE (
29110        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
29111        XFmode));
29112   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
29113
29114   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
29115   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
29116   emit_jump (label2);
29117
29118   emit_label (label1);
29119   emit_move_insn (tmp, CONST1_RTX (XFmode));
29120   emit_insn (gen_addxf3 (tmp, op1, tmp));
29121   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
29122   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
29123
29124   emit_label (label2);
29125 }
29126
29127 /* Output code to perform a Newton-Rhapson approximation of a single precision
29128    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
29129
29130 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
29131 {
29132   rtx x0, x1, e0, e1, two;
29133
29134   x0 = gen_reg_rtx (mode);
29135   e0 = gen_reg_rtx (mode);
29136   e1 = gen_reg_rtx (mode);
29137   x1 = gen_reg_rtx (mode);
29138
29139   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
29140
29141   if (VECTOR_MODE_P (mode))
29142     two = ix86_build_const_vector (SFmode, true, two);
29143
29144   two = force_reg (mode, two);
29145
29146   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
29147
29148   /* x0 = rcp(b) estimate */
29149   emit_insn (gen_rtx_SET (VOIDmode, x0,
29150                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
29151                                           UNSPEC_RCP)));
29152   /* e0 = x0 * a */
29153   emit_insn (gen_rtx_SET (VOIDmode, e0,
29154                           gen_rtx_MULT (mode, x0, a)));
29155   /* e1 = x0 * b */
29156   emit_insn (gen_rtx_SET (VOIDmode, e1,
29157                           gen_rtx_MULT (mode, x0, b)));
29158   /* x1 = 2. - e1 */
29159   emit_insn (gen_rtx_SET (VOIDmode, x1,
29160                           gen_rtx_MINUS (mode, two, e1)));
29161   /* res = e0 * x1 */
29162   emit_insn (gen_rtx_SET (VOIDmode, res,
29163                           gen_rtx_MULT (mode, e0, x1)));
29164 }
29165
29166 /* Output code to perform a Newton-Rhapson approximation of a
29167    single precision floating point [reciprocal] square root.  */
29168
29169 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
29170                          bool recip)
29171 {
29172   rtx x0, e0, e1, e2, e3, mthree, mhalf;
29173   REAL_VALUE_TYPE r;
29174
29175   x0 = gen_reg_rtx (mode);
29176   e0 = gen_reg_rtx (mode);
29177   e1 = gen_reg_rtx (mode);
29178   e2 = gen_reg_rtx (mode);
29179   e3 = gen_reg_rtx (mode);
29180
29181   real_from_integer (&r, VOIDmode, -3, -1, 0);
29182   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
29183
29184   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
29185   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
29186
29187   if (VECTOR_MODE_P (mode))
29188     {
29189       mthree = ix86_build_const_vector (SFmode, true, mthree);
29190       mhalf = ix86_build_const_vector (SFmode, true, mhalf);
29191     }
29192
29193   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
29194      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
29195
29196   /* x0 = rsqrt(a) estimate */
29197   emit_insn (gen_rtx_SET (VOIDmode, x0,
29198                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
29199                                           UNSPEC_RSQRT)));
29200
29201   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
29202   if (!recip)
29203     {
29204       rtx zero, mask;
29205
29206       zero = gen_reg_rtx (mode);
29207       mask = gen_reg_rtx (mode);
29208
29209       zero = force_reg (mode, CONST0_RTX(mode));
29210       emit_insn (gen_rtx_SET (VOIDmode, mask,
29211                               gen_rtx_NE (mode, zero, a)));
29212
29213       emit_insn (gen_rtx_SET (VOIDmode, x0,
29214                               gen_rtx_AND (mode, x0, mask)));
29215     }
29216
29217   /* e0 = x0 * a */
29218   emit_insn (gen_rtx_SET (VOIDmode, e0,
29219                           gen_rtx_MULT (mode, x0, a)));
29220   /* e1 = e0 * x0 */
29221   emit_insn (gen_rtx_SET (VOIDmode, e1,
29222                           gen_rtx_MULT (mode, e0, x0)));
29223
29224   /* e2 = e1 - 3. */
29225   mthree = force_reg (mode, mthree);
29226   emit_insn (gen_rtx_SET (VOIDmode, e2,
29227                           gen_rtx_PLUS (mode, e1, mthree)));
29228
29229   mhalf = force_reg (mode, mhalf);
29230   if (recip)
29231     /* e3 = -.5 * x0 */
29232     emit_insn (gen_rtx_SET (VOIDmode, e3,
29233                             gen_rtx_MULT (mode, x0, mhalf)));
29234   else
29235     /* e3 = -.5 * e0 */
29236     emit_insn (gen_rtx_SET (VOIDmode, e3,
29237                             gen_rtx_MULT (mode, e0, mhalf)));
29238   /* ret = e2 * e3 */
29239   emit_insn (gen_rtx_SET (VOIDmode, res,
29240                           gen_rtx_MULT (mode, e2, e3)));
29241 }
29242
29243 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
29244
29245 static void ATTRIBUTE_UNUSED
29246 i386_solaris_elf_named_section (const char *name, unsigned int flags,
29247                                 tree decl)
29248 {
29249   /* With Binutils 2.15, the "@unwind" marker must be specified on
29250      every occurrence of the ".eh_frame" section, not just the first
29251      one.  */
29252   if (TARGET_64BIT
29253       && strcmp (name, ".eh_frame") == 0)
29254     {
29255       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
29256                flags & SECTION_WRITE ? "aw" : "a");
29257       return;
29258     }
29259   default_elf_asm_named_section (name, flags, decl);
29260 }
29261
29262 /* Return the mangling of TYPE if it is an extended fundamental type.  */
29263
29264 static const char *
29265 ix86_mangle_type (const_tree type)
29266 {
29267   type = TYPE_MAIN_VARIANT (type);
29268
29269   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
29270       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
29271     return NULL;
29272
29273   switch (TYPE_MODE (type))
29274     {
29275     case TFmode:
29276       /* __float128 is "g".  */
29277       return "g";
29278     case XFmode:
29279       /* "long double" or __float80 is "e".  */
29280       return "e";
29281     default:
29282       return NULL;
29283     }
29284 }
29285
29286 /* For 32-bit code we can save PIC register setup by using
29287    __stack_chk_fail_local hidden function instead of calling
29288    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
29289    register, so it is better to call __stack_chk_fail directly.  */
29290
29291 static tree
29292 ix86_stack_protect_fail (void)
29293 {
29294   return TARGET_64BIT
29295          ? default_external_stack_protect_fail ()
29296          : default_hidden_stack_protect_fail ();
29297 }
29298
29299 /* Select a format to encode pointers in exception handling data.  CODE
29300    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
29301    true if the symbol may be affected by dynamic relocations.
29302
29303    ??? All x86 object file formats are capable of representing this.
29304    After all, the relocation needed is the same as for the call insn.
29305    Whether or not a particular assembler allows us to enter such, I
29306    guess we'll have to see.  */
29307 int
29308 asm_preferred_eh_data_format (int code, int global)
29309 {
29310   if (flag_pic)
29311     {
29312       int type = DW_EH_PE_sdata8;
29313       if (!TARGET_64BIT
29314           || ix86_cmodel == CM_SMALL_PIC
29315           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
29316         type = DW_EH_PE_sdata4;
29317       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
29318     }
29319   if (ix86_cmodel == CM_SMALL
29320       || (ix86_cmodel == CM_MEDIUM && code))
29321     return DW_EH_PE_udata4;
29322   return DW_EH_PE_absptr;
29323 }
29324 \f
29325 /* Expand copysign from SIGN to the positive value ABS_VALUE
29326    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
29327    the sign-bit.  */
29328 static void
29329 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
29330 {
29331   enum machine_mode mode = GET_MODE (sign);
29332   rtx sgn = gen_reg_rtx (mode);
29333   if (mask == NULL_RTX)
29334     {
29335       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
29336       if (!VECTOR_MODE_P (mode))
29337         {
29338           /* We need to generate a scalar mode mask in this case.  */
29339           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
29340           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
29341           mask = gen_reg_rtx (mode);
29342           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
29343         }
29344     }
29345   else
29346     mask = gen_rtx_NOT (mode, mask);
29347   emit_insn (gen_rtx_SET (VOIDmode, sgn,
29348                           gen_rtx_AND (mode, mask, sign)));
29349   emit_insn (gen_rtx_SET (VOIDmode, result,
29350                           gen_rtx_IOR (mode, abs_value, sgn)));
29351 }
29352
29353 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
29354    mask for masking out the sign-bit is stored in *SMASK, if that is
29355    non-null.  */
29356 static rtx
29357 ix86_expand_sse_fabs (rtx op0, rtx *smask)
29358 {
29359   enum machine_mode mode = GET_MODE (op0);
29360   rtx xa, mask;
29361
29362   xa = gen_reg_rtx (mode);
29363   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
29364   if (!VECTOR_MODE_P (mode))
29365     {
29366       /* We need to generate a scalar mode mask in this case.  */
29367       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
29368       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
29369       mask = gen_reg_rtx (mode);
29370       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
29371     }
29372   emit_insn (gen_rtx_SET (VOIDmode, xa,
29373                           gen_rtx_AND (mode, op0, mask)));
29374
29375   if (smask)
29376     *smask = mask;
29377
29378   return xa;
29379 }
29380
29381 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
29382    swapping the operands if SWAP_OPERANDS is true.  The expanded
29383    code is a forward jump to a newly created label in case the
29384    comparison is true.  The generated label rtx is returned.  */
29385 static rtx
29386 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
29387                                   bool swap_operands)
29388 {
29389   rtx label, tmp;
29390
29391   if (swap_operands)
29392     {
29393       tmp = op0;
29394       op0 = op1;
29395       op1 = tmp;
29396     }
29397
29398   label = gen_label_rtx ();
29399   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
29400   emit_insn (gen_rtx_SET (VOIDmode, tmp,
29401                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
29402   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
29403   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
29404                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
29405   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
29406   JUMP_LABEL (tmp) = label;
29407
29408   return label;
29409 }
29410
29411 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
29412    using comparison code CODE.  Operands are swapped for the comparison if
29413    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
29414 static rtx
29415 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
29416                               bool swap_operands)
29417 {
29418   enum machine_mode mode = GET_MODE (op0);
29419   rtx mask = gen_reg_rtx (mode);
29420
29421   if (swap_operands)
29422     {
29423       rtx tmp = op0;
29424       op0 = op1;
29425       op1 = tmp;
29426     }
29427
29428   if (mode == DFmode)
29429     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
29430                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
29431   else
29432     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
29433                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
29434
29435   return mask;
29436 }
29437
29438 /* Generate and return a rtx of mode MODE for 2**n where n is the number
29439    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
29440 static rtx
29441 ix86_gen_TWO52 (enum machine_mode mode)
29442 {
29443   REAL_VALUE_TYPE TWO52r;
29444   rtx TWO52;
29445
29446   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
29447   TWO52 = const_double_from_real_value (TWO52r, mode);
29448   TWO52 = force_reg (mode, TWO52);
29449
29450   return TWO52;
29451 }
29452
29453 /* Expand SSE sequence for computing lround from OP1 storing
29454    into OP0.  */
29455 void
29456 ix86_expand_lround (rtx op0, rtx op1)
29457 {
29458   /* C code for the stuff we're doing below:
29459        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
29460        return (long)tmp;
29461    */
29462   enum machine_mode mode = GET_MODE (op1);
29463   const struct real_format *fmt;
29464   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
29465   rtx adj;
29466
29467   /* load nextafter (0.5, 0.0) */
29468   fmt = REAL_MODE_FORMAT (mode);
29469   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
29470   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
29471
29472   /* adj = copysign (0.5, op1) */
29473   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
29474   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
29475
29476   /* adj = op1 + adj */
29477   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
29478
29479   /* op0 = (imode)adj */
29480   expand_fix (op0, adj, 0);
29481 }
29482
29483 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
29484    into OPERAND0.  */
29485 void
29486 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
29487 {
29488   /* C code for the stuff we're doing below (for do_floor):
29489         xi = (long)op1;
29490         xi -= (double)xi > op1 ? 1 : 0;
29491         return xi;
29492    */
29493   enum machine_mode fmode = GET_MODE (op1);
29494   enum machine_mode imode = GET_MODE (op0);
29495   rtx ireg, freg, label, tmp;
29496
29497   /* reg = (long)op1 */
29498   ireg = gen_reg_rtx (imode);
29499   expand_fix (ireg, op1, 0);
29500
29501   /* freg = (double)reg */
29502   freg = gen_reg_rtx (fmode);
29503   expand_float (freg, ireg, 0);
29504
29505   /* ireg = (freg > op1) ? ireg - 1 : ireg */
29506   label = ix86_expand_sse_compare_and_jump (UNLE,
29507                                             freg, op1, !do_floor);
29508   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
29509                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
29510   emit_move_insn (ireg, tmp);
29511
29512   emit_label (label);
29513   LABEL_NUSES (label) = 1;
29514
29515   emit_move_insn (op0, ireg);
29516 }
29517
29518 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
29519    result in OPERAND0.  */
29520 void
29521 ix86_expand_rint (rtx operand0, rtx operand1)
29522 {
29523   /* C code for the stuff we're doing below:
29524         xa = fabs (operand1);
29525         if (!isless (xa, 2**52))
29526           return operand1;
29527         xa = xa + 2**52 - 2**52;
29528         return copysign (xa, operand1);
29529    */
29530   enum machine_mode mode = GET_MODE (operand0);
29531   rtx res, xa, label, TWO52, mask;
29532
29533   res = gen_reg_rtx (mode);
29534   emit_move_insn (res, operand1);
29535
29536   /* xa = abs (operand1) */
29537   xa = ix86_expand_sse_fabs (res, &mask);
29538
29539   /* if (!isless (xa, TWO52)) goto label; */
29540   TWO52 = ix86_gen_TWO52 (mode);
29541   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29542
29543   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
29544   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
29545
29546   ix86_sse_copysign_to_positive (res, xa, res, mask);
29547
29548   emit_label (label);
29549   LABEL_NUSES (label) = 1;
29550
29551   emit_move_insn (operand0, res);
29552 }
29553
29554 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
29555    into OPERAND0.  */
29556 void
29557 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
29558 {
29559   /* C code for the stuff we expand below.
29560         double xa = fabs (x), x2;
29561         if (!isless (xa, TWO52))
29562           return x;
29563         xa = xa + TWO52 - TWO52;
29564         x2 = copysign (xa, x);
29565      Compensate.  Floor:
29566         if (x2 > x)
29567           x2 -= 1;
29568      Compensate.  Ceil:
29569         if (x2 < x)
29570           x2 -= -1;
29571         return x2;
29572    */
29573   enum machine_mode mode = GET_MODE (operand0);
29574   rtx xa, TWO52, tmp, label, one, res, mask;
29575
29576   TWO52 = ix86_gen_TWO52 (mode);
29577
29578   /* Temporary for holding the result, initialized to the input
29579      operand to ease control flow.  */
29580   res = gen_reg_rtx (mode);
29581   emit_move_insn (res, operand1);
29582
29583   /* xa = abs (operand1) */
29584   xa = ix86_expand_sse_fabs (res, &mask);
29585
29586   /* if (!isless (xa, TWO52)) goto label; */
29587   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29588
29589   /* xa = xa + TWO52 - TWO52; */
29590   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
29591   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
29592
29593   /* xa = copysign (xa, operand1) */
29594   ix86_sse_copysign_to_positive (xa, xa, res, mask);
29595
29596   /* generate 1.0 or -1.0 */
29597   one = force_reg (mode,
29598                    const_double_from_real_value (do_floor
29599                                                  ? dconst1 : dconstm1, mode));
29600
29601   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
29602   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
29603   emit_insn (gen_rtx_SET (VOIDmode, tmp,
29604                           gen_rtx_AND (mode, one, tmp)));
29605   /* We always need to subtract here to preserve signed zero.  */
29606   tmp = expand_simple_binop (mode, MINUS,
29607                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
29608   emit_move_insn (res, tmp);
29609
29610   emit_label (label);
29611   LABEL_NUSES (label) = 1;
29612
29613   emit_move_insn (operand0, res);
29614 }
29615
29616 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
29617    into OPERAND0.  */
29618 void
29619 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
29620 {
29621   /* C code for the stuff we expand below.
29622         double xa = fabs (x), x2;
29623         if (!isless (xa, TWO52))
29624           return x;
29625         x2 = (double)(long)x;
29626      Compensate.  Floor:
29627         if (x2 > x)
29628           x2 -= 1;
29629      Compensate.  Ceil:
29630         if (x2 < x)
29631           x2 += 1;
29632         if (HONOR_SIGNED_ZEROS (mode))
29633           return copysign (x2, x);
29634         return x2;
29635    */
29636   enum machine_mode mode = GET_MODE (operand0);
29637   rtx xa, xi, TWO52, tmp, label, one, res, mask;
29638
29639   TWO52 = ix86_gen_TWO52 (mode);
29640
29641   /* Temporary for holding the result, initialized to the input
29642      operand to ease control flow.  */
29643   res = gen_reg_rtx (mode);
29644   emit_move_insn (res, operand1);
29645
29646   /* xa = abs (operand1) */
29647   xa = ix86_expand_sse_fabs (res, &mask);
29648
29649   /* if (!isless (xa, TWO52)) goto label; */
29650   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29651
29652   /* xa = (double)(long)x */
29653   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
29654   expand_fix (xi, res, 0);
29655   expand_float (xa, xi, 0);
29656
29657   /* generate 1.0 */
29658   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
29659
29660   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
29661   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
29662   emit_insn (gen_rtx_SET (VOIDmode, tmp,
29663                           gen_rtx_AND (mode, one, tmp)));
29664   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
29665                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
29666   emit_move_insn (res, tmp);
29667
29668   if (HONOR_SIGNED_ZEROS (mode))
29669     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
29670
29671   emit_label (label);
29672   LABEL_NUSES (label) = 1;
29673
29674   emit_move_insn (operand0, res);
29675 }
29676
29677 /* Expand SSE sequence for computing round from OPERAND1 storing
29678    into OPERAND0.  Sequence that works without relying on DImode truncation
29679    via cvttsd2siq that is only available on 64bit targets.  */
29680 void
29681 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
29682 {
29683   /* C code for the stuff we expand below.
29684         double xa = fabs (x), xa2, x2;
29685         if (!isless (xa, TWO52))
29686           return x;
29687      Using the absolute value and copying back sign makes
29688      -0.0 -> -0.0 correct.
29689         xa2 = xa + TWO52 - TWO52;
29690      Compensate.
29691         dxa = xa2 - xa;
29692         if (dxa <= -0.5)
29693           xa2 += 1;
29694         else if (dxa > 0.5)
29695           xa2 -= 1;
29696         x2 = copysign (xa2, x);
29697         return x2;
29698    */
29699   enum machine_mode mode = GET_MODE (operand0);
29700   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
29701
29702   TWO52 = ix86_gen_TWO52 (mode);
29703
29704   /* Temporary for holding the result, initialized to the input
29705      operand to ease control flow.  */
29706   res = gen_reg_rtx (mode);
29707   emit_move_insn (res, operand1);
29708
29709   /* xa = abs (operand1) */
29710   xa = ix86_expand_sse_fabs (res, &mask);
29711
29712   /* if (!isless (xa, TWO52)) goto label; */
29713   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29714
29715   /* xa2 = xa + TWO52 - TWO52; */
29716   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
29717   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
29718
29719   /* dxa = xa2 - xa; */
29720   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
29721
29722   /* generate 0.5, 1.0 and -0.5 */
29723   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
29724   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
29725   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
29726                                0, OPTAB_DIRECT);
29727
29728   /* Compensate.  */
29729   tmp = gen_reg_rtx (mode);
29730   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
29731   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
29732   emit_insn (gen_rtx_SET (VOIDmode, tmp,
29733                           gen_rtx_AND (mode, one, tmp)));
29734   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
29735   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
29736   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
29737   emit_insn (gen_rtx_SET (VOIDmode, tmp,
29738                           gen_rtx_AND (mode, one, tmp)));
29739   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
29740
29741   /* res = copysign (xa2, operand1) */
29742   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
29743
29744   emit_label (label);
29745   LABEL_NUSES (label) = 1;
29746
29747   emit_move_insn (operand0, res);
29748 }
29749
29750 /* Expand SSE sequence for computing trunc from OPERAND1 storing
29751    into OPERAND0.  */
29752 void
29753 ix86_expand_trunc (rtx operand0, rtx operand1)
29754 {
29755   /* C code for SSE variant we expand below.
29756         double xa = fabs (x), x2;
29757         if (!isless (xa, TWO52))
29758           return x;
29759         x2 = (double)(long)x;
29760         if (HONOR_SIGNED_ZEROS (mode))
29761           return copysign (x2, x);
29762         return x2;
29763    */
29764   enum machine_mode mode = GET_MODE (operand0);
29765   rtx xa, xi, TWO52, label, res, mask;
29766
29767   TWO52 = ix86_gen_TWO52 (mode);
29768
29769   /* Temporary for holding the result, initialized to the input
29770      operand to ease control flow.  */
29771   res = gen_reg_rtx (mode);
29772   emit_move_insn (res, operand1);
29773
29774   /* xa = abs (operand1) */
29775   xa = ix86_expand_sse_fabs (res, &mask);
29776
29777   /* if (!isless (xa, TWO52)) goto label; */
29778   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29779
29780   /* x = (double)(long)x */
29781   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
29782   expand_fix (xi, res, 0);
29783   expand_float (res, xi, 0);
29784
29785   if (HONOR_SIGNED_ZEROS (mode))
29786     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
29787
29788   emit_label (label);
29789   LABEL_NUSES (label) = 1;
29790
29791   emit_move_insn (operand0, res);
29792 }
29793
29794 /* Expand SSE sequence for computing trunc from OPERAND1 storing
29795    into OPERAND0.  */
29796 void
29797 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
29798 {
29799   enum machine_mode mode = GET_MODE (operand0);
29800   rtx xa, mask, TWO52, label, one, res, smask, tmp;
29801
29802   /* C code for SSE variant we expand below.
29803         double xa = fabs (x), x2;
29804         if (!isless (xa, TWO52))
29805           return x;
29806         xa2 = xa + TWO52 - TWO52;
29807      Compensate:
29808         if (xa2 > xa)
29809           xa2 -= 1.0;
29810         x2 = copysign (xa2, x);
29811         return x2;
29812    */
29813
29814   TWO52 = ix86_gen_TWO52 (mode);
29815
29816   /* Temporary for holding the result, initialized to the input
29817      operand to ease control flow.  */
29818   res = gen_reg_rtx (mode);
29819   emit_move_insn (res, operand1);
29820
29821   /* xa = abs (operand1) */
29822   xa = ix86_expand_sse_fabs (res, &smask);
29823
29824   /* if (!isless (xa, TWO52)) goto label; */
29825   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29826
29827   /* res = xa + TWO52 - TWO52; */
29828   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
29829   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
29830   emit_move_insn (res, tmp);
29831
29832   /* generate 1.0 */
29833   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
29834
29835   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
29836   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
29837   emit_insn (gen_rtx_SET (VOIDmode, mask,
29838                           gen_rtx_AND (mode, mask, one)));
29839   tmp = expand_simple_binop (mode, MINUS,
29840                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
29841   emit_move_insn (res, tmp);
29842
29843   /* res = copysign (res, operand1) */
29844   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
29845
29846   emit_label (label);
29847   LABEL_NUSES (label) = 1;
29848
29849   emit_move_insn (operand0, res);
29850 }
29851
29852 /* Expand SSE sequence for computing round from OPERAND1 storing
29853    into OPERAND0.  */
29854 void
29855 ix86_expand_round (rtx operand0, rtx operand1)
29856 {
29857   /* C code for the stuff we're doing below:
29858         double xa = fabs (x);
29859         if (!isless (xa, TWO52))
29860           return x;
29861         xa = (double)(long)(xa + nextafter (0.5, 0.0));
29862         return copysign (xa, x);
29863    */
29864   enum machine_mode mode = GET_MODE (operand0);
29865   rtx res, TWO52, xa, label, xi, half, mask;
29866   const struct real_format *fmt;
29867   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
29868
29869   /* Temporary for holding the result, initialized to the input
29870      operand to ease control flow.  */
29871   res = gen_reg_rtx (mode);
29872   emit_move_insn (res, operand1);
29873
29874   TWO52 = ix86_gen_TWO52 (mode);
29875   xa = ix86_expand_sse_fabs (res, &mask);
29876   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
29877
29878   /* load nextafter (0.5, 0.0) */
29879   fmt = REAL_MODE_FORMAT (mode);
29880   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
29881   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
29882
29883   /* xa = xa + 0.5 */
29884   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
29885   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
29886
29887   /* xa = (double)(int64_t)xa */
29888   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
29889   expand_fix (xi, xa, 0);
29890   expand_float (xa, xi, 0);
29891
29892   /* res = copysign (xa, operand1) */
29893   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
29894
29895   emit_label (label);
29896   LABEL_NUSES (label) = 1;
29897
29898   emit_move_insn (operand0, res);
29899 }
29900 \f
29901
29902 /* Table of valid machine attributes.  */
29903 static const struct attribute_spec ix86_attribute_table[] =
29904 {
29905   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
29906   /* Stdcall attribute says callee is responsible for popping arguments
29907      if they are not variable.  */
29908   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
29909   /* Fastcall attribute says callee is responsible for popping arguments
29910      if they are not variable.  */
29911   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
29912   /* Thiscall attribute says callee is responsible for popping arguments
29913      if they are not variable.  */
29914   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
29915   /* Cdecl attribute says the callee is a normal C declaration */
29916   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
29917   /* Regparm attribute specifies how many integer arguments are to be
29918      passed in registers.  */
29919   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
29920   /* Sseregparm attribute says we are using x86_64 calling conventions
29921      for FP arguments.  */
29922   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
29923   /* force_align_arg_pointer says this function realigns the stack at entry.  */
29924   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
29925     false, true,  true, ix86_handle_cconv_attribute },
29926 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
29927   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
29928   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
29929   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
29930 #endif
29931   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
29932   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
29933 #ifdef SUBTARGET_ATTRIBUTE_TABLE
29934   SUBTARGET_ATTRIBUTE_TABLE,
29935 #endif
29936   /* ms_abi and sysv_abi calling convention function attributes.  */
29937   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
29938   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
29939   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute },
29940   /* End element.  */
29941   { NULL,        0, 0, false, false, false, NULL }
29942 };
29943
29944 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
29945 static int
29946 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
29947                                  tree vectype ATTRIBUTE_UNUSED,
29948                                  int misalign ATTRIBUTE_UNUSED)
29949 {
29950   switch (type_of_cost)
29951     {
29952       case scalar_stmt:
29953         return ix86_cost->scalar_stmt_cost;
29954
29955       case scalar_load:
29956         return ix86_cost->scalar_load_cost;
29957
29958       case scalar_store:
29959         return ix86_cost->scalar_store_cost;
29960
29961       case vector_stmt:
29962         return ix86_cost->vec_stmt_cost;
29963
29964       case vector_load:
29965         return ix86_cost->vec_align_load_cost;
29966
29967       case vector_store:
29968         return ix86_cost->vec_store_cost;
29969
29970       case vec_to_scalar:
29971         return ix86_cost->vec_to_scalar_cost;
29972
29973       case scalar_to_vec:
29974         return ix86_cost->scalar_to_vec_cost;
29975
29976       case unaligned_load:
29977       case unaligned_store:
29978         return ix86_cost->vec_unalign_load_cost;
29979
29980       case cond_branch_taken:
29981         return ix86_cost->cond_taken_branch_cost;
29982
29983       case cond_branch_not_taken:
29984         return ix86_cost->cond_not_taken_branch_cost;
29985
29986       case vec_perm:
29987         return 1;
29988
29989       default:
29990         gcc_unreachable ();
29991     }
29992 }
29993
29994
29995 /* Implement targetm.vectorize.builtin_vec_perm.  */
29996
29997 static tree
29998 ix86_vectorize_builtin_vec_perm (tree vec_type, tree *mask_type)
29999 {
30000   tree itype = TREE_TYPE (vec_type);
30001   bool u = TYPE_UNSIGNED (itype);
30002   enum machine_mode vmode = TYPE_MODE (vec_type);
30003   enum ix86_builtins fcode;
30004   bool ok = TARGET_SSE2;
30005
30006   switch (vmode)
30007     {
30008     case V4DFmode:
30009       ok = TARGET_AVX;
30010       fcode = IX86_BUILTIN_VEC_PERM_V4DF;
30011       goto get_di;
30012     case V2DFmode:
30013       fcode = IX86_BUILTIN_VEC_PERM_V2DF;
30014     get_di:
30015       itype = ix86_get_builtin_type (IX86_BT_DI);
30016       break;
30017
30018     case V8SFmode:
30019       ok = TARGET_AVX;
30020       fcode = IX86_BUILTIN_VEC_PERM_V8SF;
30021       goto get_si;
30022     case V4SFmode:
30023       ok = TARGET_SSE;
30024       fcode = IX86_BUILTIN_VEC_PERM_V4SF;
30025     get_si:
30026       itype = ix86_get_builtin_type (IX86_BT_SI);
30027       break;
30028
30029     case V2DImode:
30030       fcode = u ? IX86_BUILTIN_VEC_PERM_V2DI_U : IX86_BUILTIN_VEC_PERM_V2DI;
30031       break;
30032     case V4SImode:
30033       fcode = u ? IX86_BUILTIN_VEC_PERM_V4SI_U : IX86_BUILTIN_VEC_PERM_V4SI;
30034       break;
30035     case V8HImode:
30036       fcode = u ? IX86_BUILTIN_VEC_PERM_V8HI_U : IX86_BUILTIN_VEC_PERM_V8HI;
30037       break;
30038     case V16QImode:
30039       fcode = u ? IX86_BUILTIN_VEC_PERM_V16QI_U : IX86_BUILTIN_VEC_PERM_V16QI;
30040       break;
30041     default:
30042       ok = false;
30043       break;
30044     }
30045
30046   if (!ok)
30047     return NULL_TREE;
30048
30049   *mask_type = itype;
30050   return ix86_builtins[(int) fcode];
30051 }
30052
30053 /* Return a vector mode with twice as many elements as VMODE.  */
30054 /* ??? Consider moving this to a table generated by genmodes.c.  */
30055
30056 static enum machine_mode
30057 doublesize_vector_mode (enum machine_mode vmode)
30058 {
30059   switch (vmode)
30060     {
30061     case V2SFmode:      return V4SFmode;
30062     case V1DImode:      return V2DImode;
30063     case V2SImode:      return V4SImode;
30064     case V4HImode:      return V8HImode;
30065     case V8QImode:      return V16QImode;
30066
30067     case V2DFmode:      return V4DFmode;
30068     case V4SFmode:      return V8SFmode;
30069     case V2DImode:      return V4DImode;
30070     case V4SImode:      return V8SImode;
30071     case V8HImode:      return V16HImode;
30072     case V16QImode:     return V32QImode;
30073
30074     case V4DFmode:      return V8DFmode;
30075     case V8SFmode:      return V16SFmode;
30076     case V4DImode:      return V8DImode;
30077     case V8SImode:      return V16SImode;
30078     case V16HImode:     return V32HImode;
30079     case V32QImode:     return V64QImode;
30080
30081     default:
30082       gcc_unreachable ();
30083     }
30084 }
30085
30086 /* Construct (set target (vec_select op0 (parallel perm))) and
30087    return true if that's a valid instruction in the active ISA.  */
30088
30089 static bool
30090 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
30091 {
30092   rtx rperm[MAX_VECT_LEN], x;
30093   unsigned i;
30094
30095   for (i = 0; i < nelt; ++i)
30096     rperm[i] = GEN_INT (perm[i]);
30097
30098   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
30099   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
30100   x = gen_rtx_SET (VOIDmode, target, x);
30101
30102   x = emit_insn (x);
30103   if (recog_memoized (x) < 0)
30104     {
30105       remove_insn (x);
30106       return false;
30107     }
30108   return true;
30109 }
30110
30111 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
30112
30113 static bool
30114 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
30115                         const unsigned char *perm, unsigned nelt)
30116 {
30117   enum machine_mode v2mode;
30118   rtx x;
30119
30120   v2mode = doublesize_vector_mode (GET_MODE (op0));
30121   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
30122   return expand_vselect (target, x, perm, nelt);
30123 }
30124
30125 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
30126    in terms of blendp[sd] / pblendw / pblendvb.  */
30127
30128 static bool
30129 expand_vec_perm_blend (struct expand_vec_perm_d *d)
30130 {
30131   enum machine_mode vmode = d->vmode;
30132   unsigned i, mask, nelt = d->nelt;
30133   rtx target, op0, op1, x;
30134
30135   if (!TARGET_SSE4_1 || d->op0 == d->op1)
30136     return false;
30137   if (!(GET_MODE_SIZE (vmode) == 16 || vmode == V4DFmode || vmode == V8SFmode))
30138     return false;
30139
30140   /* This is a blend, not a permute.  Elements must stay in their
30141      respective lanes.  */
30142   for (i = 0; i < nelt; ++i)
30143     {
30144       unsigned e = d->perm[i];
30145       if (!(e == i || e == i + nelt))
30146         return false;
30147     }
30148
30149   if (d->testing_p)
30150     return true;
30151
30152   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
30153      decision should be extracted elsewhere, so that we only try that
30154      sequence once all budget==3 options have been tried.  */
30155
30156   /* For bytes, see if bytes move in pairs so we can use pblendw with
30157      an immediate argument, rather than pblendvb with a vector argument.  */
30158   if (vmode == V16QImode)
30159     {
30160       bool pblendw_ok = true;
30161       for (i = 0; i < 16 && pblendw_ok; i += 2)
30162         pblendw_ok = (d->perm[i] + 1 == d->perm[i + 1]);
30163
30164       if (!pblendw_ok)
30165         {
30166           rtx rperm[16], vperm;
30167
30168           for (i = 0; i < nelt; ++i)
30169             rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
30170
30171           vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
30172           vperm = force_reg (V16QImode, vperm);
30173
30174           emit_insn (gen_sse4_1_pblendvb (d->target, d->op0, d->op1, vperm));
30175           return true;
30176         }
30177     }
30178
30179   target = d->target;
30180   op0 = d->op0;
30181   op1 = d->op1;
30182   mask = 0;
30183
30184   switch (vmode)
30185     {
30186     case V4DFmode:
30187     case V8SFmode:
30188     case V2DFmode:
30189     case V4SFmode:
30190     case V8HImode:
30191       for (i = 0; i < nelt; ++i)
30192         mask |= (d->perm[i] >= nelt) << i;
30193       break;
30194
30195     case V2DImode:
30196       for (i = 0; i < 2; ++i)
30197         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
30198       goto do_subreg;
30199
30200     case V4SImode:
30201       for (i = 0; i < 4; ++i)
30202         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
30203       goto do_subreg;
30204
30205     case V16QImode:
30206       for (i = 0; i < 8; ++i)
30207         mask |= (d->perm[i * 2] >= 16) << i;
30208
30209     do_subreg:
30210       vmode = V8HImode;
30211       target = gen_lowpart (vmode, target);
30212       op0 = gen_lowpart (vmode, op0);
30213       op1 = gen_lowpart (vmode, op1);
30214       break;
30215
30216     default:
30217       gcc_unreachable ();
30218     }
30219
30220   /* This matches five different patterns with the different modes.  */
30221   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
30222   x = gen_rtx_SET (VOIDmode, target, x);
30223   emit_insn (x);
30224
30225   return true;
30226 }
30227
30228 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
30229    in terms of the variable form of vpermilps.
30230
30231    Note that we will have already failed the immediate input vpermilps,
30232    which requires that the high and low part shuffle be identical; the
30233    variable form doesn't require that.  */
30234
30235 static bool
30236 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
30237 {
30238   rtx rperm[8], vperm;
30239   unsigned i;
30240
30241   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
30242     return false;
30243
30244   /* We can only permute within the 128-bit lane.  */
30245   for (i = 0; i < 8; ++i)
30246     {
30247       unsigned e = d->perm[i];
30248       if (i < 4 ? e >= 4 : e < 4)
30249         return false;
30250     }
30251
30252   if (d->testing_p)
30253     return true;
30254
30255   for (i = 0; i < 8; ++i)
30256     {
30257       unsigned e = d->perm[i];
30258
30259       /* Within each 128-bit lane, the elements of op0 are numbered
30260          from 0 and the elements of op1 are numbered from 4.  */
30261       if (e >= 8 + 4)
30262         e -= 8;
30263       else if (e >= 4)
30264         e -= 4;
30265
30266       rperm[i] = GEN_INT (e);
30267     }
30268
30269   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
30270   vperm = force_reg (V8SImode, vperm);
30271   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
30272
30273   return true;
30274 }
30275
30276 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
30277    in terms of pshufb or vpperm.  */
30278
30279 static bool
30280 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
30281 {
30282   unsigned i, nelt, eltsz;
30283   rtx rperm[16], vperm, target, op0, op1;
30284
30285   if (!(d->op0 == d->op1 ? TARGET_SSSE3 : TARGET_XOP))
30286     return false;
30287   if (GET_MODE_SIZE (d->vmode) != 16)
30288     return false;
30289
30290   if (d->testing_p)
30291     return true;
30292
30293   nelt = d->nelt;
30294   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
30295
30296   for (i = 0; i < nelt; ++i)
30297     {
30298       unsigned j, e = d->perm[i];
30299       for (j = 0; j < eltsz; ++j)
30300         rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
30301     }
30302
30303   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
30304   vperm = force_reg (V16QImode, vperm);
30305
30306   target = gen_lowpart (V16QImode, d->target);
30307   op0 = gen_lowpart (V16QImode, d->op0);
30308   if (d->op0 == d->op1)
30309     emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
30310   else
30311     {
30312       op1 = gen_lowpart (V16QImode, d->op1);
30313       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
30314     }
30315
30316   return true;
30317 }
30318
30319 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
30320    in a single instruction.  */
30321
30322 static bool
30323 expand_vec_perm_1 (struct expand_vec_perm_d *d)
30324 {
30325   unsigned i, nelt = d->nelt;
30326   unsigned char perm2[MAX_VECT_LEN];
30327
30328   /* Check plain VEC_SELECT first, because AVX has instructions that could
30329      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
30330      input where SEL+CONCAT may not.  */
30331   if (d->op0 == d->op1)
30332     {
30333       int mask = nelt - 1;
30334
30335       for (i = 0; i < nelt; i++)
30336         perm2[i] = d->perm[i] & mask;
30337
30338       if (expand_vselect (d->target, d->op0, perm2, nelt))
30339         return true;
30340
30341       /* There are plenty of patterns in sse.md that are written for
30342          SEL+CONCAT and are not replicated for a single op.  Perhaps
30343          that should be changed, to avoid the nastiness here.  */
30344
30345       /* Recognize interleave style patterns, which means incrementing
30346          every other permutation operand.  */
30347       for (i = 0; i < nelt; i += 2)
30348         {
30349           perm2[i] = d->perm[i] & mask;
30350           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
30351         }
30352       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
30353         return true;
30354
30355       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
30356       if (nelt >= 4)
30357         {
30358           for (i = 0; i < nelt; i += 4)
30359             {
30360               perm2[i + 0] = d->perm[i + 0] & mask;
30361               perm2[i + 1] = d->perm[i + 1] & mask;
30362               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
30363               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
30364             }
30365
30366           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
30367             return true;
30368         }
30369     }
30370
30371   /* Finally, try the fully general two operand permute.  */
30372   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
30373     return true;
30374
30375   /* Recognize interleave style patterns with reversed operands.  */
30376   if (d->op0 != d->op1)
30377     {
30378       for (i = 0; i < nelt; ++i)
30379         {
30380           unsigned e = d->perm[i];
30381           if (e >= nelt)
30382             e -= nelt;
30383           else
30384             e += nelt;
30385           perm2[i] = e;
30386         }
30387
30388       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
30389         return true;
30390     }
30391
30392   /* Try the SSE4.1 blend variable merge instructions.  */
30393   if (expand_vec_perm_blend (d))
30394     return true;
30395
30396   /* Try one of the AVX vpermil variable permutations.  */
30397   if (expand_vec_perm_vpermil (d))
30398     return true;
30399
30400   /* Try the SSSE3 pshufb or XOP vpperm variable permutation.  */
30401   if (expand_vec_perm_pshufb (d))
30402     return true;
30403
30404   return false;
30405 }
30406
30407 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
30408    in terms of a pair of pshuflw + pshufhw instructions.  */
30409
30410 static bool
30411 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
30412 {
30413   unsigned char perm2[MAX_VECT_LEN];
30414   unsigned i;
30415   bool ok;
30416
30417   if (d->vmode != V8HImode || d->op0 != d->op1)
30418     return false;
30419
30420   /* The two permutations only operate in 64-bit lanes.  */
30421   for (i = 0; i < 4; ++i)
30422     if (d->perm[i] >= 4)
30423       return false;
30424   for (i = 4; i < 8; ++i)
30425     if (d->perm[i] < 4)
30426       return false;
30427
30428   if (d->testing_p)
30429     return true;
30430
30431   /* Emit the pshuflw.  */
30432   memcpy (perm2, d->perm, 4);
30433   for (i = 4; i < 8; ++i)
30434     perm2[i] = i;
30435   ok = expand_vselect (d->target, d->op0, perm2, 8);
30436   gcc_assert (ok);
30437
30438   /* Emit the pshufhw.  */
30439   memcpy (perm2 + 4, d->perm + 4, 4);
30440   for (i = 0; i < 4; ++i)
30441     perm2[i] = i;
30442   ok = expand_vselect (d->target, d->target, perm2, 8);
30443   gcc_assert (ok);
30444
30445   return true;
30446 }
30447
30448 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
30449    the permutation using the SSSE3 palignr instruction.  This succeeds
30450    when all of the elements in PERM fit within one vector and we merely
30451    need to shift them down so that a single vector permutation has a
30452    chance to succeed.  */
30453
30454 static bool
30455 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
30456 {
30457   unsigned i, nelt = d->nelt;
30458   unsigned min, max;
30459   bool in_order, ok;
30460   rtx shift;
30461
30462   /* Even with AVX, palignr only operates on 128-bit vectors.  */
30463   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
30464     return false;
30465
30466   min = nelt, max = 0;
30467   for (i = 0; i < nelt; ++i)
30468     {
30469       unsigned e = d->perm[i];
30470       if (e < min)
30471         min = e;
30472       if (e > max)
30473         max = e;
30474     }
30475   if (min == 0 || max - min >= nelt)
30476     return false;
30477
30478   /* Given that we have SSSE3, we know we'll be able to implement the
30479      single operand permutation after the palignr with pshufb.  */
30480   if (d->testing_p)
30481     return true;
30482
30483   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
30484   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
30485                                   gen_lowpart (TImode, d->op1),
30486                                   gen_lowpart (TImode, d->op0), shift));
30487
30488   d->op0 = d->op1 = d->target;
30489
30490   in_order = true;
30491   for (i = 0; i < nelt; ++i)
30492     {
30493       unsigned e = d->perm[i] - min;
30494       if (e != i)
30495         in_order = false;
30496       d->perm[i] = e;
30497     }
30498
30499   /* Test for the degenerate case where the alignment by itself
30500      produces the desired permutation.  */
30501   if (in_order)
30502     return true;
30503
30504   ok = expand_vec_perm_1 (d);
30505   gcc_assert (ok);
30506
30507   return ok;
30508 }
30509
30510 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
30511    a two vector permutation into a single vector permutation by using
30512    an interleave operation to merge the vectors.  */
30513
30514 static bool
30515 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
30516 {
30517   struct expand_vec_perm_d dremap, dfinal;
30518   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
30519   unsigned contents, h1, h2, h3, h4;
30520   unsigned char remap[2 * MAX_VECT_LEN];
30521   rtx seq;
30522   bool ok;
30523
30524   if (d->op0 == d->op1)
30525     return false;
30526
30527   /* The 256-bit unpck[lh]p[sd] instructions only operate within the 128-bit
30528      lanes.  We can use similar techniques with the vperm2f128 instruction,
30529      but it requires slightly different logic.  */
30530   if (GET_MODE_SIZE (d->vmode) != 16)
30531     return false;
30532
30533   /* Examine from whence the elements come.  */
30534   contents = 0;
30535   for (i = 0; i < nelt; ++i)
30536     contents |= 1u << d->perm[i];
30537
30538   /* Split the two input vectors into 4 halves.  */
30539   h1 = (1u << nelt2) - 1;
30540   h2 = h1 << nelt2;
30541   h3 = h2 << nelt2;
30542   h4 = h3 << nelt2;
30543
30544   memset (remap, 0xff, sizeof (remap));
30545   dremap = *d;
30546
30547   /* If the elements from the low halves use interleave low, and similarly
30548      for interleave high.  If the elements are from mis-matched halves, we
30549      can use shufps for V4SF/V4SI or do a DImode shuffle.  */
30550   if ((contents & (h1 | h3)) == contents)
30551     {
30552       for (i = 0; i < nelt2; ++i)
30553         {
30554           remap[i] = i * 2;
30555           remap[i + nelt] = i * 2 + 1;
30556           dremap.perm[i * 2] = i;
30557           dremap.perm[i * 2 + 1] = i + nelt;
30558         }
30559     }
30560   else if ((contents & (h2 | h4)) == contents)
30561     {
30562       for (i = 0; i < nelt2; ++i)
30563         {
30564           remap[i + nelt2] = i * 2;
30565           remap[i + nelt + nelt2] = i * 2 + 1;
30566           dremap.perm[i * 2] = i + nelt2;
30567           dremap.perm[i * 2 + 1] = i + nelt + nelt2;
30568         }
30569     }
30570   else if ((contents & (h1 | h4)) == contents)
30571     {
30572       for (i = 0; i < nelt2; ++i)
30573         {
30574           remap[i] = i;
30575           remap[i + nelt + nelt2] = i + nelt2;
30576           dremap.perm[i] = i;
30577           dremap.perm[i + nelt2] = i + nelt + nelt2;
30578         }
30579       if (nelt != 4)
30580         {
30581           dremap.vmode = V2DImode;
30582           dremap.nelt = 2;
30583           dremap.perm[0] = 0;
30584           dremap.perm[1] = 3;
30585         }
30586     }
30587   else if ((contents & (h2 | h3)) == contents)
30588     {
30589       for (i = 0; i < nelt2; ++i)
30590         {
30591           remap[i + nelt2] = i;
30592           remap[i + nelt] = i + nelt2;
30593           dremap.perm[i] = i + nelt2;
30594           dremap.perm[i + nelt2] = i + nelt;
30595         }
30596       if (nelt != 4)
30597         {
30598           dremap.vmode = V2DImode;
30599           dremap.nelt = 2;
30600           dremap.perm[0] = 1;
30601           dremap.perm[1] = 2;
30602         }
30603     }
30604   else
30605     return false;
30606
30607   /* Use the remapping array set up above to move the elements from their
30608      swizzled locations into their final destinations.  */
30609   dfinal = *d;
30610   for (i = 0; i < nelt; ++i)
30611     {
30612       unsigned e = remap[d->perm[i]];
30613       gcc_assert (e < nelt);
30614       dfinal.perm[i] = e;
30615     }
30616   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
30617   dfinal.op1 = dfinal.op0;
30618   dremap.target = dfinal.op0;
30619
30620   /* Test if the final remap can be done with a single insn.  For V4SFmode or
30621      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
30622   start_sequence ();
30623   ok = expand_vec_perm_1 (&dfinal);
30624   seq = get_insns ();
30625   end_sequence ();
30626
30627   if (!ok)
30628     return false;
30629
30630   if (dremap.vmode != dfinal.vmode)
30631     {
30632       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
30633       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
30634       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
30635     }
30636
30637   ok = expand_vec_perm_1 (&dremap);
30638   gcc_assert (ok);
30639
30640   emit_insn (seq);
30641   return true;
30642 }
30643
30644 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
30645    permutation with two pshufb insns and an ior.  We should have already
30646    failed all two instruction sequences.  */
30647
30648 static bool
30649 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
30650 {
30651   rtx rperm[2][16], vperm, l, h, op, m128;
30652   unsigned int i, nelt, eltsz;
30653
30654   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
30655     return false;
30656   gcc_assert (d->op0 != d->op1);
30657
30658   nelt = d->nelt;
30659   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
30660
30661   /* Generate two permutation masks.  If the required element is within
30662      the given vector it is shuffled into the proper lane.  If the required
30663      element is in the other vector, force a zero into the lane by setting
30664      bit 7 in the permutation mask.  */
30665   m128 = GEN_INT (-128);
30666   for (i = 0; i < nelt; ++i)
30667     {
30668       unsigned j, e = d->perm[i];
30669       unsigned which = (e >= nelt);
30670       if (e >= nelt)
30671         e -= nelt;
30672
30673       for (j = 0; j < eltsz; ++j)
30674         {
30675           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
30676           rperm[1-which][i*eltsz + j] = m128;
30677         }
30678     }
30679
30680   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
30681   vperm = force_reg (V16QImode, vperm);
30682
30683   l = gen_reg_rtx (V16QImode);
30684   op = gen_lowpart (V16QImode, d->op0);
30685   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
30686
30687   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
30688   vperm = force_reg (V16QImode, vperm);
30689
30690   h = gen_reg_rtx (V16QImode);
30691   op = gen_lowpart (V16QImode, d->op1);
30692   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
30693
30694   op = gen_lowpart (V16QImode, d->target);
30695   emit_insn (gen_iorv16qi3 (op, l, h));
30696
30697   return true;
30698 }
30699
30700 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
30701    and extract-odd permutations.  */
30702
30703 static bool
30704 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
30705 {
30706   rtx t1, t2, t3, t4;
30707
30708   switch (d->vmode)
30709     {
30710     case V4DFmode:
30711       t1 = gen_reg_rtx (V4DFmode);
30712       t2 = gen_reg_rtx (V4DFmode);
30713
30714       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
30715       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
30716       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
30717
30718       /* Now an unpck[lh]pd will produce the result required.  */
30719       if (odd)
30720         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
30721       else
30722         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
30723       emit_insn (t3);
30724       break;
30725
30726     case V8SFmode:
30727       {
30728         static const unsigned char perm1[8] = { 0, 2, 1, 3, 5, 6, 5, 7 };
30729         static const unsigned char perme[8] = { 0, 1,  8,  9, 4, 5, 12, 13 };
30730         static const unsigned char permo[8] = { 2, 3, 10, 11, 6, 7, 14, 15 };
30731
30732         t1 = gen_reg_rtx (V8SFmode);
30733         t2 = gen_reg_rtx (V8SFmode);
30734         t3 = gen_reg_rtx (V8SFmode);
30735         t4 = gen_reg_rtx (V8SFmode);
30736
30737         /* Shuffle within the 128-bit lanes to produce:
30738            { 0 2 1 3 4 6 5 7 } and { 8 a 9 b c e d f }.  */
30739         expand_vselect (t1, d->op0, perm1, 8);
30740         expand_vselect (t2, d->op1, perm1, 8);
30741
30742         /* Shuffle the lanes around to produce:
30743            { 0 2 1 3 8 a 9 b } and { 4 6 5 7 c e d f }.  */
30744         emit_insn (gen_avx_vperm2f128v8sf3 (t3, t1, t2, GEN_INT (0x20)));
30745         emit_insn (gen_avx_vperm2f128v8sf3 (t4, t1, t2, GEN_INT (0x31)));
30746
30747         /* Now a vpermil2p will produce the result required.  */
30748         /* ??? The vpermil2p requires a vector constant.  Another option
30749            is a unpck[lh]ps to merge the two vectors to produce
30750            { 0 4 2 6 8 c a e } or { 1 5 3 7 9 d b f }.  Then use another
30751            vpermilps to get the elements into the final order.  */
30752         d->op0 = t3;
30753         d->op1 = t4;
30754         memcpy (d->perm, odd ? permo: perme, 8);
30755         expand_vec_perm_vpermil (d);
30756       }
30757       break;
30758
30759     case V2DFmode:
30760     case V4SFmode:
30761     case V2DImode:
30762     case V4SImode:
30763       /* These are always directly implementable by expand_vec_perm_1.  */
30764       gcc_unreachable ();
30765
30766     case V8HImode:
30767       if (TARGET_SSSE3)
30768         return expand_vec_perm_pshufb2 (d);
30769       else
30770         {
30771           /* We need 2*log2(N)-1 operations to achieve odd/even
30772              with interleave. */
30773           t1 = gen_reg_rtx (V8HImode);
30774           t2 = gen_reg_rtx (V8HImode);
30775           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
30776           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
30777           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
30778           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
30779           if (odd)
30780             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
30781           else
30782             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
30783           emit_insn (t3);
30784         }
30785       break;
30786
30787     case V16QImode:
30788       if (TARGET_SSSE3)
30789         return expand_vec_perm_pshufb2 (d);
30790       else
30791         {
30792           t1 = gen_reg_rtx (V16QImode);
30793           t2 = gen_reg_rtx (V16QImode);
30794           t3 = gen_reg_rtx (V16QImode);
30795           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
30796           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
30797           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
30798           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
30799           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
30800           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
30801           if (odd)
30802             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
30803           else
30804             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
30805           emit_insn (t3);
30806         }
30807       break;
30808
30809     default:
30810       gcc_unreachable ();
30811     }
30812
30813   return true;
30814 }
30815
30816 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
30817    extract-even and extract-odd permutations.  */
30818
30819 static bool
30820 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
30821 {
30822   unsigned i, odd, nelt = d->nelt;
30823
30824   odd = d->perm[0];
30825   if (odd != 0 && odd != 1)
30826     return false;
30827
30828   for (i = 1; i < nelt; ++i)
30829     if (d->perm[i] != 2 * i + odd)
30830       return false;
30831
30832   return expand_vec_perm_even_odd_1 (d, odd);
30833 }
30834
30835 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
30836    permutations.  We assume that expand_vec_perm_1 has already failed.  */
30837
30838 static bool
30839 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
30840 {
30841   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
30842   enum machine_mode vmode = d->vmode;
30843   unsigned char perm2[4];
30844   rtx op0 = d->op0;
30845   bool ok;
30846
30847   switch (vmode)
30848     {
30849     case V4DFmode:
30850     case V8SFmode:
30851       /* These are special-cased in sse.md so that we can optionally
30852          use the vbroadcast instruction.  They expand to two insns
30853          if the input happens to be in a register.  */
30854       gcc_unreachable ();
30855
30856     case V2DFmode:
30857     case V2DImode:
30858     case V4SFmode:
30859     case V4SImode:
30860       /* These are always implementable using standard shuffle patterns.  */
30861       gcc_unreachable ();
30862
30863     case V8HImode:
30864     case V16QImode:
30865       /* These can be implemented via interleave.  We save one insn by
30866          stopping once we have promoted to V4SImode and then use pshufd.  */
30867       do
30868         {
30869           optab otab = vec_interleave_low_optab;
30870
30871           if (elt >= nelt2)
30872             {
30873               otab = vec_interleave_high_optab;
30874               elt -= nelt2;
30875             }
30876           nelt2 /= 2;
30877
30878           op0 = expand_binop (vmode, otab, op0, op0, NULL, 0, OPTAB_DIRECT);
30879           vmode = get_mode_wider_vector (vmode);
30880           op0 = gen_lowpart (vmode, op0);
30881         }
30882       while (vmode != V4SImode);
30883
30884       memset (perm2, elt, 4);
30885       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
30886       gcc_assert (ok);
30887       return true;
30888
30889     default:
30890       gcc_unreachable ();
30891     }
30892 }
30893
30894 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
30895    broadcast permutations.  */
30896
30897 static bool
30898 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
30899 {
30900   unsigned i, elt, nelt = d->nelt;
30901
30902   if (d->op0 != d->op1)
30903     return false;
30904
30905   elt = d->perm[0];
30906   for (i = 1; i < nelt; ++i)
30907     if (d->perm[i] != elt)
30908       return false;
30909
30910   return expand_vec_perm_broadcast_1 (d);
30911 }
30912
30913 /* The guts of ix86_expand_vec_perm_builtin, also used by the ok hook.
30914    With all of the interface bits taken care of, perform the expansion
30915    in D and return true on success.  */
30916
30917 static bool
30918 ix86_expand_vec_perm_builtin_1 (struct expand_vec_perm_d *d)
30919 {
30920   /* Try a single instruction expansion.  */
30921   if (expand_vec_perm_1 (d))
30922     return true;
30923
30924   /* Try sequences of two instructions.  */
30925
30926   if (expand_vec_perm_pshuflw_pshufhw (d))
30927     return true;
30928
30929   if (expand_vec_perm_palignr (d))
30930     return true;
30931
30932   if (expand_vec_perm_interleave2 (d))
30933     return true;
30934
30935   if (expand_vec_perm_broadcast (d))
30936     return true;
30937
30938   /* Try sequences of three instructions.  */
30939
30940   if (expand_vec_perm_pshufb2 (d))
30941     return true;
30942
30943   /* ??? Look for narrow permutations whose element orderings would
30944      allow the promotion to a wider mode.  */
30945
30946   /* ??? Look for sequences of interleave or a wider permute that place
30947      the data into the correct lanes for a half-vector shuffle like
30948      pshuf[lh]w or vpermilps.  */
30949
30950   /* ??? Look for sequences of interleave that produce the desired results.
30951      The combinatorics of punpck[lh] get pretty ugly... */
30952
30953   if (expand_vec_perm_even_odd (d))
30954     return true;
30955
30956   return false;
30957 }
30958
30959 /* Extract the values from the vector CST into the permutation array in D.
30960    Return 0 on error, 1 if all values from the permutation come from the
30961    first vector, 2 if all values from the second vector, and 3 otherwise.  */
30962
30963 static int
30964 extract_vec_perm_cst (struct expand_vec_perm_d *d, tree cst)
30965 {
30966   tree list = TREE_VECTOR_CST_ELTS (cst);
30967   unsigned i, nelt = d->nelt;
30968   int ret = 0;
30969
30970   for (i = 0; i < nelt; ++i, list = TREE_CHAIN (list))
30971     {
30972       unsigned HOST_WIDE_INT e;
30973
30974       if (!host_integerp (TREE_VALUE (list), 1))
30975         return 0;
30976       e = tree_low_cst (TREE_VALUE (list), 1);
30977       if (e >= 2 * nelt)
30978         return 0;
30979
30980       ret |= (e < nelt ? 1 : 2);
30981       d->perm[i] = e;
30982     }
30983   gcc_assert (list == NULL);
30984
30985   /* For all elements from second vector, fold the elements to first.  */
30986   if (ret == 2)
30987     for (i = 0; i < nelt; ++i)
30988       d->perm[i] -= nelt;
30989
30990   return ret;
30991 }
30992
30993 static rtx
30994 ix86_expand_vec_perm_builtin (tree exp)
30995 {
30996   struct expand_vec_perm_d d;
30997   tree arg0, arg1, arg2;
30998
30999   arg0 = CALL_EXPR_ARG (exp, 0);
31000   arg1 = CALL_EXPR_ARG (exp, 1);
31001   arg2 = CALL_EXPR_ARG (exp, 2);
31002
31003   d.vmode = TYPE_MODE (TREE_TYPE (arg0));
31004   d.nelt = GET_MODE_NUNITS (d.vmode);
31005   d.testing_p = false;
31006   gcc_assert (VECTOR_MODE_P (d.vmode));
31007
31008   if (TREE_CODE (arg2) != VECTOR_CST)
31009     {
31010       error_at (EXPR_LOCATION (exp),
31011                 "vector permutation requires vector constant");
31012       goto exit_error;
31013     }
31014
31015   switch (extract_vec_perm_cst (&d, arg2))
31016     {
31017     default:
31018       gcc_unreachable();
31019
31020     case 0:
31021       error_at (EXPR_LOCATION (exp), "invalid vector permutation constant");
31022       goto exit_error;
31023
31024     case 3:
31025       if (!operand_equal_p (arg0, arg1, 0))
31026         {
31027           d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
31028           d.op0 = force_reg (d.vmode, d.op0);
31029           d.op1 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
31030           d.op1 = force_reg (d.vmode, d.op1);
31031           break;
31032         }
31033
31034       /* The elements of PERM do not suggest that only the first operand
31035          is used, but both operands are identical.  Allow easier matching
31036          of the permutation by folding the permutation into the single
31037          input vector.  */
31038       {
31039         unsigned i, nelt = d.nelt;
31040         for (i = 0; i < nelt; ++i)
31041           if (d.perm[i] >= nelt)
31042             d.perm[i] -= nelt;
31043       }
31044       /* FALLTHRU */
31045
31046     case 1:
31047       d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
31048       d.op0 = force_reg (d.vmode, d.op0);
31049       d.op1 = d.op0;
31050       break;
31051
31052     case 2:
31053       d.op0 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
31054       d.op0 = force_reg (d.vmode, d.op0);
31055       d.op1 = d.op0;
31056       break;
31057     }
31058
31059   d.target = gen_reg_rtx (d.vmode);
31060   if (ix86_expand_vec_perm_builtin_1 (&d))
31061     return d.target;
31062
31063   /* For compiler generated permutations, we should never got here, because
31064      the compiler should also be checking the ok hook.  But since this is a
31065      builtin the user has access too, so don't abort.  */
31066   switch (d.nelt)
31067     {
31068     case 2:
31069       sorry ("vector permutation (%d %d)", d.perm[0], d.perm[1]);
31070       break;
31071     case 4:
31072       sorry ("vector permutation (%d %d %d %d)",
31073              d.perm[0], d.perm[1], d.perm[2], d.perm[3]);
31074       break;
31075     case 8:
31076       sorry ("vector permutation (%d %d %d %d %d %d %d %d)",
31077              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
31078              d.perm[4], d.perm[5], d.perm[6], d.perm[7]);
31079       break;
31080     case 16:
31081       sorry ("vector permutation "
31082              "(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)",
31083              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
31084              d.perm[4], d.perm[5], d.perm[6], d.perm[7],
31085              d.perm[8], d.perm[9], d.perm[10], d.perm[11],
31086              d.perm[12], d.perm[13], d.perm[14], d.perm[15]);
31087       break;
31088     default:
31089       gcc_unreachable ();
31090     }
31091  exit_error:
31092   return CONST0_RTX (d.vmode);
31093 }
31094
31095 /* Implement targetm.vectorize.builtin_vec_perm_ok.  */
31096
31097 static bool
31098 ix86_vectorize_builtin_vec_perm_ok (tree vec_type, tree mask)
31099 {
31100   struct expand_vec_perm_d d;
31101   int vec_mask;
31102   bool ret, one_vec;
31103
31104   d.vmode = TYPE_MODE (vec_type);
31105   d.nelt = GET_MODE_NUNITS (d.vmode);
31106   d.testing_p = true;
31107
31108   /* Given sufficient ISA support we can just return true here
31109      for selected vector modes.  */
31110   if (GET_MODE_SIZE (d.vmode) == 16)
31111     {
31112       /* All implementable with a single vpperm insn.  */
31113       if (TARGET_XOP)
31114         return true;
31115       /* All implementable with 2 pshufb + 1 ior.  */
31116       if (TARGET_SSSE3)
31117         return true;
31118       /* All implementable with shufpd or unpck[lh]pd.  */
31119       if (d.nelt == 2)
31120         return true;
31121     }
31122
31123   vec_mask = extract_vec_perm_cst (&d, mask);
31124
31125   /* This hook is cannot be called in response to something that the
31126      user does (unlike the builtin expander) so we shouldn't ever see
31127      an error generated from the extract.  */
31128   gcc_assert (vec_mask > 0 && vec_mask <= 3);
31129   one_vec = (vec_mask != 3);
31130
31131   /* Implementable with shufps or pshufd.  */
31132   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
31133     return true;
31134
31135   /* Otherwise we have to go through the motions and see if we can
31136      figure out how to generate the requested permutation.  */
31137   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
31138   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
31139   if (!one_vec)
31140     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
31141
31142   start_sequence ();
31143   ret = ix86_expand_vec_perm_builtin_1 (&d);
31144   end_sequence ();
31145
31146   return ret;
31147 }
31148
31149 void
31150 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
31151 {
31152   struct expand_vec_perm_d d;
31153   unsigned i, nelt;
31154
31155   d.target = targ;
31156   d.op0 = op0;
31157   d.op1 = op1;
31158   d.vmode = GET_MODE (targ);
31159   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
31160   d.testing_p = false;
31161
31162   for (i = 0; i < nelt; ++i)
31163     d.perm[i] = i * 2 + odd;
31164
31165   /* We'll either be able to implement the permutation directly...  */
31166   if (expand_vec_perm_1 (&d))
31167     return;
31168
31169   /* ... or we use the special-case patterns.  */
31170   expand_vec_perm_even_odd_1 (&d, odd);
31171 }
31172 \f
31173 /* This function returns the calling abi specific va_list type node.
31174    It returns  the FNDECL specific va_list type.  */
31175
31176 static tree
31177 ix86_fn_abi_va_list (tree fndecl)
31178 {
31179   if (!TARGET_64BIT)
31180     return va_list_type_node;
31181   gcc_assert (fndecl != NULL_TREE);
31182
31183   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
31184     return ms_va_list_type_node;
31185   else
31186     return sysv_va_list_type_node;
31187 }
31188
31189 /* Returns the canonical va_list type specified by TYPE. If there
31190    is no valid TYPE provided, it return NULL_TREE.  */
31191
31192 static tree
31193 ix86_canonical_va_list_type (tree type)
31194 {
31195   tree wtype, htype;
31196
31197   /* Resolve references and pointers to va_list type.  */
31198   if (TREE_CODE (type) == MEM_REF)
31199     type = TREE_TYPE (type);
31200   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
31201     type = TREE_TYPE (type);
31202   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
31203     type = TREE_TYPE (type);
31204
31205   if (TARGET_64BIT)
31206     {
31207       wtype = va_list_type_node;
31208           gcc_assert (wtype != NULL_TREE);
31209       htype = type;
31210       if (TREE_CODE (wtype) == ARRAY_TYPE)
31211         {
31212           /* If va_list is an array type, the argument may have decayed
31213              to a pointer type, e.g. by being passed to another function.
31214              In that case, unwrap both types so that we can compare the
31215              underlying records.  */
31216           if (TREE_CODE (htype) == ARRAY_TYPE
31217               || POINTER_TYPE_P (htype))
31218             {
31219               wtype = TREE_TYPE (wtype);
31220               htype = TREE_TYPE (htype);
31221             }
31222         }
31223       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
31224         return va_list_type_node;
31225       wtype = sysv_va_list_type_node;
31226           gcc_assert (wtype != NULL_TREE);
31227       htype = type;
31228       if (TREE_CODE (wtype) == ARRAY_TYPE)
31229         {
31230           /* If va_list is an array type, the argument may have decayed
31231              to a pointer type, e.g. by being passed to another function.
31232              In that case, unwrap both types so that we can compare the
31233              underlying records.  */
31234           if (TREE_CODE (htype) == ARRAY_TYPE
31235               || POINTER_TYPE_P (htype))
31236             {
31237               wtype = TREE_TYPE (wtype);
31238               htype = TREE_TYPE (htype);
31239             }
31240         }
31241       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
31242         return sysv_va_list_type_node;
31243       wtype = ms_va_list_type_node;
31244           gcc_assert (wtype != NULL_TREE);
31245       htype = type;
31246       if (TREE_CODE (wtype) == ARRAY_TYPE)
31247         {
31248           /* If va_list is an array type, the argument may have decayed
31249              to a pointer type, e.g. by being passed to another function.
31250              In that case, unwrap both types so that we can compare the
31251              underlying records.  */
31252           if (TREE_CODE (htype) == ARRAY_TYPE
31253               || POINTER_TYPE_P (htype))
31254             {
31255               wtype = TREE_TYPE (wtype);
31256               htype = TREE_TYPE (htype);
31257             }
31258         }
31259       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
31260         return ms_va_list_type_node;
31261       return NULL_TREE;
31262     }
31263   return std_canonical_va_list_type (type);
31264 }
31265
31266 /* Iterate through the target-specific builtin types for va_list.
31267    IDX denotes the iterator, *PTREE is set to the result type of
31268    the va_list builtin, and *PNAME to its internal type.
31269    Returns zero if there is no element for this index, otherwise
31270    IDX should be increased upon the next call.
31271    Note, do not iterate a base builtin's name like __builtin_va_list.
31272    Used from c_common_nodes_and_builtins.  */
31273
31274 static int
31275 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
31276 {
31277   if (TARGET_64BIT)
31278     {
31279       switch (idx)
31280         {
31281         default:
31282           break;
31283
31284         case 0:
31285           *ptree = ms_va_list_type_node;
31286           *pname = "__builtin_ms_va_list";
31287           return 1;
31288
31289         case 1:
31290           *ptree = sysv_va_list_type_node;
31291           *pname = "__builtin_sysv_va_list";
31292           return 1;
31293         }
31294     }
31295
31296   return 0;
31297 }
31298
31299 /* Initialize the GCC target structure.  */
31300 #undef TARGET_RETURN_IN_MEMORY
31301 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
31302
31303 #undef TARGET_LEGITIMIZE_ADDRESS
31304 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
31305
31306 #undef TARGET_ATTRIBUTE_TABLE
31307 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
31308 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
31309 #  undef TARGET_MERGE_DECL_ATTRIBUTES
31310 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
31311 #endif
31312
31313 #undef TARGET_COMP_TYPE_ATTRIBUTES
31314 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
31315
31316 #undef TARGET_INIT_BUILTINS
31317 #define TARGET_INIT_BUILTINS ix86_init_builtins
31318 #undef TARGET_BUILTIN_DECL
31319 #define TARGET_BUILTIN_DECL ix86_builtin_decl
31320 #undef TARGET_EXPAND_BUILTIN
31321 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
31322
31323 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
31324 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
31325   ix86_builtin_vectorized_function
31326
31327 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
31328 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
31329
31330 #undef TARGET_BUILTIN_RECIPROCAL
31331 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
31332
31333 #undef TARGET_ASM_FUNCTION_EPILOGUE
31334 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
31335
31336 #undef TARGET_ENCODE_SECTION_INFO
31337 #ifndef SUBTARGET_ENCODE_SECTION_INFO
31338 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
31339 #else
31340 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
31341 #endif
31342
31343 #undef TARGET_ASM_OPEN_PAREN
31344 #define TARGET_ASM_OPEN_PAREN ""
31345 #undef TARGET_ASM_CLOSE_PAREN
31346 #define TARGET_ASM_CLOSE_PAREN ""
31347
31348 #undef TARGET_ASM_BYTE_OP
31349 #define TARGET_ASM_BYTE_OP ASM_BYTE
31350
31351 #undef TARGET_ASM_ALIGNED_HI_OP
31352 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
31353 #undef TARGET_ASM_ALIGNED_SI_OP
31354 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
31355 #ifdef ASM_QUAD
31356 #undef TARGET_ASM_ALIGNED_DI_OP
31357 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
31358 #endif
31359
31360 #undef TARGET_PROFILE_BEFORE_PROLOGUE
31361 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
31362
31363 #undef TARGET_ASM_UNALIGNED_HI_OP
31364 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
31365 #undef TARGET_ASM_UNALIGNED_SI_OP
31366 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
31367 #undef TARGET_ASM_UNALIGNED_DI_OP
31368 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
31369
31370 #undef TARGET_PRINT_OPERAND
31371 #define TARGET_PRINT_OPERAND ix86_print_operand
31372 #undef TARGET_PRINT_OPERAND_ADDRESS
31373 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
31374 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
31375 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
31376
31377 #undef TARGET_SCHED_ADJUST_COST
31378 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
31379 #undef TARGET_SCHED_ISSUE_RATE
31380 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
31381 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
31382 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
31383   ia32_multipass_dfa_lookahead
31384
31385 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
31386 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
31387
31388 #ifdef HAVE_AS_TLS
31389 #undef TARGET_HAVE_TLS
31390 #define TARGET_HAVE_TLS true
31391 #endif
31392 #undef TARGET_CANNOT_FORCE_CONST_MEM
31393 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
31394 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
31395 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
31396
31397 #undef TARGET_DELEGITIMIZE_ADDRESS
31398 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
31399
31400 #undef TARGET_MS_BITFIELD_LAYOUT_P
31401 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
31402
31403 #if TARGET_MACHO
31404 #undef TARGET_BINDS_LOCAL_P
31405 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
31406 #endif
31407 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
31408 #undef TARGET_BINDS_LOCAL_P
31409 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
31410 #endif
31411
31412 #undef TARGET_ASM_OUTPUT_MI_THUNK
31413 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
31414 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
31415 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
31416
31417 #undef TARGET_ASM_FILE_START
31418 #define TARGET_ASM_FILE_START x86_file_start
31419
31420 #undef TARGET_DEFAULT_TARGET_FLAGS
31421 #define TARGET_DEFAULT_TARGET_FLAGS     \
31422   (TARGET_DEFAULT                       \
31423    | TARGET_SUBTARGET_DEFAULT           \
31424    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT \
31425    | MASK_FUSED_MADD)
31426
31427 #undef TARGET_HANDLE_OPTION
31428 #define TARGET_HANDLE_OPTION ix86_handle_option
31429
31430 #undef TARGET_REGISTER_MOVE_COST
31431 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
31432 #undef TARGET_MEMORY_MOVE_COST
31433 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
31434 #undef TARGET_RTX_COSTS
31435 #define TARGET_RTX_COSTS ix86_rtx_costs
31436 #undef TARGET_ADDRESS_COST
31437 #define TARGET_ADDRESS_COST ix86_address_cost
31438
31439 #undef TARGET_FIXED_CONDITION_CODE_REGS
31440 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
31441 #undef TARGET_CC_MODES_COMPATIBLE
31442 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
31443
31444 #undef TARGET_MACHINE_DEPENDENT_REORG
31445 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
31446
31447 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
31448 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
31449
31450 #undef TARGET_BUILD_BUILTIN_VA_LIST
31451 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
31452
31453 #undef TARGET_ENUM_VA_LIST_P
31454 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
31455
31456 #undef TARGET_FN_ABI_VA_LIST
31457 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
31458
31459 #undef TARGET_CANONICAL_VA_LIST_TYPE
31460 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
31461
31462 #undef TARGET_EXPAND_BUILTIN_VA_START
31463 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
31464
31465 #undef TARGET_MD_ASM_CLOBBERS
31466 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
31467
31468 #undef TARGET_PROMOTE_PROTOTYPES
31469 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
31470 #undef TARGET_STRUCT_VALUE_RTX
31471 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
31472 #undef TARGET_SETUP_INCOMING_VARARGS
31473 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
31474 #undef TARGET_MUST_PASS_IN_STACK
31475 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
31476 #undef TARGET_FUNCTION_ARG_ADVANCE
31477 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
31478 #undef TARGET_FUNCTION_ARG
31479 #define TARGET_FUNCTION_ARG ix86_function_arg
31480 #undef TARGET_PASS_BY_REFERENCE
31481 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
31482 #undef TARGET_INTERNAL_ARG_POINTER
31483 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
31484 #undef TARGET_UPDATE_STACK_BOUNDARY
31485 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
31486 #undef TARGET_GET_DRAP_RTX
31487 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
31488 #undef TARGET_STRICT_ARGUMENT_NAMING
31489 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
31490 #undef TARGET_STATIC_CHAIN
31491 #define TARGET_STATIC_CHAIN ix86_static_chain
31492 #undef TARGET_TRAMPOLINE_INIT
31493 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
31494 #undef TARGET_RETURN_POPS_ARGS
31495 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
31496
31497 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
31498 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
31499
31500 #undef TARGET_SCALAR_MODE_SUPPORTED_P
31501 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
31502
31503 #undef TARGET_VECTOR_MODE_SUPPORTED_P
31504 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
31505
31506 #undef TARGET_C_MODE_FOR_SUFFIX
31507 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
31508
31509 #ifdef HAVE_AS_TLS
31510 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
31511 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
31512 #endif
31513
31514 #ifdef SUBTARGET_INSERT_ATTRIBUTES
31515 #undef TARGET_INSERT_ATTRIBUTES
31516 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
31517 #endif
31518
31519 #undef TARGET_MANGLE_TYPE
31520 #define TARGET_MANGLE_TYPE ix86_mangle_type
31521
31522 #undef TARGET_STACK_PROTECT_FAIL
31523 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
31524
31525 #undef TARGET_FUNCTION_VALUE
31526 #define TARGET_FUNCTION_VALUE ix86_function_value
31527
31528 #undef TARGET_FUNCTION_VALUE_REGNO_P
31529 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
31530
31531 #undef TARGET_SECONDARY_RELOAD
31532 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
31533
31534 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
31535 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
31536   ix86_builtin_vectorization_cost
31537 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
31538 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM \
31539   ix86_vectorize_builtin_vec_perm
31540 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK
31541 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK \
31542   ix86_vectorize_builtin_vec_perm_ok
31543
31544 #undef TARGET_SET_CURRENT_FUNCTION
31545 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
31546
31547 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
31548 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
31549
31550 #undef TARGET_OPTION_SAVE
31551 #define TARGET_OPTION_SAVE ix86_function_specific_save
31552
31553 #undef TARGET_OPTION_RESTORE
31554 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
31555
31556 #undef TARGET_OPTION_PRINT
31557 #define TARGET_OPTION_PRINT ix86_function_specific_print
31558
31559 #undef TARGET_CAN_INLINE_P
31560 #define TARGET_CAN_INLINE_P ix86_can_inline_p
31561
31562 #undef TARGET_EXPAND_TO_RTL_HOOK
31563 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
31564
31565 #undef TARGET_LEGITIMATE_ADDRESS_P
31566 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
31567
31568 #undef TARGET_IRA_COVER_CLASSES
31569 #define TARGET_IRA_COVER_CLASSES i386_ira_cover_classes
31570
31571 #undef TARGET_FRAME_POINTER_REQUIRED
31572 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
31573
31574 #undef TARGET_CAN_ELIMINATE
31575 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
31576
31577 #undef TARGET_ASM_CODE_END
31578 #define TARGET_ASM_CODE_END ix86_code_end
31579
31580 struct gcc_target targetm = TARGET_INITIALIZER;
31581 \f
31582 #include "gt-i386.h"