OSDN Git Service

2008-02-19 H.J. Lu <hongjiu.lu@intel.com>
[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, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007, 2008
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "tree-gimple.h"
51 #include "dwarf2.h"
52 #include "df.h"
53 #include "tm-constrs.h"
54 #include "params.h"
55
56 static int x86_builtin_vectorization_cost (bool);
57
58 #ifndef CHECK_STACK_LIMIT
59 #define CHECK_STACK_LIMIT (-1)
60 #endif
61
62 /* Return index of given mode in mult and division cost tables.  */
63 #define MODE_INDEX(mode)                                        \
64   ((mode) == QImode ? 0                                         \
65    : (mode) == HImode ? 1                                       \
66    : (mode) == SImode ? 2                                       \
67    : (mode) == DImode ? 3                                       \
68    : 4)
69
70 /* Processor costs (relative to an add) */
71 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
72 #define COSTS_N_BYTES(N) ((N) * 2)
73
74 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
75
76 static const
77 struct processor_costs size_cost = {    /* costs for tuning for size */
78   COSTS_N_BYTES (2),                    /* cost of an add instruction */
79   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
80   COSTS_N_BYTES (2),                    /* variable shift costs */
81   COSTS_N_BYTES (3),                    /* constant shift costs */
82   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
83    COSTS_N_BYTES (3),                   /*                               HI */
84    COSTS_N_BYTES (3),                   /*                               SI */
85    COSTS_N_BYTES (3),                   /*                               DI */
86    COSTS_N_BYTES (5)},                  /*                            other */
87   0,                                    /* cost of multiply per each bit set */
88   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
89    COSTS_N_BYTES (3),                   /*                          HI */
90    COSTS_N_BYTES (3),                   /*                          SI */
91    COSTS_N_BYTES (3),                   /*                          DI */
92    COSTS_N_BYTES (5)},                  /*                       other */
93   COSTS_N_BYTES (3),                    /* cost of movsx */
94   COSTS_N_BYTES (3),                    /* cost of movzx */
95   0,                                    /* "large" insn */
96   2,                                    /* MOVE_RATIO */
97   2,                                    /* cost for loading QImode using movzbl */
98   {2, 2, 2},                            /* cost of loading integer registers
99                                            in QImode, HImode and SImode.
100                                            Relative to reg-reg move (2).  */
101   {2, 2, 2},                            /* cost of storing integer registers */
102   2,                                    /* cost of reg,reg fld/fst */
103   {2, 2, 2},                            /* cost of loading fp registers
104                                            in SFmode, DFmode and XFmode */
105   {2, 2, 2},                            /* cost of storing fp registers
106                                            in SFmode, DFmode and XFmode */
107   3,                                    /* cost of moving MMX register */
108   {3, 3},                               /* cost of loading MMX registers
109                                            in SImode and DImode */
110   {3, 3},                               /* cost of storing MMX registers
111                                            in SImode and DImode */
112   3,                                    /* cost of moving SSE register */
113   {3, 3, 3},                            /* cost of loading SSE registers
114                                            in SImode, DImode and TImode */
115   {3, 3, 3},                            /* cost of storing SSE registers
116                                            in SImode, DImode and TImode */
117   3,                                    /* MMX or SSE register to integer */
118   0,                                    /* size of l1 cache  */
119   0,                                    /* size of l2 cache  */
120   0,                                    /* size of prefetch block */
121   0,                                    /* number of parallel prefetches */
122   2,                                    /* Branch cost */
123   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
124   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
125   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
126   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
127   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
128   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
129   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
130    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
131   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
132    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
133   1,                                    /* scalar_stmt_cost.  */
134   1,                                    /* scalar load_cost.  */
135   1,                                    /* scalar_store_cost.  */
136   1,                                    /* vec_stmt_cost.  */
137   1,                                    /* vec_to_scalar_cost.  */
138   1,                                    /* scalar_to_vec_cost.  */
139   1,                                    /* vec_align_load_cost.  */
140   1,                                    /* vec_unalign_load_cost.  */
141   1,                                    /* vec_store_cost.  */
142   1,                                    /* cond_taken_branch_cost.  */
143   1,                                    /* cond_not_taken_branch_cost.  */
144 };
145
146 /* Processor costs (relative to an add) */
147 static const
148 struct processor_costs i386_cost = {    /* 386 specific costs */
149   COSTS_N_INSNS (1),                    /* cost of an add instruction */
150   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
151   COSTS_N_INSNS (3),                    /* variable shift costs */
152   COSTS_N_INSNS (2),                    /* constant shift costs */
153   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
154    COSTS_N_INSNS (6),                   /*                               HI */
155    COSTS_N_INSNS (6),                   /*                               SI */
156    COSTS_N_INSNS (6),                   /*                               DI */
157    COSTS_N_INSNS (6)},                  /*                               other */
158   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
159   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
160    COSTS_N_INSNS (23),                  /*                          HI */
161    COSTS_N_INSNS (23),                  /*                          SI */
162    COSTS_N_INSNS (23),                  /*                          DI */
163    COSTS_N_INSNS (23)},                 /*                          other */
164   COSTS_N_INSNS (3),                    /* cost of movsx */
165   COSTS_N_INSNS (2),                    /* cost of movzx */
166   15,                                   /* "large" insn */
167   3,                                    /* MOVE_RATIO */
168   4,                                    /* cost for loading QImode using movzbl */
169   {2, 4, 2},                            /* cost of loading integer registers
170                                            in QImode, HImode and SImode.
171                                            Relative to reg-reg move (2).  */
172   {2, 4, 2},                            /* cost of storing integer registers */
173   2,                                    /* cost of reg,reg fld/fst */
174   {8, 8, 8},                            /* cost of loading fp registers
175                                            in SFmode, DFmode and XFmode */
176   {8, 8, 8},                            /* cost of storing fp registers
177                                            in SFmode, DFmode and XFmode */
178   2,                                    /* cost of moving MMX register */
179   {4, 8},                               /* cost of loading MMX registers
180                                            in SImode and DImode */
181   {4, 8},                               /* cost of storing MMX registers
182                                            in SImode and DImode */
183   2,                                    /* cost of moving SSE register */
184   {4, 8, 16},                           /* cost of loading SSE registers
185                                            in SImode, DImode and TImode */
186   {4, 8, 16},                           /* cost of storing SSE registers
187                                            in SImode, DImode and TImode */
188   3,                                    /* MMX or SSE register to integer */
189   0,                                    /* size of l1 cache  */
190   0,                                    /* size of l2 cache  */
191   0,                                    /* size of prefetch block */
192   0,                                    /* number of parallel prefetches */
193   1,                                    /* Branch cost */
194   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
195   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
196   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
197   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
198   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
199   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
200   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
201    DUMMY_STRINGOP_ALGS},
202   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
203    DUMMY_STRINGOP_ALGS},
204   1,                                    /* scalar_stmt_cost.  */
205   1,                                    /* scalar load_cost.  */
206   1,                                    /* scalar_store_cost.  */
207   1,                                    /* vec_stmt_cost.  */
208   1,                                    /* vec_to_scalar_cost.  */
209   1,                                    /* scalar_to_vec_cost.  */
210   1,                                    /* vec_align_load_cost.  */
211   2,                                    /* vec_unalign_load_cost.  */
212   1,                                    /* vec_store_cost.  */
213   3,                                    /* cond_taken_branch_cost.  */
214   1,                                    /* cond_not_taken_branch_cost.  */
215 };
216
217 static const
218 struct processor_costs i486_cost = {    /* 486 specific costs */
219   COSTS_N_INSNS (1),                    /* cost of an add instruction */
220   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
221   COSTS_N_INSNS (3),                    /* variable shift costs */
222   COSTS_N_INSNS (2),                    /* constant shift costs */
223   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
224    COSTS_N_INSNS (12),                  /*                               HI */
225    COSTS_N_INSNS (12),                  /*                               SI */
226    COSTS_N_INSNS (12),                  /*                               DI */
227    COSTS_N_INSNS (12)},                 /*                               other */
228   1,                                    /* cost of multiply per each bit set */
229   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
230    COSTS_N_INSNS (40),                  /*                          HI */
231    COSTS_N_INSNS (40),                  /*                          SI */
232    COSTS_N_INSNS (40),                  /*                          DI */
233    COSTS_N_INSNS (40)},                 /*                          other */
234   COSTS_N_INSNS (3),                    /* cost of movsx */
235   COSTS_N_INSNS (2),                    /* cost of movzx */
236   15,                                   /* "large" insn */
237   3,                                    /* MOVE_RATIO */
238   4,                                    /* cost for loading QImode using movzbl */
239   {2, 4, 2},                            /* cost of loading integer registers
240                                            in QImode, HImode and SImode.
241                                            Relative to reg-reg move (2).  */
242   {2, 4, 2},                            /* cost of storing integer registers */
243   2,                                    /* cost of reg,reg fld/fst */
244   {8, 8, 8},                            /* cost of loading fp registers
245                                            in SFmode, DFmode and XFmode */
246   {8, 8, 8},                            /* cost of storing fp registers
247                                            in SFmode, DFmode and XFmode */
248   2,                                    /* cost of moving MMX register */
249   {4, 8},                               /* cost of loading MMX registers
250                                            in SImode and DImode */
251   {4, 8},                               /* cost of storing MMX registers
252                                            in SImode and DImode */
253   2,                                    /* cost of moving SSE register */
254   {4, 8, 16},                           /* cost of loading SSE registers
255                                            in SImode, DImode and TImode */
256   {4, 8, 16},                           /* cost of storing SSE registers
257                                            in SImode, DImode and TImode */
258   3,                                    /* MMX or SSE register to integer */
259   4,                                    /* size of l1 cache.  486 has 8kB cache
260                                            shared for code and data, so 4kB is
261                                            not really precise.  */
262   4,                                    /* size of l2 cache  */
263   0,                                    /* size of prefetch block */
264   0,                                    /* number of parallel prefetches */
265   1,                                    /* Branch cost */
266   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
267   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
268   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
269   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
270   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
271   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
272   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
273    DUMMY_STRINGOP_ALGS},
274   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
275    DUMMY_STRINGOP_ALGS},
276   1,                                    /* scalar_stmt_cost.  */
277   1,                                    /* scalar load_cost.  */
278   1,                                    /* scalar_store_cost.  */
279   1,                                    /* vec_stmt_cost.  */
280   1,                                    /* vec_to_scalar_cost.  */
281   1,                                    /* scalar_to_vec_cost.  */
282   1,                                    /* vec_align_load_cost.  */
283   2,                                    /* vec_unalign_load_cost.  */
284   1,                                    /* vec_store_cost.  */
285   3,                                    /* cond_taken_branch_cost.  */
286   1,                                    /* cond_not_taken_branch_cost.  */
287 };
288
289 static const
290 struct processor_costs pentium_cost = {
291   COSTS_N_INSNS (1),                    /* cost of an add instruction */
292   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
293   COSTS_N_INSNS (4),                    /* variable shift costs */
294   COSTS_N_INSNS (1),                    /* constant shift costs */
295   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
296    COSTS_N_INSNS (11),                  /*                               HI */
297    COSTS_N_INSNS (11),                  /*                               SI */
298    COSTS_N_INSNS (11),                  /*                               DI */
299    COSTS_N_INSNS (11)},                 /*                               other */
300   0,                                    /* cost of multiply per each bit set */
301   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
302    COSTS_N_INSNS (25),                  /*                          HI */
303    COSTS_N_INSNS (25),                  /*                          SI */
304    COSTS_N_INSNS (25),                  /*                          DI */
305    COSTS_N_INSNS (25)},                 /*                          other */
306   COSTS_N_INSNS (3),                    /* cost of movsx */
307   COSTS_N_INSNS (2),                    /* cost of movzx */
308   8,                                    /* "large" insn */
309   6,                                    /* MOVE_RATIO */
310   6,                                    /* cost for loading QImode using movzbl */
311   {2, 4, 2},                            /* cost of loading integer registers
312                                            in QImode, HImode and SImode.
313                                            Relative to reg-reg move (2).  */
314   {2, 4, 2},                            /* cost of storing integer registers */
315   2,                                    /* cost of reg,reg fld/fst */
316   {2, 2, 6},                            /* cost of loading fp registers
317                                            in SFmode, DFmode and XFmode */
318   {4, 4, 6},                            /* cost of storing fp registers
319                                            in SFmode, DFmode and XFmode */
320   8,                                    /* cost of moving MMX register */
321   {8, 8},                               /* cost of loading MMX registers
322                                            in SImode and DImode */
323   {8, 8},                               /* cost of storing MMX registers
324                                            in SImode and DImode */
325   2,                                    /* cost of moving SSE register */
326   {4, 8, 16},                           /* cost of loading SSE registers
327                                            in SImode, DImode and TImode */
328   {4, 8, 16},                           /* cost of storing SSE registers
329                                            in SImode, DImode and TImode */
330   3,                                    /* MMX or SSE register to integer */
331   8,                                    /* size of l1 cache.  */
332   8,                                    /* size of l2 cache  */
333   0,                                    /* size of prefetch block */
334   0,                                    /* number of parallel prefetches */
335   2,                                    /* Branch cost */
336   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
337   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
338   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
339   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
340   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
341   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
342   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
343    DUMMY_STRINGOP_ALGS},
344   {{libcall, {{-1, rep_prefix_4_byte}}},
345    DUMMY_STRINGOP_ALGS},
346   1,                                    /* scalar_stmt_cost.  */
347   1,                                    /* scalar load_cost.  */
348   1,                                    /* scalar_store_cost.  */
349   1,                                    /* vec_stmt_cost.  */
350   1,                                    /* vec_to_scalar_cost.  */
351   1,                                    /* scalar_to_vec_cost.  */
352   1,                                    /* vec_align_load_cost.  */
353   2,                                    /* vec_unalign_load_cost.  */
354   1,                                    /* vec_store_cost.  */
355   3,                                    /* cond_taken_branch_cost.  */
356   1,                                    /* cond_not_taken_branch_cost.  */
357 };
358
359 static const
360 struct processor_costs pentiumpro_cost = {
361   COSTS_N_INSNS (1),                    /* cost of an add instruction */
362   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
363   COSTS_N_INSNS (1),                    /* variable shift costs */
364   COSTS_N_INSNS (1),                    /* constant shift costs */
365   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
366    COSTS_N_INSNS (4),                   /*                               HI */
367    COSTS_N_INSNS (4),                   /*                               SI */
368    COSTS_N_INSNS (4),                   /*                               DI */
369    COSTS_N_INSNS (4)},                  /*                               other */
370   0,                                    /* cost of multiply per each bit set */
371   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
372    COSTS_N_INSNS (17),                  /*                          HI */
373    COSTS_N_INSNS (17),                  /*                          SI */
374    COSTS_N_INSNS (17),                  /*                          DI */
375    COSTS_N_INSNS (17)},                 /*                          other */
376   COSTS_N_INSNS (1),                    /* cost of movsx */
377   COSTS_N_INSNS (1),                    /* cost of movzx */
378   8,                                    /* "large" insn */
379   6,                                    /* MOVE_RATIO */
380   2,                                    /* cost for loading QImode using movzbl */
381   {4, 4, 4},                            /* cost of loading integer registers
382                                            in QImode, HImode and SImode.
383                                            Relative to reg-reg move (2).  */
384   {2, 2, 2},                            /* cost of storing integer registers */
385   2,                                    /* cost of reg,reg fld/fst */
386   {2, 2, 6},                            /* cost of loading fp registers
387                                            in SFmode, DFmode and XFmode */
388   {4, 4, 6},                            /* cost of storing fp registers
389                                            in SFmode, DFmode and XFmode */
390   2,                                    /* cost of moving MMX register */
391   {2, 2},                               /* cost of loading MMX registers
392                                            in SImode and DImode */
393   {2, 2},                               /* cost of storing MMX registers
394                                            in SImode and DImode */
395   2,                                    /* cost of moving SSE register */
396   {2, 2, 8},                            /* cost of loading SSE registers
397                                            in SImode, DImode and TImode */
398   {2, 2, 8},                            /* cost of storing SSE registers
399                                            in SImode, DImode and TImode */
400   3,                                    /* MMX or SSE register to integer */
401   8,                                    /* size of l1 cache.  */
402   256,                                  /* size of l2 cache  */
403   32,                                   /* size of prefetch block */
404   6,                                    /* number of parallel prefetches */
405   2,                                    /* Branch cost */
406   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
407   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
408   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
409   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
410   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
411   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
412   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes (we ensure
413      the alignment).  For small blocks inline loop is still a noticeable win, for bigger
414      blocks either rep movsl or rep movsb is way to go.  Rep movsb has apparently
415      more expensive startup time in CPU, but after 4K the difference is down in the noise.
416    */
417   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
418                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
419    DUMMY_STRINGOP_ALGS},
420   {{rep_prefix_4_byte, {{1024, unrolled_loop},
421                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
422    DUMMY_STRINGOP_ALGS},
423   1,                                    /* scalar_stmt_cost.  */
424   1,                                    /* scalar load_cost.  */
425   1,                                    /* scalar_store_cost.  */
426   1,                                    /* vec_stmt_cost.  */
427   1,                                    /* vec_to_scalar_cost.  */
428   1,                                    /* scalar_to_vec_cost.  */
429   1,                                    /* vec_align_load_cost.  */
430   2,                                    /* vec_unalign_load_cost.  */
431   1,                                    /* vec_store_cost.  */
432   3,                                    /* cond_taken_branch_cost.  */
433   1,                                    /* cond_not_taken_branch_cost.  */
434 };
435
436 static const
437 struct processor_costs geode_cost = {
438   COSTS_N_INSNS (1),                    /* cost of an add instruction */
439   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
440   COSTS_N_INSNS (2),                    /* variable shift costs */
441   COSTS_N_INSNS (1),                    /* constant shift costs */
442   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
443    COSTS_N_INSNS (4),                   /*                               HI */
444    COSTS_N_INSNS (7),                   /*                               SI */
445    COSTS_N_INSNS (7),                   /*                               DI */
446    COSTS_N_INSNS (7)},                  /*                               other */
447   0,                                    /* cost of multiply per each bit set */
448   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
449    COSTS_N_INSNS (23),                  /*                          HI */
450    COSTS_N_INSNS (39),                  /*                          SI */
451    COSTS_N_INSNS (39),                  /*                          DI */
452    COSTS_N_INSNS (39)},                 /*                          other */
453   COSTS_N_INSNS (1),                    /* cost of movsx */
454   COSTS_N_INSNS (1),                    /* cost of movzx */
455   8,                                    /* "large" insn */
456   4,                                    /* MOVE_RATIO */
457   1,                                    /* cost for loading QImode using movzbl */
458   {1, 1, 1},                            /* cost of loading integer registers
459                                            in QImode, HImode and SImode.
460                                            Relative to reg-reg move (2).  */
461   {1, 1, 1},                            /* cost of storing integer registers */
462   1,                                    /* cost of reg,reg fld/fst */
463   {1, 1, 1},                            /* cost of loading fp registers
464                                            in SFmode, DFmode and XFmode */
465   {4, 6, 6},                            /* cost of storing fp registers
466                                            in SFmode, DFmode and XFmode */
467
468   1,                                    /* cost of moving MMX register */
469   {1, 1},                               /* cost of loading MMX registers
470                                            in SImode and DImode */
471   {1, 1},                               /* cost of storing MMX registers
472                                            in SImode and DImode */
473   1,                                    /* cost of moving SSE register */
474   {1, 1, 1},                            /* cost of loading SSE registers
475                                            in SImode, DImode and TImode */
476   {1, 1, 1},                            /* cost of storing SSE registers
477                                            in SImode, DImode and TImode */
478   1,                                    /* MMX or SSE register to integer */
479   64,                                   /* size of l1 cache.  */
480   128,                                  /* size of l2 cache.  */
481   32,                                   /* size of prefetch block */
482   1,                                    /* number of parallel prefetches */
483   1,                                    /* Branch cost */
484   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
485   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
486   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
487   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
488   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
489   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
490   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
491    DUMMY_STRINGOP_ALGS},
492   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
493    DUMMY_STRINGOP_ALGS},
494   1,                                    /* scalar_stmt_cost.  */
495   1,                                    /* scalar load_cost.  */
496   1,                                    /* scalar_store_cost.  */
497   1,                                    /* vec_stmt_cost.  */
498   1,                                    /* vec_to_scalar_cost.  */
499   1,                                    /* scalar_to_vec_cost.  */
500   1,                                    /* vec_align_load_cost.  */
501   2,                                    /* vec_unalign_load_cost.  */
502   1,                                    /* vec_store_cost.  */
503   3,                                    /* cond_taken_branch_cost.  */
504   1,                                    /* cond_not_taken_branch_cost.  */
505 };
506
507 static const
508 struct processor_costs k6_cost = {
509   COSTS_N_INSNS (1),                    /* cost of an add instruction */
510   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
511   COSTS_N_INSNS (1),                    /* variable shift costs */
512   COSTS_N_INSNS (1),                    /* constant shift costs */
513   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
514    COSTS_N_INSNS (3),                   /*                               HI */
515    COSTS_N_INSNS (3),                   /*                               SI */
516    COSTS_N_INSNS (3),                   /*                               DI */
517    COSTS_N_INSNS (3)},                  /*                               other */
518   0,                                    /* cost of multiply per each bit set */
519   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
520    COSTS_N_INSNS (18),                  /*                          HI */
521    COSTS_N_INSNS (18),                  /*                          SI */
522    COSTS_N_INSNS (18),                  /*                          DI */
523    COSTS_N_INSNS (18)},                 /*                          other */
524   COSTS_N_INSNS (2),                    /* cost of movsx */
525   COSTS_N_INSNS (2),                    /* cost of movzx */
526   8,                                    /* "large" insn */
527   4,                                    /* MOVE_RATIO */
528   3,                                    /* cost for loading QImode using movzbl */
529   {4, 5, 4},                            /* cost of loading integer registers
530                                            in QImode, HImode and SImode.
531                                            Relative to reg-reg move (2).  */
532   {2, 3, 2},                            /* cost of storing integer registers */
533   4,                                    /* cost of reg,reg fld/fst */
534   {6, 6, 6},                            /* cost of loading fp registers
535                                            in SFmode, DFmode and XFmode */
536   {4, 4, 4},                            /* cost of storing fp registers
537                                            in SFmode, DFmode and XFmode */
538   2,                                    /* cost of moving MMX register */
539   {2, 2},                               /* cost of loading MMX registers
540                                            in SImode and DImode */
541   {2, 2},                               /* cost of storing MMX registers
542                                            in SImode and DImode */
543   2,                                    /* cost of moving SSE register */
544   {2, 2, 8},                            /* cost of loading SSE registers
545                                            in SImode, DImode and TImode */
546   {2, 2, 8},                            /* cost of storing SSE registers
547                                            in SImode, DImode and TImode */
548   6,                                    /* MMX or SSE register to integer */
549   32,                                   /* size of l1 cache.  */
550   32,                                   /* size of l2 cache.  Some models
551                                            have integrated l2 cache, but
552                                            optimizing for k6 is not important
553                                            enough to worry about that.  */
554   32,                                   /* size of prefetch block */
555   1,                                    /* number of parallel prefetches */
556   1,                                    /* Branch cost */
557   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
558   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
559   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
560   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
561   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
562   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
563   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
564    DUMMY_STRINGOP_ALGS},
565   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
566    DUMMY_STRINGOP_ALGS},
567   1,                                    /* scalar_stmt_cost.  */
568   1,                                    /* scalar load_cost.  */
569   1,                                    /* scalar_store_cost.  */
570   1,                                    /* vec_stmt_cost.  */
571   1,                                    /* vec_to_scalar_cost.  */
572   1,                                    /* scalar_to_vec_cost.  */
573   1,                                    /* vec_align_load_cost.  */
574   2,                                    /* vec_unalign_load_cost.  */
575   1,                                    /* vec_store_cost.  */
576   3,                                    /* cond_taken_branch_cost.  */
577   1,                                    /* cond_not_taken_branch_cost.  */
578 };
579
580 static const
581 struct processor_costs athlon_cost = {
582   COSTS_N_INSNS (1),                    /* cost of an add instruction */
583   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
584   COSTS_N_INSNS (1),                    /* variable shift costs */
585   COSTS_N_INSNS (1),                    /* constant shift costs */
586   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
587    COSTS_N_INSNS (5),                   /*                               HI */
588    COSTS_N_INSNS (5),                   /*                               SI */
589    COSTS_N_INSNS (5),                   /*                               DI */
590    COSTS_N_INSNS (5)},                  /*                               other */
591   0,                                    /* cost of multiply per each bit set */
592   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
593    COSTS_N_INSNS (26),                  /*                          HI */
594    COSTS_N_INSNS (42),                  /*                          SI */
595    COSTS_N_INSNS (74),                  /*                          DI */
596    COSTS_N_INSNS (74)},                 /*                          other */
597   COSTS_N_INSNS (1),                    /* cost of movsx */
598   COSTS_N_INSNS (1),                    /* cost of movzx */
599   8,                                    /* "large" insn */
600   9,                                    /* MOVE_RATIO */
601   4,                                    /* cost for loading QImode using movzbl */
602   {3, 4, 3},                            /* cost of loading integer registers
603                                            in QImode, HImode and SImode.
604                                            Relative to reg-reg move (2).  */
605   {3, 4, 3},                            /* cost of storing integer registers */
606   4,                                    /* cost of reg,reg fld/fst */
607   {4, 4, 12},                           /* cost of loading fp registers
608                                            in SFmode, DFmode and XFmode */
609   {6, 6, 8},                            /* cost of storing fp registers
610                                            in SFmode, DFmode and XFmode */
611   2,                                    /* cost of moving MMX register */
612   {4, 4},                               /* cost of loading MMX registers
613                                            in SImode and DImode */
614   {4, 4},                               /* cost of storing MMX registers
615                                            in SImode and DImode */
616   2,                                    /* cost of moving SSE register */
617   {4, 4, 6},                            /* cost of loading SSE registers
618                                            in SImode, DImode and TImode */
619   {4, 4, 5},                            /* cost of storing SSE registers
620                                            in SImode, DImode and TImode */
621   5,                                    /* MMX or SSE register to integer */
622   64,                                   /* size of l1 cache.  */
623   256,                                  /* size of l2 cache.  */
624   64,                                   /* size of prefetch block */
625   6,                                    /* number of parallel prefetches */
626   5,                                    /* Branch cost */
627   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
628   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
629   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
630   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
631   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
632   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
633   /* For some reason, Athlon deals better with REP prefix (relative to loops)
634      compared to K8. Alignment becomes important after 8 bytes for memcpy and
635      128 bytes for memset.  */
636   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
637    DUMMY_STRINGOP_ALGS},
638   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
639    DUMMY_STRINGOP_ALGS},
640   1,                                    /* scalar_stmt_cost.  */
641   1,                                    /* scalar load_cost.  */
642   1,                                    /* scalar_store_cost.  */
643   1,                                    /* vec_stmt_cost.  */
644   1,                                    /* vec_to_scalar_cost.  */
645   1,                                    /* scalar_to_vec_cost.  */
646   1,                                    /* vec_align_load_cost.  */
647   2,                                    /* vec_unalign_load_cost.  */
648   1,                                    /* vec_store_cost.  */
649   3,                                    /* cond_taken_branch_cost.  */
650   1,                                    /* cond_not_taken_branch_cost.  */
651 };
652
653 static const
654 struct processor_costs k8_cost = {
655   COSTS_N_INSNS (1),                    /* cost of an add instruction */
656   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
657   COSTS_N_INSNS (1),                    /* variable shift costs */
658   COSTS_N_INSNS (1),                    /* constant shift costs */
659   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
660    COSTS_N_INSNS (4),                   /*                               HI */
661    COSTS_N_INSNS (3),                   /*                               SI */
662    COSTS_N_INSNS (4),                   /*                               DI */
663    COSTS_N_INSNS (5)},                  /*                               other */
664   0,                                    /* cost of multiply per each bit set */
665   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
666    COSTS_N_INSNS (26),                  /*                          HI */
667    COSTS_N_INSNS (42),                  /*                          SI */
668    COSTS_N_INSNS (74),                  /*                          DI */
669    COSTS_N_INSNS (74)},                 /*                          other */
670   COSTS_N_INSNS (1),                    /* cost of movsx */
671   COSTS_N_INSNS (1),                    /* cost of movzx */
672   8,                                    /* "large" insn */
673   9,                                    /* MOVE_RATIO */
674   4,                                    /* cost for loading QImode using movzbl */
675   {3, 4, 3},                            /* cost of loading integer registers
676                                            in QImode, HImode and SImode.
677                                            Relative to reg-reg move (2).  */
678   {3, 4, 3},                            /* cost of storing integer registers */
679   4,                                    /* cost of reg,reg fld/fst */
680   {4, 4, 12},                           /* cost of loading fp registers
681                                            in SFmode, DFmode and XFmode */
682   {6, 6, 8},                            /* cost of storing fp registers
683                                            in SFmode, DFmode and XFmode */
684   2,                                    /* cost of moving MMX register */
685   {3, 3},                               /* cost of loading MMX registers
686                                            in SImode and DImode */
687   {4, 4},                               /* cost of storing MMX registers
688                                            in SImode and DImode */
689   2,                                    /* cost of moving SSE register */
690   {4, 3, 6},                            /* cost of loading SSE registers
691                                            in SImode, DImode and TImode */
692   {4, 4, 5},                            /* cost of storing SSE registers
693                                            in SImode, DImode and TImode */
694   5,                                    /* MMX or SSE register to integer */
695   64,                                   /* size of l1 cache.  */
696   512,                                  /* size of l2 cache.  */
697   64,                                   /* size of prefetch block */
698   /* New AMD processors never drop prefetches; if they cannot be performed
699      immediately, they are queued.  We set number of simultaneous prefetches
700      to a large constant to reflect this (it probably is not a good idea not
701      to limit number of prefetches at all, as their execution also takes some
702      time).  */
703   100,                                  /* number of parallel prefetches */
704   3,                                    /* Branch cost */
705   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
706   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
707   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
708   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
709   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
710   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
711   /* K8 has optimized REP instruction for medium sized blocks, but for very small
712      blocks it is better to use loop. For large blocks, libcall can do
713      nontemporary accesses and beat inline considerably.  */
714   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
715    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
716   {{libcall, {{8, loop}, {24, unrolled_loop},
717               {2048, rep_prefix_4_byte}, {-1, libcall}}},
718    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
719   4,                                    /* scalar_stmt_cost.  */
720   2,                                    /* scalar load_cost.  */
721   2,                                    /* scalar_store_cost.  */
722   5,                                    /* vec_stmt_cost.  */
723   0,                                    /* vec_to_scalar_cost.  */
724   2,                                    /* scalar_to_vec_cost.  */
725   2,                                    /* vec_align_load_cost.  */
726   3,                                    /* vec_unalign_load_cost.  */
727   3,                                    /* vec_store_cost.  */
728   3,                                    /* cond_taken_branch_cost.  */
729   2,                                    /* cond_not_taken_branch_cost.  */
730 };
731
732 struct processor_costs amdfam10_cost = {
733   COSTS_N_INSNS (1),                    /* cost of an add instruction */
734   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
735   COSTS_N_INSNS (1),                    /* variable shift costs */
736   COSTS_N_INSNS (1),                    /* constant shift costs */
737   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
738    COSTS_N_INSNS (4),                   /*                               HI */
739    COSTS_N_INSNS (3),                   /*                               SI */
740    COSTS_N_INSNS (4),                   /*                               DI */
741    COSTS_N_INSNS (5)},                  /*                               other */
742   0,                                    /* cost of multiply per each bit set */
743   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
744    COSTS_N_INSNS (35),                  /*                          HI */
745    COSTS_N_INSNS (51),                  /*                          SI */
746    COSTS_N_INSNS (83),                  /*                          DI */
747    COSTS_N_INSNS (83)},                 /*                          other */
748   COSTS_N_INSNS (1),                    /* cost of movsx */
749   COSTS_N_INSNS (1),                    /* cost of movzx */
750   8,                                    /* "large" insn */
751   9,                                    /* MOVE_RATIO */
752   4,                                    /* cost for loading QImode using movzbl */
753   {3, 4, 3},                            /* cost of loading integer registers
754                                            in QImode, HImode and SImode.
755                                            Relative to reg-reg move (2).  */
756   {3, 4, 3},                            /* cost of storing integer registers */
757   4,                                    /* cost of reg,reg fld/fst */
758   {4, 4, 12},                           /* cost of loading fp registers
759                                            in SFmode, DFmode and XFmode */
760   {6, 6, 8},                            /* cost of storing fp registers
761                                            in SFmode, DFmode and XFmode */
762   2,                                    /* cost of moving MMX register */
763   {3, 3},                               /* cost of loading MMX registers
764                                            in SImode and DImode */
765   {4, 4},                               /* cost of storing MMX registers
766                                            in SImode and DImode */
767   2,                                    /* cost of moving SSE register */
768   {4, 4, 3},                            /* cost of loading SSE registers
769                                            in SImode, DImode and TImode */
770   {4, 4, 5},                            /* cost of storing SSE registers
771                                            in SImode, DImode and TImode */
772   3,                                    /* MMX or SSE register to integer */
773                                         /* On K8
774                                             MOVD reg64, xmmreg  Double  FSTORE 4
775                                             MOVD reg32, xmmreg  Double  FSTORE 4
776                                            On AMDFAM10
777                                             MOVD reg64, xmmreg  Double  FADD 3
778                                                                 1/1  1/1
779                                             MOVD reg32, xmmreg  Double  FADD 3
780                                                                 1/1  1/1 */
781   64,                                   /* size of l1 cache.  */
782   512,                                  /* size of l2 cache.  */
783   64,                                   /* size of prefetch block */
784   /* New AMD processors never drop prefetches; if they cannot be performed
785      immediately, they are queued.  We set number of simultaneous prefetches
786      to a large constant to reflect this (it probably is not a good idea not
787      to limit number of prefetches at all, as their execution also takes some
788      time).  */
789   100,                                  /* number of parallel prefetches */
790   2,                                    /* Branch cost */
791   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
792   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
793   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
794   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
795   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
796   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
797
798   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
799      very small blocks it is better to use loop. For large blocks, libcall can
800      do nontemporary accesses and beat inline considerably.  */
801   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
802    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
803   {{libcall, {{8, loop}, {24, unrolled_loop},
804               {2048, rep_prefix_4_byte}, {-1, libcall}}},
805    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
806   4,                                    /* scalar_stmt_cost.  */
807   2,                                    /* scalar load_cost.  */
808   2,                                    /* scalar_store_cost.  */
809   6,                                    /* vec_stmt_cost.  */
810   0,                                    /* vec_to_scalar_cost.  */
811   2,                                    /* scalar_to_vec_cost.  */
812   2,                                    /* vec_align_load_cost.  */
813   2,                                    /* vec_unalign_load_cost.  */
814   2,                                    /* vec_store_cost.  */
815   2,                                    /* cond_taken_branch_cost.  */
816   1,                                    /* cond_not_taken_branch_cost.  */
817 };
818
819 static const
820 struct processor_costs pentium4_cost = {
821   COSTS_N_INSNS (1),                    /* cost of an add instruction */
822   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
823   COSTS_N_INSNS (4),                    /* variable shift costs */
824   COSTS_N_INSNS (4),                    /* constant shift costs */
825   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
826    COSTS_N_INSNS (15),                  /*                               HI */
827    COSTS_N_INSNS (15),                  /*                               SI */
828    COSTS_N_INSNS (15),                  /*                               DI */
829    COSTS_N_INSNS (15)},                 /*                               other */
830   0,                                    /* cost of multiply per each bit set */
831   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
832    COSTS_N_INSNS (56),                  /*                          HI */
833    COSTS_N_INSNS (56),                  /*                          SI */
834    COSTS_N_INSNS (56),                  /*                          DI */
835    COSTS_N_INSNS (56)},                 /*                          other */
836   COSTS_N_INSNS (1),                    /* cost of movsx */
837   COSTS_N_INSNS (1),                    /* cost of movzx */
838   16,                                   /* "large" insn */
839   6,                                    /* MOVE_RATIO */
840   2,                                    /* cost for loading QImode using movzbl */
841   {4, 5, 4},                            /* cost of loading integer registers
842                                            in QImode, HImode and SImode.
843                                            Relative to reg-reg move (2).  */
844   {2, 3, 2},                            /* cost of storing integer registers */
845   2,                                    /* cost of reg,reg fld/fst */
846   {2, 2, 6},                            /* cost of loading fp registers
847                                            in SFmode, DFmode and XFmode */
848   {4, 4, 6},                            /* cost of storing fp registers
849                                            in SFmode, DFmode and XFmode */
850   2,                                    /* cost of moving MMX register */
851   {2, 2},                               /* cost of loading MMX registers
852                                            in SImode and DImode */
853   {2, 2},                               /* cost of storing MMX registers
854                                            in SImode and DImode */
855   12,                                   /* cost of moving SSE register */
856   {12, 12, 12},                         /* cost of loading SSE registers
857                                            in SImode, DImode and TImode */
858   {2, 2, 8},                            /* cost of storing SSE registers
859                                            in SImode, DImode and TImode */
860   10,                                   /* MMX or SSE register to integer */
861   8,                                    /* size of l1 cache.  */
862   256,                                  /* size of l2 cache.  */
863   64,                                   /* size of prefetch block */
864   6,                                    /* number of parallel prefetches */
865   2,                                    /* Branch cost */
866   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
867   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
868   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
869   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
870   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
871   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
872   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
873    DUMMY_STRINGOP_ALGS},
874   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
875    {-1, libcall}}},
876    DUMMY_STRINGOP_ALGS},
877   1,                                    /* scalar_stmt_cost.  */
878   1,                                    /* scalar load_cost.  */
879   1,                                    /* scalar_store_cost.  */
880   1,                                    /* vec_stmt_cost.  */
881   1,                                    /* vec_to_scalar_cost.  */
882   1,                                    /* scalar_to_vec_cost.  */
883   1,                                    /* vec_align_load_cost.  */
884   2,                                    /* vec_unalign_load_cost.  */
885   1,                                    /* vec_store_cost.  */
886   3,                                    /* cond_taken_branch_cost.  */
887   1,                                    /* cond_not_taken_branch_cost.  */
888 };
889
890 static const
891 struct processor_costs nocona_cost = {
892   COSTS_N_INSNS (1),                    /* cost of an add instruction */
893   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
894   COSTS_N_INSNS (1),                    /* variable shift costs */
895   COSTS_N_INSNS (1),                    /* constant shift costs */
896   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
897    COSTS_N_INSNS (10),                  /*                               HI */
898    COSTS_N_INSNS (10),                  /*                               SI */
899    COSTS_N_INSNS (10),                  /*                               DI */
900    COSTS_N_INSNS (10)},                 /*                               other */
901   0,                                    /* cost of multiply per each bit set */
902   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
903    COSTS_N_INSNS (66),                  /*                          HI */
904    COSTS_N_INSNS (66),                  /*                          SI */
905    COSTS_N_INSNS (66),                  /*                          DI */
906    COSTS_N_INSNS (66)},                 /*                          other */
907   COSTS_N_INSNS (1),                    /* cost of movsx */
908   COSTS_N_INSNS (1),                    /* cost of movzx */
909   16,                                   /* "large" insn */
910   17,                                   /* MOVE_RATIO */
911   4,                                    /* cost for loading QImode using movzbl */
912   {4, 4, 4},                            /* cost of loading integer registers
913                                            in QImode, HImode and SImode.
914                                            Relative to reg-reg move (2).  */
915   {4, 4, 4},                            /* cost of storing integer registers */
916   3,                                    /* cost of reg,reg fld/fst */
917   {12, 12, 12},                         /* cost of loading fp registers
918                                            in SFmode, DFmode and XFmode */
919   {4, 4, 4},                            /* cost of storing fp registers
920                                            in SFmode, DFmode and XFmode */
921   6,                                    /* cost of moving MMX register */
922   {12, 12},                             /* cost of loading MMX registers
923                                            in SImode and DImode */
924   {12, 12},                             /* cost of storing MMX registers
925                                            in SImode and DImode */
926   6,                                    /* cost of moving SSE register */
927   {12, 12, 12},                         /* cost of loading SSE registers
928                                            in SImode, DImode and TImode */
929   {12, 12, 12},                         /* cost of storing SSE registers
930                                            in SImode, DImode and TImode */
931   8,                                    /* MMX or SSE register to integer */
932   8,                                    /* size of l1 cache.  */
933   1024,                                 /* size of l2 cache.  */
934   128,                                  /* size of prefetch block */
935   8,                                    /* number of parallel prefetches */
936   1,                                    /* Branch cost */
937   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
938   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
939   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
940   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
941   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
942   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
943   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
944    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
945               {100000, unrolled_loop}, {-1, libcall}}}},
946   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
947    {-1, libcall}}},
948    {libcall, {{24, loop}, {64, unrolled_loop},
949               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
950   1,                                    /* scalar_stmt_cost.  */
951   1,                                    /* scalar load_cost.  */
952   1,                                    /* scalar_store_cost.  */
953   1,                                    /* vec_stmt_cost.  */
954   1,                                    /* vec_to_scalar_cost.  */
955   1,                                    /* scalar_to_vec_cost.  */
956   1,                                    /* vec_align_load_cost.  */
957   2,                                    /* vec_unalign_load_cost.  */
958   1,                                    /* vec_store_cost.  */
959   3,                                    /* cond_taken_branch_cost.  */
960   1,                                    /* cond_not_taken_branch_cost.  */
961 };
962
963 static const
964 struct processor_costs core2_cost = {
965   COSTS_N_INSNS (1),                    /* cost of an add instruction */
966   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
967   COSTS_N_INSNS (1),                    /* variable shift costs */
968   COSTS_N_INSNS (1),                    /* constant shift costs */
969   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
970    COSTS_N_INSNS (3),                   /*                               HI */
971    COSTS_N_INSNS (3),                   /*                               SI */
972    COSTS_N_INSNS (3),                   /*                               DI */
973    COSTS_N_INSNS (3)},                  /*                               other */
974   0,                                    /* cost of multiply per each bit set */
975   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
976    COSTS_N_INSNS (22),                  /*                          HI */
977    COSTS_N_INSNS (22),                  /*                          SI */
978    COSTS_N_INSNS (22),                  /*                          DI */
979    COSTS_N_INSNS (22)},                 /*                          other */
980   COSTS_N_INSNS (1),                    /* cost of movsx */
981   COSTS_N_INSNS (1),                    /* cost of movzx */
982   8,                                    /* "large" insn */
983   16,                                   /* MOVE_RATIO */
984   2,                                    /* cost for loading QImode using movzbl */
985   {6, 6, 6},                            /* cost of loading integer registers
986                                            in QImode, HImode and SImode.
987                                            Relative to reg-reg move (2).  */
988   {4, 4, 4},                            /* cost of storing integer registers */
989   2,                                    /* cost of reg,reg fld/fst */
990   {6, 6, 6},                            /* cost of loading fp registers
991                                            in SFmode, DFmode and XFmode */
992   {4, 4, 4},                            /* cost of loading integer registers */
993   2,                                    /* cost of moving MMX register */
994   {6, 6},                               /* cost of loading MMX registers
995                                            in SImode and DImode */
996   {4, 4},                               /* cost of storing MMX registers
997                                            in SImode and DImode */
998   2,                                    /* cost of moving SSE register */
999   {6, 6, 6},                            /* cost of loading SSE registers
1000                                            in SImode, DImode and TImode */
1001   {4, 4, 4},                            /* cost of storing SSE registers
1002                                            in SImode, DImode and TImode */
1003   2,                                    /* MMX or SSE register to integer */
1004   32,                                   /* size of l1 cache.  */
1005   2048,                                 /* size of l2 cache.  */
1006   128,                                  /* size of prefetch block */
1007   8,                                    /* number of parallel prefetches */
1008   3,                                    /* Branch cost */
1009   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
1010   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
1011   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
1012   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
1013   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
1014   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
1015   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
1016    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
1017               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1018   {{libcall, {{8, loop}, {15, unrolled_loop},
1019               {2048, rep_prefix_4_byte}, {-1, libcall}}},
1020    {libcall, {{24, loop}, {32, unrolled_loop},
1021               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1022   1,                                    /* scalar_stmt_cost.  */
1023   1,                                    /* scalar load_cost.  */
1024   1,                                    /* scalar_store_cost.  */
1025   1,                                    /* vec_stmt_cost.  */
1026   1,                                    /* vec_to_scalar_cost.  */
1027   1,                                    /* scalar_to_vec_cost.  */
1028   1,                                    /* vec_align_load_cost.  */
1029   2,                                    /* vec_unalign_load_cost.  */
1030   1,                                    /* vec_store_cost.  */
1031   3,                                    /* cond_taken_branch_cost.  */
1032   1,                                    /* cond_not_taken_branch_cost.  */
1033 };
1034
1035 /* Generic64 should produce code tuned for Nocona and K8.  */
1036 static const
1037 struct processor_costs generic64_cost = {
1038   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1039   /* On all chips taken into consideration lea is 2 cycles and more.  With
1040      this cost however our current implementation of synth_mult results in
1041      use of unnecessary temporary registers causing regression on several
1042      SPECfp benchmarks.  */
1043   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1044   COSTS_N_INSNS (1),                    /* variable shift costs */
1045   COSTS_N_INSNS (1),                    /* constant shift costs */
1046   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1047    COSTS_N_INSNS (4),                   /*                               HI */
1048    COSTS_N_INSNS (3),                   /*                               SI */
1049    COSTS_N_INSNS (4),                   /*                               DI */
1050    COSTS_N_INSNS (2)},                  /*                               other */
1051   0,                                    /* cost of multiply per each bit set */
1052   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1053    COSTS_N_INSNS (26),                  /*                          HI */
1054    COSTS_N_INSNS (42),                  /*                          SI */
1055    COSTS_N_INSNS (74),                  /*                          DI */
1056    COSTS_N_INSNS (74)},                 /*                          other */
1057   COSTS_N_INSNS (1),                    /* cost of movsx */
1058   COSTS_N_INSNS (1),                    /* cost of movzx */
1059   8,                                    /* "large" insn */
1060   17,                                   /* MOVE_RATIO */
1061   4,                                    /* cost for loading QImode using movzbl */
1062   {4, 4, 4},                            /* cost of loading integer registers
1063                                            in QImode, HImode and SImode.
1064                                            Relative to reg-reg move (2).  */
1065   {4, 4, 4},                            /* cost of storing integer registers */
1066   4,                                    /* cost of reg,reg fld/fst */
1067   {12, 12, 12},                         /* cost of loading fp registers
1068                                            in SFmode, DFmode and XFmode */
1069   {6, 6, 8},                            /* cost of storing fp registers
1070                                            in SFmode, DFmode and XFmode */
1071   2,                                    /* cost of moving MMX register */
1072   {8, 8},                               /* cost of loading MMX registers
1073                                            in SImode and DImode */
1074   {8, 8},                               /* cost of storing MMX registers
1075                                            in SImode and DImode */
1076   2,                                    /* cost of moving SSE register */
1077   {8, 8, 8},                            /* cost of loading SSE registers
1078                                            in SImode, DImode and TImode */
1079   {8, 8, 8},                            /* cost of storing SSE registers
1080                                            in SImode, DImode and TImode */
1081   5,                                    /* MMX or SSE register to integer */
1082   32,                                   /* size of l1 cache.  */
1083   512,                                  /* size of l2 cache.  */
1084   64,                                   /* size of prefetch block */
1085   6,                                    /* number of parallel prefetches */
1086   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
1087      is increased to perhaps more appropriate value of 5.  */
1088   3,                                    /* Branch cost */
1089   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1090   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1091   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1092   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1093   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1094   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1095   {DUMMY_STRINGOP_ALGS,
1096    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1097   {DUMMY_STRINGOP_ALGS,
1098    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
1099   1,                                    /* scalar_stmt_cost.  */
1100   1,                                    /* scalar load_cost.  */
1101   1,                                    /* scalar_store_cost.  */
1102   1,                                    /* vec_stmt_cost.  */
1103   1,                                    /* vec_to_scalar_cost.  */
1104   1,                                    /* scalar_to_vec_cost.  */
1105   1,                                    /* vec_align_load_cost.  */
1106   2,                                    /* vec_unalign_load_cost.  */
1107   1,                                    /* vec_store_cost.  */
1108   3,                                    /* cond_taken_branch_cost.  */
1109   1,                                    /* cond_not_taken_branch_cost.  */
1110 };
1111
1112 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
1113 static const
1114 struct processor_costs generic32_cost = {
1115   COSTS_N_INSNS (1),                    /* cost of an add instruction */
1116   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
1117   COSTS_N_INSNS (1),                    /* variable shift costs */
1118   COSTS_N_INSNS (1),                    /* constant shift costs */
1119   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
1120    COSTS_N_INSNS (4),                   /*                               HI */
1121    COSTS_N_INSNS (3),                   /*                               SI */
1122    COSTS_N_INSNS (4),                   /*                               DI */
1123    COSTS_N_INSNS (2)},                  /*                               other */
1124   0,                                    /* cost of multiply per each bit set */
1125   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
1126    COSTS_N_INSNS (26),                  /*                          HI */
1127    COSTS_N_INSNS (42),                  /*                          SI */
1128    COSTS_N_INSNS (74),                  /*                          DI */
1129    COSTS_N_INSNS (74)},                 /*                          other */
1130   COSTS_N_INSNS (1),                    /* cost of movsx */
1131   COSTS_N_INSNS (1),                    /* cost of movzx */
1132   8,                                    /* "large" insn */
1133   17,                                   /* MOVE_RATIO */
1134   4,                                    /* cost for loading QImode using movzbl */
1135   {4, 4, 4},                            /* cost of loading integer registers
1136                                            in QImode, HImode and SImode.
1137                                            Relative to reg-reg move (2).  */
1138   {4, 4, 4},                            /* cost of storing integer registers */
1139   4,                                    /* cost of reg,reg fld/fst */
1140   {12, 12, 12},                         /* cost of loading fp registers
1141                                            in SFmode, DFmode and XFmode */
1142   {6, 6, 8},                            /* cost of storing fp registers
1143                                            in SFmode, DFmode and XFmode */
1144   2,                                    /* cost of moving MMX register */
1145   {8, 8},                               /* cost of loading MMX registers
1146                                            in SImode and DImode */
1147   {8, 8},                               /* cost of storing MMX registers
1148                                            in SImode and DImode */
1149   2,                                    /* cost of moving SSE register */
1150   {8, 8, 8},                            /* cost of loading SSE registers
1151                                            in SImode, DImode and TImode */
1152   {8, 8, 8},                            /* cost of storing SSE registers
1153                                            in SImode, DImode and TImode */
1154   5,                                    /* MMX or SSE register to integer */
1155   32,                                   /* size of l1 cache.  */
1156   256,                                  /* size of l2 cache.  */
1157   64,                                   /* size of prefetch block */
1158   6,                                    /* number of parallel prefetches */
1159   3,                                    /* Branch cost */
1160   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
1161   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
1162   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
1163   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
1164   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
1165   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
1166   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1167    DUMMY_STRINGOP_ALGS},
1168   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
1169    DUMMY_STRINGOP_ALGS},
1170   1,                                    /* scalar_stmt_cost.  */
1171   1,                                    /* scalar load_cost.  */
1172   1,                                    /* scalar_store_cost.  */
1173   1,                                    /* vec_stmt_cost.  */
1174   1,                                    /* vec_to_scalar_cost.  */
1175   1,                                    /* scalar_to_vec_cost.  */
1176   1,                                    /* vec_align_load_cost.  */
1177   2,                                    /* vec_unalign_load_cost.  */
1178   1,                                    /* vec_store_cost.  */
1179   3,                                    /* cond_taken_branch_cost.  */
1180   1,                                    /* cond_not_taken_branch_cost.  */
1181 };
1182
1183 const struct processor_costs *ix86_cost = &pentium_cost;
1184
1185 /* Processor feature/optimization bitmasks.  */
1186 #define m_386 (1<<PROCESSOR_I386)
1187 #define m_486 (1<<PROCESSOR_I486)
1188 #define m_PENT (1<<PROCESSOR_PENTIUM)
1189 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
1190 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
1191 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
1192 #define m_CORE2  (1<<PROCESSOR_CORE2)
1193
1194 #define m_GEODE  (1<<PROCESSOR_GEODE)
1195 #define m_K6  (1<<PROCESSOR_K6)
1196 #define m_K6_GEODE  (m_K6 | m_GEODE)
1197 #define m_K8  (1<<PROCESSOR_K8)
1198 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
1199 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
1200 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
1201 #define m_AMD_MULTIPLE  (m_K8 | m_ATHLON | m_AMDFAM10)
1202
1203 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1204 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1205
1206 /* Generic instruction choice should be common subset of supported CPUs
1207    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1208 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1209
1210 /* Feature tests against the various tunings.  */
1211 unsigned int ix86_tune_features[X86_TUNE_LAST] = {
1212   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1213      negatively, so enabling for Generic64 seems like good code size
1214      tradeoff.  We can't enable it for 32bit generic because it does not
1215      work well with PPro base chips.  */
1216   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_CORE2 | m_GENERIC64,
1217
1218   /* X86_TUNE_PUSH_MEMORY */
1219   m_386 | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4
1220   | m_NOCONA | m_CORE2 | m_GENERIC,
1221
1222   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1223   m_486 | m_PENT,
1224
1225   /* X86_TUNE_USE_BIT_TEST */
1226   m_386,
1227
1228   /* X86_TUNE_UNROLL_STRLEN */
1229   m_486 | m_PENT | m_PPRO | m_AMD_MULTIPLE | m_K6 | m_CORE2 | m_GENERIC,
1230
1231   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1232   m_PPRO | m_K6_GEODE | m_AMD_MULTIPLE | m_PENT4 | m_GENERIC,
1233
1234   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1235      on simulation result. But after P4 was made, no performance benefit
1236      was observed with branch hints.  It also increases the code size.
1237      As a result, icc never generates branch hints.  */
1238   0,
1239
1240   /* X86_TUNE_DOUBLE_WITH_ADD */
1241   ~m_386,
1242
1243   /* X86_TUNE_USE_SAHF */
1244   m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_PENT4
1245   | m_NOCONA | m_CORE2 | m_GENERIC,
1246
1247   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1248      partial dependencies.  */
1249   m_AMD_MULTIPLE | m_PPRO | m_PENT4 | m_NOCONA
1250   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1251
1252   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1253      register stalls on Generic32 compilation setting as well.  However
1254      in current implementation the partial register stalls are not eliminated
1255      very well - they can be introduced via subregs synthesized by combine
1256      and can happen in caller/callee saving sequences.  Because this option
1257      pays back little on PPro based chips and is in conflict with partial reg
1258      dependencies used by Athlon/P4 based chips, it is better to leave it off
1259      for generic32 for now.  */
1260   m_PPRO,
1261
1262   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1263   m_CORE2 | m_GENERIC,
1264
1265   /* X86_TUNE_USE_HIMODE_FIOP */
1266   m_386 | m_486 | m_K6_GEODE,
1267
1268   /* X86_TUNE_USE_SIMODE_FIOP */
1269   ~(m_PPRO | m_AMD_MULTIPLE | m_PENT | m_CORE2 | m_GENERIC),
1270
1271   /* X86_TUNE_USE_MOV0 */
1272   m_K6,
1273
1274   /* X86_TUNE_USE_CLTD */
1275   ~(m_PENT | m_K6 | m_CORE2 | m_GENERIC),
1276
1277   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1278   m_PENT4,
1279
1280   /* X86_TUNE_SPLIT_LONG_MOVES */
1281   m_PPRO,
1282
1283   /* X86_TUNE_READ_MODIFY_WRITE */
1284   ~m_PENT,
1285
1286   /* X86_TUNE_READ_MODIFY */
1287   ~(m_PENT | m_PPRO),
1288
1289   /* X86_TUNE_PROMOTE_QIMODE */
1290   m_K6_GEODE | m_PENT | m_386 | m_486 | m_AMD_MULTIPLE | m_CORE2
1291   | m_GENERIC /* | m_PENT4 ? */,
1292
1293   /* X86_TUNE_FAST_PREFIX */
1294   ~(m_PENT | m_486 | m_386),
1295
1296   /* X86_TUNE_SINGLE_STRINGOP */
1297   m_386 | m_PENT4 | m_NOCONA,
1298
1299   /* X86_TUNE_QIMODE_MATH */
1300   ~0,
1301
1302   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1303      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1304      might be considered for Generic32 if our scheme for avoiding partial
1305      stalls was more effective.  */
1306   ~m_PPRO,
1307
1308   /* X86_TUNE_PROMOTE_QI_REGS */
1309   0,
1310
1311   /* X86_TUNE_PROMOTE_HI_REGS */
1312   m_PPRO,
1313
1314   /* X86_TUNE_ADD_ESP_4: Enable if add/sub is preferred over 1/2 push/pop.  */
1315   m_AMD_MULTIPLE | m_K6_GEODE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1316
1317   /* X86_TUNE_ADD_ESP_8 */
1318   m_AMD_MULTIPLE | m_PPRO | m_K6_GEODE | m_386
1319   | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1320
1321   /* X86_TUNE_SUB_ESP_4 */
1322   m_AMD_MULTIPLE | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1323
1324   /* X86_TUNE_SUB_ESP_8 */
1325   m_AMD_MULTIPLE | m_PPRO | m_386 | m_486
1326   | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1327
1328   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1329      for DFmode copies */
1330   ~(m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1331     | m_GENERIC | m_GEODE),
1332
1333   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1334   m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1335
1336   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1337      conflict here in between PPro/Pentium4 based chips that thread 128bit
1338      SSE registers as single units versus K8 based chips that divide SSE
1339      registers to two 64bit halves.  This knob promotes all store destinations
1340      to be 128bit to allow register renaming on 128bit SSE units, but usually
1341      results in one extra microop on 64bit SSE units.  Experimental results
1342      shows that disabling this option on P4 brings over 20% SPECfp regression,
1343      while enabling it on K8 brings roughly 2.4% regression that can be partly
1344      masked by careful scheduling of moves.  */
1345   m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC | m_AMDFAM10,
1346
1347   /* X86_TUNE_SSE_UNALIGNED_MOVE_OPTIMAL */
1348   m_AMDFAM10,
1349
1350   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1351      are resolved on SSE register parts instead of whole registers, so we may
1352      maintain just lower part of scalar values in proper format leaving the
1353      upper part undefined.  */
1354   m_ATHLON_K8,
1355
1356   /* X86_TUNE_SSE_TYPELESS_STORES */
1357   m_AMD_MULTIPLE,
1358
1359   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1360   m_PPRO | m_PENT4 | m_NOCONA,
1361
1362   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1363   m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1364
1365   /* X86_TUNE_PROLOGUE_USING_MOVE */
1366   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1367
1368   /* X86_TUNE_EPILOGUE_USING_MOVE */
1369   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1370
1371   /* X86_TUNE_SHIFT1 */
1372   ~m_486,
1373
1374   /* X86_TUNE_USE_FFREEP */
1375   m_AMD_MULTIPLE,
1376
1377   /* X86_TUNE_INTER_UNIT_MOVES */
1378   ~(m_AMD_MULTIPLE | m_GENERIC),
1379
1380   /* X86_TUNE_INTER_UNIT_CONVERSIONS */
1381   ~(m_AMDFAM10),
1382
1383   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1384      than 4 branch instructions in the 16 byte window.  */
1385   m_PPRO | m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1386
1387   /* X86_TUNE_SCHEDULE */
1388   m_PPRO | m_AMD_MULTIPLE | m_K6_GEODE | m_PENT | m_CORE2 | m_GENERIC,
1389
1390   /* X86_TUNE_USE_BT */
1391   m_AMD_MULTIPLE,
1392
1393   /* X86_TUNE_USE_INCDEC */
1394   ~(m_PENT4 | m_NOCONA | m_GENERIC),
1395
1396   /* X86_TUNE_PAD_RETURNS */
1397   m_AMD_MULTIPLE | m_CORE2 | m_GENERIC,
1398
1399   /* X86_TUNE_EXT_80387_CONSTANTS */
1400   m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC,
1401
1402   /* X86_TUNE_SHORTEN_X87_SSE */
1403   ~m_K8,
1404
1405   /* X86_TUNE_AVOID_VECTOR_DECODE */
1406   m_K8 | m_GENERIC64,
1407
1408   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1409      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1410   ~(m_386 | m_486),
1411
1412   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1413      vector path on AMD machines.  */
1414   m_K8 | m_GENERIC64 | m_AMDFAM10,
1415
1416   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1417      machines.  */
1418   m_K8 | m_GENERIC64 | m_AMDFAM10,
1419
1420   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1421      than a MOV.  */
1422   m_PENT,
1423
1424   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1425      but one byte longer.  */
1426   m_PENT,
1427
1428   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1429      operand that cannot be represented using a modRM byte.  The XOR
1430      replacement is long decoded, so this split helps here as well.  */
1431   m_K6,
1432
1433   /* X86_TUNE_USE_VECTOR_CONVERTS: Prefer vector packed SSE conversion
1434      from integer to FP. */
1435   m_AMDFAM10,
1436 };
1437
1438 /* Feature tests against the various architecture variations.  */
1439 unsigned int ix86_arch_features[X86_ARCH_LAST] = {
1440   /* X86_ARCH_CMOVE: Conditional move was added for pentiumpro.  */
1441   ~(m_386 | m_486 | m_PENT | m_K6),
1442
1443   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1444   ~m_386,
1445
1446   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1447   ~(m_386 | m_486),
1448
1449   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1450   ~m_386,
1451
1452   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1453   ~m_386,
1454 };
1455
1456 static const unsigned int x86_accumulate_outgoing_args
1457   = m_AMD_MULTIPLE | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
1458
1459 static const unsigned int x86_arch_always_fancy_math_387
1460   = m_PENT | m_PPRO | m_AMD_MULTIPLE | m_PENT4
1461     | m_NOCONA | m_CORE2 | m_GENERIC;
1462
1463 static enum stringop_alg stringop_alg = no_stringop;
1464
1465 /* In case the average insn count for single function invocation is
1466    lower than this constant, emit fast (but longer) prologue and
1467    epilogue code.  */
1468 #define FAST_PROLOGUE_INSN_COUNT 20
1469
1470 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1471 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1472 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1473 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1474
1475 /* Array of the smallest class containing reg number REGNO, indexed by
1476    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1477
1478 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1479 {
1480   /* ax, dx, cx, bx */
1481   AREG, DREG, CREG, BREG,
1482   /* si, di, bp, sp */
1483   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1484   /* FP registers */
1485   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1486   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1487   /* arg pointer */
1488   NON_Q_REGS,
1489   /* flags, fpsr, fpcr, frame */
1490   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1491   /* SSE registers */
1492   SSE_FIRST_REG, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1493   SSE_REGS, SSE_REGS,
1494   /* MMX registers */
1495   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1496   MMX_REGS, MMX_REGS,
1497   /* REX registers */
1498   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1499   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1500   /* SSE REX registers */
1501   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1502   SSE_REGS, SSE_REGS,
1503 };
1504
1505 /* The "default" register map used in 32bit mode.  */
1506
1507 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1508 {
1509   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1510   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1511   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1512   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1513   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1514   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1515   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1516 };
1517
1518 static int const x86_64_int_parameter_registers[6] =
1519 {
1520   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
1521   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1522 };
1523
1524 static int const x86_64_ms_abi_int_parameter_registers[4] =
1525 {
1526   2 /*RCX*/, 1 /*RDX*/,
1527   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1528 };
1529
1530 static int const x86_64_int_return_registers[4] =
1531 {
1532   0 /*RAX*/, 1 /*RDX*/, 5 /*RDI*/, 4 /*RSI*/
1533 };
1534
1535 /* The "default" register map used in 64bit mode.  */
1536 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1537 {
1538   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1539   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1540   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1541   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1542   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1543   8,9,10,11,12,13,14,15,                /* extended integer registers */
1544   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1545 };
1546
1547 /* Define the register numbers to be used in Dwarf debugging information.
1548    The SVR4 reference port C compiler uses the following register numbers
1549    in its Dwarf output code:
1550         0 for %eax (gcc regno = 0)
1551         1 for %ecx (gcc regno = 2)
1552         2 for %edx (gcc regno = 1)
1553         3 for %ebx (gcc regno = 3)
1554         4 for %esp (gcc regno = 7)
1555         5 for %ebp (gcc regno = 6)
1556         6 for %esi (gcc regno = 4)
1557         7 for %edi (gcc regno = 5)
1558    The following three DWARF register numbers are never generated by
1559    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1560    believes these numbers have these meanings.
1561         8  for %eip    (no gcc equivalent)
1562         9  for %eflags (gcc regno = 17)
1563         10 for %trapno (no gcc equivalent)
1564    It is not at all clear how we should number the FP stack registers
1565    for the x86 architecture.  If the version of SDB on x86/svr4 were
1566    a bit less brain dead with respect to floating-point then we would
1567    have a precedent to follow with respect to DWARF register numbers
1568    for x86 FP registers, but the SDB on x86/svr4 is so completely
1569    broken with respect to FP registers that it is hardly worth thinking
1570    of it as something to strive for compatibility with.
1571    The version of x86/svr4 SDB I have at the moment does (partially)
1572    seem to believe that DWARF register number 11 is associated with
1573    the x86 register %st(0), but that's about all.  Higher DWARF
1574    register numbers don't seem to be associated with anything in
1575    particular, and even for DWARF regno 11, SDB only seems to under-
1576    stand that it should say that a variable lives in %st(0) (when
1577    asked via an `=' command) if we said it was in DWARF regno 11,
1578    but SDB still prints garbage when asked for the value of the
1579    variable in question (via a `/' command).
1580    (Also note that the labels SDB prints for various FP stack regs
1581    when doing an `x' command are all wrong.)
1582    Note that these problems generally don't affect the native SVR4
1583    C compiler because it doesn't allow the use of -O with -g and
1584    because when it is *not* optimizing, it allocates a memory
1585    location for each floating-point variable, and the memory
1586    location is what gets described in the DWARF AT_location
1587    attribute for the variable in question.
1588    Regardless of the severe mental illness of the x86/svr4 SDB, we
1589    do something sensible here and we use the following DWARF
1590    register numbers.  Note that these are all stack-top-relative
1591    numbers.
1592         11 for %st(0) (gcc regno = 8)
1593         12 for %st(1) (gcc regno = 9)
1594         13 for %st(2) (gcc regno = 10)
1595         14 for %st(3) (gcc regno = 11)
1596         15 for %st(4) (gcc regno = 12)
1597         16 for %st(5) (gcc regno = 13)
1598         17 for %st(6) (gcc regno = 14)
1599         18 for %st(7) (gcc regno = 15)
1600 */
1601 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1602 {
1603   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1604   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1605   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1606   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1607   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1608   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1609   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1610 };
1611
1612 /* Test and compare insns in i386.md store the information needed to
1613    generate branch and scc insns here.  */
1614
1615 rtx ix86_compare_op0 = NULL_RTX;
1616 rtx ix86_compare_op1 = NULL_RTX;
1617 rtx ix86_compare_emitted = NULL_RTX;
1618
1619 /* Size of the register save area.  */
1620 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1621
1622 /* Define the structure for the machine field in struct function.  */
1623
1624 struct stack_local_entry GTY(())
1625 {
1626   unsigned short mode;
1627   unsigned short n;
1628   rtx rtl;
1629   struct stack_local_entry *next;
1630 };
1631
1632 /* Structure describing stack frame layout.
1633    Stack grows downward:
1634
1635    [arguments]
1636                                               <- ARG_POINTER
1637    saved pc
1638
1639    saved frame pointer if frame_pointer_needed
1640                                               <- HARD_FRAME_POINTER
1641    [saved regs]
1642
1643    [padding1]          \
1644                         )
1645    [va_arg registers]  (
1646                         > to_allocate         <- FRAME_POINTER
1647    [frame]             (
1648                         )
1649    [padding2]          /
1650   */
1651 struct ix86_frame
1652 {
1653   int nregs;
1654   int padding1;
1655   int va_arg_size;
1656   HOST_WIDE_INT frame;
1657   int padding2;
1658   int outgoing_arguments_size;
1659   int red_zone_size;
1660
1661   HOST_WIDE_INT to_allocate;
1662   /* The offsets relative to ARG_POINTER.  */
1663   HOST_WIDE_INT frame_pointer_offset;
1664   HOST_WIDE_INT hard_frame_pointer_offset;
1665   HOST_WIDE_INT stack_pointer_offset;
1666
1667   /* When save_regs_using_mov is set, emit prologue using
1668      move instead of push instructions.  */
1669   bool save_regs_using_mov;
1670 };
1671
1672 /* Code model option.  */
1673 enum cmodel ix86_cmodel;
1674 /* Asm dialect.  */
1675 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1676 /* TLS dialects.  */
1677 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1678
1679 /* Which unit we are generating floating point math for.  */
1680 enum fpmath_unit ix86_fpmath;
1681
1682 /* Which cpu are we scheduling for.  */
1683 enum processor_type ix86_tune;
1684
1685 /* Which instruction set architecture to use.  */
1686 enum processor_type ix86_arch;
1687
1688 /* true if sse prefetch instruction is not NOOP.  */
1689 int x86_prefetch_sse;
1690
1691 /* ix86_regparm_string as a number */
1692 static int ix86_regparm;
1693
1694 /* -mstackrealign option */
1695 extern int ix86_force_align_arg_pointer;
1696 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1697
1698 /* Preferred alignment for stack boundary in bits.  */
1699 unsigned int ix86_preferred_stack_boundary;
1700
1701 /* Values 1-5: see jump.c */
1702 int ix86_branch_cost;
1703
1704 /* Variables which are this size or smaller are put in the data/bss
1705    or ldata/lbss sections.  */
1706
1707 int ix86_section_threshold = 65536;
1708
1709 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1710 char internal_label_prefix[16];
1711 int internal_label_prefix_len;
1712
1713 /* Fence to use after loop using movnt.  */
1714 tree x86_mfence;
1715
1716 /* Register class used for passing given 64bit part of the argument.
1717    These represent classes as documented by the PS ABI, with the exception
1718    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1719    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1720
1721    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1722    whenever possible (upper half does contain padding).  */
1723 enum x86_64_reg_class
1724   {
1725     X86_64_NO_CLASS,
1726     X86_64_INTEGER_CLASS,
1727     X86_64_INTEGERSI_CLASS,
1728     X86_64_SSE_CLASS,
1729     X86_64_SSESF_CLASS,
1730     X86_64_SSEDF_CLASS,
1731     X86_64_SSEUP_CLASS,
1732     X86_64_X87_CLASS,
1733     X86_64_X87UP_CLASS,
1734     X86_64_COMPLEX_X87_CLASS,
1735     X86_64_MEMORY_CLASS
1736   };
1737 static const char * const x86_64_reg_class_name[] =
1738 {
1739   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1740   "sseup", "x87", "x87up", "cplx87", "no"
1741 };
1742
1743 #define MAX_CLASSES 4
1744
1745 /* Table of constants used by fldpi, fldln2, etc....  */
1746 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1747 static bool ext_80387_constants_init = 0;
1748
1749 \f
1750 static struct machine_function * ix86_init_machine_status (void);
1751 static rtx ix86_function_value (const_tree, const_tree, bool);
1752 static int ix86_function_regparm (const_tree, const_tree);
1753 static void ix86_compute_frame_layout (struct ix86_frame *);
1754 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1755                                                  rtx, rtx, int);
1756
1757 \f
1758 /* The svr4 ABI for the i386 says that records and unions are returned
1759    in memory.  */
1760 #ifndef DEFAULT_PCC_STRUCT_RETURN
1761 #define DEFAULT_PCC_STRUCT_RETURN 1
1762 #endif
1763
1764 /* Bit flags that specify the ISA we are compiling for.  */
1765 int ix86_isa_flags = TARGET_64BIT_DEFAULT | TARGET_SUBTARGET_ISA_DEFAULT;
1766
1767 /* A mask of ix86_isa_flags that includes bit X if X
1768    was set or cleared on the command line.  */
1769 static int ix86_isa_flags_explicit;
1770
1771 /* Define a set of ISAs which are available when a given ISA is
1772    enabled.  MMX and SSE ISAs are handled separately.  */
1773
1774 #define OPTION_MASK_ISA_MMX_SET OPTION_MASK_ISA_MMX
1775 #define OPTION_MASK_ISA_3DNOW_SET \
1776   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_MMX_SET)
1777
1778 #define OPTION_MASK_ISA_SSE_SET OPTION_MASK_ISA_SSE
1779 #define OPTION_MASK_ISA_SSE2_SET \
1780   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE_SET)
1781 #define OPTION_MASK_ISA_SSE3_SET \
1782   (OPTION_MASK_ISA_SSE3 | OPTION_MASK_ISA_SSE2_SET)
1783 #define OPTION_MASK_ISA_SSSE3_SET \
1784   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE3_SET)
1785 #define OPTION_MASK_ISA_SSE4_1_SET \
1786   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSSE3_SET)
1787 #define OPTION_MASK_ISA_SSE4_2_SET \
1788   (OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_SSE4_1_SET)
1789
1790 /* SSE4 includes both SSE4.1 and SSE4.2. -msse4 should be the same
1791    as -msse4.2.  */
1792 #define OPTION_MASK_ISA_SSE4_SET OPTION_MASK_ISA_SSE4_2_SET
1793
1794 #define OPTION_MASK_ISA_SSE4A_SET \
1795   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE3_SET)
1796 #define OPTION_MASK_ISA_SSE5_SET \
1797   (OPTION_MASK_ISA_SSE5 | OPTION_MASK_ISA_SSE4A_SET)
1798
1799 /* Define a set of ISAs which aren't available when a given ISA is
1800    disabled.  MMX and SSE ISAs are handled separately.  */
1801
1802 #define OPTION_MASK_ISA_MMX_UNSET \
1803   (OPTION_MASK_ISA_MMX | OPTION_MASK_ISA_3DNOW_UNSET)
1804 #define OPTION_MASK_ISA_3DNOW_UNSET \
1805   (OPTION_MASK_ISA_3DNOW | OPTION_MASK_ISA_3DNOW_A_UNSET)
1806 #define OPTION_MASK_ISA_3DNOW_A_UNSET OPTION_MASK_ISA_3DNOW_A
1807
1808 #define OPTION_MASK_ISA_SSE_UNSET \
1809   (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_SSE2_UNSET)
1810 #define OPTION_MASK_ISA_SSE2_UNSET \
1811   (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE3_UNSET)
1812 #define OPTION_MASK_ISA_SSE3_UNSET \
1813   (OPTION_MASK_ISA_SSE3 \
1814    | OPTION_MASK_ISA_SSSE3_UNSET \
1815    | OPTION_MASK_ISA_SSE4A_UNSET )
1816 #define OPTION_MASK_ISA_SSSE3_UNSET \
1817   (OPTION_MASK_ISA_SSSE3 | OPTION_MASK_ISA_SSE4_1_UNSET)
1818 #define OPTION_MASK_ISA_SSE4_1_UNSET \
1819   (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_SSE4_2_UNSET)
1820 #define OPTION_MASK_ISA_SSE4_2_UNSET OPTION_MASK_ISA_SSE4_2
1821
1822 /* SSE4 includes both SSE4.1 and SSE4.2.  -mno-sse4 should the same
1823    as -mno-sse4.1. */
1824 #define OPTION_MASK_ISA_SSE4_UNSET OPTION_MASK_ISA_SSE4_1_UNSET
1825
1826 #define OPTION_MASK_ISA_SSE4A_UNSET \
1827   (OPTION_MASK_ISA_SSE4A | OPTION_MASK_ISA_SSE5_UNSET)
1828
1829 #define OPTION_MASK_ISA_SSE5_UNSET OPTION_MASK_ISA_SSE5
1830
1831 /* Vectorization library interface and handlers.  */
1832 tree (*ix86_veclib_handler)(enum built_in_function, tree, tree) = NULL;
1833 static tree ix86_veclibabi_acml (enum built_in_function, tree, tree);
1834
1835 /* Implement TARGET_HANDLE_OPTION.  */
1836
1837 static bool
1838 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1839 {
1840   switch (code)
1841     {
1842     case OPT_mmmx:
1843       if (value)
1844         {
1845           ix86_isa_flags |= OPTION_MASK_ISA_MMX_SET;
1846           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_SET;
1847         }
1848       else
1849         {
1850           ix86_isa_flags &= ~OPTION_MASK_ISA_MMX_UNSET;
1851           ix86_isa_flags_explicit |= OPTION_MASK_ISA_MMX_UNSET;
1852         }
1853       return true;
1854
1855     case OPT_m3dnow:
1856       if (value)
1857         {
1858           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_SET;
1859           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_SET;
1860         }
1861       else
1862         {
1863           ix86_isa_flags &= ~OPTION_MASK_ISA_3DNOW_UNSET;
1864           ix86_isa_flags_explicit |= OPTION_MASK_ISA_3DNOW_UNSET;
1865         }
1866       return true;
1867
1868     case OPT_m3dnowa:
1869       return false;
1870
1871     case OPT_msse:
1872       if (value)
1873         {
1874           ix86_isa_flags |= OPTION_MASK_ISA_SSE_SET;
1875           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_SET;
1876         }
1877       else
1878         {
1879           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE_UNSET;
1880           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE_UNSET;
1881         }
1882       return true;
1883
1884     case OPT_msse2:
1885       if (value)
1886         {
1887           ix86_isa_flags |= OPTION_MASK_ISA_SSE2_SET;
1888           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_SET;
1889         }
1890       else
1891         {
1892           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE2_UNSET;
1893           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE2_UNSET;
1894         }
1895       return true;
1896
1897     case OPT_msse3:
1898       if (value)
1899         {
1900           ix86_isa_flags |= OPTION_MASK_ISA_SSE3_SET;
1901           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_SET;
1902         }
1903       else
1904         {
1905           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE3_UNSET;
1906           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE3_UNSET;
1907         }
1908       return true;
1909
1910     case OPT_mssse3:
1911       if (value)
1912         {
1913           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3_SET;
1914           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_SET;
1915         }
1916       else
1917         {
1918           ix86_isa_flags &= ~OPTION_MASK_ISA_SSSE3_UNSET;
1919           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSSE3_UNSET;
1920         }
1921       return true;
1922
1923     case OPT_msse4_1:
1924       if (value)
1925         {
1926           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1_SET;
1927           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_SET;
1928         }
1929       else
1930         {
1931           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_1_UNSET;
1932           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_1_UNSET;
1933         }
1934       return true;
1935
1936     case OPT_msse4_2:
1937       if (value)
1938         {
1939           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2_SET;
1940           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_SET;
1941         }
1942       else
1943         {
1944           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_2_UNSET;
1945           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_2_UNSET;
1946         }
1947       return true;
1948
1949     case OPT_msse4:
1950       ix86_isa_flags |= OPTION_MASK_ISA_SSE4_SET;
1951       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_SET;
1952       return true;
1953
1954     case OPT_mno_sse4:
1955       ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4_UNSET;
1956       ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4_UNSET;
1957       return true;
1958
1959     case OPT_msse4a:
1960       if (value)
1961         {
1962           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A_SET;
1963           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_SET;
1964         }
1965       else
1966         {
1967           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE4A_UNSET;
1968           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE4A_UNSET;
1969         }
1970       return true;
1971
1972     case OPT_msse5:
1973       if (value)
1974         {
1975           ix86_isa_flags |= OPTION_MASK_ISA_SSE5_SET;
1976           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE5_SET;
1977         }
1978       else
1979         {
1980           ix86_isa_flags &= ~OPTION_MASK_ISA_SSE5_UNSET;
1981           ix86_isa_flags_explicit |= OPTION_MASK_ISA_SSE5_UNSET;
1982         }
1983       return true;
1984
1985     default:
1986       return true;
1987     }
1988 }
1989
1990 /* Sometimes certain combinations of command options do not make
1991    sense on a particular target machine.  You can define a macro
1992    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1993    defined, is executed once just after all the command options have
1994    been parsed.
1995
1996    Don't use this macro to turn on various extra optimizations for
1997    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1998
1999 void
2000 override_options (void)
2001 {
2002   int i;
2003   int ix86_tune_defaulted = 0;
2004   int ix86_arch_specified = 0;
2005   unsigned int ix86_arch_mask, ix86_tune_mask;
2006
2007   /* Comes from final.c -- no real reason to change it.  */
2008 #define MAX_CODE_ALIGN 16
2009
2010   static struct ptt
2011     {
2012       const struct processor_costs *cost;       /* Processor costs */
2013       const int align_loop;                     /* Default alignments.  */
2014       const int align_loop_max_skip;
2015       const int align_jump;
2016       const int align_jump_max_skip;
2017       const int align_func;
2018     }
2019   const processor_target_table[PROCESSOR_max] =
2020     {
2021       {&i386_cost, 4, 3, 4, 3, 4},
2022       {&i486_cost, 16, 15, 16, 15, 16},
2023       {&pentium_cost, 16, 7, 16, 7, 16},
2024       {&pentiumpro_cost, 16, 15, 16, 10, 16},
2025       {&geode_cost, 0, 0, 0, 0, 0},
2026       {&k6_cost, 32, 7, 32, 7, 32},
2027       {&athlon_cost, 16, 7, 16, 7, 16},
2028       {&pentium4_cost, 0, 0, 0, 0, 0},
2029       {&k8_cost, 16, 7, 16, 7, 16},
2030       {&nocona_cost, 0, 0, 0, 0, 0},
2031       {&core2_cost, 16, 10, 16, 10, 16},
2032       {&generic32_cost, 16, 7, 16, 7, 16},
2033       {&generic64_cost, 16, 10, 16, 10, 16},
2034       {&amdfam10_cost, 32, 24, 32, 7, 32}
2035     };
2036
2037   static const char *const cpu_names[TARGET_CPU_DEFAULT_max] =
2038     {
2039       "generic",
2040       "i386",
2041       "i486",
2042       "pentium",
2043       "pentium-mmx",
2044       "pentiumpro",
2045       "pentium2",
2046       "pentium3",
2047       "pentium4",
2048       "pentium-m",
2049       "prescott",
2050       "nocona",
2051       "core2",
2052       "geode",
2053       "k6",
2054       "k6-2",
2055       "k6-3",
2056       "athlon",
2057       "athlon-4",
2058       "k8",
2059       "amdfam10"
2060     };
2061
2062   enum pta_flags
2063     {
2064       PTA_SSE = 1 << 0,
2065       PTA_SSE2 = 1 << 1,
2066       PTA_SSE3 = 1 << 2,
2067       PTA_MMX = 1 << 3,
2068       PTA_PREFETCH_SSE = 1 << 4,
2069       PTA_3DNOW = 1 << 5,
2070       PTA_3DNOW_A = 1 << 6,
2071       PTA_64BIT = 1 << 7,
2072       PTA_SSSE3 = 1 << 8,
2073       PTA_CX16 = 1 << 9,
2074       PTA_POPCNT = 1 << 10,
2075       PTA_ABM = 1 << 11,
2076       PTA_SSE4A = 1 << 12,
2077       PTA_NO_SAHF = 1 << 13,
2078       PTA_SSE4_1 = 1 << 14,
2079       PTA_SSE4_2 = 1 << 15,
2080       PTA_SSE5 = 1 << 16
2081     };
2082
2083   static struct pta
2084     {
2085       const char *const name;           /* processor name or nickname.  */
2086       const enum processor_type processor;
2087       const unsigned /*enum pta_flags*/ flags;
2088     }
2089   const processor_alias_table[] =
2090     {
2091       {"i386", PROCESSOR_I386, 0},
2092       {"i486", PROCESSOR_I486, 0},
2093       {"i586", PROCESSOR_PENTIUM, 0},
2094       {"pentium", PROCESSOR_PENTIUM, 0},
2095       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
2096       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
2097       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
2098       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
2099       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE},
2100       {"i686", PROCESSOR_PENTIUMPRO, 0},
2101       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
2102       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
2103       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE},
2104       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE},
2105       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_SSE2},
2106       {"pentium4", PROCESSOR_PENTIUM4, PTA_MMX |PTA_SSE | PTA_SSE2},
2107       {"pentium4m", PROCESSOR_PENTIUM4, PTA_MMX | PTA_SSE | PTA_SSE2},
2108       {"prescott", PROCESSOR_NOCONA, PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3},
2109       {"nocona", PROCESSOR_NOCONA, (PTA_64BIT
2110                                     | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2111                                     | PTA_CX16 | PTA_NO_SAHF)},
2112       {"core2", PROCESSOR_CORE2, (PTA_64BIT
2113                                   | PTA_MMX | PTA_SSE | PTA_SSE2 | PTA_SSE3
2114                                   | PTA_SSSE3
2115                                   | PTA_CX16)},
2116       {"geode", PROCESSOR_GEODE, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2117                                   |PTA_PREFETCH_SSE)},
2118       {"k6", PROCESSOR_K6, PTA_MMX},
2119       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
2120       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
2121       {"athlon", PROCESSOR_ATHLON, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2122                                     | PTA_PREFETCH_SSE)},
2123       {"athlon-tbird", PROCESSOR_ATHLON, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2124                                           | PTA_PREFETCH_SSE)},
2125       {"athlon-4", PROCESSOR_ATHLON, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2126                                       | PTA_SSE)},
2127       {"athlon-xp", PROCESSOR_ATHLON, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2128                                        | PTA_SSE)},
2129       {"athlon-mp", PROCESSOR_ATHLON, (PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2130                                        | PTA_SSE)},
2131       {"x86-64", PROCESSOR_K8, (PTA_64BIT
2132                                 | PTA_MMX | PTA_SSE | PTA_SSE2
2133                                 | PTA_NO_SAHF)},
2134       {"k8", PROCESSOR_K8, (PTA_64BIT
2135                             | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2136                             | PTA_SSE | PTA_SSE2
2137                             | PTA_NO_SAHF)},
2138       {"k8-sse3", PROCESSOR_K8, (PTA_64BIT
2139                                  | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2140                                  | PTA_SSE | PTA_SSE2 | PTA_SSE3
2141                                  | PTA_NO_SAHF)},
2142       {"opteron", PROCESSOR_K8, (PTA_64BIT
2143                                  | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2144                                  | PTA_SSE | PTA_SSE2
2145                                  | PTA_NO_SAHF)},
2146       {"opteron-sse3", PROCESSOR_K8, (PTA_64BIT
2147                                       | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2148                                       | PTA_SSE | PTA_SSE2 | PTA_SSE3
2149                                       | PTA_NO_SAHF)},
2150       {"athlon64", PROCESSOR_K8, (PTA_64BIT
2151                                   | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2152                                   | PTA_SSE | PTA_SSE2
2153                                   | PTA_NO_SAHF)},
2154       {"athlon64-sse3", PROCESSOR_K8, (PTA_64BIT
2155                                        | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2156                                        | PTA_SSE | PTA_SSE2 | PTA_SSE3
2157                                        | PTA_NO_SAHF)},
2158       {"athlon-fx", PROCESSOR_K8, (PTA_64BIT
2159                                    | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2160                                    | PTA_SSE | PTA_SSE2
2161                                    | PTA_NO_SAHF)},
2162       {"amdfam10", PROCESSOR_AMDFAM10, (PTA_64BIT
2163                                         | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2164                                         | PTA_SSE | PTA_SSE2 | PTA_SSE3
2165                                         | PTA_SSE4A
2166                                         | PTA_CX16 | PTA_ABM)},
2167       {"barcelona", PROCESSOR_AMDFAM10, (PTA_64BIT
2168                                          | PTA_MMX | PTA_3DNOW | PTA_3DNOW_A
2169                                          | PTA_SSE | PTA_SSE2 | PTA_SSE3
2170                                          | PTA_SSE4A
2171                                          | PTA_CX16 | PTA_ABM)},
2172       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
2173       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
2174     };
2175
2176   int const pta_size = ARRAY_SIZE (processor_alias_table);
2177
2178 #ifdef SUBTARGET_OVERRIDE_OPTIONS
2179   SUBTARGET_OVERRIDE_OPTIONS;
2180 #endif
2181
2182 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
2183   SUBSUBTARGET_OVERRIDE_OPTIONS;
2184 #endif
2185
2186   /* -fPIC is the default for x86_64.  */
2187   if (TARGET_MACHO && TARGET_64BIT)
2188     flag_pic = 2;
2189
2190   /* Set the default values for switches whose default depends on TARGET_64BIT
2191      in case they weren't overwritten by command line options.  */
2192   if (TARGET_64BIT)
2193     {
2194       /* Mach-O doesn't support omitting the frame pointer for now.  */
2195       if (flag_omit_frame_pointer == 2)
2196         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
2197       if (flag_asynchronous_unwind_tables == 2)
2198         flag_asynchronous_unwind_tables = 1;
2199       if (flag_pcc_struct_return == 2)
2200         flag_pcc_struct_return = 0;
2201     }
2202   else
2203     {
2204       if (flag_omit_frame_pointer == 2)
2205         flag_omit_frame_pointer = 0;
2206       if (flag_asynchronous_unwind_tables == 2)
2207         flag_asynchronous_unwind_tables = 0;
2208       if (flag_pcc_struct_return == 2)
2209         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
2210     }
2211
2212   /* Need to check -mtune=generic first.  */
2213   if (ix86_tune_string)
2214     {
2215       if (!strcmp (ix86_tune_string, "generic")
2216           || !strcmp (ix86_tune_string, "i686")
2217           /* As special support for cross compilers we read -mtune=native
2218              as -mtune=generic.  With native compilers we won't see the
2219              -mtune=native, as it was changed by the driver.  */
2220           || !strcmp (ix86_tune_string, "native"))
2221         {
2222           if (TARGET_64BIT)
2223             ix86_tune_string = "generic64";
2224           else
2225             ix86_tune_string = "generic32";
2226         }
2227       else if (!strncmp (ix86_tune_string, "generic", 7))
2228         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
2229     }
2230   else
2231     {
2232       if (ix86_arch_string)
2233         ix86_tune_string = ix86_arch_string;
2234       if (!ix86_tune_string)
2235         {
2236           ix86_tune_string = cpu_names[TARGET_CPU_DEFAULT];
2237           ix86_tune_defaulted = 1;
2238         }
2239
2240       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
2241          need to use a sensible tune option.  */
2242       if (!strcmp (ix86_tune_string, "generic")
2243           || !strcmp (ix86_tune_string, "x86-64")
2244           || !strcmp (ix86_tune_string, "i686"))
2245         {
2246           if (TARGET_64BIT)
2247             ix86_tune_string = "generic64";
2248           else
2249             ix86_tune_string = "generic32";
2250         }
2251     }
2252   if (ix86_stringop_string)
2253     {
2254       if (!strcmp (ix86_stringop_string, "rep_byte"))
2255         stringop_alg = rep_prefix_1_byte;
2256       else if (!strcmp (ix86_stringop_string, "libcall"))
2257         stringop_alg = libcall;
2258       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
2259         stringop_alg = rep_prefix_4_byte;
2260       else if (!strcmp (ix86_stringop_string, "rep_8byte"))
2261         stringop_alg = rep_prefix_8_byte;
2262       else if (!strcmp (ix86_stringop_string, "byte_loop"))
2263         stringop_alg = loop_1_byte;
2264       else if (!strcmp (ix86_stringop_string, "loop"))
2265         stringop_alg = loop;
2266       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
2267         stringop_alg = unrolled_loop;
2268       else
2269         error ("bad value (%s) for -mstringop-strategy= switch", ix86_stringop_string);
2270     }
2271   if (!strcmp (ix86_tune_string, "x86-64"))
2272     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
2273              "-mtune=generic instead as appropriate.");
2274
2275   if (!ix86_arch_string)
2276     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
2277   else
2278     ix86_arch_specified = 1;
2279
2280   if (!strcmp (ix86_arch_string, "generic"))
2281     error ("generic CPU can be used only for -mtune= switch");
2282   if (!strncmp (ix86_arch_string, "generic", 7))
2283     error ("bad value (%s) for -march= switch", ix86_arch_string);
2284
2285   if (ix86_cmodel_string != 0)
2286     {
2287       if (!strcmp (ix86_cmodel_string, "small"))
2288         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
2289       else if (!strcmp (ix86_cmodel_string, "medium"))
2290         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
2291       else if (!strcmp (ix86_cmodel_string, "large"))
2292         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
2293       else if (flag_pic)
2294         error ("code model %s does not support PIC mode", ix86_cmodel_string);
2295       else if (!strcmp (ix86_cmodel_string, "32"))
2296         ix86_cmodel = CM_32;
2297       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
2298         ix86_cmodel = CM_KERNEL;
2299       else
2300         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
2301     }
2302   else
2303     {
2304       /* For TARGET_64BIT_MS_ABI, force pic on, in order to enable the
2305          use of rip-relative addressing.  This eliminates fixups that
2306          would otherwise be needed if this object is to be placed in a
2307          DLL, and is essentially just as efficient as direct addressing.  */
2308       if (TARGET_64BIT_MS_ABI)
2309         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
2310       else if (TARGET_64BIT)
2311         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
2312       else
2313         ix86_cmodel = CM_32;
2314     }
2315   if (ix86_asm_string != 0)
2316     {
2317       if (! TARGET_MACHO
2318           && !strcmp (ix86_asm_string, "intel"))
2319         ix86_asm_dialect = ASM_INTEL;
2320       else if (!strcmp (ix86_asm_string, "att"))
2321         ix86_asm_dialect = ASM_ATT;
2322       else
2323         error ("bad value (%s) for -masm= switch", ix86_asm_string);
2324     }
2325   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
2326     error ("code model %qs not supported in the %s bit mode",
2327            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
2328   if ((TARGET_64BIT != 0) != ((ix86_isa_flags & OPTION_MASK_ISA_64BIT) != 0))
2329     sorry ("%i-bit mode not compiled in",
2330            (ix86_isa_flags & OPTION_MASK_ISA_64BIT) ? 64 : 32);
2331
2332   for (i = 0; i < pta_size; i++)
2333     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
2334       {
2335         ix86_arch = processor_alias_table[i].processor;
2336         /* Default cpu tuning to the architecture.  */
2337         ix86_tune = ix86_arch;
2338
2339         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
2340           error ("CPU you selected does not support x86-64 "
2341                  "instruction set");
2342
2343         if (processor_alias_table[i].flags & PTA_MMX
2344             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_MMX))
2345           ix86_isa_flags |= OPTION_MASK_ISA_MMX;
2346         if (processor_alias_table[i].flags & PTA_3DNOW
2347             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW))
2348           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW;
2349         if (processor_alias_table[i].flags & PTA_3DNOW_A
2350             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_3DNOW_A))
2351           ix86_isa_flags |= OPTION_MASK_ISA_3DNOW_A;
2352         if (processor_alias_table[i].flags & PTA_SSE
2353             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE))
2354           ix86_isa_flags |= OPTION_MASK_ISA_SSE;
2355         if (processor_alias_table[i].flags & PTA_SSE2
2356             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE2))
2357           ix86_isa_flags |= OPTION_MASK_ISA_SSE2;
2358         if (processor_alias_table[i].flags & PTA_SSE3
2359             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE3))
2360           ix86_isa_flags |= OPTION_MASK_ISA_SSE3;
2361         if (processor_alias_table[i].flags & PTA_SSSE3
2362             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSSE3))
2363           ix86_isa_flags |= OPTION_MASK_ISA_SSSE3;
2364         if (processor_alias_table[i].flags & PTA_SSE4_1
2365             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_1))
2366           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_1;
2367         if (processor_alias_table[i].flags & PTA_SSE4_2
2368             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4_2))
2369           ix86_isa_flags |= OPTION_MASK_ISA_SSE4_2;
2370         if (processor_alias_table[i].flags & PTA_SSE4A
2371             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE4A))
2372           ix86_isa_flags |= OPTION_MASK_ISA_SSE4A;
2373         if (processor_alias_table[i].flags & PTA_SSE5
2374             && !(ix86_isa_flags_explicit & OPTION_MASK_ISA_SSE5))
2375           ix86_isa_flags |= OPTION_MASK_ISA_SSE5;
2376
2377         if (processor_alias_table[i].flags & PTA_ABM)
2378           x86_abm = true;
2379         if (processor_alias_table[i].flags & PTA_CX16)
2380           x86_cmpxchg16b = true;
2381         if (processor_alias_table[i].flags & (PTA_POPCNT | PTA_ABM))
2382           x86_popcnt = true;
2383         if (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE))
2384           x86_prefetch_sse = true;
2385         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF)))
2386           x86_sahf = true;
2387
2388         break;
2389       }
2390
2391   if (i == pta_size)
2392     error ("bad value (%s) for -march= switch", ix86_arch_string);
2393
2394   ix86_arch_mask = 1u << ix86_arch;
2395   for (i = 0; i < X86_ARCH_LAST; ++i)
2396     ix86_arch_features[i] &= ix86_arch_mask;
2397
2398   for (i = 0; i < pta_size; i++)
2399     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
2400       {
2401         ix86_tune = processor_alias_table[i].processor;
2402         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
2403           {
2404             if (ix86_tune_defaulted)
2405               {
2406                 ix86_tune_string = "x86-64";
2407                 for (i = 0; i < pta_size; i++)
2408                   if (! strcmp (ix86_tune_string,
2409                                 processor_alias_table[i].name))
2410                     break;
2411                 ix86_tune = processor_alias_table[i].processor;
2412               }
2413             else
2414               error ("CPU you selected does not support x86-64 "
2415                      "instruction set");
2416           }
2417         /* Intel CPUs have always interpreted SSE prefetch instructions as
2418            NOPs; so, we can enable SSE prefetch instructions even when
2419            -mtune (rather than -march) points us to a processor that has them.
2420            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
2421            higher processors.  */
2422         if (TARGET_CMOVE
2423             && (processor_alias_table[i].flags & (PTA_PREFETCH_SSE | PTA_SSE)))
2424           x86_prefetch_sse = true;
2425         break;
2426       }
2427   if (i == pta_size)
2428     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
2429
2430   ix86_tune_mask = 1u << ix86_tune;
2431   for (i = 0; i < X86_TUNE_LAST; ++i)
2432     ix86_tune_features[i] &= ix86_tune_mask;
2433
2434   if (optimize_size)
2435     ix86_cost = &size_cost;
2436   else
2437     ix86_cost = processor_target_table[ix86_tune].cost;
2438
2439   /* Arrange to set up i386_stack_locals for all functions.  */
2440   init_machine_status = ix86_init_machine_status;
2441
2442   /* Validate -mregparm= value.  */
2443   if (ix86_regparm_string)
2444     {
2445       if (TARGET_64BIT)
2446         warning (0, "-mregparm is ignored in 64-bit mode");
2447       i = atoi (ix86_regparm_string);
2448       if (i < 0 || i > REGPARM_MAX)
2449         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
2450       else
2451         ix86_regparm = i;
2452     }
2453   if (TARGET_64BIT)
2454     ix86_regparm = REGPARM_MAX;
2455
2456   /* If the user has provided any of the -malign-* options,
2457      warn and use that value only if -falign-* is not set.
2458      Remove this code in GCC 3.2 or later.  */
2459   if (ix86_align_loops_string)
2460     {
2461       warning (0, "-malign-loops is obsolete, use -falign-loops");
2462       if (align_loops == 0)
2463         {
2464           i = atoi (ix86_align_loops_string);
2465           if (i < 0 || i > MAX_CODE_ALIGN)
2466             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2467           else
2468             align_loops = 1 << i;
2469         }
2470     }
2471
2472   if (ix86_align_jumps_string)
2473     {
2474       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
2475       if (align_jumps == 0)
2476         {
2477           i = atoi (ix86_align_jumps_string);
2478           if (i < 0 || i > MAX_CODE_ALIGN)
2479             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2480           else
2481             align_jumps = 1 << i;
2482         }
2483     }
2484
2485   if (ix86_align_funcs_string)
2486     {
2487       warning (0, "-malign-functions is obsolete, use -falign-functions");
2488       if (align_functions == 0)
2489         {
2490           i = atoi (ix86_align_funcs_string);
2491           if (i < 0 || i > MAX_CODE_ALIGN)
2492             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2493           else
2494             align_functions = 1 << i;
2495         }
2496     }
2497
2498   /* Default align_* from the processor table.  */
2499   if (align_loops == 0)
2500     {
2501       align_loops = processor_target_table[ix86_tune].align_loop;
2502       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
2503     }
2504   if (align_jumps == 0)
2505     {
2506       align_jumps = processor_target_table[ix86_tune].align_jump;
2507       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
2508     }
2509   if (align_functions == 0)
2510     {
2511       align_functions = processor_target_table[ix86_tune].align_func;
2512     }
2513
2514   /* Validate -mbranch-cost= value, or provide default.  */
2515   ix86_branch_cost = ix86_cost->branch_cost;
2516   if (ix86_branch_cost_string)
2517     {
2518       i = atoi (ix86_branch_cost_string);
2519       if (i < 0 || i > 5)
2520         error ("-mbranch-cost=%d is not between 0 and 5", i);
2521       else
2522         ix86_branch_cost = i;
2523     }
2524   if (ix86_section_threshold_string)
2525     {
2526       i = atoi (ix86_section_threshold_string);
2527       if (i < 0)
2528         error ("-mlarge-data-threshold=%d is negative", i);
2529       else
2530         ix86_section_threshold = i;
2531     }
2532
2533   if (ix86_tls_dialect_string)
2534     {
2535       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
2536         ix86_tls_dialect = TLS_DIALECT_GNU;
2537       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
2538         ix86_tls_dialect = TLS_DIALECT_GNU2;
2539       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
2540         ix86_tls_dialect = TLS_DIALECT_SUN;
2541       else
2542         error ("bad value (%s) for -mtls-dialect= switch",
2543                ix86_tls_dialect_string);
2544     }
2545
2546   if (ix87_precision_string)
2547     {
2548       i = atoi (ix87_precision_string);
2549       if (i != 32 && i != 64 && i != 80)
2550         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
2551     }
2552
2553   if (TARGET_64BIT)
2554     {
2555       target_flags |= TARGET_SUBTARGET64_DEFAULT & ~target_flags_explicit;
2556
2557       /* Enable by default the SSE and MMX builtins.  Do allow the user to
2558          explicitly disable any of these.  In particular, disabling SSE and
2559          MMX for kernel code is extremely useful.  */
2560       if (!ix86_arch_specified)
2561       ix86_isa_flags
2562         |= ((OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_MMX
2563              | TARGET_SUBTARGET64_ISA_DEFAULT) & ~ix86_isa_flags_explicit);
2564
2565       if (TARGET_RTD)
2566         warning (0, "-mrtd is ignored in 64bit mode");
2567     }
2568   else
2569     {
2570       target_flags |= TARGET_SUBTARGET32_DEFAULT & ~target_flags_explicit;
2571
2572       if (!ix86_arch_specified)
2573       ix86_isa_flags
2574         |= TARGET_SUBTARGET32_ISA_DEFAULT & ~ix86_isa_flags_explicit;
2575
2576       /* i386 ABI does not specify red zone.  It still makes sense to use it
2577          when programmer takes care to stack from being destroyed.  */
2578       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
2579         target_flags |= MASK_NO_RED_ZONE;
2580     }
2581
2582   /* Keep nonleaf frame pointers.  */
2583   if (flag_omit_frame_pointer)
2584     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2585   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
2586     flag_omit_frame_pointer = 1;
2587
2588   /* If we're doing fast math, we don't care about comparison order
2589      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2590   if (flag_finite_math_only)
2591     target_flags &= ~MASK_IEEE_FP;
2592
2593   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2594      since the insns won't need emulation.  */
2595   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
2596     target_flags &= ~MASK_NO_FANCY_MATH_387;
2597
2598   /* Likewise, if the target doesn't have a 387, or we've specified
2599      software floating point, don't use 387 inline intrinsics.  */
2600   if (!TARGET_80387)
2601     target_flags |= MASK_NO_FANCY_MATH_387;
2602
2603   /* Turn on MMX builtins for -msse.  */
2604   if (TARGET_SSE)
2605     {
2606       ix86_isa_flags |= OPTION_MASK_ISA_MMX & ~ix86_isa_flags_explicit;
2607       x86_prefetch_sse = true;
2608     }
2609
2610   /* Turn on popcnt instruction for -msse4.2 or -mabm.  */
2611   if (TARGET_SSE4_2 || TARGET_ABM)
2612     x86_popcnt = true;
2613
2614   /* Validate -mpreferred-stack-boundary= value, or provide default.
2615      The default of 128 bits is for Pentium III's SSE __m128.  We can't
2616      change it because of optimize_size.  Otherwise, we can't mix object
2617      files compiled with -Os and -On.  */
2618   ix86_preferred_stack_boundary = 128;
2619   if (ix86_preferred_stack_boundary_string)
2620     {
2621       i = atoi (ix86_preferred_stack_boundary_string);
2622       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
2623         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
2624                TARGET_64BIT ? 4 : 2);
2625       else
2626         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
2627     }
2628
2629   /* Accept -msseregparm only if at least SSE support is enabled.  */
2630   if (TARGET_SSEREGPARM
2631       && ! TARGET_SSE)
2632     error ("-msseregparm used without SSE enabled");
2633
2634   ix86_fpmath = TARGET_FPMATH_DEFAULT;
2635   if (ix86_fpmath_string != 0)
2636     {
2637       if (! strcmp (ix86_fpmath_string, "387"))
2638         ix86_fpmath = FPMATH_387;
2639       else if (! strcmp (ix86_fpmath_string, "sse"))
2640         {
2641           if (!TARGET_SSE)
2642             {
2643               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2644               ix86_fpmath = FPMATH_387;
2645             }
2646           else
2647             ix86_fpmath = FPMATH_SSE;
2648         }
2649       else if (! strcmp (ix86_fpmath_string, "387,sse")
2650                || ! strcmp (ix86_fpmath_string, "sse,387"))
2651         {
2652           if (!TARGET_SSE)
2653             {
2654               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2655               ix86_fpmath = FPMATH_387;
2656             }
2657           else if (!TARGET_80387)
2658             {
2659               warning (0, "387 instruction set disabled, using SSE arithmetics");
2660               ix86_fpmath = FPMATH_SSE;
2661             }
2662           else
2663             ix86_fpmath = (enum fpmath_unit) (FPMATH_SSE | FPMATH_387);
2664         }
2665       else
2666         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2667     }
2668
2669   /* If the i387 is disabled, then do not return values in it. */
2670   if (!TARGET_80387)
2671     target_flags &= ~MASK_FLOAT_RETURNS;
2672
2673   /* Use external vectorized library in vectorizing intrinsics.  */
2674   if (ix86_veclibabi_string)
2675     {
2676       if (strcmp (ix86_veclibabi_string, "acml") == 0)
2677         ix86_veclib_handler = ix86_veclibabi_acml;
2678       else
2679         error ("unknown vectorization library ABI type (%s) for "
2680                "-mveclibabi= switch", ix86_veclibabi_string);
2681     }
2682
2683   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
2684       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2685       && !optimize_size)
2686     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2687
2688   /* ??? Unwind info is not correct around the CFG unless either a frame
2689      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2690      unwind info generation to be aware of the CFG and propagating states
2691      around edges.  */
2692   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2693        || flag_exceptions || flag_non_call_exceptions)
2694       && flag_omit_frame_pointer
2695       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2696     {
2697       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2698         warning (0, "unwind tables currently require either a frame pointer "
2699                  "or -maccumulate-outgoing-args for correctness");
2700       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2701     }
2702
2703   /* For sane SSE instruction set generation we need fcomi instruction.
2704      It is safe to enable all CMOVE instructions.  */
2705   if (TARGET_SSE)
2706     TARGET_CMOVE = 1;
2707
2708   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2709   {
2710     char *p;
2711     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2712     p = strchr (internal_label_prefix, 'X');
2713     internal_label_prefix_len = p - internal_label_prefix;
2714     *p = '\0';
2715   }
2716
2717   /* When scheduling description is not available, disable scheduler pass
2718      so it won't slow down the compilation and make x87 code slower.  */
2719   if (!TARGET_SCHEDULE)
2720     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2721
2722   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
2723     set_param_value ("simultaneous-prefetches",
2724                      ix86_cost->simultaneous_prefetches);
2725   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
2726     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
2727   if (!PARAM_SET_P (PARAM_L1_CACHE_SIZE))
2728     set_param_value ("l1-cache-size", ix86_cost->l1_cache_size);
2729   if (!PARAM_SET_P (PARAM_L2_CACHE_SIZE))
2730     set_param_value ("l2-cache-size", ix86_cost->l2_cache_size);
2731
2732   /* If using typedef char *va_list, signal that __builtin_va_start (&ap, 0)
2733      can be optimized to ap = __builtin_next_arg (0).  */
2734   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
2735     targetm.expand_builtin_va_start = NULL;
2736 }
2737 \f
2738 /* Return true if this goes in large data/bss.  */
2739
2740 static bool
2741 ix86_in_large_data_p (tree exp)
2742 {
2743   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
2744     return false;
2745
2746   /* Functions are never large data.  */
2747   if (TREE_CODE (exp) == FUNCTION_DECL)
2748     return false;
2749
2750   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
2751     {
2752       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
2753       if (strcmp (section, ".ldata") == 0
2754           || strcmp (section, ".lbss") == 0)
2755         return true;
2756       return false;
2757     }
2758   else
2759     {
2760       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2761
2762       /* If this is an incomplete type with size 0, then we can't put it
2763          in data because it might be too big when completed.  */
2764       if (!size || size > ix86_section_threshold)
2765         return true;
2766     }
2767
2768   return false;
2769 }
2770
2771 /* Switch to the appropriate section for output of DECL.
2772    DECL is either a `VAR_DECL' node or a constant of some sort.
2773    RELOC indicates whether forming the initial value of DECL requires
2774    link-time relocations.  */
2775
2776 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
2777         ATTRIBUTE_UNUSED;
2778
2779 static section *
2780 x86_64_elf_select_section (tree decl, int reloc,
2781                            unsigned HOST_WIDE_INT align)
2782 {
2783   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2784       && ix86_in_large_data_p (decl))
2785     {
2786       const char *sname = NULL;
2787       unsigned int flags = SECTION_WRITE;
2788       switch (categorize_decl_for_section (decl, reloc))
2789         {
2790         case SECCAT_DATA:
2791           sname = ".ldata";
2792           break;
2793         case SECCAT_DATA_REL:
2794           sname = ".ldata.rel";
2795           break;
2796         case SECCAT_DATA_REL_LOCAL:
2797           sname = ".ldata.rel.local";
2798           break;
2799         case SECCAT_DATA_REL_RO:
2800           sname = ".ldata.rel.ro";
2801           break;
2802         case SECCAT_DATA_REL_RO_LOCAL:
2803           sname = ".ldata.rel.ro.local";
2804           break;
2805         case SECCAT_BSS:
2806           sname = ".lbss";
2807           flags |= SECTION_BSS;
2808           break;
2809         case SECCAT_RODATA:
2810         case SECCAT_RODATA_MERGE_STR:
2811         case SECCAT_RODATA_MERGE_STR_INIT:
2812         case SECCAT_RODATA_MERGE_CONST:
2813           sname = ".lrodata";
2814           flags = 0;
2815           break;
2816         case SECCAT_SRODATA:
2817         case SECCAT_SDATA:
2818         case SECCAT_SBSS:
2819           gcc_unreachable ();
2820         case SECCAT_TEXT:
2821         case SECCAT_TDATA:
2822         case SECCAT_TBSS:
2823           /* We don't split these for medium model.  Place them into
2824              default sections and hope for best.  */
2825           break;
2826         }
2827       if (sname)
2828         {
2829           /* We might get called with string constants, but get_named_section
2830              doesn't like them as they are not DECLs.  Also, we need to set
2831              flags in that case.  */
2832           if (!DECL_P (decl))
2833             return get_section (sname, flags, NULL);
2834           return get_named_section (decl, sname, reloc);
2835         }
2836     }
2837   return default_elf_select_section (decl, reloc, align);
2838 }
2839
2840 /* Build up a unique section name, expressed as a
2841    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2842    RELOC indicates whether the initial value of EXP requires
2843    link-time relocations.  */
2844
2845 static void ATTRIBUTE_UNUSED
2846 x86_64_elf_unique_section (tree decl, int reloc)
2847 {
2848   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2849       && ix86_in_large_data_p (decl))
2850     {
2851       const char *prefix = NULL;
2852       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2853       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2854
2855       switch (categorize_decl_for_section (decl, reloc))
2856         {
2857         case SECCAT_DATA:
2858         case SECCAT_DATA_REL:
2859         case SECCAT_DATA_REL_LOCAL:
2860         case SECCAT_DATA_REL_RO:
2861         case SECCAT_DATA_REL_RO_LOCAL:
2862           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2863           break;
2864         case SECCAT_BSS:
2865           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2866           break;
2867         case SECCAT_RODATA:
2868         case SECCAT_RODATA_MERGE_STR:
2869         case SECCAT_RODATA_MERGE_STR_INIT:
2870         case SECCAT_RODATA_MERGE_CONST:
2871           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2872           break;
2873         case SECCAT_SRODATA:
2874         case SECCAT_SDATA:
2875         case SECCAT_SBSS:
2876           gcc_unreachable ();
2877         case SECCAT_TEXT:
2878         case SECCAT_TDATA:
2879         case SECCAT_TBSS:
2880           /* We don't split these for medium model.  Place them into
2881              default sections and hope for best.  */
2882           break;
2883         }
2884       if (prefix)
2885         {
2886           const char *name;
2887           size_t nlen, plen;
2888           char *string;
2889           plen = strlen (prefix);
2890
2891           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2892           name = targetm.strip_name_encoding (name);
2893           nlen = strlen (name);
2894
2895           string = (char *) alloca (nlen + plen + 1);
2896           memcpy (string, prefix, plen);
2897           memcpy (string + plen, name, nlen + 1);
2898
2899           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2900           return;
2901         }
2902     }
2903   default_unique_section (decl, reloc);
2904 }
2905
2906 #ifdef COMMON_ASM_OP
2907 /* This says how to output assembler code to declare an
2908    uninitialized external linkage data object.
2909
2910    For medium model x86-64 we need to use .largecomm opcode for
2911    large objects.  */
2912 void
2913 x86_elf_aligned_common (FILE *file,
2914                         const char *name, unsigned HOST_WIDE_INT size,
2915                         int align)
2916 {
2917   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2918       && size > (unsigned int)ix86_section_threshold)
2919     fprintf (file, ".largecomm\t");
2920   else
2921     fprintf (file, "%s", COMMON_ASM_OP);
2922   assemble_name (file, name);
2923   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2924            size, align / BITS_PER_UNIT);
2925 }
2926 #endif
2927
2928 /* Utility function for targets to use in implementing
2929    ASM_OUTPUT_ALIGNED_BSS.  */
2930
2931 void
2932 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2933                         const char *name, unsigned HOST_WIDE_INT size,
2934                         int align)
2935 {
2936   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2937       && size > (unsigned int)ix86_section_threshold)
2938     switch_to_section (get_named_section (decl, ".lbss", 0));
2939   else
2940     switch_to_section (bss_section);
2941   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2942 #ifdef ASM_DECLARE_OBJECT_NAME
2943   last_assemble_variable_decl = decl;
2944   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2945 #else
2946   /* Standard thing is just output label for the object.  */
2947   ASM_OUTPUT_LABEL (file, name);
2948 #endif /* ASM_DECLARE_OBJECT_NAME */
2949   ASM_OUTPUT_SKIP (file, size ? size : 1);
2950 }
2951 \f
2952 void
2953 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2954 {
2955   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2956      make the problem with not enough registers even worse.  */
2957 #ifdef INSN_SCHEDULING
2958   if (level > 1)
2959     flag_schedule_insns = 0;
2960 #endif
2961
2962   if (TARGET_MACHO)
2963     /* The Darwin libraries never set errno, so we might as well
2964        avoid calling them when that's the only reason we would.  */
2965     flag_errno_math = 0;
2966
2967   /* The default values of these switches depend on the TARGET_64BIT
2968      that is not known at this moment.  Mark these values with 2 and
2969      let user the to override these.  In case there is no command line option
2970      specifying them, we will set the defaults in override_options.  */
2971   if (optimize >= 1)
2972     flag_omit_frame_pointer = 2;
2973   flag_pcc_struct_return = 2;
2974   flag_asynchronous_unwind_tables = 2;
2975   flag_vect_cost_model = 1;
2976 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2977   SUBTARGET_OPTIMIZATION_OPTIONS;
2978 #endif
2979 }
2980 \f
2981 /* Decide whether we can make a sibling call to a function.  DECL is the
2982    declaration of the function being targeted by the call and EXP is the
2983    CALL_EXPR representing the call.  */
2984
2985 static bool
2986 ix86_function_ok_for_sibcall (tree decl, tree exp)
2987 {
2988   tree func;
2989   rtx a, b;
2990
2991   /* If we are generating position-independent code, we cannot sibcall
2992      optimize any indirect call, or a direct call to a global function,
2993      as the PLT requires %ebx be live.  */
2994   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2995     return false;
2996
2997   if (decl)
2998     func = decl;
2999   else
3000     {
3001       func = TREE_TYPE (CALL_EXPR_FN (exp));
3002       if (POINTER_TYPE_P (func))
3003         func = TREE_TYPE (func);
3004     }
3005
3006   /* Check that the return value locations are the same.  Like
3007      if we are returning floats on the 80387 register stack, we cannot
3008      make a sibcall from a function that doesn't return a float to a
3009      function that does or, conversely, from a function that does return
3010      a float to a function that doesn't; the necessary stack adjustment
3011      would not be executed.  This is also the place we notice
3012      differences in the return value ABI.  Note that it is ok for one
3013      of the functions to have void return type as long as the return
3014      value of the other is passed in a register.  */
3015   a = ix86_function_value (TREE_TYPE (exp), func, false);
3016   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
3017                            cfun->decl, false);
3018   if (STACK_REG_P (a) || STACK_REG_P (b))
3019     {
3020       if (!rtx_equal_p (a, b))
3021         return false;
3022     }
3023   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
3024     ;
3025   else if (!rtx_equal_p (a, b))
3026     return false;
3027
3028   /* If this call is indirect, we'll need to be able to use a call-clobbered
3029      register for the address of the target function.  Make sure that all
3030      such registers are not used for passing parameters.  */
3031   if (!decl && !TARGET_64BIT)
3032     {
3033       tree type;
3034
3035       /* We're looking at the CALL_EXPR, we need the type of the function.  */
3036       type = CALL_EXPR_FN (exp);                /* pointer expression */
3037       type = TREE_TYPE (type);                  /* pointer type */
3038       type = TREE_TYPE (type);                  /* function type */
3039
3040       if (ix86_function_regparm (type, NULL) >= 3)
3041         {
3042           /* ??? Need to count the actual number of registers to be used,
3043              not the possible number of registers.  Fix later.  */
3044           return false;
3045         }
3046     }
3047
3048   /* Dllimport'd functions are also called indirectly.  */
3049   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
3050       && decl && DECL_DLLIMPORT_P (decl)
3051       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
3052     return false;
3053
3054   /* If we forced aligned the stack, then sibcalling would unalign the
3055      stack, which may break the called function.  */
3056   if (cfun->machine->force_align_arg_pointer)
3057     return false;
3058
3059   /* Otherwise okay.  That also includes certain types of indirect calls.  */
3060   return true;
3061 }
3062
3063 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
3064    calling convention attributes;
3065    arguments as in struct attribute_spec.handler.  */
3066
3067 static tree
3068 ix86_handle_cconv_attribute (tree *node, tree name,
3069                                    tree args,
3070                                    int flags ATTRIBUTE_UNUSED,
3071                                    bool *no_add_attrs)
3072 {
3073   if (TREE_CODE (*node) != FUNCTION_TYPE
3074       && TREE_CODE (*node) != METHOD_TYPE
3075       && TREE_CODE (*node) != FIELD_DECL
3076       && TREE_CODE (*node) != TYPE_DECL)
3077     {
3078       warning (OPT_Wattributes, "%qs attribute only applies to functions",
3079                IDENTIFIER_POINTER (name));
3080       *no_add_attrs = true;
3081       return NULL_TREE;
3082     }
3083
3084   /* Can combine regparm with all attributes but fastcall.  */
3085   if (is_attribute_p ("regparm", name))
3086     {
3087       tree cst;
3088
3089       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3090         {
3091           error ("fastcall and regparm attributes are not compatible");
3092         }
3093
3094       cst = TREE_VALUE (args);
3095       if (TREE_CODE (cst) != INTEGER_CST)
3096         {
3097           warning (OPT_Wattributes,
3098                    "%qs attribute requires an integer constant argument",
3099                    IDENTIFIER_POINTER (name));
3100           *no_add_attrs = true;
3101         }
3102       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
3103         {
3104           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
3105                    IDENTIFIER_POINTER (name), REGPARM_MAX);
3106           *no_add_attrs = true;
3107         }
3108
3109       if (!TARGET_64BIT
3110           && lookup_attribute (ix86_force_align_arg_pointer_string,
3111                                TYPE_ATTRIBUTES (*node))
3112           && compare_tree_int (cst, REGPARM_MAX-1))
3113         {
3114           error ("%s functions limited to %d register parameters",
3115                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
3116         }
3117
3118       return NULL_TREE;
3119     }
3120
3121   if (TARGET_64BIT)
3122     {
3123       /* Do not warn when emulating the MS ABI.  */
3124       if (!TARGET_64BIT_MS_ABI)
3125         warning (OPT_Wattributes, "%qs attribute ignored",
3126                  IDENTIFIER_POINTER (name));
3127       *no_add_attrs = true;
3128       return NULL_TREE;
3129     }
3130
3131   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
3132   if (is_attribute_p ("fastcall", name))
3133     {
3134       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
3135         {
3136           error ("fastcall and cdecl attributes are not compatible");
3137         }
3138       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
3139         {
3140           error ("fastcall and stdcall attributes are not compatible");
3141         }
3142       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
3143         {
3144           error ("fastcall and regparm attributes are not compatible");
3145         }
3146     }
3147
3148   /* Can combine stdcall with fastcall (redundant), regparm and
3149      sseregparm.  */
3150   else if (is_attribute_p ("stdcall", name))
3151     {
3152       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
3153         {
3154           error ("stdcall and cdecl attributes are not compatible");
3155         }
3156       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3157         {
3158           error ("stdcall and fastcall attributes are not compatible");
3159         }
3160     }
3161
3162   /* Can combine cdecl with regparm and sseregparm.  */
3163   else if (is_attribute_p ("cdecl", name))
3164     {
3165       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
3166         {
3167           error ("stdcall and cdecl attributes are not compatible");
3168         }
3169       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
3170         {
3171           error ("fastcall and cdecl attributes are not compatible");
3172         }
3173     }
3174
3175   /* Can combine sseregparm with all attributes.  */
3176
3177   return NULL_TREE;
3178 }
3179
3180 /* Return 0 if the attributes for two types are incompatible, 1 if they
3181    are compatible, and 2 if they are nearly compatible (which causes a
3182    warning to be generated).  */
3183
3184 static int
3185 ix86_comp_type_attributes (const_tree type1, const_tree type2)
3186 {
3187   /* Check for mismatch of non-default calling convention.  */
3188   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
3189
3190   if (TREE_CODE (type1) != FUNCTION_TYPE
3191       && TREE_CODE (type1) != METHOD_TYPE)
3192     return 1;
3193
3194   /* Check for mismatched fastcall/regparm types.  */
3195   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
3196        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
3197       || (ix86_function_regparm (type1, NULL)
3198           != ix86_function_regparm (type2, NULL)))
3199     return 0;
3200
3201   /* Check for mismatched sseregparm types.  */
3202   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
3203       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
3204     return 0;
3205
3206   /* Check for mismatched return types (cdecl vs stdcall).  */
3207   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
3208       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
3209     return 0;
3210
3211   return 1;
3212 }
3213 \f
3214 /* Return the regparm value for a function with the indicated TYPE and DECL.
3215    DECL may be NULL when calling function indirectly
3216    or considering a libcall.  */
3217
3218 static int
3219 ix86_function_regparm (const_tree type, const_tree decl)
3220 {
3221   tree attr;
3222   int regparm = ix86_regparm;
3223
3224   if (TARGET_64BIT)
3225     return regparm;
3226
3227   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
3228   if (attr)
3229     return TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
3230
3231   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
3232     return 2;
3233
3234   /* Use register calling convention for local functions when possible.  */
3235   if (decl && TREE_CODE (decl) == FUNCTION_DECL
3236       && flag_unit_at_a_time && !profile_flag)
3237     {
3238       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
3239       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3240       if (i && i->local)
3241         {
3242           int local_regparm, globals = 0, regno;
3243           struct function *f;
3244
3245           /* Make sure no regparm register is taken by a
3246              fixed register variable.  */
3247           for (local_regparm = 0; local_regparm < REGPARM_MAX; local_regparm++)
3248             if (fixed_regs[local_regparm])
3249               break;
3250
3251           /* We can't use regparm(3) for nested functions as these use
3252              static chain pointer in third argument.  */
3253           if (local_regparm == 3
3254               && (decl_function_context (decl)
3255                   || ix86_force_align_arg_pointer)
3256               && !DECL_NO_STATIC_CHAIN (decl))
3257             local_regparm = 2;
3258
3259           /* If the function realigns its stackpointer, the prologue will
3260              clobber %ecx.  If we've already generated code for the callee,
3261              the callee DECL_STRUCT_FUNCTION is gone, so we fall back to
3262              scanning the attributes for the self-realigning property.  */
3263           f = DECL_STRUCT_FUNCTION (decl);
3264           if (local_regparm == 3
3265               && (f ? !!f->machine->force_align_arg_pointer
3266                   : !!lookup_attribute (ix86_force_align_arg_pointer_string,
3267                                         TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
3268             local_regparm = 2;
3269
3270           /* Each fixed register usage increases register pressure,
3271              so less registers should be used for argument passing.
3272              This functionality can be overriden by an explicit
3273              regparm value.  */
3274           for (regno = 0; regno <= DI_REG; regno++)
3275             if (fixed_regs[regno])
3276               globals++;
3277
3278           local_regparm
3279             = globals < local_regparm ? local_regparm - globals : 0;
3280
3281           if (local_regparm > regparm)
3282             regparm = local_regparm;
3283         }
3284     }
3285
3286   return regparm;
3287 }
3288
3289 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
3290    DFmode (2) arguments in SSE registers for a function with the
3291    indicated TYPE and DECL.  DECL may be NULL when calling function
3292    indirectly or considering a libcall.  Otherwise return 0.  */
3293
3294 static int
3295 ix86_function_sseregparm (const_tree type, const_tree decl, bool warn)
3296 {
3297   gcc_assert (!TARGET_64BIT);
3298
3299   /* Use SSE registers to pass SFmode and DFmode arguments if requested
3300      by the sseregparm attribute.  */
3301   if (TARGET_SSEREGPARM
3302       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
3303     {
3304       if (!TARGET_SSE)
3305         {
3306           if (warn)
3307             {
3308               if (decl)
3309                 error ("Calling %qD with attribute sseregparm without "
3310                        "SSE/SSE2 enabled", decl);
3311               else
3312                 error ("Calling %qT with attribute sseregparm without "
3313                        "SSE/SSE2 enabled", type);
3314             }
3315           return 0;
3316         }
3317
3318       return 2;
3319     }
3320
3321   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
3322      (and DFmode for SSE2) arguments in SSE registers.  */
3323   if (decl && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
3324     {
3325       /* FIXME: remove this CONST_CAST when cgraph.[ch] is constified.  */
3326       struct cgraph_local_info *i = cgraph_local_info (CONST_CAST_TREE(decl));
3327       if (i && i->local)
3328         return TARGET_SSE2 ? 2 : 1;
3329     }
3330
3331   return 0;
3332 }
3333
3334 /* Return true if EAX is live at the start of the function.  Used by
3335    ix86_expand_prologue to determine if we need special help before
3336    calling allocate_stack_worker.  */
3337
3338 static bool
3339 ix86_eax_live_at_start_p (void)
3340 {
3341   /* Cheat.  Don't bother working forward from ix86_function_regparm
3342      to the function type to whether an actual argument is located in
3343      eax.  Instead just look at cfg info, which is still close enough
3344      to correct at this point.  This gives false positives for broken
3345      functions that might use uninitialized data that happens to be
3346      allocated in eax, but who cares?  */
3347   return REGNO_REG_SET_P (df_get_live_out (ENTRY_BLOCK_PTR), 0);
3348 }
3349
3350 /* Value is the number of bytes of arguments automatically
3351    popped when returning from a subroutine call.
3352    FUNDECL is the declaration node of the function (as a tree),
3353    FUNTYPE is the data type of the function (as a tree),
3354    or for a library call it is an identifier node for the subroutine name.
3355    SIZE is the number of bytes of arguments passed on the stack.
3356
3357    On the 80386, the RTD insn may be used to pop them if the number
3358      of args is fixed, but if the number is variable then the caller
3359      must pop them all.  RTD can't be used for library calls now
3360      because the library is compiled with the Unix compiler.
3361    Use of RTD is a selectable option, since it is incompatible with
3362    standard Unix calling sequences.  If the option is not selected,
3363    the caller must always pop the args.
3364
3365    The attribute stdcall is equivalent to RTD on a per module basis.  */
3366
3367 int
3368 ix86_return_pops_args (tree fundecl, tree funtype, int size)
3369 {
3370   int rtd;
3371
3372   /* None of the 64-bit ABIs pop arguments.  */
3373   if (TARGET_64BIT)
3374     return 0;
3375
3376   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
3377
3378   /* Cdecl functions override -mrtd, and never pop the stack.  */
3379   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
3380     {
3381       /* Stdcall and fastcall functions will pop the stack if not
3382          variable args.  */
3383       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
3384           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
3385         rtd = 1;
3386
3387       if (rtd && ! stdarg_p (funtype))
3388         return size;
3389     }
3390
3391   /* Lose any fake structure return argument if it is passed on the stack.  */
3392   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
3393       && !KEEP_AGGREGATE_RETURN_POINTER)
3394     {
3395       int nregs = ix86_function_regparm (funtype, fundecl);
3396       if (nregs == 0)
3397         return GET_MODE_SIZE (Pmode);
3398     }
3399
3400   return 0;
3401 }
3402 \f
3403 /* Argument support functions.  */
3404
3405 /* Return true when register may be used to pass function parameters.  */
3406 bool
3407 ix86_function_arg_regno_p (int regno)
3408 {
3409   int i;
3410   const int *parm_regs;
3411
3412   if (!TARGET_64BIT)
3413     {
3414       if (TARGET_MACHO)
3415         return (regno < REGPARM_MAX
3416                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
3417       else
3418         return (regno < REGPARM_MAX
3419                 || (TARGET_MMX && MMX_REGNO_P (regno)
3420                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
3421                 || (TARGET_SSE && SSE_REGNO_P (regno)
3422                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
3423     }
3424
3425   if (TARGET_MACHO)
3426     {
3427       if (SSE_REGNO_P (regno) && TARGET_SSE)
3428         return true;
3429     }
3430   else
3431     {
3432       if (TARGET_SSE && SSE_REGNO_P (regno)
3433           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
3434         return true;
3435     }
3436
3437   /* RAX is used as hidden argument to va_arg functions.  */
3438   if (!TARGET_64BIT_MS_ABI && regno == AX_REG)
3439     return true;
3440
3441   if (TARGET_64BIT_MS_ABI)
3442     parm_regs = x86_64_ms_abi_int_parameter_registers;
3443   else
3444     parm_regs = x86_64_int_parameter_registers;
3445   for (i = 0; i < REGPARM_MAX; i++)
3446     if (regno == parm_regs[i])
3447       return true;
3448   return false;
3449 }
3450
3451 /* Return if we do not know how to pass TYPE solely in registers.  */
3452
3453 static bool
3454 ix86_must_pass_in_stack (enum machine_mode mode, const_tree type)
3455 {
3456   if (must_pass_in_stack_var_size_or_pad (mode, type))
3457     return true;
3458
3459   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
3460      The layout_type routine is crafty and tries to trick us into passing
3461      currently unsupported vector types on the stack by using TImode.  */
3462   return (!TARGET_64BIT && mode == TImode
3463           && type && TREE_CODE (type) != VECTOR_TYPE);
3464 }
3465
3466 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3467    for a call to a function whose data type is FNTYPE.
3468    For a library call, FNTYPE is 0.  */
3469
3470 void
3471 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
3472                       tree fntype,      /* tree ptr for function decl */
3473                       rtx libname,      /* SYMBOL_REF of library name or 0 */
3474                       tree fndecl)
3475 {
3476   struct cgraph_local_info *i = fndecl ? cgraph_local_info (fndecl) : NULL;
3477   memset (cum, 0, sizeof (*cum));
3478
3479   /* Set up the number of registers to use for passing arguments.  */
3480   cum->nregs = ix86_regparm;
3481   if (TARGET_SSE)
3482     cum->sse_nregs = SSE_REGPARM_MAX;
3483   if (TARGET_MMX)
3484     cum->mmx_nregs = MMX_REGPARM_MAX;
3485   cum->warn_sse = true;
3486   cum->warn_mmx = true;
3487
3488   /* Because type might mismatch in between caller and callee, we need to
3489      use actual type of function for local calls.
3490      FIXME: cgraph_analyze can be told to actually record if function uses
3491      va_start so for local functions maybe_vaarg can be made aggressive
3492      helping K&R code.
3493      FIXME: once typesytem is fixed, we won't need this code anymore.  */
3494   if (i && i->local)
3495     fntype = TREE_TYPE (fndecl);
3496   cum->maybe_vaarg = (fntype
3497                       ? (!prototype_p (fntype) || stdarg_p (fntype))
3498                       : !libname);
3499
3500   if (!TARGET_64BIT)
3501     {
3502       /* If there are variable arguments, then we won't pass anything
3503          in registers in 32-bit mode. */
3504       if (cum->maybe_vaarg)
3505         {
3506           cum->nregs = 0;
3507           cum->sse_nregs = 0;
3508           cum->mmx_nregs = 0;
3509           cum->warn_sse = 0;
3510           cum->warn_mmx = 0;
3511           return;
3512         }
3513
3514       /* Use ecx and edx registers if function has fastcall attribute,
3515          else look for regparm information.  */
3516       if (fntype)
3517         {
3518           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
3519             {
3520               cum->nregs = 2;
3521               cum->fastcall = 1;
3522             }
3523           else
3524             cum->nregs = ix86_function_regparm (fntype, fndecl);
3525         }
3526
3527       /* Set up the number of SSE registers used for passing SFmode
3528          and DFmode arguments.  Warn for mismatching ABI.  */
3529       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl, true);
3530     }
3531 }
3532
3533 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
3534    But in the case of vector types, it is some vector mode.
3535
3536    When we have only some of our vector isa extensions enabled, then there
3537    are some modes for which vector_mode_supported_p is false.  For these
3538    modes, the generic vector support in gcc will choose some non-vector mode
3539    in order to implement the type.  By computing the natural mode, we'll
3540    select the proper ABI location for the operand and not depend on whatever
3541    the middle-end decides to do with these vector types.  */
3542
3543 static enum machine_mode
3544 type_natural_mode (const_tree type)
3545 {
3546   enum machine_mode mode = TYPE_MODE (type);
3547
3548   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
3549     {
3550       HOST_WIDE_INT size = int_size_in_bytes (type);
3551       if ((size == 8 || size == 16)
3552           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
3553           && TYPE_VECTOR_SUBPARTS (type) > 1)
3554         {
3555           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
3556
3557           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
3558             mode = MIN_MODE_VECTOR_FLOAT;
3559           else
3560             mode = MIN_MODE_VECTOR_INT;
3561
3562           /* Get the mode which has this inner mode and number of units.  */
3563           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3564             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
3565                 && GET_MODE_INNER (mode) == innermode)
3566               return mode;
3567
3568           gcc_unreachable ();
3569         }
3570     }
3571
3572   return mode;
3573 }
3574
3575 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
3576    this may not agree with the mode that the type system has chosen for the
3577    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
3578    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
3579
3580 static rtx
3581 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
3582                      unsigned int regno)
3583 {
3584   rtx tmp;
3585
3586   if (orig_mode != BLKmode)
3587     tmp = gen_rtx_REG (orig_mode, regno);
3588   else
3589     {
3590       tmp = gen_rtx_REG (mode, regno);
3591       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
3592       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
3593     }
3594
3595   return tmp;
3596 }
3597
3598 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
3599    of this code is to classify each 8bytes of incoming argument by the register
3600    class and assign registers accordingly.  */
3601
3602 /* Return the union class of CLASS1 and CLASS2.
3603    See the x86-64 PS ABI for details.  */
3604
3605 static enum x86_64_reg_class
3606 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
3607 {
3608   /* Rule #1: If both classes are equal, this is the resulting class.  */
3609   if (class1 == class2)
3610     return class1;
3611
3612   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
3613      the other class.  */
3614   if (class1 == X86_64_NO_CLASS)
3615     return class2;
3616   if (class2 == X86_64_NO_CLASS)
3617     return class1;
3618
3619   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
3620   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
3621     return X86_64_MEMORY_CLASS;
3622
3623   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
3624   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
3625       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
3626     return X86_64_INTEGERSI_CLASS;
3627   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
3628       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
3629     return X86_64_INTEGER_CLASS;
3630
3631   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
3632      MEMORY is used.  */
3633   if (class1 == X86_64_X87_CLASS
3634       || class1 == X86_64_X87UP_CLASS
3635       || class1 == X86_64_COMPLEX_X87_CLASS
3636       || class2 == X86_64_X87_CLASS
3637       || class2 == X86_64_X87UP_CLASS
3638       || class2 == X86_64_COMPLEX_X87_CLASS)
3639     return X86_64_MEMORY_CLASS;
3640
3641   /* Rule #6: Otherwise class SSE is used.  */
3642   return X86_64_SSE_CLASS;
3643 }
3644
3645 /* Classify the argument of type TYPE and mode MODE.
3646    CLASSES will be filled by the register class used to pass each word
3647    of the operand.  The number of words is returned.  In case the parameter
3648    should be passed in memory, 0 is returned. As a special case for zero
3649    sized containers, classes[0] will be NO_CLASS and 1 is returned.
3650
3651    BIT_OFFSET is used internally for handling records and specifies offset
3652    of the offset in bits modulo 256 to avoid overflow cases.
3653
3654    See the x86-64 PS ABI for details.
3655 */
3656
3657 static int
3658 classify_argument (enum machine_mode mode, const_tree type,
3659                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
3660 {
3661   HOST_WIDE_INT bytes =
3662     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3663   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3664
3665   /* Variable sized entities are always passed/returned in memory.  */
3666   if (bytes < 0)
3667     return 0;
3668
3669   if (mode != VOIDmode
3670       && targetm.calls.must_pass_in_stack (mode, type))
3671     return 0;
3672
3673   if (type && AGGREGATE_TYPE_P (type))
3674     {
3675       int i;
3676       tree field;
3677       enum x86_64_reg_class subclasses[MAX_CLASSES];
3678
3679       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3680       if (bytes > 16)
3681         return 0;
3682
3683       for (i = 0; i < words; i++)
3684         classes[i] = X86_64_NO_CLASS;
3685
3686       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3687          signalize memory class, so handle it as special case.  */
3688       if (!words)
3689         {
3690           classes[0] = X86_64_NO_CLASS;
3691           return 1;
3692         }
3693
3694       /* Classify each field of record and merge classes.  */
3695       switch (TREE_CODE (type))
3696         {
3697         case RECORD_TYPE:
3698           /* And now merge the fields of structure.  */
3699           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3700             {
3701               if (TREE_CODE (field) == FIELD_DECL)
3702                 {
3703                   int num;
3704
3705                   if (TREE_TYPE (field) == error_mark_node)
3706                     continue;
3707
3708                   /* Bitfields are always classified as integer.  Handle them
3709                      early, since later code would consider them to be
3710                      misaligned integers.  */
3711                   if (DECL_BIT_FIELD (field))
3712                     {
3713                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3714                            i < ((int_bit_position (field) + (bit_offset % 64))
3715                                 + tree_low_cst (DECL_SIZE (field), 0)
3716                                 + 63) / 8 / 8; i++)
3717                         classes[i] =
3718                           merge_classes (X86_64_INTEGER_CLASS,
3719                                          classes[i]);
3720                     }
3721                   else
3722                     {
3723                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3724                                                TREE_TYPE (field), subclasses,
3725                                                (int_bit_position (field)
3726                                                 + bit_offset) % 256);
3727                       if (!num)
3728                         return 0;
3729                       for (i = 0; i < num; i++)
3730                         {
3731                           int pos =
3732                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3733                           classes[i + pos] =
3734                             merge_classes (subclasses[i], classes[i + pos]);
3735                         }
3736                     }
3737                 }
3738             }
3739           break;
3740
3741         case ARRAY_TYPE:
3742           /* Arrays are handled as small records.  */
3743           {
3744             int num;
3745             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3746                                      TREE_TYPE (type), subclasses, bit_offset);
3747             if (!num)
3748               return 0;
3749
3750             /* The partial classes are now full classes.  */
3751             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3752               subclasses[0] = X86_64_SSE_CLASS;
3753             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3754               subclasses[0] = X86_64_INTEGER_CLASS;
3755
3756             for (i = 0; i < words; i++)
3757               classes[i] = subclasses[i % num];
3758
3759             break;
3760           }
3761         case UNION_TYPE:
3762         case QUAL_UNION_TYPE:
3763           /* Unions are similar to RECORD_TYPE but offset is always 0.
3764              */
3765           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3766             {
3767               if (TREE_CODE (field) == FIELD_DECL)
3768                 {
3769                   int num;
3770
3771                   if (TREE_TYPE (field) == error_mark_node)
3772                     continue;
3773
3774                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3775                                            TREE_TYPE (field), subclasses,
3776                                            bit_offset);
3777                   if (!num)
3778                     return 0;
3779                   for (i = 0; i < num; i++)
3780                     classes[i] = merge_classes (subclasses[i], classes[i]);
3781                 }
3782             }
3783           break;
3784
3785         default:
3786           gcc_unreachable ();
3787         }
3788
3789       /* Final merger cleanup.  */
3790       for (i = 0; i < words; i++)
3791         {
3792           /* If one class is MEMORY, everything should be passed in
3793              memory.  */
3794           if (classes[i] == X86_64_MEMORY_CLASS)
3795             return 0;
3796
3797           /* The X86_64_SSEUP_CLASS should be always preceded by
3798              X86_64_SSE_CLASS.  */
3799           if (classes[i] == X86_64_SSEUP_CLASS
3800               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3801             classes[i] = X86_64_SSE_CLASS;
3802
3803           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3804           if (classes[i] == X86_64_X87UP_CLASS
3805               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3806             classes[i] = X86_64_SSE_CLASS;
3807         }
3808       return words;
3809     }
3810
3811   /* Compute alignment needed.  We align all types to natural boundaries with
3812      exception of XFmode that is aligned to 64bits.  */
3813   if (mode != VOIDmode && mode != BLKmode)
3814     {
3815       int mode_alignment = GET_MODE_BITSIZE (mode);
3816
3817       if (mode == XFmode)
3818         mode_alignment = 128;
3819       else if (mode == XCmode)
3820         mode_alignment = 256;
3821       if (COMPLEX_MODE_P (mode))
3822         mode_alignment /= 2;
3823       /* Misaligned fields are always returned in memory.  */
3824       if (bit_offset % mode_alignment)
3825         return 0;
3826     }
3827
3828   /* for V1xx modes, just use the base mode */
3829   if (VECTOR_MODE_P (mode)
3830       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3831     mode = GET_MODE_INNER (mode);
3832
3833   /* Classification of atomic types.  */
3834   switch (mode)
3835     {
3836     case SDmode:
3837     case DDmode:
3838       classes[0] = X86_64_SSE_CLASS;
3839       return 1;
3840     case TDmode:
3841       classes[0] = X86_64_SSE_CLASS;
3842       classes[1] = X86_64_SSEUP_CLASS;
3843       return 2;
3844     case DImode:
3845     case SImode:
3846     case HImode:
3847     case QImode:
3848     case CSImode:
3849     case CHImode:
3850     case CQImode:
3851       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3852         classes[0] = X86_64_INTEGERSI_CLASS;
3853       else
3854         classes[0] = X86_64_INTEGER_CLASS;
3855       return 1;
3856     case CDImode:
3857     case TImode:
3858       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3859       return 2;
3860     case CTImode:
3861       return 0;
3862     case SFmode:
3863       if (!(bit_offset % 64))
3864         classes[0] = X86_64_SSESF_CLASS;
3865       else
3866         classes[0] = X86_64_SSE_CLASS;
3867       return 1;
3868     case DFmode:
3869       classes[0] = X86_64_SSEDF_CLASS;
3870       return 1;
3871     case XFmode:
3872       classes[0] = X86_64_X87_CLASS;
3873       classes[1] = X86_64_X87UP_CLASS;
3874       return 2;
3875     case TFmode:
3876       classes[0] = X86_64_SSE_CLASS;
3877       classes[1] = X86_64_SSEUP_CLASS;
3878       return 2;
3879     case SCmode:
3880       classes[0] = X86_64_SSE_CLASS;
3881       return 1;
3882     case DCmode:
3883       classes[0] = X86_64_SSEDF_CLASS;
3884       classes[1] = X86_64_SSEDF_CLASS;
3885       return 2;
3886     case XCmode:
3887       classes[0] = X86_64_COMPLEX_X87_CLASS;
3888       return 1;
3889     case TCmode:
3890       /* This modes is larger than 16 bytes.  */
3891       return 0;
3892     case V4SFmode:
3893     case V4SImode:
3894     case V16QImode:
3895     case V8HImode:
3896     case V2DFmode:
3897     case V2DImode:
3898       classes[0] = X86_64_SSE_CLASS;
3899       classes[1] = X86_64_SSEUP_CLASS;
3900       return 2;
3901     case V2SFmode:
3902     case V2SImode:
3903     case V4HImode:
3904     case V8QImode:
3905       classes[0] = X86_64_SSE_CLASS;
3906       return 1;
3907     case BLKmode:
3908     case VOIDmode:
3909       return 0;
3910     default:
3911       gcc_assert (VECTOR_MODE_P (mode));
3912
3913       if (bytes > 16)
3914         return 0;
3915
3916       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3917
3918       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3919         classes[0] = X86_64_INTEGERSI_CLASS;
3920       else
3921         classes[0] = X86_64_INTEGER_CLASS;
3922       classes[1] = X86_64_INTEGER_CLASS;
3923       return 1 + (bytes > 8);
3924     }
3925 }
3926
3927 /* Examine the argument and return set number of register required in each
3928    class.  Return 0 iff parameter should be passed in memory.  */
3929 static int
3930 examine_argument (enum machine_mode mode, const_tree type, int in_return,
3931                   int *int_nregs, int *sse_nregs)
3932 {
3933   enum x86_64_reg_class regclass[MAX_CLASSES];
3934   int n = classify_argument (mode, type, regclass, 0);
3935
3936   *int_nregs = 0;
3937   *sse_nregs = 0;
3938   if (!n)
3939     return 0;
3940   for (n--; n >= 0; n--)
3941     switch (regclass[n])
3942       {
3943       case X86_64_INTEGER_CLASS:
3944       case X86_64_INTEGERSI_CLASS:
3945         (*int_nregs)++;
3946         break;
3947       case X86_64_SSE_CLASS:
3948       case X86_64_SSESF_CLASS:
3949       case X86_64_SSEDF_CLASS:
3950         (*sse_nregs)++;
3951         break;
3952       case X86_64_NO_CLASS:
3953       case X86_64_SSEUP_CLASS:
3954         break;
3955       case X86_64_X87_CLASS:
3956       case X86_64_X87UP_CLASS:
3957         if (!in_return)
3958           return 0;
3959         break;
3960       case X86_64_COMPLEX_X87_CLASS:
3961         return in_return ? 2 : 0;
3962       case X86_64_MEMORY_CLASS:
3963         gcc_unreachable ();
3964       }
3965   return 1;
3966 }
3967
3968 /* Construct container for the argument used by GCC interface.  See
3969    FUNCTION_ARG for the detailed description.  */
3970
3971 static rtx
3972 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3973                      const_tree type, int in_return, int nintregs, int nsseregs,
3974                      const int *intreg, int sse_regno)
3975 {
3976   /* The following variables hold the static issued_error state.  */
3977   static bool issued_sse_arg_error;
3978   static bool issued_sse_ret_error;
3979   static bool issued_x87_ret_error;
3980
3981   enum machine_mode tmpmode;
3982   int bytes =
3983     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3984   enum x86_64_reg_class regclass[MAX_CLASSES];
3985   int n;
3986   int i;
3987   int nexps = 0;
3988   int needed_sseregs, needed_intregs;
3989   rtx exp[MAX_CLASSES];
3990   rtx ret;
3991
3992   n = classify_argument (mode, type, regclass, 0);
3993   if (!n)
3994     return NULL;
3995   if (!examine_argument (mode, type, in_return, &needed_intregs,
3996                          &needed_sseregs))
3997     return NULL;
3998   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3999     return NULL;
4000
4001   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
4002      some less clueful developer tries to use floating-point anyway.  */
4003   if (needed_sseregs && !TARGET_SSE)
4004     {
4005       if (in_return)
4006         {
4007           if (!issued_sse_ret_error)
4008             {
4009               error ("SSE register return with SSE disabled");
4010               issued_sse_ret_error = true;
4011             }
4012         }
4013       else if (!issued_sse_arg_error)
4014         {
4015           error ("SSE register argument with SSE disabled");
4016           issued_sse_arg_error = true;
4017         }
4018       return NULL;
4019     }
4020
4021   /* Likewise, error if the ABI requires us to return values in the
4022      x87 registers and the user specified -mno-80387.  */
4023   if (!TARGET_80387 && in_return)
4024     for (i = 0; i < n; i++)
4025       if (regclass[i] == X86_64_X87_CLASS
4026           || regclass[i] == X86_64_X87UP_CLASS
4027           || regclass[i] == X86_64_COMPLEX_X87_CLASS)
4028         {
4029           if (!issued_x87_ret_error)
4030             {
4031               error ("x87 register return with x87 disabled");
4032               issued_x87_ret_error = true;
4033             }
4034           return NULL;
4035         }
4036
4037   /* First construct simple cases.  Avoid SCmode, since we want to use
4038      single register to pass this type.  */
4039   if (n == 1 && mode != SCmode)
4040     switch (regclass[0])
4041       {
4042       case X86_64_INTEGER_CLASS:
4043       case X86_64_INTEGERSI_CLASS:
4044         return gen_rtx_REG (mode, intreg[0]);
4045       case X86_64_SSE_CLASS:
4046       case X86_64_SSESF_CLASS:
4047       case X86_64_SSEDF_CLASS:
4048         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
4049       case X86_64_X87_CLASS:
4050       case X86_64_COMPLEX_X87_CLASS:
4051         return gen_rtx_REG (mode, FIRST_STACK_REG);
4052       case X86_64_NO_CLASS:
4053         /* Zero sized array, struct or class.  */
4054         return NULL;
4055       default:
4056         gcc_unreachable ();
4057       }
4058   if (n == 2 && regclass[0] == X86_64_SSE_CLASS
4059       && regclass[1] == X86_64_SSEUP_CLASS && mode != BLKmode)
4060     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
4061
4062   if (n == 2
4063       && regclass[0] == X86_64_X87_CLASS && regclass[1] == X86_64_X87UP_CLASS)
4064     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
4065   if (n == 2 && regclass[0] == X86_64_INTEGER_CLASS
4066       && regclass[1] == X86_64_INTEGER_CLASS
4067       && (mode == CDImode || mode == TImode || mode == TFmode)
4068       && intreg[0] + 1 == intreg[1])
4069     return gen_rtx_REG (mode, intreg[0]);
4070
4071   /* Otherwise figure out the entries of the PARALLEL.  */
4072   for (i = 0; i < n; i++)
4073     {
4074       switch (regclass[i])
4075         {
4076           case X86_64_NO_CLASS:
4077             break;
4078           case X86_64_INTEGER_CLASS:
4079           case X86_64_INTEGERSI_CLASS:
4080             /* Merge TImodes on aligned occasions here too.  */
4081             if (i * 8 + 8 > bytes)
4082               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
4083             else if (regclass[i] == X86_64_INTEGERSI_CLASS)
4084               tmpmode = SImode;
4085             else
4086               tmpmode = DImode;
4087             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
4088             if (tmpmode == BLKmode)
4089               tmpmode = DImode;
4090             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
4091                                                gen_rtx_REG (tmpmode, *intreg),
4092                                                GEN_INT (i*8));
4093             intreg++;
4094             break;
4095           case X86_64_SSESF_CLASS:
4096             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
4097                                                gen_rtx_REG (SFmode,
4098                                                             SSE_REGNO (sse_regno)),
4099                                                GEN_INT (i*8));
4100             sse_regno++;
4101             break;
4102           case X86_64_SSEDF_CLASS:
4103             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
4104                                                gen_rtx_REG (DFmode,
4105                                                             SSE_REGNO (sse_regno)),
4106                                                GEN_INT (i*8));
4107             sse_regno++;
4108             break;
4109           case X86_64_SSE_CLASS:
4110             if (i < n - 1 && regclass[i + 1] == X86_64_SSEUP_CLASS)
4111               tmpmode = TImode;
4112             else
4113               tmpmode = DImode;
4114             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
4115                                                gen_rtx_REG (tmpmode,
4116                                                             SSE_REGNO (sse_regno)),
4117                                                GEN_INT (i*8));
4118             if (tmpmode == TImode)
4119               i++;
4120             sse_regno++;
4121             break;
4122           default:
4123             gcc_unreachable ();
4124         }
4125     }
4126
4127   /* Empty aligned struct, union or class.  */
4128   if (nexps == 0)
4129     return NULL;
4130
4131   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
4132   for (i = 0; i < nexps; i++)
4133     XVECEXP (ret, 0, i) = exp [i];
4134   return ret;
4135 }
4136
4137 /* Update the data in CUM to advance over an argument of mode MODE
4138    and data type TYPE.  (TYPE is null for libcalls where that information
4139    may not be available.)  */
4140
4141 static void
4142 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4143                          tree type, HOST_WIDE_INT bytes, HOST_WIDE_INT words)
4144 {
4145   switch (mode)
4146     {
4147     default:
4148       break;
4149
4150     case BLKmode:
4151       if (bytes < 0)
4152         break;
4153       /* FALLTHRU */
4154
4155     case DImode:
4156     case SImode:
4157     case HImode:
4158     case QImode:
4159       cum->words += words;
4160       cum->nregs -= words;
4161       cum->regno += words;
4162
4163       if (cum->nregs <= 0)
4164         {
4165           cum->nregs = 0;
4166           cum->regno = 0;
4167         }
4168       break;
4169
4170     case DFmode:
4171       if (cum->float_in_sse < 2)
4172         break;
4173     case SFmode:
4174       if (cum->float_in_sse < 1)
4175         break;
4176       /* FALLTHRU */
4177
4178     case TImode:
4179     case V16QImode:
4180     case V8HImode:
4181     case V4SImode:
4182     case V2DImode:
4183     case V4SFmode:
4184     case V2DFmode:
4185       if (!type || !AGGREGATE_TYPE_P (type))
4186         {
4187           cum->sse_words += words;
4188           cum->sse_nregs -= 1;
4189           cum->sse_regno += 1;
4190           if (cum->sse_nregs <= 0)
4191             {
4192               cum->sse_nregs = 0;
4193               cum->sse_regno = 0;
4194             }
4195         }
4196       break;
4197
4198     case V8QImode:
4199     case V4HImode:
4200     case V2SImode:
4201     case V2SFmode:
4202       if (!type || !AGGREGATE_TYPE_P (type))
4203         {
4204           cum->mmx_words += words;
4205           cum->mmx_nregs -= 1;
4206           cum->mmx_regno += 1;
4207           if (cum->mmx_nregs <= 0)
4208             {
4209               cum->mmx_nregs = 0;
4210               cum->mmx_regno = 0;
4211             }
4212         }
4213       break;
4214     }
4215 }
4216
4217 static void
4218 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4219                          tree type, HOST_WIDE_INT words)
4220 {
4221   int int_nregs, sse_nregs;
4222
4223   if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
4224     cum->words += words;
4225   else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
4226     {
4227       cum->nregs -= int_nregs;
4228       cum->sse_nregs -= sse_nregs;
4229       cum->regno += int_nregs;
4230       cum->sse_regno += sse_nregs;
4231     }
4232   else
4233     cum->words += words;
4234 }
4235
4236 static void
4237 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
4238                             HOST_WIDE_INT words)
4239 {
4240   /* Otherwise, this should be passed indirect.  */
4241   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
4242
4243   cum->words += words;
4244   if (cum->nregs > 0)
4245     {
4246       cum->nregs -= 1;
4247       cum->regno += 1;
4248     }
4249 }
4250
4251 void
4252 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4253                       tree type, int named ATTRIBUTE_UNUSED)
4254 {
4255   HOST_WIDE_INT bytes, words;
4256
4257   if (mode == BLKmode)
4258     bytes = int_size_in_bytes (type);
4259   else
4260     bytes = GET_MODE_SIZE (mode);
4261   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4262
4263   if (type)
4264     mode = type_natural_mode (type);
4265
4266   if (TARGET_64BIT_MS_ABI)
4267     function_arg_advance_ms_64 (cum, bytes, words);
4268   else if (TARGET_64BIT)
4269     function_arg_advance_64 (cum, mode, type, words);
4270   else
4271     function_arg_advance_32 (cum, mode, type, bytes, words);
4272 }
4273
4274 /* Define where to put the arguments to a function.
4275    Value is zero to push the argument on the stack,
4276    or a hard register in which to store the argument.
4277
4278    MODE is the argument's machine mode.
4279    TYPE is the data type of the argument (as a tree).
4280     This is null for libcalls where that information may
4281     not be available.
4282    CUM is a variable of type CUMULATIVE_ARGS which gives info about
4283     the preceding args and about the function being called.
4284    NAMED is nonzero if this argument is a named parameter
4285     (otherwise it is an extra parameter matching an ellipsis).  */
4286
4287 static rtx
4288 function_arg_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4289                  enum machine_mode orig_mode, tree type,
4290                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
4291 {
4292   static bool warnedsse, warnedmmx;
4293
4294   /* Avoid the AL settings for the Unix64 ABI.  */
4295   if (mode == VOIDmode)
4296     return constm1_rtx;
4297
4298   switch (mode)
4299     {
4300     default:
4301       break;
4302
4303     case BLKmode:
4304       if (bytes < 0)
4305         break;
4306       /* FALLTHRU */
4307     case DImode:
4308     case SImode:
4309     case HImode:
4310     case QImode:
4311       if (words <= cum->nregs)
4312         {
4313           int regno = cum->regno;
4314
4315           /* Fastcall allocates the first two DWORD (SImode) or
4316             smaller arguments to ECX and EDX if it isn't an
4317             aggregate type .  */
4318           if (cum->fastcall)
4319             {
4320               if (mode == BLKmode
4321                   || mode == DImode
4322                   || (type && AGGREGATE_TYPE_P (type)))
4323                 break;
4324
4325               /* ECX not EAX is the first allocated register.  */
4326               if (regno == AX_REG)
4327                 regno = CX_REG;
4328             }
4329           return gen_rtx_REG (mode, regno);
4330         }
4331       break;
4332
4333     case DFmode:
4334       if (cum->float_in_sse < 2)
4335         break;
4336     case SFmode:
4337       if (cum->float_in_sse < 1)
4338         break;
4339       /* FALLTHRU */
4340     case TImode:
4341     case V16QImode:
4342     case V8HImode:
4343     case V4SImode:
4344     case V2DImode:
4345     case V4SFmode:
4346     case V2DFmode:
4347       if (!type || !AGGREGATE_TYPE_P (type))
4348         {
4349           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
4350             {
4351               warnedsse = true;
4352               warning (0, "SSE vector argument without SSE enabled "
4353                        "changes the ABI");
4354             }
4355           if (cum->sse_nregs)
4356             return gen_reg_or_parallel (mode, orig_mode,
4357                                         cum->sse_regno + FIRST_SSE_REG);
4358         }
4359       break;
4360
4361     case V8QImode:
4362     case V4HImode:
4363     case V2SImode:
4364     case V2SFmode:
4365       if (!type || !AGGREGATE_TYPE_P (type))
4366         {
4367           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
4368             {
4369               warnedmmx = true;
4370               warning (0, "MMX vector argument without MMX enabled "
4371                        "changes the ABI");
4372             }
4373           if (cum->mmx_nregs)
4374             return gen_reg_or_parallel (mode, orig_mode,
4375                                         cum->mmx_regno + FIRST_MMX_REG);
4376         }
4377       break;
4378     }
4379
4380   return NULL_RTX;
4381 }
4382
4383 static rtx
4384 function_arg_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4385                  enum machine_mode orig_mode, tree type)
4386 {
4387   /* Handle a hidden AL argument containing number of registers
4388      for varargs x86-64 functions.  */
4389   if (mode == VOIDmode)
4390     return GEN_INT (cum->maybe_vaarg
4391                     ? (cum->sse_nregs < 0
4392                        ? SSE_REGPARM_MAX
4393                        : cum->sse_regno)
4394                     : -1);
4395
4396   return construct_container (mode, orig_mode, type, 0, cum->nregs,
4397                               cum->sse_nregs,
4398                               &x86_64_int_parameter_registers [cum->regno],
4399                               cum->sse_regno);
4400 }
4401
4402 static rtx
4403 function_arg_ms_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4404                     enum machine_mode orig_mode, int named)
4405 {
4406   unsigned int regno;
4407
4408   /* Avoid the AL settings for the Unix64 ABI.  */
4409   if (mode == VOIDmode)
4410     return constm1_rtx;
4411
4412   /* If we've run out of registers, it goes on the stack.  */
4413   if (cum->nregs == 0)
4414     return NULL_RTX;
4415
4416   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
4417
4418   /* Only floating point modes are passed in anything but integer regs.  */
4419   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
4420     {
4421       if (named)
4422         regno = cum->regno + FIRST_SSE_REG;
4423       else
4424         {
4425           rtx t1, t2;
4426
4427           /* Unnamed floating parameters are passed in both the
4428              SSE and integer registers.  */
4429           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
4430           t2 = gen_rtx_REG (mode, regno);
4431           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
4432           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
4433           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
4434         }
4435     }
4436
4437   return gen_reg_or_parallel (mode, orig_mode, regno);
4438 }
4439
4440 rtx
4441 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
4442               tree type, int named)
4443 {
4444   enum machine_mode mode = omode;
4445   HOST_WIDE_INT bytes, words;
4446
4447   if (mode == BLKmode)
4448     bytes = int_size_in_bytes (type);
4449   else
4450     bytes = GET_MODE_SIZE (mode);
4451   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4452
4453   /* To simplify the code below, represent vector types with a vector mode
4454      even if MMX/SSE are not active.  */
4455   if (type && TREE_CODE (type) == VECTOR_TYPE)
4456     mode = type_natural_mode (type);
4457
4458   if (TARGET_64BIT_MS_ABI)
4459     return function_arg_ms_64 (cum, mode, omode, named);
4460   else if (TARGET_64BIT)
4461     return function_arg_64 (cum, mode, omode, type);
4462   else
4463     return function_arg_32 (cum, mode, omode, type, bytes, words);
4464 }
4465
4466 /* A C expression that indicates when an argument must be passed by
4467    reference.  If nonzero for an argument, a copy of that argument is
4468    made in memory and a pointer to the argument is passed instead of
4469    the argument itself.  The pointer is passed in whatever way is
4470    appropriate for passing a pointer to that type.  */
4471
4472 static bool
4473 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4474                         enum machine_mode mode ATTRIBUTE_UNUSED,
4475                         const_tree type, bool named ATTRIBUTE_UNUSED)
4476 {
4477   if (TARGET_64BIT_MS_ABI)
4478     {
4479       if (type)
4480         {
4481           /* Arrays are passed by reference.  */
4482           if (TREE_CODE (type) == ARRAY_TYPE)
4483             return true;
4484
4485           if (AGGREGATE_TYPE_P (type))
4486             {
4487               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
4488                  are passed by reference.  */
4489               int el2 = exact_log2 (int_size_in_bytes (type));
4490               return !(el2 >= 0 && el2 <= 3);
4491             }
4492         }
4493
4494       /* __m128 is passed by reference.  */
4495       /* ??? How to handle complex?  For now treat them as structs,
4496          and pass them by reference if they're too large.  */
4497       if (GET_MODE_SIZE (mode) > 8)
4498         return true;
4499     }
4500   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
4501     return 1;
4502
4503   return 0;
4504 }
4505
4506 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
4507    ABI.  Only called if TARGET_SSE.  */
4508 static bool
4509 contains_128bit_aligned_vector_p (tree type)
4510 {
4511   enum machine_mode mode = TYPE_MODE (type);
4512   if (SSE_REG_MODE_P (mode)
4513       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
4514     return true;
4515   if (TYPE_ALIGN (type) < 128)
4516     return false;
4517
4518   if (AGGREGATE_TYPE_P (type))
4519     {
4520       /* Walk the aggregates recursively.  */
4521       switch (TREE_CODE (type))
4522         {
4523         case RECORD_TYPE:
4524         case UNION_TYPE:
4525         case QUAL_UNION_TYPE:
4526           {
4527             tree field;
4528
4529             /* Walk all the structure fields.  */
4530             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4531               {
4532                 if (TREE_CODE (field) == FIELD_DECL
4533                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
4534                   return true;
4535               }
4536             break;
4537           }
4538
4539         case ARRAY_TYPE:
4540           /* Just for use if some languages passes arrays by value.  */
4541           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
4542             return true;
4543           break;
4544
4545         default:
4546           gcc_unreachable ();
4547         }
4548     }
4549   return false;
4550 }
4551
4552 /* Gives the alignment boundary, in bits, of an argument with the
4553    specified mode and type.  */
4554
4555 int
4556 ix86_function_arg_boundary (enum machine_mode mode, tree type)
4557 {
4558   int align;
4559   if (type)
4560     align = TYPE_ALIGN (type);
4561   else
4562     align = GET_MODE_ALIGNMENT (mode);
4563   if (align < PARM_BOUNDARY)
4564     align = PARM_BOUNDARY;
4565   if (!TARGET_64BIT)
4566     {
4567       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
4568          make an exception for SSE modes since these require 128bit
4569          alignment.
4570
4571          The handling here differs from field_alignment.  ICC aligns MMX
4572          arguments to 4 byte boundaries, while structure fields are aligned
4573          to 8 byte boundaries.  */
4574       if (!TARGET_SSE)
4575         align = PARM_BOUNDARY;
4576       else if (!type)
4577         {
4578           if (!SSE_REG_MODE_P (mode))
4579             align = PARM_BOUNDARY;
4580         }
4581       else
4582         {
4583           if (!contains_128bit_aligned_vector_p (type))
4584             align = PARM_BOUNDARY;
4585         }
4586     }
4587   if (align > 128)
4588     align = 128;
4589   return align;
4590 }
4591
4592 /* Return true if N is a possible register number of function value.  */
4593
4594 bool
4595 ix86_function_value_regno_p (int regno)
4596 {
4597   switch (regno)
4598     {
4599     case 0:
4600       return true;
4601
4602     case FIRST_FLOAT_REG:
4603       if (TARGET_64BIT_MS_ABI)
4604         return false;
4605       return TARGET_FLOAT_RETURNS_IN_80387;
4606
4607     case FIRST_SSE_REG:
4608       return TARGET_SSE;
4609
4610     case FIRST_MMX_REG:
4611       if (TARGET_MACHO || TARGET_64BIT)
4612         return false;
4613       return TARGET_MMX;
4614     }
4615
4616   return false;
4617 }
4618
4619 /* Define how to find the value returned by a function.
4620    VALTYPE is the data type of the value (as a tree).
4621    If the precise function being called is known, FUNC is its FUNCTION_DECL;
4622    otherwise, FUNC is 0.  */
4623
4624 static rtx
4625 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
4626                    const_tree fntype, const_tree fn)
4627 {
4628   unsigned int regno;
4629
4630   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4631      we normally prevent this case when mmx is not available.  However
4632      some ABIs may require the result to be returned like DImode.  */
4633   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4634     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
4635
4636   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4637      we prevent this case when sse is not available.  However some ABIs
4638      may require the result to be returned like integer TImode.  */
4639   else if (mode == TImode
4640            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4641     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
4642
4643   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
4644   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
4645     regno = FIRST_FLOAT_REG;
4646   else
4647     /* Most things go in %eax.  */
4648     regno = AX_REG;
4649
4650   /* Override FP return register with %xmm0 for local functions when
4651      SSE math is enabled or for functions with sseregparm attribute.  */
4652   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
4653     {
4654       int sse_level = ix86_function_sseregparm (fntype, fn, false);
4655       if ((sse_level >= 1 && mode == SFmode)
4656           || (sse_level == 2 && mode == DFmode))
4657         regno = FIRST_SSE_REG;
4658     }
4659
4660   return gen_rtx_REG (orig_mode, regno);
4661 }
4662
4663 static rtx
4664 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
4665                    const_tree valtype)
4666 {
4667   rtx ret;
4668
4669   /* Handle libcalls, which don't provide a type node.  */
4670   if (valtype == NULL)
4671     {
4672       switch (mode)
4673         {
4674         case SFmode:
4675         case SCmode:
4676         case DFmode:
4677         case DCmode:
4678         case TFmode:
4679         case SDmode:
4680         case DDmode:
4681         case TDmode:
4682           return gen_rtx_REG (mode, FIRST_SSE_REG);
4683         case XFmode:
4684         case XCmode:
4685           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4686         case TCmode:
4687           return NULL;
4688         default:
4689           return gen_rtx_REG (mode, AX_REG);
4690         }
4691     }
4692
4693   ret = construct_container (mode, orig_mode, valtype, 1,
4694                              REGPARM_MAX, SSE_REGPARM_MAX,
4695                              x86_64_int_return_registers, 0);
4696
4697   /* For zero sized structures, construct_container returns NULL, but we
4698      need to keep rest of compiler happy by returning meaningful value.  */
4699   if (!ret)
4700     ret = gen_rtx_REG (orig_mode, AX_REG);
4701
4702   return ret;
4703 }
4704
4705 static rtx
4706 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
4707 {
4708   unsigned int regno = AX_REG;
4709
4710   if (TARGET_SSE)
4711     {
4712       if (mode == SFmode || mode == DFmode)
4713         regno = FIRST_SSE_REG;
4714       else if (VECTOR_MODE_P (mode) || GET_MODE_SIZE (mode) == 16)
4715         regno = FIRST_SSE_REG;
4716     }
4717
4718   return gen_rtx_REG (orig_mode, regno);
4719 }
4720
4721 static rtx
4722 ix86_function_value_1 (const_tree valtype, const_tree fntype_or_decl,
4723                        enum machine_mode orig_mode, enum machine_mode mode)
4724 {
4725   const_tree fn, fntype;
4726
4727   fn = NULL_TREE;
4728   if (fntype_or_decl && DECL_P (fntype_or_decl))
4729     fn = fntype_or_decl;
4730   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
4731
4732   if (TARGET_64BIT_MS_ABI)
4733     return function_value_ms_64 (orig_mode, mode);
4734   else if (TARGET_64BIT)
4735     return function_value_64 (orig_mode, mode, valtype);
4736   else
4737     return function_value_32 (orig_mode, mode, fntype, fn);
4738 }
4739
4740 static rtx
4741 ix86_function_value (const_tree valtype, const_tree fntype_or_decl,
4742                      bool outgoing ATTRIBUTE_UNUSED)
4743 {
4744   enum machine_mode mode, orig_mode;
4745
4746   orig_mode = TYPE_MODE (valtype);
4747   mode = type_natural_mode (valtype);
4748   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
4749 }
4750
4751 rtx
4752 ix86_libcall_value (enum machine_mode mode)
4753 {
4754   return ix86_function_value_1 (NULL, NULL, mode, mode);
4755 }
4756
4757 /* Return true iff type is returned in memory.  */
4758
4759 static int
4760 return_in_memory_32 (const_tree type, enum machine_mode mode)
4761 {
4762   HOST_WIDE_INT size;
4763
4764   if (mode == BLKmode)
4765     return 1;
4766
4767   size = int_size_in_bytes (type);
4768
4769   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
4770     return 0;
4771
4772   if (VECTOR_MODE_P (mode) || mode == TImode)
4773     {
4774       /* User-created vectors small enough to fit in EAX.  */
4775       if (size < 8)
4776         return 0;
4777
4778       /* MMX/3dNow values are returned in MM0,
4779          except when it doesn't exits.  */
4780       if (size == 8)
4781         return (TARGET_MMX ? 0 : 1);
4782
4783       /* SSE values are returned in XMM0, except when it doesn't exist.  */
4784       if (size == 16)
4785         return (TARGET_SSE ? 0 : 1);
4786     }
4787
4788   if (mode == XFmode)
4789     return 0;
4790
4791   if (mode == TDmode)
4792     return 1;
4793
4794   if (size > 12)
4795     return 1;
4796   return 0;
4797 }
4798
4799 static int
4800 return_in_memory_64 (const_tree type, enum machine_mode mode)
4801 {
4802   int needed_intregs, needed_sseregs;
4803   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
4804 }
4805
4806 static int
4807 return_in_memory_ms_64 (const_tree type, enum machine_mode mode)
4808 {
4809   HOST_WIDE_INT size = int_size_in_bytes (type);
4810
4811   /* __m128 and friends are returned in xmm0.  */
4812   if (!COMPLEX_MODE_P (mode) && size == 16 && VECTOR_MODE_P (mode))
4813     return 0;
4814
4815   /* Otherwise, the size must be exactly in [1248]. But not for complex. */
4816   return (size != 1 && size != 2 && size != 4 && size != 8)
4817          || COMPLEX_MODE_P (mode);
4818 }
4819
4820 int
4821 ix86_return_in_memory (const_tree type)
4822 {
4823   const enum machine_mode mode = type_natural_mode (type);
4824
4825   if (TARGET_64BIT_MS_ABI)
4826     return return_in_memory_ms_64 (type, mode);
4827   else if (TARGET_64BIT)
4828     return return_in_memory_64 (type, mode);
4829   else
4830     return return_in_memory_32 (type, mode);
4831 }
4832
4833 /* Return false iff TYPE is returned in memory.  This version is used
4834    on Solaris 10.  It is similar to the generic ix86_return_in_memory,
4835    but differs notably in that when MMX is available, 8-byte vectors
4836    are returned in memory, rather than in MMX registers.  */
4837
4838 int
4839 ix86_sol10_return_in_memory (const_tree type)
4840 {
4841   int size;
4842   enum machine_mode mode = type_natural_mode (type);
4843
4844   if (TARGET_64BIT)
4845     return return_in_memory_64 (type, mode);
4846
4847   if (mode == BLKmode)
4848     return 1;
4849
4850   size = int_size_in_bytes (type);
4851
4852   if (VECTOR_MODE_P (mode))
4853     {
4854       /* Return in memory only if MMX registers *are* available.  This
4855          seems backwards, but it is consistent with the existing
4856          Solaris x86 ABI.  */
4857       if (size == 8)
4858         return TARGET_MMX;
4859       if (size == 16)
4860         return !TARGET_SSE;
4861     }
4862   else if (mode == TImode)
4863     return !TARGET_SSE;
4864   else if (mode == XFmode)
4865     return 0;
4866
4867   return size > 12;
4868 }
4869
4870 /* When returning SSE vector types, we have a choice of either
4871      (1) being abi incompatible with a -march switch, or
4872      (2) generating an error.
4873    Given no good solution, I think the safest thing is one warning.
4874    The user won't be able to use -Werror, but....
4875
4876    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
4877    called in response to actually generating a caller or callee that
4878    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
4879    via aggregate_value_p for general type probing from tree-ssa.  */
4880
4881 static rtx
4882 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
4883 {
4884   static bool warnedsse, warnedmmx;
4885
4886   if (!TARGET_64BIT && type)
4887     {
4888       /* Look at the return type of the function, not the function type.  */
4889       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
4890
4891       if (!TARGET_SSE && !warnedsse)
4892         {
4893           if (mode == TImode
4894               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4895             {
4896               warnedsse = true;
4897               warning (0, "SSE vector return without SSE enabled "
4898                        "changes the ABI");
4899             }
4900         }
4901
4902       if (!TARGET_MMX && !warnedmmx)
4903         {
4904           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4905             {
4906               warnedmmx = true;
4907               warning (0, "MMX vector return without MMX enabled "
4908                        "changes the ABI");
4909             }
4910         }
4911     }
4912
4913   return NULL;
4914 }
4915
4916 \f
4917 /* Create the va_list data type.  */
4918
4919 static tree
4920 ix86_build_builtin_va_list (void)
4921 {
4922   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4923
4924   /* For i386 we use plain pointer to argument area.  */
4925   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4926     return build_pointer_type (char_type_node);
4927
4928   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4929   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4930
4931   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4932                       unsigned_type_node);
4933   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4934                       unsigned_type_node);
4935   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4936                       ptr_type_node);
4937   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4938                       ptr_type_node);
4939
4940   va_list_gpr_counter_field = f_gpr;
4941   va_list_fpr_counter_field = f_fpr;
4942
4943   DECL_FIELD_CONTEXT (f_gpr) = record;
4944   DECL_FIELD_CONTEXT (f_fpr) = record;
4945   DECL_FIELD_CONTEXT (f_ovf) = record;
4946   DECL_FIELD_CONTEXT (f_sav) = record;
4947
4948   TREE_CHAIN (record) = type_decl;
4949   TYPE_NAME (record) = type_decl;
4950   TYPE_FIELDS (record) = f_gpr;
4951   TREE_CHAIN (f_gpr) = f_fpr;
4952   TREE_CHAIN (f_fpr) = f_ovf;
4953   TREE_CHAIN (f_ovf) = f_sav;
4954
4955   layout_type (record);
4956
4957   /* The correct type is an array type of one element.  */
4958   return build_array_type (record, build_index_type (size_zero_node));
4959 }
4960
4961 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4962
4963 static void
4964 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
4965 {
4966   rtx save_area, mem;
4967   rtx label;
4968   rtx label_ref;
4969   rtx tmp_reg;
4970   rtx nsse_reg;
4971   alias_set_type set;
4972   int i;
4973
4974   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4975     return;
4976
4977   /* Indicate to allocate space on the stack for varargs save area.  */
4978   ix86_save_varrargs_registers = 1;
4979   /* We need 16-byte stack alignment to save SSE registers.  If user
4980      asked for lower preferred_stack_boundary, lets just hope that he knows
4981      what he is doing and won't varargs SSE values.
4982
4983      We also may end up assuming that only 64bit values are stored in SSE
4984      register let some floating point program work.  */
4985   if (ix86_preferred_stack_boundary >= 128)
4986     cfun->stack_alignment_needed = 128;
4987
4988   save_area = frame_pointer_rtx;
4989   set = get_varargs_alias_set ();
4990
4991   for (i = cum->regno;
4992        i < ix86_regparm
4993        && i < cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4994        i++)
4995     {
4996       mem = gen_rtx_MEM (Pmode,
4997                          plus_constant (save_area, i * UNITS_PER_WORD));
4998       MEM_NOTRAP_P (mem) = 1;
4999       set_mem_alias_set (mem, set);
5000       emit_move_insn (mem, gen_rtx_REG (Pmode,
5001                                         x86_64_int_parameter_registers[i]));
5002     }
5003
5004   if (cum->sse_nregs && cfun->va_list_fpr_size)
5005     {
5006       /* Now emit code to save SSE registers.  The AX parameter contains number
5007          of SSE parameter registers used to call this function.  We use
5008          sse_prologue_save insn template that produces computed jump across
5009          SSE saves.  We need some preparation work to get this working.  */
5010
5011       label = gen_label_rtx ();
5012       label_ref = gen_rtx_LABEL_REF (Pmode, label);
5013
5014       /* Compute address to jump to :
5015          label - 5*eax + nnamed_sse_arguments*5  */
5016       tmp_reg = gen_reg_rtx (Pmode);
5017       nsse_reg = gen_reg_rtx (Pmode);
5018       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, AX_REG)));
5019       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
5020                               gen_rtx_MULT (Pmode, nsse_reg,
5021                                             GEN_INT (4))));
5022       if (cum->sse_regno)
5023         emit_move_insn
5024           (nsse_reg,
5025            gen_rtx_CONST (DImode,
5026                           gen_rtx_PLUS (DImode,
5027                                         label_ref,
5028                                         GEN_INT (cum->sse_regno * 4))));
5029       else
5030         emit_move_insn (nsse_reg, label_ref);
5031       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
5032
5033       /* Compute address of memory block we save into.  We always use pointer
5034          pointing 127 bytes after first byte to store - this is needed to keep
5035          instruction size limited by 4 bytes.  */
5036       tmp_reg = gen_reg_rtx (Pmode);
5037       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
5038                               plus_constant (save_area,
5039                                              8 * REGPARM_MAX + 127)));
5040       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
5041       MEM_NOTRAP_P (mem) = 1;
5042       set_mem_alias_set (mem, set);
5043       set_mem_align (mem, BITS_PER_WORD);
5044
5045       /* And finally do the dirty job!  */
5046       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
5047                                         GEN_INT (cum->sse_regno), label));
5048     }
5049 }
5050
5051 static void
5052 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
5053 {
5054   alias_set_type set = get_varargs_alias_set ();
5055   int i;
5056
5057   for (i = cum->regno; i < REGPARM_MAX; i++)
5058     {
5059       rtx reg, mem;
5060
5061       mem = gen_rtx_MEM (Pmode,
5062                          plus_constant (virtual_incoming_args_rtx,
5063                                         i * UNITS_PER_WORD));
5064       MEM_NOTRAP_P (mem) = 1;
5065       set_mem_alias_set (mem, set);
5066
5067       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
5068       emit_move_insn (mem, reg);
5069     }
5070 }
5071
5072 static void
5073 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
5074                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
5075                              int no_rtl)
5076 {
5077   CUMULATIVE_ARGS next_cum;
5078   tree fntype;
5079
5080   /* This argument doesn't appear to be used anymore.  Which is good,
5081      because the old code here didn't suppress rtl generation.  */
5082   gcc_assert (!no_rtl);
5083
5084   if (!TARGET_64BIT)
5085     return;
5086
5087   fntype = TREE_TYPE (current_function_decl);
5088
5089   /* For varargs, we do not want to skip the dummy va_dcl argument.
5090      For stdargs, we do want to skip the last named argument.  */
5091   next_cum = *cum;
5092   if (stdarg_p (fntype))
5093     function_arg_advance (&next_cum, mode, type, 1);
5094
5095   if (TARGET_64BIT_MS_ABI)
5096     setup_incoming_varargs_ms_64 (&next_cum);
5097   else
5098     setup_incoming_varargs_64 (&next_cum);
5099 }
5100
5101 /* Implement va_start.  */
5102
5103 static void
5104 ix86_va_start (tree valist, rtx nextarg)
5105 {
5106   HOST_WIDE_INT words, n_gpr, n_fpr;
5107   tree f_gpr, f_fpr, f_ovf, f_sav;
5108   tree gpr, fpr, ovf, sav, t;
5109   tree type;
5110
5111   /* Only 64bit target needs something special.  */
5112   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
5113     {
5114       std_expand_builtin_va_start (valist, nextarg);
5115       return;
5116     }
5117
5118   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
5119   f_fpr = TREE_CHAIN (f_gpr);
5120   f_ovf = TREE_CHAIN (f_fpr);
5121   f_sav = TREE_CHAIN (f_ovf);
5122
5123   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
5124   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
5125   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
5126   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
5127   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
5128
5129   /* Count number of gp and fp argument registers used.  */
5130   words = current_function_args_info.words;
5131   n_gpr = current_function_args_info.regno;
5132   n_fpr = current_function_args_info.sse_regno;
5133
5134   if (cfun->va_list_gpr_size)
5135     {
5136       type = TREE_TYPE (gpr);
5137       t = build2 (GIMPLE_MODIFY_STMT, type, gpr,
5138                   build_int_cst (type, n_gpr * 8));
5139       TREE_SIDE_EFFECTS (t) = 1;
5140       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5141     }
5142
5143   if (cfun->va_list_fpr_size)
5144     {
5145       type = TREE_TYPE (fpr);
5146       t = build2 (GIMPLE_MODIFY_STMT, type, fpr,
5147                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
5148       TREE_SIDE_EFFECTS (t) = 1;
5149       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5150     }
5151
5152   /* Find the overflow area.  */
5153   type = TREE_TYPE (ovf);
5154   t = make_tree (type, virtual_incoming_args_rtx);
5155   if (words != 0)
5156     t = build2 (POINTER_PLUS_EXPR, type, t,
5157                 size_int (words * UNITS_PER_WORD));
5158   t = build2 (GIMPLE_MODIFY_STMT, type, ovf, t);
5159   TREE_SIDE_EFFECTS (t) = 1;
5160   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5161
5162   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
5163     {
5164       /* Find the register save area.
5165          Prologue of the function save it right above stack frame.  */
5166       type = TREE_TYPE (sav);
5167       t = make_tree (type, frame_pointer_rtx);
5168       t = build2 (GIMPLE_MODIFY_STMT, type, sav, t);
5169       TREE_SIDE_EFFECTS (t) = 1;
5170       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
5171     }
5172 }
5173
5174 /* Implement va_arg.  */
5175
5176 static tree
5177 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
5178 {
5179   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
5180   tree f_gpr, f_fpr, f_ovf, f_sav;
5181   tree gpr, fpr, ovf, sav, t;
5182   int size, rsize;
5183   tree lab_false, lab_over = NULL_TREE;
5184   tree addr, t2;
5185   rtx container;
5186   int indirect_p = 0;
5187   tree ptrtype;
5188   enum machine_mode nat_mode;
5189
5190   /* Only 64bit target needs something special.  */
5191   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
5192     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
5193
5194   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
5195   f_fpr = TREE_CHAIN (f_gpr);
5196   f_ovf = TREE_CHAIN (f_fpr);
5197   f_sav = TREE_CHAIN (f_ovf);
5198
5199   valist = build_va_arg_indirect_ref (valist);
5200   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
5201   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
5202   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
5203   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
5204
5205   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
5206   if (indirect_p)
5207     type = build_pointer_type (type);
5208   size = int_size_in_bytes (type);
5209   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5210
5211   nat_mode = type_natural_mode (type);
5212   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
5213                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
5214
5215   /* Pull the value out of the saved registers.  */
5216
5217   addr = create_tmp_var (ptr_type_node, "addr");
5218   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
5219
5220   if (container)
5221     {
5222       int needed_intregs, needed_sseregs;
5223       bool need_temp;
5224       tree int_addr, sse_addr;
5225
5226       lab_false = create_artificial_label ();
5227       lab_over = create_artificial_label ();
5228
5229       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
5230
5231       need_temp = (!REG_P (container)
5232                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
5233                        || TYPE_ALIGN (type) > 128));
5234
5235       /* In case we are passing structure, verify that it is consecutive block
5236          on the register save area.  If not we need to do moves.  */
5237       if (!need_temp && !REG_P (container))
5238         {
5239           /* Verify that all registers are strictly consecutive  */
5240           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
5241             {
5242               int i;
5243
5244               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
5245                 {
5246                   rtx slot = XVECEXP (container, 0, i);
5247                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
5248                       || INTVAL (XEXP (slot, 1)) != i * 16)
5249                     need_temp = 1;
5250                 }
5251             }
5252           else
5253             {
5254               int i;
5255
5256               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
5257                 {
5258                   rtx slot = XVECEXP (container, 0, i);
5259                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
5260                       || INTVAL (XEXP (slot, 1)) != i * 8)
5261                     need_temp = 1;
5262                 }
5263             }
5264         }
5265       if (!need_temp)
5266         {
5267           int_addr = addr;
5268           sse_addr = addr;
5269         }
5270       else
5271         {
5272           int_addr = create_tmp_var (ptr_type_node, "int_addr");
5273           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
5274           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
5275           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
5276         }
5277
5278       /* First ensure that we fit completely in registers.  */
5279       if (needed_intregs)
5280         {
5281           t = build_int_cst (TREE_TYPE (gpr),
5282                              (REGPARM_MAX - needed_intregs + 1) * 8);
5283           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
5284           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
5285           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
5286           gimplify_and_add (t, pre_p);
5287         }
5288       if (needed_sseregs)
5289         {
5290           t = build_int_cst (TREE_TYPE (fpr),
5291                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
5292                              + REGPARM_MAX * 8);
5293           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
5294           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
5295           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
5296           gimplify_and_add (t, pre_p);
5297         }
5298
5299       /* Compute index to start of area used for integer regs.  */
5300       if (needed_intregs)
5301         {
5302           /* int_addr = gpr + sav; */
5303           t = fold_convert (sizetype, gpr);
5304           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
5305           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, int_addr, t);
5306           gimplify_and_add (t, pre_p);
5307         }
5308       if (needed_sseregs)
5309         {
5310           /* sse_addr = fpr + sav; */
5311           t = fold_convert (sizetype, fpr);
5312           t = build2 (POINTER_PLUS_EXPR, ptr_type_node, sav, t);
5313           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, sse_addr, t);
5314           gimplify_and_add (t, pre_p);
5315         }
5316       if (need_temp)
5317         {
5318           int i;
5319           tree temp = create_tmp_var (type, "va_arg_tmp");
5320
5321           /* addr = &temp; */
5322           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
5323           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
5324           gimplify_and_add (t, pre_p);
5325
5326           for (i = 0; i < XVECLEN (container, 0); i++)
5327             {
5328               rtx slot = XVECEXP (container, 0, i);
5329               rtx reg = XEXP (slot, 0);
5330               enum machine_mode mode = GET_MODE (reg);
5331               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
5332               tree addr_type = build_pointer_type (piece_type);
5333               tree src_addr, src;
5334               int src_offset;
5335               tree dest_addr, dest;
5336
5337               if (SSE_REGNO_P (REGNO (reg)))
5338                 {
5339                   src_addr = sse_addr;
5340                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
5341                 }
5342               else
5343                 {
5344                   src_addr = int_addr;
5345                   src_offset = REGNO (reg) * 8;
5346                 }
5347               src_addr = fold_convert (addr_type, src_addr);
5348               src_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, src_addr,
5349                                       size_int (src_offset));
5350               src = build_va_arg_indirect_ref (src_addr);
5351
5352               dest_addr = fold_convert (addr_type, addr);
5353               dest_addr = fold_build2 (POINTER_PLUS_EXPR, addr_type, dest_addr,
5354                                        size_int (INTVAL (XEXP (slot, 1))));
5355               dest = build_va_arg_indirect_ref (dest_addr);
5356
5357               t = build2 (GIMPLE_MODIFY_STMT, void_type_node, dest, src);
5358               gimplify_and_add (t, pre_p);
5359             }
5360         }
5361
5362       if (needed_intregs)
5363         {
5364           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
5365                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
5366           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (gpr), gpr, t);
5367           gimplify_and_add (t, pre_p);
5368         }
5369       if (needed_sseregs)
5370         {
5371           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
5372                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
5373           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (fpr), fpr, t);
5374           gimplify_and_add (t, pre_p);
5375         }
5376
5377       t = build1 (GOTO_EXPR, void_type_node, lab_over);
5378       gimplify_and_add (t, pre_p);
5379
5380       t = build1 (LABEL_EXPR, void_type_node, lab_false);
5381       append_to_statement_list (t, pre_p);
5382     }
5383
5384   /* ... otherwise out of the overflow area.  */
5385
5386   /* Care for on-stack alignment if needed.  */
5387   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
5388       || integer_zerop (TYPE_SIZE (type)))
5389     t = ovf;
5390  else
5391     {
5392       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
5393       t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (ovf), ovf,
5394                   size_int (align - 1));
5395       t = fold_convert (sizetype, t);
5396       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
5397                   size_int (-align));
5398       t = fold_convert (TREE_TYPE (ovf), t);
5399     }
5400   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
5401
5402   t2 = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
5403   gimplify_and_add (t2, pre_p);
5404
5405   t = build2 (POINTER_PLUS_EXPR, TREE_TYPE (t), t,
5406               size_int (rsize * UNITS_PER_WORD));
5407   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (ovf), ovf, t);
5408   gimplify_and_add (t, pre_p);
5409
5410   if (container)
5411     {
5412       t = build1 (LABEL_EXPR, void_type_node, lab_over);
5413       append_to_statement_list (t, pre_p);
5414     }
5415
5416   ptrtype = build_pointer_type (type);
5417   addr = fold_convert (ptrtype, addr);
5418
5419   if (indirect_p)
5420     addr = build_va_arg_indirect_ref (addr);
5421   return build_va_arg_indirect_ref (addr);
5422 }
5423 \f
5424 /* Return nonzero if OPNUM's MEM should be matched
5425    in movabs* patterns.  */
5426
5427 int
5428 ix86_check_movabs (rtx insn, int opnum)
5429 {
5430   rtx set, mem;
5431
5432   set = PATTERN (insn);
5433   if (GET_CODE (set) == PARALLEL)
5434     set = XVECEXP (set, 0, 0);
5435   gcc_assert (GET_CODE (set) == SET);
5436   mem = XEXP (set, opnum);
5437   while (GET_CODE (mem) == SUBREG)
5438     mem = SUBREG_REG (mem);
5439   gcc_assert (MEM_P (mem));
5440   return (volatile_ok || !MEM_VOLATILE_P (mem));
5441 }
5442 \f
5443 /* Initialize the table of extra 80387 mathematical constants.  */
5444
5445 static void
5446 init_ext_80387_constants (void)
5447 {
5448   static const char * cst[5] =
5449   {
5450     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
5451     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
5452     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
5453     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
5454     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
5455   };
5456   int i;
5457
5458   for (i = 0; i < 5; i++)
5459     {
5460       real_from_string (&ext_80387_constants_table[i], cst[i]);
5461       /* Ensure each constant is rounded to XFmode precision.  */
5462       real_convert (&ext_80387_constants_table[i],
5463                     XFmode, &ext_80387_constants_table[i]);
5464     }
5465
5466   ext_80387_constants_init = 1;
5467 }
5468
5469 /* Return true if the constant is something that can be loaded with
5470    a special instruction.  */
5471
5472 int
5473 standard_80387_constant_p (rtx x)
5474 {
5475   enum machine_mode mode = GET_MODE (x);
5476
5477   REAL_VALUE_TYPE r;
5478
5479   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
5480     return -1;
5481
5482   if (x == CONST0_RTX (mode))
5483     return 1;
5484   if (x == CONST1_RTX (mode))
5485     return 2;
5486
5487   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5488
5489   /* For XFmode constants, try to find a special 80387 instruction when
5490      optimizing for size or on those CPUs that benefit from them.  */
5491   if (mode == XFmode
5492       && (optimize_size || TARGET_EXT_80387_CONSTANTS))
5493     {
5494       int i;
5495
5496       if (! ext_80387_constants_init)
5497         init_ext_80387_constants ();
5498
5499       for (i = 0; i < 5; i++)
5500         if (real_identical (&r, &ext_80387_constants_table[i]))
5501           return i + 3;
5502     }
5503
5504   /* Load of the constant -0.0 or -1.0 will be split as
5505      fldz;fchs or fld1;fchs sequence.  */
5506   if (real_isnegzero (&r))
5507     return 8;
5508   if (real_identical (&r, &dconstm1))
5509     return 9;
5510
5511   return 0;
5512 }
5513
5514 /* Return the opcode of the special instruction to be used to load
5515    the constant X.  */
5516
5517 const char *
5518 standard_80387_constant_opcode (rtx x)
5519 {
5520   switch (standard_80387_constant_p (x))
5521     {
5522     case 1:
5523       return "fldz";
5524     case 2:
5525       return "fld1";
5526     case 3:
5527       return "fldlg2";
5528     case 4:
5529       return "fldln2";
5530     case 5:
5531       return "fldl2e";
5532     case 6:
5533       return "fldl2t";
5534     case 7:
5535       return "fldpi";
5536     case 8:
5537     case 9:
5538       return "#";
5539     default:
5540       gcc_unreachable ();
5541     }
5542 }
5543
5544 /* Return the CONST_DOUBLE representing the 80387 constant that is
5545    loaded by the specified special instruction.  The argument IDX
5546    matches the return value from standard_80387_constant_p.  */
5547
5548 rtx
5549 standard_80387_constant_rtx (int idx)
5550 {
5551   int i;
5552
5553   if (! ext_80387_constants_init)
5554     init_ext_80387_constants ();
5555
5556   switch (idx)
5557     {
5558     case 3:
5559     case 4:
5560     case 5:
5561     case 6:
5562     case 7:
5563       i = idx - 3;
5564       break;
5565
5566     default:
5567       gcc_unreachable ();
5568     }
5569
5570   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
5571                                        XFmode);
5572 }
5573
5574 /* Return 1 if mode is a valid mode for sse.  */
5575 static int
5576 standard_sse_mode_p (enum machine_mode mode)
5577 {
5578   switch (mode)
5579     {
5580     case V16QImode:
5581     case V8HImode:
5582     case V4SImode:
5583     case V2DImode:
5584     case V4SFmode:
5585     case V2DFmode:
5586       return 1;
5587
5588     default:
5589       return 0;
5590     }
5591 }
5592
5593 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
5594  */
5595 int
5596 standard_sse_constant_p (rtx x)
5597 {
5598   enum machine_mode mode = GET_MODE (x);
5599
5600   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
5601     return 1;
5602   if (vector_all_ones_operand (x, mode)
5603       && standard_sse_mode_p (mode))
5604     return TARGET_SSE2 ? 2 : -1;
5605
5606   return 0;
5607 }
5608
5609 /* Return the opcode of the special instruction to be used to load
5610    the constant X.  */
5611
5612 const char *
5613 standard_sse_constant_opcode (rtx insn, rtx x)
5614 {
5615   switch (standard_sse_constant_p (x))
5616     {
5617     case 1:
5618       if (get_attr_mode (insn) == MODE_V4SF)
5619         return "xorps\t%0, %0";
5620       else if (get_attr_mode (insn) == MODE_V2DF)
5621         return "xorpd\t%0, %0";
5622       else
5623         return "pxor\t%0, %0";
5624     case 2:
5625       return "pcmpeqd\t%0, %0";
5626     }
5627   gcc_unreachable ();
5628 }
5629
5630 /* Returns 1 if OP contains a symbol reference */
5631
5632 int
5633 symbolic_reference_mentioned_p (rtx op)
5634 {
5635   const char *fmt;
5636   int i;
5637
5638   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
5639     return 1;
5640
5641   fmt = GET_RTX_FORMAT (GET_CODE (op));
5642   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5643     {
5644       if (fmt[i] == 'E')
5645         {
5646           int j;
5647
5648           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5649             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
5650               return 1;
5651         }
5652
5653       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
5654         return 1;
5655     }
5656
5657   return 0;
5658 }
5659
5660 /* Return 1 if it is appropriate to emit `ret' instructions in the
5661    body of a function.  Do this only if the epilogue is simple, needing a
5662    couple of insns.  Prior to reloading, we can't tell how many registers
5663    must be saved, so return 0 then.  Return 0 if there is no frame
5664    marker to de-allocate.  */
5665
5666 int
5667 ix86_can_use_return_insn_p (void)
5668 {
5669   struct ix86_frame frame;
5670
5671   if (! reload_completed || frame_pointer_needed)
5672     return 0;
5673
5674   /* Don't allow more than 32 pop, since that's all we can do
5675      with one instruction.  */
5676   if (current_function_pops_args
5677       && current_function_args_size >= 32768)
5678     return 0;
5679
5680   ix86_compute_frame_layout (&frame);
5681   return frame.to_allocate == 0 && frame.nregs == 0;
5682 }
5683 \f
5684 /* Value should be nonzero if functions must have frame pointers.
5685    Zero means the frame pointer need not be set up (and parms may
5686    be accessed via the stack pointer) in functions that seem suitable.  */
5687
5688 int
5689 ix86_frame_pointer_required (void)
5690 {
5691   /* If we accessed previous frames, then the generated code expects
5692      to be able to access the saved ebp value in our frame.  */
5693   if (cfun->machine->accesses_prev_frame)
5694     return 1;
5695
5696   /* Several x86 os'es need a frame pointer for other reasons,
5697      usually pertaining to setjmp.  */
5698   if (SUBTARGET_FRAME_POINTER_REQUIRED)
5699     return 1;
5700
5701   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
5702      the frame pointer by default.  Turn it back on now if we've not
5703      got a leaf function.  */
5704   if (TARGET_OMIT_LEAF_FRAME_POINTER
5705       && (!current_function_is_leaf
5706           || ix86_current_function_calls_tls_descriptor))
5707     return 1;
5708
5709   if (current_function_profile)
5710     return 1;
5711
5712   return 0;
5713 }
5714
5715 /* Record that the current function accesses previous call frames.  */
5716
5717 void
5718 ix86_setup_frame_addresses (void)
5719 {
5720   cfun->machine->accesses_prev_frame = 1;
5721 }
5722 \f
5723 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
5724 # define USE_HIDDEN_LINKONCE 1
5725 #else
5726 # define USE_HIDDEN_LINKONCE 0
5727 #endif
5728
5729 static int pic_labels_used;
5730
5731 /* Fills in the label name that should be used for a pc thunk for
5732    the given register.  */
5733
5734 static void
5735 get_pc_thunk_name (char name[32], unsigned int regno)
5736 {
5737   gcc_assert (!TARGET_64BIT);
5738
5739   if (USE_HIDDEN_LINKONCE)
5740     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
5741   else
5742     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
5743 }
5744
5745
5746 /* This function generates code for -fpic that loads %ebx with
5747    the return address of the caller and then returns.  */
5748
5749 void
5750 ix86_file_end (void)
5751 {
5752   rtx xops[2];
5753   int regno;
5754
5755   for (regno = 0; regno < 8; ++regno)
5756     {
5757       char name[32];
5758
5759       if (! ((pic_labels_used >> regno) & 1))
5760         continue;
5761
5762       get_pc_thunk_name (name, regno);
5763
5764 #if TARGET_MACHO
5765       if (TARGET_MACHO)
5766         {
5767           switch_to_section (darwin_sections[text_coal_section]);
5768           fputs ("\t.weak_definition\t", asm_out_file);
5769           assemble_name (asm_out_file, name);
5770           fputs ("\n\t.private_extern\t", asm_out_file);
5771           assemble_name (asm_out_file, name);
5772           fputs ("\n", asm_out_file);
5773           ASM_OUTPUT_LABEL (asm_out_file, name);
5774         }
5775       else
5776 #endif
5777       if (USE_HIDDEN_LINKONCE)
5778         {
5779           tree decl;
5780
5781           decl = build_decl (FUNCTION_DECL, get_identifier (name),
5782                              error_mark_node);
5783           TREE_PUBLIC (decl) = 1;
5784           TREE_STATIC (decl) = 1;
5785           DECL_ONE_ONLY (decl) = 1;
5786
5787           (*targetm.asm_out.unique_section) (decl, 0);
5788           switch_to_section (get_named_section (decl, NULL, 0));
5789
5790           (*targetm.asm_out.globalize_label) (asm_out_file, name);
5791           fputs ("\t.hidden\t", asm_out_file);
5792           assemble_name (asm_out_file, name);
5793           fputc ('\n', asm_out_file);
5794           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
5795         }
5796       else
5797         {
5798           switch_to_section (text_section);
5799           ASM_OUTPUT_LABEL (asm_out_file, name);
5800         }
5801
5802       xops[0] = gen_rtx_REG (SImode, regno);
5803       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
5804       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
5805       output_asm_insn ("ret", xops);
5806     }
5807
5808   if (NEED_INDICATE_EXEC_STACK)
5809     file_end_indicate_exec_stack ();
5810 }
5811
5812 /* Emit code for the SET_GOT patterns.  */
5813
5814 const char *
5815 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
5816 {
5817   rtx xops[3];
5818
5819   xops[0] = dest;
5820
5821   if (TARGET_VXWORKS_RTP && flag_pic)
5822     {
5823       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
5824       xops[2] = gen_rtx_MEM (Pmode,
5825                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
5826       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5827
5828       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
5829          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
5830          an unadorned address.  */
5831       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5832       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
5833       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
5834       return "";
5835     }
5836
5837   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
5838
5839   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
5840     {
5841       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
5842
5843       if (!flag_pic)
5844         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5845       else
5846         output_asm_insn ("call\t%a2", xops);
5847
5848 #if TARGET_MACHO
5849       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5850          is what will be referenced by the Mach-O PIC subsystem.  */
5851       if (!label)
5852         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5853 #endif
5854
5855       (*targetm.asm_out.internal_label) (asm_out_file, "L",
5856                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
5857
5858       if (flag_pic)
5859         output_asm_insn ("pop{l}\t%0", xops);
5860     }
5861   else
5862     {
5863       char name[32];
5864       get_pc_thunk_name (name, REGNO (dest));
5865       pic_labels_used |= 1 << REGNO (dest);
5866
5867       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5868       xops[2] = gen_rtx_MEM (QImode, xops[2]);
5869       output_asm_insn ("call\t%X2", xops);
5870       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5871          is what will be referenced by the Mach-O PIC subsystem.  */
5872 #if TARGET_MACHO
5873       if (!label)
5874         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5875       else
5876         targetm.asm_out.internal_label (asm_out_file, "L",
5877                                            CODE_LABEL_NUMBER (label));
5878 #endif
5879     }
5880
5881   if (TARGET_MACHO)
5882     return "";
5883
5884   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
5885     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
5886   else
5887     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
5888
5889   return "";
5890 }
5891
5892 /* Generate an "push" pattern for input ARG.  */
5893
5894 static rtx
5895 gen_push (rtx arg)
5896 {
5897   return gen_rtx_SET (VOIDmode,
5898                       gen_rtx_MEM (Pmode,
5899                                    gen_rtx_PRE_DEC (Pmode,
5900                                                     stack_pointer_rtx)),
5901                       arg);
5902 }
5903
5904 /* Return >= 0 if there is an unused call-clobbered register available
5905    for the entire function.  */
5906
5907 static unsigned int
5908 ix86_select_alt_pic_regnum (void)
5909 {
5910   if (current_function_is_leaf && !current_function_profile
5911       && !ix86_current_function_calls_tls_descriptor)
5912     {
5913       int i;
5914       for (i = 2; i >= 0; --i)
5915         if (!df_regs_ever_live_p (i))
5916           return i;
5917     }
5918
5919   return INVALID_REGNUM;
5920 }
5921
5922 /* Return 1 if we need to save REGNO.  */
5923 static int
5924 ix86_save_reg (unsigned int regno, int maybe_eh_return)
5925 {
5926   if (pic_offset_table_rtx
5927       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5928       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
5929           || current_function_profile
5930           || current_function_calls_eh_return
5931           || current_function_uses_const_pool))
5932     {
5933       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5934         return 0;
5935       return 1;
5936     }
5937
5938   if (current_function_calls_eh_return && maybe_eh_return)
5939     {
5940       unsigned i;
5941       for (i = 0; ; i++)
5942         {
5943           unsigned test = EH_RETURN_DATA_REGNO (i);
5944           if (test == INVALID_REGNUM)
5945             break;
5946           if (test == regno)
5947             return 1;
5948         }
5949     }
5950
5951   if (cfun->machine->force_align_arg_pointer
5952       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5953     return 1;
5954
5955   return (df_regs_ever_live_p (regno)
5956           && !call_used_regs[regno]
5957           && !fixed_regs[regno]
5958           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5959 }
5960
5961 /* Return number of registers to be saved on the stack.  */
5962
5963 static int
5964 ix86_nsaved_regs (void)
5965 {
5966   int nregs = 0;
5967   int regno;
5968
5969   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5970     if (ix86_save_reg (regno, true))
5971       nregs++;
5972   return nregs;
5973 }
5974
5975 /* Return the offset between two registers, one to be eliminated, and the other
5976    its replacement, at the start of a routine.  */
5977
5978 HOST_WIDE_INT
5979 ix86_initial_elimination_offset (int from, int to)
5980 {
5981   struct ix86_frame frame;
5982   ix86_compute_frame_layout (&frame);
5983
5984   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5985     return frame.hard_frame_pointer_offset;
5986   else if (from == FRAME_POINTER_REGNUM
5987            && to == HARD_FRAME_POINTER_REGNUM)
5988     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5989   else
5990     {
5991       gcc_assert (to == STACK_POINTER_REGNUM);
5992
5993       if (from == ARG_POINTER_REGNUM)
5994         return frame.stack_pointer_offset;
5995
5996       gcc_assert (from == FRAME_POINTER_REGNUM);
5997       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5998     }
5999 }
6000
6001 /* Fill structure ix86_frame about frame of currently computed function.  */
6002
6003 static void
6004 ix86_compute_frame_layout (struct ix86_frame *frame)
6005 {
6006   HOST_WIDE_INT total_size;
6007   unsigned int stack_alignment_needed;
6008   HOST_WIDE_INT offset;
6009   unsigned int preferred_alignment;
6010   HOST_WIDE_INT size = get_frame_size ();
6011
6012   frame->nregs = ix86_nsaved_regs ();
6013   total_size = size;
6014
6015   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
6016   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
6017
6018   /* During reload iteration the amount of registers saved can change.
6019      Recompute the value as needed.  Do not recompute when amount of registers
6020      didn't change as reload does multiple calls to the function and does not
6021      expect the decision to change within single iteration.  */
6022   if (!optimize_size
6023       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
6024     {
6025       int count = frame->nregs;
6026
6027       cfun->machine->use_fast_prologue_epilogue_nregs = count;
6028       /* The fast prologue uses move instead of push to save registers.  This
6029          is significantly longer, but also executes faster as modern hardware
6030          can execute the moves in parallel, but can't do that for push/pop.
6031
6032          Be careful about choosing what prologue to emit:  When function takes
6033          many instructions to execute we may use slow version as well as in
6034          case function is known to be outside hot spot (this is known with
6035          feedback only).  Weight the size of function by number of registers
6036          to save as it is cheap to use one or two push instructions but very
6037          slow to use many of them.  */
6038       if (count)
6039         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
6040       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
6041           || (flag_branch_probabilities
6042               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
6043         cfun->machine->use_fast_prologue_epilogue = false;
6044       else
6045         cfun->machine->use_fast_prologue_epilogue
6046            = !expensive_function_p (count);
6047     }
6048   if (TARGET_PROLOGUE_USING_MOVE
6049       && cfun->machine->use_fast_prologue_epilogue)
6050     frame->save_regs_using_mov = true;
6051   else
6052     frame->save_regs_using_mov = false;
6053
6054
6055   /* Skip return address and saved base pointer.  */
6056   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
6057
6058   frame->hard_frame_pointer_offset = offset;
6059
6060   /* Do some sanity checking of stack_alignment_needed and
6061      preferred_alignment, since i386 port is the only using those features
6062      that may break easily.  */
6063
6064   gcc_assert (!size || stack_alignment_needed);
6065   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
6066   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
6067   gcc_assert (stack_alignment_needed
6068               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
6069
6070   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
6071     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
6072
6073   /* Register save area */
6074   offset += frame->nregs * UNITS_PER_WORD;
6075
6076   /* Va-arg area */
6077   if (ix86_save_varrargs_registers)
6078     {
6079       offset += X86_64_VARARGS_SIZE;
6080       frame->va_arg_size = X86_64_VARARGS_SIZE;
6081     }
6082   else
6083     frame->va_arg_size = 0;
6084
6085   /* Align start of frame for local function.  */
6086   frame->padding1 = ((offset + stack_alignment_needed - 1)
6087                      & -stack_alignment_needed) - offset;
6088
6089   offset += frame->padding1;
6090
6091   /* Frame pointer points here.  */
6092   frame->frame_pointer_offset = offset;
6093
6094   offset += size;
6095
6096   /* Add outgoing arguments area.  Can be skipped if we eliminated
6097      all the function calls as dead code.
6098      Skipping is however impossible when function calls alloca.  Alloca
6099      expander assumes that last current_function_outgoing_args_size
6100      of stack frame are unused.  */
6101   if (ACCUMULATE_OUTGOING_ARGS
6102       && (!current_function_is_leaf || current_function_calls_alloca
6103           || ix86_current_function_calls_tls_descriptor))
6104     {
6105       offset += current_function_outgoing_args_size;
6106       frame->outgoing_arguments_size = current_function_outgoing_args_size;
6107     }
6108   else
6109     frame->outgoing_arguments_size = 0;
6110
6111   /* Align stack boundary.  Only needed if we're calling another function
6112      or using alloca.  */
6113   if (!current_function_is_leaf || current_function_calls_alloca
6114       || ix86_current_function_calls_tls_descriptor)
6115     frame->padding2 = ((offset + preferred_alignment - 1)
6116                        & -preferred_alignment) - offset;
6117   else
6118     frame->padding2 = 0;
6119
6120   offset += frame->padding2;
6121
6122   /* We've reached end of stack frame.  */
6123   frame->stack_pointer_offset = offset;
6124
6125   /* Size prologue needs to allocate.  */
6126   frame->to_allocate =
6127     (size + frame->padding1 + frame->padding2
6128      + frame->outgoing_arguments_size + frame->va_arg_size);
6129
6130   if ((!frame->to_allocate && frame->nregs <= 1)
6131       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
6132     frame->save_regs_using_mov = false;
6133
6134   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
6135       && current_function_is_leaf
6136       && !ix86_current_function_calls_tls_descriptor)
6137     {
6138       frame->red_zone_size = frame->to_allocate;
6139       if (frame->save_regs_using_mov)
6140         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
6141       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
6142         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
6143     }
6144   else
6145     frame->red_zone_size = 0;
6146   frame->to_allocate -= frame->red_zone_size;
6147   frame->stack_pointer_offset -= frame->red_zone_size;
6148 #if 0
6149   fprintf (stderr, "\n");
6150   fprintf (stderr, "nregs: %ld\n", (long)frame->nregs);
6151   fprintf (stderr, "size: %ld\n", (long)size);
6152   fprintf (stderr, "alignment1: %ld\n", (long)stack_alignment_needed);
6153   fprintf (stderr, "padding1: %ld\n", (long)frame->padding1);
6154   fprintf (stderr, "va_arg: %ld\n", (long)frame->va_arg_size);
6155   fprintf (stderr, "padding2: %ld\n", (long)frame->padding2);
6156   fprintf (stderr, "to_allocate: %ld\n", (long)frame->to_allocate);
6157   fprintf (stderr, "red_zone_size: %ld\n", (long)frame->red_zone_size);
6158   fprintf (stderr, "frame_pointer_offset: %ld\n", (long)frame->frame_pointer_offset);
6159   fprintf (stderr, "hard_frame_pointer_offset: %ld\n",
6160            (long)frame->hard_frame_pointer_offset);
6161   fprintf (stderr, "stack_pointer_offset: %ld\n", (long)frame->stack_pointer_offset);
6162   fprintf (stderr, "current_function_is_leaf: %ld\n", (long)current_function_is_leaf);
6163   fprintf (stderr, "current_function_calls_alloca: %ld\n", (long)current_function_calls_alloca);
6164   fprintf (stderr, "x86_current_function_calls_tls_descriptor: %ld\n", (long)ix86_current_function_calls_tls_descriptor);
6165 #endif
6166 }
6167
6168 /* Emit code to save registers in the prologue.  */
6169
6170 static void
6171 ix86_emit_save_regs (void)
6172 {
6173   unsigned int regno;
6174   rtx insn;
6175
6176   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
6177     if (ix86_save_reg (regno, true))
6178       {
6179         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
6180         RTX_FRAME_RELATED_P (insn) = 1;
6181       }
6182 }
6183
6184 /* Emit code to save registers using MOV insns.  First register
6185    is restored from POINTER + OFFSET.  */
6186 static void
6187 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
6188 {
6189   unsigned int regno;
6190   rtx insn;
6191
6192   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6193     if (ix86_save_reg (regno, true))
6194       {
6195         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
6196                                                Pmode, offset),
6197                                gen_rtx_REG (Pmode, regno));
6198         RTX_FRAME_RELATED_P (insn) = 1;
6199         offset += UNITS_PER_WORD;
6200       }
6201 }
6202
6203 /* Expand prologue or epilogue stack adjustment.
6204    The pattern exist to put a dependency on all ebp-based memory accesses.
6205    STYLE should be negative if instructions should be marked as frame related,
6206    zero if %r11 register is live and cannot be freely used and positive
6207    otherwise.  */
6208
6209 static void
6210 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
6211 {
6212   rtx insn;
6213
6214   if (! TARGET_64BIT)
6215     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
6216   else if (x86_64_immediate_operand (offset, DImode))
6217     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
6218   else
6219     {
6220       rtx r11;
6221       /* r11 is used by indirect sibcall return as well, set before the
6222          epilogue and used after the epilogue.  ATM indirect sibcall
6223          shouldn't be used together with huge frame sizes in one
6224          function because of the frame_size check in sibcall.c.  */
6225       gcc_assert (style);
6226       r11 = gen_rtx_REG (DImode, R11_REG);
6227       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
6228       if (style < 0)
6229         RTX_FRAME_RELATED_P (insn) = 1;
6230       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
6231                                                                offset));
6232     }
6233   if (style < 0)
6234     RTX_FRAME_RELATED_P (insn) = 1;
6235 }
6236
6237 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
6238
6239 static rtx
6240 ix86_internal_arg_pointer (void)
6241 {
6242   bool has_force_align_arg_pointer =
6243     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
6244                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
6245   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
6246        && DECL_NAME (current_function_decl)
6247        && MAIN_NAME_P (DECL_NAME (current_function_decl))
6248        && DECL_FILE_SCOPE_P (current_function_decl))
6249       || ix86_force_align_arg_pointer
6250       || has_force_align_arg_pointer)
6251     {
6252       /* Nested functions can't realign the stack due to a register
6253          conflict.  */
6254       if (DECL_CONTEXT (current_function_decl)
6255           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
6256         {
6257           if (ix86_force_align_arg_pointer)
6258             warning (0, "-mstackrealign ignored for nested functions");
6259           if (has_force_align_arg_pointer)
6260             error ("%s not supported for nested functions",
6261                    ix86_force_align_arg_pointer_string);
6262           return virtual_incoming_args_rtx;
6263         }
6264       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, CX_REG);
6265       return copy_to_reg (cfun->machine->force_align_arg_pointer);
6266     }
6267   else
6268     return virtual_incoming_args_rtx;
6269 }
6270
6271 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
6272    This is called from dwarf2out.c to emit call frame instructions
6273    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
6274 static void
6275 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
6276 {
6277   rtx unspec = SET_SRC (pattern);
6278   gcc_assert (GET_CODE (unspec) == UNSPEC);
6279
6280   switch (index)
6281     {
6282     case UNSPEC_REG_SAVE:
6283       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
6284                               SET_DEST (pattern));
6285       break;
6286     case UNSPEC_DEF_CFA:
6287       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
6288                          INTVAL (XVECEXP (unspec, 0, 0)));
6289       break;
6290     default:
6291       gcc_unreachable ();
6292     }
6293 }
6294
6295 /* Expand the prologue into a bunch of separate insns.  */
6296
6297 void
6298 ix86_expand_prologue (void)
6299 {
6300   rtx insn;
6301   bool pic_reg_used;
6302   struct ix86_frame frame;
6303   HOST_WIDE_INT allocate;
6304
6305   ix86_compute_frame_layout (&frame);
6306
6307   if (cfun->machine->force_align_arg_pointer)
6308     {
6309       rtx x, y;
6310
6311       /* Grab the argument pointer.  */
6312       x = plus_constant (stack_pointer_rtx, 4);
6313       y = cfun->machine->force_align_arg_pointer;
6314       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
6315       RTX_FRAME_RELATED_P (insn) = 1;
6316
6317       /* The unwind info consists of two parts: install the fafp as the cfa,
6318          and record the fafp as the "save register" of the stack pointer.
6319          The later is there in order that the unwinder can see where it
6320          should restore the stack pointer across the and insn.  */
6321       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
6322       x = gen_rtx_SET (VOIDmode, y, x);
6323       RTX_FRAME_RELATED_P (x) = 1;
6324       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
6325                           UNSPEC_REG_SAVE);
6326       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
6327       RTX_FRAME_RELATED_P (y) = 1;
6328       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
6329       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
6330       REG_NOTES (insn) = x;
6331
6332       /* Align the stack.  */
6333       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
6334                              GEN_INT (-16)));
6335
6336       /* And here we cheat like madmen with the unwind info.  We force the
6337          cfa register back to sp+4, which is exactly what it was at the
6338          start of the function.  Re-pushing the return address results in
6339          the return at the same spot relative to the cfa, and thus is
6340          correct wrt the unwind info.  */
6341       x = cfun->machine->force_align_arg_pointer;
6342       x = gen_frame_mem (Pmode, plus_constant (x, -4));
6343       insn = emit_insn (gen_push (x));
6344       RTX_FRAME_RELATED_P (insn) = 1;
6345
6346       x = GEN_INT (4);
6347       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
6348       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
6349       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
6350       REG_NOTES (insn) = x;
6351     }
6352
6353   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
6354      slower on all targets.  Also sdb doesn't like it.  */
6355
6356   if (frame_pointer_needed)
6357     {
6358       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
6359       RTX_FRAME_RELATED_P (insn) = 1;
6360
6361       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
6362       RTX_FRAME_RELATED_P (insn) = 1;
6363     }
6364
6365   allocate = frame.to_allocate;
6366
6367   if (!frame.save_regs_using_mov)
6368     ix86_emit_save_regs ();
6369   else
6370     allocate += frame.nregs * UNITS_PER_WORD;
6371
6372   /* When using red zone we may start register saving before allocating
6373      the stack frame saving one cycle of the prologue. However I will
6374      avoid doing this if I am going to have to probe the stack since
6375      at least on x86_64 the stack probe can turn into a call that clobbers
6376      a red zone location */
6377   if (TARGET_RED_ZONE && frame.save_regs_using_mov
6378       && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT))
6379     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
6380                                    : stack_pointer_rtx,
6381                                    -frame.nregs * UNITS_PER_WORD);
6382
6383   if (allocate == 0)
6384     ;
6385   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
6386     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6387                                GEN_INT (-allocate), -1);
6388   else
6389     {
6390       /* Only valid for Win32.  */
6391       rtx eax = gen_rtx_REG (Pmode, AX_REG);
6392       bool eax_live;
6393       rtx t;
6394
6395       gcc_assert (!TARGET_64BIT || TARGET_64BIT_MS_ABI);
6396
6397       if (TARGET_64BIT_MS_ABI)
6398         eax_live = false;
6399       else
6400         eax_live = ix86_eax_live_at_start_p ();
6401
6402       if (eax_live)
6403         {
6404           emit_insn (gen_push (eax));
6405           allocate -= UNITS_PER_WORD;
6406         }
6407
6408       emit_move_insn (eax, GEN_INT (allocate));
6409
6410       if (TARGET_64BIT)
6411         insn = gen_allocate_stack_worker_64 (eax);
6412       else
6413         insn = gen_allocate_stack_worker_32 (eax);
6414       insn = emit_insn (insn);
6415       RTX_FRAME_RELATED_P (insn) = 1;
6416       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
6417       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
6418       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
6419                                             t, REG_NOTES (insn));
6420
6421       if (eax_live)
6422         {
6423           if (frame_pointer_needed)
6424             t = plus_constant (hard_frame_pointer_rtx,
6425                                allocate
6426                                - frame.to_allocate
6427                                - frame.nregs * UNITS_PER_WORD);
6428           else
6429             t = plus_constant (stack_pointer_rtx, allocate);
6430           emit_move_insn (eax, gen_rtx_MEM (Pmode, t));
6431         }
6432     }
6433
6434   if (frame.save_regs_using_mov
6435       && !(TARGET_RED_ZONE
6436          && (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)))
6437     {
6438       if (!frame_pointer_needed || !frame.to_allocate)
6439         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
6440       else
6441         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
6442                                        -frame.nregs * UNITS_PER_WORD);
6443     }
6444
6445   pic_reg_used = false;
6446   if (pic_offset_table_rtx
6447       && (df_regs_ever_live_p (REAL_PIC_OFFSET_TABLE_REGNUM)
6448           || current_function_profile))
6449     {
6450       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
6451
6452       if (alt_pic_reg_used != INVALID_REGNUM)
6453         SET_REGNO (pic_offset_table_rtx, alt_pic_reg_used);
6454
6455       pic_reg_used = true;
6456     }
6457
6458   if (pic_reg_used)
6459     {
6460       if (TARGET_64BIT)
6461         {
6462           if (ix86_cmodel == CM_LARGE_PIC)
6463             {
6464               rtx tmp_reg = gen_rtx_REG (DImode, R11_REG);
6465               rtx label = gen_label_rtx ();
6466               emit_label (label);
6467               LABEL_PRESERVE_P (label) = 1;
6468               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
6469               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
6470               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
6471               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
6472                                             pic_offset_table_rtx, tmp_reg));
6473             }
6474           else
6475             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
6476         }
6477       else
6478         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
6479     }
6480
6481   /* Prevent function calls from being scheduled before the call to mcount.
6482      In the pic_reg_used case, make sure that the got load isn't deleted.  */
6483   if (current_function_profile)
6484     {
6485       if (pic_reg_used)
6486         emit_insn (gen_prologue_use (pic_offset_table_rtx));
6487       emit_insn (gen_blockage ());
6488     }
6489 }
6490
6491 /* Emit code to restore saved registers using MOV insns.  First register
6492    is restored from POINTER + OFFSET.  */
6493 static void
6494 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
6495                                   int maybe_eh_return)
6496 {
6497   int regno;
6498   rtx base_address = gen_rtx_MEM (Pmode, pointer);
6499
6500   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6501     if (ix86_save_reg (regno, maybe_eh_return))
6502       {
6503         /* Ensure that adjust_address won't be forced to produce pointer
6504            out of range allowed by x86-64 instruction set.  */
6505         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
6506           {
6507             rtx r11;
6508
6509             r11 = gen_rtx_REG (DImode, R11_REG);
6510             emit_move_insn (r11, GEN_INT (offset));
6511             emit_insn (gen_adddi3 (r11, r11, pointer));
6512             base_address = gen_rtx_MEM (Pmode, r11);
6513             offset = 0;
6514           }
6515         emit_move_insn (gen_rtx_REG (Pmode, regno),
6516                         adjust_address (base_address, Pmode, offset));
6517         offset += UNITS_PER_WORD;
6518       }
6519 }
6520
6521 /* Restore function stack, frame, and registers.  */
6522
6523 void
6524 ix86_expand_epilogue (int style)
6525 {
6526   int regno;
6527   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
6528   struct ix86_frame frame;
6529   HOST_WIDE_INT offset;
6530
6531   ix86_compute_frame_layout (&frame);
6532
6533   /* Calculate start of saved registers relative to ebp.  Special care
6534      must be taken for the normal return case of a function using
6535      eh_return: the eax and edx registers are marked as saved, but not
6536      restored along this path.  */
6537   offset = frame.nregs;
6538   if (current_function_calls_eh_return && style != 2)
6539     offset -= 2;
6540   offset *= -UNITS_PER_WORD;
6541
6542   /* If we're only restoring one register and sp is not valid then
6543      using a move instruction to restore the register since it's
6544      less work than reloading sp and popping the register.
6545
6546      The default code result in stack adjustment using add/lea instruction,
6547      while this code results in LEAVE instruction (or discrete equivalent),
6548      so it is profitable in some other cases as well.  Especially when there
6549      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
6550      and there is exactly one register to pop. This heuristic may need some
6551      tuning in future.  */
6552   if ((!sp_valid && frame.nregs <= 1)
6553       || (TARGET_EPILOGUE_USING_MOVE
6554           && cfun->machine->use_fast_prologue_epilogue
6555           && (frame.nregs > 1 || frame.to_allocate))
6556       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
6557       || (frame_pointer_needed && TARGET_USE_LEAVE
6558           && cfun->machine->use_fast_prologue_epilogue
6559           && frame.nregs == 1)
6560       || current_function_calls_eh_return)
6561     {
6562       /* Restore registers.  We can use ebp or esp to address the memory
6563          locations.  If both are available, default to ebp, since offsets
6564          are known to be small.  Only exception is esp pointing directly to the
6565          end of block of saved registers, where we may simplify addressing
6566          mode.  */
6567
6568       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
6569         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
6570                                           frame.to_allocate, style == 2);
6571       else
6572         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
6573                                           offset, style == 2);
6574
6575       /* eh_return epilogues need %ecx added to the stack pointer.  */
6576       if (style == 2)
6577         {
6578           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
6579
6580           if (frame_pointer_needed)
6581             {
6582               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
6583               tmp = plus_constant (tmp, UNITS_PER_WORD);
6584               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
6585
6586               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
6587               emit_move_insn (hard_frame_pointer_rtx, tmp);
6588
6589               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
6590                                          const0_rtx, style);
6591             }
6592           else
6593             {
6594               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
6595               tmp = plus_constant (tmp, (frame.to_allocate
6596                                          + frame.nregs * UNITS_PER_WORD));
6597               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
6598             }
6599         }
6600       else if (!frame_pointer_needed)
6601         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6602                                    GEN_INT (frame.to_allocate
6603                                             + frame.nregs * UNITS_PER_WORD),
6604                                    style);
6605       /* If not an i386, mov & pop is faster than "leave".  */
6606       else if (TARGET_USE_LEAVE || optimize_size
6607                || !cfun->machine->use_fast_prologue_epilogue)
6608         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6609       else
6610         {
6611           pro_epilogue_adjust_stack (stack_pointer_rtx,
6612                                      hard_frame_pointer_rtx,
6613                                      const0_rtx, style);
6614           if (TARGET_64BIT)
6615             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6616           else
6617             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6618         }
6619     }
6620   else
6621     {
6622       /* First step is to deallocate the stack frame so that we can
6623          pop the registers.  */
6624       if (!sp_valid)
6625         {
6626           gcc_assert (frame_pointer_needed);
6627           pro_epilogue_adjust_stack (stack_pointer_rtx,
6628                                      hard_frame_pointer_rtx,
6629                                      GEN_INT (offset), style);
6630         }
6631       else if (frame.to_allocate)
6632         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6633                                    GEN_INT (frame.to_allocate), style);
6634
6635       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6636         if (ix86_save_reg (regno, false))
6637           {
6638             if (TARGET_64BIT)
6639               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
6640             else
6641               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
6642           }
6643       if (frame_pointer_needed)
6644         {
6645           /* Leave results in shorter dependency chains on CPUs that are
6646              able to grok it fast.  */
6647           if (TARGET_USE_LEAVE)
6648             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6649           else if (TARGET_64BIT)
6650             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6651           else
6652             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6653         }
6654     }
6655
6656   if (cfun->machine->force_align_arg_pointer)
6657     {
6658       emit_insn (gen_addsi3 (stack_pointer_rtx,
6659                              cfun->machine->force_align_arg_pointer,
6660                              GEN_INT (-4)));
6661     }
6662
6663   /* Sibcall epilogues don't want a return instruction.  */
6664   if (style == 0)
6665     return;
6666
6667   if (current_function_pops_args && current_function_args_size)
6668     {
6669       rtx popc = GEN_INT (current_function_pops_args);
6670
6671       /* i386 can only pop 64K bytes.  If asked to pop more, pop
6672          return address, do explicit add, and jump indirectly to the
6673          caller.  */
6674
6675       if (current_function_pops_args >= 65536)
6676         {
6677           rtx ecx = gen_rtx_REG (SImode, CX_REG);
6678
6679           /* There is no "pascal" calling convention in any 64bit ABI.  */
6680           gcc_assert (!TARGET_64BIT);
6681
6682           emit_insn (gen_popsi1 (ecx));
6683           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
6684           emit_jump_insn (gen_return_indirect_internal (ecx));
6685         }
6686       else
6687         emit_jump_insn (gen_return_pop_internal (popc));
6688     }
6689   else
6690     emit_jump_insn (gen_return_internal ());
6691 }
6692
6693 /* Reset from the function's potential modifications.  */
6694
6695 static void
6696 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6697                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6698 {
6699   if (pic_offset_table_rtx)
6700     SET_REGNO (pic_offset_table_rtx, REAL_PIC_OFFSET_TABLE_REGNUM);
6701 #if TARGET_MACHO
6702   /* Mach-O doesn't support labels at the end of objects, so if
6703      it looks like we might want one, insert a NOP.  */
6704   {
6705     rtx insn = get_last_insn ();
6706     while (insn
6707            && NOTE_P (insn)
6708            && NOTE_KIND (insn) != NOTE_INSN_DELETED_LABEL)
6709       insn = PREV_INSN (insn);
6710     if (insn
6711         && (LABEL_P (insn)
6712             || (NOTE_P (insn)
6713                 && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL)))
6714       fputs ("\tnop\n", file);
6715   }
6716 #endif
6717
6718 }
6719 \f
6720 /* Extract the parts of an RTL expression that is a valid memory address
6721    for an instruction.  Return 0 if the structure of the address is
6722    grossly off.  Return -1 if the address contains ASHIFT, so it is not
6723    strictly valid, but still used for computing length of lea instruction.  */
6724
6725 int
6726 ix86_decompose_address (rtx addr, struct ix86_address *out)
6727 {
6728   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
6729   rtx base_reg, index_reg;
6730   HOST_WIDE_INT scale = 1;
6731   rtx scale_rtx = NULL_RTX;
6732   int retval = 1;
6733   enum ix86_address_seg seg = SEG_DEFAULT;
6734
6735   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
6736     base = addr;
6737   else if (GET_CODE (addr) == PLUS)
6738     {
6739       rtx addends[4], op;
6740       int n = 0, i;
6741
6742       op = addr;
6743       do
6744         {
6745           if (n >= 4)
6746             return 0;
6747           addends[n++] = XEXP (op, 1);
6748           op = XEXP (op, 0);
6749         }
6750       while (GET_CODE (op) == PLUS);
6751       if (n >= 4)
6752         return 0;
6753       addends[n] = op;
6754
6755       for (i = n; i >= 0; --i)
6756         {
6757           op = addends[i];
6758           switch (GET_CODE (op))
6759             {
6760             case MULT:
6761               if (index)
6762                 return 0;
6763               index = XEXP (op, 0);
6764               scale_rtx = XEXP (op, 1);
6765               break;
6766
6767             case UNSPEC:
6768               if (XINT (op, 1) == UNSPEC_TP
6769                   && TARGET_TLS_DIRECT_SEG_REFS
6770                   && seg == SEG_DEFAULT)
6771                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
6772               else
6773                 return 0;
6774               break;
6775
6776             case REG:
6777             case SUBREG:
6778               if (!base)
6779                 base = op;
6780               else if (!index)
6781                 index = op;
6782               else
6783                 return 0;
6784               break;
6785
6786             case CONST:
6787             case CONST_INT:
6788             case SYMBOL_REF:
6789             case LABEL_REF:
6790               if (disp)
6791                 return 0;
6792               disp = op;
6793               break;
6794
6795             default:
6796               return 0;
6797             }
6798         }
6799     }
6800   else if (GET_CODE (addr) == MULT)
6801     {
6802       index = XEXP (addr, 0);           /* index*scale */
6803       scale_rtx = XEXP (addr, 1);
6804     }
6805   else if (GET_CODE (addr) == ASHIFT)
6806     {
6807       rtx tmp;
6808
6809       /* We're called for lea too, which implements ashift on occasion.  */
6810       index = XEXP (addr, 0);
6811       tmp = XEXP (addr, 1);
6812       if (!CONST_INT_P (tmp))
6813         return 0;
6814       scale = INTVAL (tmp);
6815       if ((unsigned HOST_WIDE_INT) scale > 3)
6816         return 0;
6817       scale = 1 << scale;
6818       retval = -1;
6819     }
6820   else
6821     disp = addr;                        /* displacement */
6822
6823   /* Extract the integral value of scale.  */
6824   if (scale_rtx)
6825     {
6826       if (!CONST_INT_P (scale_rtx))
6827         return 0;
6828       scale = INTVAL (scale_rtx);
6829     }
6830
6831   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
6832   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
6833
6834   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
6835   if (base_reg && index_reg && scale == 1
6836       && (index_reg == arg_pointer_rtx
6837           || index_reg == frame_pointer_rtx
6838           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
6839     {
6840       rtx tmp;
6841       tmp = base, base = index, index = tmp;
6842       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
6843     }
6844
6845   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
6846   if ((base_reg == hard_frame_pointer_rtx
6847        || base_reg == frame_pointer_rtx
6848        || base_reg == arg_pointer_rtx) && !disp)
6849     disp = const0_rtx;
6850
6851   /* Special case: on K6, [%esi] makes the instruction vector decoded.
6852      Avoid this by transforming to [%esi+0].  */
6853   if (TARGET_K6 && !optimize_size
6854       && base_reg && !index_reg && !disp
6855       && REG_P (base_reg)
6856       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
6857     disp = const0_rtx;
6858
6859   /* Special case: encode reg+reg instead of reg*2.  */
6860   if (!base && index && scale && scale == 2)
6861     base = index, base_reg = index_reg, scale = 1;
6862
6863   /* Special case: scaling cannot be encoded without base or displacement.  */
6864   if (!base && !disp && index && scale != 1)
6865     disp = const0_rtx;
6866
6867   out->base = base;
6868   out->index = index;
6869   out->disp = disp;
6870   out->scale = scale;
6871   out->seg = seg;
6872
6873   return retval;
6874 }
6875 \f
6876 /* Return cost of the memory address x.
6877    For i386, it is better to use a complex address than let gcc copy
6878    the address into a reg and make a new pseudo.  But not if the address
6879    requires to two regs - that would mean more pseudos with longer
6880    lifetimes.  */
6881 static int
6882 ix86_address_cost (rtx x)
6883 {
6884   struct ix86_address parts;
6885   int cost = 1;
6886   int ok = ix86_decompose_address (x, &parts);
6887
6888   gcc_assert (ok);
6889
6890   if (parts.base && GET_CODE (parts.base) == SUBREG)
6891     parts.base = SUBREG_REG (parts.base);
6892   if (parts.index && GET_CODE (parts.index) == SUBREG)
6893     parts.index = SUBREG_REG (parts.index);
6894
6895   /* Attempt to minimize number of registers in the address.  */
6896   if ((parts.base
6897        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
6898       || (parts.index
6899           && (!REG_P (parts.index)
6900               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
6901     cost++;
6902
6903   if (parts.base
6904       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
6905       && parts.index
6906       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
6907       && parts.base != parts.index)
6908     cost++;
6909
6910   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
6911      since it's predecode logic can't detect the length of instructions
6912      and it degenerates to vector decoded.  Increase cost of such
6913      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
6914      to split such addresses or even refuse such addresses at all.
6915
6916      Following addressing modes are affected:
6917       [base+scale*index]
6918       [scale*index+disp]
6919       [base+index]
6920
6921      The first and last case  may be avoidable by explicitly coding the zero in
6922      memory address, but I don't have AMD-K6 machine handy to check this
6923      theory.  */
6924
6925   if (TARGET_K6
6926       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
6927           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
6928           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
6929     cost += 10;
6930
6931   return cost;
6932 }
6933 \f
6934 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6935    this is used for to form addresses to local data when -fPIC is in
6936    use.  */
6937
6938 static bool
6939 darwin_local_data_pic (rtx disp)
6940 {
6941   if (GET_CODE (disp) == MINUS)
6942     {
6943       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6944           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6945         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6946           {
6947             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6948             if (! strcmp (sym_name, "<pic base>"))
6949               return true;
6950           }
6951     }
6952
6953   return false;
6954 }
6955
6956 /* Determine if a given RTX is a valid constant.  We already know this
6957    satisfies CONSTANT_P.  */
6958
6959 bool
6960 legitimate_constant_p (rtx x)
6961 {
6962   switch (GET_CODE (x))
6963     {
6964     case CONST:
6965       x = XEXP (x, 0);
6966
6967       if (GET_CODE (x) == PLUS)
6968         {
6969           if (!CONST_INT_P (XEXP (x, 1)))
6970             return false;
6971           x = XEXP (x, 0);
6972         }
6973
6974       if (TARGET_MACHO && darwin_local_data_pic (x))
6975         return true;
6976
6977       /* Only some unspecs are valid as "constants".  */
6978       if (GET_CODE (x) == UNSPEC)
6979         switch (XINT (x, 1))
6980           {
6981           case UNSPEC_GOT:
6982           case UNSPEC_GOTOFF:
6983           case UNSPEC_PLTOFF:
6984             return TARGET_64BIT;
6985           case UNSPEC_TPOFF:
6986           case UNSPEC_NTPOFF:
6987             x = XVECEXP (x, 0, 0);
6988             return (GET_CODE (x) == SYMBOL_REF
6989                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6990           case UNSPEC_DTPOFF:
6991             x = XVECEXP (x, 0, 0);
6992             return (GET_CODE (x) == SYMBOL_REF
6993                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6994           default:
6995             return false;
6996           }
6997
6998       /* We must have drilled down to a symbol.  */
6999       if (GET_CODE (x) == LABEL_REF)
7000         return true;
7001       if (GET_CODE (x) != SYMBOL_REF)
7002         return false;
7003       /* FALLTHRU */
7004
7005     case SYMBOL_REF:
7006       /* TLS symbols are never valid.  */
7007       if (SYMBOL_REF_TLS_MODEL (x))
7008         return false;
7009
7010       /* DLLIMPORT symbols are never valid.  */
7011       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
7012           && SYMBOL_REF_DLLIMPORT_P (x))
7013         return false;
7014       break;
7015
7016     case CONST_DOUBLE:
7017       if (GET_MODE (x) == TImode
7018           && x != CONST0_RTX (TImode)
7019           && !TARGET_64BIT)
7020         return false;
7021       break;
7022
7023     case CONST_VECTOR:
7024       if (x == CONST0_RTX (GET_MODE (x)))
7025         return true;
7026       return false;
7027
7028     default:
7029       break;
7030     }
7031
7032   /* Otherwise we handle everything else in the move patterns.  */
7033   return true;
7034 }
7035
7036 /* Determine if it's legal to put X into the constant pool.  This
7037    is not possible for the address of thread-local symbols, which
7038    is checked above.  */
7039
7040 static bool
7041 ix86_cannot_force_const_mem (rtx x)
7042 {
7043   /* We can always put integral constants and vectors in memory.  */
7044   switch (GET_CODE (x))
7045     {
7046     case CONST_INT:
7047     case CONST_DOUBLE:
7048     case CONST_VECTOR:
7049       return false;
7050
7051     default:
7052       break;
7053     }
7054   return !legitimate_constant_p (x);
7055 }
7056
7057 /* Determine if a given RTX is a valid constant address.  */
7058
7059 bool
7060 constant_address_p (rtx x)
7061 {
7062   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
7063 }
7064
7065 /* Nonzero if the constant value X is a legitimate general operand
7066    when generating PIC code.  It is given that flag_pic is on and
7067    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
7068
7069 bool
7070 legitimate_pic_operand_p (rtx x)
7071 {
7072   rtx inner;
7073
7074   switch (GET_CODE (x))
7075     {
7076     case CONST:
7077       inner = XEXP (x, 0);
7078       if (GET_CODE (inner) == PLUS
7079           && CONST_INT_P (XEXP (inner, 1)))
7080         inner = XEXP (inner, 0);
7081
7082       /* Only some unspecs are valid as "constants".  */
7083       if (GET_CODE (inner) == UNSPEC)
7084         switch (XINT (inner, 1))
7085           {
7086           case UNSPEC_GOT:
7087           case UNSPEC_GOTOFF:
7088           case UNSPEC_PLTOFF:
7089             return TARGET_64BIT;
7090           case UNSPEC_TPOFF:
7091             x = XVECEXP (inner, 0, 0);
7092             return (GET_CODE (x) == SYMBOL_REF
7093                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
7094           default:
7095             return false;
7096           }
7097       /* FALLTHRU */
7098
7099     case SYMBOL_REF:
7100     case LABEL_REF:
7101       return legitimate_pic_address_disp_p (x);
7102
7103     default:
7104       return true;
7105     }
7106 }
7107
7108 /* Determine if a given CONST RTX is a valid memory displacement
7109    in PIC mode.  */
7110
7111 int
7112 legitimate_pic_address_disp_p (rtx disp)
7113 {
7114   bool saw_plus;
7115
7116   /* In 64bit mode we can allow direct addresses of symbols and labels
7117      when they are not dynamic symbols.  */
7118   if (TARGET_64BIT)
7119     {
7120       rtx op0 = disp, op1;
7121
7122       switch (GET_CODE (disp))
7123         {
7124         case LABEL_REF:
7125           return true;
7126
7127         case CONST:
7128           if (GET_CODE (XEXP (disp, 0)) != PLUS)
7129             break;
7130           op0 = XEXP (XEXP (disp, 0), 0);
7131           op1 = XEXP (XEXP (disp, 0), 1);
7132           if (!CONST_INT_P (op1)
7133               || INTVAL (op1) >= 16*1024*1024
7134               || INTVAL (op1) < -16*1024*1024)
7135             break;
7136           if (GET_CODE (op0) == LABEL_REF)
7137             return true;
7138           if (GET_CODE (op0) != SYMBOL_REF)
7139             break;
7140           /* FALLTHRU */
7141
7142         case SYMBOL_REF:
7143           /* TLS references should always be enclosed in UNSPEC.  */
7144           if (SYMBOL_REF_TLS_MODEL (op0))
7145             return false;
7146           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
7147               && ix86_cmodel != CM_LARGE_PIC)
7148             return true;
7149           break;
7150
7151         default:
7152           break;
7153         }
7154     }
7155   if (GET_CODE (disp) != CONST)
7156     return 0;
7157   disp = XEXP (disp, 0);
7158
7159   if (TARGET_64BIT)
7160     {
7161       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
7162          of GOT tables.  We should not need these anyway.  */
7163       if (GET_CODE (disp) != UNSPEC
7164           || (XINT (disp, 1) != UNSPEC_GOTPCREL
7165               && XINT (disp, 1) != UNSPEC_GOTOFF
7166               && XINT (disp, 1) != UNSPEC_PLTOFF))
7167         return 0;
7168
7169       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
7170           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
7171         return 0;
7172       return 1;
7173     }
7174
7175   saw_plus = false;
7176   if (GET_CODE (disp) == PLUS)
7177     {
7178       if (!CONST_INT_P (XEXP (disp, 1)))
7179         return 0;
7180       disp = XEXP (disp, 0);
7181       saw_plus = true;
7182     }
7183
7184   if (TARGET_MACHO && darwin_local_data_pic (disp))
7185     return 1;
7186
7187   if (GET_CODE (disp) != UNSPEC)
7188     return 0;
7189
7190   switch (XINT (disp, 1))
7191     {
7192     case UNSPEC_GOT:
7193       if (saw_plus)
7194         return false;
7195       /* We need to check for both symbols and labels because VxWorks loads
7196          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
7197          details.  */
7198       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
7199               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
7200     case UNSPEC_GOTOFF:
7201       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
7202          While ABI specify also 32bit relocation but we don't produce it in
7203          small PIC model at all.  */
7204       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
7205            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
7206           && !TARGET_64BIT)
7207         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
7208       return false;
7209     case UNSPEC_GOTTPOFF:
7210     case UNSPEC_GOTNTPOFF:
7211     case UNSPEC_INDNTPOFF:
7212       if (saw_plus)
7213         return false;
7214       disp = XVECEXP (disp, 0, 0);
7215       return (GET_CODE (disp) == SYMBOL_REF
7216               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
7217     case UNSPEC_NTPOFF:
7218       disp = XVECEXP (disp, 0, 0);
7219       return (GET_CODE (disp) == SYMBOL_REF
7220               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
7221     case UNSPEC_DTPOFF:
7222       disp = XVECEXP (disp, 0, 0);
7223       return (GET_CODE (disp) == SYMBOL_REF
7224               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
7225     }
7226
7227   return 0;
7228 }
7229
7230 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
7231    memory address for an instruction.  The MODE argument is the machine mode
7232    for the MEM expression that wants to use this address.
7233
7234    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
7235    convert common non-canonical forms to canonical form so that they will
7236    be recognized.  */
7237
7238 int
7239 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
7240                       rtx addr, int strict)
7241 {
7242   struct ix86_address parts;
7243   rtx base, index, disp;
7244   HOST_WIDE_INT scale;
7245   const char *reason = NULL;
7246   rtx reason_rtx = NULL_RTX;
7247
7248   if (ix86_decompose_address (addr, &parts) <= 0)
7249     {
7250       reason = "decomposition failed";
7251       goto report_error;
7252     }
7253
7254   base = parts.base;
7255   index = parts.index;
7256   disp = parts.disp;
7257   scale = parts.scale;
7258
7259   /* Validate base register.
7260
7261      Don't allow SUBREG's that span more than a word here.  It can lead to spill
7262      failures when the base is one word out of a two word structure, which is
7263      represented internally as a DImode int.  */
7264
7265   if (base)
7266     {
7267       rtx reg;
7268       reason_rtx = base;
7269
7270       if (REG_P (base))
7271         reg = base;
7272       else if (GET_CODE (base) == SUBREG
7273                && REG_P (SUBREG_REG (base))
7274                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
7275                   <= UNITS_PER_WORD)
7276         reg = SUBREG_REG (base);
7277       else
7278         {
7279           reason = "base is not a register";
7280           goto report_error;
7281         }
7282
7283       if (GET_MODE (base) != Pmode)
7284         {
7285           reason = "base is not in Pmode";
7286           goto report_error;
7287         }
7288
7289       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
7290           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
7291         {
7292           reason = "base is not valid";
7293           goto report_error;
7294         }
7295     }
7296
7297   /* Validate index register.
7298
7299      Don't allow SUBREG's that span more than a word here -- same as above.  */
7300
7301   if (index)
7302     {
7303       rtx reg;
7304       reason_rtx = index;
7305
7306       if (REG_P (index))
7307         reg = index;
7308       else if (GET_CODE (index) == SUBREG
7309                && REG_P (SUBREG_REG (index))
7310                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
7311                   <= UNITS_PER_WORD)
7312         reg = SUBREG_REG (index);
7313       else
7314         {
7315           reason = "index is not a register";
7316           goto report_error;
7317         }
7318
7319       if (GET_MODE (index) != Pmode)
7320         {
7321           reason = "index is not in Pmode";
7322           goto report_error;
7323         }
7324
7325       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
7326           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
7327         {
7328           reason = "index is not valid";
7329           goto report_error;
7330         }
7331     }
7332
7333   /* Validate scale factor.  */
7334   if (scale != 1)
7335     {
7336       reason_rtx = GEN_INT (scale);
7337       if (!index)
7338         {
7339           reason = "scale without index";
7340           goto report_error;
7341         }
7342
7343       if (scale != 2 && scale != 4 && scale != 8)
7344         {
7345           reason = "scale is not a valid multiplier";
7346           goto report_error;
7347         }
7348     }
7349
7350   /* Validate displacement.  */
7351   if (disp)
7352     {
7353       reason_rtx = disp;
7354
7355       if (GET_CODE (disp) == CONST
7356           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
7357         switch (XINT (XEXP (disp, 0), 1))
7358           {
7359           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
7360              used.  While ABI specify also 32bit relocations, we don't produce
7361              them at all and use IP relative instead.  */
7362           case UNSPEC_GOT:
7363           case UNSPEC_GOTOFF:
7364             gcc_assert (flag_pic);
7365             if (!TARGET_64BIT)
7366               goto is_legitimate_pic;
7367             reason = "64bit address unspec";
7368             goto report_error;
7369
7370           case UNSPEC_GOTPCREL:
7371             gcc_assert (flag_pic);
7372             goto is_legitimate_pic;
7373
7374           case UNSPEC_GOTTPOFF:
7375           case UNSPEC_GOTNTPOFF:
7376           case UNSPEC_INDNTPOFF:
7377           case UNSPEC_NTPOFF:
7378           case UNSPEC_DTPOFF:
7379             break;
7380
7381           default:
7382             reason = "invalid address unspec";
7383             goto report_error;
7384           }
7385
7386       else if (SYMBOLIC_CONST (disp)
7387                && (flag_pic
7388                    || (TARGET_MACHO
7389 #if TARGET_MACHO
7390                        && MACHOPIC_INDIRECT
7391                        && !machopic_operand_p (disp)
7392 #endif
7393                )))
7394         {
7395
7396         is_legitimate_pic:
7397           if (TARGET_64BIT && (index || base))
7398             {
7399               /* foo@dtpoff(%rX) is ok.  */
7400               if (GET_CODE (disp) != CONST
7401                   || GET_CODE (XEXP (disp, 0)) != PLUS
7402                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
7403                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
7404                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
7405                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
7406                 {
7407                   reason = "non-constant pic memory reference";
7408                   goto report_error;
7409                 }
7410             }
7411           else if (! legitimate_pic_address_disp_p (disp))
7412             {
7413               reason = "displacement is an invalid pic construct";
7414               goto report_error;
7415             }
7416
7417           /* This code used to verify that a symbolic pic displacement
7418              includes the pic_offset_table_rtx register.
7419
7420              While this is good idea, unfortunately these constructs may
7421              be created by "adds using lea" optimization for incorrect
7422              code like:
7423
7424              int a;
7425              int foo(int i)
7426                {
7427                  return *(&a+i);
7428                }
7429
7430              This code is nonsensical, but results in addressing
7431              GOT table with pic_offset_table_rtx base.  We can't
7432              just refuse it easily, since it gets matched by
7433              "addsi3" pattern, that later gets split to lea in the
7434              case output register differs from input.  While this
7435              can be handled by separate addsi pattern for this case
7436              that never results in lea, this seems to be easier and
7437              correct fix for crash to disable this test.  */
7438         }
7439       else if (GET_CODE (disp) != LABEL_REF
7440                && !CONST_INT_P (disp)
7441                && (GET_CODE (disp) != CONST
7442                    || !legitimate_constant_p (disp))
7443                && (GET_CODE (disp) != SYMBOL_REF
7444                    || !legitimate_constant_p (disp)))
7445         {
7446           reason = "displacement is not constant";
7447           goto report_error;
7448         }
7449       else if (TARGET_64BIT
7450                && !x86_64_immediate_operand (disp, VOIDmode))
7451         {
7452           reason = "displacement is out of range";
7453           goto report_error;
7454         }
7455     }
7456
7457   /* Everything looks valid.  */
7458   return TRUE;
7459
7460  report_error:
7461   return FALSE;
7462 }
7463 \f
7464 /* Return a unique alias set for the GOT.  */
7465
7466 static alias_set_type
7467 ix86_GOT_alias_set (void)
7468 {
7469   static alias_set_type set = -1;
7470   if (set == -1)
7471     set = new_alias_set ();
7472   return set;
7473 }
7474
7475 /* Return a legitimate reference for ORIG (an address) using the
7476    register REG.  If REG is 0, a new pseudo is generated.
7477
7478    There are two types of references that must be handled:
7479
7480    1. Global data references must load the address from the GOT, via
7481       the PIC reg.  An insn is emitted to do this load, and the reg is
7482       returned.
7483
7484    2. Static data references, constant pool addresses, and code labels
7485       compute the address as an offset from the GOT, whose base is in
7486       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
7487       differentiate them from global data objects.  The returned
7488       address is the PIC reg + an unspec constant.
7489
7490    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
7491    reg also appears in the address.  */
7492
7493 static rtx
7494 legitimize_pic_address (rtx orig, rtx reg)
7495 {
7496   rtx addr = orig;
7497   rtx new_rtx = orig;
7498   rtx base;
7499
7500 #if TARGET_MACHO
7501   if (TARGET_MACHO && !TARGET_64BIT)
7502     {
7503       if (reg == 0)
7504         reg = gen_reg_rtx (Pmode);
7505       /* Use the generic Mach-O PIC machinery.  */
7506       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
7507     }
7508 #endif
7509
7510   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
7511     new_rtx = addr;
7512   else if (TARGET_64BIT
7513            && ix86_cmodel != CM_SMALL_PIC
7514            && gotoff_operand (addr, Pmode))
7515     {
7516       rtx tmpreg;
7517       /* This symbol may be referenced via a displacement from the PIC
7518          base address (@GOTOFF).  */
7519
7520       if (reload_in_progress)
7521         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7522       if (GET_CODE (addr) == CONST)
7523         addr = XEXP (addr, 0);
7524       if (GET_CODE (addr) == PLUS)
7525           {
7526             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7527                                       UNSPEC_GOTOFF);
7528             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
7529           }
7530         else
7531           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7532       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
7533       if (!reg)
7534         tmpreg = gen_reg_rtx (Pmode);
7535       else
7536         tmpreg = reg;
7537       emit_move_insn (tmpreg, new_rtx);
7538
7539       if (reg != 0)
7540         {
7541           new_rtx = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
7542                                          tmpreg, 1, OPTAB_DIRECT);
7543           new_rtx = reg;
7544         }
7545       else new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
7546     }
7547   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
7548     {
7549       /* This symbol may be referenced via a displacement from the PIC
7550          base address (@GOTOFF).  */
7551
7552       if (reload_in_progress)
7553         df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7554       if (GET_CODE (addr) == CONST)
7555         addr = XEXP (addr, 0);
7556       if (GET_CODE (addr) == PLUS)
7557           {
7558             new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7559                                       UNSPEC_GOTOFF);
7560             new_rtx = gen_rtx_PLUS (Pmode, new_rtx, XEXP (addr, 1));
7561           }
7562         else
7563           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7564       new_rtx = gen_rtx_CONST (Pmode, new_rtx);
7565       new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
7566
7567       if (reg != 0)
7568         {
7569           emit_move_insn (reg, new_rtx);
7570           new_rtx = reg;
7571         }
7572     }
7573   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
7574            /* We can't use @GOTOFF for text labels on VxWorks;
7575               see gotoff_operand.  */
7576            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
7577     {
7578       /* Given that we've already handled dllimport variables separately
7579          in legitimize_address, and all other variables should satisfy
7580          legitimate_pic_address_disp_p, we should never arrive here.  */
7581       gcc_assert (!TARGET_64BIT_MS_ABI);
7582
7583       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
7584         {
7585           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
7586           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
7587           new_rtx = gen_const_mem (Pmode, new_rtx);
7588           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
7589
7590           if (reg == 0)
7591             reg = gen_reg_rtx (Pmode);
7592           /* Use directly gen_movsi, otherwise the address is loaded
7593              into register for CSE.  We don't want to CSE this addresses,
7594              instead we CSE addresses from the GOT table, so skip this.  */
7595           emit_insn (gen_movsi (reg, new_rtx));
7596           new_rtx = reg;
7597         }
7598       else
7599         {
7600           /* This symbol must be referenced via a load from the
7601              Global Offset Table (@GOT).  */
7602
7603           if (reload_in_progress)
7604             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7605           new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
7606           new_rtx = gen_rtx_CONST (Pmode, new_rtx);
7607           if (TARGET_64BIT)
7608             new_rtx = force_reg (Pmode, new_rtx);
7609           new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
7610           new_rtx = gen_const_mem (Pmode, new_rtx);
7611           set_mem_alias_set (new_rtx, ix86_GOT_alias_set ());
7612
7613           if (reg == 0)
7614             reg = gen_reg_rtx (Pmode);
7615           emit_move_insn (reg, new_rtx);
7616           new_rtx = reg;
7617         }
7618     }
7619   else
7620     {
7621       if (CONST_INT_P (addr)
7622           && !x86_64_immediate_operand (addr, VOIDmode))
7623         {
7624           if (reg)
7625             {
7626               emit_move_insn (reg, addr);
7627               new_rtx = reg;
7628             }
7629           else
7630             new_rtx = force_reg (Pmode, addr);
7631         }
7632       else if (GET_CODE (addr) == CONST)
7633         {
7634           addr = XEXP (addr, 0);
7635
7636           /* We must match stuff we generate before.  Assume the only
7637              unspecs that can get here are ours.  Not that we could do
7638              anything with them anyway....  */
7639           if (GET_CODE (addr) == UNSPEC
7640               || (GET_CODE (addr) == PLUS
7641                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
7642             return orig;
7643           gcc_assert (GET_CODE (addr) == PLUS);
7644         }
7645       if (GET_CODE (addr) == PLUS)
7646         {
7647           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
7648
7649           /* Check first to see if this is a constant offset from a @GOTOFF
7650              symbol reference.  */
7651           if (gotoff_operand (op0, Pmode)
7652               && CONST_INT_P (op1))
7653             {
7654               if (!TARGET_64BIT)
7655                 {
7656                   if (reload_in_progress)
7657                     df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7658                   new_rtx = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
7659                                             UNSPEC_GOTOFF);
7660                   new_rtx = gen_rtx_PLUS (Pmode, new_rtx, op1);
7661                   new_rtx = gen_rtx_CONST (Pmode, new_rtx);
7662                   new_rtx = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new_rtx);
7663
7664                   if (reg != 0)
7665                     {
7666                       emit_move_insn (reg, new_rtx);
7667                       new_rtx = reg;
7668                     }
7669                 }
7670               else
7671                 {
7672                   if (INTVAL (op1) < -16*1024*1024
7673                       || INTVAL (op1) >= 16*1024*1024)
7674                     {
7675                       if (!x86_64_immediate_operand (op1, Pmode))
7676                         op1 = force_reg (Pmode, op1);
7677                       new_rtx = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
7678                     }
7679                 }
7680             }
7681           else
7682             {
7683               base = legitimize_pic_address (XEXP (addr, 0), reg);
7684               new_rtx  = legitimize_pic_address (XEXP (addr, 1),
7685                                                  base == reg ? NULL_RTX : reg);
7686
7687               if (CONST_INT_P (new_rtx))
7688                 new_rtx = plus_constant (base, INTVAL (new_rtx));
7689               else
7690                 {
7691                   if (GET_CODE (new_rtx) == PLUS && CONSTANT_P (XEXP (new_rtx, 1)))
7692                     {
7693                       base = gen_rtx_PLUS (Pmode, base, XEXP (new_rtx, 0));
7694                       new_rtx = XEXP (new_rtx, 1);
7695                     }
7696                   new_rtx = gen_rtx_PLUS (Pmode, base, new_rtx);
7697                 }
7698             }
7699         }
7700     }
7701   return new_rtx;
7702 }
7703 \f
7704 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
7705
7706 static rtx
7707 get_thread_pointer (int to_reg)
7708 {
7709   rtx tp, reg, insn;
7710
7711   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
7712   if (!to_reg)
7713     return tp;
7714
7715   reg = gen_reg_rtx (Pmode);
7716   insn = gen_rtx_SET (VOIDmode, reg, tp);
7717   insn = emit_insn (insn);
7718
7719   return reg;
7720 }
7721
7722 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
7723    false if we expect this to be used for a memory address and true if
7724    we expect to load the address into a register.  */
7725
7726 static rtx
7727 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
7728 {
7729   rtx dest, base, off, pic, tp;
7730   int type;
7731
7732   switch (model)
7733     {
7734     case TLS_MODEL_GLOBAL_DYNAMIC:
7735       dest = gen_reg_rtx (Pmode);
7736       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7737
7738       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7739         {
7740           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns;
7741
7742           start_sequence ();
7743           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
7744           insns = get_insns ();
7745           end_sequence ();
7746
7747           CONST_OR_PURE_CALL_P (insns) = 1;
7748           emit_libcall_block (insns, dest, rax, x);
7749         }
7750       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7751         emit_insn (gen_tls_global_dynamic_64 (dest, x));
7752       else
7753         emit_insn (gen_tls_global_dynamic_32 (dest, x));
7754
7755       if (TARGET_GNU2_TLS)
7756         {
7757           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
7758
7759           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7760         }
7761       break;
7762
7763     case TLS_MODEL_LOCAL_DYNAMIC:
7764       base = gen_reg_rtx (Pmode);
7765       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7766
7767       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7768         {
7769           rtx rax = gen_rtx_REG (Pmode, AX_REG), insns, note;
7770
7771           start_sequence ();
7772           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
7773           insns = get_insns ();
7774           end_sequence ();
7775
7776           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
7777           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
7778           CONST_OR_PURE_CALL_P (insns) = 1;
7779           emit_libcall_block (insns, base, rax, note);
7780         }
7781       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7782         emit_insn (gen_tls_local_dynamic_base_64 (base));
7783       else
7784         emit_insn (gen_tls_local_dynamic_base_32 (base));
7785
7786       if (TARGET_GNU2_TLS)
7787         {
7788           rtx x = ix86_tls_module_base ();
7789
7790           set_unique_reg_note (get_last_insn (), REG_EQUIV,
7791                                gen_rtx_MINUS (Pmode, x, tp));
7792         }
7793
7794       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
7795       off = gen_rtx_CONST (Pmode, off);
7796
7797       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
7798
7799       if (TARGET_GNU2_TLS)
7800         {
7801           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
7802
7803           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7804         }
7805
7806       break;
7807
7808     case TLS_MODEL_INITIAL_EXEC:
7809       if (TARGET_64BIT)
7810         {
7811           pic = NULL;
7812           type = UNSPEC_GOTNTPOFF;
7813         }
7814       else if (flag_pic)
7815         {
7816           if (reload_in_progress)
7817             df_set_regs_ever_live (PIC_OFFSET_TABLE_REGNUM, true);
7818           pic = pic_offset_table_rtx;
7819           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
7820         }
7821       else if (!TARGET_ANY_GNU_TLS)
7822         {
7823           pic = gen_reg_rtx (Pmode);
7824           emit_insn (gen_set_got (pic));
7825           type = UNSPEC_GOTTPOFF;
7826         }
7827       else
7828         {
7829           pic = NULL;
7830           type = UNSPEC_INDNTPOFF;
7831         }
7832
7833       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
7834       off = gen_rtx_CONST (Pmode, off);
7835       if (pic)
7836         off = gen_rtx_PLUS (Pmode, pic, off);
7837       off = gen_const_mem (Pmode, off);
7838       set_mem_alias_set (off, ix86_GOT_alias_set ());
7839
7840       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7841         {
7842           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7843           off = force_reg (Pmode, off);
7844           return gen_rtx_PLUS (Pmode, base, off);
7845         }
7846       else
7847         {
7848           base = get_thread_pointer (true);
7849           dest = gen_reg_rtx (Pmode);
7850           emit_insn (gen_subsi3 (dest, base, off));
7851         }
7852       break;
7853
7854     case TLS_MODEL_LOCAL_EXEC:
7855       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
7856                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7857                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
7858       off = gen_rtx_CONST (Pmode, off);
7859
7860       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7861         {
7862           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7863           return gen_rtx_PLUS (Pmode, base, off);
7864         }
7865       else
7866         {
7867           base = get_thread_pointer (true);
7868           dest = gen_reg_rtx (Pmode);
7869           emit_insn (gen_subsi3 (dest, base, off));
7870         }
7871       break;
7872
7873     default:
7874       gcc_unreachable ();
7875     }
7876
7877   return dest;
7878 }
7879
7880 /* Create or return the unique __imp_DECL dllimport symbol corresponding
7881    to symbol DECL.  */
7882
7883 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
7884   htab_t dllimport_map;
7885
7886 static tree
7887 get_dllimport_decl (tree decl)
7888 {
7889   struct tree_map *h, in;
7890   void **loc;
7891   const char *name;
7892   const char *prefix;
7893   size_t namelen, prefixlen;
7894   char *imp_name;
7895   tree to;
7896   rtx rtl;
7897
7898   if (!dllimport_map)
7899     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
7900
7901   in.hash = htab_hash_pointer (decl);
7902   in.base.from = decl;
7903   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
7904   h = (struct tree_map *) *loc;
7905   if (h)
7906     return h->to;
7907
7908   *loc = h = GGC_NEW (struct tree_map);
7909   h->hash = in.hash;
7910   h->base.from = decl;
7911   h->to = to = build_decl (VAR_DECL, NULL, ptr_type_node);
7912   DECL_ARTIFICIAL (to) = 1;
7913   DECL_IGNORED_P (to) = 1;
7914   DECL_EXTERNAL (to) = 1;
7915   TREE_READONLY (to) = 1;
7916
7917   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
7918   name = targetm.strip_name_encoding (name);
7919   prefix = name[0] == FASTCALL_PREFIX  ?  "*__imp_": "*__imp__";
7920   namelen = strlen (name);
7921   prefixlen = strlen (prefix);
7922   imp_name = (char *) alloca (namelen + prefixlen + 1);
7923   memcpy (imp_name, prefix, prefixlen);
7924   memcpy (imp_name + prefixlen, name, namelen + 1);
7925
7926   name = ggc_alloc_string (imp_name, namelen + prefixlen);
7927   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
7928   SET_SYMBOL_REF_DECL (rtl, to);
7929   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
7930
7931   rtl = gen_const_mem (Pmode, rtl);
7932   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
7933
7934   SET_DECL_RTL (to, rtl);
7935   SET_DECL_ASSEMBLER_NAME (to, get_identifier (name));
7936
7937   return to;
7938 }
7939
7940 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
7941    true if we require the result be a register.  */
7942
7943 static rtx
7944 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
7945 {
7946   tree imp_decl;
7947   rtx x;
7948
7949   gcc_assert (SYMBOL_REF_DECL (symbol));
7950   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
7951
7952   x = DECL_RTL (imp_decl);
7953   if (want_reg)
7954     x = force_reg (Pmode, x);
7955   return x;
7956 }
7957
7958 /* Try machine-dependent ways of modifying an illegitimate address
7959    to be legitimate.  If we find one, return the new, valid address.
7960    This macro is used in only one place: `memory_address' in explow.c.
7961
7962    OLDX is the address as it was before break_out_memory_refs was called.
7963    In some cases it is useful to look at this to decide what needs to be done.
7964
7965    MODE and WIN are passed so that this macro can use
7966    GO_IF_LEGITIMATE_ADDRESS.
7967
7968    It is always safe for this macro to do nothing.  It exists to recognize
7969    opportunities to optimize the output.
7970
7971    For the 80386, we handle X+REG by loading X into a register R and
7972    using R+REG.  R will go in a general reg and indexing will be used.
7973    However, if REG is a broken-out memory address or multiplication,
7974    nothing needs to be done because REG can certainly go in a general reg.
7975
7976    When -fpic is used, special handling is needed for symbolic references.
7977    See comments by legitimize_pic_address in i386.c for details.  */
7978
7979 rtx
7980 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
7981 {
7982   int changed = 0;
7983   unsigned log;
7984
7985   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
7986   if (log)
7987     return legitimize_tls_address (x, (enum tls_model) log, false);
7988   if (GET_CODE (x) == CONST
7989       && GET_CODE (XEXP (x, 0)) == PLUS
7990       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7991       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
7992     {
7993       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0),
7994                                       (enum tls_model) log, false);
7995       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7996     }
7997
7998   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
7999     {
8000       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
8001         return legitimize_dllimport_symbol (x, true);
8002       if (GET_CODE (x) == CONST
8003           && GET_CODE (XEXP (x, 0)) == PLUS
8004           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
8005           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
8006         {
8007           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
8008           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
8009         }
8010     }
8011
8012   if (flag_pic && SYMBOLIC_CONST (x))
8013     return legitimize_pic_address (x, 0);
8014
8015   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
8016   if (GET_CODE (x) == ASHIFT
8017       && CONST_INT_P (XEXP (x, 1))
8018       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
8019     {
8020       changed = 1;
8021       log = INTVAL (XEXP (x, 1));
8022       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
8023                         GEN_INT (1 << log));
8024     }
8025
8026   if (GET_CODE (x) == PLUS)
8027     {
8028       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
8029
8030       if (GET_CODE (XEXP (x, 0)) == ASHIFT
8031           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8032           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
8033         {
8034           changed = 1;
8035           log = INTVAL (XEXP (XEXP (x, 0), 1));
8036           XEXP (x, 0) = gen_rtx_MULT (Pmode,
8037                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
8038                                       GEN_INT (1 << log));
8039         }
8040
8041       if (GET_CODE (XEXP (x, 1)) == ASHIFT
8042           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
8043           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
8044         {
8045           changed = 1;
8046           log = INTVAL (XEXP (XEXP (x, 1), 1));
8047           XEXP (x, 1) = gen_rtx_MULT (Pmode,
8048                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
8049                                       GEN_INT (1 << log));
8050         }
8051
8052       /* Put multiply first if it isn't already.  */
8053       if (GET_CODE (XEXP (x, 1)) == MULT)
8054         {
8055           rtx tmp = XEXP (x, 0);
8056           XEXP (x, 0) = XEXP (x, 1);
8057           XEXP (x, 1) = tmp;
8058           changed = 1;
8059         }
8060
8061       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
8062          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
8063          created by virtual register instantiation, register elimination, and
8064          similar optimizations.  */
8065       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
8066         {
8067           changed = 1;
8068           x = gen_rtx_PLUS (Pmode,
8069                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
8070                                           XEXP (XEXP (x, 1), 0)),
8071                             XEXP (XEXP (x, 1), 1));
8072         }
8073
8074       /* Canonicalize
8075          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
8076          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
8077       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
8078                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
8079                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
8080                && CONSTANT_P (XEXP (x, 1)))
8081         {
8082           rtx constant;
8083           rtx other = NULL_RTX;
8084
8085           if (CONST_INT_P (XEXP (x, 1)))
8086             {
8087               constant = XEXP (x, 1);
8088               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
8089             }
8090           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
8091             {
8092               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
8093               other = XEXP (x, 1);
8094             }
8095           else
8096             constant = 0;
8097
8098           if (constant)
8099             {
8100               changed = 1;
8101               x = gen_rtx_PLUS (Pmode,
8102                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
8103                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
8104                                 plus_constant (other, INTVAL (constant)));
8105             }
8106         }
8107
8108       if (changed && legitimate_address_p (mode, x, FALSE))
8109         return x;
8110
8111       if (GET_CODE (XEXP (x, 0)) == MULT)
8112         {
8113           changed = 1;
8114           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
8115         }
8116
8117       if (GET_CODE (XEXP (x, 1)) == MULT)
8118         {
8119           changed = 1;
8120           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
8121         }
8122
8123       if (changed
8124           && REG_P (XEXP (x, 1))
8125           && REG_P (XEXP (x, 0)))
8126         return x;
8127
8128       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
8129         {
8130           changed = 1;
8131           x = legitimize_pic_address (x, 0);
8132         }
8133
8134       if (changed && legitimate_address_p (mode, x, FALSE))
8135         return x;
8136
8137       if (REG_P (XEXP (x, 0)))
8138         {
8139           rtx temp = gen_reg_rtx (Pmode);
8140           rtx val  = force_operand (XEXP (x, 1), temp);
8141           if (val != temp)
8142             emit_move_insn (temp, val);
8143
8144           XEXP (x, 1) = temp;
8145           return x;
8146         }
8147
8148       else if (REG_P (XEXP (x, 1)))
8149         {
8150           rtx temp = gen_reg_rtx (Pmode);
8151           rtx val  = force_operand (XEXP (x, 0), temp);
8152           if (val != temp)
8153             emit_move_insn (temp, val);
8154
8155           XEXP (x, 0) = temp;
8156           return x;
8157         }
8158     }
8159
8160   return x;
8161 }
8162 \f
8163 /* Print an integer constant expression in assembler syntax.  Addition
8164    and subtraction are the only arithmetic that may appear in these
8165    expressions.  FILE is the stdio stream to write to, X is the rtx, and
8166    CODE is the operand print code from the output string.  */
8167
8168 static void
8169 output_pic_addr_const (FILE *file, rtx x, int code)
8170 {
8171   char buf[256];
8172
8173   switch (GET_CODE (x))
8174     {
8175     case PC:
8176       gcc_assert (flag_pic);
8177       putc ('.', file);
8178       break;
8179
8180     case SYMBOL_REF:
8181       if (! TARGET_MACHO || TARGET_64BIT)
8182         output_addr_const (file, x);
8183       else
8184         {
8185           const char *name = XSTR (x, 0);
8186
8187           /* Mark the decl as referenced so that cgraph will
8188              output the function.  */
8189           if (SYMBOL_REF_DECL (x))
8190             mark_decl_referenced (SYMBOL_REF_DECL (x));
8191
8192 #if TARGET_MACHO
8193           if (MACHOPIC_INDIRECT
8194               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
8195             name = machopic_indirection_name (x, /*stub_p=*/true);
8196 #endif
8197           assemble_name (file, name);
8198         }
8199       if (!TARGET_MACHO && !TARGET_64BIT_MS_ABI
8200           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
8201         fputs ("@PLT", file);
8202       break;
8203
8204     case LABEL_REF:
8205       x = XEXP (x, 0);
8206       /* FALLTHRU */
8207     case CODE_LABEL:
8208       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
8209       assemble_name (asm_out_file, buf);
8210       break;
8211
8212     case CONST_INT:
8213       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8214       break;
8215
8216     case CONST:
8217       /* This used to output parentheses around the expression,
8218          but that does not work on the 386 (either ATT or BSD assembler).  */
8219       output_pic_addr_const (file, XEXP (x, 0), code);
8220       break;
8221
8222     case CONST_DOUBLE:
8223       if (GET_MODE (x) == VOIDmode)
8224         {
8225           /* We can use %d if the number is <32 bits and positive.  */
8226           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
8227             fprintf (file, "0x%lx%08lx",
8228                      (unsigned long) CONST_DOUBLE_HIGH (x),
8229                      (unsigned long) CONST_DOUBLE_LOW (x));
8230           else
8231             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
8232         }
8233       else
8234         /* We can't handle floating point constants;
8235            PRINT_OPERAND must handle them.  */
8236         output_operand_lossage ("floating constant misused");
8237       break;
8238
8239     case PLUS:
8240       /* Some assemblers need integer constants to appear first.  */
8241       if (CONST_INT_P (XEXP (x, 0)))
8242         {
8243           output_pic_addr_const (file, XEXP (x, 0), code);
8244           putc ('+', file);
8245           output_pic_addr_const (file, XEXP (x, 1), code);
8246         }
8247       else
8248         {
8249           gcc_assert (CONST_INT_P (XEXP (x, 1)));
8250           output_pic_addr_const (file, XEXP (x, 1), code);
8251           putc ('+', file);
8252           output_pic_addr_const (file, XEXP (x, 0), code);
8253         }
8254       break;
8255
8256     case MINUS:
8257       if (!TARGET_MACHO)
8258         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
8259       output_pic_addr_const (file, XEXP (x, 0), code);
8260       putc ('-', file);
8261       output_pic_addr_const (file, XEXP (x, 1), code);
8262       if (!TARGET_MACHO)
8263         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
8264       break;
8265
8266      case UNSPEC:
8267        gcc_assert (XVECLEN (x, 0) == 1);
8268        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
8269        switch (XINT (x, 1))
8270         {
8271         case UNSPEC_GOT:
8272           fputs ("@GOT", file);
8273           break;
8274         case UNSPEC_GOTOFF:
8275           fputs ("@GOTOFF", file);
8276           break;
8277         case UNSPEC_PLTOFF:
8278           fputs ("@PLTOFF", file);
8279           break;
8280         case UNSPEC_GOTPCREL:
8281           fputs (ASSEMBLER_DIALECT == ASM_ATT ?
8282                  "@GOTPCREL(%rip)" : "@GOTPCREL[rip]", file);
8283           break;
8284         case UNSPEC_GOTTPOFF:
8285           /* FIXME: This might be @TPOFF in Sun ld too.  */
8286           fputs ("@GOTTPOFF", file);
8287           break;
8288         case UNSPEC_TPOFF:
8289           fputs ("@TPOFF", file);
8290           break;
8291         case UNSPEC_NTPOFF:
8292           if (TARGET_64BIT)
8293             fputs ("@TPOFF", file);
8294           else
8295             fputs ("@NTPOFF", file);
8296           break;
8297         case UNSPEC_DTPOFF:
8298           fputs ("@DTPOFF", file);
8299           break;
8300         case UNSPEC_GOTNTPOFF:
8301           if (TARGET_64BIT)
8302             fputs (ASSEMBLER_DIALECT == ASM_ATT ?
8303                    "@GOTTPOFF(%rip)": "@GOTTPOFF[rip]", file);
8304           else
8305             fputs ("@GOTNTPOFF", file);
8306           break;
8307         case UNSPEC_INDNTPOFF:
8308           fputs ("@INDNTPOFF", file);
8309           break;
8310         default:
8311           output_operand_lossage ("invalid UNSPEC as operand");
8312           break;
8313         }
8314        break;
8315
8316     default:
8317       output_operand_lossage ("invalid expression as operand");
8318     }
8319 }
8320
8321 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
8322    We need to emit DTP-relative relocations.  */
8323
8324 static void ATTRIBUTE_UNUSED
8325 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
8326 {
8327   fputs (ASM_LONG, file);
8328   output_addr_const (file, x);
8329   fputs ("@DTPOFF", file);
8330   switch (size)
8331     {
8332     case 4:
8333       break;
8334     case 8:
8335       fputs (", 0", file);
8336       break;
8337     default:
8338       gcc_unreachable ();
8339    }
8340 }
8341
8342 /* In the name of slightly smaller debug output, and to cater to
8343    general assembler lossage, recognize PIC+GOTOFF and turn it back
8344    into a direct symbol reference.
8345
8346    On Darwin, this is necessary to avoid a crash, because Darwin
8347    has a different PIC label for each routine but the DWARF debugging
8348    information is not associated with any particular routine, so it's
8349    necessary to remove references to the PIC label from RTL stored by
8350    the DWARF output code.  */
8351
8352 static rtx
8353 ix86_delegitimize_address (rtx orig_x)
8354 {
8355   rtx x = orig_x;
8356   /* reg_addend is NULL or a multiple of some register.  */
8357   rtx reg_addend = NULL_RTX;
8358   /* const_addend is NULL or a const_int.  */
8359   rtx const_addend = NULL_RTX;
8360   /* This is the result, or NULL.  */
8361   rtx result = NULL_RTX;
8362
8363   if (MEM_P (x))
8364     x = XEXP (x, 0);
8365
8366   if (TARGET_64BIT)
8367     {
8368       if (GET_CODE (x) != CONST
8369           || GET_CODE (XEXP (x, 0)) != UNSPEC
8370           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
8371           || !MEM_P (orig_x))
8372         return orig_x;
8373       return XVECEXP (XEXP (x, 0), 0, 0);
8374     }
8375
8376   if (GET_CODE (x) != PLUS
8377       || GET_CODE (XEXP (x, 1)) != CONST)
8378     return orig_x;
8379
8380   if (REG_P (XEXP (x, 0))
8381       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
8382     /* %ebx + GOT/GOTOFF */
8383     ;
8384   else if (GET_CODE (XEXP (x, 0)) == PLUS)
8385     {
8386       /* %ebx + %reg * scale + GOT/GOTOFF */
8387       reg_addend = XEXP (x, 0);
8388       if (REG_P (XEXP (reg_addend, 0))
8389           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
8390         reg_addend = XEXP (reg_addend, 1);
8391       else if (REG_P (XEXP (reg_addend, 1))
8392                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
8393         reg_addend = XEXP (reg_addend, 0);
8394       else
8395         return orig_x;
8396       if (!REG_P (reg_addend)
8397           && GET_CODE (reg_addend) != MULT
8398           && GET_CODE (reg_addend) != ASHIFT)
8399         return orig_x;
8400     }
8401   else
8402     return orig_x;
8403
8404   x = XEXP (XEXP (x, 1), 0);
8405   if (GET_CODE (x) == PLUS
8406       && CONST_INT_P (XEXP (x, 1)))
8407     {
8408       const_addend = XEXP (x, 1);
8409       x = XEXP (x, 0);
8410     }
8411
8412   if (GET_CODE (x) == UNSPEC
8413       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x))
8414           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
8415     result = XVECEXP (x, 0, 0);
8416
8417   if (TARGET_MACHO && darwin_local_data_pic (x)
8418       && !MEM_P (orig_x))
8419     result = XEXP (x, 0);
8420
8421   if (! result)
8422     return orig_x;
8423
8424   if (const_addend)
8425     result = gen_rtx_PLUS (Pmode, result, const_addend);
8426   if (reg_addend)
8427     result = gen_rtx_PLUS (Pmode, reg_addend, result);
8428   return result;
8429 }
8430
8431 /* If X is a machine specific address (i.e. a symbol or label being
8432    referenced as a displacement from the GOT implemented using an
8433    UNSPEC), then return the base term.  Otherwise return X.  */
8434
8435 rtx
8436 ix86_find_base_term (rtx x)
8437 {
8438   rtx term;
8439
8440   if (TARGET_64BIT)
8441     {
8442       if (GET_CODE (x) != CONST)
8443         return x;
8444       term = XEXP (x, 0);
8445       if (GET_CODE (term) == PLUS
8446           && (CONST_INT_P (XEXP (term, 1))
8447               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
8448         term = XEXP (term, 0);
8449       if (GET_CODE (term) != UNSPEC
8450           || XINT (term, 1) != UNSPEC_GOTPCREL)
8451         return x;
8452
8453       term = XVECEXP (term, 0, 0);
8454
8455       if (GET_CODE (term) != SYMBOL_REF
8456           && GET_CODE (term) != LABEL_REF)
8457         return x;
8458
8459       return term;
8460     }
8461
8462   term = ix86_delegitimize_address (x);
8463
8464   if (GET_CODE (term) != SYMBOL_REF
8465       && GET_CODE (term) != LABEL_REF)
8466     return x;
8467
8468   return term;
8469 }
8470 \f
8471 static void
8472 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
8473                     int fp, FILE *file)
8474 {
8475   const char *suffix;
8476
8477   if (mode == CCFPmode || mode == CCFPUmode)
8478     {
8479       enum rtx_code second_code, bypass_code;
8480       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
8481       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
8482       code = ix86_fp_compare_code_to_integer (code);
8483       mode = CCmode;
8484     }
8485   if (reverse)
8486     code = reverse_condition (code);
8487
8488   switch (code)
8489     {
8490     case EQ:
8491       switch (mode)
8492         {
8493         case CCAmode:
8494           suffix = "a";
8495           break;
8496
8497         case CCCmode:
8498           suffix = "c";
8499           break;
8500
8501         case CCOmode:
8502           suffix = "o";
8503           break;
8504
8505         case CCSmode:
8506           suffix = "s";
8507           break;
8508
8509         default:
8510           suffix = "e";
8511         }
8512       break;
8513     case NE:
8514       switch (mode)
8515         {
8516         case CCAmode:
8517           suffix = "na";
8518           break;
8519
8520         case CCCmode:
8521           suffix = "nc";
8522           break;
8523
8524         case CCOmode:
8525           suffix = "no";
8526           break;
8527
8528         case CCSmode:
8529           suffix = "ns";
8530           break;
8531
8532         default:
8533           suffix = "ne";
8534         }
8535       break;
8536     case GT:
8537       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
8538       suffix = "g";
8539       break;
8540     case GTU:
8541       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
8542          Those same assemblers have the same but opposite lossage on cmov.  */
8543       if (mode == CCmode)
8544         suffix = fp ? "nbe" : "a";
8545       else if (mode == CCCmode)
8546         suffix = "b";
8547       else
8548         gcc_unreachable ();
8549       break;
8550     case LT:
8551       switch (mode)
8552         {
8553         case CCNOmode:
8554         case CCGOCmode:
8555           suffix = "s";
8556           break;
8557
8558         case CCmode:
8559         case CCGCmode:
8560           suffix = "l";
8561           break;
8562
8563         default:
8564           gcc_unreachable ();
8565         }
8566       break;
8567     case LTU:
8568       gcc_assert (mode == CCmode || mode == CCCmode);
8569       suffix = "b";
8570       break;
8571     case GE:
8572       switch (mode)
8573         {
8574         case CCNOmode:
8575         case CCGOCmode:
8576           suffix = "ns";
8577           break;
8578
8579         case CCmode:
8580         case CCGCmode:
8581           suffix = "ge";
8582           break;
8583
8584         default:
8585           gcc_unreachable ();
8586         }
8587       break;
8588     case GEU:
8589       /* ??? As above.  */
8590       gcc_assert (mode == CCmode || mode == CCCmode);
8591       suffix = fp ? "nb" : "ae";
8592       break;
8593     case LE:
8594       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
8595       suffix = "le";
8596       break;
8597     case LEU:
8598       /* ??? As above.  */
8599       if (mode == CCmode)
8600         suffix = "be";
8601       else if (mode == CCCmode)
8602         suffix = fp ? "nb" : "ae";
8603       else
8604         gcc_unreachable ();
8605       break;
8606     case UNORDERED:
8607       suffix = fp ? "u" : "p";
8608       break;
8609     case ORDERED:
8610       suffix = fp ? "nu" : "np";
8611       break;
8612     default:
8613       gcc_unreachable ();
8614     }
8615   fputs (suffix, file);
8616 }
8617
8618 /* Print the name of register X to FILE based on its machine mode and number.
8619    If CODE is 'w', pretend the mode is HImode.
8620    If CODE is 'b', pretend the mode is QImode.
8621    If CODE is 'k', pretend the mode is SImode.
8622    If CODE is 'q', pretend the mode is DImode.
8623    If CODE is 'h', pretend the reg is the 'high' byte register.
8624    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
8625
8626 void
8627 print_reg (rtx x, int code, FILE *file)
8628 {
8629   gcc_assert (x == pc_rtx
8630               || (REGNO (x) != ARG_POINTER_REGNUM
8631                   && REGNO (x) != FRAME_POINTER_REGNUM
8632                   && REGNO (x) != FLAGS_REG
8633                   && REGNO (x) != FPSR_REG
8634                   && REGNO (x) != FPCR_REG));
8635
8636   if (ASSEMBLER_DIALECT == ASM_ATT)
8637     putc ('%', file);
8638
8639   if (x == pc_rtx)
8640     {
8641       gcc_assert (TARGET_64BIT);
8642       fputs ("rip", file);
8643       return;
8644     }
8645
8646   if (code == 'w' || MMX_REG_P (x))
8647     code = 2;
8648   else if (code == 'b')
8649     code = 1;
8650   else if (code == 'k')
8651     code = 4;
8652   else if (code == 'q')
8653     code = 8;
8654   else if (code == 'y')
8655     code = 3;
8656   else if (code == 'h')
8657     code = 0;
8658   else
8659     code = GET_MODE_SIZE (GET_MODE (x));
8660
8661   /* Irritatingly, AMD extended registers use different naming convention
8662      from the normal registers.  */
8663   if (REX_INT_REG_P (x))
8664     {
8665       gcc_assert (TARGET_64BIT);
8666       switch (code)
8667         {
8668           case 0:
8669             error ("extended registers have no high halves");
8670             break;
8671           case 1:
8672             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
8673             break;
8674           case 2:
8675             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
8676             break;
8677           case 4:
8678             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
8679             break;
8680           case 8:
8681             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
8682             break;
8683           default:
8684             error ("unsupported operand size for extended register");
8685             break;
8686         }
8687       return;
8688     }
8689   switch (code)
8690     {
8691     case 3:
8692       if (STACK_TOP_P (x))
8693         {
8694           fputs ("st(0)", file);
8695           break;
8696         }
8697       /* FALLTHRU */
8698     case 8:
8699     case 4:
8700     case 12:
8701       if (! ANY_FP_REG_P (x))
8702         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
8703       /* FALLTHRU */
8704     case 16:
8705     case 2:
8706     normal:
8707       fputs (hi_reg_name[REGNO (x)], file);
8708       break;
8709     case 1:
8710       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
8711         goto normal;
8712       fputs (qi_reg_name[REGNO (x)], file);
8713       break;
8714     case 0:
8715       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
8716         goto normal;
8717       fputs (qi_high_reg_name[REGNO (x)], file);
8718       break;
8719     default:
8720       gcc_unreachable ();
8721     }
8722 }
8723
8724 /* Locate some local-dynamic symbol still in use by this function
8725    so that we can print its name in some tls_local_dynamic_base
8726    pattern.  */
8727
8728 static int
8729 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8730 {
8731   rtx x = *px;
8732
8733   if (GET_CODE (x) == SYMBOL_REF
8734       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8735     {
8736       cfun->machine->some_ld_name = XSTR (x, 0);
8737       return 1;
8738     }
8739
8740   return 0;
8741 }
8742
8743 static const char *
8744 get_some_local_dynamic_name (void)
8745 {
8746   rtx insn;
8747
8748   if (cfun->machine->some_ld_name)
8749     return cfun->machine->some_ld_name;
8750
8751   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8752     if (INSN_P (insn)
8753         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
8754       return cfun->machine->some_ld_name;
8755
8756   gcc_unreachable ();
8757 }
8758
8759 /* Meaning of CODE:
8760    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
8761    C -- print opcode suffix for set/cmov insn.
8762    c -- like C, but print reversed condition
8763    F,f -- likewise, but for floating-point.
8764    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
8765         otherwise nothing
8766    R -- print the prefix for register names.
8767    z -- print the opcode suffix for the size of the current operand.
8768    * -- print a star (in certain assembler syntax)
8769    A -- print an absolute memory reference.
8770    w -- print the operand as if it's a "word" (HImode) even if it isn't.
8771    s -- print a shift double count, followed by the assemblers argument
8772         delimiter.
8773    b -- print the QImode name of the register for the indicated operand.
8774         %b0 would print %al if operands[0] is reg 0.
8775    w --  likewise, print the HImode name of the register.
8776    k --  likewise, print the SImode name of the register.
8777    q --  likewise, print the DImode name of the register.
8778    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
8779    y -- print "st(0)" instead of "st" as a register.
8780    D -- print condition for SSE cmp instruction.
8781    P -- if PIC, print an @PLT suffix.
8782    X -- don't print any sort of PIC '@' suffix for a symbol.
8783    & -- print some in-use local-dynamic symbol name.
8784    H -- print a memory address offset by 8; used for sse high-parts
8785    Y -- print condition for SSE5 com* instruction.
8786    + -- print a branch hint as 'cs' or 'ds' prefix
8787    ; -- print a semicolon (after prefixes due to bug in older gas).
8788  */
8789
8790 void
8791 print_operand (FILE *file, rtx x, int code)
8792 {
8793   if (code)
8794     {
8795       switch (code)
8796         {
8797         case '*':
8798           if (ASSEMBLER_DIALECT == ASM_ATT)
8799             putc ('*', file);
8800           return;
8801
8802         case '&':
8803           assemble_name (file, get_some_local_dynamic_name ());
8804           return;
8805
8806         case 'A':
8807           switch (ASSEMBLER_DIALECT)
8808             {
8809             case ASM_ATT:
8810               putc ('*', file);
8811               break;
8812
8813             case ASM_INTEL:
8814               /* Intel syntax. For absolute addresses, registers should not
8815                  be surrounded by braces.  */
8816               if (!REG_P (x))
8817                 {
8818                   putc ('[', file);
8819                   PRINT_OPERAND (file, x, 0);
8820                   putc (']', file);
8821                   return;
8822                 }
8823               break;
8824
8825             default:
8826               gcc_unreachable ();
8827             }
8828
8829           PRINT_OPERAND (file, x, 0);
8830           return;
8831
8832
8833         case 'L':
8834           if (ASSEMBLER_DIALECT == ASM_ATT)
8835             putc ('l', file);
8836           return;
8837
8838         case 'W':
8839           if (ASSEMBLER_DIALECT == ASM_ATT)
8840             putc ('w', file);
8841           return;
8842
8843         case 'B':
8844           if (ASSEMBLER_DIALECT == ASM_ATT)
8845             putc ('b', file);
8846           return;
8847
8848         case 'Q':
8849           if (ASSEMBLER_DIALECT == ASM_ATT)
8850             putc ('l', file);
8851           return;
8852
8853         case 'S':
8854           if (ASSEMBLER_DIALECT == ASM_ATT)
8855             putc ('s', file);
8856           return;
8857
8858         case 'T':
8859           if (ASSEMBLER_DIALECT == ASM_ATT)
8860             putc ('t', file);
8861           return;
8862
8863         case 'z':
8864           /* 387 opcodes don't get size suffixes if the operands are
8865              registers.  */
8866           if (STACK_REG_P (x))
8867             return;
8868
8869           /* Likewise if using Intel opcodes.  */
8870           if (ASSEMBLER_DIALECT == ASM_INTEL)
8871             return;
8872
8873           /* This is the size of op from size of operand.  */
8874           switch (GET_MODE_SIZE (GET_MODE (x)))
8875             {
8876             case 1:
8877               putc ('b', file);
8878               return;
8879
8880             case 2:
8881               if (MEM_P (x))
8882                 {
8883 #ifdef HAVE_GAS_FILDS_FISTS
8884                   putc ('s', file);
8885 #endif
8886                   return;
8887                 }
8888               else
8889                 putc ('w', file);
8890               return;
8891
8892             case 4:
8893               if (GET_MODE (x) == SFmode)
8894                 {
8895                   putc ('s', file);
8896                   return;
8897                 }
8898               else
8899                 putc ('l', file);
8900               return;
8901
8902             case 12:
8903             case 16:
8904               putc ('t', file);
8905               return;
8906
8907             case 8:
8908               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
8909                 {
8910 #ifdef GAS_MNEMONICS
8911                   putc ('q', file);
8912 #else
8913                   putc ('l', file);
8914                   putc ('l', file);
8915 #endif
8916                 }
8917               else
8918                 putc ('l', file);
8919               return;
8920
8921             default:
8922               gcc_unreachable ();
8923             }
8924
8925         case 'b':
8926         case 'w':
8927         case 'k':
8928         case 'q':
8929         case 'h':
8930         case 'y':
8931         case 'X':
8932         case 'P':
8933           break;
8934
8935         case 's':
8936           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
8937             {
8938               PRINT_OPERAND (file, x, 0);
8939               putc (',', file);
8940             }
8941           return;
8942
8943         case 'D':
8944           /* Little bit of braindamage here.  The SSE compare instructions
8945              does use completely different names for the comparisons that the
8946              fp conditional moves.  */
8947           switch (GET_CODE (x))
8948             {
8949             case EQ:
8950             case UNEQ:
8951               fputs ("eq", file);
8952               break;
8953             case LT:
8954             case UNLT:
8955               fputs ("lt", file);
8956               break;
8957             case LE:
8958             case UNLE:
8959               fputs ("le", file);
8960               break;
8961             case UNORDERED:
8962               fputs ("unord", file);
8963               break;
8964             case NE:
8965             case LTGT:
8966               fputs ("neq", file);
8967               break;
8968             case UNGE:
8969             case GE:
8970               fputs ("nlt", file);
8971               break;
8972             case UNGT:
8973             case GT:
8974               fputs ("nle", file);
8975               break;
8976             case ORDERED:
8977               fputs ("ord", file);
8978               break;
8979             default:
8980               gcc_unreachable ();
8981             }
8982           return;
8983         case 'O':
8984 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8985           if (ASSEMBLER_DIALECT == ASM_ATT)
8986             {
8987               switch (GET_MODE (x))
8988                 {
8989                 case HImode: putc ('w', file); break;
8990                 case SImode:
8991                 case SFmode: putc ('l', file); break;
8992                 case DImode:
8993                 case DFmode: putc ('q', file); break;
8994                 default: gcc_unreachable ();
8995                 }
8996               putc ('.', file);
8997             }
8998 #endif
8999           return;
9000         case 'C':
9001           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
9002           return;
9003         case 'F':
9004 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
9005           if (ASSEMBLER_DIALECT == ASM_ATT)
9006             putc ('.', file);
9007 #endif
9008           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
9009           return;
9010
9011           /* Like above, but reverse condition */
9012         case 'c':
9013           /* Check to see if argument to %c is really a constant
9014              and not a condition code which needs to be reversed.  */
9015           if (!COMPARISON_P (x))
9016           {
9017             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
9018              return;
9019           }
9020           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
9021           return;
9022         case 'f':
9023 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
9024           if (ASSEMBLER_DIALECT == ASM_ATT)
9025             putc ('.', file);
9026 #endif
9027           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
9028           return;
9029
9030         case 'H':
9031           /* It doesn't actually matter what mode we use here, as we're
9032              only going to use this for printing.  */
9033           x = adjust_address_nv (x, DImode, 8);
9034           break;
9035
9036         case '+':
9037           {
9038             rtx x;
9039
9040             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
9041               return;
9042
9043             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
9044             if (x)
9045               {
9046                 int pred_val = INTVAL (XEXP (x, 0));
9047
9048                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
9049                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
9050                   {
9051                     int taken = pred_val > REG_BR_PROB_BASE / 2;
9052                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
9053
9054                     /* Emit hints only in the case default branch prediction
9055                        heuristics would fail.  */
9056                     if (taken != cputaken)
9057                       {
9058                         /* We use 3e (DS) prefix for taken branches and
9059                            2e (CS) prefix for not taken branches.  */
9060                         if (taken)
9061                           fputs ("ds ; ", file);
9062                         else
9063                           fputs ("cs ; ", file);
9064                       }
9065                   }
9066               }
9067             return;
9068           }
9069
9070         case 'Y':
9071           switch (GET_CODE (x))
9072             {
9073             case NE:
9074               fputs ("neq", file);
9075               break;
9076             case EQ:
9077               fputs ("eq", file);
9078               break;
9079             case GE:
9080             case GEU:
9081               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "ge" : "unlt", file);
9082               break;
9083             case GT:
9084             case GTU:
9085               fputs (INTEGRAL_MODE_P (GET_MODE (x)) ? "gt" : "unle", file);
9086               break;
9087             case LE:
9088             case LEU:
9089               fputs ("le", file);
9090               break;
9091             case LT:
9092             case LTU:
9093               fputs ("lt", file);
9094               break;
9095             case UNORDERED:
9096               fputs ("unord", file);
9097               break;
9098             case ORDERED:
9099               fputs ("ord", file);
9100               break;
9101             case UNEQ:
9102               fputs ("ueq", file);
9103               break;
9104             case UNGE:
9105               fputs ("nlt", file);
9106               break;
9107             case UNGT:
9108               fputs ("nle", file);
9109               break;
9110             case UNLE:
9111               fputs ("ule", file);
9112               break;
9113             case UNLT:
9114               fputs ("ult", file);
9115               break;
9116             case LTGT:
9117               fputs ("une", file);
9118               break;
9119             default:
9120               gcc_unreachable ();
9121             }
9122           return;
9123
9124         case ';':
9125 #if TARGET_MACHO
9126           fputs (" ; ", file);
9127 #else
9128           fputc (' ', file);
9129 #endif
9130           return;
9131
9132         default:
9133             output_operand_lossage ("invalid operand code '%c'", code);
9134         }
9135     }
9136
9137   if (REG_P (x))
9138     print_reg (x, code, file);
9139
9140   else if (MEM_P (x))
9141     {
9142       /* No `byte ptr' prefix for call instructions or BLKmode operands.  */
9143       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P'
9144           && GET_MODE (x) != BLKmode)
9145         {
9146           const char * size;
9147           switch (GET_MODE_SIZE (GET_MODE (x)))
9148             {
9149             case 1: size = "BYTE"; break;
9150             case 2: size = "WORD"; break;
9151             case 4: size = "DWORD"; break;
9152             case 8: size = "QWORD"; break;
9153             case 12: size = "XWORD"; break;
9154             case 16:
9155               if (GET_MODE (x) == XFmode)
9156                 size = "XWORD";
9157               else
9158                 size = "XMMWORD";
9159               break;
9160             default:
9161               gcc_unreachable ();
9162             }
9163
9164           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
9165           if (code == 'b')
9166             size = "BYTE";
9167           else if (code == 'w')
9168             size = "WORD";
9169           else if (code == 'k')
9170             size = "DWORD";
9171
9172           fputs (size, file);
9173           fputs (" PTR ", file);
9174         }
9175
9176       x = XEXP (x, 0);
9177       /* Avoid (%rip) for call operands.  */
9178       if (CONSTANT_ADDRESS_P (x) && code == 'P'
9179           && !CONST_INT_P (x))
9180         output_addr_const (file, x);
9181       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
9182         output_operand_lossage ("invalid constraints for operand");
9183       else
9184         output_address (x);
9185     }
9186
9187   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
9188     {
9189       REAL_VALUE_TYPE r;
9190       long l;
9191
9192       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
9193       REAL_VALUE_TO_TARGET_SINGLE (r, l);
9194
9195       if (ASSEMBLER_DIALECT == ASM_ATT)
9196         putc ('$', file);
9197       fprintf (file, "0x%08lx", l);
9198     }
9199
9200   /* These float cases don't actually occur as immediate operands.  */
9201   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
9202     {
9203       char dstr[30];
9204
9205       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
9206       fprintf (file, "%s", dstr);
9207     }
9208
9209   else if (GET_CODE (x) == CONST_DOUBLE
9210            && GET_MODE (x) == XFmode)
9211     {
9212       char dstr[30];
9213
9214       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
9215       fprintf (file, "%s", dstr);
9216     }
9217
9218   else
9219     {
9220       /* We have patterns that allow zero sets of memory, for instance.
9221          In 64-bit mode, we should probably support all 8-byte vectors,
9222          since we can in fact encode that into an immediate.  */
9223       if (GET_CODE (x) == CONST_VECTOR)
9224         {
9225           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
9226           x = const0_rtx;
9227         }
9228
9229       if (code != 'P')
9230         {
9231           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
9232             {
9233               if (ASSEMBLER_DIALECT == ASM_ATT)
9234                 putc ('$', file);
9235             }
9236           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
9237                    || GET_CODE (x) == LABEL_REF)
9238             {
9239               if (ASSEMBLER_DIALECT == ASM_ATT)
9240                 putc ('$', file);
9241               else
9242                 fputs ("OFFSET FLAT:", file);
9243             }
9244         }
9245       if (CONST_INT_P (x))
9246         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
9247       else if (flag_pic)
9248         output_pic_addr_const (file, x, code);
9249       else
9250         output_addr_const (file, x);
9251     }
9252 }
9253 \f
9254 /* Print a memory operand whose address is ADDR.  */
9255
9256 void
9257 print_operand_address (FILE *file, rtx addr)
9258 {
9259   struct ix86_address parts;
9260   rtx base, index, disp;
9261   int scale;
9262   int ok = ix86_decompose_address (addr, &parts);
9263
9264   gcc_assert (ok);
9265
9266   base = parts.base;
9267   index = parts.index;
9268   disp = parts.disp;
9269   scale = parts.scale;
9270
9271   switch (parts.seg)
9272     {
9273     case SEG_DEFAULT:
9274       break;
9275     case SEG_FS:
9276     case SEG_GS:
9277       if (ASSEMBLER_DIALECT == ASM_ATT)
9278         putc ('%', file);
9279       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
9280       break;
9281     default:
9282       gcc_unreachable ();
9283     }
9284
9285   /* Use one byte shorter RIP relative addressing for 64bit mode.  */
9286   if (TARGET_64BIT && !base && !index)
9287     {
9288       rtx symbol = disp;
9289
9290       if (GET_CODE (disp) == CONST
9291           && GET_CODE (XEXP (disp, 0)) == PLUS
9292           && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
9293         symbol = XEXP (XEXP (disp, 0), 0);
9294
9295       if (GET_CODE (symbol) == LABEL_REF
9296           || (GET_CODE (symbol) == SYMBOL_REF
9297               && SYMBOL_REF_TLS_MODEL (symbol) == 0))
9298         base = pc_rtx;
9299     }
9300   if (!base && !index)
9301     {
9302       /* Displacement only requires special attention.  */
9303
9304       if (CONST_INT_P (disp))
9305         {
9306           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
9307             fputs ("ds:", file);
9308           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
9309         }
9310       else if (flag_pic)
9311         output_pic_addr_const (file, disp, 0);
9312       else
9313         output_addr_const (file, disp);
9314     }
9315   else
9316     {
9317       if (ASSEMBLER_DIALECT == ASM_ATT)
9318         {
9319           if (disp)
9320             {
9321               if (flag_pic)
9322                 output_pic_addr_const (file, disp, 0);
9323               else if (GET_CODE (disp) == LABEL_REF)
9324                 output_asm_label (disp);
9325               else
9326                 output_addr_const (file, disp);
9327             }
9328
9329           putc ('(', file);
9330           if (base)
9331             print_reg (base, 0, file);
9332           if (index)
9333             {
9334               putc (',', file);
9335               print_reg (index, 0, file);
9336               if (scale != 1)
9337                 fprintf (file, ",%d", scale);
9338             }
9339           putc (')', file);
9340         }
9341       else
9342         {
9343           rtx offset = NULL_RTX;
9344
9345           if (disp)
9346             {
9347               /* Pull out the offset of a symbol; print any symbol itself.  */
9348               if (GET_CODE (disp) == CONST
9349                   && GET_CODE (XEXP (disp, 0)) == PLUS
9350                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
9351                 {
9352                   offset = XEXP (XEXP (disp, 0), 1);
9353                   disp = gen_rtx_CONST (VOIDmode,
9354                                         XEXP (XEXP (disp, 0), 0));
9355                 }
9356
9357               if (flag_pic)
9358                 output_pic_addr_const (file, disp, 0);
9359               else if (GET_CODE (disp) == LABEL_REF)
9360                 output_asm_label (disp);
9361               else if (CONST_INT_P (disp))
9362                 offset = disp;
9363               else
9364                 output_addr_const (file, disp);
9365             }
9366
9367           putc ('[', file);
9368           if (base)
9369             {
9370               print_reg (base, 0, file);
9371               if (offset)
9372                 {
9373                   if (INTVAL (offset) >= 0)
9374                     putc ('+', file);
9375                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
9376                 }
9377             }
9378           else if (offset)
9379             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
9380           else
9381             putc ('0', file);
9382
9383           if (index)
9384             {
9385               putc ('+', file);
9386               print_reg (index, 0, file);
9387               if (scale != 1)
9388                 fprintf (file, "*%d", scale);
9389             }
9390           putc (']', file);
9391         }
9392     }
9393 }
9394
9395 bool
9396 output_addr_const_extra (FILE *file, rtx x)
9397 {
9398   rtx op;
9399
9400   if (GET_CODE (x) != UNSPEC)
9401     return false;
9402
9403   op = XVECEXP (x, 0, 0);
9404   switch (XINT (x, 1))
9405     {
9406     case UNSPEC_GOTTPOFF:
9407       output_addr_const (file, op);
9408       /* FIXME: This might be @TPOFF in Sun ld.  */
9409       fputs ("@GOTTPOFF", file);
9410       break;
9411     case UNSPEC_TPOFF:
9412       output_addr_const (file, op);
9413       fputs ("@TPOFF", file);
9414       break;
9415     case UNSPEC_NTPOFF:
9416       output_addr_const (file, op);
9417       if (TARGET_64BIT)
9418         fputs ("@TPOFF", file);
9419       else
9420         fputs ("@NTPOFF", file);
9421       break;
9422     case UNSPEC_DTPOFF:
9423       output_addr_const (file, op);
9424       fputs ("@DTPOFF", file);
9425       break;
9426     case UNSPEC_GOTNTPOFF:
9427       output_addr_const (file, op);
9428       if (TARGET_64BIT)
9429         fputs (ASSEMBLER_DIALECT == ASM_ATT ?
9430                "@GOTTPOFF(%rip)" : "@GOTTPOFF[rip]", file);
9431       else
9432         fputs ("@GOTNTPOFF", file);
9433       break;
9434     case UNSPEC_INDNTPOFF:
9435       output_addr_const (file, op);
9436       fputs ("@INDNTPOFF", file);
9437       break;
9438
9439     default:
9440       return false;
9441     }
9442
9443   return true;
9444 }
9445 \f
9446 /* Split one or more DImode RTL references into pairs of SImode
9447    references.  The RTL can be REG, offsettable MEM, integer constant, or
9448    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
9449    split and "num" is its length.  lo_half and hi_half are output arrays
9450    that parallel "operands".  */
9451
9452 void
9453 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
9454 {
9455   while (num--)
9456     {
9457       rtx op = operands[num];
9458
9459       /* simplify_subreg refuse to split volatile memory addresses,
9460          but we still have to handle it.  */
9461       if (MEM_P (op))
9462         {
9463           lo_half[num] = adjust_address (op, SImode, 0);
9464           hi_half[num] = adjust_address (op, SImode, 4);
9465         }
9466       else
9467         {
9468           lo_half[num] = simplify_gen_subreg (SImode, op,
9469                                               GET_MODE (op) == VOIDmode
9470                                               ? DImode : GET_MODE (op), 0);
9471           hi_half[num] = simplify_gen_subreg (SImode, op,
9472                                               GET_MODE (op) == VOIDmode
9473                                               ? DImode : GET_MODE (op), 4);
9474         }
9475     }
9476 }
9477 /* Split one or more TImode RTL references into pairs of DImode
9478    references.  The RTL can be REG, offsettable MEM, integer constant, or
9479    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
9480    split and "num" is its length.  lo_half and hi_half are output arrays
9481    that parallel "operands".  */
9482
9483 void
9484 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
9485 {
9486   while (num--)
9487     {
9488       rtx op = operands[num];
9489
9490       /* simplify_subreg refuse to split volatile memory addresses, but we
9491          still have to handle it.  */
9492       if (MEM_P (op))
9493         {
9494           lo_half[num] = adjust_address (op, DImode, 0);
9495           hi_half[num] = adjust_address (op, DImode, 8);
9496         }
9497       else
9498         {
9499           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
9500           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
9501         }
9502     }
9503 }
9504 \f
9505 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
9506    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
9507    is the expression of the binary operation.  The output may either be
9508    emitted here, or returned to the caller, like all output_* functions.
9509
9510    There is no guarantee that the operands are the same mode, as they
9511    might be within FLOAT or FLOAT_EXTEND expressions.  */
9512
9513 #ifndef SYSV386_COMPAT
9514 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
9515    wants to fix the assemblers because that causes incompatibility
9516    with gcc.  No-one wants to fix gcc because that causes
9517    incompatibility with assemblers...  You can use the option of
9518    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
9519 #define SYSV386_COMPAT 1
9520 #endif
9521
9522 const char *
9523 output_387_binary_op (rtx insn, rtx *operands)
9524 {
9525   static char buf[30];
9526   const char *p;
9527   const char *ssep;
9528   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
9529
9530 #ifdef ENABLE_CHECKING
9531   /* Even if we do not want to check the inputs, this documents input
9532      constraints.  Which helps in understanding the following code.  */
9533   if (STACK_REG_P (operands[0])
9534       && ((REG_P (operands[1])
9535            && REGNO (operands[0]) == REGNO (operands[1])
9536            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
9537           || (REG_P (operands[2])
9538               && REGNO (operands[0]) == REGNO (operands[2])
9539               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
9540       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
9541     ; /* ok */
9542   else
9543     gcc_assert (is_sse);
9544 #endif
9545
9546   switch (GET_CODE (operands[3]))
9547     {
9548     case PLUS:
9549       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
9550           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
9551         p = "fiadd";
9552       else
9553         p = "fadd";
9554       ssep = "add";
9555       break;
9556
9557     case MINUS:
9558       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
9559           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
9560         p = "fisub";
9561       else
9562         p = "fsub";
9563       ssep = "sub";
9564       break;
9565
9566     case MULT:
9567       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
9568           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
9569         p = "fimul";
9570       else
9571         p = "fmul";
9572       ssep = "mul";
9573       break;
9574
9575     case DIV:
9576       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
9577           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
9578         p = "fidiv";
9579       else
9580         p = "fdiv";
9581       ssep = "div";
9582       break;
9583
9584     default:
9585       gcc_unreachable ();
9586     }
9587
9588   if (is_sse)
9589    {
9590       strcpy (buf, ssep);
9591       if (GET_MODE (operands[0]) == SFmode)
9592         strcat (buf, "ss\t{%2, %0|%0, %2}");
9593       else
9594         strcat (buf, "sd\t{%2, %0|%0, %2}");
9595       return buf;
9596    }
9597   strcpy (buf, p);
9598
9599   switch (GET_CODE (operands[3]))
9600     {
9601     case MULT:
9602     case PLUS:
9603       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
9604         {
9605           rtx temp = operands[2];
9606           operands[2] = operands[1];
9607           operands[1] = temp;
9608         }
9609
9610       /* know operands[0] == operands[1].  */
9611
9612       if (MEM_P (operands[2]))
9613         {
9614           p = "%z2\t%2";
9615           break;
9616         }
9617
9618       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
9619         {
9620           if (STACK_TOP_P (operands[0]))
9621             /* How is it that we are storing to a dead operand[2]?
9622                Well, presumably operands[1] is dead too.  We can't
9623                store the result to st(0) as st(0) gets popped on this
9624                instruction.  Instead store to operands[2] (which I
9625                think has to be st(1)).  st(1) will be popped later.
9626                gcc <= 2.8.1 didn't have this check and generated
9627                assembly code that the Unixware assembler rejected.  */
9628             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
9629           else
9630             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
9631           break;
9632         }
9633
9634       if (STACK_TOP_P (operands[0]))
9635         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
9636       else
9637         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
9638       break;
9639
9640     case MINUS:
9641     case DIV:
9642       if (MEM_P (operands[1]))
9643         {
9644           p = "r%z1\t%1";
9645           break;
9646         }
9647
9648       if (MEM_P (operands[2]))
9649         {
9650           p = "%z2\t%2";
9651           break;
9652         }
9653
9654       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
9655         {
9656 #if SYSV386_COMPAT
9657           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
9658              derived assemblers, confusingly reverse the direction of
9659              the operation for fsub{r} and fdiv{r} when the
9660              destination register is not st(0).  The Intel assembler
9661              doesn't have this brain damage.  Read !SYSV386_COMPAT to
9662              figure out what the hardware really does.  */
9663           if (STACK_TOP_P (operands[0]))
9664             p = "{p\t%0, %2|rp\t%2, %0}";
9665           else
9666             p = "{rp\t%2, %0|p\t%0, %2}";
9667 #else
9668           if (STACK_TOP_P (operands[0]))
9669             /* As above for fmul/fadd, we can't store to st(0).  */
9670             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
9671           else
9672             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
9673 #endif
9674           break;
9675         }
9676
9677       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
9678         {
9679 #if SYSV386_COMPAT
9680           if (STACK_TOP_P (operands[0]))
9681             p = "{rp\t%0, %1|p\t%1, %0}";
9682           else
9683             p = "{p\t%1, %0|rp\t%0, %1}";
9684 #else
9685           if (STACK_TOP_P (operands[0]))
9686             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
9687           else
9688             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
9689 #endif
9690           break;
9691         }
9692
9693       if (STACK_TOP_P (operands[0]))
9694         {
9695           if (STACK_TOP_P (operands[1]))
9696             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
9697           else
9698             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
9699           break;
9700         }
9701       else if (STACK_TOP_P (operands[1]))
9702         {
9703 #if SYSV386_COMPAT
9704           p = "{\t%1, %0|r\t%0, %1}";
9705 #else
9706           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
9707 #endif
9708         }
9709       else
9710         {
9711 #if SYSV386_COMPAT
9712           p = "{r\t%2, %0|\t%0, %2}";
9713 #else
9714           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
9715 #endif
9716         }
9717       break;
9718
9719     default:
9720       gcc_unreachable ();
9721     }
9722
9723   strcat (buf, p);
9724   return buf;
9725 }
9726
9727 /* Return needed mode for entity in optimize_mode_switching pass.  */
9728
9729 int
9730 ix86_mode_needed (int entity, rtx insn)
9731 {
9732   enum attr_i387_cw mode;
9733
9734   /* The mode UNINITIALIZED is used to store control word after a
9735      function call or ASM pattern.  The mode ANY specify that function
9736      has no requirements on the control word and make no changes in the
9737      bits we are interested in.  */
9738
9739   if (CALL_P (insn)
9740       || (NONJUMP_INSN_P (insn)
9741           && (asm_noperands (PATTERN (insn)) >= 0
9742               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
9743     return I387_CW_UNINITIALIZED;
9744
9745   if (recog_memoized (insn) < 0)
9746     return I387_CW_ANY;
9747
9748   mode = get_attr_i387_cw (insn);
9749
9750   switch (entity)
9751     {
9752     case I387_TRUNC:
9753       if (mode == I387_CW_TRUNC)
9754         return mode;
9755       break;
9756
9757     case I387_FLOOR:
9758       if (mode == I387_CW_FLOOR)
9759         return mode;
9760       break;
9761
9762     case I387_CEIL:
9763       if (mode == I387_CW_CEIL)
9764         return mode;
9765       break;
9766
9767     case I387_MASK_PM:
9768       if (mode == I387_CW_MASK_PM)
9769         return mode;
9770       break;
9771
9772     default:
9773       gcc_unreachable ();
9774     }
9775
9776   return I387_CW_ANY;
9777 }
9778
9779 /* Output code to initialize control word copies used by trunc?f?i and
9780    rounding patterns.  CURRENT_MODE is set to current control word,
9781    while NEW_MODE is set to new control word.  */
9782
9783 void
9784 emit_i387_cw_initialization (int mode)
9785 {
9786   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
9787   rtx new_mode;
9788
9789   enum ix86_stack_slot slot;
9790
9791   rtx reg = gen_reg_rtx (HImode);
9792
9793   emit_insn (gen_x86_fnstcw_1 (stored_mode));
9794   emit_move_insn (reg, copy_rtx (stored_mode));
9795
9796   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
9797     {
9798       switch (mode)
9799         {
9800         case I387_CW_TRUNC:
9801           /* round toward zero (truncate) */
9802           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
9803           slot = SLOT_CW_TRUNC;
9804           break;
9805
9806         case I387_CW_FLOOR:
9807           /* round down toward -oo */
9808           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9809           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
9810           slot = SLOT_CW_FLOOR;
9811           break;
9812
9813         case I387_CW_CEIL:
9814           /* round up toward +oo */
9815           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9816           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
9817           slot = SLOT_CW_CEIL;
9818           break;
9819
9820         case I387_CW_MASK_PM:
9821           /* mask precision exception for nearbyint() */
9822           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9823           slot = SLOT_CW_MASK_PM;
9824           break;
9825
9826         default:
9827           gcc_unreachable ();
9828         }
9829     }
9830   else
9831     {
9832       switch (mode)
9833         {
9834         case I387_CW_TRUNC:
9835           /* round toward zero (truncate) */
9836           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
9837           slot = SLOT_CW_TRUNC;
9838           break;
9839
9840         case I387_CW_FLOOR:
9841           /* round down toward -oo */
9842           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
9843           slot = SLOT_CW_FLOOR;
9844           break;
9845
9846         case I387_CW_CEIL:
9847           /* round up toward +oo */
9848           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
9849           slot = SLOT_CW_CEIL;
9850           break;
9851
9852         case I387_CW_MASK_PM:
9853           /* mask precision exception for nearbyint() */
9854           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9855           slot = SLOT_CW_MASK_PM;
9856           break;
9857
9858         default:
9859           gcc_unreachable ();
9860         }
9861     }
9862
9863   gcc_assert (slot < MAX_386_STACK_LOCALS);
9864
9865   new_mode = assign_386_stack_local (HImode, slot);
9866   emit_move_insn (new_mode, reg);
9867 }
9868
9869 /* Output code for INSN to convert a float to a signed int.  OPERANDS
9870    are the insn operands.  The output may be [HSD]Imode and the input
9871    operand may be [SDX]Fmode.  */
9872
9873 const char *
9874 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
9875 {
9876   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9877   int dimode_p = GET_MODE (operands[0]) == DImode;
9878   int round_mode = get_attr_i387_cw (insn);
9879
9880   /* Jump through a hoop or two for DImode, since the hardware has no
9881      non-popping instruction.  We used to do this a different way, but
9882      that was somewhat fragile and broke with post-reload splitters.  */
9883   if ((dimode_p || fisttp) && !stack_top_dies)
9884     output_asm_insn ("fld\t%y1", operands);
9885
9886   gcc_assert (STACK_TOP_P (operands[1]));
9887   gcc_assert (MEM_P (operands[0]));
9888   gcc_assert (GET_MODE (operands[1]) != TFmode);
9889
9890   if (fisttp)
9891       output_asm_insn ("fisttp%z0\t%0", operands);
9892   else
9893     {
9894       if (round_mode != I387_CW_ANY)
9895         output_asm_insn ("fldcw\t%3", operands);
9896       if (stack_top_dies || dimode_p)
9897         output_asm_insn ("fistp%z0\t%0", operands);
9898       else
9899         output_asm_insn ("fist%z0\t%0", operands);
9900       if (round_mode != I387_CW_ANY)
9901         output_asm_insn ("fldcw\t%2", operands);
9902     }
9903
9904   return "";
9905 }
9906
9907 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
9908    have the values zero or one, indicates the ffreep insn's operand
9909    from the OPERANDS array.  */
9910
9911 static const char *
9912 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
9913 {
9914   if (TARGET_USE_FFREEP)
9915 #if HAVE_AS_IX86_FFREEP
9916     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
9917 #else
9918     {
9919       static char retval[] = ".word\t0xc_df";
9920       int regno = REGNO (operands[opno]);
9921
9922       gcc_assert (FP_REGNO_P (regno));
9923
9924       retval[9] = '0' + (regno - FIRST_STACK_REG);
9925       return retval;
9926     }
9927 #endif
9928
9929   return opno ? "fstp\t%y1" : "fstp\t%y0";
9930 }
9931
9932
9933 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
9934    should be used.  UNORDERED_P is true when fucom should be used.  */
9935
9936 const char *
9937 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
9938 {
9939   int stack_top_dies;
9940   rtx cmp_op0, cmp_op1;
9941   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
9942
9943   if (eflags_p)
9944     {
9945       cmp_op0 = operands[0];
9946       cmp_op1 = operands[1];
9947     }
9948   else
9949     {
9950       cmp_op0 = operands[1];
9951       cmp_op1 = operands[2];
9952     }
9953
9954   if (is_sse)
9955     {
9956       if (GET_MODE (operands[0]) == SFmode)
9957         if (unordered_p)
9958           return "ucomiss\t{%1, %0|%0, %1}";
9959         else
9960           return "comiss\t{%1, %0|%0, %1}";
9961       else
9962         if (unordered_p)
9963           return "ucomisd\t{%1, %0|%0, %1}";
9964         else
9965           return "comisd\t{%1, %0|%0, %1}";
9966     }
9967
9968   gcc_assert (STACK_TOP_P (cmp_op0));
9969
9970   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9971
9972   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
9973     {
9974       if (stack_top_dies)
9975         {
9976           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
9977           return output_387_ffreep (operands, 1);
9978         }
9979       else
9980         return "ftst\n\tfnstsw\t%0";
9981     }
9982
9983   if (STACK_REG_P (cmp_op1)
9984       && stack_top_dies
9985       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
9986       && REGNO (cmp_op1) != FIRST_STACK_REG)
9987     {
9988       /* If both the top of the 387 stack dies, and the other operand
9989          is also a stack register that dies, then this must be a
9990          `fcompp' float compare */
9991
9992       if (eflags_p)
9993         {
9994           /* There is no double popping fcomi variant.  Fortunately,
9995              eflags is immune from the fstp's cc clobbering.  */
9996           if (unordered_p)
9997             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
9998           else
9999             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
10000           return output_387_ffreep (operands, 0);
10001         }
10002       else
10003         {
10004           if (unordered_p)
10005             return "fucompp\n\tfnstsw\t%0";
10006           else
10007             return "fcompp\n\tfnstsw\t%0";
10008         }
10009     }
10010   else
10011     {
10012       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
10013
10014       static const char * const alt[16] =
10015       {
10016         "fcom%z2\t%y2\n\tfnstsw\t%0",
10017         "fcomp%z2\t%y2\n\tfnstsw\t%0",
10018         "fucom%z2\t%y2\n\tfnstsw\t%0",
10019         "fucomp%z2\t%y2\n\tfnstsw\t%0",
10020
10021         "ficom%z2\t%y2\n\tfnstsw\t%0",
10022         "ficomp%z2\t%y2\n\tfnstsw\t%0",
10023         NULL,
10024         NULL,
10025
10026         "fcomi\t{%y1, %0|%0, %y1}",
10027         "fcomip\t{%y1, %0|%0, %y1}",
10028         "fucomi\t{%y1, %0|%0, %y1}",
10029         "fucomip\t{%y1, %0|%0, %y1}",
10030
10031         NULL,
10032         NULL,
10033         NULL,
10034         NULL
10035       };
10036
10037       int mask;
10038       const char *ret;
10039
10040       mask  = eflags_p << 3;
10041       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
10042       mask |= unordered_p << 1;
10043       mask |= stack_top_dies;
10044
10045       gcc_assert (mask < 16);
10046       ret = alt[mask];
10047       gcc_assert (ret);
10048
10049       return ret;
10050     }
10051 }
10052
10053 void
10054 ix86_output_addr_vec_elt (FILE *file, int value)
10055 {
10056   const char *directive = ASM_LONG;
10057
10058 #ifdef ASM_QUAD
10059   if (TARGET_64BIT)
10060     directive = ASM_QUAD;
10061 #else
10062   gcc_assert (!TARGET_64BIT);
10063 #endif
10064
10065   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
10066 }
10067
10068 void
10069 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
10070 {
10071   const char *directive = ASM_LONG;
10072
10073 #ifdef ASM_QUAD
10074   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
10075     directive = ASM_QUAD;
10076 #else
10077   gcc_assert (!TARGET_64BIT);
10078 #endif
10079   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
10080   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
10081     fprintf (file, "%s%s%d-%s%d\n",
10082              directive, LPREFIX, value, LPREFIX, rel);
10083   else if (HAVE_AS_GOTOFF_IN_DATA)
10084     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
10085 #if TARGET_MACHO
10086   else if (TARGET_MACHO)
10087     {
10088       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
10089       machopic_output_function_base_name (file);
10090       fprintf(file, "\n");
10091     }
10092 #endif
10093   else
10094     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
10095                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
10096 }
10097 \f
10098 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
10099    for the target.  */
10100
10101 void
10102 ix86_expand_clear (rtx dest)
10103 {
10104   rtx tmp;
10105
10106   /* We play register width games, which are only valid after reload.  */
10107   gcc_assert (reload_completed);
10108
10109   /* Avoid HImode and its attendant prefix byte.  */
10110   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
10111     dest = gen_rtx_REG (SImode, REGNO (dest));
10112   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
10113
10114   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
10115   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
10116     {
10117       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10118       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
10119     }
10120
10121   emit_insn (tmp);
10122 }
10123
10124 /* X is an unchanging MEM.  If it is a constant pool reference, return
10125    the constant pool rtx, else NULL.  */
10126
10127 rtx
10128 maybe_get_pool_constant (rtx x)
10129 {
10130   x = ix86_delegitimize_address (XEXP (x, 0));
10131
10132   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
10133     return get_pool_constant (x);
10134
10135   return NULL_RTX;
10136 }
10137
10138 void
10139 ix86_expand_move (enum machine_mode mode, rtx operands[])
10140 {
10141   rtx op0, op1;
10142   enum tls_model model;
10143
10144   op0 = operands[0];
10145   op1 = operands[1];
10146
10147   if (GET_CODE (op1) == SYMBOL_REF)
10148     {
10149       model = SYMBOL_REF_TLS_MODEL (op1);
10150       if (model)
10151         {
10152           op1 = legitimize_tls_address (op1, model, true);
10153           op1 = force_operand (op1, op0);
10154           if (op1 == op0)
10155             return;
10156         }
10157       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
10158                && SYMBOL_REF_DLLIMPORT_P (op1))
10159         op1 = legitimize_dllimport_symbol (op1, false);
10160     }
10161   else if (GET_CODE (op1) == CONST
10162            && GET_CODE (XEXP (op1, 0)) == PLUS
10163            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
10164     {
10165       rtx addend = XEXP (XEXP (op1, 0), 1);
10166       rtx symbol = XEXP (XEXP (op1, 0), 0);
10167       rtx tmp = NULL;
10168
10169       model = SYMBOL_REF_TLS_MODEL (symbol);
10170       if (model)
10171         tmp = legitimize_tls_address (symbol, model, true);
10172       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
10173                && SYMBOL_REF_DLLIMPORT_P (symbol))
10174         tmp = legitimize_dllimport_symbol (symbol, true);
10175
10176       if (tmp)
10177         {
10178           tmp = force_operand (tmp, NULL);
10179           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
10180                                      op0, 1, OPTAB_DIRECT);
10181           if (tmp == op0)
10182             return;
10183         }
10184     }
10185
10186   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
10187     {
10188       if (TARGET_MACHO && !TARGET_64BIT)
10189         {
10190 #if TARGET_MACHO
10191           if (MACHOPIC_PURE)
10192             {
10193               rtx temp = ((reload_in_progress
10194                            || ((op0 && REG_P (op0))
10195                                && mode == Pmode))
10196                           ? op0 : gen_reg_rtx (Pmode));
10197               op1 = machopic_indirect_data_reference (op1, temp);
10198               op1 = machopic_legitimize_pic_address (op1, mode,
10199                                                      temp == op1 ? 0 : temp);
10200             }
10201           else if (MACHOPIC_INDIRECT)
10202             op1 = machopic_indirect_data_reference (op1, 0);
10203           if (op0 == op1)
10204             return;
10205 #endif
10206         }
10207       else
10208         {
10209           if (MEM_P (op0))
10210             op1 = force_reg (Pmode, op1);
10211           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
10212             {
10213               rtx reg = !can_create_pseudo_p () ? op0 : NULL_RTX;
10214               op1 = legitimize_pic_address (op1, reg);
10215               if (op0 == op1)
10216                 return;
10217             }
10218         }
10219     }
10220   else
10221     {
10222       if (MEM_P (op0)
10223           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
10224               || !push_operand (op0, mode))
10225           && MEM_P (op1))
10226         op1 = force_reg (mode, op1);
10227
10228       if (push_operand (op0, mode)
10229           && ! general_no_elim_operand (op1, mode))
10230         op1 = copy_to_mode_reg (mode, op1);
10231
10232       /* Force large constants in 64bit compilation into register
10233          to get them CSEed.  */
10234       if (can_create_pseudo_p ()
10235           && (mode == DImode) && TARGET_64BIT
10236           && immediate_operand (op1, mode)
10237           && !x86_64_zext_immediate_operand (op1, VOIDmode)
10238           && !register_operand (op0, mode)
10239           && optimize)
10240         op1 = copy_to_mode_reg (mode, op1);
10241
10242       if (can_create_pseudo_p ()
10243           && FLOAT_MODE_P (mode)
10244           && GET_CODE (op1) == CONST_DOUBLE)
10245         {
10246           /* If we are loading a floating point constant to a register,
10247              force the value to memory now, since we'll get better code
10248              out the back end.  */
10249
10250           op1 = validize_mem (force_const_mem (mode, op1));
10251           if (!register_operand (op0, mode))
10252             {
10253               rtx temp = gen_reg_rtx (mode);
10254               emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
10255               emit_move_insn (op0, temp);
10256               return;
10257             }
10258         }
10259     }
10260
10261   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
10262 }
10263
10264 void
10265 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
10266 {
10267   rtx op0 = operands[0], op1 = operands[1];
10268   unsigned int align = GET_MODE_ALIGNMENT (mode);
10269
10270   /* Force constants other than zero into memory.  We do not know how
10271      the instructions used to build constants modify the upper 64 bits
10272      of the register, once we have that information we may be able
10273      to handle some of them more efficiently.  */
10274   if (can_create_pseudo_p ()
10275       && register_operand (op0, mode)
10276       && (CONSTANT_P (op1)
10277           || (GET_CODE (op1) == SUBREG
10278               && CONSTANT_P (SUBREG_REG (op1))))
10279       && standard_sse_constant_p (op1) <= 0)
10280     op1 = validize_mem (force_const_mem (mode, op1));
10281
10282   /* TDmode values are passed as TImode on the stack.  TImode values
10283      are moved via xmm registers, and moving them to stack can result in
10284      unaligned memory access.  Use ix86_expand_vector_move_misalign()
10285      if memory operand is not aligned correctly.  */
10286   if (can_create_pseudo_p ()
10287       && (mode == TImode) && !TARGET_64BIT
10288       && ((MEM_P (op0) && (MEM_ALIGN (op0) < align))
10289           || (MEM_P (op1) && (MEM_ALIGN (op1) < align))))
10290     {
10291       rtx tmp[2];
10292
10293       /* ix86_expand_vector_move_misalign() does not like constants ... */
10294       if (CONSTANT_P (op1)
10295           || (GET_CODE (op1) == SUBREG
10296               && CONSTANT_P (SUBREG_REG (op1))))
10297         op1 = validize_mem (force_const_mem (mode, op1));
10298
10299       /* ... nor both arguments in memory.  */
10300       if (!register_operand (op0, mode)
10301           && !register_operand (op1, mode))
10302         op1 = force_reg (mode, op1);
10303
10304       tmp[0] = op0; tmp[1] = op1;
10305       ix86_expand_vector_move_misalign (mode, tmp);
10306       return;
10307     }
10308
10309   /* Make operand1 a register if it isn't already.  */
10310   if (can_create_pseudo_p ()
10311       && !register_operand (op0, mode)
10312       && !register_operand (op1, mode))
10313     {
10314       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
10315       return;
10316     }
10317
10318   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
10319 }
10320
10321 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
10322    straight to ix86_expand_vector_move.  */
10323 /* Code generation for scalar reg-reg moves of single and double precision data:
10324      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
10325        movaps reg, reg
10326      else
10327        movss reg, reg
10328      if (x86_sse_partial_reg_dependency == true)
10329        movapd reg, reg
10330      else
10331        movsd reg, reg
10332
10333    Code generation for scalar loads of double precision data:
10334      if (x86_sse_split_regs == true)
10335        movlpd mem, reg      (gas syntax)
10336      else
10337        movsd mem, reg
10338
10339    Code generation for unaligned packed loads of single precision data
10340    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
10341      if (x86_sse_unaligned_move_optimal)
10342        movups mem, reg
10343
10344      if (x86_sse_partial_reg_dependency == true)
10345        {
10346          xorps  reg, reg
10347          movlps mem, reg
10348          movhps mem+8, reg
10349        }
10350      else
10351        {
10352          movlps mem, reg
10353          movhps mem+8, reg
10354        }
10355
10356    Code generation for unaligned packed loads of double precision data
10357    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
10358      if (x86_sse_unaligned_move_optimal)
10359        movupd mem, reg
10360
10361      if (x86_sse_split_regs == true)
10362        {
10363          movlpd mem, reg
10364          movhpd mem+8, reg
10365        }
10366      else
10367        {
10368          movsd  mem, reg
10369          movhpd mem+8, reg
10370        }
10371  */
10372
10373 void
10374 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
10375 {
10376   rtx op0, op1, m;
10377
10378   op0 = operands[0];
10379   op1 = operands[1];
10380
10381   if (MEM_P (op1))
10382     {
10383       /* If we're optimizing for size, movups is the smallest.  */
10384       if (optimize_size)
10385         {
10386           op0 = gen_lowpart (V4SFmode, op0);
10387           op1 = gen_lowpart (V4SFmode, op1);
10388           emit_insn (gen_sse_movups (op0, op1));
10389           return;
10390         }
10391
10392       /* ??? If we have typed data, then it would appear that using
10393          movdqu is the only way to get unaligned data loaded with
10394          integer type.  */
10395       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
10396         {
10397           op0 = gen_lowpart (V16QImode, op0);
10398           op1 = gen_lowpart (V16QImode, op1);
10399           emit_insn (gen_sse2_movdqu (op0, op1));
10400           return;
10401         }
10402
10403       if (TARGET_SSE2 && mode == V2DFmode)
10404         {
10405           rtx zero;
10406
10407           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
10408             {
10409               op0 = gen_lowpart (V2DFmode, op0);
10410               op1 = gen_lowpart (V2DFmode, op1);
10411               emit_insn (gen_sse2_movupd (op0, op1));
10412               return;
10413             }
10414
10415           /* When SSE registers are split into halves, we can avoid
10416              writing to the top half twice.  */
10417           if (TARGET_SSE_SPLIT_REGS)
10418             {
10419               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
10420               zero = op0;
10421             }
10422           else
10423             {
10424               /* ??? Not sure about the best option for the Intel chips.
10425                  The following would seem to satisfy; the register is
10426                  entirely cleared, breaking the dependency chain.  We
10427                  then store to the upper half, with a dependency depth
10428                  of one.  A rumor has it that Intel recommends two movsd
10429                  followed by an unpacklpd, but this is unconfirmed.  And
10430                  given that the dependency depth of the unpacklpd would
10431                  still be one, I'm not sure why this would be better.  */
10432               zero = CONST0_RTX (V2DFmode);
10433             }
10434
10435           m = adjust_address (op1, DFmode, 0);
10436           emit_insn (gen_sse2_loadlpd (op0, zero, m));
10437           m = adjust_address (op1, DFmode, 8);
10438           emit_insn (gen_sse2_loadhpd (op0, op0, m));
10439         }
10440       else
10441         {
10442           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
10443             {
10444               op0 = gen_lowpart (V4SFmode, op0);
10445               op1 = gen_lowpart (V4SFmode, op1);
10446               emit_insn (gen_sse_movups (op0, op1));
10447               return;
10448             }
10449
10450           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
10451             emit_move_insn (op0, CONST0_RTX (mode));
10452           else
10453             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
10454
10455           if (mode != V4SFmode)
10456             op0 = gen_lowpart (V4SFmode, op0);
10457           m = adjust_address (op1, V2SFmode, 0);
10458           emit_insn (gen_sse_loadlps (op0, op0, m));
10459           m = adjust_address (op1, V2SFmode, 8);
10460           emit_insn (gen_sse_loadhps (op0, op0, m));
10461         }
10462     }
10463   else if (MEM_P (op0))
10464     {
10465       /* If we're optimizing for size, movups is the smallest.  */
10466       if (optimize_size)
10467         {
10468           op0 = gen_lowpart (V4SFmode, op0);
10469           op1 = gen_lowpart (V4SFmode, op1);
10470           emit_insn (gen_sse_movups (op0, op1));
10471           return;
10472         }
10473
10474       /* ??? Similar to above, only less clear because of quote
10475          typeless stores unquote.  */
10476       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
10477           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
10478         {
10479           op0 = gen_lowpart (V16QImode, op0);
10480           op1 = gen_lowpart (V16QImode, op1);
10481           emit_insn (gen_sse2_movdqu (op0, op1));
10482           return;
10483         }
10484
10485       if (TARGET_SSE2 && mode == V2DFmode)
10486         {
10487           m = adjust_address (op0, DFmode, 0);
10488           emit_insn (gen_sse2_storelpd (m, op1));
10489           m = adjust_address (op0, DFmode, 8);
10490           emit_insn (gen_sse2_storehpd (m, op1));
10491         }
10492       else
10493         {
10494           if (mode != V4SFmode)
10495             op1 = gen_lowpart (V4SFmode, op1);
10496           m = adjust_address (op0, V2SFmode, 0);
10497           emit_insn (gen_sse_storelps (m, op1));
10498           m = adjust_address (op0, V2SFmode, 8);
10499           emit_insn (gen_sse_storehps (m, op1));
10500         }
10501     }
10502   else
10503     gcc_unreachable ();
10504 }
10505
10506 /* Expand a push in MODE.  This is some mode for which we do not support
10507    proper push instructions, at least from the registers that we expect
10508    the value to live in.  */
10509
10510 void
10511 ix86_expand_push (enum machine_mode mode, rtx x)
10512 {
10513   rtx tmp;
10514
10515   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
10516                              GEN_INT (-GET_MODE_SIZE (mode)),
10517                              stack_pointer_rtx, 1, OPTAB_DIRECT);
10518   if (tmp != stack_pointer_rtx)
10519     emit_move_insn (stack_pointer_rtx, tmp);
10520
10521   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
10522   emit_move_insn (tmp, x);
10523 }
10524
10525 /* Helper function of ix86_fixup_binary_operands to canonicalize
10526    operand order.  Returns true if the operands should be swapped.  */
10527
10528 static bool
10529 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
10530                              rtx operands[])
10531 {
10532   rtx dst = operands[0];
10533   rtx src1 = operands[1];
10534   rtx src2 = operands[2];
10535
10536   /* If the operation is not commutative, we can't do anything.  */
10537   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
10538     return false;
10539
10540   /* Highest priority is that src1 should match dst.  */
10541   if (rtx_equal_p (dst, src1))
10542     return false;
10543   if (rtx_equal_p (dst, src2))
10544     return true;
10545
10546   /* Next highest priority is that immediate constants come second.  */
10547   if (immediate_operand (src2, mode))
10548     return false;
10549   if (immediate_operand (src1, mode))
10550     return true;
10551
10552   /* Lowest priority is that memory references should come second.  */
10553   if (MEM_P (src2))
10554     return false;
10555   if (MEM_P (src1))
10556     return true;
10557
10558   return false;
10559 }
10560
10561
10562 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
10563    destination to use for the operation.  If different from the true
10564    destination in operands[0], a copy operation will be required.  */
10565
10566 rtx
10567 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
10568                             rtx operands[])
10569 {
10570   rtx dst = operands[0];
10571   rtx src1 = operands[1];
10572   rtx src2 = operands[2];
10573
10574   /* Canonicalize operand order.  */
10575   if (ix86_swap_binary_operands_p (code, mode, operands))
10576     {
10577       rtx temp = src1;
10578       src1 = src2;
10579       src2 = temp;
10580     }
10581
10582   /* Both source operands cannot be in memory.  */
10583   if (MEM_P (src1) && MEM_P (src2))
10584     {
10585       /* Optimization: Only read from memory once.  */
10586       if (rtx_equal_p (src1, src2))
10587         {
10588           src2 = force_reg (mode, src2);
10589           src1 = src2;
10590         }
10591       else
10592         src2 = force_reg (mode, src2);
10593     }
10594
10595   /* If the destination is memory, and we do not have matching source
10596      operands, do things in registers.  */
10597   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
10598     dst = gen_reg_rtx (mode);
10599
10600   /* Source 1 cannot be a constant.  */
10601   if (CONSTANT_P (src1))
10602     src1 = force_reg (mode, src1);
10603
10604   /* Source 1 cannot be a non-matching memory.  */
10605   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
10606     src1 = force_reg (mode, src1);
10607
10608   operands[1] = src1;
10609   operands[2] = src2;
10610   return dst;
10611 }
10612
10613 /* Similarly, but assume that the destination has already been
10614    set up properly.  */
10615
10616 void
10617 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
10618                                     enum machine_mode mode, rtx operands[])
10619 {
10620   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
10621   gcc_assert (dst == operands[0]);
10622 }
10623
10624 /* Attempt to expand a binary operator.  Make the expansion closer to the
10625    actual machine, then just general_operand, which will allow 3 separate
10626    memory references (one output, two input) in a single insn.  */
10627
10628 void
10629 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
10630                              rtx operands[])
10631 {
10632   rtx src1, src2, dst, op, clob;
10633
10634   dst = ix86_fixup_binary_operands (code, mode, operands);
10635   src1 = operands[1];
10636   src2 = operands[2];
10637
10638  /* Emit the instruction.  */
10639
10640   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
10641   if (reload_in_progress)
10642     {
10643       /* Reload doesn't know about the flags register, and doesn't know that
10644          it doesn't want to clobber it.  We can only do this with PLUS.  */
10645       gcc_assert (code == PLUS);
10646       emit_insn (op);
10647     }
10648   else
10649     {
10650       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10651       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
10652     }
10653
10654   /* Fix up the destination if needed.  */
10655   if (dst != operands[0])
10656     emit_move_insn (operands[0], dst);
10657 }
10658
10659 /* Return TRUE or FALSE depending on whether the binary operator meets the
10660    appropriate constraints.  */
10661
10662 int
10663 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
10664                          rtx operands[3])
10665 {
10666   rtx dst = operands[0];
10667   rtx src1 = operands[1];
10668   rtx src2 = operands[2];
10669
10670   /* Both source operands cannot be in memory.  */
10671   if (MEM_P (src1) && MEM_P (src2))
10672     return 0;
10673
10674   /* Canonicalize operand order for commutative operators.  */
10675   if (ix86_swap_binary_operands_p (code, mode, operands))
10676     {
10677       rtx temp = src1;
10678       src1 = src2;
10679       src2 = temp;
10680     }
10681
10682   /* If the destination is memory, we must have a matching source operand.  */
10683   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
10684       return 0;
10685
10686   /* Source 1 cannot be a constant.  */
10687   if (CONSTANT_P (src1))
10688     return 0;
10689
10690   /* Source 1 cannot be a non-matching memory.  */
10691   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
10692     return 0;
10693
10694   return 1;
10695 }
10696
10697 /* Attempt to expand a unary operator.  Make the expansion closer to the
10698    actual machine, then just general_operand, which will allow 2 separate
10699    memory references (one output, one input) in a single insn.  */
10700
10701 void
10702 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
10703                             rtx operands[])
10704 {
10705   int matching_memory;
10706   rtx src, dst, op, clob;
10707
10708   dst = operands[0];
10709   src = operands[1];
10710
10711   /* If the destination is memory, and we do not have matching source
10712      operands, do things in registers.  */
10713   matching_memory = 0;
10714   if (MEM_P (dst))
10715     {
10716       if (rtx_equal_p (dst, src))
10717         matching_memory = 1;
10718       else
10719         dst = gen_reg_rtx (mode);
10720     }
10721
10722   /* When source operand is memory, destination must match.  */
10723   if (MEM_P (src) && !matching_memory)
10724     src = force_reg (mode, src);
10725
10726   /* Emit the instruction.  */
10727
10728   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
10729   if (reload_in_progress || code == NOT)
10730     {
10731       /* Reload doesn't know about the flags register, and doesn't know that
10732          it doesn't want to clobber it.  */
10733       gcc_assert (code == NOT);
10734       emit_insn (op);
10735     }
10736   else
10737     {
10738       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10739       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
10740     }
10741
10742   /* Fix up the destination if needed.  */
10743   if (dst != operands[0])
10744     emit_move_insn (operands[0], dst);
10745 }
10746
10747 /* Return TRUE or FALSE depending on whether the unary operator meets the
10748    appropriate constraints.  */
10749
10750 int
10751 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
10752                         enum machine_mode mode ATTRIBUTE_UNUSED,
10753                         rtx operands[2] ATTRIBUTE_UNUSED)
10754 {
10755   /* If one of operands is memory, source and destination must match.  */
10756   if ((MEM_P (operands[0])
10757        || MEM_P (operands[1]))
10758       && ! rtx_equal_p (operands[0], operands[1]))
10759     return FALSE;
10760   return TRUE;
10761 }
10762
10763 /* Post-reload splitter for converting an SF or DFmode value in an
10764    SSE register into an unsigned SImode.  */
10765
10766 void
10767 ix86_split_convert_uns_si_sse (rtx operands[])
10768 {
10769   enum machine_mode vecmode;
10770   rtx value, large, zero_or_two31, input, two31, x;
10771
10772   large = operands[1];
10773   zero_or_two31 = operands[2];
10774   input = operands[3];
10775   two31 = operands[4];
10776   vecmode = GET_MODE (large);
10777   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
10778
10779   /* Load up the value into the low element.  We must ensure that the other
10780      elements are valid floats -- zero is the easiest such value.  */
10781   if (MEM_P (input))
10782     {
10783       if (vecmode == V4SFmode)
10784         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
10785       else
10786         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
10787     }
10788   else
10789     {
10790       input = gen_rtx_REG (vecmode, REGNO (input));
10791       emit_move_insn (value, CONST0_RTX (vecmode));
10792       if (vecmode == V4SFmode)
10793         emit_insn (gen_sse_movss (value, value, input));
10794       else
10795         emit_insn (gen_sse2_movsd (value, value, input));
10796     }
10797
10798   emit_move_insn (large, two31);
10799   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
10800
10801   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
10802   emit_insn (gen_rtx_SET (VOIDmode, large, x));
10803
10804   x = gen_rtx_AND (vecmode, zero_or_two31, large);
10805   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
10806
10807   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
10808   emit_insn (gen_rtx_SET (VOIDmode, value, x));
10809
10810   large = gen_rtx_REG (V4SImode, REGNO (large));
10811   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
10812
10813   x = gen_rtx_REG (V4SImode, REGNO (value));
10814   if (vecmode == V4SFmode)
10815     emit_insn (gen_sse2_cvttps2dq (x, value));
10816   else
10817     emit_insn (gen_sse2_cvttpd2dq (x, value));
10818   value = x;
10819
10820   emit_insn (gen_xorv4si3 (value, value, large));
10821 }
10822
10823 /* Convert an unsigned DImode value into a DFmode, using only SSE.
10824    Expects the 64-bit DImode to be supplied in a pair of integral
10825    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
10826    -mfpmath=sse, !optimize_size only.  */
10827
10828 void
10829 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
10830 {
10831   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
10832   rtx int_xmm, fp_xmm;
10833   rtx biases, exponents;
10834   rtx x;
10835
10836   int_xmm = gen_reg_rtx (V4SImode);
10837   if (TARGET_INTER_UNIT_MOVES)
10838     emit_insn (gen_movdi_to_sse (int_xmm, input));
10839   else if (TARGET_SSE_SPLIT_REGS)
10840     {
10841       emit_insn (gen_rtx_CLOBBER (VOIDmode, int_xmm));
10842       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
10843     }
10844   else
10845     {
10846       x = gen_reg_rtx (V2DImode);
10847       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
10848       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
10849     }
10850
10851   x = gen_rtx_CONST_VECTOR (V4SImode,
10852                             gen_rtvec (4, GEN_INT (0x43300000UL),
10853                                        GEN_INT (0x45300000UL),
10854                                        const0_rtx, const0_rtx));
10855   exponents = validize_mem (force_const_mem (V4SImode, x));
10856
10857   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
10858   emit_insn (gen_sse2_punpckldq (int_xmm, int_xmm, exponents));
10859
10860   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
10861      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
10862      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
10863      (0x1.0p84 + double(fp_value_hi_xmm)).
10864      Note these exponents differ by 32.  */
10865
10866   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
10867
10868   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
10869      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
10870   real_ldexp (&bias_lo_rvt, &dconst1, 52);
10871   real_ldexp (&bias_hi_rvt, &dconst1, 84);
10872   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
10873   x = const_double_from_real_value (bias_hi_rvt, DFmode);
10874   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
10875   biases = validize_mem (force_const_mem (V2DFmode, biases));
10876   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
10877
10878   /* Add the upper and lower DFmode values together.  */
10879   if (TARGET_SSE3)
10880     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
10881   else
10882     {
10883       x = copy_to_mode_reg (V2DFmode, fp_xmm);
10884       emit_insn (gen_sse2_unpckhpd (fp_xmm, fp_xmm, fp_xmm));
10885       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
10886     }
10887
10888   ix86_expand_vector_extract (false, target, fp_xmm, 0);
10889 }
10890
10891 /* Convert an unsigned SImode value into a DFmode.  Only currently used
10892    for SSE, but applicable anywhere.  */
10893
10894 void
10895 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
10896 {
10897   REAL_VALUE_TYPE TWO31r;
10898   rtx x, fp;
10899
10900   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
10901                            NULL, 1, OPTAB_DIRECT);
10902
10903   fp = gen_reg_rtx (DFmode);
10904   emit_insn (gen_floatsidf2 (fp, x));
10905
10906   real_ldexp (&TWO31r, &dconst1, 31);
10907   x = const_double_from_real_value (TWO31r, DFmode);
10908
10909   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
10910   if (x != target)
10911     emit_move_insn (target, x);
10912 }
10913
10914 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
10915    32-bit mode; otherwise we have a direct convert instruction.  */
10916
10917 void
10918 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
10919 {
10920   REAL_VALUE_TYPE TWO32r;
10921   rtx fp_lo, fp_hi, x;
10922
10923   fp_lo = gen_reg_rtx (DFmode);
10924   fp_hi = gen_reg_rtx (DFmode);
10925
10926   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
10927
10928   real_ldexp (&TWO32r, &dconst1, 32);
10929   x = const_double_from_real_value (TWO32r, DFmode);
10930   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
10931
10932   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
10933
10934   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
10935                            0, OPTAB_DIRECT);
10936   if (x != target)
10937     emit_move_insn (target, x);
10938 }
10939
10940 /* Convert an unsigned SImode value into a SFmode, using only SSE.
10941    For x86_32, -mfpmath=sse, !optimize_size only.  */
10942 void
10943 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
10944 {
10945   REAL_VALUE_TYPE ONE16r;
10946   rtx fp_hi, fp_lo, int_hi, int_lo, x;
10947
10948   real_ldexp (&ONE16r, &dconst1, 16);
10949   x = const_double_from_real_value (ONE16r, SFmode);
10950   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
10951                                       NULL, 0, OPTAB_DIRECT);
10952   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
10953                                       NULL, 0, OPTAB_DIRECT);
10954   fp_hi = gen_reg_rtx (SFmode);
10955   fp_lo = gen_reg_rtx (SFmode);
10956   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
10957   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
10958   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
10959                                0, OPTAB_DIRECT);
10960   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
10961                                0, OPTAB_DIRECT);
10962   if (!rtx_equal_p (target, fp_hi))
10963     emit_move_insn (target, fp_hi);
10964 }
10965
10966 /* A subroutine of ix86_build_signbit_mask_vector.  If VECT is true,
10967    then replicate the value for all elements of the vector
10968    register.  */
10969
10970 rtx
10971 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
10972 {
10973   rtvec v;
10974   switch (mode)
10975     {
10976     case SImode:
10977       gcc_assert (vect);
10978       v = gen_rtvec (4, value, value, value, value);
10979       return gen_rtx_CONST_VECTOR (V4SImode, v);
10980
10981     case DImode:
10982       gcc_assert (vect);
10983       v = gen_rtvec (2, value, value);
10984       return gen_rtx_CONST_VECTOR (V2DImode, v);
10985
10986     case SFmode:
10987       if (vect)
10988         v = gen_rtvec (4, value, value, value, value);
10989       else
10990         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
10991                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10992       return gen_rtx_CONST_VECTOR (V4SFmode, v);
10993
10994     case DFmode:
10995       if (vect)
10996         v = gen_rtvec (2, value, value);
10997       else
10998         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
10999       return gen_rtx_CONST_VECTOR (V2DFmode, v);
11000
11001     default:
11002       gcc_unreachable ();
11003     }
11004 }
11005
11006 /* A subroutine of ix86_expand_fp_absneg_operator, copysign expanders
11007    and ix86_expand_int_vcond.  Create a mask for the sign bit in MODE
11008    for an SSE register.  If VECT is true, then replicate the mask for
11009    all elements of the vector register.  If INVERT is true, then create
11010    a mask excluding the sign bit.  */
11011
11012 rtx
11013 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
11014 {
11015   enum machine_mode vec_mode, imode;
11016   HOST_WIDE_INT hi, lo;
11017   int shift = 63;
11018   rtx v;
11019   rtx mask;
11020
11021   /* Find the sign bit, sign extended to 2*HWI.  */
11022   switch (mode)
11023     {
11024     case SImode:
11025     case SFmode:
11026       imode = SImode;
11027       vec_mode = (mode == SImode) ? V4SImode : V4SFmode;
11028       lo = 0x80000000, hi = lo < 0;
11029       break;
11030
11031     case DImode:
11032     case DFmode:
11033       imode = DImode;
11034       vec_mode = (mode == DImode) ? V2DImode : V2DFmode;
11035       if (HOST_BITS_PER_WIDE_INT >= 64)
11036         lo = (HOST_WIDE_INT)1 << shift, hi = -1;
11037       else
11038         lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
11039       break;
11040
11041     case TImode:
11042     case TFmode:
11043       imode = TImode;
11044       vec_mode = VOIDmode;
11045       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
11046       lo = 0, hi = (HOST_WIDE_INT)1 << shift;
11047      break;
11048
11049     default:
11050       gcc_unreachable ();
11051     }
11052
11053   if (invert)
11054     lo = ~lo, hi = ~hi;
11055
11056   /* Force this value into the low part of a fp vector constant.  */
11057   mask = immed_double_const (lo, hi, imode);
11058   mask = gen_lowpart (mode, mask);
11059
11060   if (vec_mode == VOIDmode)
11061     return force_reg (mode, mask);
11062
11063   v = ix86_build_const_vector (mode, vect, mask);
11064   return force_reg (vec_mode, v);
11065 }
11066
11067 /* Generate code for floating point ABS or NEG.  */
11068
11069 void
11070 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
11071                                 rtx operands[])
11072 {
11073   rtx mask, set, use, clob, dst, src;
11074   bool use_sse = false;
11075   bool vector_mode = VECTOR_MODE_P (mode);
11076   enum machine_mode elt_mode = mode;
11077
11078   if (vector_mode)
11079     {
11080       elt_mode = GET_MODE_INNER (mode);
11081       use_sse = true;
11082     }
11083   else if (mode == TFmode)
11084     use_sse = true;
11085   else if (TARGET_SSE_MATH)
11086     use_sse = SSE_FLOAT_MODE_P (mode);
11087
11088   /* NEG and ABS performed with SSE use bitwise mask operations.
11089      Create the appropriate mask now.  */
11090   if (use_sse)
11091     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
11092   else
11093     mask = NULL_RTX;
11094
11095   dst = operands[0];
11096   src = operands[1];
11097
11098   if (vector_mode)
11099     {
11100       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
11101       set = gen_rtx_SET (VOIDmode, dst, set);
11102       emit_insn (set);
11103     }
11104   else
11105     {
11106       set = gen_rtx_fmt_e (code, mode, src);
11107       set = gen_rtx_SET (VOIDmode, dst, set);
11108       if (mask)
11109         {
11110           use = gen_rtx_USE (VOIDmode, mask);
11111           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
11112           emit_insn (gen_rtx_PARALLEL (VOIDmode,
11113                                        gen_rtvec (3, set, use, clob)));
11114         }
11115       else
11116         emit_insn (set);
11117     }
11118 }
11119
11120 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
11121
11122 void
11123 ix86_expand_copysign (rtx operands[])
11124 {
11125   enum machine_mode mode, vmode;
11126   rtx dest, op0, op1, mask, nmask;
11127
11128   dest = operands[0];
11129   op0 = operands[1];
11130   op1 = operands[2];
11131
11132   mode = GET_MODE (dest);
11133   vmode = mode == SFmode ? V4SFmode : V2DFmode;
11134
11135   if (GET_CODE (op0) == CONST_DOUBLE)
11136     {
11137       rtx (*copysign_insn)(rtx, rtx, rtx, rtx);
11138
11139       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
11140         op0 = simplify_unary_operation (ABS, mode, op0, mode);
11141
11142       if (mode == SFmode || mode == DFmode)
11143         {
11144           if (op0 == CONST0_RTX (mode))
11145             op0 = CONST0_RTX (vmode);
11146           else
11147             {
11148               rtvec v;
11149
11150               if (mode == SFmode)
11151                 v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
11152                                CONST0_RTX (SFmode), CONST0_RTX (SFmode));
11153               else
11154                 v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
11155               op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
11156             }
11157         }
11158
11159       mask = ix86_build_signbit_mask (mode, 0, 0);
11160
11161       if (mode == SFmode)
11162         copysign_insn = gen_copysignsf3_const;
11163       else if (mode == DFmode)
11164         copysign_insn = gen_copysigndf3_const;
11165       else
11166         copysign_insn = gen_copysigntf3_const;
11167
11168         emit_insn (copysign_insn (dest, op0, op1, mask));
11169     }
11170   else
11171     {
11172       rtx (*copysign_insn)(rtx, rtx, rtx, rtx, rtx, rtx);
11173
11174       nmask = ix86_build_signbit_mask (mode, 0, 1);
11175       mask = ix86_build_signbit_mask (mode, 0, 0);
11176
11177       if (mode == SFmode)
11178         copysign_insn = gen_copysignsf3_var;
11179       else if (mode == DFmode)
11180         copysign_insn = gen_copysigndf3_var;
11181       else
11182         copysign_insn = gen_copysigntf3_var;
11183
11184       emit_insn (copysign_insn (dest, NULL_RTX, op0, op1, nmask, mask));
11185     }
11186 }
11187
11188 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
11189    be a constant, and so has already been expanded into a vector constant.  */
11190
11191 void
11192 ix86_split_copysign_const (rtx operands[])
11193 {
11194   enum machine_mode mode, vmode;
11195   rtx dest, op0, op1, mask, x;
11196
11197   dest = operands[0];
11198   op0 = operands[1];
11199   op1 = operands[2];
11200   mask = operands[3];
11201
11202   mode = GET_MODE (dest);
11203   vmode = GET_MODE (mask);
11204
11205   dest = simplify_gen_subreg (vmode, dest, mode, 0);
11206   x = gen_rtx_AND (vmode, dest, mask);
11207   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11208
11209   if (op0 != CONST0_RTX (vmode))
11210     {
11211       x = gen_rtx_IOR (vmode, dest, op0);
11212       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11213     }
11214 }
11215
11216 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
11217    so we have to do two masks.  */
11218
11219 void
11220 ix86_split_copysign_var (rtx operands[])
11221 {
11222   enum machine_mode mode, vmode;
11223   rtx dest, scratch, op0, op1, mask, nmask, x;
11224
11225   dest = operands[0];
11226   scratch = operands[1];
11227   op0 = operands[2];
11228   op1 = operands[3];
11229   nmask = operands[4];
11230   mask = operands[5];
11231
11232   mode = GET_MODE (dest);
11233   vmode = GET_MODE (mask);
11234
11235   if (rtx_equal_p (op0, op1))
11236     {
11237       /* Shouldn't happen often (it's useless, obviously), but when it does
11238          we'd generate incorrect code if we continue below.  */
11239       emit_move_insn (dest, op0);
11240       return;
11241     }
11242
11243   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
11244     {
11245       gcc_assert (REGNO (op1) == REGNO (scratch));
11246
11247       x = gen_rtx_AND (vmode, scratch, mask);
11248       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
11249
11250       dest = mask;
11251       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
11252       x = gen_rtx_NOT (vmode, dest);
11253       x = gen_rtx_AND (vmode, x, op0);
11254       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11255     }
11256   else
11257     {
11258       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
11259         {
11260           x = gen_rtx_AND (vmode, scratch, mask);
11261         }
11262       else                                              /* alternative 2,4 */
11263         {
11264           gcc_assert (REGNO (mask) == REGNO (scratch));
11265           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
11266           x = gen_rtx_AND (vmode, scratch, op1);
11267         }
11268       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
11269
11270       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
11271         {
11272           dest = simplify_gen_subreg (vmode, op0, mode, 0);
11273           x = gen_rtx_AND (vmode, dest, nmask);
11274         }
11275       else                                              /* alternative 3,4 */
11276         {
11277           gcc_assert (REGNO (nmask) == REGNO (dest));
11278           dest = nmask;
11279           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
11280           x = gen_rtx_AND (vmode, dest, op0);
11281         }
11282       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11283     }
11284
11285   x = gen_rtx_IOR (vmode, dest, scratch);
11286   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11287 }
11288
11289 /* Return TRUE or FALSE depending on whether the first SET in INSN
11290    has source and destination with matching CC modes, and that the
11291    CC mode is at least as constrained as REQ_MODE.  */
11292
11293 int
11294 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
11295 {
11296   rtx set;
11297   enum machine_mode set_mode;
11298
11299   set = PATTERN (insn);
11300   if (GET_CODE (set) == PARALLEL)
11301     set = XVECEXP (set, 0, 0);
11302   gcc_assert (GET_CODE (set) == SET);
11303   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
11304
11305   set_mode = GET_MODE (SET_DEST (set));
11306   switch (set_mode)
11307     {
11308     case CCNOmode:
11309       if (req_mode != CCNOmode
11310           && (req_mode != CCmode
11311               || XEXP (SET_SRC (set), 1) != const0_rtx))
11312         return 0;
11313       break;
11314     case CCmode:
11315       if (req_mode == CCGCmode)
11316         return 0;
11317       /* FALLTHRU */
11318     case CCGCmode:
11319       if (req_mode == CCGOCmode || req_mode == CCNOmode)
11320         return 0;
11321       /* FALLTHRU */
11322     case CCGOCmode:
11323       if (req_mode == CCZmode)
11324         return 0;
11325       /* FALLTHRU */
11326     case CCZmode:
11327       break;
11328
11329     default:
11330       gcc_unreachable ();
11331     }
11332
11333   return (GET_MODE (SET_SRC (set)) == set_mode);
11334 }
11335
11336 /* Generate insn patterns to do an integer compare of OPERANDS.  */
11337
11338 static rtx
11339 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
11340 {
11341   enum machine_mode cmpmode;
11342   rtx tmp, flags;
11343
11344   cmpmode = SELECT_CC_MODE (code, op0, op1);
11345   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
11346
11347   /* This is very simple, but making the interface the same as in the
11348      FP case makes the rest of the code easier.  */
11349   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
11350   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
11351
11352   /* Return the test that should be put into the flags user, i.e.
11353      the bcc, scc, or cmov instruction.  */
11354   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
11355 }
11356
11357 /* Figure out whether to use ordered or unordered fp comparisons.
11358    Return the appropriate mode to use.  */
11359
11360 enum machine_mode
11361 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
11362 {
11363   /* ??? In order to make all comparisons reversible, we do all comparisons
11364      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
11365      all forms trapping and nontrapping comparisons, we can make inequality
11366      comparisons trapping again, since it results in better code when using
11367      FCOM based compares.  */
11368   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
11369 }
11370
11371 enum machine_mode
11372 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
11373 {
11374   enum machine_mode mode = GET_MODE (op0);
11375
11376   if (SCALAR_FLOAT_MODE_P (mode))
11377     {
11378       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
11379       return ix86_fp_compare_mode (code);
11380     }
11381
11382   switch (code)
11383     {
11384       /* Only zero flag is needed.  */
11385     case EQ:                    /* ZF=0 */
11386     case NE:                    /* ZF!=0 */
11387       return CCZmode;
11388       /* Codes needing carry flag.  */
11389     case GEU:                   /* CF=0 */
11390     case LTU:                   /* CF=1 */
11391       /* Detect overflow checks.  They need just the carry flag.  */
11392       if (GET_CODE (op0) == PLUS
11393           && rtx_equal_p (op1, XEXP (op0, 0)))
11394         return CCCmode;
11395       else
11396         return CCmode;
11397     case GTU:                   /* CF=0 & ZF=0 */
11398     case LEU:                   /* CF=1 | ZF=1 */
11399       /* Detect overflow checks.  They need just the carry flag.  */
11400       if (GET_CODE (op0) == MINUS
11401           && rtx_equal_p (op1, XEXP (op0, 0)))
11402         return CCCmode;
11403       else
11404         return CCmode;
11405       /* Codes possibly doable only with sign flag when
11406          comparing against zero.  */
11407     case GE:                    /* SF=OF   or   SF=0 */
11408     case LT:                    /* SF<>OF  or   SF=1 */
11409       if (op1 == const0_rtx)
11410         return CCGOCmode;
11411       else
11412         /* For other cases Carry flag is not required.  */
11413         return CCGCmode;
11414       /* Codes doable only with sign flag when comparing
11415          against zero, but we miss jump instruction for it
11416          so we need to use relational tests against overflow
11417          that thus needs to be zero.  */
11418     case GT:                    /* ZF=0 & SF=OF */
11419     case LE:                    /* ZF=1 | SF<>OF */
11420       if (op1 == const0_rtx)
11421         return CCNOmode;
11422       else
11423         return CCGCmode;
11424       /* strcmp pattern do (use flags) and combine may ask us for proper
11425          mode.  */
11426     case USE:
11427       return CCmode;
11428     default:
11429       gcc_unreachable ();
11430     }
11431 }
11432
11433 /* Return the fixed registers used for condition codes.  */
11434
11435 static bool
11436 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
11437 {
11438   *p1 = FLAGS_REG;
11439   *p2 = FPSR_REG;
11440   return true;
11441 }
11442
11443 /* If two condition code modes are compatible, return a condition code
11444    mode which is compatible with both.  Otherwise, return
11445    VOIDmode.  */
11446
11447 static enum machine_mode
11448 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
11449 {
11450   if (m1 == m2)
11451     return m1;
11452
11453   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
11454     return VOIDmode;
11455
11456   if ((m1 == CCGCmode && m2 == CCGOCmode)
11457       || (m1 == CCGOCmode && m2 == CCGCmode))
11458     return CCGCmode;
11459
11460   switch (m1)
11461     {
11462     default:
11463       gcc_unreachable ();
11464
11465     case CCmode:
11466     case CCGCmode:
11467     case CCGOCmode:
11468     case CCNOmode:
11469     case CCAmode:
11470     case CCCmode:
11471     case CCOmode:
11472     case CCSmode:
11473     case CCZmode:
11474       switch (m2)
11475         {
11476         default:
11477           return VOIDmode;
11478
11479         case CCmode:
11480         case CCGCmode:
11481         case CCGOCmode:
11482         case CCNOmode:
11483         case CCAmode:
11484         case CCCmode:
11485         case CCOmode:
11486         case CCSmode:
11487         case CCZmode:
11488           return CCmode;
11489         }
11490
11491     case CCFPmode:
11492     case CCFPUmode:
11493       /* These are only compatible with themselves, which we already
11494          checked above.  */
11495       return VOIDmode;
11496     }
11497 }
11498
11499 /* Split comparison code CODE into comparisons we can do using branch
11500    instructions.  BYPASS_CODE is comparison code for branch that will
11501    branch around FIRST_CODE and SECOND_CODE.  If some of branches
11502    is not required, set value to UNKNOWN.
11503    We never require more than two branches.  */
11504
11505 void
11506 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
11507                           enum rtx_code *first_code,
11508                           enum rtx_code *second_code)
11509 {
11510   *first_code = code;
11511   *bypass_code = UNKNOWN;
11512   *second_code = UNKNOWN;
11513
11514   /* The fcomi comparison sets flags as follows:
11515
11516      cmp    ZF PF CF
11517      >      0  0  0
11518      <      0  0  1
11519      =      1  0  0
11520      un     1  1  1 */
11521
11522   switch (code)
11523     {
11524     case GT:                    /* GTU - CF=0 & ZF=0 */
11525     case GE:                    /* GEU - CF=0 */
11526     case ORDERED:               /* PF=0 */
11527     case UNORDERED:             /* PF=1 */
11528     case UNEQ:                  /* EQ - ZF=1 */
11529     case UNLT:                  /* LTU - CF=1 */
11530     case UNLE:                  /* LEU - CF=1 | ZF=1 */
11531     case LTGT:                  /* EQ - ZF=0 */
11532       break;
11533     case LT:                    /* LTU - CF=1 - fails on unordered */
11534       *first_code = UNLT;
11535       *bypass_code = UNORDERED;
11536       break;
11537     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
11538       *first_code = UNLE;
11539       *bypass_code = UNORDERED;
11540       break;
11541     case EQ:                    /* EQ - ZF=1 - fails on unordered */
11542       *first_code = UNEQ;
11543       *bypass_code = UNORDERED;
11544       break;
11545     case NE:                    /* NE - ZF=0 - fails on unordered */
11546       *first_code = LTGT;
11547       *second_code = UNORDERED;
11548       break;
11549     case UNGE:                  /* GEU - CF=0 - fails on unordered */
11550       *first_code = GE;
11551       *second_code = UNORDERED;
11552       break;
11553     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
11554       *first_code = GT;
11555       *second_code = UNORDERED;
11556       break;
11557     default:
11558       gcc_unreachable ();
11559     }
11560   if (!TARGET_IEEE_FP)
11561     {
11562       *second_code = UNKNOWN;
11563       *bypass_code = UNKNOWN;
11564     }
11565 }
11566
11567 /* Return cost of comparison done fcom + arithmetics operations on AX.
11568    All following functions do use number of instructions as a cost metrics.
11569    In future this should be tweaked to compute bytes for optimize_size and
11570    take into account performance of various instructions on various CPUs.  */
11571 static int
11572 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
11573 {
11574   if (!TARGET_IEEE_FP)
11575     return 4;
11576   /* The cost of code output by ix86_expand_fp_compare.  */
11577   switch (code)
11578     {
11579     case UNLE:
11580     case UNLT:
11581     case LTGT:
11582     case GT:
11583     case GE:
11584     case UNORDERED:
11585     case ORDERED:
11586     case UNEQ:
11587       return 4;
11588       break;
11589     case LT:
11590     case NE:
11591     case EQ:
11592     case UNGE:
11593       return 5;
11594       break;
11595     case LE:
11596     case UNGT:
11597       return 6;
11598       break;
11599     default:
11600       gcc_unreachable ();
11601     }
11602 }
11603
11604 /* Return cost of comparison done using fcomi operation.
11605    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
11606 static int
11607 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
11608 {
11609   enum rtx_code bypass_code, first_code, second_code;
11610   /* Return arbitrarily high cost when instruction is not supported - this
11611      prevents gcc from using it.  */
11612   if (!TARGET_CMOVE)
11613     return 1024;
11614   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11615   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
11616 }
11617
11618 /* Return cost of comparison done using sahf operation.
11619    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
11620 static int
11621 ix86_fp_comparison_sahf_cost (enum rtx_code code)
11622 {
11623   enum rtx_code bypass_code, first_code, second_code;
11624   /* Return arbitrarily high cost when instruction is not preferred - this
11625      avoids gcc from using it.  */
11626   if (!(TARGET_SAHF && (TARGET_USE_SAHF || optimize_size)))
11627     return 1024;
11628   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11629   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
11630 }
11631
11632 /* Compute cost of the comparison done using any method.
11633    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
11634 static int
11635 ix86_fp_comparison_cost (enum rtx_code code)
11636 {
11637   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
11638   int min;
11639
11640   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
11641   sahf_cost = ix86_fp_comparison_sahf_cost (code);
11642
11643   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
11644   if (min > sahf_cost)
11645     min = sahf_cost;
11646   if (min > fcomi_cost)
11647     min = fcomi_cost;
11648   return min;
11649 }
11650
11651 /* Return true if we should use an FCOMI instruction for this
11652    fp comparison.  */
11653
11654 int
11655 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
11656 {
11657   enum rtx_code swapped_code = swap_condition (code);
11658
11659   return ((ix86_fp_comparison_cost (code)
11660            == ix86_fp_comparison_fcomi_cost (code))
11661           || (ix86_fp_comparison_cost (swapped_code)
11662               == ix86_fp_comparison_fcomi_cost (swapped_code)));
11663 }
11664
11665 /* Swap, force into registers, or otherwise massage the two operands
11666    to a fp comparison.  The operands are updated in place; the new
11667    comparison code is returned.  */
11668
11669 static enum rtx_code
11670 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
11671 {
11672   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
11673   rtx op0 = *pop0, op1 = *pop1;
11674   enum machine_mode op_mode = GET_MODE (op0);
11675   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
11676
11677   /* All of the unordered compare instructions only work on registers.
11678      The same is true of the fcomi compare instructions.  The XFmode
11679      compare instructions require registers except when comparing
11680      against zero or when converting operand 1 from fixed point to
11681      floating point.  */
11682
11683   if (!is_sse
11684       && (fpcmp_mode == CCFPUmode
11685           || (op_mode == XFmode
11686               && ! (standard_80387_constant_p (op0) == 1
11687                     || standard_80387_constant_p (op1) == 1)
11688               && GET_CODE (op1) != FLOAT)
11689           || ix86_use_fcomi_compare (code)))
11690     {
11691       op0 = force_reg (op_mode, op0);
11692       op1 = force_reg (op_mode, op1);
11693     }
11694   else
11695     {
11696       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
11697          things around if they appear profitable, otherwise force op0
11698          into a register.  */
11699
11700       if (standard_80387_constant_p (op0) == 0
11701           || (MEM_P (op0)
11702               && ! (standard_80387_constant_p (op1) == 0
11703                     || MEM_P (op1))))
11704         {
11705           rtx tmp;
11706           tmp = op0, op0 = op1, op1 = tmp;
11707           code = swap_condition (code);
11708         }
11709
11710       if (!REG_P (op0))
11711         op0 = force_reg (op_mode, op0);
11712
11713       if (CONSTANT_P (op1))
11714         {
11715           int tmp = standard_80387_constant_p (op1);
11716           if (tmp == 0)
11717             op1 = validize_mem (force_const_mem (op_mode, op1));
11718           else if (tmp == 1)
11719             {
11720               if (TARGET_CMOVE)
11721                 op1 = force_reg (op_mode, op1);
11722             }
11723           else
11724             op1 = force_reg (op_mode, op1);
11725         }
11726     }
11727
11728   /* Try to rearrange the comparison to make it cheaper.  */
11729   if (ix86_fp_comparison_cost (code)
11730       > ix86_fp_comparison_cost (swap_condition (code))
11731       && (REG_P (op1) || can_create_pseudo_p ()))
11732     {
11733       rtx tmp;
11734       tmp = op0, op0 = op1, op1 = tmp;
11735       code = swap_condition (code);
11736       if (!REG_P (op0))
11737         op0 = force_reg (op_mode, op0);
11738     }
11739
11740   *pop0 = op0;
11741   *pop1 = op1;
11742   return code;
11743 }
11744
11745 /* Convert comparison codes we use to represent FP comparison to integer
11746    code that will result in proper branch.  Return UNKNOWN if no such code
11747    is available.  */
11748
11749 enum rtx_code
11750 ix86_fp_compare_code_to_integer (enum rtx_code code)
11751 {
11752   switch (code)
11753     {
11754     case GT:
11755       return GTU;
11756     case GE:
11757       return GEU;
11758     case ORDERED:
11759     case UNORDERED:
11760       return code;
11761       break;
11762     case UNEQ:
11763       return EQ;
11764       break;
11765     case UNLT:
11766       return LTU;
11767       break;
11768     case UNLE:
11769       return LEU;
11770       break;
11771     case LTGT:
11772       return NE;
11773       break;
11774     default:
11775       return UNKNOWN;
11776     }
11777 }
11778
11779 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
11780
11781 static rtx
11782 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
11783                         rtx *second_test, rtx *bypass_test)
11784 {
11785   enum machine_mode fpcmp_mode, intcmp_mode;
11786   rtx tmp, tmp2;
11787   int cost = ix86_fp_comparison_cost (code);
11788   enum rtx_code bypass_code, first_code, second_code;
11789
11790   fpcmp_mode = ix86_fp_compare_mode (code);
11791   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
11792
11793   if (second_test)
11794     *second_test = NULL_RTX;
11795   if (bypass_test)
11796     *bypass_test = NULL_RTX;
11797
11798   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11799
11800   /* Do fcomi/sahf based test when profitable.  */
11801   if (ix86_fp_comparison_arithmetics_cost (code) > cost
11802       && (bypass_code == UNKNOWN || bypass_test)
11803       && (second_code == UNKNOWN || second_test))
11804     {
11805       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11806       tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
11807                          tmp);
11808       if (TARGET_CMOVE)
11809         emit_insn (tmp);
11810       else
11811         {
11812           gcc_assert (TARGET_SAHF);
11813
11814           if (!scratch)
11815             scratch = gen_reg_rtx (HImode);
11816           tmp2 = gen_rtx_CLOBBER (VOIDmode, scratch);
11817
11818           emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, tmp2)));
11819         }
11820
11821       /* The FP codes work out to act like unsigned.  */
11822       intcmp_mode = fpcmp_mode;
11823       code = first_code;
11824       if (bypass_code != UNKNOWN)
11825         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
11826                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11827                                        const0_rtx);
11828       if (second_code != UNKNOWN)
11829         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
11830                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11831                                        const0_rtx);
11832     }
11833   else
11834     {
11835       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
11836       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11837       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
11838       if (!scratch)
11839         scratch = gen_reg_rtx (HImode);
11840       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
11841
11842       /* In the unordered case, we have to check C2 for NaN's, which
11843          doesn't happen to work out to anything nice combination-wise.
11844          So do some bit twiddling on the value we've got in AH to come
11845          up with an appropriate set of condition codes.  */
11846
11847       intcmp_mode = CCNOmode;
11848       switch (code)
11849         {
11850         case GT:
11851         case UNGT:
11852           if (code == GT || !TARGET_IEEE_FP)
11853             {
11854               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11855               code = EQ;
11856             }
11857           else
11858             {
11859               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11860               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11861               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
11862               intcmp_mode = CCmode;
11863               code = GEU;
11864             }
11865           break;
11866         case LT:
11867         case UNLT:
11868           if (code == LT && TARGET_IEEE_FP)
11869             {
11870               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11871               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
11872               intcmp_mode = CCmode;
11873               code = EQ;
11874             }
11875           else
11876             {
11877               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
11878               code = NE;
11879             }
11880           break;
11881         case GE:
11882         case UNGE:
11883           if (code == GE || !TARGET_IEEE_FP)
11884             {
11885               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
11886               code = EQ;
11887             }
11888           else
11889             {
11890               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11891               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11892                                              GEN_INT (0x01)));
11893               code = NE;
11894             }
11895           break;
11896         case LE:
11897         case UNLE:
11898           if (code == LE && TARGET_IEEE_FP)
11899             {
11900               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11901               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11902               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11903               intcmp_mode = CCmode;
11904               code = LTU;
11905             }
11906           else
11907             {
11908               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11909               code = NE;
11910             }
11911           break;
11912         case EQ:
11913         case UNEQ:
11914           if (code == EQ && TARGET_IEEE_FP)
11915             {
11916               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11917               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11918               intcmp_mode = CCmode;
11919               code = EQ;
11920             }
11921           else
11922             {
11923               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11924               code = NE;
11925               break;
11926             }
11927           break;
11928         case NE:
11929         case LTGT:
11930           if (code == NE && TARGET_IEEE_FP)
11931             {
11932               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11933               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11934                                              GEN_INT (0x40)));
11935               code = NE;
11936             }
11937           else
11938             {
11939               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11940               code = EQ;
11941             }
11942           break;
11943
11944         case UNORDERED:
11945           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11946           code = NE;
11947           break;
11948         case ORDERED:
11949           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11950           code = EQ;
11951           break;
11952
11953         default:
11954           gcc_unreachable ();
11955         }
11956     }
11957
11958   /* Return the test that should be put into the flags user, i.e.
11959      the bcc, scc, or cmov instruction.  */
11960   return gen_rtx_fmt_ee (code, VOIDmode,
11961                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
11962                          const0_rtx);
11963 }
11964
11965 rtx
11966 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
11967 {
11968   rtx op0, op1, ret;
11969   op0 = ix86_compare_op0;
11970   op1 = ix86_compare_op1;
11971
11972   if (second_test)
11973     *second_test = NULL_RTX;
11974   if (bypass_test)
11975     *bypass_test = NULL_RTX;
11976
11977   if (ix86_compare_emitted)
11978     {
11979       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
11980       ix86_compare_emitted = NULL_RTX;
11981     }
11982   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
11983     {
11984       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
11985       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11986                                     second_test, bypass_test);
11987     }
11988   else
11989     ret = ix86_expand_int_compare (code, op0, op1);
11990
11991   return ret;
11992 }
11993
11994 /* Return true if the CODE will result in nontrivial jump sequence.  */
11995 bool
11996 ix86_fp_jump_nontrivial_p (enum rtx_code code)
11997 {
11998   enum rtx_code bypass_code, first_code, second_code;
11999   if (!TARGET_CMOVE)
12000     return true;
12001   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
12002   return bypass_code != UNKNOWN || second_code != UNKNOWN;
12003 }
12004
12005 void
12006 ix86_expand_branch (enum rtx_code code, rtx label)
12007 {
12008   rtx tmp;
12009
12010   /* If we have emitted a compare insn, go straight to simple.
12011      ix86_expand_compare won't emit anything if ix86_compare_emitted
12012      is non NULL.  */
12013   if (ix86_compare_emitted)
12014     goto simple;
12015
12016   switch (GET_MODE (ix86_compare_op0))
12017     {
12018     case QImode:
12019     case HImode:
12020     case SImode:
12021       simple:
12022       tmp = ix86_expand_compare (code, NULL, NULL);
12023       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
12024                                   gen_rtx_LABEL_REF (VOIDmode, label),
12025                                   pc_rtx);
12026       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
12027       return;
12028
12029     case SFmode:
12030     case DFmode:
12031     case XFmode:
12032       {
12033         rtvec vec;
12034         int use_fcomi;
12035         enum rtx_code bypass_code, first_code, second_code;
12036
12037         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
12038                                              &ix86_compare_op1);
12039
12040         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
12041
12042         /* Check whether we will use the natural sequence with one jump.  If
12043            so, we can expand jump early.  Otherwise delay expansion by
12044            creating compound insn to not confuse optimizers.  */
12045         if (bypass_code == UNKNOWN && second_code == UNKNOWN)
12046           {
12047             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
12048                                   gen_rtx_LABEL_REF (VOIDmode, label),
12049                                   pc_rtx, NULL_RTX, NULL_RTX);
12050           }
12051         else
12052           {
12053             tmp = gen_rtx_fmt_ee (code, VOIDmode,
12054                                   ix86_compare_op0, ix86_compare_op1);
12055             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
12056                                         gen_rtx_LABEL_REF (VOIDmode, label),
12057                                         pc_rtx);
12058             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
12059
12060             use_fcomi = ix86_use_fcomi_compare (code);
12061             vec = rtvec_alloc (3 + !use_fcomi);
12062             RTVEC_ELT (vec, 0) = tmp;
12063             RTVEC_ELT (vec, 1)
12064               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, FPSR_REG));
12065             RTVEC_ELT (vec, 2)
12066               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, FLAGS_REG));
12067             if (! use_fcomi)
12068               RTVEC_ELT (vec, 3)
12069                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
12070
12071             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
12072           }
12073         return;
12074       }
12075
12076     case DImode:
12077       if (TARGET_64BIT)
12078         goto simple;
12079     case TImode:
12080       /* Expand DImode branch into multiple compare+branch.  */
12081       {
12082         rtx lo[2], hi[2], label2;
12083         enum rtx_code code1, code2, code3;
12084         enum machine_mode submode;
12085
12086         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
12087           {
12088             tmp = ix86_compare_op0;
12089             ix86_compare_op0 = ix86_compare_op1;
12090             ix86_compare_op1 = tmp;
12091             code = swap_condition (code);
12092           }
12093         if (GET_MODE (ix86_compare_op0) == DImode)
12094           {
12095             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
12096             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
12097             submode = SImode;
12098           }
12099         else
12100           {
12101             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
12102             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
12103             submode = DImode;
12104           }
12105
12106         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
12107            avoid two branches.  This costs one extra insn, so disable when
12108            optimizing for size.  */
12109
12110         if ((code == EQ || code == NE)
12111             && (!optimize_size
12112                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
12113           {
12114             rtx xor0, xor1;
12115
12116             xor1 = hi[0];
12117             if (hi[1] != const0_rtx)
12118               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
12119                                    NULL_RTX, 0, OPTAB_WIDEN);
12120
12121             xor0 = lo[0];
12122             if (lo[1] != const0_rtx)
12123               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
12124                                    NULL_RTX, 0, OPTAB_WIDEN);
12125
12126             tmp = expand_binop (submode, ior_optab, xor1, xor0,
12127                                 NULL_RTX, 0, OPTAB_WIDEN);
12128
12129             ix86_compare_op0 = tmp;
12130             ix86_compare_op1 = const0_rtx;
12131             ix86_expand_branch (code, label);
12132             return;
12133           }
12134
12135         /* Otherwise, if we are doing less-than or greater-or-equal-than,
12136            op1 is a constant and the low word is zero, then we can just
12137            examine the high word.  Similarly for low word -1 and
12138            less-or-equal-than or greater-than.  */
12139
12140         if (CONST_INT_P (hi[1]))
12141           switch (code)
12142             {
12143             case LT: case LTU: case GE: case GEU:
12144               if (lo[1] == const0_rtx)
12145                 {
12146                   ix86_compare_op0 = hi[0];
12147                   ix86_compare_op1 = hi[1];
12148                   ix86_expand_branch (code, label);
12149                   return;
12150                 }
12151             case LE: case LEU: case GT: case GTU:
12152               if (lo[1] == constm1_rtx)
12153                 {
12154                   ix86_compare_op0 = hi[0];
12155                   ix86_compare_op1 = hi[1];
12156                   ix86_expand_branch (code, label);
12157                   return;
12158                 }
12159             default:
12160               break;
12161             }
12162
12163         /* Otherwise, we need two or three jumps.  */
12164
12165         label2 = gen_label_rtx ();
12166
12167         code1 = code;
12168         code2 = swap_condition (code);
12169         code3 = unsigned_condition (code);
12170
12171         switch (code)
12172           {
12173           case LT: case GT: case LTU: case GTU:
12174             break;
12175
12176           case LE:   code1 = LT;  code2 = GT;  break;
12177           case GE:   code1 = GT;  code2 = LT;  break;
12178           case LEU:  code1 = LTU; code2 = GTU; break;
12179           case GEU:  code1 = GTU; code2 = LTU; break;
12180
12181           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
12182           case NE:   code2 = UNKNOWN; break;
12183
12184           default:
12185             gcc_unreachable ();
12186           }
12187
12188         /*
12189          * a < b =>
12190          *    if (hi(a) < hi(b)) goto true;
12191          *    if (hi(a) > hi(b)) goto false;
12192          *    if (lo(a) < lo(b)) goto true;
12193          *  false:
12194          */
12195
12196         ix86_compare_op0 = hi[0];
12197         ix86_compare_op1 = hi[1];
12198
12199         if (code1 != UNKNOWN)
12200           ix86_expand_branch (code1, label);
12201         if (code2 != UNKNOWN)
12202           ix86_expand_branch (code2, label2);
12203
12204         ix86_compare_op0 = lo[0];
12205         ix86_compare_op1 = lo[1];
12206         ix86_expand_branch (code3, label);
12207
12208         if (code2 != UNKNOWN)
12209           emit_label (label2);
12210         return;
12211       }
12212
12213     default:
12214       gcc_unreachable ();
12215     }
12216 }
12217
12218 /* Split branch based on floating point condition.  */
12219 void
12220 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
12221                       rtx target1, rtx target2, rtx tmp, rtx pushed)
12222 {
12223   rtx second, bypass;
12224   rtx label = NULL_RTX;
12225   rtx condition;
12226   int bypass_probability = -1, second_probability = -1, probability = -1;
12227   rtx i;
12228
12229   if (target2 != pc_rtx)
12230     {
12231       rtx tmp = target2;
12232       code = reverse_condition_maybe_unordered (code);
12233       target2 = target1;
12234       target1 = tmp;
12235     }
12236
12237   condition = ix86_expand_fp_compare (code, op1, op2,
12238                                       tmp, &second, &bypass);
12239
12240   /* Remove pushed operand from stack.  */
12241   if (pushed)
12242     ix86_free_from_memory (GET_MODE (pushed));
12243
12244   if (split_branch_probability >= 0)
12245     {
12246       /* Distribute the probabilities across the jumps.
12247          Assume the BYPASS and SECOND to be always test
12248          for UNORDERED.  */
12249       probability = split_branch_probability;
12250
12251       /* Value of 1 is low enough to make no need for probability
12252          to be updated.  Later we may run some experiments and see
12253          if unordered values are more frequent in practice.  */
12254       if (bypass)
12255         bypass_probability = 1;
12256       if (second)
12257         second_probability = 1;
12258     }
12259   if (bypass != NULL_RTX)
12260     {
12261       label = gen_label_rtx ();
12262       i = emit_jump_insn (gen_rtx_SET
12263                           (VOIDmode, pc_rtx,
12264                            gen_rtx_IF_THEN_ELSE (VOIDmode,
12265                                                  bypass,
12266                                                  gen_rtx_LABEL_REF (VOIDmode,
12267                                                                     label),
12268                                                  pc_rtx)));
12269       if (bypass_probability >= 0)
12270         REG_NOTES (i)
12271           = gen_rtx_EXPR_LIST (REG_BR_PROB,
12272                                GEN_INT (bypass_probability),
12273                                REG_NOTES (i));
12274     }
12275   i = emit_jump_insn (gen_rtx_SET
12276                       (VOIDmode, pc_rtx,
12277                        gen_rtx_IF_THEN_ELSE (VOIDmode,
12278                                              condition, target1, target2)));
12279   if (probability >= 0)
12280     REG_NOTES (i)
12281       = gen_rtx_EXPR_LIST (REG_BR_PROB,
12282                            GEN_INT (probability),
12283                            REG_NOTES (i));
12284   if (second != NULL_RTX)
12285     {
12286       i = emit_jump_insn (gen_rtx_SET
12287                           (VOIDmode, pc_rtx,
12288                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
12289                                                  target2)));
12290       if (second_probability >= 0)
12291         REG_NOTES (i)
12292           = gen_rtx_EXPR_LIST (REG_BR_PROB,
12293                                GEN_INT (second_probability),
12294                                REG_NOTES (i));
12295     }
12296   if (label != NULL_RTX)
12297     emit_label (label);
12298 }
12299
12300 int
12301 ix86_expand_setcc (enum rtx_code code, rtx dest)
12302 {
12303   rtx ret, tmp, tmpreg, equiv;
12304   rtx second_test, bypass_test;
12305
12306   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
12307     return 0; /* FAIL */
12308
12309   gcc_assert (GET_MODE (dest) == QImode);
12310
12311   ret = ix86_expand_compare (code, &second_test, &bypass_test);
12312   PUT_MODE (ret, QImode);
12313
12314   tmp = dest;
12315   tmpreg = dest;
12316
12317   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
12318   if (bypass_test || second_test)
12319     {
12320       rtx test = second_test;
12321       int bypass = 0;
12322       rtx tmp2 = gen_reg_rtx (QImode);
12323       if (bypass_test)
12324         {
12325           gcc_assert (!second_test);
12326           test = bypass_test;
12327           bypass = 1;
12328           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
12329         }
12330       PUT_MODE (test, QImode);
12331       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
12332
12333       if (bypass)
12334         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
12335       else
12336         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
12337     }
12338
12339   /* Attach a REG_EQUAL note describing the comparison result.  */
12340   if (ix86_compare_op0 && ix86_compare_op1)
12341     {
12342       equiv = simplify_gen_relational (code, QImode,
12343                                        GET_MODE (ix86_compare_op0),
12344                                        ix86_compare_op0, ix86_compare_op1);
12345       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
12346     }
12347
12348   return 1; /* DONE */
12349 }
12350
12351 /* Expand comparison setting or clearing carry flag.  Return true when
12352    successful and set pop for the operation.  */
12353 static bool
12354 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
12355 {
12356   enum machine_mode mode =
12357     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
12358
12359   /* Do not handle DImode compares that go through special path.  */
12360   if (mode == (TARGET_64BIT ? TImode : DImode))
12361     return false;
12362
12363   if (SCALAR_FLOAT_MODE_P (mode))
12364     {
12365       rtx second_test = NULL, bypass_test = NULL;
12366       rtx compare_op, compare_seq;
12367
12368       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
12369
12370       /* Shortcut:  following common codes never translate
12371          into carry flag compares.  */
12372       if (code == EQ || code == NE || code == UNEQ || code == LTGT
12373           || code == ORDERED || code == UNORDERED)
12374         return false;
12375
12376       /* These comparisons require zero flag; swap operands so they won't.  */
12377       if ((code == GT || code == UNLE || code == LE || code == UNGT)
12378           && !TARGET_IEEE_FP)
12379         {
12380           rtx tmp = op0;
12381           op0 = op1;
12382           op1 = tmp;
12383           code = swap_condition (code);
12384         }
12385
12386       /* Try to expand the comparison and verify that we end up with
12387          carry flag based comparison.  This fails to be true only when
12388          we decide to expand comparison using arithmetic that is not
12389          too common scenario.  */
12390       start_sequence ();
12391       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
12392                                            &second_test, &bypass_test);
12393       compare_seq = get_insns ();
12394       end_sequence ();
12395
12396       if (second_test || bypass_test)
12397         return false;
12398
12399       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12400           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12401         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
12402       else
12403         code = GET_CODE (compare_op);
12404
12405       if (code != LTU && code != GEU)
12406         return false;
12407
12408       emit_insn (compare_seq);
12409       *pop = compare_op;
12410       return true;
12411     }
12412
12413   if (!INTEGRAL_MODE_P (mode))
12414     return false;
12415
12416   switch (code)
12417     {
12418     case LTU:
12419     case GEU:
12420       break;
12421
12422     /* Convert a==0 into (unsigned)a<1.  */
12423     case EQ:
12424     case NE:
12425       if (op1 != const0_rtx)
12426         return false;
12427       op1 = const1_rtx;
12428       code = (code == EQ ? LTU : GEU);
12429       break;
12430
12431     /* Convert a>b into b<a or a>=b-1.  */
12432     case GTU:
12433     case LEU:
12434       if (CONST_INT_P (op1))
12435         {
12436           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
12437           /* Bail out on overflow.  We still can swap operands but that
12438              would force loading of the constant into register.  */
12439           if (op1 == const0_rtx
12440               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
12441             return false;
12442           code = (code == GTU ? GEU : LTU);
12443         }
12444       else
12445         {
12446           rtx tmp = op1;
12447           op1 = op0;
12448           op0 = tmp;
12449           code = (code == GTU ? LTU : GEU);
12450         }
12451       break;
12452
12453     /* Convert a>=0 into (unsigned)a<0x80000000.  */
12454     case LT:
12455     case GE:
12456       if (mode == DImode || op1 != const0_rtx)
12457         return false;
12458       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
12459       code = (code == LT ? GEU : LTU);
12460       break;
12461     case LE:
12462     case GT:
12463       if (mode == DImode || op1 != constm1_rtx)
12464         return false;
12465       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
12466       code = (code == LE ? GEU : LTU);
12467       break;
12468
12469     default:
12470       return false;
12471     }
12472   /* Swapping operands may cause constant to appear as first operand.  */
12473   if (!nonimmediate_operand (op0, VOIDmode))
12474     {
12475       if (!can_create_pseudo_p ())
12476         return false;
12477       op0 = force_reg (mode, op0);
12478     }
12479   ix86_compare_op0 = op0;
12480   ix86_compare_op1 = op1;
12481   *pop = ix86_expand_compare (code, NULL, NULL);
12482   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
12483   return true;
12484 }
12485
12486 int
12487 ix86_expand_int_movcc (rtx operands[])
12488 {
12489   enum rtx_code code = GET_CODE (operands[1]), compare_code;
12490   rtx compare_seq, compare_op;
12491   rtx second_test, bypass_test;
12492   enum machine_mode mode = GET_MODE (operands[0]);
12493   bool sign_bit_compare_p = false;;
12494
12495   start_sequence ();
12496   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
12497   compare_seq = get_insns ();
12498   end_sequence ();
12499
12500   compare_code = GET_CODE (compare_op);
12501
12502   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
12503       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
12504     sign_bit_compare_p = true;
12505
12506   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
12507      HImode insns, we'd be swallowed in word prefix ops.  */
12508
12509   if ((mode != HImode || TARGET_FAST_PREFIX)
12510       && (mode != (TARGET_64BIT ? TImode : DImode))
12511       && CONST_INT_P (operands[2])
12512       && CONST_INT_P (operands[3]))
12513     {
12514       rtx out = operands[0];
12515       HOST_WIDE_INT ct = INTVAL (operands[2]);
12516       HOST_WIDE_INT cf = INTVAL (operands[3]);
12517       HOST_WIDE_INT diff;
12518
12519       diff = ct - cf;
12520       /*  Sign bit compares are better done using shifts than we do by using
12521           sbb.  */
12522       if (sign_bit_compare_p
12523           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
12524                                              ix86_compare_op1, &compare_op))
12525         {
12526           /* Detect overlap between destination and compare sources.  */
12527           rtx tmp = out;
12528
12529           if (!sign_bit_compare_p)
12530             {
12531               bool fpcmp = false;
12532
12533               compare_code = GET_CODE (compare_op);
12534
12535               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12536                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12537                 {
12538                   fpcmp = true;
12539                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
12540                 }
12541
12542               /* To simplify rest of code, restrict to the GEU case.  */
12543               if (compare_code == LTU)
12544                 {
12545                   HOST_WIDE_INT tmp = ct;
12546                   ct = cf;
12547                   cf = tmp;
12548                   compare_code = reverse_condition (compare_code);
12549                   code = reverse_condition (code);
12550                 }
12551               else
12552                 {
12553                   if (fpcmp)
12554                     PUT_CODE (compare_op,
12555                               reverse_condition_maybe_unordered
12556                                 (GET_CODE (compare_op)));
12557                   else
12558                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
12559                 }
12560               diff = ct - cf;
12561
12562               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
12563                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
12564                 tmp = gen_reg_rtx (mode);
12565
12566               if (mode == DImode)
12567                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
12568               else
12569                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
12570             }
12571           else
12572             {
12573               if (code == GT || code == GE)
12574                 code = reverse_condition (code);
12575               else
12576                 {
12577                   HOST_WIDE_INT tmp = ct;
12578                   ct = cf;
12579                   cf = tmp;
12580                   diff = ct - cf;
12581                 }
12582               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
12583                                      ix86_compare_op1, VOIDmode, 0, -1);
12584             }
12585
12586           if (diff == 1)
12587             {
12588               /*
12589                * cmpl op0,op1
12590                * sbbl dest,dest
12591                * [addl dest, ct]
12592                *
12593                * Size 5 - 8.
12594                */
12595               if (ct)
12596                 tmp = expand_simple_binop (mode, PLUS,
12597                                            tmp, GEN_INT (ct),
12598                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
12599             }
12600           else if (cf == -1)
12601             {
12602               /*
12603                * cmpl op0,op1
12604                * sbbl dest,dest
12605                * orl $ct, dest
12606                *
12607                * Size 8.
12608                */
12609               tmp = expand_simple_binop (mode, IOR,
12610                                          tmp, GEN_INT (ct),
12611                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
12612             }
12613           else if (diff == -1 && ct)
12614             {
12615               /*
12616                * cmpl op0,op1
12617                * sbbl dest,dest
12618                * notl dest
12619                * [addl dest, cf]
12620                *
12621                * Size 8 - 11.
12622                */
12623               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
12624               if (cf)
12625                 tmp = expand_simple_binop (mode, PLUS,
12626                                            copy_rtx (tmp), GEN_INT (cf),
12627                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
12628             }
12629           else
12630             {
12631               /*
12632                * cmpl op0,op1
12633                * sbbl dest,dest
12634                * [notl dest]
12635                * andl cf - ct, dest
12636                * [addl dest, ct]
12637                *
12638                * Size 8 - 11.
12639                */
12640
12641               if (cf == 0)
12642                 {
12643                   cf = ct;
12644                   ct = 0;
12645                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
12646                 }
12647
12648               tmp = expand_simple_binop (mode, AND,
12649                                          copy_rtx (tmp),
12650                                          gen_int_mode (cf - ct, mode),
12651                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
12652               if (ct)
12653                 tmp = expand_simple_binop (mode, PLUS,
12654                                            copy_rtx (tmp), GEN_INT (ct),
12655                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
12656             }
12657
12658           if (!rtx_equal_p (tmp, out))
12659             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
12660
12661           return 1; /* DONE */
12662         }
12663
12664       if (diff < 0)
12665         {
12666           enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
12667
12668           HOST_WIDE_INT tmp;
12669           tmp = ct, ct = cf, cf = tmp;
12670           diff = -diff;
12671
12672           if (SCALAR_FLOAT_MODE_P (cmp_mode))
12673             {
12674               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
12675
12676               /* We may be reversing unordered compare to normal compare, that
12677                  is not valid in general (we may convert non-trapping condition
12678                  to trapping one), however on i386 we currently emit all
12679                  comparisons unordered.  */
12680               compare_code = reverse_condition_maybe_unordered (compare_code);
12681               code = reverse_condition_maybe_unordered (code);
12682             }
12683           else
12684             {
12685               compare_code = reverse_condition (compare_code);
12686               code = reverse_condition (code);
12687             }
12688         }
12689
12690       compare_code = UNKNOWN;
12691       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
12692           && CONST_INT_P (ix86_compare_op1))
12693         {
12694           if (ix86_compare_op1 == const0_rtx
12695               && (code == LT || code == GE))
12696             compare_code = code;
12697           else if (ix86_compare_op1 == constm1_rtx)
12698             {
12699               if (code == LE)
12700                 compare_code = LT;
12701               else if (code == GT)
12702                 compare_code = GE;
12703             }
12704         }
12705
12706       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
12707       if (compare_code != UNKNOWN
12708           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
12709           && (cf == -1 || ct == -1))
12710         {
12711           /* If lea code below could be used, only optimize
12712              if it results in a 2 insn sequence.  */
12713
12714           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
12715                  || diff == 3 || diff == 5 || diff == 9)
12716               || (compare_code == LT && ct == -1)
12717               || (compare_code == GE && cf == -1))
12718             {
12719               /*
12720                * notl op1       (if necessary)
12721                * sarl $31, op1
12722                * orl cf, op1
12723                */
12724               if (ct != -1)
12725                 {
12726                   cf = ct;
12727                   ct = -1;
12728                   code = reverse_condition (code);
12729                 }
12730
12731               out = emit_store_flag (out, code, ix86_compare_op0,
12732                                      ix86_compare_op1, VOIDmode, 0, -1);
12733
12734               out = expand_simple_binop (mode, IOR,
12735                                          out, GEN_INT (cf),
12736                                          out, 1, OPTAB_DIRECT);
12737               if (out != operands[0])
12738                 emit_move_insn (operands[0], out);
12739
12740               return 1; /* DONE */
12741             }
12742         }
12743
12744
12745       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
12746            || diff == 3 || diff == 5 || diff == 9)
12747           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
12748           && (mode != DImode
12749               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
12750         {
12751           /*
12752            * xorl dest,dest
12753            * cmpl op1,op2
12754            * setcc dest
12755            * lea cf(dest*(ct-cf)),dest
12756            *
12757            * Size 14.
12758            *
12759            * This also catches the degenerate setcc-only case.
12760            */
12761
12762           rtx tmp;
12763           int nops;
12764
12765           out = emit_store_flag (out, code, ix86_compare_op0,
12766                                  ix86_compare_op1, VOIDmode, 0, 1);
12767
12768           nops = 0;
12769           /* On x86_64 the lea instruction operates on Pmode, so we need
12770              to get arithmetics done in proper mode to match.  */
12771           if (diff == 1)
12772             tmp = copy_rtx (out);
12773           else
12774             {
12775               rtx out1;
12776               out1 = copy_rtx (out);
12777               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
12778               nops++;
12779               if (diff & 1)
12780                 {
12781                   tmp = gen_rtx_PLUS (mode, tmp, out1);
12782                   nops++;
12783                 }
12784             }
12785           if (cf != 0)
12786             {
12787               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
12788               nops++;
12789             }
12790           if (!rtx_equal_p (tmp, out))
12791             {
12792               if (nops == 1)
12793                 out = force_operand (tmp, copy_rtx (out));
12794               else
12795                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
12796             }
12797           if (!rtx_equal_p (out, operands[0]))
12798             emit_move_insn (operands[0], copy_rtx (out));
12799
12800           return 1; /* DONE */
12801         }
12802
12803       /*
12804        * General case:                  Jumpful:
12805        *   xorl dest,dest               cmpl op1, op2
12806        *   cmpl op1, op2                movl ct, dest
12807        *   setcc dest                   jcc 1f
12808        *   decl dest                    movl cf, dest
12809        *   andl (cf-ct),dest            1:
12810        *   addl ct,dest
12811        *
12812        * Size 20.                       Size 14.
12813        *
12814        * This is reasonably steep, but branch mispredict costs are
12815        * high on modern cpus, so consider failing only if optimizing
12816        * for space.
12817        */
12818
12819       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12820           && BRANCH_COST >= 2)
12821         {
12822           if (cf == 0)
12823             {
12824               enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
12825
12826               cf = ct;
12827               ct = 0;
12828
12829               if (SCALAR_FLOAT_MODE_P (cmp_mode))
12830                 {
12831                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
12832
12833                   /* We may be reversing unordered compare to normal compare,
12834                      that is not valid in general (we may convert non-trapping
12835                      condition to trapping one), however on i386 we currently
12836                      emit all comparisons unordered.  */
12837                   code = reverse_condition_maybe_unordered (code);
12838                 }
12839               else
12840                 {
12841                   code = reverse_condition (code);
12842                   if (compare_code != UNKNOWN)
12843                     compare_code = reverse_condition (compare_code);
12844                 }
12845             }
12846
12847           if (compare_code != UNKNOWN)
12848             {
12849               /* notl op1       (if needed)
12850                  sarl $31, op1
12851                  andl (cf-ct), op1
12852                  addl ct, op1
12853
12854                  For x < 0 (resp. x <= -1) there will be no notl,
12855                  so if possible swap the constants to get rid of the
12856                  complement.
12857                  True/false will be -1/0 while code below (store flag
12858                  followed by decrement) is 0/-1, so the constants need
12859                  to be exchanged once more.  */
12860
12861               if (compare_code == GE || !cf)
12862                 {
12863                   code = reverse_condition (code);
12864                   compare_code = LT;
12865                 }
12866               else
12867                 {
12868                   HOST_WIDE_INT tmp = cf;
12869                   cf = ct;
12870                   ct = tmp;
12871                 }
12872
12873               out = emit_store_flag (out, code, ix86_compare_op0,
12874                                      ix86_compare_op1, VOIDmode, 0, -1);
12875             }
12876           else
12877             {
12878               out = emit_store_flag (out, code, ix86_compare_op0,
12879                                      ix86_compare_op1, VOIDmode, 0, 1);
12880
12881               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
12882                                          copy_rtx (out), 1, OPTAB_DIRECT);
12883             }
12884
12885           out = expand_simple_binop (mode, AND, copy_rtx (out),
12886                                      gen_int_mode (cf - ct, mode),
12887                                      copy_rtx (out), 1, OPTAB_DIRECT);
12888           if (ct)
12889             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
12890                                        copy_rtx (out), 1, OPTAB_DIRECT);
12891           if (!rtx_equal_p (out, operands[0]))
12892             emit_move_insn (operands[0], copy_rtx (out));
12893
12894           return 1; /* DONE */
12895         }
12896     }
12897
12898   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12899     {
12900       /* Try a few things more with specific constants and a variable.  */
12901
12902       optab op;
12903       rtx var, orig_out, out, tmp;
12904
12905       if (BRANCH_COST <= 2)
12906         return 0; /* FAIL */
12907
12908       /* If one of the two operands is an interesting constant, load a
12909          constant with the above and mask it in with a logical operation.  */
12910
12911       if (CONST_INT_P (operands[2]))
12912         {
12913           var = operands[3];
12914           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
12915             operands[3] = constm1_rtx, op = and_optab;
12916           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
12917             operands[3] = const0_rtx, op = ior_optab;
12918           else
12919             return 0; /* FAIL */
12920         }
12921       else if (CONST_INT_P (operands[3]))
12922         {
12923           var = operands[2];
12924           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
12925             operands[2] = constm1_rtx, op = and_optab;
12926           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
12927             operands[2] = const0_rtx, op = ior_optab;
12928           else
12929             return 0; /* FAIL */
12930         }
12931       else
12932         return 0; /* FAIL */
12933
12934       orig_out = operands[0];
12935       tmp = gen_reg_rtx (mode);
12936       operands[0] = tmp;
12937
12938       /* Recurse to get the constant loaded.  */
12939       if (ix86_expand_int_movcc (operands) == 0)
12940         return 0; /* FAIL */
12941
12942       /* Mask in the interesting variable.  */
12943       out = expand_binop (mode, op, var, tmp, orig_out, 0,
12944                           OPTAB_WIDEN);
12945       if (!rtx_equal_p (out, orig_out))
12946         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
12947
12948       return 1; /* DONE */
12949     }
12950
12951   /*
12952    * For comparison with above,
12953    *
12954    * movl cf,dest
12955    * movl ct,tmp
12956    * cmpl op1,op2
12957    * cmovcc tmp,dest
12958    *
12959    * Size 15.
12960    */
12961
12962   if (! nonimmediate_operand (operands[2], mode))
12963     operands[2] = force_reg (mode, operands[2]);
12964   if (! nonimmediate_operand (operands[3], mode))
12965     operands[3] = force_reg (mode, operands[3]);
12966
12967   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12968     {
12969       rtx tmp = gen_reg_rtx (mode);
12970       emit_move_insn (tmp, operands[3]);
12971       operands[3] = tmp;
12972     }
12973   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12974     {
12975       rtx tmp = gen_reg_rtx (mode);
12976       emit_move_insn (tmp, operands[2]);
12977       operands[2] = tmp;
12978     }
12979
12980   if (! register_operand (operands[2], VOIDmode)
12981       && (mode == QImode
12982           || ! register_operand (operands[3], VOIDmode)))
12983     operands[2] = force_reg (mode, operands[2]);
12984
12985   if (mode == QImode
12986       && ! register_operand (operands[3], VOIDmode))
12987     operands[3] = force_reg (mode, operands[3]);
12988
12989   emit_insn (compare_seq);
12990   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12991                           gen_rtx_IF_THEN_ELSE (mode,
12992                                                 compare_op, operands[2],
12993                                                 operands[3])));
12994   if (bypass_test)
12995     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
12996                             gen_rtx_IF_THEN_ELSE (mode,
12997                                   bypass_test,
12998                                   copy_rtx (operands[3]),
12999                                   copy_rtx (operands[0]))));
13000   if (second_test)
13001     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
13002                             gen_rtx_IF_THEN_ELSE (mode,
13003                                   second_test,
13004                                   copy_rtx (operands[2]),
13005                                   copy_rtx (operands[0]))));
13006
13007   return 1; /* DONE */
13008 }
13009
13010 /* Swap, force into registers, or otherwise massage the two operands
13011    to an sse comparison with a mask result.  Thus we differ a bit from
13012    ix86_prepare_fp_compare_args which expects to produce a flags result.
13013
13014    The DEST operand exists to help determine whether to commute commutative
13015    operators.  The POP0/POP1 operands are updated in place.  The new
13016    comparison code is returned, or UNKNOWN if not implementable.  */
13017
13018 static enum rtx_code
13019 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
13020                                   rtx *pop0, rtx *pop1)
13021 {
13022   rtx tmp;
13023
13024   switch (code)
13025     {
13026     case LTGT:
13027     case UNEQ:
13028       /* We have no LTGT as an operator.  We could implement it with
13029          NE & ORDERED, but this requires an extra temporary.  It's
13030          not clear that it's worth it.  */
13031       return UNKNOWN;
13032
13033     case LT:
13034     case LE:
13035     case UNGT:
13036     case UNGE:
13037       /* These are supported directly.  */
13038       break;
13039
13040     case EQ:
13041     case NE:
13042     case UNORDERED:
13043     case ORDERED:
13044       /* For commutative operators, try to canonicalize the destination
13045          operand to be first in the comparison - this helps reload to
13046          avoid extra moves.  */
13047       if (!dest || !rtx_equal_p (dest, *pop1))
13048         break;
13049       /* FALLTHRU */
13050
13051     case GE:
13052     case GT:
13053     case UNLE:
13054     case UNLT:
13055       /* These are not supported directly.  Swap the comparison operands
13056          to transform into something that is supported.  */
13057       tmp = *pop0;
13058       *pop0 = *pop1;
13059       *pop1 = tmp;
13060       code = swap_condition (code);
13061       break;
13062
13063     default:
13064       gcc_unreachable ();
13065     }
13066
13067   return code;
13068 }
13069
13070 /* Detect conditional moves that exactly match min/max operational
13071    semantics.  Note that this is IEEE safe, as long as we don't
13072    interchange the operands.
13073
13074    Returns FALSE if this conditional move doesn't match a MIN/MAX,
13075    and TRUE if the operation is successful and instructions are emitted.  */
13076
13077 static bool
13078 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
13079                            rtx cmp_op1, rtx if_true, rtx if_false)
13080 {
13081   enum machine_mode mode;
13082   bool is_min;
13083   rtx tmp;
13084
13085   if (code == LT)
13086     ;
13087   else if (code == UNGE)
13088     {
13089       tmp = if_true;
13090       if_true = if_false;
13091       if_false = tmp;
13092     }
13093   else
13094     return false;
13095
13096   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
13097     is_min = true;
13098   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
13099     is_min = false;
13100   else
13101     return false;
13102
13103   mode = GET_MODE (dest);
13104
13105   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
13106      but MODE may be a vector mode and thus not appropriate.  */
13107   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
13108     {
13109       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
13110       rtvec v;
13111
13112       if_true = force_reg (mode, if_true);
13113       v = gen_rtvec (2, if_true, if_false);
13114       tmp = gen_rtx_UNSPEC (mode, v, u);
13115     }
13116   else
13117     {
13118       code = is_min ? SMIN : SMAX;
13119       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
13120     }
13121
13122   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
13123   return true;
13124 }
13125
13126 /* Expand an sse vector comparison.  Return the register with the result.  */
13127
13128 static rtx
13129 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
13130                      rtx op_true, rtx op_false)
13131 {
13132   enum machine_mode mode = GET_MODE (dest);
13133   rtx x;
13134
13135   cmp_op0 = force_reg (mode, cmp_op0);
13136   if (!nonimmediate_operand (cmp_op1, mode))
13137     cmp_op1 = force_reg (mode, cmp_op1);
13138
13139   if (optimize
13140       || reg_overlap_mentioned_p (dest, op_true)
13141       || reg_overlap_mentioned_p (dest, op_false))
13142     dest = gen_reg_rtx (mode);
13143
13144   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
13145   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
13146
13147   return dest;
13148 }
13149
13150 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
13151    operations.  This is used for both scalar and vector conditional moves.  */
13152
13153 static void
13154 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
13155 {
13156   enum machine_mode mode = GET_MODE (dest);
13157   rtx t2, t3, x;
13158
13159   if (TARGET_SSE5)
13160     {
13161       rtx pcmov = gen_rtx_SET (mode, dest,
13162                                gen_rtx_IF_THEN_ELSE (mode, cmp,
13163                                                      op_true,
13164                                                      op_false));
13165       emit_insn (pcmov);
13166     }
13167   else if (op_false == CONST0_RTX (mode))
13168     {
13169       op_true = force_reg (mode, op_true);
13170       x = gen_rtx_AND (mode, cmp, op_true);
13171       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
13172     }
13173   else if (op_true == CONST0_RTX (mode))
13174     {
13175       op_false = force_reg (mode, op_false);
13176       x = gen_rtx_NOT (mode, cmp);
13177       x = gen_rtx_AND (mode, x, op_false);
13178       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
13179     }
13180   else
13181     {
13182       op_true = force_reg (mode, op_true);
13183       op_false = force_reg (mode, op_false);
13184
13185       t2 = gen_reg_rtx (mode);
13186       if (optimize)
13187         t3 = gen_reg_rtx (mode);
13188       else
13189         t3 = dest;
13190
13191       x = gen_rtx_AND (mode, op_true, cmp);
13192       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
13193
13194       x = gen_rtx_NOT (mode, cmp);
13195       x = gen_rtx_AND (mode, x, op_false);
13196       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
13197
13198       x = gen_rtx_IOR (mode, t3, t2);
13199       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
13200     }
13201 }
13202
13203 /* Expand a floating-point conditional move.  Return true if successful.  */
13204
13205 int
13206 ix86_expand_fp_movcc (rtx operands[])
13207 {
13208   enum machine_mode mode = GET_MODE (operands[0]);
13209   enum rtx_code code = GET_CODE (operands[1]);
13210   rtx tmp, compare_op, second_test, bypass_test;
13211
13212   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
13213     {
13214       enum machine_mode cmode;
13215
13216       /* Since we've no cmove for sse registers, don't force bad register
13217          allocation just to gain access to it.  Deny movcc when the
13218          comparison mode doesn't match the move mode.  */
13219       cmode = GET_MODE (ix86_compare_op0);
13220       if (cmode == VOIDmode)
13221         cmode = GET_MODE (ix86_compare_op1);
13222       if (cmode != mode)
13223         return 0;
13224
13225       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
13226                                                &ix86_compare_op0,
13227                                                &ix86_compare_op1);
13228       if (code == UNKNOWN)
13229         return 0;
13230
13231       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
13232                                      ix86_compare_op1, operands[2],
13233                                      operands[3]))
13234         return 1;
13235
13236       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
13237                                  ix86_compare_op1, operands[2], operands[3]);
13238       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
13239       return 1;
13240     }
13241
13242   /* The floating point conditional move instructions don't directly
13243      support conditions resulting from a signed integer comparison.  */
13244
13245   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
13246
13247   /* The floating point conditional move instructions don't directly
13248      support signed integer comparisons.  */
13249
13250   if (!fcmov_comparison_operator (compare_op, VOIDmode))
13251     {
13252       gcc_assert (!second_test && !bypass_test);
13253       tmp = gen_reg_rtx (QImode);
13254       ix86_expand_setcc (code, tmp);
13255       code = NE;
13256       ix86_compare_op0 = tmp;
13257       ix86_compare_op1 = const0_rtx;
13258       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
13259     }
13260   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
13261     {
13262       tmp = gen_reg_rtx (mode);
13263       emit_move_insn (tmp, operands[3]);
13264       operands[3] = tmp;
13265     }
13266   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
13267     {
13268       tmp = gen_reg_rtx (mode);
13269       emit_move_insn (tmp, operands[2]);
13270       operands[2] = tmp;
13271     }
13272
13273   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
13274                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
13275                                                 operands[2], operands[3])));
13276   if (bypass_test)
13277     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
13278                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
13279                                                   operands[3], operands[0])));
13280   if (second_test)
13281     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
13282                             gen_rtx_IF_THEN_ELSE (mode, second_test,
13283                                                   operands[2], operands[0])));
13284
13285   return 1;
13286 }
13287
13288 /* Expand a floating-point vector conditional move; a vcond operation
13289    rather than a movcc operation.  */
13290
13291 bool
13292 ix86_expand_fp_vcond (rtx operands[])
13293 {
13294   enum rtx_code code = GET_CODE (operands[3]);
13295   rtx cmp;
13296
13297   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
13298                                            &operands[4], &operands[5]);
13299   if (code == UNKNOWN)
13300     return false;
13301
13302   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
13303                                  operands[5], operands[1], operands[2]))
13304     return true;
13305
13306   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
13307                              operands[1], operands[2]);
13308   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
13309   return true;
13310 }
13311
13312 /* Expand a signed/unsigned integral vector conditional move.  */
13313
13314 bool
13315 ix86_expand_int_vcond (rtx operands[])
13316 {
13317   enum machine_mode mode = GET_MODE (operands[0]);
13318   enum rtx_code code = GET_CODE (operands[3]);
13319   bool negate = false;
13320   rtx x, cop0, cop1;
13321
13322   cop0 = operands[4];
13323   cop1 = operands[5];
13324
13325   /* Canonicalize the comparison to EQ, GT, GTU.  */
13326   switch (code)
13327     {
13328     case EQ:
13329     case GT:
13330     case GTU:
13331       break;
13332
13333     case NE:
13334     case LE:
13335     case LEU:
13336       code = reverse_condition (code);
13337       negate = true;
13338       break;
13339
13340     case GE:
13341     case GEU:
13342       code = reverse_condition (code);
13343       negate = true;
13344       /* FALLTHRU */
13345
13346     case LT:
13347     case LTU:
13348       code = swap_condition (code);
13349       x = cop0, cop0 = cop1, cop1 = x;
13350       break;
13351
13352     default:
13353       gcc_unreachable ();
13354     }
13355
13356   /* Only SSE4.1/SSE4.2 supports V2DImode.  */
13357   if (mode == V2DImode)
13358     {
13359       switch (code)
13360         {
13361         case EQ:
13362           /* SSE4.1 supports EQ.  */
13363           if (!TARGET_SSE4_1)
13364             return false;
13365           break;
13366
13367         case GT:
13368         case GTU:
13369           /* SSE4.2 supports GT/GTU.  */
13370           if (!TARGET_SSE4_2)
13371             return false;
13372           break;
13373
13374         default:
13375           gcc_unreachable ();
13376         }
13377     }
13378
13379   /* Unsigned parallel compare is not supported by the hardware.  Play some
13380      tricks to turn this into a signed comparison against 0.  */
13381   if (code == GTU)
13382     {
13383       cop0 = force_reg (mode, cop0);
13384
13385       switch (mode)
13386         {
13387         case V4SImode:
13388         case V2DImode:
13389           {
13390             rtx t1, t2, mask;
13391
13392             /* Perform a parallel modulo subtraction.  */
13393             t1 = gen_reg_rtx (mode);
13394             emit_insn ((mode == V4SImode
13395                         ? gen_subv4si3
13396                         : gen_subv2di3) (t1, cop0, cop1));
13397
13398             /* Extract the original sign bit of op0.  */
13399             mask = ix86_build_signbit_mask (GET_MODE_INNER (mode),
13400                                             true, false);
13401             t2 = gen_reg_rtx (mode);
13402             emit_insn ((mode == V4SImode
13403                         ? gen_andv4si3
13404                         : gen_andv2di3) (t2, cop0, mask));
13405
13406             /* XOR it back into the result of the subtraction.  This results
13407                in the sign bit set iff we saw unsigned underflow.  */
13408             x = gen_reg_rtx (mode);
13409             emit_insn ((mode == V4SImode
13410                         ? gen_xorv4si3
13411                         : gen_xorv2di3) (x, t1, t2));
13412
13413             code = GT;
13414           }
13415           break;
13416
13417         case V16QImode:
13418         case V8HImode:
13419           /* Perform a parallel unsigned saturating subtraction.  */
13420           x = gen_reg_rtx (mode);
13421           emit_insn (gen_rtx_SET (VOIDmode, x,
13422                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
13423
13424           code = EQ;
13425           negate = !negate;
13426           break;
13427
13428         default:
13429           gcc_unreachable ();
13430         }
13431
13432       cop0 = x;
13433       cop1 = CONST0_RTX (mode);
13434     }
13435
13436   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
13437                            operands[1+negate], operands[2-negate]);
13438
13439   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
13440                          operands[2-negate]);
13441   return true;
13442 }
13443
13444 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
13445    true if we should do zero extension, else sign extension.  HIGH_P is
13446    true if we want the N/2 high elements, else the low elements.  */
13447
13448 void
13449 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
13450 {
13451   enum machine_mode imode = GET_MODE (operands[1]);
13452   rtx (*unpack)(rtx, rtx, rtx);
13453   rtx se, dest;
13454
13455   switch (imode)
13456     {
13457     case V16QImode:
13458       if (high_p)
13459         unpack = gen_vec_interleave_highv16qi;
13460       else
13461         unpack = gen_vec_interleave_lowv16qi;
13462       break;
13463     case V8HImode:
13464       if (high_p)
13465         unpack = gen_vec_interleave_highv8hi;
13466       else
13467         unpack = gen_vec_interleave_lowv8hi;
13468       break;
13469     case V4SImode:
13470       if (high_p)
13471         unpack = gen_vec_interleave_highv4si;
13472       else
13473         unpack = gen_vec_interleave_lowv4si;
13474       break;
13475     default:
13476       gcc_unreachable ();
13477     }
13478
13479   dest = gen_lowpart (imode, operands[0]);
13480
13481   if (unsigned_p)
13482     se = force_reg (imode, CONST0_RTX (imode));
13483   else
13484     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
13485                               operands[1], pc_rtx, pc_rtx);
13486
13487   emit_insn (unpack (dest, operands[1], se));
13488 }
13489
13490 /* This function performs the same task as ix86_expand_sse_unpack,
13491    but with SSE4.1 instructions.  */
13492
13493 void
13494 ix86_expand_sse4_unpack (rtx operands[2], bool unsigned_p, bool high_p)
13495 {
13496   enum machine_mode imode = GET_MODE (operands[1]);
13497   rtx (*unpack)(rtx, rtx);
13498   rtx src, dest;
13499
13500   switch (imode)
13501     {
13502     case V16QImode:
13503       if (unsigned_p)
13504         unpack = gen_sse4_1_zero_extendv8qiv8hi2;
13505       else
13506         unpack = gen_sse4_1_extendv8qiv8hi2;
13507       break;
13508     case V8HImode:
13509       if (unsigned_p)
13510         unpack = gen_sse4_1_zero_extendv4hiv4si2;
13511       else
13512         unpack = gen_sse4_1_extendv4hiv4si2;
13513       break;
13514     case V4SImode:
13515       if (unsigned_p)
13516         unpack = gen_sse4_1_zero_extendv2siv2di2;
13517       else
13518         unpack = gen_sse4_1_extendv2siv2di2;
13519       break;
13520     default:
13521       gcc_unreachable ();
13522     }
13523
13524   dest = operands[0];
13525   if (high_p)
13526     {
13527       /* Shift higher 8 bytes to lower 8 bytes.  */
13528       src = gen_reg_rtx (imode);
13529       emit_insn (gen_sse2_lshrti3 (gen_lowpart (TImode, src),
13530                                    gen_lowpart (TImode, operands[1]),
13531                                    GEN_INT (64)));
13532     }
13533   else
13534     src = operands[1];
13535
13536   emit_insn (unpack (dest, src));
13537 }
13538
13539 /* This function performs the same task as ix86_expand_sse_unpack,
13540    but with amdfam15 instructions.  */
13541
13542 #define PPERM_SRC       0x00            /* copy source */
13543 #define PPERM_INVERT    0x20            /* invert source */
13544 #define PPERM_REVERSE   0x40            /* bit reverse source */
13545 #define PPERM_REV_INV   0x60            /* bit reverse & invert src */
13546 #define PPERM_ZERO      0x80            /* all 0's */
13547 #define PPERM_ONES      0xa0            /* all 1's */
13548 #define PPERM_SIGN      0xc0            /* propagate sign bit */
13549 #define PPERM_INV_SIGN  0xe0            /* invert & propagate sign */
13550
13551 #define PPERM_SRC1      0x00            /* use first source byte */
13552 #define PPERM_SRC2      0x10            /* use second source byte */
13553
13554 void
13555 ix86_expand_sse5_unpack (rtx operands[2], bool unsigned_p, bool high_p)
13556 {
13557   enum machine_mode imode = GET_MODE (operands[1]);
13558   int pperm_bytes[16];
13559   int i;
13560   int h = (high_p) ? 8 : 0;
13561   int h2;
13562   int sign_extend;
13563   rtvec v = rtvec_alloc (16);
13564   rtvec vs;
13565   rtx x, p;
13566   rtx op0 = operands[0], op1 = operands[1];
13567
13568   switch (imode)
13569     {
13570     case V16QImode:
13571       vs = rtvec_alloc (8);
13572       h2 = (high_p) ? 8 : 0;
13573       for (i = 0; i < 8; i++)
13574         {
13575           pperm_bytes[2*i+0] = PPERM_SRC | PPERM_SRC2 | i | h;
13576           pperm_bytes[2*i+1] = ((unsigned_p)
13577                                 ? PPERM_ZERO
13578                                 : PPERM_SIGN | PPERM_SRC2 | i | h);
13579         }
13580
13581       for (i = 0; i < 16; i++)
13582         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13583
13584       for (i = 0; i < 8; i++)
13585         RTVEC_ELT (vs, i) = GEN_INT (i + h2);
13586
13587       p = gen_rtx_PARALLEL (VOIDmode, vs);
13588       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13589       if (unsigned_p)
13590         emit_insn (gen_sse5_pperm_zero_v16qi_v8hi (op0, op1, p, x));
13591       else
13592         emit_insn (gen_sse5_pperm_sign_v16qi_v8hi (op0, op1, p, x));
13593       break;
13594
13595     case V8HImode:
13596       vs = rtvec_alloc (4);
13597       h2 = (high_p) ? 4 : 0;
13598       for (i = 0; i < 4; i++)
13599         {
13600           sign_extend = ((unsigned_p)
13601                          ? PPERM_ZERO
13602                          : PPERM_SIGN | PPERM_SRC2 | ((2*i) + 1 + h));
13603           pperm_bytes[4*i+0] = PPERM_SRC | PPERM_SRC2 | ((2*i) + 0 + h);
13604           pperm_bytes[4*i+1] = PPERM_SRC | PPERM_SRC2 | ((2*i) + 1 + h);
13605           pperm_bytes[4*i+2] = sign_extend;
13606           pperm_bytes[4*i+3] = sign_extend;
13607         }
13608
13609       for (i = 0; i < 16; i++)
13610         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13611
13612       for (i = 0; i < 4; i++)
13613         RTVEC_ELT (vs, i) = GEN_INT (i + h2);
13614
13615       p = gen_rtx_PARALLEL (VOIDmode, vs);
13616       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13617       if (unsigned_p)
13618         emit_insn (gen_sse5_pperm_zero_v8hi_v4si (op0, op1, p, x));
13619       else
13620         emit_insn (gen_sse5_pperm_sign_v8hi_v4si (op0, op1, p, x));
13621       break;
13622
13623     case V4SImode:
13624       vs = rtvec_alloc (2);
13625       h2 = (high_p) ? 2 : 0;
13626       for (i = 0; i < 2; i++)
13627         {
13628           sign_extend = ((unsigned_p)
13629                          ? PPERM_ZERO
13630                          : PPERM_SIGN | PPERM_SRC2 | ((4*i) + 3 + h));
13631           pperm_bytes[8*i+0] = PPERM_SRC | PPERM_SRC2 | ((4*i) + 0 + h);
13632           pperm_bytes[8*i+1] = PPERM_SRC | PPERM_SRC2 | ((4*i) + 1 + h);
13633           pperm_bytes[8*i+2] = PPERM_SRC | PPERM_SRC2 | ((4*i) + 2 + h);
13634           pperm_bytes[8*i+3] = PPERM_SRC | PPERM_SRC2 | ((4*i) + 3 + h);
13635           pperm_bytes[8*i+4] = sign_extend;
13636           pperm_bytes[8*i+5] = sign_extend;
13637           pperm_bytes[8*i+6] = sign_extend;
13638           pperm_bytes[8*i+7] = sign_extend;
13639         }
13640
13641       for (i = 0; i < 16; i++)
13642         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13643
13644       for (i = 0; i < 2; i++)
13645         RTVEC_ELT (vs, i) = GEN_INT (i + h2);
13646
13647       p = gen_rtx_PARALLEL (VOIDmode, vs);
13648       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13649       if (unsigned_p)
13650         emit_insn (gen_sse5_pperm_zero_v4si_v2di (op0, op1, p, x));
13651       else
13652         emit_insn (gen_sse5_pperm_sign_v4si_v2di (op0, op1, p, x));
13653       break;
13654
13655     default:
13656       gcc_unreachable ();
13657     }
13658
13659   return;
13660 }
13661
13662 /* Pack the high bits from OPERANDS[1] and low bits from OPERANDS[2] into the
13663    next narrower integer vector type */
13664 void
13665 ix86_expand_sse5_pack (rtx operands[3])
13666 {
13667   enum machine_mode imode = GET_MODE (operands[0]);
13668   int pperm_bytes[16];
13669   int i;
13670   rtvec v = rtvec_alloc (16);
13671   rtx x;
13672   rtx op0 = operands[0];
13673   rtx op1 = operands[1];
13674   rtx op2 = operands[2];
13675
13676   switch (imode)
13677     {
13678     case V16QImode:
13679       for (i = 0; i < 8; i++)
13680         {
13681           pperm_bytes[i+0] = PPERM_SRC | PPERM_SRC1 | (i*2);
13682           pperm_bytes[i+8] = PPERM_SRC | PPERM_SRC2 | (i*2);
13683         }
13684
13685       for (i = 0; i < 16; i++)
13686         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13687
13688       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13689       emit_insn (gen_sse5_pperm_pack_v8hi_v16qi (op0, op1, op2, x));
13690       break;
13691
13692     case V8HImode:
13693       for (i = 0; i < 4; i++)
13694         {
13695           pperm_bytes[(2*i)+0] = PPERM_SRC | PPERM_SRC1 | ((i*4) + 0);
13696           pperm_bytes[(2*i)+1] = PPERM_SRC | PPERM_SRC1 | ((i*4) + 1);
13697           pperm_bytes[(2*i)+8] = PPERM_SRC | PPERM_SRC2 | ((i*4) + 0);
13698           pperm_bytes[(2*i)+9] = PPERM_SRC | PPERM_SRC2 | ((i*4) + 1);
13699         }
13700
13701       for (i = 0; i < 16; i++)
13702         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13703
13704       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13705       emit_insn (gen_sse5_pperm_pack_v4si_v8hi (op0, op1, op2, x));
13706       break;
13707
13708     case V4SImode:
13709       for (i = 0; i < 2; i++)
13710         {
13711           pperm_bytes[(4*i)+0]  = PPERM_SRC | PPERM_SRC1 | ((i*8) + 0);
13712           pperm_bytes[(4*i)+1]  = PPERM_SRC | PPERM_SRC1 | ((i*8) + 1);
13713           pperm_bytes[(4*i)+2]  = PPERM_SRC | PPERM_SRC1 | ((i*8) + 2);
13714           pperm_bytes[(4*i)+3]  = PPERM_SRC | PPERM_SRC1 | ((i*8) + 3);
13715           pperm_bytes[(4*i)+8]  = PPERM_SRC | PPERM_SRC2 | ((i*8) + 0);
13716           pperm_bytes[(4*i)+9]  = PPERM_SRC | PPERM_SRC2 | ((i*8) + 1);
13717           pperm_bytes[(4*i)+10] = PPERM_SRC | PPERM_SRC2 | ((i*8) + 2);
13718           pperm_bytes[(4*i)+11] = PPERM_SRC | PPERM_SRC2 | ((i*8) + 3);
13719         }
13720
13721       for (i = 0; i < 16; i++)
13722         RTVEC_ELT (v, i) = GEN_INT (pperm_bytes[i]);
13723
13724       x = force_reg (V16QImode, gen_rtx_CONST_VECTOR (V16QImode, v));
13725       emit_insn (gen_sse5_pperm_pack_v2di_v4si (op0, op1, op2, x));
13726       break;
13727
13728     default:
13729       gcc_unreachable ();
13730     }
13731
13732   return;
13733 }
13734
13735 /* Expand conditional increment or decrement using adb/sbb instructions.
13736    The default case using setcc followed by the conditional move can be
13737    done by generic code.  */
13738 int
13739 ix86_expand_int_addcc (rtx operands[])
13740 {
13741   enum rtx_code code = GET_CODE (operands[1]);
13742   rtx compare_op;
13743   rtx val = const0_rtx;
13744   bool fpcmp = false;
13745   enum machine_mode mode = GET_MODE (operands[0]);
13746
13747   if (operands[3] != const1_rtx
13748       && operands[3] != constm1_rtx)
13749     return 0;
13750   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
13751                                        ix86_compare_op1, &compare_op))
13752      return 0;
13753   code = GET_CODE (compare_op);
13754
13755   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
13756       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
13757     {
13758       fpcmp = true;
13759       code = ix86_fp_compare_code_to_integer (code);
13760     }
13761
13762   if (code != LTU)
13763     {
13764       val = constm1_rtx;
13765       if (fpcmp)
13766         PUT_CODE (compare_op,
13767                   reverse_condition_maybe_unordered
13768                     (GET_CODE (compare_op)));
13769       else
13770         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
13771     }
13772   PUT_MODE (compare_op, mode);
13773
13774   /* Construct either adc or sbb insn.  */
13775   if ((code == LTU) == (operands[3] == constm1_rtx))
13776     {
13777       switch (GET_MODE (operands[0]))
13778         {
13779           case QImode:
13780             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
13781             break;
13782           case HImode:
13783             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
13784             break;
13785           case SImode:
13786             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
13787             break;
13788           case DImode:
13789             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
13790             break;
13791           default:
13792             gcc_unreachable ();
13793         }
13794     }
13795   else
13796     {
13797       switch (GET_MODE (operands[0]))
13798         {
13799           case QImode:
13800             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
13801             break;
13802           case HImode:
13803             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
13804             break;
13805           case SImode:
13806             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
13807             break;
13808           case DImode:
13809             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
13810             break;
13811           default:
13812             gcc_unreachable ();
13813         }
13814     }
13815   return 1; /* DONE */
13816 }
13817
13818
13819 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
13820    works for floating pointer parameters and nonoffsetable memories.
13821    For pushes, it returns just stack offsets; the values will be saved
13822    in the right order.  Maximally three parts are generated.  */
13823
13824 static int
13825 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
13826 {
13827   int size;
13828
13829   if (!TARGET_64BIT)
13830     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
13831   else
13832     size = (GET_MODE_SIZE (mode) + 4) / 8;
13833
13834   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
13835   gcc_assert (size >= 2 && size <= 3);
13836
13837   /* Optimize constant pool reference to immediates.  This is used by fp
13838      moves, that force all constants to memory to allow combining.  */
13839   if (MEM_P (operand) && MEM_READONLY_P (operand))
13840     {
13841       rtx tmp = maybe_get_pool_constant (operand);
13842       if (tmp)
13843         operand = tmp;
13844     }
13845
13846   if (MEM_P (operand) && !offsettable_memref_p (operand))
13847     {
13848       /* The only non-offsetable memories we handle are pushes.  */
13849       int ok = push_operand (operand, VOIDmode);
13850
13851       gcc_assert (ok);
13852
13853       operand = copy_rtx (operand);
13854       PUT_MODE (operand, Pmode);
13855       parts[0] = parts[1] = parts[2] = operand;
13856       return size;
13857     }
13858
13859   if (GET_CODE (operand) == CONST_VECTOR)
13860     {
13861       enum machine_mode imode = int_mode_for_mode (mode);
13862       /* Caution: if we looked through a constant pool memory above,
13863          the operand may actually have a different mode now.  That's
13864          ok, since we want to pun this all the way back to an integer.  */
13865       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
13866       gcc_assert (operand != NULL);
13867       mode = imode;
13868     }
13869
13870   if (!TARGET_64BIT)
13871     {
13872       if (mode == DImode)
13873         split_di (&operand, 1, &parts[0], &parts[1]);
13874       else
13875         {
13876           if (REG_P (operand))
13877             {
13878               gcc_assert (reload_completed);
13879               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
13880               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
13881               if (size == 3)
13882                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
13883             }
13884           else if (offsettable_memref_p (operand))
13885             {
13886               operand = adjust_address (operand, SImode, 0);
13887               parts[0] = operand;
13888               parts[1] = adjust_address (operand, SImode, 4);
13889               if (size == 3)
13890                 parts[2] = adjust_address (operand, SImode, 8);
13891             }
13892           else if (GET_CODE (operand) == CONST_DOUBLE)
13893             {
13894               REAL_VALUE_TYPE r;
13895               long l[4];
13896
13897               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
13898               switch (mode)
13899                 {
13900                 case XFmode:
13901                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
13902                   parts[2] = gen_int_mode (l[2], SImode);
13903                   break;
13904                 case DFmode:
13905                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
13906                   break;
13907                 default:
13908                   gcc_unreachable ();
13909                 }
13910               parts[1] = gen_int_mode (l[1], SImode);
13911               parts[0] = gen_int_mode (l[0], SImode);
13912             }
13913           else
13914             gcc_unreachable ();
13915         }
13916     }
13917   else
13918     {
13919       if (mode == TImode)
13920         split_ti (&operand, 1, &parts[0], &parts[1]);
13921       if (mode == XFmode || mode == TFmode)
13922         {
13923           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
13924           if (REG_P (operand))
13925             {
13926               gcc_assert (reload_completed);
13927               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
13928               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
13929             }
13930           else if (offsettable_memref_p (operand))
13931             {
13932               operand = adjust_address (operand, DImode, 0);
13933               parts[0] = operand;
13934               parts[1] = adjust_address (operand, upper_mode, 8);
13935             }
13936           else if (GET_CODE (operand) == CONST_DOUBLE)
13937             {
13938               REAL_VALUE_TYPE r;
13939               long l[4];
13940
13941               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
13942               real_to_target (l, &r, mode);
13943
13944               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
13945               if (HOST_BITS_PER_WIDE_INT >= 64)
13946                 parts[0]
13947                   = gen_int_mode
13948                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
13949                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
13950                        DImode);
13951               else
13952                 parts[0] = immed_double_const (l[0], l[1], DImode);
13953
13954               if (upper_mode == SImode)
13955                 parts[1] = gen_int_mode (l[2], SImode);
13956               else if (HOST_BITS_PER_WIDE_INT >= 64)
13957                 parts[1]
13958                   = gen_int_mode
13959                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
13960                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
13961                        DImode);
13962               else
13963                 parts[1] = immed_double_const (l[2], l[3], DImode);
13964             }
13965           else
13966             gcc_unreachable ();
13967         }
13968     }
13969
13970   return size;
13971 }
13972
13973 /* Emit insns to perform a move or push of DI, DF, and XF values.
13974    Return false when normal moves are needed; true when all required
13975    insns have been emitted.  Operands 2-4 contain the input values
13976    int the correct order; operands 5-7 contain the output values.  */
13977
13978 void
13979 ix86_split_long_move (rtx operands[])
13980 {
13981   rtx part[2][3];
13982   int nparts;
13983   int push = 0;
13984   int collisions = 0;
13985   enum machine_mode mode = GET_MODE (operands[0]);
13986
13987   /* The DFmode expanders may ask us to move double.
13988      For 64bit target this is single move.  By hiding the fact
13989      here we simplify i386.md splitters.  */
13990   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
13991     {
13992       /* Optimize constant pool reference to immediates.  This is used by
13993          fp moves, that force all constants to memory to allow combining.  */
13994
13995       if (MEM_P (operands[1])
13996           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
13997           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
13998         operands[1] = get_pool_constant (XEXP (operands[1], 0));
13999       if (push_operand (operands[0], VOIDmode))
14000         {
14001           operands[0] = copy_rtx (operands[0]);
14002           PUT_MODE (operands[0], Pmode);
14003         }
14004       else
14005         operands[0] = gen_lowpart (DImode, operands[0]);
14006       operands[1] = gen_lowpart (DImode, operands[1]);
14007       emit_move_insn (operands[0], operands[1]);
14008       return;
14009     }
14010
14011   /* The only non-offsettable memory we handle is push.  */
14012   if (push_operand (operands[0], VOIDmode))
14013     push = 1;
14014   else
14015     gcc_assert (!MEM_P (operands[0])
14016                 || offsettable_memref_p (operands[0]));
14017
14018   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
14019   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
14020
14021   /* When emitting push, take care for source operands on the stack.  */
14022   if (push && MEM_P (operands[1])
14023       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
14024     {
14025       if (nparts == 3)
14026         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
14027                                      XEXP (part[1][2], 0));
14028       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
14029                                    XEXP (part[1][1], 0));
14030     }
14031
14032   /* We need to do copy in the right order in case an address register
14033      of the source overlaps the destination.  */
14034   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
14035     {
14036       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
14037         collisions++;
14038       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
14039         collisions++;
14040       if (nparts == 3
14041           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
14042         collisions++;
14043
14044       /* Collision in the middle part can be handled by reordering.  */
14045       if (collisions == 1 && nparts == 3
14046           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
14047         {
14048           rtx tmp;
14049           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
14050           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
14051         }
14052
14053       /* If there are more collisions, we can't handle it by reordering.
14054          Do an lea to the last part and use only one colliding move.  */
14055       else if (collisions > 1)
14056         {
14057           rtx base;
14058
14059           collisions = 1;
14060
14061           base = part[0][nparts - 1];
14062
14063           /* Handle the case when the last part isn't valid for lea.
14064              Happens in 64-bit mode storing the 12-byte XFmode.  */
14065           if (GET_MODE (base) != Pmode)
14066             base = gen_rtx_REG (Pmode, REGNO (base));
14067
14068           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
14069           part[1][0] = replace_equiv_address (part[1][0], base);
14070           part[1][1] = replace_equiv_address (part[1][1],
14071                                       plus_constant (base, UNITS_PER_WORD));
14072           if (nparts == 3)
14073             part[1][2] = replace_equiv_address (part[1][2],
14074                                       plus_constant (base, 8));
14075         }
14076     }
14077
14078   if (push)
14079     {
14080       if (!TARGET_64BIT)
14081         {
14082           if (nparts == 3)
14083             {
14084               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
14085                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
14086               emit_move_insn (part[0][2], part[1][2]);
14087             }
14088         }
14089       else
14090         {
14091           /* In 64bit mode we don't have 32bit push available.  In case this is
14092              register, it is OK - we will just use larger counterpart.  We also
14093              retype memory - these comes from attempt to avoid REX prefix on
14094              moving of second half of TFmode value.  */
14095           if (GET_MODE (part[1][1]) == SImode)
14096             {
14097               switch (GET_CODE (part[1][1]))
14098                 {
14099                 case MEM:
14100                   part[1][1] = adjust_address (part[1][1], DImode, 0);
14101                   break;
14102
14103                 case REG:
14104                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
14105                   break;
14106
14107                 default:
14108                   gcc_unreachable ();
14109                 }
14110
14111               if (GET_MODE (part[1][0]) == SImode)
14112                 part[1][0] = part[1][1];
14113             }
14114         }
14115       emit_move_insn (part[0][1], part[1][1]);
14116       emit_move_insn (part[0][0], part[1][0]);
14117       return;
14118     }
14119
14120   /* Choose correct order to not overwrite the source before it is copied.  */
14121   if ((REG_P (part[0][0])
14122        && REG_P (part[1][1])
14123        && (REGNO (part[0][0]) == REGNO (part[1][1])
14124            || (nparts == 3
14125                && REGNO (part[0][0]) == REGNO (part[1][2]))))
14126       || (collisions > 0
14127           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
14128     {
14129       if (nparts == 3)
14130         {
14131           operands[2] = part[0][2];
14132           operands[3] = part[0][1];
14133           operands[4] = part[0][0];
14134           operands[5] = part[1][2];
14135           operands[6] = part[1][1];
14136           operands[7] = part[1][0];
14137         }
14138       else
14139         {
14140           operands[2] = part[0][1];
14141           operands[3] = part[0][0];
14142           operands[5] = part[1][1];
14143           operands[6] = part[1][0];
14144         }
14145     }
14146   else
14147     {
14148       if (nparts == 3)
14149         {
14150           operands[2] = part[0][0];
14151           operands[3] = part[0][1];
14152           operands[4] = part[0][2];
14153           operands[5] = part[1][0];
14154           operands[6] = part[1][1];
14155           operands[7] = part[1][2];
14156         }
14157       else
14158         {
14159           operands[2] = part[0][0];
14160           operands[3] = part[0][1];
14161           operands[5] = part[1][0];
14162           operands[6] = part[1][1];
14163         }
14164     }
14165
14166   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
14167   if (optimize_size)
14168     {
14169       if (CONST_INT_P (operands[5])
14170           && operands[5] != const0_rtx
14171           && REG_P (operands[2]))
14172         {
14173           if (CONST_INT_P (operands[6])
14174               && INTVAL (operands[6]) == INTVAL (operands[5]))
14175             operands[6] = operands[2];
14176
14177           if (nparts == 3
14178               && CONST_INT_P (operands[7])
14179               && INTVAL (operands[7]) == INTVAL (operands[5]))
14180             operands[7] = operands[2];
14181         }
14182
14183       if (nparts == 3
14184           && CONST_INT_P (operands[6])
14185           && operands[6] != const0_rtx
14186           && REG_P (operands[3])
14187           && CONST_INT_P (operands[7])
14188           && INTVAL (operands[7]) == INTVAL (operands[6]))
14189         operands[7] = operands[3];
14190     }
14191
14192   emit_move_insn (operands[2], operands[5]);
14193   emit_move_insn (operands[3], operands[6]);
14194   if (nparts == 3)
14195     emit_move_insn (operands[4], operands[7]);
14196
14197   return;
14198 }
14199
14200 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
14201    left shift by a constant, either using a single shift or
14202    a sequence of add instructions.  */
14203
14204 static void
14205 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
14206 {
14207   if (count == 1)
14208     {
14209       emit_insn ((mode == DImode
14210                   ? gen_addsi3
14211                   : gen_adddi3) (operand, operand, operand));
14212     }
14213   else if (!optimize_size
14214            && count * ix86_cost->add <= ix86_cost->shift_const)
14215     {
14216       int i;
14217       for (i=0; i<count; i++)
14218         {
14219           emit_insn ((mode == DImode
14220                       ? gen_addsi3
14221                       : gen_adddi3) (operand, operand, operand));
14222         }
14223     }
14224   else
14225     emit_insn ((mode == DImode
14226                 ? gen_ashlsi3
14227                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
14228 }
14229
14230 void
14231 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
14232 {
14233   rtx low[2], high[2];
14234   int count;
14235   const int single_width = mode == DImode ? 32 : 64;
14236
14237   if (CONST_INT_P (operands[2]))
14238     {
14239       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
14240       count = INTVAL (operands[2]) & (single_width * 2 - 1);
14241
14242       if (count >= single_width)
14243         {
14244           emit_move_insn (high[0], low[1]);
14245           emit_move_insn (low[0], const0_rtx);
14246
14247           if (count > single_width)
14248             ix86_expand_ashl_const (high[0], count - single_width, mode);
14249         }
14250       else
14251         {
14252           if (!rtx_equal_p (operands[0], operands[1]))
14253             emit_move_insn (operands[0], operands[1]);
14254           emit_insn ((mode == DImode
14255                      ? gen_x86_shld_1
14256                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
14257           ix86_expand_ashl_const (low[0], count, mode);
14258         }
14259       return;
14260     }
14261
14262   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
14263
14264   if (operands[1] == const1_rtx)
14265     {
14266       /* Assuming we've chosen a QImode capable registers, then 1 << N
14267          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
14268       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
14269         {
14270           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
14271
14272           ix86_expand_clear (low[0]);
14273           ix86_expand_clear (high[0]);
14274           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
14275
14276           d = gen_lowpart (QImode, low[0]);
14277           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
14278           s = gen_rtx_EQ (QImode, flags, const0_rtx);
14279           emit_insn (gen_rtx_SET (VOIDmode, d, s));
14280
14281           d = gen_lowpart (QImode, high[0]);
14282           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
14283           s = gen_rtx_NE (QImode, flags, const0_rtx);
14284           emit_insn (gen_rtx_SET (VOIDmode, d, s));
14285         }
14286
14287       /* Otherwise, we can get the same results by manually performing
14288          a bit extract operation on bit 5/6, and then performing the two
14289          shifts.  The two methods of getting 0/1 into low/high are exactly
14290          the same size.  Avoiding the shift in the bit extract case helps
14291          pentium4 a bit; no one else seems to care much either way.  */
14292       else
14293         {
14294           rtx x;
14295
14296           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
14297             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
14298           else
14299             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
14300           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
14301
14302           emit_insn ((mode == DImode
14303                       ? gen_lshrsi3
14304                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
14305           emit_insn ((mode == DImode
14306                       ? gen_andsi3
14307                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
14308           emit_move_insn (low[0], high[0]);
14309           emit_insn ((mode == DImode
14310                       ? gen_xorsi3
14311                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
14312         }
14313
14314       emit_insn ((mode == DImode
14315                     ? gen_ashlsi3
14316                     : gen_ashldi3) (low[0], low[0], operands[2]));
14317       emit_insn ((mode == DImode
14318                     ? gen_ashlsi3
14319                     : gen_ashldi3) (high[0], high[0], operands[2]));
14320       return;
14321     }
14322
14323   if (operands[1] == constm1_rtx)
14324     {
14325       /* For -1 << N, we can avoid the shld instruction, because we
14326          know that we're shifting 0...31/63 ones into a -1.  */
14327       emit_move_insn (low[0], constm1_rtx);
14328       if (optimize_size)
14329         emit_move_insn (high[0], low[0]);
14330       else
14331         emit_move_insn (high[0], constm1_rtx);
14332     }
14333   else
14334     {
14335       if (!rtx_equal_p (operands[0], operands[1]))
14336         emit_move_insn (operands[0], operands[1]);
14337
14338       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
14339       emit_insn ((mode == DImode
14340                   ? gen_x86_shld_1
14341                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
14342     }
14343
14344   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
14345
14346   if (TARGET_CMOVE && scratch)
14347     {
14348       ix86_expand_clear (scratch);
14349       emit_insn ((mode == DImode
14350                   ? gen_x86_shift_adj_1
14351                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
14352     }
14353   else
14354     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
14355 }
14356
14357 void
14358 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
14359 {
14360   rtx low[2], high[2];
14361   int count;
14362   const int single_width = mode == DImode ? 32 : 64;
14363
14364   if (CONST_INT_P (operands[2]))
14365     {
14366       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
14367       count = INTVAL (operands[2]) & (single_width * 2 - 1);
14368
14369       if (count == single_width * 2 - 1)
14370         {
14371           emit_move_insn (high[0], high[1]);
14372           emit_insn ((mode == DImode
14373                       ? gen_ashrsi3
14374                       : gen_ashrdi3) (high[0], high[0],
14375                                       GEN_INT (single_width - 1)));
14376           emit_move_insn (low[0], high[0]);
14377
14378         }
14379       else if (count >= single_width)
14380         {
14381           emit_move_insn (low[0], high[1]);
14382           emit_move_insn (high[0], low[0]);
14383           emit_insn ((mode == DImode
14384                       ? gen_ashrsi3
14385                       : gen_ashrdi3) (high[0], high[0],
14386                                       GEN_INT (single_width - 1)));
14387           if (count > single_width)
14388             emit_insn ((mode == DImode
14389                         ? gen_ashrsi3
14390                         : gen_ashrdi3) (low[0], low[0],
14391                                         GEN_INT (count - single_width)));
14392         }
14393       else
14394         {
14395           if (!rtx_equal_p (operands[0], operands[1]))
14396             emit_move_insn (operands[0], operands[1]);
14397           emit_insn ((mode == DImode
14398                       ? gen_x86_shrd_1
14399                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
14400           emit_insn ((mode == DImode
14401                       ? gen_ashrsi3
14402                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
14403         }
14404     }
14405   else
14406     {
14407       if (!rtx_equal_p (operands[0], operands[1]))
14408         emit_move_insn (operands[0], operands[1]);
14409
14410       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
14411
14412       emit_insn ((mode == DImode
14413                   ? gen_x86_shrd_1
14414                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
14415       emit_insn ((mode == DImode
14416                   ? gen_ashrsi3
14417                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
14418
14419       if (TARGET_CMOVE && scratch)
14420         {
14421           emit_move_insn (scratch, high[0]);
14422           emit_insn ((mode == DImode
14423                       ? gen_ashrsi3
14424                       : gen_ashrdi3) (scratch, scratch,
14425                                       GEN_INT (single_width - 1)));
14426           emit_insn ((mode == DImode
14427                       ? gen_x86_shift_adj_1
14428                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
14429                                          scratch));
14430         }
14431       else
14432         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
14433     }
14434 }
14435
14436 void
14437 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
14438 {
14439   rtx low[2], high[2];
14440   int count;
14441   const int single_width = mode == DImode ? 32 : 64;
14442
14443   if (CONST_INT_P (operands[2]))
14444     {
14445       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
14446       count = INTVAL (operands[2]) & (single_width * 2 - 1);
14447
14448       if (count >= single_width)
14449         {
14450           emit_move_insn (low[0], high[1]);
14451           ix86_expand_clear (high[0]);
14452
14453           if (count > single_width)
14454             emit_insn ((mode == DImode
14455                         ? gen_lshrsi3
14456                         : gen_lshrdi3) (low[0], low[0],
14457                                         GEN_INT (count - single_width)));
14458         }
14459       else
14460         {
14461           if (!rtx_equal_p (operands[0], operands[1]))
14462             emit_move_insn (operands[0], operands[1]);
14463           emit_insn ((mode == DImode
14464                       ? gen_x86_shrd_1
14465                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
14466           emit_insn ((mode == DImode
14467                       ? gen_lshrsi3
14468                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
14469         }
14470     }
14471   else
14472     {
14473       if (!rtx_equal_p (operands[0], operands[1]))
14474         emit_move_insn (operands[0], operands[1]);
14475
14476       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
14477
14478       emit_insn ((mode == DImode
14479                   ? gen_x86_shrd_1
14480                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
14481       emit_insn ((mode == DImode
14482                   ? gen_lshrsi3
14483                   : gen_lshrdi3) (high[0], high[0], operands[2]));
14484
14485       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
14486       if (TARGET_CMOVE && scratch)
14487         {
14488           ix86_expand_clear (scratch);
14489           emit_insn ((mode == DImode
14490                       ? gen_x86_shift_adj_1
14491                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
14492                                                scratch));
14493         }
14494       else
14495         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
14496     }
14497 }
14498
14499 /* Predict just emitted jump instruction to be taken with probability PROB.  */
14500 static void
14501 predict_jump (int prob)
14502 {
14503   rtx insn = get_last_insn ();
14504   gcc_assert (JUMP_P (insn));
14505   REG_NOTES (insn)
14506     = gen_rtx_EXPR_LIST (REG_BR_PROB,
14507                          GEN_INT (prob),
14508                          REG_NOTES (insn));
14509 }
14510
14511 /* Helper function for the string operations below.  Dest VARIABLE whether
14512    it is aligned to VALUE bytes.  If true, jump to the label.  */
14513 static rtx
14514 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
14515 {
14516   rtx label = gen_label_rtx ();
14517   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
14518   if (GET_MODE (variable) == DImode)
14519     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
14520   else
14521     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
14522   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
14523                            1, label);
14524   if (epilogue)
14525     predict_jump (REG_BR_PROB_BASE * 50 / 100);
14526   else
14527     predict_jump (REG_BR_PROB_BASE * 90 / 100);
14528   return label;
14529 }
14530
14531 /* Adjust COUNTER by the VALUE.  */
14532 static void
14533 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
14534 {
14535   if (GET_MODE (countreg) == DImode)
14536     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
14537   else
14538     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
14539 }
14540
14541 /* Zero extend possibly SImode EXP to Pmode register.  */
14542 rtx
14543 ix86_zero_extend_to_Pmode (rtx exp)
14544 {
14545   rtx r;
14546   if (GET_MODE (exp) == VOIDmode)
14547     return force_reg (Pmode, exp);
14548   if (GET_MODE (exp) == Pmode)
14549     return copy_to_mode_reg (Pmode, exp);
14550   r = gen_reg_rtx (Pmode);
14551   emit_insn (gen_zero_extendsidi2 (r, exp));
14552   return r;
14553 }
14554
14555 /* Divide COUNTREG by SCALE.  */
14556 static rtx
14557 scale_counter (rtx countreg, int scale)
14558 {
14559   rtx sc;
14560   rtx piece_size_mask;
14561
14562   if (scale == 1)
14563     return countreg;
14564   if (CONST_INT_P (countreg))
14565     return GEN_INT (INTVAL (countreg) / scale);
14566   gcc_assert (REG_P (countreg));
14567
14568   piece_size_mask = GEN_INT (scale - 1);
14569   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
14570                             GEN_INT (exact_log2 (scale)),
14571                             NULL, 1, OPTAB_DIRECT);
14572   return sc;
14573 }
14574
14575 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
14576    DImode for constant loop counts.  */
14577
14578 static enum machine_mode
14579 counter_mode (rtx count_exp)
14580 {
14581   if (GET_MODE (count_exp) != VOIDmode)
14582     return GET_MODE (count_exp);
14583   if (GET_CODE (count_exp) != CONST_INT)
14584     return Pmode;
14585   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
14586     return DImode;
14587   return SImode;
14588 }
14589
14590 /* When SRCPTR is non-NULL, output simple loop to move memory
14591    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
14592    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
14593    equivalent loop to set memory by VALUE (supposed to be in MODE).
14594
14595    The size is rounded down to whole number of chunk size moved at once.
14596    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
14597
14598
14599 static void
14600 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
14601                                rtx destptr, rtx srcptr, rtx value,
14602                                rtx count, enum machine_mode mode, int unroll,
14603                                int expected_size)
14604 {
14605   rtx out_label, top_label, iter, tmp;
14606   enum machine_mode iter_mode = counter_mode (count);
14607   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
14608   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
14609   rtx size;
14610   rtx x_addr;
14611   rtx y_addr;
14612   int i;
14613
14614   top_label = gen_label_rtx ();
14615   out_label = gen_label_rtx ();
14616   iter = gen_reg_rtx (iter_mode);
14617
14618   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
14619                               NULL, 1, OPTAB_DIRECT);
14620   /* Those two should combine.  */
14621   if (piece_size == const1_rtx)
14622     {
14623       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
14624                                true, out_label);
14625       predict_jump (REG_BR_PROB_BASE * 10 / 100);
14626     }
14627   emit_move_insn (iter, const0_rtx);
14628
14629   emit_label (top_label);
14630
14631   tmp = convert_modes (Pmode, iter_mode, iter, true);
14632   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
14633   destmem = change_address (destmem, mode, x_addr);
14634
14635   if (srcmem)
14636     {
14637       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
14638       srcmem = change_address (srcmem, mode, y_addr);
14639
14640       /* When unrolling for chips that reorder memory reads and writes,
14641          we can save registers by using single temporary.
14642          Also using 4 temporaries is overkill in 32bit mode.  */
14643       if (!TARGET_64BIT && 0)
14644         {
14645           for (i = 0; i < unroll; i++)
14646             {
14647               if (i)
14648                 {
14649                   destmem =
14650                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
14651                   srcmem =
14652                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
14653                 }
14654               emit_move_insn (destmem, srcmem);
14655             }
14656         }
14657       else
14658         {
14659           rtx tmpreg[4];
14660           gcc_assert (unroll <= 4);
14661           for (i = 0; i < unroll; i++)
14662             {
14663               tmpreg[i] = gen_reg_rtx (mode);
14664               if (i)
14665                 {
14666                   srcmem =
14667                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
14668                 }
14669               emit_move_insn (tmpreg[i], srcmem);
14670             }
14671           for (i = 0; i < unroll; i++)
14672             {
14673               if (i)
14674                 {
14675                   destmem =
14676                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
14677                 }
14678               emit_move_insn (destmem, tmpreg[i]);
14679             }
14680         }
14681     }
14682   else
14683     for (i = 0; i < unroll; i++)
14684       {
14685         if (i)
14686           destmem =
14687             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
14688         emit_move_insn (destmem, value);
14689       }
14690
14691   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
14692                              true, OPTAB_LIB_WIDEN);
14693   if (tmp != iter)
14694     emit_move_insn (iter, tmp);
14695
14696   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
14697                            true, top_label);
14698   if (expected_size != -1)
14699     {
14700       expected_size /= GET_MODE_SIZE (mode) * unroll;
14701       if (expected_size == 0)
14702         predict_jump (0);
14703       else if (expected_size > REG_BR_PROB_BASE)
14704         predict_jump (REG_BR_PROB_BASE - 1);
14705       else
14706         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
14707     }
14708   else
14709     predict_jump (REG_BR_PROB_BASE * 80 / 100);
14710   iter = ix86_zero_extend_to_Pmode (iter);
14711   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
14712                              true, OPTAB_LIB_WIDEN);
14713   if (tmp != destptr)
14714     emit_move_insn (destptr, tmp);
14715   if (srcptr)
14716     {
14717       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
14718                                  true, OPTAB_LIB_WIDEN);
14719       if (tmp != srcptr)
14720         emit_move_insn (srcptr, tmp);
14721     }
14722   emit_label (out_label);
14723 }
14724
14725 /* Output "rep; mov" instruction.
14726    Arguments have same meaning as for previous function */
14727 static void
14728 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
14729                            rtx destptr, rtx srcptr,
14730                            rtx count,
14731                            enum machine_mode mode)
14732 {
14733   rtx destexp;
14734   rtx srcexp;
14735   rtx countreg;
14736
14737   /* If the size is known, it is shorter to use rep movs.  */
14738   if (mode == QImode && CONST_INT_P (count)
14739       && !(INTVAL (count) & 3))
14740     mode = SImode;
14741
14742   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
14743     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
14744   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
14745     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
14746   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
14747   if (mode != QImode)
14748     {
14749       destexp = gen_rtx_ASHIFT (Pmode, countreg,
14750                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
14751       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
14752       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
14753                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
14754       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
14755     }
14756   else
14757     {
14758       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
14759       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
14760     }
14761   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
14762                           destexp, srcexp));
14763 }
14764
14765 /* Output "rep; stos" instruction.
14766    Arguments have same meaning as for previous function */
14767 static void
14768 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
14769                             rtx count,
14770                             enum machine_mode mode)
14771 {
14772   rtx destexp;
14773   rtx countreg;
14774
14775   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
14776     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
14777   value = force_reg (mode, gen_lowpart (mode, value));
14778   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
14779   if (mode != QImode)
14780     {
14781       destexp = gen_rtx_ASHIFT (Pmode, countreg,
14782                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
14783       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
14784     }
14785   else
14786     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
14787   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
14788 }
14789
14790 static void
14791 emit_strmov (rtx destmem, rtx srcmem,
14792              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
14793 {
14794   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
14795   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
14796   emit_insn (gen_strmov (destptr, dest, srcptr, src));
14797 }
14798
14799 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
14800 static void
14801 expand_movmem_epilogue (rtx destmem, rtx srcmem,
14802                         rtx destptr, rtx srcptr, rtx count, int max_size)
14803 {
14804   rtx src, dest;
14805   if (CONST_INT_P (count))
14806     {
14807       HOST_WIDE_INT countval = INTVAL (count);
14808       int offset = 0;
14809
14810       if ((countval & 0x10) && max_size > 16)
14811         {
14812           if (TARGET_64BIT)
14813             {
14814               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
14815               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
14816             }
14817           else
14818             gcc_unreachable ();
14819           offset += 16;
14820         }
14821       if ((countval & 0x08) && max_size > 8)
14822         {
14823           if (TARGET_64BIT)
14824             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
14825           else
14826             {
14827               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
14828               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
14829             }
14830           offset += 8;
14831         }
14832       if ((countval & 0x04) && max_size > 4)
14833         {
14834           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
14835           offset += 4;
14836         }
14837       if ((countval & 0x02) && max_size > 2)
14838         {
14839           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
14840           offset += 2;
14841         }
14842       if ((countval & 0x01) && max_size > 1)
14843         {
14844           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
14845           offset += 1;
14846         }
14847       return;
14848     }
14849   if (max_size > 8)
14850     {
14851       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
14852                                     count, 1, OPTAB_DIRECT);
14853       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
14854                                      count, QImode, 1, 4);
14855       return;
14856     }
14857
14858   /* When there are stringops, we can cheaply increase dest and src pointers.
14859      Otherwise we save code size by maintaining offset (zero is readily
14860      available from preceding rep operation) and using x86 addressing modes.
14861    */
14862   if (TARGET_SINGLE_STRINGOP)
14863     {
14864       if (max_size > 4)
14865         {
14866           rtx label = ix86_expand_aligntest (count, 4, true);
14867           src = change_address (srcmem, SImode, srcptr);
14868           dest = change_address (destmem, SImode, destptr);
14869           emit_insn (gen_strmov (destptr, dest, srcptr, src));
14870           emit_label (label);
14871           LABEL_NUSES (label) = 1;
14872         }
14873       if (max_size > 2)
14874         {
14875           rtx label = ix86_expand_aligntest (count, 2, true);
14876           src = change_address (srcmem, HImode, srcptr);
14877           dest = change_address (destmem, HImode, destptr);
14878           emit_insn (gen_strmov (destptr, dest, srcptr, src));
14879           emit_label (label);
14880           LABEL_NUSES (label) = 1;
14881         }
14882       if (max_size > 1)
14883         {
14884           rtx label = ix86_expand_aligntest (count, 1, true);
14885           src = change_address (srcmem, QImode, srcptr);
14886           dest = change_address (destmem, QImode, destptr);
14887           emit_insn (gen_strmov (destptr, dest, srcptr, src));
14888           emit_label (label);
14889           LABEL_NUSES (label) = 1;
14890         }
14891     }
14892   else
14893     {
14894       rtx offset = force_reg (Pmode, const0_rtx);
14895       rtx tmp;
14896
14897       if (max_size > 4)
14898         {
14899           rtx label = ix86_expand_aligntest (count, 4, true);
14900           src = change_address (srcmem, SImode, srcptr);
14901           dest = change_address (destmem, SImode, destptr);
14902           emit_move_insn (dest, src);
14903           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
14904                                      true, OPTAB_LIB_WIDEN);
14905           if (tmp != offset)
14906             emit_move_insn (offset, tmp);
14907           emit_label (label);
14908           LABEL_NUSES (label) = 1;
14909         }
14910       if (max_size > 2)
14911         {
14912           rtx label = ix86_expand_aligntest (count, 2, true);
14913           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
14914           src = change_address (srcmem, HImode, tmp);
14915           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
14916           dest = change_address (destmem, HImode, tmp);
14917           emit_move_insn (dest, src);
14918           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
14919                                      true, OPTAB_LIB_WIDEN);
14920           if (tmp != offset)
14921             emit_move_insn (offset, tmp);
14922           emit_label (label);
14923           LABEL_NUSES (label) = 1;
14924         }
14925       if (max_size > 1)
14926         {
14927           rtx label = ix86_expand_aligntest (count, 1, true);
14928           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
14929           src = change_address (srcmem, QImode, tmp);
14930           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
14931           dest = change_address (destmem, QImode, tmp);
14932           emit_move_insn (dest, src);
14933           emit_label (label);
14934           LABEL_NUSES (label) = 1;
14935         }
14936     }
14937 }
14938
14939 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
14940 static void
14941 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
14942                                  rtx count, int max_size)
14943 {
14944   count =
14945     expand_simple_binop (counter_mode (count), AND, count,
14946                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
14947   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
14948                                  gen_lowpart (QImode, value), count, QImode,
14949                                  1, max_size / 2);
14950 }
14951
14952 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
14953 static void
14954 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
14955 {
14956   rtx dest;
14957
14958   if (CONST_INT_P (count))
14959     {
14960       HOST_WIDE_INT countval = INTVAL (count);
14961       int offset = 0;
14962
14963       if ((countval & 0x10) && max_size > 16)
14964         {
14965           if (TARGET_64BIT)
14966             {
14967               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
14968               emit_insn (gen_strset (destptr, dest, value));
14969               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
14970               emit_insn (gen_strset (destptr, dest, value));
14971             }
14972           else
14973             gcc_unreachable ();
14974           offset += 16;
14975         }
14976       if ((countval & 0x08) && max_size > 8)
14977         {
14978           if (TARGET_64BIT)
14979             {
14980               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
14981               emit_insn (gen_strset (destptr, dest, value));
14982             }
14983           else
14984             {
14985               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
14986               emit_insn (gen_strset (destptr, dest, value));
14987               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
14988               emit_insn (gen_strset (destptr, dest, value));
14989             }
14990           offset += 8;
14991         }
14992       if ((countval & 0x04) && max_size > 4)
14993         {
14994           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
14995           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
14996           offset += 4;
14997         }
14998       if ((countval & 0x02) && max_size > 2)
14999         {
15000           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
15001           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
15002           offset += 2;
15003         }
15004       if ((countval & 0x01) && max_size > 1)
15005         {
15006           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
15007           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
15008           offset += 1;
15009         }
15010       return;
15011     }
15012   if (max_size > 32)
15013     {
15014       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
15015       return;
15016     }
15017   if (max_size > 16)
15018     {
15019       rtx label = ix86_expand_aligntest (count, 16, true);
15020       if (TARGET_64BIT)
15021         {
15022           dest = change_address (destmem, DImode, destptr);
15023           emit_insn (gen_strset (destptr, dest, value));
15024           emit_insn (gen_strset (destptr, dest, value));
15025         }
15026       else
15027         {
15028           dest = change_address (destmem, SImode, destptr);
15029           emit_insn (gen_strset (destptr, dest, value));
15030           emit_insn (gen_strset (destptr, dest, value));
15031           emit_insn (gen_strset (destptr, dest, value));
15032           emit_insn (gen_strset (destptr, dest, value));
15033         }
15034       emit_label (label);
15035       LABEL_NUSES (label) = 1;
15036     }
15037   if (max_size > 8)
15038     {
15039       rtx label = ix86_expand_aligntest (count, 8, true);
15040       if (TARGET_64BIT)
15041         {
15042           dest = change_address (destmem, DImode, destptr);
15043           emit_insn (gen_strset (destptr, dest, value));
15044         }
15045       else
15046         {
15047           dest = change_address (destmem, SImode, destptr);
15048           emit_insn (gen_strset (destptr, dest, value));
15049           emit_insn (gen_strset (destptr, dest, value));
15050         }
15051       emit_label (label);
15052       LABEL_NUSES (label) = 1;
15053     }
15054   if (max_size > 4)
15055     {
15056       rtx label = ix86_expand_aligntest (count, 4, true);
15057       dest = change_address (destmem, SImode, destptr);
15058       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
15059       emit_label (label);
15060       LABEL_NUSES (label) = 1;
15061     }
15062   if (max_size > 2)
15063     {
15064       rtx label = ix86_expand_aligntest (count, 2, true);
15065       dest = change_address (destmem, HImode, destptr);
15066       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
15067       emit_label (label);
15068       LABEL_NUSES (label) = 1;
15069     }
15070   if (max_size > 1)
15071     {
15072       rtx label = ix86_expand_aligntest (count, 1, true);
15073       dest = change_address (destmem, QImode, destptr);
15074       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
15075       emit_label (label);
15076       LABEL_NUSES (label) = 1;
15077     }
15078 }
15079
15080 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
15081    DESIRED_ALIGNMENT.  */
15082 static void
15083 expand_movmem_prologue (rtx destmem, rtx srcmem,
15084                         rtx destptr, rtx srcptr, rtx count,
15085                         int align, int desired_alignment)
15086 {
15087   if (align <= 1 && desired_alignment > 1)
15088     {
15089       rtx label = ix86_expand_aligntest (destptr, 1, false);
15090       srcmem = change_address (srcmem, QImode, srcptr);
15091       destmem = change_address (destmem, QImode, destptr);
15092       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
15093       ix86_adjust_counter (count, 1);
15094       emit_label (label);
15095       LABEL_NUSES (label) = 1;
15096     }
15097   if (align <= 2 && desired_alignment > 2)
15098     {
15099       rtx label = ix86_expand_aligntest (destptr, 2, false);
15100       srcmem = change_address (srcmem, HImode, srcptr);
15101       destmem = change_address (destmem, HImode, destptr);
15102       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
15103       ix86_adjust_counter (count, 2);
15104       emit_label (label);
15105       LABEL_NUSES (label) = 1;
15106     }
15107   if (align <= 4 && desired_alignment > 4)
15108     {
15109       rtx label = ix86_expand_aligntest (destptr, 4, false);
15110       srcmem = change_address (srcmem, SImode, srcptr);
15111       destmem = change_address (destmem, SImode, destptr);
15112       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
15113       ix86_adjust_counter (count, 4);
15114       emit_label (label);
15115       LABEL_NUSES (label) = 1;
15116     }
15117   gcc_assert (desired_alignment <= 8);
15118 }
15119
15120 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
15121    DESIRED_ALIGNMENT.  */
15122 static void
15123 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
15124                         int align, int desired_alignment)
15125 {
15126   if (align <= 1 && desired_alignment > 1)
15127     {
15128       rtx label = ix86_expand_aligntest (destptr, 1, false);
15129       destmem = change_address (destmem, QImode, destptr);
15130       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
15131       ix86_adjust_counter (count, 1);
15132       emit_label (label);
15133       LABEL_NUSES (label) = 1;
15134     }
15135   if (align <= 2 && desired_alignment > 2)
15136     {
15137       rtx label = ix86_expand_aligntest (destptr, 2, false);
15138       destmem = change_address (destmem, HImode, destptr);
15139       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
15140       ix86_adjust_counter (count, 2);
15141       emit_label (label);
15142       LABEL_NUSES (label) = 1;
15143     }
15144   if (align <= 4 && desired_alignment > 4)
15145     {
15146       rtx label = ix86_expand_aligntest (destptr, 4, false);
15147       destmem = change_address (destmem, SImode, destptr);
15148       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
15149       ix86_adjust_counter (count, 4);
15150       emit_label (label);
15151       LABEL_NUSES (label) = 1;
15152     }
15153   gcc_assert (desired_alignment <= 8);
15154 }
15155
15156 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
15157 static enum stringop_alg
15158 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
15159             int *dynamic_check)
15160 {
15161   const struct stringop_algs * algs;
15162   /* Algorithms using the rep prefix want at least edi and ecx;
15163      additionally, memset wants eax and memcpy wants esi.  Don't
15164      consider such algorithms if the user has appropriated those
15165      registers for their own purposes.  */
15166   bool rep_prefix_usable = !(fixed_regs[CX_REG] || fixed_regs[DI_REG]
15167                              || (memset
15168                                  ? fixed_regs[AX_REG] : fixed_regs[SI_REG]));
15169
15170 #define ALG_USABLE_P(alg) (rep_prefix_usable                    \
15171                            || (alg != rep_prefix_1_byte         \
15172                                && alg != rep_prefix_4_byte      \
15173                                && alg != rep_prefix_8_byte))
15174
15175   *dynamic_check = -1;
15176   if (memset)
15177     algs = &ix86_cost->memset[TARGET_64BIT != 0];
15178   else
15179     algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
15180   if (stringop_alg != no_stringop && ALG_USABLE_P (stringop_alg))
15181     return stringop_alg;
15182   /* rep; movq or rep; movl is the smallest variant.  */
15183   else if (optimize_size)
15184     {
15185       if (!count || (count & 3))
15186         return rep_prefix_usable ? rep_prefix_1_byte : loop_1_byte;
15187       else
15188         return rep_prefix_usable ? rep_prefix_4_byte : loop;
15189     }
15190   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
15191    */
15192   else if (expected_size != -1 && expected_size < 4)
15193     return loop_1_byte;
15194   else if (expected_size != -1)
15195     {
15196       unsigned int i;
15197       enum stringop_alg alg = libcall;
15198       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
15199         {
15200           /* We get here if the algorithms that were not libcall-based
15201              were rep-prefix based and we are unable to use rep prefixes
15202              based on global register usage.  Break out of the loop and
15203              use the heuristic below.  */
15204           if (algs->size[i].max == 0)
15205             break;
15206           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
15207             {
15208               enum stringop_alg candidate = algs->size[i].alg;
15209
15210               if (candidate != libcall && ALG_USABLE_P (candidate))
15211                 alg = candidate;
15212               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
15213                  last non-libcall inline algorithm.  */
15214               if (TARGET_INLINE_ALL_STRINGOPS)
15215                 {
15216                   /* When the current size is best to be copied by a libcall,
15217                      but we are still forced to inline, run the heuristic below
15218                      that will pick code for medium sized blocks.  */
15219                   if (alg != libcall)
15220                     return alg;
15221                   break;
15222                 }
15223               else if (ALG_USABLE_P (candidate))
15224                 return candidate;
15225             }
15226         }
15227       gcc_assert (TARGET_INLINE_ALL_STRINGOPS || !rep_prefix_usable);
15228     }
15229   /* When asked to inline the call anyway, try to pick meaningful choice.
15230      We look for maximal size of block that is faster to copy by hand and
15231      take blocks of at most of that size guessing that average size will
15232      be roughly half of the block.
15233
15234      If this turns out to be bad, we might simply specify the preferred
15235      choice in ix86_costs.  */
15236   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
15237       && (algs->unknown_size == libcall || !ALG_USABLE_P (algs->unknown_size)))
15238     {
15239       int max = -1;
15240       enum stringop_alg alg;
15241       int i;
15242       bool any_alg_usable_p = true;
15243
15244       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
15245         {
15246           enum stringop_alg candidate = algs->size[i].alg;
15247           any_alg_usable_p = any_alg_usable_p && ALG_USABLE_P (candidate);
15248
15249           if (candidate != libcall && candidate
15250               && ALG_USABLE_P (candidate))
15251               max = algs->size[i].max;
15252         }
15253       /* If there aren't any usable algorithms, then recursing on
15254          smaller sizes isn't going to find anything.  Just return the
15255          simple byte-at-a-time copy loop.  */
15256       if (!any_alg_usable_p)
15257         {
15258           /* Pick something reasonable.  */
15259           if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
15260             *dynamic_check = 128;
15261           return loop_1_byte;
15262         }
15263       if (max == -1)
15264         max = 4096;
15265       alg = decide_alg (count, max / 2, memset, dynamic_check);
15266       gcc_assert (*dynamic_check == -1);
15267       gcc_assert (alg != libcall);
15268       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
15269         *dynamic_check = max;
15270       return alg;
15271     }
15272   return ALG_USABLE_P (algs->unknown_size) ? algs->unknown_size : libcall;
15273 #undef ALG_USABLE_P
15274 }
15275
15276 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
15277    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
15278 static int
15279 decide_alignment (int align,
15280                   enum stringop_alg alg,
15281                   int expected_size)
15282 {
15283   int desired_align = 0;
15284   switch (alg)
15285     {
15286       case no_stringop:
15287         gcc_unreachable ();
15288       case loop:
15289       case unrolled_loop:
15290         desired_align = GET_MODE_SIZE (Pmode);
15291         break;
15292       case rep_prefix_8_byte:
15293         desired_align = 8;
15294         break;
15295       case rep_prefix_4_byte:
15296         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
15297            copying whole cacheline at once.  */
15298         if (TARGET_PENTIUMPRO)
15299           desired_align = 8;
15300         else
15301           desired_align = 4;
15302         break;
15303       case rep_prefix_1_byte:
15304         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
15305            copying whole cacheline at once.  */
15306         if (TARGET_PENTIUMPRO)
15307           desired_align = 8;
15308         else
15309           desired_align = 1;
15310         break;
15311       case loop_1_byte:
15312         desired_align = 1;
15313         break;
15314       case libcall:
15315         return 0;
15316     }
15317
15318   if (optimize_size)
15319     desired_align = 1;
15320   if (desired_align < align)
15321     desired_align = align;
15322   if (expected_size != -1 && expected_size < 4)
15323     desired_align = align;
15324   return desired_align;
15325 }
15326
15327 /* Return the smallest power of 2 greater than VAL.  */
15328 static int
15329 smallest_pow2_greater_than (int val)
15330 {
15331   int ret = 1;
15332   while (ret <= val)
15333     ret <<= 1;
15334   return ret;
15335 }
15336
15337 /* Expand string move (memcpy) operation.  Use i386 string operations when
15338    profitable.  expand_setmem contains similar code.  The code depends upon
15339    architecture, block size and alignment, but always has the same
15340    overall structure:
15341
15342    1) Prologue guard: Conditional that jumps up to epilogues for small
15343       blocks that can be handled by epilogue alone.  This is faster but
15344       also needed for correctness, since prologue assume the block is larger
15345       than the desired alignment.
15346
15347       Optional dynamic check for size and libcall for large
15348       blocks is emitted here too, with -minline-stringops-dynamically.
15349
15350    2) Prologue: copy first few bytes in order to get destination aligned
15351       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
15352       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
15353       We emit either a jump tree on power of two sized blocks, or a byte loop.
15354
15355    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
15356       with specified algorithm.
15357
15358    4) Epilogue: code copying tail of the block that is too small to be
15359       handled by main body (or up to size guarded by prologue guard).  */
15360
15361 int
15362 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
15363                     rtx expected_align_exp, rtx expected_size_exp)
15364 {
15365   rtx destreg;
15366   rtx srcreg;
15367   rtx label = NULL;
15368   rtx tmp;
15369   rtx jump_around_label = NULL;
15370   HOST_WIDE_INT align = 1;
15371   unsigned HOST_WIDE_INT count = 0;
15372   HOST_WIDE_INT expected_size = -1;
15373   int size_needed = 0, epilogue_size_needed;
15374   int desired_align = 0;
15375   enum stringop_alg alg;
15376   int dynamic_check;
15377
15378   if (CONST_INT_P (align_exp))
15379     align = INTVAL (align_exp);
15380   /* i386 can do misaligned access on reasonably increased cost.  */
15381   if (CONST_INT_P (expected_align_exp)
15382       && INTVAL (expected_align_exp) > align)
15383     align = INTVAL (expected_align_exp);
15384   if (CONST_INT_P (count_exp))
15385     count = expected_size = INTVAL (count_exp);
15386   if (CONST_INT_P (expected_size_exp) && count == 0)
15387     expected_size = INTVAL (expected_size_exp);
15388
15389   /* Make sure we don't need to care about overflow later on.  */
15390   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
15391     return 0;
15392
15393   /* Step 0: Decide on preferred algorithm, desired alignment and
15394      size of chunks to be copied by main loop.  */
15395
15396   alg = decide_alg (count, expected_size, false, &dynamic_check);
15397   desired_align = decide_alignment (align, alg, expected_size);
15398
15399   if (!TARGET_ALIGN_STRINGOPS)
15400     align = desired_align;
15401
15402   if (alg == libcall)
15403     return 0;
15404   gcc_assert (alg != no_stringop);
15405   if (!count)
15406     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
15407   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
15408   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
15409   switch (alg)
15410     {
15411     case libcall:
15412     case no_stringop:
15413       gcc_unreachable ();
15414     case loop:
15415       size_needed = GET_MODE_SIZE (Pmode);
15416       break;
15417     case unrolled_loop:
15418       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
15419       break;
15420     case rep_prefix_8_byte:
15421       size_needed = 8;
15422       break;
15423     case rep_prefix_4_byte:
15424       size_needed = 4;
15425       break;
15426     case rep_prefix_1_byte:
15427     case loop_1_byte:
15428       size_needed = 1;
15429       break;
15430     }
15431
15432   epilogue_size_needed = size_needed;
15433
15434   /* Step 1: Prologue guard.  */
15435
15436   /* Alignment code needs count to be in register.  */
15437   if (CONST_INT_P (count_exp) && desired_align > align)
15438     count_exp = force_reg (counter_mode (count_exp), count_exp);
15439   gcc_assert (desired_align >= 1 && align >= 1);
15440
15441   /* Ensure that alignment prologue won't copy past end of block.  */
15442   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
15443     {
15444       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
15445       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
15446          Make sure it is power of 2.  */
15447       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
15448
15449       if (CONST_INT_P (count_exp))
15450         {
15451           if (UINTVAL (count_exp) < (unsigned HOST_WIDE_INT)epilogue_size_needed)
15452             goto epilogue;
15453         }
15454       else
15455         {
15456           label = gen_label_rtx ();
15457           emit_cmp_and_jump_insns (count_exp,
15458                                    GEN_INT (epilogue_size_needed),
15459                                    LTU, 0, counter_mode (count_exp), 1, label);
15460           if (expected_size == -1 || expected_size < epilogue_size_needed)
15461             predict_jump (REG_BR_PROB_BASE * 60 / 100);
15462           else
15463             predict_jump (REG_BR_PROB_BASE * 20 / 100);
15464         }
15465     }
15466
15467   /* Emit code to decide on runtime whether library call or inline should be
15468      used.  */
15469   if (dynamic_check != -1)
15470     {
15471       if (CONST_INT_P (count_exp))
15472         {
15473           if (UINTVAL (count_exp) >= (unsigned HOST_WIDE_INT)dynamic_check)
15474             {
15475               emit_block_move_via_libcall (dst, src, count_exp, false);
15476               count_exp = const0_rtx;
15477               goto epilogue;
15478             }
15479         }
15480       else
15481         {
15482           rtx hot_label = gen_label_rtx ();
15483           jump_around_label = gen_label_rtx ();
15484           emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
15485                                    LEU, 0, GET_MODE (count_exp), 1, hot_label);
15486           predict_jump (REG_BR_PROB_BASE * 90 / 100);
15487           emit_block_move_via_libcall (dst, src, count_exp, false);
15488           emit_jump (jump_around_label);
15489           emit_label (hot_label);
15490         }
15491     }
15492
15493   /* Step 2: Alignment prologue.  */
15494
15495   if (desired_align > align)
15496     {
15497       /* Except for the first move in epilogue, we no longer know
15498          constant offset in aliasing info.  It don't seems to worth
15499          the pain to maintain it for the first move, so throw away
15500          the info early.  */
15501       src = change_address (src, BLKmode, srcreg);
15502       dst = change_address (dst, BLKmode, destreg);
15503       expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
15504                               desired_align);
15505     }
15506   if (label && size_needed == 1)
15507     {
15508       emit_label (label);
15509       LABEL_NUSES (label) = 1;
15510       label = NULL;
15511     }
15512
15513   /* Step 3: Main loop.  */
15514
15515   switch (alg)
15516     {
15517     case libcall:
15518     case no_stringop:
15519       gcc_unreachable ();
15520     case loop_1_byte:
15521       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
15522                                      count_exp, QImode, 1, expected_size);
15523       break;
15524     case loop:
15525       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
15526                                      count_exp, Pmode, 1, expected_size);
15527       break;
15528     case unrolled_loop:
15529       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
15530          registers for 4 temporaries anyway.  */
15531       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
15532                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
15533                                      expected_size);
15534       break;
15535     case rep_prefix_8_byte:
15536       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
15537                                  DImode);
15538       break;
15539     case rep_prefix_4_byte:
15540       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
15541                                  SImode);
15542       break;
15543     case rep_prefix_1_byte:
15544       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
15545                                  QImode);
15546       break;
15547     }
15548   /* Adjust properly the offset of src and dest memory for aliasing.  */
15549   if (CONST_INT_P (count_exp))
15550     {
15551       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
15552                                           (count / size_needed) * size_needed);
15553       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
15554                                           (count / size_needed) * size_needed);
15555     }
15556   else
15557     {
15558       src = change_address (src, BLKmode, srcreg);
15559       dst = change_address (dst, BLKmode, destreg);
15560     }
15561
15562   /* Step 4: Epilogue to copy the remaining bytes.  */
15563  epilogue:
15564   if (label)
15565     {
15566       /* When the main loop is done, COUNT_EXP might hold original count,
15567          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
15568          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
15569          bytes. Compensate if needed.  */
15570
15571       if (size_needed < epilogue_size_needed)
15572         {
15573           tmp =
15574             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
15575                                  GEN_INT (size_needed - 1), count_exp, 1,
15576                                  OPTAB_DIRECT);
15577           if (tmp != count_exp)
15578             emit_move_insn (count_exp, tmp);
15579         }
15580       emit_label (label);
15581       LABEL_NUSES (label) = 1;
15582     }
15583
15584   if (count_exp != const0_rtx && epilogue_size_needed > 1)
15585     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
15586                             epilogue_size_needed);
15587   if (jump_around_label)
15588     emit_label (jump_around_label);
15589   return 1;
15590 }
15591
15592 /* Helper function for memcpy.  For QImode value 0xXY produce
15593    0xXYXYXYXY of wide specified by MODE.  This is essentially
15594    a * 0x10101010, but we can do slightly better than
15595    synth_mult by unwinding the sequence by hand on CPUs with
15596    slow multiply.  */
15597 static rtx
15598 promote_duplicated_reg (enum machine_mode mode, rtx val)
15599 {
15600   enum machine_mode valmode = GET_MODE (val);
15601   rtx tmp;
15602   int nops = mode == DImode ? 3 : 2;
15603
15604   gcc_assert (mode == SImode || mode == DImode);
15605   if (val == const0_rtx)
15606     return copy_to_mode_reg (mode, const0_rtx);
15607   if (CONST_INT_P (val))
15608     {
15609       HOST_WIDE_INT v = INTVAL (val) & 255;
15610
15611       v |= v << 8;
15612       v |= v << 16;
15613       if (mode == DImode)
15614         v |= (v << 16) << 16;
15615       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
15616     }
15617
15618   if (valmode == VOIDmode)
15619     valmode = QImode;
15620   if (valmode != QImode)
15621     val = gen_lowpart (QImode, val);
15622   if (mode == QImode)
15623     return val;
15624   if (!TARGET_PARTIAL_REG_STALL)
15625     nops--;
15626   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
15627       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
15628       <= (ix86_cost->shift_const + ix86_cost->add) * nops
15629           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
15630     {
15631       rtx reg = convert_modes (mode, QImode, val, true);
15632       tmp = promote_duplicated_reg (mode, const1_rtx);
15633       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
15634                                   OPTAB_DIRECT);
15635     }
15636   else
15637     {
15638       rtx reg = convert_modes (mode, QImode, val, true);
15639
15640       if (!TARGET_PARTIAL_REG_STALL)
15641         if (mode == SImode)
15642           emit_insn (gen_movsi_insv_1 (reg, reg));
15643         else
15644           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
15645       else
15646         {
15647           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
15648                                      NULL, 1, OPTAB_DIRECT);
15649           reg =
15650             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
15651         }
15652       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
15653                                  NULL, 1, OPTAB_DIRECT);
15654       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
15655       if (mode == SImode)
15656         return reg;
15657       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
15658                                  NULL, 1, OPTAB_DIRECT);
15659       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
15660       return reg;
15661     }
15662 }
15663
15664 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
15665    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
15666    alignment from ALIGN to DESIRED_ALIGN.  */
15667 static rtx
15668 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
15669 {
15670   rtx promoted_val;
15671
15672   if (TARGET_64BIT
15673       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
15674     promoted_val = promote_duplicated_reg (DImode, val);
15675   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
15676     promoted_val = promote_duplicated_reg (SImode, val);
15677   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
15678     promoted_val = promote_duplicated_reg (HImode, val);
15679   else
15680     promoted_val = val;
15681
15682   return promoted_val;
15683 }
15684
15685 /* Expand string clear operation (bzero).  Use i386 string operations when
15686    profitable.  See expand_movmem comment for explanation of individual
15687    steps performed.  */
15688 int
15689 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
15690                     rtx expected_align_exp, rtx expected_size_exp)
15691 {
15692   rtx destreg;
15693   rtx label = NULL;
15694   rtx tmp;
15695   rtx jump_around_label = NULL;
15696   HOST_WIDE_INT align = 1;
15697   unsigned HOST_WIDE_INT count = 0;
15698   HOST_WIDE_INT expected_size = -1;
15699   int size_needed = 0, epilogue_size_needed;
15700   int desired_align = 0;
15701   enum stringop_alg alg;
15702   rtx promoted_val = NULL;
15703   bool force_loopy_epilogue = false;
15704   int dynamic_check;
15705
15706   if (CONST_INT_P (align_exp))
15707     align = INTVAL (align_exp);
15708   /* i386 can do misaligned access on reasonably increased cost.  */
15709   if (CONST_INT_P (expected_align_exp)
15710       && INTVAL (expected_align_exp) > align)
15711     align = INTVAL (expected_align_exp);
15712   if (CONST_INT_P (count_exp))
15713     count = expected_size = INTVAL (count_exp);
15714   if (CONST_INT_P (expected_size_exp) && count == 0)
15715     expected_size = INTVAL (expected_size_exp);
15716
15717   /* Make sure we don't need to care about overflow later on.  */
15718   if (count > ((unsigned HOST_WIDE_INT) 1 << 30))
15719     return 0;
15720
15721   /* Step 0: Decide on preferred algorithm, desired alignment and
15722      size of chunks to be copied by main loop.  */
15723
15724   alg = decide_alg (count, expected_size, true, &dynamic_check);
15725   desired_align = decide_alignment (align, alg, expected_size);
15726
15727   if (!TARGET_ALIGN_STRINGOPS)
15728     align = desired_align;
15729
15730   if (alg == libcall)
15731     return 0;
15732   gcc_assert (alg != no_stringop);
15733   if (!count)
15734     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
15735   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
15736   switch (alg)
15737     {
15738     case libcall:
15739     case no_stringop:
15740       gcc_unreachable ();
15741     case loop:
15742       size_needed = GET_MODE_SIZE (Pmode);
15743       break;
15744     case unrolled_loop:
15745       size_needed = GET_MODE_SIZE (Pmode) * 4;
15746       break;
15747     case rep_prefix_8_byte:
15748       size_needed = 8;
15749       break;
15750     case rep_prefix_4_byte:
15751       size_needed = 4;
15752       break;
15753     case rep_prefix_1_byte:
15754     case loop_1_byte:
15755       size_needed = 1;
15756       break;
15757     }
15758   epilogue_size_needed = size_needed;
15759
15760   /* Step 1: Prologue guard.  */
15761
15762   /* Alignment code needs count to be in register.  */
15763   if (CONST_INT_P (count_exp) && desired_align > align)
15764     {
15765       enum machine_mode mode = SImode;
15766       if (TARGET_64BIT && (count & ~0xffffffff))
15767         mode = DImode;
15768       count_exp = force_reg (mode, count_exp);
15769     }
15770   /* Do the cheap promotion to allow better CSE across the
15771      main loop and epilogue (ie one load of the big constant in the
15772      front of all code.  */
15773   if (CONST_INT_P (val_exp))
15774     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
15775                                                    desired_align, align);
15776   /* Ensure that alignment prologue won't copy past end of block.  */
15777   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
15778     {
15779       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
15780       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
15781          Make sure it is power of 2.  */
15782       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
15783
15784       /* To improve performance of small blocks, we jump around the VAL
15785          promoting mode.  This mean that if the promoted VAL is not constant,
15786          we might not use it in the epilogue and have to use byte
15787          loop variant.  */
15788       if (epilogue_size_needed > 2 && !promoted_val)
15789         force_loopy_epilogue = true;
15790       label = gen_label_rtx ();
15791       emit_cmp_and_jump_insns (count_exp,
15792                                GEN_INT (epilogue_size_needed),
15793                                LTU, 0, counter_mode (count_exp), 1, label);
15794       if (GET_CODE (count_exp) == CONST_INT)
15795         ;
15796       else if (expected_size == -1 || expected_size <= epilogue_size_needed)
15797         predict_jump (REG_BR_PROB_BASE * 60 / 100);
15798       else
15799         predict_jump (REG_BR_PROB_BASE * 20 / 100);
15800     }
15801   if (dynamic_check != -1)
15802     {
15803       rtx hot_label = gen_label_rtx ();
15804       jump_around_label = gen_label_rtx ();
15805       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
15806                                LEU, 0, counter_mode (count_exp), 1, hot_label);
15807       predict_jump (REG_BR_PROB_BASE * 90 / 100);
15808       set_storage_via_libcall (dst, count_exp, val_exp, false);
15809       emit_jump (jump_around_label);
15810       emit_label (hot_label);
15811     }
15812
15813   /* Step 2: Alignment prologue.  */
15814
15815   /* Do the expensive promotion once we branched off the small blocks.  */
15816   if (!promoted_val)
15817     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
15818                                                    desired_align, align);
15819   gcc_assert (desired_align >= 1 && align >= 1);
15820
15821   if (desired_align > align)
15822     {
15823       /* Except for the first move in epilogue, we no longer know
15824          constant offset in aliasing info.  It don't seems to worth
15825          the pain to maintain it for the first move, so throw away
15826          the info early.  */
15827       dst = change_address (dst, BLKmode, destreg);
15828       expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
15829                               desired_align);
15830     }
15831   if (label && size_needed == 1)
15832     {
15833       emit_label (label);
15834       LABEL_NUSES (label) = 1;
15835       label = NULL;
15836     }
15837
15838   /* Step 3: Main loop.  */
15839
15840   switch (alg)
15841     {
15842     case libcall:
15843     case no_stringop:
15844       gcc_unreachable ();
15845     case loop_1_byte:
15846       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
15847                                      count_exp, QImode, 1, expected_size);
15848       break;
15849     case loop:
15850       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
15851                                      count_exp, Pmode, 1, expected_size);
15852       break;
15853     case unrolled_loop:
15854       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
15855                                      count_exp, Pmode, 4, expected_size);
15856       break;
15857     case rep_prefix_8_byte:
15858       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
15859                                   DImode);
15860       break;
15861     case rep_prefix_4_byte:
15862       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
15863                                   SImode);
15864       break;
15865     case rep_prefix_1_byte:
15866       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
15867                                   QImode);
15868       break;
15869     }
15870   /* Adjust properly the offset of src and dest memory for aliasing.  */
15871   if (CONST_INT_P (count_exp))
15872     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
15873                                         (count / size_needed) * size_needed);
15874   else
15875     dst = change_address (dst, BLKmode, destreg);
15876
15877   /* Step 4: Epilogue to copy the remaining bytes.  */
15878
15879   if (label)
15880     {
15881       /* When the main loop is done, COUNT_EXP might hold original count,
15882          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
15883          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
15884          bytes. Compensate if needed.  */
15885
15886       if (size_needed < desired_align - align)
15887         {
15888           tmp =
15889             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
15890                                  GEN_INT (size_needed - 1), count_exp, 1,
15891                                  OPTAB_DIRECT);
15892           size_needed = desired_align - align + 1;
15893           if (tmp != count_exp)
15894             emit_move_insn (count_exp, tmp);
15895         }
15896       emit_label (label);
15897       LABEL_NUSES (label) = 1;
15898     }
15899   if (count_exp != const0_rtx && epilogue_size_needed > 1)
15900     {
15901       if (force_loopy_epilogue)
15902         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
15903                                          size_needed);
15904       else
15905         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
15906                                 size_needed);
15907     }
15908   if (jump_around_label)
15909     emit_label (jump_around_label);
15910   return 1;
15911 }
15912
15913 /* Expand the appropriate insns for doing strlen if not just doing
15914    repnz; scasb
15915
15916    out = result, initialized with the start address
15917    align_rtx = alignment of the address.
15918    scratch = scratch register, initialized with the startaddress when
15919         not aligned, otherwise undefined
15920
15921    This is just the body. It needs the initializations mentioned above and
15922    some address computing at the end.  These things are done in i386.md.  */
15923
15924 static void
15925 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
15926 {
15927   int align;
15928   rtx tmp;
15929   rtx align_2_label = NULL_RTX;
15930   rtx align_3_label = NULL_RTX;
15931   rtx align_4_label = gen_label_rtx ();
15932   rtx end_0_label = gen_label_rtx ();
15933   rtx mem;
15934   rtx tmpreg = gen_reg_rtx (SImode);
15935   rtx scratch = gen_reg_rtx (SImode);
15936   rtx cmp;
15937
15938   align = 0;
15939   if (CONST_INT_P (align_rtx))
15940     align = INTVAL (align_rtx);
15941
15942   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
15943
15944   /* Is there a known alignment and is it less than 4?  */
15945   if (align < 4)
15946     {
15947       rtx scratch1 = gen_reg_rtx (Pmode);
15948       emit_move_insn (scratch1, out);
15949       /* Is there a known alignment and is it not 2? */
15950       if (align != 2)
15951         {
15952           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
15953           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
15954
15955           /* Leave just the 3 lower bits.  */
15956           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
15957                                     NULL_RTX, 0, OPTAB_WIDEN);
15958
15959           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
15960                                    Pmode, 1, align_4_label);
15961           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
15962                                    Pmode, 1, align_2_label);
15963           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
15964                                    Pmode, 1, align_3_label);
15965         }
15966       else
15967         {
15968           /* Since the alignment is 2, we have to check 2 or 0 bytes;
15969              check if is aligned to 4 - byte.  */
15970
15971           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
15972                                     NULL_RTX, 0, OPTAB_WIDEN);
15973
15974           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
15975                                    Pmode, 1, align_4_label);
15976         }
15977
15978       mem = change_address (src, QImode, out);
15979
15980       /* Now compare the bytes.  */
15981
15982       /* Compare the first n unaligned byte on a byte per byte basis.  */
15983       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
15984                                QImode, 1, end_0_label);
15985
15986       /* Increment the address.  */
15987       if (TARGET_64BIT)
15988         emit_insn (gen_adddi3 (out, out, const1_rtx));
15989       else
15990         emit_insn (gen_addsi3 (out, out, const1_rtx));
15991
15992       /* Not needed with an alignment of 2 */
15993       if (align != 2)
15994         {
15995           emit_label (align_2_label);
15996
15997           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
15998                                    end_0_label);
15999
16000           if (TARGET_64BIT)
16001             emit_insn (gen_adddi3 (out, out, const1_rtx));
16002           else
16003             emit_insn (gen_addsi3 (out, out, const1_rtx));
16004
16005           emit_label (align_3_label);
16006         }
16007
16008       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
16009                                end_0_label);
16010
16011       if (TARGET_64BIT)
16012         emit_insn (gen_adddi3 (out, out, const1_rtx));
16013       else
16014         emit_insn (gen_addsi3 (out, out, const1_rtx));
16015     }
16016
16017   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
16018      align this loop.  It gives only huge programs, but does not help to
16019      speed up.  */
16020   emit_label (align_4_label);
16021
16022   mem = change_address (src, SImode, out);
16023   emit_move_insn (scratch, mem);
16024   if (TARGET_64BIT)
16025     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
16026   else
16027     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
16028
16029   /* This formula yields a nonzero result iff one of the bytes is zero.
16030      This saves three branches inside loop and many cycles.  */
16031
16032   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
16033   emit_insn (gen_one_cmplsi2 (scratch, scratch));
16034   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
16035   emit_insn (gen_andsi3 (tmpreg, tmpreg,
16036                          gen_int_mode (0x80808080, SImode)));
16037   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
16038                            align_4_label);
16039
16040   if (TARGET_CMOVE)
16041     {
16042        rtx reg = gen_reg_rtx (SImode);
16043        rtx reg2 = gen_reg_rtx (Pmode);
16044        emit_move_insn (reg, tmpreg);
16045        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
16046
16047        /* If zero is not in the first two bytes, move two bytes forward.  */
16048        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
16049        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
16050        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
16051        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
16052                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
16053                                                      reg,
16054                                                      tmpreg)));
16055        /* Emit lea manually to avoid clobbering of flags.  */
16056        emit_insn (gen_rtx_SET (SImode, reg2,
16057                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
16058
16059        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
16060        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
16061        emit_insn (gen_rtx_SET (VOIDmode, out,
16062                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
16063                                                      reg2,
16064                                                      out)));
16065
16066     }
16067   else
16068     {
16069        rtx end_2_label = gen_label_rtx ();
16070        /* Is zero in the first two bytes? */
16071
16072        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
16073        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
16074        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
16075        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
16076                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
16077                             pc_rtx);
16078        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
16079        JUMP_LABEL (tmp) = end_2_label;
16080
16081        /* Not in the first two.  Move two bytes forward.  */
16082        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
16083        if (TARGET_64BIT)
16084          emit_insn (gen_adddi3 (out, out, const2_rtx));
16085        else
16086          emit_insn (gen_addsi3 (out, out, const2_rtx));
16087
16088        emit_label (end_2_label);
16089
16090     }
16091
16092   /* Avoid branch in fixing the byte.  */
16093   tmpreg = gen_lowpart (QImode, tmpreg);
16094   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
16095   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, FLAGS_REG), const0_rtx);
16096   if (TARGET_64BIT)
16097     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
16098   else
16099     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
16100
16101   emit_label (end_0_label);
16102 }
16103
16104 /* Expand strlen.  */
16105
16106 int
16107 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
16108 {
16109   rtx addr, scratch1, scratch2, scratch3, scratch4;
16110
16111   /* The generic case of strlen expander is long.  Avoid it's
16112      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
16113
16114   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
16115       && !TARGET_INLINE_ALL_STRINGOPS
16116       && !optimize_size
16117       && (!CONST_INT_P (align) || INTVAL (align) < 4))
16118     return 0;
16119
16120   addr = force_reg (Pmode, XEXP (src, 0));
16121   scratch1 = gen_reg_rtx (Pmode);
16122
16123   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
16124       && !optimize_size)
16125     {
16126       /* Well it seems that some optimizer does not combine a call like
16127          foo(strlen(bar), strlen(bar));
16128          when the move and the subtraction is done here.  It does calculate
16129          the length just once when these instructions are done inside of
16130          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
16131          often used and I use one fewer register for the lifetime of
16132          output_strlen_unroll() this is better.  */
16133
16134       emit_move_insn (out, addr);
16135
16136       ix86_expand_strlensi_unroll_1 (out, src, align);
16137
16138       /* strlensi_unroll_1 returns the address of the zero at the end of
16139          the string, like memchr(), so compute the length by subtracting
16140          the start address.  */
16141       if (TARGET_64BIT)
16142         emit_insn (gen_subdi3 (out, out, addr));
16143       else
16144         emit_insn (gen_subsi3 (out, out, addr));
16145     }
16146   else
16147     {
16148       rtx unspec;
16149
16150       /* Can't use this if the user has appropriated eax, ecx, or edi.  */
16151       if (fixed_regs[AX_REG] || fixed_regs[CX_REG] || fixed_regs[DI_REG])
16152         return false;
16153
16154       scratch2 = gen_reg_rtx (Pmode);
16155       scratch3 = gen_reg_rtx (Pmode);
16156       scratch4 = force_reg (Pmode, constm1_rtx);
16157
16158       emit_move_insn (scratch3, addr);
16159       eoschar = force_reg (QImode, eoschar);
16160
16161       src = replace_equiv_address_nv (src, scratch3);
16162
16163       /* If .md starts supporting :P, this can be done in .md.  */
16164       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
16165                                                  scratch4), UNSPEC_SCAS);
16166       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
16167       if (TARGET_64BIT)
16168         {
16169           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
16170           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
16171         }
16172       else
16173         {
16174           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
16175           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
16176         }
16177     }
16178   return 1;
16179 }
16180
16181 /* For given symbol (function) construct code to compute address of it's PLT
16182    entry in large x86-64 PIC model.  */
16183 rtx
16184 construct_plt_address (rtx symbol)
16185 {
16186   rtx tmp = gen_reg_rtx (Pmode);
16187   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
16188
16189   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
16190   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
16191
16192   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
16193   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
16194   return tmp;
16195 }
16196
16197 void
16198 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
16199                   rtx callarg2 ATTRIBUTE_UNUSED,
16200                   rtx pop, int sibcall)
16201 {
16202   rtx use = NULL, call;
16203
16204   if (pop == const0_rtx)
16205     pop = NULL;
16206   gcc_assert (!TARGET_64BIT || !pop);
16207
16208   if (TARGET_MACHO && !TARGET_64BIT)
16209     {
16210 #if TARGET_MACHO
16211       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
16212         fnaddr = machopic_indirect_call_target (fnaddr);
16213 #endif
16214     }
16215   else
16216     {
16217       /* Static functions and indirect calls don't need the pic register.  */
16218       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
16219           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
16220           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
16221         use_reg (&use, pic_offset_table_rtx);
16222     }
16223
16224   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
16225     {
16226       rtx al = gen_rtx_REG (QImode, AX_REG);
16227       emit_move_insn (al, callarg2);
16228       use_reg (&use, al);
16229     }
16230
16231   if (ix86_cmodel == CM_LARGE_PIC
16232       && GET_CODE (fnaddr) == MEM
16233       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
16234       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
16235     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
16236   else if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
16237     {
16238       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
16239       fnaddr = gen_rtx_MEM (QImode, fnaddr);
16240     }
16241   if (sibcall && TARGET_64BIT
16242       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
16243     {
16244       rtx addr;
16245       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
16246       fnaddr = gen_rtx_REG (Pmode, R11_REG);
16247       emit_move_insn (fnaddr, addr);
16248       fnaddr = gen_rtx_MEM (QImode, fnaddr);
16249     }
16250
16251   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
16252   if (retval)
16253     call = gen_rtx_SET (VOIDmode, retval, call);
16254   if (pop)
16255     {
16256       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
16257       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
16258       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
16259     }
16260
16261   call = emit_call_insn (call);
16262   if (use)
16263     CALL_INSN_FUNCTION_USAGE (call) = use;
16264 }
16265
16266 \f
16267 /* Clear stack slot assignments remembered from previous functions.
16268    This is called from INIT_EXPANDERS once before RTL is emitted for each
16269    function.  */
16270
16271 static struct machine_function *
16272 ix86_init_machine_status (void)
16273 {
16274   struct machine_function *f;
16275
16276   f = GGC_CNEW (struct machine_function);
16277   f->use_fast_prologue_epilogue_nregs = -1;
16278   f->tls_descriptor_call_expanded_p = 0;
16279
16280   return f;
16281 }
16282
16283 /* Return a MEM corresponding to a stack slot with mode MODE.
16284    Allocate a new slot if necessary.
16285
16286    The RTL for a function can have several slots available: N is
16287    which slot to use.  */
16288
16289 rtx
16290 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
16291 {
16292   struct stack_local_entry *s;
16293
16294   gcc_assert (n < MAX_386_STACK_LOCALS);
16295
16296   /* Virtual slot is valid only before vregs are instantiated.  */
16297   gcc_assert ((n == SLOT_VIRTUAL) == !virtuals_instantiated);
16298
16299   for (s = ix86_stack_locals; s; s = s->next)
16300     if (s->mode == mode && s->n == n)
16301       return copy_rtx (s->rtl);
16302
16303   s = (struct stack_local_entry *)
16304     ggc_alloc (sizeof (struct stack_local_entry));
16305   s->n = n;
16306   s->mode = mode;
16307   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
16308
16309   s->next = ix86_stack_locals;
16310   ix86_stack_locals = s;
16311   return s->rtl;
16312 }
16313
16314 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
16315
16316 static GTY(()) rtx ix86_tls_symbol;
16317 rtx
16318 ix86_tls_get_addr (void)
16319 {
16320
16321   if (!ix86_tls_symbol)
16322     {
16323       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
16324                                             (TARGET_ANY_GNU_TLS
16325                                              && !TARGET_64BIT)
16326                                             ? "___tls_get_addr"
16327                                             : "__tls_get_addr");
16328     }
16329
16330   return ix86_tls_symbol;
16331 }
16332
16333 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
16334
16335 static GTY(()) rtx ix86_tls_module_base_symbol;
16336 rtx
16337 ix86_tls_module_base (void)
16338 {
16339
16340   if (!ix86_tls_module_base_symbol)
16341     {
16342       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
16343                                                         "_TLS_MODULE_BASE_");
16344       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
16345         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
16346     }
16347
16348   return ix86_tls_module_base_symbol;
16349 }
16350 \f
16351 /* Calculate the length of the memory address in the instruction
16352    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
16353
16354 int
16355 memory_address_length (rtx addr)
16356 {
16357   struct ix86_address parts;
16358   rtx base, index, disp;
16359   int len;
16360   int ok;
16361
16362   if (GET_CODE (addr) == PRE_DEC
16363       || GET_CODE (addr) == POST_INC
16364       || GET_CODE (addr) == PRE_MODIFY
16365       || GET_CODE (addr) == POST_MODIFY)
16366     return 0;
16367
16368   ok = ix86_decompose_address (addr, &parts);
16369   gcc_assert (ok);
16370
16371   if (parts.base && GET_CODE (parts.base) == SUBREG)
16372     parts.base = SUBREG_REG (parts.base);
16373   if (parts.index && GET_CODE (parts.index) == SUBREG)
16374     parts.index = SUBREG_REG (parts.index);
16375
16376   base = parts.base;
16377   index = parts.index;
16378   disp = parts.disp;
16379   len = 0;
16380
16381   /* Rule of thumb:
16382        - esp as the base always wants an index,
16383        - ebp as the base always wants a displacement.  */
16384
16385   /* Register Indirect.  */
16386   if (base && !index && !disp)
16387     {
16388       /* esp (for its index) and ebp (for its displacement) need
16389          the two-byte modrm form.  */
16390       if (addr == stack_pointer_rtx
16391           || addr == arg_pointer_rtx
16392           || addr == frame_pointer_rtx
16393           || addr == hard_frame_pointer_rtx)
16394         len = 1;
16395     }
16396
16397   /* Direct Addressing.  */
16398   else if (disp && !base && !index)
16399     len = 4;
16400
16401   else
16402     {
16403       /* Find the length of the displacement constant.  */
16404       if (disp)
16405         {
16406           if (base && satisfies_constraint_K (disp))
16407             len = 1;
16408           else
16409             len = 4;
16410         }
16411       /* ebp always wants a displacement.  */
16412       else if (base == hard_frame_pointer_rtx)
16413         len = 1;
16414
16415       /* An index requires the two-byte modrm form....  */
16416       if (index
16417           /* ...like esp, which always wants an index.  */
16418           || base == stack_pointer_rtx
16419           || base == arg_pointer_rtx
16420           || base == frame_pointer_rtx)
16421         len += 1;
16422     }
16423
16424   return len;
16425 }
16426
16427 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
16428    is set, expect that insn have 8bit immediate alternative.  */
16429 int
16430 ix86_attr_length_immediate_default (rtx insn, int shortform)
16431 {
16432   int len = 0;
16433   int i;
16434   extract_insn_cached (insn);
16435   for (i = recog_data.n_operands - 1; i >= 0; --i)
16436     if (CONSTANT_P (recog_data.operand[i]))
16437       {
16438         gcc_assert (!len);
16439         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
16440           len = 1;
16441         else
16442           {
16443             switch (get_attr_mode (insn))
16444               {
16445                 case MODE_QI:
16446                   len+=1;
16447                   break;
16448                 case MODE_HI:
16449                   len+=2;
16450                   break;
16451                 case MODE_SI:
16452                   len+=4;
16453                   break;
16454                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
16455                 case MODE_DI:
16456                   len+=4;
16457                   break;
16458                 default:
16459                   fatal_insn ("unknown insn mode", insn);
16460               }
16461           }
16462       }
16463   return len;
16464 }
16465 /* Compute default value for "length_address" attribute.  */
16466 int
16467 ix86_attr_length_address_default (rtx insn)
16468 {
16469   int i;
16470
16471   if (get_attr_type (insn) == TYPE_LEA)
16472     {
16473       rtx set = PATTERN (insn);
16474
16475       if (GET_CODE (set) == PARALLEL)
16476         set = XVECEXP (set, 0, 0);
16477
16478       gcc_assert (GET_CODE (set) == SET);
16479
16480       return memory_address_length (SET_SRC (set));
16481     }
16482
16483   extract_insn_cached (insn);
16484   for (i = recog_data.n_operands - 1; i >= 0; --i)
16485     if (MEM_P (recog_data.operand[i]))
16486       {
16487         return memory_address_length (XEXP (recog_data.operand[i], 0));
16488         break;
16489       }
16490   return 0;
16491 }
16492 \f
16493 /* Return the maximum number of instructions a cpu can issue.  */
16494
16495 static int
16496 ix86_issue_rate (void)
16497 {
16498   switch (ix86_tune)
16499     {
16500     case PROCESSOR_PENTIUM:
16501     case PROCESSOR_K6:
16502       return 2;
16503
16504     case PROCESSOR_PENTIUMPRO:
16505     case PROCESSOR_PENTIUM4:
16506     case PROCESSOR_ATHLON:
16507     case PROCESSOR_K8:
16508     case PROCESSOR_AMDFAM10:
16509     case PROCESSOR_NOCONA:
16510     case PROCESSOR_GENERIC32:
16511     case PROCESSOR_GENERIC64:
16512       return 3;
16513
16514     case PROCESSOR_CORE2:
16515       return 4;
16516
16517     default:
16518       return 1;
16519     }
16520 }
16521
16522 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
16523    by DEP_INSN and nothing set by DEP_INSN.  */
16524
16525 static int
16526 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
16527 {
16528   rtx set, set2;
16529
16530   /* Simplify the test for uninteresting insns.  */
16531   if (insn_type != TYPE_SETCC
16532       && insn_type != TYPE_ICMOV
16533       && insn_type != TYPE_FCMOV
16534       && insn_type != TYPE_IBR)
16535     return 0;
16536
16537   if ((set = single_set (dep_insn)) != 0)
16538     {
16539       set = SET_DEST (set);
16540       set2 = NULL_RTX;
16541     }
16542   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
16543            && XVECLEN (PATTERN (dep_insn), 0) == 2
16544            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
16545            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
16546     {
16547       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
16548       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
16549     }
16550   else
16551     return 0;
16552
16553   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
16554     return 0;
16555
16556   /* This test is true if the dependent insn reads the flags but
16557      not any other potentially set register.  */
16558   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
16559     return 0;
16560
16561   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
16562     return 0;
16563
16564   return 1;
16565 }
16566
16567 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
16568    address with operands set by DEP_INSN.  */
16569
16570 static int
16571 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
16572 {
16573   rtx addr;
16574
16575   if (insn_type == TYPE_LEA
16576       && TARGET_PENTIUM)
16577     {
16578       addr = PATTERN (insn);
16579
16580       if (GET_CODE (addr) == PARALLEL)
16581         addr = XVECEXP (addr, 0, 0);
16582
16583       gcc_assert (GET_CODE (addr) == SET);
16584
16585       addr = SET_SRC (addr);
16586     }
16587   else
16588     {
16589       int i;
16590       extract_insn_cached (insn);
16591       for (i = recog_data.n_operands - 1; i >= 0; --i)
16592         if (MEM_P (recog_data.operand[i]))
16593           {
16594             addr = XEXP (recog_data.operand[i], 0);
16595             goto found;
16596           }
16597       return 0;
16598     found:;
16599     }
16600
16601   return modified_in_p (addr, dep_insn);
16602 }
16603
16604 static int
16605 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
16606 {
16607   enum attr_type insn_type, dep_insn_type;
16608   enum attr_memory memory;
16609   rtx set, set2;
16610   int dep_insn_code_number;
16611
16612   /* Anti and output dependencies have zero cost on all CPUs.  */
16613   if (REG_NOTE_KIND (link) != 0)
16614     return 0;
16615
16616   dep_insn_code_number = recog_memoized (dep_insn);
16617
16618   /* If we can't recognize the insns, we can't really do anything.  */
16619   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
16620     return cost;
16621
16622   insn_type = get_attr_type (insn);
16623   dep_insn_type = get_attr_type (dep_insn);
16624
16625   switch (ix86_tune)
16626     {
16627     case PROCESSOR_PENTIUM:
16628       /* Address Generation Interlock adds a cycle of latency.  */
16629       if (ix86_agi_dependent (insn, dep_insn, insn_type))
16630         cost += 1;
16631
16632       /* ??? Compares pair with jump/setcc.  */
16633       if (ix86_flags_dependent (insn, dep_insn, insn_type))
16634         cost = 0;
16635
16636       /* Floating point stores require value to be ready one cycle earlier.  */
16637       if (insn_type == TYPE_FMOV
16638           && get_attr_memory (insn) == MEMORY_STORE
16639           && !ix86_agi_dependent (insn, dep_insn, insn_type))
16640         cost += 1;
16641       break;
16642
16643     case PROCESSOR_PENTIUMPRO:
16644       memory = get_attr_memory (insn);
16645
16646       /* INT->FP conversion is expensive.  */
16647       if (get_attr_fp_int_src (dep_insn))
16648         cost += 5;
16649
16650       /* There is one cycle extra latency between an FP op and a store.  */
16651       if (insn_type == TYPE_FMOV
16652           && (set = single_set (dep_insn)) != NULL_RTX
16653           && (set2 = single_set (insn)) != NULL_RTX
16654           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
16655           && MEM_P (SET_DEST (set2)))
16656         cost += 1;
16657
16658       /* Show ability of reorder buffer to hide latency of load by executing
16659          in parallel with previous instruction in case
16660          previous instruction is not needed to compute the address.  */
16661       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
16662           && !ix86_agi_dependent (insn, dep_insn, insn_type))
16663         {
16664           /* Claim moves to take one cycle, as core can issue one load
16665              at time and the next load can start cycle later.  */
16666           if (dep_insn_type == TYPE_IMOV
16667               || dep_insn_type == TYPE_FMOV)
16668             cost = 1;
16669           else if (cost > 1)
16670             cost--;
16671         }
16672       break;
16673
16674     case PROCESSOR_K6:
16675       memory = get_attr_memory (insn);
16676
16677       /* The esp dependency is resolved before the instruction is really
16678          finished.  */
16679       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
16680           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
16681         return 1;
16682
16683       /* INT->FP conversion is expensive.  */
16684       if (get_attr_fp_int_src (dep_insn))
16685         cost += 5;
16686
16687       /* Show ability of reorder buffer to hide latency of load by executing
16688          in parallel with previous instruction in case
16689          previous instruction is not needed to compute the address.  */
16690       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
16691           && !ix86_agi_dependent (insn, dep_insn, insn_type))
16692         {
16693           /* Claim moves to take one cycle, as core can issue one load
16694              at time and the next load can start cycle later.  */
16695           if (dep_insn_type == TYPE_IMOV
16696               || dep_insn_type == TYPE_FMOV)
16697             cost = 1;
16698           else if (cost > 2)
16699             cost -= 2;
16700           else
16701             cost = 1;
16702         }
16703       break;
16704
16705     case PROCESSOR_ATHLON:
16706     case PROCESSOR_K8:
16707     case PROCESSOR_AMDFAM10:
16708     case PROCESSOR_GENERIC32:
16709     case PROCESSOR_GENERIC64:
16710       memory = get_attr_memory (insn);
16711
16712       /* Show ability of reorder buffer to hide latency of load by executing
16713          in parallel with previous instruction in case
16714          previous instruction is not needed to compute the address.  */
16715       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
16716           && !ix86_agi_dependent (insn, dep_insn, insn_type))
16717         {
16718           enum attr_unit unit = get_attr_unit (insn);
16719           int loadcost = 3;
16720
16721           /* Because of the difference between the length of integer and
16722              floating unit pipeline preparation stages, the memory operands
16723              for floating point are cheaper.
16724
16725              ??? For Athlon it the difference is most probably 2.  */
16726           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
16727             loadcost = 3;
16728           else
16729             loadcost = TARGET_ATHLON ? 2 : 0;
16730
16731           if (cost >= loadcost)
16732             cost -= loadcost;
16733           else
16734             cost = 0;
16735         }
16736
16737     default:
16738       break;
16739     }
16740
16741   return cost;
16742 }
16743
16744 /* How many alternative schedules to try.  This should be as wide as the
16745    scheduling freedom in the DFA, but no wider.  Making this value too
16746    large results extra work for the scheduler.  */
16747
16748 static int
16749 ia32_multipass_dfa_lookahead (void)
16750 {
16751   switch (ix86_tune)
16752     {
16753     case PROCESSOR_PENTIUM:
16754       return 2;
16755
16756     case PROCESSOR_PENTIUMPRO:
16757     case PROCESSOR_K6:
16758       return 1;
16759
16760     default:
16761       return 0;
16762     }
16763 }
16764
16765 \f
16766 /* Compute the alignment given to a constant that is being placed in memory.
16767    EXP is the constant and ALIGN is the alignment that the object would
16768    ordinarily have.
16769    The value of this function is used instead of that alignment to align
16770    the object.  */
16771
16772 int
16773 ix86_constant_alignment (tree exp, int align)
16774 {
16775   if (TREE_CODE (exp) == REAL_CST)
16776     {
16777       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
16778         return 64;
16779       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
16780         return 128;
16781     }
16782   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
16783            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
16784     return BITS_PER_WORD;
16785
16786   return align;
16787 }
16788
16789 /* Compute the alignment for a static variable.
16790    TYPE is the data type, and ALIGN is the alignment that
16791    the object would ordinarily have.  The value of this function is used
16792    instead of that alignment to align the object.  */
16793
16794 int
16795 ix86_data_alignment (tree type, int align)
16796 {
16797   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
16798
16799   if (AGGREGATE_TYPE_P (type)
16800       && TYPE_SIZE (type)
16801       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
16802       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
16803           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
16804       && align < max_align)
16805     align = max_align;
16806
16807   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
16808      to 16byte boundary.  */
16809   if (TARGET_64BIT)
16810     {
16811       if (AGGREGATE_TYPE_P (type)
16812            && TYPE_SIZE (type)
16813            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
16814            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
16815                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
16816         return 128;
16817     }
16818
16819   if (TREE_CODE (type) == ARRAY_TYPE)
16820     {
16821       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
16822         return 64;
16823       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
16824         return 128;
16825     }
16826   else if (TREE_CODE (type) == COMPLEX_TYPE)
16827     {
16828
16829       if (TYPE_MODE (type) == DCmode && align < 64)
16830         return 64;
16831       if (TYPE_MODE (type) == XCmode && align < 128)
16832         return 128;
16833     }
16834   else if ((TREE_CODE (type) == RECORD_TYPE
16835             || TREE_CODE (type) == UNION_TYPE
16836             || TREE_CODE (type) == QUAL_UNION_TYPE)
16837            && TYPE_FIELDS (type))
16838     {
16839       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
16840         return 64;
16841       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
16842         return 128;
16843     }
16844   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
16845            || TREE_CODE (type) == INTEGER_TYPE)
16846     {
16847       if (TYPE_MODE (type) == DFmode && align < 64)
16848         return 64;
16849       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
16850         return 128;
16851     }
16852
16853   return align;
16854 }
16855
16856 /* Compute the alignment for a local variable.
16857    TYPE is the data type, and ALIGN is the alignment that
16858    the object would ordinarily have.  The value of this macro is used
16859    instead of that alignment to align the object.  */
16860
16861 int
16862 ix86_local_alignment (tree type, int align)
16863 {
16864   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
16865      to 16byte boundary.  */
16866   if (TARGET_64BIT)
16867     {
16868       if (AGGREGATE_TYPE_P (type)
16869            && TYPE_SIZE (type)
16870            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
16871            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
16872                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
16873         return 128;
16874     }
16875   if (TREE_CODE (type) == ARRAY_TYPE)
16876     {
16877       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
16878         return 64;
16879       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
16880         return 128;
16881     }
16882   else if (TREE_CODE (type) == COMPLEX_TYPE)
16883     {
16884       if (TYPE_MODE (type) == DCmode && align < 64)
16885         return 64;
16886       if (TYPE_MODE (type) == XCmode && align < 128)
16887         return 128;
16888     }
16889   else if ((TREE_CODE (type) == RECORD_TYPE
16890             || TREE_CODE (type) == UNION_TYPE
16891             || TREE_CODE (type) == QUAL_UNION_TYPE)
16892            && TYPE_FIELDS (type))
16893     {
16894       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
16895         return 64;
16896       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
16897         return 128;
16898     }
16899   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
16900            || TREE_CODE (type) == INTEGER_TYPE)
16901     {
16902
16903       if (TYPE_MODE (type) == DFmode && align < 64)
16904         return 64;
16905       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
16906         return 128;
16907     }
16908   return align;
16909 }
16910 \f
16911 /* Emit RTL insns to initialize the variable parts of a trampoline.
16912    FNADDR is an RTX for the address of the function's pure code.
16913    CXT is an RTX for the static chain value for the function.  */
16914 void
16915 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
16916 {
16917   if (!TARGET_64BIT)
16918     {
16919       /* Compute offset from the end of the jmp to the target function.  */
16920       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
16921                                plus_constant (tramp, 10),
16922                                NULL_RTX, 1, OPTAB_DIRECT);
16923       emit_move_insn (gen_rtx_MEM (QImode, tramp),
16924                       gen_int_mode (0xb9, QImode));
16925       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
16926       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
16927                       gen_int_mode (0xe9, QImode));
16928       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
16929     }
16930   else
16931     {
16932       int offset = 0;
16933       /* Try to load address using shorter movl instead of movabs.
16934          We may want to support movq for kernel mode, but kernel does not use
16935          trampolines at the moment.  */
16936       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
16937         {
16938           fnaddr = copy_to_mode_reg (DImode, fnaddr);
16939           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
16940                           gen_int_mode (0xbb41, HImode));
16941           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
16942                           gen_lowpart (SImode, fnaddr));
16943           offset += 6;
16944         }
16945       else
16946         {
16947           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
16948                           gen_int_mode (0xbb49, HImode));
16949           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
16950                           fnaddr);
16951           offset += 10;
16952         }
16953       /* Load static chain using movabs to r10.  */
16954       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
16955                       gen_int_mode (0xba49, HImode));
16956       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
16957                       cxt);
16958       offset += 10;
16959       /* Jump to the r11 */
16960       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
16961                       gen_int_mode (0xff49, HImode));
16962       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
16963                       gen_int_mode (0xe3, QImode));
16964       offset += 3;
16965       gcc_assert (offset <= TRAMPOLINE_SIZE);
16966     }
16967
16968 #ifdef ENABLE_EXECUTE_STACK
16969   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
16970                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
16971 #endif
16972 }
16973 \f
16974 /* Codes for all the SSE/MMX builtins.  */
16975 enum ix86_builtins
16976 {
16977   IX86_BUILTIN_ADDPS,
16978   IX86_BUILTIN_ADDSS,
16979   IX86_BUILTIN_DIVPS,
16980   IX86_BUILTIN_DIVSS,
16981   IX86_BUILTIN_MULPS,
16982   IX86_BUILTIN_MULSS,
16983   IX86_BUILTIN_SUBPS,
16984   IX86_BUILTIN_SUBSS,
16985
16986   IX86_BUILTIN_CMPEQPS,
16987   IX86_BUILTIN_CMPLTPS,
16988   IX86_BUILTIN_CMPLEPS,
16989   IX86_BUILTIN_CMPGTPS,
16990   IX86_BUILTIN_CMPGEPS,
16991   IX86_BUILTIN_CMPNEQPS,
16992   IX86_BUILTIN_CMPNLTPS,
16993   IX86_BUILTIN_CMPNLEPS,
16994   IX86_BUILTIN_CMPNGTPS,
16995   IX86_BUILTIN_CMPNGEPS,
16996   IX86_BUILTIN_CMPORDPS,
16997   IX86_BUILTIN_CMPUNORDPS,
16998   IX86_BUILTIN_CMPEQSS,
16999   IX86_BUILTIN_CMPLTSS,
17000   IX86_BUILTIN_CMPLESS,
17001   IX86_BUILTIN_CMPNEQSS,
17002   IX86_BUILTIN_CMPNLTSS,
17003   IX86_BUILTIN_CMPNLESS,
17004   IX86_BUILTIN_CMPNGTSS,
17005   IX86_BUILTIN_CMPNGESS,
17006   IX86_BUILTIN_CMPORDSS,
17007   IX86_BUILTIN_CMPUNORDSS,
17008
17009   IX86_BUILTIN_COMIEQSS,
17010   IX86_BUILTIN_COMILTSS,
17011   IX86_BUILTIN_COMILESS,
17012   IX86_BUILTIN_COMIGTSS,
17013   IX86_BUILTIN_COMIGESS,
17014   IX86_BUILTIN_COMINEQSS,
17015   IX86_BUILTIN_UCOMIEQSS,
17016   IX86_BUILTIN_UCOMILTSS,
17017   IX86_BUILTIN_UCOMILESS,
17018   IX86_BUILTIN_UCOMIGTSS,
17019   IX86_BUILTIN_UCOMIGESS,
17020   IX86_BUILTIN_UCOMINEQSS,
17021
17022   IX86_BUILTIN_CVTPI2PS,
17023   IX86_BUILTIN_CVTPS2PI,
17024   IX86_BUILTIN_CVTSI2SS,
17025   IX86_BUILTIN_CVTSI642SS,
17026   IX86_BUILTIN_CVTSS2SI,
17027   IX86_BUILTIN_CVTSS2SI64,
17028   IX86_BUILTIN_CVTTPS2PI,
17029   IX86_BUILTIN_CVTTSS2SI,
17030   IX86_BUILTIN_CVTTSS2SI64,
17031
17032   IX86_BUILTIN_MAXPS,
17033   IX86_BUILTIN_MAXSS,
17034   IX86_BUILTIN_MINPS,
17035   IX86_BUILTIN_MINSS,
17036
17037   IX86_BUILTIN_LOADUPS,
17038   IX86_BUILTIN_STOREUPS,
17039   IX86_BUILTIN_MOVSS,
17040
17041   IX86_BUILTIN_MOVHLPS,
17042   IX86_BUILTIN_MOVLHPS,
17043   IX86_BUILTIN_LOADHPS,
17044   IX86_BUILTIN_LOADLPS,
17045   IX86_BUILTIN_STOREHPS,
17046   IX86_BUILTIN_STORELPS,
17047
17048   IX86_BUILTIN_MASKMOVQ,
17049   IX86_BUILTIN_MOVMSKPS,
17050   IX86_BUILTIN_PMOVMSKB,
17051
17052   IX86_BUILTIN_MOVNTPS,
17053   IX86_BUILTIN_MOVNTQ,
17054
17055   IX86_BUILTIN_LOADDQU,
17056   IX86_BUILTIN_STOREDQU,
17057
17058   IX86_BUILTIN_PACKSSWB,
17059   IX86_BUILTIN_PACKSSDW,
17060   IX86_BUILTIN_PACKUSWB,
17061
17062   IX86_BUILTIN_PADDB,
17063   IX86_BUILTIN_PADDW,
17064   IX86_BUILTIN_PADDD,
17065   IX86_BUILTIN_PADDQ,
17066   IX86_BUILTIN_PADDSB,
17067   IX86_BUILTIN_PADDSW,
17068   IX86_BUILTIN_PADDUSB,
17069   IX86_BUILTIN_PADDUSW,
17070   IX86_BUILTIN_PSUBB,
17071   IX86_BUILTIN_PSUBW,
17072   IX86_BUILTIN_PSUBD,
17073   IX86_BUILTIN_PSUBQ,
17074   IX86_BUILTIN_PSUBSB,
17075   IX86_BUILTIN_PSUBSW,
17076   IX86_BUILTIN_PSUBUSB,
17077   IX86_BUILTIN_PSUBUSW,
17078
17079   IX86_BUILTIN_PAND,
17080   IX86_BUILTIN_PANDN,
17081   IX86_BUILTIN_POR,
17082   IX86_BUILTIN_PXOR,
17083
17084   IX86_BUILTIN_PAVGB,
17085   IX86_BUILTIN_PAVGW,
17086
17087   IX86_BUILTIN_PCMPEQB,
17088   IX86_BUILTIN_PCMPEQW,
17089   IX86_BUILTIN_PCMPEQD,
17090   IX86_BUILTIN_PCMPGTB,
17091   IX86_BUILTIN_PCMPGTW,
17092   IX86_BUILTIN_PCMPGTD,
17093
17094   IX86_BUILTIN_PMADDWD,
17095
17096   IX86_BUILTIN_PMAXSW,
17097   IX86_BUILTIN_PMAXUB,
17098   IX86_BUILTIN_PMINSW,
17099   IX86_BUILTIN_PMINUB,
17100
17101   IX86_BUILTIN_PMULHUW,
17102   IX86_BUILTIN_PMULHW,
17103   IX86_BUILTIN_PMULLW,
17104
17105   IX86_BUILTIN_PSADBW,
17106   IX86_BUILTIN_PSHUFW,
17107
17108   IX86_BUILTIN_PSLLW,
17109   IX86_BUILTIN_PSLLD,
17110   IX86_BUILTIN_PSLLQ,
17111   IX86_BUILTIN_PSRAW,
17112   IX86_BUILTIN_PSRAD,
17113   IX86_BUILTIN_PSRLW,
17114   IX86_BUILTIN_PSRLD,
17115   IX86_BUILTIN_PSRLQ,
17116   IX86_BUILTIN_PSLLWI,
17117   IX86_BUILTIN_PSLLDI,
17118   IX86_BUILTIN_PSLLQI,
17119   IX86_BUILTIN_PSRAWI,
17120   IX86_BUILTIN_PSRADI,
17121   IX86_BUILTIN_PSRLWI,
17122   IX86_BUILTIN_PSRLDI,
17123   IX86_BUILTIN_PSRLQI,
17124
17125   IX86_BUILTIN_PUNPCKHBW,
17126   IX86_BUILTIN_PUNPCKHWD,
17127   IX86_BUILTIN_PUNPCKHDQ,
17128   IX86_BUILTIN_PUNPCKLBW,
17129   IX86_BUILTIN_PUNPCKLWD,
17130   IX86_BUILTIN_PUNPCKLDQ,
17131
17132   IX86_BUILTIN_SHUFPS,
17133
17134   IX86_BUILTIN_RCPPS,
17135   IX86_BUILTIN_RCPSS,
17136   IX86_BUILTIN_RSQRTPS,
17137   IX86_BUILTIN_RSQRTPS_NR,
17138   IX86_BUILTIN_RSQRTSS,
17139   IX86_BUILTIN_RSQRTF,
17140   IX86_BUILTIN_SQRTPS,
17141   IX86_BUILTIN_SQRTPS_NR,
17142   IX86_BUILTIN_SQRTSS,
17143
17144   IX86_BUILTIN_UNPCKHPS,
17145   IX86_BUILTIN_UNPCKLPS,
17146
17147   IX86_BUILTIN_ANDPS,
17148   IX86_BUILTIN_ANDNPS,
17149   IX86_BUILTIN_ORPS,
17150   IX86_BUILTIN_XORPS,
17151
17152   IX86_BUILTIN_EMMS,
17153   IX86_BUILTIN_LDMXCSR,
17154   IX86_BUILTIN_STMXCSR,
17155   IX86_BUILTIN_SFENCE,
17156
17157   /* 3DNow! Original */
17158   IX86_BUILTIN_FEMMS,
17159   IX86_BUILTIN_PAVGUSB,
17160   IX86_BUILTIN_PF2ID,
17161   IX86_BUILTIN_PFACC,
17162   IX86_BUILTIN_PFADD,
17163   IX86_BUILTIN_PFCMPEQ,
17164   IX86_BUILTIN_PFCMPGE,
17165   IX86_BUILTIN_PFCMPGT,
17166   IX86_BUILTIN_PFMAX,
17167   IX86_BUILTIN_PFMIN,
17168   IX86_BUILTIN_PFMUL,
17169   IX86_BUILTIN_PFRCP,
17170   IX86_BUILTIN_PFRCPIT1,
17171   IX86_BUILTIN_PFRCPIT2,
17172   IX86_BUILTIN_PFRSQIT1,
17173   IX86_BUILTIN_PFRSQRT,
17174   IX86_BUILTIN_PFSUB,
17175   IX86_BUILTIN_PFSUBR,
17176   IX86_BUILTIN_PI2FD,
17177   IX86_BUILTIN_PMULHRW,
17178
17179   /* 3DNow! Athlon Extensions */
17180   IX86_BUILTIN_PF2IW,
17181   IX86_BUILTIN_PFNACC,
17182   IX86_BUILTIN_PFPNACC,
17183   IX86_BUILTIN_PI2FW,
17184   IX86_BUILTIN_PSWAPDSI,
17185   IX86_BUILTIN_PSWAPDSF,
17186
17187   /* SSE2 */
17188   IX86_BUILTIN_ADDPD,
17189   IX86_BUILTIN_ADDSD,
17190   IX86_BUILTIN_DIVPD,
17191   IX86_BUILTIN_DIVSD,
17192   IX86_BUILTIN_MULPD,
17193   IX86_BUILTIN_MULSD,
17194   IX86_BUILTIN_SUBPD,
17195   IX86_BUILTIN_SUBSD,
17196
17197   IX86_BUILTIN_CMPEQPD,
17198   IX86_BUILTIN_CMPLTPD,
17199   IX86_BUILTIN_CMPLEPD,
17200   IX86_BUILTIN_CMPGTPD,
17201   IX86_BUILTIN_CMPGEPD,
17202   IX86_BUILTIN_CMPNEQPD,
17203   IX86_BUILTIN_CMPNLTPD,
17204   IX86_BUILTIN_CMPNLEPD,
17205   IX86_BUILTIN_CMPNGTPD,
17206   IX86_BUILTIN_CMPNGEPD,
17207   IX86_BUILTIN_CMPORDPD,
17208   IX86_BUILTIN_CMPUNORDPD,
17209   IX86_BUILTIN_CMPEQSD,
17210   IX86_BUILTIN_CMPLTSD,
17211   IX86_BUILTIN_CMPLESD,
17212   IX86_BUILTIN_CMPNEQSD,
17213   IX86_BUILTIN_CMPNLTSD,
17214   IX86_BUILTIN_CMPNLESD,
17215   IX86_BUILTIN_CMPORDSD,
17216   IX86_BUILTIN_CMPUNORDSD,
17217
17218   IX86_BUILTIN_COMIEQSD,
17219   IX86_BUILTIN_COMILTSD,
17220   IX86_BUILTIN_COMILESD,
17221   IX86_BUILTIN_COMIGTSD,
17222   IX86_BUILTIN_COMIGESD,
17223   IX86_BUILTIN_COMINEQSD,
17224   IX86_BUILTIN_UCOMIEQSD,
17225   IX86_BUILTIN_UCOMILTSD,
17226   IX86_BUILTIN_UCOMILESD,
17227   IX86_BUILTIN_UCOMIGTSD,
17228   IX86_BUILTIN_UCOMIGESD,
17229   IX86_BUILTIN_UCOMINEQSD,
17230
17231   IX86_BUILTIN_MAXPD,
17232   IX86_BUILTIN_MAXSD,
17233   IX86_BUILTIN_MINPD,
17234   IX86_BUILTIN_MINSD,
17235
17236   IX86_BUILTIN_ANDPD,
17237   IX86_BUILTIN_ANDNPD,
17238   IX86_BUILTIN_ORPD,
17239   IX86_BUILTIN_XORPD,
17240
17241   IX86_BUILTIN_SQRTPD,
17242   IX86_BUILTIN_SQRTSD,
17243
17244   IX86_BUILTIN_UNPCKHPD,
17245   IX86_BUILTIN_UNPCKLPD,
17246
17247   IX86_BUILTIN_SHUFPD,
17248
17249   IX86_BUILTIN_LOADUPD,
17250   IX86_BUILTIN_STOREUPD,
17251   IX86_BUILTIN_MOVSD,
17252
17253   IX86_BUILTIN_LOADHPD,
17254   IX86_BUILTIN_LOADLPD,
17255
17256   IX86_BUILTIN_CVTDQ2PD,
17257   IX86_BUILTIN_CVTDQ2PS,
17258
17259   IX86_BUILTIN_CVTPD2DQ,
17260   IX86_BUILTIN_CVTPD2PI,
17261   IX86_BUILTIN_CVTPD2PS,
17262   IX86_BUILTIN_CVTTPD2DQ,
17263   IX86_BUILTIN_CVTTPD2PI,
17264
17265   IX86_BUILTIN_CVTPI2PD,
17266   IX86_BUILTIN_CVTSI2SD,
17267   IX86_BUILTIN_CVTSI642SD,
17268
17269   IX86_BUILTIN_CVTSD2SI,
17270   IX86_BUILTIN_CVTSD2SI64,
17271   IX86_BUILTIN_CVTSD2SS,
17272   IX86_BUILTIN_CVTSS2SD,
17273   IX86_BUILTIN_CVTTSD2SI,
17274   IX86_BUILTIN_CVTTSD2SI64,
17275
17276   IX86_BUILTIN_CVTPS2DQ,
17277   IX86_BUILTIN_CVTPS2PD,
17278   IX86_BUILTIN_CVTTPS2DQ,
17279
17280   IX86_BUILTIN_MOVNTI,
17281   IX86_BUILTIN_MOVNTPD,
17282   IX86_BUILTIN_MOVNTDQ,
17283
17284   /* SSE2 MMX */
17285   IX86_BUILTIN_MASKMOVDQU,
17286   IX86_BUILTIN_MOVMSKPD,
17287   IX86_BUILTIN_PMOVMSKB128,
17288
17289   IX86_BUILTIN_PACKSSWB128,
17290   IX86_BUILTIN_PACKSSDW128,
17291   IX86_BUILTIN_PACKUSWB128,
17292
17293   IX86_BUILTIN_PADDB128,
17294   IX86_BUILTIN_PADDW128,
17295   IX86_BUILTIN_PADDD128,
17296   IX86_BUILTIN_PADDQ128,
17297   IX86_BUILTIN_PADDSB128,
17298   IX86_BUILTIN_PADDSW128,
17299   IX86_BUILTIN_PADDUSB128,
17300   IX86_BUILTIN_PADDUSW128,
17301   IX86_BUILTIN_PSUBB128,
17302   IX86_BUILTIN_PSUBW128,
17303   IX86_BUILTIN_PSUBD128,
17304   IX86_BUILTIN_PSUBQ128,
17305   IX86_BUILTIN_PSUBSB128,
17306   IX86_BUILTIN_PSUBSW128,
17307   IX86_BUILTIN_PSUBUSB128,
17308   IX86_BUILTIN_PSUBUSW128,
17309
17310   IX86_BUILTIN_PAND128,
17311   IX86_BUILTIN_PANDN128,
17312   IX86_BUILTIN_POR128,
17313   IX86_BUILTIN_PXOR128,
17314
17315   IX86_BUILTIN_PAVGB128,
17316   IX86_BUILTIN_PAVGW128,
17317
17318   IX86_BUILTIN_PCMPEQB128,
17319   IX86_BUILTIN_PCMPEQW128,
17320   IX86_BUILTIN_PCMPEQD128,
17321   IX86_BUILTIN_PCMPGTB128,
17322   IX86_BUILTIN_PCMPGTW128,
17323   IX86_BUILTIN_PCMPGTD128,
17324
17325   IX86_BUILTIN_PMADDWD128,
17326
17327   IX86_BUILTIN_PMAXSW128,
17328   IX86_BUILTIN_PMAXUB128,
17329   IX86_BUILTIN_PMINSW128,
17330   IX86_BUILTIN_PMINUB128,
17331
17332   IX86_BUILTIN_PMULUDQ,
17333   IX86_BUILTIN_PMULUDQ128,
17334   IX86_BUILTIN_PMULHUW128,
17335   IX86_BUILTIN_PMULHW128,
17336   IX86_BUILTIN_PMULLW128,
17337
17338   IX86_BUILTIN_PSADBW128,
17339   IX86_BUILTIN_PSHUFHW,
17340   IX86_BUILTIN_PSHUFLW,
17341   IX86_BUILTIN_PSHUFD,
17342
17343   IX86_BUILTIN_PSLLDQI128,
17344   IX86_BUILTIN_PSLLWI128,
17345   IX86_BUILTIN_PSLLDI128,
17346   IX86_BUILTIN_PSLLQI128,
17347   IX86_BUILTIN_PSRAWI128,
17348   IX86_BUILTIN_PSRADI128,
17349   IX86_BUILTIN_PSRLDQI128,
17350   IX86_BUILTIN_PSRLWI128,
17351   IX86_BUILTIN_PSRLDI128,
17352   IX86_BUILTIN_PSRLQI128,
17353
17354   IX86_BUILTIN_PSLLDQ128,
17355   IX86_BUILTIN_PSLLW128,
17356   IX86_BUILTIN_PSLLD128,
17357   IX86_BUILTIN_PSLLQ128,
17358   IX86_BUILTIN_PSRAW128,
17359   IX86_BUILTIN_PSRAD128,
17360   IX86_BUILTIN_PSRLW128,
17361   IX86_BUILTIN_PSRLD128,
17362   IX86_BUILTIN_PSRLQ128,
17363
17364   IX86_BUILTIN_PUNPCKHBW128,
17365   IX86_BUILTIN_PUNPCKHWD128,
17366   IX86_BUILTIN_PUNPCKHDQ128,
17367   IX86_BUILTIN_PUNPCKHQDQ128,
17368   IX86_BUILTIN_PUNPCKLBW128,
17369   IX86_BUILTIN_PUNPCKLWD128,
17370   IX86_BUILTIN_PUNPCKLDQ128,
17371   IX86_BUILTIN_PUNPCKLQDQ128,
17372
17373   IX86_BUILTIN_CLFLUSH,
17374   IX86_BUILTIN_MFENCE,
17375   IX86_BUILTIN_LFENCE,
17376
17377   /* Prescott New Instructions.  */
17378   IX86_BUILTIN_ADDSUBPS,
17379   IX86_BUILTIN_HADDPS,
17380   IX86_BUILTIN_HSUBPS,
17381   IX86_BUILTIN_MOVSHDUP,
17382   IX86_BUILTIN_MOVSLDUP,
17383   IX86_BUILTIN_ADDSUBPD,
17384   IX86_BUILTIN_HADDPD,
17385   IX86_BUILTIN_HSUBPD,
17386   IX86_BUILTIN_LDDQU,
17387
17388   IX86_BUILTIN_MONITOR,
17389   IX86_BUILTIN_MWAIT,
17390
17391   /* SSSE3.  */
17392   IX86_BUILTIN_PHADDW,
17393   IX86_BUILTIN_PHADDD,
17394   IX86_BUILTIN_PHADDSW,
17395   IX86_BUILTIN_PHSUBW,
17396   IX86_BUILTIN_PHSUBD,
17397   IX86_BUILTIN_PHSUBSW,
17398   IX86_BUILTIN_PMADDUBSW,
17399   IX86_BUILTIN_PMULHRSW,
17400   IX86_BUILTIN_PSHUFB,
17401   IX86_BUILTIN_PSIGNB,
17402   IX86_BUILTIN_PSIGNW,
17403   IX86_BUILTIN_PSIGND,
17404   IX86_BUILTIN_PALIGNR,
17405   IX86_BUILTIN_PABSB,
17406   IX86_BUILTIN_PABSW,
17407   IX86_BUILTIN_PABSD,
17408
17409   IX86_BUILTIN_PHADDW128,
17410   IX86_BUILTIN_PHADDD128,
17411   IX86_BUILTIN_PHADDSW128,
17412   IX86_BUILTIN_PHSUBW128,
17413   IX86_BUILTIN_PHSUBD128,
17414   IX86_BUILTIN_PHSUBSW128,
17415   IX86_BUILTIN_PMADDUBSW128,
17416   IX86_BUILTIN_PMULHRSW128,
17417   IX86_BUILTIN_PSHUFB128,
17418   IX86_BUILTIN_PSIGNB128,
17419   IX86_BUILTIN_PSIGNW128,
17420   IX86_BUILTIN_PSIGND128,
17421   IX86_BUILTIN_PALIGNR128,
17422   IX86_BUILTIN_PABSB128,
17423   IX86_BUILTIN_PABSW128,
17424   IX86_BUILTIN_PABSD128,
17425
17426   /* AMDFAM10 - SSE4A New Instructions.  */
17427   IX86_BUILTIN_MOVNTSD,
17428   IX86_BUILTIN_MOVNTSS,
17429   IX86_BUILTIN_EXTRQI,
17430   IX86_BUILTIN_EXTRQ,
17431   IX86_BUILTIN_INSERTQI,
17432   IX86_BUILTIN_INSERTQ,
17433
17434   /* SSE4.1.  */
17435   IX86_BUILTIN_BLENDPD,
17436   IX86_BUILTIN_BLENDPS,
17437   IX86_BUILTIN_BLENDVPD,
17438   IX86_BUILTIN_BLENDVPS,
17439   IX86_BUILTIN_PBLENDVB128,
17440   IX86_BUILTIN_PBLENDW128,
17441
17442   IX86_BUILTIN_DPPD,
17443   IX86_BUILTIN_DPPS,
17444
17445   IX86_BUILTIN_INSERTPS128,
17446
17447   IX86_BUILTIN_MOVNTDQA,
17448   IX86_BUILTIN_MPSADBW128,
17449   IX86_BUILTIN_PACKUSDW128,
17450   IX86_BUILTIN_PCMPEQQ,
17451   IX86_BUILTIN_PHMINPOSUW128,
17452
17453   IX86_BUILTIN_PMAXSB128,
17454   IX86_BUILTIN_PMAXSD128,
17455   IX86_BUILTIN_PMAXUD128,
17456   IX86_BUILTIN_PMAXUW128,
17457
17458   IX86_BUILTIN_PMINSB128,
17459   IX86_BUILTIN_PMINSD128,
17460   IX86_BUILTIN_PMINUD128,
17461   IX86_BUILTIN_PMINUW128,
17462
17463   IX86_BUILTIN_PMOVSXBW128,
17464   IX86_BUILTIN_PMOVSXBD128,
17465   IX86_BUILTIN_PMOVSXBQ128,
17466   IX86_BUILTIN_PMOVSXWD128,
17467   IX86_BUILTIN_PMOVSXWQ128,
17468   IX86_BUILTIN_PMOVSXDQ128,
17469
17470   IX86_BUILTIN_PMOVZXBW128,
17471   IX86_BUILTIN_PMOVZXBD128,
17472   IX86_BUILTIN_PMOVZXBQ128,
17473   IX86_BUILTIN_PMOVZXWD128,
17474   IX86_BUILTIN_PMOVZXWQ128,
17475   IX86_BUILTIN_PMOVZXDQ128,
17476
17477   IX86_BUILTIN_PMULDQ128,
17478   IX86_BUILTIN_PMULLD128,
17479
17480   IX86_BUILTIN_ROUNDPD,
17481   IX86_BUILTIN_ROUNDPS,
17482   IX86_BUILTIN_ROUNDSD,
17483   IX86_BUILTIN_ROUNDSS,
17484
17485   IX86_BUILTIN_PTESTZ,
17486   IX86_BUILTIN_PTESTC,
17487   IX86_BUILTIN_PTESTNZC,
17488
17489   IX86_BUILTIN_VEC_INIT_V2SI,
17490   IX86_BUILTIN_VEC_INIT_V4HI,
17491   IX86_BUILTIN_VEC_INIT_V8QI,
17492   IX86_BUILTIN_VEC_EXT_V2DF,
17493   IX86_BUILTIN_VEC_EXT_V2DI,
17494   IX86_BUILTIN_VEC_EXT_V4SF,
17495   IX86_BUILTIN_VEC_EXT_V4SI,
17496   IX86_BUILTIN_VEC_EXT_V8HI,
17497   IX86_BUILTIN_VEC_EXT_V2SI,
17498   IX86_BUILTIN_VEC_EXT_V4HI,
17499   IX86_BUILTIN_VEC_EXT_V16QI,
17500   IX86_BUILTIN_VEC_SET_V2DI,
17501   IX86_BUILTIN_VEC_SET_V4SF,
17502   IX86_BUILTIN_VEC_SET_V4SI,
17503   IX86_BUILTIN_VEC_SET_V8HI,
17504   IX86_BUILTIN_VEC_SET_V4HI,
17505   IX86_BUILTIN_VEC_SET_V16QI,
17506
17507   IX86_BUILTIN_VEC_PACK_SFIX,
17508
17509   /* SSE4.2.  */
17510   IX86_BUILTIN_CRC32QI,
17511   IX86_BUILTIN_CRC32HI,
17512   IX86_BUILTIN_CRC32SI,
17513   IX86_BUILTIN_CRC32DI,
17514
17515   IX86_BUILTIN_PCMPESTRI128,
17516   IX86_BUILTIN_PCMPESTRM128,
17517   IX86_BUILTIN_PCMPESTRA128,
17518   IX86_BUILTIN_PCMPESTRC128,
17519   IX86_BUILTIN_PCMPESTRO128,
17520   IX86_BUILTIN_PCMPESTRS128,
17521   IX86_BUILTIN_PCMPESTRZ128,
17522   IX86_BUILTIN_PCMPISTRI128,
17523   IX86_BUILTIN_PCMPISTRM128,
17524   IX86_BUILTIN_PCMPISTRA128,
17525   IX86_BUILTIN_PCMPISTRC128,
17526   IX86_BUILTIN_PCMPISTRO128,
17527   IX86_BUILTIN_PCMPISTRS128,
17528   IX86_BUILTIN_PCMPISTRZ128,
17529
17530   IX86_BUILTIN_PCMPGTQ,
17531
17532   /* TFmode support builtins.  */
17533   IX86_BUILTIN_INFQ,
17534   IX86_BUILTIN_FABSQ,
17535   IX86_BUILTIN_COPYSIGNQ,
17536
17537   /* SSE5 instructions */
17538   IX86_BUILTIN_FMADDSS,
17539   IX86_BUILTIN_FMADDSD,
17540   IX86_BUILTIN_FMADDPS,
17541   IX86_BUILTIN_FMADDPD,
17542   IX86_BUILTIN_FMSUBSS,
17543   IX86_BUILTIN_FMSUBSD,
17544   IX86_BUILTIN_FMSUBPS,
17545   IX86_BUILTIN_FMSUBPD,
17546   IX86_BUILTIN_FNMADDSS,
17547   IX86_BUILTIN_FNMADDSD,
17548   IX86_BUILTIN_FNMADDPS,
17549   IX86_BUILTIN_FNMADDPD,
17550   IX86_BUILTIN_FNMSUBSS,
17551   IX86_BUILTIN_FNMSUBSD,
17552   IX86_BUILTIN_FNMSUBPS,
17553   IX86_BUILTIN_FNMSUBPD,
17554   IX86_BUILTIN_PCMOV_V2DI,
17555   IX86_BUILTIN_PCMOV_V4SI,
17556   IX86_BUILTIN_PCMOV_V8HI,
17557   IX86_BUILTIN_PCMOV_V16QI,
17558   IX86_BUILTIN_PCMOV_V4SF,
17559   IX86_BUILTIN_PCMOV_V2DF,
17560   IX86_BUILTIN_PPERM,
17561   IX86_BUILTIN_PERMPS,
17562   IX86_BUILTIN_PERMPD,
17563   IX86_BUILTIN_PMACSSWW,
17564   IX86_BUILTIN_PMACSWW,
17565   IX86_BUILTIN_PMACSSWD,
17566   IX86_BUILTIN_PMACSWD,
17567   IX86_BUILTIN_PMACSSDD,
17568   IX86_BUILTIN_PMACSDD,
17569   IX86_BUILTIN_PMACSSDQL,
17570   IX86_BUILTIN_PMACSSDQH,
17571   IX86_BUILTIN_PMACSDQL,
17572   IX86_BUILTIN_PMACSDQH,
17573   IX86_BUILTIN_PMADCSSWD,
17574   IX86_BUILTIN_PMADCSWD,
17575   IX86_BUILTIN_PHADDBW,
17576   IX86_BUILTIN_PHADDBD,
17577   IX86_BUILTIN_PHADDBQ,
17578   IX86_BUILTIN_PHADDWD,
17579   IX86_BUILTIN_PHADDWQ,
17580   IX86_BUILTIN_PHADDDQ,
17581   IX86_BUILTIN_PHADDUBW,
17582   IX86_BUILTIN_PHADDUBD,
17583   IX86_BUILTIN_PHADDUBQ,
17584   IX86_BUILTIN_PHADDUWD,
17585   IX86_BUILTIN_PHADDUWQ,
17586   IX86_BUILTIN_PHADDUDQ,
17587   IX86_BUILTIN_PHSUBBW,
17588   IX86_BUILTIN_PHSUBWD,
17589   IX86_BUILTIN_PHSUBDQ,
17590   IX86_BUILTIN_PROTB,
17591   IX86_BUILTIN_PROTW,
17592   IX86_BUILTIN_PROTD,
17593   IX86_BUILTIN_PROTQ,
17594   IX86_BUILTIN_PROTB_IMM,
17595   IX86_BUILTIN_PROTW_IMM,
17596   IX86_BUILTIN_PROTD_IMM,
17597   IX86_BUILTIN_PROTQ_IMM,
17598   IX86_BUILTIN_PSHLB,
17599   IX86_BUILTIN_PSHLW,
17600   IX86_BUILTIN_PSHLD,
17601   IX86_BUILTIN_PSHLQ,
17602   IX86_BUILTIN_PSHAB,
17603   IX86_BUILTIN_PSHAW,
17604   IX86_BUILTIN_PSHAD,
17605   IX86_BUILTIN_PSHAQ,
17606   IX86_BUILTIN_FRCZSS,
17607   IX86_BUILTIN_FRCZSD,
17608   IX86_BUILTIN_FRCZPS,
17609   IX86_BUILTIN_FRCZPD,
17610   IX86_BUILTIN_CVTPH2PS,
17611   IX86_BUILTIN_CVTPS2PH,
17612
17613   IX86_BUILTIN_COMEQSS,
17614   IX86_BUILTIN_COMNESS,
17615   IX86_BUILTIN_COMLTSS,
17616   IX86_BUILTIN_COMLESS,
17617   IX86_BUILTIN_COMGTSS,
17618   IX86_BUILTIN_COMGESS,
17619   IX86_BUILTIN_COMUEQSS,
17620   IX86_BUILTIN_COMUNESS,
17621   IX86_BUILTIN_COMULTSS,
17622   IX86_BUILTIN_COMULESS,
17623   IX86_BUILTIN_COMUGTSS,
17624   IX86_BUILTIN_COMUGESS,
17625   IX86_BUILTIN_COMORDSS,
17626   IX86_BUILTIN_COMUNORDSS,
17627   IX86_BUILTIN_COMFALSESS,
17628   IX86_BUILTIN_COMTRUESS,
17629
17630   IX86_BUILTIN_COMEQSD,
17631   IX86_BUILTIN_COMNESD,
17632   IX86_BUILTIN_COMLTSD,
17633   IX86_BUILTIN_COMLESD,
17634   IX86_BUILTIN_COMGTSD,
17635   IX86_BUILTIN_COMGESD,
17636   IX86_BUILTIN_COMUEQSD,
17637   IX86_BUILTIN_COMUNESD,
17638   IX86_BUILTIN_COMULTSD,
17639   IX86_BUILTIN_COMULESD,
17640   IX86_BUILTIN_COMUGTSD,
17641   IX86_BUILTIN_COMUGESD,
17642   IX86_BUILTIN_COMORDSD,
17643   IX86_BUILTIN_COMUNORDSD,
17644   IX86_BUILTIN_COMFALSESD,
17645   IX86_BUILTIN_COMTRUESD,
17646
17647   IX86_BUILTIN_COMEQPS,
17648   IX86_BUILTIN_COMNEPS,
17649   IX86_BUILTIN_COMLTPS,
17650   IX86_BUILTIN_COMLEPS,
17651   IX86_BUILTIN_COMGTPS,
17652   IX86_BUILTIN_COMGEPS,
17653   IX86_BUILTIN_COMUEQPS,
17654   IX86_BUILTIN_COMUNEPS,
17655   IX86_BUILTIN_COMULTPS,
17656   IX86_BUILTIN_COMULEPS,
17657   IX86_BUILTIN_COMUGTPS,
17658   IX86_BUILTIN_COMUGEPS,
17659   IX86_BUILTIN_COMORDPS,
17660   IX86_BUILTIN_COMUNORDPS,
17661   IX86_BUILTIN_COMFALSEPS,
17662   IX86_BUILTIN_COMTRUEPS,
17663
17664   IX86_BUILTIN_COMEQPD,
17665   IX86_BUILTIN_COMNEPD,
17666   IX86_BUILTIN_COMLTPD,
17667   IX86_BUILTIN_COMLEPD,
17668   IX86_BUILTIN_COMGTPD,
17669   IX86_BUILTIN_COMGEPD,
17670   IX86_BUILTIN_COMUEQPD,
17671   IX86_BUILTIN_COMUNEPD,
17672   IX86_BUILTIN_COMULTPD,
17673   IX86_BUILTIN_COMULEPD,
17674   IX86_BUILTIN_COMUGTPD,
17675   IX86_BUILTIN_COMUGEPD,
17676   IX86_BUILTIN_COMORDPD,
17677   IX86_BUILTIN_COMUNORDPD,
17678   IX86_BUILTIN_COMFALSEPD,
17679   IX86_BUILTIN_COMTRUEPD,
17680
17681   IX86_BUILTIN_PCOMEQUB,
17682   IX86_BUILTIN_PCOMNEUB,
17683   IX86_BUILTIN_PCOMLTUB,
17684   IX86_BUILTIN_PCOMLEUB,
17685   IX86_BUILTIN_PCOMGTUB,
17686   IX86_BUILTIN_PCOMGEUB,
17687   IX86_BUILTIN_PCOMFALSEUB,
17688   IX86_BUILTIN_PCOMTRUEUB,
17689   IX86_BUILTIN_PCOMEQUW,
17690   IX86_BUILTIN_PCOMNEUW,
17691   IX86_BUILTIN_PCOMLTUW,
17692   IX86_BUILTIN_PCOMLEUW,
17693   IX86_BUILTIN_PCOMGTUW,
17694   IX86_BUILTIN_PCOMGEUW,
17695   IX86_BUILTIN_PCOMFALSEUW,
17696   IX86_BUILTIN_PCOMTRUEUW,
17697   IX86_BUILTIN_PCOMEQUD,
17698   IX86_BUILTIN_PCOMNEUD,
17699   IX86_BUILTIN_PCOMLTUD,
17700   IX86_BUILTIN_PCOMLEUD,
17701   IX86_BUILTIN_PCOMGTUD,
17702   IX86_BUILTIN_PCOMGEUD,
17703   IX86_BUILTIN_PCOMFALSEUD,
17704   IX86_BUILTIN_PCOMTRUEUD,
17705   IX86_BUILTIN_PCOMEQUQ,
17706   IX86_BUILTIN_PCOMNEUQ,
17707   IX86_BUILTIN_PCOMLTUQ,
17708   IX86_BUILTIN_PCOMLEUQ,
17709   IX86_BUILTIN_PCOMGTUQ,
17710   IX86_BUILTIN_PCOMGEUQ,
17711   IX86_BUILTIN_PCOMFALSEUQ,
17712   IX86_BUILTIN_PCOMTRUEUQ,
17713
17714   IX86_BUILTIN_PCOMEQB,
17715   IX86_BUILTIN_PCOMNEB,
17716   IX86_BUILTIN_PCOMLTB,
17717   IX86_BUILTIN_PCOMLEB,
17718   IX86_BUILTIN_PCOMGTB,
17719   IX86_BUILTIN_PCOMGEB,
17720   IX86_BUILTIN_PCOMFALSEB,
17721   IX86_BUILTIN_PCOMTRUEB,
17722   IX86_BUILTIN_PCOMEQW,
17723   IX86_BUILTIN_PCOMNEW,
17724   IX86_BUILTIN_PCOMLTW,
17725   IX86_BUILTIN_PCOMLEW,
17726   IX86_BUILTIN_PCOMGTW,
17727   IX86_BUILTIN_PCOMGEW,
17728   IX86_BUILTIN_PCOMFALSEW,
17729   IX86_BUILTIN_PCOMTRUEW,
17730   IX86_BUILTIN_PCOMEQD,
17731   IX86_BUILTIN_PCOMNED,
17732   IX86_BUILTIN_PCOMLTD,
17733   IX86_BUILTIN_PCOMLED,
17734   IX86_BUILTIN_PCOMGTD,
17735   IX86_BUILTIN_PCOMGED,
17736   IX86_BUILTIN_PCOMFALSED,
17737   IX86_BUILTIN_PCOMTRUED,
17738   IX86_BUILTIN_PCOMEQQ,
17739   IX86_BUILTIN_PCOMNEQ,
17740   IX86_BUILTIN_PCOMLTQ,
17741   IX86_BUILTIN_PCOMLEQ,
17742   IX86_BUILTIN_PCOMGTQ,
17743   IX86_BUILTIN_PCOMGEQ,
17744   IX86_BUILTIN_PCOMFALSEQ,
17745   IX86_BUILTIN_PCOMTRUEQ,
17746
17747   IX86_BUILTIN_MAX
17748 };
17749
17750 /* Table for the ix86 builtin decls.  */
17751 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
17752
17753 /* Add an ix86 target builtin function with CODE, NAME and TYPE.  Do so,
17754  * if the target_flags include one of MASK.  Stores the function decl
17755  * in the ix86_builtins array.
17756  * Returns the function decl or NULL_TREE, if the builtin was not added.  */
17757
17758 static inline tree
17759 def_builtin (int mask, const char *name, tree type, enum ix86_builtins code)
17760 {
17761   tree decl = NULL_TREE;
17762
17763   if (mask & ix86_isa_flags
17764       && (!(mask & OPTION_MASK_ISA_64BIT) || TARGET_64BIT))
17765     {
17766       decl = add_builtin_function (name, type, code, BUILT_IN_MD,
17767                                    NULL, NULL_TREE);
17768       ix86_builtins[(int) code] = decl;
17769     }
17770
17771   return decl;
17772 }
17773
17774 /* Like def_builtin, but also marks the function decl "const".  */
17775
17776 static inline tree
17777 def_builtin_const (int mask, const char *name, tree type,
17778                    enum ix86_builtins code)
17779 {
17780   tree decl = def_builtin (mask, name, type, code);
17781   if (decl)
17782     TREE_READONLY (decl) = 1;
17783   return decl;
17784 }
17785
17786 /* Bits for builtin_description.flag.  */
17787
17788 /* Set when we don't support the comparison natively, and should
17789    swap_comparison in order to support it.  */
17790 #define BUILTIN_DESC_SWAP_OPERANDS      1
17791
17792 struct builtin_description
17793 {
17794   const unsigned int mask;
17795   const enum insn_code icode;
17796   const char *const name;
17797   const enum ix86_builtins code;
17798   const enum rtx_code comparison;
17799   const int flag;
17800 };
17801
17802 static const struct builtin_description bdesc_comi[] =
17803 {
17804   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
17805   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
17806   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
17807   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
17808   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
17809   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
17810   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
17811   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
17812   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
17813   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
17814   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
17815   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
17816   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
17817   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
17818   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
17819   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
17820   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
17821   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
17822   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
17823   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
17824   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
17825   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
17826   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
17827   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
17828 };
17829
17830 static const struct builtin_description bdesc_ptest[] =
17831 {
17832   /* SSE4.1 */
17833   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestz128", IX86_BUILTIN_PTESTZ, EQ, 0 },
17834   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestc128", IX86_BUILTIN_PTESTC, LTU, 0 },
17835   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_ptest, "__builtin_ia32_ptestnzc128", IX86_BUILTIN_PTESTNZC, GTU, 0 },
17836 };
17837
17838 static const struct builtin_description bdesc_pcmpestr[] =
17839 {
17840   /* SSE4.2 */
17841   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestri128", IX86_BUILTIN_PCMPESTRI128, UNKNOWN, 0 },
17842   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrm128", IX86_BUILTIN_PCMPESTRM128, UNKNOWN, 0 },
17843   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestria128", IX86_BUILTIN_PCMPESTRA128, UNKNOWN, (int) CCAmode },
17844   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestric128", IX86_BUILTIN_PCMPESTRC128, UNKNOWN, (int) CCCmode },
17845   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestrio128", IX86_BUILTIN_PCMPESTRO128, UNKNOWN, (int) CCOmode },
17846   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestris128", IX86_BUILTIN_PCMPESTRS128, UNKNOWN, (int) CCSmode },
17847   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpestr, "__builtin_ia32_pcmpestriz128", IX86_BUILTIN_PCMPESTRZ128, UNKNOWN, (int) CCZmode },
17848 };
17849
17850 static const struct builtin_description bdesc_pcmpistr[] =
17851 {
17852   /* SSE4.2 */
17853   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistri128", IX86_BUILTIN_PCMPISTRI128, UNKNOWN, 0 },
17854   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrm128", IX86_BUILTIN_PCMPISTRM128, UNKNOWN, 0 },
17855   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistria128", IX86_BUILTIN_PCMPISTRA128, UNKNOWN, (int) CCAmode },
17856   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistric128", IX86_BUILTIN_PCMPISTRC128, UNKNOWN, (int) CCCmode },
17857   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistrio128", IX86_BUILTIN_PCMPISTRO128, UNKNOWN, (int) CCOmode },
17858   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistris128", IX86_BUILTIN_PCMPISTRS128, UNKNOWN, (int) CCSmode },
17859   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_pcmpistr, "__builtin_ia32_pcmpistriz128", IX86_BUILTIN_PCMPISTRZ128, UNKNOWN, (int) CCZmode },
17860 };
17861
17862 static const struct builtin_description bdesc_crc32[] =
17863 {
17864   /* SSE4.2 */
17865   { OPTION_MASK_ISA_SSE4_2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse4_2_crc32qi, 0, IX86_BUILTIN_CRC32QI, UNKNOWN, 0 },
17866   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_crc32hi, 0, IX86_BUILTIN_CRC32HI, UNKNOWN, 0 },
17867   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_crc32si, 0, IX86_BUILTIN_CRC32SI, UNKNOWN, 0 },
17868   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_crc32di, 0, IX86_BUILTIN_CRC32DI, UNKNOWN, 0 },
17869 };
17870
17871 /* SSE builtins with 3 arguments and the last argument must be an immediate or xmm0.  */
17872 static const struct builtin_description bdesc_sse_3arg[] =
17873 {
17874   /* SSE4.1 */
17875   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendpd, "__builtin_ia32_blendpd", IX86_BUILTIN_BLENDPD, UNKNOWN, 0 },
17876   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendps, "__builtin_ia32_blendps", IX86_BUILTIN_BLENDPS, UNKNOWN, 0 },
17877   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvpd, "__builtin_ia32_blendvpd", IX86_BUILTIN_BLENDVPD, UNKNOWN, 0 },
17878   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_blendvps, "__builtin_ia32_blendvps", IX86_BUILTIN_BLENDVPS, UNKNOWN, 0 },
17879   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dppd, "__builtin_ia32_dppd", IX86_BUILTIN_DPPD, UNKNOWN, 0 },
17880   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_dpps, "__builtin_ia32_dpps", IX86_BUILTIN_DPPS, UNKNOWN, 0 },
17881   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_insertps, "__builtin_ia32_insertps128", IX86_BUILTIN_INSERTPS128, UNKNOWN, 0 },
17882   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mpsadbw, "__builtin_ia32_mpsadbw128", IX86_BUILTIN_MPSADBW128, UNKNOWN, 0 },
17883   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendvb, "__builtin_ia32_pblendvb128", IX86_BUILTIN_PBLENDVB128, UNKNOWN, 0 },
17884   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_pblendw, "__builtin_ia32_pblendw128", IX86_BUILTIN_PBLENDW128, UNKNOWN, 0 },
17885   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundsd, 0, IX86_BUILTIN_ROUNDSD, UNKNOWN, 0 },
17886   { OPTION_MASK_ISA_ROUND, CODE_FOR_sse4_1_roundss, 0, IX86_BUILTIN_ROUNDSS, UNKNOWN, 0 },
17887 };
17888
17889 static const struct builtin_description bdesc_2arg[] =
17890 {
17891   /* SSE */
17892   { OPTION_MASK_ISA_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, UNKNOWN, 0 },
17893   { OPTION_MASK_ISA_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, UNKNOWN, 0 },
17894   { OPTION_MASK_ISA_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, UNKNOWN, 0 },
17895   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, UNKNOWN, 0 },
17896   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, UNKNOWN, 0 },
17897   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, UNKNOWN, 0 },
17898   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, UNKNOWN, 0 },
17899   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, UNKNOWN, 0 },
17900
17901   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
17902   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
17903   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
17904   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT, BUILTIN_DESC_SWAP_OPERANDS },
17905   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE, BUILTIN_DESC_SWAP_OPERANDS },
17906   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
17907   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
17908   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
17909   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
17910   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE, BUILTIN_DESC_SWAP_OPERANDS },
17911   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT, BUILTIN_DESC_SWAP_OPERANDS },
17912   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
17913   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
17914   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
17915   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
17916   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
17917   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
17918   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
17919   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
17920   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE, BUILTIN_DESC_SWAP_OPERANDS },
17921   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT, BUILTIN_DESC_SWAP_OPERANDS },
17922   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, ORDERED, 0 },
17923
17924   { OPTION_MASK_ISA_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, UNKNOWN, 0 },
17925   { OPTION_MASK_ISA_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, UNKNOWN, 0 },
17926   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, UNKNOWN, 0 },
17927   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, UNKNOWN, 0 },
17928
17929   { OPTION_MASK_ISA_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, UNKNOWN, 0 },
17930   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, UNKNOWN, 0 },
17931   { OPTION_MASK_ISA_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, UNKNOWN, 0 },
17932   { OPTION_MASK_ISA_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, UNKNOWN, 0 },
17933
17934   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, UNKNOWN, 0 },
17935   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, UNKNOWN, 0 },
17936   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, UNKNOWN, 0 },
17937   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, UNKNOWN, 0 },
17938   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, UNKNOWN, 0 },
17939
17940   /* MMX */
17941   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, UNKNOWN, 0 },
17942   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, UNKNOWN, 0 },
17943   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, UNKNOWN, 0 },
17944   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, UNKNOWN, 0 },
17945   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, UNKNOWN, 0 },
17946   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, UNKNOWN, 0 },
17947   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, UNKNOWN, 0 },
17948   { OPTION_MASK_ISA_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, UNKNOWN, 0 },
17949
17950   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, UNKNOWN, 0 },
17951   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, UNKNOWN, 0 },
17952   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, UNKNOWN, 0 },
17953   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, UNKNOWN, 0 },
17954   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, UNKNOWN, 0 },
17955   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, UNKNOWN, 0 },
17956   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, UNKNOWN, 0 },
17957   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, UNKNOWN, 0 },
17958
17959   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, UNKNOWN, 0 },
17960   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, UNKNOWN, 0 },
17961   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, UNKNOWN, 0 },
17962
17963   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, UNKNOWN, 0 },
17964   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, UNKNOWN, 0 },
17965   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, UNKNOWN, 0 },
17966   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, UNKNOWN, 0 },
17967
17968   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, UNKNOWN, 0 },
17969   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, UNKNOWN, 0 },
17970
17971   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, UNKNOWN, 0 },
17972   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, UNKNOWN, 0 },
17973   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, UNKNOWN, 0 },
17974   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, UNKNOWN, 0 },
17975   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, UNKNOWN, 0 },
17976   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, UNKNOWN, 0 },
17977
17978   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, UNKNOWN, 0 },
17979   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, UNKNOWN, 0 },
17980   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, UNKNOWN, 0 },
17981   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, UNKNOWN, 0 },
17982
17983   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, UNKNOWN, 0 },
17984   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, UNKNOWN, 0 },
17985   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, UNKNOWN, 0 },
17986   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, UNKNOWN, 0 },
17987   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, UNKNOWN, 0 },
17988   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, UNKNOWN, 0 },
17989
17990   /* Special.  */
17991   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, UNKNOWN, 0 },
17992   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, UNKNOWN, 0 },
17993   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, UNKNOWN, 0 },
17994
17995   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, UNKNOWN, 0 },
17996   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, UNKNOWN, 0 },
17997   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, UNKNOWN, 0 },
17998
17999   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, UNKNOWN, 0 },
18000   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, UNKNOWN, 0 },
18001   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, UNKNOWN, 0 },
18002   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, UNKNOWN, 0 },
18003   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, UNKNOWN, 0 },
18004   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, UNKNOWN, 0 },
18005
18006   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, UNKNOWN, 0 },
18007   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, UNKNOWN, 0 },
18008   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, UNKNOWN, 0 },
18009   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, UNKNOWN, 0 },
18010   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, UNKNOWN, 0 },
18011   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, UNKNOWN, 0 },
18012
18013   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, UNKNOWN, 0 },
18014   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, UNKNOWN, 0 },
18015   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, UNKNOWN, 0 },
18016   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, UNKNOWN, 0 },
18017
18018   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, UNKNOWN, 0 },
18019   { OPTION_MASK_ISA_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, UNKNOWN, 0 },
18020
18021   /* SSE2 */
18022   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, UNKNOWN, 0 },
18023   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, UNKNOWN, 0 },
18024   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, UNKNOWN, 0 },
18025   { OPTION_MASK_ISA_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, UNKNOWN, 0 },
18026   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, UNKNOWN, 0 },
18027   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, UNKNOWN, 0 },
18028   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, UNKNOWN, 0 },
18029   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, UNKNOWN, 0 },
18030
18031   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
18032   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
18033   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
18034   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT, BUILTIN_DESC_SWAP_OPERANDS },
18035   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE, BUILTIN_DESC_SWAP_OPERANDS },
18036   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
18037   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
18038   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
18039   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
18040   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE, BUILTIN_DESC_SWAP_OPERANDS },
18041   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT, BUILTIN_DESC_SWAP_OPERANDS },
18042   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
18043   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
18044   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
18045   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
18046   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
18047   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
18048   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
18049   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
18050   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
18051
18052   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, UNKNOWN, 0 },
18053   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, UNKNOWN, 0 },
18054   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, UNKNOWN, 0 },
18055   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, UNKNOWN, 0 },
18056
18057   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, UNKNOWN, 0 },
18058   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, UNKNOWN, 0 },
18059   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, UNKNOWN, 0 },
18060   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, UNKNOWN, 0 },
18061
18062   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, UNKNOWN, 0 },
18063   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, UNKNOWN, 0 },
18064   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, UNKNOWN, 0 },
18065
18066   { OPTION_MASK_ISA_SSE2, CODE_FOR_vec_pack_sfix_v2df, "__builtin_ia32_vec_pack_sfix", IX86_BUILTIN_VEC_PACK_SFIX, UNKNOWN, 0 },
18067
18068   /* SSE2 MMX */
18069   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, UNKNOWN, 0 },
18070   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, UNKNOWN, 0 },
18071   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, UNKNOWN, 0 },
18072   { OPTION_MASK_ISA_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, UNKNOWN, 0 },
18073   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, UNKNOWN, 0 },
18074   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, UNKNOWN, 0 },
18075   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, UNKNOWN, 0 },
18076   { OPTION_MASK_ISA_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, UNKNOWN, 0 },
18077
18078   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, UNKNOWN, 0 },
18079   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, UNKNOWN, 0 },
18080   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, UNKNOWN, 0 },
18081   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, UNKNOWN, 0 },
18082   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, UNKNOWN, 0 },
18083   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, UNKNOWN, 0 },
18084   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, UNKNOWN, 0 },
18085   { OPTION_MASK_ISA_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, UNKNOWN, 0 },
18086
18087   { OPTION_MASK_ISA_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, UNKNOWN, 0 },
18088   { OPTION_MASK_ISA_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, UNKNOWN, 0 },
18089
18090   { OPTION_MASK_ISA_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, UNKNOWN, 0 },
18091   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, UNKNOWN, 0 },
18092   { OPTION_MASK_ISA_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, UNKNOWN, 0 },
18093   { OPTION_MASK_ISA_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, UNKNOWN, 0 },
18094
18095   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, UNKNOWN, 0 },
18096   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, UNKNOWN, 0 },
18097
18098   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, UNKNOWN, 0 },
18099   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, UNKNOWN, 0 },
18100   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, UNKNOWN, 0 },
18101   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, UNKNOWN, 0 },
18102   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, UNKNOWN, 0 },
18103   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, UNKNOWN, 0 },
18104
18105   { OPTION_MASK_ISA_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, UNKNOWN, 0 },
18106   { OPTION_MASK_ISA_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, UNKNOWN, 0 },
18107   { OPTION_MASK_ISA_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, UNKNOWN, 0 },
18108   { OPTION_MASK_ISA_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, UNKNOWN, 0 },
18109
18110   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, UNKNOWN, 0 },
18111   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, UNKNOWN, 0 },
18112   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, UNKNOWN, 0 },
18113   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, UNKNOWN, 0 },
18114   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, UNKNOWN, 0 },
18115   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, UNKNOWN, 0 },
18116   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, UNKNOWN, 0 },
18117   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, UNKNOWN, 0 },
18118
18119   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, UNKNOWN, 0 },
18120   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, UNKNOWN, 0 },
18121   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, UNKNOWN, 0 },
18122
18123   { OPTION_MASK_ISA_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, UNKNOWN, 0 },
18124   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, UNKNOWN, 0 },
18125
18126   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, UNKNOWN, 0 },
18127   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, UNKNOWN, 0 },
18128
18129   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, UNKNOWN, 0 },
18130   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, UNKNOWN, 0 },
18131   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, UNKNOWN, 0 },
18132
18133   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, UNKNOWN, 0 },
18134   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, UNKNOWN, 0 },
18135   { OPTION_MASK_ISA_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, UNKNOWN, 0 },
18136
18137   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, UNKNOWN, 0 },
18138   { OPTION_MASK_ISA_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, UNKNOWN, 0 },
18139
18140   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, UNKNOWN, 0 },
18141
18142   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, UNKNOWN, 0 },
18143   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, UNKNOWN, 0 },
18144   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, UNKNOWN, 0 },
18145   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, UNKNOWN, 0 },
18146
18147   /* SSE3 MMX */
18148   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, UNKNOWN, 0 },
18149   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, UNKNOWN, 0 },
18150   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, UNKNOWN, 0 },
18151   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, UNKNOWN, 0 },
18152   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, UNKNOWN, 0 },
18153   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, UNKNOWN, 0 },
18154
18155   /* SSSE3 */
18156   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, UNKNOWN, 0 },
18157   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, UNKNOWN, 0 },
18158   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, UNKNOWN, 0 },
18159   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, UNKNOWN, 0 },
18160   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, UNKNOWN, 0 },
18161   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, UNKNOWN, 0 },
18162   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, UNKNOWN, 0 },
18163   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, UNKNOWN, 0 },
18164   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, UNKNOWN, 0 },
18165   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, UNKNOWN, 0 },
18166   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, UNKNOWN, 0 },
18167   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, UNKNOWN, 0 },
18168   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, UNKNOWN, 0 },
18169   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, UNKNOWN, 0 },
18170   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, UNKNOWN, 0 },
18171   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, UNKNOWN, 0 },
18172   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, UNKNOWN, 0 },
18173   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, UNKNOWN, 0 },
18174   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, UNKNOWN, 0 },
18175   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, UNKNOWN, 0 },
18176   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, UNKNOWN, 0 },
18177   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, UNKNOWN, 0 },
18178   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, UNKNOWN, 0 },
18179   { OPTION_MASK_ISA_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, UNKNOWN, 0 },
18180
18181   /* SSE4.1 */
18182   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_packusdw, "__builtin_ia32_packusdw128", IX86_BUILTIN_PACKUSDW128, UNKNOWN, 0 },
18183   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_eqv2di3, "__builtin_ia32_pcmpeqq", IX86_BUILTIN_PCMPEQQ, UNKNOWN, 0 },
18184   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv16qi3, "__builtin_ia32_pmaxsb128", IX86_BUILTIN_PMAXSB128, UNKNOWN, 0 },
18185   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_smaxv4si3, "__builtin_ia32_pmaxsd128", IX86_BUILTIN_PMAXSD128, UNKNOWN, 0 },
18186   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv4si3, "__builtin_ia32_pmaxud128", IX86_BUILTIN_PMAXUD128, UNKNOWN, 0 },
18187   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_umaxv8hi3, "__builtin_ia32_pmaxuw128", IX86_BUILTIN_PMAXUW128, UNKNOWN, 0 },
18188   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv16qi3, "__builtin_ia32_pminsb128", IX86_BUILTIN_PMINSB128, UNKNOWN, 0 },
18189   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sminv4si3, "__builtin_ia32_pminsd128", IX86_BUILTIN_PMINSD128, UNKNOWN, 0 },
18190   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv4si3, "__builtin_ia32_pminud128", IX86_BUILTIN_PMINUD128, UNKNOWN, 0 },
18191   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_uminv8hi3, "__builtin_ia32_pminuw128", IX86_BUILTIN_PMINUW128, UNKNOWN, 0 },
18192   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_mulv2siv2di3, 0, IX86_BUILTIN_PMULDQ128, UNKNOWN, 0 },
18193   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_mulv4si3, "__builtin_ia32_pmulld128", IX86_BUILTIN_PMULLD128, UNKNOWN, 0 },
18194
18195   /* SSE4.2 */
18196   { OPTION_MASK_ISA_SSE4_2, CODE_FOR_sse4_2_gtv2di3, "__builtin_ia32_pcmpgtq", IX86_BUILTIN_PCMPGTQ, UNKNOWN, 0 },
18197 };
18198
18199 static const struct builtin_description bdesc_1arg[] =
18200 {
18201   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, UNKNOWN, 0 },
18202   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, UNKNOWN, 0 },
18203
18204   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, UNKNOWN, 0 },
18205   { OPTION_MASK_ISA_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS_NR, UNKNOWN, 0 },
18206   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, UNKNOWN, 0 },
18207   { OPTION_MASK_ISA_SSE, CODE_FOR_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS_NR, UNKNOWN, 0 },
18208   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, UNKNOWN, 0 },
18209
18210   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, UNKNOWN, 0 },
18211   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, UNKNOWN, 0 },
18212   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, UNKNOWN, 0 },
18213   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, UNKNOWN, 0 },
18214   { OPTION_MASK_ISA_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, UNKNOWN, 0 },
18215   { OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, UNKNOWN, 0 },
18216
18217   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, UNKNOWN, 0 },
18218   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, UNKNOWN, 0 },
18219
18220   { OPTION_MASK_ISA_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, UNKNOWN, 0 },
18221
18222   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, UNKNOWN, 0 },
18223   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, UNKNOWN, 0 },
18224
18225   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, UNKNOWN, 0 },
18226   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, UNKNOWN, 0 },
18227   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, UNKNOWN, 0 },
18228   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, UNKNOWN, 0 },
18229   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, UNKNOWN, 0 },
18230
18231   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, UNKNOWN, 0 },
18232
18233   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, UNKNOWN, 0 },
18234   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, UNKNOWN, 0 },
18235   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, UNKNOWN, 0 },
18236   { OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, UNKNOWN, 0 },
18237
18238   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, UNKNOWN, 0 },
18239   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, UNKNOWN, 0 },
18240   { OPTION_MASK_ISA_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, UNKNOWN, 0 },
18241
18242   /* SSE3 */
18243   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, UNKNOWN, 0 },
18244   { OPTION_MASK_ISA_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, UNKNOWN, 0 },
18245
18246   /* SSSE3 */
18247   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, UNKNOWN, 0 },
18248   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, UNKNOWN, 0 },
18249   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, UNKNOWN, 0 },
18250   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, UNKNOWN, 0 },
18251   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, UNKNOWN, 0 },
18252   { OPTION_MASK_ISA_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, UNKNOWN, 0 },
18253
18254   /* SSE4.1 */
18255   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv8qiv8hi2, 0, IX86_BUILTIN_PMOVSXBW128, UNKNOWN, 0 },
18256   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4qiv4si2, 0, IX86_BUILTIN_PMOVSXBD128, UNKNOWN, 0 },
18257   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2qiv2di2, 0, IX86_BUILTIN_PMOVSXBQ128, UNKNOWN, 0 },
18258   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv4hiv4si2, 0, IX86_BUILTIN_PMOVSXWD128, UNKNOWN, 0 },
18259   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2hiv2di2, 0, IX86_BUILTIN_PMOVSXWQ128, UNKNOWN, 0 },
18260   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_extendv2siv2di2, 0, IX86_BUILTIN_PMOVSXDQ128, UNKNOWN, 0 },
18261   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv8qiv8hi2, 0, IX86_BUILTIN_PMOVZXBW128, UNKNOWN, 0 },
18262   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4qiv4si2, 0, IX86_BUILTIN_PMOVZXBD128, UNKNOWN, 0 },
18263   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2qiv2di2, 0, IX86_BUILTIN_PMOVZXBQ128, UNKNOWN, 0 },
18264   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv4hiv4si2, 0, IX86_BUILTIN_PMOVZXWD128, UNKNOWN, 0 },
18265   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2hiv2di2, 0, IX86_BUILTIN_PMOVZXWQ128, UNKNOWN, 0 },
18266   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_zero_extendv2siv2di2, 0, IX86_BUILTIN_PMOVZXDQ128, UNKNOWN, 0 },
18267   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_phminposuw, "__builtin_ia32_phminposuw128", IX86_BUILTIN_PHMINPOSUW128, UNKNOWN, 0 },
18268
18269   /* Fake 1 arg builtins with a constant smaller than 8 bits as the 2nd arg.  */
18270   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_roundpd, 0, IX86_BUILTIN_ROUNDPD, UNKNOWN, 0 },
18271   { OPTION_MASK_ISA_SSE4_1, CODE_FOR_sse4_1_roundps, 0, IX86_BUILTIN_ROUNDPS, UNKNOWN, 0 },
18272 };
18273
18274 /* SSE5 */
18275 enum multi_arg_type {
18276   MULTI_ARG_UNKNOWN,
18277   MULTI_ARG_3_SF,
18278   MULTI_ARG_3_DF,
18279   MULTI_ARG_3_DI,
18280   MULTI_ARG_3_SI,
18281   MULTI_ARG_3_SI_DI,
18282   MULTI_ARG_3_HI,
18283   MULTI_ARG_3_HI_SI,
18284   MULTI_ARG_3_QI,
18285   MULTI_ARG_3_PERMPS,
18286   MULTI_ARG_3_PERMPD,
18287   MULTI_ARG_2_SF,
18288   MULTI_ARG_2_DF,
18289   MULTI_ARG_2_DI,
18290   MULTI_ARG_2_SI,
18291   MULTI_ARG_2_HI,
18292   MULTI_ARG_2_QI,
18293   MULTI_ARG_2_DI_IMM,
18294   MULTI_ARG_2_SI_IMM,
18295   MULTI_ARG_2_HI_IMM,
18296   MULTI_ARG_2_QI_IMM,
18297   MULTI_ARG_2_SF_CMP,
18298   MULTI_ARG_2_DF_CMP,
18299   MULTI_ARG_2_DI_CMP,
18300   MULTI_ARG_2_SI_CMP,
18301   MULTI_ARG_2_HI_CMP,
18302   MULTI_ARG_2_QI_CMP,
18303   MULTI_ARG_2_DI_TF,
18304   MULTI_ARG_2_SI_TF,
18305   MULTI_ARG_2_HI_TF,
18306   MULTI_ARG_2_QI_TF,
18307   MULTI_ARG_2_SF_TF,
18308   MULTI_ARG_2_DF_TF,
18309   MULTI_ARG_1_SF,
18310   MULTI_ARG_1_DF,
18311   MULTI_ARG_1_DI,
18312   MULTI_ARG_1_SI,
18313   MULTI_ARG_1_HI,
18314   MULTI_ARG_1_QI,
18315   MULTI_ARG_1_SI_DI,
18316   MULTI_ARG_1_HI_DI,
18317   MULTI_ARG_1_HI_SI,
18318   MULTI_ARG_1_QI_DI,
18319   MULTI_ARG_1_QI_SI,
18320   MULTI_ARG_1_QI_HI,
18321   MULTI_ARG_1_PH2PS,
18322   MULTI_ARG_1_PS2PH
18323 };
18324
18325 static const struct builtin_description bdesc_multi_arg[] =
18326 {
18327   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfmaddv4sf4,     "__builtin_ia32_fmaddss",    IX86_BUILTIN_FMADDSS,    0,            (int)MULTI_ARG_3_SF },
18328   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfmaddv2df4,     "__builtin_ia32_fmaddsd",    IX86_BUILTIN_FMADDSD,    0,            (int)MULTI_ARG_3_DF },
18329   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fmaddv4sf4,       "__builtin_ia32_fmaddps",    IX86_BUILTIN_FMADDPS,    0,            (int)MULTI_ARG_3_SF },
18330   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fmaddv2df4,       "__builtin_ia32_fmaddpd",    IX86_BUILTIN_FMADDPD,    0,            (int)MULTI_ARG_3_DF },
18331   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfmsubv4sf4,     "__builtin_ia32_fmsubss",    IX86_BUILTIN_FMSUBSS,    0,            (int)MULTI_ARG_3_SF },
18332   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfmsubv2df4,     "__builtin_ia32_fmsubsd",    IX86_BUILTIN_FMSUBSD,    0,            (int)MULTI_ARG_3_DF },
18333   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fmsubv4sf4,       "__builtin_ia32_fmsubps",    IX86_BUILTIN_FMSUBPS,    0,            (int)MULTI_ARG_3_SF },
18334   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fmsubv2df4,       "__builtin_ia32_fmsubpd",    IX86_BUILTIN_FMSUBPD,    0,            (int)MULTI_ARG_3_DF },
18335   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfnmaddv4sf4,    "__builtin_ia32_fnmaddss",   IX86_BUILTIN_FNMADDSS,   0,            (int)MULTI_ARG_3_SF },
18336   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfnmaddv2df4,    "__builtin_ia32_fnmaddsd",   IX86_BUILTIN_FNMADDSD,   0,            (int)MULTI_ARG_3_DF },
18337   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fnmaddv4sf4,      "__builtin_ia32_fnmaddps",   IX86_BUILTIN_FNMADDPS,   0,            (int)MULTI_ARG_3_SF },
18338   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fnmaddv2df4,      "__builtin_ia32_fnmaddpd",   IX86_BUILTIN_FNMADDPD,   0,            (int)MULTI_ARG_3_DF },
18339   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfnmsubv4sf4,    "__builtin_ia32_fnmsubss",   IX86_BUILTIN_FNMSUBSS,   0,            (int)MULTI_ARG_3_SF },
18340   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_vmfnmsubv2df4,    "__builtin_ia32_fnmsubsd",   IX86_BUILTIN_FNMSUBSD,   0,            (int)MULTI_ARG_3_DF },
18341   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fnmsubv4sf4,      "__builtin_ia32_fnmsubps",   IX86_BUILTIN_FNMSUBPS,   0,            (int)MULTI_ARG_3_SF },
18342   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5i_fnmsubv2df4,      "__builtin_ia32_fnmsubpd",   IX86_BUILTIN_FNMSUBPD,   0,            (int)MULTI_ARG_3_DF },
18343   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v2di,        "__builtin_ia32_pcmov",      IX86_BUILTIN_PCMOV_V2DI, 0,            (int)MULTI_ARG_3_DI },
18344   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v2di,        "__builtin_ia32_pcmov_v2di", IX86_BUILTIN_PCMOV_V2DI, 0,            (int)MULTI_ARG_3_DI },
18345   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v4si,        "__builtin_ia32_pcmov_v4si", IX86_BUILTIN_PCMOV_V4SI, 0,            (int)MULTI_ARG_3_SI },
18346   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v8hi,        "__builtin_ia32_pcmov_v8hi", IX86_BUILTIN_PCMOV_V8HI, 0,            (int)MULTI_ARG_3_HI },
18347   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v16qi,       "__builtin_ia32_pcmov_v16qi",IX86_BUILTIN_PCMOV_V16QI,0,            (int)MULTI_ARG_3_QI },
18348   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v2df,        "__builtin_ia32_pcmov_v2df", IX86_BUILTIN_PCMOV_V2DF, 0,            (int)MULTI_ARG_3_DF },
18349   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcmov_v4sf,        "__builtin_ia32_pcmov_v4sf", IX86_BUILTIN_PCMOV_V4SF, 0,            (int)MULTI_ARG_3_SF },
18350   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pperm,             "__builtin_ia32_pperm",      IX86_BUILTIN_PPERM,      0,            (int)MULTI_ARG_3_QI },
18351   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_permv4sf,          "__builtin_ia32_permps",     IX86_BUILTIN_PERMPS,     0,            (int)MULTI_ARG_3_PERMPS },
18352   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_permv2df,          "__builtin_ia32_permpd",     IX86_BUILTIN_PERMPD,     0,            (int)MULTI_ARG_3_PERMPD },
18353   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacssww,          "__builtin_ia32_pmacssww",   IX86_BUILTIN_PMACSSWW,   0,            (int)MULTI_ARG_3_HI },
18354   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacsww,           "__builtin_ia32_pmacsww",    IX86_BUILTIN_PMACSWW,    0,            (int)MULTI_ARG_3_HI },
18355   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacsswd,          "__builtin_ia32_pmacsswd",   IX86_BUILTIN_PMACSSWD,   0,            (int)MULTI_ARG_3_HI_SI },
18356   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacswd,           "__builtin_ia32_pmacswd",    IX86_BUILTIN_PMACSWD,    0,            (int)MULTI_ARG_3_HI_SI },
18357   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacssdd,          "__builtin_ia32_pmacssdd",   IX86_BUILTIN_PMACSSDD,   0,            (int)MULTI_ARG_3_SI },
18358   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacsdd,           "__builtin_ia32_pmacsdd",    IX86_BUILTIN_PMACSDD,    0,            (int)MULTI_ARG_3_SI },
18359   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacssdql,         "__builtin_ia32_pmacssdql",  IX86_BUILTIN_PMACSSDQL,  0,            (int)MULTI_ARG_3_SI_DI },
18360   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacssdqh,         "__builtin_ia32_pmacssdqh",  IX86_BUILTIN_PMACSSDQH,  0,            (int)MULTI_ARG_3_SI_DI },
18361   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacsdql,          "__builtin_ia32_pmacsdql",   IX86_BUILTIN_PMACSDQL,   0,            (int)MULTI_ARG_3_SI_DI },
18362   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmacsdqh,          "__builtin_ia32_pmacsdqh",   IX86_BUILTIN_PMACSDQH,   0,            (int)MULTI_ARG_3_SI_DI },
18363   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmadcsswd,         "__builtin_ia32_pmadcsswd",  IX86_BUILTIN_PMADCSSWD,  0,            (int)MULTI_ARG_3_HI_SI },
18364   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pmadcswd,          "__builtin_ia32_pmadcswd",   IX86_BUILTIN_PMADCSWD,   0,            (int)MULTI_ARG_3_HI_SI },
18365   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_rotlv2di3,         "__builtin_ia32_protq",      IX86_BUILTIN_PROTQ,      0,            (int)MULTI_ARG_2_DI },
18366   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_rotlv4si3,         "__builtin_ia32_protd",      IX86_BUILTIN_PROTD,      0,            (int)MULTI_ARG_2_SI },
18367   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_rotlv8hi3,         "__builtin_ia32_protw",      IX86_BUILTIN_PROTW,      0,            (int)MULTI_ARG_2_HI },
18368   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_rotlv16qi3,        "__builtin_ia32_protb",      IX86_BUILTIN_PROTB,      0,            (int)MULTI_ARG_2_QI },
18369   { OPTION_MASK_ISA_SSE5, CODE_FOR_rotlv2di3,              "__builtin_ia32_protqi",     IX86_BUILTIN_PROTQ_IMM,  0,            (int)MULTI_ARG_2_DI_IMM },
18370   { OPTION_MASK_ISA_SSE5, CODE_FOR_rotlv4si3,              "__builtin_ia32_protdi",     IX86_BUILTIN_PROTD_IMM,  0,            (int)MULTI_ARG_2_SI_IMM },
18371   { OPTION_MASK_ISA_SSE5, CODE_FOR_rotlv8hi3,              "__builtin_ia32_protwi",     IX86_BUILTIN_PROTW_IMM,  0,            (int)MULTI_ARG_2_HI_IMM },
18372   { OPTION_MASK_ISA_SSE5, CODE_FOR_rotlv16qi3,             "__builtin_ia32_protbi",     IX86_BUILTIN_PROTB_IMM,  0,            (int)MULTI_ARG_2_QI_IMM },
18373   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_ashlv2di3,         "__builtin_ia32_pshaq",      IX86_BUILTIN_PSHAQ,      0,            (int)MULTI_ARG_2_DI },
18374   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_ashlv4si3,         "__builtin_ia32_pshad",      IX86_BUILTIN_PSHAD,      0,            (int)MULTI_ARG_2_SI },
18375   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_ashlv8hi3,         "__builtin_ia32_pshaw",      IX86_BUILTIN_PSHAW,      0,            (int)MULTI_ARG_2_HI },
18376   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_ashlv16qi3,        "__builtin_ia32_pshab",      IX86_BUILTIN_PSHAB,      0,            (int)MULTI_ARG_2_QI },
18377   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_lshlv2di3,         "__builtin_ia32_pshlq",      IX86_BUILTIN_PSHLQ,      0,            (int)MULTI_ARG_2_DI },
18378   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_lshlv4si3,         "__builtin_ia32_pshld",      IX86_BUILTIN_PSHLD,      0,            (int)MULTI_ARG_2_SI },
18379   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_lshlv8hi3,         "__builtin_ia32_pshlw",      IX86_BUILTIN_PSHLW,      0,            (int)MULTI_ARG_2_HI },
18380   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_lshlv16qi3,        "__builtin_ia32_pshlb",      IX86_BUILTIN_PSHLB,      0,            (int)MULTI_ARG_2_QI },
18381   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmfrczv4sf2,       "__builtin_ia32_frczss",     IX86_BUILTIN_FRCZSS,     0,            (int)MULTI_ARG_2_SF },
18382   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmfrczv2df2,       "__builtin_ia32_frczsd",     IX86_BUILTIN_FRCZSD,     0,            (int)MULTI_ARG_2_DF },
18383   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_frczv4sf2,         "__builtin_ia32_frczps",     IX86_BUILTIN_FRCZPS,     0,            (int)MULTI_ARG_1_SF },
18384   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_frczv2df2,         "__builtin_ia32_frczpd",     IX86_BUILTIN_FRCZPD,     0,            (int)MULTI_ARG_1_DF },
18385   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_cvtph2ps,          "__builtin_ia32_cvtph2ps",   IX86_BUILTIN_CVTPH2PS,   0,            (int)MULTI_ARG_1_PH2PS },
18386   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_cvtps2ph,          "__builtin_ia32_cvtps2ph",   IX86_BUILTIN_CVTPS2PH,   0,            (int)MULTI_ARG_1_PS2PH },
18387   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddbw,           "__builtin_ia32_phaddbw",    IX86_BUILTIN_PHADDBW,    0,            (int)MULTI_ARG_1_QI_HI },
18388   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddbd,           "__builtin_ia32_phaddbd",    IX86_BUILTIN_PHADDBD,    0,            (int)MULTI_ARG_1_QI_SI },
18389   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddbq,           "__builtin_ia32_phaddbq",    IX86_BUILTIN_PHADDBQ,    0,            (int)MULTI_ARG_1_QI_DI },
18390   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddwd,           "__builtin_ia32_phaddwd",    IX86_BUILTIN_PHADDWD,    0,            (int)MULTI_ARG_1_HI_SI },
18391   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddwq,           "__builtin_ia32_phaddwq",    IX86_BUILTIN_PHADDWQ,    0,            (int)MULTI_ARG_1_HI_DI },
18392   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phadddq,           "__builtin_ia32_phadddq",    IX86_BUILTIN_PHADDDQ,    0,            (int)MULTI_ARG_1_SI_DI },
18393   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddubw,          "__builtin_ia32_phaddubw",   IX86_BUILTIN_PHADDUBW,   0,            (int)MULTI_ARG_1_QI_HI },
18394   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddubd,          "__builtin_ia32_phaddubd",   IX86_BUILTIN_PHADDUBD,   0,            (int)MULTI_ARG_1_QI_SI },
18395   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddubq,          "__builtin_ia32_phaddubq",   IX86_BUILTIN_PHADDUBQ,   0,            (int)MULTI_ARG_1_QI_DI },
18396   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phadduwd,          "__builtin_ia32_phadduwd",   IX86_BUILTIN_PHADDUWD,   0,            (int)MULTI_ARG_1_HI_SI },
18397   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phadduwq,          "__builtin_ia32_phadduwq",   IX86_BUILTIN_PHADDUWQ,   0,            (int)MULTI_ARG_1_HI_DI },
18398   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phaddudq,          "__builtin_ia32_phaddudq",   IX86_BUILTIN_PHADDUDQ,   0,            (int)MULTI_ARG_1_SI_DI },
18399   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phsubbw,           "__builtin_ia32_phsubbw",    IX86_BUILTIN_PHSUBBW,    0,            (int)MULTI_ARG_1_QI_HI },
18400   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phsubwd,           "__builtin_ia32_phsubwd",    IX86_BUILTIN_PHSUBWD,    0,            (int)MULTI_ARG_1_HI_SI },
18401   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_phsubdq,           "__builtin_ia32_phsubdq",    IX86_BUILTIN_PHSUBDQ,    0,            (int)MULTI_ARG_1_SI_DI },
18402
18403   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comeqss",    IX86_BUILTIN_COMEQSS,    EQ,           (int)MULTI_ARG_2_SF_CMP },
18404   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comness",    IX86_BUILTIN_COMNESS,    NE,           (int)MULTI_ARG_2_SF_CMP },
18405   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comneqss",   IX86_BUILTIN_COMNESS,    NE,           (int)MULTI_ARG_2_SF_CMP },
18406   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comltss",    IX86_BUILTIN_COMLTSS,    LT,           (int)MULTI_ARG_2_SF_CMP },
18407   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comless",    IX86_BUILTIN_COMLESS,    LE,           (int)MULTI_ARG_2_SF_CMP },
18408   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comgtss",    IX86_BUILTIN_COMGTSS,    GT,           (int)MULTI_ARG_2_SF_CMP },
18409   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comgess",    IX86_BUILTIN_COMGESS,    GE,           (int)MULTI_ARG_2_SF_CMP },
18410   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comueqss",   IX86_BUILTIN_COMUEQSS,   UNEQ,         (int)MULTI_ARG_2_SF_CMP },
18411   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comuness",   IX86_BUILTIN_COMUNESS,   LTGT,         (int)MULTI_ARG_2_SF_CMP },
18412   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comuneqss",  IX86_BUILTIN_COMUNESS,   LTGT,         (int)MULTI_ARG_2_SF_CMP },
18413   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comunltss",  IX86_BUILTIN_COMULTSS,   UNLT,         (int)MULTI_ARG_2_SF_CMP },
18414   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comunless",  IX86_BUILTIN_COMULESS,   UNLE,         (int)MULTI_ARG_2_SF_CMP },
18415   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comungtss",  IX86_BUILTIN_COMUGTSS,   UNGT,         (int)MULTI_ARG_2_SF_CMP },
18416   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comungess",  IX86_BUILTIN_COMUGESS,   UNGE,         (int)MULTI_ARG_2_SF_CMP },
18417   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comordss",   IX86_BUILTIN_COMORDSS,   ORDERED,      (int)MULTI_ARG_2_SF_CMP },
18418   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv4sf3,    "__builtin_ia32_comunordss", IX86_BUILTIN_COMUNORDSS, UNORDERED,    (int)MULTI_ARG_2_SF_CMP },
18419
18420   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comeqsd",    IX86_BUILTIN_COMEQSD,    EQ,           (int)MULTI_ARG_2_DF_CMP },
18421   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comnesd",    IX86_BUILTIN_COMNESD,    NE,           (int)MULTI_ARG_2_DF_CMP },
18422   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comneqsd",   IX86_BUILTIN_COMNESD,    NE,           (int)MULTI_ARG_2_DF_CMP },
18423   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comltsd",    IX86_BUILTIN_COMLTSD,    LT,           (int)MULTI_ARG_2_DF_CMP },
18424   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comlesd",    IX86_BUILTIN_COMLESD,    LE,           (int)MULTI_ARG_2_DF_CMP },
18425   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comgtsd",    IX86_BUILTIN_COMGTSD,    GT,           (int)MULTI_ARG_2_DF_CMP },
18426   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comgesd",    IX86_BUILTIN_COMGESD,    GE,           (int)MULTI_ARG_2_DF_CMP },
18427   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comueqsd",   IX86_BUILTIN_COMUEQSD,   UNEQ,         (int)MULTI_ARG_2_DF_CMP },
18428   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comunesd",   IX86_BUILTIN_COMUNESD,   LTGT,         (int)MULTI_ARG_2_DF_CMP },
18429   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comuneqsd",  IX86_BUILTIN_COMUNESD,   LTGT,         (int)MULTI_ARG_2_DF_CMP },
18430   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comunltsd",  IX86_BUILTIN_COMULTSD,   UNLT,         (int)MULTI_ARG_2_DF_CMP },
18431   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comunlesd",  IX86_BUILTIN_COMULESD,   UNLE,         (int)MULTI_ARG_2_DF_CMP },
18432   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comungtsd",  IX86_BUILTIN_COMUGTSD,   UNGT,         (int)MULTI_ARG_2_DF_CMP },
18433   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comungesd",  IX86_BUILTIN_COMUGESD,   UNGE,         (int)MULTI_ARG_2_DF_CMP },
18434   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comordsd",   IX86_BUILTIN_COMORDSD,   ORDERED,      (int)MULTI_ARG_2_DF_CMP },
18435   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_vmmaskcmpv2df3,    "__builtin_ia32_comunordsd", IX86_BUILTIN_COMUNORDSD, UNORDERED,    (int)MULTI_ARG_2_DF_CMP },
18436
18437   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comeqps",    IX86_BUILTIN_COMEQPS,    EQ,           (int)MULTI_ARG_2_SF_CMP },
18438   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comneps",    IX86_BUILTIN_COMNEPS,    NE,           (int)MULTI_ARG_2_SF_CMP },
18439   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comneqps",   IX86_BUILTIN_COMNEPS,    NE,           (int)MULTI_ARG_2_SF_CMP },
18440   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comltps",    IX86_BUILTIN_COMLTPS,    LT,           (int)MULTI_ARG_2_SF_CMP },
18441   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comleps",    IX86_BUILTIN_COMLEPS,    LE,           (int)MULTI_ARG_2_SF_CMP },
18442   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comgtps",    IX86_BUILTIN_COMGTPS,    GT,           (int)MULTI_ARG_2_SF_CMP },
18443   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comgeps",    IX86_BUILTIN_COMGEPS,    GE,           (int)MULTI_ARG_2_SF_CMP },
18444   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comueqps",   IX86_BUILTIN_COMUEQPS,   UNEQ,         (int)MULTI_ARG_2_SF_CMP },
18445   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comuneps",   IX86_BUILTIN_COMUNEPS,   LTGT,         (int)MULTI_ARG_2_SF_CMP },
18446   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comuneqps",  IX86_BUILTIN_COMUNEPS,   LTGT,         (int)MULTI_ARG_2_SF_CMP },
18447   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comunltps",  IX86_BUILTIN_COMULTPS,   UNLT,         (int)MULTI_ARG_2_SF_CMP },
18448   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comunleps",  IX86_BUILTIN_COMULEPS,   UNLE,         (int)MULTI_ARG_2_SF_CMP },
18449   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comungtps",  IX86_BUILTIN_COMUGTPS,   UNGT,         (int)MULTI_ARG_2_SF_CMP },
18450   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comungeps",  IX86_BUILTIN_COMUGEPS,   UNGE,         (int)MULTI_ARG_2_SF_CMP },
18451   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comordps",   IX86_BUILTIN_COMORDPS,   ORDERED,      (int)MULTI_ARG_2_SF_CMP },
18452   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4sf3,      "__builtin_ia32_comunordps", IX86_BUILTIN_COMUNORDPS, UNORDERED,    (int)MULTI_ARG_2_SF_CMP },
18453
18454   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comeqpd",    IX86_BUILTIN_COMEQPD,    EQ,           (int)MULTI_ARG_2_DF_CMP },
18455   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comnepd",    IX86_BUILTIN_COMNEPD,    NE,           (int)MULTI_ARG_2_DF_CMP },
18456   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comneqpd",   IX86_BUILTIN_COMNEPD,    NE,           (int)MULTI_ARG_2_DF_CMP },
18457   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comltpd",    IX86_BUILTIN_COMLTPD,    LT,           (int)MULTI_ARG_2_DF_CMP },
18458   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comlepd",    IX86_BUILTIN_COMLEPD,    LE,           (int)MULTI_ARG_2_DF_CMP },
18459   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comgtpd",    IX86_BUILTIN_COMGTPD,    GT,           (int)MULTI_ARG_2_DF_CMP },
18460   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comgepd",    IX86_BUILTIN_COMGEPD,    GE,           (int)MULTI_ARG_2_DF_CMP },
18461   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comueqpd",   IX86_BUILTIN_COMUEQPD,   UNEQ,         (int)MULTI_ARG_2_DF_CMP },
18462   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comunepd",   IX86_BUILTIN_COMUNEPD,   LTGT,         (int)MULTI_ARG_2_DF_CMP },
18463   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comuneqpd",  IX86_BUILTIN_COMUNEPD,   LTGT,         (int)MULTI_ARG_2_DF_CMP },
18464   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comunltpd",  IX86_BUILTIN_COMULTPD,   UNLT,         (int)MULTI_ARG_2_DF_CMP },
18465   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comunlepd",  IX86_BUILTIN_COMULEPD,   UNLE,         (int)MULTI_ARG_2_DF_CMP },
18466   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comungtpd",  IX86_BUILTIN_COMUGTPD,   UNGT,         (int)MULTI_ARG_2_DF_CMP },
18467   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comungepd",  IX86_BUILTIN_COMUGEPD,   UNGE,         (int)MULTI_ARG_2_DF_CMP },
18468   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comordpd",   IX86_BUILTIN_COMORDPD,   ORDERED,      (int)MULTI_ARG_2_DF_CMP },
18469   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2df3,      "__builtin_ia32_comunordpd", IX86_BUILTIN_COMUNORDPD, UNORDERED,    (int)MULTI_ARG_2_DF_CMP },
18470
18471   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomeqb",    IX86_BUILTIN_PCOMEQB,    EQ,           (int)MULTI_ARG_2_QI_CMP },
18472   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomneb",    IX86_BUILTIN_PCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
18473   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomneqb",   IX86_BUILTIN_PCOMNEB,    NE,           (int)MULTI_ARG_2_QI_CMP },
18474   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomltb",    IX86_BUILTIN_PCOMLTB,    LT,           (int)MULTI_ARG_2_QI_CMP },
18475   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomleb",    IX86_BUILTIN_PCOMLEB,    LE,           (int)MULTI_ARG_2_QI_CMP },
18476   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomgtb",    IX86_BUILTIN_PCOMGTB,    GT,           (int)MULTI_ARG_2_QI_CMP },
18477   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv16qi3,     "__builtin_ia32_pcomgeb",    IX86_BUILTIN_PCOMGEB,    GE,           (int)MULTI_ARG_2_QI_CMP },
18478
18479   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomeqw",    IX86_BUILTIN_PCOMEQW,    EQ,           (int)MULTI_ARG_2_HI_CMP },
18480   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomnew",    IX86_BUILTIN_PCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
18481   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomneqw",   IX86_BUILTIN_PCOMNEW,    NE,           (int)MULTI_ARG_2_HI_CMP },
18482   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomltw",    IX86_BUILTIN_PCOMLTW,    LT,           (int)MULTI_ARG_2_HI_CMP },
18483   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomlew",    IX86_BUILTIN_PCOMLEW,    LE,           (int)MULTI_ARG_2_HI_CMP },
18484   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomgtw",    IX86_BUILTIN_PCOMGTW,    GT,           (int)MULTI_ARG_2_HI_CMP },
18485   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv8hi3,      "__builtin_ia32_pcomgew",    IX86_BUILTIN_PCOMGEW,    GE,           (int)MULTI_ARG_2_HI_CMP },
18486
18487   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomeqd",    IX86_BUILTIN_PCOMEQD,    EQ,           (int)MULTI_ARG_2_SI_CMP },
18488   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomned",    IX86_BUILTIN_PCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
18489   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomneqd",   IX86_BUILTIN_PCOMNED,    NE,           (int)MULTI_ARG_2_SI_CMP },
18490   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomltd",    IX86_BUILTIN_PCOMLTD,    LT,           (int)MULTI_ARG_2_SI_CMP },
18491   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomled",    IX86_BUILTIN_PCOMLED,    LE,           (int)MULTI_ARG_2_SI_CMP },
18492   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomgtd",    IX86_BUILTIN_PCOMGTD,    GT,           (int)MULTI_ARG_2_SI_CMP },
18493   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv4si3,      "__builtin_ia32_pcomged",    IX86_BUILTIN_PCOMGED,    GE,           (int)MULTI_ARG_2_SI_CMP },
18494
18495   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomeqq",    IX86_BUILTIN_PCOMEQQ,    EQ,           (int)MULTI_ARG_2_DI_CMP },
18496   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomneq",    IX86_BUILTIN_PCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
18497   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomneqq",   IX86_BUILTIN_PCOMNEQ,    NE,           (int)MULTI_ARG_2_DI_CMP },
18498   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomltq",    IX86_BUILTIN_PCOMLTQ,    LT,           (int)MULTI_ARG_2_DI_CMP },
18499   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomleq",    IX86_BUILTIN_PCOMLEQ,    LE,           (int)MULTI_ARG_2_DI_CMP },
18500   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomgtq",    IX86_BUILTIN_PCOMGTQ,    GT,           (int)MULTI_ARG_2_DI_CMP },
18501   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmpv2di3,      "__builtin_ia32_pcomgeq",    IX86_BUILTIN_PCOMGEQ,    GE,           (int)MULTI_ARG_2_DI_CMP },
18502
18503   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v16qi3,"__builtin_ia32_pcomequb",   IX86_BUILTIN_PCOMEQUB,   EQ,           (int)MULTI_ARG_2_QI_CMP },
18504   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v16qi3,"__builtin_ia32_pcomneub",   IX86_BUILTIN_PCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
18505   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v16qi3,"__builtin_ia32_pcomnequb",  IX86_BUILTIN_PCOMNEUB,   NE,           (int)MULTI_ARG_2_QI_CMP },
18506   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv16qi3, "__builtin_ia32_pcomltub",   IX86_BUILTIN_PCOMLTUB,   LTU,          (int)MULTI_ARG_2_QI_CMP },
18507   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv16qi3, "__builtin_ia32_pcomleub",   IX86_BUILTIN_PCOMLEUB,   LEU,          (int)MULTI_ARG_2_QI_CMP },
18508   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv16qi3, "__builtin_ia32_pcomgtub",   IX86_BUILTIN_PCOMGTUB,   GTU,          (int)MULTI_ARG_2_QI_CMP },
18509   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv16qi3, "__builtin_ia32_pcomgeub",   IX86_BUILTIN_PCOMGEUB,   GEU,          (int)MULTI_ARG_2_QI_CMP },
18510
18511   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v8hi3, "__builtin_ia32_pcomequw",   IX86_BUILTIN_PCOMEQUW,   EQ,           (int)MULTI_ARG_2_HI_CMP },
18512   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v8hi3, "__builtin_ia32_pcomneuw",   IX86_BUILTIN_PCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
18513   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v8hi3, "__builtin_ia32_pcomnequw",  IX86_BUILTIN_PCOMNEUW,   NE,           (int)MULTI_ARG_2_HI_CMP },
18514   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv8hi3,  "__builtin_ia32_pcomltuw",   IX86_BUILTIN_PCOMLTUW,   LTU,          (int)MULTI_ARG_2_HI_CMP },
18515   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv8hi3,  "__builtin_ia32_pcomleuw",   IX86_BUILTIN_PCOMLEUW,   LEU,          (int)MULTI_ARG_2_HI_CMP },
18516   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv8hi3,  "__builtin_ia32_pcomgtuw",   IX86_BUILTIN_PCOMGTUW,   GTU,          (int)MULTI_ARG_2_HI_CMP },
18517   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv8hi3,  "__builtin_ia32_pcomgeuw",   IX86_BUILTIN_PCOMGEUW,   GEU,          (int)MULTI_ARG_2_HI_CMP },
18518
18519   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v4si3, "__builtin_ia32_pcomequd",   IX86_BUILTIN_PCOMEQUD,   EQ,           (int)MULTI_ARG_2_SI_CMP },
18520   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v4si3, "__builtin_ia32_pcomneud",   IX86_BUILTIN_PCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
18521   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v4si3, "__builtin_ia32_pcomnequd",  IX86_BUILTIN_PCOMNEUD,   NE,           (int)MULTI_ARG_2_SI_CMP },
18522   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv4si3,  "__builtin_ia32_pcomltud",   IX86_BUILTIN_PCOMLTUD,   LTU,          (int)MULTI_ARG_2_SI_CMP },
18523   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv4si3,  "__builtin_ia32_pcomleud",   IX86_BUILTIN_PCOMLEUD,   LEU,          (int)MULTI_ARG_2_SI_CMP },
18524   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv4si3,  "__builtin_ia32_pcomgtud",   IX86_BUILTIN_PCOMGTUD,   GTU,          (int)MULTI_ARG_2_SI_CMP },
18525   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv4si3,  "__builtin_ia32_pcomgeud",   IX86_BUILTIN_PCOMGEUD,   GEU,          (int)MULTI_ARG_2_SI_CMP },
18526
18527   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v2di3, "__builtin_ia32_pcomequq",   IX86_BUILTIN_PCOMEQUQ,   EQ,           (int)MULTI_ARG_2_DI_CMP },
18528   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v2di3, "__builtin_ia32_pcomneuq",   IX86_BUILTIN_PCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
18529   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_uns2v2di3, "__builtin_ia32_pcomnequq",  IX86_BUILTIN_PCOMNEUQ,   NE,           (int)MULTI_ARG_2_DI_CMP },
18530   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv2di3,  "__builtin_ia32_pcomltuq",   IX86_BUILTIN_PCOMLTUQ,   LTU,          (int)MULTI_ARG_2_DI_CMP },
18531   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv2di3,  "__builtin_ia32_pcomleuq",   IX86_BUILTIN_PCOMLEUQ,   LEU,          (int)MULTI_ARG_2_DI_CMP },
18532   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv2di3,  "__builtin_ia32_pcomgtuq",   IX86_BUILTIN_PCOMGTUQ,   GTU,          (int)MULTI_ARG_2_DI_CMP },
18533   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_maskcmp_unsv2di3,  "__builtin_ia32_pcomgeuq",   IX86_BUILTIN_PCOMGEUQ,   GEU,          (int)MULTI_ARG_2_DI_CMP },
18534
18535   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv4sf3,       "__builtin_ia32_comfalsess", IX86_BUILTIN_COMFALSESS, COM_FALSE_S,  (int)MULTI_ARG_2_SF_TF },
18536   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv4sf3,       "__builtin_ia32_comtruess",  IX86_BUILTIN_COMTRUESS,  COM_TRUE_S,   (int)MULTI_ARG_2_SF_TF },
18537   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv4sf3,       "__builtin_ia32_comfalseps", IX86_BUILTIN_COMFALSEPS, COM_FALSE_P,  (int)MULTI_ARG_2_SF_TF },
18538   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv4sf3,       "__builtin_ia32_comtrueps",  IX86_BUILTIN_COMTRUEPS,  COM_TRUE_P,   (int)MULTI_ARG_2_SF_TF },
18539   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv2df3,       "__builtin_ia32_comfalsesd", IX86_BUILTIN_COMFALSESD, COM_FALSE_S,  (int)MULTI_ARG_2_DF_TF },
18540   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv2df3,       "__builtin_ia32_comtruesd",  IX86_BUILTIN_COMTRUESD,  COM_TRUE_S,   (int)MULTI_ARG_2_DF_TF },
18541   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv2df3,       "__builtin_ia32_comfalsepd", IX86_BUILTIN_COMFALSEPD, COM_FALSE_P,  (int)MULTI_ARG_2_DF_TF },
18542   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_com_tfv2df3,       "__builtin_ia32_comtruepd",  IX86_BUILTIN_COMTRUEPD,  COM_TRUE_P,   (int)MULTI_ARG_2_DF_TF },
18543
18544   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv16qi3,     "__builtin_ia32_pcomfalseb", IX86_BUILTIN_PCOMFALSEB, PCOM_FALSE,   (int)MULTI_ARG_2_QI_TF },
18545   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv8hi3,      "__builtin_ia32_pcomfalsew", IX86_BUILTIN_PCOMFALSEW, PCOM_FALSE,   (int)MULTI_ARG_2_HI_TF },
18546   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv4si3,      "__builtin_ia32_pcomfalsed", IX86_BUILTIN_PCOMFALSED, PCOM_FALSE,   (int)MULTI_ARG_2_SI_TF },
18547   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv2di3,      "__builtin_ia32_pcomfalseq", IX86_BUILTIN_PCOMFALSEQ, PCOM_FALSE,   (int)MULTI_ARG_2_DI_TF },
18548   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv16qi3,     "__builtin_ia32_pcomfalseub",IX86_BUILTIN_PCOMFALSEUB,PCOM_FALSE,   (int)MULTI_ARG_2_QI_TF },
18549   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv8hi3,      "__builtin_ia32_pcomfalseuw",IX86_BUILTIN_PCOMFALSEUW,PCOM_FALSE,   (int)MULTI_ARG_2_HI_TF },
18550   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv4si3,      "__builtin_ia32_pcomfalseud",IX86_BUILTIN_PCOMFALSEUD,PCOM_FALSE,   (int)MULTI_ARG_2_SI_TF },
18551   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv2di3,      "__builtin_ia32_pcomfalseuq",IX86_BUILTIN_PCOMFALSEUQ,PCOM_FALSE,   (int)MULTI_ARG_2_DI_TF },
18552
18553   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv16qi3,     "__builtin_ia32_pcomtrueb",  IX86_BUILTIN_PCOMTRUEB,  PCOM_TRUE,    (int)MULTI_ARG_2_QI_TF },
18554   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv8hi3,      "__builtin_ia32_pcomtruew",  IX86_BUILTIN_PCOMTRUEW,  PCOM_TRUE,    (int)MULTI_ARG_2_HI_TF },
18555   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv4si3,      "__builtin_ia32_pcomtrued",  IX86_BUILTIN_PCOMTRUED,  PCOM_TRUE,    (int)MULTI_ARG_2_SI_TF },
18556   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv2di3,      "__builtin_ia32_pcomtrueq",  IX86_BUILTIN_PCOMTRUEQ,  PCOM_TRUE,    (int)MULTI_ARG_2_DI_TF },
18557   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv16qi3,     "__builtin_ia32_pcomtrueub", IX86_BUILTIN_PCOMTRUEUB, PCOM_TRUE,    (int)MULTI_ARG_2_QI_TF },
18558   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv8hi3,      "__builtin_ia32_pcomtrueuw", IX86_BUILTIN_PCOMTRUEUW, PCOM_TRUE,    (int)MULTI_ARG_2_HI_TF },
18559   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv4si3,      "__builtin_ia32_pcomtrueud", IX86_BUILTIN_PCOMTRUEUD, PCOM_TRUE,    (int)MULTI_ARG_2_SI_TF },
18560   { OPTION_MASK_ISA_SSE5, CODE_FOR_sse5_pcom_tfv2di3,      "__builtin_ia32_pcomtrueuq", IX86_BUILTIN_PCOMTRUEUQ, PCOM_TRUE,    (int)MULTI_ARG_2_DI_TF },
18561 };
18562
18563 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
18564    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
18565    builtins.  */
18566 static void
18567 ix86_init_mmx_sse_builtins (void)
18568 {
18569   const struct builtin_description * d;
18570   size_t i;
18571
18572   tree V16QI_type_node = build_vector_type_for_mode (char_type_node, V16QImode);
18573   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
18574   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
18575   tree V2DI_type_node
18576     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
18577   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
18578   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
18579   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
18580   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
18581   tree V8QI_type_node = build_vector_type_for_mode (char_type_node, V8QImode);
18582   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
18583
18584   tree pchar_type_node = build_pointer_type (char_type_node);
18585   tree pcchar_type_node = build_pointer_type (
18586                              build_type_variant (char_type_node, 1, 0));
18587   tree pfloat_type_node = build_pointer_type (float_type_node);
18588   tree pcfloat_type_node = build_pointer_type (
18589                              build_type_variant (float_type_node, 1, 0));
18590   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
18591   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
18592   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
18593
18594   /* Comparisons.  */
18595   tree int_ftype_v4sf_v4sf
18596     = build_function_type_list (integer_type_node,
18597                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
18598   tree v4si_ftype_v4sf_v4sf
18599     = build_function_type_list (V4SI_type_node,
18600                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
18601   /* MMX/SSE/integer conversions.  */
18602   tree int_ftype_v4sf
18603     = build_function_type_list (integer_type_node,
18604                                 V4SF_type_node, NULL_TREE);
18605   tree int64_ftype_v4sf
18606     = build_function_type_list (long_long_integer_type_node,
18607                                 V4SF_type_node, NULL_TREE);
18608   tree int_ftype_v8qi
18609     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
18610   tree v4sf_ftype_v4sf_int
18611     = build_function_type_list (V4SF_type_node,
18612                                 V4SF_type_node, integer_type_node, NULL_TREE);
18613   tree v4sf_ftype_v4sf_int64
18614     = build_function_type_list (V4SF_type_node,
18615                                 V4SF_type_node, long_long_integer_type_node,
18616                                 NULL_TREE);
18617   tree v4sf_ftype_v4sf_v2si
18618     = build_function_type_list (V4SF_type_node,
18619                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
18620
18621   /* Miscellaneous.  */
18622   tree v8qi_ftype_v4hi_v4hi
18623     = build_function_type_list (V8QI_type_node,
18624                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
18625   tree v4hi_ftype_v2si_v2si
18626     = build_function_type_list (V4HI_type_node,
18627                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
18628   tree v4sf_ftype_v4sf_v4sf_int
18629     = build_function_type_list (V4SF_type_node,
18630                                 V4SF_type_node, V4SF_type_node,
18631                                 integer_type_node, NULL_TREE);
18632   tree v2si_ftype_v4hi_v4hi
18633     = build_function_type_list (V2SI_type_node,
18634                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
18635   tree v4hi_ftype_v4hi_int
18636     = build_function_type_list (V4HI_type_node,
18637                                 V4HI_type_node, integer_type_node, NULL_TREE);
18638   tree v4hi_ftype_v4hi_di
18639     = build_function_type_list (V4HI_type_node,
18640                                 V4HI_type_node, long_long_unsigned_type_node,
18641                                 NULL_TREE);
18642   tree v2si_ftype_v2si_di
18643     = build_function_type_list (V2SI_type_node,
18644                                 V2SI_type_node, long_long_unsigned_type_node,
18645                                 NULL_TREE);
18646   tree void_ftype_void
18647     = build_function_type (void_type_node, void_list_node);
18648   tree void_ftype_unsigned
18649     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
18650   tree void_ftype_unsigned_unsigned
18651     = build_function_type_list (void_type_node, unsigned_type_node,
18652                                 unsigned_type_node, NULL_TREE);
18653   tree void_ftype_pcvoid_unsigned_unsigned
18654     = build_function_type_list (void_type_node, const_ptr_type_node,
18655                                 unsigned_type_node, unsigned_type_node,
18656                                 NULL_TREE);
18657   tree unsigned_ftype_void
18658     = build_function_type (unsigned_type_node, void_list_node);
18659   tree v2si_ftype_v4sf
18660     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
18661   /* Loads/stores.  */
18662   tree void_ftype_v8qi_v8qi_pchar
18663     = build_function_type_list (void_type_node,
18664                                 V8QI_type_node, V8QI_type_node,
18665                                 pchar_type_node, NULL_TREE);
18666   tree v4sf_ftype_pcfloat
18667     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
18668   /* @@@ the type is bogus */
18669   tree v4sf_ftype_v4sf_pv2si
18670     = build_function_type_list (V4SF_type_node,
18671                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
18672   tree void_ftype_pv2si_v4sf
18673     = build_function_type_list (void_type_node,
18674                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
18675   tree void_ftype_pfloat_v4sf
18676     = build_function_type_list (void_type_node,
18677                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
18678   tree void_ftype_pdi_di
18679     = build_function_type_list (void_type_node,
18680                                 pdi_type_node, long_long_unsigned_type_node,
18681                                 NULL_TREE);
18682   tree void_ftype_pv2di_v2di
18683     = build_function_type_list (void_type_node,
18684                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
18685   /* Normal vector unops.  */
18686   tree v4sf_ftype_v4sf
18687     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
18688   tree v16qi_ftype_v16qi
18689     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
18690   tree v8hi_ftype_v8hi
18691     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
18692   tree v4si_ftype_v4si
18693     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
18694   tree v8qi_ftype_v8qi
18695     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
18696   tree v4hi_ftype_v4hi
18697     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
18698
18699   /* Normal vector binops.  */
18700   tree v4sf_ftype_v4sf_v4sf
18701     = build_function_type_list (V4SF_type_node,
18702                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
18703   tree v8qi_ftype_v8qi_v8qi
18704     = build_function_type_list (V8QI_type_node,
18705                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
18706   tree v4hi_ftype_v4hi_v4hi
18707     = build_function_type_list (V4HI_type_node,
18708                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
18709   tree v2si_ftype_v2si_v2si
18710     = build_function_type_list (V2SI_type_node,
18711                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
18712   tree di_ftype_di_di
18713     = build_function_type_list (long_long_unsigned_type_node,
18714                                 long_long_unsigned_type_node,
18715                                 long_long_unsigned_type_node, NULL_TREE);
18716
18717   tree di_ftype_di_di_int
18718     = build_function_type_list (long_long_unsigned_type_node,
18719                                 long_long_unsigned_type_node,
18720                                 long_long_unsigned_type_node,
18721                                 integer_type_node, NULL_TREE);
18722
18723   tree v2si_ftype_v2sf
18724     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
18725   tree v2sf_ftype_v2si
18726     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
18727   tree v2si_ftype_v2si
18728     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
18729   tree v2sf_ftype_v2sf
18730     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
18731   tree v2sf_ftype_v2sf_v2sf
18732     = build_function_type_list (V2SF_type_node,
18733                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
18734   tree v2si_ftype_v2sf_v2sf
18735     = build_function_type_list (V2SI_type_node,
18736                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
18737   tree pint_type_node    = build_pointer_type (integer_type_node);
18738   tree pdouble_type_node = build_pointer_type (double_type_node);
18739   tree pcdouble_type_node = build_pointer_type (
18740                                 build_type_variant (double_type_node, 1, 0));
18741   tree int_ftype_v2df_v2df
18742     = build_function_type_list (integer_type_node,
18743                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
18744
18745   tree void_ftype_pcvoid
18746     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
18747   tree v4sf_ftype_v4si
18748     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
18749   tree v4si_ftype_v4sf
18750     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
18751   tree v2df_ftype_v4si
18752     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
18753   tree v4si_ftype_v2df
18754     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
18755   tree v4si_ftype_v2df_v2df
18756     = build_function_type_list (V4SI_type_node,
18757                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
18758   tree v2si_ftype_v2df
18759     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
18760   tree v4sf_ftype_v2df
18761     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
18762   tree v2df_ftype_v2si
18763     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
18764   tree v2df_ftype_v4sf
18765     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
18766   tree int_ftype_v2df
18767     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
18768   tree int64_ftype_v2df
18769     = build_function_type_list (long_long_integer_type_node,
18770                                 V2DF_type_node, NULL_TREE);
18771   tree v2df_ftype_v2df_int
18772     = build_function_type_list (V2DF_type_node,
18773                                 V2DF_type_node, integer_type_node, NULL_TREE);
18774   tree v2df_ftype_v2df_int64
18775     = build_function_type_list (V2DF_type_node,
18776                                 V2DF_type_node, long_long_integer_type_node,
18777                                 NULL_TREE);
18778   tree v4sf_ftype_v4sf_v2df
18779     = build_function_type_list (V4SF_type_node,
18780                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
18781   tree v2df_ftype_v2df_v4sf
18782     = build_function_type_list (V2DF_type_node,
18783                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
18784   tree v2df_ftype_v2df_v2df_int
18785     = build_function_type_list (V2DF_type_node,
18786                                 V2DF_type_node, V2DF_type_node,
18787                                 integer_type_node,
18788                                 NULL_TREE);
18789   tree v2df_ftype_v2df_pcdouble
18790     = build_function_type_list (V2DF_type_node,
18791                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
18792   tree void_ftype_pdouble_v2df
18793     = build_function_type_list (void_type_node,
18794                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
18795   tree void_ftype_pint_int
18796     = build_function_type_list (void_type_node,
18797                                 pint_type_node, integer_type_node, NULL_TREE);
18798   tree void_ftype_v16qi_v16qi_pchar
18799     = build_function_type_list (void_type_node,
18800                                 V16QI_type_node, V16QI_type_node,
18801                                 pchar_type_node, NULL_TREE);
18802   tree v2df_ftype_pcdouble
18803     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
18804   tree v2df_ftype_v2df_v2df
18805     = build_function_type_list (V2DF_type_node,
18806                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
18807   tree v16qi_ftype_v16qi_v16qi
18808     = build_function_type_list (V16QI_type_node,
18809                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
18810   tree v8hi_ftype_v8hi_v8hi
18811     = build_function_type_list (V8HI_type_node,
18812                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
18813   tree v4si_ftype_v4si_v4si
18814     = build_function_type_list (V4SI_type_node,
18815                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
18816   tree v2di_ftype_v2di_v2di
18817     = build_function_type_list (V2DI_type_node,
18818                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
18819   tree v2di_ftype_v2df_v2df
18820     = build_function_type_list (V2DI_type_node,
18821                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
18822   tree v2df_ftype_v2df
18823     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
18824   tree v2di_ftype_v2di_int
18825     = build_function_type_list (V2DI_type_node,
18826                                 V2DI_type_node, integer_type_node, NULL_TREE);
18827   tree v2di_ftype_v2di_v2di_int
18828     = build_function_type_list (V2DI_type_node, V2DI_type_node,
18829                                 V2DI_type_node, integer_type_node, NULL_TREE);
18830   tree v4si_ftype_v4si_int
18831     = build_function_type_list (V4SI_type_node,
18832                                 V4SI_type_node, integer_type_node, NULL_TREE);
18833   tree v8hi_ftype_v8hi_int
18834     = build_function_type_list (V8HI_type_node,
18835                                 V8HI_type_node, integer_type_node, NULL_TREE);
18836   tree v4si_ftype_v8hi_v8hi
18837     = build_function_type_list (V4SI_type_node,
18838                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
18839   tree di_ftype_v8qi_v8qi
18840     = build_function_type_list (long_long_unsigned_type_node,
18841                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
18842   tree di_ftype_v2si_v2si
18843     = build_function_type_list (long_long_unsigned_type_node,
18844                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
18845   tree v2di_ftype_v16qi_v16qi
18846     = build_function_type_list (V2DI_type_node,
18847                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
18848   tree v2di_ftype_v4si_v4si
18849     = build_function_type_list (V2DI_type_node,
18850                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
18851   tree int_ftype_v16qi
18852     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
18853   tree v16qi_ftype_pcchar
18854     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
18855   tree void_ftype_pchar_v16qi
18856     = build_function_type_list (void_type_node,
18857                                 pchar_type_node, V16QI_type_node, NULL_TREE);
18858
18859   tree v2di_ftype_v2di_unsigned_unsigned
18860     = build_function_type_list (V2DI_type_node, V2DI_type_node,
18861                                 unsigned_type_node, unsigned_type_node,
18862                                 NULL_TREE);
18863   tree v2di_ftype_v2di_v2di_unsigned_unsigned
18864     = build_function_type_list (V2DI_type_node, V2DI_type_node, V2DI_type_node,
18865                                 unsigned_type_node, unsigned_type_node,
18866                                 NULL_TREE);
18867   tree v2di_ftype_v2di_v16qi
18868     = build_function_type_list (V2DI_type_node, V2DI_type_node, V16QI_type_node,
18869                                 NULL_TREE);
18870   tree v2df_ftype_v2df_v2df_v2df
18871     = build_function_type_list (V2DF_type_node,
18872                                 V2DF_type_node, V2DF_type_node,
18873                                 V2DF_type_node, NULL_TREE);
18874   tree v4sf_ftype_v4sf_v4sf_v4sf
18875     = build_function_type_list (V4SF_type_node,
18876                                 V4SF_type_node, V4SF_type_node,
18877                                 V4SF_type_node, NULL_TREE);
18878   tree v8hi_ftype_v16qi
18879     = build_function_type_list (V8HI_type_node, V16QI_type_node,
18880                                 NULL_TREE);
18881   tree v4si_ftype_v16qi
18882     = build_function_type_list (V4SI_type_node, V16QI_type_node,
18883                                 NULL_TREE);
18884   tree v2di_ftype_v16qi
18885     = build_function_type_list (V2DI_type_node, V16QI_type_node,
18886                                 NULL_TREE);
18887   tree v4si_ftype_v8hi
18888     = build_function_type_list (V4SI_type_node, V8HI_type_node,
18889                                 NULL_TREE);
18890   tree v2di_ftype_v8hi
18891     = build_function_type_list (V2DI_type_node, V8HI_type_node,
18892                                 NULL_TREE);
18893   tree v2di_ftype_v4si
18894     = build_function_type_list (V2DI_type_node, V4SI_type_node,
18895                                 NULL_TREE);
18896   tree v2di_ftype_pv2di
18897     = build_function_type_list (V2DI_type_node, pv2di_type_node,
18898                                 NULL_TREE);
18899   tree v16qi_ftype_v16qi_v16qi_int
18900     = build_function_type_list (V16QI_type_node, V16QI_type_node,
18901                                 V16QI_type_node, integer_type_node,
18902                                 NULL_TREE);
18903   tree v16qi_ftype_v16qi_v16qi_v16qi
18904     = build_function_type_list (V16QI_type_node, V16QI_type_node,
18905                                 V16QI_type_node, V16QI_type_node,
18906                                 NULL_TREE);
18907   tree v8hi_ftype_v8hi_v8hi_int
18908     = build_function_type_list (V8HI_type_node, V8HI_type_node,
18909                                 V8HI_type_node, integer_type_node,
18910                                 NULL_TREE);
18911   tree v4si_ftype_v4si_v4si_int
18912     = build_function_type_list (V4SI_type_node, V4SI_type_node,
18913                                 V4SI_type_node, integer_type_node,
18914                                 NULL_TREE);
18915   tree int_ftype_v2di_v2di
18916     = build_function_type_list (integer_type_node,
18917                                 V2DI_type_node, V2DI_type_node,
18918                                 NULL_TREE);
18919   tree int_ftype_v16qi_int_v16qi_int_int
18920     = build_function_type_list (integer_type_node,
18921                                 V16QI_type_node,
18922                                 integer_type_node,
18923                                 V16QI_type_node,
18924                                 integer_type_node,
18925                                 integer_type_node,
18926                                 NULL_TREE);
18927   tree v16qi_ftype_v16qi_int_v16qi_int_int
18928     = build_function_type_list (V16QI_type_node,
18929                                 V16QI_type_node,
18930                                 integer_type_node,
18931                                 V16QI_type_node,
18932                                 integer_type_node,
18933                                 integer_type_node,
18934                                 NULL_TREE);
18935   tree int_ftype_v16qi_v16qi_int
18936     = build_function_type_list (integer_type_node,
18937                                 V16QI_type_node,
18938                                 V16QI_type_node,
18939                                 integer_type_node,
18940                                 NULL_TREE);
18941
18942   /* SSE5 instructions */
18943   tree v2di_ftype_v2di_v2di_v2di
18944     = build_function_type_list (V2DI_type_node,
18945                                 V2DI_type_node,
18946                                 V2DI_type_node,
18947                                 V2DI_type_node,
18948                                 NULL_TREE);
18949
18950   tree v4si_ftype_v4si_v4si_v4si
18951     = build_function_type_list (V4SI_type_node,
18952                                 V4SI_type_node,
18953                                 V4SI_type_node,
18954                                 V4SI_type_node,
18955                                 NULL_TREE);
18956
18957   tree v4si_ftype_v4si_v4si_v2di
18958     = build_function_type_list (V4SI_type_node,
18959                                 V4SI_type_node,
18960                                 V4SI_type_node,
18961                                 V2DI_type_node,
18962                                 NULL_TREE);
18963
18964   tree v8hi_ftype_v8hi_v8hi_v8hi
18965     = build_function_type_list (V8HI_type_node,
18966                                 V8HI_type_node,
18967                                 V8HI_type_node,
18968                                 V8HI_type_node,
18969                                 NULL_TREE);
18970
18971   tree v8hi_ftype_v8hi_v8hi_v4si
18972     = build_function_type_list (V8HI_type_node,
18973                                 V8HI_type_node,
18974                                 V8HI_type_node,
18975                                 V4SI_type_node,
18976                                 NULL_TREE);
18977
18978   tree v2df_ftype_v2df_v2df_v16qi
18979     = build_function_type_list (V2DF_type_node,
18980                                 V2DF_type_node,
18981                                 V2DF_type_node,
18982                                 V16QI_type_node,
18983                                 NULL_TREE);
18984
18985   tree v4sf_ftype_v4sf_v4sf_v16qi
18986     = build_function_type_list (V4SF_type_node,
18987                                 V4SF_type_node,
18988                                 V4SF_type_node,
18989                                 V16QI_type_node,
18990                                 NULL_TREE);
18991
18992   tree v2di_ftype_v2di_si
18993     = build_function_type_list (V2DI_type_node,
18994                                 V2DI_type_node,
18995                                 integer_type_node,
18996                                 NULL_TREE);
18997
18998   tree v4si_ftype_v4si_si
18999     = build_function_type_list (V4SI_type_node,
19000                                 V4SI_type_node,
19001                                 integer_type_node,
19002                                 NULL_TREE);
19003
19004   tree v8hi_ftype_v8hi_si
19005     = build_function_type_list (V8HI_type_node,
19006                                 V8HI_type_node,
19007                                 integer_type_node,
19008                                 NULL_TREE);
19009
19010   tree v16qi_ftype_v16qi_si
19011     = build_function_type_list (V16QI_type_node,
19012                                 V16QI_type_node,
19013                                 integer_type_node,
19014                                 NULL_TREE);
19015   tree v4sf_ftype_v4hi
19016     = build_function_type_list (V4SF_type_node,
19017                                 V4HI_type_node,
19018                                 NULL_TREE);
19019
19020   tree v4hi_ftype_v4sf
19021     = build_function_type_list (V4HI_type_node,
19022                                 V4SF_type_node,
19023                                 NULL_TREE);
19024
19025   tree v2di_ftype_v2di
19026     = build_function_type_list (V2DI_type_node, V2DI_type_node, NULL_TREE);
19027
19028   tree ftype;
19029
19030   /* The __float80 type.  */
19031   if (TYPE_MODE (long_double_type_node) == XFmode)
19032     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
19033                                                "__float80");
19034   else
19035     {
19036       /* The __float80 type.  */
19037       tree float80_type_node = make_node (REAL_TYPE);
19038
19039       TYPE_PRECISION (float80_type_node) = 80;
19040       layout_type (float80_type_node);
19041       (*lang_hooks.types.register_builtin_type) (float80_type_node,
19042                                                  "__float80");
19043     }
19044
19045   if (TARGET_64BIT)
19046     {
19047       tree float128_type_node = make_node (REAL_TYPE);
19048
19049       TYPE_PRECISION (float128_type_node) = 128;
19050       layout_type (float128_type_node);
19051       (*lang_hooks.types.register_builtin_type) (float128_type_node,
19052                                                  "__float128");
19053
19054       /* TFmode support builtins.  */
19055       ftype = build_function_type (float128_type_node,
19056                                    void_list_node);
19057       def_builtin (OPTION_MASK_ISA_64BIT, "__builtin_infq", ftype, IX86_BUILTIN_INFQ);
19058
19059       ftype = build_function_type_list (float128_type_node,
19060                                         float128_type_node,
19061                                         NULL_TREE);
19062       def_builtin_const (OPTION_MASK_ISA_64BIT, "__builtin_fabsq", ftype, IX86_BUILTIN_FABSQ);
19063
19064       ftype = build_function_type_list (float128_type_node,
19065                                         float128_type_node,
19066                                         float128_type_node,
19067                                         NULL_TREE);
19068       def_builtin_const (OPTION_MASK_ISA_64BIT, "__builtin_copysignq", ftype, IX86_BUILTIN_COPYSIGNQ);
19069     }
19070
19071   /* Add all SSE builtins that are more or less simple operations on
19072      three operands.  */
19073   for (i = 0, d = bdesc_sse_3arg;
19074        i < ARRAY_SIZE (bdesc_sse_3arg);
19075        i++, d++)
19076     {
19077       /* Use one of the operands; the target can have a different mode for
19078          mask-generating compares.  */
19079       enum machine_mode mode;
19080       tree type;
19081
19082       if (d->name == 0)
19083         continue;
19084       mode = insn_data[d->icode].operand[1].mode;
19085
19086       switch (mode)
19087         {
19088         case V16QImode:
19089           type = v16qi_ftype_v16qi_v16qi_int;
19090           break;
19091         case V8HImode:
19092           type = v8hi_ftype_v8hi_v8hi_int;
19093           break;
19094         case V4SImode:
19095           type = v4si_ftype_v4si_v4si_int;
19096           break;
19097         case V2DImode:
19098           type = v2di_ftype_v2di_v2di_int;
19099           break;
19100         case V2DFmode:
19101           type = v2df_ftype_v2df_v2df_int;
19102           break;
19103         case V4SFmode:
19104           type = v4sf_ftype_v4sf_v4sf_int;
19105           break;
19106         default:
19107           gcc_unreachable ();
19108         }
19109
19110       /* Override for variable blends.  */
19111       switch (d->icode)
19112         {
19113         case CODE_FOR_sse4_1_blendvpd:
19114           type = v2df_ftype_v2df_v2df_v2df;
19115           break;
19116         case CODE_FOR_sse4_1_blendvps:
19117           type = v4sf_ftype_v4sf_v4sf_v4sf;
19118           break;
19119         case CODE_FOR_sse4_1_pblendvb:
19120           type = v16qi_ftype_v16qi_v16qi_v16qi;
19121           break;
19122         default:
19123           break;
19124         }
19125
19126       def_builtin_const (d->mask, d->name, type, d->code);
19127     }
19128
19129   /* Add all builtins that are more or less simple operations on two
19130      operands.  */
19131   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
19132     {
19133       /* Use one of the operands; the target can have a different mode for
19134          mask-generating compares.  */
19135       enum machine_mode mode;
19136       tree type;
19137
19138       if (d->name == 0)
19139         continue;
19140       mode = insn_data[d->icode].operand[1].mode;
19141
19142       switch (mode)
19143         {
19144         case V16QImode:
19145           type = v16qi_ftype_v16qi_v16qi;
19146           break;
19147         case V8HImode:
19148           type = v8hi_ftype_v8hi_v8hi;
19149           break;
19150         case V4SImode:
19151           type = v4si_ftype_v4si_v4si;
19152           break;
19153         case V2DImode:
19154           type = v2di_ftype_v2di_v2di;
19155           break;
19156         case V2DFmode:
19157           type = v2df_ftype_v2df_v2df;
19158           break;
19159         case V4SFmode:
19160           type = v4sf_ftype_v4sf_v4sf;
19161           break;
19162         case V8QImode:
19163           type = v8qi_ftype_v8qi_v8qi;
19164           break;
19165         case V4HImode:
19166           type = v4hi_ftype_v4hi_v4hi;
19167           break;
19168         case V2SImode:
19169           type = v2si_ftype_v2si_v2si;
19170           break;
19171         case DImode:
19172           type = di_ftype_di_di;
19173           break;
19174
19175         default:
19176           gcc_unreachable ();
19177         }
19178
19179       /* Override for comparisons.  */
19180       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
19181           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
19182         type = v4si_ftype_v4sf_v4sf;
19183
19184       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
19185           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
19186         type = v2di_ftype_v2df_v2df;
19187
19188       if (d->icode == CODE_FOR_vec_pack_sfix_v2df)
19189         type = v4si_ftype_v2df_v2df;
19190
19191       def_builtin_const (d->mask, d->name, type, d->code);
19192     }
19193
19194   /* Add all builtins that are more or less simple operations on 1 operand.  */
19195   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
19196     {
19197       enum machine_mode mode;
19198       tree type;
19199
19200       if (d->name == 0)
19201         continue;
19202       mode = insn_data[d->icode].operand[1].mode;
19203
19204       switch (mode)
19205         {
19206         case V16QImode:
19207           type = v16qi_ftype_v16qi;
19208           break;
19209         case V8HImode:
19210           type = v8hi_ftype_v8hi;
19211           break;
19212         case V4SImode:
19213           type = v4si_ftype_v4si;
19214           break;
19215         case V2DFmode:
19216           type = v2df_ftype_v2df;
19217           break;
19218         case V4SFmode:
19219           type = v4sf_ftype_v4sf;
19220           break;
19221         case V8QImode:
19222           type = v8qi_ftype_v8qi;
19223           break;
19224         case V4HImode:
19225           type = v4hi_ftype_v4hi;
19226           break;
19227         case V2SImode:
19228           type = v2si_ftype_v2si;
19229           break;
19230
19231         default:
19232           abort ();
19233         }
19234
19235       def_builtin_const (d->mask, d->name, type, d->code);
19236     }
19237
19238   /* pcmpestr[im] insns.  */
19239   for (i = 0, d = bdesc_pcmpestr;
19240        i < ARRAY_SIZE (bdesc_pcmpestr);
19241        i++, d++)
19242     {
19243       if (d->code == IX86_BUILTIN_PCMPESTRM128)
19244         ftype = v16qi_ftype_v16qi_int_v16qi_int_int;
19245       else
19246         ftype = int_ftype_v16qi_int_v16qi_int_int;
19247       def_builtin_const (d->mask, d->name, ftype, d->code);
19248     }
19249
19250   /* pcmpistr[im] insns.  */
19251   for (i = 0, d = bdesc_pcmpistr;
19252        i < ARRAY_SIZE (bdesc_pcmpistr);
19253        i++, d++)
19254     {
19255       if (d->code == IX86_BUILTIN_PCMPISTRM128)
19256         ftype = v16qi_ftype_v16qi_v16qi_int;
19257       else
19258         ftype = int_ftype_v16qi_v16qi_int;
19259       def_builtin_const (d->mask, d->name, ftype, d->code);
19260     }
19261
19262   /* Add the remaining MMX insns with somewhat more complicated types.  */
19263   def_builtin (OPTION_MASK_ISA_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
19264   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
19265   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
19266   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
19267
19268   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
19269   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
19270   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
19271
19272   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
19273   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
19274
19275   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
19276   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
19277
19278   /* comi/ucomi insns.  */
19279   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
19280     if (d->mask == OPTION_MASK_ISA_SSE2)
19281       def_builtin_const (d->mask, d->name, int_ftype_v2df_v2df, d->code);
19282     else
19283       def_builtin_const (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
19284
19285   /* ptest insns.  */
19286   for (i = 0, d = bdesc_ptest; i < ARRAY_SIZE (bdesc_ptest); i++, d++)
19287     def_builtin_const (d->mask, d->name, int_ftype_v2di_v2di, d->code);
19288
19289   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
19290   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
19291   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
19292
19293   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
19294   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
19295   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
19296   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
19297   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
19298   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
19299   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
19300   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
19301   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
19302   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
19303   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
19304
19305   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
19306
19307   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
19308   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
19309
19310   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
19311   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
19312   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
19313   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
19314
19315   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
19316   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
19317   def_builtin (OPTION_MASK_ISA_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
19318   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
19319
19320   def_builtin (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
19321
19322   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
19323
19324   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
19325   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
19326   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
19327   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rsqrtps_nr", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS_NR);
19328   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
19329   ftype = build_function_type_list (float_type_node,
19330                                     float_type_node,
19331                                     NULL_TREE);
19332   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_rsqrtf", ftype, IX86_BUILTIN_RSQRTF);
19333   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
19334   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_sqrtps_nr", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS_NR);
19335   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
19336
19337   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
19338
19339   /* Original 3DNow!  */
19340   def_builtin (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
19341   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
19342   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
19343   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
19344   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
19345   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
19346   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
19347   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
19348   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
19349   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
19350   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
19351   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
19352   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
19353   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
19354   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
19355   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
19356   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
19357   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
19358   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
19359   def_builtin_const (OPTION_MASK_ISA_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
19360
19361   /* 3DNow! extension as used in the Athlon CPU.  */
19362   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
19363   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
19364   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
19365   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
19366   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
19367   def_builtin_const (OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
19368
19369   /* SSE2 */
19370   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
19371
19372   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
19373   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
19374
19375   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
19376   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
19377
19378   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
19379   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
19380   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
19381   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
19382   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
19383
19384   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
19385   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
19386   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
19387   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
19388
19389   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
19390   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
19391
19392   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
19393
19394   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
19395   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
19396
19397   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
19398   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
19399   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
19400   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
19401   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
19402
19403   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
19404
19405   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
19406   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
19407   def_builtin_const (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
19408   def_builtin_const (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
19409
19410   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
19411   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
19412   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
19413
19414   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
19415   def_builtin_const (OPTION_MASK_ISA_SSE2 | OPTION_MASK_ISA_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
19416   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
19417   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
19418
19419   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
19420   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
19421   x86_mfence = def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
19422
19423   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
19424   def_builtin (OPTION_MASK_ISA_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
19425
19426   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
19427   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
19428
19429   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
19430   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
19431   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
19432   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
19433   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSLLW128);
19434   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSLLD128);
19435   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
19436
19437   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
19438   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
19439   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
19440   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
19441   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRLW128);
19442   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRLD128);
19443   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
19444
19445   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
19446   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
19447   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRAW128);
19448   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRAD128);
19449
19450   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
19451
19452   /* Prescott New Instructions.  */
19453   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_monitor", void_ftype_pcvoid_unsigned_unsigned, IX86_BUILTIN_MONITOR);
19454   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_mwait", void_ftype_unsigned_unsigned, IX86_BUILTIN_MWAIT);
19455   def_builtin (OPTION_MASK_ISA_SSE3, "__builtin_ia32_lddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
19456
19457   /* SSSE3.  */
19458   def_builtin_const (OPTION_MASK_ISA_SSSE3, "__builtin_ia32_palignr128", v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
19459   def_builtin_const (OPTION_MASK_ISA_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int, IX86_BUILTIN_PALIGNR);
19460
19461   /* SSE4.1. */
19462   def_builtin (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_movntdqa", v2di_ftype_pv2di, IX86_BUILTIN_MOVNTDQA);
19463   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxbw128", v8hi_ftype_v16qi, IX86_BUILTIN_PMOVSXBW128);
19464   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxbd128", v4si_ftype_v16qi, IX86_BUILTIN_PMOVSXBD128);
19465   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxbq128", v2di_ftype_v16qi, IX86_BUILTIN_PMOVSXBQ128);
19466   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxwd128", v4si_ftype_v8hi, IX86_BUILTIN_PMOVSXWD128);
19467   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxwq128", v2di_ftype_v8hi, IX86_BUILTIN_PMOVSXWQ128);
19468   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovsxdq128", v2di_ftype_v4si, IX86_BUILTIN_PMOVSXDQ128);
19469   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxbw128", v8hi_ftype_v16qi, IX86_BUILTIN_PMOVZXBW128);
19470   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxbd128", v4si_ftype_v16qi, IX86_BUILTIN_PMOVZXBD128);
19471   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxbq128", v2di_ftype_v16qi, IX86_BUILTIN_PMOVZXBQ128);
19472   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxwd128", v4si_ftype_v8hi, IX86_BUILTIN_PMOVZXWD128);
19473   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxwq128", v2di_ftype_v8hi, IX86_BUILTIN_PMOVZXWQ128);
19474   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmovzxdq128", v2di_ftype_v4si, IX86_BUILTIN_PMOVZXDQ128);
19475   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_pmuldq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULDQ128);
19476
19477   /* SSE4.1 and SSE5 */
19478   def_builtin_const (OPTION_MASK_ISA_ROUND, "__builtin_ia32_roundpd", v2df_ftype_v2df_int, IX86_BUILTIN_ROUNDPD);
19479   def_builtin_const (OPTION_MASK_ISA_ROUND, "__builtin_ia32_roundps", v4sf_ftype_v4sf_int, IX86_BUILTIN_ROUNDPS);
19480   def_builtin_const (OPTION_MASK_ISA_ROUND, "__builtin_ia32_roundsd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_ROUNDSD);
19481   def_builtin_const (OPTION_MASK_ISA_ROUND, "__builtin_ia32_roundss", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_ROUNDSS);
19482
19483   /* SSE4.2. */
19484   ftype = build_function_type_list (unsigned_type_node,
19485                                     unsigned_type_node,
19486                                     unsigned_char_type_node,
19487                                     NULL_TREE);
19488   def_builtin_const (OPTION_MASK_ISA_SSE4_2, "__builtin_ia32_crc32qi", ftype, IX86_BUILTIN_CRC32QI);
19489   ftype = build_function_type_list (unsigned_type_node,
19490                                     unsigned_type_node,
19491                                     short_unsigned_type_node,
19492                                     NULL_TREE);
19493   def_builtin_const (OPTION_MASK_ISA_SSE4_2, "__builtin_ia32_crc32hi", ftype, IX86_BUILTIN_CRC32HI);
19494   ftype = build_function_type_list (unsigned_type_node,
19495                                     unsigned_type_node,
19496                                     unsigned_type_node,
19497                                     NULL_TREE);
19498   def_builtin_const (OPTION_MASK_ISA_SSE4_2, "__builtin_ia32_crc32si", ftype, IX86_BUILTIN_CRC32SI);
19499   ftype = build_function_type_list (long_long_unsigned_type_node,
19500                                     long_long_unsigned_type_node,
19501                                     long_long_unsigned_type_node,
19502                                     NULL_TREE);
19503   def_builtin_const (OPTION_MASK_ISA_SSE4_2, "__builtin_ia32_crc32di", ftype, IX86_BUILTIN_CRC32DI);
19504
19505   /* AMDFAM10 SSE4A New built-ins  */
19506   def_builtin (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_movntsd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTSD);
19507   def_builtin (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_movntss", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTSS);
19508   def_builtin_const (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_extrqi", v2di_ftype_v2di_unsigned_unsigned, IX86_BUILTIN_EXTRQI);
19509   def_builtin_const (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_extrq", v2di_ftype_v2di_v16qi,  IX86_BUILTIN_EXTRQ);
19510   def_builtin_const (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_insertqi", v2di_ftype_v2di_v2di_unsigned_unsigned, IX86_BUILTIN_INSERTQI);
19511   def_builtin_const (OPTION_MASK_ISA_SSE4A, "__builtin_ia32_insertq", v2di_ftype_v2di_v2di, IX86_BUILTIN_INSERTQ);
19512
19513   /* Access to the vec_init patterns.  */
19514   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
19515                                     integer_type_node, NULL_TREE);
19516   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v2si", ftype, IX86_BUILTIN_VEC_INIT_V2SI);
19517
19518   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
19519                                     short_integer_type_node,
19520                                     short_integer_type_node,
19521                                     short_integer_type_node, NULL_TREE);
19522   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v4hi", ftype, IX86_BUILTIN_VEC_INIT_V4HI);
19523
19524   ftype = build_function_type_list (V8QI_type_node, char_type_node,
19525                                     char_type_node, char_type_node,
19526                                     char_type_node, char_type_node,
19527                                     char_type_node, char_type_node,
19528                                     char_type_node, NULL_TREE);
19529   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_init_v8qi", ftype, IX86_BUILTIN_VEC_INIT_V8QI);
19530
19531   /* Access to the vec_extract patterns.  */
19532   ftype = build_function_type_list (double_type_node, V2DF_type_node,
19533                                     integer_type_node, NULL_TREE);
19534   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2df", ftype, IX86_BUILTIN_VEC_EXT_V2DF);
19535
19536   ftype = build_function_type_list (long_long_integer_type_node,
19537                                     V2DI_type_node, integer_type_node,
19538                                     NULL_TREE);
19539   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v2di", ftype, IX86_BUILTIN_VEC_EXT_V2DI);
19540
19541   ftype = build_function_type_list (float_type_node, V4SF_type_node,
19542                                     integer_type_node, NULL_TREE);
19543   def_builtin_const (OPTION_MASK_ISA_SSE, "__builtin_ia32_vec_ext_v4sf", ftype, IX86_BUILTIN_VEC_EXT_V4SF);
19544
19545   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
19546                                     integer_type_node, NULL_TREE);
19547   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v4si", ftype, IX86_BUILTIN_VEC_EXT_V4SI);
19548
19549   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
19550                                     integer_type_node, NULL_TREE);
19551   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v8hi", ftype, IX86_BUILTIN_VEC_EXT_V8HI);
19552
19553   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
19554                                     integer_type_node, NULL_TREE);
19555   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_vec_ext_v4hi", ftype, IX86_BUILTIN_VEC_EXT_V4HI);
19556
19557   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
19558                                     integer_type_node, NULL_TREE);
19559   def_builtin_const (OPTION_MASK_ISA_MMX, "__builtin_ia32_vec_ext_v2si", ftype, IX86_BUILTIN_VEC_EXT_V2SI);
19560
19561   ftype = build_function_type_list (intQI_type_node, V16QI_type_node,
19562                                     integer_type_node, NULL_TREE);
19563   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_ext_v16qi", ftype, IX86_BUILTIN_VEC_EXT_V16QI);
19564
19565   /* Access to the vec_set patterns.  */
19566   ftype = build_function_type_list (V2DI_type_node, V2DI_type_node,
19567                                     intDI_type_node,
19568                                     integer_type_node, NULL_TREE);
19569   def_builtin_const (OPTION_MASK_ISA_SSE4_1 | OPTION_MASK_ISA_64BIT, "__builtin_ia32_vec_set_v2di", ftype, IX86_BUILTIN_VEC_SET_V2DI);
19570
19571   ftype = build_function_type_list (V4SF_type_node, V4SF_type_node,
19572                                     float_type_node,
19573                                     integer_type_node, NULL_TREE);
19574   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4sf", ftype, IX86_BUILTIN_VEC_SET_V4SF);
19575
19576   ftype = build_function_type_list (V4SI_type_node, V4SI_type_node,
19577                                     intSI_type_node,
19578                                     integer_type_node, NULL_TREE);
19579   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v4si", ftype, IX86_BUILTIN_VEC_SET_V4SI);
19580
19581   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
19582                                     intHI_type_node,
19583                                     integer_type_node, NULL_TREE);
19584   def_builtin_const (OPTION_MASK_ISA_SSE2, "__builtin_ia32_vec_set_v8hi", ftype, IX86_BUILTIN_VEC_SET_V8HI);
19585
19586   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
19587                                     intHI_type_node,
19588                                     integer_type_node, NULL_TREE);
19589   def_builtin_const (OPTION_MASK_ISA_SSE | OPTION_MASK_ISA_3DNOW_A, "__builtin_ia32_vec_set_v4hi", ftype, IX86_BUILTIN_VEC_SET_V4HI);
19590
19591   ftype = build_function_type_list (V16QI_type_node, V16QI_type_node,
19592                                     intQI_type_node,
19593                                     integer_type_node, NULL_TREE);
19594   def_builtin_const (OPTION_MASK_ISA_SSE4_1, "__builtin_ia32_vec_set_v16qi", ftype, IX86_BUILTIN_VEC_SET_V16QI);
19595
19596   /* Add SSE5 multi-arg argument instructions */
19597   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
19598     {
19599       tree mtype = NULL_TREE;
19600
19601       if (d->name == 0)
19602         continue;
19603
19604       switch ((enum multi_arg_type)d->flag)
19605         {
19606         case MULTI_ARG_3_SF:     mtype = v4sf_ftype_v4sf_v4sf_v4sf;     break;
19607         case MULTI_ARG_3_DF:     mtype = v2df_ftype_v2df_v2df_v2df;     break;
19608         case MULTI_ARG_3_DI:     mtype = v2di_ftype_v2di_v2di_v2di;     break;
19609         case MULTI_ARG_3_SI:     mtype = v4si_ftype_v4si_v4si_v4si;     break;
19610         case MULTI_ARG_3_SI_DI:  mtype = v4si_ftype_v4si_v4si_v2di;     break;
19611         case MULTI_ARG_3_HI:     mtype = v8hi_ftype_v8hi_v8hi_v8hi;     break;
19612         case MULTI_ARG_3_HI_SI:  mtype = v8hi_ftype_v8hi_v8hi_v4si;     break;
19613         case MULTI_ARG_3_QI:     mtype = v16qi_ftype_v16qi_v16qi_v16qi; break;
19614         case MULTI_ARG_3_PERMPS: mtype = v4sf_ftype_v4sf_v4sf_v16qi;    break;
19615         case MULTI_ARG_3_PERMPD: mtype = v2df_ftype_v2df_v2df_v16qi;    break;
19616         case MULTI_ARG_2_SF:     mtype = v4sf_ftype_v4sf_v4sf;          break;
19617         case MULTI_ARG_2_DF:     mtype = v2df_ftype_v2df_v2df;          break;
19618         case MULTI_ARG_2_DI:     mtype = v2di_ftype_v2di_v2di;          break;
19619         case MULTI_ARG_2_SI:     mtype = v4si_ftype_v4si_v4si;          break;
19620         case MULTI_ARG_2_HI:     mtype = v8hi_ftype_v8hi_v8hi;          break;
19621         case MULTI_ARG_2_QI:     mtype = v16qi_ftype_v16qi_v16qi;       break;
19622         case MULTI_ARG_2_DI_IMM: mtype = v2di_ftype_v2di_si;            break;
19623         case MULTI_ARG_2_SI_IMM: mtype = v4si_ftype_v4si_si;            break;
19624         case MULTI_ARG_2_HI_IMM: mtype = v8hi_ftype_v8hi_si;            break;
19625         case MULTI_ARG_2_QI_IMM: mtype = v16qi_ftype_v16qi_si;          break;
19626         case MULTI_ARG_2_SF_CMP: mtype = v4sf_ftype_v4sf_v4sf;          break;
19627         case MULTI_ARG_2_DF_CMP: mtype = v2df_ftype_v2df_v2df;          break;
19628         case MULTI_ARG_2_DI_CMP: mtype = v2di_ftype_v2di_v2di;          break;
19629         case MULTI_ARG_2_SI_CMP: mtype = v4si_ftype_v4si_v4si;          break;
19630         case MULTI_ARG_2_HI_CMP: mtype = v8hi_ftype_v8hi_v8hi;          break;
19631         case MULTI_ARG_2_QI_CMP: mtype = v16qi_ftype_v16qi_v16qi;       break;
19632         case MULTI_ARG_2_SF_TF:  mtype = v4sf_ftype_v4sf_v4sf;          break;
19633         case MULTI_ARG_2_DF_TF:  mtype = v2df_ftype_v2df_v2df;          break;
19634         case MULTI_ARG_2_DI_TF:  mtype = v2di_ftype_v2di_v2di;          break;
19635         case MULTI_ARG_2_SI_TF:  mtype = v4si_ftype_v4si_v4si;          break;
19636         case MULTI_ARG_2_HI_TF:  mtype = v8hi_ftype_v8hi_v8hi;          break;
19637         case MULTI_ARG_2_QI_TF:  mtype = v16qi_ftype_v16qi_v16qi;       break;
19638         case MULTI_ARG_1_SF:     mtype = v4sf_ftype_v4sf;               break;
19639         case MULTI_ARG_1_DF:     mtype = v2df_ftype_v2df;               break;
19640         case MULTI_ARG_1_DI:     mtype = v2di_ftype_v2di;               break;
19641         case MULTI_ARG_1_SI:     mtype = v4si_ftype_v4si;               break;
19642         case MULTI_ARG_1_HI:     mtype = v8hi_ftype_v8hi;               break;
19643         case MULTI_ARG_1_QI:     mtype = v16qi_ftype_v16qi;             break;
19644         case MULTI_ARG_1_SI_DI:  mtype = v2di_ftype_v4si;               break;
19645         case MULTI_ARG_1_HI_DI:  mtype = v2di_ftype_v8hi;               break;
19646         case MULTI_ARG_1_HI_SI:  mtype = v4si_ftype_v8hi;               break;
19647         case MULTI_ARG_1_QI_DI:  mtype = v2di_ftype_v16qi;              break;
19648         case MULTI_ARG_1_QI_SI:  mtype = v4si_ftype_v16qi;              break;
19649         case MULTI_ARG_1_QI_HI:  mtype = v8hi_ftype_v16qi;              break;
19650         case MULTI_ARG_1_PH2PS:  mtype = v4sf_ftype_v4hi;               break;
19651         case MULTI_ARG_1_PS2PH:  mtype = v4hi_ftype_v4sf;               break;
19652         case MULTI_ARG_UNKNOWN:
19653         default:
19654           gcc_unreachable ();
19655         }
19656
19657       if (mtype)
19658         def_builtin_const (d->mask, d->name, mtype, d->code);
19659     }
19660 }
19661
19662 static void
19663 ix86_init_builtins (void)
19664 {
19665   if (TARGET_MMX)
19666     ix86_init_mmx_sse_builtins ();
19667 }
19668
19669 /* Errors in the source file can cause expand_expr to return const0_rtx
19670    where we expect a vector.  To avoid crashing, use one of the vector
19671    clear instructions.  */
19672 static rtx
19673 safe_vector_operand (rtx x, enum machine_mode mode)
19674 {
19675   if (x == const0_rtx)
19676     x = CONST0_RTX (mode);
19677   return x;
19678 }
19679
19680 /* Subroutine of ix86_expand_builtin to take care of SSE insns with
19681    4 operands. The third argument must be a constant smaller than 8
19682    bits or xmm0.  */
19683
19684 static rtx
19685 ix86_expand_sse_4_operands_builtin (enum insn_code icode, tree exp,
19686                                     rtx target)
19687 {
19688   rtx pat;
19689   tree arg0 = CALL_EXPR_ARG (exp, 0);
19690   tree arg1 = CALL_EXPR_ARG (exp, 1);
19691   tree arg2 = CALL_EXPR_ARG (exp, 2);
19692   rtx op0 = expand_normal (arg0);
19693   rtx op1 = expand_normal (arg1);
19694   rtx op2 = expand_normal (arg2);
19695   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19696   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
19697   enum machine_mode mode2 = insn_data[icode].operand[2].mode;
19698   enum machine_mode mode3 = insn_data[icode].operand[3].mode;
19699
19700   if (VECTOR_MODE_P (mode1))
19701     op0 = safe_vector_operand (op0, mode1);
19702   if (VECTOR_MODE_P (mode2))
19703     op1 = safe_vector_operand (op1, mode2);
19704   if (VECTOR_MODE_P (mode3))
19705     op2 = safe_vector_operand (op2, mode3);
19706
19707   if (optimize
19708       || target == 0
19709       || GET_MODE (target) != tmode
19710       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19711     target = gen_reg_rtx (tmode);
19712
19713   if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
19714     op0 = copy_to_mode_reg (mode1, op0);
19715   if ((optimize && !register_operand (op1, mode2))
19716       || !(*insn_data[icode].operand[2].predicate) (op1, mode2))
19717     op1 = copy_to_mode_reg (mode2, op1);
19718
19719   if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
19720     switch (icode)
19721       {
19722       case CODE_FOR_sse4_1_blendvpd:
19723       case CODE_FOR_sse4_1_blendvps:
19724       case CODE_FOR_sse4_1_pblendvb:
19725         op2 = copy_to_mode_reg (mode3, op2);
19726         break;
19727
19728       case CODE_FOR_sse4_1_roundsd:
19729       case CODE_FOR_sse4_1_roundss:
19730         error ("the third argument must be a 4-bit immediate");
19731         return const0_rtx;
19732
19733       default:
19734         error ("the third argument must be an 8-bit immediate");
19735         return const0_rtx;
19736       }
19737
19738   pat = GEN_FCN (icode) (target, op0, op1, op2);
19739   if (! pat)
19740     return 0;
19741   emit_insn (pat);
19742   return target;
19743 }
19744
19745 /* Subroutine of ix86_expand_builtin to take care of crc32 insns.  */
19746
19747 static rtx
19748 ix86_expand_crc32 (enum insn_code icode, tree exp, rtx target)
19749 {
19750   rtx pat;
19751   tree arg0 = CALL_EXPR_ARG (exp, 0);
19752   tree arg1 = CALL_EXPR_ARG (exp, 1);
19753   rtx op0 = expand_normal (arg0);
19754   rtx op1 = expand_normal (arg1);
19755   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19756   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19757   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19758
19759   if (optimize
19760       || !target
19761       || GET_MODE (target) != tmode
19762       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19763     target = gen_reg_rtx (tmode);
19764
19765   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
19766     op0 = copy_to_mode_reg (mode0, op0);
19767   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
19768     {
19769       op1 = copy_to_reg (op1);
19770       op1 = simplify_gen_subreg (mode1, op1, GET_MODE (op1), 0);
19771     }
19772
19773   pat = GEN_FCN (icode) (target, op0, op1);
19774   if (! pat)
19775     return 0;
19776   emit_insn (pat);
19777   return target;
19778 }
19779
19780 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
19781
19782 static rtx
19783 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
19784 {
19785   rtx pat, xops[3];
19786   tree arg0 = CALL_EXPR_ARG (exp, 0);
19787   tree arg1 = CALL_EXPR_ARG (exp, 1);
19788   rtx op0 = expand_normal (arg0);
19789   rtx op1 = expand_normal (arg1);
19790   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19791   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
19792   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
19793
19794   if (VECTOR_MODE_P (mode0))
19795     op0 = safe_vector_operand (op0, mode0);
19796   if (VECTOR_MODE_P (mode1))
19797     op1 = safe_vector_operand (op1, mode1);
19798
19799   if (optimize || !target
19800       || GET_MODE (target) != tmode
19801       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19802     target = gen_reg_rtx (tmode);
19803
19804   if (GET_MODE (op1) == SImode && mode1 == TImode)
19805     {
19806       rtx x = gen_reg_rtx (V4SImode);
19807       emit_insn (gen_sse2_loadd (x, op1));
19808       op1 = gen_lowpart (TImode, x);
19809     }
19810
19811   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
19812     op0 = copy_to_mode_reg (mode0, op0);
19813   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
19814     op1 = copy_to_mode_reg (mode1, op1);
19815
19816   /* ??? Using ix86_fixup_binary_operands is problematic when
19817      we've got mismatched modes.  Fake it.  */
19818
19819   xops[0] = target;
19820   xops[1] = op0;
19821   xops[2] = op1;
19822
19823   if (tmode == mode0 && tmode == mode1)
19824     {
19825       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
19826       op0 = xops[1];
19827       op1 = xops[2];
19828     }
19829   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
19830     {
19831       op0 = force_reg (mode0, op0);
19832       op1 = force_reg (mode1, op1);
19833       target = gen_reg_rtx (tmode);
19834     }
19835
19836   pat = GEN_FCN (icode) (target, op0, op1);
19837   if (! pat)
19838     return 0;
19839   emit_insn (pat);
19840   return target;
19841 }
19842
19843 /* Subroutine of ix86_expand_builtin to take care of 2-4 argument insns.  */
19844
19845 static rtx
19846 ix86_expand_multi_arg_builtin (enum insn_code icode, tree exp, rtx target,
19847                                enum multi_arg_type m_type,
19848                                enum insn_code sub_code)
19849 {
19850   rtx pat;
19851   int i;
19852   int nargs;
19853   bool comparison_p = false;
19854   bool tf_p = false;
19855   bool last_arg_constant = false;
19856   int num_memory = 0;
19857   struct {
19858     rtx op;
19859     enum machine_mode mode;
19860   } args[4];
19861
19862   enum machine_mode tmode = insn_data[icode].operand[0].mode;
19863
19864   switch (m_type)
19865     {
19866     case MULTI_ARG_3_SF:
19867     case MULTI_ARG_3_DF:
19868     case MULTI_ARG_3_DI:
19869     case MULTI_ARG_3_SI:
19870     case MULTI_ARG_3_SI_DI:
19871     case MULTI_ARG_3_HI:
19872     case MULTI_ARG_3_HI_SI:
19873     case MULTI_ARG_3_QI:
19874     case MULTI_ARG_3_PERMPS:
19875     case MULTI_ARG_3_PERMPD:
19876       nargs = 3;
19877       break;
19878
19879     case MULTI_ARG_2_SF:
19880     case MULTI_ARG_2_DF:
19881     case MULTI_ARG_2_DI:
19882     case MULTI_ARG_2_SI:
19883     case MULTI_ARG_2_HI:
19884     case MULTI_ARG_2_QI:
19885       nargs = 2;
19886       break;
19887
19888     case MULTI_ARG_2_DI_IMM:
19889     case MULTI_ARG_2_SI_IMM:
19890     case MULTI_ARG_2_HI_IMM:
19891     case MULTI_ARG_2_QI_IMM:
19892       nargs = 2;
19893       last_arg_constant = true;
19894       break;
19895
19896     case MULTI_ARG_1_SF:
19897     case MULTI_ARG_1_DF:
19898     case MULTI_ARG_1_DI:
19899     case MULTI_ARG_1_SI:
19900     case MULTI_ARG_1_HI:
19901     case MULTI_ARG_1_QI:
19902     case MULTI_ARG_1_SI_DI:
19903     case MULTI_ARG_1_HI_DI:
19904     case MULTI_ARG_1_HI_SI:
19905     case MULTI_ARG_1_QI_DI:
19906     case MULTI_ARG_1_QI_SI:
19907     case MULTI_ARG_1_QI_HI:
19908     case MULTI_ARG_1_PH2PS:
19909     case MULTI_ARG_1_PS2PH:
19910       nargs = 1;
19911       break;
19912
19913     case MULTI_ARG_2_SF_CMP:
19914     case MULTI_ARG_2_DF_CMP:
19915     case MULTI_ARG_2_DI_CMP:
19916     case MULTI_ARG_2_SI_CMP:
19917     case MULTI_ARG_2_HI_CMP:
19918     case MULTI_ARG_2_QI_CMP:
19919       nargs = 2;
19920       comparison_p = true;
19921       break;
19922
19923     case MULTI_ARG_2_SF_TF:
19924     case MULTI_ARG_2_DF_TF:
19925     case MULTI_ARG_2_DI_TF:
19926     case MULTI_ARG_2_SI_TF:
19927     case MULTI_ARG_2_HI_TF:
19928     case MULTI_ARG_2_QI_TF:
19929       nargs = 2;
19930       tf_p = true;
19931       break;
19932
19933     case MULTI_ARG_UNKNOWN:
19934     default:
19935       gcc_unreachable ();
19936     }
19937
19938   if (optimize || !target
19939       || GET_MODE (target) != tmode
19940       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
19941     target = gen_reg_rtx (tmode);
19942
19943   gcc_assert (nargs <= 4);
19944
19945   for (i = 0; i < nargs; i++)
19946     {
19947       tree arg = CALL_EXPR_ARG (exp, i);
19948       rtx op = expand_normal (arg);
19949       int adjust = (comparison_p) ? 1 : 0;
19950       enum machine_mode mode = insn_data[icode].operand[i+adjust+1].mode;
19951
19952       if (last_arg_constant && i == nargs-1)
19953         {
19954           if (GET_CODE (op) != CONST_INT)
19955             {
19956               error ("last argument must be an immediate");
19957               return gen_reg_rtx (tmode);
19958             }
19959         }
19960       else
19961         {
19962           if (VECTOR_MODE_P (mode))
19963             op = safe_vector_operand (op, mode);
19964
19965           /* If we aren't optimizing, only allow one memory operand to be
19966              generated.  */
19967           if (memory_operand (op, mode))
19968             num_memory++;
19969
19970           gcc_assert (GET_MODE (op) == mode || GET_MODE (op) == VOIDmode);
19971
19972           if (optimize
19973               || ! (*insn_data[icode].operand[i+adjust+1].predicate) (op, mode)
19974               || num_memory > 1)
19975             op = force_reg (mode, op);
19976         }
19977
19978       args[i].op = op;
19979       args[i].mode = mode;
19980     }
19981
19982   switch (nargs)
19983     {
19984     case 1:
19985       pat = GEN_FCN (icode) (target, args[0].op);
19986       break;
19987
19988     case 2:
19989       if (tf_p)
19990         pat = GEN_FCN (icode) (target, args[0].op, args[1].op,
19991                                GEN_INT ((int)sub_code));
19992       else if (! comparison_p)
19993         pat = GEN_FCN (icode) (target, args[0].op, args[1].op);
19994       else
19995         {
19996           rtx cmp_op = gen_rtx_fmt_ee (sub_code, GET_MODE (target),
19997                                        args[0].op,
19998                                        args[1].op);
19999
20000           pat = GEN_FCN (icode) (target, cmp_op, args[0].op, args[1].op);
20001         }
20002       break;
20003
20004     case 3:
20005       pat = GEN_FCN (icode) (target, args[0].op, args[1].op, args[2].op);
20006       break;
20007
20008     default:
20009       gcc_unreachable ();
20010     }
20011
20012   if (! pat)
20013     return 0;
20014
20015   emit_insn (pat);
20016   return target;
20017 }
20018
20019 /* Subroutine of ix86_expand_builtin to take care of stores.  */
20020
20021 static rtx
20022 ix86_expand_store_builtin (enum insn_code icode, tree exp)
20023 {
20024   rtx pat;
20025   tree arg0 = CALL_EXPR_ARG (exp, 0);
20026   tree arg1 = CALL_EXPR_ARG (exp, 1);
20027   rtx op0 = expand_normal (arg0);
20028   rtx op1 = expand_normal (arg1);
20029   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
20030   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
20031
20032   if (VECTOR_MODE_P (mode1))
20033     op1 = safe_vector_operand (op1, mode1);
20034
20035   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
20036   op1 = copy_to_mode_reg (mode1, op1);
20037
20038   pat = GEN_FCN (icode) (op0, op1);
20039   if (pat)
20040     emit_insn (pat);
20041   return 0;
20042 }
20043
20044 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
20045
20046 static rtx
20047 ix86_expand_unop_builtin (enum insn_code icode, tree exp,
20048                           rtx target, int do_load)
20049 {
20050   rtx pat;
20051   tree arg0 = CALL_EXPR_ARG (exp, 0);
20052   rtx op0 = expand_normal (arg0);
20053   enum machine_mode tmode = insn_data[icode].operand[0].mode;
20054   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
20055
20056   if (optimize || !target
20057       || GET_MODE (target) != tmode
20058       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20059     target = gen_reg_rtx (tmode);
20060   if (do_load)
20061     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
20062   else
20063     {
20064       if (VECTOR_MODE_P (mode0))
20065         op0 = safe_vector_operand (op0, mode0);
20066
20067       if ((optimize && !register_operand (op0, mode0))
20068           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20069         op0 = copy_to_mode_reg (mode0, op0);
20070     }
20071
20072   switch (icode)
20073     {
20074     case CODE_FOR_sse4_1_roundpd:
20075     case CODE_FOR_sse4_1_roundps:
20076         {
20077           tree arg1 = CALL_EXPR_ARG (exp, 1);
20078           rtx op1 = expand_normal (arg1);
20079           enum machine_mode mode1 = insn_data[icode].operand[2].mode;
20080
20081           if (! (*insn_data[icode].operand[2].predicate) (op1, mode1))
20082             {
20083               error ("the second argument must be a 4-bit immediate");
20084               return const0_rtx;
20085             }
20086           pat = GEN_FCN (icode) (target, op0, op1);
20087         }
20088       break;
20089     default:
20090       pat = GEN_FCN (icode) (target, op0);
20091       break;
20092     }
20093
20094   if (! pat)
20095     return 0;
20096   emit_insn (pat);
20097   return target;
20098 }
20099
20100 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
20101    sqrtss, rsqrtss, rcpss.  */
20102
20103 static rtx
20104 ix86_expand_unop1_builtin (enum insn_code icode, tree exp, rtx target)
20105 {
20106   rtx pat;
20107   tree arg0 = CALL_EXPR_ARG (exp, 0);
20108   rtx op1, op0 = expand_normal (arg0);
20109   enum machine_mode tmode = insn_data[icode].operand[0].mode;
20110   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
20111
20112   if (optimize || !target
20113       || GET_MODE (target) != tmode
20114       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20115     target = gen_reg_rtx (tmode);
20116
20117   if (VECTOR_MODE_P (mode0))
20118     op0 = safe_vector_operand (op0, mode0);
20119
20120   if ((optimize && !register_operand (op0, mode0))
20121       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20122     op0 = copy_to_mode_reg (mode0, op0);
20123
20124   op1 = op0;
20125   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
20126     op1 = copy_to_mode_reg (mode0, op1);
20127
20128   pat = GEN_FCN (icode) (target, op0, op1);
20129   if (! pat)
20130     return 0;
20131   emit_insn (pat);
20132   return target;
20133 }
20134
20135 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
20136
20137 static rtx
20138 ix86_expand_sse_compare (const struct builtin_description *d, tree exp,
20139                          rtx target)
20140 {
20141   rtx pat;
20142   tree arg0 = CALL_EXPR_ARG (exp, 0);
20143   tree arg1 = CALL_EXPR_ARG (exp, 1);
20144   rtx op0 = expand_normal (arg0);
20145   rtx op1 = expand_normal (arg1);
20146   rtx op2;
20147   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
20148   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
20149   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
20150   enum rtx_code comparison = d->comparison;
20151
20152   if (VECTOR_MODE_P (mode0))
20153     op0 = safe_vector_operand (op0, mode0);
20154   if (VECTOR_MODE_P (mode1))
20155     op1 = safe_vector_operand (op1, mode1);
20156
20157   /* Swap operands if we have a comparison that isn't available in
20158      hardware.  */
20159   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
20160     {
20161       rtx tmp = gen_reg_rtx (mode1);
20162       emit_move_insn (tmp, op1);
20163       op1 = op0;
20164       op0 = tmp;
20165     }
20166
20167   if (optimize || !target
20168       || GET_MODE (target) != tmode
20169       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
20170     target = gen_reg_rtx (tmode);
20171
20172   if ((optimize && !register_operand (op0, mode0))
20173       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
20174     op0 = copy_to_mode_reg (mode0, op0);
20175   if ((optimize && !register_operand (op1, mode1))
20176       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
20177     op1 = copy_to_mode_reg (mode1, op1);
20178
20179   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
20180   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
20181   if (! pat)
20182     return 0;
20183   emit_insn (pat);
20184   return target;
20185 }
20186
20187 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
20188
20189 static rtx
20190 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
20191                       rtx target)
20192 {
20193   rtx pat;
20194   tree arg0 = CALL_EXPR_ARG (exp, 0);
20195   tree arg1 = CALL_EXPR_ARG (exp, 1);
20196   rtx op0 = expand_normal (arg0);
20197   rtx op1 = expand_normal (arg1);
20198   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
20199   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
20200   enum rtx_code comparison = d->comparison;
20201
20202   if (VECTOR_MODE_P (mode0))
20203     op0 = safe_vector_operand (op0, mode0);
20204   if (VECTOR_MODE_P (mode1))
20205     op1 = safe_vector_operand (op1, mode1);
20206
20207   /* Swap operands if we have a comparison that isn't available in
20208      hardware.  */
20209   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
20210     {
20211       rtx tmp = op1;
20212       op1 = op0;
20213       op0 = tmp;
20214     }
20215
20216   target = gen_reg_rtx (SImode);
20217   emit_move_insn (target, const0_rtx);
20218   target = gen_rtx_SUBREG (QImode, target, 0);
20219
20220   if ((optimize && !register_operand (op0, mode0))
20221       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
20222     op0 = copy_to_mode_reg (mode0, op0);
20223   if ((optimize && !register_operand (op1, mode1))
20224       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
20225     op1 = copy_to_mode_reg (mode1, op1);
20226
20227   pat = GEN_FCN (d->icode) (op0, op1);
20228   if (! pat)
20229     return 0;
20230   emit_insn (pat);
20231   emit_insn (gen_rtx_SET (VOIDmode,
20232                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
20233                           gen_rtx_fmt_ee (comparison, QImode,
20234                                           SET_DEST (pat),
20235                                           const0_rtx)));
20236
20237   return SUBREG_REG (target);
20238 }
20239
20240 /* Subroutine of ix86_expand_builtin to take care of ptest insns.  */
20241
20242 static rtx
20243 ix86_expand_sse_ptest (const struct builtin_description *d, tree exp,
20244                        rtx target)
20245 {
20246   rtx pat;
20247   tree arg0 = CALL_EXPR_ARG (exp, 0);
20248   tree arg1 = CALL_EXPR_ARG (exp, 1);
20249   rtx op0 = expand_normal (arg0);
20250   rtx op1 = expand_normal (arg1);
20251   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
20252   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
20253   enum rtx_code comparison = d->comparison;
20254
20255   if (VECTOR_MODE_P (mode0))
20256     op0 = safe_vector_operand (op0, mode0);
20257   if (VECTOR_MODE_P (mode1))
20258     op1 = safe_vector_operand (op1, mode1);
20259
20260   target = gen_reg_rtx (SImode);
20261   emit_move_insn (target, const0_rtx);
20262   target = gen_rtx_SUBREG (QImode, target, 0);
20263
20264   if ((optimize && !register_operand (op0, mode0))
20265       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
20266     op0 = copy_to_mode_reg (mode0, op0);
20267   if ((optimize && !register_operand (op1, mode1))
20268       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
20269     op1 = copy_to_mode_reg (mode1, op1);
20270
20271   pat = GEN_FCN (d->icode) (op0, op1);
20272   if (! pat)
20273     return 0;
20274   emit_insn (pat);
20275   emit_insn (gen_rtx_SET (VOIDmode,
20276                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
20277                           gen_rtx_fmt_ee (comparison, QImode,
20278                                           SET_DEST (pat),
20279                                           const0_rtx)));
20280
20281   return SUBREG_REG (target);
20282 }
20283
20284 /* Subroutine of ix86_expand_builtin to take care of pcmpestr[im] insns.  */
20285
20286 static rtx
20287 ix86_expand_sse_pcmpestr (const struct builtin_description *d,
20288                           tree exp, rtx target)
20289 {
20290   rtx pat;
20291   tree arg0 = CALL_EXPR_ARG (exp, 0);
20292   tree arg1 = CALL_EXPR_ARG (exp, 1);
20293   tree arg2 = CALL_EXPR_ARG (exp, 2);
20294   tree arg3 = CALL_EXPR_ARG (exp, 3);
20295   tree arg4 = CALL_EXPR_ARG (exp, 4);
20296   rtx scratch0, scratch1;
20297   rtx op0 = expand_normal (arg0);
20298   rtx op1 = expand_normal (arg1);
20299   rtx op2 = expand_normal (arg2);
20300   rtx op3 = expand_normal (arg3);
20301   rtx op4 = expand_normal (arg4);
20302   enum machine_mode tmode0, tmode1, modev2, modei3, modev4, modei5, modeimm;
20303
20304   tmode0 = insn_data[d->icode].operand[0].mode;
20305   tmode1 = insn_data[d->icode].operand[1].mode;
20306   modev2 = insn_data[d->icode].operand[2].mode;
20307   modei3 = insn_data[d->icode].operand[3].mode;
20308   modev4 = insn_data[d->icode].operand[4].mode;
20309   modei5 = insn_data[d->icode].operand[5].mode;
20310   modeimm = insn_data[d->icode].operand[6].mode;
20311
20312   if (VECTOR_MODE_P (modev2))
20313     op0 = safe_vector_operand (op0, modev2);
20314   if (VECTOR_MODE_P (modev4))
20315     op2 = safe_vector_operand (op2, modev4);
20316
20317   if (! (*insn_data[d->icode].operand[2].predicate) (op0, modev2))
20318     op0 = copy_to_mode_reg (modev2, op0);
20319   if (! (*insn_data[d->icode].operand[3].predicate) (op1, modei3))
20320     op1 = copy_to_mode_reg (modei3, op1);
20321   if ((optimize && !register_operand (op2, modev4))
20322       || !(*insn_data[d->icode].operand[4].predicate) (op2, modev4))
20323     op2 = copy_to_mode_reg (modev4, op2);
20324   if (! (*insn_data[d->icode].operand[5].predicate) (op3, modei5))
20325     op3 = copy_to_mode_reg (modei5, op3);
20326
20327   if (! (*insn_data[d->icode].operand[6].predicate) (op4, modeimm))
20328     {
20329       error ("the fifth argument must be a 8-bit immediate");
20330       return const0_rtx;
20331     }
20332
20333   if (d->code == IX86_BUILTIN_PCMPESTRI128)
20334     {
20335       if (optimize || !target
20336           || GET_MODE (target) != tmode0
20337           || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode0))
20338         target = gen_reg_rtx (tmode0);
20339
20340       scratch1 = gen_reg_rtx (tmode1);
20341
20342       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2, op3, op4);
20343     }
20344   else if (d->code == IX86_BUILTIN_PCMPESTRM128)
20345     {
20346       if (optimize || !target
20347           || GET_MODE (target) != tmode1
20348           || ! (*insn_data[d->icode].operand[1].predicate) (target, tmode1))
20349         target = gen_reg_rtx (tmode1);
20350
20351       scratch0 = gen_reg_rtx (tmode0);
20352
20353       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2, op3, op4);
20354     }
20355   else
20356     {
20357       gcc_assert (d->flag);
20358
20359       scratch0 = gen_reg_rtx (tmode0);
20360       scratch1 = gen_reg_rtx (tmode1);
20361
20362       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2, op3, op4);
20363     }
20364
20365   if (! pat)
20366     return 0;
20367
20368   emit_insn (pat);
20369
20370   if (d->flag)
20371     {
20372       target = gen_reg_rtx (SImode);
20373       emit_move_insn (target, const0_rtx);
20374       target = gen_rtx_SUBREG (QImode, target, 0);
20375
20376       emit_insn
20377         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
20378                       gen_rtx_fmt_ee (EQ, QImode,
20379                                       gen_rtx_REG ((enum machine_mode) d->flag,
20380                                                    FLAGS_REG),
20381                                       const0_rtx)));
20382       return SUBREG_REG (target);
20383     }
20384   else
20385     return target;
20386 }
20387
20388
20389 /* Subroutine of ix86_expand_builtin to take care of pcmpistr[im] insns.  */
20390
20391 static rtx
20392 ix86_expand_sse_pcmpistr (const struct builtin_description *d,
20393                           tree exp, rtx target)
20394 {
20395   rtx pat;
20396   tree arg0 = CALL_EXPR_ARG (exp, 0);
20397   tree arg1 = CALL_EXPR_ARG (exp, 1);
20398   tree arg2 = CALL_EXPR_ARG (exp, 2);
20399   rtx scratch0, scratch1;
20400   rtx op0 = expand_normal (arg0);
20401   rtx op1 = expand_normal (arg1);
20402   rtx op2 = expand_normal (arg2);
20403   enum machine_mode tmode0, tmode1, modev2, modev3, modeimm;
20404
20405   tmode0 = insn_data[d->icode].operand[0].mode;
20406   tmode1 = insn_data[d->icode].operand[1].mode;
20407   modev2 = insn_data[d->icode].operand[2].mode;
20408   modev3 = insn_data[d->icode].operand[3].mode;
20409   modeimm = insn_data[d->icode].operand[4].mode;
20410
20411   if (VECTOR_MODE_P (modev2))
20412     op0 = safe_vector_operand (op0, modev2);
20413   if (VECTOR_MODE_P (modev3))
20414     op1 = safe_vector_operand (op1, modev3);
20415
20416   if (! (*insn_data[d->icode].operand[2].predicate) (op0, modev2))
20417     op0 = copy_to_mode_reg (modev2, op0);
20418   if ((optimize && !register_operand (op1, modev3))
20419       || !(*insn_data[d->icode].operand[3].predicate) (op1, modev3))
20420     op1 = copy_to_mode_reg (modev3, op1);
20421
20422   if (! (*insn_data[d->icode].operand[4].predicate) (op2, modeimm))
20423     {
20424       error ("the third argument must be a 8-bit immediate");
20425       return const0_rtx;
20426     }
20427
20428   if (d->code == IX86_BUILTIN_PCMPISTRI128)
20429     {
20430       if (optimize || !target
20431           || GET_MODE (target) != tmode0
20432           || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode0))
20433         target = gen_reg_rtx (tmode0);
20434
20435       scratch1 = gen_reg_rtx (tmode1);
20436
20437       pat = GEN_FCN (d->icode) (target, scratch1, op0, op1, op2);
20438     }
20439   else if (d->code == IX86_BUILTIN_PCMPISTRM128)
20440     {
20441       if (optimize || !target
20442           || GET_MODE (target) != tmode1
20443           || ! (*insn_data[d->icode].operand[1].predicate) (target, tmode1))
20444         target = gen_reg_rtx (tmode1);
20445
20446       scratch0 = gen_reg_rtx (tmode0);
20447
20448       pat = GEN_FCN (d->icode) (scratch0, target, op0, op1, op2);
20449     }
20450   else
20451     {
20452       gcc_assert (d->flag);
20453
20454       scratch0 = gen_reg_rtx (tmode0);
20455       scratch1 = gen_reg_rtx (tmode1);
20456
20457       pat = GEN_FCN (d->icode) (scratch0, scratch1, op0, op1, op2);
20458     }
20459
20460   if (! pat)
20461     return 0;
20462
20463   emit_insn (pat);
20464
20465   if (d->flag)
20466     {
20467       target = gen_reg_rtx (SImode);
20468       emit_move_insn (target, const0_rtx);
20469       target = gen_rtx_SUBREG (QImode, target, 0);
20470
20471       emit_insn
20472         (gen_rtx_SET (VOIDmode, gen_rtx_STRICT_LOW_PART (VOIDmode, target),
20473                       gen_rtx_fmt_ee (EQ, QImode,
20474                                       gen_rtx_REG ((enum machine_mode) d->flag,
20475                                                    FLAGS_REG),
20476                                       const0_rtx)));
20477       return SUBREG_REG (target);
20478     }
20479   else
20480     return target;
20481 }
20482
20483 /* Return the integer constant in ARG.  Constrain it to be in the range
20484    of the subparts of VEC_TYPE; issue an error if not.  */
20485
20486 static int
20487 get_element_number (tree vec_type, tree arg)
20488 {
20489   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
20490
20491   if (!host_integerp (arg, 1)
20492       || (elt = tree_low_cst (arg, 1), elt > max))
20493     {
20494       error ("selector must be an integer constant in the range 0..%wi", max);
20495       return 0;
20496     }
20497
20498   return elt;
20499 }
20500
20501 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
20502    ix86_expand_vector_init.  We DO have language-level syntax for this, in
20503    the form of  (type){ init-list }.  Except that since we can't place emms
20504    instructions from inside the compiler, we can't allow the use of MMX
20505    registers unless the user explicitly asks for it.  So we do *not* define
20506    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
20507    we have builtins invoked by mmintrin.h that gives us license to emit
20508    these sorts of instructions.  */
20509
20510 static rtx
20511 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
20512 {
20513   enum machine_mode tmode = TYPE_MODE (type);
20514   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
20515   int i, n_elt = GET_MODE_NUNITS (tmode);
20516   rtvec v = rtvec_alloc (n_elt);
20517
20518   gcc_assert (VECTOR_MODE_P (tmode));
20519   gcc_assert (call_expr_nargs (exp) == n_elt);
20520
20521   for (i = 0; i < n_elt; ++i)
20522     {
20523       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
20524       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
20525     }
20526
20527   if (!target || !register_operand (target, tmode))
20528     target = gen_reg_rtx (tmode);
20529
20530   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
20531   return target;
20532 }
20533
20534 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
20535    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
20536    had a language-level syntax for referencing vector elements.  */
20537
20538 static rtx
20539 ix86_expand_vec_ext_builtin (tree exp, rtx target)
20540 {
20541   enum machine_mode tmode, mode0;
20542   tree arg0, arg1;
20543   int elt;
20544   rtx op0;
20545
20546   arg0 = CALL_EXPR_ARG (exp, 0);
20547   arg1 = CALL_EXPR_ARG (exp, 1);
20548
20549   op0 = expand_normal (arg0);
20550   elt = get_element_number (TREE_TYPE (arg0), arg1);
20551
20552   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
20553   mode0 = TYPE_MODE (TREE_TYPE (arg0));
20554   gcc_assert (VECTOR_MODE_P (mode0));
20555
20556   op0 = force_reg (mode0, op0);
20557
20558   if (optimize || !target || !register_operand (target, tmode))
20559     target = gen_reg_rtx (tmode);
20560
20561   ix86_expand_vector_extract (true, target, op0, elt);
20562
20563   return target;
20564 }
20565
20566 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
20567    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
20568    a language-level syntax for referencing vector elements.  */
20569
20570 static rtx
20571 ix86_expand_vec_set_builtin (tree exp)
20572 {
20573   enum machine_mode tmode, mode1;
20574   tree arg0, arg1, arg2;
20575   int elt;
20576   rtx op0, op1, target;
20577
20578   arg0 = CALL_EXPR_ARG (exp, 0);
20579   arg1 = CALL_EXPR_ARG (exp, 1);
20580   arg2 = CALL_EXPR_ARG (exp, 2);
20581
20582   tmode = TYPE_MODE (TREE_TYPE (arg0));
20583   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
20584   gcc_assert (VECTOR_MODE_P (tmode));
20585
20586   op0 = expand_expr (arg0, NULL_RTX, tmode, EXPAND_NORMAL);
20587   op1 = expand_expr (arg1, NULL_RTX, mode1, EXPAND_NORMAL);
20588   elt = get_element_number (TREE_TYPE (arg0), arg2);
20589
20590   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
20591     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
20592
20593   op0 = force_reg (tmode, op0);
20594   op1 = force_reg (mode1, op1);
20595
20596   /* OP0 is the source of these builtin functions and shouldn't be
20597      modified.  Create a copy, use it and return it as target.  */
20598   target = gen_reg_rtx (tmode);
20599   emit_move_insn (target, op0);
20600   ix86_expand_vector_set (true, target, op1, elt);
20601
20602   return target;
20603 }
20604
20605 /* Expand an expression EXP that calls a built-in function,
20606    with result going to TARGET if that's convenient
20607    (and in mode MODE if that's convenient).
20608    SUBTARGET may be used as the target for computing one of EXP's operands.
20609    IGNORE is nonzero if the value is to be ignored.  */
20610
20611 static rtx
20612 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
20613                      enum machine_mode mode ATTRIBUTE_UNUSED,
20614                      int ignore ATTRIBUTE_UNUSED)
20615 {
20616   const struct builtin_description *d;
20617   size_t i;
20618   enum insn_code icode;
20619   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
20620   tree arg0, arg1, arg2, arg3;
20621   rtx op0, op1, op2, op3, pat;
20622   enum machine_mode tmode, mode0, mode1, mode2, mode3, mode4;
20623   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
20624
20625   switch (fcode)
20626     {
20627     case IX86_BUILTIN_EMMS:
20628       emit_insn (gen_mmx_emms ());
20629       return 0;
20630
20631     case IX86_BUILTIN_SFENCE:
20632       emit_insn (gen_sse_sfence ());
20633       return 0;
20634
20635     case IX86_BUILTIN_MASKMOVQ:
20636     case IX86_BUILTIN_MASKMOVDQU:
20637       icode = (fcode == IX86_BUILTIN_MASKMOVQ
20638                ? CODE_FOR_mmx_maskmovq
20639                : CODE_FOR_sse2_maskmovdqu);
20640       /* Note the arg order is different from the operand order.  */
20641       arg1 = CALL_EXPR_ARG (exp, 0);
20642       arg2 = CALL_EXPR_ARG (exp, 1);
20643       arg0 = CALL_EXPR_ARG (exp, 2);
20644       op0 = expand_normal (arg0);
20645       op1 = expand_normal (arg1);
20646       op2 = expand_normal (arg2);
20647       mode0 = insn_data[icode].operand[0].mode;
20648       mode1 = insn_data[icode].operand[1].mode;
20649       mode2 = insn_data[icode].operand[2].mode;
20650
20651       op0 = force_reg (Pmode, op0);
20652       op0 = gen_rtx_MEM (mode1, op0);
20653
20654       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
20655         op0 = copy_to_mode_reg (mode0, op0);
20656       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
20657         op1 = copy_to_mode_reg (mode1, op1);
20658       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
20659         op2 = copy_to_mode_reg (mode2, op2);
20660       pat = GEN_FCN (icode) (op0, op1, op2);
20661       if (! pat)
20662         return 0;
20663       emit_insn (pat);
20664       return 0;
20665
20666     case IX86_BUILTIN_RSQRTF:
20667       return ix86_expand_unop1_builtin (CODE_FOR_rsqrtsf2, exp, target);
20668
20669     case IX86_BUILTIN_SQRTSS:
20670       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, exp, target);
20671     case IX86_BUILTIN_RSQRTSS:
20672       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, exp, target);
20673     case IX86_BUILTIN_RCPSS:
20674       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, exp, target);
20675
20676     case IX86_BUILTIN_LOADUPS:
20677       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, exp, target, 1);
20678
20679     case IX86_BUILTIN_STOREUPS:
20680       return ix86_expand_store_builtin (CODE_FOR_sse_movups, exp);
20681
20682     case IX86_BUILTIN_LOADHPS:
20683     case IX86_BUILTIN_LOADLPS:
20684     case IX86_BUILTIN_LOADHPD:
20685     case IX86_BUILTIN_LOADLPD:
20686       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
20687                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
20688                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
20689                : CODE_FOR_sse2_loadlpd);
20690       arg0 = CALL_EXPR_ARG (exp, 0);
20691       arg1 = CALL_EXPR_ARG (exp, 1);
20692       op0 = expand_normal (arg0);
20693       op1 = expand_normal (arg1);
20694       tmode = insn_data[icode].operand[0].mode;
20695       mode0 = insn_data[icode].operand[1].mode;
20696       mode1 = insn_data[icode].operand[2].mode;
20697
20698       op0 = force_reg (mode0, op0);
20699       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
20700       if (optimize || target == 0
20701           || GET_MODE (target) != tmode
20702           || !register_operand (target, tmode))
20703         target = gen_reg_rtx (tmode);
20704       pat = GEN_FCN (icode) (target, op0, op1);
20705       if (! pat)
20706         return 0;
20707       emit_insn (pat);
20708       return target;
20709
20710     case IX86_BUILTIN_STOREHPS:
20711     case IX86_BUILTIN_STORELPS:
20712       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
20713                : CODE_FOR_sse_storelps);
20714       arg0 = CALL_EXPR_ARG (exp, 0);
20715       arg1 = CALL_EXPR_ARG (exp, 1);
20716       op0 = expand_normal (arg0);
20717       op1 = expand_normal (arg1);
20718       mode0 = insn_data[icode].operand[0].mode;
20719       mode1 = insn_data[icode].operand[1].mode;
20720
20721       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
20722       op1 = force_reg (mode1, op1);
20723
20724       pat = GEN_FCN (icode) (op0, op1);
20725       if (! pat)
20726         return 0;
20727       emit_insn (pat);
20728       return const0_rtx;
20729
20730     case IX86_BUILTIN_MOVNTPS:
20731       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, exp);
20732     case IX86_BUILTIN_MOVNTQ:
20733       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, exp);
20734
20735     case IX86_BUILTIN_LDMXCSR:
20736       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
20737       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
20738       emit_move_insn (target, op0);
20739       emit_insn (gen_sse_ldmxcsr (target));
20740       return 0;
20741
20742     case IX86_BUILTIN_STMXCSR:
20743       target = assign_386_stack_local (SImode, SLOT_VIRTUAL);
20744       emit_insn (gen_sse_stmxcsr (target));
20745       return copy_to_mode_reg (SImode, target);
20746
20747     case IX86_BUILTIN_SHUFPS:
20748     case IX86_BUILTIN_SHUFPD:
20749       icode = (fcode == IX86_BUILTIN_SHUFPS
20750                ? CODE_FOR_sse_shufps
20751                : CODE_FOR_sse2_shufpd);
20752       arg0 = CALL_EXPR_ARG (exp, 0);
20753       arg1 = CALL_EXPR_ARG (exp, 1);
20754       arg2 = CALL_EXPR_ARG (exp, 2);
20755       op0 = expand_normal (arg0);
20756       op1 = expand_normal (arg1);
20757       op2 = expand_normal (arg2);
20758       tmode = insn_data[icode].operand[0].mode;
20759       mode0 = insn_data[icode].operand[1].mode;
20760       mode1 = insn_data[icode].operand[2].mode;
20761       mode2 = insn_data[icode].operand[3].mode;
20762
20763       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
20764         op0 = copy_to_mode_reg (mode0, op0);
20765       if ((optimize && !register_operand (op1, mode1))
20766           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
20767         op1 = copy_to_mode_reg (mode1, op1);
20768       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
20769         {
20770           /* @@@ better error message */
20771           error ("mask must be an immediate");
20772           return gen_reg_rtx (tmode);
20773         }
20774       if (optimize || target == 0
20775           || GET_MODE (target) != tmode
20776           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20777         target = gen_reg_rtx (tmode);
20778       pat = GEN_FCN (icode) (target, op0, op1, op2);
20779       if (! pat)
20780         return 0;
20781       emit_insn (pat);
20782       return target;
20783
20784     case IX86_BUILTIN_PSHUFW:
20785     case IX86_BUILTIN_PSHUFD:
20786     case IX86_BUILTIN_PSHUFHW:
20787     case IX86_BUILTIN_PSHUFLW:
20788       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
20789                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
20790                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
20791                : CODE_FOR_mmx_pshufw);
20792       arg0 = CALL_EXPR_ARG (exp, 0);
20793       arg1 = CALL_EXPR_ARG (exp, 1);
20794       op0 = expand_normal (arg0);
20795       op1 = expand_normal (arg1);
20796       tmode = insn_data[icode].operand[0].mode;
20797       mode1 = insn_data[icode].operand[1].mode;
20798       mode2 = insn_data[icode].operand[2].mode;
20799
20800       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
20801         op0 = copy_to_mode_reg (mode1, op0);
20802       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
20803         {
20804           /* @@@ better error message */
20805           error ("mask must be an immediate");
20806           return const0_rtx;
20807         }
20808       if (target == 0
20809           || GET_MODE (target) != tmode
20810           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
20811         target = gen_reg_rtx (tmode);
20812       pat = GEN_FCN (icode) (target, op0, op1);
20813       if (! pat)
20814         return 0;
20815       emit_insn (pat);
20816       return target;
20817
20818     case IX86_BUILTIN_PSLLW128:
20819     case IX86_BUILTIN_PSLLWI128:
20820       icode = CODE_FOR_ashlv8hi3;
20821       goto do_pshift;
20822     case IX86_BUILTIN_PSLLD128:
20823     case IX86_BUILTIN_PSLLDI128:
20824       icode = CODE_FOR_ashlv4si3;
20825       goto do_pshift;
20826     case IX86_BUILTIN_PSLLQ128:
20827     case IX86_BUILTIN_PSLLQI128:
20828       icode = CODE_FOR_ashlv2di3;
20829       goto do_pshift;
20830     case IX86_BUILTIN_PSRAW128:
20831     case IX86_BUILTIN_PSRAWI128:
20832       icode = CODE_FOR_ashrv8hi3;
20833       goto do_pshift;
20834     case IX86_BUILTIN_PSRAD128:
20835     case IX86_BUILTIN_PSRADI128:
20836       icode = CODE_FOR_ashrv4si3;
20837       goto do_pshift;
20838     case IX86_BUILTIN_PSRLW128:
20839     case IX86_BUILTIN_PSRLWI128:
20840       icode = CODE_FOR_lshrv8hi3;
20841       goto do_pshift;
20842     case IX86_BUILTIN_PSRLD128:
20843     case IX86_BUILTIN_PSRLDI128:
20844       icode = CODE_FOR_lshrv4si3;
20845       goto do_pshift;
20846     case IX86_BUILTIN_PSRLQ128:
20847     case IX86_BUILTIN_PSRLQI128:
20848       icode = CODE_FOR_lshrv2di3;
20849
20850     do_pshift:
20851       arg0 = CALL_EXPR_ARG (exp, 0);
20852       arg1 = CALL_EXPR_ARG (exp, 1);
20853       op0 = expand_normal (arg0);
20854       op1 = expand_normal (arg1);
20855
20856       tmode = insn_data[icode].operand[0].mode;
20857       mode1 = insn_data[icode].operand[1].mode;
20858
20859       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
20860         op0 = copy_to_reg (op0);
20861
20862       if (!CONST_INT_P (op1))
20863         op1 = simplify_gen_subreg (SImode, op1, GET_MODE (op1), 0);
20864
20865       if (! (*insn_data[icode].operand[2].predicate) (op1, SImode))
20866         op1 = copy_to_reg (op1);
20867
20868       target = gen_reg_rtx (tmode);
20869       pat = GEN_FCN (icode) (target, op0, op1);
20870       if (!pat)
20871         return 0;
20872       emit_insn (pat);
20873       return target;
20874
20875     case IX86_BUILTIN_PSLLDQI128:
20876     case IX86_BUILTIN_PSRLDQI128:
20877       icode = (fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
20878                : CODE_FOR_sse2_lshrti3);
20879       arg0 = CALL_EXPR_ARG (exp, 0);
20880       arg1 = CALL_EXPR_ARG (exp, 1);
20881       op0 = expand_normal (arg0);
20882       op1 = expand_normal (arg1);
20883       tmode = insn_data[icode].operand[0].mode;
20884       mode1 = insn_data[icode].operand[1].mode;
20885       mode2 = insn_data[icode].operand[2].mode;
20886
20887       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
20888         {
20889           op0 = copy_to_reg (op0);
20890           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
20891         }
20892       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
20893         {
20894           error ("shift must be an immediate");
20895           return const0_rtx;
20896         }
20897       target = gen_reg_rtx (V2DImode);
20898       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0),
20899                              op0, op1);
20900       if (! pat)
20901         return 0;
20902       emit_insn (pat);
20903       return target;
20904
20905     case IX86_BUILTIN_FEMMS:
20906       emit_insn (gen_mmx_femms ());
20907       return NULL_RTX;
20908
20909     case IX86_BUILTIN_PAVGUSB:
20910       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, exp, target);
20911
20912     case IX86_BUILTIN_PF2ID:
20913       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, exp, target, 0);
20914
20915     case IX86_BUILTIN_PFACC:
20916       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, exp, target);
20917
20918     case IX86_BUILTIN_PFADD:
20919      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, exp, target);
20920
20921     case IX86_BUILTIN_PFCMPEQ:
20922       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, exp, target);
20923
20924     case IX86_BUILTIN_PFCMPGE:
20925       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, exp, target);
20926
20927     case IX86_BUILTIN_PFCMPGT:
20928       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, exp, target);
20929
20930     case IX86_BUILTIN_PFMAX:
20931       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, exp, target);
20932
20933     case IX86_BUILTIN_PFMIN:
20934       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, exp, target);
20935
20936     case IX86_BUILTIN_PFMUL:
20937       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, exp, target);
20938
20939     case IX86_BUILTIN_PFRCP:
20940       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, exp, target, 0);
20941
20942     case IX86_BUILTIN_PFRCPIT1:
20943       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, exp, target);
20944
20945     case IX86_BUILTIN_PFRCPIT2:
20946       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, exp, target);
20947
20948     case IX86_BUILTIN_PFRSQIT1:
20949       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, exp, target);
20950
20951     case IX86_BUILTIN_PFRSQRT:
20952       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, exp, target, 0);
20953
20954     case IX86_BUILTIN_PFSUB:
20955       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, exp, target);
20956
20957     case IX86_BUILTIN_PFSUBR:
20958       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, exp, target);
20959
20960     case IX86_BUILTIN_PI2FD:
20961       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, exp, target, 0);
20962
20963     case IX86_BUILTIN_PMULHRW:
20964       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, exp, target);
20965
20966     case IX86_BUILTIN_PF2IW:
20967       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, exp, target, 0);
20968
20969     case IX86_BUILTIN_PFNACC:
20970       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, exp, target);
20971
20972     case IX86_BUILTIN_PFPNACC:
20973       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, exp, target);
20974
20975     case IX86_BUILTIN_PI2FW:
20976       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, exp, target, 0);
20977
20978     case IX86_BUILTIN_PSWAPDSI:
20979       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, exp, target, 0);
20980
20981     case IX86_BUILTIN_PSWAPDSF:
20982       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, exp, target, 0);
20983
20984     case IX86_BUILTIN_SQRTSD:
20985       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, exp, target);
20986     case IX86_BUILTIN_LOADUPD:
20987       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, exp, target, 1);
20988     case IX86_BUILTIN_STOREUPD:
20989       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, exp);
20990
20991     case IX86_BUILTIN_MFENCE:
20992         emit_insn (gen_sse2_mfence ());
20993         return 0;
20994     case IX86_BUILTIN_LFENCE:
20995         emit_insn (gen_sse2_lfence ());
20996         return 0;
20997
20998     case IX86_BUILTIN_CLFLUSH:
20999         arg0 = CALL_EXPR_ARG (exp, 0);
21000         op0 = expand_normal (arg0);
21001         icode = CODE_FOR_sse2_clflush;
21002         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
21003             op0 = copy_to_mode_reg (Pmode, op0);
21004
21005         emit_insn (gen_sse2_clflush (op0));
21006         return 0;
21007
21008     case IX86_BUILTIN_MOVNTPD:
21009       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, exp);
21010     case IX86_BUILTIN_MOVNTDQ:
21011       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, exp);
21012     case IX86_BUILTIN_MOVNTI:
21013       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, exp);
21014
21015     case IX86_BUILTIN_LOADDQU:
21016       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, exp, target, 1);
21017     case IX86_BUILTIN_STOREDQU:
21018       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, exp);
21019
21020     case IX86_BUILTIN_MONITOR:
21021       arg0 = CALL_EXPR_ARG (exp, 0);
21022       arg1 = CALL_EXPR_ARG (exp, 1);
21023       arg2 = CALL_EXPR_ARG (exp, 2);
21024       op0 = expand_normal (arg0);
21025       op1 = expand_normal (arg1);
21026       op2 = expand_normal (arg2);
21027       if (!REG_P (op0))
21028         op0 = copy_to_mode_reg (Pmode, op0);
21029       if (!REG_P (op1))
21030         op1 = copy_to_mode_reg (SImode, op1);
21031       if (!REG_P (op2))
21032         op2 = copy_to_mode_reg (SImode, op2);
21033       if (!TARGET_64BIT)
21034         emit_insn (gen_sse3_monitor (op0, op1, op2));
21035       else
21036         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
21037       return 0;
21038
21039     case IX86_BUILTIN_MWAIT:
21040       arg0 = CALL_EXPR_ARG (exp, 0);
21041       arg1 = CALL_EXPR_ARG (exp, 1);
21042       op0 = expand_normal (arg0);
21043       op1 = expand_normal (arg1);
21044       if (!REG_P (op0))
21045         op0 = copy_to_mode_reg (SImode, op0);
21046       if (!REG_P (op1))
21047         op1 = copy_to_mode_reg (SImode, op1);
21048       emit_insn (gen_sse3_mwait (op0, op1));
21049       return 0;
21050
21051     case IX86_BUILTIN_LDDQU:
21052       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, exp,
21053                                        target, 1);
21054
21055     case IX86_BUILTIN_PALIGNR:
21056     case IX86_BUILTIN_PALIGNR128:
21057       if (fcode == IX86_BUILTIN_PALIGNR)
21058         {
21059           icode = CODE_FOR_ssse3_palignrdi;
21060           mode = DImode;
21061         }
21062       else
21063         {
21064           icode = CODE_FOR_ssse3_palignrti;
21065           mode = V2DImode;
21066         }
21067       arg0 = CALL_EXPR_ARG (exp, 0);
21068       arg1 = CALL_EXPR_ARG (exp, 1);
21069       arg2 = CALL_EXPR_ARG (exp, 2);
21070       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
21071       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
21072       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, EXPAND_NORMAL);
21073       tmode = insn_data[icode].operand[0].mode;
21074       mode1 = insn_data[icode].operand[1].mode;
21075       mode2 = insn_data[icode].operand[2].mode;
21076       mode3 = insn_data[icode].operand[3].mode;
21077
21078       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
21079         {
21080           op0 = copy_to_reg (op0);
21081           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
21082         }
21083       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
21084         {
21085           op1 = copy_to_reg (op1);
21086           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
21087         }
21088       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
21089         {
21090           error ("shift must be an immediate");
21091           return const0_rtx;
21092         }
21093       target = gen_reg_rtx (mode);
21094       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
21095                              op0, op1, op2);
21096       if (! pat)
21097         return 0;
21098       emit_insn (pat);
21099       return target;
21100
21101     case IX86_BUILTIN_MOVNTDQA:
21102       return ix86_expand_unop_builtin (CODE_FOR_sse4_1_movntdqa, exp,
21103                                        target, 1);
21104
21105     case IX86_BUILTIN_MOVNTSD:
21106       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv2df, exp);
21107
21108     case IX86_BUILTIN_MOVNTSS:
21109       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv4sf, exp);
21110
21111     case IX86_BUILTIN_INSERTQ:
21112     case IX86_BUILTIN_EXTRQ:
21113       icode = (fcode == IX86_BUILTIN_EXTRQ
21114                ? CODE_FOR_sse4a_extrq
21115                : CODE_FOR_sse4a_insertq);
21116       arg0 = CALL_EXPR_ARG (exp, 0);
21117       arg1 = CALL_EXPR_ARG (exp, 1);
21118       op0 = expand_normal (arg0);
21119       op1 = expand_normal (arg1);
21120       tmode = insn_data[icode].operand[0].mode;
21121       mode1 = insn_data[icode].operand[1].mode;
21122       mode2 = insn_data[icode].operand[2].mode;
21123       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
21124         op0 = copy_to_mode_reg (mode1, op0);
21125       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
21126         op1 = copy_to_mode_reg (mode2, op1);
21127       if (optimize || target == 0
21128           || GET_MODE (target) != tmode
21129           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21130         target = gen_reg_rtx (tmode);
21131       pat = GEN_FCN (icode) (target, op0, op1);
21132       if (! pat)
21133         return NULL_RTX;
21134       emit_insn (pat);
21135       return target;
21136
21137     case IX86_BUILTIN_EXTRQI:
21138       icode = CODE_FOR_sse4a_extrqi;
21139       arg0 = CALL_EXPR_ARG (exp, 0);
21140       arg1 = CALL_EXPR_ARG (exp, 1);
21141       arg2 = CALL_EXPR_ARG (exp, 2);
21142       op0 = expand_normal (arg0);
21143       op1 = expand_normal (arg1);
21144       op2 = expand_normal (arg2);
21145       tmode = insn_data[icode].operand[0].mode;
21146       mode1 = insn_data[icode].operand[1].mode;
21147       mode2 = insn_data[icode].operand[2].mode;
21148       mode3 = insn_data[icode].operand[3].mode;
21149       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
21150         op0 = copy_to_mode_reg (mode1, op0);
21151       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
21152         {
21153           error ("index mask must be an immediate");
21154           return gen_reg_rtx (tmode);
21155         }
21156       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
21157         {
21158           error ("length mask must be an immediate");
21159           return gen_reg_rtx (tmode);
21160         }
21161       if (optimize || target == 0
21162           || GET_MODE (target) != tmode
21163           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21164         target = gen_reg_rtx (tmode);
21165       pat = GEN_FCN (icode) (target, op0, op1, op2);
21166       if (! pat)
21167         return NULL_RTX;
21168       emit_insn (pat);
21169       return target;
21170
21171     case IX86_BUILTIN_INSERTQI:
21172       icode = CODE_FOR_sse4a_insertqi;
21173       arg0 = CALL_EXPR_ARG (exp, 0);
21174       arg1 = CALL_EXPR_ARG (exp, 1);
21175       arg2 = CALL_EXPR_ARG (exp, 2);
21176       arg3 = CALL_EXPR_ARG (exp, 3);
21177       op0 = expand_normal (arg0);
21178       op1 = expand_normal (arg1);
21179       op2 = expand_normal (arg2);
21180       op3 = expand_normal (arg3);
21181       tmode = insn_data[icode].operand[0].mode;
21182       mode1 = insn_data[icode].operand[1].mode;
21183       mode2 = insn_data[icode].operand[2].mode;
21184       mode3 = insn_data[icode].operand[3].mode;
21185       mode4 = insn_data[icode].operand[4].mode;
21186
21187       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
21188         op0 = copy_to_mode_reg (mode1, op0);
21189
21190       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
21191         op1 = copy_to_mode_reg (mode2, op1);
21192
21193       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
21194         {
21195           error ("index mask must be an immediate");
21196           return gen_reg_rtx (tmode);
21197         }
21198       if (! (*insn_data[icode].operand[4].predicate) (op3, mode4))
21199         {
21200           error ("length mask must be an immediate");
21201           return gen_reg_rtx (tmode);
21202         }
21203       if (optimize || target == 0
21204           || GET_MODE (target) != tmode
21205           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
21206         target = gen_reg_rtx (tmode);
21207       pat = GEN_FCN (icode) (target, op0, op1, op2, op3);
21208       if (! pat)
21209         return NULL_RTX;
21210       emit_insn (pat);
21211       return target;
21212
21213     case IX86_BUILTIN_VEC_INIT_V2SI:
21214     case IX86_BUILTIN_VEC_INIT_V4HI:
21215     case IX86_BUILTIN_VEC_INIT_V8QI:
21216       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
21217
21218     case IX86_BUILTIN_VEC_EXT_V2DF:
21219     case IX86_BUILTIN_VEC_EXT_V2DI:
21220     case IX86_BUILTIN_VEC_EXT_V4SF:
21221     case IX86_BUILTIN_VEC_EXT_V4SI:
21222     case IX86_BUILTIN_VEC_EXT_V8HI:
21223     case IX86_BUILTIN_VEC_EXT_V2SI:
21224     case IX86_BUILTIN_VEC_EXT_V4HI:
21225     case IX86_BUILTIN_VEC_EXT_V16QI:
21226       return ix86_expand_vec_ext_builtin (exp, target);
21227
21228     case IX86_BUILTIN_VEC_SET_V2DI:
21229     case IX86_BUILTIN_VEC_SET_V4SF:
21230     case IX86_BUILTIN_VEC_SET_V4SI:
21231     case IX86_BUILTIN_VEC_SET_V8HI:
21232     case IX86_BUILTIN_VEC_SET_V4HI:
21233     case IX86_BUILTIN_VEC_SET_V16QI:
21234       return ix86_expand_vec_set_builtin (exp);
21235
21236     case IX86_BUILTIN_INFQ:
21237       {
21238         REAL_VALUE_TYPE inf;
21239         rtx tmp;
21240
21241         real_inf (&inf);
21242         tmp = CONST_DOUBLE_FROM_REAL_VALUE (inf, mode);
21243
21244         tmp = validize_mem (force_const_mem (mode, tmp));
21245
21246         if (target == 0)
21247           target = gen_reg_rtx (mode);
21248
21249         emit_move_insn (target, tmp);
21250         return target;
21251       }
21252
21253     case IX86_BUILTIN_FABSQ:
21254       return ix86_expand_unop_builtin (CODE_FOR_abstf2, exp, target, 0);
21255
21256     case IX86_BUILTIN_COPYSIGNQ:
21257       return ix86_expand_binop_builtin (CODE_FOR_copysigntf3, exp, target);
21258
21259     default:
21260       break;
21261     }
21262
21263   for (i = 0, d = bdesc_sse_3arg;
21264        i < ARRAY_SIZE (bdesc_sse_3arg);
21265        i++, d++)
21266     if (d->code == fcode)
21267       return ix86_expand_sse_4_operands_builtin (d->icode, exp,
21268                                                  target);
21269
21270   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
21271     if (d->code == fcode)
21272       {
21273         /* Compares are treated specially.  */
21274         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
21275             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
21276             || d->icode == CODE_FOR_sse2_maskcmpv2df3
21277             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
21278           return ix86_expand_sse_compare (d, exp, target);
21279
21280         return ix86_expand_binop_builtin (d->icode, exp, target);
21281       }
21282
21283   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
21284     if (d->code == fcode)
21285       return ix86_expand_unop_builtin (d->icode, exp, target, 0);
21286
21287   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
21288     if (d->code == fcode)
21289       return ix86_expand_sse_comi (d, exp, target);
21290
21291   for (i = 0, d = bdesc_ptest; i < ARRAY_SIZE (bdesc_ptest); i++, d++)
21292     if (d->code == fcode)
21293       return ix86_expand_sse_ptest (d, exp, target);
21294
21295   for (i = 0, d = bdesc_crc32; i < ARRAY_SIZE (bdesc_crc32); i++, d++)
21296     if (d->code == fcode)
21297       return ix86_expand_crc32 (d->icode, exp, target);
21298
21299   for (i = 0, d = bdesc_pcmpestr;
21300        i < ARRAY_SIZE (bdesc_pcmpestr);
21301        i++, d++)
21302     if (d->code == fcode)
21303       return ix86_expand_sse_pcmpestr (d, exp, target);
21304
21305   for (i = 0, d = bdesc_pcmpistr;
21306        i < ARRAY_SIZE (bdesc_pcmpistr);
21307        i++, d++)
21308     if (d->code == fcode)
21309       return ix86_expand_sse_pcmpistr (d, exp, target);
21310
21311   for (i = 0, d = bdesc_multi_arg; i < ARRAY_SIZE (bdesc_multi_arg); i++, d++)
21312     if (d->code == fcode)
21313       return ix86_expand_multi_arg_builtin (d->icode, exp, target,
21314                                             (enum multi_arg_type)d->flag,
21315                                             d->comparison);
21316
21317   gcc_unreachable ();
21318 }
21319
21320 /* Returns a function decl for a vectorized version of the builtin function
21321    with builtin function code FN and the result vector type TYPE, or NULL_TREE
21322    if it is not available.  */
21323
21324 static tree
21325 ix86_builtin_vectorized_function (unsigned int fn, tree type_out,
21326                                   tree type_in)
21327 {
21328   enum machine_mode in_mode, out_mode;
21329   int in_n, out_n;
21330
21331   if (TREE_CODE (type_out) != VECTOR_TYPE
21332       || TREE_CODE (type_in) != VECTOR_TYPE)
21333     return NULL_TREE;
21334
21335   out_mode = TYPE_MODE (TREE_TYPE (type_out));
21336   out_n = TYPE_VECTOR_SUBPARTS (type_out);
21337   in_mode = TYPE_MODE (TREE_TYPE (type_in));
21338   in_n = TYPE_VECTOR_SUBPARTS (type_in);
21339
21340   switch (fn)
21341     {
21342     case BUILT_IN_SQRT:
21343       if (out_mode == DFmode && out_n == 2
21344           && in_mode == DFmode && in_n == 2)
21345         return ix86_builtins[IX86_BUILTIN_SQRTPD];
21346       break;
21347
21348     case BUILT_IN_SQRTF:
21349       if (out_mode == SFmode && out_n == 4
21350           && in_mode == SFmode && in_n == 4)
21351         return ix86_builtins[IX86_BUILTIN_SQRTPS_NR];
21352       break;
21353
21354     case BUILT_IN_LRINT:
21355       if (out_mode == SImode && out_n == 4
21356           && in_mode == DFmode && in_n == 2)
21357         return ix86_builtins[IX86_BUILTIN_VEC_PACK_SFIX];
21358       break;
21359
21360     case BUILT_IN_LRINTF:
21361       if (out_mode == SImode && out_n == 4
21362           && in_mode == SFmode && in_n == 4)
21363         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
21364       break;
21365
21366     default:
21367       ;
21368     }
21369
21370   /* Dispatch to a handler for a vectorization library.  */
21371   if (ix86_veclib_handler)
21372     return (*ix86_veclib_handler)(fn, type_out, type_in);
21373
21374   return NULL_TREE;
21375 }
21376
21377 /* Handler for an ACML-style interface to a library with vectorized
21378    intrinsics.  */
21379
21380 static tree
21381 ix86_veclibabi_acml (enum built_in_function fn, tree type_out, tree type_in)
21382 {
21383   char name[20] = "__vr.._";
21384   tree fntype, new_fndecl, args;
21385   unsigned arity;
21386   const char *bname;
21387   enum machine_mode el_mode, in_mode;
21388   int n, in_n;
21389
21390   /* The ACML is 64bits only and suitable for unsafe math only as
21391      it does not correctly support parts of IEEE with the required
21392      precision such as denormals.  */
21393   if (!TARGET_64BIT
21394       || !flag_unsafe_math_optimizations)
21395     return NULL_TREE;
21396
21397   el_mode = TYPE_MODE (TREE_TYPE (type_out));
21398   n = TYPE_VECTOR_SUBPARTS (type_out);
21399   in_mode = TYPE_MODE (TREE_TYPE (type_in));
21400   in_n = TYPE_VECTOR_SUBPARTS (type_in);
21401   if (el_mode != in_mode
21402       || n != in_n)
21403     return NULL_TREE;
21404
21405   switch (fn)
21406     {
21407     case BUILT_IN_SIN:
21408     case BUILT_IN_COS:
21409     case BUILT_IN_EXP:
21410     case BUILT_IN_LOG:
21411     case BUILT_IN_LOG2:
21412     case BUILT_IN_LOG10:
21413       name[4] = 'd';
21414       name[5] = '2';
21415       if (el_mode != DFmode
21416           || n != 2)
21417         return NULL_TREE;
21418       break;
21419
21420     case BUILT_IN_SINF:
21421     case BUILT_IN_COSF:
21422     case BUILT_IN_EXPF:
21423     case BUILT_IN_POWF:
21424     case BUILT_IN_LOGF:
21425     case BUILT_IN_LOG2F:
21426     case BUILT_IN_LOG10F:
21427       name[4] = 's';
21428       name[5] = '4';
21429       if (el_mode != SFmode
21430           || n != 4)
21431         return NULL_TREE;
21432       break;
21433
21434     default:
21435       return NULL_TREE;
21436     }
21437
21438   bname = IDENTIFIER_POINTER (DECL_NAME (implicit_built_in_decls[fn]));
21439   sprintf (name + 7, "%s", bname+10);
21440
21441   arity = 0;
21442   for (args = DECL_ARGUMENTS (implicit_built_in_decls[fn]); args;
21443        args = TREE_CHAIN (args))
21444     arity++;
21445
21446   if (arity == 1)
21447     fntype = build_function_type_list (type_out, type_in, NULL);
21448   else
21449     fntype = build_function_type_list (type_out, type_in, type_in, NULL);
21450
21451   /* Build a function declaration for the vectorized function.  */
21452   new_fndecl = build_decl (FUNCTION_DECL, get_identifier (name), fntype);
21453   TREE_PUBLIC (new_fndecl) = 1;
21454   DECL_EXTERNAL (new_fndecl) = 1;
21455   DECL_IS_NOVOPS (new_fndecl) = 1;
21456   TREE_READONLY (new_fndecl) = 1;
21457
21458   return new_fndecl;
21459 }
21460
21461
21462 /* Returns a decl of a function that implements conversion of the
21463    input vector of type TYPE, or NULL_TREE if it is not available.  */
21464
21465 static tree
21466 ix86_vectorize_builtin_conversion (unsigned int code, tree type)
21467 {
21468   if (TREE_CODE (type) != VECTOR_TYPE)
21469     return NULL_TREE;
21470
21471   switch (code)
21472     {
21473     case FLOAT_EXPR:
21474       switch (TYPE_MODE (type))
21475         {
21476         case V4SImode:
21477           return ix86_builtins[IX86_BUILTIN_CVTDQ2PS];
21478         default:
21479           return NULL_TREE;
21480         }
21481
21482     case FIX_TRUNC_EXPR:
21483       switch (TYPE_MODE (type))
21484         {
21485         case V4SFmode:
21486           return ix86_builtins[IX86_BUILTIN_CVTTPS2DQ];
21487         default:
21488           return NULL_TREE;
21489         }
21490     default:
21491       return NULL_TREE;
21492
21493     }
21494 }
21495
21496 /* Returns a code for a target-specific builtin that implements
21497    reciprocal of the function, or NULL_TREE if not available.  */
21498
21499 static tree
21500 ix86_builtin_reciprocal (unsigned int fn, bool md_fn,
21501                          bool sqrt ATTRIBUTE_UNUSED)
21502 {
21503   if (! (TARGET_SSE_MATH && TARGET_RECIP && !optimize_size
21504          && flag_finite_math_only && !flag_trapping_math
21505          && flag_unsafe_math_optimizations))
21506     return NULL_TREE;
21507
21508   if (md_fn)
21509     /* Machine dependent builtins.  */
21510     switch (fn)
21511       {
21512         /* Vectorized version of sqrt to rsqrt conversion.  */
21513       case IX86_BUILTIN_SQRTPS_NR:
21514         return ix86_builtins[IX86_BUILTIN_RSQRTPS_NR];
21515
21516       default:
21517         return NULL_TREE;
21518       }
21519   else
21520     /* Normal builtins.  */
21521     switch (fn)
21522       {
21523         /* Sqrt to rsqrt conversion.  */
21524       case BUILT_IN_SQRTF:
21525         return ix86_builtins[IX86_BUILTIN_RSQRTF];
21526
21527       default:
21528         return NULL_TREE;
21529       }
21530 }
21531
21532 /* Store OPERAND to the memory after reload is completed.  This means
21533    that we can't easily use assign_stack_local.  */
21534 rtx
21535 ix86_force_to_memory (enum machine_mode mode, rtx operand)
21536 {
21537   rtx result;
21538
21539   gcc_assert (reload_completed);
21540   if (TARGET_RED_ZONE)
21541     {
21542       result = gen_rtx_MEM (mode,
21543                             gen_rtx_PLUS (Pmode,
21544                                           stack_pointer_rtx,
21545                                           GEN_INT (-RED_ZONE_SIZE)));
21546       emit_move_insn (result, operand);
21547     }
21548   else if (!TARGET_RED_ZONE && TARGET_64BIT)
21549     {
21550       switch (mode)
21551         {
21552         case HImode:
21553         case SImode:
21554           operand = gen_lowpart (DImode, operand);
21555           /* FALLTHRU */
21556         case DImode:
21557           emit_insn (
21558                       gen_rtx_SET (VOIDmode,
21559                                    gen_rtx_MEM (DImode,
21560                                                 gen_rtx_PRE_DEC (DImode,
21561                                                         stack_pointer_rtx)),
21562                                    operand));
21563           break;
21564         default:
21565           gcc_unreachable ();
21566         }
21567       result = gen_rtx_MEM (mode, stack_pointer_rtx);
21568     }
21569   else
21570     {
21571       switch (mode)
21572         {
21573         case DImode:
21574           {
21575             rtx operands[2];
21576             split_di (&operand, 1, operands, operands + 1);
21577             emit_insn (
21578                         gen_rtx_SET (VOIDmode,
21579                                      gen_rtx_MEM (SImode,
21580                                                   gen_rtx_PRE_DEC (Pmode,
21581                                                         stack_pointer_rtx)),
21582                                      operands[1]));
21583             emit_insn (
21584                         gen_rtx_SET (VOIDmode,
21585                                      gen_rtx_MEM (SImode,
21586                                                   gen_rtx_PRE_DEC (Pmode,
21587                                                         stack_pointer_rtx)),
21588                                      operands[0]));
21589           }
21590           break;
21591         case HImode:
21592           /* Store HImodes as SImodes.  */
21593           operand = gen_lowpart (SImode, operand);
21594           /* FALLTHRU */
21595         case SImode:
21596           emit_insn (
21597                       gen_rtx_SET (VOIDmode,
21598                                    gen_rtx_MEM (GET_MODE (operand),
21599                                                 gen_rtx_PRE_DEC (SImode,
21600                                                         stack_pointer_rtx)),
21601                                    operand));
21602           break;
21603         default:
21604           gcc_unreachable ();
21605         }
21606       result = gen_rtx_MEM (mode, stack_pointer_rtx);
21607     }
21608   return result;
21609 }
21610
21611 /* Free operand from the memory.  */
21612 void
21613 ix86_free_from_memory (enum machine_mode mode)
21614 {
21615   if (!TARGET_RED_ZONE)
21616     {
21617       int size;
21618
21619       if (mode == DImode || TARGET_64BIT)
21620         size = 8;
21621       else
21622         size = 4;
21623       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
21624          to pop or add instruction if registers are available.  */
21625       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
21626                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
21627                                             GEN_INT (size))));
21628     }
21629 }
21630
21631 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
21632    QImode must go into class Q_REGS.
21633    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
21634    movdf to do mem-to-mem moves through integer regs.  */
21635 enum reg_class
21636 ix86_preferred_reload_class (rtx x, enum reg_class regclass)
21637 {
21638   enum machine_mode mode = GET_MODE (x);
21639
21640   /* We're only allowed to return a subclass of CLASS.  Many of the
21641      following checks fail for NO_REGS, so eliminate that early.  */
21642   if (regclass == NO_REGS)
21643     return NO_REGS;
21644
21645   /* All classes can load zeros.  */
21646   if (x == CONST0_RTX (mode))
21647     return regclass;
21648
21649   /* Force constants into memory if we are loading a (nonzero) constant into
21650      an MMX or SSE register.  This is because there are no MMX/SSE instructions
21651      to load from a constant.  */
21652   if (CONSTANT_P (x)
21653       && (MAYBE_MMX_CLASS_P (regclass) || MAYBE_SSE_CLASS_P (regclass)))
21654     return NO_REGS;
21655
21656   /* Prefer SSE regs only, if we can use them for math.  */
21657   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
21658     return SSE_CLASS_P (regclass) ? regclass : NO_REGS;
21659
21660   /* Floating-point constants need more complex checks.  */
21661   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
21662     {
21663       /* General regs can load everything.  */
21664       if (reg_class_subset_p (regclass, GENERAL_REGS))
21665         return regclass;
21666
21667       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
21668          zero above.  We only want to wind up preferring 80387 registers if
21669          we plan on doing computation with them.  */
21670       if (TARGET_80387
21671           && standard_80387_constant_p (x))
21672         {
21673           /* Limit class to non-sse.  */
21674           if (regclass == FLOAT_SSE_REGS)
21675             return FLOAT_REGS;
21676           if (regclass == FP_TOP_SSE_REGS)
21677             return FP_TOP_REG;
21678           if (regclass == FP_SECOND_SSE_REGS)
21679             return FP_SECOND_REG;
21680           if (regclass == FLOAT_INT_REGS || regclass == FLOAT_REGS)
21681             return regclass;
21682         }
21683
21684       return NO_REGS;
21685     }
21686
21687   /* Generally when we see PLUS here, it's the function invariant
21688      (plus soft-fp const_int).  Which can only be computed into general
21689      regs.  */
21690   if (GET_CODE (x) == PLUS)
21691     return reg_class_subset_p (regclass, GENERAL_REGS) ? regclass : NO_REGS;
21692
21693   /* QImode constants are easy to load, but non-constant QImode data
21694      must go into Q_REGS.  */
21695   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
21696     {
21697       if (reg_class_subset_p (regclass, Q_REGS))
21698         return regclass;
21699       if (reg_class_subset_p (Q_REGS, regclass))
21700         return Q_REGS;
21701       return NO_REGS;
21702     }
21703
21704   return regclass;
21705 }
21706
21707 /* Discourage putting floating-point values in SSE registers unless
21708    SSE math is being used, and likewise for the 387 registers.  */
21709 enum reg_class
21710 ix86_preferred_output_reload_class (rtx x, enum reg_class regclass)
21711 {
21712   enum machine_mode mode = GET_MODE (x);
21713
21714   /* Restrict the output reload class to the register bank that we are doing
21715      math on.  If we would like not to return a subset of CLASS, reject this
21716      alternative: if reload cannot do this, it will still use its choice.  */
21717   mode = GET_MODE (x);
21718   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
21719     return MAYBE_SSE_CLASS_P (regclass) ? SSE_REGS : NO_REGS;
21720
21721   if (X87_FLOAT_MODE_P (mode))
21722     {
21723       if (regclass == FP_TOP_SSE_REGS)
21724         return FP_TOP_REG;
21725       else if (regclass == FP_SECOND_SSE_REGS)
21726         return FP_SECOND_REG;
21727       else
21728         return FLOAT_CLASS_P (regclass) ? regclass : NO_REGS;
21729     }
21730
21731   return regclass;
21732 }
21733
21734 /* If we are copying between general and FP registers, we need a memory
21735    location. The same is true for SSE and MMX registers.
21736
21737    To optimize register_move_cost performance, allow inline variant.
21738
21739    The macro can't work reliably when one of the CLASSES is class containing
21740    registers from multiple units (SSE, MMX, integer).  We avoid this by never
21741    combining those units in single alternative in the machine description.
21742    Ensure that this constraint holds to avoid unexpected surprises.
21743
21744    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
21745    enforce these sanity checks.  */
21746
21747 static inline int
21748 inline_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
21749                               enum machine_mode mode, int strict)
21750 {
21751   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
21752       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
21753       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
21754       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
21755       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
21756       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
21757     {
21758       gcc_assert (!strict);
21759       return true;
21760     }
21761
21762   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
21763     return true;
21764
21765   /* ??? This is a lie.  We do have moves between mmx/general, and for
21766      mmx/sse2.  But by saying we need secondary memory we discourage the
21767      register allocator from using the mmx registers unless needed.  */
21768   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
21769     return true;
21770
21771   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
21772     {
21773       /* SSE1 doesn't have any direct moves from other classes.  */
21774       if (!TARGET_SSE2)
21775         return true;
21776
21777       /* If the target says that inter-unit moves are more expensive
21778          than moving through memory, then don't generate them.  */
21779       if (!TARGET_INTER_UNIT_MOVES)
21780         return true;
21781
21782       /* Between SSE and general, we have moves no larger than word size.  */
21783       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
21784         return true;
21785     }
21786
21787   return false;
21788 }
21789
21790 int
21791 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
21792                               enum machine_mode mode, int strict)
21793 {
21794   return inline_secondary_memory_needed (class1, class2, mode, strict);
21795 }
21796
21797 /* Return true if the registers in CLASS cannot represent the change from
21798    modes FROM to TO.  */
21799
21800 bool
21801 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
21802                                enum reg_class regclass)
21803 {
21804   if (from == to)
21805     return false;
21806
21807   /* x87 registers can't do subreg at all, as all values are reformatted
21808      to extended precision.  */
21809   if (MAYBE_FLOAT_CLASS_P (regclass))
21810     return true;
21811
21812   if (MAYBE_SSE_CLASS_P (regclass) || MAYBE_MMX_CLASS_P (regclass))
21813     {
21814       /* Vector registers do not support QI or HImode loads.  If we don't
21815          disallow a change to these modes, reload will assume it's ok to
21816          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
21817          the vec_dupv4hi pattern.  */
21818       if (GET_MODE_SIZE (from) < 4)
21819         return true;
21820
21821       /* Vector registers do not support subreg with nonzero offsets, which
21822          are otherwise valid for integer registers.  Since we can't see
21823          whether we have a nonzero offset from here, prohibit all
21824          nonparadoxical subregs changing size.  */
21825       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
21826         return true;
21827     }
21828
21829   return false;
21830 }
21831
21832 /* Return the cost of moving data of mode M between a
21833    register and memory.  A value of 2 is the default; this cost is
21834    relative to those in `REGISTER_MOVE_COST'.
21835
21836    This function is used extensively by register_move_cost that is used to
21837    build tables at startup.  Make it inline in this case.
21838    When IN is 2, return maximum of in and out move cost.
21839
21840    If moving between registers and memory is more expensive than
21841    between two registers, you should define this macro to express the
21842    relative cost.
21843
21844    Model also increased moving costs of QImode registers in non
21845    Q_REGS classes.
21846  */
21847 static inline int
21848 inline_memory_move_cost (enum machine_mode mode, enum reg_class regclass,
21849                          int in)
21850 {
21851   int cost;
21852   if (FLOAT_CLASS_P (regclass))
21853     {
21854       int index;
21855       switch (mode)
21856         {
21857           case SFmode:
21858             index = 0;
21859             break;
21860           case DFmode:
21861             index = 1;
21862             break;
21863           case XFmode:
21864             index = 2;
21865             break;
21866           default:
21867             return 100;
21868         }
21869       if (in == 2)
21870         return MAX (ix86_cost->fp_load [index], ix86_cost->fp_store [index]);
21871       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
21872     }
21873   if (SSE_CLASS_P (regclass))
21874     {
21875       int index;
21876       switch (GET_MODE_SIZE (mode))
21877         {
21878           case 4:
21879             index = 0;
21880             break;
21881           case 8:
21882             index = 1;
21883             break;
21884           case 16:
21885             index = 2;
21886             break;
21887           default:
21888             return 100;
21889         }
21890       if (in == 2)
21891         return MAX (ix86_cost->sse_load [index], ix86_cost->sse_store [index]);
21892       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
21893     }
21894   if (MMX_CLASS_P (regclass))
21895     {
21896       int index;
21897       switch (GET_MODE_SIZE (mode))
21898         {
21899           case 4:
21900             index = 0;
21901             break;
21902           case 8:
21903             index = 1;
21904             break;
21905           default:
21906             return 100;
21907         }
21908       if (in)
21909         return MAX (ix86_cost->mmx_load [index], ix86_cost->mmx_store [index]);
21910       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
21911     }
21912   switch (GET_MODE_SIZE (mode))
21913     {
21914       case 1:
21915         if (Q_CLASS_P (regclass) || TARGET_64BIT)
21916           {
21917             if (!in)
21918               return ix86_cost->int_store[0];
21919             if (TARGET_PARTIAL_REG_DEPENDENCY && !optimize_size)
21920               cost = ix86_cost->movzbl_load;
21921             else
21922               cost = ix86_cost->int_load[0];
21923             if (in == 2)
21924               return MAX (cost, ix86_cost->int_store[0]);
21925             return cost;
21926           }
21927         else
21928           {
21929            if (in == 2)
21930              return MAX (ix86_cost->movzbl_load, ix86_cost->int_store[0] + 4);
21931            if (in)
21932              return ix86_cost->movzbl_load;
21933            else
21934              return ix86_cost->int_store[0] + 4;
21935           }
21936         break;
21937       case 2:
21938         if (in == 2)
21939           return MAX (ix86_cost->int_load[1], ix86_cost->int_store[1]);
21940         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
21941       default:
21942         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
21943         if (mode == TFmode)
21944           mode = XFmode;
21945         if (in == 2)
21946           cost = MAX (ix86_cost->int_load[2] , ix86_cost->int_store[2]);
21947         else if (in)
21948           cost = ix86_cost->int_load[2];
21949         else
21950           cost = ix86_cost->int_store[2];
21951         return (cost * (((int) GET_MODE_SIZE (mode)
21952                         + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
21953     }
21954 }
21955
21956 int
21957 ix86_memory_move_cost (enum machine_mode mode, enum reg_class regclass, int in)
21958 {
21959   return inline_memory_move_cost (mode, regclass, in);
21960 }
21961
21962
21963 /* Return the cost of moving data from a register in class CLASS1 to
21964    one in class CLASS2.
21965
21966    It is not required that the cost always equal 2 when FROM is the same as TO;
21967    on some machines it is expensive to move between registers if they are not
21968    general registers.  */
21969
21970 int
21971 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
21972                          enum reg_class class2)
21973 {
21974   /* In case we require secondary memory, compute cost of the store followed
21975      by load.  In order to avoid bad register allocation choices, we need
21976      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
21977
21978   if (inline_secondary_memory_needed (class1, class2, mode, 0))
21979     {
21980       int cost = 1;
21981
21982       cost += inline_memory_move_cost (mode, class1, 2);
21983       cost += inline_memory_move_cost (mode, class2, 2);
21984
21985       /* In case of copying from general_purpose_register we may emit multiple
21986          stores followed by single load causing memory size mismatch stall.
21987          Count this as arbitrarily high cost of 20.  */
21988       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
21989         cost += 20;
21990
21991       /* In the case of FP/MMX moves, the registers actually overlap, and we
21992          have to switch modes in order to treat them differently.  */
21993       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
21994           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
21995         cost += 20;
21996
21997       return cost;
21998     }
21999
22000   /* Moves between SSE/MMX and integer unit are expensive.  */
22001   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
22002       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
22003
22004     /* ??? By keeping returned value relatively high, we limit the number
22005        of moves between integer and MMX/SSE registers for all targets.
22006        Additionally, high value prevents problem with x86_modes_tieable_p(),
22007        where integer modes in MMX/SSE registers are not tieable
22008        because of missing QImode and HImode moves to, from or between
22009        MMX/SSE registers.  */
22010     return MAX (8, ix86_cost->mmxsse_to_integer);
22011
22012   if (MAYBE_FLOAT_CLASS_P (class1))
22013     return ix86_cost->fp_move;
22014   if (MAYBE_SSE_CLASS_P (class1))
22015     return ix86_cost->sse_move;
22016   if (MAYBE_MMX_CLASS_P (class1))
22017     return ix86_cost->mmx_move;
22018   return 2;
22019 }
22020
22021 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
22022
22023 bool
22024 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
22025 {
22026   /* Flags and only flags can only hold CCmode values.  */
22027   if (CC_REGNO_P (regno))
22028     return GET_MODE_CLASS (mode) == MODE_CC;
22029   if (GET_MODE_CLASS (mode) == MODE_CC
22030       || GET_MODE_CLASS (mode) == MODE_RANDOM
22031       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
22032     return 0;
22033   if (FP_REGNO_P (regno))
22034     return VALID_FP_MODE_P (mode);
22035   if (SSE_REGNO_P (regno))
22036     {
22037       /* We implement the move patterns for all vector modes into and
22038          out of SSE registers, even when no operation instructions
22039          are available.  */
22040       return (VALID_SSE_REG_MODE (mode)
22041               || VALID_SSE2_REG_MODE (mode)
22042               || VALID_MMX_REG_MODE (mode)
22043               || VALID_MMX_REG_MODE_3DNOW (mode));
22044     }
22045   if (MMX_REGNO_P (regno))
22046     {
22047       /* We implement the move patterns for 3DNOW modes even in MMX mode,
22048          so if the register is available at all, then we can move data of
22049          the given mode into or out of it.  */
22050       return (VALID_MMX_REG_MODE (mode)
22051               || VALID_MMX_REG_MODE_3DNOW (mode));
22052     }
22053
22054   if (mode == QImode)
22055     {
22056       /* Take care for QImode values - they can be in non-QI regs,
22057          but then they do cause partial register stalls.  */
22058       if (regno < 4 || TARGET_64BIT)
22059         return 1;
22060       if (!TARGET_PARTIAL_REG_STALL)
22061         return 1;
22062       return reload_in_progress || reload_completed;
22063     }
22064   /* We handle both integer and floats in the general purpose registers.  */
22065   else if (VALID_INT_MODE_P (mode))
22066     return 1;
22067   else if (VALID_FP_MODE_P (mode))
22068     return 1;
22069   else if (VALID_DFP_MODE_P (mode))
22070     return 1;
22071   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
22072      on to use that value in smaller contexts, this can easily force a
22073      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
22074      supporting DImode, allow it.  */
22075   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
22076     return 1;
22077
22078   return 0;
22079 }
22080
22081 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
22082    tieable integer mode.  */
22083
22084 static bool
22085 ix86_tieable_integer_mode_p (enum machine_mode mode)
22086 {
22087   switch (mode)
22088     {
22089     case HImode:
22090     case SImode:
22091       return true;
22092
22093     case QImode:
22094       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
22095
22096     case DImode:
22097       return TARGET_64BIT;
22098
22099     default:
22100       return false;
22101     }
22102 }
22103
22104 /* Return true if MODE1 is accessible in a register that can hold MODE2
22105    without copying.  That is, all register classes that can hold MODE2
22106    can also hold MODE1.  */
22107
22108 bool
22109 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
22110 {
22111   if (mode1 == mode2)
22112     return true;
22113
22114   if (ix86_tieable_integer_mode_p (mode1)
22115       && ix86_tieable_integer_mode_p (mode2))
22116     return true;
22117
22118   /* MODE2 being XFmode implies fp stack or general regs, which means we
22119      can tie any smaller floating point modes to it.  Note that we do not
22120      tie this with TFmode.  */
22121   if (mode2 == XFmode)
22122     return mode1 == SFmode || mode1 == DFmode;
22123
22124   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
22125      that we can tie it with SFmode.  */
22126   if (mode2 == DFmode)
22127     return mode1 == SFmode;
22128
22129   /* If MODE2 is only appropriate for an SSE register, then tie with
22130      any other mode acceptable to SSE registers.  */
22131   if (GET_MODE_SIZE (mode2) == 16
22132       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
22133     return (GET_MODE_SIZE (mode1) == 16
22134             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
22135
22136   /* If MODE2 is appropriate for an MMX register, then tie
22137      with any other mode acceptable to MMX registers.  */
22138   if (GET_MODE_SIZE (mode2) == 8
22139       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
22140     return (GET_MODE_SIZE (mode1) == 8
22141             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
22142
22143   return false;
22144 }
22145
22146 /* Compute a (partial) cost for rtx X.  Return true if the complete
22147    cost has been computed, and false if subexpressions should be
22148    scanned.  In either case, *TOTAL contains the cost result.  */
22149
22150 static bool
22151 ix86_rtx_costs (rtx x, int code, int outer_code_i, int *total)
22152 {
22153   enum rtx_code outer_code = (enum rtx_code) outer_code_i;
22154   enum machine_mode mode = GET_MODE (x);
22155
22156   switch (code)
22157     {
22158     case CONST_INT:
22159     case CONST:
22160     case LABEL_REF:
22161     case SYMBOL_REF:
22162       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
22163         *total = 3;
22164       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
22165         *total = 2;
22166       else if (flag_pic && SYMBOLIC_CONST (x)
22167                && (!TARGET_64BIT
22168                    || (!GET_CODE (x) != LABEL_REF
22169                        && (GET_CODE (x) != SYMBOL_REF
22170                            || !SYMBOL_REF_LOCAL_P (x)))))
22171         *total = 1;
22172       else
22173         *total = 0;
22174       return true;
22175
22176     case CONST_DOUBLE:
22177       if (mode == VOIDmode)
22178         *total = 0;
22179       else
22180         switch (standard_80387_constant_p (x))
22181           {
22182           case 1: /* 0.0 */
22183             *total = 1;
22184             break;
22185           default: /* Other constants */
22186             *total = 2;
22187             break;
22188           case 0:
22189           case -1:
22190             /* Start with (MEM (SYMBOL_REF)), since that's where
22191                it'll probably end up.  Add a penalty for size.  */
22192             *total = (COSTS_N_INSNS (1)
22193                       + (flag_pic != 0 && !TARGET_64BIT)
22194                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
22195             break;
22196           }
22197       return true;
22198
22199     case ZERO_EXTEND:
22200       /* The zero extensions is often completely free on x86_64, so make
22201          it as cheap as possible.  */
22202       if (TARGET_64BIT && mode == DImode
22203           && GET_MODE (XEXP (x, 0)) == SImode)
22204         *total = 1;
22205       else if (TARGET_ZERO_EXTEND_WITH_AND)
22206         *total = ix86_cost->add;
22207       else
22208         *total = ix86_cost->movzx;
22209       return false;
22210
22211     case SIGN_EXTEND:
22212       *total = ix86_cost->movsx;
22213       return false;
22214
22215     case ASHIFT:
22216       if (CONST_INT_P (XEXP (x, 1))
22217           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
22218         {
22219           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
22220           if (value == 1)
22221             {
22222               *total = ix86_cost->add;
22223               return false;
22224             }
22225           if ((value == 2 || value == 3)
22226               && ix86_cost->lea <= ix86_cost->shift_const)
22227             {
22228               *total = ix86_cost->lea;
22229               return false;
22230             }
22231         }
22232       /* FALLTHRU */
22233
22234     case ROTATE:
22235     case ASHIFTRT:
22236     case LSHIFTRT:
22237     case ROTATERT:
22238       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
22239         {
22240           if (CONST_INT_P (XEXP (x, 1)))
22241             {
22242               if (INTVAL (XEXP (x, 1)) > 32)
22243                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
22244               else
22245                 *total = ix86_cost->shift_const * 2;
22246             }
22247           else
22248             {
22249               if (GET_CODE (XEXP (x, 1)) == AND)
22250                 *total = ix86_cost->shift_var * 2;
22251               else
22252                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
22253             }
22254         }
22255       else
22256         {
22257           if (CONST_INT_P (XEXP (x, 1)))
22258             *total = ix86_cost->shift_const;
22259           else
22260             *total = ix86_cost->shift_var;
22261         }
22262       return false;
22263
22264     case MULT:
22265       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22266         {
22267           /* ??? SSE scalar cost should be used here.  */
22268           *total = ix86_cost->fmul;
22269           return false;
22270         }
22271       else if (X87_FLOAT_MODE_P (mode))
22272         {
22273           *total = ix86_cost->fmul;
22274           return false;
22275         }
22276       else if (FLOAT_MODE_P (mode))
22277         {
22278           /* ??? SSE vector cost should be used here.  */
22279           *total = ix86_cost->fmul;
22280           return false;
22281         }
22282       else
22283         {
22284           rtx op0 = XEXP (x, 0);
22285           rtx op1 = XEXP (x, 1);
22286           int nbits;
22287           if (CONST_INT_P (XEXP (x, 1)))
22288             {
22289               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
22290               for (nbits = 0; value != 0; value &= value - 1)
22291                 nbits++;
22292             }
22293           else
22294             /* This is arbitrary.  */
22295             nbits = 7;
22296
22297           /* Compute costs correctly for widening multiplication.  */
22298           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op0) == ZERO_EXTEND)
22299               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
22300                  == GET_MODE_SIZE (mode))
22301             {
22302               int is_mulwiden = 0;
22303               enum machine_mode inner_mode = GET_MODE (op0);
22304
22305               if (GET_CODE (op0) == GET_CODE (op1))
22306                 is_mulwiden = 1, op1 = XEXP (op1, 0);
22307               else if (CONST_INT_P (op1))
22308                 {
22309                   if (GET_CODE (op0) == SIGN_EXTEND)
22310                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
22311                                   == INTVAL (op1);
22312                   else
22313                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
22314                 }
22315
22316               if (is_mulwiden)
22317                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
22318             }
22319
22320           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
22321                     + nbits * ix86_cost->mult_bit
22322                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
22323
22324           return true;
22325         }
22326
22327     case DIV:
22328     case UDIV:
22329     case MOD:
22330     case UMOD:
22331       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22332         /* ??? SSE cost should be used here.  */
22333         *total = ix86_cost->fdiv;
22334       else if (X87_FLOAT_MODE_P (mode))
22335         *total = ix86_cost->fdiv;
22336       else if (FLOAT_MODE_P (mode))
22337         /* ??? SSE vector cost should be used here.  */
22338         *total = ix86_cost->fdiv;
22339       else
22340         *total = ix86_cost->divide[MODE_INDEX (mode)];
22341       return false;
22342
22343     case PLUS:
22344       if (GET_MODE_CLASS (mode) == MODE_INT
22345                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
22346         {
22347           if (GET_CODE (XEXP (x, 0)) == PLUS
22348               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
22349               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
22350               && CONSTANT_P (XEXP (x, 1)))
22351             {
22352               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
22353               if (val == 2 || val == 4 || val == 8)
22354                 {
22355                   *total = ix86_cost->lea;
22356                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
22357                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
22358                                       outer_code);
22359                   *total += rtx_cost (XEXP (x, 1), outer_code);
22360                   return true;
22361                 }
22362             }
22363           else if (GET_CODE (XEXP (x, 0)) == MULT
22364                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
22365             {
22366               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
22367               if (val == 2 || val == 4 || val == 8)
22368                 {
22369                   *total = ix86_cost->lea;
22370                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
22371                   *total += rtx_cost (XEXP (x, 1), outer_code);
22372                   return true;
22373                 }
22374             }
22375           else if (GET_CODE (XEXP (x, 0)) == PLUS)
22376             {
22377               *total = ix86_cost->lea;
22378               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
22379               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
22380               *total += rtx_cost (XEXP (x, 1), outer_code);
22381               return true;
22382             }
22383         }
22384       /* FALLTHRU */
22385
22386     case MINUS:
22387       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22388         {
22389           /* ??? SSE cost should be used here.  */
22390           *total = ix86_cost->fadd;
22391           return false;
22392         }
22393       else if (X87_FLOAT_MODE_P (mode))
22394         {
22395           *total = ix86_cost->fadd;
22396           return false;
22397         }
22398       else if (FLOAT_MODE_P (mode))
22399         {
22400           /* ??? SSE vector cost should be used here.  */
22401           *total = ix86_cost->fadd;
22402           return false;
22403         }
22404       /* FALLTHRU */
22405
22406     case AND:
22407     case IOR:
22408     case XOR:
22409       if (!TARGET_64BIT && mode == DImode)
22410         {
22411           *total = (ix86_cost->add * 2
22412                     + (rtx_cost (XEXP (x, 0), outer_code)
22413                        << (GET_MODE (XEXP (x, 0)) != DImode))
22414                     + (rtx_cost (XEXP (x, 1), outer_code)
22415                        << (GET_MODE (XEXP (x, 1)) != DImode)));
22416           return true;
22417         }
22418       /* FALLTHRU */
22419
22420     case NEG:
22421       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22422         {
22423           /* ??? SSE cost should be used here.  */
22424           *total = ix86_cost->fchs;
22425           return false;
22426         }
22427       else if (X87_FLOAT_MODE_P (mode))
22428         {
22429           *total = ix86_cost->fchs;
22430           return false;
22431         }
22432       else if (FLOAT_MODE_P (mode))
22433         {
22434           /* ??? SSE vector cost should be used here.  */
22435           *total = ix86_cost->fchs;
22436           return false;
22437         }
22438       /* FALLTHRU */
22439
22440     case NOT:
22441       if (!TARGET_64BIT && mode == DImode)
22442         *total = ix86_cost->add * 2;
22443       else
22444         *total = ix86_cost->add;
22445       return false;
22446
22447     case COMPARE:
22448       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
22449           && XEXP (XEXP (x, 0), 1) == const1_rtx
22450           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
22451           && XEXP (x, 1) == const0_rtx)
22452         {
22453           /* This kind of construct is implemented using test[bwl].
22454              Treat it as if we had an AND.  */
22455           *total = (ix86_cost->add
22456                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
22457                     + rtx_cost (const1_rtx, outer_code));
22458           return true;
22459         }
22460       return false;
22461
22462     case FLOAT_EXTEND:
22463       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
22464         *total = 0;
22465       return false;
22466
22467     case ABS:
22468       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22469         /* ??? SSE cost should be used here.  */
22470         *total = ix86_cost->fabs;
22471       else if (X87_FLOAT_MODE_P (mode))
22472         *total = ix86_cost->fabs;
22473       else if (FLOAT_MODE_P (mode))
22474         /* ??? SSE vector cost should be used here.  */
22475         *total = ix86_cost->fabs;
22476       return false;
22477
22478     case SQRT:
22479       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
22480         /* ??? SSE cost should be used here.  */
22481         *total = ix86_cost->fsqrt;
22482       else if (X87_FLOAT_MODE_P (mode))
22483         *total = ix86_cost->fsqrt;
22484       else if (FLOAT_MODE_P (mode))
22485         /* ??? SSE vector cost should be used here.  */
22486         *total = ix86_cost->fsqrt;
22487       return false;
22488
22489     case UNSPEC:
22490       if (XINT (x, 1) == UNSPEC_TP)
22491         *total = 0;
22492       return false;
22493
22494     default:
22495       return false;
22496     }
22497 }
22498
22499 #if TARGET_MACHO
22500
22501 static int current_machopic_label_num;
22502
22503 /* Given a symbol name and its associated stub, write out the
22504    definition of the stub.  */
22505
22506 void
22507 machopic_output_stub (FILE *file, const char *symb, const char *stub)
22508 {
22509   unsigned int length;
22510   char *binder_name, *symbol_name, lazy_ptr_name[32];
22511   int label = ++current_machopic_label_num;
22512
22513   /* For 64-bit we shouldn't get here.  */
22514   gcc_assert (!TARGET_64BIT);
22515
22516   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
22517   symb = (*targetm.strip_name_encoding) (symb);
22518
22519   length = strlen (stub);
22520   binder_name = alloca (length + 32);
22521   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
22522
22523   length = strlen (symb);
22524   symbol_name = alloca (length + 32);
22525   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
22526
22527   sprintf (lazy_ptr_name, "L%d$lz", label);
22528
22529   if (MACHOPIC_PURE)
22530     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
22531   else
22532     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
22533
22534   fprintf (file, "%s:\n", stub);
22535   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
22536
22537   if (MACHOPIC_PURE)
22538     {
22539       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
22540       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
22541       fprintf (file, "\tjmp\t*%%edx\n");
22542     }
22543   else
22544     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
22545
22546   fprintf (file, "%s:\n", binder_name);
22547
22548   if (MACHOPIC_PURE)
22549     {
22550       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
22551       fprintf (file, "\tpushl\t%%eax\n");
22552     }
22553   else
22554     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
22555
22556   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
22557
22558   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
22559   fprintf (file, "%s:\n", lazy_ptr_name);
22560   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
22561   fprintf (file, "\t.long %s\n", binder_name);
22562 }
22563
22564 void
22565 darwin_x86_file_end (void)
22566 {
22567   darwin_file_end ();
22568   ix86_file_end ();
22569 }
22570 #endif /* TARGET_MACHO */
22571
22572 /* Order the registers for register allocator.  */
22573
22574 void
22575 x86_order_regs_for_local_alloc (void)
22576 {
22577    int pos = 0;
22578    int i;
22579
22580    /* First allocate the local general purpose registers.  */
22581    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
22582      if (GENERAL_REGNO_P (i) && call_used_regs[i])
22583         reg_alloc_order [pos++] = i;
22584
22585    /* Global general purpose registers.  */
22586    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
22587      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
22588         reg_alloc_order [pos++] = i;
22589
22590    /* x87 registers come first in case we are doing FP math
22591       using them.  */
22592    if (!TARGET_SSE_MATH)
22593      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
22594        reg_alloc_order [pos++] = i;
22595
22596    /* SSE registers.  */
22597    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
22598      reg_alloc_order [pos++] = i;
22599    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
22600      reg_alloc_order [pos++] = i;
22601
22602    /* x87 registers.  */
22603    if (TARGET_SSE_MATH)
22604      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
22605        reg_alloc_order [pos++] = i;
22606
22607    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
22608      reg_alloc_order [pos++] = i;
22609
22610    /* Initialize the rest of array as we do not allocate some registers
22611       at all.  */
22612    while (pos < FIRST_PSEUDO_REGISTER)
22613      reg_alloc_order [pos++] = 0;
22614 }
22615
22616 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
22617    struct attribute_spec.handler.  */
22618 static tree
22619 ix86_handle_struct_attribute (tree *node, tree name,
22620                               tree args ATTRIBUTE_UNUSED,
22621                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
22622 {
22623   tree *type = NULL;
22624   if (DECL_P (*node))
22625     {
22626       if (TREE_CODE (*node) == TYPE_DECL)
22627         type = &TREE_TYPE (*node);
22628     }
22629   else
22630     type = node;
22631
22632   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
22633                  || TREE_CODE (*type) == UNION_TYPE)))
22634     {
22635       warning (OPT_Wattributes, "%qs attribute ignored",
22636                IDENTIFIER_POINTER (name));
22637       *no_add_attrs = true;
22638     }
22639
22640   else if ((is_attribute_p ("ms_struct", name)
22641             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
22642            || ((is_attribute_p ("gcc_struct", name)
22643                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
22644     {
22645       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
22646                IDENTIFIER_POINTER (name));
22647       *no_add_attrs = true;
22648     }
22649
22650   return NULL_TREE;
22651 }
22652
22653 static bool
22654 ix86_ms_bitfield_layout_p (const_tree record_type)
22655 {
22656   return (TARGET_MS_BITFIELD_LAYOUT &&
22657           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
22658     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
22659 }
22660
22661 /* Returns an expression indicating where the this parameter is
22662    located on entry to the FUNCTION.  */
22663
22664 static rtx
22665 x86_this_parameter (tree function)
22666 {
22667   tree type = TREE_TYPE (function);
22668   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
22669
22670   if (TARGET_64BIT)
22671     {
22672       const int *parm_regs;
22673
22674       if (TARGET_64BIT_MS_ABI)
22675         parm_regs = x86_64_ms_abi_int_parameter_registers;
22676       else
22677         parm_regs = x86_64_int_parameter_registers;
22678       return gen_rtx_REG (DImode, parm_regs[aggr]);
22679     }
22680
22681   if (ix86_function_regparm (type, function) > 0 && !stdarg_p (type))
22682     {
22683       int regno = AX_REG;
22684       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
22685         regno = CX_REG;
22686       return gen_rtx_REG (SImode, regno);
22687     }
22688
22689   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
22690 }
22691
22692 /* Determine whether x86_output_mi_thunk can succeed.  */
22693
22694 static bool
22695 x86_can_output_mi_thunk (const_tree thunk ATTRIBUTE_UNUSED,
22696                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
22697                          HOST_WIDE_INT vcall_offset, const_tree function)
22698 {
22699   /* 64-bit can handle anything.  */
22700   if (TARGET_64BIT)
22701     return true;
22702
22703   /* For 32-bit, everything's fine if we have one free register.  */
22704   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
22705     return true;
22706
22707   /* Need a free register for vcall_offset.  */
22708   if (vcall_offset)
22709     return false;
22710
22711   /* Need a free register for GOT references.  */
22712   if (flag_pic && !(*targetm.binds_local_p) (function))
22713     return false;
22714
22715   /* Otherwise ok.  */
22716   return true;
22717 }
22718
22719 /* Output the assembler code for a thunk function.  THUNK_DECL is the
22720    declaration for the thunk function itself, FUNCTION is the decl for
22721    the target function.  DELTA is an immediate constant offset to be
22722    added to THIS.  If VCALL_OFFSET is nonzero, the word at
22723    *(*this + vcall_offset) should be added to THIS.  */
22724
22725 static void
22726 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
22727                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
22728                      HOST_WIDE_INT vcall_offset, tree function)
22729 {
22730   rtx xops[3];
22731   rtx this_param = x86_this_parameter (function);
22732   rtx this_reg, tmp;
22733
22734   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
22735      pull it in now and let DELTA benefit.  */
22736   if (REG_P (this_param))
22737     this_reg = this_param;
22738   else if (vcall_offset)
22739     {
22740       /* Put the this parameter into %eax.  */
22741       xops[0] = this_param;
22742       xops[1] = this_reg = gen_rtx_REG (Pmode, AX_REG);
22743       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
22744     }
22745   else
22746     this_reg = NULL_RTX;
22747
22748   /* Adjust the this parameter by a fixed constant.  */
22749   if (delta)
22750     {
22751       xops[0] = GEN_INT (delta);
22752       xops[1] = this_reg ? this_reg : this_param;
22753       if (TARGET_64BIT)
22754         {
22755           if (!x86_64_general_operand (xops[0], DImode))
22756             {
22757               tmp = gen_rtx_REG (DImode, R10_REG);
22758               xops[1] = tmp;
22759               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
22760               xops[0] = tmp;
22761               xops[1] = this_param;
22762             }
22763           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
22764         }
22765       else
22766         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
22767     }
22768
22769   /* Adjust the this parameter by a value stored in the vtable.  */
22770   if (vcall_offset)
22771     {
22772       if (TARGET_64BIT)
22773         tmp = gen_rtx_REG (DImode, R10_REG);
22774       else
22775         {
22776           int tmp_regno = CX_REG;
22777           if (lookup_attribute ("fastcall",
22778                                 TYPE_ATTRIBUTES (TREE_TYPE (function))))
22779             tmp_regno = AX_REG;
22780           tmp = gen_rtx_REG (SImode, tmp_regno);
22781         }
22782
22783       xops[0] = gen_rtx_MEM (Pmode, this_reg);
22784       xops[1] = tmp;
22785       if (TARGET_64BIT)
22786         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
22787       else
22788         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
22789
22790       /* Adjust the this parameter.  */
22791       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
22792       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
22793         {
22794           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
22795           xops[0] = GEN_INT (vcall_offset);
22796           xops[1] = tmp2;
22797           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
22798           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
22799         }
22800       xops[1] = this_reg;
22801       if (TARGET_64BIT)
22802         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
22803       else
22804         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
22805     }
22806
22807   /* If necessary, drop THIS back to its stack slot.  */
22808   if (this_reg && this_reg != this_param)
22809     {
22810       xops[0] = this_reg;
22811       xops[1] = this_param;
22812       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
22813     }
22814
22815   xops[0] = XEXP (DECL_RTL (function), 0);
22816   if (TARGET_64BIT)
22817     {
22818       if (!flag_pic || (*targetm.binds_local_p) (function))
22819         output_asm_insn ("jmp\t%P0", xops);
22820       /* All thunks should be in the same object as their target,
22821          and thus binds_local_p should be true.  */
22822       else if (TARGET_64BIT_MS_ABI)
22823         gcc_unreachable ();
22824       else
22825         {
22826           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
22827           tmp = gen_rtx_CONST (Pmode, tmp);
22828           tmp = gen_rtx_MEM (QImode, tmp);
22829           xops[0] = tmp;
22830           output_asm_insn ("jmp\t%A0", xops);
22831         }
22832     }
22833   else
22834     {
22835       if (!flag_pic || (*targetm.binds_local_p) (function))
22836         output_asm_insn ("jmp\t%P0", xops);
22837       else
22838 #if TARGET_MACHO
22839         if (TARGET_MACHO)
22840           {
22841             rtx sym_ref = XEXP (DECL_RTL (function), 0);
22842             tmp = (gen_rtx_SYMBOL_REF
22843                    (Pmode,
22844                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
22845             tmp = gen_rtx_MEM (QImode, tmp);
22846             xops[0] = tmp;
22847             output_asm_insn ("jmp\t%0", xops);
22848           }
22849         else
22850 #endif /* TARGET_MACHO */
22851         {
22852           tmp = gen_rtx_REG (SImode, CX_REG);
22853           output_set_got (tmp, NULL_RTX);
22854
22855           xops[1] = tmp;
22856           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
22857           output_asm_insn ("jmp\t{*}%1", xops);
22858         }
22859     }
22860 }
22861
22862 static void
22863 x86_file_start (void)
22864 {
22865   default_file_start ();
22866 #if TARGET_MACHO
22867   darwin_file_start ();
22868 #endif
22869   if (X86_FILE_START_VERSION_DIRECTIVE)
22870     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
22871   if (X86_FILE_START_FLTUSED)
22872     fputs ("\t.global\t__fltused\n", asm_out_file);
22873   if (ix86_asm_dialect == ASM_INTEL)
22874     fputs ("\t.intel_syntax noprefix\n", asm_out_file);
22875 }
22876
22877 int
22878 x86_field_alignment (tree field, int computed)
22879 {
22880   enum machine_mode mode;
22881   tree type = TREE_TYPE (field);
22882
22883   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
22884     return computed;
22885   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
22886                     ? get_inner_array_type (type) : type);
22887   if (mode == DFmode || mode == DCmode
22888       || GET_MODE_CLASS (mode) == MODE_INT
22889       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
22890     return MIN (32, computed);
22891   return computed;
22892 }
22893
22894 /* Output assembler code to FILE to increment profiler label # LABELNO
22895    for profiling a function entry.  */
22896 void
22897 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
22898 {
22899   if (TARGET_64BIT)
22900     {
22901 #ifndef NO_PROFILE_COUNTERS
22902       fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
22903 #endif
22904
22905       if (!TARGET_64BIT_MS_ABI && flag_pic)
22906         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
22907       else
22908         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
22909     }
22910   else if (flag_pic)
22911     {
22912 #ifndef NO_PROFILE_COUNTERS
22913       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
22914                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
22915 #endif
22916       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
22917     }
22918   else
22919     {
22920 #ifndef NO_PROFILE_COUNTERS
22921       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
22922                PROFILE_COUNT_REGISTER);
22923 #endif
22924       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
22925     }
22926 }
22927
22928 /* We don't have exact information about the insn sizes, but we may assume
22929    quite safely that we are informed about all 1 byte insns and memory
22930    address sizes.  This is enough to eliminate unnecessary padding in
22931    99% of cases.  */
22932
22933 static int
22934 min_insn_size (rtx insn)
22935 {
22936   int l = 0;
22937
22938   if (!INSN_P (insn) || !active_insn_p (insn))
22939     return 0;
22940
22941   /* Discard alignments we've emit and jump instructions.  */
22942   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
22943       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
22944     return 0;
22945   if (JUMP_P (insn)
22946       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
22947           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
22948     return 0;
22949
22950   /* Important case - calls are always 5 bytes.
22951      It is common to have many calls in the row.  */
22952   if (CALL_P (insn)
22953       && symbolic_reference_mentioned_p (PATTERN (insn))
22954       && !SIBLING_CALL_P (insn))
22955     return 5;
22956   if (get_attr_length (insn) <= 1)
22957     return 1;
22958
22959   /* For normal instructions we may rely on the sizes of addresses
22960      and the presence of symbol to require 4 bytes of encoding.
22961      This is not the case for jumps where references are PC relative.  */
22962   if (!JUMP_P (insn))
22963     {
22964       l = get_attr_length_address (insn);
22965       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
22966         l = 4;
22967     }
22968   if (l)
22969     return 1+l;
22970   else
22971     return 2;
22972 }
22973
22974 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
22975    window.  */
22976
22977 static void
22978 ix86_avoid_jump_misspredicts (void)
22979 {
22980   rtx insn, start = get_insns ();
22981   int nbytes = 0, njumps = 0;
22982   int isjump = 0;
22983
22984   /* Look for all minimal intervals of instructions containing 4 jumps.
22985      The intervals are bounded by START and INSN.  NBYTES is the total
22986      size of instructions in the interval including INSN and not including
22987      START.  When the NBYTES is smaller than 16 bytes, it is possible
22988      that the end of START and INSN ends up in the same 16byte page.
22989
22990      The smallest offset in the page INSN can start is the case where START
22991      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
22992      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
22993      */
22994   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
22995     {
22996
22997       nbytes += min_insn_size (insn);
22998       if (dump_file)
22999         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
23000                 INSN_UID (insn), min_insn_size (insn));
23001       if ((JUMP_P (insn)
23002            && GET_CODE (PATTERN (insn)) != ADDR_VEC
23003            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
23004           || CALL_P (insn))
23005         njumps++;
23006       else
23007         continue;
23008
23009       while (njumps > 3)
23010         {
23011           start = NEXT_INSN (start);
23012           if ((JUMP_P (start)
23013                && GET_CODE (PATTERN (start)) != ADDR_VEC
23014                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
23015               || CALL_P (start))
23016             njumps--, isjump = 1;
23017           else
23018             isjump = 0;
23019           nbytes -= min_insn_size (start);
23020         }
23021       gcc_assert (njumps >= 0);
23022       if (dump_file)
23023         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
23024                 INSN_UID (start), INSN_UID (insn), nbytes);
23025
23026       if (njumps == 3 && isjump && nbytes < 16)
23027         {
23028           int padsize = 15 - nbytes + min_insn_size (insn);
23029
23030           if (dump_file)
23031             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
23032                      INSN_UID (insn), padsize);
23033           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
23034         }
23035     }
23036 }
23037
23038 /* AMD Athlon works faster
23039    when RET is not destination of conditional jump or directly preceded
23040    by other jump instruction.  We avoid the penalty by inserting NOP just
23041    before the RET instructions in such cases.  */
23042 static void
23043 ix86_pad_returns (void)
23044 {
23045   edge e;
23046   edge_iterator ei;
23047
23048   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
23049     {
23050       basic_block bb = e->src;
23051       rtx ret = BB_END (bb);
23052       rtx prev;
23053       bool replace = false;
23054
23055       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
23056           || !maybe_hot_bb_p (bb))
23057         continue;
23058       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
23059         if (active_insn_p (prev) || LABEL_P (prev))
23060           break;
23061       if (prev && LABEL_P (prev))
23062         {
23063           edge e;
23064           edge_iterator ei;
23065
23066           FOR_EACH_EDGE (e, ei, bb->preds)
23067             if (EDGE_FREQUENCY (e) && e->src->index >= 0
23068                 && !(e->flags & EDGE_FALLTHRU))
23069               replace = true;
23070         }
23071       if (!replace)
23072         {
23073           prev = prev_active_insn (ret);
23074           if (prev
23075               && ((JUMP_P (prev) && any_condjump_p (prev))
23076                   || CALL_P (prev)))
23077             replace = true;
23078           /* Empty functions get branch mispredict even when the jump destination
23079              is not visible to us.  */
23080           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
23081             replace = true;
23082         }
23083       if (replace)
23084         {
23085           emit_insn_before (gen_return_internal_long (), ret);
23086           delete_insn (ret);
23087         }
23088     }
23089 }
23090
23091 /* Implement machine specific optimizations.  We implement padding of returns
23092    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
23093 static void
23094 ix86_reorg (void)
23095 {
23096   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
23097     ix86_pad_returns ();
23098   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
23099     ix86_avoid_jump_misspredicts ();
23100 }
23101
23102 /* Return nonzero when QImode register that must be represented via REX prefix
23103    is used.  */
23104 bool
23105 x86_extended_QIreg_mentioned_p (rtx insn)
23106 {
23107   int i;
23108   extract_insn_cached (insn);
23109   for (i = 0; i < recog_data.n_operands; i++)
23110     if (REG_P (recog_data.operand[i])
23111         && REGNO (recog_data.operand[i]) >= 4)
23112        return true;
23113   return false;
23114 }
23115
23116 /* Return nonzero when P points to register encoded via REX prefix.
23117    Called via for_each_rtx.  */
23118 static int
23119 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
23120 {
23121    unsigned int regno;
23122    if (!REG_P (*p))
23123      return 0;
23124    regno = REGNO (*p);
23125    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
23126 }
23127
23128 /* Return true when INSN mentions register that must be encoded using REX
23129    prefix.  */
23130 bool
23131 x86_extended_reg_mentioned_p (rtx insn)
23132 {
23133   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
23134 }
23135
23136 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
23137    optabs would emit if we didn't have TFmode patterns.  */
23138
23139 void
23140 x86_emit_floatuns (rtx operands[2])
23141 {
23142   rtx neglab, donelab, i0, i1, f0, in, out;
23143   enum machine_mode mode, inmode;
23144
23145   inmode = GET_MODE (operands[1]);
23146   gcc_assert (inmode == SImode || inmode == DImode);
23147
23148   out = operands[0];
23149   in = force_reg (inmode, operands[1]);
23150   mode = GET_MODE (out);
23151   neglab = gen_label_rtx ();
23152   donelab = gen_label_rtx ();
23153   f0 = gen_reg_rtx (mode);
23154
23155   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
23156
23157   expand_float (out, in, 0);
23158
23159   emit_jump_insn (gen_jump (donelab));
23160   emit_barrier ();
23161
23162   emit_label (neglab);
23163
23164   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
23165                             1, OPTAB_DIRECT);
23166   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
23167                             1, OPTAB_DIRECT);
23168   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
23169
23170   expand_float (f0, i0, 0);
23171
23172   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
23173
23174   emit_label (donelab);
23175 }
23176 \f
23177 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
23178    with all elements equal to VAR.  Return true if successful.  */
23179
23180 static bool
23181 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
23182                                    rtx target, rtx val)
23183 {
23184   enum machine_mode smode, wsmode, wvmode;
23185   rtx x;
23186
23187   switch (mode)
23188     {
23189     case V2SImode:
23190     case V2SFmode:
23191       if (!mmx_ok)
23192         return false;
23193       /* FALLTHRU */
23194
23195     case V2DFmode:
23196     case V2DImode:
23197     case V4SFmode:
23198     case V4SImode:
23199       val = force_reg (GET_MODE_INNER (mode), val);
23200       x = gen_rtx_VEC_DUPLICATE (mode, val);
23201       emit_insn (gen_rtx_SET (VOIDmode, target, x));
23202       return true;
23203
23204     case V4HImode:
23205       if (!mmx_ok)
23206         return false;
23207       if (TARGET_SSE || TARGET_3DNOW_A)
23208         {
23209           val = gen_lowpart (SImode, val);
23210           x = gen_rtx_TRUNCATE (HImode, val);
23211           x = gen_rtx_VEC_DUPLICATE (mode, x);
23212           emit_insn (gen_rtx_SET (VOIDmode, target, x));
23213           return true;
23214         }
23215       else
23216         {
23217           smode = HImode;
23218           wsmode = SImode;
23219           wvmode = V2SImode;
23220           goto widen;
23221         }
23222
23223     case V8QImode:
23224       if (!mmx_ok)
23225         return false;
23226       smode = QImode;
23227       wsmode = HImode;
23228       wvmode = V4HImode;
23229       goto widen;
23230     case V8HImode:
23231       if (TARGET_SSE2)
23232         {
23233           rtx tmp1, tmp2;
23234           /* Extend HImode to SImode using a paradoxical SUBREG.  */
23235           tmp1 = gen_reg_rtx (SImode);
23236           emit_move_insn (tmp1, gen_lowpart (SImode, val));
23237           /* Insert the SImode value as low element of V4SImode vector. */
23238           tmp2 = gen_reg_rtx (V4SImode);
23239           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
23240                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
23241                                     CONST0_RTX (V4SImode),
23242                                     const1_rtx);
23243           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
23244           /* Cast the V4SImode vector back to a V8HImode vector.  */
23245           tmp1 = gen_reg_rtx (V8HImode);
23246           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
23247           /* Duplicate the low short through the whole low SImode word.  */
23248           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
23249           /* Cast the V8HImode vector back to a V4SImode vector.  */
23250           tmp2 = gen_reg_rtx (V4SImode);
23251           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
23252           /* Replicate the low element of the V4SImode vector.  */
23253           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
23254           /* Cast the V2SImode back to V8HImode, and store in target.  */
23255           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
23256           return true;
23257         }
23258       smode = HImode;
23259       wsmode = SImode;
23260       wvmode = V4SImode;
23261       goto widen;
23262     case V16QImode:
23263       if (TARGET_SSE2)
23264         {
23265           rtx tmp1, tmp2;
23266           /* Extend QImode to SImode using a paradoxical SUBREG.  */
23267           tmp1 = gen_reg_rtx (SImode);
23268           emit_move_insn (tmp1, gen_lowpart (SImode, val));
23269           /* Insert the SImode value as low element of V4SImode vector. */
23270           tmp2 = gen_reg_rtx (V4SImode);
23271           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
23272                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
23273                                     CONST0_RTX (V4SImode),
23274                                     const1_rtx);
23275           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
23276           /* Cast the V4SImode vector back to a V16QImode vector.  */
23277           tmp1 = gen_reg_rtx (V16QImode);
23278           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
23279           /* Duplicate the low byte through the whole low SImode word.  */
23280           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
23281           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
23282           /* Cast the V16QImode vector back to a V4SImode vector.  */
23283           tmp2 = gen_reg_rtx (V4SImode);
23284           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
23285           /* Replicate the low element of the V4SImode vector.  */
23286           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
23287           /* Cast the V2SImode back to V16QImode, and store in target.  */
23288           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
23289           return true;
23290         }
23291       smode = QImode;
23292       wsmode = HImode;
23293       wvmode = V8HImode;
23294       goto widen;
23295     widen:
23296       /* Replicate the value once into the next wider mode and recurse.  */
23297       val = convert_modes (wsmode, smode, val, true);
23298       x = expand_simple_binop (wsmode, ASHIFT, val,
23299                                GEN_INT (GET_MODE_BITSIZE (smode)),
23300                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
23301       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
23302
23303       x = gen_reg_rtx (wvmode);
23304       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
23305         gcc_unreachable ();
23306       emit_move_insn (target, gen_lowpart (mode, x));
23307       return true;
23308
23309     default:
23310       return false;
23311     }
23312 }
23313
23314 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
23315    whose ONE_VAR element is VAR, and other elements are zero.  Return true
23316    if successful.  */
23317
23318 static bool
23319 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
23320                                      rtx target, rtx var, int one_var)
23321 {
23322   enum machine_mode vsimode;
23323   rtx new_target;
23324   rtx x, tmp;
23325
23326   switch (mode)
23327     {
23328     case V2SFmode:
23329     case V2SImode:
23330       if (!mmx_ok)
23331         return false;
23332       /* FALLTHRU */
23333
23334     case V2DFmode:
23335     case V2DImode:
23336       if (one_var != 0)
23337         return false;
23338       var = force_reg (GET_MODE_INNER (mode), var);
23339       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
23340       emit_insn (gen_rtx_SET (VOIDmode, target, x));
23341       return true;
23342
23343     case V4SFmode:
23344     case V4SImode:
23345       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
23346         new_target = gen_reg_rtx (mode);
23347       else
23348         new_target = target;
23349       var = force_reg (GET_MODE_INNER (mode), var);
23350       x = gen_rtx_VEC_DUPLICATE (mode, var);
23351       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
23352       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
23353       if (one_var != 0)
23354         {
23355           /* We need to shuffle the value to the correct position, so
23356              create a new pseudo to store the intermediate result.  */
23357
23358           /* With SSE2, we can use the integer shuffle insns.  */
23359           if (mode != V4SFmode && TARGET_SSE2)
23360             {
23361               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
23362                                             GEN_INT (1),
23363                                             GEN_INT (one_var == 1 ? 0 : 1),
23364                                             GEN_INT (one_var == 2 ? 0 : 1),
23365                                             GEN_INT (one_var == 3 ? 0 : 1)));
23366               if (target != new_target)
23367                 emit_move_insn (target, new_target);
23368               return true;
23369             }
23370
23371           /* Otherwise convert the intermediate result to V4SFmode and
23372              use the SSE1 shuffle instructions.  */
23373           if (mode != V4SFmode)
23374             {
23375               tmp = gen_reg_rtx (V4SFmode);
23376               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
23377             }
23378           else
23379             tmp = new_target;
23380
23381           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
23382                                        GEN_INT (1),
23383                                        GEN_INT (one_var == 1 ? 0 : 1),
23384                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
23385                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
23386
23387           if (mode != V4SFmode)
23388             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
23389           else if (tmp != target)
23390             emit_move_insn (target, tmp);
23391         }
23392       else if (target != new_target)
23393         emit_move_insn (target, new_target);
23394       return true;
23395
23396     case V8HImode:
23397     case V16QImode:
23398       vsimode = V4SImode;
23399       goto widen;
23400     case V4HImode:
23401     case V8QImode:
23402       if (!mmx_ok)
23403         return false;
23404       vsimode = V2SImode;
23405       goto widen;
23406     widen:
23407       if (one_var != 0)
23408         return false;
23409
23410       /* Zero extend the variable element to SImode and recurse.  */
23411       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
23412
23413       x = gen_reg_rtx (vsimode);
23414       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
23415                                                 var, one_var))
23416         gcc_unreachable ();
23417
23418       emit_move_insn (target, gen_lowpart (mode, x));
23419       return true;
23420
23421     default:
23422       return false;
23423     }
23424 }
23425
23426 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
23427    consisting of the values in VALS.  It is known that all elements
23428    except ONE_VAR are constants.  Return true if successful.  */
23429
23430 static bool
23431 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
23432                                  rtx target, rtx vals, int one_var)
23433 {
23434   rtx var = XVECEXP (vals, 0, one_var);
23435   enum machine_mode wmode;
23436   rtx const_vec, x;
23437
23438   const_vec = copy_rtx (vals);
23439   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
23440   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
23441
23442   switch (mode)
23443     {
23444     case V2DFmode:
23445     case V2DImode:
23446     case V2SFmode:
23447     case V2SImode:
23448       /* For the two element vectors, it's just as easy to use
23449          the general case.  */
23450       return false;
23451
23452     case V4SFmode:
23453     case V4SImode:
23454     case V8HImode:
23455     case V4HImode:
23456       break;
23457
23458     case V16QImode:
23459       wmode = V8HImode;
23460       goto widen;
23461     case V8QImode:
23462       wmode = V4HImode;
23463       goto widen;
23464     widen:
23465       /* There's no way to set one QImode entry easily.  Combine
23466          the variable value with its adjacent constant value, and
23467          promote to an HImode set.  */
23468       x = XVECEXP (vals, 0, one_var ^ 1);
23469       if (one_var & 1)
23470         {
23471           var = convert_modes (HImode, QImode, var, true);
23472           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
23473                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
23474           x = GEN_INT (INTVAL (x) & 0xff);
23475         }
23476       else
23477         {
23478           var = convert_modes (HImode, QImode, var, true);
23479           x = gen_int_mode (INTVAL (x) << 8, HImode);
23480         }
23481       if (x != const0_rtx)
23482         var = expand_simple_binop (HImode, IOR, var, x, var,
23483                                    1, OPTAB_LIB_WIDEN);
23484
23485       x = gen_reg_rtx (wmode);
23486       emit_move_insn (x, gen_lowpart (wmode, const_vec));
23487       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
23488
23489       emit_move_insn (target, gen_lowpart (mode, x));
23490       return true;
23491
23492     default:
23493       return false;
23494     }
23495
23496   emit_move_insn (target, const_vec);
23497   ix86_expand_vector_set (mmx_ok, target, var, one_var);
23498   return true;
23499 }
23500
23501 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
23502    all values variable, and none identical.  */
23503
23504 static void
23505 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
23506                                  rtx target, rtx vals)
23507 {
23508   enum machine_mode half_mode = GET_MODE_INNER (mode);
23509   rtx op0 = NULL, op1 = NULL;
23510   bool use_vec_concat = false;
23511
23512   switch (mode)
23513     {
23514     case V2SFmode:
23515     case V2SImode:
23516       if (!mmx_ok && !TARGET_SSE)
23517         break;
23518       /* FALLTHRU */
23519
23520     case V2DFmode:
23521     case V2DImode:
23522       /* For the two element vectors, we always implement VEC_CONCAT.  */
23523       op0 = XVECEXP (vals, 0, 0);
23524       op1 = XVECEXP (vals, 0, 1);
23525       use_vec_concat = true;
23526       break;
23527
23528     case V4SFmode:
23529       half_mode = V2SFmode;
23530       goto half;
23531     case V4SImode:
23532       half_mode = V2SImode;
23533       goto half;
23534     half:
23535       {
23536         rtvec v;
23537
23538         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
23539            Recurse to load the two halves.  */
23540
23541         op0 = gen_reg_rtx (half_mode);
23542         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
23543         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
23544
23545         op1 = gen_reg_rtx (half_mode);
23546         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
23547         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
23548
23549         use_vec_concat = true;
23550       }
23551       break;
23552
23553     case V8HImode:
23554     case V16QImode:
23555     case V4HImode:
23556     case V8QImode:
23557       break;
23558
23559     default:
23560       gcc_unreachable ();
23561     }
23562
23563   if (use_vec_concat)
23564     {
23565       if (!register_operand (op0, half_mode))
23566         op0 = force_reg (half_mode, op0);
23567       if (!register_operand (op1, half_mode))
23568         op1 = force_reg (half_mode, op1);
23569
23570       emit_insn (gen_rtx_SET (VOIDmode, target,
23571                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
23572     }
23573   else
23574     {
23575       int i, j, n_elts, n_words, n_elt_per_word;
23576       enum machine_mode inner_mode;
23577       rtx words[4], shift;
23578
23579       inner_mode = GET_MODE_INNER (mode);
23580       n_elts = GET_MODE_NUNITS (mode);
23581       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
23582       n_elt_per_word = n_elts / n_words;
23583       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
23584
23585       for (i = 0; i < n_words; ++i)
23586         {
23587           rtx word = NULL_RTX;
23588
23589           for (j = 0; j < n_elt_per_word; ++j)
23590             {
23591               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
23592               elt = convert_modes (word_mode, inner_mode, elt, true);
23593
23594               if (j == 0)
23595                 word = elt;
23596               else
23597                 {
23598                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
23599                                               word, 1, OPTAB_LIB_WIDEN);
23600                   word = expand_simple_binop (word_mode, IOR, word, elt,
23601                                               word, 1, OPTAB_LIB_WIDEN);
23602                 }
23603             }
23604
23605           words[i] = word;
23606         }
23607
23608       if (n_words == 1)
23609         emit_move_insn (target, gen_lowpart (mode, words[0]));
23610       else if (n_words == 2)
23611         {
23612           rtx tmp = gen_reg_rtx (mode);
23613           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
23614           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
23615           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
23616           emit_move_insn (target, tmp);
23617         }
23618       else if (n_words == 4)
23619         {
23620           rtx tmp = gen_reg_rtx (V4SImode);
23621           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
23622           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
23623           emit_move_insn (target, gen_lowpart (mode, tmp));
23624         }
23625       else
23626         gcc_unreachable ();
23627     }
23628 }
23629
23630 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
23631    instructions unless MMX_OK is true.  */
23632
23633 void
23634 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
23635 {
23636   enum machine_mode mode = GET_MODE (target);
23637   enum machine_mode inner_mode = GET_MODE_INNER (mode);
23638   int n_elts = GET_MODE_NUNITS (mode);
23639   int n_var = 0, one_var = -1;
23640   bool all_same = true, all_const_zero = true;
23641   int i;
23642   rtx x;
23643
23644   for (i = 0; i < n_elts; ++i)
23645     {
23646       x = XVECEXP (vals, 0, i);
23647       if (!(CONST_INT_P (x)
23648             || GET_CODE (x) == CONST_DOUBLE
23649             || GET_CODE (x) == CONST_FIXED))
23650         n_var++, one_var = i;
23651       else if (x != CONST0_RTX (inner_mode))
23652         all_const_zero = false;
23653       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
23654         all_same = false;
23655     }
23656
23657   /* Constants are best loaded from the constant pool.  */
23658   if (n_var == 0)
23659     {
23660       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
23661       return;
23662     }
23663
23664   /* If all values are identical, broadcast the value.  */
23665   if (all_same
23666       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
23667                                             XVECEXP (vals, 0, 0)))
23668     return;
23669
23670   /* Values where only one field is non-constant are best loaded from
23671      the pool and overwritten via move later.  */
23672   if (n_var == 1)
23673     {
23674       if (all_const_zero
23675           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
23676                                                   XVECEXP (vals, 0, one_var),
23677                                                   one_var))
23678         return;
23679
23680       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
23681         return;
23682     }
23683
23684   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
23685 }
23686
23687 void
23688 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
23689 {
23690   enum machine_mode mode = GET_MODE (target);
23691   enum machine_mode inner_mode = GET_MODE_INNER (mode);
23692   bool use_vec_merge = false;
23693   rtx tmp;
23694
23695   switch (mode)
23696     {
23697     case V2SFmode:
23698     case V2SImode:
23699       if (mmx_ok)
23700         {
23701           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
23702           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
23703           if (elt == 0)
23704             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
23705           else
23706             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
23707           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
23708           return;
23709         }
23710       break;
23711
23712     case V2DImode:
23713       use_vec_merge = TARGET_SSE4_1;
23714       if (use_vec_merge)
23715         break;
23716
23717     case V2DFmode:
23718       {
23719         rtx op0, op1;
23720
23721         /* For the two element vectors, we implement a VEC_CONCAT with
23722            the extraction of the other element.  */
23723
23724         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
23725         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
23726
23727         if (elt == 0)
23728           op0 = val, op1 = tmp;
23729         else
23730           op0 = tmp, op1 = val;
23731
23732         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
23733         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
23734       }
23735       return;
23736
23737     case V4SFmode:
23738       use_vec_merge = TARGET_SSE4_1;
23739       if (use_vec_merge)
23740         break;
23741
23742       switch (elt)
23743         {
23744         case 0:
23745           use_vec_merge = true;
23746           break;
23747
23748         case 1:
23749           /* tmp = target = A B C D */
23750           tmp = copy_to_reg (target);
23751           /* target = A A B B */
23752           emit_insn (gen_sse_unpcklps (target, target, target));
23753           /* target = X A B B */
23754           ix86_expand_vector_set (false, target, val, 0);
23755           /* target = A X C D  */
23756           emit_insn (gen_sse_shufps_1 (target, target, tmp,
23757                                        GEN_INT (1), GEN_INT (0),
23758                                        GEN_INT (2+4), GEN_INT (3+4)));
23759           return;
23760
23761         case 2:
23762           /* tmp = target = A B C D */
23763           tmp = copy_to_reg (target);
23764           /* tmp = X B C D */
23765           ix86_expand_vector_set (false, tmp, val, 0);
23766           /* target = A B X D */
23767           emit_insn (gen_sse_shufps_1 (target, target, tmp,
23768                                        GEN_INT (0), GEN_INT (1),
23769                                        GEN_INT (0+4), GEN_INT (3+4)));
23770           return;
23771
23772         case 3:
23773           /* tmp = target = A B C D */
23774           tmp = copy_to_reg (target);
23775           /* tmp = X B C D */
23776           ix86_expand_vector_set (false, tmp, val, 0);
23777           /* target = A B X D */
23778           emit_insn (gen_sse_shufps_1 (target, target, tmp,
23779                                        GEN_INT (0), GEN_INT (1),
23780                                        GEN_INT (2+4), GEN_INT (0+4)));
23781           return;
23782
23783         default:
23784           gcc_unreachable ();
23785         }
23786       break;
23787
23788     case V4SImode:
23789       use_vec_merge = TARGET_SSE4_1;
23790       if (use_vec_merge)
23791         break;
23792
23793       /* Element 0 handled by vec_merge below.  */
23794       if (elt == 0)
23795         {
23796           use_vec_merge = true;
23797           break;
23798         }
23799
23800       if (TARGET_SSE2)
23801         {
23802           /* With SSE2, use integer shuffles to swap element 0 and ELT,
23803              store into element 0, then shuffle them back.  */
23804
23805           rtx order[4];
23806
23807           order[0] = GEN_INT (elt);
23808           order[1] = const1_rtx;
23809           order[2] = const2_rtx;
23810           order[3] = GEN_INT (3);
23811           order[elt] = const0_rtx;
23812
23813           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
23814                                         order[1], order[2], order[3]));
23815
23816           ix86_expand_vector_set (false, target, val, 0);
23817
23818           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
23819                                         order[1], order[2], order[3]));
23820         }
23821       else
23822         {
23823           /* For SSE1, we have to reuse the V4SF code.  */
23824           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
23825                                   gen_lowpart (SFmode, val), elt);
23826         }
23827       return;
23828
23829     case V8HImode:
23830       use_vec_merge = TARGET_SSE2;
23831       break;
23832     case V4HImode:
23833       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
23834       break;
23835
23836     case V16QImode:
23837       use_vec_merge = TARGET_SSE4_1;
23838       break;
23839
23840     case V8QImode:
23841     default:
23842       break;
23843     }
23844
23845   if (use_vec_merge)
23846     {
23847       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
23848       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
23849       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
23850     }
23851   else
23852     {
23853       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
23854
23855       emit_move_insn (mem, target);
23856
23857       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
23858       emit_move_insn (tmp, val);
23859
23860       emit_move_insn (target, mem);
23861     }
23862 }
23863
23864 void
23865 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
23866 {
23867   enum machine_mode mode = GET_MODE (vec);
23868   enum machine_mode inner_mode = GET_MODE_INNER (mode);
23869   bool use_vec_extr = false;
23870   rtx tmp;
23871
23872   switch (mode)
23873     {
23874     case V2SImode:
23875     case V2SFmode:
23876       if (!mmx_ok)
23877         break;
23878       /* FALLTHRU */
23879
23880     case V2DFmode:
23881     case V2DImode:
23882       use_vec_extr = true;
23883       break;
23884
23885     case V4SFmode:
23886       use_vec_extr = TARGET_SSE4_1;
23887       if (use_vec_extr)
23888         break;
23889
23890       switch (elt)
23891         {
23892         case 0:
23893           tmp = vec;
23894           break;
23895
23896         case 1:
23897         case 3:
23898           tmp = gen_reg_rtx (mode);
23899           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
23900                                        GEN_INT (elt), GEN_INT (elt),
23901                                        GEN_INT (elt+4), GEN_INT (elt+4)));
23902           break;
23903
23904         case 2:
23905           tmp = gen_reg_rtx (mode);
23906           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
23907           break;
23908
23909         default:
23910           gcc_unreachable ();
23911         }
23912       vec = tmp;
23913       use_vec_extr = true;
23914       elt = 0;
23915       break;
23916
23917     case V4SImode:
23918       use_vec_extr = TARGET_SSE4_1;
23919       if (use_vec_extr)
23920         break;
23921
23922       if (TARGET_SSE2)
23923         {
23924           switch (elt)
23925             {
23926             case 0:
23927               tmp = vec;
23928               break;
23929
23930             case 1:
23931             case 3:
23932               tmp = gen_reg_rtx (mode);
23933               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
23934                                             GEN_INT (elt), GEN_INT (elt),
23935                                             GEN_INT (elt), GEN_INT (elt)));
23936               break;
23937
23938             case 2:
23939               tmp = gen_reg_rtx (mode);
23940               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
23941               break;
23942
23943             default:
23944               gcc_unreachable ();
23945             }
23946           vec = tmp;
23947           use_vec_extr = true;
23948           elt = 0;
23949         }
23950       else
23951         {
23952           /* For SSE1, we have to reuse the V4SF code.  */
23953           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
23954                                       gen_lowpart (V4SFmode, vec), elt);
23955           return;
23956         }
23957       break;
23958
23959     case V8HImode:
23960       use_vec_extr = TARGET_SSE2;
23961       break;
23962     case V4HImode:
23963       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
23964       break;
23965
23966     case V16QImode:
23967       use_vec_extr = TARGET_SSE4_1;
23968       break;
23969
23970     case V8QImode:
23971       /* ??? Could extract the appropriate HImode element and shift.  */
23972     default:
23973       break;
23974     }
23975
23976   if (use_vec_extr)
23977     {
23978       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
23979       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
23980
23981       /* Let the rtl optimizers know about the zero extension performed.  */
23982       if (inner_mode == QImode || inner_mode == HImode)
23983         {
23984           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
23985           target = gen_lowpart (SImode, target);
23986         }
23987
23988       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
23989     }
23990   else
23991     {
23992       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
23993
23994       emit_move_insn (mem, vec);
23995
23996       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
23997       emit_move_insn (target, tmp);
23998     }
23999 }
24000
24001 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
24002    pattern to reduce; DEST is the destination; IN is the input vector.  */
24003
24004 void
24005 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
24006 {
24007   rtx tmp1, tmp2, tmp3;
24008
24009   tmp1 = gen_reg_rtx (V4SFmode);
24010   tmp2 = gen_reg_rtx (V4SFmode);
24011   tmp3 = gen_reg_rtx (V4SFmode);
24012
24013   emit_insn (gen_sse_movhlps (tmp1, in, in));
24014   emit_insn (fn (tmp2, tmp1, in));
24015
24016   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
24017                                GEN_INT (1), GEN_INT (1),
24018                                GEN_INT (1+4), GEN_INT (1+4)));
24019   emit_insn (fn (dest, tmp2, tmp3));
24020 }
24021 \f
24022 /* Target hook for scalar_mode_supported_p.  */
24023 static bool
24024 ix86_scalar_mode_supported_p (enum machine_mode mode)
24025 {
24026   if (DECIMAL_FLOAT_MODE_P (mode))
24027     return true;
24028   else if (mode == TFmode)
24029     return TARGET_64BIT;
24030   else
24031     return default_scalar_mode_supported_p (mode);
24032 }
24033
24034 /* Implements target hook vector_mode_supported_p.  */
24035 static bool
24036 ix86_vector_mode_supported_p (enum machine_mode mode)
24037 {
24038   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
24039     return true;
24040   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
24041     return true;
24042   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
24043     return true;
24044   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
24045     return true;
24046   return false;
24047 }
24048
24049 /* Target hook for c_mode_for_suffix.  */
24050 static enum machine_mode
24051 ix86_c_mode_for_suffix (char suffix)
24052 {
24053   if (TARGET_64BIT && suffix == 'q')
24054     return TFmode;
24055   if (TARGET_MMX && suffix == 'w')
24056     return XFmode;
24057
24058   return VOIDmode;
24059 }
24060
24061 /* Worker function for TARGET_MD_ASM_CLOBBERS.
24062
24063    We do this in the new i386 backend to maintain source compatibility
24064    with the old cc0-based compiler.  */
24065
24066 static tree
24067 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
24068                       tree inputs ATTRIBUTE_UNUSED,
24069                       tree clobbers)
24070 {
24071   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
24072                         clobbers);
24073   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
24074                         clobbers);
24075   return clobbers;
24076 }
24077
24078 /* Implements target vector targetm.asm.encode_section_info.  This
24079    is not used by netware.  */
24080
24081 static void ATTRIBUTE_UNUSED
24082 ix86_encode_section_info (tree decl, rtx rtl, int first)
24083 {
24084   default_encode_section_info (decl, rtl, first);
24085
24086   if (TREE_CODE (decl) == VAR_DECL
24087       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
24088       && ix86_in_large_data_p (decl))
24089     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
24090 }
24091
24092 /* Worker function for REVERSE_CONDITION.  */
24093
24094 enum rtx_code
24095 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
24096 {
24097   return (mode != CCFPmode && mode != CCFPUmode
24098           ? reverse_condition (code)
24099           : reverse_condition_maybe_unordered (code));
24100 }
24101
24102 /* Output code to perform an x87 FP register move, from OPERANDS[1]
24103    to OPERANDS[0].  */
24104
24105 const char *
24106 output_387_reg_move (rtx insn, rtx *operands)
24107 {
24108   if (REG_P (operands[0]))
24109     {
24110       if (REG_P (operands[1])
24111           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
24112         {
24113           if (REGNO (operands[0]) == FIRST_STACK_REG)
24114             return output_387_ffreep (operands, 0);
24115           return "fstp\t%y0";
24116         }
24117       if (STACK_TOP_P (operands[0]))
24118         return "fld%z1\t%y1";
24119       return "fst\t%y0";
24120     }
24121   else if (MEM_P (operands[0]))
24122     {
24123       gcc_assert (REG_P (operands[1]));
24124       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
24125         return "fstp%z0\t%y0";
24126       else
24127         {
24128           /* There is no non-popping store to memory for XFmode.
24129              So if we need one, follow the store with a load.  */
24130           if (GET_MODE (operands[0]) == XFmode)
24131             return "fstp%z0\t%y0\n\tfld%z0\t%y0";
24132           else
24133             return "fst%z0\t%y0";
24134         }
24135     }
24136   else
24137     gcc_unreachable();
24138 }
24139
24140 /* Output code to perform a conditional jump to LABEL, if C2 flag in
24141    FP status register is set.  */
24142
24143 void
24144 ix86_emit_fp_unordered_jump (rtx label)
24145 {
24146   rtx reg = gen_reg_rtx (HImode);
24147   rtx temp;
24148
24149   emit_insn (gen_x86_fnstsw_1 (reg));
24150
24151   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_size))
24152     {
24153       emit_insn (gen_x86_sahf_1 (reg));
24154
24155       temp = gen_rtx_REG (CCmode, FLAGS_REG);
24156       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
24157     }
24158   else
24159     {
24160       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
24161
24162       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
24163       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
24164     }
24165
24166   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
24167                               gen_rtx_LABEL_REF (VOIDmode, label),
24168                               pc_rtx);
24169   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
24170
24171   emit_jump_insn (temp);
24172   predict_jump (REG_BR_PROB_BASE * 10 / 100);
24173 }
24174
24175 /* Output code to perform a log1p XFmode calculation.  */
24176
24177 void ix86_emit_i387_log1p (rtx op0, rtx op1)
24178 {
24179   rtx label1 = gen_label_rtx ();
24180   rtx label2 = gen_label_rtx ();
24181
24182   rtx tmp = gen_reg_rtx (XFmode);
24183   rtx tmp2 = gen_reg_rtx (XFmode);
24184
24185   emit_insn (gen_absxf2 (tmp, op1));
24186   emit_insn (gen_cmpxf (tmp,
24187     CONST_DOUBLE_FROM_REAL_VALUE (
24188        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
24189        XFmode)));
24190   emit_jump_insn (gen_bge (label1));
24191
24192   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
24193   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
24194   emit_jump (label2);
24195
24196   emit_label (label1);
24197   emit_move_insn (tmp, CONST1_RTX (XFmode));
24198   emit_insn (gen_addxf3 (tmp, op1, tmp));
24199   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
24200   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
24201
24202   emit_label (label2);
24203 }
24204
24205 /* Output code to perform a Newton-Rhapson approximation of a single precision
24206    floating point divide [http://en.wikipedia.org/wiki/N-th_root_algorithm].  */
24207
24208 void ix86_emit_swdivsf (rtx res, rtx a, rtx b, enum machine_mode mode)
24209 {
24210   rtx x0, x1, e0, e1, two;
24211
24212   x0 = gen_reg_rtx (mode);
24213   e0 = gen_reg_rtx (mode);
24214   e1 = gen_reg_rtx (mode);
24215   x1 = gen_reg_rtx (mode);
24216
24217   two = CONST_DOUBLE_FROM_REAL_VALUE (dconst2, SFmode);
24218
24219   if (VECTOR_MODE_P (mode))
24220     two = ix86_build_const_vector (SFmode, true, two);
24221
24222   two = force_reg (mode, two);
24223
24224   /* a / b = a * rcp(b) * (2.0 - b * rcp(b)) */
24225
24226   /* x0 = rcp(b) estimate */
24227   emit_insn (gen_rtx_SET (VOIDmode, x0,
24228                           gen_rtx_UNSPEC (mode, gen_rtvec (1, b),
24229                                           UNSPEC_RCP)));
24230   /* e0 = x0 * b */
24231   emit_insn (gen_rtx_SET (VOIDmode, e0,
24232                           gen_rtx_MULT (mode, x0, b)));
24233   /* e1 = 2. - e0 */
24234   emit_insn (gen_rtx_SET (VOIDmode, e1,
24235                           gen_rtx_MINUS (mode, two, e0)));
24236   /* x1 = x0 * e1 */
24237   emit_insn (gen_rtx_SET (VOIDmode, x1,
24238                           gen_rtx_MULT (mode, x0, e1)));
24239   /* res = a * x1 */
24240   emit_insn (gen_rtx_SET (VOIDmode, res,
24241                           gen_rtx_MULT (mode, a, x1)));
24242 }
24243
24244 /* Output code to perform a Newton-Rhapson approximation of a
24245    single precision floating point [reciprocal] square root.  */
24246
24247 void ix86_emit_swsqrtsf (rtx res, rtx a, enum machine_mode mode,
24248                          bool recip)
24249 {
24250   rtx x0, e0, e1, e2, e3, mthree, mhalf;
24251   REAL_VALUE_TYPE r;
24252
24253   x0 = gen_reg_rtx (mode);
24254   e0 = gen_reg_rtx (mode);
24255   e1 = gen_reg_rtx (mode);
24256   e2 = gen_reg_rtx (mode);
24257   e3 = gen_reg_rtx (mode);
24258
24259   real_arithmetic (&r, NEGATE_EXPR, &dconst3, NULL);
24260   mthree = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
24261
24262   real_arithmetic (&r, NEGATE_EXPR, &dconsthalf, NULL);
24263   mhalf = CONST_DOUBLE_FROM_REAL_VALUE (r, SFmode);
24264
24265   if (VECTOR_MODE_P (mode))
24266     {
24267       mthree = ix86_build_const_vector (SFmode, true, mthree);
24268       mhalf = ix86_build_const_vector (SFmode, true, mhalf);
24269     }
24270
24271   /* sqrt(a)  = -0.5 * a * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0)
24272      rsqrt(a) = -0.5     * rsqrtss(a) * (a * rsqrtss(a) * rsqrtss(a) - 3.0) */
24273
24274   /* x0 = rsqrt(a) estimate */
24275   emit_insn (gen_rtx_SET (VOIDmode, x0,
24276                           gen_rtx_UNSPEC (mode, gen_rtvec (1, a),
24277                                           UNSPEC_RSQRT)));
24278
24279   /* If (a == 0.0) Filter out infinity to prevent NaN for sqrt(0.0).  */
24280   if (!recip)
24281     {
24282       rtx zero, mask;
24283
24284       zero = gen_reg_rtx (mode);
24285       mask = gen_reg_rtx (mode);
24286
24287       zero = force_reg (mode, CONST0_RTX(mode));
24288       emit_insn (gen_rtx_SET (VOIDmode, mask,
24289                               gen_rtx_NE (mode, zero, a)));
24290
24291       emit_insn (gen_rtx_SET (VOIDmode, x0,
24292                               gen_rtx_AND (mode, x0, mask)));
24293     }
24294
24295   /* e0 = x0 * a */
24296   emit_insn (gen_rtx_SET (VOIDmode, e0,
24297                           gen_rtx_MULT (mode, x0, a)));
24298   /* e1 = e0 * x0 */
24299   emit_insn (gen_rtx_SET (VOIDmode, e1,
24300                           gen_rtx_MULT (mode, e0, x0)));
24301
24302   /* e2 = e1 - 3. */
24303   mthree = force_reg (mode, mthree);
24304   emit_insn (gen_rtx_SET (VOIDmode, e2,
24305                           gen_rtx_PLUS (mode, e1, mthree)));
24306
24307   mhalf = force_reg (mode, mhalf);
24308   if (recip)
24309     /* e3 = -.5 * x0 */
24310     emit_insn (gen_rtx_SET (VOIDmode, e3,
24311                             gen_rtx_MULT (mode, x0, mhalf)));
24312   else
24313     /* e3 = -.5 * e0 */
24314     emit_insn (gen_rtx_SET (VOIDmode, e3,
24315                             gen_rtx_MULT (mode, e0, mhalf)));
24316   /* ret = e2 * e3 */
24317   emit_insn (gen_rtx_SET (VOIDmode, res,
24318                           gen_rtx_MULT (mode, e2, e3)));
24319 }
24320
24321 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
24322
24323 static void ATTRIBUTE_UNUSED
24324 i386_solaris_elf_named_section (const char *name, unsigned int flags,
24325                                 tree decl)
24326 {
24327   /* With Binutils 2.15, the "@unwind" marker must be specified on
24328      every occurrence of the ".eh_frame" section, not just the first
24329      one.  */
24330   if (TARGET_64BIT
24331       && strcmp (name, ".eh_frame") == 0)
24332     {
24333       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
24334                flags & SECTION_WRITE ? "aw" : "a");
24335       return;
24336     }
24337   default_elf_asm_named_section (name, flags, decl);
24338 }
24339
24340 /* Return the mangling of TYPE if it is an extended fundamental type.  */
24341
24342 static const char *
24343 ix86_mangle_type (const_tree type)
24344 {
24345   type = TYPE_MAIN_VARIANT (type);
24346
24347   if (TREE_CODE (type) != VOID_TYPE && TREE_CODE (type) != BOOLEAN_TYPE
24348       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
24349     return NULL;
24350
24351   switch (TYPE_MODE (type))
24352     {
24353     case TFmode:
24354       /* __float128 is "g".  */
24355       return "g";
24356     case XFmode:
24357       /* "long double" or __float80 is "e".  */
24358       return "e";
24359     default:
24360       return NULL;
24361     }
24362 }
24363
24364 /* For 32-bit code we can save PIC register setup by using
24365    __stack_chk_fail_local hidden function instead of calling
24366    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
24367    register, so it is better to call __stack_chk_fail directly.  */
24368
24369 static tree
24370 ix86_stack_protect_fail (void)
24371 {
24372   return TARGET_64BIT
24373          ? default_external_stack_protect_fail ()
24374          : default_hidden_stack_protect_fail ();
24375 }
24376
24377 /* Select a format to encode pointers in exception handling data.  CODE
24378    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
24379    true if the symbol may be affected by dynamic relocations.
24380
24381    ??? All x86 object file formats are capable of representing this.
24382    After all, the relocation needed is the same as for the call insn.
24383    Whether or not a particular assembler allows us to enter such, I
24384    guess we'll have to see.  */
24385 int
24386 asm_preferred_eh_data_format (int code, int global)
24387 {
24388   if (flag_pic)
24389     {
24390       int type = DW_EH_PE_sdata8;
24391       if (!TARGET_64BIT
24392           || ix86_cmodel == CM_SMALL_PIC
24393           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
24394         type = DW_EH_PE_sdata4;
24395       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
24396     }
24397   if (ix86_cmodel == CM_SMALL
24398       || (ix86_cmodel == CM_MEDIUM && code))
24399     return DW_EH_PE_udata4;
24400   return DW_EH_PE_absptr;
24401 }
24402 \f
24403 /* Expand copysign from SIGN to the positive value ABS_VALUE
24404    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
24405    the sign-bit.  */
24406 static void
24407 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
24408 {
24409   enum machine_mode mode = GET_MODE (sign);
24410   rtx sgn = gen_reg_rtx (mode);
24411   if (mask == NULL_RTX)
24412     {
24413       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
24414       if (!VECTOR_MODE_P (mode))
24415         {
24416           /* We need to generate a scalar mode mask in this case.  */
24417           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
24418           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
24419           mask = gen_reg_rtx (mode);
24420           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
24421         }
24422     }
24423   else
24424     mask = gen_rtx_NOT (mode, mask);
24425   emit_insn (gen_rtx_SET (VOIDmode, sgn,
24426                           gen_rtx_AND (mode, mask, sign)));
24427   emit_insn (gen_rtx_SET (VOIDmode, result,
24428                           gen_rtx_IOR (mode, abs_value, sgn)));
24429 }
24430
24431 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
24432    mask for masking out the sign-bit is stored in *SMASK, if that is
24433    non-null.  */
24434 static rtx
24435 ix86_expand_sse_fabs (rtx op0, rtx *smask)
24436 {
24437   enum machine_mode mode = GET_MODE (op0);
24438   rtx xa, mask;
24439
24440   xa = gen_reg_rtx (mode);
24441   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
24442   if (!VECTOR_MODE_P (mode))
24443     {
24444       /* We need to generate a scalar mode mask in this case.  */
24445       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
24446       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
24447       mask = gen_reg_rtx (mode);
24448       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
24449     }
24450   emit_insn (gen_rtx_SET (VOIDmode, xa,
24451                           gen_rtx_AND (mode, op0, mask)));
24452
24453   if (smask)
24454     *smask = mask;
24455
24456   return xa;
24457 }
24458
24459 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
24460    swapping the operands if SWAP_OPERANDS is true.  The expanded
24461    code is a forward jump to a newly created label in case the
24462    comparison is true.  The generated label rtx is returned.  */
24463 static rtx
24464 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
24465                                   bool swap_operands)
24466 {
24467   rtx label, tmp;
24468
24469   if (swap_operands)
24470     {
24471       tmp = op0;
24472       op0 = op1;
24473       op1 = tmp;
24474     }
24475
24476   label = gen_label_rtx ();
24477   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
24478   emit_insn (gen_rtx_SET (VOIDmode, tmp,
24479                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
24480   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
24481   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
24482                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
24483   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
24484   JUMP_LABEL (tmp) = label;
24485
24486   return label;
24487 }
24488
24489 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
24490    using comparison code CODE.  Operands are swapped for the comparison if
24491    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
24492 static rtx
24493 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
24494                               bool swap_operands)
24495 {
24496   enum machine_mode mode = GET_MODE (op0);
24497   rtx mask = gen_reg_rtx (mode);
24498
24499   if (swap_operands)
24500     {
24501       rtx tmp = op0;
24502       op0 = op1;
24503       op1 = tmp;
24504     }
24505
24506   if (mode == DFmode)
24507     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
24508                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
24509   else
24510     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
24511                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
24512
24513   return mask;
24514 }
24515
24516 /* Generate and return a rtx of mode MODE for 2**n where n is the number
24517    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
24518 static rtx
24519 ix86_gen_TWO52 (enum machine_mode mode)
24520 {
24521   REAL_VALUE_TYPE TWO52r;
24522   rtx TWO52;
24523
24524   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
24525   TWO52 = const_double_from_real_value (TWO52r, mode);
24526   TWO52 = force_reg (mode, TWO52);
24527
24528   return TWO52;
24529 }
24530
24531 /* Expand SSE sequence for computing lround from OP1 storing
24532    into OP0.  */
24533 void
24534 ix86_expand_lround (rtx op0, rtx op1)
24535 {
24536   /* C code for the stuff we're doing below:
24537        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
24538        return (long)tmp;
24539    */
24540   enum machine_mode mode = GET_MODE (op1);
24541   const struct real_format *fmt;
24542   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
24543   rtx adj;
24544
24545   /* load nextafter (0.5, 0.0) */
24546   fmt = REAL_MODE_FORMAT (mode);
24547   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
24548   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
24549
24550   /* adj = copysign (0.5, op1) */
24551   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
24552   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
24553
24554   /* adj = op1 + adj */
24555   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
24556
24557   /* op0 = (imode)adj */
24558   expand_fix (op0, adj, 0);
24559 }
24560
24561 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
24562    into OPERAND0.  */
24563 void
24564 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
24565 {
24566   /* C code for the stuff we're doing below (for do_floor):
24567         xi = (long)op1;
24568         xi -= (double)xi > op1 ? 1 : 0;
24569         return xi;
24570    */
24571   enum machine_mode fmode = GET_MODE (op1);
24572   enum machine_mode imode = GET_MODE (op0);
24573   rtx ireg, freg, label, tmp;
24574
24575   /* reg = (long)op1 */
24576   ireg = gen_reg_rtx (imode);
24577   expand_fix (ireg, op1, 0);
24578
24579   /* freg = (double)reg */
24580   freg = gen_reg_rtx (fmode);
24581   expand_float (freg, ireg, 0);
24582
24583   /* ireg = (freg > op1) ? ireg - 1 : ireg */
24584   label = ix86_expand_sse_compare_and_jump (UNLE,
24585                                             freg, op1, !do_floor);
24586   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
24587                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
24588   emit_move_insn (ireg, tmp);
24589
24590   emit_label (label);
24591   LABEL_NUSES (label) = 1;
24592
24593   emit_move_insn (op0, ireg);
24594 }
24595
24596 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
24597    result in OPERAND0.  */
24598 void
24599 ix86_expand_rint (rtx operand0, rtx operand1)
24600 {
24601   /* C code for the stuff we're doing below:
24602         xa = fabs (operand1);
24603         if (!isless (xa, 2**52))
24604           return operand1;
24605         xa = xa + 2**52 - 2**52;
24606         return copysign (xa, operand1);
24607    */
24608   enum machine_mode mode = GET_MODE (operand0);
24609   rtx res, xa, label, TWO52, mask;
24610
24611   res = gen_reg_rtx (mode);
24612   emit_move_insn (res, operand1);
24613
24614   /* xa = abs (operand1) */
24615   xa = ix86_expand_sse_fabs (res, &mask);
24616
24617   /* if (!isless (xa, TWO52)) goto label; */
24618   TWO52 = ix86_gen_TWO52 (mode);
24619   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24620
24621   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
24622   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
24623
24624   ix86_sse_copysign_to_positive (res, xa, res, mask);
24625
24626   emit_label (label);
24627   LABEL_NUSES (label) = 1;
24628
24629   emit_move_insn (operand0, res);
24630 }
24631
24632 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
24633    into OPERAND0.  */
24634 void
24635 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
24636 {
24637   /* C code for the stuff we expand below.
24638         double xa = fabs (x), x2;
24639         if (!isless (xa, TWO52))
24640           return x;
24641         xa = xa + TWO52 - TWO52;
24642         x2 = copysign (xa, x);
24643      Compensate.  Floor:
24644         if (x2 > x)
24645           x2 -= 1;
24646      Compensate.  Ceil:
24647         if (x2 < x)
24648           x2 -= -1;
24649         return x2;
24650    */
24651   enum machine_mode mode = GET_MODE (operand0);
24652   rtx xa, TWO52, tmp, label, one, res, mask;
24653
24654   TWO52 = ix86_gen_TWO52 (mode);
24655
24656   /* Temporary for holding the result, initialized to the input
24657      operand to ease control flow.  */
24658   res = gen_reg_rtx (mode);
24659   emit_move_insn (res, operand1);
24660
24661   /* xa = abs (operand1) */
24662   xa = ix86_expand_sse_fabs (res, &mask);
24663
24664   /* if (!isless (xa, TWO52)) goto label; */
24665   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24666
24667   /* xa = xa + TWO52 - TWO52; */
24668   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
24669   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
24670
24671   /* xa = copysign (xa, operand1) */
24672   ix86_sse_copysign_to_positive (xa, xa, res, mask);
24673
24674   /* generate 1.0 or -1.0 */
24675   one = force_reg (mode,
24676                    const_double_from_real_value (do_floor
24677                                                  ? dconst1 : dconstm1, mode));
24678
24679   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
24680   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
24681   emit_insn (gen_rtx_SET (VOIDmode, tmp,
24682                           gen_rtx_AND (mode, one, tmp)));
24683   /* We always need to subtract here to preserve signed zero.  */
24684   tmp = expand_simple_binop (mode, MINUS,
24685                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
24686   emit_move_insn (res, tmp);
24687
24688   emit_label (label);
24689   LABEL_NUSES (label) = 1;
24690
24691   emit_move_insn (operand0, res);
24692 }
24693
24694 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
24695    into OPERAND0.  */
24696 void
24697 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
24698 {
24699   /* C code for the stuff we expand below.
24700         double xa = fabs (x), x2;
24701         if (!isless (xa, TWO52))
24702           return x;
24703         x2 = (double)(long)x;
24704      Compensate.  Floor:
24705         if (x2 > x)
24706           x2 -= 1;
24707      Compensate.  Ceil:
24708         if (x2 < x)
24709           x2 += 1;
24710         if (HONOR_SIGNED_ZEROS (mode))
24711           return copysign (x2, x);
24712         return x2;
24713    */
24714   enum machine_mode mode = GET_MODE (operand0);
24715   rtx xa, xi, TWO52, tmp, label, one, res, mask;
24716
24717   TWO52 = ix86_gen_TWO52 (mode);
24718
24719   /* Temporary for holding the result, initialized to the input
24720      operand to ease control flow.  */
24721   res = gen_reg_rtx (mode);
24722   emit_move_insn (res, operand1);
24723
24724   /* xa = abs (operand1) */
24725   xa = ix86_expand_sse_fabs (res, &mask);
24726
24727   /* if (!isless (xa, TWO52)) goto label; */
24728   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24729
24730   /* xa = (double)(long)x */
24731   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
24732   expand_fix (xi, res, 0);
24733   expand_float (xa, xi, 0);
24734
24735   /* generate 1.0 */
24736   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
24737
24738   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
24739   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
24740   emit_insn (gen_rtx_SET (VOIDmode, tmp,
24741                           gen_rtx_AND (mode, one, tmp)));
24742   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
24743                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
24744   emit_move_insn (res, tmp);
24745
24746   if (HONOR_SIGNED_ZEROS (mode))
24747     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
24748
24749   emit_label (label);
24750   LABEL_NUSES (label) = 1;
24751
24752   emit_move_insn (operand0, res);
24753 }
24754
24755 /* Expand SSE sequence for computing round from OPERAND1 storing
24756    into OPERAND0.  Sequence that works without relying on DImode truncation
24757    via cvttsd2siq that is only available on 64bit targets.  */
24758 void
24759 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
24760 {
24761   /* C code for the stuff we expand below.
24762         double xa = fabs (x), xa2, x2;
24763         if (!isless (xa, TWO52))
24764           return x;
24765      Using the absolute value and copying back sign makes
24766      -0.0 -> -0.0 correct.
24767         xa2 = xa + TWO52 - TWO52;
24768      Compensate.
24769         dxa = xa2 - xa;
24770         if (dxa <= -0.5)
24771           xa2 += 1;
24772         else if (dxa > 0.5)
24773           xa2 -= 1;
24774         x2 = copysign (xa2, x);
24775         return x2;
24776    */
24777   enum machine_mode mode = GET_MODE (operand0);
24778   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
24779
24780   TWO52 = ix86_gen_TWO52 (mode);
24781
24782   /* Temporary for holding the result, initialized to the input
24783      operand to ease control flow.  */
24784   res = gen_reg_rtx (mode);
24785   emit_move_insn (res, operand1);
24786
24787   /* xa = abs (operand1) */
24788   xa = ix86_expand_sse_fabs (res, &mask);
24789
24790   /* if (!isless (xa, TWO52)) goto label; */
24791   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24792
24793   /* xa2 = xa + TWO52 - TWO52; */
24794   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
24795   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
24796
24797   /* dxa = xa2 - xa; */
24798   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
24799
24800   /* generate 0.5, 1.0 and -0.5 */
24801   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
24802   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
24803   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
24804                                0, OPTAB_DIRECT);
24805
24806   /* Compensate.  */
24807   tmp = gen_reg_rtx (mode);
24808   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
24809   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
24810   emit_insn (gen_rtx_SET (VOIDmode, tmp,
24811                           gen_rtx_AND (mode, one, tmp)));
24812   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
24813   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
24814   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
24815   emit_insn (gen_rtx_SET (VOIDmode, tmp,
24816                           gen_rtx_AND (mode, one, tmp)));
24817   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
24818
24819   /* res = copysign (xa2, operand1) */
24820   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
24821
24822   emit_label (label);
24823   LABEL_NUSES (label) = 1;
24824
24825   emit_move_insn (operand0, res);
24826 }
24827
24828 /* Expand SSE sequence for computing trunc from OPERAND1 storing
24829    into OPERAND0.  */
24830 void
24831 ix86_expand_trunc (rtx operand0, rtx operand1)
24832 {
24833   /* C code for SSE variant we expand below.
24834         double xa = fabs (x), x2;
24835         if (!isless (xa, TWO52))
24836           return x;
24837         x2 = (double)(long)x;
24838         if (HONOR_SIGNED_ZEROS (mode))
24839           return copysign (x2, x);
24840         return x2;
24841    */
24842   enum machine_mode mode = GET_MODE (operand0);
24843   rtx xa, xi, TWO52, label, res, mask;
24844
24845   TWO52 = ix86_gen_TWO52 (mode);
24846
24847   /* Temporary for holding the result, initialized to the input
24848      operand to ease control flow.  */
24849   res = gen_reg_rtx (mode);
24850   emit_move_insn (res, operand1);
24851
24852   /* xa = abs (operand1) */
24853   xa = ix86_expand_sse_fabs (res, &mask);
24854
24855   /* if (!isless (xa, TWO52)) goto label; */
24856   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24857
24858   /* x = (double)(long)x */
24859   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
24860   expand_fix (xi, res, 0);
24861   expand_float (res, xi, 0);
24862
24863   if (HONOR_SIGNED_ZEROS (mode))
24864     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
24865
24866   emit_label (label);
24867   LABEL_NUSES (label) = 1;
24868
24869   emit_move_insn (operand0, res);
24870 }
24871
24872 /* Expand SSE sequence for computing trunc from OPERAND1 storing
24873    into OPERAND0.  */
24874 void
24875 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
24876 {
24877   enum machine_mode mode = GET_MODE (operand0);
24878   rtx xa, mask, TWO52, label, one, res, smask, tmp;
24879
24880   /* C code for SSE variant we expand below.
24881         double xa = fabs (x), x2;
24882         if (!isless (xa, TWO52))
24883           return x;
24884         xa2 = xa + TWO52 - TWO52;
24885      Compensate:
24886         if (xa2 > xa)
24887           xa2 -= 1.0;
24888         x2 = copysign (xa2, x);
24889         return x2;
24890    */
24891
24892   TWO52 = ix86_gen_TWO52 (mode);
24893
24894   /* Temporary for holding the result, initialized to the input
24895      operand to ease control flow.  */
24896   res = gen_reg_rtx (mode);
24897   emit_move_insn (res, operand1);
24898
24899   /* xa = abs (operand1) */
24900   xa = ix86_expand_sse_fabs (res, &smask);
24901
24902   /* if (!isless (xa, TWO52)) goto label; */
24903   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24904
24905   /* res = xa + TWO52 - TWO52; */
24906   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
24907   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
24908   emit_move_insn (res, tmp);
24909
24910   /* generate 1.0 */
24911   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
24912
24913   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
24914   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
24915   emit_insn (gen_rtx_SET (VOIDmode, mask,
24916                           gen_rtx_AND (mode, mask, one)));
24917   tmp = expand_simple_binop (mode, MINUS,
24918                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
24919   emit_move_insn (res, tmp);
24920
24921   /* res = copysign (res, operand1) */
24922   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
24923
24924   emit_label (label);
24925   LABEL_NUSES (label) = 1;
24926
24927   emit_move_insn (operand0, res);
24928 }
24929
24930 /* Expand SSE sequence for computing round from OPERAND1 storing
24931    into OPERAND0.  */
24932 void
24933 ix86_expand_round (rtx operand0, rtx operand1)
24934 {
24935   /* C code for the stuff we're doing below:
24936         double xa = fabs (x);
24937         if (!isless (xa, TWO52))
24938           return x;
24939         xa = (double)(long)(xa + nextafter (0.5, 0.0));
24940         return copysign (xa, x);
24941    */
24942   enum machine_mode mode = GET_MODE (operand0);
24943   rtx res, TWO52, xa, label, xi, half, mask;
24944   const struct real_format *fmt;
24945   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
24946
24947   /* Temporary for holding the result, initialized to the input
24948      operand to ease control flow.  */
24949   res = gen_reg_rtx (mode);
24950   emit_move_insn (res, operand1);
24951
24952   TWO52 = ix86_gen_TWO52 (mode);
24953   xa = ix86_expand_sse_fabs (res, &mask);
24954   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
24955
24956   /* load nextafter (0.5, 0.0) */
24957   fmt = REAL_MODE_FORMAT (mode);
24958   real_2expN (&half_minus_pred_half, -(fmt->p) - 1, mode);
24959   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
24960
24961   /* xa = xa + 0.5 */
24962   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
24963   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
24964
24965   /* xa = (double)(int64_t)xa */
24966   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
24967   expand_fix (xi, xa, 0);
24968   expand_float (xa, xi, 0);
24969
24970   /* res = copysign (xa, operand1) */
24971   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
24972
24973   emit_label (label);
24974   LABEL_NUSES (label) = 1;
24975
24976   emit_move_insn (operand0, res);
24977 }
24978
24979 \f
24980 /* Validate whether a SSE5 instruction is valid or not.
24981    OPERANDS is the array of operands.
24982    NUM is the number of operands.
24983    USES_OC0 is true if the instruction uses OC0 and provides 4 variants.
24984    NUM_MEMORY is the maximum number of memory operands to accept.  */
24985 bool
24986 ix86_sse5_valid_op_p (rtx operands[], rtx insn, int num, bool uses_oc0, int num_memory)
24987 {
24988   int mem_mask;
24989   int mem_count;
24990   int i;
24991
24992   /* Count the number of memory arguments */
24993   mem_mask = 0;
24994   mem_count = 0;
24995   for (i = 0; i < num; i++)
24996     {
24997       enum machine_mode mode = GET_MODE (operands[i]);
24998       if (register_operand (operands[i], mode))
24999         ;
25000
25001       else if (memory_operand (operands[i], mode))
25002         {
25003           mem_mask |= (1 << i);
25004           mem_count++;
25005         }
25006
25007       else
25008         {
25009           rtx pattern = PATTERN (insn);
25010
25011           /* allow 0 for pcmov */
25012           if (GET_CODE (pattern) != SET
25013               || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE
25014               || i < 2
25015               || operands[i] != CONST0_RTX (mode))
25016             return false;
25017         }
25018     }
25019
25020   /* If there were no memory operations, allow the insn */
25021   if (mem_mask == 0)
25022     return true;
25023
25024   /* Do not allow the destination register to be a memory operand.  */
25025   else if (mem_mask & (1 << 0))
25026     return false;
25027
25028   /* If there are too many memory operations, disallow the instruction.  While
25029      the hardware only allows 1 memory reference, before register allocation
25030      for some insns, we allow two memory operations sometimes in order to allow
25031      code like the following to be optimized:
25032
25033         float fmadd (float *a, float *b, float *c) { return (*a * *b) + *c; }
25034
25035     or similar cases that are vectorized into using the fmaddss
25036     instruction.  */
25037   else if (mem_count > num_memory)
25038     return false;
25039
25040   /* Don't allow more than one memory operation if not optimizing.  */
25041   else if (mem_count > 1 && !optimize)
25042     return false;
25043
25044   else if (num == 4 && mem_count == 1)
25045     {
25046       /* formats (destination is the first argument), example fmaddss:
25047          xmm1, xmm1, xmm2, xmm3/mem
25048          xmm1, xmm1, xmm2/mem, xmm3
25049          xmm1, xmm2, xmm3/mem, xmm1
25050          xmm1, xmm2/mem, xmm3, xmm1 */
25051       if (uses_oc0)
25052         return ((mem_mask == (1 << 1))
25053                 || (mem_mask == (1 << 2))
25054                 || (mem_mask == (1 << 3)));
25055
25056       /* format, example pmacsdd:
25057          xmm1, xmm2, xmm3/mem, xmm1 */
25058       else
25059         return (mem_mask == (1 << 2));
25060     }
25061
25062   else if (num == 4 && num_memory == 2)
25063     {
25064       /* If there are two memory operations, we can load one of the memory ops
25065          into the destination register.  This is for optimizing the
25066          multiply/add ops, which the combiner has optimized both the multiply
25067          and the add insns to have a memory operation.  We have to be careful
25068          that the destination doesn't overlap with the inputs.  */
25069       rtx op0 = operands[0];
25070
25071       if (reg_mentioned_p (op0, operands[1])
25072           || reg_mentioned_p (op0, operands[2])
25073           || reg_mentioned_p (op0, operands[3]))
25074         return false;
25075
25076       /* formats (destination is the first argument), example fmaddss:
25077          xmm1, xmm1, xmm2, xmm3/mem
25078          xmm1, xmm1, xmm2/mem, xmm3
25079          xmm1, xmm2, xmm3/mem, xmm1
25080          xmm1, xmm2/mem, xmm3, xmm1
25081
25082          For the oc0 case, we will load either operands[1] or operands[3] into
25083          operands[0], so any combination of 2 memory operands is ok.  */
25084       if (uses_oc0)
25085         return true;
25086
25087       /* format, example pmacsdd:
25088          xmm1, xmm2, xmm3/mem, xmm1
25089
25090          For the integer multiply/add instructions be more restrictive and
25091          require operands[2] and operands[3] to be the memory operands.  */
25092       else
25093         return (mem_mask == ((1 << 2) | (1 << 3)));
25094     }
25095
25096   else if (num == 3 && num_memory == 1)
25097     {
25098       /* formats, example protb:
25099          xmm1, xmm2, xmm3/mem
25100          xmm1, xmm2/mem, xmm3 */
25101       if (uses_oc0)
25102         return ((mem_mask == (1 << 1)) || (mem_mask == (1 << 2)));
25103
25104       /* format, example comeq:
25105          xmm1, xmm2, xmm3/mem */
25106       else
25107         return (mem_mask == (1 << 2));
25108     }
25109
25110   else
25111     gcc_unreachable ();
25112
25113   return false;
25114 }
25115
25116 \f
25117 /* Fixup an SSE5 instruction that has 2 memory input references into a form the
25118    hardware will allow by using the destination register to load one of the
25119    memory operations.  Presently this is used by the multiply/add routines to
25120    allow 2 memory references.  */
25121
25122 void
25123 ix86_expand_sse5_multiple_memory (rtx operands[],
25124                                   int num,
25125                                   enum machine_mode mode)
25126 {
25127   rtx op0 = operands[0];
25128   if (num != 4
25129       || memory_operand (op0, mode)
25130       || reg_mentioned_p (op0, operands[1])
25131       || reg_mentioned_p (op0, operands[2])
25132       || reg_mentioned_p (op0, operands[3]))
25133     gcc_unreachable ();
25134
25135   /* For 2 memory operands, pick either operands[1] or operands[3] to move into
25136      the destination register.  */
25137   if (memory_operand (operands[1], mode))
25138     {
25139       emit_move_insn (op0, operands[1]);
25140       operands[1] = op0;
25141     }
25142   else if (memory_operand (operands[3], mode))
25143     {
25144       emit_move_insn (op0, operands[3]);
25145       operands[3] = op0;
25146     }
25147   else
25148     gcc_unreachable ();
25149
25150   return;
25151 }
25152
25153 \f
25154 /* Table of valid machine attributes.  */
25155 static const struct attribute_spec ix86_attribute_table[] =
25156 {
25157   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
25158   /* Stdcall attribute says callee is responsible for popping arguments
25159      if they are not variable.  */
25160   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
25161   /* Fastcall attribute says callee is responsible for popping arguments
25162      if they are not variable.  */
25163   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
25164   /* Cdecl attribute says the callee is a normal C declaration */
25165   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
25166   /* Regparm attribute specifies how many integer arguments are to be
25167      passed in registers.  */
25168   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
25169   /* Sseregparm attribute says we are using x86_64 calling conventions
25170      for FP arguments.  */
25171   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
25172   /* force_align_arg_pointer says this function realigns the stack at entry.  */
25173   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
25174     false, true,  true, ix86_handle_cconv_attribute },
25175 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
25176   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
25177   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
25178   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
25179 #endif
25180   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
25181   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
25182 #ifdef SUBTARGET_ATTRIBUTE_TABLE
25183   SUBTARGET_ATTRIBUTE_TABLE,
25184 #endif
25185   { NULL,        0, 0, false, false, false, NULL }
25186 };
25187
25188 /* Implement targetm.vectorize.builtin_vectorization_cost.  */
25189 static int
25190 x86_builtin_vectorization_cost (bool runtime_test)
25191 {
25192   /* If the branch of the runtime test is taken - i.e. - the vectorized
25193      version is skipped - this incurs a misprediction cost (because the
25194      vectorized version is expected to be the fall-through).  So we subtract
25195      the latency of a mispredicted branch from the costs that are incured
25196      when the vectorized version is executed.
25197
25198      TODO: The values in individual target tables have to be tuned or new
25199      fields may be needed. For eg. on K8, the default branch path is the
25200      not-taken path. If the taken path is predicted correctly, the minimum
25201      penalty of going down the taken-path is 1 cycle. If the taken-path is
25202      not predicted correctly, then the minimum penalty is 10 cycles.  */
25203
25204   if (runtime_test)
25205     {
25206       return (-(ix86_cost->cond_taken_branch_cost));
25207     }
25208   else
25209     return 0;
25210 }
25211
25212 /* Initialize the GCC target structure.  */
25213 #undef TARGET_ATTRIBUTE_TABLE
25214 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
25215 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
25216 #  undef TARGET_MERGE_DECL_ATTRIBUTES
25217 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
25218 #endif
25219
25220 #undef TARGET_COMP_TYPE_ATTRIBUTES
25221 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
25222
25223 #undef TARGET_INIT_BUILTINS
25224 #define TARGET_INIT_BUILTINS ix86_init_builtins
25225 #undef TARGET_EXPAND_BUILTIN
25226 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
25227
25228 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
25229 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION \
25230   ix86_builtin_vectorized_function
25231
25232 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
25233 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_vectorize_builtin_conversion
25234
25235 #undef TARGET_BUILTIN_RECIPROCAL
25236 #define TARGET_BUILTIN_RECIPROCAL ix86_builtin_reciprocal
25237
25238 #undef TARGET_ASM_FUNCTION_EPILOGUE
25239 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
25240
25241 #undef TARGET_ENCODE_SECTION_INFO
25242 #ifndef SUBTARGET_ENCODE_SECTION_INFO
25243 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
25244 #else
25245 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
25246 #endif
25247
25248 #undef TARGET_ASM_OPEN_PAREN
25249 #define TARGET_ASM_OPEN_PAREN ""
25250 #undef TARGET_ASM_CLOSE_PAREN
25251 #define TARGET_ASM_CLOSE_PAREN ""
25252
25253 #undef TARGET_ASM_ALIGNED_HI_OP
25254 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
25255 #undef TARGET_ASM_ALIGNED_SI_OP
25256 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
25257 #ifdef ASM_QUAD
25258 #undef TARGET_ASM_ALIGNED_DI_OP
25259 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
25260 #endif
25261
25262 #undef TARGET_ASM_UNALIGNED_HI_OP
25263 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
25264 #undef TARGET_ASM_UNALIGNED_SI_OP
25265 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
25266 #undef TARGET_ASM_UNALIGNED_DI_OP
25267 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
25268
25269 #undef TARGET_SCHED_ADJUST_COST
25270 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
25271 #undef TARGET_SCHED_ISSUE_RATE
25272 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
25273 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
25274 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
25275   ia32_multipass_dfa_lookahead
25276
25277 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
25278 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
25279
25280 #ifdef HAVE_AS_TLS
25281 #undef TARGET_HAVE_TLS
25282 #define TARGET_HAVE_TLS true
25283 #endif
25284 #undef TARGET_CANNOT_FORCE_CONST_MEM
25285 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
25286 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
25287 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
25288
25289 #undef TARGET_DELEGITIMIZE_ADDRESS
25290 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
25291
25292 #undef TARGET_MS_BITFIELD_LAYOUT_P
25293 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
25294
25295 #if TARGET_MACHO
25296 #undef TARGET_BINDS_LOCAL_P
25297 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
25298 #endif
25299 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
25300 #undef TARGET_BINDS_LOCAL_P
25301 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
25302 #endif
25303
25304 #undef TARGET_ASM_OUTPUT_MI_THUNK
25305 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
25306 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
25307 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
25308
25309 #undef TARGET_ASM_FILE_START
25310 #define TARGET_ASM_FILE_START x86_file_start
25311
25312 #undef TARGET_DEFAULT_TARGET_FLAGS
25313 #define TARGET_DEFAULT_TARGET_FLAGS     \
25314   (TARGET_DEFAULT                       \
25315    | TARGET_SUBTARGET_DEFAULT           \
25316    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
25317
25318 #undef TARGET_HANDLE_OPTION
25319 #define TARGET_HANDLE_OPTION ix86_handle_option
25320
25321 #undef TARGET_RTX_COSTS
25322 #define TARGET_RTX_COSTS ix86_rtx_costs
25323 #undef TARGET_ADDRESS_COST
25324 #define TARGET_ADDRESS_COST ix86_address_cost
25325
25326 #undef TARGET_FIXED_CONDITION_CODE_REGS
25327 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
25328 #undef TARGET_CC_MODES_COMPATIBLE
25329 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
25330
25331 #undef TARGET_MACHINE_DEPENDENT_REORG
25332 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
25333
25334 #undef TARGET_BUILD_BUILTIN_VA_LIST
25335 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
25336
25337 #undef TARGET_EXPAND_BUILTIN_VA_START
25338 #define TARGET_EXPAND_BUILTIN_VA_START ix86_va_start
25339
25340 #undef TARGET_MD_ASM_CLOBBERS
25341 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
25342
25343 #undef TARGET_PROMOTE_PROTOTYPES
25344 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
25345 #undef TARGET_STRUCT_VALUE_RTX
25346 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
25347 #undef TARGET_SETUP_INCOMING_VARARGS
25348 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
25349 #undef TARGET_MUST_PASS_IN_STACK
25350 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
25351 #undef TARGET_PASS_BY_REFERENCE
25352 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
25353 #undef TARGET_INTERNAL_ARG_POINTER
25354 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
25355 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
25356 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
25357 #undef TARGET_STRICT_ARGUMENT_NAMING
25358 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
25359
25360 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
25361 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
25362
25363 #undef TARGET_SCALAR_MODE_SUPPORTED_P
25364 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
25365
25366 #undef TARGET_VECTOR_MODE_SUPPORTED_P
25367 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
25368
25369 #undef TARGET_C_MODE_FOR_SUFFIX
25370 #define TARGET_C_MODE_FOR_SUFFIX ix86_c_mode_for_suffix
25371
25372 #ifdef HAVE_AS_TLS
25373 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
25374 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
25375 #endif
25376
25377 #ifdef SUBTARGET_INSERT_ATTRIBUTES
25378 #undef TARGET_INSERT_ATTRIBUTES
25379 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
25380 #endif
25381
25382 #undef TARGET_MANGLE_TYPE
25383 #define TARGET_MANGLE_TYPE ix86_mangle_type
25384
25385 #undef TARGET_STACK_PROTECT_FAIL
25386 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
25387
25388 #undef TARGET_FUNCTION_VALUE
25389 #define TARGET_FUNCTION_VALUE ix86_function_value
25390
25391 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST
25392 #define TARGET_VECTORIZE_BUILTIN_VECTORIZATION_COST x86_builtin_vectorization_cost
25393
25394 struct gcc_target targetm = TARGET_INITIALIZER;
25395 \f
25396 #include "gt-i386.h"