OSDN Git Service

2007-04-16 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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
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 "tm-constrs.h"
53 #include "params.h"
54
55 #ifndef CHECK_STACK_LIMIT
56 #define CHECK_STACK_LIMIT (-1)
57 #endif
58
59 /* Return index of given mode in mult and division cost tables.  */
60 #define MODE_INDEX(mode)                                        \
61   ((mode) == QImode ? 0                                         \
62    : (mode) == HImode ? 1                                       \
63    : (mode) == SImode ? 2                                       \
64    : (mode) == DImode ? 3                                       \
65    : 4)
66
67 /* Processor costs (relative to an add) */
68 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
69 #define COSTS_N_BYTES(N) ((N) * 2)
70
71 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
72
73 static const
74 struct processor_costs size_cost = {    /* costs for tuning for size */
75   COSTS_N_BYTES (2),                    /* cost of an add instruction */
76   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
77   COSTS_N_BYTES (2),                    /* variable shift costs */
78   COSTS_N_BYTES (3),                    /* constant shift costs */
79   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
80    COSTS_N_BYTES (3),                   /*                               HI */
81    COSTS_N_BYTES (3),                   /*                               SI */
82    COSTS_N_BYTES (3),                   /*                               DI */
83    COSTS_N_BYTES (5)},                  /*                            other */
84   0,                                    /* cost of multiply per each bit set */
85   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
86    COSTS_N_BYTES (3),                   /*                          HI */
87    COSTS_N_BYTES (3),                   /*                          SI */
88    COSTS_N_BYTES (3),                   /*                          DI */
89    COSTS_N_BYTES (5)},                  /*                       other */
90   COSTS_N_BYTES (3),                    /* cost of movsx */
91   COSTS_N_BYTES (3),                    /* cost of movzx */
92   0,                                    /* "large" insn */
93   2,                                    /* MOVE_RATIO */
94   2,                                    /* cost for loading QImode using movzbl */
95   {2, 2, 2},                            /* cost of loading integer registers
96                                            in QImode, HImode and SImode.
97                                            Relative to reg-reg move (2).  */
98   {2, 2, 2},                            /* cost of storing integer registers */
99   2,                                    /* cost of reg,reg fld/fst */
100   {2, 2, 2},                            /* cost of loading fp registers
101                                            in SFmode, DFmode and XFmode */
102   {2, 2, 2},                            /* cost of storing fp registers
103                                            in SFmode, DFmode and XFmode */
104   3,                                    /* cost of moving MMX register */
105   {3, 3},                               /* cost of loading MMX registers
106                                            in SImode and DImode */
107   {3, 3},                               /* cost of storing MMX registers
108                                            in SImode and DImode */
109   3,                                    /* cost of moving SSE register */
110   {3, 3, 3},                            /* cost of loading SSE registers
111                                            in SImode, DImode and TImode */
112   {3, 3, 3},                            /* cost of storing SSE registers
113                                            in SImode, DImode and TImode */
114   3,                                    /* MMX or SSE register to integer */
115   0,                                    /* size of prefetch block */
116   0,                                    /* number of parallel prefetches */
117   2,                                    /* Branch cost */
118   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
119   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
120   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
121   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
122   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
123   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
124   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
125    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
126   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
127    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}}
128 };
129
130 /* Processor costs (relative to an add) */
131 static const
132 struct processor_costs i386_cost = {    /* 386 specific costs */
133   COSTS_N_INSNS (1),                    /* cost of an add instruction */
134   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
135   COSTS_N_INSNS (3),                    /* variable shift costs */
136   COSTS_N_INSNS (2),                    /* constant shift costs */
137   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
138    COSTS_N_INSNS (6),                   /*                               HI */
139    COSTS_N_INSNS (6),                   /*                               SI */
140    COSTS_N_INSNS (6),                   /*                               DI */
141    COSTS_N_INSNS (6)},                  /*                               other */
142   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
143   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
144    COSTS_N_INSNS (23),                  /*                          HI */
145    COSTS_N_INSNS (23),                  /*                          SI */
146    COSTS_N_INSNS (23),                  /*                          DI */
147    COSTS_N_INSNS (23)},                 /*                          other */
148   COSTS_N_INSNS (3),                    /* cost of movsx */
149   COSTS_N_INSNS (2),                    /* cost of movzx */
150   15,                                   /* "large" insn */
151   3,                                    /* MOVE_RATIO */
152   4,                                    /* cost for loading QImode using movzbl */
153   {2, 4, 2},                            /* cost of loading integer registers
154                                            in QImode, HImode and SImode.
155                                            Relative to reg-reg move (2).  */
156   {2, 4, 2},                            /* cost of storing integer registers */
157   2,                                    /* cost of reg,reg fld/fst */
158   {8, 8, 8},                            /* cost of loading fp registers
159                                            in SFmode, DFmode and XFmode */
160   {8, 8, 8},                            /* cost of storing fp registers
161                                            in SFmode, DFmode and XFmode */
162   2,                                    /* cost of moving MMX register */
163   {4, 8},                               /* cost of loading MMX registers
164                                            in SImode and DImode */
165   {4, 8},                               /* cost of storing MMX registers
166                                            in SImode and DImode */
167   2,                                    /* cost of moving SSE register */
168   {4, 8, 16},                           /* cost of loading SSE registers
169                                            in SImode, DImode and TImode */
170   {4, 8, 16},                           /* cost of storing SSE registers
171                                            in SImode, DImode and TImode */
172   3,                                    /* MMX or SSE register to integer */
173   0,                                    /* size of prefetch block */
174   0,                                    /* number of parallel prefetches */
175   1,                                    /* Branch cost */
176   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
177   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
178   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
179   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
180   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
181   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
182   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
183    DUMMY_STRINGOP_ALGS},
184   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
185    DUMMY_STRINGOP_ALGS},
186 };
187
188 static const
189 struct processor_costs i486_cost = {    /* 486 specific costs */
190   COSTS_N_INSNS (1),                    /* cost of an add instruction */
191   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
192   COSTS_N_INSNS (3),                    /* variable shift costs */
193   COSTS_N_INSNS (2),                    /* constant shift costs */
194   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
195    COSTS_N_INSNS (12),                  /*                               HI */
196    COSTS_N_INSNS (12),                  /*                               SI */
197    COSTS_N_INSNS (12),                  /*                               DI */
198    COSTS_N_INSNS (12)},                 /*                               other */
199   1,                                    /* cost of multiply per each bit set */
200   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
201    COSTS_N_INSNS (40),                  /*                          HI */
202    COSTS_N_INSNS (40),                  /*                          SI */
203    COSTS_N_INSNS (40),                  /*                          DI */
204    COSTS_N_INSNS (40)},                 /*                          other */
205   COSTS_N_INSNS (3),                    /* cost of movsx */
206   COSTS_N_INSNS (2),                    /* cost of movzx */
207   15,                                   /* "large" insn */
208   3,                                    /* MOVE_RATIO */
209   4,                                    /* cost for loading QImode using movzbl */
210   {2, 4, 2},                            /* cost of loading integer registers
211                                            in QImode, HImode and SImode.
212                                            Relative to reg-reg move (2).  */
213   {2, 4, 2},                            /* cost of storing integer registers */
214   2,                                    /* cost of reg,reg fld/fst */
215   {8, 8, 8},                            /* cost of loading fp registers
216                                            in SFmode, DFmode and XFmode */
217   {8, 8, 8},                            /* cost of storing fp registers
218                                            in SFmode, DFmode and XFmode */
219   2,                                    /* cost of moving MMX register */
220   {4, 8},                               /* cost of loading MMX registers
221                                            in SImode and DImode */
222   {4, 8},                               /* cost of storing MMX registers
223                                            in SImode and DImode */
224   2,                                    /* cost of moving SSE register */
225   {4, 8, 16},                           /* cost of loading SSE registers
226                                            in SImode, DImode and TImode */
227   {4, 8, 16},                           /* cost of storing SSE registers
228                                            in SImode, DImode and TImode */
229   3,                                    /* MMX or SSE register to integer */
230   0,                                    /* size of prefetch block */
231   0,                                    /* number of parallel prefetches */
232   1,                                    /* Branch cost */
233   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
234   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
235   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
236   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
237   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
238   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
239   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
240    DUMMY_STRINGOP_ALGS},
241   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
242    DUMMY_STRINGOP_ALGS}
243 };
244
245 static const
246 struct processor_costs pentium_cost = {
247   COSTS_N_INSNS (1),                    /* cost of an add instruction */
248   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
249   COSTS_N_INSNS (4),                    /* variable shift costs */
250   COSTS_N_INSNS (1),                    /* constant shift costs */
251   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
252    COSTS_N_INSNS (11),                  /*                               HI */
253    COSTS_N_INSNS (11),                  /*                               SI */
254    COSTS_N_INSNS (11),                  /*                               DI */
255    COSTS_N_INSNS (11)},                 /*                               other */
256   0,                                    /* cost of multiply per each bit set */
257   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
258    COSTS_N_INSNS (25),                  /*                          HI */
259    COSTS_N_INSNS (25),                  /*                          SI */
260    COSTS_N_INSNS (25),                  /*                          DI */
261    COSTS_N_INSNS (25)},                 /*                          other */
262   COSTS_N_INSNS (3),                    /* cost of movsx */
263   COSTS_N_INSNS (2),                    /* cost of movzx */
264   8,                                    /* "large" insn */
265   6,                                    /* MOVE_RATIO */
266   6,                                    /* cost for loading QImode using movzbl */
267   {2, 4, 2},                            /* cost of loading integer registers
268                                            in QImode, HImode and SImode.
269                                            Relative to reg-reg move (2).  */
270   {2, 4, 2},                            /* cost of storing integer registers */
271   2,                                    /* cost of reg,reg fld/fst */
272   {2, 2, 6},                            /* cost of loading fp registers
273                                            in SFmode, DFmode and XFmode */
274   {4, 4, 6},                            /* cost of storing fp registers
275                                            in SFmode, DFmode and XFmode */
276   8,                                    /* cost of moving MMX register */
277   {8, 8},                               /* cost of loading MMX registers
278                                            in SImode and DImode */
279   {8, 8},                               /* cost of storing MMX registers
280                                            in SImode and DImode */
281   2,                                    /* cost of moving SSE register */
282   {4, 8, 16},                           /* cost of loading SSE registers
283                                            in SImode, DImode and TImode */
284   {4, 8, 16},                           /* cost of storing SSE registers
285                                            in SImode, DImode and TImode */
286   3,                                    /* MMX or SSE register to integer */
287   0,                                    /* size of prefetch block */
288   0,                                    /* number of parallel prefetches */
289   2,                                    /* Branch cost */
290   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
291   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
292   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
293   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
294   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
295   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
296   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
297    DUMMY_STRINGOP_ALGS},
298   {{libcall, {{-1, rep_prefix_4_byte}}},
299    DUMMY_STRINGOP_ALGS}
300 };
301
302 static const
303 struct processor_costs pentiumpro_cost = {
304   COSTS_N_INSNS (1),                    /* cost of an add instruction */
305   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
306   COSTS_N_INSNS (1),                    /* variable shift costs */
307   COSTS_N_INSNS (1),                    /* constant shift costs */
308   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
309    COSTS_N_INSNS (4),                   /*                               HI */
310    COSTS_N_INSNS (4),                   /*                               SI */
311    COSTS_N_INSNS (4),                   /*                               DI */
312    COSTS_N_INSNS (4)},                  /*                               other */
313   0,                                    /* cost of multiply per each bit set */
314   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
315    COSTS_N_INSNS (17),                  /*                          HI */
316    COSTS_N_INSNS (17),                  /*                          SI */
317    COSTS_N_INSNS (17),                  /*                          DI */
318    COSTS_N_INSNS (17)},                 /*                          other */
319   COSTS_N_INSNS (1),                    /* cost of movsx */
320   COSTS_N_INSNS (1),                    /* cost of movzx */
321   8,                                    /* "large" insn */
322   6,                                    /* MOVE_RATIO */
323   2,                                    /* cost for loading QImode using movzbl */
324   {4, 4, 4},                            /* cost of loading integer registers
325                                            in QImode, HImode and SImode.
326                                            Relative to reg-reg move (2).  */
327   {2, 2, 2},                            /* cost of storing integer registers */
328   2,                                    /* cost of reg,reg fld/fst */
329   {2, 2, 6},                            /* cost of loading fp registers
330                                            in SFmode, DFmode and XFmode */
331   {4, 4, 6},                            /* cost of storing fp registers
332                                            in SFmode, DFmode and XFmode */
333   2,                                    /* cost of moving MMX register */
334   {2, 2},                               /* cost of loading MMX registers
335                                            in SImode and DImode */
336   {2, 2},                               /* cost of storing MMX registers
337                                            in SImode and DImode */
338   2,                                    /* cost of moving SSE register */
339   {2, 2, 8},                            /* cost of loading SSE registers
340                                            in SImode, DImode and TImode */
341   {2, 2, 8},                            /* cost of storing SSE registers
342                                            in SImode, DImode and TImode */
343   3,                                    /* MMX or SSE register to integer */
344   32,                                   /* size of prefetch block */
345   6,                                    /* number of parallel prefetches */
346   2,                                    /* Branch cost */
347   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
348   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
349   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
350   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
351   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
352   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
353   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes (we ensure
354      the alignment).  For small blocks inline loop is still a noticeable win, for bigger
355      blocks either rep movsl or rep movsb is way to go.  Rep movsb has apparently
356      more expensive startup time in CPU, but after 4K the difference is down in the noise.
357    */
358   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
359                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
360    DUMMY_STRINGOP_ALGS},
361   {{rep_prefix_4_byte, {{1024, unrolled_loop},
362                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
363    DUMMY_STRINGOP_ALGS}
364 };
365
366 static const
367 struct processor_costs geode_cost = {
368   COSTS_N_INSNS (1),                    /* cost of an add instruction */
369   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
370   COSTS_N_INSNS (2),                    /* variable shift costs */
371   COSTS_N_INSNS (1),                    /* constant shift costs */
372   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
373    COSTS_N_INSNS (4),                   /*                               HI */
374    COSTS_N_INSNS (7),                   /*                               SI */
375    COSTS_N_INSNS (7),                   /*                               DI */
376    COSTS_N_INSNS (7)},                  /*                               other */
377   0,                                    /* cost of multiply per each bit set */
378   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
379    COSTS_N_INSNS (23),                  /*                          HI */
380    COSTS_N_INSNS (39),                  /*                          SI */
381    COSTS_N_INSNS (39),                  /*                          DI */
382    COSTS_N_INSNS (39)},                 /*                          other */
383   COSTS_N_INSNS (1),                    /* cost of movsx */
384   COSTS_N_INSNS (1),                    /* cost of movzx */
385   8,                                    /* "large" insn */
386   4,                                    /* MOVE_RATIO */
387   1,                                    /* cost for loading QImode using movzbl */
388   {1, 1, 1},                            /* cost of loading integer registers
389                                            in QImode, HImode and SImode.
390                                            Relative to reg-reg move (2).  */
391   {1, 1, 1},                            /* cost of storing integer registers */
392   1,                                    /* cost of reg,reg fld/fst */
393   {1, 1, 1},                            /* cost of loading fp registers
394                                            in SFmode, DFmode and XFmode */
395   {4, 6, 6},                            /* cost of storing fp registers
396                                            in SFmode, DFmode and XFmode */
397
398   1,                                    /* cost of moving MMX register */
399   {1, 1},                               /* cost of loading MMX registers
400                                            in SImode and DImode */
401   {1, 1},                               /* cost of storing MMX registers
402                                            in SImode and DImode */
403   1,                                    /* cost of moving SSE register */
404   {1, 1, 1},                            /* cost of loading SSE registers
405                                            in SImode, DImode and TImode */
406   {1, 1, 1},                            /* cost of storing SSE registers
407                                            in SImode, DImode and TImode */
408   1,                                    /* MMX or SSE register to integer */
409   32,                                   /* size of prefetch block */
410   1,                                    /* number of parallel prefetches */
411   1,                                    /* Branch cost */
412   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
413   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
414   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
415   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
416   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
417   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
418   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
419    DUMMY_STRINGOP_ALGS},
420   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
421    DUMMY_STRINGOP_ALGS}
422 };
423
424 static const
425 struct processor_costs k6_cost = {
426   COSTS_N_INSNS (1),                    /* cost of an add instruction */
427   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
428   COSTS_N_INSNS (1),                    /* variable shift costs */
429   COSTS_N_INSNS (1),                    /* constant shift costs */
430   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
431    COSTS_N_INSNS (3),                   /*                               HI */
432    COSTS_N_INSNS (3),                   /*                               SI */
433    COSTS_N_INSNS (3),                   /*                               DI */
434    COSTS_N_INSNS (3)},                  /*                               other */
435   0,                                    /* cost of multiply per each bit set */
436   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
437    COSTS_N_INSNS (18),                  /*                          HI */
438    COSTS_N_INSNS (18),                  /*                          SI */
439    COSTS_N_INSNS (18),                  /*                          DI */
440    COSTS_N_INSNS (18)},                 /*                          other */
441   COSTS_N_INSNS (2),                    /* cost of movsx */
442   COSTS_N_INSNS (2),                    /* cost of movzx */
443   8,                                    /* "large" insn */
444   4,                                    /* MOVE_RATIO */
445   3,                                    /* cost for loading QImode using movzbl */
446   {4, 5, 4},                            /* cost of loading integer registers
447                                            in QImode, HImode and SImode.
448                                            Relative to reg-reg move (2).  */
449   {2, 3, 2},                            /* cost of storing integer registers */
450   4,                                    /* cost of reg,reg fld/fst */
451   {6, 6, 6},                            /* cost of loading fp registers
452                                            in SFmode, DFmode and XFmode */
453   {4, 4, 4},                            /* cost of storing fp registers
454                                            in SFmode, DFmode and XFmode */
455   2,                                    /* cost of moving MMX register */
456   {2, 2},                               /* cost of loading MMX registers
457                                            in SImode and DImode */
458   {2, 2},                               /* cost of storing MMX registers
459                                            in SImode and DImode */
460   2,                                    /* cost of moving SSE register */
461   {2, 2, 8},                            /* cost of loading SSE registers
462                                            in SImode, DImode and TImode */
463   {2, 2, 8},                            /* cost of storing SSE registers
464                                            in SImode, DImode and TImode */
465   6,                                    /* MMX or SSE register to integer */
466   32,                                   /* size of prefetch block */
467   1,                                    /* number of parallel prefetches */
468   1,                                    /* Branch cost */
469   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
470   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
471   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
472   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
473   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
474   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
475   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
476    DUMMY_STRINGOP_ALGS},
477   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
478    DUMMY_STRINGOP_ALGS}
479 };
480
481 static const
482 struct processor_costs athlon_cost = {
483   COSTS_N_INSNS (1),                    /* cost of an add instruction */
484   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
485   COSTS_N_INSNS (1),                    /* variable shift costs */
486   COSTS_N_INSNS (1),                    /* constant shift costs */
487   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
488    COSTS_N_INSNS (5),                   /*                               HI */
489    COSTS_N_INSNS (5),                   /*                               SI */
490    COSTS_N_INSNS (5),                   /*                               DI */
491    COSTS_N_INSNS (5)},                  /*                               other */
492   0,                                    /* cost of multiply per each bit set */
493   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
494    COSTS_N_INSNS (26),                  /*                          HI */
495    COSTS_N_INSNS (42),                  /*                          SI */
496    COSTS_N_INSNS (74),                  /*                          DI */
497    COSTS_N_INSNS (74)},                 /*                          other */
498   COSTS_N_INSNS (1),                    /* cost of movsx */
499   COSTS_N_INSNS (1),                    /* cost of movzx */
500   8,                                    /* "large" insn */
501   9,                                    /* MOVE_RATIO */
502   4,                                    /* cost for loading QImode using movzbl */
503   {3, 4, 3},                            /* cost of loading integer registers
504                                            in QImode, HImode and SImode.
505                                            Relative to reg-reg move (2).  */
506   {3, 4, 3},                            /* cost of storing integer registers */
507   4,                                    /* cost of reg,reg fld/fst */
508   {4, 4, 12},                           /* cost of loading fp registers
509                                            in SFmode, DFmode and XFmode */
510   {6, 6, 8},                            /* cost of storing fp registers
511                                            in SFmode, DFmode and XFmode */
512   2,                                    /* cost of moving MMX register */
513   {4, 4},                               /* cost of loading MMX registers
514                                            in SImode and DImode */
515   {4, 4},                               /* cost of storing MMX registers
516                                            in SImode and DImode */
517   2,                                    /* cost of moving SSE register */
518   {4, 4, 6},                            /* cost of loading SSE registers
519                                            in SImode, DImode and TImode */
520   {4, 4, 5},                            /* cost of storing SSE registers
521                                            in SImode, DImode and TImode */
522   5,                                    /* MMX or SSE register to integer */
523   64,                                   /* size of prefetch block */
524   6,                                    /* number of parallel prefetches */
525   5,                                    /* Branch cost */
526   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
527   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
528   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
529   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
530   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
531   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
532   /* For some reason, Athlon deals better with REP prefix (relative to loops)
533      compared to K8. Alignment becomes important after 8 bytes for memcpy and
534      128 bytes for memset.  */
535   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
536    DUMMY_STRINGOP_ALGS},
537   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
538    DUMMY_STRINGOP_ALGS}
539 };
540
541 static const
542 struct processor_costs k8_cost = {
543   COSTS_N_INSNS (1),                    /* cost of an add instruction */
544   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
545   COSTS_N_INSNS (1),                    /* variable shift costs */
546   COSTS_N_INSNS (1),                    /* constant shift costs */
547   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
548    COSTS_N_INSNS (4),                   /*                               HI */
549    COSTS_N_INSNS (3),                   /*                               SI */
550    COSTS_N_INSNS (4),                   /*                               DI */
551    COSTS_N_INSNS (5)},                  /*                               other */
552   0,                                    /* cost of multiply per each bit set */
553   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
554    COSTS_N_INSNS (26),                  /*                          HI */
555    COSTS_N_INSNS (42),                  /*                          SI */
556    COSTS_N_INSNS (74),                  /*                          DI */
557    COSTS_N_INSNS (74)},                 /*                          other */
558   COSTS_N_INSNS (1),                    /* cost of movsx */
559   COSTS_N_INSNS (1),                    /* cost of movzx */
560   8,                                    /* "large" insn */
561   9,                                    /* MOVE_RATIO */
562   4,                                    /* cost for loading QImode using movzbl */
563   {3, 4, 3},                            /* cost of loading integer registers
564                                            in QImode, HImode and SImode.
565                                            Relative to reg-reg move (2).  */
566   {3, 4, 3},                            /* cost of storing integer registers */
567   4,                                    /* cost of reg,reg fld/fst */
568   {4, 4, 12},                           /* cost of loading fp registers
569                                            in SFmode, DFmode and XFmode */
570   {6, 6, 8},                            /* cost of storing fp registers
571                                            in SFmode, DFmode and XFmode */
572   2,                                    /* cost of moving MMX register */
573   {3, 3},                               /* cost of loading MMX registers
574                                            in SImode and DImode */
575   {4, 4},                               /* cost of storing MMX registers
576                                            in SImode and DImode */
577   2,                                    /* cost of moving SSE register */
578   {4, 3, 6},                            /* cost of loading SSE registers
579                                            in SImode, DImode and TImode */
580   {4, 4, 5},                            /* cost of storing SSE registers
581                                            in SImode, DImode and TImode */
582   5,                                    /* MMX or SSE register to integer */
583   64,                                   /* size of prefetch block */
584   /* New AMD processors never drop prefetches; if they cannot be performed
585      immediately, they are queued.  We set number of simultaneous prefetches
586      to a large constant to reflect this (it probably is not a good idea not
587      to limit number of prefetches at all, as their execution also takes some
588      time).  */
589   100,                                  /* number of parallel prefetches */
590   5,                                    /* Branch cost */
591   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
592   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
593   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
594   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
595   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
596   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
597   /* K8 has optimized REP instruction for medium sized blocks, but for very small
598      blocks it is better to use loop. For large blocks, libcall can do
599      nontemporary accesses and beat inline considerably.  */
600   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
601    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
602   {{libcall, {{8, loop}, {24, unrolled_loop},
603               {2048, rep_prefix_4_byte}, {-1, libcall}}},
604    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
605 };
606
607 struct processor_costs amdfam10_cost = {
608   COSTS_N_INSNS (1),                    /* cost of an add instruction */
609   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
610   COSTS_N_INSNS (1),                    /* variable shift costs */
611   COSTS_N_INSNS (1),                    /* constant shift costs */
612   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
613    COSTS_N_INSNS (4),                   /*                               HI */
614    COSTS_N_INSNS (3),                   /*                               SI */
615    COSTS_N_INSNS (4),                   /*                               DI */
616    COSTS_N_INSNS (5)},                  /*                               other */
617   0,                                    /* cost of multiply per each bit set */
618   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
619    COSTS_N_INSNS (35),                  /*                          HI */
620    COSTS_N_INSNS (51),                  /*                          SI */
621    COSTS_N_INSNS (83),                  /*                          DI */
622    COSTS_N_INSNS (83)},                 /*                          other */
623   COSTS_N_INSNS (1),                    /* cost of movsx */
624   COSTS_N_INSNS (1),                    /* cost of movzx */
625   8,                                    /* "large" insn */
626   9,                                    /* MOVE_RATIO */
627   4,                                    /* cost for loading QImode using movzbl */
628   {3, 4, 3},                            /* cost of loading integer registers
629                                            in QImode, HImode and SImode.
630                                            Relative to reg-reg move (2).  */
631   {3, 4, 3},                            /* cost of storing integer registers */
632   4,                                    /* cost of reg,reg fld/fst */
633   {4, 4, 12},                           /* cost of loading fp registers
634                                            in SFmode, DFmode and XFmode */
635   {6, 6, 8},                            /* cost of storing fp registers
636                                            in SFmode, DFmode and XFmode */
637   2,                                    /* cost of moving MMX register */
638   {3, 3},                               /* cost of loading MMX registers
639                                            in SImode and DImode */
640   {4, 4},                               /* cost of storing MMX registers
641                                            in SImode and DImode */
642   2,                                    /* cost of moving SSE register */
643   {4, 4, 3},                            /* cost of loading SSE registers
644                                            in SImode, DImode and TImode */
645   {4, 4, 5},                            /* cost of storing SSE registers
646                                            in SImode, DImode and TImode */
647   3,                                    /* MMX or SSE register to integer */
648                                         /* On K8
649                                             MOVD reg64, xmmreg  Double  FSTORE 4
650                                             MOVD reg32, xmmreg  Double  FSTORE 4
651                                            On AMDFAM10
652                                             MOVD reg64, xmmreg  Double  FADD 3
653                                                                 1/1  1/1
654                                             MOVD reg32, xmmreg  Double  FADD 3
655                                                                 1/1  1/1 */
656   64,                                   /* size of prefetch block */
657   /* New AMD processors never drop prefetches; if they cannot be performed
658      immediately, they are queued.  We set number of simultaneous prefetches
659      to a large constant to reflect this (it probably is not a good idea not
660      to limit number of prefetches at all, as their execution also takes some
661      time).  */
662   100,                                  /* number of parallel prefetches */
663   5,                                    /* Branch cost */
664   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
665   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
666   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
667   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
668   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
669   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
670
671   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
672      very small blocks it is better to use loop. For large blocks, libcall can
673      do nontemporary accesses and beat inline considerably.  */
674   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
675    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
676   {{libcall, {{8, loop}, {24, unrolled_loop},
677               {2048, rep_prefix_4_byte}, {-1, libcall}}},
678    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
679 };
680
681 static const
682 struct processor_costs pentium4_cost = {
683   COSTS_N_INSNS (1),                    /* cost of an add instruction */
684   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
685   COSTS_N_INSNS (4),                    /* variable shift costs */
686   COSTS_N_INSNS (4),                    /* constant shift costs */
687   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
688    COSTS_N_INSNS (15),                  /*                               HI */
689    COSTS_N_INSNS (15),                  /*                               SI */
690    COSTS_N_INSNS (15),                  /*                               DI */
691    COSTS_N_INSNS (15)},                 /*                               other */
692   0,                                    /* cost of multiply per each bit set */
693   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
694    COSTS_N_INSNS (56),                  /*                          HI */
695    COSTS_N_INSNS (56),                  /*                          SI */
696    COSTS_N_INSNS (56),                  /*                          DI */
697    COSTS_N_INSNS (56)},                 /*                          other */
698   COSTS_N_INSNS (1),                    /* cost of movsx */
699   COSTS_N_INSNS (1),                    /* cost of movzx */
700   16,                                   /* "large" insn */
701   6,                                    /* MOVE_RATIO */
702   2,                                    /* cost for loading QImode using movzbl */
703   {4, 5, 4},                            /* cost of loading integer registers
704                                            in QImode, HImode and SImode.
705                                            Relative to reg-reg move (2).  */
706   {2, 3, 2},                            /* cost of storing integer registers */
707   2,                                    /* cost of reg,reg fld/fst */
708   {2, 2, 6},                            /* cost of loading fp registers
709                                            in SFmode, DFmode and XFmode */
710   {4, 4, 6},                            /* cost of storing fp registers
711                                            in SFmode, DFmode and XFmode */
712   2,                                    /* cost of moving MMX register */
713   {2, 2},                               /* cost of loading MMX registers
714                                            in SImode and DImode */
715   {2, 2},                               /* cost of storing MMX registers
716                                            in SImode and DImode */
717   12,                                   /* cost of moving SSE register */
718   {12, 12, 12},                         /* cost of loading SSE registers
719                                            in SImode, DImode and TImode */
720   {2, 2, 8},                            /* cost of storing SSE registers
721                                            in SImode, DImode and TImode */
722   10,                                   /* MMX or SSE register to integer */
723   64,                                   /* size of prefetch block */
724   6,                                    /* number of parallel prefetches */
725   2,                                    /* Branch cost */
726   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
727   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
728   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
729   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
730   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
731   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
732   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
733    DUMMY_STRINGOP_ALGS},
734   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
735    {-1, libcall}}},
736    DUMMY_STRINGOP_ALGS},
737 };
738
739 static const
740 struct processor_costs nocona_cost = {
741   COSTS_N_INSNS (1),                    /* cost of an add instruction */
742   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
743   COSTS_N_INSNS (1),                    /* variable shift costs */
744   COSTS_N_INSNS (1),                    /* constant shift costs */
745   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
746    COSTS_N_INSNS (10),                  /*                               HI */
747    COSTS_N_INSNS (10),                  /*                               SI */
748    COSTS_N_INSNS (10),                  /*                               DI */
749    COSTS_N_INSNS (10)},                 /*                               other */
750   0,                                    /* cost of multiply per each bit set */
751   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
752    COSTS_N_INSNS (66),                  /*                          HI */
753    COSTS_N_INSNS (66),                  /*                          SI */
754    COSTS_N_INSNS (66),                  /*                          DI */
755    COSTS_N_INSNS (66)},                 /*                          other */
756   COSTS_N_INSNS (1),                    /* cost of movsx */
757   COSTS_N_INSNS (1),                    /* cost of movzx */
758   16,                                   /* "large" insn */
759   17,                                   /* MOVE_RATIO */
760   4,                                    /* cost for loading QImode using movzbl */
761   {4, 4, 4},                            /* cost of loading integer registers
762                                            in QImode, HImode and SImode.
763                                            Relative to reg-reg move (2).  */
764   {4, 4, 4},                            /* cost of storing integer registers */
765   3,                                    /* cost of reg,reg fld/fst */
766   {12, 12, 12},                         /* cost of loading fp registers
767                                            in SFmode, DFmode and XFmode */
768   {4, 4, 4},                            /* cost of storing fp registers
769                                            in SFmode, DFmode and XFmode */
770   6,                                    /* cost of moving MMX register */
771   {12, 12},                             /* cost of loading MMX registers
772                                            in SImode and DImode */
773   {12, 12},                             /* cost of storing MMX registers
774                                            in SImode and DImode */
775   6,                                    /* cost of moving SSE register */
776   {12, 12, 12},                         /* cost of loading SSE registers
777                                            in SImode, DImode and TImode */
778   {12, 12, 12},                         /* cost of storing SSE registers
779                                            in SImode, DImode and TImode */
780   8,                                    /* MMX or SSE register to integer */
781   128,                                  /* size of prefetch block */
782   8,                                    /* number of parallel prefetches */
783   1,                                    /* Branch cost */
784   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
785   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
786   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
787   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
788   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
789   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
790   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
791    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
792               {100000, unrolled_loop}, {-1, libcall}}}},
793   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
794    {-1, libcall}}},
795    {libcall, {{24, loop}, {64, unrolled_loop},
796               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
797 };
798
799 static const
800 struct processor_costs core2_cost = {
801   COSTS_N_INSNS (1),                    /* cost of an add instruction */
802   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
803   COSTS_N_INSNS (1),                    /* variable shift costs */
804   COSTS_N_INSNS (1),                    /* constant shift costs */
805   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
806    COSTS_N_INSNS (3),                   /*                               HI */
807    COSTS_N_INSNS (3),                   /*                               SI */
808    COSTS_N_INSNS (3),                   /*                               DI */
809    COSTS_N_INSNS (3)},                  /*                               other */
810   0,                                    /* cost of multiply per each bit set */
811   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
812    COSTS_N_INSNS (22),                  /*                          HI */
813    COSTS_N_INSNS (22),                  /*                          SI */
814    COSTS_N_INSNS (22),                  /*                          DI */
815    COSTS_N_INSNS (22)},                 /*                          other */
816   COSTS_N_INSNS (1),                    /* cost of movsx */
817   COSTS_N_INSNS (1),                    /* cost of movzx */
818   8,                                    /* "large" insn */
819   16,                                   /* MOVE_RATIO */
820   2,                                    /* cost for loading QImode using movzbl */
821   {6, 6, 6},                            /* cost of loading integer registers
822                                            in QImode, HImode and SImode.
823                                            Relative to reg-reg move (2).  */
824   {4, 4, 4},                            /* cost of storing integer registers */
825   2,                                    /* cost of reg,reg fld/fst */
826   {6, 6, 6},                            /* cost of loading fp registers
827                                            in SFmode, DFmode and XFmode */
828   {4, 4, 4},                            /* cost of loading integer registers */
829   2,                                    /* cost of moving MMX register */
830   {6, 6},                               /* cost of loading MMX registers
831                                            in SImode and DImode */
832   {4, 4},                               /* cost of storing MMX registers
833                                            in SImode and DImode */
834   2,                                    /* cost of moving SSE register */
835   {6, 6, 6},                            /* cost of loading SSE registers
836                                            in SImode, DImode and TImode */
837   {4, 4, 4},                            /* cost of storing SSE registers
838                                            in SImode, DImode and TImode */
839   2,                                    /* MMX or SSE register to integer */
840   128,                                  /* size of prefetch block */
841   8,                                    /* number of parallel prefetches */
842   3,                                    /* Branch cost */
843   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
844   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
845   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
846   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
847   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
848   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
849   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
850    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
851               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
852   {{libcall, {{8, loop}, {15, unrolled_loop},
853               {2048, rep_prefix_4_byte}, {-1, libcall}}},
854    {libcall, {{24, loop}, {32, unrolled_loop},
855               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
856 };
857
858 /* Generic64 should produce code tuned for Nocona and K8.  */
859 static const
860 struct processor_costs generic64_cost = {
861   COSTS_N_INSNS (1),                    /* cost of an add instruction */
862   /* On all chips taken into consideration lea is 2 cycles and more.  With
863      this cost however our current implementation of synth_mult results in
864      use of unnecessary temporary registers causing regression on several
865      SPECfp benchmarks.  */
866   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
867   COSTS_N_INSNS (1),                    /* variable shift costs */
868   COSTS_N_INSNS (1),                    /* constant shift costs */
869   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
870    COSTS_N_INSNS (4),                   /*                               HI */
871    COSTS_N_INSNS (3),                   /*                               SI */
872    COSTS_N_INSNS (4),                   /*                               DI */
873    COSTS_N_INSNS (2)},                  /*                               other */
874   0,                                    /* cost of multiply per each bit set */
875   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
876    COSTS_N_INSNS (26),                  /*                          HI */
877    COSTS_N_INSNS (42),                  /*                          SI */
878    COSTS_N_INSNS (74),                  /*                          DI */
879    COSTS_N_INSNS (74)},                 /*                          other */
880   COSTS_N_INSNS (1),                    /* cost of movsx */
881   COSTS_N_INSNS (1),                    /* cost of movzx */
882   8,                                    /* "large" insn */
883   17,                                   /* MOVE_RATIO */
884   4,                                    /* cost for loading QImode using movzbl */
885   {4, 4, 4},                            /* cost of loading integer registers
886                                            in QImode, HImode and SImode.
887                                            Relative to reg-reg move (2).  */
888   {4, 4, 4},                            /* cost of storing integer registers */
889   4,                                    /* cost of reg,reg fld/fst */
890   {12, 12, 12},                         /* cost of loading fp registers
891                                            in SFmode, DFmode and XFmode */
892   {6, 6, 8},                            /* cost of storing fp registers
893                                            in SFmode, DFmode and XFmode */
894   2,                                    /* cost of moving MMX register */
895   {8, 8},                               /* cost of loading MMX registers
896                                            in SImode and DImode */
897   {8, 8},                               /* cost of storing MMX registers
898                                            in SImode and DImode */
899   2,                                    /* cost of moving SSE register */
900   {8, 8, 8},                            /* cost of loading SSE registers
901                                            in SImode, DImode and TImode */
902   {8, 8, 8},                            /* cost of storing SSE registers
903                                            in SImode, DImode and TImode */
904   5,                                    /* MMX or SSE register to integer */
905   64,                                   /* size of prefetch block */
906   6,                                    /* number of parallel prefetches */
907   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
908      is increased to perhaps more appropriate value of 5.  */
909   3,                                    /* Branch cost */
910   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
911   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
912   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
913   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
914   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
915   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
916   {DUMMY_STRINGOP_ALGS,
917    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
918   {DUMMY_STRINGOP_ALGS,
919    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
920 };
921
922 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
923 static const
924 struct processor_costs generic32_cost = {
925   COSTS_N_INSNS (1),                    /* cost of an add instruction */
926   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
927   COSTS_N_INSNS (1),                    /* variable shift costs */
928   COSTS_N_INSNS (1),                    /* constant shift costs */
929   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
930    COSTS_N_INSNS (4),                   /*                               HI */
931    COSTS_N_INSNS (3),                   /*                               SI */
932    COSTS_N_INSNS (4),                   /*                               DI */
933    COSTS_N_INSNS (2)},                  /*                               other */
934   0,                                    /* cost of multiply per each bit set */
935   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
936    COSTS_N_INSNS (26),                  /*                          HI */
937    COSTS_N_INSNS (42),                  /*                          SI */
938    COSTS_N_INSNS (74),                  /*                          DI */
939    COSTS_N_INSNS (74)},                 /*                          other */
940   COSTS_N_INSNS (1),                    /* cost of movsx */
941   COSTS_N_INSNS (1),                    /* cost of movzx */
942   8,                                    /* "large" insn */
943   17,                                   /* MOVE_RATIO */
944   4,                                    /* cost for loading QImode using movzbl */
945   {4, 4, 4},                            /* cost of loading integer registers
946                                            in QImode, HImode and SImode.
947                                            Relative to reg-reg move (2).  */
948   {4, 4, 4},                            /* cost of storing integer registers */
949   4,                                    /* cost of reg,reg fld/fst */
950   {12, 12, 12},                         /* cost of loading fp registers
951                                            in SFmode, DFmode and XFmode */
952   {6, 6, 8},                            /* cost of storing fp registers
953                                            in SFmode, DFmode and XFmode */
954   2,                                    /* cost of moving MMX register */
955   {8, 8},                               /* cost of loading MMX registers
956                                            in SImode and DImode */
957   {8, 8},                               /* cost of storing MMX registers
958                                            in SImode and DImode */
959   2,                                    /* cost of moving SSE register */
960   {8, 8, 8},                            /* cost of loading SSE registers
961                                            in SImode, DImode and TImode */
962   {8, 8, 8},                            /* cost of storing SSE registers
963                                            in SImode, DImode and TImode */
964   5,                                    /* MMX or SSE register to integer */
965   64,                                   /* size of prefetch block */
966   6,                                    /* number of parallel prefetches */
967   3,                                    /* Branch cost */
968   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
969   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
970   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
971   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
972   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
973   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
974   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
975    DUMMY_STRINGOP_ALGS},
976   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
977    DUMMY_STRINGOP_ALGS},
978 };
979
980 const struct processor_costs *ix86_cost = &pentium_cost;
981
982 /* Processor feature/optimization bitmasks.  */
983 #define m_386 (1<<PROCESSOR_I386)
984 #define m_486 (1<<PROCESSOR_I486)
985 #define m_PENT (1<<PROCESSOR_PENTIUM)
986 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
987 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
988 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
989 #define m_CORE2  (1<<PROCESSOR_CORE2)
990
991 #define m_GEODE  (1<<PROCESSOR_GEODE)
992 #define m_K6  (1<<PROCESSOR_K6)
993 #define m_K6_GEODE  (m_K6 | m_GEODE)
994 #define m_K8  (1<<PROCESSOR_K8)
995 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
996 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
997 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
998 #define m_ATHLON_K8_AMDFAM10  (m_K8 | m_ATHLON | m_AMDFAM10)
999
1000 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1001 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1002
1003 /* Generic instruction choice should be common subset of supported CPUs
1004    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1005 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1006
1007 /* Feature tests against the various tunings.  */
1008 unsigned int ix86_tune_features[X86_TUNE_LAST] = {
1009   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1010      negatively, so enabling for Generic64 seems like good code size
1011      tradeoff.  We can't enable it for 32bit generic because it does not
1012      work well with PPro base chips.  */
1013   m_386 | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_CORE2 | m_GENERIC64,
1014
1015   /* X86_TUNE_PUSH_MEMORY */
1016   m_386 | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4
1017   | m_NOCONA | m_CORE2 | m_GENERIC,
1018
1019   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1020   m_486 | m_PENT,
1021
1022   /* X86_TUNE_USE_BIT_TEST */
1023   m_386,
1024
1025   /* X86_TUNE_UNROLL_STRLEN */
1026   m_486 | m_PENT | m_PPRO | m_ATHLON_K8_AMDFAM10 | m_K6 | m_CORE2 | m_GENERIC,
1027
1028   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1029   m_PPRO | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4
1030   | m_NOCONA | m_CORE2 | m_GENERIC,
1031
1032   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1033      on simulation result. But after P4 was made, no performance benefit
1034      was observed with branch hints.  It also increases the code size.
1035      As a result, icc never generates branch hints.  */
1036   0,
1037
1038   /* X86_TUNE_DOUBLE_WITH_ADD */
1039   ~m_386,
1040
1041   /* X86_TUNE_USE_SAHF */
1042   m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_PENT4
1043   | m_NOCONA | m_CORE2 | m_GENERIC,
1044
1045   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1046      partial dependencies.  */
1047   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_PENT4 | m_NOCONA
1048   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1049
1050   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1051      register stalls on Generic32 compilation setting as well.  However
1052      in current implementation the partial register stalls are not eliminated
1053      very well - they can be introduced via subregs synthesized by combine
1054      and can happen in caller/callee saving sequences.  Because this option
1055      pays back little on PPro based chips and is in conflict with partial reg
1056      dependencies used by Athlon/P4 based chips, it is better to leave it off
1057      for generic32 for now.  */
1058   m_PPRO,
1059
1060   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1061   m_CORE2 | m_GENERIC,
1062
1063   /* X86_TUNE_USE_HIMODE_FIOP */
1064   m_386 | m_486 | m_K6_GEODE,
1065
1066   /* X86_TUNE_USE_SIMODE_FIOP */
1067   ~(m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT | m_CORE2 | m_GENERIC),
1068
1069   /* X86_TUNE_USE_MOV0 */
1070   m_K6,
1071
1072   /* X86_TUNE_USE_CLTD */
1073   ~(m_PENT | m_K6 | m_CORE2 | m_GENERIC),
1074
1075   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1076   m_PENT4,
1077
1078   /* X86_TUNE_SPLIT_LONG_MOVES */
1079   m_PPRO,
1080
1081   /* X86_TUNE_READ_MODIFY_WRITE */
1082   ~m_PENT,
1083
1084   /* X86_TUNE_READ_MODIFY */
1085   ~(m_PENT | m_PPRO),
1086
1087   /* X86_TUNE_PROMOTE_QIMODE */
1088   m_K6_GEODE | m_PENT | m_386 | m_486 | m_ATHLON_K8_AMDFAM10 | m_CORE2
1089   | m_GENERIC /* | m_PENT4 ? */,
1090
1091   /* X86_TUNE_FAST_PREFIX */
1092   ~(m_PENT | m_486 | m_386),
1093
1094   /* X86_TUNE_SINGLE_STRINGOP */
1095   m_386 | m_PENT4 | m_NOCONA,
1096
1097   /* X86_TUNE_QIMODE_MATH */
1098   ~0,
1099
1100   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1101      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1102      might be considered for Generic32 if our scheme for avoiding partial
1103      stalls was more effective.  */
1104   ~m_PPRO,
1105
1106   /* X86_TUNE_PROMOTE_QI_REGS */
1107   0,
1108
1109   /* X86_TUNE_PROMOTE_HI_REGS */
1110   m_PPRO,
1111
1112   /* X86_TUNE_ADD_ESP_4: Enable if add/sub is preferred over 1/2 push/pop.  */
1113   m_ATHLON_K8_AMDFAM10 | m_K6_GEODE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1114
1115   /* X86_TUNE_ADD_ESP_8 */
1116   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_K6_GEODE | m_386
1117   | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1118
1119   /* X86_TUNE_SUB_ESP_4 */
1120   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1121
1122   /* X86_TUNE_SUB_ESP_8 */
1123   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_386 | m_486
1124   | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1125
1126   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1127      for DFmode copies */
1128   ~(m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1129     | m_GENERIC | m_GEODE),
1130
1131   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1132   m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1133
1134   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1135      conflict here in between PPro/Pentium4 based chips that thread 128bit
1136      SSE registers as single units versus K8 based chips that divide SSE
1137      registers to two 64bit halves.  This knob promotes all store destinations
1138      to be 128bit to allow register renaming on 128bit SSE units, but usually
1139      results in one extra microop on 64bit SSE units.  Experimental results
1140      shows that disabling this option on P4 brings over 20% SPECfp regression,
1141      while enabling it on K8 brings roughly 2.4% regression that can be partly
1142      masked by careful scheduling of moves.  */
1143   m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC | m_AMDFAM10,
1144
1145   /* X86_TUNE_SSE_UNALIGNED_MOVE_OPTIMAL */
1146   m_AMDFAM10,
1147
1148   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1149      are resolved on SSE register parts instead of whole registers, so we may
1150      maintain just lower part of scalar values in proper format leaving the
1151      upper part undefined.  */
1152   m_ATHLON_K8,
1153
1154   /* X86_TUNE_SSE_TYPELESS_STORES */
1155   m_ATHLON_K8_AMDFAM10,
1156
1157   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1158   m_PPRO | m_PENT4 | m_NOCONA,
1159
1160   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1161   m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1162
1163   /* X86_TUNE_PROLOGUE_USING_MOVE */
1164   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1165
1166   /* X86_TUNE_EPILOGUE_USING_MOVE */
1167   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1168
1169   /* X86_TUNE_SHIFT1 */
1170   ~m_486,
1171
1172   /* X86_TUNE_USE_FFREEP */
1173   m_ATHLON_K8_AMDFAM10,
1174
1175   /* X86_TUNE_INTER_UNIT_MOVES */
1176   ~(m_ATHLON_K8_AMDFAM10 | m_GENERIC),
1177
1178   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1179      than 4 branch instructions in the 16 byte window.  */
1180   m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1181
1182   /* X86_TUNE_SCHEDULE */
1183   m_PPRO | m_ATHLON_K8_AMDFAM10 | m_K6_GEODE | m_PENT | m_CORE2 | m_GENERIC,
1184
1185   /* X86_TUNE_USE_BT */
1186   m_ATHLON_K8_AMDFAM10,
1187
1188   /* X86_TUNE_USE_INCDEC */
1189   ~(m_PENT4 | m_NOCONA | m_GENERIC),
1190
1191   /* X86_TUNE_PAD_RETURNS */
1192   m_ATHLON_K8_AMDFAM10 | m_CORE2 | m_GENERIC,
1193
1194   /* X86_TUNE_EXT_80387_CONSTANTS */
1195   m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC,
1196
1197   /* X86_TUNE_SHORTEN_X87_SSE */
1198   ~m_K8,
1199
1200   /* X86_TUNE_AVOID_VECTOR_DECODE */
1201   m_K8 | m_GENERIC64,
1202
1203   /* X86_TUNE_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1204      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1205   ~(m_386 | m_486),
1206
1207   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1208      vector path on AMD machines.  */
1209   m_K8 | m_GENERIC64 | m_AMDFAM10,
1210
1211   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1212      machines.  */
1213   m_K8 | m_GENERIC64 | m_AMDFAM10,
1214
1215   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1216      than a MOV.  */
1217   m_PENT,
1218
1219   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1220      but one byte longer.  */
1221   m_PENT,
1222
1223   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1224      operand that cannot be represented using a modRM byte.  The XOR
1225      replacement is long decoded, so this split helps here as well.  */
1226   m_K6,
1227 };
1228
1229 /* Feature tests against the various architecture variations.  */
1230 unsigned int ix86_arch_features[X86_ARCH_LAST] = {
1231   /* X86_ARCH_CMOVE */
1232   m_PPRO | m_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA,
1233
1234   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1235   ~m_386,
1236
1237   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1238   ~(m_386 | m_486),
1239
1240   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1241   ~m_386,
1242
1243   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1244   ~m_386,
1245 };
1246
1247 static const unsigned int x86_accumulate_outgoing_args
1248   = m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
1249
1250 static const unsigned int x86_arch_always_fancy_math_387
1251   = m_PENT | m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT4
1252     | m_NOCONA | m_CORE2 | m_GENERIC;
1253
1254 static enum stringop_alg stringop_alg = no_stringop;
1255
1256 /* In case the average insn count for single function invocation is
1257    lower than this constant, emit fast (but longer) prologue and
1258    epilogue code.  */
1259 #define FAST_PROLOGUE_INSN_COUNT 20
1260
1261 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1262 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1263 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1264 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1265
1266 /* Array of the smallest class containing reg number REGNO, indexed by
1267    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1268
1269 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1270 {
1271   /* ax, dx, cx, bx */
1272   AREG, DREG, CREG, BREG,
1273   /* si, di, bp, sp */
1274   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1275   /* FP registers */
1276   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1277   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1278   /* arg pointer */
1279   NON_Q_REGS,
1280   /* flags, fpsr, fpcr, frame */
1281   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1282   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1283   SSE_REGS, SSE_REGS,
1284   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1285   MMX_REGS, MMX_REGS,
1286   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1287   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1288   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1289   SSE_REGS, SSE_REGS,
1290 };
1291
1292 /* The "default" register map used in 32bit mode.  */
1293
1294 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1295 {
1296   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1297   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1298   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1299   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1300   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1301   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1302   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1303 };
1304
1305 static int const x86_64_int_parameter_registers[6] =
1306 {
1307   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
1308   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1309 };
1310
1311 static int const x86_64_ms_abi_int_parameter_registers[4] =
1312 {
1313   2 /*RCX*/, 1 /*RDX*/,
1314   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1315 };
1316
1317 static int const x86_64_int_return_registers[4] =
1318 {
1319   0 /*RAX*/, 1 /*RDX*/, 5 /*RDI*/, 4 /*RSI*/
1320 };
1321
1322 /* The "default" register map used in 64bit mode.  */
1323 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1324 {
1325   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1326   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1327   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1328   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1329   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1330   8,9,10,11,12,13,14,15,                /* extended integer registers */
1331   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1332 };
1333
1334 /* Define the register numbers to be used in Dwarf debugging information.
1335    The SVR4 reference port C compiler uses the following register numbers
1336    in its Dwarf output code:
1337         0 for %eax (gcc regno = 0)
1338         1 for %ecx (gcc regno = 2)
1339         2 for %edx (gcc regno = 1)
1340         3 for %ebx (gcc regno = 3)
1341         4 for %esp (gcc regno = 7)
1342         5 for %ebp (gcc regno = 6)
1343         6 for %esi (gcc regno = 4)
1344         7 for %edi (gcc regno = 5)
1345    The following three DWARF register numbers are never generated by
1346    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1347    believes these numbers have these meanings.
1348         8  for %eip    (no gcc equivalent)
1349         9  for %eflags (gcc regno = 17)
1350         10 for %trapno (no gcc equivalent)
1351    It is not at all clear how we should number the FP stack registers
1352    for the x86 architecture.  If the version of SDB on x86/svr4 were
1353    a bit less brain dead with respect to floating-point then we would
1354    have a precedent to follow with respect to DWARF register numbers
1355    for x86 FP registers, but the SDB on x86/svr4 is so completely
1356    broken with respect to FP registers that it is hardly worth thinking
1357    of it as something to strive for compatibility with.
1358    The version of x86/svr4 SDB I have at the moment does (partially)
1359    seem to believe that DWARF register number 11 is associated with
1360    the x86 register %st(0), but that's about all.  Higher DWARF
1361    register numbers don't seem to be associated with anything in
1362    particular, and even for DWARF regno 11, SDB only seems to under-
1363    stand that it should say that a variable lives in %st(0) (when
1364    asked via an `=' command) if we said it was in DWARF regno 11,
1365    but SDB still prints garbage when asked for the value of the
1366    variable in question (via a `/' command).
1367    (Also note that the labels SDB prints for various FP stack regs
1368    when doing an `x' command are all wrong.)
1369    Note that these problems generally don't affect the native SVR4
1370    C compiler because it doesn't allow the use of -O with -g and
1371    because when it is *not* optimizing, it allocates a memory
1372    location for each floating-point variable, and the memory
1373    location is what gets described in the DWARF AT_location
1374    attribute for the variable in question.
1375    Regardless of the severe mental illness of the x86/svr4 SDB, we
1376    do something sensible here and we use the following DWARF
1377    register numbers.  Note that these are all stack-top-relative
1378    numbers.
1379         11 for %st(0) (gcc regno = 8)
1380         12 for %st(1) (gcc regno = 9)
1381         13 for %st(2) (gcc regno = 10)
1382         14 for %st(3) (gcc regno = 11)
1383         15 for %st(4) (gcc regno = 12)
1384         16 for %st(5) (gcc regno = 13)
1385         17 for %st(6) (gcc regno = 14)
1386         18 for %st(7) (gcc regno = 15)
1387 */
1388 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1389 {
1390   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1391   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1392   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1393   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1394   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1395   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1396   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1397 };
1398
1399 /* Test and compare insns in i386.md store the information needed to
1400    generate branch and scc insns here.  */
1401
1402 rtx ix86_compare_op0 = NULL_RTX;
1403 rtx ix86_compare_op1 = NULL_RTX;
1404 rtx ix86_compare_emitted = NULL_RTX;
1405
1406 /* Size of the register save area.  */
1407 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1408
1409 /* Define the structure for the machine field in struct function.  */
1410
1411 struct stack_local_entry GTY(())
1412 {
1413   unsigned short mode;
1414   unsigned short n;
1415   rtx rtl;
1416   struct stack_local_entry *next;
1417 };
1418
1419 /* Structure describing stack frame layout.
1420    Stack grows downward:
1421
1422    [arguments]
1423                                               <- ARG_POINTER
1424    saved pc
1425
1426    saved frame pointer if frame_pointer_needed
1427                                               <- HARD_FRAME_POINTER
1428    [saved regs]
1429
1430    [padding1]          \
1431                         )
1432    [va_arg registers]  (
1433                         > to_allocate         <- FRAME_POINTER
1434    [frame]             (
1435                         )
1436    [padding2]          /
1437   */
1438 struct ix86_frame
1439 {
1440   int nregs;
1441   int padding1;
1442   int va_arg_size;
1443   HOST_WIDE_INT frame;
1444   int padding2;
1445   int outgoing_arguments_size;
1446   int red_zone_size;
1447
1448   HOST_WIDE_INT to_allocate;
1449   /* The offsets relative to ARG_POINTER.  */
1450   HOST_WIDE_INT frame_pointer_offset;
1451   HOST_WIDE_INT hard_frame_pointer_offset;
1452   HOST_WIDE_INT stack_pointer_offset;
1453
1454   /* When save_regs_using_mov is set, emit prologue using
1455      move instead of push instructions.  */
1456   bool save_regs_using_mov;
1457 };
1458
1459 /* Code model option.  */
1460 enum cmodel ix86_cmodel;
1461 /* Asm dialect.  */
1462 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1463 /* TLS dialects.  */
1464 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1465
1466 /* Which unit we are generating floating point math for.  */
1467 enum fpmath_unit ix86_fpmath;
1468
1469 /* Which cpu are we scheduling for.  */
1470 enum processor_type ix86_tune;
1471
1472 /* Which instruction set architecture to use.  */
1473 enum processor_type ix86_arch;
1474
1475 /* true if sse prefetch instruction is not NOOP.  */
1476 int x86_prefetch_sse;
1477
1478 /* ix86_regparm_string as a number */
1479 static int ix86_regparm;
1480
1481 /* -mstackrealign option */
1482 extern int ix86_force_align_arg_pointer;
1483 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1484
1485 /* Preferred alignment for stack boundary in bits.  */
1486 unsigned int ix86_preferred_stack_boundary;
1487
1488 /* Values 1-5: see jump.c */
1489 int ix86_branch_cost;
1490
1491 /* Variables which are this size or smaller are put in the data/bss
1492    or ldata/lbss sections.  */
1493
1494 int ix86_section_threshold = 65536;
1495
1496 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1497 char internal_label_prefix[16];
1498 int internal_label_prefix_len;
1499
1500 /* Register class used for passing given 64bit part of the argument.
1501    These represent classes as documented by the PS ABI, with the exception
1502    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1503    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1504
1505    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1506    whenever possible (upper half does contain padding).  */
1507 enum x86_64_reg_class
1508   {
1509     X86_64_NO_CLASS,
1510     X86_64_INTEGER_CLASS,
1511     X86_64_INTEGERSI_CLASS,
1512     X86_64_SSE_CLASS,
1513     X86_64_SSESF_CLASS,
1514     X86_64_SSEDF_CLASS,
1515     X86_64_SSEUP_CLASS,
1516     X86_64_X87_CLASS,
1517     X86_64_X87UP_CLASS,
1518     X86_64_COMPLEX_X87_CLASS,
1519     X86_64_MEMORY_CLASS
1520   };
1521 static const char * const x86_64_reg_class_name[] =
1522 {
1523   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1524   "sseup", "x87", "x87up", "cplx87", "no"
1525 };
1526
1527 #define MAX_CLASSES 4
1528
1529 /* Table of constants used by fldpi, fldln2, etc....  */
1530 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1531 static bool ext_80387_constants_init = 0;
1532
1533 \f
1534 static struct machine_function * ix86_init_machine_status (void);
1535 static rtx ix86_function_value (tree, tree, bool);
1536 static int ix86_function_regparm (tree, tree);
1537 static void ix86_compute_frame_layout (struct ix86_frame *);
1538 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1539                                                  rtx, rtx, int);
1540
1541 \f
1542 /* The svr4 ABI for the i386 says that records and unions are returned
1543    in memory.  */
1544 #ifndef DEFAULT_PCC_STRUCT_RETURN
1545 #define DEFAULT_PCC_STRUCT_RETURN 1
1546 #endif
1547
1548 /* Implement TARGET_HANDLE_OPTION.  */
1549
1550 static bool
1551 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1552 {
1553   switch (code)
1554     {
1555     case OPT_m3dnow:
1556       if (!value)
1557         {
1558           target_flags &= ~MASK_3DNOW_A;
1559           target_flags_explicit |= MASK_3DNOW_A;
1560         }
1561       return true;
1562
1563     case OPT_mmmx:
1564       if (!value)
1565         {
1566           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1567           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1568         }
1569       return true;
1570
1571     case OPT_msse:
1572       if (!value)
1573         {
1574           target_flags &= ~(MASK_SSE2 | MASK_SSE3 | MASK_SSSE3
1575                             | MASK_SSE4A);
1576           target_flags_explicit |= (MASK_SSE2 | MASK_SSE3 | MASK_SSSE3
1577                                     | MASK_SSE4A);
1578         }
1579       return true;
1580
1581     case OPT_msse2:
1582       if (!value)
1583         {
1584           target_flags &= ~(MASK_SSE3 | MASK_SSSE3 | MASK_SSE4A);
1585           target_flags_explicit |= MASK_SSE3 | MASK_SSSE3 | MASK_SSE4A;
1586         }
1587       return true;
1588
1589     case OPT_msse3:
1590       if (!value)
1591         {
1592           target_flags &= ~(MASK_SSSE3 | MASK_SSE4A);
1593           target_flags_explicit |= MASK_SSSE3 | MASK_SSE4A;
1594         }
1595       return true;
1596
1597     case OPT_mssse3:
1598       if (!value)
1599         {
1600           target_flags &= ~MASK_SSE4A;
1601           target_flags_explicit |= MASK_SSE4A;
1602         }
1603       return true;
1604
1605     default:
1606       return true;
1607     }
1608 }
1609
1610 /* Sometimes certain combinations of command options do not make
1611    sense on a particular target machine.  You can define a macro
1612    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1613    defined, is executed once just after all the command options have
1614    been parsed.
1615
1616    Don't use this macro to turn on various extra optimizations for
1617    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1618
1619 void
1620 override_options (void)
1621 {
1622   int i;
1623   int ix86_tune_defaulted = 0;
1624   unsigned int ix86_arch_mask, ix86_tune_mask;
1625
1626   /* Comes from final.c -- no real reason to change it.  */
1627 #define MAX_CODE_ALIGN 16
1628
1629   static struct ptt
1630     {
1631       const struct processor_costs *cost;       /* Processor costs */
1632       const int target_enable;                  /* Target flags to enable.  */
1633       const int target_disable;                 /* Target flags to disable.  */
1634       const int align_loop;                     /* Default alignments.  */
1635       const int align_loop_max_skip;
1636       const int align_jump;
1637       const int align_jump_max_skip;
1638       const int align_func;
1639     }
1640   const processor_target_table[PROCESSOR_max] =
1641     {
1642       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1643       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1644       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1645       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1646       {&geode_cost, 0, 0, 0, 0, 0, 0, 0},
1647       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1648       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1649       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1650       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1651       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1652       {&core2_cost, 0, 0, 16, 7, 16, 7, 16},
1653       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1654       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16},
1655       {&amdfam10_cost, 0, 0, 32, 7, 32, 7, 32}
1656     };
1657
1658   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1659   static struct pta
1660     {
1661       const char *const name;           /* processor name or nickname.  */
1662       const enum processor_type processor;
1663       const enum pta_flags
1664         {
1665           PTA_SSE = 1 << 0,
1666           PTA_SSE2 = 1 << 1,
1667           PTA_SSE3 = 1 << 2,
1668           PTA_MMX = 1 << 3,
1669           PTA_PREFETCH_SSE = 1 << 4,
1670           PTA_3DNOW = 1 << 5,
1671           PTA_3DNOW_A = 1 << 6,
1672           PTA_64BIT = 1 << 7,
1673           PTA_SSSE3 = 1 << 8,
1674           PTA_CX16 = 1 << 9,
1675           PTA_POPCNT = 1 << 10,
1676           PTA_ABM = 1 << 11,
1677           PTA_SSE4A = 1 << 12,
1678           PTA_NO_SAHF = 1 << 13
1679         } flags;
1680     }
1681   const processor_alias_table[] =
1682     {
1683       {"i386", PROCESSOR_I386, 0},
1684       {"i486", PROCESSOR_I486, 0},
1685       {"i586", PROCESSOR_PENTIUM, 0},
1686       {"pentium", PROCESSOR_PENTIUM, 0},
1687       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1688       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1689       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1690       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1691       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1692       {"i686", PROCESSOR_PENTIUMPRO, 0},
1693       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1694       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1695       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1696       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1697       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1698       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1699                                        | PTA_MMX | PTA_PREFETCH_SSE},
1700       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1701                                         | PTA_MMX | PTA_PREFETCH_SSE},
1702       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1703                                         | PTA_MMX | PTA_PREFETCH_SSE},
1704       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1705                                         | PTA_MMX | PTA_PREFETCH_SSE
1706                                         | PTA_CX16 | PTA_NO_SAHF},
1707       {"core2", PROCESSOR_CORE2, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_SSSE3
1708                                         | PTA_64BIT | PTA_MMX
1709                                         | PTA_PREFETCH_SSE | PTA_CX16},
1710       {"geode", PROCESSOR_GEODE, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1711                                    | PTA_3DNOW_A},
1712       {"k6", PROCESSOR_K6, PTA_MMX},
1713       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1714       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1715       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1716                                    | PTA_3DNOW_A},
1717       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1718                                          | PTA_3DNOW | PTA_3DNOW_A},
1719       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1720                                     | PTA_3DNOW_A | PTA_SSE},
1721       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1722                                       | PTA_3DNOW_A | PTA_SSE},
1723       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1724                                       | PTA_3DNOW_A | PTA_SSE},
1725       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1726                                | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
1727       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1728                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2
1729                                       | PTA_NO_SAHF},
1730       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1731                                         | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1732                                         | PTA_SSE2 | PTA_NO_SAHF},
1733       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1734                                          | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1735                                          | PTA_SSE2 | PTA_NO_SAHF},
1736       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1737                                           | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1738                                           | PTA_SSE2 | PTA_NO_SAHF},
1739       {"amdfam10", PROCESSOR_AMDFAM10, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1740                                        | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1741                                        | PTA_SSE2 | PTA_SSE3 | PTA_POPCNT
1742                                        | PTA_ABM | PTA_SSE4A | PTA_CX16},
1743       {"barcelona", PROCESSOR_AMDFAM10, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1744                                        | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1745                                        | PTA_SSE2 | PTA_SSE3 | PTA_POPCNT
1746                                        | PTA_ABM | PTA_SSE4A | PTA_CX16},
1747       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1748       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1749     };
1750
1751   int const pta_size = ARRAY_SIZE (processor_alias_table);
1752
1753 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1754   SUBTARGET_OVERRIDE_OPTIONS;
1755 #endif
1756
1757 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1758   SUBSUBTARGET_OVERRIDE_OPTIONS;
1759 #endif
1760
1761   /* -fPIC is the default for x86_64.  */
1762   if (TARGET_MACHO && TARGET_64BIT)
1763     flag_pic = 2;
1764
1765   /* Set the default values for switches whose default depends on TARGET_64BIT
1766      in case they weren't overwritten by command line options.  */
1767   if (TARGET_64BIT)
1768     {
1769       /* Mach-O doesn't support omitting the frame pointer for now.  */
1770       if (flag_omit_frame_pointer == 2)
1771         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1772       if (flag_asynchronous_unwind_tables == 2)
1773         flag_asynchronous_unwind_tables = 1;
1774       if (flag_pcc_struct_return == 2)
1775         flag_pcc_struct_return = 0;
1776     }
1777   else
1778     {
1779       if (flag_omit_frame_pointer == 2)
1780         flag_omit_frame_pointer = 0;
1781       if (flag_asynchronous_unwind_tables == 2)
1782         flag_asynchronous_unwind_tables = 0;
1783       if (flag_pcc_struct_return == 2)
1784         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1785     }
1786
1787   /* Need to check -mtune=generic first.  */
1788   if (ix86_tune_string)
1789     {
1790       if (!strcmp (ix86_tune_string, "generic")
1791           || !strcmp (ix86_tune_string, "i686")
1792           /* As special support for cross compilers we read -mtune=native
1793              as -mtune=generic.  With native compilers we won't see the
1794              -mtune=native, as it was changed by the driver.  */
1795           || !strcmp (ix86_tune_string, "native"))
1796         {
1797           if (TARGET_64BIT)
1798             ix86_tune_string = "generic64";
1799           else
1800             ix86_tune_string = "generic32";
1801         }
1802       else if (!strncmp (ix86_tune_string, "generic", 7))
1803         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1804     }
1805   else
1806     {
1807       if (ix86_arch_string)
1808         ix86_tune_string = ix86_arch_string;
1809       if (!ix86_tune_string)
1810         {
1811           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1812           ix86_tune_defaulted = 1;
1813         }
1814
1815       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1816          need to use a sensible tune option.  */
1817       if (!strcmp (ix86_tune_string, "generic")
1818           || !strcmp (ix86_tune_string, "x86-64")
1819           || !strcmp (ix86_tune_string, "i686"))
1820         {
1821           if (TARGET_64BIT)
1822             ix86_tune_string = "generic64";
1823           else
1824             ix86_tune_string = "generic32";
1825         }
1826     }
1827   if (ix86_stringop_string)
1828     {
1829       if (!strcmp (ix86_stringop_string, "rep_byte"))
1830         stringop_alg = rep_prefix_1_byte;
1831       else if (!strcmp (ix86_stringop_string, "libcall"))
1832         stringop_alg = libcall;
1833       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
1834         stringop_alg = rep_prefix_4_byte;
1835       else if (!strcmp (ix86_stringop_string, "rep_8byte"))
1836         stringop_alg = rep_prefix_8_byte;
1837       else if (!strcmp (ix86_stringop_string, "byte_loop"))
1838         stringop_alg = loop_1_byte;
1839       else if (!strcmp (ix86_stringop_string, "loop"))
1840         stringop_alg = loop;
1841       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
1842         stringop_alg = unrolled_loop;
1843       else
1844         error ("bad value (%s) for -mstringop-strategy= switch", ix86_stringop_string);
1845     }
1846   if (!strcmp (ix86_tune_string, "x86-64"))
1847     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1848              "-mtune=generic instead as appropriate.");
1849
1850   if (!ix86_arch_string)
1851     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1852   if (!strcmp (ix86_arch_string, "generic"))
1853     error ("generic CPU can be used only for -mtune= switch");
1854   if (!strncmp (ix86_arch_string, "generic", 7))
1855     error ("bad value (%s) for -march= switch", ix86_arch_string);
1856
1857   if (ix86_cmodel_string != 0)
1858     {
1859       if (!strcmp (ix86_cmodel_string, "small"))
1860         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1861       else if (!strcmp (ix86_cmodel_string, "medium"))
1862         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1863       else if (!strcmp (ix86_cmodel_string, "large"))
1864         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
1865       else if (flag_pic)
1866         error ("code model %s does not support PIC mode", ix86_cmodel_string);
1867       else if (!strcmp (ix86_cmodel_string, "32"))
1868         ix86_cmodel = CM_32;
1869       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1870         ix86_cmodel = CM_KERNEL;
1871       else
1872         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1873     }
1874   else
1875     {
1876       /* For TARGET_64BIT_MS_ABI, force pic on, in order to enable the
1877          use of rip-relative addressing.  This eliminates fixups that
1878          would otherwise be needed if this object is to be placed in a
1879          DLL, and is essentially just as efficient as direct addressing.  */
1880       if (TARGET_64BIT_MS_ABI)
1881         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
1882       else if (TARGET_64BIT)
1883         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1884       else
1885         ix86_cmodel = CM_32;
1886     }
1887   if (ix86_asm_string != 0)
1888     {
1889       if (! TARGET_MACHO
1890           && !strcmp (ix86_asm_string, "intel"))
1891         ix86_asm_dialect = ASM_INTEL;
1892       else if (!strcmp (ix86_asm_string, "att"))
1893         ix86_asm_dialect = ASM_ATT;
1894       else
1895         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1896     }
1897   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1898     error ("code model %qs not supported in the %s bit mode",
1899            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1900   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1901     sorry ("%i-bit mode not compiled in",
1902            (target_flags & MASK_64BIT) ? 64 : 32);
1903
1904   for (i = 0; i < pta_size; i++)
1905     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1906       {
1907         ix86_arch = processor_alias_table[i].processor;
1908         /* Default cpu tuning to the architecture.  */
1909         ix86_tune = ix86_arch;
1910         if (processor_alias_table[i].flags & PTA_MMX
1911             && !(target_flags_explicit & MASK_MMX))
1912           target_flags |= MASK_MMX;
1913         if (processor_alias_table[i].flags & PTA_3DNOW
1914             && !(target_flags_explicit & MASK_3DNOW))
1915           target_flags |= MASK_3DNOW;
1916         if (processor_alias_table[i].flags & PTA_3DNOW_A
1917             && !(target_flags_explicit & MASK_3DNOW_A))
1918           target_flags |= MASK_3DNOW_A;
1919         if (processor_alias_table[i].flags & PTA_SSE
1920             && !(target_flags_explicit & MASK_SSE))
1921           target_flags |= MASK_SSE;
1922         if (processor_alias_table[i].flags & PTA_SSE2
1923             && !(target_flags_explicit & MASK_SSE2))
1924           target_flags |= MASK_SSE2;
1925         if (processor_alias_table[i].flags & PTA_SSE3
1926             && !(target_flags_explicit & MASK_SSE3))
1927           target_flags |= MASK_SSE3;
1928         if (processor_alias_table[i].flags & PTA_SSSE3
1929             && !(target_flags_explicit & MASK_SSSE3))
1930           target_flags |= MASK_SSSE3;
1931         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1932           x86_prefetch_sse = true;
1933         if (processor_alias_table[i].flags & PTA_CX16)
1934           x86_cmpxchg16b = true;
1935         if (processor_alias_table[i].flags & PTA_POPCNT
1936             && !(target_flags_explicit & MASK_POPCNT))
1937           target_flags |= MASK_POPCNT;
1938         if (processor_alias_table[i].flags & PTA_ABM
1939             && !(target_flags_explicit & MASK_ABM))
1940           target_flags |= MASK_ABM;
1941         if (processor_alias_table[i].flags & PTA_SSE4A
1942             && !(target_flags_explicit & MASK_SSE4A))
1943           target_flags |= MASK_SSE4A;
1944         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF)))
1945           x86_sahf = true;
1946         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1947           error ("CPU you selected does not support x86-64 "
1948                  "instruction set");
1949         break;
1950       }
1951
1952   if (i == pta_size)
1953     error ("bad value (%s) for -march= switch", ix86_arch_string);
1954
1955   ix86_arch_mask = 1u << ix86_arch;
1956   for (i = 0; i < X86_ARCH_LAST; ++i)
1957     ix86_arch_features[i] &= ix86_arch_mask;
1958
1959   for (i = 0; i < pta_size; i++)
1960     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1961       {
1962         ix86_tune = processor_alias_table[i].processor;
1963         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1964           {
1965             if (ix86_tune_defaulted)
1966               {
1967                 ix86_tune_string = "x86-64";
1968                 for (i = 0; i < pta_size; i++)
1969                   if (! strcmp (ix86_tune_string,
1970                                 processor_alias_table[i].name))
1971                     break;
1972                 ix86_tune = processor_alias_table[i].processor;
1973               }
1974             else
1975               error ("CPU you selected does not support x86-64 "
1976                      "instruction set");
1977           }
1978         /* Intel CPUs have always interpreted SSE prefetch instructions as
1979            NOPs; so, we can enable SSE prefetch instructions even when
1980            -mtune (rather than -march) points us to a processor that has them.
1981            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1982            higher processors.  */
1983         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1984           x86_prefetch_sse = true;
1985         break;
1986       }
1987   if (i == pta_size)
1988     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1989
1990   ix86_tune_mask = 1u << ix86_tune;
1991   for (i = 0; i < X86_TUNE_LAST; ++i)
1992     ix86_tune_features[i] &= ix86_tune_mask;
1993
1994   if (optimize_size)
1995     ix86_cost = &size_cost;
1996   else
1997     ix86_cost = processor_target_table[ix86_tune].cost;
1998   target_flags |= processor_target_table[ix86_tune].target_enable;
1999   target_flags &= ~processor_target_table[ix86_tune].target_disable;
2000
2001   /* Arrange to set up i386_stack_locals for all functions.  */
2002   init_machine_status = ix86_init_machine_status;
2003
2004   /* Validate -mregparm= value.  */
2005   if (ix86_regparm_string)
2006     {
2007       if (TARGET_64BIT)
2008         warning (0, "-mregparm is ignored in 64-bit mode");
2009       i = atoi (ix86_regparm_string);
2010       if (i < 0 || i > REGPARM_MAX)
2011         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
2012       else
2013         ix86_regparm = i;
2014     }
2015   if (TARGET_64BIT)
2016     ix86_regparm = REGPARM_MAX;
2017
2018   /* If the user has provided any of the -malign-* options,
2019      warn and use that value only if -falign-* is not set.
2020      Remove this code in GCC 3.2 or later.  */
2021   if (ix86_align_loops_string)
2022     {
2023       warning (0, "-malign-loops is obsolete, use -falign-loops");
2024       if (align_loops == 0)
2025         {
2026           i = atoi (ix86_align_loops_string);
2027           if (i < 0 || i > MAX_CODE_ALIGN)
2028             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2029           else
2030             align_loops = 1 << i;
2031         }
2032     }
2033
2034   if (ix86_align_jumps_string)
2035     {
2036       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
2037       if (align_jumps == 0)
2038         {
2039           i = atoi (ix86_align_jumps_string);
2040           if (i < 0 || i > MAX_CODE_ALIGN)
2041             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2042           else
2043             align_jumps = 1 << i;
2044         }
2045     }
2046
2047   if (ix86_align_funcs_string)
2048     {
2049       warning (0, "-malign-functions is obsolete, use -falign-functions");
2050       if (align_functions == 0)
2051         {
2052           i = atoi (ix86_align_funcs_string);
2053           if (i < 0 || i > MAX_CODE_ALIGN)
2054             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2055           else
2056             align_functions = 1 << i;
2057         }
2058     }
2059
2060   /* Default align_* from the processor table.  */
2061   if (align_loops == 0)
2062     {
2063       align_loops = processor_target_table[ix86_tune].align_loop;
2064       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
2065     }
2066   if (align_jumps == 0)
2067     {
2068       align_jumps = processor_target_table[ix86_tune].align_jump;
2069       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
2070     }
2071   if (align_functions == 0)
2072     {
2073       align_functions = processor_target_table[ix86_tune].align_func;
2074     }
2075
2076   /* Validate -mbranch-cost= value, or provide default.  */
2077   ix86_branch_cost = ix86_cost->branch_cost;
2078   if (ix86_branch_cost_string)
2079     {
2080       i = atoi (ix86_branch_cost_string);
2081       if (i < 0 || i > 5)
2082         error ("-mbranch-cost=%d is not between 0 and 5", i);
2083       else
2084         ix86_branch_cost = i;
2085     }
2086   if (ix86_section_threshold_string)
2087     {
2088       i = atoi (ix86_section_threshold_string);
2089       if (i < 0)
2090         error ("-mlarge-data-threshold=%d is negative", i);
2091       else
2092         ix86_section_threshold = i;
2093     }
2094
2095   if (ix86_tls_dialect_string)
2096     {
2097       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
2098         ix86_tls_dialect = TLS_DIALECT_GNU;
2099       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
2100         ix86_tls_dialect = TLS_DIALECT_GNU2;
2101       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
2102         ix86_tls_dialect = TLS_DIALECT_SUN;
2103       else
2104         error ("bad value (%s) for -mtls-dialect= switch",
2105                ix86_tls_dialect_string);
2106     }
2107
2108   if (ix87_precision_string)
2109     {
2110       i = atoi (ix87_precision_string);
2111       if (i != 32 && i != 64 && i != 80)
2112         error ("pc%d is not valid precision setting (32, 64 or 80)", i);
2113     }
2114
2115   /* Keep nonleaf frame pointers.  */
2116   if (flag_omit_frame_pointer)
2117     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2118   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
2119     flag_omit_frame_pointer = 1;
2120
2121   /* If we're doing fast math, we don't care about comparison order
2122      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2123   if (flag_finite_math_only)
2124     target_flags &= ~MASK_IEEE_FP;
2125
2126   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2127      since the insns won't need emulation.  */
2128   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
2129     target_flags &= ~MASK_NO_FANCY_MATH_387;
2130
2131   /* Likewise, if the target doesn't have a 387, or we've specified
2132      software floating point, don't use 387 inline intrinsics.  */
2133   if (!TARGET_80387)
2134     target_flags |= MASK_NO_FANCY_MATH_387;
2135
2136   /* Turn on SSE3 builtins for -mssse3.  */
2137   if (TARGET_SSSE3)
2138     target_flags |= MASK_SSE3;
2139
2140   /* Turn on SSE3 builtins for -msse4a.  */
2141   if (TARGET_SSE4A)
2142     target_flags |= MASK_SSE3;
2143
2144   /* Turn on SSE2 builtins for -msse3.  */
2145   if (TARGET_SSE3)
2146     target_flags |= MASK_SSE2;
2147
2148   /* Turn on SSE builtins for -msse2.  */
2149   if (TARGET_SSE2)
2150     target_flags |= MASK_SSE;
2151
2152   /* Turn on MMX builtins for -msse.  */
2153   if (TARGET_SSE)
2154     {
2155       target_flags |= MASK_MMX & ~target_flags_explicit;
2156       x86_prefetch_sse = true;
2157     }
2158
2159   /* Turn on MMX builtins for 3Dnow.  */
2160   if (TARGET_3DNOW)
2161     target_flags |= MASK_MMX;
2162
2163   /* Turn on POPCNT builtins for -mabm.  */
2164   if (TARGET_ABM)
2165     target_flags |= MASK_POPCNT;
2166
2167   if (TARGET_64BIT)
2168     {
2169       if (TARGET_RTD)
2170         warning (0, "-mrtd is ignored in 64bit mode");
2171
2172       /* Enable by default the SSE and MMX builtins.  Do allow the user to
2173          explicitly disable any of these.  In particular, disabling SSE and
2174          MMX for kernel code is extremely useful.  */
2175       target_flags
2176         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | TARGET_SUBTARGET64_DEFAULT)
2177             & ~target_flags_explicit);
2178     }
2179   else
2180     {
2181       /* i386 ABI does not specify red zone.  It still makes sense to use it
2182          when programmer takes care to stack from being destroyed.  */
2183       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
2184         target_flags |= MASK_NO_RED_ZONE;
2185     }
2186
2187   /* Validate -mpreferred-stack-boundary= value, or provide default.
2188      The default of 128 bits is for Pentium III's SSE __m128.  We can't
2189      change it because of optimize_size.  Otherwise, we can't mix object
2190      files compiled with -Os and -On.  */
2191   ix86_preferred_stack_boundary = 128;
2192   if (ix86_preferred_stack_boundary_string)
2193     {
2194       i = atoi (ix86_preferred_stack_boundary_string);
2195       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
2196         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
2197                TARGET_64BIT ? 4 : 2);
2198       else
2199         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
2200     }
2201
2202   /* Accept -msseregparm only if at least SSE support is enabled.  */
2203   if (TARGET_SSEREGPARM
2204       && ! TARGET_SSE)
2205     error ("-msseregparm used without SSE enabled");
2206
2207   ix86_fpmath = TARGET_FPMATH_DEFAULT;
2208   if (ix86_fpmath_string != 0)
2209     {
2210       if (! strcmp (ix86_fpmath_string, "387"))
2211         ix86_fpmath = FPMATH_387;
2212       else if (! strcmp (ix86_fpmath_string, "sse"))
2213         {
2214           if (!TARGET_SSE)
2215             {
2216               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2217               ix86_fpmath = FPMATH_387;
2218             }
2219           else
2220             ix86_fpmath = FPMATH_SSE;
2221         }
2222       else if (! strcmp (ix86_fpmath_string, "387,sse")
2223                || ! strcmp (ix86_fpmath_string, "sse,387"))
2224         {
2225           if (!TARGET_SSE)
2226             {
2227               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2228               ix86_fpmath = FPMATH_387;
2229             }
2230           else if (!TARGET_80387)
2231             {
2232               warning (0, "387 instruction set disabled, using SSE arithmetics");
2233               ix86_fpmath = FPMATH_SSE;
2234             }
2235           else
2236             ix86_fpmath = FPMATH_SSE | FPMATH_387;
2237         }
2238       else
2239         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2240     }
2241
2242   /* If the i387 is disabled, then do not return values in it. */
2243   if (!TARGET_80387)
2244     target_flags &= ~MASK_FLOAT_RETURNS;
2245
2246   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
2247       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2248       && !optimize_size)
2249     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2250
2251   /* ??? Unwind info is not correct around the CFG unless either a frame
2252      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2253      unwind info generation to be aware of the CFG and propagating states
2254      around edges.  */
2255   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2256        || flag_exceptions || flag_non_call_exceptions)
2257       && flag_omit_frame_pointer
2258       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2259     {
2260       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2261         warning (0, "unwind tables currently require either a frame pointer "
2262                  "or -maccumulate-outgoing-args for correctness");
2263       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2264     }
2265
2266   /* For sane SSE instruction set generation we need fcomi instruction.
2267      It is safe to enable all CMOVE instructions.  */
2268   if (TARGET_SSE)
2269     TARGET_CMOVE = 1;
2270
2271   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2272   {
2273     char *p;
2274     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2275     p = strchr (internal_label_prefix, 'X');
2276     internal_label_prefix_len = p - internal_label_prefix;
2277     *p = '\0';
2278   }
2279
2280   /* When scheduling description is not available, disable scheduler pass
2281      so it won't slow down the compilation and make x87 code slower.  */
2282   if (!TARGET_SCHEDULE)
2283     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2284
2285   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
2286     set_param_value ("simultaneous-prefetches",
2287                      ix86_cost->simultaneous_prefetches);
2288   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
2289     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
2290 }
2291 \f
2292 /* Return true if this goes in large data/bss.  */
2293
2294 static bool
2295 ix86_in_large_data_p (tree exp)
2296 {
2297   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
2298     return false;
2299
2300   /* Functions are never large data.  */
2301   if (TREE_CODE (exp) == FUNCTION_DECL)
2302     return false;
2303
2304   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
2305     {
2306       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
2307       if (strcmp (section, ".ldata") == 0
2308           || strcmp (section, ".lbss") == 0)
2309         return true;
2310       return false;
2311     }
2312   else
2313     {
2314       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2315
2316       /* If this is an incomplete type with size 0, then we can't put it
2317          in data because it might be too big when completed.  */
2318       if (!size || size > ix86_section_threshold)
2319         return true;
2320     }
2321
2322   return false;
2323 }
2324
2325 /* Switch to the appropriate section for output of DECL.
2326    DECL is either a `VAR_DECL' node or a constant of some sort.
2327    RELOC indicates whether forming the initial value of DECL requires
2328    link-time relocations.  */
2329
2330 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
2331         ATTRIBUTE_UNUSED;
2332
2333 static section *
2334 x86_64_elf_select_section (tree decl, int reloc,
2335                            unsigned HOST_WIDE_INT align)
2336 {
2337   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2338       && ix86_in_large_data_p (decl))
2339     {
2340       const char *sname = NULL;
2341       unsigned int flags = SECTION_WRITE;
2342       switch (categorize_decl_for_section (decl, reloc))
2343         {
2344         case SECCAT_DATA:
2345           sname = ".ldata";
2346           break;
2347         case SECCAT_DATA_REL:
2348           sname = ".ldata.rel";
2349           break;
2350         case SECCAT_DATA_REL_LOCAL:
2351           sname = ".ldata.rel.local";
2352           break;
2353         case SECCAT_DATA_REL_RO:
2354           sname = ".ldata.rel.ro";
2355           break;
2356         case SECCAT_DATA_REL_RO_LOCAL:
2357           sname = ".ldata.rel.ro.local";
2358           break;
2359         case SECCAT_BSS:
2360           sname = ".lbss";
2361           flags |= SECTION_BSS;
2362           break;
2363         case SECCAT_RODATA:
2364         case SECCAT_RODATA_MERGE_STR:
2365         case SECCAT_RODATA_MERGE_STR_INIT:
2366         case SECCAT_RODATA_MERGE_CONST:
2367           sname = ".lrodata";
2368           flags = 0;
2369           break;
2370         case SECCAT_SRODATA:
2371         case SECCAT_SDATA:
2372         case SECCAT_SBSS:
2373           gcc_unreachable ();
2374         case SECCAT_TEXT:
2375         case SECCAT_TDATA:
2376         case SECCAT_TBSS:
2377           /* We don't split these for medium model.  Place them into
2378              default sections and hope for best.  */
2379           break;
2380         }
2381       if (sname)
2382         {
2383           /* We might get called with string constants, but get_named_section
2384              doesn't like them as they are not DECLs.  Also, we need to set
2385              flags in that case.  */
2386           if (!DECL_P (decl))
2387             return get_section (sname, flags, NULL);
2388           return get_named_section (decl, sname, reloc);
2389         }
2390     }
2391   return default_elf_select_section (decl, reloc, align);
2392 }
2393
2394 /* Build up a unique section name, expressed as a
2395    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2396    RELOC indicates whether the initial value of EXP requires
2397    link-time relocations.  */
2398
2399 static void ATTRIBUTE_UNUSED
2400 x86_64_elf_unique_section (tree decl, int reloc)
2401 {
2402   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2403       && ix86_in_large_data_p (decl))
2404     {
2405       const char *prefix = NULL;
2406       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2407       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2408
2409       switch (categorize_decl_for_section (decl, reloc))
2410         {
2411         case SECCAT_DATA:
2412         case SECCAT_DATA_REL:
2413         case SECCAT_DATA_REL_LOCAL:
2414         case SECCAT_DATA_REL_RO:
2415         case SECCAT_DATA_REL_RO_LOCAL:
2416           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2417           break;
2418         case SECCAT_BSS:
2419           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2420           break;
2421         case SECCAT_RODATA:
2422         case SECCAT_RODATA_MERGE_STR:
2423         case SECCAT_RODATA_MERGE_STR_INIT:
2424         case SECCAT_RODATA_MERGE_CONST:
2425           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2426           break;
2427         case SECCAT_SRODATA:
2428         case SECCAT_SDATA:
2429         case SECCAT_SBSS:
2430           gcc_unreachable ();
2431         case SECCAT_TEXT:
2432         case SECCAT_TDATA:
2433         case SECCAT_TBSS:
2434           /* We don't split these for medium model.  Place them into
2435              default sections and hope for best.  */
2436           break;
2437         }
2438       if (prefix)
2439         {
2440           const char *name;
2441           size_t nlen, plen;
2442           char *string;
2443           plen = strlen (prefix);
2444
2445           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2446           name = targetm.strip_name_encoding (name);
2447           nlen = strlen (name);
2448
2449           string = alloca (nlen + plen + 1);
2450           memcpy (string, prefix, plen);
2451           memcpy (string + plen, name, nlen + 1);
2452
2453           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2454           return;
2455         }
2456     }
2457   default_unique_section (decl, reloc);
2458 }
2459
2460 #ifdef COMMON_ASM_OP
2461 /* This says how to output assembler code to declare an
2462    uninitialized external linkage data object.
2463
2464    For medium model x86-64 we need to use .largecomm opcode for
2465    large objects.  */
2466 void
2467 x86_elf_aligned_common (FILE *file,
2468                         const char *name, unsigned HOST_WIDE_INT size,
2469                         int align)
2470 {
2471   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2472       && size > (unsigned int)ix86_section_threshold)
2473     fprintf (file, ".largecomm\t");
2474   else
2475     fprintf (file, "%s", COMMON_ASM_OP);
2476   assemble_name (file, name);
2477   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2478            size, align / BITS_PER_UNIT);
2479 }
2480 #endif
2481
2482 /* Utility function for targets to use in implementing
2483    ASM_OUTPUT_ALIGNED_BSS.  */
2484
2485 void
2486 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2487                         const char *name, unsigned HOST_WIDE_INT size,
2488                         int align)
2489 {
2490   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2491       && size > (unsigned int)ix86_section_threshold)
2492     switch_to_section (get_named_section (decl, ".lbss", 0));
2493   else
2494     switch_to_section (bss_section);
2495   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2496 #ifdef ASM_DECLARE_OBJECT_NAME
2497   last_assemble_variable_decl = decl;
2498   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2499 #else
2500   /* Standard thing is just output label for the object.  */
2501   ASM_OUTPUT_LABEL (file, name);
2502 #endif /* ASM_DECLARE_OBJECT_NAME */
2503   ASM_OUTPUT_SKIP (file, size ? size : 1);
2504 }
2505 \f
2506 void
2507 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2508 {
2509   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2510      make the problem with not enough registers even worse.  */
2511 #ifdef INSN_SCHEDULING
2512   if (level > 1)
2513     flag_schedule_insns = 0;
2514 #endif
2515
2516   if (TARGET_MACHO)
2517     /* The Darwin libraries never set errno, so we might as well
2518        avoid calling them when that's the only reason we would.  */
2519     flag_errno_math = 0;
2520
2521   /* The default values of these switches depend on the TARGET_64BIT
2522      that is not known at this moment.  Mark these values with 2 and
2523      let user the to override these.  In case there is no command line option
2524      specifying them, we will set the defaults in override_options.  */
2525   if (optimize >= 1)
2526     flag_omit_frame_pointer = 2;
2527   flag_pcc_struct_return = 2;
2528   flag_asynchronous_unwind_tables = 2;
2529 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2530   SUBTARGET_OPTIMIZATION_OPTIONS;
2531 #endif
2532 }
2533 \f
2534 /* Decide whether we can make a sibling call to a function.  DECL is the
2535    declaration of the function being targeted by the call and EXP is the
2536    CALL_EXPR representing the call.  */
2537
2538 static bool
2539 ix86_function_ok_for_sibcall (tree decl, tree exp)
2540 {
2541   tree func;
2542   rtx a, b;
2543
2544   /* If we are generating position-independent code, we cannot sibcall
2545      optimize any indirect call, or a direct call to a global function,
2546      as the PLT requires %ebx be live.  */
2547   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2548     return false;
2549
2550   if (decl)
2551     func = decl;
2552   else
2553     {
2554       func = TREE_TYPE (CALL_EXPR_FN (exp));
2555       if (POINTER_TYPE_P (func))
2556         func = TREE_TYPE (func);
2557     }
2558
2559   /* Check that the return value locations are the same.  Like
2560      if we are returning floats on the 80387 register stack, we cannot
2561      make a sibcall from a function that doesn't return a float to a
2562      function that does or, conversely, from a function that does return
2563      a float to a function that doesn't; the necessary stack adjustment
2564      would not be executed.  This is also the place we notice
2565      differences in the return value ABI.  Note that it is ok for one
2566      of the functions to have void return type as long as the return
2567      value of the other is passed in a register.  */
2568   a = ix86_function_value (TREE_TYPE (exp), func, false);
2569   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2570                            cfun->decl, false);
2571   if (STACK_REG_P (a) || STACK_REG_P (b))
2572     {
2573       if (!rtx_equal_p (a, b))
2574         return false;
2575     }
2576   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2577     ;
2578   else if (!rtx_equal_p (a, b))
2579     return false;
2580
2581   /* If this call is indirect, we'll need to be able to use a call-clobbered
2582      register for the address of the target function.  Make sure that all
2583      such registers are not used for passing parameters.  */
2584   if (!decl && !TARGET_64BIT)
2585     {
2586       tree type;
2587
2588       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2589       type = CALL_EXPR_FN (exp);                /* pointer expression */
2590       type = TREE_TYPE (type);                  /* pointer type */
2591       type = TREE_TYPE (type);                  /* function type */
2592
2593       if (ix86_function_regparm (type, NULL) >= 3)
2594         {
2595           /* ??? Need to count the actual number of registers to be used,
2596              not the possible number of registers.  Fix later.  */
2597           return false;
2598         }
2599     }
2600
2601   /* Dllimport'd functions are also called indirectly.  */
2602   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2603       && decl && DECL_DLLIMPORT_P (decl)
2604       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2605     return false;
2606
2607   /* If we forced aligned the stack, then sibcalling would unalign the
2608      stack, which may break the called function.  */
2609   if (cfun->machine->force_align_arg_pointer)
2610     return false;
2611
2612   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2613   return true;
2614 }
2615
2616 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2617    calling convention attributes;
2618    arguments as in struct attribute_spec.handler.  */
2619
2620 static tree
2621 ix86_handle_cconv_attribute (tree *node, tree name,
2622                                    tree args,
2623                                    int flags ATTRIBUTE_UNUSED,
2624                                    bool *no_add_attrs)
2625 {
2626   if (TREE_CODE (*node) != FUNCTION_TYPE
2627       && TREE_CODE (*node) != METHOD_TYPE
2628       && TREE_CODE (*node) != FIELD_DECL
2629       && TREE_CODE (*node) != TYPE_DECL)
2630     {
2631       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2632                IDENTIFIER_POINTER (name));
2633       *no_add_attrs = true;
2634       return NULL_TREE;
2635     }
2636
2637   /* Can combine regparm with all attributes but fastcall.  */
2638   if (is_attribute_p ("regparm", name))
2639     {
2640       tree cst;
2641
2642       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2643         {
2644           error ("fastcall and regparm attributes are not compatible");
2645         }
2646
2647       cst = TREE_VALUE (args);
2648       if (TREE_CODE (cst) != INTEGER_CST)
2649         {
2650           warning (OPT_Wattributes,
2651                    "%qs attribute requires an integer constant argument",
2652                    IDENTIFIER_POINTER (name));
2653           *no_add_attrs = true;
2654         }
2655       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2656         {
2657           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2658                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2659           *no_add_attrs = true;
2660         }
2661
2662       if (!TARGET_64BIT
2663           && lookup_attribute (ix86_force_align_arg_pointer_string,
2664                                TYPE_ATTRIBUTES (*node))
2665           && compare_tree_int (cst, REGPARM_MAX-1))
2666         {
2667           error ("%s functions limited to %d register parameters",
2668                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2669         }
2670
2671       return NULL_TREE;
2672     }
2673
2674   if (TARGET_64BIT)
2675     {
2676       /* Do not warn when emulating the MS ABI.  */
2677       if (!TARGET_64BIT_MS_ABI)
2678         warning (OPT_Wattributes, "%qs attribute ignored",
2679                  IDENTIFIER_POINTER (name));
2680       *no_add_attrs = true;
2681       return NULL_TREE;
2682     }
2683
2684   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2685   if (is_attribute_p ("fastcall", name))
2686     {
2687       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2688         {
2689           error ("fastcall and cdecl attributes are not compatible");
2690         }
2691       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2692         {
2693           error ("fastcall and stdcall attributes are not compatible");
2694         }
2695       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2696         {
2697           error ("fastcall and regparm attributes are not compatible");
2698         }
2699     }
2700
2701   /* Can combine stdcall with fastcall (redundant), regparm and
2702      sseregparm.  */
2703   else if (is_attribute_p ("stdcall", name))
2704     {
2705       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2706         {
2707           error ("stdcall and cdecl attributes are not compatible");
2708         }
2709       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2710         {
2711           error ("stdcall and fastcall attributes are not compatible");
2712         }
2713     }
2714
2715   /* Can combine cdecl with regparm and sseregparm.  */
2716   else if (is_attribute_p ("cdecl", name))
2717     {
2718       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2719         {
2720           error ("stdcall and cdecl attributes are not compatible");
2721         }
2722       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2723         {
2724           error ("fastcall and cdecl attributes are not compatible");
2725         }
2726     }
2727
2728   /* Can combine sseregparm with all attributes.  */
2729
2730   return NULL_TREE;
2731 }
2732
2733 /* Return 0 if the attributes for two types are incompatible, 1 if they
2734    are compatible, and 2 if they are nearly compatible (which causes a
2735    warning to be generated).  */
2736
2737 static int
2738 ix86_comp_type_attributes (tree type1, tree type2)
2739 {
2740   /* Check for mismatch of non-default calling convention.  */
2741   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2742
2743   if (TREE_CODE (type1) != FUNCTION_TYPE)
2744     return 1;
2745
2746   /* Check for mismatched fastcall/regparm types.  */
2747   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2748        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2749       || (ix86_function_regparm (type1, NULL)
2750           != ix86_function_regparm (type2, NULL)))
2751     return 0;
2752
2753   /* Check for mismatched sseregparm types.  */
2754   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2755       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2756     return 0;
2757
2758   /* Check for mismatched return types (cdecl vs stdcall).  */
2759   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2760       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2761     return 0;
2762
2763   return 1;
2764 }
2765 \f
2766 /* Return the regparm value for a function with the indicated TYPE and DECL.
2767    DECL may be NULL when calling function indirectly
2768    or considering a libcall.  */
2769
2770 static int
2771 ix86_function_regparm (tree type, tree decl)
2772 {
2773   tree attr;
2774   int regparm = ix86_regparm;
2775
2776   if (TARGET_64BIT)
2777     return regparm;
2778
2779   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2780   if (attr)
2781     return TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2782
2783   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2784     return 2;
2785
2786   /* Use register calling convention for local functions when possible.  */
2787   if (decl && flag_unit_at_a_time && !profile_flag)
2788     {
2789       struct cgraph_local_info *i = cgraph_local_info (decl);
2790       if (i && i->local)
2791         {
2792           int local_regparm, globals = 0, regno;
2793           struct function *f;
2794
2795           /* Make sure no regparm register is taken by a
2796              global register variable.  */
2797           for (local_regparm = 0; local_regparm < 3; local_regparm++)
2798             if (global_regs[local_regparm])
2799               break;
2800
2801           /* We can't use regparm(3) for nested functions as these use
2802              static chain pointer in third argument.  */
2803           if (local_regparm == 3
2804               && decl_function_context (decl)
2805               && !DECL_NO_STATIC_CHAIN (decl))
2806             local_regparm = 2;
2807
2808           /* If the function realigns its stackpointer, the prologue will
2809              clobber %ecx.  If we've already generated code for the callee,
2810              the callee DECL_STRUCT_FUNCTION is gone, so we fall back to
2811              scanning the attributes for the self-realigning property.  */
2812           f = DECL_STRUCT_FUNCTION (decl);
2813           if (local_regparm == 3
2814               && (f ? !!f->machine->force_align_arg_pointer
2815                   : !!lookup_attribute (ix86_force_align_arg_pointer_string,
2816                                         TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2817             local_regparm = 2;
2818
2819           /* Each global register variable increases register preassure,
2820              so the more global reg vars there are, the smaller regparm
2821              optimization use, unless requested by the user explicitly.  */
2822           for (regno = 0; regno < 6; regno++)
2823             if (global_regs[regno])
2824               globals++;
2825           local_regparm
2826             = globals < local_regparm ? local_regparm - globals : 0;
2827
2828           if (local_regparm > regparm)
2829             regparm = local_regparm;
2830         }
2831     }
2832
2833   return regparm;
2834 }
2835
2836 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
2837    DFmode (2) arguments in SSE registers for a function with the
2838    indicated TYPE and DECL.  DECL may be NULL when calling function
2839    indirectly or considering a libcall.  Otherwise return 0.  */
2840
2841 static int
2842 ix86_function_sseregparm (tree type, tree decl)
2843 {
2844   gcc_assert (!TARGET_64BIT);
2845
2846   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2847      by the sseregparm attribute.  */
2848   if (TARGET_SSEREGPARM
2849       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2850     {
2851       if (!TARGET_SSE)
2852         {
2853           if (decl)
2854             error ("Calling %qD with attribute sseregparm without "
2855                    "SSE/SSE2 enabled", decl);
2856           else
2857             error ("Calling %qT with attribute sseregparm without "
2858                    "SSE/SSE2 enabled", type);
2859           return 0;
2860         }
2861
2862       return 2;
2863     }
2864
2865   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
2866      (and DFmode for SSE2) arguments in SSE registers.  */
2867   if (decl && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2868     {
2869       struct cgraph_local_info *i = cgraph_local_info (decl);
2870       if (i && i->local)
2871         return TARGET_SSE2 ? 2 : 1;
2872     }
2873
2874   return 0;
2875 }
2876
2877 /* Return true if EAX is live at the start of the function.  Used by
2878    ix86_expand_prologue to determine if we need special help before
2879    calling allocate_stack_worker.  */
2880
2881 static bool
2882 ix86_eax_live_at_start_p (void)
2883 {
2884   /* Cheat.  Don't bother working forward from ix86_function_regparm
2885      to the function type to whether an actual argument is located in
2886      eax.  Instead just look at cfg info, which is still close enough
2887      to correct at this point.  This gives false positives for broken
2888      functions that might use uninitialized data that happens to be
2889      allocated in eax, but who cares?  */
2890   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2891 }
2892
2893 /* Return true if TYPE has a variable argument list.  */
2894
2895 static bool
2896 type_has_variadic_args_p (tree type)
2897 {
2898   tree t;
2899
2900   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
2901     if (t == void_list_node)
2902       return false;
2903   return true;
2904 }
2905
2906 /* Value is the number of bytes of arguments automatically
2907    popped when returning from a subroutine call.
2908    FUNDECL is the declaration node of the function (as a tree),
2909    FUNTYPE is the data type of the function (as a tree),
2910    or for a library call it is an identifier node for the subroutine name.
2911    SIZE is the number of bytes of arguments passed on the stack.
2912
2913    On the 80386, the RTD insn may be used to pop them if the number
2914      of args is fixed, but if the number is variable then the caller
2915      must pop them all.  RTD can't be used for library calls now
2916      because the library is compiled with the Unix compiler.
2917    Use of RTD is a selectable option, since it is incompatible with
2918    standard Unix calling sequences.  If the option is not selected,
2919    the caller must always pop the args.
2920
2921    The attribute stdcall is equivalent to RTD on a per module basis.  */
2922
2923 int
2924 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2925 {
2926   int rtd;
2927
2928   /* None of the 64-bit ABIs pop arguments.  */
2929   if (TARGET_64BIT)
2930     return 0;
2931
2932   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2933
2934   /* Cdecl functions override -mrtd, and never pop the stack.  */
2935   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
2936     {
2937       /* Stdcall and fastcall functions will pop the stack if not
2938          variable args.  */
2939       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2940           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2941         rtd = 1;
2942
2943       if (rtd && ! type_has_variadic_args_p (funtype))
2944         return size;
2945     }
2946
2947   /* Lose any fake structure return argument if it is passed on the stack.  */
2948   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2949       && !KEEP_AGGREGATE_RETURN_POINTER)
2950     {
2951       int nregs = ix86_function_regparm (funtype, fundecl);
2952       if (nregs == 0)
2953         return GET_MODE_SIZE (Pmode);
2954     }
2955
2956   return 0;
2957 }
2958 \f
2959 /* Argument support functions.  */
2960
2961 /* Return true when register may be used to pass function parameters.  */
2962 bool
2963 ix86_function_arg_regno_p (int regno)
2964 {
2965   int i;
2966   const int *parm_regs;
2967
2968   if (!TARGET_64BIT)
2969     {
2970       if (TARGET_MACHO)
2971         return (regno < REGPARM_MAX
2972                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
2973       else
2974         return (regno < REGPARM_MAX
2975                 || (TARGET_MMX && MMX_REGNO_P (regno)
2976                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2977                 || (TARGET_SSE && SSE_REGNO_P (regno)
2978                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2979     }
2980
2981   if (TARGET_MACHO)
2982     {
2983       if (SSE_REGNO_P (regno) && TARGET_SSE)
2984         return true;
2985     }
2986   else
2987     {
2988       if (TARGET_SSE && SSE_REGNO_P (regno)
2989           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2990         return true;
2991     }
2992
2993   /* RAX is used as hidden argument to va_arg functions.  */
2994   if (!TARGET_64BIT_MS_ABI && regno == 0)
2995     return true;
2996
2997   if (TARGET_64BIT_MS_ABI)
2998     parm_regs = x86_64_ms_abi_int_parameter_registers;
2999   else
3000     parm_regs = x86_64_int_parameter_registers;
3001   for (i = 0; i < REGPARM_MAX; i++)
3002     if (regno == parm_regs[i])
3003       return true;
3004   return false;
3005 }
3006
3007 /* Return if we do not know how to pass TYPE solely in registers.  */
3008
3009 static bool
3010 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
3011 {
3012   if (must_pass_in_stack_var_size_or_pad (mode, type))
3013     return true;
3014
3015   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
3016      The layout_type routine is crafty and tries to trick us into passing
3017      currently unsupported vector types on the stack by using TImode.  */
3018   return (!TARGET_64BIT && mode == TImode
3019           && type && TREE_CODE (type) != VECTOR_TYPE);
3020 }
3021
3022 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3023    for a call to a function whose data type is FNTYPE.
3024    For a library call, FNTYPE is 0.  */
3025
3026 void
3027 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
3028                       tree fntype,      /* tree ptr for function decl */
3029                       rtx libname,      /* SYMBOL_REF of library name or 0 */
3030                       tree fndecl)
3031 {
3032   memset (cum, 0, sizeof (*cum));
3033
3034   /* Set up the number of registers to use for passing arguments.  */
3035   cum->nregs = ix86_regparm;
3036   if (TARGET_SSE)
3037     cum->sse_nregs = SSE_REGPARM_MAX;
3038   if (TARGET_MMX)
3039     cum->mmx_nregs = MMX_REGPARM_MAX;
3040   cum->warn_sse = true;
3041   cum->warn_mmx = true;
3042   cum->maybe_vaarg = (fntype ? type_has_variadic_args_p (fntype) : !libname);
3043
3044   if (!TARGET_64BIT)
3045     {
3046       /* If there are variable arguments, then we won't pass anything
3047          in registers in 32-bit mode. */
3048       if (cum->maybe_vaarg)
3049         {
3050           cum->nregs = 0;
3051           cum->sse_nregs = 0;
3052           cum->mmx_nregs = 0;
3053           cum->warn_sse = 0;
3054           cum->warn_mmx = 0;
3055           return;
3056         }
3057
3058       /* Use ecx and edx registers if function has fastcall attribute,
3059          else look for regparm information.  */
3060       if (fntype)
3061         {
3062           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
3063             {
3064               cum->nregs = 2;
3065               cum->fastcall = 1;
3066             }
3067           else
3068             cum->nregs = ix86_function_regparm (fntype, fndecl);
3069         }
3070
3071       /* Set up the number of SSE registers used for passing SFmode
3072          and DFmode arguments.  Warn for mismatching ABI.  */
3073       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
3074     }
3075 }
3076
3077 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
3078    But in the case of vector types, it is some vector mode.
3079
3080    When we have only some of our vector isa extensions enabled, then there
3081    are some modes for which vector_mode_supported_p is false.  For these
3082    modes, the generic vector support in gcc will choose some non-vector mode
3083    in order to implement the type.  By computing the natural mode, we'll
3084    select the proper ABI location for the operand and not depend on whatever
3085    the middle-end decides to do with these vector types.  */
3086
3087 static enum machine_mode
3088 type_natural_mode (tree type)
3089 {
3090   enum machine_mode mode = TYPE_MODE (type);
3091
3092   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
3093     {
3094       HOST_WIDE_INT size = int_size_in_bytes (type);
3095       if ((size == 8 || size == 16)
3096           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
3097           && TYPE_VECTOR_SUBPARTS (type) > 1)
3098         {
3099           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
3100
3101           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
3102             mode = MIN_MODE_VECTOR_FLOAT;
3103           else
3104             mode = MIN_MODE_VECTOR_INT;
3105
3106           /* Get the mode which has this inner mode and number of units.  */
3107           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3108             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
3109                 && GET_MODE_INNER (mode) == innermode)
3110               return mode;
3111
3112           gcc_unreachable ();
3113         }
3114     }
3115
3116   return mode;
3117 }
3118
3119 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
3120    this may not agree with the mode that the type system has chosen for the
3121    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
3122    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
3123
3124 static rtx
3125 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
3126                      unsigned int regno)
3127 {
3128   rtx tmp;
3129
3130   if (orig_mode != BLKmode)
3131     tmp = gen_rtx_REG (orig_mode, regno);
3132   else
3133     {
3134       tmp = gen_rtx_REG (mode, regno);
3135       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
3136       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
3137     }
3138
3139   return tmp;
3140 }
3141
3142 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
3143    of this code is to classify each 8bytes of incoming argument by the register
3144    class and assign registers accordingly.  */
3145
3146 /* Return the union class of CLASS1 and CLASS2.
3147    See the x86-64 PS ABI for details.  */
3148
3149 static enum x86_64_reg_class
3150 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
3151 {
3152   /* Rule #1: If both classes are equal, this is the resulting class.  */
3153   if (class1 == class2)
3154     return class1;
3155
3156   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
3157      the other class.  */
3158   if (class1 == X86_64_NO_CLASS)
3159     return class2;
3160   if (class2 == X86_64_NO_CLASS)
3161     return class1;
3162
3163   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
3164   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
3165     return X86_64_MEMORY_CLASS;
3166
3167   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
3168   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
3169       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
3170     return X86_64_INTEGERSI_CLASS;
3171   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
3172       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
3173     return X86_64_INTEGER_CLASS;
3174
3175   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
3176      MEMORY is used.  */
3177   if (class1 == X86_64_X87_CLASS
3178       || class1 == X86_64_X87UP_CLASS
3179       || class1 == X86_64_COMPLEX_X87_CLASS
3180       || class2 == X86_64_X87_CLASS
3181       || class2 == X86_64_X87UP_CLASS
3182       || class2 == X86_64_COMPLEX_X87_CLASS)
3183     return X86_64_MEMORY_CLASS;
3184
3185   /* Rule #6: Otherwise class SSE is used.  */
3186   return X86_64_SSE_CLASS;
3187 }
3188
3189 /* Classify the argument of type TYPE and mode MODE.
3190    CLASSES will be filled by the register class used to pass each word
3191    of the operand.  The number of words is returned.  In case the parameter
3192    should be passed in memory, 0 is returned. As a special case for zero
3193    sized containers, classes[0] will be NO_CLASS and 1 is returned.
3194
3195    BIT_OFFSET is used internally for handling records and specifies offset
3196    of the offset in bits modulo 256 to avoid overflow cases.
3197
3198    See the x86-64 PS ABI for details.
3199 */
3200
3201 static int
3202 classify_argument (enum machine_mode mode, tree type,
3203                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
3204 {
3205   HOST_WIDE_INT bytes =
3206     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3207   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3208
3209   /* Variable sized entities are always passed/returned in memory.  */
3210   if (bytes < 0)
3211     return 0;
3212
3213   if (mode != VOIDmode
3214       && targetm.calls.must_pass_in_stack (mode, type))
3215     return 0;
3216
3217   if (type && AGGREGATE_TYPE_P (type))
3218     {
3219       int i;
3220       tree field;
3221       enum x86_64_reg_class subclasses[MAX_CLASSES];
3222
3223       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3224       if (bytes > 16)
3225         return 0;
3226
3227       for (i = 0; i < words; i++)
3228         classes[i] = X86_64_NO_CLASS;
3229
3230       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3231          signalize memory class, so handle it as special case.  */
3232       if (!words)
3233         {
3234           classes[0] = X86_64_NO_CLASS;
3235           return 1;
3236         }
3237
3238       /* Classify each field of record and merge classes.  */
3239       switch (TREE_CODE (type))
3240         {
3241         case RECORD_TYPE:
3242           /* And now merge the fields of structure.  */
3243           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3244             {
3245               if (TREE_CODE (field) == FIELD_DECL)
3246                 {
3247                   int num;
3248
3249                   if (TREE_TYPE (field) == error_mark_node)
3250                     continue;
3251
3252                   /* Bitfields are always classified as integer.  Handle them
3253                      early, since later code would consider them to be
3254                      misaligned integers.  */
3255                   if (DECL_BIT_FIELD (field))
3256                     {
3257                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3258                            i < ((int_bit_position (field) + (bit_offset % 64))
3259                                 + tree_low_cst (DECL_SIZE (field), 0)
3260                                 + 63) / 8 / 8; i++)
3261                         classes[i] =
3262                           merge_classes (X86_64_INTEGER_CLASS,
3263                                          classes[i]);
3264                     }
3265                   else
3266                     {
3267                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3268                                                TREE_TYPE (field), subclasses,
3269                                                (int_bit_position (field)
3270                                                 + bit_offset) % 256);
3271                       if (!num)
3272                         return 0;
3273                       for (i = 0; i < num; i++)
3274                         {
3275                           int pos =
3276                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3277                           classes[i + pos] =
3278                             merge_classes (subclasses[i], classes[i + pos]);
3279                         }
3280                     }
3281                 }
3282             }
3283           break;
3284
3285         case ARRAY_TYPE:
3286           /* Arrays are handled as small records.  */
3287           {
3288             int num;
3289             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3290                                      TREE_TYPE (type), subclasses, bit_offset);
3291             if (!num)
3292               return 0;
3293
3294             /* The partial classes are now full classes.  */
3295             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3296               subclasses[0] = X86_64_SSE_CLASS;
3297             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3298               subclasses[0] = X86_64_INTEGER_CLASS;
3299
3300             for (i = 0; i < words; i++)
3301               classes[i] = subclasses[i % num];
3302
3303             break;
3304           }
3305         case UNION_TYPE:
3306         case QUAL_UNION_TYPE:
3307           /* Unions are similar to RECORD_TYPE but offset is always 0.
3308              */
3309           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3310             {
3311               if (TREE_CODE (field) == FIELD_DECL)
3312                 {
3313                   int num;
3314
3315                   if (TREE_TYPE (field) == error_mark_node)
3316                     continue;
3317
3318                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3319                                            TREE_TYPE (field), subclasses,
3320                                            bit_offset);
3321                   if (!num)
3322                     return 0;
3323                   for (i = 0; i < num; i++)
3324                     classes[i] = merge_classes (subclasses[i], classes[i]);
3325                 }
3326             }
3327           break;
3328
3329         default:
3330           gcc_unreachable ();
3331         }
3332
3333       /* Final merger cleanup.  */
3334       for (i = 0; i < words; i++)
3335         {
3336           /* If one class is MEMORY, everything should be passed in
3337              memory.  */
3338           if (classes[i] == X86_64_MEMORY_CLASS)
3339             return 0;
3340
3341           /* The X86_64_SSEUP_CLASS should be always preceded by
3342              X86_64_SSE_CLASS.  */
3343           if (classes[i] == X86_64_SSEUP_CLASS
3344               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3345             classes[i] = X86_64_SSE_CLASS;
3346
3347           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3348           if (classes[i] == X86_64_X87UP_CLASS
3349               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3350             classes[i] = X86_64_SSE_CLASS;
3351         }
3352       return words;
3353     }
3354
3355   /* Compute alignment needed.  We align all types to natural boundaries with
3356      exception of XFmode that is aligned to 64bits.  */
3357   if (mode != VOIDmode && mode != BLKmode)
3358     {
3359       int mode_alignment = GET_MODE_BITSIZE (mode);
3360
3361       if (mode == XFmode)
3362         mode_alignment = 128;
3363       else if (mode == XCmode)
3364         mode_alignment = 256;
3365       if (COMPLEX_MODE_P (mode))
3366         mode_alignment /= 2;
3367       /* Misaligned fields are always returned in memory.  */
3368       if (bit_offset % mode_alignment)
3369         return 0;
3370     }
3371
3372   /* for V1xx modes, just use the base mode */
3373   if (VECTOR_MODE_P (mode)
3374       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3375     mode = GET_MODE_INNER (mode);
3376
3377   /* Classification of atomic types.  */
3378   switch (mode)
3379     {
3380     case SDmode:
3381     case DDmode:
3382       classes[0] = X86_64_SSE_CLASS;
3383       return 1;
3384     case TDmode:
3385       classes[0] = X86_64_SSE_CLASS;
3386       classes[1] = X86_64_SSEUP_CLASS;
3387       return 2;
3388     case DImode:
3389     case SImode:
3390     case HImode:
3391     case QImode:
3392     case CSImode:
3393     case CHImode:
3394     case CQImode:
3395       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3396         classes[0] = X86_64_INTEGERSI_CLASS;
3397       else
3398         classes[0] = X86_64_INTEGER_CLASS;
3399       return 1;
3400     case CDImode:
3401     case TImode:
3402       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3403       return 2;
3404     case CTImode:
3405       return 0;
3406     case SFmode:
3407       if (!(bit_offset % 64))
3408         classes[0] = X86_64_SSESF_CLASS;
3409       else
3410         classes[0] = X86_64_SSE_CLASS;
3411       return 1;
3412     case DFmode:
3413       classes[0] = X86_64_SSEDF_CLASS;
3414       return 1;
3415     case XFmode:
3416       classes[0] = X86_64_X87_CLASS;
3417       classes[1] = X86_64_X87UP_CLASS;
3418       return 2;
3419     case TFmode:
3420       classes[0] = X86_64_SSE_CLASS;
3421       classes[1] = X86_64_SSEUP_CLASS;
3422       return 2;
3423     case SCmode:
3424       classes[0] = X86_64_SSE_CLASS;
3425       return 1;
3426     case DCmode:
3427       classes[0] = X86_64_SSEDF_CLASS;
3428       classes[1] = X86_64_SSEDF_CLASS;
3429       return 2;
3430     case XCmode:
3431       classes[0] = X86_64_COMPLEX_X87_CLASS;
3432       return 1;
3433     case TCmode:
3434       /* This modes is larger than 16 bytes.  */
3435       return 0;
3436     case V4SFmode:
3437     case V4SImode:
3438     case V16QImode:
3439     case V8HImode:
3440     case V2DFmode:
3441     case V2DImode:
3442       classes[0] = X86_64_SSE_CLASS;
3443       classes[1] = X86_64_SSEUP_CLASS;
3444       return 2;
3445     case V2SFmode:
3446     case V2SImode:
3447     case V4HImode:
3448     case V8QImode:
3449       classes[0] = X86_64_SSE_CLASS;
3450       return 1;
3451     case BLKmode:
3452     case VOIDmode:
3453       return 0;
3454     default:
3455       gcc_assert (VECTOR_MODE_P (mode));
3456
3457       if (bytes > 16)
3458         return 0;
3459
3460       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3461
3462       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3463         classes[0] = X86_64_INTEGERSI_CLASS;
3464       else
3465         classes[0] = X86_64_INTEGER_CLASS;
3466       classes[1] = X86_64_INTEGER_CLASS;
3467       return 1 + (bytes > 8);
3468     }
3469 }
3470
3471 /* Examine the argument and return set number of register required in each
3472    class.  Return 0 iff parameter should be passed in memory.  */
3473 static int
3474 examine_argument (enum machine_mode mode, tree type, int in_return,
3475                   int *int_nregs, int *sse_nregs)
3476 {
3477   enum x86_64_reg_class class[MAX_CLASSES];
3478   int n = classify_argument (mode, type, class, 0);
3479
3480   *int_nregs = 0;
3481   *sse_nregs = 0;
3482   if (!n)
3483     return 0;
3484   for (n--; n >= 0; n--)
3485     switch (class[n])
3486       {
3487       case X86_64_INTEGER_CLASS:
3488       case X86_64_INTEGERSI_CLASS:
3489         (*int_nregs)++;
3490         break;
3491       case X86_64_SSE_CLASS:
3492       case X86_64_SSESF_CLASS:
3493       case X86_64_SSEDF_CLASS:
3494         (*sse_nregs)++;
3495         break;
3496       case X86_64_NO_CLASS:
3497       case X86_64_SSEUP_CLASS:
3498         break;
3499       case X86_64_X87_CLASS:
3500       case X86_64_X87UP_CLASS:
3501         if (!in_return)
3502           return 0;
3503         break;
3504       case X86_64_COMPLEX_X87_CLASS:
3505         return in_return ? 2 : 0;
3506       case X86_64_MEMORY_CLASS:
3507         gcc_unreachable ();
3508       }
3509   return 1;
3510 }
3511
3512 /* Construct container for the argument used by GCC interface.  See
3513    FUNCTION_ARG for the detailed description.  */
3514
3515 static rtx
3516 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3517                      tree type, int in_return, int nintregs, int nsseregs,
3518                      const int *intreg, int sse_regno)
3519 {
3520   /* The following variables hold the static issued_error state.  */
3521   static bool issued_sse_arg_error;
3522   static bool issued_sse_ret_error;
3523   static bool issued_x87_ret_error;
3524
3525   enum machine_mode tmpmode;
3526   int bytes =
3527     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3528   enum x86_64_reg_class class[MAX_CLASSES];
3529   int n;
3530   int i;
3531   int nexps = 0;
3532   int needed_sseregs, needed_intregs;
3533   rtx exp[MAX_CLASSES];
3534   rtx ret;
3535
3536   n = classify_argument (mode, type, class, 0);
3537   if (!n)
3538     return NULL;
3539   if (!examine_argument (mode, type, in_return, &needed_intregs,
3540                          &needed_sseregs))
3541     return NULL;
3542   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3543     return NULL;
3544
3545   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3546      some less clueful developer tries to use floating-point anyway.  */
3547   if (needed_sseregs && !TARGET_SSE)
3548     {
3549       if (in_return)
3550         {
3551           if (!issued_sse_ret_error)
3552             {
3553               error ("SSE register return with SSE disabled");
3554               issued_sse_ret_error = true;
3555             }
3556         }
3557       else if (!issued_sse_arg_error)
3558         {
3559           error ("SSE register argument with SSE disabled");
3560           issued_sse_arg_error = true;
3561         }
3562       return NULL;
3563     }
3564
3565   /* Likewise, error if the ABI requires us to return values in the
3566      x87 registers and the user specified -mno-80387.  */
3567   if (!TARGET_80387 && in_return)
3568     for (i = 0; i < n; i++)
3569       if (class[i] == X86_64_X87_CLASS
3570           || class[i] == X86_64_X87UP_CLASS
3571           || class[i] == X86_64_COMPLEX_X87_CLASS)
3572         {
3573           if (!issued_x87_ret_error)
3574             {
3575               error ("x87 register return with x87 disabled");
3576               issued_x87_ret_error = true;
3577             }
3578           return NULL;
3579         }
3580
3581   /* First construct simple cases.  Avoid SCmode, since we want to use
3582      single register to pass this type.  */
3583   if (n == 1 && mode != SCmode)
3584     switch (class[0])
3585       {
3586       case X86_64_INTEGER_CLASS:
3587       case X86_64_INTEGERSI_CLASS:
3588         return gen_rtx_REG (mode, intreg[0]);
3589       case X86_64_SSE_CLASS:
3590       case X86_64_SSESF_CLASS:
3591       case X86_64_SSEDF_CLASS:
3592         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3593       case X86_64_X87_CLASS:
3594       case X86_64_COMPLEX_X87_CLASS:
3595         return gen_rtx_REG (mode, FIRST_STACK_REG);
3596       case X86_64_NO_CLASS:
3597         /* Zero sized array, struct or class.  */
3598         return NULL;
3599       default:
3600         gcc_unreachable ();
3601       }
3602   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3603       && mode != BLKmode)
3604     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3605
3606   if (n == 2
3607       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3608     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3609   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3610       && class[1] == X86_64_INTEGER_CLASS
3611       && (mode == CDImode || mode == TImode || mode == TFmode)
3612       && intreg[0] + 1 == intreg[1])
3613     return gen_rtx_REG (mode, intreg[0]);
3614
3615   /* Otherwise figure out the entries of the PARALLEL.  */
3616   for (i = 0; i < n; i++)
3617     {
3618       switch (class[i])
3619         {
3620           case X86_64_NO_CLASS:
3621             break;
3622           case X86_64_INTEGER_CLASS:
3623           case X86_64_INTEGERSI_CLASS:
3624             /* Merge TImodes on aligned occasions here too.  */
3625             if (i * 8 + 8 > bytes)
3626               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3627             else if (class[i] == X86_64_INTEGERSI_CLASS)
3628               tmpmode = SImode;
3629             else
3630               tmpmode = DImode;
3631             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3632             if (tmpmode == BLKmode)
3633               tmpmode = DImode;
3634             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3635                                                gen_rtx_REG (tmpmode, *intreg),
3636                                                GEN_INT (i*8));
3637             intreg++;
3638             break;
3639           case X86_64_SSESF_CLASS:
3640             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3641                                                gen_rtx_REG (SFmode,
3642                                                             SSE_REGNO (sse_regno)),
3643                                                GEN_INT (i*8));
3644             sse_regno++;
3645             break;
3646           case X86_64_SSEDF_CLASS:
3647             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3648                                                gen_rtx_REG (DFmode,
3649                                                             SSE_REGNO (sse_regno)),
3650                                                GEN_INT (i*8));
3651             sse_regno++;
3652             break;
3653           case X86_64_SSE_CLASS:
3654             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3655               tmpmode = TImode;
3656             else
3657               tmpmode = DImode;
3658             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3659                                                gen_rtx_REG (tmpmode,
3660                                                             SSE_REGNO (sse_regno)),
3661                                                GEN_INT (i*8));
3662             if (tmpmode == TImode)
3663               i++;
3664             sse_regno++;
3665             break;
3666           default:
3667             gcc_unreachable ();
3668         }
3669     }
3670
3671   /* Empty aligned struct, union or class.  */
3672   if (nexps == 0)
3673     return NULL;
3674
3675   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3676   for (i = 0; i < nexps; i++)
3677     XVECEXP (ret, 0, i) = exp [i];
3678   return ret;
3679 }
3680
3681 /* Update the data in CUM to advance over an argument of mode MODE
3682    and data type TYPE.  (TYPE is null for libcalls where that information
3683    may not be available.)  */
3684
3685 static void
3686 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3687                          tree type, HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3688 {
3689   switch (mode)
3690     {
3691     default:
3692       break;
3693
3694     case BLKmode:
3695       if (bytes < 0)
3696         break;
3697       /* FALLTHRU */
3698
3699     case DImode:
3700     case SImode:
3701     case HImode:
3702     case QImode:
3703       cum->words += words;
3704       cum->nregs -= words;
3705       cum->regno += words;
3706
3707       if (cum->nregs <= 0)
3708         {
3709           cum->nregs = 0;
3710           cum->regno = 0;
3711         }
3712       break;
3713
3714     case DFmode:
3715       if (cum->float_in_sse < 2)
3716         break;
3717     case SFmode:
3718       if (cum->float_in_sse < 1)
3719         break;
3720       /* FALLTHRU */
3721
3722     case TImode:
3723     case V16QImode:
3724     case V8HImode:
3725     case V4SImode:
3726     case V2DImode:
3727     case V4SFmode:
3728     case V2DFmode:
3729       if (!type || !AGGREGATE_TYPE_P (type))
3730         {
3731           cum->sse_words += words;
3732           cum->sse_nregs -= 1;
3733           cum->sse_regno += 1;
3734           if (cum->sse_nregs <= 0)
3735             {
3736               cum->sse_nregs = 0;
3737               cum->sse_regno = 0;
3738             }
3739         }
3740       break;
3741
3742     case V8QImode:
3743     case V4HImode:
3744     case V2SImode:
3745     case V2SFmode:
3746       if (!type || !AGGREGATE_TYPE_P (type))
3747         {
3748           cum->mmx_words += words;
3749           cum->mmx_nregs -= 1;
3750           cum->mmx_regno += 1;
3751           if (cum->mmx_nregs <= 0)
3752             {
3753               cum->mmx_nregs = 0;
3754               cum->mmx_regno = 0;
3755             }
3756         }
3757       break;
3758     }
3759 }
3760
3761 static void
3762 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3763                          tree type, HOST_WIDE_INT words)
3764 {
3765   int int_nregs, sse_nregs;
3766
3767   if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3768     cum->words += words;
3769   else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3770     {
3771       cum->nregs -= int_nregs;
3772       cum->sse_nregs -= sse_nregs;
3773       cum->regno += int_nregs;
3774       cum->sse_regno += sse_nregs;
3775     }
3776   else
3777     cum->words += words;
3778 }
3779
3780 static void
3781 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
3782                             HOST_WIDE_INT words)
3783 {
3784   /* Otherwise, this should be passed indirect.  */
3785   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
3786
3787   cum->words += words;
3788   if (cum->nregs > 0)
3789     {
3790       cum->nregs -= 1;
3791       cum->regno += 1;
3792     }
3793 }
3794
3795 void
3796 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3797                       tree type, int named ATTRIBUTE_UNUSED)
3798 {
3799   HOST_WIDE_INT bytes, words;
3800
3801   if (mode == BLKmode)
3802     bytes = int_size_in_bytes (type);
3803   else
3804     bytes = GET_MODE_SIZE (mode);
3805   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3806
3807   if (type)
3808     mode = type_natural_mode (type);
3809
3810   if (TARGET_64BIT_MS_ABI)
3811     function_arg_advance_ms_64 (cum, bytes, words);
3812   else if (TARGET_64BIT)
3813     function_arg_advance_64 (cum, mode, type, words);
3814   else
3815     function_arg_advance_32 (cum, mode, type, bytes, words);
3816 }
3817
3818 /* Define where to put the arguments to a function.
3819    Value is zero to push the argument on the stack,
3820    or a hard register in which to store the argument.
3821
3822    MODE is the argument's machine mode.
3823    TYPE is the data type of the argument (as a tree).
3824     This is null for libcalls where that information may
3825     not be available.
3826    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3827     the preceding args and about the function being called.
3828    NAMED is nonzero if this argument is a named parameter
3829     (otherwise it is an extra parameter matching an ellipsis).  */
3830
3831 static rtx
3832 function_arg_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3833                  enum machine_mode orig_mode, tree type,
3834                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3835 {
3836   static bool warnedsse, warnedmmx;
3837
3838   /* Avoid the AL settings for the Unix64 ABI.  */
3839   if (mode == VOIDmode)
3840     return constm1_rtx;
3841
3842   switch (mode)
3843     {
3844     default:
3845       break;
3846
3847     case BLKmode:
3848       if (bytes < 0)
3849         break;
3850       /* FALLTHRU */
3851     case DImode:
3852     case SImode:
3853     case HImode:
3854     case QImode:
3855       if (words <= cum->nregs)
3856         {
3857           int regno = cum->regno;
3858
3859           /* Fastcall allocates the first two DWORD (SImode) or
3860              smaller arguments to ECX and EDX.  */
3861           if (cum->fastcall)
3862             {
3863               if (mode == BLKmode || mode == DImode)
3864                 break;
3865
3866               /* ECX not EAX is the first allocated register.  */
3867               if (regno == 0)
3868                 regno = 2;
3869             }
3870           return gen_rtx_REG (mode, regno);
3871         }
3872       break;
3873
3874     case DFmode:
3875       if (cum->float_in_sse < 2)
3876         break;
3877     case SFmode:
3878       if (cum->float_in_sse < 1)
3879         break;
3880       /* FALLTHRU */
3881     case TImode:
3882     case V16QImode:
3883     case V8HImode:
3884     case V4SImode:
3885     case V2DImode:
3886     case V4SFmode:
3887     case V2DFmode:
3888       if (!type || !AGGREGATE_TYPE_P (type))
3889         {
3890           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3891             {
3892               warnedsse = true;
3893               warning (0, "SSE vector argument without SSE enabled "
3894                        "changes the ABI");
3895             }
3896           if (cum->sse_nregs)
3897             return gen_reg_or_parallel (mode, orig_mode,
3898                                         cum->sse_regno + FIRST_SSE_REG);
3899         }
3900       break;
3901
3902     case V8QImode:
3903     case V4HImode:
3904     case V2SImode:
3905     case V2SFmode:
3906       if (!type || !AGGREGATE_TYPE_P (type))
3907         {
3908           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3909             {
3910               warnedmmx = true;
3911               warning (0, "MMX vector argument without MMX enabled "
3912                        "changes the ABI");
3913             }
3914           if (cum->mmx_nregs)
3915             return gen_reg_or_parallel (mode, orig_mode,
3916                                         cum->mmx_regno + FIRST_MMX_REG);
3917         }
3918       break;
3919     }
3920
3921   return NULL_RTX;
3922 }
3923
3924 static rtx
3925 function_arg_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3926                  enum machine_mode orig_mode, tree type)
3927 {
3928   /* Handle a hidden AL argument containing number of registers
3929      for varargs x86-64 functions.  */
3930   if (mode == VOIDmode)
3931     return GEN_INT (cum->maybe_vaarg
3932                     ? (cum->sse_nregs < 0
3933                        ? SSE_REGPARM_MAX
3934                        : cum->sse_regno)
3935                     : -1);
3936
3937   return construct_container (mode, orig_mode, type, 0, cum->nregs,
3938                               cum->sse_nregs,
3939                               &x86_64_int_parameter_registers [cum->regno],
3940                               cum->sse_regno);
3941 }
3942
3943 static rtx
3944 function_arg_ms_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3945                     enum machine_mode orig_mode, int named)
3946 {
3947   unsigned int regno;
3948
3949   /* Avoid the AL settings for the Unix64 ABI.  */
3950   if (mode == VOIDmode)
3951     return constm1_rtx;
3952
3953   /* If we've run out of registers, it goes on the stack.  */
3954   if (cum->nregs == 0)
3955     return NULL_RTX;
3956
3957   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
3958
3959   /* Only floating point modes are passed in anything but integer regs.  */
3960   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
3961     {
3962       if (named)
3963         regno = cum->regno + FIRST_SSE_REG;
3964       else
3965         {
3966           rtx t1, t2;
3967
3968           /* Unnamed floating parameters are passed in both the
3969              SSE and integer registers.  */
3970           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
3971           t2 = gen_rtx_REG (mode, regno);
3972           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
3973           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
3974           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
3975         }
3976     }
3977
3978   return gen_reg_or_parallel (mode, orig_mode, regno);
3979 }
3980
3981 rtx
3982 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
3983               tree type, int named)
3984 {
3985   enum machine_mode mode = omode;
3986   HOST_WIDE_INT bytes, words;
3987
3988   if (mode == BLKmode)
3989     bytes = int_size_in_bytes (type);
3990   else
3991     bytes = GET_MODE_SIZE (mode);
3992   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3993
3994   /* To simplify the code below, represent vector types with a vector mode
3995      even if MMX/SSE are not active.  */
3996   if (type && TREE_CODE (type) == VECTOR_TYPE)
3997     mode = type_natural_mode (type);
3998
3999   if (TARGET_64BIT_MS_ABI)
4000     return function_arg_ms_64 (cum, mode, omode, named);
4001   else if (TARGET_64BIT)
4002     return function_arg_64 (cum, mode, omode, type);
4003   else
4004     return function_arg_32 (cum, mode, omode, type, bytes, words);
4005 }
4006
4007 /* A C expression that indicates when an argument must be passed by
4008    reference.  If nonzero for an argument, a copy of that argument is
4009    made in memory and a pointer to the argument is passed instead of
4010    the argument itself.  The pointer is passed in whatever way is
4011    appropriate for passing a pointer to that type.  */
4012
4013 static bool
4014 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4015                         enum machine_mode mode ATTRIBUTE_UNUSED,
4016                         tree type, bool named ATTRIBUTE_UNUSED)
4017 {
4018   if (TARGET_64BIT_MS_ABI)
4019     {
4020       if (type)
4021         {
4022           /* Arrays are passed by reference.  */
4023           if (TREE_CODE (type) == ARRAY_TYPE)
4024             return true;
4025
4026           if (AGGREGATE_TYPE_P (type))
4027             {
4028               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
4029                  are passed by reference.  */
4030               int el2 = exact_log2 (int_size_in_bytes (type));
4031               return !(el2 >= 0 && el2 <= 3);
4032             }
4033         }
4034
4035       /* __m128 is passed by reference.  */
4036       /* ??? How to handle complex?  For now treat them as structs,
4037          and pass them by reference if they're too large.  */
4038       if (GET_MODE_SIZE (mode) > 8)
4039         return true;
4040     }
4041   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
4042     return 1;
4043
4044   return 0;
4045 }
4046
4047 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
4048    ABI.  Only called if TARGET_SSE.  */
4049 static bool
4050 contains_128bit_aligned_vector_p (tree type)
4051 {
4052   enum machine_mode mode = TYPE_MODE (type);
4053   if (SSE_REG_MODE_P (mode)
4054       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
4055     return true;
4056   if (TYPE_ALIGN (type) < 128)
4057     return false;
4058
4059   if (AGGREGATE_TYPE_P (type))
4060     {
4061       /* Walk the aggregates recursively.  */
4062       switch (TREE_CODE (type))
4063         {
4064         case RECORD_TYPE:
4065         case UNION_TYPE:
4066         case QUAL_UNION_TYPE:
4067           {
4068             tree field;
4069
4070             /* Walk all the structure fields.  */
4071             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4072               {
4073                 if (TREE_CODE (field) == FIELD_DECL
4074                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
4075                   return true;
4076               }
4077             break;
4078           }
4079
4080         case ARRAY_TYPE:
4081           /* Just for use if some languages passes arrays by value.  */
4082           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
4083             return true;
4084           break;
4085
4086         default:
4087           gcc_unreachable ();
4088         }
4089     }
4090   return false;
4091 }
4092
4093 /* Gives the alignment boundary, in bits, of an argument with the
4094    specified mode and type.  */
4095
4096 int
4097 ix86_function_arg_boundary (enum machine_mode mode, tree type)
4098 {
4099   int align;
4100   if (type)
4101     align = TYPE_ALIGN (type);
4102   else
4103     align = GET_MODE_ALIGNMENT (mode);
4104   if (align < PARM_BOUNDARY)
4105     align = PARM_BOUNDARY;
4106   if (!TARGET_64BIT)
4107     {
4108       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
4109          make an exception for SSE modes since these require 128bit
4110          alignment.
4111
4112          The handling here differs from field_alignment.  ICC aligns MMX
4113          arguments to 4 byte boundaries, while structure fields are aligned
4114          to 8 byte boundaries.  */
4115       if (!TARGET_SSE)
4116         align = PARM_BOUNDARY;
4117       else if (!type)
4118         {
4119           if (!SSE_REG_MODE_P (mode))
4120             align = PARM_BOUNDARY;
4121         }
4122       else
4123         {
4124           if (!contains_128bit_aligned_vector_p (type))
4125             align = PARM_BOUNDARY;
4126         }
4127     }
4128   if (align > 128)
4129     align = 128;
4130   return align;
4131 }
4132
4133 /* Return true if N is a possible register number of function value.  */
4134
4135 bool
4136 ix86_function_value_regno_p (int regno)
4137 {
4138   switch (regno)
4139     {
4140     case 0:
4141       return true;
4142
4143     case FIRST_FLOAT_REG:
4144       if (TARGET_64BIT_MS_ABI)
4145         return false;
4146       return TARGET_FLOAT_RETURNS_IN_80387;
4147
4148     case FIRST_SSE_REG:
4149       return TARGET_SSE;
4150
4151     case FIRST_MMX_REG:
4152       if (TARGET_MACHO || TARGET_64BIT)
4153         return false;
4154       return TARGET_MMX;
4155     }
4156
4157   return false;
4158 }
4159
4160 /* Define how to find the value returned by a function.
4161    VALTYPE is the data type of the value (as a tree).
4162    If the precise function being called is known, FUNC is its FUNCTION_DECL;
4163    otherwise, FUNC is 0.  */
4164
4165 static rtx
4166 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
4167                    tree fntype, tree fn)
4168 {
4169   unsigned int regno;
4170
4171   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4172      we normally prevent this case when mmx is not available.  However
4173      some ABIs may require the result to be returned like DImode.  */
4174   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4175     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
4176
4177   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4178      we prevent this case when sse is not available.  However some ABIs
4179      may require the result to be returned like integer TImode.  */
4180   else if (mode == TImode
4181            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4182     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
4183
4184   /* Floating point return values in %st(0) (unless -mno-fp-ret-in-387).  */
4185   else if (X87_FLOAT_MODE_P (mode) && TARGET_FLOAT_RETURNS_IN_80387)
4186     regno = FIRST_FLOAT_REG;
4187   else
4188     /* Most things go in %eax.  */
4189     regno = 0;
4190   
4191   /* Override FP return register with %xmm0 for local functions when
4192      SSE math is enabled or for functions with sseregparm attribute.  */
4193   if ((fn || fntype) && (mode == SFmode || mode == DFmode))
4194     {
4195       int sse_level = ix86_function_sseregparm (fntype, fn);
4196       if ((sse_level >= 1 && mode == SFmode)
4197           || (sse_level == 2 && mode == DFmode))
4198         regno = FIRST_SSE_REG;
4199     }
4200
4201   return gen_rtx_REG (orig_mode, regno);
4202 }
4203
4204 static rtx
4205 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
4206                    tree valtype)
4207 {
4208   rtx ret;
4209
4210   /* Handle libcalls, which don't provide a type node.  */
4211   if (valtype == NULL)
4212     {
4213       switch (mode)
4214         {
4215         case SFmode:
4216         case SCmode:
4217         case DFmode:
4218         case DCmode:
4219         case TFmode:
4220         case SDmode:
4221         case DDmode:
4222         case TDmode:
4223           return gen_rtx_REG (mode, FIRST_SSE_REG);
4224         case XFmode:
4225         case XCmode:
4226           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4227         case TCmode:
4228           return NULL;
4229         default:
4230           return gen_rtx_REG (mode, 0);
4231         }
4232     }
4233
4234   ret = construct_container (mode, orig_mode, valtype, 1,
4235                              REGPARM_MAX, SSE_REGPARM_MAX,
4236                              x86_64_int_return_registers, 0);
4237
4238   /* For zero sized structures, construct_container returns NULL, but we
4239      need to keep rest of compiler happy by returning meaningful value.  */
4240   if (!ret)
4241     ret = gen_rtx_REG (orig_mode, 0);
4242
4243   return ret;
4244 }
4245
4246 static rtx
4247 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
4248 {
4249   unsigned int regno = 0;
4250
4251   if (TARGET_SSE)
4252     {
4253       if (mode == SFmode || mode == DFmode)
4254         regno = FIRST_SSE_REG;
4255       else if (VECTOR_MODE_P (mode) || GET_MODE_SIZE (mode) == 16)
4256         regno = FIRST_SSE_REG;
4257     }
4258
4259   return gen_rtx_REG (orig_mode, regno);
4260 }
4261
4262 static rtx
4263 ix86_function_value_1 (tree valtype, tree fntype_or_decl,
4264                        enum machine_mode orig_mode, enum machine_mode mode)
4265 {
4266   tree fn, fntype;
4267
4268   fn = NULL_TREE;
4269   if (fntype_or_decl && DECL_P (fntype_or_decl))
4270     fn = fntype_or_decl;
4271   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
4272
4273   if (TARGET_64BIT_MS_ABI)
4274     return function_value_ms_64 (orig_mode, mode);
4275   else if (TARGET_64BIT)
4276     return function_value_64 (orig_mode, mode, valtype);
4277   else
4278     return function_value_32 (orig_mode, mode, fntype, fn);
4279 }
4280
4281 static rtx
4282 ix86_function_value (tree valtype, tree fntype_or_decl,
4283                      bool outgoing ATTRIBUTE_UNUSED)
4284 {
4285   enum machine_mode mode, orig_mode;
4286
4287   orig_mode = TYPE_MODE (valtype);
4288   mode = type_natural_mode (valtype);
4289   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
4290 }
4291
4292 rtx
4293 ix86_libcall_value (enum machine_mode mode)
4294 {
4295   return ix86_function_value_1 (NULL, NULL, mode, mode);
4296 }
4297
4298 /* Return true iff type is returned in memory.  */
4299
4300 static int
4301 return_in_memory_32 (tree type, enum machine_mode mode)
4302 {
4303   HOST_WIDE_INT size;
4304
4305   if (mode == BLKmode)
4306     return 1;
4307
4308   size = int_size_in_bytes (type);
4309
4310   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
4311     return 0;
4312
4313   if (VECTOR_MODE_P (mode) || mode == TImode)
4314     {
4315       /* User-created vectors small enough to fit in EAX.  */
4316       if (size < 8)
4317         return 0;
4318
4319       /* MMX/3dNow values are returned in MM0,
4320          except when it doesn't exits.  */
4321       if (size == 8)
4322         return (TARGET_MMX ? 0 : 1);
4323
4324       /* SSE values are returned in XMM0, except when it doesn't exist.  */
4325       if (size == 16)
4326         return (TARGET_SSE ? 0 : 1);
4327     }
4328
4329   if (mode == XFmode)
4330     return 0;
4331
4332   if (mode == TDmode)
4333     return 1;
4334
4335   if (size > 12)
4336     return 1;
4337   return 0;
4338 }
4339
4340 static int
4341 return_in_memory_64 (tree type, enum machine_mode mode)
4342 {
4343   int needed_intregs, needed_sseregs;
4344   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
4345 }
4346
4347 static int
4348 return_in_memory_ms_64 (tree type, enum machine_mode mode)
4349 {
4350   HOST_WIDE_INT size = int_size_in_bytes (type);
4351
4352   /* __m128 and friends are returned in xmm0.  */
4353   if (size == 16 && VECTOR_MODE_P (mode))
4354     return 0;
4355
4356   /* Otherwise, the size must be exactly in [1248].  */
4357   return (size != 1 && size != 2 && size != 4 && size != 8);
4358 }
4359
4360 int
4361 ix86_return_in_memory (tree type)
4362 {
4363   enum machine_mode mode = type_natural_mode (type);
4364
4365   if (TARGET_64BIT_MS_ABI)
4366     return return_in_memory_ms_64 (type, mode);
4367   else if (TARGET_64BIT)
4368     return return_in_memory_64 (type, mode);
4369   else
4370     return return_in_memory_32 (type, mode);
4371 }
4372
4373 /* When returning SSE vector types, we have a choice of either
4374      (1) being abi incompatible with a -march switch, or
4375      (2) generating an error.
4376    Given no good solution, I think the safest thing is one warning.
4377    The user won't be able to use -Werror, but....
4378
4379    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
4380    called in response to actually generating a caller or callee that
4381    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
4382    via aggregate_value_p for general type probing from tree-ssa.  */
4383
4384 static rtx
4385 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
4386 {
4387   static bool warnedsse, warnedmmx;
4388
4389   if (!TARGET_64BIT && type)
4390     {
4391       /* Look at the return type of the function, not the function type.  */
4392       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
4393
4394       if (!TARGET_SSE && !warnedsse)
4395         {
4396           if (mode == TImode
4397               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4398             {
4399               warnedsse = true;
4400               warning (0, "SSE vector return without SSE enabled "
4401                        "changes the ABI");
4402             }
4403         }
4404
4405       if (!TARGET_MMX && !warnedmmx)
4406         {
4407           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4408             {
4409               warnedmmx = true;
4410               warning (0, "MMX vector return without MMX enabled "
4411                        "changes the ABI");
4412             }
4413         }
4414     }
4415
4416   return NULL;
4417 }
4418
4419 \f
4420 /* Create the va_list data type.  */
4421
4422 static tree
4423 ix86_build_builtin_va_list (void)
4424 {
4425   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4426
4427   /* For i386 we use plain pointer to argument area.  */
4428   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4429     return build_pointer_type (char_type_node);
4430
4431   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4432   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4433
4434   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4435                       unsigned_type_node);
4436   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4437                       unsigned_type_node);
4438   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4439                       ptr_type_node);
4440   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4441                       ptr_type_node);
4442
4443   va_list_gpr_counter_field = f_gpr;
4444   va_list_fpr_counter_field = f_fpr;
4445
4446   DECL_FIELD_CONTEXT (f_gpr) = record;
4447   DECL_FIELD_CONTEXT (f_fpr) = record;
4448   DECL_FIELD_CONTEXT (f_ovf) = record;
4449   DECL_FIELD_CONTEXT (f_sav) = record;
4450
4451   TREE_CHAIN (record) = type_decl;
4452   TYPE_NAME (record) = type_decl;
4453   TYPE_FIELDS (record) = f_gpr;
4454   TREE_CHAIN (f_gpr) = f_fpr;
4455   TREE_CHAIN (f_fpr) = f_ovf;
4456   TREE_CHAIN (f_ovf) = f_sav;
4457
4458   layout_type (record);
4459
4460   /* The correct type is an array type of one element.  */
4461   return build_array_type (record, build_index_type (size_zero_node));
4462 }
4463
4464 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4465
4466 static void
4467 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
4468 {
4469   rtx save_area, mem;
4470   rtx label;
4471   rtx label_ref;
4472   rtx tmp_reg;
4473   rtx nsse_reg;
4474   int set;
4475   int i;
4476
4477   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4478     return;
4479
4480   /* Indicate to allocate space on the stack for varargs save area.  */
4481   ix86_save_varrargs_registers = 1;
4482   cfun->stack_alignment_needed = 128;
4483
4484   save_area = frame_pointer_rtx;
4485   set = get_varargs_alias_set ();
4486
4487   for (i = cum->regno;
4488        i < ix86_regparm
4489        && i < cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4490        i++)
4491     {
4492       mem = gen_rtx_MEM (Pmode,
4493                          plus_constant (save_area, i * UNITS_PER_WORD));
4494       MEM_NOTRAP_P (mem) = 1;
4495       set_mem_alias_set (mem, set);
4496       emit_move_insn (mem, gen_rtx_REG (Pmode,
4497                                         x86_64_int_parameter_registers[i]));
4498     }
4499
4500   if (cum->sse_nregs && cfun->va_list_fpr_size)
4501     {
4502       /* Now emit code to save SSE registers.  The AX parameter contains number
4503          of SSE parameter registers used to call this function.  We use
4504          sse_prologue_save insn template that produces computed jump across
4505          SSE saves.  We need some preparation work to get this working.  */
4506
4507       label = gen_label_rtx ();
4508       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4509
4510       /* Compute address to jump to :
4511          label - 5*eax + nnamed_sse_arguments*5  */
4512       tmp_reg = gen_reg_rtx (Pmode);
4513       nsse_reg = gen_reg_rtx (Pmode);
4514       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4515       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4516                               gen_rtx_MULT (Pmode, nsse_reg,
4517                                             GEN_INT (4))));
4518       if (cum->sse_regno)
4519         emit_move_insn
4520           (nsse_reg,
4521            gen_rtx_CONST (DImode,
4522                           gen_rtx_PLUS (DImode,
4523                                         label_ref,
4524                                         GEN_INT (cum->sse_regno * 4))));
4525       else
4526         emit_move_insn (nsse_reg, label_ref);
4527       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4528
4529       /* Compute address of memory block we save into.  We always use pointer
4530          pointing 127 bytes after first byte to store - this is needed to keep
4531          instruction size limited by 4 bytes.  */
4532       tmp_reg = gen_reg_rtx (Pmode);
4533       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4534                               plus_constant (save_area,
4535                                              8 * REGPARM_MAX + 127)));
4536       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4537       MEM_NOTRAP_P (mem) = 1;
4538       set_mem_alias_set (mem, set);
4539       set_mem_align (mem, BITS_PER_WORD);
4540
4541       /* And finally do the dirty job!  */
4542       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4543                                         GEN_INT (cum->sse_regno), label));
4544     }
4545 }
4546
4547 static void
4548 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
4549 {
4550   int set = get_varargs_alias_set ();
4551   int i;
4552
4553   for (i = cum->regno; i < REGPARM_MAX; i++)
4554     {
4555       rtx reg, mem;
4556
4557       mem = gen_rtx_MEM (Pmode,
4558                          plus_constant (virtual_incoming_args_rtx,
4559                                         i * UNITS_PER_WORD));
4560       MEM_NOTRAP_P (mem) = 1;
4561       set_mem_alias_set (mem, set);
4562
4563       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
4564       emit_move_insn (mem, reg);
4565     }
4566 }
4567
4568 static void
4569 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4570                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4571                              int no_rtl)
4572 {
4573   CUMULATIVE_ARGS next_cum;
4574   tree fntype;
4575   int stdarg_p;
4576
4577   /* This argument doesn't appear to be used anymore.  Which is good,
4578      because the old code here didn't suppress rtl generation.  */
4579   gcc_assert (!no_rtl);
4580
4581   if (!TARGET_64BIT)
4582     return;
4583
4584   fntype = TREE_TYPE (current_function_decl);
4585   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4586               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4587                   != void_type_node));
4588
4589   /* For varargs, we do not want to skip the dummy va_dcl argument.
4590      For stdargs, we do want to skip the last named argument.  */
4591   next_cum = *cum;
4592   if (stdarg_p)
4593     function_arg_advance (&next_cum, mode, type, 1);
4594
4595   if (TARGET_64BIT_MS_ABI)
4596     setup_incoming_varargs_ms_64 (&next_cum);
4597   else
4598     setup_incoming_varargs_64 (&next_cum);
4599 }
4600
4601 /* Implement va_start.  */
4602
4603 void
4604 ix86_va_start (tree valist, rtx nextarg)
4605 {
4606   HOST_WIDE_INT words, n_gpr, n_fpr;
4607   tree f_gpr, f_fpr, f_ovf, f_sav;
4608   tree gpr, fpr, ovf, sav, t;
4609   tree type;
4610
4611   /* Only 64bit target needs something special.  */
4612   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4613     {
4614       std_expand_builtin_va_start (valist, nextarg);
4615       return;
4616     }
4617
4618   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4619   f_fpr = TREE_CHAIN (f_gpr);
4620   f_ovf = TREE_CHAIN (f_fpr);
4621   f_sav = TREE_CHAIN (f_ovf);
4622
4623   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4624   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4625   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4626   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4627   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4628
4629   /* Count number of gp and fp argument registers used.  */
4630   words = current_function_args_info.words;
4631   n_gpr = current_function_args_info.regno;
4632   n_fpr = current_function_args_info.sse_regno;
4633
4634   if (cfun->va_list_gpr_size)
4635     {
4636       type = TREE_TYPE (gpr);
4637       t = build2 (GIMPLE_MODIFY_STMT, type, gpr,
4638                   build_int_cst (type, n_gpr * 8));
4639       TREE_SIDE_EFFECTS (t) = 1;
4640       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4641     }
4642
4643   if (cfun->va_list_fpr_size)
4644     {
4645       type = TREE_TYPE (fpr);
4646       t = build2 (GIMPLE_MODIFY_STMT, type, fpr,
4647                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4648       TREE_SIDE_EFFECTS (t) = 1;
4649       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4650     }
4651
4652   /* Find the overflow area.  */
4653   type = TREE_TYPE (ovf);
4654   t = make_tree (type, virtual_incoming_args_rtx);
4655   if (words != 0)
4656     t = build2 (PLUS_EXPR, type, t,
4657                 build_int_cst (type, words * UNITS_PER_WORD));
4658   t = build2 (GIMPLE_MODIFY_STMT, type, ovf, t);
4659   TREE_SIDE_EFFECTS (t) = 1;
4660   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4661
4662   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4663     {
4664       /* Find the register save area.
4665          Prologue of the function save it right above stack frame.  */
4666       type = TREE_TYPE (sav);
4667       t = make_tree (type, frame_pointer_rtx);
4668       t = build2 (GIMPLE_MODIFY_STMT, type, sav, t);
4669       TREE_SIDE_EFFECTS (t) = 1;
4670       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4671     }
4672 }
4673
4674 /* Implement va_arg.  */
4675
4676 static tree
4677 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4678 {
4679   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4680   tree f_gpr, f_fpr, f_ovf, f_sav;
4681   tree gpr, fpr, ovf, sav, t;
4682   int size, rsize;
4683   tree lab_false, lab_over = NULL_TREE;
4684   tree addr, t2;
4685   rtx container;
4686   int indirect_p = 0;
4687   tree ptrtype;
4688   enum machine_mode nat_mode;
4689
4690   /* Only 64bit target needs something special.  */
4691   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4692     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4693
4694   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4695   f_fpr = TREE_CHAIN (f_gpr);
4696   f_ovf = TREE_CHAIN (f_fpr);
4697   f_sav = TREE_CHAIN (f_ovf);
4698
4699   valist = build_va_arg_indirect_ref (valist);
4700   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4701   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4702   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4703   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4704
4705   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4706   if (indirect_p)
4707     type = build_pointer_type (type);
4708   size = int_size_in_bytes (type);
4709   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4710
4711   nat_mode = type_natural_mode (type);
4712   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4713                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4714
4715   /* Pull the value out of the saved registers.  */
4716
4717   addr = create_tmp_var (ptr_type_node, "addr");
4718   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4719
4720   if (container)
4721     {
4722       int needed_intregs, needed_sseregs;
4723       bool need_temp;
4724       tree int_addr, sse_addr;
4725
4726       lab_false = create_artificial_label ();
4727       lab_over = create_artificial_label ();
4728
4729       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4730
4731       need_temp = (!REG_P (container)
4732                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4733                        || TYPE_ALIGN (type) > 128));
4734
4735       /* In case we are passing structure, verify that it is consecutive block
4736          on the register save area.  If not we need to do moves.  */
4737       if (!need_temp && !REG_P (container))
4738         {
4739           /* Verify that all registers are strictly consecutive  */
4740           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4741             {
4742               int i;
4743
4744               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4745                 {
4746                   rtx slot = XVECEXP (container, 0, i);
4747                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4748                       || INTVAL (XEXP (slot, 1)) != i * 16)
4749                     need_temp = 1;
4750                 }
4751             }
4752           else
4753             {
4754               int i;
4755
4756               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4757                 {
4758                   rtx slot = XVECEXP (container, 0, i);
4759                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4760                       || INTVAL (XEXP (slot, 1)) != i * 8)
4761                     need_temp = 1;
4762                 }
4763             }
4764         }
4765       if (!need_temp)
4766         {
4767           int_addr = addr;
4768           sse_addr = addr;
4769         }
4770       else
4771         {
4772           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4773           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4774           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4775           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4776         }
4777
4778       /* First ensure that we fit completely in registers.  */
4779       if (needed_intregs)
4780         {
4781           t = build_int_cst (TREE_TYPE (gpr),
4782                              (REGPARM_MAX - needed_intregs + 1) * 8);
4783           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4784           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4785           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4786           gimplify_and_add (t, pre_p);
4787         }
4788       if (needed_sseregs)
4789         {
4790           t = build_int_cst (TREE_TYPE (fpr),
4791                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4792                              + REGPARM_MAX * 8);
4793           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4794           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4795           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4796           gimplify_and_add (t, pre_p);
4797         }
4798
4799       /* Compute index to start of area used for integer regs.  */
4800       if (needed_intregs)
4801         {
4802           /* int_addr = gpr + sav; */
4803           t = fold_convert (ptr_type_node, gpr);
4804           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4805           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, int_addr, t);
4806           gimplify_and_add (t, pre_p);
4807         }
4808       if (needed_sseregs)
4809         {
4810           /* sse_addr = fpr + sav; */
4811           t = fold_convert (ptr_type_node, fpr);
4812           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4813           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, sse_addr, t);
4814           gimplify_and_add (t, pre_p);
4815         }
4816       if (need_temp)
4817         {
4818           int i;
4819           tree temp = create_tmp_var (type, "va_arg_tmp");
4820
4821           /* addr = &temp; */
4822           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4823           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4824           gimplify_and_add (t, pre_p);
4825
4826           for (i = 0; i < XVECLEN (container, 0); i++)
4827             {
4828               rtx slot = XVECEXP (container, 0, i);
4829               rtx reg = XEXP (slot, 0);
4830               enum machine_mode mode = GET_MODE (reg);
4831               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4832               tree addr_type = build_pointer_type (piece_type);
4833               tree src_addr, src;
4834               int src_offset;
4835               tree dest_addr, dest;
4836
4837               if (SSE_REGNO_P (REGNO (reg)))
4838                 {
4839                   src_addr = sse_addr;
4840                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4841                 }
4842               else
4843                 {
4844                   src_addr = int_addr;
4845                   src_offset = REGNO (reg) * 8;
4846                 }
4847               src_addr = fold_convert (addr_type, src_addr);
4848               src_addr = fold_build2 (PLUS_EXPR, addr_type, src_addr,
4849                                       size_int (src_offset));
4850               src = build_va_arg_indirect_ref (src_addr);
4851
4852               dest_addr = fold_convert (addr_type, addr);
4853               dest_addr = fold_build2 (PLUS_EXPR, addr_type, dest_addr,
4854                                        size_int (INTVAL (XEXP (slot, 1))));
4855               dest = build_va_arg_indirect_ref (dest_addr);
4856
4857               t = build2 (GIMPLE_MODIFY_STMT, void_type_node, dest, src);
4858               gimplify_and_add (t, pre_p);
4859             }
4860         }
4861
4862       if (needed_intregs)
4863         {
4864           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4865                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4866           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (gpr), gpr, t);
4867           gimplify_and_add (t, pre_p);
4868         }
4869       if (needed_sseregs)
4870         {
4871           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4872                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4873           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (fpr), fpr, t);
4874           gimplify_and_add (t, pre_p);
4875         }
4876
4877       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4878       gimplify_and_add (t, pre_p);
4879
4880       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4881       append_to_statement_list (t, pre_p);
4882     }
4883
4884   /* ... otherwise out of the overflow area.  */
4885
4886   /* Care for on-stack alignment if needed.  */
4887   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4888       || integer_zerop (TYPE_SIZE (type)))
4889     t = ovf;
4890   else
4891     {
4892       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4893       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4894                   build_int_cst (TREE_TYPE (ovf), align - 1));
4895       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4896                   build_int_cst (TREE_TYPE (t), -align));
4897     }
4898   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4899
4900   t2 = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4901   gimplify_and_add (t2, pre_p);
4902
4903   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4904               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4905   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (ovf), ovf, t);
4906   gimplify_and_add (t, pre_p);
4907
4908   if (container)
4909     {
4910       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4911       append_to_statement_list (t, pre_p);
4912     }
4913
4914   ptrtype = build_pointer_type (type);
4915   addr = fold_convert (ptrtype, addr);
4916
4917   if (indirect_p)
4918     addr = build_va_arg_indirect_ref (addr);
4919   return build_va_arg_indirect_ref (addr);
4920 }
4921 \f
4922 /* Return nonzero if OPNUM's MEM should be matched
4923    in movabs* patterns.  */
4924
4925 int
4926 ix86_check_movabs (rtx insn, int opnum)
4927 {
4928   rtx set, mem;
4929
4930   set = PATTERN (insn);
4931   if (GET_CODE (set) == PARALLEL)
4932     set = XVECEXP (set, 0, 0);
4933   gcc_assert (GET_CODE (set) == SET);
4934   mem = XEXP (set, opnum);
4935   while (GET_CODE (mem) == SUBREG)
4936     mem = SUBREG_REG (mem);
4937   gcc_assert (MEM_P (mem));
4938   return (volatile_ok || !MEM_VOLATILE_P (mem));
4939 }
4940 \f
4941 /* Initialize the table of extra 80387 mathematical constants.  */
4942
4943 static void
4944 init_ext_80387_constants (void)
4945 {
4946   static const char * cst[5] =
4947   {
4948     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4949     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4950     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4951     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4952     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4953   };
4954   int i;
4955
4956   for (i = 0; i < 5; i++)
4957     {
4958       real_from_string (&ext_80387_constants_table[i], cst[i]);
4959       /* Ensure each constant is rounded to XFmode precision.  */
4960       real_convert (&ext_80387_constants_table[i],
4961                     XFmode, &ext_80387_constants_table[i]);
4962     }
4963
4964   ext_80387_constants_init = 1;
4965 }
4966
4967 /* Return true if the constant is something that can be loaded with
4968    a special instruction.  */
4969
4970 int
4971 standard_80387_constant_p (rtx x)
4972 {
4973   enum machine_mode mode = GET_MODE (x);
4974
4975   REAL_VALUE_TYPE r;
4976
4977   if (!(X87_FLOAT_MODE_P (mode) && (GET_CODE (x) == CONST_DOUBLE)))
4978     return -1;
4979
4980   if (x == CONST0_RTX (mode))
4981     return 1;
4982   if (x == CONST1_RTX (mode))
4983     return 2;
4984
4985   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4986
4987   /* For XFmode constants, try to find a special 80387 instruction when
4988      optimizing for size or on those CPUs that benefit from them.  */
4989   if (mode == XFmode
4990       && (optimize_size || TARGET_EXT_80387_CONSTANTS))
4991     {
4992       int i;
4993
4994       if (! ext_80387_constants_init)
4995         init_ext_80387_constants ();
4996
4997       for (i = 0; i < 5; i++)
4998         if (real_identical (&r, &ext_80387_constants_table[i]))
4999           return i + 3;
5000     }
5001
5002   /* Load of the constant -0.0 or -1.0 will be split as
5003      fldz;fchs or fld1;fchs sequence.  */
5004   if (real_isnegzero (&r))
5005     return 8;
5006   if (real_identical (&r, &dconstm1))
5007     return 9;
5008
5009   return 0;
5010 }
5011
5012 /* Return the opcode of the special instruction to be used to load
5013    the constant X.  */
5014
5015 const char *
5016 standard_80387_constant_opcode (rtx x)
5017 {
5018   switch (standard_80387_constant_p (x))
5019     {
5020     case 1:
5021       return "fldz";
5022     case 2:
5023       return "fld1";
5024     case 3:
5025       return "fldlg2";
5026     case 4:
5027       return "fldln2";
5028     case 5:
5029       return "fldl2e";
5030     case 6:
5031       return "fldl2t";
5032     case 7:
5033       return "fldpi";
5034     case 8:
5035     case 9:
5036       return "#";
5037     default:
5038       gcc_unreachable ();
5039     }
5040 }
5041
5042 /* Return the CONST_DOUBLE representing the 80387 constant that is
5043    loaded by the specified special instruction.  The argument IDX
5044    matches the return value from standard_80387_constant_p.  */
5045
5046 rtx
5047 standard_80387_constant_rtx (int idx)
5048 {
5049   int i;
5050
5051   if (! ext_80387_constants_init)
5052     init_ext_80387_constants ();
5053
5054   switch (idx)
5055     {
5056     case 3:
5057     case 4:
5058     case 5:
5059     case 6:
5060     case 7:
5061       i = idx - 3;
5062       break;
5063
5064     default:
5065       gcc_unreachable ();
5066     }
5067
5068   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
5069                                        XFmode);
5070 }
5071
5072 /* Return 1 if mode is a valid mode for sse.  */
5073 static int
5074 standard_sse_mode_p (enum machine_mode mode)
5075 {
5076   switch (mode)
5077     {
5078     case V16QImode:
5079     case V8HImode:
5080     case V4SImode:
5081     case V2DImode:
5082     case V4SFmode:
5083     case V2DFmode:
5084       return 1;
5085
5086     default:
5087       return 0;
5088     }
5089 }
5090
5091 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
5092  */
5093 int
5094 standard_sse_constant_p (rtx x)
5095 {
5096   enum machine_mode mode = GET_MODE (x);
5097
5098   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
5099     return 1;
5100   if (vector_all_ones_operand (x, mode)
5101       && standard_sse_mode_p (mode))
5102     return TARGET_SSE2 ? 2 : -1;
5103
5104   return 0;
5105 }
5106
5107 /* Return the opcode of the special instruction to be used to load
5108    the constant X.  */
5109
5110 const char *
5111 standard_sse_constant_opcode (rtx insn, rtx x)
5112 {
5113   switch (standard_sse_constant_p (x))
5114     {
5115     case 1:
5116       if (get_attr_mode (insn) == MODE_V4SF)
5117         return "xorps\t%0, %0";
5118       else if (get_attr_mode (insn) == MODE_V2DF)
5119         return "xorpd\t%0, %0";
5120       else
5121         return "pxor\t%0, %0";
5122     case 2:
5123       return "pcmpeqd\t%0, %0";
5124     }
5125   gcc_unreachable ();
5126 }
5127
5128 /* Returns 1 if OP contains a symbol reference */
5129
5130 int
5131 symbolic_reference_mentioned_p (rtx op)
5132 {
5133   const char *fmt;
5134   int i;
5135
5136   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
5137     return 1;
5138
5139   fmt = GET_RTX_FORMAT (GET_CODE (op));
5140   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5141     {
5142       if (fmt[i] == 'E')
5143         {
5144           int j;
5145
5146           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5147             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
5148               return 1;
5149         }
5150
5151       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
5152         return 1;
5153     }
5154
5155   return 0;
5156 }
5157
5158 /* Return 1 if it is appropriate to emit `ret' instructions in the
5159    body of a function.  Do this only if the epilogue is simple, needing a
5160    couple of insns.  Prior to reloading, we can't tell how many registers
5161    must be saved, so return 0 then.  Return 0 if there is no frame
5162    marker to de-allocate.  */
5163
5164 int
5165 ix86_can_use_return_insn_p (void)
5166 {
5167   struct ix86_frame frame;
5168
5169   if (! reload_completed || frame_pointer_needed)
5170     return 0;
5171
5172   /* Don't allow more than 32 pop, since that's all we can do
5173      with one instruction.  */
5174   if (current_function_pops_args
5175       && current_function_args_size >= 32768)
5176     return 0;
5177
5178   ix86_compute_frame_layout (&frame);
5179   return frame.to_allocate == 0 && frame.nregs == 0;
5180 }
5181 \f
5182 /* Value should be nonzero if functions must have frame pointers.
5183    Zero means the frame pointer need not be set up (and parms may
5184    be accessed via the stack pointer) in functions that seem suitable.  */
5185
5186 int
5187 ix86_frame_pointer_required (void)
5188 {
5189   /* If we accessed previous frames, then the generated code expects
5190      to be able to access the saved ebp value in our frame.  */
5191   if (cfun->machine->accesses_prev_frame)
5192     return 1;
5193
5194   /* Several x86 os'es need a frame pointer for other reasons,
5195      usually pertaining to setjmp.  */
5196   if (SUBTARGET_FRAME_POINTER_REQUIRED)
5197     return 1;
5198
5199   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
5200      the frame pointer by default.  Turn it back on now if we've not
5201      got a leaf function.  */
5202   if (TARGET_OMIT_LEAF_FRAME_POINTER
5203       && (!current_function_is_leaf
5204           || ix86_current_function_calls_tls_descriptor))
5205     return 1;
5206
5207   if (current_function_profile)
5208     return 1;
5209
5210   return 0;
5211 }
5212
5213 /* Record that the current function accesses previous call frames.  */
5214
5215 void
5216 ix86_setup_frame_addresses (void)
5217 {
5218   cfun->machine->accesses_prev_frame = 1;
5219 }
5220 \f
5221 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
5222 # define USE_HIDDEN_LINKONCE 1
5223 #else
5224 # define USE_HIDDEN_LINKONCE 0
5225 #endif
5226
5227 static int pic_labels_used;
5228
5229 /* Fills in the label name that should be used for a pc thunk for
5230    the given register.  */
5231
5232 static void
5233 get_pc_thunk_name (char name[32], unsigned int regno)
5234 {
5235   gcc_assert (!TARGET_64BIT);
5236
5237   if (USE_HIDDEN_LINKONCE)
5238     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
5239   else
5240     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
5241 }
5242
5243
5244 /* This function generates code for -fpic that loads %ebx with
5245    the return address of the caller and then returns.  */
5246
5247 void
5248 ix86_file_end (void)
5249 {
5250   rtx xops[2];
5251   int regno;
5252
5253   for (regno = 0; regno < 8; ++regno)
5254     {
5255       char name[32];
5256
5257       if (! ((pic_labels_used >> regno) & 1))
5258         continue;
5259
5260       get_pc_thunk_name (name, regno);
5261
5262 #if TARGET_MACHO
5263       if (TARGET_MACHO)
5264         {
5265           switch_to_section (darwin_sections[text_coal_section]);
5266           fputs ("\t.weak_definition\t", asm_out_file);
5267           assemble_name (asm_out_file, name);
5268           fputs ("\n\t.private_extern\t", asm_out_file);
5269           assemble_name (asm_out_file, name);
5270           fputs ("\n", asm_out_file);
5271           ASM_OUTPUT_LABEL (asm_out_file, name);
5272         }
5273       else
5274 #endif
5275       if (USE_HIDDEN_LINKONCE)
5276         {
5277           tree decl;
5278
5279           decl = build_decl (FUNCTION_DECL, get_identifier (name),
5280                              error_mark_node);
5281           TREE_PUBLIC (decl) = 1;
5282           TREE_STATIC (decl) = 1;
5283           DECL_ONE_ONLY (decl) = 1;
5284
5285           (*targetm.asm_out.unique_section) (decl, 0);
5286           switch_to_section (get_named_section (decl, NULL, 0));
5287
5288           (*targetm.asm_out.globalize_label) (asm_out_file, name);
5289           fputs ("\t.hidden\t", asm_out_file);
5290           assemble_name (asm_out_file, name);
5291           fputc ('\n', asm_out_file);
5292           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
5293         }
5294       else
5295         {
5296           switch_to_section (text_section);
5297           ASM_OUTPUT_LABEL (asm_out_file, name);
5298         }
5299
5300       xops[0] = gen_rtx_REG (SImode, regno);
5301       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
5302       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
5303       output_asm_insn ("ret", xops);
5304     }
5305
5306   if (NEED_INDICATE_EXEC_STACK)
5307     file_end_indicate_exec_stack ();
5308 }
5309
5310 /* Emit code for the SET_GOT patterns.  */
5311
5312 const char *
5313 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
5314 {
5315   rtx xops[3];
5316
5317   xops[0] = dest;
5318
5319   if (TARGET_VXWORKS_RTP && flag_pic)
5320     {
5321       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
5322       xops[2] = gen_rtx_MEM (Pmode,
5323                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
5324       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5325
5326       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
5327          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
5328          an unadorned address.  */
5329       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5330       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
5331       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
5332       return "";
5333     }
5334
5335   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
5336
5337   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
5338     {
5339       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
5340
5341       if (!flag_pic)
5342         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5343       else
5344         output_asm_insn ("call\t%a2", xops);
5345
5346 #if TARGET_MACHO
5347       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5348          is what will be referenced by the Mach-O PIC subsystem.  */
5349       if (!label)
5350         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5351 #endif
5352
5353       (*targetm.asm_out.internal_label) (asm_out_file, "L",
5354                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
5355
5356       if (flag_pic)
5357         output_asm_insn ("pop{l}\t%0", xops);
5358     }
5359   else
5360     {
5361       char name[32];
5362       get_pc_thunk_name (name, REGNO (dest));
5363       pic_labels_used |= 1 << REGNO (dest);
5364
5365       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5366       xops[2] = gen_rtx_MEM (QImode, xops[2]);
5367       output_asm_insn ("call\t%X2", xops);
5368       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5369          is what will be referenced by the Mach-O PIC subsystem.  */
5370 #if TARGET_MACHO
5371       if (!label)
5372         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5373       else
5374         targetm.asm_out.internal_label (asm_out_file, "L",
5375                                            CODE_LABEL_NUMBER (label));
5376 #endif
5377     }
5378
5379   if (TARGET_MACHO)
5380     return "";
5381
5382   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
5383     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
5384   else
5385     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
5386
5387   return "";
5388 }
5389
5390 /* Generate an "push" pattern for input ARG.  */
5391
5392 static rtx
5393 gen_push (rtx arg)
5394 {
5395   return gen_rtx_SET (VOIDmode,
5396                       gen_rtx_MEM (Pmode,
5397                                    gen_rtx_PRE_DEC (Pmode,
5398                                                     stack_pointer_rtx)),
5399                       arg);
5400 }
5401
5402 /* Return >= 0 if there is an unused call-clobbered register available
5403    for the entire function.  */
5404
5405 static unsigned int
5406 ix86_select_alt_pic_regnum (void)
5407 {
5408   if (current_function_is_leaf && !current_function_profile
5409       && !ix86_current_function_calls_tls_descriptor)
5410     {
5411       int i;
5412       for (i = 2; i >= 0; --i)
5413         if (!regs_ever_live[i])
5414           return i;
5415     }
5416
5417   return INVALID_REGNUM;
5418 }
5419
5420 /* Return 1 if we need to save REGNO.  */
5421 static int
5422 ix86_save_reg (unsigned int regno, int maybe_eh_return)
5423 {
5424   if (pic_offset_table_rtx
5425       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5426       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5427           || current_function_profile
5428           || current_function_calls_eh_return
5429           || current_function_uses_const_pool))
5430     {
5431       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5432         return 0;
5433       return 1;
5434     }
5435
5436   if (current_function_calls_eh_return && maybe_eh_return)
5437     {
5438       unsigned i;
5439       for (i = 0; ; i++)
5440         {
5441           unsigned test = EH_RETURN_DATA_REGNO (i);
5442           if (test == INVALID_REGNUM)
5443             break;
5444           if (test == regno)
5445             return 1;
5446         }
5447     }
5448
5449   if (cfun->machine->force_align_arg_pointer
5450       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5451     return 1;
5452
5453   return (regs_ever_live[regno]
5454           && !call_used_regs[regno]
5455           && !fixed_regs[regno]
5456           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5457 }
5458
5459 /* Return number of registers to be saved on the stack.  */
5460
5461 static int
5462 ix86_nsaved_regs (void)
5463 {
5464   int nregs = 0;
5465   int regno;
5466
5467   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5468     if (ix86_save_reg (regno, true))
5469       nregs++;
5470   return nregs;
5471 }
5472
5473 /* Return the offset between two registers, one to be eliminated, and the other
5474    its replacement, at the start of a routine.  */
5475
5476 HOST_WIDE_INT
5477 ix86_initial_elimination_offset (int from, int to)
5478 {
5479   struct ix86_frame frame;
5480   ix86_compute_frame_layout (&frame);
5481
5482   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5483     return frame.hard_frame_pointer_offset;
5484   else if (from == FRAME_POINTER_REGNUM
5485            && to == HARD_FRAME_POINTER_REGNUM)
5486     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5487   else
5488     {
5489       gcc_assert (to == STACK_POINTER_REGNUM);
5490
5491       if (from == ARG_POINTER_REGNUM)
5492         return frame.stack_pointer_offset;
5493
5494       gcc_assert (from == FRAME_POINTER_REGNUM);
5495       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5496     }
5497 }
5498
5499 /* Fill structure ix86_frame about frame of currently computed function.  */
5500
5501 static void
5502 ix86_compute_frame_layout (struct ix86_frame *frame)
5503 {
5504   HOST_WIDE_INT total_size;
5505   unsigned int stack_alignment_needed;
5506   HOST_WIDE_INT offset;
5507   unsigned int preferred_alignment;
5508   HOST_WIDE_INT size = get_frame_size ();
5509
5510   frame->nregs = ix86_nsaved_regs ();
5511   total_size = size;
5512
5513   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5514   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5515
5516   /* During reload iteration the amount of registers saved can change.
5517      Recompute the value as needed.  Do not recompute when amount of registers
5518      didn't change as reload does multiple calls to the function and does not
5519      expect the decision to change within single iteration.  */
5520   if (!optimize_size
5521       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5522     {
5523       int count = frame->nregs;
5524
5525       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5526       /* The fast prologue uses move instead of push to save registers.  This
5527          is significantly longer, but also executes faster as modern hardware
5528          can execute the moves in parallel, but can't do that for push/pop.
5529
5530          Be careful about choosing what prologue to emit:  When function takes
5531          many instructions to execute we may use slow version as well as in
5532          case function is known to be outside hot spot (this is known with
5533          feedback only).  Weight the size of function by number of registers
5534          to save as it is cheap to use one or two push instructions but very
5535          slow to use many of them.  */
5536       if (count)
5537         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5538       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5539           || (flag_branch_probabilities
5540               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5541         cfun->machine->use_fast_prologue_epilogue = false;
5542       else
5543         cfun->machine->use_fast_prologue_epilogue
5544            = !expensive_function_p (count);
5545     }
5546   if (TARGET_PROLOGUE_USING_MOVE
5547       && cfun->machine->use_fast_prologue_epilogue)
5548     frame->save_regs_using_mov = true;
5549   else
5550     frame->save_regs_using_mov = false;
5551
5552
5553   /* Skip return address and saved base pointer.  */
5554   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5555
5556   frame->hard_frame_pointer_offset = offset;
5557
5558   /* Do some sanity checking of stack_alignment_needed and
5559      preferred_alignment, since i386 port is the only using those features
5560      that may break easily.  */
5561
5562   gcc_assert (!size || stack_alignment_needed);
5563   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5564   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5565   gcc_assert (stack_alignment_needed
5566               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5567
5568   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5569     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5570
5571   /* Register save area */
5572   offset += frame->nregs * UNITS_PER_WORD;
5573
5574   /* Va-arg area */
5575   if (ix86_save_varrargs_registers)
5576     {
5577       offset += X86_64_VARARGS_SIZE;
5578       frame->va_arg_size = X86_64_VARARGS_SIZE;
5579     }
5580   else
5581     frame->va_arg_size = 0;
5582
5583   /* Align start of frame for local function.  */
5584   frame->padding1 = ((offset + stack_alignment_needed - 1)
5585                      & -stack_alignment_needed) - offset;
5586
5587   offset += frame->padding1;
5588
5589   /* Frame pointer points here.  */
5590   frame->frame_pointer_offset = offset;
5591
5592   offset += size;
5593
5594   /* Add outgoing arguments area.  Can be skipped if we eliminated
5595      all the function calls as dead code.
5596      Skipping is however impossible when function calls alloca.  Alloca
5597      expander assumes that last current_function_outgoing_args_size
5598      of stack frame are unused.  */
5599   if (ACCUMULATE_OUTGOING_ARGS
5600       && (!current_function_is_leaf || current_function_calls_alloca
5601           || ix86_current_function_calls_tls_descriptor))
5602     {
5603       offset += current_function_outgoing_args_size;
5604       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5605     }
5606   else
5607     frame->outgoing_arguments_size = 0;
5608
5609   /* Align stack boundary.  Only needed if we're calling another function
5610      or using alloca.  */
5611   if (!current_function_is_leaf || current_function_calls_alloca
5612       || ix86_current_function_calls_tls_descriptor)
5613     frame->padding2 = ((offset + preferred_alignment - 1)
5614                        & -preferred_alignment) - offset;
5615   else
5616     frame->padding2 = 0;
5617
5618   offset += frame->padding2;
5619
5620   /* We've reached end of stack frame.  */
5621   frame->stack_pointer_offset = offset;
5622
5623   /* Size prologue needs to allocate.  */
5624   frame->to_allocate =
5625     (size + frame->padding1 + frame->padding2
5626      + frame->outgoing_arguments_size + frame->va_arg_size);
5627
5628   if ((!frame->to_allocate && frame->nregs <= 1)
5629       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5630     frame->save_regs_using_mov = false;
5631
5632   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5633       && current_function_is_leaf
5634       && !ix86_current_function_calls_tls_descriptor)
5635     {
5636       frame->red_zone_size = frame->to_allocate;
5637       if (frame->save_regs_using_mov)
5638         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5639       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5640         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5641     }
5642   else
5643     frame->red_zone_size = 0;
5644   frame->to_allocate -= frame->red_zone_size;
5645   frame->stack_pointer_offset -= frame->red_zone_size;
5646 #if 0
5647   fprintf (stderr, "\n");
5648   fprintf (stderr, "nregs: %ld\n", (long)frame->nregs);
5649   fprintf (stderr, "size: %ld\n", (long)size);
5650   fprintf (stderr, "alignment1: %ld\n", (long)stack_alignment_needed);
5651   fprintf (stderr, "padding1: %ld\n", (long)frame->padding1);
5652   fprintf (stderr, "va_arg: %ld\n", (long)frame->va_arg_size);
5653   fprintf (stderr, "padding2: %ld\n", (long)frame->padding2);
5654   fprintf (stderr, "to_allocate: %ld\n", (long)frame->to_allocate);
5655   fprintf (stderr, "red_zone_size: %ld\n", (long)frame->red_zone_size);
5656   fprintf (stderr, "frame_pointer_offset: %ld\n", (long)frame->frame_pointer_offset);
5657   fprintf (stderr, "hard_frame_pointer_offset: %ld\n",
5658            (long)frame->hard_frame_pointer_offset);
5659   fprintf (stderr, "stack_pointer_offset: %ld\n", (long)frame->stack_pointer_offset);
5660   fprintf (stderr, "current_function_is_leaf: %ld\n", (long)current_function_is_leaf);
5661   fprintf (stderr, "current_function_calls_alloca: %ld\n", (long)current_function_calls_alloca);
5662   fprintf (stderr, "x86_current_function_calls_tls_descriptor: %ld\n", (long)ix86_current_function_calls_tls_descriptor);
5663 #endif
5664 }
5665
5666 /* Emit code to save registers in the prologue.  */
5667
5668 static void
5669 ix86_emit_save_regs (void)
5670 {
5671   unsigned int regno;
5672   rtx insn;
5673
5674   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5675     if (ix86_save_reg (regno, true))
5676       {
5677         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5678         RTX_FRAME_RELATED_P (insn) = 1;
5679       }
5680 }
5681
5682 /* Emit code to save registers using MOV insns.  First register
5683    is restored from POINTER + OFFSET.  */
5684 static void
5685 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5686 {
5687   unsigned int regno;
5688   rtx insn;
5689
5690   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5691     if (ix86_save_reg (regno, true))
5692       {
5693         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5694                                                Pmode, offset),
5695                                gen_rtx_REG (Pmode, regno));
5696         RTX_FRAME_RELATED_P (insn) = 1;
5697         offset += UNITS_PER_WORD;
5698       }
5699 }
5700
5701 /* Expand prologue or epilogue stack adjustment.
5702    The pattern exist to put a dependency on all ebp-based memory accesses.
5703    STYLE should be negative if instructions should be marked as frame related,
5704    zero if %r11 register is live and cannot be freely used and positive
5705    otherwise.  */
5706
5707 static void
5708 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5709 {
5710   rtx insn;
5711
5712   if (! TARGET_64BIT)
5713     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5714   else if (x86_64_immediate_operand (offset, DImode))
5715     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5716   else
5717     {
5718       rtx r11;
5719       /* r11 is used by indirect sibcall return as well, set before the
5720          epilogue and used after the epilogue.  ATM indirect sibcall
5721          shouldn't be used together with huge frame sizes in one
5722          function because of the frame_size check in sibcall.c.  */
5723       gcc_assert (style);
5724       r11 = gen_rtx_REG (DImode, R11_REG);
5725       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5726       if (style < 0)
5727         RTX_FRAME_RELATED_P (insn) = 1;
5728       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5729                                                                offset));
5730     }
5731   if (style < 0)
5732     RTX_FRAME_RELATED_P (insn) = 1;
5733 }
5734
5735 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5736
5737 static rtx
5738 ix86_internal_arg_pointer (void)
5739 {
5740   bool has_force_align_arg_pointer =
5741     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5742                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5743   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5744        && DECL_NAME (current_function_decl)
5745        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5746        && DECL_FILE_SCOPE_P (current_function_decl))
5747       || ix86_force_align_arg_pointer
5748       || has_force_align_arg_pointer)
5749     {
5750       /* Nested functions can't realign the stack due to a register
5751          conflict.  */
5752       if (DECL_CONTEXT (current_function_decl)
5753           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5754         {
5755           if (ix86_force_align_arg_pointer)
5756             warning (0, "-mstackrealign ignored for nested functions");
5757           if (has_force_align_arg_pointer)
5758             error ("%s not supported for nested functions",
5759                    ix86_force_align_arg_pointer_string);
5760           return virtual_incoming_args_rtx;
5761         }
5762       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5763       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5764     }
5765   else
5766     return virtual_incoming_args_rtx;
5767 }
5768
5769 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5770    This is called from dwarf2out.c to emit call frame instructions
5771    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5772 static void
5773 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5774 {
5775   rtx unspec = SET_SRC (pattern);
5776   gcc_assert (GET_CODE (unspec) == UNSPEC);
5777
5778   switch (index)
5779     {
5780     case UNSPEC_REG_SAVE:
5781       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5782                               SET_DEST (pattern));
5783       break;
5784     case UNSPEC_DEF_CFA:
5785       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5786                          INTVAL (XVECEXP (unspec, 0, 0)));
5787       break;
5788     default:
5789       gcc_unreachable ();
5790     }
5791 }
5792
5793 /* Expand the prologue into a bunch of separate insns.  */
5794
5795 void
5796 ix86_expand_prologue (void)
5797 {
5798   rtx insn;
5799   bool pic_reg_used;
5800   struct ix86_frame frame;
5801   HOST_WIDE_INT allocate;
5802
5803   ix86_compute_frame_layout (&frame);
5804
5805   if (cfun->machine->force_align_arg_pointer)
5806     {
5807       rtx x, y;
5808
5809       /* Grab the argument pointer.  */
5810       x = plus_constant (stack_pointer_rtx, 4);
5811       y = cfun->machine->force_align_arg_pointer;
5812       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5813       RTX_FRAME_RELATED_P (insn) = 1;
5814
5815       /* The unwind info consists of two parts: install the fafp as the cfa,
5816          and record the fafp as the "save register" of the stack pointer.
5817          The later is there in order that the unwinder can see where it
5818          should restore the stack pointer across the and insn.  */
5819       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5820       x = gen_rtx_SET (VOIDmode, y, x);
5821       RTX_FRAME_RELATED_P (x) = 1;
5822       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5823                           UNSPEC_REG_SAVE);
5824       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5825       RTX_FRAME_RELATED_P (y) = 1;
5826       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5827       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5828       REG_NOTES (insn) = x;
5829
5830       /* Align the stack.  */
5831       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5832                              GEN_INT (-16)));
5833
5834       /* And here we cheat like madmen with the unwind info.  We force the
5835          cfa register back to sp+4, which is exactly what it was at the
5836          start of the function.  Re-pushing the return address results in
5837          the return at the same spot relative to the cfa, and thus is
5838          correct wrt the unwind info.  */
5839       x = cfun->machine->force_align_arg_pointer;
5840       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5841       insn = emit_insn (gen_push (x));
5842       RTX_FRAME_RELATED_P (insn) = 1;
5843
5844       x = GEN_INT (4);
5845       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5846       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5847       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5848       REG_NOTES (insn) = x;
5849     }
5850
5851   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5852      slower on all targets.  Also sdb doesn't like it.  */
5853
5854   if (frame_pointer_needed)
5855     {
5856       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5857       RTX_FRAME_RELATED_P (insn) = 1;
5858
5859       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5860       RTX_FRAME_RELATED_P (insn) = 1;
5861     }
5862
5863   allocate = frame.to_allocate;
5864
5865   if (!frame.save_regs_using_mov)
5866     ix86_emit_save_regs ();
5867   else
5868     allocate += frame.nregs * UNITS_PER_WORD;
5869
5870   /* When using red zone we may start register saving before allocating
5871      the stack frame saving one cycle of the prologue.  */
5872   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5873     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5874                                    : stack_pointer_rtx,
5875                                    -frame.nregs * UNITS_PER_WORD);
5876
5877   if (allocate == 0)
5878     ;
5879   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5880     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5881                                GEN_INT (-allocate), -1);
5882   else
5883     {
5884       /* Only valid for Win32.  */
5885       rtx eax = gen_rtx_REG (Pmode, 0);
5886       bool eax_live;
5887       rtx t;
5888
5889       gcc_assert (!TARGET_64BIT || TARGET_64BIT_MS_ABI);
5890
5891       if (TARGET_64BIT_MS_ABI)
5892         eax_live = false;
5893       else
5894         eax_live = ix86_eax_live_at_start_p ();
5895
5896       if (eax_live)
5897         {
5898           emit_insn (gen_push (eax));
5899           allocate -= UNITS_PER_WORD;
5900         }
5901
5902       emit_move_insn (eax, GEN_INT (allocate));
5903
5904       if (TARGET_64BIT)
5905         insn = gen_allocate_stack_worker_64 (eax);
5906       else
5907         insn = gen_allocate_stack_worker_32 (eax);
5908       insn = emit_insn (insn);
5909       RTX_FRAME_RELATED_P (insn) = 1;
5910       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5911       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5912       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5913                                             t, REG_NOTES (insn));
5914
5915       if (eax_live)
5916         {
5917           if (frame_pointer_needed)
5918             t = plus_constant (hard_frame_pointer_rtx,
5919                                allocate
5920                                - frame.to_allocate
5921                                - frame.nregs * UNITS_PER_WORD);
5922           else
5923             t = plus_constant (stack_pointer_rtx, allocate);
5924           emit_move_insn (eax, gen_rtx_MEM (Pmode, t));
5925         }
5926     }
5927
5928   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5929     {
5930       if (!frame_pointer_needed || !frame.to_allocate)
5931         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5932       else
5933         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5934                                        -frame.nregs * UNITS_PER_WORD);
5935     }
5936
5937   pic_reg_used = false;
5938   if (pic_offset_table_rtx
5939       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5940           || current_function_profile))
5941     {
5942       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5943
5944       if (alt_pic_reg_used != INVALID_REGNUM)
5945         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5946
5947       pic_reg_used = true;
5948     }
5949
5950   if (pic_reg_used)
5951     {
5952       if (TARGET_64BIT)
5953         {
5954           if (ix86_cmodel == CM_LARGE_PIC)
5955             {
5956               rtx tmp_reg = gen_rtx_REG (DImode,
5957                                          FIRST_REX_INT_REG + 3 /* R11 */);
5958               rtx label = gen_label_rtx ();
5959               emit_label (label);
5960               LABEL_PRESERVE_P (label) = 1;
5961               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
5962               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
5963               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5964               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
5965               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5966               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
5967                                             pic_offset_table_rtx, tmp_reg));
5968             }
5969           else
5970             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5971         }
5972       else
5973         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5974
5975       /* Even with accurate pre-reload life analysis, we can wind up
5976          deleting all references to the pic register after reload.
5977          Consider if cross-jumping unifies two sides of a branch
5978          controlled by a comparison vs the only read from a global.
5979          In which case, allow the set_got to be deleted, though we're
5980          too late to do anything about the ebx save in the prologue.  */
5981       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5982     }
5983
5984   /* Prevent function calls from be scheduled before the call to mcount.
5985      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5986   if (current_function_profile)
5987     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5988 }
5989
5990 /* Emit code to restore saved registers using MOV insns.  First register
5991    is restored from POINTER + OFFSET.  */
5992 static void
5993 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5994                                   int maybe_eh_return)
5995 {
5996   int regno;
5997   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5998
5999   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6000     if (ix86_save_reg (regno, maybe_eh_return))
6001       {
6002         /* Ensure that adjust_address won't be forced to produce pointer
6003            out of range allowed by x86-64 instruction set.  */
6004         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
6005           {
6006             rtx r11;
6007
6008             r11 = gen_rtx_REG (DImode, R11_REG);
6009             emit_move_insn (r11, GEN_INT (offset));
6010             emit_insn (gen_adddi3 (r11, r11, pointer));
6011             base_address = gen_rtx_MEM (Pmode, r11);
6012             offset = 0;
6013           }
6014         emit_move_insn (gen_rtx_REG (Pmode, regno),
6015                         adjust_address (base_address, Pmode, offset));
6016         offset += UNITS_PER_WORD;
6017       }
6018 }
6019
6020 /* Restore function stack, frame, and registers.  */
6021
6022 void
6023 ix86_expand_epilogue (int style)
6024 {
6025   int regno;
6026   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
6027   struct ix86_frame frame;
6028   HOST_WIDE_INT offset;
6029
6030   ix86_compute_frame_layout (&frame);
6031
6032   /* Calculate start of saved registers relative to ebp.  Special care
6033      must be taken for the normal return case of a function using
6034      eh_return: the eax and edx registers are marked as saved, but not
6035      restored along this path.  */
6036   offset = frame.nregs;
6037   if (current_function_calls_eh_return && style != 2)
6038     offset -= 2;
6039   offset *= -UNITS_PER_WORD;
6040
6041   /* If we're only restoring one register and sp is not valid then
6042      using a move instruction to restore the register since it's
6043      less work than reloading sp and popping the register.
6044
6045      The default code result in stack adjustment using add/lea instruction,
6046      while this code results in LEAVE instruction (or discrete equivalent),
6047      so it is profitable in some other cases as well.  Especially when there
6048      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
6049      and there is exactly one register to pop. This heuristic may need some
6050      tuning in future.  */
6051   if ((!sp_valid && frame.nregs <= 1)
6052       || (TARGET_EPILOGUE_USING_MOVE
6053           && cfun->machine->use_fast_prologue_epilogue
6054           && (frame.nregs > 1 || frame.to_allocate))
6055       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
6056       || (frame_pointer_needed && TARGET_USE_LEAVE
6057           && cfun->machine->use_fast_prologue_epilogue
6058           && frame.nregs == 1)
6059       || current_function_calls_eh_return)
6060     {
6061       /* Restore registers.  We can use ebp or esp to address the memory
6062          locations.  If both are available, default to ebp, since offsets
6063          are known to be small.  Only exception is esp pointing directly to the
6064          end of block of saved registers, where we may simplify addressing
6065          mode.  */
6066
6067       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
6068         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
6069                                           frame.to_allocate, style == 2);
6070       else
6071         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
6072                                           offset, style == 2);
6073
6074       /* eh_return epilogues need %ecx added to the stack pointer.  */
6075       if (style == 2)
6076         {
6077           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
6078
6079           if (frame_pointer_needed)
6080             {
6081               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
6082               tmp = plus_constant (tmp, UNITS_PER_WORD);
6083               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
6084
6085               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
6086               emit_move_insn (hard_frame_pointer_rtx, tmp);
6087
6088               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
6089                                          const0_rtx, style);
6090             }
6091           else
6092             {
6093               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
6094               tmp = plus_constant (tmp, (frame.to_allocate
6095                                          + frame.nregs * UNITS_PER_WORD));
6096               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
6097             }
6098         }
6099       else if (!frame_pointer_needed)
6100         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6101                                    GEN_INT (frame.to_allocate
6102                                             + frame.nregs * UNITS_PER_WORD),
6103                                    style);
6104       /* If not an i386, mov & pop is faster than "leave".  */
6105       else if (TARGET_USE_LEAVE || optimize_size
6106                || !cfun->machine->use_fast_prologue_epilogue)
6107         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6108       else
6109         {
6110           pro_epilogue_adjust_stack (stack_pointer_rtx,
6111                                      hard_frame_pointer_rtx,
6112                                      const0_rtx, style);
6113           if (TARGET_64BIT)
6114             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6115           else
6116             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6117         }
6118     }
6119   else
6120     {
6121       /* First step is to deallocate the stack frame so that we can
6122          pop the registers.  */
6123       if (!sp_valid)
6124         {
6125           gcc_assert (frame_pointer_needed);
6126           pro_epilogue_adjust_stack (stack_pointer_rtx,
6127                                      hard_frame_pointer_rtx,
6128                                      GEN_INT (offset), style);
6129         }
6130       else if (frame.to_allocate)
6131         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6132                                    GEN_INT (frame.to_allocate), style);
6133
6134       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6135         if (ix86_save_reg (regno, false))
6136           {
6137             if (TARGET_64BIT)
6138               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
6139             else
6140               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
6141           }
6142       if (frame_pointer_needed)
6143         {
6144           /* Leave results in shorter dependency chains on CPUs that are
6145              able to grok it fast.  */
6146           if (TARGET_USE_LEAVE)
6147             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6148           else if (TARGET_64BIT)
6149             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6150           else
6151             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6152         }
6153     }
6154
6155   if (cfun->machine->force_align_arg_pointer)
6156     {
6157       emit_insn (gen_addsi3 (stack_pointer_rtx,
6158                              cfun->machine->force_align_arg_pointer,
6159                              GEN_INT (-4)));
6160     }
6161
6162   /* Sibcall epilogues don't want a return instruction.  */
6163   if (style == 0)
6164     return;
6165
6166   if (current_function_pops_args && current_function_args_size)
6167     {
6168       rtx popc = GEN_INT (current_function_pops_args);
6169
6170       /* i386 can only pop 64K bytes.  If asked to pop more, pop
6171          return address, do explicit add, and jump indirectly to the
6172          caller.  */
6173
6174       if (current_function_pops_args >= 65536)
6175         {
6176           rtx ecx = gen_rtx_REG (SImode, 2);
6177
6178           /* There is no "pascal" calling convention in any 64bit ABI.  */
6179           gcc_assert (!TARGET_64BIT);
6180
6181           emit_insn (gen_popsi1 (ecx));
6182           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
6183           emit_jump_insn (gen_return_indirect_internal (ecx));
6184         }
6185       else
6186         emit_jump_insn (gen_return_pop_internal (popc));
6187     }
6188   else
6189     emit_jump_insn (gen_return_internal ());
6190 }
6191
6192 /* Reset from the function's potential modifications.  */
6193
6194 static void
6195 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6196                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6197 {
6198   if (pic_offset_table_rtx)
6199     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
6200 #if TARGET_MACHO
6201   /* Mach-O doesn't support labels at the end of objects, so if
6202      it looks like we might want one, insert a NOP.  */
6203   {
6204     rtx insn = get_last_insn ();
6205     while (insn
6206            && NOTE_P (insn)
6207            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
6208       insn = PREV_INSN (insn);
6209     if (insn
6210         && (LABEL_P (insn)
6211             || (NOTE_P (insn)
6212                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
6213       fputs ("\tnop\n", file);
6214   }
6215 #endif
6216
6217 }
6218 \f
6219 /* Extract the parts of an RTL expression that is a valid memory address
6220    for an instruction.  Return 0 if the structure of the address is
6221    grossly off.  Return -1 if the address contains ASHIFT, so it is not
6222    strictly valid, but still used for computing length of lea instruction.  */
6223
6224 int
6225 ix86_decompose_address (rtx addr, struct ix86_address *out)
6226 {
6227   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
6228   rtx base_reg, index_reg;
6229   HOST_WIDE_INT scale = 1;
6230   rtx scale_rtx = NULL_RTX;
6231   int retval = 1;
6232   enum ix86_address_seg seg = SEG_DEFAULT;
6233
6234   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
6235     base = addr;
6236   else if (GET_CODE (addr) == PLUS)
6237     {
6238       rtx addends[4], op;
6239       int n = 0, i;
6240
6241       op = addr;
6242       do
6243         {
6244           if (n >= 4)
6245             return 0;
6246           addends[n++] = XEXP (op, 1);
6247           op = XEXP (op, 0);
6248         }
6249       while (GET_CODE (op) == PLUS);
6250       if (n >= 4)
6251         return 0;
6252       addends[n] = op;
6253
6254       for (i = n; i >= 0; --i)
6255         {
6256           op = addends[i];
6257           switch (GET_CODE (op))
6258             {
6259             case MULT:
6260               if (index)
6261                 return 0;
6262               index = XEXP (op, 0);
6263               scale_rtx = XEXP (op, 1);
6264               break;
6265
6266             case UNSPEC:
6267               if (XINT (op, 1) == UNSPEC_TP
6268                   && TARGET_TLS_DIRECT_SEG_REFS
6269                   && seg == SEG_DEFAULT)
6270                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
6271               else
6272                 return 0;
6273               break;
6274
6275             case REG:
6276             case SUBREG:
6277               if (!base)
6278                 base = op;
6279               else if (!index)
6280                 index = op;
6281               else
6282                 return 0;
6283               break;
6284
6285             case CONST:
6286             case CONST_INT:
6287             case SYMBOL_REF:
6288             case LABEL_REF:
6289               if (disp)
6290                 return 0;
6291               disp = op;
6292               break;
6293
6294             default:
6295               return 0;
6296             }
6297         }
6298     }
6299   else if (GET_CODE (addr) == MULT)
6300     {
6301       index = XEXP (addr, 0);           /* index*scale */
6302       scale_rtx = XEXP (addr, 1);
6303     }
6304   else if (GET_CODE (addr) == ASHIFT)
6305     {
6306       rtx tmp;
6307
6308       /* We're called for lea too, which implements ashift on occasion.  */
6309       index = XEXP (addr, 0);
6310       tmp = XEXP (addr, 1);
6311       if (!CONST_INT_P (tmp))
6312         return 0;
6313       scale = INTVAL (tmp);
6314       if ((unsigned HOST_WIDE_INT) scale > 3)
6315         return 0;
6316       scale = 1 << scale;
6317       retval = -1;
6318     }
6319   else
6320     disp = addr;                        /* displacement */
6321
6322   /* Extract the integral value of scale.  */
6323   if (scale_rtx)
6324     {
6325       if (!CONST_INT_P (scale_rtx))
6326         return 0;
6327       scale = INTVAL (scale_rtx);
6328     }
6329
6330   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
6331   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
6332
6333   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
6334   if (base_reg && index_reg && scale == 1
6335       && (index_reg == arg_pointer_rtx
6336           || index_reg == frame_pointer_rtx
6337           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
6338     {
6339       rtx tmp;
6340       tmp = base, base = index, index = tmp;
6341       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
6342     }
6343
6344   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
6345   if ((base_reg == hard_frame_pointer_rtx
6346        || base_reg == frame_pointer_rtx
6347        || base_reg == arg_pointer_rtx) && !disp)
6348     disp = const0_rtx;
6349
6350   /* Special case: on K6, [%esi] makes the instruction vector decoded.
6351      Avoid this by transforming to [%esi+0].  */
6352   if (ix86_tune == PROCESSOR_K6 && !optimize_size
6353       && base_reg && !index_reg && !disp
6354       && REG_P (base_reg)
6355       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
6356     disp = const0_rtx;
6357
6358   /* Special case: encode reg+reg instead of reg*2.  */
6359   if (!base && index && scale && scale == 2)
6360     base = index, base_reg = index_reg, scale = 1;
6361
6362   /* Special case: scaling cannot be encoded without base or displacement.  */
6363   if (!base && !disp && index && scale != 1)
6364     disp = const0_rtx;
6365
6366   out->base = base;
6367   out->index = index;
6368   out->disp = disp;
6369   out->scale = scale;
6370   out->seg = seg;
6371
6372   return retval;
6373 }
6374 \f
6375 /* Return cost of the memory address x.
6376    For i386, it is better to use a complex address than let gcc copy
6377    the address into a reg and make a new pseudo.  But not if the address
6378    requires to two regs - that would mean more pseudos with longer
6379    lifetimes.  */
6380 static int
6381 ix86_address_cost (rtx x)
6382 {
6383   struct ix86_address parts;
6384   int cost = 1;
6385   int ok = ix86_decompose_address (x, &parts);
6386
6387   gcc_assert (ok);
6388
6389   if (parts.base && GET_CODE (parts.base) == SUBREG)
6390     parts.base = SUBREG_REG (parts.base);
6391   if (parts.index && GET_CODE (parts.index) == SUBREG)
6392     parts.index = SUBREG_REG (parts.index);
6393
6394   /* More complex memory references are better.  */
6395   if (parts.disp && parts.disp != const0_rtx)
6396     cost--;
6397   if (parts.seg != SEG_DEFAULT)
6398     cost--;
6399
6400   /* Attempt to minimize number of registers in the address.  */
6401   if ((parts.base
6402        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
6403       || (parts.index
6404           && (!REG_P (parts.index)
6405               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
6406     cost++;
6407
6408   if (parts.base
6409       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
6410       && parts.index
6411       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
6412       && parts.base != parts.index)
6413     cost++;
6414
6415   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
6416      since it's predecode logic can't detect the length of instructions
6417      and it degenerates to vector decoded.  Increase cost of such
6418      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
6419      to split such addresses or even refuse such addresses at all.
6420
6421      Following addressing modes are affected:
6422       [base+scale*index]
6423       [scale*index+disp]
6424       [base+index]
6425
6426      The first and last case  may be avoidable by explicitly coding the zero in
6427      memory address, but I don't have AMD-K6 machine handy to check this
6428      theory.  */
6429
6430   if (TARGET_K6
6431       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
6432           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
6433           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
6434     cost += 10;
6435
6436   return cost;
6437 }
6438 \f
6439 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6440    this is used for to form addresses to local data when -fPIC is in
6441    use.  */
6442
6443 static bool
6444 darwin_local_data_pic (rtx disp)
6445 {
6446   if (GET_CODE (disp) == MINUS)
6447     {
6448       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6449           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6450         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6451           {
6452             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6453             if (! strcmp (sym_name, "<pic base>"))
6454               return true;
6455           }
6456     }
6457
6458   return false;
6459 }
6460
6461 /* Determine if a given RTX is a valid constant.  We already know this
6462    satisfies CONSTANT_P.  */
6463
6464 bool
6465 legitimate_constant_p (rtx x)
6466 {
6467   switch (GET_CODE (x))
6468     {
6469     case CONST:
6470       x = XEXP (x, 0);
6471
6472       if (GET_CODE (x) == PLUS)
6473         {
6474           if (!CONST_INT_P (XEXP (x, 1)))
6475             return false;
6476           x = XEXP (x, 0);
6477         }
6478
6479       if (TARGET_MACHO && darwin_local_data_pic (x))
6480         return true;
6481
6482       /* Only some unspecs are valid as "constants".  */
6483       if (GET_CODE (x) == UNSPEC)
6484         switch (XINT (x, 1))
6485           {
6486           case UNSPEC_GOT:
6487           case UNSPEC_GOTOFF:
6488           case UNSPEC_PLTOFF:
6489             return TARGET_64BIT;
6490           case UNSPEC_TPOFF:
6491           case UNSPEC_NTPOFF:
6492             x = XVECEXP (x, 0, 0);
6493             return (GET_CODE (x) == SYMBOL_REF
6494                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6495           case UNSPEC_DTPOFF:
6496             x = XVECEXP (x, 0, 0);
6497             return (GET_CODE (x) == SYMBOL_REF
6498                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6499           default:
6500             return false;
6501           }
6502
6503       /* We must have drilled down to a symbol.  */
6504       if (GET_CODE (x) == LABEL_REF)
6505         return true;
6506       if (GET_CODE (x) != SYMBOL_REF)
6507         return false;
6508       /* FALLTHRU */
6509
6510     case SYMBOL_REF:
6511       /* TLS symbols are never valid.  */
6512       if (SYMBOL_REF_TLS_MODEL (x))
6513         return false;
6514
6515       /* DLLIMPORT symbols are never valid.  */
6516       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6517           && SYMBOL_REF_DLLIMPORT_P (x))
6518         return false;
6519       break;
6520
6521     case CONST_DOUBLE:
6522       if (GET_MODE (x) == TImode
6523           && x != CONST0_RTX (TImode)
6524           && !TARGET_64BIT)
6525         return false;
6526       break;
6527
6528     case CONST_VECTOR:
6529       if (x == CONST0_RTX (GET_MODE (x)))
6530         return true;
6531       return false;
6532
6533     default:
6534       break;
6535     }
6536
6537   /* Otherwise we handle everything else in the move patterns.  */
6538   return true;
6539 }
6540
6541 /* Determine if it's legal to put X into the constant pool.  This
6542    is not possible for the address of thread-local symbols, which
6543    is checked above.  */
6544
6545 static bool
6546 ix86_cannot_force_const_mem (rtx x)
6547 {
6548   /* We can always put integral constants and vectors in memory.  */
6549   switch (GET_CODE (x))
6550     {
6551     case CONST_INT:
6552     case CONST_DOUBLE:
6553     case CONST_VECTOR:
6554       return false;
6555
6556     default:
6557       break;
6558     }
6559   return !legitimate_constant_p (x);
6560 }
6561
6562 /* Determine if a given RTX is a valid constant address.  */
6563
6564 bool
6565 constant_address_p (rtx x)
6566 {
6567   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6568 }
6569
6570 /* Nonzero if the constant value X is a legitimate general operand
6571    when generating PIC code.  It is given that flag_pic is on and
6572    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6573
6574 bool
6575 legitimate_pic_operand_p (rtx x)
6576 {
6577   rtx inner;
6578
6579   switch (GET_CODE (x))
6580     {
6581     case CONST:
6582       inner = XEXP (x, 0);
6583       if (GET_CODE (inner) == PLUS
6584           && CONST_INT_P (XEXP (inner, 1)))
6585         inner = XEXP (inner, 0);
6586
6587       /* Only some unspecs are valid as "constants".  */
6588       if (GET_CODE (inner) == UNSPEC)
6589         switch (XINT (inner, 1))
6590           {
6591           case UNSPEC_GOT:
6592           case UNSPEC_GOTOFF:
6593           case UNSPEC_PLTOFF:
6594             return TARGET_64BIT;
6595           case UNSPEC_TPOFF:
6596             x = XVECEXP (inner, 0, 0);
6597             return (GET_CODE (x) == SYMBOL_REF
6598                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6599           default:
6600             return false;
6601           }
6602       /* FALLTHRU */
6603
6604     case SYMBOL_REF:
6605     case LABEL_REF:
6606       return legitimate_pic_address_disp_p (x);
6607
6608     default:
6609       return true;
6610     }
6611 }
6612
6613 /* Determine if a given CONST RTX is a valid memory displacement
6614    in PIC mode.  */
6615
6616 int
6617 legitimate_pic_address_disp_p (rtx disp)
6618 {
6619   bool saw_plus;
6620
6621   /* In 64bit mode we can allow direct addresses of symbols and labels
6622      when they are not dynamic symbols.  */
6623   if (TARGET_64BIT)
6624     {
6625       rtx op0 = disp, op1;
6626
6627       switch (GET_CODE (disp))
6628         {
6629         case LABEL_REF:
6630           return true;
6631
6632         case CONST:
6633           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6634             break;
6635           op0 = XEXP (XEXP (disp, 0), 0);
6636           op1 = XEXP (XEXP (disp, 0), 1);
6637           if (!CONST_INT_P (op1)
6638               || INTVAL (op1) >= 16*1024*1024
6639               || INTVAL (op1) < -16*1024*1024)
6640             break;
6641           if (GET_CODE (op0) == LABEL_REF)
6642             return true;
6643           if (GET_CODE (op0) != SYMBOL_REF)
6644             break;
6645           /* FALLTHRU */
6646
6647         case SYMBOL_REF:
6648           /* TLS references should always be enclosed in UNSPEC.  */
6649           if (SYMBOL_REF_TLS_MODEL (op0))
6650             return false;
6651           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
6652               && ix86_cmodel != CM_LARGE_PIC)
6653             return true;
6654           break;
6655
6656         default:
6657           break;
6658         }
6659     }
6660   if (GET_CODE (disp) != CONST)
6661     return 0;
6662   disp = XEXP (disp, 0);
6663
6664   if (TARGET_64BIT)
6665     {
6666       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6667          of GOT tables.  We should not need these anyway.  */
6668       if (GET_CODE (disp) != UNSPEC
6669           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6670               && XINT (disp, 1) != UNSPEC_GOTOFF
6671               && XINT (disp, 1) != UNSPEC_PLTOFF))
6672         return 0;
6673
6674       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6675           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6676         return 0;
6677       return 1;
6678     }
6679
6680   saw_plus = false;
6681   if (GET_CODE (disp) == PLUS)
6682     {
6683       if (!CONST_INT_P (XEXP (disp, 1)))
6684         return 0;
6685       disp = XEXP (disp, 0);
6686       saw_plus = true;
6687     }
6688
6689   if (TARGET_MACHO && darwin_local_data_pic (disp))
6690     return 1;
6691
6692   if (GET_CODE (disp) != UNSPEC)
6693     return 0;
6694
6695   switch (XINT (disp, 1))
6696     {
6697     case UNSPEC_GOT:
6698       if (saw_plus)
6699         return false;
6700       /* We need to check for both symbols and labels because VxWorks loads
6701          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
6702          details.  */
6703       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6704               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
6705     case UNSPEC_GOTOFF:
6706       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6707          While ABI specify also 32bit relocation but we don't produce it in
6708          small PIC model at all.  */
6709       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6710            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6711           && !TARGET_64BIT)
6712         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
6713       return false;
6714     case UNSPEC_GOTTPOFF:
6715     case UNSPEC_GOTNTPOFF:
6716     case UNSPEC_INDNTPOFF:
6717       if (saw_plus)
6718         return false;
6719       disp = XVECEXP (disp, 0, 0);
6720       return (GET_CODE (disp) == SYMBOL_REF
6721               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6722     case UNSPEC_NTPOFF:
6723       disp = XVECEXP (disp, 0, 0);
6724       return (GET_CODE (disp) == SYMBOL_REF
6725               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6726     case UNSPEC_DTPOFF:
6727       disp = XVECEXP (disp, 0, 0);
6728       return (GET_CODE (disp) == SYMBOL_REF
6729               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6730     }
6731
6732   return 0;
6733 }
6734
6735 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6736    memory address for an instruction.  The MODE argument is the machine mode
6737    for the MEM expression that wants to use this address.
6738
6739    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6740    convert common non-canonical forms to canonical form so that they will
6741    be recognized.  */
6742
6743 int
6744 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
6745                       rtx addr, int strict)
6746 {
6747   struct ix86_address parts;
6748   rtx base, index, disp;
6749   HOST_WIDE_INT scale;
6750   const char *reason = NULL;
6751   rtx reason_rtx = NULL_RTX;
6752
6753   if (ix86_decompose_address (addr, &parts) <= 0)
6754     {
6755       reason = "decomposition failed";
6756       goto report_error;
6757     }
6758
6759   base = parts.base;
6760   index = parts.index;
6761   disp = parts.disp;
6762   scale = parts.scale;
6763
6764   /* Validate base register.
6765
6766      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6767      failures when the base is one word out of a two word structure, which is
6768      represented internally as a DImode int.  */
6769
6770   if (base)
6771     {
6772       rtx reg;
6773       reason_rtx = base;
6774
6775       if (REG_P (base))
6776         reg = base;
6777       else if (GET_CODE (base) == SUBREG
6778                && REG_P (SUBREG_REG (base))
6779                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6780                   <= UNITS_PER_WORD)
6781         reg = SUBREG_REG (base);
6782       else
6783         {
6784           reason = "base is not a register";
6785           goto report_error;
6786         }
6787
6788       if (GET_MODE (base) != Pmode)
6789         {
6790           reason = "base is not in Pmode";
6791           goto report_error;
6792         }
6793
6794       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6795           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6796         {
6797           reason = "base is not valid";
6798           goto report_error;
6799         }
6800     }
6801
6802   /* Validate index register.
6803
6804      Don't allow SUBREG's that span more than a word here -- same as above.  */
6805
6806   if (index)
6807     {
6808       rtx reg;
6809       reason_rtx = index;
6810
6811       if (REG_P (index))
6812         reg = index;
6813       else if (GET_CODE (index) == SUBREG
6814                && REG_P (SUBREG_REG (index))
6815                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6816                   <= UNITS_PER_WORD)
6817         reg = SUBREG_REG (index);
6818       else
6819         {
6820           reason = "index is not a register";
6821           goto report_error;
6822         }
6823
6824       if (GET_MODE (index) != Pmode)
6825         {
6826           reason = "index is not in Pmode";
6827           goto report_error;
6828         }
6829
6830       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6831           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6832         {
6833           reason = "index is not valid";
6834           goto report_error;
6835         }
6836     }
6837
6838   /* Validate scale factor.  */
6839   if (scale != 1)
6840     {
6841       reason_rtx = GEN_INT (scale);
6842       if (!index)
6843         {
6844           reason = "scale without index";
6845           goto report_error;
6846         }
6847
6848       if (scale != 2 && scale != 4 && scale != 8)
6849         {
6850           reason = "scale is not a valid multiplier";
6851           goto report_error;
6852         }
6853     }
6854
6855   /* Validate displacement.  */
6856   if (disp)
6857     {
6858       reason_rtx = disp;
6859
6860       if (GET_CODE (disp) == CONST
6861           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6862         switch (XINT (XEXP (disp, 0), 1))
6863           {
6864           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6865              used.  While ABI specify also 32bit relocations, we don't produce
6866              them at all and use IP relative instead.  */
6867           case UNSPEC_GOT:
6868           case UNSPEC_GOTOFF:
6869             gcc_assert (flag_pic);
6870             if (!TARGET_64BIT)
6871               goto is_legitimate_pic;
6872             reason = "64bit address unspec";
6873             goto report_error;
6874
6875           case UNSPEC_GOTPCREL:
6876             gcc_assert (flag_pic);
6877             goto is_legitimate_pic;
6878
6879           case UNSPEC_GOTTPOFF:
6880           case UNSPEC_GOTNTPOFF:
6881           case UNSPEC_INDNTPOFF:
6882           case UNSPEC_NTPOFF:
6883           case UNSPEC_DTPOFF:
6884             break;
6885
6886           default:
6887             reason = "invalid address unspec";
6888             goto report_error;
6889           }
6890
6891       else if (SYMBOLIC_CONST (disp)
6892                && (flag_pic
6893                    || (TARGET_MACHO
6894 #if TARGET_MACHO
6895                        && MACHOPIC_INDIRECT
6896                        && !machopic_operand_p (disp)
6897 #endif
6898                )))
6899         {
6900
6901         is_legitimate_pic:
6902           if (TARGET_64BIT && (index || base))
6903             {
6904               /* foo@dtpoff(%rX) is ok.  */
6905               if (GET_CODE (disp) != CONST
6906                   || GET_CODE (XEXP (disp, 0)) != PLUS
6907                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6908                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
6909                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6910                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6911                 {
6912                   reason = "non-constant pic memory reference";
6913                   goto report_error;
6914                 }
6915             }
6916           else if (! legitimate_pic_address_disp_p (disp))
6917             {
6918               reason = "displacement is an invalid pic construct";
6919               goto report_error;
6920             }
6921
6922           /* This code used to verify that a symbolic pic displacement
6923              includes the pic_offset_table_rtx register.
6924
6925              While this is good idea, unfortunately these constructs may
6926              be created by "adds using lea" optimization for incorrect
6927              code like:
6928
6929              int a;
6930              int foo(int i)
6931                {
6932                  return *(&a+i);
6933                }
6934
6935              This code is nonsensical, but results in addressing
6936              GOT table with pic_offset_table_rtx base.  We can't
6937              just refuse it easily, since it gets matched by
6938              "addsi3" pattern, that later gets split to lea in the
6939              case output register differs from input.  While this
6940              can be handled by separate addsi pattern for this case
6941              that never results in lea, this seems to be easier and
6942              correct fix for crash to disable this test.  */
6943         }
6944       else if (GET_CODE (disp) != LABEL_REF
6945                && !CONST_INT_P (disp)
6946                && (GET_CODE (disp) != CONST
6947                    || !legitimate_constant_p (disp))
6948                && (GET_CODE (disp) != SYMBOL_REF
6949                    || !legitimate_constant_p (disp)))
6950         {
6951           reason = "displacement is not constant";
6952           goto report_error;
6953         }
6954       else if (TARGET_64BIT
6955                && !x86_64_immediate_operand (disp, VOIDmode))
6956         {
6957           reason = "displacement is out of range";
6958           goto report_error;
6959         }
6960     }
6961
6962   /* Everything looks valid.  */
6963   return TRUE;
6964
6965  report_error:
6966   return FALSE;
6967 }
6968 \f
6969 /* Return a unique alias set for the GOT.  */
6970
6971 static HOST_WIDE_INT
6972 ix86_GOT_alias_set (void)
6973 {
6974   static HOST_WIDE_INT set = -1;
6975   if (set == -1)
6976     set = new_alias_set ();
6977   return set;
6978 }
6979
6980 /* Return a legitimate reference for ORIG (an address) using the
6981    register REG.  If REG is 0, a new pseudo is generated.
6982
6983    There are two types of references that must be handled:
6984
6985    1. Global data references must load the address from the GOT, via
6986       the PIC reg.  An insn is emitted to do this load, and the reg is
6987       returned.
6988
6989    2. Static data references, constant pool addresses, and code labels
6990       compute the address as an offset from the GOT, whose base is in
6991       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6992       differentiate them from global data objects.  The returned
6993       address is the PIC reg + an unspec constant.
6994
6995    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6996    reg also appears in the address.  */
6997
6998 static rtx
6999 legitimize_pic_address (rtx orig, rtx reg)
7000 {
7001   rtx addr = orig;
7002   rtx new = orig;
7003   rtx base;
7004
7005 #if TARGET_MACHO
7006   if (TARGET_MACHO && !TARGET_64BIT)
7007     {
7008       if (reg == 0)
7009         reg = gen_reg_rtx (Pmode);
7010       /* Use the generic Mach-O PIC machinery.  */
7011       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
7012     }
7013 #endif
7014
7015   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
7016     new = addr;
7017   else if (TARGET_64BIT
7018            && ix86_cmodel != CM_SMALL_PIC
7019            && gotoff_operand (addr, Pmode))
7020     {
7021       rtx tmpreg;
7022       /* This symbol may be referenced via a displacement from the PIC
7023          base address (@GOTOFF).  */
7024
7025       if (reload_in_progress)
7026         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7027       if (GET_CODE (addr) == CONST)
7028         addr = XEXP (addr, 0);
7029       if (GET_CODE (addr) == PLUS)
7030           {
7031             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7032                                   UNSPEC_GOTOFF);
7033             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
7034           }
7035         else
7036           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7037       new = gen_rtx_CONST (Pmode, new);
7038       if (!reg)
7039         tmpreg = gen_reg_rtx (Pmode);
7040       else
7041         tmpreg = reg;
7042       emit_move_insn (tmpreg, new);
7043
7044       if (reg != 0)
7045         {
7046           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
7047                                      tmpreg, 1, OPTAB_DIRECT);
7048           new = reg;
7049         }
7050       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
7051     }
7052   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
7053     {
7054       /* This symbol may be referenced via a displacement from the PIC
7055          base address (@GOTOFF).  */
7056
7057       if (reload_in_progress)
7058         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7059       if (GET_CODE (addr) == CONST)
7060         addr = XEXP (addr, 0);
7061       if (GET_CODE (addr) == PLUS)
7062           {
7063             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7064                                   UNSPEC_GOTOFF);
7065             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
7066           }
7067         else
7068           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7069       new = gen_rtx_CONST (Pmode, new);
7070       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7071
7072       if (reg != 0)
7073         {
7074           emit_move_insn (reg, new);
7075           new = reg;
7076         }
7077     }
7078   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
7079            /* We can't use @GOTOFF for text labels on VxWorks;
7080               see gotoff_operand.  */
7081            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
7082     {
7083       /* Given that we've already handled dllimport variables separately
7084          in legitimize_address, and all other variables should satisfy
7085          legitimate_pic_address_disp_p, we should never arrive here.  */
7086       gcc_assert (!TARGET_64BIT_MS_ABI);
7087
7088       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
7089         {
7090           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
7091           new = gen_rtx_CONST (Pmode, new);
7092           new = gen_const_mem (Pmode, new);
7093           set_mem_alias_set (new, ix86_GOT_alias_set ());
7094
7095           if (reg == 0)
7096             reg = gen_reg_rtx (Pmode);
7097           /* Use directly gen_movsi, otherwise the address is loaded
7098              into register for CSE.  We don't want to CSE this addresses,
7099              instead we CSE addresses from the GOT table, so skip this.  */
7100           emit_insn (gen_movsi (reg, new));
7101           new = reg;
7102         }
7103       else
7104         {
7105           /* This symbol must be referenced via a load from the
7106              Global Offset Table (@GOT).  */
7107
7108           if (reload_in_progress)
7109             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7110           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
7111           new = gen_rtx_CONST (Pmode, new);
7112           if (TARGET_64BIT)
7113             new = force_reg (Pmode, new);
7114           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7115           new = gen_const_mem (Pmode, new);
7116           set_mem_alias_set (new, ix86_GOT_alias_set ());
7117
7118           if (reg == 0)
7119             reg = gen_reg_rtx (Pmode);
7120           emit_move_insn (reg, new);
7121           new = reg;
7122         }
7123     }
7124   else
7125     {
7126       if (CONST_INT_P (addr)
7127           && !x86_64_immediate_operand (addr, VOIDmode))
7128         {
7129           if (reg)
7130             {
7131               emit_move_insn (reg, addr);
7132               new = reg;
7133             }
7134           else
7135             new = force_reg (Pmode, addr);
7136         }
7137       else if (GET_CODE (addr) == CONST)
7138         {
7139           addr = XEXP (addr, 0);
7140
7141           /* We must match stuff we generate before.  Assume the only
7142              unspecs that can get here are ours.  Not that we could do
7143              anything with them anyway....  */
7144           if (GET_CODE (addr) == UNSPEC
7145               || (GET_CODE (addr) == PLUS
7146                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
7147             return orig;
7148           gcc_assert (GET_CODE (addr) == PLUS);
7149         }
7150       if (GET_CODE (addr) == PLUS)
7151         {
7152           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
7153
7154           /* Check first to see if this is a constant offset from a @GOTOFF
7155              symbol reference.  */
7156           if (gotoff_operand (op0, Pmode)
7157               && CONST_INT_P (op1))
7158             {
7159               if (!TARGET_64BIT)
7160                 {
7161                   if (reload_in_progress)
7162                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7163                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
7164                                         UNSPEC_GOTOFF);
7165                   new = gen_rtx_PLUS (Pmode, new, op1);
7166                   new = gen_rtx_CONST (Pmode, new);
7167                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7168
7169                   if (reg != 0)
7170                     {
7171                       emit_move_insn (reg, new);
7172                       new = reg;
7173                     }
7174                 }
7175               else
7176                 {
7177                   if (INTVAL (op1) < -16*1024*1024
7178                       || INTVAL (op1) >= 16*1024*1024)
7179                     {
7180                       if (!x86_64_immediate_operand (op1, Pmode))
7181                         op1 = force_reg (Pmode, op1);
7182                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
7183                     }
7184                 }
7185             }
7186           else
7187             {
7188               base = legitimize_pic_address (XEXP (addr, 0), reg);
7189               new  = legitimize_pic_address (XEXP (addr, 1),
7190                                              base == reg ? NULL_RTX : reg);
7191
7192               if (CONST_INT_P (new))
7193                 new = plus_constant (base, INTVAL (new));
7194               else
7195                 {
7196                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
7197                     {
7198                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
7199                       new = XEXP (new, 1);
7200                     }
7201                   new = gen_rtx_PLUS (Pmode, base, new);
7202                 }
7203             }
7204         }
7205     }
7206   return new;
7207 }
7208 \f
7209 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
7210
7211 static rtx
7212 get_thread_pointer (int to_reg)
7213 {
7214   rtx tp, reg, insn;
7215
7216   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
7217   if (!to_reg)
7218     return tp;
7219
7220   reg = gen_reg_rtx (Pmode);
7221   insn = gen_rtx_SET (VOIDmode, reg, tp);
7222   insn = emit_insn (insn);
7223
7224   return reg;
7225 }
7226
7227 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
7228    false if we expect this to be used for a memory address and true if
7229    we expect to load the address into a register.  */
7230
7231 static rtx
7232 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
7233 {
7234   rtx dest, base, off, pic, tp;
7235   int type;
7236
7237   switch (model)
7238     {
7239     case TLS_MODEL_GLOBAL_DYNAMIC:
7240       dest = gen_reg_rtx (Pmode);
7241       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7242
7243       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7244         {
7245           rtx rax = gen_rtx_REG (Pmode, 0), insns;
7246
7247           start_sequence ();
7248           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
7249           insns = get_insns ();
7250           end_sequence ();
7251
7252           emit_libcall_block (insns, dest, rax, x);
7253         }
7254       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7255         emit_insn (gen_tls_global_dynamic_64 (dest, x));
7256       else
7257         emit_insn (gen_tls_global_dynamic_32 (dest, x));
7258
7259       if (TARGET_GNU2_TLS)
7260         {
7261           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
7262
7263           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7264         }
7265       break;
7266
7267     case TLS_MODEL_LOCAL_DYNAMIC:
7268       base = gen_reg_rtx (Pmode);
7269       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7270
7271       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7272         {
7273           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
7274
7275           start_sequence ();
7276           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
7277           insns = get_insns ();
7278           end_sequence ();
7279
7280           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
7281           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
7282           emit_libcall_block (insns, base, rax, note);
7283         }
7284       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7285         emit_insn (gen_tls_local_dynamic_base_64 (base));
7286       else
7287         emit_insn (gen_tls_local_dynamic_base_32 (base));
7288
7289       if (TARGET_GNU2_TLS)
7290         {
7291           rtx x = ix86_tls_module_base ();
7292
7293           set_unique_reg_note (get_last_insn (), REG_EQUIV,
7294                                gen_rtx_MINUS (Pmode, x, tp));
7295         }
7296
7297       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
7298       off = gen_rtx_CONST (Pmode, off);
7299
7300       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
7301
7302       if (TARGET_GNU2_TLS)
7303         {
7304           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
7305
7306           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7307         }
7308
7309       break;
7310
7311     case TLS_MODEL_INITIAL_EXEC:
7312       if (TARGET_64BIT)
7313         {
7314           pic = NULL;
7315           type = UNSPEC_GOTNTPOFF;
7316         }
7317       else if (flag_pic)
7318         {
7319           if (reload_in_progress)
7320             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7321           pic = pic_offset_table_rtx;
7322           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
7323         }
7324       else if (!TARGET_ANY_GNU_TLS)
7325         {
7326           pic = gen_reg_rtx (Pmode);
7327           emit_insn (gen_set_got (pic));
7328           type = UNSPEC_GOTTPOFF;
7329         }
7330       else
7331         {
7332           pic = NULL;
7333           type = UNSPEC_INDNTPOFF;
7334         }
7335
7336       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
7337       off = gen_rtx_CONST (Pmode, off);
7338       if (pic)
7339         off = gen_rtx_PLUS (Pmode, pic, off);
7340       off = gen_const_mem (Pmode, off);
7341       set_mem_alias_set (off, ix86_GOT_alias_set ());
7342
7343       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7344         {
7345           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7346           off = force_reg (Pmode, off);
7347           return gen_rtx_PLUS (Pmode, base, off);
7348         }
7349       else
7350         {
7351           base = get_thread_pointer (true);
7352           dest = gen_reg_rtx (Pmode);
7353           emit_insn (gen_subsi3 (dest, base, off));
7354         }
7355       break;
7356
7357     case TLS_MODEL_LOCAL_EXEC:
7358       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
7359                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7360                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
7361       off = gen_rtx_CONST (Pmode, off);
7362
7363       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7364         {
7365           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7366           return gen_rtx_PLUS (Pmode, base, off);
7367         }
7368       else
7369         {
7370           base = get_thread_pointer (true);
7371           dest = gen_reg_rtx (Pmode);
7372           emit_insn (gen_subsi3 (dest, base, off));
7373         }
7374       break;
7375
7376     default:
7377       gcc_unreachable ();
7378     }
7379
7380   return dest;
7381 }
7382
7383 /* Create or return the unique __imp_DECL dllimport symbol corresponding
7384    to symbol DECL.  */
7385
7386 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
7387   htab_t dllimport_map;
7388
7389 static tree
7390 get_dllimport_decl (tree decl)
7391 {
7392   struct tree_map *h, in;
7393   void **loc;
7394   const char *name;
7395   const char *prefix;
7396   size_t namelen, prefixlen;
7397   char *imp_name;
7398   tree to;
7399   rtx rtl;
7400
7401   if (!dllimport_map)
7402     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
7403
7404   in.hash = htab_hash_pointer (decl);
7405   in.base.from = decl;
7406   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
7407   h = *loc;
7408   if (h)
7409     return h->to;
7410
7411   *loc = h = ggc_alloc (sizeof (struct tree_map));
7412   h->hash = in.hash;
7413   h->base.from = decl;
7414   h->to = to = build_decl (VAR_DECL, NULL, ptr_type_node);
7415   DECL_ARTIFICIAL (to) = 1;
7416   DECL_IGNORED_P (to) = 1;
7417   DECL_EXTERNAL (to) = 1;
7418   TREE_READONLY (to) = 1;
7419
7420   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
7421   name = targetm.strip_name_encoding (name);
7422   if (name[0] == FASTCALL_PREFIX)
7423     {
7424       name++;
7425       prefix = "*__imp_";
7426     }
7427   else
7428     prefix = "*__imp__";
7429
7430   namelen = strlen (name);
7431   prefixlen = strlen (prefix);
7432   imp_name = alloca (namelen + prefixlen + 1);
7433   memcpy (imp_name, prefix, prefixlen);
7434   memcpy (imp_name + prefixlen, name, namelen + 1);
7435
7436   name = ggc_alloc_string (imp_name, namelen + prefixlen);
7437   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
7438   SET_SYMBOL_REF_DECL (rtl, to);
7439   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
7440
7441   rtl = gen_const_mem (Pmode, rtl);
7442   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
7443
7444   SET_DECL_RTL (to, rtl);
7445
7446   return to;
7447 }
7448
7449 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
7450    true if we require the result be a register.  */
7451
7452 static rtx
7453 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
7454 {
7455   tree imp_decl;
7456   rtx x;
7457
7458   gcc_assert (SYMBOL_REF_DECL (symbol));
7459   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
7460
7461   x = DECL_RTL (imp_decl);
7462   if (want_reg)
7463     x = force_reg (Pmode, x);
7464   return x;
7465 }
7466
7467 /* Try machine-dependent ways of modifying an illegitimate address
7468    to be legitimate.  If we find one, return the new, valid address.
7469    This macro is used in only one place: `memory_address' in explow.c.
7470
7471    OLDX is the address as it was before break_out_memory_refs was called.
7472    In some cases it is useful to look at this to decide what needs to be done.
7473
7474    MODE and WIN are passed so that this macro can use
7475    GO_IF_LEGITIMATE_ADDRESS.
7476
7477    It is always safe for this macro to do nothing.  It exists to recognize
7478    opportunities to optimize the output.
7479
7480    For the 80386, we handle X+REG by loading X into a register R and
7481    using R+REG.  R will go in a general reg and indexing will be used.
7482    However, if REG is a broken-out memory address or multiplication,
7483    nothing needs to be done because REG can certainly go in a general reg.
7484
7485    When -fpic is used, special handling is needed for symbolic references.
7486    See comments by legitimize_pic_address in i386.c for details.  */
7487
7488 rtx
7489 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
7490 {
7491   int changed = 0;
7492   unsigned log;
7493
7494   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
7495   if (log)
7496     return legitimize_tls_address (x, log, false);
7497   if (GET_CODE (x) == CONST
7498       && GET_CODE (XEXP (x, 0)) == PLUS
7499       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7500       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
7501     {
7502       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
7503       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7504     }
7505
7506   if (flag_pic && SYMBOLIC_CONST (x))
7507     return legitimize_pic_address (x, 0);
7508
7509   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
7510     {
7511       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
7512         return legitimize_dllimport_symbol (x, true);
7513       if (GET_CODE (x) == CONST
7514           && GET_CODE (XEXP (x, 0)) == PLUS
7515           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7516           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
7517         {
7518           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
7519           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7520         }
7521     }
7522
7523   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
7524   if (GET_CODE (x) == ASHIFT
7525       && CONST_INT_P (XEXP (x, 1))
7526       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
7527     {
7528       changed = 1;
7529       log = INTVAL (XEXP (x, 1));
7530       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
7531                         GEN_INT (1 << log));
7532     }
7533
7534   if (GET_CODE (x) == PLUS)
7535     {
7536       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
7537
7538       if (GET_CODE (XEXP (x, 0)) == ASHIFT
7539           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7540           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
7541         {
7542           changed = 1;
7543           log = INTVAL (XEXP (XEXP (x, 0), 1));
7544           XEXP (x, 0) = gen_rtx_MULT (Pmode,
7545                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
7546                                       GEN_INT (1 << log));
7547         }
7548
7549       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7550           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
7551           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7552         {
7553           changed = 1;
7554           log = INTVAL (XEXP (XEXP (x, 1), 1));
7555           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7556                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7557                                       GEN_INT (1 << log));
7558         }
7559
7560       /* Put multiply first if it isn't already.  */
7561       if (GET_CODE (XEXP (x, 1)) == MULT)
7562         {
7563           rtx tmp = XEXP (x, 0);
7564           XEXP (x, 0) = XEXP (x, 1);
7565           XEXP (x, 1) = tmp;
7566           changed = 1;
7567         }
7568
7569       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7570          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7571          created by virtual register instantiation, register elimination, and
7572          similar optimizations.  */
7573       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7574         {
7575           changed = 1;
7576           x = gen_rtx_PLUS (Pmode,
7577                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7578                                           XEXP (XEXP (x, 1), 0)),
7579                             XEXP (XEXP (x, 1), 1));
7580         }
7581
7582       /* Canonicalize
7583          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7584          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7585       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7586                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7587                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7588                && CONSTANT_P (XEXP (x, 1)))
7589         {
7590           rtx constant;
7591           rtx other = NULL_RTX;
7592
7593           if (CONST_INT_P (XEXP (x, 1)))
7594             {
7595               constant = XEXP (x, 1);
7596               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7597             }
7598           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
7599             {
7600               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7601               other = XEXP (x, 1);
7602             }
7603           else
7604             constant = 0;
7605
7606           if (constant)
7607             {
7608               changed = 1;
7609               x = gen_rtx_PLUS (Pmode,
7610                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7611                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7612                                 plus_constant (other, INTVAL (constant)));
7613             }
7614         }
7615
7616       if (changed && legitimate_address_p (mode, x, FALSE))
7617         return x;
7618
7619       if (GET_CODE (XEXP (x, 0)) == MULT)
7620         {
7621           changed = 1;
7622           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7623         }
7624
7625       if (GET_CODE (XEXP (x, 1)) == MULT)
7626         {
7627           changed = 1;
7628           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7629         }
7630
7631       if (changed
7632           && REG_P (XEXP (x, 1))
7633           && REG_P (XEXP (x, 0)))
7634         return x;
7635
7636       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7637         {
7638           changed = 1;
7639           x = legitimize_pic_address (x, 0);
7640         }
7641
7642       if (changed && legitimate_address_p (mode, x, FALSE))
7643         return x;
7644
7645       if (REG_P (XEXP (x, 0)))
7646         {
7647           rtx temp = gen_reg_rtx (Pmode);
7648           rtx val  = force_operand (XEXP (x, 1), temp);
7649           if (val != temp)
7650             emit_move_insn (temp, val);
7651
7652           XEXP (x, 1) = temp;
7653           return x;
7654         }
7655
7656       else if (REG_P (XEXP (x, 1)))
7657         {
7658           rtx temp = gen_reg_rtx (Pmode);
7659           rtx val  = force_operand (XEXP (x, 0), temp);
7660           if (val != temp)
7661             emit_move_insn (temp, val);
7662
7663           XEXP (x, 0) = temp;
7664           return x;
7665         }
7666     }
7667
7668   return x;
7669 }
7670 \f
7671 /* Print an integer constant expression in assembler syntax.  Addition
7672    and subtraction are the only arithmetic that may appear in these
7673    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7674    CODE is the operand print code from the output string.  */
7675
7676 static void
7677 output_pic_addr_const (FILE *file, rtx x, int code)
7678 {
7679   char buf[256];
7680
7681   switch (GET_CODE (x))
7682     {
7683     case PC:
7684       gcc_assert (flag_pic);
7685       putc ('.', file);
7686       break;
7687
7688     case SYMBOL_REF:
7689       if (! TARGET_MACHO || TARGET_64BIT)
7690         output_addr_const (file, x);
7691       else
7692         {
7693           const char *name = XSTR (x, 0);
7694
7695           /* Mark the decl as referenced so that cgraph will
7696              output the function.  */
7697           if (SYMBOL_REF_DECL (x))
7698             mark_decl_referenced (SYMBOL_REF_DECL (x));
7699
7700 #if TARGET_MACHO
7701           if (MACHOPIC_INDIRECT
7702               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
7703             name = machopic_indirection_name (x, /*stub_p=*/true);
7704 #endif
7705           assemble_name (file, name);
7706         }
7707       if (!TARGET_MACHO && !TARGET_64BIT_MS_ABI
7708           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7709         fputs ("@PLT", file);
7710       break;
7711
7712     case LABEL_REF:
7713       x = XEXP (x, 0);
7714       /* FALLTHRU */
7715     case CODE_LABEL:
7716       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7717       assemble_name (asm_out_file, buf);
7718       break;
7719
7720     case CONST_INT:
7721       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7722       break;
7723
7724     case CONST:
7725       /* This used to output parentheses around the expression,
7726          but that does not work on the 386 (either ATT or BSD assembler).  */
7727       output_pic_addr_const (file, XEXP (x, 0), code);
7728       break;
7729
7730     case CONST_DOUBLE:
7731       if (GET_MODE (x) == VOIDmode)
7732         {
7733           /* We can use %d if the number is <32 bits and positive.  */
7734           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7735             fprintf (file, "0x%lx%08lx",
7736                      (unsigned long) CONST_DOUBLE_HIGH (x),
7737                      (unsigned long) CONST_DOUBLE_LOW (x));
7738           else
7739             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7740         }
7741       else
7742         /* We can't handle floating point constants;
7743            PRINT_OPERAND must handle them.  */
7744         output_operand_lossage ("floating constant misused");
7745       break;
7746
7747     case PLUS:
7748       /* Some assemblers need integer constants to appear first.  */
7749       if (CONST_INT_P (XEXP (x, 0)))
7750         {
7751           output_pic_addr_const (file, XEXP (x, 0), code);
7752           putc ('+', file);
7753           output_pic_addr_const (file, XEXP (x, 1), code);
7754         }
7755       else
7756         {
7757           gcc_assert (CONST_INT_P (XEXP (x, 1)));
7758           output_pic_addr_const (file, XEXP (x, 1), code);
7759           putc ('+', file);
7760           output_pic_addr_const (file, XEXP (x, 0), code);
7761         }
7762       break;
7763
7764     case MINUS:
7765       if (!TARGET_MACHO)
7766         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7767       output_pic_addr_const (file, XEXP (x, 0), code);
7768       putc ('-', file);
7769       output_pic_addr_const (file, XEXP (x, 1), code);
7770       if (!TARGET_MACHO)
7771         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7772       break;
7773
7774      case UNSPEC:
7775        gcc_assert (XVECLEN (x, 0) == 1);
7776        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7777        switch (XINT (x, 1))
7778         {
7779         case UNSPEC_GOT:
7780           fputs ("@GOT", file);
7781           break;
7782         case UNSPEC_GOTOFF:
7783           fputs ("@GOTOFF", file);
7784           break;
7785         case UNSPEC_PLTOFF:
7786           fputs ("@PLTOFF", file);
7787           break;
7788         case UNSPEC_GOTPCREL:
7789           fputs ("@GOTPCREL(%rip)", file);
7790           break;
7791         case UNSPEC_GOTTPOFF:
7792           /* FIXME: This might be @TPOFF in Sun ld too.  */
7793           fputs ("@GOTTPOFF", file);
7794           break;
7795         case UNSPEC_TPOFF:
7796           fputs ("@TPOFF", file);
7797           break;
7798         case UNSPEC_NTPOFF:
7799           if (TARGET_64BIT)
7800             fputs ("@TPOFF", file);
7801           else
7802             fputs ("@NTPOFF", file);
7803           break;
7804         case UNSPEC_DTPOFF:
7805           fputs ("@DTPOFF", file);
7806           break;
7807         case UNSPEC_GOTNTPOFF:
7808           if (TARGET_64BIT)
7809             fputs ("@GOTTPOFF(%rip)", file);
7810           else
7811             fputs ("@GOTNTPOFF", file);
7812           break;
7813         case UNSPEC_INDNTPOFF:
7814           fputs ("@INDNTPOFF", file);
7815           break;
7816         default:
7817           output_operand_lossage ("invalid UNSPEC as operand");
7818           break;
7819         }
7820        break;
7821
7822     default:
7823       output_operand_lossage ("invalid expression as operand");
7824     }
7825 }
7826
7827 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7828    We need to emit DTP-relative relocations.  */
7829
7830 static void ATTRIBUTE_UNUSED
7831 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7832 {
7833   fputs (ASM_LONG, file);
7834   output_addr_const (file, x);
7835   fputs ("@DTPOFF", file);
7836   switch (size)
7837     {
7838     case 4:
7839       break;
7840     case 8:
7841       fputs (", 0", file);
7842       break;
7843     default:
7844       gcc_unreachable ();
7845    }
7846 }
7847
7848 /* In the name of slightly smaller debug output, and to cater to
7849    general assembler lossage, recognize PIC+GOTOFF and turn it back
7850    into a direct symbol reference.
7851
7852    On Darwin, this is necessary to avoid a crash, because Darwin
7853    has a different PIC label for each routine but the DWARF debugging
7854    information is not associated with any particular routine, so it's
7855    necessary to remove references to the PIC label from RTL stored by
7856    the DWARF output code.  */
7857
7858 static rtx
7859 ix86_delegitimize_address (rtx orig_x)
7860 {
7861   rtx x = orig_x;
7862   /* reg_addend is NULL or a multiple of some register.  */
7863   rtx reg_addend = NULL_RTX;
7864   /* const_addend is NULL or a const_int.  */
7865   rtx const_addend = NULL_RTX;
7866   /* This is the result, or NULL.  */
7867   rtx result = NULL_RTX;
7868
7869   if (MEM_P (x))
7870     x = XEXP (x, 0);
7871
7872   if (TARGET_64BIT)
7873     {
7874       if (GET_CODE (x) != CONST
7875           || GET_CODE (XEXP (x, 0)) != UNSPEC
7876           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7877           || !MEM_P (orig_x))
7878         return orig_x;
7879       return XVECEXP (XEXP (x, 0), 0, 0);
7880     }
7881
7882   if (GET_CODE (x) != PLUS
7883       || GET_CODE (XEXP (x, 1)) != CONST)
7884     return orig_x;
7885
7886   if (REG_P (XEXP (x, 0))
7887       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7888     /* %ebx + GOT/GOTOFF */
7889     ;
7890   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7891     {
7892       /* %ebx + %reg * scale + GOT/GOTOFF */
7893       reg_addend = XEXP (x, 0);
7894       if (REG_P (XEXP (reg_addend, 0))
7895           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7896         reg_addend = XEXP (reg_addend, 1);
7897       else if (REG_P (XEXP (reg_addend, 1))
7898                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7899         reg_addend = XEXP (reg_addend, 0);
7900       else
7901         return orig_x;
7902       if (!REG_P (reg_addend)
7903           && GET_CODE (reg_addend) != MULT
7904           && GET_CODE (reg_addend) != ASHIFT)
7905         return orig_x;
7906     }
7907   else
7908     return orig_x;
7909
7910   x = XEXP (XEXP (x, 1), 0);
7911   if (GET_CODE (x) == PLUS
7912       && CONST_INT_P (XEXP (x, 1)))
7913     {
7914       const_addend = XEXP (x, 1);
7915       x = XEXP (x, 0);
7916     }
7917
7918   if (GET_CODE (x) == UNSPEC
7919       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x))
7920           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
7921     result = XVECEXP (x, 0, 0);
7922
7923   if (TARGET_MACHO && darwin_local_data_pic (x)
7924       && !MEM_P (orig_x))
7925     result = XEXP (x, 0);
7926
7927   if (! result)
7928     return orig_x;
7929
7930   if (const_addend)
7931     result = gen_rtx_PLUS (Pmode, result, const_addend);
7932   if (reg_addend)
7933     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7934   return result;
7935 }
7936
7937 /* If X is a machine specific address (i.e. a symbol or label being
7938    referenced as a displacement from the GOT implemented using an
7939    UNSPEC), then return the base term.  Otherwise return X.  */
7940
7941 rtx
7942 ix86_find_base_term (rtx x)
7943 {
7944   rtx term;
7945
7946   if (TARGET_64BIT)
7947     {
7948       if (GET_CODE (x) != CONST)
7949         return x;
7950       term = XEXP (x, 0);
7951       if (GET_CODE (term) == PLUS
7952           && (CONST_INT_P (XEXP (term, 1))
7953               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
7954         term = XEXP (term, 0);
7955       if (GET_CODE (term) != UNSPEC
7956           || XINT (term, 1) != UNSPEC_GOTPCREL)
7957         return x;
7958
7959       term = XVECEXP (term, 0, 0);
7960
7961       if (GET_CODE (term) != SYMBOL_REF
7962           && GET_CODE (term) != LABEL_REF)
7963         return x;
7964
7965       return term;
7966     }
7967
7968   term = ix86_delegitimize_address (x);
7969
7970   if (GET_CODE (term) != SYMBOL_REF
7971       && GET_CODE (term) != LABEL_REF)
7972     return x;
7973
7974   return term;
7975 }
7976 \f
7977 static void
7978 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7979                     int fp, FILE *file)
7980 {
7981   const char *suffix;
7982
7983   if (mode == CCFPmode || mode == CCFPUmode)
7984     {
7985       enum rtx_code second_code, bypass_code;
7986       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7987       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7988       code = ix86_fp_compare_code_to_integer (code);
7989       mode = CCmode;
7990     }
7991   if (reverse)
7992     code = reverse_condition (code);
7993
7994   switch (code)
7995     {
7996     case EQ:
7997       suffix = "e";
7998       break;
7999     case NE:
8000       suffix = "ne";
8001       break;
8002     case GT:
8003       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
8004       suffix = "g";
8005       break;
8006     case GTU:
8007       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
8008          Those same assemblers have the same but opposite lossage on cmov.  */
8009       gcc_assert (mode == CCmode);
8010       suffix = fp ? "nbe" : "a";
8011       break;
8012     case LT:
8013       switch (mode)
8014         {
8015         case CCNOmode:
8016         case CCGOCmode:
8017           suffix = "s";
8018           break;
8019
8020         case CCmode:
8021         case CCGCmode:
8022           suffix = "l";
8023           break;
8024
8025         default:
8026           gcc_unreachable ();
8027         }
8028       break;
8029     case LTU:
8030       gcc_assert (mode == CCmode);
8031       suffix = "b";
8032       break;
8033     case GE:
8034       switch (mode)
8035         {
8036         case CCNOmode:
8037         case CCGOCmode:
8038           suffix = "ns";
8039           break;
8040
8041         case CCmode:
8042         case CCGCmode:
8043           suffix = "ge";
8044           break;
8045
8046         default:
8047           gcc_unreachable ();
8048         }
8049       break;
8050     case GEU:
8051       /* ??? As above.  */
8052       gcc_assert (mode == CCmode);
8053       suffix = fp ? "nb" : "ae";
8054       break;
8055     case LE:
8056       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
8057       suffix = "le";
8058       break;
8059     case LEU:
8060       gcc_assert (mode == CCmode);
8061       suffix = "be";
8062       break;
8063     case UNORDERED:
8064       suffix = fp ? "u" : "p";
8065       break;
8066     case ORDERED:
8067       suffix = fp ? "nu" : "np";
8068       break;
8069     default:
8070       gcc_unreachable ();
8071     }
8072   fputs (suffix, file);
8073 }
8074
8075 /* Print the name of register X to FILE based on its machine mode and number.
8076    If CODE is 'w', pretend the mode is HImode.
8077    If CODE is 'b', pretend the mode is QImode.
8078    If CODE is 'k', pretend the mode is SImode.
8079    If CODE is 'q', pretend the mode is DImode.
8080    If CODE is 'h', pretend the reg is the 'high' byte register.
8081    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
8082
8083 void
8084 print_reg (rtx x, int code, FILE *file)
8085 {
8086   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
8087               && REGNO (x) != FRAME_POINTER_REGNUM
8088               && REGNO (x) != FLAGS_REG
8089               && REGNO (x) != FPSR_REG
8090               && REGNO (x) != FPCR_REG);
8091
8092   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
8093     putc ('%', file);
8094
8095   if (code == 'w' || MMX_REG_P (x))
8096     code = 2;
8097   else if (code == 'b')
8098     code = 1;
8099   else if (code == 'k')
8100     code = 4;
8101   else if (code == 'q')
8102     code = 8;
8103   else if (code == 'y')
8104     code = 3;
8105   else if (code == 'h')
8106     code = 0;
8107   else
8108     code = GET_MODE_SIZE (GET_MODE (x));
8109
8110   /* Irritatingly, AMD extended registers use different naming convention
8111      from the normal registers.  */
8112   if (REX_INT_REG_P (x))
8113     {
8114       gcc_assert (TARGET_64BIT);
8115       switch (code)
8116         {
8117           case 0:
8118             error ("extended registers have no high halves");
8119             break;
8120           case 1:
8121             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
8122             break;
8123           case 2:
8124             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
8125             break;
8126           case 4:
8127             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
8128             break;
8129           case 8:
8130             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
8131             break;
8132           default:
8133             error ("unsupported operand size for extended register");
8134             break;
8135         }
8136       return;
8137     }
8138   switch (code)
8139     {
8140     case 3:
8141       if (STACK_TOP_P (x))
8142         {
8143           fputs ("st(0)", file);
8144           break;
8145         }
8146       /* FALLTHRU */
8147     case 8:
8148     case 4:
8149     case 12:
8150       if (! ANY_FP_REG_P (x))
8151         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
8152       /* FALLTHRU */
8153     case 16:
8154     case 2:
8155     normal:
8156       fputs (hi_reg_name[REGNO (x)], file);
8157       break;
8158     case 1:
8159       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
8160         goto normal;
8161       fputs (qi_reg_name[REGNO (x)], file);
8162       break;
8163     case 0:
8164       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
8165         goto normal;
8166       fputs (qi_high_reg_name[REGNO (x)], file);
8167       break;
8168     default:
8169       gcc_unreachable ();
8170     }
8171 }
8172
8173 /* Locate some local-dynamic symbol still in use by this function
8174    so that we can print its name in some tls_local_dynamic_base
8175    pattern.  */
8176
8177 static int
8178 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8179 {
8180   rtx x = *px;
8181
8182   if (GET_CODE (x) == SYMBOL_REF
8183       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8184     {
8185       cfun->machine->some_ld_name = XSTR (x, 0);
8186       return 1;
8187     }
8188
8189   return 0;
8190 }
8191
8192 static const char *
8193 get_some_local_dynamic_name (void)
8194 {
8195   rtx insn;
8196
8197   if (cfun->machine->some_ld_name)
8198     return cfun->machine->some_ld_name;
8199
8200   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8201     if (INSN_P (insn)
8202         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
8203       return cfun->machine->some_ld_name;
8204
8205   gcc_unreachable ();
8206 }
8207
8208 /* Meaning of CODE:
8209    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
8210    C -- print opcode suffix for set/cmov insn.
8211    c -- like C, but print reversed condition
8212    F,f -- likewise, but for floating-point.
8213    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
8214         otherwise nothing
8215    R -- print the prefix for register names.
8216    z -- print the opcode suffix for the size of the current operand.
8217    * -- print a star (in certain assembler syntax)
8218    A -- print an absolute memory reference.
8219    w -- print the operand as if it's a "word" (HImode) even if it isn't.
8220    s -- print a shift double count, followed by the assemblers argument
8221         delimiter.
8222    b -- print the QImode name of the register for the indicated operand.
8223         %b0 would print %al if operands[0] is reg 0.
8224    w --  likewise, print the HImode name of the register.
8225    k --  likewise, print the SImode name of the register.
8226    q --  likewise, print the DImode name of the register.
8227    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
8228    y -- print "st(0)" instead of "st" as a register.
8229    D -- print condition for SSE cmp instruction.
8230    P -- if PIC, print an @PLT suffix.
8231    X -- don't print any sort of PIC '@' suffix for a symbol.
8232    & -- print some in-use local-dynamic symbol name.
8233    H -- print a memory address offset by 8; used for sse high-parts
8234  */
8235
8236 void
8237 print_operand (FILE *file, rtx x, int code)
8238 {
8239   if (code)
8240     {
8241       switch (code)
8242         {
8243         case '*':
8244           if (ASSEMBLER_DIALECT == ASM_ATT)
8245             putc ('*', file);
8246           return;
8247
8248         case '&':
8249           assemble_name (file, get_some_local_dynamic_name ());
8250           return;
8251
8252         case 'A':
8253           switch (ASSEMBLER_DIALECT)
8254             {
8255             case ASM_ATT:
8256               putc ('*', file);
8257               break;
8258
8259             case ASM_INTEL:
8260               /* Intel syntax. For absolute addresses, registers should not
8261                  be surrounded by braces.  */
8262               if (!REG_P (x))
8263                 {
8264                   putc ('[', file);
8265                   PRINT_OPERAND (file, x, 0);
8266                   putc (']', file);
8267                   return;
8268                 }
8269               break;
8270
8271             default:
8272               gcc_unreachable ();
8273             }
8274
8275           PRINT_OPERAND (file, x, 0);
8276           return;
8277
8278
8279         case 'L':
8280           if (ASSEMBLER_DIALECT == ASM_ATT)
8281             putc ('l', file);
8282           return;
8283
8284         case 'W':
8285           if (ASSEMBLER_DIALECT == ASM_ATT)
8286             putc ('w', file);
8287           return;
8288
8289         case 'B':
8290           if (ASSEMBLER_DIALECT == ASM_ATT)
8291             putc ('b', file);
8292           return;
8293
8294         case 'Q':
8295           if (ASSEMBLER_DIALECT == ASM_ATT)
8296             putc ('l', file);
8297           return;
8298
8299         case 'S':
8300           if (ASSEMBLER_DIALECT == ASM_ATT)
8301             putc ('s', file);
8302           return;
8303
8304         case 'T':
8305           if (ASSEMBLER_DIALECT == ASM_ATT)
8306             putc ('t', file);
8307           return;
8308
8309         case 'z':
8310           /* 387 opcodes don't get size suffixes if the operands are
8311              registers.  */
8312           if (STACK_REG_P (x))
8313             return;
8314
8315           /* Likewise if using Intel opcodes.  */
8316           if (ASSEMBLER_DIALECT == ASM_INTEL)
8317             return;
8318
8319           /* This is the size of op from size of operand.  */
8320           switch (GET_MODE_SIZE (GET_MODE (x)))
8321             {
8322             case 1:
8323               putc ('b', file);
8324               return;
8325
8326             case 2:
8327 #ifdef HAVE_GAS_FILDS_FISTS
8328               putc ('s', file);
8329 #endif
8330               return;
8331
8332             case 4:
8333               if (GET_MODE (x) == SFmode)
8334                 {
8335                   putc ('s', file);
8336                   return;
8337                 }
8338               else
8339                 putc ('l', file);
8340               return;
8341
8342             case 12:
8343             case 16:
8344               putc ('t', file);
8345               return;
8346
8347             case 8:
8348               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
8349                 {
8350 #ifdef GAS_MNEMONICS
8351                   putc ('q', file);
8352 #else
8353                   putc ('l', file);
8354                   putc ('l', file);
8355 #endif
8356                 }
8357               else
8358                 putc ('l', file);
8359               return;
8360
8361             default:
8362               gcc_unreachable ();
8363             }
8364
8365         case 'b':
8366         case 'w':
8367         case 'k':
8368         case 'q':
8369         case 'h':
8370         case 'y':
8371         case 'X':
8372         case 'P':
8373           break;
8374
8375         case 's':
8376           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
8377             {
8378               PRINT_OPERAND (file, x, 0);
8379               putc (',', file);
8380             }
8381           return;
8382
8383         case 'D':
8384           /* Little bit of braindamage here.  The SSE compare instructions
8385              does use completely different names for the comparisons that the
8386              fp conditional moves.  */
8387           switch (GET_CODE (x))
8388             {
8389             case EQ:
8390             case UNEQ:
8391               fputs ("eq", file);
8392               break;
8393             case LT:
8394             case UNLT:
8395               fputs ("lt", file);
8396               break;
8397             case LE:
8398             case UNLE:
8399               fputs ("le", file);
8400               break;
8401             case UNORDERED:
8402               fputs ("unord", file);
8403               break;
8404             case NE:
8405             case LTGT:
8406               fputs ("neq", file);
8407               break;
8408             case UNGE:
8409             case GE:
8410               fputs ("nlt", file);
8411               break;
8412             case UNGT:
8413             case GT:
8414               fputs ("nle", file);
8415               break;
8416             case ORDERED:
8417               fputs ("ord", file);
8418               break;
8419             default:
8420               gcc_unreachable ();
8421             }
8422           return;
8423         case 'O':
8424 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8425           if (ASSEMBLER_DIALECT == ASM_ATT)
8426             {
8427               switch (GET_MODE (x))
8428                 {
8429                 case HImode: putc ('w', file); break;
8430                 case SImode:
8431                 case SFmode: putc ('l', file); break;
8432                 case DImode:
8433                 case DFmode: putc ('q', file); break;
8434                 default: gcc_unreachable ();
8435                 }
8436               putc ('.', file);
8437             }
8438 #endif
8439           return;
8440         case 'C':
8441           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
8442           return;
8443         case 'F':
8444 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8445           if (ASSEMBLER_DIALECT == ASM_ATT)
8446             putc ('.', file);
8447 #endif
8448           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
8449           return;
8450
8451           /* Like above, but reverse condition */
8452         case 'c':
8453           /* Check to see if argument to %c is really a constant
8454              and not a condition code which needs to be reversed.  */
8455           if (!COMPARISON_P (x))
8456           {
8457             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
8458              return;
8459           }
8460           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
8461           return;
8462         case 'f':
8463 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8464           if (ASSEMBLER_DIALECT == ASM_ATT)
8465             putc ('.', file);
8466 #endif
8467           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
8468           return;
8469
8470         case 'H':
8471           /* It doesn't actually matter what mode we use here, as we're
8472              only going to use this for printing.  */
8473           x = adjust_address_nv (x, DImode, 8);
8474           break;
8475
8476         case '+':
8477           {
8478             rtx x;
8479
8480             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
8481               return;
8482
8483             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
8484             if (x)
8485               {
8486                 int pred_val = INTVAL (XEXP (x, 0));
8487
8488                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
8489                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
8490                   {
8491                     int taken = pred_val > REG_BR_PROB_BASE / 2;
8492                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
8493
8494                     /* Emit hints only in the case default branch prediction
8495                        heuristics would fail.  */
8496                     if (taken != cputaken)
8497                       {
8498                         /* We use 3e (DS) prefix for taken branches and
8499                            2e (CS) prefix for not taken branches.  */
8500                         if (taken)
8501                           fputs ("ds ; ", file);
8502                         else
8503                           fputs ("cs ; ", file);
8504                       }
8505                   }
8506               }
8507             return;
8508           }
8509         default:
8510             output_operand_lossage ("invalid operand code '%c'", code);
8511         }
8512     }
8513
8514   if (REG_P (x))
8515     print_reg (x, code, file);
8516
8517   else if (MEM_P (x))
8518     {
8519       /* No `byte ptr' prefix for call instructions.  */
8520       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
8521         {
8522           const char * size;
8523           switch (GET_MODE_SIZE (GET_MODE (x)))
8524             {
8525             case 1: size = "BYTE"; break;
8526             case 2: size = "WORD"; break;
8527             case 4: size = "DWORD"; break;
8528             case 8: size = "QWORD"; break;
8529             case 12: size = "XWORD"; break;
8530             case 16: size = "XMMWORD"; break;
8531             default:
8532               gcc_unreachable ();
8533             }
8534
8535           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
8536           if (code == 'b')
8537             size = "BYTE";
8538           else if (code == 'w')
8539             size = "WORD";
8540           else if (code == 'k')
8541             size = "DWORD";
8542
8543           fputs (size, file);
8544           fputs (" PTR ", file);
8545         }
8546
8547       x = XEXP (x, 0);
8548       /* Avoid (%rip) for call operands.  */
8549       if (CONSTANT_ADDRESS_P (x) && code == 'P'
8550           && !CONST_INT_P (x))
8551         output_addr_const (file, x);
8552       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
8553         output_operand_lossage ("invalid constraints for operand");
8554       else
8555         output_address (x);
8556     }
8557
8558   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
8559     {
8560       REAL_VALUE_TYPE r;
8561       long l;
8562
8563       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8564       REAL_VALUE_TO_TARGET_SINGLE (r, l);
8565
8566       if (ASSEMBLER_DIALECT == ASM_ATT)
8567         putc ('$', file);
8568       fprintf (file, "0x%08lx", l);
8569     }
8570
8571   /* These float cases don't actually occur as immediate operands.  */
8572   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
8573     {
8574       char dstr[30];
8575
8576       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8577       fprintf (file, "%s", dstr);
8578     }
8579
8580   else if (GET_CODE (x) == CONST_DOUBLE
8581            && GET_MODE (x) == XFmode)
8582     {
8583       char dstr[30];
8584
8585       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8586       fprintf (file, "%s", dstr);
8587     }
8588
8589   else
8590     {
8591       /* We have patterns that allow zero sets of memory, for instance.
8592          In 64-bit mode, we should probably support all 8-byte vectors,
8593          since we can in fact encode that into an immediate.  */
8594       if (GET_CODE (x) == CONST_VECTOR)
8595         {
8596           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
8597           x = const0_rtx;
8598         }
8599
8600       if (code != 'P')
8601         {
8602           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
8603             {
8604               if (ASSEMBLER_DIALECT == ASM_ATT)
8605                 putc ('$', file);
8606             }
8607           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
8608                    || GET_CODE (x) == LABEL_REF)
8609             {
8610               if (ASSEMBLER_DIALECT == ASM_ATT)
8611                 putc ('$', file);
8612               else
8613                 fputs ("OFFSET FLAT:", file);
8614             }
8615         }
8616       if (CONST_INT_P (x))
8617         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8618       else if (flag_pic)
8619         output_pic_addr_const (file, x, code);
8620       else
8621         output_addr_const (file, x);
8622     }
8623 }
8624 \f
8625 /* Print a memory operand whose address is ADDR.  */
8626
8627 void
8628 print_operand_address (FILE *file, rtx addr)
8629 {
8630   struct ix86_address parts;
8631   rtx base, index, disp;
8632   int scale;
8633   int ok = ix86_decompose_address (addr, &parts);
8634
8635   gcc_assert (ok);
8636
8637   base = parts.base;
8638   index = parts.index;
8639   disp = parts.disp;
8640   scale = parts.scale;
8641
8642   switch (parts.seg)
8643     {
8644     case SEG_DEFAULT:
8645       break;
8646     case SEG_FS:
8647     case SEG_GS:
8648       if (USER_LABEL_PREFIX[0] == 0)
8649         putc ('%', file);
8650       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8651       break;
8652     default:
8653       gcc_unreachable ();
8654     }
8655
8656   if (!base && !index)
8657     {
8658       /* Displacement only requires special attention.  */
8659
8660       if (CONST_INT_P (disp))
8661         {
8662           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8663             {
8664               if (USER_LABEL_PREFIX[0] == 0)
8665                 putc ('%', file);
8666               fputs ("ds:", file);
8667             }
8668           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8669         }
8670       else if (flag_pic)
8671         output_pic_addr_const (file, disp, 0);
8672       else
8673         output_addr_const (file, disp);
8674
8675       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8676       if (TARGET_64BIT)
8677         {
8678           if (GET_CODE (disp) == CONST
8679               && GET_CODE (XEXP (disp, 0)) == PLUS
8680               && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8681             disp = XEXP (XEXP (disp, 0), 0);
8682           if (GET_CODE (disp) == LABEL_REF
8683               || (GET_CODE (disp) == SYMBOL_REF
8684                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8685             fputs ("(%rip)", file);
8686         }
8687     }
8688   else
8689     {
8690       if (ASSEMBLER_DIALECT == ASM_ATT)
8691         {
8692           if (disp)
8693             {
8694               if (flag_pic)
8695                 output_pic_addr_const (file, disp, 0);
8696               else if (GET_CODE (disp) == LABEL_REF)
8697                 output_asm_label (disp);
8698               else
8699                 output_addr_const (file, disp);
8700             }
8701
8702           putc ('(', file);
8703           if (base)
8704             print_reg (base, 0, file);
8705           if (index)
8706             {
8707               putc (',', file);
8708               print_reg (index, 0, file);
8709               if (scale != 1)
8710                 fprintf (file, ",%d", scale);
8711             }
8712           putc (')', file);
8713         }
8714       else
8715         {
8716           rtx offset = NULL_RTX;
8717
8718           if (disp)
8719             {
8720               /* Pull out the offset of a symbol; print any symbol itself.  */
8721               if (GET_CODE (disp) == CONST
8722                   && GET_CODE (XEXP (disp, 0)) == PLUS
8723                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8724                 {
8725                   offset = XEXP (XEXP (disp, 0), 1);
8726                   disp = gen_rtx_CONST (VOIDmode,
8727                                         XEXP (XEXP (disp, 0), 0));
8728                 }
8729
8730               if (flag_pic)
8731                 output_pic_addr_const (file, disp, 0);
8732               else if (GET_CODE (disp) == LABEL_REF)
8733                 output_asm_label (disp);
8734               else if (CONST_INT_P (disp))
8735                 offset = disp;
8736               else
8737                 output_addr_const (file, disp);
8738             }
8739
8740           putc ('[', file);
8741           if (base)
8742             {
8743               print_reg (base, 0, file);
8744               if (offset)
8745                 {
8746                   if (INTVAL (offset) >= 0)
8747                     putc ('+', file);
8748                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8749                 }
8750             }
8751           else if (offset)
8752             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8753           else
8754             putc ('0', file);
8755
8756           if (index)
8757             {
8758               putc ('+', file);
8759               print_reg (index, 0, file);
8760               if (scale != 1)
8761                 fprintf (file, "*%d", scale);
8762             }
8763           putc (']', file);
8764         }
8765     }
8766 }
8767
8768 bool
8769 output_addr_const_extra (FILE *file, rtx x)
8770 {
8771   rtx op;
8772
8773   if (GET_CODE (x) != UNSPEC)
8774     return false;
8775
8776   op = XVECEXP (x, 0, 0);
8777   switch (XINT (x, 1))
8778     {
8779     case UNSPEC_GOTTPOFF:
8780       output_addr_const (file, op);
8781       /* FIXME: This might be @TPOFF in Sun ld.  */
8782       fputs ("@GOTTPOFF", file);
8783       break;
8784     case UNSPEC_TPOFF:
8785       output_addr_const (file, op);
8786       fputs ("@TPOFF", file);
8787       break;
8788     case UNSPEC_NTPOFF:
8789       output_addr_const (file, op);
8790       if (TARGET_64BIT)
8791         fputs ("@TPOFF", file);
8792       else
8793         fputs ("@NTPOFF", file);
8794       break;
8795     case UNSPEC_DTPOFF:
8796       output_addr_const (file, op);
8797       fputs ("@DTPOFF", file);
8798       break;
8799     case UNSPEC_GOTNTPOFF:
8800       output_addr_const (file, op);
8801       if (TARGET_64BIT)
8802         fputs ("@GOTTPOFF(%rip)", file);
8803       else
8804         fputs ("@GOTNTPOFF", file);
8805       break;
8806     case UNSPEC_INDNTPOFF:
8807       output_addr_const (file, op);
8808       fputs ("@INDNTPOFF", file);
8809       break;
8810
8811     default:
8812       return false;
8813     }
8814
8815   return true;
8816 }
8817 \f
8818 /* Split one or more DImode RTL references into pairs of SImode
8819    references.  The RTL can be REG, offsettable MEM, integer constant, or
8820    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8821    split and "num" is its length.  lo_half and hi_half are output arrays
8822    that parallel "operands".  */
8823
8824 void
8825 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8826 {
8827   while (num--)
8828     {
8829       rtx op = operands[num];
8830
8831       /* simplify_subreg refuse to split volatile memory addresses,
8832          but we still have to handle it.  */
8833       if (MEM_P (op))
8834         {
8835           lo_half[num] = adjust_address (op, SImode, 0);
8836           hi_half[num] = adjust_address (op, SImode, 4);
8837         }
8838       else
8839         {
8840           lo_half[num] = simplify_gen_subreg (SImode, op,
8841                                               GET_MODE (op) == VOIDmode
8842                                               ? DImode : GET_MODE (op), 0);
8843           hi_half[num] = simplify_gen_subreg (SImode, op,
8844                                               GET_MODE (op) == VOIDmode
8845                                               ? DImode : GET_MODE (op), 4);
8846         }
8847     }
8848 }
8849 /* Split one or more TImode RTL references into pairs of DImode
8850    references.  The RTL can be REG, offsettable MEM, integer constant, or
8851    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8852    split and "num" is its length.  lo_half and hi_half are output arrays
8853    that parallel "operands".  */
8854
8855 void
8856 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8857 {
8858   while (num--)
8859     {
8860       rtx op = operands[num];
8861
8862       /* simplify_subreg refuse to split volatile memory addresses, but we
8863          still have to handle it.  */
8864       if (MEM_P (op))
8865         {
8866           lo_half[num] = adjust_address (op, DImode, 0);
8867           hi_half[num] = adjust_address (op, DImode, 8);
8868         }
8869       else
8870         {
8871           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8872           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8873         }
8874     }
8875 }
8876 \f
8877 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8878    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8879    is the expression of the binary operation.  The output may either be
8880    emitted here, or returned to the caller, like all output_* functions.
8881
8882    There is no guarantee that the operands are the same mode, as they
8883    might be within FLOAT or FLOAT_EXTEND expressions.  */
8884
8885 #ifndef SYSV386_COMPAT
8886 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8887    wants to fix the assemblers because that causes incompatibility
8888    with gcc.  No-one wants to fix gcc because that causes
8889    incompatibility with assemblers...  You can use the option of
8890    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8891 #define SYSV386_COMPAT 1
8892 #endif
8893
8894 const char *
8895 output_387_binary_op (rtx insn, rtx *operands)
8896 {
8897   static char buf[30];
8898   const char *p;
8899   const char *ssep;
8900   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8901
8902 #ifdef ENABLE_CHECKING
8903   /* Even if we do not want to check the inputs, this documents input
8904      constraints.  Which helps in understanding the following code.  */
8905   if (STACK_REG_P (operands[0])
8906       && ((REG_P (operands[1])
8907            && REGNO (operands[0]) == REGNO (operands[1])
8908            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
8909           || (REG_P (operands[2])
8910               && REGNO (operands[0]) == REGNO (operands[2])
8911               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
8912       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8913     ; /* ok */
8914   else
8915     gcc_assert (is_sse);
8916 #endif
8917
8918   switch (GET_CODE (operands[3]))
8919     {
8920     case PLUS:
8921       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8922           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8923         p = "fiadd";
8924       else
8925         p = "fadd";
8926       ssep = "add";
8927       break;
8928
8929     case MINUS:
8930       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8931           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8932         p = "fisub";
8933       else
8934         p = "fsub";
8935       ssep = "sub";
8936       break;
8937
8938     case MULT:
8939       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8940           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8941         p = "fimul";
8942       else
8943         p = "fmul";
8944       ssep = "mul";
8945       break;
8946
8947     case DIV:
8948       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8949           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8950         p = "fidiv";
8951       else
8952         p = "fdiv";
8953       ssep = "div";
8954       break;
8955
8956     default:
8957       gcc_unreachable ();
8958     }
8959
8960   if (is_sse)
8961    {
8962       strcpy (buf, ssep);
8963       if (GET_MODE (operands[0]) == SFmode)
8964         strcat (buf, "ss\t{%2, %0|%0, %2}");
8965       else
8966         strcat (buf, "sd\t{%2, %0|%0, %2}");
8967       return buf;
8968    }
8969   strcpy (buf, p);
8970
8971   switch (GET_CODE (operands[3]))
8972     {
8973     case MULT:
8974     case PLUS:
8975       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8976         {
8977           rtx temp = operands[2];
8978           operands[2] = operands[1];
8979           operands[1] = temp;
8980         }
8981
8982       /* know operands[0] == operands[1].  */
8983
8984       if (MEM_P (operands[2]))
8985         {
8986           p = "%z2\t%2";
8987           break;
8988         }
8989
8990       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8991         {
8992           if (STACK_TOP_P (operands[0]))
8993             /* How is it that we are storing to a dead operand[2]?
8994                Well, presumably operands[1] is dead too.  We can't
8995                store the result to st(0) as st(0) gets popped on this
8996                instruction.  Instead store to operands[2] (which I
8997                think has to be st(1)).  st(1) will be popped later.
8998                gcc <= 2.8.1 didn't have this check and generated
8999                assembly code that the Unixware assembler rejected.  */
9000             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
9001           else
9002             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
9003           break;
9004         }
9005
9006       if (STACK_TOP_P (operands[0]))
9007         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
9008       else
9009         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
9010       break;
9011
9012     case MINUS:
9013     case DIV:
9014       if (MEM_P (operands[1]))
9015         {
9016           p = "r%z1\t%1";
9017           break;
9018         }
9019
9020       if (MEM_P (operands[2]))
9021         {
9022           p = "%z2\t%2";
9023           break;
9024         }
9025
9026       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
9027         {
9028 #if SYSV386_COMPAT
9029           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
9030              derived assemblers, confusingly reverse the direction of
9031              the operation for fsub{r} and fdiv{r} when the
9032              destination register is not st(0).  The Intel assembler
9033              doesn't have this brain damage.  Read !SYSV386_COMPAT to
9034              figure out what the hardware really does.  */
9035           if (STACK_TOP_P (operands[0]))
9036             p = "{p\t%0, %2|rp\t%2, %0}";
9037           else
9038             p = "{rp\t%2, %0|p\t%0, %2}";
9039 #else
9040           if (STACK_TOP_P (operands[0]))
9041             /* As above for fmul/fadd, we can't store to st(0).  */
9042             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
9043           else
9044             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
9045 #endif
9046           break;
9047         }
9048
9049       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
9050         {
9051 #if SYSV386_COMPAT
9052           if (STACK_TOP_P (operands[0]))
9053             p = "{rp\t%0, %1|p\t%1, %0}";
9054           else
9055             p = "{p\t%1, %0|rp\t%0, %1}";
9056 #else
9057           if (STACK_TOP_P (operands[0]))
9058             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
9059           else
9060             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
9061 #endif
9062           break;
9063         }
9064
9065       if (STACK_TOP_P (operands[0]))
9066         {
9067           if (STACK_TOP_P (operands[1]))
9068             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
9069           else
9070             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
9071           break;
9072         }
9073       else if (STACK_TOP_P (operands[1]))
9074         {
9075 #if SYSV386_COMPAT
9076           p = "{\t%1, %0|r\t%0, %1}";
9077 #else
9078           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
9079 #endif
9080         }
9081       else
9082         {
9083 #if SYSV386_COMPAT
9084           p = "{r\t%2, %0|\t%0, %2}";
9085 #else
9086           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
9087 #endif
9088         }
9089       break;
9090
9091     default:
9092       gcc_unreachable ();
9093     }
9094
9095   strcat (buf, p);
9096   return buf;
9097 }
9098
9099 /* Return needed mode for entity in optimize_mode_switching pass.  */
9100
9101 int
9102 ix86_mode_needed (int entity, rtx insn)
9103 {
9104   enum attr_i387_cw mode;
9105
9106   /* The mode UNINITIALIZED is used to store control word after a
9107      function call or ASM pattern.  The mode ANY specify that function
9108      has no requirements on the control word and make no changes in the
9109      bits we are interested in.  */
9110
9111   if (CALL_P (insn)
9112       || (NONJUMP_INSN_P (insn)
9113           && (asm_noperands (PATTERN (insn)) >= 0
9114               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
9115     return I387_CW_UNINITIALIZED;
9116
9117   if (recog_memoized (insn) < 0)
9118     return I387_CW_ANY;
9119
9120   mode = get_attr_i387_cw (insn);
9121
9122   switch (entity)
9123     {
9124     case I387_TRUNC:
9125       if (mode == I387_CW_TRUNC)
9126         return mode;
9127       break;
9128
9129     case I387_FLOOR:
9130       if (mode == I387_CW_FLOOR)
9131         return mode;
9132       break;
9133
9134     case I387_CEIL:
9135       if (mode == I387_CW_CEIL)
9136         return mode;
9137       break;
9138
9139     case I387_MASK_PM:
9140       if (mode == I387_CW_MASK_PM)
9141         return mode;
9142       break;
9143
9144     default:
9145       gcc_unreachable ();
9146     }
9147
9148   return I387_CW_ANY;
9149 }
9150
9151 /* Output code to initialize control word copies used by trunc?f?i and
9152    rounding patterns.  CURRENT_MODE is set to current control word,
9153    while NEW_MODE is set to new control word.  */
9154
9155 void
9156 emit_i387_cw_initialization (int mode)
9157 {
9158   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
9159   rtx new_mode;
9160
9161   int slot;
9162
9163   rtx reg = gen_reg_rtx (HImode);
9164
9165   emit_insn (gen_x86_fnstcw_1 (stored_mode));
9166   emit_move_insn (reg, copy_rtx (stored_mode));
9167
9168   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
9169     {
9170       switch (mode)
9171         {
9172         case I387_CW_TRUNC:
9173           /* round toward zero (truncate) */
9174           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
9175           slot = SLOT_CW_TRUNC;
9176           break;
9177
9178         case I387_CW_FLOOR:
9179           /* round down toward -oo */
9180           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9181           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
9182           slot = SLOT_CW_FLOOR;
9183           break;
9184
9185         case I387_CW_CEIL:
9186           /* round up toward +oo */
9187           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9188           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
9189           slot = SLOT_CW_CEIL;
9190           break;
9191
9192         case I387_CW_MASK_PM:
9193           /* mask precision exception for nearbyint() */
9194           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9195           slot = SLOT_CW_MASK_PM;
9196           break;
9197
9198         default:
9199           gcc_unreachable ();
9200         }
9201     }
9202   else
9203     {
9204       switch (mode)
9205         {
9206         case I387_CW_TRUNC:
9207           /* round toward zero (truncate) */
9208           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
9209           slot = SLOT_CW_TRUNC;
9210           break;
9211
9212         case I387_CW_FLOOR:
9213           /* round down toward -oo */
9214           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
9215           slot = SLOT_CW_FLOOR;
9216           break;
9217
9218         case I387_CW_CEIL:
9219           /* round up toward +oo */
9220           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
9221           slot = SLOT_CW_CEIL;
9222           break;
9223
9224         case I387_CW_MASK_PM:
9225           /* mask precision exception for nearbyint() */
9226           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9227           slot = SLOT_CW_MASK_PM;
9228           break;
9229
9230         default:
9231           gcc_unreachable ();
9232         }
9233     }
9234
9235   gcc_assert (slot < MAX_386_STACK_LOCALS);
9236
9237   new_mode = assign_386_stack_local (HImode, slot);
9238   emit_move_insn (new_mode, reg);
9239 }
9240
9241 /* Output code for INSN to convert a float to a signed int.  OPERANDS
9242    are the insn operands.  The output may be [HSD]Imode and the input
9243    operand may be [SDX]Fmode.  */
9244
9245 const char *
9246 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
9247 {
9248   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9249   int dimode_p = GET_MODE (operands[0]) == DImode;
9250   int round_mode = get_attr_i387_cw (insn);
9251
9252   /* Jump through a hoop or two for DImode, since the hardware has no
9253      non-popping instruction.  We used to do this a different way, but
9254      that was somewhat fragile and broke with post-reload splitters.  */
9255   if ((dimode_p || fisttp) && !stack_top_dies)
9256     output_asm_insn ("fld\t%y1", operands);
9257
9258   gcc_assert (STACK_TOP_P (operands[1]));
9259   gcc_assert (MEM_P (operands[0]));
9260   gcc_assert (GET_MODE (operands[1]) != TFmode);
9261
9262   if (fisttp)
9263       output_asm_insn ("fisttp%z0\t%0", operands);
9264   else
9265     {
9266       if (round_mode != I387_CW_ANY)
9267         output_asm_insn ("fldcw\t%3", operands);
9268       if (stack_top_dies || dimode_p)
9269         output_asm_insn ("fistp%z0\t%0", operands);
9270       else
9271         output_asm_insn ("fist%z0\t%0", operands);
9272       if (round_mode != I387_CW_ANY)
9273         output_asm_insn ("fldcw\t%2", operands);
9274     }
9275
9276   return "";
9277 }
9278
9279 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
9280    have the values zero or one, indicates the ffreep insn's operand
9281    from the OPERANDS array.  */
9282
9283 static const char *
9284 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
9285 {
9286   if (TARGET_USE_FFREEP)
9287 #if HAVE_AS_IX86_FFREEP
9288     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
9289 #else
9290     {
9291       static char retval[] = ".word\t0xc_df";
9292       int regno = REGNO (operands[opno]);
9293
9294       gcc_assert (FP_REGNO_P (regno));
9295
9296       retval[9] = '0' + (regno - FIRST_STACK_REG);
9297       return retval;
9298     }
9299 #endif
9300
9301   return opno ? "fstp\t%y1" : "fstp\t%y0";
9302 }
9303
9304
9305 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
9306    should be used.  UNORDERED_P is true when fucom should be used.  */
9307
9308 const char *
9309 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
9310 {
9311   int stack_top_dies;
9312   rtx cmp_op0, cmp_op1;
9313   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
9314
9315   if (eflags_p)
9316     {
9317       cmp_op0 = operands[0];
9318       cmp_op1 = operands[1];
9319     }
9320   else
9321     {
9322       cmp_op0 = operands[1];
9323       cmp_op1 = operands[2];
9324     }
9325
9326   if (is_sse)
9327     {
9328       if (GET_MODE (operands[0]) == SFmode)
9329         if (unordered_p)
9330           return "ucomiss\t{%1, %0|%0, %1}";
9331         else
9332           return "comiss\t{%1, %0|%0, %1}";
9333       else
9334         if (unordered_p)
9335           return "ucomisd\t{%1, %0|%0, %1}";
9336         else
9337           return "comisd\t{%1, %0|%0, %1}";
9338     }
9339
9340   gcc_assert (STACK_TOP_P (cmp_op0));
9341
9342   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9343
9344   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
9345     {
9346       if (stack_top_dies)
9347         {
9348           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
9349           return output_387_ffreep (operands, 1);
9350         }
9351       else
9352         return "ftst\n\tfnstsw\t%0";
9353     }
9354
9355   if (STACK_REG_P (cmp_op1)
9356       && stack_top_dies
9357       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
9358       && REGNO (cmp_op1) != FIRST_STACK_REG)
9359     {
9360       /* If both the top of the 387 stack dies, and the other operand
9361          is also a stack register that dies, then this must be a
9362          `fcompp' float compare */
9363
9364       if (eflags_p)
9365         {
9366           /* There is no double popping fcomi variant.  Fortunately,
9367              eflags is immune from the fstp's cc clobbering.  */
9368           if (unordered_p)
9369             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
9370           else
9371             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
9372           return output_387_ffreep (operands, 0);
9373         }
9374       else
9375         {
9376           if (unordered_p)
9377             return "fucompp\n\tfnstsw\t%0";
9378           else
9379             return "fcompp\n\tfnstsw\t%0";
9380         }
9381     }
9382   else
9383     {
9384       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
9385
9386       static const char * const alt[16] =
9387       {
9388         "fcom%z2\t%y2\n\tfnstsw\t%0",
9389         "fcomp%z2\t%y2\n\tfnstsw\t%0",
9390         "fucom%z2\t%y2\n\tfnstsw\t%0",
9391         "fucomp%z2\t%y2\n\tfnstsw\t%0",
9392
9393         "ficom%z2\t%y2\n\tfnstsw\t%0",
9394         "ficomp%z2\t%y2\n\tfnstsw\t%0",
9395         NULL,
9396         NULL,
9397
9398         "fcomi\t{%y1, %0|%0, %y1}",
9399         "fcomip\t{%y1, %0|%0, %y1}",
9400         "fucomi\t{%y1, %0|%0, %y1}",
9401         "fucomip\t{%y1, %0|%0, %y1}",
9402
9403         NULL,
9404         NULL,
9405         NULL,
9406         NULL
9407       };
9408
9409       int mask;
9410       const char *ret;
9411
9412       mask  = eflags_p << 3;
9413       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
9414       mask |= unordered_p << 1;
9415       mask |= stack_top_dies;
9416
9417       gcc_assert (mask < 16);
9418       ret = alt[mask];
9419       gcc_assert (ret);
9420
9421       return ret;
9422     }
9423 }
9424
9425 void
9426 ix86_output_addr_vec_elt (FILE *file, int value)
9427 {
9428   const char *directive = ASM_LONG;
9429
9430 #ifdef ASM_QUAD
9431   if (TARGET_64BIT)
9432     directive = ASM_QUAD;
9433 #else
9434   gcc_assert (!TARGET_64BIT);
9435 #endif
9436
9437   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
9438 }
9439
9440 void
9441 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
9442 {
9443   const char *directive = ASM_LONG;
9444
9445 #ifdef ASM_QUAD
9446   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
9447     directive = ASM_QUAD;
9448 #else
9449   gcc_assert (!TARGET_64BIT);
9450 #endif
9451   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
9452   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
9453     fprintf (file, "%s%s%d-%s%d\n",
9454              directive, LPREFIX, value, LPREFIX, rel);
9455   else if (HAVE_AS_GOTOFF_IN_DATA)
9456     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
9457 #if TARGET_MACHO
9458   else if (TARGET_MACHO)
9459     {
9460       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
9461       machopic_output_function_base_name (file);
9462       fprintf(file, "\n");
9463     }
9464 #endif
9465   else
9466     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
9467                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
9468 }
9469 \f
9470 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
9471    for the target.  */
9472
9473 void
9474 ix86_expand_clear (rtx dest)
9475 {
9476   rtx tmp;
9477
9478   /* We play register width games, which are only valid after reload.  */
9479   gcc_assert (reload_completed);
9480
9481   /* Avoid HImode and its attendant prefix byte.  */
9482   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
9483     dest = gen_rtx_REG (SImode, REGNO (dest));
9484   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
9485
9486   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
9487   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
9488     {
9489       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
9490       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
9491     }
9492
9493   emit_insn (tmp);
9494 }
9495
9496 /* X is an unchanging MEM.  If it is a constant pool reference, return
9497    the constant pool rtx, else NULL.  */
9498
9499 rtx
9500 maybe_get_pool_constant (rtx x)
9501 {
9502   x = ix86_delegitimize_address (XEXP (x, 0));
9503
9504   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
9505     return get_pool_constant (x);
9506
9507   return NULL_RTX;
9508 }
9509
9510 void
9511 ix86_expand_move (enum machine_mode mode, rtx operands[])
9512 {
9513   int strict = (reload_in_progress || reload_completed);
9514   rtx op0, op1;
9515   enum tls_model model;
9516
9517   op0 = operands[0];
9518   op1 = operands[1];
9519
9520   if (GET_CODE (op1) == SYMBOL_REF)
9521     {
9522       model = SYMBOL_REF_TLS_MODEL (op1);
9523       if (model)
9524         {
9525           op1 = legitimize_tls_address (op1, model, true);
9526           op1 = force_operand (op1, op0);
9527           if (op1 == op0)
9528             return;
9529         }
9530       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
9531                && SYMBOL_REF_DLLIMPORT_P (op1))
9532         op1 = legitimize_dllimport_symbol (op1, false);
9533     }
9534   else if (GET_CODE (op1) == CONST
9535            && GET_CODE (XEXP (op1, 0)) == PLUS
9536            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
9537     {
9538       rtx addend = XEXP (XEXP (op1, 0), 1);
9539       rtx symbol = XEXP (XEXP (op1, 0), 0);
9540       rtx tmp = NULL;
9541
9542       model = SYMBOL_REF_TLS_MODEL (symbol);
9543       if (model)
9544         tmp = legitimize_tls_address (symbol, model, true);
9545       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
9546                && SYMBOL_REF_DLLIMPORT_P (symbol))
9547         tmp = legitimize_dllimport_symbol (symbol, true);
9548
9549       if (tmp)
9550         {
9551           tmp = force_operand (tmp, NULL);
9552           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
9553                                      op0, 1, OPTAB_DIRECT);
9554           if (tmp == op0)
9555             return;
9556         }
9557     }
9558
9559   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
9560     {
9561       if (TARGET_MACHO && !TARGET_64BIT)
9562         {
9563 #if TARGET_MACHO
9564           if (MACHOPIC_PURE)
9565             {
9566               rtx temp = ((reload_in_progress
9567                            || ((op0 && REG_P (op0))
9568                                && mode == Pmode))
9569                           ? op0 : gen_reg_rtx (Pmode));
9570               op1 = machopic_indirect_data_reference (op1, temp);
9571               op1 = machopic_legitimize_pic_address (op1, mode,
9572                                                      temp == op1 ? 0 : temp);
9573             }
9574           else if (MACHOPIC_INDIRECT)
9575             op1 = machopic_indirect_data_reference (op1, 0);
9576           if (op0 == op1)
9577             return;
9578 #endif
9579         }
9580       else
9581         {
9582           if (MEM_P (op0))
9583             op1 = force_reg (Pmode, op1);
9584           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
9585             {
9586               rtx reg = no_new_pseudos ? op0 : NULL_RTX;
9587               op1 = legitimize_pic_address (op1, reg);
9588               if (op0 == op1)
9589                 return;
9590             }
9591         }
9592     }
9593   else
9594     {
9595       if (MEM_P (op0)
9596           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
9597               || !push_operand (op0, mode))
9598           && MEM_P (op1))
9599         op1 = force_reg (mode, op1);
9600
9601       if (push_operand (op0, mode)
9602           && ! general_no_elim_operand (op1, mode))
9603         op1 = copy_to_mode_reg (mode, op1);
9604
9605       /* Force large constants in 64bit compilation into register
9606          to get them CSEed.  */
9607       if (TARGET_64BIT && mode == DImode
9608           && immediate_operand (op1, mode)
9609           && !x86_64_zext_immediate_operand (op1, VOIDmode)
9610           && !register_operand (op0, mode)
9611           && optimize && !reload_completed && !reload_in_progress)
9612         op1 = copy_to_mode_reg (mode, op1);
9613
9614       if (FLOAT_MODE_P (mode))
9615         {
9616           /* If we are loading a floating point constant to a register,
9617              force the value to memory now, since we'll get better code
9618              out the back end.  */
9619
9620           if (strict)
9621             ;
9622           else if (GET_CODE (op1) == CONST_DOUBLE)
9623             {
9624               op1 = validize_mem (force_const_mem (mode, op1));
9625               if (!register_operand (op0, mode))
9626                 {
9627                   rtx temp = gen_reg_rtx (mode);
9628                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
9629                   emit_move_insn (op0, temp);
9630                   return;
9631                 }
9632             }
9633         }
9634     }
9635
9636   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9637 }
9638
9639 void
9640 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9641 {
9642   rtx op0 = operands[0], op1 = operands[1];
9643
9644   /* Force constants other than zero into memory.  We do not know how
9645      the instructions used to build constants modify the upper 64 bits
9646      of the register, once we have that information we may be able
9647      to handle some of them more efficiently.  */
9648   if ((reload_in_progress | reload_completed) == 0
9649       && register_operand (op0, mode)
9650       && CONSTANT_P (op1)
9651       && standard_sse_constant_p (op1) <= 0)
9652     op1 = validize_mem (force_const_mem (mode, op1));
9653
9654   /* Make operand1 a register if it isn't already.  */
9655   if (!no_new_pseudos
9656       && !register_operand (op0, mode)
9657       && !register_operand (op1, mode))
9658     {
9659       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9660       return;
9661     }
9662
9663   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9664 }
9665
9666 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9667    straight to ix86_expand_vector_move.  */
9668 /* Code generation for scalar reg-reg moves of single and double precision data:
9669      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
9670        movaps reg, reg
9671      else
9672        movss reg, reg
9673      if (x86_sse_partial_reg_dependency == true)
9674        movapd reg, reg
9675      else
9676        movsd reg, reg
9677
9678    Code generation for scalar loads of double precision data:
9679      if (x86_sse_split_regs == true)
9680        movlpd mem, reg      (gas syntax)
9681      else
9682        movsd mem, reg
9683
9684    Code generation for unaligned packed loads of single precision data
9685    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
9686      if (x86_sse_unaligned_move_optimal)
9687        movups mem, reg
9688
9689      if (x86_sse_partial_reg_dependency == true)
9690        {
9691          xorps  reg, reg
9692          movlps mem, reg
9693          movhps mem+8, reg
9694        }
9695      else
9696        {
9697          movlps mem, reg
9698          movhps mem+8, reg
9699        }
9700
9701    Code generation for unaligned packed loads of double precision data
9702    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
9703      if (x86_sse_unaligned_move_optimal)
9704        movupd mem, reg
9705
9706      if (x86_sse_split_regs == true)
9707        {
9708          movlpd mem, reg
9709          movhpd mem+8, reg
9710        }
9711      else
9712        {
9713          movsd  mem, reg
9714          movhpd mem+8, reg
9715        }
9716  */
9717
9718 void
9719 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9720 {
9721   rtx op0, op1, m;
9722
9723   op0 = operands[0];
9724   op1 = operands[1];
9725
9726   if (MEM_P (op1))
9727     {
9728       /* If we're optimizing for size, movups is the smallest.  */
9729       if (optimize_size)
9730         {
9731           op0 = gen_lowpart (V4SFmode, op0);
9732           op1 = gen_lowpart (V4SFmode, op1);
9733           emit_insn (gen_sse_movups (op0, op1));
9734           return;
9735         }
9736
9737       /* ??? If we have typed data, then it would appear that using
9738          movdqu is the only way to get unaligned data loaded with
9739          integer type.  */
9740       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9741         {
9742           op0 = gen_lowpart (V16QImode, op0);
9743           op1 = gen_lowpart (V16QImode, op1);
9744           emit_insn (gen_sse2_movdqu (op0, op1));
9745           return;
9746         }
9747
9748       if (TARGET_SSE2 && mode == V2DFmode)
9749         {
9750           rtx zero;
9751
9752           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9753             {
9754               op0 = gen_lowpart (V2DFmode, op0);
9755               op1 = gen_lowpart (V2DFmode, op1);
9756               emit_insn (gen_sse2_movupd (op0, op1));
9757               return;
9758             }
9759
9760           /* When SSE registers are split into halves, we can avoid
9761              writing to the top half twice.  */
9762           if (TARGET_SSE_SPLIT_REGS)
9763             {
9764               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9765               zero = op0;
9766             }
9767           else
9768             {
9769               /* ??? Not sure about the best option for the Intel chips.
9770                  The following would seem to satisfy; the register is
9771                  entirely cleared, breaking the dependency chain.  We
9772                  then store to the upper half, with a dependency depth
9773                  of one.  A rumor has it that Intel recommends two movsd
9774                  followed by an unpacklpd, but this is unconfirmed.  And
9775                  given that the dependency depth of the unpacklpd would
9776                  still be one, I'm not sure why this would be better.  */
9777               zero = CONST0_RTX (V2DFmode);
9778             }
9779
9780           m = adjust_address (op1, DFmode, 0);
9781           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9782           m = adjust_address (op1, DFmode, 8);
9783           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9784         }
9785       else
9786         {
9787           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9788             {
9789               op0 = gen_lowpart (V4SFmode, op0);
9790               op1 = gen_lowpart (V4SFmode, op1);
9791               emit_insn (gen_sse_movups (op0, op1));
9792               return;
9793             }
9794
9795           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9796             emit_move_insn (op0, CONST0_RTX (mode));
9797           else
9798             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9799
9800           if (mode != V4SFmode)
9801             op0 = gen_lowpart (V4SFmode, op0);
9802           m = adjust_address (op1, V2SFmode, 0);
9803           emit_insn (gen_sse_loadlps (op0, op0, m));
9804           m = adjust_address (op1, V2SFmode, 8);
9805           emit_insn (gen_sse_loadhps (op0, op0, m));
9806         }
9807     }
9808   else if (MEM_P (op0))
9809     {
9810       /* If we're optimizing for size, movups is the smallest.  */
9811       if (optimize_size)
9812         {
9813           op0 = gen_lowpart (V4SFmode, op0);
9814           op1 = gen_lowpart (V4SFmode, op1);
9815           emit_insn (gen_sse_movups (op0, op1));
9816           return;
9817         }
9818
9819       /* ??? Similar to above, only less clear because of quote
9820          typeless stores unquote.  */
9821       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9822           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9823         {
9824           op0 = gen_lowpart (V16QImode, op0);
9825           op1 = gen_lowpart (V16QImode, op1);
9826           emit_insn (gen_sse2_movdqu (op0, op1));
9827           return;
9828         }
9829
9830       if (TARGET_SSE2 && mode == V2DFmode)
9831         {
9832           m = adjust_address (op0, DFmode, 0);
9833           emit_insn (gen_sse2_storelpd (m, op1));
9834           m = adjust_address (op0, DFmode, 8);
9835           emit_insn (gen_sse2_storehpd (m, op1));
9836         }
9837       else
9838         {
9839           if (mode != V4SFmode)
9840             op1 = gen_lowpart (V4SFmode, op1);
9841           m = adjust_address (op0, V2SFmode, 0);
9842           emit_insn (gen_sse_storelps (m, op1));
9843           m = adjust_address (op0, V2SFmode, 8);
9844           emit_insn (gen_sse_storehps (m, op1));
9845         }
9846     }
9847   else
9848     gcc_unreachable ();
9849 }
9850
9851 /* Expand a push in MODE.  This is some mode for which we do not support
9852    proper push instructions, at least from the registers that we expect
9853    the value to live in.  */
9854
9855 void
9856 ix86_expand_push (enum machine_mode mode, rtx x)
9857 {
9858   rtx tmp;
9859
9860   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9861                              GEN_INT (-GET_MODE_SIZE (mode)),
9862                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9863   if (tmp != stack_pointer_rtx)
9864     emit_move_insn (stack_pointer_rtx, tmp);
9865
9866   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9867   emit_move_insn (tmp, x);
9868 }
9869
9870 /* Helper function of ix86_fixup_binary_operands to canonicalize
9871    operand order.  Returns true if the operands should be swapped.  */
9872
9873 static bool
9874 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
9875                              rtx operands[])
9876 {
9877   rtx dst = operands[0];
9878   rtx src1 = operands[1];
9879   rtx src2 = operands[2];
9880
9881   /* If the operation is not commutative, we can't do anything.  */
9882   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9883     return false;
9884
9885   /* Highest priority is that src1 should match dst.  */
9886   if (rtx_equal_p (dst, src1))
9887     return false;
9888   if (rtx_equal_p (dst, src2))
9889     return true;
9890
9891   /* Next highest priority is that immediate constants come second.  */
9892   if (immediate_operand (src2, mode))
9893     return false;
9894   if (immediate_operand (src1, mode))
9895     return true;
9896
9897   /* Lowest priority is that memory references should come second.  */
9898   if (MEM_P (src2))
9899     return false;
9900   if (MEM_P (src1))
9901     return true;
9902
9903   return false;
9904 }
9905
9906
9907 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9908    destination to use for the operation.  If different from the true
9909    destination in operands[0], a copy operation will be required.  */
9910
9911 rtx
9912 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9913                             rtx operands[])
9914 {
9915   rtx dst = operands[0];
9916   rtx src1 = operands[1];
9917   rtx src2 = operands[2];
9918
9919   /* Canonicalize operand order.  */
9920   if (ix86_swap_binary_operands_p (code, mode, operands))
9921     {
9922       rtx temp = src1;
9923       src1 = src2;
9924       src2 = temp;
9925     }
9926
9927   /* Both source operands cannot be in memory.  */
9928   if (MEM_P (src1) && MEM_P (src2))
9929     {
9930       /* Optimization: Only read from memory once.  */
9931       if (rtx_equal_p (src1, src2))
9932         {
9933           src2 = force_reg (mode, src2);
9934           src1 = src2;
9935         }
9936       else
9937         src2 = force_reg (mode, src2);
9938     }
9939
9940   /* If the destination is memory, and we do not have matching source
9941      operands, do things in registers.  */
9942   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
9943     dst = gen_reg_rtx (mode);
9944
9945   /* Source 1 cannot be a constant.  */
9946   if (CONSTANT_P (src1))
9947     src1 = force_reg (mode, src1);
9948
9949   /* Source 1 cannot be a non-matching memory.  */
9950   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
9951     src1 = force_reg (mode, src1);
9952
9953   operands[1] = src1;
9954   operands[2] = src2;
9955   return dst;
9956 }
9957
9958 /* Similarly, but assume that the destination has already been
9959    set up properly.  */
9960
9961 void
9962 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9963                                     enum machine_mode mode, rtx operands[])
9964 {
9965   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9966   gcc_assert (dst == operands[0]);
9967 }
9968
9969 /* Attempt to expand a binary operator.  Make the expansion closer to the
9970    actual machine, then just general_operand, which will allow 3 separate
9971    memory references (one output, two input) in a single insn.  */
9972
9973 void
9974 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9975                              rtx operands[])
9976 {
9977   rtx src1, src2, dst, op, clob;
9978
9979   dst = ix86_fixup_binary_operands (code, mode, operands);
9980   src1 = operands[1];
9981   src2 = operands[2];
9982
9983  /* Emit the instruction.  */
9984
9985   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9986   if (reload_in_progress)
9987     {
9988       /* Reload doesn't know about the flags register, and doesn't know that
9989          it doesn't want to clobber it.  We can only do this with PLUS.  */
9990       gcc_assert (code == PLUS);
9991       emit_insn (op);
9992     }
9993   else
9994     {
9995       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9996       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9997     }
9998
9999   /* Fix up the destination if needed.  */
10000   if (dst != operands[0])
10001     emit_move_insn (operands[0], dst);
10002 }
10003
10004 /* Return TRUE or FALSE depending on whether the binary operator meets the
10005    appropriate constraints.  */
10006
10007 int
10008 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
10009                          rtx operands[3])
10010 {
10011   rtx dst = operands[0];
10012   rtx src1 = operands[1];
10013   rtx src2 = operands[2];
10014
10015   /* Both source operands cannot be in memory.  */
10016   if (MEM_P (src1) && MEM_P (src2))
10017     return 0;
10018
10019   /* Canonicalize operand order for commutative operators.  */
10020   if (ix86_swap_binary_operands_p (code, mode, operands))
10021     {
10022       rtx temp = src1;
10023       src1 = src2;
10024       src2 = temp;
10025     }
10026
10027   /* If the destination is memory, we must have a matching source operand.  */
10028   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
10029       return 0;
10030
10031   /* Source 1 cannot be a constant.  */
10032   if (CONSTANT_P (src1))
10033     return 0;
10034
10035   /* Source 1 cannot be a non-matching memory.  */
10036   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
10037     return 0;
10038
10039   return 1;
10040 }
10041
10042 /* Attempt to expand a unary operator.  Make the expansion closer to the
10043    actual machine, then just general_operand, which will allow 2 separate
10044    memory references (one output, one input) in a single insn.  */
10045
10046 void
10047 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
10048                             rtx operands[])
10049 {
10050   int matching_memory;
10051   rtx src, dst, op, clob;
10052
10053   dst = operands[0];
10054   src = operands[1];
10055
10056   /* If the destination is memory, and we do not have matching source
10057      operands, do things in registers.  */
10058   matching_memory = 0;
10059   if (MEM_P (dst))
10060     {
10061       if (rtx_equal_p (dst, src))
10062         matching_memory = 1;
10063       else
10064         dst = gen_reg_rtx (mode);
10065     }
10066
10067   /* When source operand is memory, destination must match.  */
10068   if (MEM_P (src) && !matching_memory)
10069     src = force_reg (mode, src);
10070
10071   /* Emit the instruction.  */
10072
10073   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
10074   if (reload_in_progress || code == NOT)
10075     {
10076       /* Reload doesn't know about the flags register, and doesn't know that
10077          it doesn't want to clobber it.  */
10078       gcc_assert (code == NOT);
10079       emit_insn (op);
10080     }
10081   else
10082     {
10083       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10084       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
10085     }
10086
10087   /* Fix up the destination if needed.  */
10088   if (dst != operands[0])
10089     emit_move_insn (operands[0], dst);
10090 }
10091
10092 /* Return TRUE or FALSE depending on whether the unary operator meets the
10093    appropriate constraints.  */
10094
10095 int
10096 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
10097                         enum machine_mode mode ATTRIBUTE_UNUSED,
10098                         rtx operands[2] ATTRIBUTE_UNUSED)
10099 {
10100   /* If one of operands is memory, source and destination must match.  */
10101   if ((MEM_P (operands[0])
10102        || MEM_P (operands[1]))
10103       && ! rtx_equal_p (operands[0], operands[1]))
10104     return FALSE;
10105   return TRUE;
10106 }
10107
10108 /* Post-reload splitter for converting an SF or DFmode value in an
10109    SSE register into an unsigned SImode.  */
10110
10111 void
10112 ix86_split_convert_uns_si_sse (rtx operands[])
10113 {
10114   enum machine_mode vecmode;
10115   rtx value, large, zero_or_two31, input, two31, x;
10116
10117   large = operands[1];
10118   zero_or_two31 = operands[2];
10119   input = operands[3];
10120   two31 = operands[4];
10121   vecmode = GET_MODE (large);
10122   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
10123
10124   /* Load up the value into the low element.  We must ensure that the other
10125      elements are valid floats -- zero is the easiest such value.  */
10126   if (MEM_P (input))
10127     {
10128       if (vecmode == V4SFmode)
10129         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
10130       else
10131         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
10132     }
10133   else
10134     {
10135       input = gen_rtx_REG (vecmode, REGNO (input));
10136       emit_move_insn (value, CONST0_RTX (vecmode));
10137       if (vecmode == V4SFmode)
10138         emit_insn (gen_sse_movss (value, value, input));
10139       else
10140         emit_insn (gen_sse2_movsd (value, value, input));
10141     }
10142
10143   emit_move_insn (large, two31);
10144   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
10145
10146   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
10147   emit_insn (gen_rtx_SET (VOIDmode, large, x));
10148
10149   x = gen_rtx_AND (vecmode, zero_or_two31, large);
10150   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
10151
10152   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
10153   emit_insn (gen_rtx_SET (VOIDmode, value, x));
10154
10155   large = gen_rtx_REG (V4SImode, REGNO (large));
10156   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
10157
10158   x = gen_rtx_REG (V4SImode, REGNO (value));
10159   if (vecmode == V4SFmode)
10160     emit_insn (gen_sse2_cvttps2dq (x, value));
10161   else
10162     emit_insn (gen_sse2_cvttpd2dq (x, value));
10163   value = x;
10164
10165   emit_insn (gen_xorv4si3 (value, value, large));
10166 }
10167
10168 /* Convert an unsigned DImode value into a DFmode, using only SSE.
10169    Expects the 64-bit DImode to be supplied in a pair of integral
10170    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
10171    -mfpmath=sse, !optimize_size only.  */
10172
10173 void
10174 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
10175 {
10176   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
10177   rtx int_xmm, fp_xmm;
10178   rtx biases, exponents;
10179   rtx x;
10180
10181   int_xmm = gen_reg_rtx (V4SImode);
10182   if (TARGET_INTER_UNIT_MOVES)
10183     emit_insn (gen_movdi_to_sse (int_xmm, input));
10184   else if (TARGET_SSE_SPLIT_REGS)
10185     {
10186       emit_insn (gen_rtx_CLOBBER (VOIDmode, int_xmm));
10187       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
10188     }
10189   else
10190     {
10191       x = gen_reg_rtx (V2DImode);
10192       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
10193       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
10194     }
10195
10196   x = gen_rtx_CONST_VECTOR (V4SImode,
10197                             gen_rtvec (4, GEN_INT (0x43300000UL),
10198                                        GEN_INT (0x45300000UL),
10199                                        const0_rtx, const0_rtx));
10200   exponents = validize_mem (force_const_mem (V4SImode, x));
10201
10202   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
10203   emit_insn (gen_sse2_punpckldq (int_xmm, int_xmm, exponents));
10204
10205   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
10206      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
10207      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
10208      (0x1.0p84 + double(fp_value_hi_xmm)).
10209      Note these exponents differ by 32.  */
10210
10211   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
10212
10213   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
10214      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
10215   real_ldexp (&bias_lo_rvt, &dconst1, 52);
10216   real_ldexp (&bias_hi_rvt, &dconst1, 84);
10217   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
10218   x = const_double_from_real_value (bias_hi_rvt, DFmode);
10219   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
10220   biases = validize_mem (force_const_mem (V2DFmode, biases));
10221   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
10222
10223   /* Add the upper and lower DFmode values together.  */
10224   if (TARGET_SSE3)
10225     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
10226   else
10227     {
10228       x = copy_to_mode_reg (V2DFmode, fp_xmm);
10229       emit_insn (gen_sse2_unpckhpd (fp_xmm, fp_xmm, fp_xmm));
10230       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
10231     }
10232
10233   ix86_expand_vector_extract (false, target, fp_xmm, 0);
10234 }
10235
10236 /* Convert an unsigned SImode value into a DFmode.  Only currently used
10237    for SSE, but applicable anywhere.  */
10238
10239 void
10240 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
10241 {
10242   REAL_VALUE_TYPE TWO31r;
10243   rtx x, fp;
10244
10245   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
10246                            NULL, 1, OPTAB_DIRECT);
10247
10248   fp = gen_reg_rtx (DFmode);
10249   emit_insn (gen_floatsidf2 (fp, x));
10250
10251   real_ldexp (&TWO31r, &dconst1, 31);
10252   x = const_double_from_real_value (TWO31r, DFmode);
10253
10254   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
10255   if (x != target)
10256     emit_move_insn (target, x);
10257 }
10258
10259 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
10260    32-bit mode; otherwise we have a direct convert instruction.  */
10261
10262 void
10263 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
10264 {
10265   REAL_VALUE_TYPE TWO32r;
10266   rtx fp_lo, fp_hi, x;
10267
10268   fp_lo = gen_reg_rtx (DFmode);
10269   fp_hi = gen_reg_rtx (DFmode);
10270
10271   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
10272
10273   real_ldexp (&TWO32r, &dconst1, 32);
10274   x = const_double_from_real_value (TWO32r, DFmode);
10275   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
10276
10277   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
10278
10279   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
10280                            0, OPTAB_DIRECT);
10281   if (x != target)
10282     emit_move_insn (target, x);
10283 }
10284
10285 /* Convert an unsigned SImode value into a SFmode, using only SSE.
10286    For x86_32, -mfpmath=sse, !optimize_size only.  */
10287 void
10288 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
10289 {
10290   REAL_VALUE_TYPE ONE16r;
10291   rtx fp_hi, fp_lo, int_hi, int_lo, x;
10292
10293   real_ldexp (&ONE16r, &dconst1, 16);
10294   x = const_double_from_real_value (ONE16r, SFmode);
10295   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
10296                                       NULL, 0, OPTAB_DIRECT);
10297   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
10298                                       NULL, 0, OPTAB_DIRECT);
10299   fp_hi = gen_reg_rtx (SFmode);
10300   fp_lo = gen_reg_rtx (SFmode);
10301   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
10302   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
10303   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
10304                                0, OPTAB_DIRECT);
10305   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
10306                                0, OPTAB_DIRECT);
10307   if (!rtx_equal_p (target, fp_hi))
10308     emit_move_insn (target, fp_hi);
10309 }
10310
10311 /* A subroutine of ix86_build_signbit_mask_vector.  If VECT is true,
10312    then replicate the value for all elements of the vector
10313    register.  */
10314
10315 rtx
10316 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
10317 {
10318   rtvec v;
10319   switch (mode)
10320     {
10321     case SFmode:
10322       if (vect)
10323         v = gen_rtvec (4, value, value, value, value);
10324       else
10325         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
10326                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10327       return gen_rtx_CONST_VECTOR (V4SFmode, v);
10328
10329     case DFmode:
10330       if (vect)
10331         v = gen_rtvec (2, value, value);
10332       else
10333         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
10334       return gen_rtx_CONST_VECTOR (V2DFmode, v);
10335
10336     default:
10337       gcc_unreachable ();
10338     }
10339 }
10340
10341 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
10342    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
10343    true, then replicate the mask for all elements of the vector register.
10344    If INVERT is true, then create a mask excluding the sign bit.  */
10345
10346 rtx
10347 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
10348 {
10349   enum machine_mode vec_mode;
10350   HOST_WIDE_INT hi, lo;
10351   int shift = 63;
10352   rtx v;
10353   rtx mask;
10354
10355   /* Find the sign bit, sign extended to 2*HWI.  */
10356   if (mode == SFmode)
10357     lo = 0x80000000, hi = lo < 0;
10358   else if (HOST_BITS_PER_WIDE_INT >= 64)
10359     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
10360   else
10361     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
10362
10363   if (invert)
10364     lo = ~lo, hi = ~hi;
10365
10366   /* Force this value into the low part of a fp vector constant.  */
10367   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
10368   mask = gen_lowpart (mode, mask);
10369
10370   v = ix86_build_const_vector (mode, vect, mask);
10371   vec_mode = (mode == SFmode) ? V4SFmode : V2DFmode;
10372   return force_reg (vec_mode, v);
10373 }
10374
10375 /* Generate code for floating point ABS or NEG.  */
10376
10377 void
10378 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
10379                                 rtx operands[])
10380 {
10381   rtx mask, set, use, clob, dst, src;
10382   bool matching_memory;
10383   bool use_sse = false;
10384   bool vector_mode = VECTOR_MODE_P (mode);
10385   enum machine_mode elt_mode = mode;
10386
10387   if (vector_mode)
10388     {
10389       elt_mode = GET_MODE_INNER (mode);
10390       use_sse = true;
10391     }
10392   else if (TARGET_SSE_MATH)
10393     use_sse = SSE_FLOAT_MODE_P (mode);
10394
10395   /* NEG and ABS performed with SSE use bitwise mask operations.
10396      Create the appropriate mask now.  */
10397   if (use_sse)
10398     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
10399   else
10400     mask = NULL_RTX;
10401
10402   dst = operands[0];
10403   src = operands[1];
10404
10405   /* If the destination is memory, and we don't have matching source
10406      operands or we're using the x87, do things in registers.  */
10407   matching_memory = false;
10408   if (MEM_P (dst))
10409     {
10410       if (use_sse && rtx_equal_p (dst, src))
10411         matching_memory = true;
10412       else
10413         dst = gen_reg_rtx (mode);
10414     }
10415   if (MEM_P (src) && !matching_memory)
10416     src = force_reg (mode, src);
10417
10418   if (vector_mode)
10419     {
10420       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
10421       set = gen_rtx_SET (VOIDmode, dst, set);
10422       emit_insn (set);
10423     }
10424   else
10425     {
10426       set = gen_rtx_fmt_e (code, mode, src);
10427       set = gen_rtx_SET (VOIDmode, dst, set);
10428       if (mask)
10429         {
10430           use = gen_rtx_USE (VOIDmode, mask);
10431           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10432           emit_insn (gen_rtx_PARALLEL (VOIDmode,
10433                                        gen_rtvec (3, set, use, clob)));
10434         }
10435       else
10436         emit_insn (set);
10437     }
10438
10439   if (dst != operands[0])
10440     emit_move_insn (operands[0], dst);
10441 }
10442
10443 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
10444
10445 void
10446 ix86_expand_copysign (rtx operands[])
10447 {
10448   enum machine_mode mode, vmode;
10449   rtx dest, op0, op1, mask, nmask;
10450
10451   dest = operands[0];
10452   op0 = operands[1];
10453   op1 = operands[2];
10454
10455   mode = GET_MODE (dest);
10456   vmode = mode == SFmode ? V4SFmode : V2DFmode;
10457
10458   if (GET_CODE (op0) == CONST_DOUBLE)
10459     {
10460       rtvec v;
10461
10462       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
10463         op0 = simplify_unary_operation (ABS, mode, op0, mode);
10464
10465       if (op0 == CONST0_RTX (mode))
10466         op0 = CONST0_RTX (vmode);
10467       else
10468         {
10469           if (mode == SFmode)
10470             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
10471                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10472           else
10473             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
10474           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
10475         }
10476
10477       mask = ix86_build_signbit_mask (mode, 0, 0);
10478
10479       if (mode == SFmode)
10480         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
10481       else
10482         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
10483     }
10484   else
10485     {
10486       nmask = ix86_build_signbit_mask (mode, 0, 1);
10487       mask = ix86_build_signbit_mask (mode, 0, 0);
10488
10489       if (mode == SFmode)
10490         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
10491       else
10492         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
10493     }
10494 }
10495
10496 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
10497    be a constant, and so has already been expanded into a vector constant.  */
10498
10499 void
10500 ix86_split_copysign_const (rtx operands[])
10501 {
10502   enum machine_mode mode, vmode;
10503   rtx dest, op0, op1, mask, x;
10504
10505   dest = operands[0];
10506   op0 = operands[1];
10507   op1 = operands[2];
10508   mask = operands[3];
10509
10510   mode = GET_MODE (dest);
10511   vmode = GET_MODE (mask);
10512
10513   dest = simplify_gen_subreg (vmode, dest, mode, 0);
10514   x = gen_rtx_AND (vmode, dest, mask);
10515   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10516
10517   if (op0 != CONST0_RTX (vmode))
10518     {
10519       x = gen_rtx_IOR (vmode, dest, op0);
10520       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10521     }
10522 }
10523
10524 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
10525    so we have to do two masks.  */
10526
10527 void
10528 ix86_split_copysign_var (rtx operands[])
10529 {
10530   enum machine_mode mode, vmode;
10531   rtx dest, scratch, op0, op1, mask, nmask, x;
10532
10533   dest = operands[0];
10534   scratch = operands[1];
10535   op0 = operands[2];
10536   op1 = operands[3];
10537   nmask = operands[4];
10538   mask = operands[5];
10539
10540   mode = GET_MODE (dest);
10541   vmode = GET_MODE (mask);
10542
10543   if (rtx_equal_p (op0, op1))
10544     {
10545       /* Shouldn't happen often (it's useless, obviously), but when it does
10546          we'd generate incorrect code if we continue below.  */
10547       emit_move_insn (dest, op0);
10548       return;
10549     }
10550
10551   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
10552     {
10553       gcc_assert (REGNO (op1) == REGNO (scratch));
10554
10555       x = gen_rtx_AND (vmode, scratch, mask);
10556       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10557
10558       dest = mask;
10559       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10560       x = gen_rtx_NOT (vmode, dest);
10561       x = gen_rtx_AND (vmode, x, op0);
10562       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10563     }
10564   else
10565     {
10566       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
10567         {
10568           x = gen_rtx_AND (vmode, scratch, mask);
10569         }
10570       else                                              /* alternative 2,4 */
10571         {
10572           gcc_assert (REGNO (mask) == REGNO (scratch));
10573           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
10574           x = gen_rtx_AND (vmode, scratch, op1);
10575         }
10576       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10577
10578       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
10579         {
10580           dest = simplify_gen_subreg (vmode, op0, mode, 0);
10581           x = gen_rtx_AND (vmode, dest, nmask);
10582         }
10583       else                                              /* alternative 3,4 */
10584         {
10585           gcc_assert (REGNO (nmask) == REGNO (dest));
10586           dest = nmask;
10587           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10588           x = gen_rtx_AND (vmode, dest, op0);
10589         }
10590       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10591     }
10592
10593   x = gen_rtx_IOR (vmode, dest, scratch);
10594   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10595 }
10596
10597 /* Return TRUE or FALSE depending on whether the first SET in INSN
10598    has source and destination with matching CC modes, and that the
10599    CC mode is at least as constrained as REQ_MODE.  */
10600
10601 int
10602 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
10603 {
10604   rtx set;
10605   enum machine_mode set_mode;
10606
10607   set = PATTERN (insn);
10608   if (GET_CODE (set) == PARALLEL)
10609     set = XVECEXP (set, 0, 0);
10610   gcc_assert (GET_CODE (set) == SET);
10611   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
10612
10613   set_mode = GET_MODE (SET_DEST (set));
10614   switch (set_mode)
10615     {
10616     case CCNOmode:
10617       if (req_mode != CCNOmode
10618           && (req_mode != CCmode
10619               || XEXP (SET_SRC (set), 1) != const0_rtx))
10620         return 0;
10621       break;
10622     case CCmode:
10623       if (req_mode == CCGCmode)
10624         return 0;
10625       /* FALLTHRU */
10626     case CCGCmode:
10627       if (req_mode == CCGOCmode || req_mode == CCNOmode)
10628         return 0;
10629       /* FALLTHRU */
10630     case CCGOCmode:
10631       if (req_mode == CCZmode)
10632         return 0;
10633       /* FALLTHRU */
10634     case CCZmode:
10635       break;
10636
10637     default:
10638       gcc_unreachable ();
10639     }
10640
10641   return (GET_MODE (SET_SRC (set)) == set_mode);
10642 }
10643
10644 /* Generate insn patterns to do an integer compare of OPERANDS.  */
10645
10646 static rtx
10647 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
10648 {
10649   enum machine_mode cmpmode;
10650   rtx tmp, flags;
10651
10652   cmpmode = SELECT_CC_MODE (code, op0, op1);
10653   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
10654
10655   /* This is very simple, but making the interface the same as in the
10656      FP case makes the rest of the code easier.  */
10657   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
10658   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
10659
10660   /* Return the test that should be put into the flags user, i.e.
10661      the bcc, scc, or cmov instruction.  */
10662   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
10663 }
10664
10665 /* Figure out whether to use ordered or unordered fp comparisons.
10666    Return the appropriate mode to use.  */
10667
10668 enum machine_mode
10669 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
10670 {
10671   /* ??? In order to make all comparisons reversible, we do all comparisons
10672      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
10673      all forms trapping and nontrapping comparisons, we can make inequality
10674      comparisons trapping again, since it results in better code when using
10675      FCOM based compares.  */
10676   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
10677 }
10678
10679 enum machine_mode
10680 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
10681 {
10682   enum machine_mode mode = GET_MODE (op0);
10683
10684   if (SCALAR_FLOAT_MODE_P (mode))
10685     {
10686       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
10687       return ix86_fp_compare_mode (code);
10688     }
10689
10690   switch (code)
10691     {
10692       /* Only zero flag is needed.  */
10693     case EQ:                    /* ZF=0 */
10694     case NE:                    /* ZF!=0 */
10695       return CCZmode;
10696       /* Codes needing carry flag.  */
10697     case GEU:                   /* CF=0 */
10698     case GTU:                   /* CF=0 & ZF=0 */
10699     case LTU:                   /* CF=1 */
10700     case LEU:                   /* CF=1 | ZF=1 */
10701       return CCmode;
10702       /* Codes possibly doable only with sign flag when
10703          comparing against zero.  */
10704     case GE:                    /* SF=OF   or   SF=0 */
10705     case LT:                    /* SF<>OF  or   SF=1 */
10706       if (op1 == const0_rtx)
10707         return CCGOCmode;
10708       else
10709         /* For other cases Carry flag is not required.  */
10710         return CCGCmode;
10711       /* Codes doable only with sign flag when comparing
10712          against zero, but we miss jump instruction for it
10713          so we need to use relational tests against overflow
10714          that thus needs to be zero.  */
10715     case GT:                    /* ZF=0 & SF=OF */
10716     case LE:                    /* ZF=1 | SF<>OF */
10717       if (op1 == const0_rtx)
10718         return CCNOmode;
10719       else
10720         return CCGCmode;
10721       /* strcmp pattern do (use flags) and combine may ask us for proper
10722          mode.  */
10723     case USE:
10724       return CCmode;
10725     default:
10726       gcc_unreachable ();
10727     }
10728 }
10729
10730 /* Return the fixed registers used for condition codes.  */
10731
10732 static bool
10733 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
10734 {
10735   *p1 = FLAGS_REG;
10736   *p2 = FPSR_REG;
10737   return true;
10738 }
10739
10740 /* If two condition code modes are compatible, return a condition code
10741    mode which is compatible with both.  Otherwise, return
10742    VOIDmode.  */
10743
10744 static enum machine_mode
10745 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
10746 {
10747   if (m1 == m2)
10748     return m1;
10749
10750   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
10751     return VOIDmode;
10752
10753   if ((m1 == CCGCmode && m2 == CCGOCmode)
10754       || (m1 == CCGOCmode && m2 == CCGCmode))
10755     return CCGCmode;
10756
10757   switch (m1)
10758     {
10759     default:
10760       gcc_unreachable ();
10761
10762     case CCmode:
10763     case CCGCmode:
10764     case CCGOCmode:
10765     case CCNOmode:
10766     case CCZmode:
10767       switch (m2)
10768         {
10769         default:
10770           return VOIDmode;
10771
10772         case CCmode:
10773         case CCGCmode:
10774         case CCGOCmode:
10775         case CCNOmode:
10776         case CCZmode:
10777           return CCmode;
10778         }
10779
10780     case CCFPmode:
10781     case CCFPUmode:
10782       /* These are only compatible with themselves, which we already
10783          checked above.  */
10784       return VOIDmode;
10785     }
10786 }
10787
10788 /* Split comparison code CODE into comparisons we can do using branch
10789    instructions.  BYPASS_CODE is comparison code for branch that will
10790    branch around FIRST_CODE and SECOND_CODE.  If some of branches
10791    is not required, set value to UNKNOWN.
10792    We never require more than two branches.  */
10793
10794 void
10795 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
10796                           enum rtx_code *first_code,
10797                           enum rtx_code *second_code)
10798 {
10799   *first_code = code;
10800   *bypass_code = UNKNOWN;
10801   *second_code = UNKNOWN;
10802
10803   /* The fcomi comparison sets flags as follows:
10804
10805      cmp    ZF PF CF
10806      >      0  0  0
10807      <      0  0  1
10808      =      1  0  0
10809      un     1  1  1 */
10810
10811   switch (code)
10812     {
10813     case GT:                    /* GTU - CF=0 & ZF=0 */
10814     case GE:                    /* GEU - CF=0 */
10815     case ORDERED:               /* PF=0 */
10816     case UNORDERED:             /* PF=1 */
10817     case UNEQ:                  /* EQ - ZF=1 */
10818     case UNLT:                  /* LTU - CF=1 */
10819     case UNLE:                  /* LEU - CF=1 | ZF=1 */
10820     case LTGT:                  /* EQ - ZF=0 */
10821       break;
10822     case LT:                    /* LTU - CF=1 - fails on unordered */
10823       *first_code = UNLT;
10824       *bypass_code = UNORDERED;
10825       break;
10826     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
10827       *first_code = UNLE;
10828       *bypass_code = UNORDERED;
10829       break;
10830     case EQ:                    /* EQ - ZF=1 - fails on unordered */
10831       *first_code = UNEQ;
10832       *bypass_code = UNORDERED;
10833       break;
10834     case NE:                    /* NE - ZF=0 - fails on unordered */
10835       *first_code = LTGT;
10836       *second_code = UNORDERED;
10837       break;
10838     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10839       *first_code = GE;
10840       *second_code = UNORDERED;
10841       break;
10842     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10843       *first_code = GT;
10844       *second_code = UNORDERED;
10845       break;
10846     default:
10847       gcc_unreachable ();
10848     }
10849   if (!TARGET_IEEE_FP)
10850     {
10851       *second_code = UNKNOWN;
10852       *bypass_code = UNKNOWN;
10853     }
10854 }
10855
10856 /* Return cost of comparison done fcom + arithmetics operations on AX.
10857    All following functions do use number of instructions as a cost metrics.
10858    In future this should be tweaked to compute bytes for optimize_size and
10859    take into account performance of various instructions on various CPUs.  */
10860 static int
10861 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10862 {
10863   if (!TARGET_IEEE_FP)
10864     return 4;
10865   /* The cost of code output by ix86_expand_fp_compare.  */
10866   switch (code)
10867     {
10868     case UNLE:
10869     case UNLT:
10870     case LTGT:
10871     case GT:
10872     case GE:
10873     case UNORDERED:
10874     case ORDERED:
10875     case UNEQ:
10876       return 4;
10877       break;
10878     case LT:
10879     case NE:
10880     case EQ:
10881     case UNGE:
10882       return 5;
10883       break;
10884     case LE:
10885     case UNGT:
10886       return 6;
10887       break;
10888     default:
10889       gcc_unreachable ();
10890     }
10891 }
10892
10893 /* Return cost of comparison done using fcomi operation.
10894    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10895 static int
10896 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10897 {
10898   enum rtx_code bypass_code, first_code, second_code;
10899   /* Return arbitrarily high cost when instruction is not supported - this
10900      prevents gcc from using it.  */
10901   if (!TARGET_CMOVE)
10902     return 1024;
10903   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10904   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10905 }
10906
10907 /* Return cost of comparison done using sahf operation.
10908    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10909 static int
10910 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10911 {
10912   enum rtx_code bypass_code, first_code, second_code;
10913   /* Return arbitrarily high cost when instruction is not preferred - this
10914      avoids gcc from using it.  */
10915   if (!(TARGET_SAHF && (TARGET_USE_SAHF || optimize_size)))
10916     return 1024;
10917   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10918   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10919 }
10920
10921 /* Compute cost of the comparison done using any method.
10922    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10923 static int
10924 ix86_fp_comparison_cost (enum rtx_code code)
10925 {
10926   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10927   int min;
10928
10929   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10930   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10931
10932   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10933   if (min > sahf_cost)
10934     min = sahf_cost;
10935   if (min > fcomi_cost)
10936     min = fcomi_cost;
10937   return min;
10938 }
10939
10940 /* Return true if we should use an FCOMI instruction for this
10941    fp comparison.  */
10942
10943 int
10944 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
10945 {
10946   enum rtx_code swapped_code = swap_condition (code);
10947
10948   return ((ix86_fp_comparison_cost (code)
10949            == ix86_fp_comparison_fcomi_cost (code))
10950           || (ix86_fp_comparison_cost (swapped_code)
10951               == ix86_fp_comparison_fcomi_cost (swapped_code)));
10952 }
10953
10954 /* Swap, force into registers, or otherwise massage the two operands
10955    to a fp comparison.  The operands are updated in place; the new
10956    comparison code is returned.  */
10957
10958 static enum rtx_code
10959 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
10960 {
10961   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
10962   rtx op0 = *pop0, op1 = *pop1;
10963   enum machine_mode op_mode = GET_MODE (op0);
10964   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
10965
10966   /* All of the unordered compare instructions only work on registers.
10967      The same is true of the fcomi compare instructions.  The XFmode
10968      compare instructions require registers except when comparing
10969      against zero or when converting operand 1 from fixed point to
10970      floating point.  */
10971
10972   if (!is_sse
10973       && (fpcmp_mode == CCFPUmode
10974           || (op_mode == XFmode
10975               && ! (standard_80387_constant_p (op0) == 1
10976                     || standard_80387_constant_p (op1) == 1)
10977               && GET_CODE (op1) != FLOAT)
10978           || ix86_use_fcomi_compare (code)))
10979     {
10980       op0 = force_reg (op_mode, op0);
10981       op1 = force_reg (op_mode, op1);
10982     }
10983   else
10984     {
10985       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
10986          things around if they appear profitable, otherwise force op0
10987          into a register.  */
10988
10989       if (standard_80387_constant_p (op0) == 0
10990           || (MEM_P (op0)
10991               && ! (standard_80387_constant_p (op1) == 0
10992                     || MEM_P (op1))))
10993         {
10994           rtx tmp;
10995           tmp = op0, op0 = op1, op1 = tmp;
10996           code = swap_condition (code);
10997         }
10998
10999       if (!REG_P (op0))
11000         op0 = force_reg (op_mode, op0);
11001
11002       if (CONSTANT_P (op1))
11003         {
11004           int tmp = standard_80387_constant_p (op1);
11005           if (tmp == 0)
11006             op1 = validize_mem (force_const_mem (op_mode, op1));
11007           else if (tmp == 1)
11008             {
11009               if (TARGET_CMOVE)
11010                 op1 = force_reg (op_mode, op1);
11011             }
11012           else
11013             op1 = force_reg (op_mode, op1);
11014         }
11015     }
11016
11017   /* Try to rearrange the comparison to make it cheaper.  */
11018   if (ix86_fp_comparison_cost (code)
11019       > ix86_fp_comparison_cost (swap_condition (code))
11020       && (REG_P (op1) || !no_new_pseudos))
11021     {
11022       rtx tmp;
11023       tmp = op0, op0 = op1, op1 = tmp;
11024       code = swap_condition (code);
11025       if (!REG_P (op0))
11026         op0 = force_reg (op_mode, op0);
11027     }
11028
11029   *pop0 = op0;
11030   *pop1 = op1;
11031   return code;
11032 }
11033
11034 /* Convert comparison codes we use to represent FP comparison to integer
11035    code that will result in proper branch.  Return UNKNOWN if no such code
11036    is available.  */
11037
11038 enum rtx_code
11039 ix86_fp_compare_code_to_integer (enum rtx_code code)
11040 {
11041   switch (code)
11042     {
11043     case GT:
11044       return GTU;
11045     case GE:
11046       return GEU;
11047     case ORDERED:
11048     case UNORDERED:
11049       return code;
11050       break;
11051     case UNEQ:
11052       return EQ;
11053       break;
11054     case UNLT:
11055       return LTU;
11056       break;
11057     case UNLE:
11058       return LEU;
11059       break;
11060     case LTGT:
11061       return NE;
11062       break;
11063     default:
11064       return UNKNOWN;
11065     }
11066 }
11067
11068 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
11069
11070 static rtx
11071 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
11072                         rtx *second_test, rtx *bypass_test)
11073 {
11074   enum machine_mode fpcmp_mode, intcmp_mode;
11075   rtx tmp, tmp2;
11076   int cost = ix86_fp_comparison_cost (code);
11077   enum rtx_code bypass_code, first_code, second_code;
11078
11079   fpcmp_mode = ix86_fp_compare_mode (code);
11080   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
11081
11082   if (second_test)
11083     *second_test = NULL_RTX;
11084   if (bypass_test)
11085     *bypass_test = NULL_RTX;
11086
11087   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11088
11089   /* Do fcomi/sahf based test when profitable.  */
11090   if ((TARGET_CMOVE || TARGET_SAHF)
11091       && (bypass_code == UNKNOWN || bypass_test)
11092       && (second_code == UNKNOWN || second_test)
11093       && ix86_fp_comparison_arithmetics_cost (code) > cost)
11094     {
11095       if (TARGET_CMOVE)
11096         {
11097           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11098           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
11099                              tmp);
11100           emit_insn (tmp);
11101         }
11102       else
11103         {
11104           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11105           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
11106           if (!scratch)
11107             scratch = gen_reg_rtx (HImode);
11108           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
11109           emit_insn (gen_x86_sahf_1 (scratch));
11110         }
11111
11112       /* The FP codes work out to act like unsigned.  */
11113       intcmp_mode = fpcmp_mode;
11114       code = first_code;
11115       if (bypass_code != UNKNOWN)
11116         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
11117                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11118                                        const0_rtx);
11119       if (second_code != UNKNOWN)
11120         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
11121                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11122                                        const0_rtx);
11123     }
11124   else
11125     {
11126       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
11127       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11128       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
11129       if (!scratch)
11130         scratch = gen_reg_rtx (HImode);
11131       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
11132
11133       /* In the unordered case, we have to check C2 for NaN's, which
11134          doesn't happen to work out to anything nice combination-wise.
11135          So do some bit twiddling on the value we've got in AH to come
11136          up with an appropriate set of condition codes.  */
11137
11138       intcmp_mode = CCNOmode;
11139       switch (code)
11140         {
11141         case GT:
11142         case UNGT:
11143           if (code == GT || !TARGET_IEEE_FP)
11144             {
11145               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11146               code = EQ;
11147             }
11148           else
11149             {
11150               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11151               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11152               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
11153               intcmp_mode = CCmode;
11154               code = GEU;
11155             }
11156           break;
11157         case LT:
11158         case UNLT:
11159           if (code == LT && TARGET_IEEE_FP)
11160             {
11161               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11162               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
11163               intcmp_mode = CCmode;
11164               code = EQ;
11165             }
11166           else
11167             {
11168               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
11169               code = NE;
11170             }
11171           break;
11172         case GE:
11173         case UNGE:
11174           if (code == GE || !TARGET_IEEE_FP)
11175             {
11176               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
11177               code = EQ;
11178             }
11179           else
11180             {
11181               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11182               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11183                                              GEN_INT (0x01)));
11184               code = NE;
11185             }
11186           break;
11187         case LE:
11188         case UNLE:
11189           if (code == LE && TARGET_IEEE_FP)
11190             {
11191               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11192               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11193               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11194               intcmp_mode = CCmode;
11195               code = LTU;
11196             }
11197           else
11198             {
11199               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11200               code = NE;
11201             }
11202           break;
11203         case EQ:
11204         case UNEQ:
11205           if (code == EQ && TARGET_IEEE_FP)
11206             {
11207               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11208               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11209               intcmp_mode = CCmode;
11210               code = EQ;
11211             }
11212           else
11213             {
11214               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11215               code = NE;
11216               break;
11217             }
11218           break;
11219         case NE:
11220         case LTGT:
11221           if (code == NE && TARGET_IEEE_FP)
11222             {
11223               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11224               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11225                                              GEN_INT (0x40)));
11226               code = NE;
11227             }
11228           else
11229             {
11230               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11231               code = EQ;
11232             }
11233           break;
11234
11235         case UNORDERED:
11236           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11237           code = NE;
11238           break;
11239         case ORDERED:
11240           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11241           code = EQ;
11242           break;
11243
11244         default:
11245           gcc_unreachable ();
11246         }
11247     }
11248
11249   /* Return the test that should be put into the flags user, i.e.
11250      the bcc, scc, or cmov instruction.  */
11251   return gen_rtx_fmt_ee (code, VOIDmode,
11252                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
11253                          const0_rtx);
11254 }
11255
11256 rtx
11257 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
11258 {
11259   rtx op0, op1, ret;
11260   op0 = ix86_compare_op0;
11261   op1 = ix86_compare_op1;
11262
11263   if (second_test)
11264     *second_test = NULL_RTX;
11265   if (bypass_test)
11266     *bypass_test = NULL_RTX;
11267
11268   if (ix86_compare_emitted)
11269     {
11270       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
11271       ix86_compare_emitted = NULL_RTX;
11272     }
11273   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
11274     {
11275       gcc_assert (!DECIMAL_FLOAT_MODE_P (GET_MODE (op0)));
11276       ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11277                                     second_test, bypass_test);
11278     }
11279   else
11280     ret = ix86_expand_int_compare (code, op0, op1);
11281
11282   return ret;
11283 }
11284
11285 /* Return true if the CODE will result in nontrivial jump sequence.  */
11286 bool
11287 ix86_fp_jump_nontrivial_p (enum rtx_code code)
11288 {
11289   enum rtx_code bypass_code, first_code, second_code;
11290   if (!TARGET_CMOVE)
11291     return true;
11292   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11293   return bypass_code != UNKNOWN || second_code != UNKNOWN;
11294 }
11295
11296 void
11297 ix86_expand_branch (enum rtx_code code, rtx label)
11298 {
11299   rtx tmp;
11300
11301   /* If we have emitted a compare insn, go straight to simple.
11302      ix86_expand_compare won't emit anything if ix86_compare_emitted
11303      is non NULL.  */
11304   if (ix86_compare_emitted)
11305     goto simple;
11306
11307   switch (GET_MODE (ix86_compare_op0))
11308     {
11309     case QImode:
11310     case HImode:
11311     case SImode:
11312       simple:
11313       tmp = ix86_expand_compare (code, NULL, NULL);
11314       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11315                                   gen_rtx_LABEL_REF (VOIDmode, label),
11316                                   pc_rtx);
11317       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
11318       return;
11319
11320     case SFmode:
11321     case DFmode:
11322     case XFmode:
11323       {
11324         rtvec vec;
11325         int use_fcomi;
11326         enum rtx_code bypass_code, first_code, second_code;
11327
11328         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
11329                                              &ix86_compare_op1);
11330
11331         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11332
11333         /* Check whether we will use the natural sequence with one jump.  If
11334            so, we can expand jump early.  Otherwise delay expansion by
11335            creating compound insn to not confuse optimizers.  */
11336         if (bypass_code == UNKNOWN && second_code == UNKNOWN
11337             && TARGET_CMOVE)
11338           {
11339             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
11340                                   gen_rtx_LABEL_REF (VOIDmode, label),
11341                                   pc_rtx, NULL_RTX, NULL_RTX);
11342           }
11343         else
11344           {
11345             tmp = gen_rtx_fmt_ee (code, VOIDmode,
11346                                   ix86_compare_op0, ix86_compare_op1);
11347             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11348                                         gen_rtx_LABEL_REF (VOIDmode, label),
11349                                         pc_rtx);
11350             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
11351
11352             use_fcomi = ix86_use_fcomi_compare (code);
11353             vec = rtvec_alloc (3 + !use_fcomi);
11354             RTVEC_ELT (vec, 0) = tmp;
11355             RTVEC_ELT (vec, 1)
11356               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
11357             RTVEC_ELT (vec, 2)
11358               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
11359             if (! use_fcomi)
11360               RTVEC_ELT (vec, 3)
11361                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
11362
11363             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
11364           }
11365         return;
11366       }
11367
11368     case DImode:
11369       if (TARGET_64BIT)
11370         goto simple;
11371     case TImode:
11372       /* Expand DImode branch into multiple compare+branch.  */
11373       {
11374         rtx lo[2], hi[2], label2;
11375         enum rtx_code code1, code2, code3;
11376         enum machine_mode submode;
11377
11378         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
11379           {
11380             tmp = ix86_compare_op0;
11381             ix86_compare_op0 = ix86_compare_op1;
11382             ix86_compare_op1 = tmp;
11383             code = swap_condition (code);
11384           }
11385         if (GET_MODE (ix86_compare_op0) == DImode)
11386           {
11387             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
11388             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
11389             submode = SImode;
11390           }
11391         else
11392           {
11393             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
11394             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
11395             submode = DImode;
11396           }
11397
11398         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
11399            avoid two branches.  This costs one extra insn, so disable when
11400            optimizing for size.  */
11401
11402         if ((code == EQ || code == NE)
11403             && (!optimize_size
11404                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
11405           {
11406             rtx xor0, xor1;
11407
11408             xor1 = hi[0];
11409             if (hi[1] != const0_rtx)
11410               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
11411                                    NULL_RTX, 0, OPTAB_WIDEN);
11412
11413             xor0 = lo[0];
11414             if (lo[1] != const0_rtx)
11415               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
11416                                    NULL_RTX, 0, OPTAB_WIDEN);
11417
11418             tmp = expand_binop (submode, ior_optab, xor1, xor0,
11419                                 NULL_RTX, 0, OPTAB_WIDEN);
11420
11421             ix86_compare_op0 = tmp;
11422             ix86_compare_op1 = const0_rtx;
11423             ix86_expand_branch (code, label);
11424             return;
11425           }
11426
11427         /* Otherwise, if we are doing less-than or greater-or-equal-than,
11428            op1 is a constant and the low word is zero, then we can just
11429            examine the high word.  */
11430
11431         if (CONST_INT_P (hi[1]) && lo[1] == const0_rtx)
11432           switch (code)
11433             {
11434             case LT: case LTU: case GE: case GEU:
11435               ix86_compare_op0 = hi[0];
11436               ix86_compare_op1 = hi[1];
11437               ix86_expand_branch (code, label);
11438               return;
11439             default:
11440               break;
11441             }
11442
11443         /* Otherwise, we need two or three jumps.  */
11444
11445         label2 = gen_label_rtx ();
11446
11447         code1 = code;
11448         code2 = swap_condition (code);
11449         code3 = unsigned_condition (code);
11450
11451         switch (code)
11452           {
11453           case LT: case GT: case LTU: case GTU:
11454             break;
11455
11456           case LE:   code1 = LT;  code2 = GT;  break;
11457           case GE:   code1 = GT;  code2 = LT;  break;
11458           case LEU:  code1 = LTU; code2 = GTU; break;
11459           case GEU:  code1 = GTU; code2 = LTU; break;
11460
11461           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
11462           case NE:   code2 = UNKNOWN; break;
11463
11464           default:
11465             gcc_unreachable ();
11466           }
11467
11468         /*
11469          * a < b =>
11470          *    if (hi(a) < hi(b)) goto true;
11471          *    if (hi(a) > hi(b)) goto false;
11472          *    if (lo(a) < lo(b)) goto true;
11473          *  false:
11474          */
11475
11476         ix86_compare_op0 = hi[0];
11477         ix86_compare_op1 = hi[1];
11478
11479         if (code1 != UNKNOWN)
11480           ix86_expand_branch (code1, label);
11481         if (code2 != UNKNOWN)
11482           ix86_expand_branch (code2, label2);
11483
11484         ix86_compare_op0 = lo[0];
11485         ix86_compare_op1 = lo[1];
11486         ix86_expand_branch (code3, label);
11487
11488         if (code2 != UNKNOWN)
11489           emit_label (label2);
11490         return;
11491       }
11492
11493     default:
11494       gcc_unreachable ();
11495     }
11496 }
11497
11498 /* Split branch based on floating point condition.  */
11499 void
11500 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
11501                       rtx target1, rtx target2, rtx tmp, rtx pushed)
11502 {
11503   rtx second, bypass;
11504   rtx label = NULL_RTX;
11505   rtx condition;
11506   int bypass_probability = -1, second_probability = -1, probability = -1;
11507   rtx i;
11508
11509   if (target2 != pc_rtx)
11510     {
11511       rtx tmp = target2;
11512       code = reverse_condition_maybe_unordered (code);
11513       target2 = target1;
11514       target1 = tmp;
11515     }
11516
11517   condition = ix86_expand_fp_compare (code, op1, op2,
11518                                       tmp, &second, &bypass);
11519
11520   /* Remove pushed operand from stack.  */
11521   if (pushed)
11522     ix86_free_from_memory (GET_MODE (pushed));
11523
11524   if (split_branch_probability >= 0)
11525     {
11526       /* Distribute the probabilities across the jumps.
11527          Assume the BYPASS and SECOND to be always test
11528          for UNORDERED.  */
11529       probability = split_branch_probability;
11530
11531       /* Value of 1 is low enough to make no need for probability
11532          to be updated.  Later we may run some experiments and see
11533          if unordered values are more frequent in practice.  */
11534       if (bypass)
11535         bypass_probability = 1;
11536       if (second)
11537         second_probability = 1;
11538     }
11539   if (bypass != NULL_RTX)
11540     {
11541       label = gen_label_rtx ();
11542       i = emit_jump_insn (gen_rtx_SET
11543                           (VOIDmode, pc_rtx,
11544                            gen_rtx_IF_THEN_ELSE (VOIDmode,
11545                                                  bypass,
11546                                                  gen_rtx_LABEL_REF (VOIDmode,
11547                                                                     label),
11548                                                  pc_rtx)));
11549       if (bypass_probability >= 0)
11550         REG_NOTES (i)
11551           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11552                                GEN_INT (bypass_probability),
11553                                REG_NOTES (i));
11554     }
11555   i = emit_jump_insn (gen_rtx_SET
11556                       (VOIDmode, pc_rtx,
11557                        gen_rtx_IF_THEN_ELSE (VOIDmode,
11558                                              condition, target1, target2)));
11559   if (probability >= 0)
11560     REG_NOTES (i)
11561       = gen_rtx_EXPR_LIST (REG_BR_PROB,
11562                            GEN_INT (probability),
11563                            REG_NOTES (i));
11564   if (second != NULL_RTX)
11565     {
11566       i = emit_jump_insn (gen_rtx_SET
11567                           (VOIDmode, pc_rtx,
11568                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
11569                                                  target2)));
11570       if (second_probability >= 0)
11571         REG_NOTES (i)
11572           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11573                                GEN_INT (second_probability),
11574                                REG_NOTES (i));
11575     }
11576   if (label != NULL_RTX)
11577     emit_label (label);
11578 }
11579
11580 int
11581 ix86_expand_setcc (enum rtx_code code, rtx dest)
11582 {
11583   rtx ret, tmp, tmpreg, equiv;
11584   rtx second_test, bypass_test;
11585
11586   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
11587     return 0; /* FAIL */
11588
11589   gcc_assert (GET_MODE (dest) == QImode);
11590
11591   ret = ix86_expand_compare (code, &second_test, &bypass_test);
11592   PUT_MODE (ret, QImode);
11593
11594   tmp = dest;
11595   tmpreg = dest;
11596
11597   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
11598   if (bypass_test || second_test)
11599     {
11600       rtx test = second_test;
11601       int bypass = 0;
11602       rtx tmp2 = gen_reg_rtx (QImode);
11603       if (bypass_test)
11604         {
11605           gcc_assert (!second_test);
11606           test = bypass_test;
11607           bypass = 1;
11608           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
11609         }
11610       PUT_MODE (test, QImode);
11611       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
11612
11613       if (bypass)
11614         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
11615       else
11616         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
11617     }
11618
11619   /* Attach a REG_EQUAL note describing the comparison result.  */
11620   if (ix86_compare_op0 && ix86_compare_op1)
11621     {
11622       equiv = simplify_gen_relational (code, QImode,
11623                                        GET_MODE (ix86_compare_op0),
11624                                        ix86_compare_op0, ix86_compare_op1);
11625       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
11626     }
11627
11628   return 1; /* DONE */
11629 }
11630
11631 /* Expand comparison setting or clearing carry flag.  Return true when
11632    successful and set pop for the operation.  */
11633 static bool
11634 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
11635 {
11636   enum machine_mode mode =
11637     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
11638
11639   /* Do not handle DImode compares that go through special path.
11640      Also we can't deal with FP compares yet.  This is possible to add.  */
11641   if (mode == (TARGET_64BIT ? TImode : DImode))
11642     return false;
11643
11644   if (SCALAR_FLOAT_MODE_P (mode))
11645     {
11646       rtx second_test = NULL, bypass_test = NULL;
11647       rtx compare_op, compare_seq;
11648
11649       gcc_assert (!DECIMAL_FLOAT_MODE_P (mode));
11650
11651       /* Shortcut:  following common codes never translate
11652          into carry flag compares.  */
11653       if (code == EQ || code == NE || code == UNEQ || code == LTGT
11654           || code == ORDERED || code == UNORDERED)
11655         return false;
11656
11657       /* These comparisons require zero flag; swap operands so they won't.  */
11658       if ((code == GT || code == UNLE || code == LE || code == UNGT)
11659           && !TARGET_IEEE_FP)
11660         {
11661           rtx tmp = op0;
11662           op0 = op1;
11663           op1 = tmp;
11664           code = swap_condition (code);
11665         }
11666
11667       /* Try to expand the comparison and verify that we end up with carry flag
11668          based comparison.  This is fails to be true only when we decide to expand
11669          comparison using arithmetic that is not too common scenario.  */
11670       start_sequence ();
11671       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11672                                            &second_test, &bypass_test);
11673       compare_seq = get_insns ();
11674       end_sequence ();
11675
11676       if (second_test || bypass_test)
11677         return false;
11678       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11679           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11680         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
11681       else
11682         code = GET_CODE (compare_op);
11683       if (code != LTU && code != GEU)
11684         return false;
11685       emit_insn (compare_seq);
11686       *pop = compare_op;
11687       return true;
11688     }
11689   if (!INTEGRAL_MODE_P (mode))
11690     return false;
11691   switch (code)
11692     {
11693     case LTU:
11694     case GEU:
11695       break;
11696
11697     /* Convert a==0 into (unsigned)a<1.  */
11698     case EQ:
11699     case NE:
11700       if (op1 != const0_rtx)
11701         return false;
11702       op1 = const1_rtx;
11703       code = (code == EQ ? LTU : GEU);
11704       break;
11705
11706     /* Convert a>b into b<a or a>=b-1.  */
11707     case GTU:
11708     case LEU:
11709       if (CONST_INT_P (op1))
11710         {
11711           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
11712           /* Bail out on overflow.  We still can swap operands but that
11713              would force loading of the constant into register.  */
11714           if (op1 == const0_rtx
11715               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
11716             return false;
11717           code = (code == GTU ? GEU : LTU);
11718         }
11719       else
11720         {
11721           rtx tmp = op1;
11722           op1 = op0;
11723           op0 = tmp;
11724           code = (code == GTU ? LTU : GEU);
11725         }
11726       break;
11727
11728     /* Convert a>=0 into (unsigned)a<0x80000000.  */
11729     case LT:
11730     case GE:
11731       if (mode == DImode || op1 != const0_rtx)
11732         return false;
11733       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11734       code = (code == LT ? GEU : LTU);
11735       break;
11736     case LE:
11737     case GT:
11738       if (mode == DImode || op1 != constm1_rtx)
11739         return false;
11740       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11741       code = (code == LE ? GEU : LTU);
11742       break;
11743
11744     default:
11745       return false;
11746     }
11747   /* Swapping operands may cause constant to appear as first operand.  */
11748   if (!nonimmediate_operand (op0, VOIDmode))
11749     {
11750       if (no_new_pseudos)
11751         return false;
11752       op0 = force_reg (mode, op0);
11753     }
11754   ix86_compare_op0 = op0;
11755   ix86_compare_op1 = op1;
11756   *pop = ix86_expand_compare (code, NULL, NULL);
11757   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
11758   return true;
11759 }
11760
11761 int
11762 ix86_expand_int_movcc (rtx operands[])
11763 {
11764   enum rtx_code code = GET_CODE (operands[1]), compare_code;
11765   rtx compare_seq, compare_op;
11766   rtx second_test, bypass_test;
11767   enum machine_mode mode = GET_MODE (operands[0]);
11768   bool sign_bit_compare_p = false;;
11769
11770   start_sequence ();
11771   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11772   compare_seq = get_insns ();
11773   end_sequence ();
11774
11775   compare_code = GET_CODE (compare_op);
11776
11777   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
11778       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
11779     sign_bit_compare_p = true;
11780
11781   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
11782      HImode insns, we'd be swallowed in word prefix ops.  */
11783
11784   if ((mode != HImode || TARGET_FAST_PREFIX)
11785       && (mode != (TARGET_64BIT ? TImode : DImode))
11786       && CONST_INT_P (operands[2])
11787       && CONST_INT_P (operands[3]))
11788     {
11789       rtx out = operands[0];
11790       HOST_WIDE_INT ct = INTVAL (operands[2]);
11791       HOST_WIDE_INT cf = INTVAL (operands[3]);
11792       HOST_WIDE_INT diff;
11793
11794       diff = ct - cf;
11795       /*  Sign bit compares are better done using shifts than we do by using
11796           sbb.  */
11797       if (sign_bit_compare_p
11798           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11799                                              ix86_compare_op1, &compare_op))
11800         {
11801           /* Detect overlap between destination and compare sources.  */
11802           rtx tmp = out;
11803
11804           if (!sign_bit_compare_p)
11805             {
11806               bool fpcmp = false;
11807
11808               compare_code = GET_CODE (compare_op);
11809
11810               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11811                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11812                 {
11813                   fpcmp = true;
11814                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
11815                 }
11816
11817               /* To simplify rest of code, restrict to the GEU case.  */
11818               if (compare_code == LTU)
11819                 {
11820                   HOST_WIDE_INT tmp = ct;
11821                   ct = cf;
11822                   cf = tmp;
11823                   compare_code = reverse_condition (compare_code);
11824                   code = reverse_condition (code);
11825                 }
11826               else
11827                 {
11828                   if (fpcmp)
11829                     PUT_CODE (compare_op,
11830                               reverse_condition_maybe_unordered
11831                                 (GET_CODE (compare_op)));
11832                   else
11833                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11834                 }
11835               diff = ct - cf;
11836
11837               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
11838                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
11839                 tmp = gen_reg_rtx (mode);
11840
11841               if (mode == DImode)
11842                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
11843               else
11844                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
11845             }
11846           else
11847             {
11848               if (code == GT || code == GE)
11849                 code = reverse_condition (code);
11850               else
11851                 {
11852                   HOST_WIDE_INT tmp = ct;
11853                   ct = cf;
11854                   cf = tmp;
11855                   diff = ct - cf;
11856                 }
11857               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
11858                                      ix86_compare_op1, VOIDmode, 0, -1);
11859             }
11860
11861           if (diff == 1)
11862             {
11863               /*
11864                * cmpl op0,op1
11865                * sbbl dest,dest
11866                * [addl dest, ct]
11867                *
11868                * Size 5 - 8.
11869                */
11870               if (ct)
11871                 tmp = expand_simple_binop (mode, PLUS,
11872                                            tmp, GEN_INT (ct),
11873                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11874             }
11875           else if (cf == -1)
11876             {
11877               /*
11878                * cmpl op0,op1
11879                * sbbl dest,dest
11880                * orl $ct, dest
11881                *
11882                * Size 8.
11883                */
11884               tmp = expand_simple_binop (mode, IOR,
11885                                          tmp, GEN_INT (ct),
11886                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11887             }
11888           else if (diff == -1 && ct)
11889             {
11890               /*
11891                * cmpl op0,op1
11892                * sbbl dest,dest
11893                * notl dest
11894                * [addl dest, cf]
11895                *
11896                * Size 8 - 11.
11897                */
11898               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11899               if (cf)
11900                 tmp = expand_simple_binop (mode, PLUS,
11901                                            copy_rtx (tmp), GEN_INT (cf),
11902                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11903             }
11904           else
11905             {
11906               /*
11907                * cmpl op0,op1
11908                * sbbl dest,dest
11909                * [notl dest]
11910                * andl cf - ct, dest
11911                * [addl dest, ct]
11912                *
11913                * Size 8 - 11.
11914                */
11915
11916               if (cf == 0)
11917                 {
11918                   cf = ct;
11919                   ct = 0;
11920                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11921                 }
11922
11923               tmp = expand_simple_binop (mode, AND,
11924                                          copy_rtx (tmp),
11925                                          gen_int_mode (cf - ct, mode),
11926                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11927               if (ct)
11928                 tmp = expand_simple_binop (mode, PLUS,
11929                                            copy_rtx (tmp), GEN_INT (ct),
11930                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11931             }
11932
11933           if (!rtx_equal_p (tmp, out))
11934             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
11935
11936           return 1; /* DONE */
11937         }
11938
11939       if (diff < 0)
11940         {
11941           enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
11942
11943           HOST_WIDE_INT tmp;
11944           tmp = ct, ct = cf, cf = tmp;
11945           diff = -diff;
11946
11947           if (SCALAR_FLOAT_MODE_P (cmp_mode))
11948             {
11949               gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
11950
11951               /* We may be reversing unordered compare to normal compare, that
11952                  is not valid in general (we may convert non-trapping condition
11953                  to trapping one), however on i386 we currently emit all
11954                  comparisons unordered.  */
11955               compare_code = reverse_condition_maybe_unordered (compare_code);
11956               code = reverse_condition_maybe_unordered (code);
11957             }
11958           else
11959             {
11960               compare_code = reverse_condition (compare_code);
11961               code = reverse_condition (code);
11962             }
11963         }
11964
11965       compare_code = UNKNOWN;
11966       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
11967           && CONST_INT_P (ix86_compare_op1))
11968         {
11969           if (ix86_compare_op1 == const0_rtx
11970               && (code == LT || code == GE))
11971             compare_code = code;
11972           else if (ix86_compare_op1 == constm1_rtx)
11973             {
11974               if (code == LE)
11975                 compare_code = LT;
11976               else if (code == GT)
11977                 compare_code = GE;
11978             }
11979         }
11980
11981       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11982       if (compare_code != UNKNOWN
11983           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11984           && (cf == -1 || ct == -1))
11985         {
11986           /* If lea code below could be used, only optimize
11987              if it results in a 2 insn sequence.  */
11988
11989           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11990                  || diff == 3 || diff == 5 || diff == 9)
11991               || (compare_code == LT && ct == -1)
11992               || (compare_code == GE && cf == -1))
11993             {
11994               /*
11995                * notl op1       (if necessary)
11996                * sarl $31, op1
11997                * orl cf, op1
11998                */
11999               if (ct != -1)
12000                 {
12001                   cf = ct;
12002                   ct = -1;
12003                   code = reverse_condition (code);
12004                 }
12005
12006               out = emit_store_flag (out, code, ix86_compare_op0,
12007                                      ix86_compare_op1, VOIDmode, 0, -1);
12008
12009               out = expand_simple_binop (mode, IOR,
12010                                          out, GEN_INT (cf),
12011                                          out, 1, OPTAB_DIRECT);
12012               if (out != operands[0])
12013                 emit_move_insn (operands[0], out);
12014
12015               return 1; /* DONE */
12016             }
12017         }
12018
12019
12020       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
12021            || diff == 3 || diff == 5 || diff == 9)
12022           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
12023           && (mode != DImode
12024               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
12025         {
12026           /*
12027            * xorl dest,dest
12028            * cmpl op1,op2
12029            * setcc dest
12030            * lea cf(dest*(ct-cf)),dest
12031            *
12032            * Size 14.
12033            *
12034            * This also catches the degenerate setcc-only case.
12035            */
12036
12037           rtx tmp;
12038           int nops;
12039
12040           out = emit_store_flag (out, code, ix86_compare_op0,
12041                                  ix86_compare_op1, VOIDmode, 0, 1);
12042
12043           nops = 0;
12044           /* On x86_64 the lea instruction operates on Pmode, so we need
12045              to get arithmetics done in proper mode to match.  */
12046           if (diff == 1)
12047             tmp = copy_rtx (out);
12048           else
12049             {
12050               rtx out1;
12051               out1 = copy_rtx (out);
12052               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
12053               nops++;
12054               if (diff & 1)
12055                 {
12056                   tmp = gen_rtx_PLUS (mode, tmp, out1);
12057                   nops++;
12058                 }
12059             }
12060           if (cf != 0)
12061             {
12062               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
12063               nops++;
12064             }
12065           if (!rtx_equal_p (tmp, out))
12066             {
12067               if (nops == 1)
12068                 out = force_operand (tmp, copy_rtx (out));
12069               else
12070                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
12071             }
12072           if (!rtx_equal_p (out, operands[0]))
12073             emit_move_insn (operands[0], copy_rtx (out));
12074
12075           return 1; /* DONE */
12076         }
12077
12078       /*
12079        * General case:                  Jumpful:
12080        *   xorl dest,dest               cmpl op1, op2
12081        *   cmpl op1, op2                movl ct, dest
12082        *   setcc dest                   jcc 1f
12083        *   decl dest                    movl cf, dest
12084        *   andl (cf-ct),dest            1:
12085        *   addl ct,dest
12086        *
12087        * Size 20.                       Size 14.
12088        *
12089        * This is reasonably steep, but branch mispredict costs are
12090        * high on modern cpus, so consider failing only if optimizing
12091        * for space.
12092        */
12093
12094       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12095           && BRANCH_COST >= 2)
12096         {
12097           if (cf == 0)
12098             {
12099               enum machine_mode cmp_mode = GET_MODE (ix86_compare_op0);
12100
12101               cf = ct;
12102               ct = 0;
12103
12104               if (SCALAR_FLOAT_MODE_P (cmp_mode))
12105                 {
12106                   gcc_assert (!DECIMAL_FLOAT_MODE_P (cmp_mode));
12107
12108                   /* We may be reversing unordered compare to normal compare,
12109                      that is not valid in general (we may convert non-trapping
12110                      condition to trapping one), however on i386 we currently
12111                      emit all comparisons unordered.  */
12112                   code = reverse_condition_maybe_unordered (code);
12113                 }
12114               else
12115                 {
12116                   code = reverse_condition (code);
12117                   if (compare_code != UNKNOWN)
12118                     compare_code = reverse_condition (compare_code);
12119                 }
12120             }
12121
12122           if (compare_code != UNKNOWN)
12123             {
12124               /* notl op1       (if needed)
12125                  sarl $31, op1
12126                  andl (cf-ct), op1
12127                  addl ct, op1
12128
12129                  For x < 0 (resp. x <= -1) there will be no notl,
12130                  so if possible swap the constants to get rid of the
12131                  complement.
12132                  True/false will be -1/0 while code below (store flag
12133                  followed by decrement) is 0/-1, so the constants need
12134                  to be exchanged once more.  */
12135
12136               if (compare_code == GE || !cf)
12137                 {
12138                   code = reverse_condition (code);
12139                   compare_code = LT;
12140                 }
12141               else
12142                 {
12143                   HOST_WIDE_INT tmp = cf;
12144                   cf = ct;
12145                   ct = tmp;
12146                 }
12147
12148               out = emit_store_flag (out, code, ix86_compare_op0,
12149                                      ix86_compare_op1, VOIDmode, 0, -1);
12150             }
12151           else
12152             {
12153               out = emit_store_flag (out, code, ix86_compare_op0,
12154                                      ix86_compare_op1, VOIDmode, 0, 1);
12155
12156               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
12157                                          copy_rtx (out), 1, OPTAB_DIRECT);
12158             }
12159
12160           out = expand_simple_binop (mode, AND, copy_rtx (out),
12161                                      gen_int_mode (cf - ct, mode),
12162                                      copy_rtx (out), 1, OPTAB_DIRECT);
12163           if (ct)
12164             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
12165                                        copy_rtx (out), 1, OPTAB_DIRECT);
12166           if (!rtx_equal_p (out, operands[0]))
12167             emit_move_insn (operands[0], copy_rtx (out));
12168
12169           return 1; /* DONE */
12170         }
12171     }
12172
12173   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12174     {
12175       /* Try a few things more with specific constants and a variable.  */
12176
12177       optab op;
12178       rtx var, orig_out, out, tmp;
12179
12180       if (BRANCH_COST <= 2)
12181         return 0; /* FAIL */
12182
12183       /* If one of the two operands is an interesting constant, load a
12184          constant with the above and mask it in with a logical operation.  */
12185
12186       if (CONST_INT_P (operands[2]))
12187         {
12188           var = operands[3];
12189           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
12190             operands[3] = constm1_rtx, op = and_optab;
12191           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
12192             operands[3] = const0_rtx, op = ior_optab;
12193           else
12194             return 0; /* FAIL */
12195         }
12196       else if (CONST_INT_P (operands[3]))
12197         {
12198           var = operands[2];
12199           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
12200             operands[2] = constm1_rtx, op = and_optab;
12201           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
12202             operands[2] = const0_rtx, op = ior_optab;
12203           else
12204             return 0; /* FAIL */
12205         }
12206       else
12207         return 0; /* FAIL */
12208
12209       orig_out = operands[0];
12210       tmp = gen_reg_rtx (mode);
12211       operands[0] = tmp;
12212
12213       /* Recurse to get the constant loaded.  */
12214       if (ix86_expand_int_movcc (operands) == 0)
12215         return 0; /* FAIL */
12216
12217       /* Mask in the interesting variable.  */
12218       out = expand_binop (mode, op, var, tmp, orig_out, 0,
12219                           OPTAB_WIDEN);
12220       if (!rtx_equal_p (out, orig_out))
12221         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
12222
12223       return 1; /* DONE */
12224     }
12225
12226   /*
12227    * For comparison with above,
12228    *
12229    * movl cf,dest
12230    * movl ct,tmp
12231    * cmpl op1,op2
12232    * cmovcc tmp,dest
12233    *
12234    * Size 15.
12235    */
12236
12237   if (! nonimmediate_operand (operands[2], mode))
12238     operands[2] = force_reg (mode, operands[2]);
12239   if (! nonimmediate_operand (operands[3], mode))
12240     operands[3] = force_reg (mode, operands[3]);
12241
12242   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12243     {
12244       rtx tmp = gen_reg_rtx (mode);
12245       emit_move_insn (tmp, operands[3]);
12246       operands[3] = tmp;
12247     }
12248   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12249     {
12250       rtx tmp = gen_reg_rtx (mode);
12251       emit_move_insn (tmp, operands[2]);
12252       operands[2] = tmp;
12253     }
12254
12255   if (! register_operand (operands[2], VOIDmode)
12256       && (mode == QImode
12257           || ! register_operand (operands[3], VOIDmode)))
12258     operands[2] = force_reg (mode, operands[2]);
12259
12260   if (mode == QImode
12261       && ! register_operand (operands[3], VOIDmode))
12262     operands[3] = force_reg (mode, operands[3]);
12263
12264   emit_insn (compare_seq);
12265   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12266                           gen_rtx_IF_THEN_ELSE (mode,
12267                                                 compare_op, operands[2],
12268                                                 operands[3])));
12269   if (bypass_test)
12270     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
12271                             gen_rtx_IF_THEN_ELSE (mode,
12272                                   bypass_test,
12273                                   copy_rtx (operands[3]),
12274                                   copy_rtx (operands[0]))));
12275   if (second_test)
12276     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
12277                             gen_rtx_IF_THEN_ELSE (mode,
12278                                   second_test,
12279                                   copy_rtx (operands[2]),
12280                                   copy_rtx (operands[0]))));
12281
12282   return 1; /* DONE */
12283 }
12284
12285 /* Swap, force into registers, or otherwise massage the two operands
12286    to an sse comparison with a mask result.  Thus we differ a bit from
12287    ix86_prepare_fp_compare_args which expects to produce a flags result.
12288
12289    The DEST operand exists to help determine whether to commute commutative
12290    operators.  The POP0/POP1 operands are updated in place.  The new
12291    comparison code is returned, or UNKNOWN if not implementable.  */
12292
12293 static enum rtx_code
12294 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
12295                                   rtx *pop0, rtx *pop1)
12296 {
12297   rtx tmp;
12298
12299   switch (code)
12300     {
12301     case LTGT:
12302     case UNEQ:
12303       /* We have no LTGT as an operator.  We could implement it with
12304          NE & ORDERED, but this requires an extra temporary.  It's
12305          not clear that it's worth it.  */
12306       return UNKNOWN;
12307
12308     case LT:
12309     case LE:
12310     case UNGT:
12311     case UNGE:
12312       /* These are supported directly.  */
12313       break;
12314
12315     case EQ:
12316     case NE:
12317     case UNORDERED:
12318     case ORDERED:
12319       /* For commutative operators, try to canonicalize the destination
12320          operand to be first in the comparison - this helps reload to
12321          avoid extra moves.  */
12322       if (!dest || !rtx_equal_p (dest, *pop1))
12323         break;
12324       /* FALLTHRU */
12325
12326     case GE:
12327     case GT:
12328     case UNLE:
12329     case UNLT:
12330       /* These are not supported directly.  Swap the comparison operands
12331          to transform into something that is supported.  */
12332       tmp = *pop0;
12333       *pop0 = *pop1;
12334       *pop1 = tmp;
12335       code = swap_condition (code);
12336       break;
12337
12338     default:
12339       gcc_unreachable ();
12340     }
12341
12342   return code;
12343 }
12344
12345 /* Detect conditional moves that exactly match min/max operational
12346    semantics.  Note that this is IEEE safe, as long as we don't
12347    interchange the operands.
12348
12349    Returns FALSE if this conditional move doesn't match a MIN/MAX,
12350    and TRUE if the operation is successful and instructions are emitted.  */
12351
12352 static bool
12353 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
12354                            rtx cmp_op1, rtx if_true, rtx if_false)
12355 {
12356   enum machine_mode mode;
12357   bool is_min;
12358   rtx tmp;
12359
12360   if (code == LT)
12361     ;
12362   else if (code == UNGE)
12363     {
12364       tmp = if_true;
12365       if_true = if_false;
12366       if_false = tmp;
12367     }
12368   else
12369     return false;
12370
12371   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
12372     is_min = true;
12373   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
12374     is_min = false;
12375   else
12376     return false;
12377
12378   mode = GET_MODE (dest);
12379
12380   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
12381      but MODE may be a vector mode and thus not appropriate.  */
12382   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
12383     {
12384       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
12385       rtvec v;
12386
12387       if_true = force_reg (mode, if_true);
12388       v = gen_rtvec (2, if_true, if_false);
12389       tmp = gen_rtx_UNSPEC (mode, v, u);
12390     }
12391   else
12392     {
12393       code = is_min ? SMIN : SMAX;
12394       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
12395     }
12396
12397   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
12398   return true;
12399 }
12400
12401 /* Expand an sse vector comparison.  Return the register with the result.  */
12402
12403 static rtx
12404 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
12405                      rtx op_true, rtx op_false)
12406 {
12407   enum machine_mode mode = GET_MODE (dest);
12408   rtx x;
12409
12410   cmp_op0 = force_reg (mode, cmp_op0);
12411   if (!nonimmediate_operand (cmp_op1, mode))
12412     cmp_op1 = force_reg (mode, cmp_op1);
12413
12414   if (optimize
12415       || reg_overlap_mentioned_p (dest, op_true)
12416       || reg_overlap_mentioned_p (dest, op_false))
12417     dest = gen_reg_rtx (mode);
12418
12419   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
12420   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12421
12422   return dest;
12423 }
12424
12425 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
12426    operations.  This is used for both scalar and vector conditional moves.  */
12427
12428 static void
12429 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
12430 {
12431   enum machine_mode mode = GET_MODE (dest);
12432   rtx t2, t3, x;
12433
12434   if (op_false == CONST0_RTX (mode))
12435     {
12436       op_true = force_reg (mode, op_true);
12437       x = gen_rtx_AND (mode, cmp, op_true);
12438       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12439     }
12440   else if (op_true == CONST0_RTX (mode))
12441     {
12442       op_false = force_reg (mode, op_false);
12443       x = gen_rtx_NOT (mode, cmp);
12444       x = gen_rtx_AND (mode, x, op_false);
12445       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12446     }
12447   else
12448     {
12449       op_true = force_reg (mode, op_true);
12450       op_false = force_reg (mode, op_false);
12451
12452       t2 = gen_reg_rtx (mode);
12453       if (optimize)
12454         t3 = gen_reg_rtx (mode);
12455       else
12456         t3 = dest;
12457
12458       x = gen_rtx_AND (mode, op_true, cmp);
12459       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
12460
12461       x = gen_rtx_NOT (mode, cmp);
12462       x = gen_rtx_AND (mode, x, op_false);
12463       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
12464
12465       x = gen_rtx_IOR (mode, t3, t2);
12466       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12467     }
12468 }
12469
12470 /* Expand a floating-point conditional move.  Return true if successful.  */
12471
12472 int
12473 ix86_expand_fp_movcc (rtx operands[])
12474 {
12475   enum machine_mode mode = GET_MODE (operands[0]);
12476   enum rtx_code code = GET_CODE (operands[1]);
12477   rtx tmp, compare_op, second_test, bypass_test;
12478
12479   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
12480     {
12481       enum machine_mode cmode;
12482
12483       /* Since we've no cmove for sse registers, don't force bad register
12484          allocation just to gain access to it.  Deny movcc when the
12485          comparison mode doesn't match the move mode.  */
12486       cmode = GET_MODE (ix86_compare_op0);
12487       if (cmode == VOIDmode)
12488         cmode = GET_MODE (ix86_compare_op1);
12489       if (cmode != mode)
12490         return 0;
12491
12492       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12493                                                &ix86_compare_op0,
12494                                                &ix86_compare_op1);
12495       if (code == UNKNOWN)
12496         return 0;
12497
12498       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
12499                                      ix86_compare_op1, operands[2],
12500                                      operands[3]))
12501         return 1;
12502
12503       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
12504                                  ix86_compare_op1, operands[2], operands[3]);
12505       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
12506       return 1;
12507     }
12508
12509   /* The floating point conditional move instructions don't directly
12510      support conditions resulting from a signed integer comparison.  */
12511
12512   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
12513
12514   /* The floating point conditional move instructions don't directly
12515      support signed integer comparisons.  */
12516
12517   if (!fcmov_comparison_operator (compare_op, VOIDmode))
12518     {
12519       gcc_assert (!second_test && !bypass_test);
12520       tmp = gen_reg_rtx (QImode);
12521       ix86_expand_setcc (code, tmp);
12522       code = NE;
12523       ix86_compare_op0 = tmp;
12524       ix86_compare_op1 = const0_rtx;
12525       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
12526     }
12527   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12528     {
12529       tmp = gen_reg_rtx (mode);
12530       emit_move_insn (tmp, operands[3]);
12531       operands[3] = tmp;
12532     }
12533   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12534     {
12535       tmp = gen_reg_rtx (mode);
12536       emit_move_insn (tmp, operands[2]);
12537       operands[2] = tmp;
12538     }
12539
12540   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12541                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
12542                                                 operands[2], operands[3])));
12543   if (bypass_test)
12544     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12545                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
12546                                                   operands[3], operands[0])));
12547   if (second_test)
12548     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12549                             gen_rtx_IF_THEN_ELSE (mode, second_test,
12550                                                   operands[2], operands[0])));
12551
12552   return 1;
12553 }
12554
12555 /* Expand a floating-point vector conditional move; a vcond operation
12556    rather than a movcc operation.  */
12557
12558 bool
12559 ix86_expand_fp_vcond (rtx operands[])
12560 {
12561   enum rtx_code code = GET_CODE (operands[3]);
12562   rtx cmp;
12563
12564   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12565                                            &operands[4], &operands[5]);
12566   if (code == UNKNOWN)
12567     return false;
12568
12569   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
12570                                  operands[5], operands[1], operands[2]))
12571     return true;
12572
12573   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
12574                              operands[1], operands[2]);
12575   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
12576   return true;
12577 }
12578
12579 /* Expand a signed integral vector conditional move.  */
12580
12581 bool
12582 ix86_expand_int_vcond (rtx operands[])
12583 {
12584   enum machine_mode mode = GET_MODE (operands[0]);
12585   enum rtx_code code = GET_CODE (operands[3]);
12586   bool negate = false;
12587   rtx x, cop0, cop1;
12588
12589   cop0 = operands[4];
12590   cop1 = operands[5];
12591
12592   /* Canonicalize the comparison to EQ, GT, GTU.  */
12593   switch (code)
12594     {
12595     case EQ:
12596     case GT:
12597     case GTU:
12598       break;
12599
12600     case NE:
12601     case LE:
12602     case LEU:
12603       code = reverse_condition (code);
12604       negate = true;
12605       break;
12606
12607     case GE:
12608     case GEU:
12609       code = reverse_condition (code);
12610       negate = true;
12611       /* FALLTHRU */
12612
12613     case LT:
12614     case LTU:
12615       code = swap_condition (code);
12616       x = cop0, cop0 = cop1, cop1 = x;
12617       break;
12618
12619     default:
12620       gcc_unreachable ();
12621     }
12622
12623   /* Unsigned parallel compare is not supported by the hardware.  Play some
12624      tricks to turn this into a signed comparison against 0.  */
12625   if (code == GTU)
12626     {
12627       cop0 = force_reg (mode, cop0);
12628
12629       switch (mode)
12630         {
12631         case V4SImode:
12632           {
12633             rtx t1, t2, mask;
12634
12635             /* Perform a parallel modulo subtraction.  */
12636             t1 = gen_reg_rtx (mode);
12637             emit_insn (gen_subv4si3 (t1, cop0, cop1));
12638
12639             /* Extract the original sign bit of op0.  */
12640             mask = GEN_INT (-0x80000000);
12641             mask = gen_rtx_CONST_VECTOR (mode,
12642                         gen_rtvec (4, mask, mask, mask, mask));
12643             mask = force_reg (mode, mask);
12644             t2 = gen_reg_rtx (mode);
12645             emit_insn (gen_andv4si3 (t2, cop0, mask));
12646
12647             /* XOR it back into the result of the subtraction.  This results
12648                in the sign bit set iff we saw unsigned underflow.  */
12649             x = gen_reg_rtx (mode);
12650             emit_insn (gen_xorv4si3 (x, t1, t2));
12651
12652             code = GT;
12653           }
12654           break;
12655
12656         case V16QImode:
12657         case V8HImode:
12658           /* Perform a parallel unsigned saturating subtraction.  */
12659           x = gen_reg_rtx (mode);
12660           emit_insn (gen_rtx_SET (VOIDmode, x,
12661                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
12662
12663           code = EQ;
12664           negate = !negate;
12665           break;
12666
12667         default:
12668           gcc_unreachable ();
12669         }
12670
12671       cop0 = x;
12672       cop1 = CONST0_RTX (mode);
12673     }
12674
12675   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
12676                            operands[1+negate], operands[2-negate]);
12677
12678   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
12679                          operands[2-negate]);
12680   return true;
12681 }
12682
12683 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
12684    true if we should do zero extension, else sign extension.  HIGH_P is
12685    true if we want the N/2 high elements, else the low elements.  */
12686
12687 void
12688 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
12689 {
12690   enum machine_mode imode = GET_MODE (operands[1]);
12691   rtx (*unpack)(rtx, rtx, rtx);
12692   rtx se, dest;
12693
12694   switch (imode)
12695     {
12696     case V16QImode:
12697       if (high_p)
12698         unpack = gen_vec_interleave_highv16qi;
12699       else
12700         unpack = gen_vec_interleave_lowv16qi;
12701       break;
12702     case V8HImode:
12703       if (high_p)
12704         unpack = gen_vec_interleave_highv8hi;
12705       else
12706         unpack = gen_vec_interleave_lowv8hi;
12707       break;
12708     case V4SImode:
12709       if (high_p)
12710         unpack = gen_vec_interleave_highv4si;
12711       else
12712         unpack = gen_vec_interleave_lowv4si;
12713       break;
12714     default:
12715       gcc_unreachable ();
12716     }
12717
12718   dest = gen_lowpart (imode, operands[0]);
12719
12720   if (unsigned_p)
12721     se = force_reg (imode, CONST0_RTX (imode));
12722   else
12723     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
12724                               operands[1], pc_rtx, pc_rtx);
12725
12726   emit_insn (unpack (dest, operands[1], se));
12727 }
12728
12729 /* Expand conditional increment or decrement using adb/sbb instructions.
12730    The default case using setcc followed by the conditional move can be
12731    done by generic code.  */
12732 int
12733 ix86_expand_int_addcc (rtx operands[])
12734 {
12735   enum rtx_code code = GET_CODE (operands[1]);
12736   rtx compare_op;
12737   rtx val = const0_rtx;
12738   bool fpcmp = false;
12739   enum machine_mode mode = GET_MODE (operands[0]);
12740
12741   if (operands[3] != const1_rtx
12742       && operands[3] != constm1_rtx)
12743     return 0;
12744   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
12745                                        ix86_compare_op1, &compare_op))
12746      return 0;
12747   code = GET_CODE (compare_op);
12748
12749   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12750       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12751     {
12752       fpcmp = true;
12753       code = ix86_fp_compare_code_to_integer (code);
12754     }
12755
12756   if (code != LTU)
12757     {
12758       val = constm1_rtx;
12759       if (fpcmp)
12760         PUT_CODE (compare_op,
12761                   reverse_condition_maybe_unordered
12762                     (GET_CODE (compare_op)));
12763       else
12764         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
12765     }
12766   PUT_MODE (compare_op, mode);
12767
12768   /* Construct either adc or sbb insn.  */
12769   if ((code == LTU) == (operands[3] == constm1_rtx))
12770     {
12771       switch (GET_MODE (operands[0]))
12772         {
12773           case QImode:
12774             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
12775             break;
12776           case HImode:
12777             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
12778             break;
12779           case SImode:
12780             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
12781             break;
12782           case DImode:
12783             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12784             break;
12785           default:
12786             gcc_unreachable ();
12787         }
12788     }
12789   else
12790     {
12791       switch (GET_MODE (operands[0]))
12792         {
12793           case QImode:
12794             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
12795             break;
12796           case HImode:
12797             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
12798             break;
12799           case SImode:
12800             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
12801             break;
12802           case DImode:
12803             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12804             break;
12805           default:
12806             gcc_unreachable ();
12807         }
12808     }
12809   return 1; /* DONE */
12810 }
12811
12812
12813 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
12814    works for floating pointer parameters and nonoffsetable memories.
12815    For pushes, it returns just stack offsets; the values will be saved
12816    in the right order.  Maximally three parts are generated.  */
12817
12818 static int
12819 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
12820 {
12821   int size;
12822
12823   if (!TARGET_64BIT)
12824     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
12825   else
12826     size = (GET_MODE_SIZE (mode) + 4) / 8;
12827
12828   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
12829   gcc_assert (size >= 2 && size <= 3);
12830
12831   /* Optimize constant pool reference to immediates.  This is used by fp
12832      moves, that force all constants to memory to allow combining.  */
12833   if (MEM_P (operand) && MEM_READONLY_P (operand))
12834     {
12835       rtx tmp = maybe_get_pool_constant (operand);
12836       if (tmp)
12837         operand = tmp;
12838     }
12839
12840   if (MEM_P (operand) && !offsettable_memref_p (operand))
12841     {
12842       /* The only non-offsetable memories we handle are pushes.  */
12843       int ok = push_operand (operand, VOIDmode);
12844
12845       gcc_assert (ok);
12846
12847       operand = copy_rtx (operand);
12848       PUT_MODE (operand, Pmode);
12849       parts[0] = parts[1] = parts[2] = operand;
12850       return size;
12851     }
12852
12853   if (GET_CODE (operand) == CONST_VECTOR)
12854     {
12855       enum machine_mode imode = int_mode_for_mode (mode);
12856       /* Caution: if we looked through a constant pool memory above,
12857          the operand may actually have a different mode now.  That's
12858          ok, since we want to pun this all the way back to an integer.  */
12859       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
12860       gcc_assert (operand != NULL);
12861       mode = imode;
12862     }
12863
12864   if (!TARGET_64BIT)
12865     {
12866       if (mode == DImode)
12867         split_di (&operand, 1, &parts[0], &parts[1]);
12868       else
12869         {
12870           if (REG_P (operand))
12871             {
12872               gcc_assert (reload_completed);
12873               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
12874               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
12875               if (size == 3)
12876                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
12877             }
12878           else if (offsettable_memref_p (operand))
12879             {
12880               operand = adjust_address (operand, SImode, 0);
12881               parts[0] = operand;
12882               parts[1] = adjust_address (operand, SImode, 4);
12883               if (size == 3)
12884                 parts[2] = adjust_address (operand, SImode, 8);
12885             }
12886           else if (GET_CODE (operand) == CONST_DOUBLE)
12887             {
12888               REAL_VALUE_TYPE r;
12889               long l[4];
12890
12891               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12892               switch (mode)
12893                 {
12894                 case XFmode:
12895                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
12896                   parts[2] = gen_int_mode (l[2], SImode);
12897                   break;
12898                 case DFmode:
12899                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
12900                   break;
12901                 default:
12902                   gcc_unreachable ();
12903                 }
12904               parts[1] = gen_int_mode (l[1], SImode);
12905               parts[0] = gen_int_mode (l[0], SImode);
12906             }
12907           else
12908             gcc_unreachable ();
12909         }
12910     }
12911   else
12912     {
12913       if (mode == TImode)
12914         split_ti (&operand, 1, &parts[0], &parts[1]);
12915       if (mode == XFmode || mode == TFmode)
12916         {
12917           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
12918           if (REG_P (operand))
12919             {
12920               gcc_assert (reload_completed);
12921               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
12922               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
12923             }
12924           else if (offsettable_memref_p (operand))
12925             {
12926               operand = adjust_address (operand, DImode, 0);
12927               parts[0] = operand;
12928               parts[1] = adjust_address (operand, upper_mode, 8);
12929             }
12930           else if (GET_CODE (operand) == CONST_DOUBLE)
12931             {
12932               REAL_VALUE_TYPE r;
12933               long l[4];
12934
12935               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12936               real_to_target (l, &r, mode);
12937
12938               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
12939               if (HOST_BITS_PER_WIDE_INT >= 64)
12940                 parts[0]
12941                   = gen_int_mode
12942                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
12943                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
12944                        DImode);
12945               else
12946                 parts[0] = immed_double_const (l[0], l[1], DImode);
12947
12948               if (upper_mode == SImode)
12949                 parts[1] = gen_int_mode (l[2], SImode);
12950               else if (HOST_BITS_PER_WIDE_INT >= 64)
12951                 parts[1]
12952                   = gen_int_mode
12953                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
12954                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
12955                        DImode);
12956               else
12957                 parts[1] = immed_double_const (l[2], l[3], DImode);
12958             }
12959           else
12960             gcc_unreachable ();
12961         }
12962     }
12963
12964   return size;
12965 }
12966
12967 /* Emit insns to perform a move or push of DI, DF, and XF values.
12968    Return false when normal moves are needed; true when all required
12969    insns have been emitted.  Operands 2-4 contain the input values
12970    int the correct order; operands 5-7 contain the output values.  */
12971
12972 void
12973 ix86_split_long_move (rtx operands[])
12974 {
12975   rtx part[2][3];
12976   int nparts;
12977   int push = 0;
12978   int collisions = 0;
12979   enum machine_mode mode = GET_MODE (operands[0]);
12980
12981   /* The DFmode expanders may ask us to move double.
12982      For 64bit target this is single move.  By hiding the fact
12983      here we simplify i386.md splitters.  */
12984   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
12985     {
12986       /* Optimize constant pool reference to immediates.  This is used by
12987          fp moves, that force all constants to memory to allow combining.  */
12988
12989       if (MEM_P (operands[1])
12990           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
12991           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
12992         operands[1] = get_pool_constant (XEXP (operands[1], 0));
12993       if (push_operand (operands[0], VOIDmode))
12994         {
12995           operands[0] = copy_rtx (operands[0]);
12996           PUT_MODE (operands[0], Pmode);
12997         }
12998       else
12999         operands[0] = gen_lowpart (DImode, operands[0]);
13000       operands[1] = gen_lowpart (DImode, operands[1]);
13001       emit_move_insn (operands[0], operands[1]);
13002       return;
13003     }
13004
13005   /* The only non-offsettable memory we handle is push.  */
13006   if (push_operand (operands[0], VOIDmode))
13007     push = 1;
13008   else
13009     gcc_assert (!MEM_P (operands[0])
13010                 || offsettable_memref_p (operands[0]));
13011
13012   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
13013   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
13014
13015   /* When emitting push, take care for source operands on the stack.  */
13016   if (push && MEM_P (operands[1])
13017       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
13018     {
13019       if (nparts == 3)
13020         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
13021                                      XEXP (part[1][2], 0));
13022       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
13023                                    XEXP (part[1][1], 0));
13024     }
13025
13026   /* We need to do copy in the right order in case an address register
13027      of the source overlaps the destination.  */
13028   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
13029     {
13030       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
13031         collisions++;
13032       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
13033         collisions++;
13034       if (nparts == 3
13035           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
13036         collisions++;
13037
13038       /* Collision in the middle part can be handled by reordering.  */
13039       if (collisions == 1 && nparts == 3
13040           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
13041         {
13042           rtx tmp;
13043           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
13044           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
13045         }
13046
13047       /* If there are more collisions, we can't handle it by reordering.
13048          Do an lea to the last part and use only one colliding move.  */
13049       else if (collisions > 1)
13050         {
13051           rtx base;
13052
13053           collisions = 1;
13054
13055           base = part[0][nparts - 1];
13056
13057           /* Handle the case when the last part isn't valid for lea.
13058              Happens in 64-bit mode storing the 12-byte XFmode.  */
13059           if (GET_MODE (base) != Pmode)
13060             base = gen_rtx_REG (Pmode, REGNO (base));
13061
13062           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
13063           part[1][0] = replace_equiv_address (part[1][0], base);
13064           part[1][1] = replace_equiv_address (part[1][1],
13065                                       plus_constant (base, UNITS_PER_WORD));
13066           if (nparts == 3)
13067             part[1][2] = replace_equiv_address (part[1][2],
13068                                       plus_constant (base, 8));
13069         }
13070     }
13071
13072   if (push)
13073     {
13074       if (!TARGET_64BIT)
13075         {
13076           if (nparts == 3)
13077             {
13078               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
13079                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
13080               emit_move_insn (part[0][2], part[1][2]);
13081             }
13082         }
13083       else
13084         {
13085           /* In 64bit mode we don't have 32bit push available.  In case this is
13086              register, it is OK - we will just use larger counterpart.  We also
13087              retype memory - these comes from attempt to avoid REX prefix on
13088              moving of second half of TFmode value.  */
13089           if (GET_MODE (part[1][1]) == SImode)
13090             {
13091               switch (GET_CODE (part[1][1]))
13092                 {
13093                 case MEM:
13094                   part[1][1] = adjust_address (part[1][1], DImode, 0);
13095                   break;
13096
13097                 case REG:
13098                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
13099                   break;
13100
13101                 default:
13102                   gcc_unreachable ();
13103                 }
13104
13105               if (GET_MODE (part[1][0]) == SImode)
13106                 part[1][0] = part[1][1];
13107             }
13108         }
13109       emit_move_insn (part[0][1], part[1][1]);
13110       emit_move_insn (part[0][0], part[1][0]);
13111       return;
13112     }
13113
13114   /* Choose correct order to not overwrite the source before it is copied.  */
13115   if ((REG_P (part[0][0])
13116        && REG_P (part[1][1])
13117        && (REGNO (part[0][0]) == REGNO (part[1][1])
13118            || (nparts == 3
13119                && REGNO (part[0][0]) == REGNO (part[1][2]))))
13120       || (collisions > 0
13121           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
13122     {
13123       if (nparts == 3)
13124         {
13125           operands[2] = part[0][2];
13126           operands[3] = part[0][1];
13127           operands[4] = part[0][0];
13128           operands[5] = part[1][2];
13129           operands[6] = part[1][1];
13130           operands[7] = part[1][0];
13131         }
13132       else
13133         {
13134           operands[2] = part[0][1];
13135           operands[3] = part[0][0];
13136           operands[5] = part[1][1];
13137           operands[6] = part[1][0];
13138         }
13139     }
13140   else
13141     {
13142       if (nparts == 3)
13143         {
13144           operands[2] = part[0][0];
13145           operands[3] = part[0][1];
13146           operands[4] = part[0][2];
13147           operands[5] = part[1][0];
13148           operands[6] = part[1][1];
13149           operands[7] = part[1][2];
13150         }
13151       else
13152         {
13153           operands[2] = part[0][0];
13154           operands[3] = part[0][1];
13155           operands[5] = part[1][0];
13156           operands[6] = part[1][1];
13157         }
13158     }
13159
13160   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
13161   if (optimize_size)
13162     {
13163       if (CONST_INT_P (operands[5])
13164           && operands[5] != const0_rtx
13165           && REG_P (operands[2]))
13166         {
13167           if (CONST_INT_P (operands[6])
13168               && INTVAL (operands[6]) == INTVAL (operands[5]))
13169             operands[6] = operands[2];
13170
13171           if (nparts == 3
13172               && CONST_INT_P (operands[7])
13173               && INTVAL (operands[7]) == INTVAL (operands[5]))
13174             operands[7] = operands[2];
13175         }
13176
13177       if (nparts == 3
13178           && CONST_INT_P (operands[6])
13179           && operands[6] != const0_rtx
13180           && REG_P (operands[3])
13181           && CONST_INT_P (operands[7])
13182           && INTVAL (operands[7]) == INTVAL (operands[6]))
13183         operands[7] = operands[3];
13184     }
13185
13186   emit_move_insn (operands[2], operands[5]);
13187   emit_move_insn (operands[3], operands[6]);
13188   if (nparts == 3)
13189     emit_move_insn (operands[4], operands[7]);
13190
13191   return;
13192 }
13193
13194 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
13195    left shift by a constant, either using a single shift or
13196    a sequence of add instructions.  */
13197
13198 static void
13199 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
13200 {
13201   if (count == 1)
13202     {
13203       emit_insn ((mode == DImode
13204                   ? gen_addsi3
13205                   : gen_adddi3) (operand, operand, operand));
13206     }
13207   else if (!optimize_size
13208            && count * ix86_cost->add <= ix86_cost->shift_const)
13209     {
13210       int i;
13211       for (i=0; i<count; i++)
13212         {
13213           emit_insn ((mode == DImode
13214                       ? gen_addsi3
13215                       : gen_adddi3) (operand, operand, operand));
13216         }
13217     }
13218   else
13219     emit_insn ((mode == DImode
13220                 ? gen_ashlsi3
13221                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
13222 }
13223
13224 void
13225 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
13226 {
13227   rtx low[2], high[2];
13228   int count;
13229   const int single_width = mode == DImode ? 32 : 64;
13230
13231   if (CONST_INT_P (operands[2]))
13232     {
13233       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13234       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13235
13236       if (count >= single_width)
13237         {
13238           emit_move_insn (high[0], low[1]);
13239           emit_move_insn (low[0], const0_rtx);
13240
13241           if (count > single_width)
13242             ix86_expand_ashl_const (high[0], count - single_width, mode);
13243         }
13244       else
13245         {
13246           if (!rtx_equal_p (operands[0], operands[1]))
13247             emit_move_insn (operands[0], operands[1]);
13248           emit_insn ((mode == DImode
13249                      ? gen_x86_shld_1
13250                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
13251           ix86_expand_ashl_const (low[0], count, mode);
13252         }
13253       return;
13254     }
13255
13256   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13257
13258   if (operands[1] == const1_rtx)
13259     {
13260       /* Assuming we've chosen a QImode capable registers, then 1 << N
13261          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
13262       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
13263         {
13264           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
13265
13266           ix86_expand_clear (low[0]);
13267           ix86_expand_clear (high[0]);
13268           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
13269
13270           d = gen_lowpart (QImode, low[0]);
13271           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
13272           s = gen_rtx_EQ (QImode, flags, const0_rtx);
13273           emit_insn (gen_rtx_SET (VOIDmode, d, s));
13274
13275           d = gen_lowpart (QImode, high[0]);
13276           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
13277           s = gen_rtx_NE (QImode, flags, const0_rtx);
13278           emit_insn (gen_rtx_SET (VOIDmode, d, s));
13279         }
13280
13281       /* Otherwise, we can get the same results by manually performing
13282          a bit extract operation on bit 5/6, and then performing the two
13283          shifts.  The two methods of getting 0/1 into low/high are exactly
13284          the same size.  Avoiding the shift in the bit extract case helps
13285          pentium4 a bit; no one else seems to care much either way.  */
13286       else
13287         {
13288           rtx x;
13289
13290           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
13291             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
13292           else
13293             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
13294           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
13295
13296           emit_insn ((mode == DImode
13297                       ? gen_lshrsi3
13298                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
13299           emit_insn ((mode == DImode
13300                       ? gen_andsi3
13301                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
13302           emit_move_insn (low[0], high[0]);
13303           emit_insn ((mode == DImode
13304                       ? gen_xorsi3
13305                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
13306         }
13307
13308       emit_insn ((mode == DImode
13309                     ? gen_ashlsi3
13310                     : gen_ashldi3) (low[0], low[0], operands[2]));
13311       emit_insn ((mode == DImode
13312                     ? gen_ashlsi3
13313                     : gen_ashldi3) (high[0], high[0], operands[2]));
13314       return;
13315     }
13316
13317   if (operands[1] == constm1_rtx)
13318     {
13319       /* For -1 << N, we can avoid the shld instruction, because we
13320          know that we're shifting 0...31/63 ones into a -1.  */
13321       emit_move_insn (low[0], constm1_rtx);
13322       if (optimize_size)
13323         emit_move_insn (high[0], low[0]);
13324       else
13325         emit_move_insn (high[0], constm1_rtx);
13326     }
13327   else
13328     {
13329       if (!rtx_equal_p (operands[0], operands[1]))
13330         emit_move_insn (operands[0], operands[1]);
13331
13332       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13333       emit_insn ((mode == DImode
13334                   ? gen_x86_shld_1
13335                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
13336     }
13337
13338   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
13339
13340   if (TARGET_CMOVE && scratch)
13341     {
13342       ix86_expand_clear (scratch);
13343       emit_insn ((mode == DImode
13344                   ? gen_x86_shift_adj_1
13345                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
13346     }
13347   else
13348     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
13349 }
13350
13351 void
13352 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
13353 {
13354   rtx low[2], high[2];
13355   int count;
13356   const int single_width = mode == DImode ? 32 : 64;
13357
13358   if (CONST_INT_P (operands[2]))
13359     {
13360       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13361       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13362
13363       if (count == single_width * 2 - 1)
13364         {
13365           emit_move_insn (high[0], high[1]);
13366           emit_insn ((mode == DImode
13367                       ? gen_ashrsi3
13368                       : gen_ashrdi3) (high[0], high[0],
13369                                       GEN_INT (single_width - 1)));
13370           emit_move_insn (low[0], high[0]);
13371
13372         }
13373       else if (count >= single_width)
13374         {
13375           emit_move_insn (low[0], high[1]);
13376           emit_move_insn (high[0], low[0]);
13377           emit_insn ((mode == DImode
13378                       ? gen_ashrsi3
13379                       : gen_ashrdi3) (high[0], high[0],
13380                                       GEN_INT (single_width - 1)));
13381           if (count > single_width)
13382             emit_insn ((mode == DImode
13383                         ? gen_ashrsi3
13384                         : gen_ashrdi3) (low[0], low[0],
13385                                         GEN_INT (count - single_width)));
13386         }
13387       else
13388         {
13389           if (!rtx_equal_p (operands[0], operands[1]))
13390             emit_move_insn (operands[0], operands[1]);
13391           emit_insn ((mode == DImode
13392                       ? gen_x86_shrd_1
13393                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13394           emit_insn ((mode == DImode
13395                       ? gen_ashrsi3
13396                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
13397         }
13398     }
13399   else
13400     {
13401       if (!rtx_equal_p (operands[0], operands[1]))
13402         emit_move_insn (operands[0], operands[1]);
13403
13404       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13405
13406       emit_insn ((mode == DImode
13407                   ? gen_x86_shrd_1
13408                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13409       emit_insn ((mode == DImode
13410                   ? gen_ashrsi3
13411                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
13412
13413       if (TARGET_CMOVE && scratch)
13414         {
13415           emit_move_insn (scratch, high[0]);
13416           emit_insn ((mode == DImode
13417                       ? gen_ashrsi3
13418                       : gen_ashrdi3) (scratch, scratch,
13419                                       GEN_INT (single_width - 1)));
13420           emit_insn ((mode == DImode
13421                       ? gen_x86_shift_adj_1
13422                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13423                                          scratch));
13424         }
13425       else
13426         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
13427     }
13428 }
13429
13430 void
13431 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
13432 {
13433   rtx low[2], high[2];
13434   int count;
13435   const int single_width = mode == DImode ? 32 : 64;
13436
13437   if (CONST_INT_P (operands[2]))
13438     {
13439       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13440       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13441
13442       if (count >= single_width)
13443         {
13444           emit_move_insn (low[0], high[1]);
13445           ix86_expand_clear (high[0]);
13446
13447           if (count > single_width)
13448             emit_insn ((mode == DImode
13449                         ? gen_lshrsi3
13450                         : gen_lshrdi3) (low[0], low[0],
13451                                         GEN_INT (count - single_width)));
13452         }
13453       else
13454         {
13455           if (!rtx_equal_p (operands[0], operands[1]))
13456             emit_move_insn (operands[0], operands[1]);
13457           emit_insn ((mode == DImode
13458                       ? gen_x86_shrd_1
13459                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13460           emit_insn ((mode == DImode
13461                       ? gen_lshrsi3
13462                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
13463         }
13464     }
13465   else
13466     {
13467       if (!rtx_equal_p (operands[0], operands[1]))
13468         emit_move_insn (operands[0], operands[1]);
13469
13470       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13471
13472       emit_insn ((mode == DImode
13473                   ? gen_x86_shrd_1
13474                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13475       emit_insn ((mode == DImode
13476                   ? gen_lshrsi3
13477                   : gen_lshrdi3) (high[0], high[0], operands[2]));
13478
13479       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
13480       if (TARGET_CMOVE && scratch)
13481         {
13482           ix86_expand_clear (scratch);
13483           emit_insn ((mode == DImode
13484                       ? gen_x86_shift_adj_1
13485                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13486                                                scratch));
13487         }
13488       else
13489         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
13490     }
13491 }
13492
13493 /* Predict just emitted jump instruction to be taken with probability PROB.  */
13494 static void
13495 predict_jump (int prob)
13496 {
13497   rtx insn = get_last_insn ();
13498   gcc_assert (JUMP_P (insn));
13499   REG_NOTES (insn)
13500     = gen_rtx_EXPR_LIST (REG_BR_PROB,
13501                          GEN_INT (prob),
13502                          REG_NOTES (insn));
13503 }
13504
13505 /* Helper function for the string operations below.  Dest VARIABLE whether
13506    it is aligned to VALUE bytes.  If true, jump to the label.  */
13507 static rtx
13508 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
13509 {
13510   rtx label = gen_label_rtx ();
13511   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
13512   if (GET_MODE (variable) == DImode)
13513     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
13514   else
13515     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
13516   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
13517                            1, label);
13518   if (epilogue)
13519     predict_jump (REG_BR_PROB_BASE * 50 / 100);
13520   else
13521     predict_jump (REG_BR_PROB_BASE * 90 / 100);
13522   return label;
13523 }
13524
13525 /* Adjust COUNTER by the VALUE.  */
13526 static void
13527 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
13528 {
13529   if (GET_MODE (countreg) == DImode)
13530     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
13531   else
13532     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
13533 }
13534
13535 /* Zero extend possibly SImode EXP to Pmode register.  */
13536 rtx
13537 ix86_zero_extend_to_Pmode (rtx exp)
13538 {
13539   rtx r;
13540   if (GET_MODE (exp) == VOIDmode)
13541     return force_reg (Pmode, exp);
13542   if (GET_MODE (exp) == Pmode)
13543     return copy_to_mode_reg (Pmode, exp);
13544   r = gen_reg_rtx (Pmode);
13545   emit_insn (gen_zero_extendsidi2 (r, exp));
13546   return r;
13547 }
13548
13549 /* Divide COUNTREG by SCALE.  */
13550 static rtx
13551 scale_counter (rtx countreg, int scale)
13552 {
13553   rtx sc;
13554   rtx piece_size_mask;
13555
13556   if (scale == 1)
13557     return countreg;
13558   if (CONST_INT_P (countreg))
13559     return GEN_INT (INTVAL (countreg) / scale);
13560   gcc_assert (REG_P (countreg));
13561
13562   piece_size_mask = GEN_INT (scale - 1);
13563   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
13564                             GEN_INT (exact_log2 (scale)),
13565                             NULL, 1, OPTAB_DIRECT);
13566   return sc;
13567 }
13568
13569 /* Return mode for the memcpy/memset loop counter.  Prefer SImode over
13570    DImode for constant loop counts.  */
13571
13572 static enum machine_mode
13573 counter_mode (rtx count_exp)
13574 {
13575   if (GET_MODE (count_exp) != VOIDmode)
13576     return GET_MODE (count_exp);
13577   if (GET_CODE (count_exp) != CONST_INT)
13578     return Pmode;
13579   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
13580     return DImode;
13581   return SImode;
13582 }
13583
13584 /* When SRCPTR is non-NULL, output simple loop to move memory
13585    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
13586    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
13587    equivalent loop to set memory by VALUE (supposed to be in MODE).
13588
13589    The size is rounded down to whole number of chunk size moved at once.
13590    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
13591
13592
13593 static void
13594 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
13595                                rtx destptr, rtx srcptr, rtx value,
13596                                rtx count, enum machine_mode mode, int unroll,
13597                                int expected_size)
13598 {
13599   rtx out_label, top_label, iter, tmp;
13600   enum machine_mode iter_mode = counter_mode (count);
13601   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
13602   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
13603   rtx size;
13604   rtx x_addr;
13605   rtx y_addr;
13606   int i;
13607
13608   top_label = gen_label_rtx ();
13609   out_label = gen_label_rtx ();
13610   iter = gen_reg_rtx (iter_mode);
13611
13612   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
13613                               NULL, 1, OPTAB_DIRECT);
13614   /* Those two should combine.  */
13615   if (piece_size == const1_rtx)
13616     {
13617       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
13618                                true, out_label);
13619       predict_jump (REG_BR_PROB_BASE * 10 / 100);
13620     }
13621   emit_move_insn (iter, const0_rtx);
13622
13623   emit_label (top_label);
13624
13625   tmp = convert_modes (Pmode, iter_mode, iter, true);
13626   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
13627   destmem = change_address (destmem, mode, x_addr);
13628
13629   if (srcmem)
13630     {
13631       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
13632       srcmem = change_address (srcmem, mode, y_addr);
13633
13634       /* When unrolling for chips that reorder memory reads and writes,
13635          we can save registers by using single temporary.
13636          Also using 4 temporaries is overkill in 32bit mode.  */
13637       if (!TARGET_64BIT && 0)
13638         {
13639           for (i = 0; i < unroll; i++)
13640             {
13641               if (i)
13642                 {
13643                   destmem =
13644                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13645                   srcmem =
13646                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13647                 }
13648               emit_move_insn (destmem, srcmem);
13649             }
13650         }
13651       else
13652         {
13653           rtx tmpreg[4];
13654           gcc_assert (unroll <= 4);
13655           for (i = 0; i < unroll; i++)
13656             {
13657               tmpreg[i] = gen_reg_rtx (mode);
13658               if (i)
13659                 {
13660                   srcmem =
13661                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13662                 }
13663               emit_move_insn (tmpreg[i], srcmem);
13664             }
13665           for (i = 0; i < unroll; i++)
13666             {
13667               if (i)
13668                 {
13669                   destmem =
13670                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13671                 }
13672               emit_move_insn (destmem, tmpreg[i]);
13673             }
13674         }
13675     }
13676   else
13677     for (i = 0; i < unroll; i++)
13678       {
13679         if (i)
13680           destmem =
13681             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13682         emit_move_insn (destmem, value);
13683       }
13684
13685   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
13686                              true, OPTAB_LIB_WIDEN);
13687   if (tmp != iter)
13688     emit_move_insn (iter, tmp);
13689
13690   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
13691                            true, top_label);
13692   if (expected_size != -1)
13693     {
13694       expected_size /= GET_MODE_SIZE (mode) * unroll;
13695       if (expected_size == 0)
13696         predict_jump (0);
13697       else if (expected_size > REG_BR_PROB_BASE)
13698         predict_jump (REG_BR_PROB_BASE - 1);
13699       else
13700         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
13701     }
13702   else
13703     predict_jump (REG_BR_PROB_BASE * 80 / 100);
13704   iter = ix86_zero_extend_to_Pmode (iter);
13705   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
13706                              true, OPTAB_LIB_WIDEN);
13707   if (tmp != destptr)
13708     emit_move_insn (destptr, tmp);
13709   if (srcptr)
13710     {
13711       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
13712                                  true, OPTAB_LIB_WIDEN);
13713       if (tmp != srcptr)
13714         emit_move_insn (srcptr, tmp);
13715     }
13716   emit_label (out_label);
13717 }
13718
13719 /* Output "rep; mov" instruction.
13720    Arguments have same meaning as for previous function */
13721 static void
13722 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
13723                            rtx destptr, rtx srcptr,
13724                            rtx count,
13725                            enum machine_mode mode)
13726 {
13727   rtx destexp;
13728   rtx srcexp;
13729   rtx countreg;
13730
13731   /* If the size is known, it is shorter to use rep movs.  */
13732   if (mode == QImode && CONST_INT_P (count)
13733       && !(INTVAL (count) & 3))
13734     mode = SImode;
13735
13736   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13737     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13738   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
13739     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
13740   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13741   if (mode != QImode)
13742     {
13743       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13744                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13745       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13746       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
13747                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13748       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
13749     }
13750   else
13751     {
13752       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13753       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
13754     }
13755   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
13756                           destexp, srcexp));
13757 }
13758
13759 /* Output "rep; stos" instruction.
13760    Arguments have same meaning as for previous function */
13761 static void
13762 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
13763                             rtx count,
13764                             enum machine_mode mode)
13765 {
13766   rtx destexp;
13767   rtx countreg;
13768
13769   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13770     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13771   value = force_reg (mode, gen_lowpart (mode, value));
13772   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13773   if (mode != QImode)
13774     {
13775       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13776                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13777       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13778     }
13779   else
13780     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13781   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
13782 }
13783
13784 static void
13785 emit_strmov (rtx destmem, rtx srcmem,
13786              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
13787 {
13788   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
13789   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
13790   emit_insn (gen_strmov (destptr, dest, srcptr, src));
13791 }
13792
13793 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
13794 static void
13795 expand_movmem_epilogue (rtx destmem, rtx srcmem,
13796                         rtx destptr, rtx srcptr, rtx count, int max_size)
13797 {
13798   rtx src, dest;
13799   if (CONST_INT_P (count))
13800     {
13801       HOST_WIDE_INT countval = INTVAL (count);
13802       int offset = 0;
13803
13804       if ((countval & 0x10) && max_size > 16)
13805         {
13806           if (TARGET_64BIT)
13807             {
13808               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13809               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
13810             }
13811           else
13812             gcc_unreachable ();
13813           offset += 16;
13814         }
13815       if ((countval & 0x08) && max_size > 8)
13816         {
13817           if (TARGET_64BIT)
13818             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13819           else
13820             {
13821               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13822               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
13823             }
13824           offset += 8;
13825         }
13826       if ((countval & 0x04) && max_size > 4)
13827         {
13828           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13829           offset += 4;
13830         }
13831       if ((countval & 0x02) && max_size > 2)
13832         {
13833           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
13834           offset += 2;
13835         }
13836       if ((countval & 0x01) && max_size > 1)
13837         {
13838           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
13839           offset += 1;
13840         }
13841       return;
13842     }
13843   if (max_size > 8)
13844     {
13845       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
13846                                     count, 1, OPTAB_DIRECT);
13847       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
13848                                      count, QImode, 1, 4);
13849       return;
13850     }
13851
13852   /* When there are stringops, we can cheaply increase dest and src pointers.
13853      Otherwise we save code size by maintaining offset (zero is readily
13854      available from preceding rep operation) and using x86 addressing modes.
13855    */
13856   if (TARGET_SINGLE_STRINGOP)
13857     {
13858       if (max_size > 4)
13859         {
13860           rtx label = ix86_expand_aligntest (count, 4, true);
13861           src = change_address (srcmem, SImode, srcptr);
13862           dest = change_address (destmem, SImode, destptr);
13863           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13864           emit_label (label);
13865           LABEL_NUSES (label) = 1;
13866         }
13867       if (max_size > 2)
13868         {
13869           rtx label = ix86_expand_aligntest (count, 2, true);
13870           src = change_address (srcmem, HImode, srcptr);
13871           dest = change_address (destmem, HImode, destptr);
13872           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13873           emit_label (label);
13874           LABEL_NUSES (label) = 1;
13875         }
13876       if (max_size > 1)
13877         {
13878           rtx label = ix86_expand_aligntest (count, 1, true);
13879           src = change_address (srcmem, QImode, srcptr);
13880           dest = change_address (destmem, QImode, destptr);
13881           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13882           emit_label (label);
13883           LABEL_NUSES (label) = 1;
13884         }
13885     }
13886   else
13887     {
13888       rtx offset = force_reg (Pmode, const0_rtx);
13889       rtx tmp;
13890
13891       if (max_size > 4)
13892         {
13893           rtx label = ix86_expand_aligntest (count, 4, true);
13894           src = change_address (srcmem, SImode, srcptr);
13895           dest = change_address (destmem, SImode, destptr);
13896           emit_move_insn (dest, src);
13897           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
13898                                      true, OPTAB_LIB_WIDEN);
13899           if (tmp != offset)
13900             emit_move_insn (offset, tmp);
13901           emit_label (label);
13902           LABEL_NUSES (label) = 1;
13903         }
13904       if (max_size > 2)
13905         {
13906           rtx label = ix86_expand_aligntest (count, 2, true);
13907           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13908           src = change_address (srcmem, HImode, tmp);
13909           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13910           dest = change_address (destmem, HImode, tmp);
13911           emit_move_insn (dest, src);
13912           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
13913                                      true, OPTAB_LIB_WIDEN);
13914           if (tmp != offset)
13915             emit_move_insn (offset, tmp);
13916           emit_label (label);
13917           LABEL_NUSES (label) = 1;
13918         }
13919       if (max_size > 1)
13920         {
13921           rtx label = ix86_expand_aligntest (count, 1, true);
13922           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13923           src = change_address (srcmem, QImode, tmp);
13924           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13925           dest = change_address (destmem, QImode, tmp);
13926           emit_move_insn (dest, src);
13927           emit_label (label);
13928           LABEL_NUSES (label) = 1;
13929         }
13930     }
13931 }
13932
13933 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13934 static void
13935 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
13936                                  rtx count, int max_size)
13937 {
13938   count =
13939     expand_simple_binop (counter_mode (count), AND, count,
13940                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
13941   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
13942                                  gen_lowpart (QImode, value), count, QImode,
13943                                  1, max_size / 2);
13944 }
13945
13946 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13947 static void
13948 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
13949 {
13950   rtx dest;
13951
13952   if (CONST_INT_P (count))
13953     {
13954       HOST_WIDE_INT countval = INTVAL (count);
13955       int offset = 0;
13956
13957       if ((countval & 0x10) && max_size > 16)
13958         {
13959           if (TARGET_64BIT)
13960             {
13961               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13962               emit_insn (gen_strset (destptr, dest, value));
13963               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
13964               emit_insn (gen_strset (destptr, dest, value));
13965             }
13966           else
13967             gcc_unreachable ();
13968           offset += 16;
13969         }
13970       if ((countval & 0x08) && max_size > 8)
13971         {
13972           if (TARGET_64BIT)
13973             {
13974               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13975               emit_insn (gen_strset (destptr, dest, value));
13976             }
13977           else
13978             {
13979               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13980               emit_insn (gen_strset (destptr, dest, value));
13981               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
13982               emit_insn (gen_strset (destptr, dest, value));
13983             }
13984           offset += 8;
13985         }
13986       if ((countval & 0x04) && max_size > 4)
13987         {
13988           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13989           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13990           offset += 4;
13991         }
13992       if ((countval & 0x02) && max_size > 2)
13993         {
13994           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
13995           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13996           offset += 2;
13997         }
13998       if ((countval & 0x01) && max_size > 1)
13999         {
14000           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
14001           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
14002           offset += 1;
14003         }
14004       return;
14005     }
14006   if (max_size > 32)
14007     {
14008       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
14009       return;
14010     }
14011   if (max_size > 16)
14012     {
14013       rtx label = ix86_expand_aligntest (count, 16, true);
14014       if (TARGET_64BIT)
14015         {
14016           dest = change_address (destmem, DImode, destptr);
14017           emit_insn (gen_strset (destptr, dest, value));
14018           emit_insn (gen_strset (destptr, dest, value));
14019         }
14020       else
14021         {
14022           dest = change_address (destmem, SImode, destptr);
14023           emit_insn (gen_strset (destptr, dest, value));
14024           emit_insn (gen_strset (destptr, dest, value));
14025           emit_insn (gen_strset (destptr, dest, value));
14026           emit_insn (gen_strset (destptr, dest, value));
14027         }
14028       emit_label (label);
14029       LABEL_NUSES (label) = 1;
14030     }
14031   if (max_size > 8)
14032     {
14033       rtx label = ix86_expand_aligntest (count, 8, true);
14034       if (TARGET_64BIT)
14035         {
14036           dest = change_address (destmem, DImode, destptr);
14037           emit_insn (gen_strset (destptr, dest, value));
14038         }
14039       else
14040         {
14041           dest = change_address (destmem, SImode, destptr);
14042           emit_insn (gen_strset (destptr, dest, value));
14043           emit_insn (gen_strset (destptr, dest, value));
14044         }
14045       emit_label (label);
14046       LABEL_NUSES (label) = 1;
14047     }
14048   if (max_size > 4)
14049     {
14050       rtx label = ix86_expand_aligntest (count, 4, true);
14051       dest = change_address (destmem, SImode, destptr);
14052       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
14053       emit_label (label);
14054       LABEL_NUSES (label) = 1;
14055     }
14056   if (max_size > 2)
14057     {
14058       rtx label = ix86_expand_aligntest (count, 2, true);
14059       dest = change_address (destmem, HImode, destptr);
14060       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
14061       emit_label (label);
14062       LABEL_NUSES (label) = 1;
14063     }
14064   if (max_size > 1)
14065     {
14066       rtx label = ix86_expand_aligntest (count, 1, true);
14067       dest = change_address (destmem, QImode, destptr);
14068       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
14069       emit_label (label);
14070       LABEL_NUSES (label) = 1;
14071     }
14072 }
14073
14074 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
14075    DESIRED_ALIGNMENT.  */
14076 static void
14077 expand_movmem_prologue (rtx destmem, rtx srcmem,
14078                         rtx destptr, rtx srcptr, rtx count,
14079                         int align, int desired_alignment)
14080 {
14081   if (align <= 1 && desired_alignment > 1)
14082     {
14083       rtx label = ix86_expand_aligntest (destptr, 1, false);
14084       srcmem = change_address (srcmem, QImode, srcptr);
14085       destmem = change_address (destmem, QImode, destptr);
14086       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14087       ix86_adjust_counter (count, 1);
14088       emit_label (label);
14089       LABEL_NUSES (label) = 1;
14090     }
14091   if (align <= 2 && desired_alignment > 2)
14092     {
14093       rtx label = ix86_expand_aligntest (destptr, 2, false);
14094       srcmem = change_address (srcmem, HImode, srcptr);
14095       destmem = change_address (destmem, HImode, destptr);
14096       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14097       ix86_adjust_counter (count, 2);
14098       emit_label (label);
14099       LABEL_NUSES (label) = 1;
14100     }
14101   if (align <= 4 && desired_alignment > 4)
14102     {
14103       rtx label = ix86_expand_aligntest (destptr, 4, false);
14104       srcmem = change_address (srcmem, SImode, srcptr);
14105       destmem = change_address (destmem, SImode, destptr);
14106       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14107       ix86_adjust_counter (count, 4);
14108       emit_label (label);
14109       LABEL_NUSES (label) = 1;
14110     }
14111   gcc_assert (desired_alignment <= 8);
14112 }
14113
14114 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
14115    DESIRED_ALIGNMENT.  */
14116 static void
14117 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
14118                         int align, int desired_alignment)
14119 {
14120   if (align <= 1 && desired_alignment > 1)
14121     {
14122       rtx label = ix86_expand_aligntest (destptr, 1, false);
14123       destmem = change_address (destmem, QImode, destptr);
14124       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
14125       ix86_adjust_counter (count, 1);
14126       emit_label (label);
14127       LABEL_NUSES (label) = 1;
14128     }
14129   if (align <= 2 && desired_alignment > 2)
14130     {
14131       rtx label = ix86_expand_aligntest (destptr, 2, false);
14132       destmem = change_address (destmem, HImode, destptr);
14133       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
14134       ix86_adjust_counter (count, 2);
14135       emit_label (label);
14136       LABEL_NUSES (label) = 1;
14137     }
14138   if (align <= 4 && desired_alignment > 4)
14139     {
14140       rtx label = ix86_expand_aligntest (destptr, 4, false);
14141       destmem = change_address (destmem, SImode, destptr);
14142       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
14143       ix86_adjust_counter (count, 4);
14144       emit_label (label);
14145       LABEL_NUSES (label) = 1;
14146     }
14147   gcc_assert (desired_alignment <= 8);
14148 }
14149
14150 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
14151 static enum stringop_alg
14152 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
14153             int *dynamic_check)
14154 {
14155   const struct stringop_algs * algs;
14156
14157   *dynamic_check = -1;
14158   if (memset)
14159     algs = &ix86_cost->memset[TARGET_64BIT != 0];
14160   else
14161     algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
14162   if (stringop_alg != no_stringop)
14163     return stringop_alg;
14164   /* rep; movq or rep; movl is the smallest variant.  */
14165   else if (optimize_size)
14166     {
14167       if (!count || (count & 3))
14168         return rep_prefix_1_byte;
14169       else
14170         return rep_prefix_4_byte;
14171     }
14172   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
14173    */
14174   else if (expected_size != -1 && expected_size < 4)
14175     return loop_1_byte;
14176   else if (expected_size != -1)
14177     {
14178       unsigned int i;
14179       enum stringop_alg alg = libcall;
14180       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
14181         {
14182           gcc_assert (algs->size[i].max);
14183           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
14184             {
14185               if (algs->size[i].alg != libcall)
14186                 alg = algs->size[i].alg;
14187               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
14188                  last non-libcall inline algorithm.  */
14189               if (TARGET_INLINE_ALL_STRINGOPS)
14190                 {
14191                   /* When the current size is best to be copied by a libcall,
14192                      but we are still forced to inline, run the heuristic bellow
14193                      that will pick code for medium sized blocks.  */
14194                   if (alg != libcall)
14195                     return alg;
14196                   break;
14197                 }
14198               else
14199                 return algs->size[i].alg;
14200             }
14201         }
14202       gcc_assert (TARGET_INLINE_ALL_STRINGOPS);
14203     }
14204   /* When asked to inline the call anyway, try to pick meaningful choice.
14205      We look for maximal size of block that is faster to copy by hand and
14206      take blocks of at most of that size guessing that average size will
14207      be roughly half of the block.
14208
14209      If this turns out to be bad, we might simply specify the preferred
14210      choice in ix86_costs.  */
14211   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
14212       && algs->unknown_size == libcall)
14213     {
14214       int max = -1;
14215       enum stringop_alg alg;
14216       int i;
14217
14218       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
14219         if (algs->size[i].alg != libcall && algs->size[i].alg)
14220           max = algs->size[i].max;
14221       if (max == -1)
14222         max = 4096;
14223       alg = decide_alg (count, max / 2, memset, dynamic_check);
14224       gcc_assert (*dynamic_check == -1);
14225       gcc_assert (alg != libcall);
14226       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
14227         *dynamic_check = max;
14228       return alg;
14229     }
14230   return algs->unknown_size;
14231 }
14232
14233 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
14234    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
14235 static int
14236 decide_alignment (int align,
14237                   enum stringop_alg alg,
14238                   int expected_size)
14239 {
14240   int desired_align = 0;
14241   switch (alg)
14242     {
14243       case no_stringop:
14244         gcc_unreachable ();
14245       case loop:
14246       case unrolled_loop:
14247         desired_align = GET_MODE_SIZE (Pmode);
14248         break;
14249       case rep_prefix_8_byte:
14250         desired_align = 8;
14251         break;
14252       case rep_prefix_4_byte:
14253         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
14254            copying whole cacheline at once.  */
14255         if (TARGET_PENTIUMPRO)
14256           desired_align = 8;
14257         else
14258           desired_align = 4;
14259         break;
14260       case rep_prefix_1_byte:
14261         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
14262            copying whole cacheline at once.  */
14263         if (TARGET_PENTIUMPRO)
14264           desired_align = 8;
14265         else
14266           desired_align = 1;
14267         break;
14268       case loop_1_byte:
14269         desired_align = 1;
14270         break;
14271       case libcall:
14272         return 0;
14273     }
14274
14275   if (optimize_size)
14276     desired_align = 1;
14277   if (desired_align < align)
14278     desired_align = align;
14279   if (expected_size != -1 && expected_size < 4)
14280     desired_align = align;
14281   return desired_align;
14282 }
14283
14284 /* Return the smallest power of 2 greater than VAL.  */
14285 static int
14286 smallest_pow2_greater_than (int val)
14287 {
14288   int ret = 1;
14289   while (ret <= val)
14290     ret <<= 1;
14291   return ret;
14292 }
14293
14294 /* Expand string move (memcpy) operation.  Use i386 string operations when
14295    profitable.  expand_clrmem contains similar code. The code depends upon
14296    architecture, block size and alignment, but always has the same
14297    overall structure:
14298
14299    1) Prologue guard: Conditional that jumps up to epilogues for small
14300       blocks that can be handled by epilogue alone.  This is faster but
14301       also needed for correctness, since prologue assume the block is larger
14302       than the desired alignment.
14303
14304       Optional dynamic check for size and libcall for large
14305       blocks is emitted here too, with -minline-stringops-dynamically.
14306
14307    2) Prologue: copy first few bytes in order to get destination aligned
14308       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
14309       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
14310       We emit either a jump tree on power of two sized blocks, or a byte loop.
14311
14312    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
14313       with specified algorithm.
14314
14315    4) Epilogue: code copying tail of the block that is too small to be
14316       handled by main body (or up to size guarded by prologue guard).  */
14317
14318 int
14319 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
14320                     rtx expected_align_exp, rtx expected_size_exp)
14321 {
14322   rtx destreg;
14323   rtx srcreg;
14324   rtx label = NULL;
14325   rtx tmp;
14326   rtx jump_around_label = NULL;
14327   HOST_WIDE_INT align = 1;
14328   unsigned HOST_WIDE_INT count = 0;
14329   HOST_WIDE_INT expected_size = -1;
14330   int size_needed = 0, epilogue_size_needed;
14331   int desired_align = 0;
14332   enum stringop_alg alg;
14333   int dynamic_check;
14334
14335   if (CONST_INT_P (align_exp))
14336     align = INTVAL (align_exp);
14337   /* i386 can do misaligned access on reasonably increased cost.  */
14338   if (CONST_INT_P (expected_align_exp)
14339       && INTVAL (expected_align_exp) > align)
14340     align = INTVAL (expected_align_exp);
14341   if (CONST_INT_P (count_exp))
14342     count = expected_size = INTVAL (count_exp);
14343   if (CONST_INT_P (expected_size_exp) && count == 0)
14344     expected_size = INTVAL (expected_size_exp);
14345
14346   /* Step 0: Decide on preferred algorithm, desired alignment and
14347      size of chunks to be copied by main loop.  */
14348
14349   alg = decide_alg (count, expected_size, false, &dynamic_check);
14350   desired_align = decide_alignment (align, alg, expected_size);
14351
14352   if (!TARGET_ALIGN_STRINGOPS)
14353     align = desired_align;
14354
14355   if (alg == libcall)
14356     return 0;
14357   gcc_assert (alg != no_stringop);
14358   if (!count)
14359     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
14360   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14361   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
14362   switch (alg)
14363     {
14364     case libcall:
14365     case no_stringop:
14366       gcc_unreachable ();
14367     case loop:
14368       size_needed = GET_MODE_SIZE (Pmode);
14369       break;
14370     case unrolled_loop:
14371       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
14372       break;
14373     case rep_prefix_8_byte:
14374       size_needed = 8;
14375       break;
14376     case rep_prefix_4_byte:
14377       size_needed = 4;
14378       break;
14379     case rep_prefix_1_byte:
14380     case loop_1_byte:
14381       size_needed = 1;
14382       break;
14383     }
14384
14385   epilogue_size_needed = size_needed;
14386
14387   /* Step 1: Prologue guard.  */
14388
14389   /* Alignment code needs count to be in register.  */
14390   if (CONST_INT_P (count_exp) && desired_align > align)
14391     {
14392       enum machine_mode mode = SImode;
14393       if (TARGET_64BIT && (count & ~0xffffffff))
14394         mode = DImode;
14395       count_exp = force_reg (mode, count_exp);
14396     }
14397   gcc_assert (desired_align >= 1 && align >= 1);
14398
14399   /* Ensure that alignment prologue won't copy past end of block.  */
14400   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14401     {
14402       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14403       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14404          Make sure it is power of 2.  */
14405       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14406
14407       label = gen_label_rtx ();
14408       emit_cmp_and_jump_insns (count_exp,
14409                                GEN_INT (epilogue_size_needed),
14410                                LTU, 0, counter_mode (count_exp), 1, label);
14411       if (GET_CODE (count_exp) == CONST_INT)
14412         ;
14413       else if (expected_size == -1 || expected_size < epilogue_size_needed)
14414         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14415       else
14416         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14417     }
14418   /* Emit code to decide on runtime whether library call or inline should be
14419      used.  */
14420   if (dynamic_check != -1)
14421     {
14422       rtx hot_label = gen_label_rtx ();
14423       jump_around_label = gen_label_rtx ();
14424       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14425                                LEU, 0, GET_MODE (count_exp), 1, hot_label);
14426       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14427       emit_block_move_via_libcall (dst, src, count_exp, false);
14428       emit_jump (jump_around_label);
14429       emit_label (hot_label);
14430     }
14431
14432   /* Step 2: Alignment prologue.  */
14433
14434   if (desired_align > align)
14435     {
14436       /* Except for the first move in epilogue, we no longer know
14437          constant offset in aliasing info.  It don't seems to worth
14438          the pain to maintain it for the first move, so throw away
14439          the info early.  */
14440       src = change_address (src, BLKmode, srcreg);
14441       dst = change_address (dst, BLKmode, destreg);
14442       expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
14443                               desired_align);
14444     }
14445   if (label && size_needed == 1)
14446     {
14447       emit_label (label);
14448       LABEL_NUSES (label) = 1;
14449       label = NULL;
14450     }
14451
14452   /* Step 3: Main loop.  */
14453
14454   switch (alg)
14455     {
14456     case libcall:
14457     case no_stringop:
14458       gcc_unreachable ();
14459     case loop_1_byte:
14460       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14461                                      count_exp, QImode, 1, expected_size);
14462       break;
14463     case loop:
14464       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14465                                      count_exp, Pmode, 1, expected_size);
14466       break;
14467     case unrolled_loop:
14468       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
14469          registers for 4 temporaries anyway.  */
14470       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14471                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
14472                                      expected_size);
14473       break;
14474     case rep_prefix_8_byte:
14475       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14476                                  DImode);
14477       break;
14478     case rep_prefix_4_byte:
14479       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14480                                  SImode);
14481       break;
14482     case rep_prefix_1_byte:
14483       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14484                                  QImode);
14485       break;
14486     }
14487   /* Adjust properly the offset of src and dest memory for aliasing.  */
14488   if (CONST_INT_P (count_exp))
14489     {
14490       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
14491                                           (count / size_needed) * size_needed);
14492       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14493                                           (count / size_needed) * size_needed);
14494     }
14495   else
14496     {
14497       src = change_address (src, BLKmode, srcreg);
14498       dst = change_address (dst, BLKmode, destreg);
14499     }
14500
14501   /* Step 4: Epilogue to copy the remaining bytes.  */
14502
14503   if (label)
14504     {
14505       /* When the main loop is done, COUNT_EXP might hold original count,
14506          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14507          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14508          bytes. Compensate if needed.  */
14509
14510       if (size_needed < epilogue_size_needed)
14511         {
14512           tmp =
14513             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14514                                  GEN_INT (size_needed - 1), count_exp, 1,
14515                                  OPTAB_DIRECT);
14516           if (tmp != count_exp)
14517             emit_move_insn (count_exp, tmp);
14518         }
14519       emit_label (label);
14520       LABEL_NUSES (label) = 1;
14521     }
14522
14523   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14524     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
14525                             epilogue_size_needed);
14526   if (jump_around_label)
14527     emit_label (jump_around_label);
14528   return 1;
14529 }
14530
14531 /* Helper function for memcpy.  For QImode value 0xXY produce
14532    0xXYXYXYXY of wide specified by MODE.  This is essentially
14533    a * 0x10101010, but we can do slightly better than
14534    synth_mult by unwinding the sequence by hand on CPUs with
14535    slow multiply.  */
14536 static rtx
14537 promote_duplicated_reg (enum machine_mode mode, rtx val)
14538 {
14539   enum machine_mode valmode = GET_MODE (val);
14540   rtx tmp;
14541   int nops = mode == DImode ? 3 : 2;
14542
14543   gcc_assert (mode == SImode || mode == DImode);
14544   if (val == const0_rtx)
14545     return copy_to_mode_reg (mode, const0_rtx);
14546   if (CONST_INT_P (val))
14547     {
14548       HOST_WIDE_INT v = INTVAL (val) & 255;
14549
14550       v |= v << 8;
14551       v |= v << 16;
14552       if (mode == DImode)
14553         v |= (v << 16) << 16;
14554       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
14555     }
14556
14557   if (valmode == VOIDmode)
14558     valmode = QImode;
14559   if (valmode != QImode)
14560     val = gen_lowpart (QImode, val);
14561   if (mode == QImode)
14562     return val;
14563   if (!TARGET_PARTIAL_REG_STALL)
14564     nops--;
14565   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
14566       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
14567       <= (ix86_cost->shift_const + ix86_cost->add) * nops
14568           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
14569     {
14570       rtx reg = convert_modes (mode, QImode, val, true);
14571       tmp = promote_duplicated_reg (mode, const1_rtx);
14572       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
14573                                   OPTAB_DIRECT);
14574     }
14575   else
14576     {
14577       rtx reg = convert_modes (mode, QImode, val, true);
14578
14579       if (!TARGET_PARTIAL_REG_STALL)
14580         if (mode == SImode)
14581           emit_insn (gen_movsi_insv_1 (reg, reg));
14582         else
14583           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
14584       else
14585         {
14586           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
14587                                      NULL, 1, OPTAB_DIRECT);
14588           reg =
14589             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14590         }
14591       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
14592                                  NULL, 1, OPTAB_DIRECT);
14593       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14594       if (mode == SImode)
14595         return reg;
14596       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
14597                                  NULL, 1, OPTAB_DIRECT);
14598       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14599       return reg;
14600     }
14601 }
14602
14603 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
14604    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
14605    alignment from ALIGN to DESIRED_ALIGN.  */
14606 static rtx
14607 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
14608 {
14609   rtx promoted_val;
14610
14611   if (TARGET_64BIT
14612       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
14613     promoted_val = promote_duplicated_reg (DImode, val);
14614   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
14615     promoted_val = promote_duplicated_reg (SImode, val);
14616   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
14617     promoted_val = promote_duplicated_reg (HImode, val);
14618   else
14619     promoted_val = val;
14620
14621   return promoted_val;
14622 }
14623
14624 /* Expand string clear operation (bzero).  Use i386 string operations when
14625    profitable.  See expand_movmem comment for explanation of individual
14626    steps performed.  */
14627 int
14628 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
14629                     rtx expected_align_exp, rtx expected_size_exp)
14630 {
14631   rtx destreg;
14632   rtx label = NULL;
14633   rtx tmp;
14634   rtx jump_around_label = NULL;
14635   HOST_WIDE_INT align = 1;
14636   unsigned HOST_WIDE_INT count = 0;
14637   HOST_WIDE_INT expected_size = -1;
14638   int size_needed = 0, epilogue_size_needed;
14639   int desired_align = 0;
14640   enum stringop_alg alg;
14641   rtx promoted_val = NULL;
14642   bool force_loopy_epilogue = false;
14643   int dynamic_check;
14644
14645   if (CONST_INT_P (align_exp))
14646     align = INTVAL (align_exp);
14647   /* i386 can do misaligned access on reasonably increased cost.  */
14648   if (CONST_INT_P (expected_align_exp)
14649       && INTVAL (expected_align_exp) > align)
14650     align = INTVAL (expected_align_exp);
14651   if (CONST_INT_P (count_exp))
14652     count = expected_size = INTVAL (count_exp);
14653   if (CONST_INT_P (expected_size_exp) && count == 0)
14654     expected_size = INTVAL (expected_size_exp);
14655
14656   /* Step 0: Decide on preferred algorithm, desired alignment and
14657      size of chunks to be copied by main loop.  */
14658
14659   alg = decide_alg (count, expected_size, true, &dynamic_check);
14660   desired_align = decide_alignment (align, alg, expected_size);
14661
14662   if (!TARGET_ALIGN_STRINGOPS)
14663     align = desired_align;
14664
14665   if (alg == libcall)
14666     return 0;
14667   gcc_assert (alg != no_stringop);
14668   if (!count)
14669     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
14670   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14671   switch (alg)
14672     {
14673     case libcall:
14674     case no_stringop:
14675       gcc_unreachable ();
14676     case loop:
14677       size_needed = GET_MODE_SIZE (Pmode);
14678       break;
14679     case unrolled_loop:
14680       size_needed = GET_MODE_SIZE (Pmode) * 4;
14681       break;
14682     case rep_prefix_8_byte:
14683       size_needed = 8;
14684       break;
14685     case rep_prefix_4_byte:
14686       size_needed = 4;
14687       break;
14688     case rep_prefix_1_byte:
14689     case loop_1_byte:
14690       size_needed = 1;
14691       break;
14692     }
14693   epilogue_size_needed = size_needed;
14694
14695   /* Step 1: Prologue guard.  */
14696
14697   /* Alignment code needs count to be in register.  */
14698   if (CONST_INT_P (count_exp) && desired_align > align)
14699     {
14700       enum machine_mode mode = SImode;
14701       if (TARGET_64BIT && (count & ~0xffffffff))
14702         mode = DImode;
14703       count_exp = force_reg (mode, count_exp);
14704     }
14705   /* Do the cheap promotion to allow better CSE across the
14706      main loop and epilogue (ie one load of the big constant in the
14707      front of all code.  */
14708   if (CONST_INT_P (val_exp))
14709     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14710                                                    desired_align, align);
14711   /* Ensure that alignment prologue won't copy past end of block.  */
14712   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14713     {
14714       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14715       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14716          Make sure it is power of 2.  */
14717       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14718
14719       /* To improve performance of small blocks, we jump around the VAL
14720          promoting mode.  This mean that if the promoted VAL is not constant,
14721          we might not use it in the epilogue and have to use byte
14722          loop variant.  */
14723       if (epilogue_size_needed > 2 && !promoted_val)
14724         force_loopy_epilogue = true;
14725       label = gen_label_rtx ();
14726       emit_cmp_and_jump_insns (count_exp,
14727                                GEN_INT (epilogue_size_needed),
14728                                LTU, 0, counter_mode (count_exp), 1, label);
14729       if (GET_CODE (count_exp) == CONST_INT)
14730         ;
14731       else if (expected_size == -1 || expected_size <= epilogue_size_needed)
14732         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14733       else
14734         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14735     }
14736   if (dynamic_check != -1)
14737     {
14738       rtx hot_label = gen_label_rtx ();
14739       jump_around_label = gen_label_rtx ();
14740       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14741                                LEU, 0, counter_mode (count_exp), 1, hot_label);
14742       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14743       set_storage_via_libcall (dst, count_exp, val_exp, false);
14744       emit_jump (jump_around_label);
14745       emit_label (hot_label);
14746     }
14747
14748   /* Step 2: Alignment prologue.  */
14749
14750   /* Do the expensive promotion once we branched off the small blocks.  */
14751   if (!promoted_val)
14752     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14753                                                    desired_align, align);
14754   gcc_assert (desired_align >= 1 && align >= 1);
14755
14756   if (desired_align > align)
14757     {
14758       /* Except for the first move in epilogue, we no longer know
14759          constant offset in aliasing info.  It don't seems to worth
14760          the pain to maintain it for the first move, so throw away
14761          the info early.  */
14762       dst = change_address (dst, BLKmode, destreg);
14763       expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
14764                               desired_align);
14765     }
14766   if (label && size_needed == 1)
14767     {
14768       emit_label (label);
14769       LABEL_NUSES (label) = 1;
14770       label = NULL;
14771     }
14772
14773   /* Step 3: Main loop.  */
14774
14775   switch (alg)
14776     {
14777     case libcall:
14778     case no_stringop:
14779       gcc_unreachable ();
14780     case loop_1_byte:
14781       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14782                                      count_exp, QImode, 1, expected_size);
14783       break;
14784     case loop:
14785       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14786                                      count_exp, Pmode, 1, expected_size);
14787       break;
14788     case unrolled_loop:
14789       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14790                                      count_exp, Pmode, 4, expected_size);
14791       break;
14792     case rep_prefix_8_byte:
14793       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14794                                   DImode);
14795       break;
14796     case rep_prefix_4_byte:
14797       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14798                                   SImode);
14799       break;
14800     case rep_prefix_1_byte:
14801       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14802                                   QImode);
14803       break;
14804     }
14805   /* Adjust properly the offset of src and dest memory for aliasing.  */
14806   if (CONST_INT_P (count_exp))
14807     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14808                                         (count / size_needed) * size_needed);
14809   else
14810     dst = change_address (dst, BLKmode, destreg);
14811
14812   /* Step 4: Epilogue to copy the remaining bytes.  */
14813
14814   if (label)
14815     {
14816       /* When the main loop is done, COUNT_EXP might hold original count,
14817          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14818          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14819          bytes. Compensate if needed.  */
14820
14821       if (size_needed < desired_align - align)
14822         {
14823           tmp =
14824             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14825                                  GEN_INT (size_needed - 1), count_exp, 1,
14826                                  OPTAB_DIRECT);
14827           size_needed = desired_align - align + 1;
14828           if (tmp != count_exp)
14829             emit_move_insn (count_exp, tmp);
14830         }
14831       emit_label (label);
14832       LABEL_NUSES (label) = 1;
14833     }
14834   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14835     {
14836       if (force_loopy_epilogue)
14837         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
14838                                          size_needed);
14839       else
14840         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
14841                                 size_needed);
14842     }
14843   if (jump_around_label)
14844     emit_label (jump_around_label);
14845   return 1;
14846 }
14847
14848 /* Expand the appropriate insns for doing strlen if not just doing
14849    repnz; scasb
14850
14851    out = result, initialized with the start address
14852    align_rtx = alignment of the address.
14853    scratch = scratch register, initialized with the startaddress when
14854         not aligned, otherwise undefined
14855
14856    This is just the body. It needs the initializations mentioned above and
14857    some address computing at the end.  These things are done in i386.md.  */
14858
14859 static void
14860 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
14861 {
14862   int align;
14863   rtx tmp;
14864   rtx align_2_label = NULL_RTX;
14865   rtx align_3_label = NULL_RTX;
14866   rtx align_4_label = gen_label_rtx ();
14867   rtx end_0_label = gen_label_rtx ();
14868   rtx mem;
14869   rtx tmpreg = gen_reg_rtx (SImode);
14870   rtx scratch = gen_reg_rtx (SImode);
14871   rtx cmp;
14872
14873   align = 0;
14874   if (CONST_INT_P (align_rtx))
14875     align = INTVAL (align_rtx);
14876
14877   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
14878
14879   /* Is there a known alignment and is it less than 4?  */
14880   if (align < 4)
14881     {
14882       rtx scratch1 = gen_reg_rtx (Pmode);
14883       emit_move_insn (scratch1, out);
14884       /* Is there a known alignment and is it not 2? */
14885       if (align != 2)
14886         {
14887           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
14888           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
14889
14890           /* Leave just the 3 lower bits.  */
14891           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
14892                                     NULL_RTX, 0, OPTAB_WIDEN);
14893
14894           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14895                                    Pmode, 1, align_4_label);
14896           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
14897                                    Pmode, 1, align_2_label);
14898           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
14899                                    Pmode, 1, align_3_label);
14900         }
14901       else
14902         {
14903           /* Since the alignment is 2, we have to check 2 or 0 bytes;
14904              check if is aligned to 4 - byte.  */
14905
14906           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
14907                                     NULL_RTX, 0, OPTAB_WIDEN);
14908
14909           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14910                                    Pmode, 1, align_4_label);
14911         }
14912
14913       mem = change_address (src, QImode, out);
14914
14915       /* Now compare the bytes.  */
14916
14917       /* Compare the first n unaligned byte on a byte per byte basis.  */
14918       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
14919                                QImode, 1, end_0_label);
14920
14921       /* Increment the address.  */
14922       if (TARGET_64BIT)
14923         emit_insn (gen_adddi3 (out, out, const1_rtx));
14924       else
14925         emit_insn (gen_addsi3 (out, out, const1_rtx));
14926
14927       /* Not needed with an alignment of 2 */
14928       if (align != 2)
14929         {
14930           emit_label (align_2_label);
14931
14932           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14933                                    end_0_label);
14934
14935           if (TARGET_64BIT)
14936             emit_insn (gen_adddi3 (out, out, const1_rtx));
14937           else
14938             emit_insn (gen_addsi3 (out, out, const1_rtx));
14939
14940           emit_label (align_3_label);
14941         }
14942
14943       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14944                                end_0_label);
14945
14946       if (TARGET_64BIT)
14947         emit_insn (gen_adddi3 (out, out, const1_rtx));
14948       else
14949         emit_insn (gen_addsi3 (out, out, const1_rtx));
14950     }
14951
14952   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
14953      align this loop.  It gives only huge programs, but does not help to
14954      speed up.  */
14955   emit_label (align_4_label);
14956
14957   mem = change_address (src, SImode, out);
14958   emit_move_insn (scratch, mem);
14959   if (TARGET_64BIT)
14960     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
14961   else
14962     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
14963
14964   /* This formula yields a nonzero result iff one of the bytes is zero.
14965      This saves three branches inside loop and many cycles.  */
14966
14967   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
14968   emit_insn (gen_one_cmplsi2 (scratch, scratch));
14969   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
14970   emit_insn (gen_andsi3 (tmpreg, tmpreg,
14971                          gen_int_mode (0x80808080, SImode)));
14972   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
14973                            align_4_label);
14974
14975   if (TARGET_CMOVE)
14976     {
14977        rtx reg = gen_reg_rtx (SImode);
14978        rtx reg2 = gen_reg_rtx (Pmode);
14979        emit_move_insn (reg, tmpreg);
14980        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
14981
14982        /* If zero is not in the first two bytes, move two bytes forward.  */
14983        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14984        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14985        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14986        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
14987                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
14988                                                      reg,
14989                                                      tmpreg)));
14990        /* Emit lea manually to avoid clobbering of flags.  */
14991        emit_insn (gen_rtx_SET (SImode, reg2,
14992                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
14993
14994        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14995        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14996        emit_insn (gen_rtx_SET (VOIDmode, out,
14997                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
14998                                                      reg2,
14999                                                      out)));
15000
15001     }
15002   else
15003     {
15004        rtx end_2_label = gen_label_rtx ();
15005        /* Is zero in the first two bytes? */
15006
15007        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
15008        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
15009        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
15010        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
15011                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
15012                             pc_rtx);
15013        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
15014        JUMP_LABEL (tmp) = end_2_label;
15015
15016        /* Not in the first two.  Move two bytes forward.  */
15017        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
15018        if (TARGET_64BIT)
15019          emit_insn (gen_adddi3 (out, out, const2_rtx));
15020        else
15021          emit_insn (gen_addsi3 (out, out, const2_rtx));
15022
15023        emit_label (end_2_label);
15024
15025     }
15026
15027   /* Avoid branch in fixing the byte.  */
15028   tmpreg = gen_lowpart (QImode, tmpreg);
15029   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
15030   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
15031   if (TARGET_64BIT)
15032     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
15033   else
15034     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
15035
15036   emit_label (end_0_label);
15037 }
15038
15039 /* Expand strlen.  */
15040
15041 int
15042 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
15043 {
15044   rtx addr, scratch1, scratch2, scratch3, scratch4;
15045
15046   /* The generic case of strlen expander is long.  Avoid it's
15047      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
15048
15049   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
15050       && !TARGET_INLINE_ALL_STRINGOPS
15051       && !optimize_size
15052       && (!CONST_INT_P (align) || INTVAL (align) < 4))
15053     return 0;
15054
15055   addr = force_reg (Pmode, XEXP (src, 0));
15056   scratch1 = gen_reg_rtx (Pmode);
15057
15058   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
15059       && !optimize_size)
15060     {
15061       /* Well it seems that some optimizer does not combine a call like
15062          foo(strlen(bar), strlen(bar));
15063          when the move and the subtraction is done here.  It does calculate
15064          the length just once when these instructions are done inside of
15065          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
15066          often used and I use one fewer register for the lifetime of
15067          output_strlen_unroll() this is better.  */
15068
15069       emit_move_insn (out, addr);
15070
15071       ix86_expand_strlensi_unroll_1 (out, src, align);
15072
15073       /* strlensi_unroll_1 returns the address of the zero at the end of
15074          the string, like memchr(), so compute the length by subtracting
15075          the start address.  */
15076       if (TARGET_64BIT)
15077         emit_insn (gen_subdi3 (out, out, addr));
15078       else
15079         emit_insn (gen_subsi3 (out, out, addr));
15080     }
15081   else
15082     {
15083       rtx unspec;
15084       scratch2 = gen_reg_rtx (Pmode);
15085       scratch3 = gen_reg_rtx (Pmode);
15086       scratch4 = force_reg (Pmode, constm1_rtx);
15087
15088       emit_move_insn (scratch3, addr);
15089       eoschar = force_reg (QImode, eoschar);
15090
15091       src = replace_equiv_address_nv (src, scratch3);
15092
15093       /* If .md starts supporting :P, this can be done in .md.  */
15094       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
15095                                                  scratch4), UNSPEC_SCAS);
15096       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
15097       if (TARGET_64BIT)
15098         {
15099           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
15100           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
15101         }
15102       else
15103         {
15104           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
15105           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
15106         }
15107     }
15108   return 1;
15109 }
15110
15111 /* For given symbol (function) construct code to compute address of it's PLT
15112    entry in large x86-64 PIC model.  */
15113 rtx
15114 construct_plt_address (rtx symbol)
15115 {
15116   rtx tmp = gen_reg_rtx (Pmode);
15117   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
15118
15119   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15120   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
15121
15122   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
15123   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
15124   return tmp;
15125 }
15126
15127 void
15128 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
15129                   rtx callarg2 ATTRIBUTE_UNUSED,
15130                   rtx pop, int sibcall)
15131 {
15132   rtx use = NULL, call;
15133
15134   if (pop == const0_rtx)
15135     pop = NULL;
15136   gcc_assert (!TARGET_64BIT || !pop);
15137
15138   if (TARGET_MACHO && !TARGET_64BIT)
15139     {
15140 #if TARGET_MACHO
15141       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
15142         fnaddr = machopic_indirect_call_target (fnaddr);
15143 #endif
15144     }
15145   else
15146     {
15147       /* Static functions and indirect calls don't need the pic register.  */
15148       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
15149           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
15150           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
15151         use_reg (&use, pic_offset_table_rtx);
15152     }
15153
15154   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
15155     {
15156       rtx al = gen_rtx_REG (QImode, 0);
15157       emit_move_insn (al, callarg2);
15158       use_reg (&use, al);
15159     }
15160
15161   if (ix86_cmodel == CM_LARGE_PIC
15162       && GET_CODE (fnaddr) == MEM
15163       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
15164       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
15165     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
15166   else if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
15167     {
15168       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
15169       fnaddr = gen_rtx_MEM (QImode, fnaddr);
15170     }
15171   if (sibcall && TARGET_64BIT
15172       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
15173     {
15174       rtx addr;
15175       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
15176       fnaddr = gen_rtx_REG (Pmode, R11_REG);
15177       emit_move_insn (fnaddr, addr);
15178       fnaddr = gen_rtx_MEM (QImode, fnaddr);
15179     }
15180
15181   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
15182   if (retval)
15183     call = gen_rtx_SET (VOIDmode, retval, call);
15184   if (pop)
15185     {
15186       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
15187       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
15188       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
15189     }
15190
15191   call = emit_call_insn (call);
15192   if (use)
15193     CALL_INSN_FUNCTION_USAGE (call) = use;
15194 }
15195
15196 \f
15197 /* Clear stack slot assignments remembered from previous functions.
15198    This is called from INIT_EXPANDERS once before RTL is emitted for each
15199    function.  */
15200
15201 static struct machine_function *
15202 ix86_init_machine_status (void)
15203 {
15204   struct machine_function *f;
15205
15206   f = ggc_alloc_cleared (sizeof (struct machine_function));
15207   f->use_fast_prologue_epilogue_nregs = -1;
15208   f->tls_descriptor_call_expanded_p = 0;
15209
15210   return f;
15211 }
15212
15213 /* Return a MEM corresponding to a stack slot with mode MODE.
15214    Allocate a new slot if necessary.
15215
15216    The RTL for a function can have several slots available: N is
15217    which slot to use.  */
15218
15219 rtx
15220 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
15221 {
15222   struct stack_local_entry *s;
15223
15224   gcc_assert (n < MAX_386_STACK_LOCALS);
15225
15226   for (s = ix86_stack_locals; s; s = s->next)
15227     if (s->mode == mode && s->n == n)
15228       return copy_rtx (s->rtl);
15229
15230   s = (struct stack_local_entry *)
15231     ggc_alloc (sizeof (struct stack_local_entry));
15232   s->n = n;
15233   s->mode = mode;
15234   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
15235
15236   s->next = ix86_stack_locals;
15237   ix86_stack_locals = s;
15238   return s->rtl;
15239 }
15240
15241 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
15242
15243 static GTY(()) rtx ix86_tls_symbol;
15244 rtx
15245 ix86_tls_get_addr (void)
15246 {
15247
15248   if (!ix86_tls_symbol)
15249     {
15250       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
15251                                             (TARGET_ANY_GNU_TLS
15252                                              && !TARGET_64BIT)
15253                                             ? "___tls_get_addr"
15254                                             : "__tls_get_addr");
15255     }
15256
15257   return ix86_tls_symbol;
15258 }
15259
15260 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
15261
15262 static GTY(()) rtx ix86_tls_module_base_symbol;
15263 rtx
15264 ix86_tls_module_base (void)
15265 {
15266
15267   if (!ix86_tls_module_base_symbol)
15268     {
15269       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
15270                                                         "_TLS_MODULE_BASE_");
15271       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
15272         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
15273     }
15274
15275   return ix86_tls_module_base_symbol;
15276 }
15277 \f
15278 /* Calculate the length of the memory address in the instruction
15279    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
15280
15281 int
15282 memory_address_length (rtx addr)
15283 {
15284   struct ix86_address parts;
15285   rtx base, index, disp;
15286   int len;
15287   int ok;
15288
15289   if (GET_CODE (addr) == PRE_DEC
15290       || GET_CODE (addr) == POST_INC
15291       || GET_CODE (addr) == PRE_MODIFY
15292       || GET_CODE (addr) == POST_MODIFY)
15293     return 0;
15294
15295   ok = ix86_decompose_address (addr, &parts);
15296   gcc_assert (ok);
15297
15298   if (parts.base && GET_CODE (parts.base) == SUBREG)
15299     parts.base = SUBREG_REG (parts.base);
15300   if (parts.index && GET_CODE (parts.index) == SUBREG)
15301     parts.index = SUBREG_REG (parts.index);
15302
15303   base = parts.base;
15304   index = parts.index;
15305   disp = parts.disp;
15306   len = 0;
15307
15308   /* Rule of thumb:
15309        - esp as the base always wants an index,
15310        - ebp as the base always wants a displacement.  */
15311
15312   /* Register Indirect.  */
15313   if (base && !index && !disp)
15314     {
15315       /* esp (for its index) and ebp (for its displacement) need
15316          the two-byte modrm form.  */
15317       if (addr == stack_pointer_rtx
15318           || addr == arg_pointer_rtx
15319           || addr == frame_pointer_rtx
15320           || addr == hard_frame_pointer_rtx)
15321         len = 1;
15322     }
15323
15324   /* Direct Addressing.  */
15325   else if (disp && !base && !index)
15326     len = 4;
15327
15328   else
15329     {
15330       /* Find the length of the displacement constant.  */
15331       if (disp)
15332         {
15333           if (base && satisfies_constraint_K (disp))
15334             len = 1;
15335           else
15336             len = 4;
15337         }
15338       /* ebp always wants a displacement.  */
15339       else if (base == hard_frame_pointer_rtx)
15340         len = 1;
15341
15342       /* An index requires the two-byte modrm form....  */
15343       if (index
15344           /* ...like esp, which always wants an index.  */
15345           || base == stack_pointer_rtx
15346           || base == arg_pointer_rtx
15347           || base == frame_pointer_rtx)
15348         len += 1;
15349     }
15350
15351   return len;
15352 }
15353
15354 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
15355    is set, expect that insn have 8bit immediate alternative.  */
15356 int
15357 ix86_attr_length_immediate_default (rtx insn, int shortform)
15358 {
15359   int len = 0;
15360   int i;
15361   extract_insn_cached (insn);
15362   for (i = recog_data.n_operands - 1; i >= 0; --i)
15363     if (CONSTANT_P (recog_data.operand[i]))
15364       {
15365         gcc_assert (!len);
15366         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
15367           len = 1;
15368         else
15369           {
15370             switch (get_attr_mode (insn))
15371               {
15372                 case MODE_QI:
15373                   len+=1;
15374                   break;
15375                 case MODE_HI:
15376                   len+=2;
15377                   break;
15378                 case MODE_SI:
15379                   len+=4;
15380                   break;
15381                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
15382                 case MODE_DI:
15383                   len+=4;
15384                   break;
15385                 default:
15386                   fatal_insn ("unknown insn mode", insn);
15387               }
15388           }
15389       }
15390   return len;
15391 }
15392 /* Compute default value for "length_address" attribute.  */
15393 int
15394 ix86_attr_length_address_default (rtx insn)
15395 {
15396   int i;
15397
15398   if (get_attr_type (insn) == TYPE_LEA)
15399     {
15400       rtx set = PATTERN (insn);
15401
15402       if (GET_CODE (set) == PARALLEL)
15403         set = XVECEXP (set, 0, 0);
15404
15405       gcc_assert (GET_CODE (set) == SET);
15406
15407       return memory_address_length (SET_SRC (set));
15408     }
15409
15410   extract_insn_cached (insn);
15411   for (i = recog_data.n_operands - 1; i >= 0; --i)
15412     if (MEM_P (recog_data.operand[i]))
15413       {
15414         return memory_address_length (XEXP (recog_data.operand[i], 0));
15415         break;
15416       }
15417   return 0;
15418 }
15419 \f
15420 /* Return the maximum number of instructions a cpu can issue.  */
15421
15422 static int
15423 ix86_issue_rate (void)
15424 {
15425   switch (ix86_tune)
15426     {
15427     case PROCESSOR_PENTIUM:
15428     case PROCESSOR_K6:
15429       return 2;
15430
15431     case PROCESSOR_PENTIUMPRO:
15432     case PROCESSOR_PENTIUM4:
15433     case PROCESSOR_ATHLON:
15434     case PROCESSOR_K8:
15435     case PROCESSOR_AMDFAM10:
15436     case PROCESSOR_NOCONA:
15437     case PROCESSOR_GENERIC32:
15438     case PROCESSOR_GENERIC64:
15439       return 3;
15440
15441     case PROCESSOR_CORE2:
15442       return 4;
15443
15444     default:
15445       return 1;
15446     }
15447 }
15448
15449 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
15450    by DEP_INSN and nothing set by DEP_INSN.  */
15451
15452 static int
15453 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15454 {
15455   rtx set, set2;
15456
15457   /* Simplify the test for uninteresting insns.  */
15458   if (insn_type != TYPE_SETCC
15459       && insn_type != TYPE_ICMOV
15460       && insn_type != TYPE_FCMOV
15461       && insn_type != TYPE_IBR)
15462     return 0;
15463
15464   if ((set = single_set (dep_insn)) != 0)
15465     {
15466       set = SET_DEST (set);
15467       set2 = NULL_RTX;
15468     }
15469   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
15470            && XVECLEN (PATTERN (dep_insn), 0) == 2
15471            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
15472            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
15473     {
15474       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15475       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15476     }
15477   else
15478     return 0;
15479
15480   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
15481     return 0;
15482
15483   /* This test is true if the dependent insn reads the flags but
15484      not any other potentially set register.  */
15485   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
15486     return 0;
15487
15488   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
15489     return 0;
15490
15491   return 1;
15492 }
15493
15494 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
15495    address with operands set by DEP_INSN.  */
15496
15497 static int
15498 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15499 {
15500   rtx addr;
15501
15502   if (insn_type == TYPE_LEA
15503       && TARGET_PENTIUM)
15504     {
15505       addr = PATTERN (insn);
15506
15507       if (GET_CODE (addr) == PARALLEL)
15508         addr = XVECEXP (addr, 0, 0);
15509
15510       gcc_assert (GET_CODE (addr) == SET);
15511
15512       addr = SET_SRC (addr);
15513     }
15514   else
15515     {
15516       int i;
15517       extract_insn_cached (insn);
15518       for (i = recog_data.n_operands - 1; i >= 0; --i)
15519         if (MEM_P (recog_data.operand[i]))
15520           {
15521             addr = XEXP (recog_data.operand[i], 0);
15522             goto found;
15523           }
15524       return 0;
15525     found:;
15526     }
15527
15528   return modified_in_p (addr, dep_insn);
15529 }
15530
15531 static int
15532 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
15533 {
15534   enum attr_type insn_type, dep_insn_type;
15535   enum attr_memory memory;
15536   rtx set, set2;
15537   int dep_insn_code_number;
15538
15539   /* Anti and output dependencies have zero cost on all CPUs.  */
15540   if (REG_NOTE_KIND (link) != 0)
15541     return 0;
15542
15543   dep_insn_code_number = recog_memoized (dep_insn);
15544
15545   /* If we can't recognize the insns, we can't really do anything.  */
15546   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
15547     return cost;
15548
15549   insn_type = get_attr_type (insn);
15550   dep_insn_type = get_attr_type (dep_insn);
15551
15552   switch (ix86_tune)
15553     {
15554     case PROCESSOR_PENTIUM:
15555       /* Address Generation Interlock adds a cycle of latency.  */
15556       if (ix86_agi_dependent (insn, dep_insn, insn_type))
15557         cost += 1;
15558
15559       /* ??? Compares pair with jump/setcc.  */
15560       if (ix86_flags_dependent (insn, dep_insn, insn_type))
15561         cost = 0;
15562
15563       /* Floating point stores require value to be ready one cycle earlier.  */
15564       if (insn_type == TYPE_FMOV
15565           && get_attr_memory (insn) == MEMORY_STORE
15566           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15567         cost += 1;
15568       break;
15569
15570     case PROCESSOR_PENTIUMPRO:
15571       memory = get_attr_memory (insn);
15572
15573       /* INT->FP conversion is expensive.  */
15574       if (get_attr_fp_int_src (dep_insn))
15575         cost += 5;
15576
15577       /* There is one cycle extra latency between an FP op and a store.  */
15578       if (insn_type == TYPE_FMOV
15579           && (set = single_set (dep_insn)) != NULL_RTX
15580           && (set2 = single_set (insn)) != NULL_RTX
15581           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
15582           && MEM_P (SET_DEST (set2)))
15583         cost += 1;
15584
15585       /* Show ability of reorder buffer to hide latency of load by executing
15586          in parallel with previous instruction in case
15587          previous instruction is not needed to compute the address.  */
15588       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15589           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15590         {
15591           /* Claim moves to take one cycle, as core can issue one load
15592              at time and the next load can start cycle later.  */
15593           if (dep_insn_type == TYPE_IMOV
15594               || dep_insn_type == TYPE_FMOV)
15595             cost = 1;
15596           else if (cost > 1)
15597             cost--;
15598         }
15599       break;
15600
15601     case PROCESSOR_K6:
15602       memory = get_attr_memory (insn);
15603
15604       /* The esp dependency is resolved before the instruction is really
15605          finished.  */
15606       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
15607           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
15608         return 1;
15609
15610       /* INT->FP conversion is expensive.  */
15611       if (get_attr_fp_int_src (dep_insn))
15612         cost += 5;
15613
15614       /* Show ability of reorder buffer to hide latency of load by executing
15615          in parallel with previous instruction in case
15616          previous instruction is not needed to compute the address.  */
15617       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15618           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15619         {
15620           /* Claim moves to take one cycle, as core can issue one load
15621              at time and the next load can start cycle later.  */
15622           if (dep_insn_type == TYPE_IMOV
15623               || dep_insn_type == TYPE_FMOV)
15624             cost = 1;
15625           else if (cost > 2)
15626             cost -= 2;
15627           else
15628             cost = 1;
15629         }
15630       break;
15631
15632     case PROCESSOR_ATHLON:
15633     case PROCESSOR_K8:
15634     case PROCESSOR_AMDFAM10:
15635     case PROCESSOR_GENERIC32:
15636     case PROCESSOR_GENERIC64:
15637       memory = get_attr_memory (insn);
15638
15639       /* Show ability of reorder buffer to hide latency of load by executing
15640          in parallel with previous instruction in case
15641          previous instruction is not needed to compute the address.  */
15642       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15643           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15644         {
15645           enum attr_unit unit = get_attr_unit (insn);
15646           int loadcost = 3;
15647
15648           /* Because of the difference between the length of integer and
15649              floating unit pipeline preparation stages, the memory operands
15650              for floating point are cheaper.
15651
15652              ??? For Athlon it the difference is most probably 2.  */
15653           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
15654             loadcost = 3;
15655           else
15656             loadcost = TARGET_ATHLON ? 2 : 0;
15657
15658           if (cost >= loadcost)
15659             cost -= loadcost;
15660           else
15661             cost = 0;
15662         }
15663
15664     default:
15665       break;
15666     }
15667
15668   return cost;
15669 }
15670
15671 /* How many alternative schedules to try.  This should be as wide as the
15672    scheduling freedom in the DFA, but no wider.  Making this value too
15673    large results extra work for the scheduler.  */
15674
15675 static int
15676 ia32_multipass_dfa_lookahead (void)
15677 {
15678   if (ix86_tune == PROCESSOR_PENTIUM)
15679     return 2;
15680
15681   if (ix86_tune == PROCESSOR_PENTIUMPRO
15682       || ix86_tune == PROCESSOR_K6)
15683     return 1;
15684
15685   else
15686     return 0;
15687 }
15688
15689 \f
15690 /* Compute the alignment given to a constant that is being placed in memory.
15691    EXP is the constant and ALIGN is the alignment that the object would
15692    ordinarily have.
15693    The value of this function is used instead of that alignment to align
15694    the object.  */
15695
15696 int
15697 ix86_constant_alignment (tree exp, int align)
15698 {
15699   if (TREE_CODE (exp) == REAL_CST)
15700     {
15701       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
15702         return 64;
15703       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
15704         return 128;
15705     }
15706   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
15707            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
15708     return BITS_PER_WORD;
15709
15710   return align;
15711 }
15712
15713 /* Compute the alignment for a static variable.
15714    TYPE is the data type, and ALIGN is the alignment that
15715    the object would ordinarily have.  The value of this function is used
15716    instead of that alignment to align the object.  */
15717
15718 int
15719 ix86_data_alignment (tree type, int align)
15720 {
15721   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
15722
15723   if (AGGREGATE_TYPE_P (type)
15724       && TYPE_SIZE (type)
15725       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15726       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
15727           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
15728       && align < max_align)
15729     align = max_align;
15730
15731   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15732      to 16byte boundary.  */
15733   if (TARGET_64BIT)
15734     {
15735       if (AGGREGATE_TYPE_P (type)
15736            && TYPE_SIZE (type)
15737            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15738            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
15739                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15740         return 128;
15741     }
15742
15743   if (TREE_CODE (type) == ARRAY_TYPE)
15744     {
15745       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15746         return 64;
15747       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15748         return 128;
15749     }
15750   else if (TREE_CODE (type) == COMPLEX_TYPE)
15751     {
15752
15753       if (TYPE_MODE (type) == DCmode && align < 64)
15754         return 64;
15755       if (TYPE_MODE (type) == XCmode && align < 128)
15756         return 128;
15757     }
15758   else if ((TREE_CODE (type) == RECORD_TYPE
15759             || TREE_CODE (type) == UNION_TYPE
15760             || TREE_CODE (type) == QUAL_UNION_TYPE)
15761            && TYPE_FIELDS (type))
15762     {
15763       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15764         return 64;
15765       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15766         return 128;
15767     }
15768   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15769            || TREE_CODE (type) == INTEGER_TYPE)
15770     {
15771       if (TYPE_MODE (type) == DFmode && align < 64)
15772         return 64;
15773       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15774         return 128;
15775     }
15776
15777   return align;
15778 }
15779
15780 /* Compute the alignment for a local variable.
15781    TYPE is the data type, and ALIGN is the alignment that
15782    the object would ordinarily have.  The value of this macro is used
15783    instead of that alignment to align the object.  */
15784
15785 int
15786 ix86_local_alignment (tree type, int align)
15787 {
15788   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15789      to 16byte boundary.  */
15790   if (TARGET_64BIT)
15791     {
15792       if (AGGREGATE_TYPE_P (type)
15793            && TYPE_SIZE (type)
15794            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15795            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
15796                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15797         return 128;
15798     }
15799   if (TREE_CODE (type) == ARRAY_TYPE)
15800     {
15801       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15802         return 64;
15803       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15804         return 128;
15805     }
15806   else if (TREE_CODE (type) == COMPLEX_TYPE)
15807     {
15808       if (TYPE_MODE (type) == DCmode && align < 64)
15809         return 64;
15810       if (TYPE_MODE (type) == XCmode && align < 128)
15811         return 128;
15812     }
15813   else if ((TREE_CODE (type) == RECORD_TYPE
15814             || TREE_CODE (type) == UNION_TYPE
15815             || TREE_CODE (type) == QUAL_UNION_TYPE)
15816            && TYPE_FIELDS (type))
15817     {
15818       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15819         return 64;
15820       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15821         return 128;
15822     }
15823   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15824            || TREE_CODE (type) == INTEGER_TYPE)
15825     {
15826
15827       if (TYPE_MODE (type) == DFmode && align < 64)
15828         return 64;
15829       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15830         return 128;
15831     }
15832   return align;
15833 }
15834 \f
15835 /* Emit RTL insns to initialize the variable parts of a trampoline.
15836    FNADDR is an RTX for the address of the function's pure code.
15837    CXT is an RTX for the static chain value for the function.  */
15838 void
15839 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
15840 {
15841   if (!TARGET_64BIT)
15842     {
15843       /* Compute offset from the end of the jmp to the target function.  */
15844       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
15845                                plus_constant (tramp, 10),
15846                                NULL_RTX, 1, OPTAB_DIRECT);
15847       emit_move_insn (gen_rtx_MEM (QImode, tramp),
15848                       gen_int_mode (0xb9, QImode));
15849       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
15850       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
15851                       gen_int_mode (0xe9, QImode));
15852       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
15853     }
15854   else
15855     {
15856       int offset = 0;
15857       /* Try to load address using shorter movl instead of movabs.
15858          We may want to support movq for kernel mode, but kernel does not use
15859          trampolines at the moment.  */
15860       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
15861         {
15862           fnaddr = copy_to_mode_reg (DImode, fnaddr);
15863           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15864                           gen_int_mode (0xbb41, HImode));
15865           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
15866                           gen_lowpart (SImode, fnaddr));
15867           offset += 6;
15868         }
15869       else
15870         {
15871           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15872                           gen_int_mode (0xbb49, HImode));
15873           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15874                           fnaddr);
15875           offset += 10;
15876         }
15877       /* Load static chain using movabs to r10.  */
15878       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15879                       gen_int_mode (0xba49, HImode));
15880       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15881                       cxt);
15882       offset += 10;
15883       /* Jump to the r11 */
15884       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15885                       gen_int_mode (0xff49, HImode));
15886       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
15887                       gen_int_mode (0xe3, QImode));
15888       offset += 3;
15889       gcc_assert (offset <= TRAMPOLINE_SIZE);
15890     }
15891
15892 #ifdef ENABLE_EXECUTE_STACK
15893   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
15894                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
15895 #endif
15896 }
15897 \f
15898 /* Codes for all the SSE/MMX builtins.  */
15899 enum ix86_builtins
15900 {
15901   IX86_BUILTIN_ADDPS,
15902   IX86_BUILTIN_ADDSS,
15903   IX86_BUILTIN_DIVPS,
15904   IX86_BUILTIN_DIVSS,
15905   IX86_BUILTIN_MULPS,
15906   IX86_BUILTIN_MULSS,
15907   IX86_BUILTIN_SUBPS,
15908   IX86_BUILTIN_SUBSS,
15909
15910   IX86_BUILTIN_CMPEQPS,
15911   IX86_BUILTIN_CMPLTPS,
15912   IX86_BUILTIN_CMPLEPS,
15913   IX86_BUILTIN_CMPGTPS,
15914   IX86_BUILTIN_CMPGEPS,
15915   IX86_BUILTIN_CMPNEQPS,
15916   IX86_BUILTIN_CMPNLTPS,
15917   IX86_BUILTIN_CMPNLEPS,
15918   IX86_BUILTIN_CMPNGTPS,
15919   IX86_BUILTIN_CMPNGEPS,
15920   IX86_BUILTIN_CMPORDPS,
15921   IX86_BUILTIN_CMPUNORDPS,
15922   IX86_BUILTIN_CMPEQSS,
15923   IX86_BUILTIN_CMPLTSS,
15924   IX86_BUILTIN_CMPLESS,
15925   IX86_BUILTIN_CMPNEQSS,
15926   IX86_BUILTIN_CMPNLTSS,
15927   IX86_BUILTIN_CMPNLESS,
15928   IX86_BUILTIN_CMPNGTSS,
15929   IX86_BUILTIN_CMPNGESS,
15930   IX86_BUILTIN_CMPORDSS,
15931   IX86_BUILTIN_CMPUNORDSS,
15932
15933   IX86_BUILTIN_COMIEQSS,
15934   IX86_BUILTIN_COMILTSS,
15935   IX86_BUILTIN_COMILESS,
15936   IX86_BUILTIN_COMIGTSS,
15937   IX86_BUILTIN_COMIGESS,
15938   IX86_BUILTIN_COMINEQSS,
15939   IX86_BUILTIN_UCOMIEQSS,
15940   IX86_BUILTIN_UCOMILTSS,
15941   IX86_BUILTIN_UCOMILESS,
15942   IX86_BUILTIN_UCOMIGTSS,
15943   IX86_BUILTIN_UCOMIGESS,
15944   IX86_BUILTIN_UCOMINEQSS,
15945
15946   IX86_BUILTIN_CVTPI2PS,
15947   IX86_BUILTIN_CVTPS2PI,
15948   IX86_BUILTIN_CVTSI2SS,
15949   IX86_BUILTIN_CVTSI642SS,
15950   IX86_BUILTIN_CVTSS2SI,
15951   IX86_BUILTIN_CVTSS2SI64,
15952   IX86_BUILTIN_CVTTPS2PI,
15953   IX86_BUILTIN_CVTTSS2SI,
15954   IX86_BUILTIN_CVTTSS2SI64,
15955
15956   IX86_BUILTIN_MAXPS,
15957   IX86_BUILTIN_MAXSS,
15958   IX86_BUILTIN_MINPS,
15959   IX86_BUILTIN_MINSS,
15960
15961   IX86_BUILTIN_LOADUPS,
15962   IX86_BUILTIN_STOREUPS,
15963   IX86_BUILTIN_MOVSS,
15964
15965   IX86_BUILTIN_MOVHLPS,
15966   IX86_BUILTIN_MOVLHPS,
15967   IX86_BUILTIN_LOADHPS,
15968   IX86_BUILTIN_LOADLPS,
15969   IX86_BUILTIN_STOREHPS,
15970   IX86_BUILTIN_STORELPS,
15971
15972   IX86_BUILTIN_MASKMOVQ,
15973   IX86_BUILTIN_MOVMSKPS,
15974   IX86_BUILTIN_PMOVMSKB,
15975
15976   IX86_BUILTIN_MOVNTPS,
15977   IX86_BUILTIN_MOVNTQ,
15978
15979   IX86_BUILTIN_LOADDQU,
15980   IX86_BUILTIN_STOREDQU,
15981
15982   IX86_BUILTIN_PACKSSWB,
15983   IX86_BUILTIN_PACKSSDW,
15984   IX86_BUILTIN_PACKUSWB,
15985
15986   IX86_BUILTIN_PADDB,
15987   IX86_BUILTIN_PADDW,
15988   IX86_BUILTIN_PADDD,
15989   IX86_BUILTIN_PADDQ,
15990   IX86_BUILTIN_PADDSB,
15991   IX86_BUILTIN_PADDSW,
15992   IX86_BUILTIN_PADDUSB,
15993   IX86_BUILTIN_PADDUSW,
15994   IX86_BUILTIN_PSUBB,
15995   IX86_BUILTIN_PSUBW,
15996   IX86_BUILTIN_PSUBD,
15997   IX86_BUILTIN_PSUBQ,
15998   IX86_BUILTIN_PSUBSB,
15999   IX86_BUILTIN_PSUBSW,
16000   IX86_BUILTIN_PSUBUSB,
16001   IX86_BUILTIN_PSUBUSW,
16002
16003   IX86_BUILTIN_PAND,
16004   IX86_BUILTIN_PANDN,
16005   IX86_BUILTIN_POR,
16006   IX86_BUILTIN_PXOR,
16007
16008   IX86_BUILTIN_PAVGB,
16009   IX86_BUILTIN_PAVGW,
16010
16011   IX86_BUILTIN_PCMPEQB,
16012   IX86_BUILTIN_PCMPEQW,
16013   IX86_BUILTIN_PCMPEQD,
16014   IX86_BUILTIN_PCMPGTB,
16015   IX86_BUILTIN_PCMPGTW,
16016   IX86_BUILTIN_PCMPGTD,
16017
16018   IX86_BUILTIN_PMADDWD,
16019
16020   IX86_BUILTIN_PMAXSW,
16021   IX86_BUILTIN_PMAXUB,
16022   IX86_BUILTIN_PMINSW,
16023   IX86_BUILTIN_PMINUB,
16024
16025   IX86_BUILTIN_PMULHUW,
16026   IX86_BUILTIN_PMULHW,
16027   IX86_BUILTIN_PMULLW,
16028
16029   IX86_BUILTIN_PSADBW,
16030   IX86_BUILTIN_PSHUFW,
16031
16032   IX86_BUILTIN_PSLLW,
16033   IX86_BUILTIN_PSLLD,
16034   IX86_BUILTIN_PSLLQ,
16035   IX86_BUILTIN_PSRAW,
16036   IX86_BUILTIN_PSRAD,
16037   IX86_BUILTIN_PSRLW,
16038   IX86_BUILTIN_PSRLD,
16039   IX86_BUILTIN_PSRLQ,
16040   IX86_BUILTIN_PSLLWI,
16041   IX86_BUILTIN_PSLLDI,
16042   IX86_BUILTIN_PSLLQI,
16043   IX86_BUILTIN_PSRAWI,
16044   IX86_BUILTIN_PSRADI,
16045   IX86_BUILTIN_PSRLWI,
16046   IX86_BUILTIN_PSRLDI,
16047   IX86_BUILTIN_PSRLQI,
16048
16049   IX86_BUILTIN_PUNPCKHBW,
16050   IX86_BUILTIN_PUNPCKHWD,
16051   IX86_BUILTIN_PUNPCKHDQ,
16052   IX86_BUILTIN_PUNPCKLBW,
16053   IX86_BUILTIN_PUNPCKLWD,
16054   IX86_BUILTIN_PUNPCKLDQ,
16055
16056   IX86_BUILTIN_SHUFPS,
16057
16058   IX86_BUILTIN_RCPPS,
16059   IX86_BUILTIN_RCPSS,
16060   IX86_BUILTIN_RSQRTPS,
16061   IX86_BUILTIN_RSQRTSS,
16062   IX86_BUILTIN_SQRTPS,
16063   IX86_BUILTIN_SQRTSS,
16064
16065   IX86_BUILTIN_UNPCKHPS,
16066   IX86_BUILTIN_UNPCKLPS,
16067
16068   IX86_BUILTIN_ANDPS,
16069   IX86_BUILTIN_ANDNPS,
16070   IX86_BUILTIN_ORPS,
16071   IX86_BUILTIN_XORPS,
16072
16073   IX86_BUILTIN_EMMS,
16074   IX86_BUILTIN_LDMXCSR,
16075   IX86_BUILTIN_STMXCSR,
16076   IX86_BUILTIN_SFENCE,
16077
16078   /* 3DNow! Original */
16079   IX86_BUILTIN_FEMMS,
16080   IX86_BUILTIN_PAVGUSB,
16081   IX86_BUILTIN_PF2ID,
16082   IX86_BUILTIN_PFACC,
16083   IX86_BUILTIN_PFADD,
16084   IX86_BUILTIN_PFCMPEQ,
16085   IX86_BUILTIN_PFCMPGE,
16086   IX86_BUILTIN_PFCMPGT,
16087   IX86_BUILTIN_PFMAX,
16088   IX86_BUILTIN_PFMIN,
16089   IX86_BUILTIN_PFMUL,
16090   IX86_BUILTIN_PFRCP,
16091   IX86_BUILTIN_PFRCPIT1,
16092   IX86_BUILTIN_PFRCPIT2,
16093   IX86_BUILTIN_PFRSQIT1,
16094   IX86_BUILTIN_PFRSQRT,
16095   IX86_BUILTIN_PFSUB,
16096   IX86_BUILTIN_PFSUBR,
16097   IX86_BUILTIN_PI2FD,
16098   IX86_BUILTIN_PMULHRW,
16099
16100   /* 3DNow! Athlon Extensions */
16101   IX86_BUILTIN_PF2IW,
16102   IX86_BUILTIN_PFNACC,
16103   IX86_BUILTIN_PFPNACC,
16104   IX86_BUILTIN_PI2FW,
16105   IX86_BUILTIN_PSWAPDSI,
16106   IX86_BUILTIN_PSWAPDSF,
16107
16108   /* SSE2 */
16109   IX86_BUILTIN_ADDPD,
16110   IX86_BUILTIN_ADDSD,
16111   IX86_BUILTIN_DIVPD,
16112   IX86_BUILTIN_DIVSD,
16113   IX86_BUILTIN_MULPD,
16114   IX86_BUILTIN_MULSD,
16115   IX86_BUILTIN_SUBPD,
16116   IX86_BUILTIN_SUBSD,
16117
16118   IX86_BUILTIN_CMPEQPD,
16119   IX86_BUILTIN_CMPLTPD,
16120   IX86_BUILTIN_CMPLEPD,
16121   IX86_BUILTIN_CMPGTPD,
16122   IX86_BUILTIN_CMPGEPD,
16123   IX86_BUILTIN_CMPNEQPD,
16124   IX86_BUILTIN_CMPNLTPD,
16125   IX86_BUILTIN_CMPNLEPD,
16126   IX86_BUILTIN_CMPNGTPD,
16127   IX86_BUILTIN_CMPNGEPD,
16128   IX86_BUILTIN_CMPORDPD,
16129   IX86_BUILTIN_CMPUNORDPD,
16130   IX86_BUILTIN_CMPEQSD,
16131   IX86_BUILTIN_CMPLTSD,
16132   IX86_BUILTIN_CMPLESD,
16133   IX86_BUILTIN_CMPNEQSD,
16134   IX86_BUILTIN_CMPNLTSD,
16135   IX86_BUILTIN_CMPNLESD,
16136   IX86_BUILTIN_CMPORDSD,
16137   IX86_BUILTIN_CMPUNORDSD,
16138
16139   IX86_BUILTIN_COMIEQSD,
16140   IX86_BUILTIN_COMILTSD,
16141   IX86_BUILTIN_COMILESD,
16142   IX86_BUILTIN_COMIGTSD,
16143   IX86_BUILTIN_COMIGESD,
16144   IX86_BUILTIN_COMINEQSD,
16145   IX86_BUILTIN_UCOMIEQSD,
16146   IX86_BUILTIN_UCOMILTSD,
16147   IX86_BUILTIN_UCOMILESD,
16148   IX86_BUILTIN_UCOMIGTSD,
16149   IX86_BUILTIN_UCOMIGESD,
16150   IX86_BUILTIN_UCOMINEQSD,
16151
16152   IX86_BUILTIN_MAXPD,
16153   IX86_BUILTIN_MAXSD,
16154   IX86_BUILTIN_MINPD,
16155   IX86_BUILTIN_MINSD,
16156
16157   IX86_BUILTIN_ANDPD,
16158   IX86_BUILTIN_ANDNPD,
16159   IX86_BUILTIN_ORPD,
16160   IX86_BUILTIN_XORPD,
16161
16162   IX86_BUILTIN_SQRTPD,
16163   IX86_BUILTIN_SQRTSD,
16164
16165   IX86_BUILTIN_UNPCKHPD,
16166   IX86_BUILTIN_UNPCKLPD,
16167
16168   IX86_BUILTIN_SHUFPD,
16169
16170   IX86_BUILTIN_LOADUPD,
16171   IX86_BUILTIN_STOREUPD,
16172   IX86_BUILTIN_MOVSD,
16173
16174   IX86_BUILTIN_LOADHPD,
16175   IX86_BUILTIN_LOADLPD,
16176
16177   IX86_BUILTIN_CVTDQ2PD,
16178   IX86_BUILTIN_CVTDQ2PS,
16179
16180   IX86_BUILTIN_CVTPD2DQ,
16181   IX86_BUILTIN_CVTPD2PI,
16182   IX86_BUILTIN_CVTPD2PS,
16183   IX86_BUILTIN_CVTTPD2DQ,
16184   IX86_BUILTIN_CVTTPD2PI,
16185
16186   IX86_BUILTIN_CVTPI2PD,
16187   IX86_BUILTIN_CVTSI2SD,
16188   IX86_BUILTIN_CVTSI642SD,
16189
16190   IX86_BUILTIN_CVTSD2SI,
16191   IX86_BUILTIN_CVTSD2SI64,
16192   IX86_BUILTIN_CVTSD2SS,
16193   IX86_BUILTIN_CVTSS2SD,
16194   IX86_BUILTIN_CVTTSD2SI,
16195   IX86_BUILTIN_CVTTSD2SI64,
16196
16197   IX86_BUILTIN_CVTPS2DQ,
16198   IX86_BUILTIN_CVTPS2PD,
16199   IX86_BUILTIN_CVTTPS2DQ,
16200
16201   IX86_BUILTIN_MOVNTI,
16202   IX86_BUILTIN_MOVNTPD,
16203   IX86_BUILTIN_MOVNTDQ,
16204
16205   /* SSE2 MMX */
16206   IX86_BUILTIN_MASKMOVDQU,
16207   IX86_BUILTIN_MOVMSKPD,
16208   IX86_BUILTIN_PMOVMSKB128,
16209
16210   IX86_BUILTIN_PACKSSWB128,
16211   IX86_BUILTIN_PACKSSDW128,
16212   IX86_BUILTIN_PACKUSWB128,
16213
16214   IX86_BUILTIN_PADDB128,
16215   IX86_BUILTIN_PADDW128,
16216   IX86_BUILTIN_PADDD128,
16217   IX86_BUILTIN_PADDQ128,
16218   IX86_BUILTIN_PADDSB128,
16219   IX86_BUILTIN_PADDSW128,
16220   IX86_BUILTIN_PADDUSB128,
16221   IX86_BUILTIN_PADDUSW128,
16222   IX86_BUILTIN_PSUBB128,
16223   IX86_BUILTIN_PSUBW128,
16224   IX86_BUILTIN_PSUBD128,
16225   IX86_BUILTIN_PSUBQ128,
16226   IX86_BUILTIN_PSUBSB128,
16227   IX86_BUILTIN_PSUBSW128,
16228   IX86_BUILTIN_PSUBUSB128,
16229   IX86_BUILTIN_PSUBUSW128,
16230
16231   IX86_BUILTIN_PAND128,
16232   IX86_BUILTIN_PANDN128,
16233   IX86_BUILTIN_POR128,
16234   IX86_BUILTIN_PXOR128,
16235
16236   IX86_BUILTIN_PAVGB128,
16237   IX86_BUILTIN_PAVGW128,
16238
16239   IX86_BUILTIN_PCMPEQB128,
16240   IX86_BUILTIN_PCMPEQW128,
16241   IX86_BUILTIN_PCMPEQD128,
16242   IX86_BUILTIN_PCMPGTB128,
16243   IX86_BUILTIN_PCMPGTW128,
16244   IX86_BUILTIN_PCMPGTD128,
16245
16246   IX86_BUILTIN_PMADDWD128,
16247
16248   IX86_BUILTIN_PMAXSW128,
16249   IX86_BUILTIN_PMAXUB128,
16250   IX86_BUILTIN_PMINSW128,
16251   IX86_BUILTIN_PMINUB128,
16252
16253   IX86_BUILTIN_PMULUDQ,
16254   IX86_BUILTIN_PMULUDQ128,
16255   IX86_BUILTIN_PMULHUW128,
16256   IX86_BUILTIN_PMULHW128,
16257   IX86_BUILTIN_PMULLW128,
16258
16259   IX86_BUILTIN_PSADBW128,
16260   IX86_BUILTIN_PSHUFHW,
16261   IX86_BUILTIN_PSHUFLW,
16262   IX86_BUILTIN_PSHUFD,
16263
16264   IX86_BUILTIN_PSLLDQI128,
16265   IX86_BUILTIN_PSLLWI128,
16266   IX86_BUILTIN_PSLLDI128,
16267   IX86_BUILTIN_PSLLQI128,
16268   IX86_BUILTIN_PSRAWI128,
16269   IX86_BUILTIN_PSRADI128,
16270   IX86_BUILTIN_PSRLDQI128,
16271   IX86_BUILTIN_PSRLWI128,
16272   IX86_BUILTIN_PSRLDI128,
16273   IX86_BUILTIN_PSRLQI128,
16274
16275   IX86_BUILTIN_PSLLDQ128,
16276   IX86_BUILTIN_PSLLW128,
16277   IX86_BUILTIN_PSLLD128,
16278   IX86_BUILTIN_PSLLQ128,
16279   IX86_BUILTIN_PSRAW128,
16280   IX86_BUILTIN_PSRAD128,
16281   IX86_BUILTIN_PSRLW128,
16282   IX86_BUILTIN_PSRLD128,
16283   IX86_BUILTIN_PSRLQ128,
16284
16285   IX86_BUILTIN_PUNPCKHBW128,
16286   IX86_BUILTIN_PUNPCKHWD128,
16287   IX86_BUILTIN_PUNPCKHDQ128,
16288   IX86_BUILTIN_PUNPCKHQDQ128,
16289   IX86_BUILTIN_PUNPCKLBW128,
16290   IX86_BUILTIN_PUNPCKLWD128,
16291   IX86_BUILTIN_PUNPCKLDQ128,
16292   IX86_BUILTIN_PUNPCKLQDQ128,
16293
16294   IX86_BUILTIN_CLFLUSH,
16295   IX86_BUILTIN_MFENCE,
16296   IX86_BUILTIN_LFENCE,
16297
16298   /* Prescott New Instructions.  */
16299   IX86_BUILTIN_ADDSUBPS,
16300   IX86_BUILTIN_HADDPS,
16301   IX86_BUILTIN_HSUBPS,
16302   IX86_BUILTIN_MOVSHDUP,
16303   IX86_BUILTIN_MOVSLDUP,
16304   IX86_BUILTIN_ADDSUBPD,
16305   IX86_BUILTIN_HADDPD,
16306   IX86_BUILTIN_HSUBPD,
16307   IX86_BUILTIN_LDDQU,
16308
16309   IX86_BUILTIN_MONITOR,
16310   IX86_BUILTIN_MWAIT,
16311
16312   /* SSSE3.  */
16313   IX86_BUILTIN_PHADDW,
16314   IX86_BUILTIN_PHADDD,
16315   IX86_BUILTIN_PHADDSW,
16316   IX86_BUILTIN_PHSUBW,
16317   IX86_BUILTIN_PHSUBD,
16318   IX86_BUILTIN_PHSUBSW,
16319   IX86_BUILTIN_PMADDUBSW,
16320   IX86_BUILTIN_PMULHRSW,
16321   IX86_BUILTIN_PSHUFB,
16322   IX86_BUILTIN_PSIGNB,
16323   IX86_BUILTIN_PSIGNW,
16324   IX86_BUILTIN_PSIGND,
16325   IX86_BUILTIN_PALIGNR,
16326   IX86_BUILTIN_PABSB,
16327   IX86_BUILTIN_PABSW,
16328   IX86_BUILTIN_PABSD,
16329
16330   IX86_BUILTIN_PHADDW128,
16331   IX86_BUILTIN_PHADDD128,
16332   IX86_BUILTIN_PHADDSW128,
16333   IX86_BUILTIN_PHSUBW128,
16334   IX86_BUILTIN_PHSUBD128,
16335   IX86_BUILTIN_PHSUBSW128,
16336   IX86_BUILTIN_PMADDUBSW128,
16337   IX86_BUILTIN_PMULHRSW128,
16338   IX86_BUILTIN_PSHUFB128,
16339   IX86_BUILTIN_PSIGNB128,
16340   IX86_BUILTIN_PSIGNW128,
16341   IX86_BUILTIN_PSIGND128,
16342   IX86_BUILTIN_PALIGNR128,
16343   IX86_BUILTIN_PABSB128,
16344   IX86_BUILTIN_PABSW128,
16345   IX86_BUILTIN_PABSD128,
16346
16347   /* AMDFAM10 - SSE4A New Instructions.  */
16348   IX86_BUILTIN_MOVNTSD,
16349   IX86_BUILTIN_MOVNTSS,
16350   IX86_BUILTIN_EXTRQI,
16351   IX86_BUILTIN_EXTRQ,
16352   IX86_BUILTIN_INSERTQI,
16353   IX86_BUILTIN_INSERTQ,
16354
16355   IX86_BUILTIN_VEC_INIT_V2SI,
16356   IX86_BUILTIN_VEC_INIT_V4HI,
16357   IX86_BUILTIN_VEC_INIT_V8QI,
16358   IX86_BUILTIN_VEC_EXT_V2DF,
16359   IX86_BUILTIN_VEC_EXT_V2DI,
16360   IX86_BUILTIN_VEC_EXT_V4SF,
16361   IX86_BUILTIN_VEC_EXT_V4SI,
16362   IX86_BUILTIN_VEC_EXT_V8HI,
16363   IX86_BUILTIN_VEC_EXT_V2SI,
16364   IX86_BUILTIN_VEC_EXT_V4HI,
16365   IX86_BUILTIN_VEC_SET_V8HI,
16366   IX86_BUILTIN_VEC_SET_V4HI,
16367
16368   IX86_BUILTIN_MAX
16369 };
16370
16371 /* Table for the ix86 builtin decls.  */
16372 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
16373
16374 /* Add a ix86 target builtin function with CODE, NAME and TYPE.  Do so,
16375  * if the target_flags include one of MASK.  Stores the function decl
16376  * in the ix86_builtins array.
16377  * Returns the function decl or NULL_TREE, if the builtin was not added.  */
16378
16379 static inline tree
16380 def_builtin (int mask, const char *name, tree type, enum ix86_builtins code)
16381 {
16382   tree decl = NULL_TREE;
16383
16384   if (mask & target_flags
16385       && (!(mask & MASK_64BIT) || TARGET_64BIT))
16386     {
16387       decl = add_builtin_function (name, type, code, BUILT_IN_MD,
16388                                    NULL, NULL_TREE);
16389       ix86_builtins[(int) code] = decl;
16390     }
16391
16392   return decl;
16393 }
16394
16395 /* Like def_builtin, but also marks the function decl "const".  */
16396
16397 static inline tree
16398 def_builtin_const (int mask, const char *name, tree type,
16399                    enum ix86_builtins code)
16400 {
16401   tree decl = def_builtin (mask, name, type, code);
16402   if (decl)
16403     TREE_READONLY (decl) = 1;
16404   return decl;
16405 }
16406
16407 /* Bits for builtin_description.flag.  */
16408
16409 /* Set when we don't support the comparison natively, and should
16410    swap_comparison in order to support it.  */
16411 #define BUILTIN_DESC_SWAP_OPERANDS      1
16412
16413 struct builtin_description
16414 {
16415   const unsigned int mask;
16416   const enum insn_code icode;
16417   const char *const name;
16418   const enum ix86_builtins code;
16419   const enum rtx_code comparison;
16420   const unsigned int flag;
16421 };
16422
16423 static const struct builtin_description bdesc_comi[] =
16424 {
16425   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
16426   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
16427   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
16428   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
16429   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
16430   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
16431   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
16432   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
16433   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
16434   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
16435   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
16436   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
16437   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
16438   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
16439   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
16440   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
16441   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
16442   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
16443   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
16444   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
16445   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
16446   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
16447   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
16448   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
16449 };
16450
16451 static const struct builtin_description bdesc_2arg[] =
16452 {
16453   /* SSE */
16454   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
16455   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
16456   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
16457   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
16458   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
16459   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
16460   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
16461   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
16462
16463   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
16464   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
16465   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
16466   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
16467     BUILTIN_DESC_SWAP_OPERANDS },
16468   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
16469     BUILTIN_DESC_SWAP_OPERANDS },
16470   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
16471   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
16472   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
16473   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
16474   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
16475     BUILTIN_DESC_SWAP_OPERANDS },
16476   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
16477     BUILTIN_DESC_SWAP_OPERANDS },
16478   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
16479   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
16480   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
16481   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
16482   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
16483   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
16484   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
16485   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
16486   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
16487     BUILTIN_DESC_SWAP_OPERANDS },
16488   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
16489     BUILTIN_DESC_SWAP_OPERANDS },
16490   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
16491
16492   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
16493   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
16494   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
16495   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
16496
16497   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
16498   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
16499   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
16500   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
16501
16502   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
16503   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
16504   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
16505   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
16506   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
16507
16508   /* MMX */
16509   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
16510   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
16511   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
16512   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
16513   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
16514   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
16515   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
16516   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
16517
16518   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
16519   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
16520   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
16521   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
16522   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
16523   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
16524   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
16525   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
16526
16527   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
16528   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
16529   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
16530
16531   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
16532   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
16533   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
16534   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
16535
16536   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
16537   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
16538
16539   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
16540   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
16541   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
16542   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
16543   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
16544   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
16545
16546   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
16547   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
16548   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
16549   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
16550
16551   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
16552   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
16553   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
16554   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
16555   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
16556   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
16557
16558   /* Special.  */
16559   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
16560   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
16561   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
16562
16563   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
16564   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
16565   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
16566
16567   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
16568   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
16569   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
16570   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
16571   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
16572   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
16573
16574   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
16575   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
16576   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
16577   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
16578   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
16579   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
16580
16581   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
16582   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
16583   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
16584   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
16585
16586   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
16587   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
16588
16589   /* SSE2 */
16590   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
16591   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
16592   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
16593   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
16594   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
16595   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
16596   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
16597   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
16598
16599   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
16600   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
16601   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
16602   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
16603     BUILTIN_DESC_SWAP_OPERANDS },
16604   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
16605     BUILTIN_DESC_SWAP_OPERANDS },
16606   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
16607   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
16608   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
16609   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
16610   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
16611     BUILTIN_DESC_SWAP_OPERANDS },
16612   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
16613     BUILTIN_DESC_SWAP_OPERANDS },
16614   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
16615   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
16616   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
16617   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
16618   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
16619   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
16620   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
16621   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
16622   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
16623
16624   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
16625   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
16626   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
16627   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
16628
16629   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
16630   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
16631   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
16632   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
16633
16634   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
16635   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
16636   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
16637
16638   /* SSE2 MMX */
16639   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
16640   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
16641   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
16642   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
16643   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
16644   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
16645   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
16646   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
16647
16648   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
16649   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
16650   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
16651   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
16652   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
16653   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
16654   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
16655   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
16656
16657   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
16658   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
16659
16660   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
16661   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
16662   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
16663   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
16664
16665   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
16666   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
16667
16668   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
16669   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
16670   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
16671   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
16672   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
16673   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
16674
16675   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
16676   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
16677   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
16678   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
16679
16680   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
16681   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
16682   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
16683   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
16684   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
16685   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
16686   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
16687   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
16688
16689   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
16690   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
16691   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
16692
16693   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
16694   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
16695
16696   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
16697   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
16698
16699   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
16700   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
16701   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
16702
16703   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
16704   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
16705   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
16706
16707   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
16708   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
16709
16710   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
16711
16712   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
16713   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
16714   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
16715   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
16716
16717   /* SSE3 MMX */
16718   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
16719   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
16720   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
16721   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
16722   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
16723   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 },
16724
16725   /* SSSE3 */
16726   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, 0, 0 },
16727   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, 0, 0 },
16728   { MASK_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, 0, 0 },
16729   { MASK_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, 0, 0 },
16730   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, 0, 0 },
16731   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, 0, 0 },
16732   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, 0, 0 },
16733   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, 0, 0 },
16734   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, 0, 0 },
16735   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, 0, 0 },
16736   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, 0, 0 },
16737   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, 0, 0 },
16738   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, 0, 0 },
16739   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, 0, 0 },
16740   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, 0, 0 },
16741   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, 0, 0 },
16742   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, 0, 0 },
16743   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, 0, 0 },
16744   { MASK_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, 0, 0 },
16745   { MASK_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, 0, 0 },
16746   { MASK_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, 0, 0 },
16747   { MASK_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, 0, 0 },
16748   { MASK_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, 0, 0 },
16749   { MASK_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, 0, 0 }
16750 };
16751
16752 static const struct builtin_description bdesc_1arg[] =
16753 {
16754   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
16755   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
16756
16757   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
16758   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
16759   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
16760
16761   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
16762   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
16763   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
16764   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
16765   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
16766   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
16767
16768   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
16769   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
16770
16771   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
16772
16773   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
16774   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
16775
16776   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
16777   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
16778   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
16779   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
16780   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
16781
16782   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
16783
16784   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
16785   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
16786   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
16787   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
16788
16789   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
16790   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
16791   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
16792
16793   /* SSE3 */
16794   { MASK_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, 0, 0 },
16795   { MASK_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, 0, 0 },
16796
16797   /* SSSE3 */
16798   { MASK_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, 0, 0 },
16799   { MASK_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, 0, 0 },
16800   { MASK_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, 0, 0 },
16801   { MASK_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, 0, 0 },
16802   { MASK_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, 0, 0 },
16803   { MASK_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, 0, 0 },
16804 };
16805
16806 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
16807    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
16808    builtins.  */
16809 static void
16810 ix86_init_mmx_sse_builtins (void)
16811 {
16812   const struct builtin_description * d;
16813   size_t i;
16814
16815   tree V16QI_type_node = build_vector_type_for_mode (char_type_node, V16QImode);
16816   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
16817   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
16818   tree V2DI_type_node
16819     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
16820   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
16821   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
16822   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
16823   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
16824   tree V8QI_type_node = build_vector_type_for_mode (char_type_node, V8QImode);
16825   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
16826
16827   tree pchar_type_node = build_pointer_type (char_type_node);
16828   tree pcchar_type_node = build_pointer_type (
16829                              build_type_variant (char_type_node, 1, 0));
16830   tree pfloat_type_node = build_pointer_type (float_type_node);
16831   tree pcfloat_type_node = build_pointer_type (
16832                              build_type_variant (float_type_node, 1, 0));
16833   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
16834   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
16835   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
16836
16837   /* Comparisons.  */
16838   tree int_ftype_v4sf_v4sf
16839     = build_function_type_list (integer_type_node,
16840                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16841   tree v4si_ftype_v4sf_v4sf
16842     = build_function_type_list (V4SI_type_node,
16843                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16844   /* MMX/SSE/integer conversions.  */
16845   tree int_ftype_v4sf
16846     = build_function_type_list (integer_type_node,
16847                                 V4SF_type_node, NULL_TREE);
16848   tree int64_ftype_v4sf
16849     = build_function_type_list (long_long_integer_type_node,
16850                                 V4SF_type_node, NULL_TREE);
16851   tree int_ftype_v8qi
16852     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
16853   tree v4sf_ftype_v4sf_int
16854     = build_function_type_list (V4SF_type_node,
16855                                 V4SF_type_node, integer_type_node, NULL_TREE);
16856   tree v4sf_ftype_v4sf_int64
16857     = build_function_type_list (V4SF_type_node,
16858                                 V4SF_type_node, long_long_integer_type_node,
16859                                 NULL_TREE);
16860   tree v4sf_ftype_v4sf_v2si
16861     = build_function_type_list (V4SF_type_node,
16862                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
16863
16864   /* Miscellaneous.  */
16865   tree v8qi_ftype_v4hi_v4hi
16866     = build_function_type_list (V8QI_type_node,
16867                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16868   tree v4hi_ftype_v2si_v2si
16869     = build_function_type_list (V4HI_type_node,
16870                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16871   tree v4sf_ftype_v4sf_v4sf_int
16872     = build_function_type_list (V4SF_type_node,
16873                                 V4SF_type_node, V4SF_type_node,
16874                                 integer_type_node, NULL_TREE);
16875   tree v2si_ftype_v4hi_v4hi
16876     = build_function_type_list (V2SI_type_node,
16877                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16878   tree v4hi_ftype_v4hi_int
16879     = build_function_type_list (V4HI_type_node,
16880                                 V4HI_type_node, integer_type_node, NULL_TREE);
16881   tree v4hi_ftype_v4hi_di
16882     = build_function_type_list (V4HI_type_node,
16883                                 V4HI_type_node, long_long_unsigned_type_node,
16884                                 NULL_TREE);
16885   tree v2si_ftype_v2si_di
16886     = build_function_type_list (V2SI_type_node,
16887                                 V2SI_type_node, long_long_unsigned_type_node,
16888                                 NULL_TREE);
16889   tree void_ftype_void
16890     = build_function_type (void_type_node, void_list_node);
16891   tree void_ftype_unsigned
16892     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
16893   tree void_ftype_unsigned_unsigned
16894     = build_function_type_list (void_type_node, unsigned_type_node,
16895                                 unsigned_type_node, NULL_TREE);
16896   tree void_ftype_pcvoid_unsigned_unsigned
16897     = build_function_type_list (void_type_node, const_ptr_type_node,
16898                                 unsigned_type_node, unsigned_type_node,
16899                                 NULL_TREE);
16900   tree unsigned_ftype_void
16901     = build_function_type (unsigned_type_node, void_list_node);
16902   tree v2si_ftype_v4sf
16903     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
16904   /* Loads/stores.  */
16905   tree void_ftype_v8qi_v8qi_pchar
16906     = build_function_type_list (void_type_node,
16907                                 V8QI_type_node, V8QI_type_node,
16908                                 pchar_type_node, NULL_TREE);
16909   tree v4sf_ftype_pcfloat
16910     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
16911   /* @@@ the type is bogus */
16912   tree v4sf_ftype_v4sf_pv2si
16913     = build_function_type_list (V4SF_type_node,
16914                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
16915   tree void_ftype_pv2si_v4sf
16916     = build_function_type_list (void_type_node,
16917                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
16918   tree void_ftype_pfloat_v4sf
16919     = build_function_type_list (void_type_node,
16920                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
16921   tree void_ftype_pdi_di
16922     = build_function_type_list (void_type_node,
16923                                 pdi_type_node, long_long_unsigned_type_node,
16924                                 NULL_TREE);
16925   tree void_ftype_pv2di_v2di
16926     = build_function_type_list (void_type_node,
16927                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
16928   /* Normal vector unops.  */
16929   tree v4sf_ftype_v4sf
16930     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
16931   tree v16qi_ftype_v16qi
16932     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
16933   tree v8hi_ftype_v8hi
16934     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
16935   tree v4si_ftype_v4si
16936     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
16937   tree v8qi_ftype_v8qi
16938     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
16939   tree v4hi_ftype_v4hi
16940     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
16941
16942   /* Normal vector binops.  */
16943   tree v4sf_ftype_v4sf_v4sf
16944     = build_function_type_list (V4SF_type_node,
16945                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16946   tree v8qi_ftype_v8qi_v8qi
16947     = build_function_type_list (V8QI_type_node,
16948                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16949   tree v4hi_ftype_v4hi_v4hi
16950     = build_function_type_list (V4HI_type_node,
16951                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16952   tree v2si_ftype_v2si_v2si
16953     = build_function_type_list (V2SI_type_node,
16954                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16955   tree di_ftype_di_di
16956     = build_function_type_list (long_long_unsigned_type_node,
16957                                 long_long_unsigned_type_node,
16958                                 long_long_unsigned_type_node, NULL_TREE);
16959
16960   tree di_ftype_di_di_int
16961     = build_function_type_list (long_long_unsigned_type_node,
16962                                 long_long_unsigned_type_node,
16963                                 long_long_unsigned_type_node,
16964                                 integer_type_node, NULL_TREE);
16965
16966   tree v2si_ftype_v2sf
16967     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
16968   tree v2sf_ftype_v2si
16969     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
16970   tree v2si_ftype_v2si
16971     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
16972   tree v2sf_ftype_v2sf
16973     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
16974   tree v2sf_ftype_v2sf_v2sf
16975     = build_function_type_list (V2SF_type_node,
16976                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16977   tree v2si_ftype_v2sf_v2sf
16978     = build_function_type_list (V2SI_type_node,
16979                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16980   tree pint_type_node    = build_pointer_type (integer_type_node);
16981   tree pdouble_type_node = build_pointer_type (double_type_node);
16982   tree pcdouble_type_node = build_pointer_type (
16983                                 build_type_variant (double_type_node, 1, 0));
16984   tree int_ftype_v2df_v2df
16985     = build_function_type_list (integer_type_node,
16986                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16987
16988   tree void_ftype_pcvoid
16989     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
16990   tree v4sf_ftype_v4si
16991     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
16992   tree v4si_ftype_v4sf
16993     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
16994   tree v2df_ftype_v4si
16995     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
16996   tree v4si_ftype_v2df
16997     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
16998   tree v2si_ftype_v2df
16999     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
17000   tree v4sf_ftype_v2df
17001     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
17002   tree v2df_ftype_v2si
17003     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
17004   tree v2df_ftype_v4sf
17005     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
17006   tree int_ftype_v2df
17007     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
17008   tree int64_ftype_v2df
17009     = build_function_type_list (long_long_integer_type_node,
17010                                 V2DF_type_node, NULL_TREE);
17011   tree v2df_ftype_v2df_int
17012     = build_function_type_list (V2DF_type_node,
17013                                 V2DF_type_node, integer_type_node, NULL_TREE);
17014   tree v2df_ftype_v2df_int64
17015     = build_function_type_list (V2DF_type_node,
17016                                 V2DF_type_node, long_long_integer_type_node,
17017                                 NULL_TREE);
17018   tree v4sf_ftype_v4sf_v2df
17019     = build_function_type_list (V4SF_type_node,
17020                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
17021   tree v2df_ftype_v2df_v4sf
17022     = build_function_type_list (V2DF_type_node,
17023                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
17024   tree v2df_ftype_v2df_v2df_int
17025     = build_function_type_list (V2DF_type_node,
17026                                 V2DF_type_node, V2DF_type_node,
17027                                 integer_type_node,
17028                                 NULL_TREE);
17029   tree v2df_ftype_v2df_pcdouble
17030     = build_function_type_list (V2DF_type_node,
17031                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
17032   tree void_ftype_pdouble_v2df
17033     = build_function_type_list (void_type_node,
17034                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
17035   tree void_ftype_pint_int
17036     = build_function_type_list (void_type_node,
17037                                 pint_type_node, integer_type_node, NULL_TREE);
17038   tree void_ftype_v16qi_v16qi_pchar
17039     = build_function_type_list (void_type_node,
17040                                 V16QI_type_node, V16QI_type_node,
17041                                 pchar_type_node, NULL_TREE);
17042   tree v2df_ftype_pcdouble
17043     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
17044   tree v2df_ftype_v2df_v2df
17045     = build_function_type_list (V2DF_type_node,
17046                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
17047   tree v16qi_ftype_v16qi_v16qi
17048     = build_function_type_list (V16QI_type_node,
17049                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
17050   tree v8hi_ftype_v8hi_v8hi
17051     = build_function_type_list (V8HI_type_node,
17052                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
17053   tree v4si_ftype_v4si_v4si
17054     = build_function_type_list (V4SI_type_node,
17055                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
17056   tree v2di_ftype_v2di_v2di
17057     = build_function_type_list (V2DI_type_node,
17058                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
17059   tree v2di_ftype_v2df_v2df
17060     = build_function_type_list (V2DI_type_node,
17061                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
17062   tree v2df_ftype_v2df
17063     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
17064   tree v2di_ftype_v2di_int
17065     = build_function_type_list (V2DI_type_node,
17066                                 V2DI_type_node, integer_type_node, NULL_TREE);
17067   tree v2di_ftype_v2di_v2di_int
17068     = build_function_type_list (V2DI_type_node, V2DI_type_node,
17069                                 V2DI_type_node, integer_type_node, NULL_TREE);
17070   tree v4si_ftype_v4si_int
17071     = build_function_type_list (V4SI_type_node,
17072                                 V4SI_type_node, integer_type_node, NULL_TREE);
17073   tree v8hi_ftype_v8hi_int
17074     = build_function_type_list (V8HI_type_node,
17075                                 V8HI_type_node, integer_type_node, NULL_TREE);
17076   tree v4si_ftype_v8hi_v8hi
17077     = build_function_type_list (V4SI_type_node,
17078                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
17079   tree di_ftype_v8qi_v8qi
17080     = build_function_type_list (long_long_unsigned_type_node,
17081                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
17082   tree di_ftype_v2si_v2si
17083     = build_function_type_list (long_long_unsigned_type_node,
17084                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
17085   tree v2di_ftype_v16qi_v16qi
17086     = build_function_type_list (V2DI_type_node,
17087                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
17088   tree v2di_ftype_v4si_v4si
17089     = build_function_type_list (V2DI_type_node,
17090                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
17091   tree int_ftype_v16qi
17092     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
17093   tree v16qi_ftype_pcchar
17094     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
17095   tree void_ftype_pchar_v16qi
17096     = build_function_type_list (void_type_node,
17097                                 pchar_type_node, V16QI_type_node, NULL_TREE);
17098
17099   tree v2di_ftype_v2di_unsigned_unsigned
17100     = build_function_type_list (V2DI_type_node, V2DI_type_node,
17101                                 unsigned_type_node, unsigned_type_node,
17102                                 NULL_TREE);
17103   tree v2di_ftype_v2di_v2di_unsigned_unsigned
17104     = build_function_type_list (V2DI_type_node, V2DI_type_node, V2DI_type_node,
17105                                 unsigned_type_node, unsigned_type_node,
17106                                 NULL_TREE);
17107   tree v2di_ftype_v2di_v16qi
17108     = build_function_type_list (V2DI_type_node, V2DI_type_node, V16QI_type_node,
17109                                 NULL_TREE);
17110
17111   tree float80_type;
17112   tree float128_type;
17113   tree ftype;
17114
17115   /* The __float80 type.  */
17116   if (TYPE_MODE (long_double_type_node) == XFmode)
17117     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
17118                                                "__float80");
17119   else
17120     {
17121       /* The __float80 type.  */
17122       float80_type = make_node (REAL_TYPE);
17123       TYPE_PRECISION (float80_type) = 80;
17124       layout_type (float80_type);
17125       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
17126     }
17127
17128   if (TARGET_64BIT)
17129     {
17130       float128_type = make_node (REAL_TYPE);
17131       TYPE_PRECISION (float128_type) = 128;
17132       layout_type (float128_type);
17133       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
17134     }
17135
17136   /* Add all builtins that are more or less simple operations on two
17137      operands.  */
17138   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17139     {
17140       /* Use one of the operands; the target can have a different mode for
17141          mask-generating compares.  */
17142       enum machine_mode mode;
17143       tree type;
17144
17145       if (d->name == 0)
17146         continue;
17147       mode = insn_data[d->icode].operand[1].mode;
17148
17149       switch (mode)
17150         {
17151         case V16QImode:
17152           type = v16qi_ftype_v16qi_v16qi;
17153           break;
17154         case V8HImode:
17155           type = v8hi_ftype_v8hi_v8hi;
17156           break;
17157         case V4SImode:
17158           type = v4si_ftype_v4si_v4si;
17159           break;
17160         case V2DImode:
17161           type = v2di_ftype_v2di_v2di;
17162           break;
17163         case V2DFmode:
17164           type = v2df_ftype_v2df_v2df;
17165           break;
17166         case V4SFmode:
17167           type = v4sf_ftype_v4sf_v4sf;
17168           break;
17169         case V8QImode:
17170           type = v8qi_ftype_v8qi_v8qi;
17171           break;
17172         case V4HImode:
17173           type = v4hi_ftype_v4hi_v4hi;
17174           break;
17175         case V2SImode:
17176           type = v2si_ftype_v2si_v2si;
17177           break;
17178         case DImode:
17179           type = di_ftype_di_di;
17180           break;
17181
17182         default:
17183           gcc_unreachable ();
17184         }
17185
17186       /* Override for comparisons.  */
17187       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
17188           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
17189         type = v4si_ftype_v4sf_v4sf;
17190
17191       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
17192           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
17193         type = v2di_ftype_v2df_v2df;
17194
17195       def_builtin (d->mask, d->name, type, d->code);
17196     }
17197
17198   /* Add all builtins that are more or less simple operations on 1 operand.  */
17199   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17200     {
17201       enum machine_mode mode;
17202       tree type;
17203
17204       if (d->name == 0)
17205         continue;
17206       mode = insn_data[d->icode].operand[1].mode;
17207
17208       switch (mode)
17209         {
17210         case V16QImode:
17211           type = v16qi_ftype_v16qi;
17212           break;
17213         case V8HImode:
17214           type = v8hi_ftype_v8hi;
17215           break;
17216         case V4SImode:
17217           type = v4si_ftype_v4si;
17218           break;
17219         case V2DFmode:
17220           type = v2df_ftype_v2df;
17221           break;
17222         case V4SFmode:
17223           type = v4sf_ftype_v4sf;
17224           break;
17225         case V8QImode:
17226           type = v8qi_ftype_v8qi;
17227           break;
17228         case V4HImode:
17229           type = v4hi_ftype_v4hi;
17230           break;
17231         case V2SImode:
17232           type = v2si_ftype_v2si;
17233           break;
17234
17235         default:
17236           abort ();
17237         }
17238
17239       def_builtin (d->mask, d->name, type, d->code);
17240     }
17241
17242   /* Add the remaining MMX insns with somewhat more complicated types.  */
17243   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
17244   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
17245   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
17246   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
17247
17248   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
17249   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
17250   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
17251
17252   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
17253   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
17254
17255   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
17256   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
17257
17258   /* comi/ucomi insns.  */
17259   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
17260     if (d->mask == MASK_SSE2)
17261       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
17262     else
17263       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
17264
17265   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
17266   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
17267   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
17268
17269   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
17270   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
17271   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
17272   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
17273   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
17274   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
17275   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
17276   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
17277   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
17278   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
17279   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
17280
17281   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
17282
17283   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
17284   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
17285
17286   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
17287   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
17288   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
17289   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
17290
17291   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
17292   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
17293   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
17294   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
17295
17296   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
17297
17298   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
17299
17300   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
17301   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
17302   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
17303   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
17304   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
17305   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
17306
17307   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
17308
17309   /* Original 3DNow!  */
17310   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
17311   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
17312   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
17313   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
17314   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
17315   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
17316   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
17317   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
17318   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
17319   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
17320   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
17321   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
17322   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
17323   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
17324   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
17325   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
17326   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
17327   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
17328   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
17329   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
17330
17331   /* 3DNow! extension as used in the Athlon CPU.  */
17332   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
17333   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
17334   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
17335   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
17336   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
17337   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
17338
17339   /* SSE2 */
17340   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
17341
17342   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
17343   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
17344
17345   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
17346   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
17347
17348   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
17349   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
17350   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
17351   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
17352   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
17353
17354   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
17355   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
17356   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
17357   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
17358
17359   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
17360   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
17361
17362   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
17363
17364   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
17365   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
17366
17367   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
17368   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
17369   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
17370   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
17371   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
17372
17373   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
17374
17375   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
17376   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
17377   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
17378   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
17379
17380   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
17381   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
17382   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
17383
17384   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
17385   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
17386   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
17387   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
17388
17389   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
17390   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
17391   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
17392
17393   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
17394   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
17395
17396   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
17397   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
17398
17399   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
17400   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
17401   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
17402   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
17403   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSLLW128);
17404   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSLLD128);
17405   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
17406
17407   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
17408   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
17409   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
17410   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
17411   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRLW128);
17412   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRLD128);
17413   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
17414
17415   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
17416   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
17417   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRAW128);
17418   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRAD128);
17419
17420   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
17421
17422   /* Prescott New Instructions.  */
17423   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
17424                void_ftype_pcvoid_unsigned_unsigned,
17425                IX86_BUILTIN_MONITOR);
17426   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
17427                void_ftype_unsigned_unsigned,
17428                IX86_BUILTIN_MWAIT);
17429   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
17430                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
17431
17432   /* SSSE3.  */
17433   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr128",
17434                v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
17435   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int,
17436                IX86_BUILTIN_PALIGNR);
17437
17438   /* AMDFAM10 SSE4A New built-ins  */
17439   def_builtin (MASK_SSE4A, "__builtin_ia32_movntsd",
17440                void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTSD);
17441   def_builtin (MASK_SSE4A, "__builtin_ia32_movntss",
17442                void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTSS);
17443   def_builtin (MASK_SSE4A, "__builtin_ia32_extrqi",
17444                v2di_ftype_v2di_unsigned_unsigned, IX86_BUILTIN_EXTRQI);
17445   def_builtin (MASK_SSE4A, "__builtin_ia32_extrq",
17446                v2di_ftype_v2di_v16qi,  IX86_BUILTIN_EXTRQ);
17447   def_builtin (MASK_SSE4A, "__builtin_ia32_insertqi",
17448                v2di_ftype_v2di_v2di_unsigned_unsigned, IX86_BUILTIN_INSERTQI);
17449   def_builtin (MASK_SSE4A, "__builtin_ia32_insertq",
17450                v2di_ftype_v2di_v2di, IX86_BUILTIN_INSERTQ);
17451
17452   /* Access to the vec_init patterns.  */
17453   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
17454                                     integer_type_node, NULL_TREE);
17455   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
17456                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
17457
17458   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
17459                                     short_integer_type_node,
17460                                     short_integer_type_node,
17461                                     short_integer_type_node, NULL_TREE);
17462   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
17463                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
17464
17465   ftype = build_function_type_list (V8QI_type_node, char_type_node,
17466                                     char_type_node, char_type_node,
17467                                     char_type_node, char_type_node,
17468                                     char_type_node, char_type_node,
17469                                     char_type_node, NULL_TREE);
17470   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
17471                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
17472
17473   /* Access to the vec_extract patterns.  */
17474   ftype = build_function_type_list (double_type_node, V2DF_type_node,
17475                                     integer_type_node, NULL_TREE);
17476   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
17477                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
17478
17479   ftype = build_function_type_list (long_long_integer_type_node,
17480                                     V2DI_type_node, integer_type_node,
17481                                     NULL_TREE);
17482   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
17483                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
17484
17485   ftype = build_function_type_list (float_type_node, V4SF_type_node,
17486                                     integer_type_node, NULL_TREE);
17487   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
17488                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
17489
17490   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
17491                                     integer_type_node, NULL_TREE);
17492   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
17493                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
17494
17495   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
17496                                     integer_type_node, NULL_TREE);
17497   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
17498                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
17499
17500   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
17501                                     integer_type_node, NULL_TREE);
17502   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
17503                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
17504
17505   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
17506                                     integer_type_node, NULL_TREE);
17507   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
17508                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
17509
17510   /* Access to the vec_set patterns.  */
17511   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
17512                                     intHI_type_node,
17513                                     integer_type_node, NULL_TREE);
17514   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
17515                ftype, IX86_BUILTIN_VEC_SET_V8HI);
17516
17517   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
17518                                     intHI_type_node,
17519                                     integer_type_node, NULL_TREE);
17520   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
17521                ftype, IX86_BUILTIN_VEC_SET_V4HI);
17522 }
17523
17524 static void
17525 ix86_init_builtins (void)
17526 {
17527   if (TARGET_MMX)
17528     ix86_init_mmx_sse_builtins ();
17529 }
17530
17531 /* Errors in the source file can cause expand_expr to return const0_rtx
17532    where we expect a vector.  To avoid crashing, use one of the vector
17533    clear instructions.  */
17534 static rtx
17535 safe_vector_operand (rtx x, enum machine_mode mode)
17536 {
17537   if (x == const0_rtx)
17538     x = CONST0_RTX (mode);
17539   return x;
17540 }
17541
17542 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
17543
17544 static rtx
17545 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
17546 {
17547   rtx pat, xops[3];
17548   tree arg0 = CALL_EXPR_ARG (exp, 0);
17549   tree arg1 = CALL_EXPR_ARG (exp, 1);
17550   rtx op0 = expand_normal (arg0);
17551   rtx op1 = expand_normal (arg1);
17552   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17553   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17554   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
17555
17556   if (VECTOR_MODE_P (mode0))
17557     op0 = safe_vector_operand (op0, mode0);
17558   if (VECTOR_MODE_P (mode1))
17559     op1 = safe_vector_operand (op1, mode1);
17560
17561   if (optimize || !target
17562       || GET_MODE (target) != tmode
17563       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17564     target = gen_reg_rtx (tmode);
17565
17566   if (GET_MODE (op1) == SImode && mode1 == TImode)
17567     {
17568       rtx x = gen_reg_rtx (V4SImode);
17569       emit_insn (gen_sse2_loadd (x, op1));
17570       op1 = gen_lowpart (TImode, x);
17571     }
17572
17573   /* The insn must want input operands in the same modes as the
17574      result.  */
17575   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
17576               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
17577
17578   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
17579     op0 = copy_to_mode_reg (mode0, op0);
17580   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
17581     op1 = copy_to_mode_reg (mode1, op1);
17582
17583   /* ??? Using ix86_fixup_binary_operands is problematic when
17584      we've got mismatched modes.  Fake it.  */
17585
17586   xops[0] = target;
17587   xops[1] = op0;
17588   xops[2] = op1;
17589
17590   if (tmode == mode0 && tmode == mode1)
17591     {
17592       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
17593       op0 = xops[1];
17594       op1 = xops[2];
17595     }
17596   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
17597     {
17598       op0 = force_reg (mode0, op0);
17599       op1 = force_reg (mode1, op1);
17600       target = gen_reg_rtx (tmode);
17601     }
17602
17603   pat = GEN_FCN (icode) (target, op0, op1);
17604   if (! pat)
17605     return 0;
17606   emit_insn (pat);
17607   return target;
17608 }
17609
17610 /* Subroutine of ix86_expand_builtin to take care of stores.  */
17611
17612 static rtx
17613 ix86_expand_store_builtin (enum insn_code icode, tree exp)
17614 {
17615   rtx pat;
17616   tree arg0 = CALL_EXPR_ARG (exp, 0);
17617   tree arg1 = CALL_EXPR_ARG (exp, 1);
17618   rtx op0 = expand_normal (arg0);
17619   rtx op1 = expand_normal (arg1);
17620   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
17621   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
17622
17623   if (VECTOR_MODE_P (mode1))
17624     op1 = safe_vector_operand (op1, mode1);
17625
17626   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17627   op1 = copy_to_mode_reg (mode1, op1);
17628
17629   pat = GEN_FCN (icode) (op0, op1);
17630   if (pat)
17631     emit_insn (pat);
17632   return 0;
17633 }
17634
17635 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
17636
17637 static rtx
17638 ix86_expand_unop_builtin (enum insn_code icode, tree exp,
17639                           rtx target, int do_load)
17640 {
17641   rtx pat;
17642   tree arg0 = CALL_EXPR_ARG (exp, 0);
17643   rtx op0 = expand_normal (arg0);
17644   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17645   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17646
17647   if (optimize || !target
17648       || GET_MODE (target) != tmode
17649       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17650     target = gen_reg_rtx (tmode);
17651   if (do_load)
17652     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17653   else
17654     {
17655       if (VECTOR_MODE_P (mode0))
17656         op0 = safe_vector_operand (op0, mode0);
17657
17658       if ((optimize && !register_operand (op0, mode0))
17659           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17660         op0 = copy_to_mode_reg (mode0, op0);
17661     }
17662
17663   pat = GEN_FCN (icode) (target, op0);
17664   if (! pat)
17665     return 0;
17666   emit_insn (pat);
17667   return target;
17668 }
17669
17670 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
17671    sqrtss, rsqrtss, rcpss.  */
17672
17673 static rtx
17674 ix86_expand_unop1_builtin (enum insn_code icode, tree exp, rtx target)
17675 {
17676   rtx pat;
17677   tree arg0 = CALL_EXPR_ARG (exp, 0);
17678   rtx op1, op0 = expand_normal (arg0);
17679   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17680   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17681
17682   if (optimize || !target
17683       || GET_MODE (target) != tmode
17684       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17685     target = gen_reg_rtx (tmode);
17686
17687   if (VECTOR_MODE_P (mode0))
17688     op0 = safe_vector_operand (op0, mode0);
17689
17690   if ((optimize && !register_operand (op0, mode0))
17691       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17692     op0 = copy_to_mode_reg (mode0, op0);
17693
17694   op1 = op0;
17695   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
17696     op1 = copy_to_mode_reg (mode0, op1);
17697
17698   pat = GEN_FCN (icode) (target, op0, op1);
17699   if (! pat)
17700     return 0;
17701   emit_insn (pat);
17702   return target;
17703 }
17704
17705 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
17706
17707 static rtx
17708 ix86_expand_sse_compare (const struct builtin_description *d, tree exp,
17709                          rtx target)
17710 {
17711   rtx pat;
17712   tree arg0 = CALL_EXPR_ARG (exp, 0);
17713   tree arg1 = CALL_EXPR_ARG (exp, 1);
17714   rtx op0 = expand_normal (arg0);
17715   rtx op1 = expand_normal (arg1);
17716   rtx op2;
17717   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
17718   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
17719   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
17720   enum rtx_code comparison = d->comparison;
17721
17722   if (VECTOR_MODE_P (mode0))
17723     op0 = safe_vector_operand (op0, mode0);
17724   if (VECTOR_MODE_P (mode1))
17725     op1 = safe_vector_operand (op1, mode1);
17726
17727   /* Swap operands if we have a comparison that isn't available in
17728      hardware.  */
17729   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17730     {
17731       rtx tmp = gen_reg_rtx (mode1);
17732       emit_move_insn (tmp, op1);
17733       op1 = op0;
17734       op0 = tmp;
17735     }
17736
17737   if (optimize || !target
17738       || GET_MODE (target) != tmode
17739       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
17740     target = gen_reg_rtx (tmode);
17741
17742   if ((optimize && !register_operand (op0, mode0))
17743       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
17744     op0 = copy_to_mode_reg (mode0, op0);
17745   if ((optimize && !register_operand (op1, mode1))
17746       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
17747     op1 = copy_to_mode_reg (mode1, op1);
17748
17749   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17750   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
17751   if (! pat)
17752     return 0;
17753   emit_insn (pat);
17754   return target;
17755 }
17756
17757 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
17758
17759 static rtx
17760 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
17761                       rtx target)
17762 {
17763   rtx pat;
17764   tree arg0 = CALL_EXPR_ARG (exp, 0);
17765   tree arg1 = CALL_EXPR_ARG (exp, 1);
17766   rtx op0 = expand_normal (arg0);
17767   rtx op1 = expand_normal (arg1);
17768   rtx op2;
17769   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
17770   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
17771   enum rtx_code comparison = d->comparison;
17772
17773   if (VECTOR_MODE_P (mode0))
17774     op0 = safe_vector_operand (op0, mode0);
17775   if (VECTOR_MODE_P (mode1))
17776     op1 = safe_vector_operand (op1, mode1);
17777
17778   /* Swap operands if we have a comparison that isn't available in
17779      hardware.  */
17780   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17781     {
17782       rtx tmp = op1;
17783       op1 = op0;
17784       op0 = tmp;
17785     }
17786
17787   target = gen_reg_rtx (SImode);
17788   emit_move_insn (target, const0_rtx);
17789   target = gen_rtx_SUBREG (QImode, target, 0);
17790
17791   if ((optimize && !register_operand (op0, mode0))
17792       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
17793     op0 = copy_to_mode_reg (mode0, op0);
17794   if ((optimize && !register_operand (op1, mode1))
17795       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
17796     op1 = copy_to_mode_reg (mode1, op1);
17797
17798   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17799   pat = GEN_FCN (d->icode) (op0, op1);
17800   if (! pat)
17801     return 0;
17802   emit_insn (pat);
17803   emit_insn (gen_rtx_SET (VOIDmode,
17804                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
17805                           gen_rtx_fmt_ee (comparison, QImode,
17806                                           SET_DEST (pat),
17807                                           const0_rtx)));
17808
17809   return SUBREG_REG (target);
17810 }
17811
17812 /* Return the integer constant in ARG.  Constrain it to be in the range
17813    of the subparts of VEC_TYPE; issue an error if not.  */
17814
17815 static int
17816 get_element_number (tree vec_type, tree arg)
17817 {
17818   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
17819
17820   if (!host_integerp (arg, 1)
17821       || (elt = tree_low_cst (arg, 1), elt > max))
17822     {
17823       error ("selector must be an integer constant in the range 0..%wi", max);
17824       return 0;
17825     }
17826
17827   return elt;
17828 }
17829
17830 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17831    ix86_expand_vector_init.  We DO have language-level syntax for this, in
17832    the form of  (type){ init-list }.  Except that since we can't place emms
17833    instructions from inside the compiler, we can't allow the use of MMX
17834    registers unless the user explicitly asks for it.  So we do *not* define
17835    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
17836    we have builtins invoked by mmintrin.h that gives us license to emit
17837    these sorts of instructions.  */
17838
17839 static rtx
17840 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
17841 {
17842   enum machine_mode tmode = TYPE_MODE (type);
17843   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
17844   int i, n_elt = GET_MODE_NUNITS (tmode);
17845   rtvec v = rtvec_alloc (n_elt);
17846
17847   gcc_assert (VECTOR_MODE_P (tmode));
17848   gcc_assert (call_expr_nargs (exp) == n_elt);
17849
17850   for (i = 0; i < n_elt; ++i)
17851     {
17852       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
17853       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
17854     }
17855
17856   if (!target || !register_operand (target, tmode))
17857     target = gen_reg_rtx (tmode);
17858
17859   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
17860   return target;
17861 }
17862
17863 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17864    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
17865    had a language-level syntax for referencing vector elements.  */
17866
17867 static rtx
17868 ix86_expand_vec_ext_builtin (tree exp, rtx target)
17869 {
17870   enum machine_mode tmode, mode0;
17871   tree arg0, arg1;
17872   int elt;
17873   rtx op0;
17874
17875   arg0 = CALL_EXPR_ARG (exp, 0);
17876   arg1 = CALL_EXPR_ARG (exp, 1);
17877
17878   op0 = expand_normal (arg0);
17879   elt = get_element_number (TREE_TYPE (arg0), arg1);
17880
17881   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17882   mode0 = TYPE_MODE (TREE_TYPE (arg0));
17883   gcc_assert (VECTOR_MODE_P (mode0));
17884
17885   op0 = force_reg (mode0, op0);
17886
17887   if (optimize || !target || !register_operand (target, tmode))
17888     target = gen_reg_rtx (tmode);
17889
17890   ix86_expand_vector_extract (true, target, op0, elt);
17891
17892   return target;
17893 }
17894
17895 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17896    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
17897    a language-level syntax for referencing vector elements.  */
17898
17899 static rtx
17900 ix86_expand_vec_set_builtin (tree exp)
17901 {
17902   enum machine_mode tmode, mode1;
17903   tree arg0, arg1, arg2;
17904   int elt;
17905   rtx op0, op1, target;
17906
17907   arg0 = CALL_EXPR_ARG (exp, 0);
17908   arg1 = CALL_EXPR_ARG (exp, 1);
17909   arg2 = CALL_EXPR_ARG (exp, 2);
17910
17911   tmode = TYPE_MODE (TREE_TYPE (arg0));
17912   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17913   gcc_assert (VECTOR_MODE_P (tmode));
17914
17915   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
17916   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
17917   elt = get_element_number (TREE_TYPE (arg0), arg2);
17918
17919   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
17920     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
17921
17922   op0 = force_reg (tmode, op0);
17923   op1 = force_reg (mode1, op1);
17924
17925   /* OP0 is the source of these builtin functions and shouldn't be
17926      modifified.  Create a copy, use it and return it as target.  */
17927   target = gen_reg_rtx (tmode);
17928   emit_move_insn (target, op0);
17929   ix86_expand_vector_set (true, target, op1, elt);
17930
17931   return target;
17932 }
17933
17934 /* Expand an expression EXP that calls a built-in function,
17935    with result going to TARGET if that's convenient
17936    (and in mode MODE if that's convenient).
17937    SUBTARGET may be used as the target for computing one of EXP's operands.
17938    IGNORE is nonzero if the value is to be ignored.  */
17939
17940 static rtx
17941 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17942                      enum machine_mode mode ATTRIBUTE_UNUSED,
17943                      int ignore ATTRIBUTE_UNUSED)
17944 {
17945   const struct builtin_description *d;
17946   size_t i;
17947   enum insn_code icode;
17948   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17949   tree arg0, arg1, arg2, arg3;
17950   rtx op0, op1, op2, op3, pat;
17951   enum machine_mode tmode, mode0, mode1, mode2, mode3, mode4;
17952   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
17953
17954   switch (fcode)
17955     {
17956     case IX86_BUILTIN_EMMS:
17957       emit_insn (gen_mmx_emms ());
17958       return 0;
17959
17960     case IX86_BUILTIN_SFENCE:
17961       emit_insn (gen_sse_sfence ());
17962       return 0;
17963
17964     case IX86_BUILTIN_MASKMOVQ:
17965     case IX86_BUILTIN_MASKMOVDQU:
17966       icode = (fcode == IX86_BUILTIN_MASKMOVQ
17967                ? CODE_FOR_mmx_maskmovq
17968                : CODE_FOR_sse2_maskmovdqu);
17969       /* Note the arg order is different from the operand order.  */
17970       arg1 = CALL_EXPR_ARG (exp, 0);
17971       arg2 = CALL_EXPR_ARG (exp, 1);
17972       arg0 = CALL_EXPR_ARG (exp, 2);
17973       op0 = expand_normal (arg0);
17974       op1 = expand_normal (arg1);
17975       op2 = expand_normal (arg2);
17976       mode0 = insn_data[icode].operand[0].mode;
17977       mode1 = insn_data[icode].operand[1].mode;
17978       mode2 = insn_data[icode].operand[2].mode;
17979
17980       op0 = force_reg (Pmode, op0);
17981       op0 = gen_rtx_MEM (mode1, op0);
17982
17983       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
17984         op0 = copy_to_mode_reg (mode0, op0);
17985       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17986         op1 = copy_to_mode_reg (mode1, op1);
17987       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
17988         op2 = copy_to_mode_reg (mode2, op2);
17989       pat = GEN_FCN (icode) (op0, op1, op2);
17990       if (! pat)
17991         return 0;
17992       emit_insn (pat);
17993       return 0;
17994
17995     case IX86_BUILTIN_SQRTSS:
17996       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, exp, target);
17997     case IX86_BUILTIN_RSQRTSS:
17998       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, exp, target);
17999     case IX86_BUILTIN_RCPSS:
18000       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, exp, target);
18001
18002     case IX86_BUILTIN_LOADUPS:
18003       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, exp, target, 1);
18004
18005     case IX86_BUILTIN_STOREUPS:
18006       return ix86_expand_store_builtin (CODE_FOR_sse_movups, exp);
18007
18008     case IX86_BUILTIN_LOADHPS:
18009     case IX86_BUILTIN_LOADLPS:
18010     case IX86_BUILTIN_LOADHPD:
18011     case IX86_BUILTIN_LOADLPD:
18012       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
18013                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
18014                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
18015                : CODE_FOR_sse2_loadlpd);
18016       arg0 = CALL_EXPR_ARG (exp, 0);
18017       arg1 = CALL_EXPR_ARG (exp, 1);
18018       op0 = expand_normal (arg0);
18019       op1 = expand_normal (arg1);
18020       tmode = insn_data[icode].operand[0].mode;
18021       mode0 = insn_data[icode].operand[1].mode;
18022       mode1 = insn_data[icode].operand[2].mode;
18023
18024       op0 = force_reg (mode0, op0);
18025       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
18026       if (optimize || target == 0
18027           || GET_MODE (target) != tmode
18028           || !register_operand (target, tmode))
18029         target = gen_reg_rtx (tmode);
18030       pat = GEN_FCN (icode) (target, op0, op1);
18031       if (! pat)
18032         return 0;
18033       emit_insn (pat);
18034       return target;
18035
18036     case IX86_BUILTIN_STOREHPS:
18037     case IX86_BUILTIN_STORELPS:
18038       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
18039                : CODE_FOR_sse_storelps);
18040       arg0 = CALL_EXPR_ARG (exp, 0);
18041       arg1 = CALL_EXPR_ARG (exp, 1);
18042       op0 = expand_normal (arg0);
18043       op1 = expand_normal (arg1);
18044       mode0 = insn_data[icode].operand[0].mode;
18045       mode1 = insn_data[icode].operand[1].mode;
18046
18047       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
18048       op1 = force_reg (mode1, op1);
18049
18050       pat = GEN_FCN (icode) (op0, op1);
18051       if (! pat)
18052         return 0;
18053       emit_insn (pat);
18054       return const0_rtx;
18055
18056     case IX86_BUILTIN_MOVNTPS:
18057       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, exp);
18058     case IX86_BUILTIN_MOVNTQ:
18059       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, exp);
18060
18061     case IX86_BUILTIN_LDMXCSR:
18062       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
18063       target = assign_386_stack_local (SImode, SLOT_TEMP);
18064       emit_move_insn (target, op0);
18065       emit_insn (gen_sse_ldmxcsr (target));
18066       return 0;
18067
18068     case IX86_BUILTIN_STMXCSR:
18069       target = assign_386_stack_local (SImode, SLOT_TEMP);
18070       emit_insn (gen_sse_stmxcsr (target));
18071       return copy_to_mode_reg (SImode, target);
18072
18073     case IX86_BUILTIN_SHUFPS:
18074     case IX86_BUILTIN_SHUFPD:
18075       icode = (fcode == IX86_BUILTIN_SHUFPS
18076                ? CODE_FOR_sse_shufps
18077                : CODE_FOR_sse2_shufpd);
18078       arg0 = CALL_EXPR_ARG (exp, 0);
18079       arg1 = CALL_EXPR_ARG (exp, 1);
18080       arg2 = CALL_EXPR_ARG (exp, 2);
18081       op0 = expand_normal (arg0);
18082       op1 = expand_normal (arg1);
18083       op2 = expand_normal (arg2);
18084       tmode = insn_data[icode].operand[0].mode;
18085       mode0 = insn_data[icode].operand[1].mode;
18086       mode1 = insn_data[icode].operand[2].mode;
18087       mode2 = insn_data[icode].operand[3].mode;
18088
18089       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18090         op0 = copy_to_mode_reg (mode0, op0);
18091       if ((optimize && !register_operand (op1, mode1))
18092           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
18093         op1 = copy_to_mode_reg (mode1, op1);
18094       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
18095         {
18096           /* @@@ better error message */
18097           error ("mask must be an immediate");
18098           return gen_reg_rtx (tmode);
18099         }
18100       if (optimize || target == 0
18101           || GET_MODE (target) != tmode
18102           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18103         target = gen_reg_rtx (tmode);
18104       pat = GEN_FCN (icode) (target, op0, op1, op2);
18105       if (! pat)
18106         return 0;
18107       emit_insn (pat);
18108       return target;
18109
18110     case IX86_BUILTIN_PSHUFW:
18111     case IX86_BUILTIN_PSHUFD:
18112     case IX86_BUILTIN_PSHUFHW:
18113     case IX86_BUILTIN_PSHUFLW:
18114       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
18115                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
18116                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
18117                : CODE_FOR_mmx_pshufw);
18118       arg0 = CALL_EXPR_ARG (exp, 0);
18119       arg1 = CALL_EXPR_ARG (exp, 1);
18120       op0 = expand_normal (arg0);
18121       op1 = expand_normal (arg1);
18122       tmode = insn_data[icode].operand[0].mode;
18123       mode1 = insn_data[icode].operand[1].mode;
18124       mode2 = insn_data[icode].operand[2].mode;
18125
18126       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18127         op0 = copy_to_mode_reg (mode1, op0);
18128       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18129         {
18130           /* @@@ better error message */
18131           error ("mask must be an immediate");
18132           return const0_rtx;
18133         }
18134       if (target == 0
18135           || GET_MODE (target) != tmode
18136           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18137         target = gen_reg_rtx (tmode);
18138       pat = GEN_FCN (icode) (target, op0, op1);
18139       if (! pat)
18140         return 0;
18141       emit_insn (pat);
18142       return target;
18143
18144     case IX86_BUILTIN_PSLLWI128:
18145       icode = CODE_FOR_ashlv8hi3;
18146       goto do_pshifti;
18147     case IX86_BUILTIN_PSLLDI128:
18148       icode = CODE_FOR_ashlv4si3;
18149       goto do_pshifti;
18150     case IX86_BUILTIN_PSLLQI128:
18151       icode = CODE_FOR_ashlv2di3;
18152       goto do_pshifti;
18153     case IX86_BUILTIN_PSRAWI128:
18154       icode = CODE_FOR_ashrv8hi3;
18155       goto do_pshifti;
18156     case IX86_BUILTIN_PSRADI128:
18157       icode = CODE_FOR_ashrv4si3;
18158       goto do_pshifti;
18159     case IX86_BUILTIN_PSRLWI128:
18160       icode = CODE_FOR_lshrv8hi3;
18161       goto do_pshifti;
18162     case IX86_BUILTIN_PSRLDI128:
18163       icode = CODE_FOR_lshrv4si3;
18164       goto do_pshifti;
18165     case IX86_BUILTIN_PSRLQI128:
18166       icode = CODE_FOR_lshrv2di3;
18167       goto do_pshifti;
18168     do_pshifti:
18169       arg0 = CALL_EXPR_ARG (exp, 0);
18170       arg1 = CALL_EXPR_ARG (exp, 1);
18171       op0 = expand_normal (arg0);
18172       op1 = expand_normal (arg1);
18173
18174       if (!CONST_INT_P (op1))
18175         {
18176           error ("shift must be an immediate");
18177           return const0_rtx;
18178         }
18179       if (INTVAL (op1) < 0 || INTVAL (op1) > 255)
18180         op1 = GEN_INT (255);
18181
18182       tmode = insn_data[icode].operand[0].mode;
18183       mode1 = insn_data[icode].operand[1].mode;
18184       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18185         op0 = copy_to_reg (op0);
18186
18187       target = gen_reg_rtx (tmode);
18188       pat = GEN_FCN (icode) (target, op0, op1);
18189       if (!pat)
18190         return 0;
18191       emit_insn (pat);
18192       return target;
18193
18194     case IX86_BUILTIN_PSLLW128:
18195       icode = CODE_FOR_ashlv8hi3;
18196       goto do_pshift;
18197     case IX86_BUILTIN_PSLLD128:
18198       icode = CODE_FOR_ashlv4si3;
18199       goto do_pshift;
18200     case IX86_BUILTIN_PSLLQ128:
18201       icode = CODE_FOR_ashlv2di3;
18202       goto do_pshift;
18203     case IX86_BUILTIN_PSRAW128:
18204       icode = CODE_FOR_ashrv8hi3;
18205       goto do_pshift;
18206     case IX86_BUILTIN_PSRAD128:
18207       icode = CODE_FOR_ashrv4si3;
18208       goto do_pshift;
18209     case IX86_BUILTIN_PSRLW128:
18210       icode = CODE_FOR_lshrv8hi3;
18211       goto do_pshift;
18212     case IX86_BUILTIN_PSRLD128:
18213       icode = CODE_FOR_lshrv4si3;
18214       goto do_pshift;
18215     case IX86_BUILTIN_PSRLQ128:
18216       icode = CODE_FOR_lshrv2di3;
18217       goto do_pshift;
18218     do_pshift:
18219       arg0 = CALL_EXPR_ARG (exp, 0);
18220       arg1 = CALL_EXPR_ARG (exp, 1);
18221       op0 = expand_normal (arg0);
18222       op1 = expand_normal (arg1);
18223
18224       tmode = insn_data[icode].operand[0].mode;
18225       mode1 = insn_data[icode].operand[1].mode;
18226
18227       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18228         op0 = copy_to_reg (op0);
18229
18230       op1 = simplify_gen_subreg (TImode, op1, GET_MODE (op1), 0);
18231       if (! (*insn_data[icode].operand[2].predicate) (op1, TImode))
18232         op1 = copy_to_reg (op1);
18233
18234       target = gen_reg_rtx (tmode);
18235       pat = GEN_FCN (icode) (target, op0, op1);
18236       if (!pat)
18237         return 0;
18238       emit_insn (pat);
18239       return target;
18240
18241     case IX86_BUILTIN_PSLLDQI128:
18242     case IX86_BUILTIN_PSRLDQI128:
18243       icode = (fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
18244                : CODE_FOR_sse2_lshrti3);
18245       arg0 = CALL_EXPR_ARG (exp, 0);
18246       arg1 = CALL_EXPR_ARG (exp, 1);
18247       op0 = expand_normal (arg0);
18248       op1 = expand_normal (arg1);
18249       tmode = insn_data[icode].operand[0].mode;
18250       mode1 = insn_data[icode].operand[1].mode;
18251       mode2 = insn_data[icode].operand[2].mode;
18252
18253       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18254         {
18255           op0 = copy_to_reg (op0);
18256           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
18257         }
18258       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18259         {
18260           error ("shift must be an immediate");
18261           return const0_rtx;
18262         }
18263       target = gen_reg_rtx (V2DImode);
18264       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0),
18265                              op0, op1);
18266       if (! pat)
18267         return 0;
18268       emit_insn (pat);
18269       return target;
18270
18271     case IX86_BUILTIN_FEMMS:
18272       emit_insn (gen_mmx_femms ());
18273       return NULL_RTX;
18274
18275     case IX86_BUILTIN_PAVGUSB:
18276       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, exp, target);
18277
18278     case IX86_BUILTIN_PF2ID:
18279       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, exp, target, 0);
18280
18281     case IX86_BUILTIN_PFACC:
18282       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, exp, target);
18283
18284     case IX86_BUILTIN_PFADD:
18285      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, exp, target);
18286
18287     case IX86_BUILTIN_PFCMPEQ:
18288       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, exp, target);
18289
18290     case IX86_BUILTIN_PFCMPGE:
18291       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, exp, target);
18292
18293     case IX86_BUILTIN_PFCMPGT:
18294       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, exp, target);
18295
18296     case IX86_BUILTIN_PFMAX:
18297       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, exp, target);
18298
18299     case IX86_BUILTIN_PFMIN:
18300       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, exp, target);
18301
18302     case IX86_BUILTIN_PFMUL:
18303       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, exp, target);
18304
18305     case IX86_BUILTIN_PFRCP:
18306       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, exp, target, 0);
18307
18308     case IX86_BUILTIN_PFRCPIT1:
18309       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, exp, target);
18310
18311     case IX86_BUILTIN_PFRCPIT2:
18312       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, exp, target);
18313
18314     case IX86_BUILTIN_PFRSQIT1:
18315       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, exp, target);
18316
18317     case IX86_BUILTIN_PFRSQRT:
18318       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, exp, target, 0);
18319
18320     case IX86_BUILTIN_PFSUB:
18321       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, exp, target);
18322
18323     case IX86_BUILTIN_PFSUBR:
18324       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, exp, target);
18325
18326     case IX86_BUILTIN_PI2FD:
18327       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, exp, target, 0);
18328
18329     case IX86_BUILTIN_PMULHRW:
18330       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, exp, target);
18331
18332     case IX86_BUILTIN_PF2IW:
18333       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, exp, target, 0);
18334
18335     case IX86_BUILTIN_PFNACC:
18336       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, exp, target);
18337
18338     case IX86_BUILTIN_PFPNACC:
18339       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, exp, target);
18340
18341     case IX86_BUILTIN_PI2FW:
18342       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, exp, target, 0);
18343
18344     case IX86_BUILTIN_PSWAPDSI:
18345       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, exp, target, 0);
18346
18347     case IX86_BUILTIN_PSWAPDSF:
18348       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, exp, target, 0);
18349
18350     case IX86_BUILTIN_SQRTSD:
18351       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, exp, target);
18352     case IX86_BUILTIN_LOADUPD:
18353       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, exp, target, 1);
18354     case IX86_BUILTIN_STOREUPD:
18355       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, exp);
18356
18357     case IX86_BUILTIN_MFENCE:
18358         emit_insn (gen_sse2_mfence ());
18359         return 0;
18360     case IX86_BUILTIN_LFENCE:
18361         emit_insn (gen_sse2_lfence ());
18362         return 0;
18363
18364     case IX86_BUILTIN_CLFLUSH:
18365         arg0 = CALL_EXPR_ARG (exp, 0);
18366         op0 = expand_normal (arg0);
18367         icode = CODE_FOR_sse2_clflush;
18368         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
18369             op0 = copy_to_mode_reg (Pmode, op0);
18370
18371         emit_insn (gen_sse2_clflush (op0));
18372         return 0;
18373
18374     case IX86_BUILTIN_MOVNTPD:
18375       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, exp);
18376     case IX86_BUILTIN_MOVNTDQ:
18377       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, exp);
18378     case IX86_BUILTIN_MOVNTI:
18379       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, exp);
18380
18381     case IX86_BUILTIN_LOADDQU:
18382       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, exp, target, 1);
18383     case IX86_BUILTIN_STOREDQU:
18384       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, exp);
18385
18386     case IX86_BUILTIN_MONITOR:
18387       arg0 = CALL_EXPR_ARG (exp, 0);
18388       arg1 = CALL_EXPR_ARG (exp, 1);
18389       arg2 = CALL_EXPR_ARG (exp, 2);
18390       op0 = expand_normal (arg0);
18391       op1 = expand_normal (arg1);
18392       op2 = expand_normal (arg2);
18393       if (!REG_P (op0))
18394         op0 = copy_to_mode_reg (Pmode, op0);
18395       if (!REG_P (op1))
18396         op1 = copy_to_mode_reg (SImode, op1);
18397       if (!REG_P (op2))
18398         op2 = copy_to_mode_reg (SImode, op2);
18399       if (!TARGET_64BIT)
18400         emit_insn (gen_sse3_monitor (op0, op1, op2));
18401       else
18402         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
18403       return 0;
18404
18405     case IX86_BUILTIN_MWAIT:
18406       arg0 = CALL_EXPR_ARG (exp, 0);
18407       arg1 = CALL_EXPR_ARG (exp, 1);
18408       op0 = expand_normal (arg0);
18409       op1 = expand_normal (arg1);
18410       if (!REG_P (op0))
18411         op0 = copy_to_mode_reg (SImode, op0);
18412       if (!REG_P (op1))
18413         op1 = copy_to_mode_reg (SImode, op1);
18414       emit_insn (gen_sse3_mwait (op0, op1));
18415       return 0;
18416
18417     case IX86_BUILTIN_LDDQU:
18418       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, exp,
18419                                        target, 1);
18420
18421     case IX86_BUILTIN_PALIGNR:
18422     case IX86_BUILTIN_PALIGNR128:
18423       if (fcode == IX86_BUILTIN_PALIGNR)
18424         {
18425           icode = CODE_FOR_ssse3_palignrdi;
18426           mode = DImode;
18427         }
18428       else
18429         {
18430           icode = CODE_FOR_ssse3_palignrti;
18431           mode = V2DImode;
18432         }
18433       arg0 = CALL_EXPR_ARG (exp, 0);
18434       arg1 = CALL_EXPR_ARG (exp, 1);
18435       arg2 = CALL_EXPR_ARG (exp, 2);
18436       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
18437       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
18438       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
18439       tmode = insn_data[icode].operand[0].mode;
18440       mode1 = insn_data[icode].operand[1].mode;
18441       mode2 = insn_data[icode].operand[2].mode;
18442       mode3 = insn_data[icode].operand[3].mode;
18443
18444       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18445         {
18446           op0 = copy_to_reg (op0);
18447           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
18448         }
18449       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18450         {
18451           op1 = copy_to_reg (op1);
18452           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
18453         }
18454       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18455         {
18456           error ("shift must be an immediate");
18457           return const0_rtx;
18458         }
18459       target = gen_reg_rtx (mode);
18460       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
18461                              op0, op1, op2);
18462       if (! pat)
18463         return 0;
18464       emit_insn (pat);
18465       return target;
18466
18467     case IX86_BUILTIN_MOVNTSD:
18468       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv2df, exp);
18469
18470     case IX86_BUILTIN_MOVNTSS:
18471       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv4sf, exp);
18472
18473     case IX86_BUILTIN_INSERTQ:
18474     case IX86_BUILTIN_EXTRQ:
18475       icode = (fcode == IX86_BUILTIN_EXTRQ
18476                ? CODE_FOR_sse4a_extrq
18477                : CODE_FOR_sse4a_insertq);
18478       arg0 = CALL_EXPR_ARG (exp, 0);
18479       arg1 = CALL_EXPR_ARG (exp, 1);
18480       op0 = expand_normal (arg0);
18481       op1 = expand_normal (arg1);
18482       tmode = insn_data[icode].operand[0].mode;
18483       mode1 = insn_data[icode].operand[1].mode;
18484       mode2 = insn_data[icode].operand[2].mode;
18485       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18486         op0 = copy_to_mode_reg (mode1, op0);
18487       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18488         op1 = copy_to_mode_reg (mode2, op1);
18489       if (optimize || target == 0
18490           || GET_MODE (target) != tmode
18491           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18492         target = gen_reg_rtx (tmode);
18493       pat = GEN_FCN (icode) (target, op0, op1);
18494       if (! pat)
18495         return NULL_RTX;
18496       emit_insn (pat);
18497       return target;
18498
18499     case IX86_BUILTIN_EXTRQI:
18500       icode = CODE_FOR_sse4a_extrqi;
18501       arg0 = CALL_EXPR_ARG (exp, 0);
18502       arg1 = CALL_EXPR_ARG (exp, 1);
18503       arg2 = CALL_EXPR_ARG (exp, 2);
18504       op0 = expand_normal (arg0);
18505       op1 = expand_normal (arg1);
18506       op2 = expand_normal (arg2);
18507       tmode = insn_data[icode].operand[0].mode;
18508       mode1 = insn_data[icode].operand[1].mode;
18509       mode2 = insn_data[icode].operand[2].mode;
18510       mode3 = insn_data[icode].operand[3].mode;
18511       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18512         op0 = copy_to_mode_reg (mode1, op0);
18513       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18514         {
18515           error ("index mask must be an immediate");
18516           return gen_reg_rtx (tmode);
18517         }
18518       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18519         {
18520           error ("length mask must be an immediate");
18521           return gen_reg_rtx (tmode);
18522         }
18523       if (optimize || target == 0
18524           || GET_MODE (target) != tmode
18525           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18526         target = gen_reg_rtx (tmode);
18527       pat = GEN_FCN (icode) (target, op0, op1, op2);
18528       if (! pat)
18529         return NULL_RTX;
18530       emit_insn (pat);
18531       return target;
18532
18533     case IX86_BUILTIN_INSERTQI:
18534       icode = CODE_FOR_sse4a_insertqi;
18535       arg0 = CALL_EXPR_ARG (exp, 0);
18536       arg1 = CALL_EXPR_ARG (exp, 1);
18537       arg2 = CALL_EXPR_ARG (exp, 2);
18538       arg3 = CALL_EXPR_ARG (exp, 3);
18539       op0 = expand_normal (arg0);
18540       op1 = expand_normal (arg1);
18541       op2 = expand_normal (arg2);
18542       op3 = expand_normal (arg3);
18543       tmode = insn_data[icode].operand[0].mode;
18544       mode1 = insn_data[icode].operand[1].mode;
18545       mode2 = insn_data[icode].operand[2].mode;
18546       mode3 = insn_data[icode].operand[3].mode;
18547       mode4 = insn_data[icode].operand[4].mode;
18548
18549       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18550         op0 = copy_to_mode_reg (mode1, op0);
18551
18552       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18553         op1 = copy_to_mode_reg (mode2, op1);
18554
18555       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18556         {
18557           error ("index mask must be an immediate");
18558           return gen_reg_rtx (tmode);
18559         }
18560       if (! (*insn_data[icode].operand[4].predicate) (op3, mode4))
18561         {
18562           error ("length mask must be an immediate");
18563           return gen_reg_rtx (tmode);
18564         }
18565       if (optimize || target == 0
18566           || GET_MODE (target) != tmode
18567           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18568         target = gen_reg_rtx (tmode);
18569       pat = GEN_FCN (icode) (target, op0, op1, op2, op3);
18570       if (! pat)
18571         return NULL_RTX;
18572       emit_insn (pat);
18573       return target;
18574
18575     case IX86_BUILTIN_VEC_INIT_V2SI:
18576     case IX86_BUILTIN_VEC_INIT_V4HI:
18577     case IX86_BUILTIN_VEC_INIT_V8QI:
18578       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
18579
18580     case IX86_BUILTIN_VEC_EXT_V2DF:
18581     case IX86_BUILTIN_VEC_EXT_V2DI:
18582     case IX86_BUILTIN_VEC_EXT_V4SF:
18583     case IX86_BUILTIN_VEC_EXT_V4SI:
18584     case IX86_BUILTIN_VEC_EXT_V8HI:
18585     case IX86_BUILTIN_VEC_EXT_V2SI:
18586     case IX86_BUILTIN_VEC_EXT_V4HI:
18587       return ix86_expand_vec_ext_builtin (exp, target);
18588
18589     case IX86_BUILTIN_VEC_SET_V8HI:
18590     case IX86_BUILTIN_VEC_SET_V4HI:
18591       return ix86_expand_vec_set_builtin (exp);
18592
18593     default:
18594       break;
18595     }
18596
18597   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
18598     if (d->code == fcode)
18599       {
18600         /* Compares are treated specially.  */
18601         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
18602             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
18603             || d->icode == CODE_FOR_sse2_maskcmpv2df3
18604             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
18605           return ix86_expand_sse_compare (d, exp, target);
18606
18607         return ix86_expand_binop_builtin (d->icode, exp, target);
18608       }
18609
18610   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
18611     if (d->code == fcode)
18612       return ix86_expand_unop_builtin (d->icode, exp, target, 0);
18613
18614   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
18615     if (d->code == fcode)
18616       return ix86_expand_sse_comi (d, exp, target);
18617
18618   gcc_unreachable ();
18619 }
18620
18621 /* Returns a function decl for a vectorized version of the builtin function
18622    with builtin function code FN and the result vector type TYPE, or NULL_TREE
18623    if it is not available.  */
18624
18625 static tree
18626 ix86_builtin_vectorized_function (enum built_in_function fn, tree type_out,
18627                                   tree type_in)
18628 {
18629   enum machine_mode in_mode, out_mode;
18630   int in_n, out_n;
18631
18632   if (TREE_CODE (type_out) != VECTOR_TYPE
18633       || TREE_CODE (type_in) != VECTOR_TYPE)
18634     return NULL_TREE;
18635
18636   out_mode = TYPE_MODE (TREE_TYPE (type_out));
18637   out_n = TYPE_VECTOR_SUBPARTS (type_out);
18638   in_mode = TYPE_MODE (TREE_TYPE (type_in));
18639   in_n = TYPE_VECTOR_SUBPARTS (type_in);
18640
18641   switch (fn)
18642     {
18643     case BUILT_IN_SQRT:
18644       if (out_mode == DFmode && out_n == 2
18645           && in_mode == DFmode && in_n == 2)
18646         return ix86_builtins[IX86_BUILTIN_SQRTPD];
18647       return NULL_TREE;
18648
18649     case BUILT_IN_SQRTF:
18650       if (out_mode == SFmode && out_n == 4
18651           && in_mode == SFmode && in_n == 4)
18652         return ix86_builtins[IX86_BUILTIN_SQRTPS];
18653       return NULL_TREE;
18654
18655     case BUILT_IN_LRINTF:
18656       if (out_mode == SImode && out_n == 4
18657           && in_mode == SFmode && in_n == 4)
18658         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
18659       return NULL_TREE;
18660
18661     default:
18662       ;
18663     }
18664
18665   return NULL_TREE;
18666 }
18667
18668 /* Returns a decl of a function that implements conversion of the
18669    input vector of type TYPE, or NULL_TREE if it is not available.  */
18670
18671 static tree
18672 ix86_builtin_conversion (enum tree_code code, tree type)
18673 {
18674   if (TREE_CODE (type) != VECTOR_TYPE)
18675     return NULL_TREE;
18676
18677   switch (code)
18678     {
18679     case FLOAT_EXPR:
18680       switch (TYPE_MODE (type))
18681         {
18682         case V4SImode:
18683           return ix86_builtins[IX86_BUILTIN_CVTDQ2PS];
18684         default:
18685           return NULL_TREE;
18686         }
18687
18688     case FIX_TRUNC_EXPR:
18689       switch (TYPE_MODE (type))
18690         {
18691         case V4SFmode:
18692           return ix86_builtins[IX86_BUILTIN_CVTTPS2DQ];
18693         default:
18694           return NULL_TREE;
18695         }
18696     default:
18697       return NULL_TREE;
18698
18699     }
18700 }
18701
18702 /* Store OPERAND to the memory after reload is completed.  This means
18703    that we can't easily use assign_stack_local.  */
18704 rtx
18705 ix86_force_to_memory (enum machine_mode mode, rtx operand)
18706 {
18707   rtx result;
18708
18709   gcc_assert (reload_completed);
18710   if (TARGET_RED_ZONE)
18711     {
18712       result = gen_rtx_MEM (mode,
18713                             gen_rtx_PLUS (Pmode,
18714                                           stack_pointer_rtx,
18715                                           GEN_INT (-RED_ZONE_SIZE)));
18716       emit_move_insn (result, operand);
18717     }
18718   else if (!TARGET_RED_ZONE && TARGET_64BIT)
18719     {
18720       switch (mode)
18721         {
18722         case HImode:
18723         case SImode:
18724           operand = gen_lowpart (DImode, operand);
18725           /* FALLTHRU */
18726         case DImode:
18727           emit_insn (
18728                       gen_rtx_SET (VOIDmode,
18729                                    gen_rtx_MEM (DImode,
18730                                                 gen_rtx_PRE_DEC (DImode,
18731                                                         stack_pointer_rtx)),
18732                                    operand));
18733           break;
18734         default:
18735           gcc_unreachable ();
18736         }
18737       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18738     }
18739   else
18740     {
18741       switch (mode)
18742         {
18743         case DImode:
18744           {
18745             rtx operands[2];
18746             split_di (&operand, 1, operands, operands + 1);
18747             emit_insn (
18748                         gen_rtx_SET (VOIDmode,
18749                                      gen_rtx_MEM (SImode,
18750                                                   gen_rtx_PRE_DEC (Pmode,
18751                                                         stack_pointer_rtx)),
18752                                      operands[1]));
18753             emit_insn (
18754                         gen_rtx_SET (VOIDmode,
18755                                      gen_rtx_MEM (SImode,
18756                                                   gen_rtx_PRE_DEC (Pmode,
18757                                                         stack_pointer_rtx)),
18758                                      operands[0]));
18759           }
18760           break;
18761         case HImode:
18762           /* Store HImodes as SImodes.  */
18763           operand = gen_lowpart (SImode, operand);
18764           /* FALLTHRU */
18765         case SImode:
18766           emit_insn (
18767                       gen_rtx_SET (VOIDmode,
18768                                    gen_rtx_MEM (GET_MODE (operand),
18769                                                 gen_rtx_PRE_DEC (SImode,
18770                                                         stack_pointer_rtx)),
18771                                    operand));
18772           break;
18773         default:
18774           gcc_unreachable ();
18775         }
18776       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18777     }
18778   return result;
18779 }
18780
18781 /* Free operand from the memory.  */
18782 void
18783 ix86_free_from_memory (enum machine_mode mode)
18784 {
18785   if (!TARGET_RED_ZONE)
18786     {
18787       int size;
18788
18789       if (mode == DImode || TARGET_64BIT)
18790         size = 8;
18791       else
18792         size = 4;
18793       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
18794          to pop or add instruction if registers are available.  */
18795       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
18796                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
18797                                             GEN_INT (size))));
18798     }
18799 }
18800
18801 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
18802    QImode must go into class Q_REGS.
18803    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
18804    movdf to do mem-to-mem moves through integer regs.  */
18805 enum reg_class
18806 ix86_preferred_reload_class (rtx x, enum reg_class class)
18807 {
18808   enum machine_mode mode = GET_MODE (x);
18809
18810   /* We're only allowed to return a subclass of CLASS.  Many of the
18811      following checks fail for NO_REGS, so eliminate that early.  */
18812   if (class == NO_REGS)
18813     return NO_REGS;
18814
18815   /* All classes can load zeros.  */
18816   if (x == CONST0_RTX (mode))
18817     return class;
18818
18819   /* Force constants into memory if we are loading a (nonzero) constant into
18820      an MMX or SSE register.  This is because there are no MMX/SSE instructions
18821      to load from a constant.  */
18822   if (CONSTANT_P (x)
18823       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
18824     return NO_REGS;
18825
18826   /* Prefer SSE regs only, if we can use them for math.  */
18827   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
18828     return SSE_CLASS_P (class) ? class : NO_REGS;
18829
18830   /* Floating-point constants need more complex checks.  */
18831   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
18832     {
18833       /* General regs can load everything.  */
18834       if (reg_class_subset_p (class, GENERAL_REGS))
18835         return class;
18836
18837       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
18838          zero above.  We only want to wind up preferring 80387 registers if
18839          we plan on doing computation with them.  */
18840       if (TARGET_80387
18841           && standard_80387_constant_p (x))
18842         {
18843           /* Limit class to non-sse.  */
18844           if (class == FLOAT_SSE_REGS)
18845             return FLOAT_REGS;
18846           if (class == FP_TOP_SSE_REGS)
18847             return FP_TOP_REG;
18848           if (class == FP_SECOND_SSE_REGS)
18849             return FP_SECOND_REG;
18850           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
18851             return class;
18852         }
18853
18854       return NO_REGS;
18855     }
18856
18857   /* Generally when we see PLUS here, it's the function invariant
18858      (plus soft-fp const_int).  Which can only be computed into general
18859      regs.  */
18860   if (GET_CODE (x) == PLUS)
18861     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
18862
18863   /* QImode constants are easy to load, but non-constant QImode data
18864      must go into Q_REGS.  */
18865   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
18866     {
18867       if (reg_class_subset_p (class, Q_REGS))
18868         return class;
18869       if (reg_class_subset_p (Q_REGS, class))
18870         return Q_REGS;
18871       return NO_REGS;
18872     }
18873
18874   return class;
18875 }
18876
18877 /* Discourage putting floating-point values in SSE registers unless
18878    SSE math is being used, and likewise for the 387 registers.  */
18879 enum reg_class
18880 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
18881 {
18882   enum machine_mode mode = GET_MODE (x);
18883
18884   /* Restrict the output reload class to the register bank that we are doing
18885      math on.  If we would like not to return a subset of CLASS, reject this
18886      alternative: if reload cannot do this, it will still use its choice.  */
18887   mode = GET_MODE (x);
18888   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
18889     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
18890
18891   if (X87_FLOAT_MODE_P (mode))
18892     {
18893       if (class == FP_TOP_SSE_REGS)
18894         return FP_TOP_REG;
18895       else if (class == FP_SECOND_SSE_REGS)
18896         return FP_SECOND_REG;
18897       else
18898         return FLOAT_CLASS_P (class) ? class : NO_REGS;
18899     }
18900
18901   return class;
18902 }
18903
18904 /* If we are copying between general and FP registers, we need a memory
18905    location. The same is true for SSE and MMX registers.
18906
18907    The macro can't work reliably when one of the CLASSES is class containing
18908    registers from multiple units (SSE, MMX, integer).  We avoid this by never
18909    combining those units in single alternative in the machine description.
18910    Ensure that this constraint holds to avoid unexpected surprises.
18911
18912    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
18913    enforce these sanity checks.  */
18914
18915 int
18916 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
18917                               enum machine_mode mode, int strict)
18918 {
18919   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
18920       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
18921       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
18922       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
18923       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
18924       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
18925     {
18926       gcc_assert (!strict);
18927       return true;
18928     }
18929
18930   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
18931     return true;
18932
18933   /* ??? This is a lie.  We do have moves between mmx/general, and for
18934      mmx/sse2.  But by saying we need secondary memory we discourage the
18935      register allocator from using the mmx registers unless needed.  */
18936   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
18937     return true;
18938
18939   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
18940     {
18941       /* SSE1 doesn't have any direct moves from other classes.  */
18942       if (!TARGET_SSE2)
18943         return true;
18944
18945       /* If the target says that inter-unit moves are more expensive
18946          than moving through memory, then don't generate them.  */
18947       if (!TARGET_INTER_UNIT_MOVES)
18948         return true;
18949
18950       /* Between SSE and general, we have moves no larger than word size.  */
18951       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
18952         return true;
18953     }
18954
18955   return false;
18956 }
18957
18958 /* Return true if the registers in CLASS cannot represent the change from
18959    modes FROM to TO.  */
18960
18961 bool
18962 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
18963                                enum reg_class class)
18964 {
18965   if (from == to)
18966     return false;
18967
18968   /* x87 registers can't do subreg at all, as all values are reformatted
18969      to extended precision.  */
18970   if (MAYBE_FLOAT_CLASS_P (class))
18971     return true;
18972
18973   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
18974     {
18975       /* Vector registers do not support QI or HImode loads.  If we don't
18976          disallow a change to these modes, reload will assume it's ok to
18977          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
18978          the vec_dupv4hi pattern.  */
18979       if (GET_MODE_SIZE (from) < 4)
18980         return true;
18981
18982       /* Vector registers do not support subreg with nonzero offsets, which
18983          are otherwise valid for integer registers.  Since we can't see
18984          whether we have a nonzero offset from here, prohibit all
18985          nonparadoxical subregs changing size.  */
18986       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
18987         return true;
18988     }
18989
18990   return false;
18991 }
18992
18993 /* Return the cost of moving data from a register in class CLASS1 to
18994    one in class CLASS2.
18995
18996    It is not required that the cost always equal 2 when FROM is the same as TO;
18997    on some machines it is expensive to move between registers if they are not
18998    general registers.  */
18999
19000 int
19001 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
19002                          enum reg_class class2)
19003 {
19004   /* In case we require secondary memory, compute cost of the store followed
19005      by load.  In order to avoid bad register allocation choices, we need
19006      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
19007
19008   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
19009     {
19010       int cost = 1;
19011
19012       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
19013                    MEMORY_MOVE_COST (mode, class1, 1));
19014       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
19015                    MEMORY_MOVE_COST (mode, class2, 1));
19016
19017       /* In case of copying from general_purpose_register we may emit multiple
19018          stores followed by single load causing memory size mismatch stall.
19019          Count this as arbitrarily high cost of 20.  */
19020       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
19021         cost += 20;
19022
19023       /* In the case of FP/MMX moves, the registers actually overlap, and we
19024          have to switch modes in order to treat them differently.  */
19025       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
19026           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
19027         cost += 20;
19028
19029       return cost;
19030     }
19031
19032   /* Moves between SSE/MMX and integer unit are expensive.  */
19033   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
19034       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
19035     return ix86_cost->mmxsse_to_integer;
19036   if (MAYBE_FLOAT_CLASS_P (class1))
19037     return ix86_cost->fp_move;
19038   if (MAYBE_SSE_CLASS_P (class1))
19039     return ix86_cost->sse_move;
19040   if (MAYBE_MMX_CLASS_P (class1))
19041     return ix86_cost->mmx_move;
19042   return 2;
19043 }
19044
19045 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
19046
19047 bool
19048 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
19049 {
19050   /* Flags and only flags can only hold CCmode values.  */
19051   if (CC_REGNO_P (regno))
19052     return GET_MODE_CLASS (mode) == MODE_CC;
19053   if (GET_MODE_CLASS (mode) == MODE_CC
19054       || GET_MODE_CLASS (mode) == MODE_RANDOM
19055       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
19056     return 0;
19057   if (FP_REGNO_P (regno))
19058     return VALID_FP_MODE_P (mode);
19059   if (SSE_REGNO_P (regno))
19060     {
19061       /* We implement the move patterns for all vector modes into and
19062          out of SSE registers, even when no operation instructions
19063          are available.  */
19064       return (VALID_SSE_REG_MODE (mode)
19065               || VALID_SSE2_REG_MODE (mode)
19066               || VALID_MMX_REG_MODE (mode)
19067               || VALID_MMX_REG_MODE_3DNOW (mode));
19068     }
19069   if (MMX_REGNO_P (regno))
19070     {
19071       /* We implement the move patterns for 3DNOW modes even in MMX mode,
19072          so if the register is available at all, then we can move data of
19073          the given mode into or out of it.  */
19074       return (VALID_MMX_REG_MODE (mode)
19075               || VALID_MMX_REG_MODE_3DNOW (mode));
19076     }
19077
19078   if (mode == QImode)
19079     {
19080       /* Take care for QImode values - they can be in non-QI regs,
19081          but then they do cause partial register stalls.  */
19082       if (regno < 4 || TARGET_64BIT)
19083         return 1;
19084       if (!TARGET_PARTIAL_REG_STALL)
19085         return 1;
19086       return reload_in_progress || reload_completed;
19087     }
19088   /* We handle both integer and floats in the general purpose registers.  */
19089   else if (VALID_INT_MODE_P (mode))
19090     return 1;
19091   else if (VALID_FP_MODE_P (mode))
19092     return 1;
19093   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
19094      on to use that value in smaller contexts, this can easily force a
19095      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
19096      supporting DImode, allow it.  */
19097   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
19098     return 1;
19099
19100   return 0;
19101 }
19102
19103 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
19104    tieable integer mode.  */
19105
19106 static bool
19107 ix86_tieable_integer_mode_p (enum machine_mode mode)
19108 {
19109   switch (mode)
19110     {
19111     case HImode:
19112     case SImode:
19113       return true;
19114
19115     case QImode:
19116       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
19117
19118     case DImode:
19119       return TARGET_64BIT;
19120
19121     default:
19122       return false;
19123     }
19124 }
19125
19126 /* Return true if MODE1 is accessible in a register that can hold MODE2
19127    without copying.  That is, all register classes that can hold MODE2
19128    can also hold MODE1.  */
19129
19130 bool
19131 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
19132 {
19133   if (mode1 == mode2)
19134     return true;
19135
19136   if (ix86_tieable_integer_mode_p (mode1)
19137       && ix86_tieable_integer_mode_p (mode2))
19138     return true;
19139
19140   /* MODE2 being XFmode implies fp stack or general regs, which means we
19141      can tie any smaller floating point modes to it.  Note that we do not
19142      tie this with TFmode.  */
19143   if (mode2 == XFmode)
19144     return mode1 == SFmode || mode1 == DFmode;
19145
19146   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
19147      that we can tie it with SFmode.  */
19148   if (mode2 == DFmode)
19149     return mode1 == SFmode;
19150
19151   /* If MODE2 is only appropriate for an SSE register, then tie with
19152      any other mode acceptable to SSE registers.  */
19153   if (GET_MODE_SIZE (mode2) == 16
19154       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
19155     return (GET_MODE_SIZE (mode1) == 16
19156             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
19157
19158   /* If MODE2 is appropriate for an MMX register, then tie
19159      with any other mode acceptable to MMX registers.  */
19160   if (GET_MODE_SIZE (mode2) == 8
19161       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
19162     return (GET_MODE_SIZE (mode1) == 8
19163             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
19164
19165   return false;
19166 }
19167
19168 /* Return the cost of moving data of mode M between a
19169    register and memory.  A value of 2 is the default; this cost is
19170    relative to those in `REGISTER_MOVE_COST'.
19171
19172    If moving between registers and memory is more expensive than
19173    between two registers, you should define this macro to express the
19174    relative cost.
19175
19176    Model also increased moving costs of QImode registers in non
19177    Q_REGS classes.
19178  */
19179 int
19180 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
19181 {
19182   if (FLOAT_CLASS_P (class))
19183     {
19184       int index;
19185       switch (mode)
19186         {
19187           case SFmode:
19188             index = 0;
19189             break;
19190           case DFmode:
19191             index = 1;
19192             break;
19193           case XFmode:
19194             index = 2;
19195             break;
19196           default:
19197             return 100;
19198         }
19199       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
19200     }
19201   if (SSE_CLASS_P (class))
19202     {
19203       int index;
19204       switch (GET_MODE_SIZE (mode))
19205         {
19206           case 4:
19207             index = 0;
19208             break;
19209           case 8:
19210             index = 1;
19211             break;
19212           case 16:
19213             index = 2;
19214             break;
19215           default:
19216             return 100;
19217         }
19218       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
19219     }
19220   if (MMX_CLASS_P (class))
19221     {
19222       int index;
19223       switch (GET_MODE_SIZE (mode))
19224         {
19225           case 4:
19226             index = 0;
19227             break;
19228           case 8:
19229             index = 1;
19230             break;
19231           default:
19232             return 100;
19233         }
19234       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
19235     }
19236   switch (GET_MODE_SIZE (mode))
19237     {
19238       case 1:
19239         if (in)
19240           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
19241                   : ix86_cost->movzbl_load);
19242         else
19243           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
19244                   : ix86_cost->int_store[0] + 4);
19245         break;
19246       case 2:
19247         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
19248       default:
19249         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
19250         if (mode == TFmode)
19251           mode = XFmode;
19252         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
19253                 * (((int) GET_MODE_SIZE (mode)
19254                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
19255     }
19256 }
19257
19258 /* Compute a (partial) cost for rtx X.  Return true if the complete
19259    cost has been computed, and false if subexpressions should be
19260    scanned.  In either case, *TOTAL contains the cost result.  */
19261
19262 static bool
19263 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
19264 {
19265   enum machine_mode mode = GET_MODE (x);
19266
19267   switch (code)
19268     {
19269     case CONST_INT:
19270     case CONST:
19271     case LABEL_REF:
19272     case SYMBOL_REF:
19273       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
19274         *total = 3;
19275       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
19276         *total = 2;
19277       else if (flag_pic && SYMBOLIC_CONST (x)
19278                && (!TARGET_64BIT
19279                    || (!GET_CODE (x) != LABEL_REF
19280                        && (GET_CODE (x) != SYMBOL_REF
19281                            || !SYMBOL_REF_LOCAL_P (x)))))
19282         *total = 1;
19283       else
19284         *total = 0;
19285       return true;
19286
19287     case CONST_DOUBLE:
19288       if (mode == VOIDmode)
19289         *total = 0;
19290       else
19291         switch (standard_80387_constant_p (x))
19292           {
19293           case 1: /* 0.0 */
19294             *total = 1;
19295             break;
19296           default: /* Other constants */
19297             *total = 2;
19298             break;
19299           case 0:
19300           case -1:
19301             /* Start with (MEM (SYMBOL_REF)), since that's where
19302                it'll probably end up.  Add a penalty for size.  */
19303             *total = (COSTS_N_INSNS (1)
19304                       + (flag_pic != 0 && !TARGET_64BIT)
19305                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
19306             break;
19307           }
19308       return true;
19309
19310     case ZERO_EXTEND:
19311       /* The zero extensions is often completely free on x86_64, so make
19312          it as cheap as possible.  */
19313       if (TARGET_64BIT && mode == DImode
19314           && GET_MODE (XEXP (x, 0)) == SImode)
19315         *total = 1;
19316       else if (TARGET_ZERO_EXTEND_WITH_AND)
19317         *total = ix86_cost->add;
19318       else
19319         *total = ix86_cost->movzx;
19320       return false;
19321
19322     case SIGN_EXTEND:
19323       *total = ix86_cost->movsx;
19324       return false;
19325
19326     case ASHIFT:
19327       if (CONST_INT_P (XEXP (x, 1))
19328           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
19329         {
19330           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
19331           if (value == 1)
19332             {
19333               *total = ix86_cost->add;
19334               return false;
19335             }
19336           if ((value == 2 || value == 3)
19337               && ix86_cost->lea <= ix86_cost->shift_const)
19338             {
19339               *total = ix86_cost->lea;
19340               return false;
19341             }
19342         }
19343       /* FALLTHRU */
19344
19345     case ROTATE:
19346     case ASHIFTRT:
19347     case LSHIFTRT:
19348     case ROTATERT:
19349       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
19350         {
19351           if (CONST_INT_P (XEXP (x, 1)))
19352             {
19353               if (INTVAL (XEXP (x, 1)) > 32)
19354                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
19355               else
19356                 *total = ix86_cost->shift_const * 2;
19357             }
19358           else
19359             {
19360               if (GET_CODE (XEXP (x, 1)) == AND)
19361                 *total = ix86_cost->shift_var * 2;
19362               else
19363                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
19364             }
19365         }
19366       else
19367         {
19368           if (CONST_INT_P (XEXP (x, 1)))
19369             *total = ix86_cost->shift_const;
19370           else
19371             *total = ix86_cost->shift_var;
19372         }
19373       return false;
19374
19375     case MULT:
19376       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19377         {
19378           /* ??? SSE scalar cost should be used here.  */
19379           *total = ix86_cost->fmul;
19380           return false;
19381         }
19382       else if (X87_FLOAT_MODE_P (mode))
19383         {
19384           *total = ix86_cost->fmul;
19385           return false;
19386         }
19387       else if (FLOAT_MODE_P (mode))
19388         {
19389           /* ??? SSE vector cost should be used here.  */
19390           *total = ix86_cost->fmul;
19391           return false;
19392         }
19393       else
19394         {
19395           rtx op0 = XEXP (x, 0);
19396           rtx op1 = XEXP (x, 1);
19397           int nbits;
19398           if (CONST_INT_P (XEXP (x, 1)))
19399             {
19400               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
19401               for (nbits = 0; value != 0; value &= value - 1)
19402                 nbits++;
19403             }
19404           else
19405             /* This is arbitrary.  */
19406             nbits = 7;
19407
19408           /* Compute costs correctly for widening multiplication.  */
19409           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
19410               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
19411                  == GET_MODE_SIZE (mode))
19412             {
19413               int is_mulwiden = 0;
19414               enum machine_mode inner_mode = GET_MODE (op0);
19415
19416               if (GET_CODE (op0) == GET_CODE (op1))
19417                 is_mulwiden = 1, op1 = XEXP (op1, 0);
19418               else if (CONST_INT_P (op1))
19419                 {
19420                   if (GET_CODE (op0) == SIGN_EXTEND)
19421                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
19422                                   == INTVAL (op1);
19423                   else
19424                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
19425                 }
19426
19427               if (is_mulwiden)
19428                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
19429             }
19430
19431           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
19432                     + nbits * ix86_cost->mult_bit
19433                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
19434
19435           return true;
19436         }
19437
19438     case DIV:
19439     case UDIV:
19440     case MOD:
19441     case UMOD:
19442       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19443         /* ??? SSE cost should be used here.  */
19444         *total = ix86_cost->fdiv;
19445       else if (X87_FLOAT_MODE_P (mode))
19446         *total = ix86_cost->fdiv;
19447       else if (FLOAT_MODE_P (mode))
19448         /* ??? SSE vector cost should be used here.  */
19449         *total = ix86_cost->fdiv;
19450       else
19451         *total = ix86_cost->divide[MODE_INDEX (mode)];
19452       return false;
19453
19454     case PLUS:
19455       if (GET_MODE_CLASS (mode) == MODE_INT
19456                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
19457         {
19458           if (GET_CODE (XEXP (x, 0)) == PLUS
19459               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
19460               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
19461               && CONSTANT_P (XEXP (x, 1)))
19462             {
19463               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
19464               if (val == 2 || val == 4 || val == 8)
19465                 {
19466                   *total = ix86_cost->lea;
19467                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19468                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
19469                                       outer_code);
19470                   *total += rtx_cost (XEXP (x, 1), outer_code);
19471                   return true;
19472                 }
19473             }
19474           else if (GET_CODE (XEXP (x, 0)) == MULT
19475                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
19476             {
19477               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
19478               if (val == 2 || val == 4 || val == 8)
19479                 {
19480                   *total = ix86_cost->lea;
19481                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19482                   *total += rtx_cost (XEXP (x, 1), outer_code);
19483                   return true;
19484                 }
19485             }
19486           else if (GET_CODE (XEXP (x, 0)) == PLUS)
19487             {
19488               *total = ix86_cost->lea;
19489               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19490               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19491               *total += rtx_cost (XEXP (x, 1), outer_code);
19492               return true;
19493             }
19494         }
19495       /* FALLTHRU */
19496
19497     case MINUS:
19498       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19499         {
19500           /* ??? SSE cost should be used here.  */
19501           *total = ix86_cost->fadd;
19502           return false;
19503         }
19504       else if (X87_FLOAT_MODE_P (mode))
19505         {
19506           *total = ix86_cost->fadd;
19507           return false;
19508         }
19509       else if (FLOAT_MODE_P (mode))
19510         {
19511           /* ??? SSE vector cost should be used here.  */
19512           *total = ix86_cost->fadd;
19513           return false;
19514         }
19515       /* FALLTHRU */
19516
19517     case AND:
19518     case IOR:
19519     case XOR:
19520       if (!TARGET_64BIT && mode == DImode)
19521         {
19522           *total = (ix86_cost->add * 2
19523                     + (rtx_cost (XEXP (x, 0), outer_code)
19524                        << (GET_MODE (XEXP (x, 0)) != DImode))
19525                     + (rtx_cost (XEXP (x, 1), outer_code)
19526                        << (GET_MODE (XEXP (x, 1)) != DImode)));
19527           return true;
19528         }
19529       /* FALLTHRU */
19530
19531     case NEG:
19532       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19533         {
19534           /* ??? SSE cost should be used here.  */
19535           *total = ix86_cost->fchs;
19536           return false;
19537         }
19538       else if (X87_FLOAT_MODE_P (mode))
19539         {
19540           *total = ix86_cost->fchs;
19541           return false;
19542         }
19543       else if (FLOAT_MODE_P (mode))
19544         {
19545           /* ??? SSE vector cost should be used here.  */
19546           *total = ix86_cost->fchs;
19547           return false;
19548         }
19549       /* FALLTHRU */
19550
19551     case NOT:
19552       if (!TARGET_64BIT && mode == DImode)
19553         *total = ix86_cost->add * 2;
19554       else
19555         *total = ix86_cost->add;
19556       return false;
19557
19558     case COMPARE:
19559       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
19560           && XEXP (XEXP (x, 0), 1) == const1_rtx
19561           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
19562           && XEXP (x, 1) == const0_rtx)
19563         {
19564           /* This kind of construct is implemented using test[bwl].
19565              Treat it as if we had an AND.  */
19566           *total = (ix86_cost->add
19567                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
19568                     + rtx_cost (const1_rtx, outer_code));
19569           return true;
19570         }
19571       return false;
19572
19573     case FLOAT_EXTEND:
19574       if (!(SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH))
19575         *total = 0;
19576       return false;
19577
19578     case ABS:
19579       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19580         /* ??? SSE cost should be used here.  */
19581         *total = ix86_cost->fabs;
19582       else if (X87_FLOAT_MODE_P (mode))
19583         *total = ix86_cost->fabs;
19584       else if (FLOAT_MODE_P (mode))
19585         /* ??? SSE vector cost should be used here.  */
19586         *total = ix86_cost->fabs;
19587       return false;
19588
19589     case SQRT:
19590       if (SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH)
19591         /* ??? SSE cost should be used here.  */
19592         *total = ix86_cost->fsqrt;
19593       else if (X87_FLOAT_MODE_P (mode))
19594         *total = ix86_cost->fsqrt;
19595       else if (FLOAT_MODE_P (mode))
19596         /* ??? SSE vector cost should be used here.  */
19597         *total = ix86_cost->fsqrt;
19598       return false;
19599
19600     case UNSPEC:
19601       if (XINT (x, 1) == UNSPEC_TP)
19602         *total = 0;
19603       return false;
19604
19605     default:
19606       return false;
19607     }
19608 }
19609
19610 #if TARGET_MACHO
19611
19612 static int current_machopic_label_num;
19613
19614 /* Given a symbol name and its associated stub, write out the
19615    definition of the stub.  */
19616
19617 void
19618 machopic_output_stub (FILE *file, const char *symb, const char *stub)
19619 {
19620   unsigned int length;
19621   char *binder_name, *symbol_name, lazy_ptr_name[32];
19622   int label = ++current_machopic_label_num;
19623
19624   /* For 64-bit we shouldn't get here.  */
19625   gcc_assert (!TARGET_64BIT);
19626
19627   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
19628   symb = (*targetm.strip_name_encoding) (symb);
19629
19630   length = strlen (stub);
19631   binder_name = alloca (length + 32);
19632   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
19633
19634   length = strlen (symb);
19635   symbol_name = alloca (length + 32);
19636   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
19637
19638   sprintf (lazy_ptr_name, "L%d$lz", label);
19639
19640   if (MACHOPIC_PURE)
19641     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
19642   else
19643     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
19644
19645   fprintf (file, "%s:\n", stub);
19646   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19647
19648   if (MACHOPIC_PURE)
19649     {
19650       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
19651       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
19652       fprintf (file, "\tjmp\t*%%edx\n");
19653     }
19654   else
19655     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
19656
19657   fprintf (file, "%s:\n", binder_name);
19658
19659   if (MACHOPIC_PURE)
19660     {
19661       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
19662       fprintf (file, "\tpushl\t%%eax\n");
19663     }
19664   else
19665     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
19666
19667   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
19668
19669   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
19670   fprintf (file, "%s:\n", lazy_ptr_name);
19671   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19672   fprintf (file, "\t.long %s\n", binder_name);
19673 }
19674
19675 void
19676 darwin_x86_file_end (void)
19677 {
19678   darwin_file_end ();
19679   ix86_file_end ();
19680 }
19681 #endif /* TARGET_MACHO */
19682
19683 /* Order the registers for register allocator.  */
19684
19685 void
19686 x86_order_regs_for_local_alloc (void)
19687 {
19688    int pos = 0;
19689    int i;
19690
19691    /* First allocate the local general purpose registers.  */
19692    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19693      if (GENERAL_REGNO_P (i) && call_used_regs[i])
19694         reg_alloc_order [pos++] = i;
19695
19696    /* Global general purpose registers.  */
19697    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19698      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
19699         reg_alloc_order [pos++] = i;
19700
19701    /* x87 registers come first in case we are doing FP math
19702       using them.  */
19703    if (!TARGET_SSE_MATH)
19704      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19705        reg_alloc_order [pos++] = i;
19706
19707    /* SSE registers.  */
19708    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
19709      reg_alloc_order [pos++] = i;
19710    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
19711      reg_alloc_order [pos++] = i;
19712
19713    /* x87 registers.  */
19714    if (TARGET_SSE_MATH)
19715      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19716        reg_alloc_order [pos++] = i;
19717
19718    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
19719      reg_alloc_order [pos++] = i;
19720
19721    /* Initialize the rest of array as we do not allocate some registers
19722       at all.  */
19723    while (pos < FIRST_PSEUDO_REGISTER)
19724      reg_alloc_order [pos++] = 0;
19725 }
19726
19727 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
19728    struct attribute_spec.handler.  */
19729 static tree
19730 ix86_handle_struct_attribute (tree *node, tree name,
19731                               tree args ATTRIBUTE_UNUSED,
19732                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
19733 {
19734   tree *type = NULL;
19735   if (DECL_P (*node))
19736     {
19737       if (TREE_CODE (*node) == TYPE_DECL)
19738         type = &TREE_TYPE (*node);
19739     }
19740   else
19741     type = node;
19742
19743   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
19744                  || TREE_CODE (*type) == UNION_TYPE)))
19745     {
19746       warning (OPT_Wattributes, "%qs attribute ignored",
19747                IDENTIFIER_POINTER (name));
19748       *no_add_attrs = true;
19749     }
19750
19751   else if ((is_attribute_p ("ms_struct", name)
19752             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
19753            || ((is_attribute_p ("gcc_struct", name)
19754                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
19755     {
19756       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
19757                IDENTIFIER_POINTER (name));
19758       *no_add_attrs = true;
19759     }
19760
19761   return NULL_TREE;
19762 }
19763
19764 static bool
19765 ix86_ms_bitfield_layout_p (tree record_type)
19766 {
19767   return (TARGET_MS_BITFIELD_LAYOUT &&
19768           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
19769     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
19770 }
19771
19772 /* Returns an expression indicating where the this parameter is
19773    located on entry to the FUNCTION.  */
19774
19775 static rtx
19776 x86_this_parameter (tree function)
19777 {
19778   tree type = TREE_TYPE (function);
19779   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
19780
19781   if (TARGET_64BIT)
19782     {
19783       const int *parm_regs;
19784
19785       if (TARGET_64BIT_MS_ABI)
19786         parm_regs = x86_64_ms_abi_int_parameter_registers;
19787       else
19788         parm_regs = x86_64_int_parameter_registers;
19789       return gen_rtx_REG (DImode, parm_regs[aggr]);
19790     }
19791
19792   if (ix86_function_regparm (type, function) > 0
19793       && !type_has_variadic_args_p (type))
19794     {
19795       int regno = 0;
19796       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
19797         regno = 2;
19798       return gen_rtx_REG (SImode, regno);
19799     }
19800
19801   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
19802 }
19803
19804 /* Determine whether x86_output_mi_thunk can succeed.  */
19805
19806 static bool
19807 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
19808                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
19809                          HOST_WIDE_INT vcall_offset, tree function)
19810 {
19811   /* 64-bit can handle anything.  */
19812   if (TARGET_64BIT)
19813     return true;
19814
19815   /* For 32-bit, everything's fine if we have one free register.  */
19816   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
19817     return true;
19818
19819   /* Need a free register for vcall_offset.  */
19820   if (vcall_offset)
19821     return false;
19822
19823   /* Need a free register for GOT references.  */
19824   if (flag_pic && !(*targetm.binds_local_p) (function))
19825     return false;
19826
19827   /* Otherwise ok.  */
19828   return true;
19829 }
19830
19831 /* Output the assembler code for a thunk function.  THUNK_DECL is the
19832    declaration for the thunk function itself, FUNCTION is the decl for
19833    the target function.  DELTA is an immediate constant offset to be
19834    added to THIS.  If VCALL_OFFSET is nonzero, the word at
19835    *(*this + vcall_offset) should be added to THIS.  */
19836
19837 static void
19838 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
19839                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
19840                      HOST_WIDE_INT vcall_offset, tree function)
19841 {
19842   rtx xops[3];
19843   rtx this = x86_this_parameter (function);
19844   rtx this_reg, tmp;
19845
19846   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
19847      pull it in now and let DELTA benefit.  */
19848   if (REG_P (this))
19849     this_reg = this;
19850   else if (vcall_offset)
19851     {
19852       /* Put the this parameter into %eax.  */
19853       xops[0] = this;
19854       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
19855       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19856     }
19857   else
19858     this_reg = NULL_RTX;
19859
19860   /* Adjust the this parameter by a fixed constant.  */
19861   if (delta)
19862     {
19863       xops[0] = GEN_INT (delta);
19864       xops[1] = this_reg ? this_reg : this;
19865       if (TARGET_64BIT)
19866         {
19867           if (!x86_64_general_operand (xops[0], DImode))
19868             {
19869               tmp = gen_rtx_REG (DImode, R10_REG);
19870               xops[1] = tmp;
19871               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
19872               xops[0] = tmp;
19873               xops[1] = this;
19874             }
19875           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19876         }
19877       else
19878         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19879     }
19880
19881   /* Adjust the this parameter by a value stored in the vtable.  */
19882   if (vcall_offset)
19883     {
19884       if (TARGET_64BIT)
19885         tmp = gen_rtx_REG (DImode, R10_REG);
19886       else
19887         {
19888           int tmp_regno = 2 /* ECX */;
19889           if (lookup_attribute ("fastcall",
19890                                 TYPE_ATTRIBUTES (TREE_TYPE (function))))
19891             tmp_regno = 0 /* EAX */;
19892           tmp = gen_rtx_REG (SImode, tmp_regno);
19893         }
19894
19895       xops[0] = gen_rtx_MEM (Pmode, this_reg);
19896       xops[1] = tmp;
19897       if (TARGET_64BIT)
19898         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19899       else
19900         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19901
19902       /* Adjust the this parameter.  */
19903       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
19904       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
19905         {
19906           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
19907           xops[0] = GEN_INT (vcall_offset);
19908           xops[1] = tmp2;
19909           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19910           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
19911         }
19912       xops[1] = this_reg;
19913       if (TARGET_64BIT)
19914         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19915       else
19916         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19917     }
19918
19919   /* If necessary, drop THIS back to its stack slot.  */
19920   if (this_reg && this_reg != this)
19921     {
19922       xops[0] = this_reg;
19923       xops[1] = this;
19924       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19925     }
19926
19927   xops[0] = XEXP (DECL_RTL (function), 0);
19928   if (TARGET_64BIT)
19929     {
19930       if (!flag_pic || (*targetm.binds_local_p) (function))
19931         output_asm_insn ("jmp\t%P0", xops);
19932       /* All thunks should be in the same object as their target,
19933          and thus binds_local_p should be true.  */
19934       else if (TARGET_64BIT_MS_ABI)
19935         gcc_unreachable ();
19936       else
19937         {
19938           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
19939           tmp = gen_rtx_CONST (Pmode, tmp);
19940           tmp = gen_rtx_MEM (QImode, tmp);
19941           xops[0] = tmp;
19942           output_asm_insn ("jmp\t%A0", xops);
19943         }
19944     }
19945   else
19946     {
19947       if (!flag_pic || (*targetm.binds_local_p) (function))
19948         output_asm_insn ("jmp\t%P0", xops);
19949       else
19950 #if TARGET_MACHO
19951         if (TARGET_MACHO)
19952           {
19953             rtx sym_ref = XEXP (DECL_RTL (function), 0);
19954             tmp = (gen_rtx_SYMBOL_REF
19955                    (Pmode,
19956                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
19957             tmp = gen_rtx_MEM (QImode, tmp);
19958             xops[0] = tmp;
19959             output_asm_insn ("jmp\t%0", xops);
19960           }
19961         else
19962 #endif /* TARGET_MACHO */
19963         {
19964           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
19965           output_set_got (tmp, NULL_RTX);
19966
19967           xops[1] = tmp;
19968           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
19969           output_asm_insn ("jmp\t{*}%1", xops);
19970         }
19971     }
19972 }
19973
19974 static void
19975 x86_file_start (void)
19976 {
19977   default_file_start ();
19978 #if TARGET_MACHO
19979   darwin_file_start ();
19980 #endif
19981   if (X86_FILE_START_VERSION_DIRECTIVE)
19982     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
19983   if (X86_FILE_START_FLTUSED)
19984     fputs ("\t.global\t__fltused\n", asm_out_file);
19985   if (ix86_asm_dialect == ASM_INTEL)
19986     fputs ("\t.intel_syntax\n", asm_out_file);
19987 }
19988
19989 int
19990 x86_field_alignment (tree field, int computed)
19991 {
19992   enum machine_mode mode;
19993   tree type = TREE_TYPE (field);
19994
19995   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
19996     return computed;
19997   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
19998                     ? get_inner_array_type (type) : type);
19999   if (mode == DFmode || mode == DCmode
20000       || GET_MODE_CLASS (mode) == MODE_INT
20001       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
20002     return MIN (32, computed);
20003   return computed;
20004 }
20005
20006 /* Output assembler code to FILE to increment profiler label # LABELNO
20007    for profiling a function entry.  */
20008 void
20009 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
20010 {
20011   if (TARGET_64BIT)
20012     {
20013 #ifndef NO_PROFILE_COUNTERS
20014       fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
20015 #endif
20016
20017       if (!TARGET_64BIT_MS_ABI && flag_pic)
20018         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
20019       else
20020         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
20021     }
20022   else if (flag_pic)
20023     {
20024 #ifndef NO_PROFILE_COUNTERS
20025       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
20026                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
20027 #endif
20028       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
20029     }
20030   else
20031     {
20032 #ifndef NO_PROFILE_COUNTERS
20033       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
20034                PROFILE_COUNT_REGISTER);
20035 #endif
20036       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
20037     }
20038 }
20039
20040 /* We don't have exact information about the insn sizes, but we may assume
20041    quite safely that we are informed about all 1 byte insns and memory
20042    address sizes.  This is enough to eliminate unnecessary padding in
20043    99% of cases.  */
20044
20045 static int
20046 min_insn_size (rtx insn)
20047 {
20048   int l = 0;
20049
20050   if (!INSN_P (insn) || !active_insn_p (insn))
20051     return 0;
20052
20053   /* Discard alignments we've emit and jump instructions.  */
20054   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
20055       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
20056     return 0;
20057   if (JUMP_P (insn)
20058       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
20059           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
20060     return 0;
20061
20062   /* Important case - calls are always 5 bytes.
20063      It is common to have many calls in the row.  */
20064   if (CALL_P (insn)
20065       && symbolic_reference_mentioned_p (PATTERN (insn))
20066       && !SIBLING_CALL_P (insn))
20067     return 5;
20068   if (get_attr_length (insn) <= 1)
20069     return 1;
20070
20071   /* For normal instructions we may rely on the sizes of addresses
20072      and the presence of symbol to require 4 bytes of encoding.
20073      This is not the case for jumps where references are PC relative.  */
20074   if (!JUMP_P (insn))
20075     {
20076       l = get_attr_length_address (insn);
20077       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
20078         l = 4;
20079     }
20080   if (l)
20081     return 1+l;
20082   else
20083     return 2;
20084 }
20085
20086 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
20087    window.  */
20088
20089 static void
20090 ix86_avoid_jump_misspredicts (void)
20091 {
20092   rtx insn, start = get_insns ();
20093   int nbytes = 0, njumps = 0;
20094   int isjump = 0;
20095
20096   /* Look for all minimal intervals of instructions containing 4 jumps.
20097      The intervals are bounded by START and INSN.  NBYTES is the total
20098      size of instructions in the interval including INSN and not including
20099      START.  When the NBYTES is smaller than 16 bytes, it is possible
20100      that the end of START and INSN ends up in the same 16byte page.
20101
20102      The smallest offset in the page INSN can start is the case where START
20103      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
20104      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
20105      */
20106   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20107     {
20108
20109       nbytes += min_insn_size (insn);
20110       if (dump_file)
20111         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
20112                 INSN_UID (insn), min_insn_size (insn));
20113       if ((JUMP_P (insn)
20114            && GET_CODE (PATTERN (insn)) != ADDR_VEC
20115            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
20116           || CALL_P (insn))
20117         njumps++;
20118       else
20119         continue;
20120
20121       while (njumps > 3)
20122         {
20123           start = NEXT_INSN (start);
20124           if ((JUMP_P (start)
20125                && GET_CODE (PATTERN (start)) != ADDR_VEC
20126                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
20127               || CALL_P (start))
20128             njumps--, isjump = 1;
20129           else
20130             isjump = 0;
20131           nbytes -= min_insn_size (start);
20132         }
20133       gcc_assert (njumps >= 0);
20134       if (dump_file)
20135         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
20136                 INSN_UID (start), INSN_UID (insn), nbytes);
20137
20138       if (njumps == 3 && isjump && nbytes < 16)
20139         {
20140           int padsize = 15 - nbytes + min_insn_size (insn);
20141
20142           if (dump_file)
20143             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
20144                      INSN_UID (insn), padsize);
20145           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
20146         }
20147     }
20148 }
20149
20150 /* AMD Athlon works faster
20151    when RET is not destination of conditional jump or directly preceded
20152    by other jump instruction.  We avoid the penalty by inserting NOP just
20153    before the RET instructions in such cases.  */
20154 static void
20155 ix86_pad_returns (void)
20156 {
20157   edge e;
20158   edge_iterator ei;
20159
20160   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
20161     {
20162       basic_block bb = e->src;
20163       rtx ret = BB_END (bb);
20164       rtx prev;
20165       bool replace = false;
20166
20167       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
20168           || !maybe_hot_bb_p (bb))
20169         continue;
20170       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
20171         if (active_insn_p (prev) || LABEL_P (prev))
20172           break;
20173       if (prev && LABEL_P (prev))
20174         {
20175           edge e;
20176           edge_iterator ei;
20177
20178           FOR_EACH_EDGE (e, ei, bb->preds)
20179             if (EDGE_FREQUENCY (e) && e->src->index >= 0
20180                 && !(e->flags & EDGE_FALLTHRU))
20181               replace = true;
20182         }
20183       if (!replace)
20184         {
20185           prev = prev_active_insn (ret);
20186           if (prev
20187               && ((JUMP_P (prev) && any_condjump_p (prev))
20188                   || CALL_P (prev)))
20189             replace = true;
20190           /* Empty functions get branch mispredict even when the jump destination
20191              is not visible to us.  */
20192           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
20193             replace = true;
20194         }
20195       if (replace)
20196         {
20197           emit_insn_before (gen_return_internal_long (), ret);
20198           delete_insn (ret);
20199         }
20200     }
20201 }
20202
20203 /* Implement machine specific optimizations.  We implement padding of returns
20204    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
20205 static void
20206 ix86_reorg (void)
20207 {
20208   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
20209     ix86_pad_returns ();
20210   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
20211     ix86_avoid_jump_misspredicts ();
20212 }
20213
20214 /* Return nonzero when QImode register that must be represented via REX prefix
20215    is used.  */
20216 bool
20217 x86_extended_QIreg_mentioned_p (rtx insn)
20218 {
20219   int i;
20220   extract_insn_cached (insn);
20221   for (i = 0; i < recog_data.n_operands; i++)
20222     if (REG_P (recog_data.operand[i])
20223         && REGNO (recog_data.operand[i]) >= 4)
20224        return true;
20225   return false;
20226 }
20227
20228 /* Return nonzero when P points to register encoded via REX prefix.
20229    Called via for_each_rtx.  */
20230 static int
20231 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
20232 {
20233    unsigned int regno;
20234    if (!REG_P (*p))
20235      return 0;
20236    regno = REGNO (*p);
20237    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
20238 }
20239
20240 /* Return true when INSN mentions register that must be encoded using REX
20241    prefix.  */
20242 bool
20243 x86_extended_reg_mentioned_p (rtx insn)
20244 {
20245   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
20246 }
20247
20248 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
20249    optabs would emit if we didn't have TFmode patterns.  */
20250
20251 void
20252 x86_emit_floatuns (rtx operands[2])
20253 {
20254   rtx neglab, donelab, i0, i1, f0, in, out;
20255   enum machine_mode mode, inmode;
20256
20257   inmode = GET_MODE (operands[1]);
20258   gcc_assert (inmode == SImode || inmode == DImode);
20259
20260   out = operands[0];
20261   in = force_reg (inmode, operands[1]);
20262   mode = GET_MODE (out);
20263   neglab = gen_label_rtx ();
20264   donelab = gen_label_rtx ();
20265   f0 = gen_reg_rtx (mode);
20266
20267   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
20268
20269   expand_float (out, in, 0);
20270
20271   emit_jump_insn (gen_jump (donelab));
20272   emit_barrier ();
20273
20274   emit_label (neglab);
20275
20276   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
20277                             1, OPTAB_DIRECT);
20278   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
20279                             1, OPTAB_DIRECT);
20280   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
20281
20282   expand_float (f0, i0, 0);
20283
20284   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
20285
20286   emit_label (donelab);
20287 }
20288 \f
20289 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20290    with all elements equal to VAR.  Return true if successful.  */
20291
20292 static bool
20293 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
20294                                    rtx target, rtx val)
20295 {
20296   enum machine_mode smode, wsmode, wvmode;
20297   rtx x;
20298
20299   switch (mode)
20300     {
20301     case V2SImode:
20302     case V2SFmode:
20303       if (!mmx_ok)
20304         return false;
20305       /* FALLTHRU */
20306
20307     case V2DFmode:
20308     case V2DImode:
20309     case V4SFmode:
20310     case V4SImode:
20311       val = force_reg (GET_MODE_INNER (mode), val);
20312       x = gen_rtx_VEC_DUPLICATE (mode, val);
20313       emit_insn (gen_rtx_SET (VOIDmode, target, x));
20314       return true;
20315
20316     case V4HImode:
20317       if (!mmx_ok)
20318         return false;
20319       if (TARGET_SSE || TARGET_3DNOW_A)
20320         {
20321           val = gen_lowpart (SImode, val);
20322           x = gen_rtx_TRUNCATE (HImode, val);
20323           x = gen_rtx_VEC_DUPLICATE (mode, x);
20324           emit_insn (gen_rtx_SET (VOIDmode, target, x));
20325           return true;
20326         }
20327       else
20328         {
20329           smode = HImode;
20330           wsmode = SImode;
20331           wvmode = V2SImode;
20332           goto widen;
20333         }
20334
20335     case V8QImode:
20336       if (!mmx_ok)
20337         return false;
20338       smode = QImode;
20339       wsmode = HImode;
20340       wvmode = V4HImode;
20341       goto widen;
20342     case V8HImode:
20343       if (TARGET_SSE2)
20344         {
20345           rtx tmp1, tmp2;
20346           /* Extend HImode to SImode using a paradoxical SUBREG.  */
20347           tmp1 = gen_reg_rtx (SImode);
20348           emit_move_insn (tmp1, gen_lowpart (SImode, val));
20349           /* Insert the SImode value as low element of V4SImode vector. */
20350           tmp2 = gen_reg_rtx (V4SImode);
20351           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
20352                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
20353                                     CONST0_RTX (V4SImode),
20354                                     const1_rtx);
20355           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
20356           /* Cast the V4SImode vector back to a V8HImode vector.  */
20357           tmp1 = gen_reg_rtx (V8HImode);
20358           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
20359           /* Duplicate the low short through the whole low SImode word.  */
20360           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
20361           /* Cast the V8HImode vector back to a V4SImode vector.  */
20362           tmp2 = gen_reg_rtx (V4SImode);
20363           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
20364           /* Replicate the low element of the V4SImode vector.  */
20365           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
20366           /* Cast the V2SImode back to V8HImode, and store in target.  */
20367           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
20368           return true;
20369         }
20370       smode = HImode;
20371       wsmode = SImode;
20372       wvmode = V4SImode;
20373       goto widen;
20374     case V16QImode:
20375       if (TARGET_SSE2)
20376         {
20377           rtx tmp1, tmp2;
20378           /* Extend QImode to SImode using a paradoxical SUBREG.  */
20379           tmp1 = gen_reg_rtx (SImode);
20380           emit_move_insn (tmp1, gen_lowpart (SImode, val));
20381           /* Insert the SImode value as low element of V4SImode vector. */
20382           tmp2 = gen_reg_rtx (V4SImode);
20383           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
20384                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
20385                                     CONST0_RTX (V4SImode),
20386                                     const1_rtx);
20387           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
20388           /* Cast the V4SImode vector back to a V16QImode vector.  */
20389           tmp1 = gen_reg_rtx (V16QImode);
20390           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
20391           /* Duplicate the low byte through the whole low SImode word.  */
20392           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20393           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20394           /* Cast the V16QImode vector back to a V4SImode vector.  */
20395           tmp2 = gen_reg_rtx (V4SImode);
20396           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
20397           /* Replicate the low element of the V4SImode vector.  */
20398           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
20399           /* Cast the V2SImode back to V16QImode, and store in target.  */
20400           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
20401           return true;
20402         }
20403       smode = QImode;
20404       wsmode = HImode;
20405       wvmode = V8HImode;
20406       goto widen;
20407     widen:
20408       /* Replicate the value once into the next wider mode and recurse.  */
20409       val = convert_modes (wsmode, smode, val, true);
20410       x = expand_simple_binop (wsmode, ASHIFT, val,
20411                                GEN_INT (GET_MODE_BITSIZE (smode)),
20412                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
20413       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
20414
20415       x = gen_reg_rtx (wvmode);
20416       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
20417         gcc_unreachable ();
20418       emit_move_insn (target, gen_lowpart (mode, x));
20419       return true;
20420
20421     default:
20422       return false;
20423     }
20424 }
20425
20426 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20427    whose ONE_VAR element is VAR, and other elements are zero.  Return true
20428    if successful.  */
20429
20430 static bool
20431 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
20432                                      rtx target, rtx var, int one_var)
20433 {
20434   enum machine_mode vsimode;
20435   rtx new_target;
20436   rtx x, tmp;
20437
20438   switch (mode)
20439     {
20440     case V2SFmode:
20441     case V2SImode:
20442       if (!mmx_ok)
20443         return false;
20444       /* FALLTHRU */
20445
20446     case V2DFmode:
20447     case V2DImode:
20448       if (one_var != 0)
20449         return false;
20450       var = force_reg (GET_MODE_INNER (mode), var);
20451       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
20452       emit_insn (gen_rtx_SET (VOIDmode, target, x));
20453       return true;
20454
20455     case V4SFmode:
20456     case V4SImode:
20457       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
20458         new_target = gen_reg_rtx (mode);
20459       else
20460         new_target = target;
20461       var = force_reg (GET_MODE_INNER (mode), var);
20462       x = gen_rtx_VEC_DUPLICATE (mode, var);
20463       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
20464       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
20465       if (one_var != 0)
20466         {
20467           /* We need to shuffle the value to the correct position, so
20468              create a new pseudo to store the intermediate result.  */
20469
20470           /* With SSE2, we can use the integer shuffle insns.  */
20471           if (mode != V4SFmode && TARGET_SSE2)
20472             {
20473               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
20474                                             GEN_INT (1),
20475                                             GEN_INT (one_var == 1 ? 0 : 1),
20476                                             GEN_INT (one_var == 2 ? 0 : 1),
20477                                             GEN_INT (one_var == 3 ? 0 : 1)));
20478               if (target != new_target)
20479                 emit_move_insn (target, new_target);
20480               return true;
20481             }
20482
20483           /* Otherwise convert the intermediate result to V4SFmode and
20484              use the SSE1 shuffle instructions.  */
20485           if (mode != V4SFmode)
20486             {
20487               tmp = gen_reg_rtx (V4SFmode);
20488               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
20489             }
20490           else
20491             tmp = new_target;
20492
20493           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
20494                                        GEN_INT (1),
20495                                        GEN_INT (one_var == 1 ? 0 : 1),
20496                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
20497                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
20498
20499           if (mode != V4SFmode)
20500             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
20501           else if (tmp != target)
20502             emit_move_insn (target, tmp);
20503         }
20504       else if (target != new_target)
20505         emit_move_insn (target, new_target);
20506       return true;
20507
20508     case V8HImode:
20509     case V16QImode:
20510       vsimode = V4SImode;
20511       goto widen;
20512     case V4HImode:
20513     case V8QImode:
20514       if (!mmx_ok)
20515         return false;
20516       vsimode = V2SImode;
20517       goto widen;
20518     widen:
20519       if (one_var != 0)
20520         return false;
20521
20522       /* Zero extend the variable element to SImode and recurse.  */
20523       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
20524
20525       x = gen_reg_rtx (vsimode);
20526       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
20527                                                 var, one_var))
20528         gcc_unreachable ();
20529
20530       emit_move_insn (target, gen_lowpart (mode, x));
20531       return true;
20532
20533     default:
20534       return false;
20535     }
20536 }
20537
20538 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20539    consisting of the values in VALS.  It is known that all elements
20540    except ONE_VAR are constants.  Return true if successful.  */
20541
20542 static bool
20543 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
20544                                  rtx target, rtx vals, int one_var)
20545 {
20546   rtx var = XVECEXP (vals, 0, one_var);
20547   enum machine_mode wmode;
20548   rtx const_vec, x;
20549
20550   const_vec = copy_rtx (vals);
20551   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
20552   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
20553
20554   switch (mode)
20555     {
20556     case V2DFmode:
20557     case V2DImode:
20558     case V2SFmode:
20559     case V2SImode:
20560       /* For the two element vectors, it's just as easy to use
20561          the general case.  */
20562       return false;
20563
20564     case V4SFmode:
20565     case V4SImode:
20566     case V8HImode:
20567     case V4HImode:
20568       break;
20569
20570     case V16QImode:
20571       wmode = V8HImode;
20572       goto widen;
20573     case V8QImode:
20574       wmode = V4HImode;
20575       goto widen;
20576     widen:
20577       /* There's no way to set one QImode entry easily.  Combine
20578          the variable value with its adjacent constant value, and
20579          promote to an HImode set.  */
20580       x = XVECEXP (vals, 0, one_var ^ 1);
20581       if (one_var & 1)
20582         {
20583           var = convert_modes (HImode, QImode, var, true);
20584           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
20585                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
20586           x = GEN_INT (INTVAL (x) & 0xff);
20587         }
20588       else
20589         {
20590           var = convert_modes (HImode, QImode, var, true);
20591           x = gen_int_mode (INTVAL (x) << 8, HImode);
20592         }
20593       if (x != const0_rtx)
20594         var = expand_simple_binop (HImode, IOR, var, x, var,
20595                                    1, OPTAB_LIB_WIDEN);
20596
20597       x = gen_reg_rtx (wmode);
20598       emit_move_insn (x, gen_lowpart (wmode, const_vec));
20599       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
20600
20601       emit_move_insn (target, gen_lowpart (mode, x));
20602       return true;
20603
20604     default:
20605       return false;
20606     }
20607
20608   emit_move_insn (target, const_vec);
20609   ix86_expand_vector_set (mmx_ok, target, var, one_var);
20610   return true;
20611 }
20612
20613 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
20614    all values variable, and none identical.  */
20615
20616 static void
20617 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
20618                                  rtx target, rtx vals)
20619 {
20620   enum machine_mode half_mode = GET_MODE_INNER (mode);
20621   rtx op0 = NULL, op1 = NULL;
20622   bool use_vec_concat = false;
20623
20624   switch (mode)
20625     {
20626     case V2SFmode:
20627     case V2SImode:
20628       if (!mmx_ok && !TARGET_SSE)
20629         break;
20630       /* FALLTHRU */
20631
20632     case V2DFmode:
20633     case V2DImode:
20634       /* For the two element vectors, we always implement VEC_CONCAT.  */
20635       op0 = XVECEXP (vals, 0, 0);
20636       op1 = XVECEXP (vals, 0, 1);
20637       use_vec_concat = true;
20638       break;
20639
20640     case V4SFmode:
20641       half_mode = V2SFmode;
20642       goto half;
20643     case V4SImode:
20644       half_mode = V2SImode;
20645       goto half;
20646     half:
20647       {
20648         rtvec v;
20649
20650         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
20651            Recurse to load the two halves.  */
20652
20653         op0 = gen_reg_rtx (half_mode);
20654         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
20655         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
20656
20657         op1 = gen_reg_rtx (half_mode);
20658         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
20659         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
20660
20661         use_vec_concat = true;
20662       }
20663       break;
20664
20665     case V8HImode:
20666     case V16QImode:
20667     case V4HImode:
20668     case V8QImode:
20669       break;
20670
20671     default:
20672       gcc_unreachable ();
20673     }
20674
20675   if (use_vec_concat)
20676     {
20677       if (!register_operand (op0, half_mode))
20678         op0 = force_reg (half_mode, op0);
20679       if (!register_operand (op1, half_mode))
20680         op1 = force_reg (half_mode, op1);
20681
20682       emit_insn (gen_rtx_SET (VOIDmode, target,
20683                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
20684     }
20685   else
20686     {
20687       int i, j, n_elts, n_words, n_elt_per_word;
20688       enum machine_mode inner_mode;
20689       rtx words[4], shift;
20690
20691       inner_mode = GET_MODE_INNER (mode);
20692       n_elts = GET_MODE_NUNITS (mode);
20693       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
20694       n_elt_per_word = n_elts / n_words;
20695       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
20696
20697       for (i = 0; i < n_words; ++i)
20698         {
20699           rtx word = NULL_RTX;
20700
20701           for (j = 0; j < n_elt_per_word; ++j)
20702             {
20703               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
20704               elt = convert_modes (word_mode, inner_mode, elt, true);
20705
20706               if (j == 0)
20707                 word = elt;
20708               else
20709                 {
20710                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
20711                                               word, 1, OPTAB_LIB_WIDEN);
20712                   word = expand_simple_binop (word_mode, IOR, word, elt,
20713                                               word, 1, OPTAB_LIB_WIDEN);
20714                 }
20715             }
20716
20717           words[i] = word;
20718         }
20719
20720       if (n_words == 1)
20721         emit_move_insn (target, gen_lowpart (mode, words[0]));
20722       else if (n_words == 2)
20723         {
20724           rtx tmp = gen_reg_rtx (mode);
20725           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
20726           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
20727           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
20728           emit_move_insn (target, tmp);
20729         }
20730       else if (n_words == 4)
20731         {
20732           rtx tmp = gen_reg_rtx (V4SImode);
20733           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
20734           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
20735           emit_move_insn (target, gen_lowpart (mode, tmp));
20736         }
20737       else
20738         gcc_unreachable ();
20739     }
20740 }
20741
20742 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
20743    instructions unless MMX_OK is true.  */
20744
20745 void
20746 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
20747 {
20748   enum machine_mode mode = GET_MODE (target);
20749   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20750   int n_elts = GET_MODE_NUNITS (mode);
20751   int n_var = 0, one_var = -1;
20752   bool all_same = true, all_const_zero = true;
20753   int i;
20754   rtx x;
20755
20756   for (i = 0; i < n_elts; ++i)
20757     {
20758       x = XVECEXP (vals, 0, i);
20759       if (!CONSTANT_P (x))
20760         n_var++, one_var = i;
20761       else if (x != CONST0_RTX (inner_mode))
20762         all_const_zero = false;
20763       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
20764         all_same = false;
20765     }
20766
20767   /* Constants are best loaded from the constant pool.  */
20768   if (n_var == 0)
20769     {
20770       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
20771       return;
20772     }
20773
20774   /* If all values are identical, broadcast the value.  */
20775   if (all_same
20776       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
20777                                             XVECEXP (vals, 0, 0)))
20778     return;
20779
20780   /* Values where only one field is non-constant are best loaded from
20781      the pool and overwritten via move later.  */
20782   if (n_var == 1)
20783     {
20784       if (all_const_zero
20785           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
20786                                                   XVECEXP (vals, 0, one_var),
20787                                                   one_var))
20788         return;
20789
20790       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
20791         return;
20792     }
20793
20794   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
20795 }
20796
20797 void
20798 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
20799 {
20800   enum machine_mode mode = GET_MODE (target);
20801   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20802   bool use_vec_merge = false;
20803   rtx tmp;
20804
20805   switch (mode)
20806     {
20807     case V2SFmode:
20808     case V2SImode:
20809       if (mmx_ok)
20810         {
20811           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
20812           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
20813           if (elt == 0)
20814             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
20815           else
20816             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
20817           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20818           return;
20819         }
20820       break;
20821
20822     case V2DFmode:
20823     case V2DImode:
20824       {
20825         rtx op0, op1;
20826
20827         /* For the two element vectors, we implement a VEC_CONCAT with
20828            the extraction of the other element.  */
20829
20830         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
20831         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
20832
20833         if (elt == 0)
20834           op0 = val, op1 = tmp;
20835         else
20836           op0 = tmp, op1 = val;
20837
20838         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
20839         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20840       }
20841       return;
20842
20843     case V4SFmode:
20844       switch (elt)
20845         {
20846         case 0:
20847           use_vec_merge = true;
20848           break;
20849
20850         case 1:
20851           /* tmp = target = A B C D */
20852           tmp = copy_to_reg (target);
20853           /* target = A A B B */
20854           emit_insn (gen_sse_unpcklps (target, target, target));
20855           /* target = X A B B */
20856           ix86_expand_vector_set (false, target, val, 0);
20857           /* target = A X C D  */
20858           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20859                                        GEN_INT (1), GEN_INT (0),
20860                                        GEN_INT (2+4), GEN_INT (3+4)));
20861           return;
20862
20863         case 2:
20864           /* tmp = target = A B C D */
20865           tmp = copy_to_reg (target);
20866           /* tmp = X B C D */
20867           ix86_expand_vector_set (false, tmp, val, 0);
20868           /* target = A B X D */
20869           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20870                                        GEN_INT (0), GEN_INT (1),
20871                                        GEN_INT (0+4), GEN_INT (3+4)));
20872           return;
20873
20874         case 3:
20875           /* tmp = target = A B C D */
20876           tmp = copy_to_reg (target);
20877           /* tmp = X B C D */
20878           ix86_expand_vector_set (false, tmp, val, 0);
20879           /* target = A B X D */
20880           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20881                                        GEN_INT (0), GEN_INT (1),
20882                                        GEN_INT (2+4), GEN_INT (0+4)));
20883           return;
20884
20885         default:
20886           gcc_unreachable ();
20887         }
20888       break;
20889
20890     case V4SImode:
20891       /* Element 0 handled by vec_merge below.  */
20892       if (elt == 0)
20893         {
20894           use_vec_merge = true;
20895           break;
20896         }
20897
20898       if (TARGET_SSE2)
20899         {
20900           /* With SSE2, use integer shuffles to swap element 0 and ELT,
20901              store into element 0, then shuffle them back.  */
20902
20903           rtx order[4];
20904
20905           order[0] = GEN_INT (elt);
20906           order[1] = const1_rtx;
20907           order[2] = const2_rtx;
20908           order[3] = GEN_INT (3);
20909           order[elt] = const0_rtx;
20910
20911           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20912                                         order[1], order[2], order[3]));
20913
20914           ix86_expand_vector_set (false, target, val, 0);
20915
20916           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20917                                         order[1], order[2], order[3]));
20918         }
20919       else
20920         {
20921           /* For SSE1, we have to reuse the V4SF code.  */
20922           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
20923                                   gen_lowpart (SFmode, val), elt);
20924         }
20925       return;
20926
20927     case V8HImode:
20928       use_vec_merge = TARGET_SSE2;
20929       break;
20930     case V4HImode:
20931       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
20932       break;
20933
20934     case V16QImode:
20935     case V8QImode:
20936     default:
20937       break;
20938     }
20939
20940   if (use_vec_merge)
20941     {
20942       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
20943       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
20944       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20945     }
20946   else
20947     {
20948       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
20949
20950       emit_move_insn (mem, target);
20951
20952       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
20953       emit_move_insn (tmp, val);
20954
20955       emit_move_insn (target, mem);
20956     }
20957 }
20958
20959 void
20960 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
20961 {
20962   enum machine_mode mode = GET_MODE (vec);
20963   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20964   bool use_vec_extr = false;
20965   rtx tmp;
20966
20967   switch (mode)
20968     {
20969     case V2SImode:
20970     case V2SFmode:
20971       if (!mmx_ok)
20972         break;
20973       /* FALLTHRU */
20974
20975     case V2DFmode:
20976     case V2DImode:
20977       use_vec_extr = true;
20978       break;
20979
20980     case V4SFmode:
20981       switch (elt)
20982         {
20983         case 0:
20984           tmp = vec;
20985           break;
20986
20987         case 1:
20988         case 3:
20989           tmp = gen_reg_rtx (mode);
20990           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
20991                                        GEN_INT (elt), GEN_INT (elt),
20992                                        GEN_INT (elt+4), GEN_INT (elt+4)));
20993           break;
20994
20995         case 2:
20996           tmp = gen_reg_rtx (mode);
20997           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
20998           break;
20999
21000         default:
21001           gcc_unreachable ();
21002         }
21003       vec = tmp;
21004       use_vec_extr = true;
21005       elt = 0;
21006       break;
21007
21008     case V4SImode:
21009       if (TARGET_SSE2)
21010         {
21011           switch (elt)
21012             {
21013             case 0:
21014               tmp = vec;
21015               break;
21016
21017             case 1:
21018             case 3:
21019               tmp = gen_reg_rtx (mode);
21020               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
21021                                             GEN_INT (elt), GEN_INT (elt),
21022                                             GEN_INT (elt), GEN_INT (elt)));
21023               break;
21024
21025             case 2:
21026               tmp = gen_reg_rtx (mode);
21027               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
21028               break;
21029
21030             default:
21031               gcc_unreachable ();
21032             }
21033           vec = tmp;
21034           use_vec_extr = true;
21035           elt = 0;
21036         }
21037       else
21038         {
21039           /* For SSE1, we have to reuse the V4SF code.  */
21040           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
21041                                       gen_lowpart (V4SFmode, vec), elt);
21042           return;
21043         }
21044       break;
21045
21046     case V8HImode:
21047       use_vec_extr = TARGET_SSE2;
21048       break;
21049     case V4HImode:
21050       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
21051       break;
21052
21053     case V16QImode:
21054     case V8QImode:
21055       /* ??? Could extract the appropriate HImode element and shift.  */
21056     default:
21057       break;
21058     }
21059
21060   if (use_vec_extr)
21061     {
21062       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
21063       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
21064
21065       /* Let the rtl optimizers know about the zero extension performed.  */
21066       if (inner_mode == HImode)
21067         {
21068           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
21069           target = gen_lowpart (SImode, target);
21070         }
21071
21072       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
21073     }
21074   else
21075     {
21076       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
21077
21078       emit_move_insn (mem, vec);
21079
21080       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
21081       emit_move_insn (target, tmp);
21082     }
21083 }
21084
21085 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
21086    pattern to reduce; DEST is the destination; IN is the input vector.  */
21087
21088 void
21089 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
21090 {
21091   rtx tmp1, tmp2, tmp3;
21092
21093   tmp1 = gen_reg_rtx (V4SFmode);
21094   tmp2 = gen_reg_rtx (V4SFmode);
21095   tmp3 = gen_reg_rtx (V4SFmode);
21096
21097   emit_insn (gen_sse_movhlps (tmp1, in, in));
21098   emit_insn (fn (tmp2, tmp1, in));
21099
21100   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
21101                                GEN_INT (1), GEN_INT (1),
21102                                GEN_INT (1+4), GEN_INT (1+4)));
21103   emit_insn (fn (dest, tmp2, tmp3));
21104 }
21105 \f
21106 /* Target hook for scalar_mode_supported_p.  */
21107 static bool
21108 ix86_scalar_mode_supported_p (enum machine_mode mode)
21109 {
21110   if (DECIMAL_FLOAT_MODE_P (mode))
21111     return true;
21112   else
21113     return default_scalar_mode_supported_p (mode);
21114 }
21115
21116 /* Implements target hook vector_mode_supported_p.  */
21117 static bool
21118 ix86_vector_mode_supported_p (enum machine_mode mode)
21119 {
21120   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
21121     return true;
21122   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
21123     return true;
21124   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
21125     return true;
21126   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
21127     return true;
21128   return false;
21129 }
21130
21131 /* Worker function for TARGET_MD_ASM_CLOBBERS.
21132
21133    We do this in the new i386 backend to maintain source compatibility
21134    with the old cc0-based compiler.  */
21135
21136 static tree
21137 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
21138                       tree inputs ATTRIBUTE_UNUSED,
21139                       tree clobbers)
21140 {
21141   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
21142                         clobbers);
21143   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
21144                         clobbers);
21145   return clobbers;
21146 }
21147
21148 /* Implements target vector targetm.asm.encode_section_info.  This
21149    is not used by netware.  */
21150
21151 static void ATTRIBUTE_UNUSED
21152 ix86_encode_section_info (tree decl, rtx rtl, int first)
21153 {
21154   default_encode_section_info (decl, rtl, first);
21155
21156   if (TREE_CODE (decl) == VAR_DECL
21157       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
21158       && ix86_in_large_data_p (decl))
21159     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
21160 }
21161
21162 /* Worker function for REVERSE_CONDITION.  */
21163
21164 enum rtx_code
21165 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
21166 {
21167   return (mode != CCFPmode && mode != CCFPUmode
21168           ? reverse_condition (code)
21169           : reverse_condition_maybe_unordered (code));
21170 }
21171
21172 /* Output code to perform an x87 FP register move, from OPERANDS[1]
21173    to OPERANDS[0].  */
21174
21175 const char *
21176 output_387_reg_move (rtx insn, rtx *operands)
21177 {
21178   if (REG_P (operands[0]))
21179     {
21180       if (REG_P (operands[1])
21181           && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
21182         {
21183           if (REGNO (operands[0]) == FIRST_STACK_REG)
21184             return output_387_ffreep (operands, 0);
21185           return "fstp\t%y0";
21186         }
21187       if (STACK_TOP_P (operands[0]))
21188         return "fld%z1\t%y1";
21189       return "fst\t%y0";
21190     }
21191   else if (MEM_P (operands[0]))
21192     {
21193       gcc_assert (REG_P (operands[1]));
21194       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
21195         return "fstp%z0\t%y0";
21196       else
21197         {
21198           /* There is no non-popping store to memory for XFmode.
21199              So if we need one, follow the store with a load.  */
21200           if (GET_MODE (operands[0]) == XFmode)
21201             return "fstp%z0\t%y0\n\tfld%z0\t%y0";
21202           else
21203             return "fst%z0\t%y0";
21204         }
21205     }
21206   else
21207     gcc_unreachable();
21208 }
21209
21210 /* Output code to perform a conditional jump to LABEL, if C2 flag in
21211    FP status register is set.  */
21212
21213 void
21214 ix86_emit_fp_unordered_jump (rtx label)
21215 {
21216   rtx reg = gen_reg_rtx (HImode);
21217   rtx temp;
21218
21219   emit_insn (gen_x86_fnstsw_1 (reg));
21220
21221   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_size))
21222     {
21223       emit_insn (gen_x86_sahf_1 (reg));
21224
21225       temp = gen_rtx_REG (CCmode, FLAGS_REG);
21226       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
21227     }
21228   else
21229     {
21230       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
21231
21232       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21233       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
21234     }
21235
21236   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
21237                               gen_rtx_LABEL_REF (VOIDmode, label),
21238                               pc_rtx);
21239   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
21240
21241   emit_jump_insn (temp);
21242   predict_jump (REG_BR_PROB_BASE * 10 / 100);
21243 }
21244
21245 /* Output code to perform a log1p XFmode calculation.  */
21246
21247 void ix86_emit_i387_log1p (rtx op0, rtx op1)
21248 {
21249   rtx label1 = gen_label_rtx ();
21250   rtx label2 = gen_label_rtx ();
21251
21252   rtx tmp = gen_reg_rtx (XFmode);
21253   rtx tmp2 = gen_reg_rtx (XFmode);
21254
21255   emit_insn (gen_absxf2 (tmp, op1));
21256   emit_insn (gen_cmpxf (tmp,
21257     CONST_DOUBLE_FROM_REAL_VALUE (
21258        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
21259        XFmode)));
21260   emit_jump_insn (gen_bge (label1));
21261
21262   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
21263   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
21264   emit_jump (label2);
21265
21266   emit_label (label1);
21267   emit_move_insn (tmp, CONST1_RTX (XFmode));
21268   emit_insn (gen_addxf3 (tmp, op1, tmp));
21269   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
21270   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
21271
21272   emit_label (label2);
21273 }
21274
21275 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
21276
21277 static void ATTRIBUTE_UNUSED
21278 i386_solaris_elf_named_section (const char *name, unsigned int flags,
21279                                 tree decl)
21280 {
21281   /* With Binutils 2.15, the "@unwind" marker must be specified on
21282      every occurrence of the ".eh_frame" section, not just the first
21283      one.  */
21284   if (TARGET_64BIT
21285       && strcmp (name, ".eh_frame") == 0)
21286     {
21287       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
21288                flags & SECTION_WRITE ? "aw" : "a");
21289       return;
21290     }
21291   default_elf_asm_named_section (name, flags, decl);
21292 }
21293
21294 /* Return the mangling of TYPE if it is an extended fundamental type.  */
21295
21296 static const char *
21297 ix86_mangle_fundamental_type (tree type)
21298 {
21299   switch (TYPE_MODE (type))
21300     {
21301     case TFmode:
21302       /* __float128 is "g".  */
21303       return "g";
21304     case XFmode:
21305       /* "long double" or __float80 is "e".  */
21306       return "e";
21307     default:
21308       return NULL;
21309     }
21310 }
21311
21312 /* For 32-bit code we can save PIC register setup by using
21313    __stack_chk_fail_local hidden function instead of calling
21314    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
21315    register, so it is better to call __stack_chk_fail directly.  */
21316
21317 static tree
21318 ix86_stack_protect_fail (void)
21319 {
21320   return TARGET_64BIT
21321          ? default_external_stack_protect_fail ()
21322          : default_hidden_stack_protect_fail ();
21323 }
21324
21325 /* Select a format to encode pointers in exception handling data.  CODE
21326    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
21327    true if the symbol may be affected by dynamic relocations.
21328
21329    ??? All x86 object file formats are capable of representing this.
21330    After all, the relocation needed is the same as for the call insn.
21331    Whether or not a particular assembler allows us to enter such, I
21332    guess we'll have to see.  */
21333 int
21334 asm_preferred_eh_data_format (int code, int global)
21335 {
21336   if (flag_pic)
21337     {
21338       int type = DW_EH_PE_sdata8;
21339       if (!TARGET_64BIT
21340           || ix86_cmodel == CM_SMALL_PIC
21341           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
21342         type = DW_EH_PE_sdata4;
21343       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
21344     }
21345   if (ix86_cmodel == CM_SMALL
21346       || (ix86_cmodel == CM_MEDIUM && code))
21347     return DW_EH_PE_udata4;
21348   return DW_EH_PE_absptr;
21349 }
21350 \f
21351 /* Expand copysign from SIGN to the positive value ABS_VALUE
21352    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
21353    the sign-bit.  */
21354 static void
21355 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
21356 {
21357   enum machine_mode mode = GET_MODE (sign);
21358   rtx sgn = gen_reg_rtx (mode);
21359   if (mask == NULL_RTX)
21360     {
21361       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
21362       if (!VECTOR_MODE_P (mode))
21363         {
21364           /* We need to generate a scalar mode mask in this case.  */
21365           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
21366           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
21367           mask = gen_reg_rtx (mode);
21368           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
21369         }
21370     }
21371   else
21372     mask = gen_rtx_NOT (mode, mask);
21373   emit_insn (gen_rtx_SET (VOIDmode, sgn,
21374                           gen_rtx_AND (mode, mask, sign)));
21375   emit_insn (gen_rtx_SET (VOIDmode, result,
21376                           gen_rtx_IOR (mode, abs_value, sgn)));
21377 }
21378
21379 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
21380    mask for masking out the sign-bit is stored in *SMASK, if that is
21381    non-null.  */
21382 static rtx
21383 ix86_expand_sse_fabs (rtx op0, rtx *smask)
21384 {
21385   enum machine_mode mode = GET_MODE (op0);
21386   rtx xa, mask;
21387
21388   xa = gen_reg_rtx (mode);
21389   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
21390   if (!VECTOR_MODE_P (mode))
21391     {
21392       /* We need to generate a scalar mode mask in this case.  */
21393       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
21394       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
21395       mask = gen_reg_rtx (mode);
21396       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
21397     }
21398   emit_insn (gen_rtx_SET (VOIDmode, xa,
21399                           gen_rtx_AND (mode, op0, mask)));
21400
21401   if (smask)
21402     *smask = mask;
21403
21404   return xa;
21405 }
21406
21407 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
21408    swapping the operands if SWAP_OPERANDS is true.  The expanded
21409    code is a forward jump to a newly created label in case the
21410    comparison is true.  The generated label rtx is returned.  */
21411 static rtx
21412 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
21413                                   bool swap_operands)
21414 {
21415   rtx label, tmp;
21416
21417   if (swap_operands)
21418     {
21419       tmp = op0;
21420       op0 = op1;
21421       op1 = tmp;
21422     }
21423
21424   label = gen_label_rtx ();
21425   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
21426   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21427                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
21428   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
21429   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
21430                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
21431   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
21432   JUMP_LABEL (tmp) = label;
21433
21434   return label;
21435 }
21436
21437 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
21438    using comparison code CODE.  Operands are swapped for the comparison if
21439    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
21440 static rtx
21441 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
21442                               bool swap_operands)
21443 {
21444   enum machine_mode mode = GET_MODE (op0);
21445   rtx mask = gen_reg_rtx (mode);
21446
21447   if (swap_operands)
21448     {
21449       rtx tmp = op0;
21450       op0 = op1;
21451       op1 = tmp;
21452     }
21453
21454   if (mode == DFmode)
21455     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
21456                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
21457   else
21458     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
21459                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
21460
21461   return mask;
21462 }
21463
21464 /* Generate and return a rtx of mode MODE for 2**n where n is the number
21465    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
21466 static rtx
21467 ix86_gen_TWO52 (enum machine_mode mode)
21468 {
21469   REAL_VALUE_TYPE TWO52r;
21470   rtx TWO52;
21471
21472   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
21473   TWO52 = const_double_from_real_value (TWO52r, mode);
21474   TWO52 = force_reg (mode, TWO52);
21475
21476   return TWO52;
21477 }
21478
21479 /* Expand SSE sequence for computing lround from OP1 storing
21480    into OP0.  */
21481 void
21482 ix86_expand_lround (rtx op0, rtx op1)
21483 {
21484   /* C code for the stuff we're doing below:
21485        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
21486        return (long)tmp;
21487    */
21488   enum machine_mode mode = GET_MODE (op1);
21489   const struct real_format *fmt;
21490   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21491   rtx adj;
21492
21493   /* load nextafter (0.5, 0.0) */
21494   fmt = REAL_MODE_FORMAT (mode);
21495   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21496   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21497
21498   /* adj = copysign (0.5, op1) */
21499   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
21500   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
21501
21502   /* adj = op1 + adj */
21503   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
21504
21505   /* op0 = (imode)adj */
21506   expand_fix (op0, adj, 0);
21507 }
21508
21509 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
21510    into OPERAND0.  */
21511 void
21512 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
21513 {
21514   /* C code for the stuff we're doing below (for do_floor):
21515         xi = (long)op1;
21516         xi -= (double)xi > op1 ? 1 : 0;
21517         return xi;
21518    */
21519   enum machine_mode fmode = GET_MODE (op1);
21520   enum machine_mode imode = GET_MODE (op0);
21521   rtx ireg, freg, label, tmp;
21522
21523   /* reg = (long)op1 */
21524   ireg = gen_reg_rtx (imode);
21525   expand_fix (ireg, op1, 0);
21526
21527   /* freg = (double)reg */
21528   freg = gen_reg_rtx (fmode);
21529   expand_float (freg, ireg, 0);
21530
21531   /* ireg = (freg > op1) ? ireg - 1 : ireg */
21532   label = ix86_expand_sse_compare_and_jump (UNLE,
21533                                             freg, op1, !do_floor);
21534   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
21535                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
21536   emit_move_insn (ireg, tmp);
21537
21538   emit_label (label);
21539   LABEL_NUSES (label) = 1;
21540
21541   emit_move_insn (op0, ireg);
21542 }
21543
21544 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
21545    result in OPERAND0.  */
21546 void
21547 ix86_expand_rint (rtx operand0, rtx operand1)
21548 {
21549   /* C code for the stuff we're doing below:
21550         xa = fabs (operand1);
21551         if (!isless (xa, 2**52))
21552           return operand1;
21553         xa = xa + 2**52 - 2**52;
21554         return copysign (xa, operand1);
21555    */
21556   enum machine_mode mode = GET_MODE (operand0);
21557   rtx res, xa, label, TWO52, mask;
21558
21559   res = gen_reg_rtx (mode);
21560   emit_move_insn (res, operand1);
21561
21562   /* xa = abs (operand1) */
21563   xa = ix86_expand_sse_fabs (res, &mask);
21564
21565   /* if (!isless (xa, TWO52)) goto label; */
21566   TWO52 = ix86_gen_TWO52 (mode);
21567   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21568
21569   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21570   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21571
21572   ix86_sse_copysign_to_positive (res, xa, res, mask);
21573
21574   emit_label (label);
21575   LABEL_NUSES (label) = 1;
21576
21577   emit_move_insn (operand0, res);
21578 }
21579
21580 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21581    into OPERAND0.  */
21582 void
21583 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
21584 {
21585   /* C code for the stuff we expand below.
21586         double xa = fabs (x), x2;
21587         if (!isless (xa, TWO52))
21588           return x;
21589         xa = xa + TWO52 - TWO52;
21590         x2 = copysign (xa, x);
21591      Compensate.  Floor:
21592         if (x2 > x)
21593           x2 -= 1;
21594      Compensate.  Ceil:
21595         if (x2 < x)
21596           x2 -= -1;
21597         return x2;
21598    */
21599   enum machine_mode mode = GET_MODE (operand0);
21600   rtx xa, TWO52, tmp, label, one, res, mask;
21601
21602   TWO52 = ix86_gen_TWO52 (mode);
21603
21604   /* Temporary for holding the result, initialized to the input
21605      operand to ease control flow.  */
21606   res = gen_reg_rtx (mode);
21607   emit_move_insn (res, operand1);
21608
21609   /* xa = abs (operand1) */
21610   xa = ix86_expand_sse_fabs (res, &mask);
21611
21612   /* if (!isless (xa, TWO52)) goto label; */
21613   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21614
21615   /* xa = xa + TWO52 - TWO52; */
21616   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21617   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21618
21619   /* xa = copysign (xa, operand1) */
21620   ix86_sse_copysign_to_positive (xa, xa, res, mask);
21621
21622   /* generate 1.0 or -1.0 */
21623   one = force_reg (mode,
21624                    const_double_from_real_value (do_floor
21625                                                  ? dconst1 : dconstm1, mode));
21626
21627   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21628   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21629   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21630                           gen_rtx_AND (mode, one, tmp)));
21631   /* We always need to subtract here to preserve signed zero.  */
21632   tmp = expand_simple_binop (mode, MINUS,
21633                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21634   emit_move_insn (res, tmp);
21635
21636   emit_label (label);
21637   LABEL_NUSES (label) = 1;
21638
21639   emit_move_insn (operand0, res);
21640 }
21641
21642 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21643    into OPERAND0.  */
21644 void
21645 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
21646 {
21647   /* C code for the stuff we expand below.
21648         double xa = fabs (x), x2;
21649         if (!isless (xa, TWO52))
21650           return x;
21651         x2 = (double)(long)x;
21652      Compensate.  Floor:
21653         if (x2 > x)
21654           x2 -= 1;
21655      Compensate.  Ceil:
21656         if (x2 < x)
21657           x2 += 1;
21658         if (HONOR_SIGNED_ZEROS (mode))
21659           return copysign (x2, x);
21660         return x2;
21661    */
21662   enum machine_mode mode = GET_MODE (operand0);
21663   rtx xa, xi, TWO52, tmp, label, one, res, mask;
21664
21665   TWO52 = ix86_gen_TWO52 (mode);
21666
21667   /* Temporary for holding the result, initialized to the input
21668      operand to ease control flow.  */
21669   res = gen_reg_rtx (mode);
21670   emit_move_insn (res, operand1);
21671
21672   /* xa = abs (operand1) */
21673   xa = ix86_expand_sse_fabs (res, &mask);
21674
21675   /* if (!isless (xa, TWO52)) goto label; */
21676   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21677
21678   /* xa = (double)(long)x */
21679   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21680   expand_fix (xi, res, 0);
21681   expand_float (xa, xi, 0);
21682
21683   /* generate 1.0 */
21684   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21685
21686   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21687   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21688   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21689                           gen_rtx_AND (mode, one, tmp)));
21690   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
21691                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21692   emit_move_insn (res, tmp);
21693
21694   if (HONOR_SIGNED_ZEROS (mode))
21695     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21696
21697   emit_label (label);
21698   LABEL_NUSES (label) = 1;
21699
21700   emit_move_insn (operand0, res);
21701 }
21702
21703 /* Expand SSE sequence for computing round from OPERAND1 storing
21704    into OPERAND0.  Sequence that works without relying on DImode truncation
21705    via cvttsd2siq that is only available on 64bit targets.  */
21706 void
21707 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
21708 {
21709   /* C code for the stuff we expand below.
21710         double xa = fabs (x), xa2, x2;
21711         if (!isless (xa, TWO52))
21712           return x;
21713      Using the absolute value and copying back sign makes
21714      -0.0 -> -0.0 correct.
21715         xa2 = xa + TWO52 - TWO52;
21716      Compensate.
21717         dxa = xa2 - xa;
21718         if (dxa <= -0.5)
21719           xa2 += 1;
21720         else if (dxa > 0.5)
21721           xa2 -= 1;
21722         x2 = copysign (xa2, x);
21723         return x2;
21724    */
21725   enum machine_mode mode = GET_MODE (operand0);
21726   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
21727
21728   TWO52 = ix86_gen_TWO52 (mode);
21729
21730   /* Temporary for holding the result, initialized to the input
21731      operand to ease control flow.  */
21732   res = gen_reg_rtx (mode);
21733   emit_move_insn (res, operand1);
21734
21735   /* xa = abs (operand1) */
21736   xa = ix86_expand_sse_fabs (res, &mask);
21737
21738   /* if (!isless (xa, TWO52)) goto label; */
21739   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21740
21741   /* xa2 = xa + TWO52 - TWO52; */
21742   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21743   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
21744
21745   /* dxa = xa2 - xa; */
21746   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
21747
21748   /* generate 0.5, 1.0 and -0.5 */
21749   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
21750   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
21751   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
21752                                0, OPTAB_DIRECT);
21753
21754   /* Compensate.  */
21755   tmp = gen_reg_rtx (mode);
21756   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
21757   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
21758   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21759                           gen_rtx_AND (mode, one, tmp)));
21760   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21761   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
21762   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
21763   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21764                           gen_rtx_AND (mode, one, tmp)));
21765   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21766
21767   /* res = copysign (xa2, operand1) */
21768   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
21769
21770   emit_label (label);
21771   LABEL_NUSES (label) = 1;
21772
21773   emit_move_insn (operand0, res);
21774 }
21775
21776 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21777    into OPERAND0.  */
21778 void
21779 ix86_expand_trunc (rtx operand0, rtx operand1)
21780 {
21781   /* C code for SSE variant we expand below.
21782         double xa = fabs (x), x2;
21783         if (!isless (xa, TWO52))
21784           return x;
21785         x2 = (double)(long)x;
21786         if (HONOR_SIGNED_ZEROS (mode))
21787           return copysign (x2, x);
21788         return x2;
21789    */
21790   enum machine_mode mode = GET_MODE (operand0);
21791   rtx xa, xi, TWO52, label, res, mask;
21792
21793   TWO52 = ix86_gen_TWO52 (mode);
21794
21795   /* Temporary for holding the result, initialized to the input
21796      operand to ease control flow.  */
21797   res = gen_reg_rtx (mode);
21798   emit_move_insn (res, operand1);
21799
21800   /* xa = abs (operand1) */
21801   xa = ix86_expand_sse_fabs (res, &mask);
21802
21803   /* if (!isless (xa, TWO52)) goto label; */
21804   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21805
21806   /* x = (double)(long)x */
21807   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21808   expand_fix (xi, res, 0);
21809   expand_float (res, xi, 0);
21810
21811   if (HONOR_SIGNED_ZEROS (mode))
21812     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21813
21814   emit_label (label);
21815   LABEL_NUSES (label) = 1;
21816
21817   emit_move_insn (operand0, res);
21818 }
21819
21820 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21821    into OPERAND0.  */
21822 void
21823 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
21824 {
21825   enum machine_mode mode = GET_MODE (operand0);
21826   rtx xa, mask, TWO52, label, one, res, smask, tmp;
21827
21828   /* C code for SSE variant we expand below.
21829         double xa = fabs (x), x2;
21830         if (!isless (xa, TWO52))
21831           return x;
21832         xa2 = xa + TWO52 - TWO52;
21833      Compensate:
21834         if (xa2 > xa)
21835           xa2 -= 1.0;
21836         x2 = copysign (xa2, x);
21837         return x2;
21838    */
21839
21840   TWO52 = ix86_gen_TWO52 (mode);
21841
21842   /* Temporary for holding the result, initialized to the input
21843      operand to ease control flow.  */
21844   res = gen_reg_rtx (mode);
21845   emit_move_insn (res, operand1);
21846
21847   /* xa = abs (operand1) */
21848   xa = ix86_expand_sse_fabs (res, &smask);
21849
21850   /* if (!isless (xa, TWO52)) goto label; */
21851   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21852
21853   /* res = xa + TWO52 - TWO52; */
21854   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21855   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
21856   emit_move_insn (res, tmp);
21857
21858   /* generate 1.0 */
21859   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21860
21861   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
21862   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
21863   emit_insn (gen_rtx_SET (VOIDmode, mask,
21864                           gen_rtx_AND (mode, mask, one)));
21865   tmp = expand_simple_binop (mode, MINUS,
21866                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
21867   emit_move_insn (res, tmp);
21868
21869   /* res = copysign (res, operand1) */
21870   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
21871
21872   emit_label (label);
21873   LABEL_NUSES (label) = 1;
21874
21875   emit_move_insn (operand0, res);
21876 }
21877
21878 /* Expand SSE sequence for computing round from OPERAND1 storing
21879    into OPERAND0.  */
21880 void
21881 ix86_expand_round (rtx operand0, rtx operand1)
21882 {
21883   /* C code for the stuff we're doing below:
21884         double xa = fabs (x);
21885         if (!isless (xa, TWO52))
21886           return x;
21887         xa = (double)(long)(xa + nextafter (0.5, 0.0));
21888         return copysign (xa, x);
21889    */
21890   enum machine_mode mode = GET_MODE (operand0);
21891   rtx res, TWO52, xa, label, xi, half, mask;
21892   const struct real_format *fmt;
21893   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21894
21895   /* Temporary for holding the result, initialized to the input
21896      operand to ease control flow.  */
21897   res = gen_reg_rtx (mode);
21898   emit_move_insn (res, operand1);
21899
21900   TWO52 = ix86_gen_TWO52 (mode);
21901   xa = ix86_expand_sse_fabs (res, &mask);
21902   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21903
21904   /* load nextafter (0.5, 0.0) */
21905   fmt = REAL_MODE_FORMAT (mode);
21906   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21907   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21908
21909   /* xa = xa + 0.5 */
21910   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
21911   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
21912
21913   /* xa = (double)(int64_t)xa */
21914   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21915   expand_fix (xi, xa, 0);
21916   expand_float (xa, xi, 0);
21917
21918   /* res = copysign (xa, operand1) */
21919   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
21920
21921   emit_label (label);
21922   LABEL_NUSES (label) = 1;
21923
21924   emit_move_insn (operand0, res);
21925 }
21926
21927 \f
21928 /* Table of valid machine attributes.  */
21929 static const struct attribute_spec ix86_attribute_table[] =
21930 {
21931   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
21932   /* Stdcall attribute says callee is responsible for popping arguments
21933      if they are not variable.  */
21934   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21935   /* Fastcall attribute says callee is responsible for popping arguments
21936      if they are not variable.  */
21937   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21938   /* Cdecl attribute says the callee is a normal C declaration */
21939   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21940   /* Regparm attribute specifies how many integer arguments are to be
21941      passed in registers.  */
21942   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
21943   /* Sseregparm attribute says we are using x86_64 calling conventions
21944      for FP arguments.  */
21945   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
21946   /* force_align_arg_pointer says this function realigns the stack at entry.  */
21947   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
21948     false, true,  true, ix86_handle_cconv_attribute },
21949 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21950   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
21951   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
21952   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
21953 #endif
21954   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21955   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21956 #ifdef SUBTARGET_ATTRIBUTE_TABLE
21957   SUBTARGET_ATTRIBUTE_TABLE,
21958 #endif
21959   { NULL,        0, 0, false, false, false, NULL }
21960 };
21961
21962 /* Initialize the GCC target structure.  */
21963 #undef TARGET_ATTRIBUTE_TABLE
21964 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
21965 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21966 #  undef TARGET_MERGE_DECL_ATTRIBUTES
21967 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
21968 #endif
21969
21970 #undef TARGET_COMP_TYPE_ATTRIBUTES
21971 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
21972
21973 #undef TARGET_INIT_BUILTINS
21974 #define TARGET_INIT_BUILTINS ix86_init_builtins
21975 #undef TARGET_EXPAND_BUILTIN
21976 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
21977
21978 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
21979 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION ix86_builtin_vectorized_function
21980 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
21981 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_builtin_conversion
21982
21983 #undef TARGET_ASM_FUNCTION_EPILOGUE
21984 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
21985
21986 #undef TARGET_ENCODE_SECTION_INFO
21987 #ifndef SUBTARGET_ENCODE_SECTION_INFO
21988 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
21989 #else
21990 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
21991 #endif
21992
21993 #undef TARGET_ASM_OPEN_PAREN
21994 #define TARGET_ASM_OPEN_PAREN ""
21995 #undef TARGET_ASM_CLOSE_PAREN
21996 #define TARGET_ASM_CLOSE_PAREN ""
21997
21998 #undef TARGET_ASM_ALIGNED_HI_OP
21999 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
22000 #undef TARGET_ASM_ALIGNED_SI_OP
22001 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
22002 #ifdef ASM_QUAD
22003 #undef TARGET_ASM_ALIGNED_DI_OP
22004 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
22005 #endif
22006
22007 #undef TARGET_ASM_UNALIGNED_HI_OP
22008 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
22009 #undef TARGET_ASM_UNALIGNED_SI_OP
22010 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
22011 #undef TARGET_ASM_UNALIGNED_DI_OP
22012 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
22013
22014 #undef TARGET_SCHED_ADJUST_COST
22015 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
22016 #undef TARGET_SCHED_ISSUE_RATE
22017 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
22018 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
22019 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
22020   ia32_multipass_dfa_lookahead
22021
22022 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
22023 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
22024
22025 #ifdef HAVE_AS_TLS
22026 #undef TARGET_HAVE_TLS
22027 #define TARGET_HAVE_TLS true
22028 #endif
22029 #undef TARGET_CANNOT_FORCE_CONST_MEM
22030 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
22031 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
22032 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
22033
22034 #undef TARGET_DELEGITIMIZE_ADDRESS
22035 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
22036
22037 #undef TARGET_MS_BITFIELD_LAYOUT_P
22038 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
22039
22040 #if TARGET_MACHO
22041 #undef TARGET_BINDS_LOCAL_P
22042 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
22043 #endif
22044 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
22045 #undef TARGET_BINDS_LOCAL_P
22046 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
22047 #endif
22048
22049 #undef TARGET_ASM_OUTPUT_MI_THUNK
22050 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
22051 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
22052 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
22053
22054 #undef TARGET_ASM_FILE_START
22055 #define TARGET_ASM_FILE_START x86_file_start
22056
22057 #undef TARGET_DEFAULT_TARGET_FLAGS
22058 #define TARGET_DEFAULT_TARGET_FLAGS     \
22059   (TARGET_DEFAULT                       \
22060    | TARGET_64BIT_DEFAULT               \
22061    | TARGET_SUBTARGET_DEFAULT           \
22062    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
22063
22064 #undef TARGET_HANDLE_OPTION
22065 #define TARGET_HANDLE_OPTION ix86_handle_option
22066
22067 #undef TARGET_RTX_COSTS
22068 #define TARGET_RTX_COSTS ix86_rtx_costs
22069 #undef TARGET_ADDRESS_COST
22070 #define TARGET_ADDRESS_COST ix86_address_cost
22071
22072 #undef TARGET_FIXED_CONDITION_CODE_REGS
22073 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
22074 #undef TARGET_CC_MODES_COMPATIBLE
22075 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
22076
22077 #undef TARGET_MACHINE_DEPENDENT_REORG
22078 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
22079
22080 #undef TARGET_BUILD_BUILTIN_VA_LIST
22081 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
22082
22083 #undef TARGET_MD_ASM_CLOBBERS
22084 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
22085
22086 #undef TARGET_PROMOTE_PROTOTYPES
22087 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
22088 #undef TARGET_STRUCT_VALUE_RTX
22089 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
22090 #undef TARGET_SETUP_INCOMING_VARARGS
22091 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
22092 #undef TARGET_MUST_PASS_IN_STACK
22093 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
22094 #undef TARGET_PASS_BY_REFERENCE
22095 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
22096 #undef TARGET_INTERNAL_ARG_POINTER
22097 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
22098 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
22099 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
22100 #undef TARGET_STRICT_ARGUMENT_NAMING
22101 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
22102
22103 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
22104 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
22105
22106 #undef TARGET_SCALAR_MODE_SUPPORTED_P
22107 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
22108
22109 #undef TARGET_VECTOR_MODE_SUPPORTED_P
22110 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
22111
22112 #ifdef HAVE_AS_TLS
22113 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
22114 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
22115 #endif
22116
22117 #ifdef SUBTARGET_INSERT_ATTRIBUTES
22118 #undef TARGET_INSERT_ATTRIBUTES
22119 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
22120 #endif
22121
22122 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
22123 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
22124
22125 #undef TARGET_STACK_PROTECT_FAIL
22126 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
22127
22128 #undef TARGET_FUNCTION_VALUE
22129 #define TARGET_FUNCTION_VALUE ix86_function_value
22130
22131 struct gcc_target targetm = TARGET_INITIALIZER;
22132 \f
22133 #include "gt-i386.h"