OSDN Git Service

Add patterns for FMA3.
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "insn-config.h"
32 #include "conditions.h"
33 #include "output.h"
34 #include "insn-codes.h"
35 #include "insn-attr.h"
36 #include "flags.h"
37 #include "except.h"
38 #include "function.h"
39 #include "recog.h"
40 #include "expr.h"
41 #include "optabs.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "gimple.h"
51 #include "dwarf2.h"
52 #include "df.h"
53 #include "tm-constrs.h"
54 #include "params.h"
55 #include "cselib.h"
56 #include "debug.h"
57 #include "dwarf2out.h"
58 #include "sched-int.h"
59 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_SINGLE_POP: Enable if single pop insn is preferred
1484      over esp addition.  */
1485   m_386 | m_486 | m_PENT | m_PPRO,
1486
1487   /* X86_TUNE_DOUBLE_POP: Enable if double pop insn is preferred
1488      over esp addition.  */
1489   m_PENT,
1490
1491   /* X86_TUNE_SINGLE_PUSH: Enable if single push insn is preferred
1492      over esp subtraction.  */
1493   m_386 | m_486 | m_PENT | m_K6_GEODE,
1494
1495   /* X86_TUNE_DOUBLE_PUSH. Enable if double push insn is preferred
1496      over esp subtraction.  */
1497   m_PENT | m_K6_GEODE,
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_PAD_SHORT_FUNCTION: Pad short funtion.  */
1580   m_ATOM,
1581
1582   /* X86_TUNE_EXT_80387_CONSTANTS */
1583   m_K6_GEODE | m_ATHLON_K8 | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO
1584   | m_CORE2 | m_GENERIC,
1585
1586   /* X86_TUNE_SHORTEN_X87_SSE */
1587   ~m_K8,
1588
1589   /* X86_TUNE_AVOID_VECTOR_DECODE */
1590   m_K8 | m_GENERIC64,
1591
1592   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1593      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1594   ~(m_386 | m_486),
1595
1596   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1597      vector path on AMD machines.  */
1598   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1599
1600   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1601      machines.  */
1602   m_K8 | m_GENERIC64 | m_AMDFAM10 | m_BDVER1,
1603
1604   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1605      than a MOV.  */
1606   m_PENT,
1607
1608   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1609      but one byte longer.  */
1610   m_PENT,
1611
1612   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1613      operand that cannot be represented using a modRM byte.  The XOR
1614      replacement is long decoded, so this split helps here as well.  */
1615   m_K6,
1616
1617   /* X86_TUNE_USE_VECTOR_FP_CONVERTS: Prefer vector packed SSE conversion
1618      from FP to FP. */
1619   m_AMDFAM10 | m_GENERIC,
1620
1621   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1622      from integer to FP. */
1623   m_AMDFAM10,
1624
1625   /* X86_TUNE_FUSE_CMP_AND_BRANCH: Fuse a compare or test instruction
1626      with a subsequent conditional jump instruction into a single
1627      compare-and-branch uop.  */
1628   m_CORE2 | m_BDVER1,
1629
1630   /* X86_TUNE_OPT_AGU: Optimize for Address Generation Unit. This flag
1631      will impact LEA instruction selection. */
1632   m_ATOM,
1633
1634   /* X86_TUNE_VECTORIZE_DOUBLE: Enable double precision vector
1635      instructions.  */
1636   ~m_ATOM,
1637 };
1638
1639 /* Feature tests against the various architecture variations.  */
1640 unsigned char ix86_arch_features[X86_ARCH_LAST];
1641
1642 /* Feature tests against the various architecture variations, used to create
1643    ix86_arch_features based on the processor mask.  */
1644 static unsigned int initial_ix86_arch_features[X86_ARCH_LAST] = {
1645   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1646   ~(m_386 | m_486 | m_PENT | m_K6),
1647
1648   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1649   ~m_386,
1650
1651   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1652   ~(m_386 | m_486),
1653
1654   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1655   ~m_386,
1656
1657   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1658   ~m_386,
1659 };
1660
1661 static const unsigned int x86_accumulate_outgoing_args
1662   = m_AMD_MULTIPLE | m_ATOM | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1663     | m_GENERIC;
1664
1665 static const unsigned int x86_arch_always_fancy_math_387
1666   = m_PENT | m_ATOM | m_PPRO | m_AMD_MULTIPLE | m_PENT4
1667     | m_NOCONA | m_CORE2 | m_GENERIC;
1668
1669 static enum stringop_alg stringop_alg = no_stringop;
1670
1671 /* In case the average insn count for single function invocation is
1672    lower than this constant, emit fast (but longer) prologue and
1673    epilogue code.  */
1674 #define FAST_PROLOGUE_INSN_COUNT 20
1675
1676 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1677 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1678 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1679 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1680
1681 /* Array of the smallest class containing reg number REGNO, indexed by
1682    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1683
1684 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1685 {
1686   /* ax, dx, cx, bx */
1687   AREG, DREG, CREG, BREG,
1688   /* si, di, bp, sp */
1689   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1690   /* FP registers */
1691   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1692   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1693   /* arg pointer */
1694   NON_Q_REGS,
1695   /* flags, fpsr, fpcr, frame */
1696   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1697   /* SSE registers */
1698   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1699   SSE_REGS, SSE_REGS,
1700   /* MMX registers */
1701   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1702   MMX_REGS, MMX_REGS,
1703   /* REX registers */
1704   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1705   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1706   /* SSE REX registers */
1707   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1708   SSE_REGS, SSE_REGS,
1709 };
1710
1711 /* The "default" register map used in 32bit mode.  */
1712
1713 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1714 {
1715   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1716   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1717   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1718   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1719   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1720   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1721   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1722 };
1723
1724 /* The "default" register map used in 64bit mode.  */
1725
1726 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1727 {
1728   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1729   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1730   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1731   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1732   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1733   8,9,10,11,12,13,14,15,                /* extended integer registers */
1734   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1735 };
1736
1737 /* Define the register numbers to be used in Dwarf debugging information.
1738    The SVR4 reference port C compiler uses the following register numbers
1739    in its Dwarf output code:
1740         0 for %eax (gcc regno = 0)
1741         1 for %ecx (gcc regno = 2)
1742         2 for %edx (gcc regno = 1)
1743         3 for %ebx (gcc regno = 3)
1744         4 for %esp (gcc regno = 7)
1745         5 for %ebp (gcc regno = 6)
1746         6 for %esi (gcc regno = 4)
1747         7 for %edi (gcc regno = 5)
1748    The following three DWARF register numbers are never generated by
1749    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1750    believes these numbers have these meanings.
1751         8  for %eip    (no gcc equivalent)
1752         9  for %eflags (gcc regno = 17)
1753         10 for %trapno (no gcc equivalent)
1754    It is not at all clear how we should number the FP stack registers
1755    for the x86 architecture.  If the version of SDB on x86/svr4 were
1756    a bit less brain dead with respect to floating-point then we would
1757    have a precedent to follow with respect to DWARF register numbers
1758    for x86 FP registers, but the SDB on x86/svr4 is so completely
1759    broken with respect to FP registers that it is hardly worth thinking
1760    of it as something to strive for compatibility with.
1761    The version of x86/svr4 SDB I have at the moment does (partially)
1762    seem to believe that DWARF register number 11 is associated with
1763    the x86 register %st(0), but that's about all.  Higher DWARF
1764    register numbers don't seem to be associated with anything in
1765    particular, and even for DWARF regno 11, SDB only seems to under-
1766    stand that it should say that a variable lives in %st(0) (when
1767    asked via an `=' command) if we said it was in DWARF regno 11,
1768    but SDB still prints garbage when asked for the value of the
1769    variable in question (via a `/' command).
1770    (Also note that the labels SDB prints for various FP stack regs
1771    when doing an `x' command are all wrong.)
1772    Note that these problems generally don't affect the native SVR4
1773    C compiler because it doesn't allow the use of -O with -g and
1774    because when it is *not* optimizing, it allocates a memory
1775    location for each floating-point variable, and the memory
1776    location is what gets described in the DWARF AT_location
1777    attribute for the variable in question.
1778    Regardless of the severe mental illness of the x86/svr4 SDB, we
1779    do something sensible here and we use the following DWARF
1780    register numbers.  Note that these are all stack-top-relative
1781    numbers.
1782         11 for %st(0) (gcc regno = 8)
1783         12 for %st(1) (gcc regno = 9)
1784         13 for %st(2) (gcc regno = 10)
1785         14 for %st(3) (gcc regno = 11)
1786         15 for %st(4) (gcc regno = 12)
1787         16 for %st(5) (gcc regno = 13)
1788         17 for %st(6) (gcc regno = 14)
1789         18 for %st(7) (gcc regno = 15)
1790 */
1791 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1792 {
1793   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1794   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1795   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1796   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1797   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1798   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1799   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1800 };
1801
1802 /* Define parameter passing and return registers.  */
1803
1804 static int const x86_64_int_parameter_registers[6] =
1805 {
1806   DI_REG, SI_REG, DX_REG, CX_REG, R8_REG, R9_REG
1807 };
1808
1809 static int const x86_64_ms_abi_int_parameter_registers[4] =
1810 {
1811   CX_REG, DX_REG, R8_REG, R9_REG
1812 };
1813
1814 static int const x86_64_int_return_registers[4] =
1815 {
1816   AX_REG, DX_REG, DI_REG, SI_REG
1817 };
1818
1819 /* Define the structure for the machine field in struct function.  */
1820
1821 struct GTY(()) stack_local_entry {
1822   unsigned short mode;
1823   unsigned short n;
1824   rtx rtl;
1825   struct stack_local_entry *next;
1826 };
1827
1828 /* Structure describing stack frame layout.
1829    Stack grows downward:
1830
1831    [arguments]
1832                                         <- ARG_POINTER
1833    saved pc
1834
1835    saved static chain                   if ix86_static_chain_on_stack
1836
1837    saved frame pointer                  if frame_pointer_needed
1838                                         <- HARD_FRAME_POINTER
1839    [saved regs]
1840                                         <- regs_save_offset
1841    [padding0]
1842
1843    [saved SSE regs]
1844                                         <- sse_regs_save_offset
1845    [padding1]          |
1846                        |                <- FRAME_POINTER
1847    [va_arg registers]  |
1848                        |
1849    [frame]             |
1850                        |
1851    [padding2]          | = to_allocate
1852                                         <- STACK_POINTER
1853   */
1854 struct ix86_frame
1855 {
1856   int nsseregs;
1857   int nregs;
1858   int va_arg_size;
1859   int red_zone_size;
1860   int outgoing_arguments_size;
1861   HOST_WIDE_INT frame;
1862
1863   /* The offsets relative to ARG_POINTER.  */
1864   HOST_WIDE_INT frame_pointer_offset;
1865   HOST_WIDE_INT hard_frame_pointer_offset;
1866   HOST_WIDE_INT stack_pointer_offset;
1867   HOST_WIDE_INT reg_save_offset;
1868   HOST_WIDE_INT sse_reg_save_offset;
1869
1870   /* When save_regs_using_mov is set, emit prologue using
1871      move instead of push instructions.  */
1872   bool save_regs_using_mov;
1873 };
1874
1875 /* Code model option.  */
1876 enum cmodel ix86_cmodel;
1877 /* Asm dialect.  */
1878 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1879 /* TLS dialects.  */
1880 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1881
1882 /* Which unit we are generating floating point math for.  */
1883 enum fpmath_unit ix86_fpmath;
1884
1885 /* Which cpu are we scheduling for.  */
1886 enum attr_cpu ix86_schedule;
1887
1888 /* Which cpu are we optimizing for.  */
1889 enum processor_type ix86_tune;
1890
1891 /* Which instruction set architecture to use.  */
1892 enum processor_type ix86_arch;
1893
1894 /* true if sse prefetch instruction is not NOOP.  */
1895 int x86_prefetch_sse;
1896
1897 /* ix86_regparm_string as a number */
1898 static int ix86_regparm;
1899
1900 /* -mstackrealign option */
1901 static const char ix86_force_align_arg_pointer_string[]
1902   = "force_align_arg_pointer";
1903
1904 static rtx (*ix86_gen_leave) (void);
1905 static rtx (*ix86_gen_add3) (rtx, rtx, rtx);
1906 static rtx (*ix86_gen_sub3) (rtx, rtx, rtx);
1907 static rtx (*ix86_gen_sub3_carry) (rtx, rtx, rtx, rtx, rtx);
1908 static rtx (*ix86_gen_one_cmpl2) (rtx, rtx);
1909 static rtx (*ix86_gen_monitor) (rtx, rtx, rtx);
1910 static rtx (*ix86_gen_andsp) (rtx, rtx, rtx);
1911 static rtx (*ix86_gen_allocate_stack_worker) (rtx, rtx);
1912 static rtx (*ix86_gen_adjust_stack_and_probe) (rtx, rtx, rtx);
1913 static rtx (*ix86_gen_probe_stack_range) (rtx, rtx, rtx);
1914
1915 /* Preferred alignment for stack boundary in bits.  */
1916 unsigned int ix86_preferred_stack_boundary;
1917
1918 /* Alignment for incoming stack boundary in bits specified at
1919    command line.  */
1920 static unsigned int ix86_user_incoming_stack_boundary;
1921
1922 /* Default alignment for incoming stack boundary in bits.  */
1923 static unsigned int ix86_default_incoming_stack_boundary;
1924
1925 /* Alignment for incoming stack boundary in bits.  */
1926 unsigned int ix86_incoming_stack_boundary;
1927
1928 /* The abi used by target.  */
1929 enum calling_abi ix86_abi;
1930
1931 /* Values 1-5: see jump.c */
1932 int ix86_branch_cost;
1933
1934 /* Calling abi specific va_list type nodes.  */
1935 static GTY(()) tree sysv_va_list_type_node;
1936 static GTY(()) tree ms_va_list_type_node;
1937
1938 /* Variables which are this size or smaller are put in the data/bss
1939    or ldata/lbss sections.  */
1940
1941 int ix86_section_threshold = 65536;
1942
1943 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1944 char internal_label_prefix[16];
1945 int internal_label_prefix_len;
1946
1947 /* Fence to use after loop using movnt.  */
1948 tree x86_mfence;
1949
1950 /* Register class used for passing given 64bit part of the argument.
1951    These represent classes as documented by the PS ABI, with the exception
1952    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1953    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1954
1955    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1956    whenever possible (upper half does contain padding).  */
1957 enum x86_64_reg_class
1958   {
1959     X86_64_NO_CLASS,
1960     X86_64_INTEGER_CLASS,
1961     X86_64_INTEGERSI_CLASS,
1962     X86_64_SSE_CLASS,
1963     X86_64_SSESF_CLASS,
1964     X86_64_SSEDF_CLASS,
1965     X86_64_SSEUP_CLASS,
1966     X86_64_X87_CLASS,
1967     X86_64_X87UP_CLASS,
1968     X86_64_COMPLEX_X87_CLASS,
1969     X86_64_MEMORY_CLASS
1970   };
1971
1972 #define MAX_CLASSES 4
1973
1974 /* Table of constants used by fldpi, fldln2, etc....  */
1975 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1976 static bool ext_80387_constants_init = 0;
1977
1978 \f
1979 static struct machine_function * ix86_init_machine_status (void);
1980 static rtx ix86_function_value (const_tree, const_tree, bool);
1981 static bool ix86_function_value_regno_p (const unsigned int);
1982 static rtx ix86_static_chain (const_tree, bool);
1983 static int ix86_function_regparm (const_tree, const_tree);
1984 static void ix86_compute_frame_layout (struct ix86_frame *);
1985 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1986                                                  rtx, rtx, int);
1987 static void ix86_add_new_builtins (int);
1988 static rtx ix86_expand_vec_perm_builtin (tree);
1989 static tree ix86_canonical_va_list_type (tree);
1990 static void predict_jump (int);
1991 static unsigned int split_stack_prologue_scratch_regno (void);
1992 static bool i386_asm_output_addr_const_extra (FILE *, rtx);
1993
1994 enum ix86_function_specific_strings
1995 {
1996   IX86_FUNCTION_SPECIFIC_ARCH,
1997   IX86_FUNCTION_SPECIFIC_TUNE,
1998   IX86_FUNCTION_SPECIFIC_FPMATH,
1999   IX86_FUNCTION_SPECIFIC_MAX
2000 };
2001
2002 static char *ix86_target_string (int, int, const char *, const char *,
2003                                  const char *, bool);
2004 static void ix86_debug_options (void) ATTRIBUTE_UNUSED;
2005 static void ix86_function_specific_save (struct cl_target_option *);
2006 static void ix86_function_specific_restore (struct cl_target_option *);
2007 static void ix86_function_specific_print (FILE *, int,
2008                                           struct cl_target_option *);
2009 static bool ix86_valid_target_attribute_p (tree, tree, tree, int);
2010 static bool ix86_valid_target_attribute_inner_p (tree, char *[]);
2011 static bool ix86_can_inline_p (tree, tree);
2012 static void ix86_set_current_function (tree);
2013 static unsigned int ix86_minimum_incoming_stack_boundary (bool);
2014
2015 static enum calling_abi ix86_function_abi (const_tree);
2016
2017 \f
2018 #ifndef SUBTARGET32_DEFAULT_CPU
2019 #define SUBTARGET32_DEFAULT_CPU "i386"
2020 #endif
2021
2022 /* The svr4 ABI for the i386 says that records and unions are returned
2023    in memory.  */
2024 #ifndef DEFAULT_PCC_STRUCT_RETURN
2025 #define DEFAULT_PCC_STRUCT_RETURN 1
2026 #endif
2027
2028 /* Whether -mtune= or -march= were specified */
2029 static int ix86_tune_defaulted;
2030 static int ix86_arch_specified;
2031
2032 /* A mask of ix86_isa_flags that includes bit X if X
2033    was set or cleared on the command line.  */
2034 static int ix86_isa_flags_explicit;
2035
2036 /* Define a set of ISAs which are available when a given ISA is
2037    enabled.  MMX and SSE ISAs are handled separately.  */
2038
2039 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
2040 #define OPTION_MASK_ISA_3DNOW_SET \
2041   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
2042
2043 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
2044 #define OPTION_MASK_ISA_SSE2_SET \
2045   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
2046 #define OPTION_MASK_ISA_SSE3_SET \
2047   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
2048 #define OPTION_MASK_ISA_SSSE3_SET \
2049   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
2050 #define OPTION_MASK_ISA_SSE4_1_SET \
2051   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
2052 #define OPTION_MASK_ISA_SSE4_2_SET \
2053   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
2054 #define OPTION_MASK_ISA_AVX_SET \
2055   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_SSE4_2_SET)
2056 #define OPTION_MASK_ISA_FMA_SET \
2057   (OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_AVX_SET)
2058
2059 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
2060    as -msse4.2.  */
2061 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
2062
2063 #define OPTION_MASK_ISA_SSE4A_SET \
2064   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
2065 #define OPTION_MASK_ISA_FMA4_SET \
2066   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_SSE4A_SET \
2067    | OPTION_MASK_ISA_AVX_SET)
2068 #define OPTION_MASK_ISA_XOP_SET \
2069   (OPTION_MASK_ISA_XOP | OPTION_MASK_ISA_FMA4_SET)
2070 #define OPTION_MASK_ISA_LWP_SET \
2071   OPTION_MASK_ISA_LWP
2072
2073 /* AES and PCLMUL need SSE2 because they use xmm registers */
2074 #define OPTION_MASK_ISA_AES_SET \
2075   (OPTION_MASK_ISA_AES | OPTION_MASK_ISA_SSE2_SET)
2076 #define OPTION_MASK_ISA_PCLMUL_SET \
2077   (OPTION_MASK_ISA_PCLMUL | OPTION_MASK_ISA_SSE2_SET)
2078
2079 #define OPTION_MASK_ISA_ABM_SET \
2080   (OPTION_MASK_ISA_ABM | OPTION_MASK_ISA_POPCNT)
2081
2082 #define OPTION_MASK_ISA_POPCNT_SET OPTION_MASK_ISA_POPCNT
2083 #define OPTION_MASK_ISA_CX16_SET OPTION_MASK_ISA_CX16
2084 #define OPTION_MASK_ISA_SAHF_SET OPTION_MASK_ISA_SAHF
2085 #define OPTION_MASK_ISA_MOVBE_SET OPTION_MASK_ISA_MOVBE
2086 #define OPTION_MASK_ISA_CRC32_SET OPTION_MASK_ISA_CRC32
2087
2088 #define OPTION_MASK_ISA_FSGSBASE_SET OPTION_MASK_ISA_FSGSBASE
2089 #define OPTION_MASK_ISA_RDRND_SET OPTION_MASK_ISA_RDRND
2090 #define OPTION_MASK_ISA_F16C_SET \
2091   (OPTION_MASK_ISA_F16C | OPTION_MASK_ISA_AVX_SET)
2092
2093 /* Define a set of ISAs which aren't available when a given ISA is
2094    disabled.  MMX and SSE ISAs are handled separately.  */
2095
2096 #define OPTION_MASK_ISA_MMX_UNSET \
2097   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
2098 #define OPTION_MASK_ISA_3DNOW_UNSET \
2099   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
2100 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
2101
2102 #define OPTION_MASK_ISA_SSE_UNSET \
2103   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
2104 #define OPTION_MASK_ISA_SSE2_UNSET \
2105   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
2106 #define OPTION_MASK_ISA_SSE3_UNSET \
2107   (OPTION_MASK_ISA_SSE3 \
2108    | OPTION_MASK_ISA_SSSE3_UNSET \
2109    | OPTION_MASK_ISA_SSE4A_UNSET )
2110 #define OPTION_MASK_ISA_SSSE3_UNSET \
2111   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
2112 #define OPTION_MASK_ISA_SSE4_1_UNSET \
2113   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
2114 #define OPTION_MASK_ISA_SSE4_2_UNSET \
2115   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_AVX_UNSET )
2116 #define OPTION_MASK_ISA_AVX_UNSET \
2117   (OPTION_MASK_ISA_AVX | OPTION_MASK_ISA_FMA_UNSET \
2118    | OPTION_MASK_ISA_FMA4_UNSET | OPTION_MASK_ISA_F16C_UNSET)
2119 #define OPTION_MASK_ISA_FMA_UNSET OPTION_MASK_ISA_FMA
2120
2121 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
2122    as -mno-sse4.1. */
2123 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
2124
2125 #define OPTION_MASK_ISA_SSE4A_UNSET \
2126   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_FMA4_UNSET)
2127
2128 #define OPTION_MASK_ISA_FMA4_UNSET \
2129   (OPTION_MASK_ISA_FMA4 | OPTION_MASK_ISA_XOP_UNSET)
2130 #define OPTION_MASK_ISA_XOP_UNSET OPTION_MASK_ISA_XOP
2131 #define OPTION_MASK_ISA_LWP_UNSET OPTION_MASK_ISA_LWP
2132
2133 #define OPTION_MASK_ISA_AES_UNSET OPTION_MASK_ISA_AES
2134 #define OPTION_MASK_ISA_PCLMUL_UNSET OPTION_MASK_ISA_PCLMUL
2135 #define OPTION_MASK_ISA_ABM_UNSET OPTION_MASK_ISA_ABM
2136 #define OPTION_MASK_ISA_POPCNT_UNSET OPTION_MASK_ISA_POPCNT
2137 #define OPTION_MASK_ISA_CX16_UNSET OPTION_MASK_ISA_CX16
2138 #define OPTION_MASK_ISA_SAHF_UNSET OPTION_MASK_ISA_SAHF
2139 #define OPTION_MASK_ISA_MOVBE_UNSET OPTION_MASK_ISA_MOVBE
2140 #define OPTION_MASK_ISA_CRC32_UNSET OPTION_MASK_ISA_CRC32
2141
2142 #define OPTION_MASK_ISA_FSGSBASE_UNSET OPTION_MASK_ISA_FSGSBASE
2143 #define OPTION_MASK_ISA_RDRND_UNSET OPTION_MASK_ISA_RDRND
2144 #define OPTION_MASK_ISA_F16C_UNSET OPTION_MASK_ISA_F16C
2145
2146 /* Vectorization library interface and handlers.  */
2147 static tree (*ix86_veclib_handler) (enum built_in_function, tree, tree);
2148
2149 static tree ix86_veclibabi_svml (enum built_in_function, tree, tree);
2150 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
2151
2152 /* Processor target table, indexed by processor number */
2153 struct ptt
2154 {
2155   const struct processor_costs *cost;           /* Processor costs */
2156   const int align_loop;                         /* Default alignments.  */
2157   const int align_loop_max_skip;
2158   const int align_jump;
2159   const int align_jump_max_skip;
2160   const int align_func;
2161 };
2162
2163 static const struct ptt processor_target_table[PROCESSOR_max] =
2164 {
2165   {&i386_cost, 4, 3, 4, 3, 4},
2166   {&i486_cost, 16, 15, 16, 15, 16},
2167   {&pentium_cost, 16, 7, 16, 7, 16},
2168   {&pentiumpro_cost, 16, 15, 16, 10, 16},
2169   {&geode_cost, 0, 0, 0, 0, 0},
2170   {&k6_cost, 32, 7, 32, 7, 32},
2171   {&athlon_cost, 16, 7, 16, 7, 16},
2172   {&pentium4_cost, 0, 0, 0, 0, 0},
2173   {&k8_cost, 16, 7, 16, 7, 16},
2174   {&nocona_cost, 0, 0, 0, 0, 0},
2175   {&core2_cost, 16, 10, 16, 10, 16},
2176   {&generic32_cost, 16, 7, 16, 7, 16},
2177   {&generic64_cost, 16, 10, 16, 10, 16},
2178   {&amdfam10_cost, 32, 24, 32, 7, 32},
2179   {&bdver1_cost, 32, 24, 32, 7, 32},
2180   {&atom_cost, 16, 7, 16, 7, 16}
2181 };
2182
2183 static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2184 {
2185   "generic",
2186   "i386",
2187   "i486",
2188   "pentium",
2189   "pentium-mmx",
2190   "pentiumpro",
2191   "pentium2",
2192   "pentium3",
2193   "pentium4",
2194   "pentium-m",
2195   "prescott",
2196   "nocona",
2197   "core2",
2198   "atom",
2199   "geode",
2200   "k6",
2201   "k6-2",
2202   "k6-3",
2203   "athlon",
2204   "athlon-4",
2205   "k8",
2206   "amdfam10",
2207   "bdver1"
2208 };
2209 \f
2210 /* Return true if a red-zone is in use.  */
2211
2212 static inline bool
2213 ix86_using_red_zone (void)
2214 {
2215   return TARGET_RED_ZONE && !TARGET_64BIT_MS_ABI;
2216 }
2217
2218 /* Implement TARGET_HANDLE_OPTION.  */
2219
2220 static bool
2221 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
2222 {
2223   switch (code)
2224     {
2225     case OPT_mmmx:
2226       if (value)
2227         {
2228           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
2229           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
2230         }
2231       else
2232         {
2233           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
2234           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
2235         }
2236       return true;
2237
2238     case OPT_m3dnow:
2239       if (value)
2240         {
2241           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
2242           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
2243         }
2244       else
2245         {
2246           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
2247           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
2248         }
2249       return true;
2250
2251     case OPT_m3dnowa:
2252       return false;
2253
2254     case OPT_msse:
2255       if (value)
2256         {
2257           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
2258           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
2259         }
2260       else
2261         {
2262           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
2263           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
2264         }
2265       return true;
2266
2267     case OPT_msse2:
2268       if (value)
2269         {
2270           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
2271           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
2272         }
2273       else
2274         {
2275           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
2276           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
2277         }
2278       return true;
2279
2280     case OPT_msse3:
2281       if (value)
2282         {
2283           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
2284           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
2285         }
2286       else
2287         {
2288           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
2289           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
2290         }
2291       return true;
2292
2293     case OPT_mssse3:
2294       if (value)
2295         {
2296           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
2297           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
2298         }
2299       else
2300         {
2301           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
2302           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
2303         }
2304       return true;
2305
2306     case OPT_msse4_1:
2307       if (value)
2308         {
2309           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
2310           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
2311         }
2312       else
2313         {
2314           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
2315           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
2316         }
2317       return true;
2318
2319     case OPT_msse4_2:
2320       if (value)
2321         {
2322           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
2323           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
2324         }
2325       else
2326         {
2327           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
2328           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
2329         }
2330       return true;
2331
2332     case OPT_mavx:
2333       if (value)
2334         {
2335           ix86_isa_flags |= OPTION_MASK_ISA_AVX_SET;
2336           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_SET;
2337         }
2338       else
2339         {
2340           ix86_isa_flags &= ~OPTION_MASK_ISA_AVX_UNSET;
2341           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AVX_UNSET;
2342         }
2343       return true;
2344
2345     case OPT_mfma:
2346       if (value)
2347         {
2348           ix86_isa_flags |= OPTION_MASK_ISA_FMA_SET;
2349           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_SET;
2350         }
2351       else
2352         {
2353           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA_UNSET;
2354           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA_UNSET;
2355         }
2356       return true;
2357
2358     case OPT_msse4:
2359       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
2360       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
2361       return true;
2362
2363     case OPT_mno_sse4:
2364       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
2365       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
2366       return true;
2367
2368     case OPT_msse4a:
2369       if (value)
2370         {
2371           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
2372           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
2373         }
2374       else
2375         {
2376           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
2377           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
2378         }
2379       return true;
2380
2381     case OPT_mfma4:
2382       if (value)
2383         {
2384           ix86_isa_flags |= OPTION_MASK_ISA_FMA4_SET;
2385           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_SET;
2386         }
2387       else
2388         {
2389           ix86_isa_flags &= ~OPTION_MASK_ISA_FMA4_UNSET;
2390           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FMA4_UNSET;
2391         }
2392       return true;
2393
2394    case OPT_mxop:
2395       if (value)
2396         {
2397           ix86_isa_flags |= OPTION_MASK_ISA_XOP_SET;
2398           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_SET;
2399         }
2400       else
2401         {
2402           ix86_isa_flags &= ~OPTION_MASK_ISA_XOP_UNSET;
2403           ix86_isa_flags_explicit |= OPTION_MASK_ISA_XOP_UNSET;
2404         }
2405       return true;
2406
2407    case OPT_mlwp:
2408       if (value)
2409         {
2410           ix86_isa_flags |= OPTION_MASK_ISA_LWP_SET;
2411           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_SET;
2412         }
2413       else
2414         {
2415           ix86_isa_flags &= ~OPTION_MASK_ISA_LWP_UNSET;
2416           ix86_isa_flags_explicit |= OPTION_MASK_ISA_LWP_UNSET;
2417         }
2418       return true;
2419
2420     case OPT_mabm:
2421       if (value)
2422         {
2423           ix86_isa_flags |= OPTION_MASK_ISA_ABM_SET;
2424           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_SET;
2425         }
2426       else
2427         {
2428           ix86_isa_flags &= ~OPTION_MASK_ISA_ABM_UNSET;
2429           ix86_isa_flags_explicit |= OPTION_MASK_ISA_ABM_UNSET;
2430         }
2431       return true;
2432
2433     case OPT_mpopcnt:
2434       if (value)
2435         {
2436           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT_SET;
2437           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_SET;
2438         }
2439       else
2440         {
2441           ix86_isa_flags &= ~OPTION_MASK_ISA_POPCNT_UNSET;
2442           ix86_isa_flags_explicit |= OPTION_MASK_ISA_POPCNT_UNSET;
2443         }
2444       return true;
2445
2446     case OPT_msahf:
2447       if (value)
2448         {
2449           ix86_isa_flags |= OPTION_MASK_ISA_SAHF_SET;
2450           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_SET;
2451         }
2452       else
2453         {
2454           ix86_isa_flags &= ~OPTION_MASK_ISA_SAHF_UNSET;
2455           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SAHF_UNSET;
2456         }
2457       return true;
2458
2459     case OPT_mcx16:
2460       if (value)
2461         {
2462           ix86_isa_flags |= OPTION_MASK_ISA_CX16_SET;
2463           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_SET;
2464         }
2465       else
2466         {
2467           ix86_isa_flags &= ~OPTION_MASK_ISA_CX16_UNSET;
2468           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CX16_UNSET;
2469         }
2470       return true;
2471
2472     case OPT_mmovbe:
2473       if (value)
2474         {
2475           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE_SET;
2476           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_SET;
2477         }
2478       else
2479         {
2480           ix86_isa_flags &= ~OPTION_MASK_ISA_MOVBE_UNSET;
2481           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MOVBE_UNSET;
2482         }
2483       return true;
2484
2485     case OPT_mcrc32:
2486       if (value)
2487         {
2488           ix86_isa_flags |= OPTION_MASK_ISA_CRC32_SET;
2489           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_SET;
2490         }
2491       else
2492         {
2493           ix86_isa_flags &= ~OPTION_MASK_ISA_CRC32_UNSET;
2494           ix86_isa_flags_explicit |= OPTION_MASK_ISA_CRC32_UNSET;
2495         }
2496       return true;
2497
2498     case OPT_maes:
2499       if (value)
2500         {
2501           ix86_isa_flags |= OPTION_MASK_ISA_AES_SET;
2502           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_SET;
2503         }
2504       else
2505         {
2506           ix86_isa_flags &= ~OPTION_MASK_ISA_AES_UNSET;
2507           ix86_isa_flags_explicit |= OPTION_MASK_ISA_AES_UNSET;
2508         }
2509       return true;
2510
2511     case OPT_mpclmul:
2512       if (value)
2513         {
2514           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL_SET;
2515           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_SET;
2516         }
2517       else
2518         {
2519           ix86_isa_flags &= ~OPTION_MASK_ISA_PCLMUL_UNSET;
2520           ix86_isa_flags_explicit |= OPTION_MASK_ISA_PCLMUL_UNSET;
2521         }
2522       return true;
2523
2524     case OPT_mfsgsbase:
2525       if (value)
2526         {
2527           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE_SET;
2528           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_SET;
2529         }
2530       else
2531         {
2532           ix86_isa_flags &= ~OPTION_MASK_ISA_FSGSBASE_UNSET;
2533           ix86_isa_flags_explicit |= OPTION_MASK_ISA_FSGSBASE_UNSET;
2534         }
2535       return true;
2536
2537     case OPT_mrdrnd:
2538       if (value)
2539         {
2540           ix86_isa_flags |= OPTION_MASK_ISA_RDRND_SET;
2541           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_SET;
2542         }
2543       else
2544         {
2545           ix86_isa_flags &= ~OPTION_MASK_ISA_RDRND_UNSET;
2546           ix86_isa_flags_explicit |= OPTION_MASK_ISA_RDRND_UNSET;
2547         }
2548       return true;
2549
2550     case OPT_mf16c:
2551       if (value)
2552         {
2553           ix86_isa_flags |= OPTION_MASK_ISA_F16C_SET;
2554           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_SET;
2555         }
2556       else
2557         {
2558           ix86_isa_flags &= ~OPTION_MASK_ISA_F16C_UNSET;
2559           ix86_isa_flags_explicit |= OPTION_MASK_ISA_F16C_UNSET;
2560         }
2561       return true;
2562
2563     default:
2564       return true;
2565     }
2566 }
2567 \f
2568 /* Return a string that documents the current -m options.  The caller is
2569    responsible for freeing the string.  */
2570
2571 static char *
2572 ix86_target_string (int isa, int flags, const char *arch, const char *tune,
2573                     const char *fpmath, bool add_nl_p)
2574 {
2575   struct ix86_target_opts
2576   {
2577     const char *option;         /* option string */
2578     int mask;                   /* isa mask options */
2579   };
2580
2581   /* This table is ordered so that options like -msse4.2 that imply
2582      preceding options while match those first.  */
2583   static struct ix86_target_opts isa_opts[] =
2584   {
2585     { "-m64",           OPTION_MASK_ISA_64BIT },
2586     { "-mfma4",         OPTION_MASK_ISA_FMA4 },
2587     { "-mfma",          OPTION_MASK_ISA_FMA },
2588     { "-mxop",          OPTION_MASK_ISA_XOP },
2589     { "-mlwp",          OPTION_MASK_ISA_LWP },
2590     { "-msse4a",        OPTION_MASK_ISA_SSE4A },
2591     { "-msse4.2",       OPTION_MASK_ISA_SSE4_2 },
2592     { "-msse4.1",       OPTION_MASK_ISA_SSE4_1 },
2593     { "-mssse3",        OPTION_MASK_ISA_SSSE3 },
2594     { "-msse3",         OPTION_MASK_ISA_SSE3 },
2595     { "-msse2",         OPTION_MASK_ISA_SSE2 },
2596     { "-msse",          OPTION_MASK_ISA_SSE },
2597     { "-m3dnow",        OPTION_MASK_ISA_3DNOW },
2598     { "-m3dnowa",       OPTION_MASK_ISA_3DNOW_A },
2599     { "-mmmx",          OPTION_MASK_ISA_MMX },
2600     { "-mabm",          OPTION_MASK_ISA_ABM },
2601     { "-mpopcnt",       OPTION_MASK_ISA_POPCNT },
2602     { "-mmovbe",        OPTION_MASK_ISA_MOVBE },
2603     { "-mcrc32",        OPTION_MASK_ISA_CRC32 },
2604     { "-maes",          OPTION_MASK_ISA_AES },
2605     { "-mpclmul",       OPTION_MASK_ISA_PCLMUL },
2606     { "-mfsgsbase",     OPTION_MASK_ISA_FSGSBASE },
2607     { "-mrdrnd",        OPTION_MASK_ISA_RDRND },
2608     { "-mf16c",         OPTION_MASK_ISA_F16C },
2609   };
2610
2611   /* Flag options.  */
2612   static struct ix86_target_opts flag_opts[] =
2613   {
2614     { "-m128bit-long-double",           MASK_128BIT_LONG_DOUBLE },
2615     { "-m80387",                        MASK_80387 },
2616     { "-maccumulate-outgoing-args",     MASK_ACCUMULATE_OUTGOING_ARGS },
2617     { "-malign-double",                 MASK_ALIGN_DOUBLE },
2618     { "-mcld",                          MASK_CLD },
2619     { "-mfp-ret-in-387",                MASK_FLOAT_RETURNS },
2620     { "-mieee-fp",                      MASK_IEEE_FP },
2621     { "-minline-all-stringops",         MASK_INLINE_ALL_STRINGOPS },
2622     { "-minline-stringops-dynamically", MASK_INLINE_STRINGOPS_DYNAMICALLY },
2623     { "-mms-bitfields",                 MASK_MS_BITFIELD_LAYOUT },
2624     { "-mno-align-stringops",           MASK_NO_ALIGN_STRINGOPS },
2625     { "-mno-fancy-math-387",            MASK_NO_FANCY_MATH_387 },
2626     { "-mno-push-args",                 MASK_NO_PUSH_ARGS },
2627     { "-mno-red-zone",                  MASK_NO_RED_ZONE },
2628     { "-momit-leaf-frame-pointer",      MASK_OMIT_LEAF_FRAME_POINTER },
2629     { "-mrecip",                        MASK_RECIP },
2630     { "-mrtd",                          MASK_RTD },
2631     { "-msseregparm",                   MASK_SSEREGPARM },
2632     { "-mstack-arg-probe",              MASK_STACK_PROBE },
2633     { "-mtls-direct-seg-refs",          MASK_TLS_DIRECT_SEG_REFS },
2634     { "-mvect8-ret-in-mem",             MASK_VECT8_RETURNS },
2635     { "-m8bit-idiv",                    MASK_USE_8BIT_IDIV },
2636   };
2637
2638   const char *opts[ARRAY_SIZE (isa_opts) + ARRAY_SIZE (flag_opts) + 6][2];
2639
2640   char isa_other[40];
2641   char target_other[40];
2642   unsigned num = 0;
2643   unsigned i, j;
2644   char *ret;
2645   char *ptr;
2646   size_t len;
2647   size_t line_len;
2648   size_t sep_len;
2649
2650   memset (opts, '\0', sizeof (opts));
2651
2652   /* Add -march= option.  */
2653   if (arch)
2654     {
2655       opts[num][0] = "-march=";
2656       opts[num++][1] = arch;
2657     }
2658
2659   /* Add -mtune= option.  */
2660   if (tune)
2661     {
2662       opts[num][0] = "-mtune=";
2663       opts[num++][1] = tune;
2664     }
2665
2666   /* Pick out the options in isa options.  */
2667   for (i = 0; i < ARRAY_SIZE (isa_opts); i++)
2668     {
2669       if ((isa & isa_opts[i].mask) != 0)
2670         {
2671           opts[num++][0] = isa_opts[i].option;
2672           isa &= ~ isa_opts[i].mask;
2673         }
2674     }
2675
2676   if (isa && add_nl_p)
2677     {
2678       opts[num++][0] = isa_other;
2679       sprintf (isa_other, "(other isa: %#x)", isa);
2680     }
2681
2682   /* Add flag options.  */
2683   for (i = 0; i < ARRAY_SIZE (flag_opts); i++)
2684     {
2685       if ((flags & flag_opts[i].mask) != 0)
2686         {
2687           opts[num++][0] = flag_opts[i].option;
2688           flags &= ~ flag_opts[i].mask;
2689         }
2690     }
2691
2692   if (flags && add_nl_p)
2693     {
2694       opts[num++][0] = target_other;
2695       sprintf (target_other, "(other flags: %#x)", flags);
2696     }
2697
2698   /* Add -fpmath= option.  */
2699   if (fpmath)
2700     {
2701       opts[num][0] = "-mfpmath=";
2702       opts[num++][1] = fpmath;
2703     }
2704
2705   /* Any options?  */
2706   if (num == 0)
2707     return NULL;
2708
2709   gcc_assert (num < ARRAY_SIZE (opts));
2710
2711   /* Size the string.  */
2712   len = 0;
2713   sep_len = (add_nl_p) ? 3 : 1;
2714   for (i = 0; i < num; i++)
2715     {
2716       len += sep_len;
2717       for (j = 0; j < 2; j++)
2718         if (opts[i][j])
2719           len += strlen (opts[i][j]);
2720     }
2721
2722   /* Build the string.  */
2723   ret = ptr = (char *) xmalloc (len);
2724   line_len = 0;
2725
2726   for (i = 0; i < num; i++)
2727     {
2728       size_t len2[2];
2729
2730       for (j = 0; j < 2; j++)
2731         len2[j] = (opts[i][j]) ? strlen (opts[i][j]) : 0;
2732
2733       if (i != 0)
2734         {
2735           *ptr++ = ' ';
2736           line_len++;
2737
2738           if (add_nl_p && line_len + len2[0] + len2[1] > 70)
2739             {
2740               *ptr++ = '\\';
2741               *ptr++ = '\n';
2742               line_len = 0;
2743             }
2744         }
2745
2746       for (j = 0; j < 2; j++)
2747         if (opts[i][j])
2748           {
2749             memcpy (ptr, opts[i][j], len2[j]);
2750             ptr += len2[j];
2751             line_len += len2[j];
2752           }
2753     }
2754
2755   *ptr = '\0';
2756   gcc_assert (ret + len >= ptr);
2757
2758   return ret;
2759 }
2760
2761 /* Return TRUE if software prefetching is beneficial for the
2762    given CPU. */
2763
2764 static bool
2765 software_prefetching_beneficial_p (void)
2766 {
2767   switch (ix86_tune)
2768     {
2769     case PROCESSOR_GEODE:
2770     case PROCESSOR_K6:
2771     case PROCESSOR_ATHLON:
2772     case PROCESSOR_K8:
2773     case PROCESSOR_AMDFAM10:
2774       return true;
2775
2776     default:
2777       return false;
2778     }
2779 }
2780
2781 /* Return true, if profiling code should be emitted before
2782    prologue. Otherwise it returns false.
2783    Note: For x86 with "hotfix" it is sorried.  */
2784 static bool
2785 ix86_profile_before_prologue (void)
2786 {
2787   return flag_fentry != 0;
2788 }
2789
2790 /* Function that is callable from the debugger to print the current
2791    options.  */
2792 void
2793 ix86_debug_options (void)
2794 {
2795   char *opts = ix86_target_string (ix86_isa_flags, target_flags,
2796                                    ix86_arch_string, ix86_tune_string,
2797                                    ix86_fpmath_string, true);
2798
2799   if (opts)
2800     {
2801       fprintf (stderr, "%s\n\n", opts);
2802       free (opts);
2803     }
2804   else
2805     fputs ("<no options>\n\n", stderr);
2806
2807   return;
2808 }
2809 \f
2810 /* Override various settings based on options.  If MAIN_ARGS_P, the
2811    options are from the command line, otherwise they are from
2812    attributes.  */
2813
2814 static void
2815 ix86_option_override_internal (bool main_args_p)
2816 {
2817   int i;
2818   unsigned int ix86_arch_mask, ix86_tune_mask;
2819   const bool ix86_tune_specified = (ix86_tune_string != NULL);
2820   const char *prefix;
2821   const char *suffix;
2822   const char *sw;
2823
2824   /* Comes from final.c -- no real reason to change it.  */
2825 #define MAX_CODE_ALIGN 16
2826
2827   enum pta_flags
2828     {
2829       PTA_SSE = 1 << 0,
2830       PTA_SSE2 = 1 << 1,
2831       PTA_SSE3 = 1 << 2,
2832       PTA_MMX = 1 << 3,
2833       PTA_PREFETCH_SSE = 1 << 4,
2834       PTA_3DNOW = 1 << 5,
2835       PTA_3DNOW_A = 1 << 6,
2836       PTA_64BIT = 1 << 7,
2837       PTA_SSSE3 = 1 << 8,
2838       PTA_CX16 = 1 << 9,
2839       PTA_POPCNT = 1 << 10,
2840       PTA_ABM = 1 << 11,
2841       PTA_SSE4A = 1 << 12,
2842       PTA_NO_SAHF = 1 << 13,
2843       PTA_SSE4_1 = 1 << 14,
2844       PTA_SSE4_2 = 1 << 15,
2845       PTA_AES = 1 << 16,
2846       PTA_PCLMUL = 1 << 17,
2847       PTA_AVX = 1 << 18,
2848       PTA_FMA = 1 << 19,
2849       PTA_MOVBE = 1 << 20,
2850       PTA_FMA4 = 1 << 21,
2851       PTA_XOP = 1 << 22,
2852       PTA_LWP = 1 << 23,
2853       PTA_FSGSBASE = 1 << 24,
2854       PTA_RDRND = 1 << 25,
2855       PTA_F16C = 1 << 26
2856     };
2857
2858   static struct pta
2859     {
2860       const char *const name;           /* processor name or nickname.  */
2861       const enum processor_type processor;
2862       const enum attr_cpu schedule;
2863       const unsigned /*enum pta_flags*/ flags;
2864     }
2865   const processor_alias_table[] =
2866     {
2867       {"i386", PROCESSOR_I386, CPU_NONE, 0},
2868       {"i486", PROCESSOR_I486, CPU_NONE, 0},
2869       {"i586", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2870       {"pentium", PROCESSOR_PENTIUM, CPU_PENTIUM, 0},
2871       {"pentium-mmx", PROCESSOR_PENTIUM, CPU_PENTIUM, PTA_MMX},
2872       {"winchip-c6", PROCESSOR_I486, CPU_NONE, PTA_MMX},
2873       {"winchip2", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2874       {"c3", PROCESSOR_I486, CPU_NONE, PTA_MMX | PTA_3DNOW},
2875       {"c3-2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX | PTA_SSE},
2876       {"i686", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2877       {"pentiumpro", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, 0},
2878       {"pentium2", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO, PTA_MMX},
2879       {"pentium3", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2880         PTA_MMX | PTA_SSE},
2881       {"pentium3m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2882         PTA_MMX | PTA_SSE},
2883       {"pentium-m", PROCESSOR_PENTIUMPRO, CPU_PENTIUMPRO,
2884         PTA_MMX | PTA_SSE | PTA_SSE2},
2885       {"pentium4", PROCESSOR_PENTIUM4, CPU_NONE,
2886         PTA_MMX |PTA_SSE | PTA_SSE2},
2887       {"pentium4m", PROCESSOR_PENTIUM4, CPU_NONE,
2888         PTA_MMX | PTA_SSE | PTA_SSE2},
2889       {"prescott", PROCESSOR_NOCONA, CPU_NONE,
2890         PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2891       {"nocona", PROCESSOR_NOCONA, CPU_NONE,
2892         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2893         | PTA_CX16 | PTA_NO_SAHF},
2894       {"core2", PROCESSOR_CORE2, CPU_CORE2,
2895         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2896         | PTA_SSSE3 | PTA_CX16},
2897       {"atom", PROCESSOR_ATOM, CPU_ATOM,
2898         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2899         | PTA_SSSE3 | PTA_CX16 | PTA_MOVBE},
2900       {"geode", PROCESSOR_GEODE, CPU_GEODE,
2901         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A |PTA_PREFETCH_SSE},
2902       {"k6", PROCESSOR_K6, CPU_K6, PTA_MMX},
2903       {"k6-2", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2904       {"k6-3", PROCESSOR_K6, CPU_K6, PTA_MMX | PTA_3DNOW},
2905       {"athlon", PROCESSOR_ATHLON, CPU_ATHLON,
2906         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2907       {"athlon-tbird", PROCESSOR_ATHLON, CPU_ATHLON,
2908         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_PREFETCH_SSE},
2909       {"athlon-4", PROCESSOR_ATHLON, CPU_ATHLON,
2910         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2911       {"athlon-xp", PROCESSOR_ATHLON, CPU_ATHLON,
2912         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2913       {"athlon-mp", PROCESSOR_ATHLON, CPU_ATHLON,
2914         PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE},
2915       {"x86-64", PROCESSOR_K8, CPU_K8,
2916         PTA_64BIT | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
2917       {"k8", PROCESSOR_K8, CPU_K8,
2918         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2919         | PTA_SSE2 | PTA_NO_SAHF},
2920       {"k8-sse3", PROCESSOR_K8, CPU_K8,
2921         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2922         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2923       {"opteron", PROCESSOR_K8, CPU_K8,
2924         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2925         | PTA_SSE2 | PTA_NO_SAHF},
2926       {"opteron-sse3", PROCESSOR_K8, CPU_K8,
2927         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2928         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2929       {"athlon64", PROCESSOR_K8, CPU_K8,
2930         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2931         | PTA_SSE2 | PTA_NO_SAHF},
2932       {"athlon64-sse3", PROCESSOR_K8, CPU_K8,
2933         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2934         | PTA_SSE2 | PTA_SSE3 | PTA_NO_SAHF},
2935       {"athlon-fx", PROCESSOR_K8, CPU_K8,
2936         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2937         | PTA_SSE2 | PTA_NO_SAHF},
2938       {"amdfam10", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2939         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2940         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2941       {"barcelona", PROCESSOR_AMDFAM10, CPU_AMDFAM10,
2942         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2943         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM},
2944       {"bdver1", PROCESSOR_BDVER1, CPU_BDVER1,
2945         PTA_64BIT | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A | PTA_SSE
2946         | PTA_SSE2 | PTA_SSE3 | PTA_SSE4A | PTA_CX16 | PTA_ABM
2947         | PTA_SSSE3 | PTA_SSE4_1 | PTA_SSE4_2 | PTA_AES
2948         | PTA_PCLMUL | PTA_AVX | PTA_FMA4 | PTA_XOP | PTA_LWP},
2949       {"generic32", PROCESSOR_GENERIC32, CPU_PENTIUMPRO,
2950         0 /* flags are only used for -march switch.  */ },
2951       {"generic64", PROCESSOR_GENERIC64, CPU_GENERIC64,
2952         PTA_64BIT /* flags are only used for -march switch.  */ },
2953     };
2954
2955   int const pta_size = ARRAY_SIZE (processor_alias_table);
2956
2957   /* Set up prefix/suffix so the error messages refer to either the command
2958      line argument, or the attribute(target).  */
2959   if (main_args_p)
2960     {
2961       prefix = "-m";
2962       suffix = "";
2963       sw = "switch";
2964     }
2965   else
2966     {
2967       prefix = "option(\"";
2968       suffix = "\")";
2969       sw = "attribute";
2970     }
2971
2972 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2973   SUBTARGET_OVERRIDE_OPTIONS;
2974 #endif
2975
2976 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2977   SUBSUBTARGET_OVERRIDE_OPTIONS;
2978 #endif
2979
2980   /* -fPIC is the default for x86_64.  */
2981   if (TARGET_MACHO && TARGET_64BIT)
2982     flag_pic = 2;
2983
2984   /* Need to check -mtune=generic first.  */
2985   if (ix86_tune_string)
2986     {
2987       if (!strcmp (ix86_tune_string, "generic")
2988           || !strcmp (ix86_tune_string, "i686")
2989           /* As special support for cross compilers we read -mtune=native
2990              as -mtune=generic.  With native compilers we won't see the
2991              -mtune=native, as it was changed by the driver.  */
2992           || !strcmp (ix86_tune_string, "native"))
2993         {
2994           if (TARGET_64BIT)
2995             ix86_tune_string = "generic64";
2996           else
2997             ix86_tune_string = "generic32";
2998         }
2999       /* If this call is for setting the option attribute, allow the
3000          generic32/generic64 that was previously set.  */
3001       else if (!main_args_p
3002                && (!strcmp (ix86_tune_string, "generic32")
3003                    || !strcmp (ix86_tune_string, "generic64")))
3004         ;
3005       else if (!strncmp (ix86_tune_string, "generic", 7))
3006         error ("bad value (%s) for %stune=%s %s",
3007                ix86_tune_string, prefix, suffix, sw);
3008       else if (!strcmp (ix86_tune_string, "x86-64"))
3009         warning (OPT_Wdeprecated, "%stune=x86-64%s is deprecated.  Use "
3010                  "%stune=k8%s or %stune=generic%s instead as appropriate.",
3011                  prefix, suffix, prefix, suffix, prefix, suffix);
3012     }
3013   else
3014     {
3015       if (ix86_arch_string)
3016         ix86_tune_string = ix86_arch_string;
3017       if (!ix86_tune_string)
3018         {
3019           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
3020           ix86_tune_defaulted = 1;
3021         }
3022
3023       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
3024          need to use a sensible tune option.  */
3025       if (!strcmp (ix86_tune_string, "generic")
3026           || !strcmp (ix86_tune_string, "x86-64")
3027           || !strcmp (ix86_tune_string, "i686"))
3028         {
3029           if (TARGET_64BIT)
3030             ix86_tune_string = "generic64";
3031           else
3032             ix86_tune_string = "generic32";
3033         }
3034     }
3035
3036   if (ix86_stringop_string)
3037     {
3038       if (!strcmp (ix86_stringop_string, "rep_byte"))
3039         stringop_alg = rep_prefix_1_byte;
3040       else if (!strcmp (ix86_stringop_string, "libcall"))
3041         stringop_alg = libcall;
3042       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
3043         stringop_alg = rep_prefix_4_byte;
3044       else if (!strcmp (ix86_stringop_string, "rep_8byte")
3045                && TARGET_64BIT)
3046         /* rep; movq isn't available in 32-bit code.  */
3047         stringop_alg = rep_prefix_8_byte;
3048       else if (!strcmp (ix86_stringop_string, "byte_loop"))
3049         stringop_alg = loop_1_byte;
3050       else if (!strcmp (ix86_stringop_string, "loop"))
3051         stringop_alg = loop;
3052       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
3053         stringop_alg = unrolled_loop;
3054       else
3055         error ("bad value (%s) for %sstringop-strategy=%s %s",
3056                ix86_stringop_string, prefix, suffix, sw);
3057     }
3058
3059   if (!ix86_arch_string)
3060     ix86_arch_string = TARGET_64BIT ? "x86-64" : SUBTARGET32_DEFAULT_CPU;
3061   else
3062     ix86_arch_specified = 1;
3063
3064   /* Validate -mabi= value.  */
3065   if (ix86_abi_string)
3066     {
3067       if (strcmp (ix86_abi_string, "sysv") == 0)
3068         ix86_abi = SYSV_ABI;
3069       else if (strcmp (ix86_abi_string, "ms") == 0)
3070         ix86_abi = MS_ABI;
3071       else
3072         error ("unknown ABI (%s) for %sabi=%s %s",
3073                ix86_abi_string, prefix, suffix, sw);
3074     }
3075   else
3076     ix86_abi = DEFAULT_ABI;
3077
3078   if (ix86_cmodel_string != 0)
3079     {
3080       if (!strcmp (ix86_cmodel_string, "small"))
3081         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3082       else if (!strcmp (ix86_cmodel_string, "medium"))
3083         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
3084       else if (!strcmp (ix86_cmodel_string, "large"))
3085         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
3086       else if (flag_pic)
3087         error ("code model %s does not support PIC mode", ix86_cmodel_string);
3088       else if (!strcmp (ix86_cmodel_string, "32"))
3089         ix86_cmodel = CM_32;
3090       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
3091         ix86_cmodel = CM_KERNEL;
3092       else
3093         error ("bad value (%s) for %scmodel=%s %s",
3094                ix86_cmodel_string, prefix, suffix, sw);
3095     }
3096   else
3097     {
3098       /* For TARGET_64BIT and MS_ABI, force pic on, in order to enable the
3099          use of rip-relative addressing.  This eliminates fixups that
3100          would otherwise be needed if this object is to be placed in a
3101          DLL, and is essentially just as efficient as direct addressing.  */
3102       if (TARGET_64BIT && DEFAULT_ABI == MS_ABI)
3103         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
3104       else if (TARGET_64BIT)
3105         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
3106       else
3107         ix86_cmodel = CM_32;
3108     }
3109   if (ix86_asm_string != 0)
3110     {
3111       if (! TARGET_MACHO
3112           && !strcmp (ix86_asm_string, "intel"))
3113         ix86_asm_dialect = ASM_INTEL;
3114       else if (!strcmp (ix86_asm_string, "att"))
3115         ix86_asm_dialect = ASM_ATT;
3116       else
3117         error ("bad value (%s) for %sasm=%s %s",
3118                ix86_asm_string, prefix, suffix, sw);
3119     }
3120   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
3121     error ("code model %qs not supported in the %s bit mode",
3122            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
3123   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
3124     sorry ("%i-bit mode not compiled in",
3125            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
3126
3127   for (i = 0; i < pta_size; i++)
3128     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
3129       {
3130         ix86_schedule = processor_alias_table[i].schedule;
3131         ix86_arch = processor_alias_table[i].processor;
3132         /* Default cpu tuning to the architecture.  */
3133         ix86_tune = ix86_arch;
3134
3135         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3136           error ("CPU you selected does not support x86-64 "
3137                  "instruction set");
3138
3139         if (processor_alias_table[i].flags & PTA_MMX
3140             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
3141           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
3142         if (processor_alias_table[i].flags & PTA_3DNOW
3143             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
3144           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
3145         if (processor_alias_table[i].flags & PTA_3DNOW_A
3146             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
3147           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
3148         if (processor_alias_table[i].flags & PTA_SSE
3149             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
3150           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
3151         if (processor_alias_table[i].flags & PTA_SSE2
3152             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
3153           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
3154         if (processor_alias_table[i].flags & PTA_SSE3
3155             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
3156           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
3157         if (processor_alias_table[i].flags & PTA_SSSE3
3158             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
3159           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
3160         if (processor_alias_table[i].flags & PTA_SSE4_1
3161             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
3162           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
3163         if (processor_alias_table[i].flags & PTA_SSE4_2
3164             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
3165           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
3166         if (processor_alias_table[i].flags & PTA_AVX
3167             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AVX))
3168           ix86_isa_flags |= OPTION_MASK_ISA_AVX;
3169         if (processor_alias_table[i].flags & PTA_FMA
3170             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA))
3171           ix86_isa_flags |= OPTION_MASK_ISA_FMA;
3172         if (processor_alias_table[i].flags & PTA_SSE4A
3173             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
3174           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
3175         if (processor_alias_table[i].flags & PTA_FMA4
3176             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FMA4))
3177           ix86_isa_flags |= OPTION_MASK_ISA_FMA4;
3178         if (processor_alias_table[i].flags & PTA_XOP
3179             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_XOP))
3180           ix86_isa_flags |= OPTION_MASK_ISA_XOP;
3181         if (processor_alias_table[i].flags & PTA_LWP
3182             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_LWP))
3183           ix86_isa_flags |= OPTION_MASK_ISA_LWP;
3184         if (processor_alias_table[i].flags & PTA_ABM
3185             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_ABM))
3186           ix86_isa_flags |= OPTION_MASK_ISA_ABM;
3187         if (processor_alias_table[i].flags & PTA_CX16
3188             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_CX16))
3189           ix86_isa_flags |= OPTION_MASK_ISA_CX16;
3190         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM)
3191             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_POPCNT))
3192           ix86_isa_flags |= OPTION_MASK_ISA_POPCNT;
3193         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF))
3194             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SAHF))
3195           ix86_isa_flags |= OPTION_MASK_ISA_SAHF;
3196         if (processor_alias_table[i].flags & PTA_MOVBE
3197             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MOVBE))
3198           ix86_isa_flags |= OPTION_MASK_ISA_MOVBE;
3199         if (processor_alias_table[i].flags & PTA_AES
3200             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_AES))
3201           ix86_isa_flags |= OPTION_MASK_ISA_AES;
3202         if (processor_alias_table[i].flags & PTA_PCLMUL
3203             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_PCLMUL))
3204           ix86_isa_flags |= OPTION_MASK_ISA_PCLMUL;
3205         if (processor_alias_table[i].flags & PTA_FSGSBASE
3206             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_FSGSBASE))
3207           ix86_isa_flags |= OPTION_MASK_ISA_FSGSBASE;
3208         if (processor_alias_table[i].flags & PTA_RDRND
3209             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_RDRND))
3210           ix86_isa_flags |= OPTION_MASK_ISA_RDRND;
3211         if (processor_alias_table[i].flags & PTA_F16C
3212             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_F16C))
3213           ix86_isa_flags |= OPTION_MASK_ISA_F16C;
3214         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
3215           x86_prefetch_sse = true;
3216
3217         break;
3218       }
3219
3220   if (!strcmp (ix86_arch_string, "generic"))
3221     error ("generic CPU can be used only for %stune=%s %s",
3222            prefix, suffix, sw);
3223   else if (!strncmp (ix86_arch_string, "generic", 7) || i == pta_size)
3224     error ("bad value (%s) for %sarch=%s %s",
3225            ix86_arch_string, prefix, suffix, sw);
3226
3227   ix86_arch_mask = 1u << ix86_arch;
3228   for (i = 0; i < X86_ARCH_LAST; ++i)
3229     ix86_arch_features[i] = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3230
3231   for (i = 0; i < pta_size; i++)
3232     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
3233       {
3234         ix86_schedule = processor_alias_table[i].schedule;
3235         ix86_tune = processor_alias_table[i].processor;
3236         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
3237           {
3238             if (ix86_tune_defaulted)
3239               {
3240                 ix86_tune_string = "x86-64";
3241                 for (i = 0; i < pta_size; i++)
3242                   if (! strcmp (ix86_tune_string,
3243                                 processor_alias_table[i].name))
3244                     break;
3245                 ix86_schedule = processor_alias_table[i].schedule;
3246                 ix86_tune = processor_alias_table[i].processor;
3247               }
3248             else
3249               error ("CPU you selected does not support x86-64 "
3250                      "instruction set");
3251           }
3252         /* Intel CPUs have always interpreted SSE prefetch instructions as
3253            NOPs; so, we can enable SSE prefetch instructions even when
3254            -mtune (rather than -march) points us to a processor that has them.
3255            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
3256            higher processors.  */
3257         if (TARGET_CMOVE
3258             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
3259           x86_prefetch_sse = true;
3260         break;
3261       }
3262
3263   if (ix86_tune_specified && i == pta_size)
3264     error ("bad value (%s) for %stune=%s %s",
3265            ix86_tune_string, prefix, suffix, sw);
3266
3267   ix86_tune_mask = 1u << ix86_tune;
3268   for (i = 0; i < X86_TUNE_LAST; ++i)
3269     ix86_tune_features[i] = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3270
3271 #ifndef USE_IX86_FRAME_POINTER
3272 #define USE_IX86_FRAME_POINTER 0
3273 #endif
3274
3275 #ifndef USE_X86_64_FRAME_POINTER
3276 #define USE_X86_64_FRAME_POINTER 0
3277 #endif
3278
3279   /* Set the default values for switches whose default depends on TARGET_64BIT
3280      in case they weren't overwritten by command line options.  */
3281   if (TARGET_64BIT)
3282     {
3283       if (optimize > 1 && !global_options_set.x_flag_zee)
3284         flag_zee = 1;
3285       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3286         flag_omit_frame_pointer = !USE_X86_64_FRAME_POINTER;
3287       if (flag_asynchronous_unwind_tables == 2)
3288         flag_asynchronous_unwind_tables = 1;
3289       if (flag_pcc_struct_return == 2)
3290         flag_pcc_struct_return = 0;
3291     }
3292   else
3293     {
3294       if (optimize >= 1 && !global_options_set.x_flag_omit_frame_pointer)
3295         flag_omit_frame_pointer = !(USE_IX86_FRAME_POINTER || optimize_size);
3296       if (flag_asynchronous_unwind_tables == 2)
3297         flag_asynchronous_unwind_tables = !USE_IX86_FRAME_POINTER;
3298       if (flag_pcc_struct_return == 2)
3299         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
3300     }
3301
3302   if (optimize_size)
3303     ix86_cost = &ix86_size_cost;
3304   else
3305     ix86_cost = processor_target_table[ix86_tune].cost;
3306
3307   /* Arrange to set up i386_stack_locals for all functions.  */
3308   init_machine_status = ix86_init_machine_status;
3309
3310   /* Validate -mregparm= value.  */
3311   if (ix86_regparm_string)
3312     {
3313       if (TARGET_64BIT)
3314         warning (0, "%sregparm%s is ignored in 64-bit mode", prefix, suffix);
3315       i = atoi (ix86_regparm_string);
3316       if (i < 0 || i > REGPARM_MAX)
3317         error ("%sregparm=%d%s is not between 0 and %d",
3318                prefix, i, suffix, REGPARM_MAX);
3319       else
3320         ix86_regparm = i;
3321     }
3322   if (TARGET_64BIT)
3323     ix86_regparm = REGPARM_MAX;
3324
3325   /* If the user has provided any of the -malign-* options,
3326      warn and use that value only if -falign-* is not set.
3327      Remove this code in GCC 3.2 or later.  */
3328   if (ix86_align_loops_string)
3329     {
3330       warning (0, "%salign-loops%s is obsolete, use -falign-loops%s",
3331                prefix, suffix, suffix);
3332       if (align_loops == 0)
3333         {
3334           i = atoi (ix86_align_loops_string);
3335           if (i < 0 || i > MAX_CODE_ALIGN)
3336             error ("%salign-loops=%d%s is not between 0 and %d",
3337                    prefix, i, suffix, MAX_CODE_ALIGN);
3338           else
3339             align_loops = 1 << i;
3340         }
3341     }
3342
3343   if (ix86_align_jumps_string)
3344     {
3345       warning (0, "%salign-jumps%s is obsolete, use -falign-jumps%s",
3346                prefix, suffix, suffix);
3347       if (align_jumps == 0)
3348         {
3349           i = atoi (ix86_align_jumps_string);
3350           if (i < 0 || i > MAX_CODE_ALIGN)
3351             error ("%salign-loops=%d%s is not between 0 and %d",
3352                    prefix, i, suffix, MAX_CODE_ALIGN);
3353           else
3354             align_jumps = 1 << i;
3355         }
3356     }
3357
3358   if (ix86_align_funcs_string)
3359     {
3360       warning (0, "%salign-functions%s is obsolete, use -falign-functions%s",
3361                prefix, suffix, suffix);
3362       if (align_functions == 0)
3363         {
3364           i = atoi (ix86_align_funcs_string);
3365           if (i < 0 || i > MAX_CODE_ALIGN)
3366             error ("%salign-loops=%d%s is not between 0 and %d",
3367                    prefix, i, suffix, MAX_CODE_ALIGN);
3368           else
3369             align_functions = 1 << i;
3370         }
3371     }
3372
3373   /* Default align_* from the processor table.  */
3374   if (align_loops == 0)
3375     {
3376       align_loops = processor_target_table[ix86_tune].align_loop;
3377       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
3378     }
3379   if (align_jumps == 0)
3380     {
3381       align_jumps = processor_target_table[ix86_tune].align_jump;
3382       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
3383     }
3384   if (align_functions == 0)
3385     {
3386       align_functions = processor_target_table[ix86_tune].align_func;
3387     }
3388
3389   /* Validate -mbranch-cost= value, or provide default.  */
3390   ix86_branch_cost = ix86_cost->branch_cost;
3391   if (ix86_branch_cost_string)
3392     {
3393       i = atoi (ix86_branch_cost_string);
3394       if (i < 0 || i > 5)
3395         error ("%sbranch-cost=%d%s is not between 0 and 5", prefix, i, suffix);
3396       else
3397         ix86_branch_cost = i;
3398     }
3399   if (ix86_section_threshold_string)
3400     {
3401       i = atoi (ix86_section_threshold_string);
3402       if (i < 0)
3403         error ("%slarge-data-threshold=%d%s is negative", prefix, i, suffix);
3404       else
3405         ix86_section_threshold = i;
3406     }
3407
3408   if (ix86_tls_dialect_string)
3409     {
3410       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
3411         ix86_tls_dialect = TLS_DIALECT_GNU;
3412       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
3413         ix86_tls_dialect = TLS_DIALECT_GNU2;
3414       else
3415         error ("bad value (%s) for %stls-dialect=%s %s",
3416                ix86_tls_dialect_string, prefix, suffix, sw);
3417     }
3418
3419   if (ix87_precision_string)
3420     {
3421       i = atoi (ix87_precision_string);
3422       if (i != 32 && i != 64 && i != 80)
3423         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
3424     }
3425
3426   if (TARGET_64BIT)
3427     {
3428       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
3429
3430       /* Enable by default the SSE and MMX builtins.  Do allow the user to
3431          explicitly disable any of these.  In particular, disabling SSE and
3432          MMX for kernel code is extremely useful.  */
3433       if (!ix86_arch_specified)
3434       ix86_isa_flags
3435         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
3436              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
3437
3438       if (TARGET_RTD)
3439         warning (0, "%srtd%s is ignored in 64bit mode", prefix, suffix);
3440     }
3441   else
3442     {
3443       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
3444
3445       if (!ix86_arch_specified)
3446       ix86_isa_flags
3447         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
3448
3449       /* i386 ABI does not specify red zone.  It still makes sense to use it
3450          when programmer takes care to stack from being destroyed.  */
3451       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
3452         target_flags |= MASK_NO_RED_ZONE;
3453     }
3454
3455   /* Keep nonleaf frame pointers.  */
3456   if (flag_omit_frame_pointer)
3457     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
3458   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
3459     flag_omit_frame_pointer = 1;
3460
3461   /* If we're doing fast math, we don't care about comparison order
3462      wrt NaNs.  This lets us use a shorter comparison sequence.  */
3463   if (flag_finite_math_only)
3464     target_flags &= ~MASK_IEEE_FP;
3465
3466   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
3467      since the insns won't need emulation.  */
3468   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
3469     target_flags &= ~MASK_NO_FANCY_MATH_387;
3470
3471   /* Likewise, if the target doesn't have a 387, or we've specified
3472      software floating point, don't use 387 inline intrinsics.  */
3473   if (!TARGET_80387)
3474     target_flags |= MASK_NO_FANCY_MATH_387;
3475
3476   /* Turn on MMX builtins for -msse.  */
3477   if (TARGET_SSE)
3478     {
3479       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
3480       x86_prefetch_sse = true;
3481     }
3482
3483   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
3484   if (TARGET_SSE4_2 || TARGET_ABM)
3485     ix86_isa_flags |= OPTION_MASK_ISA_POPCNT & ~ix86_isa_flags_explicit;
3486
3487   /* Validate -mpreferred-stack-boundary= value or default it to
3488      PREFERRED_STACK_BOUNDARY_DEFAULT.  */
3489   ix86_preferred_stack_boundary = PREFERRED_STACK_BOUNDARY_DEFAULT;
3490   if (ix86_preferred_stack_boundary_string)
3491     {
3492       i = atoi (ix86_preferred_stack_boundary_string);
3493       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3494         error ("%spreferred-stack-boundary=%d%s is not between %d and 12",
3495                prefix, i, suffix, TARGET_64BIT ? 4 : 2);
3496       else
3497         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
3498     }
3499
3500   /* Set the default value for -mstackrealign.  */
3501   if (ix86_force_align_arg_pointer == -1)
3502     ix86_force_align_arg_pointer = STACK_REALIGN_DEFAULT;
3503
3504   ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
3505
3506   /* Validate -mincoming-stack-boundary= value or default it to
3507      MIN_STACK_BOUNDARY/PREFERRED_STACK_BOUNDARY.  */
3508   ix86_incoming_stack_boundary = ix86_default_incoming_stack_boundary;
3509   if (ix86_incoming_stack_boundary_string)
3510     {
3511       i = atoi (ix86_incoming_stack_boundary_string);
3512       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
3513         error ("-mincoming-stack-boundary=%d is not between %d and 12",
3514                i, TARGET_64BIT ? 4 : 2);
3515       else
3516         {
3517           ix86_user_incoming_stack_boundary = (1 << i) * BITS_PER_UNIT;
3518           ix86_incoming_stack_boundary
3519             = ix86_user_incoming_stack_boundary;
3520         }
3521     }
3522
3523   /* Accept -msseregparm only if at least SSE support is enabled.  */
3524   if (TARGET_SSEREGPARM
3525       && ! TARGET_SSE)
3526     error ("%ssseregparm%s used without SSE enabled", prefix, suffix);
3527
3528   ix86_fpmath = TARGET_FPMATH_DEFAULT;
3529   if (ix86_fpmath_string != 0)
3530     {
3531       if (! strcmp (ix86_fpmath_string, "387"))
3532         ix86_fpmath = FPMATH_387;
3533       else if (! strcmp (ix86_fpmath_string, "sse"))
3534         {
3535           if (!TARGET_SSE)
3536             {
3537               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3538               ix86_fpmath = FPMATH_387;
3539             }
3540           else
3541             ix86_fpmath = FPMATH_SSE;
3542         }
3543       else if (! strcmp (ix86_fpmath_string, "387,sse")
3544                || ! strcmp (ix86_fpmath_string, "387+sse")
3545                || ! strcmp (ix86_fpmath_string, "sse,387")
3546                || ! strcmp (ix86_fpmath_string, "sse+387")
3547                || ! strcmp (ix86_fpmath_string, "both"))
3548         {
3549           if (!TARGET_SSE)
3550             {
3551               warning (0, "SSE instruction set disabled, using 387 arithmetics");
3552               ix86_fpmath = FPMATH_387;
3553             }
3554           else if (!TARGET_80387)
3555             {
3556               warning (0, "387 instruction set disabled, using SSE arithmetics");
3557               ix86_fpmath = FPMATH_SSE;
3558             }
3559           else
3560             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
3561         }
3562       else
3563         error ("bad value (%s) for %sfpmath=%s %s",
3564                ix86_fpmath_string, prefix, suffix, sw);
3565     }
3566
3567   /* If the i387 is disabled, then do not return values in it. */
3568   if (!TARGET_80387)
3569     target_flags &= ~MASK_FLOAT_RETURNS;
3570
3571   /* Use external vectorized library in vectorizing intrinsics.  */
3572   if (ix86_veclibabi_string)
3573     {
3574       if (strcmp (ix86_veclibabi_string, "svml") == 0)
3575         ix86_veclib_handler = ix86_veclibabi_svml;
3576       else if (strcmp (ix86_veclibabi_string, "acml") == 0)
3577         ix86_veclib_handler = ix86_veclibabi_acml;
3578       else
3579         error ("unknown vectorization library ABI type (%s) for "
3580                "%sveclibabi=%s %s", ix86_veclibabi_string,
3581                prefix, suffix, sw);
3582     }
3583
3584   if ((!USE_IX86_FRAME_POINTER
3585        || (x86_accumulate_outgoing_args & ix86_tune_mask))
3586       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3587       && !optimize_size)
3588     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3589
3590   /* ??? Unwind info is not correct around the CFG unless either a frame
3591      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
3592      unwind info generation to be aware of the CFG and propagating states
3593      around edges.  */
3594   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
3595        || flag_exceptions || flag_non_call_exceptions)
3596       && flag_omit_frame_pointer
3597       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3598     {
3599       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3600         warning (0, "unwind tables currently require either a frame pointer "
3601                  "or %saccumulate-outgoing-args%s for correctness",
3602                  prefix, suffix);
3603       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3604     }
3605
3606   /* If stack probes are required, the space used for large function
3607      arguments on the stack must also be probed, so enable
3608      -maccumulate-outgoing-args so this happens in the prologue.  */
3609   if (TARGET_STACK_PROBE
3610       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
3611     {
3612       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
3613         warning (0, "stack probing requires %saccumulate-outgoing-args%s "
3614                  "for correctness", prefix, suffix);
3615       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
3616     }
3617
3618   /* For sane SSE instruction set generation we need fcomi instruction.
3619      It is safe to enable all CMOVE instructions.  */
3620   if (TARGET_SSE)
3621     TARGET_CMOVE = 1;
3622
3623   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
3624   {
3625     char *p;
3626     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
3627     p = strchr (internal_label_prefix, 'X');
3628     internal_label_prefix_len = p - internal_label_prefix;
3629     *p = '\0';
3630   }
3631
3632   /* When scheduling description is not available, disable scheduler pass
3633      so it won't slow down the compilation and make x87 code slower.  */
3634   if (!TARGET_SCHEDULE)
3635     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
3636
3637   maybe_set_param_value (PARAM_SIMULTANEOUS_PREFETCHES,
3638                          ix86_cost->simultaneous_prefetches,
3639                          global_options.x_param_values,
3640                          global_options_set.x_param_values);
3641   maybe_set_param_value (PARAM_L1_CACHE_LINE_SIZE, ix86_cost->prefetch_block,
3642                          global_options.x_param_values,
3643                          global_options_set.x_param_values);
3644   maybe_set_param_value (PARAM_L1_CACHE_SIZE, ix86_cost->l1_cache_size,
3645                          global_options.x_param_values,
3646                          global_options_set.x_param_values);
3647   maybe_set_param_value (PARAM_L2_CACHE_SIZE, ix86_cost->l2_cache_size,
3648                          global_options.x_param_values,
3649                          global_options_set.x_param_values);
3650
3651   /* Enable sw prefetching at -O3 for CPUS that prefetching is helpful.  */
3652   if (flag_prefetch_loop_arrays < 0
3653       && HAVE_prefetch
3654       && optimize >= 3
3655       && software_prefetching_beneficial_p ())
3656     flag_prefetch_loop_arrays = 1;
3657
3658   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
3659      can be optimized to ap = __builtin_next_arg (0).  */
3660   if (!TARGET_64BIT && !flag_split_stack)
3661     targetm.expand_builtin_va_start = NULL;
3662
3663   if (TARGET_64BIT)
3664     {
3665       ix86_gen_leave = gen_leave_rex64;
3666       ix86_gen_add3 = gen_adddi3;
3667       ix86_gen_sub3 = gen_subdi3;
3668       ix86_gen_sub3_carry = gen_subdi3_carry;
3669       ix86_gen_one_cmpl2 = gen_one_cmpldi2;
3670       ix86_gen_monitor = gen_sse3_monitor64;
3671       ix86_gen_andsp = gen_anddi3;
3672       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_di;
3673       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probedi;
3674       ix86_gen_probe_stack_range = gen_probe_stack_rangedi;
3675     }
3676   else
3677     {
3678       ix86_gen_leave = gen_leave;
3679       ix86_gen_add3 = gen_addsi3;
3680       ix86_gen_sub3 = gen_subsi3;
3681       ix86_gen_sub3_carry = gen_subsi3_carry;
3682       ix86_gen_one_cmpl2 = gen_one_cmplsi2;
3683       ix86_gen_monitor = gen_sse3_monitor;
3684       ix86_gen_andsp = gen_andsi3;
3685       ix86_gen_allocate_stack_worker = gen_allocate_stack_worker_probe_si;
3686       ix86_gen_adjust_stack_and_probe = gen_adjust_stack_and_probesi;
3687       ix86_gen_probe_stack_range = gen_probe_stack_rangesi;
3688     }
3689
3690 #ifdef USE_IX86_CLD
3691   /* Use -mcld by default for 32-bit code if configured with --enable-cld.  */
3692   if (!TARGET_64BIT)
3693     target_flags |= MASK_CLD & ~target_flags_explicit;
3694 #endif
3695
3696   if (!TARGET_64BIT && flag_pic)
3697     {
3698       if (flag_fentry > 0)
3699         sorry ("-mfentry isn't supported for 32-bit in combination with -fpic");
3700       flag_fentry = 0;
3701     }
3702   if (flag_fentry < 0)
3703    {
3704 #if defined(PROFILE_BEFORE_PROLOGUE)
3705      flag_fentry = 1;
3706 #else
3707      flag_fentry = 0;
3708 #endif
3709    }
3710
3711   /* Save the initial options in case the user does function specific options */
3712   if (main_args_p)
3713     target_option_default_node = target_option_current_node
3714       = build_target_option_node ();
3715 }
3716
3717 /* Implement the TARGET_OPTION_OVERRIDE hook.  */
3718
3719 static void
3720 ix86_option_override (void)
3721 {
3722   ix86_option_override_internal (true);
3723 }
3724
3725 /* Update register usage after having seen the compiler flags.  */
3726
3727 void
3728 ix86_conditional_register_usage (void)
3729 {
3730   int i;
3731   unsigned int j;
3732
3733   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3734     {
3735       if (fixed_regs[i] > 1)
3736         fixed_regs[i] = (fixed_regs[i] == (TARGET_64BIT ? 3 : 2));
3737       if (call_used_regs[i] > 1)
3738         call_used_regs[i] = (call_used_regs[i] == (TARGET_64BIT ? 3 : 2));
3739     }
3740
3741   /* The PIC register, if it exists, is fixed.  */
3742   j = PIC_OFFSET_TABLE_REGNUM;
3743   if (j != INVALID_REGNUM)
3744     fixed_regs[j] = call_used_regs[j] = 1;
3745
3746   /* The MS_ABI changes the set of call-used registers.  */
3747   if (TARGET_64BIT && ix86_cfun_abi () == MS_ABI)
3748     {
3749       call_used_regs[SI_REG] = 0;
3750       call_used_regs[DI_REG] = 0;
3751       call_used_regs[XMM6_REG] = 0;
3752       call_used_regs[XMM7_REG] = 0;
3753       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3754         call_used_regs[i] = 0;
3755     }
3756
3757   /* The default setting of CLOBBERED_REGS is for 32-bit; add in the
3758      other call-clobbered regs for 64-bit.  */
3759   if (TARGET_64BIT)
3760     {
3761       CLEAR_HARD_REG_SET (reg_class_contents[(int)CLOBBERED_REGS]);
3762
3763       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3764         if (TEST_HARD_REG_BIT (reg_class_contents[(int)GENERAL_REGS], i)
3765             && call_used_regs[i])
3766           SET_HARD_REG_BIT (reg_class_contents[(int)CLOBBERED_REGS], i);
3767     }
3768
3769   /* If MMX is disabled, squash the registers.  */
3770   if (! TARGET_MMX)
3771     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3772       if (TEST_HARD_REG_BIT (reg_class_contents[(int)MMX_REGS], i))
3773         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3774
3775   /* If SSE is disabled, squash the registers.  */
3776   if (! TARGET_SSE)
3777     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3778       if (TEST_HARD_REG_BIT (reg_class_contents[(int)SSE_REGS], i))
3779         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3780
3781   /* If the FPU is disabled, squash the registers.  */
3782   if (! (TARGET_80387 || TARGET_FLOAT_RETURNS_IN_80387))
3783     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3784       if (TEST_HARD_REG_BIT (reg_class_contents[(int)FLOAT_REGS], i))
3785         fixed_regs[i] = call_used_regs[i] = 1, reg_names[i] = "";
3786
3787   /* If 32-bit, squash the 64-bit registers.  */
3788   if (! TARGET_64BIT)
3789     {
3790       for (i = FIRST_REX_INT_REG; i <= LAST_REX_INT_REG; i++)
3791         reg_names[i] = "";
3792       for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
3793         reg_names[i] = "";
3794     }
3795 }
3796
3797 \f
3798 /* Save the current options */
3799
3800 static void
3801 ix86_function_specific_save (struct cl_target_option *ptr)
3802 {
3803   ptr->arch = ix86_arch;
3804   ptr->schedule = ix86_schedule;
3805   ptr->tune = ix86_tune;
3806   ptr->fpmath = ix86_fpmath;
3807   ptr->branch_cost = ix86_branch_cost;
3808   ptr->tune_defaulted = ix86_tune_defaulted;
3809   ptr->arch_specified = ix86_arch_specified;
3810   ptr->ix86_isa_flags_explicit = ix86_isa_flags_explicit;
3811   ptr->ix86_target_flags_explicit = target_flags_explicit;
3812
3813   /* The fields are char but the variables are not; make sure the
3814      values fit in the fields.  */
3815   gcc_assert (ptr->arch == ix86_arch);
3816   gcc_assert (ptr->schedule == ix86_schedule);
3817   gcc_assert (ptr->tune == ix86_tune);
3818   gcc_assert (ptr->fpmath == ix86_fpmath);
3819   gcc_assert (ptr->branch_cost == ix86_branch_cost);
3820 }
3821
3822 /* Restore the current options */
3823
3824 static void
3825 ix86_function_specific_restore (struct cl_target_option *ptr)
3826 {
3827   enum processor_type old_tune = ix86_tune;
3828   enum processor_type old_arch = ix86_arch;
3829   unsigned int ix86_arch_mask, ix86_tune_mask;
3830   int i;
3831
3832   ix86_arch = (enum processor_type) ptr->arch;
3833   ix86_schedule = (enum attr_cpu) ptr->schedule;
3834   ix86_tune = (enum processor_type) ptr->tune;
3835   ix86_fpmath = (enum fpmath_unit) ptr->fpmath;
3836   ix86_branch_cost = ptr->branch_cost;
3837   ix86_tune_defaulted = ptr->tune_defaulted;
3838   ix86_arch_specified = ptr->arch_specified;
3839   ix86_isa_flags_explicit = ptr->ix86_isa_flags_explicit;
3840   target_flags_explicit = ptr->ix86_target_flags_explicit;
3841
3842   /* Recreate the arch feature tests if the arch changed */
3843   if (old_arch != ix86_arch)
3844     {
3845       ix86_arch_mask = 1u << ix86_arch;
3846       for (i = 0; i < X86_ARCH_LAST; ++i)
3847         ix86_arch_features[i]
3848           = !!(initial_ix86_arch_features[i] & ix86_arch_mask);
3849     }
3850
3851   /* Recreate the tune optimization tests */
3852   if (old_tune != ix86_tune)
3853     {
3854       ix86_tune_mask = 1u << ix86_tune;
3855       for (i = 0; i < X86_TUNE_LAST; ++i)
3856         ix86_tune_features[i]
3857           = !!(initial_ix86_tune_features[i] & ix86_tune_mask);
3858     }
3859 }
3860
3861 /* Print the current options */
3862
3863 static void
3864 ix86_function_specific_print (FILE *file, int indent,
3865                               struct cl_target_option *ptr)
3866 {
3867   char *target_string
3868     = ix86_target_string (ptr->x_ix86_isa_flags, ptr->x_target_flags,
3869                           NULL, NULL, NULL, false);
3870
3871   fprintf (file, "%*sarch = %d (%s)\n",
3872            indent, "",
3873            ptr->arch,
3874            ((ptr->arch < TARGET_CPU_DEFAULT_max)
3875             ? cpu_names[ptr->arch]
3876             : "<unknown>"));
3877
3878   fprintf (file, "%*stune = %d (%s)\n",
3879            indent, "",
3880            ptr->tune,
3881            ((ptr->tune < TARGET_CPU_DEFAULT_max)
3882             ? cpu_names[ptr->tune]
3883             : "<unknown>"));
3884
3885   fprintf (file, "%*sfpmath = %d%s%s\n", indent, "", ptr->fpmath,
3886            (ptr->fpmath & FPMATH_387) ? ", 387" : "",
3887            (ptr->fpmath & FPMATH_SSE) ? ", sse" : "");
3888   fprintf (file, "%*sbranch_cost = %d\n", indent, "", ptr->branch_cost);
3889
3890   if (target_string)
3891     {
3892       fprintf (file, "%*s%s\n", indent, "", target_string);
3893       free (target_string);
3894     }
3895 }
3896
3897 \f
3898 /* Inner function to process the attribute((target(...))), take an argument and
3899    set the current options from the argument. If we have a list, recursively go
3900    over the list.  */
3901
3902 static bool
3903 ix86_valid_target_attribute_inner_p (tree args, char *p_strings[])
3904 {
3905   char *next_optstr;
3906   bool ret = true;
3907
3908 #define IX86_ATTR_ISA(S,O)   { S, sizeof (S)-1, ix86_opt_isa, O, 0 }
3909 #define IX86_ATTR_STR(S,O)   { S, sizeof (S)-1, ix86_opt_str, O, 0 }
3910 #define IX86_ATTR_YES(S,O,M) { S, sizeof (S)-1, ix86_opt_yes, O, M }
3911 #define IX86_ATTR_NO(S,O,M)  { S, sizeof (S)-1, ix86_opt_no,  O, M }
3912
3913   enum ix86_opt_type
3914   {
3915     ix86_opt_unknown,
3916     ix86_opt_yes,
3917     ix86_opt_no,
3918     ix86_opt_str,
3919     ix86_opt_isa
3920   };
3921
3922   static const struct
3923   {
3924     const char *string;
3925     size_t len;
3926     enum ix86_opt_type type;
3927     int opt;
3928     int mask;
3929   } attrs[] = {
3930     /* isa options */
3931     IX86_ATTR_ISA ("3dnow",     OPT_m3dnow),
3932     IX86_ATTR_ISA ("abm",       OPT_mabm),
3933     IX86_ATTR_ISA ("aes",       OPT_maes),
3934     IX86_ATTR_ISA ("avx",       OPT_mavx),
3935     IX86_ATTR_ISA ("mmx",       OPT_mmmx),
3936     IX86_ATTR_ISA ("pclmul",    OPT_mpclmul),
3937     IX86_ATTR_ISA ("popcnt",    OPT_mpopcnt),
3938     IX86_ATTR_ISA ("sse",       OPT_msse),
3939     IX86_ATTR_ISA ("sse2",      OPT_msse2),
3940     IX86_ATTR_ISA ("sse3",      OPT_msse3),
3941     IX86_ATTR_ISA ("sse4",      OPT_msse4),
3942     IX86_ATTR_ISA ("sse4.1",    OPT_msse4_1),
3943     IX86_ATTR_ISA ("sse4.2",    OPT_msse4_2),
3944     IX86_ATTR_ISA ("sse4a",     OPT_msse4a),
3945     IX86_ATTR_ISA ("ssse3",     OPT_mssse3),
3946     IX86_ATTR_ISA ("fma4",      OPT_mfma4),
3947     IX86_ATTR_ISA ("xop",       OPT_mxop),
3948     IX86_ATTR_ISA ("lwp",       OPT_mlwp),
3949     IX86_ATTR_ISA ("fsgsbase",  OPT_mfsgsbase),
3950     IX86_ATTR_ISA ("rdrnd",     OPT_mrdrnd),
3951     IX86_ATTR_ISA ("f16c",      OPT_mf16c),
3952
3953     /* string options */
3954     IX86_ATTR_STR ("arch=",     IX86_FUNCTION_SPECIFIC_ARCH),
3955     IX86_ATTR_STR ("fpmath=",   IX86_FUNCTION_SPECIFIC_FPMATH),
3956     IX86_ATTR_STR ("tune=",     IX86_FUNCTION_SPECIFIC_TUNE),
3957
3958     /* flag options */
3959     IX86_ATTR_YES ("cld",
3960                    OPT_mcld,
3961                    MASK_CLD),
3962
3963     IX86_ATTR_NO ("fancy-math-387",
3964                   OPT_mfancy_math_387,
3965                   MASK_NO_FANCY_MATH_387),
3966
3967     IX86_ATTR_YES ("ieee-fp",
3968                    OPT_mieee_fp,
3969                    MASK_IEEE_FP),
3970
3971     IX86_ATTR_YES ("inline-all-stringops",
3972                    OPT_minline_all_stringops,
3973                    MASK_INLINE_ALL_STRINGOPS),
3974
3975     IX86_ATTR_YES ("inline-stringops-dynamically",
3976                    OPT_minline_stringops_dynamically,
3977                    MASK_INLINE_STRINGOPS_DYNAMICALLY),
3978
3979     IX86_ATTR_NO ("align-stringops",
3980                   OPT_mno_align_stringops,
3981                   MASK_NO_ALIGN_STRINGOPS),
3982
3983     IX86_ATTR_YES ("recip",
3984                    OPT_mrecip,
3985                    MASK_RECIP),
3986
3987   };
3988
3989   /* If this is a list, recurse to get the options.  */
3990   if (TREE_CODE (args) == TREE_LIST)
3991     {
3992       bool ret = true;
3993
3994       for (; args; args = TREE_CHAIN (args))
3995         if (TREE_VALUE (args)
3996             && !ix86_valid_target_attribute_inner_p (TREE_VALUE (args), p_strings))
3997           ret = false;
3998
3999       return ret;
4000     }
4001
4002   else if (TREE_CODE (args) != STRING_CST)
4003     gcc_unreachable ();
4004
4005   /* Handle multiple arguments separated by commas.  */
4006   next_optstr = ASTRDUP (TREE_STRING_POINTER (args));
4007
4008   while (next_optstr && *next_optstr != '\0')
4009     {
4010       char *p = next_optstr;
4011       char *orig_p = p;
4012       char *comma = strchr (next_optstr, ',');
4013       const char *opt_string;
4014       size_t len, opt_len;
4015       int opt;
4016       bool opt_set_p;
4017       char ch;
4018       unsigned i;
4019       enum ix86_opt_type type = ix86_opt_unknown;
4020       int mask = 0;
4021
4022       if (comma)
4023         {
4024           *comma = '\0';
4025           len = comma - next_optstr;
4026           next_optstr = comma + 1;
4027         }
4028       else
4029         {
4030           len = strlen (p);
4031           next_optstr = NULL;
4032         }
4033
4034       /* Recognize no-xxx.  */
4035       if (len > 3 && p[0] == 'n' && p[1] == 'o' && p[2] == '-')
4036         {
4037           opt_set_p = false;
4038           p += 3;
4039           len -= 3;
4040         }
4041       else
4042         opt_set_p = true;
4043
4044       /* Find the option.  */
4045       ch = *p;
4046       opt = N_OPTS;
4047       for (i = 0; i < ARRAY_SIZE (attrs); i++)
4048         {
4049           type = attrs[i].type;
4050           opt_len = attrs[i].len;
4051           if (ch == attrs[i].string[0]
4052               && ((type != ix86_opt_str) ? len == opt_len : len > opt_len)
4053               && memcmp (p, attrs[i].string, opt_len) == 0)
4054             {
4055               opt = attrs[i].opt;
4056               mask = attrs[i].mask;
4057               opt_string = attrs[i].string;
4058               break;
4059             }
4060         }
4061
4062       /* Process the option.  */
4063       if (opt == N_OPTS)
4064         {
4065           error ("attribute(target(\"%s\")) is unknown", orig_p);
4066           ret = false;
4067         }
4068
4069       else if (type == ix86_opt_isa)
4070         ix86_handle_option (opt, p, opt_set_p);
4071
4072       else if (type == ix86_opt_yes || type == ix86_opt_no)
4073         {
4074           if (type == ix86_opt_no)
4075             opt_set_p = !opt_set_p;
4076
4077           if (opt_set_p)
4078             target_flags |= mask;
4079           else
4080             target_flags &= ~mask;
4081         }
4082
4083       else if (type == ix86_opt_str)
4084         {
4085           if (p_strings[opt])
4086             {
4087               error ("option(\"%s\") was already specified", opt_string);
4088               ret = false;
4089             }
4090           else
4091             p_strings[opt] = xstrdup (p + opt_len);
4092         }
4093
4094       else
4095         gcc_unreachable ();
4096     }
4097
4098   return ret;
4099 }
4100
4101 /* Return a TARGET_OPTION_NODE tree of the target options listed or NULL.  */
4102
4103 tree
4104 ix86_valid_target_attribute_tree (tree args)
4105 {
4106   const char *orig_arch_string = ix86_arch_string;
4107   const char *orig_tune_string = ix86_tune_string;
4108   const char *orig_fpmath_string = ix86_fpmath_string;
4109   int orig_tune_defaulted = ix86_tune_defaulted;
4110   int orig_arch_specified = ix86_arch_specified;
4111   char *option_strings[IX86_FUNCTION_SPECIFIC_MAX] = { NULL, NULL, NULL };
4112   tree t = NULL_TREE;
4113   int i;
4114   struct cl_target_option *def
4115     = TREE_TARGET_OPTION (target_option_default_node);
4116
4117   /* Process each of the options on the chain.  */
4118   if (! ix86_valid_target_attribute_inner_p (args, option_strings))
4119     return NULL_TREE;
4120
4121   /* If the changed options are different from the default, rerun
4122      ix86_option_override_internal, and then save the options away.
4123      The string options are are attribute options, and will be undone
4124      when we copy the save structure.  */
4125   if (ix86_isa_flags != def->x_ix86_isa_flags
4126       || target_flags != def->x_target_flags
4127       || option_strings[IX86_FUNCTION_SPECIFIC_ARCH]
4128       || option_strings[IX86_FUNCTION_SPECIFIC_TUNE]
4129       || option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4130     {
4131       /* If we are using the default tune= or arch=, undo the string assigned,
4132          and use the default.  */
4133       if (option_strings[IX86_FUNCTION_SPECIFIC_ARCH])
4134         ix86_arch_string = option_strings[IX86_FUNCTION_SPECIFIC_ARCH];
4135       else if (!orig_arch_specified)
4136         ix86_arch_string = NULL;
4137
4138       if (option_strings[IX86_FUNCTION_SPECIFIC_TUNE])
4139         ix86_tune_string = option_strings[IX86_FUNCTION_SPECIFIC_TUNE];
4140       else if (orig_tune_defaulted)
4141         ix86_tune_string = NULL;
4142
4143       /* If fpmath= is not set, and we now have sse2 on 32-bit, use it.  */
4144       if (option_strings[IX86_FUNCTION_SPECIFIC_FPMATH])
4145         ix86_fpmath_string = option_strings[IX86_FUNCTION_SPECIFIC_FPMATH];
4146       else if (!TARGET_64BIT && TARGET_SSE)
4147         ix86_fpmath_string = "sse,387";
4148
4149       /* Do any overrides, such as arch=xxx, or tune=xxx support.  */
4150       ix86_option_override_internal (false);
4151
4152       /* Add any builtin functions with the new isa if any.  */
4153       ix86_add_new_builtins (ix86_isa_flags);
4154
4155       /* Save the current options unless we are validating options for
4156          #pragma.  */
4157       t = build_target_option_node ();
4158
4159       ix86_arch_string = orig_arch_string;
4160       ix86_tune_string = orig_tune_string;
4161       ix86_fpmath_string = orig_fpmath_string;
4162
4163       /* Free up memory allocated to hold the strings */
4164       for (i = 0; i < IX86_FUNCTION_SPECIFIC_MAX; i++)
4165         if (option_strings[i])
4166           free (option_strings[i]);
4167     }
4168
4169   return t;
4170 }
4171
4172 /* Hook to validate attribute((target("string"))).  */
4173
4174 static bool
4175 ix86_valid_target_attribute_p (tree fndecl,
4176                                tree ARG_UNUSED (name),
4177                                tree args,
4178                                int ARG_UNUSED (flags))
4179 {
4180   struct cl_target_option cur_target;
4181   bool ret = true;
4182   tree old_optimize = build_optimization_node ();
4183   tree new_target, new_optimize;
4184   tree func_optimize = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl);
4185
4186   /* If the function changed the optimization levels as well as setting target
4187      options, start with the optimizations specified.  */
4188   if (func_optimize && func_optimize != old_optimize)
4189     cl_optimization_restore (&global_options,
4190                              TREE_OPTIMIZATION (func_optimize));
4191
4192   /* The target attributes may also change some optimization flags, so update
4193      the optimization options if necessary.  */
4194   cl_target_option_save (&cur_target, &global_options);
4195   new_target = ix86_valid_target_attribute_tree (args);
4196   new_optimize = build_optimization_node ();
4197
4198   if (!new_target)
4199     ret = false;
4200
4201   else if (fndecl)
4202     {
4203       DECL_FUNCTION_SPECIFIC_TARGET (fndecl) = new_target;
4204
4205       if (old_optimize != new_optimize)
4206         DECL_FUNCTION_SPECIFIC_OPTIMIZATION (fndecl) = new_optimize;
4207     }
4208
4209   cl_target_option_restore (&global_options, &cur_target);
4210
4211   if (old_optimize != new_optimize)
4212     cl_optimization_restore (&global_options,
4213                              TREE_OPTIMIZATION (old_optimize));
4214
4215   return ret;
4216 }
4217
4218 \f
4219 /* Hook to determine if one function can safely inline another.  */
4220
4221 static bool
4222 ix86_can_inline_p (tree caller, tree callee)
4223 {
4224   bool ret = false;
4225   tree caller_tree = DECL_FUNCTION_SPECIFIC_TARGET (caller);
4226   tree callee_tree = DECL_FUNCTION_SPECIFIC_TARGET (callee);
4227
4228   /* If callee has no option attributes, then it is ok to inline.  */
4229   if (!callee_tree)
4230     ret = true;
4231
4232   /* If caller has no option attributes, but callee does then it is not ok to
4233      inline.  */
4234   else if (!caller_tree)
4235     ret = false;
4236
4237   else
4238     {
4239       struct cl_target_option *caller_opts = TREE_TARGET_OPTION (caller_tree);
4240       struct cl_target_option *callee_opts = TREE_TARGET_OPTION (callee_tree);
4241
4242       /* Callee's isa options should a subset of the caller's, i.e. a SSE4 function
4243          can inline a SSE2 function but a SSE2 function can't inline a SSE4
4244          function.  */
4245       if ((caller_opts->x_ix86_isa_flags & callee_opts->x_ix86_isa_flags)
4246           != callee_opts->x_ix86_isa_flags)
4247         ret = false;
4248
4249       /* See if we have the same non-isa options.  */
4250       else if (caller_opts->x_target_flags != callee_opts->x_target_flags)
4251         ret = false;
4252
4253       /* See if arch, tune, etc. are the same.  */
4254       else if (caller_opts->arch != callee_opts->arch)
4255         ret = false;
4256
4257       else if (caller_opts->tune != callee_opts->tune)
4258         ret = false;
4259
4260       else if (caller_opts->fpmath != callee_opts->fpmath)
4261         ret = false;
4262
4263       else if (caller_opts->branch_cost != callee_opts->branch_cost)
4264         ret = false;
4265
4266       else
4267         ret = true;
4268     }
4269
4270   return ret;
4271 }
4272
4273 \f
4274 /* Remember the last target of ix86_set_current_function.  */
4275 static GTY(()) tree ix86_previous_fndecl;
4276
4277 /* Establish appropriate back-end context for processing the function
4278    FNDECL.  The argument might be NULL to indicate processing at top
4279    level, outside of any function scope.  */
4280 static void
4281 ix86_set_current_function (tree fndecl)
4282 {
4283   /* Only change the context if the function changes.  This hook is called
4284      several times in the course of compiling a function, and we don't want to
4285      slow things down too much or call target_reinit when it isn't safe.  */
4286   if (fndecl && fndecl != ix86_previous_fndecl)
4287     {
4288       tree old_tree = (ix86_previous_fndecl
4289                        ? DECL_FUNCTION_SPECIFIC_TARGET (ix86_previous_fndecl)
4290                        : NULL_TREE);
4291
4292       tree new_tree = (fndecl
4293                        ? DECL_FUNCTION_SPECIFIC_TARGET (fndecl)
4294                        : NULL_TREE);
4295
4296       ix86_previous_fndecl = fndecl;
4297       if (old_tree == new_tree)
4298         ;
4299
4300       else if (new_tree)
4301         {
4302           cl_target_option_restore (&global_options,
4303                                     TREE_TARGET_OPTION (new_tree));
4304           target_reinit ();
4305         }
4306
4307       else if (old_tree)
4308         {
4309           struct cl_target_option *def
4310             = TREE_TARGET_OPTION (target_option_current_node);
4311
4312           cl_target_option_restore (&global_options, def);
4313           target_reinit ();
4314         }
4315     }
4316 }
4317
4318 \f
4319 /* Return true if this goes in large data/bss.  */
4320
4321 static bool
4322 ix86_in_large_data_p (tree exp)
4323 {
4324   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
4325     return false;
4326
4327   /* Functions are never large data.  */
4328   if (TREE_CODE (exp) == FUNCTION_DECL)
4329     return false;
4330
4331   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
4332     {
4333       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
4334       if (strcmp (section, ".ldata") == 0
4335           || strcmp (section, ".lbss") == 0)
4336         return true;
4337       return false;
4338     }
4339   else
4340     {
4341       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
4342
4343       /* If this is an incomplete type with size 0, then we can't put it
4344          in data because it might be too big when completed.  */
4345       if (!size || size > ix86_section_threshold)
4346         return true;
4347     }
4348
4349   return false;
4350 }
4351
4352 /* Switch to the appropriate section for output of DECL.
4353    DECL is either a `VAR_DECL' node or a constant of some sort.
4354    RELOC indicates whether forming the initial value of DECL requires
4355    link-time relocations.  */
4356
4357 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
4358         ATTRIBUTE_UNUSED;
4359
4360 static section *
4361 x86_64_elf_select_section (tree decl, int reloc,
4362                            unsigned HOST_WIDE_INT align)
4363 {
4364   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4365       && ix86_in_large_data_p (decl))
4366     {
4367       const char *sname = NULL;
4368       unsigned int flags = SECTION_WRITE;
4369       switch (categorize_decl_for_section (decl, reloc))
4370         {
4371         case SECCAT_DATA:
4372           sname = ".ldata";
4373           break;
4374         case SECCAT_DATA_REL:
4375           sname = ".ldata.rel";
4376           break;
4377         case SECCAT_DATA_REL_LOCAL:
4378           sname = ".ldata.rel.local";
4379           break;
4380         case SECCAT_DATA_REL_RO:
4381           sname = ".ldata.rel.ro";
4382           break;
4383         case SECCAT_DATA_REL_RO_LOCAL:
4384           sname = ".ldata.rel.ro.local";
4385           break;
4386         case SECCAT_BSS:
4387           sname = ".lbss";
4388           flags |= SECTION_BSS;
4389           break;
4390         case SECCAT_RODATA:
4391         case SECCAT_RODATA_MERGE_STR:
4392         case SECCAT_RODATA_MERGE_STR_INIT:
4393         case SECCAT_RODATA_MERGE_CONST:
4394           sname = ".lrodata";
4395           flags = 0;
4396           break;
4397         case SECCAT_SRODATA:
4398         case SECCAT_SDATA:
4399         case SECCAT_SBSS:
4400           gcc_unreachable ();
4401         case SECCAT_TEXT:
4402         case SECCAT_TDATA:
4403         case SECCAT_TBSS:
4404           /* We don't split these for medium model.  Place them into
4405              default sections and hope for best.  */
4406           break;
4407         }
4408       if (sname)
4409         {
4410           /* We might get called with string constants, but get_named_section
4411              doesn't like them as they are not DECLs.  Also, we need to set
4412              flags in that case.  */
4413           if (!DECL_P (decl))
4414             return get_section (sname, flags, NULL);
4415           return get_named_section (decl, sname, reloc);
4416         }
4417     }
4418   return default_elf_select_section (decl, reloc, align);
4419 }
4420
4421 /* Build up a unique section name, expressed as a
4422    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
4423    RELOC indicates whether the initial value of EXP requires
4424    link-time relocations.  */
4425
4426 static void ATTRIBUTE_UNUSED
4427 x86_64_elf_unique_section (tree decl, int reloc)
4428 {
4429   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4430       && ix86_in_large_data_p (decl))
4431     {
4432       const char *prefix = NULL;
4433       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
4434       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
4435
4436       switch (categorize_decl_for_section (decl, reloc))
4437         {
4438         case SECCAT_DATA:
4439         case SECCAT_DATA_REL:
4440         case SECCAT_DATA_REL_LOCAL:
4441         case SECCAT_DATA_REL_RO:
4442         case SECCAT_DATA_REL_RO_LOCAL:
4443           prefix = one_only ? ".ld" : ".ldata";
4444           break;
4445         case SECCAT_BSS:
4446           prefix = one_only ? ".lb" : ".lbss";
4447           break;
4448         case SECCAT_RODATA:
4449         case SECCAT_RODATA_MERGE_STR:
4450         case SECCAT_RODATA_MERGE_STR_INIT:
4451         case SECCAT_RODATA_MERGE_CONST:
4452           prefix = one_only ? ".lr" : ".lrodata";
4453           break;
4454         case SECCAT_SRODATA:
4455         case SECCAT_SDATA:
4456         case SECCAT_SBSS:
4457           gcc_unreachable ();
4458         case SECCAT_TEXT:
4459         case SECCAT_TDATA:
4460         case SECCAT_TBSS:
4461           /* We don't split these for medium model.  Place them into
4462              default sections and hope for best.  */
4463           break;
4464         }
4465       if (prefix)
4466         {
4467           const char *name, *linkonce;
4468           char *string;
4469
4470           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4471           name = targetm.strip_name_encoding (name);
4472
4473           /* If we're using one_only, then there needs to be a .gnu.linkonce
4474              prefix to the section name.  */
4475           linkonce = one_only ? ".gnu.linkonce" : "";
4476
4477           string = ACONCAT ((linkonce, prefix, ".", name, NULL));
4478
4479           DECL_SECTION_NAME (decl) = build_string (strlen (string), string);
4480           return;
4481         }
4482     }
4483   default_unique_section (decl, reloc);
4484 }
4485
4486 #ifdef COMMON_ASM_OP
4487 /* This says how to output assembler code to declare an
4488    uninitialized external linkage data object.
4489
4490    For medium model x86-64 we need to use .largecomm opcode for
4491    large objects.  */
4492 void
4493 x86_elf_aligned_common (FILE *file,
4494                         const char *name, unsigned HOST_WIDE_INT size,
4495                         int align)
4496 {
4497   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4498       && size > (unsigned int)ix86_section_threshold)
4499     fputs (".largecomm\t", file);
4500   else
4501     fputs (COMMON_ASM_OP, file);
4502   assemble_name (file, name);
4503   fprintf (file, "," HOST_WIDE_INT_PRINT_UNSIGNED ",%u\n",
4504            size, align / BITS_PER_UNIT);
4505 }
4506 #endif
4507
4508 /* Utility function for targets to use in implementing
4509    ASM_OUTPUT_ALIGNED_BSS.  */
4510
4511 void
4512 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
4513                         const char *name, unsigned HOST_WIDE_INT size,
4514                         int align)
4515 {
4516   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
4517       && size > (unsigned int)ix86_section_threshold)
4518     switch_to_section (get_named_section (decl, ".lbss", 0));
4519   else
4520     switch_to_section (bss_section);
4521   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
4522 #ifdef ASM_DECLARE_OBJECT_NAME
4523   last_assemble_variable_decl = decl;
4524   ASM_DECLARE_OBJECT_NAME (file, name, decl);
4525 #else
4526   /* Standard thing is just output label for the object.  */
4527   ASM_OUTPUT_LABEL (file, name);
4528 #endif /* ASM_DECLARE_OBJECT_NAME */
4529   ASM_OUTPUT_SKIP (file, size ? size : 1);
4530 }
4531 \f
4532 static void
4533 ix86_option_optimization (int level, int size ATTRIBUTE_UNUSED)
4534 {
4535   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
4536      make the problem with not enough registers even worse.  */
4537 #ifdef INSN_SCHEDULING
4538   if (level > 1)
4539     flag_schedule_insns = 0;
4540 #endif
4541
4542 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
4543   SUBTARGET_OPTIMIZATION_OPTIONS;
4544 #endif
4545 }
4546
4547 /* Implement TARGET_OPTION_INIT_STRUCT.  */
4548
4549 static void
4550 ix86_option_init_struct (struct gcc_options *opts)
4551 {
4552   if (TARGET_MACHO)
4553     /* The Darwin libraries never set errno, so we might as well
4554        avoid calling them when that's the only reason we would.  */
4555     opts->x_flag_errno_math = 0;
4556
4557   opts->x_flag_pcc_struct_return = 2;
4558   opts->x_flag_asynchronous_unwind_tables = 2;
4559   opts->x_flag_vect_cost_model = 1;
4560 }
4561
4562 /* Decide whether we must probe the stack before any space allocation
4563    on this target.  It's essentially TARGET_STACK_PROBE except when
4564    -fstack-check causes the stack to be already probed differently.  */
4565
4566 bool
4567 ix86_target_stack_probe (void)
4568 {
4569   /* Do not probe the stack twice if static stack checking is enabled.  */
4570   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
4571     return false;
4572
4573   return TARGET_STACK_PROBE;
4574 }
4575 \f
4576 /* Decide whether we can make a sibling call to a function.  DECL is the
4577    declaration of the function being targeted by the call and EXP is the
4578    CALL_EXPR representing the call.  */
4579
4580 static bool
4581 ix86_function_ok_for_sibcall (tree decl, tree exp)
4582 {
4583   tree type, decl_or_type;
4584   rtx a, b;
4585
4586   /* If we are generating position-independent code, we cannot sibcall
4587      optimize any indirect call, or a direct call to a global function,
4588      as the PLT requires %ebx be live.  */
4589   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
4590     return false;
4591
4592   /* If we need to align the outgoing stack, then sibcalling would
4593      unalign the stack, which may break the called function.  */
4594   if (ix86_minimum_incoming_stack_boundary (true)
4595       < PREFERRED_STACK_BOUNDARY)
4596     return false;
4597
4598   if (decl)
4599     {
4600       decl_or_type = decl;
4601       type = TREE_TYPE (decl);
4602     }
4603   else
4604     {
4605       /* We're looking at the CALL_EXPR, we need the type of the function.  */
4606       type = CALL_EXPR_FN (exp);                /* pointer expression */
4607       type = TREE_TYPE (type);                  /* pointer type */
4608       type = TREE_TYPE (type);                  /* function type */
4609       decl_or_type = type;
4610     }
4611
4612   /* Check that the return value locations are the same.  Like
4613      if we are returning floats on the 80387 register stack, we cannot
4614      make a sibcall from a function that doesn't return a float to a
4615      function that does or, conversely, from a function that does return
4616      a float to a function that doesn't; the necessary stack adjustment
4617      would not be executed.  This is also the place we notice
4618      differences in the return value ABI.  Note that it is ok for one
4619      of the functions to have void return type as long as the return
4620      value of the other is passed in a register.  */
4621   a = ix86_function_value (TREE_TYPE (exp), decl_or_type, false);
4622   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
4623                            cfun->decl, false);
4624   if (STACK_REG_P (a) || STACK_REG_P (b))
4625     {
4626       if (!rtx_equal_p (a, b))
4627         return false;
4628     }
4629   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
4630     ;
4631   else if (!rtx_equal_p (a, b))
4632     return false;
4633
4634   if (TARGET_64BIT)
4635     {
4636       /* The SYSV ABI has more call-clobbered registers;
4637          disallow sibcalls from MS to SYSV.  */
4638       if (cfun->machine->call_abi == MS_ABI
4639           && ix86_function_type_abi (type) == SYSV_ABI)
4640         return false;
4641     }
4642   else
4643     {
4644       /* If this call is indirect, we'll need to be able to use a
4645          call-clobbered register for the address of the target function.
4646          Make sure that all such registers are not used for passing
4647          parameters.  Note that DLLIMPORT functions are indirect.  */
4648       if (!decl
4649           || (TARGET_DLLIMPORT_DECL_ATTRIBUTES && DECL_DLLIMPORT_P (decl)))
4650         {
4651           if (ix86_function_regparm (type, NULL) >= 3)
4652             {
4653               /* ??? Need to count the actual number of registers to be used,
4654                  not the possible number of registers.  Fix later.  */
4655               return false;
4656             }
4657         }
4658     }
4659
4660   /* Otherwise okay.  That also includes certain types of indirect calls.  */
4661   return true;
4662 }
4663
4664 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "thiscall",
4665    and "sseregparm" calling convention attributes;
4666    arguments as in struct attribute_spec.handler.  */
4667
4668 static tree
4669 ix86_handle_cconv_attribute (tree *node, tree name,
4670                                    tree args,
4671                                    int flags ATTRIBUTE_UNUSED,
4672                                    bool *no_add_attrs)
4673 {
4674   if (TREE_CODE (*node) != FUNCTION_TYPE
4675       && TREE_CODE (*node) != METHOD_TYPE
4676       && TREE_CODE (*node) != FIELD_DECL
4677       && TREE_CODE (*node) != TYPE_DECL)
4678     {
4679       warning (OPT_Wattributes, "%qE attribute only applies to functions",
4680                name);
4681       *no_add_attrs = true;
4682       return NULL_TREE;
4683     }
4684
4685   /* Can combine regparm with all attributes but fastcall.  */
4686   if (is_attribute_p ("regparm", name))
4687     {
4688       tree cst;
4689
4690       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4691         {
4692           error ("fastcall and regparm attributes are not compatible");
4693         }
4694
4695       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4696         {
4697           error ("regparam and thiscall attributes are not compatible");
4698         }
4699
4700       cst = TREE_VALUE (args);
4701       if (TREE_CODE (cst) != INTEGER_CST)
4702         {
4703           warning (OPT_Wattributes,
4704                    "%qE attribute requires an integer constant argument",
4705                    name);
4706           *no_add_attrs = true;
4707         }
4708       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
4709         {
4710           warning (OPT_Wattributes, "argument to %qE attribute larger than %d",
4711                    name, REGPARM_MAX);
4712           *no_add_attrs = true;
4713         }
4714
4715       return NULL_TREE;
4716     }
4717
4718   if (TARGET_64BIT)
4719     {
4720       /* Do not warn when emulating the MS ABI.  */
4721       if ((TREE_CODE (*node) != FUNCTION_TYPE
4722            && TREE_CODE (*node) != METHOD_TYPE)
4723           || ix86_function_type_abi (*node) != MS_ABI)
4724         warning (OPT_Wattributes, "%qE attribute ignored",
4725                  name);
4726       *no_add_attrs = true;
4727       return NULL_TREE;
4728     }
4729
4730   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
4731   if (is_attribute_p ("fastcall", name))
4732     {
4733       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4734         {
4735           error ("fastcall and cdecl attributes are not compatible");
4736         }
4737       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4738         {
4739           error ("fastcall and stdcall attributes are not compatible");
4740         }
4741       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
4742         {
4743           error ("fastcall and regparm attributes are not compatible");
4744         }
4745       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4746         {
4747           error ("fastcall and thiscall attributes are not compatible");
4748         }
4749     }
4750
4751   /* Can combine stdcall with fastcall (redundant), regparm and
4752      sseregparm.  */
4753   else if (is_attribute_p ("stdcall", name))
4754     {
4755       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4756         {
4757           error ("stdcall and cdecl attributes are not compatible");
4758         }
4759       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4760         {
4761           error ("stdcall and fastcall attributes are not compatible");
4762         }
4763       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4764         {
4765           error ("stdcall and thiscall attributes are not compatible");
4766         }
4767     }
4768
4769   /* Can combine cdecl with regparm and sseregparm.  */
4770   else if (is_attribute_p ("cdecl", name))
4771     {
4772       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4773         {
4774           error ("stdcall and cdecl attributes are not compatible");
4775         }
4776       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4777         {
4778           error ("fastcall and cdecl attributes are not compatible");
4779         }
4780       if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (*node)))
4781         {
4782           error ("cdecl and thiscall attributes are not compatible");
4783         }
4784     }
4785   else if (is_attribute_p ("thiscall", name))
4786     {
4787       if (TREE_CODE (*node) != METHOD_TYPE && pedantic)
4788         warning (OPT_Wattributes, "%qE attribute is used for none class-method",
4789                  name);
4790       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
4791         {
4792           error ("stdcall and thiscall attributes are not compatible");
4793         }
4794       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
4795         {
4796           error ("fastcall and thiscall attributes are not compatible");
4797         }
4798       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
4799         {
4800           error ("cdecl and thiscall attributes are not compatible");
4801         }
4802     }
4803
4804   /* Can combine sseregparm with all attributes.  */
4805
4806   return NULL_TREE;
4807 }
4808
4809 /* Return 0 if the attributes for two types are incompatible, 1 if they
4810    are compatible, and 2 if they are nearly compatible (which causes a
4811    warning to be generated).  */
4812
4813 static int
4814 ix86_comp_type_attributes (const_tree type1, const_tree type2)
4815 {
4816   /* Check for mismatch of non-default calling convention.  */
4817   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
4818
4819   if (TREE_CODE (type1) != FUNCTION_TYPE
4820       && TREE_CODE (type1) != METHOD_TYPE)
4821     return 1;
4822
4823   /* Check for mismatched fastcall/regparm types.  */
4824   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
4825        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
4826       || (ix86_function_regparm (type1, NULL)
4827           != ix86_function_regparm (type2, NULL)))
4828     return 0;
4829
4830   /* Check for mismatched sseregparm types.  */
4831   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
4832       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
4833     return 0;
4834
4835   /* Check for mismatched thiscall types.  */
4836   if (!lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type1))
4837       != !lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type2)))
4838     return 0;
4839
4840   /* Check for mismatched return types (cdecl vs stdcall).  */
4841   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
4842       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
4843     return 0;
4844
4845   return 1;
4846 }
4847 \f
4848 /* Return the regparm value for a function with the indicated TYPE and DECL.
4849    DECL may be NULL when calling function indirectly
4850    or considering a libcall.  */
4851
4852 static int
4853 ix86_function_regparm (const_tree type, const_tree decl)
4854 {
4855   tree attr;
4856   int regparm;
4857
4858   if (TARGET_64BIT)
4859     return (ix86_function_type_abi (type) == SYSV_ABI
4860             ? X86_64_REGPARM_MAX : X86_64_MS_REGPARM_MAX);
4861
4862   regparm = ix86_regparm;
4863   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
4864   if (attr)
4865     {
4866       regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
4867       return regparm;
4868     }
4869
4870   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
4871     return 2;
4872
4873   if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
4874     return 1;
4875
4876   /* Use register calling convention for local functions when possible.  */
4877   if (decl
4878       && TREE_CODE (decl) == FUNCTION_DECL
4879       && optimize
4880       && !(profile_flag && !flag_fentry))
4881     {
4882       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4883       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE (decl));
4884       if (i && i->local)
4885         {
4886           int local_regparm, globals = 0, regno;
4887
4888           /* Make sure no regparm register is taken by a
4889              fixed register variable.  */
4890           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
4891             if (fixed_regs[local_regparm])
4892               break;
4893
4894           /* We don't want to use regparm(3) for nested functions as
4895              these use a static chain pointer in the third argument.  */
4896           if (local_regparm == 3 && DECL_STATIC_CHAIN (decl))
4897             local_regparm = 2;
4898
4899           /* In 32-bit mode save a register for the split stack.  */
4900           if (!TARGET_64BIT && local_regparm == 3 && flag_split_stack)
4901             local_regparm = 2;
4902
4903           /* Each fixed register usage increases register pressure,
4904              so less registers should be used for argument passing.
4905              This functionality can be overriden by an explicit
4906              regparm value.  */
4907           for (regno = 0; regno <= DI_REG; regno++)
4908             if (fixed_regs[regno])
4909               globals++;
4910
4911           local_regparm
4912             = globals < local_regparm ? local_regparm - globals : 0;
4913
4914           if (local_regparm > regparm)
4915             regparm = local_regparm;
4916         }
4917     }
4918
4919   return regparm;
4920 }
4921
4922 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
4923    DFmode (2) arguments in SSE registers for a function with the
4924    indicated TYPE and DECL.  DECL may be NULL when calling function
4925    indirectly or considering a libcall.  Otherwise return 0.  */
4926
4927 static int
4928 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
4929 {
4930   gcc_assert (!TARGET_64BIT);
4931
4932   /* Use SSE registers to pass SFmode and DFmode arguments if requested
4933      by the sseregparm attribute.  */
4934   if (TARGET_SSEREGPARM
4935       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
4936     {
4937       if (!TARGET_SSE)
4938         {
4939           if (warn)
4940             {
4941               if (decl)
4942                 error ("Calling %qD with attribute sseregparm without "
4943                        "SSE/SSE2 enabled", decl);
4944               else
4945                 error ("Calling %qT with attribute sseregparm without "
4946                        "SSE/SSE2 enabled", type);
4947             }
4948           return 0;
4949         }
4950
4951       return 2;
4952     }
4953
4954   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
4955      (and DFmode for SSE2) arguments in SSE registers.  */
4956   if (decl && TARGET_SSE_MATH && optimize
4957       && !(profile_flag && !flag_fentry))
4958     {
4959       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
4960       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
4961       if (i && i->local)
4962         return TARGET_SSE2 ? 2 : 1;
4963     }
4964
4965   return 0;
4966 }
4967
4968 /* Return true if EAX is live at the start of the function.  Used by
4969    ix86_expand_prologue to determine if we need special help before
4970    calling allocate_stack_worker.  */
4971
4972 static bool
4973 ix86_eax_live_at_start_p (void)
4974 {
4975   /* Cheat.  Don't bother working forward from ix86_function_regparm
4976      to the function type to whether an actual argument is located in
4977      eax.  Instead just look at cfg info, which is still close enough
4978      to correct at this point.  This gives false positives for broken
4979      functions that might use uninitialized data that happens to be
4980      allocated in eax, but who cares?  */
4981   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
4982 }
4983
4984 /* Value is the number of bytes of arguments automatically
4985    popped when returning from a subroutine call.
4986    FUNDECL is the declaration node of the function (as a tree),
4987    FUNTYPE is the data type of the function (as a tree),
4988    or for a library call it is an identifier node for the subroutine name.
4989    SIZE is the number of bytes of arguments passed on the stack.
4990
4991    On the 80386, the RTD insn may be used to pop them if the number
4992      of args is fixed, but if the number is variable then the caller
4993      must pop them all.  RTD can't be used for library calls now
4994      because the library is compiled with the Unix compiler.
4995    Use of RTD is a selectable option, since it is incompatible with
4996    standard Unix calling sequences.  If the option is not selected,
4997    the caller must always pop the args.
4998
4999    The attribute stdcall is equivalent to RTD on a per module basis.  */
5000
5001 static int
5002 ix86_return_pops_args (tree fundecl, tree funtype, int size)
5003 {
5004   int rtd;
5005
5006   /* None of the 64-bit ABIs pop arguments.  */
5007   if (TARGET_64BIT)
5008     return 0;
5009
5010   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
5011
5012   /* Cdecl functions override -mrtd, and never pop the stack.  */
5013   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
5014     {
5015       /* Stdcall and fastcall functions will pop the stack if not
5016          variable args.  */
5017       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
5018           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype))
5019           || lookup_attribute ("thiscall", TYPE_ATTRIBUTES (funtype)))
5020         rtd = 1;
5021
5022       if (rtd && ! stdarg_p (funtype))
5023         return size;
5024     }
5025
5026   /* Lose any fake structure return argument if it is passed on the stack.  */
5027   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
5028       && !KEEP_AGGREGATE_RETURN_POINTER)
5029     {
5030       int nregs = ix86_function_regparm (funtype, fundecl);
5031       if (nregs == 0)
5032         return GET_MODE_SIZE (Pmode);
5033     }
5034
5035   return 0;
5036 }
5037 \f
5038 /* Argument support functions.  */
5039
5040 /* Return true when register may be used to pass function parameters.  */
5041 bool
5042 ix86_function_arg_regno_p (int regno)
5043 {
5044   int i;
5045   const int *parm_regs;
5046
5047   if (!TARGET_64BIT)
5048     {
5049       if (TARGET_MACHO)
5050         return (regno < REGPARM_MAX
5051                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
5052       else
5053         return (regno < REGPARM_MAX
5054                 || (TARGET_MMX && MMX_REGNO_P (regno)
5055                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
5056                 || (TARGET_SSE && SSE_REGNO_P (regno)
5057                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
5058     }
5059
5060   if (TARGET_MACHO)
5061     {
5062       if (SSE_REGNO_P (regno) && TARGET_SSE)
5063         return true;
5064     }
5065   else
5066     {
5067       if (TARGET_SSE && SSE_REGNO_P (regno)
5068           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
5069         return true;
5070     }
5071
5072   /* TODO: The function should depend on current function ABI but
5073      builtins.c would need updating then. Therefore we use the
5074      default ABI.  */
5075
5076   /* RAX is used as hidden argument to va_arg functions.  */
5077   if (ix86_abi == SYSV_ABI && regno == AX_REG)
5078     return true;
5079
5080   if (ix86_abi == MS_ABI)
5081     parm_regs = x86_64_ms_abi_int_parameter_registers;
5082   else
5083     parm_regs = x86_64_int_parameter_registers;
5084   for (i = 0; i < (ix86_abi == MS_ABI
5085                    ? X86_64_MS_REGPARM_MAX : X86_64_REGPARM_MAX); i++)
5086     if (regno == parm_regs[i])
5087       return true;
5088   return false;
5089 }
5090
5091 /* Return if we do not know how to pass TYPE solely in registers.  */
5092
5093 static bool
5094 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
5095 {
5096   if (must_pass_in_stack_var_size_or_pad (mode, type))
5097     return true;
5098
5099   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
5100      The layout_type routine is crafty and tries to trick us into passing
5101      currently unsupported vector types on the stack by using TImode.  */
5102   return (!TARGET_64BIT && mode == TImode
5103           && type && TREE_CODE (type) != VECTOR_TYPE);
5104 }
5105
5106 /* It returns the size, in bytes, of the area reserved for arguments passed
5107    in registers for the function represented by fndecl dependent to the used
5108    abi format.  */
5109 int
5110 ix86_reg_parm_stack_space (const_tree fndecl)
5111 {
5112   enum calling_abi call_abi = SYSV_ABI;
5113   if (fndecl != NULL_TREE && TREE_CODE (fndecl) == FUNCTION_DECL)
5114     call_abi = ix86_function_abi (fndecl);
5115   else
5116     call_abi = ix86_function_type_abi (fndecl);
5117   if (call_abi == MS_ABI)
5118     return 32;
5119   return 0;
5120 }
5121
5122 /* Returns value SYSV_ABI, MS_ABI dependent on fntype, specifying the
5123    call abi used.  */
5124 enum calling_abi
5125 ix86_function_type_abi (const_tree fntype)
5126 {
5127   if (TARGET_64BIT && fntype != NULL)
5128     {
5129       enum calling_abi abi = ix86_abi;
5130       if (abi == SYSV_ABI)
5131         {
5132           if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (fntype)))
5133             abi = MS_ABI;
5134         }
5135       else if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (fntype)))
5136         abi = SYSV_ABI;
5137       return abi;
5138     }
5139   return ix86_abi;
5140 }
5141
5142 static bool
5143 ix86_function_ms_hook_prologue (const_tree fn)
5144 {
5145   if (fn && lookup_attribute ("ms_hook_prologue", DECL_ATTRIBUTES (fn)))
5146     {
5147       if (decl_function_context (fn) != NULL_TREE)
5148         error_at (DECL_SOURCE_LOCATION (fn),
5149                   "ms_hook_prologue is not compatible with nested function");
5150       else
5151         return true;
5152     }
5153   return false;
5154 }
5155
5156 static enum calling_abi
5157 ix86_function_abi (const_tree fndecl)
5158 {
5159   if (! fndecl)
5160     return ix86_abi;
5161   return ix86_function_type_abi (TREE_TYPE (fndecl));
5162 }
5163
5164 /* Returns value SYSV_ABI, MS_ABI dependent on cfun, specifying the
5165    call abi used.  */
5166 enum calling_abi
5167 ix86_cfun_abi (void)
5168 {
5169   if (! cfun || ! TARGET_64BIT)
5170     return ix86_abi;
5171   return cfun->machine->call_abi;
5172 }
5173
5174 /* Write the extra assembler code needed to declare a function properly.  */
5175
5176 void
5177 ix86_asm_output_function_label (FILE *asm_out_file, const char *fname,
5178                                 tree decl)
5179 {
5180   bool is_ms_hook = ix86_function_ms_hook_prologue (decl);
5181
5182   if (is_ms_hook)
5183     {
5184       int i, filler_count = (TARGET_64BIT ? 32 : 16);
5185       unsigned int filler_cc = 0xcccccccc;
5186
5187       for (i = 0; i < filler_count; i += 4)
5188         fprintf (asm_out_file, ASM_LONG " %#x\n", filler_cc);
5189     }
5190
5191   ASM_OUTPUT_LABEL (asm_out_file, fname);
5192
5193   /* Output magic byte marker, if hot-patch attribute is set.  */
5194   if (is_ms_hook)
5195     {
5196       if (TARGET_64BIT)
5197         {
5198           /* leaq [%rsp + 0], %rsp  */
5199           asm_fprintf (asm_out_file, ASM_BYTE
5200                        "0x48, 0x8d, 0xa4, 0x24, 0x00, 0x00, 0x00, 0x00\n");
5201         }
5202       else
5203         {
5204           /* movl.s %edi, %edi
5205              push   %ebp
5206              movl.s %esp, %ebp */
5207           asm_fprintf (asm_out_file, ASM_BYTE
5208                        "0x8b, 0xff, 0x55, 0x8b, 0xec\n");
5209         }
5210     }
5211 }
5212
5213 /* regclass.c  */
5214 extern void init_regs (void);
5215
5216 /* Implementation of call abi switching target hook. Specific to FNDECL
5217    the specific call register sets are set. See also CONDITIONAL_REGISTER_USAGE
5218    for more details.  */
5219 void
5220 ix86_call_abi_override (const_tree fndecl)
5221 {
5222   if (fndecl == NULL_TREE)
5223     cfun->machine->call_abi = ix86_abi;
5224   else
5225     cfun->machine->call_abi = ix86_function_type_abi (TREE_TYPE (fndecl));
5226 }
5227
5228 /* MS and SYSV ABI have different set of call used registers.  Avoid expensive
5229    re-initialization of init_regs each time we switch function context since
5230    this is needed only during RTL expansion.  */
5231 static void
5232 ix86_maybe_switch_abi (void)
5233 {
5234   if (TARGET_64BIT &&
5235       call_used_regs[SI_REG] == (cfun->machine->call_abi == MS_ABI))
5236     reinit_regs ();
5237 }
5238
5239 /* Initialize a variable CUM of type CUMULATIVE_ARGS
5240    for a call to a function whose data type is FNTYPE.
5241    For a library call, FNTYPE is 0.  */
5242
5243 void
5244 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
5245                       tree fntype,      /* tree ptr for function decl */
5246                       rtx libname,      /* SYMBOL_REF of library name or 0 */
5247                       tree fndecl)
5248 {
5249   struct cgraph_local_info *i = fndecl ? cgraph_local_info (fndecl) : NULL;
5250   memset (cum, 0, sizeof (*cum));
5251
5252   if (fndecl)
5253    cum->call_abi = ix86_function_abi (fndecl);
5254   else
5255    cum->call_abi = ix86_function_type_abi (fntype);
5256   /* Set up the number of registers to use for passing arguments.  */
5257
5258   if (cum->call_abi == MS_ABI && !ACCUMULATE_OUTGOING_ARGS)
5259     sorry ("ms_abi attribute requires -maccumulate-outgoing-args "
5260            "or subtarget optimization implying it");
5261   cum->nregs = ix86_regparm;
5262   if (TARGET_64BIT)
5263     {
5264       cum->nregs = (cum->call_abi == SYSV_ABI
5265                    ? X86_64_REGPARM_MAX
5266                    : X86_64_MS_REGPARM_MAX);
5267     }
5268   if (TARGET_SSE)
5269     {
5270       cum->sse_nregs = SSE_REGPARM_MAX;
5271       if (TARGET_64BIT)
5272         {
5273           cum->sse_nregs = (cum->call_abi == SYSV_ABI
5274                            ? X86_64_SSE_REGPARM_MAX
5275                            : X86_64_MS_SSE_REGPARM_MAX);
5276         }
5277     }
5278   if (TARGET_MMX)
5279     cum->mmx_nregs = MMX_REGPARM_MAX;
5280   cum->warn_avx = true;
5281   cum->warn_sse = true;
5282   cum->warn_mmx = true;
5283
5284   /* Because type might mismatch in between caller and callee, we need to
5285      use actual type of function for local calls.
5286      FIXME: cgraph_analyze can be told to actually record if function uses
5287      va_start so for local functions maybe_vaarg can be made aggressive
5288      helping K&R code.
5289      FIXME: once typesytem is fixed, we won't need this code anymore.  */
5290   if (i && i->local)
5291     fntype = TREE_TYPE (fndecl);
5292   cum->maybe_vaarg = (fntype
5293                       ? (!prototype_p (fntype) || stdarg_p (fntype))
5294                       : !libname);
5295
5296   if (!TARGET_64BIT)
5297     {
5298       /* If there are variable arguments, then we won't pass anything
5299          in registers in 32-bit mode. */
5300       if (stdarg_p (fntype))
5301         {
5302           cum->nregs = 0;
5303           cum->sse_nregs = 0;
5304           cum->mmx_nregs = 0;
5305           cum->warn_avx = 0;
5306           cum->warn_sse = 0;
5307           cum->warn_mmx = 0;
5308           return;
5309         }
5310
5311       /* Use ecx and edx registers if function has fastcall attribute,
5312          else look for regparm information.  */
5313       if (fntype)
5314         {
5315           if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
5316             {
5317               cum->nregs = 1;
5318               cum->fastcall = 1; /* Same first register as in fastcall.  */
5319             }
5320           else if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
5321             {
5322               cum->nregs = 2;
5323               cum->fastcall = 1;
5324             }
5325           else
5326             cum->nregs = ix86_function_regparm (fntype, fndecl);
5327         }
5328
5329       /* Set up the number of SSE registers used for passing SFmode
5330          and DFmode arguments.  Warn for mismatching ABI.  */
5331       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
5332     }
5333 }
5334
5335 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
5336    But in the case of vector types, it is some vector mode.
5337
5338    When we have only some of our vector isa extensions enabled, then there
5339    are some modes for which vector_mode_supported_p is false.  For these
5340    modes, the generic vector support in gcc will choose some non-vector mode
5341    in order to implement the type.  By computing the natural mode, we'll
5342    select the proper ABI location for the operand and not depend on whatever
5343    the middle-end decides to do with these vector types.
5344
5345    The midde-end can't deal with the vector types > 16 bytes.  In this
5346    case, we return the original mode and warn ABI change if CUM isn't
5347    NULL.  */
5348
5349 static enum machine_mode
5350 type_natural_mode (const_tree type, const CUMULATIVE_ARGS *cum)
5351 {
5352   enum machine_mode mode = TYPE_MODE (type);
5353
5354   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
5355     {
5356       HOST_WIDE_INT size = int_size_in_bytes (type);
5357       if ((size == 8 || size == 16 || size == 32)
5358           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
5359           && TYPE_VECTOR_SUBPARTS (type) > 1)
5360         {
5361           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
5362
5363           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
5364             mode = MIN_MODE_VECTOR_FLOAT;
5365           else
5366             mode = MIN_MODE_VECTOR_INT;
5367
5368           /* Get the mode which has this inner mode and number of units.  */
5369           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
5370             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
5371                 && GET_MODE_INNER (mode) == innermode)
5372               {
5373                 if (size == 32 && !TARGET_AVX)
5374                   {
5375                     static bool warnedavx;
5376
5377                     if (cum
5378                         && !warnedavx
5379                         && cum->warn_avx)
5380                       {
5381                         warnedavx = true;
5382                         warning (0, "AVX vector argument without AVX "
5383                                  "enabled changes the ABI");
5384                       }
5385                     return TYPE_MODE (type);
5386                   }
5387                 else
5388                   return mode;
5389               }
5390
5391           gcc_unreachable ();
5392         }
5393     }
5394
5395   return mode;
5396 }
5397
5398 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
5399    this may not agree with the mode that the type system has chosen for the
5400    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
5401    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
5402
5403 static rtx
5404 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
5405                      unsigned int regno)
5406 {
5407   rtx tmp;
5408
5409   if (orig_mode != BLKmode)
5410     tmp = gen_rtx_REG (orig_mode, regno);
5411   else
5412     {
5413       tmp = gen_rtx_REG (mode, regno);
5414       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
5415       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
5416     }
5417
5418   return tmp;
5419 }
5420
5421 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
5422    of this code is to classify each 8bytes of incoming argument by the register
5423    class and assign registers accordingly.  */
5424
5425 /* Return the union class of CLASS1 and CLASS2.
5426    See the x86-64 PS ABI for details.  */
5427
5428 static enum x86_64_reg_class
5429 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
5430 {
5431   /* Rule #1: If both classes are equal, this is the resulting class.  */
5432   if (class1 == class2)
5433     return class1;
5434
5435   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
5436      the other class.  */
5437   if (class1 == X86_64_NO_CLASS)
5438     return class2;
5439   if (class2 == X86_64_NO_CLASS)
5440     return class1;
5441
5442   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
5443   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
5444     return X86_64_MEMORY_CLASS;
5445
5446   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
5447   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
5448       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
5449     return X86_64_INTEGERSI_CLASS;
5450   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
5451       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
5452     return X86_64_INTEGER_CLASS;
5453
5454   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
5455      MEMORY is used.  */
5456   if (class1 == X86_64_X87_CLASS
5457       || class1 == X86_64_X87UP_CLASS
5458       || class1 == X86_64_COMPLEX_X87_CLASS
5459       || class2 == X86_64_X87_CLASS
5460       || class2 == X86_64_X87UP_CLASS
5461       || class2 == X86_64_COMPLEX_X87_CLASS)
5462     return X86_64_MEMORY_CLASS;
5463
5464   /* Rule #6: Otherwise class SSE is used.  */
5465   return X86_64_SSE_CLASS;
5466 }
5467
5468 /* Classify the argument of type TYPE and mode MODE.
5469    CLASSES will be filled by the register class used to pass each word
5470    of the operand.  The number of words is returned.  In case the parameter
5471    should be passed in memory, 0 is returned. As a special case for zero
5472    sized containers, classes[0] will be NO_CLASS and 1 is returned.
5473
5474    BIT_OFFSET is used internally for handling records and specifies offset
5475    of the offset in bits modulo 256 to avoid overflow cases.
5476
5477    See the x86-64 PS ABI for details.
5478 */
5479
5480 static int
5481 classify_argument (enum machine_mode mode, const_tree type,
5482                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
5483 {
5484   HOST_WIDE_INT bytes =
5485     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5486   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5487
5488   /* Variable sized entities are always passed/returned in memory.  */
5489   if (bytes < 0)
5490     return 0;
5491
5492   if (mode != VOIDmode
5493       && targetm.calls.must_pass_in_stack (mode, type))
5494     return 0;
5495
5496   if (type && AGGREGATE_TYPE_P (type))
5497     {
5498       int i;
5499       tree field;
5500       enum x86_64_reg_class subclasses[MAX_CLASSES];
5501
5502       /* On x86-64 we pass structures larger than 32 bytes on the stack.  */
5503       if (bytes > 32)
5504         return 0;
5505
5506       for (i = 0; i < words; i++)
5507         classes[i] = X86_64_NO_CLASS;
5508
5509       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
5510          signalize memory class, so handle it as special case.  */
5511       if (!words)
5512         {
5513           classes[0] = X86_64_NO_CLASS;
5514           return 1;
5515         }
5516
5517       /* Classify each field of record and merge classes.  */
5518       switch (TREE_CODE (type))
5519         {
5520         case RECORD_TYPE:
5521           /* And now merge the fields of structure.  */
5522           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5523             {
5524               if (TREE_CODE (field) == FIELD_DECL)
5525                 {
5526                   int num;
5527
5528                   if (TREE_TYPE (field) == error_mark_node)
5529                     continue;
5530
5531                   /* Bitfields are always classified as integer.  Handle them
5532                      early, since later code would consider them to be
5533                      misaligned integers.  */
5534                   if (DECL_BIT_FIELD (field))
5535                     {
5536                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5537                            i < ((int_bit_position (field) + (bit_offset % 64))
5538                                 + tree_low_cst (DECL_SIZE (field), 0)
5539                                 + 63) / 8 / 8; i++)
5540                         classes[i] =
5541                           merge_classes (X86_64_INTEGER_CLASS,
5542                                          classes[i]);
5543                     }
5544                   else
5545                     {
5546                       int pos;
5547
5548                       type = TREE_TYPE (field);
5549
5550                       /* Flexible array member is ignored.  */
5551                       if (TYPE_MODE (type) == BLKmode
5552                           && TREE_CODE (type) == ARRAY_TYPE
5553                           && TYPE_SIZE (type) == NULL_TREE
5554                           && TYPE_DOMAIN (type) != NULL_TREE
5555                           && (TYPE_MAX_VALUE (TYPE_DOMAIN (type))
5556                               == NULL_TREE))
5557                         {
5558                           static bool warned;
5559
5560                           if (!warned && warn_psabi)
5561                             {
5562                               warned = true;
5563                               inform (input_location,
5564                                       "The ABI of passing struct with"
5565                                       " a flexible array member has"
5566                                       " changed in GCC 4.4");
5567                             }
5568                           continue;
5569                         }
5570                       num = classify_argument (TYPE_MODE (type), type,
5571                                                subclasses,
5572                                                (int_bit_position (field)
5573                                                 + bit_offset) % 256);
5574                       if (!num)
5575                         return 0;
5576                       pos = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
5577                       for (i = 0; i < num && (i + pos) < words; i++)
5578                         classes[i + pos] =
5579                           merge_classes (subclasses[i], classes[i + pos]);
5580                     }
5581                 }
5582             }
5583           break;
5584
5585         case ARRAY_TYPE:
5586           /* Arrays are handled as small records.  */
5587           {
5588             int num;
5589             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
5590                                      TREE_TYPE (type), subclasses, bit_offset);
5591             if (!num)
5592               return 0;
5593
5594             /* The partial classes are now full classes.  */
5595             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
5596               subclasses[0] = X86_64_SSE_CLASS;
5597             if (subclasses[0] == X86_64_INTEGERSI_CLASS
5598                 && !((bit_offset % 64) == 0 && bytes == 4))
5599               subclasses[0] = X86_64_INTEGER_CLASS;
5600
5601             for (i = 0; i < words; i++)
5602               classes[i] = subclasses[i % num];
5603
5604             break;
5605           }
5606         case UNION_TYPE:
5607         case QUAL_UNION_TYPE:
5608           /* Unions are similar to RECORD_TYPE but offset is always 0.
5609              */
5610           for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5611             {
5612               if (TREE_CODE (field) == FIELD_DECL)
5613                 {
5614                   int num;
5615
5616                   if (TREE_TYPE (field) == error_mark_node)
5617                     continue;
5618
5619                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
5620                                            TREE_TYPE (field), subclasses,
5621                                            bit_offset);
5622                   if (!num)
5623                     return 0;
5624                   for (i = 0; i < num; i++)
5625                     classes[i] = merge_classes (subclasses[i], classes[i]);
5626                 }
5627             }
5628           break;
5629
5630         default:
5631           gcc_unreachable ();
5632         }
5633
5634       if (words > 2)
5635         {
5636           /* When size > 16 bytes, if the first one isn't
5637              X86_64_SSE_CLASS or any other ones aren't
5638              X86_64_SSEUP_CLASS, everything should be passed in
5639              memory.  */
5640           if (classes[0] != X86_64_SSE_CLASS)
5641               return 0;
5642
5643           for (i = 1; i < words; i++)
5644             if (classes[i] != X86_64_SSEUP_CLASS)
5645               return 0;
5646         }
5647
5648       /* Final merger cleanup.  */
5649       for (i = 0; i < words; i++)
5650         {
5651           /* If one class is MEMORY, everything should be passed in
5652              memory.  */
5653           if (classes[i] == X86_64_MEMORY_CLASS)
5654             return 0;
5655
5656           /* The X86_64_SSEUP_CLASS should be always preceded by
5657              X86_64_SSE_CLASS or X86_64_SSEUP_CLASS.  */
5658           if (classes[i] == X86_64_SSEUP_CLASS
5659               && classes[i - 1] != X86_64_SSE_CLASS
5660               && classes[i - 1] != X86_64_SSEUP_CLASS)
5661             {
5662               /* The first one should never be X86_64_SSEUP_CLASS.  */
5663               gcc_assert (i != 0);
5664               classes[i] = X86_64_SSE_CLASS;
5665             }
5666
5667           /*  If X86_64_X87UP_CLASS isn't preceded by X86_64_X87_CLASS,
5668                everything should be passed in memory.  */
5669           if (classes[i] == X86_64_X87UP_CLASS
5670               && (classes[i - 1] != X86_64_X87_CLASS))
5671             {
5672               static bool warned;
5673
5674               /* The first one should never be X86_64_X87UP_CLASS.  */
5675               gcc_assert (i != 0);
5676               if (!warned && warn_psabi)
5677                 {
5678                   warned = true;
5679                   inform (input_location,
5680                           "The ABI of passing union with long double"
5681                           " has changed in GCC 4.4");
5682                 }
5683               return 0;
5684             }
5685         }
5686       return words;
5687     }
5688
5689   /* Compute alignment needed.  We align all types to natural boundaries with
5690      exception of XFmode that is aligned to 64bits.  */
5691   if (mode != VOIDmode && mode != BLKmode)
5692     {
5693       int mode_alignment = GET_MODE_BITSIZE (mode);
5694
5695       if (mode == XFmode)
5696         mode_alignment = 128;
5697       else if (mode == XCmode)
5698         mode_alignment = 256;
5699       if (COMPLEX_MODE_P (mode))
5700         mode_alignment /= 2;
5701       /* Misaligned fields are always returned in memory.  */
5702       if (bit_offset % mode_alignment)
5703         return 0;
5704     }
5705
5706   /* for V1xx modes, just use the base mode */
5707   if (VECTOR_MODE_P (mode) && mode != V1DImode && mode != V1TImode
5708       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
5709     mode = GET_MODE_INNER (mode);
5710
5711   /* Classification of atomic types.  */
5712   switch (mode)
5713     {
5714     case SDmode:
5715     case DDmode:
5716       classes[0] = X86_64_SSE_CLASS;
5717       return 1;
5718     case TDmode:
5719       classes[0] = X86_64_SSE_CLASS;
5720       classes[1] = X86_64_SSEUP_CLASS;
5721       return 2;
5722     case DImode:
5723     case SImode:
5724     case HImode:
5725     case QImode:
5726     case CSImode:
5727     case CHImode:
5728     case CQImode:
5729       {
5730         int size = (bit_offset % 64)+ (int) GET_MODE_BITSIZE (mode);
5731
5732         if (size <= 32)
5733           {
5734             classes[0] = X86_64_INTEGERSI_CLASS;
5735             return 1;
5736           }
5737         else if (size <= 64)
5738           {
5739             classes[0] = X86_64_INTEGER_CLASS;
5740             return 1;
5741           }
5742         else if (size <= 64+32)
5743           {
5744             classes[0] = X86_64_INTEGER_CLASS;
5745             classes[1] = X86_64_INTEGERSI_CLASS;
5746             return 2;
5747           }
5748         else if (size <= 64+64)
5749           {
5750             classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5751             return 2;
5752           }
5753         else
5754           gcc_unreachable ();
5755       }
5756     case CDImode:
5757     case TImode:
5758       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
5759       return 2;
5760     case COImode:
5761     case OImode:
5762       /* OImode shouldn't be used directly.  */
5763       gcc_unreachable ();
5764     case CTImode:
5765       return 0;
5766     case SFmode:
5767       if (!(bit_offset % 64))
5768         classes[0] = X86_64_SSESF_CLASS;
5769       else
5770         classes[0] = X86_64_SSE_CLASS;
5771       return 1;
5772     case DFmode:
5773       classes[0] = X86_64_SSEDF_CLASS;
5774       return 1;
5775     case XFmode:
5776       classes[0] = X86_64_X87_CLASS;
5777       classes[1] = X86_64_X87UP_CLASS;
5778       return 2;
5779     case TFmode:
5780       classes[0] = X86_64_SSE_CLASS;
5781       classes[1] = X86_64_SSEUP_CLASS;
5782       return 2;
5783     case SCmode:
5784       classes[0] = X86_64_SSE_CLASS;
5785       if (!(bit_offset % 64))
5786         return 1;
5787       else
5788         {
5789           static bool warned;
5790
5791           if (!warned && warn_psabi)
5792             {
5793               warned = true;
5794               inform (input_location,
5795                       "The ABI of passing structure with complex float"
5796                       " member has changed in GCC 4.4");
5797             }
5798           classes[1] = X86_64_SSESF_CLASS;
5799           return 2;
5800         }
5801     case DCmode:
5802       classes[0] = X86_64_SSEDF_CLASS;
5803       classes[1] = X86_64_SSEDF_CLASS;
5804       return 2;
5805     case XCmode:
5806       classes[0] = X86_64_COMPLEX_X87_CLASS;
5807       return 1;
5808     case TCmode:
5809       /* This modes is larger than 16 bytes.  */
5810       return 0;
5811     case V8SFmode:
5812     case V8SImode:
5813     case V32QImode:
5814     case V16HImode:
5815     case V4DFmode:
5816     case V4DImode:
5817       classes[0] = X86_64_SSE_CLASS;
5818       classes[1] = X86_64_SSEUP_CLASS;
5819       classes[2] = X86_64_SSEUP_CLASS;
5820       classes[3] = X86_64_SSEUP_CLASS;
5821       return 4;
5822     case V4SFmode:
5823     case V4SImode:
5824     case V16QImode:
5825     case V8HImode:
5826     case V2DFmode:
5827     case V2DImode:
5828       classes[0] = X86_64_SSE_CLASS;
5829       classes[1] = X86_64_SSEUP_CLASS;
5830       return 2;
5831     case V1TImode:
5832     case V1DImode:
5833     case V2SFmode:
5834     case V2SImode:
5835     case V4HImode:
5836     case V8QImode:
5837       classes[0] = X86_64_SSE_CLASS;
5838       return 1;
5839     case BLKmode:
5840     case VOIDmode:
5841       return 0;
5842     default:
5843       gcc_assert (VECTOR_MODE_P (mode));
5844
5845       if (bytes > 16)
5846         return 0;
5847
5848       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
5849
5850       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
5851         classes[0] = X86_64_INTEGERSI_CLASS;
5852       else
5853         classes[0] = X86_64_INTEGER_CLASS;
5854       classes[1] = X86_64_INTEGER_CLASS;
5855       return 1 + (bytes > 8);
5856     }
5857 }
5858
5859 /* Examine the argument and return set number of register required in each
5860    class.  Return 0 iff parameter should be passed in memory.  */
5861 static int
5862 examine_argument (enum machine_mode mode, const_tree type, int in_return,
5863                   int *int_nregs, int *sse_nregs)
5864 {
5865   enum x86_64_reg_class regclass[MAX_CLASSES];
5866   int n = classify_argument (mode, type, regclass, 0);
5867
5868   *int_nregs = 0;
5869   *sse_nregs = 0;
5870   if (!n)
5871     return 0;
5872   for (n--; n >= 0; n--)
5873     switch (regclass[n])
5874       {
5875       case X86_64_INTEGER_CLASS:
5876       case X86_64_INTEGERSI_CLASS:
5877         (*int_nregs)++;
5878         break;
5879       case X86_64_SSE_CLASS:
5880       case X86_64_SSESF_CLASS:
5881       case X86_64_SSEDF_CLASS:
5882         (*sse_nregs)++;
5883         break;
5884       case X86_64_NO_CLASS:
5885       case X86_64_SSEUP_CLASS:
5886         break;
5887       case X86_64_X87_CLASS:
5888       case X86_64_X87UP_CLASS:
5889         if (!in_return)
5890           return 0;
5891         break;
5892       case X86_64_COMPLEX_X87_CLASS:
5893         return in_return ? 2 : 0;
5894       case X86_64_MEMORY_CLASS:
5895         gcc_unreachable ();
5896       }
5897   return 1;
5898 }
5899
5900 /* Construct container for the argument used by GCC interface.  See
5901    FUNCTION_ARG for the detailed description.  */
5902
5903 static rtx
5904 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
5905                      const_tree type, int in_return, int nintregs, int nsseregs,
5906                      const int *intreg, int sse_regno)
5907 {
5908   /* The following variables hold the static issued_error state.  */
5909   static bool issued_sse_arg_error;
5910   static bool issued_sse_ret_error;
5911   static bool issued_x87_ret_error;
5912
5913   enum machine_mode tmpmode;
5914   int bytes =
5915     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
5916   enum x86_64_reg_class regclass[MAX_CLASSES];
5917   int n;
5918   int i;
5919   int nexps = 0;
5920   int needed_sseregs, needed_intregs;
5921   rtx exp[MAX_CLASSES];
5922   rtx ret;
5923
5924   n = classify_argument (mode, type, regclass, 0);
5925   if (!n)
5926     return NULL;
5927   if (!examine_argument (mode, type, in_return, &needed_intregs,
5928                          &needed_sseregs))
5929     return NULL;
5930   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
5931     return NULL;
5932
5933   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
5934      some less clueful developer tries to use floating-point anyway.  */
5935   if (needed_sseregs && !TARGET_SSE)
5936     {
5937       if (in_return)
5938         {
5939           if (!issued_sse_ret_error)
5940             {
5941               error ("SSE register return with SSE disabled");
5942               issued_sse_ret_error = true;
5943             }
5944         }
5945       else if (!issued_sse_arg_error)
5946         {
5947           error ("SSE register argument with SSE disabled");
5948           issued_sse_arg_error = true;
5949         }
5950       return NULL;
5951     }
5952
5953   /* Likewise, error if the ABI requires us to return values in the
5954      x87 registers and the user specified -mno-80387.  */
5955   if (!TARGET_80387 && in_return)
5956     for (i = 0; i < n; i++)
5957       if (regclass[i] == X86_64_X87_CLASS
5958           || regclass[i] == X86_64_X87UP_CLASS
5959           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
5960         {
5961           if (!issued_x87_ret_error)
5962             {
5963               error ("x87 register return with x87 disabled");
5964               issued_x87_ret_error = true;
5965             }
5966           return NULL;
5967         }
5968
5969   /* First construct simple cases.  Avoid SCmode, since we want to use
5970      single register to pass this type.  */
5971   if (n == 1 && mode != SCmode)
5972     switch (regclass[0])
5973       {
5974       case X86_64_INTEGER_CLASS:
5975       case X86_64_INTEGERSI_CLASS:
5976         return gen_rtx_REG (mode, intreg[0]);
5977       case X86_64_SSE_CLASS:
5978       case X86_64_SSESF_CLASS:
5979       case X86_64_SSEDF_CLASS:
5980         if (mode != BLKmode)
5981           return gen_reg_or_parallel (mode, orig_mode,
5982                                       SSE_REGNO (sse_regno));
5983         break;
5984       case X86_64_X87_CLASS:
5985       case X86_64_COMPLEX_X87_CLASS:
5986         return gen_rtx_REG (mode, FIRST_STACK_REG);
5987       case X86_64_NO_CLASS:
5988         /* Zero sized array, struct or class.  */
5989         return NULL;
5990       default:
5991         gcc_unreachable ();
5992       }
5993   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
5994       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
5995     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
5996   if (n == 4
5997       && regclass[0] == X86_64_SSE_CLASS
5998       && regclass[1] == X86_64_SSEUP_CLASS
5999       && regclass[2] == X86_64_SSEUP_CLASS
6000       && regclass[3] == X86_64_SSEUP_CLASS
6001       && mode != BLKmode)
6002     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
6003
6004   if (n == 2
6005       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
6006     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
6007   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
6008       && regclass[1] == X86_64_INTEGER_CLASS
6009       && (mode == CDImode || mode == TImode || mode == TFmode)
6010       && intreg[0] + 1 == intreg[1])
6011     return gen_rtx_REG (mode, intreg[0]);
6012
6013   /* Otherwise figure out the entries of the PARALLEL.  */
6014   for (i = 0; i < n; i++)
6015     {
6016       int pos;
6017
6018       switch (regclass[i])
6019         {
6020           case X86_64_NO_CLASS:
6021             break;
6022           case X86_64_INTEGER_CLASS:
6023           case X86_64_INTEGERSI_CLASS:
6024             /* Merge TImodes on aligned occasions here too.  */
6025             if (i * 8 + 8 > bytes)
6026               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
6027             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
6028               tmpmode = SImode;
6029             else
6030               tmpmode = DImode;
6031             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
6032             if (tmpmode == BLKmode)
6033               tmpmode = DImode;
6034             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6035                                                gen_rtx_REG (tmpmode, *intreg),
6036                                                GEN_INT (i*8));
6037             intreg++;
6038             break;
6039           case X86_64_SSESF_CLASS:
6040             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6041                                                gen_rtx_REG (SFmode,
6042                                                             SSE_REGNO (sse_regno)),
6043                                                GEN_INT (i*8));
6044             sse_regno++;
6045             break;
6046           case X86_64_SSEDF_CLASS:
6047             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6048                                                gen_rtx_REG (DFmode,
6049                                                             SSE_REGNO (sse_regno)),
6050                                                GEN_INT (i*8));
6051             sse_regno++;
6052             break;
6053           case X86_64_SSE_CLASS:
6054             pos = i;
6055             switch (n)
6056               {
6057               case 1:
6058                 tmpmode = DImode;
6059                 break;
6060               case 2:
6061                 if (i == 0 && regclass[1] == X86_64_SSEUP_CLASS)
6062                   {
6063                     tmpmode = TImode;
6064                     i++;
6065                   }
6066                 else
6067                   tmpmode = DImode;
6068                 break;
6069               case 4:
6070                 gcc_assert (i == 0
6071                             && regclass[1] == X86_64_SSEUP_CLASS
6072                             && regclass[2] == X86_64_SSEUP_CLASS
6073                             && regclass[3] == X86_64_SSEUP_CLASS);
6074                 tmpmode = OImode;
6075                 i += 3;
6076                 break;
6077               default:
6078                 gcc_unreachable ();
6079               }
6080             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
6081                                                gen_rtx_REG (tmpmode,
6082                                                             SSE_REGNO (sse_regno)),
6083                                                GEN_INT (pos*8));
6084             sse_regno++;
6085             break;
6086           default:
6087             gcc_unreachable ();
6088         }
6089     }
6090
6091   /* Empty aligned struct, union or class.  */
6092   if (nexps == 0)
6093     return NULL;
6094
6095   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
6096   for (i = 0; i < nexps; i++)
6097     XVECEXP (ret, 0, i) = exp [i];
6098   return ret;
6099 }
6100
6101 /* Update the data in CUM to advance over an argument of mode MODE
6102    and data type TYPE.  (TYPE is null for libcalls where that information
6103    may not be available.)  */
6104
6105 static void
6106 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6107                          const_tree type, HOST_WIDE_INT bytes,
6108                          HOST_WIDE_INT words)
6109 {
6110   switch (mode)
6111     {
6112     default:
6113       break;
6114
6115     case BLKmode:
6116       if (bytes < 0)
6117         break;
6118       /* FALLTHRU */
6119
6120     case DImode:
6121     case SImode:
6122     case HImode:
6123     case QImode:
6124       cum->words += words;
6125       cum->nregs -= words;
6126       cum->regno += words;
6127
6128       if (cum->nregs <= 0)
6129         {
6130           cum->nregs = 0;
6131           cum->regno = 0;
6132         }
6133       break;
6134
6135     case OImode:
6136       /* OImode shouldn't be used directly.  */
6137       gcc_unreachable ();
6138
6139     case DFmode:
6140       if (cum->float_in_sse < 2)
6141         break;
6142     case SFmode:
6143       if (cum->float_in_sse < 1)
6144         break;
6145       /* FALLTHRU */
6146
6147     case V8SFmode:
6148     case V8SImode:
6149     case V32QImode:
6150     case V16HImode:
6151     case V4DFmode:
6152     case V4DImode:
6153     case TImode:
6154     case V16QImode:
6155     case V8HImode:
6156     case V4SImode:
6157     case V2DImode:
6158     case V4SFmode:
6159     case V2DFmode:
6160       if (!type || !AGGREGATE_TYPE_P (type))
6161         {
6162           cum->sse_words += words;
6163           cum->sse_nregs -= 1;
6164           cum->sse_regno += 1;
6165           if (cum->sse_nregs <= 0)
6166             {
6167               cum->sse_nregs = 0;
6168               cum->sse_regno = 0;
6169             }
6170         }
6171       break;
6172
6173     case V8QImode:
6174     case V4HImode:
6175     case V2SImode:
6176     case V2SFmode:
6177     case V1TImode:
6178     case V1DImode:
6179       if (!type || !AGGREGATE_TYPE_P (type))
6180         {
6181           cum->mmx_words += words;
6182           cum->mmx_nregs -= 1;
6183           cum->mmx_regno += 1;
6184           if (cum->mmx_nregs <= 0)
6185             {
6186               cum->mmx_nregs = 0;
6187               cum->mmx_regno = 0;
6188             }
6189         }
6190       break;
6191     }
6192 }
6193
6194 static void
6195 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6196                          const_tree type, HOST_WIDE_INT words, bool named)
6197 {
6198   int int_nregs, sse_nregs;
6199
6200   /* Unnamed 256bit vector mode parameters are passed on stack.  */
6201   if (!named && VALID_AVX256_REG_MODE (mode))
6202     return;
6203
6204   if (examine_argument (mode, type, 0, &int_nregs, &sse_nregs)
6205       && sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
6206     {
6207       cum->nregs -= int_nregs;
6208       cum->sse_nregs -= sse_nregs;
6209       cum->regno += int_nregs;
6210       cum->sse_regno += sse_nregs;
6211     }
6212   else
6213     {
6214       int align = ix86_function_arg_boundary (mode, type) / BITS_PER_WORD;
6215       cum->words = (cum->words + align - 1) & ~(align - 1);
6216       cum->words += words;
6217     }
6218 }
6219
6220 static void
6221 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
6222                             HOST_WIDE_INT words)
6223 {
6224   /* Otherwise, this should be passed indirect.  */
6225   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
6226
6227   cum->words += words;
6228   if (cum->nregs > 0)
6229     {
6230       cum->nregs -= 1;
6231       cum->regno += 1;
6232     }
6233 }
6234
6235 /* Update the data in CUM to advance over an argument of mode MODE and
6236    data type TYPE.  (TYPE is null for libcalls where that information
6237    may not be available.)  */
6238
6239 static void
6240 ix86_function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
6241                            const_tree type, bool named)
6242 {
6243   HOST_WIDE_INT bytes, words;
6244
6245   if (mode == BLKmode)
6246     bytes = int_size_in_bytes (type);
6247   else
6248     bytes = GET_MODE_SIZE (mode);
6249   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6250
6251   if (type)
6252     mode = type_natural_mode (type, NULL);
6253
6254   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6255     function_arg_advance_ms_64 (cum, bytes, words);
6256   else if (TARGET_64BIT)
6257     function_arg_advance_64 (cum, mode, type, words, named);
6258   else
6259     function_arg_advance_32 (cum, mode, type, bytes, words);
6260 }
6261
6262 /* Define where to put the arguments to a function.
6263    Value is zero to push the argument on the stack,
6264    or a hard register in which to store the argument.
6265
6266    MODE is the argument's machine mode.
6267    TYPE is the data type of the argument (as a tree).
6268     This is null for libcalls where that information may
6269     not be available.
6270    CUM is a variable of type CUMULATIVE_ARGS which gives info about
6271     the preceding args and about the function being called.
6272    NAMED is nonzero if this argument is a named parameter
6273     (otherwise it is an extra parameter matching an ellipsis).  */
6274
6275 static rtx
6276 function_arg_32 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6277                  enum machine_mode orig_mode, const_tree type,
6278                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
6279 {
6280   static bool warnedsse, warnedmmx;
6281
6282   /* Avoid the AL settings for the Unix64 ABI.  */
6283   if (mode == VOIDmode)
6284     return constm1_rtx;
6285
6286   switch (mode)
6287     {
6288     default:
6289       break;
6290
6291     case BLKmode:
6292       if (bytes < 0)
6293         break;
6294       /* FALLTHRU */
6295     case DImode:
6296     case SImode:
6297     case HImode:
6298     case QImode:
6299       if (words <= cum->nregs)
6300         {
6301           int regno = cum->regno;
6302
6303           /* Fastcall allocates the first two DWORD (SImode) or
6304             smaller arguments to ECX and EDX if it isn't an
6305             aggregate type .  */
6306           if (cum->fastcall)
6307             {
6308               if (mode == BLKmode
6309                   || mode == DImode
6310                   || (type && AGGREGATE_TYPE_P (type)))
6311                 break;
6312
6313               /* ECX not EAX is the first allocated register.  */
6314               if (regno == AX_REG)
6315                 regno = CX_REG;
6316             }
6317           return gen_rtx_REG (mode, regno);
6318         }
6319       break;
6320
6321     case DFmode:
6322       if (cum->float_in_sse < 2)
6323         break;
6324     case SFmode:
6325       if (cum->float_in_sse < 1)
6326         break;
6327       /* FALLTHRU */
6328     case TImode:
6329       /* In 32bit, we pass TImode in xmm registers.  */
6330     case V16QImode:
6331     case V8HImode:
6332     case V4SImode:
6333     case V2DImode:
6334     case V4SFmode:
6335     case V2DFmode:
6336       if (!type || !AGGREGATE_TYPE_P (type))
6337         {
6338           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
6339             {
6340               warnedsse = true;
6341               warning (0, "SSE vector argument without SSE enabled "
6342                        "changes the ABI");
6343             }
6344           if (cum->sse_nregs)
6345             return gen_reg_or_parallel (mode, orig_mode,
6346                                         cum->sse_regno + FIRST_SSE_REG);
6347         }
6348       break;
6349
6350     case OImode:
6351       /* OImode shouldn't be used directly.  */
6352       gcc_unreachable ();
6353
6354     case V8SFmode:
6355     case V8SImode:
6356     case V32QImode:
6357     case V16HImode:
6358     case V4DFmode:
6359     case V4DImode:
6360       if (!type || !AGGREGATE_TYPE_P (type))
6361         {
6362           if (cum->sse_nregs)
6363             return gen_reg_or_parallel (mode, orig_mode,
6364                                         cum->sse_regno + FIRST_SSE_REG);
6365         }
6366       break;
6367
6368     case V8QImode:
6369     case V4HImode:
6370     case V2SImode:
6371     case V2SFmode:
6372     case V1TImode:
6373     case V1DImode:
6374       if (!type || !AGGREGATE_TYPE_P (type))
6375         {
6376           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
6377             {
6378               warnedmmx = true;
6379               warning (0, "MMX vector argument without MMX enabled "
6380                        "changes the ABI");
6381             }
6382           if (cum->mmx_nregs)
6383             return gen_reg_or_parallel (mode, orig_mode,
6384                                         cum->mmx_regno + FIRST_MMX_REG);
6385         }
6386       break;
6387     }
6388
6389   return NULL_RTX;
6390 }
6391
6392 static rtx
6393 function_arg_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6394                  enum machine_mode orig_mode, const_tree type, bool named)
6395 {
6396   /* Handle a hidden AL argument containing number of registers
6397      for varargs x86-64 functions.  */
6398   if (mode == VOIDmode)
6399     return GEN_INT (cum->maybe_vaarg
6400                     ? (cum->sse_nregs < 0
6401                        ? X86_64_SSE_REGPARM_MAX
6402                        : cum->sse_regno)
6403                     : -1);
6404
6405   switch (mode)
6406     {
6407     default:
6408       break;
6409
6410     case V8SFmode:
6411     case V8SImode:
6412     case V32QImode:
6413     case V16HImode:
6414     case V4DFmode:
6415     case V4DImode:
6416       /* Unnamed 256bit vector mode parameters are passed on stack.  */
6417       if (!named)
6418         return NULL;
6419       break;
6420     }
6421
6422   return construct_container (mode, orig_mode, type, 0, cum->nregs,
6423                               cum->sse_nregs,
6424                               &x86_64_int_parameter_registers [cum->regno],
6425                               cum->sse_regno);
6426 }
6427
6428 static rtx
6429 function_arg_ms_64 (const CUMULATIVE_ARGS *cum, enum machine_mode mode,
6430                     enum machine_mode orig_mode, bool named,
6431                     HOST_WIDE_INT bytes)
6432 {
6433   unsigned int regno;
6434
6435   /* We need to add clobber for MS_ABI->SYSV ABI calls in expand_call.
6436      We use value of -2 to specify that current function call is MSABI.  */
6437   if (mode == VOIDmode)
6438     return GEN_INT (-2);
6439
6440   /* If we've run out of registers, it goes on the stack.  */
6441   if (cum->nregs == 0)
6442     return NULL_RTX;
6443
6444   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
6445
6446   /* Only floating point modes are passed in anything but integer regs.  */
6447   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
6448     {
6449       if (named)
6450         regno = cum->regno + FIRST_SSE_REG;
6451       else
6452         {
6453           rtx t1, t2;
6454
6455           /* Unnamed floating parameters are passed in both the
6456              SSE and integer registers.  */
6457           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
6458           t2 = gen_rtx_REG (mode, regno);
6459           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
6460           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
6461           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
6462         }
6463     }
6464   /* Handle aggregated types passed in register.  */
6465   if (orig_mode == BLKmode)
6466     {
6467       if (bytes > 0 && bytes <= 8)
6468         mode = (bytes > 4 ? DImode : SImode);
6469       if (mode == BLKmode)
6470         mode = DImode;
6471     }
6472
6473   return gen_reg_or_parallel (mode, orig_mode, regno);
6474 }
6475
6476 /* Return where to put the arguments to a function.
6477    Return zero to push the argument on the stack, or a hard register in which to store the argument.
6478
6479    MODE is the argument's machine mode.  TYPE is the data type of the
6480    argument.  It is null for libcalls where that information may not be
6481    available.  CUM gives information about the preceding args and about
6482    the function being called.  NAMED is nonzero if this argument is a
6483    named parameter (otherwise it is an extra parameter matching an
6484    ellipsis).  */
6485
6486 static rtx
6487 ix86_function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
6488                    const_tree type, bool named)
6489 {
6490   enum machine_mode mode = omode;
6491   HOST_WIDE_INT bytes, words;
6492
6493   if (mode == BLKmode)
6494     bytes = int_size_in_bytes (type);
6495   else
6496     bytes = GET_MODE_SIZE (mode);
6497   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
6498
6499   /* To simplify the code below, represent vector types with a vector mode
6500      even if MMX/SSE are not active.  */
6501   if (type && TREE_CODE (type) == VECTOR_TYPE)
6502     mode = type_natural_mode (type, cum);
6503
6504   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6505     return function_arg_ms_64 (cum, mode, omode, named, bytes);
6506   else if (TARGET_64BIT)
6507     return function_arg_64 (cum, mode, omode, type, named);
6508   else
6509     return function_arg_32 (cum, mode, omode, type, bytes, words);
6510 }
6511
6512 /* A C expression that indicates when an argument must be passed by
6513    reference.  If nonzero for an argument, a copy of that argument is
6514    made in memory and a pointer to the argument is passed instead of
6515    the argument itself.  The pointer is passed in whatever way is
6516    appropriate for passing a pointer to that type.  */
6517
6518 static bool
6519 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
6520                         enum machine_mode mode ATTRIBUTE_UNUSED,
6521                         const_tree type, bool named ATTRIBUTE_UNUSED)
6522 {
6523   /* See Windows x64 Software Convention.  */
6524   if (TARGET_64BIT && (cum ? cum->call_abi : ix86_abi) == MS_ABI)
6525     {
6526       int msize = (int) GET_MODE_SIZE (mode);
6527       if (type)
6528         {
6529           /* Arrays are passed by reference.  */
6530           if (TREE_CODE (type) == ARRAY_TYPE)
6531             return true;
6532
6533           if (AGGREGATE_TYPE_P (type))
6534             {
6535               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
6536                  are passed by reference.  */
6537               msize = int_size_in_bytes (type);
6538             }
6539         }
6540
6541       /* __m128 is passed by reference.  */
6542       switch (msize) {
6543       case 1: case 2: case 4: case 8:
6544         break;
6545       default:
6546         return true;
6547       }
6548     }
6549   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
6550     return 1;
6551
6552   return 0;
6553 }
6554
6555 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
6556    ABI.  */
6557 static bool
6558 contains_aligned_value_p (const_tree type)
6559 {
6560   enum machine_mode mode = TYPE_MODE (type);
6561   if (((TARGET_SSE && SSE_REG_MODE_P (mode))
6562        || mode == TDmode
6563        || mode == TFmode
6564        || mode == TCmode)
6565       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
6566     return true;
6567   if (TYPE_ALIGN (type) < 128)
6568     return false;
6569
6570   if (AGGREGATE_TYPE_P (type))
6571     {
6572       /* Walk the aggregates recursively.  */
6573       switch (TREE_CODE (type))
6574         {
6575         case RECORD_TYPE:
6576         case UNION_TYPE:
6577         case QUAL_UNION_TYPE:
6578           {
6579             tree field;
6580
6581             /* Walk all the structure fields.  */
6582             for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
6583               {
6584                 if (TREE_CODE (field) == FIELD_DECL
6585                     && contains_aligned_value_p (TREE_TYPE (field)))
6586                   return true;
6587               }
6588             break;
6589           }
6590
6591         case ARRAY_TYPE:
6592           /* Just for use if some languages passes arrays by value.  */
6593           if (contains_aligned_value_p (TREE_TYPE (type)))
6594             return true;
6595           break;
6596
6597         default:
6598           gcc_unreachable ();
6599         }
6600     }
6601   return false;
6602 }
6603
6604 /* Gives the alignment boundary, in bits, of an argument with the
6605    specified mode and type.  */
6606
6607 int
6608 ix86_function_arg_boundary (enum machine_mode mode, const_tree type)
6609 {
6610   int align;
6611   if (type)
6612     {
6613       /* Since the main variant type is used for call, we convert it to
6614          the main variant type.  */
6615       type = TYPE_MAIN_VARIANT (type);
6616       align = TYPE_ALIGN (type);
6617     }
6618   else
6619     align = GET_MODE_ALIGNMENT (mode);
6620   if (align < PARM_BOUNDARY)
6621     align = PARM_BOUNDARY;
6622   /* In 32bit, only _Decimal128 and __float128 are aligned to their
6623      natural boundaries.  */
6624   if (!TARGET_64BIT && mode != TDmode && mode != TFmode)
6625     {
6626       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
6627          make an exception for SSE modes since these require 128bit
6628          alignment.
6629
6630          The handling here differs from field_alignment.  ICC aligns MMX
6631          arguments to 4 byte boundaries, while structure fields are aligned
6632          to 8 byte boundaries.  */
6633       if (!type)
6634         {
6635           if (!(TARGET_SSE && SSE_REG_MODE_P (mode)))
6636             align = PARM_BOUNDARY;
6637         }
6638       else
6639         {
6640           if (!contains_aligned_value_p (type))
6641             align = PARM_BOUNDARY;
6642         }
6643     }
6644   if (align > BIGGEST_ALIGNMENT)
6645     align = BIGGEST_ALIGNMENT;
6646   return align;
6647 }
6648
6649 /* Return true if N is a possible register number of function value.  */
6650
6651 static bool
6652 ix86_function_value_regno_p (const unsigned int regno)
6653 {
6654   switch (regno)
6655     {
6656     case 0:
6657       return true;
6658
6659     case FIRST_FLOAT_REG:
6660       /* TODO: The function should depend on current function ABI but
6661        builtins.c would need updating then. Therefore we use the
6662        default ABI.  */
6663       if (TARGET_64BIT && ix86_abi == MS_ABI)
6664         return false;
6665       return TARGET_FLOAT_RETURNS_IN_80387;
6666
6667     case FIRST_SSE_REG:
6668       return TARGET_SSE;
6669
6670     case FIRST_MMX_REG:
6671       if (TARGET_MACHO || TARGET_64BIT)
6672         return false;
6673       return TARGET_MMX;
6674     }
6675
6676   return false;
6677 }
6678
6679 /* Define how to find the value returned by a function.
6680    VALTYPE is the data type of the value (as a tree).
6681    If the precise function being called is known, FUNC is its FUNCTION_DECL;
6682    otherwise, FUNC is 0.  */
6683
6684 static rtx
6685 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
6686                    const_tree fntype, const_tree fn)
6687 {
6688   unsigned int regno;
6689
6690   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
6691      we normally prevent this case when mmx is not available.  However
6692      some ABIs may require the result to be returned like DImode.  */
6693   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6694     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
6695
6696   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
6697      we prevent this case when sse is not available.  However some ABIs
6698      may require the result to be returned like integer TImode.  */
6699   else if (mode == TImode
6700            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6701     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
6702
6703   /* 32-byte vector modes in %ymm0.   */
6704   else if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 32)
6705     regno = TARGET_AVX ? FIRST_SSE_REG : 0;
6706
6707   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
6708   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
6709     regno = FIRST_FLOAT_REG;
6710   else
6711     /* Most things go in %eax.  */
6712     regno = AX_REG;
6713
6714   /* Override FP return register with %xmm0 for local functions when
6715      SSE math is enabled or for functions with sseregparm attribute.  */
6716   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
6717     {
6718       int sse_level = ix86_function_sseregparm (fntype, fn, false);
6719       if ((sse_level >= 1 && mode == SFmode)
6720           || (sse_level == 2 && mode == DFmode))
6721         regno = FIRST_SSE_REG;
6722     }
6723
6724   /* OImode shouldn't be used directly.  */
6725   gcc_assert (mode != OImode);
6726
6727   return gen_rtx_REG (orig_mode, regno);
6728 }
6729
6730 static rtx
6731 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
6732                    const_tree valtype)
6733 {
6734   rtx ret;
6735
6736   /* Handle libcalls, which don't provide a type node.  */
6737   if (valtype == NULL)
6738     {
6739       switch (mode)
6740         {
6741         case SFmode:
6742         case SCmode:
6743         case DFmode:
6744         case DCmode:
6745         case TFmode:
6746         case SDmode:
6747         case DDmode:
6748         case TDmode:
6749           return gen_rtx_REG (mode, FIRST_SSE_REG);
6750         case XFmode:
6751         case XCmode:
6752           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
6753         case TCmode:
6754           return NULL;
6755         default:
6756           return gen_rtx_REG (mode, AX_REG);
6757         }
6758     }
6759
6760   ret = construct_container (mode, orig_mode, valtype, 1,
6761                              X86_64_REGPARM_MAX, X86_64_SSE_REGPARM_MAX,
6762                              x86_64_int_return_registers, 0);
6763
6764   /* For zero sized structures, construct_container returns NULL, but we
6765      need to keep rest of compiler happy by returning meaningful value.  */
6766   if (!ret)
6767     ret = gen_rtx_REG (orig_mode, AX_REG);
6768
6769   return ret;
6770 }
6771
6772 static rtx
6773 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
6774 {
6775   unsigned int regno = AX_REG;
6776
6777   if (TARGET_SSE)
6778     {
6779       switch (GET_MODE_SIZE (mode))
6780         {
6781         case 16:
6782           if((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6783              && !COMPLEX_MODE_P (mode))
6784             regno = FIRST_SSE_REG;
6785           break;
6786         case 8:
6787         case 4:
6788           if (mode == SFmode || mode == DFmode)
6789             regno = FIRST_SSE_REG;
6790           break;
6791         default:
6792           break;
6793         }
6794     }
6795   return gen_rtx_REG (orig_mode, regno);
6796 }
6797
6798 static rtx
6799 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
6800                        enum machine_mode orig_mode, enum machine_mode mode)
6801 {
6802   const_tree fn, fntype;
6803
6804   fn = NULL_TREE;
6805   if (fntype_or_decl && DECL_P (fntype_or_decl))
6806     fn = fntype_or_decl;
6807   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
6808
6809   if (TARGET_64BIT && ix86_function_type_abi (fntype) == MS_ABI)
6810     return function_value_ms_64 (orig_mode, mode);
6811   else if (TARGET_64BIT)
6812     return function_value_64 (orig_mode, mode, valtype);
6813   else
6814     return function_value_32 (orig_mode, mode, fntype, fn);
6815 }
6816
6817 static rtx
6818 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
6819                      bool outgoing ATTRIBUTE_UNUSED)
6820 {
6821   enum machine_mode mode, orig_mode;
6822
6823   orig_mode = TYPE_MODE (valtype);
6824   mode = type_natural_mode (valtype, NULL);
6825   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
6826 }
6827
6828 rtx
6829 ix86_libcall_value (enum machine_mode mode)
6830 {
6831   return ix86_function_value_1 (NULL, NULL, mode, mode);
6832 }
6833
6834 /* Return true iff type is returned in memory.  */
6835
6836 static bool ATTRIBUTE_UNUSED
6837 return_in_memory_32 (const_tree type, enum machine_mode mode)
6838 {
6839   HOST_WIDE_INT size;
6840
6841   if (mode == BLKmode)
6842     return true;
6843
6844   size = int_size_in_bytes (type);
6845
6846   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
6847     return false;
6848
6849   if (VECTOR_MODE_P (mode) || mode == TImode)
6850     {
6851       /* User-created vectors small enough to fit in EAX.  */
6852       if (size < 8)
6853         return false;
6854
6855       /* MMX/3dNow values are returned in MM0,
6856          except when it doesn't exits or the ABI prescribes otherwise.  */
6857       if (size == 8)
6858         return !TARGET_MMX || TARGET_VECT8_RETURNS;
6859
6860       /* SSE values are returned in XMM0, except when it doesn't exist.  */
6861       if (size == 16)
6862         return !TARGET_SSE;
6863
6864       /* AVX values are returned in YMM0, except when it doesn't exist.  */
6865       if (size == 32)
6866         return !TARGET_AVX;
6867     }
6868
6869   if (mode == XFmode)
6870     return false;
6871
6872   if (size > 12)
6873     return true;
6874
6875   /* OImode shouldn't be used directly.  */
6876   gcc_assert (mode != OImode);
6877
6878   return false;
6879 }
6880
6881 static bool ATTRIBUTE_UNUSED
6882 return_in_memory_64 (const_tree type, enum machine_mode mode)
6883 {
6884   int needed_intregs, needed_sseregs;
6885   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
6886 }
6887
6888 static bool ATTRIBUTE_UNUSED
6889 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
6890 {
6891   HOST_WIDE_INT size = int_size_in_bytes (type);
6892
6893   /* __m128 is returned in xmm0.  */
6894   if ((SCALAR_INT_MODE_P (mode) || VECTOR_MODE_P (mode))
6895       && !COMPLEX_MODE_P (mode) && (GET_MODE_SIZE (mode) == 16 || size == 16))
6896     return false;
6897
6898   /* Otherwise, the size must be exactly in [1248]. */
6899   return size != 1 && size != 2 && size != 4 && size != 8;
6900 }
6901
6902 static bool
6903 ix86_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6904 {
6905 #ifdef SUBTARGET_RETURN_IN_MEMORY
6906   return SUBTARGET_RETURN_IN_MEMORY (type, fntype);
6907 #else
6908   const enum machine_mode mode = type_natural_mode (type, NULL);
6909
6910   if (TARGET_64BIT)
6911     {
6912       if (ix86_function_type_abi (fntype) == MS_ABI)
6913         return return_in_memory_ms_64 (type, mode);
6914       else
6915         return return_in_memory_64 (type, mode);
6916     }
6917   else
6918     return return_in_memory_32 (type, mode);
6919 #endif
6920 }
6921
6922 /* When returning SSE vector types, we have a choice of either
6923      (1) being abi incompatible with a -march switch, or
6924      (2) generating an error.
6925    Given no good solution, I think the safest thing is one warning.
6926    The user won't be able to use -Werror, but....
6927
6928    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
6929    called in response to actually generating a caller or callee that
6930    uses such a type.  As opposed to TARGET_RETURN_IN_MEMORY, which is called
6931    via aggregate_value_p for general type probing from tree-ssa.  */
6932
6933 static rtx
6934 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
6935 {
6936   static bool warnedsse, warnedmmx;
6937
6938   if (!TARGET_64BIT && type)
6939     {
6940       /* Look at the return type of the function, not the function type.  */
6941       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
6942
6943       if (!TARGET_SSE && !warnedsse)
6944         {
6945           if (mode == TImode
6946               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
6947             {
6948               warnedsse = true;
6949               warning (0, "SSE vector return without SSE enabled "
6950                        "changes the ABI");
6951             }
6952         }
6953
6954       if (!TARGET_MMX && !warnedmmx)
6955         {
6956           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
6957             {
6958               warnedmmx = true;
6959               warning (0, "MMX vector return without MMX enabled "
6960                        "changes the ABI");
6961             }
6962         }
6963     }
6964
6965   return NULL;
6966 }
6967
6968 \f
6969 /* Create the va_list data type.  */
6970
6971 /* Returns the calling convention specific va_list date type.
6972    The argument ABI can be DEFAULT_ABI, MS_ABI, or SYSV_ABI.  */
6973
6974 static tree
6975 ix86_build_builtin_va_list_abi (enum calling_abi abi)
6976 {
6977   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
6978
6979   /* For i386 we use plain pointer to argument area.  */
6980   if (!TARGET_64BIT || abi == MS_ABI)
6981     return build_pointer_type (char_type_node);
6982
6983   record = lang_hooks.types.make_type (RECORD_TYPE);
6984   type_decl = build_decl (BUILTINS_LOCATION,
6985                           TYPE_DECL, get_identifier ("__va_list_tag"), record);
6986
6987   f_gpr = build_decl (BUILTINS_LOCATION,
6988                       FIELD_DECL, get_identifier ("gp_offset"),
6989                       unsigned_type_node);
6990   f_fpr = build_decl (BUILTINS_LOCATION,
6991                       FIELD_DECL, get_identifier ("fp_offset"),
6992                       unsigned_type_node);
6993   f_ovf = build_decl (BUILTINS_LOCATION,
6994                       FIELD_DECL, get_identifier ("overflow_arg_area"),
6995                       ptr_type_node);
6996   f_sav = build_decl (BUILTINS_LOCATION,
6997                       FIELD_DECL, get_identifier ("reg_save_area"),
6998                       ptr_type_node);
6999
7000   va_list_gpr_counter_field = f_gpr;
7001   va_list_fpr_counter_field = f_fpr;
7002
7003   DECL_FIELD_CONTEXT (f_gpr) = record;
7004   DECL_FIELD_CONTEXT (f_fpr) = record;
7005   DECL_FIELD_CONTEXT (f_ovf) = record;
7006   DECL_FIELD_CONTEXT (f_sav) = record;
7007
7008   TREE_CHAIN (record) = type_decl;
7009   TYPE_NAME (record) = type_decl;
7010   TYPE_FIELDS (record) = f_gpr;
7011   DECL_CHAIN (f_gpr) = f_fpr;
7012   DECL_CHAIN (f_fpr) = f_ovf;
7013   DECL_CHAIN (f_ovf) = f_sav;
7014
7015   layout_type (record);
7016
7017   /* The correct type is an array type of one element.  */
7018   return build_array_type (record, build_index_type (size_zero_node));
7019 }
7020
7021 /* Setup the builtin va_list data type and for 64-bit the additional
7022    calling convention specific va_list data types.  */
7023
7024 static tree
7025 ix86_build_builtin_va_list (void)
7026 {
7027   tree ret = ix86_build_builtin_va_list_abi (ix86_abi);
7028
7029   /* Initialize abi specific va_list builtin types.  */
7030   if (TARGET_64BIT)
7031     {
7032       tree t;
7033       if (ix86_abi == MS_ABI)
7034         {
7035           t = ix86_build_builtin_va_list_abi (SYSV_ABI);
7036           if (TREE_CODE (t) != RECORD_TYPE)
7037             t = build_variant_type_copy (t);
7038           sysv_va_list_type_node = t;
7039         }
7040       else
7041         {
7042           t = ret;
7043           if (TREE_CODE (t) != RECORD_TYPE)
7044             t = build_variant_type_copy (t);
7045           sysv_va_list_type_node = t;
7046         }
7047       if (ix86_abi != MS_ABI)
7048         {
7049           t = ix86_build_builtin_va_list_abi (MS_ABI);
7050           if (TREE_CODE (t) != RECORD_TYPE)
7051             t = build_variant_type_copy (t);
7052           ms_va_list_type_node = t;
7053         }
7054       else
7055         {
7056           t = ret;
7057           if (TREE_CODE (t) != RECORD_TYPE)
7058             t = build_variant_type_copy (t);
7059           ms_va_list_type_node = t;
7060         }
7061     }
7062
7063   return ret;
7064 }
7065
7066 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
7067
7068 static void
7069 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
7070 {
7071   rtx save_area, mem;
7072   alias_set_type set;
7073   int i, max;
7074
7075   /* GPR size of varargs save area.  */
7076   if (cfun->va_list_gpr_size)
7077     ix86_varargs_gpr_size = X86_64_REGPARM_MAX * UNITS_PER_WORD;
7078   else
7079     ix86_varargs_gpr_size = 0;
7080
7081   /* FPR size of varargs save area.  We don't need it if we don't pass
7082      anything in SSE registers.  */
7083   if (TARGET_SSE && cfun->va_list_fpr_size)
7084     ix86_varargs_fpr_size = X86_64_SSE_REGPARM_MAX * 16;
7085   else
7086     ix86_varargs_fpr_size = 0;
7087
7088   if (! ix86_varargs_gpr_size && ! ix86_varargs_fpr_size)
7089     return;
7090
7091   save_area = frame_pointer_rtx;
7092   set = get_varargs_alias_set ();
7093
7094   max = cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
7095   if (max > X86_64_REGPARM_MAX)
7096     max = X86_64_REGPARM_MAX;
7097
7098   for (i = cum->regno; i < max; i++)
7099     {
7100       mem = gen_rtx_MEM (Pmode,
7101                          plus_constant (save_area, i * UNITS_PER_WORD));
7102       MEM_NOTRAP_P (mem) = 1;
7103       set_mem_alias_set (mem, set);
7104       emit_move_insn (mem, gen_rtx_REG (Pmode,
7105                                         x86_64_int_parameter_registers[i]));
7106     }
7107
7108   if (ix86_varargs_fpr_size)
7109     {
7110       enum machine_mode smode;
7111       rtx label, test;
7112
7113       /* Now emit code to save SSE registers.  The AX parameter contains number
7114          of SSE parameter registers used to call this function, though all we
7115          actually check here is the zero/non-zero status.  */
7116
7117       label = gen_label_rtx ();
7118       test = gen_rtx_EQ (VOIDmode, gen_rtx_REG (QImode, AX_REG), const0_rtx);
7119       emit_jump_insn (gen_cbranchqi4 (test, XEXP (test, 0), XEXP (test, 1),
7120                                       label));
7121
7122       /* ??? If !TARGET_SSE_TYPELESS_STORES, would we perform better if
7123          we used movdqa (i.e. TImode) instead?  Perhaps even better would
7124          be if we could determine the real mode of the data, via a hook
7125          into pass_stdarg.  Ignore all that for now.  */
7126       smode = V4SFmode;
7127       if (crtl->stack_alignment_needed < GET_MODE_ALIGNMENT (smode))
7128         crtl->stack_alignment_needed = GET_MODE_ALIGNMENT (smode);
7129
7130       max = cum->sse_regno + cfun->va_list_fpr_size / 16;
7131       if (max > X86_64_SSE_REGPARM_MAX)
7132         max = X86_64_SSE_REGPARM_MAX;
7133
7134       for (i = cum->sse_regno; i < max; ++i)
7135         {
7136           mem = plus_constant (save_area, i * 16 + ix86_varargs_gpr_size);
7137           mem = gen_rtx_MEM (smode, mem);
7138           MEM_NOTRAP_P (mem) = 1;
7139           set_mem_alias_set (mem, set);
7140           set_mem_align (mem, GET_MODE_ALIGNMENT (smode));
7141
7142           emit_move_insn (mem, gen_rtx_REG (smode, SSE_REGNO (i)));
7143         }
7144
7145       emit_label (label);
7146     }
7147 }
7148
7149 static void
7150 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
7151 {
7152   alias_set_type set = get_varargs_alias_set ();
7153   int i;
7154
7155   for (i = cum->regno; i < X86_64_MS_REGPARM_MAX; i++)
7156     {
7157       rtx reg, mem;
7158
7159       mem = gen_rtx_MEM (Pmode,
7160                          plus_constant (virtual_incoming_args_rtx,
7161                                         i * UNITS_PER_WORD));
7162       MEM_NOTRAP_P (mem) = 1;
7163       set_mem_alias_set (mem, set);
7164
7165       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
7166       emit_move_insn (mem, reg);
7167     }
7168 }
7169
7170 static void
7171 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
7172                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
7173                              int no_rtl)
7174 {
7175   CUMULATIVE_ARGS next_cum;
7176   tree fntype;
7177
7178   /* This argument doesn't appear to be used anymore.  Which is good,
7179      because the old code here didn't suppress rtl generation.  */
7180   gcc_assert (!no_rtl);
7181
7182   if (!TARGET_64BIT)
7183     return;
7184
7185   fntype = TREE_TYPE (current_function_decl);
7186
7187   /* For varargs, we do not want to skip the dummy va_dcl argument.
7188      For stdargs, we do want to skip the last named argument.  */
7189   next_cum = *cum;
7190   if (stdarg_p (fntype))
7191     ix86_function_arg_advance (&next_cum, mode, type, true);
7192
7193   if (cum->call_abi == MS_ABI)
7194     setup_incoming_varargs_ms_64 (&next_cum);
7195   else
7196     setup_incoming_varargs_64 (&next_cum);
7197 }
7198
7199 /* Checks if TYPE is of kind va_list char *.  */
7200
7201 static bool
7202 is_va_list_char_pointer (tree type)
7203 {
7204   tree canonic;
7205
7206   /* For 32-bit it is always true.  */
7207   if (!TARGET_64BIT)
7208     return true;
7209   canonic = ix86_canonical_va_list_type (type);
7210   return (canonic == ms_va_list_type_node
7211           || (ix86_abi == MS_ABI && canonic == va_list_type_node));
7212 }
7213
7214 /* Implement va_start.  */
7215
7216 static void
7217 ix86_va_start (tree valist, rtx nextarg)
7218 {
7219   HOST_WIDE_INT words, n_gpr, n_fpr;
7220   tree f_gpr, f_fpr, f_ovf, f_sav;
7221   tree gpr, fpr, ovf, sav, t;
7222   tree type;
7223
7224   rtx ovf_rtx;
7225
7226   if (flag_split_stack
7227       && cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7228     {
7229       unsigned int scratch_regno;
7230
7231       /* When we are splitting the stack, we can't refer to the stack
7232          arguments using internal_arg_pointer, because they may be on
7233          the old stack.  The split stack prologue will arrange to
7234          leave a pointer to the old stack arguments in a scratch
7235          register, which we here copy to a pseudo-register.  The split
7236          stack prologue can't set the pseudo-register directly because
7237          it (the prologue) runs before any registers have been saved.  */
7238
7239       scratch_regno = split_stack_prologue_scratch_regno ();
7240       if (scratch_regno != INVALID_REGNUM)
7241         {
7242           rtx reg, seq;
7243
7244           reg = gen_reg_rtx (Pmode);
7245           cfun->machine->split_stack_varargs_pointer = reg;
7246
7247           start_sequence ();
7248           emit_move_insn (reg, gen_rtx_REG (Pmode, scratch_regno));
7249           seq = get_insns ();
7250           end_sequence ();
7251
7252           push_topmost_sequence ();
7253           emit_insn_after (seq, entry_of_function ());
7254           pop_topmost_sequence ();
7255         }
7256     }
7257
7258   /* Only 64bit target needs something special.  */
7259   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7260     {
7261       if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7262         std_expand_builtin_va_start (valist, nextarg);
7263       else
7264         {
7265           rtx va_r, next;
7266
7267           va_r = expand_expr (valist, NULL_RTX, VOIDmode, EXPAND_WRITE);
7268           next = expand_binop (ptr_mode, add_optab,
7269                                cfun->machine->split_stack_varargs_pointer,
7270                                crtl->args.arg_offset_rtx,
7271                                NULL_RTX, 0, OPTAB_LIB_WIDEN);
7272           convert_move (va_r, next, 0);
7273         }
7274       return;
7275     }
7276
7277   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7278   f_fpr = DECL_CHAIN (f_gpr);
7279   f_ovf = DECL_CHAIN (f_fpr);
7280   f_sav = DECL_CHAIN (f_ovf);
7281
7282   valist = build_simple_mem_ref (valist);
7283   TREE_TYPE (valist) = TREE_TYPE (sysv_va_list_type_node);
7284   /* The following should be folded into the MEM_REF offset.  */
7285   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), unshare_expr (valist),
7286                 f_gpr, NULL_TREE);
7287   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), unshare_expr (valist),
7288                 f_fpr, NULL_TREE);
7289   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), unshare_expr (valist),
7290                 f_ovf, NULL_TREE);
7291   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), unshare_expr (valist),
7292                 f_sav, NULL_TREE);
7293
7294   /* Count number of gp and fp argument registers used.  */
7295   words = crtl->args.info.words;
7296   n_gpr = crtl->args.info.regno;
7297   n_fpr = crtl->args.info.sse_regno;
7298
7299   if (cfun->va_list_gpr_size)
7300     {
7301       type = TREE_TYPE (gpr);
7302       t = build2 (MODIFY_EXPR, type,
7303                   gpr, build_int_cst (type, n_gpr * 8));
7304       TREE_SIDE_EFFECTS (t) = 1;
7305       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7306     }
7307
7308   if (TARGET_SSE && cfun->va_list_fpr_size)
7309     {
7310       type = TREE_TYPE (fpr);
7311       t = build2 (MODIFY_EXPR, type, fpr,
7312                   build_int_cst (type, n_fpr * 16 + 8*X86_64_REGPARM_MAX));
7313       TREE_SIDE_EFFECTS (t) = 1;
7314       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7315     }
7316
7317   /* Find the overflow area.  */
7318   type = TREE_TYPE (ovf);
7319   if (cfun->machine->split_stack_varargs_pointer == NULL_RTX)
7320     ovf_rtx = crtl->args.internal_arg_pointer;
7321   else
7322     ovf_rtx = cfun->machine->split_stack_varargs_pointer;
7323   t = make_tree (type, ovf_rtx);
7324   if (words != 0)
7325     t = build2 (POINTER_PLUS_EXPR, type, t,
7326                 size_int (words * UNITS_PER_WORD));
7327   t = build2 (MODIFY_EXPR, type, ovf, t);
7328   TREE_SIDE_EFFECTS (t) = 1;
7329   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7330
7331   if (ix86_varargs_gpr_size || ix86_varargs_fpr_size)
7332     {
7333       /* Find the register save area.
7334          Prologue of the function save it right above stack frame.  */
7335       type = TREE_TYPE (sav);
7336       t = make_tree (type, frame_pointer_rtx);
7337       if (!ix86_varargs_gpr_size)
7338         t = build2 (POINTER_PLUS_EXPR, type, t,
7339                     size_int (-8 * X86_64_REGPARM_MAX));
7340       t = build2 (MODIFY_EXPR, type, sav, t);
7341       TREE_SIDE_EFFECTS (t) = 1;
7342       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
7343     }
7344 }
7345
7346 /* Implement va_arg.  */
7347
7348 static tree
7349 ix86_gimplify_va_arg (tree valist, tree type, gimple_seq *pre_p,
7350                       gimple_seq *post_p)
7351 {
7352   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
7353   tree f_gpr, f_fpr, f_ovf, f_sav;
7354   tree gpr, fpr, ovf, sav, t;
7355   int size, rsize;
7356   tree lab_false, lab_over = NULL_TREE;
7357   tree addr, t2;
7358   rtx container;
7359   int indirect_p = 0;
7360   tree ptrtype;
7361   enum machine_mode nat_mode;
7362   unsigned int arg_boundary;
7363
7364   /* Only 64bit target needs something special.  */
7365   if (!TARGET_64BIT || is_va_list_char_pointer (TREE_TYPE (valist)))
7366     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
7367
7368   f_gpr = TYPE_FIELDS (TREE_TYPE (sysv_va_list_type_node));
7369   f_fpr = DECL_CHAIN (f_gpr);
7370   f_ovf = DECL_CHAIN (f_fpr);
7371   f_sav = DECL_CHAIN (f_ovf);
7372
7373   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr),
7374                 build_va_arg_indirect_ref (valist), f_gpr, NULL_TREE);
7375   valist = build_va_arg_indirect_ref (valist);
7376   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
7377   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
7378   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
7379
7380   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
7381   if (indirect_p)
7382     type = build_pointer_type (type);
7383   size = int_size_in_bytes (type);
7384   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
7385
7386   nat_mode = type_natural_mode (type, NULL);
7387   switch (nat_mode)
7388     {
7389     case V8SFmode:
7390     case V8SImode:
7391     case V32QImode:
7392     case V16HImode:
7393     case V4DFmode:
7394     case V4DImode:
7395       /* Unnamed 256bit vector mode parameters are passed on stack.  */
7396       if (ix86_cfun_abi () == SYSV_ABI)
7397         {
7398           container = NULL;
7399           break;
7400         }
7401
7402     default:
7403       container = construct_container (nat_mode, TYPE_MODE (type),
7404                                        type, 0, X86_64_REGPARM_MAX,
7405                                        X86_64_SSE_REGPARM_MAX, intreg,
7406                                        0);
7407       break;
7408     }
7409
7410   /* Pull the value out of the saved registers.  */
7411
7412   addr = create_tmp_var (ptr_type_node, "addr");
7413
7414   if (container)
7415     {
7416       int needed_intregs, needed_sseregs;
7417       bool need_temp;
7418       tree int_addr, sse_addr;
7419
7420       lab_false = create_artificial_label (UNKNOWN_LOCATION);
7421       lab_over = create_artificial_label (UNKNOWN_LOCATION);
7422
7423       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
7424
7425       need_temp = (!REG_P (container)
7426                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
7427                        || TYPE_ALIGN (type) > 128));
7428
7429       /* In case we are passing structure, verify that it is consecutive block
7430          on the register save area.  If not we need to do moves.  */
7431       if (!need_temp && !REG_P (container))
7432         {
7433           /* Verify that all registers are strictly consecutive  */
7434           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
7435             {
7436               int i;
7437
7438               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7439                 {
7440                   rtx slot = XVECEXP (container, 0, i);
7441                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
7442                       || INTVAL (XEXP (slot, 1)) != i * 16)
7443                     need_temp = 1;
7444                 }
7445             }
7446           else
7447             {
7448               int i;
7449
7450               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
7451                 {
7452                   rtx slot = XVECEXP (container, 0, i);
7453                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
7454                       || INTVAL (XEXP (slot, 1)) != i * 8)
7455                     need_temp = 1;
7456                 }
7457             }
7458         }
7459       if (!need_temp)
7460         {
7461           int_addr = addr;
7462           sse_addr = addr;
7463         }
7464       else
7465         {
7466           int_addr = create_tmp_var (ptr_type_node, "int_addr");
7467           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
7468         }
7469
7470       /* First ensure that we fit completely in registers.  */
7471       if (needed_intregs)
7472         {
7473           t = build_int_cst (TREE_TYPE (gpr),
7474                              (X86_64_REGPARM_MAX - needed_intregs + 1) * 8);
7475           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
7476           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7477           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7478           gimplify_and_add (t, pre_p);
7479         }
7480       if (needed_sseregs)
7481         {
7482           t = build_int_cst (TREE_TYPE (fpr),
7483                              (X86_64_SSE_REGPARM_MAX - needed_sseregs + 1) * 16
7484                              + X86_64_REGPARM_MAX * 8);
7485           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
7486           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
7487           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
7488           gimplify_and_add (t, pre_p);
7489         }
7490
7491       /* Compute index to start of area used for integer regs.  */
7492       if (needed_intregs)
7493         {
7494           /* int_addr = gpr + sav; */
7495           t = fold_convert (sizetype, gpr);
7496           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7497           gimplify_assign (int_addr, t, pre_p);
7498         }
7499       if (needed_sseregs)
7500         {
7501           /* sse_addr = fpr + sav; */
7502           t = fold_convert (sizetype, fpr);
7503           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
7504           gimplify_assign (sse_addr, t, pre_p);
7505         }
7506       if (need_temp)
7507         {
7508           int i, prev_size = 0;
7509           tree temp = create_tmp_var (type, "va_arg_tmp");
7510
7511           /* addr = &temp; */
7512           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
7513           gimplify_assign (addr, t, pre_p);
7514
7515           for (i = 0; i < XVECLEN (container, 0); i++)
7516             {
7517               rtx slot = XVECEXP (container, 0, i);
7518               rtx reg = XEXP (slot, 0);
7519               enum machine_mode mode = GET_MODE (reg);
7520               tree piece_type;
7521               tree addr_type;
7522               tree daddr_type;
7523               tree src_addr, src;
7524               int src_offset;
7525               tree dest_addr, dest;
7526               int cur_size = GET_MODE_SIZE (mode);
7527
7528               gcc_assert (prev_size <= INTVAL (XEXP (slot, 1)));
7529               prev_size = INTVAL (XEXP (slot, 1));
7530               if (prev_size + cur_size > size)
7531                 {
7532                   cur_size = size - prev_size;
7533                   mode = mode_for_size (cur_size * BITS_PER_UNIT, MODE_INT, 1);
7534                   if (mode == BLKmode)
7535                     mode = QImode;
7536                 }
7537               piece_type = lang_hooks.types.type_for_mode (mode, 1);
7538               if (mode == GET_MODE (reg))
7539                 addr_type = build_pointer_type (piece_type);
7540               else
7541                 addr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
7542                                                          true);
7543               daddr_type = build_pointer_type_for_mode (piece_type, ptr_mode,
7544                                                         true);
7545
7546               if (SSE_REGNO_P (REGNO (reg)))
7547                 {
7548                   src_addr = sse_addr;
7549                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
7550                 }
7551               else
7552                 {
7553                   src_addr = int_addr;
7554                   src_offset = REGNO (reg) * 8;
7555                 }
7556               src_addr = fold_convert (addr_type, src_addr);
7557               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
7558                                       size_int (src_offset));
7559
7560               dest_addr = fold_convert (daddr_type, addr);
7561               dest_addr = fold_build2 (POINTER_PLUS_EXPR, daddr_type, dest_addr,
7562                                        size_int (prev_size));
7563               if (cur_size == GET_MODE_SIZE (mode))
7564                 {
7565                   src = build_va_arg_indirect_ref (src_addr);
7566                   dest = build_va_arg_indirect_ref (dest_addr);
7567
7568                   gimplify_assign (dest, src, pre_p);
7569                 }
7570               else
7571                 {
7572                   tree copy
7573                     = build_call_expr (implicit_built_in_decls[BUILT_IN_MEMCPY],
7574                                        3, dest_addr, src_addr,
7575                                        size_int (cur_size));
7576                   gimplify_and_add (copy, pre_p);
7577                 }
7578               prev_size += cur_size;
7579             }
7580         }
7581
7582       if (needed_intregs)
7583         {
7584           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
7585                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
7586           gimplify_assign (gpr, t, pre_p);
7587         }
7588
7589       if (needed_sseregs)
7590         {
7591           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
7592                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
7593           gimplify_assign (fpr, t, pre_p);
7594         }
7595
7596       gimple_seq_add_stmt (pre_p, gimple_build_goto (lab_over));
7597
7598       gimple_seq_add_stmt (pre_p, gimple_build_label (lab_false));
7599     }
7600
7601   /* ... otherwise out of the overflow area.  */
7602
7603   /* When we align parameter on stack for caller, if the parameter
7604      alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
7605      aligned at MAX_SUPPORTED_STACK_ALIGNMENT.  We will match callee
7606      here with caller.  */
7607   arg_boundary = FUNCTION_ARG_BOUNDARY (VOIDmode, type);
7608   if ((unsigned int) arg_boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
7609     arg_boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
7610
7611   /* Care for on-stack alignment if needed.  */
7612   if (arg_boundary <= 64 || size == 0)
7613     t = ovf;
7614  else
7615     {
7616       HOST_WIDE_INT align = arg_boundary / 8;
7617       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
7618                   size_int (align - 1));
7619       t = fold_convert (sizetype, t);
7620       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
7621                   size_int (-align));
7622       t = fold_convert (TREE_TYPE (ovf), t);
7623     }
7624
7625   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
7626   gimplify_assign (addr, t, pre_p);
7627
7628   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
7629               size_int (rsize * UNITS_PER_WORD));
7630   gimplify_assign (unshare_expr (ovf), t, pre_p);
7631
7632   if (container)
7633     gimple_seq_add_stmt (pre_p, gimple_build_label (lab_over));
7634
7635   ptrtype = build_pointer_type_for_mode (type, ptr_mode, true);
7636   addr = fold_convert (ptrtype, addr);
7637
7638   if (indirect_p)
7639     addr = build_va_arg_indirect_ref (addr);
7640   return build_va_arg_indirect_ref (addr);
7641 }
7642 \f
7643 /* Return true if OPNUM's MEM should be matched
7644    in movabs* patterns.  */
7645
7646 bool
7647 ix86_check_movabs (rtx insn, int opnum)
7648 {
7649   rtx set, mem;
7650
7651   set = PATTERN (insn);
7652   if (GET_CODE (set) == PARALLEL)
7653     set = XVECEXP (set, 0, 0);
7654   gcc_assert (GET_CODE (set) == SET);
7655   mem = XEXP (set, opnum);
7656   while (GET_CODE (mem) == SUBREG)
7657     mem = SUBREG_REG (mem);
7658   gcc_assert (MEM_P (mem));
7659   return volatile_ok || !MEM_VOLATILE_P (mem);
7660 }
7661 \f
7662 /* Initialize the table of extra 80387 mathematical constants.  */
7663
7664 static void
7665 init_ext_80387_constants (void)
7666 {
7667   static const char * cst[5] =
7668   {
7669     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
7670     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
7671     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
7672     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
7673     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
7674   };
7675   int i;
7676
7677   for (i = 0; i < 5; i++)
7678     {
7679       real_from_string (&ext_80387_constants_table[i], cst[i]);
7680       /* Ensure each constant is rounded to XFmode precision.  */
7681       real_convert (&ext_80387_constants_table[i],
7682                     XFmode, &ext_80387_constants_table[i]);
7683     }
7684
7685   ext_80387_constants_init = 1;
7686 }
7687
7688 /* Return non-zero if the constant is something that
7689    can be loaded with a special instruction.  */
7690
7691 int
7692 standard_80387_constant_p (rtx x)
7693 {
7694   enum machine_mode mode = GET_MODE (x);
7695
7696   REAL_VALUE_TYPE r;
7697
7698   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
7699     return -1;
7700
7701   if (x == CONST0_RTX (mode))
7702     return 1;
7703   if (x == CONST1_RTX (mode))
7704     return 2;
7705
7706   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7707
7708   /* For XFmode constants, try to find a special 80387 instruction when
7709      optimizing for size or on those CPUs that benefit from them.  */
7710   if (mode == XFmode
7711       && (optimize_function_for_size_p (cfun) || TARGET_EXT_80387_CONSTANTS))
7712     {
7713       int i;
7714
7715       if (! ext_80387_constants_init)
7716         init_ext_80387_constants ();
7717
7718       for (i = 0; i < 5; i++)
7719         if (real_identical (&r, &ext_80387_constants_table[i]))
7720           return i + 3;
7721     }
7722
7723   /* Load of the constant -0.0 or -1.0 will be split as
7724      fldz;fchs or fld1;fchs sequence.  */
7725   if (real_isnegzero (&r))
7726     return 8;
7727   if (real_identical (&r, &dconstm1))
7728     return 9;
7729
7730   return 0;
7731 }
7732
7733 /* Return the opcode of the special instruction to be used to load
7734    the constant X.  */
7735
7736 const char *
7737 standard_80387_constant_opcode (rtx x)
7738 {
7739   switch (standard_80387_constant_p (x))
7740     {
7741     case 1:
7742       return "fldz";
7743     case 2:
7744       return "fld1";
7745     case 3:
7746       return "fldlg2";
7747     case 4:
7748       return "fldln2";
7749     case 5:
7750       return "fldl2e";
7751     case 6:
7752       return "fldl2t";
7753     case 7:
7754       return "fldpi";
7755     case 8:
7756     case 9:
7757       return "#";
7758     default:
7759       gcc_unreachable ();
7760     }
7761 }
7762
7763 /* Return the CONST_DOUBLE representing the 80387 constant that is
7764    loaded by the specified special instruction.  The argument IDX
7765    matches the return value from standard_80387_constant_p.  */
7766
7767 rtx
7768 standard_80387_constant_rtx (int idx)
7769 {
7770   int i;
7771
7772   if (! ext_80387_constants_init)
7773     init_ext_80387_constants ();
7774
7775   switch (idx)
7776     {
7777     case 3:
7778     case 4:
7779     case 5:
7780     case 6:
7781     case 7:
7782       i = idx - 3;
7783       break;
7784
7785     default:
7786       gcc_unreachable ();
7787     }
7788
7789   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
7790                                        XFmode);
7791 }
7792
7793 /* Return 1 if X is all 0s and 2 if x is all 1s
7794    in supported SSE vector mode.  */
7795
7796 int
7797 standard_sse_constant_p (rtx x)
7798 {
7799   enum machine_mode mode = GET_MODE (x);
7800
7801   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
7802     return 1;
7803   if (vector_all_ones_operand (x, mode))
7804     switch (mode)
7805       {
7806       case V16QImode:
7807       case V8HImode:
7808       case V4SImode:
7809       case V2DImode:
7810         if (TARGET_SSE2)
7811           return 2;
7812       default:
7813         break;
7814       }
7815
7816   return 0;
7817 }
7818
7819 /* Return the opcode of the special instruction to be used to load
7820    the constant X.  */
7821
7822 const char *
7823 standard_sse_constant_opcode (rtx insn, rtx x)
7824 {
7825   switch (standard_sse_constant_p (x))
7826     {
7827     case 1:
7828       switch (get_attr_mode (insn))
7829         {
7830         case MODE_V4SF:
7831           return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7832         case MODE_V2DF:
7833           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7834             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7835           else
7836             return TARGET_AVX ? "vxorpd\t%0, %0, %0" : "xorpd\t%0, %0";
7837         case MODE_TI:
7838           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7839             return TARGET_AVX ? "vxorps\t%0, %0, %0" : "xorps\t%0, %0";
7840           else
7841             return TARGET_AVX ? "vpxor\t%0, %0, %0" : "pxor\t%0, %0";
7842         case MODE_V8SF:
7843           return "vxorps\t%x0, %x0, %x0";
7844         case MODE_V4DF:
7845           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7846             return "vxorps\t%x0, %x0, %x0";
7847           else
7848             return "vxorpd\t%x0, %x0, %x0";
7849         case MODE_OI:
7850           if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
7851             return "vxorps\t%x0, %x0, %x0";
7852           else
7853             return "vpxor\t%x0, %x0, %x0";
7854         default:
7855           break;
7856         }
7857     case 2:
7858       return TARGET_AVX ? "vpcmpeqd\t%0, %0, %0" : "pcmpeqd\t%0, %0";
7859     default:
7860       break;
7861     }
7862   gcc_unreachable ();
7863 }
7864
7865 /* Returns true if OP contains a symbol reference */
7866
7867 bool
7868 symbolic_reference_mentioned_p (rtx op)
7869 {
7870   const char *fmt;
7871   int i;
7872
7873   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
7874     return true;
7875
7876   fmt = GET_RTX_FORMAT (GET_CODE (op));
7877   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
7878     {
7879       if (fmt[i] == 'E')
7880         {
7881           int j;
7882
7883           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
7884             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
7885               return true;
7886         }
7887
7888       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
7889         return true;
7890     }
7891
7892   return false;
7893 }
7894
7895 /* Return true if it is appropriate to emit `ret' instructions in the
7896    body of a function.  Do this only if the epilogue is simple, needing a
7897    couple of insns.  Prior to reloading, we can't tell how many registers
7898    must be saved, so return false then.  Return false if there is no frame
7899    marker to de-allocate.  */
7900
7901 bool
7902 ix86_can_use_return_insn_p (void)
7903 {
7904   struct ix86_frame frame;
7905
7906   if (! reload_completed || frame_pointer_needed)
7907     return 0;
7908
7909   /* Don't allow more than 32k pop, since that's all we can do
7910      with one instruction.  */
7911   if (crtl->args.pops_args && crtl->args.size >= 32768)
7912     return 0;
7913
7914   ix86_compute_frame_layout (&frame);
7915   return (frame.stack_pointer_offset == UNITS_PER_WORD
7916           && (frame.nregs + frame.nsseregs) == 0);
7917 }
7918 \f
7919 /* Value should be nonzero if functions must have frame pointers.
7920    Zero means the frame pointer need not be set up (and parms may
7921    be accessed via the stack pointer) in functions that seem suitable.  */
7922
7923 static bool
7924 ix86_frame_pointer_required (void)
7925 {
7926   /* If we accessed previous frames, then the generated code expects
7927      to be able to access the saved ebp value in our frame.  */
7928   if (cfun->machine->accesses_prev_frame)
7929     return true;
7930
7931   /* Several x86 os'es need a frame pointer for other reasons,
7932      usually pertaining to setjmp.  */
7933   if (SUBTARGET_FRAME_POINTER_REQUIRED)
7934     return true;
7935
7936   /* In ix86_option_override_internal, TARGET_OMIT_LEAF_FRAME_POINTER
7937      turns off the frame pointer by default.  Turn it back on now if
7938      we've not got a leaf function.  */
7939   if (TARGET_OMIT_LEAF_FRAME_POINTER
7940       && (!current_function_is_leaf
7941           || ix86_current_function_calls_tls_descriptor))
7942     return true;
7943
7944   if (crtl->profile && !flag_fentry)
7945     return true;
7946
7947   return false;
7948 }
7949
7950 /* Record that the current function accesses previous call frames.  */
7951
7952 void
7953 ix86_setup_frame_addresses (void)
7954 {
7955   cfun->machine->accesses_prev_frame = 1;
7956 }
7957 \f
7958 #ifndef USE_HIDDEN_LINKONCE
7959 # if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
7960 #  define USE_HIDDEN_LINKONCE 1
7961 # else
7962 #  define USE_HIDDEN_LINKONCE 0
7963 # endif
7964 #endif
7965
7966 static int pic_labels_used;
7967
7968 /* Fills in the label name that should be used for a pc thunk for
7969    the given register.  */
7970
7971 static void
7972 get_pc_thunk_name (char name[32], unsigned int regno)
7973 {
7974   gcc_assert (!TARGET_64BIT);
7975
7976   if (USE_HIDDEN_LINKONCE)
7977     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
7978   else
7979     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
7980 }
7981
7982
7983 /* This function generates code for -fpic that loads %ebx with
7984    the return address of the caller and then returns.  */
7985
7986 static void
7987 ix86_code_end (void)
7988 {
7989   rtx xops[2];
7990   int regno;
7991
7992   for (regno = AX_REG; regno <= SP_REG; regno++)
7993     {
7994       char name[32];
7995       tree decl;
7996
7997       if (!(pic_labels_used & (1 << regno)))
7998         continue;
7999
8000       get_pc_thunk_name (name, regno);
8001
8002       decl = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
8003                          get_identifier (name),
8004                          build_function_type (void_type_node, void_list_node));
8005       DECL_RESULT (decl) = build_decl (BUILTINS_LOCATION, RESULT_DECL,
8006                                        NULL_TREE, void_type_node);
8007       TREE_PUBLIC (decl) = 1;
8008       TREE_STATIC (decl) = 1;
8009
8010 #if TARGET_MACHO
8011       if (TARGET_MACHO)
8012         {
8013           switch_to_section (darwin_sections[text_coal_section]);
8014           fputs ("\t.weak_definition\t", asm_out_file);
8015           assemble_name (asm_out_file, name);
8016           fputs ("\n\t.private_extern\t", asm_out_file);
8017           assemble_name (asm_out_file, name);
8018           putc ('\n', asm_out_file);
8019           ASM_OUTPUT_LABEL (asm_out_file, name);
8020           DECL_WEAK (decl) = 1;
8021         }
8022       else
8023 #endif
8024       if (USE_HIDDEN_LINKONCE)
8025         {
8026           DECL_COMDAT_GROUP (decl) = DECL_ASSEMBLER_NAME (decl);
8027
8028           targetm.asm_out.unique_section (decl, 0);
8029           switch_to_section (get_named_section (decl, NULL, 0));
8030
8031           targetm.asm_out.globalize_label (asm_out_file, name);
8032           fputs ("\t.hidden\t", asm_out_file);
8033           assemble_name (asm_out_file, name);
8034           putc ('\n', asm_out_file);
8035           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
8036         }
8037       else
8038         {
8039           switch_to_section (text_section);
8040           ASM_OUTPUT_LABEL (asm_out_file, name);
8041         }
8042
8043       DECL_INITIAL (decl) = make_node (BLOCK);
8044       current_function_decl = decl;
8045       init_function_start (decl);
8046       first_function_block_is_cold = false;
8047       /* Make sure unwind info is emitted for the thunk if needed.  */
8048       final_start_function (emit_barrier (), asm_out_file, 1);
8049
8050       /* Pad stack IP move with 4 instructions (two NOPs count
8051          as one instruction).  */
8052       if (TARGET_PAD_SHORT_FUNCTION)
8053         {
8054           int i = 8;
8055
8056           while (i--)
8057             fputs ("\tnop\n", asm_out_file);
8058         }
8059
8060       xops[0] = gen_rtx_REG (Pmode, regno);
8061       xops[1] = gen_rtx_MEM (Pmode, stack_pointer_rtx);
8062       output_asm_insn ("mov%z0\t{%1, %0|%0, %1}", xops);
8063       fputs ("\tret\n", asm_out_file);
8064       final_end_function ();
8065       init_insn_lengths ();
8066       free_after_compilation (cfun);
8067       set_cfun (NULL);
8068       current_function_decl = NULL;
8069     }
8070
8071   if (flag_split_stack)
8072     file_end_indicate_split_stack ();
8073 }
8074
8075 /* Emit code for the SET_GOT patterns.  */
8076
8077 const char *
8078 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
8079 {
8080   rtx xops[3];
8081
8082   xops[0] = dest;
8083
8084   if (TARGET_VXWORKS_RTP && flag_pic)
8085     {
8086       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
8087       xops[2] = gen_rtx_MEM (Pmode,
8088                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
8089       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
8090
8091       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
8092          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
8093          an unadorned address.  */
8094       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
8095       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
8096       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
8097       return "";
8098     }
8099
8100   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
8101
8102   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
8103     {
8104       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
8105
8106       if (!flag_pic)
8107         output_asm_insn ("mov%z0\t{%2, %0|%0, %2}", xops);
8108       else
8109         {
8110           output_asm_insn ("call\t%a2", xops);
8111 #ifdef DWARF2_UNWIND_INFO
8112           /* The call to next label acts as a push.  */
8113           if (dwarf2out_do_frame ())
8114             {
8115               rtx insn;
8116               start_sequence ();
8117               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8118                                              gen_rtx_PLUS (Pmode,
8119                                                            stack_pointer_rtx,
8120                                                            GEN_INT (-4))));
8121               RTX_FRAME_RELATED_P (insn) = 1;
8122               dwarf2out_frame_debug (insn, true);
8123               end_sequence ();
8124             }
8125 #endif
8126         }
8127
8128 #if TARGET_MACHO
8129       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8130          is what will be referenced by the Mach-O PIC subsystem.  */
8131       if (!label)
8132         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8133 #endif
8134
8135       targetm.asm_out.internal_label (asm_out_file, "L",
8136                                       CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
8137
8138       if (flag_pic)
8139         {
8140           output_asm_insn ("pop%z0\t%0", xops);
8141 #ifdef DWARF2_UNWIND_INFO
8142           /* The pop is a pop and clobbers dest, but doesn't restore it
8143              for unwind info purposes.  */
8144           if (dwarf2out_do_frame ())
8145             {
8146               rtx insn;
8147               start_sequence ();
8148               insn = emit_insn (gen_rtx_SET (VOIDmode, dest, const0_rtx));
8149               dwarf2out_frame_debug (insn, true);
8150               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
8151                                              gen_rtx_PLUS (Pmode,
8152                                                            stack_pointer_rtx,
8153                                                            GEN_INT (4))));
8154               RTX_FRAME_RELATED_P (insn) = 1;
8155               dwarf2out_frame_debug (insn, true);
8156               end_sequence ();
8157             }
8158 #endif
8159         }
8160     }
8161   else
8162     {
8163       char name[32];
8164       get_pc_thunk_name (name, REGNO (dest));
8165       pic_labels_used |= 1 << REGNO (dest);
8166
8167 #ifdef DWARF2_UNWIND_INFO
8168       /* Ensure all queued register saves are flushed before the
8169          call.  */
8170       if (dwarf2out_do_frame ())
8171         dwarf2out_flush_queued_reg_saves ();
8172 #endif
8173       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
8174       xops[2] = gen_rtx_MEM (QImode, xops[2]);
8175       output_asm_insn ("call\t%X2", xops);
8176       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
8177          is what will be referenced by the Mach-O PIC subsystem.  */
8178 #if TARGET_MACHO
8179       if (!label)
8180         ASM_OUTPUT_LABEL (asm_out_file, MACHOPIC_FUNCTION_BASE_NAME);
8181       else
8182         targetm.asm_out.internal_label (asm_out_file, "L",
8183                                            CODE_LABEL_NUMBER (label));
8184 #endif
8185     }
8186
8187   if (TARGET_MACHO)
8188     return "";
8189
8190   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
8191     output_asm_insn ("add%z0\t{%1, %0|%0, %1}", xops);
8192   else
8193     output_asm_insn ("add%z0\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
8194
8195   return "";
8196 }
8197
8198 /* Generate an "push" pattern for input ARG.  */
8199
8200 static rtx
8201 gen_push (rtx arg)
8202 {
8203   struct machine_function *m = cfun->machine;
8204
8205   if (m->fs.cfa_reg == stack_pointer_rtx)
8206     m->fs.cfa_offset += UNITS_PER_WORD;
8207   m->fs.sp_offset += UNITS_PER_WORD;
8208
8209   return gen_rtx_SET (VOIDmode,
8210                       gen_rtx_MEM (Pmode,
8211                                    gen_rtx_PRE_DEC (Pmode,
8212                                                     stack_pointer_rtx)),
8213                       arg);
8214 }
8215
8216 /* Generate an "pop" pattern for input ARG.  */
8217
8218 static rtx
8219 gen_pop (rtx arg)
8220 {
8221   return gen_rtx_SET (VOIDmode,
8222                       arg,
8223                       gen_rtx_MEM (Pmode,
8224                                    gen_rtx_POST_INC (Pmode,
8225                                                      stack_pointer_rtx)));
8226 }
8227
8228 /* Return >= 0 if there is an unused call-clobbered register available
8229    for the entire function.  */
8230
8231 static unsigned int
8232 ix86_select_alt_pic_regnum (void)
8233 {
8234   if (current_function_is_leaf
8235       && !crtl->profile
8236       && !ix86_current_function_calls_tls_descriptor)
8237     {
8238       int i, drap;
8239       /* Can't use the same register for both PIC and DRAP.  */
8240       if (crtl->drap_reg)
8241         drap = REGNO (crtl->drap_reg);
8242       else
8243         drap = -1;
8244       for (i = 2; i >= 0; --i)
8245         if (i != drap && !df_regs_ever_live_p (i))
8246           return i;
8247     }
8248
8249   return INVALID_REGNUM;
8250 }
8251
8252 /* Return 1 if we need to save REGNO.  */
8253 static int
8254 ix86_save_reg (unsigned int regno, int maybe_eh_return)
8255 {
8256   if (pic_offset_table_rtx
8257       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
8258       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
8259           || crtl->profile
8260           || crtl->calls_eh_return
8261           || crtl->uses_const_pool))
8262     {
8263       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
8264         return 0;
8265       return 1;
8266     }
8267
8268   if (crtl->calls_eh_return && maybe_eh_return)
8269     {
8270       unsigned i;
8271       for (i = 0; ; i++)
8272         {
8273           unsigned test = EH_RETURN_DATA_REGNO (i);
8274           if (test == INVALID_REGNUM)
8275             break;
8276           if (test == regno)
8277             return 1;
8278         }
8279     }
8280
8281   if (crtl->drap_reg && regno == REGNO (crtl->drap_reg))
8282     return 1;
8283
8284   return (df_regs_ever_live_p (regno)
8285           && !call_used_regs[regno]
8286           && !fixed_regs[regno]
8287           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
8288 }
8289
8290 /* Return number of saved general prupose registers.  */
8291
8292 static int
8293 ix86_nsaved_regs (void)
8294 {
8295   int nregs = 0;
8296   int regno;
8297
8298   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8299     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8300       nregs ++;
8301   return nregs;
8302 }
8303
8304 /* Return number of saved SSE registrers.  */
8305
8306 static int
8307 ix86_nsaved_sseregs (void)
8308 {
8309   int nregs = 0;
8310   int regno;
8311
8312   if (ix86_cfun_abi () != MS_ABI)
8313     return 0;
8314   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8315     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8316       nregs ++;
8317   return nregs;
8318 }
8319
8320 /* Given FROM and TO register numbers, say whether this elimination is
8321    allowed.  If stack alignment is needed, we can only replace argument
8322    pointer with hard frame pointer, or replace frame pointer with stack
8323    pointer.  Otherwise, frame pointer elimination is automatically
8324    handled and all other eliminations are valid.  */
8325
8326 static bool
8327 ix86_can_eliminate (const int from, const int to)
8328 {
8329   if (stack_realign_fp)
8330     return ((from == ARG_POINTER_REGNUM
8331              && to == HARD_FRAME_POINTER_REGNUM)
8332             || (from == FRAME_POINTER_REGNUM
8333                 && to == STACK_POINTER_REGNUM));
8334   else
8335     return to == STACK_POINTER_REGNUM ? !frame_pointer_needed : true;
8336 }
8337
8338 /* Return the offset between two registers, one to be eliminated, and the other
8339    its replacement, at the start of a routine.  */
8340
8341 HOST_WIDE_INT
8342 ix86_initial_elimination_offset (int from, int to)
8343 {
8344   struct ix86_frame frame;
8345   ix86_compute_frame_layout (&frame);
8346
8347   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
8348     return frame.hard_frame_pointer_offset;
8349   else if (from == FRAME_POINTER_REGNUM
8350            && to == HARD_FRAME_POINTER_REGNUM)
8351     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
8352   else
8353     {
8354       gcc_assert (to == STACK_POINTER_REGNUM);
8355
8356       if (from == ARG_POINTER_REGNUM)
8357         return frame.stack_pointer_offset;
8358
8359       gcc_assert (from == FRAME_POINTER_REGNUM);
8360       return frame.stack_pointer_offset - frame.frame_pointer_offset;
8361     }
8362 }
8363
8364 /* In a dynamically-aligned function, we can't know the offset from
8365    stack pointer to frame pointer, so we must ensure that setjmp
8366    eliminates fp against the hard fp (%ebp) rather than trying to
8367    index from %esp up to the top of the frame across a gap that is
8368    of unknown (at compile-time) size.  */
8369 static rtx
8370 ix86_builtin_setjmp_frame_value (void)
8371 {
8372   return stack_realign_fp ? hard_frame_pointer_rtx : virtual_stack_vars_rtx;
8373 }
8374
8375 /* On the x86 -fsplit-stack and -fstack-protector both use the same
8376    field in the TCB, so they can not be used together.  */
8377
8378 static bool
8379 ix86_supports_split_stack (bool report ATTRIBUTE_UNUSED)
8380 {
8381   bool ret = true;
8382
8383 #ifndef TARGET_THREAD_SPLIT_STACK_OFFSET
8384   if (report)
8385     error ("%<-fsplit-stack%> currently only supported on GNU/Linux");
8386   ret = false;
8387 #else
8388   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
8389     {
8390       if (report)
8391         error ("%<-fsplit-stack%> requires "
8392                "assembler support for CFI directives");
8393       ret = false;
8394     }
8395 #endif
8396
8397   return ret;
8398 }
8399
8400 /* When using -fsplit-stack, the allocation routines set a field in
8401    the TCB to the bottom of the stack plus this much space, measured
8402    in bytes.  */
8403
8404 #define SPLIT_STACK_AVAILABLE 256
8405
8406 /* Fill structure ix86_frame about frame of currently computed function.  */
8407
8408 static void
8409 ix86_compute_frame_layout (struct ix86_frame *frame)
8410 {
8411   unsigned int stack_alignment_needed;
8412   HOST_WIDE_INT offset;
8413   unsigned int preferred_alignment;
8414   HOST_WIDE_INT size = get_frame_size ();
8415   HOST_WIDE_INT to_allocate;
8416
8417   frame->nregs = ix86_nsaved_regs ();
8418   frame->nsseregs = ix86_nsaved_sseregs ();
8419
8420   stack_alignment_needed = crtl->stack_alignment_needed / BITS_PER_UNIT;
8421   preferred_alignment = crtl->preferred_stack_boundary / BITS_PER_UNIT;
8422
8423   /* MS ABI seem to require stack alignment to be always 16 except for function
8424      prologues and leaf.  */
8425   if ((ix86_cfun_abi () == MS_ABI && preferred_alignment < 16)
8426       && (!current_function_is_leaf || cfun->calls_alloca != 0
8427           || ix86_current_function_calls_tls_descriptor))
8428     {
8429       preferred_alignment = 16;
8430       stack_alignment_needed = 16;
8431       crtl->preferred_stack_boundary = 128;
8432       crtl->stack_alignment_needed = 128;
8433     }
8434
8435   gcc_assert (!size || stack_alignment_needed);
8436   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
8437   gcc_assert (preferred_alignment <= stack_alignment_needed);
8438
8439   /* During reload iteration the amount of registers saved can change.
8440      Recompute the value as needed.  Do not recompute when amount of registers
8441      didn't change as reload does multiple calls to the function and does not
8442      expect the decision to change within single iteration.  */
8443   if (!optimize_function_for_size_p (cfun)
8444       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
8445     {
8446       int count = frame->nregs;
8447       struct cgraph_node *node = cgraph_node (current_function_decl);
8448
8449       cfun->machine->use_fast_prologue_epilogue_nregs = count;
8450       /* The fast prologue uses move instead of push to save registers.  This
8451          is significantly longer, but also executes faster as modern hardware
8452          can execute the moves in parallel, but can't do that for push/pop.
8453
8454          Be careful about choosing what prologue to emit:  When function takes
8455          many instructions to execute we may use slow version as well as in
8456          case function is known to be outside hot spot (this is known with
8457          feedback only).  Weight the size of function by number of registers
8458          to save as it is cheap to use one or two push instructions but very
8459          slow to use many of them.  */
8460       if (count)
8461         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
8462       if (node->frequency < NODE_FREQUENCY_NORMAL
8463           || (flag_branch_probabilities
8464               && node->frequency < NODE_FREQUENCY_HOT))
8465         cfun->machine->use_fast_prologue_epilogue = false;
8466       else
8467         cfun->machine->use_fast_prologue_epilogue
8468            = !expensive_function_p (count);
8469     }
8470   if (TARGET_PROLOGUE_USING_MOVE
8471       && cfun->machine->use_fast_prologue_epilogue)
8472     frame->save_regs_using_mov = true;
8473   else
8474     frame->save_regs_using_mov = false;
8475
8476   /* If static stack checking is enabled and done with probes, the registers
8477      need to be saved before allocating the frame.  */
8478   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
8479     frame->save_regs_using_mov = false;
8480
8481   /* Skip return address.  */
8482   offset = UNITS_PER_WORD;
8483
8484   /* Skip pushed static chain.  */
8485   if (ix86_static_chain_on_stack)
8486     offset += UNITS_PER_WORD;
8487
8488   /* Skip saved base pointer.  */
8489   if (frame_pointer_needed)
8490     offset += UNITS_PER_WORD;
8491
8492   frame->hard_frame_pointer_offset = offset;
8493
8494   /* Register save area */
8495   offset += frame->nregs * UNITS_PER_WORD;
8496   frame->reg_save_offset = offset;
8497
8498   /* Align and set SSE register save area.  */
8499   if (frame->nsseregs)
8500     {
8501       /* The only ABI that has saved SSE registers (Win64) also has a
8502          16-byte aligned default stack, and thus we don't need to be
8503          within the re-aligned local stack frame to save them.  */
8504       gcc_assert (INCOMING_STACK_BOUNDARY >= 128);
8505       offset = (offset + 16 - 1) & -16;
8506       offset += frame->nsseregs * 16;
8507     }
8508   frame->sse_reg_save_offset = offset;
8509
8510   /* The re-aligned stack starts here.  Values before this point are not
8511      directly comparable with values below this point.  In order to make
8512      sure that no value happens to be the same before and after, force
8513      the alignment computation below to add a non-zero value.  */
8514   if (stack_realign_fp)
8515     offset = (offset + stack_alignment_needed) & -stack_alignment_needed;
8516
8517   /* Va-arg area */
8518   frame->va_arg_size = ix86_varargs_gpr_size + ix86_varargs_fpr_size;
8519   offset += frame->va_arg_size;
8520
8521   /* Align start of frame for local function.  */
8522   offset = (offset + stack_alignment_needed - 1) & -stack_alignment_needed;
8523
8524   /* Frame pointer points here.  */
8525   frame->frame_pointer_offset = offset;
8526
8527   offset += size;
8528
8529   /* Add outgoing arguments area.  Can be skipped if we eliminated
8530      all the function calls as dead code.
8531      Skipping is however impossible when function calls alloca.  Alloca
8532      expander assumes that last crtl->outgoing_args_size
8533      of stack frame are unused.  */
8534   if (ACCUMULATE_OUTGOING_ARGS
8535       && (!current_function_is_leaf || cfun->calls_alloca
8536           || ix86_current_function_calls_tls_descriptor))
8537     {
8538       offset += crtl->outgoing_args_size;
8539       frame->outgoing_arguments_size = crtl->outgoing_args_size;
8540     }
8541   else
8542     frame->outgoing_arguments_size = 0;
8543
8544   /* Align stack boundary.  Only needed if we're calling another function
8545      or using alloca.  */
8546   if (!current_function_is_leaf || cfun->calls_alloca
8547       || ix86_current_function_calls_tls_descriptor)
8548     offset = (offset + preferred_alignment - 1) & -preferred_alignment;
8549
8550   /* We've reached end of stack frame.  */
8551   frame->stack_pointer_offset = offset;
8552
8553   /* Size prologue needs to allocate.  */
8554   to_allocate = offset - frame->sse_reg_save_offset;
8555
8556   if ((!to_allocate && frame->nregs <= 1)
8557       || (TARGET_64BIT && to_allocate >= (HOST_WIDE_INT) 0x80000000))
8558     frame->save_regs_using_mov = false;
8559
8560   if (ix86_using_red_zone ()
8561       && current_function_sp_is_unchanging
8562       && current_function_is_leaf
8563       && !ix86_current_function_calls_tls_descriptor)
8564     {
8565       frame->red_zone_size = to_allocate;
8566       if (frame->save_regs_using_mov)
8567         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
8568       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
8569         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
8570     }
8571   else
8572     frame->red_zone_size = 0;
8573   frame->stack_pointer_offset -= frame->red_zone_size;
8574 }
8575
8576 /* This is semi-inlined memory_address_length, but simplified
8577    since we know that we're always dealing with reg+offset, and
8578    to avoid having to create and discard all that rtl.  */
8579
8580 static inline int
8581 choose_baseaddr_len (unsigned int regno, HOST_WIDE_INT offset)
8582 {
8583   int len = 4;
8584
8585   if (offset == 0)
8586     {
8587       /* EBP and R13 cannot be encoded without an offset.  */
8588       len = (regno == BP_REG || regno == R13_REG);
8589     }
8590   else if (IN_RANGE (offset, -128, 127))
8591     len = 1;
8592
8593   /* ESP and R12 must be encoded with a SIB byte.  */
8594   if (regno == SP_REG || regno == R12_REG)
8595     len++;
8596
8597   return len;
8598 }
8599   
8600 /* Return an RTX that points to CFA_OFFSET within the stack frame.
8601    The valid base registers are taken from CFUN->MACHINE->FS.  */
8602
8603 static rtx
8604 choose_baseaddr (HOST_WIDE_INT cfa_offset)
8605 {
8606   const struct machine_function *m = cfun->machine;
8607   rtx base_reg = NULL;
8608   HOST_WIDE_INT base_offset = 0;
8609
8610   if (m->use_fast_prologue_epilogue)
8611     {
8612       /* Choose the base register most likely to allow the most scheduling
8613          opportunities.  Generally FP is valid througout the function,
8614          while DRAP must be reloaded within the epilogue.  But choose either
8615          over the SP due to increased encoding size.  */
8616
8617       if (m->fs.fp_valid)
8618         {
8619           base_reg = hard_frame_pointer_rtx;
8620           base_offset = m->fs.fp_offset - cfa_offset;
8621         }
8622       else if (m->fs.drap_valid)
8623         {
8624           base_reg = crtl->drap_reg;
8625           base_offset = 0 - cfa_offset;
8626         }
8627       else if (m->fs.sp_valid)
8628         {
8629           base_reg = stack_pointer_rtx;
8630           base_offset = m->fs.sp_offset - cfa_offset;
8631         }
8632     }
8633   else
8634     {
8635       HOST_WIDE_INT toffset;
8636       int len = 16, tlen;
8637
8638       /* Choose the base register with the smallest address encoding.
8639          With a tie, choose FP > DRAP > SP.  */
8640       if (m->fs.sp_valid)
8641         {
8642           base_reg = stack_pointer_rtx;
8643           base_offset = m->fs.sp_offset - cfa_offset;
8644           len = choose_baseaddr_len (STACK_POINTER_REGNUM, base_offset);
8645         }
8646       if (m->fs.drap_valid)
8647         {
8648           toffset = 0 - cfa_offset;
8649           tlen = choose_baseaddr_len (REGNO (crtl->drap_reg), toffset);
8650           if (tlen <= len)
8651             {
8652               base_reg = crtl->drap_reg;
8653               base_offset = toffset;
8654               len = tlen;
8655             }
8656         }
8657       if (m->fs.fp_valid)
8658         {
8659           toffset = m->fs.fp_offset - cfa_offset;
8660           tlen = choose_baseaddr_len (HARD_FRAME_POINTER_REGNUM, toffset);
8661           if (tlen <= len)
8662             {
8663               base_reg = hard_frame_pointer_rtx;
8664               base_offset = toffset;
8665               len = tlen;
8666             }
8667         }
8668     }
8669   gcc_assert (base_reg != NULL);
8670
8671   return plus_constant (base_reg, base_offset);
8672 }
8673
8674 /* Emit code to save registers in the prologue.  */
8675
8676 static void
8677 ix86_emit_save_regs (void)
8678 {
8679   unsigned int regno;
8680   rtx insn;
8681
8682   for (regno = FIRST_PSEUDO_REGISTER - 1; regno-- > 0; )
8683     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8684       {
8685         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
8686         RTX_FRAME_RELATED_P (insn) = 1;
8687       }
8688 }
8689
8690 /* Emit a single register save at CFA - CFA_OFFSET.  */
8691
8692 static void
8693 ix86_emit_save_reg_using_mov (enum machine_mode mode, unsigned int regno,
8694                               HOST_WIDE_INT cfa_offset)
8695 {
8696   struct machine_function *m = cfun->machine;
8697   rtx reg = gen_rtx_REG (mode, regno);
8698   rtx mem, addr, base, insn;
8699
8700   addr = choose_baseaddr (cfa_offset);
8701   mem = gen_frame_mem (mode, addr);
8702
8703   /* For SSE saves, we need to indicate the 128-bit alignment.  */
8704   set_mem_align (mem, GET_MODE_ALIGNMENT (mode));
8705
8706   insn = emit_move_insn (mem, reg);
8707   RTX_FRAME_RELATED_P (insn) = 1;
8708
8709   base = addr;
8710   if (GET_CODE (base) == PLUS)
8711     base = XEXP (base, 0);
8712   gcc_checking_assert (REG_P (base));
8713
8714   /* When saving registers into a re-aligned local stack frame, avoid
8715      any tricky guessing by dwarf2out.  */
8716   if (m->fs.realigned)
8717     {
8718       gcc_checking_assert (stack_realign_drap);
8719
8720       if (regno == REGNO (crtl->drap_reg))
8721         {
8722           /* A bit of a hack.  We force the DRAP register to be saved in
8723              the re-aligned stack frame, which provides us with a copy
8724              of the CFA that will last past the prologue.  Install it.  */
8725           gcc_checking_assert (cfun->machine->fs.fp_valid);
8726           addr = plus_constant (hard_frame_pointer_rtx,
8727                                 cfun->machine->fs.fp_offset - cfa_offset);
8728           mem = gen_rtx_MEM (mode, addr);
8729           add_reg_note (insn, REG_CFA_DEF_CFA, mem);
8730         }
8731       else
8732         {
8733           /* The frame pointer is a stable reference within the
8734              aligned frame.  Use it.  */
8735           gcc_checking_assert (cfun->machine->fs.fp_valid);
8736           addr = plus_constant (hard_frame_pointer_rtx,
8737                                 cfun->machine->fs.fp_offset - cfa_offset);
8738           mem = gen_rtx_MEM (mode, addr);
8739           add_reg_note (insn, REG_CFA_EXPRESSION,
8740                         gen_rtx_SET (VOIDmode, mem, reg));
8741         }
8742     }
8743
8744   /* The memory may not be relative to the current CFA register,
8745      which means that we may need to generate a new pattern for
8746      use by the unwind info.  */
8747   else if (base != m->fs.cfa_reg)
8748     {
8749       addr = plus_constant (m->fs.cfa_reg, m->fs.cfa_offset - cfa_offset);
8750       mem = gen_rtx_MEM (mode, addr);
8751       add_reg_note (insn, REG_CFA_OFFSET, gen_rtx_SET (VOIDmode, mem, reg));
8752     }
8753 }
8754
8755 /* Emit code to save registers using MOV insns.
8756    First register is stored at CFA - CFA_OFFSET.  */
8757 static void
8758 ix86_emit_save_regs_using_mov (HOST_WIDE_INT cfa_offset)
8759 {
8760   unsigned int regno;
8761
8762   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8763     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8764       {
8765         ix86_emit_save_reg_using_mov (Pmode, regno, cfa_offset);
8766         cfa_offset -= UNITS_PER_WORD;
8767       }
8768 }
8769
8770 /* Emit code to save SSE registers using MOV insns.
8771    First register is stored at CFA - CFA_OFFSET.  */
8772 static void
8773 ix86_emit_save_sse_regs_using_mov (HOST_WIDE_INT cfa_offset)
8774 {
8775   unsigned int regno;
8776
8777   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
8778     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, true))
8779       {
8780         ix86_emit_save_reg_using_mov (V4SFmode, regno, cfa_offset);
8781         cfa_offset -= 16;
8782       }
8783 }
8784
8785 static GTY(()) rtx queued_cfa_restores;
8786
8787 /* Add a REG_CFA_RESTORE REG note to INSN or queue them until next stack
8788    manipulation insn.  The value is on the stack at CFA - CFA_OFFSET.
8789    Don't add the note if the previously saved value will be left untouched
8790    within stack red-zone till return, as unwinders can find the same value
8791    in the register and on the stack.  */
8792
8793 static void
8794 ix86_add_cfa_restore_note (rtx insn, rtx reg, HOST_WIDE_INT cfa_offset)
8795 {
8796   if (cfa_offset <= cfun->machine->fs.red_zone_offset)
8797     return;
8798
8799   if (insn)
8800     {
8801       add_reg_note (insn, REG_CFA_RESTORE, reg);
8802       RTX_FRAME_RELATED_P (insn) = 1;
8803     }
8804   else
8805     queued_cfa_restores
8806       = alloc_reg_note (REG_CFA_RESTORE, reg, queued_cfa_restores);
8807 }
8808
8809 /* Add queued REG_CFA_RESTORE notes if any to INSN.  */
8810
8811 static void
8812 ix86_add_queued_cfa_restore_notes (rtx insn)
8813 {
8814   rtx last;
8815   if (!queued_cfa_restores)
8816     return;
8817   for (last = queued_cfa_restores; XEXP (last, 1); last = XEXP (last, 1))
8818     ;
8819   XEXP (last, 1) = REG_NOTES (insn);
8820   REG_NOTES (insn) = queued_cfa_restores;
8821   queued_cfa_restores = NULL_RTX;
8822   RTX_FRAME_RELATED_P (insn) = 1;
8823 }
8824
8825 /* Expand prologue or epilogue stack adjustment.
8826    The pattern exist to put a dependency on all ebp-based memory accesses.
8827    STYLE should be negative if instructions should be marked as frame related,
8828    zero if %r11 register is live and cannot be freely used and positive
8829    otherwise.  */
8830
8831 static void
8832 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset,
8833                            int style, bool set_cfa)
8834 {
8835   struct machine_function *m = cfun->machine;
8836   rtx insn;
8837
8838   if (! TARGET_64BIT)
8839     insn = gen_pro_epilogue_adjust_stack_si_add (dest, src, offset);
8840   else if (x86_64_immediate_operand (offset, DImode))
8841     insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, offset);
8842   else
8843     {
8844       rtx tmp;
8845       /* r11 is used by indirect sibcall return as well, set before the
8846          epilogue and used after the epilogue.  */
8847       if (style)
8848         tmp = gen_rtx_REG (DImode, R11_REG);
8849       else
8850         {
8851           gcc_assert (src != hard_frame_pointer_rtx
8852                       && dest != hard_frame_pointer_rtx);
8853           tmp = hard_frame_pointer_rtx;
8854         }
8855       insn = emit_insn (gen_rtx_SET (DImode, tmp, offset));
8856       if (style < 0)
8857         RTX_FRAME_RELATED_P (insn) = 1;
8858
8859       insn = gen_pro_epilogue_adjust_stack_di_add (dest, src, tmp);
8860     }
8861
8862   insn = emit_insn (insn);
8863   if (style >= 0)
8864     ix86_add_queued_cfa_restore_notes (insn);
8865
8866   if (set_cfa)
8867     {
8868       rtx r;
8869
8870       gcc_assert (m->fs.cfa_reg == src);
8871       m->fs.cfa_offset += INTVAL (offset);
8872       m->fs.cfa_reg = dest;
8873
8874       r = gen_rtx_PLUS (Pmode, src, offset);
8875       r = gen_rtx_SET (VOIDmode, dest, r);
8876       add_reg_note (insn, REG_CFA_ADJUST_CFA, r);
8877       RTX_FRAME_RELATED_P (insn) = 1;
8878     }
8879   else if (style < 0)
8880     RTX_FRAME_RELATED_P (insn) = 1;
8881
8882   if (dest == stack_pointer_rtx)
8883     {
8884       HOST_WIDE_INT ooffset = m->fs.sp_offset;
8885       bool valid = m->fs.sp_valid;
8886
8887       if (src == hard_frame_pointer_rtx)
8888         {
8889           valid = m->fs.fp_valid;
8890           ooffset = m->fs.fp_offset;
8891         }
8892       else if (src == crtl->drap_reg)
8893         {
8894           valid = m->fs.drap_valid;
8895           ooffset = 0;
8896         }
8897       else
8898         {
8899           /* Else there are two possibilities: SP itself, which we set
8900              up as the default above.  Or EH_RETURN_STACKADJ_RTX, which is
8901              taken care of this by hand along the eh_return path.  */
8902           gcc_checking_assert (src == stack_pointer_rtx
8903                                || offset == const0_rtx);
8904         }
8905
8906       m->fs.sp_offset = ooffset - INTVAL (offset);
8907       m->fs.sp_valid = valid;
8908     }
8909 }
8910
8911 /* Find an available register to be used as dynamic realign argument
8912    pointer regsiter.  Such a register will be written in prologue and
8913    used in begin of body, so it must not be
8914         1. parameter passing register.
8915         2. GOT pointer.
8916    We reuse static-chain register if it is available.  Otherwise, we
8917    use DI for i386 and R13 for x86-64.  We chose R13 since it has
8918    shorter encoding.
8919
8920    Return: the regno of chosen register.  */
8921
8922 static unsigned int
8923 find_drap_reg (void)
8924 {
8925   tree decl = cfun->decl;
8926
8927   if (TARGET_64BIT)
8928     {
8929       /* Use R13 for nested function or function need static chain.
8930          Since function with tail call may use any caller-saved
8931          registers in epilogue, DRAP must not use caller-saved
8932          register in such case.  */
8933       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8934         return R13_REG;
8935
8936       return R10_REG;
8937     }
8938   else
8939     {
8940       /* Use DI for nested function or function need static chain.
8941          Since function with tail call may use any caller-saved
8942          registers in epilogue, DRAP must not use caller-saved
8943          register in such case.  */
8944       if (DECL_STATIC_CHAIN (decl) || crtl->tail_call_emit)
8945         return DI_REG;
8946
8947       /* Reuse static chain register if it isn't used for parameter
8948          passing.  */
8949       if (ix86_function_regparm (TREE_TYPE (decl), decl) <= 2
8950           && !lookup_attribute ("fastcall",
8951                                 TYPE_ATTRIBUTES (TREE_TYPE (decl)))
8952           && !lookup_attribute ("thiscall",
8953                                 TYPE_ATTRIBUTES (TREE_TYPE (decl))))
8954         return CX_REG;
8955       else
8956         return DI_REG;
8957     }
8958 }
8959
8960 /* Return minimum incoming stack alignment.  */
8961
8962 static unsigned int
8963 ix86_minimum_incoming_stack_boundary (bool sibcall)
8964 {
8965   unsigned int incoming_stack_boundary;
8966
8967   /* Prefer the one specified at command line. */
8968   if (ix86_user_incoming_stack_boundary)
8969     incoming_stack_boundary = ix86_user_incoming_stack_boundary;
8970   /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
8971      if -mstackrealign is used, it isn't used for sibcall check and
8972      estimated stack alignment is 128bit.  */
8973   else if (!sibcall
8974            && !TARGET_64BIT
8975            && ix86_force_align_arg_pointer
8976            && crtl->stack_alignment_estimated == 128)
8977     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8978   else
8979     incoming_stack_boundary = ix86_default_incoming_stack_boundary;
8980
8981   /* Incoming stack alignment can be changed on individual functions
8982      via force_align_arg_pointer attribute.  We use the smallest
8983      incoming stack boundary.  */
8984   if (incoming_stack_boundary > MIN_STACK_BOUNDARY
8985       && lookup_attribute (ix86_force_align_arg_pointer_string,
8986                            TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))))
8987     incoming_stack_boundary = MIN_STACK_BOUNDARY;
8988
8989   /* The incoming stack frame has to be aligned at least at
8990      parm_stack_boundary.  */
8991   if (incoming_stack_boundary < crtl->parm_stack_boundary)
8992     incoming_stack_boundary = crtl->parm_stack_boundary;
8993
8994   /* Stack at entrance of main is aligned by runtime.  We use the
8995      smallest incoming stack boundary. */
8996   if (incoming_stack_boundary > MAIN_STACK_BOUNDARY
8997       && DECL_NAME (current_function_decl)
8998       && MAIN_NAME_P (DECL_NAME (current_function_decl))
8999       && DECL_FILE_SCOPE_P (current_function_decl))
9000     incoming_stack_boundary = MAIN_STACK_BOUNDARY;
9001
9002   return incoming_stack_boundary;
9003 }
9004
9005 /* Update incoming stack boundary and estimated stack alignment.  */
9006
9007 static void
9008 ix86_update_stack_boundary (void)
9009 {
9010   ix86_incoming_stack_boundary
9011     = ix86_minimum_incoming_stack_boundary (false);
9012
9013   /* x86_64 vararg needs 16byte stack alignment for register save
9014      area.  */
9015   if (TARGET_64BIT
9016       && cfun->stdarg
9017       && crtl->stack_alignment_estimated < 128)
9018     crtl->stack_alignment_estimated = 128;
9019 }
9020
9021 /* Handle the TARGET_GET_DRAP_RTX hook.  Return NULL if no DRAP is
9022    needed or an rtx for DRAP otherwise.  */
9023
9024 static rtx
9025 ix86_get_drap_rtx (void)
9026 {
9027   if (ix86_force_drap || !ACCUMULATE_OUTGOING_ARGS)
9028     crtl->need_drap = true;
9029
9030   if (stack_realign_drap)
9031     {
9032       /* Assign DRAP to vDRAP and returns vDRAP */
9033       unsigned int regno = find_drap_reg ();
9034       rtx drap_vreg;
9035       rtx arg_ptr;
9036       rtx seq, insn;
9037
9038       arg_ptr = gen_rtx_REG (Pmode, regno);
9039       crtl->drap_reg = arg_ptr;
9040
9041       start_sequence ();
9042       drap_vreg = copy_to_reg (arg_ptr);
9043       seq = get_insns ();
9044       end_sequence ();
9045
9046       insn = emit_insn_before (seq, NEXT_INSN (entry_of_function ()));
9047       if (!optimize)
9048         {
9049           add_reg_note (insn, REG_CFA_SET_VDRAP, drap_vreg);
9050           RTX_FRAME_RELATED_P (insn) = 1;
9051         }
9052       return drap_vreg;
9053     }
9054   else
9055     return NULL;
9056 }
9057
9058 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
9059
9060 static rtx
9061 ix86_internal_arg_pointer (void)
9062 {
9063   return virtual_incoming_args_rtx;
9064 }
9065
9066 struct scratch_reg {
9067   rtx reg;
9068   bool saved;
9069 };
9070
9071 /* Return a short-lived scratch register for use on function entry.
9072    In 32-bit mode, it is valid only after the registers are saved
9073    in the prologue.  This register must be released by means of
9074    release_scratch_register_on_entry once it is dead.  */
9075
9076 static void
9077 get_scratch_register_on_entry (struct scratch_reg *sr)
9078 {
9079   int regno;
9080
9081   sr->saved = false;
9082
9083   if (TARGET_64BIT)
9084     {
9085       /* We always use R11 in 64-bit mode.  */
9086       regno = R11_REG;
9087     }
9088   else
9089     {
9090       tree decl = current_function_decl, fntype = TREE_TYPE (decl);
9091       bool fastcall_p
9092         = lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)) != NULL_TREE;
9093       bool static_chain_p = DECL_STATIC_CHAIN (decl);
9094       int regparm = ix86_function_regparm (fntype, decl);
9095       int drap_regno
9096         = crtl->drap_reg ? REGNO (crtl->drap_reg) : INVALID_REGNUM;
9097
9098       /* 'fastcall' sets regparm to 2, uses ecx/edx for arguments and eax
9099           for the static chain register.  */
9100       if ((regparm < 1 || (fastcall_p && !static_chain_p))
9101           && drap_regno != AX_REG)
9102         regno = AX_REG;
9103       else if (regparm < 2 && drap_regno != DX_REG)
9104         regno = DX_REG;
9105       /* ecx is the static chain register.  */
9106       else if (regparm < 3 && !fastcall_p && !static_chain_p
9107                && drap_regno != CX_REG)
9108         regno = CX_REG;
9109       else if (ix86_save_reg (BX_REG, true))
9110         regno = BX_REG;
9111       /* esi is the static chain register.  */
9112       else if (!(regparm == 3 && static_chain_p)
9113                && ix86_save_reg (SI_REG, true))
9114         regno = SI_REG;
9115       else if (ix86_save_reg (DI_REG, true))
9116         regno = DI_REG;
9117       else
9118         {
9119           regno = (drap_regno == AX_REG ? DX_REG : AX_REG);
9120           sr->saved = true;
9121         }
9122     }
9123
9124   sr->reg = gen_rtx_REG (Pmode, regno);
9125   if (sr->saved)
9126     {
9127       rtx insn = emit_insn (gen_push (sr->reg));
9128       RTX_FRAME_RELATED_P (insn) = 1;
9129     }
9130 }
9131
9132 /* Release a scratch register obtained from the preceding function.  */
9133
9134 static void
9135 release_scratch_register_on_entry (struct scratch_reg *sr)
9136 {
9137   if (sr->saved)
9138     {
9139       rtx x, insn = emit_insn (gen_pop (sr->reg));
9140
9141       /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
9142       RTX_FRAME_RELATED_P (insn) = 1;
9143       x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
9144       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9145       add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
9146     }
9147 }
9148
9149 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
9150
9151 /* Emit code to adjust the stack pointer by SIZE bytes while probing it.  */
9152
9153 static void
9154 ix86_adjust_stack_and_probe (const HOST_WIDE_INT size)
9155 {
9156   /* We skip the probe for the first interval + a small dope of 4 words and
9157      probe that many bytes past the specified size to maintain a protection
9158      area at the botton of the stack.  */
9159   const int dope = 4 * UNITS_PER_WORD;
9160   rtx size_rtx = GEN_INT (size);
9161
9162   /* See if we have a constant small number of probes to generate.  If so,
9163      that's the easy case.  The run-time loop is made up of 11 insns in the
9164      generic case while the compile-time loop is made up of 3+2*(n-1) insns
9165      for n # of intervals.  */
9166   if (size <= 5 * PROBE_INTERVAL)
9167     {
9168       HOST_WIDE_INT i, adjust;
9169       bool first_probe = true;
9170
9171       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
9172          values of N from 1 until it exceeds SIZE.  If only one probe is
9173          needed, this will not generate any code.  Then adjust and probe
9174          to PROBE_INTERVAL + SIZE.  */
9175       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9176         {
9177           if (first_probe)
9178             {
9179               adjust = 2 * PROBE_INTERVAL + dope;
9180               first_probe = false;
9181             }
9182           else
9183             adjust = PROBE_INTERVAL;
9184
9185           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9186                                   plus_constant (stack_pointer_rtx, -adjust)));
9187           emit_stack_probe (stack_pointer_rtx);
9188         }
9189
9190       if (first_probe)
9191         adjust = size + PROBE_INTERVAL + dope;
9192       else
9193         adjust = size + PROBE_INTERVAL - i;
9194
9195       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9196                               plus_constant (stack_pointer_rtx, -adjust)));
9197       emit_stack_probe (stack_pointer_rtx);
9198
9199       /* Adjust back to account for the additional first interval.  */
9200       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9201                               plus_constant (stack_pointer_rtx,
9202                                              PROBE_INTERVAL + dope)));
9203     }
9204
9205   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9206      extra careful with variables wrapping around because we might be at
9207      the very top (or the very bottom) of the address space and we have
9208      to be able to handle this case properly; in particular, we use an
9209      equality test for the loop condition.  */
9210   else
9211     {
9212       HOST_WIDE_INT rounded_size;
9213       struct scratch_reg sr;
9214
9215       get_scratch_register_on_entry (&sr);
9216
9217
9218       /* Step 1: round SIZE to the previous multiple of the interval.  */
9219
9220       rounded_size = size & -PROBE_INTERVAL;
9221
9222
9223       /* Step 2: compute initial and final value of the loop counter.  */
9224
9225       /* SP = SP_0 + PROBE_INTERVAL.  */
9226       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9227                               plus_constant (stack_pointer_rtx,
9228                                              - (PROBE_INTERVAL + dope))));
9229
9230       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
9231       emit_move_insn (sr.reg, GEN_INT (-rounded_size));
9232       emit_insn (gen_rtx_SET (VOIDmode, sr.reg,
9233                               gen_rtx_PLUS (Pmode, sr.reg,
9234                                             stack_pointer_rtx)));
9235
9236
9237       /* Step 3: the loop
9238
9239          while (SP != LAST_ADDR)
9240            {
9241              SP = SP + PROBE_INTERVAL
9242              probe at SP
9243            }
9244
9245          adjusts SP and probes to PROBE_INTERVAL + N * PROBE_INTERVAL for
9246          values of N from 1 until it is equal to ROUNDED_SIZE.  */
9247
9248       emit_insn (ix86_gen_adjust_stack_and_probe (sr.reg, sr.reg, size_rtx));
9249
9250
9251       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
9252          assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
9253
9254       if (size != rounded_size)
9255         {
9256           emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9257                                   plus_constant (stack_pointer_rtx,
9258                                                  rounded_size - size)));
9259           emit_stack_probe (stack_pointer_rtx);
9260         }
9261
9262       /* Adjust back to account for the additional first interval.  */
9263       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9264                               plus_constant (stack_pointer_rtx,
9265                                              PROBE_INTERVAL + dope)));
9266
9267       release_scratch_register_on_entry (&sr);
9268     }
9269
9270   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
9271   cfun->machine->fs.sp_offset += size;
9272
9273   /* Make sure nothing is scheduled before we are done.  */
9274   emit_insn (gen_blockage ());
9275 }
9276
9277 /* Adjust the stack pointer up to REG while probing it.  */
9278
9279 const char *
9280 output_adjust_stack_and_probe (rtx reg)
9281 {
9282   static int labelno = 0;
9283   char loop_lab[32], end_lab[32];
9284   rtx xops[2];
9285
9286   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9287   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9288
9289   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9290
9291   /* Jump to END_LAB if SP == LAST_ADDR.  */
9292   xops[0] = stack_pointer_rtx;
9293   xops[1] = reg;
9294   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9295   fputs ("\tje\t", asm_out_file);
9296   assemble_name_raw (asm_out_file, end_lab);
9297   fputc ('\n', asm_out_file);
9298
9299   /* SP = SP + PROBE_INTERVAL.  */
9300   xops[1] = GEN_INT (PROBE_INTERVAL);
9301   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9302
9303   /* Probe at SP.  */
9304   xops[1] = const0_rtx;
9305   output_asm_insn ("or%z0\t{%1, (%0)|DWORD PTR [%0], %1}", xops);
9306
9307   fprintf (asm_out_file, "\tjmp\t");
9308   assemble_name_raw (asm_out_file, loop_lab);
9309   fputc ('\n', asm_out_file);
9310
9311   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9312
9313   return "";
9314 }
9315
9316 /* Emit code to probe a range of stack addresses from FIRST to FIRST+SIZE,
9317    inclusive.  These are offsets from the current stack pointer.  */
9318
9319 static void
9320 ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
9321 {
9322   /* See if we have a constant small number of probes to generate.  If so,
9323      that's the easy case.  The run-time loop is made up of 7 insns in the
9324      generic case while the compile-time loop is made up of n insns for n #
9325      of intervals.  */
9326   if (size <= 7 * PROBE_INTERVAL)
9327     {
9328       HOST_WIDE_INT i;
9329
9330       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
9331          it exceeds SIZE.  If only one probe is needed, this will not
9332          generate any code.  Then probe at FIRST + SIZE.  */
9333       for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL)
9334         emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + i)));
9335
9336       emit_stack_probe (plus_constant (stack_pointer_rtx, -(first + size)));
9337     }
9338
9339   /* Otherwise, do the same as above, but in a loop.  Note that we must be
9340      extra careful with variables wrapping around because we might be at
9341      the very top (or the very bottom) of the address space and we have
9342      to be able to handle this case properly; in particular, we use an
9343      equality test for the loop condition.  */
9344   else
9345     {
9346       HOST_WIDE_INT rounded_size, last;
9347       struct scratch_reg sr;
9348
9349       get_scratch_register_on_entry (&sr);
9350
9351
9352       /* Step 1: round SIZE to the previous multiple of the interval.  */
9353
9354       rounded_size = size & -PROBE_INTERVAL;
9355
9356
9357       /* Step 2: compute initial and final value of the loop counter.  */
9358
9359       /* TEST_OFFSET = FIRST.  */
9360       emit_move_insn (sr.reg, GEN_INT (-first));
9361
9362       /* LAST_OFFSET = FIRST + ROUNDED_SIZE.  */
9363       last = first + rounded_size;
9364
9365
9366       /* Step 3: the loop
9367
9368          while (TEST_ADDR != LAST_ADDR)
9369            {
9370              TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
9371              probe at TEST_ADDR
9372            }
9373
9374          probes at FIRST + N * PROBE_INTERVAL for values of N from 1
9375          until it is equal to ROUNDED_SIZE.  */
9376
9377       emit_insn (ix86_gen_probe_stack_range (sr.reg, sr.reg, GEN_INT (-last)));
9378
9379
9380       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
9381          that SIZE is equal to ROUNDED_SIZE.  */
9382
9383       if (size != rounded_size)
9384         emit_stack_probe (plus_constant (gen_rtx_PLUS (Pmode,
9385                                                        stack_pointer_rtx,
9386                                                        sr.reg),
9387                                          rounded_size - size));
9388
9389       release_scratch_register_on_entry (&sr);
9390     }
9391
9392   /* Make sure nothing is scheduled before we are done.  */
9393   emit_insn (gen_blockage ());
9394 }
9395
9396 /* Probe a range of stack addresses from REG to END, inclusive.  These are
9397    offsets from the current stack pointer.  */
9398
9399 const char *
9400 output_probe_stack_range (rtx reg, rtx end)
9401 {
9402   static int labelno = 0;
9403   char loop_lab[32], end_lab[32];
9404   rtx xops[3];
9405
9406   ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno);
9407   ASM_GENERATE_INTERNAL_LABEL (end_lab, "LPSRE", labelno++);
9408
9409   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
9410
9411   /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
9412   xops[0] = reg;
9413   xops[1] = end;
9414   output_asm_insn ("cmp%z0\t{%1, %0|%0, %1}", xops);
9415   fputs ("\tje\t", asm_out_file);
9416   assemble_name_raw (asm_out_file, end_lab);
9417   fputc ('\n', asm_out_file);
9418
9419   /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
9420   xops[1] = GEN_INT (PROBE_INTERVAL);
9421   output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops);
9422
9423   /* Probe at TEST_ADDR.  */
9424   xops[0] = stack_pointer_rtx;
9425   xops[1] = reg;
9426   xops[2] = const0_rtx;
9427   output_asm_insn ("or%z0\t{%2, (%0,%1)|DWORD PTR [%0+%1], %2}", xops);
9428
9429   fprintf (asm_out_file, "\tjmp\t");
9430   assemble_name_raw (asm_out_file, loop_lab);
9431   fputc ('\n', asm_out_file);
9432
9433   ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, end_lab);
9434
9435   return "";
9436 }
9437
9438 /* Finalize stack_realign_needed flag, which will guide prologue/epilogue
9439    to be generated in correct form.  */
9440 static void
9441 ix86_finalize_stack_realign_flags (void)
9442 {
9443   /* Check if stack realign is really needed after reload, and
9444      stores result in cfun */
9445   unsigned int incoming_stack_boundary
9446     = (crtl->parm_stack_boundary > ix86_incoming_stack_boundary
9447        ? crtl->parm_stack_boundary : ix86_incoming_stack_boundary);
9448   unsigned int stack_realign = (incoming_stack_boundary
9449                                 < (current_function_is_leaf
9450                                    ? crtl->max_used_stack_slot_alignment
9451                                    : crtl->stack_alignment_needed));
9452
9453   if (crtl->stack_realign_finalized)
9454     {
9455       /* After stack_realign_needed is finalized, we can't no longer
9456          change it.  */
9457       gcc_assert (crtl->stack_realign_needed == stack_realign);
9458     }
9459   else
9460     {
9461       crtl->stack_realign_needed = stack_realign;
9462       crtl->stack_realign_finalized = true;
9463     }
9464 }
9465
9466 /* Expand the prologue into a bunch of separate insns.  */
9467
9468 void
9469 ix86_expand_prologue (void)
9470 {
9471   struct machine_function *m = cfun->machine;
9472   rtx insn, t;
9473   bool pic_reg_used;
9474   struct ix86_frame frame;
9475   HOST_WIDE_INT allocate;
9476   bool int_registers_saved;
9477
9478   ix86_finalize_stack_realign_flags ();
9479
9480   /* DRAP should not coexist with stack_realign_fp */
9481   gcc_assert (!(crtl->drap_reg && stack_realign_fp));
9482
9483   memset (&m->fs, 0, sizeof (m->fs));
9484
9485   /* Initialize CFA state for before the prologue.  */
9486   m->fs.cfa_reg = stack_pointer_rtx;
9487   m->fs.cfa_offset = INCOMING_FRAME_SP_OFFSET;
9488
9489   /* Track SP offset to the CFA.  We continue tracking this after we've
9490      swapped the CFA register away from SP.  In the case of re-alignment
9491      this is fudged; we're interested to offsets within the local frame.  */
9492   m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
9493   m->fs.sp_valid = true;
9494
9495   ix86_compute_frame_layout (&frame);
9496
9497   if (!TARGET_64BIT && ix86_function_ms_hook_prologue (current_function_decl))
9498     {
9499       /* We should have already generated an error for any use of
9500          ms_hook on a nested function.  */
9501       gcc_checking_assert (!ix86_static_chain_on_stack);
9502
9503       /* Check if profiling is active and we shall use profiling before
9504          prologue variant. If so sorry.  */
9505       if (crtl->profile && flag_fentry != 0)
9506         sorry ("ms_hook_prologue attribute isn't compatible with -mfentry for 32-bit");
9507
9508       /* In ix86_asm_output_function_label we emitted:
9509          8b ff     movl.s %edi,%edi
9510          55        push   %ebp
9511          8b ec     movl.s %esp,%ebp
9512
9513          This matches the hookable function prologue in Win32 API
9514          functions in Microsoft Windows XP Service Pack 2 and newer.
9515          Wine uses this to enable Windows apps to hook the Win32 API
9516          functions provided by Wine.
9517
9518          What that means is that we've already set up the frame pointer.  */
9519
9520       if (frame_pointer_needed
9521           && !(crtl->drap_reg && crtl->stack_realign_needed))
9522         {
9523           rtx push, mov;
9524
9525           /* We've decided to use the frame pointer already set up.
9526              Describe this to the unwinder by pretending that both
9527              push and mov insns happen right here.
9528
9529              Putting the unwind info here at the end of the ms_hook
9530              is done so that we can make absolutely certain we get
9531              the required byte sequence at the start of the function,
9532              rather than relying on an assembler that can produce
9533              the exact encoding required.
9534
9535              However it does mean (in the unpatched case) that we have
9536              a 1 insn window where the asynchronous unwind info is
9537              incorrect.  However, if we placed the unwind info at
9538              its correct location we would have incorrect unwind info
9539              in the patched case.  Which is probably all moot since
9540              I don't expect Wine generates dwarf2 unwind info for the
9541              system libraries that use this feature.  */
9542
9543           insn = emit_insn (gen_blockage ());
9544
9545           push = gen_push (hard_frame_pointer_rtx);
9546           mov = gen_rtx_SET (VOIDmode, hard_frame_pointer_rtx,
9547                              stack_pointer_rtx);
9548           RTX_FRAME_RELATED_P (push) = 1;
9549           RTX_FRAME_RELATED_P (mov) = 1;
9550
9551           RTX_FRAME_RELATED_P (insn) = 1;
9552           add_reg_note (insn, REG_FRAME_RELATED_EXPR,
9553                         gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, push, mov)));
9554
9555           /* Note that gen_push incremented m->fs.cfa_offset, even
9556              though we didn't emit the push insn here.  */
9557           m->fs.cfa_reg = hard_frame_pointer_rtx;
9558           m->fs.fp_offset = m->fs.cfa_offset;
9559           m->fs.fp_valid = true;
9560         }
9561       else
9562         {
9563           /* The frame pointer is not needed so pop %ebp again.
9564              This leaves us with a pristine state.  */
9565           emit_insn (gen_pop (hard_frame_pointer_rtx));
9566         }
9567     }
9568
9569   /* The first insn of a function that accepts its static chain on the
9570      stack is to push the register that would be filled in by a direct
9571      call.  This insn will be skipped by the trampoline.  */
9572   else if (ix86_static_chain_on_stack)
9573     {
9574       insn = emit_insn (gen_push (ix86_static_chain (cfun->decl, false)));
9575       emit_insn (gen_blockage ());
9576
9577       /* We don't want to interpret this push insn as a register save,
9578          only as a stack adjustment.  The real copy of the register as
9579          a save will be done later, if needed.  */
9580       t = plus_constant (stack_pointer_rtx, -UNITS_PER_WORD);
9581       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
9582       add_reg_note (insn, REG_CFA_ADJUST_CFA, t);
9583       RTX_FRAME_RELATED_P (insn) = 1;
9584     }
9585
9586   /* Emit prologue code to adjust stack alignment and setup DRAP, in case
9587      of DRAP is needed and stack realignment is really needed after reload */
9588   if (stack_realign_drap)
9589     {
9590       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
9591
9592       /* Only need to push parameter pointer reg if it is caller saved.  */
9593       if (!call_used_regs[REGNO (crtl->drap_reg)])
9594         {
9595           /* Push arg pointer reg */
9596           insn = emit_insn (gen_push (crtl->drap_reg));
9597           RTX_FRAME_RELATED_P (insn) = 1;
9598         }
9599
9600       /* Grab the argument pointer.  */
9601       t = plus_constant (stack_pointer_rtx, m->fs.sp_offset);
9602       insn = emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
9603       RTX_FRAME_RELATED_P (insn) = 1;
9604       m->fs.cfa_reg = crtl->drap_reg;
9605       m->fs.cfa_offset = 0;
9606
9607       /* Align the stack.  */
9608       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
9609                                         stack_pointer_rtx,
9610                                         GEN_INT (-align_bytes)));
9611       RTX_FRAME_RELATED_P (insn) = 1;
9612
9613       /* Replicate the return address on the stack so that return
9614          address can be reached via (argp - 1) slot.  This is needed
9615          to implement macro RETURN_ADDR_RTX and intrinsic function
9616          expand_builtin_return_addr etc.  */
9617       t = plus_constant (crtl->drap_reg, -UNITS_PER_WORD);
9618       t = gen_frame_mem (Pmode, t);
9619       insn = emit_insn (gen_push (t));
9620       RTX_FRAME_RELATED_P (insn) = 1;
9621
9622       /* For the purposes of frame and register save area addressing,
9623          we've started over with a new frame.  */
9624       m->fs.sp_offset = INCOMING_FRAME_SP_OFFSET;
9625       m->fs.realigned = true;
9626     }
9627
9628   if (frame_pointer_needed && !m->fs.fp_valid)
9629     {
9630       /* Note: AT&T enter does NOT have reversed args.  Enter is probably
9631          slower on all targets.  Also sdb doesn't like it.  */
9632       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
9633       RTX_FRAME_RELATED_P (insn) = 1;
9634
9635       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
9636       RTX_FRAME_RELATED_P (insn) = 1;
9637
9638       if (m->fs.cfa_reg == stack_pointer_rtx)
9639         m->fs.cfa_reg = hard_frame_pointer_rtx;
9640       gcc_assert (m->fs.sp_offset == frame.hard_frame_pointer_offset);
9641       m->fs.fp_offset = m->fs.sp_offset;
9642       m->fs.fp_valid = true;
9643     }
9644
9645   int_registers_saved = (frame.nregs == 0);
9646
9647   if (!int_registers_saved)
9648     {
9649       /* If saving registers via PUSH, do so now.  */
9650       if (!frame.save_regs_using_mov)
9651         {
9652           ix86_emit_save_regs ();
9653           int_registers_saved = true;
9654           gcc_assert (m->fs.sp_offset == frame.reg_save_offset);
9655         }
9656
9657       /* When using red zone we may start register saving before allocating
9658          the stack frame saving one cycle of the prologue.  However, avoid
9659          doing this if we have to probe the stack; at least on x86_64 the
9660          stack probe can turn into a call that clobbers a red zone location. */
9661       else if (ix86_using_red_zone ()
9662                && (! TARGET_STACK_PROBE
9663                    || frame.stack_pointer_offset < CHECK_STACK_LIMIT))
9664         {
9665           ix86_emit_save_regs_using_mov (frame.reg_save_offset);
9666           int_registers_saved = true;
9667         }
9668     }
9669
9670   if (stack_realign_fp)
9671     {
9672       int align_bytes = crtl->stack_alignment_needed / BITS_PER_UNIT;
9673       gcc_assert (align_bytes > MIN_STACK_BOUNDARY / BITS_PER_UNIT);
9674
9675       /* The computation of the size of the re-aligned stack frame means
9676          that we must allocate the size of the register save area before
9677          performing the actual alignment.  Otherwise we cannot guarantee
9678          that there's enough storage above the realignment point.  */
9679       if (m->fs.sp_offset != frame.sse_reg_save_offset)
9680         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9681                                    GEN_INT (m->fs.sp_offset
9682                                             - frame.sse_reg_save_offset),
9683                                    -1, false);
9684
9685       /* Align the stack.  */
9686       insn = emit_insn (ix86_gen_andsp (stack_pointer_rtx,
9687                                         stack_pointer_rtx,
9688                                         GEN_INT (-align_bytes)));
9689
9690       /* For the purposes of register save area addressing, the stack
9691          pointer is no longer valid.  As for the value of sp_offset,
9692          see ix86_compute_frame_layout, which we need to match in order
9693          to pass verification of stack_pointer_offset at the end.  */
9694       m->fs.sp_offset = (m->fs.sp_offset + align_bytes) & -align_bytes;
9695       m->fs.sp_valid = false;
9696     }
9697
9698   allocate = frame.stack_pointer_offset - m->fs.sp_offset;
9699
9700   if (flag_stack_usage)
9701     {
9702       /* We start to count from ARG_POINTER.  */
9703       HOST_WIDE_INT stack_size = frame.stack_pointer_offset;
9704
9705       /* If it was realigned, take into account the fake frame.  */
9706       if (stack_realign_drap)
9707         {
9708           if (ix86_static_chain_on_stack)
9709             stack_size += UNITS_PER_WORD;
9710
9711           if (!call_used_regs[REGNO (crtl->drap_reg)])
9712             stack_size += UNITS_PER_WORD;
9713
9714           /* This over-estimates by 1 minimal-stack-alignment-unit but
9715              mitigates that by counting in the new return address slot.  */
9716           current_function_dynamic_stack_size
9717             += crtl->stack_alignment_needed / BITS_PER_UNIT;
9718         }
9719
9720       current_function_static_stack_size = stack_size;
9721     }
9722
9723   /* The stack has already been decremented by the instruction calling us
9724      so we need to probe unconditionally to preserve the protection area.  */
9725   if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
9726     {
9727       /* We expect the registers to be saved when probes are used.  */
9728       gcc_assert (int_registers_saved);
9729
9730       if (STACK_CHECK_MOVING_SP)
9731         {
9732           ix86_adjust_stack_and_probe (allocate);
9733           allocate = 0;
9734         }
9735       else
9736         {
9737           HOST_WIDE_INT size = allocate;
9738
9739           if (TARGET_64BIT && size >= (HOST_WIDE_INT) 0x80000000)
9740             size = 0x80000000 - STACK_CHECK_PROTECT - 1;
9741
9742           if (TARGET_STACK_PROBE)
9743             ix86_emit_probe_stack_range (0, size + STACK_CHECK_PROTECT);
9744           else
9745             ix86_emit_probe_stack_range (STACK_CHECK_PROTECT, size);
9746         }
9747     }
9748
9749   if (allocate == 0)
9750     ;
9751   else if (!ix86_target_stack_probe ()
9752            || frame.stack_pointer_offset < CHECK_STACK_LIMIT)
9753     {
9754       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
9755                                  GEN_INT (-allocate), -1,
9756                                  m->fs.cfa_reg == stack_pointer_rtx);
9757     }
9758   else
9759     {
9760       rtx eax = gen_rtx_REG (Pmode, AX_REG);
9761       rtx r10 = NULL;
9762       rtx (*adjust_stack_insn)(rtx, rtx, rtx);
9763
9764       bool eax_live = false;
9765       bool r10_live = false;
9766
9767       if (TARGET_64BIT)
9768         r10_live = (DECL_STATIC_CHAIN (current_function_decl) != 0);
9769       if (!TARGET_64BIT_MS_ABI)
9770         eax_live = ix86_eax_live_at_start_p ();
9771
9772       if (eax_live)
9773         {
9774           emit_insn (gen_push (eax));
9775           allocate -= UNITS_PER_WORD;
9776         }
9777       if (r10_live)
9778         {
9779           r10 = gen_rtx_REG (Pmode, R10_REG);
9780           emit_insn (gen_push (r10));
9781           allocate -= UNITS_PER_WORD;
9782         }
9783
9784       emit_move_insn (eax, GEN_INT (allocate));
9785       emit_insn (ix86_gen_allocate_stack_worker (eax, eax));
9786
9787       /* Use the fact that AX still contains ALLOCATE.  */
9788       adjust_stack_insn = (TARGET_64BIT
9789                            ? gen_pro_epilogue_adjust_stack_di_sub
9790                            : gen_pro_epilogue_adjust_stack_si_sub);
9791
9792       insn = emit_insn (adjust_stack_insn (stack_pointer_rtx,
9793                                            stack_pointer_rtx, eax));
9794
9795       if (m->fs.cfa_reg == stack_pointer_rtx)
9796         {
9797           m->fs.cfa_offset += allocate;
9798
9799           RTX_FRAME_RELATED_P (insn) = 1;
9800           add_reg_note (insn, REG_CFA_ADJUST_CFA,
9801                         gen_rtx_SET (VOIDmode, stack_pointer_rtx,
9802                                      plus_constant (stack_pointer_rtx,
9803                                                     -allocate)));
9804         }
9805       m->fs.sp_offset += allocate;
9806
9807       if (r10_live && eax_live)
9808         {
9809           t = choose_baseaddr (m->fs.sp_offset - allocate);
9810           emit_move_insn (r10, gen_frame_mem (Pmode, t));
9811           t = choose_baseaddr (m->fs.sp_offset - allocate - UNITS_PER_WORD);
9812           emit_move_insn (eax, gen_frame_mem (Pmode, t));
9813         }
9814       else if (eax_live || r10_live)
9815         {
9816           t = choose_baseaddr (m->fs.sp_offset - allocate);
9817           emit_move_insn ((eax_live ? eax : r10), gen_frame_mem (Pmode, t));
9818         }
9819     }
9820   gcc_assert (m->fs.sp_offset == frame.stack_pointer_offset);
9821
9822   if (!int_registers_saved)
9823     ix86_emit_save_regs_using_mov (frame.reg_save_offset);
9824   if (frame.nsseregs)
9825     ix86_emit_save_sse_regs_using_mov (frame.sse_reg_save_offset);
9826
9827   pic_reg_used = false;
9828   if (pic_offset_table_rtx
9829       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
9830           || crtl->profile))
9831     {
9832       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
9833
9834       if (alt_pic_reg_used != INVALID_REGNUM)
9835         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
9836
9837       pic_reg_used = true;
9838     }
9839
9840   if (pic_reg_used)
9841     {
9842       if (TARGET_64BIT)
9843         {
9844           if (ix86_cmodel == CM_LARGE_PIC)
9845             {
9846               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
9847               rtx label = gen_label_rtx ();
9848               emit_label (label);
9849               LABEL_PRESERVE_P (label) = 1;
9850               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
9851               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
9852               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
9853               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
9854                                             pic_offset_table_rtx, tmp_reg));
9855             }
9856           else
9857             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
9858         }
9859       else
9860         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
9861     }
9862
9863   /* In the pic_reg_used case, make sure that the got load isn't deleted
9864      when mcount needs it.  Blockage to avoid call movement across mcount
9865      call is emitted in generic code after the NOTE_INSN_PROLOGUE_END
9866      note.  */
9867   if (crtl->profile && !flag_fentry && pic_reg_used)
9868     emit_insn (gen_prologue_use (pic_offset_table_rtx));
9869
9870   if (crtl->drap_reg && !crtl->stack_realign_needed)
9871     {
9872       /* vDRAP is setup but after reload it turns out stack realign
9873          isn't necessary, here we will emit prologue to setup DRAP
9874          without stack realign adjustment */
9875       t = choose_baseaddr (0);
9876       emit_insn (gen_rtx_SET (VOIDmode, crtl->drap_reg, t));
9877     }
9878
9879   /* Prevent instructions from being scheduled into register save push
9880      sequence when access to the redzone area is done through frame pointer.
9881      The offset between the frame pointer and the stack pointer is calculated
9882      relative to the value of the stack pointer at the end of the function
9883      prologue, and moving instructions that access redzone area via frame
9884      pointer inside push sequence violates this assumption.  */
9885   if (frame_pointer_needed && frame.red_zone_size)
9886     emit_insn (gen_memory_blockage ());
9887
9888   /* Emit cld instruction if stringops are used in the function.  */
9889   if (TARGET_CLD && ix86_current_function_needs_cld)
9890     emit_insn (gen_cld ());
9891 }
9892
9893 /* Emit code to restore REG using a POP insn.  */
9894
9895 static void
9896 ix86_emit_restore_reg_using_pop (rtx reg)
9897 {
9898   struct machine_function *m = cfun->machine;
9899   rtx insn = emit_insn (gen_pop (reg));
9900
9901   ix86_add_cfa_restore_note (insn, reg, m->fs.sp_offset);
9902   m->fs.sp_offset -= UNITS_PER_WORD;
9903
9904   if (m->fs.cfa_reg == crtl->drap_reg
9905       && REGNO (reg) == REGNO (crtl->drap_reg))
9906     {
9907       /* Previously we'd represented the CFA as an expression
9908          like *(%ebp - 8).  We've just popped that value from
9909          the stack, which means we need to reset the CFA to
9910          the drap register.  This will remain until we restore
9911          the stack pointer.  */
9912       add_reg_note (insn, REG_CFA_DEF_CFA, reg);
9913       RTX_FRAME_RELATED_P (insn) = 1;
9914
9915       /* This means that the DRAP register is valid for addressing too.  */
9916       m->fs.drap_valid = true;
9917       return;
9918     }
9919
9920   if (m->fs.cfa_reg == stack_pointer_rtx)
9921     {
9922       rtx x = plus_constant (stack_pointer_rtx, UNITS_PER_WORD);
9923       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
9924       add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
9925       RTX_FRAME_RELATED_P (insn) = 1;
9926
9927       m->fs.cfa_offset -= UNITS_PER_WORD;
9928     }
9929
9930   /* When the frame pointer is the CFA, and we pop it, we are
9931      swapping back to the stack pointer as the CFA.  This happens
9932      for stack frames that don't allocate other data, so we assume
9933      the stack pointer is now pointing at the return address, i.e.
9934      the function entry state, which makes the offset be 1 word.  */
9935   if (reg == hard_frame_pointer_rtx)
9936     {
9937       m->fs.fp_valid = false;
9938       if (m->fs.cfa_reg == hard_frame_pointer_rtx)
9939         {
9940           m->fs.cfa_reg = stack_pointer_rtx;
9941           m->fs.cfa_offset -= UNITS_PER_WORD;
9942
9943           add_reg_note (insn, REG_CFA_DEF_CFA,
9944                         gen_rtx_PLUS (Pmode, stack_pointer_rtx,
9945                                       GEN_INT (m->fs.cfa_offset)));
9946           RTX_FRAME_RELATED_P (insn) = 1;
9947         }
9948     }
9949 }
9950
9951 /* Emit code to restore saved registers using POP insns.  */
9952
9953 static void
9954 ix86_emit_restore_regs_using_pop (void)
9955 {
9956   unsigned int regno;
9957
9958   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
9959     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, false))
9960       ix86_emit_restore_reg_using_pop (gen_rtx_REG (Pmode, regno));
9961 }
9962
9963 /* Emit code and notes for the LEAVE instruction.  */
9964
9965 static void
9966 ix86_emit_leave (void)
9967 {
9968   struct machine_function *m = cfun->machine;
9969   rtx insn = emit_insn (ix86_gen_leave ());
9970
9971   ix86_add_queued_cfa_restore_notes (insn);
9972
9973   gcc_assert (m->fs.fp_valid);
9974   m->fs.sp_valid = true;
9975   m->fs.sp_offset = m->fs.fp_offset - UNITS_PER_WORD;
9976   m->fs.fp_valid = false;
9977
9978   if (m->fs.cfa_reg == hard_frame_pointer_rtx)
9979     {
9980       m->fs.cfa_reg = stack_pointer_rtx;
9981       m->fs.cfa_offset = m->fs.sp_offset;
9982
9983       add_reg_note (insn, REG_CFA_DEF_CFA,
9984                     plus_constant (stack_pointer_rtx, m->fs.sp_offset));
9985       RTX_FRAME_RELATED_P (insn) = 1;
9986       ix86_add_cfa_restore_note (insn, hard_frame_pointer_rtx,
9987                                  m->fs.fp_offset);
9988     }
9989 }
9990
9991 /* Emit code to restore saved registers using MOV insns.
9992    First register is restored from CFA - CFA_OFFSET.  */
9993 static void
9994 ix86_emit_restore_regs_using_mov (HOST_WIDE_INT cfa_offset,
9995                                   int maybe_eh_return)
9996 {
9997   struct machine_function *m = cfun->machine;
9998   unsigned int regno;
9999
10000   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10001     if (!SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10002       {
10003         rtx reg = gen_rtx_REG (Pmode, regno);
10004         rtx insn, mem;
10005         
10006         mem = choose_baseaddr (cfa_offset);
10007         mem = gen_frame_mem (Pmode, mem);
10008         insn = emit_move_insn (reg, mem);
10009
10010         if (m->fs.cfa_reg == crtl->drap_reg && regno == REGNO (crtl->drap_reg))
10011           {
10012             /* Previously we'd represented the CFA as an expression
10013                like *(%ebp - 8).  We've just popped that value from
10014                the stack, which means we need to reset the CFA to
10015                the drap register.  This will remain until we restore
10016                the stack pointer.  */
10017             add_reg_note (insn, REG_CFA_DEF_CFA, reg);
10018             RTX_FRAME_RELATED_P (insn) = 1;
10019
10020             /* This means that the DRAP register is valid for addressing.  */
10021             m->fs.drap_valid = true;
10022           }
10023         else
10024           ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10025
10026         cfa_offset -= UNITS_PER_WORD;
10027       }
10028 }
10029
10030 /* Emit code to restore saved registers using MOV insns.
10031    First register is restored from CFA - CFA_OFFSET.  */
10032 static void
10033 ix86_emit_restore_sse_regs_using_mov (HOST_WIDE_INT cfa_offset,
10034                                       int maybe_eh_return)
10035 {
10036   unsigned int regno;
10037
10038   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
10039     if (SSE_REGNO_P (regno) && ix86_save_reg (regno, maybe_eh_return))
10040       {
10041         rtx reg = gen_rtx_REG (V4SFmode, regno);
10042         rtx mem;
10043
10044         mem = choose_baseaddr (cfa_offset);
10045         mem = gen_rtx_MEM (V4SFmode, mem);
10046         set_mem_align (mem, 128);
10047         emit_move_insn (reg, mem);
10048
10049         ix86_add_cfa_restore_note (NULL_RTX, reg, cfa_offset);
10050
10051         cfa_offset -= 16;
10052       }
10053 }
10054
10055 /* Restore function stack, frame, and registers.  */
10056
10057 void
10058 ix86_expand_epilogue (int style)
10059 {
10060   struct machine_function *m = cfun->machine;
10061   struct machine_frame_state frame_state_save = m->fs;
10062   struct ix86_frame frame;
10063   bool restore_regs_via_mov;
10064   bool using_drap;
10065
10066   ix86_finalize_stack_realign_flags ();
10067   ix86_compute_frame_layout (&frame);
10068
10069   m->fs.sp_valid = (!frame_pointer_needed
10070                     || (current_function_sp_is_unchanging
10071                         && !stack_realign_fp));
10072   gcc_assert (!m->fs.sp_valid
10073               || m->fs.sp_offset == frame.stack_pointer_offset);
10074
10075   /* The FP must be valid if the frame pointer is present.  */
10076   gcc_assert (frame_pointer_needed == m->fs.fp_valid);
10077   gcc_assert (!m->fs.fp_valid
10078               || m->fs.fp_offset == frame.hard_frame_pointer_offset);
10079
10080   /* We must have *some* valid pointer to the stack frame.  */
10081   gcc_assert (m->fs.sp_valid || m->fs.fp_valid);
10082
10083   /* The DRAP is never valid at this point.  */
10084   gcc_assert (!m->fs.drap_valid);
10085
10086   /* See the comment about red zone and frame
10087      pointer usage in ix86_expand_prologue.  */
10088   if (frame_pointer_needed && frame.red_zone_size)
10089     emit_insn (gen_memory_blockage ());
10090
10091   using_drap = crtl->drap_reg && crtl->stack_realign_needed;
10092   gcc_assert (!using_drap || m->fs.cfa_reg == crtl->drap_reg);
10093
10094   /* Determine the CFA offset of the end of the red-zone.  */
10095   m->fs.red_zone_offset = 0;
10096   if (ix86_using_red_zone () && crtl->args.pops_args < 65536)
10097     {
10098       /* The red-zone begins below the return address.  */
10099       m->fs.red_zone_offset = RED_ZONE_SIZE + UNITS_PER_WORD;
10100
10101       /* When the register save area is in the aligned portion of
10102          the stack, determine the maximum runtime displacement that
10103          matches up with the aligned frame.  */
10104       if (stack_realign_drap)
10105         m->fs.red_zone_offset -= (crtl->stack_alignment_needed / BITS_PER_UNIT
10106                                   + UNITS_PER_WORD);
10107     }
10108
10109   /* Special care must be taken for the normal return case of a function
10110      using eh_return: the eax and edx registers are marked as saved, but
10111      not restored along this path.  Adjust the save location to match.  */
10112   if (crtl->calls_eh_return && style != 2)
10113     frame.reg_save_offset -= 2 * UNITS_PER_WORD;
10114
10115   /* If we're only restoring one register and sp is not valid then
10116      using a move instruction to restore the register since it's
10117      less work than reloading sp and popping the register.  */
10118   if (!m->fs.sp_valid && frame.nregs <= 1)
10119     restore_regs_via_mov = true;
10120   /* EH_RETURN requires the use of moves to function properly.  */
10121   else if (crtl->calls_eh_return)
10122     restore_regs_via_mov = true;
10123   else if (TARGET_EPILOGUE_USING_MOVE
10124            && cfun->machine->use_fast_prologue_epilogue
10125            && (frame.nregs > 1
10126                || m->fs.sp_offset != frame.reg_save_offset))
10127     restore_regs_via_mov = true;
10128   else if (frame_pointer_needed
10129            && !frame.nregs
10130            && m->fs.sp_offset != frame.reg_save_offset)
10131     restore_regs_via_mov = true;
10132   else if (frame_pointer_needed
10133            && TARGET_USE_LEAVE
10134            && cfun->machine->use_fast_prologue_epilogue
10135            && frame.nregs == 1)
10136     restore_regs_via_mov = true;
10137   else
10138     restore_regs_via_mov = false;
10139
10140   if (restore_regs_via_mov || frame.nsseregs)
10141     {
10142       /* Ensure that the entire register save area is addressable via
10143          the stack pointer, if we will restore via sp.  */
10144       if (TARGET_64BIT
10145           && m->fs.sp_offset > 0x7fffffff
10146           && !(m->fs.fp_valid || m->fs.drap_valid)
10147           && (frame.nsseregs + frame.nregs) != 0)
10148         {
10149           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10150                                      GEN_INT (m->fs.sp_offset
10151                                               - frame.sse_reg_save_offset),
10152                                      style,
10153                                      m->fs.cfa_reg == stack_pointer_rtx);
10154         }
10155     }
10156
10157   /* If there are any SSE registers to restore, then we have to do it
10158      via moves, since there's obviously no pop for SSE regs.  */
10159   if (frame.nsseregs)
10160     ix86_emit_restore_sse_regs_using_mov (frame.sse_reg_save_offset,
10161                                           style == 2);
10162
10163   if (restore_regs_via_mov)
10164     {
10165       rtx t;
10166
10167       if (frame.nregs)
10168         ix86_emit_restore_regs_using_mov (frame.reg_save_offset, style == 2);
10169
10170       /* eh_return epilogues need %ecx added to the stack pointer.  */
10171       if (style == 2)
10172         {
10173           rtx insn, sa = EH_RETURN_STACKADJ_RTX;
10174
10175           /* Stack align doesn't work with eh_return.  */
10176           gcc_assert (!stack_realign_drap);
10177           /* Neither does regparm nested functions.  */
10178           gcc_assert (!ix86_static_chain_on_stack);
10179
10180           if (frame_pointer_needed)
10181             {
10182               t = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
10183               t = plus_constant (t, m->fs.fp_offset - UNITS_PER_WORD);
10184               emit_insn (gen_rtx_SET (VOIDmode, sa, t));
10185
10186               t = gen_frame_mem (Pmode, hard_frame_pointer_rtx);
10187               insn = emit_move_insn (hard_frame_pointer_rtx, t);
10188
10189               /* Note that we use SA as a temporary CFA, as the return
10190                  address is at the proper place relative to it.  We
10191                  pretend this happens at the FP restore insn because
10192                  prior to this insn the FP would be stored at the wrong
10193                  offset relative to SA, and after this insn we have no
10194                  other reasonable register to use for the CFA.  We don't
10195                  bother resetting the CFA to the SP for the duration of
10196                  the return insn.  */
10197               add_reg_note (insn, REG_CFA_DEF_CFA,
10198                             plus_constant (sa, UNITS_PER_WORD));
10199               ix86_add_queued_cfa_restore_notes (insn);
10200               add_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx);
10201               RTX_FRAME_RELATED_P (insn) = 1;
10202
10203               m->fs.cfa_reg = sa;
10204               m->fs.cfa_offset = UNITS_PER_WORD;
10205               m->fs.fp_valid = false;
10206
10207               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
10208                                          const0_rtx, style, false);
10209             }
10210           else
10211             {
10212               t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
10213               t = plus_constant (t, m->fs.sp_offset - UNITS_PER_WORD);
10214               insn = emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, t));
10215               ix86_add_queued_cfa_restore_notes (insn);
10216
10217               gcc_assert (m->fs.cfa_reg == stack_pointer_rtx);
10218               if (m->fs.cfa_offset != UNITS_PER_WORD)
10219                 {
10220                   m->fs.cfa_offset = UNITS_PER_WORD;
10221                   add_reg_note (insn, REG_CFA_DEF_CFA,
10222                                 plus_constant (stack_pointer_rtx,
10223                                                UNITS_PER_WORD));
10224                   RTX_FRAME_RELATED_P (insn) = 1;
10225                 }
10226             }
10227           m->fs.sp_offset = UNITS_PER_WORD;
10228           m->fs.sp_valid = true;
10229         }
10230     }
10231   else
10232     {
10233       /* First step is to deallocate the stack frame so that we can
10234          pop the registers.  */
10235       if (!m->fs.sp_valid)
10236         {
10237           pro_epilogue_adjust_stack (stack_pointer_rtx, hard_frame_pointer_rtx,
10238                                      GEN_INT (m->fs.fp_offset
10239                                               - frame.reg_save_offset),
10240                                      style, false);
10241         }
10242       else if (m->fs.sp_offset != frame.reg_save_offset)
10243         {
10244           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10245                                      GEN_INT (m->fs.sp_offset
10246                                               - frame.reg_save_offset),
10247                                      style,
10248                                      m->fs.cfa_reg == stack_pointer_rtx);
10249         }
10250
10251       ix86_emit_restore_regs_using_pop ();
10252     }
10253
10254   /* If we used a stack pointer and haven't already got rid of it,
10255      then do so now.  */
10256   if (m->fs.fp_valid)
10257     {
10258       /* If the stack pointer is valid and pointing at the frame
10259          pointer store address, then we only need a pop.  */
10260       if (m->fs.sp_valid && m->fs.sp_offset == frame.hard_frame_pointer_offset)
10261         ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10262       /* Leave results in shorter dependency chains on CPUs that are
10263          able to grok it fast.  */
10264       else if (TARGET_USE_LEAVE
10265                || optimize_function_for_size_p (cfun)
10266                || !cfun->machine->use_fast_prologue_epilogue)
10267         ix86_emit_leave ();
10268       else
10269         {
10270           pro_epilogue_adjust_stack (stack_pointer_rtx,
10271                                      hard_frame_pointer_rtx,
10272                                      const0_rtx, style, !using_drap);
10273           ix86_emit_restore_reg_using_pop (hard_frame_pointer_rtx);
10274         }
10275     }
10276
10277   if (using_drap)
10278     {
10279       int param_ptr_offset = UNITS_PER_WORD;
10280       rtx insn;
10281
10282       gcc_assert (stack_realign_drap);
10283
10284       if (ix86_static_chain_on_stack)
10285         param_ptr_offset += UNITS_PER_WORD;
10286       if (!call_used_regs[REGNO (crtl->drap_reg)])
10287         param_ptr_offset += UNITS_PER_WORD;
10288
10289       insn = emit_insn (gen_rtx_SET
10290                         (VOIDmode, stack_pointer_rtx,
10291                          gen_rtx_PLUS (Pmode,
10292                                        crtl->drap_reg,
10293                                        GEN_INT (-param_ptr_offset))));
10294       m->fs.cfa_reg = stack_pointer_rtx;
10295       m->fs.cfa_offset = param_ptr_offset;
10296       m->fs.sp_offset = param_ptr_offset;
10297       m->fs.realigned = false;
10298
10299       add_reg_note (insn, REG_CFA_DEF_CFA,
10300                     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10301                                   GEN_INT (param_ptr_offset)));
10302       RTX_FRAME_RELATED_P (insn) = 1;
10303
10304       if (!call_used_regs[REGNO (crtl->drap_reg)])
10305         ix86_emit_restore_reg_using_pop (crtl->drap_reg);
10306     }
10307
10308   /* At this point the stack pointer must be valid, and we must have
10309      restored all of the registers.  We may not have deallocated the
10310      entire stack frame.  We've delayed this until now because it may
10311      be possible to merge the local stack deallocation with the
10312      deallocation forced by ix86_static_chain_on_stack.   */
10313   gcc_assert (m->fs.sp_valid);
10314   gcc_assert (!m->fs.fp_valid);
10315   gcc_assert (!m->fs.realigned);
10316   if (m->fs.sp_offset != UNITS_PER_WORD)
10317     {
10318       pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10319                                  GEN_INT (m->fs.sp_offset - UNITS_PER_WORD),
10320                                  style, true);
10321     }
10322
10323   /* Sibcall epilogues don't want a return instruction.  */
10324   if (style == 0)
10325     {
10326       m->fs = frame_state_save;
10327       return;
10328     }
10329
10330   if (crtl->args.pops_args && crtl->args.size)
10331     {
10332       rtx popc = GEN_INT (crtl->args.pops_args);
10333
10334       /* i386 can only pop 64K bytes.  If asked to pop more, pop return
10335          address, do explicit add, and jump indirectly to the caller.  */
10336
10337       if (crtl->args.pops_args >= 65536)
10338         {
10339           rtx ecx = gen_rtx_REG (SImode, CX_REG);
10340           rtx insn;
10341
10342           /* There is no "pascal" calling convention in any 64bit ABI.  */
10343           gcc_assert (!TARGET_64BIT);
10344
10345           insn = emit_insn (gen_pop (ecx));
10346           m->fs.cfa_offset -= UNITS_PER_WORD;
10347           m->fs.sp_offset -= UNITS_PER_WORD;
10348
10349           add_reg_note (insn, REG_CFA_ADJUST_CFA,
10350                         copy_rtx (XVECEXP (PATTERN (insn), 0, 1)));
10351           add_reg_note (insn, REG_CFA_REGISTER,
10352                         gen_rtx_SET (VOIDmode, ecx, pc_rtx));
10353           RTX_FRAME_RELATED_P (insn) = 1;
10354
10355           pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
10356                                      popc, -1, true);
10357           emit_jump_insn (gen_return_indirect_internal (ecx));
10358         }
10359       else
10360         emit_jump_insn (gen_return_pop_internal (popc));
10361     }
10362   else
10363     emit_jump_insn (gen_return_internal ());
10364
10365   /* Restore the state back to the state from the prologue,
10366      so that it's correct for the next epilogue.  */
10367   m->fs = frame_state_save;
10368 }
10369
10370 /* Reset from the function's potential modifications.  */
10371
10372 static void
10373 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
10374                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
10375 {
10376   if (pic_offset_table_rtx)
10377     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
10378 #if TARGET_MACHO
10379   /* Mach-O doesn't support labels at the end of objects, so if
10380      it looks like we might want one, insert a NOP.  */
10381   {
10382     rtx insn = get_last_insn ();
10383     while (insn
10384            && NOTE_P (insn)
10385            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
10386       insn = PREV_INSN (insn);
10387     if (insn
10388         && (LABEL_P (insn)
10389             || (NOTE_P (insn)
10390                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
10391       fputs ("\tnop\n", file);
10392   }
10393 #endif
10394
10395 }
10396
10397 /* Return a scratch register to use in the split stack prologue.  The
10398    split stack prologue is used for -fsplit-stack.  It is the first
10399    instructions in the function, even before the regular prologue.
10400    The scratch register can be any caller-saved register which is not
10401    used for parameters or for the static chain.  */
10402
10403 static unsigned int
10404 split_stack_prologue_scratch_regno (void)
10405 {
10406   if (TARGET_64BIT)
10407     return R11_REG;
10408   else
10409     {
10410       bool is_fastcall;
10411       int regparm;
10412
10413       is_fastcall = (lookup_attribute ("fastcall",
10414                                        TYPE_ATTRIBUTES (TREE_TYPE (cfun->decl)))
10415                      != NULL);
10416       regparm = ix86_function_regparm (TREE_TYPE (cfun->decl), cfun->decl);
10417
10418       if (is_fastcall)
10419         {
10420           if (DECL_STATIC_CHAIN (cfun->decl))
10421             {
10422               sorry ("-fsplit-stack does not support fastcall with "
10423                      "nested function");
10424               return INVALID_REGNUM;
10425             }
10426           return AX_REG;
10427         }
10428       else if (regparm < 3)
10429         {
10430           if (!DECL_STATIC_CHAIN (cfun->decl))
10431             return CX_REG;
10432           else
10433             {
10434               if (regparm >= 2)
10435                 {
10436                   sorry ("-fsplit-stack does not support 2 register "
10437                          " parameters for a nested function");
10438                   return INVALID_REGNUM;
10439                 }
10440               return DX_REG;
10441             }
10442         }
10443       else
10444         {
10445           /* FIXME: We could make this work by pushing a register
10446              around the addition and comparison.  */
10447           sorry ("-fsplit-stack does not support 3 register parameters");
10448           return INVALID_REGNUM;
10449         }
10450     }
10451 }
10452
10453 /* A SYMBOL_REF for the function which allocates new stackspace for
10454    -fsplit-stack.  */
10455
10456 static GTY(()) rtx split_stack_fn;
10457
10458 /* Handle -fsplit-stack.  These are the first instructions in the
10459    function, even before the regular prologue.  */
10460
10461 void
10462 ix86_expand_split_stack_prologue (void)
10463 {
10464   struct ix86_frame frame;
10465   HOST_WIDE_INT allocate;
10466   int args_size;
10467   rtx label, limit, current, jump_insn, allocate_rtx, call_insn, call_fusage;
10468   rtx scratch_reg = NULL_RTX;
10469   rtx varargs_label = NULL_RTX;
10470
10471   gcc_assert (flag_split_stack && reload_completed);
10472
10473   ix86_finalize_stack_realign_flags ();
10474   ix86_compute_frame_layout (&frame);
10475   allocate = frame.stack_pointer_offset - INCOMING_FRAME_SP_OFFSET;
10476
10477   /* This is the label we will branch to if we have enough stack
10478      space.  We expect the basic block reordering pass to reverse this
10479      branch if optimizing, so that we branch in the unlikely case.  */
10480   label = gen_label_rtx ();
10481
10482   /* We need to compare the stack pointer minus the frame size with
10483      the stack boundary in the TCB.  The stack boundary always gives
10484      us SPLIT_STACK_AVAILABLE bytes, so if we need less than that we
10485      can compare directly.  Otherwise we need to do an addition.  */
10486
10487   limit = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx),
10488                           UNSPEC_STACK_CHECK);
10489   limit = gen_rtx_CONST (Pmode, limit);
10490   limit = gen_rtx_MEM (Pmode, limit);
10491   if (allocate < SPLIT_STACK_AVAILABLE)
10492     current = stack_pointer_rtx;
10493   else
10494     {
10495       unsigned int scratch_regno;
10496       rtx offset;
10497
10498       /* We need a scratch register to hold the stack pointer minus
10499          the required frame size.  Since this is the very start of the
10500          function, the scratch register can be any caller-saved
10501          register which is not used for parameters.  */
10502       offset = GEN_INT (- allocate);
10503       scratch_regno = split_stack_prologue_scratch_regno ();
10504       if (scratch_regno == INVALID_REGNUM)
10505         return;
10506       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
10507       if (!TARGET_64BIT || x86_64_immediate_operand (offset, Pmode))
10508         {
10509           /* We don't use ix86_gen_add3 in this case because it will
10510              want to split to lea, but when not optimizing the insn
10511              will not be split after this point.  */
10512           emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
10513                                   gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10514                                                 offset)));
10515         }
10516       else
10517         {
10518           emit_move_insn (scratch_reg, offset);
10519           emit_insn (gen_adddi3 (scratch_reg, scratch_reg,
10520                                  stack_pointer_rtx));
10521         }
10522       current = scratch_reg;
10523     }
10524
10525   ix86_expand_branch (GEU, current, limit, label);
10526   jump_insn = get_last_insn ();
10527   JUMP_LABEL (jump_insn) = label;
10528
10529   /* Mark the jump as very likely to be taken.  */
10530   add_reg_note (jump_insn, REG_BR_PROB,
10531                 GEN_INT (REG_BR_PROB_BASE - REG_BR_PROB_BASE / 100));
10532
10533   /* Get more stack space.  We pass in the desired stack space and the
10534      size of the arguments to copy to the new stack.  In 32-bit mode
10535      we push the parameters; __morestack will return on a new stack
10536      anyhow.  In 64-bit mode we pass the parameters in r10 and
10537      r11.  */
10538   allocate_rtx = GEN_INT (allocate);
10539   args_size = crtl->args.size >= 0 ? crtl->args.size : 0;
10540   call_fusage = NULL_RTX;
10541   if (TARGET_64BIT)
10542     {
10543       rtx reg;
10544
10545       reg = gen_rtx_REG (Pmode, R10_REG);
10546
10547       /* If this function uses a static chain, it will be in %r10.
10548          Preserve it across the call to __morestack.  */
10549       if (DECL_STATIC_CHAIN (cfun->decl))
10550         {
10551           rtx rax;
10552
10553           rax = gen_rtx_REG (Pmode, AX_REG);
10554           emit_move_insn (rax, reg);
10555           use_reg (&call_fusage, rax);
10556         }
10557
10558       emit_move_insn (reg, allocate_rtx);
10559       use_reg (&call_fusage, reg);
10560       reg = gen_rtx_REG (Pmode, R11_REG);
10561       emit_move_insn (reg, GEN_INT (args_size));
10562       use_reg (&call_fusage, reg);
10563     }
10564   else
10565     {
10566       emit_insn (gen_push (GEN_INT (args_size)));
10567       emit_insn (gen_push (allocate_rtx));
10568     }
10569   if (split_stack_fn == NULL_RTX)
10570     split_stack_fn = gen_rtx_SYMBOL_REF (Pmode, "__morestack");
10571   call_insn = ix86_expand_call (NULL_RTX, gen_rtx_MEM (QImode, split_stack_fn),
10572                                 GEN_INT (UNITS_PER_WORD), constm1_rtx,
10573                                 NULL_RTX, 0);
10574   add_function_usage_to (call_insn, call_fusage);
10575
10576   /* In order to make call/return prediction work right, we now need
10577      to execute a return instruction.  See
10578      libgcc/config/i386/morestack.S for the details on how this works.
10579
10580      For flow purposes gcc must not see this as a return
10581      instruction--we need control flow to continue at the subsequent
10582      label.  Therefore, we use an unspec.  */
10583   gcc_assert (crtl->args.pops_args < 65536);
10584   emit_insn (gen_split_stack_return (GEN_INT (crtl->args.pops_args)));
10585
10586   /* If we are in 64-bit mode and this function uses a static chain,
10587      we saved %r10 in %rax before calling _morestack.  */
10588   if (TARGET_64BIT && DECL_STATIC_CHAIN (cfun->decl))
10589     emit_move_insn (gen_rtx_REG (Pmode, R10_REG),
10590                     gen_rtx_REG (Pmode, AX_REG));
10591
10592   /* If this function calls va_start, we need to store a pointer to
10593      the arguments on the old stack, because they may not have been
10594      all copied to the new stack.  At this point the old stack can be
10595      found at the frame pointer value used by __morestack, because
10596      __morestack has set that up before calling back to us.  Here we
10597      store that pointer in a scratch register, and in
10598      ix86_expand_prologue we store the scratch register in a stack
10599      slot.  */
10600   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
10601     {
10602       unsigned int scratch_regno;
10603       rtx frame_reg;
10604       int words;
10605
10606       scratch_regno = split_stack_prologue_scratch_regno ();
10607       scratch_reg = gen_rtx_REG (Pmode, scratch_regno);
10608       frame_reg = gen_rtx_REG (Pmode, BP_REG);
10609
10610       /* 64-bit:
10611          fp -> old fp value
10612                return address within this function
10613                return address of caller of this function
10614                stack arguments
10615          So we add three words to get to the stack arguments.
10616
10617          32-bit:
10618          fp -> old fp value
10619                return address within this function
10620                first argument to __morestack
10621                second argument to __morestack
10622                return address of caller of this function
10623                stack arguments
10624          So we add five words to get to the stack arguments.
10625       */
10626       words = TARGET_64BIT ? 3 : 5;
10627       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
10628                               gen_rtx_PLUS (Pmode, frame_reg,
10629                                             GEN_INT (words * UNITS_PER_WORD))));
10630
10631       varargs_label = gen_label_rtx ();
10632       emit_jump_insn (gen_jump (varargs_label));
10633       JUMP_LABEL (get_last_insn ()) = varargs_label;
10634
10635       emit_barrier ();
10636     }
10637
10638   emit_label (label);
10639   LABEL_NUSES (label) = 1;
10640
10641   /* If this function calls va_start, we now have to set the scratch
10642      register for the case where we do not call __morestack.  In this
10643      case we need to set it based on the stack pointer.  */
10644   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
10645     {
10646       emit_insn (gen_rtx_SET (VOIDmode, scratch_reg,
10647                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
10648                                             GEN_INT (UNITS_PER_WORD))));
10649
10650       emit_label (varargs_label);
10651       LABEL_NUSES (varargs_label) = 1;
10652     }
10653 }
10654
10655 /* We may have to tell the dataflow pass that the split stack prologue
10656    is initializing a scratch register.  */
10657
10658 static void
10659 ix86_live_on_entry (bitmap regs)
10660 {
10661   if (cfun->machine->split_stack_varargs_pointer != NULL_RTX)
10662     {
10663       gcc_assert (flag_split_stack);
10664       bitmap_set_bit (regs, split_stack_prologue_scratch_regno ());
10665     }
10666 }
10667 \f
10668 /* Extract the parts of an RTL expression that is a valid memory address
10669    for an instruction.  Return 0 if the structure of the address is
10670    grossly off.  Return -1 if the address contains ASHIFT, so it is not
10671    strictly valid, but still used for computing length of lea instruction.  */
10672
10673 int
10674 ix86_decompose_address (rtx addr, struct ix86_address *out)
10675 {
10676   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
10677   rtx base_reg, index_reg;
10678   HOST_WIDE_INT scale = 1;
10679   rtx scale_rtx = NULL_RTX;
10680   rtx tmp;
10681   int retval = 1;
10682   enum ix86_address_seg seg = SEG_DEFAULT;
10683
10684   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
10685     base = addr;
10686   else if (GET_CODE (addr) == PLUS)
10687     {
10688       rtx addends[4], op;
10689       int n = 0, i;
10690
10691       op = addr;
10692       do
10693         {
10694           if (n >= 4)
10695             return 0;
10696           addends[n++] = XEXP (op, 1);
10697           op = XEXP (op, 0);
10698         }
10699       while (GET_CODE (op) == PLUS);
10700       if (n >= 4)
10701         return 0;
10702       addends[n] = op;
10703
10704       for (i = n; i >= 0; --i)
10705         {
10706           op = addends[i];
10707           switch (GET_CODE (op))
10708             {
10709             case MULT:
10710               if (index)
10711                 return 0;
10712               index = XEXP (op, 0);
10713               scale_rtx = XEXP (op, 1);
10714               break;
10715
10716             case ASHIFT:
10717               if (index)
10718                 return 0;
10719               index = XEXP (op, 0);
10720               tmp = XEXP (op, 1);
10721               if (!CONST_INT_P (tmp))
10722                 return 0;
10723               scale = INTVAL (tmp);
10724               if ((unsigned HOST_WIDE_INT) scale > 3)
10725                 return 0;
10726               scale = 1 << scale;
10727               break;
10728
10729             case UNSPEC:
10730               if (XINT (op, 1) == UNSPEC_TP
10731                   && TARGET_TLS_DIRECT_SEG_REFS
10732                   && seg == SEG_DEFAULT)
10733                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
10734               else
10735                 return 0;
10736               break;
10737
10738             case REG:
10739             case SUBREG:
10740               if (!base)
10741                 base = op;
10742               else if (!index)
10743                 index = op;
10744               else
10745                 return 0;
10746               break;
10747
10748             case CONST:
10749             case CONST_INT:
10750             case SYMBOL_REF:
10751             case LABEL_REF:
10752               if (disp)
10753                 return 0;
10754               disp = op;
10755               break;
10756
10757             default:
10758               return 0;
10759             }
10760         }
10761     }
10762   else if (GET_CODE (addr) == MULT)
10763     {
10764       index = XEXP (addr, 0);           /* index*scale */
10765       scale_rtx = XEXP (addr, 1);
10766     }
10767   else if (GET_CODE (addr) == ASHIFT)
10768     {
10769       /* We're called for lea too, which implements ashift on occasion.  */
10770       index = XEXP (addr, 0);
10771       tmp = XEXP (addr, 1);
10772       if (!CONST_INT_P (tmp))
10773         return 0;
10774       scale = INTVAL (tmp);
10775       if ((unsigned HOST_WIDE_INT) scale > 3)
10776         return 0;
10777       scale = 1 << scale;
10778       retval = -1;
10779     }
10780   else
10781     disp = addr;                        /* displacement */
10782
10783   /* Extract the integral value of scale.  */
10784   if (scale_rtx)
10785     {
10786       if (!CONST_INT_P (scale_rtx))
10787         return 0;
10788       scale = INTVAL (scale_rtx);
10789     }
10790
10791   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
10792   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
10793
10794   /* Avoid useless 0 displacement.  */
10795   if (disp == const0_rtx && (base || index))
10796     disp = NULL_RTX;
10797
10798   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
10799   if (base_reg && index_reg && scale == 1
10800       && (index_reg == arg_pointer_rtx
10801           || index_reg == frame_pointer_rtx
10802           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
10803     {
10804       rtx tmp;
10805       tmp = base, base = index, index = tmp;
10806       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
10807     }
10808
10809   /* Special case: %ebp cannot be encoded as a base without a displacement.
10810      Similarly %r13.  */
10811   if (!disp
10812       && base_reg
10813       && (base_reg == hard_frame_pointer_rtx
10814           || base_reg == frame_pointer_rtx
10815           || base_reg == arg_pointer_rtx
10816           || (REG_P (base_reg)
10817               && (REGNO (base_reg) == HARD_FRAME_POINTER_REGNUM
10818                   || REGNO (base_reg) == R13_REG))))
10819     disp = const0_rtx;
10820
10821   /* Special case: on K6, [%esi] makes the instruction vector decoded.
10822      Avoid this by transforming to [%esi+0].
10823      Reload calls address legitimization without cfun defined, so we need
10824      to test cfun for being non-NULL. */
10825   if (TARGET_K6 && cfun && optimize_function_for_speed_p (cfun)
10826       && base_reg && !index_reg && !disp
10827       && REG_P (base_reg) && REGNO (base_reg) == SI_REG)
10828     disp = const0_rtx;
10829
10830   /* Special case: encode reg+reg instead of reg*2.  */
10831   if (!base && index && scale == 2)
10832     base = index, base_reg = index_reg, scale = 1;
10833
10834   /* Special case: scaling cannot be encoded without base or displacement.  */
10835   if (!base && !disp && index && scale != 1)
10836     disp = const0_rtx;
10837
10838   out->base = base;
10839   out->index = index;
10840   out->disp = disp;
10841   out->scale = scale;
10842   out->seg = seg;
10843
10844   return retval;
10845 }
10846 \f
10847 /* Return cost of the memory address x.
10848    For i386, it is better to use a complex address than let gcc copy
10849    the address into a reg and make a new pseudo.  But not if the address
10850    requires to two regs - that would mean more pseudos with longer
10851    lifetimes.  */
10852 static int
10853 ix86_address_cost (rtx x, bool speed ATTRIBUTE_UNUSED)
10854 {
10855   struct ix86_address parts;
10856   int cost = 1;
10857   int ok = ix86_decompose_address (x, &parts);
10858
10859   gcc_assert (ok);
10860
10861   if (parts.base && GET_CODE (parts.base) == SUBREG)
10862     parts.base = SUBREG_REG (parts.base);
10863   if (parts.index && GET_CODE (parts.index) == SUBREG)
10864     parts.index = SUBREG_REG (parts.index);
10865
10866   /* Attempt to minimize number of registers in the address.  */
10867   if ((parts.base
10868        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
10869       || (parts.index
10870           && (!REG_P (parts.index)
10871               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
10872     cost++;
10873
10874   if (parts.base
10875       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
10876       && parts.index
10877       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
10878       && parts.base != parts.index)
10879     cost++;
10880
10881   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
10882      since it's predecode logic can't detect the length of instructions
10883      and it degenerates to vector decoded.  Increase cost of such
10884      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
10885      to split such addresses or even refuse such addresses at all.
10886
10887      Following addressing modes are affected:
10888       [base+scale*index]
10889       [scale*index+disp]
10890       [base+index]
10891
10892      The first and last case  may be avoidable by explicitly coding the zero in
10893      memory address, but I don't have AMD-K6 machine handy to check this
10894      theory.  */
10895
10896   if (TARGET_K6
10897       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
10898           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
10899           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
10900     cost += 10;
10901
10902   return cost;
10903 }
10904 \f
10905 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
10906    this is used for to form addresses to local data when -fPIC is in
10907    use.  */
10908
10909 static bool
10910 darwin_local_data_pic (rtx disp)
10911 {
10912   return (GET_CODE (disp) == UNSPEC
10913           && XINT (disp, 1) == UNSPEC_MACHOPIC_OFFSET);
10914 }
10915
10916 /* Determine if a given RTX is a valid constant.  We already know this
10917    satisfies CONSTANT_P.  */
10918
10919 bool
10920 legitimate_constant_p (rtx x)
10921 {
10922   switch (GET_CODE (x))
10923     {
10924     case CONST:
10925       x = XEXP (x, 0);
10926
10927       if (GET_CODE (x) == PLUS)
10928         {
10929           if (!CONST_INT_P (XEXP (x, 1)))
10930             return false;
10931           x = XEXP (x, 0);
10932         }
10933
10934       if (TARGET_MACHO && darwin_local_data_pic (x))
10935         return true;
10936
10937       /* Only some unspecs are valid as "constants".  */
10938       if (GET_CODE (x) == UNSPEC)
10939         switch (XINT (x, 1))
10940           {
10941           case UNSPEC_GOT:
10942           case UNSPEC_GOTOFF:
10943           case UNSPEC_PLTOFF:
10944             return TARGET_64BIT;
10945           case UNSPEC_TPOFF:
10946           case UNSPEC_NTPOFF:
10947             x = XVECEXP (x, 0, 0);
10948             return (GET_CODE (x) == SYMBOL_REF
10949                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
10950           case UNSPEC_DTPOFF:
10951             x = XVECEXP (x, 0, 0);
10952             return (GET_CODE (x) == SYMBOL_REF
10953                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
10954           default:
10955             return false;
10956           }
10957
10958       /* We must have drilled down to a symbol.  */
10959       if (GET_CODE (x) == LABEL_REF)
10960         return true;
10961       if (GET_CODE (x) != SYMBOL_REF)
10962         return false;
10963       /* FALLTHRU */
10964
10965     case SYMBOL_REF:
10966       /* TLS symbols are never valid.  */
10967       if (SYMBOL_REF_TLS_MODEL (x))
10968         return false;
10969
10970       /* DLLIMPORT symbols are never valid.  */
10971       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
10972           && SYMBOL_REF_DLLIMPORT_P (x))
10973         return false;
10974       break;
10975
10976     case CONST_DOUBLE:
10977       if (GET_MODE (x) == TImode
10978           && x != CONST0_RTX (TImode)
10979           && !TARGET_64BIT)
10980         return false;
10981       break;
10982
10983     case CONST_VECTOR:
10984       if (!standard_sse_constant_p (x))
10985         return false;
10986
10987     default:
10988       break;
10989     }
10990
10991   /* Otherwise we handle everything else in the move patterns.  */
10992   return true;
10993 }
10994
10995 /* Determine if it's legal to put X into the constant pool.  This
10996    is not possible for the address of thread-local symbols, which
10997    is checked above.  */
10998
10999 static bool
11000 ix86_cannot_force_const_mem (rtx x)
11001 {
11002   /* We can always put integral constants and vectors in memory.  */
11003   switch (GET_CODE (x))
11004     {
11005     case CONST_INT:
11006     case CONST_DOUBLE:
11007     case CONST_VECTOR:
11008       return false;
11009
11010     default:
11011       break;
11012     }
11013   return !legitimate_constant_p (x);
11014 }
11015
11016
11017 /* Nonzero if the constant value X is a legitimate general operand
11018    when generating PIC code.  It is given that flag_pic is on and
11019    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
11020
11021 bool
11022 legitimate_pic_operand_p (rtx x)
11023 {
11024   rtx inner;
11025
11026   switch (GET_CODE (x))
11027     {
11028     case CONST:
11029       inner = XEXP (x, 0);
11030       if (GET_CODE (inner) == PLUS
11031           && CONST_INT_P (XEXP (inner, 1)))
11032         inner = XEXP (inner, 0);
11033
11034       /* Only some unspecs are valid as "constants".  */
11035       if (GET_CODE (inner) == UNSPEC)
11036         switch (XINT (inner, 1))
11037           {
11038           case UNSPEC_GOT:
11039           case UNSPEC_GOTOFF:
11040           case UNSPEC_PLTOFF:
11041             return TARGET_64BIT;
11042           case UNSPEC_TPOFF:
11043             x = XVECEXP (inner, 0, 0);
11044             return (GET_CODE (x) == SYMBOL_REF
11045                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
11046           case UNSPEC_MACHOPIC_OFFSET:
11047             return legitimate_pic_address_disp_p (x);
11048           default:
11049             return false;
11050           }
11051       /* FALLTHRU */
11052
11053     case SYMBOL_REF:
11054     case LABEL_REF:
11055       return legitimate_pic_address_disp_p (x);
11056
11057     default:
11058       return true;
11059     }
11060 }
11061
11062 /* Determine if a given CONST RTX is a valid memory displacement
11063    in PIC mode.  */
11064
11065 bool
11066 legitimate_pic_address_disp_p (rtx disp)
11067 {
11068   bool saw_plus;
11069
11070   /* In 64bit mode we can allow direct addresses of symbols and labels
11071      when they are not dynamic symbols.  */
11072   if (TARGET_64BIT)
11073     {
11074       rtx op0 = disp, op1;
11075
11076       switch (GET_CODE (disp))
11077         {
11078         case LABEL_REF:
11079           return true;
11080
11081         case CONST:
11082           if (GET_CODE (XEXP (disp, 0)) != PLUS)
11083             break;
11084           op0 = XEXP (XEXP (disp, 0), 0);
11085           op1 = XEXP (XEXP (disp, 0), 1);
11086           if (!CONST_INT_P (op1)
11087               || INTVAL (op1) >= 16*1024*1024
11088               || INTVAL (op1) < -16*1024*1024)
11089             break;
11090           if (GET_CODE (op0) == LABEL_REF)
11091             return true;
11092           if (GET_CODE (op0) != SYMBOL_REF)
11093             break;
11094           /* FALLTHRU */
11095
11096         case SYMBOL_REF:
11097           /* TLS references should always be enclosed in UNSPEC.  */
11098           if (SYMBOL_REF_TLS_MODEL (op0))
11099             return false;
11100           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
11101               && ix86_cmodel != CM_LARGE_PIC)
11102             return true;
11103           break;
11104
11105         default:
11106           break;
11107         }
11108     }
11109   if (GET_CODE (disp) != CONST)
11110     return false;
11111   disp = XEXP (disp, 0);
11112
11113   if (TARGET_64BIT)
11114     {
11115       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
11116          of GOT tables.  We should not need these anyway.  */
11117       if (GET_CODE (disp) != UNSPEC
11118           || (XINT (disp, 1) != UNSPEC_GOTPCREL
11119               && XINT (disp, 1) != UNSPEC_GOTOFF
11120               && XINT (disp, 1) != UNSPEC_PLTOFF))
11121         return false;
11122
11123       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
11124           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
11125         return false;
11126       return true;
11127     }
11128
11129   saw_plus = false;
11130   if (GET_CODE (disp) == PLUS)
11131     {
11132       if (!CONST_INT_P (XEXP (disp, 1)))
11133         return false;
11134       disp = XEXP (disp, 0);
11135       saw_plus = true;
11136     }
11137
11138   if (TARGET_MACHO && darwin_local_data_pic (disp))
11139     return true;
11140
11141   if (GET_CODE (disp) != UNSPEC)
11142     return false;
11143
11144   switch (XINT (disp, 1))
11145     {
11146     case UNSPEC_GOT:
11147       if (saw_plus)
11148         return false;
11149       /* We need to check for both symbols and labels because VxWorks loads
11150          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
11151          details.  */
11152       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11153               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
11154     case UNSPEC_GOTOFF:
11155       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
11156          While ABI specify also 32bit relocation but we don't produce it in
11157          small PIC model at all.  */
11158       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
11159            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
11160           && !TARGET_64BIT)
11161         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
11162       return false;
11163     case UNSPEC_GOTTPOFF:
11164     case UNSPEC_GOTNTPOFF:
11165     case UNSPEC_INDNTPOFF:
11166       if (saw_plus)
11167         return false;
11168       disp = XVECEXP (disp, 0, 0);
11169       return (GET_CODE (disp) == SYMBOL_REF
11170               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
11171     case UNSPEC_NTPOFF:
11172       disp = XVECEXP (disp, 0, 0);
11173       return (GET_CODE (disp) == SYMBOL_REF
11174               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
11175     case UNSPEC_DTPOFF:
11176       disp = XVECEXP (disp, 0, 0);
11177       return (GET_CODE (disp) == SYMBOL_REF
11178               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
11179     }
11180
11181   return false;
11182 }
11183
11184 /* Recognizes RTL expressions that are valid memory addresses for an
11185    instruction.  The MODE argument is the machine mode for the MEM
11186    expression that wants to use this address.
11187
11188    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
11189    convert common non-canonical forms to canonical form so that they will
11190    be recognized.  */
11191
11192 static bool
11193 ix86_legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
11194                            rtx addr, bool strict)
11195 {
11196   struct ix86_address parts;
11197   rtx base, index, disp;
11198   HOST_WIDE_INT scale;
11199
11200   if (ix86_decompose_address (addr, &parts) <= 0)
11201     /* Decomposition failed.  */
11202     return false;
11203
11204   base = parts.base;
11205   index = parts.index;
11206   disp = parts.disp;
11207   scale = parts.scale;
11208
11209   /* Validate base register.
11210
11211      Don't allow SUBREG's that span more than a word here.  It can lead to spill
11212      failures when the base is one word out of a two word structure, which is
11213      represented internally as a DImode int.  */
11214
11215   if (base)
11216     {
11217       rtx reg;
11218
11219       if (REG_P (base))
11220         reg = base;
11221       else if (GET_CODE (base) == SUBREG
11222                && REG_P (SUBREG_REG (base))
11223                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
11224                   <= UNITS_PER_WORD)
11225         reg = SUBREG_REG (base);
11226       else
11227         /* Base is not a register.  */
11228         return false;
11229
11230       if (GET_MODE (base) != Pmode)
11231         /* Base is not in Pmode.  */
11232         return false;
11233
11234       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
11235           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
11236         /* Base is not valid.  */
11237         return false;
11238     }
11239
11240   /* Validate index register.
11241
11242      Don't allow SUBREG's that span more than a word here -- same as above.  */
11243
11244   if (index)
11245     {
11246       rtx reg;
11247
11248       if (REG_P (index))
11249         reg = index;
11250       else if (GET_CODE (index) == SUBREG
11251                && REG_P (SUBREG_REG (index))
11252                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
11253                   <= UNITS_PER_WORD)
11254         reg = SUBREG_REG (index);
11255       else
11256         /* Index is not a register.  */
11257         return false;
11258
11259       if (GET_MODE (index) != Pmode)
11260         /* Index is not in Pmode.  */
11261         return false;
11262
11263       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
11264           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
11265         /* Index is not valid.  */
11266         return false;
11267     }
11268
11269   /* Validate scale factor.  */
11270   if (scale != 1)
11271     {
11272       if (!index)
11273         /* Scale without index.  */
11274         return false;
11275
11276       if (scale != 2 && scale != 4 && scale != 8)
11277         /* Scale is not a valid multiplier.  */
11278         return false;
11279     }
11280
11281   /* Validate displacement.  */
11282   if (disp)
11283     {
11284       if (GET_CODE (disp) == CONST
11285           && GET_CODE (XEXP (disp, 0)) == UNSPEC
11286           && XINT (XEXP (disp, 0), 1) != UNSPEC_MACHOPIC_OFFSET)
11287         switch (XINT (XEXP (disp, 0), 1))
11288           {
11289           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
11290              used.  While ABI specify also 32bit relocations, we don't produce
11291              them at all and use IP relative instead.  */
11292           case UNSPEC_GOT:
11293           case UNSPEC_GOTOFF:
11294             gcc_assert (flag_pic);
11295             if (!TARGET_64BIT)
11296               goto is_legitimate_pic;
11297
11298             /* 64bit address unspec.  */
11299             return false;
11300
11301           case UNSPEC_GOTPCREL:
11302             gcc_assert (flag_pic);
11303             goto is_legitimate_pic;
11304
11305           case UNSPEC_GOTTPOFF:
11306           case UNSPEC_GOTNTPOFF:
11307           case UNSPEC_INDNTPOFF:
11308           case UNSPEC_NTPOFF:
11309           case UNSPEC_DTPOFF:
11310             break;
11311
11312           case UNSPEC_STACK_CHECK:
11313             gcc_assert (flag_split_stack);
11314             break;
11315
11316           default:
11317             /* Invalid address unspec.  */
11318             return false;
11319           }
11320
11321       else if (SYMBOLIC_CONST (disp)
11322                && (flag_pic
11323                    || (TARGET_MACHO
11324 #if TARGET_MACHO
11325                        && MACHOPIC_INDIRECT
11326                        && !machopic_operand_p (disp)
11327 #endif
11328                )))
11329         {
11330
11331         is_legitimate_pic:
11332           if (TARGET_64BIT && (index || base))
11333             {
11334               /* foo@dtpoff(%rX) is ok.  */
11335               if (GET_CODE (disp) != CONST
11336                   || GET_CODE (XEXP (disp, 0)) != PLUS
11337                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
11338                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
11339                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
11340                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
11341                 /* Non-constant pic memory reference.  */
11342                 return false;
11343             }
11344           else if (! legitimate_pic_address_disp_p (disp))
11345             /* Displacement is an invalid pic construct.  */
11346             return false;
11347
11348           /* This code used to verify that a symbolic pic displacement
11349              includes the pic_offset_table_rtx register.
11350
11351              While this is good idea, unfortunately these constructs may
11352              be created by "adds using lea" optimization for incorrect
11353              code like:
11354
11355              int a;
11356              int foo(int i)
11357                {
11358                  return *(&a+i);
11359                }
11360
11361              This code is nonsensical, but results in addressing
11362              GOT table with pic_offset_table_rtx base.  We can't
11363              just refuse it easily, since it gets matched by
11364              "addsi3" pattern, that later gets split to lea in the
11365              case output register differs from input.  While this
11366              can be handled by separate addsi pattern for this case
11367              that never results in lea, this seems to be easier and
11368              correct fix for crash to disable this test.  */
11369         }
11370       else if (GET_CODE (disp) != LABEL_REF
11371                && !CONST_INT_P (disp)
11372                && (GET_CODE (disp) != CONST
11373                    || !legitimate_constant_p (disp))
11374                && (GET_CODE (disp) != SYMBOL_REF
11375                    || !legitimate_constant_p (disp)))
11376         /* Displacement is not constant.  */
11377         return false;
11378       else if (TARGET_64BIT
11379                && !x86_64_immediate_operand (disp, VOIDmode))
11380         /* Displacement is out of range.  */
11381         return false;
11382     }
11383
11384   /* Everything looks valid.  */
11385   return true;
11386 }
11387
11388 /* Determine if a given RTX is a valid constant address.  */
11389
11390 bool
11391 constant_address_p (rtx x)
11392 {
11393   return CONSTANT_P (x) && ix86_legitimate_address_p (Pmode, x, 1);
11394 }
11395 \f
11396 /* Return a unique alias set for the GOT.  */
11397
11398 static alias_set_type
11399 ix86_GOT_alias_set (void)
11400 {
11401   static alias_set_type set = -1;
11402   if (set == -1)
11403     set = new_alias_set ();
11404   return set;
11405 }
11406
11407 /* Return a legitimate reference for ORIG (an address) using the
11408    register REG.  If REG is 0, a new pseudo is generated.
11409
11410    There are two types of references that must be handled:
11411
11412    1. Global data references must load the address from the GOT, via
11413       the PIC reg.  An insn is emitted to do this load, and the reg is
11414       returned.
11415
11416    2. Static data references, constant pool addresses, and code labels
11417       compute the address as an offset from the GOT, whose base is in
11418       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
11419       differentiate them from global data objects.  The returned
11420       address is the PIC reg + an unspec constant.
11421
11422    TARGET_LEGITIMATE_ADDRESS_P rejects symbolic references unless the PIC
11423    reg also appears in the address.  */
11424
11425 static rtx
11426 legitimize_pic_address (rtx orig, rtx reg)
11427 {
11428   rtx addr = orig;
11429   rtx new_rtx = orig;
11430   rtx base;
11431
11432 #if TARGET_MACHO
11433   if (TARGET_MACHO && !TARGET_64BIT)
11434     {
11435       if (reg == 0)
11436         reg = gen_reg_rtx (Pmode);
11437       /* Use the generic Mach-O PIC machinery.  */
11438       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
11439     }
11440 #endif
11441
11442   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
11443     new_rtx = addr;
11444   else if (TARGET_64BIT
11445            && ix86_cmodel != CM_SMALL_PIC
11446            && gotoff_operand (addr, Pmode))
11447     {
11448       rtx tmpreg;
11449       /* This symbol may be referenced via a displacement from the PIC
11450          base address (@GOTOFF).  */
11451
11452       if (reload_in_progress)
11453         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11454       if (GET_CODE (addr) == CONST)
11455         addr = XEXP (addr, 0);
11456       if (GET_CODE (addr) == PLUS)
11457           {
11458             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
11459                                       UNSPEC_GOTOFF);
11460             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
11461           }
11462         else
11463           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
11464       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11465       if (!reg)
11466         tmpreg = gen_reg_rtx (Pmode);
11467       else
11468         tmpreg = reg;
11469       emit_move_insn (tmpreg, new_rtx);
11470
11471       if (reg != 0)
11472         {
11473           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
11474                                          tmpreg, 1, OPTAB_DIRECT);
11475           new_rtx = reg;
11476         }
11477       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
11478     }
11479   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
11480     {
11481       /* This symbol may be referenced via a displacement from the PIC
11482          base address (@GOTOFF).  */
11483
11484       if (reload_in_progress)
11485         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11486       if (GET_CODE (addr) == CONST)
11487         addr = XEXP (addr, 0);
11488       if (GET_CODE (addr) == PLUS)
11489           {
11490             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
11491                                       UNSPEC_GOTOFF);
11492             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
11493           }
11494         else
11495           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
11496       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11497       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
11498
11499       if (reg != 0)
11500         {
11501           emit_move_insn (reg, new_rtx);
11502           new_rtx = reg;
11503         }
11504     }
11505   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
11506            /* We can't use @GOTOFF for text labels on VxWorks;
11507               see gotoff_operand.  */
11508            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
11509     {
11510       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
11511         {
11512           if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (addr))
11513             return legitimize_dllimport_symbol (addr, true);
11514           if (GET_CODE (addr) == CONST && GET_CODE (XEXP (addr, 0)) == PLUS
11515               && GET_CODE (XEXP (XEXP (addr, 0), 0)) == SYMBOL_REF
11516               && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (addr, 0), 0)))
11517             {
11518               rtx t = legitimize_dllimport_symbol (XEXP (XEXP (addr, 0), 0), true);
11519               return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (addr, 0), 1));
11520             }
11521         }
11522
11523       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
11524         {
11525           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
11526           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11527           new_rtx = gen_const_mem (Pmode, new_rtx);
11528           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
11529
11530           if (reg == 0)
11531             reg = gen_reg_rtx (Pmode);
11532           /* Use directly gen_movsi, otherwise the address is loaded
11533              into register for CSE.  We don't want to CSE this addresses,
11534              instead we CSE addresses from the GOT table, so skip this.  */
11535           emit_insn (gen_movsi (reg, new_rtx));
11536           new_rtx = reg;
11537         }
11538       else
11539         {
11540           /* This symbol must be referenced via a load from the
11541              Global Offset Table (@GOT).  */
11542
11543           if (reload_in_progress)
11544             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11545           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
11546           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11547           if (TARGET_64BIT)
11548             new_rtx = force_reg (Pmode, new_rtx);
11549           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
11550           new_rtx = gen_const_mem (Pmode, new_rtx);
11551           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
11552
11553           if (reg == 0)
11554             reg = gen_reg_rtx (Pmode);
11555           emit_move_insn (reg, new_rtx);
11556           new_rtx = reg;
11557         }
11558     }
11559   else
11560     {
11561       if (CONST_INT_P (addr)
11562           && !x86_64_immediate_operand (addr, VOIDmode))
11563         {
11564           if (reg)
11565             {
11566               emit_move_insn (reg, addr);
11567               new_rtx = reg;
11568             }
11569           else
11570             new_rtx = force_reg (Pmode, addr);
11571         }
11572       else if (GET_CODE (addr) == CONST)
11573         {
11574           addr = XEXP (addr, 0);
11575
11576           /* We must match stuff we generate before.  Assume the only
11577              unspecs that can get here are ours.  Not that we could do
11578              anything with them anyway....  */
11579           if (GET_CODE (addr) == UNSPEC
11580               || (GET_CODE (addr) == PLUS
11581                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
11582             return orig;
11583           gcc_assert (GET_CODE (addr) == PLUS);
11584         }
11585       if (GET_CODE (addr) == PLUS)
11586         {
11587           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
11588
11589           /* Check first to see if this is a constant offset from a @GOTOFF
11590              symbol reference.  */
11591           if (gotoff_operand (op0, Pmode)
11592               && CONST_INT_P (op1))
11593             {
11594               if (!TARGET_64BIT)
11595                 {
11596                   if (reload_in_progress)
11597                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11598                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
11599                                             UNSPEC_GOTOFF);
11600                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
11601                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
11602                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
11603
11604                   if (reg != 0)
11605                     {
11606                       emit_move_insn (reg, new_rtx);
11607                       new_rtx = reg;
11608                     }
11609                 }
11610               else
11611                 {
11612                   if (INTVAL (op1) < -16*1024*1024
11613                       || INTVAL (op1) >= 16*1024*1024)
11614                     {
11615                       if (!x86_64_immediate_operand (op1, Pmode))
11616                         op1 = force_reg (Pmode, op1);
11617                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
11618                     }
11619                 }
11620             }
11621           else
11622             {
11623               base = legitimize_pic_address (XEXP (addr, 0), reg);
11624               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
11625                                                  base == reg ? NULL_RTX : reg);
11626
11627               if (CONST_INT_P (new_rtx))
11628                 new_rtx = plus_constant (base, INTVAL (new_rtx));
11629               else
11630                 {
11631                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
11632                     {
11633                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
11634                       new_rtx = XEXP (new_rtx, 1);
11635                     }
11636                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
11637                 }
11638             }
11639         }
11640     }
11641   return new_rtx;
11642 }
11643 \f
11644 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
11645
11646 static rtx
11647 get_thread_pointer (int to_reg)
11648 {
11649   rtx tp, reg, insn;
11650
11651   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
11652   if (!to_reg)
11653     return tp;
11654
11655   reg = gen_reg_rtx (Pmode);
11656   insn = gen_rtx_SET (VOIDmode, reg, tp);
11657   insn = emit_insn (insn);
11658
11659   return reg;
11660 }
11661
11662 /* A subroutine of ix86_legitimize_address and ix86_expand_move.  FOR_MOV is
11663    false if we expect this to be used for a memory address and true if
11664    we expect to load the address into a register.  */
11665
11666 static rtx
11667 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
11668 {
11669   rtx dest, base, off, pic, tp;
11670   int type;
11671
11672   switch (model)
11673     {
11674     case TLS_MODEL_GLOBAL_DYNAMIC:
11675       dest = gen_reg_rtx (Pmode);
11676       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
11677
11678       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
11679         {
11680           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
11681
11682           start_sequence ();
11683           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
11684           insns = get_insns ();
11685           end_sequence ();
11686
11687           RTL_CONST_CALL_P (insns) = 1;
11688           emit_libcall_block (insns, dest, rax, x);
11689         }
11690       else if (TARGET_64BIT && TARGET_GNU2_TLS)
11691         emit_insn (gen_tls_global_dynamic_64 (dest, x));
11692       else
11693         emit_insn (gen_tls_global_dynamic_32 (dest, x));
11694
11695       if (TARGET_GNU2_TLS)
11696         {
11697           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
11698
11699           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
11700         }
11701       break;
11702
11703     case TLS_MODEL_LOCAL_DYNAMIC:
11704       base = gen_reg_rtx (Pmode);
11705       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
11706
11707       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
11708         {
11709           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
11710
11711           start_sequence ();
11712           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
11713           insns = get_insns ();
11714           end_sequence ();
11715
11716           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
11717           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
11718           RTL_CONST_CALL_P (insns) = 1;
11719           emit_libcall_block (insns, base, rax, note);
11720         }
11721       else if (TARGET_64BIT && TARGET_GNU2_TLS)
11722         emit_insn (gen_tls_local_dynamic_base_64 (base));
11723       else
11724         emit_insn (gen_tls_local_dynamic_base_32 (base));
11725
11726       if (TARGET_GNU2_TLS)
11727         {
11728           rtx x = ix86_tls_module_base ();
11729
11730           set_unique_reg_note (get_last_insn (), REG_EQUIV,
11731                                gen_rtx_MINUS (Pmode, x, tp));
11732         }
11733
11734       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
11735       off = gen_rtx_CONST (Pmode, off);
11736
11737       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
11738
11739       if (TARGET_GNU2_TLS)
11740         {
11741           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
11742
11743           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
11744         }
11745
11746       break;
11747
11748     case TLS_MODEL_INITIAL_EXEC:
11749       if (TARGET_64BIT)
11750         {
11751           pic = NULL;
11752           type = UNSPEC_GOTNTPOFF;
11753         }
11754       else if (flag_pic)
11755         {
11756           if (reload_in_progress)
11757             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
11758           pic = pic_offset_table_rtx;
11759           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
11760         }
11761       else if (!TARGET_ANY_GNU_TLS)
11762         {
11763           pic = gen_reg_rtx (Pmode);
11764           emit_insn (gen_set_got (pic));
11765           type = UNSPEC_GOTTPOFF;
11766         }
11767       else
11768         {
11769           pic = NULL;
11770           type = UNSPEC_INDNTPOFF;
11771         }
11772
11773       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
11774       off = gen_rtx_CONST (Pmode, off);
11775       if (pic)
11776         off = gen_rtx_PLUS (Pmode, pic, off);
11777       off = gen_const_mem (Pmode, off);
11778       set_mem_alias_set (off, ix86_GOT_alias_set ());
11779
11780       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11781         {
11782           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
11783           off = force_reg (Pmode, off);
11784           return gen_rtx_PLUS (Pmode, base, off);
11785         }
11786       else
11787         {
11788           base = get_thread_pointer (true);
11789           dest = gen_reg_rtx (Pmode);
11790           emit_insn (gen_subsi3 (dest, base, off));
11791         }
11792       break;
11793
11794     case TLS_MODEL_LOCAL_EXEC:
11795       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
11796                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11797                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
11798       off = gen_rtx_CONST (Pmode, off);
11799
11800       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
11801         {
11802           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
11803           return gen_rtx_PLUS (Pmode, base, off);
11804         }
11805       else
11806         {
11807           base = get_thread_pointer (true);
11808           dest = gen_reg_rtx (Pmode);
11809           emit_insn (gen_subsi3 (dest, base, off));
11810         }
11811       break;
11812
11813     default:
11814       gcc_unreachable ();
11815     }
11816
11817   return dest;
11818 }
11819
11820 /* Create or return the unique __imp_DECL dllimport symbol corresponding
11821    to symbol DECL.  */
11822
11823 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
11824   htab_t dllimport_map;
11825
11826 static tree
11827 get_dllimport_decl (tree decl)
11828 {
11829   struct tree_map *h, in;
11830   void **loc;
11831   const char *name;
11832   const char *prefix;
11833   size_t namelen, prefixlen;
11834   char *imp_name;
11835   tree to;
11836   rtx rtl;
11837
11838   if (!dllimport_map)
11839     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
11840
11841   in.hash = htab_hash_pointer (decl);
11842   in.base.from = decl;
11843   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
11844   h = (struct tree_map *) *loc;
11845   if (h)
11846     return h->to;
11847
11848   *loc = h = ggc_alloc_tree_map ();
11849   h->hash = in.hash;
11850   h->base.from = decl;
11851   h->to = to = build_decl (DECL_SOURCE_LOCATION (decl),
11852                            VAR_DECL, NULL, ptr_type_node);
11853   DECL_ARTIFICIAL (to) = 1;
11854   DECL_IGNORED_P (to) = 1;
11855   DECL_EXTERNAL (to) = 1;
11856   TREE_READONLY (to) = 1;
11857
11858   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
11859   name = targetm.strip_name_encoding (name);
11860   prefix = name[0] == FASTCALL_PREFIX || user_label_prefix[0] == 0
11861     ? "*__imp_" : "*__imp__";
11862   namelen = strlen (name);
11863   prefixlen = strlen (prefix);
11864   imp_name = (char *) alloca (namelen + prefixlen + 1);
11865   memcpy (imp_name, prefix, prefixlen);
11866   memcpy (imp_name + prefixlen, name, namelen + 1);
11867
11868   name = ggc_alloc_string (imp_name, namelen + prefixlen);
11869   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
11870   SET_SYMBOL_REF_DECL (rtl, to);
11871   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
11872
11873   rtl = gen_const_mem (Pmode, rtl);
11874   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
11875
11876   SET_DECL_RTL (to, rtl);
11877   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
11878
11879   return to;
11880 }
11881
11882 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
11883    true if we require the result be a register.  */
11884
11885 static rtx
11886 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
11887 {
11888   tree imp_decl;
11889   rtx x;
11890
11891   gcc_assert (SYMBOL_REF_DECL (symbol));
11892   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
11893
11894   x = DECL_RTL (imp_decl);
11895   if (want_reg)
11896     x = force_reg (Pmode, x);
11897   return x;
11898 }
11899
11900 /* Try machine-dependent ways of modifying an illegitimate address
11901    to be legitimate.  If we find one, return the new, valid address.
11902    This macro is used in only one place: `memory_address' in explow.c.
11903
11904    OLDX is the address as it was before break_out_memory_refs was called.
11905    In some cases it is useful to look at this to decide what needs to be done.
11906
11907    It is always safe for this macro to do nothing.  It exists to recognize
11908    opportunities to optimize the output.
11909
11910    For the 80386, we handle X+REG by loading X into a register R and
11911    using R+REG.  R will go in a general reg and indexing will be used.
11912    However, if REG is a broken-out memory address or multiplication,
11913    nothing needs to be done because REG can certainly go in a general reg.
11914
11915    When -fpic is used, special handling is needed for symbolic references.
11916    See comments by legitimize_pic_address in i386.c for details.  */
11917
11918 static rtx
11919 ix86_legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED,
11920                          enum machine_mode mode)
11921 {
11922   int changed = 0;
11923   unsigned log;
11924
11925   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
11926   if (log)
11927     return legitimize_tls_address (x, (enum tls_model) log, false);
11928   if (GET_CODE (x) == CONST
11929       && GET_CODE (XEXP (x, 0)) == PLUS
11930       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
11931       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
11932     {
11933       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
11934                                       (enum tls_model) log, false);
11935       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
11936     }
11937
11938   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
11939     {
11940       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
11941         return legitimize_dllimport_symbol (x, true);
11942       if (GET_CODE (x) == CONST
11943           && GET_CODE (XEXP (x, 0)) == PLUS
11944           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
11945           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
11946         {
11947           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
11948           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
11949         }
11950     }
11951
11952   if (flag_pic && SYMBOLIC_CONST (x))
11953     return legitimize_pic_address (x, 0);
11954
11955   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
11956   if (GET_CODE (x) == ASHIFT
11957       && CONST_INT_P (XEXP (x, 1))
11958       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
11959     {
11960       changed = 1;
11961       log = INTVAL (XEXP (x, 1));
11962       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
11963                         GEN_INT (1 << log));
11964     }
11965
11966   if (GET_CODE (x) == PLUS)
11967     {
11968       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
11969
11970       if (GET_CODE (XEXP (x, 0)) == ASHIFT
11971           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
11972           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
11973         {
11974           changed = 1;
11975           log = INTVAL (XEXP (XEXP (x, 0), 1));
11976           XEXP (x, 0) = gen_rtx_MULT (Pmode,
11977                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
11978                                       GEN_INT (1 << log));
11979         }
11980
11981       if (GET_CODE (XEXP (x, 1)) == ASHIFT
11982           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
11983           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
11984         {
11985           changed = 1;
11986           log = INTVAL (XEXP (XEXP (x, 1), 1));
11987           XEXP (x, 1) = gen_rtx_MULT (Pmode,
11988                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
11989                                       GEN_INT (1 << log));
11990         }
11991
11992       /* Put multiply first if it isn't already.  */
11993       if (GET_CODE (XEXP (x, 1)) == MULT)
11994         {
11995           rtx tmp = XEXP (x, 0);
11996           XEXP (x, 0) = XEXP (x, 1);
11997           XEXP (x, 1) = tmp;
11998           changed = 1;
11999         }
12000
12001       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
12002          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
12003          created by virtual register instantiation, register elimination, and
12004          similar optimizations.  */
12005       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
12006         {
12007           changed = 1;
12008           x = gen_rtx_PLUS (Pmode,
12009                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
12010                                           XEXP (XEXP (x, 1), 0)),
12011                             XEXP (XEXP (x, 1), 1));
12012         }
12013
12014       /* Canonicalize
12015          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
12016          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
12017       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
12018                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
12019                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
12020                && CONSTANT_P (XEXP (x, 1)))
12021         {
12022           rtx constant;
12023           rtx other = NULL_RTX;
12024
12025           if (CONST_INT_P (XEXP (x, 1)))
12026             {
12027               constant = XEXP (x, 1);
12028               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
12029             }
12030           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
12031             {
12032               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
12033               other = XEXP (x, 1);
12034             }
12035           else
12036             constant = 0;
12037
12038           if (constant)
12039             {
12040               changed = 1;
12041               x = gen_rtx_PLUS (Pmode,
12042                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
12043                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
12044                                 plus_constant (other, INTVAL (constant)));
12045             }
12046         }
12047
12048       if (changed && ix86_legitimate_address_p (mode, x, false))
12049         return x;
12050
12051       if (GET_CODE (XEXP (x, 0)) == MULT)
12052         {
12053           changed = 1;
12054           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
12055         }
12056
12057       if (GET_CODE (XEXP (x, 1)) == MULT)
12058         {
12059           changed = 1;
12060           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
12061         }
12062
12063       if (changed
12064           && REG_P (XEXP (x, 1))
12065           && REG_P (XEXP (x, 0)))
12066         return x;
12067
12068       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
12069         {
12070           changed = 1;
12071           x = legitimize_pic_address (x, 0);
12072         }
12073
12074       if (changed && ix86_legitimate_address_p (mode, x, false))
12075         return x;
12076
12077       if (REG_P (XEXP (x, 0)))
12078         {
12079           rtx temp = gen_reg_rtx (Pmode);
12080           rtx val  = force_operand (XEXP (x, 1), temp);
12081           if (val != temp)
12082             emit_move_insn (temp, val);
12083
12084           XEXP (x, 1) = temp;
12085           return x;
12086         }
12087
12088       else if (REG_P (XEXP (x, 1)))
12089         {
12090           rtx temp = gen_reg_rtx (Pmode);
12091           rtx val  = force_operand (XEXP (x, 0), temp);
12092           if (val != temp)
12093             emit_move_insn (temp, val);
12094
12095           XEXP (x, 0) = temp;
12096           return x;
12097         }
12098     }
12099
12100   return x;
12101 }
12102 \f
12103 /* Print an integer constant expression in assembler syntax.  Addition
12104    and subtraction are the only arithmetic that may appear in these
12105    expressions.  FILE is the stdio stream to write to, X is the rtx, and
12106    CODE is the operand print code from the output string.  */
12107
12108 static void
12109 output_pic_addr_const (FILE *file, rtx x, int code)
12110 {
12111   char buf[256];
12112
12113   switch (GET_CODE (x))
12114     {
12115     case PC:
12116       gcc_assert (flag_pic);
12117       putc ('.', file);
12118       break;
12119
12120     case SYMBOL_REF:
12121       if (TARGET_64BIT || ! TARGET_MACHO_BRANCH_ISLANDS)
12122         output_addr_const (file, x);
12123       else
12124         {
12125           const char *name = XSTR (x, 0);
12126
12127           /* Mark the decl as referenced so that cgraph will
12128              output the function.  */
12129           if (SYMBOL_REF_DECL (x))
12130             mark_decl_referenced (SYMBOL_REF_DECL (x));
12131
12132 #if TARGET_MACHO
12133           if (MACHOPIC_INDIRECT
12134               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
12135             name = machopic_indirection_name (x, /*stub_p=*/true);
12136 #endif
12137           assemble_name (file, name);
12138         }
12139       if (!TARGET_MACHO && !(TARGET_64BIT && DEFAULT_ABI == MS_ABI)
12140           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
12141         fputs ("@PLT", file);
12142       break;
12143
12144     case LABEL_REF:
12145       x = XEXP (x, 0);
12146       /* FALLTHRU */
12147     case CODE_LABEL:
12148       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
12149       assemble_name (asm_out_file, buf);
12150       break;
12151
12152     case CONST_INT:
12153       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
12154       break;
12155
12156     case CONST:
12157       /* This used to output parentheses around the expression,
12158          but that does not work on the 386 (either ATT or BSD assembler).  */
12159       output_pic_addr_const (file, XEXP (x, 0), code);
12160       break;
12161
12162     case CONST_DOUBLE:
12163       if (GET_MODE (x) == VOIDmode)
12164         {
12165           /* We can use %d if the number is <32 bits and positive.  */
12166           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
12167             fprintf (file, "0x%lx%08lx",
12168                      (unsigned long) CONST_DOUBLE_HIGH (x),
12169                      (unsigned long) CONST_DOUBLE_LOW (x));
12170           else
12171             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
12172         }
12173       else
12174         /* We can't handle floating point constants;
12175            TARGET_PRINT_OPERAND must handle them.  */
12176         output_operand_lossage ("floating constant misused");
12177       break;
12178
12179     case PLUS:
12180       /* Some assemblers need integer constants to appear first.  */
12181       if (CONST_INT_P (XEXP (x, 0)))
12182         {
12183           output_pic_addr_const (file, XEXP (x, 0), code);
12184           putc ('+', file);
12185           output_pic_addr_const (file, XEXP (x, 1), code);
12186         }
12187       else
12188         {
12189           gcc_assert (CONST_INT_P (XEXP (x, 1)));
12190           output_pic_addr_const (file, XEXP (x, 1), code);
12191           putc ('+', file);
12192           output_pic_addr_const (file, XEXP (x, 0), code);
12193         }
12194       break;
12195
12196     case MINUS:
12197       if (!TARGET_MACHO)
12198         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
12199       output_pic_addr_const (file, XEXP (x, 0), code);
12200       putc ('-', file);
12201       output_pic_addr_const (file, XEXP (x, 1), code);
12202       if (!TARGET_MACHO)
12203         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
12204       break;
12205
12206      case UNSPEC:
12207        if (XINT (x, 1) == UNSPEC_STACK_CHECK)
12208          {
12209            bool f = i386_asm_output_addr_const_extra (file, x);
12210            gcc_assert (f);
12211            break;
12212          }
12213
12214        gcc_assert (XVECLEN (x, 0) == 1);
12215        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
12216        switch (XINT (x, 1))
12217         {
12218         case UNSPEC_GOT:
12219           fputs ("@GOT", file);
12220           break;
12221         case UNSPEC_GOTOFF:
12222           fputs ("@GOTOFF", file);
12223           break;
12224         case UNSPEC_PLTOFF:
12225           fputs ("@PLTOFF", file);
12226           break;
12227         case UNSPEC_GOTPCREL:
12228           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12229                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
12230           break;
12231         case UNSPEC_GOTTPOFF:
12232           /* FIXME: This might be @TPOFF in Sun ld too.  */
12233           fputs ("@gottpoff", file);
12234           break;
12235         case UNSPEC_TPOFF:
12236           fputs ("@tpoff", file);
12237           break;
12238         case UNSPEC_NTPOFF:
12239           if (TARGET_64BIT)
12240             fputs ("@tpoff", file);
12241           else
12242             fputs ("@ntpoff", file);
12243           break;
12244         case UNSPEC_DTPOFF:
12245           fputs ("@dtpoff", file);
12246           break;
12247         case UNSPEC_GOTNTPOFF:
12248           if (TARGET_64BIT)
12249             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
12250                    "@gottpoff(%rip)": "@gottpoff[rip]", file);
12251           else
12252             fputs ("@gotntpoff", file);
12253           break;
12254         case UNSPEC_INDNTPOFF:
12255           fputs ("@indntpoff", file);
12256           break;
12257 #if TARGET_MACHO
12258         case UNSPEC_MACHOPIC_OFFSET:
12259           putc ('-', file);
12260           machopic_output_function_base_name (file);
12261           break;
12262 #endif
12263         default:
12264           output_operand_lossage ("invalid UNSPEC as operand");
12265           break;
12266         }
12267        break;
12268
12269     default:
12270       output_operand_lossage ("invalid expression as operand");
12271     }
12272 }
12273
12274 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
12275    We need to emit DTP-relative relocations.  */
12276
12277 static void ATTRIBUTE_UNUSED
12278 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
12279 {
12280   fputs (ASM_LONG, file);
12281   output_addr_const (file, x);
12282   fputs ("@dtpoff", file);
12283   switch (size)
12284     {
12285     case 4:
12286       break;
12287     case 8:
12288       fputs (", 0", file);
12289       break;
12290     default:
12291       gcc_unreachable ();
12292    }
12293 }
12294
12295 /* Return true if X is a representation of the PIC register.  This copes
12296    with calls from ix86_find_base_term, where the register might have
12297    been replaced by a cselib value.  */
12298
12299 static bool
12300 ix86_pic_register_p (rtx x)
12301 {
12302   if (GET_CODE (x) == VALUE && CSELIB_VAL_PTR (x))
12303     return (pic_offset_table_rtx
12304             && rtx_equal_for_cselib_p (x, pic_offset_table_rtx));
12305   else
12306     return REG_P (x) && REGNO (x) == PIC_OFFSET_TABLE_REGNUM;
12307 }
12308
12309 /* Helper function for ix86_delegitimize_address.
12310    Attempt to delegitimize TLS local-exec accesses.  */
12311
12312 static rtx
12313 ix86_delegitimize_tls_address (rtx orig_x)
12314 {
12315   rtx x = orig_x, unspec;
12316   struct ix86_address addr;
12317
12318   if (!TARGET_TLS_DIRECT_SEG_REFS)
12319     return orig_x;
12320   if (MEM_P (x))
12321     x = XEXP (x, 0);
12322   if (GET_CODE (x) != PLUS || GET_MODE (x) != Pmode)
12323     return orig_x;
12324   if (ix86_decompose_address (x, &addr) == 0
12325       || addr.seg != (TARGET_64BIT ? SEG_FS : SEG_GS)
12326       || addr.disp == NULL_RTX
12327       || GET_CODE (addr.disp) != CONST)
12328     return orig_x;
12329   unspec = XEXP (addr.disp, 0);
12330   if (GET_CODE (unspec) == PLUS && CONST_INT_P (XEXP (unspec, 1)))
12331     unspec = XEXP (unspec, 0);
12332   if (GET_CODE (unspec) != UNSPEC || XINT (unspec, 1) != UNSPEC_NTPOFF)
12333     return orig_x;
12334   x = XVECEXP (unspec, 0, 0);
12335   gcc_assert (GET_CODE (x) == SYMBOL_REF);
12336   if (unspec != XEXP (addr.disp, 0))
12337     x = gen_rtx_PLUS (Pmode, x, XEXP (XEXP (addr.disp, 0), 1));
12338   if (addr.index)
12339     {
12340       rtx idx = addr.index;
12341       if (addr.scale != 1)
12342         idx = gen_rtx_MULT (Pmode, idx, GEN_INT (addr.scale));
12343       x = gen_rtx_PLUS (Pmode, idx, x);
12344     }
12345   if (addr.base)
12346     x = gen_rtx_PLUS (Pmode, addr.base, x);
12347   if (MEM_P (orig_x))
12348     x = replace_equiv_address_nv (orig_x, x);
12349   return x;
12350 }
12351
12352 /* In the name of slightly smaller debug output, and to cater to
12353    general assembler lossage, recognize PIC+GOTOFF and turn it back
12354    into a direct symbol reference.
12355
12356    On Darwin, this is necessary to avoid a crash, because Darwin
12357    has a different PIC label for each routine but the DWARF debugging
12358    information is not associated with any particular routine, so it's
12359    necessary to remove references to the PIC label from RTL stored by
12360    the DWARF output code.  */
12361
12362 static rtx
12363 ix86_delegitimize_address (rtx x)
12364 {
12365   rtx orig_x = delegitimize_mem_from_attrs (x);
12366   /* addend is NULL or some rtx if x is something+GOTOFF where
12367      something doesn't include the PIC register.  */
12368   rtx addend = NULL_RTX;
12369   /* reg_addend is NULL or a multiple of some register.  */
12370   rtx reg_addend = NULL_RTX;
12371   /* const_addend is NULL or a const_int.  */
12372   rtx const_addend = NULL_RTX;
12373   /* This is the result, or NULL.  */
12374   rtx result = NULL_RTX;
12375
12376   x = orig_x;
12377
12378   if (MEM_P (x))
12379     x = XEXP (x, 0);
12380
12381   if (TARGET_64BIT)
12382     {
12383       if (GET_CODE (x) != CONST
12384           || GET_CODE (XEXP (x, 0)) != UNSPEC
12385           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
12386           || !MEM_P (orig_x))
12387         return ix86_delegitimize_tls_address (orig_x);
12388       x = XVECEXP (XEXP (x, 0), 0, 0);
12389       if (GET_MODE (orig_x) != Pmode)
12390         return simplify_gen_subreg (GET_MODE (orig_x), x, Pmode, 0);
12391       return x;
12392     }
12393
12394   if (GET_CODE (x) != PLUS
12395       || GET_CODE (XEXP (x, 1)) != CONST)
12396     return ix86_delegitimize_tls_address (orig_x);
12397
12398   if (ix86_pic_register_p (XEXP (x, 0)))
12399     /* %ebx + GOT/GOTOFF */
12400     ;
12401   else if (GET_CODE (XEXP (x, 0)) == PLUS)
12402     {
12403       /* %ebx + %reg * scale + GOT/GOTOFF */
12404       reg_addend = XEXP (x, 0);
12405       if (ix86_pic_register_p (XEXP (reg_addend, 0)))
12406         reg_addend = XEXP (reg_addend, 1);
12407       else if (ix86_pic_register_p (XEXP (reg_addend, 1)))
12408         reg_addend = XEXP (reg_addend, 0);
12409       else
12410         {
12411           reg_addend = NULL_RTX;
12412           addend = XEXP (x, 0);
12413         }
12414     }
12415   else
12416     addend = XEXP (x, 0);
12417
12418   x = XEXP (XEXP (x, 1), 0);
12419   if (GET_CODE (x) == PLUS
12420       && CONST_INT_P (XEXP (x, 1)))
12421     {
12422       const_addend = XEXP (x, 1);
12423       x = XEXP (x, 0);
12424     }
12425
12426   if (GET_CODE (x) == UNSPEC
12427       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x) && !addend)
12428           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
12429     result = XVECEXP (x, 0, 0);
12430
12431   if (TARGET_MACHO && darwin_local_data_pic (x)
12432       && !MEM_P (orig_x))
12433     result = XVECEXP (x, 0, 0);
12434
12435   if (! result)
12436     return ix86_delegitimize_tls_address (orig_x);
12437
12438   if (const_addend)
12439     result = gen_rtx_CONST (Pmode, gen_rtx_PLUS (Pmode, result, const_addend));
12440   if (reg_addend)
12441     result = gen_rtx_PLUS (Pmode, reg_addend, result);
12442   if (addend)
12443     {
12444       /* If the rest of original X doesn't involve the PIC register, add
12445          addend and subtract pic_offset_table_rtx.  This can happen e.g.
12446          for code like:
12447          leal (%ebx, %ecx, 4), %ecx
12448          ...
12449          movl foo@GOTOFF(%ecx), %edx
12450          in which case we return (%ecx - %ebx) + foo.  */
12451       if (pic_offset_table_rtx)
12452         result = gen_rtx_PLUS (Pmode, gen_rtx_MINUS (Pmode, copy_rtx (addend),
12453                                                      pic_offset_table_rtx),
12454                                result);
12455       else
12456         return orig_x;
12457     }
12458   if (GET_MODE (orig_x) != Pmode && MEM_P (orig_x))
12459     return simplify_gen_subreg (GET_MODE (orig_x), result, Pmode, 0);
12460   return result;
12461 }
12462
12463 /* If X is a machine specific address (i.e. a symbol or label being
12464    referenced as a displacement from the GOT implemented using an
12465    UNSPEC), then return the base term.  Otherwise return X.  */
12466
12467 rtx
12468 ix86_find_base_term (rtx x)
12469 {
12470   rtx term;
12471
12472   if (TARGET_64BIT)
12473     {
12474       if (GET_CODE (x) != CONST)
12475         return x;
12476       term = XEXP (x, 0);
12477       if (GET_CODE (term) == PLUS
12478           && (CONST_INT_P (XEXP (term, 1))
12479               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
12480         term = XEXP (term, 0);
12481       if (GET_CODE (term) != UNSPEC
12482           || XINT (term, 1) != UNSPEC_GOTPCREL)
12483         return x;
12484
12485       return XVECEXP (term, 0, 0);
12486     }
12487
12488   return ix86_delegitimize_address (x);
12489 }
12490 \f
12491 static void
12492 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
12493                     int fp, FILE *file)
12494 {
12495   const char *suffix;
12496
12497   if (mode == CCFPmode || mode == CCFPUmode)
12498     {
12499       code = ix86_fp_compare_code_to_integer (code);
12500       mode = CCmode;
12501     }
12502   if (reverse)
12503     code = reverse_condition (code);
12504
12505   switch (code)
12506     {
12507     case EQ:
12508       switch (mode)
12509         {
12510         case CCAmode:
12511           suffix = "a";
12512           break;
12513
12514         case CCCmode:
12515           suffix = "c";
12516           break;
12517
12518         case CCOmode:
12519           suffix = "o";
12520           break;
12521
12522         case CCSmode:
12523           suffix = "s";
12524           break;
12525
12526         default:
12527           suffix = "e";
12528         }
12529       break;
12530     case NE:
12531       switch (mode)
12532         {
12533         case CCAmode:
12534           suffix = "na";
12535           break;
12536
12537         case CCCmode:
12538           suffix = "nc";
12539           break;
12540
12541         case CCOmode:
12542           suffix = "no";
12543           break;
12544
12545         case CCSmode:
12546           suffix = "ns";
12547           break;
12548
12549         default:
12550           suffix = "ne";
12551         }
12552       break;
12553     case GT:
12554       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
12555       suffix = "g";
12556       break;
12557     case GTU:
12558       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
12559          Those same assemblers have the same but opposite lossage on cmov.  */
12560       if (mode == CCmode)
12561         suffix = fp ? "nbe" : "a";
12562       else if (mode == CCCmode)
12563         suffix = "b";
12564       else
12565         gcc_unreachable ();
12566       break;
12567     case LT:
12568       switch (mode)
12569         {
12570         case CCNOmode:
12571         case CCGOCmode:
12572           suffix = "s";
12573           break;
12574
12575         case CCmode:
12576         case CCGCmode:
12577           suffix = "l";
12578           break;
12579
12580         default:
12581           gcc_unreachable ();
12582         }
12583       break;
12584     case LTU:
12585       gcc_assert (mode == CCmode || mode == CCCmode);
12586       suffix = "b";
12587       break;
12588     case GE:
12589       switch (mode)
12590         {
12591         case CCNOmode:
12592         case CCGOCmode:
12593           suffix = "ns";
12594           break;
12595
12596         case CCmode:
12597         case CCGCmode:
12598           suffix = "ge";
12599           break;
12600
12601         default:
12602           gcc_unreachable ();
12603         }
12604       break;
12605     case GEU:
12606       /* ??? As above.  */
12607       gcc_assert (mode == CCmode || mode == CCCmode);
12608       suffix = fp ? "nb" : "ae";
12609       break;
12610     case LE:
12611       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
12612       suffix = "le";
12613       break;
12614     case LEU:
12615       /* ??? As above.  */
12616       if (mode == CCmode)
12617         suffix = "be";
12618       else if (mode == CCCmode)
12619         suffix = fp ? "nb" : "ae";
12620       else
12621         gcc_unreachable ();
12622       break;
12623     case UNORDERED:
12624       suffix = fp ? "u" : "p";
12625       break;
12626     case ORDERED:
12627       suffix = fp ? "nu" : "np";
12628       break;
12629     default:
12630       gcc_unreachable ();
12631     }
12632   fputs (suffix, file);
12633 }
12634
12635 /* Print the name of register X to FILE based on its machine mode and number.
12636    If CODE is 'w', pretend the mode is HImode.
12637    If CODE is 'b', pretend the mode is QImode.
12638    If CODE is 'k', pretend the mode is SImode.
12639    If CODE is 'q', pretend the mode is DImode.
12640    If CODE is 'x', pretend the mode is V4SFmode.
12641    If CODE is 't', pretend the mode is V8SFmode.
12642    If CODE is 'h', pretend the reg is the 'high' byte register.
12643    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.
12644    If CODE is 'd', duplicate the operand for AVX instruction.
12645  */
12646
12647 void
12648 print_reg (rtx x, int code, FILE *file)
12649 {
12650   const char *reg;
12651   bool duplicated = code == 'd' && TARGET_AVX;
12652
12653   gcc_assert (x == pc_rtx
12654               || (REGNO (x) != ARG_POINTER_REGNUM
12655                   && REGNO (x) != FRAME_POINTER_REGNUM
12656                   && REGNO (x) != FLAGS_REG
12657                   && REGNO (x) != FPSR_REG
12658                   && REGNO (x) != FPCR_REG));
12659
12660   if (ASSEMBLER_DIALECT == ASM_ATT)
12661     putc ('%', file);
12662
12663   if (x == pc_rtx)
12664     {
12665       gcc_assert (TARGET_64BIT);
12666       fputs ("rip", file);
12667       return;
12668     }
12669
12670   if (code == 'w' || MMX_REG_P (x))
12671     code = 2;
12672   else if (code == 'b')
12673     code = 1;
12674   else if (code == 'k')
12675     code = 4;
12676   else if (code == 'q')
12677     code = 8;
12678   else if (code == 'y')
12679     code = 3;
12680   else if (code == 'h')
12681     code = 0;
12682   else if (code == 'x')
12683     code = 16;
12684   else if (code == 't')
12685     code = 32;
12686   else
12687     code = GET_MODE_SIZE (GET_MODE (x));
12688
12689   /* Irritatingly, AMD extended registers use different naming convention
12690      from the normal registers.  */
12691   if (REX_INT_REG_P (x))
12692     {
12693       gcc_assert (TARGET_64BIT);
12694       switch (code)
12695         {
12696           case 0:
12697             error ("extended registers have no high halves");
12698             break;
12699           case 1:
12700             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
12701             break;
12702           case 2:
12703             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
12704             break;
12705           case 4:
12706             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
12707             break;
12708           case 8:
12709             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
12710             break;
12711           default:
12712             error ("unsupported operand size for extended register");
12713             break;
12714         }
12715       return;
12716     }
12717
12718   reg = NULL;
12719   switch (code)
12720     {
12721     case 3:
12722       if (STACK_TOP_P (x))
12723         {
12724           reg = "st(0)";
12725           break;
12726         }
12727       /* FALLTHRU */
12728     case 8:
12729     case 4:
12730     case 12:
12731       if (! ANY_FP_REG_P (x))
12732         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
12733       /* FALLTHRU */
12734     case 16:
12735     case 2:
12736     normal:
12737       reg = hi_reg_name[REGNO (x)];
12738       break;
12739     case 1:
12740       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
12741         goto normal;
12742       reg = qi_reg_name[REGNO (x)];
12743       break;
12744     case 0:
12745       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
12746         goto normal;
12747       reg = qi_high_reg_name[REGNO (x)];
12748       break;
12749     case 32:
12750       if (SSE_REG_P (x))
12751         {
12752           gcc_assert (!duplicated);
12753           putc ('y', file);
12754           fputs (hi_reg_name[REGNO (x)] + 1, file);
12755           return;
12756         }
12757       break;
12758     default:
12759       gcc_unreachable ();
12760     }
12761
12762   fputs (reg, file);
12763   if (duplicated)
12764     {
12765       if (ASSEMBLER_DIALECT == ASM_ATT)
12766         fprintf (file, ", %%%s", reg);
12767       else
12768         fprintf (file, ", %s", reg);
12769     }
12770 }
12771
12772 /* Locate some local-dynamic symbol still in use by this function
12773    so that we can print its name in some tls_local_dynamic_base
12774    pattern.  */
12775
12776 static int
12777 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
12778 {
12779   rtx x = *px;
12780
12781   if (GET_CODE (x) == SYMBOL_REF
12782       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
12783     {
12784       cfun->machine->some_ld_name = XSTR (x, 0);
12785       return 1;
12786     }
12787
12788   return 0;
12789 }
12790
12791 static const char *
12792 get_some_local_dynamic_name (void)
12793 {
12794   rtx insn;
12795
12796   if (cfun->machine->some_ld_name)
12797     return cfun->machine->some_ld_name;
12798
12799   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
12800     if (NONDEBUG_INSN_P (insn)
12801         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
12802       return cfun->machine->some_ld_name;
12803
12804   return NULL;
12805 }
12806
12807 /* Meaning of CODE:
12808    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
12809    C -- print opcode suffix for set/cmov insn.
12810    c -- like C, but print reversed condition
12811    F,f -- likewise, but for floating-point.
12812    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
12813         otherwise nothing
12814    R -- print the prefix for register names.
12815    z -- print the opcode suffix for the size of the current operand.
12816    Z -- likewise, with special suffixes for x87 instructions.
12817    * -- print a star (in certain assembler syntax)
12818    A -- print an absolute memory reference.
12819    w -- print the operand as if it's a "word" (HImode) even if it isn't.
12820    s -- print a shift double count, followed by the assemblers argument
12821         delimiter.
12822    b -- print the QImode name of the register for the indicated operand.
12823         %b0 would print %al if operands[0] is reg 0.
12824    w --  likewise, print the HImode name of the register.
12825    k --  likewise, print the SImode name of the register.
12826    q --  likewise, print the DImode name of the register.
12827    x --  likewise, print the V4SFmode name of the register.
12828    t --  likewise, print the V8SFmode name of the register.
12829    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
12830    y -- print "st(0)" instead of "st" as a register.
12831    d -- print duplicated register operand for AVX instruction.
12832    D -- print condition for SSE cmp instruction.
12833    P -- if PIC, print an @PLT suffix.
12834    X -- don't print any sort of PIC '@' suffix for a symbol.
12835    & -- print some in-use local-dynamic symbol name.
12836    H -- print a memory address offset by 8; used for sse high-parts
12837    Y -- print condition for XOP pcom* instruction.
12838    + -- print a branch hint as 'cs' or 'ds' prefix
12839    ; -- print a semicolon (after prefixes due to bug in older gas).
12840    @ -- print a segment register of thread base pointer load
12841  */
12842
12843 void
12844 ix86_print_operand (FILE *file, rtx x, int code)
12845 {
12846   if (code)
12847     {
12848       switch (code)
12849         {
12850         case '*':
12851           if (ASSEMBLER_DIALECT == ASM_ATT)
12852             putc ('*', file);
12853           return;
12854
12855         case '&':
12856           {
12857             const char *name = get_some_local_dynamic_name ();
12858             if (name == NULL)
12859               output_operand_lossage ("'%%&' used without any "
12860                                       "local dynamic TLS references");
12861             else
12862               assemble_name (file, name);
12863             return;
12864           }
12865
12866         case 'A':
12867           switch (ASSEMBLER_DIALECT)
12868             {
12869             case ASM_ATT:
12870               putc ('*', file);
12871               break;
12872
12873             case ASM_INTEL:
12874               /* Intel syntax. For absolute addresses, registers should not
12875                  be surrounded by braces.  */
12876               if (!REG_P (x))
12877                 {
12878                   putc ('[', file);
12879                   ix86_print_operand (file, x, 0);
12880                   putc (']', file);
12881                   return;
12882                 }
12883               break;
12884
12885             default:
12886               gcc_unreachable ();
12887             }
12888
12889           ix86_print_operand (file, x, 0);
12890           return;
12891
12892
12893         case 'L':
12894           if (ASSEMBLER_DIALECT == ASM_ATT)
12895             putc ('l', file);
12896           return;
12897
12898         case 'W':
12899           if (ASSEMBLER_DIALECT == ASM_ATT)
12900             putc ('w', file);
12901           return;
12902
12903         case 'B':
12904           if (ASSEMBLER_DIALECT == ASM_ATT)
12905             putc ('b', file);
12906           return;
12907
12908         case 'Q':
12909           if (ASSEMBLER_DIALECT == ASM_ATT)
12910             putc ('l', file);
12911           return;
12912
12913         case 'S':
12914           if (ASSEMBLER_DIALECT == ASM_ATT)
12915             putc ('s', file);
12916           return;
12917
12918         case 'T':
12919           if (ASSEMBLER_DIALECT == ASM_ATT)
12920             putc ('t', file);
12921           return;
12922
12923         case 'z':
12924           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
12925             {
12926               /* Opcodes don't get size suffixes if using Intel opcodes.  */
12927               if (ASSEMBLER_DIALECT == ASM_INTEL)
12928                 return;
12929
12930               switch (GET_MODE_SIZE (GET_MODE (x)))
12931                 {
12932                 case 1:
12933                   putc ('b', file);
12934                   return;
12935
12936                 case 2:
12937                   putc ('w', file);
12938                   return;
12939
12940                 case 4:
12941                   putc ('l', file);
12942                   return;
12943
12944                 case 8:
12945                   putc ('q', file);
12946                   return;
12947
12948                 default:
12949                   output_operand_lossage
12950                     ("invalid operand size for operand code '%c'", code);
12951                   return;
12952                 }
12953             }
12954
12955           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
12956             warning
12957               (0, "non-integer operand used with operand code '%c'", code);
12958           /* FALLTHRU */
12959
12960         case 'Z':
12961           /* 387 opcodes don't get size suffixes if using Intel opcodes.  */
12962           if (ASSEMBLER_DIALECT == ASM_INTEL)
12963             return;
12964
12965           if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
12966             {
12967               switch (GET_MODE_SIZE (GET_MODE (x)))
12968                 {
12969                 case 2:
12970 #ifdef HAVE_AS_IX86_FILDS
12971                   putc ('s', file);
12972 #endif
12973                   return;
12974
12975                 case 4:
12976                   putc ('l', file);
12977                   return;
12978
12979                 case 8:
12980 #ifdef HAVE_AS_IX86_FILDQ
12981                   putc ('q', file);
12982 #else
12983                   fputs ("ll", file);
12984 #endif
12985                   return;
12986
12987                 default:
12988                   break;
12989                 }
12990             }
12991           else if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
12992             {
12993               /* 387 opcodes don't get size suffixes
12994                  if the operands are registers.  */
12995               if (STACK_REG_P (x))
12996                 return;
12997
12998               switch (GET_MODE_SIZE (GET_MODE (x)))
12999                 {
13000                 case 4:
13001                   putc ('s', file);
13002                   return;
13003
13004                 case 8:
13005                   putc ('l', file);
13006                   return;
13007
13008                 case 12:
13009                 case 16:
13010                   putc ('t', file);
13011                   return;
13012
13013                 default:
13014                   break;
13015                 }
13016             }
13017           else
13018             {
13019               output_operand_lossage
13020                 ("invalid operand type used with operand code '%c'", code);
13021               return;
13022             }
13023
13024           output_operand_lossage
13025             ("invalid operand size for operand code '%c'", code);
13026           return;
13027
13028         case 'd':
13029         case 'b':
13030         case 'w':
13031         case 'k':
13032         case 'q':
13033         case 'h':
13034         case 't':
13035         case 'y':
13036         case 'x':
13037         case 'X':
13038         case 'P':
13039           break;
13040
13041         case 's':
13042           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
13043             {
13044               ix86_print_operand (file, x, 0);
13045               fputs (", ", file);
13046             }
13047           return;
13048
13049         case 'D':
13050           /* Little bit of braindamage here.  The SSE compare instructions
13051              does use completely different names for the comparisons that the
13052              fp conditional moves.  */
13053           if (TARGET_AVX)
13054             {
13055               switch (GET_CODE (x))
13056                 {
13057                 case EQ:
13058                   fputs ("eq", file);
13059                   break;
13060                 case UNEQ:
13061                   fputs ("eq_us", file);
13062                   break;
13063                 case LT:
13064                   fputs ("lt", file);
13065                   break;
13066                 case UNLT:
13067                   fputs ("nge", file);
13068                   break;
13069                 case LE:
13070                   fputs ("le", file);
13071                   break;
13072                 case UNLE:
13073                   fputs ("ngt", file);
13074                   break;
13075                 case UNORDERED:
13076                   fputs ("unord", file);
13077                   break;
13078                 case NE:
13079                   fputs ("neq", file);
13080                   break;
13081                 case LTGT:
13082                   fputs ("neq_oq", file);
13083                   break;
13084                 case GE:
13085                   fputs ("ge", file);
13086                   break;
13087                 case UNGE:
13088                   fputs ("nlt", file);
13089                   break;
13090                 case GT:
13091                   fputs ("gt", file);
13092                   break;
13093                 case UNGT:
13094                   fputs ("nle", file);
13095                   break;
13096                 case ORDERED:
13097                   fputs ("ord", file);
13098                   break;
13099                 default:
13100                   output_operand_lossage ("operand is not a condition code, "
13101                                           "invalid operand code 'D'");
13102                   return;
13103                 }
13104             }
13105           else
13106             {
13107               switch (GET_CODE (x))
13108                 {
13109                 case EQ:
13110                 case UNEQ:
13111                   fputs ("eq", file);
13112                   break;
13113                 case LT:
13114                 case UNLT:
13115                   fputs ("lt", file);
13116                   break;
13117                 case LE:
13118                 case UNLE:
13119                   fputs ("le", file);
13120                   break;
13121                 case UNORDERED:
13122                   fputs ("unord", file);
13123                   break;
13124                 case NE:
13125                 case LTGT:
13126                   fputs ("neq", file);
13127                   break;
13128                 case UNGE:
13129                 case GE:
13130                   fputs ("nlt", file);
13131                   break;
13132                 case UNGT:
13133                 case GT:
13134                   fputs ("nle", file);
13135                   break;
13136                 case ORDERED:
13137                   fputs ("ord", file);
13138                   break;
13139                 default:
13140                   output_operand_lossage ("operand is not a condition code, "
13141                                           "invalid operand code 'D'");
13142                   return;
13143                 }
13144             }
13145           return;
13146         case 'O':
13147 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13148           if (ASSEMBLER_DIALECT == ASM_ATT)
13149             {
13150               switch (GET_MODE (x))
13151                 {
13152                 case HImode: putc ('w', file); break;
13153                 case SImode:
13154                 case SFmode: putc ('l', file); break;
13155                 case DImode:
13156                 case DFmode: putc ('q', file); break;
13157                 default: gcc_unreachable ();
13158                 }
13159               putc ('.', file);
13160             }
13161 #endif
13162           return;
13163         case 'C':
13164           if (!COMPARISON_P (x))
13165             {
13166               output_operand_lossage ("operand is neither a constant nor a "
13167                                       "condition code, invalid operand code "
13168                                       "'C'");
13169               return;
13170             }
13171           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
13172           return;
13173         case 'F':
13174           if (!COMPARISON_P (x))
13175             {
13176               output_operand_lossage ("operand is neither a constant nor a "
13177                                       "condition code, invalid operand code "
13178                                       "'F'");
13179               return;
13180             }
13181 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13182           if (ASSEMBLER_DIALECT == ASM_ATT)
13183             putc ('.', file);
13184 #endif
13185           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
13186           return;
13187
13188           /* Like above, but reverse condition */
13189         case 'c':
13190           /* Check to see if argument to %c is really a constant
13191              and not a condition code which needs to be reversed.  */
13192           if (!COMPARISON_P (x))
13193             {
13194               output_operand_lossage ("operand is neither a constant nor a "
13195                                       "condition code, invalid operand "
13196                                       "code 'c'");
13197               return;
13198             }
13199           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
13200           return;
13201         case 'f':
13202           if (!COMPARISON_P (x))
13203             {
13204               output_operand_lossage ("operand is neither a constant nor a "
13205                                       "condition code, invalid operand "
13206                                       "code 'f'");
13207               return;
13208             }
13209 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
13210           if (ASSEMBLER_DIALECT == ASM_ATT)
13211             putc ('.', file);
13212 #endif
13213           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
13214           return;
13215
13216         case 'H':
13217           /* It doesn't actually matter what mode we use here, as we're
13218              only going to use this for printing.  */
13219           x = adjust_address_nv (x, DImode, 8);
13220           break;
13221
13222         case '+':
13223           {
13224             rtx x;
13225
13226             if (!optimize
13227                 || optimize_function_for_size_p (cfun) || !TARGET_BRANCH_PREDICTION_HINTS)
13228               return;
13229
13230             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
13231             if (x)
13232               {
13233                 int pred_val = INTVAL (XEXP (x, 0));
13234
13235                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
13236                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
13237                   {
13238                     int taken = pred_val > REG_BR_PROB_BASE / 2;
13239                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
13240
13241                     /* Emit hints only in the case default branch prediction
13242                        heuristics would fail.  */
13243                     if (taken != cputaken)
13244                       {
13245                         /* We use 3e (DS) prefix for taken branches and
13246                            2e (CS) prefix for not taken branches.  */
13247                         if (taken)
13248                           fputs ("ds ; ", file);
13249                         else
13250                           fputs ("cs ; ", file);
13251                       }
13252                   }
13253               }
13254             return;
13255           }
13256
13257         case 'Y':
13258           switch (GET_CODE (x))
13259             {
13260             case NE:
13261               fputs ("neq", file);
13262               break;
13263             case EQ:
13264               fputs ("eq", file);
13265               break;
13266             case GE:
13267             case GEU:
13268               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
13269               break;
13270             case GT:
13271             case GTU:
13272               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
13273               break;
13274             case LE:
13275             case LEU:
13276               fputs ("le", file);
13277               break;
13278             case LT:
13279             case LTU:
13280               fputs ("lt", file);
13281               break;
13282             case UNORDERED:
13283               fputs ("unord", file);
13284               break;
13285             case ORDERED:
13286               fputs ("ord", file);
13287               break;
13288             case UNEQ:
13289               fputs ("ueq", file);
13290               break;
13291             case UNGE:
13292               fputs ("nlt", file);
13293               break;
13294             case UNGT:
13295               fputs ("nle", file);
13296               break;
13297             case UNLE:
13298               fputs ("ule", file);
13299               break;
13300             case UNLT:
13301               fputs ("ult", file);
13302               break;
13303             case LTGT:
13304               fputs ("une", file);
13305               break;
13306             default:
13307               output_operand_lossage ("operand is not a condition code, "
13308                                       "invalid operand code 'Y'");
13309               return;
13310             }
13311           return;
13312
13313         case ';':
13314 #ifndef HAVE_AS_IX86_REP_LOCK_PREFIX
13315           putc (';', file);
13316 #endif
13317           return;
13318
13319         case '@':
13320           if (ASSEMBLER_DIALECT == ASM_ATT)
13321             putc ('%', file);
13322
13323           /* The kernel uses a different segment register for performance
13324              reasons; a system call would not have to trash the userspace
13325              segment register, which would be expensive.  */
13326           if (TARGET_64BIT && ix86_cmodel != CM_KERNEL)
13327             fputs ("fs", file);
13328           else
13329             fputs ("gs", file);
13330           return;
13331
13332         default:
13333             output_operand_lossage ("invalid operand code '%c'", code);
13334         }
13335     }
13336
13337   if (REG_P (x))
13338     print_reg (x, code, file);
13339
13340   else if (MEM_P (x))
13341     {
13342       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
13343       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
13344           && GET_MODE (x) != BLKmode)
13345         {
13346           const char * size;
13347           switch (GET_MODE_SIZE (GET_MODE (x)))
13348             {
13349             case 1: size = "BYTE"; break;
13350             case 2: size = "WORD"; break;
13351             case 4: size = "DWORD"; break;
13352             case 8: size = "QWORD"; break;
13353             case 12: size = "TBYTE"; break;
13354             case 16:
13355               if (GET_MODE (x) == XFmode)
13356                 size = "TBYTE";
13357               else
13358                 size = "XMMWORD";
13359               break;
13360             case 32: size = "YMMWORD"; break;
13361             default:
13362               gcc_unreachable ();
13363             }
13364
13365           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
13366           if (code == 'b')
13367             size = "BYTE";
13368           else if (code == 'w')
13369             size = "WORD";
13370           else if (code == 'k')
13371             size = "DWORD";
13372
13373           fputs (size, file);
13374           fputs (" PTR ", file);
13375         }
13376
13377       x = XEXP (x, 0);
13378       /* Avoid (%rip) for call operands.  */
13379       if (CONSTANT_ADDRESS_P (x) && code == 'P'
13380           && !CONST_INT_P (x))
13381         output_addr_const (file, x);
13382       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
13383         output_operand_lossage ("invalid constraints for operand");
13384       else
13385         output_address (x);
13386     }
13387
13388   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
13389     {
13390       REAL_VALUE_TYPE r;
13391       long l;
13392
13393       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
13394       REAL_VALUE_TO_TARGET_SINGLE (r, l);
13395
13396       if (ASSEMBLER_DIALECT == ASM_ATT)
13397         putc ('$', file);
13398       /* Sign extend 32bit SFmode immediate to 8 bytes.  */
13399       if (code == 'q')
13400         fprintf (file, "0x%08llx", (unsigned long long) (int) l);
13401       else
13402         fprintf (file, "0x%08x", (unsigned int) l);
13403     }
13404
13405   /* These float cases don't actually occur as immediate operands.  */
13406   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
13407     {
13408       char dstr[30];
13409
13410       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
13411       fputs (dstr, file);
13412     }
13413
13414   else if (GET_CODE (x) == CONST_DOUBLE
13415            && GET_MODE (x) == XFmode)
13416     {
13417       char dstr[30];
13418
13419       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
13420       fputs (dstr, file);
13421     }
13422
13423   else
13424     {
13425       /* We have patterns that allow zero sets of memory, for instance.
13426          In 64-bit mode, we should probably support all 8-byte vectors,
13427          since we can in fact encode that into an immediate.  */
13428       if (GET_CODE (x) == CONST_VECTOR)
13429         {
13430           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
13431           x = const0_rtx;
13432         }
13433
13434       if (code != 'P')
13435         {
13436           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
13437             {
13438               if (ASSEMBLER_DIALECT == ASM_ATT)
13439                 putc ('$', file);
13440             }
13441           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
13442                    || GET_CODE (x) == LABEL_REF)
13443             {
13444               if (ASSEMBLER_DIALECT == ASM_ATT)
13445                 putc ('$', file);
13446               else
13447                 fputs ("OFFSET FLAT:", file);
13448             }
13449         }
13450       if (CONST_INT_P (x))
13451         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
13452       else if (flag_pic)
13453         output_pic_addr_const (file, x, code);
13454       else
13455         output_addr_const (file, x);
13456     }
13457 }
13458
13459 static bool
13460 ix86_print_operand_punct_valid_p (unsigned char code)
13461 {
13462   return (code == '@' || code == '*' || code == '+'
13463           || code == '&' || code == ';');
13464 }
13465 \f
13466 /* Print a memory operand whose address is ADDR.  */
13467
13468 static void
13469 ix86_print_operand_address (FILE *file, rtx addr)
13470 {
13471   struct ix86_address parts;
13472   rtx base, index, disp;
13473   int scale;
13474   int ok = ix86_decompose_address (addr, &parts);
13475
13476   gcc_assert (ok);
13477
13478   base = parts.base;
13479   index = parts.index;
13480   disp = parts.disp;
13481   scale = parts.scale;
13482
13483   switch (parts.seg)
13484     {
13485     case SEG_DEFAULT:
13486       break;
13487     case SEG_FS:
13488     case SEG_GS:
13489       if (ASSEMBLER_DIALECT == ASM_ATT)
13490         putc ('%', file);
13491       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
13492       break;
13493     default:
13494       gcc_unreachable ();
13495     }
13496
13497   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
13498   if (TARGET_64BIT && !base && !index)
13499     {
13500       rtx symbol = disp;
13501
13502       if (GET_CODE (disp) == CONST
13503           && GET_CODE (XEXP (disp, 0)) == PLUS
13504           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
13505         symbol = XEXP (XEXP (disp, 0), 0);
13506
13507       if (GET_CODE (symbol) == LABEL_REF
13508           || (GET_CODE (symbol) == SYMBOL_REF
13509               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
13510         base = pc_rtx;
13511     }
13512   if (!base && !index)
13513     {
13514       /* Displacement only requires special attention.  */
13515
13516       if (CONST_INT_P (disp))
13517         {
13518           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
13519             fputs ("ds:", file);
13520           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
13521         }
13522       else if (flag_pic)
13523         output_pic_addr_const (file, disp, 0);
13524       else
13525         output_addr_const (file, disp);
13526     }
13527   else
13528     {
13529       if (ASSEMBLER_DIALECT == ASM_ATT)
13530         {
13531           if (disp)
13532             {
13533               if (flag_pic)
13534                 output_pic_addr_const (file, disp, 0);
13535               else if (GET_CODE (disp) == LABEL_REF)
13536                 output_asm_label (disp);
13537               else
13538                 output_addr_const (file, disp);
13539             }
13540
13541           putc ('(', file);
13542           if (base)
13543             print_reg (base, 0, file);
13544           if (index)
13545             {
13546               putc (',', file);
13547               print_reg (index, 0, file);
13548               if (scale != 1)
13549                 fprintf (file, ",%d", scale);
13550             }
13551           putc (')', file);
13552         }
13553       else
13554         {
13555           rtx offset = NULL_RTX;
13556
13557           if (disp)
13558             {
13559               /* Pull out the offset of a symbol; print any symbol itself.  */
13560               if (GET_CODE (disp) == CONST
13561                   && GET_CODE (XEXP (disp, 0)) == PLUS
13562                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
13563                 {
13564                   offset = XEXP (XEXP (disp, 0), 1);
13565                   disp = gen_rtx_CONST (VOIDmode,
13566                                         XEXP (XEXP (disp, 0), 0));
13567                 }
13568
13569               if (flag_pic)
13570                 output_pic_addr_const (file, disp, 0);
13571               else if (GET_CODE (disp) == LABEL_REF)
13572                 output_asm_label (disp);
13573               else if (CONST_INT_P (disp))
13574                 offset = disp;
13575               else
13576                 output_addr_const (file, disp);
13577             }
13578
13579           putc ('[', file);
13580           if (base)
13581             {
13582               print_reg (base, 0, file);
13583               if (offset)
13584                 {
13585                   if (INTVAL (offset) >= 0)
13586                     putc ('+', file);
13587                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
13588                 }
13589             }
13590           else if (offset)
13591             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
13592           else
13593             putc ('0', file);
13594
13595           if (index)
13596             {
13597               putc ('+', file);
13598               print_reg (index, 0, file);
13599               if (scale != 1)
13600                 fprintf (file, "*%d", scale);
13601             }
13602           putc (']', file);
13603         }
13604     }
13605 }
13606
13607 /* Implementation of TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA.  */
13608
13609 static bool
13610 i386_asm_output_addr_const_extra (FILE *file, rtx x)
13611 {
13612   rtx op;
13613
13614   if (GET_CODE (x) != UNSPEC)
13615     return false;
13616
13617   op = XVECEXP (x, 0, 0);
13618   switch (XINT (x, 1))
13619     {
13620     case UNSPEC_GOTTPOFF:
13621       output_addr_const (file, op);
13622       /* FIXME: This might be @TPOFF in Sun ld.  */
13623       fputs ("@gottpoff", file);
13624       break;
13625     case UNSPEC_TPOFF:
13626       output_addr_const (file, op);
13627       fputs ("@tpoff", file);
13628       break;
13629     case UNSPEC_NTPOFF:
13630       output_addr_const (file, op);
13631       if (TARGET_64BIT)
13632         fputs ("@tpoff", file);
13633       else
13634         fputs ("@ntpoff", file);
13635       break;
13636     case UNSPEC_DTPOFF:
13637       output_addr_const (file, op);
13638       fputs ("@dtpoff", file);
13639       break;
13640     case UNSPEC_GOTNTPOFF:
13641       output_addr_const (file, op);
13642       if (TARGET_64BIT)
13643         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
13644                "@gottpoff(%rip)" : "@gottpoff[rip]", file);
13645       else
13646         fputs ("@gotntpoff", file);
13647       break;
13648     case UNSPEC_INDNTPOFF:
13649       output_addr_const (file, op);
13650       fputs ("@indntpoff", file);
13651       break;
13652 #if TARGET_MACHO
13653     case UNSPEC_MACHOPIC_OFFSET:
13654       output_addr_const (file, op);
13655       putc ('-', file);
13656       machopic_output_function_base_name (file);
13657       break;
13658 #endif
13659
13660     case UNSPEC_STACK_CHECK:
13661       {
13662         int offset;
13663
13664         gcc_assert (flag_split_stack);
13665
13666 #ifdef TARGET_THREAD_SPLIT_STACK_OFFSET
13667         offset = TARGET_THREAD_SPLIT_STACK_OFFSET;
13668 #else
13669         gcc_unreachable ();
13670 #endif
13671
13672         fprintf (file, "%s:%d", TARGET_64BIT ? "%fs" : "%gs", offset);
13673       }
13674       break;
13675
13676     default:
13677       return false;
13678     }
13679
13680   return true;
13681 }
13682 \f
13683 /* Split one or more double-mode RTL references into pairs of half-mode
13684    references.  The RTL can be REG, offsettable MEM, integer constant, or
13685    CONST_DOUBLE.  "operands" is a pointer to an array of double-mode RTLs to
13686    split and "num" is its length.  lo_half and hi_half are output arrays
13687    that parallel "operands".  */
13688
13689 void
13690 split_double_mode (enum machine_mode mode, rtx operands[],
13691                    int num, rtx lo_half[], rtx hi_half[])
13692 {
13693   enum machine_mode half_mode;
13694   unsigned int byte;
13695
13696   switch (mode)
13697     {
13698     case TImode:
13699       half_mode = DImode;
13700       break;
13701     case DImode:
13702       half_mode = SImode;
13703       break;
13704     default:
13705       gcc_unreachable ();
13706     }
13707
13708   byte = GET_MODE_SIZE (half_mode);
13709
13710   while (num--)
13711     {
13712       rtx op = operands[num];
13713
13714       /* simplify_subreg refuse to split volatile memory addresses,
13715          but we still have to handle it.  */
13716       if (MEM_P (op))
13717         {
13718           lo_half[num] = adjust_address (op, half_mode, 0);
13719           hi_half[num] = adjust_address (op, half_mode, byte);
13720         }
13721       else
13722         {
13723           lo_half[num] = simplify_gen_subreg (half_mode, op,
13724                                               GET_MODE (op) == VOIDmode
13725                                               ? mode : GET_MODE (op), 0);
13726           hi_half[num] = simplify_gen_subreg (half_mode, op,
13727                                               GET_MODE (op) == VOIDmode
13728                                               ? mode : GET_MODE (op), byte);
13729         }
13730     }
13731 }
13732 \f
13733 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
13734    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
13735    is the expression of the binary operation.  The output may either be
13736    emitted here, or returned to the caller, like all output_* functions.
13737
13738    There is no guarantee that the operands are the same mode, as they
13739    might be within FLOAT or FLOAT_EXTEND expressions.  */
13740
13741 #ifndef SYSV386_COMPAT
13742 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
13743    wants to fix the assemblers because that causes incompatibility
13744    with gcc.  No-one wants to fix gcc because that causes
13745    incompatibility with assemblers...  You can use the option of
13746    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
13747 #define SYSV386_COMPAT 1
13748 #endif
13749
13750 const char *
13751 output_387_binary_op (rtx insn, rtx *operands)
13752 {
13753   static char buf[40];
13754   const char *p;
13755   const char *ssep;
13756   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
13757
13758 #ifdef ENABLE_CHECKING
13759   /* Even if we do not want to check the inputs, this documents input
13760      constraints.  Which helps in understanding the following code.  */
13761   if (STACK_REG_P (operands[0])
13762       && ((REG_P (operands[1])
13763            && REGNO (operands[0]) == REGNO (operands[1])
13764            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
13765           || (REG_P (operands[2])
13766               && REGNO (operands[0]) == REGNO (operands[2])
13767               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
13768       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
13769     ; /* ok */
13770   else
13771     gcc_assert (is_sse);
13772 #endif
13773
13774   switch (GET_CODE (operands[3]))
13775     {
13776     case PLUS:
13777       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13778           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13779         p = "fiadd";
13780       else
13781         p = "fadd";
13782       ssep = "vadd";
13783       break;
13784
13785     case MINUS:
13786       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13787           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13788         p = "fisub";
13789       else
13790         p = "fsub";
13791       ssep = "vsub";
13792       break;
13793
13794     case MULT:
13795       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13796           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13797         p = "fimul";
13798       else
13799         p = "fmul";
13800       ssep = "vmul";
13801       break;
13802
13803     case DIV:
13804       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
13805           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
13806         p = "fidiv";
13807       else
13808         p = "fdiv";
13809       ssep = "vdiv";
13810       break;
13811
13812     default:
13813       gcc_unreachable ();
13814     }
13815
13816   if (is_sse)
13817    {
13818      if (TARGET_AVX)
13819        {
13820          strcpy (buf, ssep);
13821          if (GET_MODE (operands[0]) == SFmode)
13822            strcat (buf, "ss\t{%2, %1, %0|%0, %1, %2}");
13823          else
13824            strcat (buf, "sd\t{%2, %1, %0|%0, %1, %2}");
13825        }
13826      else
13827        {
13828          strcpy (buf, ssep + 1);
13829          if (GET_MODE (operands[0]) == SFmode)
13830            strcat (buf, "ss\t{%2, %0|%0, %2}");
13831          else
13832            strcat (buf, "sd\t{%2, %0|%0, %2}");
13833        }
13834       return buf;
13835    }
13836   strcpy (buf, p);
13837
13838   switch (GET_CODE (operands[3]))
13839     {
13840     case MULT:
13841     case PLUS:
13842       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
13843         {
13844           rtx temp = operands[2];
13845           operands[2] = operands[1];
13846           operands[1] = temp;
13847         }
13848
13849       /* know operands[0] == operands[1].  */
13850
13851       if (MEM_P (operands[2]))
13852         {
13853           p = "%Z2\t%2";
13854           break;
13855         }
13856
13857       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
13858         {
13859           if (STACK_TOP_P (operands[0]))
13860             /* How is it that we are storing to a dead operand[2]?
13861                Well, presumably operands[1] is dead too.  We can't
13862                store the result to st(0) as st(0) gets popped on this
13863                instruction.  Instead store to operands[2] (which I
13864                think has to be st(1)).  st(1) will be popped later.
13865                gcc <= 2.8.1 didn't have this check and generated
13866                assembly code that the Unixware assembler rejected.  */
13867             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
13868           else
13869             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
13870           break;
13871         }
13872
13873       if (STACK_TOP_P (operands[0]))
13874         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
13875       else
13876         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
13877       break;
13878
13879     case MINUS:
13880     case DIV:
13881       if (MEM_P (operands[1]))
13882         {
13883           p = "r%Z1\t%1";
13884           break;
13885         }
13886
13887       if (MEM_P (operands[2]))
13888         {
13889           p = "%Z2\t%2";
13890           break;
13891         }
13892
13893       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
13894         {
13895 #if SYSV386_COMPAT
13896           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
13897              derived assemblers, confusingly reverse the direction of
13898              the operation for fsub{r} and fdiv{r} when the
13899              destination register is not st(0).  The Intel assembler
13900              doesn't have this brain damage.  Read !SYSV386_COMPAT to
13901              figure out what the hardware really does.  */
13902           if (STACK_TOP_P (operands[0]))
13903             p = "{p\t%0, %2|rp\t%2, %0}";
13904           else
13905             p = "{rp\t%2, %0|p\t%0, %2}";
13906 #else
13907           if (STACK_TOP_P (operands[0]))
13908             /* As above for fmul/fadd, we can't store to st(0).  */
13909             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
13910           else
13911             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
13912 #endif
13913           break;
13914         }
13915
13916       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
13917         {
13918 #if SYSV386_COMPAT
13919           if (STACK_TOP_P (operands[0]))
13920             p = "{rp\t%0, %1|p\t%1, %0}";
13921           else
13922             p = "{p\t%1, %0|rp\t%0, %1}";
13923 #else
13924           if (STACK_TOP_P (operands[0]))
13925             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
13926           else
13927             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
13928 #endif
13929           break;
13930         }
13931
13932       if (STACK_TOP_P (operands[0]))
13933         {
13934           if (STACK_TOP_P (operands[1]))
13935             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
13936           else
13937             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
13938           break;
13939         }
13940       else if (STACK_TOP_P (operands[1]))
13941         {
13942 #if SYSV386_COMPAT
13943           p = "{\t%1, %0|r\t%0, %1}";
13944 #else
13945           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
13946 #endif
13947         }
13948       else
13949         {
13950 #if SYSV386_COMPAT
13951           p = "{r\t%2, %0|\t%0, %2}";
13952 #else
13953           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
13954 #endif
13955         }
13956       break;
13957
13958     default:
13959       gcc_unreachable ();
13960     }
13961
13962   strcat (buf, p);
13963   return buf;
13964 }
13965
13966 /* Return needed mode for entity in optimize_mode_switching pass.  */
13967
13968 int
13969 ix86_mode_needed (int entity, rtx insn)
13970 {
13971   enum attr_i387_cw mode;
13972
13973   /* The mode UNINITIALIZED is used to store control word after a
13974      function call or ASM pattern.  The mode ANY specify that function
13975      has no requirements on the control word and make no changes in the
13976      bits we are interested in.  */
13977
13978   if (CALL_P (insn)
13979       || (NONJUMP_INSN_P (insn)
13980           && (asm_noperands (PATTERN (insn)) >= 0
13981               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
13982     return I387_CW_UNINITIALIZED;
13983
13984   if (recog_memoized (insn) < 0)
13985     return I387_CW_ANY;
13986
13987   mode = get_attr_i387_cw (insn);
13988
13989   switch (entity)
13990     {
13991     case I387_TRUNC:
13992       if (mode == I387_CW_TRUNC)
13993         return mode;
13994       break;
13995
13996     case I387_FLOOR:
13997       if (mode == I387_CW_FLOOR)
13998         return mode;
13999       break;
14000
14001     case I387_CEIL:
14002       if (mode == I387_CW_CEIL)
14003         return mode;
14004       break;
14005
14006     case I387_MASK_PM:
14007       if (mode == I387_CW_MASK_PM)
14008         return mode;
14009       break;
14010
14011     default:
14012       gcc_unreachable ();
14013     }
14014
14015   return I387_CW_ANY;
14016 }
14017
14018 /* Output code to initialize control word copies used by trunc?f?i and
14019    rounding patterns.  CURRENT_MODE is set to current control word,
14020    while NEW_MODE is set to new control word.  */
14021
14022 void
14023 emit_i387_cw_initialization (int mode)
14024 {
14025   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
14026   rtx new_mode;
14027
14028   enum ix86_stack_slot slot;
14029
14030   rtx reg = gen_reg_rtx (HImode);
14031
14032   emit_insn (gen_x86_fnstcw_1 (stored_mode));
14033   emit_move_insn (reg, copy_rtx (stored_mode));
14034
14035   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL
14036       || optimize_function_for_size_p (cfun))
14037     {
14038       switch (mode)
14039         {
14040         case I387_CW_TRUNC:
14041           /* round toward zero (truncate) */
14042           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
14043           slot = SLOT_CW_TRUNC;
14044           break;
14045
14046         case I387_CW_FLOOR:
14047           /* round down toward -oo */
14048           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14049           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
14050           slot = SLOT_CW_FLOOR;
14051           break;
14052
14053         case I387_CW_CEIL:
14054           /* round up toward +oo */
14055           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
14056           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
14057           slot = SLOT_CW_CEIL;
14058           break;
14059
14060         case I387_CW_MASK_PM:
14061           /* mask precision exception for nearbyint() */
14062           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14063           slot = SLOT_CW_MASK_PM;
14064           break;
14065
14066         default:
14067           gcc_unreachable ();
14068         }
14069     }
14070   else
14071     {
14072       switch (mode)
14073         {
14074         case I387_CW_TRUNC:
14075           /* round toward zero (truncate) */
14076           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
14077           slot = SLOT_CW_TRUNC;
14078           break;
14079
14080         case I387_CW_FLOOR:
14081           /* round down toward -oo */
14082           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
14083           slot = SLOT_CW_FLOOR;
14084           break;
14085
14086         case I387_CW_CEIL:
14087           /* round up toward +oo */
14088           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
14089           slot = SLOT_CW_CEIL;
14090           break;
14091
14092         case I387_CW_MASK_PM:
14093           /* mask precision exception for nearbyint() */
14094           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
14095           slot = SLOT_CW_MASK_PM;
14096           break;
14097
14098         default:
14099           gcc_unreachable ();
14100         }
14101     }
14102
14103   gcc_assert (slot < MAX_386_STACK_LOCALS);
14104
14105   new_mode = assign_386_stack_local (HImode, slot);
14106   emit_move_insn (new_mode, reg);
14107 }
14108
14109 /* Output code for INSN to convert a float to a signed int.  OPERANDS
14110    are the insn operands.  The output may be [HSD]Imode and the input
14111    operand may be [SDX]Fmode.  */
14112
14113 const char *
14114 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
14115 {
14116   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
14117   int dimode_p = GET_MODE (operands[0]) == DImode;
14118   int round_mode = get_attr_i387_cw (insn);
14119
14120   /* Jump through a hoop or two for DImode, since the hardware has no
14121      non-popping instruction.  We used to do this a different way, but
14122      that was somewhat fragile and broke with post-reload splitters.  */
14123   if ((dimode_p || fisttp) && !stack_top_dies)
14124     output_asm_insn ("fld\t%y1", operands);
14125
14126   gcc_assert (STACK_TOP_P (operands[1]));
14127   gcc_assert (MEM_P (operands[0]));
14128   gcc_assert (GET_MODE (operands[1]) != TFmode);
14129
14130   if (fisttp)
14131       output_asm_insn ("fisttp%Z0\t%0", operands);
14132   else
14133     {
14134       if (round_mode != I387_CW_ANY)
14135         output_asm_insn ("fldcw\t%3", operands);
14136       if (stack_top_dies || dimode_p)
14137         output_asm_insn ("fistp%Z0\t%0", operands);
14138       else
14139         output_asm_insn ("fist%Z0\t%0", operands);
14140       if (round_mode != I387_CW_ANY)
14141         output_asm_insn ("fldcw\t%2", operands);
14142     }
14143
14144   return "";
14145 }
14146
14147 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
14148    have the values zero or one, indicates the ffreep insn's operand
14149    from the OPERANDS array.  */
14150
14151 static const char *
14152 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
14153 {
14154   if (TARGET_USE_FFREEP)
14155 #ifdef HAVE_AS_IX86_FFREEP
14156     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
14157 #else
14158     {
14159       static char retval[32];
14160       int regno = REGNO (operands[opno]);
14161
14162       gcc_assert (FP_REGNO_P (regno));
14163
14164       regno -= FIRST_STACK_REG;
14165
14166       snprintf (retval, sizeof (retval), ASM_SHORT "0xc%ddf", regno);
14167       return retval;
14168     }
14169 #endif
14170
14171   return opno ? "fstp\t%y1" : "fstp\t%y0";
14172 }
14173
14174
14175 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
14176    should be used.  UNORDERED_P is true when fucom should be used.  */
14177
14178 const char *
14179 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
14180 {
14181   int stack_top_dies;
14182   rtx cmp_op0, cmp_op1;
14183   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
14184
14185   if (eflags_p)
14186     {
14187       cmp_op0 = operands[0];
14188       cmp_op1 = operands[1];
14189     }
14190   else
14191     {
14192       cmp_op0 = operands[1];
14193       cmp_op1 = operands[2];
14194     }
14195
14196   if (is_sse)
14197     {
14198       static const char ucomiss[] = "vucomiss\t{%1, %0|%0, %1}";
14199       static const char ucomisd[] = "vucomisd\t{%1, %0|%0, %1}";
14200       static const char comiss[] = "vcomiss\t{%1, %0|%0, %1}";
14201       static const char comisd[] = "vcomisd\t{%1, %0|%0, %1}";
14202
14203       if (GET_MODE (operands[0]) == SFmode)
14204         if (unordered_p)
14205           return &ucomiss[TARGET_AVX ? 0 : 1];
14206         else
14207           return &comiss[TARGET_AVX ? 0 : 1];
14208       else
14209         if (unordered_p)
14210           return &ucomisd[TARGET_AVX ? 0 : 1];
14211         else
14212           return &comisd[TARGET_AVX ? 0 : 1];
14213     }
14214
14215   gcc_assert (STACK_TOP_P (cmp_op0));
14216
14217   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
14218
14219   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
14220     {
14221       if (stack_top_dies)
14222         {
14223           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
14224           return output_387_ffreep (operands, 1);
14225         }
14226       else
14227         return "ftst\n\tfnstsw\t%0";
14228     }
14229
14230   if (STACK_REG_P (cmp_op1)
14231       && stack_top_dies
14232       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
14233       && REGNO (cmp_op1) != FIRST_STACK_REG)
14234     {
14235       /* If both the top of the 387 stack dies, and the other operand
14236          is also a stack register that dies, then this must be a
14237          `fcompp' float compare */
14238
14239       if (eflags_p)
14240         {
14241           /* There is no double popping fcomi variant.  Fortunately,
14242              eflags is immune from the fstp's cc clobbering.  */
14243           if (unordered_p)
14244             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
14245           else
14246             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
14247           return output_387_ffreep (operands, 0);
14248         }
14249       else
14250         {
14251           if (unordered_p)
14252             return "fucompp\n\tfnstsw\t%0";
14253           else
14254             return "fcompp\n\tfnstsw\t%0";
14255         }
14256     }
14257   else
14258     {
14259       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
14260
14261       static const char * const alt[16] =
14262       {
14263         "fcom%Z2\t%y2\n\tfnstsw\t%0",
14264         "fcomp%Z2\t%y2\n\tfnstsw\t%0",
14265         "fucom%Z2\t%y2\n\tfnstsw\t%0",
14266         "fucomp%Z2\t%y2\n\tfnstsw\t%0",
14267
14268         "ficom%Z2\t%y2\n\tfnstsw\t%0",
14269         "ficomp%Z2\t%y2\n\tfnstsw\t%0",
14270         NULL,
14271         NULL,
14272
14273         "fcomi\t{%y1, %0|%0, %y1}",
14274         "fcomip\t{%y1, %0|%0, %y1}",
14275         "fucomi\t{%y1, %0|%0, %y1}",
14276         "fucomip\t{%y1, %0|%0, %y1}",
14277
14278         NULL,
14279         NULL,
14280         NULL,
14281         NULL
14282       };
14283
14284       int mask;
14285       const char *ret;
14286
14287       mask  = eflags_p << 3;
14288       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
14289       mask |= unordered_p << 1;
14290       mask |= stack_top_dies;
14291
14292       gcc_assert (mask < 16);
14293       ret = alt[mask];
14294       gcc_assert (ret);
14295
14296       return ret;
14297     }
14298 }
14299
14300 void
14301 ix86_output_addr_vec_elt (FILE *file, int value)
14302 {
14303   const char *directive = ASM_LONG;
14304
14305 #ifdef ASM_QUAD
14306   if (TARGET_64BIT)
14307     directive = ASM_QUAD;
14308 #else
14309   gcc_assert (!TARGET_64BIT);
14310 #endif
14311
14312   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
14313 }
14314
14315 void
14316 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
14317 {
14318   const char *directive = ASM_LONG;
14319
14320 #ifdef ASM_QUAD
14321   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
14322     directive = ASM_QUAD;
14323 #else
14324   gcc_assert (!TARGET_64BIT);
14325 #endif
14326   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
14327   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
14328     fprintf (file, "%s%s%d-%s%d\n",
14329              directive, LPREFIX, value, LPREFIX, rel);
14330   else if (HAVE_AS_GOTOFF_IN_DATA)
14331     fprintf (file, ASM_LONG "%s%d@GOTOFF\n", LPREFIX, value);
14332 #if TARGET_MACHO
14333   else if (TARGET_MACHO)
14334     {
14335       fprintf (file, ASM_LONG "%s%d-", LPREFIX, value);
14336       machopic_output_function_base_name (file);
14337       putc ('\n', file);
14338     }
14339 #endif
14340   else
14341     asm_fprintf (file, ASM_LONG "%U%s+[.-%s%d]\n",
14342                  GOT_SYMBOL_NAME, LPREFIX, value);
14343 }
14344 \f
14345 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
14346    for the target.  */
14347
14348 void
14349 ix86_expand_clear (rtx dest)
14350 {
14351   rtx tmp;
14352
14353   /* We play register width games, which are only valid after reload.  */
14354   gcc_assert (reload_completed);
14355
14356   /* Avoid HImode and its attendant prefix byte.  */
14357   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
14358     dest = gen_rtx_REG (SImode, REGNO (dest));
14359   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
14360
14361   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
14362   if (!TARGET_USE_MOV0 || optimize_insn_for_speed_p ())
14363     {
14364       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
14365       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
14366     }
14367
14368   emit_insn (tmp);
14369 }
14370
14371 /* X is an unchanging MEM.  If it is a constant pool reference, return
14372    the constant pool rtx, else NULL.  */
14373
14374 rtx
14375 maybe_get_pool_constant (rtx x)
14376 {
14377   x = ix86_delegitimize_address (XEXP (x, 0));
14378
14379   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
14380     return get_pool_constant (x);
14381
14382   return NULL_RTX;
14383 }
14384
14385 void
14386 ix86_expand_move (enum machine_mode mode, rtx operands[])
14387 {
14388   rtx op0, op1;
14389   enum tls_model model;
14390
14391   op0 = operands[0];
14392   op1 = operands[1];
14393
14394   if (GET_CODE (op1) == SYMBOL_REF)
14395     {
14396       model = SYMBOL_REF_TLS_MODEL (op1);
14397       if (model)
14398         {
14399           op1 = legitimize_tls_address (op1, model, true);
14400           op1 = force_operand (op1, op0);
14401           if (op1 == op0)
14402             return;
14403         }
14404       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
14405                && SYMBOL_REF_DLLIMPORT_P (op1))
14406         op1 = legitimize_dllimport_symbol (op1, false);
14407     }
14408   else if (GET_CODE (op1) == CONST
14409            && GET_CODE (XEXP (op1, 0)) == PLUS
14410            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
14411     {
14412       rtx addend = XEXP (XEXP (op1, 0), 1);
14413       rtx symbol = XEXP (XEXP (op1, 0), 0);
14414       rtx tmp = NULL;
14415
14416       model = SYMBOL_REF_TLS_MODEL (symbol);
14417       if (model)
14418         tmp = legitimize_tls_address (symbol, model, true);
14419       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
14420                && SYMBOL_REF_DLLIMPORT_P (symbol))
14421         tmp = legitimize_dllimport_symbol (symbol, true);
14422
14423       if (tmp)
14424         {
14425           tmp = force_operand (tmp, NULL);
14426           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
14427                                      op0, 1, OPTAB_DIRECT);
14428           if (tmp == op0)
14429             return;
14430         }
14431     }
14432
14433   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
14434     {
14435       if (TARGET_MACHO && !TARGET_64BIT)
14436         {
14437 #if TARGET_MACHO
14438           if (MACHOPIC_PURE)
14439             {
14440               rtx temp = ((reload_in_progress
14441                            || ((op0 && REG_P (op0))
14442                                && mode == Pmode))
14443                           ? op0 : gen_reg_rtx (Pmode));
14444               op1 = machopic_indirect_data_reference (op1, temp);
14445               op1 = machopic_legitimize_pic_address (op1, mode,
14446                                                      temp == op1 ? 0 : temp);
14447             }
14448           else if (MACHOPIC_INDIRECT)
14449             op1 = machopic_indirect_data_reference (op1, 0);
14450           if (op0 == op1)
14451             return;
14452 #endif
14453         }
14454       else
14455         {
14456           if (MEM_P (op0))
14457             op1 = force_reg (Pmode, op1);
14458           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
14459             {
14460               rtx reg = can_create_pseudo_p () ? NULL_RTX : op0;
14461               op1 = legitimize_pic_address (op1, reg);
14462               if (op0 == op1)
14463                 return;
14464             }
14465         }
14466     }
14467   else
14468     {
14469       if (MEM_P (op0)
14470           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
14471               || !push_operand (op0, mode))
14472           && MEM_P (op1))
14473         op1 = force_reg (mode, op1);
14474
14475       if (push_operand (op0, mode)
14476           && ! general_no_elim_operand (op1, mode))
14477         op1 = copy_to_mode_reg (mode, op1);
14478
14479       /* Force large constants in 64bit compilation into register
14480          to get them CSEed.  */
14481       if (can_create_pseudo_p ()
14482           && (mode == DImode) && TARGET_64BIT
14483           && immediate_operand (op1, mode)
14484           && !x86_64_zext_immediate_operand (op1, VOIDmode)
14485           && !register_operand (op0, mode)
14486           && optimize)
14487         op1 = copy_to_mode_reg (mode, op1);
14488
14489       if (can_create_pseudo_p ()
14490           && FLOAT_MODE_P (mode)
14491           && GET_CODE (op1) == CONST_DOUBLE)
14492         {
14493           /* If we are loading a floating point constant to a register,
14494              force the value to memory now, since we'll get better code
14495              out the back end.  */
14496
14497           op1 = validize_mem (force_const_mem (mode, op1));
14498           if (!register_operand (op0, mode))
14499             {
14500               rtx temp = gen_reg_rtx (mode);
14501               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
14502               emit_move_insn (op0, temp);
14503               return;
14504             }
14505         }
14506     }
14507
14508   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
14509 }
14510
14511 void
14512 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
14513 {
14514   rtx op0 = operands[0], op1 = operands[1];
14515   unsigned int align = GET_MODE_ALIGNMENT (mode);
14516
14517   /* Force constants other than zero into memory.  We do not know how
14518      the instructions used to build constants modify the upper 64 bits
14519      of the register, once we have that information we may be able
14520      to handle some of them more efficiently.  */
14521   if (can_create_pseudo_p ()
14522       && register_operand (op0, mode)
14523       && (CONSTANT_P (op1)
14524           || (GET_CODE (op1) == SUBREG
14525               && CONSTANT_P (SUBREG_REG (op1))))
14526       && !standard_sse_constant_p (op1))
14527     op1 = validize_mem (force_const_mem (mode, op1));
14528
14529   /* We need to check memory alignment for SSE mode since attribute
14530      can make operands unaligned.  */
14531   if (can_create_pseudo_p ()
14532       && SSE_REG_MODE_P (mode)
14533       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
14534           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
14535     {
14536       rtx tmp[2];
14537
14538       /* ix86_expand_vector_move_misalign() does not like constants ... */
14539       if (CONSTANT_P (op1)
14540           || (GET_CODE (op1) == SUBREG
14541               && CONSTANT_P (SUBREG_REG (op1))))
14542         op1 = validize_mem (force_const_mem (mode, op1));
14543
14544       /* ... nor both arguments in memory.  */
14545       if (!register_operand (op0, mode)
14546           && !register_operand (op1, mode))
14547         op1 = force_reg (mode, op1);
14548
14549       tmp[0] = op0; tmp[1] = op1;
14550       ix86_expand_vector_move_misalign (mode, tmp);
14551       return;
14552     }
14553
14554   /* Make operand1 a register if it isn't already.  */
14555   if (can_create_pseudo_p ()
14556       && !register_operand (op0, mode)
14557       && !register_operand (op1, mode))
14558     {
14559       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
14560       return;
14561     }
14562
14563   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
14564 }
14565
14566 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
14567    straight to ix86_expand_vector_move.  */
14568 /* Code generation for scalar reg-reg moves of single and double precision data:
14569      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
14570        movaps reg, reg
14571      else
14572        movss reg, reg
14573      if (x86_sse_partial_reg_dependency == true)
14574        movapd reg, reg
14575      else
14576        movsd reg, reg
14577
14578    Code generation for scalar loads of double precision data:
14579      if (x86_sse_split_regs == true)
14580        movlpd mem, reg      (gas syntax)
14581      else
14582        movsd mem, reg
14583
14584    Code generation for unaligned packed loads of single precision data
14585    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
14586      if (x86_sse_unaligned_move_optimal)
14587        movups mem, reg
14588
14589      if (x86_sse_partial_reg_dependency == true)
14590        {
14591          xorps  reg, reg
14592          movlps mem, reg
14593          movhps mem+8, reg
14594        }
14595      else
14596        {
14597          movlps mem, reg
14598          movhps mem+8, reg
14599        }
14600
14601    Code generation for unaligned packed loads of double precision data
14602    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
14603      if (x86_sse_unaligned_move_optimal)
14604        movupd mem, reg
14605
14606      if (x86_sse_split_regs == true)
14607        {
14608          movlpd mem, reg
14609          movhpd mem+8, reg
14610        }
14611      else
14612        {
14613          movsd  mem, reg
14614          movhpd mem+8, reg
14615        }
14616  */
14617
14618 void
14619 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
14620 {
14621   rtx op0, op1, m;
14622
14623   op0 = operands[0];
14624   op1 = operands[1];
14625
14626   if (TARGET_AVX)
14627     {
14628       switch (GET_MODE_CLASS (mode))
14629         {
14630         case MODE_VECTOR_INT:
14631         case MODE_INT:
14632           switch (GET_MODE_SIZE (mode))
14633             {
14634             case 16:
14635               /*  If we're optimizing for size, movups is the smallest.  */
14636               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14637                 {
14638                   op0 = gen_lowpart (V4SFmode, op0);
14639                   op1 = gen_lowpart (V4SFmode, op1);
14640                   emit_insn (gen_avx_movups (op0, op1));
14641                   return;
14642                 }
14643               op0 = gen_lowpart (V16QImode, op0);
14644               op1 = gen_lowpart (V16QImode, op1);
14645               emit_insn (gen_avx_movdqu (op0, op1));
14646               break;
14647             case 32:
14648               op0 = gen_lowpart (V32QImode, op0);
14649               op1 = gen_lowpart (V32QImode, op1);
14650               emit_insn (gen_avx_movdqu256 (op0, op1));
14651               break;
14652             default:
14653               gcc_unreachable ();
14654             }
14655           break;
14656         case MODE_VECTOR_FLOAT:
14657           op0 = gen_lowpart (mode, op0);
14658           op1 = gen_lowpart (mode, op1);
14659
14660           switch (mode)
14661             {
14662             case V4SFmode:
14663               emit_insn (gen_avx_movups (op0, op1));
14664               break;
14665             case V8SFmode:
14666               emit_insn (gen_avx_movups256 (op0, op1));
14667               break;
14668             case V2DFmode:
14669               if (TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14670                 {
14671                   op0 = gen_lowpart (V4SFmode, op0);
14672                   op1 = gen_lowpart (V4SFmode, op1);
14673                   emit_insn (gen_avx_movups (op0, op1));
14674                   return;
14675                 }
14676               emit_insn (gen_avx_movupd (op0, op1));
14677               break;
14678             case V4DFmode:
14679               emit_insn (gen_avx_movupd256 (op0, op1));
14680               break;
14681             default:
14682               gcc_unreachable ();
14683             }
14684           break;
14685
14686         default:
14687           gcc_unreachable ();
14688         }
14689
14690       return;
14691     }
14692
14693   if (MEM_P (op1))
14694     {
14695       /* If we're optimizing for size, movups is the smallest.  */
14696       if (optimize_insn_for_size_p ()
14697           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14698         {
14699           op0 = gen_lowpart (V4SFmode, op0);
14700           op1 = gen_lowpart (V4SFmode, op1);
14701           emit_insn (gen_sse_movups (op0, op1));
14702           return;
14703         }
14704
14705       /* ??? If we have typed data, then it would appear that using
14706          movdqu is the only way to get unaligned data loaded with
14707          integer type.  */
14708       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14709         {
14710           op0 = gen_lowpart (V16QImode, op0);
14711           op1 = gen_lowpart (V16QImode, op1);
14712           emit_insn (gen_sse2_movdqu (op0, op1));
14713           return;
14714         }
14715
14716       if (TARGET_SSE2 && mode == V2DFmode)
14717         {
14718           rtx zero;
14719
14720           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
14721             {
14722               op0 = gen_lowpart (V2DFmode, op0);
14723               op1 = gen_lowpart (V2DFmode, op1);
14724               emit_insn (gen_sse2_movupd (op0, op1));
14725               return;
14726             }
14727
14728           /* When SSE registers are split into halves, we can avoid
14729              writing to the top half twice.  */
14730           if (TARGET_SSE_SPLIT_REGS)
14731             {
14732               emit_clobber (op0);
14733               zero = op0;
14734             }
14735           else
14736             {
14737               /* ??? Not sure about the best option for the Intel chips.
14738                  The following would seem to satisfy; the register is
14739                  entirely cleared, breaking the dependency chain.  We
14740                  then store to the upper half, with a dependency depth
14741                  of one.  A rumor has it that Intel recommends two movsd
14742                  followed by an unpacklpd, but this is unconfirmed.  And
14743                  given that the dependency depth of the unpacklpd would
14744                  still be one, I'm not sure why this would be better.  */
14745               zero = CONST0_RTX (V2DFmode);
14746             }
14747
14748           m = adjust_address (op1, DFmode, 0);
14749           emit_insn (gen_sse2_loadlpd (op0, zero, m));
14750           m = adjust_address (op1, DFmode, 8);
14751           emit_insn (gen_sse2_loadhpd (op0, op0, m));
14752         }
14753       else
14754         {
14755           if (TARGET_SSE_UNALIGNED_LOAD_OPTIMAL)
14756             {
14757               op0 = gen_lowpart (V4SFmode, op0);
14758               op1 = gen_lowpart (V4SFmode, op1);
14759               emit_insn (gen_sse_movups (op0, op1));
14760               return;
14761             }
14762
14763           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
14764             emit_move_insn (op0, CONST0_RTX (mode));
14765           else
14766             emit_clobber (op0);
14767
14768           if (mode != V4SFmode)
14769             op0 = gen_lowpart (V4SFmode, op0);
14770           m = adjust_address (op1, V2SFmode, 0);
14771           emit_insn (gen_sse_loadlps (op0, op0, m));
14772           m = adjust_address (op1, V2SFmode, 8);
14773           emit_insn (gen_sse_loadhps (op0, op0, m));
14774         }
14775     }
14776   else if (MEM_P (op0))
14777     {
14778       /* If we're optimizing for size, movups is the smallest.  */
14779       if (optimize_insn_for_size_p ()
14780           || TARGET_SSE_PACKED_SINGLE_INSN_OPTIMAL)
14781         {
14782           op0 = gen_lowpart (V4SFmode, op0);
14783           op1 = gen_lowpart (V4SFmode, op1);
14784           emit_insn (gen_sse_movups (op0, op1));
14785           return;
14786         }
14787
14788       /* ??? Similar to above, only less clear because of quote
14789          typeless stores unquote.  */
14790       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
14791           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
14792         {
14793           op0 = gen_lowpart (V16QImode, op0);
14794           op1 = gen_lowpart (V16QImode, op1);
14795           emit_insn (gen_sse2_movdqu (op0, op1));
14796           return;
14797         }
14798
14799       if (TARGET_SSE2 && mode == V2DFmode)
14800         {
14801           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
14802             {
14803               op0 = gen_lowpart (V2DFmode, op0);
14804               op1 = gen_lowpart (V2DFmode, op1);
14805               emit_insn (gen_sse2_movupd (op0, op1));
14806             }
14807           else
14808             {
14809               m = adjust_address (op0, DFmode, 0);
14810               emit_insn (gen_sse2_storelpd (m, op1));
14811               m = adjust_address (op0, DFmode, 8);
14812               emit_insn (gen_sse2_storehpd (m, op1));
14813             }
14814         }
14815       else
14816         {
14817           if (mode != V4SFmode)
14818             op1 = gen_lowpart (V4SFmode, op1);
14819
14820           if (TARGET_SSE_UNALIGNED_STORE_OPTIMAL)
14821             {
14822               op0 = gen_lowpart (V4SFmode, op0);
14823               emit_insn (gen_sse_movups (op0, op1));
14824             }
14825           else
14826             {
14827               m = adjust_address (op0, V2SFmode, 0);
14828               emit_insn (gen_sse_storelps (m, op1));
14829               m = adjust_address (op0, V2SFmode, 8);
14830               emit_insn (gen_sse_storehps (m, op1));
14831             }
14832         }
14833     }
14834   else
14835     gcc_unreachable ();
14836 }
14837
14838 /* Expand a push in MODE.  This is some mode for which we do not support
14839    proper push instructions, at least from the registers that we expect
14840    the value to live in.  */
14841
14842 void
14843 ix86_expand_push (enum machine_mode mode, rtx x)
14844 {
14845   rtx tmp;
14846
14847   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
14848                              GEN_INT (-GET_MODE_SIZE (mode)),
14849                              stack_pointer_rtx, 1, OPTAB_DIRECT);
14850   if (tmp != stack_pointer_rtx)
14851     emit_move_insn (stack_pointer_rtx, tmp);
14852
14853   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
14854
14855   /* When we push an operand onto stack, it has to be aligned at least
14856      at the function argument boundary.  However since we don't have
14857      the argument type, we can't determine the actual argument
14858      boundary.  */
14859   emit_move_insn (tmp, x);
14860 }
14861
14862 /* Helper function of ix86_fixup_binary_operands to canonicalize
14863    operand order.  Returns true if the operands should be swapped.  */
14864
14865 static bool
14866 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
14867                              rtx operands[])
14868 {
14869   rtx dst = operands[0];
14870   rtx src1 = operands[1];
14871   rtx src2 = operands[2];
14872
14873   /* If the operation is not commutative, we can't do anything.  */
14874   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
14875     return false;
14876
14877   /* Highest priority is that src1 should match dst.  */
14878   if (rtx_equal_p (dst, src1))
14879     return false;
14880   if (rtx_equal_p (dst, src2))
14881     return true;
14882
14883   /* Next highest priority is that immediate constants come second.  */
14884   if (immediate_operand (src2, mode))
14885     return false;
14886   if (immediate_operand (src1, mode))
14887     return true;
14888
14889   /* Lowest priority is that memory references should come second.  */
14890   if (MEM_P (src2))
14891     return false;
14892   if (MEM_P (src1))
14893     return true;
14894
14895   return false;
14896 }
14897
14898
14899 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
14900    destination to use for the operation.  If different from the true
14901    destination in operands[0], a copy operation will be required.  */
14902
14903 rtx
14904 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
14905                             rtx operands[])
14906 {
14907   rtx dst = operands[0];
14908   rtx src1 = operands[1];
14909   rtx src2 = operands[2];
14910
14911   /* Canonicalize operand order.  */
14912   if (ix86_swap_binary_operands_p (code, mode, operands))
14913     {
14914       rtx temp;
14915
14916       /* It is invalid to swap operands of different modes.  */
14917       gcc_assert (GET_MODE (src1) == GET_MODE (src2));
14918
14919       temp = src1;
14920       src1 = src2;
14921       src2 = temp;
14922     }
14923
14924   /* Both source operands cannot be in memory.  */
14925   if (MEM_P (src1) && MEM_P (src2))
14926     {
14927       /* Optimization: Only read from memory once.  */
14928       if (rtx_equal_p (src1, src2))
14929         {
14930           src2 = force_reg (mode, src2);
14931           src1 = src2;
14932         }
14933       else
14934         src2 = force_reg (mode, src2);
14935     }
14936
14937   /* If the destination is memory, and we do not have matching source
14938      operands, do things in registers.  */
14939   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
14940     dst = gen_reg_rtx (mode);
14941
14942   /* Source 1 cannot be a constant.  */
14943   if (CONSTANT_P (src1))
14944     src1 = force_reg (mode, src1);
14945
14946   /* Source 1 cannot be a non-matching memory.  */
14947   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
14948     src1 = force_reg (mode, src1);
14949
14950   operands[1] = src1;
14951   operands[2] = src2;
14952   return dst;
14953 }
14954
14955 /* Similarly, but assume that the destination has already been
14956    set up properly.  */
14957
14958 void
14959 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
14960                                     enum machine_mode mode, rtx operands[])
14961 {
14962   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
14963   gcc_assert (dst == operands[0]);
14964 }
14965
14966 /* Attempt to expand a binary operator.  Make the expansion closer to the
14967    actual machine, then just general_operand, which will allow 3 separate
14968    memory references (one output, two input) in a single insn.  */
14969
14970 void
14971 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
14972                              rtx operands[])
14973 {
14974   rtx src1, src2, dst, op, clob;
14975
14976   dst = ix86_fixup_binary_operands (code, mode, operands);
14977   src1 = operands[1];
14978   src2 = operands[2];
14979
14980  /* Emit the instruction.  */
14981
14982   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
14983   if (reload_in_progress)
14984     {
14985       /* Reload doesn't know about the flags register, and doesn't know that
14986          it doesn't want to clobber it.  We can only do this with PLUS.  */
14987       gcc_assert (code == PLUS);
14988       emit_insn (op);
14989     }
14990   else
14991     {
14992       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
14993       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
14994     }
14995
14996   /* Fix up the destination if needed.  */
14997   if (dst != operands[0])
14998     emit_move_insn (operands[0], dst);
14999 }
15000
15001 /* Return TRUE or FALSE depending on whether the binary operator meets the
15002    appropriate constraints.  */
15003
15004 bool
15005 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
15006                          rtx operands[3])
15007 {
15008   rtx dst = operands[0];
15009   rtx src1 = operands[1];
15010   rtx src2 = operands[2];
15011
15012   /* Both source operands cannot be in memory.  */
15013   if (MEM_P (src1) && MEM_P (src2))
15014     return false;
15015
15016   /* Canonicalize operand order for commutative operators.  */
15017   if (ix86_swap_binary_operands_p (code, mode, operands))
15018     {
15019       rtx temp = src1;
15020       src1 = src2;
15021       src2 = temp;
15022     }
15023
15024   /* If the destination is memory, we must have a matching source operand.  */
15025   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
15026       return false;
15027
15028   /* Source 1 cannot be a constant.  */
15029   if (CONSTANT_P (src1))
15030     return false;
15031
15032   /* Source 1 cannot be a non-matching memory.  */
15033   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
15034     {
15035       /* Support "andhi/andsi/anddi" as a zero-extending move.  */
15036       return (code == AND
15037               && (mode == HImode
15038                   || mode == SImode
15039                   || (TARGET_64BIT && mode == DImode))
15040               && CONST_INT_P (src2)
15041               && (INTVAL (src2) == 0xff
15042                   || INTVAL (src2) == 0xffff));
15043     }
15044
15045   return true;
15046 }
15047
15048 /* Attempt to expand a unary operator.  Make the expansion closer to the
15049    actual machine, then just general_operand, which will allow 2 separate
15050    memory references (one output, one input) in a single insn.  */
15051
15052 void
15053 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
15054                             rtx operands[])
15055 {
15056   int matching_memory;
15057   rtx src, dst, op, clob;
15058
15059   dst = operands[0];
15060   src = operands[1];
15061
15062   /* If the destination is memory, and we do not have matching source
15063      operands, do things in registers.  */
15064   matching_memory = 0;
15065   if (MEM_P (dst))
15066     {
15067       if (rtx_equal_p (dst, src))
15068         matching_memory = 1;
15069       else
15070         dst = gen_reg_rtx (mode);
15071     }
15072
15073   /* When source operand is memory, destination must match.  */
15074   if (MEM_P (src) && !matching_memory)
15075     src = force_reg (mode, src);
15076
15077   /* Emit the instruction.  */
15078
15079   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
15080   if (reload_in_progress || code == NOT)
15081     {
15082       /* Reload doesn't know about the flags register, and doesn't know that
15083          it doesn't want to clobber it.  */
15084       gcc_assert (code == NOT);
15085       emit_insn (op);
15086     }
15087   else
15088     {
15089       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15090       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
15091     }
15092
15093   /* Fix up the destination if needed.  */
15094   if (dst != operands[0])
15095     emit_move_insn (operands[0], dst);
15096 }
15097
15098 /* Split 32bit/64bit divmod with 8bit unsigned divmod if dividend and
15099    divisor are within the the range [0-255].  */
15100
15101 void
15102 ix86_split_idivmod (enum machine_mode mode, rtx operands[],
15103                     bool signed_p)
15104 {
15105   rtx end_label, qimode_label;
15106   rtx insn, div, mod;
15107   rtx scratch, tmp0, tmp1, tmp2;
15108   rtx (*gen_divmod4_1) (rtx, rtx, rtx, rtx);
15109   rtx (*gen_zero_extend) (rtx, rtx);
15110   rtx (*gen_test_ccno_1) (rtx, rtx);
15111
15112   switch (mode)
15113     {
15114     case SImode:
15115       gen_divmod4_1 = signed_p ? gen_divmodsi4_1 : gen_udivmodsi4_1;
15116       gen_test_ccno_1 = gen_testsi_ccno_1;
15117       gen_zero_extend = gen_zero_extendqisi2;
15118       break;
15119     case DImode:
15120       gen_divmod4_1 = signed_p ? gen_divmoddi4_1 : gen_udivmoddi4_1;
15121       gen_test_ccno_1 = gen_testdi_ccno_1;
15122       gen_zero_extend = gen_zero_extendqidi2;
15123       break;
15124     default:
15125       gcc_unreachable ();
15126     }
15127
15128   end_label = gen_label_rtx ();
15129   qimode_label = gen_label_rtx ();
15130
15131   scratch = gen_reg_rtx (mode);
15132
15133   /* Use 8bit unsigned divimod if dividend and divisor are within the
15134      the range [0-255].  */
15135   emit_move_insn (scratch, operands[2]);
15136   scratch = expand_simple_binop (mode, IOR, scratch, operands[3],
15137                                  scratch, 1, OPTAB_DIRECT);
15138   emit_insn (gen_test_ccno_1 (scratch, GEN_INT (-0x100)));
15139   tmp0 = gen_rtx_REG (CCNOmode, FLAGS_REG);
15140   tmp0 = gen_rtx_EQ (VOIDmode, tmp0, const0_rtx);
15141   tmp0 = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp0,
15142                                gen_rtx_LABEL_REF (VOIDmode, qimode_label),
15143                                pc_rtx);
15144   insn = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp0));
15145   predict_jump (REG_BR_PROB_BASE * 50 / 100);
15146   JUMP_LABEL (insn) = qimode_label;
15147
15148   /* Generate original signed/unsigned divimod.  */
15149   div = gen_divmod4_1 (operands[0], operands[1],
15150                        operands[2], operands[3]);
15151   emit_insn (div);
15152
15153   /* Branch to the end.  */
15154   emit_jump_insn (gen_jump (end_label));
15155   emit_barrier ();
15156
15157   /* Generate 8bit unsigned divide.  */
15158   emit_label (qimode_label);
15159   /* Don't use operands[0] for result of 8bit divide since not all
15160      registers support QImode ZERO_EXTRACT.  */
15161   tmp0 = simplify_gen_subreg (HImode, scratch, mode, 0);
15162   tmp1 = simplify_gen_subreg (HImode, operands[2], mode, 0);
15163   tmp2 = simplify_gen_subreg (QImode, operands[3], mode, 0);
15164   emit_insn (gen_udivmodhiqi3 (tmp0, tmp1, tmp2));
15165
15166   if (signed_p)
15167     {
15168       div = gen_rtx_DIV (SImode, operands[2], operands[3]);
15169       mod = gen_rtx_MOD (SImode, operands[2], operands[3]);
15170     }
15171   else
15172     {
15173       div = gen_rtx_UDIV (SImode, operands[2], operands[3]);
15174       mod = gen_rtx_UMOD (SImode, operands[2], operands[3]);
15175     }
15176
15177   /* Extract remainder from AH.  */
15178   tmp1 = gen_rtx_ZERO_EXTRACT (mode, tmp0, GEN_INT (8), GEN_INT (8));
15179   if (REG_P (operands[1]))
15180     insn = emit_move_insn (operands[1], tmp1);
15181   else
15182     {
15183       /* Need a new scratch register since the old one has result 
15184          of 8bit divide.  */
15185       scratch = gen_reg_rtx (mode);
15186       emit_move_insn (scratch, tmp1);
15187       insn = emit_move_insn (operands[1], scratch);
15188     }
15189   set_unique_reg_note (insn, REG_EQUAL, mod);
15190
15191   /* Zero extend quotient from AL.  */
15192   tmp1 = gen_lowpart (QImode, tmp0);
15193   insn = emit_insn (gen_zero_extend (operands[0], tmp1));
15194   set_unique_reg_note (insn, REG_EQUAL, div);
15195
15196   emit_label (end_label);
15197 }
15198
15199 #define LEA_SEARCH_THRESHOLD 12
15200
15201 /* Search backward for non-agu definition of register number REGNO1
15202    or register number REGNO2 in INSN's basic block until
15203    1. Pass LEA_SEARCH_THRESHOLD instructions, or
15204    2. Reach BB boundary, or
15205    3. Reach agu definition.
15206    Returns the distance between the non-agu definition point and INSN.
15207    If no definition point, returns -1.  */
15208
15209 static int
15210 distance_non_agu_define (unsigned int regno1, unsigned int regno2,
15211                          rtx insn)
15212 {
15213   basic_block bb = BLOCK_FOR_INSN (insn);
15214   int distance = 0;
15215   df_ref *def_rec;
15216   enum attr_type insn_type;
15217
15218   if (insn != BB_HEAD (bb))
15219     {
15220       rtx prev = PREV_INSN (insn);
15221       while (prev && distance < LEA_SEARCH_THRESHOLD)
15222         {
15223           if (NONDEBUG_INSN_P (prev))
15224             {
15225               distance++;
15226               for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
15227                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15228                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
15229                     && (regno1 == DF_REF_REGNO (*def_rec)
15230                         || regno2 == DF_REF_REGNO (*def_rec)))
15231                   {
15232                     insn_type = get_attr_type (prev);
15233                     if (insn_type != TYPE_LEA)
15234                       goto done;
15235                   }
15236             }
15237           if (prev == BB_HEAD (bb))
15238             break;
15239           prev = PREV_INSN (prev);
15240         }
15241     }
15242
15243   if (distance < LEA_SEARCH_THRESHOLD)
15244     {
15245       edge e;
15246       edge_iterator ei;
15247       bool simple_loop = false;
15248
15249       FOR_EACH_EDGE (e, ei, bb->preds)
15250         if (e->src == bb)
15251           {
15252             simple_loop = true;
15253             break;
15254           }
15255
15256       if (simple_loop)
15257         {
15258           rtx prev = BB_END (bb);
15259           while (prev
15260                  && prev != insn
15261                  && distance < LEA_SEARCH_THRESHOLD)
15262             {
15263               if (NONDEBUG_INSN_P (prev))
15264                 {
15265                   distance++;
15266                   for (def_rec = DF_INSN_DEFS (prev); *def_rec; def_rec++)
15267                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15268                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
15269                         && (regno1 == DF_REF_REGNO (*def_rec)
15270                             || regno2 == DF_REF_REGNO (*def_rec)))
15271                       {
15272                         insn_type = get_attr_type (prev);
15273                         if (insn_type != TYPE_LEA)
15274                           goto done;
15275                       }
15276                 }
15277               prev = PREV_INSN (prev);
15278             }
15279         }
15280     }
15281
15282   distance = -1;
15283
15284 done:
15285   /* get_attr_type may modify recog data.  We want to make sure
15286      that recog data is valid for instruction INSN, on which
15287      distance_non_agu_define is called.  INSN is unchanged here.  */
15288   extract_insn_cached (insn);
15289   return distance;
15290 }
15291
15292 /* Return the distance between INSN and the next insn that uses
15293    register number REGNO0 in memory address.  Return -1 if no such
15294    a use is found within LEA_SEARCH_THRESHOLD or REGNO0 is set.  */
15295
15296 static int
15297 distance_agu_use (unsigned int regno0, rtx insn)
15298 {
15299   basic_block bb = BLOCK_FOR_INSN (insn);
15300   int distance = 0;
15301   df_ref *def_rec;
15302   df_ref *use_rec;
15303
15304   if (insn != BB_END (bb))
15305     {
15306       rtx next = NEXT_INSN (insn);
15307       while (next && distance < LEA_SEARCH_THRESHOLD)
15308         {
15309           if (NONDEBUG_INSN_P (next))
15310             {
15311               distance++;
15312
15313               for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
15314                 if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
15315                      || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
15316                     && regno0 == DF_REF_REGNO (*use_rec))
15317                   {
15318                     /* Return DISTANCE if OP0 is used in memory
15319                        address in NEXT.  */
15320                     return distance;
15321                   }
15322
15323               for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
15324                 if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15325                     && !DF_REF_IS_ARTIFICIAL (*def_rec)
15326                     && regno0 == DF_REF_REGNO (*def_rec))
15327                   {
15328                     /* Return -1 if OP0 is set in NEXT.  */
15329                     return -1;
15330                   }
15331             }
15332           if (next == BB_END (bb))
15333             break;
15334           next = NEXT_INSN (next);
15335         }
15336     }
15337
15338   if (distance < LEA_SEARCH_THRESHOLD)
15339     {
15340       edge e;
15341       edge_iterator ei;
15342       bool simple_loop = false;
15343
15344       FOR_EACH_EDGE (e, ei, bb->succs)
15345         if (e->dest == bb)
15346           {
15347             simple_loop = true;
15348             break;
15349           }
15350
15351       if (simple_loop)
15352         {
15353           rtx next = BB_HEAD (bb);
15354           while (next
15355                  && next != insn
15356                  && distance < LEA_SEARCH_THRESHOLD)
15357             {
15358               if (NONDEBUG_INSN_P (next))
15359                 {
15360                   distance++;
15361
15362                   for (use_rec = DF_INSN_USES (next); *use_rec; use_rec++)
15363                     if ((DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_LOAD
15364                          || DF_REF_TYPE (*use_rec) == DF_REF_REG_MEM_STORE)
15365                         && regno0 == DF_REF_REGNO (*use_rec))
15366                       {
15367                         /* Return DISTANCE if OP0 is used in memory
15368                            address in NEXT.  */
15369                         return distance;
15370                       }
15371
15372                   for (def_rec = DF_INSN_DEFS (next); *def_rec; def_rec++)
15373                     if (DF_REF_TYPE (*def_rec) == DF_REF_REG_DEF
15374                         && !DF_REF_IS_ARTIFICIAL (*def_rec)
15375                         && regno0 == DF_REF_REGNO (*def_rec))
15376                       {
15377                         /* Return -1 if OP0 is set in NEXT.  */
15378                         return -1;
15379                       }
15380
15381                 }
15382               next = NEXT_INSN (next);
15383             }
15384         }
15385     }
15386
15387   return -1;
15388 }
15389
15390 /* Define this macro to tune LEA priority vs ADD, it take effect when
15391    there is a dilemma of choicing LEA or ADD
15392    Negative value: ADD is more preferred than LEA
15393    Zero: Netrual
15394    Positive value: LEA is more preferred than ADD*/
15395 #define IX86_LEA_PRIORITY 2
15396
15397 /* Return true if it is ok to optimize an ADD operation to LEA
15398    operation to avoid flag register consumation.  For most processors,
15399    ADD is faster than LEA.  For the processors like ATOM, if the
15400    destination register of LEA holds an actual address which will be
15401    used soon, LEA is better and otherwise ADD is better.  */
15402
15403 bool
15404 ix86_lea_for_add_ok (rtx insn, rtx operands[])
15405 {
15406   unsigned int regno0 = true_regnum (operands[0]);
15407   unsigned int regno1 = true_regnum (operands[1]);
15408   unsigned int regno2 = true_regnum (operands[2]);
15409
15410   /* If a = b + c, (a!=b && a!=c), must use lea form. */
15411   if (regno0 != regno1 && regno0 != regno2)
15412     return true;
15413
15414   if (!TARGET_OPT_AGU || optimize_function_for_size_p (cfun))
15415     return false;
15416   else
15417     {
15418       int dist_define, dist_use;
15419
15420       /* Return false if REGNO0 isn't used in memory address. */
15421       dist_use = distance_agu_use (regno0, insn);
15422       if (dist_use <= 0)
15423         return false;
15424
15425       dist_define = distance_non_agu_define (regno1, regno2, insn);
15426       if (dist_define <= 0)
15427         return true;
15428
15429       /* If this insn has both backward non-agu dependence and forward
15430          agu dependence, the one with short distance take effect. */
15431       if ((dist_define + IX86_LEA_PRIORITY) < dist_use)
15432         return false;
15433
15434       return true;
15435     }
15436 }
15437
15438 /* Return true if destination reg of SET_BODY is shift count of
15439    USE_BODY.  */
15440
15441 static bool
15442 ix86_dep_by_shift_count_body (const_rtx set_body, const_rtx use_body)
15443 {
15444   rtx set_dest;
15445   rtx shift_rtx;
15446   int i;
15447
15448   /* Retrieve destination of SET_BODY.  */
15449   switch (GET_CODE (set_body))
15450     {
15451     case SET:
15452       set_dest = SET_DEST (set_body);
15453       if (!set_dest || !REG_P (set_dest))
15454         return false;
15455       break;
15456     case PARALLEL:
15457       for (i = XVECLEN (set_body, 0) - 1; i >= 0; i--)
15458         if (ix86_dep_by_shift_count_body (XVECEXP (set_body, 0, i),
15459                                           use_body))
15460           return true;
15461     default:
15462       return false;
15463       break;
15464     }
15465
15466   /* Retrieve shift count of USE_BODY.  */
15467   switch (GET_CODE (use_body))
15468     {
15469     case SET:
15470       shift_rtx = XEXP (use_body, 1);
15471       break;
15472     case PARALLEL:
15473       for (i = XVECLEN (use_body, 0) - 1; i >= 0; i--)
15474         if (ix86_dep_by_shift_count_body (set_body,
15475                                           XVECEXP (use_body, 0, i)))
15476           return true;
15477     default:
15478       return false;
15479       break;
15480     }
15481
15482   if (shift_rtx
15483       && (GET_CODE (shift_rtx) == ASHIFT
15484           || GET_CODE (shift_rtx) == LSHIFTRT
15485           || GET_CODE (shift_rtx) == ASHIFTRT
15486           || GET_CODE (shift_rtx) == ROTATE
15487           || GET_CODE (shift_rtx) == ROTATERT))
15488     {
15489       rtx shift_count = XEXP (shift_rtx, 1);
15490
15491       /* Return true if shift count is dest of SET_BODY.  */
15492       if (REG_P (shift_count)
15493           && true_regnum (set_dest) == true_regnum (shift_count))
15494         return true;
15495     }
15496
15497   return false;
15498 }
15499
15500 /* Return true if destination reg of SET_INSN is shift count of
15501    USE_INSN.  */
15502
15503 bool
15504 ix86_dep_by_shift_count (const_rtx set_insn, const_rtx use_insn)
15505 {
15506   return ix86_dep_by_shift_count_body (PATTERN (set_insn),
15507                                        PATTERN (use_insn));
15508 }
15509
15510 /* Return TRUE or FALSE depending on whether the unary operator meets the
15511    appropriate constraints.  */
15512
15513 bool
15514 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
15515                         enum machine_mode mode ATTRIBUTE_UNUSED,
15516                         rtx operands[2] ATTRIBUTE_UNUSED)
15517 {
15518   /* If one of operands is memory, source and destination must match.  */
15519   if ((MEM_P (operands[0])
15520        || MEM_P (operands[1]))
15521       && ! rtx_equal_p (operands[0], operands[1]))
15522     return false;
15523   return true;
15524 }
15525
15526 /* Return TRUE if the operands to a vec_interleave_{high,low}v2df
15527    are ok, keeping in mind the possible movddup alternative.  */
15528
15529 bool
15530 ix86_vec_interleave_v2df_operator_ok (rtx operands[3], bool high)
15531 {
15532   if (MEM_P (operands[0]))
15533     return rtx_equal_p (operands[0], operands[1 + high]);
15534   if (MEM_P (operands[1]) && MEM_P (operands[2]))
15535     return TARGET_SSE3 && rtx_equal_p (operands[1], operands[2]);
15536   return true;
15537 }
15538
15539 /* Post-reload splitter for converting an SF or DFmode value in an
15540    SSE register into an unsigned SImode.  */
15541
15542 void
15543 ix86_split_convert_uns_si_sse (rtx operands[])
15544 {
15545   enum machine_mode vecmode;
15546   rtx value, large, zero_or_two31, input, two31, x;
15547
15548   large = operands[1];
15549   zero_or_two31 = operands[2];
15550   input = operands[3];
15551   two31 = operands[4];
15552   vecmode = GET_MODE (large);
15553   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
15554
15555   /* Load up the value into the low element.  We must ensure that the other
15556      elements are valid floats -- zero is the easiest such value.  */
15557   if (MEM_P (input))
15558     {
15559       if (vecmode == V4SFmode)
15560         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
15561       else
15562         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
15563     }
15564   else
15565     {
15566       input = gen_rtx_REG (vecmode, REGNO (input));
15567       emit_move_insn (value, CONST0_RTX (vecmode));
15568       if (vecmode == V4SFmode)
15569         emit_insn (gen_sse_movss (value, value, input));
15570       else
15571         emit_insn (gen_sse2_movsd (value, value, input));
15572     }
15573
15574   emit_move_insn (large, two31);
15575   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
15576
15577   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
15578   emit_insn (gen_rtx_SET (VOIDmode, large, x));
15579
15580   x = gen_rtx_AND (vecmode, zero_or_two31, large);
15581   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
15582
15583   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
15584   emit_insn (gen_rtx_SET (VOIDmode, value, x));
15585
15586   large = gen_rtx_REG (V4SImode, REGNO (large));
15587   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
15588
15589   x = gen_rtx_REG (V4SImode, REGNO (value));
15590   if (vecmode == V4SFmode)
15591     emit_insn (gen_sse2_cvttps2dq (x, value));
15592   else
15593     emit_insn (gen_sse2_cvttpd2dq (x, value));
15594   value = x;
15595
15596   emit_insn (gen_xorv4si3 (value, value, large));
15597 }
15598
15599 /* Convert an unsigned DImode value into a DFmode, using only SSE.
15600    Expects the 64-bit DImode to be supplied in a pair of integral
15601    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
15602    -mfpmath=sse, !optimize_size only.  */
15603
15604 void
15605 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
15606 {
15607   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
15608   rtx int_xmm, fp_xmm;
15609   rtx biases, exponents;
15610   rtx x;
15611
15612   int_xmm = gen_reg_rtx (V4SImode);
15613   if (TARGET_INTER_UNIT_MOVES)
15614     emit_insn (gen_movdi_to_sse (int_xmm, input));
15615   else if (TARGET_SSE_SPLIT_REGS)
15616     {
15617       emit_clobber (int_xmm);
15618       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
15619     }
15620   else
15621     {
15622       x = gen_reg_rtx (V2DImode);
15623       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
15624       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
15625     }
15626
15627   x = gen_rtx_CONST_VECTOR (V4SImode,
15628                             gen_rtvec (4, GEN_INT (0x43300000UL),
15629                                        GEN_INT (0x45300000UL),
15630                                        const0_rtx, const0_rtx));
15631   exponents = validize_mem (force_const_mem (V4SImode, x));
15632
15633   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
15634   emit_insn (gen_vec_interleave_lowv4si (int_xmm, int_xmm, exponents));
15635
15636   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
15637      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
15638      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
15639      (0x1.0p84 + double(fp_value_hi_xmm)).
15640      Note these exponents differ by 32.  */
15641
15642   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
15643
15644   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
15645      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
15646   real_ldexp (&bias_lo_rvt, &dconst1, 52);
15647   real_ldexp (&bias_hi_rvt, &dconst1, 84);
15648   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
15649   x = const_double_from_real_value (bias_hi_rvt, DFmode);
15650   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
15651   biases = validize_mem (force_const_mem (V2DFmode, biases));
15652   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
15653
15654   /* Add the upper and lower DFmode values together.  */
15655   if (TARGET_SSE3)
15656     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
15657   else
15658     {
15659       x = copy_to_mode_reg (V2DFmode, fp_xmm);
15660       emit_insn (gen_vec_interleave_highv2df (fp_xmm, fp_xmm, fp_xmm));
15661       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
15662     }
15663
15664   ix86_expand_vector_extract (false, target, fp_xmm, 0);
15665 }
15666
15667 /* Not used, but eases macroization of patterns.  */
15668 void
15669 ix86_expand_convert_uns_sixf_sse (rtx target ATTRIBUTE_UNUSED,
15670                                   rtx input ATTRIBUTE_UNUSED)
15671 {
15672   gcc_unreachable ();
15673 }
15674
15675 /* Convert an unsigned SImode value into a DFmode.  Only currently used
15676    for SSE, but applicable anywhere.  */
15677
15678 void
15679 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
15680 {
15681   REAL_VALUE_TYPE TWO31r;
15682   rtx x, fp;
15683
15684   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
15685                            NULL, 1, OPTAB_DIRECT);
15686
15687   fp = gen_reg_rtx (DFmode);
15688   emit_insn (gen_floatsidf2 (fp, x));
15689
15690   real_ldexp (&TWO31r, &dconst1, 31);
15691   x = const_double_from_real_value (TWO31r, DFmode);
15692
15693   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
15694   if (x != target)
15695     emit_move_insn (target, x);
15696 }
15697
15698 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
15699    32-bit mode; otherwise we have a direct convert instruction.  */
15700
15701 void
15702 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
15703 {
15704   REAL_VALUE_TYPE TWO32r;
15705   rtx fp_lo, fp_hi, x;
15706
15707   fp_lo = gen_reg_rtx (DFmode);
15708   fp_hi = gen_reg_rtx (DFmode);
15709
15710   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
15711
15712   real_ldexp (&TWO32r, &dconst1, 32);
15713   x = const_double_from_real_value (TWO32r, DFmode);
15714   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
15715
15716   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
15717
15718   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
15719                            0, OPTAB_DIRECT);
15720   if (x != target)
15721     emit_move_insn (target, x);
15722 }
15723
15724 /* Convert an unsigned SImode value into a SFmode, using only SSE.
15725    For x86_32, -mfpmath=sse, !optimize_size only.  */
15726 void
15727 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
15728 {
15729   REAL_VALUE_TYPE ONE16r;
15730   rtx fp_hi, fp_lo, int_hi, int_lo, x;
15731
15732   real_ldexp (&ONE16r, &dconst1, 16);
15733   x = const_double_from_real_value (ONE16r, SFmode);
15734   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
15735                                       NULL, 0, OPTAB_DIRECT);
15736   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
15737                                       NULL, 0, OPTAB_DIRECT);
15738   fp_hi = gen_reg_rtx (SFmode);
15739   fp_lo = gen_reg_rtx (SFmode);
15740   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
15741   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
15742   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
15743                                0, OPTAB_DIRECT);
15744   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
15745                                0, OPTAB_DIRECT);
15746   if (!rtx_equal_p (target, fp_hi))
15747     emit_move_insn (target, fp_hi);
15748 }
15749
15750 /* A subroutine of ix86_build_signbit_mask.  If VECT is true,
15751    then replicate the value for all elements of the vector
15752    register.  */
15753
15754 rtx
15755 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
15756 {
15757   rtvec v;
15758   switch (mode)
15759     {
15760     case V4SImode:
15761       gcc_assert (vect);
15762       v = gen_rtvec (4, value, value, value, value);
15763       return gen_rtx_CONST_VECTOR (V4SImode, v);
15764
15765     case V2DImode:
15766       gcc_assert (vect);
15767       v = gen_rtvec (2, value, value);
15768       return gen_rtx_CONST_VECTOR (V2DImode, v);
15769
15770     case V8SFmode:
15771       if (vect)
15772         v = gen_rtvec (8, value, value, value, value,
15773                        value, value, value, value);
15774       else
15775         v = gen_rtvec (8, value, CONST0_RTX (SFmode),
15776                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
15777                        CONST0_RTX (SFmode), CONST0_RTX (SFmode),
15778                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
15779       return gen_rtx_CONST_VECTOR (V8SFmode, v);
15780
15781     case V4SFmode:
15782       if (vect)
15783         v = gen_rtvec (4, value, value, value, value);
15784       else
15785         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
15786                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
15787       return gen_rtx_CONST_VECTOR (V4SFmode, v);
15788
15789     case V4DFmode:
15790       if (vect)
15791         v = gen_rtvec (4, value, value, value, value);
15792       else
15793         v = gen_rtvec (4, value, CONST0_RTX (DFmode),
15794                        CONST0_RTX (DFmode), CONST0_RTX (DFmode));
15795       return gen_rtx_CONST_VECTOR (V4DFmode, v);
15796
15797     case V2DFmode:
15798       if (vect)
15799         v = gen_rtvec (2, value, value);
15800       else
15801         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
15802       return gen_rtx_CONST_VECTOR (V2DFmode, v);
15803
15804     default:
15805       gcc_unreachable ();
15806     }
15807 }
15808
15809 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
15810    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
15811    for an SSE register.  If VECT is true, then replicate the mask for
15812    all elements of the vector register.  If INVERT is true, then create
15813    a mask excluding the sign bit.  */
15814
15815 rtx
15816 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
15817 {
15818   enum machine_mode vec_mode, imode;
15819   HOST_WIDE_INT hi, lo;
15820   int shift = 63;
15821   rtx v;
15822   rtx mask;
15823
15824   /* Find the sign bit, sign extended to 2*HWI.  */
15825   switch (mode)
15826     {
15827     case V4SImode:
15828     case V8SFmode:
15829     case V4SFmode:
15830       vec_mode = mode;
15831       mode = GET_MODE_INNER (mode);
15832       imode = SImode;
15833       lo = 0x80000000, hi = lo < 0;
15834       break;
15835
15836     case V2DImode:
15837     case V4DFmode:
15838     case V2DFmode:
15839       vec_mode = mode;
15840       mode = GET_MODE_INNER (mode);
15841       imode = DImode;
15842       if (HOST_BITS_PER_WIDE_INT >= 64)
15843         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
15844       else
15845         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
15846       break;
15847
15848     case TImode:
15849     case TFmode:
15850       vec_mode = VOIDmode;
15851       if (HOST_BITS_PER_WIDE_INT >= 64)
15852         {
15853           imode = TImode;
15854           lo = 0, hi = (HOST_WIDE_INT)1 << shift;
15855         }
15856       else
15857         {
15858           rtvec vec;
15859
15860           imode = DImode;
15861           lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
15862
15863           if (invert)
15864             {
15865               lo = ~lo, hi = ~hi;
15866               v = constm1_rtx;
15867             }
15868           else
15869             v = const0_rtx;
15870
15871           mask = immed_double_const (lo, hi, imode);
15872
15873           vec = gen_rtvec (2, v, mask);
15874           v = gen_rtx_CONST_VECTOR (V2DImode, vec);
15875           v = copy_to_mode_reg (mode, gen_lowpart (mode, v));
15876
15877           return v;
15878         }
15879      break;
15880
15881     default:
15882       gcc_unreachable ();
15883     }
15884
15885   if (invert)
15886     lo = ~lo, hi = ~hi;
15887
15888   /* Force this value into the low part of a fp vector constant.  */
15889   mask = immed_double_const (lo, hi, imode);
15890   mask = gen_lowpart (mode, mask);
15891
15892   if (vec_mode == VOIDmode)
15893     return force_reg (mode, mask);
15894
15895   v = ix86_build_const_vector (vec_mode, vect, mask);
15896   return force_reg (vec_mode, v);
15897 }
15898
15899 /* Generate code for floating point ABS or NEG.  */
15900
15901 void
15902 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
15903                                 rtx operands[])
15904 {
15905   rtx mask, set, use, clob, dst, src;
15906   bool use_sse = false;
15907   bool vector_mode = VECTOR_MODE_P (mode);
15908   enum machine_mode vmode = mode;
15909
15910   if (vector_mode)
15911     use_sse = true;
15912   else if (mode == TFmode)
15913     use_sse = true;
15914   else if (TARGET_SSE_MATH)
15915     {
15916       use_sse = SSE_FLOAT_MODE_P (mode);
15917       if (mode == SFmode)
15918         vmode = V4SFmode;
15919       else if (mode == DFmode)
15920         vmode = V2DFmode;
15921     }
15922
15923   /* NEG and ABS performed with SSE use bitwise mask operations.
15924      Create the appropriate mask now.  */
15925   if (use_sse)
15926     mask = ix86_build_signbit_mask (vmode, vector_mode, code == ABS);
15927   else
15928     mask = NULL_RTX;
15929
15930   dst = operands[0];
15931   src = operands[1];
15932
15933   if (vector_mode)
15934     {
15935       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
15936       set = gen_rtx_SET (VOIDmode, dst, set);
15937       emit_insn (set);
15938     }
15939   else
15940     {
15941       set = gen_rtx_fmt_e (code, mode, src);
15942       set = gen_rtx_SET (VOIDmode, dst, set);
15943       if (mask)
15944         {
15945           use = gen_rtx_USE (VOIDmode, mask);
15946           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
15947           emit_insn (gen_rtx_PARALLEL (VOIDmode,
15948                                        gen_rtvec (3, set, use, clob)));
15949         }
15950       else
15951         emit_insn (set);
15952     }
15953 }
15954
15955 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
15956
15957 void
15958 ix86_expand_copysign (rtx operands[])
15959 {
15960   enum machine_mode mode, vmode;
15961   rtx dest, op0, op1, mask, nmask;
15962
15963   dest = operands[0];
15964   op0 = operands[1];
15965   op1 = operands[2];
15966
15967   mode = GET_MODE (dest);
15968
15969   if (mode == SFmode)
15970     vmode = V4SFmode;
15971   else if (mode == DFmode)
15972     vmode = V2DFmode;
15973   else
15974     vmode = mode;
15975
15976   if (GET_CODE (op0) == CONST_DOUBLE)
15977     {
15978       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
15979
15980       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
15981         op0 = simplify_unary_operation (ABS, mode, op0, mode);
15982
15983       if (mode == SFmode || mode == DFmode)
15984         {
15985           if (op0 == CONST0_RTX (mode))
15986             op0 = CONST0_RTX (vmode);
15987           else
15988             {
15989               rtx v = ix86_build_const_vector (vmode, false, op0);
15990
15991               op0 = force_reg (vmode, v);
15992             }
15993         }
15994       else if (op0 != CONST0_RTX (mode))
15995         op0 = force_reg (mode, op0);
15996
15997       mask = ix86_build_signbit_mask (vmode, 0, 0);
15998
15999       if (mode == SFmode)
16000         copysign_insn = gen_copysignsf3_const;
16001       else if (mode == DFmode)
16002         copysign_insn = gen_copysigndf3_const;
16003       else
16004         copysign_insn = gen_copysigntf3_const;
16005
16006         emit_insn (copysign_insn (dest, op0, op1, mask));
16007     }
16008   else
16009     {
16010       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
16011
16012       nmask = ix86_build_signbit_mask (vmode, 0, 1);
16013       mask = ix86_build_signbit_mask (vmode, 0, 0);
16014
16015       if (mode == SFmode)
16016         copysign_insn = gen_copysignsf3_var;
16017       else if (mode == DFmode)
16018         copysign_insn = gen_copysigndf3_var;
16019       else
16020         copysign_insn = gen_copysigntf3_var;
16021
16022       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
16023     }
16024 }
16025
16026 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
16027    be a constant, and so has already been expanded into a vector constant.  */
16028
16029 void
16030 ix86_split_copysign_const (rtx operands[])
16031 {
16032   enum machine_mode mode, vmode;
16033   rtx dest, op0, mask, x;
16034
16035   dest = operands[0];
16036   op0 = operands[1];
16037   mask = operands[3];
16038
16039   mode = GET_MODE (dest);
16040   vmode = GET_MODE (mask);
16041
16042   dest = simplify_gen_subreg (vmode, dest, mode, 0);
16043   x = gen_rtx_AND (vmode, dest, mask);
16044   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16045
16046   if (op0 != CONST0_RTX (vmode))
16047     {
16048       x = gen_rtx_IOR (vmode, dest, op0);
16049       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16050     }
16051 }
16052
16053 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
16054    so we have to do two masks.  */
16055
16056 void
16057 ix86_split_copysign_var (rtx operands[])
16058 {
16059   enum machine_mode mode, vmode;
16060   rtx dest, scratch, op0, op1, mask, nmask, x;
16061
16062   dest = operands[0];
16063   scratch = operands[1];
16064   op0 = operands[2];
16065   op1 = operands[3];
16066   nmask = operands[4];
16067   mask = operands[5];
16068
16069   mode = GET_MODE (dest);
16070   vmode = GET_MODE (mask);
16071
16072   if (rtx_equal_p (op0, op1))
16073     {
16074       /* Shouldn't happen often (it's useless, obviously), but when it does
16075          we'd generate incorrect code if we continue below.  */
16076       emit_move_insn (dest, op0);
16077       return;
16078     }
16079
16080   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
16081     {
16082       gcc_assert (REGNO (op1) == REGNO (scratch));
16083
16084       x = gen_rtx_AND (vmode, scratch, mask);
16085       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16086
16087       dest = mask;
16088       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
16089       x = gen_rtx_NOT (vmode, dest);
16090       x = gen_rtx_AND (vmode, x, op0);
16091       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16092     }
16093   else
16094     {
16095       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
16096         {
16097           x = gen_rtx_AND (vmode, scratch, mask);
16098         }
16099       else                                              /* alternative 2,4 */
16100         {
16101           gcc_assert (REGNO (mask) == REGNO (scratch));
16102           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
16103           x = gen_rtx_AND (vmode, scratch, op1);
16104         }
16105       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
16106
16107       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
16108         {
16109           dest = simplify_gen_subreg (vmode, op0, mode, 0);
16110           x = gen_rtx_AND (vmode, dest, nmask);
16111         }
16112       else                                              /* alternative 3,4 */
16113         {
16114           gcc_assert (REGNO (nmask) == REGNO (dest));
16115           dest = nmask;
16116           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
16117           x = gen_rtx_AND (vmode, dest, op0);
16118         }
16119       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16120     }
16121
16122   x = gen_rtx_IOR (vmode, dest, scratch);
16123   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
16124 }
16125
16126 /* Return TRUE or FALSE depending on whether the first SET in INSN
16127    has source and destination with matching CC modes, and that the
16128    CC mode is at least as constrained as REQ_MODE.  */
16129
16130 bool
16131 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
16132 {
16133   rtx set;
16134   enum machine_mode set_mode;
16135
16136   set = PATTERN (insn);
16137   if (GET_CODE (set) == PARALLEL)
16138     set = XVECEXP (set, 0, 0);
16139   gcc_assert (GET_CODE (set) == SET);
16140   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
16141
16142   set_mode = GET_MODE (SET_DEST (set));
16143   switch (set_mode)
16144     {
16145     case CCNOmode:
16146       if (req_mode != CCNOmode
16147           && (req_mode != CCmode
16148               || XEXP (SET_SRC (set), 1) != const0_rtx))
16149         return false;
16150       break;
16151     case CCmode:
16152       if (req_mode == CCGCmode)
16153         return false;
16154       /* FALLTHRU */
16155     case CCGCmode:
16156       if (req_mode == CCGOCmode || req_mode == CCNOmode)
16157         return false;
16158       /* FALLTHRU */
16159     case CCGOCmode:
16160       if (req_mode == CCZmode)
16161         return false;
16162       /* FALLTHRU */
16163     case CCAmode:
16164     case CCCmode:
16165     case CCOmode:
16166     case CCSmode:
16167     case CCZmode:
16168       break;
16169
16170     default:
16171       gcc_unreachable ();
16172     }
16173
16174   return GET_MODE (SET_SRC (set)) == set_mode;
16175 }
16176
16177 /* Generate insn patterns to do an integer compare of OPERANDS.  */
16178
16179 static rtx
16180 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
16181 {
16182   enum machine_mode cmpmode;
16183   rtx tmp, flags;
16184
16185   cmpmode = SELECT_CC_MODE (code, op0, op1);
16186   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
16187
16188   /* This is very simple, but making the interface the same as in the
16189      FP case makes the rest of the code easier.  */
16190   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
16191   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
16192
16193   /* Return the test that should be put into the flags user, i.e.
16194      the bcc, scc, or cmov instruction.  */
16195   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
16196 }
16197
16198 /* Figure out whether to use ordered or unordered fp comparisons.
16199    Return the appropriate mode to use.  */
16200
16201 enum machine_mode
16202 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
16203 {
16204   /* ??? In order to make all comparisons reversible, we do all comparisons
16205      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
16206      all forms trapping and nontrapping comparisons, we can make inequality
16207      comparisons trapping again, since it results in better code when using
16208      FCOM based compares.  */
16209   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
16210 }
16211
16212 enum machine_mode
16213 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
16214 {
16215   enum machine_mode mode = GET_MODE (op0);
16216
16217   if (SCALAR_FLOAT_MODE_P (mode))
16218     {
16219       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
16220       return ix86_fp_compare_mode (code);
16221     }
16222
16223   switch (code)
16224     {
16225       /* Only zero flag is needed.  */
16226     case EQ:                    /* ZF=0 */
16227     case NE:                    /* ZF!=0 */
16228       return CCZmode;
16229       /* Codes needing carry flag.  */
16230     case GEU:                   /* CF=0 */
16231     case LTU:                   /* CF=1 */
16232       /* Detect overflow checks.  They need just the carry flag.  */
16233       if (GET_CODE (op0) == PLUS
16234           && rtx_equal_p (op1, XEXP (op0, 0)))
16235         return CCCmode;
16236       else
16237         return CCmode;
16238     case GTU:                   /* CF=0 & ZF=0 */
16239     case LEU:                   /* CF=1 | ZF=1 */
16240       /* Detect overflow checks.  They need just the carry flag.  */
16241       if (GET_CODE (op0) == MINUS
16242           && rtx_equal_p (op1, XEXP (op0, 0)))
16243         return CCCmode;
16244       else
16245         return CCmode;
16246       /* Codes possibly doable only with sign flag when
16247          comparing against zero.  */
16248     case GE:                    /* SF=OF   or   SF=0 */
16249     case LT:                    /* SF<>OF  or   SF=1 */
16250       if (op1 == const0_rtx)
16251         return CCGOCmode;
16252       else
16253         /* For other cases Carry flag is not required.  */
16254         return CCGCmode;
16255       /* Codes doable only with sign flag when comparing
16256          against zero, but we miss jump instruction for it
16257          so we need to use relational tests against overflow
16258          that thus needs to be zero.  */
16259     case GT:                    /* ZF=0 & SF=OF */
16260     case LE:                    /* ZF=1 | SF<>OF */
16261       if (op1 == const0_rtx)
16262         return CCNOmode;
16263       else
16264         return CCGCmode;
16265       /* strcmp pattern do (use flags) and combine may ask us for proper
16266          mode.  */
16267     case USE:
16268       return CCmode;
16269     default:
16270       gcc_unreachable ();
16271     }
16272 }
16273
16274 /* Return the fixed registers used for condition codes.  */
16275
16276 static bool
16277 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
16278 {
16279   *p1 = FLAGS_REG;
16280   *p2 = FPSR_REG;
16281   return true;
16282 }
16283
16284 /* If two condition code modes are compatible, return a condition code
16285    mode which is compatible with both.  Otherwise, return
16286    VOIDmode.  */
16287
16288 static enum machine_mode
16289 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
16290 {
16291   if (m1 == m2)
16292     return m1;
16293
16294   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
16295     return VOIDmode;
16296
16297   if ((m1 == CCGCmode && m2 == CCGOCmode)
16298       || (m1 == CCGOCmode && m2 == CCGCmode))
16299     return CCGCmode;
16300
16301   switch (m1)
16302     {
16303     default:
16304       gcc_unreachable ();
16305
16306     case CCmode:
16307     case CCGCmode:
16308     case CCGOCmode:
16309     case CCNOmode:
16310     case CCAmode:
16311     case CCCmode:
16312     case CCOmode:
16313     case CCSmode:
16314     case CCZmode:
16315       switch (m2)
16316         {
16317         default:
16318           return VOIDmode;
16319
16320         case CCmode:
16321         case CCGCmode:
16322         case CCGOCmode:
16323         case CCNOmode:
16324         case CCAmode:
16325         case CCCmode:
16326         case CCOmode:
16327         case CCSmode:
16328         case CCZmode:
16329           return CCmode;
16330         }
16331
16332     case CCFPmode:
16333     case CCFPUmode:
16334       /* These are only compatible with themselves, which we already
16335          checked above.  */
16336       return VOIDmode;
16337     }
16338 }
16339
16340
16341 /* Return a comparison we can do and that it is equivalent to
16342    swap_condition (code) apart possibly from orderedness.
16343    But, never change orderedness if TARGET_IEEE_FP, returning
16344    UNKNOWN in that case if necessary.  */
16345
16346 static enum rtx_code
16347 ix86_fp_swap_condition (enum rtx_code code)
16348 {
16349   switch (code)
16350     {
16351     case GT:                   /* GTU - CF=0 & ZF=0 */
16352       return TARGET_IEEE_FP ? UNKNOWN : UNLT;
16353     case GE:                   /* GEU - CF=0 */
16354       return TARGET_IEEE_FP ? UNKNOWN : UNLE;
16355     case UNLT:                 /* LTU - CF=1 */
16356       return TARGET_IEEE_FP ? UNKNOWN : GT;
16357     case UNLE:                 /* LEU - CF=1 | ZF=1 */
16358       return TARGET_IEEE_FP ? UNKNOWN : GE;
16359     default:
16360       return swap_condition (code);
16361     }
16362 }
16363
16364 /* Return cost of comparison CODE using the best strategy for performance.
16365    All following functions do use number of instructions as a cost metrics.
16366    In future this should be tweaked to compute bytes for optimize_size and
16367    take into account performance of various instructions on various CPUs.  */
16368
16369 static int
16370 ix86_fp_comparison_cost (enum rtx_code code)
16371 {
16372   int arith_cost;
16373
16374   /* The cost of code using bit-twiddling on %ah.  */
16375   switch (code)
16376     {
16377     case UNLE:
16378     case UNLT:
16379     case LTGT:
16380     case GT:
16381     case GE:
16382     case UNORDERED:
16383     case ORDERED:
16384     case UNEQ:
16385       arith_cost = 4;
16386       break;
16387     case LT:
16388     case NE:
16389     case EQ:
16390     case UNGE:
16391       arith_cost = TARGET_IEEE_FP ? 5 : 4;
16392       break;
16393     case LE:
16394     case UNGT:
16395       arith_cost = TARGET_IEEE_FP ? 6 : 4;
16396       break;
16397     default:
16398       gcc_unreachable ();
16399     }
16400
16401   switch (ix86_fp_comparison_strategy (code))
16402     {
16403     case IX86_FPCMP_COMI:
16404       return arith_cost > 4 ? 3 : 2;
16405     case IX86_FPCMP_SAHF:
16406       return arith_cost > 4 ? 4 : 3;
16407     default:
16408       return arith_cost;
16409     }
16410 }
16411
16412 /* Return strategy to use for floating-point.  We assume that fcomi is always
16413    preferrable where available, since that is also true when looking at size
16414    (2 bytes, vs. 3 for fnstsw+sahf and at least 5 for fnstsw+test).  */
16415
16416 enum ix86_fpcmp_strategy
16417 ix86_fp_comparison_strategy (enum rtx_code code ATTRIBUTE_UNUSED)
16418 {
16419   /* Do fcomi/sahf based test when profitable.  */
16420
16421   if (TARGET_CMOVE)
16422     return IX86_FPCMP_COMI;
16423
16424   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_function_for_size_p (cfun)))
16425     return IX86_FPCMP_SAHF;
16426
16427   return IX86_FPCMP_ARITH;
16428 }
16429
16430 /* Swap, force into registers, or otherwise massage the two operands
16431    to a fp comparison.  The operands are updated in place; the new
16432    comparison code is returned.  */
16433
16434 static enum rtx_code
16435 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
16436 {
16437   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
16438   rtx op0 = *pop0, op1 = *pop1;
16439   enum machine_mode op_mode = GET_MODE (op0);
16440   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
16441
16442   /* All of the unordered compare instructions only work on registers.
16443      The same is true of the fcomi compare instructions.  The XFmode
16444      compare instructions require registers except when comparing
16445      against zero or when converting operand 1 from fixed point to
16446      floating point.  */
16447
16448   if (!is_sse
16449       && (fpcmp_mode == CCFPUmode
16450           || (op_mode == XFmode
16451               && ! (standard_80387_constant_p (op0) == 1
16452                     || standard_80387_constant_p (op1) == 1)
16453               && GET_CODE (op1) != FLOAT)
16454           || ix86_fp_comparison_strategy (code) == IX86_FPCMP_COMI))
16455     {
16456       op0 = force_reg (op_mode, op0);
16457       op1 = force_reg (op_mode, op1);
16458     }
16459   else
16460     {
16461       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
16462          things around if they appear profitable, otherwise force op0
16463          into a register.  */
16464
16465       if (standard_80387_constant_p (op0) == 0
16466           || (MEM_P (op0)
16467               && ! (standard_80387_constant_p (op1) == 0
16468                     || MEM_P (op1))))
16469         {
16470           enum rtx_code new_code = ix86_fp_swap_condition (code);
16471           if (new_code != UNKNOWN)
16472             {
16473               rtx tmp;
16474               tmp = op0, op0 = op1, op1 = tmp;
16475               code = new_code;
16476             }
16477         }
16478
16479       if (!REG_P (op0))
16480         op0 = force_reg (op_mode, op0);
16481
16482       if (CONSTANT_P (op1))
16483         {
16484           int tmp = standard_80387_constant_p (op1);
16485           if (tmp == 0)
16486             op1 = validize_mem (force_const_mem (op_mode, op1));
16487           else if (tmp == 1)
16488             {
16489               if (TARGET_CMOVE)
16490                 op1 = force_reg (op_mode, op1);
16491             }
16492           else
16493             op1 = force_reg (op_mode, op1);
16494         }
16495     }
16496
16497   /* Try to rearrange the comparison to make it cheaper.  */
16498   if (ix86_fp_comparison_cost (code)
16499       > ix86_fp_comparison_cost (swap_condition (code))
16500       && (REG_P (op1) || can_create_pseudo_p ()))
16501     {
16502       rtx tmp;
16503       tmp = op0, op0 = op1, op1 = tmp;
16504       code = swap_condition (code);
16505       if (!REG_P (op0))
16506         op0 = force_reg (op_mode, op0);
16507     }
16508
16509   *pop0 = op0;
16510   *pop1 = op1;
16511   return code;
16512 }
16513
16514 /* Convert comparison codes we use to represent FP comparison to integer
16515    code that will result in proper branch.  Return UNKNOWN if no such code
16516    is available.  */
16517
16518 enum rtx_code
16519 ix86_fp_compare_code_to_integer (enum rtx_code code)
16520 {
16521   switch (code)
16522     {
16523     case GT:
16524       return GTU;
16525     case GE:
16526       return GEU;
16527     case ORDERED:
16528     case UNORDERED:
16529       return code;
16530       break;
16531     case UNEQ:
16532       return EQ;
16533       break;
16534     case UNLT:
16535       return LTU;
16536       break;
16537     case UNLE:
16538       return LEU;
16539       break;
16540     case LTGT:
16541       return NE;
16542       break;
16543     default:
16544       return UNKNOWN;
16545     }
16546 }
16547
16548 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
16549
16550 static rtx
16551 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch)
16552 {
16553   enum machine_mode fpcmp_mode, intcmp_mode;
16554   rtx tmp, tmp2;
16555
16556   fpcmp_mode = ix86_fp_compare_mode (code);
16557   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
16558
16559   /* Do fcomi/sahf based test when profitable.  */
16560   switch (ix86_fp_comparison_strategy (code))
16561     {
16562     case IX86_FPCMP_COMI:
16563       intcmp_mode = fpcmp_mode;
16564       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
16565       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
16566                          tmp);
16567       emit_insn (tmp);
16568       break;
16569
16570     case IX86_FPCMP_SAHF:
16571       intcmp_mode = fpcmp_mode;
16572       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
16573       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
16574                          tmp);
16575
16576       if (!scratch)
16577         scratch = gen_reg_rtx (HImode);
16578       tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
16579       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
16580       break;
16581
16582     case IX86_FPCMP_ARITH:
16583       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
16584       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
16585       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
16586       if (!scratch)
16587         scratch = gen_reg_rtx (HImode);
16588       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
16589
16590       /* In the unordered case, we have to check C2 for NaN's, which
16591          doesn't happen to work out to anything nice combination-wise.
16592          So do some bit twiddling on the value we've got in AH to come
16593          up with an appropriate set of condition codes.  */
16594
16595       intcmp_mode = CCNOmode;
16596       switch (code)
16597         {
16598         case GT:
16599         case UNGT:
16600           if (code == GT || !TARGET_IEEE_FP)
16601             {
16602               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
16603               code = EQ;
16604             }
16605           else
16606             {
16607               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16608               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
16609               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
16610               intcmp_mode = CCmode;
16611               code = GEU;
16612             }
16613           break;
16614         case LT:
16615         case UNLT:
16616           if (code == LT && TARGET_IEEE_FP)
16617             {
16618               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16619               emit_insn (gen_cmpqi_ext_3 (scratch, const1_rtx));
16620               intcmp_mode = CCmode;
16621               code = EQ;
16622             }
16623           else
16624             {
16625               emit_insn (gen_testqi_ext_ccno_0 (scratch, const1_rtx));
16626               code = NE;
16627             }
16628           break;
16629         case GE:
16630         case UNGE:
16631           if (code == GE || !TARGET_IEEE_FP)
16632             {
16633               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
16634               code = EQ;
16635             }
16636           else
16637             {
16638               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16639               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch, const1_rtx));
16640               code = NE;
16641             }
16642           break;
16643         case LE:
16644         case UNLE:
16645           if (code == LE && TARGET_IEEE_FP)
16646             {
16647               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16648               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
16649               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
16650               intcmp_mode = CCmode;
16651               code = LTU;
16652             }
16653           else
16654             {
16655               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
16656               code = NE;
16657             }
16658           break;
16659         case EQ:
16660         case UNEQ:
16661           if (code == EQ && TARGET_IEEE_FP)
16662             {
16663               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16664               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
16665               intcmp_mode = CCmode;
16666               code = EQ;
16667             }
16668           else
16669             {
16670               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
16671               code = NE;
16672             }
16673           break;
16674         case NE:
16675         case LTGT:
16676           if (code == NE && TARGET_IEEE_FP)
16677             {
16678               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
16679               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
16680                                              GEN_INT (0x40)));
16681               code = NE;
16682             }
16683           else
16684             {
16685               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
16686               code = EQ;
16687             }
16688           break;
16689
16690         case UNORDERED:
16691           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
16692           code = NE;
16693           break;
16694         case ORDERED:
16695           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
16696           code = EQ;
16697           break;
16698
16699         default:
16700           gcc_unreachable ();
16701         }
16702         break;
16703
16704     default:
16705       gcc_unreachable();
16706     }
16707
16708   /* Return the test that should be put into the flags user, i.e.
16709      the bcc, scc, or cmov instruction.  */
16710   return gen_rtx_fmt_ee (code, VOIDmode,
16711                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
16712                          const0_rtx);
16713 }
16714
16715 static rtx
16716 ix86_expand_compare (enum rtx_code code, rtx op0, rtx op1)
16717 {
16718   rtx ret;
16719
16720   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
16721     ret = gen_rtx_fmt_ee (code, VOIDmode, op0, op1);
16722
16723   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
16724     {
16725       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
16726       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
16727     }
16728   else
16729     ret = ix86_expand_int_compare (code, op0, op1);
16730
16731   return ret;
16732 }
16733
16734 void
16735 ix86_expand_branch (enum rtx_code code, rtx op0, rtx op1, rtx label)
16736 {
16737   enum machine_mode mode = GET_MODE (op0);
16738   rtx tmp;
16739
16740   switch (mode)
16741     {
16742     case SFmode:
16743     case DFmode:
16744     case XFmode:
16745     case QImode:
16746     case HImode:
16747     case SImode:
16748       simple:
16749       tmp = ix86_expand_compare (code, op0, op1);
16750       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
16751                                   gen_rtx_LABEL_REF (VOIDmode, label),
16752                                   pc_rtx);
16753       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
16754       return;
16755
16756     case DImode:
16757       if (TARGET_64BIT)
16758         goto simple;
16759     case TImode:
16760       /* Expand DImode branch into multiple compare+branch.  */
16761       {
16762         rtx lo[2], hi[2], label2;
16763         enum rtx_code code1, code2, code3;
16764         enum machine_mode submode;
16765
16766         if (CONSTANT_P (op0) && !CONSTANT_P (op1))
16767           {
16768             tmp = op0, op0 = op1, op1 = tmp;
16769             code = swap_condition (code);
16770           }
16771
16772         split_double_mode (mode, &op0, 1, lo+0, hi+0);
16773         split_double_mode (mode, &op1, 1, lo+1, hi+1);
16774
16775         submode = mode == DImode ? SImode : DImode;
16776
16777         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
16778            avoid two branches.  This costs one extra insn, so disable when
16779            optimizing for size.  */
16780
16781         if ((code == EQ || code == NE)
16782             && (!optimize_insn_for_size_p ()
16783                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
16784           {
16785             rtx xor0, xor1;
16786
16787             xor1 = hi[0];
16788             if (hi[1] != const0_rtx)
16789               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
16790                                    NULL_RTX, 0, OPTAB_WIDEN);
16791
16792             xor0 = lo[0];
16793             if (lo[1] != const0_rtx)
16794               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
16795                                    NULL_RTX, 0, OPTAB_WIDEN);
16796
16797             tmp = expand_binop (submode, ior_optab, xor1, xor0,
16798                                 NULL_RTX, 0, OPTAB_WIDEN);
16799
16800             ix86_expand_branch (code, tmp, const0_rtx, label);
16801             return;
16802           }
16803
16804         /* Otherwise, if we are doing less-than or greater-or-equal-than,
16805            op1 is a constant and the low word is zero, then we can just
16806            examine the high word.  Similarly for low word -1 and
16807            less-or-equal-than or greater-than.  */
16808
16809         if (CONST_INT_P (hi[1]))
16810           switch (code)
16811             {
16812             case LT: case LTU: case GE: case GEU:
16813               if (lo[1] == const0_rtx)
16814                 {
16815                   ix86_expand_branch (code, hi[0], hi[1], label);
16816                   return;
16817                 }
16818               break;
16819             case LE: case LEU: case GT: case GTU:
16820               if (lo[1] == constm1_rtx)
16821                 {
16822                   ix86_expand_branch (code, hi[0], hi[1], label);
16823                   return;
16824                 }
16825               break;
16826             default:
16827               break;
16828             }
16829
16830         /* Otherwise, we need two or three jumps.  */
16831
16832         label2 = gen_label_rtx ();
16833
16834         code1 = code;
16835         code2 = swap_condition (code);
16836         code3 = unsigned_condition (code);
16837
16838         switch (code)
16839           {
16840           case LT: case GT: case LTU: case GTU:
16841             break;
16842
16843           case LE:   code1 = LT;  code2 = GT;  break;
16844           case GE:   code1 = GT;  code2 = LT;  break;
16845           case LEU:  code1 = LTU; code2 = GTU; break;
16846           case GEU:  code1 = GTU; code2 = LTU; break;
16847
16848           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
16849           case NE:   code2 = UNKNOWN; break;
16850
16851           default:
16852             gcc_unreachable ();
16853           }
16854
16855         /*
16856          * a < b =>
16857          *    if (hi(a) < hi(b)) goto true;
16858          *    if (hi(a) > hi(b)) goto false;
16859          *    if (lo(a) < lo(b)) goto true;
16860          *  false:
16861          */
16862
16863         if (code1 != UNKNOWN)
16864           ix86_expand_branch (code1, hi[0], hi[1], label);
16865         if (code2 != UNKNOWN)
16866           ix86_expand_branch (code2, hi[0], hi[1], label2);
16867
16868         ix86_expand_branch (code3, lo[0], lo[1], label);
16869
16870         if (code2 != UNKNOWN)
16871           emit_label (label2);
16872         return;
16873       }
16874
16875     default:
16876       gcc_assert (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC);
16877       goto simple;
16878     }
16879 }
16880
16881 /* Split branch based on floating point condition.  */
16882 void
16883 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
16884                       rtx target1, rtx target2, rtx tmp, rtx pushed)
16885 {
16886   rtx condition;
16887   rtx i;
16888
16889   if (target2 != pc_rtx)
16890     {
16891       rtx tmp = target2;
16892       code = reverse_condition_maybe_unordered (code);
16893       target2 = target1;
16894       target1 = tmp;
16895     }
16896
16897   condition = ix86_expand_fp_compare (code, op1, op2,
16898                                       tmp);
16899
16900   /* Remove pushed operand from stack.  */
16901   if (pushed)
16902     ix86_free_from_memory (GET_MODE (pushed));
16903
16904   i = emit_jump_insn (gen_rtx_SET
16905                       (VOIDmode, pc_rtx,
16906                        gen_rtx_IF_THEN_ELSE (VOIDmode,
16907                                              condition, target1, target2)));
16908   if (split_branch_probability >= 0)
16909     add_reg_note (i, REG_BR_PROB, GEN_INT (split_branch_probability));
16910 }
16911
16912 void
16913 ix86_expand_setcc (rtx dest, enum rtx_code code, rtx op0, rtx op1)
16914 {
16915   rtx ret;
16916
16917   gcc_assert (GET_MODE (dest) == QImode);
16918
16919   ret = ix86_expand_compare (code, op0, op1);
16920   PUT_MODE (ret, QImode);
16921   emit_insn (gen_rtx_SET (VOIDmode, dest, ret));
16922 }
16923
16924 /* Expand comparison setting or clearing carry flag.  Return true when
16925    successful and set pop for the operation.  */
16926 static bool
16927 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
16928 {
16929   enum machine_mode mode =
16930     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
16931
16932   /* Do not handle double-mode compares that go through special path.  */
16933   if (mode == (TARGET_64BIT ? TImode : DImode))
16934     return false;
16935
16936   if (SCALAR_FLOAT_MODE_P (mode))
16937     {
16938       rtx compare_op, compare_seq;
16939
16940       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
16941
16942       /* Shortcut:  following common codes never translate
16943          into carry flag compares.  */
16944       if (code == EQ || code == NE || code == UNEQ || code == LTGT
16945           || code == ORDERED || code == UNORDERED)
16946         return false;
16947
16948       /* These comparisons require zero flag; swap operands so they won't.  */
16949       if ((code == GT || code == UNLE || code == LE || code == UNGT)
16950           && !TARGET_IEEE_FP)
16951         {
16952           rtx tmp = op0;
16953           op0 = op1;
16954           op1 = tmp;
16955           code = swap_condition (code);
16956         }
16957
16958       /* Try to expand the comparison and verify that we end up with
16959          carry flag based comparison.  This fails to be true only when
16960          we decide to expand comparison using arithmetic that is not
16961          too common scenario.  */
16962       start_sequence ();
16963       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX);
16964       compare_seq = get_insns ();
16965       end_sequence ();
16966
16967       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
16968           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
16969         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
16970       else
16971         code = GET_CODE (compare_op);
16972
16973       if (code != LTU && code != GEU)
16974         return false;
16975
16976       emit_insn (compare_seq);
16977       *pop = compare_op;
16978       return true;
16979     }
16980
16981   if (!INTEGRAL_MODE_P (mode))
16982     return false;
16983
16984   switch (code)
16985     {
16986     case LTU:
16987     case GEU:
16988       break;
16989
16990     /* Convert a==0 into (unsigned)a<1.  */
16991     case EQ:
16992     case NE:
16993       if (op1 != const0_rtx)
16994         return false;
16995       op1 = const1_rtx;
16996       code = (code == EQ ? LTU : GEU);
16997       break;
16998
16999     /* Convert a>b into b<a or a>=b-1.  */
17000     case GTU:
17001     case LEU:
17002       if (CONST_INT_P (op1))
17003         {
17004           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
17005           /* Bail out on overflow.  We still can swap operands but that
17006              would force loading of the constant into register.  */
17007           if (op1 == const0_rtx
17008               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
17009             return false;
17010           code = (code == GTU ? GEU : LTU);
17011         }
17012       else
17013         {
17014           rtx tmp = op1;
17015           op1 = op0;
17016           op0 = tmp;
17017           code = (code == GTU ? LTU : GEU);
17018         }
17019       break;
17020
17021     /* Convert a>=0 into (unsigned)a<0x80000000.  */
17022     case LT:
17023     case GE:
17024       if (mode == DImode || op1 != const0_rtx)
17025         return false;
17026       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17027       code = (code == LT ? GEU : LTU);
17028       break;
17029     case LE:
17030     case GT:
17031       if (mode == DImode || op1 != constm1_rtx)
17032         return false;
17033       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
17034       code = (code == LE ? GEU : LTU);
17035       break;
17036
17037     default:
17038       return false;
17039     }
17040   /* Swapping operands may cause constant to appear as first operand.  */
17041   if (!nonimmediate_operand (op0, VOIDmode))
17042     {
17043       if (!can_create_pseudo_p ())
17044         return false;
17045       op0 = force_reg (mode, op0);
17046     }
17047   *pop = ix86_expand_compare (code, op0, op1);
17048   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
17049   return true;
17050 }
17051
17052 bool
17053 ix86_expand_int_movcc (rtx operands[])
17054 {
17055   enum rtx_code code = GET_CODE (operands[1]), compare_code;
17056   rtx compare_seq, compare_op;
17057   enum machine_mode mode = GET_MODE (operands[0]);
17058   bool sign_bit_compare_p = false;
17059   rtx op0 = XEXP (operands[1], 0);
17060   rtx op1 = XEXP (operands[1], 1);
17061
17062   start_sequence ();
17063   compare_op = ix86_expand_compare (code, op0, op1);
17064   compare_seq = get_insns ();
17065   end_sequence ();
17066
17067   compare_code = GET_CODE (compare_op);
17068
17069   if ((op1 == const0_rtx && (code == GE || code == LT))
17070       || (op1 == constm1_rtx && (code == GT || code == LE)))
17071     sign_bit_compare_p = true;
17072
17073   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
17074      HImode insns, we'd be swallowed in word prefix ops.  */
17075
17076   if ((mode != HImode || TARGET_FAST_PREFIX)
17077       && (mode != (TARGET_64BIT ? TImode : DImode))
17078       && CONST_INT_P (operands[2])
17079       && CONST_INT_P (operands[3]))
17080     {
17081       rtx out = operands[0];
17082       HOST_WIDE_INT ct = INTVAL (operands[2]);
17083       HOST_WIDE_INT cf = INTVAL (operands[3]);
17084       HOST_WIDE_INT diff;
17085
17086       diff = ct - cf;
17087       /*  Sign bit compares are better done using shifts than we do by using
17088           sbb.  */
17089       if (sign_bit_compare_p
17090           || ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
17091         {
17092           /* Detect overlap between destination and compare sources.  */
17093           rtx tmp = out;
17094
17095           if (!sign_bit_compare_p)
17096             {
17097               rtx flags;
17098               bool fpcmp = false;
17099
17100               compare_code = GET_CODE (compare_op);
17101
17102               flags = XEXP (compare_op, 0);
17103
17104               if (GET_MODE (flags) == CCFPmode
17105                   || GET_MODE (flags) == CCFPUmode)
17106                 {
17107                   fpcmp = true;
17108                   compare_code
17109                     = ix86_fp_compare_code_to_integer (compare_code);
17110                 }
17111
17112               /* To simplify rest of code, restrict to the GEU case.  */
17113               if (compare_code == LTU)
17114                 {
17115                   HOST_WIDE_INT tmp = ct;
17116                   ct = cf;
17117                   cf = tmp;
17118                   compare_code = reverse_condition (compare_code);
17119                   code = reverse_condition (code);
17120                 }
17121               else
17122                 {
17123                   if (fpcmp)
17124                     PUT_CODE (compare_op,
17125                               reverse_condition_maybe_unordered
17126                                 (GET_CODE (compare_op)));
17127                   else
17128                     PUT_CODE (compare_op,
17129                               reverse_condition (GET_CODE (compare_op)));
17130                 }
17131               diff = ct - cf;
17132
17133               if (reg_overlap_mentioned_p (out, op0)
17134                   || reg_overlap_mentioned_p (out, op1))
17135                 tmp = gen_reg_rtx (mode);
17136
17137               if (mode == DImode)
17138                 emit_insn (gen_x86_movdicc_0_m1 (tmp, flags, compare_op));
17139               else
17140                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp),
17141                                                  flags, compare_op));
17142             }
17143           else
17144             {
17145               if (code == GT || code == GE)
17146                 code = reverse_condition (code);
17147               else
17148                 {
17149                   HOST_WIDE_INT tmp = ct;
17150                   ct = cf;
17151                   cf = tmp;
17152                   diff = ct - cf;
17153                 }
17154               tmp = emit_store_flag (tmp, code, op0, op1, VOIDmode, 0, -1);
17155             }
17156
17157           if (diff == 1)
17158             {
17159               /*
17160                * cmpl op0,op1
17161                * sbbl dest,dest
17162                * [addl dest, ct]
17163                *
17164                * Size 5 - 8.
17165                */
17166               if (ct)
17167                 tmp = expand_simple_binop (mode, PLUS,
17168                                            tmp, GEN_INT (ct),
17169                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17170             }
17171           else if (cf == -1)
17172             {
17173               /*
17174                * cmpl op0,op1
17175                * sbbl dest,dest
17176                * orl $ct, dest
17177                *
17178                * Size 8.
17179                */
17180               tmp = expand_simple_binop (mode, IOR,
17181                                          tmp, GEN_INT (ct),
17182                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
17183             }
17184           else if (diff == -1 && ct)
17185             {
17186               /*
17187                * cmpl op0,op1
17188                * sbbl dest,dest
17189                * notl dest
17190                * [addl dest, cf]
17191                *
17192                * Size 8 - 11.
17193                */
17194               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
17195               if (cf)
17196                 tmp = expand_simple_binop (mode, PLUS,
17197                                            copy_rtx (tmp), GEN_INT (cf),
17198                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17199             }
17200           else
17201             {
17202               /*
17203                * cmpl op0,op1
17204                * sbbl dest,dest
17205                * [notl dest]
17206                * andl cf - ct, dest
17207                * [addl dest, ct]
17208                *
17209                * Size 8 - 11.
17210                */
17211
17212               if (cf == 0)
17213                 {
17214                   cf = ct;
17215                   ct = 0;
17216                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
17217                 }
17218
17219               tmp = expand_simple_binop (mode, AND,
17220                                          copy_rtx (tmp),
17221                                          gen_int_mode (cf - ct, mode),
17222                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
17223               if (ct)
17224                 tmp = expand_simple_binop (mode, PLUS,
17225                                            copy_rtx (tmp), GEN_INT (ct),
17226                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
17227             }
17228
17229           if (!rtx_equal_p (tmp, out))
17230             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
17231
17232           return true;
17233         }
17234
17235       if (diff < 0)
17236         {
17237           enum machine_mode cmp_mode = GET_MODE (op0);
17238
17239           HOST_WIDE_INT tmp;
17240           tmp = ct, ct = cf, cf = tmp;
17241           diff = -diff;
17242
17243           if (SCALAR_FLOAT_MODE_P (cmp_mode))
17244             {
17245               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
17246
17247               /* We may be reversing unordered compare to normal compare, that
17248                  is not valid in general (we may convert non-trapping condition
17249                  to trapping one), however on i386 we currently emit all
17250                  comparisons unordered.  */
17251               compare_code = reverse_condition_maybe_unordered (compare_code);
17252               code = reverse_condition_maybe_unordered (code);
17253             }
17254           else
17255             {
17256               compare_code = reverse_condition (compare_code);
17257               code = reverse_condition (code);
17258             }
17259         }
17260
17261       compare_code = UNKNOWN;
17262       if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
17263           && CONST_INT_P (op1))
17264         {
17265           if (op1 == const0_rtx
17266               && (code == LT || code == GE))
17267             compare_code = code;
17268           else if (op1 == constm1_rtx)
17269             {
17270               if (code == LE)
17271                 compare_code = LT;
17272               else if (code == GT)
17273                 compare_code = GE;
17274             }
17275         }
17276
17277       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
17278       if (compare_code != UNKNOWN
17279           && GET_MODE (op0) == GET_MODE (out)
17280           && (cf == -1 || ct == -1))
17281         {
17282           /* If lea code below could be used, only optimize
17283              if it results in a 2 insn sequence.  */
17284
17285           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
17286                  || diff == 3 || diff == 5 || diff == 9)
17287               || (compare_code == LT && ct == -1)
17288               || (compare_code == GE && cf == -1))
17289             {
17290               /*
17291                * notl op1       (if necessary)
17292                * sarl $31, op1
17293                * orl cf, op1
17294                */
17295               if (ct != -1)
17296                 {
17297                   cf = ct;
17298                   ct = -1;
17299                   code = reverse_condition (code);
17300                 }
17301
17302               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
17303
17304               out = expand_simple_binop (mode, IOR,
17305                                          out, GEN_INT (cf),
17306                                          out, 1, OPTAB_DIRECT);
17307               if (out != operands[0])
17308                 emit_move_insn (operands[0], out);
17309
17310               return true;
17311             }
17312         }
17313
17314
17315       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
17316            || diff == 3 || diff == 5 || diff == 9)
17317           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
17318           && (mode != DImode
17319               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
17320         {
17321           /*
17322            * xorl dest,dest
17323            * cmpl op1,op2
17324            * setcc dest
17325            * lea cf(dest*(ct-cf)),dest
17326            *
17327            * Size 14.
17328            *
17329            * This also catches the degenerate setcc-only case.
17330            */
17331
17332           rtx tmp;
17333           int nops;
17334
17335           out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
17336
17337           nops = 0;
17338           /* On x86_64 the lea instruction operates on Pmode, so we need
17339              to get arithmetics done in proper mode to match.  */
17340           if (diff == 1)
17341             tmp = copy_rtx (out);
17342           else
17343             {
17344               rtx out1;
17345               out1 = copy_rtx (out);
17346               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
17347               nops++;
17348               if (diff & 1)
17349                 {
17350                   tmp = gen_rtx_PLUS (mode, tmp, out1);
17351                   nops++;
17352                 }
17353             }
17354           if (cf != 0)
17355             {
17356               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
17357               nops++;
17358             }
17359           if (!rtx_equal_p (tmp, out))
17360             {
17361               if (nops == 1)
17362                 out = force_operand (tmp, copy_rtx (out));
17363               else
17364                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
17365             }
17366           if (!rtx_equal_p (out, operands[0]))
17367             emit_move_insn (operands[0], copy_rtx (out));
17368
17369           return true;
17370         }
17371
17372       /*
17373        * General case:                  Jumpful:
17374        *   xorl dest,dest               cmpl op1, op2
17375        *   cmpl op1, op2                movl ct, dest
17376        *   setcc dest                   jcc 1f
17377        *   decl dest                    movl cf, dest
17378        *   andl (cf-ct),dest            1:
17379        *   addl ct,dest
17380        *
17381        * Size 20.                       Size 14.
17382        *
17383        * This is reasonably steep, but branch mispredict costs are
17384        * high on modern cpus, so consider failing only if optimizing
17385        * for space.
17386        */
17387
17388       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
17389           && BRANCH_COST (optimize_insn_for_speed_p (),
17390                           false) >= 2)
17391         {
17392           if (cf == 0)
17393             {
17394               enum machine_mode cmp_mode = GET_MODE (op0);
17395
17396               cf = ct;
17397               ct = 0;
17398
17399               if (SCALAR_FLOAT_MODE_P (cmp_mode))
17400                 {
17401                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
17402
17403                   /* We may be reversing unordered compare to normal compare,
17404                      that is not valid in general (we may convert non-trapping
17405                      condition to trapping one), however on i386 we currently
17406                      emit all comparisons unordered.  */
17407                   code = reverse_condition_maybe_unordered (code);
17408                 }
17409               else
17410                 {
17411                   code = reverse_condition (code);
17412                   if (compare_code != UNKNOWN)
17413                     compare_code = reverse_condition (compare_code);
17414                 }
17415             }
17416
17417           if (compare_code != UNKNOWN)
17418             {
17419               /* notl op1       (if needed)
17420                  sarl $31, op1
17421                  andl (cf-ct), op1
17422                  addl ct, op1
17423
17424                  For x < 0 (resp. x <= -1) there will be no notl,
17425                  so if possible swap the constants to get rid of the
17426                  complement.
17427                  True/false will be -1/0 while code below (store flag
17428                  followed by decrement) is 0/-1, so the constants need
17429                  to be exchanged once more.  */
17430
17431               if (compare_code == GE || !cf)
17432                 {
17433                   code = reverse_condition (code);
17434                   compare_code = LT;
17435                 }
17436               else
17437                 {
17438                   HOST_WIDE_INT tmp = cf;
17439                   cf = ct;
17440                   ct = tmp;
17441                 }
17442
17443               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, -1);
17444             }
17445           else
17446             {
17447               out = emit_store_flag (out, code, op0, op1, VOIDmode, 0, 1);
17448
17449               out = expand_simple_binop (mode, PLUS, copy_rtx (out),
17450                                          constm1_rtx,
17451                                          copy_rtx (out), 1, OPTAB_DIRECT);
17452             }
17453
17454           out = expand_simple_binop (mode, AND, copy_rtx (out),
17455                                      gen_int_mode (cf - ct, mode),
17456                                      copy_rtx (out), 1, OPTAB_DIRECT);
17457           if (ct)
17458             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
17459                                        copy_rtx (out), 1, OPTAB_DIRECT);
17460           if (!rtx_equal_p (out, operands[0]))
17461             emit_move_insn (operands[0], copy_rtx (out));
17462
17463           return true;
17464         }
17465     }
17466
17467   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
17468     {
17469       /* Try a few things more with specific constants and a variable.  */
17470
17471       optab op;
17472       rtx var, orig_out, out, tmp;
17473
17474       if (BRANCH_COST (optimize_insn_for_speed_p (), false) <= 2)
17475         return false;
17476
17477       /* If one of the two operands is an interesting constant, load a
17478          constant with the above and mask it in with a logical operation.  */
17479
17480       if (CONST_INT_P (operands[2]))
17481         {
17482           var = operands[3];
17483           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
17484             operands[3] = constm1_rtx, op = and_optab;
17485           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
17486             operands[3] = const0_rtx, op = ior_optab;
17487           else
17488             return false;
17489         }
17490       else if (CONST_INT_P (operands[3]))
17491         {
17492           var = operands[2];
17493           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
17494             operands[2] = constm1_rtx, op = and_optab;
17495           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
17496             operands[2] = const0_rtx, op = ior_optab;
17497           else
17498             return false;
17499         }
17500       else
17501         return false;
17502
17503       orig_out = operands[0];
17504       tmp = gen_reg_rtx (mode);
17505       operands[0] = tmp;
17506
17507       /* Recurse to get the constant loaded.  */
17508       if (ix86_expand_int_movcc (operands) == 0)
17509         return false;
17510
17511       /* Mask in the interesting variable.  */
17512       out = expand_binop (mode, op, var, tmp, orig_out, 0,
17513                           OPTAB_WIDEN);
17514       if (!rtx_equal_p (out, orig_out))
17515         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
17516
17517       return true;
17518     }
17519
17520   /*
17521    * For comparison with above,
17522    *
17523    * movl cf,dest
17524    * movl ct,tmp
17525    * cmpl op1,op2
17526    * cmovcc tmp,dest
17527    *
17528    * Size 15.
17529    */
17530
17531   if (! nonimmediate_operand (operands[2], mode))
17532     operands[2] = force_reg (mode, operands[2]);
17533   if (! nonimmediate_operand (operands[3], mode))
17534     operands[3] = force_reg (mode, operands[3]);
17535
17536   if (! register_operand (operands[2], VOIDmode)
17537       && (mode == QImode
17538           || ! register_operand (operands[3], VOIDmode)))
17539     operands[2] = force_reg (mode, operands[2]);
17540
17541   if (mode == QImode
17542       && ! register_operand (operands[3], VOIDmode))
17543     operands[3] = force_reg (mode, operands[3]);
17544
17545   emit_insn (compare_seq);
17546   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
17547                           gen_rtx_IF_THEN_ELSE (mode,
17548                                                 compare_op, operands[2],
17549                                                 operands[3])));
17550   return true;
17551 }
17552
17553 /* Swap, force into registers, or otherwise massage the two operands
17554    to an sse comparison with a mask result.  Thus we differ a bit from
17555    ix86_prepare_fp_compare_args which expects to produce a flags result.
17556
17557    The DEST operand exists to help determine whether to commute commutative
17558    operators.  The POP0/POP1 operands are updated in place.  The new
17559    comparison code is returned, or UNKNOWN if not implementable.  */
17560
17561 static enum rtx_code
17562 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
17563                                   rtx *pop0, rtx *pop1)
17564 {
17565   rtx tmp;
17566
17567   switch (code)
17568     {
17569     case LTGT:
17570     case UNEQ:
17571       /* We have no LTGT as an operator.  We could implement it with
17572          NE & ORDERED, but this requires an extra temporary.  It's
17573          not clear that it's worth it.  */
17574       return UNKNOWN;
17575
17576     case LT:
17577     case LE:
17578     case UNGT:
17579     case UNGE:
17580       /* These are supported directly.  */
17581       break;
17582
17583     case EQ:
17584     case NE:
17585     case UNORDERED:
17586     case ORDERED:
17587       /* For commutative operators, try to canonicalize the destination
17588          operand to be first in the comparison - this helps reload to
17589          avoid extra moves.  */
17590       if (!dest || !rtx_equal_p (dest, *pop1))
17591         break;
17592       /* FALLTHRU */
17593
17594     case GE:
17595     case GT:
17596     case UNLE:
17597     case UNLT:
17598       /* These are not supported directly.  Swap the comparison operands
17599          to transform into something that is supported.  */
17600       tmp = *pop0;
17601       *pop0 = *pop1;
17602       *pop1 = tmp;
17603       code = swap_condition (code);
17604       break;
17605
17606     default:
17607       gcc_unreachable ();
17608     }
17609
17610   return code;
17611 }
17612
17613 /* Detect conditional moves that exactly match min/max operational
17614    semantics.  Note that this is IEEE safe, as long as we don't
17615    interchange the operands.
17616
17617    Returns FALSE if this conditional move doesn't match a MIN/MAX,
17618    and TRUE if the operation is successful and instructions are emitted.  */
17619
17620 static bool
17621 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
17622                            rtx cmp_op1, rtx if_true, rtx if_false)
17623 {
17624   enum machine_mode mode;
17625   bool is_min;
17626   rtx tmp;
17627
17628   if (code == LT)
17629     ;
17630   else if (code == UNGE)
17631     {
17632       tmp = if_true;
17633       if_true = if_false;
17634       if_false = tmp;
17635     }
17636   else
17637     return false;
17638
17639   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
17640     is_min = true;
17641   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
17642     is_min = false;
17643   else
17644     return false;
17645
17646   mode = GET_MODE (dest);
17647
17648   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
17649      but MODE may be a vector mode and thus not appropriate.  */
17650   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
17651     {
17652       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
17653       rtvec v;
17654
17655       if_true = force_reg (mode, if_true);
17656       v = gen_rtvec (2, if_true, if_false);
17657       tmp = gen_rtx_UNSPEC (mode, v, u);
17658     }
17659   else
17660     {
17661       code = is_min ? SMIN : SMAX;
17662       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
17663     }
17664
17665   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
17666   return true;
17667 }
17668
17669 /* Expand an sse vector comparison.  Return the register with the result.  */
17670
17671 static rtx
17672 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
17673                      rtx op_true, rtx op_false)
17674 {
17675   enum machine_mode mode = GET_MODE (dest);
17676   rtx x;
17677
17678   cmp_op0 = force_reg (mode, cmp_op0);
17679   if (!nonimmediate_operand (cmp_op1, mode))
17680     cmp_op1 = force_reg (mode, cmp_op1);
17681
17682   if (optimize
17683       || reg_overlap_mentioned_p (dest, op_true)
17684       || reg_overlap_mentioned_p (dest, op_false))
17685     dest = gen_reg_rtx (mode);
17686
17687   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
17688   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17689
17690   return dest;
17691 }
17692
17693 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
17694    operations.  This is used for both scalar and vector conditional moves.  */
17695
17696 static void
17697 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
17698 {
17699   enum machine_mode mode = GET_MODE (dest);
17700   rtx t2, t3, x;
17701
17702   if (op_false == CONST0_RTX (mode))
17703     {
17704       op_true = force_reg (mode, op_true);
17705       x = gen_rtx_AND (mode, cmp, op_true);
17706       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17707     }
17708   else if (op_true == CONST0_RTX (mode))
17709     {
17710       op_false = force_reg (mode, op_false);
17711       x = gen_rtx_NOT (mode, cmp);
17712       x = gen_rtx_AND (mode, x, op_false);
17713       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17714     }
17715   else if (TARGET_XOP)
17716     {
17717       rtx pcmov = gen_rtx_SET (mode, dest,
17718                                gen_rtx_IF_THEN_ELSE (mode, cmp,
17719                                                      op_true,
17720                                                      op_false));
17721       emit_insn (pcmov);
17722     }
17723   else
17724     {
17725       op_true = force_reg (mode, op_true);
17726       op_false = force_reg (mode, op_false);
17727
17728       t2 = gen_reg_rtx (mode);
17729       if (optimize)
17730         t3 = gen_reg_rtx (mode);
17731       else
17732         t3 = dest;
17733
17734       x = gen_rtx_AND (mode, op_true, cmp);
17735       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
17736
17737       x = gen_rtx_NOT (mode, cmp);
17738       x = gen_rtx_AND (mode, x, op_false);
17739       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
17740
17741       x = gen_rtx_IOR (mode, t3, t2);
17742       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
17743     }
17744 }
17745
17746 /* Expand a floating-point conditional move.  Return true if successful.  */
17747
17748 bool
17749 ix86_expand_fp_movcc (rtx operands[])
17750 {
17751   enum machine_mode mode = GET_MODE (operands[0]);
17752   enum rtx_code code = GET_CODE (operands[1]);
17753   rtx tmp, compare_op;
17754   rtx op0 = XEXP (operands[1], 0);
17755   rtx op1 = XEXP (operands[1], 1);
17756
17757   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
17758     {
17759       enum machine_mode cmode;
17760
17761       /* Since we've no cmove for sse registers, don't force bad register
17762          allocation just to gain access to it.  Deny movcc when the
17763          comparison mode doesn't match the move mode.  */
17764       cmode = GET_MODE (op0);
17765       if (cmode == VOIDmode)
17766         cmode = GET_MODE (op1);
17767       if (cmode != mode)
17768         return false;
17769
17770       code = ix86_prepare_sse_fp_compare_args (operands[0], code, &op0, &op1);
17771       if (code == UNKNOWN)
17772         return false;
17773
17774       if (ix86_expand_sse_fp_minmax (operands[0], code, op0, op1,
17775                                      operands[2], operands[3]))
17776         return true;
17777
17778       tmp = ix86_expand_sse_cmp (operands[0], code, op0, op1,
17779                                  operands[2], operands[3]);
17780       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
17781       return true;
17782     }
17783
17784   /* The floating point conditional move instructions don't directly
17785      support conditions resulting from a signed integer comparison.  */
17786
17787   compare_op = ix86_expand_compare (code, op0, op1);
17788   if (!fcmov_comparison_operator (compare_op, VOIDmode))
17789     {
17790       tmp = gen_reg_rtx (QImode);
17791       ix86_expand_setcc (tmp, code, op0, op1);
17792
17793       compare_op = ix86_expand_compare (NE, tmp, const0_rtx);
17794     }
17795
17796   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
17797                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
17798                                                 operands[2], operands[3])));
17799
17800   return true;
17801 }
17802
17803 /* Expand a floating-point vector conditional move; a vcond operation
17804    rather than a movcc operation.  */
17805
17806 bool
17807 ix86_expand_fp_vcond (rtx operands[])
17808 {
17809   enum rtx_code code = GET_CODE (operands[3]);
17810   rtx cmp;
17811
17812   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
17813                                            &operands[4], &operands[5]);
17814   if (code == UNKNOWN)
17815     return false;
17816
17817   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
17818                                  operands[5], operands[1], operands[2]))
17819     return true;
17820
17821   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
17822                              operands[1], operands[2]);
17823   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
17824   return true;
17825 }
17826
17827 /* Expand a signed/unsigned integral vector conditional move.  */
17828
17829 bool
17830 ix86_expand_int_vcond (rtx operands[])
17831 {
17832   enum machine_mode mode = GET_MODE (operands[0]);
17833   enum rtx_code code = GET_CODE (operands[3]);
17834   bool negate = false;
17835   rtx x, cop0, cop1;
17836
17837   cop0 = operands[4];
17838   cop1 = operands[5];
17839
17840   /* XOP supports all of the comparisons on all vector int types.  */
17841   if (!TARGET_XOP)
17842     {
17843       /* Canonicalize the comparison to EQ, GT, GTU.  */
17844       switch (code)
17845         {
17846         case EQ:
17847         case GT:
17848         case GTU:
17849           break;
17850
17851         case NE:
17852         case LE:
17853         case LEU:
17854           code = reverse_condition (code);
17855           negate = true;
17856           break;
17857
17858         case GE:
17859         case GEU:
17860           code = reverse_condition (code);
17861           negate = true;
17862           /* FALLTHRU */
17863
17864         case LT:
17865         case LTU:
17866           code = swap_condition (code);
17867           x = cop0, cop0 = cop1, cop1 = x;
17868           break;
17869
17870         default:
17871           gcc_unreachable ();
17872         }
17873
17874       /* Only SSE4.1/SSE4.2 supports V2DImode.  */
17875       if (mode == V2DImode)
17876         {
17877           switch (code)
17878             {
17879             case EQ:
17880               /* SSE4.1 supports EQ.  */
17881               if (!TARGET_SSE4_1)
17882                 return false;
17883               break;
17884
17885             case GT:
17886             case GTU:
17887               /* SSE4.2 supports GT/GTU.  */
17888               if (!TARGET_SSE4_2)
17889                 return false;
17890               break;
17891
17892             default:
17893               gcc_unreachable ();
17894             }
17895         }
17896
17897       /* Unsigned parallel compare is not supported by the hardware.
17898          Play some tricks to turn this into a signed comparison
17899          against 0.  */
17900       if (code == GTU)
17901         {
17902           cop0 = force_reg (mode, cop0);
17903
17904           switch (mode)
17905             {
17906             case V4SImode:
17907             case V2DImode:
17908                 {
17909                   rtx t1, t2, mask;
17910                   rtx (*gen_sub3) (rtx, rtx, rtx);
17911
17912                   /* Subtract (-(INT MAX) - 1) from both operands to make
17913                      them signed.  */
17914                   mask = ix86_build_signbit_mask (mode, true, false);
17915                   gen_sub3 = (mode == V4SImode
17916                               ? gen_subv4si3 : gen_subv2di3);
17917                   t1 = gen_reg_rtx (mode);
17918                   emit_insn (gen_sub3 (t1, cop0, mask));
17919
17920                   t2 = gen_reg_rtx (mode);
17921                   emit_insn (gen_sub3 (t2, cop1, mask));
17922
17923                   cop0 = t1;
17924                   cop1 = t2;
17925                   code = GT;
17926                 }
17927               break;
17928
17929             case V16QImode:
17930             case V8HImode:
17931               /* Perform a parallel unsigned saturating subtraction.  */
17932               x = gen_reg_rtx (mode);
17933               emit_insn (gen_rtx_SET (VOIDmode, x,
17934                                       gen_rtx_US_MINUS (mode, cop0, cop1)));
17935
17936               cop0 = x;
17937               cop1 = CONST0_RTX (mode);
17938               code = EQ;
17939               negate = !negate;
17940               break;
17941
17942             default:
17943               gcc_unreachable ();
17944             }
17945         }
17946     }
17947
17948   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
17949                            operands[1+negate], operands[2-negate]);
17950
17951   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
17952                          operands[2-negate]);
17953   return true;
17954 }
17955
17956 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
17957    true if we should do zero extension, else sign extension.  HIGH_P is
17958    true if we want the N/2 high elements, else the low elements.  */
17959
17960 void
17961 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
17962 {
17963   enum machine_mode imode = GET_MODE (operands[1]);
17964   rtx (*unpack)(rtx, rtx, rtx);
17965   rtx se, dest;
17966
17967   switch (imode)
17968     {
17969     case V16QImode:
17970       if (high_p)
17971         unpack = gen_vec_interleave_highv16qi;
17972       else
17973         unpack = gen_vec_interleave_lowv16qi;
17974       break;
17975     case V8HImode:
17976       if (high_p)
17977         unpack = gen_vec_interleave_highv8hi;
17978       else
17979         unpack = gen_vec_interleave_lowv8hi;
17980       break;
17981     case V4SImode:
17982       if (high_p)
17983         unpack = gen_vec_interleave_highv4si;
17984       else
17985         unpack = gen_vec_interleave_lowv4si;
17986       break;
17987     default:
17988       gcc_unreachable ();
17989     }
17990
17991   dest = gen_lowpart (imode, operands[0]);
17992
17993   if (unsigned_p)
17994     se = force_reg (imode, CONST0_RTX (imode));
17995   else
17996     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
17997                               operands[1], pc_rtx, pc_rtx);
17998
17999   emit_insn (unpack (dest, operands[1], se));
18000 }
18001
18002 /* This function performs the same task as ix86_expand_sse_unpack,
18003    but with SSE4.1 instructions.  */
18004
18005 void
18006 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
18007 {
18008   enum machine_mode imode = GET_MODE (operands[1]);
18009   rtx (*unpack)(rtx, rtx);
18010   rtx src, dest;
18011
18012   switch (imode)
18013     {
18014     case V16QImode:
18015       if (unsigned_p)
18016         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
18017       else
18018         unpack = gen_sse4_1_sign_extendv8qiv8hi2;
18019       break;
18020     case V8HImode:
18021       if (unsigned_p)
18022         unpack = gen_sse4_1_zero_extendv4hiv4si2;
18023       else
18024         unpack = gen_sse4_1_sign_extendv4hiv4si2;
18025       break;
18026     case V4SImode:
18027       if (unsigned_p)
18028         unpack = gen_sse4_1_zero_extendv2siv2di2;
18029       else
18030         unpack = gen_sse4_1_sign_extendv2siv2di2;
18031       break;
18032     default:
18033       gcc_unreachable ();
18034     }
18035
18036   dest = operands[0];
18037   if (high_p)
18038     {
18039       /* Shift higher 8 bytes to lower 8 bytes.  */
18040       src = gen_reg_rtx (imode);
18041       emit_insn (gen_sse2_lshrv1ti3 (gen_lowpart (V1TImode, src),
18042                                      gen_lowpart (V1TImode, operands[1]),
18043                                      GEN_INT (64)));
18044     }
18045   else
18046     src = operands[1];
18047
18048   emit_insn (unpack (dest, src));
18049 }
18050
18051 /* Expand conditional increment or decrement using adb/sbb instructions.
18052    The default case using setcc followed by the conditional move can be
18053    done by generic code.  */
18054 bool
18055 ix86_expand_int_addcc (rtx operands[])
18056 {
18057   enum rtx_code code = GET_CODE (operands[1]);
18058   rtx flags;
18059   rtx (*insn)(rtx, rtx, rtx, rtx, rtx);
18060   rtx compare_op;
18061   rtx val = const0_rtx;
18062   bool fpcmp = false;
18063   enum machine_mode mode;
18064   rtx op0 = XEXP (operands[1], 0);
18065   rtx op1 = XEXP (operands[1], 1);
18066
18067   if (operands[3] != const1_rtx
18068       && operands[3] != constm1_rtx)
18069     return false;
18070   if (!ix86_expand_carry_flag_compare (code, op0, op1, &compare_op))
18071      return false;
18072   code = GET_CODE (compare_op);
18073
18074   flags = XEXP (compare_op, 0);
18075
18076   if (GET_MODE (flags) == CCFPmode
18077       || GET_MODE (flags) == CCFPUmode)
18078     {
18079       fpcmp = true;
18080       code = ix86_fp_compare_code_to_integer (code);
18081     }
18082
18083   if (code != LTU)
18084     {
18085       val = constm1_rtx;
18086       if (fpcmp)
18087         PUT_CODE (compare_op,
18088                   reverse_condition_maybe_unordered
18089                     (GET_CODE (compare_op)));
18090       else
18091         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
18092     }
18093
18094   mode = GET_MODE (operands[0]);
18095
18096   /* Construct either adc or sbb insn.  */
18097   if ((code == LTU) == (operands[3] == constm1_rtx))
18098     {
18099       switch (mode)
18100         {
18101           case QImode:
18102             insn = gen_subqi3_carry;
18103             break;
18104           case HImode:
18105             insn = gen_subhi3_carry;
18106             break;
18107           case SImode:
18108             insn = gen_subsi3_carry;
18109             break;
18110           case DImode:
18111             insn = gen_subdi3_carry;
18112             break;
18113           default:
18114             gcc_unreachable ();
18115         }
18116     }
18117   else
18118     {
18119       switch (mode)
18120         {
18121           case QImode:
18122             insn = gen_addqi3_carry;
18123             break;
18124           case HImode:
18125             insn = gen_addhi3_carry;
18126             break;
18127           case SImode:
18128             insn = gen_addsi3_carry;
18129             break;
18130           case DImode:
18131             insn = gen_adddi3_carry;
18132             break;
18133           default:
18134             gcc_unreachable ();
18135         }
18136     }
18137   emit_insn (insn (operands[0], operands[2], val, flags, compare_op));
18138
18139   return true;
18140 }
18141
18142
18143 /* Split operands 0 and 1 into half-mode parts.  Similar to split_double_mode,
18144    but works for floating pointer parameters and nonoffsetable memories.
18145    For pushes, it returns just stack offsets; the values will be saved
18146    in the right order.  Maximally three parts are generated.  */
18147
18148 static int
18149 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
18150 {
18151   int size;
18152
18153   if (!TARGET_64BIT)
18154     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
18155   else
18156     size = (GET_MODE_SIZE (mode) + 4) / 8;
18157
18158   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
18159   gcc_assert (size >= 2 && size <= 4);
18160
18161   /* Optimize constant pool reference to immediates.  This is used by fp
18162      moves, that force all constants to memory to allow combining.  */
18163   if (MEM_P (operand) && MEM_READONLY_P (operand))
18164     {
18165       rtx tmp = maybe_get_pool_constant (operand);
18166       if (tmp)
18167         operand = tmp;
18168     }
18169
18170   if (MEM_P (operand) && !offsettable_memref_p (operand))
18171     {
18172       /* The only non-offsetable memories we handle are pushes.  */
18173       int ok = push_operand (operand, VOIDmode);
18174
18175       gcc_assert (ok);
18176
18177       operand = copy_rtx (operand);
18178       PUT_MODE (operand, Pmode);
18179       parts[0] = parts[1] = parts[2] = parts[3] = operand;
18180       return size;
18181     }
18182
18183   if (GET_CODE (operand) == CONST_VECTOR)
18184     {
18185       enum machine_mode imode = int_mode_for_mode (mode);
18186       /* Caution: if we looked through a constant pool memory above,
18187          the operand may actually have a different mode now.  That's
18188          ok, since we want to pun this all the way back to an integer.  */
18189       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
18190       gcc_assert (operand != NULL);
18191       mode = imode;
18192     }
18193
18194   if (!TARGET_64BIT)
18195     {
18196       if (mode == DImode)
18197         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
18198       else
18199         {
18200           int i;
18201
18202           if (REG_P (operand))
18203             {
18204               gcc_assert (reload_completed);
18205               for (i = 0; i < size; i++)
18206                 parts[i] = gen_rtx_REG (SImode, REGNO (operand) + i);
18207             }
18208           else if (offsettable_memref_p (operand))
18209             {
18210               operand = adjust_address (operand, SImode, 0);
18211               parts[0] = operand;
18212               for (i = 1; i < size; i++)
18213                 parts[i] = adjust_address (operand, SImode, 4 * i);
18214             }
18215           else if (GET_CODE (operand) == CONST_DOUBLE)
18216             {
18217               REAL_VALUE_TYPE r;
18218               long l[4];
18219
18220               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
18221               switch (mode)
18222                 {
18223                 case TFmode:
18224                   real_to_target (l, &r, mode);
18225                   parts[3] = gen_int_mode (l[3], SImode);
18226                   parts[2] = gen_int_mode (l[2], SImode);
18227                   break;
18228                 case XFmode:
18229                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
18230                   parts[2] = gen_int_mode (l[2], SImode);
18231                   break;
18232                 case DFmode:
18233                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
18234                   break;
18235                 default:
18236                   gcc_unreachable ();
18237                 }
18238               parts[1] = gen_int_mode (l[1], SImode);
18239               parts[0] = gen_int_mode (l[0], SImode);
18240             }
18241           else
18242             gcc_unreachable ();
18243         }
18244     }
18245   else
18246     {
18247       if (mode == TImode)
18248         split_double_mode (mode, &operand, 1, &parts[0], &parts[1]);
18249       if (mode == XFmode || mode == TFmode)
18250         {
18251           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
18252           if (REG_P (operand))
18253             {
18254               gcc_assert (reload_completed);
18255               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
18256               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
18257             }
18258           else if (offsettable_memref_p (operand))
18259             {
18260               operand = adjust_address (operand, DImode, 0);
18261               parts[0] = operand;
18262               parts[1] = adjust_address (operand, upper_mode, 8);
18263             }
18264           else if (GET_CODE (operand) == CONST_DOUBLE)
18265             {
18266               REAL_VALUE_TYPE r;
18267               long l[4];
18268
18269               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
18270               real_to_target (l, &r, mode);
18271
18272               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
18273               if (HOST_BITS_PER_WIDE_INT >= 64)
18274                 parts[0]
18275                   = gen_int_mode
18276                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
18277                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
18278                        DImode);
18279               else
18280                 parts[0] = immed_double_const (l[0], l[1], DImode);
18281
18282               if (upper_mode == SImode)
18283                 parts[1] = gen_int_mode (l[2], SImode);
18284               else if (HOST_BITS_PER_WIDE_INT >= 64)
18285                 parts[1]
18286                   = gen_int_mode
18287                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
18288                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
18289                        DImode);
18290               else
18291                 parts[1] = immed_double_const (l[2], l[3], DImode);
18292             }
18293           else
18294             gcc_unreachable ();
18295         }
18296     }
18297
18298   return size;
18299 }
18300
18301 /* Emit insns to perform a move or push of DI, DF, XF, and TF values.
18302    Return false when normal moves are needed; true when all required
18303    insns have been emitted.  Operands 2-4 contain the input values
18304    int the correct order; operands 5-7 contain the output values.  */
18305
18306 void
18307 ix86_split_long_move (rtx operands[])
18308 {
18309   rtx part[2][4];
18310   int nparts, i, j;
18311   int push = 0;
18312   int collisions = 0;
18313   enum machine_mode mode = GET_MODE (operands[0]);
18314   bool collisionparts[4];
18315
18316   /* The DFmode expanders may ask us to move double.
18317      For 64bit target this is single move.  By hiding the fact
18318      here we simplify i386.md splitters.  */
18319   if (TARGET_64BIT && GET_MODE_SIZE (GET_MODE (operands[0])) == 8)
18320     {
18321       /* Optimize constant pool reference to immediates.  This is used by
18322          fp moves, that force all constants to memory to allow combining.  */
18323
18324       if (MEM_P (operands[1])
18325           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
18326           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
18327         operands[1] = get_pool_constant (XEXP (operands[1], 0));
18328       if (push_operand (operands[0], VOIDmode))
18329         {
18330           operands[0] = copy_rtx (operands[0]);
18331           PUT_MODE (operands[0], Pmode);
18332         }
18333       else
18334         operands[0] = gen_lowpart (DImode, operands[0]);
18335       operands[1] = gen_lowpart (DImode, operands[1]);
18336       emit_move_insn (operands[0], operands[1]);
18337       return;
18338     }
18339
18340   /* The only non-offsettable memory we handle is push.  */
18341   if (push_operand (operands[0], VOIDmode))
18342     push = 1;
18343   else
18344     gcc_assert (!MEM_P (operands[0])
18345                 || offsettable_memref_p (operands[0]));
18346
18347   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
18348   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
18349
18350   /* When emitting push, take care for source operands on the stack.  */
18351   if (push && MEM_P (operands[1])
18352       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
18353     {
18354       rtx src_base = XEXP (part[1][nparts - 1], 0);
18355
18356       /* Compensate for the stack decrement by 4.  */
18357       if (!TARGET_64BIT && nparts == 3
18358           && mode == XFmode && TARGET_128BIT_LONG_DOUBLE)
18359         src_base = plus_constant (src_base, 4);
18360
18361       /* src_base refers to the stack pointer and is
18362          automatically decreased by emitted push.  */
18363       for (i = 0; i < nparts; i++)
18364         part[1][i] = change_address (part[1][i],
18365                                      GET_MODE (part[1][i]), src_base);
18366     }
18367
18368   /* We need to do copy in the right order in case an address register
18369      of the source overlaps the destination.  */
18370   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
18371     {
18372       rtx tmp;
18373
18374       for (i = 0; i < nparts; i++)
18375         {
18376           collisionparts[i]
18377             = reg_overlap_mentioned_p (part[0][i], XEXP (part[1][0], 0));
18378           if (collisionparts[i])
18379             collisions++;
18380         }
18381
18382       /* Collision in the middle part can be handled by reordering.  */
18383       if (collisions == 1 && nparts == 3 && collisionparts [1])
18384         {
18385           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
18386           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
18387         }
18388       else if (collisions == 1
18389                && nparts == 4
18390                && (collisionparts [1] || collisionparts [2]))
18391         {
18392           if (collisionparts [1])
18393             {
18394               tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
18395               tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
18396             }
18397           else
18398             {
18399               tmp = part[0][2]; part[0][2] = part[0][3]; part[0][3] = tmp;
18400               tmp = part[1][2]; part[1][2] = part[1][3]; part[1][3] = tmp;
18401             }
18402         }
18403
18404       /* If there are more collisions, we can't handle it by reordering.
18405          Do an lea to the last part and use only one colliding move.  */
18406       else if (collisions > 1)
18407         {
18408           rtx base;
18409
18410           collisions = 1;
18411
18412           base = part[0][nparts - 1];
18413
18414           /* Handle the case when the last part isn't valid for lea.
18415              Happens in 64-bit mode storing the 12-byte XFmode.  */
18416           if (GET_MODE (base) != Pmode)
18417             base = gen_rtx_REG (Pmode, REGNO (base));
18418
18419           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
18420           part[1][0] = replace_equiv_address (part[1][0], base);
18421           for (i = 1; i < nparts; i++)
18422             {
18423               tmp = plus_constant (base, UNITS_PER_WORD * i);
18424               part[1][i] = replace_equiv_address (part[1][i], tmp);
18425             }
18426         }
18427     }
18428
18429   if (push)
18430     {
18431       if (!TARGET_64BIT)
18432         {
18433           if (nparts == 3)
18434             {
18435               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
18436                 emit_insn (gen_addsi3 (stack_pointer_rtx,
18437                                        stack_pointer_rtx, GEN_INT (-4)));
18438               emit_move_insn (part[0][2], part[1][2]);
18439             }
18440           else if (nparts == 4)
18441             {
18442               emit_move_insn (part[0][3], part[1][3]);
18443               emit_move_insn (part[0][2], part[1][2]);
18444             }
18445         }
18446       else
18447         {
18448           /* In 64bit mode we don't have 32bit push available.  In case this is
18449              register, it is OK - we will just use larger counterpart.  We also
18450              retype memory - these comes from attempt to avoid REX prefix on
18451              moving of second half of TFmode value.  */
18452           if (GET_MODE (part[1][1]) == SImode)
18453             {
18454               switch (GET_CODE (part[1][1]))
18455                 {
18456                 case MEM:
18457                   part[1][1] = adjust_address (part[1][1], DImode, 0);
18458                   break;
18459
18460                 case REG:
18461                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
18462                   break;
18463
18464                 default:
18465                   gcc_unreachable ();
18466                 }
18467
18468               if (GET_MODE (part[1][0]) == SImode)
18469                 part[1][0] = part[1][1];
18470             }
18471         }
18472       emit_move_insn (part[0][1], part[1][1]);
18473       emit_move_insn (part[0][0], part[1][0]);
18474       return;
18475     }
18476
18477   /* Choose correct order to not overwrite the source before it is copied.  */
18478   if ((REG_P (part[0][0])
18479        && REG_P (part[1][1])
18480        && (REGNO (part[0][0]) == REGNO (part[1][1])
18481            || (nparts == 3
18482                && REGNO (part[0][0]) == REGNO (part[1][2]))
18483            || (nparts == 4
18484                && REGNO (part[0][0]) == REGNO (part[1][3]))))
18485       || (collisions > 0
18486           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
18487     {
18488       for (i = 0, j = nparts - 1; i < nparts; i++, j--)
18489         {
18490           operands[2 + i] = part[0][j];
18491           operands[6 + i] = part[1][j];
18492         }
18493     }
18494   else
18495     {
18496       for (i = 0; i < nparts; i++)
18497         {
18498           operands[2 + i] = part[0][i];
18499           operands[6 + i] = part[1][i];
18500         }
18501     }
18502
18503   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
18504   if (optimize_insn_for_size_p ())
18505     {
18506       for (j = 0; j < nparts - 1; j++)
18507         if (CONST_INT_P (operands[6 + j])
18508             && operands[6 + j] != const0_rtx
18509             && REG_P (operands[2 + j]))
18510           for (i = j; i < nparts - 1; i++)
18511             if (CONST_INT_P (operands[7 + i])
18512                 && INTVAL (operands[7 + i]) == INTVAL (operands[6 + j]))
18513               operands[7 + i] = operands[2 + j];
18514     }
18515
18516   for (i = 0; i < nparts; i++)
18517     emit_move_insn (operands[2 + i], operands[6 + i]);
18518
18519   return;
18520 }
18521
18522 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
18523    left shift by a constant, either using a single shift or
18524    a sequence of add instructions.  */
18525
18526 static void
18527 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
18528 {
18529   rtx (*insn)(rtx, rtx, rtx);
18530
18531   if (count == 1
18532       || (count * ix86_cost->add <= ix86_cost->shift_const
18533           && !optimize_insn_for_size_p ()))
18534     {
18535       insn = mode == DImode ? gen_addsi3 : gen_adddi3;
18536       while (count-- > 0)
18537         emit_insn (insn (operand, operand, operand));
18538     }
18539   else
18540     {
18541       insn = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
18542       emit_insn (insn (operand, operand, GEN_INT (count)));
18543     }
18544 }
18545
18546 void
18547 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
18548 {
18549   rtx (*gen_ashl3)(rtx, rtx, rtx);
18550   rtx (*gen_shld)(rtx, rtx, rtx);
18551   int half_width = GET_MODE_BITSIZE (mode) >> 1;
18552
18553   rtx low[2], high[2];
18554   int count;
18555
18556   if (CONST_INT_P (operands[2]))
18557     {
18558       split_double_mode (mode, operands, 2, low, high);
18559       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
18560
18561       if (count >= half_width)
18562         {
18563           emit_move_insn (high[0], low[1]);
18564           emit_move_insn (low[0], const0_rtx);
18565
18566           if (count > half_width)
18567             ix86_expand_ashl_const (high[0], count - half_width, mode);
18568         }
18569       else
18570         {
18571           gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
18572
18573           if (!rtx_equal_p (operands[0], operands[1]))
18574             emit_move_insn (operands[0], operands[1]);
18575
18576           emit_insn (gen_shld (high[0], low[0], GEN_INT (count)));
18577           ix86_expand_ashl_const (low[0], count, mode);
18578         }
18579       return;
18580     }
18581
18582   split_double_mode (mode, operands, 1, low, high);
18583
18584   gen_ashl3 = mode == DImode ? gen_ashlsi3 : gen_ashldi3;
18585
18586   if (operands[1] == const1_rtx)
18587     {
18588       /* Assuming we've chosen a QImode capable registers, then 1 << N
18589          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
18590       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
18591         {
18592           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
18593
18594           ix86_expand_clear (low[0]);
18595           ix86_expand_clear (high[0]);
18596           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (half_width)));
18597
18598           d = gen_lowpart (QImode, low[0]);
18599           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
18600           s = gen_rtx_EQ (QImode, flags, const0_rtx);
18601           emit_insn (gen_rtx_SET (VOIDmode, d, s));
18602
18603           d = gen_lowpart (QImode, high[0]);
18604           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
18605           s = gen_rtx_NE (QImode, flags, const0_rtx);
18606           emit_insn (gen_rtx_SET (VOIDmode, d, s));
18607         }
18608
18609       /* Otherwise, we can get the same results by manually performing
18610          a bit extract operation on bit 5/6, and then performing the two
18611          shifts.  The two methods of getting 0/1 into low/high are exactly
18612          the same size.  Avoiding the shift in the bit extract case helps
18613          pentium4 a bit; no one else seems to care much either way.  */
18614       else
18615         {
18616           enum machine_mode half_mode;
18617           rtx (*gen_lshr3)(rtx, rtx, rtx);
18618           rtx (*gen_and3)(rtx, rtx, rtx);
18619           rtx (*gen_xor3)(rtx, rtx, rtx);
18620           HOST_WIDE_INT bits;
18621           rtx x;
18622
18623           if (mode == DImode)
18624             {
18625               half_mode = SImode;
18626               gen_lshr3 = gen_lshrsi3;
18627               gen_and3 = gen_andsi3;
18628               gen_xor3 = gen_xorsi3;
18629               bits = 5;
18630             }
18631           else
18632             {
18633               half_mode = DImode;
18634               gen_lshr3 = gen_lshrdi3;
18635               gen_and3 = gen_anddi3;
18636               gen_xor3 = gen_xordi3;
18637               bits = 6;
18638             }
18639
18640           if (TARGET_PARTIAL_REG_STALL && !optimize_insn_for_size_p ())
18641             x = gen_rtx_ZERO_EXTEND (half_mode, operands[2]);
18642           else
18643             x = gen_lowpart (half_mode, operands[2]);
18644           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
18645
18646           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (bits)));
18647           emit_insn (gen_and3 (high[0], high[0], const1_rtx));
18648           emit_move_insn (low[0], high[0]);
18649           emit_insn (gen_xor3 (low[0], low[0], const1_rtx));
18650         }
18651
18652       emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
18653       emit_insn (gen_ashl3 (high[0], high[0], operands[2]));
18654       return;
18655     }
18656
18657   if (operands[1] == constm1_rtx)
18658     {
18659       /* For -1 << N, we can avoid the shld instruction, because we
18660          know that we're shifting 0...31/63 ones into a -1.  */
18661       emit_move_insn (low[0], constm1_rtx);
18662       if (optimize_insn_for_size_p ())
18663         emit_move_insn (high[0], low[0]);
18664       else
18665         emit_move_insn (high[0], constm1_rtx);
18666     }
18667   else
18668     {
18669       gen_shld = mode == DImode ? gen_x86_shld : gen_x86_64_shld;
18670
18671       if (!rtx_equal_p (operands[0], operands[1]))
18672         emit_move_insn (operands[0], operands[1]);
18673
18674       split_double_mode (mode, operands, 1, low, high);
18675       emit_insn (gen_shld (high[0], low[0], operands[2]));
18676     }
18677
18678   emit_insn (gen_ashl3 (low[0], low[0], operands[2]));
18679
18680   if (TARGET_CMOVE && scratch)
18681     {
18682       rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
18683         = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
18684
18685       ix86_expand_clear (scratch);
18686       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
18687     }
18688   else
18689     {
18690       rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
18691         = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
18692
18693       emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
18694     }
18695 }
18696
18697 void
18698 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
18699 {
18700   rtx (*gen_ashr3)(rtx, rtx, rtx)
18701     = mode == DImode ? gen_ashrsi3 : gen_ashrdi3;
18702   rtx (*gen_shrd)(rtx, rtx, rtx);
18703   int half_width = GET_MODE_BITSIZE (mode) >> 1;
18704
18705   rtx low[2], high[2];
18706   int count;
18707
18708   if (CONST_INT_P (operands[2]))
18709     {
18710       split_double_mode (mode, operands, 2, low, high);
18711       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
18712
18713       if (count == GET_MODE_BITSIZE (mode) - 1)
18714         {
18715           emit_move_insn (high[0], high[1]);
18716           emit_insn (gen_ashr3 (high[0], high[0],
18717                                 GEN_INT (half_width - 1)));
18718           emit_move_insn (low[0], high[0]);
18719
18720         }
18721       else if (count >= half_width)
18722         {
18723           emit_move_insn (low[0], high[1]);
18724           emit_move_insn (high[0], low[0]);
18725           emit_insn (gen_ashr3 (high[0], high[0],
18726                                 GEN_INT (half_width - 1)));
18727
18728           if (count > half_width)
18729             emit_insn (gen_ashr3 (low[0], low[0],
18730                                   GEN_INT (count - half_width)));
18731         }
18732       else
18733         {
18734           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
18735
18736           if (!rtx_equal_p (operands[0], operands[1]))
18737             emit_move_insn (operands[0], operands[1]);
18738
18739           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
18740           emit_insn (gen_ashr3 (high[0], high[0], GEN_INT (count)));
18741         }
18742     }
18743   else
18744     {
18745       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
18746
18747      if (!rtx_equal_p (operands[0], operands[1]))
18748         emit_move_insn (operands[0], operands[1]);
18749
18750       split_double_mode (mode, operands, 1, low, high);
18751
18752       emit_insn (gen_shrd (low[0], high[0], operands[2]));
18753       emit_insn (gen_ashr3 (high[0], high[0], operands[2]));
18754
18755       if (TARGET_CMOVE && scratch)
18756         {
18757           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
18758             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
18759
18760           emit_move_insn (scratch, high[0]);
18761           emit_insn (gen_ashr3 (scratch, scratch,
18762                                 GEN_INT (half_width - 1)));
18763           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
18764                                           scratch));
18765         }
18766       else
18767         {
18768           rtx (*gen_x86_shift_adj_3)(rtx, rtx, rtx)
18769             = mode == DImode ? gen_x86_shiftsi_adj_3 : gen_x86_shiftdi_adj_3;
18770
18771           emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
18772         }
18773     }
18774 }
18775
18776 void
18777 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
18778 {
18779   rtx (*gen_lshr3)(rtx, rtx, rtx)
18780     = mode == DImode ? gen_lshrsi3 : gen_lshrdi3;
18781   rtx (*gen_shrd)(rtx, rtx, rtx);
18782   int half_width = GET_MODE_BITSIZE (mode) >> 1;
18783
18784   rtx low[2], high[2];
18785   int count;
18786
18787   if (CONST_INT_P (operands[2]))
18788     {
18789       split_double_mode (mode, operands, 2, low, high);
18790       count = INTVAL (operands[2]) & (GET_MODE_BITSIZE (mode) - 1);
18791
18792       if (count >= half_width)
18793         {
18794           emit_move_insn (low[0], high[1]);
18795           ix86_expand_clear (high[0]);
18796
18797           if (count > half_width)
18798             emit_insn (gen_lshr3 (low[0], low[0],
18799                                   GEN_INT (count - half_width)));
18800         }
18801       else
18802         {
18803           gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
18804
18805           if (!rtx_equal_p (operands[0], operands[1]))
18806             emit_move_insn (operands[0], operands[1]);
18807
18808           emit_insn (gen_shrd (low[0], high[0], GEN_INT (count)));
18809           emit_insn (gen_lshr3 (high[0], high[0], GEN_INT (count)));
18810         }
18811     }
18812   else
18813     {
18814       gen_shrd = mode == DImode ? gen_x86_shrd : gen_x86_64_shrd;
18815
18816       if (!rtx_equal_p (operands[0], operands[1]))
18817         emit_move_insn (operands[0], operands[1]);
18818
18819       split_double_mode (mode, operands, 1, low, high);
18820
18821       emit_insn (gen_shrd (low[0], high[0], operands[2]));
18822       emit_insn (gen_lshr3 (high[0], high[0], operands[2]));
18823
18824       if (TARGET_CMOVE && scratch)
18825         {
18826           rtx (*gen_x86_shift_adj_1)(rtx, rtx, rtx, rtx)
18827             = mode == DImode ? gen_x86_shiftsi_adj_1 : gen_x86_shiftdi_adj_1;
18828
18829           ix86_expand_clear (scratch);
18830           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
18831                                           scratch));
18832         }
18833       else
18834         {
18835           rtx (*gen_x86_shift_adj_2)(rtx, rtx, rtx)
18836             = mode == DImode ? gen_x86_shiftsi_adj_2 : gen_x86_shiftdi_adj_2;
18837
18838           emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
18839         }
18840     }
18841 }
18842
18843 /* Predict just emitted jump instruction to be taken with probability PROB.  */
18844 static void
18845 predict_jump (int prob)
18846 {
18847   rtx insn = get_last_insn ();
18848   gcc_assert (JUMP_P (insn));
18849   add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
18850 }
18851
18852 /* Helper function for the string operations below.  Dest VARIABLE whether
18853    it is aligned to VALUE bytes.  If true, jump to the label.  */
18854 static rtx
18855 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
18856 {
18857   rtx label = gen_label_rtx ();
18858   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
18859   if (GET_MODE (variable) == DImode)
18860     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
18861   else
18862     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
18863   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
18864                            1, label);
18865   if (epilogue)
18866     predict_jump (REG_BR_PROB_BASE * 50 / 100);
18867   else
18868     predict_jump (REG_BR_PROB_BASE * 90 / 100);
18869   return label;
18870 }
18871
18872 /* Adjust COUNTER by the VALUE.  */
18873 static void
18874 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
18875 {
18876   rtx (*gen_add)(rtx, rtx, rtx)
18877     = GET_MODE (countreg) == DImode ? gen_adddi3 : gen_addsi3;
18878
18879   emit_insn (gen_add (countreg, countreg, GEN_INT (-value)));
18880 }
18881
18882 /* Zero extend possibly SImode EXP to Pmode register.  */
18883 rtx
18884 ix86_zero_extend_to_Pmode (rtx exp)
18885 {
18886   rtx r;
18887   if (GET_MODE (exp) == VOIDmode)
18888     return force_reg (Pmode, exp);
18889   if (GET_MODE (exp) == Pmode)
18890     return copy_to_mode_reg (Pmode, exp);
18891   r = gen_reg_rtx (Pmode);
18892   emit_insn (gen_zero_extendsidi2 (r, exp));
18893   return r;
18894 }
18895
18896 /* Divide COUNTREG by SCALE.  */
18897 static rtx
18898 scale_counter (rtx countreg, int scale)
18899 {
18900   rtx sc;
18901
18902   if (scale == 1)
18903     return countreg;
18904   if (CONST_INT_P (countreg))
18905     return GEN_INT (INTVAL (countreg) / scale);
18906   gcc_assert (REG_P (countreg));
18907
18908   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
18909                             GEN_INT (exact_log2 (scale)),
18910                             NULL, 1, OPTAB_DIRECT);
18911   return sc;
18912 }
18913
18914 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
18915    DImode for constant loop counts.  */
18916
18917 static enum machine_mode
18918 counter_mode (rtx count_exp)
18919 {
18920   if (GET_MODE (count_exp) != VOIDmode)
18921     return GET_MODE (count_exp);
18922   if (!CONST_INT_P (count_exp))
18923     return Pmode;
18924   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
18925     return DImode;
18926   return SImode;
18927 }
18928
18929 /* When SRCPTR is non-NULL, output simple loop to move memory
18930    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
18931    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
18932    equivalent loop to set memory by VALUE (supposed to be in MODE).
18933
18934    The size is rounded down to whole number of chunk size moved at once.
18935    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
18936
18937
18938 static void
18939 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
18940                                rtx destptr, rtx srcptr, rtx value,
18941                                rtx count, enum machine_mode mode, int unroll,
18942                                int expected_size)
18943 {
18944   rtx out_label, top_label, iter, tmp;
18945   enum machine_mode iter_mode = counter_mode (count);
18946   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
18947   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
18948   rtx size;
18949   rtx x_addr;
18950   rtx y_addr;
18951   int i;
18952
18953   top_label = gen_label_rtx ();
18954   out_label = gen_label_rtx ();
18955   iter = gen_reg_rtx (iter_mode);
18956
18957   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
18958                               NULL, 1, OPTAB_DIRECT);
18959   /* Those two should combine.  */
18960   if (piece_size == const1_rtx)
18961     {
18962       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
18963                                true, out_label);
18964       predict_jump (REG_BR_PROB_BASE * 10 / 100);
18965     }
18966   emit_move_insn (iter, const0_rtx);
18967
18968   emit_label (top_label);
18969
18970   tmp = convert_modes (Pmode, iter_mode, iter, true);
18971   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
18972   destmem = change_address (destmem, mode, x_addr);
18973
18974   if (srcmem)
18975     {
18976       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
18977       srcmem = change_address (srcmem, mode, y_addr);
18978
18979       /* When unrolling for chips that reorder memory reads and writes,
18980          we can save registers by using single temporary.
18981          Also using 4 temporaries is overkill in 32bit mode.  */
18982       if (!TARGET_64BIT && 0)
18983         {
18984           for (i = 0; i < unroll; i++)
18985             {
18986               if (i)
18987                 {
18988                   destmem =
18989                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
18990                   srcmem =
18991                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
18992                 }
18993               emit_move_insn (destmem, srcmem);
18994             }
18995         }
18996       else
18997         {
18998           rtx tmpreg[4];
18999           gcc_assert (unroll <= 4);
19000           for (i = 0; i < unroll; i++)
19001             {
19002               tmpreg[i] = gen_reg_rtx (mode);
19003               if (i)
19004                 {
19005                   srcmem =
19006                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
19007                 }
19008               emit_move_insn (tmpreg[i], srcmem);
19009             }
19010           for (i = 0; i < unroll; i++)
19011             {
19012               if (i)
19013                 {
19014                   destmem =
19015                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19016                 }
19017               emit_move_insn (destmem, tmpreg[i]);
19018             }
19019         }
19020     }
19021   else
19022     for (i = 0; i < unroll; i++)
19023       {
19024         if (i)
19025           destmem =
19026             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
19027         emit_move_insn (destmem, value);
19028       }
19029
19030   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
19031                              true, OPTAB_LIB_WIDEN);
19032   if (tmp != iter)
19033     emit_move_insn (iter, tmp);
19034
19035   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
19036                            true, top_label);
19037   if (expected_size != -1)
19038     {
19039       expected_size /= GET_MODE_SIZE (mode) * unroll;
19040       if (expected_size == 0)
19041         predict_jump (0);
19042       else if (expected_size > REG_BR_PROB_BASE)
19043         predict_jump (REG_BR_PROB_BASE - 1);
19044       else
19045         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
19046     }
19047   else
19048     predict_jump (REG_BR_PROB_BASE * 80 / 100);
19049   iter = ix86_zero_extend_to_Pmode (iter);
19050   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
19051                              true, OPTAB_LIB_WIDEN);
19052   if (tmp != destptr)
19053     emit_move_insn (destptr, tmp);
19054   if (srcptr)
19055     {
19056       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
19057                                  true, OPTAB_LIB_WIDEN);
19058       if (tmp != srcptr)
19059         emit_move_insn (srcptr, tmp);
19060     }
19061   emit_label (out_label);
19062 }
19063
19064 /* Output "rep; mov" instruction.
19065    Arguments have same meaning as for previous function */
19066 static void
19067 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
19068                            rtx destptr, rtx srcptr,
19069                            rtx count,
19070                            enum machine_mode mode)
19071 {
19072   rtx destexp;
19073   rtx srcexp;
19074   rtx countreg;
19075
19076   /* If the size is known, it is shorter to use rep movs.  */
19077   if (mode == QImode && CONST_INT_P (count)
19078       && !(INTVAL (count) & 3))
19079     mode = SImode;
19080
19081   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
19082     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
19083   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
19084     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
19085   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
19086   if (mode != QImode)
19087     {
19088       destexp = gen_rtx_ASHIFT (Pmode, countreg,
19089                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19090       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
19091       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
19092                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19093       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
19094     }
19095   else
19096     {
19097       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
19098       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
19099     }
19100   if (CONST_INT_P (count))
19101     {
19102       count = GEN_INT (INTVAL (count)
19103                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
19104       destmem = shallow_copy_rtx (destmem);
19105       srcmem = shallow_copy_rtx (srcmem);
19106       set_mem_size (destmem, count);
19107       set_mem_size (srcmem, count);
19108     }
19109   else
19110     {
19111       if (MEM_SIZE (destmem))
19112         set_mem_size (destmem, NULL_RTX);
19113       if (MEM_SIZE (srcmem))
19114         set_mem_size (srcmem, NULL_RTX);
19115     }
19116   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
19117                           destexp, srcexp));
19118 }
19119
19120 /* Output "rep; stos" instruction.
19121    Arguments have same meaning as for previous function */
19122 static void
19123 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
19124                             rtx count, enum machine_mode mode,
19125                             rtx orig_value)
19126 {
19127   rtx destexp;
19128   rtx countreg;
19129
19130   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
19131     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
19132   value = force_reg (mode, gen_lowpart (mode, value));
19133   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
19134   if (mode != QImode)
19135     {
19136       destexp = gen_rtx_ASHIFT (Pmode, countreg,
19137                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
19138       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
19139     }
19140   else
19141     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
19142   if (orig_value == const0_rtx && CONST_INT_P (count))
19143     {
19144       count = GEN_INT (INTVAL (count)
19145                        & ~((HOST_WIDE_INT) GET_MODE_SIZE (mode) - 1));
19146       destmem = shallow_copy_rtx (destmem);
19147       set_mem_size (destmem, count);
19148     }
19149   else if (MEM_SIZE (destmem))
19150     set_mem_size (destmem, NULL_RTX);
19151   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
19152 }
19153
19154 static void
19155 emit_strmov (rtx destmem, rtx srcmem,
19156              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
19157 {
19158   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
19159   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
19160   emit_insn (gen_strmov (destptr, dest, srcptr, src));
19161 }
19162
19163 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
19164 static void
19165 expand_movmem_epilogue (rtx destmem, rtx srcmem,
19166                         rtx destptr, rtx srcptr, rtx count, int max_size)
19167 {
19168   rtx src, dest;
19169   if (CONST_INT_P (count))
19170     {
19171       HOST_WIDE_INT countval = INTVAL (count);
19172       int offset = 0;
19173
19174       if ((countval & 0x10) && max_size > 16)
19175         {
19176           if (TARGET_64BIT)
19177             {
19178               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
19179               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
19180             }
19181           else
19182             gcc_unreachable ();
19183           offset += 16;
19184         }
19185       if ((countval & 0x08) && max_size > 8)
19186         {
19187           if (TARGET_64BIT)
19188             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
19189           else
19190             {
19191               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
19192               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
19193             }
19194           offset += 8;
19195         }
19196       if ((countval & 0x04) && max_size > 4)
19197         {
19198           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
19199           offset += 4;
19200         }
19201       if ((countval & 0x02) && max_size > 2)
19202         {
19203           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
19204           offset += 2;
19205         }
19206       if ((countval & 0x01) && max_size > 1)
19207         {
19208           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
19209           offset += 1;
19210         }
19211       return;
19212     }
19213   if (max_size > 8)
19214     {
19215       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
19216                                     count, 1, OPTAB_DIRECT);
19217       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
19218                                      count, QImode, 1, 4);
19219       return;
19220     }
19221
19222   /* When there are stringops, we can cheaply increase dest and src pointers.
19223      Otherwise we save code size by maintaining offset (zero is readily
19224      available from preceding rep operation) and using x86 addressing modes.
19225    */
19226   if (TARGET_SINGLE_STRINGOP)
19227     {
19228       if (max_size > 4)
19229         {
19230           rtx label = ix86_expand_aligntest (count, 4, true);
19231           src = change_address (srcmem, SImode, srcptr);
19232           dest = change_address (destmem, SImode, destptr);
19233           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19234           emit_label (label);
19235           LABEL_NUSES (label) = 1;
19236         }
19237       if (max_size > 2)
19238         {
19239           rtx label = ix86_expand_aligntest (count, 2, true);
19240           src = change_address (srcmem, HImode, srcptr);
19241           dest = change_address (destmem, HImode, destptr);
19242           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19243           emit_label (label);
19244           LABEL_NUSES (label) = 1;
19245         }
19246       if (max_size > 1)
19247         {
19248           rtx label = ix86_expand_aligntest (count, 1, true);
19249           src = change_address (srcmem, QImode, srcptr);
19250           dest = change_address (destmem, QImode, destptr);
19251           emit_insn (gen_strmov (destptr, dest, srcptr, src));
19252           emit_label (label);
19253           LABEL_NUSES (label) = 1;
19254         }
19255     }
19256   else
19257     {
19258       rtx offset = force_reg (Pmode, const0_rtx);
19259       rtx tmp;
19260
19261       if (max_size > 4)
19262         {
19263           rtx label = ix86_expand_aligntest (count, 4, true);
19264           src = change_address (srcmem, SImode, srcptr);
19265           dest = change_address (destmem, SImode, destptr);
19266           emit_move_insn (dest, src);
19267           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
19268                                      true, OPTAB_LIB_WIDEN);
19269           if (tmp != offset)
19270             emit_move_insn (offset, tmp);
19271           emit_label (label);
19272           LABEL_NUSES (label) = 1;
19273         }
19274       if (max_size > 2)
19275         {
19276           rtx label = ix86_expand_aligntest (count, 2, true);
19277           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
19278           src = change_address (srcmem, HImode, tmp);
19279           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
19280           dest = change_address (destmem, HImode, tmp);
19281           emit_move_insn (dest, src);
19282           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
19283                                      true, OPTAB_LIB_WIDEN);
19284           if (tmp != offset)
19285             emit_move_insn (offset, tmp);
19286           emit_label (label);
19287           LABEL_NUSES (label) = 1;
19288         }
19289       if (max_size > 1)
19290         {
19291           rtx label = ix86_expand_aligntest (count, 1, true);
19292           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
19293           src = change_address (srcmem, QImode, tmp);
19294           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
19295           dest = change_address (destmem, QImode, tmp);
19296           emit_move_insn (dest, src);
19297           emit_label (label);
19298           LABEL_NUSES (label) = 1;
19299         }
19300     }
19301 }
19302
19303 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
19304 static void
19305 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
19306                                  rtx count, int max_size)
19307 {
19308   count =
19309     expand_simple_binop (counter_mode (count), AND, count,
19310                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
19311   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
19312                                  gen_lowpart (QImode, value), count, QImode,
19313                                  1, max_size / 2);
19314 }
19315
19316 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
19317 static void
19318 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
19319 {
19320   rtx dest;
19321
19322   if (CONST_INT_P (count))
19323     {
19324       HOST_WIDE_INT countval = INTVAL (count);
19325       int offset = 0;
19326
19327       if ((countval & 0x10) && max_size > 16)
19328         {
19329           if (TARGET_64BIT)
19330             {
19331               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
19332               emit_insn (gen_strset (destptr, dest, value));
19333               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
19334               emit_insn (gen_strset (destptr, dest, value));
19335             }
19336           else
19337             gcc_unreachable ();
19338           offset += 16;
19339         }
19340       if ((countval & 0x08) && max_size > 8)
19341         {
19342           if (TARGET_64BIT)
19343             {
19344               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
19345               emit_insn (gen_strset (destptr, dest, value));
19346             }
19347           else
19348             {
19349               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
19350               emit_insn (gen_strset (destptr, dest, value));
19351               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
19352               emit_insn (gen_strset (destptr, dest, value));
19353             }
19354           offset += 8;
19355         }
19356       if ((countval & 0x04) && max_size > 4)
19357         {
19358           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
19359           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
19360           offset += 4;
19361         }
19362       if ((countval & 0x02) && max_size > 2)
19363         {
19364           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
19365           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
19366           offset += 2;
19367         }
19368       if ((countval & 0x01) && max_size > 1)
19369         {
19370           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
19371           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
19372           offset += 1;
19373         }
19374       return;
19375     }
19376   if (max_size > 32)
19377     {
19378       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
19379       return;
19380     }
19381   if (max_size > 16)
19382     {
19383       rtx label = ix86_expand_aligntest (count, 16, true);
19384       if (TARGET_64BIT)
19385         {
19386           dest = change_address (destmem, DImode, destptr);
19387           emit_insn (gen_strset (destptr, dest, value));
19388           emit_insn (gen_strset (destptr, dest, value));
19389         }
19390       else
19391         {
19392           dest = change_address (destmem, SImode, destptr);
19393           emit_insn (gen_strset (destptr, dest, value));
19394           emit_insn (gen_strset (destptr, dest, value));
19395           emit_insn (gen_strset (destptr, dest, value));
19396           emit_insn (gen_strset (destptr, dest, value));
19397         }
19398       emit_label (label);
19399       LABEL_NUSES (label) = 1;
19400     }
19401   if (max_size > 8)
19402     {
19403       rtx label = ix86_expand_aligntest (count, 8, true);
19404       if (TARGET_64BIT)
19405         {
19406           dest = change_address (destmem, DImode, destptr);
19407           emit_insn (gen_strset (destptr, dest, value));
19408         }
19409       else
19410         {
19411           dest = change_address (destmem, SImode, destptr);
19412           emit_insn (gen_strset (destptr, dest, value));
19413           emit_insn (gen_strset (destptr, dest, value));
19414         }
19415       emit_label (label);
19416       LABEL_NUSES (label) = 1;
19417     }
19418   if (max_size > 4)
19419     {
19420       rtx label = ix86_expand_aligntest (count, 4, true);
19421       dest = change_address (destmem, SImode, destptr);
19422       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
19423       emit_label (label);
19424       LABEL_NUSES (label) = 1;
19425     }
19426   if (max_size > 2)
19427     {
19428       rtx label = ix86_expand_aligntest (count, 2, true);
19429       dest = change_address (destmem, HImode, destptr);
19430       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
19431       emit_label (label);
19432       LABEL_NUSES (label) = 1;
19433     }
19434   if (max_size > 1)
19435     {
19436       rtx label = ix86_expand_aligntest (count, 1, true);
19437       dest = change_address (destmem, QImode, destptr);
19438       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
19439       emit_label (label);
19440       LABEL_NUSES (label) = 1;
19441     }
19442 }
19443
19444 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
19445    DESIRED_ALIGNMENT.  */
19446 static void
19447 expand_movmem_prologue (rtx destmem, rtx srcmem,
19448                         rtx destptr, rtx srcptr, rtx count,
19449                         int align, int desired_alignment)
19450 {
19451   if (align <= 1 && desired_alignment > 1)
19452     {
19453       rtx label = ix86_expand_aligntest (destptr, 1, false);
19454       srcmem = change_address (srcmem, QImode, srcptr);
19455       destmem = change_address (destmem, QImode, destptr);
19456       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
19457       ix86_adjust_counter (count, 1);
19458       emit_label (label);
19459       LABEL_NUSES (label) = 1;
19460     }
19461   if (align <= 2 && desired_alignment > 2)
19462     {
19463       rtx label = ix86_expand_aligntest (destptr, 2, false);
19464       srcmem = change_address (srcmem, HImode, srcptr);
19465       destmem = change_address (destmem, HImode, destptr);
19466       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
19467       ix86_adjust_counter (count, 2);
19468       emit_label (label);
19469       LABEL_NUSES (label) = 1;
19470     }
19471   if (align <= 4 && desired_alignment > 4)
19472     {
19473       rtx label = ix86_expand_aligntest (destptr, 4, false);
19474       srcmem = change_address (srcmem, SImode, srcptr);
19475       destmem = change_address (destmem, SImode, destptr);
19476       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
19477       ix86_adjust_counter (count, 4);
19478       emit_label (label);
19479       LABEL_NUSES (label) = 1;
19480     }
19481   gcc_assert (desired_alignment <= 8);
19482 }
19483
19484 /* Copy enough from DST to SRC to align DST known to DESIRED_ALIGN.
19485    ALIGN_BYTES is how many bytes need to be copied.  */
19486 static rtx
19487 expand_constant_movmem_prologue (rtx dst, rtx *srcp, rtx destreg, rtx srcreg,
19488                                  int desired_align, int align_bytes)
19489 {
19490   rtx src = *srcp;
19491   rtx src_size, dst_size;
19492   int off = 0;
19493   int src_align_bytes = get_mem_align_offset (src, desired_align * BITS_PER_UNIT);
19494   if (src_align_bytes >= 0)
19495     src_align_bytes = desired_align - src_align_bytes;
19496   src_size = MEM_SIZE (src);
19497   dst_size = MEM_SIZE (dst);
19498   if (align_bytes & 1)
19499     {
19500       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
19501       src = adjust_automodify_address_nv (src, QImode, srcreg, 0);
19502       off = 1;
19503       emit_insn (gen_strmov (destreg, dst, srcreg, src));
19504     }
19505   if (align_bytes & 2)
19506     {
19507       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
19508       src = adjust_automodify_address_nv (src, HImode, srcreg, off);
19509       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
19510         set_mem_align (dst, 2 * BITS_PER_UNIT);
19511       if (src_align_bytes >= 0
19512           && (src_align_bytes & 1) == (align_bytes & 1)
19513           && MEM_ALIGN (src) < 2 * BITS_PER_UNIT)
19514         set_mem_align (src, 2 * BITS_PER_UNIT);
19515       off = 2;
19516       emit_insn (gen_strmov (destreg, dst, srcreg, src));
19517     }
19518   if (align_bytes & 4)
19519     {
19520       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
19521       src = adjust_automodify_address_nv (src, SImode, srcreg, off);
19522       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
19523         set_mem_align (dst, 4 * BITS_PER_UNIT);
19524       if (src_align_bytes >= 0)
19525         {
19526           unsigned int src_align = 0;
19527           if ((src_align_bytes & 3) == (align_bytes & 3))
19528             src_align = 4;
19529           else if ((src_align_bytes & 1) == (align_bytes & 1))
19530             src_align = 2;
19531           if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
19532             set_mem_align (src, src_align * BITS_PER_UNIT);
19533         }
19534       off = 4;
19535       emit_insn (gen_strmov (destreg, dst, srcreg, src));
19536     }
19537   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
19538   src = adjust_automodify_address_nv (src, BLKmode, srcreg, off);
19539   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
19540     set_mem_align (dst, desired_align * BITS_PER_UNIT);
19541   if (src_align_bytes >= 0)
19542     {
19543       unsigned int src_align = 0;
19544       if ((src_align_bytes & 7) == (align_bytes & 7))
19545         src_align = 8;
19546       else if ((src_align_bytes & 3) == (align_bytes & 3))
19547         src_align = 4;
19548       else if ((src_align_bytes & 1) == (align_bytes & 1))
19549         src_align = 2;
19550       if (src_align > (unsigned int) desired_align)
19551         src_align = desired_align;
19552       if (MEM_ALIGN (src) < src_align * BITS_PER_UNIT)
19553         set_mem_align (src, src_align * BITS_PER_UNIT);
19554     }
19555   if (dst_size)
19556     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
19557   if (src_size)
19558     set_mem_size (dst, GEN_INT (INTVAL (src_size) - align_bytes));
19559   *srcp = src;
19560   return dst;
19561 }
19562
19563 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
19564    DESIRED_ALIGNMENT.  */
19565 static void
19566 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
19567                         int align, int desired_alignment)
19568 {
19569   if (align <= 1 && desired_alignment > 1)
19570     {
19571       rtx label = ix86_expand_aligntest (destptr, 1, false);
19572       destmem = change_address (destmem, QImode, destptr);
19573       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
19574       ix86_adjust_counter (count, 1);
19575       emit_label (label);
19576       LABEL_NUSES (label) = 1;
19577     }
19578   if (align <= 2 && desired_alignment > 2)
19579     {
19580       rtx label = ix86_expand_aligntest (destptr, 2, false);
19581       destmem = change_address (destmem, HImode, destptr);
19582       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
19583       ix86_adjust_counter (count, 2);
19584       emit_label (label);
19585       LABEL_NUSES (label) = 1;
19586     }
19587   if (align <= 4 && desired_alignment > 4)
19588     {
19589       rtx label = ix86_expand_aligntest (destptr, 4, false);
19590       destmem = change_address (destmem, SImode, destptr);
19591       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
19592       ix86_adjust_counter (count, 4);
19593       emit_label (label);
19594       LABEL_NUSES (label) = 1;
19595     }
19596   gcc_assert (desired_alignment <= 8);
19597 }
19598
19599 /* Set enough from DST to align DST known to by aligned by ALIGN to
19600    DESIRED_ALIGN.  ALIGN_BYTES is how many bytes need to be stored.  */
19601 static rtx
19602 expand_constant_setmem_prologue (rtx dst, rtx destreg, rtx value,
19603                                  int desired_align, int align_bytes)
19604 {
19605   int off = 0;
19606   rtx dst_size = MEM_SIZE (dst);
19607   if (align_bytes & 1)
19608     {
19609       dst = adjust_automodify_address_nv (dst, QImode, destreg, 0);
19610       off = 1;
19611       emit_insn (gen_strset (destreg, dst,
19612                              gen_lowpart (QImode, value)));
19613     }
19614   if (align_bytes & 2)
19615     {
19616       dst = adjust_automodify_address_nv (dst, HImode, destreg, off);
19617       if (MEM_ALIGN (dst) < 2 * BITS_PER_UNIT)
19618         set_mem_align (dst, 2 * BITS_PER_UNIT);
19619       off = 2;
19620       emit_insn (gen_strset (destreg, dst,
19621                              gen_lowpart (HImode, value)));
19622     }
19623   if (align_bytes & 4)
19624     {
19625       dst = adjust_automodify_address_nv (dst, SImode, destreg, off);
19626       if (MEM_ALIGN (dst) < 4 * BITS_PER_UNIT)
19627         set_mem_align (dst, 4 * BITS_PER_UNIT);
19628       off = 4;
19629       emit_insn (gen_strset (destreg, dst,
19630                              gen_lowpart (SImode, value)));
19631     }
19632   dst = adjust_automodify_address_nv (dst, BLKmode, destreg, off);
19633   if (MEM_ALIGN (dst) < (unsigned int) desired_align * BITS_PER_UNIT)
19634     set_mem_align (dst, desired_align * BITS_PER_UNIT);
19635   if (dst_size)
19636     set_mem_size (dst, GEN_INT (INTVAL (dst_size) - align_bytes));
19637   return dst;
19638 }
19639
19640 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
19641 static enum stringop_alg
19642 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
19643             int *dynamic_check)
19644 {
19645   const struct stringop_algs * algs;
19646   bool optimize_for_speed;
19647   /* Algorithms using the rep prefix want at least edi and ecx;
19648      additionally, memset wants eax and memcpy wants esi.  Don't
19649      consider such algorithms if the user has appropriated those
19650      registers for their own purposes.  */
19651   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
19652                              || (memset
19653                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
19654
19655 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
19656                            || (alg != rep_prefix_1_byte         \
19657                                && alg != rep_prefix_4_byte      \
19658                                && alg != rep_prefix_8_byte))
19659   const struct processor_costs *cost;
19660
19661   /* Even if the string operation call is cold, we still might spend a lot
19662      of time processing large blocks.  */
19663   if (optimize_function_for_size_p (cfun)
19664       || (optimize_insn_for_size_p ()
19665           && expected_size != -1 && expected_size < 256))
19666     optimize_for_speed = false;
19667   else
19668     optimize_for_speed = true;
19669
19670   cost = optimize_for_speed ? ix86_cost : &ix86_size_cost;
19671
19672   *dynamic_check = -1;
19673   if (memset)
19674     algs = &cost->memset[TARGET_64BIT != 0];
19675   else
19676     algs = &cost->memcpy[TARGET_64BIT != 0];
19677   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
19678     return stringop_alg;
19679   /* rep; movq or rep; movl is the smallest variant.  */
19680   else if (!optimize_for_speed)
19681     {
19682       if (!count || (count & 3))
19683         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
19684       else
19685         return rep_prefix_usable ? rep_prefix_4_byte : loop;
19686     }
19687   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
19688    */
19689   else if (expected_size != -1 && expected_size < 4)
19690     return loop_1_byte;
19691   else if (expected_size != -1)
19692     {
19693       unsigned int i;
19694       enum stringop_alg alg = libcall;
19695       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
19696         {
19697           /* We get here if the algorithms that were not libcall-based
19698              were rep-prefix based and we are unable to use rep prefixes
19699              based on global register usage.  Break out of the loop and
19700              use the heuristic below.  */
19701           if (algs->size[i].max == 0)
19702             break;
19703           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
19704             {
19705               enum stringop_alg candidate = algs->size[i].alg;
19706
19707               if (candidate != libcall && ALG_USABLE_P (candidate))
19708                 alg = candidate;
19709               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
19710                  last non-libcall inline algorithm.  */
19711               if (TARGET_INLINE_ALL_STRINGOPS)
19712                 {
19713                   /* When the current size is best to be copied by a libcall,
19714                      but we are still forced to inline, run the heuristic below
19715                      that will pick code for medium sized blocks.  */
19716                   if (alg != libcall)
19717                     return alg;
19718                   break;
19719                 }
19720               else if (ALG_USABLE_P (candidate))
19721                 return candidate;
19722             }
19723         }
19724       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
19725     }
19726   /* When asked to inline the call anyway, try to pick meaningful choice.
19727      We look for maximal size of block that is faster to copy by hand and
19728      take blocks of at most of that size guessing that average size will
19729      be roughly half of the block.
19730
19731      If this turns out to be bad, we might simply specify the preferred
19732      choice in ix86_costs.  */
19733   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
19734       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
19735     {
19736       int max = -1;
19737       enum stringop_alg alg;
19738       int i;
19739       bool any_alg_usable_p = true;
19740
19741       for (i = 0; i < MAX_STRINGOP_ALGS; i++)
19742         {
19743           enum stringop_alg candidate = algs->size[i].alg;
19744           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
19745
19746           if (candidate != libcall && candidate
19747               && ALG_USABLE_P (candidate))
19748               max = algs->size[i].max;
19749         }
19750       /* If there aren't any usable algorithms, then recursing on
19751          smaller sizes isn't going to find anything.  Just return the
19752          simple byte-at-a-time copy loop.  */
19753       if (!any_alg_usable_p)
19754         {
19755           /* Pick something reasonable.  */
19756           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
19757             *dynamic_check = 128;
19758           return loop_1_byte;
19759         }
19760       if (max == -1)
19761         max = 4096;
19762       alg = decide_alg (count, max / 2, memset, dynamic_check);
19763       gcc_assert (*dynamic_check == -1);
19764       gcc_assert (alg != libcall);
19765       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
19766         *dynamic_check = max;
19767       return alg;
19768     }
19769   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
19770 #undef ALG_USABLE_P
19771 }
19772
19773 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
19774    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
19775 static int
19776 decide_alignment (int align,
19777                   enum stringop_alg alg,
19778                   int expected_size)
19779 {
19780   int desired_align = 0;
19781   switch (alg)
19782     {
19783       case no_stringop:
19784         gcc_unreachable ();
19785       case loop:
19786       case unrolled_loop:
19787         desired_align = GET_MODE_SIZE (Pmode);
19788         break;
19789       case rep_prefix_8_byte:
19790         desired_align = 8;
19791         break;
19792       case rep_prefix_4_byte:
19793         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
19794            copying whole cacheline at once.  */
19795         if (TARGET_PENTIUMPRO)
19796           desired_align = 8;
19797         else
19798           desired_align = 4;
19799         break;
19800       case rep_prefix_1_byte:
19801         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
19802            copying whole cacheline at once.  */
19803         if (TARGET_PENTIUMPRO)
19804           desired_align = 8;
19805         else
19806           desired_align = 1;
19807         break;
19808       case loop_1_byte:
19809         desired_align = 1;
19810         break;
19811       case libcall:
19812         return 0;
19813     }
19814
19815   if (optimize_size)
19816     desired_align = 1;
19817   if (desired_align < align)
19818     desired_align = align;
19819   if (expected_size != -1 && expected_size < 4)
19820     desired_align = align;
19821   return desired_align;
19822 }
19823
19824 /* Return the smallest power of 2 greater than VAL.  */
19825 static int
19826 smallest_pow2_greater_than (int val)
19827 {
19828   int ret = 1;
19829   while (ret <= val)
19830     ret <<= 1;
19831   return ret;
19832 }
19833
19834 /* Expand string move (memcpy) operation.  Use i386 string operations when
19835    profitable.  expand_setmem contains similar code.  The code depends upon
19836    architecture, block size and alignment, but always has the same
19837    overall structure:
19838
19839    1) Prologue guard: Conditional that jumps up to epilogues for small
19840       blocks that can be handled by epilogue alone.  This is faster but
19841       also needed for correctness, since prologue assume the block is larger
19842       than the desired alignment.
19843
19844       Optional dynamic check for size and libcall for large
19845       blocks is emitted here too, with -minline-stringops-dynamically.
19846
19847    2) Prologue: copy first few bytes in order to get destination aligned
19848       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
19849       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
19850       We emit either a jump tree on power of two sized blocks, or a byte loop.
19851
19852    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
19853       with specified algorithm.
19854
19855    4) Epilogue: code copying tail of the block that is too small to be
19856       handled by main body (or up to size guarded by prologue guard).  */
19857
19858 bool
19859 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
19860                     rtx expected_align_exp, rtx expected_size_exp)
19861 {
19862   rtx destreg;
19863   rtx srcreg;
19864   rtx label = NULL;
19865   rtx tmp;
19866   rtx jump_around_label = NULL;
19867   HOST_WIDE_INT align = 1;
19868   unsigned HOST_WIDE_INT count = 0;
19869   HOST_WIDE_INT expected_size = -1;
19870   int size_needed = 0, epilogue_size_needed;
19871   int desired_align = 0, align_bytes = 0;
19872   enum stringop_alg alg;
19873   int dynamic_check;
19874   bool need_zero_guard = false;
19875
19876   if (CONST_INT_P (align_exp))
19877     align = INTVAL (align_exp);
19878   /* i386 can do misaligned access on reasonably increased cost.  */
19879   if (CONST_INT_P (expected_align_exp)
19880       && INTVAL (expected_align_exp) > align)
19881     align = INTVAL (expected_align_exp);
19882   /* ALIGN is the minimum of destination and source alignment, but we care here
19883      just about destination alignment.  */
19884   else if (MEM_ALIGN (dst) > (unsigned HOST_WIDE_INT) align * BITS_PER_UNIT)
19885     align = MEM_ALIGN (dst) / BITS_PER_UNIT;
19886
19887   if (CONST_INT_P (count_exp))
19888     count = expected_size = INTVAL (count_exp);
19889   if (CONST_INT_P (expected_size_exp) && count == 0)
19890     expected_size = INTVAL (expected_size_exp);
19891
19892   /* Make sure we don't need to care about overflow later on.  */
19893   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
19894     return false;
19895
19896   /* Step 0: Decide on preferred algorithm, desired alignment and
19897      size of chunks to be copied by main loop.  */
19898
19899   alg = decide_alg (count, expected_size, false, &dynamic_check);
19900   desired_align = decide_alignment (align, alg, expected_size);
19901
19902   if (!TARGET_ALIGN_STRINGOPS)
19903     align = desired_align;
19904
19905   if (alg == libcall)
19906     return false;
19907   gcc_assert (alg != no_stringop);
19908   if (!count)
19909     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
19910   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
19911   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
19912   switch (alg)
19913     {
19914     case libcall:
19915     case no_stringop:
19916       gcc_unreachable ();
19917     case loop:
19918       need_zero_guard = true;
19919       size_needed = GET_MODE_SIZE (Pmode);
19920       break;
19921     case unrolled_loop:
19922       need_zero_guard = true;
19923       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
19924       break;
19925     case rep_prefix_8_byte:
19926       size_needed = 8;
19927       break;
19928     case rep_prefix_4_byte:
19929       size_needed = 4;
19930       break;
19931     case rep_prefix_1_byte:
19932       size_needed = 1;
19933       break;
19934     case loop_1_byte:
19935       need_zero_guard = true;
19936       size_needed = 1;
19937       break;
19938     }
19939
19940   epilogue_size_needed = size_needed;
19941
19942   /* Step 1: Prologue guard.  */
19943
19944   /* Alignment code needs count to be in register.  */
19945   if (CONST_INT_P (count_exp) && desired_align > align)
19946     {
19947       if (INTVAL (count_exp) > desired_align
19948           && INTVAL (count_exp) > size_needed)
19949         {
19950           align_bytes
19951             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
19952           if (align_bytes <= 0)
19953             align_bytes = 0;
19954           else
19955             align_bytes = desired_align - align_bytes;
19956         }
19957       if (align_bytes == 0)
19958         count_exp = force_reg (counter_mode (count_exp), count_exp);
19959     }
19960   gcc_assert (desired_align >= 1 && align >= 1);
19961
19962   /* Ensure that alignment prologue won't copy past end of block.  */
19963   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
19964     {
19965       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
19966       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
19967          Make sure it is power of 2.  */
19968       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
19969
19970       if (count)
19971         {
19972           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
19973             {
19974               /* If main algorithm works on QImode, no epilogue is needed.
19975                  For small sizes just don't align anything.  */
19976               if (size_needed == 1)
19977                 desired_align = align;
19978               else
19979                 goto epilogue;
19980             }
19981         }
19982       else
19983         {
19984           label = gen_label_rtx ();
19985           emit_cmp_and_jump_insns (count_exp,
19986                                    GEN_INT (epilogue_size_needed),
19987                                    LTU, 0, counter_mode (count_exp), 1, label);
19988           if (expected_size == -1 || expected_size < epilogue_size_needed)
19989             predict_jump (REG_BR_PROB_BASE * 60 / 100);
19990           else
19991             predict_jump (REG_BR_PROB_BASE * 20 / 100);
19992         }
19993     }
19994
19995   /* Emit code to decide on runtime whether library call or inline should be
19996      used.  */
19997   if (dynamic_check != -1)
19998     {
19999       if (CONST_INT_P (count_exp))
20000         {
20001           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
20002             {
20003               emit_block_move_via_libcall (dst, src, count_exp, false);
20004               count_exp = const0_rtx;
20005               goto epilogue;
20006             }
20007         }
20008       else
20009         {
20010           rtx hot_label = gen_label_rtx ();
20011           jump_around_label = gen_label_rtx ();
20012           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
20013                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
20014           predict_jump (REG_BR_PROB_BASE * 90 / 100);
20015           emit_block_move_via_libcall (dst, src, count_exp, false);
20016           emit_jump (jump_around_label);
20017           emit_label (hot_label);
20018         }
20019     }
20020
20021   /* Step 2: Alignment prologue.  */
20022
20023   if (desired_align > align)
20024     {
20025       if (align_bytes == 0)
20026         {
20027           /* Except for the first move in epilogue, we no longer know
20028              constant offset in aliasing info.  It don't seems to worth
20029              the pain to maintain it for the first move, so throw away
20030              the info early.  */
20031           src = change_address (src, BLKmode, srcreg);
20032           dst = change_address (dst, BLKmode, destreg);
20033           expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
20034                                   desired_align);
20035         }
20036       else
20037         {
20038           /* If we know how many bytes need to be stored before dst is
20039              sufficiently aligned, maintain aliasing info accurately.  */
20040           dst = expand_constant_movmem_prologue (dst, &src, destreg, srcreg,
20041                                                  desired_align, align_bytes);
20042           count_exp = plus_constant (count_exp, -align_bytes);
20043           count -= align_bytes;
20044         }
20045       if (need_zero_guard
20046           && (count < (unsigned HOST_WIDE_INT) size_needed
20047               || (align_bytes == 0
20048                   && count < ((unsigned HOST_WIDE_INT) size_needed
20049                               + desired_align - align))))
20050         {
20051           /* It is possible that we copied enough so the main loop will not
20052              execute.  */
20053           gcc_assert (size_needed > 1);
20054           if (label == NULL_RTX)
20055             label = gen_label_rtx ();
20056           emit_cmp_and_jump_insns (count_exp,
20057                                    GEN_INT (size_needed),
20058                                    LTU, 0, counter_mode (count_exp), 1, label);
20059           if (expected_size == -1
20060               || expected_size < (desired_align - align) / 2 + size_needed)
20061             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20062           else
20063             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20064         }
20065     }
20066   if (label && size_needed == 1)
20067     {
20068       emit_label (label);
20069       LABEL_NUSES (label) = 1;
20070       label = NULL;
20071       epilogue_size_needed = 1;
20072     }
20073   else if (label == NULL_RTX)
20074     epilogue_size_needed = size_needed;
20075
20076   /* Step 3: Main loop.  */
20077
20078   switch (alg)
20079     {
20080     case libcall:
20081     case no_stringop:
20082       gcc_unreachable ();
20083     case loop_1_byte:
20084       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20085                                      count_exp, QImode, 1, expected_size);
20086       break;
20087     case loop:
20088       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20089                                      count_exp, Pmode, 1, expected_size);
20090       break;
20091     case unrolled_loop:
20092       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
20093          registers for 4 temporaries anyway.  */
20094       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
20095                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
20096                                      expected_size);
20097       break;
20098     case rep_prefix_8_byte:
20099       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20100                                  DImode);
20101       break;
20102     case rep_prefix_4_byte:
20103       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20104                                  SImode);
20105       break;
20106     case rep_prefix_1_byte:
20107       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
20108                                  QImode);
20109       break;
20110     }
20111   /* Adjust properly the offset of src and dest memory for aliasing.  */
20112   if (CONST_INT_P (count_exp))
20113     {
20114       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
20115                                           (count / size_needed) * size_needed);
20116       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
20117                                           (count / size_needed) * size_needed);
20118     }
20119   else
20120     {
20121       src = change_address (src, BLKmode, srcreg);
20122       dst = change_address (dst, BLKmode, destreg);
20123     }
20124
20125   /* Step 4: Epilogue to copy the remaining bytes.  */
20126  epilogue:
20127   if (label)
20128     {
20129       /* When the main loop is done, COUNT_EXP might hold original count,
20130          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
20131          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
20132          bytes. Compensate if needed.  */
20133
20134       if (size_needed < epilogue_size_needed)
20135         {
20136           tmp =
20137             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
20138                                  GEN_INT (size_needed - 1), count_exp, 1,
20139                                  OPTAB_DIRECT);
20140           if (tmp != count_exp)
20141             emit_move_insn (count_exp, tmp);
20142         }
20143       emit_label (label);
20144       LABEL_NUSES (label) = 1;
20145     }
20146
20147   if (count_exp != const0_rtx && epilogue_size_needed > 1)
20148     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
20149                             epilogue_size_needed);
20150   if (jump_around_label)
20151     emit_label (jump_around_label);
20152   return true;
20153 }
20154
20155 /* Helper function for memcpy.  For QImode value 0xXY produce
20156    0xXYXYXYXY of wide specified by MODE.  This is essentially
20157    a * 0x10101010, but we can do slightly better than
20158    synth_mult by unwinding the sequence by hand on CPUs with
20159    slow multiply.  */
20160 static rtx
20161 promote_duplicated_reg (enum machine_mode mode, rtx val)
20162 {
20163   enum machine_mode valmode = GET_MODE (val);
20164   rtx tmp;
20165   int nops = mode == DImode ? 3 : 2;
20166
20167   gcc_assert (mode == SImode || mode == DImode);
20168   if (val == const0_rtx)
20169     return copy_to_mode_reg (mode, const0_rtx);
20170   if (CONST_INT_P (val))
20171     {
20172       HOST_WIDE_INT v = INTVAL (val) & 255;
20173
20174       v |= v << 8;
20175       v |= v << 16;
20176       if (mode == DImode)
20177         v |= (v << 16) << 16;
20178       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
20179     }
20180
20181   if (valmode == VOIDmode)
20182     valmode = QImode;
20183   if (valmode != QImode)
20184     val = gen_lowpart (QImode, val);
20185   if (mode == QImode)
20186     return val;
20187   if (!TARGET_PARTIAL_REG_STALL)
20188     nops--;
20189   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
20190       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
20191       <= (ix86_cost->shift_const + ix86_cost->add) * nops
20192           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
20193     {
20194       rtx reg = convert_modes (mode, QImode, val, true);
20195       tmp = promote_duplicated_reg (mode, const1_rtx);
20196       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
20197                                   OPTAB_DIRECT);
20198     }
20199   else
20200     {
20201       rtx reg = convert_modes (mode, QImode, val, true);
20202
20203       if (!TARGET_PARTIAL_REG_STALL)
20204         if (mode == SImode)
20205           emit_insn (gen_movsi_insv_1 (reg, reg));
20206         else
20207           emit_insn (gen_movdi_insv_1 (reg, reg));
20208       else
20209         {
20210           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
20211                                      NULL, 1, OPTAB_DIRECT);
20212           reg =
20213             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20214         }
20215       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
20216                                  NULL, 1, OPTAB_DIRECT);
20217       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20218       if (mode == SImode)
20219         return reg;
20220       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
20221                                  NULL, 1, OPTAB_DIRECT);
20222       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
20223       return reg;
20224     }
20225 }
20226
20227 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
20228    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
20229    alignment from ALIGN to DESIRED_ALIGN.  */
20230 static rtx
20231 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
20232 {
20233   rtx promoted_val;
20234
20235   if (TARGET_64BIT
20236       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
20237     promoted_val = promote_duplicated_reg (DImode, val);
20238   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
20239     promoted_val = promote_duplicated_reg (SImode, val);
20240   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
20241     promoted_val = promote_duplicated_reg (HImode, val);
20242   else
20243     promoted_val = val;
20244
20245   return promoted_val;
20246 }
20247
20248 /* Expand string clear operation (bzero).  Use i386 string operations when
20249    profitable.  See expand_movmem comment for explanation of individual
20250    steps performed.  */
20251 bool
20252 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
20253                     rtx expected_align_exp, rtx expected_size_exp)
20254 {
20255   rtx destreg;
20256   rtx label = NULL;
20257   rtx tmp;
20258   rtx jump_around_label = NULL;
20259   HOST_WIDE_INT align = 1;
20260   unsigned HOST_WIDE_INT count = 0;
20261   HOST_WIDE_INT expected_size = -1;
20262   int size_needed = 0, epilogue_size_needed;
20263   int desired_align = 0, align_bytes = 0;
20264   enum stringop_alg alg;
20265   rtx promoted_val = NULL;
20266   bool force_loopy_epilogue = false;
20267   int dynamic_check;
20268   bool need_zero_guard = false;
20269
20270   if (CONST_INT_P (align_exp))
20271     align = INTVAL (align_exp);
20272   /* i386 can do misaligned access on reasonably increased cost.  */
20273   if (CONST_INT_P (expected_align_exp)
20274       && INTVAL (expected_align_exp) > align)
20275     align = INTVAL (expected_align_exp);
20276   if (CONST_INT_P (count_exp))
20277     count = expected_size = INTVAL (count_exp);
20278   if (CONST_INT_P (expected_size_exp) && count == 0)
20279     expected_size = INTVAL (expected_size_exp);
20280
20281   /* Make sure we don't need to care about overflow later on.  */
20282   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
20283     return false;
20284
20285   /* Step 0: Decide on preferred algorithm, desired alignment and
20286      size of chunks to be copied by main loop.  */
20287
20288   alg = decide_alg (count, expected_size, true, &dynamic_check);
20289   desired_align = decide_alignment (align, alg, expected_size);
20290
20291   if (!TARGET_ALIGN_STRINGOPS)
20292     align = desired_align;
20293
20294   if (alg == libcall)
20295     return false;
20296   gcc_assert (alg != no_stringop);
20297   if (!count)
20298     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
20299   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
20300   switch (alg)
20301     {
20302     case libcall:
20303     case no_stringop:
20304       gcc_unreachable ();
20305     case loop:
20306       need_zero_guard = true;
20307       size_needed = GET_MODE_SIZE (Pmode);
20308       break;
20309     case unrolled_loop:
20310       need_zero_guard = true;
20311       size_needed = GET_MODE_SIZE (Pmode) * 4;
20312       break;
20313     case rep_prefix_8_byte:
20314       size_needed = 8;
20315       break;
20316     case rep_prefix_4_byte:
20317       size_needed = 4;
20318       break;
20319     case rep_prefix_1_byte:
20320       size_needed = 1;
20321       break;
20322     case loop_1_byte:
20323       need_zero_guard = true;
20324       size_needed = 1;
20325       break;
20326     }
20327   epilogue_size_needed = size_needed;
20328
20329   /* Step 1: Prologue guard.  */
20330
20331   /* Alignment code needs count to be in register.  */
20332   if (CONST_INT_P (count_exp) && desired_align > align)
20333     {
20334       if (INTVAL (count_exp) > desired_align
20335           && INTVAL (count_exp) > size_needed)
20336         {
20337           align_bytes
20338             = get_mem_align_offset (dst, desired_align * BITS_PER_UNIT);
20339           if (align_bytes <= 0)
20340             align_bytes = 0;
20341           else
20342             align_bytes = desired_align - align_bytes;
20343         }
20344       if (align_bytes == 0)
20345         {
20346           enum machine_mode mode = SImode;
20347           if (TARGET_64BIT && (count & ~0xffffffff))
20348             mode = DImode;
20349           count_exp = force_reg (mode, count_exp);
20350         }
20351     }
20352   /* Do the cheap promotion to allow better CSE across the
20353      main loop and epilogue (ie one load of the big constant in the
20354      front of all code.  */
20355   if (CONST_INT_P (val_exp))
20356     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
20357                                                    desired_align, align);
20358   /* Ensure that alignment prologue won't copy past end of block.  */
20359   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
20360     {
20361       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
20362       /* Epilogue always copies COUNT_EXP & (EPILOGUE_SIZE_NEEDED - 1) bytes.
20363          Make sure it is power of 2.  */
20364       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
20365
20366       /* To improve performance of small blocks, we jump around the VAL
20367          promoting mode.  This mean that if the promoted VAL is not constant,
20368          we might not use it in the epilogue and have to use byte
20369          loop variant.  */
20370       if (epilogue_size_needed > 2 && !promoted_val)
20371         force_loopy_epilogue = true;
20372       if (count)
20373         {
20374           if (count < (unsigned HOST_WIDE_INT)epilogue_size_needed)
20375             {
20376               /* If main algorithm works on QImode, no epilogue is needed.
20377                  For small sizes just don't align anything.  */
20378               if (size_needed == 1)
20379                 desired_align = align;
20380               else
20381                 goto epilogue;
20382             }
20383         }
20384       else
20385         {
20386           label = gen_label_rtx ();
20387           emit_cmp_and_jump_insns (count_exp,
20388                                    GEN_INT (epilogue_size_needed),
20389                                    LTU, 0, counter_mode (count_exp), 1, label);
20390           if (expected_size == -1 || expected_size <= epilogue_size_needed)
20391             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20392           else
20393             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20394         }
20395     }
20396   if (dynamic_check != -1)
20397     {
20398       rtx hot_label = gen_label_rtx ();
20399       jump_around_label = gen_label_rtx ();
20400       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
20401                                LEU, 0, counter_mode (count_exp), 1, hot_label);
20402       predict_jump (REG_BR_PROB_BASE * 90 / 100);
20403       set_storage_via_libcall (dst, count_exp, val_exp, false);
20404       emit_jump (jump_around_label);
20405       emit_label (hot_label);
20406     }
20407
20408   /* Step 2: Alignment prologue.  */
20409
20410   /* Do the expensive promotion once we branched off the small blocks.  */
20411   if (!promoted_val)
20412     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
20413                                                    desired_align, align);
20414   gcc_assert (desired_align >= 1 && align >= 1);
20415
20416   if (desired_align > align)
20417     {
20418       if (align_bytes == 0)
20419         {
20420           /* Except for the first move in epilogue, we no longer know
20421              constant offset in aliasing info.  It don't seems to worth
20422              the pain to maintain it for the first move, so throw away
20423              the info early.  */
20424           dst = change_address (dst, BLKmode, destreg);
20425           expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
20426                                   desired_align);
20427         }
20428       else
20429         {
20430           /* If we know how many bytes need to be stored before dst is
20431              sufficiently aligned, maintain aliasing info accurately.  */
20432           dst = expand_constant_setmem_prologue (dst, destreg, promoted_val,
20433                                                  desired_align, align_bytes);
20434           count_exp = plus_constant (count_exp, -align_bytes);
20435           count -= align_bytes;
20436         }
20437       if (need_zero_guard
20438           && (count < (unsigned HOST_WIDE_INT) size_needed
20439               || (align_bytes == 0
20440                   && count < ((unsigned HOST_WIDE_INT) size_needed
20441                               + desired_align - align))))
20442         {
20443           /* It is possible that we copied enough so the main loop will not
20444              execute.  */
20445           gcc_assert (size_needed > 1);
20446           if (label == NULL_RTX)
20447             label = gen_label_rtx ();
20448           emit_cmp_and_jump_insns (count_exp,
20449                                    GEN_INT (size_needed),
20450                                    LTU, 0, counter_mode (count_exp), 1, label);
20451           if (expected_size == -1
20452               || expected_size < (desired_align - align) / 2 + size_needed)
20453             predict_jump (REG_BR_PROB_BASE * 20 / 100);
20454           else
20455             predict_jump (REG_BR_PROB_BASE * 60 / 100);
20456         }
20457     }
20458   if (label && size_needed == 1)
20459     {
20460       emit_label (label);
20461       LABEL_NUSES (label) = 1;
20462       label = NULL;
20463       promoted_val = val_exp;
20464       epilogue_size_needed = 1;
20465     }
20466   else if (label == NULL_RTX)
20467     epilogue_size_needed = size_needed;
20468
20469   /* Step 3: Main loop.  */
20470
20471   switch (alg)
20472     {
20473     case libcall:
20474     case no_stringop:
20475       gcc_unreachable ();
20476     case loop_1_byte:
20477       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
20478                                      count_exp, QImode, 1, expected_size);
20479       break;
20480     case loop:
20481       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
20482                                      count_exp, Pmode, 1, expected_size);
20483       break;
20484     case unrolled_loop:
20485       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
20486                                      count_exp, Pmode, 4, expected_size);
20487       break;
20488     case rep_prefix_8_byte:
20489       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
20490                                   DImode, val_exp);
20491       break;
20492     case rep_prefix_4_byte:
20493       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
20494                                   SImode, val_exp);
20495       break;
20496     case rep_prefix_1_byte:
20497       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
20498                                   QImode, val_exp);
20499       break;
20500     }
20501   /* Adjust properly the offset of src and dest memory for aliasing.  */
20502   if (CONST_INT_P (count_exp))
20503     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
20504                                         (count / size_needed) * size_needed);
20505   else
20506     dst = change_address (dst, BLKmode, destreg);
20507
20508   /* Step 4: Epilogue to copy the remaining bytes.  */
20509
20510   if (label)
20511     {
20512       /* When the main loop is done, COUNT_EXP might hold original count,
20513          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
20514          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
20515          bytes. Compensate if needed.  */
20516
20517       if (size_needed < epilogue_size_needed)
20518         {
20519           tmp =
20520             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
20521                                  GEN_INT (size_needed - 1), count_exp, 1,
20522                                  OPTAB_DIRECT);
20523           if (tmp != count_exp)
20524             emit_move_insn (count_exp, tmp);
20525         }
20526       emit_label (label);
20527       LABEL_NUSES (label) = 1;
20528     }
20529  epilogue:
20530   if (count_exp != const0_rtx && epilogue_size_needed > 1)
20531     {
20532       if (force_loopy_epilogue)
20533         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
20534                                          epilogue_size_needed);
20535       else
20536         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
20537                                 epilogue_size_needed);
20538     }
20539   if (jump_around_label)
20540     emit_label (jump_around_label);
20541   return true;
20542 }
20543
20544 /* Expand the appropriate insns for doing strlen if not just doing
20545    repnz; scasb
20546
20547    out = result, initialized with the start address
20548    align_rtx = alignment of the address.
20549    scratch = scratch register, initialized with the startaddress when
20550         not aligned, otherwise undefined
20551
20552    This is just the body. It needs the initializations mentioned above and
20553    some address computing at the end.  These things are done in i386.md.  */
20554
20555 static void
20556 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
20557 {
20558   int align;
20559   rtx tmp;
20560   rtx align_2_label = NULL_RTX;
20561   rtx align_3_label = NULL_RTX;
20562   rtx align_4_label = gen_label_rtx ();
20563   rtx end_0_label = gen_label_rtx ();
20564   rtx mem;
20565   rtx tmpreg = gen_reg_rtx (SImode);
20566   rtx scratch = gen_reg_rtx (SImode);
20567   rtx cmp;
20568
20569   align = 0;
20570   if (CONST_INT_P (align_rtx))
20571     align = INTVAL (align_rtx);
20572
20573   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
20574
20575   /* Is there a known alignment and is it less than 4?  */
20576   if (align < 4)
20577     {
20578       rtx scratch1 = gen_reg_rtx (Pmode);
20579       emit_move_insn (scratch1, out);
20580       /* Is there a known alignment and is it not 2? */
20581       if (align != 2)
20582         {
20583           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
20584           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
20585
20586           /* Leave just the 3 lower bits.  */
20587           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
20588                                     NULL_RTX, 0, OPTAB_WIDEN);
20589
20590           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
20591                                    Pmode, 1, align_4_label);
20592           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
20593                                    Pmode, 1, align_2_label);
20594           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
20595                                    Pmode, 1, align_3_label);
20596         }
20597       else
20598         {
20599           /* Since the alignment is 2, we have to check 2 or 0 bytes;
20600              check if is aligned to 4 - byte.  */
20601
20602           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
20603                                     NULL_RTX, 0, OPTAB_WIDEN);
20604
20605           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
20606                                    Pmode, 1, align_4_label);
20607         }
20608
20609       mem = change_address (src, QImode, out);
20610
20611       /* Now compare the bytes.  */
20612
20613       /* Compare the first n unaligned byte on a byte per byte basis.  */
20614       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
20615                                QImode, 1, end_0_label);
20616
20617       /* Increment the address.  */
20618       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
20619
20620       /* Not needed with an alignment of 2 */
20621       if (align != 2)
20622         {
20623           emit_label (align_2_label);
20624
20625           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
20626                                    end_0_label);
20627
20628           emit_insn (ix86_gen_add3 (out, out, const1_rtx));
20629
20630           emit_label (align_3_label);
20631         }
20632
20633       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
20634                                end_0_label);
20635
20636       emit_insn (ix86_gen_add3 (out, out, const1_rtx));
20637     }
20638
20639   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
20640      align this loop.  It gives only huge programs, but does not help to
20641      speed up.  */
20642   emit_label (align_4_label);
20643
20644   mem = change_address (src, SImode, out);
20645   emit_move_insn (scratch, mem);
20646   emit_insn (ix86_gen_add3 (out, out, GEN_INT (4)));
20647
20648   /* This formula yields a nonzero result iff one of the bytes is zero.
20649      This saves three branches inside loop and many cycles.  */
20650
20651   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
20652   emit_insn (gen_one_cmplsi2 (scratch, scratch));
20653   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
20654   emit_insn (gen_andsi3 (tmpreg, tmpreg,
20655                          gen_int_mode (0x80808080, SImode)));
20656   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
20657                            align_4_label);
20658
20659   if (TARGET_CMOVE)
20660     {
20661        rtx reg = gen_reg_rtx (SImode);
20662        rtx reg2 = gen_reg_rtx (Pmode);
20663        emit_move_insn (reg, tmpreg);
20664        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
20665
20666        /* If zero is not in the first two bytes, move two bytes forward.  */
20667        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
20668        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
20669        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
20670        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
20671                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
20672                                                      reg,
20673                                                      tmpreg)));
20674        /* Emit lea manually to avoid clobbering of flags.  */
20675        emit_insn (gen_rtx_SET (SImode, reg2,
20676                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
20677
20678        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
20679        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
20680        emit_insn (gen_rtx_SET (VOIDmode, out,
20681                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
20682                                                      reg2,
20683                                                      out)));
20684     }
20685   else
20686     {
20687        rtx end_2_label = gen_label_rtx ();
20688        /* Is zero in the first two bytes? */
20689
20690        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
20691        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
20692        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
20693        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
20694                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
20695                             pc_rtx);
20696        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
20697        JUMP_LABEL (tmp) = end_2_label;
20698
20699        /* Not in the first two.  Move two bytes forward.  */
20700        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
20701        emit_insn (ix86_gen_add3 (out, out, const2_rtx));
20702
20703        emit_label (end_2_label);
20704
20705     }
20706
20707   /* Avoid branch in fixing the byte.  */
20708   tmpreg = gen_lowpart (QImode, tmpreg);
20709   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
20710   tmp = gen_rtx_REG (CCmode, FLAGS_REG);
20711   cmp = gen_rtx_LTU (VOIDmode, tmp, const0_rtx);
20712   emit_insn (ix86_gen_sub3_carry (out, out, GEN_INT (3), tmp, cmp));
20713
20714   emit_label (end_0_label);
20715 }
20716
20717 /* Expand strlen.  */
20718
20719 bool
20720 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
20721 {
20722   rtx addr, scratch1, scratch2, scratch3, scratch4;
20723
20724   /* The generic case of strlen expander is long.  Avoid it's
20725      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
20726
20727   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
20728       && !TARGET_INLINE_ALL_STRINGOPS
20729       && !optimize_insn_for_size_p ()
20730       && (!CONST_INT_P (align) || INTVAL (align) < 4))
20731     return false;
20732
20733   addr = force_reg (Pmode, XEXP (src, 0));
20734   scratch1 = gen_reg_rtx (Pmode);
20735
20736   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
20737       && !optimize_insn_for_size_p ())
20738     {
20739       /* Well it seems that some optimizer does not combine a call like
20740          foo(strlen(bar), strlen(bar));
20741          when the move and the subtraction is done here.  It does calculate
20742          the length just once when these instructions are done inside of
20743          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
20744          often used and I use one fewer register for the lifetime of
20745          output_strlen_unroll() this is better.  */
20746
20747       emit_move_insn (out, addr);
20748
20749       ix86_expand_strlensi_unroll_1 (out, src, align);
20750
20751       /* strlensi_unroll_1 returns the address of the zero at the end of
20752          the string, like memchr(), so compute the length by subtracting
20753          the start address.  */
20754       emit_insn (ix86_gen_sub3 (out, out, addr));
20755     }
20756   else
20757     {
20758       rtx unspec;
20759
20760       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
20761       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
20762         return false;
20763
20764       scratch2 = gen_reg_rtx (Pmode);
20765       scratch3 = gen_reg_rtx (Pmode);
20766       scratch4 = force_reg (Pmode, constm1_rtx);
20767
20768       emit_move_insn (scratch3, addr);
20769       eoschar = force_reg (QImode, eoschar);
20770
20771       src = replace_equiv_address_nv (src, scratch3);
20772
20773       /* If .md starts supporting :P, this can be done in .md.  */
20774       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
20775                                                  scratch4), UNSPEC_SCAS);
20776       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
20777       emit_insn (ix86_gen_one_cmpl2 (scratch2, scratch1));
20778       emit_insn (ix86_gen_add3 (out, scratch2, constm1_rtx));
20779     }
20780   return true;
20781 }
20782
20783 /* For given symbol (function) construct code to compute address of it's PLT
20784    entry in large x86-64 PIC model.  */
20785 rtx
20786 construct_plt_address (rtx symbol)
20787 {
20788   rtx tmp = gen_reg_rtx (Pmode);
20789   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
20790
20791   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
20792   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
20793
20794   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
20795   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
20796   return tmp;
20797 }
20798
20799 rtx
20800 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
20801                   rtx callarg2,
20802                   rtx pop, int sibcall)
20803 {
20804   rtx use = NULL, call;
20805
20806   if (pop == const0_rtx)
20807     pop = NULL;
20808   gcc_assert (!TARGET_64BIT || !pop);
20809
20810   if (TARGET_MACHO && !TARGET_64BIT)
20811     {
20812 #if TARGET_MACHO
20813       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
20814         fnaddr = machopic_indirect_call_target (fnaddr);
20815 #endif
20816     }
20817   else
20818     {
20819       /* Static functions and indirect calls don't need the pic register.  */
20820       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
20821           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
20822           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
20823         use_reg (&use, pic_offset_table_rtx);
20824     }
20825
20826   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
20827     {
20828       rtx al = gen_rtx_REG (QImode, AX_REG);
20829       emit_move_insn (al, callarg2);
20830       use_reg (&use, al);
20831     }
20832
20833   if (ix86_cmodel == CM_LARGE_PIC
20834       && MEM_P (fnaddr)
20835       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
20836       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
20837     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
20838   else if (sibcall
20839            ? !sibcall_insn_operand (XEXP (fnaddr, 0), Pmode)
20840            : !call_insn_operand (XEXP (fnaddr, 0), Pmode))
20841     {
20842       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
20843       fnaddr = gen_rtx_MEM (QImode, fnaddr);
20844     }
20845
20846   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
20847   if (retval)
20848     call = gen_rtx_SET (VOIDmode, retval, call);
20849   if (pop)
20850     {
20851       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
20852       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
20853       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
20854     }
20855   if (TARGET_64BIT
20856       && ix86_cfun_abi () == MS_ABI
20857       && (!callarg2 || INTVAL (callarg2) != -2))
20858     {
20859       /* We need to represent that SI and DI registers are clobbered
20860          by SYSV calls.  */
20861       static int clobbered_registers[] = {
20862         XMM6_REG, XMM7_REG, XMM8_REG,
20863         XMM9_REG, XMM10_REG, XMM11_REG,
20864         XMM12_REG, XMM13_REG, XMM14_REG,
20865         XMM15_REG, SI_REG, DI_REG
20866       };
20867       unsigned int i;
20868       rtx vec[ARRAY_SIZE (clobbered_registers) + 2];
20869       rtx unspec = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx),
20870                                    UNSPEC_MS_TO_SYSV_CALL);
20871
20872       vec[0] = call;
20873       vec[1] = unspec;
20874       for (i = 0; i < ARRAY_SIZE (clobbered_registers); i++)
20875         vec[i + 2] = gen_rtx_CLOBBER (SSE_REGNO_P (clobbered_registers[i])
20876                                       ? TImode : DImode,
20877                                       gen_rtx_REG
20878                                         (SSE_REGNO_P (clobbered_registers[i])
20879                                                       ? TImode : DImode,
20880                                          clobbered_registers[i]));
20881
20882       call = gen_rtx_PARALLEL (VOIDmode,
20883                                gen_rtvec_v (ARRAY_SIZE (clobbered_registers)
20884                                + 2, vec));
20885     }
20886
20887   call = emit_call_insn (call);
20888   if (use)
20889     CALL_INSN_FUNCTION_USAGE (call) = use;
20890
20891   return call;
20892 }
20893
20894 \f
20895 /* Clear stack slot assignments remembered from previous functions.
20896    This is called from INIT_EXPANDERS once before RTL is emitted for each
20897    function.  */
20898
20899 static struct machine_function *
20900 ix86_init_machine_status (void)
20901 {
20902   struct machine_function *f;
20903
20904   f = ggc_alloc_cleared_machine_function ();
20905   f->use_fast_prologue_epilogue_nregs = -1;
20906   f->tls_descriptor_call_expanded_p = 0;
20907   f->call_abi = ix86_abi;
20908
20909   return f;
20910 }
20911
20912 /* Return a MEM corresponding to a stack slot with mode MODE.
20913    Allocate a new slot if necessary.
20914
20915    The RTL for a function can have several slots available: N is
20916    which slot to use.  */
20917
20918 rtx
20919 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
20920 {
20921   struct stack_local_entry *s;
20922
20923   gcc_assert (n < MAX_386_STACK_LOCALS);
20924
20925   /* Virtual slot is valid only before vregs are instantiated.  */
20926   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
20927
20928   for (s = ix86_stack_locals; s; s = s->next)
20929     if (s->mode == mode && s->n == n)
20930       return copy_rtx (s->rtl);
20931
20932   s = ggc_alloc_stack_local_entry ();
20933   s->n = n;
20934   s->mode = mode;
20935   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
20936
20937   s->next = ix86_stack_locals;
20938   ix86_stack_locals = s;
20939   return s->rtl;
20940 }
20941
20942 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
20943
20944 static GTY(()) rtx ix86_tls_symbol;
20945 rtx
20946 ix86_tls_get_addr (void)
20947 {
20948
20949   if (!ix86_tls_symbol)
20950     {
20951       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
20952                                             (TARGET_ANY_GNU_TLS
20953                                              && !TARGET_64BIT)
20954                                             ? "___tls_get_addr"
20955                                             : "__tls_get_addr");
20956     }
20957
20958   return ix86_tls_symbol;
20959 }
20960
20961 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
20962
20963 static GTY(()) rtx ix86_tls_module_base_symbol;
20964 rtx
20965 ix86_tls_module_base (void)
20966 {
20967
20968   if (!ix86_tls_module_base_symbol)
20969     {
20970       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
20971                                                         "_TLS_MODULE_BASE_");
20972       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
20973         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
20974     }
20975
20976   return ix86_tls_module_base_symbol;
20977 }
20978 \f
20979 /* Calculate the length of the memory address in the instruction
20980    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
20981
20982 int
20983 memory_address_length (rtx addr)
20984 {
20985   struct ix86_address parts;
20986   rtx base, index, disp;
20987   int len;
20988   int ok;
20989
20990   if (GET_CODE (addr) == PRE_DEC
20991       || GET_CODE (addr) == POST_INC
20992       || GET_CODE (addr) == PRE_MODIFY
20993       || GET_CODE (addr) == POST_MODIFY)
20994     return 0;
20995
20996   ok = ix86_decompose_address (addr, &parts);
20997   gcc_assert (ok);
20998
20999   if (parts.base && GET_CODE (parts.base) == SUBREG)
21000     parts.base = SUBREG_REG (parts.base);
21001   if (parts.index && GET_CODE (parts.index) == SUBREG)
21002     parts.index = SUBREG_REG (parts.index);
21003
21004   base = parts.base;
21005   index = parts.index;
21006   disp = parts.disp;
21007   len = 0;
21008
21009   /* Rule of thumb:
21010        - esp as the base always wants an index,
21011        - ebp as the base always wants a displacement,
21012        - r12 as the base always wants an index,
21013        - r13 as the base always wants a displacement.  */
21014
21015   /* Register Indirect.  */
21016   if (base && !index && !disp)
21017     {
21018       /* esp (for its index) and ebp (for its displacement) need
21019          the two-byte modrm form.  Similarly for r12 and r13 in 64-bit
21020          code.  */
21021       if (REG_P (addr)
21022           && (addr == arg_pointer_rtx
21023               || addr == frame_pointer_rtx
21024               || REGNO (addr) == SP_REG
21025               || REGNO (addr) == BP_REG
21026               || REGNO (addr) == R12_REG
21027               || REGNO (addr) == R13_REG))
21028         len = 1;
21029     }
21030
21031   /* Direct Addressing.  In 64-bit mode mod 00 r/m 5
21032      is not disp32, but disp32(%rip), so for disp32
21033      SIB byte is needed, unless print_operand_address
21034      optimizes it into disp32(%rip) or (%rip) is implied
21035      by UNSPEC.  */
21036   else if (disp && !base && !index)
21037     {
21038       len = 4;
21039       if (TARGET_64BIT)
21040         {
21041           rtx symbol = disp;
21042
21043           if (GET_CODE (disp) == CONST)
21044             symbol = XEXP (disp, 0);
21045           if (GET_CODE (symbol) == PLUS
21046               && CONST_INT_P (XEXP (symbol, 1)))
21047             symbol = XEXP (symbol, 0);
21048
21049           if (GET_CODE (symbol) != LABEL_REF
21050               && (GET_CODE (symbol) != SYMBOL_REF
21051                   || SYMBOL_REF_TLS_MODEL (symbol) != 0)
21052               && (GET_CODE (symbol) != UNSPEC
21053                   || (XINT (symbol, 1) != UNSPEC_GOTPCREL
21054                       && XINT (symbol, 1) != UNSPEC_GOTNTPOFF)))
21055             len += 1;
21056         }
21057     }
21058
21059   else
21060     {
21061       /* Find the length of the displacement constant.  */
21062       if (disp)
21063         {
21064           if (base && satisfies_constraint_K (disp))
21065             len = 1;
21066           else
21067             len = 4;
21068         }
21069       /* ebp always wants a displacement.  Similarly r13.  */
21070       else if (base && REG_P (base)
21071                && (REGNO (base) == BP_REG || REGNO (base) == R13_REG))
21072         len = 1;
21073
21074       /* An index requires the two-byte modrm form....  */
21075       if (index
21076           /* ...like esp (or r12), which always wants an index.  */
21077           || base == arg_pointer_rtx
21078           || base == frame_pointer_rtx
21079           || (base && REG_P (base)
21080               && (REGNO (base) == SP_REG || REGNO (base) == R12_REG)))
21081         len += 1;
21082     }
21083
21084   switch (parts.seg)
21085     {
21086     case SEG_FS:
21087     case SEG_GS:
21088       len += 1;
21089       break;
21090     default:
21091       break;
21092     }
21093
21094   return len;
21095 }
21096
21097 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
21098    is set, expect that insn have 8bit immediate alternative.  */
21099 int
21100 ix86_attr_length_immediate_default (rtx insn, int shortform)
21101 {
21102   int len = 0;
21103   int i;
21104   extract_insn_cached (insn);
21105   for (i = recog_data.n_operands - 1; i >= 0; --i)
21106     if (CONSTANT_P (recog_data.operand[i]))
21107       {
21108         enum attr_mode mode = get_attr_mode (insn);
21109
21110         gcc_assert (!len);
21111         if (shortform && CONST_INT_P (recog_data.operand[i]))
21112           {
21113             HOST_WIDE_INT ival = INTVAL (recog_data.operand[i]);
21114             switch (mode)
21115               {
21116               case MODE_QI:
21117                 len = 1;
21118                 continue;
21119               case MODE_HI:
21120                 ival = trunc_int_for_mode (ival, HImode);
21121                 break;
21122               case MODE_SI:
21123                 ival = trunc_int_for_mode (ival, SImode);
21124                 break;
21125               default:
21126                 break;
21127               }
21128             if (IN_RANGE (ival, -128, 127))
21129               {
21130                 len = 1;
21131                 continue;
21132               }
21133           }
21134         switch (mode)
21135           {
21136           case MODE_QI:
21137             len = 1;
21138             break;
21139           case MODE_HI:
21140             len = 2;
21141             break;
21142           case MODE_SI:
21143             len = 4;
21144             break;
21145           /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
21146           case MODE_DI:
21147             len = 4;
21148             break;
21149           default:
21150             fatal_insn ("unknown insn mode", insn);
21151         }
21152       }
21153   return len;
21154 }
21155 /* Compute default value for "length_address" attribute.  */
21156 int
21157 ix86_attr_length_address_default (rtx insn)
21158 {
21159   int i;
21160
21161   if (get_attr_type (insn) == TYPE_LEA)
21162     {
21163       rtx set = PATTERN (insn), addr;
21164
21165       if (GET_CODE (set) == PARALLEL)
21166         set = XVECEXP (set, 0, 0);
21167
21168       gcc_assert (GET_CODE (set) == SET);
21169
21170       addr = SET_SRC (set);
21171       if (TARGET_64BIT && get_attr_mode (insn) == MODE_SI)
21172         {
21173           if (GET_CODE (addr) == ZERO_EXTEND)
21174             addr = XEXP (addr, 0);
21175           if (GET_CODE (addr) == SUBREG)
21176             addr = SUBREG_REG (addr);
21177         }
21178
21179       return memory_address_length (addr);
21180     }
21181
21182   extract_insn_cached (insn);
21183   for (i = recog_data.n_operands - 1; i >= 0; --i)
21184     if (MEM_P (recog_data.operand[i]))
21185       {
21186         constrain_operands_cached (reload_completed);
21187         if (which_alternative != -1)
21188           {
21189             const char *constraints = recog_data.constraints[i];
21190             int alt = which_alternative;
21191
21192             while (*constraints == '=' || *constraints == '+')
21193               constraints++;
21194             while (alt-- > 0)
21195               while (*constraints++ != ',')
21196                 ;
21197             /* Skip ignored operands.  */
21198             if (*constraints == 'X')
21199               continue;
21200           }
21201         return memory_address_length (XEXP (recog_data.operand[i], 0));
21202       }
21203   return 0;
21204 }
21205
21206 /* Compute default value for "length_vex" attribute. It includes
21207    2 or 3 byte VEX prefix and 1 opcode byte.  */
21208
21209 int
21210 ix86_attr_length_vex_default (rtx insn, int has_0f_opcode,
21211                               int has_vex_w)
21212 {
21213   int i;
21214
21215   /* Only 0f opcode can use 2 byte VEX prefix and  VEX W bit uses 3
21216      byte VEX prefix.  */
21217   if (!has_0f_opcode || has_vex_w)
21218     return 3 + 1;
21219
21220  /* We can always use 2 byte VEX prefix in 32bit.  */
21221   if (!TARGET_64BIT)
21222     return 2 + 1;
21223
21224   extract_insn_cached (insn);
21225
21226   for (i = recog_data.n_operands - 1; i >= 0; --i)
21227     if (REG_P (recog_data.operand[i]))
21228       {
21229         /* REX.W bit uses 3 byte VEX prefix.  */
21230         if (GET_MODE (recog_data.operand[i]) == DImode
21231             && GENERAL_REG_P (recog_data.operand[i]))
21232           return 3 + 1;
21233       }
21234     else
21235       {
21236         /* REX.X or REX.B bits use 3 byte VEX prefix.  */
21237         if (MEM_P (recog_data.operand[i])
21238             && x86_extended_reg_mentioned_p (recog_data.operand[i]))
21239           return 3 + 1;
21240       }
21241
21242   return 2 + 1;
21243 }
21244 \f
21245 /* Return the maximum number of instructions a cpu can issue.  */
21246
21247 static int
21248 ix86_issue_rate (void)
21249 {
21250   switch (ix86_tune)
21251     {
21252     case PROCESSOR_PENTIUM:
21253     case PROCESSOR_ATOM:
21254     case PROCESSOR_K6:
21255       return 2;
21256
21257     case PROCESSOR_PENTIUMPRO:
21258     case PROCESSOR_PENTIUM4:
21259     case PROCESSOR_ATHLON:
21260     case PROCESSOR_K8:
21261     case PROCESSOR_AMDFAM10:
21262     case PROCESSOR_NOCONA:
21263     case PROCESSOR_GENERIC32:
21264     case PROCESSOR_GENERIC64:
21265     case PROCESSOR_BDVER1:
21266       return 3;
21267
21268     case PROCESSOR_CORE2:
21269       return 4;
21270
21271     default:
21272       return 1;
21273     }
21274 }
21275
21276 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
21277    by DEP_INSN and nothing set by DEP_INSN.  */
21278
21279 static int
21280 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
21281 {
21282   rtx set, set2;
21283
21284   /* Simplify the test for uninteresting insns.  */
21285   if (insn_type != TYPE_SETCC
21286       && insn_type != TYPE_ICMOV
21287       && insn_type != TYPE_FCMOV
21288       && insn_type != TYPE_IBR)
21289     return 0;
21290
21291   if ((set = single_set (dep_insn)) != 0)
21292     {
21293       set = SET_DEST (set);
21294       set2 = NULL_RTX;
21295     }
21296   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
21297            && XVECLEN (PATTERN (dep_insn), 0) == 2
21298            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
21299            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
21300     {
21301       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
21302       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
21303     }
21304   else
21305     return 0;
21306
21307   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
21308     return 0;
21309
21310   /* This test is true if the dependent insn reads the flags but
21311      not any other potentially set register.  */
21312   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
21313     return 0;
21314
21315   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
21316     return 0;
21317
21318   return 1;
21319 }
21320
21321 /* Return true iff USE_INSN has a memory address with operands set by
21322    SET_INSN.  */
21323
21324 bool
21325 ix86_agi_dependent (rtx set_insn, rtx use_insn)
21326 {
21327   int i;
21328   extract_insn_cached (use_insn);
21329   for (i = recog_data.n_operands - 1; i >= 0; --i)
21330     if (MEM_P (recog_data.operand[i]))
21331       {
21332         rtx addr = XEXP (recog_data.operand[i], 0);
21333         return modified_in_p (addr, set_insn) != 0;
21334       }
21335   return false;
21336 }
21337
21338 static int
21339 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
21340 {
21341   enum attr_type insn_type, dep_insn_type;
21342   enum attr_memory memory;
21343   rtx set, set2;
21344   int dep_insn_code_number;
21345
21346   /* Anti and output dependencies have zero cost on all CPUs.  */
21347   if (REG_NOTE_KIND (link) != 0)
21348     return 0;
21349
21350   dep_insn_code_number = recog_memoized (dep_insn);
21351
21352   /* If we can't recognize the insns, we can't really do anything.  */
21353   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
21354     return cost;
21355
21356   insn_type = get_attr_type (insn);
21357   dep_insn_type = get_attr_type (dep_insn);
21358
21359   switch (ix86_tune)
21360     {
21361     case PROCESSOR_PENTIUM:
21362       /* Address Generation Interlock adds a cycle of latency.  */
21363       if (insn_type == TYPE_LEA)
21364         {
21365           rtx addr = PATTERN (insn);
21366
21367           if (GET_CODE (addr) == PARALLEL)
21368             addr = XVECEXP (addr, 0, 0);
21369
21370           gcc_assert (GET_CODE (addr) == SET);
21371
21372           addr = SET_SRC (addr);
21373           if (modified_in_p (addr, dep_insn))
21374             cost += 1;
21375         }
21376       else if (ix86_agi_dependent (dep_insn, insn))
21377         cost += 1;
21378
21379       /* ??? Compares pair with jump/setcc.  */
21380       if (ix86_flags_dependent (insn, dep_insn, insn_type))
21381         cost = 0;
21382
21383       /* Floating point stores require value to be ready one cycle earlier.  */
21384       if (insn_type == TYPE_FMOV
21385           && get_attr_memory (insn) == MEMORY_STORE
21386           && !ix86_agi_dependent (dep_insn, insn))
21387         cost += 1;
21388       break;
21389
21390     case PROCESSOR_PENTIUMPRO:
21391       memory = get_attr_memory (insn);
21392
21393       /* INT->FP conversion is expensive.  */
21394       if (get_attr_fp_int_src (dep_insn))
21395         cost += 5;
21396
21397       /* There is one cycle extra latency between an FP op and a store.  */
21398       if (insn_type == TYPE_FMOV
21399           && (set = single_set (dep_insn)) != NULL_RTX
21400           && (set2 = single_set (insn)) != NULL_RTX
21401           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
21402           && MEM_P (SET_DEST (set2)))
21403         cost += 1;
21404
21405       /* Show ability of reorder buffer to hide latency of load by executing
21406          in parallel with previous instruction in case
21407          previous instruction is not needed to compute the address.  */
21408       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
21409           && !ix86_agi_dependent (dep_insn, insn))
21410         {
21411           /* Claim moves to take one cycle, as core can issue one load
21412              at time and the next load can start cycle later.  */
21413           if (dep_insn_type == TYPE_IMOV
21414               || dep_insn_type == TYPE_FMOV)
21415             cost = 1;
21416           else if (cost > 1)
21417             cost--;
21418         }
21419       break;
21420
21421     case PROCESSOR_K6:
21422       memory = get_attr_memory (insn);
21423
21424       /* The esp dependency is resolved before the instruction is really
21425          finished.  */
21426       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
21427           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
21428         return 1;
21429
21430       /* INT->FP conversion is expensive.  */
21431       if (get_attr_fp_int_src (dep_insn))
21432         cost += 5;
21433
21434       /* Show ability of reorder buffer to hide latency of load by executing
21435          in parallel with previous instruction in case
21436          previous instruction is not needed to compute the address.  */
21437       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
21438           && !ix86_agi_dependent (dep_insn, insn))
21439         {
21440           /* Claim moves to take one cycle, as core can issue one load
21441              at time and the next load can start cycle later.  */
21442           if (dep_insn_type == TYPE_IMOV
21443               || dep_insn_type == TYPE_FMOV)
21444             cost = 1;
21445           else if (cost > 2)
21446             cost -= 2;
21447           else
21448             cost = 1;
21449         }
21450       break;
21451
21452     case PROCESSOR_ATHLON:
21453     case PROCESSOR_K8:
21454     case PROCESSOR_AMDFAM10:
21455     case PROCESSOR_BDVER1:
21456     case PROCESSOR_ATOM:
21457     case PROCESSOR_GENERIC32:
21458     case PROCESSOR_GENERIC64:
21459       memory = get_attr_memory (insn);
21460
21461       /* Show ability of reorder buffer to hide latency of load by executing
21462          in parallel with previous instruction in case
21463          previous instruction is not needed to compute the address.  */
21464       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
21465           && !ix86_agi_dependent (dep_insn, insn))
21466         {
21467           enum attr_unit unit = get_attr_unit (insn);
21468           int loadcost = 3;
21469
21470           /* Because of the difference between the length of integer and
21471              floating unit pipeline preparation stages, the memory operands
21472              for floating point are cheaper.
21473
21474              ??? For Athlon it the difference is most probably 2.  */
21475           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
21476             loadcost = 3;
21477           else
21478             loadcost = TARGET_ATHLON ? 2 : 0;
21479
21480           if (cost >= loadcost)
21481             cost -= loadcost;
21482           else
21483             cost = 0;
21484         }
21485
21486     default:
21487       break;
21488     }
21489
21490   return cost;
21491 }
21492
21493 /* How many alternative schedules to try.  This should be as wide as the
21494    scheduling freedom in the DFA, but no wider.  Making this value too
21495    large results extra work for the scheduler.  */
21496
21497 static int
21498 ia32_multipass_dfa_lookahead (void)
21499 {
21500   switch (ix86_tune)
21501     {
21502     case PROCESSOR_PENTIUM:
21503       return 2;
21504
21505     case PROCESSOR_PENTIUMPRO:
21506     case PROCESSOR_K6:
21507       return 1;
21508
21509     default:
21510       return 0;
21511     }
21512 }
21513
21514 \f
21515 /* Compute the alignment given to a constant that is being placed in memory.
21516    EXP is the constant and ALIGN is the alignment that the object would
21517    ordinarily have.
21518    The value of this function is used instead of that alignment to align
21519    the object.  */
21520
21521 int
21522 ix86_constant_alignment (tree exp, int align)
21523 {
21524   if (TREE_CODE (exp) == REAL_CST || TREE_CODE (exp) == VECTOR_CST
21525       || TREE_CODE (exp) == INTEGER_CST)
21526     {
21527       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
21528         return 64;
21529       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
21530         return 128;
21531     }
21532   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
21533            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
21534     return BITS_PER_WORD;
21535
21536   return align;
21537 }
21538
21539 /* Compute the alignment for a static variable.
21540    TYPE is the data type, and ALIGN is the alignment that
21541    the object would ordinarily have.  The value of this function is used
21542    instead of that alignment to align the object.  */
21543
21544 int
21545 ix86_data_alignment (tree type, int align)
21546 {
21547   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
21548
21549   if (AGGREGATE_TYPE_P (type)
21550       && TYPE_SIZE (type)
21551       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
21552       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
21553           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
21554       && align < max_align)
21555     align = max_align;
21556
21557   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
21558      to 16byte boundary.  */
21559   if (TARGET_64BIT)
21560     {
21561       if (AGGREGATE_TYPE_P (type)
21562            && TYPE_SIZE (type)
21563            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
21564            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
21565                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
21566         return 128;
21567     }
21568
21569   if (TREE_CODE (type) == ARRAY_TYPE)
21570     {
21571       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
21572         return 64;
21573       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
21574         return 128;
21575     }
21576   else if (TREE_CODE (type) == COMPLEX_TYPE)
21577     {
21578
21579       if (TYPE_MODE (type) == DCmode && align < 64)
21580         return 64;
21581       if ((TYPE_MODE (type) == XCmode
21582            || TYPE_MODE (type) == TCmode) && align < 128)
21583         return 128;
21584     }
21585   else if ((TREE_CODE (type) == RECORD_TYPE
21586             || TREE_CODE (type) == UNION_TYPE
21587             || TREE_CODE (type) == QUAL_UNION_TYPE)
21588            && TYPE_FIELDS (type))
21589     {
21590       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
21591         return 64;
21592       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
21593         return 128;
21594     }
21595   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
21596            || TREE_CODE (type) == INTEGER_TYPE)
21597     {
21598       if (TYPE_MODE (type) == DFmode && align < 64)
21599         return 64;
21600       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
21601         return 128;
21602     }
21603
21604   return align;
21605 }
21606
21607 /* Compute the alignment for a local variable or a stack slot.  EXP is
21608    the data type or decl itself, MODE is the widest mode available and
21609    ALIGN is the alignment that the object would ordinarily have.  The
21610    value of this macro is used instead of that alignment to align the
21611    object.  */
21612
21613 unsigned int
21614 ix86_local_alignment (tree exp, enum machine_mode mode,
21615                       unsigned int align)
21616 {
21617   tree type, decl;
21618
21619   if (exp && DECL_P (exp))
21620     {
21621       type = TREE_TYPE (exp);
21622       decl = exp;
21623     }
21624   else
21625     {
21626       type = exp;
21627       decl = NULL;
21628     }
21629
21630   /* Don't do dynamic stack realignment for long long objects with
21631      -mpreferred-stack-boundary=2.  */
21632   if (!TARGET_64BIT
21633       && align == 64
21634       && ix86_preferred_stack_boundary < 64
21635       && (mode == DImode || (type && TYPE_MODE (type) == DImode))
21636       && (!type || !TYPE_USER_ALIGN (type))
21637       && (!decl || !DECL_USER_ALIGN (decl)))
21638     align = 32;
21639
21640   /* If TYPE is NULL, we are allocating a stack slot for caller-save
21641      register in MODE.  We will return the largest alignment of XF
21642      and DF.  */
21643   if (!type)
21644     {
21645       if (mode == XFmode && align < GET_MODE_ALIGNMENT (DFmode))
21646         align = GET_MODE_ALIGNMENT (DFmode);
21647       return align;
21648     }
21649
21650   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
21651      to 16byte boundary.  Exact wording is:
21652
21653      An array uses the same alignment as its elements, except that a local or
21654      global array variable of length at least 16 bytes or
21655      a C99 variable-length array variable always has alignment of at least 16 bytes.
21656
21657      This was added to allow use of aligned SSE instructions at arrays.  This
21658      rule is meant for static storage (where compiler can not do the analysis
21659      by itself).  We follow it for automatic variables only when convenient.
21660      We fully control everything in the function compiled and functions from
21661      other unit can not rely on the alignment.
21662
21663      Exclude va_list type.  It is the common case of local array where
21664      we can not benefit from the alignment.  */
21665   if (TARGET_64BIT && optimize_function_for_speed_p (cfun)
21666       && TARGET_SSE)
21667     {
21668       if (AGGREGATE_TYPE_P (type)
21669            && (TYPE_MAIN_VARIANT (type)
21670                != TYPE_MAIN_VARIANT (va_list_type_node))
21671            && TYPE_SIZE (type)
21672            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
21673            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
21674                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
21675         return 128;
21676     }
21677   if (TREE_CODE (type) == ARRAY_TYPE)
21678     {
21679       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
21680         return 64;
21681       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
21682         return 128;
21683     }
21684   else if (TREE_CODE (type) == COMPLEX_TYPE)
21685     {
21686       if (TYPE_MODE (type) == DCmode && align < 64)
21687         return 64;
21688       if ((TYPE_MODE (type) == XCmode
21689            || TYPE_MODE (type) == TCmode) && align < 128)
21690         return 128;
21691     }
21692   else if ((TREE_CODE (type) == RECORD_TYPE
21693             || TREE_CODE (type) == UNION_TYPE
21694             || TREE_CODE (type) == QUAL_UNION_TYPE)
21695            && TYPE_FIELDS (type))
21696     {
21697       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
21698         return 64;
21699       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
21700         return 128;
21701     }
21702   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
21703            || TREE_CODE (type) == INTEGER_TYPE)
21704     {
21705
21706       if (TYPE_MODE (type) == DFmode && align < 64)
21707         return 64;
21708       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
21709         return 128;
21710     }
21711   return align;
21712 }
21713
21714 /* Compute the minimum required alignment for dynamic stack realignment
21715    purposes for a local variable, parameter or a stack slot.  EXP is
21716    the data type or decl itself, MODE is its mode and ALIGN is the
21717    alignment that the object would ordinarily have.  */
21718
21719 unsigned int
21720 ix86_minimum_alignment (tree exp, enum machine_mode mode,
21721                         unsigned int align)
21722 {
21723   tree type, decl;
21724
21725   if (TARGET_64BIT || align != 64 || ix86_preferred_stack_boundary >= 64)
21726     return align;
21727
21728   if (exp && DECL_P (exp))
21729     {
21730       type = TREE_TYPE (exp);
21731       decl = exp;
21732     }
21733   else
21734     {
21735       type = exp;
21736       decl = NULL;
21737     }
21738
21739   /* Don't do dynamic stack realignment for long long objects with
21740      -mpreferred-stack-boundary=2.  */
21741   if ((mode == DImode || (type && TYPE_MODE (type) == DImode))
21742       && (!type || !TYPE_USER_ALIGN (type))
21743       && (!decl || !DECL_USER_ALIGN (decl)))
21744     return 32;
21745
21746   return align;
21747 }
21748 \f
21749 /* Find a location for the static chain incoming to a nested function.
21750    This is a register, unless all free registers are used by arguments.  */
21751
21752 static rtx
21753 ix86_static_chain (const_tree fndecl, bool incoming_p)
21754 {
21755   unsigned regno;
21756
21757   if (!DECL_STATIC_CHAIN (fndecl))
21758     return NULL;
21759
21760   if (TARGET_64BIT)
21761     {
21762       /* We always use R10 in 64-bit mode.  */
21763       regno = R10_REG;
21764     }
21765   else
21766     {
21767       tree fntype;
21768       /* By default in 32-bit mode we use ECX to pass the static chain.  */
21769       regno = CX_REG;
21770
21771       fntype = TREE_TYPE (fndecl);
21772       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
21773         {
21774           /* Fastcall functions use ecx/edx for arguments, which leaves
21775              us with EAX for the static chain.  */
21776           regno = AX_REG;
21777         }
21778       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (fntype)))
21779         {
21780           /* Thiscall functions use ecx for arguments, which leaves
21781              us with EAX for the static chain.  */
21782           regno = AX_REG;
21783         }
21784       else if (ix86_function_regparm (fntype, fndecl) == 3)
21785         {
21786           /* For regparm 3, we have no free call-clobbered registers in
21787              which to store the static chain.  In order to implement this,
21788              we have the trampoline push the static chain to the stack.
21789              However, we can't push a value below the return address when
21790              we call the nested function directly, so we have to use an
21791              alternate entry point.  For this we use ESI, and have the
21792              alternate entry point push ESI, so that things appear the
21793              same once we're executing the nested function.  */
21794           if (incoming_p)
21795             {
21796               if (fndecl == current_function_decl)
21797                 ix86_static_chain_on_stack = true;
21798               return gen_frame_mem (SImode,
21799                                     plus_constant (arg_pointer_rtx, -8));
21800             }
21801           regno = SI_REG;
21802         }
21803     }
21804
21805   return gen_rtx_REG (Pmode, regno);
21806 }
21807
21808 /* Emit RTL insns to initialize the variable parts of a trampoline.
21809    FNDECL is the decl of the target address; M_TRAMP is a MEM for
21810    the trampoline, and CHAIN_VALUE is an RTX for the static chain
21811    to be passed to the target function.  */
21812
21813 static void
21814 ix86_trampoline_init (rtx m_tramp, tree fndecl, rtx chain_value)
21815 {
21816   rtx mem, fnaddr;
21817
21818   fnaddr = XEXP (DECL_RTL (fndecl), 0);
21819
21820   if (!TARGET_64BIT)
21821     {
21822       rtx disp, chain;
21823       int opcode;
21824
21825       /* Depending on the static chain location, either load a register
21826          with a constant, or push the constant to the stack.  All of the
21827          instructions are the same size.  */
21828       chain = ix86_static_chain (fndecl, true);
21829       if (REG_P (chain))
21830         {
21831           if (REGNO (chain) == CX_REG)
21832             opcode = 0xb9;
21833           else if (REGNO (chain) == AX_REG)
21834             opcode = 0xb8;
21835           else
21836             gcc_unreachable ();
21837         }
21838       else
21839         opcode = 0x68;
21840
21841       mem = adjust_address (m_tramp, QImode, 0);
21842       emit_move_insn (mem, gen_int_mode (opcode, QImode));
21843
21844       mem = adjust_address (m_tramp, SImode, 1);
21845       emit_move_insn (mem, chain_value);
21846
21847       /* Compute offset from the end of the jmp to the target function.
21848          In the case in which the trampoline stores the static chain on
21849          the stack, we need to skip the first insn which pushes the
21850          (call-saved) register static chain; this push is 1 byte.  */
21851       disp = expand_binop (SImode, sub_optab, fnaddr,
21852                            plus_constant (XEXP (m_tramp, 0),
21853                                           MEM_P (chain) ? 9 : 10),
21854                            NULL_RTX, 1, OPTAB_DIRECT);
21855
21856       mem = adjust_address (m_tramp, QImode, 5);
21857       emit_move_insn (mem, gen_int_mode (0xe9, QImode));
21858
21859       mem = adjust_address (m_tramp, SImode, 6);
21860       emit_move_insn (mem, disp);
21861     }
21862   else
21863     {
21864       int offset = 0;
21865
21866       /* Load the function address to r11.  Try to load address using
21867          the shorter movl instead of movabs.  We may want to support
21868          movq for kernel mode, but kernel does not use trampolines at
21869          the moment.  */
21870       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
21871         {
21872           fnaddr = copy_to_mode_reg (DImode, fnaddr);
21873
21874           mem = adjust_address (m_tramp, HImode, offset);
21875           emit_move_insn (mem, gen_int_mode (0xbb41, HImode));
21876
21877           mem = adjust_address (m_tramp, SImode, offset + 2);
21878           emit_move_insn (mem, gen_lowpart (SImode, fnaddr));
21879           offset += 6;
21880         }
21881       else
21882         {
21883           mem = adjust_address (m_tramp, HImode, offset);
21884           emit_move_insn (mem, gen_int_mode (0xbb49, HImode));
21885
21886           mem = adjust_address (m_tramp, DImode, offset + 2);
21887           emit_move_insn (mem, fnaddr);
21888           offset += 10;
21889         }
21890
21891       /* Load static chain using movabs to r10.  */
21892       mem = adjust_address (m_tramp, HImode, offset);
21893       emit_move_insn (mem, gen_int_mode (0xba49, HImode));
21894
21895       mem = adjust_address (m_tramp, DImode, offset + 2);
21896       emit_move_insn (mem, chain_value);
21897       offset += 10;
21898
21899       /* Jump to r11; the last (unused) byte is a nop, only there to
21900          pad the write out to a single 32-bit store.  */
21901       mem = adjust_address (m_tramp, SImode, offset);
21902       emit_move_insn (mem, gen_int_mode (0x90e3ff49, SImode));
21903       offset += 4;
21904
21905       gcc_assert (offset <= TRAMPOLINE_SIZE);
21906     }
21907
21908 #ifdef ENABLE_EXECUTE_STACK
21909 #ifdef CHECK_EXECUTE_STACK_ENABLED
21910   if (CHECK_EXECUTE_STACK_ENABLED)
21911 #endif
21912   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
21913                      LCT_NORMAL, VOIDmode, 1, XEXP (m_tramp, 0), Pmode);
21914 #endif
21915 }
21916 \f
21917 /* The following file contains several enumerations and data structures
21918    built from the definitions in i386-builtin-types.def.  */
21919
21920 #include "i386-builtin-types.inc"
21921
21922 /* Table for the ix86 builtin non-function types.  */
21923 static GTY(()) tree ix86_builtin_type_tab[(int) IX86_BT_LAST_CPTR + 1];
21924
21925 /* Retrieve an element from the above table, building some of
21926    the types lazily.  */
21927
21928 static tree
21929 ix86_get_builtin_type (enum ix86_builtin_type tcode)
21930 {
21931   unsigned int index;
21932   tree type, itype;
21933
21934   gcc_assert ((unsigned)tcode < ARRAY_SIZE(ix86_builtin_type_tab));
21935
21936   type = ix86_builtin_type_tab[(int) tcode];
21937   if (type != NULL)
21938     return type;
21939
21940   gcc_assert (tcode > IX86_BT_LAST_PRIM);
21941   if (tcode <= IX86_BT_LAST_VECT)
21942     {
21943       enum machine_mode mode;
21944
21945       index = tcode - IX86_BT_LAST_PRIM - 1;
21946       itype = ix86_get_builtin_type (ix86_builtin_type_vect_base[index]);
21947       mode = ix86_builtin_type_vect_mode[index];
21948
21949       type = build_vector_type_for_mode (itype, mode);
21950     }
21951   else
21952     {
21953       int quals;
21954
21955       index = tcode - IX86_BT_LAST_VECT - 1;
21956       if (tcode <= IX86_BT_LAST_PTR)
21957         quals = TYPE_UNQUALIFIED;
21958       else
21959         quals = TYPE_QUAL_CONST;
21960
21961       itype = ix86_get_builtin_type (ix86_builtin_type_ptr_base[index]);
21962       if (quals != TYPE_UNQUALIFIED)
21963         itype = build_qualified_type (itype, quals);
21964
21965       type = build_pointer_type (itype);
21966     }
21967
21968   ix86_builtin_type_tab[(int) tcode] = type;
21969   return type;
21970 }
21971
21972 /* Table for the ix86 builtin function types.  */
21973 static GTY(()) tree ix86_builtin_func_type_tab[(int) IX86_BT_LAST_ALIAS + 1];
21974
21975 /* Retrieve an element from the above table, building some of
21976    the types lazily.  */
21977
21978 static tree
21979 ix86_get_builtin_func_type (enum ix86_builtin_func_type tcode)
21980 {
21981   tree type;
21982
21983   gcc_assert ((unsigned)tcode < ARRAY_SIZE (ix86_builtin_func_type_tab));
21984
21985   type = ix86_builtin_func_type_tab[(int) tcode];
21986   if (type != NULL)
21987     return type;
21988
21989   if (tcode <= IX86_BT_LAST_FUNC)
21990     {
21991       unsigned start = ix86_builtin_func_start[(int) tcode];
21992       unsigned after = ix86_builtin_func_start[(int) tcode + 1];
21993       tree rtype, atype, args = void_list_node;
21994       unsigned i;
21995
21996       rtype = ix86_get_builtin_type (ix86_builtin_func_args[start]);
21997       for (i = after - 1; i > start; --i)
21998         {
21999           atype = ix86_get_builtin_type (ix86_builtin_func_args[i]);
22000           args = tree_cons (NULL, atype, args);
22001         }
22002
22003       type = build_function_type (rtype, args);
22004     }
22005   else
22006     {
22007       unsigned index = tcode - IX86_BT_LAST_FUNC - 1;
22008       enum ix86_builtin_func_type icode;
22009
22010       icode = ix86_builtin_func_alias_base[index];
22011       type = ix86_get_builtin_func_type (icode);
22012     }
22013
22014   ix86_builtin_func_type_tab[(int) tcode] = type;
22015   return type;
22016 }
22017
22018
22019 /* Codes for all the SSE/MMX builtins.  */
22020 enum ix86_builtins
22021 {
22022   IX86_BUILTIN_ADDPS,
22023   IX86_BUILTIN_ADDSS,
22024   IX86_BUILTIN_DIVPS,
22025   IX86_BUILTIN_DIVSS,
22026   IX86_BUILTIN_MULPS,
22027   IX86_BUILTIN_MULSS,
22028   IX86_BUILTIN_SUBPS,
22029   IX86_BUILTIN_SUBSS,
22030
22031   IX86_BUILTIN_CMPEQPS,
22032   IX86_BUILTIN_CMPLTPS,
22033   IX86_BUILTIN_CMPLEPS,
22034   IX86_BUILTIN_CMPGTPS,
22035   IX86_BUILTIN_CMPGEPS,
22036   IX86_BUILTIN_CMPNEQPS,
22037   IX86_BUILTIN_CMPNLTPS,
22038   IX86_BUILTIN_CMPNLEPS,
22039   IX86_BUILTIN_CMPNGTPS,
22040   IX86_BUILTIN_CMPNGEPS,
22041   IX86_BUILTIN_CMPORDPS,
22042   IX86_BUILTIN_CMPUNORDPS,
22043   IX86_BUILTIN_CMPEQSS,
22044   IX86_BUILTIN_CMPLTSS,
22045   IX86_BUILTIN_CMPLESS,
22046   IX86_BUILTIN_CMPNEQSS,
22047   IX86_BUILTIN_CMPNLTSS,
22048   IX86_BUILTIN_CMPNLESS,
22049   IX86_BUILTIN_CMPNGTSS,
22050   IX86_BUILTIN_CMPNGESS,
22051   IX86_BUILTIN_CMPORDSS,
22052   IX86_BUILTIN_CMPUNORDSS,
22053
22054   IX86_BUILTIN_COMIEQSS,
22055   IX86_BUILTIN_COMILTSS,
22056   IX86_BUILTIN_COMILESS,
22057   IX86_BUILTIN_COMIGTSS,
22058   IX86_BUILTIN_COMIGESS,
22059   IX86_BUILTIN_COMINEQSS,
22060   IX86_BUILTIN_UCOMIEQSS,
22061   IX86_BUILTIN_UCOMILTSS,
22062   IX86_BUILTIN_UCOMILESS,
22063   IX86_BUILTIN_UCOMIGTSS,
22064   IX86_BUILTIN_UCOMIGESS,
22065   IX86_BUILTIN_UCOMINEQSS,
22066
22067   IX86_BUILTIN_CVTPI2PS,
22068   IX86_BUILTIN_CVTPS2PI,
22069   IX86_BUILTIN_CVTSI2SS,
22070   IX86_BUILTIN_CVTSI642SS,
22071   IX86_BUILTIN_CVTSS2SI,
22072   IX86_BUILTIN_CVTSS2SI64,
22073   IX86_BUILTIN_CVTTPS2PI,
22074   IX86_BUILTIN_CVTTSS2SI,
22075   IX86_BUILTIN_CVTTSS2SI64,
22076
22077   IX86_BUILTIN_MAXPS,
22078   IX86_BUILTIN_MAXSS,
22079   IX86_BUILTIN_MINPS,
22080   IX86_BUILTIN_MINSS,
22081
22082   IX86_BUILTIN_LOADUPS,
22083   IX86_BUILTIN_STOREUPS,
22084   IX86_BUILTIN_MOVSS,
22085
22086   IX86_BUILTIN_MOVHLPS,
22087   IX86_BUILTIN_MOVLHPS,
22088   IX86_BUILTIN_LOADHPS,
22089   IX86_BUILTIN_LOADLPS,
22090   IX86_BUILTIN_STOREHPS,
22091   IX86_BUILTIN_STORELPS,
22092
22093   IX86_BUILTIN_MASKMOVQ,
22094   IX86_BUILTIN_MOVMSKPS,
22095   IX86_BUILTIN_PMOVMSKB,
22096
22097   IX86_BUILTIN_MOVNTPS,
22098   IX86_BUILTIN_MOVNTQ,
22099
22100   IX86_BUILTIN_LOADDQU,
22101   IX86_BUILTIN_STOREDQU,
22102
22103   IX86_BUILTIN_PACKSSWB,
22104   IX86_BUILTIN_PACKSSDW,
22105   IX86_BUILTIN_PACKUSWB,
22106
22107   IX86_BUILTIN_PADDB,
22108   IX86_BUILTIN_PADDW,
22109   IX86_BUILTIN_PADDD,
22110   IX86_BUILTIN_PADDQ,
22111   IX86_BUILTIN_PADDSB,
22112   IX86_BUILTIN_PADDSW,
22113   IX86_BUILTIN_PADDUSB,
22114   IX86_BUILTIN_PADDUSW,
22115   IX86_BUILTIN_PSUBB,
22116   IX86_BUILTIN_PSUBW,
22117   IX86_BUILTIN_PSUBD,
22118   IX86_BUILTIN_PSUBQ,
22119   IX86_BUILTIN_PSUBSB,
22120   IX86_BUILTIN_PSUBSW,
22121   IX86_BUILTIN_PSUBUSB,
22122   IX86_BUILTIN_PSUBUSW,
22123
22124   IX86_BUILTIN_PAND,
22125   IX86_BUILTIN_PANDN,
22126   IX86_BUILTIN_POR,
22127   IX86_BUILTIN_PXOR,
22128
22129   IX86_BUILTIN_PAVGB,
22130   IX86_BUILTIN_PAVGW,
22131
22132   IX86_BUILTIN_PCMPEQB,
22133   IX86_BUILTIN_PCMPEQW,
22134   IX86_BUILTIN_PCMPEQD,
22135   IX86_BUILTIN_PCMPGTB,
22136   IX86_BUILTIN_PCMPGTW,
22137   IX86_BUILTIN_PCMPGTD,
22138
22139   IX86_BUILTIN_PMADDWD,
22140
22141   IX86_BUILTIN_PMAXSW,
22142   IX86_BUILTIN_PMAXUB,
22143   IX86_BUILTIN_PMINSW,
22144   IX86_BUILTIN_PMINUB,
22145
22146   IX86_BUILTIN_PMULHUW,
22147   IX86_BUILTIN_PMULHW,
22148   IX86_BUILTIN_PMULLW,
22149
22150   IX86_BUILTIN_PSADBW,
22151   IX86_BUILTIN_PSHUFW,
22152
22153   IX86_BUILTIN_PSLLW,
22154   IX86_BUILTIN_PSLLD,
22155   IX86_BUILTIN_PSLLQ,
22156   IX86_BUILTIN_PSRAW,
22157   IX86_BUILTIN_PSRAD,
22158   IX86_BUILTIN_PSRLW,
22159   IX86_BUILTIN_PSRLD,
22160   IX86_BUILTIN_PSRLQ,
22161   IX86_BUILTIN_PSLLWI,
22162   IX86_BUILTIN_PSLLDI,
22163   IX86_BUILTIN_PSLLQI,
22164   IX86_BUILTIN_PSRAWI,
22165   IX86_BUILTIN_PSRADI,
22166   IX86_BUILTIN_PSRLWI,
22167   IX86_BUILTIN_PSRLDI,
22168   IX86_BUILTIN_PSRLQI,
22169
22170   IX86_BUILTIN_PUNPCKHBW,
22171   IX86_BUILTIN_PUNPCKHWD,
22172   IX86_BUILTIN_PUNPCKHDQ,
22173   IX86_BUILTIN_PUNPCKLBW,
22174   IX86_BUILTIN_PUNPCKLWD,
22175   IX86_BUILTIN_PUNPCKLDQ,
22176
22177   IX86_BUILTIN_SHUFPS,
22178
22179   IX86_BUILTIN_RCPPS,
22180   IX86_BUILTIN_RCPSS,
22181   IX86_BUILTIN_RSQRTPS,
22182   IX86_BUILTIN_RSQRTPS_NR,
22183   IX86_BUILTIN_RSQRTSS,
22184   IX86_BUILTIN_RSQRTF,
22185   IX86_BUILTIN_SQRTPS,
22186   IX86_BUILTIN_SQRTPS_NR,
22187   IX86_BUILTIN_SQRTSS,
22188
22189   IX86_BUILTIN_UNPCKHPS,
22190   IX86_BUILTIN_UNPCKLPS,
22191
22192   IX86_BUILTIN_ANDPS,
22193   IX86_BUILTIN_ANDNPS,
22194   IX86_BUILTIN_ORPS,
22195   IX86_BUILTIN_XORPS,
22196
22197   IX86_BUILTIN_EMMS,
22198   IX86_BUILTIN_LDMXCSR,
22199   IX86_BUILTIN_STMXCSR,
22200   IX86_BUILTIN_SFENCE,
22201
22202   /* 3DNow! Original */
22203   IX86_BUILTIN_FEMMS,
22204   IX86_BUILTIN_PAVGUSB,
22205   IX86_BUILTIN_PF2ID,
22206   IX86_BUILTIN_PFACC,
22207   IX86_BUILTIN_PFADD,
22208   IX86_BUILTIN_PFCMPEQ,
22209   IX86_BUILTIN_PFCMPGE,
22210   IX86_BUILTIN_PFCMPGT,
22211   IX86_BUILTIN_PFMAX,
22212   IX86_BUILTIN_PFMIN,
22213   IX86_BUILTIN_PFMUL,
22214   IX86_BUILTIN_PFRCP,
22215   IX86_BUILTIN_PFRCPIT1,
22216   IX86_BUILTIN_PFRCPIT2,
22217   IX86_BUILTIN_PFRSQIT1,
22218   IX86_BUILTIN_PFRSQRT,
22219   IX86_BUILTIN_PFSUB,
22220   IX86_BUILTIN_PFSUBR,
22221   IX86_BUILTIN_PI2FD,
22222   IX86_BUILTIN_PMULHRW,
22223
22224   /* 3DNow! Athlon Extensions */
22225   IX86_BUILTIN_PF2IW,
22226   IX86_BUILTIN_PFNACC,
22227   IX86_BUILTIN_PFPNACC,
22228   IX86_BUILTIN_PI2FW,
22229   IX86_BUILTIN_PSWAPDSI,
22230   IX86_BUILTIN_PSWAPDSF,
22231
22232   /* SSE2 */
22233   IX86_BUILTIN_ADDPD,
22234   IX86_BUILTIN_ADDSD,
22235   IX86_BUILTIN_DIVPD,
22236   IX86_BUILTIN_DIVSD,
22237   IX86_BUILTIN_MULPD,
22238   IX86_BUILTIN_MULSD,
22239   IX86_BUILTIN_SUBPD,
22240   IX86_BUILTIN_SUBSD,
22241
22242   IX86_BUILTIN_CMPEQPD,
22243   IX86_BUILTIN_CMPLTPD,
22244   IX86_BUILTIN_CMPLEPD,
22245   IX86_BUILTIN_CMPGTPD,
22246   IX86_BUILTIN_CMPGEPD,
22247   IX86_BUILTIN_CMPNEQPD,
22248   IX86_BUILTIN_CMPNLTPD,
22249   IX86_BUILTIN_CMPNLEPD,
22250   IX86_BUILTIN_CMPNGTPD,
22251   IX86_BUILTIN_CMPNGEPD,
22252   IX86_BUILTIN_CMPORDPD,
22253   IX86_BUILTIN_CMPUNORDPD,
22254   IX86_BUILTIN_CMPEQSD,
22255   IX86_BUILTIN_CMPLTSD,
22256   IX86_BUILTIN_CMPLESD,
22257   IX86_BUILTIN_CMPNEQSD,
22258   IX86_BUILTIN_CMPNLTSD,
22259   IX86_BUILTIN_CMPNLESD,
22260   IX86_BUILTIN_CMPORDSD,
22261   IX86_BUILTIN_CMPUNORDSD,
22262
22263   IX86_BUILTIN_COMIEQSD,
22264   IX86_BUILTIN_COMILTSD,
22265   IX86_BUILTIN_COMILESD,
22266   IX86_BUILTIN_COMIGTSD,
22267   IX86_BUILTIN_COMIGESD,
22268   IX86_BUILTIN_COMINEQSD,
22269   IX86_BUILTIN_UCOMIEQSD,
22270   IX86_BUILTIN_UCOMILTSD,
22271   IX86_BUILTIN_UCOMILESD,
22272   IX86_BUILTIN_UCOMIGTSD,
22273   IX86_BUILTIN_UCOMIGESD,
22274   IX86_BUILTIN_UCOMINEQSD,
22275
22276   IX86_BUILTIN_MAXPD,
22277   IX86_BUILTIN_MAXSD,
22278   IX86_BUILTIN_MINPD,
22279   IX86_BUILTIN_MINSD,
22280
22281   IX86_BUILTIN_ANDPD,
22282   IX86_BUILTIN_ANDNPD,
22283   IX86_BUILTIN_ORPD,
22284   IX86_BUILTIN_XORPD,
22285
22286   IX86_BUILTIN_SQRTPD,
22287   IX86_BUILTIN_SQRTSD,
22288
22289   IX86_BUILTIN_UNPCKHPD,
22290   IX86_BUILTIN_UNPCKLPD,
22291
22292   IX86_BUILTIN_SHUFPD,
22293
22294   IX86_BUILTIN_LOADUPD,
22295   IX86_BUILTIN_STOREUPD,
22296   IX86_BUILTIN_MOVSD,
22297
22298   IX86_BUILTIN_LOADHPD,
22299   IX86_BUILTIN_LOADLPD,
22300
22301   IX86_BUILTIN_CVTDQ2PD,
22302   IX86_BUILTIN_CVTDQ2PS,
22303
22304   IX86_BUILTIN_CVTPD2DQ,
22305   IX86_BUILTIN_CVTPD2PI,
22306   IX86_BUILTIN_CVTPD2PS,
22307   IX86_BUILTIN_CVTTPD2DQ,
22308   IX86_BUILTIN_CVTTPD2PI,
22309
22310   IX86_BUILTIN_CVTPI2PD,
22311   IX86_BUILTIN_CVTSI2SD,
22312   IX86_BUILTIN_CVTSI642SD,
22313
22314   IX86_BUILTIN_CVTSD2SI,
22315   IX86_BUILTIN_CVTSD2SI64,
22316   IX86_BUILTIN_CVTSD2SS,
22317   IX86_BUILTIN_CVTSS2SD,
22318   IX86_BUILTIN_CVTTSD2SI,
22319   IX86_BUILTIN_CVTTSD2SI64,
22320
22321   IX86_BUILTIN_CVTPS2DQ,
22322   IX86_BUILTIN_CVTPS2PD,
22323   IX86_BUILTIN_CVTTPS2DQ,
22324
22325   IX86_BUILTIN_MOVNTI,
22326   IX86_BUILTIN_MOVNTPD,
22327   IX86_BUILTIN_MOVNTDQ,
22328
22329   IX86_BUILTIN_MOVQ128,
22330
22331   /* SSE2 MMX */
22332   IX86_BUILTIN_MASKMOVDQU,
22333   IX86_BUILTIN_MOVMSKPD,
22334   IX86_BUILTIN_PMOVMSKB128,
22335
22336   IX86_BUILTIN_PACKSSWB128,
22337   IX86_BUILTIN_PACKSSDW128,
22338   IX86_BUILTIN_PACKUSWB128,
22339
22340   IX86_BUILTIN_PADDB128,
22341   IX86_BUILTIN_PADDW128,
22342   IX86_BUILTIN_PADDD128,
22343   IX86_BUILTIN_PADDQ128,
22344   IX86_BUILTIN_PADDSB128,
22345   IX86_BUILTIN_PADDSW128,
22346   IX86_BUILTIN_PADDUSB128,
22347   IX86_BUILTIN_PADDUSW128,
22348   IX86_BUILTIN_PSUBB128,
22349   IX86_BUILTIN_PSUBW128,
22350   IX86_BUILTIN_PSUBD128,
22351   IX86_BUILTIN_PSUBQ128,
22352   IX86_BUILTIN_PSUBSB128,
22353   IX86_BUILTIN_PSUBSW128,
22354   IX86_BUILTIN_PSUBUSB128,
22355   IX86_BUILTIN_PSUBUSW128,
22356
22357   IX86_BUILTIN_PAND128,
22358   IX86_BUILTIN_PANDN128,
22359   IX86_BUILTIN_POR128,
22360   IX86_BUILTIN_PXOR128,
22361
22362   IX86_BUILTIN_PAVGB128,
22363   IX86_BUILTIN_PAVGW128,
22364
22365   IX86_BUILTIN_PCMPEQB128,
22366   IX86_BUILTIN_PCMPEQW128,
22367   IX86_BUILTIN_PCMPEQD128,
22368   IX86_BUILTIN_PCMPGTB128,
22369   IX86_BUILTIN_PCMPGTW128,
22370   IX86_BUILTIN_PCMPGTD128,
22371
22372   IX86_BUILTIN_PMADDWD128,
22373
22374   IX86_BUILTIN_PMAXSW128,
22375   IX86_BUILTIN_PMAXUB128,
22376   IX86_BUILTIN_PMINSW128,
22377   IX86_BUILTIN_PMINUB128,
22378
22379   IX86_BUILTIN_PMULUDQ,
22380   IX86_BUILTIN_PMULUDQ128,
22381   IX86_BUILTIN_PMULHUW128,
22382   IX86_BUILTIN_PMULHW128,
22383   IX86_BUILTIN_PMULLW128,
22384
22385   IX86_BUILTIN_PSADBW128,
22386   IX86_BUILTIN_PSHUFHW,
22387   IX86_BUILTIN_PSHUFLW,
22388   IX86_BUILTIN_PSHUFD,
22389
22390   IX86_BUILTIN_PSLLDQI128,
22391   IX86_BUILTIN_PSLLWI128,
22392   IX86_BUILTIN_PSLLDI128,
22393   IX86_BUILTIN_PSLLQI128,
22394   IX86_BUILTIN_PSRAWI128,
22395   IX86_BUILTIN_PSRADI128,
22396   IX86_BUILTIN_PSRLDQI128,
22397   IX86_BUILTIN_PSRLWI128,
22398   IX86_BUILTIN_PSRLDI128,
22399   IX86_BUILTIN_PSRLQI128,
22400
22401   IX86_BUILTIN_PSLLDQ128,
22402   IX86_BUILTIN_PSLLW128,
22403   IX86_BUILTIN_PSLLD128,
22404   IX86_BUILTIN_PSLLQ128,
22405   IX86_BUILTIN_PSRAW128,
22406   IX86_BUILTIN_PSRAD128,
22407   IX86_BUILTIN_PSRLW128,
22408   IX86_BUILTIN_PSRLD128,
22409   IX86_BUILTIN_PSRLQ128,
22410
22411   IX86_BUILTIN_PUNPCKHBW128,
22412   IX86_BUILTIN_PUNPCKHWD128,
22413   IX86_BUILTIN_PUNPCKHDQ128,
22414   IX86_BUILTIN_PUNPCKHQDQ128,
22415   IX86_BUILTIN_PUNPCKLBW128,
22416   IX86_BUILTIN_PUNPCKLWD128,
22417   IX86_BUILTIN_PUNPCKLDQ128,
22418   IX86_BUILTIN_PUNPCKLQDQ128,
22419
22420   IX86_BUILTIN_CLFLUSH,
22421   IX86_BUILTIN_MFENCE,
22422   IX86_BUILTIN_LFENCE,
22423
22424   IX86_BUILTIN_BSRSI,
22425   IX86_BUILTIN_BSRDI,
22426   IX86_BUILTIN_RDPMC,
22427   IX86_BUILTIN_RDTSC,
22428   IX86_BUILTIN_RDTSCP,
22429   IX86_BUILTIN_ROLQI,
22430   IX86_BUILTIN_ROLHI,
22431   IX86_BUILTIN_RORQI,
22432   IX86_BUILTIN_RORHI,
22433
22434   /* SSE3.  */
22435   IX86_BUILTIN_ADDSUBPS,
22436   IX86_BUILTIN_HADDPS,
22437   IX86_BUILTIN_HSUBPS,
22438   IX86_BUILTIN_MOVSHDUP,
22439   IX86_BUILTIN_MOVSLDUP,
22440   IX86_BUILTIN_ADDSUBPD,
22441   IX86_BUILTIN_HADDPD,
22442   IX86_BUILTIN_HSUBPD,
22443   IX86_BUILTIN_LDDQU,
22444
22445   IX86_BUILTIN_MONITOR,
22446   IX86_BUILTIN_MWAIT,
22447
22448   /* SSSE3.  */
22449   IX86_BUILTIN_PHADDW,
22450   IX86_BUILTIN_PHADDD,
22451   IX86_BUILTIN_PHADDSW,
22452   IX86_BUILTIN_PHSUBW,
22453   IX86_BUILTIN_PHSUBD,
22454   IX86_BUILTIN_PHSUBSW,
22455   IX86_BUILTIN_PMADDUBSW,
22456   IX86_BUILTIN_PMULHRSW,
22457   IX86_BUILTIN_PSHUFB,
22458   IX86_BUILTIN_PSIGNB,
22459   IX86_BUILTIN_PSIGNW,
22460   IX86_BUILTIN_PSIGND,
22461   IX86_BUILTIN_PALIGNR,
22462   IX86_BUILTIN_PABSB,
22463   IX86_BUILTIN_PABSW,
22464   IX86_BUILTIN_PABSD,
22465
22466   IX86_BUILTIN_PHADDW128,
22467   IX86_BUILTIN_PHADDD128,
22468   IX86_BUILTIN_PHADDSW128,
22469   IX86_BUILTIN_PHSUBW128,
22470   IX86_BUILTIN_PHSUBD128,
22471   IX86_BUILTIN_PHSUBSW128,
22472   IX86_BUILTIN_PMADDUBSW128,
22473   IX86_BUILTIN_PMULHRSW128,
22474   IX86_BUILTIN_PSHUFB128,
22475   IX86_BUILTIN_PSIGNB128,
22476   IX86_BUILTIN_PSIGNW128,
22477   IX86_BUILTIN_PSIGND128,
22478   IX86_BUILTIN_PALIGNR128,
22479   IX86_BUILTIN_PABSB128,
22480   IX86_BUILTIN_PABSW128,
22481   IX86_BUILTIN_PABSD128,
22482
22483   /* AMDFAM10 - SSE4A New Instructions.  */
22484   IX86_BUILTIN_MOVNTSD,
22485   IX86_BUILTIN_MOVNTSS,
22486   IX86_BUILTIN_EXTRQI,
22487   IX86_BUILTIN_EXTRQ,
22488   IX86_BUILTIN_INSERTQI,
22489   IX86_BUILTIN_INSERTQ,
22490
22491   /* SSE4.1.  */
22492   IX86_BUILTIN_BLENDPD,
22493   IX86_BUILTIN_BLENDPS,
22494   IX86_BUILTIN_BLENDVPD,
22495   IX86_BUILTIN_BLENDVPS,
22496   IX86_BUILTIN_PBLENDVB128,
22497   IX86_BUILTIN_PBLENDW128,
22498
22499   IX86_BUILTIN_DPPD,
22500   IX86_BUILTIN_DPPS,
22501
22502   IX86_BUILTIN_INSERTPS128,
22503
22504   IX86_BUILTIN_MOVNTDQA,
22505   IX86_BUILTIN_MPSADBW128,
22506   IX86_BUILTIN_PACKUSDW128,
22507   IX86_BUILTIN_PCMPEQQ,
22508   IX86_BUILTIN_PHMINPOSUW128,
22509
22510   IX86_BUILTIN_PMAXSB128,
22511   IX86_BUILTIN_PMAXSD128,
22512   IX86_BUILTIN_PMAXUD128,
22513   IX86_BUILTIN_PMAXUW128,
22514
22515   IX86_BUILTIN_PMINSB128,
22516   IX86_BUILTIN_PMINSD128,
22517   IX86_BUILTIN_PMINUD128,
22518   IX86_BUILTIN_PMINUW128,
22519
22520   IX86_BUILTIN_PMOVSXBW128,
22521   IX86_BUILTIN_PMOVSXBD128,
22522   IX86_BUILTIN_PMOVSXBQ128,
22523   IX86_BUILTIN_PMOVSXWD128,
22524   IX86_BUILTIN_PMOVSXWQ128,
22525   IX86_BUILTIN_PMOVSXDQ128,
22526
22527   IX86_BUILTIN_PMOVZXBW128,
22528   IX86_BUILTIN_PMOVZXBD128,
22529   IX86_BUILTIN_PMOVZXBQ128,
22530   IX86_BUILTIN_PMOVZXWD128,
22531   IX86_BUILTIN_PMOVZXWQ128,
22532   IX86_BUILTIN_PMOVZXDQ128,
22533
22534   IX86_BUILTIN_PMULDQ128,
22535   IX86_BUILTIN_PMULLD128,
22536
22537   IX86_BUILTIN_ROUNDPD,
22538   IX86_BUILTIN_ROUNDPS,
22539   IX86_BUILTIN_ROUNDSD,
22540   IX86_BUILTIN_ROUNDSS,
22541
22542   IX86_BUILTIN_PTESTZ,
22543   IX86_BUILTIN_PTESTC,
22544   IX86_BUILTIN_PTESTNZC,
22545
22546   IX86_BUILTIN_VEC_INIT_V2SI,
22547   IX86_BUILTIN_VEC_INIT_V4HI,
22548   IX86_BUILTIN_VEC_INIT_V8QI,
22549   IX86_BUILTIN_VEC_EXT_V2DF,
22550   IX86_BUILTIN_VEC_EXT_V2DI,
22551   IX86_BUILTIN_VEC_EXT_V4SF,
22552   IX86_BUILTIN_VEC_EXT_V4SI,
22553   IX86_BUILTIN_VEC_EXT_V8HI,
22554   IX86_BUILTIN_VEC_EXT_V2SI,
22555   IX86_BUILTIN_VEC_EXT_V4HI,
22556   IX86_BUILTIN_VEC_EXT_V16QI,
22557   IX86_BUILTIN_VEC_SET_V2DI,
22558   IX86_BUILTIN_VEC_SET_V4SF,
22559   IX86_BUILTIN_VEC_SET_V4SI,
22560   IX86_BUILTIN_VEC_SET_V8HI,
22561   IX86_BUILTIN_VEC_SET_V4HI,
22562   IX86_BUILTIN_VEC_SET_V16QI,
22563
22564   IX86_BUILTIN_VEC_PACK_SFIX,
22565
22566   /* SSE4.2.  */
22567   IX86_BUILTIN_CRC32QI,
22568   IX86_BUILTIN_CRC32HI,
22569   IX86_BUILTIN_CRC32SI,
22570   IX86_BUILTIN_CRC32DI,
22571
22572   IX86_BUILTIN_PCMPESTRI128,
22573   IX86_BUILTIN_PCMPESTRM128,
22574   IX86_BUILTIN_PCMPESTRA128,
22575   IX86_BUILTIN_PCMPESTRC128,
22576   IX86_BUILTIN_PCMPESTRO128,
22577   IX86_BUILTIN_PCMPESTRS128,
22578   IX86_BUILTIN_PCMPESTRZ128,
22579   IX86_BUILTIN_PCMPISTRI128,
22580   IX86_BUILTIN_PCMPISTRM128,
22581   IX86_BUILTIN_PCMPISTRA128,
22582   IX86_BUILTIN_PCMPISTRC128,
22583   IX86_BUILTIN_PCMPISTRO128,
22584   IX86_BUILTIN_PCMPISTRS128,
22585   IX86_BUILTIN_PCMPISTRZ128,
22586
22587   IX86_BUILTIN_PCMPGTQ,
22588
22589   /* AES instructions */
22590   IX86_BUILTIN_AESENC128,
22591   IX86_BUILTIN_AESENCLAST128,
22592   IX86_BUILTIN_AESDEC128,
22593   IX86_BUILTIN_AESDECLAST128,
22594   IX86_BUILTIN_AESIMC128,
22595   IX86_BUILTIN_AESKEYGENASSIST128,
22596
22597   /* PCLMUL instruction */
22598   IX86_BUILTIN_PCLMULQDQ128,
22599
22600   /* AVX */
22601   IX86_BUILTIN_ADDPD256,
22602   IX86_BUILTIN_ADDPS256,
22603   IX86_BUILTIN_ADDSUBPD256,
22604   IX86_BUILTIN_ADDSUBPS256,
22605   IX86_BUILTIN_ANDPD256,
22606   IX86_BUILTIN_ANDPS256,
22607   IX86_BUILTIN_ANDNPD256,
22608   IX86_BUILTIN_ANDNPS256,
22609   IX86_BUILTIN_BLENDPD256,
22610   IX86_BUILTIN_BLENDPS256,
22611   IX86_BUILTIN_BLENDVPD256,
22612   IX86_BUILTIN_BLENDVPS256,
22613   IX86_BUILTIN_DIVPD256,
22614   IX86_BUILTIN_DIVPS256,
22615   IX86_BUILTIN_DPPS256,
22616   IX86_BUILTIN_HADDPD256,
22617   IX86_BUILTIN_HADDPS256,
22618   IX86_BUILTIN_HSUBPD256,
22619   IX86_BUILTIN_HSUBPS256,
22620   IX86_BUILTIN_MAXPD256,
22621   IX86_BUILTIN_MAXPS256,
22622   IX86_BUILTIN_MINPD256,
22623   IX86_BUILTIN_MINPS256,
22624   IX86_BUILTIN_MULPD256,
22625   IX86_BUILTIN_MULPS256,
22626   IX86_BUILTIN_ORPD256,
22627   IX86_BUILTIN_ORPS256,
22628   IX86_BUILTIN_SHUFPD256,
22629   IX86_BUILTIN_SHUFPS256,
22630   IX86_BUILTIN_SUBPD256,
22631   IX86_BUILTIN_SUBPS256,
22632   IX86_BUILTIN_XORPD256,
22633   IX86_BUILTIN_XORPS256,
22634   IX86_BUILTIN_CMPSD,
22635   IX86_BUILTIN_CMPSS,
22636   IX86_BUILTIN_CMPPD,
22637   IX86_BUILTIN_CMPPS,
22638   IX86_BUILTIN_CMPPD256,
22639   IX86_BUILTIN_CMPPS256,
22640   IX86_BUILTIN_CVTDQ2PD256,
22641   IX86_BUILTIN_CVTDQ2PS256,
22642   IX86_BUILTIN_CVTPD2PS256,
22643   IX86_BUILTIN_CVTPS2DQ256,
22644   IX86_BUILTIN_CVTPS2PD256,
22645   IX86_BUILTIN_CVTTPD2DQ256,
22646   IX86_BUILTIN_CVTPD2DQ256,
22647   IX86_BUILTIN_CVTTPS2DQ256,
22648   IX86_BUILTIN_EXTRACTF128PD256,
22649   IX86_BUILTIN_EXTRACTF128PS256,
22650   IX86_BUILTIN_EXTRACTF128SI256,
22651   IX86_BUILTIN_VZEROALL,
22652   IX86_BUILTIN_VZEROUPPER,
22653   IX86_BUILTIN_VPERMILVARPD,
22654   IX86_BUILTIN_VPERMILVARPS,
22655   IX86_BUILTIN_VPERMILVARPD256,
22656   IX86_BUILTIN_VPERMILVARPS256,
22657   IX86_BUILTIN_VPERMILPD,
22658   IX86_BUILTIN_VPERMILPS,
22659   IX86_BUILTIN_VPERMILPD256,
22660   IX86_BUILTIN_VPERMILPS256,
22661   IX86_BUILTIN_VPERMIL2PD,
22662   IX86_BUILTIN_VPERMIL2PS,
22663   IX86_BUILTIN_VPERMIL2PD256,
22664   IX86_BUILTIN_VPERMIL2PS256,
22665   IX86_BUILTIN_VPERM2F128PD256,
22666   IX86_BUILTIN_VPERM2F128PS256,
22667   IX86_BUILTIN_VPERM2F128SI256,
22668   IX86_BUILTIN_VBROADCASTSS,
22669   IX86_BUILTIN_VBROADCASTSD256,
22670   IX86_BUILTIN_VBROADCASTSS256,
22671   IX86_BUILTIN_VBROADCASTPD256,
22672   IX86_BUILTIN_VBROADCASTPS256,
22673   IX86_BUILTIN_VINSERTF128PD256,
22674   IX86_BUILTIN_VINSERTF128PS256,
22675   IX86_BUILTIN_VINSERTF128SI256,
22676   IX86_BUILTIN_LOADUPD256,
22677   IX86_BUILTIN_LOADUPS256,
22678   IX86_BUILTIN_STOREUPD256,
22679   IX86_BUILTIN_STOREUPS256,
22680   IX86_BUILTIN_LDDQU256,
22681   IX86_BUILTIN_MOVNTDQ256,
22682   IX86_BUILTIN_MOVNTPD256,
22683   IX86_BUILTIN_MOVNTPS256,
22684   IX86_BUILTIN_LOADDQU256,
22685   IX86_BUILTIN_STOREDQU256,
22686   IX86_BUILTIN_MASKLOADPD,
22687   IX86_BUILTIN_MASKLOADPS,
22688   IX86_BUILTIN_MASKSTOREPD,
22689   IX86_BUILTIN_MASKSTOREPS,
22690   IX86_BUILTIN_MASKLOADPD256,
22691   IX86_BUILTIN_MASKLOADPS256,
22692   IX86_BUILTIN_MASKSTOREPD256,
22693   IX86_BUILTIN_MASKSTOREPS256,
22694   IX86_BUILTIN_MOVSHDUP256,
22695   IX86_BUILTIN_MOVSLDUP256,
22696   IX86_BUILTIN_MOVDDUP256,
22697
22698   IX86_BUILTIN_SQRTPD256,
22699   IX86_BUILTIN_SQRTPS256,
22700   IX86_BUILTIN_SQRTPS_NR256,
22701   IX86_BUILTIN_RSQRTPS256,
22702   IX86_BUILTIN_RSQRTPS_NR256,
22703
22704   IX86_BUILTIN_RCPPS256,
22705
22706   IX86_BUILTIN_ROUNDPD256,
22707   IX86_BUILTIN_ROUNDPS256,
22708
22709   IX86_BUILTIN_UNPCKHPD256,
22710   IX86_BUILTIN_UNPCKLPD256,
22711   IX86_BUILTIN_UNPCKHPS256,
22712   IX86_BUILTIN_UNPCKLPS256,
22713
22714   IX86_BUILTIN_SI256_SI,
22715   IX86_BUILTIN_PS256_PS,
22716   IX86_BUILTIN_PD256_PD,
22717   IX86_BUILTIN_SI_SI256,
22718   IX86_BUILTIN_PS_PS256,
22719   IX86_BUILTIN_PD_PD256,
22720
22721   IX86_BUILTIN_VTESTZPD,
22722   IX86_BUILTIN_VTESTCPD,
22723   IX86_BUILTIN_VTESTNZCPD,
22724   IX86_BUILTIN_VTESTZPS,
22725   IX86_BUILTIN_VTESTCPS,
22726   IX86_BUILTIN_VTESTNZCPS,
22727   IX86_BUILTIN_VTESTZPD256,
22728   IX86_BUILTIN_VTESTCPD256,
22729   IX86_BUILTIN_VTESTNZCPD256,
22730   IX86_BUILTIN_VTESTZPS256,
22731   IX86_BUILTIN_VTESTCPS256,
22732   IX86_BUILTIN_VTESTNZCPS256,
22733   IX86_BUILTIN_PTESTZ256,
22734   IX86_BUILTIN_PTESTC256,
22735   IX86_BUILTIN_PTESTNZC256,
22736
22737   IX86_BUILTIN_MOVMSKPD256,
22738   IX86_BUILTIN_MOVMSKPS256,
22739
22740   /* TFmode support builtins.  */
22741   IX86_BUILTIN_INFQ,
22742   IX86_BUILTIN_HUGE_VALQ,
22743   IX86_BUILTIN_FABSQ,
22744   IX86_BUILTIN_COPYSIGNQ,
22745
22746   /* Vectorizer support builtins.  */
22747   IX86_BUILTIN_CPYSGNPS,
22748   IX86_BUILTIN_CPYSGNPD,
22749   IX86_BUILTIN_CPYSGNPS256,
22750   IX86_BUILTIN_CPYSGNPD256,
22751
22752   IX86_BUILTIN_CVTUDQ2PS,
22753
22754   IX86_BUILTIN_VEC_PERM_V2DF,
22755   IX86_BUILTIN_VEC_PERM_V4SF,
22756   IX86_BUILTIN_VEC_PERM_V2DI,
22757   IX86_BUILTIN_VEC_PERM_V4SI,
22758   IX86_BUILTIN_VEC_PERM_V8HI,
22759   IX86_BUILTIN_VEC_PERM_V16QI,
22760   IX86_BUILTIN_VEC_PERM_V2DI_U,
22761   IX86_BUILTIN_VEC_PERM_V4SI_U,
22762   IX86_BUILTIN_VEC_PERM_V8HI_U,
22763   IX86_BUILTIN_VEC_PERM_V16QI_U,
22764   IX86_BUILTIN_VEC_PERM_V4DF,
22765   IX86_BUILTIN_VEC_PERM_V8SF,
22766
22767   /* FMA4 and XOP instructions.  */
22768   IX86_BUILTIN_VFMADDSS,
22769   IX86_BUILTIN_VFMADDSD,
22770   IX86_BUILTIN_VFMADDPS,
22771   IX86_BUILTIN_VFMADDPD,
22772   IX86_BUILTIN_VFMADDPS256,
22773   IX86_BUILTIN_VFMADDPD256,
22774   IX86_BUILTIN_VFMADDSUBPS,
22775   IX86_BUILTIN_VFMADDSUBPD,
22776   IX86_BUILTIN_VFMADDSUBPS256,
22777   IX86_BUILTIN_VFMADDSUBPD256,
22778
22779   IX86_BUILTIN_VPCMOV,
22780   IX86_BUILTIN_VPCMOV_V2DI,
22781   IX86_BUILTIN_VPCMOV_V4SI,
22782   IX86_BUILTIN_VPCMOV_V8HI,
22783   IX86_BUILTIN_VPCMOV_V16QI,
22784   IX86_BUILTIN_VPCMOV_V4SF,
22785   IX86_BUILTIN_VPCMOV_V2DF,
22786   IX86_BUILTIN_VPCMOV256,
22787   IX86_BUILTIN_VPCMOV_V4DI256,
22788   IX86_BUILTIN_VPCMOV_V8SI256,
22789   IX86_BUILTIN_VPCMOV_V16HI256,
22790   IX86_BUILTIN_VPCMOV_V32QI256,
22791   IX86_BUILTIN_VPCMOV_V8SF256,
22792   IX86_BUILTIN_VPCMOV_V4DF256,
22793
22794   IX86_BUILTIN_VPPERM,
22795
22796   IX86_BUILTIN_VPMACSSWW,
22797   IX86_BUILTIN_VPMACSWW,
22798   IX86_BUILTIN_VPMACSSWD,
22799   IX86_BUILTIN_VPMACSWD,
22800   IX86_BUILTIN_VPMACSSDD,
22801   IX86_BUILTIN_VPMACSDD,
22802   IX86_BUILTIN_VPMACSSDQL,
22803   IX86_BUILTIN_VPMACSSDQH,
22804   IX86_BUILTIN_VPMACSDQL,
22805   IX86_BUILTIN_VPMACSDQH,
22806   IX86_BUILTIN_VPMADCSSWD,
22807   IX86_BUILTIN_VPMADCSWD,
22808
22809   IX86_BUILTIN_VPHADDBW,
22810   IX86_BUILTIN_VPHADDBD,
22811   IX86_BUILTIN_VPHADDBQ,
22812   IX86_BUILTIN_VPHADDWD,
22813   IX86_BUILTIN_VPHADDWQ,
22814   IX86_BUILTIN_VPHADDDQ,
22815   IX86_BUILTIN_VPHADDUBW,
22816   IX86_BUILTIN_VPHADDUBD,
22817   IX86_BUILTIN_VPHADDUBQ,
22818   IX86_BUILTIN_VPHADDUWD,
22819   IX86_BUILTIN_VPHADDUWQ,
22820   IX86_BUILTIN_VPHADDUDQ,
22821   IX86_BUILTIN_VPHSUBBW,
22822   IX86_BUILTIN_VPHSUBWD,
22823   IX86_BUILTIN_VPHSUBDQ,
22824
22825   IX86_BUILTIN_VPROTB,
22826   IX86_BUILTIN_VPROTW,
22827   IX86_BUILTIN_VPROTD,
22828   IX86_BUILTIN_VPROTQ,
22829   IX86_BUILTIN_VPROTB_IMM,
22830   IX86_BUILTIN_VPROTW_IMM,
22831   IX86_BUILTIN_VPROTD_IMM,
22832   IX86_BUILTIN_VPROTQ_IMM,
22833
22834   IX86_BUILTIN_VPSHLB,
22835   IX86_BUILTIN_VPSHLW,
22836   IX86_BUILTIN_VPSHLD,
22837   IX86_BUILTIN_VPSHLQ,
22838   IX86_BUILTIN_VPSHAB,
22839   IX86_BUILTIN_VPSHAW,
22840   IX86_BUILTIN_VPSHAD,
22841   IX86_BUILTIN_VPSHAQ,
22842
22843   IX86_BUILTIN_VFRCZSS,
22844   IX86_BUILTIN_VFRCZSD,
22845   IX86_BUILTIN_VFRCZPS,
22846   IX86_BUILTIN_VFRCZPD,
22847   IX86_BUILTIN_VFRCZPS256,
22848   IX86_BUILTIN_VFRCZPD256,
22849
22850   IX86_BUILTIN_VPCOMEQUB,
22851   IX86_BUILTIN_VPCOMNEUB,
22852   IX86_BUILTIN_VPCOMLTUB,
22853   IX86_BUILTIN_VPCOMLEUB,
22854   IX86_BUILTIN_VPCOMGTUB,
22855   IX86_BUILTIN_VPCOMGEUB,
22856   IX86_BUILTIN_VPCOMFALSEUB,
22857   IX86_BUILTIN_VPCOMTRUEUB,
22858
22859   IX86_BUILTIN_VPCOMEQUW,
22860   IX86_BUILTIN_VPCOMNEUW,
22861   IX86_BUILTIN_VPCOMLTUW,
22862   IX86_BUILTIN_VPCOMLEUW,
22863   IX86_BUILTIN_VPCOMGTUW,
22864   IX86_BUILTIN_VPCOMGEUW,
22865   IX86_BUILTIN_VPCOMFALSEUW,
22866   IX86_BUILTIN_VPCOMTRUEUW,
22867
22868   IX86_BUILTIN_VPCOMEQUD,
22869   IX86_BUILTIN_VPCOMNEUD,
22870   IX86_BUILTIN_VPCOMLTUD,
22871   IX86_BUILTIN_VPCOMLEUD,
22872   IX86_BUILTIN_VPCOMGTUD,
22873   IX86_BUILTIN_VPCOMGEUD,
22874   IX86_BUILTIN_VPCOMFALSEUD,
22875   IX86_BUILTIN_VPCOMTRUEUD,
22876
22877   IX86_BUILTIN_VPCOMEQUQ,
22878   IX86_BUILTIN_VPCOMNEUQ,
22879   IX86_BUILTIN_VPCOMLTUQ,
22880   IX86_BUILTIN_VPCOMLEUQ,
22881   IX86_BUILTIN_VPCOMGTUQ,
22882   IX86_BUILTIN_VPCOMGEUQ,
22883   IX86_BUILTIN_VPCOMFALSEUQ,
22884   IX86_BUILTIN_VPCOMTRUEUQ,
22885
22886   IX86_BUILTIN_VPCOMEQB,
22887   IX86_BUILTIN_VPCOMNEB,
22888   IX86_BUILTIN_VPCOMLTB,
22889   IX86_BUILTIN_VPCOMLEB,
22890   IX86_BUILTIN_VPCOMGTB,
22891   IX86_BUILTIN_VPCOMGEB,
22892   IX86_BUILTIN_VPCOMFALSEB,
22893   IX86_BUILTIN_VPCOMTRUEB,
22894
22895   IX86_BUILTIN_VPCOMEQW,
22896   IX86_BUILTIN_VPCOMNEW,
22897   IX86_BUILTIN_VPCOMLTW,
22898   IX86_BUILTIN_VPCOMLEW,
22899   IX86_BUILTIN_VPCOMGTW,
22900   IX86_BUILTIN_VPCOMGEW,
22901   IX86_BUILTIN_VPCOMFALSEW,
22902   IX86_BUILTIN_VPCOMTRUEW,
22903
22904   IX86_BUILTIN_VPCOMEQD,
22905   IX86_BUILTIN_VPCOMNED,
22906   IX86_BUILTIN_VPCOMLTD,
22907   IX86_BUILTIN_VPCOMLED,
22908   IX86_BUILTIN_VPCOMGTD,
22909   IX86_BUILTIN_VPCOMGED,
22910   IX86_BUILTIN_VPCOMFALSED,
22911   IX86_BUILTIN_VPCOMTRUED,
22912
22913   IX86_BUILTIN_VPCOMEQQ,
22914   IX86_BUILTIN_VPCOMNEQ,
22915   IX86_BUILTIN_VPCOMLTQ,
22916   IX86_BUILTIN_VPCOMLEQ,
22917   IX86_BUILTIN_VPCOMGTQ,
22918   IX86_BUILTIN_VPCOMGEQ,
22919   IX86_BUILTIN_VPCOMFALSEQ,
22920   IX86_BUILTIN_VPCOMTRUEQ,
22921
22922   /* LWP instructions.  */
22923   IX86_BUILTIN_LLWPCB,
22924   IX86_BUILTIN_SLWPCB,
22925   IX86_BUILTIN_LWPVAL32,
22926   IX86_BUILTIN_LWPVAL64,
22927   IX86_BUILTIN_LWPINS32,
22928   IX86_BUILTIN_LWPINS64,
22929
22930   IX86_BUILTIN_CLZS,
22931
22932   /* FSGSBASE instructions.  */
22933   IX86_BUILTIN_RDFSBASE32,
22934   IX86_BUILTIN_RDFSBASE64,
22935   IX86_BUILTIN_RDGSBASE32,
22936   IX86_BUILTIN_RDGSBASE64,
22937   IX86_BUILTIN_WRFSBASE32,
22938   IX86_BUILTIN_WRFSBASE64,
22939   IX86_BUILTIN_WRGSBASE32,
22940   IX86_BUILTIN_WRGSBASE64,
22941
22942   /* RDRND instructions.  */
22943   IX86_BUILTIN_RDRAND16,
22944   IX86_BUILTIN_RDRAND32,
22945   IX86_BUILTIN_RDRAND64,
22946
22947   /* F16C instructions.  */
22948   IX86_BUILTIN_CVTPH2PS,
22949   IX86_BUILTIN_CVTPH2PS256,
22950   IX86_BUILTIN_CVTPS2PH,
22951   IX86_BUILTIN_CVTPS2PH256,
22952
22953   IX86_BUILTIN_MAX
22954 };
22955
22956 /* Table for the ix86 builtin decls.  */
22957 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
22958
22959 /* Table of all of the builtin functions that are possible with different ISA's
22960    but are waiting to be built until a function is declared to use that
22961    ISA.  */
22962 struct builtin_isa {
22963   const char *name;             /* function name */
22964   enum ix86_builtin_func_type tcode; /* type to use in the declaration */
22965   int isa;                      /* isa_flags this builtin is defined for */
22966   bool const_p;                 /* true if the declaration is constant */
22967   bool set_and_not_built_p;
22968 };
22969
22970 static struct builtin_isa ix86_builtins_isa[(int) IX86_BUILTIN_MAX];
22971
22972
22973 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Save the MASK
22974    of which isa_flags to use in the ix86_builtins_isa array.  Stores the
22975    function decl in the ix86_builtins array.  Returns the function decl or
22976    NULL_TREE, if the builtin was not added.
22977
22978    If the front end has a special hook for builtin functions, delay adding
22979    builtin functions that aren't in the current ISA until the ISA is changed
22980    with function specific optimization.  Doing so, can save about 300K for the
22981    default compiler.  When the builtin is expanded, check at that time whether
22982    it is valid.
22983
22984    If the front end doesn't have a special hook, record all builtins, even if
22985    it isn't an instruction set in the current ISA in case the user uses
22986    function specific options for a different ISA, so that we don't get scope
22987    errors if a builtin is added in the middle of a function scope.  */
22988
22989 static inline tree
22990 def_builtin (int mask, const char *name, enum ix86_builtin_func_type tcode,
22991              enum ix86_builtins code)
22992 {
22993   tree decl = NULL_TREE;
22994
22995   if (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT)
22996     {
22997       ix86_builtins_isa[(int) code].isa = mask;
22998
22999       mask &= ~OPTION_MASK_ISA_64BIT;
23000       if (mask == 0
23001           || (mask & ix86_isa_flags) != 0
23002           || (lang_hooks.builtin_function
23003               == lang_hooks.builtin_function_ext_scope))
23004
23005         {
23006           tree type = ix86_get_builtin_func_type (tcode);
23007           decl = add_builtin_function (name, type, code, BUILT_IN_MD,
23008                                        NULL, NULL_TREE);
23009           ix86_builtins[(int) code] = decl;
23010           ix86_builtins_isa[(int) code].set_and_not_built_p = false;
23011         }
23012       else
23013         {
23014           ix86_builtins[(int) code] = NULL_TREE;
23015           ix86_builtins_isa[(int) code].tcode = tcode;
23016           ix86_builtins_isa[(int) code].name = name;
23017           ix86_builtins_isa[(int) code].const_p = false;
23018           ix86_builtins_isa[(int) code].set_and_not_built_p = true;
23019         }
23020     }
23021
23022   return decl;
23023 }
23024
23025 /* Like def_builtin, but also marks the function decl "const".  */
23026
23027 static inline tree
23028 def_builtin_const (int mask, const char *name,
23029                    enum ix86_builtin_func_type tcode, enum ix86_builtins code)
23030 {
23031   tree decl = def_builtin (mask, name, tcode, code);
23032   if (decl)
23033     TREE_READONLY (decl) = 1;
23034   else
23035     ix86_builtins_isa[(int) code].const_p = true;
23036
23037   return decl;
23038 }
23039
23040 /* Add any new builtin functions for a given ISA that may not have been
23041    declared.  This saves a bit of space compared to adding all of the
23042    declarations to the tree, even if we didn't use them.  */
23043
23044 static void
23045 ix86_add_new_builtins (int isa)
23046 {
23047   int i;
23048
23049   for (i = 0; i < (int)IX86_BUILTIN_MAX; i++)
23050     {
23051       if ((ix86_builtins_isa[i].isa & isa) != 0
23052           && ix86_builtins_isa[i].set_and_not_built_p)
23053         {
23054           tree decl, type;
23055
23056           /* Don't define the builtin again.  */
23057           ix86_builtins_isa[i].set_and_not_built_p = false;
23058
23059           type = ix86_get_builtin_func_type (ix86_builtins_isa[i].tcode);
23060           decl = add_builtin_function_ext_scope (ix86_builtins_isa[i].name,
23061                                                  type, i, BUILT_IN_MD, NULL,
23062                                                  NULL_TREE);
23063
23064           ix86_builtins[i] = decl;
23065           if (ix86_builtins_isa[i].const_p)
23066             TREE_READONLY (decl) = 1;
23067         }
23068     }
23069 }
23070
23071 /* Bits for builtin_description.flag.  */
23072
23073 /* Set when we don't support the comparison natively, and should
23074    swap_comparison in order to support it.  */
23075 #define BUILTIN_DESC_SWAP_OPERANDS      1
23076
23077 struct builtin_description
23078 {
23079   const unsigned int mask;
23080   const enum insn_code icode;
23081   const char *const name;
23082   const enum ix86_builtins code;
23083   const enum rtx_code comparison;
23084   const int flag;
23085 };
23086
23087 static const struct builtin_description bdesc_comi[] =
23088 {
23089   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
23090   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
23091   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
23092   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
23093   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
23094   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
23095   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
23096   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
23097   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
23098   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
23099   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
23100   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
23101   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
23102   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
23103   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
23104   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
23105   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
23106   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
23107   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
23108   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
23109   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
23110   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
23111   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
23112   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
23113 };
23114
23115 static const struct builtin_description bdesc_pcmpestr[] =
23116 {
23117   /* SSE4.2 */
23118   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
23119   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
23120   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
23121   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
23122   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
23123   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
23124   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
23125 };
23126
23127 static const struct builtin_description bdesc_pcmpistr[] =
23128 {
23129   /* SSE4.2 */
23130   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
23131   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
23132   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
23133   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
23134   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
23135   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
23136   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
23137 };
23138
23139 /* Special builtins with variable number of arguments.  */
23140 static const struct builtin_description bdesc_special_args[] =
23141 {
23142   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtsc, "__builtin_ia32_rdtsc", IX86_BUILTIN_RDTSC, UNKNOWN, (int) UINT64_FTYPE_VOID },
23143   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdtscp, "__builtin_ia32_rdtscp", IX86_BUILTIN_RDTSCP, UNKNOWN, (int) UINT64_FTYPE_PUNSIGNED },
23144
23145   /* MMX */
23146   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_emms, "__builtin_ia32_emms", IX86_BUILTIN_EMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
23147
23148   /* 3DNow! */
23149   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_femms, "__builtin_ia32_femms", IX86_BUILTIN_FEMMS, UNKNOWN, (int) VOID_FTYPE_VOID },
23150
23151   /* SSE */
23152   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_storeups", IX86_BUILTIN_STOREUPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
23153   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movntv4sf, "__builtin_ia32_movntps", IX86_BUILTIN_MOVNTPS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
23154   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movups, "__builtin_ia32_loadups", IX86_BUILTIN_LOADUPS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
23155
23156   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadhps_exp, "__builtin_ia32_loadhps", IX86_BUILTIN_LOADHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
23157   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_loadlps_exp, "__builtin_ia32_loadlps", IX86_BUILTIN_LOADLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_PCV2SF },
23158   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storehps, "__builtin_ia32_storehps", IX86_BUILTIN_STOREHPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
23159   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_storelps, "__builtin_ia32_storelps", IX86_BUILTIN_STORELPS, UNKNOWN, (int) VOID_FTYPE_PV2SF_V4SF },
23160
23161   /* SSE or 3DNow!A  */
23162   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_sse_sfence, "__builtin_ia32_sfence", IX86_BUILTIN_SFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
23163   { 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 },
23164
23165   /* SSE2 */
23166   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lfence, "__builtin_ia32_lfence", IX86_BUILTIN_LFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
23167   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_mfence, 0, IX86_BUILTIN_MFENCE, UNKNOWN, (int) VOID_FTYPE_VOID },
23168   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_storeupd", IX86_BUILTIN_STOREUPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
23169   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_storedqu", IX86_BUILTIN_STOREDQU, UNKNOWN, (int) VOID_FTYPE_PCHAR_V16QI },
23170   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2df, "__builtin_ia32_movntpd", IX86_BUILTIN_MOVNTPD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
23171   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntv2di, "__builtin_ia32_movntdq", IX86_BUILTIN_MOVNTDQ, UNKNOWN, (int) VOID_FTYPE_PV2DI_V2DI },
23172   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movntsi, "__builtin_ia32_movnti", IX86_BUILTIN_MOVNTI, UNKNOWN, (int) VOID_FTYPE_PINT_INT },
23173   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movupd, "__builtin_ia32_loadupd", IX86_BUILTIN_LOADUPD, UNKNOWN, (int) V2DF_FTYPE_PCDOUBLE },
23174   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movdqu, "__builtin_ia32_loaddqu", IX86_BUILTIN_LOADDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
23175
23176   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadhpd_exp, "__builtin_ia32_loadhpd", IX86_BUILTIN_LOADHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
23177   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_loadlpd_exp, "__builtin_ia32_loadlpd", IX86_BUILTIN_LOADLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_PCDOUBLE },
23178
23179   /* SSE3 */
23180   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_lddqu, "__builtin_ia32_lddqu", IX86_BUILTIN_LDDQU, UNKNOWN, (int) V16QI_FTYPE_PCCHAR },
23181
23182   /* SSE4.1 */
23183   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_movntdqa, "__builtin_ia32_movntdqa", IX86_BUILTIN_MOVNTDQA, UNKNOWN, (int) V2DI_FTYPE_PV2DI },
23184
23185   /* SSE4A */
23186   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv2df, "__builtin_ia32_movntsd", IX86_BUILTIN_MOVNTSD, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V2DF },
23187   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_vmmovntv4sf, "__builtin_ia32_movntss", IX86_BUILTIN_MOVNTSS, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V4SF },
23188
23189   /* AVX */
23190   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroall, "__builtin_ia32_vzeroall", IX86_BUILTIN_VZEROALL, UNKNOWN, (int) VOID_FTYPE_VOID },
23191   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vzeroupper, "__builtin_ia32_vzeroupper", IX86_BUILTIN_VZEROUPPER, UNKNOWN, (int) VOID_FTYPE_VOID },
23192
23193   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4sf, "__builtin_ia32_vbroadcastss", IX86_BUILTIN_VBROADCASTSS, UNKNOWN, (int) V4SF_FTYPE_PCFLOAT },
23194   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv4df, "__builtin_ia32_vbroadcastsd256", IX86_BUILTIN_VBROADCASTSD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
23195   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_dupv8sf, "__builtin_ia32_vbroadcastss256", IX86_BUILTIN_VBROADCASTSS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
23196   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v4df, "__builtin_ia32_vbroadcastf128_pd256", IX86_BUILTIN_VBROADCASTPD256, UNKNOWN, (int) V4DF_FTYPE_PCV2DF },
23197   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vbroadcastf128_v8sf, "__builtin_ia32_vbroadcastf128_ps256", IX86_BUILTIN_VBROADCASTPS256, UNKNOWN, (int) V8SF_FTYPE_PCV4SF },
23198
23199   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_loadupd256", IX86_BUILTIN_LOADUPD256, UNKNOWN, (int) V4DF_FTYPE_PCDOUBLE },
23200   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_loadups256", IX86_BUILTIN_LOADUPS256, UNKNOWN, (int) V8SF_FTYPE_PCFLOAT },
23201   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movupd256, "__builtin_ia32_storeupd256", IX86_BUILTIN_STOREUPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
23202   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movups256, "__builtin_ia32_storeups256", IX86_BUILTIN_STOREUPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
23203   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_loaddqu256", IX86_BUILTIN_LOADDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
23204   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movdqu256, "__builtin_ia32_storedqu256", IX86_BUILTIN_STOREDQU256, UNKNOWN, (int) VOID_FTYPE_PCHAR_V32QI },
23205   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_lddqu256, "__builtin_ia32_lddqu256", IX86_BUILTIN_LDDQU256, UNKNOWN, (int) V32QI_FTYPE_PCCHAR },
23206
23207   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4di, "__builtin_ia32_movntdq256", IX86_BUILTIN_MOVNTDQ256, UNKNOWN, (int) VOID_FTYPE_PV4DI_V4DI },
23208   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv4df, "__builtin_ia32_movntpd256", IX86_BUILTIN_MOVNTPD256, UNKNOWN, (int) VOID_FTYPE_PDOUBLE_V4DF },
23209   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movntv8sf, "__builtin_ia32_movntps256", IX86_BUILTIN_MOVNTPS256, UNKNOWN, (int) VOID_FTYPE_PFLOAT_V8SF },
23210
23211   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd, "__builtin_ia32_maskloadpd", IX86_BUILTIN_MASKLOADPD, UNKNOWN, (int) V2DF_FTYPE_PCV2DF_V2DF },
23212   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps, "__builtin_ia32_maskloadps", IX86_BUILTIN_MASKLOADPS, UNKNOWN, (int) V4SF_FTYPE_PCV4SF_V4SF },
23213   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadpd256, "__builtin_ia32_maskloadpd256", IX86_BUILTIN_MASKLOADPD256, UNKNOWN, (int) V4DF_FTYPE_PCV4DF_V4DF },
23214   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskloadps256, "__builtin_ia32_maskloadps256", IX86_BUILTIN_MASKLOADPS256, UNKNOWN, (int) V8SF_FTYPE_PCV8SF_V8SF },
23215   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd, "__builtin_ia32_maskstorepd", IX86_BUILTIN_MASKSTOREPD, UNKNOWN, (int) VOID_FTYPE_PV2DF_V2DF_V2DF },
23216   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps, "__builtin_ia32_maskstoreps", IX86_BUILTIN_MASKSTOREPS, UNKNOWN, (int) VOID_FTYPE_PV4SF_V4SF_V4SF },
23217   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstorepd256, "__builtin_ia32_maskstorepd256", IX86_BUILTIN_MASKSTOREPD256, UNKNOWN, (int) VOID_FTYPE_PV4DF_V4DF_V4DF },
23218   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_maskstoreps256, "__builtin_ia32_maskstoreps256", IX86_BUILTIN_MASKSTOREPS256, UNKNOWN, (int) VOID_FTYPE_PV8SF_V8SF_V8SF },
23219
23220   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_llwpcb, "__builtin_ia32_llwpcb", IX86_BUILTIN_LLWPCB, UNKNOWN, (int) VOID_FTYPE_PVOID },
23221   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_slwpcb, "__builtin_ia32_slwpcb", IX86_BUILTIN_SLWPCB, UNKNOWN, (int) PVOID_FTYPE_VOID },
23222   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvalsi3, "__builtin_ia32_lwpval32", IX86_BUILTIN_LWPVAL32, UNKNOWN, (int) VOID_FTYPE_UINT_UINT_UINT },
23223   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpvaldi3, "__builtin_ia32_lwpval64", IX86_BUILTIN_LWPVAL64, UNKNOWN, (int) VOID_FTYPE_UINT64_UINT_UINT },
23224   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinssi3, "__builtin_ia32_lwpins32", IX86_BUILTIN_LWPINS32, UNKNOWN, (int) UCHAR_FTYPE_UINT_UINT_UINT },
23225   { OPTION_MASK_ISA_LWP, CODE_FOR_lwp_lwpinsdi3, "__builtin_ia32_lwpins64", IX86_BUILTIN_LWPINS64, UNKNOWN, (int) UCHAR_FTYPE_UINT64_UINT_UINT },
23226
23227   /* FSGSBASE */
23228   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasesi, "__builtin_ia32_rdfsbase32", IX86_BUILTIN_RDFSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
23229   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdfsbasedi, "__builtin_ia32_rdfsbase64", IX86_BUILTIN_RDFSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
23230   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasesi, "__builtin_ia32_rdgsbase32", IX86_BUILTIN_RDGSBASE32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
23231   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_rdgsbasedi, "__builtin_ia32_rdgsbase64", IX86_BUILTIN_RDGSBASE64, UNKNOWN, (int) UINT64_FTYPE_VOID },
23232   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasesi, "__builtin_ia32_wrfsbase32", IX86_BUILTIN_WRFSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
23233   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrfsbasedi, "__builtin_ia32_wrfsbase64", IX86_BUILTIN_WRFSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
23234   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasesi, "__builtin_ia32_wrgsbase32", IX86_BUILTIN_WRGSBASE32, UNKNOWN, (int) VOID_FTYPE_UNSIGNED },
23235   { OPTION_MASK_ISA_FSGSBASE | OPTION_MASK_ISA_64BIT, CODE_FOR_wrgsbasedi, "__builtin_ia32_wrgsbase64", IX86_BUILTIN_WRGSBASE64, UNKNOWN, (int) VOID_FTYPE_UINT64 },
23236
23237   /* RDRND */
23238   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandhi, "__builtin_ia32_rdrand16", IX86_BUILTIN_RDRAND16, UNKNOWN, (int) UINT16_FTYPE_VOID },
23239   { OPTION_MASK_ISA_RDRND, CODE_FOR_rdrandsi, "__builtin_ia32_rdrand32", IX86_BUILTIN_RDRAND32, UNKNOWN, (int) UNSIGNED_FTYPE_VOID },
23240   { OPTION_MASK_ISA_RDRND | OPTION_MASK_ISA_64BIT, CODE_FOR_rdranddi, "__builtin_ia32_rdrand64", IX86_BUILTIN_RDRAND64, UNKNOWN, (int) UINT64_FTYPE_VOID },
23241 };
23242
23243 /* Builtins with variable number of arguments.  */
23244 static const struct builtin_description bdesc_args[] =
23245 {
23246   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_bsr, "__builtin_ia32_bsrsi", IX86_BUILTIN_BSRSI, UNKNOWN, (int) INT_FTYPE_INT },
23247   { OPTION_MASK_ISA_64BIT, CODE_FOR_bsr_rex64, "__builtin_ia32_bsrdi", IX86_BUILTIN_BSRDI, UNKNOWN, (int) INT64_FTYPE_INT64 },
23248   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rdpmc, "__builtin_ia32_rdpmc", IX86_BUILTIN_RDPMC, UNKNOWN, (int) UINT64_FTYPE_INT },
23249   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlqi3, "__builtin_ia32_rolqi", IX86_BUILTIN_ROLQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
23250   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotlhi3, "__builtin_ia32_rolhi", IX86_BUILTIN_ROLHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
23251   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrqi3, "__builtin_ia32_rorqi", IX86_BUILTIN_RORQI, UNKNOWN, (int) UINT8_FTYPE_UINT8_INT },
23252   { ~OPTION_MASK_ISA_64BIT, CODE_FOR_rotrhi3, "__builtin_ia32_rorhi", IX86_BUILTIN_RORHI, UNKNOWN, (int) UINT16_FTYPE_UINT16_INT },
23253
23254   /* MMX */
23255   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23256   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23257   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23258   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23259   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23260   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23261
23262   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23263   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23264   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23265   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23266   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23267   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23268   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23269   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23270
23271   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23272   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23273
23274   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23275   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andnotv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23276   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23277   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23278
23279   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23280   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23281   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23282   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23283   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23284   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23285
23286   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23287   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23288   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23289   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23290   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI},
23291   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI},
23292
23293   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, "__builtin_ia32_packsswb", IX86_BUILTIN_PACKSSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
23294   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, "__builtin_ia32_packssdw", IX86_BUILTIN_PACKSSDW, UNKNOWN, (int) V4HI_FTYPE_V2SI_V2SI },
23295   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, "__builtin_ia32_packuswb", IX86_BUILTIN_PACKUSWB, UNKNOWN, (int) V8QI_FTYPE_V4HI_V4HI },
23296
23297   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, "__builtin_ia32_pmaddwd", IX86_BUILTIN_PMADDWD, UNKNOWN, (int) V2SI_FTYPE_V4HI_V4HI },
23298
23299   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllwi", IX86_BUILTIN_PSLLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
23300   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslldi", IX86_BUILTIN_PSLLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
23301   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllqi", IX86_BUILTIN_PSLLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
23302   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, "__builtin_ia32_psllw", IX86_BUILTIN_PSLLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
23303   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, "__builtin_ia32_pslld", IX86_BUILTIN_PSLLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
23304   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv1di3, "__builtin_ia32_psllq", IX86_BUILTIN_PSLLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
23305
23306   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlwi", IX86_BUILTIN_PSRLWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
23307   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrldi", IX86_BUILTIN_PSRLDI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
23308   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlqi", IX86_BUILTIN_PSRLQI, UNKNOWN, (int) V1DI_FTYPE_V1DI_SI_COUNT },
23309   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, "__builtin_ia32_psrlw", IX86_BUILTIN_PSRLW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
23310   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, "__builtin_ia32_psrld", IX86_BUILTIN_PSRLD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
23311   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv1di3, "__builtin_ia32_psrlq", IX86_BUILTIN_PSRLQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_COUNT },
23312
23313   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psrawi", IX86_BUILTIN_PSRAWI, UNKNOWN, (int) V4HI_FTYPE_V4HI_SI_COUNT },
23314   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psradi", IX86_BUILTIN_PSRADI, UNKNOWN, (int) V2SI_FTYPE_V2SI_SI_COUNT },
23315   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, "__builtin_ia32_psraw", IX86_BUILTIN_PSRAW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI_COUNT },
23316   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, "__builtin_ia32_psrad", IX86_BUILTIN_PSRAD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI_COUNT },
23317
23318   /* 3DNow! */
23319   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pf2id, "__builtin_ia32_pf2id", IX86_BUILTIN_PF2ID, UNKNOWN, (int) V2SI_FTYPE_V2SF },
23320   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_floatv2si2, "__builtin_ia32_pi2fd", IX86_BUILTIN_PI2FD, UNKNOWN, (int) V2SF_FTYPE_V2SI },
23321   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpv2sf2, "__builtin_ia32_pfrcp", IX86_BUILTIN_PFRCP, UNKNOWN, (int) V2SF_FTYPE_V2SF },
23322   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqrtv2sf2, "__builtin_ia32_pfrsqrt", IX86_BUILTIN_PFRSQRT, UNKNOWN, (int) V2SF_FTYPE_V2SF },
23323
23324   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgusb", IX86_BUILTIN_PAVGUSB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23325   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_haddv2sf3, "__builtin_ia32_pfacc", IX86_BUILTIN_PFACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23326   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_addv2sf3, "__builtin_ia32_pfadd", IX86_BUILTIN_PFADD, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23327   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_eqv2sf3, "__builtin_ia32_pfcmpeq", IX86_BUILTIN_PFCMPEQ, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
23328   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gev2sf3, "__builtin_ia32_pfcmpge", IX86_BUILTIN_PFCMPGE, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
23329   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_gtv2sf3, "__builtin_ia32_pfcmpgt", IX86_BUILTIN_PFCMPGT, UNKNOWN, (int) V2SI_FTYPE_V2SF_V2SF },
23330   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_smaxv2sf3, "__builtin_ia32_pfmax", IX86_BUILTIN_PFMAX, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23331   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_sminv2sf3, "__builtin_ia32_pfmin", IX86_BUILTIN_PFMIN, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23332   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_mulv2sf3, "__builtin_ia32_pfmul", IX86_BUILTIN_PFMUL, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23333   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit1v2sf3, "__builtin_ia32_pfrcpit1", IX86_BUILTIN_PFRCPIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23334   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rcpit2v2sf3, "__builtin_ia32_pfrcpit2", IX86_BUILTIN_PFRCPIT2, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23335   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_rsqit1v2sf3, "__builtin_ia32_pfrsqit1", IX86_BUILTIN_PFRSQIT1, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23336   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subv2sf3, "__builtin_ia32_pfsub", IX86_BUILTIN_PFSUB, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23337   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_subrv2sf3, "__builtin_ia32_pfsubr", IX86_BUILTIN_PFSUBR, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23338   { OPTION_MASK_ISA_3DNOW, CODE_FOR_mmx_pmulhrwv4hi3, "__builtin_ia32_pmulhrw", IX86_BUILTIN_PMULHRW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23339
23340   /* 3DNow!A */
23341   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pf2iw, "__builtin_ia32_pf2iw", IX86_BUILTIN_PF2IW, UNKNOWN, (int) V2SI_FTYPE_V2SF },
23342   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pi2fw, "__builtin_ia32_pi2fw", IX86_BUILTIN_PI2FW, UNKNOWN, (int) V2SF_FTYPE_V2SI },
23343   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2si2, "__builtin_ia32_pswapdsi", IX86_BUILTIN_PSWAPDSI, UNKNOWN, (int) V2SI_FTYPE_V2SI },
23344   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pswapdv2sf2, "__builtin_ia32_pswapdsf", IX86_BUILTIN_PSWAPDSF, UNKNOWN, (int) V2SF_FTYPE_V2SF },
23345   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_hsubv2sf3, "__builtin_ia32_pfnacc", IX86_BUILTIN_PFNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23346   { OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_addsubv2sf3, "__builtin_ia32_pfpnacc", IX86_BUILTIN_PFPNACC, UNKNOWN, (int) V2SF_FTYPE_V2SF_V2SF },
23347
23348   /* SSE */
23349   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, "__builtin_ia32_movmskps", IX86_BUILTIN_MOVMSKPS, UNKNOWN, (int) INT_FTYPE_V4SF },
23350   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, "__builtin_ia32_sqrtps", IX86_BUILTIN_SQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23351   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, "__builtin_ia32_sqrtps_nr", IX86_BUILTIN_SQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23352   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, "__builtin_ia32_rsqrtps", IX86_BUILTIN_RSQRTPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23353   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, "__builtin_ia32_rsqrtps_nr", IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23354   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, "__builtin_ia32_rcpps", IX86_BUILTIN_RCPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23355   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, "__builtin_ia32_cvtps2pi", IX86_BUILTIN_CVTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
23356   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, "__builtin_ia32_cvtss2si", IX86_BUILTIN_CVTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
23357   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, "__builtin_ia32_cvtss2si64", IX86_BUILTIN_CVTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
23358   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, "__builtin_ia32_cvttps2pi", IX86_BUILTIN_CVTTPS2PI, UNKNOWN, (int) V2SI_FTYPE_V4SF },
23359   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, "__builtin_ia32_cvttss2si", IX86_BUILTIN_CVTTSS2SI, UNKNOWN, (int) INT_FTYPE_V4SF },
23360   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, "__builtin_ia32_cvttss2si64", IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, (int) INT64_FTYPE_V4SF },
23361
23362   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_shufps, "__builtin_ia32_shufps", IX86_BUILTIN_SHUFPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23363
23364   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23365   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23366   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23367   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23368   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23369   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23370   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23371   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23372
23373   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
23374   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
23375   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
23376   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
23377   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
23378   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
23379   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
23380   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
23381   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
23382   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
23383   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP},
23384   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
23385   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, (int) V4SF_FTYPE_V4SF_V4SF },
23386   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, (int) V4SF_FTYPE_V4SF_V4SF },
23387   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, (int) V4SF_FTYPE_V4SF_V4SF },
23388   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
23389   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, (int) V4SF_FTYPE_V4SF_V4SF },
23390   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF },
23391   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF },
23392   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
23393   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, (int) V4SF_FTYPE_V4SF_V4SF_SWAP },
23394   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, (int) V4SF_FTYPE_V4SF_V4SF },
23395
23396   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23397   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23398   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23399   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23400
23401   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23402   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_andnotv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23403   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23404   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23405
23406   { OPTION_MASK_ISA_SSE, CODE_FOR_copysignv4sf3,  "__builtin_ia32_copysignps", IX86_BUILTIN_CPYSGNPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23407
23408   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23409   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps_exp,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23410   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps_exp,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23411   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_highv4sf, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23412   { OPTION_MASK_ISA_SSE, CODE_FOR_vec_interleave_lowv4sf, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23413
23414   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, "__builtin_ia32_cvtpi2ps", IX86_BUILTIN_CVTPI2PS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2SI },
23415   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, "__builtin_ia32_cvtsi2ss", IX86_BUILTIN_CVTSI2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_SI },
23416   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, "__builtin_ia32_cvtsi642ss", IX86_BUILTIN_CVTSI642SS, UNKNOWN, V4SF_FTYPE_V4SF_DI },
23417
23418   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtsf2, "__builtin_ia32_rsqrtf", IX86_BUILTIN_RSQRTF, UNKNOWN, (int) FLOAT_FTYPE_FLOAT },
23419
23420   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsqrtv4sf2, "__builtin_ia32_sqrtss", IX86_BUILTIN_SQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
23421   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrsqrtv4sf2, "__builtin_ia32_rsqrtss", IX86_BUILTIN_RSQRTSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
23422   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmrcpv4sf2, "__builtin_ia32_rcpss", IX86_BUILTIN_RCPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_VEC_MERGE },
23423
23424   /* SSE MMX or 3Dnow!A */
23425   { 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 },
23426   { 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 },
23427   { 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 },
23428
23429   { 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 },
23430   { 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 },
23431   { 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 },
23432   { 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 },
23433
23434   { 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 },
23435   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, "__builtin_ia32_pmovmskb", IX86_BUILTIN_PMOVMSKB, UNKNOWN, (int) INT_FTYPE_V8QI },
23436
23437   { 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 },
23438
23439   /* SSE2 */
23440   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_shufpd, "__builtin_ia32_shufpd", IX86_BUILTIN_SHUFPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23441
23442   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2df", IX86_BUILTIN_VEC_PERM_V2DF, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DI },
23443   { OPTION_MASK_ISA_SSE, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4sf", IX86_BUILTIN_VEC_PERM_V4SF, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SI },
23444   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v2di", IX86_BUILTIN_VEC_PERM_V2DI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_V2DI },
23445   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4si", IX86_BUILTIN_VEC_PERM_V4SI, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_V4SI },
23446   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8hi", IX86_BUILTIN_VEC_PERM_V8HI, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_V8HI },
23447   { OPTION_MASK_ISA_SSE2, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v16qi", IX86_BUILTIN_VEC_PERM_V16QI, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
23448   { 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 },
23449   { 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 },
23450   { 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 },
23451   { 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 },
23452   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v4df", IX86_BUILTIN_VEC_PERM_V4DF, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DI },
23453   { OPTION_MASK_ISA_AVX, CODE_FOR_nothing, "__builtin_ia32_vec_perm_v8sf", IX86_BUILTIN_VEC_PERM_V8SF, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SI },
23454
23455   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, "__builtin_ia32_movmskpd", IX86_BUILTIN_MOVMSKPD, UNKNOWN, (int) INT_FTYPE_V2DF  },
23456   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, "__builtin_ia32_pmovmskb128", IX86_BUILTIN_PMOVMSKB128, UNKNOWN, (int) INT_FTYPE_V16QI },
23457   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, "__builtin_ia32_sqrtpd", IX86_BUILTIN_SQRTPD, UNKNOWN, (int) V2DF_FTYPE_V2DF },
23458   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, "__builtin_ia32_cvtdq2pd", IX86_BUILTIN_CVTDQ2PD, UNKNOWN, (int) V2DF_FTYPE_V4SI },
23459   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, "__builtin_ia32_cvtdq2ps", IX86_BUILTIN_CVTDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
23460   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtudq2ps, "__builtin_ia32_cvtudq2ps", IX86_BUILTIN_CVTUDQ2PS, UNKNOWN, (int) V4SF_FTYPE_V4SI },
23461
23462   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, "__builtin_ia32_cvtpd2dq", IX86_BUILTIN_CVTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
23463   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, "__builtin_ia32_cvtpd2pi", IX86_BUILTIN_CVTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
23464   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, "__builtin_ia32_cvtpd2ps", IX86_BUILTIN_CVTPD2PS, UNKNOWN, (int) V4SF_FTYPE_V2DF },
23465   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, "__builtin_ia32_cvttpd2dq", IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, (int) V4SI_FTYPE_V2DF },
23466   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, "__builtin_ia32_cvttpd2pi", IX86_BUILTIN_CVTTPD2PI, UNKNOWN, (int) V2SI_FTYPE_V2DF },
23467
23468   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, "__builtin_ia32_cvtpi2pd", IX86_BUILTIN_CVTPI2PD, UNKNOWN, (int) V2DF_FTYPE_V2SI },
23469
23470   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, "__builtin_ia32_cvtsd2si", IX86_BUILTIN_CVTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
23471   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, "__builtin_ia32_cvttsd2si", IX86_BUILTIN_CVTTSD2SI, UNKNOWN, (int) INT_FTYPE_V2DF },
23472   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, "__builtin_ia32_cvtsd2si64", IX86_BUILTIN_CVTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
23473   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, "__builtin_ia32_cvttsd2si64", IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, (int) INT64_FTYPE_V2DF },
23474
23475   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, "__builtin_ia32_cvtps2dq", IX86_BUILTIN_CVTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
23476   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, "__builtin_ia32_cvtps2pd", IX86_BUILTIN_CVTPS2PD, UNKNOWN, (int) V2DF_FTYPE_V4SF },
23477   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, "__builtin_ia32_cvttps2dq", IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, (int) V4SI_FTYPE_V4SF },
23478
23479   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23480   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23481   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23482   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23483   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23484   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23485   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23486   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23487
23488   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
23489   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
23490   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
23491   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
23492   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP},
23493   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
23494   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
23495   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
23496   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
23497   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
23498   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF_SWAP },
23499   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
23500   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, (int) V2DF_FTYPE_V2DF_V2DF },
23501   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, (int) V2DF_FTYPE_V2DF_V2DF },
23502   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, (int) V2DF_FTYPE_V2DF_V2DF },
23503   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
23504   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, (int) V2DF_FTYPE_V2DF_V2DF },
23505   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, (int) V2DF_FTYPE_V2DF_V2DF },
23506   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, (int) V2DF_FTYPE_V2DF_V2DF },
23507   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, (int) V2DF_FTYPE_V2DF_V2DF },
23508
23509   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23510   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23511   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23512   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23513
23514   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23515   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23516   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23517   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23518
23519   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysignv2df3,  "__builtin_ia32_copysignpd", IX86_BUILTIN_CPYSGNPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23520
23521   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23522   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2df, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23523   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2df, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23524
23525   { 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 },
23526
23527   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23528   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23529   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23530   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23531   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23532   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23533   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23534   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23535
23536   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23537   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23538   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23539   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23540   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23541   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23542   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23543   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23544
23545   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23546   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN,(int) V8HI_FTYPE_V8HI_V8HI },
23547
23548   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23549   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_andnotv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23550   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23551   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23552
23553   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23554   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23555
23556   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23557   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23558   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
23559   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23560   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23561   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI  },
23562
23563   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23564   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23565   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23566   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23567
23568   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv16qi, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23569   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv8hi, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI  },
23570   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv4si, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN,  (int) V4SI_FTYPE_V4SI_V4SI },
23571   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_highv2di, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23572   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv16qi, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23573   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv8hi, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23574   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv4si, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23575   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_interleave_lowv2di, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23576
23577   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
23578   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
23579   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, (int) V16QI_FTYPE_V8HI_V8HI },
23580
23581   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23582   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, "__builtin_ia32_psadbw128", IX86_BUILTIN_PSADBW128, UNKNOWN, (int) V2DI_FTYPE_V16QI_V16QI },
23583
23584   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv1siv1di3, "__builtin_ia32_pmuludq", IX86_BUILTIN_PMULUDQ, UNKNOWN, (int) V1DI_FTYPE_V2SI_V2SI },
23585   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, "__builtin_ia32_pmuludq128", IX86_BUILTIN_PMULUDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
23586
23587   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, "__builtin_ia32_pmaddwd128", IX86_BUILTIN_PMADDWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI_V8HI },
23588
23589   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, "__builtin_ia32_cvtsi2sd", IX86_BUILTIN_CVTSI2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_SI },
23590   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, "__builtin_ia32_cvtsi642sd", IX86_BUILTIN_CVTSI642SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_DI },
23591   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, "__builtin_ia32_cvtsd2ss", IX86_BUILTIN_CVTSD2SS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V2DF },
23592   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, "__builtin_ia32_cvtss2sd", IX86_BUILTIN_CVTSS2SD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V4SF },
23593
23594   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ashlv1ti3, "__builtin_ia32_pslldqi128", IX86_BUILTIN_PSLLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
23595   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllwi128", IX86_BUILTIN_PSLLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
23596   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslldi128", IX86_BUILTIN_PSLLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
23597   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllqi128", IX86_BUILTIN_PSLLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
23598   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, "__builtin_ia32_psllw128", IX86_BUILTIN_PSLLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
23599   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, "__builtin_ia32_pslld128", IX86_BUILTIN_PSLLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
23600   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, "__builtin_ia32_psllq128", IX86_BUILTIN_PSLLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
23601
23602   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_lshrv1ti3, "__builtin_ia32_psrldqi128", IX86_BUILTIN_PSRLDQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT_CONVERT },
23603   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlwi128", IX86_BUILTIN_PSRLWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
23604   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrldi128", IX86_BUILTIN_PSRLDI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
23605   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlqi128", IX86_BUILTIN_PSRLQI128, UNKNOWN, (int) V2DI_FTYPE_V2DI_SI_COUNT },
23606   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, "__builtin_ia32_psrlw128", IX86_BUILTIN_PSRLW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
23607   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, "__builtin_ia32_psrld128", IX86_BUILTIN_PSRLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
23608   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, "__builtin_ia32_psrlq128", IX86_BUILTIN_PSRLQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_COUNT },
23609
23610   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psrawi128", IX86_BUILTIN_PSRAWI128, UNKNOWN, (int) V8HI_FTYPE_V8HI_SI_COUNT },
23611   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psradi128", IX86_BUILTIN_PSRADI128, UNKNOWN, (int) V4SI_FTYPE_V4SI_SI_COUNT },
23612   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, "__builtin_ia32_psraw128", IX86_BUILTIN_PSRAW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_COUNT },
23613   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, "__builtin_ia32_psrad128", IX86_BUILTIN_PSRAD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI_COUNT },
23614
23615   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufd, "__builtin_ia32_pshufd", IX86_BUILTIN_PSHUFD, UNKNOWN, (int) V4SI_FTYPE_V4SI_INT },
23616   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshuflw, "__builtin_ia32_pshuflw", IX86_BUILTIN_PSHUFLW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
23617   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pshufhw, "__builtin_ia32_pshufhw", IX86_BUILTIN_PSHUFHW, UNKNOWN, (int) V8HI_FTYPE_V8HI_INT },
23618
23619   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsqrtv2df2, "__builtin_ia32_sqrtsd", IX86_BUILTIN_SQRTSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_VEC_MERGE },
23620
23621   { OPTION_MASK_ISA_SSE2, CODE_FOR_abstf2, 0, IX86_BUILTIN_FABSQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128 },
23622   { OPTION_MASK_ISA_SSE2, CODE_FOR_copysigntf3, 0, IX86_BUILTIN_COPYSIGNQ, UNKNOWN, (int) FLOAT128_FTYPE_FLOAT128_FLOAT128 },
23623
23624   { OPTION_MASK_ISA_SSE, CODE_FOR_sse2_movq128, "__builtin_ia32_movq128", IX86_BUILTIN_MOVQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
23625
23626   /* SSE2 MMX */
23627   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_addv1di3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
23628   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subv1di3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI },
23629
23630   /* SSE3 */
23631   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF},
23632   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, (int) V4SF_FTYPE_V4SF },
23633
23634   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23635   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23636   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23637   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23638   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF },
23639   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF },
23640
23641   /* SSSE3 */
23642   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI },
23643   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, (int) V8QI_FTYPE_V8QI },
23644   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
23645   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, (int) V4HI_FTYPE_V4HI },
23646   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI },
23647   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, (int) V2SI_FTYPE_V2SI },
23648
23649   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23650   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23651   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23652   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23653   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23654   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23655   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23656   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23657   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23658   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23659   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23660   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23661   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw128, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, (int) V8HI_FTYPE_V16QI_V16QI },
23662   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubsw, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, (int) V4HI_FTYPE_V8QI_V8QI },
23663   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23664   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23665   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23666   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23667   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23668   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, (int) V8QI_FTYPE_V8QI_V8QI },
23669   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23670   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, (int) V4HI_FTYPE_V4HI_V4HI },
23671   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23672   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, (int) V2SI_FTYPE_V2SI_V2SI },
23673
23674   /* SSSE3.  */
23675   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrti, "__builtin_ia32_palignr128", IX86_BUILTIN_PALIGNR128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT_CONVERT },
23676   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_palignrdi, "__builtin_ia32_palignr", IX86_BUILTIN_PALIGNR, UNKNOWN, (int) V1DI_FTYPE_V1DI_V1DI_INT_CONVERT },
23677
23678   /* SSE4.1 */
23679   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23680   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23681   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_V2DF },
23682   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_V4SF },
23683   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23684   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23685   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23686   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_INT },
23687   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI_V16QI },
23688   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI_INT },
23689
23690   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv8qiv8hi2, "__builtin_ia32_pmovsxbw128", IX86_BUILTIN_PMOVSXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
23691   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4qiv4si2, "__builtin_ia32_pmovsxbd128", IX86_BUILTIN_PMOVSXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
23692   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2qiv2di2, "__builtin_ia32_pmovsxbq128", IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
23693   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv4hiv4si2, "__builtin_ia32_pmovsxwd128", IX86_BUILTIN_PMOVSXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
23694   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2hiv2di2, "__builtin_ia32_pmovsxwq128", IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
23695   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_sign_extendv2siv2di2, "__builtin_ia32_pmovsxdq128", IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
23696   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, "__builtin_ia32_pmovzxbw128", IX86_BUILTIN_PMOVZXBW128, UNKNOWN, (int) V8HI_FTYPE_V16QI },
23697   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, "__builtin_ia32_pmovzxbd128", IX86_BUILTIN_PMOVZXBD128, UNKNOWN, (int) V4SI_FTYPE_V16QI },
23698   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, "__builtin_ia32_pmovzxbq128", IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, (int) V2DI_FTYPE_V16QI },
23699   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, "__builtin_ia32_pmovzxwd128", IX86_BUILTIN_PMOVZXWD128, UNKNOWN, (int) V4SI_FTYPE_V8HI },
23700   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, "__builtin_ia32_pmovzxwq128", IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, (int) V2DI_FTYPE_V8HI },
23701   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, "__builtin_ia32_pmovzxdq128", IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI },
23702   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI },
23703
23704   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, (int) V8HI_FTYPE_V4SI_V4SI },
23705   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23706   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23707   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23708   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23709   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23710   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, (int) V16QI_FTYPE_V16QI_V16QI },
23711   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23712   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23713   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, (int) V8HI_FTYPE_V8HI_V8HI },
23714   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, "__builtin_ia32_pmuldq128", IX86_BUILTIN_PMULDQ128, UNKNOWN, (int) V2DI_FTYPE_V4SI_V4SI },
23715   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, (int) V4SI_FTYPE_V4SI_V4SI },
23716
23717   /* SSE4.1 */
23718   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundpd, "__builtin_ia32_roundpd", IX86_BUILTIN_ROUNDPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
23719   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundps, "__builtin_ia32_roundps", IX86_BUILTIN_ROUNDPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
23720   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, "__builtin_ia32_roundsd", IX86_BUILTIN_ROUNDSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23721   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, "__builtin_ia32_roundss", IX86_BUILTIN_ROUNDSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23722
23723   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, (int) INT_FTYPE_V2DI_V2DI_PTEST },
23724   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
23725   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, (int) INT_FTYPE_V2DI_V2DI_PTEST },
23726
23727   /* SSE4.2 */
23728   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23729   { 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 },
23730   { 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 },
23731   { 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 },
23732   { 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 },
23733
23734   /* SSE4A */
23735   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrqi, "__builtin_ia32_extrqi", IX86_BUILTIN_EXTRQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_UINT_UINT },
23736   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_extrq, "__builtin_ia32_extrq", IX86_BUILTIN_EXTRQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V16QI },
23737   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertqi, "__builtin_ia32_insertqi", IX86_BUILTIN_INSERTQI, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_UINT_UINT },
23738   { OPTION_MASK_ISA_SSE4A, CODE_FOR_sse4a_insertq, "__builtin_ia32_insertq", IX86_BUILTIN_INSERTQ, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23739
23740   /* AES */
23741   { OPTION_MASK_ISA_SSE2, CODE_FOR_aeskeygenassist, 0, IX86_BUILTIN_AESKEYGENASSIST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_INT },
23742   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesimc, 0, IX86_BUILTIN_AESIMC128, UNKNOWN, (int) V2DI_FTYPE_V2DI },
23743
23744   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenc, 0, IX86_BUILTIN_AESENC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23745   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesenclast, 0, IX86_BUILTIN_AESENCLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23746   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdec, 0, IX86_BUILTIN_AESDEC128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23747   { OPTION_MASK_ISA_SSE2, CODE_FOR_aesdeclast, 0, IX86_BUILTIN_AESDECLAST128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI },
23748
23749   /* PCLMUL */
23750   { OPTION_MASK_ISA_SSE2, CODE_FOR_pclmulqdq, 0, IX86_BUILTIN_PCLMULQDQ128, UNKNOWN, (int) V2DI_FTYPE_V2DI_V2DI_INT },
23751
23752   /* AVX */
23753   { OPTION_MASK_ISA_AVX, CODE_FOR_addv4df3, "__builtin_ia32_addpd256", IX86_BUILTIN_ADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23754   { OPTION_MASK_ISA_AVX, CODE_FOR_addv8sf3, "__builtin_ia32_addps256", IX86_BUILTIN_ADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23755   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv4df3, "__builtin_ia32_addsubpd256", IX86_BUILTIN_ADDSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23756   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_addsubv8sf3, "__builtin_ia32_addsubps256", IX86_BUILTIN_ADDSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23757   { OPTION_MASK_ISA_AVX, CODE_FOR_andv4df3, "__builtin_ia32_andpd256", IX86_BUILTIN_ANDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23758   { OPTION_MASK_ISA_AVX, CODE_FOR_andv8sf3, "__builtin_ia32_andps256", IX86_BUILTIN_ANDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23759   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv4df3, "__builtin_ia32_andnpd256", IX86_BUILTIN_ANDNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23760   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_andnotv8sf3, "__builtin_ia32_andnps256", IX86_BUILTIN_ANDNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23761   { OPTION_MASK_ISA_AVX, CODE_FOR_divv4df3, "__builtin_ia32_divpd256", IX86_BUILTIN_DIVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23762   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_divv8sf3, "__builtin_ia32_divps256", IX86_BUILTIN_DIVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23763   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv4df3, "__builtin_ia32_haddpd256", IX86_BUILTIN_HADDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23764   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv8sf3, "__builtin_ia32_hsubps256", IX86_BUILTIN_HSUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23765   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_hsubv4df3, "__builtin_ia32_hsubpd256", IX86_BUILTIN_HSUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23766   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_haddv8sf3, "__builtin_ia32_haddps256", IX86_BUILTIN_HADDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23767   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv4df3, "__builtin_ia32_maxpd256", IX86_BUILTIN_MAXPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23768   { OPTION_MASK_ISA_AVX, CODE_FOR_smaxv8sf3, "__builtin_ia32_maxps256", IX86_BUILTIN_MAXPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23769   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv4df3, "__builtin_ia32_minpd256", IX86_BUILTIN_MINPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23770   { OPTION_MASK_ISA_AVX, CODE_FOR_sminv8sf3, "__builtin_ia32_minps256", IX86_BUILTIN_MINPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23771   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv4df3, "__builtin_ia32_mulpd256", IX86_BUILTIN_MULPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23772   { OPTION_MASK_ISA_AVX, CODE_FOR_mulv8sf3, "__builtin_ia32_mulps256", IX86_BUILTIN_MULPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23773   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv4df3, "__builtin_ia32_orpd256", IX86_BUILTIN_ORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23774   { OPTION_MASK_ISA_AVX, CODE_FOR_iorv8sf3, "__builtin_ia32_orps256", IX86_BUILTIN_ORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23775   { OPTION_MASK_ISA_AVX, CODE_FOR_subv4df3, "__builtin_ia32_subpd256", IX86_BUILTIN_SUBPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23776   { OPTION_MASK_ISA_AVX, CODE_FOR_subv8sf3, "__builtin_ia32_subps256", IX86_BUILTIN_SUBPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23777   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv4df3, "__builtin_ia32_xorpd256", IX86_BUILTIN_XORPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23778   { OPTION_MASK_ISA_AVX, CODE_FOR_xorv8sf3, "__builtin_ia32_xorps256", IX86_BUILTIN_XORPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23779
23780   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv2df3, "__builtin_ia32_vpermilvarpd", IX86_BUILTIN_VPERMILVARPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DI },
23781   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4sf3, "__builtin_ia32_vpermilvarps", IX86_BUILTIN_VPERMILVARPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SI },
23782   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv4df3, "__builtin_ia32_vpermilvarpd256", IX86_BUILTIN_VPERMILVARPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DI },
23783   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilvarv8sf3, "__builtin_ia32_vpermilvarps256", IX86_BUILTIN_VPERMILVARPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SI },
23784
23785   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendpd256, "__builtin_ia32_blendpd256", IX86_BUILTIN_BLENDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23786   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendps256, "__builtin_ia32_blendps256", IX86_BUILTIN_BLENDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23787   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvpd256, "__builtin_ia32_blendvpd256", IX86_BUILTIN_BLENDVPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_V4DF },
23788   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_blendvps256, "__builtin_ia32_blendvps256", IX86_BUILTIN_BLENDVPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_V8SF },
23789   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_dpps256, "__builtin_ia32_dpps256", IX86_BUILTIN_DPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23790   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufpd256, "__builtin_ia32_shufpd256", IX86_BUILTIN_SHUFPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23791   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_shufps256, "__builtin_ia32_shufps256", IX86_BUILTIN_SHUFPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23792   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpsdv2df3, "__builtin_ia32_cmpsd", IX86_BUILTIN_CMPSD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23793   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmpssv4sf3, "__builtin_ia32_cmpss", IX86_BUILTIN_CMPSS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23794   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv2df3, "__builtin_ia32_cmppd", IX86_BUILTIN_CMPPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_V2DF_INT },
23795   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv4sf3, "__builtin_ia32_cmpps", IX86_BUILTIN_CMPPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_V4SF_INT },
23796   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppdv4df3, "__builtin_ia32_cmppd256", IX86_BUILTIN_CMPPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23797   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cmppsv8sf3, "__builtin_ia32_cmpps256", IX86_BUILTIN_CMPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23798   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v4df, "__builtin_ia32_vextractf128_pd256", IX86_BUILTIN_EXTRACTF128PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF_INT },
23799   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8sf, "__builtin_ia32_vextractf128_ps256", IX86_BUILTIN_EXTRACTF128PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF_INT },
23800   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vextractf128v8si, "__builtin_ia32_vextractf128_si256", IX86_BUILTIN_EXTRACTF128SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI_INT },
23801   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2pd256, "__builtin_ia32_cvtdq2pd256", IX86_BUILTIN_CVTDQ2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SI },
23802   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtdq2ps256, "__builtin_ia32_cvtdq2ps256", IX86_BUILTIN_CVTDQ2PS256, UNKNOWN, (int) V8SF_FTYPE_V8SI },
23803   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2ps256, "__builtin_ia32_cvtpd2ps256", IX86_BUILTIN_CVTPD2PS256, UNKNOWN, (int) V4SF_FTYPE_V4DF },
23804   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2dq256, "__builtin_ia32_cvtps2dq256", IX86_BUILTIN_CVTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
23805   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtps2pd256, "__builtin_ia32_cvtps2pd256", IX86_BUILTIN_CVTPS2PD256, UNKNOWN, (int) V4DF_FTYPE_V4SF },
23806   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttpd2dq256, "__builtin_ia32_cvttpd2dq256", IX86_BUILTIN_CVTTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
23807   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvtpd2dq256, "__builtin_ia32_cvtpd2dq256", IX86_BUILTIN_CVTPD2DQ256, UNKNOWN, (int) V4SI_FTYPE_V4DF },
23808   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_cvttps2dq256, "__builtin_ia32_cvttps2dq256", IX86_BUILTIN_CVTTPS2DQ256, UNKNOWN, (int) V8SI_FTYPE_V8SF },
23809   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v4df3, "__builtin_ia32_vperm2f128_pd256", IX86_BUILTIN_VPERM2F128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF_INT },
23810   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8sf3, "__builtin_ia32_vperm2f128_ps256", IX86_BUILTIN_VPERM2F128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF_INT },
23811   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vperm2f128v8si3, "__builtin_ia32_vperm2f128_si256", IX86_BUILTIN_VPERM2F128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V8SI_INT },
23812   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv2df, "__builtin_ia32_vpermilpd", IX86_BUILTIN_VPERMILPD, UNKNOWN, (int) V2DF_FTYPE_V2DF_INT },
23813   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4sf, "__builtin_ia32_vpermilps", IX86_BUILTIN_VPERMILPS, UNKNOWN, (int) V4SF_FTYPE_V4SF_INT },
23814   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv4df, "__builtin_ia32_vpermilpd256", IX86_BUILTIN_VPERMILPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
23815   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vpermilv8sf, "__builtin_ia32_vpermilps256", IX86_BUILTIN_VPERMILPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
23816   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v4df, "__builtin_ia32_vinsertf128_pd256", IX86_BUILTIN_VINSERTF128PD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V2DF_INT },
23817   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8sf, "__builtin_ia32_vinsertf128_ps256", IX86_BUILTIN_VINSERTF128PS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V4SF_INT },
23818   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vinsertf128v8si, "__builtin_ia32_vinsertf128_si256", IX86_BUILTIN_VINSERTF128SI256, UNKNOWN, (int) V8SI_FTYPE_V8SI_V4SI_INT },
23819
23820   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movshdup256, "__builtin_ia32_movshdup256", IX86_BUILTIN_MOVSHDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23821   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movsldup256, "__builtin_ia32_movsldup256", IX86_BUILTIN_MOVSLDUP256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23822   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movddup256, "__builtin_ia32_movddup256", IX86_BUILTIN_MOVDDUP256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
23823
23824   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv4df2, "__builtin_ia32_sqrtpd256", IX86_BUILTIN_SQRTPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF },
23825   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_sqrtv8sf2, "__builtin_ia32_sqrtps256", IX86_BUILTIN_SQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23826   { OPTION_MASK_ISA_AVX, CODE_FOR_sqrtv8sf2, "__builtin_ia32_sqrtps_nr256", IX86_BUILTIN_SQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23827   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rsqrtv8sf2, "__builtin_ia32_rsqrtps256", IX86_BUILTIN_RSQRTPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23828   { OPTION_MASK_ISA_AVX, CODE_FOR_rsqrtv8sf2, "__builtin_ia32_rsqrtps_nr256", IX86_BUILTIN_RSQRTPS_NR256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23829
23830   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_rcpv8sf2, "__builtin_ia32_rcpps256", IX86_BUILTIN_RCPPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF },
23831
23832   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundpd256, "__builtin_ia32_roundpd256", IX86_BUILTIN_ROUNDPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_INT },
23833   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_roundps256, "__builtin_ia32_roundps256", IX86_BUILTIN_ROUNDPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_INT },
23834
23835   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhpd256,  "__builtin_ia32_unpckhpd256", IX86_BUILTIN_UNPCKHPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23836   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklpd256,  "__builtin_ia32_unpcklpd256", IX86_BUILTIN_UNPCKLPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23837   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpckhps256,  "__builtin_ia32_unpckhps256", IX86_BUILTIN_UNPCKHPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23838   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_unpcklps256,  "__builtin_ia32_unpcklps256", IX86_BUILTIN_UNPCKLPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23839
23840   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_si256_si, "__builtin_ia32_si256_si", IX86_BUILTIN_SI256_SI, UNKNOWN, (int) V8SI_FTYPE_V4SI },
23841   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ps256_ps, "__builtin_ia32_ps256_ps", IX86_BUILTIN_PS256_PS, UNKNOWN, (int) V8SF_FTYPE_V4SF },
23842   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_pd256_pd, "__builtin_ia32_pd256_pd", IX86_BUILTIN_PD256_PD, UNKNOWN, (int) V4DF_FTYPE_V2DF },
23843   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8si, "__builtin_ia32_si_si256", IX86_BUILTIN_SI_SI256, UNKNOWN, (int) V4SI_FTYPE_V8SI },
23844   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v8sf, "__builtin_ia32_ps_ps256", IX86_BUILTIN_PS_PS256, UNKNOWN, (int) V4SF_FTYPE_V8SF },
23845   { OPTION_MASK_ISA_AVX, CODE_FOR_vec_extract_lo_v4df, "__builtin_ia32_pd_pd256", IX86_BUILTIN_PD_PD256, UNKNOWN, (int) V2DF_FTYPE_V4DF },
23846
23847   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestzpd", IX86_BUILTIN_VTESTZPD, EQ, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23848   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestcpd", IX86_BUILTIN_VTESTCPD, LTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23849   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd, "__builtin_ia32_vtestnzcpd", IX86_BUILTIN_VTESTNZCPD, GTU, (int) INT_FTYPE_V2DF_V2DF_PTEST },
23850   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestzps", IX86_BUILTIN_VTESTZPS, EQ, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23851   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestcps", IX86_BUILTIN_VTESTCPS, LTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23852   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps, "__builtin_ia32_vtestnzcps", IX86_BUILTIN_VTESTNZCPS, GTU, (int) INT_FTYPE_V4SF_V4SF_PTEST },
23853   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestzpd256", IX86_BUILTIN_VTESTZPD256, EQ, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23854   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestcpd256", IX86_BUILTIN_VTESTCPD256, LTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23855   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestpd256, "__builtin_ia32_vtestnzcpd256", IX86_BUILTIN_VTESTNZCPD256, GTU, (int) INT_FTYPE_V4DF_V4DF_PTEST },
23856   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestzps256", IX86_BUILTIN_VTESTZPS256, EQ, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23857   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestcps256", IX86_BUILTIN_VTESTCPS256, LTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23858   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_vtestps256, "__builtin_ia32_vtestnzcps256", IX86_BUILTIN_VTESTNZCPS256, GTU, (int) INT_FTYPE_V8SF_V8SF_PTEST },
23859   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestz256", IX86_BUILTIN_PTESTZ256, EQ, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23860   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestc256", IX86_BUILTIN_PTESTC256, LTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23861   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_ptest256, "__builtin_ia32_ptestnzc256", IX86_BUILTIN_PTESTNZC256, GTU, (int) INT_FTYPE_V4DI_V4DI_PTEST },
23862
23863   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskpd256, "__builtin_ia32_movmskpd256", IX86_BUILTIN_MOVMSKPD256, UNKNOWN, (int) INT_FTYPE_V4DF  },
23864   { OPTION_MASK_ISA_AVX, CODE_FOR_avx_movmskps256, "__builtin_ia32_movmskps256", IX86_BUILTIN_MOVMSKPS256, UNKNOWN, (int) INT_FTYPE_V8SF },
23865
23866   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv8sf3,  "__builtin_ia32_copysignps256", IX86_BUILTIN_CPYSGNPS256, UNKNOWN, (int) V8SF_FTYPE_V8SF_V8SF },
23867   { OPTION_MASK_ISA_AVX, CODE_FOR_copysignv4df3,  "__builtin_ia32_copysignpd256", IX86_BUILTIN_CPYSGNPD256, UNKNOWN, (int) V4DF_FTYPE_V4DF_V4DF },
23868
23869   { OPTION_MASK_ISA_ABM, CODE_FOR_clzhi2_abm,   "__builtin_clzs",   IX86_BUILTIN_CLZS,    UNKNOWN,     (int) UINT16_FTYPE_UINT16 },
23870
23871   /* F16C */
23872   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps, "__builtin_ia32_vcvtph2ps", IX86_BUILTIN_CVTPH2PS, UNKNOWN, (int) V4SF_FTYPE_V8HI },
23873   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtph2ps256, "__builtin_ia32_vcvtph2ps256", IX86_BUILTIN_CVTPH2PS256, UNKNOWN, (int) V8SF_FTYPE_V8HI },
23874   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph, "__builtin_ia32_vcvtps2ph", IX86_BUILTIN_CVTPS2PH, UNKNOWN, (int) V8HI_FTYPE_V4SF_INT },
23875   { OPTION_MASK_ISA_F16C, CODE_FOR_vcvtps2ph256, "__builtin_ia32_vcvtps2ph256", IX86_BUILTIN_CVTPS2PH256, UNKNOWN, (int) V8HI_FTYPE_V8SF_INT },
23876 };
23877
23878 /* FMA4 and XOP.  */
23879 #define MULTI_ARG_4_DF2_DI_I    V2DF_FTYPE_V2DF_V2DF_V2DI_INT
23880 #define MULTI_ARG_4_DF2_DI_I1   V4DF_FTYPE_V4DF_V4DF_V4DI_INT
23881 #define MULTI_ARG_4_SF2_SI_I    V4SF_FTYPE_V4SF_V4SF_V4SI_INT
23882 #define MULTI_ARG_4_SF2_SI_I1   V8SF_FTYPE_V8SF_V8SF_V8SI_INT
23883 #define MULTI_ARG_3_SF          V4SF_FTYPE_V4SF_V4SF_V4SF
23884 #define MULTI_ARG_3_DF          V2DF_FTYPE_V2DF_V2DF_V2DF
23885 #define MULTI_ARG_3_SF2         V8SF_FTYPE_V8SF_V8SF_V8SF
23886 #define MULTI_ARG_3_DF2         V4DF_FTYPE_V4DF_V4DF_V4DF
23887 #define MULTI_ARG_3_DI          V2DI_FTYPE_V2DI_V2DI_V2DI
23888 #define MULTI_ARG_3_SI          V4SI_FTYPE_V4SI_V4SI_V4SI
23889 #define MULTI_ARG_3_SI_DI       V4SI_FTYPE_V4SI_V4SI_V2DI
23890 #define MULTI_ARG_3_HI          V8HI_FTYPE_V8HI_V8HI_V8HI
23891 #define MULTI_ARG_3_HI_SI       V8HI_FTYPE_V8HI_V8HI_V4SI
23892 #define MULTI_ARG_3_QI          V16QI_FTYPE_V16QI_V16QI_V16QI
23893 #define MULTI_ARG_3_DI2         V4DI_FTYPE_V4DI_V4DI_V4DI
23894 #define MULTI_ARG_3_SI2         V8SI_FTYPE_V8SI_V8SI_V8SI
23895 #define MULTI_ARG_3_HI2         V16HI_FTYPE_V16HI_V16HI_V16HI
23896 #define MULTI_ARG_3_QI2         V32QI_FTYPE_V32QI_V32QI_V32QI
23897 #define MULTI_ARG_2_SF          V4SF_FTYPE_V4SF_V4SF
23898 #define MULTI_ARG_2_DF          V2DF_FTYPE_V2DF_V2DF
23899 #define MULTI_ARG_2_DI          V2DI_FTYPE_V2DI_V2DI
23900 #define MULTI_ARG_2_SI          V4SI_FTYPE_V4SI_V4SI
23901 #define MULTI_ARG_2_HI          V8HI_FTYPE_V8HI_V8HI
23902 #define MULTI_ARG_2_QI          V16QI_FTYPE_V16QI_V16QI
23903 #define MULTI_ARG_2_DI_IMM      V2DI_FTYPE_V2DI_SI
23904 #define MULTI_ARG_2_SI_IMM      V4SI_FTYPE_V4SI_SI
23905 #define MULTI_ARG_2_HI_IMM      V8HI_FTYPE_V8HI_SI
23906 #define MULTI_ARG_2_QI_IMM      V16QI_FTYPE_V16QI_SI
23907 #define MULTI_ARG_2_DI_CMP      V2DI_FTYPE_V2DI_V2DI_CMP
23908 #define MULTI_ARG_2_SI_CMP      V4SI_FTYPE_V4SI_V4SI_CMP
23909 #define MULTI_ARG_2_HI_CMP      V8HI_FTYPE_V8HI_V8HI_CMP
23910 #define MULTI_ARG_2_QI_CMP      V16QI_FTYPE_V16QI_V16QI_CMP
23911 #define MULTI_ARG_2_SF_TF       V4SF_FTYPE_V4SF_V4SF_TF
23912 #define MULTI_ARG_2_DF_TF       V2DF_FTYPE_V2DF_V2DF_TF
23913 #define MULTI_ARG_2_DI_TF       V2DI_FTYPE_V2DI_V2DI_TF
23914 #define MULTI_ARG_2_SI_TF       V4SI_FTYPE_V4SI_V4SI_TF
23915 #define MULTI_ARG_2_HI_TF       V8HI_FTYPE_V8HI_V8HI_TF
23916 #define MULTI_ARG_2_QI_TF       V16QI_FTYPE_V16QI_V16QI_TF
23917 #define MULTI_ARG_1_SF          V4SF_FTYPE_V4SF
23918 #define MULTI_ARG_1_DF          V2DF_FTYPE_V2DF
23919 #define MULTI_ARG_1_SF2         V8SF_FTYPE_V8SF
23920 #define MULTI_ARG_1_DF2         V4DF_FTYPE_V4DF
23921 #define MULTI_ARG_1_DI          V2DI_FTYPE_V2DI
23922 #define MULTI_ARG_1_SI          V4SI_FTYPE_V4SI
23923 #define MULTI_ARG_1_HI          V8HI_FTYPE_V8HI
23924 #define MULTI_ARG_1_QI          V16QI_FTYPE_V16QI
23925 #define MULTI_ARG_1_SI_DI       V2DI_FTYPE_V4SI
23926 #define MULTI_ARG_1_HI_DI       V2DI_FTYPE_V8HI
23927 #define MULTI_ARG_1_HI_SI       V4SI_FTYPE_V8HI
23928 #define MULTI_ARG_1_QI_DI       V2DI_FTYPE_V16QI
23929 #define MULTI_ARG_1_QI_SI       V4SI_FTYPE_V16QI
23930 #define MULTI_ARG_1_QI_HI       V8HI_FTYPE_V16QI
23931
23932 static const struct builtin_description bdesc_multi_arg[] =
23933 {
23934   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v4sf,
23935     "__builtin_ia32_vfmaddss", IX86_BUILTIN_VFMADDSS,
23936     UNKNOWN, (int)MULTI_ARG_3_SF },
23937   { OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_vmfmadd_v2df,
23938     "__builtin_ia32_vfmaddsd", IX86_BUILTIN_VFMADDSD,
23939     UNKNOWN, (int)MULTI_ARG_3_DF },
23940
23941   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4sf,
23942     "__builtin_ia32_vfmaddps", IX86_BUILTIN_VFMADDPS,
23943     UNKNOWN, (int)MULTI_ARG_3_SF },
23944   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v2df,
23945     "__builtin_ia32_vfmaddpd", IX86_BUILTIN_VFMADDPD,
23946     UNKNOWN, (int)MULTI_ARG_3_DF },
23947   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v8sf,
23948     "__builtin_ia32_vfmaddps256", IX86_BUILTIN_VFMADDPS256,
23949     UNKNOWN, (int)MULTI_ARG_3_SF2 },
23950   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fma4i_fmadd_v4df,
23951     "__builtin_ia32_vfmaddpd256", IX86_BUILTIN_VFMADDPD256,
23952     UNKNOWN, (int)MULTI_ARG_3_DF2 },
23953
23954   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4sf,
23955     "__builtin_ia32_vfmaddsubps", IX86_BUILTIN_VFMADDSUBPS,
23956     UNKNOWN, (int)MULTI_ARG_3_SF },
23957   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v2df,
23958     "__builtin_ia32_vfmaddsubpd", IX86_BUILTIN_VFMADDSUBPD,
23959     UNKNOWN, (int)MULTI_ARG_3_DF },
23960   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v8sf,
23961     "__builtin_ia32_vfmaddsubps256", IX86_BUILTIN_VFMADDSUBPS256,
23962     UNKNOWN, (int)MULTI_ARG_3_SF2 },
23963   { OPTION_MASK_ISA_FMA | OPTION_MASK_ISA_FMA4, CODE_FOR_fmaddsub_v4df,
23964     "__builtin_ia32_vfmaddsubpd256", IX86_BUILTIN_VFMADDSUBPD256,
23965     UNKNOWN, (int)MULTI_ARG_3_DF2 },
23966
23967   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov",      IX86_BUILTIN_VPCMOV,      UNKNOWN,      (int)MULTI_ARG_3_DI },
23968   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2di,        "__builtin_ia32_vpcmov_v2di", IX86_BUILTIN_VPCMOV_V2DI, UNKNOWN,      (int)MULTI_ARG_3_DI },
23969   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4si,        "__builtin_ia32_vpcmov_v4si", IX86_BUILTIN_VPCMOV_V4SI, UNKNOWN,      (int)MULTI_ARG_3_SI },
23970   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8hi,        "__builtin_ia32_vpcmov_v8hi", IX86_BUILTIN_VPCMOV_V8HI, UNKNOWN,      (int)MULTI_ARG_3_HI },
23971   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16qi,       "__builtin_ia32_vpcmov_v16qi",IX86_BUILTIN_VPCMOV_V16QI,UNKNOWN,      (int)MULTI_ARG_3_QI },
23972   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v2df,        "__builtin_ia32_vpcmov_v2df", IX86_BUILTIN_VPCMOV_V2DF, UNKNOWN,      (int)MULTI_ARG_3_DF },
23973   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4sf,        "__builtin_ia32_vpcmov_v4sf", IX86_BUILTIN_VPCMOV_V4SF, UNKNOWN,      (int)MULTI_ARG_3_SF },
23974
23975   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov256",       IX86_BUILTIN_VPCMOV256,       UNKNOWN,      (int)MULTI_ARG_3_DI2 },
23976   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4di256,        "__builtin_ia32_vpcmov_v4di256",  IX86_BUILTIN_VPCMOV_V4DI256,  UNKNOWN,      (int)MULTI_ARG_3_DI2 },
23977   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8si256,        "__builtin_ia32_vpcmov_v8si256",  IX86_BUILTIN_VPCMOV_V8SI256,  UNKNOWN,      (int)MULTI_ARG_3_SI2 },
23978   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v16hi256,       "__builtin_ia32_vpcmov_v16hi256", IX86_BUILTIN_VPCMOV_V16HI256, UNKNOWN,      (int)MULTI_ARG_3_HI2 },
23979   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v32qi256,       "__builtin_ia32_vpcmov_v32qi256", IX86_BUILTIN_VPCMOV_V32QI256, UNKNOWN,      (int)MULTI_ARG_3_QI2 },
23980   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v4df256,        "__builtin_ia32_vpcmov_v4df256",  IX86_BUILTIN_VPCMOV_V4DF256,  UNKNOWN,      (int)MULTI_ARG_3_DF2 },
23981   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pcmov_v8sf256,        "__builtin_ia32_vpcmov_v8sf256",  IX86_BUILTIN_VPCMOV_V8SF256,  UNKNOWN,      (int)MULTI_ARG_3_SF2 },
23982
23983   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pperm,             "__builtin_ia32_vpperm",      IX86_BUILTIN_VPPERM,      UNKNOWN,      (int)MULTI_ARG_3_QI },
23984
23985   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssww,          "__builtin_ia32_vpmacssww",   IX86_BUILTIN_VPMACSSWW,   UNKNOWN,      (int)MULTI_ARG_3_HI },
23986   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsww,           "__builtin_ia32_vpmacsww",    IX86_BUILTIN_VPMACSWW,    UNKNOWN,      (int)MULTI_ARG_3_HI },
23987   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsswd,          "__builtin_ia32_vpmacsswd",   IX86_BUILTIN_VPMACSSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23988   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacswd,           "__builtin_ia32_vpmacswd",    IX86_BUILTIN_VPMACSWD,    UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23989   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdd,          "__builtin_ia32_vpmacssdd",   IX86_BUILTIN_VPMACSSDD,   UNKNOWN,      (int)MULTI_ARG_3_SI },
23990   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdd,           "__builtin_ia32_vpmacsdd",    IX86_BUILTIN_VPMACSDD,    UNKNOWN,      (int)MULTI_ARG_3_SI },
23991   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdql,         "__builtin_ia32_vpmacssdql",  IX86_BUILTIN_VPMACSSDQL,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23992   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacssdqh,         "__builtin_ia32_vpmacssdqh",  IX86_BUILTIN_VPMACSSDQH,  UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23993   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdql,          "__builtin_ia32_vpmacsdql",   IX86_BUILTIN_VPMACSDQL,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23994   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmacsdqh,          "__builtin_ia32_vpmacsdqh",   IX86_BUILTIN_VPMACSDQH,   UNKNOWN,      (int)MULTI_ARG_3_SI_DI },
23995   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcsswd,         "__builtin_ia32_vpmadcsswd",  IX86_BUILTIN_VPMADCSSWD,  UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23996   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_pmadcswd,          "__builtin_ia32_vpmadcswd",   IX86_BUILTIN_VPMADCSWD,   UNKNOWN,      (int)MULTI_ARG_3_HI_SI },
23997
23998   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv2di3,        "__builtin_ia32_vprotq",      IX86_BUILTIN_VPROTQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
23999   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv4si3,        "__builtin_ia32_vprotd",      IX86_BUILTIN_VPROTD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
24000   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv8hi3,        "__builtin_ia32_vprotw",      IX86_BUILTIN_VPROTW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
24001   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vrotlv16qi3,       "__builtin_ia32_vprotb",      IX86_BUILTIN_VPROTB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
24002   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv2di3,         "__builtin_ia32_vprotqi",     IX86_BUILTIN_VPROTQ_IMM,  UNKNOWN,      (int)MULTI_ARG_2_DI_IMM },
24003   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv4si3,         "__builtin_ia32_vprotdi",     IX86_BUILTIN_VPROTD_IMM,  UNKNOWN,      (int)MULTI_ARG_2_SI_IMM },
24004   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv8hi3,         "__builtin_ia32_vprotwi",     IX86_BUILTIN_VPROTW_IMM,  UNKNOWN,      (int)MULTI_ARG_2_HI_IMM },
24005   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_rotlv16qi3,        "__builtin_ia32_vprotbi",     IX86_BUILTIN_VPROTB_IMM,  UNKNOWN,      (int)MULTI_ARG_2_QI_IMM },
24006   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv2di3,         "__builtin_ia32_vpshaq",      IX86_BUILTIN_VPSHAQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
24007   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv4si3,         "__builtin_ia32_vpshad",      IX86_BUILTIN_VPSHAD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
24008   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv8hi3,         "__builtin_ia32_vpshaw",      IX86_BUILTIN_VPSHAW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
24009   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_ashlv16qi3,        "__builtin_ia32_vpshab",      IX86_BUILTIN_VPSHAB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
24010   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv2di3,         "__builtin_ia32_vpshlq",      IX86_BUILTIN_VPSHLQ,      UNKNOWN,      (int)MULTI_ARG_2_DI },
24011   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv4si3,         "__builtin_ia32_vpshld",      IX86_BUILTIN_VPSHLD,      UNKNOWN,      (int)MULTI_ARG_2_SI },
24012   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv8hi3,         "__builtin_ia32_vpshlw",      IX86_BUILTIN_VPSHLW,      UNKNOWN,      (int)MULTI_ARG_2_HI },
24013   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_lshlv16qi3,        "__builtin_ia32_vpshlb",      IX86_BUILTIN_VPSHLB,      UNKNOWN,      (int)MULTI_ARG_2_QI },
24014
24015   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv4sf2,       "__builtin_ia32_vfrczss",     IX86_BUILTIN_VFRCZSS,     UNKNOWN,      (int)MULTI_ARG_2_SF },
24016   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vmfrczv2df2,       "__builtin_ia32_vfrczsd",     IX86_BUILTIN_VFRCZSD,     UNKNOWN,      (int)MULTI_ARG_2_DF },
24017   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4sf2,         "__builtin_ia32_vfrczps",     IX86_BUILTIN_VFRCZPS,     UNKNOWN,      (int)MULTI_ARG_1_SF },
24018   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv2df2,         "__builtin_ia32_vfrczpd",     IX86_BUILTIN_VFRCZPD,     UNKNOWN,      (int)MULTI_ARG_1_DF },
24019   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv8sf2,         "__builtin_ia32_vfrczps256",  IX86_BUILTIN_VFRCZPS256,  UNKNOWN,      (int)MULTI_ARG_1_SF2 },
24020   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_frczv4df2,         "__builtin_ia32_vfrczpd256",  IX86_BUILTIN_VFRCZPD256,  UNKNOWN,      (int)MULTI_ARG_1_DF2 },
24021
24022   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbw,           "__builtin_ia32_vphaddbw",    IX86_BUILTIN_VPHADDBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
24023   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbd,           "__builtin_ia32_vphaddbd",    IX86_BUILTIN_VPHADDBD,    UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
24024   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddbq,           "__builtin_ia32_vphaddbq",    IX86_BUILTIN_VPHADDBQ,    UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
24025   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwd,           "__builtin_ia32_vphaddwd",    IX86_BUILTIN_VPHADDWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
24026   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddwq,           "__builtin_ia32_vphaddwq",    IX86_BUILTIN_VPHADDWQ,    UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
24027   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadddq,           "__builtin_ia32_vphadddq",    IX86_BUILTIN_VPHADDDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
24028   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubw,          "__builtin_ia32_vphaddubw",   IX86_BUILTIN_VPHADDUBW,   UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
24029   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubd,          "__builtin_ia32_vphaddubd",   IX86_BUILTIN_VPHADDUBD,   UNKNOWN,      (int)MULTI_ARG_1_QI_SI },
24030   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddubq,          "__builtin_ia32_vphaddubq",   IX86_BUILTIN_VPHADDUBQ,   UNKNOWN,      (int)MULTI_ARG_1_QI_DI },
24031   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwd,          "__builtin_ia32_vphadduwd",   IX86_BUILTIN_VPHADDUWD,   UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
24032   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phadduwq,          "__builtin_ia32_vphadduwq",   IX86_BUILTIN_VPHADDUWQ,   UNKNOWN,      (int)MULTI_ARG_1_HI_DI },
24033   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phaddudq,          "__builtin_ia32_vphaddudq",   IX86_BUILTIN_VPHADDUDQ,   UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
24034   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubbw,           "__builtin_ia32_vphsubbw",    IX86_BUILTIN_VPHSUBBW,    UNKNOWN,      (int)MULTI_ARG_1_QI_HI },
24035   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubwd,           "__builtin_ia32_vphsubwd",    IX86_BUILTIN_VPHSUBWD,    UNKNOWN,      (int)MULTI_ARG_1_HI_SI },
24036   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_phsubdq,           "__builtin_ia32_vphsubdq",    IX86_BUILTIN_VPHSUBDQ,    UNKNOWN,      (int)MULTI_ARG_1_SI_DI },
24037
24038   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomeqb",    IX86_BUILTIN_VPCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
24039   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneb",    IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
24040   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomneqb",   IX86_BUILTIN_VPCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
24041   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomltb",    IX86_BUILTIN_VPCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
24042   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomleb",    IX86_BUILTIN_VPCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
24043   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgtb",    IX86_BUILTIN_VPCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
24044   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv16qi3,     "__builtin_ia32_vpcomgeb",    IX86_BUILTIN_VPCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
24045
24046   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomeqw",    IX86_BUILTIN_VPCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
24047   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomnew",    IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
24048   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomneqw",   IX86_BUILTIN_VPCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
24049   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomltw",    IX86_BUILTIN_VPCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
24050   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomlew",    IX86_BUILTIN_VPCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
24051   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgtw",    IX86_BUILTIN_VPCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
24052   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv8hi3,      "__builtin_ia32_vpcomgew",    IX86_BUILTIN_VPCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
24053
24054   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomeqd",    IX86_BUILTIN_VPCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
24055   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomned",    IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
24056   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomneqd",   IX86_BUILTIN_VPCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
24057   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomltd",    IX86_BUILTIN_VPCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
24058   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomled",    IX86_BUILTIN_VPCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
24059   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomgtd",    IX86_BUILTIN_VPCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
24060   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv4si3,      "__builtin_ia32_vpcomged",    IX86_BUILTIN_VPCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
24061
24062   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomeqq",    IX86_BUILTIN_VPCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
24063   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneq",    IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
24064   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomneqq",   IX86_BUILTIN_VPCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
24065   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomltq",    IX86_BUILTIN_VPCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
24066   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomleq",    IX86_BUILTIN_VPCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
24067   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgtq",    IX86_BUILTIN_VPCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
24068   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmpv2di3,      "__builtin_ia32_vpcomgeq",    IX86_BUILTIN_VPCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
24069
24070   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomequb",   IX86_BUILTIN_VPCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
24071   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomneub",   IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
24072   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v16qi3,"__builtin_ia32_vpcomnequb",  IX86_BUILTIN_VPCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
24073   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomltub",   IX86_BUILTIN_VPCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
24074   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomleub",   IX86_BUILTIN_VPCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
24075   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgtub",   IX86_BUILTIN_VPCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
24076   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv16qi3, "__builtin_ia32_vpcomgeub",   IX86_BUILTIN_VPCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
24077
24078   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomequw",   IX86_BUILTIN_VPCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
24079   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomneuw",   IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
24080   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v8hi3, "__builtin_ia32_vpcomnequw",  IX86_BUILTIN_VPCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
24081   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomltuw",   IX86_BUILTIN_VPCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
24082   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomleuw",   IX86_BUILTIN_VPCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
24083   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgtuw",   IX86_BUILTIN_VPCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
24084   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv8hi3,  "__builtin_ia32_vpcomgeuw",   IX86_BUILTIN_VPCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
24085
24086   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomequd",   IX86_BUILTIN_VPCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
24087   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomneud",   IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
24088   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v4si3, "__builtin_ia32_vpcomnequd",  IX86_BUILTIN_VPCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
24089   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomltud",   IX86_BUILTIN_VPCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
24090   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomleud",   IX86_BUILTIN_VPCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
24091   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgtud",   IX86_BUILTIN_VPCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
24092   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv4si3,  "__builtin_ia32_vpcomgeud",   IX86_BUILTIN_VPCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
24093
24094   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomequq",   IX86_BUILTIN_VPCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
24095   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomneuq",   IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
24096   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_uns2v2di3, "__builtin_ia32_vpcomnequq",  IX86_BUILTIN_VPCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
24097   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomltuq",   IX86_BUILTIN_VPCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
24098   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomleuq",   IX86_BUILTIN_VPCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
24099   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgtuq",   IX86_BUILTIN_VPCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
24100   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_maskcmp_unsv2di3,  "__builtin_ia32_vpcomgeuq",   IX86_BUILTIN_VPCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
24101
24102   { 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 },
24103   { 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 },
24104   { 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 },
24105   { 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 },
24106   { 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 },
24107   { 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 },
24108   { 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 },
24109   { 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 },
24110
24111   { 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 },
24112   { 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 },
24113   { 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 },
24114   { 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 },
24115   { 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 },
24116   { 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 },
24117   { 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 },
24118   { 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 },
24119
24120   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v2df3,     "__builtin_ia32_vpermil2pd",  IX86_BUILTIN_VPERMIL2PD, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I },
24121   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4sf3,     "__builtin_ia32_vpermil2ps",  IX86_BUILTIN_VPERMIL2PS, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I },
24122   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v4df3,     "__builtin_ia32_vpermil2pd256", IX86_BUILTIN_VPERMIL2PD256, UNKNOWN, (int)MULTI_ARG_4_DF2_DI_I1 },
24123   { OPTION_MASK_ISA_XOP, CODE_FOR_xop_vpermil2v8sf3,     "__builtin_ia32_vpermil2ps256", IX86_BUILTIN_VPERMIL2PS256, UNKNOWN, (int)MULTI_ARG_4_SF2_SI_I1 },
24124
24125 };
24126
24127 /* Set up all the MMX/SSE builtins, even builtins for instructions that are not
24128    in the current target ISA to allow the user to compile particular modules
24129    with different target specific options that differ from the command line
24130    options.  */
24131 static void
24132 ix86_init_mmx_sse_builtins (void)
24133 {
24134   const struct builtin_description * d;
24135   enum ix86_builtin_func_type ftype;
24136   size_t i;
24137
24138   /* Add all special builtins with variable number of operands.  */
24139   for (i = 0, d = bdesc_special_args;
24140        i < ARRAY_SIZE (bdesc_special_args);
24141        i++, d++)
24142     {
24143       if (d->name == 0)
24144         continue;
24145
24146       ftype = (enum ix86_builtin_func_type) d->flag;
24147       def_builtin (d->mask, d->name, ftype, d->code);
24148     }
24149
24150   /* Add all builtins with variable number of operands.  */
24151   for (i = 0, d = bdesc_args;
24152        i < ARRAY_SIZE (bdesc_args);
24153        i++, d++)
24154     {
24155       if (d->name == 0)
24156         continue;
24157
24158       ftype = (enum ix86_builtin_func_type) d->flag;
24159       def_builtin_const (d->mask, d->name, ftype, d->code);
24160     }
24161
24162   /* pcmpestr[im] insns.  */
24163   for (i = 0, d = bdesc_pcmpestr;
24164        i < ARRAY_SIZE (bdesc_pcmpestr);
24165        i++, d++)
24166     {
24167       if (d->code == IX86_BUILTIN_PCMPESTRM128)
24168         ftype = V16QI_FTYPE_V16QI_INT_V16QI_INT_INT;
24169       else
24170         ftype = INT_FTYPE_V16QI_INT_V16QI_INT_INT;
24171       def_builtin_const (d->mask, d->name, ftype, d->code);
24172     }
24173
24174   /* pcmpistr[im] insns.  */
24175   for (i = 0, d = bdesc_pcmpistr;
24176        i < ARRAY_SIZE (bdesc_pcmpistr);
24177        i++, d++)
24178     {
24179       if (d->code == IX86_BUILTIN_PCMPISTRM128)
24180         ftype = V16QI_FTYPE_V16QI_V16QI_INT;
24181       else
24182         ftype = INT_FTYPE_V16QI_V16QI_INT;
24183       def_builtin_const (d->mask, d->name, ftype, d->code);
24184     }
24185
24186   /* comi/ucomi insns.  */
24187   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
24188     {
24189       if (d->mask == OPTION_MASK_ISA_SSE2)
24190         ftype = INT_FTYPE_V2DF_V2DF;
24191       else
24192         ftype = INT_FTYPE_V4SF_V4SF;
24193       def_builtin_const (d->mask, d->name, ftype, d->code);
24194     }
24195
24196   /* SSE */
24197   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr",
24198                VOID_FTYPE_UNSIGNED, IX86_BUILTIN_LDMXCSR);
24199   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr",
24200                UNSIGNED_FTYPE_VOID, IX86_BUILTIN_STMXCSR);
24201
24202   /* SSE or 3DNow!A */
24203   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
24204                "__builtin_ia32_maskmovq", VOID_FTYPE_V8QI_V8QI_PCHAR,
24205                IX86_BUILTIN_MASKMOVQ);
24206
24207   /* SSE2 */
24208   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu",
24209                VOID_FTYPE_V16QI_V16QI_PCHAR, IX86_BUILTIN_MASKMOVDQU);
24210
24211   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush",
24212                VOID_FTYPE_PCVOID, IX86_BUILTIN_CLFLUSH);
24213   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence",
24214                             VOID_FTYPE_VOID, IX86_BUILTIN_MFENCE);
24215
24216   /* SSE3.  */
24217   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor",
24218                VOID_FTYPE_PCVOID_UNSIGNED_UNSIGNED, IX86_BUILTIN_MONITOR);
24219   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait",
24220                VOID_FTYPE_UNSIGNED_UNSIGNED, IX86_BUILTIN_MWAIT);
24221
24222   /* AES */
24223   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenc128",
24224                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENC128);
24225   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesenclast128",
24226                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESENCLAST128);
24227   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdec128",
24228                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDEC128);
24229   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesdeclast128",
24230                      V2DI_FTYPE_V2DI_V2DI, IX86_BUILTIN_AESDECLAST128);
24231   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aesimc128",
24232                      V2DI_FTYPE_V2DI, IX86_BUILTIN_AESIMC128);
24233   def_builtin_const (OPTION_MASK_ISA_AES, "__builtin_ia32_aeskeygenassist128",
24234                      V2DI_FTYPE_V2DI_INT, IX86_BUILTIN_AESKEYGENASSIST128);
24235
24236   /* PCLMUL */
24237   def_builtin_const (OPTION_MASK_ISA_PCLMUL, "__builtin_ia32_pclmulqdq128",
24238                      V2DI_FTYPE_V2DI_V2DI_INT, IX86_BUILTIN_PCLMULQDQ128);
24239
24240   /* MMX access to the vec_init patterns.  */
24241   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si",
24242                      V2SI_FTYPE_INT_INT, IX86_BUILTIN_VEC_INIT_V2SI);
24243
24244   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi",
24245                      V4HI_FTYPE_HI_HI_HI_HI,
24246                      IX86_BUILTIN_VEC_INIT_V4HI);
24247
24248   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi",
24249                      V8QI_FTYPE_QI_QI_QI_QI_QI_QI_QI_QI,
24250                      IX86_BUILTIN_VEC_INIT_V8QI);
24251
24252   /* Access to the vec_extract patterns.  */
24253   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df",
24254                      DOUBLE_FTYPE_V2DF_INT, IX86_BUILTIN_VEC_EXT_V2DF);
24255   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di",
24256                      DI_FTYPE_V2DI_INT, IX86_BUILTIN_VEC_EXT_V2DI);
24257   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf",
24258                      FLOAT_FTYPE_V4SF_INT, IX86_BUILTIN_VEC_EXT_V4SF);
24259   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si",
24260                      SI_FTYPE_V4SI_INT, IX86_BUILTIN_VEC_EXT_V4SI);
24261   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi",
24262                      HI_FTYPE_V8HI_INT, IX86_BUILTIN_VEC_EXT_V8HI);
24263
24264   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
24265                      "__builtin_ia32_vec_ext_v4hi",
24266                      HI_FTYPE_V4HI_INT, IX86_BUILTIN_VEC_EXT_V4HI);
24267
24268   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si",
24269                      SI_FTYPE_V2SI_INT, IX86_BUILTIN_VEC_EXT_V2SI);
24270
24271   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi",
24272                      QI_FTYPE_V16QI_INT, IX86_BUILTIN_VEC_EXT_V16QI);
24273
24274   /* Access to the vec_set patterns.  */
24275   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT,
24276                      "__builtin_ia32_vec_set_v2di",
24277                      V2DI_FTYPE_V2DI_DI_INT, IX86_BUILTIN_VEC_SET_V2DI);
24278
24279   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf",
24280                      V4SF_FTYPE_V4SF_FLOAT_INT, IX86_BUILTIN_VEC_SET_V4SF);
24281
24282   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si",
24283                      V4SI_FTYPE_V4SI_SI_INT, IX86_BUILTIN_VEC_SET_V4SI);
24284
24285   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi",
24286                      V8HI_FTYPE_V8HI_HI_INT, IX86_BUILTIN_VEC_SET_V8HI);
24287
24288   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A,
24289                      "__builtin_ia32_vec_set_v4hi",
24290                      V4HI_FTYPE_V4HI_HI_INT, IX86_BUILTIN_VEC_SET_V4HI);
24291
24292   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi",
24293                      V16QI_FTYPE_V16QI_QI_INT, IX86_BUILTIN_VEC_SET_V16QI);
24294
24295   /* Add FMA4 multi-arg argument instructions */
24296   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
24297     {
24298       if (d->name == 0)
24299         continue;
24300
24301       ftype = (enum ix86_builtin_func_type) d->flag;
24302       def_builtin_const (d->mask, d->name, ftype, d->code);
24303     }
24304 }
24305
24306 /* Internal method for ix86_init_builtins.  */
24307
24308 static void
24309 ix86_init_builtins_va_builtins_abi (void)
24310 {
24311   tree ms_va_ref, sysv_va_ref;
24312   tree fnvoid_va_end_ms, fnvoid_va_end_sysv;
24313   tree fnvoid_va_start_ms, fnvoid_va_start_sysv;
24314   tree fnvoid_va_copy_ms, fnvoid_va_copy_sysv;
24315   tree fnattr_ms = NULL_TREE, fnattr_sysv = NULL_TREE;
24316
24317   if (!TARGET_64BIT)
24318     return;
24319   fnattr_ms = build_tree_list (get_identifier ("ms_abi"), NULL_TREE);
24320   fnattr_sysv = build_tree_list (get_identifier ("sysv_abi"), NULL_TREE);
24321   ms_va_ref = build_reference_type (ms_va_list_type_node);
24322   sysv_va_ref =
24323     build_pointer_type (TREE_TYPE (sysv_va_list_type_node));
24324
24325   fnvoid_va_end_ms =
24326     build_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
24327   fnvoid_va_start_ms =
24328     build_varargs_function_type_list (void_type_node, ms_va_ref, NULL_TREE);
24329   fnvoid_va_end_sysv =
24330     build_function_type_list (void_type_node, sysv_va_ref, NULL_TREE);
24331   fnvoid_va_start_sysv =
24332     build_varargs_function_type_list (void_type_node, sysv_va_ref,
24333                                        NULL_TREE);
24334   fnvoid_va_copy_ms =
24335     build_function_type_list (void_type_node, ms_va_ref, ms_va_list_type_node,
24336                               NULL_TREE);
24337   fnvoid_va_copy_sysv =
24338     build_function_type_list (void_type_node, sysv_va_ref,
24339                               sysv_va_ref, NULL_TREE);
24340
24341   add_builtin_function ("__builtin_ms_va_start", fnvoid_va_start_ms,
24342                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_ms);
24343   add_builtin_function ("__builtin_ms_va_end", fnvoid_va_end_ms,
24344                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_ms);
24345   add_builtin_function ("__builtin_ms_va_copy", fnvoid_va_copy_ms,
24346                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_ms);
24347   add_builtin_function ("__builtin_sysv_va_start", fnvoid_va_start_sysv,
24348                         BUILT_IN_VA_START, BUILT_IN_NORMAL, NULL, fnattr_sysv);
24349   add_builtin_function ("__builtin_sysv_va_end", fnvoid_va_end_sysv,
24350                         BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL, fnattr_sysv);
24351   add_builtin_function ("__builtin_sysv_va_copy", fnvoid_va_copy_sysv,
24352                         BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL, fnattr_sysv);
24353 }
24354
24355 static void
24356 ix86_init_builtin_types (void)
24357 {
24358   tree float128_type_node, float80_type_node;
24359
24360   /* The __float80 type.  */
24361   float80_type_node = long_double_type_node;
24362   if (TYPE_MODE (float80_type_node) != XFmode)
24363     {
24364       /* The __float80 type.  */
24365       float80_type_node = make_node (REAL_TYPE);
24366
24367       TYPE_PRECISION (float80_type_node) = 80;
24368       layout_type (float80_type_node);
24369     }
24370   lang_hooks.types.register_builtin_type (float80_type_node, "__float80");
24371
24372   /* The __float128 type.  */
24373   float128_type_node = make_node (REAL_TYPE);
24374   TYPE_PRECISION (float128_type_node) = 128;
24375   layout_type (float128_type_node);
24376   lang_hooks.types.register_builtin_type (float128_type_node, "__float128");
24377
24378   /* This macro is built by i386-builtin-types.awk.  */
24379   DEFINE_BUILTIN_PRIMITIVE_TYPES;
24380 }
24381
24382 static void
24383 ix86_init_builtins (void)
24384 {
24385   tree t;
24386
24387   ix86_init_builtin_types ();
24388
24389   /* TFmode support builtins.  */
24390   def_builtin_const (0, "__builtin_infq",
24391                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_INFQ);
24392   def_builtin_const (0, "__builtin_huge_valq",
24393                      FLOAT128_FTYPE_VOID, IX86_BUILTIN_HUGE_VALQ);
24394
24395   /* We will expand them to normal call if SSE2 isn't available since
24396      they are used by libgcc. */
24397   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128);
24398   t = add_builtin_function ("__builtin_fabsq", t, IX86_BUILTIN_FABSQ,
24399                             BUILT_IN_MD, "__fabstf2", NULL_TREE);
24400   TREE_READONLY (t) = 1;
24401   ix86_builtins[(int) IX86_BUILTIN_FABSQ] = t;
24402
24403   t = ix86_get_builtin_func_type (FLOAT128_FTYPE_FLOAT128_FLOAT128);
24404   t = add_builtin_function ("__builtin_copysignq", t, IX86_BUILTIN_COPYSIGNQ,
24405                             BUILT_IN_MD, "__copysigntf3", NULL_TREE);
24406   TREE_READONLY (t) = 1;
24407   ix86_builtins[(int) IX86_BUILTIN_COPYSIGNQ] = t;
24408
24409   ix86_init_mmx_sse_builtins ();
24410
24411   if (TARGET_64BIT)
24412     ix86_init_builtins_va_builtins_abi ();
24413 }
24414
24415 /* Return the ix86 builtin for CODE.  */
24416
24417 static tree
24418 ix86_builtin_decl (unsigned code, bool initialize_p ATTRIBUTE_UNUSED)
24419 {
24420   if (code >= IX86_BUILTIN_MAX)
24421     return error_mark_node;
24422
24423   return ix86_builtins[code];
24424 }
24425
24426 /* Errors in the source file can cause expand_expr to return const0_rtx
24427    where we expect a vector.  To avoid crashing, use one of the vector
24428    clear instructions.  */
24429 static rtx
24430 safe_vector_operand (rtx x, enum machine_mode mode)
24431 {
24432   if (x == const0_rtx)
24433     x = CONST0_RTX (mode);
24434   return x;
24435 }
24436
24437 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
24438
24439 static rtx
24440 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
24441 {
24442   rtx pat;
24443   tree arg0 = CALL_EXPR_ARG (exp, 0);
24444   tree arg1 = CALL_EXPR_ARG (exp, 1);
24445   rtx op0 = expand_normal (arg0);
24446   rtx op1 = expand_normal (arg1);
24447   enum machine_mode tmode = insn_data[icode].operand[0].mode;
24448   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
24449   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
24450
24451   if (VECTOR_MODE_P (mode0))
24452     op0 = safe_vector_operand (op0, mode0);
24453   if (VECTOR_MODE_P (mode1))
24454     op1 = safe_vector_operand (op1, mode1);
24455
24456   if (optimize || !target
24457       || GET_MODE (target) != tmode
24458       || !insn_data[icode].operand[0].predicate (target, tmode))
24459     target = gen_reg_rtx (tmode);
24460
24461   if (GET_MODE (op1) == SImode && mode1 == TImode)
24462     {
24463       rtx x = gen_reg_rtx (V4SImode);
24464       emit_insn (gen_sse2_loadd (x, op1));
24465       op1 = gen_lowpart (TImode, x);
24466     }
24467
24468   if (!insn_data[icode].operand[1].predicate (op0, mode0))
24469     op0 = copy_to_mode_reg (mode0, op0);
24470   if (!insn_data[icode].operand[2].predicate (op1, mode1))
24471     op1 = copy_to_mode_reg (mode1, op1);
24472
24473   pat = GEN_FCN (icode) (target, op0, op1);
24474   if (! pat)
24475     return 0;
24476
24477   emit_insn (pat);
24478
24479   return target;
24480 }
24481
24482 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
24483
24484 static rtx
24485 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
24486                                enum ix86_builtin_func_type m_type,
24487                                enum rtx_code sub_code)
24488 {
24489   rtx pat;
24490   int i;
24491   int nargs;
24492   bool comparison_p = false;
24493   bool tf_p = false;
24494   bool last_arg_constant = false;
24495   int num_memory = 0;
24496   struct {
24497     rtx op;
24498     enum machine_mode mode;
24499   } args[4];
24500
24501   enum machine_mode tmode = insn_data[icode].operand[0].mode;
24502
24503   switch (m_type)
24504     {
24505     case MULTI_ARG_4_DF2_DI_I:
24506     case MULTI_ARG_4_DF2_DI_I1:
24507     case MULTI_ARG_4_SF2_SI_I:
24508     case MULTI_ARG_4_SF2_SI_I1:
24509       nargs = 4;
24510       last_arg_constant = true;
24511       break;
24512
24513     case MULTI_ARG_3_SF:
24514     case MULTI_ARG_3_DF:
24515     case MULTI_ARG_3_SF2:
24516     case MULTI_ARG_3_DF2:
24517     case MULTI_ARG_3_DI:
24518     case MULTI_ARG_3_SI:
24519     case MULTI_ARG_3_SI_DI:
24520     case MULTI_ARG_3_HI:
24521     case MULTI_ARG_3_HI_SI:
24522     case MULTI_ARG_3_QI:
24523     case MULTI_ARG_3_DI2:
24524     case MULTI_ARG_3_SI2:
24525     case MULTI_ARG_3_HI2:
24526     case MULTI_ARG_3_QI2:
24527       nargs = 3;
24528       break;
24529
24530     case MULTI_ARG_2_SF:
24531     case MULTI_ARG_2_DF:
24532     case MULTI_ARG_2_DI:
24533     case MULTI_ARG_2_SI:
24534     case MULTI_ARG_2_HI:
24535     case MULTI_ARG_2_QI:
24536       nargs = 2;
24537       break;
24538
24539     case MULTI_ARG_2_DI_IMM:
24540     case MULTI_ARG_2_SI_IMM:
24541     case MULTI_ARG_2_HI_IMM:
24542     case MULTI_ARG_2_QI_IMM:
24543       nargs = 2;
24544       last_arg_constant = true;
24545       break;
24546
24547     case MULTI_ARG_1_SF:
24548     case MULTI_ARG_1_DF:
24549     case MULTI_ARG_1_SF2:
24550     case MULTI_ARG_1_DF2:
24551     case MULTI_ARG_1_DI:
24552     case MULTI_ARG_1_SI:
24553     case MULTI_ARG_1_HI:
24554     case MULTI_ARG_1_QI:
24555     case MULTI_ARG_1_SI_DI:
24556     case MULTI_ARG_1_HI_DI:
24557     case MULTI_ARG_1_HI_SI:
24558     case MULTI_ARG_1_QI_DI:
24559     case MULTI_ARG_1_QI_SI:
24560     case MULTI_ARG_1_QI_HI:
24561       nargs = 1;
24562       break;
24563
24564     case MULTI_ARG_2_DI_CMP:
24565     case MULTI_ARG_2_SI_CMP:
24566     case MULTI_ARG_2_HI_CMP:
24567     case MULTI_ARG_2_QI_CMP:
24568       nargs = 2;
24569       comparison_p = true;
24570       break;
24571
24572     case MULTI_ARG_2_SF_TF:
24573     case MULTI_ARG_2_DF_TF:
24574     case MULTI_ARG_2_DI_TF:
24575     case MULTI_ARG_2_SI_TF:
24576     case MULTI_ARG_2_HI_TF:
24577     case MULTI_ARG_2_QI_TF:
24578       nargs = 2;
24579       tf_p = true;
24580       break;
24581
24582     default:
24583       gcc_unreachable ();
24584     }
24585
24586   if (optimize || !target
24587       || GET_MODE (target) != tmode
24588       || !insn_data[icode].operand[0].predicate (target, tmode))
24589     target = gen_reg_rtx (tmode);
24590
24591   gcc_assert (nargs <= 4);
24592
24593   for (i = 0; i < nargs; i++)
24594     {
24595       tree arg = CALL_EXPR_ARG (exp, i);
24596       rtx op = expand_normal (arg);
24597       int adjust = (comparison_p) ? 1 : 0;
24598       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
24599
24600       if (last_arg_constant && i == nargs-1)
24601         {
24602           if (!CONST_INT_P (op))
24603             {
24604               error ("last argument must be an immediate");
24605               return gen_reg_rtx (tmode);
24606             }
24607         }
24608       else
24609         {
24610           if (VECTOR_MODE_P (mode))
24611             op = safe_vector_operand (op, mode);
24612
24613           /* If we aren't optimizing, only allow one memory operand to be
24614              generated.  */
24615           if (memory_operand (op, mode))
24616             num_memory++;
24617
24618           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
24619
24620           if (optimize
24621               || !insn_data[icode].operand[i+adjust+1].predicate (op, mode)
24622               || num_memory > 1)
24623             op = force_reg (mode, op);
24624         }
24625
24626       args[i].op = op;
24627       args[i].mode = mode;
24628     }
24629
24630   switch (nargs)
24631     {
24632     case 1:
24633       pat = GEN_FCN (icode) (target, args[0].op);
24634       break;
24635
24636     case 2:
24637       if (tf_p)
24638         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
24639                                GEN_INT ((int)sub_code));
24640       else if (! comparison_p)
24641         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
24642       else
24643         {
24644           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
24645                                        args[0].op,
24646                                        args[1].op);
24647
24648           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
24649         }
24650       break;
24651
24652     case 3:
24653       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
24654       break;
24655
24656     case 4:
24657       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op, args[3].op);
24658       break;
24659
24660     default:
24661       gcc_unreachable ();
24662     }
24663
24664   if (! pat)
24665     return 0;
24666
24667   emit_insn (pat);
24668   return target;
24669 }
24670
24671 /* Subroutine of ix86_expand_args_builtin to take care of scalar unop
24672    insns with vec_merge.  */
24673
24674 static rtx
24675 ix86_expand_unop_vec_merge_builtin (enum insn_code icode, tree exp,
24676                                     rtx target)
24677 {
24678   rtx pat;
24679   tree arg0 = CALL_EXPR_ARG (exp, 0);
24680   rtx op1, op0 = expand_normal (arg0);
24681   enum machine_mode tmode = insn_data[icode].operand[0].mode;
24682   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
24683
24684   if (optimize || !target
24685       || GET_MODE (target) != tmode
24686       || !insn_data[icode].operand[0].predicate (target, tmode))
24687     target = gen_reg_rtx (tmode);
24688
24689   if (VECTOR_MODE_P (mode0))
24690     op0 = safe_vector_operand (op0, mode0);
24691
24692   if ((optimize && !register_operand (op0, mode0))
24693       || !insn_data[icode].operand[1].predicate (op0, mode0))
24694     op0 = copy_to_mode_reg (mode0, op0);
24695
24696   op1 = op0;
24697   if (!insn_data[icode].operand[2].predicate (op1, mode0))
24698     op1 = copy_to_mode_reg (mode0, op1);
24699
24700   pat = GEN_FCN (icode) (target, op0, op1);
24701   if (! pat)
24702     return 0;
24703   emit_insn (pat);
24704   return target;
24705 }
24706
24707 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
24708
24709 static rtx
24710 ix86_expand_sse_compare (const struct builtin_description *d,
24711                          tree exp, rtx target, bool swap)
24712 {
24713   rtx pat;
24714   tree arg0 = CALL_EXPR_ARG (exp, 0);
24715   tree arg1 = CALL_EXPR_ARG (exp, 1);
24716   rtx op0 = expand_normal (arg0);
24717   rtx op1 = expand_normal (arg1);
24718   rtx op2;
24719   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
24720   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
24721   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
24722   enum rtx_code comparison = d->comparison;
24723
24724   if (VECTOR_MODE_P (mode0))
24725     op0 = safe_vector_operand (op0, mode0);
24726   if (VECTOR_MODE_P (mode1))
24727     op1 = safe_vector_operand (op1, mode1);
24728
24729   /* Swap operands if we have a comparison that isn't available in
24730      hardware.  */
24731   if (swap)
24732     {
24733       rtx tmp = gen_reg_rtx (mode1);
24734       emit_move_insn (tmp, op1);
24735       op1 = op0;
24736       op0 = tmp;
24737     }
24738
24739   if (optimize || !target
24740       || GET_MODE (target) != tmode
24741       || !insn_data[d->icode].operand[0].predicate (target, tmode))
24742     target = gen_reg_rtx (tmode);
24743
24744   if ((optimize && !register_operand (op0, mode0))
24745       || !insn_data[d->icode].operand[1].predicate (op0, mode0))
24746     op0 = copy_to_mode_reg (mode0, op0);
24747   if ((optimize && !register_operand (op1, mode1))
24748       || !insn_data[d->icode].operand[2].predicate (op1, mode1))
24749     op1 = copy_to_mode_reg (mode1, op1);
24750
24751   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
24752   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
24753   if (! pat)
24754     return 0;
24755   emit_insn (pat);
24756   return target;
24757 }
24758
24759 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
24760
24761 static rtx
24762 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
24763                       rtx target)
24764 {
24765   rtx pat;
24766   tree arg0 = CALL_EXPR_ARG (exp, 0);
24767   tree arg1 = CALL_EXPR_ARG (exp, 1);
24768   rtx op0 = expand_normal (arg0);
24769   rtx op1 = expand_normal (arg1);
24770   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
24771   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
24772   enum rtx_code comparison = d->comparison;
24773
24774   if (VECTOR_MODE_P (mode0))
24775     op0 = safe_vector_operand (op0, mode0);
24776   if (VECTOR_MODE_P (mode1))
24777     op1 = safe_vector_operand (op1, mode1);
24778
24779   /* Swap operands if we have a comparison that isn't available in
24780      hardware.  */
24781   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
24782     {
24783       rtx tmp = op1;
24784       op1 = op0;
24785       op0 = tmp;
24786     }
24787
24788   target = gen_reg_rtx (SImode);
24789   emit_move_insn (target, const0_rtx);
24790   target = gen_rtx_SUBREG (QImode, target, 0);
24791
24792   if ((optimize && !register_operand (op0, mode0))
24793       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
24794     op0 = copy_to_mode_reg (mode0, op0);
24795   if ((optimize && !register_operand (op1, mode1))
24796       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
24797     op1 = copy_to_mode_reg (mode1, op1);
24798
24799   pat = GEN_FCN (d->icode) (op0, op1);
24800   if (! pat)
24801     return 0;
24802   emit_insn (pat);
24803   emit_insn (gen_rtx_SET (VOIDmode,
24804                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24805                           gen_rtx_fmt_ee (comparison, QImode,
24806                                           SET_DEST (pat),
24807                                           const0_rtx)));
24808
24809   return SUBREG_REG (target);
24810 }
24811
24812 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
24813
24814 static rtx
24815 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
24816                        rtx target)
24817 {
24818   rtx pat;
24819   tree arg0 = CALL_EXPR_ARG (exp, 0);
24820   tree arg1 = CALL_EXPR_ARG (exp, 1);
24821   rtx op0 = expand_normal (arg0);
24822   rtx op1 = expand_normal (arg1);
24823   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
24824   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
24825   enum rtx_code comparison = d->comparison;
24826
24827   if (VECTOR_MODE_P (mode0))
24828     op0 = safe_vector_operand (op0, mode0);
24829   if (VECTOR_MODE_P (mode1))
24830     op1 = safe_vector_operand (op1, mode1);
24831
24832   target = gen_reg_rtx (SImode);
24833   emit_move_insn (target, const0_rtx);
24834   target = gen_rtx_SUBREG (QImode, target, 0);
24835
24836   if ((optimize && !register_operand (op0, mode0))
24837       || !insn_data[d->icode].operand[0].predicate (op0, mode0))
24838     op0 = copy_to_mode_reg (mode0, op0);
24839   if ((optimize && !register_operand (op1, mode1))
24840       || !insn_data[d->icode].operand[1].predicate (op1, mode1))
24841     op1 = copy_to_mode_reg (mode1, op1);
24842
24843   pat = GEN_FCN (d->icode) (op0, op1);
24844   if (! pat)
24845     return 0;
24846   emit_insn (pat);
24847   emit_insn (gen_rtx_SET (VOIDmode,
24848                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24849                           gen_rtx_fmt_ee (comparison, QImode,
24850                                           SET_DEST (pat),
24851                                           const0_rtx)));
24852
24853   return SUBREG_REG (target);
24854 }
24855
24856 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
24857
24858 static rtx
24859 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
24860                           tree exp, rtx target)
24861 {
24862   rtx pat;
24863   tree arg0 = CALL_EXPR_ARG (exp, 0);
24864   tree arg1 = CALL_EXPR_ARG (exp, 1);
24865   tree arg2 = CALL_EXPR_ARG (exp, 2);
24866   tree arg3 = CALL_EXPR_ARG (exp, 3);
24867   tree arg4 = CALL_EXPR_ARG (exp, 4);
24868   rtx scratch0, scratch1;
24869   rtx op0 = expand_normal (arg0);
24870   rtx op1 = expand_normal (arg1);
24871   rtx op2 = expand_normal (arg2);
24872   rtx op3 = expand_normal (arg3);
24873   rtx op4 = expand_normal (arg4);
24874   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
24875
24876   tmode0 = insn_data[d->icode].operand[0].mode;
24877   tmode1 = insn_data[d->icode].operand[1].mode;
24878   modev2 = insn_data[d->icode].operand[2].mode;
24879   modei3 = insn_data[d->icode].operand[3].mode;
24880   modev4 = insn_data[d->icode].operand[4].mode;
24881   modei5 = insn_data[d->icode].operand[5].mode;
24882   modeimm = insn_data[d->icode].operand[6].mode;
24883
24884   if (VECTOR_MODE_P (modev2))
24885     op0 = safe_vector_operand (op0, modev2);
24886   if (VECTOR_MODE_P (modev4))
24887     op2 = safe_vector_operand (op2, modev4);
24888
24889   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
24890     op0 = copy_to_mode_reg (modev2, op0);
24891   if (!insn_data[d->icode].operand[3].predicate (op1, modei3))
24892     op1 = copy_to_mode_reg (modei3, op1);
24893   if ((optimize && !register_operand (op2, modev4))
24894       || !insn_data[d->icode].operand[4].predicate (op2, modev4))
24895     op2 = copy_to_mode_reg (modev4, op2);
24896   if (!insn_data[d->icode].operand[5].predicate (op3, modei5))
24897     op3 = copy_to_mode_reg (modei5, op3);
24898
24899   if (!insn_data[d->icode].operand[6].predicate (op4, modeimm))
24900     {
24901       error ("the fifth argument must be a 8-bit immediate");
24902       return const0_rtx;
24903     }
24904
24905   if (d->code == IX86_BUILTIN_PCMPESTRI128)
24906     {
24907       if (optimize || !target
24908           || GET_MODE (target) != tmode0
24909           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
24910         target = gen_reg_rtx (tmode0);
24911
24912       scratch1 = gen_reg_rtx (tmode1);
24913
24914       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
24915     }
24916   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
24917     {
24918       if (optimize || !target
24919           || GET_MODE (target) != tmode1
24920           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
24921         target = gen_reg_rtx (tmode1);
24922
24923       scratch0 = gen_reg_rtx (tmode0);
24924
24925       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
24926     }
24927   else
24928     {
24929       gcc_assert (d->flag);
24930
24931       scratch0 = gen_reg_rtx (tmode0);
24932       scratch1 = gen_reg_rtx (tmode1);
24933
24934       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
24935     }
24936
24937   if (! pat)
24938     return 0;
24939
24940   emit_insn (pat);
24941
24942   if (d->flag)
24943     {
24944       target = gen_reg_rtx (SImode);
24945       emit_move_insn (target, const0_rtx);
24946       target = gen_rtx_SUBREG (QImode, target, 0);
24947
24948       emit_insn
24949         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
24950                       gen_rtx_fmt_ee (EQ, QImode,
24951                                       gen_rtx_REG ((enum machine_mode) d->flag,
24952                                                    FLAGS_REG),
24953                                       const0_rtx)));
24954       return SUBREG_REG (target);
24955     }
24956   else
24957     return target;
24958 }
24959
24960
24961 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
24962
24963 static rtx
24964 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
24965                           tree exp, rtx target)
24966 {
24967   rtx pat;
24968   tree arg0 = CALL_EXPR_ARG (exp, 0);
24969   tree arg1 = CALL_EXPR_ARG (exp, 1);
24970   tree arg2 = CALL_EXPR_ARG (exp, 2);
24971   rtx scratch0, scratch1;
24972   rtx op0 = expand_normal (arg0);
24973   rtx op1 = expand_normal (arg1);
24974   rtx op2 = expand_normal (arg2);
24975   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
24976
24977   tmode0 = insn_data[d->icode].operand[0].mode;
24978   tmode1 = insn_data[d->icode].operand[1].mode;
24979   modev2 = insn_data[d->icode].operand[2].mode;
24980   modev3 = insn_data[d->icode].operand[3].mode;
24981   modeimm = insn_data[d->icode].operand[4].mode;
24982
24983   if (VECTOR_MODE_P (modev2))
24984     op0 = safe_vector_operand (op0, modev2);
24985   if (VECTOR_MODE_P (modev3))
24986     op1 = safe_vector_operand (op1, modev3);
24987
24988   if (!insn_data[d->icode].operand[2].predicate (op0, modev2))
24989     op0 = copy_to_mode_reg (modev2, op0);
24990   if ((optimize && !register_operand (op1, modev3))
24991       || !insn_data[d->icode].operand[3].predicate (op1, modev3))
24992     op1 = copy_to_mode_reg (modev3, op1);
24993
24994   if (!insn_data[d->icode].operand[4].predicate (op2, modeimm))
24995     {
24996       error ("the third argument must be a 8-bit immediate");
24997       return const0_rtx;
24998     }
24999
25000   if (d->code == IX86_BUILTIN_PCMPISTRI128)
25001     {
25002       if (optimize || !target
25003           || GET_MODE (target) != tmode0
25004           || !insn_data[d->icode].operand[0].predicate (target, tmode0))
25005         target = gen_reg_rtx (tmode0);
25006
25007       scratch1 = gen_reg_rtx (tmode1);
25008
25009       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
25010     }
25011   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
25012     {
25013       if (optimize || !target
25014           || GET_MODE (target) != tmode1
25015           || !insn_data[d->icode].operand[1].predicate (target, tmode1))
25016         target = gen_reg_rtx (tmode1);
25017
25018       scratch0 = gen_reg_rtx (tmode0);
25019
25020       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
25021     }
25022   else
25023     {
25024       gcc_assert (d->flag);
25025
25026       scratch0 = gen_reg_rtx (tmode0);
25027       scratch1 = gen_reg_rtx (tmode1);
25028
25029       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
25030     }
25031
25032   if (! pat)
25033     return 0;
25034
25035   emit_insn (pat);
25036
25037   if (d->flag)
25038     {
25039       target = gen_reg_rtx (SImode);
25040       emit_move_insn (target, const0_rtx);
25041       target = gen_rtx_SUBREG (QImode, target, 0);
25042
25043       emit_insn
25044         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
25045                       gen_rtx_fmt_ee (EQ, QImode,
25046                                       gen_rtx_REG ((enum machine_mode) d->flag,
25047                                                    FLAGS_REG),
25048                                       const0_rtx)));
25049       return SUBREG_REG (target);
25050     }
25051   else
25052     return target;
25053 }
25054
25055 /* Subroutine of ix86_expand_builtin to take care of insns with
25056    variable number of operands.  */
25057
25058 static rtx
25059 ix86_expand_args_builtin (const struct builtin_description *d,
25060                           tree exp, rtx target)
25061 {
25062   rtx pat, real_target;
25063   unsigned int i, nargs;
25064   unsigned int nargs_constant = 0;
25065   int num_memory = 0;
25066   struct
25067     {
25068       rtx op;
25069       enum machine_mode mode;
25070     } args[4];
25071   bool last_arg_count = false;
25072   enum insn_code icode = d->icode;
25073   const struct insn_data_d *insn_p = &insn_data[icode];
25074   enum machine_mode tmode = insn_p->operand[0].mode;
25075   enum machine_mode rmode = VOIDmode;
25076   bool swap = false;
25077   enum rtx_code comparison = d->comparison;
25078
25079   switch ((enum ix86_builtin_func_type) d->flag)
25080     {
25081     case INT_FTYPE_V8SF_V8SF_PTEST:
25082     case INT_FTYPE_V4DI_V4DI_PTEST:
25083     case INT_FTYPE_V4DF_V4DF_PTEST:
25084     case INT_FTYPE_V4SF_V4SF_PTEST:
25085     case INT_FTYPE_V2DI_V2DI_PTEST:
25086     case INT_FTYPE_V2DF_V2DF_PTEST:
25087       return ix86_expand_sse_ptest (d, exp, target);
25088     case FLOAT128_FTYPE_FLOAT128:
25089     case FLOAT_FTYPE_FLOAT:
25090     case INT_FTYPE_INT:
25091     case UINT64_FTYPE_INT:
25092     case UINT16_FTYPE_UINT16:
25093     case INT64_FTYPE_INT64:
25094     case INT64_FTYPE_V4SF:
25095     case INT64_FTYPE_V2DF:
25096     case INT_FTYPE_V16QI:
25097     case INT_FTYPE_V8QI:
25098     case INT_FTYPE_V8SF:
25099     case INT_FTYPE_V4DF:
25100     case INT_FTYPE_V4SF:
25101     case INT_FTYPE_V2DF:
25102     case V16QI_FTYPE_V16QI:
25103     case V8SI_FTYPE_V8SF:
25104     case V8SI_FTYPE_V4SI:
25105     case V8HI_FTYPE_V8HI:
25106     case V8HI_FTYPE_V16QI:
25107     case V8QI_FTYPE_V8QI:
25108     case V8SF_FTYPE_V8SF:
25109     case V8SF_FTYPE_V8SI:
25110     case V8SF_FTYPE_V4SF:
25111     case V8SF_FTYPE_V8HI:
25112     case V4SI_FTYPE_V4SI:
25113     case V4SI_FTYPE_V16QI:
25114     case V4SI_FTYPE_V4SF:
25115     case V4SI_FTYPE_V8SI:
25116     case V4SI_FTYPE_V8HI:
25117     case V4SI_FTYPE_V4DF:
25118     case V4SI_FTYPE_V2DF:
25119     case V4HI_FTYPE_V4HI:
25120     case V4DF_FTYPE_V4DF:
25121     case V4DF_FTYPE_V4SI:
25122     case V4DF_FTYPE_V4SF:
25123     case V4DF_FTYPE_V2DF:
25124     case V4SF_FTYPE_V4SF:
25125     case V4SF_FTYPE_V4SI:
25126     case V4SF_FTYPE_V8SF:
25127     case V4SF_FTYPE_V4DF:
25128     case V4SF_FTYPE_V8HI:
25129     case V4SF_FTYPE_V2DF:
25130     case V2DI_FTYPE_V2DI:
25131     case V2DI_FTYPE_V16QI:
25132     case V2DI_FTYPE_V8HI:
25133     case V2DI_FTYPE_V4SI:
25134     case V2DF_FTYPE_V2DF:
25135     case V2DF_FTYPE_V4SI:
25136     case V2DF_FTYPE_V4DF:
25137     case V2DF_FTYPE_V4SF:
25138     case V2DF_FTYPE_V2SI:
25139     case V2SI_FTYPE_V2SI:
25140     case V2SI_FTYPE_V4SF:
25141     case V2SI_FTYPE_V2SF:
25142     case V2SI_FTYPE_V2DF:
25143     case V2SF_FTYPE_V2SF:
25144     case V2SF_FTYPE_V2SI:
25145       nargs = 1;
25146       break;
25147     case V4SF_FTYPE_V4SF_VEC_MERGE:
25148     case V2DF_FTYPE_V2DF_VEC_MERGE:
25149       return ix86_expand_unop_vec_merge_builtin (icode, exp, target);
25150     case FLOAT128_FTYPE_FLOAT128_FLOAT128:
25151     case V16QI_FTYPE_V16QI_V16QI:
25152     case V16QI_FTYPE_V8HI_V8HI:
25153     case V8QI_FTYPE_V8QI_V8QI:
25154     case V8QI_FTYPE_V4HI_V4HI:
25155     case V8HI_FTYPE_V8HI_V8HI:
25156     case V8HI_FTYPE_V16QI_V16QI:
25157     case V8HI_FTYPE_V4SI_V4SI:
25158     case V8SF_FTYPE_V8SF_V8SF:
25159     case V8SF_FTYPE_V8SF_V8SI:
25160     case V4SI_FTYPE_V4SI_V4SI:
25161     case V4SI_FTYPE_V8HI_V8HI:
25162     case V4SI_FTYPE_V4SF_V4SF:
25163     case V4SI_FTYPE_V2DF_V2DF:
25164     case V4HI_FTYPE_V4HI_V4HI:
25165     case V4HI_FTYPE_V8QI_V8QI:
25166     case V4HI_FTYPE_V2SI_V2SI:
25167     case V4DF_FTYPE_V4DF_V4DF:
25168     case V4DF_FTYPE_V4DF_V4DI:
25169     case V4SF_FTYPE_V4SF_V4SF:
25170     case V4SF_FTYPE_V4SF_V4SI:
25171     case V4SF_FTYPE_V4SF_V2SI:
25172     case V4SF_FTYPE_V4SF_V2DF:
25173     case V4SF_FTYPE_V4SF_DI:
25174     case V4SF_FTYPE_V4SF_SI:
25175     case V2DI_FTYPE_V2DI_V2DI:
25176     case V2DI_FTYPE_V16QI_V16QI:
25177     case V2DI_FTYPE_V4SI_V4SI:
25178     case V2DI_FTYPE_V2DI_V16QI:
25179     case V2DI_FTYPE_V2DF_V2DF:
25180     case V2SI_FTYPE_V2SI_V2SI:
25181     case V2SI_FTYPE_V4HI_V4HI:
25182     case V2SI_FTYPE_V2SF_V2SF:
25183     case V2DF_FTYPE_V2DF_V2DF:
25184     case V2DF_FTYPE_V2DF_V4SF:
25185     case V2DF_FTYPE_V2DF_V2DI:
25186     case V2DF_FTYPE_V2DF_DI:
25187     case V2DF_FTYPE_V2DF_SI:
25188     case V2SF_FTYPE_V2SF_V2SF:
25189     case V1DI_FTYPE_V1DI_V1DI:
25190     case V1DI_FTYPE_V8QI_V8QI:
25191     case V1DI_FTYPE_V2SI_V2SI:
25192       if (comparison == UNKNOWN)
25193         return ix86_expand_binop_builtin (icode, exp, target);
25194       nargs = 2;
25195       break;
25196     case V4SF_FTYPE_V4SF_V4SF_SWAP:
25197     case V2DF_FTYPE_V2DF_V2DF_SWAP:
25198       gcc_assert (comparison != UNKNOWN);
25199       nargs = 2;
25200       swap = true;
25201       break;
25202     case V8HI_FTYPE_V8HI_V8HI_COUNT:
25203     case V8HI_FTYPE_V8HI_SI_COUNT:
25204     case V4SI_FTYPE_V4SI_V4SI_COUNT:
25205     case V4SI_FTYPE_V4SI_SI_COUNT:
25206     case V4HI_FTYPE_V4HI_V4HI_COUNT:
25207     case V4HI_FTYPE_V4HI_SI_COUNT:
25208     case V2DI_FTYPE_V2DI_V2DI_COUNT:
25209     case V2DI_FTYPE_V2DI_SI_COUNT:
25210     case V2SI_FTYPE_V2SI_V2SI_COUNT:
25211     case V2SI_FTYPE_V2SI_SI_COUNT:
25212     case V1DI_FTYPE_V1DI_V1DI_COUNT:
25213     case V1DI_FTYPE_V1DI_SI_COUNT:
25214       nargs = 2;
25215       last_arg_count = true;
25216       break;
25217     case UINT64_FTYPE_UINT64_UINT64:
25218     case UINT_FTYPE_UINT_UINT:
25219     case UINT_FTYPE_UINT_USHORT:
25220     case UINT_FTYPE_UINT_UCHAR:
25221     case UINT16_FTYPE_UINT16_INT:
25222     case UINT8_FTYPE_UINT8_INT:
25223       nargs = 2;
25224       break;
25225     case V2DI_FTYPE_V2DI_INT_CONVERT:
25226       nargs = 2;
25227       rmode = V1TImode;
25228       nargs_constant = 1;
25229       break;
25230     case V8HI_FTYPE_V8HI_INT:
25231     case V8HI_FTYPE_V8SF_INT:
25232     case V8HI_FTYPE_V4SF_INT:
25233     case V8SF_FTYPE_V8SF_INT:
25234     case V4SI_FTYPE_V4SI_INT:
25235     case V4SI_FTYPE_V8SI_INT:
25236     case V4HI_FTYPE_V4HI_INT:
25237     case V4DF_FTYPE_V4DF_INT:
25238     case V4SF_FTYPE_V4SF_INT:
25239     case V4SF_FTYPE_V8SF_INT:
25240     case V2DI_FTYPE_V2DI_INT:
25241     case V2DF_FTYPE_V2DF_INT:
25242     case V2DF_FTYPE_V4DF_INT:
25243       nargs = 2;
25244       nargs_constant = 1;
25245       break;
25246     case V16QI_FTYPE_V16QI_V16QI_V16QI:
25247     case V8SF_FTYPE_V8SF_V8SF_V8SF:
25248     case V4DF_FTYPE_V4DF_V4DF_V4DF:
25249     case V4SF_FTYPE_V4SF_V4SF_V4SF:
25250     case V2DF_FTYPE_V2DF_V2DF_V2DF:
25251       nargs = 3;
25252       break;
25253     case V16QI_FTYPE_V16QI_V16QI_INT:
25254     case V8HI_FTYPE_V8HI_V8HI_INT:
25255     case V8SI_FTYPE_V8SI_V8SI_INT:
25256     case V8SI_FTYPE_V8SI_V4SI_INT:
25257     case V8SF_FTYPE_V8SF_V8SF_INT:
25258     case V8SF_FTYPE_V8SF_V4SF_INT:
25259     case V4SI_FTYPE_V4SI_V4SI_INT:
25260     case V4DF_FTYPE_V4DF_V4DF_INT:
25261     case V4DF_FTYPE_V4DF_V2DF_INT:
25262     case V4SF_FTYPE_V4SF_V4SF_INT:
25263     case V2DI_FTYPE_V2DI_V2DI_INT:
25264     case V2DF_FTYPE_V2DF_V2DF_INT:
25265       nargs = 3;
25266       nargs_constant = 1;
25267       break;
25268     case V2DI_FTYPE_V2DI_V2DI_INT_CONVERT:
25269       nargs = 3;
25270       rmode = V2DImode;
25271       nargs_constant = 1;
25272       break;
25273     case V1DI_FTYPE_V1DI_V1DI_INT_CONVERT:
25274       nargs = 3;
25275       rmode = DImode;
25276       nargs_constant = 1;
25277       break;
25278     case V2DI_FTYPE_V2DI_UINT_UINT:
25279       nargs = 3;
25280       nargs_constant = 2;
25281       break;
25282     case V2DF_FTYPE_V2DF_V2DF_V2DI_INT:
25283     case V4DF_FTYPE_V4DF_V4DF_V4DI_INT:
25284     case V4SF_FTYPE_V4SF_V4SF_V4SI_INT:
25285     case V8SF_FTYPE_V8SF_V8SF_V8SI_INT:
25286       nargs = 4;
25287       nargs_constant = 1;
25288       break;
25289     case V2DI_FTYPE_V2DI_V2DI_UINT_UINT:
25290       nargs = 4;
25291       nargs_constant = 2;
25292       break;
25293     default:
25294       gcc_unreachable ();
25295     }
25296
25297   gcc_assert (nargs <= ARRAY_SIZE (args));
25298
25299   if (comparison != UNKNOWN)
25300     {
25301       gcc_assert (nargs == 2);
25302       return ix86_expand_sse_compare (d, exp, target, swap);
25303     }
25304
25305   if (rmode == VOIDmode || rmode == tmode)
25306     {
25307       if (optimize
25308           || target == 0
25309           || GET_MODE (target) != tmode
25310           || !insn_p->operand[0].predicate (target, tmode))
25311         target = gen_reg_rtx (tmode);
25312       real_target = target;
25313     }
25314   else
25315     {
25316       target = gen_reg_rtx (rmode);
25317       real_target = simplify_gen_subreg (tmode, target, rmode, 0);
25318     }
25319
25320   for (i = 0; i < nargs; i++)
25321     {
25322       tree arg = CALL_EXPR_ARG (exp, i);
25323       rtx op = expand_normal (arg);
25324       enum machine_mode mode = insn_p->operand[i + 1].mode;
25325       bool match = insn_p->operand[i + 1].predicate (op, mode);
25326
25327       if (last_arg_count && (i + 1) == nargs)
25328         {
25329           /* SIMD shift insns take either an 8-bit immediate or
25330              register as count.  But builtin functions take int as
25331              count.  If count doesn't match, we put it in register.  */
25332           if (!match)
25333             {
25334               op = simplify_gen_subreg (SImode, op, GET_MODE (op), 0);
25335               if (!insn_p->operand[i + 1].predicate (op, mode))
25336                 op = copy_to_reg (op);
25337             }
25338         }
25339       else if ((nargs - i) <= nargs_constant)
25340         {
25341           if (!match)
25342             switch (icode)
25343               {
25344               case CODE_FOR_sse4_1_roundpd:
25345               case CODE_FOR_sse4_1_roundps:
25346               case CODE_FOR_sse4_1_roundsd:
25347               case CODE_FOR_sse4_1_roundss:
25348               case CODE_FOR_sse4_1_blendps:
25349               case CODE_FOR_avx_blendpd256:
25350               case CODE_FOR_avx_vpermilv4df:
25351               case CODE_FOR_avx_roundpd256:
25352               case CODE_FOR_avx_roundps256:
25353                 error ("the last argument must be a 4-bit immediate");
25354                 return const0_rtx;
25355
25356               case CODE_FOR_sse4_1_blendpd:
25357               case CODE_FOR_avx_vpermilv2df:
25358               case CODE_FOR_xop_vpermil2v2df3:
25359               case CODE_FOR_xop_vpermil2v4sf3:
25360               case CODE_FOR_xop_vpermil2v4df3:
25361               case CODE_FOR_xop_vpermil2v8sf3:
25362                 error ("the last argument must be a 2-bit immediate");
25363                 return const0_rtx;
25364
25365               case CODE_FOR_avx_vextractf128v4df:
25366               case CODE_FOR_avx_vextractf128v8sf:
25367               case CODE_FOR_avx_vextractf128v8si:
25368               case CODE_FOR_avx_vinsertf128v4df:
25369               case CODE_FOR_avx_vinsertf128v8sf:
25370               case CODE_FOR_avx_vinsertf128v8si:
25371                 error ("the last argument must be a 1-bit immediate");
25372                 return const0_rtx;
25373
25374               case CODE_FOR_avx_cmpsdv2df3:
25375               case CODE_FOR_avx_cmpssv4sf3:
25376               case CODE_FOR_avx_cmppdv2df3:
25377               case CODE_FOR_avx_cmppsv4sf3:
25378               case CODE_FOR_avx_cmppdv4df3:
25379               case CODE_FOR_avx_cmppsv8sf3:
25380                 error ("the last argument must be a 5-bit immediate");
25381                 return const0_rtx;
25382
25383              default:
25384                 switch (nargs_constant)
25385                   {
25386                   case 2:
25387                     if ((nargs - i) == nargs_constant)
25388                       {
25389                         error ("the next to last argument must be an 8-bit immediate");
25390                         break;
25391                       }
25392                   case 1:
25393                     error ("the last argument must be an 8-bit immediate");
25394                     break;
25395                   default:
25396                     gcc_unreachable ();
25397                   }
25398                 return const0_rtx;
25399               }
25400         }
25401       else
25402         {
25403           if (VECTOR_MODE_P (mode))
25404             op = safe_vector_operand (op, mode);
25405
25406           /* If we aren't optimizing, only allow one memory operand to
25407              be generated.  */
25408           if (memory_operand (op, mode))
25409             num_memory++;
25410
25411           if (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode)
25412             {
25413               if (optimize || !match || num_memory > 1)
25414                 op = copy_to_mode_reg (mode, op);
25415             }
25416           else
25417             {
25418               op = copy_to_reg (op);
25419               op = simplify_gen_subreg (mode, op, GET_MODE (op), 0);
25420             }
25421         }
25422
25423       args[i].op = op;
25424       args[i].mode = mode;
25425     }
25426
25427   switch (nargs)
25428     {
25429     case 1:
25430       pat = GEN_FCN (icode) (real_target, args[0].op);
25431       break;
25432     case 2:
25433       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op);
25434       break;
25435     case 3:
25436       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
25437                              args[2].op);
25438       break;
25439     case 4:
25440       pat = GEN_FCN (icode) (real_target, args[0].op, args[1].op,
25441                              args[2].op, args[3].op);
25442       break;
25443     default:
25444       gcc_unreachable ();
25445     }
25446
25447   if (! pat)
25448     return 0;
25449
25450   emit_insn (pat);
25451   return target;
25452 }
25453
25454 /* Subroutine of ix86_expand_builtin to take care of special insns
25455    with variable number of operands.  */
25456
25457 static rtx
25458 ix86_expand_special_args_builtin (const struct builtin_description *d,
25459                                     tree exp, rtx target)
25460 {
25461   tree arg;
25462   rtx pat, op;
25463   unsigned int i, nargs, arg_adjust, memory;
25464   struct
25465     {
25466       rtx op;
25467       enum machine_mode mode;
25468     } args[3];
25469   enum insn_code icode = d->icode;
25470   bool last_arg_constant = false;
25471   const struct insn_data_d *insn_p = &insn_data[icode];
25472   enum machine_mode tmode = insn_p->operand[0].mode;
25473   enum { load, store } klass;
25474
25475   switch ((enum ix86_builtin_func_type) d->flag)
25476     {
25477     case VOID_FTYPE_VOID:
25478       emit_insn (GEN_FCN (icode) (target));
25479       return 0;
25480     case VOID_FTYPE_UINT64:
25481     case VOID_FTYPE_UNSIGNED:
25482       nargs = 0;
25483       klass = store;
25484       memory = 0;
25485       break;
25486       break;
25487     case UINT64_FTYPE_VOID:
25488     case UNSIGNED_FTYPE_VOID:
25489     case UINT16_FTYPE_VOID:
25490       nargs = 0;
25491       klass = load;
25492       memory = 0;
25493       break;
25494     case UINT64_FTYPE_PUNSIGNED:
25495     case V2DI_FTYPE_PV2DI:
25496     case V32QI_FTYPE_PCCHAR:
25497     case V16QI_FTYPE_PCCHAR:
25498     case V8SF_FTYPE_PCV4SF:
25499     case V8SF_FTYPE_PCFLOAT:
25500     case V4SF_FTYPE_PCFLOAT:
25501     case V4DF_FTYPE_PCV2DF:
25502     case V4DF_FTYPE_PCDOUBLE:
25503     case V2DF_FTYPE_PCDOUBLE:
25504     case VOID_FTYPE_PVOID:
25505       nargs = 1;
25506       klass = load;
25507       memory = 0;
25508       break;
25509     case VOID_FTYPE_PV2SF_V4SF:
25510     case VOID_FTYPE_PV4DI_V4DI:
25511     case VOID_FTYPE_PV2DI_V2DI:
25512     case VOID_FTYPE_PCHAR_V32QI:
25513     case VOID_FTYPE_PCHAR_V16QI:
25514     case VOID_FTYPE_PFLOAT_V8SF:
25515     case VOID_FTYPE_PFLOAT_V4SF:
25516     case VOID_FTYPE_PDOUBLE_V4DF:
25517     case VOID_FTYPE_PDOUBLE_V2DF:
25518     case VOID_FTYPE_PULONGLONG_ULONGLONG:
25519     case VOID_FTYPE_PINT_INT:
25520       nargs = 1;
25521       klass = store;
25522       /* Reserve memory operand for target.  */
25523       memory = ARRAY_SIZE (args);
25524       break;
25525     case V4SF_FTYPE_V4SF_PCV2SF:
25526     case V2DF_FTYPE_V2DF_PCDOUBLE:
25527       nargs = 2;
25528       klass = load;
25529       memory = 1;
25530       break;
25531     case V8SF_FTYPE_PCV8SF_V8SF:
25532     case V4DF_FTYPE_PCV4DF_V4DF:
25533     case V4SF_FTYPE_PCV4SF_V4SF:
25534     case V2DF_FTYPE_PCV2DF_V2DF:
25535       nargs = 2;
25536       klass = load;
25537       memory = 0;
25538       break;
25539     case VOID_FTYPE_PV8SF_V8SF_V8SF:
25540     case VOID_FTYPE_PV4DF_V4DF_V4DF:
25541     case VOID_FTYPE_PV4SF_V4SF_V4SF:
25542     case VOID_FTYPE_PV2DF_V2DF_V2DF:
25543       nargs = 2;
25544       klass = store;
25545       /* Reserve memory operand for target.  */
25546       memory = ARRAY_SIZE (args);
25547       break;
25548     case VOID_FTYPE_UINT_UINT_UINT:
25549     case VOID_FTYPE_UINT64_UINT_UINT:
25550     case UCHAR_FTYPE_UINT_UINT_UINT:
25551     case UCHAR_FTYPE_UINT64_UINT_UINT:
25552       nargs = 3;
25553       klass = load;
25554       memory = ARRAY_SIZE (args);
25555       last_arg_constant = true;
25556       break;
25557     default:
25558       gcc_unreachable ();
25559     }
25560
25561   gcc_assert (nargs <= ARRAY_SIZE (args));
25562
25563   if (klass == store)
25564     {
25565       arg = CALL_EXPR_ARG (exp, 0);
25566       op = expand_normal (arg);
25567       gcc_assert (target == 0);
25568       if (memory)
25569         target = gen_rtx_MEM (tmode, copy_to_mode_reg (Pmode, op));
25570       else
25571         target = force_reg (tmode, op);
25572       arg_adjust = 1;
25573     }
25574   else
25575     {
25576       arg_adjust = 0;
25577       if (optimize
25578           || target == 0
25579           || GET_MODE (target) != tmode
25580           || !insn_p->operand[0].predicate (target, tmode))
25581         target = gen_reg_rtx (tmode);
25582     }
25583
25584   for (i = 0; i < nargs; i++)
25585     {
25586       enum machine_mode mode = insn_p->operand[i + 1].mode;
25587       bool match;
25588
25589       arg = CALL_EXPR_ARG (exp, i + arg_adjust);
25590       op = expand_normal (arg);
25591       match = insn_p->operand[i + 1].predicate (op, mode);
25592
25593       if (last_arg_constant && (i + 1) == nargs)
25594         {
25595           if (!match)
25596             {
25597               if (icode == CODE_FOR_lwp_lwpvalsi3
25598                   || icode == CODE_FOR_lwp_lwpinssi3
25599                   || icode == CODE_FOR_lwp_lwpvaldi3
25600                   || icode == CODE_FOR_lwp_lwpinsdi3)
25601                 error ("the last argument must be a 32-bit immediate");
25602               else
25603                 error ("the last argument must be an 8-bit immediate");
25604               return const0_rtx;
25605             }
25606         }
25607       else
25608         {
25609           if (i == memory)
25610             {
25611               /* This must be the memory operand.  */
25612               op = gen_rtx_MEM (mode, copy_to_mode_reg (Pmode, op));
25613               gcc_assert (GET_MODE (op) == mode
25614                           || GET_MODE (op) == VOIDmode);
25615             }
25616           else
25617             {
25618               /* This must be register.  */
25619               if (VECTOR_MODE_P (mode))
25620                 op = safe_vector_operand (op, mode);
25621
25622               gcc_assert (GET_MODE (op) == mode
25623                           || GET_MODE (op) == VOIDmode);
25624               op = copy_to_mode_reg (mode, op);
25625             }
25626         }
25627
25628       args[i].op = op;
25629       args[i].mode = mode;
25630     }
25631
25632   switch (nargs)
25633     {
25634     case 0:
25635       pat = GEN_FCN (icode) (target);
25636       break;
25637     case 1:
25638       pat = GEN_FCN (icode) (target, args[0].op);
25639       break;
25640     case 2:
25641       pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
25642       break;
25643     case 3:
25644       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
25645       break;
25646     default:
25647       gcc_unreachable ();
25648     }
25649
25650   if (! pat)
25651     return 0;
25652   emit_insn (pat);
25653   return klass == store ? 0 : target;
25654 }
25655
25656 /* Return the integer constant in ARG.  Constrain it to be in the range
25657    of the subparts of VEC_TYPE; issue an error if not.  */
25658
25659 static int
25660 get_element_number (tree vec_type, tree arg)
25661 {
25662   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
25663
25664   if (!host_integerp (arg, 1)
25665       || (elt = tree_low_cst (arg, 1), elt > max))
25666     {
25667       error ("selector must be an integer constant in the range 0..%wi", max);
25668       return 0;
25669     }
25670
25671   return elt;
25672 }
25673
25674 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
25675    ix86_expand_vector_init.  We DO have language-level syntax for this, in
25676    the form of  (type){ init-list }.  Except that since we can't place emms
25677    instructions from inside the compiler, we can't allow the use of MMX
25678    registers unless the user explicitly asks for it.  So we do *not* define
25679    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
25680    we have builtins invoked by mmintrin.h that gives us license to emit
25681    these sorts of instructions.  */
25682
25683 static rtx
25684 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
25685 {
25686   enum machine_mode tmode = TYPE_MODE (type);
25687   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
25688   int i, n_elt = GET_MODE_NUNITS (tmode);
25689   rtvec v = rtvec_alloc (n_elt);
25690
25691   gcc_assert (VECTOR_MODE_P (tmode));
25692   gcc_assert (call_expr_nargs (exp) == n_elt);
25693
25694   for (i = 0; i < n_elt; ++i)
25695     {
25696       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
25697       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
25698     }
25699
25700   if (!target || !register_operand (target, tmode))
25701     target = gen_reg_rtx (tmode);
25702
25703   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
25704   return target;
25705 }
25706
25707 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
25708    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
25709    had a language-level syntax for referencing vector elements.  */
25710
25711 static rtx
25712 ix86_expand_vec_ext_builtin (tree exp, rtx target)
25713 {
25714   enum machine_mode tmode, mode0;
25715   tree arg0, arg1;
25716   int elt;
25717   rtx op0;
25718
25719   arg0 = CALL_EXPR_ARG (exp, 0);
25720   arg1 = CALL_EXPR_ARG (exp, 1);
25721
25722   op0 = expand_normal (arg0);
25723   elt = get_element_number (TREE_TYPE (arg0), arg1);
25724
25725   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
25726   mode0 = TYPE_MODE (TREE_TYPE (arg0));
25727   gcc_assert (VECTOR_MODE_P (mode0));
25728
25729   op0 = force_reg (mode0, op0);
25730
25731   if (optimize || !target || !register_operand (target, tmode))
25732     target = gen_reg_rtx (tmode);
25733
25734   ix86_expand_vector_extract (true, target, op0, elt);
25735
25736   return target;
25737 }
25738
25739 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
25740    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
25741    a language-level syntax for referencing vector elements.  */
25742
25743 static rtx
25744 ix86_expand_vec_set_builtin (tree exp)
25745 {
25746   enum machine_mode tmode, mode1;
25747   tree arg0, arg1, arg2;
25748   int elt;
25749   rtx op0, op1, target;
25750
25751   arg0 = CALL_EXPR_ARG (exp, 0);
25752   arg1 = CALL_EXPR_ARG (exp, 1);
25753   arg2 = CALL_EXPR_ARG (exp, 2);
25754
25755   tmode = TYPE_MODE (TREE_TYPE (arg0));
25756   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
25757   gcc_assert (VECTOR_MODE_P (tmode));
25758
25759   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
25760   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
25761   elt = get_element_number (TREE_TYPE (arg0), arg2);
25762
25763   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
25764     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
25765
25766   op0 = force_reg (tmode, op0);
25767   op1 = force_reg (mode1, op1);
25768
25769   /* OP0 is the source of these builtin functions and shouldn't be
25770      modified.  Create a copy, use it and return it as target.  */
25771   target = gen_reg_rtx (tmode);
25772   emit_move_insn (target, op0);
25773   ix86_expand_vector_set (true, target, op1, elt);
25774
25775   return target;
25776 }
25777
25778 /* Expand an expression EXP that calls a built-in function,
25779    with result going to TARGET if that's convenient
25780    (and in mode MODE if that's convenient).
25781    SUBTARGET may be used as the target for computing one of EXP's operands.
25782    IGNORE is nonzero if the value is to be ignored.  */
25783
25784 static rtx
25785 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
25786                      enum machine_mode mode ATTRIBUTE_UNUSED,
25787                      int ignore ATTRIBUTE_UNUSED)
25788 {
25789   const struct builtin_description *d;
25790   size_t i;
25791   enum insn_code icode;
25792   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
25793   tree arg0, arg1, arg2;
25794   rtx op0, op1, op2, pat;
25795   enum machine_mode mode0, mode1, mode2;
25796   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
25797
25798   /* Determine whether the builtin function is available under the current ISA.
25799      Originally the builtin was not created if it wasn't applicable to the
25800      current ISA based on the command line switches.  With function specific
25801      options, we need to check in the context of the function making the call
25802      whether it is supported.  */
25803   if (ix86_builtins_isa[fcode].isa
25804       && !(ix86_builtins_isa[fcode].isa & ix86_isa_flags))
25805     {
25806       char *opts = ix86_target_string (ix86_builtins_isa[fcode].isa, 0, NULL,
25807                                        NULL, NULL, false);
25808
25809       if (!opts)
25810         error ("%qE needs unknown isa option", fndecl);
25811       else
25812         {
25813           gcc_assert (opts != NULL);
25814           error ("%qE needs isa option %s", fndecl, opts);
25815           free (opts);
25816         }
25817       return const0_rtx;
25818     }
25819
25820   switch (fcode)
25821     {
25822     case IX86_BUILTIN_MASKMOVQ:
25823     case IX86_BUILTIN_MASKMOVDQU:
25824       icode = (fcode == IX86_BUILTIN_MASKMOVQ
25825                ? CODE_FOR_mmx_maskmovq
25826                : CODE_FOR_sse2_maskmovdqu);
25827       /* Note the arg order is different from the operand order.  */
25828       arg1 = CALL_EXPR_ARG (exp, 0);
25829       arg2 = CALL_EXPR_ARG (exp, 1);
25830       arg0 = CALL_EXPR_ARG (exp, 2);
25831       op0 = expand_normal (arg0);
25832       op1 = expand_normal (arg1);
25833       op2 = expand_normal (arg2);
25834       mode0 = insn_data[icode].operand[0].mode;
25835       mode1 = insn_data[icode].operand[1].mode;
25836       mode2 = insn_data[icode].operand[2].mode;
25837
25838       op0 = force_reg (Pmode, op0);
25839       op0 = gen_rtx_MEM (mode1, op0);
25840
25841       if (!insn_data[icode].operand[0].predicate (op0, mode0))
25842         op0 = copy_to_mode_reg (mode0, op0);
25843       if (!insn_data[icode].operand[1].predicate (op1, mode1))
25844         op1 = copy_to_mode_reg (mode1, op1);
25845       if (!insn_data[icode].operand[2].predicate (op2, mode2))
25846         op2 = copy_to_mode_reg (mode2, op2);
25847       pat = GEN_FCN (icode) (op0, op1, op2);
25848       if (! pat)
25849         return 0;
25850       emit_insn (pat);
25851       return 0;
25852
25853     case IX86_BUILTIN_LDMXCSR:
25854       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
25855       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
25856       emit_move_insn (target, op0);
25857       emit_insn (gen_sse_ldmxcsr (target));
25858       return 0;
25859
25860     case IX86_BUILTIN_STMXCSR:
25861       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
25862       emit_insn (gen_sse_stmxcsr (target));
25863       return copy_to_mode_reg (SImode, target);
25864
25865     case IX86_BUILTIN_CLFLUSH:
25866         arg0 = CALL_EXPR_ARG (exp, 0);
25867         op0 = expand_normal (arg0);
25868         icode = CODE_FOR_sse2_clflush;
25869         if (!insn_data[icode].operand[0].predicate (op0, Pmode))
25870             op0 = copy_to_mode_reg (Pmode, op0);
25871
25872         emit_insn (gen_sse2_clflush (op0));
25873         return 0;
25874
25875     case IX86_BUILTIN_MONITOR:
25876       arg0 = CALL_EXPR_ARG (exp, 0);
25877       arg1 = CALL_EXPR_ARG (exp, 1);
25878       arg2 = CALL_EXPR_ARG (exp, 2);
25879       op0 = expand_normal (arg0);
25880       op1 = expand_normal (arg1);
25881       op2 = expand_normal (arg2);
25882       if (!REG_P (op0))
25883         op0 = copy_to_mode_reg (Pmode, op0);
25884       if (!REG_P (op1))
25885         op1 = copy_to_mode_reg (SImode, op1);
25886       if (!REG_P (op2))
25887         op2 = copy_to_mode_reg (SImode, op2);
25888       emit_insn (ix86_gen_monitor (op0, op1, op2));
25889       return 0;
25890
25891     case IX86_BUILTIN_MWAIT:
25892       arg0 = CALL_EXPR_ARG (exp, 0);
25893       arg1 = CALL_EXPR_ARG (exp, 1);
25894       op0 = expand_normal (arg0);
25895       op1 = expand_normal (arg1);
25896       if (!REG_P (op0))
25897         op0 = copy_to_mode_reg (SImode, op0);
25898       if (!REG_P (op1))
25899         op1 = copy_to_mode_reg (SImode, op1);
25900       emit_insn (gen_sse3_mwait (op0, op1));
25901       return 0;
25902
25903     case IX86_BUILTIN_VEC_INIT_V2SI:
25904     case IX86_BUILTIN_VEC_INIT_V4HI:
25905     case IX86_BUILTIN_VEC_INIT_V8QI:
25906       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
25907
25908     case IX86_BUILTIN_VEC_EXT_V2DF:
25909     case IX86_BUILTIN_VEC_EXT_V2DI:
25910     case IX86_BUILTIN_VEC_EXT_V4SF:
25911     case IX86_BUILTIN_VEC_EXT_V4SI:
25912     case IX86_BUILTIN_VEC_EXT_V8HI:
25913     case IX86_BUILTIN_VEC_EXT_V2SI:
25914     case IX86_BUILTIN_VEC_EXT_V4HI:
25915     case IX86_BUILTIN_VEC_EXT_V16QI:
25916       return ix86_expand_vec_ext_builtin (exp, target);
25917
25918     case IX86_BUILTIN_VEC_SET_V2DI:
25919     case IX86_BUILTIN_VEC_SET_V4SF:
25920     case IX86_BUILTIN_VEC_SET_V4SI:
25921     case IX86_BUILTIN_VEC_SET_V8HI:
25922     case IX86_BUILTIN_VEC_SET_V4HI:
25923     case IX86_BUILTIN_VEC_SET_V16QI:
25924       return ix86_expand_vec_set_builtin (exp);
25925
25926     case IX86_BUILTIN_VEC_PERM_V2DF:
25927     case IX86_BUILTIN_VEC_PERM_V4SF:
25928     case IX86_BUILTIN_VEC_PERM_V2DI:
25929     case IX86_BUILTIN_VEC_PERM_V4SI:
25930     case IX86_BUILTIN_VEC_PERM_V8HI:
25931     case IX86_BUILTIN_VEC_PERM_V16QI:
25932     case IX86_BUILTIN_VEC_PERM_V2DI_U:
25933     case IX86_BUILTIN_VEC_PERM_V4SI_U:
25934     case IX86_BUILTIN_VEC_PERM_V8HI_U:
25935     case IX86_BUILTIN_VEC_PERM_V16QI_U:
25936     case IX86_BUILTIN_VEC_PERM_V4DF:
25937     case IX86_BUILTIN_VEC_PERM_V8SF:
25938       return ix86_expand_vec_perm_builtin (exp);
25939
25940     case IX86_BUILTIN_INFQ:
25941     case IX86_BUILTIN_HUGE_VALQ:
25942       {
25943         REAL_VALUE_TYPE inf;
25944         rtx tmp;
25945
25946         real_inf (&inf);
25947         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
25948
25949         tmp = validize_mem (force_const_mem (mode, tmp));
25950
25951         if (target == 0)
25952           target = gen_reg_rtx (mode);
25953
25954         emit_move_insn (target, tmp);
25955         return target;
25956       }
25957
25958     case IX86_BUILTIN_LLWPCB:
25959       arg0 = CALL_EXPR_ARG (exp, 0);
25960       op0 = expand_normal (arg0);
25961       icode = CODE_FOR_lwp_llwpcb;
25962       if (!insn_data[icode].operand[0].predicate (op0, Pmode))
25963         op0 = copy_to_mode_reg (Pmode, op0);
25964       emit_insn (gen_lwp_llwpcb (op0));
25965       return 0;
25966
25967     case IX86_BUILTIN_SLWPCB:
25968       icode = CODE_FOR_lwp_slwpcb;
25969       if (!target
25970           || !insn_data[icode].operand[0].predicate (target, Pmode))
25971         target = gen_reg_rtx (Pmode);
25972       emit_insn (gen_lwp_slwpcb (target));
25973       return target;
25974
25975     default:
25976       break;
25977     }
25978
25979   for (i = 0, d = bdesc_special_args;
25980        i < ARRAY_SIZE (bdesc_special_args);
25981        i++, d++)
25982     if (d->code == fcode)
25983       return ix86_expand_special_args_builtin (d, exp, target);
25984
25985   for (i = 0, d = bdesc_args;
25986        i < ARRAY_SIZE (bdesc_args);
25987        i++, d++)
25988     if (d->code == fcode)
25989       switch (fcode)
25990         {
25991         case IX86_BUILTIN_FABSQ:
25992         case IX86_BUILTIN_COPYSIGNQ:
25993           if (!TARGET_SSE2)
25994             /* Emit a normal call if SSE2 isn't available.  */
25995             return expand_call (exp, target, ignore);
25996         default:
25997           return ix86_expand_args_builtin (d, exp, target);
25998         }
25999
26000   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
26001     if (d->code == fcode)
26002       return ix86_expand_sse_comi (d, exp, target);
26003
26004   for (i = 0, d = bdesc_pcmpestr;
26005        i < ARRAY_SIZE (bdesc_pcmpestr);
26006        i++, d++)
26007     if (d->code == fcode)
26008       return ix86_expand_sse_pcmpestr (d, exp, target);
26009
26010   for (i = 0, d = bdesc_pcmpistr;
26011        i < ARRAY_SIZE (bdesc_pcmpistr);
26012        i++, d++)
26013     if (d->code == fcode)
26014       return ix86_expand_sse_pcmpistr (d, exp, target);
26015
26016   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
26017     if (d->code == fcode)
26018       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
26019                                             (enum ix86_builtin_func_type)
26020                                             d->flag, d->comparison);
26021
26022   gcc_unreachable ();
26023 }
26024
26025 /* Returns a function decl for a vectorized version of the builtin function
26026    with builtin function code FN and the result vector type TYPE, or NULL_TREE
26027    if it is not available.  */
26028
26029 static tree
26030 ix86_builtin_vectorized_function (tree fndecl, tree type_out,
26031                                   tree type_in)
26032 {
26033   enum machine_mode in_mode, out_mode;
26034   int in_n, out_n;
26035   enum built_in_function fn = DECL_FUNCTION_CODE (fndecl);
26036
26037   if (TREE_CODE (type_out) != VECTOR_TYPE
26038       || TREE_CODE (type_in) != VECTOR_TYPE
26039       || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
26040     return NULL_TREE;
26041
26042   out_mode = TYPE_MODE (TREE_TYPE (type_out));
26043   out_n = TYPE_VECTOR_SUBPARTS (type_out);
26044   in_mode = TYPE_MODE (TREE_TYPE (type_in));
26045   in_n = TYPE_VECTOR_SUBPARTS (type_in);
26046
26047   switch (fn)
26048     {
26049     case BUILT_IN_SQRT:
26050       if (out_mode == DFmode && in_mode == DFmode)
26051         {
26052           if (out_n == 2 && in_n == 2)
26053             return ix86_builtins[IX86_BUILTIN_SQRTPD];
26054           else if (out_n == 4 && in_n == 4)
26055             return ix86_builtins[IX86_BUILTIN_SQRTPD256];
26056         }
26057       break;
26058
26059     case BUILT_IN_SQRTF:
26060       if (out_mode == SFmode && in_mode == SFmode)
26061         {
26062           if (out_n == 4 && in_n == 4)
26063             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
26064           else if (out_n == 8 && in_n == 8)
26065             return ix86_builtins[IX86_BUILTIN_SQRTPS_NR256];
26066         }
26067       break;
26068
26069     case BUILT_IN_LRINT:
26070       if (out_mode == SImode && out_n == 4
26071           && in_mode == DFmode && in_n == 2)
26072         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
26073       break;
26074
26075     case BUILT_IN_LRINTF:
26076       if (out_mode == SImode && in_mode == SFmode)
26077         {
26078           if (out_n == 4 && in_n == 4)
26079             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
26080           else if (out_n == 8 && in_n == 8)
26081             return ix86_builtins[IX86_BUILTIN_CVTPS2DQ256];
26082         }
26083       break;
26084
26085     case BUILT_IN_COPYSIGN:
26086       if (out_mode == DFmode && in_mode == DFmode)
26087         {
26088           if (out_n == 2 && in_n == 2)
26089             return ix86_builtins[IX86_BUILTIN_CPYSGNPD];
26090           else if (out_n == 4 && in_n == 4)
26091             return ix86_builtins[IX86_BUILTIN_CPYSGNPD256];
26092         }
26093       break;
26094
26095     case BUILT_IN_COPYSIGNF:
26096       if (out_mode == SFmode && in_mode == SFmode)
26097         {
26098           if (out_n == 4 && in_n == 4)
26099             return ix86_builtins[IX86_BUILTIN_CPYSGNPS];
26100           else if (out_n == 8 && in_n == 8)
26101             return ix86_builtins[IX86_BUILTIN_CPYSGNPS256];
26102         }
26103       break;
26104
26105     default:
26106       ;
26107     }
26108
26109   /* Dispatch to a handler for a vectorization library.  */
26110   if (ix86_veclib_handler)
26111     return ix86_veclib_handler ((enum built_in_function) fn, type_out,
26112                                 type_in);
26113
26114   return NULL_TREE;
26115 }
26116
26117 /* Handler for an SVML-style interface to
26118    a library with vectorized intrinsics.  */
26119
26120 static tree
26121 ix86_veclibabi_svml (enum built_in_function fn, tree type_out, tree type_in)
26122 {
26123   char name[20];
26124   tree fntype, new_fndecl, args;
26125   unsigned arity;
26126   const char *bname;
26127   enum machine_mode el_mode, in_mode;
26128   int n, in_n;
26129
26130   /* The SVML is suitable for unsafe math only.  */
26131   if (!flag_unsafe_math_optimizations)
26132     return NULL_TREE;
26133
26134   el_mode = TYPE_MODE (TREE_TYPE (type_out));
26135   n = TYPE_VECTOR_SUBPARTS (type_out);
26136   in_mode = TYPE_MODE (TREE_TYPE (type_in));
26137   in_n = TYPE_VECTOR_SUBPARTS (type_in);
26138   if (el_mode != in_mode
26139       || n != in_n)
26140     return NULL_TREE;
26141
26142   switch (fn)
26143     {
26144     case BUILT_IN_EXP:
26145     case BUILT_IN_LOG:
26146     case BUILT_IN_LOG10:
26147     case BUILT_IN_POW:
26148     case BUILT_IN_TANH:
26149     case BUILT_IN_TAN:
26150     case BUILT_IN_ATAN:
26151     case BUILT_IN_ATAN2:
26152     case BUILT_IN_ATANH:
26153     case BUILT_IN_CBRT:
26154     case BUILT_IN_SINH:
26155     case BUILT_IN_SIN:
26156     case BUILT_IN_ASINH:
26157     case BUILT_IN_ASIN:
26158     case BUILT_IN_COSH:
26159     case BUILT_IN_COS:
26160     case BUILT_IN_ACOSH:
26161     case BUILT_IN_ACOS:
26162       if (el_mode != DFmode || n != 2)
26163         return NULL_TREE;
26164       break;
26165
26166     case BUILT_IN_EXPF:
26167     case BUILT_IN_LOGF:
26168     case BUILT_IN_LOG10F:
26169     case BUILT_IN_POWF:
26170     case BUILT_IN_TANHF:
26171     case BUILT_IN_TANF:
26172     case BUILT_IN_ATANF:
26173     case BUILT_IN_ATAN2F:
26174     case BUILT_IN_ATANHF:
26175     case BUILT_IN_CBRTF:
26176     case BUILT_IN_SINHF:
26177     case BUILT_IN_SINF:
26178     case BUILT_IN_ASINHF:
26179     case BUILT_IN_ASINF:
26180     case BUILT_IN_COSHF:
26181     case BUILT_IN_COSF:
26182     case BUILT_IN_ACOSHF:
26183     case BUILT_IN_ACOSF:
26184       if (el_mode != SFmode || n != 4)
26185         return NULL_TREE;
26186       break;
26187
26188     default:
26189       return NULL_TREE;
26190     }
26191
26192   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
26193
26194   if (fn == BUILT_IN_LOGF)
26195     strcpy (name, "vmlsLn4");
26196   else if (fn == BUILT_IN_LOG)
26197     strcpy (name, "vmldLn2");
26198   else if (n == 4)
26199     {
26200       sprintf (name, "vmls%s", bname+10);
26201       name[strlen (name)-1] = '4';
26202     }
26203   else
26204     sprintf (name, "vmld%s2", bname+10);
26205
26206   /* Convert to uppercase. */
26207   name[4] &= ~0x20;
26208
26209   arity = 0;
26210   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
26211        args = TREE_CHAIN (args))
26212     arity++;
26213
26214   if (arity == 1)
26215     fntype = build_function_type_list (type_out, type_in, NULL);
26216   else
26217     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
26218
26219   /* Build a function declaration for the vectorized function.  */
26220   new_fndecl = build_decl (BUILTINS_LOCATION,
26221                            FUNCTION_DECL, get_identifier (name), fntype);
26222   TREE_PUBLIC (new_fndecl) = 1;
26223   DECL_EXTERNAL (new_fndecl) = 1;
26224   DECL_IS_NOVOPS (new_fndecl) = 1;
26225   TREE_READONLY (new_fndecl) = 1;
26226
26227   return new_fndecl;
26228 }
26229
26230 /* Handler for an ACML-style interface to
26231    a library with vectorized intrinsics.  */
26232
26233 static tree
26234 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
26235 {
26236   char name[20] = "__vr.._";
26237   tree fntype, new_fndecl, args;
26238   unsigned arity;
26239   const char *bname;
26240   enum machine_mode el_mode, in_mode;
26241   int n, in_n;
26242
26243   /* The ACML is 64bits only and suitable for unsafe math only as
26244      it does not correctly support parts of IEEE with the required
26245      precision such as denormals.  */
26246   if (!TARGET_64BIT
26247       || !flag_unsafe_math_optimizations)
26248     return NULL_TREE;
26249
26250   el_mode = TYPE_MODE (TREE_TYPE (type_out));
26251   n = TYPE_VECTOR_SUBPARTS (type_out);
26252   in_mode = TYPE_MODE (TREE_TYPE (type_in));
26253   in_n = TYPE_VECTOR_SUBPARTS (type_in);
26254   if (el_mode != in_mode
26255       || n != in_n)
26256     return NULL_TREE;
26257
26258   switch (fn)
26259     {
26260     case BUILT_IN_SIN:
26261     case BUILT_IN_COS:
26262     case BUILT_IN_EXP:
26263     case BUILT_IN_LOG:
26264     case BUILT_IN_LOG2:
26265     case BUILT_IN_LOG10:
26266       name[4] = 'd';
26267       name[5] = '2';
26268       if (el_mode != DFmode
26269           || n != 2)
26270         return NULL_TREE;
26271       break;
26272
26273     case BUILT_IN_SINF:
26274     case BUILT_IN_COSF:
26275     case BUILT_IN_EXPF:
26276     case BUILT_IN_POWF:
26277     case BUILT_IN_LOGF:
26278     case BUILT_IN_LOG2F:
26279     case BUILT_IN_LOG10F:
26280       name[4] = 's';
26281       name[5] = '4';
26282       if (el_mode != SFmode
26283           || n != 4)
26284         return NULL_TREE;
26285       break;
26286
26287     default:
26288       return NULL_TREE;
26289     }
26290
26291   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
26292   sprintf (name + 7, "%s", bname+10);
26293
26294   arity = 0;
26295   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
26296        args = TREE_CHAIN (args))
26297     arity++;
26298
26299   if (arity == 1)
26300     fntype = build_function_type_list (type_out, type_in, NULL);
26301   else
26302     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
26303
26304   /* Build a function declaration for the vectorized function.  */
26305   new_fndecl = build_decl (BUILTINS_LOCATION,
26306                            FUNCTION_DECL, get_identifier (name), fntype);
26307   TREE_PUBLIC (new_fndecl) = 1;
26308   DECL_EXTERNAL (new_fndecl) = 1;
26309   DECL_IS_NOVOPS (new_fndecl) = 1;
26310   TREE_READONLY (new_fndecl) = 1;
26311
26312   return new_fndecl;
26313 }
26314
26315
26316 /* Returns a decl of a function that implements conversion of an integer vector
26317    into a floating-point vector, or vice-versa.  DEST_TYPE and SRC_TYPE
26318    are the types involved when converting according to CODE.
26319    Return NULL_TREE if it is not available.  */
26320
26321 static tree
26322 ix86_vectorize_builtin_conversion (unsigned int code,
26323                                    tree dest_type, tree src_type)
26324 {
26325   if (! TARGET_SSE2)
26326     return NULL_TREE;
26327
26328   switch (code)
26329     {
26330     case FLOAT_EXPR:
26331       switch (TYPE_MODE (src_type))
26332         {
26333         case V4SImode:
26334           switch (TYPE_MODE (dest_type))
26335             {
26336             case V4SFmode:
26337               return (TYPE_UNSIGNED (src_type)
26338                       ? ix86_builtins[IX86_BUILTIN_CVTUDQ2PS]
26339                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS]);
26340             case V4DFmode:
26341               return (TYPE_UNSIGNED (src_type)
26342                       ? NULL_TREE
26343                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PD256]);
26344             default:
26345               return NULL_TREE;
26346             }
26347           break;
26348         case V8SImode:
26349           switch (TYPE_MODE (dest_type))
26350             {
26351             case V8SFmode:
26352               return (TYPE_UNSIGNED (src_type)
26353                       ? NULL_TREE
26354                       : ix86_builtins[IX86_BUILTIN_CVTDQ2PS256]);
26355             default:
26356               return NULL_TREE;
26357             }
26358           break;
26359         default:
26360           return NULL_TREE;
26361         }
26362
26363     case FIX_TRUNC_EXPR:
26364       switch (TYPE_MODE (dest_type))
26365         {
26366         case V4SImode:
26367           switch (TYPE_MODE (src_type))
26368             {
26369             case V4SFmode:
26370               return (TYPE_UNSIGNED (dest_type)
26371                       ? NULL_TREE
26372                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ]);
26373             case V4DFmode:
26374               return (TYPE_UNSIGNED (dest_type)
26375                       ? NULL_TREE
26376                       : ix86_builtins[IX86_BUILTIN_CVTTPD2DQ256]);
26377             default:
26378               return NULL_TREE;
26379             }
26380           break;
26381
26382         case V8SImode:
26383           switch (TYPE_MODE (src_type))
26384             {
26385             case V8SFmode:
26386               return (TYPE_UNSIGNED (dest_type)
26387                       ? NULL_TREE
26388                       : ix86_builtins[IX86_BUILTIN_CVTTPS2DQ256]);
26389             default:
26390               return NULL_TREE;
26391             }
26392           break;
26393
26394         default:
26395           return NULL_TREE;
26396         }
26397
26398     default:
26399       return NULL_TREE;
26400     }
26401
26402   return NULL_TREE;
26403 }
26404
26405 /* Returns a code for a target-specific builtin that implements
26406    reciprocal of the function, or NULL_TREE if not available.  */
26407
26408 static tree
26409 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
26410                          bool sqrt ATTRIBUTE_UNUSED)
26411 {
26412   if (! (TARGET_SSE_MATH && !optimize_insn_for_size_p ()
26413          && flag_finite_math_only && !flag_trapping_math
26414          && flag_unsafe_math_optimizations))
26415     return NULL_TREE;
26416
26417   if (md_fn)
26418     /* Machine dependent builtins.  */
26419     switch (fn)
26420       {
26421         /* Vectorized version of sqrt to rsqrt conversion.  */
26422       case IX86_BUILTIN_SQRTPS_NR:
26423         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
26424
26425       case IX86_BUILTIN_SQRTPS_NR256:
26426         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR256];
26427
26428       default:
26429         return NULL_TREE;
26430       }
26431   else
26432     /* Normal builtins.  */
26433     switch (fn)
26434       {
26435         /* Sqrt to rsqrt conversion.  */
26436       case BUILT_IN_SQRTF:
26437         return ix86_builtins[IX86_BUILTIN_RSQRTF];
26438
26439       default:
26440         return NULL_TREE;
26441       }
26442 }
26443 \f
26444 /* Helper for avx_vpermilps256_operand et al.  This is also used by
26445    the expansion functions to turn the parallel back into a mask.
26446    The return value is 0 for no match and the imm8+1 for a match.  */
26447
26448 int
26449 avx_vpermilp_parallel (rtx par, enum machine_mode mode)
26450 {
26451   unsigned i, nelt = GET_MODE_NUNITS (mode);
26452   unsigned mask = 0;
26453   unsigned char ipar[8];
26454
26455   if (XVECLEN (par, 0) != (int) nelt)
26456     return 0;
26457
26458   /* Validate that all of the elements are constants, and not totally
26459      out of range.  Copy the data into an integral array to make the
26460      subsequent checks easier.  */
26461   for (i = 0; i < nelt; ++i)
26462     {
26463       rtx er = XVECEXP (par, 0, i);
26464       unsigned HOST_WIDE_INT ei;
26465
26466       if (!CONST_INT_P (er))
26467         return 0;
26468       ei = INTVAL (er);
26469       if (ei >= nelt)
26470         return 0;
26471       ipar[i] = ei;
26472     }
26473
26474   switch (mode)
26475     {
26476     case V4DFmode:
26477       /* In the 256-bit DFmode case, we can only move elements within
26478          a 128-bit lane.  */
26479       for (i = 0; i < 2; ++i)
26480         {
26481           if (ipar[i] >= 2)
26482             return 0;
26483           mask |= ipar[i] << i;
26484         }
26485       for (i = 2; i < 4; ++i)
26486         {
26487           if (ipar[i] < 2)
26488             return 0;
26489           mask |= (ipar[i] - 2) << i;
26490         }
26491       break;
26492
26493     case V8SFmode:
26494       /* In the 256-bit SFmode case, we have full freedom of movement
26495          within the low 128-bit lane, but the high 128-bit lane must
26496          mirror the exact same pattern.  */
26497       for (i = 0; i < 4; ++i)
26498         if (ipar[i] + 4 != ipar[i + 4])
26499           return 0;
26500       nelt = 4;
26501       /* FALLTHRU */
26502
26503     case V2DFmode:
26504     case V4SFmode:
26505       /* In the 128-bit case, we've full freedom in the placement of
26506          the elements from the source operand.  */
26507       for (i = 0; i < nelt; ++i)
26508         mask |= ipar[i] << (i * (nelt / 2));
26509       break;
26510
26511     default:
26512       gcc_unreachable ();
26513     }
26514
26515   /* Make sure success has a non-zero value by adding one.  */
26516   return mask + 1;
26517 }
26518
26519 /* Helper for avx_vperm2f128_v4df_operand et al.  This is also used by
26520    the expansion functions to turn the parallel back into a mask.
26521    The return value is 0 for no match and the imm8+1 for a match.  */
26522
26523 int
26524 avx_vperm2f128_parallel (rtx par, enum machine_mode mode)
26525 {
26526   unsigned i, nelt = GET_MODE_NUNITS (mode), nelt2 = nelt / 2;
26527   unsigned mask = 0;
26528   unsigned char ipar[8];
26529
26530   if (XVECLEN (par, 0) != (int) nelt)
26531     return 0;
26532
26533   /* Validate that all of the elements are constants, and not totally
26534      out of range.  Copy the data into an integral array to make the
26535      subsequent checks easier.  */
26536   for (i = 0; i < nelt; ++i)
26537     {
26538       rtx er = XVECEXP (par, 0, i);
26539       unsigned HOST_WIDE_INT ei;
26540
26541       if (!CONST_INT_P (er))
26542         return 0;
26543       ei = INTVAL (er);
26544       if (ei >= 2 * nelt)
26545         return 0;
26546       ipar[i] = ei;
26547     }
26548
26549   /* Validate that the halves of the permute are halves.  */
26550   for (i = 0; i < nelt2 - 1; ++i)
26551     if (ipar[i] + 1 != ipar[i + 1])
26552       return 0;
26553   for (i = nelt2; i < nelt - 1; ++i)
26554     if (ipar[i] + 1 != ipar[i + 1])
26555       return 0;
26556
26557   /* Reconstruct the mask.  */
26558   for (i = 0; i < 2; ++i)
26559     {
26560       unsigned e = ipar[i * nelt2];
26561       if (e % nelt2)
26562         return 0;
26563       e /= nelt2;
26564       mask |= e << (i * 4);
26565     }
26566
26567   /* Make sure success has a non-zero value by adding one.  */
26568   return mask + 1;
26569 }
26570 \f
26571
26572 /* Store OPERAND to the memory after reload is completed.  This means
26573    that we can't easily use assign_stack_local.  */
26574 rtx
26575 ix86_force_to_memory (enum machine_mode mode, rtx operand)
26576 {
26577   rtx result;
26578
26579   gcc_assert (reload_completed);
26580   if (ix86_using_red_zone ())
26581     {
26582       result = gen_rtx_MEM (mode,
26583                             gen_rtx_PLUS (Pmode,
26584                                           stack_pointer_rtx,
26585                                           GEN_INT (-RED_ZONE_SIZE)));
26586       emit_move_insn (result, operand);
26587     }
26588   else if (TARGET_64BIT)
26589     {
26590       switch (mode)
26591         {
26592         case HImode:
26593         case SImode:
26594           operand = gen_lowpart (DImode, operand);
26595           /* FALLTHRU */
26596         case DImode:
26597           emit_insn (
26598                       gen_rtx_SET (VOIDmode,
26599                                    gen_rtx_MEM (DImode,
26600                                                 gen_rtx_PRE_DEC (DImode,
26601                                                         stack_pointer_rtx)),
26602                                    operand));
26603           break;
26604         default:
26605           gcc_unreachable ();
26606         }
26607       result = gen_rtx_MEM (mode, stack_pointer_rtx);
26608     }
26609   else
26610     {
26611       switch (mode)
26612         {
26613         case DImode:
26614           {
26615             rtx operands[2];
26616             split_double_mode (mode, &operand, 1, operands, operands + 1);
26617             emit_insn (
26618                         gen_rtx_SET (VOIDmode,
26619                                      gen_rtx_MEM (SImode,
26620                                                   gen_rtx_PRE_DEC (Pmode,
26621                                                         stack_pointer_rtx)),
26622                                      operands[1]));
26623             emit_insn (
26624                         gen_rtx_SET (VOIDmode,
26625                                      gen_rtx_MEM (SImode,
26626                                                   gen_rtx_PRE_DEC (Pmode,
26627                                                         stack_pointer_rtx)),
26628                                      operands[0]));
26629           }
26630           break;
26631         case HImode:
26632           /* Store HImodes as SImodes.  */
26633           operand = gen_lowpart (SImode, operand);
26634           /* FALLTHRU */
26635         case SImode:
26636           emit_insn (
26637                       gen_rtx_SET (VOIDmode,
26638                                    gen_rtx_MEM (GET_MODE (operand),
26639                                                 gen_rtx_PRE_DEC (SImode,
26640                                                         stack_pointer_rtx)),
26641                                    operand));
26642           break;
26643         default:
26644           gcc_unreachable ();
26645         }
26646       result = gen_rtx_MEM (mode, stack_pointer_rtx);
26647     }
26648   return result;
26649 }
26650
26651 /* Free operand from the memory.  */
26652 void
26653 ix86_free_from_memory (enum machine_mode mode)
26654 {
26655   if (!ix86_using_red_zone ())
26656     {
26657       int size;
26658
26659       if (mode == DImode || TARGET_64BIT)
26660         size = 8;
26661       else
26662         size = 4;
26663       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
26664          to pop or add instruction if registers are available.  */
26665       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
26666                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
26667                                             GEN_INT (size))));
26668     }
26669 }
26670
26671 /* Implement TARGET_IRA_COVER_CLASSES.  If -mfpmath=sse, we prefer
26672    SSE_REGS to FLOAT_REGS if their costs for a pseudo are the
26673    same.  */
26674 static const reg_class_t *
26675 i386_ira_cover_classes (void)
26676 {
26677   static const reg_class_t sse_fpmath_classes[] = {
26678     GENERAL_REGS, SSE_REGS, MMX_REGS, FLOAT_REGS, LIM_REG_CLASSES
26679   };
26680   static const reg_class_t no_sse_fpmath_classes[] = {
26681     GENERAL_REGS, FLOAT_REGS, MMX_REGS, SSE_REGS, LIM_REG_CLASSES
26682   };
26683
26684  return TARGET_SSE_MATH ? sse_fpmath_classes : no_sse_fpmath_classes;
26685 }
26686
26687 /* Implement TARGET_PREFERRED_RELOAD_CLASS.
26688
26689    Put float CONST_DOUBLE in the constant pool instead of fp regs.
26690    QImode must go into class Q_REGS.
26691    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
26692    movdf to do mem-to-mem moves through integer regs.  */
26693
26694 static reg_class_t
26695 ix86_preferred_reload_class (rtx x, reg_class_t regclass)
26696 {
26697   enum machine_mode mode = GET_MODE (x);
26698
26699   /* We're only allowed to return a subclass of CLASS.  Many of the
26700      following checks fail for NO_REGS, so eliminate that early.  */
26701   if (regclass == NO_REGS)
26702     return NO_REGS;
26703
26704   /* All classes can load zeros.  */
26705   if (x == CONST0_RTX (mode))
26706     return regclass;
26707
26708   /* Force constants into memory if we are loading a (nonzero) constant into
26709      an MMX or SSE register.  This is because there are no MMX/SSE instructions
26710      to load from a constant.  */
26711   if (CONSTANT_P (x)
26712       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
26713     return NO_REGS;
26714
26715   /* Prefer SSE regs only, if we can use them for math.  */
26716   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
26717     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
26718
26719   /* Floating-point constants need more complex checks.  */
26720   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
26721     {
26722       /* General regs can load everything.  */
26723       if (reg_class_subset_p (regclass, GENERAL_REGS))
26724         return regclass;
26725
26726       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
26727          zero above.  We only want to wind up preferring 80387 registers if
26728          we plan on doing computation with them.  */
26729       if (TARGET_80387
26730           && standard_80387_constant_p (x))
26731         {
26732           /* Limit class to non-sse.  */
26733           if (regclass == FLOAT_SSE_REGS)
26734             return FLOAT_REGS;
26735           if (regclass == FP_TOP_SSE_REGS)
26736             return FP_TOP_REG;
26737           if (regclass == FP_SECOND_SSE_REGS)
26738             return FP_SECOND_REG;
26739           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
26740             return regclass;
26741         }
26742
26743       return NO_REGS;
26744     }
26745
26746   /* Generally when we see PLUS here, it's the function invariant
26747      (plus soft-fp const_int).  Which can only be computed into general
26748      regs.  */
26749   if (GET_CODE (x) == PLUS)
26750     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
26751
26752   /* QImode constants are easy to load, but non-constant QImode data
26753      must go into Q_REGS.  */
26754   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
26755     {
26756       if (reg_class_subset_p (regclass, Q_REGS))
26757         return regclass;
26758       if (reg_class_subset_p (Q_REGS, regclass))
26759         return Q_REGS;
26760       return NO_REGS;
26761     }
26762
26763   return regclass;
26764 }
26765
26766 /* Discourage putting floating-point values in SSE registers unless
26767    SSE math is being used, and likewise for the 387 registers.  */
26768 static reg_class_t
26769 ix86_preferred_output_reload_class (rtx x, reg_class_t regclass)
26770 {
26771   enum machine_mode mode = GET_MODE (x);
26772
26773   /* Restrict the output reload class to the register bank that we are doing
26774      math on.  If we would like not to return a subset of CLASS, reject this
26775      alternative: if reload cannot do this, it will still use its choice.  */
26776   mode = GET_MODE (x);
26777   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
26778     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
26779
26780   if (X87_FLOAT_MODE_P (mode))
26781     {
26782       if (regclass == FP_TOP_SSE_REGS)
26783         return FP_TOP_REG;
26784       else if (regclass == FP_SECOND_SSE_REGS)
26785         return FP_SECOND_REG;
26786       else
26787         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
26788     }
26789
26790   return regclass;
26791 }
26792
26793 static reg_class_t
26794 ix86_secondary_reload (bool in_p, rtx x, reg_class_t rclass,
26795                        enum machine_mode mode,
26796                        secondary_reload_info *sri ATTRIBUTE_UNUSED)
26797 {
26798   /* QImode spills from non-QI registers require
26799      intermediate register on 32bit targets.  */
26800   if (!in_p && mode == QImode && !TARGET_64BIT
26801       && (rclass == GENERAL_REGS
26802           || rclass == LEGACY_REGS
26803           || rclass == INDEX_REGS))
26804     {
26805       int regno;
26806
26807       if (REG_P (x))
26808         regno = REGNO (x);
26809       else
26810         regno = -1;
26811
26812       if (regno >= FIRST_PSEUDO_REGISTER || GET_CODE (x) == SUBREG)
26813         regno = true_regnum (x);
26814
26815       /* Return Q_REGS if the operand is in memory.  */
26816       if (regno == -1)
26817         return Q_REGS;
26818     }
26819
26820   return NO_REGS;
26821 }
26822
26823 /* Implement TARGET_CLASS_LIKELY_SPILLED_P.  */
26824
26825 static bool
26826 ix86_class_likely_spilled_p (reg_class_t rclass)
26827 {
26828   switch (rclass)
26829     {
26830       case AREG:
26831       case DREG:
26832       case CREG:
26833       case BREG:
26834       case AD_REGS:
26835       case SIREG:
26836       case DIREG:
26837       case SSE_FIRST_REG:
26838       case FP_TOP_REG:
26839       case FP_SECOND_REG:
26840         return true;
26841
26842       default:
26843         break;
26844     }
26845
26846   return false;
26847 }
26848
26849 /* If we are copying between general and FP registers, we need a memory
26850    location. The same is true for SSE and MMX registers.
26851
26852    To optimize register_move_cost performance, allow inline variant.
26853
26854    The macro can't work reliably when one of the CLASSES is class containing
26855    registers from multiple units (SSE, MMX, integer).  We avoid this by never
26856    combining those units in single alternative in the machine description.
26857    Ensure that this constraint holds to avoid unexpected surprises.
26858
26859    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
26860    enforce these sanity checks.  */
26861
26862 static inline bool
26863 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
26864                                 enum machine_mode mode, int strict)
26865 {
26866   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
26867       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
26868       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
26869       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
26870       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
26871       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
26872     {
26873       gcc_assert (!strict);
26874       return true;
26875     }
26876
26877   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
26878     return true;
26879
26880   /* ??? This is a lie.  We do have moves between mmx/general, and for
26881      mmx/sse2.  But by saying we need secondary memory we discourage the
26882      register allocator from using the mmx registers unless needed.  */
26883   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
26884     return true;
26885
26886   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
26887     {
26888       /* SSE1 doesn't have any direct moves from other classes.  */
26889       if (!TARGET_SSE2)
26890         return true;
26891
26892       /* If the target says that inter-unit moves are more expensive
26893          than moving through memory, then don't generate them.  */
26894       if (!TARGET_INTER_UNIT_MOVES)
26895         return true;
26896
26897       /* Between SSE and general, we have moves no larger than word size.  */
26898       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
26899         return true;
26900     }
26901
26902   return false;
26903 }
26904
26905 bool
26906 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
26907                               enum machine_mode mode, int strict)
26908 {
26909   return inline_secondary_memory_needed (class1, class2, mode, strict);
26910 }
26911
26912 /* Return true if the registers in CLASS cannot represent the change from
26913    modes FROM to TO.  */
26914
26915 bool
26916 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
26917                                enum reg_class regclass)
26918 {
26919   if (from == to)
26920     return false;
26921
26922   /* x87 registers can't do subreg at all, as all values are reformatted
26923      to extended precision.  */
26924   if (MAYBE_FLOAT_CLASS_P (regclass))
26925     return true;
26926
26927   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
26928     {
26929       /* Vector registers do not support QI or HImode loads.  If we don't
26930          disallow a change to these modes, reload will assume it's ok to
26931          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
26932          the vec_dupv4hi pattern.  */
26933       if (GET_MODE_SIZE (from) < 4)
26934         return true;
26935
26936       /* Vector registers do not support subreg with nonzero offsets, which
26937          are otherwise valid for integer registers.  Since we can't see
26938          whether we have a nonzero offset from here, prohibit all
26939          nonparadoxical subregs changing size.  */
26940       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
26941         return true;
26942     }
26943
26944   return false;
26945 }
26946
26947 /* Return the cost of moving data of mode M between a
26948    register and memory.  A value of 2 is the default; this cost is
26949    relative to those in `REGISTER_MOVE_COST'.
26950
26951    This function is used extensively by register_move_cost that is used to
26952    build tables at startup.  Make it inline in this case.
26953    When IN is 2, return maximum of in and out move cost.
26954
26955    If moving between registers and memory is more expensive than
26956    between two registers, you should define this macro to express the
26957    relative cost.
26958
26959    Model also increased moving costs of QImode registers in non
26960    Q_REGS classes.
26961  */
26962 static inline int
26963 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
26964                          int in)
26965 {
26966   int cost;
26967   if (FLOAT_CLASS_P (regclass))
26968     {
26969       int index;
26970       switch (mode)
26971         {
26972           case SFmode:
26973             index = 0;
26974             break;
26975           case DFmode:
26976             index = 1;
26977             break;
26978           case XFmode:
26979             index = 2;
26980             break;
26981           default:
26982             return 100;
26983         }
26984       if (in == 2)
26985         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
26986       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
26987     }
26988   if (SSE_CLASS_P (regclass))
26989     {
26990       int index;
26991       switch (GET_MODE_SIZE (mode))
26992         {
26993           case 4:
26994             index = 0;
26995             break;
26996           case 8:
26997             index = 1;
26998             break;
26999           case 16:
27000             index = 2;
27001             break;
27002           default:
27003             return 100;
27004         }
27005       if (in == 2)
27006         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
27007       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
27008     }
27009   if (MMX_CLASS_P (regclass))
27010     {
27011       int index;
27012       switch (GET_MODE_SIZE (mode))
27013         {
27014           case 4:
27015             index = 0;
27016             break;
27017           case 8:
27018             index = 1;
27019             break;
27020           default:
27021             return 100;
27022         }
27023       if (in)
27024         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
27025       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
27026     }
27027   switch (GET_MODE_SIZE (mode))
27028     {
27029       case 1:
27030         if (Q_CLASS_P (regclass) || TARGET_64BIT)
27031           {
27032             if (!in)
27033               return ix86_cost->int_store[0];
27034             if (TARGET_PARTIAL_REG_DEPENDENCY
27035                 && optimize_function_for_speed_p (cfun))
27036               cost = ix86_cost->movzbl_load;
27037             else
27038               cost = ix86_cost->int_load[0];
27039             if (in == 2)
27040               return MAX (cost, ix86_cost->int_store[0]);
27041             return cost;
27042           }
27043         else
27044           {
27045            if (in == 2)
27046              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
27047            if (in)
27048              return ix86_cost->movzbl_load;
27049            else
27050              return ix86_cost->int_store[0] + 4;
27051           }
27052         break;
27053       case 2:
27054         if (in == 2)
27055           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
27056         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
27057       default:
27058         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
27059         if (mode == TFmode)
27060           mode = XFmode;
27061         if (in == 2)
27062           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
27063         else if (in)
27064           cost = ix86_cost->int_load[2];
27065         else
27066           cost = ix86_cost->int_store[2];
27067         return (cost * (((int) GET_MODE_SIZE (mode)
27068                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
27069     }
27070 }
27071
27072 static int
27073 ix86_memory_move_cost (enum machine_mode mode, reg_class_t regclass,
27074                        bool in)
27075 {
27076   return inline_memory_move_cost (mode, (enum reg_class) regclass, in ? 1 : 0);
27077 }
27078
27079
27080 /* Return the cost of moving data from a register in class CLASS1 to
27081    one in class CLASS2.
27082
27083    It is not required that the cost always equal 2 when FROM is the same as TO;
27084    on some machines it is expensive to move between registers if they are not
27085    general registers.  */
27086
27087 static int
27088 ix86_register_move_cost (enum machine_mode mode, reg_class_t class1_i,
27089                          reg_class_t class2_i)
27090 {
27091   enum reg_class class1 = (enum reg_class) class1_i;
27092   enum reg_class class2 = (enum reg_class) class2_i;
27093
27094   /* In case we require secondary memory, compute cost of the store followed
27095      by load.  In order to avoid bad register allocation choices, we need
27096      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
27097
27098   if (inline_secondary_memory_needed (class1, class2, mode, 0))
27099     {
27100       int cost = 1;
27101
27102       cost += inline_memory_move_cost (mode, class1, 2);
27103       cost += inline_memory_move_cost (mode, class2, 2);
27104
27105       /* In case of copying from general_purpose_register we may emit multiple
27106          stores followed by single load causing memory size mismatch stall.
27107          Count this as arbitrarily high cost of 20.  */
27108       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
27109         cost += 20;
27110
27111       /* In the case of FP/MMX moves, the registers actually overlap, and we
27112          have to switch modes in order to treat them differently.  */
27113       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
27114           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
27115         cost += 20;
27116
27117       return cost;
27118     }
27119
27120   /* Moves between SSE/MMX and integer unit are expensive.  */
27121   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
27122       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
27123
27124     /* ??? By keeping returned value relatively high, we limit the number
27125        of moves between integer and MMX/SSE registers for all targets.
27126        Additionally, high value prevents problem with x86_modes_tieable_p(),
27127        where integer modes in MMX/SSE registers are not tieable
27128        because of missing QImode and HImode moves to, from or between
27129        MMX/SSE registers.  */
27130     return MAX (8, ix86_cost->mmxsse_to_integer);
27131
27132   if (MAYBE_FLOAT_CLASS_P (class1))
27133     return ix86_cost->fp_move;
27134   if (MAYBE_SSE_CLASS_P (class1))
27135     return ix86_cost->sse_move;
27136   if (MAYBE_MMX_CLASS_P (class1))
27137     return ix86_cost->mmx_move;
27138   return 2;
27139 }
27140
27141 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
27142
27143 bool
27144 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
27145 {
27146   /* Flags and only flags can only hold CCmode values.  */
27147   if (CC_REGNO_P (regno))
27148     return GET_MODE_CLASS (mode) == MODE_CC;
27149   if (GET_MODE_CLASS (mode) == MODE_CC
27150       || GET_MODE_CLASS (mode) == MODE_RANDOM
27151       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
27152     return 0;
27153   if (FP_REGNO_P (regno))
27154     return VALID_FP_MODE_P (mode);
27155   if (SSE_REGNO_P (regno))
27156     {
27157       /* We implement the move patterns for all vector modes into and
27158          out of SSE registers, even when no operation instructions
27159          are available.  OImode move is available only when AVX is
27160          enabled.  */
27161       return ((TARGET_AVX && mode == OImode)
27162               || VALID_AVX256_REG_MODE (mode)
27163               || VALID_SSE_REG_MODE (mode)
27164               || VALID_SSE2_REG_MODE (mode)
27165               || VALID_MMX_REG_MODE (mode)
27166               || VALID_MMX_REG_MODE_3DNOW (mode));
27167     }
27168   if (MMX_REGNO_P (regno))
27169     {
27170       /* We implement the move patterns for 3DNOW modes even in MMX mode,
27171          so if the register is available at all, then we can move data of
27172          the given mode into or out of it.  */
27173       return (VALID_MMX_REG_MODE (mode)
27174               || VALID_MMX_REG_MODE_3DNOW (mode));
27175     }
27176
27177   if (mode == QImode)
27178     {
27179       /* Take care for QImode values - they can be in non-QI regs,
27180          but then they do cause partial register stalls.  */
27181       if (regno <= BX_REG || TARGET_64BIT)
27182         return 1;
27183       if (!TARGET_PARTIAL_REG_STALL)
27184         return 1;
27185       return reload_in_progress || reload_completed;
27186     }
27187   /* We handle both integer and floats in the general purpose registers.  */
27188   else if (VALID_INT_MODE_P (mode))
27189     return 1;
27190   else if (VALID_FP_MODE_P (mode))
27191     return 1;
27192   else if (VALID_DFP_MODE_P (mode))
27193     return 1;
27194   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
27195      on to use that value in smaller contexts, this can easily force a
27196      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
27197      supporting DImode, allow it.  */
27198   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
27199     return 1;
27200
27201   return 0;
27202 }
27203
27204 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
27205    tieable integer mode.  */
27206
27207 static bool
27208 ix86_tieable_integer_mode_p (enum machine_mode mode)
27209 {
27210   switch (mode)
27211     {
27212     case HImode:
27213     case SImode:
27214       return true;
27215
27216     case QImode:
27217       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
27218
27219     case DImode:
27220       return TARGET_64BIT;
27221
27222     default:
27223       return false;
27224     }
27225 }
27226
27227 /* Return true if MODE1 is accessible in a register that can hold MODE2
27228    without copying.  That is, all register classes that can hold MODE2
27229    can also hold MODE1.  */
27230
27231 bool
27232 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
27233 {
27234   if (mode1 == mode2)
27235     return true;
27236
27237   if (ix86_tieable_integer_mode_p (mode1)
27238       && ix86_tieable_integer_mode_p (mode2))
27239     return true;
27240
27241   /* MODE2 being XFmode implies fp stack or general regs, which means we
27242      can tie any smaller floating point modes to it.  Note that we do not
27243      tie this with TFmode.  */
27244   if (mode2 == XFmode)
27245     return mode1 == SFmode || mode1 == DFmode;
27246
27247   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
27248      that we can tie it with SFmode.  */
27249   if (mode2 == DFmode)
27250     return mode1 == SFmode;
27251
27252   /* If MODE2 is only appropriate for an SSE register, then tie with
27253      any other mode acceptable to SSE registers.  */
27254   if (GET_MODE_SIZE (mode2) == 16
27255       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
27256     return (GET_MODE_SIZE (mode1) == 16
27257             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
27258
27259   /* If MODE2 is appropriate for an MMX register, then tie
27260      with any other mode acceptable to MMX registers.  */
27261   if (GET_MODE_SIZE (mode2) == 8
27262       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
27263     return (GET_MODE_SIZE (mode1) == 8
27264             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
27265
27266   return false;
27267 }
27268
27269 /* Compute a (partial) cost for rtx X.  Return true if the complete
27270    cost has been computed, and false if subexpressions should be
27271    scanned.  In either case, *TOTAL contains the cost result.  */
27272
27273 static bool
27274 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total, bool speed)
27275 {
27276   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
27277   enum machine_mode mode = GET_MODE (x);
27278   const struct processor_costs *cost = speed ? ix86_cost : &ix86_size_cost;
27279
27280   switch (code)
27281     {
27282     case CONST_INT:
27283     case CONST:
27284     case LABEL_REF:
27285     case SYMBOL_REF:
27286       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
27287         *total = 3;
27288       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
27289         *total = 2;
27290       else if (flag_pic && SYMBOLIC_CONST (x)
27291                && (!TARGET_64BIT
27292                    || (!GET_CODE (x) != LABEL_REF
27293                        && (GET_CODE (x) != SYMBOL_REF
27294                            || !SYMBOL_REF_LOCAL_P (x)))))
27295         *total = 1;
27296       else
27297         *total = 0;
27298       return true;
27299
27300     case CONST_DOUBLE:
27301       if (mode == VOIDmode)
27302         *total = 0;
27303       else
27304         switch (standard_80387_constant_p (x))
27305           {
27306           case 1: /* 0.0 */
27307             *total = 1;
27308             break;
27309           default: /* Other constants */
27310             *total = 2;
27311             break;
27312           case 0:
27313           case -1:
27314             /* Start with (MEM (SYMBOL_REF)), since that's where
27315                it'll probably end up.  Add a penalty for size.  */
27316             *total = (COSTS_N_INSNS (1)
27317                       + (flag_pic != 0 && !TARGET_64BIT)
27318                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
27319             break;
27320           }
27321       return true;
27322
27323     case ZERO_EXTEND:
27324       /* The zero extensions is often completely free on x86_64, so make
27325          it as cheap as possible.  */
27326       if (TARGET_64BIT && mode == DImode
27327           && GET_MODE (XEXP (x, 0)) == SImode)
27328         *total = 1;
27329       else if (TARGET_ZERO_EXTEND_WITH_AND)
27330         *total = cost->add;
27331       else
27332         *total = cost->movzx;
27333       return false;
27334
27335     case SIGN_EXTEND:
27336       *total = cost->movsx;
27337       return false;
27338
27339     case ASHIFT:
27340       if (CONST_INT_P (XEXP (x, 1))
27341           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
27342         {
27343           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
27344           if (value == 1)
27345             {
27346               *total = cost->add;
27347               return false;
27348             }
27349           if ((value == 2 || value == 3)
27350               && cost->lea <= cost->shift_const)
27351             {
27352               *total = cost->lea;
27353               return false;
27354             }
27355         }
27356       /* FALLTHRU */
27357
27358     case ROTATE:
27359     case ASHIFTRT:
27360     case LSHIFTRT:
27361     case ROTATERT:
27362       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
27363         {
27364           if (CONST_INT_P (XEXP (x, 1)))
27365             {
27366               if (INTVAL (XEXP (x, 1)) > 32)
27367                 *total = cost->shift_const + COSTS_N_INSNS (2);
27368               else
27369                 *total = cost->shift_const * 2;
27370             }
27371           else
27372             {
27373               if (GET_CODE (XEXP (x, 1)) == AND)
27374                 *total = cost->shift_var * 2;
27375               else
27376                 *total = cost->shift_var * 6 + COSTS_N_INSNS (2);
27377             }
27378         }
27379       else
27380         {
27381           if (CONST_INT_P (XEXP (x, 1)))
27382             *total = cost->shift_const;
27383           else
27384             *total = cost->shift_var;
27385         }
27386       return false;
27387
27388     case MULT:
27389       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27390         {
27391           /* ??? SSE scalar cost should be used here.  */
27392           *total = cost->fmul;
27393           return false;
27394         }
27395       else if (X87_FLOAT_MODE_P (mode))
27396         {
27397           *total = cost->fmul;
27398           return false;
27399         }
27400       else if (FLOAT_MODE_P (mode))
27401         {
27402           /* ??? SSE vector cost should be used here.  */
27403           *total = cost->fmul;
27404           return false;
27405         }
27406       else
27407         {
27408           rtx op0 = XEXP (x, 0);
27409           rtx op1 = XEXP (x, 1);
27410           int nbits;
27411           if (CONST_INT_P (XEXP (x, 1)))
27412             {
27413               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
27414               for (nbits = 0; value != 0; value &= value - 1)
27415                 nbits++;
27416             }
27417           else
27418             /* This is arbitrary.  */
27419             nbits = 7;
27420
27421           /* Compute costs correctly for widening multiplication.  */
27422           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
27423               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
27424                  == GET_MODE_SIZE (mode))
27425             {
27426               int is_mulwiden = 0;
27427               enum machine_mode inner_mode = GET_MODE (op0);
27428
27429               if (GET_CODE (op0) == GET_CODE (op1))
27430                 is_mulwiden = 1, op1 = XEXP (op1, 0);
27431               else if (CONST_INT_P (op1))
27432                 {
27433                   if (GET_CODE (op0) == SIGN_EXTEND)
27434                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
27435                                   == INTVAL (op1);
27436                   else
27437                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
27438                 }
27439
27440               if (is_mulwiden)
27441                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
27442             }
27443
27444           *total = (cost->mult_init[MODE_INDEX (mode)]
27445                     + nbits * cost->mult_bit
27446                     + rtx_cost (op0, outer_code, speed) + rtx_cost (op1, outer_code, speed));
27447
27448           return true;
27449         }
27450
27451     case DIV:
27452     case UDIV:
27453     case MOD:
27454     case UMOD:
27455       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27456         /* ??? SSE cost should be used here.  */
27457         *total = cost->fdiv;
27458       else if (X87_FLOAT_MODE_P (mode))
27459         *total = cost->fdiv;
27460       else if (FLOAT_MODE_P (mode))
27461         /* ??? SSE vector cost should be used here.  */
27462         *total = cost->fdiv;
27463       else
27464         *total = cost->divide[MODE_INDEX (mode)];
27465       return false;
27466
27467     case PLUS:
27468       if (GET_MODE_CLASS (mode) == MODE_INT
27469                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
27470         {
27471           if (GET_CODE (XEXP (x, 0)) == PLUS
27472               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
27473               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
27474               && CONSTANT_P (XEXP (x, 1)))
27475             {
27476               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
27477               if (val == 2 || val == 4 || val == 8)
27478                 {
27479                   *total = cost->lea;
27480                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
27481                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
27482                                       outer_code, speed);
27483                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
27484                   return true;
27485                 }
27486             }
27487           else if (GET_CODE (XEXP (x, 0)) == MULT
27488                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
27489             {
27490               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
27491               if (val == 2 || val == 4 || val == 8)
27492                 {
27493                   *total = cost->lea;
27494                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
27495                   *total += rtx_cost (XEXP (x, 1), outer_code, speed);
27496                   return true;
27497                 }
27498             }
27499           else if (GET_CODE (XEXP (x, 0)) == PLUS)
27500             {
27501               *total = cost->lea;
27502               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed);
27503               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code, speed);
27504               *total += rtx_cost (XEXP (x, 1), outer_code, speed);
27505               return true;
27506             }
27507         }
27508       /* FALLTHRU */
27509
27510     case MINUS:
27511       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27512         {
27513           /* ??? SSE cost should be used here.  */
27514           *total = cost->fadd;
27515           return false;
27516         }
27517       else if (X87_FLOAT_MODE_P (mode))
27518         {
27519           *total = cost->fadd;
27520           return false;
27521         }
27522       else if (FLOAT_MODE_P (mode))
27523         {
27524           /* ??? SSE vector cost should be used here.  */
27525           *total = cost->fadd;
27526           return false;
27527         }
27528       /* FALLTHRU */
27529
27530     case AND:
27531     case IOR:
27532     case XOR:
27533       if (!TARGET_64BIT && mode == DImode)
27534         {
27535           *total = (cost->add * 2
27536                     + (rtx_cost (XEXP (x, 0), outer_code, speed)
27537                        << (GET_MODE (XEXP (x, 0)) != DImode))
27538                     + (rtx_cost (XEXP (x, 1), outer_code, speed)
27539                        << (GET_MODE (XEXP (x, 1)) != DImode)));
27540           return true;
27541         }
27542       /* FALLTHRU */
27543
27544     case NEG:
27545       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27546         {
27547           /* ??? SSE cost should be used here.  */
27548           *total = cost->fchs;
27549           return false;
27550         }
27551       else if (X87_FLOAT_MODE_P (mode))
27552         {
27553           *total = cost->fchs;
27554           return false;
27555         }
27556       else if (FLOAT_MODE_P (mode))
27557         {
27558           /* ??? SSE vector cost should be used here.  */
27559           *total = cost->fchs;
27560           return false;
27561         }
27562       /* FALLTHRU */
27563
27564     case NOT:
27565       if (!TARGET_64BIT && mode == DImode)
27566         *total = cost->add * 2;
27567       else
27568         *total = cost->add;
27569       return false;
27570
27571     case COMPARE:
27572       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
27573           && XEXP (XEXP (x, 0), 1) == const1_rtx
27574           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
27575           && XEXP (x, 1) == const0_rtx)
27576         {
27577           /* This kind of construct is implemented using test[bwl].
27578              Treat it as if we had an AND.  */
27579           *total = (cost->add
27580                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code, speed)
27581                     + rtx_cost (const1_rtx, outer_code, speed));
27582           return true;
27583         }
27584       return false;
27585
27586     case FLOAT_EXTEND:
27587       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
27588         *total = 0;
27589       return false;
27590
27591     case ABS:
27592       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27593         /* ??? SSE cost should be used here.  */
27594         *total = cost->fabs;
27595       else if (X87_FLOAT_MODE_P (mode))
27596         *total = cost->fabs;
27597       else if (FLOAT_MODE_P (mode))
27598         /* ??? SSE vector cost should be used here.  */
27599         *total = cost->fabs;
27600       return false;
27601
27602     case SQRT:
27603       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
27604         /* ??? SSE cost should be used here.  */
27605         *total = cost->fsqrt;
27606       else if (X87_FLOAT_MODE_P (mode))
27607         *total = cost->fsqrt;
27608       else if (FLOAT_MODE_P (mode))
27609         /* ??? SSE vector cost should be used here.  */
27610         *total = cost->fsqrt;
27611       return false;
27612
27613     case UNSPEC:
27614       if (XINT (x, 1) == UNSPEC_TP)
27615         *total = 0;
27616       return false;
27617
27618     case VEC_SELECT:
27619     case VEC_CONCAT:
27620     case VEC_MERGE:
27621     case VEC_DUPLICATE:
27622       /* ??? Assume all of these vector manipulation patterns are
27623          recognizable.  In which case they all pretty much have the
27624          same cost.  */
27625      *total = COSTS_N_INSNS (1);
27626      return true;
27627
27628     default:
27629       return false;
27630     }
27631 }
27632
27633 #if TARGET_MACHO
27634
27635 static int current_machopic_label_num;
27636
27637 /* Given a symbol name and its associated stub, write out the
27638    definition of the stub.  */
27639
27640 void
27641 machopic_output_stub (FILE *file, const char *symb, const char *stub)
27642 {
27643   unsigned int length;
27644   char *binder_name, *symbol_name, lazy_ptr_name[32];
27645   int label = ++current_machopic_label_num;
27646
27647   /* For 64-bit we shouldn't get here.  */
27648   gcc_assert (!TARGET_64BIT);
27649
27650   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
27651   symb = targetm.strip_name_encoding (symb);
27652
27653   length = strlen (stub);
27654   binder_name = XALLOCAVEC (char, length + 32);
27655   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
27656
27657   length = strlen (symb);
27658   symbol_name = XALLOCAVEC (char, length + 32);
27659   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
27660
27661   sprintf (lazy_ptr_name, "L%d$lz", label);
27662
27663   if (MACHOPIC_PURE)
27664     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
27665   else
27666     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
27667
27668   fprintf (file, "%s:\n", stub);
27669   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
27670
27671   if (MACHOPIC_PURE)
27672     {
27673       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
27674       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
27675       fprintf (file, "\tjmp\t*%%edx\n");
27676     }
27677   else
27678     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
27679
27680   fprintf (file, "%s:\n", binder_name);
27681
27682   if (MACHOPIC_PURE)
27683     {
27684       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
27685       fputs ("\tpushl\t%eax\n", file);
27686     }
27687   else
27688     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
27689
27690   fputs ("\tjmp\tdyld_stub_binding_helper\n", file);
27691
27692   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
27693   fprintf (file, "%s:\n", lazy_ptr_name);
27694   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
27695   fprintf (file, ASM_LONG "%s\n", binder_name);
27696 }
27697 #endif /* TARGET_MACHO */
27698
27699 /* Order the registers for register allocator.  */
27700
27701 void
27702 x86_order_regs_for_local_alloc (void)
27703 {
27704    int pos = 0;
27705    int i;
27706
27707    /* First allocate the local general purpose registers.  */
27708    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
27709      if (GENERAL_REGNO_P (i) && call_used_regs[i])
27710         reg_alloc_order [pos++] = i;
27711
27712    /* Global general purpose registers.  */
27713    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
27714      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
27715         reg_alloc_order [pos++] = i;
27716
27717    /* x87 registers come first in case we are doing FP math
27718       using them.  */
27719    if (!TARGET_SSE_MATH)
27720      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
27721        reg_alloc_order [pos++] = i;
27722
27723    /* SSE registers.  */
27724    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
27725      reg_alloc_order [pos++] = i;
27726    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
27727      reg_alloc_order [pos++] = i;
27728
27729    /* x87 registers.  */
27730    if (TARGET_SSE_MATH)
27731      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
27732        reg_alloc_order [pos++] = i;
27733
27734    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
27735      reg_alloc_order [pos++] = i;
27736
27737    /* Initialize the rest of array as we do not allocate some registers
27738       at all.  */
27739    while (pos < FIRST_PSEUDO_REGISTER)
27740      reg_alloc_order [pos++] = 0;
27741 }
27742
27743 /* Handle a "ms_abi" or "sysv" attribute; arguments as in
27744    struct attribute_spec.handler.  */
27745 static tree
27746 ix86_handle_abi_attribute (tree *node, tree name,
27747                               tree args ATTRIBUTE_UNUSED,
27748                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
27749 {
27750   if (TREE_CODE (*node) != FUNCTION_TYPE
27751       && TREE_CODE (*node) != METHOD_TYPE
27752       && TREE_CODE (*node) != FIELD_DECL
27753       && TREE_CODE (*node) != TYPE_DECL)
27754     {
27755       warning (OPT_Wattributes, "%qE attribute only applies to functions",
27756                name);
27757       *no_add_attrs = true;
27758       return NULL_TREE;
27759     }
27760   if (!TARGET_64BIT)
27761     {
27762       warning (OPT_Wattributes, "%qE attribute only available for 64-bit",
27763                name);
27764       *no_add_attrs = true;
27765       return NULL_TREE;
27766     }
27767
27768   /* Can combine regparm with all attributes but fastcall.  */
27769   if (is_attribute_p ("ms_abi", name))
27770     {
27771       if (lookup_attribute ("sysv_abi", TYPE_ATTRIBUTES (*node)))
27772         {
27773           error ("ms_abi and sysv_abi attributes are not compatible");
27774         }
27775
27776       return NULL_TREE;
27777     }
27778   else if (is_attribute_p ("sysv_abi", name))
27779     {
27780       if (lookup_attribute ("ms_abi", TYPE_ATTRIBUTES (*node)))
27781         {
27782           error ("ms_abi and sysv_abi attributes are not compatible");
27783         }
27784
27785       return NULL_TREE;
27786     }
27787
27788   return NULL_TREE;
27789 }
27790
27791 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
27792    struct attribute_spec.handler.  */
27793 static tree
27794 ix86_handle_struct_attribute (tree *node, tree name,
27795                               tree args ATTRIBUTE_UNUSED,
27796                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
27797 {
27798   tree *type = NULL;
27799   if (DECL_P (*node))
27800     {
27801       if (TREE_CODE (*node) == TYPE_DECL)
27802         type = &TREE_TYPE (*node);
27803     }
27804   else
27805     type = node;
27806
27807   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
27808                  || TREE_CODE (*type) == UNION_TYPE)))
27809     {
27810       warning (OPT_Wattributes, "%qE attribute ignored",
27811                name);
27812       *no_add_attrs = true;
27813     }
27814
27815   else if ((is_attribute_p ("ms_struct", name)
27816             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
27817            || ((is_attribute_p ("gcc_struct", name)
27818                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
27819     {
27820       warning (OPT_Wattributes, "%qE incompatible attribute ignored",
27821                name);
27822       *no_add_attrs = true;
27823     }
27824
27825   return NULL_TREE;
27826 }
27827
27828 static tree
27829 ix86_handle_fndecl_attribute (tree *node, tree name,
27830                               tree args ATTRIBUTE_UNUSED,
27831                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
27832 {
27833   if (TREE_CODE (*node) != FUNCTION_DECL)
27834     {
27835       warning (OPT_Wattributes, "%qE attribute only applies to functions",
27836                name);
27837       *no_add_attrs = true;
27838     }
27839   return NULL_TREE;
27840 }
27841
27842 static bool
27843 ix86_ms_bitfield_layout_p (const_tree record_type)
27844 {
27845   return ((TARGET_MS_BITFIELD_LAYOUT
27846            && !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
27847           || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type)));
27848 }
27849
27850 /* Returns an expression indicating where the this parameter is
27851    located on entry to the FUNCTION.  */
27852
27853 static rtx
27854 x86_this_parameter (tree function)
27855 {
27856   tree type = TREE_TYPE (function);
27857   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
27858   int nregs;
27859
27860   if (TARGET_64BIT)
27861     {
27862       const int *parm_regs;
27863
27864       if (ix86_function_type_abi (type) == MS_ABI)
27865         parm_regs = x86_64_ms_abi_int_parameter_registers;
27866       else
27867         parm_regs = x86_64_int_parameter_registers;
27868       return gen_rtx_REG (DImode, parm_regs[aggr]);
27869     }
27870
27871   nregs = ix86_function_regparm (type, function);
27872
27873   if (nregs > 0 && !stdarg_p (type))
27874     {
27875       int regno;
27876
27877       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
27878         regno = aggr ? DX_REG : CX_REG;
27879       else if (lookup_attribute ("thiscall", TYPE_ATTRIBUTES (type)))
27880         {
27881           regno = CX_REG;
27882           if (aggr)
27883             return gen_rtx_MEM (SImode,
27884                                 plus_constant (stack_pointer_rtx, 4));
27885         }
27886       else
27887         {
27888           regno = AX_REG;
27889           if (aggr)
27890             {
27891               regno = DX_REG;
27892               if (nregs == 1)
27893                 return gen_rtx_MEM (SImode,
27894                                     plus_constant (stack_pointer_rtx, 4));
27895             }
27896         }
27897       return gen_rtx_REG (SImode, regno);
27898     }
27899
27900   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
27901 }
27902
27903 /* Determine whether x86_output_mi_thunk can succeed.  */
27904
27905 static bool
27906 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
27907                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
27908                          HOST_WIDE_INT vcall_offset, const_tree function)
27909 {
27910   /* 64-bit can handle anything.  */
27911   if (TARGET_64BIT)
27912     return true;
27913
27914   /* For 32-bit, everything's fine if we have one free register.  */
27915   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
27916     return true;
27917
27918   /* Need a free register for vcall_offset.  */
27919   if (vcall_offset)
27920     return false;
27921
27922   /* Need a free register for GOT references.  */
27923   if (flag_pic && !targetm.binds_local_p (function))
27924     return false;
27925
27926   /* Otherwise ok.  */
27927   return true;
27928 }
27929
27930 /* Output the assembler code for a thunk function.  THUNK_DECL is the
27931    declaration for the thunk function itself, FUNCTION is the decl for
27932    the target function.  DELTA is an immediate constant offset to be
27933    added to THIS.  If VCALL_OFFSET is nonzero, the word at
27934    *(*this + vcall_offset) should be added to THIS.  */
27935
27936 static void
27937 x86_output_mi_thunk (FILE *file,
27938                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
27939                      HOST_WIDE_INT vcall_offset, tree function)
27940 {
27941   rtx xops[3];
27942   rtx this_param = x86_this_parameter (function);
27943   rtx this_reg, tmp;
27944
27945   /* Make sure unwind info is emitted for the thunk if needed.  */
27946   final_start_function (emit_barrier (), file, 1);
27947
27948   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
27949      pull it in now and let DELTA benefit.  */
27950   if (REG_P (this_param))
27951     this_reg = this_param;
27952   else if (vcall_offset)
27953     {
27954       /* Put the this parameter into %eax.  */
27955       xops[0] = this_param;
27956       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
27957       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
27958     }
27959   else
27960     this_reg = NULL_RTX;
27961
27962   /* Adjust the this parameter by a fixed constant.  */
27963   if (delta)
27964     {
27965       xops[0] = GEN_INT (delta);
27966       xops[1] = this_reg ? this_reg : this_param;
27967       if (TARGET_64BIT)
27968         {
27969           if (!x86_64_general_operand (xops[0], DImode))
27970             {
27971               tmp = gen_rtx_REG (DImode, R10_REG);
27972               xops[1] = tmp;
27973               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
27974               xops[0] = tmp;
27975               xops[1] = this_param;
27976             }
27977           if (x86_maybe_negate_const_int (&xops[0], DImode))
27978             output_asm_insn ("sub{q}\t{%0, %1|%1, %0}", xops);
27979           else
27980             output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
27981         }
27982       else if (x86_maybe_negate_const_int (&xops[0], SImode))
27983         output_asm_insn ("sub{l}\t{%0, %1|%1, %0}", xops);
27984       else
27985         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
27986     }
27987
27988   /* Adjust the this parameter by a value stored in the vtable.  */
27989   if (vcall_offset)
27990     {
27991       if (TARGET_64BIT)
27992         tmp = gen_rtx_REG (DImode, R10_REG);
27993       else
27994         {
27995           int tmp_regno = CX_REG;
27996           if (lookup_attribute ("fastcall",
27997                                 TYPE_ATTRIBUTES (TREE_TYPE (function)))
27998               || lookup_attribute ("thiscall",
27999                                    TYPE_ATTRIBUTES (TREE_TYPE (function))))
28000             tmp_regno = AX_REG;
28001           tmp = gen_rtx_REG (SImode, tmp_regno);
28002         }
28003
28004       xops[0] = gen_rtx_MEM (Pmode, this_reg);
28005       xops[1] = tmp;
28006       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
28007
28008       /* Adjust the this parameter.  */
28009       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
28010       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
28011         {
28012           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
28013           xops[0] = GEN_INT (vcall_offset);
28014           xops[1] = tmp2;
28015           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
28016           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
28017         }
28018       xops[1] = this_reg;
28019       output_asm_insn ("add%z1\t{%0, %1|%1, %0}", xops);
28020     }
28021
28022   /* If necessary, drop THIS back to its stack slot.  */
28023   if (this_reg && this_reg != this_param)
28024     {
28025       xops[0] = this_reg;
28026       xops[1] = this_param;
28027       output_asm_insn ("mov%z1\t{%0, %1|%1, %0}", xops);
28028     }
28029
28030   xops[0] = XEXP (DECL_RTL (function), 0);
28031   if (TARGET_64BIT)
28032     {
28033       if (!flag_pic || targetm.binds_local_p (function))
28034         output_asm_insn ("jmp\t%P0", xops);
28035       /* All thunks should be in the same object as their target,
28036          and thus binds_local_p should be true.  */
28037       else if (TARGET_64BIT && cfun->machine->call_abi == MS_ABI)
28038         gcc_unreachable ();
28039       else
28040         {
28041           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
28042           tmp = gen_rtx_CONST (Pmode, tmp);
28043           tmp = gen_rtx_MEM (QImode, tmp);
28044           xops[0] = tmp;
28045           output_asm_insn ("jmp\t%A0", xops);
28046         }
28047     }
28048   else
28049     {
28050       if (!flag_pic || targetm.binds_local_p (function))
28051         output_asm_insn ("jmp\t%P0", xops);
28052       else
28053 #if TARGET_MACHO
28054         if (TARGET_MACHO)
28055           {
28056             rtx sym_ref = XEXP (DECL_RTL (function), 0);
28057             if (TARGET_MACHO_BRANCH_ISLANDS)
28058               sym_ref = (gen_rtx_SYMBOL_REF
28059                    (Pmode,
28060                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
28061             tmp = gen_rtx_MEM (QImode, sym_ref);
28062             xops[0] = tmp;
28063             output_asm_insn ("jmp\t%0", xops);
28064           }
28065         else
28066 #endif /* TARGET_MACHO */
28067         {
28068           tmp = gen_rtx_REG (SImode, CX_REG);
28069           output_set_got (tmp, NULL_RTX);
28070
28071           xops[1] = tmp;
28072           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
28073           output_asm_insn ("jmp\t{*}%1", xops);
28074         }
28075     }
28076   final_end_function ();
28077 }
28078
28079 static void
28080 x86_file_start (void)
28081 {
28082   default_file_start ();
28083 #if TARGET_MACHO
28084   darwin_file_start ();
28085 #endif
28086   if (X86_FILE_START_VERSION_DIRECTIVE)
28087     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
28088   if (X86_FILE_START_FLTUSED)
28089     fputs ("\t.global\t__fltused\n", asm_out_file);
28090   if (ix86_asm_dialect == ASM_INTEL)
28091     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
28092 }
28093
28094 int
28095 x86_field_alignment (tree field, int computed)
28096 {
28097   enum machine_mode mode;
28098   tree type = TREE_TYPE (field);
28099
28100   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
28101     return computed;
28102   mode = TYPE_MODE (strip_array_types (type));
28103   if (mode == DFmode || mode == DCmode
28104       || GET_MODE_CLASS (mode) == MODE_INT
28105       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
28106     return MIN (32, computed);
28107   return computed;
28108 }
28109
28110 /* Output assembler code to FILE to increment profiler label # LABELNO
28111    for profiling a function entry.  */
28112 void
28113 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
28114 {
28115   const char *mcount_name = (flag_fentry ? MCOUNT_NAME_BEFORE_PROLOGUE
28116                                          : MCOUNT_NAME);
28117
28118   if (TARGET_64BIT)
28119     {
28120 #ifndef NO_PROFILE_COUNTERS
28121       fprintf (file, "\tleaq\t%sP%d(%%rip),%%r11\n", LPREFIX, labelno);
28122 #endif
28123
28124       if (DEFAULT_ABI == SYSV_ABI && flag_pic)
28125         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", mcount_name);
28126       else
28127         fprintf (file, "\tcall\t%s\n", mcount_name);
28128     }
28129   else if (flag_pic)
28130     {
28131 #ifndef NO_PROFILE_COUNTERS
28132       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%" PROFILE_COUNT_REGISTER "\n",
28133                LPREFIX, labelno);
28134 #endif
28135       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", mcount_name);
28136     }
28137   else
28138     {
28139 #ifndef NO_PROFILE_COUNTERS
28140       fprintf (file, "\tmovl\t$%sP%d,%%" PROFILE_COUNT_REGISTER "\n",
28141                LPREFIX, labelno);
28142 #endif
28143       fprintf (file, "\tcall\t%s\n", mcount_name);
28144     }
28145 }
28146
28147 /* We don't have exact information about the insn sizes, but we may assume
28148    quite safely that we are informed about all 1 byte insns and memory
28149    address sizes.  This is enough to eliminate unnecessary padding in
28150    99% of cases.  */
28151
28152 static int
28153 min_insn_size (rtx insn)
28154 {
28155   int l = 0, len;
28156
28157   if (!INSN_P (insn) || !active_insn_p (insn))
28158     return 0;
28159
28160   /* Discard alignments we've emit and jump instructions.  */
28161   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
28162       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
28163     return 0;
28164   if (JUMP_TABLE_DATA_P (insn))
28165     return 0;
28166
28167   /* Important case - calls are always 5 bytes.
28168      It is common to have many calls in the row.  */
28169   if (CALL_P (insn)
28170       && symbolic_reference_mentioned_p (PATTERN (insn))
28171       && !SIBLING_CALL_P (insn))
28172     return 5;
28173   len = get_attr_length (insn);
28174   if (len <= 1)
28175     return 1;
28176
28177   /* For normal instructions we rely on get_attr_length being exact,
28178      with a few exceptions.  */
28179   if (!JUMP_P (insn))
28180     {
28181       enum attr_type type = get_attr_type (insn);
28182
28183       switch (type)
28184         {
28185         case TYPE_MULTI:
28186           if (GET_CODE (PATTERN (insn)) == ASM_INPUT
28187               || asm_noperands (PATTERN (insn)) >= 0)
28188             return 0;
28189           break;
28190         case TYPE_OTHER:
28191         case TYPE_FCMP:
28192           break;
28193         default:
28194           /* Otherwise trust get_attr_length.  */
28195           return len;
28196         }
28197
28198       l = get_attr_length_address (insn);
28199       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
28200         l = 4;
28201     }
28202   if (l)
28203     return 1+l;
28204   else
28205     return 2;
28206 }
28207
28208 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
28209
28210 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
28211    window.  */
28212
28213 static void
28214 ix86_avoid_jump_mispredicts (void)
28215 {
28216   rtx insn, start = get_insns ();
28217   int nbytes = 0, njumps = 0;
28218   int isjump = 0;
28219
28220   /* Look for all minimal intervals of instructions containing 4 jumps.
28221      The intervals are bounded by START and INSN.  NBYTES is the total
28222      size of instructions in the interval including INSN and not including
28223      START.  When the NBYTES is smaller than 16 bytes, it is possible
28224      that the end of START and INSN ends up in the same 16byte page.
28225
28226      The smallest offset in the page INSN can start is the case where START
28227      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
28228      We add p2align to 16byte window with maxskip 15 - NBYTES + sizeof (INSN).
28229      */
28230   for (insn = start; insn; insn = NEXT_INSN (insn))
28231     {
28232       int min_size;
28233
28234       if (LABEL_P (insn))
28235         {
28236           int align = label_to_alignment (insn);
28237           int max_skip = label_to_max_skip (insn);
28238
28239           if (max_skip > 15)
28240             max_skip = 15;
28241           /* If align > 3, only up to 16 - max_skip - 1 bytes can be
28242              already in the current 16 byte page, because otherwise
28243              ASM_OUTPUT_MAX_SKIP_ALIGN could skip max_skip or fewer
28244              bytes to reach 16 byte boundary.  */
28245           if (align <= 0
28246               || (align <= 3 && max_skip != (1 << align) - 1))
28247             max_skip = 0;
28248           if (dump_file)
28249             fprintf (dump_file, "Label %i with max_skip %i\n",
28250                      INSN_UID (insn), max_skip);
28251           if (max_skip)
28252             {
28253               while (nbytes + max_skip >= 16)
28254                 {
28255                   start = NEXT_INSN (start);
28256                   if ((JUMP_P (start)
28257                        && GET_CODE (PATTERN (start)) != ADDR_VEC
28258                        && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
28259                       || CALL_P (start))
28260                     njumps--, isjump = 1;
28261                   else
28262                     isjump = 0;
28263                   nbytes -= min_insn_size (start);
28264                 }
28265             }
28266           continue;
28267         }
28268
28269       min_size = min_insn_size (insn);
28270       nbytes += min_size;
28271       if (dump_file)
28272         fprintf (dump_file, "Insn %i estimated to %i bytes\n",
28273                  INSN_UID (insn), min_size);
28274       if ((JUMP_P (insn)
28275            && GET_CODE (PATTERN (insn)) != ADDR_VEC
28276            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
28277           || CALL_P (insn))
28278         njumps++;
28279       else
28280         continue;
28281
28282       while (njumps > 3)
28283         {
28284           start = NEXT_INSN (start);
28285           if ((JUMP_P (start)
28286                && GET_CODE (PATTERN (start)) != ADDR_VEC
28287                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
28288               || CALL_P (start))
28289             njumps--, isjump = 1;
28290           else
28291             isjump = 0;
28292           nbytes -= min_insn_size (start);
28293         }
28294       gcc_assert (njumps >= 0);
28295       if (dump_file)
28296         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
28297                  INSN_UID (start), INSN_UID (insn), nbytes);
28298
28299       if (njumps == 3 && isjump && nbytes < 16)
28300         {
28301           int padsize = 15 - nbytes + min_insn_size (insn);
28302
28303           if (dump_file)
28304             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
28305                      INSN_UID (insn), padsize);
28306           emit_insn_before (gen_pad (GEN_INT (padsize)), insn);
28307         }
28308     }
28309 }
28310 #endif
28311
28312 /* AMD Athlon works faster
28313    when RET is not destination of conditional jump or directly preceded
28314    by other jump instruction.  We avoid the penalty by inserting NOP just
28315    before the RET instructions in such cases.  */
28316 static void
28317 ix86_pad_returns (void)
28318 {
28319   edge e;
28320   edge_iterator ei;
28321
28322   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
28323     {
28324       basic_block bb = e->src;
28325       rtx ret = BB_END (bb);
28326       rtx prev;
28327       bool replace = false;
28328
28329       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
28330           || optimize_bb_for_size_p (bb))
28331         continue;
28332       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
28333         if (active_insn_p (prev) || LABEL_P (prev))
28334           break;
28335       if (prev && LABEL_P (prev))
28336         {
28337           edge e;
28338           edge_iterator ei;
28339
28340           FOR_EACH_EDGE (e, ei, bb->preds)
28341             if (EDGE_FREQUENCY (e) && e->src->index >= 0
28342                 && !(e->flags & EDGE_FALLTHRU))
28343               replace = true;
28344         }
28345       if (!replace)
28346         {
28347           prev = prev_active_insn (ret);
28348           if (prev
28349               && ((JUMP_P (prev) && any_condjump_p (prev))
28350                   || CALL_P (prev)))
28351             replace = true;
28352           /* Empty functions get branch mispredict even when the jump destination
28353              is not visible to us.  */
28354           if (!prev && !optimize_function_for_size_p (cfun))
28355             replace = true;
28356         }
28357       if (replace)
28358         {
28359           emit_jump_insn_before (gen_return_internal_long (), ret);
28360           delete_insn (ret);
28361         }
28362     }
28363 }
28364
28365 /* Count the minimum number of instructions in BB.  Return 4 if the
28366    number of instructions >= 4.  */
28367
28368 static int 
28369 ix86_count_insn_bb (basic_block bb)
28370 {
28371   rtx insn;
28372   int insn_count = 0;
28373
28374   /* Count number of instructions in this block.  Return 4 if the number
28375      of instructions >= 4.  */
28376   FOR_BB_INSNS (bb, insn)
28377     {
28378       /* Only happen in exit blocks.  */
28379       if (JUMP_P (insn)
28380           && GET_CODE (PATTERN (insn)) == RETURN)
28381         break;
28382
28383       if (NONDEBUG_INSN_P (insn)
28384           && GET_CODE (PATTERN (insn)) != USE
28385           && GET_CODE (PATTERN (insn)) != CLOBBER)
28386         {
28387           insn_count++;
28388           if (insn_count >= 4)
28389             return insn_count;
28390         }
28391     }
28392
28393   return insn_count;
28394 }
28395
28396
28397 /* Count the minimum number of instructions in code path in BB.  
28398    Return 4 if the number of instructions >= 4.  */
28399
28400 static int 
28401 ix86_count_insn (basic_block bb)
28402 {
28403   edge e;
28404   edge_iterator ei;
28405   int min_prev_count;
28406
28407   /* Only bother counting instructions along paths with no
28408      more than 2 basic blocks between entry and exit.  Given
28409      that BB has an edge to exit, determine if a predecessor
28410      of BB has an edge from entry.  If so, compute the number
28411      of instructions in the predecessor block.  If there
28412      happen to be multiple such blocks, compute the minimum.  */
28413   min_prev_count = 4;
28414   FOR_EACH_EDGE (e, ei, bb->preds)
28415     {
28416       edge prev_e;
28417       edge_iterator prev_ei;
28418
28419       if (e->src == ENTRY_BLOCK_PTR)
28420         {
28421           min_prev_count = 0;
28422           break;
28423         }
28424       FOR_EACH_EDGE (prev_e, prev_ei, e->src->preds)
28425         {
28426           if (prev_e->src == ENTRY_BLOCK_PTR)
28427             {
28428               int count = ix86_count_insn_bb (e->src);
28429               if (count < min_prev_count)
28430                 min_prev_count = count;
28431               break;
28432             }
28433         }
28434     }
28435
28436   if (min_prev_count < 4)
28437     min_prev_count += ix86_count_insn_bb (bb);
28438
28439   return min_prev_count;
28440 }
28441
28442 /* Pad short funtion to 4 instructions.   */
28443
28444 static void
28445 ix86_pad_short_function (void)
28446 {
28447   edge e;
28448   edge_iterator ei;
28449
28450   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
28451     {
28452       rtx ret = BB_END (e->src);
28453       if (JUMP_P (ret) && GET_CODE (PATTERN (ret)) == RETURN)
28454         {
28455           int insn_count = ix86_count_insn (e->src);
28456
28457           /* Pad short function.  */
28458           if (insn_count < 4)
28459             {
28460               rtx insn = ret;
28461
28462               /* Find epilogue.  */
28463               while (insn
28464                      && (!NOTE_P (insn)
28465                          || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG))
28466                 insn = PREV_INSN (insn);
28467
28468               if (!insn)
28469                 insn = ret;
28470
28471               /* Two NOPs are counted as one instruction.  */
28472               insn_count = 2 * (4  - insn_count);
28473               emit_insn_before (gen_nops (GEN_INT (insn_count)), insn);
28474             }
28475         }
28476     }
28477 }
28478
28479 /* Implement machine specific optimizations.  We implement padding of returns
28480    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
28481 static void
28482 ix86_reorg (void)
28483 {
28484   if (optimize && optimize_function_for_speed_p (cfun))
28485     {
28486       if (TARGET_PAD_SHORT_FUNCTION)
28487         ix86_pad_short_function ();
28488       else if (TARGET_PAD_RETURNS)
28489         ix86_pad_returns ();
28490 #ifdef ASM_OUTPUT_MAX_SKIP_PAD
28491       if (TARGET_FOUR_JUMP_LIMIT)
28492         ix86_avoid_jump_mispredicts ();
28493 #endif
28494     }
28495 }
28496
28497 /* Return nonzero when QImode register that must be represented via REX prefix
28498    is used.  */
28499 bool
28500 x86_extended_QIreg_mentioned_p (rtx insn)
28501 {
28502   int i;
28503   extract_insn_cached (insn);
28504   for (i = 0; i < recog_data.n_operands; i++)
28505     if (REG_P (recog_data.operand[i])
28506         && REGNO (recog_data.operand[i]) > BX_REG)
28507        return true;
28508   return false;
28509 }
28510
28511 /* Return nonzero when P points to register encoded via REX prefix.
28512    Called via for_each_rtx.  */
28513 static int
28514 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
28515 {
28516    unsigned int regno;
28517    if (!REG_P (*p))
28518      return 0;
28519    regno = REGNO (*p);
28520    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
28521 }
28522
28523 /* Return true when INSN mentions register that must be encoded using REX
28524    prefix.  */
28525 bool
28526 x86_extended_reg_mentioned_p (rtx insn)
28527 {
28528   return for_each_rtx (INSN_P (insn) ? &PATTERN (insn) : &insn,
28529                        extended_reg_mentioned_1, NULL);
28530 }
28531
28532 /* If profitable, negate (without causing overflow) integer constant
28533    of mode MODE at location LOC.  Return true in this case.  */
28534 bool
28535 x86_maybe_negate_const_int (rtx *loc, enum machine_mode mode)
28536 {
28537   HOST_WIDE_INT val;
28538
28539   if (!CONST_INT_P (*loc))
28540     return false;
28541
28542   switch (mode)
28543     {
28544     case DImode:
28545       /* DImode x86_64 constants must fit in 32 bits.  */
28546       gcc_assert (x86_64_immediate_operand (*loc, mode));
28547
28548       mode = SImode;
28549       break;
28550
28551     case SImode:
28552     case HImode:
28553     case QImode:
28554       break;
28555
28556     default:
28557       gcc_unreachable ();
28558     }
28559
28560   /* Avoid overflows.  */
28561   if (mode_signbit_p (mode, *loc))
28562     return false;
28563
28564   val = INTVAL (*loc);
28565
28566   /* Make things pretty and `subl $4,%eax' rather than `addl $-4,%eax'.
28567      Exceptions: -128 encodes smaller than 128, so swap sign and op.  */
28568   if ((val < 0 && val != -128)
28569       || val == 128)
28570     {
28571       *loc = GEN_INT (-val);
28572       return true;
28573     }
28574
28575   return false;
28576 }
28577
28578 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
28579    optabs would emit if we didn't have TFmode patterns.  */
28580
28581 void
28582 x86_emit_floatuns (rtx operands[2])
28583 {
28584   rtx neglab, donelab, i0, i1, f0, in, out;
28585   enum machine_mode mode, inmode;
28586
28587   inmode = GET_MODE (operands[1]);
28588   gcc_assert (inmode == SImode || inmode == DImode);
28589
28590   out = operands[0];
28591   in = force_reg (inmode, operands[1]);
28592   mode = GET_MODE (out);
28593   neglab = gen_label_rtx ();
28594   donelab = gen_label_rtx ();
28595   f0 = gen_reg_rtx (mode);
28596
28597   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
28598
28599   expand_float (out, in, 0);
28600
28601   emit_jump_insn (gen_jump (donelab));
28602   emit_barrier ();
28603
28604   emit_label (neglab);
28605
28606   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
28607                             1, OPTAB_DIRECT);
28608   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
28609                             1, OPTAB_DIRECT);
28610   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
28611
28612   expand_float (f0, i0, 0);
28613
28614   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
28615
28616   emit_label (donelab);
28617 }
28618 \f
28619 /* AVX does not support 32-byte integer vector operations,
28620    thus the longest vector we are faced with is V16QImode.  */
28621 #define MAX_VECT_LEN    16
28622
28623 struct expand_vec_perm_d
28624 {
28625   rtx target, op0, op1;
28626   unsigned char perm[MAX_VECT_LEN];
28627   enum machine_mode vmode;
28628   unsigned char nelt;
28629   bool testing_p;
28630 };
28631
28632 static bool expand_vec_perm_1 (struct expand_vec_perm_d *d);
28633 static bool expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d);
28634
28635 /* Get a vector mode of the same size as the original but with elements
28636    twice as wide.  This is only guaranteed to apply to integral vectors.  */
28637
28638 static inline enum machine_mode
28639 get_mode_wider_vector (enum machine_mode o)
28640 {
28641   /* ??? Rely on the ordering that genmodes.c gives to vectors.  */
28642   enum machine_mode n = GET_MODE_WIDER_MODE (o);
28643   gcc_assert (GET_MODE_NUNITS (o) == GET_MODE_NUNITS (n) * 2);
28644   gcc_assert (GET_MODE_SIZE (o) == GET_MODE_SIZE (n));
28645   return n;
28646 }
28647
28648 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
28649    with all elements equal to VAR.  Return true if successful.  */
28650
28651 static bool
28652 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
28653                                    rtx target, rtx val)
28654 {
28655   bool ok;
28656
28657   switch (mode)
28658     {
28659     case V2SImode:
28660     case V2SFmode:
28661       if (!mmx_ok)
28662         return false;
28663       /* FALLTHRU */
28664
28665     case V4DFmode:
28666     case V4DImode:
28667     case V8SFmode:
28668     case V8SImode:
28669     case V2DFmode:
28670     case V2DImode:
28671     case V4SFmode:
28672     case V4SImode:
28673       {
28674         rtx insn, dup;
28675
28676         /* First attempt to recognize VAL as-is.  */
28677         dup = gen_rtx_VEC_DUPLICATE (mode, val);
28678         insn = emit_insn (gen_rtx_SET (VOIDmode, target, dup));
28679         if (recog_memoized (insn) < 0)
28680           {
28681             rtx seq;
28682             /* If that fails, force VAL into a register.  */
28683
28684             start_sequence ();
28685             XEXP (dup, 0) = force_reg (GET_MODE_INNER (mode), val);
28686             seq = get_insns ();
28687             end_sequence ();
28688             if (seq)
28689               emit_insn_before (seq, insn);
28690
28691             ok = recog_memoized (insn) >= 0;
28692             gcc_assert (ok);
28693           }
28694       }
28695       return true;
28696
28697     case V4HImode:
28698       if (!mmx_ok)
28699         return false;
28700       if (TARGET_SSE || TARGET_3DNOW_A)
28701         {
28702           rtx x;
28703
28704           val = gen_lowpart (SImode, val);
28705           x = gen_rtx_TRUNCATE (HImode, val);
28706           x = gen_rtx_VEC_DUPLICATE (mode, x);
28707           emit_insn (gen_rtx_SET (VOIDmode, target, x));
28708           return true;
28709         }
28710       goto widen;
28711
28712     case V8QImode:
28713       if (!mmx_ok)
28714         return false;
28715       goto widen;
28716
28717     case V8HImode:
28718       if (TARGET_SSE2)
28719         {
28720           struct expand_vec_perm_d dperm;
28721           rtx tmp1, tmp2;
28722
28723         permute:
28724           memset (&dperm, 0, sizeof (dperm));
28725           dperm.target = target;
28726           dperm.vmode = mode;
28727           dperm.nelt = GET_MODE_NUNITS (mode);
28728           dperm.op0 = dperm.op1 = gen_reg_rtx (mode);
28729
28730           /* Extend to SImode using a paradoxical SUBREG.  */
28731           tmp1 = gen_reg_rtx (SImode);
28732           emit_move_insn (tmp1, gen_lowpart (SImode, val));
28733
28734           /* Insert the SImode value as low element of a V4SImode vector. */
28735           tmp2 = gen_lowpart (V4SImode, dperm.op0);
28736           emit_insn (gen_vec_setv4si_0 (tmp2, CONST0_RTX (V4SImode), tmp1));
28737
28738           ok = (expand_vec_perm_1 (&dperm)
28739                 || expand_vec_perm_broadcast_1 (&dperm));
28740           gcc_assert (ok);
28741           return ok;
28742         }
28743       goto widen;
28744
28745     case V16QImode:
28746       if (TARGET_SSE2)
28747         goto permute;
28748       goto widen;
28749
28750     widen:
28751       /* Replicate the value once into the next wider mode and recurse.  */
28752       {
28753         enum machine_mode smode, wsmode, wvmode;
28754         rtx x;
28755
28756         smode = GET_MODE_INNER (mode);
28757         wvmode = get_mode_wider_vector (mode);
28758         wsmode = GET_MODE_INNER (wvmode);
28759
28760         val = convert_modes (wsmode, smode, val, true);
28761         x = expand_simple_binop (wsmode, ASHIFT, val,
28762                                  GEN_INT (GET_MODE_BITSIZE (smode)),
28763                                  NULL_RTX, 1, OPTAB_LIB_WIDEN);
28764         val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
28765
28766         x = gen_lowpart (wvmode, target);
28767         ok = ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val);
28768         gcc_assert (ok);
28769         return ok;
28770       }
28771
28772     case V16HImode:
28773     case V32QImode:
28774       {
28775         enum machine_mode hvmode = (mode == V16HImode ? V8HImode : V16QImode);
28776         rtx x = gen_reg_rtx (hvmode);
28777
28778         ok = ix86_expand_vector_init_duplicate (false, hvmode, x, val);
28779         gcc_assert (ok);
28780
28781         x = gen_rtx_VEC_CONCAT (mode, x, x);
28782         emit_insn (gen_rtx_SET (VOIDmode, target, x));
28783       }
28784       return true;
28785
28786     default:
28787       return false;
28788     }
28789 }
28790
28791 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
28792    whose ONE_VAR element is VAR, and other elements are zero.  Return true
28793    if successful.  */
28794
28795 static bool
28796 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
28797                                      rtx target, rtx var, int one_var)
28798 {
28799   enum machine_mode vsimode;
28800   rtx new_target;
28801   rtx x, tmp;
28802   bool use_vector_set = false;
28803
28804   switch (mode)
28805     {
28806     case V2DImode:
28807       /* For SSE4.1, we normally use vector set.  But if the second
28808          element is zero and inter-unit moves are OK, we use movq
28809          instead.  */
28810       use_vector_set = (TARGET_64BIT
28811                         && TARGET_SSE4_1
28812                         && !(TARGET_INTER_UNIT_MOVES
28813                              && one_var == 0));
28814       break;
28815     case V16QImode:
28816     case V4SImode:
28817     case V4SFmode:
28818       use_vector_set = TARGET_SSE4_1;
28819       break;
28820     case V8HImode:
28821       use_vector_set = TARGET_SSE2;
28822       break;
28823     case V4HImode:
28824       use_vector_set = TARGET_SSE || TARGET_3DNOW_A;
28825       break;
28826     case V32QImode:
28827     case V16HImode:
28828     case V8SImode:
28829     case V8SFmode:
28830     case V4DFmode:
28831       use_vector_set = TARGET_AVX;
28832       break;
28833     case V4DImode:
28834       /* Use ix86_expand_vector_set in 64bit mode only.  */
28835       use_vector_set = TARGET_AVX && TARGET_64BIT;
28836       break;
28837     default:
28838       break;
28839     }
28840
28841   if (use_vector_set)
28842     {
28843       emit_insn (gen_rtx_SET (VOIDmode, target, CONST0_RTX (mode)));
28844       var = force_reg (GET_MODE_INNER (mode), var);
28845       ix86_expand_vector_set (mmx_ok, target, var, one_var);
28846       return true;
28847     }
28848
28849   switch (mode)
28850     {
28851     case V2SFmode:
28852     case V2SImode:
28853       if (!mmx_ok)
28854         return false;
28855       /* FALLTHRU */
28856
28857     case V2DFmode:
28858     case V2DImode:
28859       if (one_var != 0)
28860         return false;
28861       var = force_reg (GET_MODE_INNER (mode), var);
28862       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
28863       emit_insn (gen_rtx_SET (VOIDmode, target, x));
28864       return true;
28865
28866     case V4SFmode:
28867     case V4SImode:
28868       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
28869         new_target = gen_reg_rtx (mode);
28870       else
28871         new_target = target;
28872       var = force_reg (GET_MODE_INNER (mode), var);
28873       x = gen_rtx_VEC_DUPLICATE (mode, var);
28874       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
28875       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
28876       if (one_var != 0)
28877         {
28878           /* We need to shuffle the value to the correct position, so
28879              create a new pseudo to store the intermediate result.  */
28880
28881           /* With SSE2, we can use the integer shuffle insns.  */
28882           if (mode != V4SFmode && TARGET_SSE2)
28883             {
28884               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
28885                                             const1_rtx,
28886                                             GEN_INT (one_var == 1 ? 0 : 1),
28887                                             GEN_INT (one_var == 2 ? 0 : 1),
28888                                             GEN_INT (one_var == 3 ? 0 : 1)));
28889               if (target != new_target)
28890                 emit_move_insn (target, new_target);
28891               return true;
28892             }
28893
28894           /* Otherwise convert the intermediate result to V4SFmode and
28895              use the SSE1 shuffle instructions.  */
28896           if (mode != V4SFmode)
28897             {
28898               tmp = gen_reg_rtx (V4SFmode);
28899               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
28900             }
28901           else
28902             tmp = new_target;
28903
28904           emit_insn (gen_sse_shufps_v4sf (tmp, tmp, tmp,
28905                                        const1_rtx,
28906                                        GEN_INT (one_var == 1 ? 0 : 1),
28907                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
28908                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
28909
28910           if (mode != V4SFmode)
28911             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
28912           else if (tmp != target)
28913             emit_move_insn (target, tmp);
28914         }
28915       else if (target != new_target)
28916         emit_move_insn (target, new_target);
28917       return true;
28918
28919     case V8HImode:
28920     case V16QImode:
28921       vsimode = V4SImode;
28922       goto widen;
28923     case V4HImode:
28924     case V8QImode:
28925       if (!mmx_ok)
28926         return false;
28927       vsimode = V2SImode;
28928       goto widen;
28929     widen:
28930       if (one_var != 0)
28931         return false;
28932
28933       /* Zero extend the variable element to SImode and recurse.  */
28934       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
28935
28936       x = gen_reg_rtx (vsimode);
28937       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
28938                                                 var, one_var))
28939         gcc_unreachable ();
28940
28941       emit_move_insn (target, gen_lowpart (mode, x));
28942       return true;
28943
28944     default:
28945       return false;
28946     }
28947 }
28948
28949 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
28950    consisting of the values in VALS.  It is known that all elements
28951    except ONE_VAR are constants.  Return true if successful.  */
28952
28953 static bool
28954 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
28955                                  rtx target, rtx vals, int one_var)
28956 {
28957   rtx var = XVECEXP (vals, 0, one_var);
28958   enum machine_mode wmode;
28959   rtx const_vec, x;
28960
28961   const_vec = copy_rtx (vals);
28962   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
28963   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
28964
28965   switch (mode)
28966     {
28967     case V2DFmode:
28968     case V2DImode:
28969     case V2SFmode:
28970     case V2SImode:
28971       /* For the two element vectors, it's just as easy to use
28972          the general case.  */
28973       return false;
28974
28975     case V4DImode:
28976       /* Use ix86_expand_vector_set in 64bit mode only.  */
28977       if (!TARGET_64BIT)
28978         return false;
28979     case V4DFmode:
28980     case V8SFmode:
28981     case V8SImode:
28982     case V16HImode:
28983     case V32QImode:
28984     case V4SFmode:
28985     case V4SImode:
28986     case V8HImode:
28987     case V4HImode:
28988       break;
28989
28990     case V16QImode:
28991       if (TARGET_SSE4_1)
28992         break;
28993       wmode = V8HImode;
28994       goto widen;
28995     case V8QImode:
28996       wmode = V4HImode;
28997       goto widen;
28998     widen:
28999       /* There's no way to set one QImode entry easily.  Combine
29000          the variable value with its adjacent constant value, and
29001          promote to an HImode set.  */
29002       x = XVECEXP (vals, 0, one_var ^ 1);
29003       if (one_var & 1)
29004         {
29005           var = convert_modes (HImode, QImode, var, true);
29006           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
29007                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
29008           x = GEN_INT (INTVAL (x) & 0xff);
29009         }
29010       else
29011         {
29012           var = convert_modes (HImode, QImode, var, true);
29013           x = gen_int_mode (INTVAL (x) << 8, HImode);
29014         }
29015       if (x != const0_rtx)
29016         var = expand_simple_binop (HImode, IOR, var, x, var,
29017                                    1, OPTAB_LIB_WIDEN);
29018
29019       x = gen_reg_rtx (wmode);
29020       emit_move_insn (x, gen_lowpart (wmode, const_vec));
29021       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
29022
29023       emit_move_insn (target, gen_lowpart (mode, x));
29024       return true;
29025
29026     default:
29027       return false;
29028     }
29029
29030   emit_move_insn (target, const_vec);
29031   ix86_expand_vector_set (mmx_ok, target, var, one_var);
29032   return true;
29033 }
29034
29035 /* A subroutine of ix86_expand_vector_init_general.  Use vector
29036    concatenate to handle the most general case: all values variable,
29037    and none identical.  */
29038
29039 static void
29040 ix86_expand_vector_init_concat (enum machine_mode mode,
29041                                 rtx target, rtx *ops, int n)
29042 {
29043   enum machine_mode cmode, hmode = VOIDmode;
29044   rtx first[8], second[4];
29045   rtvec v;
29046   int i, j;
29047
29048   switch (n)
29049     {
29050     case 2:
29051       switch (mode)
29052         {
29053         case V8SImode:
29054           cmode = V4SImode;
29055           break;
29056         case V8SFmode:
29057           cmode = V4SFmode;
29058           break;
29059         case V4DImode:
29060           cmode = V2DImode;
29061           break;
29062         case V4DFmode:
29063           cmode = V2DFmode;
29064           break;
29065         case V4SImode:
29066           cmode = V2SImode;
29067           break;
29068         case V4SFmode:
29069           cmode = V2SFmode;
29070           break;
29071         case V2DImode:
29072           cmode = DImode;
29073           break;
29074         case V2SImode:
29075           cmode = SImode;
29076           break;
29077         case V2DFmode:
29078           cmode = DFmode;
29079           break;
29080         case V2SFmode:
29081           cmode = SFmode;
29082           break;
29083         default:
29084           gcc_unreachable ();
29085         }
29086
29087       if (!register_operand (ops[1], cmode))
29088         ops[1] = force_reg (cmode, ops[1]);
29089       if (!register_operand (ops[0], cmode))
29090         ops[0] = force_reg (cmode, ops[0]);
29091       emit_insn (gen_rtx_SET (VOIDmode, target,
29092                               gen_rtx_VEC_CONCAT (mode, ops[0],
29093                                                   ops[1])));
29094       break;
29095
29096     case 4:
29097       switch (mode)
29098         {
29099         case V4DImode:
29100           cmode = V2DImode;
29101           break;
29102         case V4DFmode:
29103           cmode = V2DFmode;
29104           break;
29105         case V4SImode:
29106           cmode = V2SImode;
29107           break;
29108         case V4SFmode:
29109           cmode = V2SFmode;
29110           break;
29111         default:
29112           gcc_unreachable ();
29113         }
29114       goto half;
29115
29116     case 8:
29117       switch (mode)
29118         {
29119         case V8SImode:
29120           cmode = V2SImode;
29121           hmode = V4SImode;
29122           break;
29123         case V8SFmode:
29124           cmode = V2SFmode;
29125           hmode = V4SFmode;
29126           break;
29127         default:
29128           gcc_unreachable ();
29129         }
29130       goto half;
29131
29132 half:
29133       /* FIXME: We process inputs backward to help RA.  PR 36222.  */
29134       i = n - 1;
29135       j = (n >> 1) - 1;
29136       for (; i > 0; i -= 2, j--)
29137         {
29138           first[j] = gen_reg_rtx (cmode);
29139           v = gen_rtvec (2, ops[i - 1], ops[i]);
29140           ix86_expand_vector_init (false, first[j],
29141                                    gen_rtx_PARALLEL (cmode, v));
29142         }
29143
29144       n >>= 1;
29145       if (n > 2)
29146         {
29147           gcc_assert (hmode != VOIDmode);
29148           for (i = j = 0; i < n; i += 2, j++)
29149             {
29150               second[j] = gen_reg_rtx (hmode);
29151               ix86_expand_vector_init_concat (hmode, second [j],
29152                                               &first [i], 2);
29153             }
29154           n >>= 1;
29155           ix86_expand_vector_init_concat (mode, target, second, n);
29156         }
29157       else
29158         ix86_expand_vector_init_concat (mode, target, first, n);
29159       break;
29160
29161     default:
29162       gcc_unreachable ();
29163     }
29164 }
29165
29166 /* A subroutine of ix86_expand_vector_init_general.  Use vector
29167    interleave to handle the most general case: all values variable,
29168    and none identical.  */
29169
29170 static void
29171 ix86_expand_vector_init_interleave (enum machine_mode mode,
29172                                     rtx target, rtx *ops, int n)
29173 {
29174   enum machine_mode first_imode, second_imode, third_imode, inner_mode;
29175   int i, j;
29176   rtx op0, op1;
29177   rtx (*gen_load_even) (rtx, rtx, rtx);
29178   rtx (*gen_interleave_first_low) (rtx, rtx, rtx);
29179   rtx (*gen_interleave_second_low) (rtx, rtx, rtx);
29180
29181   switch (mode)
29182     {
29183     case V8HImode:
29184       gen_load_even = gen_vec_setv8hi;
29185       gen_interleave_first_low = gen_vec_interleave_lowv4si;
29186       gen_interleave_second_low = gen_vec_interleave_lowv2di;
29187       inner_mode = HImode;
29188       first_imode = V4SImode;
29189       second_imode = V2DImode;
29190       third_imode = VOIDmode;
29191       break;
29192     case V16QImode:
29193       gen_load_even = gen_vec_setv16qi;
29194       gen_interleave_first_low = gen_vec_interleave_lowv8hi;
29195       gen_interleave_second_low = gen_vec_interleave_lowv4si;
29196       inner_mode = QImode;
29197       first_imode = V8HImode;
29198       second_imode = V4SImode;
29199       third_imode = V2DImode;
29200       break;
29201     default:
29202       gcc_unreachable ();
29203     }
29204
29205   for (i = 0; i < n; i++)
29206     {
29207       /* Extend the odd elment to SImode using a paradoxical SUBREG.  */
29208       op0 = gen_reg_rtx (SImode);
29209       emit_move_insn (op0, gen_lowpart (SImode, ops [i + i]));
29210
29211       /* Insert the SImode value as low element of V4SImode vector. */
29212       op1 = gen_reg_rtx (V4SImode);
29213       op0 = gen_rtx_VEC_MERGE (V4SImode,
29214                                gen_rtx_VEC_DUPLICATE (V4SImode,
29215                                                       op0),
29216                                CONST0_RTX (V4SImode),
29217                                const1_rtx);
29218       emit_insn (gen_rtx_SET (VOIDmode, op1, op0));
29219
29220       /* Cast the V4SImode vector back to a vector in orignal mode.  */
29221       op0 = gen_reg_rtx (mode);
29222       emit_move_insn (op0, gen_lowpart (mode, op1));
29223
29224       /* Load even elements into the second positon.  */
29225       emit_insn (gen_load_even (op0,
29226                                 force_reg (inner_mode,
29227                                            ops [i + i + 1]),
29228                                 const1_rtx));
29229
29230       /* Cast vector to FIRST_IMODE vector.  */
29231       ops[i] = gen_reg_rtx (first_imode);
29232       emit_move_insn (ops[i], gen_lowpart (first_imode, op0));
29233     }
29234
29235   /* Interleave low FIRST_IMODE vectors.  */
29236   for (i = j = 0; i < n; i += 2, j++)
29237     {
29238       op0 = gen_reg_rtx (first_imode);
29239       emit_insn (gen_interleave_first_low (op0, ops[i], ops[i + 1]));
29240
29241       /* Cast FIRST_IMODE vector to SECOND_IMODE vector.  */
29242       ops[j] = gen_reg_rtx (second_imode);
29243       emit_move_insn (ops[j], gen_lowpart (second_imode, op0));
29244     }
29245
29246   /* Interleave low SECOND_IMODE vectors.  */
29247   switch (second_imode)
29248     {
29249     case V4SImode:
29250       for (i = j = 0; i < n / 2; i += 2, j++)
29251         {
29252           op0 = gen_reg_rtx (second_imode);
29253           emit_insn (gen_interleave_second_low (op0, ops[i],
29254                                                 ops[i + 1]));
29255
29256           /* Cast the SECOND_IMODE vector to the THIRD_IMODE
29257              vector.  */
29258           ops[j] = gen_reg_rtx (third_imode);
29259           emit_move_insn (ops[j], gen_lowpart (third_imode, op0));
29260         }
29261       second_imode = V2DImode;
29262       gen_interleave_second_low = gen_vec_interleave_lowv2di;
29263       /* FALLTHRU */
29264
29265     case V2DImode:
29266       op0 = gen_reg_rtx (second_imode);
29267       emit_insn (gen_interleave_second_low (op0, ops[0],
29268                                             ops[1]));
29269
29270       /* Cast the SECOND_IMODE vector back to a vector on original
29271          mode.  */
29272       emit_insn (gen_rtx_SET (VOIDmode, target,
29273                               gen_lowpart (mode, op0)));
29274       break;
29275
29276     default:
29277       gcc_unreachable ();
29278     }
29279 }
29280
29281 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
29282    all values variable, and none identical.  */
29283
29284 static void
29285 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
29286                                  rtx target, rtx vals)
29287 {
29288   rtx ops[32], op0, op1;
29289   enum machine_mode half_mode = VOIDmode;
29290   int n, i;
29291
29292   switch (mode)
29293     {
29294     case V2SFmode:
29295     case V2SImode:
29296       if (!mmx_ok && !TARGET_SSE)
29297         break;
29298       /* FALLTHRU */
29299
29300     case V8SFmode:
29301     case V8SImode:
29302     case V4DFmode:
29303     case V4DImode:
29304     case V4SFmode:
29305     case V4SImode:
29306     case V2DFmode:
29307     case V2DImode:
29308       n = GET_MODE_NUNITS (mode);
29309       for (i = 0; i < n; i++)
29310         ops[i] = XVECEXP (vals, 0, i);
29311       ix86_expand_vector_init_concat (mode, target, ops, n);
29312       return;
29313
29314     case V32QImode:
29315       half_mode = V16QImode;
29316       goto half;
29317
29318     case V16HImode:
29319       half_mode = V8HImode;
29320       goto half;
29321
29322 half:
29323       n = GET_MODE_NUNITS (mode);
29324       for (i = 0; i < n; i++)
29325         ops[i] = XVECEXP (vals, 0, i);
29326       op0 = gen_reg_rtx (half_mode);
29327       op1 = gen_reg_rtx (half_mode);
29328       ix86_expand_vector_init_interleave (half_mode, op0, ops,
29329                                           n >> 2);
29330       ix86_expand_vector_init_interleave (half_mode, op1,
29331                                           &ops [n >> 1], n >> 2);
29332       emit_insn (gen_rtx_SET (VOIDmode, target,
29333                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
29334       return;
29335
29336     case V16QImode:
29337       if (!TARGET_SSE4_1)
29338         break;
29339       /* FALLTHRU */
29340
29341     case V8HImode:
29342       if (!TARGET_SSE2)
29343         break;
29344
29345       /* Don't use ix86_expand_vector_init_interleave if we can't
29346          move from GPR to SSE register directly.  */
29347       if (!TARGET_INTER_UNIT_MOVES)
29348         break;
29349
29350       n = GET_MODE_NUNITS (mode);
29351       for (i = 0; i < n; i++)
29352         ops[i] = XVECEXP (vals, 0, i);
29353       ix86_expand_vector_init_interleave (mode, target, ops, n >> 1);
29354       return;
29355
29356     case V4HImode:
29357     case V8QImode:
29358       break;
29359
29360     default:
29361       gcc_unreachable ();
29362     }
29363
29364     {
29365       int i, j, n_elts, n_words, n_elt_per_word;
29366       enum machine_mode inner_mode;
29367       rtx words[4], shift;
29368
29369       inner_mode = GET_MODE_INNER (mode);
29370       n_elts = GET_MODE_NUNITS (mode);
29371       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
29372       n_elt_per_word = n_elts / n_words;
29373       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
29374
29375       for (i = 0; i < n_words; ++i)
29376         {
29377           rtx word = NULL_RTX;
29378
29379           for (j = 0; j < n_elt_per_word; ++j)
29380             {
29381               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
29382               elt = convert_modes (word_mode, inner_mode, elt, true);
29383
29384               if (j == 0)
29385                 word = elt;
29386               else
29387                 {
29388                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
29389                                               word, 1, OPTAB_LIB_WIDEN);
29390                   word = expand_simple_binop (word_mode, IOR, word, elt,
29391                                               word, 1, OPTAB_LIB_WIDEN);
29392                 }
29393             }
29394
29395           words[i] = word;
29396         }
29397
29398       if (n_words == 1)
29399         emit_move_insn (target, gen_lowpart (mode, words[0]));
29400       else if (n_words == 2)
29401         {
29402           rtx tmp = gen_reg_rtx (mode);
29403           emit_clobber (tmp);
29404           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
29405           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
29406           emit_move_insn (target, tmp);
29407         }
29408       else if (n_words == 4)
29409         {
29410           rtx tmp = gen_reg_rtx (V4SImode);
29411           gcc_assert (word_mode == SImode);
29412           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
29413           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
29414           emit_move_insn (target, gen_lowpart (mode, tmp));
29415         }
29416       else
29417         gcc_unreachable ();
29418     }
29419 }
29420
29421 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
29422    instructions unless MMX_OK is true.  */
29423
29424 void
29425 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
29426 {
29427   enum machine_mode mode = GET_MODE (target);
29428   enum machine_mode inner_mode = GET_MODE_INNER (mode);
29429   int n_elts = GET_MODE_NUNITS (mode);
29430   int n_var = 0, one_var = -1;
29431   bool all_same = true, all_const_zero = true;
29432   int i;
29433   rtx x;
29434
29435   for (i = 0; i < n_elts; ++i)
29436     {
29437       x = XVECEXP (vals, 0, i);
29438       if (!(CONST_INT_P (x)
29439             || GET_CODE (x) == CONST_DOUBLE
29440             || GET_CODE (x) == CONST_FIXED))
29441         n_var++, one_var = i;
29442       else if (x != CONST0_RTX (inner_mode))
29443         all_const_zero = false;
29444       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
29445         all_same = false;
29446     }
29447
29448   /* Constants are best loaded from the constant pool.  */
29449   if (n_var == 0)
29450     {
29451       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
29452       return;
29453     }
29454
29455   /* If all values are identical, broadcast the value.  */
29456   if (all_same
29457       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
29458                                             XVECEXP (vals, 0, 0)))
29459     return;
29460
29461   /* Values where only one field is non-constant are best loaded from
29462      the pool and overwritten via move later.  */
29463   if (n_var == 1)
29464     {
29465       if (all_const_zero
29466           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
29467                                                   XVECEXP (vals, 0, one_var),
29468                                                   one_var))
29469         return;
29470
29471       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
29472         return;
29473     }
29474
29475   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
29476 }
29477
29478 void
29479 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
29480 {
29481   enum machine_mode mode = GET_MODE (target);
29482   enum machine_mode inner_mode = GET_MODE_INNER (mode);
29483   enum machine_mode half_mode;
29484   bool use_vec_merge = false;
29485   rtx tmp;
29486   static rtx (*gen_extract[6][2]) (rtx, rtx)
29487     = {
29488         { gen_vec_extract_lo_v32qi, gen_vec_extract_hi_v32qi },
29489         { gen_vec_extract_lo_v16hi, gen_vec_extract_hi_v16hi },
29490         { gen_vec_extract_lo_v8si, gen_vec_extract_hi_v8si },
29491         { gen_vec_extract_lo_v4di, gen_vec_extract_hi_v4di },
29492         { gen_vec_extract_lo_v8sf, gen_vec_extract_hi_v8sf },
29493         { gen_vec_extract_lo_v4df, gen_vec_extract_hi_v4df }
29494       };
29495   static rtx (*gen_insert[6][2]) (rtx, rtx, rtx)
29496     = {
29497         { gen_vec_set_lo_v32qi, gen_vec_set_hi_v32qi },
29498         { gen_vec_set_lo_v16hi, gen_vec_set_hi_v16hi },
29499         { gen_vec_set_lo_v8si, gen_vec_set_hi_v8si },
29500         { gen_vec_set_lo_v4di, gen_vec_set_hi_v4di },
29501         { gen_vec_set_lo_v8sf, gen_vec_set_hi_v8sf },
29502         { gen_vec_set_lo_v4df, gen_vec_set_hi_v4df }
29503       };
29504   int i, j, n;
29505
29506   switch (mode)
29507     {
29508     case V2SFmode:
29509     case V2SImode:
29510       if (mmx_ok)
29511         {
29512           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
29513           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
29514           if (elt == 0)
29515             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
29516           else
29517             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
29518           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
29519           return;
29520         }
29521       break;
29522
29523     case V2DImode:
29524       use_vec_merge = TARGET_SSE4_1;
29525       if (use_vec_merge)
29526         break;
29527
29528     case V2DFmode:
29529       {
29530         rtx op0, op1;
29531
29532         /* For the two element vectors, we implement a VEC_CONCAT with
29533            the extraction of the other element.  */
29534
29535         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
29536         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
29537
29538         if (elt == 0)
29539           op0 = val, op1 = tmp;
29540         else
29541           op0 = tmp, op1 = val;
29542
29543         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
29544         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
29545       }
29546       return;
29547
29548     case V4SFmode:
29549       use_vec_merge = TARGET_SSE4_1;
29550       if (use_vec_merge)
29551         break;
29552
29553       switch (elt)
29554         {
29555         case 0:
29556           use_vec_merge = true;
29557           break;
29558
29559         case 1:
29560           /* tmp = target = A B C D */
29561           tmp = copy_to_reg (target);
29562           /* target = A A B B */
29563           emit_insn (gen_vec_interleave_lowv4sf (target, target, target));
29564           /* target = X A B B */
29565           ix86_expand_vector_set (false, target, val, 0);
29566           /* target = A X C D  */
29567           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
29568                                           const1_rtx, const0_rtx,
29569                                           GEN_INT (2+4), GEN_INT (3+4)));
29570           return;
29571
29572         case 2:
29573           /* tmp = target = A B C D */
29574           tmp = copy_to_reg (target);
29575           /* tmp = X B C D */
29576           ix86_expand_vector_set (false, tmp, val, 0);
29577           /* target = A B X D */
29578           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
29579                                           const0_rtx, const1_rtx,
29580                                           GEN_INT (0+4), GEN_INT (3+4)));
29581           return;
29582
29583         case 3:
29584           /* tmp = target = A B C D */
29585           tmp = copy_to_reg (target);
29586           /* tmp = X B C D */
29587           ix86_expand_vector_set (false, tmp, val, 0);
29588           /* target = A B X D */
29589           emit_insn (gen_sse_shufps_v4sf (target, target, tmp,
29590                                           const0_rtx, const1_rtx,
29591                                           GEN_INT (2+4), GEN_INT (0+4)));
29592           return;
29593
29594         default:
29595           gcc_unreachable ();
29596         }
29597       break;
29598
29599     case V4SImode:
29600       use_vec_merge = TARGET_SSE4_1;
29601       if (use_vec_merge)
29602         break;
29603
29604       /* Element 0 handled by vec_merge below.  */
29605       if (elt == 0)
29606         {
29607           use_vec_merge = true;
29608           break;
29609         }
29610
29611       if (TARGET_SSE2)
29612         {
29613           /* With SSE2, use integer shuffles to swap element 0 and ELT,
29614              store into element 0, then shuffle them back.  */
29615
29616           rtx order[4];
29617
29618           order[0] = GEN_INT (elt);
29619           order[1] = const1_rtx;
29620           order[2] = const2_rtx;
29621           order[3] = GEN_INT (3);
29622           order[elt] = const0_rtx;
29623
29624           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
29625                                         order[1], order[2], order[3]));
29626
29627           ix86_expand_vector_set (false, target, val, 0);
29628
29629           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
29630                                         order[1], order[2], order[3]));
29631         }
29632       else
29633         {
29634           /* For SSE1, we have to reuse the V4SF code.  */
29635           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
29636                                   gen_lowpart (SFmode, val), elt);
29637         }
29638       return;
29639
29640     case V8HImode:
29641       use_vec_merge = TARGET_SSE2;
29642       break;
29643     case V4HImode:
29644       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
29645       break;
29646
29647     case V16QImode:
29648       use_vec_merge = TARGET_SSE4_1;
29649       break;
29650
29651     case V8QImode:
29652       break;
29653
29654     case V32QImode:
29655       half_mode = V16QImode;
29656       j = 0;
29657       n = 16;
29658       goto half;
29659
29660     case V16HImode:
29661       half_mode = V8HImode;
29662       j = 1;
29663       n = 8;
29664       goto half;
29665
29666     case V8SImode:
29667       half_mode = V4SImode;
29668       j = 2;
29669       n = 4;
29670       goto half;
29671
29672     case V4DImode:
29673       half_mode = V2DImode;
29674       j = 3;
29675       n = 2;
29676       goto half;
29677
29678     case V8SFmode:
29679       half_mode = V4SFmode;
29680       j = 4;
29681       n = 4;
29682       goto half;
29683
29684     case V4DFmode:
29685       half_mode = V2DFmode;
29686       j = 5;
29687       n = 2;
29688       goto half;
29689
29690 half:
29691       /* Compute offset.  */
29692       i = elt / n;
29693       elt %= n;
29694
29695       gcc_assert (i <= 1);
29696
29697       /* Extract the half.  */
29698       tmp = gen_reg_rtx (half_mode);
29699       emit_insn (gen_extract[j][i] (tmp, target));
29700
29701       /* Put val in tmp at elt.  */
29702       ix86_expand_vector_set (false, tmp, val, elt);
29703
29704       /* Put it back.  */
29705       emit_insn (gen_insert[j][i] (target, target, tmp));
29706       return;
29707
29708     default:
29709       break;
29710     }
29711
29712   if (use_vec_merge)
29713     {
29714       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
29715       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
29716       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
29717     }
29718   else
29719     {
29720       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
29721
29722       emit_move_insn (mem, target);
29723
29724       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
29725       emit_move_insn (tmp, val);
29726
29727       emit_move_insn (target, mem);
29728     }
29729 }
29730
29731 void
29732 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
29733 {
29734   enum machine_mode mode = GET_MODE (vec);
29735   enum machine_mode inner_mode = GET_MODE_INNER (mode);
29736   bool use_vec_extr = false;
29737   rtx tmp;
29738
29739   switch (mode)
29740     {
29741     case V2SImode:
29742     case V2SFmode:
29743       if (!mmx_ok)
29744         break;
29745       /* FALLTHRU */
29746
29747     case V2DFmode:
29748     case V2DImode:
29749       use_vec_extr = true;
29750       break;
29751
29752     case V4SFmode:
29753       use_vec_extr = TARGET_SSE4_1;
29754       if (use_vec_extr)
29755         break;
29756
29757       switch (elt)
29758         {
29759         case 0:
29760           tmp = vec;
29761           break;
29762
29763         case 1:
29764         case 3:
29765           tmp = gen_reg_rtx (mode);
29766           emit_insn (gen_sse_shufps_v4sf (tmp, vec, vec,
29767                                        GEN_INT (elt), GEN_INT (elt),
29768                                        GEN_INT (elt+4), GEN_INT (elt+4)));
29769           break;
29770
29771         case 2:
29772           tmp = gen_reg_rtx (mode);
29773           emit_insn (gen_vec_interleave_highv4sf (tmp, vec, vec));
29774           break;
29775
29776         default:
29777           gcc_unreachable ();
29778         }
29779       vec = tmp;
29780       use_vec_extr = true;
29781       elt = 0;
29782       break;
29783
29784     case V4SImode:
29785       use_vec_extr = TARGET_SSE4_1;
29786       if (use_vec_extr)
29787         break;
29788
29789       if (TARGET_SSE2)
29790         {
29791           switch (elt)
29792             {
29793             case 0:
29794               tmp = vec;
29795               break;
29796
29797             case 1:
29798             case 3:
29799               tmp = gen_reg_rtx (mode);
29800               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
29801                                             GEN_INT (elt), GEN_INT (elt),
29802                                             GEN_INT (elt), GEN_INT (elt)));
29803               break;
29804
29805             case 2:
29806               tmp = gen_reg_rtx (mode);
29807               emit_insn (gen_vec_interleave_highv4si (tmp, vec, vec));
29808               break;
29809
29810             default:
29811               gcc_unreachable ();
29812             }
29813           vec = tmp;
29814           use_vec_extr = true;
29815           elt = 0;
29816         }
29817       else
29818         {
29819           /* For SSE1, we have to reuse the V4SF code.  */
29820           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
29821                                       gen_lowpart (V4SFmode, vec), elt);
29822           return;
29823         }
29824       break;
29825
29826     case V8HImode:
29827       use_vec_extr = TARGET_SSE2;
29828       break;
29829     case V4HImode:
29830       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
29831       break;
29832
29833     case V16QImode:
29834       use_vec_extr = TARGET_SSE4_1;
29835       break;
29836
29837     case V8QImode:
29838       /* ??? Could extract the appropriate HImode element and shift.  */
29839     default:
29840       break;
29841     }
29842
29843   if (use_vec_extr)
29844     {
29845       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
29846       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
29847
29848       /* Let the rtl optimizers know about the zero extension performed.  */
29849       if (inner_mode == QImode || inner_mode == HImode)
29850         {
29851           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
29852           target = gen_lowpart (SImode, target);
29853         }
29854
29855       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
29856     }
29857   else
29858     {
29859       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
29860
29861       emit_move_insn (mem, vec);
29862
29863       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
29864       emit_move_insn (target, tmp);
29865     }
29866 }
29867
29868 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
29869    pattern to reduce; DEST is the destination; IN is the input vector.  */
29870
29871 void
29872 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
29873 {
29874   rtx tmp1, tmp2, tmp3;
29875
29876   tmp1 = gen_reg_rtx (V4SFmode);
29877   tmp2 = gen_reg_rtx (V4SFmode);
29878   tmp3 = gen_reg_rtx (V4SFmode);
29879
29880   emit_insn (gen_sse_movhlps (tmp1, in, in));
29881   emit_insn (fn (tmp2, tmp1, in));
29882
29883   emit_insn (gen_sse_shufps_v4sf (tmp3, tmp2, tmp2,
29884                                   const1_rtx, const1_rtx,
29885                                   GEN_INT (1+4), GEN_INT (1+4)));
29886   emit_insn (fn (dest, tmp2, tmp3));
29887 }
29888 \f
29889 /* Target hook for scalar_mode_supported_p.  */
29890 static bool
29891 ix86_scalar_mode_supported_p (enum machine_mode mode)
29892 {
29893   if (DECIMAL_FLOAT_MODE_P (mode))
29894     return default_decimal_float_supported_p ();
29895   else if (mode == TFmode)
29896     return true;
29897   else
29898     return default_scalar_mode_supported_p (mode);
29899 }
29900
29901 /* Implements target hook vector_mode_supported_p.  */
29902 static bool
29903 ix86_vector_mode_supported_p (enum machine_mode mode)
29904 {
29905   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
29906     return true;
29907   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
29908     return true;
29909   if (TARGET_AVX && VALID_AVX256_REG_MODE (mode))
29910     return true;
29911   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
29912     return true;
29913   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
29914     return true;
29915   return false;
29916 }
29917
29918 /* Target hook for c_mode_for_suffix.  */
29919 static enum machine_mode
29920 ix86_c_mode_for_suffix (char suffix)
29921 {
29922   if (suffix == 'q')
29923     return TFmode;
29924   if (suffix == 'w')
29925     return XFmode;
29926
29927   return VOIDmode;
29928 }
29929
29930 /* Worker function for TARGET_MD_ASM_CLOBBERS.
29931
29932    We do this in the new i386 backend to maintain source compatibility
29933    with the old cc0-based compiler.  */
29934
29935 static tree
29936 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
29937                       tree inputs ATTRIBUTE_UNUSED,
29938                       tree clobbers)
29939 {
29940   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
29941                         clobbers);
29942   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
29943                         clobbers);
29944   return clobbers;
29945 }
29946
29947 /* Implements target vector targetm.asm.encode_section_info.  This
29948    is not used by netware.  */
29949
29950 static void ATTRIBUTE_UNUSED
29951 ix86_encode_section_info (tree decl, rtx rtl, int first)
29952 {
29953   default_encode_section_info (decl, rtl, first);
29954
29955   if (TREE_CODE (decl) == VAR_DECL
29956       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
29957       && ix86_in_large_data_p (decl))
29958     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
29959 }
29960
29961 /* Worker function for REVERSE_CONDITION.  */
29962
29963 enum rtx_code
29964 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
29965 {
29966   return (mode != CCFPmode && mode != CCFPUmode
29967           ? reverse_condition (code)
29968           : reverse_condition_maybe_unordered (code));
29969 }
29970
29971 /* Output code to perform an x87 FP register move, from OPERANDS[1]
29972    to OPERANDS[0].  */
29973
29974 const char *
29975 output_387_reg_move (rtx insn, rtx *operands)
29976 {
29977   if (REG_P (operands[0]))
29978     {
29979       if (REG_P (operands[1])
29980           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
29981         {
29982           if (REGNO (operands[0]) == FIRST_STACK_REG)
29983             return output_387_ffreep (operands, 0);
29984           return "fstp\t%y0";
29985         }
29986       if (STACK_TOP_P (operands[0]))
29987         return "fld%Z1\t%y1";
29988       return "fst\t%y0";
29989     }
29990   else if (MEM_P (operands[0]))
29991     {
29992       gcc_assert (REG_P (operands[1]));
29993       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
29994         return "fstp%Z0\t%y0";
29995       else
29996         {
29997           /* There is no non-popping store to memory for XFmode.
29998              So if we need one, follow the store with a load.  */
29999           if (GET_MODE (operands[0]) == XFmode)
30000             return "fstp%Z0\t%y0\n\tfld%Z0\t%y0";
30001           else
30002             return "fst%Z0\t%y0";
30003         }
30004     }
30005   else
30006     gcc_unreachable();
30007 }
30008
30009 /* Output code to perform a conditional jump to LABEL, if C2 flag in
30010    FP status register is set.  */
30011
30012 void
30013 ix86_emit_fp_unordered_jump (rtx label)
30014 {
30015   rtx reg = gen_reg_rtx (HImode);
30016   rtx temp;
30017
30018   emit_insn (gen_x86_fnstsw_1 (reg));
30019
30020   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_insn_for_size_p ()))
30021     {
30022       emit_insn (gen_x86_sahf_1 (reg));
30023
30024       temp = gen_rtx_REG (CCmode, FLAGS_REG);
30025       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
30026     }
30027   else
30028     {
30029       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
30030
30031       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
30032       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
30033     }
30034
30035   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
30036                               gen_rtx_LABEL_REF (VOIDmode, label),
30037                               pc_rtx);
30038   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
30039
30040   emit_jump_insn (temp);
30041   predict_jump (REG_BR_PROB_BASE * 10 / 100);
30042 }
30043
30044 /* Output code to perform a log1p XFmode calculation.  */
30045
30046 void ix86_emit_i387_log1p (rtx op0, rtx op1)
30047 {
30048   rtx label1 = gen_label_rtx ();
30049   rtx label2 = gen_label_rtx ();
30050
30051   rtx tmp = gen_reg_rtx (XFmode);
30052   rtx tmp2 = gen_reg_rtx (XFmode);
30053   rtx test;
30054
30055   emit_insn (gen_absxf2 (tmp, op1));
30056   test = gen_rtx_GE (VOIDmode, tmp,
30057     CONST_DOUBLE_FROM_REAL_VALUE (
30058        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
30059        XFmode));
30060   emit_jump_insn (gen_cbranchxf4 (test, XEXP (test, 0), XEXP (test, 1), label1));
30061
30062   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
30063   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
30064   emit_jump (label2);
30065
30066   emit_label (label1);
30067   emit_move_insn (tmp, CONST1_RTX (XFmode));
30068   emit_insn (gen_addxf3 (tmp, op1, tmp));
30069   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
30070   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
30071
30072   emit_label (label2);
30073 }
30074
30075 /* Output code to perform a Newton-Rhapson approximation of a single precision
30076    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
30077
30078 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
30079 {
30080   rtx x0, x1, e0, e1, two;
30081
30082   x0 = gen_reg_rtx (mode);
30083   e0 = gen_reg_rtx (mode);
30084   e1 = gen_reg_rtx (mode);
30085   x1 = gen_reg_rtx (mode);
30086
30087   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
30088
30089   if (VECTOR_MODE_P (mode))
30090     two = ix86_build_const_vector (mode, true, two);
30091
30092   two = force_reg (mode, two);
30093
30094   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
30095
30096   /* x0 = rcp(b) estimate */
30097   emit_insn (gen_rtx_SET (VOIDmode, x0,
30098                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
30099                                           UNSPEC_RCP)));
30100   /* e0 = x0 * a */
30101   emit_insn (gen_rtx_SET (VOIDmode, e0,
30102                           gen_rtx_MULT (mode, x0, a)));
30103   /* e1 = x0 * b */
30104   emit_insn (gen_rtx_SET (VOIDmode, e1,
30105                           gen_rtx_MULT (mode, x0, b)));
30106   /* x1 = 2. - e1 */
30107   emit_insn (gen_rtx_SET (VOIDmode, x1,
30108                           gen_rtx_MINUS (mode, two, e1)));
30109   /* res = e0 * x1 */
30110   emit_insn (gen_rtx_SET (VOIDmode, res,
30111                           gen_rtx_MULT (mode, e0, x1)));
30112 }
30113
30114 /* Output code to perform a Newton-Rhapson approximation of a
30115    single precision floating point [reciprocal] square root.  */
30116
30117 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
30118                          bool recip)
30119 {
30120   rtx x0, e0, e1, e2, e3, mthree, mhalf;
30121   REAL_VALUE_TYPE r;
30122
30123   x0 = gen_reg_rtx (mode);
30124   e0 = gen_reg_rtx (mode);
30125   e1 = gen_reg_rtx (mode);
30126   e2 = gen_reg_rtx (mode);
30127   e3 = gen_reg_rtx (mode);
30128
30129   real_from_integer (&r, VOIDmode, -3, -1, 0);
30130   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
30131
30132   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
30133   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
30134
30135   if (VECTOR_MODE_P (mode))
30136     {
30137       mthree = ix86_build_const_vector (mode, true, mthree);
30138       mhalf = ix86_build_const_vector (mode, true, mhalf);
30139     }
30140
30141   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
30142      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
30143
30144   /* x0 = rsqrt(a) estimate */
30145   emit_insn (gen_rtx_SET (VOIDmode, x0,
30146                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
30147                                           UNSPEC_RSQRT)));
30148
30149   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
30150   if (!recip)
30151     {
30152       rtx zero, mask;
30153
30154       zero = gen_reg_rtx (mode);
30155       mask = gen_reg_rtx (mode);
30156
30157       zero = force_reg (mode, CONST0_RTX(mode));
30158       emit_insn (gen_rtx_SET (VOIDmode, mask,
30159                               gen_rtx_NE (mode, zero, a)));
30160
30161       emit_insn (gen_rtx_SET (VOIDmode, x0,
30162                               gen_rtx_AND (mode, x0, mask)));
30163     }
30164
30165   /* e0 = x0 * a */
30166   emit_insn (gen_rtx_SET (VOIDmode, e0,
30167                           gen_rtx_MULT (mode, x0, a)));
30168   /* e1 = e0 * x0 */
30169   emit_insn (gen_rtx_SET (VOIDmode, e1,
30170                           gen_rtx_MULT (mode, e0, x0)));
30171
30172   /* e2 = e1 - 3. */
30173   mthree = force_reg (mode, mthree);
30174   emit_insn (gen_rtx_SET (VOIDmode, e2,
30175                           gen_rtx_PLUS (mode, e1, mthree)));
30176
30177   mhalf = force_reg (mode, mhalf);
30178   if (recip)
30179     /* e3 = -.5 * x0 */
30180     emit_insn (gen_rtx_SET (VOIDmode, e3,
30181                             gen_rtx_MULT (mode, x0, mhalf)));
30182   else
30183     /* e3 = -.5 * e0 */
30184     emit_insn (gen_rtx_SET (VOIDmode, e3,
30185                             gen_rtx_MULT (mode, e0, mhalf)));
30186   /* ret = e2 * e3 */
30187   emit_insn (gen_rtx_SET (VOIDmode, res,
30188                           gen_rtx_MULT (mode, e2, e3)));
30189 }
30190
30191 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
30192
30193 static void ATTRIBUTE_UNUSED
30194 i386_solaris_elf_named_section (const char *name, unsigned int flags,
30195                                 tree decl)
30196 {
30197   /* With Binutils 2.15, the "@unwind" marker must be specified on
30198      every occurrence of the ".eh_frame" section, not just the first
30199      one.  */
30200   if (TARGET_64BIT
30201       && strcmp (name, ".eh_frame") == 0)
30202     {
30203       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
30204                flags & SECTION_WRITE ? "aw" : "a");
30205       return;
30206     }
30207   default_elf_asm_named_section (name, flags, decl);
30208 }
30209
30210 /* Return the mangling of TYPE if it is an extended fundamental type.  */
30211
30212 static const char *
30213 ix86_mangle_type (const_tree type)
30214 {
30215   type = TYPE_MAIN_VARIANT (type);
30216
30217   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
30218       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
30219     return NULL;
30220
30221   switch (TYPE_MODE (type))
30222     {
30223     case TFmode:
30224       /* __float128 is "g".  */
30225       return "g";
30226     case XFmode:
30227       /* "long double" or __float80 is "e".  */
30228       return "e";
30229     default:
30230       return NULL;
30231     }
30232 }
30233
30234 /* For 32-bit code we can save PIC register setup by using
30235    __stack_chk_fail_local hidden function instead of calling
30236    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
30237    register, so it is better to call __stack_chk_fail directly.  */
30238
30239 static tree
30240 ix86_stack_protect_fail (void)
30241 {
30242   return TARGET_64BIT
30243          ? default_external_stack_protect_fail ()
30244          : default_hidden_stack_protect_fail ();
30245 }
30246
30247 /* Select a format to encode pointers in exception handling data.  CODE
30248    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
30249    true if the symbol may be affected by dynamic relocations.
30250
30251    ??? All x86 object file formats are capable of representing this.
30252    After all, the relocation needed is the same as for the call insn.
30253    Whether or not a particular assembler allows us to enter such, I
30254    guess we'll have to see.  */
30255 int
30256 asm_preferred_eh_data_format (int code, int global)
30257 {
30258   if (flag_pic)
30259     {
30260       int type = DW_EH_PE_sdata8;
30261       if (!TARGET_64BIT
30262           || ix86_cmodel == CM_SMALL_PIC
30263           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
30264         type = DW_EH_PE_sdata4;
30265       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
30266     }
30267   if (ix86_cmodel == CM_SMALL
30268       || (ix86_cmodel == CM_MEDIUM && code))
30269     return DW_EH_PE_udata4;
30270   return DW_EH_PE_absptr;
30271 }
30272 \f
30273 /* Expand copysign from SIGN to the positive value ABS_VALUE
30274    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
30275    the sign-bit.  */
30276 static void
30277 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
30278 {
30279   enum machine_mode mode = GET_MODE (sign);
30280   rtx sgn = gen_reg_rtx (mode);
30281   if (mask == NULL_RTX)
30282     {
30283       enum machine_mode vmode;
30284
30285       if (mode == SFmode)
30286         vmode = V4SFmode;
30287       else if (mode == DFmode)
30288         vmode = V2DFmode;
30289       else
30290         vmode = mode;
30291
30292       mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), false);
30293       if (!VECTOR_MODE_P (mode))
30294         {
30295           /* We need to generate a scalar mode mask in this case.  */
30296           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
30297           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
30298           mask = gen_reg_rtx (mode);
30299           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
30300         }
30301     }
30302   else
30303     mask = gen_rtx_NOT (mode, mask);
30304   emit_insn (gen_rtx_SET (VOIDmode, sgn,
30305                           gen_rtx_AND (mode, mask, sign)));
30306   emit_insn (gen_rtx_SET (VOIDmode, result,
30307                           gen_rtx_IOR (mode, abs_value, sgn)));
30308 }
30309
30310 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
30311    mask for masking out the sign-bit is stored in *SMASK, if that is
30312    non-null.  */
30313 static rtx
30314 ix86_expand_sse_fabs (rtx op0, rtx *smask)
30315 {
30316   enum machine_mode vmode, mode = GET_MODE (op0);
30317   rtx xa, mask;
30318
30319   xa = gen_reg_rtx (mode);
30320   if (mode == SFmode)
30321     vmode = V4SFmode;
30322   else if (mode == DFmode)
30323     vmode = V2DFmode;
30324   else
30325     vmode = mode;
30326   mask = ix86_build_signbit_mask (vmode, VECTOR_MODE_P (mode), true);
30327   if (!VECTOR_MODE_P (mode))
30328     {
30329       /* We need to generate a scalar mode mask in this case.  */
30330       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
30331       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
30332       mask = gen_reg_rtx (mode);
30333       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
30334     }
30335   emit_insn (gen_rtx_SET (VOIDmode, xa,
30336                           gen_rtx_AND (mode, op0, mask)));
30337
30338   if (smask)
30339     *smask = mask;
30340
30341   return xa;
30342 }
30343
30344 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
30345    swapping the operands if SWAP_OPERANDS is true.  The expanded
30346    code is a forward jump to a newly created label in case the
30347    comparison is true.  The generated label rtx is returned.  */
30348 static rtx
30349 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
30350                                   bool swap_operands)
30351 {
30352   rtx label, tmp;
30353
30354   if (swap_operands)
30355     {
30356       tmp = op0;
30357       op0 = op1;
30358       op1 = tmp;
30359     }
30360
30361   label = gen_label_rtx ();
30362   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
30363   emit_insn (gen_rtx_SET (VOIDmode, tmp,
30364                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
30365   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
30366   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
30367                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
30368   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
30369   JUMP_LABEL (tmp) = label;
30370
30371   return label;
30372 }
30373
30374 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
30375    using comparison code CODE.  Operands are swapped for the comparison if
30376    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
30377 static rtx
30378 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
30379                               bool swap_operands)
30380 {
30381   enum machine_mode mode = GET_MODE (op0);
30382   rtx mask = gen_reg_rtx (mode);
30383
30384   if (swap_operands)
30385     {
30386       rtx tmp = op0;
30387       op0 = op1;
30388       op1 = tmp;
30389     }
30390
30391   if (mode == DFmode)
30392     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
30393                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
30394   else
30395     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
30396                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
30397
30398   return mask;
30399 }
30400
30401 /* Generate and return a rtx of mode MODE for 2**n where n is the number
30402    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
30403 static rtx
30404 ix86_gen_TWO52 (enum machine_mode mode)
30405 {
30406   REAL_VALUE_TYPE TWO52r;
30407   rtx TWO52;
30408
30409   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
30410   TWO52 = const_double_from_real_value (TWO52r, mode);
30411   TWO52 = force_reg (mode, TWO52);
30412
30413   return TWO52;
30414 }
30415
30416 /* Expand SSE sequence for computing lround from OP1 storing
30417    into OP0.  */
30418 void
30419 ix86_expand_lround (rtx op0, rtx op1)
30420 {
30421   /* C code for the stuff we're doing below:
30422        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
30423        return (long)tmp;
30424    */
30425   enum machine_mode mode = GET_MODE (op1);
30426   const struct real_format *fmt;
30427   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
30428   rtx adj;
30429
30430   /* load nextafter (0.5, 0.0) */
30431   fmt = REAL_MODE_FORMAT (mode);
30432   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
30433   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
30434
30435   /* adj = copysign (0.5, op1) */
30436   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
30437   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
30438
30439   /* adj = op1 + adj */
30440   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
30441
30442   /* op0 = (imode)adj */
30443   expand_fix (op0, adj, 0);
30444 }
30445
30446 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
30447    into OPERAND0.  */
30448 void
30449 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
30450 {
30451   /* C code for the stuff we're doing below (for do_floor):
30452         xi = (long)op1;
30453         xi -= (double)xi > op1 ? 1 : 0;
30454         return xi;
30455    */
30456   enum machine_mode fmode = GET_MODE (op1);
30457   enum machine_mode imode = GET_MODE (op0);
30458   rtx ireg, freg, label, tmp;
30459
30460   /* reg = (long)op1 */
30461   ireg = gen_reg_rtx (imode);
30462   expand_fix (ireg, op1, 0);
30463
30464   /* freg = (double)reg */
30465   freg = gen_reg_rtx (fmode);
30466   expand_float (freg, ireg, 0);
30467
30468   /* ireg = (freg > op1) ? ireg - 1 : ireg */
30469   label = ix86_expand_sse_compare_and_jump (UNLE,
30470                                             freg, op1, !do_floor);
30471   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
30472                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
30473   emit_move_insn (ireg, tmp);
30474
30475   emit_label (label);
30476   LABEL_NUSES (label) = 1;
30477
30478   emit_move_insn (op0, ireg);
30479 }
30480
30481 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
30482    result in OPERAND0.  */
30483 void
30484 ix86_expand_rint (rtx operand0, rtx operand1)
30485 {
30486   /* C code for the stuff we're doing below:
30487         xa = fabs (operand1);
30488         if (!isless (xa, 2**52))
30489           return operand1;
30490         xa = xa + 2**52 - 2**52;
30491         return copysign (xa, operand1);
30492    */
30493   enum machine_mode mode = GET_MODE (operand0);
30494   rtx res, xa, label, TWO52, mask;
30495
30496   res = gen_reg_rtx (mode);
30497   emit_move_insn (res, operand1);
30498
30499   /* xa = abs (operand1) */
30500   xa = ix86_expand_sse_fabs (res, &mask);
30501
30502   /* if (!isless (xa, TWO52)) goto label; */
30503   TWO52 = ix86_gen_TWO52 (mode);
30504   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30505
30506   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
30507   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
30508
30509   ix86_sse_copysign_to_positive (res, xa, res, mask);
30510
30511   emit_label (label);
30512   LABEL_NUSES (label) = 1;
30513
30514   emit_move_insn (operand0, res);
30515 }
30516
30517 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
30518    into OPERAND0.  */
30519 void
30520 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
30521 {
30522   /* C code for the stuff we expand below.
30523         double xa = fabs (x), x2;
30524         if (!isless (xa, TWO52))
30525           return x;
30526         xa = xa + TWO52 - TWO52;
30527         x2 = copysign (xa, x);
30528      Compensate.  Floor:
30529         if (x2 > x)
30530           x2 -= 1;
30531      Compensate.  Ceil:
30532         if (x2 < x)
30533           x2 -= -1;
30534         return x2;
30535    */
30536   enum machine_mode mode = GET_MODE (operand0);
30537   rtx xa, TWO52, tmp, label, one, res, mask;
30538
30539   TWO52 = ix86_gen_TWO52 (mode);
30540
30541   /* Temporary for holding the result, initialized to the input
30542      operand to ease control flow.  */
30543   res = gen_reg_rtx (mode);
30544   emit_move_insn (res, operand1);
30545
30546   /* xa = abs (operand1) */
30547   xa = ix86_expand_sse_fabs (res, &mask);
30548
30549   /* if (!isless (xa, TWO52)) goto label; */
30550   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30551
30552   /* xa = xa + TWO52 - TWO52; */
30553   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
30554   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
30555
30556   /* xa = copysign (xa, operand1) */
30557   ix86_sse_copysign_to_positive (xa, xa, res, mask);
30558
30559   /* generate 1.0 or -1.0 */
30560   one = force_reg (mode,
30561                    const_double_from_real_value (do_floor
30562                                                  ? dconst1 : dconstm1, mode));
30563
30564   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
30565   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
30566   emit_insn (gen_rtx_SET (VOIDmode, tmp,
30567                           gen_rtx_AND (mode, one, tmp)));
30568   /* We always need to subtract here to preserve signed zero.  */
30569   tmp = expand_simple_binop (mode, MINUS,
30570                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
30571   emit_move_insn (res, tmp);
30572
30573   emit_label (label);
30574   LABEL_NUSES (label) = 1;
30575
30576   emit_move_insn (operand0, res);
30577 }
30578
30579 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
30580    into OPERAND0.  */
30581 void
30582 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
30583 {
30584   /* C code for the stuff we expand below.
30585         double xa = fabs (x), x2;
30586         if (!isless (xa, TWO52))
30587           return x;
30588         x2 = (double)(long)x;
30589      Compensate.  Floor:
30590         if (x2 > x)
30591           x2 -= 1;
30592      Compensate.  Ceil:
30593         if (x2 < x)
30594           x2 += 1;
30595         if (HONOR_SIGNED_ZEROS (mode))
30596           return copysign (x2, x);
30597         return x2;
30598    */
30599   enum machine_mode mode = GET_MODE (operand0);
30600   rtx xa, xi, TWO52, tmp, label, one, res, mask;
30601
30602   TWO52 = ix86_gen_TWO52 (mode);
30603
30604   /* Temporary for holding the result, initialized to the input
30605      operand to ease control flow.  */
30606   res = gen_reg_rtx (mode);
30607   emit_move_insn (res, operand1);
30608
30609   /* xa = abs (operand1) */
30610   xa = ix86_expand_sse_fabs (res, &mask);
30611
30612   /* if (!isless (xa, TWO52)) goto label; */
30613   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30614
30615   /* xa = (double)(long)x */
30616   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
30617   expand_fix (xi, res, 0);
30618   expand_float (xa, xi, 0);
30619
30620   /* generate 1.0 */
30621   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
30622
30623   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
30624   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
30625   emit_insn (gen_rtx_SET (VOIDmode, tmp,
30626                           gen_rtx_AND (mode, one, tmp)));
30627   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
30628                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
30629   emit_move_insn (res, tmp);
30630
30631   if (HONOR_SIGNED_ZEROS (mode))
30632     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
30633
30634   emit_label (label);
30635   LABEL_NUSES (label) = 1;
30636
30637   emit_move_insn (operand0, res);
30638 }
30639
30640 /* Expand SSE sequence for computing round from OPERAND1 storing
30641    into OPERAND0.  Sequence that works without relying on DImode truncation
30642    via cvttsd2siq that is only available on 64bit targets.  */
30643 void
30644 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
30645 {
30646   /* C code for the stuff we expand below.
30647         double xa = fabs (x), xa2, x2;
30648         if (!isless (xa, TWO52))
30649           return x;
30650      Using the absolute value and copying back sign makes
30651      -0.0 -> -0.0 correct.
30652         xa2 = xa + TWO52 - TWO52;
30653      Compensate.
30654         dxa = xa2 - xa;
30655         if (dxa <= -0.5)
30656           xa2 += 1;
30657         else if (dxa > 0.5)
30658           xa2 -= 1;
30659         x2 = copysign (xa2, x);
30660         return x2;
30661    */
30662   enum machine_mode mode = GET_MODE (operand0);
30663   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
30664
30665   TWO52 = ix86_gen_TWO52 (mode);
30666
30667   /* Temporary for holding the result, initialized to the input
30668      operand to ease control flow.  */
30669   res = gen_reg_rtx (mode);
30670   emit_move_insn (res, operand1);
30671
30672   /* xa = abs (operand1) */
30673   xa = ix86_expand_sse_fabs (res, &mask);
30674
30675   /* if (!isless (xa, TWO52)) goto label; */
30676   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30677
30678   /* xa2 = xa + TWO52 - TWO52; */
30679   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
30680   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
30681
30682   /* dxa = xa2 - xa; */
30683   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
30684
30685   /* generate 0.5, 1.0 and -0.5 */
30686   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
30687   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
30688   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
30689                                0, OPTAB_DIRECT);
30690
30691   /* Compensate.  */
30692   tmp = gen_reg_rtx (mode);
30693   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
30694   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
30695   emit_insn (gen_rtx_SET (VOIDmode, tmp,
30696                           gen_rtx_AND (mode, one, tmp)));
30697   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
30698   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
30699   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
30700   emit_insn (gen_rtx_SET (VOIDmode, tmp,
30701                           gen_rtx_AND (mode, one, tmp)));
30702   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
30703
30704   /* res = copysign (xa2, operand1) */
30705   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
30706
30707   emit_label (label);
30708   LABEL_NUSES (label) = 1;
30709
30710   emit_move_insn (operand0, res);
30711 }
30712
30713 /* Expand SSE sequence for computing trunc from OPERAND1 storing
30714    into OPERAND0.  */
30715 void
30716 ix86_expand_trunc (rtx operand0, rtx operand1)
30717 {
30718   /* C code for SSE variant we expand below.
30719         double xa = fabs (x), x2;
30720         if (!isless (xa, TWO52))
30721           return x;
30722         x2 = (double)(long)x;
30723         if (HONOR_SIGNED_ZEROS (mode))
30724           return copysign (x2, x);
30725         return x2;
30726    */
30727   enum machine_mode mode = GET_MODE (operand0);
30728   rtx xa, xi, TWO52, label, res, mask;
30729
30730   TWO52 = ix86_gen_TWO52 (mode);
30731
30732   /* Temporary for holding the result, initialized to the input
30733      operand to ease control flow.  */
30734   res = gen_reg_rtx (mode);
30735   emit_move_insn (res, operand1);
30736
30737   /* xa = abs (operand1) */
30738   xa = ix86_expand_sse_fabs (res, &mask);
30739
30740   /* if (!isless (xa, TWO52)) goto label; */
30741   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30742
30743   /* x = (double)(long)x */
30744   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
30745   expand_fix (xi, res, 0);
30746   expand_float (res, xi, 0);
30747
30748   if (HONOR_SIGNED_ZEROS (mode))
30749     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
30750
30751   emit_label (label);
30752   LABEL_NUSES (label) = 1;
30753
30754   emit_move_insn (operand0, res);
30755 }
30756
30757 /* Expand SSE sequence for computing trunc from OPERAND1 storing
30758    into OPERAND0.  */
30759 void
30760 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
30761 {
30762   enum machine_mode mode = GET_MODE (operand0);
30763   rtx xa, mask, TWO52, label, one, res, smask, tmp;
30764
30765   /* C code for SSE variant we expand below.
30766         double xa = fabs (x), x2;
30767         if (!isless (xa, TWO52))
30768           return x;
30769         xa2 = xa + TWO52 - TWO52;
30770      Compensate:
30771         if (xa2 > xa)
30772           xa2 -= 1.0;
30773         x2 = copysign (xa2, x);
30774         return x2;
30775    */
30776
30777   TWO52 = ix86_gen_TWO52 (mode);
30778
30779   /* Temporary for holding the result, initialized to the input
30780      operand to ease control flow.  */
30781   res = gen_reg_rtx (mode);
30782   emit_move_insn (res, operand1);
30783
30784   /* xa = abs (operand1) */
30785   xa = ix86_expand_sse_fabs (res, &smask);
30786
30787   /* if (!isless (xa, TWO52)) goto label; */
30788   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30789
30790   /* res = xa + TWO52 - TWO52; */
30791   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
30792   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
30793   emit_move_insn (res, tmp);
30794
30795   /* generate 1.0 */
30796   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
30797
30798   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
30799   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
30800   emit_insn (gen_rtx_SET (VOIDmode, mask,
30801                           gen_rtx_AND (mode, mask, one)));
30802   tmp = expand_simple_binop (mode, MINUS,
30803                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
30804   emit_move_insn (res, tmp);
30805
30806   /* res = copysign (res, operand1) */
30807   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
30808
30809   emit_label (label);
30810   LABEL_NUSES (label) = 1;
30811
30812   emit_move_insn (operand0, res);
30813 }
30814
30815 /* Expand SSE sequence for computing round from OPERAND1 storing
30816    into OPERAND0.  */
30817 void
30818 ix86_expand_round (rtx operand0, rtx operand1)
30819 {
30820   /* C code for the stuff we're doing below:
30821         double xa = fabs (x);
30822         if (!isless (xa, TWO52))
30823           return x;
30824         xa = (double)(long)(xa + nextafter (0.5, 0.0));
30825         return copysign (xa, x);
30826    */
30827   enum machine_mode mode = GET_MODE (operand0);
30828   rtx res, TWO52, xa, label, xi, half, mask;
30829   const struct real_format *fmt;
30830   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
30831
30832   /* Temporary for holding the result, initialized to the input
30833      operand to ease control flow.  */
30834   res = gen_reg_rtx (mode);
30835   emit_move_insn (res, operand1);
30836
30837   TWO52 = ix86_gen_TWO52 (mode);
30838   xa = ix86_expand_sse_fabs (res, &mask);
30839   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
30840
30841   /* load nextafter (0.5, 0.0) */
30842   fmt = REAL_MODE_FORMAT (mode);
30843   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
30844   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
30845
30846   /* xa = xa + 0.5 */
30847   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
30848   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
30849
30850   /* xa = (double)(int64_t)xa */
30851   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
30852   expand_fix (xi, xa, 0);
30853   expand_float (xa, xi, 0);
30854
30855   /* res = copysign (xa, operand1) */
30856   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
30857
30858   emit_label (label);
30859   LABEL_NUSES (label) = 1;
30860
30861   emit_move_insn (operand0, res);
30862 }
30863 \f
30864
30865 /* Table of valid machine attributes.  */
30866 static const struct attribute_spec ix86_attribute_table[] =
30867 {
30868   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
30869   /* Stdcall attribute says callee is responsible for popping arguments
30870      if they are not variable.  */
30871   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
30872   /* Fastcall attribute says callee is responsible for popping arguments
30873      if they are not variable.  */
30874   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
30875   /* Thiscall attribute says callee is responsible for popping arguments
30876      if they are not variable.  */
30877   { "thiscall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
30878   /* Cdecl attribute says the callee is a normal C declaration */
30879   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
30880   /* Regparm attribute specifies how many integer arguments are to be
30881      passed in registers.  */
30882   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
30883   /* Sseregparm attribute says we are using x86_64 calling conventions
30884      for FP arguments.  */
30885   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
30886   /* force_align_arg_pointer says this function realigns the stack at entry.  */
30887   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
30888     false, true,  true, ix86_handle_cconv_attribute },
30889 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
30890   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
30891   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
30892   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
30893 #endif
30894   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
30895   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
30896 #ifdef SUBTARGET_ATTRIBUTE_TABLE
30897   SUBTARGET_ATTRIBUTE_TABLE,
30898 #endif
30899   /* ms_abi and sysv_abi calling convention function attributes.  */
30900   { "ms_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
30901   { "sysv_abi", 0, 0, false, true, true, ix86_handle_abi_attribute },
30902   { "ms_hook_prologue", 0, 0, true, false, false, ix86_handle_fndecl_attribute },
30903   /* End element.  */
30904   { NULL,        0, 0, false, false, false, NULL }
30905 };
30906
30907 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
30908 static int
30909 ix86_builtin_vectorization_cost (enum vect_cost_for_stmt type_of_cost,
30910                                  tree vectype ATTRIBUTE_UNUSED,
30911                                  int misalign ATTRIBUTE_UNUSED)
30912 {
30913   switch (type_of_cost)
30914     {
30915       case scalar_stmt:
30916         return ix86_cost->scalar_stmt_cost;
30917
30918       case scalar_load:
30919         return ix86_cost->scalar_load_cost;
30920
30921       case scalar_store:
30922         return ix86_cost->scalar_store_cost;
30923
30924       case vector_stmt:
30925         return ix86_cost->vec_stmt_cost;
30926
30927       case vector_load:
30928         return ix86_cost->vec_align_load_cost;
30929
30930       case vector_store:
30931         return ix86_cost->vec_store_cost;
30932
30933       case vec_to_scalar:
30934         return ix86_cost->vec_to_scalar_cost;
30935
30936       case scalar_to_vec:
30937         return ix86_cost->scalar_to_vec_cost;
30938
30939       case unaligned_load:
30940       case unaligned_store:
30941         return ix86_cost->vec_unalign_load_cost;
30942
30943       case cond_branch_taken:
30944         return ix86_cost->cond_taken_branch_cost;
30945
30946       case cond_branch_not_taken:
30947         return ix86_cost->cond_not_taken_branch_cost;
30948
30949       case vec_perm:
30950         return 1;
30951
30952       default:
30953         gcc_unreachable ();
30954     }
30955 }
30956
30957
30958 /* Implement targetm.vectorize.builtin_vec_perm.  */
30959
30960 static tree
30961 ix86_vectorize_builtin_vec_perm (tree vec_type, tree *mask_type)
30962 {
30963   tree itype = TREE_TYPE (vec_type);
30964   bool u = TYPE_UNSIGNED (itype);
30965   enum machine_mode vmode = TYPE_MODE (vec_type);
30966   enum ix86_builtins fcode;
30967   bool ok = TARGET_SSE2;
30968
30969   switch (vmode)
30970     {
30971     case V4DFmode:
30972       ok = TARGET_AVX;
30973       fcode = IX86_BUILTIN_VEC_PERM_V4DF;
30974       goto get_di;
30975     case V2DFmode:
30976       fcode = IX86_BUILTIN_VEC_PERM_V2DF;
30977     get_di:
30978       itype = ix86_get_builtin_type (IX86_BT_DI);
30979       break;
30980
30981     case V8SFmode:
30982       ok = TARGET_AVX;
30983       fcode = IX86_BUILTIN_VEC_PERM_V8SF;
30984       goto get_si;
30985     case V4SFmode:
30986       ok = TARGET_SSE;
30987       fcode = IX86_BUILTIN_VEC_PERM_V4SF;
30988     get_si:
30989       itype = ix86_get_builtin_type (IX86_BT_SI);
30990       break;
30991
30992     case V2DImode:
30993       fcode = u ? IX86_BUILTIN_VEC_PERM_V2DI_U : IX86_BUILTIN_VEC_PERM_V2DI;
30994       break;
30995     case V4SImode:
30996       fcode = u ? IX86_BUILTIN_VEC_PERM_V4SI_U : IX86_BUILTIN_VEC_PERM_V4SI;
30997       break;
30998     case V8HImode:
30999       fcode = u ? IX86_BUILTIN_VEC_PERM_V8HI_U : IX86_BUILTIN_VEC_PERM_V8HI;
31000       break;
31001     case V16QImode:
31002       fcode = u ? IX86_BUILTIN_VEC_PERM_V16QI_U : IX86_BUILTIN_VEC_PERM_V16QI;
31003       break;
31004     default:
31005       ok = false;
31006       break;
31007     }
31008
31009   if (!ok)
31010     return NULL_TREE;
31011
31012   *mask_type = itype;
31013   return ix86_builtins[(int) fcode];
31014 }
31015
31016 /* Return a vector mode with twice as many elements as VMODE.  */
31017 /* ??? Consider moving this to a table generated by genmodes.c.  */
31018
31019 static enum machine_mode
31020 doublesize_vector_mode (enum machine_mode vmode)
31021 {
31022   switch (vmode)
31023     {
31024     case V2SFmode:      return V4SFmode;
31025     case V1DImode:      return V2DImode;
31026     case V2SImode:      return V4SImode;
31027     case V4HImode:      return V8HImode;
31028     case V8QImode:      return V16QImode;
31029
31030     case V2DFmode:      return V4DFmode;
31031     case V4SFmode:      return V8SFmode;
31032     case V2DImode:      return V4DImode;
31033     case V4SImode:      return V8SImode;
31034     case V8HImode:      return V16HImode;
31035     case V16QImode:     return V32QImode;
31036
31037     case V4DFmode:      return V8DFmode;
31038     case V8SFmode:      return V16SFmode;
31039     case V4DImode:      return V8DImode;
31040     case V8SImode:      return V16SImode;
31041     case V16HImode:     return V32HImode;
31042     case V32QImode:     return V64QImode;
31043
31044     default:
31045       gcc_unreachable ();
31046     }
31047 }
31048
31049 /* Construct (set target (vec_select op0 (parallel perm))) and
31050    return true if that's a valid instruction in the active ISA.  */
31051
31052 static bool
31053 expand_vselect (rtx target, rtx op0, const unsigned char *perm, unsigned nelt)
31054 {
31055   rtx rperm[MAX_VECT_LEN], x;
31056   unsigned i;
31057
31058   for (i = 0; i < nelt; ++i)
31059     rperm[i] = GEN_INT (perm[i]);
31060
31061   x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (nelt, rperm));
31062   x = gen_rtx_VEC_SELECT (GET_MODE (target), op0, x);
31063   x = gen_rtx_SET (VOIDmode, target, x);
31064
31065   x = emit_insn (x);
31066   if (recog_memoized (x) < 0)
31067     {
31068       remove_insn (x);
31069       return false;
31070     }
31071   return true;
31072 }
31073
31074 /* Similar, but generate a vec_concat from op0 and op1 as well.  */
31075
31076 static bool
31077 expand_vselect_vconcat (rtx target, rtx op0, rtx op1,
31078                         const unsigned char *perm, unsigned nelt)
31079 {
31080   enum machine_mode v2mode;
31081   rtx x;
31082
31083   v2mode = doublesize_vector_mode (GET_MODE (op0));
31084   x = gen_rtx_VEC_CONCAT (v2mode, op0, op1);
31085   return expand_vselect (target, x, perm, nelt);
31086 }
31087
31088 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
31089    in terms of blendp[sd] / pblendw / pblendvb.  */
31090
31091 static bool
31092 expand_vec_perm_blend (struct expand_vec_perm_d *d)
31093 {
31094   enum machine_mode vmode = d->vmode;
31095   unsigned i, mask, nelt = d->nelt;
31096   rtx target, op0, op1, x;
31097
31098   if (!TARGET_SSE4_1 || d->op0 == d->op1)
31099     return false;
31100   if (!(GET_MODE_SIZE (vmode) == 16 || vmode == V4DFmode || vmode == V8SFmode))
31101     return false;
31102
31103   /* This is a blend, not a permute.  Elements must stay in their
31104      respective lanes.  */
31105   for (i = 0; i < nelt; ++i)
31106     {
31107       unsigned e = d->perm[i];
31108       if (!(e == i || e == i + nelt))
31109         return false;
31110     }
31111
31112   if (d->testing_p)
31113     return true;
31114
31115   /* ??? Without SSE4.1, we could implement this with and/andn/or.  This
31116      decision should be extracted elsewhere, so that we only try that
31117      sequence once all budget==3 options have been tried.  */
31118
31119   /* For bytes, see if bytes move in pairs so we can use pblendw with
31120      an immediate argument, rather than pblendvb with a vector argument.  */
31121   if (vmode == V16QImode)
31122     {
31123       bool pblendw_ok = true;
31124       for (i = 0; i < 16 && pblendw_ok; i += 2)
31125         pblendw_ok = (d->perm[i] + 1 == d->perm[i + 1]);
31126
31127       if (!pblendw_ok)
31128         {
31129           rtx rperm[16], vperm;
31130
31131           for (i = 0; i < nelt; ++i)
31132             rperm[i] = (d->perm[i] < nelt ? const0_rtx : constm1_rtx);
31133
31134           vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
31135           vperm = force_reg (V16QImode, vperm);
31136
31137           emit_insn (gen_sse4_1_pblendvb (d->target, d->op0, d->op1, vperm));
31138           return true;
31139         }
31140     }
31141
31142   target = d->target;
31143   op0 = d->op0;
31144   op1 = d->op1;
31145   mask = 0;
31146
31147   switch (vmode)
31148     {
31149     case V4DFmode:
31150     case V8SFmode:
31151     case V2DFmode:
31152     case V4SFmode:
31153     case V8HImode:
31154       for (i = 0; i < nelt; ++i)
31155         mask |= (d->perm[i] >= nelt) << i;
31156       break;
31157
31158     case V2DImode:
31159       for (i = 0; i < 2; ++i)
31160         mask |= (d->perm[i] >= 2 ? 15 : 0) << (i * 4);
31161       goto do_subreg;
31162
31163     case V4SImode:
31164       for (i = 0; i < 4; ++i)
31165         mask |= (d->perm[i] >= 4 ? 3 : 0) << (i * 2);
31166       goto do_subreg;
31167
31168     case V16QImode:
31169       for (i = 0; i < 8; ++i)
31170         mask |= (d->perm[i * 2] >= 16) << i;
31171
31172     do_subreg:
31173       vmode = V8HImode;
31174       target = gen_lowpart (vmode, target);
31175       op0 = gen_lowpart (vmode, op0);
31176       op1 = gen_lowpart (vmode, op1);
31177       break;
31178
31179     default:
31180       gcc_unreachable ();
31181     }
31182
31183   /* This matches five different patterns with the different modes.  */
31184   x = gen_rtx_VEC_MERGE (vmode, op1, op0, GEN_INT (mask));
31185   x = gen_rtx_SET (VOIDmode, target, x);
31186   emit_insn (x);
31187
31188   return true;
31189 }
31190
31191 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
31192    in terms of the variable form of vpermilps.
31193
31194    Note that we will have already failed the immediate input vpermilps,
31195    which requires that the high and low part shuffle be identical; the
31196    variable form doesn't require that.  */
31197
31198 static bool
31199 expand_vec_perm_vpermil (struct expand_vec_perm_d *d)
31200 {
31201   rtx rperm[8], vperm;
31202   unsigned i;
31203
31204   if (!TARGET_AVX || d->vmode != V8SFmode || d->op0 != d->op1)
31205     return false;
31206
31207   /* We can only permute within the 128-bit lane.  */
31208   for (i = 0; i < 8; ++i)
31209     {
31210       unsigned e = d->perm[i];
31211       if (i < 4 ? e >= 4 : e < 4)
31212         return false;
31213     }
31214
31215   if (d->testing_p)
31216     return true;
31217
31218   for (i = 0; i < 8; ++i)
31219     {
31220       unsigned e = d->perm[i];
31221
31222       /* Within each 128-bit lane, the elements of op0 are numbered
31223          from 0 and the elements of op1 are numbered from 4.  */
31224       if (e >= 8 + 4)
31225         e -= 8;
31226       else if (e >= 4)
31227         e -= 4;
31228
31229       rperm[i] = GEN_INT (e);
31230     }
31231
31232   vperm = gen_rtx_CONST_VECTOR (V8SImode, gen_rtvec_v (8, rperm));
31233   vperm = force_reg (V8SImode, vperm);
31234   emit_insn (gen_avx_vpermilvarv8sf3 (d->target, d->op0, vperm));
31235
31236   return true;
31237 }
31238
31239 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
31240    in terms of pshufb or vpperm.  */
31241
31242 static bool
31243 expand_vec_perm_pshufb (struct expand_vec_perm_d *d)
31244 {
31245   unsigned i, nelt, eltsz;
31246   rtx rperm[16], vperm, target, op0, op1;
31247
31248   if (!(d->op0 == d->op1 ? TARGET_SSSE3 : TARGET_XOP))
31249     return false;
31250   if (GET_MODE_SIZE (d->vmode) != 16)
31251     return false;
31252
31253   if (d->testing_p)
31254     return true;
31255
31256   nelt = d->nelt;
31257   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
31258
31259   for (i = 0; i < nelt; ++i)
31260     {
31261       unsigned j, e = d->perm[i];
31262       for (j = 0; j < eltsz; ++j)
31263         rperm[i * eltsz + j] = GEN_INT (e * eltsz + j);
31264     }
31265
31266   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm));
31267   vperm = force_reg (V16QImode, vperm);
31268
31269   target = gen_lowpart (V16QImode, d->target);
31270   op0 = gen_lowpart (V16QImode, d->op0);
31271   if (d->op0 == d->op1)
31272     emit_insn (gen_ssse3_pshufbv16qi3 (target, op0, vperm));
31273   else
31274     {
31275       op1 = gen_lowpart (V16QImode, d->op1);
31276       emit_insn (gen_xop_pperm (target, op0, op1, vperm));
31277     }
31278
31279   return true;
31280 }
31281
31282 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to instantiate D
31283    in a single instruction.  */
31284
31285 static bool
31286 expand_vec_perm_1 (struct expand_vec_perm_d *d)
31287 {
31288   unsigned i, nelt = d->nelt;
31289   unsigned char perm2[MAX_VECT_LEN];
31290
31291   /* Check plain VEC_SELECT first, because AVX has instructions that could
31292      match both SEL and SEL+CONCAT, but the plain SEL will allow a memory
31293      input where SEL+CONCAT may not.  */
31294   if (d->op0 == d->op1)
31295     {
31296       int mask = nelt - 1;
31297
31298       for (i = 0; i < nelt; i++)
31299         perm2[i] = d->perm[i] & mask;
31300
31301       if (expand_vselect (d->target, d->op0, perm2, nelt))
31302         return true;
31303
31304       /* There are plenty of patterns in sse.md that are written for
31305          SEL+CONCAT and are not replicated for a single op.  Perhaps
31306          that should be changed, to avoid the nastiness here.  */
31307
31308       /* Recognize interleave style patterns, which means incrementing
31309          every other permutation operand.  */
31310       for (i = 0; i < nelt; i += 2)
31311         {
31312           perm2[i] = d->perm[i] & mask;
31313           perm2[i + 1] = (d->perm[i + 1] & mask) + nelt;
31314         }
31315       if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
31316         return true;
31317
31318       /* Recognize shufps, which means adding {0, 0, nelt, nelt}.  */
31319       if (nelt >= 4)
31320         {
31321           for (i = 0; i < nelt; i += 4)
31322             {
31323               perm2[i + 0] = d->perm[i + 0] & mask;
31324               perm2[i + 1] = d->perm[i + 1] & mask;
31325               perm2[i + 2] = (d->perm[i + 2] & mask) + nelt;
31326               perm2[i + 3] = (d->perm[i + 3] & mask) + nelt;
31327             }
31328
31329           if (expand_vselect_vconcat (d->target, d->op0, d->op0, perm2, nelt))
31330             return true;
31331         }
31332     }
31333
31334   /* Finally, try the fully general two operand permute.  */
31335   if (expand_vselect_vconcat (d->target, d->op0, d->op1, d->perm, nelt))
31336     return true;
31337
31338   /* Recognize interleave style patterns with reversed operands.  */
31339   if (d->op0 != d->op1)
31340     {
31341       for (i = 0; i < nelt; ++i)
31342         {
31343           unsigned e = d->perm[i];
31344           if (e >= nelt)
31345             e -= nelt;
31346           else
31347             e += nelt;
31348           perm2[i] = e;
31349         }
31350
31351       if (expand_vselect_vconcat (d->target, d->op1, d->op0, perm2, nelt))
31352         return true;
31353     }
31354
31355   /* Try the SSE4.1 blend variable merge instructions.  */
31356   if (expand_vec_perm_blend (d))
31357     return true;
31358
31359   /* Try one of the AVX vpermil variable permutations.  */
31360   if (expand_vec_perm_vpermil (d))
31361     return true;
31362
31363   /* Try the SSSE3 pshufb or XOP vpperm variable permutation.  */
31364   if (expand_vec_perm_pshufb (d))
31365     return true;
31366
31367   return false;
31368 }
31369
31370 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to implement D
31371    in terms of a pair of pshuflw + pshufhw instructions.  */
31372
31373 static bool
31374 expand_vec_perm_pshuflw_pshufhw (struct expand_vec_perm_d *d)
31375 {
31376   unsigned char perm2[MAX_VECT_LEN];
31377   unsigned i;
31378   bool ok;
31379
31380   if (d->vmode != V8HImode || d->op0 != d->op1)
31381     return false;
31382
31383   /* The two permutations only operate in 64-bit lanes.  */
31384   for (i = 0; i < 4; ++i)
31385     if (d->perm[i] >= 4)
31386       return false;
31387   for (i = 4; i < 8; ++i)
31388     if (d->perm[i] < 4)
31389       return false;
31390
31391   if (d->testing_p)
31392     return true;
31393
31394   /* Emit the pshuflw.  */
31395   memcpy (perm2, d->perm, 4);
31396   for (i = 4; i < 8; ++i)
31397     perm2[i] = i;
31398   ok = expand_vselect (d->target, d->op0, perm2, 8);
31399   gcc_assert (ok);
31400
31401   /* Emit the pshufhw.  */
31402   memcpy (perm2 + 4, d->perm + 4, 4);
31403   for (i = 0; i < 4; ++i)
31404     perm2[i] = i;
31405   ok = expand_vselect (d->target, d->target, perm2, 8);
31406   gcc_assert (ok);
31407
31408   return true;
31409 }
31410
31411 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
31412    the permutation using the SSSE3 palignr instruction.  This succeeds
31413    when all of the elements in PERM fit within one vector and we merely
31414    need to shift them down so that a single vector permutation has a
31415    chance to succeed.  */
31416
31417 static bool
31418 expand_vec_perm_palignr (struct expand_vec_perm_d *d)
31419 {
31420   unsigned i, nelt = d->nelt;
31421   unsigned min, max;
31422   bool in_order, ok;
31423   rtx shift;
31424
31425   /* Even with AVX, palignr only operates on 128-bit vectors.  */
31426   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
31427     return false;
31428
31429   min = nelt, max = 0;
31430   for (i = 0; i < nelt; ++i)
31431     {
31432       unsigned e = d->perm[i];
31433       if (e < min)
31434         min = e;
31435       if (e > max)
31436         max = e;
31437     }
31438   if (min == 0 || max - min >= nelt)
31439     return false;
31440
31441   /* Given that we have SSSE3, we know we'll be able to implement the
31442      single operand permutation after the palignr with pshufb.  */
31443   if (d->testing_p)
31444     return true;
31445
31446   shift = GEN_INT (min * GET_MODE_BITSIZE (GET_MODE_INNER (d->vmode)));
31447   emit_insn (gen_ssse3_palignrti (gen_lowpart (TImode, d->target),
31448                                   gen_lowpart (TImode, d->op1),
31449                                   gen_lowpart (TImode, d->op0), shift));
31450
31451   d->op0 = d->op1 = d->target;
31452
31453   in_order = true;
31454   for (i = 0; i < nelt; ++i)
31455     {
31456       unsigned e = d->perm[i] - min;
31457       if (e != i)
31458         in_order = false;
31459       d->perm[i] = e;
31460     }
31461
31462   /* Test for the degenerate case where the alignment by itself
31463      produces the desired permutation.  */
31464   if (in_order)
31465     return true;
31466
31467   ok = expand_vec_perm_1 (d);
31468   gcc_assert (ok);
31469
31470   return ok;
31471 }
31472
31473 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Try to simplify
31474    a two vector permutation into a single vector permutation by using
31475    an interleave operation to merge the vectors.  */
31476
31477 static bool
31478 expand_vec_perm_interleave2 (struct expand_vec_perm_d *d)
31479 {
31480   struct expand_vec_perm_d dremap, dfinal;
31481   unsigned i, nelt = d->nelt, nelt2 = nelt / 2;
31482   unsigned contents, h1, h2, h3, h4;
31483   unsigned char remap[2 * MAX_VECT_LEN];
31484   rtx seq;
31485   bool ok;
31486
31487   if (d->op0 == d->op1)
31488     return false;
31489
31490   /* The 256-bit unpck[lh]p[sd] instructions only operate within the 128-bit
31491      lanes.  We can use similar techniques with the vperm2f128 instruction,
31492      but it requires slightly different logic.  */
31493   if (GET_MODE_SIZE (d->vmode) != 16)
31494     return false;
31495
31496   /* Examine from whence the elements come.  */
31497   contents = 0;
31498   for (i = 0; i < nelt; ++i)
31499     contents |= 1u << d->perm[i];
31500
31501   /* Split the two input vectors into 4 halves.  */
31502   h1 = (1u << nelt2) - 1;
31503   h2 = h1 << nelt2;
31504   h3 = h2 << nelt2;
31505   h4 = h3 << nelt2;
31506
31507   memset (remap, 0xff, sizeof (remap));
31508   dremap = *d;
31509
31510   /* If the elements from the low halves use interleave low, and similarly
31511      for interleave high.  If the elements are from mis-matched halves, we
31512      can use shufps for V4SF/V4SI or do a DImode shuffle.  */
31513   if ((contents & (h1 | h3)) == contents)
31514     {
31515       for (i = 0; i < nelt2; ++i)
31516         {
31517           remap[i] = i * 2;
31518           remap[i + nelt] = i * 2 + 1;
31519           dremap.perm[i * 2] = i;
31520           dremap.perm[i * 2 + 1] = i + nelt;
31521         }
31522     }
31523   else if ((contents & (h2 | h4)) == contents)
31524     {
31525       for (i = 0; i < nelt2; ++i)
31526         {
31527           remap[i + nelt2] = i * 2;
31528           remap[i + nelt + nelt2] = i * 2 + 1;
31529           dremap.perm[i * 2] = i + nelt2;
31530           dremap.perm[i * 2 + 1] = i + nelt + nelt2;
31531         }
31532     }
31533   else if ((contents & (h1 | h4)) == contents)
31534     {
31535       for (i = 0; i < nelt2; ++i)
31536         {
31537           remap[i] = i;
31538           remap[i + nelt + nelt2] = i + nelt2;
31539           dremap.perm[i] = i;
31540           dremap.perm[i + nelt2] = i + nelt + nelt2;
31541         }
31542       if (nelt != 4)
31543         {
31544           dremap.vmode = V2DImode;
31545           dremap.nelt = 2;
31546           dremap.perm[0] = 0;
31547           dremap.perm[1] = 3;
31548         }
31549     }
31550   else if ((contents & (h2 | h3)) == contents)
31551     {
31552       for (i = 0; i < nelt2; ++i)
31553         {
31554           remap[i + nelt2] = i;
31555           remap[i + nelt] = i + nelt2;
31556           dremap.perm[i] = i + nelt2;
31557           dremap.perm[i + nelt2] = i + nelt;
31558         }
31559       if (nelt != 4)
31560         {
31561           dremap.vmode = V2DImode;
31562           dremap.nelt = 2;
31563           dremap.perm[0] = 1;
31564           dremap.perm[1] = 2;
31565         }
31566     }
31567   else
31568     return false;
31569
31570   /* Use the remapping array set up above to move the elements from their
31571      swizzled locations into their final destinations.  */
31572   dfinal = *d;
31573   for (i = 0; i < nelt; ++i)
31574     {
31575       unsigned e = remap[d->perm[i]];
31576       gcc_assert (e < nelt);
31577       dfinal.perm[i] = e;
31578     }
31579   dfinal.op0 = gen_reg_rtx (dfinal.vmode);
31580   dfinal.op1 = dfinal.op0;
31581   dremap.target = dfinal.op0;
31582
31583   /* Test if the final remap can be done with a single insn.  For V4SFmode or
31584      V4SImode this *will* succeed.  For V8HImode or V16QImode it may not.  */
31585   start_sequence ();
31586   ok = expand_vec_perm_1 (&dfinal);
31587   seq = get_insns ();
31588   end_sequence ();
31589
31590   if (!ok)
31591     return false;
31592
31593   if (dremap.vmode != dfinal.vmode)
31594     {
31595       dremap.target = gen_lowpart (dremap.vmode, dremap.target);
31596       dremap.op0 = gen_lowpart (dremap.vmode, dremap.op0);
31597       dremap.op1 = gen_lowpart (dremap.vmode, dremap.op1);
31598     }
31599
31600   ok = expand_vec_perm_1 (&dremap);
31601   gcc_assert (ok);
31602
31603   emit_insn (seq);
31604   return true;
31605 }
31606
31607 /* A subroutine of expand_vec_perm_even_odd_1.  Implement the double-word
31608    permutation with two pshufb insns and an ior.  We should have already
31609    failed all two instruction sequences.  */
31610
31611 static bool
31612 expand_vec_perm_pshufb2 (struct expand_vec_perm_d *d)
31613 {
31614   rtx rperm[2][16], vperm, l, h, op, m128;
31615   unsigned int i, nelt, eltsz;
31616
31617   if (!TARGET_SSSE3 || GET_MODE_SIZE (d->vmode) != 16)
31618     return false;
31619   gcc_assert (d->op0 != d->op1);
31620
31621   nelt = d->nelt;
31622   eltsz = GET_MODE_SIZE (GET_MODE_INNER (d->vmode));
31623
31624   /* Generate two permutation masks.  If the required element is within
31625      the given vector it is shuffled into the proper lane.  If the required
31626      element is in the other vector, force a zero into the lane by setting
31627      bit 7 in the permutation mask.  */
31628   m128 = GEN_INT (-128);
31629   for (i = 0; i < nelt; ++i)
31630     {
31631       unsigned j, e = d->perm[i];
31632       unsigned which = (e >= nelt);
31633       if (e >= nelt)
31634         e -= nelt;
31635
31636       for (j = 0; j < eltsz; ++j)
31637         {
31638           rperm[which][i*eltsz + j] = GEN_INT (e*eltsz + j);
31639           rperm[1-which][i*eltsz + j] = m128;
31640         }
31641     }
31642
31643   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[0]));
31644   vperm = force_reg (V16QImode, vperm);
31645
31646   l = gen_reg_rtx (V16QImode);
31647   op = gen_lowpart (V16QImode, d->op0);
31648   emit_insn (gen_ssse3_pshufbv16qi3 (l, op, vperm));
31649
31650   vperm = gen_rtx_CONST_VECTOR (V16QImode, gen_rtvec_v (16, rperm[1]));
31651   vperm = force_reg (V16QImode, vperm);
31652
31653   h = gen_reg_rtx (V16QImode);
31654   op = gen_lowpart (V16QImode, d->op1);
31655   emit_insn (gen_ssse3_pshufbv16qi3 (h, op, vperm));
31656
31657   op = gen_lowpart (V16QImode, d->target);
31658   emit_insn (gen_iorv16qi3 (op, l, h));
31659
31660   return true;
31661 }
31662
31663 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement extract-even
31664    and extract-odd permutations.  */
31665
31666 static bool
31667 expand_vec_perm_even_odd_1 (struct expand_vec_perm_d *d, unsigned odd)
31668 {
31669   rtx t1, t2, t3;
31670
31671   switch (d->vmode)
31672     {
31673     case V4DFmode:
31674       t1 = gen_reg_rtx (V4DFmode);
31675       t2 = gen_reg_rtx (V4DFmode);
31676
31677       /* Shuffle the lanes around into { 0 1 4 5 } and { 2 3 6 7 }.  */
31678       emit_insn (gen_avx_vperm2f128v4df3 (t1, d->op0, d->op1, GEN_INT (0x20)));
31679       emit_insn (gen_avx_vperm2f128v4df3 (t2, d->op0, d->op1, GEN_INT (0x31)));
31680
31681       /* Now an unpck[lh]pd will produce the result required.  */
31682       if (odd)
31683         t3 = gen_avx_unpckhpd256 (d->target, t1, t2);
31684       else
31685         t3 = gen_avx_unpcklpd256 (d->target, t1, t2);
31686       emit_insn (t3);
31687       break;
31688
31689     case V8SFmode:
31690       {
31691         int mask = odd ? 0xdd : 0x88;
31692
31693         t1 = gen_reg_rtx (V8SFmode);
31694         t2 = gen_reg_rtx (V8SFmode);
31695         t3 = gen_reg_rtx (V8SFmode);
31696
31697         /* Shuffle within the 128-bit lanes to produce:
31698            { 0 2 8 a 4 6 c e } | { 1 3 9 b 5 7 d f }.  */
31699         emit_insn (gen_avx_shufps256 (t1, d->op0, d->op1,
31700                                       GEN_INT (mask)));
31701
31702         /* Shuffle the lanes around to produce:
31703            { 4 6 c e 0 2 8 a } and { 5 7 d f 1 3 9 b }.  */
31704         emit_insn (gen_avx_vperm2f128v8sf3 (t2, t1, t1,
31705                                             GEN_INT (0x3)));
31706
31707         /* Shuffle within the 128-bit lanes to produce:
31708            { 0 2 4 6 4 6 0 2 } | { 1 3 5 7 5 7 1 3 }.  */
31709         emit_insn (gen_avx_shufps256 (t3, t1, t2, GEN_INT (0x44)));
31710
31711         /* Shuffle within the 128-bit lanes to produce:
31712            { 8 a c e c e 8 a } | { 9 b d f d f 9 b }.  */
31713         emit_insn (gen_avx_shufps256 (t2, t1, t2, GEN_INT (0xee)));
31714
31715         /* Shuffle the lanes around to produce:
31716            { 0 2 4 6 8 a c e } | { 1 3 5 7 9 b d f }.  */
31717         emit_insn (gen_avx_vperm2f128v8sf3 (d->target, t3, t2,
31718                                             GEN_INT (0x20)));
31719       }
31720       break;
31721
31722     case V2DFmode:
31723     case V4SFmode:
31724     case V2DImode:
31725     case V4SImode:
31726       /* These are always directly implementable by expand_vec_perm_1.  */
31727       gcc_unreachable ();
31728
31729     case V8HImode:
31730       if (TARGET_SSSE3)
31731         return expand_vec_perm_pshufb2 (d);
31732       else
31733         {
31734           /* We need 2*log2(N)-1 operations to achieve odd/even
31735              with interleave. */
31736           t1 = gen_reg_rtx (V8HImode);
31737           t2 = gen_reg_rtx (V8HImode);
31738           emit_insn (gen_vec_interleave_highv8hi (t1, d->op0, d->op1));
31739           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->op0, d->op1));
31740           emit_insn (gen_vec_interleave_highv8hi (t2, d->target, t1));
31741           emit_insn (gen_vec_interleave_lowv8hi (d->target, d->target, t1));
31742           if (odd)
31743             t3 = gen_vec_interleave_highv8hi (d->target, d->target, t2);
31744           else
31745             t3 = gen_vec_interleave_lowv8hi (d->target, d->target, t2);
31746           emit_insn (t3);
31747         }
31748       break;
31749
31750     case V16QImode:
31751       if (TARGET_SSSE3)
31752         return expand_vec_perm_pshufb2 (d);
31753       else
31754         {
31755           t1 = gen_reg_rtx (V16QImode);
31756           t2 = gen_reg_rtx (V16QImode);
31757           t3 = gen_reg_rtx (V16QImode);
31758           emit_insn (gen_vec_interleave_highv16qi (t1, d->op0, d->op1));
31759           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->op0, d->op1));
31760           emit_insn (gen_vec_interleave_highv16qi (t2, d->target, t1));
31761           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t1));
31762           emit_insn (gen_vec_interleave_highv16qi (t3, d->target, t2));
31763           emit_insn (gen_vec_interleave_lowv16qi (d->target, d->target, t2));
31764           if (odd)
31765             t3 = gen_vec_interleave_highv16qi (d->target, d->target, t3);
31766           else
31767             t3 = gen_vec_interleave_lowv16qi (d->target, d->target, t3);
31768           emit_insn (t3);
31769         }
31770       break;
31771
31772     default:
31773       gcc_unreachable ();
31774     }
31775
31776   return true;
31777 }
31778
31779 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
31780    extract-even and extract-odd permutations.  */
31781
31782 static bool
31783 expand_vec_perm_even_odd (struct expand_vec_perm_d *d)
31784 {
31785   unsigned i, odd, nelt = d->nelt;
31786
31787   odd = d->perm[0];
31788   if (odd != 0 && odd != 1)
31789     return false;
31790
31791   for (i = 1; i < nelt; ++i)
31792     if (d->perm[i] != 2 * i + odd)
31793       return false;
31794
31795   return expand_vec_perm_even_odd_1 (d, odd);
31796 }
31797
31798 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Implement broadcast
31799    permutations.  We assume that expand_vec_perm_1 has already failed.  */
31800
31801 static bool
31802 expand_vec_perm_broadcast_1 (struct expand_vec_perm_d *d)
31803 {
31804   unsigned elt = d->perm[0], nelt2 = d->nelt / 2;
31805   enum machine_mode vmode = d->vmode;
31806   unsigned char perm2[4];
31807   rtx op0 = d->op0;
31808   bool ok;
31809
31810   switch (vmode)
31811     {
31812     case V4DFmode:
31813     case V8SFmode:
31814       /* These are special-cased in sse.md so that we can optionally
31815          use the vbroadcast instruction.  They expand to two insns
31816          if the input happens to be in a register.  */
31817       gcc_unreachable ();
31818
31819     case V2DFmode:
31820     case V2DImode:
31821     case V4SFmode:
31822     case V4SImode:
31823       /* These are always implementable using standard shuffle patterns.  */
31824       gcc_unreachable ();
31825
31826     case V8HImode:
31827     case V16QImode:
31828       /* These can be implemented via interleave.  We save one insn by
31829          stopping once we have promoted to V4SImode and then use pshufd.  */
31830       do
31831         {
31832           optab otab = vec_interleave_low_optab;
31833
31834           if (elt >= nelt2)
31835             {
31836               otab = vec_interleave_high_optab;
31837               elt -= nelt2;
31838             }
31839           nelt2 /= 2;
31840
31841           op0 = expand_binop (vmode, otab, op0, op0, NULL, 0, OPTAB_DIRECT);
31842           vmode = get_mode_wider_vector (vmode);
31843           op0 = gen_lowpart (vmode, op0);
31844         }
31845       while (vmode != V4SImode);
31846
31847       memset (perm2, elt, 4);
31848       ok = expand_vselect (gen_lowpart (V4SImode, d->target), op0, perm2, 4);
31849       gcc_assert (ok);
31850       return true;
31851
31852     default:
31853       gcc_unreachable ();
31854     }
31855 }
31856
31857 /* A subroutine of ix86_expand_vec_perm_builtin_1.  Pattern match
31858    broadcast permutations.  */
31859
31860 static bool
31861 expand_vec_perm_broadcast (struct expand_vec_perm_d *d)
31862 {
31863   unsigned i, elt, nelt = d->nelt;
31864
31865   if (d->op0 != d->op1)
31866     return false;
31867
31868   elt = d->perm[0];
31869   for (i = 1; i < nelt; ++i)
31870     if (d->perm[i] != elt)
31871       return false;
31872
31873   return expand_vec_perm_broadcast_1 (d);
31874 }
31875
31876 /* The guts of ix86_expand_vec_perm_builtin, also used by the ok hook.
31877    With all of the interface bits taken care of, perform the expansion
31878    in D and return true on success.  */
31879
31880 static bool
31881 ix86_expand_vec_perm_builtin_1 (struct expand_vec_perm_d *d)
31882 {
31883   /* Try a single instruction expansion.  */
31884   if (expand_vec_perm_1 (d))
31885     return true;
31886
31887   /* Try sequences of two instructions.  */
31888
31889   if (expand_vec_perm_pshuflw_pshufhw (d))
31890     return true;
31891
31892   if (expand_vec_perm_palignr (d))
31893     return true;
31894
31895   if (expand_vec_perm_interleave2 (d))
31896     return true;
31897
31898   if (expand_vec_perm_broadcast (d))
31899     return true;
31900
31901   /* Try sequences of three instructions.  */
31902
31903   if (expand_vec_perm_pshufb2 (d))
31904     return true;
31905
31906   /* ??? Look for narrow permutations whose element orderings would
31907      allow the promotion to a wider mode.  */
31908
31909   /* ??? Look for sequences of interleave or a wider permute that place
31910      the data into the correct lanes for a half-vector shuffle like
31911      pshuf[lh]w or vpermilps.  */
31912
31913   /* ??? Look for sequences of interleave that produce the desired results.
31914      The combinatorics of punpck[lh] get pretty ugly... */
31915
31916   if (expand_vec_perm_even_odd (d))
31917     return true;
31918
31919   return false;
31920 }
31921
31922 /* Extract the values from the vector CST into the permutation array in D.
31923    Return 0 on error, 1 if all values from the permutation come from the
31924    first vector, 2 if all values from the second vector, and 3 otherwise.  */
31925
31926 static int
31927 extract_vec_perm_cst (struct expand_vec_perm_d *d, tree cst)
31928 {
31929   tree list = TREE_VECTOR_CST_ELTS (cst);
31930   unsigned i, nelt = d->nelt;
31931   int ret = 0;
31932
31933   for (i = 0; i < nelt; ++i, list = TREE_CHAIN (list))
31934     {
31935       unsigned HOST_WIDE_INT e;
31936
31937       if (!host_integerp (TREE_VALUE (list), 1))
31938         return 0;
31939       e = tree_low_cst (TREE_VALUE (list), 1);
31940       if (e >= 2 * nelt)
31941         return 0;
31942
31943       ret |= (e < nelt ? 1 : 2);
31944       d->perm[i] = e;
31945     }
31946   gcc_assert (list == NULL);
31947
31948   /* For all elements from second vector, fold the elements to first.  */
31949   if (ret == 2)
31950     for (i = 0; i < nelt; ++i)
31951       d->perm[i] -= nelt;
31952
31953   return ret;
31954 }
31955
31956 static rtx
31957 ix86_expand_vec_perm_builtin (tree exp)
31958 {
31959   struct expand_vec_perm_d d;
31960   tree arg0, arg1, arg2;
31961
31962   arg0 = CALL_EXPR_ARG (exp, 0);
31963   arg1 = CALL_EXPR_ARG (exp, 1);
31964   arg2 = CALL_EXPR_ARG (exp, 2);
31965
31966   d.vmode = TYPE_MODE (TREE_TYPE (arg0));
31967   d.nelt = GET_MODE_NUNITS (d.vmode);
31968   d.testing_p = false;
31969   gcc_assert (VECTOR_MODE_P (d.vmode));
31970
31971   if (TREE_CODE (arg2) != VECTOR_CST)
31972     {
31973       error_at (EXPR_LOCATION (exp),
31974                 "vector permutation requires vector constant");
31975       goto exit_error;
31976     }
31977
31978   switch (extract_vec_perm_cst (&d, arg2))
31979     {
31980     default:
31981       gcc_unreachable();
31982
31983     case 0:
31984       error_at (EXPR_LOCATION (exp), "invalid vector permutation constant");
31985       goto exit_error;
31986
31987     case 3:
31988       if (!operand_equal_p (arg0, arg1, 0))
31989         {
31990           d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
31991           d.op0 = force_reg (d.vmode, d.op0);
31992           d.op1 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
31993           d.op1 = force_reg (d.vmode, d.op1);
31994           break;
31995         }
31996
31997       /* The elements of PERM do not suggest that only the first operand
31998          is used, but both operands are identical.  Allow easier matching
31999          of the permutation by folding the permutation into the single
32000          input vector.  */
32001       {
32002         unsigned i, nelt = d.nelt;
32003         for (i = 0; i < nelt; ++i)
32004           if (d.perm[i] >= nelt)
32005             d.perm[i] -= nelt;
32006       }
32007       /* FALLTHRU */
32008
32009     case 1:
32010       d.op0 = expand_expr (arg0, NULL_RTX, d.vmode, EXPAND_NORMAL);
32011       d.op0 = force_reg (d.vmode, d.op0);
32012       d.op1 = d.op0;
32013       break;
32014
32015     case 2:
32016       d.op0 = expand_expr (arg1, NULL_RTX, d.vmode, EXPAND_NORMAL);
32017       d.op0 = force_reg (d.vmode, d.op0);
32018       d.op1 = d.op0;
32019       break;
32020     }
32021
32022   d.target = gen_reg_rtx (d.vmode);
32023   if (ix86_expand_vec_perm_builtin_1 (&d))
32024     return d.target;
32025
32026   /* For compiler generated permutations, we should never got here, because
32027      the compiler should also be checking the ok hook.  But since this is a
32028      builtin the user has access too, so don't abort.  */
32029   switch (d.nelt)
32030     {
32031     case 2:
32032       sorry ("vector permutation (%d %d)", d.perm[0], d.perm[1]);
32033       break;
32034     case 4:
32035       sorry ("vector permutation (%d %d %d %d)",
32036              d.perm[0], d.perm[1], d.perm[2], d.perm[3]);
32037       break;
32038     case 8:
32039       sorry ("vector permutation (%d %d %d %d %d %d %d %d)",
32040              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
32041              d.perm[4], d.perm[5], d.perm[6], d.perm[7]);
32042       break;
32043     case 16:
32044       sorry ("vector permutation "
32045              "(%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d)",
32046              d.perm[0], d.perm[1], d.perm[2], d.perm[3],
32047              d.perm[4], d.perm[5], d.perm[6], d.perm[7],
32048              d.perm[8], d.perm[9], d.perm[10], d.perm[11],
32049              d.perm[12], d.perm[13], d.perm[14], d.perm[15]);
32050       break;
32051     default:
32052       gcc_unreachable ();
32053     }
32054  exit_error:
32055   return CONST0_RTX (d.vmode);
32056 }
32057
32058 /* Implement targetm.vectorize.builtin_vec_perm_ok.  */
32059
32060 static bool
32061 ix86_vectorize_builtin_vec_perm_ok (tree vec_type, tree mask)
32062 {
32063   struct expand_vec_perm_d d;
32064   int vec_mask;
32065   bool ret, one_vec;
32066
32067   d.vmode = TYPE_MODE (vec_type);
32068   d.nelt = GET_MODE_NUNITS (d.vmode);
32069   d.testing_p = true;
32070
32071   /* Given sufficient ISA support we can just return true here
32072      for selected vector modes.  */
32073   if (GET_MODE_SIZE (d.vmode) == 16)
32074     {
32075       /* All implementable with a single vpperm insn.  */
32076       if (TARGET_XOP)
32077         return true;
32078       /* All implementable with 2 pshufb + 1 ior.  */
32079       if (TARGET_SSSE3)
32080         return true;
32081       /* All implementable with shufpd or unpck[lh]pd.  */
32082       if (d.nelt == 2)
32083         return true;
32084     }
32085
32086   vec_mask = extract_vec_perm_cst (&d, mask);
32087
32088   /* This hook is cannot be called in response to something that the
32089      user does (unlike the builtin expander) so we shouldn't ever see
32090      an error generated from the extract.  */
32091   gcc_assert (vec_mask > 0 && vec_mask <= 3);
32092   one_vec = (vec_mask != 3);
32093
32094   /* Implementable with shufps or pshufd.  */
32095   if (one_vec && (d.vmode == V4SFmode || d.vmode == V4SImode))
32096     return true;
32097
32098   /* Otherwise we have to go through the motions and see if we can
32099      figure out how to generate the requested permutation.  */
32100   d.target = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 1);
32101   d.op1 = d.op0 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 2);
32102   if (!one_vec)
32103     d.op1 = gen_raw_REG (d.vmode, LAST_VIRTUAL_REGISTER + 3);
32104
32105   start_sequence ();
32106   ret = ix86_expand_vec_perm_builtin_1 (&d);
32107   end_sequence ();
32108
32109   return ret;
32110 }
32111
32112 void
32113 ix86_expand_vec_extract_even_odd (rtx targ, rtx op0, rtx op1, unsigned odd)
32114 {
32115   struct expand_vec_perm_d d;
32116   unsigned i, nelt;
32117
32118   d.target = targ;
32119   d.op0 = op0;
32120   d.op1 = op1;
32121   d.vmode = GET_MODE (targ);
32122   d.nelt = nelt = GET_MODE_NUNITS (d.vmode);
32123   d.testing_p = false;
32124
32125   for (i = 0; i < nelt; ++i)
32126     d.perm[i] = i * 2 + odd;
32127
32128   /* We'll either be able to implement the permutation directly...  */
32129   if (expand_vec_perm_1 (&d))
32130     return;
32131
32132   /* ... or we use the special-case patterns.  */
32133   expand_vec_perm_even_odd_1 (&d, odd);
32134 }
32135 \f
32136 /* This function returns the calling abi specific va_list type node.
32137    It returns  the FNDECL specific va_list type.  */
32138
32139 static tree
32140 ix86_fn_abi_va_list (tree fndecl)
32141 {
32142   if (!TARGET_64BIT)
32143     return va_list_type_node;
32144   gcc_assert (fndecl != NULL_TREE);
32145
32146   if (ix86_function_abi ((const_tree) fndecl) == MS_ABI)
32147     return ms_va_list_type_node;
32148   else
32149     return sysv_va_list_type_node;
32150 }
32151
32152 /* Returns the canonical va_list type specified by TYPE. If there
32153    is no valid TYPE provided, it return NULL_TREE.  */
32154
32155 static tree
32156 ix86_canonical_va_list_type (tree type)
32157 {
32158   tree wtype, htype;
32159
32160   /* Resolve references and pointers to va_list type.  */
32161   if (TREE_CODE (type) == MEM_REF)
32162     type = TREE_TYPE (type);
32163   else if (POINTER_TYPE_P (type) && POINTER_TYPE_P (TREE_TYPE(type)))
32164     type = TREE_TYPE (type);
32165   else if (POINTER_TYPE_P (type) && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
32166     type = TREE_TYPE (type);
32167
32168   if (TARGET_64BIT)
32169     {
32170       wtype = va_list_type_node;
32171           gcc_assert (wtype != NULL_TREE);
32172       htype = type;
32173       if (TREE_CODE (wtype) == ARRAY_TYPE)
32174         {
32175           /* If va_list is an array type, the argument may have decayed
32176              to a pointer type, e.g. by being passed to another function.
32177              In that case, unwrap both types so that we can compare the
32178              underlying records.  */
32179           if (TREE_CODE (htype) == ARRAY_TYPE
32180               || POINTER_TYPE_P (htype))
32181             {
32182               wtype = TREE_TYPE (wtype);
32183               htype = TREE_TYPE (htype);
32184             }
32185         }
32186       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
32187         return va_list_type_node;
32188       wtype = sysv_va_list_type_node;
32189           gcc_assert (wtype != NULL_TREE);
32190       htype = type;
32191       if (TREE_CODE (wtype) == ARRAY_TYPE)
32192         {
32193           /* If va_list is an array type, the argument may have decayed
32194              to a pointer type, e.g. by being passed to another function.
32195              In that case, unwrap both types so that we can compare the
32196              underlying records.  */
32197           if (TREE_CODE (htype) == ARRAY_TYPE
32198               || POINTER_TYPE_P (htype))
32199             {
32200               wtype = TREE_TYPE (wtype);
32201               htype = TREE_TYPE (htype);
32202             }
32203         }
32204       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
32205         return sysv_va_list_type_node;
32206       wtype = ms_va_list_type_node;
32207           gcc_assert (wtype != NULL_TREE);
32208       htype = type;
32209       if (TREE_CODE (wtype) == ARRAY_TYPE)
32210         {
32211           /* If va_list is an array type, the argument may have decayed
32212              to a pointer type, e.g. by being passed to another function.
32213              In that case, unwrap both types so that we can compare the
32214              underlying records.  */
32215           if (TREE_CODE (htype) == ARRAY_TYPE
32216               || POINTER_TYPE_P (htype))
32217             {
32218               wtype = TREE_TYPE (wtype);
32219               htype = TREE_TYPE (htype);
32220             }
32221         }
32222       if (TYPE_MAIN_VARIANT (wtype) == TYPE_MAIN_VARIANT (htype))
32223         return ms_va_list_type_node;
32224       return NULL_TREE;
32225     }
32226   return std_canonical_va_list_type (type);
32227 }
32228
32229 /* Iterate through the target-specific builtin types for va_list.
32230    IDX denotes the iterator, *PTREE is set to the result type of
32231    the va_list builtin, and *PNAME to its internal type.
32232    Returns zero if there is no element for this index, otherwise
32233    IDX should be increased upon the next call.
32234    Note, do not iterate a base builtin's name like __builtin_va_list.
32235    Used from c_common_nodes_and_builtins.  */
32236
32237 static int
32238 ix86_enum_va_list (int idx, const char **pname, tree *ptree)
32239 {
32240   if (TARGET_64BIT)
32241     {
32242       switch (idx)
32243         {
32244         default:
32245           break;
32246
32247         case 0:
32248           *ptree = ms_va_list_type_node;
32249           *pname = "__builtin_ms_va_list";
32250           return 1;
32251
32252         case 1:
32253           *ptree = sysv_va_list_type_node;
32254           *pname = "__builtin_sysv_va_list";
32255           return 1;
32256         }
32257     }
32258
32259   return 0;
32260 }
32261
32262 #undef TARGET_SCHED_DISPATCH
32263 #define TARGET_SCHED_DISPATCH has_dispatch
32264 #undef TARGET_SCHED_DISPATCH_DO
32265 #define TARGET_SCHED_DISPATCH_DO do_dispatch
32266
32267 /* The size of the dispatch window is the total number of bytes of
32268    object code allowed in a window.  */
32269 #define DISPATCH_WINDOW_SIZE 16
32270
32271 /* Number of dispatch windows considered for scheduling.  */
32272 #define MAX_DISPATCH_WINDOWS 3
32273
32274 /* Maximum number of instructions in a window.  */
32275 #define MAX_INSN 4
32276
32277 /* Maximum number of immediate operands in a window.  */
32278 #define MAX_IMM 4
32279
32280 /* Maximum number of immediate bits allowed in a window.  */
32281 #define MAX_IMM_SIZE 128
32282
32283 /* Maximum number of 32 bit immediates allowed in a window.  */
32284 #define MAX_IMM_32 4
32285
32286 /* Maximum number of 64 bit immediates allowed in a window.  */
32287 #define MAX_IMM_64 2
32288
32289 /* Maximum total of loads or prefetches allowed in a window.  */
32290 #define MAX_LOAD 2
32291
32292 /* Maximum total of stores allowed in a window.  */
32293 #define MAX_STORE 1
32294
32295 #undef BIG
32296 #define BIG 100
32297
32298
32299 /* Dispatch groups.  Istructions that affect the mix in a dispatch window.  */
32300 enum dispatch_group {
32301   disp_no_group = 0,
32302   disp_load,
32303   disp_store,
32304   disp_load_store,
32305   disp_prefetch,
32306   disp_imm,
32307   disp_imm_32,
32308   disp_imm_64,
32309   disp_branch,
32310   disp_cmp,
32311   disp_jcc,
32312   disp_last
32313 };
32314
32315 /* Number of allowable groups in a dispatch window.  It is an array
32316    indexed by dispatch_group enum.  100 is used as a big number,
32317    because the number of these kind of operations does not have any
32318    effect in dispatch window, but we need them for other reasons in
32319    the table.  */
32320 static unsigned int num_allowable_groups[disp_last] = {
32321   0, 2, 1, 1, 2, 4, 4, 2, 1, BIG, BIG
32322 };
32323
32324 char group_name[disp_last + 1][16] = {
32325   "disp_no_group", "disp_load", "disp_store", "disp_load_store",
32326   "disp_prefetch", "disp_imm", "disp_imm_32", "disp_imm_64",
32327   "disp_branch", "disp_cmp", "disp_jcc", "disp_last"
32328 };
32329
32330 /* Instruction path.  */
32331 enum insn_path {
32332   no_path = 0,
32333   path_single, /* Single micro op.  */
32334   path_double, /* Double micro op.  */
32335   path_multi,  /* Instructions with more than 2 micro op..  */
32336   last_path
32337 };
32338
32339 /* sched_insn_info defines a window to the instructions scheduled in
32340    the basic block.  It contains a pointer to the insn_info table and
32341    the instruction scheduled.
32342
32343    Windows are allocated for each basic block and are linked
32344    together.  */
32345 typedef struct sched_insn_info_s {
32346   rtx insn;
32347   enum dispatch_group group;
32348   enum insn_path path;
32349   int byte_len;
32350   int imm_bytes;
32351 } sched_insn_info;
32352
32353 /* Linked list of dispatch windows.  This is a two way list of
32354    dispatch windows of a basic block.  It contains information about
32355    the number of uops in the window and the total number of
32356    instructions and of bytes in the object code for this dispatch
32357    window.  */
32358 typedef struct dispatch_windows_s {
32359   int num_insn;            /* Number of insn in the window.  */
32360   int num_uops;            /* Number of uops in the window.  */
32361   int window_size;         /* Number of bytes in the window.  */
32362   int window_num;          /* Window number between 0 or 1.  */
32363   int num_imm;             /* Number of immediates in an insn.  */
32364   int num_imm_32;          /* Number of 32 bit immediates in an insn.  */
32365   int num_imm_64;          /* Number of 64 bit immediates in an insn.  */
32366   int imm_size;            /* Total immediates in the window.  */
32367   int num_loads;           /* Total memory loads in the window.  */
32368   int num_stores;          /* Total memory stores in the window.  */
32369   int violation;          /* Violation exists in window.  */
32370   sched_insn_info *window; /* Pointer to the window.  */
32371   struct dispatch_windows_s *next;
32372   struct dispatch_windows_s *prev;
32373 } dispatch_windows;
32374
32375 /* Immediate valuse used in an insn.  */
32376 typedef struct imm_info_s
32377   {
32378     int imm;
32379     int imm32;
32380     int imm64;
32381   } imm_info;
32382
32383 static dispatch_windows *dispatch_window_list;
32384 static dispatch_windows *dispatch_window_list1;
32385
32386 /* Get dispatch group of insn.  */
32387
32388 static enum dispatch_group
32389 get_mem_group (rtx insn)
32390 {
32391   enum attr_memory memory;
32392
32393   if (INSN_CODE (insn) < 0)
32394     return disp_no_group;
32395   memory = get_attr_memory (insn);
32396   if (memory == MEMORY_STORE)
32397     return disp_store;
32398
32399   if (memory == MEMORY_LOAD)
32400     return disp_load;
32401
32402   if (memory == MEMORY_BOTH)
32403     return disp_load_store;
32404
32405   return disp_no_group;
32406 }
32407
32408 /* Return true if insn is a compare instruction.  */
32409
32410 static bool
32411 is_cmp (rtx insn)
32412 {
32413   enum attr_type type;
32414
32415   type = get_attr_type (insn);
32416   return (type == TYPE_TEST
32417           || type == TYPE_ICMP
32418           || type == TYPE_FCMP
32419           || GET_CODE (PATTERN (insn)) == COMPARE);
32420 }
32421
32422 /* Return true if a dispatch violation encountered.  */
32423
32424 static bool
32425 dispatch_violation (void)
32426 {
32427   if (dispatch_window_list->next)
32428     return dispatch_window_list->next->violation;
32429   return dispatch_window_list->violation;
32430 }
32431
32432 /* Return true if insn is a branch instruction.  */
32433
32434 static bool
32435 is_branch (rtx insn)
32436 {
32437   return (CALL_P (insn) || JUMP_P (insn));
32438 }
32439
32440 /* Return true if insn is a prefetch instruction.  */
32441
32442 static bool
32443 is_prefetch (rtx insn)
32444 {
32445   return NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == PREFETCH;
32446 }
32447
32448 /* This function initializes a dispatch window and the list container holding a
32449    pointer to the window.  */
32450
32451 static void
32452 init_window (int window_num)
32453 {
32454   int i;
32455   dispatch_windows *new_list;
32456
32457   if (window_num == 0)
32458     new_list = dispatch_window_list;
32459   else
32460     new_list = dispatch_window_list1;
32461
32462   new_list->num_insn = 0;
32463   new_list->num_uops = 0;
32464   new_list->window_size = 0;
32465   new_list->next = NULL;
32466   new_list->prev = NULL;
32467   new_list->window_num = window_num;
32468   new_list->num_imm = 0;
32469   new_list->num_imm_32 = 0;
32470   new_list->num_imm_64 = 0;
32471   new_list->imm_size = 0;
32472   new_list->num_loads = 0;
32473   new_list->num_stores = 0;
32474   new_list->violation = false;
32475
32476   for (i = 0; i < MAX_INSN; i++)
32477     {
32478       new_list->window[i].insn = NULL;
32479       new_list->window[i].group = disp_no_group;
32480       new_list->window[i].path = no_path;
32481       new_list->window[i].byte_len = 0;
32482       new_list->window[i].imm_bytes = 0;
32483     }
32484   return;
32485 }
32486
32487 /* This function allocates and initializes a dispatch window and the
32488    list container holding a pointer to the window.  */
32489
32490 static dispatch_windows *
32491 allocate_window (void)
32492 {
32493   dispatch_windows *new_list = XNEW (struct dispatch_windows_s);
32494   new_list->window = XNEWVEC (struct sched_insn_info_s, MAX_INSN + 1);
32495
32496   return new_list;
32497 }
32498
32499 /* This routine initializes the dispatch scheduling information.  It
32500    initiates building dispatch scheduler tables and constructs the
32501    first dispatch window.  */
32502
32503 static void
32504 init_dispatch_sched (void)
32505 {
32506   /* Allocate a dispatch list and a window.  */
32507   dispatch_window_list = allocate_window ();
32508   dispatch_window_list1 = allocate_window ();
32509   init_window (0);
32510   init_window (1);
32511 }
32512
32513 /* This function returns true if a branch is detected.  End of a basic block
32514    does not have to be a branch, but here we assume only branches end a
32515    window.  */
32516
32517 static bool
32518 is_end_basic_block (enum dispatch_group group)
32519 {
32520   return group == disp_branch;
32521 }
32522
32523 /* This function is called when the end of a window processing is reached.  */
32524
32525 static void
32526 process_end_window (void)
32527 {
32528   gcc_assert (dispatch_window_list->num_insn <= MAX_INSN);
32529   if (dispatch_window_list->next)
32530     {
32531       gcc_assert (dispatch_window_list1->num_insn <= MAX_INSN);
32532       gcc_assert (dispatch_window_list->window_size
32533                   + dispatch_window_list1->window_size <= 48);
32534       init_window (1);
32535     }
32536   init_window (0);
32537 }
32538
32539 /* Allocates a new dispatch window and adds it to WINDOW_LIST.
32540    WINDOW_NUM is either 0 or 1.  A maximum of two windows are generated
32541    for 48 bytes of instructions.  Note that these windows are not dispatch
32542    windows that their sizes are DISPATCH_WINDOW_SIZE.  */
32543
32544 static dispatch_windows *
32545 allocate_next_window (int window_num)
32546 {
32547   if (window_num == 0)
32548     {
32549       if (dispatch_window_list->next)
32550           init_window (1);
32551       init_window (0);
32552       return dispatch_window_list;
32553     }
32554
32555   dispatch_window_list->next = dispatch_window_list1;
32556   dispatch_window_list1->prev = dispatch_window_list;
32557
32558   return dispatch_window_list1;
32559 }
32560
32561 /* Increment the number of immediate operands of an instruction.  */
32562
32563 static int
32564 find_constant_1 (rtx *in_rtx, imm_info *imm_values)
32565 {
32566   if (*in_rtx == 0)
32567     return 0;
32568
32569     switch ( GET_CODE (*in_rtx))
32570     {
32571     case CONST:
32572     case SYMBOL_REF:
32573     case CONST_INT:
32574       (imm_values->imm)++;
32575       if (x86_64_immediate_operand (*in_rtx, SImode))
32576         (imm_values->imm32)++;
32577       else
32578         (imm_values->imm64)++;
32579       break;
32580
32581     case CONST_DOUBLE:
32582       (imm_values->imm)++;
32583       (imm_values->imm64)++;
32584       break;
32585
32586     case CODE_LABEL:
32587       if (LABEL_KIND (*in_rtx) == LABEL_NORMAL)
32588         {
32589           (imm_values->imm)++;
32590           (imm_values->imm32)++;
32591         }
32592       break;
32593
32594     default:
32595       break;
32596     }
32597
32598   return 0;
32599 }
32600
32601 /* Compute number of immediate operands of an instruction.  */
32602
32603 static void
32604 find_constant (rtx in_rtx, imm_info *imm_values)
32605 {
32606   for_each_rtx (INSN_P (in_rtx) ? &PATTERN (in_rtx) : &in_rtx,
32607                 (rtx_function) find_constant_1, (void *) imm_values);
32608 }
32609
32610 /* Return total size of immediate operands of an instruction along with number
32611    of corresponding immediate-operands.  It initializes its parameters to zero
32612    befor calling FIND_CONSTANT.
32613    INSN is the input instruction.  IMM is the total of immediates.
32614    IMM32 is the number of 32 bit immediates.  IMM64 is the number of 64
32615    bit immediates.  */
32616
32617 static int
32618 get_num_immediates (rtx insn, int *imm, int *imm32, int *imm64)
32619 {
32620   imm_info imm_values = {0, 0, 0};
32621
32622   find_constant (insn, &imm_values);
32623   *imm = imm_values.imm;
32624   *imm32 = imm_values.imm32;
32625   *imm64 = imm_values.imm64;
32626   return imm_values.imm32 * 4 + imm_values.imm64 * 8;
32627 }
32628
32629 /* This function indicates if an operand of an instruction is an
32630    immediate.  */
32631
32632 static bool
32633 has_immediate (rtx insn)
32634 {
32635   int num_imm_operand;
32636   int num_imm32_operand;
32637   int num_imm64_operand;
32638
32639   if (insn)
32640     return get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
32641                                &num_imm64_operand);
32642   return false;
32643 }
32644
32645 /* Return single or double path for instructions.  */
32646
32647 static enum insn_path
32648 get_insn_path (rtx insn)
32649 {
32650   enum attr_amdfam10_decode path = get_attr_amdfam10_decode (insn);
32651
32652   if ((int)path == 0)
32653     return path_single;
32654
32655   if ((int)path == 1)
32656     return path_double;
32657
32658   return path_multi;
32659 }
32660
32661 /* Return insn dispatch group.  */
32662
32663 static enum dispatch_group
32664 get_insn_group (rtx insn)
32665 {
32666   enum dispatch_group group = get_mem_group (insn);
32667   if (group)
32668     return group;
32669
32670   if (is_branch (insn))
32671     return disp_branch;
32672
32673   if (is_cmp (insn))
32674     return disp_cmp;
32675
32676   if (has_immediate (insn))
32677     return disp_imm;
32678
32679   if (is_prefetch (insn))
32680     return disp_prefetch;
32681
32682   return disp_no_group;
32683 }
32684
32685 /* Count number of GROUP restricted instructions in a dispatch
32686    window WINDOW_LIST.  */
32687
32688 static int
32689 count_num_restricted (rtx insn, dispatch_windows *window_list)
32690 {
32691   enum dispatch_group group = get_insn_group (insn);
32692   int imm_size;
32693   int num_imm_operand;
32694   int num_imm32_operand;
32695   int num_imm64_operand;
32696
32697   if (group == disp_no_group)
32698     return 0;
32699
32700   if (group == disp_imm)
32701     {
32702       imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
32703                               &num_imm64_operand);
32704       if (window_list->imm_size + imm_size > MAX_IMM_SIZE
32705           || num_imm_operand + window_list->num_imm > MAX_IMM
32706           || (num_imm32_operand > 0
32707               && (window_list->num_imm_32 + num_imm32_operand > MAX_IMM_32
32708                   || window_list->num_imm_64 * 2 + num_imm32_operand > MAX_IMM_32))
32709           || (num_imm64_operand > 0
32710               && (window_list->num_imm_64 + num_imm64_operand > MAX_IMM_64
32711                   || window_list->num_imm_32 + num_imm64_operand * 2 > MAX_IMM_32))
32712           || (window_list->imm_size + imm_size == MAX_IMM_SIZE
32713               && num_imm64_operand > 0
32714               && ((window_list->num_imm_64 > 0
32715                    && window_list->num_insn >= 2)
32716                   || window_list->num_insn >= 3)))
32717         return BIG;
32718
32719       return 1;
32720     }
32721
32722   if ((group == disp_load_store
32723        && (window_list->num_loads >= MAX_LOAD
32724            || window_list->num_stores >= MAX_STORE))
32725       || ((group == disp_load
32726            || group == disp_prefetch)
32727           && window_list->num_loads >= MAX_LOAD)
32728       || (group == disp_store
32729           && window_list->num_stores >= MAX_STORE))
32730     return BIG;
32731
32732   return 1;
32733 }
32734
32735 /* This function returns true if insn satisfies dispatch rules on the
32736    last window scheduled.  */
32737
32738 static bool
32739 fits_dispatch_window (rtx insn)
32740 {
32741   dispatch_windows *window_list = dispatch_window_list;
32742   dispatch_windows *window_list_next = dispatch_window_list->next;
32743   unsigned int num_restrict;
32744   enum dispatch_group group = get_insn_group (insn);
32745   enum insn_path path = get_insn_path (insn);
32746   int sum;
32747
32748   /* Make disp_cmp and disp_jcc get scheduled at the latest.  These
32749      instructions should be given the lowest priority in the
32750      scheduling process in Haifa scheduler to make sure they will be
32751      scheduled in the same dispatch window as the refrence to them.  */
32752   if (group == disp_jcc || group == disp_cmp)
32753     return false;
32754
32755   /* Check nonrestricted.  */
32756   if (group == disp_no_group || group == disp_branch)
32757     return true;
32758
32759   /* Get last dispatch window.  */
32760   if (window_list_next)
32761     window_list = window_list_next;
32762
32763   if (window_list->window_num == 1)
32764     {
32765       sum = window_list->prev->window_size + window_list->window_size;
32766
32767       if (sum == 32
32768           || (min_insn_size (insn) + sum) >= 48)
32769         /* Window 1 is full.  Go for next window.  */
32770         return true;
32771     }
32772
32773   num_restrict = count_num_restricted (insn, window_list);
32774
32775   if (num_restrict > num_allowable_groups[group])
32776     return false;
32777
32778   /* See if it fits in the first window.  */
32779   if (window_list->window_num == 0)
32780     {
32781       /* The first widow should have only single and double path
32782          uops.  */
32783       if (path == path_double
32784           && (window_list->num_uops + 2) > MAX_INSN)
32785         return false;
32786       else if (path != path_single)
32787         return false;
32788     }
32789   return true;
32790 }
32791
32792 /* Add an instruction INSN with NUM_UOPS micro-operations to the
32793    dispatch window WINDOW_LIST.  */
32794
32795 static void
32796 add_insn_window (rtx insn, dispatch_windows *window_list, int num_uops)
32797 {
32798   int byte_len = min_insn_size (insn);
32799   int num_insn = window_list->num_insn;
32800   int imm_size;
32801   sched_insn_info *window = window_list->window;
32802   enum dispatch_group group = get_insn_group (insn);
32803   enum insn_path path = get_insn_path (insn);
32804   int num_imm_operand;
32805   int num_imm32_operand;
32806   int num_imm64_operand;
32807
32808   if (!window_list->violation && group != disp_cmp
32809       && !fits_dispatch_window (insn))
32810     window_list->violation = true;
32811
32812   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
32813                                  &num_imm64_operand);
32814
32815   /* Initialize window with new instruction.  */
32816   window[num_insn].insn = insn;
32817   window[num_insn].byte_len = byte_len;
32818   window[num_insn].group = group;
32819   window[num_insn].path = path;
32820   window[num_insn].imm_bytes = imm_size;
32821
32822   window_list->window_size += byte_len;
32823   window_list->num_insn = num_insn + 1;
32824   window_list->num_uops = window_list->num_uops + num_uops;
32825   window_list->imm_size += imm_size;
32826   window_list->num_imm += num_imm_operand;
32827   window_list->num_imm_32 += num_imm32_operand;
32828   window_list->num_imm_64 += num_imm64_operand;
32829
32830   if (group == disp_store)
32831     window_list->num_stores += 1;
32832   else if (group == disp_load
32833            || group == disp_prefetch)
32834     window_list->num_loads += 1;
32835   else if (group == disp_load_store)
32836     {
32837       window_list->num_stores += 1;
32838       window_list->num_loads += 1;
32839     }
32840 }
32841
32842 /* Adds a scheduled instruction, INSN, to the current dispatch window.
32843    If the total bytes of instructions or the number of instructions in
32844    the window exceed allowable, it allocates a new window.  */
32845
32846 static void
32847 add_to_dispatch_window (rtx insn)
32848 {
32849   int byte_len;
32850   dispatch_windows *window_list;
32851   dispatch_windows *next_list;
32852   dispatch_windows *window0_list;
32853   enum insn_path path;
32854   enum dispatch_group insn_group;
32855   bool insn_fits;
32856   int num_insn;
32857   int num_uops;
32858   int window_num;
32859   int insn_num_uops;
32860   int sum;
32861
32862   if (INSN_CODE (insn) < 0)
32863     return;
32864
32865   byte_len = min_insn_size (insn);
32866   window_list = dispatch_window_list;
32867   next_list = window_list->next;
32868   path = get_insn_path (insn);
32869   insn_group = get_insn_group (insn);
32870
32871   /* Get the last dispatch window.  */
32872   if (next_list)
32873       window_list = dispatch_window_list->next;
32874
32875   if (path == path_single)
32876     insn_num_uops = 1;
32877   else if (path == path_double)
32878     insn_num_uops = 2;
32879   else
32880     insn_num_uops = (int) path;
32881
32882   /* If current window is full, get a new window.
32883      Window number zero is full, if MAX_INSN uops are scheduled in it.
32884      Window number one is full, if window zero's bytes plus window
32885      one's bytes is 32, or if the bytes of the new instruction added
32886      to the total makes it greater than 48, or it has already MAX_INSN
32887      instructions in it.  */
32888   num_insn = window_list->num_insn;
32889   num_uops = window_list->num_uops;
32890   window_num = window_list->window_num;
32891   insn_fits = fits_dispatch_window (insn);
32892
32893   if (num_insn >= MAX_INSN
32894       || num_uops + insn_num_uops > MAX_INSN
32895       || !(insn_fits))
32896     {
32897       window_num = ~window_num & 1;
32898       window_list = allocate_next_window (window_num);
32899     }
32900
32901   if (window_num == 0)
32902     {
32903       add_insn_window (insn, window_list, insn_num_uops);
32904       if (window_list->num_insn >= MAX_INSN
32905           && insn_group == disp_branch)
32906         {
32907           process_end_window ();
32908           return;
32909         }
32910     }
32911   else if (window_num == 1)
32912     {
32913       window0_list = window_list->prev;
32914       sum = window0_list->window_size + window_list->window_size;
32915       if (sum == 32
32916           || (byte_len + sum) >= 48)
32917         {
32918           process_end_window ();
32919           window_list = dispatch_window_list;
32920         }
32921
32922       add_insn_window (insn, window_list, insn_num_uops);
32923     }
32924   else
32925     gcc_unreachable ();
32926
32927   if (is_end_basic_block (insn_group))
32928     {
32929       /* End of basic block is reached do end-basic-block process.  */
32930       process_end_window ();
32931       return;
32932     }
32933 }
32934
32935 /* Print the dispatch window, WINDOW_NUM, to FILE.  */
32936
32937 DEBUG_FUNCTION static void
32938 debug_dispatch_window_file (FILE *file, int window_num)
32939 {
32940   dispatch_windows *list;
32941   int i;
32942
32943   if (window_num == 0)
32944     list = dispatch_window_list;
32945   else
32946     list = dispatch_window_list1;
32947
32948   fprintf (file, "Window #%d:\n", list->window_num);
32949   fprintf (file, "  num_insn = %d, num_uops = %d, window_size = %d\n",
32950           list->num_insn, list->num_uops, list->window_size);
32951   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
32952            list->num_imm, list->num_imm_32, list->num_imm_64, list->imm_size);
32953
32954   fprintf (file, "  num_loads = %d, num_stores = %d\n", list->num_loads,
32955           list->num_stores);
32956   fprintf (file, " insn info:\n");
32957
32958   for (i = 0; i < MAX_INSN; i++)
32959     {
32960       if (!list->window[i].insn)
32961         break;
32962       fprintf (file, "    group[%d] = %s, insn[%d] = %p, path[%d] = %d byte_len[%d] = %d, imm_bytes[%d] = %d\n",
32963               i, group_name[list->window[i].group],
32964               i, (void *)list->window[i].insn,
32965               i, list->window[i].path,
32966               i, list->window[i].byte_len,
32967               i, list->window[i].imm_bytes);
32968     }
32969 }
32970
32971 /* Print to stdout a dispatch window.  */
32972
32973 DEBUG_FUNCTION void
32974 debug_dispatch_window (int window_num)
32975 {
32976   debug_dispatch_window_file (stdout, window_num);
32977 }
32978
32979 /* Print INSN dispatch information to FILE.  */
32980
32981 DEBUG_FUNCTION static void
32982 debug_insn_dispatch_info_file (FILE *file, rtx insn)
32983 {
32984   int byte_len;
32985   enum insn_path path;
32986   enum dispatch_group group;
32987   int imm_size;
32988   int num_imm_operand;
32989   int num_imm32_operand;
32990   int num_imm64_operand;
32991
32992   if (INSN_CODE (insn) < 0)
32993     return;
32994
32995   byte_len = min_insn_size (insn);
32996   path = get_insn_path (insn);
32997   group = get_insn_group (insn);
32998   imm_size = get_num_immediates (insn, &num_imm_operand, &num_imm32_operand,
32999                                  &num_imm64_operand);
33000
33001   fprintf (file, " insn info:\n");
33002   fprintf (file, "  group = %s, path = %d, byte_len = %d\n",
33003            group_name[group], path, byte_len);
33004   fprintf (file, "  num_imm = %d, num_imm_32 = %d, num_imm_64 = %d, imm_size = %d\n",
33005            num_imm_operand, num_imm32_operand, num_imm64_operand, imm_size);
33006 }
33007
33008 /* Print to STDERR the status of the ready list with respect to
33009    dispatch windows.  */
33010
33011 DEBUG_FUNCTION void
33012 debug_ready_dispatch (void)
33013 {
33014   int i;
33015   int no_ready = number_in_ready ();
33016
33017   fprintf (stdout, "Number of ready: %d\n", no_ready);
33018
33019   for (i = 0; i < no_ready; i++)
33020     debug_insn_dispatch_info_file (stdout, get_ready_element (i));
33021 }
33022
33023 /* This routine is the driver of the dispatch scheduler.  */
33024
33025 static void
33026 do_dispatch (rtx insn, int mode)
33027 {
33028   if (mode == DISPATCH_INIT)
33029     init_dispatch_sched ();
33030   else if (mode == ADD_TO_DISPATCH_WINDOW)
33031     add_to_dispatch_window (insn);
33032 }
33033
33034 /* Return TRUE if Dispatch Scheduling is supported.  */
33035
33036 static bool
33037 has_dispatch (rtx insn, int action)
33038 {
33039   if (ix86_tune == PROCESSOR_BDVER1 && flag_dispatch_scheduler)
33040     switch (action)
33041       {
33042       default:
33043         return false;
33044
33045       case IS_DISPATCH_ON:
33046         return true;
33047         break;
33048
33049       case IS_CMP:
33050         return is_cmp (insn);
33051
33052       case DISPATCH_VIOLATION:
33053         return dispatch_violation ();
33054
33055       case FITS_DISPATCH_WINDOW:
33056         return fits_dispatch_window (insn);
33057       }
33058
33059   return false;
33060 }
33061
33062 /* ??? No autovectorization into MMX or 3DNOW until we can reliably
33063    place emms and femms instructions.  */
33064
33065 static enum machine_mode
33066 ix86_preferred_simd_mode (enum machine_mode mode)
33067 {
33068   /* Disable double precision vectorizer if needed.  */
33069   if (mode == DFmode && !TARGET_VECTORIZE_DOUBLE)
33070     return word_mode;
33071
33072   if (!TARGET_AVX && !TARGET_SSE)
33073     return word_mode;
33074
33075   switch (mode)
33076     {
33077     case SFmode:
33078       return TARGET_AVX ? V8SFmode : V4SFmode;
33079     case DFmode:
33080       return TARGET_AVX ? V4DFmode : V2DFmode;
33081     case DImode:
33082       return V2DImode;
33083     case SImode:
33084       return V4SImode;
33085     case HImode:
33086       return V8HImode;
33087     case QImode:
33088       return V16QImode;
33089
33090     default:;
33091     }
33092
33093   return word_mode;
33094 }
33095
33096 /* If AVX is enabled then try vectorizing with both 256bit and 128bit
33097    vectors.  */
33098
33099 static unsigned int
33100 ix86_autovectorize_vector_sizes (void)
33101 {
33102   return TARGET_AVX ? 32 | 16 : 0;
33103 }
33104
33105 /* Initialize the GCC target structure.  */
33106 #undef TARGET_RETURN_IN_MEMORY
33107 #define TARGET_RETURN_IN_MEMORY ix86_return_in_memory
33108
33109 #undef TARGET_LEGITIMIZE_ADDRESS
33110 #define TARGET_LEGITIMIZE_ADDRESS ix86_legitimize_address
33111
33112 #undef TARGET_ATTRIBUTE_TABLE
33113 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
33114 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
33115 #  undef TARGET_MERGE_DECL_ATTRIBUTES
33116 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
33117 #endif
33118
33119 #undef TARGET_COMP_TYPE_ATTRIBUTES
33120 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
33121
33122 #undef TARGET_INIT_BUILTINS
33123 #define TARGET_INIT_BUILTINS ix86_init_builtins
33124 #undef TARGET_BUILTIN_DECL
33125 #define TARGET_BUILTIN_DECL ix86_builtin_decl
33126 #undef TARGET_EXPAND_BUILTIN
33127 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
33128
33129 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
33130 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
33131   ix86_builtin_vectorized_function
33132
33133 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
33134 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
33135
33136 #undef TARGET_BUILTIN_RECIPROCAL
33137 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
33138
33139 #undef TARGET_ASM_FUNCTION_EPILOGUE
33140 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
33141
33142 #undef TARGET_ENCODE_SECTION_INFO
33143 #ifndef SUBTARGET_ENCODE_SECTION_INFO
33144 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
33145 #else
33146 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
33147 #endif
33148
33149 #undef TARGET_ASM_OPEN_PAREN
33150 #define TARGET_ASM_OPEN_PAREN ""
33151 #undef TARGET_ASM_CLOSE_PAREN
33152 #define TARGET_ASM_CLOSE_PAREN ""
33153
33154 #undef TARGET_ASM_BYTE_OP
33155 #define TARGET_ASM_BYTE_OP ASM_BYTE
33156
33157 #undef TARGET_ASM_ALIGNED_HI_OP
33158 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
33159 #undef TARGET_ASM_ALIGNED_SI_OP
33160 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
33161 #ifdef ASM_QUAD
33162 #undef TARGET_ASM_ALIGNED_DI_OP
33163 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
33164 #endif
33165
33166 #undef TARGET_PROFILE_BEFORE_PROLOGUE
33167 #define TARGET_PROFILE_BEFORE_PROLOGUE ix86_profile_before_prologue
33168
33169 #undef TARGET_ASM_UNALIGNED_HI_OP
33170 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
33171 #undef TARGET_ASM_UNALIGNED_SI_OP
33172 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
33173 #undef TARGET_ASM_UNALIGNED_DI_OP
33174 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
33175
33176 #undef TARGET_PRINT_OPERAND
33177 #define TARGET_PRINT_OPERAND ix86_print_operand
33178 #undef TARGET_PRINT_OPERAND_ADDRESS
33179 #define TARGET_PRINT_OPERAND_ADDRESS ix86_print_operand_address
33180 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
33181 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P ix86_print_operand_punct_valid_p
33182 #undef TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA
33183 #define TARGET_ASM_OUTPUT_ADDR_CONST_EXTRA i386_asm_output_addr_const_extra 
33184
33185 #undef TARGET_SCHED_ADJUST_COST
33186 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
33187 #undef TARGET_SCHED_ISSUE_RATE
33188 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
33189 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
33190 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
33191   ia32_multipass_dfa_lookahead
33192
33193 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
33194 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
33195
33196 #ifdef HAVE_AS_TLS
33197 #undef TARGET_HAVE_TLS
33198 #define TARGET_HAVE_TLS true
33199 #endif
33200 #undef TARGET_CANNOT_FORCE_CONST_MEM
33201 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
33202 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
33203 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
33204
33205 #undef TARGET_DELEGITIMIZE_ADDRESS
33206 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
33207
33208 #undef TARGET_MS_BITFIELD_LAYOUT_P
33209 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
33210
33211 #if TARGET_MACHO
33212 #undef TARGET_BINDS_LOCAL_P
33213 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
33214 #endif
33215 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
33216 #undef TARGET_BINDS_LOCAL_P
33217 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
33218 #endif
33219
33220 #undef TARGET_ASM_OUTPUT_MI_THUNK
33221 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
33222 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
33223 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
33224
33225 #undef TARGET_ASM_FILE_START
33226 #define TARGET_ASM_FILE_START x86_file_start
33227
33228 #undef TARGET_DEFAULT_TARGET_FLAGS
33229 #define TARGET_DEFAULT_TARGET_FLAGS     \
33230   (TARGET_DEFAULT                       \
33231    | TARGET_SUBTARGET_DEFAULT           \
33232    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT \
33233    | MASK_FUSED_MADD)
33234
33235 #undef TARGET_HANDLE_OPTION
33236 #define TARGET_HANDLE_OPTION ix86_handle_option
33237
33238 #undef TARGET_OPTION_OVERRIDE
33239 #define TARGET_OPTION_OVERRIDE ix86_option_override
33240 #undef TARGET_OPTION_OPTIMIZATION
33241 #define TARGET_OPTION_OPTIMIZATION ix86_option_optimization
33242 #undef TARGET_OPTION_INIT_STRUCT
33243 #define TARGET_OPTION_INIT_STRUCT ix86_option_init_struct
33244
33245 #undef TARGET_REGISTER_MOVE_COST
33246 #define TARGET_REGISTER_MOVE_COST ix86_register_move_cost
33247 #undef TARGET_MEMORY_MOVE_COST
33248 #define TARGET_MEMORY_MOVE_COST ix86_memory_move_cost
33249 #undef TARGET_RTX_COSTS
33250 #define TARGET_RTX_COSTS ix86_rtx_costs
33251 #undef TARGET_ADDRESS_COST
33252 #define TARGET_ADDRESS_COST ix86_address_cost
33253
33254 #undef TARGET_FIXED_CONDITION_CODE_REGS
33255 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
33256 #undef TARGET_CC_MODES_COMPATIBLE
33257 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
33258
33259 #undef TARGET_MACHINE_DEPENDENT_REORG
33260 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
33261
33262 #undef TARGET_BUILTIN_SETJMP_FRAME_VALUE
33263 #define TARGET_BUILTIN_SETJMP_FRAME_VALUE ix86_builtin_setjmp_frame_value
33264
33265 #undef TARGET_BUILD_BUILTIN_VA_LIST
33266 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
33267
33268 #undef TARGET_ENUM_VA_LIST_P
33269 #define TARGET_ENUM_VA_LIST_P ix86_enum_va_list
33270
33271 #undef TARGET_FN_ABI_VA_LIST
33272 #define TARGET_FN_ABI_VA_LIST ix86_fn_abi_va_list
33273
33274 #undef TARGET_CANONICAL_VA_LIST_TYPE
33275 #define TARGET_CANONICAL_VA_LIST_TYPE ix86_canonical_va_list_type
33276
33277 #undef TARGET_EXPAND_BUILTIN_VA_START
33278 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
33279
33280 #undef TARGET_MD_ASM_CLOBBERS
33281 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
33282
33283 #undef TARGET_PROMOTE_PROTOTYPES
33284 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
33285 #undef TARGET_STRUCT_VALUE_RTX
33286 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
33287 #undef TARGET_SETUP_INCOMING_VARARGS
33288 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
33289 #undef TARGET_MUST_PASS_IN_STACK
33290 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
33291 #undef TARGET_FUNCTION_ARG_ADVANCE
33292 #define TARGET_FUNCTION_ARG_ADVANCE ix86_function_arg_advance
33293 #undef TARGET_FUNCTION_ARG
33294 #define TARGET_FUNCTION_ARG ix86_function_arg
33295 #undef TARGET_PASS_BY_REFERENCE
33296 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
33297 #undef TARGET_INTERNAL_ARG_POINTER
33298 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
33299 #undef TARGET_UPDATE_STACK_BOUNDARY
33300 #define TARGET_UPDATE_STACK_BOUNDARY ix86_update_stack_boundary
33301 #undef TARGET_GET_DRAP_RTX
33302 #define TARGET_GET_DRAP_RTX ix86_get_drap_rtx
33303 #undef TARGET_STRICT_ARGUMENT_NAMING
33304 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
33305 #undef TARGET_STATIC_CHAIN
33306 #define TARGET_STATIC_CHAIN ix86_static_chain
33307 #undef TARGET_TRAMPOLINE_INIT
33308 #define TARGET_TRAMPOLINE_INIT ix86_trampoline_init
33309 #undef TARGET_RETURN_POPS_ARGS
33310 #define TARGET_RETURN_POPS_ARGS ix86_return_pops_args
33311
33312 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
33313 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
33314
33315 #undef TARGET_SCALAR_MODE_SUPPORTED_P
33316 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
33317
33318 #undef TARGET_VECTOR_MODE_SUPPORTED_P
33319 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
33320
33321 #undef TARGET_C_MODE_FOR_SUFFIX
33322 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
33323
33324 #ifdef HAVE_AS_TLS
33325 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
33326 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
33327 #endif
33328
33329 #ifdef SUBTARGET_INSERT_ATTRIBUTES
33330 #undef TARGET_INSERT_ATTRIBUTES
33331 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
33332 #endif
33333
33334 #undef TARGET_MANGLE_TYPE
33335 #define TARGET_MANGLE_TYPE ix86_mangle_type
33336
33337 #undef TARGET_STACK_PROTECT_FAIL
33338 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
33339
33340 #undef TARGET_SUPPORTS_SPLIT_STACK
33341 #define TARGET_SUPPORTS_SPLIT_STACK ix86_supports_split_stack
33342
33343 #undef TARGET_FUNCTION_VALUE
33344 #define TARGET_FUNCTION_VALUE ix86_function_value
33345
33346 #undef TARGET_FUNCTION_VALUE_REGNO_P
33347 #define TARGET_FUNCTION_VALUE_REGNO_P ix86_function_value_regno_p
33348
33349 #undef TARGET_SECONDARY_RELOAD
33350 #define TARGET_SECONDARY_RELOAD ix86_secondary_reload
33351
33352 #undef TARGET_PREFERRED_RELOAD_CLASS
33353 #define TARGET_PREFERRED_RELOAD_CLASS ix86_preferred_reload_class
33354 #undef TARGET_PREFERRED_OUTPUT_RELOAD_CLASS
33355 #define TARGET_PREFERRED_OUTPUT_RELOAD_CLASS ix86_preferred_output_reload_class
33356 #undef TARGET_CLASS_LIKELY_SPILLED_P
33357 #define TARGET_CLASS_LIKELY_SPILLED_P ix86_class_likely_spilled_p
33358
33359 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
33360 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST \
33361   ix86_builtin_vectorization_cost
33362 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM
33363 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM \
33364   ix86_vectorize_builtin_vec_perm
33365 #undef TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK
33366 #define TARGET_VECTORIZE_BUILTIN_VEC_PERM_OK \
33367   ix86_vectorize_builtin_vec_perm_ok
33368 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
33369 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE \
33370   ix86_preferred_simd_mode
33371 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
33372 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES \
33373   ix86_autovectorize_vector_sizes
33374
33375 #undef TARGET_SET_CURRENT_FUNCTION
33376 #define TARGET_SET_CURRENT_FUNCTION ix86_set_current_function
33377
33378 #undef TARGET_OPTION_VALID_ATTRIBUTE_P
33379 #define TARGET_OPTION_VALID_ATTRIBUTE_P ix86_valid_target_attribute_p
33380
33381 #undef TARGET_OPTION_SAVE
33382 #define TARGET_OPTION_SAVE ix86_function_specific_save
33383
33384 #undef TARGET_OPTION_RESTORE
33385 #define TARGET_OPTION_RESTORE ix86_function_specific_restore
33386
33387 #undef TARGET_OPTION_PRINT
33388 #define TARGET_OPTION_PRINT ix86_function_specific_print
33389
33390 #undef TARGET_CAN_INLINE_P
33391 #define TARGET_CAN_INLINE_P ix86_can_inline_p
33392
33393 #undef TARGET_EXPAND_TO_RTL_HOOK
33394 #define TARGET_EXPAND_TO_RTL_HOOK ix86_maybe_switch_abi
33395
33396 #undef TARGET_LEGITIMATE_ADDRESS_P
33397 #define TARGET_LEGITIMATE_ADDRESS_P ix86_legitimate_address_p
33398
33399 #undef TARGET_IRA_COVER_CLASSES
33400 #define TARGET_IRA_COVER_CLASSES i386_ira_cover_classes
33401
33402 #undef TARGET_FRAME_POINTER_REQUIRED
33403 #define TARGET_FRAME_POINTER_REQUIRED ix86_frame_pointer_required
33404
33405 #undef TARGET_CAN_ELIMINATE
33406 #define TARGET_CAN_ELIMINATE ix86_can_eliminate
33407
33408 #undef TARGET_EXTRA_LIVE_ON_ENTRY
33409 #define TARGET_EXTRA_LIVE_ON_ENTRY ix86_live_on_entry
33410
33411 #undef TARGET_ASM_CODE_END
33412 #define TARGET_ASM_CODE_END ix86_code_end
33413
33414 struct gcc_target targetm = TARGET_INITIALIZER;
33415 \f
33416 #include "gt-i386.h"