i386.c (pentium4_cost, [...]): Update 32bit memcpy/memset decriptors.
[platform/upstream/gcc.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 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 static const
608 struct processor_costs pentium4_cost = {
609   COSTS_N_INSNS (1),                    /* cost of an add instruction */
610   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
611   COSTS_N_INSNS (4),                    /* variable shift costs */
612   COSTS_N_INSNS (4),                    /* constant shift costs */
613   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
614    COSTS_N_INSNS (15),                  /*                               HI */
615    COSTS_N_INSNS (15),                  /*                               SI */
616    COSTS_N_INSNS (15),                  /*                               DI */
617    COSTS_N_INSNS (15)},                 /*                               other */
618   0,                                    /* cost of multiply per each bit set */
619   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
620    COSTS_N_INSNS (56),                  /*                          HI */
621    COSTS_N_INSNS (56),                  /*                          SI */
622    COSTS_N_INSNS (56),                  /*                          DI */
623    COSTS_N_INSNS (56)},                 /*                          other */
624   COSTS_N_INSNS (1),                    /* cost of movsx */
625   COSTS_N_INSNS (1),                    /* cost of movzx */
626   16,                                   /* "large" insn */
627   6,                                    /* MOVE_RATIO */
628   2,                                    /* cost for loading QImode using movzbl */
629   {4, 5, 4},                            /* cost of loading integer registers
630                                            in QImode, HImode and SImode.
631                                            Relative to reg-reg move (2).  */
632   {2, 3, 2},                            /* cost of storing integer registers */
633   2,                                    /* cost of reg,reg fld/fst */
634   {2, 2, 6},                            /* cost of loading fp registers
635                                            in SFmode, DFmode and XFmode */
636   {4, 4, 6},                            /* cost of storing fp registers
637                                            in SFmode, DFmode and XFmode */
638   2,                                    /* cost of moving MMX register */
639   {2, 2},                               /* cost of loading MMX registers
640                                            in SImode and DImode */
641   {2, 2},                               /* cost of storing MMX registers
642                                            in SImode and DImode */
643   12,                                   /* cost of moving SSE register */
644   {12, 12, 12},                         /* cost of loading SSE registers
645                                            in SImode, DImode and TImode */
646   {2, 2, 8},                            /* cost of storing SSE registers
647                                            in SImode, DImode and TImode */
648   10,                                   /* MMX or SSE register to integer */
649   64,                                   /* size of prefetch block */
650   6,                                    /* number of parallel prefetches */
651   2,                                    /* Branch cost */
652   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
653   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
654   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
655   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
656   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
657   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
658   {{libcall, {{12, loop_1_byte}, {64, loop}, {-1, rep_prefix_4_byte}}},
659    DUMMY_STRINGOP_ALGS},
660   {{libcall, {{6, loop_1_byte}, {64, loop}, {20480, rep_prefix_4_byte},
661    {-1, libcall}}},
662    DUMMY_STRINGOP_ALGS},
663 };
664
665 static const
666 struct processor_costs nocona_cost = {
667   COSTS_N_INSNS (1),                    /* cost of an add instruction */
668   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
669   COSTS_N_INSNS (1),                    /* variable shift costs */
670   COSTS_N_INSNS (1),                    /* constant shift costs */
671   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
672    COSTS_N_INSNS (10),                  /*                               HI */
673    COSTS_N_INSNS (10),                  /*                               SI */
674    COSTS_N_INSNS (10),                  /*                               DI */
675    COSTS_N_INSNS (10)},                 /*                               other */
676   0,                                    /* cost of multiply per each bit set */
677   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
678    COSTS_N_INSNS (66),                  /*                          HI */
679    COSTS_N_INSNS (66),                  /*                          SI */
680    COSTS_N_INSNS (66),                  /*                          DI */
681    COSTS_N_INSNS (66)},                 /*                          other */
682   COSTS_N_INSNS (1),                    /* cost of movsx */
683   COSTS_N_INSNS (1),                    /* cost of movzx */
684   16,                                   /* "large" insn */
685   17,                                   /* MOVE_RATIO */
686   4,                                    /* cost for loading QImode using movzbl */
687   {4, 4, 4},                            /* cost of loading integer registers
688                                            in QImode, HImode and SImode.
689                                            Relative to reg-reg move (2).  */
690   {4, 4, 4},                            /* cost of storing integer registers */
691   3,                                    /* cost of reg,reg fld/fst */
692   {12, 12, 12},                         /* cost of loading fp registers
693                                            in SFmode, DFmode and XFmode */
694   {4, 4, 4},                            /* cost of storing fp registers
695                                            in SFmode, DFmode and XFmode */
696   6,                                    /* cost of moving MMX register */
697   {12, 12},                             /* cost of loading MMX registers
698                                            in SImode and DImode */
699   {12, 12},                             /* cost of storing MMX registers
700                                            in SImode and DImode */
701   6,                                    /* cost of moving SSE register */
702   {12, 12, 12},                         /* cost of loading SSE registers
703                                            in SImode, DImode and TImode */
704   {12, 12, 12},                         /* cost of storing SSE registers
705                                            in SImode, DImode and TImode */
706   8,                                    /* MMX or SSE register to integer */
707   128,                                  /* size of prefetch block */
708   8,                                    /* number of parallel prefetches */
709   1,                                    /* Branch cost */
710   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
711   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
712   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
713   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
714   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
715   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
716   {{libcall, {{12, loop_1_byte}, {64, loop}, {-1, rep_prefix_4_byte}}},
717    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
718               {100000, unrolled_loop}, {-1, libcall}}}},
719   {{libcall, {{6, loop_1_byte}, {64, loop}, {20480, rep_prefix_4_byte},
720    {-1, libcall}}},
721    {libcall, {{24, loop}, {64, unrolled_loop},
722               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
723 };
724
725 static const
726 struct processor_costs core2_cost = {
727   COSTS_N_INSNS (1),                    /* cost of an add instruction */
728   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
729   COSTS_N_INSNS (1),                    /* variable shift costs */
730   COSTS_N_INSNS (1),                    /* constant shift costs */
731   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
732    COSTS_N_INSNS (3),                   /*                               HI */
733    COSTS_N_INSNS (3),                   /*                               SI */
734    COSTS_N_INSNS (3),                   /*                               DI */
735    COSTS_N_INSNS (3)},                  /*                               other */
736   0,                                    /* cost of multiply per each bit set */
737   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
738    COSTS_N_INSNS (22),                  /*                          HI */
739    COSTS_N_INSNS (22),                  /*                          SI */
740    COSTS_N_INSNS (22),                  /*                          DI */
741    COSTS_N_INSNS (22)},                 /*                          other */
742   COSTS_N_INSNS (1),                    /* cost of movsx */
743   COSTS_N_INSNS (1),                    /* cost of movzx */
744   8,                                    /* "large" insn */
745   16,                                   /* MOVE_RATIO */
746   2,                                    /* cost for loading QImode using movzbl */
747   {6, 6, 6},                            /* cost of loading integer registers
748                                            in QImode, HImode and SImode.
749                                            Relative to reg-reg move (2).  */
750   {4, 4, 4},                            /* cost of storing integer registers */
751   2,                                    /* cost of reg,reg fld/fst */
752   {6, 6, 6},                            /* cost of loading fp registers
753                                            in SFmode, DFmode and XFmode */
754   {4, 4, 4},                            /* cost of loading integer registers */
755   2,                                    /* cost of moving MMX register */
756   {6, 6},                               /* cost of loading MMX registers
757                                            in SImode and DImode */
758   {4, 4},                               /* cost of storing MMX registers
759                                            in SImode and DImode */
760   2,                                    /* cost of moving SSE register */
761   {6, 6, 6},                            /* cost of loading SSE registers
762                                            in SImode, DImode and TImode */
763   {4, 4, 4},                            /* cost of storing SSE registers
764                                            in SImode, DImode and TImode */
765   2,                                    /* MMX or SSE register to integer */
766   128,                                  /* size of prefetch block */
767   8,                                    /* number of parallel prefetches */
768   3,                                    /* Branch cost */
769   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
770   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
771   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
772   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
773   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
774   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
775   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
776    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
777               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
778   {{libcall, {{8, loop}, {15, unrolled_loop},
779               {2048, rep_prefix_4_byte}, {-1, libcall}}},
780    {libcall, {{24, loop}, {32, unrolled_loop},
781               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
782 };
783
784 /* Generic64 should produce code tuned for Nocona and K8.  */
785 static const
786 struct processor_costs generic64_cost = {
787   COSTS_N_INSNS (1),                    /* cost of an add instruction */
788   /* On all chips taken into consideration lea is 2 cycles and more.  With
789      this cost however our current implementation of synth_mult results in
790      use of unnecessary temporary registers causing regression on several
791      SPECfp benchmarks.  */
792   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
793   COSTS_N_INSNS (1),                    /* variable shift costs */
794   COSTS_N_INSNS (1),                    /* constant shift costs */
795   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
796    COSTS_N_INSNS (4),                   /*                               HI */
797    COSTS_N_INSNS (3),                   /*                               SI */
798    COSTS_N_INSNS (4),                   /*                               DI */
799    COSTS_N_INSNS (2)},                  /*                               other */
800   0,                                    /* cost of multiply per each bit set */
801   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
802    COSTS_N_INSNS (26),                  /*                          HI */
803    COSTS_N_INSNS (42),                  /*                          SI */
804    COSTS_N_INSNS (74),                  /*                          DI */
805    COSTS_N_INSNS (74)},                 /*                          other */
806   COSTS_N_INSNS (1),                    /* cost of movsx */
807   COSTS_N_INSNS (1),                    /* cost of movzx */
808   8,                                    /* "large" insn */
809   17,                                   /* MOVE_RATIO */
810   4,                                    /* cost for loading QImode using movzbl */
811   {4, 4, 4},                            /* cost of loading integer registers
812                                            in QImode, HImode and SImode.
813                                            Relative to reg-reg move (2).  */
814   {4, 4, 4},                            /* cost of storing integer registers */
815   4,                                    /* cost of reg,reg fld/fst */
816   {12, 12, 12},                         /* cost of loading fp registers
817                                            in SFmode, DFmode and XFmode */
818   {6, 6, 8},                            /* cost of storing fp registers
819                                            in SFmode, DFmode and XFmode */
820   2,                                    /* cost of moving MMX register */
821   {8, 8},                               /* cost of loading MMX registers
822                                            in SImode and DImode */
823   {8, 8},                               /* cost of storing MMX registers
824                                            in SImode and DImode */
825   2,                                    /* cost of moving SSE register */
826   {8, 8, 8},                            /* cost of loading SSE registers
827                                            in SImode, DImode and TImode */
828   {8, 8, 8},                            /* cost of storing SSE registers
829                                            in SImode, DImode and TImode */
830   5,                                    /* MMX or SSE register to integer */
831   64,                                   /* size of prefetch block */
832   6,                                    /* number of parallel prefetches */
833   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
834      is increased to perhaps more appropriate value of 5.  */
835   3,                                    /* Branch cost */
836   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
837   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
838   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
839   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
840   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
841   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
842   {DUMMY_STRINGOP_ALGS,
843    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
844   {DUMMY_STRINGOP_ALGS,
845    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
846 };
847
848 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
849 static const
850 struct processor_costs generic32_cost = {
851   COSTS_N_INSNS (1),                    /* cost of an add instruction */
852   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
853   COSTS_N_INSNS (1),                    /* variable shift costs */
854   COSTS_N_INSNS (1),                    /* constant shift costs */
855   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
856    COSTS_N_INSNS (4),                   /*                               HI */
857    COSTS_N_INSNS (3),                   /*                               SI */
858    COSTS_N_INSNS (4),                   /*                               DI */
859    COSTS_N_INSNS (2)},                  /*                               other */
860   0,                                    /* cost of multiply per each bit set */
861   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
862    COSTS_N_INSNS (26),                  /*                          HI */
863    COSTS_N_INSNS (42),                  /*                          SI */
864    COSTS_N_INSNS (74),                  /*                          DI */
865    COSTS_N_INSNS (74)},                 /*                          other */
866   COSTS_N_INSNS (1),                    /* cost of movsx */
867   COSTS_N_INSNS (1),                    /* cost of movzx */
868   8,                                    /* "large" insn */
869   17,                                   /* MOVE_RATIO */
870   4,                                    /* cost for loading QImode using movzbl */
871   {4, 4, 4},                            /* cost of loading integer registers
872                                            in QImode, HImode and SImode.
873                                            Relative to reg-reg move (2).  */
874   {4, 4, 4},                            /* cost of storing integer registers */
875   4,                                    /* cost of reg,reg fld/fst */
876   {12, 12, 12},                         /* cost of loading fp registers
877                                            in SFmode, DFmode and XFmode */
878   {6, 6, 8},                            /* cost of storing fp registers
879                                            in SFmode, DFmode and XFmode */
880   2,                                    /* cost of moving MMX register */
881   {8, 8},                               /* cost of loading MMX registers
882                                            in SImode and DImode */
883   {8, 8},                               /* cost of storing MMX registers
884                                            in SImode and DImode */
885   2,                                    /* cost of moving SSE register */
886   {8, 8, 8},                            /* cost of loading SSE registers
887                                            in SImode, DImode and TImode */
888   {8, 8, 8},                            /* cost of storing SSE registers
889                                            in SImode, DImode and TImode */
890   5,                                    /* MMX or SSE register to integer */
891   64,                                   /* size of prefetch block */
892   6,                                    /* number of parallel prefetches */
893   3,                                    /* Branch cost */
894   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
895   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
896   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
897   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
898   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
899   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
900   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
901    DUMMY_STRINGOP_ALGS},
902   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
903    DUMMY_STRINGOP_ALGS},
904 };
905
906 const struct processor_costs *ix86_cost = &pentium_cost;
907
908 /* Processor feature/optimization bitmasks.  */
909 #define m_386 (1<<PROCESSOR_I386)
910 #define m_486 (1<<PROCESSOR_I486)
911 #define m_PENT (1<<PROCESSOR_PENTIUM)
912 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
913 #define m_GEODE  (1<<PROCESSOR_GEODE)
914 #define m_K6_GEODE  (m_K6 | m_GEODE)
915 #define m_K6  (1<<PROCESSOR_K6)
916 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
917 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
918 #define m_K8  (1<<PROCESSOR_K8)
919 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
920 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
921 #define m_CORE2  (1<<PROCESSOR_CORE2)
922 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
923 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
924 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
925
926 /* Generic instruction choice should be common subset of supported CPUs
927    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
928
929 /* Leave is not affecting Nocona SPEC2000 results negatively, so enabling for
930    Generic64 seems like good code size tradeoff.  We can't enable it for 32bit
931    generic because it is not working well with PPro base chips.  */
932 const int x86_use_leave = m_386 | m_K6_GEODE | m_ATHLON_K8 | m_CORE2 | m_GENERIC64;
933 const int x86_push_memory = m_386 | m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
934 const int x86_zero_extend_with_and = m_486 | m_PENT;
935 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */;
936 const int x86_double_with_add = ~m_386;
937 const int x86_use_bit_test = m_386;
938 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6 | m_CORE2 | m_GENERIC;
939 const int x86_cmove = m_PPRO | m_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
940 const int x86_3dnow_a = m_ATHLON_K8;
941 const int x86_deep_branch = m_PPRO | m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
942 /* Branch hints were put in P4 based on simulation result. But
943    after P4 was made, no performance benefit was observed with
944    branch hints. It also increases the code size. As the result,
945    icc never generates branch hints.  */
946 const int x86_branch_hints = 0;
947 const int x86_use_sahf = m_PPRO | m_K6_GEODE | m_PENT4 | m_NOCONA | m_GENERIC32; /*m_GENERIC | m_ATHLON_K8 ? */
948 /* We probably ought to watch for partial register stalls on Generic32
949    compilation setting as well.  However in current implementation the
950    partial register stalls are not eliminated very well - they can
951    be introduced via subregs synthesized by combine and can happen
952    in caller/callee saving sequences.
953    Because this option pays back little on PPro based chips and is in conflict
954    with partial reg. dependencies used by Athlon/P4 based chips, it is better
955    to leave it off for generic32 for now.  */
956 const int x86_partial_reg_stall = m_PPRO;
957 const int x86_partial_flag_reg_stall =  m_CORE2 | m_GENERIC;
958 const int x86_use_himode_fiop = m_386 | m_486 | m_K6_GEODE;
959 const int x86_use_simode_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT | m_CORE2 | m_GENERIC);
960 const int x86_use_mov0 = m_K6;
961 const int x86_use_cltd = ~(m_PENT | m_K6 | m_CORE2 | m_GENERIC);
962 const int x86_read_modify_write = ~m_PENT;
963 const int x86_read_modify = ~(m_PENT | m_PPRO);
964 const int x86_split_long_moves = m_PPRO;
965 const int x86_promote_QImode = m_K6_GEODE | m_PENT | m_386 | m_486 | m_ATHLON_K8 | m_CORE2 | m_GENERIC; /* m_PENT4 ? */
966 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
967 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
968 const int x86_qimode_math = ~(0);
969 const int x86_promote_qi_regs = 0;
970 /* On PPro this flag is meant to avoid partial register stalls.  Just like
971    the x86_partial_reg_stall this option might be considered for Generic32
972    if our scheme for avoiding partial stalls was more effective.  */
973 const int x86_himode_math = ~(m_PPRO);
974 const int x86_promote_hi_regs = m_PPRO;
975 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
976 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
977 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6_GEODE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
978 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6_GEODE | m_386 | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
979 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC | m_GEODE);
980 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
981 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
982 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
983 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC;
984 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC;
985 const int x86_shift1 = ~m_486;
986 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
987 /* In Generic model we have an conflict here in between PPro/Pentium4 based chips
988    that thread 128bit SSE registers as single units versus K8 based chips that
989    divide SSE registers to two 64bit halves.
990    x86_sse_partial_reg_dependency promote all store destinations to be 128bit
991    to allow register renaming on 128bit SSE units, but usually results in one
992    extra microop on 64bit SSE units.  Experimental results shows that disabling
993    this option on P4 brings over 20% SPECfp regression, while enabling it on
994    K8 brings roughly 2.4% regression that can be partly masked by careful scheduling
995    of moves.  */
996 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
997 /* Set for machines where the type and dependencies are resolved on SSE
998    register parts instead of whole registers, so we may maintain just
999    lower part of scalar values in proper format leaving the upper part
1000    undefined.  */
1001 const int x86_sse_split_regs = m_ATHLON_K8;
1002 const int x86_sse_typeless_stores = m_ATHLON_K8;
1003 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4 | m_NOCONA;
1004 const int x86_use_ffreep = m_ATHLON_K8;
1005 const int x86_use_incdec = ~(m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC);
1006
1007 /* ??? Allowing interunit moves makes it all too easy for the compiler to put
1008    integer data in xmm registers.  Which results in pretty abysmal code.  */
1009 const int x86_inter_unit_moves = 0 /* ~(m_ATHLON_K8) */;
1010
1011 const int x86_ext_80387_constants = m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
1012 /* Some CPU cores are not able to predict more than 4 branch instructions in
1013    the 16 byte window.  */
1014 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC;
1015 const int x86_schedule = m_PPRO | m_ATHLON_K8 | m_K6_GEODE | m_PENT | m_CORE2 | m_GENERIC;
1016 const int x86_use_bt = m_ATHLON_K8;
1017 /* Compare and exchange was added for 80486.  */
1018 const int x86_cmpxchg = ~m_386;
1019 /* Compare and exchange 8 bytes was added for pentium.  */
1020 const int x86_cmpxchg8b = ~(m_386 | m_486);
1021 /* Compare and exchange 16 bytes was added for nocona.  */
1022 const int x86_cmpxchg16b = m_NOCONA;
1023 /* Exchange and add was added for 80486.  */
1024 const int x86_xadd = ~m_386;
1025 /* Byteswap was added for 80486.  */
1026 const int x86_bswap = ~m_386;
1027 const int x86_pad_returns = m_ATHLON_K8 | m_CORE2 | m_GENERIC;
1028
1029 static enum stringop_alg stringop_alg = no_stringop;
1030
1031 /* In case the average insn count for single function invocation is
1032    lower than this constant, emit fast (but longer) prologue and
1033    epilogue code.  */
1034 #define FAST_PROLOGUE_INSN_COUNT 20
1035
1036 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1037 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1038 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1039 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1040
1041 /* Array of the smallest class containing reg number REGNO, indexed by
1042    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1043
1044 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1045 {
1046   /* ax, dx, cx, bx */
1047   AREG, DREG, CREG, BREG,
1048   /* si, di, bp, sp */
1049   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1050   /* FP registers */
1051   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1052   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1053   /* arg pointer */
1054   NON_Q_REGS,
1055   /* flags, fpsr, fpcr, dirflag, frame */
1056   NO_REGS, NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1057   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1058   SSE_REGS, SSE_REGS,
1059   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1060   MMX_REGS, MMX_REGS,
1061   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1062   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1063   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1064   SSE_REGS, SSE_REGS,
1065 };
1066
1067 /* The "default" register map used in 32bit mode.  */
1068
1069 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1070 {
1071   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1072   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1073   -1, -1, -1, -1, -1, -1,               /* arg, flags, fpsr, fpcr, dir, frame */
1074   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1075   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1076   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1077   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1078 };
1079
1080 static int const x86_64_int_parameter_registers[6] =
1081 {
1082   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
1083   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1084 };
1085
1086 static int const x86_64_int_return_registers[4] =
1087 {
1088   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
1089 };
1090
1091 /* The "default" register map used in 64bit mode.  */
1092 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1093 {
1094   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1095   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1096   -1, -1, -1, -1, -1, -1,               /* arg, flags, fpsr, fpcr, dir, frame */
1097   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1098   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1099   8,9,10,11,12,13,14,15,                /* extended integer registers */
1100   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1101 };
1102
1103 /* Define the register numbers to be used in Dwarf debugging information.
1104    The SVR4 reference port C compiler uses the following register numbers
1105    in its Dwarf output code:
1106         0 for %eax (gcc regno = 0)
1107         1 for %ecx (gcc regno = 2)
1108         2 for %edx (gcc regno = 1)
1109         3 for %ebx (gcc regno = 3)
1110         4 for %esp (gcc regno = 7)
1111         5 for %ebp (gcc regno = 6)
1112         6 for %esi (gcc regno = 4)
1113         7 for %edi (gcc regno = 5)
1114    The following three DWARF register numbers are never generated by
1115    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1116    believes these numbers have these meanings.
1117         8  for %eip    (no gcc equivalent)
1118         9  for %eflags (gcc regno = 17)
1119         10 for %trapno (no gcc equivalent)
1120    It is not at all clear how we should number the FP stack registers
1121    for the x86 architecture.  If the version of SDB on x86/svr4 were
1122    a bit less brain dead with respect to floating-point then we would
1123    have a precedent to follow with respect to DWARF register numbers
1124    for x86 FP registers, but the SDB on x86/svr4 is so completely
1125    broken with respect to FP registers that it is hardly worth thinking
1126    of it as something to strive for compatibility with.
1127    The version of x86/svr4 SDB I have at the moment does (partially)
1128    seem to believe that DWARF register number 11 is associated with
1129    the x86 register %st(0), but that's about all.  Higher DWARF
1130    register numbers don't seem to be associated with anything in
1131    particular, and even for DWARF regno 11, SDB only seems to under-
1132    stand that it should say that a variable lives in %st(0) (when
1133    asked via an `=' command) if we said it was in DWARF regno 11,
1134    but SDB still prints garbage when asked for the value of the
1135    variable in question (via a `/' command).
1136    (Also note that the labels SDB prints for various FP stack regs
1137    when doing an `x' command are all wrong.)
1138    Note that these problems generally don't affect the native SVR4
1139    C compiler because it doesn't allow the use of -O with -g and
1140    because when it is *not* optimizing, it allocates a memory
1141    location for each floating-point variable, and the memory
1142    location is what gets described in the DWARF AT_location
1143    attribute for the variable in question.
1144    Regardless of the severe mental illness of the x86/svr4 SDB, we
1145    do something sensible here and we use the following DWARF
1146    register numbers.  Note that these are all stack-top-relative
1147    numbers.
1148         11 for %st(0) (gcc regno = 8)
1149         12 for %st(1) (gcc regno = 9)
1150         13 for %st(2) (gcc regno = 10)
1151         14 for %st(3) (gcc regno = 11)
1152         15 for %st(4) (gcc regno = 12)
1153         16 for %st(5) (gcc regno = 13)
1154         17 for %st(6) (gcc regno = 14)
1155         18 for %st(7) (gcc regno = 15)
1156 */
1157 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1158 {
1159   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1160   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1161   -1, 9, -1, -1, -1, -1,                /* arg, flags, fpsr, fpcr, dir, frame */
1162   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1163   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1164   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1165   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1166 };
1167
1168 /* Test and compare insns in i386.md store the information needed to
1169    generate branch and scc insns here.  */
1170
1171 rtx ix86_compare_op0 = NULL_RTX;
1172 rtx ix86_compare_op1 = NULL_RTX;
1173 rtx ix86_compare_emitted = NULL_RTX;
1174
1175 /* Size of the register save area.  */
1176 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1177
1178 /* Define the structure for the machine field in struct function.  */
1179
1180 struct stack_local_entry GTY(())
1181 {
1182   unsigned short mode;
1183   unsigned short n;
1184   rtx rtl;
1185   struct stack_local_entry *next;
1186 };
1187
1188 /* Structure describing stack frame layout.
1189    Stack grows downward:
1190
1191    [arguments]
1192                                               <- ARG_POINTER
1193    saved pc
1194
1195    saved frame pointer if frame_pointer_needed
1196                                               <- HARD_FRAME_POINTER
1197    [saved regs]
1198
1199    [padding1]          \
1200                         )
1201    [va_arg registers]  (
1202                         > to_allocate         <- FRAME_POINTER
1203    [frame]             (
1204                         )
1205    [padding2]          /
1206   */
1207 struct ix86_frame
1208 {
1209   int nregs;
1210   int padding1;
1211   int va_arg_size;
1212   HOST_WIDE_INT frame;
1213   int padding2;
1214   int outgoing_arguments_size;
1215   int red_zone_size;
1216
1217   HOST_WIDE_INT to_allocate;
1218   /* The offsets relative to ARG_POINTER.  */
1219   HOST_WIDE_INT frame_pointer_offset;
1220   HOST_WIDE_INT hard_frame_pointer_offset;
1221   HOST_WIDE_INT stack_pointer_offset;
1222
1223   /* When save_regs_using_mov is set, emit prologue using
1224      move instead of push instructions.  */
1225   bool save_regs_using_mov;
1226 };
1227
1228 /* Code model option.  */
1229 enum cmodel ix86_cmodel;
1230 /* Asm dialect.  */
1231 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1232 /* TLS dialects.  */
1233 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1234
1235 /* Which unit we are generating floating point math for.  */
1236 enum fpmath_unit ix86_fpmath;
1237
1238 /* Which cpu are we scheduling for.  */
1239 enum processor_type ix86_tune;
1240 /* Which instruction set architecture to use.  */
1241 enum processor_type ix86_arch;
1242
1243 /* true if sse prefetch instruction is not NOOP.  */
1244 int x86_prefetch_sse;
1245
1246 /* ix86_regparm_string as a number */
1247 static int ix86_regparm;
1248
1249 /* -mstackrealign option */
1250 extern int ix86_force_align_arg_pointer;
1251 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1252
1253 /* Preferred alignment for stack boundary in bits.  */
1254 unsigned int ix86_preferred_stack_boundary;
1255
1256 /* Values 1-5: see jump.c */
1257 int ix86_branch_cost;
1258
1259 /* Variables which are this size or smaller are put in the data/bss
1260    or ldata/lbss sections.  */
1261
1262 int ix86_section_threshold = 65536;
1263
1264 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1265 char internal_label_prefix[16];
1266 int internal_label_prefix_len;
1267 \f
1268 static bool ix86_handle_option (size_t, const char *, int);
1269 static void output_pic_addr_const (FILE *, rtx, int);
1270 static void put_condition_code (enum rtx_code, enum machine_mode,
1271                                 int, int, FILE *);
1272 static const char *get_some_local_dynamic_name (void);
1273 static int get_some_local_dynamic_name_1 (rtx *, void *);
1274 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
1275 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
1276                                                    rtx *);
1277 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
1278 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
1279                                                    enum machine_mode);
1280 static rtx get_thread_pointer (int);
1281 static rtx legitimize_tls_address (rtx, enum tls_model, int);
1282 static void get_pc_thunk_name (char [32], unsigned int);
1283 static rtx gen_push (rtx);
1284 static int ix86_flags_dependent (rtx, rtx, enum attr_type);
1285 static int ix86_agi_dependent (rtx, rtx, enum attr_type);
1286 static struct machine_function * ix86_init_machine_status (void);
1287 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
1288 static int ix86_nsaved_regs (void);
1289 static void ix86_emit_save_regs (void);
1290 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
1291 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
1292 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
1293 static HOST_WIDE_INT ix86_GOT_alias_set (void);
1294 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
1295 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
1296 static int ix86_issue_rate (void);
1297 static int ix86_adjust_cost (rtx, rtx, rtx, int);
1298 static int ia32_multipass_dfa_lookahead (void);
1299 static void ix86_init_mmx_sse_builtins (void);
1300 static rtx x86_this_parameter (tree);
1301 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
1302                                  HOST_WIDE_INT, tree);
1303 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
1304 static void x86_file_start (void);
1305 static void ix86_reorg (void);
1306 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
1307 static tree ix86_build_builtin_va_list (void);
1308 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
1309                                          tree, int *, int);
1310 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
1311 static bool ix86_scalar_mode_supported_p (enum machine_mode);
1312 static bool ix86_vector_mode_supported_p (enum machine_mode);
1313
1314 static int ix86_address_cost (rtx);
1315 static bool ix86_cannot_force_const_mem (rtx);
1316 static rtx ix86_delegitimize_address (rtx);
1317
1318 static void i386_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1319
1320 struct builtin_description;
1321 static rtx ix86_expand_sse_comi (const struct builtin_description *,
1322                                  tree, rtx);
1323 static rtx ix86_expand_sse_compare (const struct builtin_description *,
1324                                     tree, rtx);
1325 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
1326 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
1327 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
1328 static rtx ix86_expand_store_builtin (enum insn_code, tree);
1329 static rtx safe_vector_operand (rtx, enum machine_mode);
1330 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
1331 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
1332 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
1333 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
1334 static int ix86_fp_comparison_cost (enum rtx_code code);
1335 static unsigned int ix86_select_alt_pic_regnum (void);
1336 static int ix86_save_reg (unsigned int, int);
1337 static void ix86_compute_frame_layout (struct ix86_frame *);
1338 static int ix86_comp_type_attributes (tree, tree);
1339 static int ix86_function_regparm (tree, tree);
1340 const struct attribute_spec ix86_attribute_table[];
1341 static bool ix86_function_ok_for_sibcall (tree, tree);
1342 static tree ix86_handle_cconv_attribute (tree *, tree, tree, int, bool *);
1343 static int ix86_value_regno (enum machine_mode, tree, tree);
1344 static bool contains_128bit_aligned_vector_p (tree);
1345 static rtx ix86_struct_value_rtx (tree, int);
1346 static bool ix86_ms_bitfield_layout_p (tree);
1347 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
1348 static int extended_reg_mentioned_1 (rtx *, void *);
1349 static bool ix86_rtx_costs (rtx, int, int, int *);
1350 static int min_insn_size (rtx);
1351 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
1352 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
1353 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
1354                                     tree, bool);
1355 static void ix86_init_builtins (void);
1356 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1357 static const char *ix86_mangle_fundamental_type (tree);
1358 static tree ix86_stack_protect_fail (void);
1359 static rtx ix86_internal_arg_pointer (void);
1360 static void ix86_dwarf_handle_frame_unspec (const char *, rtx, int);
1361
1362 /* This function is only used on Solaris.  */
1363 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
1364   ATTRIBUTE_UNUSED;
1365
1366 /* Register class used for passing given 64bit part of the argument.
1367    These represent classes as documented by the PS ABI, with the exception
1368    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1369    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1370
1371    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1372    whenever possible (upper half does contain padding).
1373  */
1374 enum x86_64_reg_class
1375   {
1376     X86_64_NO_CLASS,
1377     X86_64_INTEGER_CLASS,
1378     X86_64_INTEGERSI_CLASS,
1379     X86_64_SSE_CLASS,
1380     X86_64_SSESF_CLASS,
1381     X86_64_SSEDF_CLASS,
1382     X86_64_SSEUP_CLASS,
1383     X86_64_X87_CLASS,
1384     X86_64_X87UP_CLASS,
1385     X86_64_COMPLEX_X87_CLASS,
1386     X86_64_MEMORY_CLASS
1387   };
1388 static const char * const x86_64_reg_class_name[] = {
1389   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1390   "sseup", "x87", "x87up", "cplx87", "no"
1391 };
1392
1393 #define MAX_CLASSES 4
1394
1395 /* Table of constants used by fldpi, fldln2, etc....  */
1396 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1397 static bool ext_80387_constants_init = 0;
1398 static void init_ext_80387_constants (void);
1399 static bool ix86_in_large_data_p (tree) ATTRIBUTE_UNUSED;
1400 static void ix86_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
1401 static void x86_64_elf_unique_section (tree decl, int reloc) ATTRIBUTE_UNUSED;
1402 static section *x86_64_elf_select_section (tree decl, int reloc,
1403                                            unsigned HOST_WIDE_INT align)
1404                                              ATTRIBUTE_UNUSED;
1405 \f
1406 /* Initialize the GCC target structure.  */
1407 #undef TARGET_ATTRIBUTE_TABLE
1408 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
1409 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1410 #  undef TARGET_MERGE_DECL_ATTRIBUTES
1411 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
1412 #endif
1413
1414 #undef TARGET_COMP_TYPE_ATTRIBUTES
1415 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
1416
1417 #undef TARGET_INIT_BUILTINS
1418 #define TARGET_INIT_BUILTINS ix86_init_builtins
1419 #undef TARGET_EXPAND_BUILTIN
1420 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
1421
1422 #undef TARGET_ASM_FUNCTION_EPILOGUE
1423 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
1424
1425 #undef TARGET_ENCODE_SECTION_INFO
1426 #ifndef SUBTARGET_ENCODE_SECTION_INFO
1427 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
1428 #else
1429 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
1430 #endif
1431
1432 #undef TARGET_ASM_OPEN_PAREN
1433 #define TARGET_ASM_OPEN_PAREN ""
1434 #undef TARGET_ASM_CLOSE_PAREN
1435 #define TARGET_ASM_CLOSE_PAREN ""
1436
1437 #undef TARGET_ASM_ALIGNED_HI_OP
1438 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
1439 #undef TARGET_ASM_ALIGNED_SI_OP
1440 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
1441 #ifdef ASM_QUAD
1442 #undef TARGET_ASM_ALIGNED_DI_OP
1443 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1444 #endif
1445
1446 #undef TARGET_ASM_UNALIGNED_HI_OP
1447 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1448 #undef TARGET_ASM_UNALIGNED_SI_OP
1449 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1450 #undef TARGET_ASM_UNALIGNED_DI_OP
1451 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1452
1453 #undef TARGET_SCHED_ADJUST_COST
1454 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1455 #undef TARGET_SCHED_ISSUE_RATE
1456 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1457 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1458 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1459   ia32_multipass_dfa_lookahead
1460
1461 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1462 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1463
1464 #ifdef HAVE_AS_TLS
1465 #undef TARGET_HAVE_TLS
1466 #define TARGET_HAVE_TLS true
1467 #endif
1468 #undef TARGET_CANNOT_FORCE_CONST_MEM
1469 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1470 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1471 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
1472
1473 #undef TARGET_DELEGITIMIZE_ADDRESS
1474 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1475
1476 #undef TARGET_MS_BITFIELD_LAYOUT_P
1477 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1478
1479 #if TARGET_MACHO
1480 #undef TARGET_BINDS_LOCAL_P
1481 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1482 #endif
1483
1484 #undef TARGET_ASM_OUTPUT_MI_THUNK
1485 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1486 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1487 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1488
1489 #undef TARGET_ASM_FILE_START
1490 #define TARGET_ASM_FILE_START x86_file_start
1491
1492 #undef TARGET_DEFAULT_TARGET_FLAGS
1493 #define TARGET_DEFAULT_TARGET_FLAGS     \
1494   (TARGET_DEFAULT                       \
1495    | TARGET_64BIT_DEFAULT               \
1496    | TARGET_SUBTARGET_DEFAULT           \
1497    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
1498
1499 #undef TARGET_HANDLE_OPTION
1500 #define TARGET_HANDLE_OPTION ix86_handle_option
1501
1502 #undef TARGET_RTX_COSTS
1503 #define TARGET_RTX_COSTS ix86_rtx_costs
1504 #undef TARGET_ADDRESS_COST
1505 #define TARGET_ADDRESS_COST ix86_address_cost
1506
1507 #undef TARGET_FIXED_CONDITION_CODE_REGS
1508 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1509 #undef TARGET_CC_MODES_COMPATIBLE
1510 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1511
1512 #undef TARGET_MACHINE_DEPENDENT_REORG
1513 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1514
1515 #undef TARGET_BUILD_BUILTIN_VA_LIST
1516 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1517
1518 #undef TARGET_MD_ASM_CLOBBERS
1519 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1520
1521 #undef TARGET_PROMOTE_PROTOTYPES
1522 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1523 #undef TARGET_STRUCT_VALUE_RTX
1524 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1525 #undef TARGET_SETUP_INCOMING_VARARGS
1526 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1527 #undef TARGET_MUST_PASS_IN_STACK
1528 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1529 #undef TARGET_PASS_BY_REFERENCE
1530 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1531 #undef TARGET_INTERNAL_ARG_POINTER
1532 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
1533 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
1534 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
1535
1536 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1537 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1538
1539 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1540 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
1541
1542 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1543 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1544
1545 #ifdef HAVE_AS_TLS
1546 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1547 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
1548 #endif
1549
1550 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1551 #undef TARGET_INSERT_ATTRIBUTES
1552 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1553 #endif
1554
1555 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
1556 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
1557
1558 #undef TARGET_STACK_PROTECT_FAIL
1559 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
1560
1561 #undef TARGET_FUNCTION_VALUE
1562 #define TARGET_FUNCTION_VALUE ix86_function_value
1563
1564 struct gcc_target targetm = TARGET_INITIALIZER;
1565
1566 \f
1567 /* The svr4 ABI for the i386 says that records and unions are returned
1568    in memory.  */
1569 #ifndef DEFAULT_PCC_STRUCT_RETURN
1570 #define DEFAULT_PCC_STRUCT_RETURN 1
1571 #endif
1572
1573 /* Implement TARGET_HANDLE_OPTION.  */
1574
1575 static bool
1576 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1577 {
1578   switch (code)
1579     {
1580     case OPT_m3dnow:
1581       if (!value)
1582         {
1583           target_flags &= ~MASK_3DNOW_A;
1584           target_flags_explicit |= MASK_3DNOW_A;
1585         }
1586       return true;
1587
1588     case OPT_mmmx:
1589       if (!value)
1590         {
1591           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1592           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1593         }
1594       return true;
1595
1596     case OPT_msse:
1597       if (!value)
1598         {
1599           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1600           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1601         }
1602       return true;
1603
1604     case OPT_msse2:
1605       if (!value)
1606         {
1607           target_flags &= ~MASK_SSE3;
1608           target_flags_explicit |= MASK_SSE3;
1609         }
1610       return true;
1611
1612     default:
1613       return true;
1614     }
1615 }
1616
1617 /* Sometimes certain combinations of command options do not make
1618    sense on a particular target machine.  You can define a macro
1619    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1620    defined, is executed once just after all the command options have
1621    been parsed.
1622
1623    Don't use this macro to turn on various extra optimizations for
1624    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1625
1626 void
1627 override_options (void)
1628 {
1629   int i;
1630   int ix86_tune_defaulted = 0;
1631
1632   /* Comes from final.c -- no real reason to change it.  */
1633 #define MAX_CODE_ALIGN 16
1634
1635   static struct ptt
1636     {
1637       const struct processor_costs *cost;       /* Processor costs */
1638       const int target_enable;                  /* Target flags to enable.  */
1639       const int target_disable;                 /* Target flags to disable.  */
1640       const int align_loop;                     /* Default alignments.  */
1641       const int align_loop_max_skip;
1642       const int align_jump;
1643       const int align_jump_max_skip;
1644       const int align_func;
1645     }
1646   const processor_target_table[PROCESSOR_max] =
1647     {
1648       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1649       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1650       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1651       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1652       {&geode_cost, 0, 0, 0, 0, 0, 0, 0},
1653       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1654       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1655       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1656       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1657       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1658       {&core2_cost, 0, 0, 16, 7, 16, 7, 16},
1659       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1660       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16}
1661     };
1662
1663   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1664   static struct pta
1665     {
1666       const char *const name;           /* processor name or nickname.  */
1667       const enum processor_type processor;
1668       const enum pta_flags
1669         {
1670           PTA_SSE = 1,
1671           PTA_SSE2 = 2,
1672           PTA_SSE3 = 4,
1673           PTA_MMX = 8,
1674           PTA_PREFETCH_SSE = 16,
1675           PTA_3DNOW = 32,
1676           PTA_3DNOW_A = 64,
1677           PTA_64BIT = 128,
1678           PTA_SSSE3 = 256
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       {"core2", PROCESSOR_CORE2, PTA_SSE | PTA_SSE2 | PTA_SSE3
1707                                         | PTA_64BIT | PTA_MMX
1708                                         | PTA_PREFETCH_SSE},
1709       {"geode", PROCESSOR_GEODE, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1710                                    | PTA_3DNOW_A},
1711       {"k6", PROCESSOR_K6, PTA_MMX},
1712       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1713       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1714       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1715                                    | PTA_3DNOW_A},
1716       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1717                                          | PTA_3DNOW | PTA_3DNOW_A},
1718       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1719                                     | PTA_3DNOW_A | PTA_SSE},
1720       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1721                                       | PTA_3DNOW_A | PTA_SSE},
1722       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1723                                       | PTA_3DNOW_A | PTA_SSE},
1724       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1725                                | PTA_SSE | PTA_SSE2 },
1726       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1727                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1728       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1729                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1730       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1731                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1732       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1733                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1734       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1735       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1736     };
1737
1738   int const pta_size = ARRAY_SIZE (processor_alias_table);
1739
1740 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1741   SUBTARGET_OVERRIDE_OPTIONS;
1742 #endif
1743
1744 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1745   SUBSUBTARGET_OVERRIDE_OPTIONS;
1746 #endif
1747
1748   /* -fPIC is the default for x86_64.  */
1749   if (TARGET_MACHO && TARGET_64BIT)
1750     flag_pic = 2;
1751
1752   /* Set the default values for switches whose default depends on TARGET_64BIT
1753      in case they weren't overwritten by command line options.  */
1754   if (TARGET_64BIT)
1755     {
1756       /* Mach-O doesn't support omitting the frame pointer for now.  */
1757       if (flag_omit_frame_pointer == 2)
1758         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1759       if (flag_asynchronous_unwind_tables == 2)
1760         flag_asynchronous_unwind_tables = 1;
1761       if (flag_pcc_struct_return == 2)
1762         flag_pcc_struct_return = 0;
1763     }
1764   else
1765     {
1766       if (flag_omit_frame_pointer == 2)
1767         flag_omit_frame_pointer = 0;
1768       if (flag_asynchronous_unwind_tables == 2)
1769         flag_asynchronous_unwind_tables = 0;
1770       if (flag_pcc_struct_return == 2)
1771         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1772     }
1773
1774   /* Need to check -mtune=generic first.  */
1775   if (ix86_tune_string)
1776     {
1777       if (!strcmp (ix86_tune_string, "generic")
1778           || !strcmp (ix86_tune_string, "i686")
1779           /* As special support for cross compilers we read -mtune=native
1780              as -mtune=generic.  With native compilers we won't see the
1781              -mtune=native, as it was changed by the driver.  */
1782           || !strcmp (ix86_tune_string, "native"))
1783         {
1784           if (TARGET_64BIT)
1785             ix86_tune_string = "generic64";
1786           else
1787             ix86_tune_string = "generic32";
1788         }
1789       else if (!strncmp (ix86_tune_string, "generic", 7))
1790         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1791     }
1792   else
1793     {
1794       if (ix86_arch_string)
1795         ix86_tune_string = ix86_arch_string;
1796       if (!ix86_tune_string)
1797         {
1798           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1799           ix86_tune_defaulted = 1;
1800         }
1801
1802       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1803          need to use a sensible tune option.  */
1804       if (!strcmp (ix86_tune_string, "generic")
1805           || !strcmp (ix86_tune_string, "x86-64")
1806           || !strcmp (ix86_tune_string, "i686"))
1807         {
1808           if (TARGET_64BIT)
1809             ix86_tune_string = "generic64";
1810           else
1811             ix86_tune_string = "generic32";
1812         }
1813     }
1814   if (ix86_stringop_string)
1815     {
1816       if (!strcmp (ix86_stringop_string, "rep_byte"))
1817         stringop_alg = rep_prefix_1_byte;
1818       else if (!strcmp (ix86_stringop_string, "libcall"))
1819         stringop_alg = libcall;
1820       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
1821         stringop_alg = rep_prefix_4_byte;
1822       else if (!strcmp (ix86_stringop_string, "rep_8byte"))
1823         stringop_alg = rep_prefix_8_byte;
1824       else if (!strcmp (ix86_stringop_string, "byte_loop"))
1825         stringop_alg = loop_1_byte;
1826       else if (!strcmp (ix86_stringop_string, "loop"))
1827         stringop_alg = loop;
1828       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
1829         stringop_alg = unrolled_loop;
1830       else
1831         error ("bad value (%s) for -mstringop-strategy= switch", ix86_stringop_string);
1832     }
1833   if (!strcmp (ix86_tune_string, "x86-64"))
1834     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1835              "-mtune=generic instead as appropriate.");
1836
1837   if (!ix86_arch_string)
1838     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1839   if (!strcmp (ix86_arch_string, "generic"))
1840     error ("generic CPU can be used only for -mtune= switch");
1841   if (!strncmp (ix86_arch_string, "generic", 7))
1842     error ("bad value (%s) for -march= switch", ix86_arch_string);
1843
1844   if (ix86_cmodel_string != 0)
1845     {
1846       if (!strcmp (ix86_cmodel_string, "small"))
1847         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1848       else if (!strcmp (ix86_cmodel_string, "medium"))
1849         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1850       else if (flag_pic)
1851         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1852       else if (!strcmp (ix86_cmodel_string, "32"))
1853         ix86_cmodel = CM_32;
1854       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1855         ix86_cmodel = CM_KERNEL;
1856       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1857         ix86_cmodel = CM_LARGE;
1858       else
1859         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1860     }
1861   else
1862     {
1863       ix86_cmodel = CM_32;
1864       if (TARGET_64BIT)
1865         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1866     }
1867   if (ix86_asm_string != 0)
1868     {
1869       if (! TARGET_MACHO
1870           && !strcmp (ix86_asm_string, "intel"))
1871         ix86_asm_dialect = ASM_INTEL;
1872       else if (!strcmp (ix86_asm_string, "att"))
1873         ix86_asm_dialect = ASM_ATT;
1874       else
1875         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1876     }
1877   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1878     error ("code model %qs not supported in the %s bit mode",
1879            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1880   if (ix86_cmodel == CM_LARGE)
1881     sorry ("code model %<large%> not supported yet");
1882   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1883     sorry ("%i-bit mode not compiled in",
1884            (target_flags & MASK_64BIT) ? 64 : 32);
1885
1886   for (i = 0; i < pta_size; i++)
1887     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1888       {
1889         ix86_arch = processor_alias_table[i].processor;
1890         /* Default cpu tuning to the architecture.  */
1891         ix86_tune = ix86_arch;
1892         if (processor_alias_table[i].flags & PTA_MMX
1893             && !(target_flags_explicit & MASK_MMX))
1894           target_flags |= MASK_MMX;
1895         if (processor_alias_table[i].flags & PTA_3DNOW
1896             && !(target_flags_explicit & MASK_3DNOW))
1897           target_flags |= MASK_3DNOW;
1898         if (processor_alias_table[i].flags & PTA_3DNOW_A
1899             && !(target_flags_explicit & MASK_3DNOW_A))
1900           target_flags |= MASK_3DNOW_A;
1901         if (processor_alias_table[i].flags & PTA_SSE
1902             && !(target_flags_explicit & MASK_SSE))
1903           target_flags |= MASK_SSE;
1904         if (processor_alias_table[i].flags & PTA_SSE2
1905             && !(target_flags_explicit & MASK_SSE2))
1906           target_flags |= MASK_SSE2;
1907         if (processor_alias_table[i].flags & PTA_SSE3
1908             && !(target_flags_explicit & MASK_SSE3))
1909           target_flags |= MASK_SSE3;
1910         if (processor_alias_table[i].flags & PTA_SSSE3
1911             && !(target_flags_explicit & MASK_SSSE3))
1912           target_flags |= MASK_SSSE3;
1913         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1914           x86_prefetch_sse = true;
1915         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1916           error ("CPU you selected does not support x86-64 "
1917                  "instruction set");
1918         break;
1919       }
1920
1921   if (i == pta_size)
1922     error ("bad value (%s) for -march= switch", ix86_arch_string);
1923
1924   for (i = 0; i < pta_size; i++)
1925     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1926       {
1927         ix86_tune = processor_alias_table[i].processor;
1928         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1929           {
1930             if (ix86_tune_defaulted)
1931               {
1932                 ix86_tune_string = "x86-64";
1933                 for (i = 0; i < pta_size; i++)
1934                   if (! strcmp (ix86_tune_string,
1935                                 processor_alias_table[i].name))
1936                     break;
1937                 ix86_tune = processor_alias_table[i].processor;
1938               }
1939             else
1940               error ("CPU you selected does not support x86-64 "
1941                      "instruction set");
1942           }
1943         /* Intel CPUs have always interpreted SSE prefetch instructions as
1944            NOPs; so, we can enable SSE prefetch instructions even when
1945            -mtune (rather than -march) points us to a processor that has them.
1946            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1947            higher processors.  */
1948         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1949           x86_prefetch_sse = true;
1950         break;
1951       }
1952   if (i == pta_size)
1953     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1954
1955   if (optimize_size)
1956     ix86_cost = &size_cost;
1957   else
1958     ix86_cost = processor_target_table[ix86_tune].cost;
1959   target_flags |= processor_target_table[ix86_tune].target_enable;
1960   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1961
1962   /* Arrange to set up i386_stack_locals for all functions.  */
1963   init_machine_status = ix86_init_machine_status;
1964
1965   /* Validate -mregparm= value.  */
1966   if (ix86_regparm_string)
1967     {
1968       i = atoi (ix86_regparm_string);
1969       if (i < 0 || i > REGPARM_MAX)
1970         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1971       else
1972         ix86_regparm = i;
1973     }
1974   else
1975    if (TARGET_64BIT)
1976      ix86_regparm = REGPARM_MAX;
1977
1978   /* If the user has provided any of the -malign-* options,
1979      warn and use that value only if -falign-* is not set.
1980      Remove this code in GCC 3.2 or later.  */
1981   if (ix86_align_loops_string)
1982     {
1983       warning (0, "-malign-loops is obsolete, use -falign-loops");
1984       if (align_loops == 0)
1985         {
1986           i = atoi (ix86_align_loops_string);
1987           if (i < 0 || i > MAX_CODE_ALIGN)
1988             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1989           else
1990             align_loops = 1 << i;
1991         }
1992     }
1993
1994   if (ix86_align_jumps_string)
1995     {
1996       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
1997       if (align_jumps == 0)
1998         {
1999           i = atoi (ix86_align_jumps_string);
2000           if (i < 0 || i > MAX_CODE_ALIGN)
2001             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2002           else
2003             align_jumps = 1 << i;
2004         }
2005     }
2006
2007   if (ix86_align_funcs_string)
2008     {
2009       warning (0, "-malign-functions is obsolete, use -falign-functions");
2010       if (align_functions == 0)
2011         {
2012           i = atoi (ix86_align_funcs_string);
2013           if (i < 0 || i > MAX_CODE_ALIGN)
2014             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2015           else
2016             align_functions = 1 << i;
2017         }
2018     }
2019
2020   /* Default align_* from the processor table.  */
2021   if (align_loops == 0)
2022     {
2023       align_loops = processor_target_table[ix86_tune].align_loop;
2024       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
2025     }
2026   if (align_jumps == 0)
2027     {
2028       align_jumps = processor_target_table[ix86_tune].align_jump;
2029       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
2030     }
2031   if (align_functions == 0)
2032     {
2033       align_functions = processor_target_table[ix86_tune].align_func;
2034     }
2035
2036   /* Validate -mbranch-cost= value, or provide default.  */
2037   ix86_branch_cost = ix86_cost->branch_cost;
2038   if (ix86_branch_cost_string)
2039     {
2040       i = atoi (ix86_branch_cost_string);
2041       if (i < 0 || i > 5)
2042         error ("-mbranch-cost=%d is not between 0 and 5", i);
2043       else
2044         ix86_branch_cost = i;
2045     }
2046   if (ix86_section_threshold_string)
2047     {
2048       i = atoi (ix86_section_threshold_string);
2049       if (i < 0)
2050         error ("-mlarge-data-threshold=%d is negative", i);
2051       else
2052         ix86_section_threshold = i;
2053     }
2054
2055   if (ix86_tls_dialect_string)
2056     {
2057       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
2058         ix86_tls_dialect = TLS_DIALECT_GNU;
2059       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
2060         ix86_tls_dialect = TLS_DIALECT_GNU2;
2061       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
2062         ix86_tls_dialect = TLS_DIALECT_SUN;
2063       else
2064         error ("bad value (%s) for -mtls-dialect= switch",
2065                ix86_tls_dialect_string);
2066     }
2067
2068   /* Keep nonleaf frame pointers.  */
2069   if (flag_omit_frame_pointer)
2070     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2071   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
2072     flag_omit_frame_pointer = 1;
2073
2074   /* If we're doing fast math, we don't care about comparison order
2075      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2076   if (flag_finite_math_only)
2077     target_flags &= ~MASK_IEEE_FP;
2078
2079   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2080      since the insns won't need emulation.  */
2081   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
2082     target_flags &= ~MASK_NO_FANCY_MATH_387;
2083
2084   /* Likewise, if the target doesn't have a 387, or we've specified
2085      software floating point, don't use 387 inline intrinsics.  */
2086   if (!TARGET_80387)
2087     target_flags |= MASK_NO_FANCY_MATH_387;
2088
2089   /* Turn on SSE3 builtins for -mssse3.  */
2090   if (TARGET_SSSE3)
2091     target_flags |= MASK_SSE3;
2092
2093   /* Turn on SSE2 builtins for -msse3.  */
2094   if (TARGET_SSE3)
2095     target_flags |= MASK_SSE2;
2096
2097   /* Turn on SSE builtins for -msse2.  */
2098   if (TARGET_SSE2)
2099     target_flags |= MASK_SSE;
2100
2101   /* Turn on MMX builtins for -msse.  */
2102   if (TARGET_SSE)
2103     {
2104       target_flags |= MASK_MMX & ~target_flags_explicit;
2105       x86_prefetch_sse = true;
2106     }
2107
2108   /* Turn on MMX builtins for 3Dnow.  */
2109   if (TARGET_3DNOW)
2110     target_flags |= MASK_MMX;
2111
2112   if (TARGET_64BIT)
2113     {
2114       if (TARGET_ALIGN_DOUBLE)
2115         error ("-malign-double makes no sense in the 64bit mode");
2116       if (TARGET_RTD)
2117         error ("-mrtd calling convention not supported in the 64bit mode");
2118
2119       /* Enable by default the SSE and MMX builtins.  Do allow the user to
2120          explicitly disable any of these.  In particular, disabling SSE and
2121          MMX for kernel code is extremely useful.  */
2122       target_flags
2123         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
2124             & ~target_flags_explicit);
2125      }
2126   else
2127     {
2128       /* i386 ABI does not specify red zone.  It still makes sense to use it
2129          when programmer takes care to stack from being destroyed.  */
2130       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
2131         target_flags |= MASK_NO_RED_ZONE;
2132     }
2133
2134   /* Validate -mpreferred-stack-boundary= value, or provide default.
2135      The default of 128 bits is for Pentium III's SSE __m128.  We can't
2136      change it because of optimize_size.  Otherwise, we can't mix object
2137      files compiled with -Os and -On.  */
2138   ix86_preferred_stack_boundary = 128;
2139   if (ix86_preferred_stack_boundary_string)
2140     {
2141       i = atoi (ix86_preferred_stack_boundary_string);
2142       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
2143         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
2144                TARGET_64BIT ? 4 : 2);
2145       else
2146         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
2147     }
2148
2149   /* Accept -mx87regparm only if 80387 support is enabled.  */
2150   if (TARGET_X87REGPARM
2151       && ! TARGET_80387)
2152     error ("-mx87regparm used without 80387 enabled");
2153
2154   /* Accept -msseregparm only if at least SSE support is enabled.  */
2155   if (TARGET_SSEREGPARM
2156       && ! TARGET_SSE)
2157     error ("-msseregparm used without SSE enabled");
2158
2159   ix86_fpmath = TARGET_FPMATH_DEFAULT;
2160
2161   if (ix86_fpmath_string != 0)
2162     {
2163       if (! strcmp (ix86_fpmath_string, "387"))
2164         ix86_fpmath = FPMATH_387;
2165       else if (! strcmp (ix86_fpmath_string, "sse"))
2166         {
2167           if (!TARGET_SSE)
2168             {
2169               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2170               ix86_fpmath = FPMATH_387;
2171             }
2172           else
2173             ix86_fpmath = FPMATH_SSE;
2174         }
2175       else if (! strcmp (ix86_fpmath_string, "387,sse")
2176                || ! strcmp (ix86_fpmath_string, "sse,387"))
2177         {
2178           if (!TARGET_SSE)
2179             {
2180               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2181               ix86_fpmath = FPMATH_387;
2182             }
2183           else if (!TARGET_80387)
2184             {
2185               warning (0, "387 instruction set disabled, using SSE arithmetics");
2186               ix86_fpmath = FPMATH_SSE;
2187             }
2188           else
2189             ix86_fpmath = FPMATH_SSE | FPMATH_387;
2190         }
2191       else
2192         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2193     }
2194
2195   /* If the i387 is disabled, then do not return values in it. */
2196   if (!TARGET_80387)
2197     target_flags &= ~MASK_FLOAT_RETURNS;
2198
2199   if ((x86_accumulate_outgoing_args & TUNEMASK)
2200       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2201       && !optimize_size)
2202     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2203
2204   /* ??? Unwind info is not correct around the CFG unless either a frame
2205      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2206      unwind info generation to be aware of the CFG and propagating states
2207      around edges.  */
2208   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2209        || flag_exceptions || flag_non_call_exceptions)
2210       && flag_omit_frame_pointer
2211       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2212     {
2213       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2214         warning (0, "unwind tables currently require either a frame pointer "
2215                  "or -maccumulate-outgoing-args for correctness");
2216       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2217     }
2218
2219   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2220   {
2221     char *p;
2222     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2223     p = strchr (internal_label_prefix, 'X');
2224     internal_label_prefix_len = p - internal_label_prefix;
2225     *p = '\0';
2226   }
2227
2228   /* When scheduling description is not available, disable scheduler pass
2229      so it won't slow down the compilation and make x87 code slower.  */
2230   if (!TARGET_SCHEDULE)
2231     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2232
2233   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
2234     set_param_value ("simultaneous-prefetches",
2235                      ix86_cost->simultaneous_prefetches);
2236   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
2237     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
2238 }
2239 \f
2240 /* switch to the appropriate section for output of DECL.
2241    DECL is either a `VAR_DECL' node or a constant of some sort.
2242    RELOC indicates whether forming the initial value of DECL requires
2243    link-time relocations.  */
2244
2245 static section *
2246 x86_64_elf_select_section (tree decl, int reloc,
2247                            unsigned HOST_WIDE_INT align)
2248 {
2249   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2250       && ix86_in_large_data_p (decl))
2251     {
2252       const char *sname = NULL;
2253       unsigned int flags = SECTION_WRITE;
2254       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2255         {
2256         case SECCAT_DATA:
2257           sname = ".ldata";
2258           break;
2259         case SECCAT_DATA_REL:
2260           sname = ".ldata.rel";
2261           break;
2262         case SECCAT_DATA_REL_LOCAL:
2263           sname = ".ldata.rel.local";
2264           break;
2265         case SECCAT_DATA_REL_RO:
2266           sname = ".ldata.rel.ro";
2267           break;
2268         case SECCAT_DATA_REL_RO_LOCAL:
2269           sname = ".ldata.rel.ro.local";
2270           break;
2271         case SECCAT_BSS:
2272           sname = ".lbss";
2273           flags |= SECTION_BSS;
2274           break;
2275         case SECCAT_RODATA:
2276         case SECCAT_RODATA_MERGE_STR:
2277         case SECCAT_RODATA_MERGE_STR_INIT:
2278         case SECCAT_RODATA_MERGE_CONST:
2279           sname = ".lrodata";
2280           flags = 0;
2281           break;
2282         case SECCAT_SRODATA:
2283         case SECCAT_SDATA:
2284         case SECCAT_SBSS:
2285           gcc_unreachable ();
2286         case SECCAT_TEXT:
2287         case SECCAT_TDATA:
2288         case SECCAT_TBSS:
2289           /* We don't split these for medium model.  Place them into
2290              default sections and hope for best.  */
2291           break;
2292         }
2293       if (sname)
2294         {
2295           /* We might get called with string constants, but get_named_section
2296              doesn't like them as they are not DECLs.  Also, we need to set
2297              flags in that case.  */
2298           if (!DECL_P (decl))
2299             return get_section (sname, flags, NULL);
2300           return get_named_section (decl, sname, reloc);
2301         }
2302     }
2303   return default_elf_select_section (decl, reloc, align);
2304 }
2305
2306 /* Build up a unique section name, expressed as a
2307    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2308    RELOC indicates whether the initial value of EXP requires
2309    link-time relocations.  */
2310
2311 static void
2312 x86_64_elf_unique_section (tree decl, int reloc)
2313 {
2314   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2315       && ix86_in_large_data_p (decl))
2316     {
2317       const char *prefix = NULL;
2318       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2319       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2320
2321       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2322         {
2323         case SECCAT_DATA:
2324         case SECCAT_DATA_REL:
2325         case SECCAT_DATA_REL_LOCAL:
2326         case SECCAT_DATA_REL_RO:
2327         case SECCAT_DATA_REL_RO_LOCAL:
2328           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2329           break;
2330         case SECCAT_BSS:
2331           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2332           break;
2333         case SECCAT_RODATA:
2334         case SECCAT_RODATA_MERGE_STR:
2335         case SECCAT_RODATA_MERGE_STR_INIT:
2336         case SECCAT_RODATA_MERGE_CONST:
2337           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2338           break;
2339         case SECCAT_SRODATA:
2340         case SECCAT_SDATA:
2341         case SECCAT_SBSS:
2342           gcc_unreachable ();
2343         case SECCAT_TEXT:
2344         case SECCAT_TDATA:
2345         case SECCAT_TBSS:
2346           /* We don't split these for medium model.  Place them into
2347              default sections and hope for best.  */
2348           break;
2349         }
2350       if (prefix)
2351         {
2352           const char *name;
2353           size_t nlen, plen;
2354           char *string;
2355           plen = strlen (prefix);
2356
2357           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2358           name = targetm.strip_name_encoding (name);
2359           nlen = strlen (name);
2360
2361           string = alloca (nlen + plen + 1);
2362           memcpy (string, prefix, plen);
2363           memcpy (string + plen, name, nlen + 1);
2364
2365           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2366           return;
2367         }
2368     }
2369   default_unique_section (decl, reloc);
2370 }
2371
2372 #ifdef COMMON_ASM_OP
2373 /* This says how to output assembler code to declare an
2374    uninitialized external linkage data object.
2375
2376    For medium model x86-64 we need to use .largecomm opcode for
2377    large objects.  */
2378 void
2379 x86_elf_aligned_common (FILE *file,
2380                         const char *name, unsigned HOST_WIDE_INT size,
2381                         int align)
2382 {
2383   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2384       && size > (unsigned int)ix86_section_threshold)
2385     fprintf (file, ".largecomm\t");
2386   else
2387     fprintf (file, "%s", COMMON_ASM_OP);
2388   assemble_name (file, name);
2389   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2390            size, align / BITS_PER_UNIT);
2391 }
2392
2393 /* Utility function for targets to use in implementing
2394    ASM_OUTPUT_ALIGNED_BSS.  */
2395
2396 void
2397 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2398                         const char *name, unsigned HOST_WIDE_INT size,
2399                         int align)
2400 {
2401   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2402       && size > (unsigned int)ix86_section_threshold)
2403     switch_to_section (get_named_section (decl, ".lbss", 0));
2404   else
2405     switch_to_section (bss_section);
2406   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2407 #ifdef ASM_DECLARE_OBJECT_NAME
2408   last_assemble_variable_decl = decl;
2409   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2410 #else
2411   /* Standard thing is just output label for the object.  */
2412   ASM_OUTPUT_LABEL (file, name);
2413 #endif /* ASM_DECLARE_OBJECT_NAME */
2414   ASM_OUTPUT_SKIP (file, size ? size : 1);
2415 }
2416 #endif
2417 \f
2418 void
2419 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2420 {
2421   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2422      make the problem with not enough registers even worse.  */
2423 #ifdef INSN_SCHEDULING
2424   if (level > 1)
2425     flag_schedule_insns = 0;
2426 #endif
2427
2428   if (TARGET_MACHO)
2429     /* The Darwin libraries never set errno, so we might as well
2430        avoid calling them when that's the only reason we would.  */
2431     flag_errno_math = 0;
2432
2433   /* The default values of these switches depend on the TARGET_64BIT
2434      that is not known at this moment.  Mark these values with 2 and
2435      let user the to override these.  In case there is no command line option
2436      specifying them, we will set the defaults in override_options.  */
2437   if (optimize >= 1)
2438     flag_omit_frame_pointer = 2;
2439   flag_pcc_struct_return = 2;
2440   flag_asynchronous_unwind_tables = 2;
2441 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2442   SUBTARGET_OPTIMIZATION_OPTIONS;
2443 #endif
2444 }
2445 \f
2446 /* Table of valid machine attributes.  */
2447 const struct attribute_spec ix86_attribute_table[] =
2448 {
2449   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2450   /* Stdcall attribute says callee is responsible for popping arguments
2451      if they are not variable.  */
2452   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2453   /* Fastcall attribute says callee is responsible for popping arguments
2454      if they are not variable.  */
2455   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2456   /* Cdecl attribute says the callee is a normal C declaration */
2457   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2458   /* Regparm attribute specifies how many integer arguments are to be
2459      passed in registers.  */
2460   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
2461   /* X87regparm attribute says we are passing floating point arguments
2462      in 80387 registers.  */
2463   { "x87regparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
2464   /* Sseregparm attribute says we are using x86_64 calling conventions
2465      for FP arguments.  */
2466   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
2467   /* force_align_arg_pointer says this function realigns the stack at entry.  */
2468   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
2469     false, true,  true, ix86_handle_cconv_attribute },
2470 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2471   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2472   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2473   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
2474 #endif
2475   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2476   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2477 #ifdef SUBTARGET_ATTRIBUTE_TABLE
2478   SUBTARGET_ATTRIBUTE_TABLE,
2479 #endif
2480   { NULL,        0, 0, false, false, false, NULL }
2481 };
2482
2483 /* Decide whether we can make a sibling call to a function.  DECL is the
2484    declaration of the function being targeted by the call and EXP is the
2485    CALL_EXPR representing the call.  */
2486
2487 static bool
2488 ix86_function_ok_for_sibcall (tree decl, tree exp)
2489 {
2490   tree func;
2491   rtx a, b;
2492
2493   /* If we are generating position-independent code, we cannot sibcall
2494      optimize any indirect call, or a direct call to a global function,
2495      as the PLT requires %ebx be live.  */
2496   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2497     return false;
2498
2499   if (decl)
2500     func = decl;
2501   else
2502     {
2503       func = TREE_TYPE (TREE_OPERAND (exp, 0));
2504       if (POINTER_TYPE_P (func))
2505         func = TREE_TYPE (func);
2506     }
2507
2508   /* Check that the return value locations are the same.  Like
2509      if we are returning floats on the 80387 register stack, we cannot
2510      make a sibcall from a function that doesn't return a float to a
2511      function that does or, conversely, from a function that does return
2512      a float to a function that doesn't; the necessary stack adjustment
2513      would not be executed.  This is also the place we notice
2514      differences in the return value ABI.  Note that it is ok for one
2515      of the functions to have void return type as long as the return
2516      value of the other is passed in a register.  */
2517   a = ix86_function_value (TREE_TYPE (exp), func, false);
2518   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2519                            cfun->decl, false);
2520   if (STACK_REG_P (a) || STACK_REG_P (b))
2521     {
2522       if (!rtx_equal_p (a, b))
2523         return false;
2524     }
2525   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2526     ;
2527   else if (!rtx_equal_p (a, b))
2528     return false;
2529
2530   /* If this call is indirect, we'll need to be able to use a call-clobbered
2531      register for the address of the target function.  Make sure that all
2532      such registers are not used for passing parameters.  */
2533   if (!decl && !TARGET_64BIT)
2534     {
2535       tree type;
2536
2537       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2538       type = TREE_OPERAND (exp, 0);             /* pointer expression */
2539       type = TREE_TYPE (type);                  /* pointer type */
2540       type = TREE_TYPE (type);                  /* function type */
2541
2542       if (ix86_function_regparm (type, NULL) >= 3)
2543         {
2544           /* ??? Need to count the actual number of registers to be used,
2545              not the possible number of registers.  Fix later.  */
2546           return false;
2547         }
2548     }
2549
2550 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2551   /* Dllimport'd functions are also called indirectly.  */
2552   if (decl && DECL_DLLIMPORT_P (decl)
2553       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2554     return false;
2555 #endif
2556
2557   /* If we forced aligned the stack, then sibcalling would unalign the
2558      stack, which may break the called function.  */
2559   if (cfun->machine->force_align_arg_pointer)
2560     return false;
2561
2562   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2563   return true;
2564 }
2565
2566 /* Handle "cdecl", "stdcall", "fastcall", "regparm", "x87regparm"
2567    and "sseregparm" calling convention attributes;
2568    arguments as in struct attribute_spec.handler.  */
2569
2570 static tree
2571 ix86_handle_cconv_attribute (tree *node, tree name,
2572                                    tree args,
2573                                    int flags ATTRIBUTE_UNUSED,
2574                                    bool *no_add_attrs)
2575 {
2576   if (TREE_CODE (*node) != FUNCTION_TYPE
2577       && TREE_CODE (*node) != METHOD_TYPE
2578       && TREE_CODE (*node) != FIELD_DECL
2579       && TREE_CODE (*node) != TYPE_DECL)
2580     {
2581       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2582                IDENTIFIER_POINTER (name));
2583       *no_add_attrs = true;
2584       return NULL_TREE;
2585     }
2586
2587   /* Can combine regparm with all attributes but fastcall.  */
2588   if (is_attribute_p ("regparm", name))
2589     {
2590       tree cst;
2591
2592       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2593         {
2594           error ("fastcall and regparm attributes are not compatible");
2595         }
2596
2597       cst = TREE_VALUE (args);
2598       if (TREE_CODE (cst) != INTEGER_CST)
2599         {
2600           warning (OPT_Wattributes,
2601                    "%qs attribute requires an integer constant argument",
2602                    IDENTIFIER_POINTER (name));
2603           *no_add_attrs = true;
2604         }
2605       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2606         {
2607           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2608                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2609           *no_add_attrs = true;
2610         }
2611
2612       if (!TARGET_64BIT
2613           && lookup_attribute (ix86_force_align_arg_pointer_string,
2614                                TYPE_ATTRIBUTES (*node))
2615           && compare_tree_int (cst, REGPARM_MAX-1))
2616         {
2617           error ("%s functions limited to %d register parameters",
2618                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2619         }
2620
2621       return NULL_TREE;
2622     }
2623
2624   if (TARGET_64BIT)
2625     {
2626       warning (OPT_Wattributes, "%qs attribute ignored",
2627                IDENTIFIER_POINTER (name));
2628       *no_add_attrs = true;
2629       return NULL_TREE;
2630     }
2631
2632   /* Can combine fastcall with stdcall (redundant), x87regparm
2633      and sseregparm.  */
2634   if (is_attribute_p ("fastcall", name))
2635     {
2636       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2637         {
2638           error ("fastcall and cdecl attributes are not compatible");
2639         }
2640       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2641         {
2642           error ("fastcall and stdcall attributes are not compatible");
2643         }
2644       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2645         {
2646           error ("fastcall and regparm attributes are not compatible");
2647         }
2648     }
2649
2650   /* Can combine stdcall with fastcall (redundant), regparm,
2651      x87regparm and sseregparm.  */
2652   else if (is_attribute_p ("stdcall", name))
2653     {
2654       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2655         {
2656           error ("stdcall and cdecl attributes are not compatible");
2657         }
2658       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2659         {
2660           error ("stdcall and fastcall attributes are not compatible");
2661         }
2662     }
2663
2664   /* Can combine cdecl with regparm, x87regparm and sseregparm.  */
2665   else if (is_attribute_p ("cdecl", name))
2666     {
2667       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2668         {
2669           error ("stdcall and cdecl attributes are not compatible");
2670         }
2671       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2672         {
2673           error ("fastcall and cdecl attributes are not compatible");
2674         }
2675     }
2676
2677   /* Can combine x87regparm or sseregparm with all attributes.  */
2678
2679   return NULL_TREE;
2680 }
2681
2682 /* Return 0 if the attributes for two types are incompatible, 1 if they
2683    are compatible, and 2 if they are nearly compatible (which causes a
2684    warning to be generated).  */
2685
2686 static int
2687 ix86_comp_type_attributes (tree type1, tree type2)
2688 {
2689   /* Check for mismatch of non-default calling convention.  */
2690   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2691
2692   if (TREE_CODE (type1) != FUNCTION_TYPE)
2693     return 1;
2694
2695   /* Check for mismatched fastcall/regparm types.  */
2696   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2697        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2698       || (ix86_function_regparm (type1, NULL)
2699           != ix86_function_regparm (type2, NULL)))
2700     return 0;
2701
2702   /* Check for mismatched x87regparm types.  */
2703   if (!lookup_attribute ("x87regparm", TYPE_ATTRIBUTES (type1))
2704       != !lookup_attribute ("x87regparm", TYPE_ATTRIBUTES (type2)))
2705     return 0;
2706
2707   /* Check for mismatched sseregparm types.  */
2708   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2709       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2710     return 0;
2711
2712   /* Check for mismatched return types (cdecl vs stdcall).  */
2713   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2714       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2715     return 0;
2716
2717   return 1;
2718 }
2719 \f
2720 /* Return the regparm value for a function with the indicated TYPE and DECL.
2721    DECL may be NULL when calling function indirectly
2722    or considering a libcall.  */
2723
2724 static int
2725 ix86_function_regparm (tree type, tree decl)
2726 {
2727   tree attr;
2728   int regparm = ix86_regparm;
2729   bool user_convention = false;
2730
2731   if (!TARGET_64BIT)
2732     {
2733       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2734       if (attr)
2735         {
2736           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2737           user_convention = true;
2738         }
2739
2740       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2741         {
2742           regparm = 2;
2743           user_convention = true;
2744         }
2745
2746       /* Use register calling convention for local functions when possible.  */
2747       if (!TARGET_64BIT && !user_convention && decl
2748           && flag_unit_at_a_time && !profile_flag)
2749         {
2750           struct cgraph_local_info *i = cgraph_local_info (decl);
2751           if (i && i->local)
2752             {
2753               int local_regparm, globals = 0, regno;
2754
2755               /* Make sure no regparm register is taken by a global register
2756                  variable.  */
2757               for (local_regparm = 0; local_regparm < 3; local_regparm++)
2758                 if (global_regs[local_regparm])
2759                   break;
2760               /* We can't use regparm(3) for nested functions as these use
2761                  static chain pointer in third argument.  */
2762               if (local_regparm == 3
2763                   && decl_function_context (decl)
2764                   && !DECL_NO_STATIC_CHAIN (decl))
2765                 local_regparm = 2;
2766               /* If the function realigns its stackpointer, the
2767                  prologue will clobber %ecx.  If we've already
2768                  generated code for the callee, the callee
2769                  DECL_STRUCT_FUNCTION is gone, so we fall back to
2770                  scanning the attributes for the self-realigning
2771                  property.  */
2772               if ((DECL_STRUCT_FUNCTION (decl)
2773                    && DECL_STRUCT_FUNCTION (decl)->machine->force_align_arg_pointer)
2774                   || (!DECL_STRUCT_FUNCTION (decl)
2775                       && lookup_attribute (ix86_force_align_arg_pointer_string,
2776                                            TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2777                 local_regparm = 2;
2778               /* Each global register variable increases register preassure,
2779                  so the more global reg vars there are, the smaller regparm
2780                  optimization use, unless requested by the user explicitly.  */
2781               for (regno = 0; regno < 6; regno++)
2782                 if (global_regs[regno])
2783                   globals++;
2784               local_regparm
2785                 = globals < local_regparm ? local_regparm - globals : 0;
2786
2787               if (local_regparm > regparm)
2788                 regparm = local_regparm;
2789             }
2790         }
2791     }
2792   return regparm;
2793 }
2794
2795 /* Return 1 if we can pass up to X87_REGPARM_MAX floating point
2796    arguments in x87 registers for a function with the indicated
2797    TYPE and DECL.  DECL may be NULL when calling function indirectly
2798    or considering a libcall.  For local functions, return 2.
2799    Otherwise return 0.  */
2800
2801 static int
2802 ix86_function_x87regparm (tree type, tree decl)
2803 {
2804   /* Use x87 registers to pass floating point arguments if requested
2805      by the x87regparm attribute.  */
2806   if (TARGET_X87REGPARM
2807       || (type
2808           && lookup_attribute ("x87regparm", TYPE_ATTRIBUTES (type))))
2809     {
2810       if (!TARGET_80387)
2811         {
2812           if (decl)
2813             error ("Calling %qD with attribute x87regparm without "
2814                    "80387 enabled", decl);
2815           else
2816             error ("Calling %qT with attribute x87regparm without "
2817                    "80387 enabled", type);
2818           return 0;
2819         }
2820
2821       return 1;
2822     }
2823
2824   /* For local functions, pass up to X87_REGPARM_MAX floating point
2825      arguments in x87 registers.  */
2826   if (!TARGET_64BIT && decl
2827       && flag_unit_at_a_time && !profile_flag)
2828     {
2829       struct cgraph_local_info *i = cgraph_local_info (decl);
2830       if (i && i->local)
2831         return 2;
2832     }
2833
2834   return 0;
2835 }
2836
2837 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
2838    DFmode (2) arguments in SSE registers for a function with the
2839    indicated TYPE and DECL.  DECL may be NULL when calling function
2840    indirectly or considering a libcall.  Otherwise return 0.  */
2841
2842 static int
2843 ix86_function_sseregparm (tree type, tree decl)
2844 {
2845   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2846      by the sseregparm attribute.  */
2847   if (TARGET_SSEREGPARM
2848       || (type
2849           && 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      even for 32-bit targets.  */
2868   if (!TARGET_64BIT && decl
2869       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2870     {
2871       struct cgraph_local_info *i = cgraph_local_info (decl);
2872       if (i && i->local)
2873         return TARGET_SSE2 ? 2 : 1;
2874     }
2875
2876   return 0;
2877 }
2878
2879 /* Return true if EAX is live at the start of the function.  Used by
2880    ix86_expand_prologue to determine if we need special help before
2881    calling allocate_stack_worker.  */
2882
2883 static bool
2884 ix86_eax_live_at_start_p (void)
2885 {
2886   /* Cheat.  Don't bother working forward from ix86_function_regparm
2887      to the function type to whether an actual argument is located in
2888      eax.  Instead just look at cfg info, which is still close enough
2889      to correct at this point.  This gives false positives for broken
2890      functions that might use uninitialized data that happens to be
2891      allocated in eax, but who cares?  */
2892   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2893 }
2894
2895 /* Value is the number of bytes of arguments automatically
2896    popped when returning from a subroutine call.
2897    FUNDECL is the declaration node of the function (as a tree),
2898    FUNTYPE is the data type of the function (as a tree),
2899    or for a library call it is an identifier node for the subroutine name.
2900    SIZE is the number of bytes of arguments passed on the stack.
2901
2902    On the 80386, the RTD insn may be used to pop them if the number
2903      of args is fixed, but if the number is variable then the caller
2904      must pop them all.  RTD can't be used for library calls now
2905      because the library is compiled with the Unix compiler.
2906    Use of RTD is a selectable option, since it is incompatible with
2907    standard Unix calling sequences.  If the option is not selected,
2908    the caller must always pop the args.
2909
2910    The attribute stdcall is equivalent to RTD on a per module basis.  */
2911
2912 int
2913 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2914 {
2915   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2916
2917   /* Cdecl functions override -mrtd, and never pop the stack.  */
2918   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2919
2920     /* Stdcall and fastcall functions will pop the stack if not
2921        variable args.  */
2922     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2923         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2924       rtd = 1;
2925
2926     if (rtd
2927         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2928             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2929                 == void_type_node)))
2930       return size;
2931   }
2932
2933   /* Lose any fake structure return argument if it is passed on the stack.  */
2934   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2935       && !TARGET_64BIT
2936       && !KEEP_AGGREGATE_RETURN_POINTER)
2937     {
2938       int nregs = ix86_function_regparm (funtype, fundecl);
2939
2940       if (!nregs)
2941         return GET_MODE_SIZE (Pmode);
2942     }
2943
2944   return 0;
2945 }
2946 \f
2947 /* Argument support functions.  */
2948
2949 /* Return true when register may be used to pass function parameters.  */
2950 bool
2951 ix86_function_arg_regno_p (int regno)
2952 {
2953   int i;
2954   if (!TARGET_64BIT)
2955     return (regno < REGPARM_MAX
2956             || (TARGET_80387 && FP_REGNO_P (regno)
2957                 && (regno < FIRST_FLOAT_REG + X87_REGPARM_MAX))
2958             || (TARGET_MMX && MMX_REGNO_P (regno)
2959                 && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2960             || (TARGET_SSE && SSE_REGNO_P (regno)
2961                 && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2962
2963   if (TARGET_SSE && SSE_REGNO_P (regno)
2964       && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2965     return true;
2966   /* RAX is used as hidden argument to va_arg functions.  */
2967   if (!regno)
2968     return true;
2969   for (i = 0; i < REGPARM_MAX; i++)
2970     if (regno == x86_64_int_parameter_registers[i])
2971       return true;
2972   return false;
2973 }
2974
2975 /* Return if we do not know how to pass TYPE solely in registers.  */
2976
2977 static bool
2978 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2979 {
2980   if (must_pass_in_stack_var_size_or_pad (mode, type))
2981     return true;
2982
2983   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2984      The layout_type routine is crafty and tries to trick us into passing
2985      currently unsupported vector types on the stack by using TImode.  */
2986   return (!TARGET_64BIT && mode == TImode
2987           && type && TREE_CODE (type) != VECTOR_TYPE);
2988 }
2989
2990 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2991    for a call to a function whose data type is FNTYPE.
2992    For a library call, FNTYPE is 0.  */
2993
2994 void
2995 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2996                       tree fntype,      /* tree ptr for function decl */
2997                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2998                       tree fndecl)
2999 {
3000   static CUMULATIVE_ARGS zero_cum;
3001   tree param, next_param;
3002
3003   if (TARGET_DEBUG_ARG)
3004     {
3005       fprintf (stderr, "\ninit_cumulative_args (");
3006       if (fntype)
3007         fprintf (stderr, "fntype code = %s, ret code = %s",
3008                  tree_code_name[(int) TREE_CODE (fntype)],
3009                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
3010       else
3011         fprintf (stderr, "no fntype");
3012
3013       if (libname)
3014         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
3015     }
3016
3017   *cum = zero_cum;
3018
3019   /* Set up the number of registers to use for passing arguments.  */
3020   cum->nregs = ix86_regparm;
3021   if (TARGET_80387)
3022     cum->x87_nregs = X87_REGPARM_MAX;
3023   if (TARGET_SSE)
3024     cum->sse_nregs = SSE_REGPARM_MAX;
3025   if (TARGET_MMX)
3026     cum->mmx_nregs = MMX_REGPARM_MAX;
3027   cum->warn_sse = true;
3028   cum->warn_mmx = true;
3029   cum->maybe_vaarg = false;
3030
3031   /* Use ecx and edx registers if function has fastcall attribute,
3032      else look for regparm information.  */
3033   if (fntype && !TARGET_64BIT)
3034     {
3035       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
3036         {
3037           cum->nregs = 2;
3038           cum->fastcall = 1;
3039         }
3040       else
3041         cum->nregs = ix86_function_regparm (fntype, fndecl);
3042     }
3043
3044   /* Set up the number of 80387 registers used for passing
3045      floating point arguments.  Warn for mismatching ABI.  */
3046   cum->float_in_x87 = ix86_function_x87regparm (fntype, fndecl);
3047
3048   /* Set up the number of SSE registers used for passing SFmode
3049      and DFmode arguments.  Warn for mismatching ABI.  */
3050   cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
3051
3052   /* Determine if this function has variable arguments.  This is
3053      indicated by the last argument being 'void_type_mode' if there
3054      are no variable arguments.  If there are variable arguments, then
3055      we won't pass anything in registers in 32-bit mode. */
3056
3057   if (cum->nregs || cum->mmx_nregs
3058       || cum->x87_nregs || cum->sse_nregs)
3059     {
3060       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
3061            param != 0; param = next_param)
3062         {
3063           next_param = TREE_CHAIN (param);
3064           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
3065             {
3066               if (!TARGET_64BIT)
3067                 {
3068                   cum->nregs = 0;
3069                   cum->x87_nregs = 0;
3070                   cum->sse_nregs = 0;
3071                   cum->mmx_nregs = 0;
3072                   cum->warn_sse = 0;
3073                   cum->warn_mmx = 0;
3074                   cum->fastcall = 0;
3075                   cum->float_in_x87 = 0;
3076                   cum->float_in_sse = 0;
3077                 }
3078               cum->maybe_vaarg = true;
3079             }
3080         }
3081     }
3082   if ((!fntype && !libname)
3083       || (fntype && !TYPE_ARG_TYPES (fntype)))
3084     cum->maybe_vaarg = true;
3085
3086   if (TARGET_DEBUG_ARG)
3087     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
3088
3089   return;
3090 }
3091
3092 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
3093    But in the case of vector types, it is some vector mode.
3094
3095    When we have only some of our vector isa extensions enabled, then there
3096    are some modes for which vector_mode_supported_p is false.  For these
3097    modes, the generic vector support in gcc will choose some non-vector mode
3098    in order to implement the type.  By computing the natural mode, we'll
3099    select the proper ABI location for the operand and not depend on whatever
3100    the middle-end decides to do with these vector types.  */
3101
3102 static enum machine_mode
3103 type_natural_mode (tree type)
3104 {
3105   enum machine_mode mode = TYPE_MODE (type);
3106
3107   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
3108     {
3109       HOST_WIDE_INT size = int_size_in_bytes (type);
3110       if ((size == 8 || size == 16)
3111           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
3112           && TYPE_VECTOR_SUBPARTS (type) > 1)
3113         {
3114           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
3115
3116           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
3117             mode = MIN_MODE_VECTOR_FLOAT;
3118           else
3119             mode = MIN_MODE_VECTOR_INT;
3120
3121           /* Get the mode which has this inner mode and number of units.  */
3122           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3123             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
3124                 && GET_MODE_INNER (mode) == innermode)
3125               return mode;
3126
3127           gcc_unreachable ();
3128         }
3129     }
3130
3131   return mode;
3132 }
3133
3134 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
3135    this may not agree with the mode that the type system has chosen for the
3136    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
3137    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
3138
3139 static rtx
3140 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
3141                      unsigned int regno)
3142 {
3143   rtx tmp;
3144
3145   if (orig_mode != BLKmode)
3146     tmp = gen_rtx_REG (orig_mode, regno);
3147   else
3148     {
3149       tmp = gen_rtx_REG (mode, regno);
3150       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
3151       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
3152     }
3153
3154   return tmp;
3155 }
3156
3157 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
3158    of this code is to classify each 8bytes of incoming argument by the register
3159    class and assign registers accordingly.  */
3160
3161 /* Return the union class of CLASS1 and CLASS2.
3162    See the x86-64 PS ABI for details.  */
3163
3164 static enum x86_64_reg_class
3165 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
3166 {
3167   /* Rule #1: If both classes are equal, this is the resulting class.  */
3168   if (class1 == class2)
3169     return class1;
3170
3171   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
3172      the other class.  */
3173   if (class1 == X86_64_NO_CLASS)
3174     return class2;
3175   if (class2 == X86_64_NO_CLASS)
3176     return class1;
3177
3178   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
3179   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
3180     return X86_64_MEMORY_CLASS;
3181
3182   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
3183   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
3184       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
3185     return X86_64_INTEGERSI_CLASS;
3186   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
3187       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
3188     return X86_64_INTEGER_CLASS;
3189
3190   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
3191      MEMORY is used.  */
3192   if (class1 == X86_64_X87_CLASS
3193       || class1 == X86_64_X87UP_CLASS
3194       || class1 == X86_64_COMPLEX_X87_CLASS
3195       || class2 == X86_64_X87_CLASS
3196       || class2 == X86_64_X87UP_CLASS
3197       || class2 == X86_64_COMPLEX_X87_CLASS)
3198     return X86_64_MEMORY_CLASS;
3199
3200   /* Rule #6: Otherwise class SSE is used.  */
3201   return X86_64_SSE_CLASS;
3202 }
3203
3204 /* Classify the argument of type TYPE and mode MODE.
3205    CLASSES will be filled by the register class used to pass each word
3206    of the operand.  The number of words is returned.  In case the parameter
3207    should be passed in memory, 0 is returned. As a special case for zero
3208    sized containers, classes[0] will be NO_CLASS and 1 is returned.
3209
3210    BIT_OFFSET is used internally for handling records and specifies offset
3211    of the offset in bits modulo 256 to avoid overflow cases.
3212
3213    See the x86-64 PS ABI for details.
3214 */
3215
3216 static int
3217 classify_argument (enum machine_mode mode, tree type,
3218                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
3219 {
3220   HOST_WIDE_INT bytes =
3221     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3222   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3223
3224   /* Variable sized entities are always passed/returned in memory.  */
3225   if (bytes < 0)
3226     return 0;
3227
3228   if (mode != VOIDmode
3229       && targetm.calls.must_pass_in_stack (mode, type))
3230     return 0;
3231
3232   if (type && AGGREGATE_TYPE_P (type))
3233     {
3234       int i;
3235       tree field;
3236       enum x86_64_reg_class subclasses[MAX_CLASSES];
3237
3238       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3239       if (bytes > 16)
3240         return 0;
3241
3242       for (i = 0; i < words; i++)
3243         classes[i] = X86_64_NO_CLASS;
3244
3245       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3246          signalize memory class, so handle it as special case.  */
3247       if (!words)
3248         {
3249           classes[0] = X86_64_NO_CLASS;
3250           return 1;
3251         }
3252
3253       /* Classify each field of record and merge classes.  */
3254       switch (TREE_CODE (type))
3255         {
3256         case RECORD_TYPE:
3257           /* And now merge the fields of structure.  */
3258           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3259             {
3260               if (TREE_CODE (field) == FIELD_DECL)
3261                 {
3262                   int num;
3263
3264                   if (TREE_TYPE (field) == error_mark_node)
3265                     continue;
3266
3267                   /* Bitfields are always classified as integer.  Handle them
3268                      early, since later code would consider them to be
3269                      misaligned integers.  */
3270                   if (DECL_BIT_FIELD (field))
3271                     {
3272                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3273                            i < ((int_bit_position (field) + (bit_offset % 64))
3274                                 + tree_low_cst (DECL_SIZE (field), 0)
3275                                 + 63) / 8 / 8; i++)
3276                         classes[i] =
3277                           merge_classes (X86_64_INTEGER_CLASS,
3278                                          classes[i]);
3279                     }
3280                   else
3281                     {
3282                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3283                                                TREE_TYPE (field), subclasses,
3284                                                (int_bit_position (field)
3285                                                 + bit_offset) % 256);
3286                       if (!num)
3287                         return 0;
3288                       for (i = 0; i < num; i++)
3289                         {
3290                           int pos =
3291                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3292                           classes[i + pos] =
3293                             merge_classes (subclasses[i], classes[i + pos]);
3294                         }
3295                     }
3296                 }
3297             }
3298           break;
3299
3300         case ARRAY_TYPE:
3301           /* Arrays are handled as small records.  */
3302           {
3303             int num;
3304             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3305                                      TREE_TYPE (type), subclasses, bit_offset);
3306             if (!num)
3307               return 0;
3308
3309             /* The partial classes are now full classes.  */
3310             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3311               subclasses[0] = X86_64_SSE_CLASS;
3312             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3313               subclasses[0] = X86_64_INTEGER_CLASS;
3314
3315             for (i = 0; i < words; i++)
3316               classes[i] = subclasses[i % num];
3317
3318             break;
3319           }
3320         case UNION_TYPE:
3321         case QUAL_UNION_TYPE:
3322           /* Unions are similar to RECORD_TYPE but offset is always 0.
3323              */
3324           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3325             {
3326               if (TREE_CODE (field) == FIELD_DECL)
3327                 {
3328                   int num;
3329
3330                   if (TREE_TYPE (field) == error_mark_node)
3331                     continue;
3332
3333                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3334                                            TREE_TYPE (field), subclasses,
3335                                            bit_offset);
3336                   if (!num)
3337                     return 0;
3338                   for (i = 0; i < num; i++)
3339                     classes[i] = merge_classes (subclasses[i], classes[i]);
3340                 }
3341             }
3342           break;
3343
3344         default:
3345           gcc_unreachable ();
3346         }
3347
3348       /* Final merger cleanup.  */
3349       for (i = 0; i < words; i++)
3350         {
3351           /* If one class is MEMORY, everything should be passed in
3352              memory.  */
3353           if (classes[i] == X86_64_MEMORY_CLASS)
3354             return 0;
3355
3356           /* The X86_64_SSEUP_CLASS should be always preceded by
3357              X86_64_SSE_CLASS.  */
3358           if (classes[i] == X86_64_SSEUP_CLASS
3359               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3360             classes[i] = X86_64_SSE_CLASS;
3361
3362           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3363           if (classes[i] == X86_64_X87UP_CLASS
3364               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3365             classes[i] = X86_64_SSE_CLASS;
3366         }
3367       return words;
3368     }
3369
3370   /* Compute alignment needed.  We align all types to natural boundaries with
3371      exception of XFmode that is aligned to 64bits.  */
3372   if (mode != VOIDmode && mode != BLKmode)
3373     {
3374       int mode_alignment = GET_MODE_BITSIZE (mode);
3375
3376       if (mode == XFmode)
3377         mode_alignment = 128;
3378       else if (mode == XCmode)
3379         mode_alignment = 256;
3380       if (COMPLEX_MODE_P (mode))
3381         mode_alignment /= 2;
3382       /* Misaligned fields are always returned in memory.  */
3383       if (bit_offset % mode_alignment)
3384         return 0;
3385     }
3386
3387   /* for V1xx modes, just use the base mode */
3388   if (VECTOR_MODE_P (mode)
3389       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3390     mode = GET_MODE_INNER (mode);
3391
3392   /* Classification of atomic types.  */
3393   switch (mode)
3394     {
3395     case SDmode:
3396     case DDmode:
3397       classes[0] = X86_64_SSE_CLASS;
3398       return 1;
3399     case TDmode:
3400       classes[0] = X86_64_SSE_CLASS;
3401       classes[1] = X86_64_SSEUP_CLASS;
3402       return 2;
3403     case DImode:
3404     case SImode:
3405     case HImode:
3406     case QImode:
3407     case CSImode:
3408     case CHImode:
3409     case CQImode:
3410       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3411         classes[0] = X86_64_INTEGERSI_CLASS;
3412       else
3413         classes[0] = X86_64_INTEGER_CLASS;
3414       return 1;
3415     case CDImode:
3416     case TImode:
3417       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3418       return 2;
3419     case CTImode:
3420       return 0;
3421     case SFmode:
3422       if (!(bit_offset % 64))
3423         classes[0] = X86_64_SSESF_CLASS;
3424       else
3425         classes[0] = X86_64_SSE_CLASS;
3426       return 1;
3427     case DFmode:
3428       classes[0] = X86_64_SSEDF_CLASS;
3429       return 1;
3430     case XFmode:
3431       classes[0] = X86_64_X87_CLASS;
3432       classes[1] = X86_64_X87UP_CLASS;
3433       return 2;
3434     case TFmode:
3435       classes[0] = X86_64_SSE_CLASS;
3436       classes[1] = X86_64_SSEUP_CLASS;
3437       return 2;
3438     case SCmode:
3439       classes[0] = X86_64_SSE_CLASS;
3440       return 1;
3441     case DCmode:
3442       classes[0] = X86_64_SSEDF_CLASS;
3443       classes[1] = X86_64_SSEDF_CLASS;
3444       return 2;
3445     case XCmode:
3446       classes[0] = X86_64_COMPLEX_X87_CLASS;
3447       return 1;
3448     case TCmode:
3449       /* This modes is larger than 16 bytes.  */
3450       return 0;
3451     case V4SFmode:
3452     case V4SImode:
3453     case V16QImode:
3454     case V8HImode:
3455     case V2DFmode:
3456     case V2DImode:
3457       classes[0] = X86_64_SSE_CLASS;
3458       classes[1] = X86_64_SSEUP_CLASS;
3459       return 2;
3460     case V2SFmode:
3461     case V2SImode:
3462     case V4HImode:
3463     case V8QImode:
3464       classes[0] = X86_64_SSE_CLASS;
3465       return 1;
3466     case BLKmode:
3467     case VOIDmode:
3468       return 0;
3469     default:
3470       gcc_assert (VECTOR_MODE_P (mode));
3471
3472       if (bytes > 16)
3473         return 0;
3474
3475       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3476
3477       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3478         classes[0] = X86_64_INTEGERSI_CLASS;
3479       else
3480         classes[0] = X86_64_INTEGER_CLASS;
3481       classes[1] = X86_64_INTEGER_CLASS;
3482       return 1 + (bytes > 8);
3483     }
3484 }
3485
3486 /* Examine the argument and return set number of register required in each
3487    class.  Return 0 iff parameter should be passed in memory.  */
3488 static int
3489 examine_argument (enum machine_mode mode, tree type, int in_return,
3490                   int *int_nregs, int *sse_nregs)
3491 {
3492   enum x86_64_reg_class class[MAX_CLASSES];
3493   int n = classify_argument (mode, type, class, 0);
3494
3495   *int_nregs = 0;
3496   *sse_nregs = 0;
3497   if (!n)
3498     return 0;
3499   for (n--; n >= 0; n--)
3500     switch (class[n])
3501       {
3502       case X86_64_INTEGER_CLASS:
3503       case X86_64_INTEGERSI_CLASS:
3504         (*int_nregs)++;
3505         break;
3506       case X86_64_SSE_CLASS:
3507       case X86_64_SSESF_CLASS:
3508       case X86_64_SSEDF_CLASS:
3509         (*sse_nregs)++;
3510         break;
3511       case X86_64_NO_CLASS:
3512       case X86_64_SSEUP_CLASS:
3513         break;
3514       case X86_64_X87_CLASS:
3515       case X86_64_X87UP_CLASS:
3516         if (!in_return)
3517           return 0;
3518         break;
3519       case X86_64_COMPLEX_X87_CLASS:
3520         return in_return ? 2 : 0;
3521       case X86_64_MEMORY_CLASS:
3522         gcc_unreachable ();
3523       }
3524   return 1;
3525 }
3526
3527 /* Construct container for the argument used by GCC interface.  See
3528    FUNCTION_ARG for the detailed description.  */
3529
3530 static rtx
3531 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3532                      tree type, int in_return, int nintregs, int nsseregs,
3533                      const int *intreg, int sse_regno)
3534 {
3535   /* The following variables hold the static issued_error state.  */
3536   static bool issued_sse_arg_error;
3537   static bool issued_sse_ret_error;
3538   static bool issued_x87_ret_error;
3539
3540   enum machine_mode tmpmode;
3541   int bytes =
3542     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3543   enum x86_64_reg_class class[MAX_CLASSES];
3544   int n;
3545   int i;
3546   int nexps = 0;
3547   int needed_sseregs, needed_intregs;
3548   rtx exp[MAX_CLASSES];
3549   rtx ret;
3550
3551   n = classify_argument (mode, type, class, 0);
3552   if (TARGET_DEBUG_ARG)
3553     {
3554       if (!n)
3555         fprintf (stderr, "Memory class\n");
3556       else
3557         {
3558           fprintf (stderr, "Classes:");
3559           for (i = 0; i < n; i++)
3560             {
3561               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
3562             }
3563            fprintf (stderr, "\n");
3564         }
3565     }
3566   if (!n)
3567     return NULL;
3568   if (!examine_argument (mode, type, in_return, &needed_intregs,
3569                          &needed_sseregs))
3570     return NULL;
3571   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3572     return NULL;
3573
3574   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3575      some less clueful developer tries to use floating-point anyway.  */
3576   if (needed_sseregs && !TARGET_SSE)
3577     {
3578       if (in_return)
3579         {
3580           if (!issued_sse_ret_error)
3581             {
3582               error ("SSE register return with SSE disabled");
3583               issued_sse_ret_error = true;
3584             }
3585         }
3586       else if (!issued_sse_arg_error)
3587         {
3588           error ("SSE register argument with SSE disabled");
3589           issued_sse_arg_error = true;
3590         }
3591       return NULL;
3592     }
3593
3594   /* Likewise, error if the ABI requires us to return values in the
3595      x87 registers and the user specified -mno-80387.  */
3596   if (!TARGET_80387 && in_return)
3597     for (i = 0; i < n; i++)
3598       if (class[i] == X86_64_X87_CLASS
3599           || class[i] == X86_64_X87UP_CLASS
3600           || class[i] == X86_64_COMPLEX_X87_CLASS)
3601         {
3602           if (!issued_x87_ret_error)
3603             {
3604               error ("x87 register return with x87 disabled");
3605               issued_x87_ret_error = true;
3606             }
3607           return NULL;
3608         }
3609
3610   /* First construct simple cases.  Avoid SCmode, since we want to use
3611      single register to pass this type.  */
3612   if (n == 1 && mode != SCmode)
3613     switch (class[0])
3614       {
3615       case X86_64_INTEGER_CLASS:
3616       case X86_64_INTEGERSI_CLASS:
3617         return gen_rtx_REG (mode, intreg[0]);
3618       case X86_64_SSE_CLASS:
3619       case X86_64_SSESF_CLASS:
3620       case X86_64_SSEDF_CLASS:
3621         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3622       case X86_64_X87_CLASS:
3623       case X86_64_COMPLEX_X87_CLASS:
3624         return gen_rtx_REG (mode, FIRST_STACK_REG);
3625       case X86_64_NO_CLASS:
3626         /* Zero sized array, struct or class.  */
3627         return NULL;
3628       default:
3629         gcc_unreachable ();
3630       }
3631   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3632       && mode != BLKmode)
3633     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3634   if (n == 2
3635       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3636     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3637   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3638       && class[1] == X86_64_INTEGER_CLASS
3639       && (mode == CDImode || mode == TImode || mode == TFmode)
3640       && intreg[0] + 1 == intreg[1])
3641     return gen_rtx_REG (mode, intreg[0]);
3642
3643   /* Otherwise figure out the entries of the PARALLEL.  */
3644   for (i = 0; i < n; i++)
3645     {
3646       switch (class[i])
3647         {
3648           case X86_64_NO_CLASS:
3649             break;
3650           case X86_64_INTEGER_CLASS:
3651           case X86_64_INTEGERSI_CLASS:
3652             /* Merge TImodes on aligned occasions here too.  */
3653             if (i * 8 + 8 > bytes)
3654               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3655             else if (class[i] == X86_64_INTEGERSI_CLASS)
3656               tmpmode = SImode;
3657             else
3658               tmpmode = DImode;
3659             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3660             if (tmpmode == BLKmode)
3661               tmpmode = DImode;
3662             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3663                                                gen_rtx_REG (tmpmode, *intreg),
3664                                                GEN_INT (i*8));
3665             intreg++;
3666             break;
3667           case X86_64_SSESF_CLASS:
3668             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3669                                                gen_rtx_REG (SFmode,
3670                                                             SSE_REGNO (sse_regno)),
3671                                                GEN_INT (i*8));
3672             sse_regno++;
3673             break;
3674           case X86_64_SSEDF_CLASS:
3675             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3676                                                gen_rtx_REG (DFmode,
3677                                                             SSE_REGNO (sse_regno)),
3678                                                GEN_INT (i*8));
3679             sse_regno++;
3680             break;
3681           case X86_64_SSE_CLASS:
3682             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3683               tmpmode = TImode;
3684             else
3685               tmpmode = DImode;
3686             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3687                                                gen_rtx_REG (tmpmode,
3688                                                             SSE_REGNO (sse_regno)),
3689                                                GEN_INT (i*8));
3690             if (tmpmode == TImode)
3691               i++;
3692             sse_regno++;
3693             break;
3694           default:
3695             gcc_unreachable ();
3696         }
3697     }
3698
3699   /* Empty aligned struct, union or class.  */
3700   if (nexps == 0)
3701     return NULL;
3702
3703   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3704   for (i = 0; i < nexps; i++)
3705     XVECEXP (ret, 0, i) = exp [i];
3706   return ret;
3707 }
3708
3709 /* Update the data in CUM to advance over an argument
3710    of mode MODE and data type TYPE.
3711    (TYPE is null for libcalls where that information may not be available.)  */
3712
3713 void
3714 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3715                       tree type, int named)
3716 {
3717   int bytes =
3718     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3719   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3720
3721   if (type)
3722     mode = type_natural_mode (type);
3723
3724   if (TARGET_DEBUG_ARG)
3725     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
3726              "mode=%s, named=%d)\n\n",
3727              words, cum->words, cum->nregs, cum->sse_nregs,
3728              GET_MODE_NAME (mode), named);
3729
3730   if (TARGET_64BIT)
3731     {
3732       int int_nregs, sse_nregs;
3733       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3734         cum->words += words;
3735       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3736         {
3737           cum->nregs -= int_nregs;
3738           cum->sse_nregs -= sse_nregs;
3739           cum->regno += int_nregs;
3740           cum->sse_regno += sse_nregs;
3741         }
3742       else
3743         cum->words += words;
3744     }
3745   else
3746     {
3747       switch (mode)
3748         {
3749         default:
3750           break;
3751
3752         case BLKmode:
3753           if (bytes < 0)
3754             break;
3755           /* FALLTHRU */
3756
3757         case DImode:
3758         case SImode:
3759         case HImode:
3760         case QImode:
3761           cum->words += words;
3762           cum->nregs -= words;
3763           cum->regno += words;
3764
3765           if (cum->nregs <= 0)
3766             {
3767               cum->nregs = 0;
3768               cum->regno = 0;
3769             }
3770           break;
3771
3772         case SFmode:
3773           if (cum->float_in_sse > 0)
3774             goto skip_80387;
3775
3776         case DFmode:
3777           if (cum->float_in_sse > 1)
3778             goto skip_80387;
3779
3780           /* Because no inherent XFmode->DFmode and XFmode->SFmode
3781              rounding takes place when values are passed in x87
3782              registers, pass DFmode and SFmode types to local functions
3783              only when flag_unsafe_math_optimizations is set.  */
3784           if (!cum->float_in_x87
3785               || (cum->float_in_x87 == 2
3786                   && !flag_unsafe_math_optimizations))
3787             break;
3788
3789         case XFmode:
3790           if (!cum->float_in_x87)
3791             break;
3792
3793           if (!type || !AGGREGATE_TYPE_P (type))
3794             {
3795               cum->x87_nregs -= 1;
3796               cum->x87_regno += 1;
3797               if (cum->x87_nregs <= 0)
3798                 {
3799                   cum->x87_nregs = 0;
3800                   cum->x87_regno = 0;
3801                 }
3802             }
3803           break;
3804
3805  skip_80387:
3806
3807         case TImode:
3808         case V16QImode:
3809         case V8HImode:
3810         case V4SImode:
3811         case V2DImode:
3812         case V4SFmode:
3813         case V2DFmode:
3814           if (!type || !AGGREGATE_TYPE_P (type))
3815             {
3816               cum->sse_nregs -= 1;
3817               cum->sse_regno += 1;
3818               if (cum->sse_nregs <= 0)
3819                 {
3820                   cum->sse_nregs = 0;
3821                   cum->sse_regno = 0;
3822                 }
3823             }
3824           break;
3825
3826         case V8QImode:
3827         case V4HImode:
3828         case V2SImode:
3829         case V2SFmode:
3830           if (!type || !AGGREGATE_TYPE_P (type))
3831             {
3832               cum->mmx_nregs -= 1;
3833               cum->mmx_regno += 1;
3834               if (cum->mmx_nregs <= 0)
3835                 {
3836                   cum->mmx_nregs = 0;
3837                   cum->mmx_regno = 0;
3838                 }
3839             }
3840           break;
3841         }
3842     }
3843 }
3844
3845 /* Define where to put the arguments to a function.
3846    Value is zero to push the argument on the stack,
3847    or a hard register in which to store the argument.
3848
3849    MODE is the argument's machine mode.
3850    TYPE is the data type of the argument (as a tree).
3851     This is null for libcalls where that information may
3852     not be available.
3853    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3854     the preceding args and about the function being called.
3855    NAMED is nonzero if this argument is a named parameter
3856     (otherwise it is an extra parameter matching an ellipsis).  */
3857
3858 rtx
3859 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
3860               tree type, int named)
3861 {
3862   enum machine_mode mode = orig_mode;
3863   rtx ret = NULL_RTX;
3864   int bytes =
3865     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3866   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3867   static bool warnedsse, warnedmmx;
3868
3869   /* To simplify the code below, represent vector types with a vector mode
3870      even if MMX/SSE are not active.  */
3871   if (type && TREE_CODE (type) == VECTOR_TYPE)
3872     mode = type_natural_mode (type);
3873
3874   /* Handle a hidden AL argument containing number of registers for varargs
3875      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
3876      any AL settings.  */
3877   if (mode == VOIDmode)
3878     {
3879       if (TARGET_64BIT)
3880         return GEN_INT (cum->maybe_vaarg
3881                         ? (cum->sse_nregs < 0
3882                            ? SSE_REGPARM_MAX
3883                            : cum->sse_regno)
3884                         : -1);
3885       else
3886         return constm1_rtx;
3887     }
3888   if (TARGET_64BIT)
3889     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
3890                                cum->sse_nregs,
3891                                &x86_64_int_parameter_registers [cum->regno],
3892                                cum->sse_regno);
3893   else
3894     switch (mode)
3895       {
3896       default:
3897         break;
3898
3899       case BLKmode:
3900         if (bytes < 0)
3901           break;
3902         /* FALLTHRU */
3903       case DImode:
3904       case SImode:
3905       case HImode:
3906       case QImode:
3907         if (words <= cum->nregs)
3908           {
3909             int regno = cum->regno;
3910
3911             /* Fastcall allocates the first two DWORD (SImode) or
3912                smaller arguments to ECX and EDX.  */
3913             if (cum->fastcall)
3914               {
3915                 if (mode == BLKmode || mode == DImode)
3916                   break;
3917
3918                 /* ECX not EAX is the first allocated register.  */
3919                 if (regno == 0)
3920                   regno = 2;
3921               }
3922             ret = gen_rtx_REG (mode, regno);
3923           }
3924         break;
3925
3926         case SFmode:
3927           if (cum->float_in_sse > 0)
3928             goto skip_80387;
3929
3930         case DFmode:
3931           if (cum->float_in_sse > 1)
3932             goto skip_80387;
3933
3934           /* Because no inherent XFmode->DFmode and XFmode->SFmode
3935              rounding takes place when values are passed in x87
3936              registers, pass DFmode and SFmode types to local functions
3937              only when flag_unsafe_math_optimizations is set.  */
3938           if (!cum->float_in_x87
3939               || (cum->float_in_x87 == 2
3940                   && !flag_unsafe_math_optimizations))
3941             break;
3942
3943         case XFmode:
3944           if (!cum->float_in_x87)
3945             break;
3946
3947           if (!type || !AGGREGATE_TYPE_P (type))
3948             if (cum->x87_nregs)
3949               ret = gen_rtx_REG (mode, cum->x87_regno + FIRST_FLOAT_REG);
3950           break;
3951
3952  skip_80387:
3953
3954       case TImode:
3955       case V16QImode:
3956       case V8HImode:
3957       case V4SImode:
3958       case V2DImode:
3959       case V4SFmode:
3960       case V2DFmode:
3961         if (!type || !AGGREGATE_TYPE_P (type))
3962           {
3963             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3964               {
3965                 warnedsse = true;
3966                 warning (0, "SSE vector argument without SSE enabled "
3967                          "changes the ABI");
3968               }
3969             if (cum->sse_nregs)
3970               ret = gen_reg_or_parallel (mode, orig_mode,
3971                                          cum->sse_regno + FIRST_SSE_REG);
3972           }
3973         break;
3974       case V8QImode:
3975       case V4HImode:
3976       case V2SImode:
3977       case V2SFmode:
3978         if (!type || !AGGREGATE_TYPE_P (type))
3979           {
3980             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3981               {
3982                 warnedmmx = true;
3983                 warning (0, "MMX vector argument without MMX enabled "
3984                          "changes the ABI");
3985               }
3986             if (cum->mmx_nregs)
3987               ret = gen_reg_or_parallel (mode, orig_mode,
3988                                          cum->mmx_regno + FIRST_MMX_REG);
3989           }
3990         break;
3991       }
3992
3993   if (TARGET_DEBUG_ARG)
3994     {
3995       fprintf (stderr,
3996                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3997                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3998
3999       if (ret)
4000         print_simple_rtl (stderr, ret);
4001       else
4002         fprintf (stderr, ", stack");
4003
4004       fprintf (stderr, " )\n");
4005     }
4006
4007   return ret;
4008 }
4009
4010 /* A C expression that indicates when an argument must be passed by
4011    reference.  If nonzero for an argument, a copy of that argument is
4012    made in memory and a pointer to the argument is passed instead of
4013    the argument itself.  The pointer is passed in whatever way is
4014    appropriate for passing a pointer to that type.  */
4015
4016 static bool
4017 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
4018                         enum machine_mode mode ATTRIBUTE_UNUSED,
4019                         tree type, bool named ATTRIBUTE_UNUSED)
4020 {
4021   if (!TARGET_64BIT)
4022     return 0;
4023
4024   if (type && int_size_in_bytes (type) == -1)
4025     {
4026       if (TARGET_DEBUG_ARG)
4027         fprintf (stderr, "function_arg_pass_by_reference\n");
4028       return 1;
4029     }
4030
4031   return 0;
4032 }
4033
4034 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
4035    ABI.  Only called if TARGET_SSE.  */
4036 static bool
4037 contains_128bit_aligned_vector_p (tree type)
4038 {
4039   enum machine_mode mode = TYPE_MODE (type);
4040   if (SSE_REG_MODE_P (mode)
4041       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
4042     return true;
4043   if (TYPE_ALIGN (type) < 128)
4044     return false;
4045
4046   if (AGGREGATE_TYPE_P (type))
4047     {
4048       /* Walk the aggregates recursively.  */
4049       switch (TREE_CODE (type))
4050         {
4051         case RECORD_TYPE:
4052         case UNION_TYPE:
4053         case QUAL_UNION_TYPE:
4054           {
4055             tree field;
4056
4057             /* Walk all the structure fields.  */
4058             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4059               {
4060                 if (TREE_CODE (field) == FIELD_DECL
4061                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
4062                   return true;
4063               }
4064             break;
4065           }
4066
4067         case ARRAY_TYPE:
4068           /* Just for use if some languages passes arrays by value.  */
4069           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
4070             return true;
4071           break;
4072
4073         default:
4074           gcc_unreachable ();
4075         }
4076     }
4077   return false;
4078 }
4079
4080 /* Gives the alignment boundary, in bits, of an argument with the
4081    specified mode and type.  */
4082
4083 int
4084 ix86_function_arg_boundary (enum machine_mode mode, tree type)
4085 {
4086   int align;
4087   if (type)
4088     align = TYPE_ALIGN (type);
4089   else
4090     align = GET_MODE_ALIGNMENT (mode);
4091   if (align < PARM_BOUNDARY)
4092     align = PARM_BOUNDARY;
4093   if (!TARGET_64BIT)
4094     {
4095       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
4096          make an exception for SSE modes since these require 128bit
4097          alignment.
4098
4099          The handling here differs from field_alignment.  ICC aligns MMX
4100          arguments to 4 byte boundaries, while structure fields are aligned
4101          to 8 byte boundaries.  */
4102       if (!TARGET_SSE)
4103         align = PARM_BOUNDARY;
4104       else if (!type)
4105         {
4106           if (!SSE_REG_MODE_P (mode))
4107             align = PARM_BOUNDARY;
4108         }
4109       else
4110         {
4111           if (!contains_128bit_aligned_vector_p (type))
4112             align = PARM_BOUNDARY;
4113         }
4114     }
4115   if (align > 128)
4116     align = 128;
4117   return align;
4118 }
4119
4120 /* Return true if N is a possible register number of function value.  */
4121 bool
4122 ix86_function_value_regno_p (int regno)
4123 {
4124   if (regno == 0
4125       || (regno == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
4126       || (regno == FIRST_SSE_REG && TARGET_SSE))
4127     return true;
4128
4129   if (!TARGET_64BIT
4130       && (regno == FIRST_MMX_REG && TARGET_MMX))
4131         return true;
4132
4133   return false;
4134 }
4135
4136 /* Define how to find the value returned by a function.
4137    VALTYPE is the data type of the value (as a tree).
4138    If the precise function being called is known, FUNC is its FUNCTION_DECL;
4139    otherwise, FUNC is 0.  */
4140 rtx
4141 ix86_function_value (tree valtype, tree fntype_or_decl,
4142                      bool outgoing ATTRIBUTE_UNUSED)
4143 {
4144   enum machine_mode natmode = type_natural_mode (valtype);
4145
4146   if (TARGET_64BIT)
4147     {
4148       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
4149                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
4150                                      x86_64_int_return_registers, 0);
4151       /* For zero sized structures, construct_container return NULL, but we
4152          need to keep rest of compiler happy by returning meaningful value.  */
4153       if (!ret)
4154         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
4155       return ret;
4156     }
4157   else
4158     {
4159       tree fn = NULL_TREE, fntype;
4160       if (fntype_or_decl
4161           && DECL_P (fntype_or_decl))
4162         fn = fntype_or_decl;
4163       fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
4164       return gen_rtx_REG (TYPE_MODE (valtype),
4165                           ix86_value_regno (natmode, fn, fntype));
4166     }
4167 }
4168
4169 /* Return true iff type is returned in memory.  */
4170 int
4171 ix86_return_in_memory (tree type)
4172 {
4173   int needed_intregs, needed_sseregs, size;
4174   enum machine_mode mode = type_natural_mode (type);
4175
4176   if (TARGET_64BIT)
4177     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
4178
4179   if (mode == BLKmode)
4180     return 1;
4181
4182   size = int_size_in_bytes (type);
4183
4184   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
4185     return 0;
4186
4187   if (VECTOR_MODE_P (mode) || mode == TImode)
4188     {
4189       /* User-created vectors small enough to fit in EAX.  */
4190       if (size < 8)
4191         return 0;
4192
4193       /* MMX/3dNow values are returned in MM0,
4194          except when it doesn't exits.  */
4195       if (size == 8)
4196         return (TARGET_MMX ? 0 : 1);
4197
4198       /* SSE values are returned in XMM0, except when it doesn't exist.  */
4199       if (size == 16)
4200         return (TARGET_SSE ? 0 : 1);
4201     }
4202
4203   if (mode == XFmode)
4204     return 0;
4205
4206   if (mode == TDmode)
4207     return 1;
4208
4209   if (size > 12)
4210     return 1;
4211   return 0;
4212 }
4213
4214 /* When returning SSE vector types, we have a choice of either
4215      (1) being abi incompatible with a -march switch, or
4216      (2) generating an error.
4217    Given no good solution, I think the safest thing is one warning.
4218    The user won't be able to use -Werror, but....
4219
4220    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
4221    called in response to actually generating a caller or callee that
4222    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
4223    via aggregate_value_p for general type probing from tree-ssa.  */
4224
4225 static rtx
4226 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
4227 {
4228   static bool warnedsse, warnedmmx;
4229
4230   if (type)
4231     {
4232       /* Look at the return type of the function, not the function type.  */
4233       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
4234
4235       if (!TARGET_SSE && !warnedsse)
4236         {
4237           if (mode == TImode
4238               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4239             {
4240               warnedsse = true;
4241               warning (0, "SSE vector return without SSE enabled "
4242                        "changes the ABI");
4243             }
4244         }
4245
4246       if (!TARGET_MMX && !warnedmmx)
4247         {
4248           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4249             {
4250               warnedmmx = true;
4251               warning (0, "MMX vector return without MMX enabled "
4252                        "changes the ABI");
4253             }
4254         }
4255     }
4256
4257   return NULL;
4258 }
4259
4260 /* Define how to find the value returned by a library function
4261    assuming the value has mode MODE.  */
4262 rtx
4263 ix86_libcall_value (enum machine_mode mode)
4264 {
4265   if (TARGET_64BIT)
4266     {
4267       switch (mode)
4268         {
4269         case SFmode:
4270         case SCmode:
4271         case DFmode:
4272         case DCmode:
4273         case TFmode:
4274         case SDmode:
4275         case DDmode:
4276         case TDmode:
4277           return gen_rtx_REG (mode, FIRST_SSE_REG);
4278         case XFmode:
4279         case XCmode:
4280           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4281         case TCmode:
4282           return NULL;
4283         default:
4284           return gen_rtx_REG (mode, 0);
4285         }
4286     }
4287   else
4288     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL, NULL));
4289 }
4290
4291 /* Given a mode, return the register to use for a return value.  */
4292
4293 static int
4294 ix86_value_regno (enum machine_mode mode, tree func, tree fntype)
4295 {
4296   gcc_assert (!TARGET_64BIT);
4297
4298   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4299      we normally prevent this case when mmx is not available.  However
4300      some ABIs may require the result to be returned like DImode.  */
4301   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4302     return TARGET_MMX ? FIRST_MMX_REG : 0;
4303
4304   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4305      we prevent this case when sse is not available.  However some ABIs
4306      may require the result to be returned like integer TImode.  */
4307   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4308     return TARGET_SSE ? FIRST_SSE_REG : 0;
4309
4310   /* Decimal floating point values can go in %eax, unlike other float modes.  */
4311   if (DECIMAL_FLOAT_MODE_P (mode))
4312     return 0;
4313
4314   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
4315   if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
4316     return 0;
4317
4318   /* Floating point return values in %st(0), except for local functions when
4319      SSE math is enabled or for functions with sseregparm attribute.  */
4320   if ((func || fntype)
4321       && (mode == SFmode || mode == DFmode))
4322     {
4323       int sse_level = ix86_function_sseregparm (fntype, func);
4324       if ((sse_level >= 1 && mode == SFmode)
4325           || (sse_level == 2 && mode == DFmode))
4326         return FIRST_SSE_REG;
4327     }
4328
4329   return FIRST_FLOAT_REG;
4330 }
4331 \f
4332 /* Create the va_list data type.  */
4333
4334 static tree
4335 ix86_build_builtin_va_list (void)
4336 {
4337   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4338
4339   /* For i386 we use plain pointer to argument area.  */
4340   if (!TARGET_64BIT)
4341     return build_pointer_type (char_type_node);
4342
4343   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4344   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4345
4346   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4347                       unsigned_type_node);
4348   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4349                       unsigned_type_node);
4350   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4351                       ptr_type_node);
4352   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4353                       ptr_type_node);
4354
4355   va_list_gpr_counter_field = f_gpr;
4356   va_list_fpr_counter_field = f_fpr;
4357
4358   DECL_FIELD_CONTEXT (f_gpr) = record;
4359   DECL_FIELD_CONTEXT (f_fpr) = record;
4360   DECL_FIELD_CONTEXT (f_ovf) = record;
4361   DECL_FIELD_CONTEXT (f_sav) = record;
4362
4363   TREE_CHAIN (record) = type_decl;
4364   TYPE_NAME (record) = type_decl;
4365   TYPE_FIELDS (record) = f_gpr;
4366   TREE_CHAIN (f_gpr) = f_fpr;
4367   TREE_CHAIN (f_fpr) = f_ovf;
4368   TREE_CHAIN (f_ovf) = f_sav;
4369
4370   layout_type (record);
4371
4372   /* The correct type is an array type of one element.  */
4373   return build_array_type (record, build_index_type (size_zero_node));
4374 }
4375
4376 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4377
4378 static void
4379 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4380                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4381                              int no_rtl)
4382 {
4383   CUMULATIVE_ARGS next_cum;
4384   rtx save_area = NULL_RTX, mem;
4385   rtx label;
4386   rtx label_ref;
4387   rtx tmp_reg;
4388   rtx nsse_reg;
4389   int set;
4390   tree fntype;
4391   int stdarg_p;
4392   int i;
4393
4394   if (!TARGET_64BIT)
4395     return;
4396
4397   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4398     return;
4399
4400   /* Indicate to allocate space on the stack for varargs save area.  */
4401   ix86_save_varrargs_registers = 1;
4402
4403   cfun->stack_alignment_needed = 128;
4404
4405   fntype = TREE_TYPE (current_function_decl);
4406   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4407               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4408                   != void_type_node));
4409
4410   /* For varargs, we do not want to skip the dummy va_dcl argument.
4411      For stdargs, we do want to skip the last named argument.  */
4412   next_cum = *cum;
4413   if (stdarg_p)
4414     function_arg_advance (&next_cum, mode, type, 1);
4415
4416   if (!no_rtl)
4417     save_area = frame_pointer_rtx;
4418
4419   set = get_varargs_alias_set ();
4420
4421   for (i = next_cum.regno;
4422        i < ix86_regparm
4423        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4424        i++)
4425     {
4426       mem = gen_rtx_MEM (Pmode,
4427                          plus_constant (save_area, i * UNITS_PER_WORD));
4428       MEM_NOTRAP_P (mem) = 1;
4429       set_mem_alias_set (mem, set);
4430       emit_move_insn (mem, gen_rtx_REG (Pmode,
4431                                         x86_64_int_parameter_registers[i]));
4432     }
4433
4434   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
4435     {
4436       /* Now emit code to save SSE registers.  The AX parameter contains number
4437          of SSE parameter registers used to call this function.  We use
4438          sse_prologue_save insn template that produces computed jump across
4439          SSE saves.  We need some preparation work to get this working.  */
4440
4441       label = gen_label_rtx ();
4442       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4443
4444       /* Compute address to jump to :
4445          label - 5*eax + nnamed_sse_arguments*5  */
4446       tmp_reg = gen_reg_rtx (Pmode);
4447       nsse_reg = gen_reg_rtx (Pmode);
4448       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4449       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4450                               gen_rtx_MULT (Pmode, nsse_reg,
4451                                             GEN_INT (4))));
4452       if (next_cum.sse_regno)
4453         emit_move_insn
4454           (nsse_reg,
4455            gen_rtx_CONST (DImode,
4456                           gen_rtx_PLUS (DImode,
4457                                         label_ref,
4458                                         GEN_INT (next_cum.sse_regno * 4))));
4459       else
4460         emit_move_insn (nsse_reg, label_ref);
4461       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4462
4463       /* Compute address of memory block we save into.  We always use pointer
4464          pointing 127 bytes after first byte to store - this is needed to keep
4465          instruction size limited by 4 bytes.  */
4466       tmp_reg = gen_reg_rtx (Pmode);
4467       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4468                               plus_constant (save_area,
4469                                              8 * REGPARM_MAX + 127)));
4470       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4471       MEM_NOTRAP_P (mem) = 1;
4472       set_mem_alias_set (mem, set);
4473       set_mem_align (mem, BITS_PER_WORD);
4474
4475       /* And finally do the dirty job!  */
4476       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4477                                         GEN_INT (next_cum.sse_regno), label));
4478     }
4479
4480 }
4481
4482 /* Implement va_start.  */
4483
4484 void
4485 ix86_va_start (tree valist, rtx nextarg)
4486 {
4487   HOST_WIDE_INT words, n_gpr, n_fpr;
4488   tree f_gpr, f_fpr, f_ovf, f_sav;
4489   tree gpr, fpr, ovf, sav, t;
4490   tree type;
4491
4492   /* Only 64bit target needs something special.  */
4493   if (!TARGET_64BIT)
4494     {
4495       std_expand_builtin_va_start (valist, nextarg);
4496       return;
4497     }
4498
4499   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4500   f_fpr = TREE_CHAIN (f_gpr);
4501   f_ovf = TREE_CHAIN (f_fpr);
4502   f_sav = TREE_CHAIN (f_ovf);
4503
4504   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4505   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4506   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4507   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4508   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4509
4510   /* Count number of gp and fp argument registers used.  */
4511   words = current_function_args_info.words;
4512   n_gpr = current_function_args_info.regno;
4513   n_fpr = current_function_args_info.sse_regno;
4514
4515   if (TARGET_DEBUG_ARG)
4516     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
4517              (int) words, (int) n_gpr, (int) n_fpr);
4518
4519   if (cfun->va_list_gpr_size)
4520     {
4521       type = TREE_TYPE (gpr);
4522       t = build2 (MODIFY_EXPR, type, gpr,
4523                   build_int_cst (type, n_gpr * 8));
4524       TREE_SIDE_EFFECTS (t) = 1;
4525       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4526     }
4527
4528   if (cfun->va_list_fpr_size)
4529     {
4530       type = TREE_TYPE (fpr);
4531       t = build2 (MODIFY_EXPR, type, fpr,
4532                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4533       TREE_SIDE_EFFECTS (t) = 1;
4534       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4535     }
4536
4537   /* Find the overflow area.  */
4538   type = TREE_TYPE (ovf);
4539   t = make_tree (type, virtual_incoming_args_rtx);
4540   if (words != 0)
4541     t = build2 (PLUS_EXPR, type, t,
4542                 build_int_cst (type, words * UNITS_PER_WORD));
4543   t = build2 (MODIFY_EXPR, type, ovf, t);
4544   TREE_SIDE_EFFECTS (t) = 1;
4545   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4546
4547   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4548     {
4549       /* Find the register save area.
4550          Prologue of the function save it right above stack frame.  */
4551       type = TREE_TYPE (sav);
4552       t = make_tree (type, frame_pointer_rtx);
4553       t = build2 (MODIFY_EXPR, type, sav, t);
4554       TREE_SIDE_EFFECTS (t) = 1;
4555       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4556     }
4557 }
4558
4559 /* Implement va_arg.  */
4560
4561 tree
4562 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4563 {
4564   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4565   tree f_gpr, f_fpr, f_ovf, f_sav;
4566   tree gpr, fpr, ovf, sav, t;
4567   int size, rsize;
4568   tree lab_false, lab_over = NULL_TREE;
4569   tree addr, t2;
4570   rtx container;
4571   int indirect_p = 0;
4572   tree ptrtype;
4573   enum machine_mode nat_mode;
4574
4575   /* Only 64bit target needs something special.  */
4576   if (!TARGET_64BIT)
4577     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4578
4579   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4580   f_fpr = TREE_CHAIN (f_gpr);
4581   f_ovf = TREE_CHAIN (f_fpr);
4582   f_sav = TREE_CHAIN (f_ovf);
4583
4584   valist = build_va_arg_indirect_ref (valist);
4585   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4586   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4587   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4588   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4589
4590   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4591   if (indirect_p)
4592     type = build_pointer_type (type);
4593   size = int_size_in_bytes (type);
4594   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4595
4596   nat_mode = type_natural_mode (type);
4597   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4598                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4599
4600   /* Pull the value out of the saved registers.  */
4601
4602   addr = create_tmp_var (ptr_type_node, "addr");
4603   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4604
4605   if (container)
4606     {
4607       int needed_intregs, needed_sseregs;
4608       bool need_temp;
4609       tree int_addr, sse_addr;
4610
4611       lab_false = create_artificial_label ();
4612       lab_over = create_artificial_label ();
4613
4614       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4615
4616       need_temp = (!REG_P (container)
4617                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4618                        || TYPE_ALIGN (type) > 128));
4619
4620       /* In case we are passing structure, verify that it is consecutive block
4621          on the register save area.  If not we need to do moves.  */
4622       if (!need_temp && !REG_P (container))
4623         {
4624           /* Verify that all registers are strictly consecutive  */
4625           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4626             {
4627               int i;
4628
4629               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4630                 {
4631                   rtx slot = XVECEXP (container, 0, i);
4632                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4633                       || INTVAL (XEXP (slot, 1)) != i * 16)
4634                     need_temp = 1;
4635                 }
4636             }
4637           else
4638             {
4639               int i;
4640
4641               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4642                 {
4643                   rtx slot = XVECEXP (container, 0, i);
4644                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4645                       || INTVAL (XEXP (slot, 1)) != i * 8)
4646                     need_temp = 1;
4647                 }
4648             }
4649         }
4650       if (!need_temp)
4651         {
4652           int_addr = addr;
4653           sse_addr = addr;
4654         }
4655       else
4656         {
4657           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4658           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4659           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4660           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4661         }
4662
4663       /* First ensure that we fit completely in registers.  */
4664       if (needed_intregs)
4665         {
4666           t = build_int_cst (TREE_TYPE (gpr),
4667                              (REGPARM_MAX - needed_intregs + 1) * 8);
4668           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4669           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4670           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4671           gimplify_and_add (t, pre_p);
4672         }
4673       if (needed_sseregs)
4674         {
4675           t = build_int_cst (TREE_TYPE (fpr),
4676                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4677                              + REGPARM_MAX * 8);
4678           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4679           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4680           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4681           gimplify_and_add (t, pre_p);
4682         }
4683
4684       /* Compute index to start of area used for integer regs.  */
4685       if (needed_intregs)
4686         {
4687           /* int_addr = gpr + sav; */
4688           t = fold_convert (ptr_type_node, gpr);
4689           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4690           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
4691           gimplify_and_add (t, pre_p);
4692         }
4693       if (needed_sseregs)
4694         {
4695           /* sse_addr = fpr + sav; */
4696           t = fold_convert (ptr_type_node, fpr);
4697           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4698           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
4699           gimplify_and_add (t, pre_p);
4700         }
4701       if (need_temp)
4702         {
4703           int i;
4704           tree temp = create_tmp_var (type, "va_arg_tmp");
4705
4706           /* addr = &temp; */
4707           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4708           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
4709           gimplify_and_add (t, pre_p);
4710
4711           for (i = 0; i < XVECLEN (container, 0); i++)
4712             {
4713               rtx slot = XVECEXP (container, 0, i);
4714               rtx reg = XEXP (slot, 0);
4715               enum machine_mode mode = GET_MODE (reg);
4716               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4717               tree addr_type = build_pointer_type (piece_type);
4718               tree src_addr, src;
4719               int src_offset;
4720               tree dest_addr, dest;
4721
4722               if (SSE_REGNO_P (REGNO (reg)))
4723                 {
4724                   src_addr = sse_addr;
4725                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4726                 }
4727               else
4728                 {
4729                   src_addr = int_addr;
4730                   src_offset = REGNO (reg) * 8;
4731                 }
4732               src_addr = fold_convert (addr_type, src_addr);
4733               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
4734                                        size_int (src_offset)));
4735               src = build_va_arg_indirect_ref (src_addr);
4736
4737               dest_addr = fold_convert (addr_type, addr);
4738               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
4739                                         size_int (INTVAL (XEXP (slot, 1)))));
4740               dest = build_va_arg_indirect_ref (dest_addr);
4741
4742               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
4743               gimplify_and_add (t, pre_p);
4744             }
4745         }
4746
4747       if (needed_intregs)
4748         {
4749           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4750                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4751           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4752           gimplify_and_add (t, pre_p);
4753         }
4754       if (needed_sseregs)
4755         {
4756           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4757                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4758           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
4759           gimplify_and_add (t, pre_p);
4760         }
4761
4762       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4763       gimplify_and_add (t, pre_p);
4764
4765       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4766       append_to_statement_list (t, pre_p);
4767     }
4768
4769   /* ... otherwise out of the overflow area.  */
4770
4771   /* Care for on-stack alignment if needed.  */
4772   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4773       || integer_zerop (TYPE_SIZE (type)))
4774     t = ovf;
4775   else
4776     {
4777       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4778       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4779                   build_int_cst (TREE_TYPE (ovf), align - 1));
4780       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4781                   build_int_cst (TREE_TYPE (t), -align));
4782     }
4783   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4784
4785   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
4786   gimplify_and_add (t2, pre_p);
4787
4788   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4789               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4790   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4791   gimplify_and_add (t, pre_p);
4792
4793   if (container)
4794     {
4795       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4796       append_to_statement_list (t, pre_p);
4797     }
4798
4799   ptrtype = build_pointer_type (type);
4800   addr = fold_convert (ptrtype, addr);
4801
4802   if (indirect_p)
4803     addr = build_va_arg_indirect_ref (addr);
4804   return build_va_arg_indirect_ref (addr);
4805 }
4806 \f
4807 /* Return nonzero if OPNUM's MEM should be matched
4808    in movabs* patterns.  */
4809
4810 int
4811 ix86_check_movabs (rtx insn, int opnum)
4812 {
4813   rtx set, mem;
4814
4815   set = PATTERN (insn);
4816   if (GET_CODE (set) == PARALLEL)
4817     set = XVECEXP (set, 0, 0);
4818   gcc_assert (GET_CODE (set) == SET);
4819   mem = XEXP (set, opnum);
4820   while (GET_CODE (mem) == SUBREG)
4821     mem = SUBREG_REG (mem);
4822   gcc_assert (GET_CODE (mem) == MEM);
4823   return (volatile_ok || !MEM_VOLATILE_P (mem));
4824 }
4825 \f
4826 /* Initialize the table of extra 80387 mathematical constants.  */
4827
4828 static void
4829 init_ext_80387_constants (void)
4830 {
4831   static const char * cst[5] =
4832   {
4833     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4834     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4835     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4836     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4837     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4838   };
4839   int i;
4840
4841   for (i = 0; i < 5; i++)
4842     {
4843       real_from_string (&ext_80387_constants_table[i], cst[i]);
4844       /* Ensure each constant is rounded to XFmode precision.  */
4845       real_convert (&ext_80387_constants_table[i],
4846                     XFmode, &ext_80387_constants_table[i]);
4847     }
4848
4849   ext_80387_constants_init = 1;
4850 }
4851
4852 /* Return true if the constant is something that can be loaded with
4853    a special instruction.  */
4854
4855 int
4856 standard_80387_constant_p (rtx x)
4857 {
4858   REAL_VALUE_TYPE r;
4859
4860   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4861     return -1;
4862
4863   if (x == CONST0_RTX (GET_MODE (x)))
4864     return 1;
4865   if (x == CONST1_RTX (GET_MODE (x)))
4866     return 2;
4867
4868   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4869
4870   /* For XFmode constants, try to find a special 80387 instruction when
4871      optimizing for size or on those CPUs that benefit from them.  */
4872   if (GET_MODE (x) == XFmode
4873       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
4874     {
4875       int i;
4876
4877       if (! ext_80387_constants_init)
4878         init_ext_80387_constants ();
4879
4880       for (i = 0; i < 5; i++)
4881         if (real_identical (&r, &ext_80387_constants_table[i]))
4882           return i + 3;
4883     }
4884
4885   /* Load of the constant -0.0 or -1.0 will be split as
4886      fldz;fchs or fld1;fchs sequence.  */
4887   if (real_isnegzero (&r))
4888     return 8;
4889   if (real_identical (&r, &dconstm1))
4890     return 9;
4891
4892   return 0;
4893 }
4894
4895 /* Return the opcode of the special instruction to be used to load
4896    the constant X.  */
4897
4898 const char *
4899 standard_80387_constant_opcode (rtx x)
4900 {
4901   switch (standard_80387_constant_p (x))
4902     {
4903     case 1:
4904       return "fldz";
4905     case 2:
4906       return "fld1";
4907     case 3:
4908       return "fldlg2";
4909     case 4:
4910       return "fldln2";
4911     case 5:
4912       return "fldl2e";
4913     case 6:
4914       return "fldl2t";
4915     case 7:
4916       return "fldpi";
4917     case 8:
4918     case 9:
4919       return "#";
4920     default:
4921       gcc_unreachable ();
4922     }
4923 }
4924
4925 /* Return the CONST_DOUBLE representing the 80387 constant that is
4926    loaded by the specified special instruction.  The argument IDX
4927    matches the return value from standard_80387_constant_p.  */
4928
4929 rtx
4930 standard_80387_constant_rtx (int idx)
4931 {
4932   int i;
4933
4934   if (! ext_80387_constants_init)
4935     init_ext_80387_constants ();
4936
4937   switch (idx)
4938     {
4939     case 3:
4940     case 4:
4941     case 5:
4942     case 6:
4943     case 7:
4944       i = idx - 3;
4945       break;
4946
4947     default:
4948       gcc_unreachable ();
4949     }
4950
4951   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4952                                        XFmode);
4953 }
4954
4955 /* Return 1 if mode is a valid mode for sse.  */
4956 static int
4957 standard_sse_mode_p (enum machine_mode mode)
4958 {
4959   switch (mode)
4960     {
4961     case V16QImode:
4962     case V8HImode:
4963     case V4SImode:
4964     case V2DImode:
4965     case V4SFmode:
4966     case V2DFmode:
4967       return 1;
4968
4969     default:
4970       return 0;
4971     }
4972 }
4973
4974 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4975  */
4976 int
4977 standard_sse_constant_p (rtx x)
4978 {
4979   enum machine_mode mode = GET_MODE (x);
4980
4981   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
4982     return 1;
4983   if (vector_all_ones_operand (x, mode)
4984       && standard_sse_mode_p (mode))
4985     return TARGET_SSE2 ? 2 : -1;
4986
4987   return 0;
4988 }
4989
4990 /* Return the opcode of the special instruction to be used to load
4991    the constant X.  */
4992
4993 const char *
4994 standard_sse_constant_opcode (rtx insn, rtx x)
4995 {
4996   switch (standard_sse_constant_p (x))
4997     {
4998     case 1:
4999       if (get_attr_mode (insn) == MODE_V4SF)
5000         return "xorps\t%0, %0";
5001       else if (get_attr_mode (insn) == MODE_V2DF)
5002         return "xorpd\t%0, %0";
5003       else
5004         return "pxor\t%0, %0";
5005     case 2:
5006       return "pcmpeqd\t%0, %0";
5007     }
5008   gcc_unreachable ();
5009 }
5010
5011 /* Returns 1 if OP contains a symbol reference */
5012
5013 int
5014 symbolic_reference_mentioned_p (rtx op)
5015 {
5016   const char *fmt;
5017   int i;
5018
5019   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
5020     return 1;
5021
5022   fmt = GET_RTX_FORMAT (GET_CODE (op));
5023   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5024     {
5025       if (fmt[i] == 'E')
5026         {
5027           int j;
5028
5029           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5030             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
5031               return 1;
5032         }
5033
5034       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
5035         return 1;
5036     }
5037
5038   return 0;
5039 }
5040
5041 /* Return 1 if it is appropriate to emit `ret' instructions in the
5042    body of a function.  Do this only if the epilogue is simple, needing a
5043    couple of insns.  Prior to reloading, we can't tell how many registers
5044    must be saved, so return 0 then.  Return 0 if there is no frame
5045    marker to de-allocate.  */
5046
5047 int
5048 ix86_can_use_return_insn_p (void)
5049 {
5050   struct ix86_frame frame;
5051
5052   if (! reload_completed || frame_pointer_needed)
5053     return 0;
5054
5055   /* Don't allow more than 32 pop, since that's all we can do
5056      with one instruction.  */
5057   if (current_function_pops_args
5058       && current_function_args_size >= 32768)
5059     return 0;
5060
5061   ix86_compute_frame_layout (&frame);
5062   return frame.to_allocate == 0 && frame.nregs == 0;
5063 }
5064 \f
5065 /* Value should be nonzero if functions must have frame pointers.
5066    Zero means the frame pointer need not be set up (and parms may
5067    be accessed via the stack pointer) in functions that seem suitable.  */
5068
5069 int
5070 ix86_frame_pointer_required (void)
5071 {
5072   /* If we accessed previous frames, then the generated code expects
5073      to be able to access the saved ebp value in our frame.  */
5074   if (cfun->machine->accesses_prev_frame)
5075     return 1;
5076
5077   /* Several x86 os'es need a frame pointer for other reasons,
5078      usually pertaining to setjmp.  */
5079   if (SUBTARGET_FRAME_POINTER_REQUIRED)
5080     return 1;
5081
5082   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
5083      the frame pointer by default.  Turn it back on now if we've not
5084      got a leaf function.  */
5085   if (TARGET_OMIT_LEAF_FRAME_POINTER
5086       && (!current_function_is_leaf
5087           || ix86_current_function_calls_tls_descriptor))
5088     return 1;
5089
5090   if (current_function_profile)
5091     return 1;
5092
5093   return 0;
5094 }
5095
5096 /* Record that the current function accesses previous call frames.  */
5097
5098 void
5099 ix86_setup_frame_addresses (void)
5100 {
5101   cfun->machine->accesses_prev_frame = 1;
5102 }
5103 \f
5104 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
5105 # define USE_HIDDEN_LINKONCE 1
5106 #else
5107 # define USE_HIDDEN_LINKONCE 0
5108 #endif
5109
5110 static int pic_labels_used;
5111
5112 /* Fills in the label name that should be used for a pc thunk for
5113    the given register.  */
5114
5115 static void
5116 get_pc_thunk_name (char name[32], unsigned int regno)
5117 {
5118   gcc_assert (!TARGET_64BIT);
5119
5120   if (USE_HIDDEN_LINKONCE)
5121     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
5122   else
5123     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
5124 }
5125
5126
5127 /* This function generates code for -fpic that loads %ebx with
5128    the return address of the caller and then returns.  */
5129
5130 void
5131 ix86_file_end (void)
5132 {
5133   rtx xops[2];
5134   int regno;
5135
5136   for (regno = 0; regno < 8; ++regno)
5137     {
5138       char name[32];
5139
5140       if (! ((pic_labels_used >> regno) & 1))
5141         continue;
5142
5143       get_pc_thunk_name (name, regno);
5144
5145 #if TARGET_MACHO
5146       if (TARGET_MACHO)
5147         {
5148           switch_to_section (darwin_sections[text_coal_section]);
5149           fputs ("\t.weak_definition\t", asm_out_file);
5150           assemble_name (asm_out_file, name);
5151           fputs ("\n\t.private_extern\t", asm_out_file);
5152           assemble_name (asm_out_file, name);
5153           fputs ("\n", asm_out_file);
5154           ASM_OUTPUT_LABEL (asm_out_file, name);
5155         }
5156       else
5157 #endif
5158       if (USE_HIDDEN_LINKONCE)
5159         {
5160           tree decl;
5161
5162           decl = build_decl (FUNCTION_DECL, get_identifier (name),
5163                              error_mark_node);
5164           TREE_PUBLIC (decl) = 1;
5165           TREE_STATIC (decl) = 1;
5166           DECL_ONE_ONLY (decl) = 1;
5167
5168           (*targetm.asm_out.unique_section) (decl, 0);
5169           switch_to_section (get_named_section (decl, NULL, 0));
5170
5171           (*targetm.asm_out.globalize_label) (asm_out_file, name);
5172           fputs ("\t.hidden\t", asm_out_file);
5173           assemble_name (asm_out_file, name);
5174           fputc ('\n', asm_out_file);
5175           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
5176         }
5177       else
5178         {
5179           switch_to_section (text_section);
5180           ASM_OUTPUT_LABEL (asm_out_file, name);
5181         }
5182
5183       xops[0] = gen_rtx_REG (SImode, regno);
5184       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
5185       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
5186       output_asm_insn ("ret", xops);
5187     }
5188
5189   if (NEED_INDICATE_EXEC_STACK)
5190     file_end_indicate_exec_stack ();
5191 }
5192
5193 /* Emit code for the SET_GOT patterns.  */
5194
5195 const char *
5196 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
5197 {
5198   rtx xops[3];
5199
5200   xops[0] = dest;
5201   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
5202
5203   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
5204     {
5205       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
5206
5207       if (!flag_pic)
5208         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5209       else
5210         output_asm_insn ("call\t%a2", xops);
5211
5212 #if TARGET_MACHO
5213       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5214          is what will be referenced by the Mach-O PIC subsystem.  */
5215       if (!label)
5216         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5217 #endif
5218
5219       (*targetm.asm_out.internal_label) (asm_out_file, "L",
5220                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
5221
5222       if (flag_pic)
5223         output_asm_insn ("pop{l}\t%0", xops);
5224     }
5225   else
5226     {
5227       char name[32];
5228       get_pc_thunk_name (name, REGNO (dest));
5229       pic_labels_used |= 1 << REGNO (dest);
5230
5231       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5232       xops[2] = gen_rtx_MEM (QImode, xops[2]);
5233       output_asm_insn ("call\t%X2", xops);
5234       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5235          is what will be referenced by the Mach-O PIC subsystem.  */
5236 #if TARGET_MACHO
5237       if (!label)
5238         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5239       else
5240         targetm.asm_out.internal_label (asm_out_file, "L",
5241                                            CODE_LABEL_NUMBER (label));
5242 #endif
5243     }
5244
5245   if (TARGET_MACHO)
5246     return "";
5247
5248   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
5249     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
5250   else
5251     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
5252
5253   return "";
5254 }
5255
5256 /* Generate an "push" pattern for input ARG.  */
5257
5258 static rtx
5259 gen_push (rtx arg)
5260 {
5261   return gen_rtx_SET (VOIDmode,
5262                       gen_rtx_MEM (Pmode,
5263                                    gen_rtx_PRE_DEC (Pmode,
5264                                                     stack_pointer_rtx)),
5265                       arg);
5266 }
5267
5268 /* Return >= 0 if there is an unused call-clobbered register available
5269    for the entire function.  */
5270
5271 static unsigned int
5272 ix86_select_alt_pic_regnum (void)
5273 {
5274   if (current_function_is_leaf && !current_function_profile
5275       && !ix86_current_function_calls_tls_descriptor)
5276     {
5277       int i;
5278       for (i = 2; i >= 0; --i)
5279         if (!regs_ever_live[i])
5280           return i;
5281     }
5282
5283   return INVALID_REGNUM;
5284 }
5285
5286 /* Return 1 if we need to save REGNO.  */
5287 static int
5288 ix86_save_reg (unsigned int regno, int maybe_eh_return)
5289 {
5290   if (pic_offset_table_rtx
5291       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5292       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5293           || current_function_profile
5294           || current_function_calls_eh_return
5295           || current_function_uses_const_pool))
5296     {
5297       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5298         return 0;
5299       return 1;
5300     }
5301
5302   if (current_function_calls_eh_return && maybe_eh_return)
5303     {
5304       unsigned i;
5305       for (i = 0; ; i++)
5306         {
5307           unsigned test = EH_RETURN_DATA_REGNO (i);
5308           if (test == INVALID_REGNUM)
5309             break;
5310           if (test == regno)
5311             return 1;
5312         }
5313     }
5314
5315   if (cfun->machine->force_align_arg_pointer
5316       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5317     return 1;
5318
5319   return (regs_ever_live[regno]
5320           && !call_used_regs[regno]
5321           && !fixed_regs[regno]
5322           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5323 }
5324
5325 /* Return number of registers to be saved on the stack.  */
5326
5327 static int
5328 ix86_nsaved_regs (void)
5329 {
5330   int nregs = 0;
5331   int regno;
5332
5333   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5334     if (ix86_save_reg (regno, true))
5335       nregs++;
5336   return nregs;
5337 }
5338
5339 /* Return the offset between two registers, one to be eliminated, and the other
5340    its replacement, at the start of a routine.  */
5341
5342 HOST_WIDE_INT
5343 ix86_initial_elimination_offset (int from, int to)
5344 {
5345   struct ix86_frame frame;
5346   ix86_compute_frame_layout (&frame);
5347
5348   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5349     return frame.hard_frame_pointer_offset;
5350   else if (from == FRAME_POINTER_REGNUM
5351            && to == HARD_FRAME_POINTER_REGNUM)
5352     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5353   else
5354     {
5355       gcc_assert (to == STACK_POINTER_REGNUM);
5356
5357       if (from == ARG_POINTER_REGNUM)
5358         return frame.stack_pointer_offset;
5359
5360       gcc_assert (from == FRAME_POINTER_REGNUM);
5361       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5362     }
5363 }
5364
5365 /* Fill structure ix86_frame about frame of currently computed function.  */
5366
5367 static void
5368 ix86_compute_frame_layout (struct ix86_frame *frame)
5369 {
5370   HOST_WIDE_INT total_size;
5371   unsigned int stack_alignment_needed;
5372   HOST_WIDE_INT offset;
5373   unsigned int preferred_alignment;
5374   HOST_WIDE_INT size = get_frame_size ();
5375
5376   frame->nregs = ix86_nsaved_regs ();
5377   total_size = size;
5378
5379   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5380   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5381
5382   /* During reload iteration the amount of registers saved can change.
5383      Recompute the value as needed.  Do not recompute when amount of registers
5384      didn't change as reload does multiple calls to the function and does not
5385      expect the decision to change within single iteration.  */
5386   if (!optimize_size
5387       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5388     {
5389       int count = frame->nregs;
5390
5391       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5392       /* The fast prologue uses move instead of push to save registers.  This
5393          is significantly longer, but also executes faster as modern hardware
5394          can execute the moves in parallel, but can't do that for push/pop.
5395
5396          Be careful about choosing what prologue to emit:  When function takes
5397          many instructions to execute we may use slow version as well as in
5398          case function is known to be outside hot spot (this is known with
5399          feedback only).  Weight the size of function by number of registers
5400          to save as it is cheap to use one or two push instructions but very
5401          slow to use many of them.  */
5402       if (count)
5403         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5404       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5405           || (flag_branch_probabilities
5406               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5407         cfun->machine->use_fast_prologue_epilogue = false;
5408       else
5409         cfun->machine->use_fast_prologue_epilogue
5410            = !expensive_function_p (count);
5411     }
5412   if (TARGET_PROLOGUE_USING_MOVE
5413       && cfun->machine->use_fast_prologue_epilogue)
5414     frame->save_regs_using_mov = true;
5415   else
5416     frame->save_regs_using_mov = false;
5417
5418
5419   /* Skip return address and saved base pointer.  */
5420   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5421
5422   frame->hard_frame_pointer_offset = offset;
5423
5424   /* Do some sanity checking of stack_alignment_needed and
5425      preferred_alignment, since i386 port is the only using those features
5426      that may break easily.  */
5427
5428   gcc_assert (!size || stack_alignment_needed);
5429   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5430   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5431   gcc_assert (stack_alignment_needed
5432               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5433
5434   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5435     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5436
5437   /* Register save area */
5438   offset += frame->nregs * UNITS_PER_WORD;
5439
5440   /* Va-arg area */
5441   if (ix86_save_varrargs_registers)
5442     {
5443       offset += X86_64_VARARGS_SIZE;
5444       frame->va_arg_size = X86_64_VARARGS_SIZE;
5445     }
5446   else
5447     frame->va_arg_size = 0;
5448
5449   /* Align start of frame for local function.  */
5450   frame->padding1 = ((offset + stack_alignment_needed - 1)
5451                      & -stack_alignment_needed) - offset;
5452
5453   offset += frame->padding1;
5454
5455   /* Frame pointer points here.  */
5456   frame->frame_pointer_offset = offset;
5457
5458   offset += size;
5459
5460   /* Add outgoing arguments area.  Can be skipped if we eliminated
5461      all the function calls as dead code.
5462      Skipping is however impossible when function calls alloca.  Alloca
5463      expander assumes that last current_function_outgoing_args_size
5464      of stack frame are unused.  */
5465   if (ACCUMULATE_OUTGOING_ARGS
5466       && (!current_function_is_leaf || current_function_calls_alloca
5467           || ix86_current_function_calls_tls_descriptor))
5468     {
5469       offset += current_function_outgoing_args_size;
5470       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5471     }
5472   else
5473     frame->outgoing_arguments_size = 0;
5474
5475   /* Align stack boundary.  Only needed if we're calling another function
5476      or using alloca.  */
5477   if (!current_function_is_leaf || current_function_calls_alloca
5478       || ix86_current_function_calls_tls_descriptor)
5479     frame->padding2 = ((offset + preferred_alignment - 1)
5480                        & -preferred_alignment) - offset;
5481   else
5482     frame->padding2 = 0;
5483
5484   offset += frame->padding2;
5485
5486   /* We've reached end of stack frame.  */
5487   frame->stack_pointer_offset = offset;
5488
5489   /* Size prologue needs to allocate.  */
5490   frame->to_allocate =
5491     (size + frame->padding1 + frame->padding2
5492      + frame->outgoing_arguments_size + frame->va_arg_size);
5493
5494   if ((!frame->to_allocate && frame->nregs <= 1)
5495       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5496     frame->save_regs_using_mov = false;
5497
5498   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5499       && current_function_is_leaf
5500       && !ix86_current_function_calls_tls_descriptor)
5501     {
5502       frame->red_zone_size = frame->to_allocate;
5503       if (frame->save_regs_using_mov)
5504         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5505       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5506         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5507     }
5508   else
5509     frame->red_zone_size = 0;
5510   frame->to_allocate -= frame->red_zone_size;
5511   frame->stack_pointer_offset -= frame->red_zone_size;
5512 #if 0
5513   fprintf (stderr, "nregs: %i\n", frame->nregs);
5514   fprintf (stderr, "size: %i\n", size);
5515   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
5516   fprintf (stderr, "padding1: %i\n", frame->padding1);
5517   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
5518   fprintf (stderr, "padding2: %i\n", frame->padding2);
5519   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
5520   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
5521   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
5522   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
5523            frame->hard_frame_pointer_offset);
5524   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
5525 #endif
5526 }
5527
5528 /* Emit code to save registers in the prologue.  */
5529
5530 static void
5531 ix86_emit_save_regs (void)
5532 {
5533   unsigned int regno;
5534   rtx insn;
5535
5536   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5537     if (ix86_save_reg (regno, true))
5538       {
5539         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5540         RTX_FRAME_RELATED_P (insn) = 1;
5541       }
5542 }
5543
5544 /* Emit code to save registers using MOV insns.  First register
5545    is restored from POINTER + OFFSET.  */
5546 static void
5547 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5548 {
5549   unsigned int regno;
5550   rtx insn;
5551
5552   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5553     if (ix86_save_reg (regno, true))
5554       {
5555         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5556                                                Pmode, offset),
5557                                gen_rtx_REG (Pmode, regno));
5558         RTX_FRAME_RELATED_P (insn) = 1;
5559         offset += UNITS_PER_WORD;
5560       }
5561 }
5562
5563 /* Expand prologue or epilogue stack adjustment.
5564    The pattern exist to put a dependency on all ebp-based memory accesses.
5565    STYLE should be negative if instructions should be marked as frame related,
5566    zero if %r11 register is live and cannot be freely used and positive
5567    otherwise.  */
5568
5569 static void
5570 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5571 {
5572   rtx insn;
5573
5574   if (! TARGET_64BIT)
5575     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5576   else if (x86_64_immediate_operand (offset, DImode))
5577     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5578   else
5579     {
5580       rtx r11;
5581       /* r11 is used by indirect sibcall return as well, set before the
5582          epilogue and used after the epilogue.  ATM indirect sibcall
5583          shouldn't be used together with huge frame sizes in one
5584          function because of the frame_size check in sibcall.c.  */
5585       gcc_assert (style);
5586       r11 = gen_rtx_REG (DImode, R11_REG);
5587       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5588       if (style < 0)
5589         RTX_FRAME_RELATED_P (insn) = 1;
5590       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5591                                                                offset));
5592     }
5593   if (style < 0)
5594     RTX_FRAME_RELATED_P (insn) = 1;
5595 }
5596
5597 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5598
5599 static rtx
5600 ix86_internal_arg_pointer (void)
5601 {
5602   bool has_force_align_arg_pointer =
5603     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5604                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5605   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5606        && DECL_NAME (current_function_decl)
5607        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5608        && DECL_FILE_SCOPE_P (current_function_decl))
5609       || ix86_force_align_arg_pointer
5610       || has_force_align_arg_pointer)
5611     {
5612       /* Nested functions can't realign the stack due to a register
5613          conflict.  */
5614       if (DECL_CONTEXT (current_function_decl)
5615           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5616         {
5617           if (ix86_force_align_arg_pointer)
5618             warning (0, "-mstackrealign ignored for nested functions");
5619           if (has_force_align_arg_pointer)
5620             error ("%s not supported for nested functions",
5621                    ix86_force_align_arg_pointer_string);
5622           return virtual_incoming_args_rtx;
5623         }
5624       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5625       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5626     }
5627   else
5628     return virtual_incoming_args_rtx;
5629 }
5630
5631 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5632    This is called from dwarf2out.c to emit call frame instructions
5633    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5634 static void
5635 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5636 {
5637   rtx unspec = SET_SRC (pattern);
5638   gcc_assert (GET_CODE (unspec) == UNSPEC);
5639
5640   switch (index)
5641     {
5642     case UNSPEC_REG_SAVE:
5643       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5644                               SET_DEST (pattern));
5645       break;
5646     case UNSPEC_DEF_CFA:
5647       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5648                          INTVAL (XVECEXP (unspec, 0, 0)));
5649       break;
5650     default:
5651       gcc_unreachable ();
5652     }
5653 }
5654
5655 /* Expand the prologue into a bunch of separate insns.  */
5656
5657 void
5658 ix86_expand_prologue (void)
5659 {
5660   rtx insn;
5661   bool pic_reg_used;
5662   struct ix86_frame frame;
5663   HOST_WIDE_INT allocate;
5664
5665   ix86_compute_frame_layout (&frame);
5666
5667   if (cfun->machine->force_align_arg_pointer)
5668     {
5669       rtx x, y;
5670
5671       /* Grab the argument pointer.  */
5672       x = plus_constant (stack_pointer_rtx, 4);
5673       y = cfun->machine->force_align_arg_pointer;
5674       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5675       RTX_FRAME_RELATED_P (insn) = 1;
5676
5677       /* The unwind info consists of two parts: install the fafp as the cfa,
5678          and record the fafp as the "save register" of the stack pointer.
5679          The later is there in order that the unwinder can see where it
5680          should restore the stack pointer across the and insn.  */
5681       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5682       x = gen_rtx_SET (VOIDmode, y, x);
5683       RTX_FRAME_RELATED_P (x) = 1;
5684       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5685                           UNSPEC_REG_SAVE);
5686       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5687       RTX_FRAME_RELATED_P (y) = 1;
5688       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5689       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5690       REG_NOTES (insn) = x;
5691
5692       /* Align the stack.  */
5693       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5694                              GEN_INT (-16)));
5695
5696       /* And here we cheat like madmen with the unwind info.  We force the
5697          cfa register back to sp+4, which is exactly what it was at the
5698          start of the function.  Re-pushing the return address results in
5699          the return at the same spot relative to the cfa, and thus is
5700          correct wrt the unwind info.  */
5701       x = cfun->machine->force_align_arg_pointer;
5702       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5703       insn = emit_insn (gen_push (x));
5704       RTX_FRAME_RELATED_P (insn) = 1;
5705
5706       x = GEN_INT (4);
5707       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5708       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5709       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5710       REG_NOTES (insn) = x;
5711     }
5712
5713   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5714      slower on all targets.  Also sdb doesn't like it.  */
5715
5716   if (frame_pointer_needed)
5717     {
5718       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5719       RTX_FRAME_RELATED_P (insn) = 1;
5720
5721       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5722       RTX_FRAME_RELATED_P (insn) = 1;
5723     }
5724
5725   allocate = frame.to_allocate;
5726
5727   if (!frame.save_regs_using_mov)
5728     ix86_emit_save_regs ();
5729   else
5730     allocate += frame.nregs * UNITS_PER_WORD;
5731
5732   /* When using red zone we may start register saving before allocating
5733      the stack frame saving one cycle of the prologue.  */
5734   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5735     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5736                                    : stack_pointer_rtx,
5737                                    -frame.nregs * UNITS_PER_WORD);
5738
5739   if (allocate == 0)
5740     ;
5741   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5742     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5743                                GEN_INT (-allocate), -1);
5744   else
5745     {
5746       /* Only valid for Win32.  */
5747       rtx eax = gen_rtx_REG (SImode, 0);
5748       bool eax_live = ix86_eax_live_at_start_p ();
5749       rtx t;
5750
5751       gcc_assert (!TARGET_64BIT);
5752
5753       if (eax_live)
5754         {
5755           emit_insn (gen_push (eax));
5756           allocate -= 4;
5757         }
5758
5759       emit_move_insn (eax, GEN_INT (allocate));
5760
5761       insn = emit_insn (gen_allocate_stack_worker (eax));
5762       RTX_FRAME_RELATED_P (insn) = 1;
5763       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5764       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5765       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5766                                             t, REG_NOTES (insn));
5767
5768       if (eax_live)
5769         {
5770           if (frame_pointer_needed)
5771             t = plus_constant (hard_frame_pointer_rtx,
5772                                allocate
5773                                - frame.to_allocate
5774                                - frame.nregs * UNITS_PER_WORD);
5775           else
5776             t = plus_constant (stack_pointer_rtx, allocate);
5777           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5778         }
5779     }
5780
5781   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5782     {
5783       if (!frame_pointer_needed || !frame.to_allocate)
5784         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5785       else
5786         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5787                                        -frame.nregs * UNITS_PER_WORD);
5788     }
5789
5790   pic_reg_used = false;
5791   if (pic_offset_table_rtx
5792       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5793           || current_function_profile))
5794     {
5795       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5796
5797       if (alt_pic_reg_used != INVALID_REGNUM)
5798         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5799
5800       pic_reg_used = true;
5801     }
5802
5803   if (pic_reg_used)
5804     {
5805       if (TARGET_64BIT)
5806         insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5807       else
5808         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5809
5810       /* Even with accurate pre-reload life analysis, we can wind up
5811          deleting all references to the pic register after reload.
5812          Consider if cross-jumping unifies two sides of a branch
5813          controlled by a comparison vs the only read from a global.
5814          In which case, allow the set_got to be deleted, though we're
5815          too late to do anything about the ebx save in the prologue.  */
5816       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5817     }
5818
5819   /* Prevent function calls from be scheduled before the call to mcount.
5820      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5821   if (current_function_profile)
5822     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5823 }
5824
5825 /* Emit code to restore saved registers using MOV insns.  First register
5826    is restored from POINTER + OFFSET.  */
5827 static void
5828 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5829                                   int maybe_eh_return)
5830 {
5831   int regno;
5832   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5833
5834   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5835     if (ix86_save_reg (regno, maybe_eh_return))
5836       {
5837         /* Ensure that adjust_address won't be forced to produce pointer
5838            out of range allowed by x86-64 instruction set.  */
5839         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5840           {
5841             rtx r11;
5842
5843             r11 = gen_rtx_REG (DImode, R11_REG);
5844             emit_move_insn (r11, GEN_INT (offset));
5845             emit_insn (gen_adddi3 (r11, r11, pointer));
5846             base_address = gen_rtx_MEM (Pmode, r11);
5847             offset = 0;
5848           }
5849         emit_move_insn (gen_rtx_REG (Pmode, regno),
5850                         adjust_address (base_address, Pmode, offset));
5851         offset += UNITS_PER_WORD;
5852       }
5853 }
5854
5855 /* Restore function stack, frame, and registers.  */
5856
5857 void
5858 ix86_expand_epilogue (int style)
5859 {
5860   int regno;
5861   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5862   struct ix86_frame frame;
5863   HOST_WIDE_INT offset;
5864
5865   ix86_compute_frame_layout (&frame);
5866
5867   /* Calculate start of saved registers relative to ebp.  Special care
5868      must be taken for the normal return case of a function using
5869      eh_return: the eax and edx registers are marked as saved, but not
5870      restored along this path.  */
5871   offset = frame.nregs;
5872   if (current_function_calls_eh_return && style != 2)
5873     offset -= 2;
5874   offset *= -UNITS_PER_WORD;
5875
5876   /* If we're only restoring one register and sp is not valid then
5877      using a move instruction to restore the register since it's
5878      less work than reloading sp and popping the register.
5879
5880      The default code result in stack adjustment using add/lea instruction,
5881      while this code results in LEAVE instruction (or discrete equivalent),
5882      so it is profitable in some other cases as well.  Especially when there
5883      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5884      and there is exactly one register to pop. This heuristic may need some
5885      tuning in future.  */
5886   if ((!sp_valid && frame.nregs <= 1)
5887       || (TARGET_EPILOGUE_USING_MOVE
5888           && cfun->machine->use_fast_prologue_epilogue
5889           && (frame.nregs > 1 || frame.to_allocate))
5890       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5891       || (frame_pointer_needed && TARGET_USE_LEAVE
5892           && cfun->machine->use_fast_prologue_epilogue
5893           && frame.nregs == 1)
5894       || current_function_calls_eh_return)
5895     {
5896       /* Restore registers.  We can use ebp or esp to address the memory
5897          locations.  If both are available, default to ebp, since offsets
5898          are known to be small.  Only exception is esp pointing directly to the
5899          end of block of saved registers, where we may simplify addressing
5900          mode.  */
5901
5902       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5903         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5904                                           frame.to_allocate, style == 2);
5905       else
5906         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5907                                           offset, style == 2);
5908
5909       /* eh_return epilogues need %ecx added to the stack pointer.  */
5910       if (style == 2)
5911         {
5912           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5913
5914           if (frame_pointer_needed)
5915             {
5916               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5917               tmp = plus_constant (tmp, UNITS_PER_WORD);
5918               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5919
5920               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5921               emit_move_insn (hard_frame_pointer_rtx, tmp);
5922
5923               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5924                                          const0_rtx, style);
5925             }
5926           else
5927             {
5928               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5929               tmp = plus_constant (tmp, (frame.to_allocate
5930                                          + frame.nregs * UNITS_PER_WORD));
5931               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5932             }
5933         }
5934       else if (!frame_pointer_needed)
5935         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5936                                    GEN_INT (frame.to_allocate
5937                                             + frame.nregs * UNITS_PER_WORD),
5938                                    style);
5939       /* If not an i386, mov & pop is faster than "leave".  */
5940       else if (TARGET_USE_LEAVE || optimize_size
5941                || !cfun->machine->use_fast_prologue_epilogue)
5942         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5943       else
5944         {
5945           pro_epilogue_adjust_stack (stack_pointer_rtx,
5946                                      hard_frame_pointer_rtx,
5947                                      const0_rtx, style);
5948           if (TARGET_64BIT)
5949             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5950           else
5951             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5952         }
5953     }
5954   else
5955     {
5956       /* First step is to deallocate the stack frame so that we can
5957          pop the registers.  */
5958       if (!sp_valid)
5959         {
5960           gcc_assert (frame_pointer_needed);
5961           pro_epilogue_adjust_stack (stack_pointer_rtx,
5962                                      hard_frame_pointer_rtx,
5963                                      GEN_INT (offset), style);
5964         }
5965       else if (frame.to_allocate)
5966         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5967                                    GEN_INT (frame.to_allocate), style);
5968
5969       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5970         if (ix86_save_reg (regno, false))
5971           {
5972             if (TARGET_64BIT)
5973               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5974             else
5975               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5976           }
5977       if (frame_pointer_needed)
5978         {
5979           /* Leave results in shorter dependency chains on CPUs that are
5980              able to grok it fast.  */
5981           if (TARGET_USE_LEAVE)
5982             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5983           else if (TARGET_64BIT)
5984             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5985           else
5986             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5987         }
5988     }
5989
5990   if (cfun->machine->force_align_arg_pointer)
5991     {
5992       emit_insn (gen_addsi3 (stack_pointer_rtx,
5993                              cfun->machine->force_align_arg_pointer,
5994                              GEN_INT (-4)));
5995     }
5996
5997   /* Sibcall epilogues don't want a return instruction.  */
5998   if (style == 0)
5999     return;
6000
6001   if (current_function_pops_args && current_function_args_size)
6002     {
6003       rtx popc = GEN_INT (current_function_pops_args);
6004
6005       /* i386 can only pop 64K bytes.  If asked to pop more, pop
6006          return address, do explicit add, and jump indirectly to the
6007          caller.  */
6008
6009       if (current_function_pops_args >= 65536)
6010         {
6011           rtx ecx = gen_rtx_REG (SImode, 2);
6012
6013           /* There is no "pascal" calling convention in 64bit ABI.  */
6014           gcc_assert (!TARGET_64BIT);
6015
6016           emit_insn (gen_popsi1 (ecx));
6017           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
6018           emit_jump_insn (gen_return_indirect_internal (ecx));
6019         }
6020       else
6021         emit_jump_insn (gen_return_pop_internal (popc));
6022     }
6023   else
6024     emit_jump_insn (gen_return_internal ());
6025 }
6026
6027 /* Reset from the function's potential modifications.  */
6028
6029 static void
6030 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6031                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6032 {
6033   if (pic_offset_table_rtx)
6034     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
6035 #if TARGET_MACHO
6036   /* Mach-O doesn't support labels at the end of objects, so if
6037      it looks like we might want one, insert a NOP.  */
6038   {
6039     rtx insn = get_last_insn ();
6040     while (insn
6041            && NOTE_P (insn)
6042            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
6043       insn = PREV_INSN (insn);
6044     if (insn
6045         && (LABEL_P (insn)
6046             || (NOTE_P (insn)
6047                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
6048       fputs ("\tnop\n", file);
6049   }
6050 #endif
6051
6052 }
6053 \f
6054 /* Extract the parts of an RTL expression that is a valid memory address
6055    for an instruction.  Return 0 if the structure of the address is
6056    grossly off.  Return -1 if the address contains ASHIFT, so it is not
6057    strictly valid, but still used for computing length of lea instruction.  */
6058
6059 int
6060 ix86_decompose_address (rtx addr, struct ix86_address *out)
6061 {
6062   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
6063   rtx base_reg, index_reg;
6064   HOST_WIDE_INT scale = 1;
6065   rtx scale_rtx = NULL_RTX;
6066   int retval = 1;
6067   enum ix86_address_seg seg = SEG_DEFAULT;
6068
6069   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
6070     base = addr;
6071   else if (GET_CODE (addr) == PLUS)
6072     {
6073       rtx addends[4], op;
6074       int n = 0, i;
6075
6076       op = addr;
6077       do
6078         {
6079           if (n >= 4)
6080             return 0;
6081           addends[n++] = XEXP (op, 1);
6082           op = XEXP (op, 0);
6083         }
6084       while (GET_CODE (op) == PLUS);
6085       if (n >= 4)
6086         return 0;
6087       addends[n] = op;
6088
6089       for (i = n; i >= 0; --i)
6090         {
6091           op = addends[i];
6092           switch (GET_CODE (op))
6093             {
6094             case MULT:
6095               if (index)
6096                 return 0;
6097               index = XEXP (op, 0);
6098               scale_rtx = XEXP (op, 1);
6099               break;
6100
6101             case UNSPEC:
6102               if (XINT (op, 1) == UNSPEC_TP
6103                   && TARGET_TLS_DIRECT_SEG_REFS
6104                   && seg == SEG_DEFAULT)
6105                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
6106               else
6107                 return 0;
6108               break;
6109
6110             case REG:
6111             case SUBREG:
6112               if (!base)
6113                 base = op;
6114               else if (!index)
6115                 index = op;
6116               else
6117                 return 0;
6118               break;
6119
6120             case CONST:
6121             case CONST_INT:
6122             case SYMBOL_REF:
6123             case LABEL_REF:
6124               if (disp)
6125                 return 0;
6126               disp = op;
6127               break;
6128
6129             default:
6130               return 0;
6131             }
6132         }
6133     }
6134   else if (GET_CODE (addr) == MULT)
6135     {
6136       index = XEXP (addr, 0);           /* index*scale */
6137       scale_rtx = XEXP (addr, 1);
6138     }
6139   else if (GET_CODE (addr) == ASHIFT)
6140     {
6141       rtx tmp;
6142
6143       /* We're called for lea too, which implements ashift on occasion.  */
6144       index = XEXP (addr, 0);
6145       tmp = XEXP (addr, 1);
6146       if (GET_CODE (tmp) != CONST_INT)
6147         return 0;
6148       scale = INTVAL (tmp);
6149       if ((unsigned HOST_WIDE_INT) scale > 3)
6150         return 0;
6151       scale = 1 << scale;
6152       retval = -1;
6153     }
6154   else
6155     disp = addr;                        /* displacement */
6156
6157   /* Extract the integral value of scale.  */
6158   if (scale_rtx)
6159     {
6160       if (GET_CODE (scale_rtx) != CONST_INT)
6161         return 0;
6162       scale = INTVAL (scale_rtx);
6163     }
6164
6165   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
6166   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
6167
6168   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
6169   if (base_reg && index_reg && scale == 1
6170       && (index_reg == arg_pointer_rtx
6171           || index_reg == frame_pointer_rtx
6172           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
6173     {
6174       rtx tmp;
6175       tmp = base, base = index, index = tmp;
6176       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
6177     }
6178
6179   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
6180   if ((base_reg == hard_frame_pointer_rtx
6181        || base_reg == frame_pointer_rtx
6182        || base_reg == arg_pointer_rtx) && !disp)
6183     disp = const0_rtx;
6184
6185   /* Special case: on K6, [%esi] makes the instruction vector decoded.
6186      Avoid this by transforming to [%esi+0].  */
6187   if (ix86_tune == PROCESSOR_K6 && !optimize_size
6188       && base_reg && !index_reg && !disp
6189       && REG_P (base_reg)
6190       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
6191     disp = const0_rtx;
6192
6193   /* Special case: encode reg+reg instead of reg*2.  */
6194   if (!base && index && scale && scale == 2)
6195     base = index, base_reg = index_reg, scale = 1;
6196
6197   /* Special case: scaling cannot be encoded without base or displacement.  */
6198   if (!base && !disp && index && scale != 1)
6199     disp = const0_rtx;
6200
6201   out->base = base;
6202   out->index = index;
6203   out->disp = disp;
6204   out->scale = scale;
6205   out->seg = seg;
6206
6207   return retval;
6208 }
6209 \f
6210 /* Return cost of the memory address x.
6211    For i386, it is better to use a complex address than let gcc copy
6212    the address into a reg and make a new pseudo.  But not if the address
6213    requires to two regs - that would mean more pseudos with longer
6214    lifetimes.  */
6215 static int
6216 ix86_address_cost (rtx x)
6217 {
6218   struct ix86_address parts;
6219   int cost = 1;
6220   int ok = ix86_decompose_address (x, &parts);
6221
6222   gcc_assert (ok);
6223
6224   if (parts.base && GET_CODE (parts.base) == SUBREG)
6225     parts.base = SUBREG_REG (parts.base);
6226   if (parts.index && GET_CODE (parts.index) == SUBREG)
6227     parts.index = SUBREG_REG (parts.index);
6228
6229   /* More complex memory references are better.  */
6230   if (parts.disp && parts.disp != const0_rtx)
6231     cost--;
6232   if (parts.seg != SEG_DEFAULT)
6233     cost--;
6234
6235   /* Attempt to minimize number of registers in the address.  */
6236   if ((parts.base
6237        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
6238       || (parts.index
6239           && (!REG_P (parts.index)
6240               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
6241     cost++;
6242
6243   if (parts.base
6244       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
6245       && parts.index
6246       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
6247       && parts.base != parts.index)
6248     cost++;
6249
6250   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
6251      since it's predecode logic can't detect the length of instructions
6252      and it degenerates to vector decoded.  Increase cost of such
6253      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
6254      to split such addresses or even refuse such addresses at all.
6255
6256      Following addressing modes are affected:
6257       [base+scale*index]
6258       [scale*index+disp]
6259       [base+index]
6260
6261      The first and last case  may be avoidable by explicitly coding the zero in
6262      memory address, but I don't have AMD-K6 machine handy to check this
6263      theory.  */
6264
6265   if (TARGET_K6
6266       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
6267           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
6268           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
6269     cost += 10;
6270
6271   return cost;
6272 }
6273 \f
6274 /* If X is a machine specific address (i.e. a symbol or label being
6275    referenced as a displacement from the GOT implemented using an
6276    UNSPEC), then return the base term.  Otherwise return X.  */
6277
6278 rtx
6279 ix86_find_base_term (rtx x)
6280 {
6281   rtx term;
6282
6283   if (TARGET_64BIT)
6284     {
6285       if (GET_CODE (x) != CONST)
6286         return x;
6287       term = XEXP (x, 0);
6288       if (GET_CODE (term) == PLUS
6289           && (GET_CODE (XEXP (term, 1)) == CONST_INT
6290               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
6291         term = XEXP (term, 0);
6292       if (GET_CODE (term) != UNSPEC
6293           || XINT (term, 1) != UNSPEC_GOTPCREL)
6294         return x;
6295
6296       term = XVECEXP (term, 0, 0);
6297
6298       if (GET_CODE (term) != SYMBOL_REF
6299           && GET_CODE (term) != LABEL_REF)
6300         return x;
6301
6302       return term;
6303     }
6304
6305   term = ix86_delegitimize_address (x);
6306
6307   if (GET_CODE (term) != SYMBOL_REF
6308       && GET_CODE (term) != LABEL_REF)
6309     return x;
6310
6311   return term;
6312 }
6313
6314 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6315    this is used for to form addresses to local data when -fPIC is in
6316    use.  */
6317
6318 static bool
6319 darwin_local_data_pic (rtx disp)
6320 {
6321   if (GET_CODE (disp) == MINUS)
6322     {
6323       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6324           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6325         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6326           {
6327             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6328             if (! strcmp (sym_name, "<pic base>"))
6329               return true;
6330           }
6331     }
6332
6333   return false;
6334 }
6335 \f
6336 /* Determine if a given RTX is a valid constant.  We already know this
6337    satisfies CONSTANT_P.  */
6338
6339 bool
6340 legitimate_constant_p (rtx x)
6341 {
6342   switch (GET_CODE (x))
6343     {
6344     case CONST:
6345       x = XEXP (x, 0);
6346
6347       if (GET_CODE (x) == PLUS)
6348         {
6349           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6350             return false;
6351           x = XEXP (x, 0);
6352         }
6353
6354       if (TARGET_MACHO && darwin_local_data_pic (x))
6355         return true;
6356
6357       /* Only some unspecs are valid as "constants".  */
6358       if (GET_CODE (x) == UNSPEC)
6359         switch (XINT (x, 1))
6360           {
6361           case UNSPEC_GOTOFF:
6362             return TARGET_64BIT;
6363           case UNSPEC_TPOFF:
6364           case UNSPEC_NTPOFF:
6365             x = XVECEXP (x, 0, 0);
6366             return (GET_CODE (x) == SYMBOL_REF
6367                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6368           case UNSPEC_DTPOFF:
6369             x = XVECEXP (x, 0, 0);
6370             return (GET_CODE (x) == SYMBOL_REF
6371                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6372           default:
6373             return false;
6374           }
6375
6376       /* We must have drilled down to a symbol.  */
6377       if (GET_CODE (x) == LABEL_REF)
6378         return true;
6379       if (GET_CODE (x) != SYMBOL_REF)
6380         return false;
6381       /* FALLTHRU */
6382
6383     case SYMBOL_REF:
6384       /* TLS symbols are never valid.  */
6385       if (SYMBOL_REF_TLS_MODEL (x))
6386         return false;
6387       break;
6388
6389     case CONST_DOUBLE:
6390       if (GET_MODE (x) == TImode
6391           && x != CONST0_RTX (TImode)
6392           && !TARGET_64BIT)
6393         return false;
6394       break;
6395
6396     case CONST_VECTOR:
6397       if (x == CONST0_RTX (GET_MODE (x)))
6398         return true;
6399       return false;
6400
6401     default:
6402       break;
6403     }
6404
6405   /* Otherwise we handle everything else in the move patterns.  */
6406   return true;
6407 }
6408
6409 /* Determine if it's legal to put X into the constant pool.  This
6410    is not possible for the address of thread-local symbols, which
6411    is checked above.  */
6412
6413 static bool
6414 ix86_cannot_force_const_mem (rtx x)
6415 {
6416   /* We can always put integral constants and vectors in memory.  */
6417   switch (GET_CODE (x))
6418     {
6419     case CONST_INT:
6420     case CONST_DOUBLE:
6421     case CONST_VECTOR:
6422       return false;
6423
6424     default:
6425       break;
6426     }
6427   return !legitimate_constant_p (x);
6428 }
6429
6430 /* Determine if a given RTX is a valid constant address.  */
6431
6432 bool
6433 constant_address_p (rtx x)
6434 {
6435   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6436 }
6437
6438 /* Nonzero if the constant value X is a legitimate general operand
6439    when generating PIC code.  It is given that flag_pic is on and
6440    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6441
6442 bool
6443 legitimate_pic_operand_p (rtx x)
6444 {
6445   rtx inner;
6446
6447   switch (GET_CODE (x))
6448     {
6449     case CONST:
6450       inner = XEXP (x, 0);
6451       if (GET_CODE (inner) == PLUS
6452           && GET_CODE (XEXP (inner, 1)) == CONST_INT)
6453         inner = XEXP (inner, 0);
6454
6455       /* Only some unspecs are valid as "constants".  */
6456       if (GET_CODE (inner) == UNSPEC)
6457         switch (XINT (inner, 1))
6458           {
6459           case UNSPEC_GOTOFF:
6460             return TARGET_64BIT;
6461           case UNSPEC_TPOFF:
6462             x = XVECEXP (inner, 0, 0);
6463             return (GET_CODE (x) == SYMBOL_REF
6464                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6465           default:
6466             return false;
6467           }
6468       /* FALLTHRU */
6469
6470     case SYMBOL_REF:
6471     case LABEL_REF:
6472       return legitimate_pic_address_disp_p (x);
6473
6474     default:
6475       return true;
6476     }
6477 }
6478
6479 /* Determine if a given CONST RTX is a valid memory displacement
6480    in PIC mode.  */
6481
6482 int
6483 legitimate_pic_address_disp_p (rtx disp)
6484 {
6485   bool saw_plus;
6486
6487   /* In 64bit mode we can allow direct addresses of symbols and labels
6488      when they are not dynamic symbols.  */
6489   if (TARGET_64BIT)
6490     {
6491       rtx op0 = disp, op1;
6492
6493       switch (GET_CODE (disp))
6494         {
6495         case LABEL_REF:
6496           return true;
6497
6498         case CONST:
6499           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6500             break;
6501           op0 = XEXP (XEXP (disp, 0), 0);
6502           op1 = XEXP (XEXP (disp, 0), 1);
6503           if (GET_CODE (op1) != CONST_INT
6504               || INTVAL (op1) >= 16*1024*1024
6505               || INTVAL (op1) < -16*1024*1024)
6506             break;
6507           if (GET_CODE (op0) == LABEL_REF)
6508             return true;
6509           if (GET_CODE (op0) != SYMBOL_REF)
6510             break;
6511           /* FALLTHRU */
6512
6513         case SYMBOL_REF:
6514           /* TLS references should always be enclosed in UNSPEC.  */
6515           if (SYMBOL_REF_TLS_MODEL (op0))
6516             return false;
6517           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0))
6518             return true;
6519           break;
6520
6521         default:
6522           break;
6523         }
6524     }
6525   if (GET_CODE (disp) != CONST)
6526     return 0;
6527   disp = XEXP (disp, 0);
6528
6529   if (TARGET_64BIT)
6530     {
6531       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6532          of GOT tables.  We should not need these anyway.  */
6533       if (GET_CODE (disp) != UNSPEC
6534           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6535               && XINT (disp, 1) != UNSPEC_GOTOFF))
6536         return 0;
6537
6538       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6539           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6540         return 0;
6541       return 1;
6542     }
6543
6544   saw_plus = false;
6545   if (GET_CODE (disp) == PLUS)
6546     {
6547       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
6548         return 0;
6549       disp = XEXP (disp, 0);
6550       saw_plus = true;
6551     }
6552
6553   if (TARGET_MACHO && darwin_local_data_pic (disp))
6554     return 1;
6555
6556   if (GET_CODE (disp) != UNSPEC)
6557     return 0;
6558
6559   switch (XINT (disp, 1))
6560     {
6561     case UNSPEC_GOT:
6562       if (saw_plus)
6563         return false;
6564       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
6565     case UNSPEC_GOTOFF:
6566       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6567          While ABI specify also 32bit relocation but we don't produce it in
6568          small PIC model at all.  */
6569       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6570            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6571           && !TARGET_64BIT)
6572         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
6573       return false;
6574     case UNSPEC_GOTTPOFF:
6575     case UNSPEC_GOTNTPOFF:
6576     case UNSPEC_INDNTPOFF:
6577       if (saw_plus)
6578         return false;
6579       disp = XVECEXP (disp, 0, 0);
6580       return (GET_CODE (disp) == SYMBOL_REF
6581               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6582     case UNSPEC_NTPOFF:
6583       disp = XVECEXP (disp, 0, 0);
6584       return (GET_CODE (disp) == SYMBOL_REF
6585               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6586     case UNSPEC_DTPOFF:
6587       disp = XVECEXP (disp, 0, 0);
6588       return (GET_CODE (disp) == SYMBOL_REF
6589               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6590     }
6591
6592   return 0;
6593 }
6594
6595 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6596    memory address for an instruction.  The MODE argument is the machine mode
6597    for the MEM expression that wants to use this address.
6598
6599    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6600    convert common non-canonical forms to canonical form so that they will
6601    be recognized.  */
6602
6603 int
6604 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
6605 {
6606   struct ix86_address parts;
6607   rtx base, index, disp;
6608   HOST_WIDE_INT scale;
6609   const char *reason = NULL;
6610   rtx reason_rtx = NULL_RTX;
6611
6612   if (TARGET_DEBUG_ADDR)
6613     {
6614       fprintf (stderr,
6615                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
6616                GET_MODE_NAME (mode), strict);
6617       debug_rtx (addr);
6618     }
6619
6620   if (ix86_decompose_address (addr, &parts) <= 0)
6621     {
6622       reason = "decomposition failed";
6623       goto report_error;
6624     }
6625
6626   base = parts.base;
6627   index = parts.index;
6628   disp = parts.disp;
6629   scale = parts.scale;
6630
6631   /* Validate base register.
6632
6633      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6634      failures when the base is one word out of a two word structure, which is
6635      represented internally as a DImode int.  */
6636
6637   if (base)
6638     {
6639       rtx reg;
6640       reason_rtx = base;
6641
6642       if (REG_P (base))
6643         reg = base;
6644       else if (GET_CODE (base) == SUBREG
6645                && REG_P (SUBREG_REG (base))
6646                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6647                   <= UNITS_PER_WORD)
6648         reg = SUBREG_REG (base);
6649       else
6650         {
6651           reason = "base is not a register";
6652           goto report_error;
6653         }
6654
6655       if (GET_MODE (base) != Pmode)
6656         {
6657           reason = "base is not in Pmode";
6658           goto report_error;
6659         }
6660
6661       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6662           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6663         {
6664           reason = "base is not valid";
6665           goto report_error;
6666         }
6667     }
6668
6669   /* Validate index register.
6670
6671      Don't allow SUBREG's that span more than a word here -- same as above.  */
6672
6673   if (index)
6674     {
6675       rtx reg;
6676       reason_rtx = index;
6677
6678       if (REG_P (index))
6679         reg = index;
6680       else if (GET_CODE (index) == SUBREG
6681                && REG_P (SUBREG_REG (index))
6682                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6683                   <= UNITS_PER_WORD)
6684         reg = SUBREG_REG (index);
6685       else
6686         {
6687           reason = "index is not a register";
6688           goto report_error;
6689         }
6690
6691       if (GET_MODE (index) != Pmode)
6692         {
6693           reason = "index is not in Pmode";
6694           goto report_error;
6695         }
6696
6697       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6698           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6699         {
6700           reason = "index is not valid";
6701           goto report_error;
6702         }
6703     }
6704
6705   /* Validate scale factor.  */
6706   if (scale != 1)
6707     {
6708       reason_rtx = GEN_INT (scale);
6709       if (!index)
6710         {
6711           reason = "scale without index";
6712           goto report_error;
6713         }
6714
6715       if (scale != 2 && scale != 4 && scale != 8)
6716         {
6717           reason = "scale is not a valid multiplier";
6718           goto report_error;
6719         }
6720     }
6721
6722   /* Validate displacement.  */
6723   if (disp)
6724     {
6725       reason_rtx = disp;
6726
6727       if (GET_CODE (disp) == CONST
6728           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6729         switch (XINT (XEXP (disp, 0), 1))
6730           {
6731           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6732              used.  While ABI specify also 32bit relocations, we don't produce
6733              them at all and use IP relative instead.  */
6734           case UNSPEC_GOT:
6735           case UNSPEC_GOTOFF:
6736             gcc_assert (flag_pic);
6737             if (!TARGET_64BIT)
6738               goto is_legitimate_pic;
6739             reason = "64bit address unspec";
6740             goto report_error;
6741
6742           case UNSPEC_GOTPCREL:
6743             gcc_assert (flag_pic);
6744             goto is_legitimate_pic;
6745
6746           case UNSPEC_GOTTPOFF:
6747           case UNSPEC_GOTNTPOFF:
6748           case UNSPEC_INDNTPOFF:
6749           case UNSPEC_NTPOFF:
6750           case UNSPEC_DTPOFF:
6751             break;
6752
6753           default:
6754             reason = "invalid address unspec";
6755             goto report_error;
6756           }
6757
6758       else if (SYMBOLIC_CONST (disp)
6759                && (flag_pic
6760                    || (TARGET_MACHO
6761 #if TARGET_MACHO
6762                        && MACHOPIC_INDIRECT
6763                        && !machopic_operand_p (disp)
6764 #endif
6765                )))
6766         {
6767
6768         is_legitimate_pic:
6769           if (TARGET_64BIT && (index || base))
6770             {
6771               /* foo@dtpoff(%rX) is ok.  */
6772               if (GET_CODE (disp) != CONST
6773                   || GET_CODE (XEXP (disp, 0)) != PLUS
6774                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6775                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
6776                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6777                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6778                 {
6779                   reason = "non-constant pic memory reference";
6780                   goto report_error;
6781                 }
6782             }
6783           else if (! legitimate_pic_address_disp_p (disp))
6784             {
6785               reason = "displacement is an invalid pic construct";
6786               goto report_error;
6787             }
6788
6789           /* This code used to verify that a symbolic pic displacement
6790              includes the pic_offset_table_rtx register.
6791
6792              While this is good idea, unfortunately these constructs may
6793              be created by "adds using lea" optimization for incorrect
6794              code like:
6795
6796              int a;
6797              int foo(int i)
6798                {
6799                  return *(&a+i);
6800                }
6801
6802              This code is nonsensical, but results in addressing
6803              GOT table with pic_offset_table_rtx base.  We can't
6804              just refuse it easily, since it gets matched by
6805              "addsi3" pattern, that later gets split to lea in the
6806              case output register differs from input.  While this
6807              can be handled by separate addsi pattern for this case
6808              that never results in lea, this seems to be easier and
6809              correct fix for crash to disable this test.  */
6810         }
6811       else if (GET_CODE (disp) != LABEL_REF
6812                && GET_CODE (disp) != CONST_INT
6813                && (GET_CODE (disp) != CONST
6814                    || !legitimate_constant_p (disp))
6815                && (GET_CODE (disp) != SYMBOL_REF
6816                    || !legitimate_constant_p (disp)))
6817         {
6818           reason = "displacement is not constant";
6819           goto report_error;
6820         }
6821       else if (TARGET_64BIT
6822                && !x86_64_immediate_operand (disp, VOIDmode))
6823         {
6824           reason = "displacement is out of range";
6825           goto report_error;
6826         }
6827     }
6828
6829   /* Everything looks valid.  */
6830   if (TARGET_DEBUG_ADDR)
6831     fprintf (stderr, "Success.\n");
6832   return TRUE;
6833
6834  report_error:
6835   if (TARGET_DEBUG_ADDR)
6836     {
6837       fprintf (stderr, "Error: %s\n", reason);
6838       debug_rtx (reason_rtx);
6839     }
6840   return FALSE;
6841 }
6842 \f
6843 /* Return a unique alias set for the GOT.  */
6844
6845 static HOST_WIDE_INT
6846 ix86_GOT_alias_set (void)
6847 {
6848   static HOST_WIDE_INT set = -1;
6849   if (set == -1)
6850     set = new_alias_set ();
6851   return set;
6852 }
6853
6854 /* Return a legitimate reference for ORIG (an address) using the
6855    register REG.  If REG is 0, a new pseudo is generated.
6856
6857    There are two types of references that must be handled:
6858
6859    1. Global data references must load the address from the GOT, via
6860       the PIC reg.  An insn is emitted to do this load, and the reg is
6861       returned.
6862
6863    2. Static data references, constant pool addresses, and code labels
6864       compute the address as an offset from the GOT, whose base is in
6865       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6866       differentiate them from global data objects.  The returned
6867       address is the PIC reg + an unspec constant.
6868
6869    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6870    reg also appears in the address.  */
6871
6872 static rtx
6873 legitimize_pic_address (rtx orig, rtx reg)
6874 {
6875   rtx addr = orig;
6876   rtx new = orig;
6877   rtx base;
6878
6879 #if TARGET_MACHO
6880   if (TARGET_MACHO && !TARGET_64BIT)
6881     {
6882       if (reg == 0)
6883         reg = gen_reg_rtx (Pmode);
6884       /* Use the generic Mach-O PIC machinery.  */
6885       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6886     }
6887 #endif
6888
6889   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6890     new = addr;
6891   else if (TARGET_64BIT
6892            && ix86_cmodel != CM_SMALL_PIC
6893            && local_symbolic_operand (addr, Pmode))
6894     {
6895       rtx tmpreg;
6896       /* This symbol may be referenced via a displacement from the PIC
6897          base address (@GOTOFF).  */
6898
6899       if (reload_in_progress)
6900         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6901       if (GET_CODE (addr) == CONST)
6902         addr = XEXP (addr, 0);
6903       if (GET_CODE (addr) == PLUS)
6904           {
6905             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6906             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6907           }
6908         else
6909           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6910       new = gen_rtx_CONST (Pmode, new);
6911       if (!reg)
6912         tmpreg = gen_reg_rtx (Pmode);
6913       else
6914         tmpreg = reg;
6915       emit_move_insn (tmpreg, new);
6916
6917       if (reg != 0)
6918         {
6919           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6920                                      tmpreg, 1, OPTAB_DIRECT);
6921           new = reg;
6922         }
6923       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6924     }
6925   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6926     {
6927       /* This symbol may be referenced via a displacement from the PIC
6928          base address (@GOTOFF).  */
6929
6930       if (reload_in_progress)
6931         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6932       if (GET_CODE (addr) == CONST)
6933         addr = XEXP (addr, 0);
6934       if (GET_CODE (addr) == PLUS)
6935           {
6936             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6937             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6938           }
6939         else
6940           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6941       new = gen_rtx_CONST (Pmode, new);
6942       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6943
6944       if (reg != 0)
6945         {
6946           emit_move_insn (reg, new);
6947           new = reg;
6948         }
6949     }
6950   else if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
6951     {
6952       if (TARGET_64BIT)
6953         {
6954           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6955           new = gen_rtx_CONST (Pmode, new);
6956           new = gen_const_mem (Pmode, new);
6957           set_mem_alias_set (new, ix86_GOT_alias_set ());
6958
6959           if (reg == 0)
6960             reg = gen_reg_rtx (Pmode);
6961           /* Use directly gen_movsi, otherwise the address is loaded
6962              into register for CSE.  We don't want to CSE this addresses,
6963              instead we CSE addresses from the GOT table, so skip this.  */
6964           emit_insn (gen_movsi (reg, new));
6965           new = reg;
6966         }
6967       else
6968         {
6969           /* This symbol must be referenced via a load from the
6970              Global Offset Table (@GOT).  */
6971
6972           if (reload_in_progress)
6973             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6974           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6975           new = gen_rtx_CONST (Pmode, new);
6976           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6977           new = gen_const_mem (Pmode, new);
6978           set_mem_alias_set (new, ix86_GOT_alias_set ());
6979
6980           if (reg == 0)
6981             reg = gen_reg_rtx (Pmode);
6982           emit_move_insn (reg, new);
6983           new = reg;
6984         }
6985     }
6986   else
6987     {
6988       if (GET_CODE (addr) == CONST_INT
6989           && !x86_64_immediate_operand (addr, VOIDmode))
6990         {
6991           if (reg)
6992             {
6993               emit_move_insn (reg, addr);
6994               new = reg;
6995             }
6996           else
6997             new = force_reg (Pmode, addr);
6998         }
6999       else if (GET_CODE (addr) == CONST)
7000         {
7001           addr = XEXP (addr, 0);
7002
7003           /* We must match stuff we generate before.  Assume the only
7004              unspecs that can get here are ours.  Not that we could do
7005              anything with them anyway....  */
7006           if (GET_CODE (addr) == UNSPEC
7007               || (GET_CODE (addr) == PLUS
7008                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
7009             return orig;
7010           gcc_assert (GET_CODE (addr) == PLUS);
7011         }
7012       if (GET_CODE (addr) == PLUS)
7013         {
7014           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
7015
7016           /* Check first to see if this is a constant offset from a @GOTOFF
7017              symbol reference.  */
7018           if (local_symbolic_operand (op0, Pmode)
7019               && GET_CODE (op1) == CONST_INT)
7020             {
7021               if (!TARGET_64BIT)
7022                 {
7023                   if (reload_in_progress)
7024                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7025                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
7026                                         UNSPEC_GOTOFF);
7027                   new = gen_rtx_PLUS (Pmode, new, op1);
7028                   new = gen_rtx_CONST (Pmode, new);
7029                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7030
7031                   if (reg != 0)
7032                     {
7033                       emit_move_insn (reg, new);
7034                       new = reg;
7035                     }
7036                 }
7037               else
7038                 {
7039                   if (INTVAL (op1) < -16*1024*1024
7040                       || INTVAL (op1) >= 16*1024*1024)
7041                     {
7042                       if (!x86_64_immediate_operand (op1, Pmode))
7043                         op1 = force_reg (Pmode, op1);
7044                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
7045                     }
7046                 }
7047             }
7048           else
7049             {
7050               base = legitimize_pic_address (XEXP (addr, 0), reg);
7051               new  = legitimize_pic_address (XEXP (addr, 1),
7052                                              base == reg ? NULL_RTX : reg);
7053
7054               if (GET_CODE (new) == CONST_INT)
7055                 new = plus_constant (base, INTVAL (new));
7056               else
7057                 {
7058                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
7059                     {
7060                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
7061                       new = XEXP (new, 1);
7062                     }
7063                   new = gen_rtx_PLUS (Pmode, base, new);
7064                 }
7065             }
7066         }
7067     }
7068   return new;
7069 }
7070 \f
7071 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
7072
7073 static rtx
7074 get_thread_pointer (int to_reg)
7075 {
7076   rtx tp, reg, insn;
7077
7078   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
7079   if (!to_reg)
7080     return tp;
7081
7082   reg = gen_reg_rtx (Pmode);
7083   insn = gen_rtx_SET (VOIDmode, reg, tp);
7084   insn = emit_insn (insn);
7085
7086   return reg;
7087 }
7088
7089 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
7090    false if we expect this to be used for a memory address and true if
7091    we expect to load the address into a register.  */
7092
7093 static rtx
7094 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
7095 {
7096   rtx dest, base, off, pic, tp;
7097   int type;
7098
7099   switch (model)
7100     {
7101     case TLS_MODEL_GLOBAL_DYNAMIC:
7102       dest = gen_reg_rtx (Pmode);
7103       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7104
7105       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7106         {
7107           rtx rax = gen_rtx_REG (Pmode, 0), insns;
7108
7109           start_sequence ();
7110           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
7111           insns = get_insns ();
7112           end_sequence ();
7113
7114           emit_libcall_block (insns, dest, rax, x);
7115         }
7116       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7117         emit_insn (gen_tls_global_dynamic_64 (dest, x));
7118       else
7119         emit_insn (gen_tls_global_dynamic_32 (dest, x));
7120
7121       if (TARGET_GNU2_TLS)
7122         {
7123           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
7124
7125           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7126         }
7127       break;
7128
7129     case TLS_MODEL_LOCAL_DYNAMIC:
7130       base = gen_reg_rtx (Pmode);
7131       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7132
7133       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7134         {
7135           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
7136
7137           start_sequence ();
7138           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
7139           insns = get_insns ();
7140           end_sequence ();
7141
7142           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
7143           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
7144           emit_libcall_block (insns, base, rax, note);
7145         }
7146       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7147         emit_insn (gen_tls_local_dynamic_base_64 (base));
7148       else
7149         emit_insn (gen_tls_local_dynamic_base_32 (base));
7150
7151       if (TARGET_GNU2_TLS)
7152         {
7153           rtx x = ix86_tls_module_base ();
7154
7155           set_unique_reg_note (get_last_insn (), REG_EQUIV,
7156                                gen_rtx_MINUS (Pmode, x, tp));
7157         }
7158
7159       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
7160       off = gen_rtx_CONST (Pmode, off);
7161
7162       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
7163
7164       if (TARGET_GNU2_TLS)
7165         {
7166           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
7167
7168           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7169         }
7170
7171       break;
7172
7173     case TLS_MODEL_INITIAL_EXEC:
7174       if (TARGET_64BIT)
7175         {
7176           pic = NULL;
7177           type = UNSPEC_GOTNTPOFF;
7178         }
7179       else if (flag_pic)
7180         {
7181           if (reload_in_progress)
7182             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7183           pic = pic_offset_table_rtx;
7184           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
7185         }
7186       else if (!TARGET_ANY_GNU_TLS)
7187         {
7188           pic = gen_reg_rtx (Pmode);
7189           emit_insn (gen_set_got (pic));
7190           type = UNSPEC_GOTTPOFF;
7191         }
7192       else
7193         {
7194           pic = NULL;
7195           type = UNSPEC_INDNTPOFF;
7196         }
7197
7198       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
7199       off = gen_rtx_CONST (Pmode, off);
7200       if (pic)
7201         off = gen_rtx_PLUS (Pmode, pic, off);
7202       off = gen_const_mem (Pmode, off);
7203       set_mem_alias_set (off, ix86_GOT_alias_set ());
7204
7205       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7206         {
7207           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7208           off = force_reg (Pmode, off);
7209           return gen_rtx_PLUS (Pmode, base, off);
7210         }
7211       else
7212         {
7213           base = get_thread_pointer (true);
7214           dest = gen_reg_rtx (Pmode);
7215           emit_insn (gen_subsi3 (dest, base, off));
7216         }
7217       break;
7218
7219     case TLS_MODEL_LOCAL_EXEC:
7220       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
7221                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7222                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
7223       off = gen_rtx_CONST (Pmode, off);
7224
7225       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7226         {
7227           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7228           return gen_rtx_PLUS (Pmode, base, off);
7229         }
7230       else
7231         {
7232           base = get_thread_pointer (true);
7233           dest = gen_reg_rtx (Pmode);
7234           emit_insn (gen_subsi3 (dest, base, off));
7235         }
7236       break;
7237
7238     default:
7239       gcc_unreachable ();
7240     }
7241
7242   return dest;
7243 }
7244
7245 /* Try machine-dependent ways of modifying an illegitimate address
7246    to be legitimate.  If we find one, return the new, valid address.
7247    This macro is used in only one place: `memory_address' in explow.c.
7248
7249    OLDX is the address as it was before break_out_memory_refs was called.
7250    In some cases it is useful to look at this to decide what needs to be done.
7251
7252    MODE and WIN are passed so that this macro can use
7253    GO_IF_LEGITIMATE_ADDRESS.
7254
7255    It is always safe for this macro to do nothing.  It exists to recognize
7256    opportunities to optimize the output.
7257
7258    For the 80386, we handle X+REG by loading X into a register R and
7259    using R+REG.  R will go in a general reg and indexing will be used.
7260    However, if REG is a broken-out memory address or multiplication,
7261    nothing needs to be done because REG can certainly go in a general reg.
7262
7263    When -fpic is used, special handling is needed for symbolic references.
7264    See comments by legitimize_pic_address in i386.c for details.  */
7265
7266 rtx
7267 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
7268 {
7269   int changed = 0;
7270   unsigned log;
7271
7272   if (TARGET_DEBUG_ADDR)
7273     {
7274       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
7275                GET_MODE_NAME (mode));
7276       debug_rtx (x);
7277     }
7278
7279   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
7280   if (log)
7281     return legitimize_tls_address (x, log, false);
7282   if (GET_CODE (x) == CONST
7283       && GET_CODE (XEXP (x, 0)) == PLUS
7284       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7285       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
7286     {
7287       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
7288       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7289     }
7290
7291   if (flag_pic && SYMBOLIC_CONST (x))
7292     return legitimize_pic_address (x, 0);
7293
7294   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
7295   if (GET_CODE (x) == ASHIFT
7296       && GET_CODE (XEXP (x, 1)) == CONST_INT
7297       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
7298     {
7299       changed = 1;
7300       log = INTVAL (XEXP (x, 1));
7301       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
7302                         GEN_INT (1 << log));
7303     }
7304
7305   if (GET_CODE (x) == PLUS)
7306     {
7307       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
7308
7309       if (GET_CODE (XEXP (x, 0)) == ASHIFT
7310           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7311           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
7312         {
7313           changed = 1;
7314           log = INTVAL (XEXP (XEXP (x, 0), 1));
7315           XEXP (x, 0) = gen_rtx_MULT (Pmode,
7316                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
7317                                       GEN_INT (1 << log));
7318         }
7319
7320       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7321           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
7322           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7323         {
7324           changed = 1;
7325           log = INTVAL (XEXP (XEXP (x, 1), 1));
7326           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7327                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7328                                       GEN_INT (1 << log));
7329         }
7330
7331       /* Put multiply first if it isn't already.  */
7332       if (GET_CODE (XEXP (x, 1)) == MULT)
7333         {
7334           rtx tmp = XEXP (x, 0);
7335           XEXP (x, 0) = XEXP (x, 1);
7336           XEXP (x, 1) = tmp;
7337           changed = 1;
7338         }
7339
7340       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7341          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7342          created by virtual register instantiation, register elimination, and
7343          similar optimizations.  */
7344       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7345         {
7346           changed = 1;
7347           x = gen_rtx_PLUS (Pmode,
7348                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7349                                           XEXP (XEXP (x, 1), 0)),
7350                             XEXP (XEXP (x, 1), 1));
7351         }
7352
7353       /* Canonicalize
7354          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7355          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7356       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7357                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7358                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7359                && CONSTANT_P (XEXP (x, 1)))
7360         {
7361           rtx constant;
7362           rtx other = NULL_RTX;
7363
7364           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7365             {
7366               constant = XEXP (x, 1);
7367               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7368             }
7369           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
7370             {
7371               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7372               other = XEXP (x, 1);
7373             }
7374           else
7375             constant = 0;
7376
7377           if (constant)
7378             {
7379               changed = 1;
7380               x = gen_rtx_PLUS (Pmode,
7381                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7382                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7383                                 plus_constant (other, INTVAL (constant)));
7384             }
7385         }
7386
7387       if (changed && legitimate_address_p (mode, x, FALSE))
7388         return x;
7389
7390       if (GET_CODE (XEXP (x, 0)) == MULT)
7391         {
7392           changed = 1;
7393           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7394         }
7395
7396       if (GET_CODE (XEXP (x, 1)) == MULT)
7397         {
7398           changed = 1;
7399           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7400         }
7401
7402       if (changed
7403           && GET_CODE (XEXP (x, 1)) == REG
7404           && GET_CODE (XEXP (x, 0)) == REG)
7405         return x;
7406
7407       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7408         {
7409           changed = 1;
7410           x = legitimize_pic_address (x, 0);
7411         }
7412
7413       if (changed && legitimate_address_p (mode, x, FALSE))
7414         return x;
7415
7416       if (GET_CODE (XEXP (x, 0)) == REG)
7417         {
7418           rtx temp = gen_reg_rtx (Pmode);
7419           rtx val  = force_operand (XEXP (x, 1), temp);
7420           if (val != temp)
7421             emit_move_insn (temp, val);
7422
7423           XEXP (x, 1) = temp;
7424           return x;
7425         }
7426
7427       else if (GET_CODE (XEXP (x, 1)) == REG)
7428         {
7429           rtx temp = gen_reg_rtx (Pmode);
7430           rtx val  = force_operand (XEXP (x, 0), temp);
7431           if (val != temp)
7432             emit_move_insn (temp, val);
7433
7434           XEXP (x, 0) = temp;
7435           return x;
7436         }
7437     }
7438
7439   return x;
7440 }
7441 \f
7442 /* Print an integer constant expression in assembler syntax.  Addition
7443    and subtraction are the only arithmetic that may appear in these
7444    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7445    CODE is the operand print code from the output string.  */
7446
7447 static void
7448 output_pic_addr_const (FILE *file, rtx x, int code)
7449 {
7450   char buf[256];
7451
7452   switch (GET_CODE (x))
7453     {
7454     case PC:
7455       gcc_assert (flag_pic);
7456       putc ('.', file);
7457       break;
7458
7459     case SYMBOL_REF:
7460       output_addr_const (file, x);
7461       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7462         fputs ("@PLT", file);
7463       break;
7464
7465     case LABEL_REF:
7466       x = XEXP (x, 0);
7467       /* FALLTHRU */
7468     case CODE_LABEL:
7469       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7470       assemble_name (asm_out_file, buf);
7471       break;
7472
7473     case CONST_INT:
7474       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7475       break;
7476
7477     case CONST:
7478       /* This used to output parentheses around the expression,
7479          but that does not work on the 386 (either ATT or BSD assembler).  */
7480       output_pic_addr_const (file, XEXP (x, 0), code);
7481       break;
7482
7483     case CONST_DOUBLE:
7484       if (GET_MODE (x) == VOIDmode)
7485         {
7486           /* We can use %d if the number is <32 bits and positive.  */
7487           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7488             fprintf (file, "0x%lx%08lx",
7489                      (unsigned long) CONST_DOUBLE_HIGH (x),
7490                      (unsigned long) CONST_DOUBLE_LOW (x));
7491           else
7492             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7493         }
7494       else
7495         /* We can't handle floating point constants;
7496            PRINT_OPERAND must handle them.  */
7497         output_operand_lossage ("floating constant misused");
7498       break;
7499
7500     case PLUS:
7501       /* Some assemblers need integer constants to appear first.  */
7502       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
7503         {
7504           output_pic_addr_const (file, XEXP (x, 0), code);
7505           putc ('+', file);
7506           output_pic_addr_const (file, XEXP (x, 1), code);
7507         }
7508       else
7509         {
7510           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
7511           output_pic_addr_const (file, XEXP (x, 1), code);
7512           putc ('+', file);
7513           output_pic_addr_const (file, XEXP (x, 0), code);
7514         }
7515       break;
7516
7517     case MINUS:
7518       if (!TARGET_MACHO)
7519         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7520       output_pic_addr_const (file, XEXP (x, 0), code);
7521       putc ('-', file);
7522       output_pic_addr_const (file, XEXP (x, 1), code);
7523       if (!TARGET_MACHO)
7524         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7525       break;
7526
7527      case UNSPEC:
7528        gcc_assert (XVECLEN (x, 0) == 1);
7529        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7530        switch (XINT (x, 1))
7531         {
7532         case UNSPEC_GOT:
7533           fputs ("@GOT", file);
7534           break;
7535         case UNSPEC_GOTOFF:
7536           fputs ("@GOTOFF", file);
7537           break;
7538         case UNSPEC_GOTPCREL:
7539           fputs ("@GOTPCREL(%rip)", file);
7540           break;
7541         case UNSPEC_GOTTPOFF:
7542           /* FIXME: This might be @TPOFF in Sun ld too.  */
7543           fputs ("@GOTTPOFF", file);
7544           break;
7545         case UNSPEC_TPOFF:
7546           fputs ("@TPOFF", file);
7547           break;
7548         case UNSPEC_NTPOFF:
7549           if (TARGET_64BIT)
7550             fputs ("@TPOFF", file);
7551           else
7552             fputs ("@NTPOFF", file);
7553           break;
7554         case UNSPEC_DTPOFF:
7555           fputs ("@DTPOFF", file);
7556           break;
7557         case UNSPEC_GOTNTPOFF:
7558           if (TARGET_64BIT)
7559             fputs ("@GOTTPOFF(%rip)", file);
7560           else
7561             fputs ("@GOTNTPOFF", file);
7562           break;
7563         case UNSPEC_INDNTPOFF:
7564           fputs ("@INDNTPOFF", file);
7565           break;
7566         default:
7567           output_operand_lossage ("invalid UNSPEC as operand");
7568           break;
7569         }
7570        break;
7571
7572     default:
7573       output_operand_lossage ("invalid expression as operand");
7574     }
7575 }
7576
7577 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7578    We need to emit DTP-relative relocations.  */
7579
7580 static void
7581 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7582 {
7583   fputs (ASM_LONG, file);
7584   output_addr_const (file, x);
7585   fputs ("@DTPOFF", file);
7586   switch (size)
7587     {
7588     case 4:
7589       break;
7590     case 8:
7591       fputs (", 0", file);
7592       break;
7593     default:
7594       gcc_unreachable ();
7595    }
7596 }
7597
7598 /* In the name of slightly smaller debug output, and to cater to
7599    general assembler lossage, recognize PIC+GOTOFF and turn it back
7600    into a direct symbol reference.
7601
7602    On Darwin, this is necessary to avoid a crash, because Darwin
7603    has a different PIC label for each routine but the DWARF debugging
7604    information is not associated with any particular routine, so it's
7605    necessary to remove references to the PIC label from RTL stored by
7606    the DWARF output code.  */
7607
7608 static rtx
7609 ix86_delegitimize_address (rtx orig_x)
7610 {
7611   rtx x = orig_x;
7612   /* reg_addend is NULL or a multiple of some register.  */
7613   rtx reg_addend = NULL_RTX;
7614   /* const_addend is NULL or a const_int.  */
7615   rtx const_addend = NULL_RTX;
7616   /* This is the result, or NULL.  */
7617   rtx result = NULL_RTX;
7618
7619   if (GET_CODE (x) == MEM)
7620     x = XEXP (x, 0);
7621
7622   if (TARGET_64BIT)
7623     {
7624       if (GET_CODE (x) != CONST
7625           || GET_CODE (XEXP (x, 0)) != UNSPEC
7626           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7627           || GET_CODE (orig_x) != MEM)
7628         return orig_x;
7629       return XVECEXP (XEXP (x, 0), 0, 0);
7630     }
7631
7632   if (GET_CODE (x) != PLUS
7633       || GET_CODE (XEXP (x, 1)) != CONST)
7634     return orig_x;
7635
7636   if (GET_CODE (XEXP (x, 0)) == REG
7637       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7638     /* %ebx + GOT/GOTOFF */
7639     ;
7640   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7641     {
7642       /* %ebx + %reg * scale + GOT/GOTOFF */
7643       reg_addend = XEXP (x, 0);
7644       if (GET_CODE (XEXP (reg_addend, 0)) == REG
7645           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7646         reg_addend = XEXP (reg_addend, 1);
7647       else if (GET_CODE (XEXP (reg_addend, 1)) == REG
7648                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7649         reg_addend = XEXP (reg_addend, 0);
7650       else
7651         return orig_x;
7652       if (GET_CODE (reg_addend) != REG
7653           && GET_CODE (reg_addend) != MULT
7654           && GET_CODE (reg_addend) != ASHIFT)
7655         return orig_x;
7656     }
7657   else
7658     return orig_x;
7659
7660   x = XEXP (XEXP (x, 1), 0);
7661   if (GET_CODE (x) == PLUS
7662       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7663     {
7664       const_addend = XEXP (x, 1);
7665       x = XEXP (x, 0);
7666     }
7667
7668   if (GET_CODE (x) == UNSPEC
7669       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
7670           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
7671     result = XVECEXP (x, 0, 0);
7672
7673   if (TARGET_MACHO && darwin_local_data_pic (x)
7674       && GET_CODE (orig_x) != MEM)
7675     result = XEXP (x, 0);
7676
7677   if (! result)
7678     return orig_x;
7679
7680   if (const_addend)
7681     result = gen_rtx_PLUS (Pmode, result, const_addend);
7682   if (reg_addend)
7683     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7684   return result;
7685 }
7686 \f
7687 static void
7688 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7689                     int fp, FILE *file)
7690 {
7691   const char *suffix;
7692
7693   if (mode == CCFPmode || mode == CCFPUmode)
7694     {
7695       enum rtx_code second_code, bypass_code;
7696       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7697       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7698       code = ix86_fp_compare_code_to_integer (code);
7699       mode = CCmode;
7700     }
7701   if (reverse)
7702     code = reverse_condition (code);
7703
7704   switch (code)
7705     {
7706     case EQ:
7707       suffix = "e";
7708       break;
7709     case NE:
7710       suffix = "ne";
7711       break;
7712     case GT:
7713       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7714       suffix = "g";
7715       break;
7716     case GTU:
7717       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7718          Those same assemblers have the same but opposite lossage on cmov.  */
7719       gcc_assert (mode == CCmode);
7720       suffix = fp ? "nbe" : "a";
7721       break;
7722     case LT:
7723       switch (mode)
7724         {
7725         case CCNOmode:
7726         case CCGOCmode:
7727           suffix = "s";
7728           break;
7729
7730         case CCmode:
7731         case CCGCmode:
7732           suffix = "l";
7733           break;
7734
7735         default:
7736           gcc_unreachable ();
7737         }
7738       break;
7739     case LTU:
7740       gcc_assert (mode == CCmode);
7741       suffix = "b";
7742       break;
7743     case GE:
7744       switch (mode)
7745         {
7746         case CCNOmode:
7747         case CCGOCmode:
7748           suffix = "ns";
7749           break;
7750
7751         case CCmode:
7752         case CCGCmode:
7753           suffix = "ge";
7754           break;
7755
7756         default:
7757           gcc_unreachable ();
7758         }
7759       break;
7760     case GEU:
7761       /* ??? As above.  */
7762       gcc_assert (mode == CCmode);
7763       suffix = fp ? "nb" : "ae";
7764       break;
7765     case LE:
7766       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
7767       suffix = "le";
7768       break;
7769     case LEU:
7770       gcc_assert (mode == CCmode);
7771       suffix = "be";
7772       break;
7773     case UNORDERED:
7774       suffix = fp ? "u" : "p";
7775       break;
7776     case ORDERED:
7777       suffix = fp ? "nu" : "np";
7778       break;
7779     default:
7780       gcc_unreachable ();
7781     }
7782   fputs (suffix, file);
7783 }
7784
7785 /* Print the name of register X to FILE based on its machine mode and number.
7786    If CODE is 'w', pretend the mode is HImode.
7787    If CODE is 'b', pretend the mode is QImode.
7788    If CODE is 'k', pretend the mode is SImode.
7789    If CODE is 'q', pretend the mode is DImode.
7790    If CODE is 'h', pretend the reg is the 'high' byte register.
7791    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7792
7793 void
7794 print_reg (rtx x, int code, FILE *file)
7795 {
7796   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
7797               && REGNO (x) != FRAME_POINTER_REGNUM
7798               && REGNO (x) != FLAGS_REG
7799               && REGNO (x) != FPSR_REG
7800               && REGNO (x) != FPCR_REG);
7801
7802   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7803     putc ('%', file);
7804
7805   if (code == 'w' || MMX_REG_P (x))
7806     code = 2;
7807   else if (code == 'b')
7808     code = 1;
7809   else if (code == 'k')
7810     code = 4;
7811   else if (code == 'q')
7812     code = 8;
7813   else if (code == 'y')
7814     code = 3;
7815   else if (code == 'h')
7816     code = 0;
7817   else
7818     code = GET_MODE_SIZE (GET_MODE (x));
7819
7820   /* Irritatingly, AMD extended registers use different naming convention
7821      from the normal registers.  */
7822   if (REX_INT_REG_P (x))
7823     {
7824       gcc_assert (TARGET_64BIT);
7825       switch (code)
7826         {
7827           case 0:
7828             error ("extended registers have no high halves");
7829             break;
7830           case 1:
7831             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7832             break;
7833           case 2:
7834             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7835             break;
7836           case 4:
7837             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7838             break;
7839           case 8:
7840             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7841             break;
7842           default:
7843             error ("unsupported operand size for extended register");
7844             break;
7845         }
7846       return;
7847     }
7848   switch (code)
7849     {
7850     case 3:
7851       if (STACK_TOP_P (x))
7852         {
7853           fputs ("st(0)", file);
7854           break;
7855         }
7856       /* FALLTHRU */
7857     case 8:
7858     case 4:
7859     case 12:
7860       if (! ANY_FP_REG_P (x))
7861         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7862       /* FALLTHRU */
7863     case 16:
7864     case 2:
7865     normal:
7866       fputs (hi_reg_name[REGNO (x)], file);
7867       break;
7868     case 1:
7869       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7870         goto normal;
7871       fputs (qi_reg_name[REGNO (x)], file);
7872       break;
7873     case 0:
7874       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7875         goto normal;
7876       fputs (qi_high_reg_name[REGNO (x)], file);
7877       break;
7878     default:
7879       gcc_unreachable ();
7880     }
7881 }
7882
7883 /* Locate some local-dynamic symbol still in use by this function
7884    so that we can print its name in some tls_local_dynamic_base
7885    pattern.  */
7886
7887 static const char *
7888 get_some_local_dynamic_name (void)
7889 {
7890   rtx insn;
7891
7892   if (cfun->machine->some_ld_name)
7893     return cfun->machine->some_ld_name;
7894
7895   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7896     if (INSN_P (insn)
7897         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7898       return cfun->machine->some_ld_name;
7899
7900   gcc_unreachable ();
7901 }
7902
7903 static int
7904 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7905 {
7906   rtx x = *px;
7907
7908   if (GET_CODE (x) == SYMBOL_REF
7909       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7910     {
7911       cfun->machine->some_ld_name = XSTR (x, 0);
7912       return 1;
7913     }
7914
7915   return 0;
7916 }
7917
7918 /* Meaning of CODE:
7919    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7920    C -- print opcode suffix for set/cmov insn.
7921    c -- like C, but print reversed condition
7922    F,f -- likewise, but for floating-point.
7923    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7924         otherwise nothing
7925    R -- print the prefix for register names.
7926    z -- print the opcode suffix for the size of the current operand.
7927    * -- print a star (in certain assembler syntax)
7928    A -- print an absolute memory reference.
7929    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7930    s -- print a shift double count, followed by the assemblers argument
7931         delimiter.
7932    b -- print the QImode name of the register for the indicated operand.
7933         %b0 would print %al if operands[0] is reg 0.
7934    w --  likewise, print the HImode name of the register.
7935    k --  likewise, print the SImode name of the register.
7936    q --  likewise, print the DImode name of the register.
7937    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7938    y -- print "st(0)" instead of "st" as a register.
7939    D -- print condition for SSE cmp instruction.
7940    P -- if PIC, print an @PLT suffix.
7941    X -- don't print any sort of PIC '@' suffix for a symbol.
7942    & -- print some in-use local-dynamic symbol name.
7943    H -- print a memory address offset by 8; used for sse high-parts
7944  */
7945
7946 void
7947 print_operand (FILE *file, rtx x, int code)
7948 {
7949   if (code)
7950     {
7951       switch (code)
7952         {
7953         case '*':
7954           if (ASSEMBLER_DIALECT == ASM_ATT)
7955             putc ('*', file);
7956           return;
7957
7958         case '&':
7959           assemble_name (file, get_some_local_dynamic_name ());
7960           return;
7961
7962         case 'A':
7963           switch (ASSEMBLER_DIALECT)
7964             {
7965             case ASM_ATT:
7966               putc ('*', file);
7967               break;
7968
7969             case ASM_INTEL:
7970               /* Intel syntax. For absolute addresses, registers should not
7971                  be surrounded by braces.  */
7972               if (GET_CODE (x) != REG)
7973                 {
7974                   putc ('[', file);
7975                   PRINT_OPERAND (file, x, 0);
7976                   putc (']', file);
7977                   return;
7978                 }
7979               break;
7980
7981             default:
7982               gcc_unreachable ();
7983             }
7984
7985           PRINT_OPERAND (file, x, 0);
7986           return;
7987
7988
7989         case 'L':
7990           if (ASSEMBLER_DIALECT == ASM_ATT)
7991             putc ('l', file);
7992           return;
7993
7994         case 'W':
7995           if (ASSEMBLER_DIALECT == ASM_ATT)
7996             putc ('w', file);
7997           return;
7998
7999         case 'B':
8000           if (ASSEMBLER_DIALECT == ASM_ATT)
8001             putc ('b', file);
8002           return;
8003
8004         case 'Q':
8005           if (ASSEMBLER_DIALECT == ASM_ATT)
8006             putc ('l', file);
8007           return;
8008
8009         case 'S':
8010           if (ASSEMBLER_DIALECT == ASM_ATT)
8011             putc ('s', file);
8012           return;
8013
8014         case 'T':
8015           if (ASSEMBLER_DIALECT == ASM_ATT)
8016             putc ('t', file);
8017           return;
8018
8019         case 'z':
8020           /* 387 opcodes don't get size suffixes if the operands are
8021              registers.  */
8022           if (STACK_REG_P (x))
8023             return;
8024
8025           /* Likewise if using Intel opcodes.  */
8026           if (ASSEMBLER_DIALECT == ASM_INTEL)
8027             return;
8028
8029           /* This is the size of op from size of operand.  */
8030           switch (GET_MODE_SIZE (GET_MODE (x)))
8031             {
8032             case 2:
8033 #ifdef HAVE_GAS_FILDS_FISTS
8034               putc ('s', file);
8035 #endif
8036               return;
8037
8038             case 4:
8039               if (GET_MODE (x) == SFmode)
8040                 {
8041                   putc ('s', file);
8042                   return;
8043                 }
8044               else
8045                 putc ('l', file);
8046               return;
8047
8048             case 12:
8049             case 16:
8050               putc ('t', file);
8051               return;
8052
8053             case 8:
8054               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
8055                 {
8056 #ifdef GAS_MNEMONICS
8057                   putc ('q', file);
8058 #else
8059                   putc ('l', file);
8060                   putc ('l', file);
8061 #endif
8062                 }
8063               else
8064                 putc ('l', file);
8065               return;
8066
8067             default:
8068               gcc_unreachable ();
8069             }
8070
8071         case 'b':
8072         case 'w':
8073         case 'k':
8074         case 'q':
8075         case 'h':
8076         case 'y':
8077         case 'X':
8078         case 'P':
8079           break;
8080
8081         case 's':
8082           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
8083             {
8084               PRINT_OPERAND (file, x, 0);
8085               putc (',', file);
8086             }
8087           return;
8088
8089         case 'D':
8090           /* Little bit of braindamage here.  The SSE compare instructions
8091              does use completely different names for the comparisons that the
8092              fp conditional moves.  */
8093           switch (GET_CODE (x))
8094             {
8095             case EQ:
8096             case UNEQ:
8097               fputs ("eq", file);
8098               break;
8099             case LT:
8100             case UNLT:
8101               fputs ("lt", file);
8102               break;
8103             case LE:
8104             case UNLE:
8105               fputs ("le", file);
8106               break;
8107             case UNORDERED:
8108               fputs ("unord", file);
8109               break;
8110             case NE:
8111             case LTGT:
8112               fputs ("neq", file);
8113               break;
8114             case UNGE:
8115             case GE:
8116               fputs ("nlt", file);
8117               break;
8118             case UNGT:
8119             case GT:
8120               fputs ("nle", file);
8121               break;
8122             case ORDERED:
8123               fputs ("ord", file);
8124               break;
8125             default:
8126               gcc_unreachable ();
8127             }
8128           return;
8129         case 'O':
8130 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8131           if (ASSEMBLER_DIALECT == ASM_ATT)
8132             {
8133               switch (GET_MODE (x))
8134                 {
8135                 case HImode: putc ('w', file); break;
8136                 case SImode:
8137                 case SFmode: putc ('l', file); break;
8138                 case DImode:
8139                 case DFmode: putc ('q', file); break;
8140                 default: gcc_unreachable ();
8141                 }
8142               putc ('.', file);
8143             }
8144 #endif
8145           return;
8146         case 'C':
8147           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
8148           return;
8149         case 'F':
8150 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8151           if (ASSEMBLER_DIALECT == ASM_ATT)
8152             putc ('.', file);
8153 #endif
8154           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
8155           return;
8156
8157           /* Like above, but reverse condition */
8158         case 'c':
8159           /* Check to see if argument to %c is really a constant
8160              and not a condition code which needs to be reversed.  */
8161           if (!COMPARISON_P (x))
8162           {
8163             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
8164              return;
8165           }
8166           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
8167           return;
8168         case 'f':
8169 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8170           if (ASSEMBLER_DIALECT == ASM_ATT)
8171             putc ('.', file);
8172 #endif
8173           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
8174           return;
8175
8176         case 'H':
8177           /* It doesn't actually matter what mode we use here, as we're
8178              only going to use this for printing.  */
8179           x = adjust_address_nv (x, DImode, 8);
8180           break;
8181
8182         case '+':
8183           {
8184             rtx x;
8185
8186             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
8187               return;
8188
8189             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
8190             if (x)
8191               {
8192                 int pred_val = INTVAL (XEXP (x, 0));
8193
8194                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
8195                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
8196                   {
8197                     int taken = pred_val > REG_BR_PROB_BASE / 2;
8198                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
8199
8200                     /* Emit hints only in the case default branch prediction
8201                        heuristics would fail.  */
8202                     if (taken != cputaken)
8203                       {
8204                         /* We use 3e (DS) prefix for taken branches and
8205                            2e (CS) prefix for not taken branches.  */
8206                         if (taken)
8207                           fputs ("ds ; ", file);
8208                         else
8209                           fputs ("cs ; ", file);
8210                       }
8211                   }
8212               }
8213             return;
8214           }
8215         default:
8216             output_operand_lossage ("invalid operand code '%c'", code);
8217         }
8218     }
8219
8220   if (GET_CODE (x) == REG)
8221     print_reg (x, code, file);
8222
8223   else if (GET_CODE (x) == MEM)
8224     {
8225       /* No `byte ptr' prefix for call instructions.  */
8226       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
8227         {
8228           const char * size;
8229           switch (GET_MODE_SIZE (GET_MODE (x)))
8230             {
8231             case 1: size = "BYTE"; break;
8232             case 2: size = "WORD"; break;
8233             case 4: size = "DWORD"; break;
8234             case 8: size = "QWORD"; break;
8235             case 12: size = "XWORD"; break;
8236             case 16: size = "XMMWORD"; break;
8237             default:
8238               gcc_unreachable ();
8239             }
8240
8241           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
8242           if (code == 'b')
8243             size = "BYTE";
8244           else if (code == 'w')
8245             size = "WORD";
8246           else if (code == 'k')
8247             size = "DWORD";
8248
8249           fputs (size, file);
8250           fputs (" PTR ", file);
8251         }
8252
8253       x = XEXP (x, 0);
8254       /* Avoid (%rip) for call operands.  */
8255       if (CONSTANT_ADDRESS_P (x) && code == 'P'
8256                && GET_CODE (x) != CONST_INT)
8257         output_addr_const (file, x);
8258       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
8259         output_operand_lossage ("invalid constraints for operand");
8260       else
8261         output_address (x);
8262     }
8263
8264   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
8265     {
8266       REAL_VALUE_TYPE r;
8267       long l;
8268
8269       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8270       REAL_VALUE_TO_TARGET_SINGLE (r, l);
8271
8272       if (ASSEMBLER_DIALECT == ASM_ATT)
8273         putc ('$', file);
8274       fprintf (file, "0x%08lx", l);
8275     }
8276
8277   /* These float cases don't actually occur as immediate operands.  */
8278   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
8279     {
8280       char dstr[30];
8281
8282       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8283       fprintf (file, "%s", dstr);
8284     }
8285
8286   else if (GET_CODE (x) == CONST_DOUBLE
8287            && GET_MODE (x) == XFmode)
8288     {
8289       char dstr[30];
8290
8291       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8292       fprintf (file, "%s", dstr);
8293     }
8294
8295   else
8296     {
8297       /* We have patterns that allow zero sets of memory, for instance.
8298          In 64-bit mode, we should probably support all 8-byte vectors,
8299          since we can in fact encode that into an immediate.  */
8300       if (GET_CODE (x) == CONST_VECTOR)
8301         {
8302           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
8303           x = const0_rtx;
8304         }
8305
8306       if (code != 'P')
8307         {
8308           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8309             {
8310               if (ASSEMBLER_DIALECT == ASM_ATT)
8311                 putc ('$', file);
8312             }
8313           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
8314                    || GET_CODE (x) == LABEL_REF)
8315             {
8316               if (ASSEMBLER_DIALECT == ASM_ATT)
8317                 putc ('$', file);
8318               else
8319                 fputs ("OFFSET FLAT:", file);
8320             }
8321         }
8322       if (GET_CODE (x) == CONST_INT)
8323         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8324       else if (flag_pic)
8325         output_pic_addr_const (file, x, code);
8326       else
8327         output_addr_const (file, x);
8328     }
8329 }
8330 \f
8331 /* Print a memory operand whose address is ADDR.  */
8332
8333 void
8334 print_operand_address (FILE *file, rtx addr)
8335 {
8336   struct ix86_address parts;
8337   rtx base, index, disp;
8338   int scale;
8339   int ok = ix86_decompose_address (addr, &parts);
8340
8341   gcc_assert (ok);
8342
8343   base = parts.base;
8344   index = parts.index;
8345   disp = parts.disp;
8346   scale = parts.scale;
8347
8348   switch (parts.seg)
8349     {
8350     case SEG_DEFAULT:
8351       break;
8352     case SEG_FS:
8353     case SEG_GS:
8354       if (USER_LABEL_PREFIX[0] == 0)
8355         putc ('%', file);
8356       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8357       break;
8358     default:
8359       gcc_unreachable ();
8360     }
8361
8362   if (!base && !index)
8363     {
8364       /* Displacement only requires special attention.  */
8365
8366       if (GET_CODE (disp) == CONST_INT)
8367         {
8368           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8369             {
8370               if (USER_LABEL_PREFIX[0] == 0)
8371                 putc ('%', file);
8372               fputs ("ds:", file);
8373             }
8374           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8375         }
8376       else if (flag_pic)
8377         output_pic_addr_const (file, disp, 0);
8378       else
8379         output_addr_const (file, disp);
8380
8381       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8382       if (TARGET_64BIT)
8383         {
8384           if (GET_CODE (disp) == CONST
8385               && GET_CODE (XEXP (disp, 0)) == PLUS
8386               && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8387             disp = XEXP (XEXP (disp, 0), 0);
8388           if (GET_CODE (disp) == LABEL_REF
8389               || (GET_CODE (disp) == SYMBOL_REF
8390                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8391             fputs ("(%rip)", file);
8392         }
8393     }
8394   else
8395     {
8396       if (ASSEMBLER_DIALECT == ASM_ATT)
8397         {
8398           if (disp)
8399             {
8400               if (flag_pic)
8401                 output_pic_addr_const (file, disp, 0);
8402               else if (GET_CODE (disp) == LABEL_REF)
8403                 output_asm_label (disp);
8404               else
8405                 output_addr_const (file, disp);
8406             }
8407
8408           putc ('(', file);
8409           if (base)
8410             print_reg (base, 0, file);
8411           if (index)
8412             {
8413               putc (',', file);
8414               print_reg (index, 0, file);
8415               if (scale != 1)
8416                 fprintf (file, ",%d", scale);
8417             }
8418           putc (')', file);
8419         }
8420       else
8421         {
8422           rtx offset = NULL_RTX;
8423
8424           if (disp)
8425             {
8426               /* Pull out the offset of a symbol; print any symbol itself.  */
8427               if (GET_CODE (disp) == CONST
8428                   && GET_CODE (XEXP (disp, 0)) == PLUS
8429                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8430                 {
8431                   offset = XEXP (XEXP (disp, 0), 1);
8432                   disp = gen_rtx_CONST (VOIDmode,
8433                                         XEXP (XEXP (disp, 0), 0));
8434                 }
8435
8436               if (flag_pic)
8437                 output_pic_addr_const (file, disp, 0);
8438               else if (GET_CODE (disp) == LABEL_REF)
8439                 output_asm_label (disp);
8440               else if (GET_CODE (disp) == CONST_INT)
8441                 offset = disp;
8442               else
8443                 output_addr_const (file, disp);
8444             }
8445
8446           putc ('[', file);
8447           if (base)
8448             {
8449               print_reg (base, 0, file);
8450               if (offset)
8451                 {
8452                   if (INTVAL (offset) >= 0)
8453                     putc ('+', file);
8454                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8455                 }
8456             }
8457           else if (offset)
8458             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8459           else
8460             putc ('0', file);
8461
8462           if (index)
8463             {
8464               putc ('+', file);
8465               print_reg (index, 0, file);
8466               if (scale != 1)
8467                 fprintf (file, "*%d", scale);
8468             }
8469           putc (']', file);
8470         }
8471     }
8472 }
8473
8474 bool
8475 output_addr_const_extra (FILE *file, rtx x)
8476 {
8477   rtx op;
8478
8479   if (GET_CODE (x) != UNSPEC)
8480     return false;
8481
8482   op = XVECEXP (x, 0, 0);
8483   switch (XINT (x, 1))
8484     {
8485     case UNSPEC_GOTTPOFF:
8486       output_addr_const (file, op);
8487       /* FIXME: This might be @TPOFF in Sun ld.  */
8488       fputs ("@GOTTPOFF", file);
8489       break;
8490     case UNSPEC_TPOFF:
8491       output_addr_const (file, op);
8492       fputs ("@TPOFF", file);
8493       break;
8494     case UNSPEC_NTPOFF:
8495       output_addr_const (file, op);
8496       if (TARGET_64BIT)
8497         fputs ("@TPOFF", file);
8498       else
8499         fputs ("@NTPOFF", file);
8500       break;
8501     case UNSPEC_DTPOFF:
8502       output_addr_const (file, op);
8503       fputs ("@DTPOFF", file);
8504       break;
8505     case UNSPEC_GOTNTPOFF:
8506       output_addr_const (file, op);
8507       if (TARGET_64BIT)
8508         fputs ("@GOTTPOFF(%rip)", file);
8509       else
8510         fputs ("@GOTNTPOFF", file);
8511       break;
8512     case UNSPEC_INDNTPOFF:
8513       output_addr_const (file, op);
8514       fputs ("@INDNTPOFF", file);
8515       break;
8516
8517     default:
8518       return false;
8519     }
8520
8521   return true;
8522 }
8523 \f
8524 /* Split one or more DImode RTL references into pairs of SImode
8525    references.  The RTL can be REG, offsettable MEM, integer constant, or
8526    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8527    split and "num" is its length.  lo_half and hi_half are output arrays
8528    that parallel "operands".  */
8529
8530 void
8531 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8532 {
8533   while (num--)
8534     {
8535       rtx op = operands[num];
8536
8537       /* simplify_subreg refuse to split volatile memory addresses,
8538          but we still have to handle it.  */
8539       if (GET_CODE (op) == MEM)
8540         {
8541           lo_half[num] = adjust_address (op, SImode, 0);
8542           hi_half[num] = adjust_address (op, SImode, 4);
8543         }
8544       else
8545         {
8546           lo_half[num] = simplify_gen_subreg (SImode, op,
8547                                               GET_MODE (op) == VOIDmode
8548                                               ? DImode : GET_MODE (op), 0);
8549           hi_half[num] = simplify_gen_subreg (SImode, op,
8550                                               GET_MODE (op) == VOIDmode
8551                                               ? DImode : GET_MODE (op), 4);
8552         }
8553     }
8554 }
8555 /* Split one or more TImode RTL references into pairs of DImode
8556    references.  The RTL can be REG, offsettable MEM, integer constant, or
8557    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8558    split and "num" is its length.  lo_half and hi_half are output arrays
8559    that parallel "operands".  */
8560
8561 void
8562 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8563 {
8564   while (num--)
8565     {
8566       rtx op = operands[num];
8567
8568       /* simplify_subreg refuse to split volatile memory addresses, but we
8569          still have to handle it.  */
8570       if (GET_CODE (op) == MEM)
8571         {
8572           lo_half[num] = adjust_address (op, DImode, 0);
8573           hi_half[num] = adjust_address (op, DImode, 8);
8574         }
8575       else
8576         {
8577           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8578           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8579         }
8580     }
8581 }
8582 \f
8583 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8584    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8585    is the expression of the binary operation.  The output may either be
8586    emitted here, or returned to the caller, like all output_* functions.
8587
8588    There is no guarantee that the operands are the same mode, as they
8589    might be within FLOAT or FLOAT_EXTEND expressions.  */
8590
8591 #ifndef SYSV386_COMPAT
8592 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8593    wants to fix the assemblers because that causes incompatibility
8594    with gcc.  No-one wants to fix gcc because that causes
8595    incompatibility with assemblers...  You can use the option of
8596    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8597 #define SYSV386_COMPAT 1
8598 #endif
8599
8600 const char *
8601 output_387_binary_op (rtx insn, rtx *operands)
8602 {
8603   static char buf[30];
8604   const char *p;
8605   const char *ssep;
8606   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8607
8608 #ifdef ENABLE_CHECKING
8609   /* Even if we do not want to check the inputs, this documents input
8610      constraints.  Which helps in understanding the following code.  */
8611   if (STACK_REG_P (operands[0])
8612       && ((REG_P (operands[1])
8613            && REGNO (operands[0]) == REGNO (operands[1])
8614            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
8615           || (REG_P (operands[2])
8616               && REGNO (operands[0]) == REGNO (operands[2])
8617               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
8618       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8619     ; /* ok */
8620   else
8621     gcc_assert (is_sse);
8622 #endif
8623
8624   switch (GET_CODE (operands[3]))
8625     {
8626     case PLUS:
8627       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8628           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8629         p = "fiadd";
8630       else
8631         p = "fadd";
8632       ssep = "add";
8633       break;
8634
8635     case MINUS:
8636       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8637           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8638         p = "fisub";
8639       else
8640         p = "fsub";
8641       ssep = "sub";
8642       break;
8643
8644     case MULT:
8645       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8646           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8647         p = "fimul";
8648       else
8649         p = "fmul";
8650       ssep = "mul";
8651       break;
8652
8653     case DIV:
8654       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8655           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8656         p = "fidiv";
8657       else
8658         p = "fdiv";
8659       ssep = "div";
8660       break;
8661
8662     default:
8663       gcc_unreachable ();
8664     }
8665
8666   if (is_sse)
8667    {
8668       strcpy (buf, ssep);
8669       if (GET_MODE (operands[0]) == SFmode)
8670         strcat (buf, "ss\t{%2, %0|%0, %2}");
8671       else
8672         strcat (buf, "sd\t{%2, %0|%0, %2}");
8673       return buf;
8674    }
8675   strcpy (buf, p);
8676
8677   switch (GET_CODE (operands[3]))
8678     {
8679     case MULT:
8680     case PLUS:
8681       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8682         {
8683           rtx temp = operands[2];
8684           operands[2] = operands[1];
8685           operands[1] = temp;
8686         }
8687
8688       /* know operands[0] == operands[1].  */
8689
8690       if (GET_CODE (operands[2]) == MEM)
8691         {
8692           p = "%z2\t%2";
8693           break;
8694         }
8695
8696       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8697         {
8698           if (STACK_TOP_P (operands[0]))
8699             /* How is it that we are storing to a dead operand[2]?
8700                Well, presumably operands[1] is dead too.  We can't
8701                store the result to st(0) as st(0) gets popped on this
8702                instruction.  Instead store to operands[2] (which I
8703                think has to be st(1)).  st(1) will be popped later.
8704                gcc <= 2.8.1 didn't have this check and generated
8705                assembly code that the Unixware assembler rejected.  */
8706             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8707           else
8708             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8709           break;
8710         }
8711
8712       if (STACK_TOP_P (operands[0]))
8713         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8714       else
8715         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8716       break;
8717
8718     case MINUS:
8719     case DIV:
8720       if (GET_CODE (operands[1]) == MEM)
8721         {
8722           p = "r%z1\t%1";
8723           break;
8724         }
8725
8726       if (GET_CODE (operands[2]) == MEM)
8727         {
8728           p = "%z2\t%2";
8729           break;
8730         }
8731
8732       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8733         {
8734 #if SYSV386_COMPAT
8735           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
8736              derived assemblers, confusingly reverse the direction of
8737              the operation for fsub{r} and fdiv{r} when the
8738              destination register is not st(0).  The Intel assembler
8739              doesn't have this brain damage.  Read !SYSV386_COMPAT to
8740              figure out what the hardware really does.  */
8741           if (STACK_TOP_P (operands[0]))
8742             p = "{p\t%0, %2|rp\t%2, %0}";
8743           else
8744             p = "{rp\t%2, %0|p\t%0, %2}";
8745 #else
8746           if (STACK_TOP_P (operands[0]))
8747             /* As above for fmul/fadd, we can't store to st(0).  */
8748             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
8749           else
8750             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8751 #endif
8752           break;
8753         }
8754
8755       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
8756         {
8757 #if SYSV386_COMPAT
8758           if (STACK_TOP_P (operands[0]))
8759             p = "{rp\t%0, %1|p\t%1, %0}";
8760           else
8761             p = "{p\t%1, %0|rp\t%0, %1}";
8762 #else
8763           if (STACK_TOP_P (operands[0]))
8764             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
8765           else
8766             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
8767 #endif
8768           break;
8769         }
8770
8771       if (STACK_TOP_P (operands[0]))
8772         {
8773           if (STACK_TOP_P (operands[1]))
8774             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
8775           else
8776             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
8777           break;
8778         }
8779       else if (STACK_TOP_P (operands[1]))
8780         {
8781 #if SYSV386_COMPAT
8782           p = "{\t%1, %0|r\t%0, %1}";
8783 #else
8784           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8785 #endif
8786         }
8787       else
8788         {
8789 #if SYSV386_COMPAT
8790           p = "{r\t%2, %0|\t%0, %2}";
8791 #else
8792           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8793 #endif
8794         }
8795       break;
8796
8797     default:
8798       gcc_unreachable ();
8799     }
8800
8801   strcat (buf, p);
8802   return buf;
8803 }
8804
8805 /* Return needed mode for entity in optimize_mode_switching pass.  */
8806
8807 int
8808 ix86_mode_needed (int entity, rtx insn)
8809 {
8810   enum attr_i387_cw mode;
8811
8812   /* The mode UNINITIALIZED is used to store control word after a
8813      function call or ASM pattern.  The mode ANY specify that function
8814      has no requirements on the control word and make no changes in the
8815      bits we are interested in.  */
8816
8817   if (CALL_P (insn)
8818       || (NONJUMP_INSN_P (insn)
8819           && (asm_noperands (PATTERN (insn)) >= 0
8820               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
8821     return I387_CW_UNINITIALIZED;
8822
8823   if (recog_memoized (insn) < 0)
8824     return I387_CW_ANY;
8825
8826   mode = get_attr_i387_cw (insn);
8827
8828   switch (entity)
8829     {
8830     case I387_TRUNC:
8831       if (mode == I387_CW_TRUNC)
8832         return mode;
8833       break;
8834
8835     case I387_FLOOR:
8836       if (mode == I387_CW_FLOOR)
8837         return mode;
8838       break;
8839
8840     case I387_CEIL:
8841       if (mode == I387_CW_CEIL)
8842         return mode;
8843       break;
8844
8845     case I387_MASK_PM:
8846       if (mode == I387_CW_MASK_PM)
8847         return mode;
8848       break;
8849
8850     default:
8851       gcc_unreachable ();
8852     }
8853
8854   return I387_CW_ANY;
8855 }
8856
8857 /* Output code to initialize control word copies used by trunc?f?i and
8858    rounding patterns.  CURRENT_MODE is set to current control word,
8859    while NEW_MODE is set to new control word.  */
8860
8861 void
8862 emit_i387_cw_initialization (int mode)
8863 {
8864   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
8865   rtx new_mode;
8866
8867   int slot;
8868
8869   rtx reg = gen_reg_rtx (HImode);
8870
8871   emit_insn (gen_x86_fnstcw_1 (stored_mode));
8872   emit_move_insn (reg, copy_rtx (stored_mode));
8873
8874   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
8875     {
8876       switch (mode)
8877         {
8878         case I387_CW_TRUNC:
8879           /* round toward zero (truncate) */
8880           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
8881           slot = SLOT_CW_TRUNC;
8882           break;
8883
8884         case I387_CW_FLOOR:
8885           /* round down toward -oo */
8886           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8887           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
8888           slot = SLOT_CW_FLOOR;
8889           break;
8890
8891         case I387_CW_CEIL:
8892           /* round up toward +oo */
8893           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8894           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
8895           slot = SLOT_CW_CEIL;
8896           break;
8897
8898         case I387_CW_MASK_PM:
8899           /* mask precision exception for nearbyint() */
8900           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8901           slot = SLOT_CW_MASK_PM;
8902           break;
8903
8904         default:
8905           gcc_unreachable ();
8906         }
8907     }
8908   else
8909     {
8910       switch (mode)
8911         {
8912         case I387_CW_TRUNC:
8913           /* round toward zero (truncate) */
8914           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8915           slot = SLOT_CW_TRUNC;
8916           break;
8917
8918         case I387_CW_FLOOR:
8919           /* round down toward -oo */
8920           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8921           slot = SLOT_CW_FLOOR;
8922           break;
8923
8924         case I387_CW_CEIL:
8925           /* round up toward +oo */
8926           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8927           slot = SLOT_CW_CEIL;
8928           break;
8929
8930         case I387_CW_MASK_PM:
8931           /* mask precision exception for nearbyint() */
8932           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8933           slot = SLOT_CW_MASK_PM;
8934           break;
8935
8936         default:
8937           gcc_unreachable ();
8938         }
8939     }
8940
8941   gcc_assert (slot < MAX_386_STACK_LOCALS);
8942
8943   new_mode = assign_386_stack_local (HImode, slot);
8944   emit_move_insn (new_mode, reg);
8945 }
8946
8947 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8948    are the insn operands.  The output may be [HSD]Imode and the input
8949    operand may be [SDX]Fmode.  */
8950
8951 const char *
8952 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8953 {
8954   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8955   int dimode_p = GET_MODE (operands[0]) == DImode;
8956   int round_mode = get_attr_i387_cw (insn);
8957
8958   /* Jump through a hoop or two for DImode, since the hardware has no
8959      non-popping instruction.  We used to do this a different way, but
8960      that was somewhat fragile and broke with post-reload splitters.  */
8961   if ((dimode_p || fisttp) && !stack_top_dies)
8962     output_asm_insn ("fld\t%y1", operands);
8963
8964   gcc_assert (STACK_TOP_P (operands[1]));
8965   gcc_assert (GET_CODE (operands[0]) == MEM);
8966
8967   if (fisttp)
8968       output_asm_insn ("fisttp%z0\t%0", operands);
8969   else
8970     {
8971       if (round_mode != I387_CW_ANY)
8972         output_asm_insn ("fldcw\t%3", operands);
8973       if (stack_top_dies || dimode_p)
8974         output_asm_insn ("fistp%z0\t%0", operands);
8975       else
8976         output_asm_insn ("fist%z0\t%0", operands);
8977       if (round_mode != I387_CW_ANY)
8978         output_asm_insn ("fldcw\t%2", operands);
8979     }
8980
8981   return "";
8982 }
8983
8984 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
8985    have the values zero or one, indicates the ffreep insn's operand
8986    from the OPERANDS array.  */
8987
8988 static const char *
8989 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
8990 {
8991   if (TARGET_USE_FFREEP)
8992 #if HAVE_AS_IX86_FFREEP
8993     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
8994 #else
8995     {
8996       static char retval[] = ".word\t0xc_df";
8997       int regno = REGNO (operands[opno]);
8998       
8999       gcc_assert (FP_REGNO_P (regno));
9000
9001       retval[9] = '0' + (regno - FIRST_STACK_REG);
9002       return retval;
9003     }
9004 #endif
9005
9006   return opno ? "fstp\t%y1" : "fstp\t%y0";
9007 }
9008
9009
9010 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
9011    should be used.  UNORDERED_P is true when fucom should be used.  */
9012
9013 const char *
9014 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
9015 {
9016   int stack_top_dies;
9017   rtx cmp_op0, cmp_op1;
9018   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
9019
9020   if (eflags_p)
9021     {
9022       cmp_op0 = operands[0];
9023       cmp_op1 = operands[1];
9024     }
9025   else
9026     {
9027       cmp_op0 = operands[1];
9028       cmp_op1 = operands[2];
9029     }
9030
9031   if (is_sse)
9032     {
9033       if (GET_MODE (operands[0]) == SFmode)
9034         if (unordered_p)
9035           return "ucomiss\t{%1, %0|%0, %1}";
9036         else
9037           return "comiss\t{%1, %0|%0, %1}";
9038       else
9039         if (unordered_p)
9040           return "ucomisd\t{%1, %0|%0, %1}";
9041         else
9042           return "comisd\t{%1, %0|%0, %1}";
9043     }
9044
9045   gcc_assert (STACK_TOP_P (cmp_op0));
9046
9047   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9048
9049   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
9050     {
9051       if (stack_top_dies)
9052         {
9053           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
9054           return output_387_ffreep (operands, 1);
9055         }
9056       else
9057         return "ftst\n\tfnstsw\t%0";
9058     }
9059
9060   if (STACK_REG_P (cmp_op1)
9061       && stack_top_dies
9062       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
9063       && REGNO (cmp_op1) != FIRST_STACK_REG)
9064     {
9065       /* If both the top of the 387 stack dies, and the other operand
9066          is also a stack register that dies, then this must be a
9067          `fcompp' float compare */
9068
9069       if (eflags_p)
9070         {
9071           /* There is no double popping fcomi variant.  Fortunately,
9072              eflags is immune from the fstp's cc clobbering.  */
9073           if (unordered_p)
9074             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
9075           else
9076             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
9077           return output_387_ffreep (operands, 0);
9078         }
9079       else
9080         {
9081           if (unordered_p)
9082             return "fucompp\n\tfnstsw\t%0";
9083           else
9084             return "fcompp\n\tfnstsw\t%0";
9085         }
9086     }
9087   else
9088     {
9089       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
9090
9091       static const char * const alt[16] =
9092       {
9093         "fcom%z2\t%y2\n\tfnstsw\t%0",
9094         "fcomp%z2\t%y2\n\tfnstsw\t%0",
9095         "fucom%z2\t%y2\n\tfnstsw\t%0",
9096         "fucomp%z2\t%y2\n\tfnstsw\t%0",
9097
9098         "ficom%z2\t%y2\n\tfnstsw\t%0",
9099         "ficomp%z2\t%y2\n\tfnstsw\t%0",
9100         NULL,
9101         NULL,
9102
9103         "fcomi\t{%y1, %0|%0, %y1}",
9104         "fcomip\t{%y1, %0|%0, %y1}",
9105         "fucomi\t{%y1, %0|%0, %y1}",
9106         "fucomip\t{%y1, %0|%0, %y1}",
9107
9108         NULL,
9109         NULL,
9110         NULL,
9111         NULL
9112       };
9113
9114       int mask;
9115       const char *ret;
9116
9117       mask  = eflags_p << 3;
9118       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
9119       mask |= unordered_p << 1;
9120       mask |= stack_top_dies;
9121
9122       gcc_assert (mask < 16);
9123       ret = alt[mask];
9124       gcc_assert (ret);
9125
9126       return ret;
9127     }
9128 }
9129
9130 void
9131 ix86_output_addr_vec_elt (FILE *file, int value)
9132 {
9133   const char *directive = ASM_LONG;
9134
9135 #ifdef ASM_QUAD
9136   if (TARGET_64BIT)
9137     directive = ASM_QUAD;
9138 #else
9139   gcc_assert (!TARGET_64BIT);
9140 #endif
9141
9142   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
9143 }
9144
9145 void
9146 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
9147 {
9148   if (TARGET_64BIT)
9149     fprintf (file, "%s%s%d-%s%d\n",
9150              ASM_LONG, LPREFIX, value, LPREFIX, rel);
9151   else if (HAVE_AS_GOTOFF_IN_DATA)
9152     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
9153 #if TARGET_MACHO
9154   else if (TARGET_MACHO)
9155     {
9156       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
9157       machopic_output_function_base_name (file);
9158       fprintf(file, "\n");
9159     }
9160 #endif
9161   else
9162     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
9163                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
9164 }
9165 \f
9166 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
9167    for the target.  */
9168
9169 void
9170 ix86_expand_clear (rtx dest)
9171 {
9172   rtx tmp;
9173
9174   /* We play register width games, which are only valid after reload.  */
9175   gcc_assert (reload_completed);
9176
9177   /* Avoid HImode and its attendant prefix byte.  */
9178   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
9179     dest = gen_rtx_REG (SImode, REGNO (dest));
9180
9181   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
9182
9183   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
9184   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
9185     {
9186       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
9187       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
9188     }
9189
9190   emit_insn (tmp);
9191 }
9192
9193 /* X is an unchanging MEM.  If it is a constant pool reference, return
9194    the constant pool rtx, else NULL.  */
9195
9196 rtx
9197 maybe_get_pool_constant (rtx x)
9198 {
9199   x = ix86_delegitimize_address (XEXP (x, 0));
9200
9201   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
9202     return get_pool_constant (x);
9203
9204   return NULL_RTX;
9205 }
9206
9207 void
9208 ix86_expand_move (enum machine_mode mode, rtx operands[])
9209 {
9210   int strict = (reload_in_progress || reload_completed);
9211   rtx op0, op1;
9212   enum tls_model model;
9213
9214   op0 = operands[0];
9215   op1 = operands[1];
9216
9217   if (GET_CODE (op1) == SYMBOL_REF)
9218     {
9219       model = SYMBOL_REF_TLS_MODEL (op1);
9220       if (model)
9221         {
9222           op1 = legitimize_tls_address (op1, model, true);
9223           op1 = force_operand (op1, op0);
9224           if (op1 == op0)
9225             return;
9226         }
9227     }
9228   else if (GET_CODE (op1) == CONST
9229            && GET_CODE (XEXP (op1, 0)) == PLUS
9230            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
9231     {
9232       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
9233       if (model)
9234         {
9235           rtx addend = XEXP (XEXP (op1, 0), 1);
9236           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
9237           op1 = force_operand (op1, NULL);
9238           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
9239                                      op0, 1, OPTAB_DIRECT);
9240           if (op1 == op0)
9241             return;
9242         }
9243     }
9244
9245   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
9246     {
9247       if (TARGET_MACHO && !TARGET_64BIT)
9248         {
9249 #if TARGET_MACHO
9250           if (MACHOPIC_PURE)
9251             {
9252               rtx temp = ((reload_in_progress
9253                            || ((op0 && GET_CODE (op0) == REG)
9254                                && mode == Pmode))
9255                           ? op0 : gen_reg_rtx (Pmode));
9256               op1 = machopic_indirect_data_reference (op1, temp);
9257               op1 = machopic_legitimize_pic_address (op1, mode,
9258                                                      temp == op1 ? 0 : temp);
9259             }
9260           else if (MACHOPIC_INDIRECT)
9261             op1 = machopic_indirect_data_reference (op1, 0);
9262           if (op0 == op1)
9263             return;
9264 #endif
9265         }
9266       else
9267         {
9268           if (GET_CODE (op0) == MEM)
9269             op1 = force_reg (Pmode, op1);
9270           else
9271             op1 = legitimize_address (op1, op1, Pmode);
9272         }
9273     }
9274   else
9275     {
9276       if (GET_CODE (op0) == MEM
9277           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
9278               || !push_operand (op0, mode))
9279           && GET_CODE (op1) == MEM)
9280         op1 = force_reg (mode, op1);
9281
9282       if (push_operand (op0, mode)
9283           && ! general_no_elim_operand (op1, mode))
9284         op1 = copy_to_mode_reg (mode, op1);
9285
9286       /* Force large constants in 64bit compilation into register
9287          to get them CSEed.  */
9288       if (TARGET_64BIT && mode == DImode
9289           && immediate_operand (op1, mode)
9290           && !x86_64_zext_immediate_operand (op1, VOIDmode)
9291           && !register_operand (op0, mode)
9292           && optimize && !reload_completed && !reload_in_progress)
9293         op1 = copy_to_mode_reg (mode, op1);
9294
9295       if (FLOAT_MODE_P (mode))
9296         {
9297           /* If we are loading a floating point constant to a register,
9298              force the value to memory now, since we'll get better code
9299              out the back end.  */
9300
9301           if (strict)
9302             ;
9303           else if (GET_CODE (op1) == CONST_DOUBLE)
9304             {
9305               op1 = validize_mem (force_const_mem (mode, op1));
9306               if (!register_operand (op0, mode))
9307                 {
9308                   rtx temp = gen_reg_rtx (mode);
9309                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
9310                   emit_move_insn (op0, temp);
9311                   return;
9312                 }
9313             }
9314         }
9315     }
9316
9317   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9318 }
9319
9320 void
9321 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9322 {
9323   rtx op0 = operands[0], op1 = operands[1];
9324
9325   /* Force constants other than zero into memory.  We do not know how
9326      the instructions used to build constants modify the upper 64 bits
9327      of the register, once we have that information we may be able
9328      to handle some of them more efficiently.  */
9329   if ((reload_in_progress | reload_completed) == 0
9330       && register_operand (op0, mode)
9331       && CONSTANT_P (op1)
9332       && standard_sse_constant_p (op1) <= 0)
9333     op1 = validize_mem (force_const_mem (mode, op1));
9334
9335   /* Make operand1 a register if it isn't already.  */
9336   if (!no_new_pseudos
9337       && !register_operand (op0, mode)
9338       && !register_operand (op1, mode))
9339     {
9340       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9341       return;
9342     }
9343
9344   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9345 }
9346
9347 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9348    straight to ix86_expand_vector_move.  */
9349
9350 void
9351 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9352 {
9353   rtx op0, op1, m;
9354
9355   op0 = operands[0];
9356   op1 = operands[1];
9357
9358   if (MEM_P (op1))
9359     {
9360       /* If we're optimizing for size, movups is the smallest.  */
9361       if (optimize_size)
9362         {
9363           op0 = gen_lowpart (V4SFmode, op0);
9364           op1 = gen_lowpart (V4SFmode, op1);
9365           emit_insn (gen_sse_movups (op0, op1));
9366           return;
9367         }
9368
9369       /* ??? If we have typed data, then it would appear that using
9370          movdqu is the only way to get unaligned data loaded with
9371          integer type.  */
9372       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9373         {
9374           op0 = gen_lowpart (V16QImode, op0);
9375           op1 = gen_lowpart (V16QImode, op1);
9376           emit_insn (gen_sse2_movdqu (op0, op1));
9377           return;
9378         }
9379
9380       if (TARGET_SSE2 && mode == V2DFmode)
9381         {
9382           rtx zero;
9383
9384           /* When SSE registers are split into halves, we can avoid
9385              writing to the top half twice.  */
9386           if (TARGET_SSE_SPLIT_REGS)
9387             {
9388               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9389               zero = op0;
9390             }
9391           else
9392             {
9393               /* ??? Not sure about the best option for the Intel chips.
9394                  The following would seem to satisfy; the register is
9395                  entirely cleared, breaking the dependency chain.  We
9396                  then store to the upper half, with a dependency depth
9397                  of one.  A rumor has it that Intel recommends two movsd
9398                  followed by an unpacklpd, but this is unconfirmed.  And
9399                  given that the dependency depth of the unpacklpd would
9400                  still be one, I'm not sure why this would be better.  */
9401               zero = CONST0_RTX (V2DFmode);
9402             }
9403
9404           m = adjust_address (op1, DFmode, 0);
9405           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9406           m = adjust_address (op1, DFmode, 8);
9407           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9408         }
9409       else
9410         {
9411           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9412             emit_move_insn (op0, CONST0_RTX (mode));
9413           else
9414             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9415
9416           if (mode != V4SFmode)
9417             op0 = gen_lowpart (V4SFmode, op0);
9418           m = adjust_address (op1, V2SFmode, 0);
9419           emit_insn (gen_sse_loadlps (op0, op0, m));
9420           m = adjust_address (op1, V2SFmode, 8);
9421           emit_insn (gen_sse_loadhps (op0, op0, m));
9422         }
9423     }
9424   else if (MEM_P (op0))
9425     {
9426       /* If we're optimizing for size, movups is the smallest.  */
9427       if (optimize_size)
9428         {
9429           op0 = gen_lowpart (V4SFmode, op0);
9430           op1 = gen_lowpart (V4SFmode, op1);
9431           emit_insn (gen_sse_movups (op0, op1));
9432           return;
9433         }
9434
9435       /* ??? Similar to above, only less clear because of quote
9436          typeless stores unquote.  */
9437       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9438           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9439         {
9440           op0 = gen_lowpart (V16QImode, op0);
9441           op1 = gen_lowpart (V16QImode, op1);
9442           emit_insn (gen_sse2_movdqu (op0, op1));
9443           return;
9444         }
9445
9446       if (TARGET_SSE2 && mode == V2DFmode)
9447         {
9448           m = adjust_address (op0, DFmode, 0);
9449           emit_insn (gen_sse2_storelpd (m, op1));
9450           m = adjust_address (op0, DFmode, 8);
9451           emit_insn (gen_sse2_storehpd (m, op1));
9452         }
9453       else
9454         {
9455           if (mode != V4SFmode)
9456             op1 = gen_lowpart (V4SFmode, op1);
9457           m = adjust_address (op0, V2SFmode, 0);
9458           emit_insn (gen_sse_storelps (m, op1));
9459           m = adjust_address (op0, V2SFmode, 8);
9460           emit_insn (gen_sse_storehps (m, op1));
9461         }
9462     }
9463   else
9464     gcc_unreachable ();
9465 }
9466
9467 /* Expand a push in MODE.  This is some mode for which we do not support
9468    proper push instructions, at least from the registers that we expect
9469    the value to live in.  */
9470
9471 void
9472 ix86_expand_push (enum machine_mode mode, rtx x)
9473 {
9474   rtx tmp;
9475
9476   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9477                              GEN_INT (-GET_MODE_SIZE (mode)),
9478                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9479   if (tmp != stack_pointer_rtx)
9480     emit_move_insn (stack_pointer_rtx, tmp);
9481
9482   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9483   emit_move_insn (tmp, x);
9484 }
9485
9486 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9487    destination to use for the operation.  If different from the true
9488    destination in operands[0], a copy operation will be required.  */
9489
9490 rtx
9491 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9492                             rtx operands[])
9493 {
9494   int matching_memory;
9495   rtx src1, src2, dst;
9496
9497   dst = operands[0];
9498   src1 = operands[1];
9499   src2 = operands[2];
9500
9501   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
9502   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9503       && (rtx_equal_p (dst, src2)
9504           || immediate_operand (src1, mode)))
9505     {
9506       rtx temp = src1;
9507       src1 = src2;
9508       src2 = temp;
9509     }
9510
9511   /* If the destination is memory, and we do not have matching source
9512      operands, do things in registers.  */
9513   matching_memory = 0;
9514   if (GET_CODE (dst) == MEM)
9515     {
9516       if (rtx_equal_p (dst, src1))
9517         matching_memory = 1;
9518       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9519                && rtx_equal_p (dst, src2))
9520         matching_memory = 2;
9521       else
9522         dst = gen_reg_rtx (mode);
9523     }
9524
9525   /* Both source operands cannot be in memory.  */
9526   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
9527     {
9528       if (matching_memory != 2)
9529         src2 = force_reg (mode, src2);
9530       else
9531         src1 = force_reg (mode, src1);
9532     }
9533
9534   /* If the operation is not commutable, source 1 cannot be a constant
9535      or non-matching memory.  */
9536   if ((CONSTANT_P (src1)
9537        || (!matching_memory && GET_CODE (src1) == MEM))
9538       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9539     src1 = force_reg (mode, src1);
9540
9541   src1 = operands[1] = src1;
9542   src2 = operands[2] = src2;
9543   return dst;
9544 }
9545
9546 /* Similarly, but assume that the destination has already been
9547    set up properly.  */
9548
9549 void
9550 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9551                                     enum machine_mode mode, rtx operands[])
9552 {
9553   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9554   gcc_assert (dst == operands[0]);
9555 }
9556
9557 /* Attempt to expand a binary operator.  Make the expansion closer to the
9558    actual machine, then just general_operand, which will allow 3 separate
9559    memory references (one output, two input) in a single insn.  */
9560
9561 void
9562 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9563                              rtx operands[])
9564 {
9565   rtx src1, src2, dst, op, clob;
9566
9567   dst = ix86_fixup_binary_operands (code, mode, operands);
9568   src1 = operands[1];
9569   src2 = operands[2];
9570
9571  /* Emit the instruction.  */
9572
9573   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9574   if (reload_in_progress)
9575     {
9576       /* Reload doesn't know about the flags register, and doesn't know that
9577          it doesn't want to clobber it.  We can only do this with PLUS.  */
9578       gcc_assert (code == PLUS);
9579       emit_insn (op);
9580     }
9581   else
9582     {
9583       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9584       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9585     }
9586
9587   /* Fix up the destination if needed.  */
9588   if (dst != operands[0])
9589     emit_move_insn (operands[0], dst);
9590 }
9591
9592 /* Return TRUE or FALSE depending on whether the binary operator meets the
9593    appropriate constraints.  */
9594
9595 int
9596 ix86_binary_operator_ok (enum rtx_code code,
9597                          enum machine_mode mode ATTRIBUTE_UNUSED,
9598                          rtx operands[3])
9599 {
9600   /* Both source operands cannot be in memory.  */
9601   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
9602     return 0;
9603   /* If the operation is not commutable, source 1 cannot be a constant.  */
9604   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9605     return 0;
9606   /* If the destination is memory, we must have a matching source operand.  */
9607   if (GET_CODE (operands[0]) == MEM
9608       && ! (rtx_equal_p (operands[0], operands[1])
9609             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9610                 && rtx_equal_p (operands[0], operands[2]))))
9611     return 0;
9612   /* If the operation is not commutable and the source 1 is memory, we must
9613      have a matching destination.  */
9614   if (GET_CODE (operands[1]) == MEM
9615       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
9616       && ! rtx_equal_p (operands[0], operands[1]))
9617     return 0;
9618   return 1;
9619 }
9620
9621 /* Attempt to expand a unary operator.  Make the expansion closer to the
9622    actual machine, then just general_operand, which will allow 2 separate
9623    memory references (one output, one input) in a single insn.  */
9624
9625 void
9626 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
9627                             rtx operands[])
9628 {
9629   int matching_memory;
9630   rtx src, dst, op, clob;
9631
9632   dst = operands[0];
9633   src = operands[1];
9634
9635   /* If the destination is memory, and we do not have matching source
9636      operands, do things in registers.  */
9637   matching_memory = 0;
9638   if (MEM_P (dst))
9639     {
9640       if (rtx_equal_p (dst, src))
9641         matching_memory = 1;
9642       else
9643         dst = gen_reg_rtx (mode);
9644     }
9645
9646   /* When source operand is memory, destination must match.  */
9647   if (MEM_P (src) && !matching_memory)
9648     src = force_reg (mode, src);
9649
9650   /* Emit the instruction.  */
9651
9652   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
9653   if (reload_in_progress || code == NOT)
9654     {
9655       /* Reload doesn't know about the flags register, and doesn't know that
9656          it doesn't want to clobber it.  */
9657       gcc_assert (code == NOT);
9658       emit_insn (op);
9659     }
9660   else
9661     {
9662       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9663       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9664     }
9665
9666   /* Fix up the destination if needed.  */
9667   if (dst != operands[0])
9668     emit_move_insn (operands[0], dst);
9669 }
9670
9671 /* Return TRUE or FALSE depending on whether the unary operator meets the
9672    appropriate constraints.  */
9673
9674 int
9675 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
9676                         enum machine_mode mode ATTRIBUTE_UNUSED,
9677                         rtx operands[2] ATTRIBUTE_UNUSED)
9678 {
9679   /* If one of operands is memory, source and destination must match.  */
9680   if ((GET_CODE (operands[0]) == MEM
9681        || GET_CODE (operands[1]) == MEM)
9682       && ! rtx_equal_p (operands[0], operands[1]))
9683     return FALSE;
9684   return TRUE;
9685 }
9686
9687 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
9688    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
9689    true, then replicate the mask for all elements of the vector register.
9690    If INVERT is true, then create a mask excluding the sign bit.  */
9691
9692 rtx
9693 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
9694 {
9695   enum machine_mode vec_mode;
9696   HOST_WIDE_INT hi, lo;
9697   int shift = 63;
9698   rtvec v;
9699   rtx mask;
9700
9701   /* Find the sign bit, sign extended to 2*HWI.  */
9702   if (mode == SFmode)
9703     lo = 0x80000000, hi = lo < 0;
9704   else if (HOST_BITS_PER_WIDE_INT >= 64)
9705     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
9706   else
9707     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
9708
9709   if (invert)
9710     lo = ~lo, hi = ~hi;
9711
9712   /* Force this value into the low part of a fp vector constant.  */
9713   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
9714   mask = gen_lowpart (mode, mask);
9715
9716   if (mode == SFmode)
9717     {
9718       if (vect)
9719         v = gen_rtvec (4, mask, mask, mask, mask);
9720       else
9721         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
9722                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9723       vec_mode = V4SFmode;
9724     }
9725   else
9726     {
9727       if (vect)
9728         v = gen_rtvec (2, mask, mask);
9729       else
9730         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
9731       vec_mode = V2DFmode;
9732     }
9733
9734   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
9735 }
9736
9737 /* Generate code for floating point ABS or NEG.  */
9738
9739 void
9740 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
9741                                 rtx operands[])
9742 {
9743   rtx mask, set, use, clob, dst, src;
9744   bool matching_memory;
9745   bool use_sse = false;
9746   bool vector_mode = VECTOR_MODE_P (mode);
9747   enum machine_mode elt_mode = mode;
9748
9749   if (vector_mode)
9750     {
9751       elt_mode = GET_MODE_INNER (mode);
9752       use_sse = true;
9753     }
9754   else if (TARGET_SSE_MATH)
9755     use_sse = SSE_FLOAT_MODE_P (mode);
9756
9757   /* NEG and ABS performed with SSE use bitwise mask operations.
9758      Create the appropriate mask now.  */
9759   if (use_sse)
9760     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
9761   else
9762     mask = NULL_RTX;
9763
9764   dst = operands[0];
9765   src = operands[1];
9766
9767   /* If the destination is memory, and we don't have matching source
9768      operands or we're using the x87, do things in registers.  */
9769   matching_memory = false;
9770   if (MEM_P (dst))
9771     {
9772       if (use_sse && rtx_equal_p (dst, src))
9773         matching_memory = true;
9774       else
9775         dst = gen_reg_rtx (mode);
9776     }
9777   if (MEM_P (src) && !matching_memory)
9778     src = force_reg (mode, src);
9779
9780   if (vector_mode)
9781     {
9782       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
9783       set = gen_rtx_SET (VOIDmode, dst, set);
9784       emit_insn (set);
9785     }
9786   else
9787     {
9788       set = gen_rtx_fmt_e (code, mode, src);
9789       set = gen_rtx_SET (VOIDmode, dst, set);
9790       if (mask)
9791         {
9792           use = gen_rtx_USE (VOIDmode, mask);
9793           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9794           emit_insn (gen_rtx_PARALLEL (VOIDmode,
9795                                        gen_rtvec (3, set, use, clob)));
9796         }
9797       else
9798         emit_insn (set);
9799     }
9800
9801   if (dst != operands[0])
9802     emit_move_insn (operands[0], dst);
9803 }
9804
9805 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
9806
9807 void
9808 ix86_expand_copysign (rtx operands[])
9809 {
9810   enum machine_mode mode, vmode;
9811   rtx dest, op0, op1, mask, nmask;
9812
9813   dest = operands[0];
9814   op0 = operands[1];
9815   op1 = operands[2];
9816
9817   mode = GET_MODE (dest);
9818   vmode = mode == SFmode ? V4SFmode : V2DFmode;
9819
9820   if (GET_CODE (op0) == CONST_DOUBLE)
9821     {
9822       rtvec v;
9823
9824       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
9825         op0 = simplify_unary_operation (ABS, mode, op0, mode);
9826
9827       if (op0 == CONST0_RTX (mode))
9828         op0 = CONST0_RTX (vmode);
9829       else
9830         {
9831           if (mode == SFmode)
9832             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
9833                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9834           else
9835             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
9836           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
9837         }
9838
9839       mask = ix86_build_signbit_mask (mode, 0, 0);
9840
9841       if (mode == SFmode)
9842         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
9843       else
9844         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
9845     }
9846   else
9847     {
9848       nmask = ix86_build_signbit_mask (mode, 0, 1);
9849       mask = ix86_build_signbit_mask (mode, 0, 0);
9850
9851       if (mode == SFmode)
9852         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
9853       else
9854         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
9855     }
9856 }
9857
9858 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
9859    be a constant, and so has already been expanded into a vector constant.  */
9860
9861 void
9862 ix86_split_copysign_const (rtx operands[])
9863 {
9864   enum machine_mode mode, vmode;
9865   rtx dest, op0, op1, mask, x;
9866
9867   dest = operands[0];
9868   op0 = operands[1];
9869   op1 = operands[2];
9870   mask = operands[3];
9871
9872   mode = GET_MODE (dest);
9873   vmode = GET_MODE (mask);
9874
9875   dest = simplify_gen_subreg (vmode, dest, mode, 0);
9876   x = gen_rtx_AND (vmode, dest, mask);
9877   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9878
9879   if (op0 != CONST0_RTX (vmode))
9880     {
9881       x = gen_rtx_IOR (vmode, dest, op0);
9882       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9883     }
9884 }
9885
9886 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
9887    so we have to do two masks.  */
9888
9889 void
9890 ix86_split_copysign_var (rtx operands[])
9891 {
9892   enum machine_mode mode, vmode;
9893   rtx dest, scratch, op0, op1, mask, nmask, x;
9894
9895   dest = operands[0];
9896   scratch = operands[1];
9897   op0 = operands[2];
9898   op1 = operands[3];
9899   nmask = operands[4];
9900   mask = operands[5];
9901
9902   mode = GET_MODE (dest);
9903   vmode = GET_MODE (mask);
9904
9905   if (rtx_equal_p (op0, op1))
9906     {
9907       /* Shouldn't happen often (it's useless, obviously), but when it does
9908          we'd generate incorrect code if we continue below.  */
9909       emit_move_insn (dest, op0);
9910       return;
9911     }
9912
9913   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
9914     {
9915       gcc_assert (REGNO (op1) == REGNO (scratch));
9916
9917       x = gen_rtx_AND (vmode, scratch, mask);
9918       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9919
9920       dest = mask;
9921       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9922       x = gen_rtx_NOT (vmode, dest);
9923       x = gen_rtx_AND (vmode, x, op0);
9924       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9925     }
9926   else
9927     {
9928       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
9929         {
9930           x = gen_rtx_AND (vmode, scratch, mask);
9931         }
9932       else                                              /* alternative 2,4 */
9933         {
9934           gcc_assert (REGNO (mask) == REGNO (scratch));
9935           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
9936           x = gen_rtx_AND (vmode, scratch, op1);
9937         }
9938       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9939
9940       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
9941         {
9942           dest = simplify_gen_subreg (vmode, op0, mode, 0);
9943           x = gen_rtx_AND (vmode, dest, nmask);
9944         }
9945       else                                              /* alternative 3,4 */
9946         {
9947           gcc_assert (REGNO (nmask) == REGNO (dest));
9948           dest = nmask;
9949           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9950           x = gen_rtx_AND (vmode, dest, op0);
9951         }
9952       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9953     }
9954
9955   x = gen_rtx_IOR (vmode, dest, scratch);
9956   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9957 }
9958
9959 /* Return TRUE or FALSE depending on whether the first SET in INSN
9960    has source and destination with matching CC modes, and that the
9961    CC mode is at least as constrained as REQ_MODE.  */
9962
9963 int
9964 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
9965 {
9966   rtx set;
9967   enum machine_mode set_mode;
9968
9969   set = PATTERN (insn);
9970   if (GET_CODE (set) == PARALLEL)
9971     set = XVECEXP (set, 0, 0);
9972   gcc_assert (GET_CODE (set) == SET);
9973   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
9974
9975   set_mode = GET_MODE (SET_DEST (set));
9976   switch (set_mode)
9977     {
9978     case CCNOmode:
9979       if (req_mode != CCNOmode
9980           && (req_mode != CCmode
9981               || XEXP (SET_SRC (set), 1) != const0_rtx))
9982         return 0;
9983       break;
9984     case CCmode:
9985       if (req_mode == CCGCmode)
9986         return 0;
9987       /* FALLTHRU */
9988     case CCGCmode:
9989       if (req_mode == CCGOCmode || req_mode == CCNOmode)
9990         return 0;
9991       /* FALLTHRU */
9992     case CCGOCmode:
9993       if (req_mode == CCZmode)
9994         return 0;
9995       /* FALLTHRU */
9996     case CCZmode:
9997       break;
9998
9999     default:
10000       gcc_unreachable ();
10001     }
10002
10003   return (GET_MODE (SET_SRC (set)) == set_mode);
10004 }
10005
10006 /* Generate insn patterns to do an integer compare of OPERANDS.  */
10007
10008 static rtx
10009 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
10010 {
10011   enum machine_mode cmpmode;
10012   rtx tmp, flags;
10013
10014   cmpmode = SELECT_CC_MODE (code, op0, op1);
10015   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
10016
10017   /* This is very simple, but making the interface the same as in the
10018      FP case makes the rest of the code easier.  */
10019   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
10020   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
10021
10022   /* Return the test that should be put into the flags user, i.e.
10023      the bcc, scc, or cmov instruction.  */
10024   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
10025 }
10026
10027 /* Figure out whether to use ordered or unordered fp comparisons.
10028    Return the appropriate mode to use.  */
10029
10030 enum machine_mode
10031 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
10032 {
10033   /* ??? In order to make all comparisons reversible, we do all comparisons
10034      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
10035      all forms trapping and nontrapping comparisons, we can make inequality
10036      comparisons trapping again, since it results in better code when using
10037      FCOM based compares.  */
10038   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
10039 }
10040
10041 enum machine_mode
10042 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
10043 {
10044   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10045     return ix86_fp_compare_mode (code);
10046   switch (code)
10047     {
10048       /* Only zero flag is needed.  */
10049     case EQ:                    /* ZF=0 */
10050     case NE:                    /* ZF!=0 */
10051       return CCZmode;
10052       /* Codes needing carry flag.  */
10053     case GEU:                   /* CF=0 */
10054     case GTU:                   /* CF=0 & ZF=0 */
10055     case LTU:                   /* CF=1 */
10056     case LEU:                   /* CF=1 | ZF=1 */
10057       return CCmode;
10058       /* Codes possibly doable only with sign flag when
10059          comparing against zero.  */
10060     case GE:                    /* SF=OF   or   SF=0 */
10061     case LT:                    /* SF<>OF  or   SF=1 */
10062       if (op1 == const0_rtx)
10063         return CCGOCmode;
10064       else
10065         /* For other cases Carry flag is not required.  */
10066         return CCGCmode;
10067       /* Codes doable only with sign flag when comparing
10068          against zero, but we miss jump instruction for it
10069          so we need to use relational tests against overflow
10070          that thus needs to be zero.  */
10071     case GT:                    /* ZF=0 & SF=OF */
10072     case LE:                    /* ZF=1 | SF<>OF */
10073       if (op1 == const0_rtx)
10074         return CCNOmode;
10075       else
10076         return CCGCmode;
10077       /* strcmp pattern do (use flags) and combine may ask us for proper
10078          mode.  */
10079     case USE:
10080       return CCmode;
10081     default:
10082       gcc_unreachable ();
10083     }
10084 }
10085
10086 /* Return the fixed registers used for condition codes.  */
10087
10088 static bool
10089 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
10090 {
10091   *p1 = FLAGS_REG;
10092   *p2 = FPSR_REG;
10093   return true;
10094 }
10095
10096 /* If two condition code modes are compatible, return a condition code
10097    mode which is compatible with both.  Otherwise, return
10098    VOIDmode.  */
10099
10100 static enum machine_mode
10101 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
10102 {
10103   if (m1 == m2)
10104     return m1;
10105
10106   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
10107     return VOIDmode;
10108
10109   if ((m1 == CCGCmode && m2 == CCGOCmode)
10110       || (m1 == CCGOCmode && m2 == CCGCmode))
10111     return CCGCmode;
10112
10113   switch (m1)
10114     {
10115     default:
10116       gcc_unreachable ();
10117
10118     case CCmode:
10119     case CCGCmode:
10120     case CCGOCmode:
10121     case CCNOmode:
10122     case CCZmode:
10123       switch (m2)
10124         {
10125         default:
10126           return VOIDmode;
10127
10128         case CCmode:
10129         case CCGCmode:
10130         case CCGOCmode:
10131         case CCNOmode:
10132         case CCZmode:
10133           return CCmode;
10134         }
10135
10136     case CCFPmode:
10137     case CCFPUmode:
10138       /* These are only compatible with themselves, which we already
10139          checked above.  */
10140       return VOIDmode;
10141     }
10142 }
10143
10144 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
10145
10146 int
10147 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
10148 {
10149   enum rtx_code swapped_code = swap_condition (code);
10150   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
10151           || (ix86_fp_comparison_cost (swapped_code)
10152               == ix86_fp_comparison_fcomi_cost (swapped_code)));
10153 }
10154
10155 /* Swap, force into registers, or otherwise massage the two operands
10156    to a fp comparison.  The operands are updated in place; the new
10157    comparison code is returned.  */
10158
10159 static enum rtx_code
10160 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
10161 {
10162   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
10163   rtx op0 = *pop0, op1 = *pop1;
10164   enum machine_mode op_mode = GET_MODE (op0);
10165   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
10166
10167   /* All of the unordered compare instructions only work on registers.
10168      The same is true of the fcomi compare instructions.  The XFmode
10169      compare instructions require registers except when comparing
10170      against zero or when converting operand 1 from fixed point to
10171      floating point.  */
10172
10173   if (!is_sse
10174       && (fpcmp_mode == CCFPUmode
10175           || (op_mode == XFmode
10176               && ! (standard_80387_constant_p (op0) == 1
10177                     || standard_80387_constant_p (op1) == 1)
10178               && GET_CODE (op1) != FLOAT)
10179           || ix86_use_fcomi_compare (code)))
10180     {
10181       op0 = force_reg (op_mode, op0);
10182       op1 = force_reg (op_mode, op1);
10183     }
10184   else
10185     {
10186       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
10187          things around if they appear profitable, otherwise force op0
10188          into a register.  */
10189
10190       if (standard_80387_constant_p (op0) == 0
10191           || (GET_CODE (op0) == MEM
10192               && ! (standard_80387_constant_p (op1) == 0
10193                     || GET_CODE (op1) == MEM)))
10194         {
10195           rtx tmp;
10196           tmp = op0, op0 = op1, op1 = tmp;
10197           code = swap_condition (code);
10198         }
10199
10200       if (GET_CODE (op0) != REG)
10201         op0 = force_reg (op_mode, op0);
10202
10203       if (CONSTANT_P (op1))
10204         {
10205           int tmp = standard_80387_constant_p (op1);
10206           if (tmp == 0)
10207             op1 = validize_mem (force_const_mem (op_mode, op1));
10208           else if (tmp == 1)
10209             {
10210               if (TARGET_CMOVE)
10211                 op1 = force_reg (op_mode, op1);
10212             }
10213           else
10214             op1 = force_reg (op_mode, op1);
10215         }
10216     }
10217
10218   /* Try to rearrange the comparison to make it cheaper.  */
10219   if (ix86_fp_comparison_cost (code)
10220       > ix86_fp_comparison_cost (swap_condition (code))
10221       && (GET_CODE (op1) == REG || !no_new_pseudos))
10222     {
10223       rtx tmp;
10224       tmp = op0, op0 = op1, op1 = tmp;
10225       code = swap_condition (code);
10226       if (GET_CODE (op0) != REG)
10227         op0 = force_reg (op_mode, op0);
10228     }
10229
10230   *pop0 = op0;
10231   *pop1 = op1;
10232   return code;
10233 }
10234
10235 /* Convert comparison codes we use to represent FP comparison to integer
10236    code that will result in proper branch.  Return UNKNOWN if no such code
10237    is available.  */
10238
10239 enum rtx_code
10240 ix86_fp_compare_code_to_integer (enum rtx_code code)
10241 {
10242   switch (code)
10243     {
10244     case GT:
10245       return GTU;
10246     case GE:
10247       return GEU;
10248     case ORDERED:
10249     case UNORDERED:
10250       return code;
10251       break;
10252     case UNEQ:
10253       return EQ;
10254       break;
10255     case UNLT:
10256       return LTU;
10257       break;
10258     case UNLE:
10259       return LEU;
10260       break;
10261     case LTGT:
10262       return NE;
10263       break;
10264     default:
10265       return UNKNOWN;
10266     }
10267 }
10268
10269 /* Split comparison code CODE into comparisons we can do using branch
10270    instructions.  BYPASS_CODE is comparison code for branch that will
10271    branch around FIRST_CODE and SECOND_CODE.  If some of branches
10272    is not required, set value to UNKNOWN.
10273    We never require more than two branches.  */
10274
10275 void
10276 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
10277                           enum rtx_code *first_code,
10278                           enum rtx_code *second_code)
10279 {
10280   *first_code = code;
10281   *bypass_code = UNKNOWN;
10282   *second_code = UNKNOWN;
10283
10284   /* The fcomi comparison sets flags as follows:
10285
10286      cmp    ZF PF CF
10287      >      0  0  0
10288      <      0  0  1
10289      =      1  0  0
10290      un     1  1  1 */
10291
10292   switch (code)
10293     {
10294     case GT:                    /* GTU - CF=0 & ZF=0 */
10295     case GE:                    /* GEU - CF=0 */
10296     case ORDERED:               /* PF=0 */
10297     case UNORDERED:             /* PF=1 */
10298     case UNEQ:                  /* EQ - ZF=1 */
10299     case UNLT:                  /* LTU - CF=1 */
10300     case UNLE:                  /* LEU - CF=1 | ZF=1 */
10301     case LTGT:                  /* EQ - ZF=0 */
10302       break;
10303     case LT:                    /* LTU - CF=1 - fails on unordered */
10304       *first_code = UNLT;
10305       *bypass_code = UNORDERED;
10306       break;
10307     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
10308       *first_code = UNLE;
10309       *bypass_code = UNORDERED;
10310       break;
10311     case EQ:                    /* EQ - ZF=1 - fails on unordered */
10312       *first_code = UNEQ;
10313       *bypass_code = UNORDERED;
10314       break;
10315     case NE:                    /* NE - ZF=0 - fails on unordered */
10316       *first_code = LTGT;
10317       *second_code = UNORDERED;
10318       break;
10319     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10320       *first_code = GE;
10321       *second_code = UNORDERED;
10322       break;
10323     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10324       *first_code = GT;
10325       *second_code = UNORDERED;
10326       break;
10327     default:
10328       gcc_unreachable ();
10329     }
10330   if (!TARGET_IEEE_FP)
10331     {
10332       *second_code = UNKNOWN;
10333       *bypass_code = UNKNOWN;
10334     }
10335 }
10336
10337 /* Return cost of comparison done fcom + arithmetics operations on AX.
10338    All following functions do use number of instructions as a cost metrics.
10339    In future this should be tweaked to compute bytes for optimize_size and
10340    take into account performance of various instructions on various CPUs.  */
10341 static int
10342 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10343 {
10344   if (!TARGET_IEEE_FP)
10345     return 4;
10346   /* The cost of code output by ix86_expand_fp_compare.  */
10347   switch (code)
10348     {
10349     case UNLE:
10350     case UNLT:
10351     case LTGT:
10352     case GT:
10353     case GE:
10354     case UNORDERED:
10355     case ORDERED:
10356     case UNEQ:
10357       return 4;
10358       break;
10359     case LT:
10360     case NE:
10361     case EQ:
10362     case UNGE:
10363       return 5;
10364       break;
10365     case LE:
10366     case UNGT:
10367       return 6;
10368       break;
10369     default:
10370       gcc_unreachable ();
10371     }
10372 }
10373
10374 /* Return cost of comparison done using fcomi operation.
10375    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10376 static int
10377 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10378 {
10379   enum rtx_code bypass_code, first_code, second_code;
10380   /* Return arbitrarily high cost when instruction is not supported - this
10381      prevents gcc from using it.  */
10382   if (!TARGET_CMOVE)
10383     return 1024;
10384   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10385   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10386 }
10387
10388 /* Return cost of comparison done using sahf operation.
10389    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10390 static int
10391 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10392 {
10393   enum rtx_code bypass_code, first_code, second_code;
10394   /* Return arbitrarily high cost when instruction is not preferred - this
10395      avoids gcc from using it.  */
10396   if (!TARGET_USE_SAHF && !optimize_size)
10397     return 1024;
10398   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10399   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10400 }
10401
10402 /* Compute cost of the comparison done using any method.
10403    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10404 static int
10405 ix86_fp_comparison_cost (enum rtx_code code)
10406 {
10407   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10408   int min;
10409
10410   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10411   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10412
10413   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10414   if (min > sahf_cost)
10415     min = sahf_cost;
10416   if (min > fcomi_cost)
10417     min = fcomi_cost;
10418   return min;
10419 }
10420
10421 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
10422
10423 static rtx
10424 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
10425                         rtx *second_test, rtx *bypass_test)
10426 {
10427   enum machine_mode fpcmp_mode, intcmp_mode;
10428   rtx tmp, tmp2;
10429   int cost = ix86_fp_comparison_cost (code);
10430   enum rtx_code bypass_code, first_code, second_code;
10431
10432   fpcmp_mode = ix86_fp_compare_mode (code);
10433   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
10434
10435   if (second_test)
10436     *second_test = NULL_RTX;
10437   if (bypass_test)
10438     *bypass_test = NULL_RTX;
10439
10440   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10441
10442   /* Do fcomi/sahf based test when profitable.  */
10443   if ((bypass_code == UNKNOWN || bypass_test)
10444       && (second_code == UNKNOWN || second_test)
10445       && ix86_fp_comparison_arithmetics_cost (code) > cost)
10446     {
10447       if (TARGET_CMOVE)
10448         {
10449           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10450           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
10451                              tmp);
10452           emit_insn (tmp);
10453         }
10454       else
10455         {
10456           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10457           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10458           if (!scratch)
10459             scratch = gen_reg_rtx (HImode);
10460           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10461           emit_insn (gen_x86_sahf_1 (scratch));
10462         }
10463
10464       /* The FP codes work out to act like unsigned.  */
10465       intcmp_mode = fpcmp_mode;
10466       code = first_code;
10467       if (bypass_code != UNKNOWN)
10468         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
10469                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10470                                        const0_rtx);
10471       if (second_code != UNKNOWN)
10472         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
10473                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10474                                        const0_rtx);
10475     }
10476   else
10477     {
10478       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
10479       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10480       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10481       if (!scratch)
10482         scratch = gen_reg_rtx (HImode);
10483       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10484
10485       /* In the unordered case, we have to check C2 for NaN's, which
10486          doesn't happen to work out to anything nice combination-wise.
10487          So do some bit twiddling on the value we've got in AH to come
10488          up with an appropriate set of condition codes.  */
10489
10490       intcmp_mode = CCNOmode;
10491       switch (code)
10492         {
10493         case GT:
10494         case UNGT:
10495           if (code == GT || !TARGET_IEEE_FP)
10496             {
10497               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10498               code = EQ;
10499             }
10500           else
10501             {
10502               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10503               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10504               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
10505               intcmp_mode = CCmode;
10506               code = GEU;
10507             }
10508           break;
10509         case LT:
10510         case UNLT:
10511           if (code == LT && TARGET_IEEE_FP)
10512             {
10513               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10514               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
10515               intcmp_mode = CCmode;
10516               code = EQ;
10517             }
10518           else
10519             {
10520               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
10521               code = NE;
10522             }
10523           break;
10524         case GE:
10525         case UNGE:
10526           if (code == GE || !TARGET_IEEE_FP)
10527             {
10528               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
10529               code = EQ;
10530             }
10531           else
10532             {
10533               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10534               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10535                                              GEN_INT (0x01)));
10536               code = NE;
10537             }
10538           break;
10539         case LE:
10540         case UNLE:
10541           if (code == LE && TARGET_IEEE_FP)
10542             {
10543               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10544               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10545               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10546               intcmp_mode = CCmode;
10547               code = LTU;
10548             }
10549           else
10550             {
10551               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10552               code = NE;
10553             }
10554           break;
10555         case EQ:
10556         case UNEQ:
10557           if (code == EQ && TARGET_IEEE_FP)
10558             {
10559               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10560               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10561               intcmp_mode = CCmode;
10562               code = EQ;
10563             }
10564           else
10565             {
10566               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10567               code = NE;
10568               break;
10569             }
10570           break;
10571         case NE:
10572         case LTGT:
10573           if (code == NE && TARGET_IEEE_FP)
10574             {
10575               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10576               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10577                                              GEN_INT (0x40)));
10578               code = NE;
10579             }
10580           else
10581             {
10582               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10583               code = EQ;
10584             }
10585           break;
10586
10587         case UNORDERED:
10588           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10589           code = NE;
10590           break;
10591         case ORDERED:
10592           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10593           code = EQ;
10594           break;
10595
10596         default:
10597           gcc_unreachable ();
10598         }
10599     }
10600
10601   /* Return the test that should be put into the flags user, i.e.
10602      the bcc, scc, or cmov instruction.  */
10603   return gen_rtx_fmt_ee (code, VOIDmode,
10604                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
10605                          const0_rtx);
10606 }
10607
10608 rtx
10609 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
10610 {
10611   rtx op0, op1, ret;
10612   op0 = ix86_compare_op0;
10613   op1 = ix86_compare_op1;
10614
10615   if (second_test)
10616     *second_test = NULL_RTX;
10617   if (bypass_test)
10618     *bypass_test = NULL_RTX;
10619
10620   if (ix86_compare_emitted)
10621     {
10622       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
10623       ix86_compare_emitted = NULL_RTX;
10624     }
10625   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10626     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10627                                   second_test, bypass_test);
10628   else
10629     ret = ix86_expand_int_compare (code, op0, op1);
10630
10631   return ret;
10632 }
10633
10634 /* Return true if the CODE will result in nontrivial jump sequence.  */
10635 bool
10636 ix86_fp_jump_nontrivial_p (enum rtx_code code)
10637 {
10638   enum rtx_code bypass_code, first_code, second_code;
10639   if (!TARGET_CMOVE)
10640     return true;
10641   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10642   return bypass_code != UNKNOWN || second_code != UNKNOWN;
10643 }
10644
10645 void
10646 ix86_expand_branch (enum rtx_code code, rtx label)
10647 {
10648   rtx tmp;
10649
10650   /* If we have emitted a compare insn, go straight to simple.
10651      ix86_expand_compare won't emit anything if ix86_compare_emitted
10652      is non NULL.  */
10653   if (ix86_compare_emitted)
10654     goto simple;
10655
10656   switch (GET_MODE (ix86_compare_op0))
10657     {
10658     case QImode:
10659     case HImode:
10660     case SImode:
10661       simple:
10662       tmp = ix86_expand_compare (code, NULL, NULL);
10663       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10664                                   gen_rtx_LABEL_REF (VOIDmode, label),
10665                                   pc_rtx);
10666       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10667       return;
10668
10669     case SFmode:
10670     case DFmode:
10671     case XFmode:
10672       {
10673         rtvec vec;
10674         int use_fcomi;
10675         enum rtx_code bypass_code, first_code, second_code;
10676
10677         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
10678                                              &ix86_compare_op1);
10679
10680         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10681
10682         /* Check whether we will use the natural sequence with one jump.  If
10683            so, we can expand jump early.  Otherwise delay expansion by
10684            creating compound insn to not confuse optimizers.  */
10685         if (bypass_code == UNKNOWN && second_code == UNKNOWN
10686             && TARGET_CMOVE)
10687           {
10688             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
10689                                   gen_rtx_LABEL_REF (VOIDmode, label),
10690                                   pc_rtx, NULL_RTX, NULL_RTX);
10691           }
10692         else
10693           {
10694             tmp = gen_rtx_fmt_ee (code, VOIDmode,
10695                                   ix86_compare_op0, ix86_compare_op1);
10696             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10697                                         gen_rtx_LABEL_REF (VOIDmode, label),
10698                                         pc_rtx);
10699             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
10700
10701             use_fcomi = ix86_use_fcomi_compare (code);
10702             vec = rtvec_alloc (3 + !use_fcomi);
10703             RTVEC_ELT (vec, 0) = tmp;
10704             RTVEC_ELT (vec, 1)
10705               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
10706             RTVEC_ELT (vec, 2)
10707               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
10708             if (! use_fcomi)
10709               RTVEC_ELT (vec, 3)
10710                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
10711
10712             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
10713           }
10714         return;
10715       }
10716
10717     case DImode:
10718       if (TARGET_64BIT)
10719         goto simple;
10720     case TImode:
10721       /* Expand DImode branch into multiple compare+branch.  */
10722       {
10723         rtx lo[2], hi[2], label2;
10724         enum rtx_code code1, code2, code3;
10725         enum machine_mode submode;
10726
10727         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
10728           {
10729             tmp = ix86_compare_op0;
10730             ix86_compare_op0 = ix86_compare_op1;
10731             ix86_compare_op1 = tmp;
10732             code = swap_condition (code);
10733           }
10734         if (GET_MODE (ix86_compare_op0) == DImode)
10735           {
10736             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
10737             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
10738             submode = SImode;
10739           }
10740         else
10741           {
10742             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
10743             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
10744             submode = DImode;
10745           }
10746
10747         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
10748            avoid two branches.  This costs one extra insn, so disable when
10749            optimizing for size.  */
10750
10751         if ((code == EQ || code == NE)
10752             && (!optimize_size
10753                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
10754           {
10755             rtx xor0, xor1;
10756
10757             xor1 = hi[0];
10758             if (hi[1] != const0_rtx)
10759               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
10760                                    NULL_RTX, 0, OPTAB_WIDEN);
10761
10762             xor0 = lo[0];
10763             if (lo[1] != const0_rtx)
10764               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
10765                                    NULL_RTX, 0, OPTAB_WIDEN);
10766
10767             tmp = expand_binop (submode, ior_optab, xor1, xor0,
10768                                 NULL_RTX, 0, OPTAB_WIDEN);
10769
10770             ix86_compare_op0 = tmp;
10771             ix86_compare_op1 = const0_rtx;
10772             ix86_expand_branch (code, label);
10773             return;
10774           }
10775
10776         /* Otherwise, if we are doing less-than or greater-or-equal-than,
10777            op1 is a constant and the low word is zero, then we can just
10778            examine the high word.  */
10779
10780         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
10781           switch (code)
10782             {
10783             case LT: case LTU: case GE: case GEU:
10784               ix86_compare_op0 = hi[0];
10785               ix86_compare_op1 = hi[1];
10786               ix86_expand_branch (code, label);
10787               return;
10788             default:
10789               break;
10790             }
10791
10792         /* Otherwise, we need two or three jumps.  */
10793
10794         label2 = gen_label_rtx ();
10795
10796         code1 = code;
10797         code2 = swap_condition (code);
10798         code3 = unsigned_condition (code);
10799
10800         switch (code)
10801           {
10802           case LT: case GT: case LTU: case GTU:
10803             break;
10804
10805           case LE:   code1 = LT;  code2 = GT;  break;
10806           case GE:   code1 = GT;  code2 = LT;  break;
10807           case LEU:  code1 = LTU; code2 = GTU; break;
10808           case GEU:  code1 = GTU; code2 = LTU; break;
10809
10810           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
10811           case NE:   code2 = UNKNOWN; break;
10812
10813           default:
10814             gcc_unreachable ();
10815           }
10816
10817         /*
10818          * a < b =>
10819          *    if (hi(a) < hi(b)) goto true;
10820          *    if (hi(a) > hi(b)) goto false;
10821          *    if (lo(a) < lo(b)) goto true;
10822          *  false:
10823          */
10824
10825         ix86_compare_op0 = hi[0];
10826         ix86_compare_op1 = hi[1];
10827
10828         if (code1 != UNKNOWN)
10829           ix86_expand_branch (code1, label);
10830         if (code2 != UNKNOWN)
10831           ix86_expand_branch (code2, label2);
10832
10833         ix86_compare_op0 = lo[0];
10834         ix86_compare_op1 = lo[1];
10835         ix86_expand_branch (code3, label);
10836
10837         if (code2 != UNKNOWN)
10838           emit_label (label2);
10839         return;
10840       }
10841
10842     default:
10843       gcc_unreachable ();
10844     }
10845 }
10846
10847 /* Split branch based on floating point condition.  */
10848 void
10849 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
10850                       rtx target1, rtx target2, rtx tmp, rtx pushed)
10851 {
10852   rtx second, bypass;
10853   rtx label = NULL_RTX;
10854   rtx condition;
10855   int bypass_probability = -1, second_probability = -1, probability = -1;
10856   rtx i;
10857
10858   if (target2 != pc_rtx)
10859     {
10860       rtx tmp = target2;
10861       code = reverse_condition_maybe_unordered (code);
10862       target2 = target1;
10863       target1 = tmp;
10864     }
10865
10866   condition = ix86_expand_fp_compare (code, op1, op2,
10867                                       tmp, &second, &bypass);
10868
10869   /* Remove pushed operand from stack.  */
10870   if (pushed)
10871     ix86_free_from_memory (GET_MODE (pushed));
10872
10873   if (split_branch_probability >= 0)
10874     {
10875       /* Distribute the probabilities across the jumps.
10876          Assume the BYPASS and SECOND to be always test
10877          for UNORDERED.  */
10878       probability = split_branch_probability;
10879
10880       /* Value of 1 is low enough to make no need for probability
10881          to be updated.  Later we may run some experiments and see
10882          if unordered values are more frequent in practice.  */
10883       if (bypass)
10884         bypass_probability = 1;
10885       if (second)
10886         second_probability = 1;
10887     }
10888   if (bypass != NULL_RTX)
10889     {
10890       label = gen_label_rtx ();
10891       i = emit_jump_insn (gen_rtx_SET
10892                           (VOIDmode, pc_rtx,
10893                            gen_rtx_IF_THEN_ELSE (VOIDmode,
10894                                                  bypass,
10895                                                  gen_rtx_LABEL_REF (VOIDmode,
10896                                                                     label),
10897                                                  pc_rtx)));
10898       if (bypass_probability >= 0)
10899         REG_NOTES (i)
10900           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10901                                GEN_INT (bypass_probability),
10902                                REG_NOTES (i));
10903     }
10904   i = emit_jump_insn (gen_rtx_SET
10905                       (VOIDmode, pc_rtx,
10906                        gen_rtx_IF_THEN_ELSE (VOIDmode,
10907                                              condition, target1, target2)));
10908   if (probability >= 0)
10909     REG_NOTES (i)
10910       = gen_rtx_EXPR_LIST (REG_BR_PROB,
10911                            GEN_INT (probability),
10912                            REG_NOTES (i));
10913   if (second != NULL_RTX)
10914     {
10915       i = emit_jump_insn (gen_rtx_SET
10916                           (VOIDmode, pc_rtx,
10917                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
10918                                                  target2)));
10919       if (second_probability >= 0)
10920         REG_NOTES (i)
10921           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10922                                GEN_INT (second_probability),
10923                                REG_NOTES (i));
10924     }
10925   if (label != NULL_RTX)
10926     emit_label (label);
10927 }
10928
10929 int
10930 ix86_expand_setcc (enum rtx_code code, rtx dest)
10931 {
10932   rtx ret, tmp, tmpreg, equiv;
10933   rtx second_test, bypass_test;
10934
10935   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
10936     return 0; /* FAIL */
10937
10938   gcc_assert (GET_MODE (dest) == QImode);
10939
10940   ret = ix86_expand_compare (code, &second_test, &bypass_test);
10941   PUT_MODE (ret, QImode);
10942
10943   tmp = dest;
10944   tmpreg = dest;
10945
10946   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
10947   if (bypass_test || second_test)
10948     {
10949       rtx test = second_test;
10950       int bypass = 0;
10951       rtx tmp2 = gen_reg_rtx (QImode);
10952       if (bypass_test)
10953         {
10954           gcc_assert (!second_test);
10955           test = bypass_test;
10956           bypass = 1;
10957           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
10958         }
10959       PUT_MODE (test, QImode);
10960       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
10961
10962       if (bypass)
10963         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
10964       else
10965         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
10966     }
10967
10968   /* Attach a REG_EQUAL note describing the comparison result.  */
10969   if (ix86_compare_op0 && ix86_compare_op1)
10970     {
10971       equiv = simplify_gen_relational (code, QImode,
10972                                        GET_MODE (ix86_compare_op0),
10973                                        ix86_compare_op0, ix86_compare_op1);
10974       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
10975     }
10976
10977   return 1; /* DONE */
10978 }
10979
10980 /* Expand comparison setting or clearing carry flag.  Return true when
10981    successful and set pop for the operation.  */
10982 static bool
10983 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
10984 {
10985   enum machine_mode mode =
10986     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
10987
10988   /* Do not handle DImode compares that go through special path.  Also we can't
10989      deal with FP compares yet.  This is possible to add.  */
10990   if (mode == (TARGET_64BIT ? TImode : DImode))
10991     return false;
10992   if (FLOAT_MODE_P (mode))
10993     {
10994       rtx second_test = NULL, bypass_test = NULL;
10995       rtx compare_op, compare_seq;
10996
10997       /* Shortcut:  following common codes never translate into carry flag compares.  */
10998       if (code == EQ || code == NE || code == UNEQ || code == LTGT
10999           || code == ORDERED || code == UNORDERED)
11000         return false;
11001
11002       /* These comparisons require zero flag; swap operands so they won't.  */
11003       if ((code == GT || code == UNLE || code == LE || code == UNGT)
11004           && !TARGET_IEEE_FP)
11005         {
11006           rtx tmp = op0;
11007           op0 = op1;
11008           op1 = tmp;
11009           code = swap_condition (code);
11010         }
11011
11012       /* Try to expand the comparison and verify that we end up with carry flag
11013          based comparison.  This is fails to be true only when we decide to expand
11014          comparison using arithmetic that is not too common scenario.  */
11015       start_sequence ();
11016       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11017                                            &second_test, &bypass_test);
11018       compare_seq = get_insns ();
11019       end_sequence ();
11020
11021       if (second_test || bypass_test)
11022         return false;
11023       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11024           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11025         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
11026       else
11027         code = GET_CODE (compare_op);
11028       if (code != LTU && code != GEU)
11029         return false;
11030       emit_insn (compare_seq);
11031       *pop = compare_op;
11032       return true;
11033     }
11034   if (!INTEGRAL_MODE_P (mode))
11035     return false;
11036   switch (code)
11037     {
11038     case LTU:
11039     case GEU:
11040       break;
11041
11042     /* Convert a==0 into (unsigned)a<1.  */
11043     case EQ:
11044     case NE:
11045       if (op1 != const0_rtx)
11046         return false;
11047       op1 = const1_rtx;
11048       code = (code == EQ ? LTU : GEU);
11049       break;
11050
11051     /* Convert a>b into b<a or a>=b-1.  */
11052     case GTU:
11053     case LEU:
11054       if (GET_CODE (op1) == CONST_INT)
11055         {
11056           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
11057           /* Bail out on overflow.  We still can swap operands but that
11058              would force loading of the constant into register.  */
11059           if (op1 == const0_rtx
11060               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
11061             return false;
11062           code = (code == GTU ? GEU : LTU);
11063         }
11064       else
11065         {
11066           rtx tmp = op1;
11067           op1 = op0;
11068           op0 = tmp;
11069           code = (code == GTU ? LTU : GEU);
11070         }
11071       break;
11072
11073     /* Convert a>=0 into (unsigned)a<0x80000000.  */
11074     case LT:
11075     case GE:
11076       if (mode == DImode || op1 != const0_rtx)
11077         return false;
11078       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11079       code = (code == LT ? GEU : LTU);
11080       break;
11081     case LE:
11082     case GT:
11083       if (mode == DImode || op1 != constm1_rtx)
11084         return false;
11085       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11086       code = (code == LE ? GEU : LTU);
11087       break;
11088
11089     default:
11090       return false;
11091     }
11092   /* Swapping operands may cause constant to appear as first operand.  */
11093   if (!nonimmediate_operand (op0, VOIDmode))
11094     {
11095       if (no_new_pseudos)
11096         return false;
11097       op0 = force_reg (mode, op0);
11098     }
11099   ix86_compare_op0 = op0;
11100   ix86_compare_op1 = op1;
11101   *pop = ix86_expand_compare (code, NULL, NULL);
11102   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
11103   return true;
11104 }
11105
11106 int
11107 ix86_expand_int_movcc (rtx operands[])
11108 {
11109   enum rtx_code code = GET_CODE (operands[1]), compare_code;
11110   rtx compare_seq, compare_op;
11111   rtx second_test, bypass_test;
11112   enum machine_mode mode = GET_MODE (operands[0]);
11113   bool sign_bit_compare_p = false;;
11114
11115   start_sequence ();
11116   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11117   compare_seq = get_insns ();
11118   end_sequence ();
11119
11120   compare_code = GET_CODE (compare_op);
11121
11122   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
11123       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
11124     sign_bit_compare_p = true;
11125
11126   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
11127      HImode insns, we'd be swallowed in word prefix ops.  */
11128
11129   if ((mode != HImode || TARGET_FAST_PREFIX)
11130       && (mode != (TARGET_64BIT ? TImode : DImode))
11131       && GET_CODE (operands[2]) == CONST_INT
11132       && GET_CODE (operands[3]) == CONST_INT)
11133     {
11134       rtx out = operands[0];
11135       HOST_WIDE_INT ct = INTVAL (operands[2]);
11136       HOST_WIDE_INT cf = INTVAL (operands[3]);
11137       HOST_WIDE_INT diff;
11138
11139       diff = ct - cf;
11140       /*  Sign bit compares are better done using shifts than we do by using
11141           sbb.  */
11142       if (sign_bit_compare_p
11143           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11144                                              ix86_compare_op1, &compare_op))
11145         {
11146           /* Detect overlap between destination and compare sources.  */
11147           rtx tmp = out;
11148
11149           if (!sign_bit_compare_p)
11150             {
11151               bool fpcmp = false;
11152
11153               compare_code = GET_CODE (compare_op);
11154
11155               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11156                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11157                 {
11158                   fpcmp = true;
11159                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
11160                 }
11161
11162               /* To simplify rest of code, restrict to the GEU case.  */
11163               if (compare_code == LTU)
11164                 {
11165                   HOST_WIDE_INT tmp = ct;
11166                   ct = cf;
11167                   cf = tmp;
11168                   compare_code = reverse_condition (compare_code);
11169                   code = reverse_condition (code);
11170                 }
11171               else
11172                 {
11173                   if (fpcmp)
11174                     PUT_CODE (compare_op,
11175                               reverse_condition_maybe_unordered
11176                                 (GET_CODE (compare_op)));
11177                   else
11178                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11179                 }
11180               diff = ct - cf;
11181
11182               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
11183                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
11184                 tmp = gen_reg_rtx (mode);
11185
11186               if (mode == DImode)
11187                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
11188               else
11189                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
11190             }
11191           else
11192             {
11193               if (code == GT || code == GE)
11194                 code = reverse_condition (code);
11195               else
11196                 {
11197                   HOST_WIDE_INT tmp = ct;
11198                   ct = cf;
11199                   cf = tmp;
11200                   diff = ct - cf;
11201                 }
11202               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
11203                                      ix86_compare_op1, VOIDmode, 0, -1);
11204             }
11205
11206           if (diff == 1)
11207             {
11208               /*
11209                * cmpl op0,op1
11210                * sbbl dest,dest
11211                * [addl dest, ct]
11212                *
11213                * Size 5 - 8.
11214                */
11215               if (ct)
11216                 tmp = expand_simple_binop (mode, PLUS,
11217                                            tmp, GEN_INT (ct),
11218                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11219             }
11220           else if (cf == -1)
11221             {
11222               /*
11223                * cmpl op0,op1
11224                * sbbl dest,dest
11225                * orl $ct, dest
11226                *
11227                * Size 8.
11228                */
11229               tmp = expand_simple_binop (mode, IOR,
11230                                          tmp, GEN_INT (ct),
11231                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11232             }
11233           else if (diff == -1 && ct)
11234             {
11235               /*
11236                * cmpl op0,op1
11237                * sbbl dest,dest
11238                * notl dest
11239                * [addl dest, cf]
11240                *
11241                * Size 8 - 11.
11242                */
11243               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11244               if (cf)
11245                 tmp = expand_simple_binop (mode, PLUS,
11246                                            copy_rtx (tmp), GEN_INT (cf),
11247                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11248             }
11249           else
11250             {
11251               /*
11252                * cmpl op0,op1
11253                * sbbl dest,dest
11254                * [notl dest]
11255                * andl cf - ct, dest
11256                * [addl dest, ct]
11257                *
11258                * Size 8 - 11.
11259                */
11260
11261               if (cf == 0)
11262                 {
11263                   cf = ct;
11264                   ct = 0;
11265                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11266                 }
11267
11268               tmp = expand_simple_binop (mode, AND,
11269                                          copy_rtx (tmp),
11270                                          gen_int_mode (cf - ct, mode),
11271                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11272               if (ct)
11273                 tmp = expand_simple_binop (mode, PLUS,
11274                                            copy_rtx (tmp), GEN_INT (ct),
11275                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11276             }
11277
11278           if (!rtx_equal_p (tmp, out))
11279             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
11280
11281           return 1; /* DONE */
11282         }
11283
11284       if (diff < 0)
11285         {
11286           HOST_WIDE_INT tmp;
11287           tmp = ct, ct = cf, cf = tmp;
11288           diff = -diff;
11289           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11290             {
11291               /* We may be reversing unordered compare to normal compare, that
11292                  is not valid in general (we may convert non-trapping condition
11293                  to trapping one), however on i386 we currently emit all
11294                  comparisons unordered.  */
11295               compare_code = reverse_condition_maybe_unordered (compare_code);
11296               code = reverse_condition_maybe_unordered (code);
11297             }
11298           else
11299             {
11300               compare_code = reverse_condition (compare_code);
11301               code = reverse_condition (code);
11302             }
11303         }
11304
11305       compare_code = UNKNOWN;
11306       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
11307           && GET_CODE (ix86_compare_op1) == CONST_INT)
11308         {
11309           if (ix86_compare_op1 == const0_rtx
11310               && (code == LT || code == GE))
11311             compare_code = code;
11312           else if (ix86_compare_op1 == constm1_rtx)
11313             {
11314               if (code == LE)
11315                 compare_code = LT;
11316               else if (code == GT)
11317                 compare_code = GE;
11318             }
11319         }
11320
11321       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11322       if (compare_code != UNKNOWN
11323           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11324           && (cf == -1 || ct == -1))
11325         {
11326           /* If lea code below could be used, only optimize
11327              if it results in a 2 insn sequence.  */
11328
11329           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11330                  || diff == 3 || diff == 5 || diff == 9)
11331               || (compare_code == LT && ct == -1)
11332               || (compare_code == GE && cf == -1))
11333             {
11334               /*
11335                * notl op1       (if necessary)
11336                * sarl $31, op1
11337                * orl cf, op1
11338                */
11339               if (ct != -1)
11340                 {
11341                   cf = ct;
11342                   ct = -1;
11343                   code = reverse_condition (code);
11344                 }
11345
11346               out = emit_store_flag (out, code, ix86_compare_op0,
11347                                      ix86_compare_op1, VOIDmode, 0, -1);
11348
11349               out = expand_simple_binop (mode, IOR,
11350                                          out, GEN_INT (cf),
11351                                          out, 1, OPTAB_DIRECT);
11352               if (out != operands[0])
11353                 emit_move_insn (operands[0], out);
11354
11355               return 1; /* DONE */
11356             }
11357         }
11358
11359
11360       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
11361            || diff == 3 || diff == 5 || diff == 9)
11362           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
11363           && (mode != DImode
11364               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
11365         {
11366           /*
11367            * xorl dest,dest
11368            * cmpl op1,op2
11369            * setcc dest
11370            * lea cf(dest*(ct-cf)),dest
11371            *
11372            * Size 14.
11373            *
11374            * This also catches the degenerate setcc-only case.
11375            */
11376
11377           rtx tmp;
11378           int nops;
11379
11380           out = emit_store_flag (out, code, ix86_compare_op0,
11381                                  ix86_compare_op1, VOIDmode, 0, 1);
11382
11383           nops = 0;
11384           /* On x86_64 the lea instruction operates on Pmode, so we need
11385              to get arithmetics done in proper mode to match.  */
11386           if (diff == 1)
11387             tmp = copy_rtx (out);
11388           else
11389             {
11390               rtx out1;
11391               out1 = copy_rtx (out);
11392               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
11393               nops++;
11394               if (diff & 1)
11395                 {
11396                   tmp = gen_rtx_PLUS (mode, tmp, out1);
11397                   nops++;
11398                 }
11399             }
11400           if (cf != 0)
11401             {
11402               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
11403               nops++;
11404             }
11405           if (!rtx_equal_p (tmp, out))
11406             {
11407               if (nops == 1)
11408                 out = force_operand (tmp, copy_rtx (out));
11409               else
11410                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
11411             }
11412           if (!rtx_equal_p (out, operands[0]))
11413             emit_move_insn (operands[0], copy_rtx (out));
11414
11415           return 1; /* DONE */
11416         }
11417
11418       /*
11419        * General case:                  Jumpful:
11420        *   xorl dest,dest               cmpl op1, op2
11421        *   cmpl op1, op2                movl ct, dest
11422        *   setcc dest                   jcc 1f
11423        *   decl dest                    movl cf, dest
11424        *   andl (cf-ct),dest            1:
11425        *   addl ct,dest
11426        *
11427        * Size 20.                       Size 14.
11428        *
11429        * This is reasonably steep, but branch mispredict costs are
11430        * high on modern cpus, so consider failing only if optimizing
11431        * for space.
11432        */
11433
11434       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11435           && BRANCH_COST >= 2)
11436         {
11437           if (cf == 0)
11438             {
11439               cf = ct;
11440               ct = 0;
11441               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11442                 /* We may be reversing unordered compare to normal compare,
11443                    that is not valid in general (we may convert non-trapping
11444                    condition to trapping one), however on i386 we currently
11445                    emit all comparisons unordered.  */
11446                 code = reverse_condition_maybe_unordered (code);
11447               else
11448                 {
11449                   code = reverse_condition (code);
11450                   if (compare_code != UNKNOWN)
11451                     compare_code = reverse_condition (compare_code);
11452                 }
11453             }
11454
11455           if (compare_code != UNKNOWN)
11456             {
11457               /* notl op1       (if needed)
11458                  sarl $31, op1
11459                  andl (cf-ct), op1
11460                  addl ct, op1
11461
11462                  For x < 0 (resp. x <= -1) there will be no notl,
11463                  so if possible swap the constants to get rid of the
11464                  complement.
11465                  True/false will be -1/0 while code below (store flag
11466                  followed by decrement) is 0/-1, so the constants need
11467                  to be exchanged once more.  */
11468
11469               if (compare_code == GE || !cf)
11470                 {
11471                   code = reverse_condition (code);
11472                   compare_code = LT;
11473                 }
11474               else
11475                 {
11476                   HOST_WIDE_INT tmp = cf;
11477                   cf = ct;
11478                   ct = tmp;
11479                 }
11480
11481               out = emit_store_flag (out, code, ix86_compare_op0,
11482                                      ix86_compare_op1, VOIDmode, 0, -1);
11483             }
11484           else
11485             {
11486               out = emit_store_flag (out, code, ix86_compare_op0,
11487                                      ix86_compare_op1, VOIDmode, 0, 1);
11488
11489               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
11490                                          copy_rtx (out), 1, OPTAB_DIRECT);
11491             }
11492
11493           out = expand_simple_binop (mode, AND, copy_rtx (out),
11494                                      gen_int_mode (cf - ct, mode),
11495                                      copy_rtx (out), 1, OPTAB_DIRECT);
11496           if (ct)
11497             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
11498                                        copy_rtx (out), 1, OPTAB_DIRECT);
11499           if (!rtx_equal_p (out, operands[0]))
11500             emit_move_insn (operands[0], copy_rtx (out));
11501
11502           return 1; /* DONE */
11503         }
11504     }
11505
11506   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11507     {
11508       /* Try a few things more with specific constants and a variable.  */
11509
11510       optab op;
11511       rtx var, orig_out, out, tmp;
11512
11513       if (BRANCH_COST <= 2)
11514         return 0; /* FAIL */
11515
11516       /* If one of the two operands is an interesting constant, load a
11517          constant with the above and mask it in with a logical operation.  */
11518
11519       if (GET_CODE (operands[2]) == CONST_INT)
11520         {
11521           var = operands[3];
11522           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
11523             operands[3] = constm1_rtx, op = and_optab;
11524           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
11525             operands[3] = const0_rtx, op = ior_optab;
11526           else
11527             return 0; /* FAIL */
11528         }
11529       else if (GET_CODE (operands[3]) == CONST_INT)
11530         {
11531           var = operands[2];
11532           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
11533             operands[2] = constm1_rtx, op = and_optab;
11534           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
11535             operands[2] = const0_rtx, op = ior_optab;
11536           else
11537             return 0; /* FAIL */
11538         }
11539       else
11540         return 0; /* FAIL */
11541
11542       orig_out = operands[0];
11543       tmp = gen_reg_rtx (mode);
11544       operands[0] = tmp;
11545
11546       /* Recurse to get the constant loaded.  */
11547       if (ix86_expand_int_movcc (operands) == 0)
11548         return 0; /* FAIL */
11549
11550       /* Mask in the interesting variable.  */
11551       out = expand_binop (mode, op, var, tmp, orig_out, 0,
11552                           OPTAB_WIDEN);
11553       if (!rtx_equal_p (out, orig_out))
11554         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
11555
11556       return 1; /* DONE */
11557     }
11558
11559   /*
11560    * For comparison with above,
11561    *
11562    * movl cf,dest
11563    * movl ct,tmp
11564    * cmpl op1,op2
11565    * cmovcc tmp,dest
11566    *
11567    * Size 15.
11568    */
11569
11570   if (! nonimmediate_operand (operands[2], mode))
11571     operands[2] = force_reg (mode, operands[2]);
11572   if (! nonimmediate_operand (operands[3], mode))
11573     operands[3] = force_reg (mode, operands[3]);
11574
11575   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11576     {
11577       rtx tmp = gen_reg_rtx (mode);
11578       emit_move_insn (tmp, operands[3]);
11579       operands[3] = tmp;
11580     }
11581   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11582     {
11583       rtx tmp = gen_reg_rtx (mode);
11584       emit_move_insn (tmp, operands[2]);
11585       operands[2] = tmp;
11586     }
11587
11588   if (! register_operand (operands[2], VOIDmode)
11589       && (mode == QImode
11590           || ! register_operand (operands[3], VOIDmode)))
11591     operands[2] = force_reg (mode, operands[2]);
11592
11593   if (mode == QImode
11594       && ! register_operand (operands[3], VOIDmode))
11595     operands[3] = force_reg (mode, operands[3]);
11596
11597   emit_insn (compare_seq);
11598   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11599                           gen_rtx_IF_THEN_ELSE (mode,
11600                                                 compare_op, operands[2],
11601                                                 operands[3])));
11602   if (bypass_test)
11603     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11604                             gen_rtx_IF_THEN_ELSE (mode,
11605                                   bypass_test,
11606                                   copy_rtx (operands[3]),
11607                                   copy_rtx (operands[0]))));
11608   if (second_test)
11609     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11610                             gen_rtx_IF_THEN_ELSE (mode,
11611                                   second_test,
11612                                   copy_rtx (operands[2]),
11613                                   copy_rtx (operands[0]))));
11614
11615   return 1; /* DONE */
11616 }
11617
11618 /* Swap, force into registers, or otherwise massage the two operands
11619    to an sse comparison with a mask result.  Thus we differ a bit from
11620    ix86_prepare_fp_compare_args which expects to produce a flags result.
11621
11622    The DEST operand exists to help determine whether to commute commutative
11623    operators.  The POP0/POP1 operands are updated in place.  The new
11624    comparison code is returned, or UNKNOWN if not implementable.  */
11625
11626 static enum rtx_code
11627 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
11628                                   rtx *pop0, rtx *pop1)
11629 {
11630   rtx tmp;
11631
11632   switch (code)
11633     {
11634     case LTGT:
11635     case UNEQ:
11636       /* We have no LTGT as an operator.  We could implement it with
11637          NE & ORDERED, but this requires an extra temporary.  It's
11638          not clear that it's worth it.  */
11639       return UNKNOWN;
11640
11641     case LT:
11642     case LE:
11643     case UNGT:
11644     case UNGE:
11645       /* These are supported directly.  */
11646       break;
11647
11648     case EQ:
11649     case NE:
11650     case UNORDERED:
11651     case ORDERED:
11652       /* For commutative operators, try to canonicalize the destination
11653          operand to be first in the comparison - this helps reload to
11654          avoid extra moves.  */
11655       if (!dest || !rtx_equal_p (dest, *pop1))
11656         break;
11657       /* FALLTHRU */
11658
11659     case GE:
11660     case GT:
11661     case UNLE:
11662     case UNLT:
11663       /* These are not supported directly.  Swap the comparison operands
11664          to transform into something that is supported.  */
11665       tmp = *pop0;
11666       *pop0 = *pop1;
11667       *pop1 = tmp;
11668       code = swap_condition (code);
11669       break;
11670
11671     default:
11672       gcc_unreachable ();
11673     }
11674
11675   return code;
11676 }
11677
11678 /* Detect conditional moves that exactly match min/max operational
11679    semantics.  Note that this is IEEE safe, as long as we don't
11680    interchange the operands.
11681
11682    Returns FALSE if this conditional move doesn't match a MIN/MAX,
11683    and TRUE if the operation is successful and instructions are emitted.  */
11684
11685 static bool
11686 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
11687                            rtx cmp_op1, rtx if_true, rtx if_false)
11688 {
11689   enum machine_mode mode;
11690   bool is_min;
11691   rtx tmp;
11692
11693   if (code == LT)
11694     ;
11695   else if (code == UNGE)
11696     {
11697       tmp = if_true;
11698       if_true = if_false;
11699       if_false = tmp;
11700     }
11701   else
11702     return false;
11703
11704   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
11705     is_min = true;
11706   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
11707     is_min = false;
11708   else
11709     return false;
11710
11711   mode = GET_MODE (dest);
11712
11713   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
11714      but MODE may be a vector mode and thus not appropriate.  */
11715   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
11716     {
11717       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
11718       rtvec v;
11719
11720       if_true = force_reg (mode, if_true);
11721       v = gen_rtvec (2, if_true, if_false);
11722       tmp = gen_rtx_UNSPEC (mode, v, u);
11723     }
11724   else
11725     {
11726       code = is_min ? SMIN : SMAX;
11727       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
11728     }
11729
11730   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
11731   return true;
11732 }
11733
11734 /* Expand an sse vector comparison.  Return the register with the result.  */
11735
11736 static rtx
11737 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
11738                      rtx op_true, rtx op_false)
11739 {
11740   enum machine_mode mode = GET_MODE (dest);
11741   rtx x;
11742
11743   cmp_op0 = force_reg (mode, cmp_op0);
11744   if (!nonimmediate_operand (cmp_op1, mode))
11745     cmp_op1 = force_reg (mode, cmp_op1);
11746
11747   if (optimize
11748       || reg_overlap_mentioned_p (dest, op_true)
11749       || reg_overlap_mentioned_p (dest, op_false))
11750     dest = gen_reg_rtx (mode);
11751
11752   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
11753   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11754
11755   return dest;
11756 }
11757
11758 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
11759    operations.  This is used for both scalar and vector conditional moves.  */
11760
11761 static void
11762 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
11763 {
11764   enum machine_mode mode = GET_MODE (dest);
11765   rtx t2, t3, x;
11766
11767   if (op_false == CONST0_RTX (mode))
11768     {
11769       op_true = force_reg (mode, op_true);
11770       x = gen_rtx_AND (mode, cmp, op_true);
11771       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11772     }
11773   else if (op_true == CONST0_RTX (mode))
11774     {
11775       op_false = force_reg (mode, op_false);
11776       x = gen_rtx_NOT (mode, cmp);
11777       x = gen_rtx_AND (mode, x, op_false);
11778       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11779     }
11780   else
11781     {
11782       op_true = force_reg (mode, op_true);
11783       op_false = force_reg (mode, op_false);
11784
11785       t2 = gen_reg_rtx (mode);
11786       if (optimize)
11787         t3 = gen_reg_rtx (mode);
11788       else
11789         t3 = dest;
11790
11791       x = gen_rtx_AND (mode, op_true, cmp);
11792       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
11793
11794       x = gen_rtx_NOT (mode, cmp);
11795       x = gen_rtx_AND (mode, x, op_false);
11796       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
11797
11798       x = gen_rtx_IOR (mode, t3, t2);
11799       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11800     }
11801 }
11802
11803 /* Expand a floating-point conditional move.  Return true if successful.  */
11804
11805 int
11806 ix86_expand_fp_movcc (rtx operands[])
11807 {
11808   enum machine_mode mode = GET_MODE (operands[0]);
11809   enum rtx_code code = GET_CODE (operands[1]);
11810   rtx tmp, compare_op, second_test, bypass_test;
11811
11812   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
11813     {
11814       enum machine_mode cmode;
11815
11816       /* Since we've no cmove for sse registers, don't force bad register
11817          allocation just to gain access to it.  Deny movcc when the
11818          comparison mode doesn't match the move mode.  */
11819       cmode = GET_MODE (ix86_compare_op0);
11820       if (cmode == VOIDmode)
11821         cmode = GET_MODE (ix86_compare_op1);
11822       if (cmode != mode)
11823         return 0;
11824
11825       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11826                                                &ix86_compare_op0,
11827                                                &ix86_compare_op1);
11828       if (code == UNKNOWN)
11829         return 0;
11830
11831       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
11832                                      ix86_compare_op1, operands[2],
11833                                      operands[3]))
11834         return 1;
11835
11836       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
11837                                  ix86_compare_op1, operands[2], operands[3]);
11838       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
11839       return 1;
11840     }
11841
11842   /* The floating point conditional move instructions don't directly
11843      support conditions resulting from a signed integer comparison.  */
11844
11845   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11846
11847   /* The floating point conditional move instructions don't directly
11848      support signed integer comparisons.  */
11849
11850   if (!fcmov_comparison_operator (compare_op, VOIDmode))
11851     {
11852       gcc_assert (!second_test && !bypass_test);
11853       tmp = gen_reg_rtx (QImode);
11854       ix86_expand_setcc (code, tmp);
11855       code = NE;
11856       ix86_compare_op0 = tmp;
11857       ix86_compare_op1 = const0_rtx;
11858       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
11859     }
11860   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11861     {
11862       tmp = gen_reg_rtx (mode);
11863       emit_move_insn (tmp, operands[3]);
11864       operands[3] = tmp;
11865     }
11866   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11867     {
11868       tmp = gen_reg_rtx (mode);
11869       emit_move_insn (tmp, operands[2]);
11870       operands[2] = tmp;
11871     }
11872
11873   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11874                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
11875                                                 operands[2], operands[3])));
11876   if (bypass_test)
11877     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11878                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
11879                                                   operands[3], operands[0])));
11880   if (second_test)
11881     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11882                             gen_rtx_IF_THEN_ELSE (mode, second_test,
11883                                                   operands[2], operands[0])));
11884
11885   return 1;
11886 }
11887
11888 /* Expand a floating-point vector conditional move; a vcond operation
11889    rather than a movcc operation.  */
11890
11891 bool
11892 ix86_expand_fp_vcond (rtx operands[])
11893 {
11894   enum rtx_code code = GET_CODE (operands[3]);
11895   rtx cmp;
11896
11897   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11898                                            &operands[4], &operands[5]);
11899   if (code == UNKNOWN)
11900     return false;
11901
11902   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
11903                                  operands[5], operands[1], operands[2]))
11904     return true;
11905
11906   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
11907                              operands[1], operands[2]);
11908   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
11909   return true;
11910 }
11911
11912 /* Expand a signed integral vector conditional move.  */
11913
11914 bool
11915 ix86_expand_int_vcond (rtx operands[])
11916 {
11917   enum machine_mode mode = GET_MODE (operands[0]);
11918   enum rtx_code code = GET_CODE (operands[3]);
11919   bool negate = false;
11920   rtx x, cop0, cop1;
11921
11922   cop0 = operands[4];
11923   cop1 = operands[5];
11924
11925   /* Canonicalize the comparison to EQ, GT, GTU.  */
11926   switch (code)
11927     {
11928     case EQ:
11929     case GT:
11930     case GTU:
11931       break;
11932
11933     case NE:
11934     case LE:
11935     case LEU:
11936       code = reverse_condition (code);
11937       negate = true;
11938       break;
11939
11940     case GE:
11941     case GEU:
11942       code = reverse_condition (code);
11943       negate = true;
11944       /* FALLTHRU */
11945
11946     case LT:
11947     case LTU:
11948       code = swap_condition (code);
11949       x = cop0, cop0 = cop1, cop1 = x;
11950       break;
11951
11952     default:
11953       gcc_unreachable ();
11954     }
11955
11956   /* Unsigned parallel compare is not supported by the hardware.  Play some
11957      tricks to turn this into a signed comparison against 0.  */
11958   if (code == GTU)
11959     {
11960       cop0 = force_reg (mode, cop0);
11961
11962       switch (mode)
11963         {
11964         case V4SImode:
11965           {
11966             rtx t1, t2, mask;
11967
11968             /* Perform a parallel modulo subtraction.  */
11969             t1 = gen_reg_rtx (mode);
11970             emit_insn (gen_subv4si3 (t1, cop0, cop1));
11971
11972             /* Extract the original sign bit of op0.  */
11973             mask = GEN_INT (-0x80000000);
11974             mask = gen_rtx_CONST_VECTOR (mode,
11975                         gen_rtvec (4, mask, mask, mask, mask));
11976             mask = force_reg (mode, mask);
11977             t2 = gen_reg_rtx (mode);
11978             emit_insn (gen_andv4si3 (t2, cop0, mask));
11979
11980             /* XOR it back into the result of the subtraction.  This results
11981                in the sign bit set iff we saw unsigned underflow.  */
11982             x = gen_reg_rtx (mode);
11983             emit_insn (gen_xorv4si3 (x, t1, t2));
11984
11985             code = GT;
11986           }
11987           break;
11988
11989         case V16QImode:
11990         case V8HImode:
11991           /* Perform a parallel unsigned saturating subtraction.  */
11992           x = gen_reg_rtx (mode);
11993           emit_insn (gen_rtx_SET (VOIDmode, x,
11994                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
11995
11996           code = EQ;
11997           negate = !negate;
11998           break;
11999
12000         default:
12001           gcc_unreachable ();
12002         }
12003
12004       cop0 = x;
12005       cop1 = CONST0_RTX (mode);
12006     }
12007
12008   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
12009                            operands[1+negate], operands[2-negate]);
12010
12011   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
12012                          operands[2-negate]);
12013   return true;
12014 }
12015
12016 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
12017    true if we should do zero extension, else sign extension.  HIGH_P is
12018    true if we want the N/2 high elements, else the low elements.  */
12019
12020 void
12021 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
12022 {
12023   enum machine_mode imode = GET_MODE (operands[1]);
12024   rtx (*unpack)(rtx, rtx, rtx);
12025   rtx se, dest;
12026
12027   switch (imode)
12028     {
12029     case V16QImode:
12030       if (high_p)
12031         unpack = gen_vec_interleave_highv16qi;
12032       else
12033         unpack = gen_vec_interleave_lowv16qi;
12034       break;
12035     case V8HImode:
12036       if (high_p)
12037         unpack = gen_vec_interleave_highv8hi;
12038       else
12039         unpack = gen_vec_interleave_lowv8hi;
12040       break;
12041     case V4SImode:
12042       if (high_p)
12043         unpack = gen_vec_interleave_highv4si;
12044       else 
12045         unpack = gen_vec_interleave_lowv4si;
12046       break;
12047     default:
12048       gcc_unreachable (); 
12049     }
12050
12051   dest = gen_lowpart (imode, operands[0]);
12052
12053   if (unsigned_p)
12054     se = force_reg (imode, CONST0_RTX (imode));
12055   else
12056     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
12057                               operands[1], pc_rtx, pc_rtx);
12058
12059   emit_insn (unpack (dest, operands[1], se));
12060 }
12061
12062 /* Expand conditional increment or decrement using adb/sbb instructions.
12063    The default case using setcc followed by the conditional move can be
12064    done by generic code.  */
12065 int
12066 ix86_expand_int_addcc (rtx operands[])
12067 {
12068   enum rtx_code code = GET_CODE (operands[1]);
12069   rtx compare_op;
12070   rtx val = const0_rtx;
12071   bool fpcmp = false;
12072   enum machine_mode mode = GET_MODE (operands[0]);
12073
12074   if (operands[3] != const1_rtx
12075       && operands[3] != constm1_rtx)
12076     return 0;
12077   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
12078                                        ix86_compare_op1, &compare_op))
12079      return 0;
12080   code = GET_CODE (compare_op);
12081
12082   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12083       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12084     {
12085       fpcmp = true;
12086       code = ix86_fp_compare_code_to_integer (code);
12087     }
12088
12089   if (code != LTU)
12090     {
12091       val = constm1_rtx;
12092       if (fpcmp)
12093         PUT_CODE (compare_op,
12094                   reverse_condition_maybe_unordered
12095                     (GET_CODE (compare_op)));
12096       else
12097         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
12098     }
12099   PUT_MODE (compare_op, mode);
12100
12101   /* Construct either adc or sbb insn.  */
12102   if ((code == LTU) == (operands[3] == constm1_rtx))
12103     {
12104       switch (GET_MODE (operands[0]))
12105         {
12106           case QImode:
12107             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
12108             break;
12109           case HImode:
12110             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
12111             break;
12112           case SImode:
12113             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
12114             break;
12115           case DImode:
12116             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12117             break;
12118           default:
12119             gcc_unreachable ();
12120         }
12121     }
12122   else
12123     {
12124       switch (GET_MODE (operands[0]))
12125         {
12126           case QImode:
12127             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
12128             break;
12129           case HImode:
12130             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
12131             break;
12132           case SImode:
12133             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
12134             break;
12135           case DImode:
12136             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12137             break;
12138           default:
12139             gcc_unreachable ();
12140         }
12141     }
12142   return 1; /* DONE */
12143 }
12144
12145
12146 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
12147    works for floating pointer parameters and nonoffsetable memories.
12148    For pushes, it returns just stack offsets; the values will be saved
12149    in the right order.  Maximally three parts are generated.  */
12150
12151 static int
12152 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
12153 {
12154   int size;
12155
12156   if (!TARGET_64BIT)
12157     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
12158   else
12159     size = (GET_MODE_SIZE (mode) + 4) / 8;
12160
12161   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
12162   gcc_assert (size >= 2 && size <= 3);
12163
12164   /* Optimize constant pool reference to immediates.  This is used by fp
12165      moves, that force all constants to memory to allow combining.  */
12166   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
12167     {
12168       rtx tmp = maybe_get_pool_constant (operand);
12169       if (tmp)
12170         operand = tmp;
12171     }
12172
12173   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
12174     {
12175       /* The only non-offsetable memories we handle are pushes.  */
12176       int ok = push_operand (operand, VOIDmode);
12177
12178       gcc_assert (ok);
12179
12180       operand = copy_rtx (operand);
12181       PUT_MODE (operand, Pmode);
12182       parts[0] = parts[1] = parts[2] = operand;
12183       return size;
12184     }
12185
12186   if (GET_CODE (operand) == CONST_VECTOR)
12187     {
12188       enum machine_mode imode = int_mode_for_mode (mode);
12189       /* Caution: if we looked through a constant pool memory above,
12190          the operand may actually have a different mode now.  That's
12191          ok, since we want to pun this all the way back to an integer.  */
12192       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
12193       gcc_assert (operand != NULL);
12194       mode = imode;
12195     }
12196
12197   if (!TARGET_64BIT)
12198     {
12199       if (mode == DImode)
12200         split_di (&operand, 1, &parts[0], &parts[1]);
12201       else
12202         {
12203           if (REG_P (operand))
12204             {
12205               gcc_assert (reload_completed);
12206               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
12207               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
12208               if (size == 3)
12209                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
12210             }
12211           else if (offsettable_memref_p (operand))
12212             {
12213               operand = adjust_address (operand, SImode, 0);
12214               parts[0] = operand;
12215               parts[1] = adjust_address (operand, SImode, 4);
12216               if (size == 3)
12217                 parts[2] = adjust_address (operand, SImode, 8);
12218             }
12219           else if (GET_CODE (operand) == CONST_DOUBLE)
12220             {
12221               REAL_VALUE_TYPE r;
12222               long l[4];
12223
12224               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12225               switch (mode)
12226                 {
12227                 case XFmode:
12228                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
12229                   parts[2] = gen_int_mode (l[2], SImode);
12230                   break;
12231                 case DFmode:
12232                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
12233                   break;
12234                 default:
12235                   gcc_unreachable ();
12236                 }
12237               parts[1] = gen_int_mode (l[1], SImode);
12238               parts[0] = gen_int_mode (l[0], SImode);
12239             }
12240           else
12241             gcc_unreachable ();
12242         }
12243     }
12244   else
12245     {
12246       if (mode == TImode)
12247         split_ti (&operand, 1, &parts[0], &parts[1]);
12248       if (mode == XFmode || mode == TFmode)
12249         {
12250           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
12251           if (REG_P (operand))
12252             {
12253               gcc_assert (reload_completed);
12254               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
12255               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
12256             }
12257           else if (offsettable_memref_p (operand))
12258             {
12259               operand = adjust_address (operand, DImode, 0);
12260               parts[0] = operand;
12261               parts[1] = adjust_address (operand, upper_mode, 8);
12262             }
12263           else if (GET_CODE (operand) == CONST_DOUBLE)
12264             {
12265               REAL_VALUE_TYPE r;
12266               long l[4];
12267
12268               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12269               real_to_target (l, &r, mode);
12270
12271               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
12272               if (HOST_BITS_PER_WIDE_INT >= 64)
12273                 parts[0]
12274                   = gen_int_mode
12275                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
12276                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
12277                        DImode);
12278               else
12279                 parts[0] = immed_double_const (l[0], l[1], DImode);
12280
12281               if (upper_mode == SImode)
12282                 parts[1] = gen_int_mode (l[2], SImode);
12283               else if (HOST_BITS_PER_WIDE_INT >= 64)
12284                 parts[1]
12285                   = gen_int_mode
12286                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
12287                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
12288                        DImode);
12289               else
12290                 parts[1] = immed_double_const (l[2], l[3], DImode);
12291             }
12292           else
12293             gcc_unreachable ();
12294         }
12295     }
12296
12297   return size;
12298 }
12299
12300 /* Emit insns to perform a move or push of DI, DF, and XF values.
12301    Return false when normal moves are needed; true when all required
12302    insns have been emitted.  Operands 2-4 contain the input values
12303    int the correct order; operands 5-7 contain the output values.  */
12304
12305 void
12306 ix86_split_long_move (rtx operands[])
12307 {
12308   rtx part[2][3];
12309   int nparts;
12310   int push = 0;
12311   int collisions = 0;
12312   enum machine_mode mode = GET_MODE (operands[0]);
12313
12314   /* The DFmode expanders may ask us to move double.
12315      For 64bit target this is single move.  By hiding the fact
12316      here we simplify i386.md splitters.  */
12317   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
12318     {
12319       /* Optimize constant pool reference to immediates.  This is used by
12320          fp moves, that force all constants to memory to allow combining.  */
12321
12322       if (GET_CODE (operands[1]) == MEM
12323           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
12324           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
12325         operands[1] = get_pool_constant (XEXP (operands[1], 0));
12326       if (push_operand (operands[0], VOIDmode))
12327         {
12328           operands[0] = copy_rtx (operands[0]);
12329           PUT_MODE (operands[0], Pmode);
12330         }
12331       else
12332         operands[0] = gen_lowpart (DImode, operands[0]);
12333       operands[1] = gen_lowpart (DImode, operands[1]);
12334       emit_move_insn (operands[0], operands[1]);
12335       return;
12336     }
12337
12338   /* The only non-offsettable memory we handle is push.  */
12339   if (push_operand (operands[0], VOIDmode))
12340     push = 1;
12341   else
12342     gcc_assert (GET_CODE (operands[0]) != MEM
12343                 || offsettable_memref_p (operands[0]));
12344
12345   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
12346   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
12347
12348   /* When emitting push, take care for source operands on the stack.  */
12349   if (push && GET_CODE (operands[1]) == MEM
12350       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
12351     {
12352       if (nparts == 3)
12353         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
12354                                      XEXP (part[1][2], 0));
12355       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
12356                                    XEXP (part[1][1], 0));
12357     }
12358
12359   /* We need to do copy in the right order in case an address register
12360      of the source overlaps the destination.  */
12361   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
12362     {
12363       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
12364         collisions++;
12365       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12366         collisions++;
12367       if (nparts == 3
12368           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
12369         collisions++;
12370
12371       /* Collision in the middle part can be handled by reordering.  */
12372       if (collisions == 1 && nparts == 3
12373           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12374         {
12375           rtx tmp;
12376           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
12377           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
12378         }
12379
12380       /* If there are more collisions, we can't handle it by reordering.
12381          Do an lea to the last part and use only one colliding move.  */
12382       else if (collisions > 1)
12383         {
12384           rtx base;
12385
12386           collisions = 1;
12387
12388           base = part[0][nparts - 1];
12389
12390           /* Handle the case when the last part isn't valid for lea.
12391              Happens in 64-bit mode storing the 12-byte XFmode.  */
12392           if (GET_MODE (base) != Pmode)
12393             base = gen_rtx_REG (Pmode, REGNO (base));
12394
12395           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
12396           part[1][0] = replace_equiv_address (part[1][0], base);
12397           part[1][1] = replace_equiv_address (part[1][1],
12398                                       plus_constant (base, UNITS_PER_WORD));
12399           if (nparts == 3)
12400             part[1][2] = replace_equiv_address (part[1][2],
12401                                       plus_constant (base, 8));
12402         }
12403     }
12404
12405   if (push)
12406     {
12407       if (!TARGET_64BIT)
12408         {
12409           if (nparts == 3)
12410             {
12411               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
12412                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
12413               emit_move_insn (part[0][2], part[1][2]);
12414             }
12415         }
12416       else
12417         {
12418           /* In 64bit mode we don't have 32bit push available.  In case this is
12419              register, it is OK - we will just use larger counterpart.  We also
12420              retype memory - these comes from attempt to avoid REX prefix on
12421              moving of second half of TFmode value.  */
12422           if (GET_MODE (part[1][1]) == SImode)
12423             {
12424               switch (GET_CODE (part[1][1]))
12425                 {
12426                 case MEM:
12427                   part[1][1] = adjust_address (part[1][1], DImode, 0);
12428                   break;
12429
12430                 case REG:
12431                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
12432                   break;
12433
12434                 default:
12435                   gcc_unreachable ();
12436                 }
12437
12438               if (GET_MODE (part[1][0]) == SImode)
12439                 part[1][0] = part[1][1];
12440             }
12441         }
12442       emit_move_insn (part[0][1], part[1][1]);
12443       emit_move_insn (part[0][0], part[1][0]);
12444       return;
12445     }
12446
12447   /* Choose correct order to not overwrite the source before it is copied.  */
12448   if ((REG_P (part[0][0])
12449        && REG_P (part[1][1])
12450        && (REGNO (part[0][0]) == REGNO (part[1][1])
12451            || (nparts == 3
12452                && REGNO (part[0][0]) == REGNO (part[1][2]))))
12453       || (collisions > 0
12454           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
12455     {
12456       if (nparts == 3)
12457         {
12458           operands[2] = part[0][2];
12459           operands[3] = part[0][1];
12460           operands[4] = part[0][0];
12461           operands[5] = part[1][2];
12462           operands[6] = part[1][1];
12463           operands[7] = part[1][0];
12464         }
12465       else
12466         {
12467           operands[2] = part[0][1];
12468           operands[3] = part[0][0];
12469           operands[5] = part[1][1];
12470           operands[6] = part[1][0];
12471         }
12472     }
12473   else
12474     {
12475       if (nparts == 3)
12476         {
12477           operands[2] = part[0][0];
12478           operands[3] = part[0][1];
12479           operands[4] = part[0][2];
12480           operands[5] = part[1][0];
12481           operands[6] = part[1][1];
12482           operands[7] = part[1][2];
12483         }
12484       else
12485         {
12486           operands[2] = part[0][0];
12487           operands[3] = part[0][1];
12488           operands[5] = part[1][0];
12489           operands[6] = part[1][1];
12490         }
12491     }
12492
12493   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
12494   if (optimize_size)
12495     {
12496       if (GET_CODE (operands[5]) == CONST_INT
12497           && operands[5] != const0_rtx
12498           && REG_P (operands[2]))
12499         {
12500           if (GET_CODE (operands[6]) == CONST_INT
12501               && INTVAL (operands[6]) == INTVAL (operands[5]))
12502             operands[6] = operands[2];
12503
12504           if (nparts == 3
12505               && GET_CODE (operands[7]) == CONST_INT
12506               && INTVAL (operands[7]) == INTVAL (operands[5]))
12507             operands[7] = operands[2];
12508         }
12509
12510       if (nparts == 3
12511           && GET_CODE (operands[6]) == CONST_INT
12512           && operands[6] != const0_rtx
12513           && REG_P (operands[3])
12514           && GET_CODE (operands[7]) == CONST_INT
12515           && INTVAL (operands[7]) == INTVAL (operands[6]))
12516         operands[7] = operands[3];
12517     }
12518
12519   emit_move_insn (operands[2], operands[5]);
12520   emit_move_insn (operands[3], operands[6]);
12521   if (nparts == 3)
12522     emit_move_insn (operands[4], operands[7]);
12523
12524   return;
12525 }
12526
12527 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
12528    left shift by a constant, either using a single shift or
12529    a sequence of add instructions.  */
12530
12531 static void
12532 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
12533 {
12534   if (count == 1)
12535     {
12536       emit_insn ((mode == DImode
12537                   ? gen_addsi3
12538                   : gen_adddi3) (operand, operand, operand));
12539     }
12540   else if (!optimize_size
12541            && count * ix86_cost->add <= ix86_cost->shift_const)
12542     {
12543       int i;
12544       for (i=0; i<count; i++)
12545         {
12546           emit_insn ((mode == DImode
12547                       ? gen_addsi3
12548                       : gen_adddi3) (operand, operand, operand));
12549         }
12550     }
12551   else
12552     emit_insn ((mode == DImode
12553                 ? gen_ashlsi3
12554                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
12555 }
12556
12557 void
12558 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
12559 {
12560   rtx low[2], high[2];
12561   int count;
12562   const int single_width = mode == DImode ? 32 : 64;
12563
12564   if (GET_CODE (operands[2]) == CONST_INT)
12565     {
12566       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12567       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12568
12569       if (count >= single_width)
12570         {
12571           emit_move_insn (high[0], low[1]);
12572           emit_move_insn (low[0], const0_rtx);
12573
12574           if (count > single_width)
12575             ix86_expand_ashl_const (high[0], count - single_width, mode);
12576         }
12577       else
12578         {
12579           if (!rtx_equal_p (operands[0], operands[1]))
12580             emit_move_insn (operands[0], operands[1]);
12581           emit_insn ((mode == DImode
12582                      ? gen_x86_shld_1
12583                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
12584           ix86_expand_ashl_const (low[0], count, mode);
12585         }
12586       return;
12587     }
12588
12589   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12590
12591   if (operands[1] == const1_rtx)
12592     {
12593       /* Assuming we've chosen a QImode capable registers, then 1 << N
12594          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
12595       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
12596         {
12597           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
12598
12599           ix86_expand_clear (low[0]);
12600           ix86_expand_clear (high[0]);
12601           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
12602
12603           d = gen_lowpart (QImode, low[0]);
12604           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12605           s = gen_rtx_EQ (QImode, flags, const0_rtx);
12606           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12607
12608           d = gen_lowpart (QImode, high[0]);
12609           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12610           s = gen_rtx_NE (QImode, flags, const0_rtx);
12611           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12612         }
12613
12614       /* Otherwise, we can get the same results by manually performing
12615          a bit extract operation on bit 5/6, and then performing the two
12616          shifts.  The two methods of getting 0/1 into low/high are exactly
12617          the same size.  Avoiding the shift in the bit extract case helps
12618          pentium4 a bit; no one else seems to care much either way.  */
12619       else
12620         {
12621           rtx x;
12622
12623           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
12624             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
12625           else
12626             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
12627           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
12628
12629           emit_insn ((mode == DImode
12630                       ? gen_lshrsi3
12631                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
12632           emit_insn ((mode == DImode
12633                       ? gen_andsi3
12634                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
12635           emit_move_insn (low[0], high[0]);
12636           emit_insn ((mode == DImode
12637                       ? gen_xorsi3
12638                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
12639         }
12640
12641       emit_insn ((mode == DImode
12642                     ? gen_ashlsi3
12643                     : gen_ashldi3) (low[0], low[0], operands[2]));
12644       emit_insn ((mode == DImode
12645                     ? gen_ashlsi3
12646                     : gen_ashldi3) (high[0], high[0], operands[2]));
12647       return;
12648     }
12649
12650   if (operands[1] == constm1_rtx)
12651     {
12652       /* For -1 << N, we can avoid the shld instruction, because we
12653          know that we're shifting 0...31/63 ones into a -1.  */
12654       emit_move_insn (low[0], constm1_rtx);
12655       if (optimize_size)
12656         emit_move_insn (high[0], low[0]);
12657       else
12658         emit_move_insn (high[0], constm1_rtx);
12659     }
12660   else
12661     {
12662       if (!rtx_equal_p (operands[0], operands[1]))
12663         emit_move_insn (operands[0], operands[1]);
12664
12665       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12666       emit_insn ((mode == DImode
12667                   ? gen_x86_shld_1
12668                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
12669     }
12670
12671   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
12672
12673   if (TARGET_CMOVE && scratch)
12674     {
12675       ix86_expand_clear (scratch);
12676       emit_insn ((mode == DImode
12677                   ? gen_x86_shift_adj_1
12678                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
12679     }
12680   else
12681     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
12682 }
12683
12684 void
12685 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
12686 {
12687   rtx low[2], high[2];
12688   int count;
12689   const int single_width = mode == DImode ? 32 : 64;
12690
12691   if (GET_CODE (operands[2]) == CONST_INT)
12692     {
12693       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12694       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12695
12696       if (count == single_width * 2 - 1)
12697         {
12698           emit_move_insn (high[0], high[1]);
12699           emit_insn ((mode == DImode
12700                       ? gen_ashrsi3
12701                       : gen_ashrdi3) (high[0], high[0],
12702                                       GEN_INT (single_width - 1)));
12703           emit_move_insn (low[0], high[0]);
12704
12705         }
12706       else if (count >= single_width)
12707         {
12708           emit_move_insn (low[0], high[1]);
12709           emit_move_insn (high[0], low[0]);
12710           emit_insn ((mode == DImode
12711                       ? gen_ashrsi3
12712                       : gen_ashrdi3) (high[0], high[0],
12713                                       GEN_INT (single_width - 1)));
12714           if (count > single_width)
12715             emit_insn ((mode == DImode
12716                         ? gen_ashrsi3
12717                         : gen_ashrdi3) (low[0], low[0],
12718                                         GEN_INT (count - single_width)));
12719         }
12720       else
12721         {
12722           if (!rtx_equal_p (operands[0], operands[1]))
12723             emit_move_insn (operands[0], operands[1]);
12724           emit_insn ((mode == DImode
12725                       ? gen_x86_shrd_1
12726                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12727           emit_insn ((mode == DImode
12728                       ? gen_ashrsi3
12729                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
12730         }
12731     }
12732   else
12733     {
12734       if (!rtx_equal_p (operands[0], operands[1]))
12735         emit_move_insn (operands[0], operands[1]);
12736
12737       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12738
12739       emit_insn ((mode == DImode
12740                   ? gen_x86_shrd_1
12741                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12742       emit_insn ((mode == DImode
12743                   ? gen_ashrsi3
12744                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
12745
12746       if (TARGET_CMOVE && scratch)
12747         {
12748           emit_move_insn (scratch, high[0]);
12749           emit_insn ((mode == DImode
12750                       ? gen_ashrsi3
12751                       : gen_ashrdi3) (scratch, scratch,
12752                                       GEN_INT (single_width - 1)));
12753           emit_insn ((mode == DImode
12754                       ? gen_x86_shift_adj_1
12755                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12756                                          scratch));
12757         }
12758       else
12759         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
12760     }
12761 }
12762
12763 void
12764 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
12765 {
12766   rtx low[2], high[2];
12767   int count;
12768   const int single_width = mode == DImode ? 32 : 64;
12769
12770   if (GET_CODE (operands[2]) == CONST_INT)
12771     {
12772       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12773       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12774
12775       if (count >= single_width)
12776         {
12777           emit_move_insn (low[0], high[1]);
12778           ix86_expand_clear (high[0]);
12779
12780           if (count > single_width)
12781             emit_insn ((mode == DImode
12782                         ? gen_lshrsi3
12783                         : gen_lshrdi3) (low[0], low[0],
12784                                         GEN_INT (count - single_width)));
12785         }
12786       else
12787         {
12788           if (!rtx_equal_p (operands[0], operands[1]))
12789             emit_move_insn (operands[0], operands[1]);
12790           emit_insn ((mode == DImode
12791                       ? gen_x86_shrd_1
12792                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12793           emit_insn ((mode == DImode
12794                       ? gen_lshrsi3
12795                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
12796         }
12797     }
12798   else
12799     {
12800       if (!rtx_equal_p (operands[0], operands[1]))
12801         emit_move_insn (operands[0], operands[1]);
12802
12803       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12804
12805       emit_insn ((mode == DImode
12806                   ? gen_x86_shrd_1
12807                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12808       emit_insn ((mode == DImode
12809                   ? gen_lshrsi3
12810                   : gen_lshrdi3) (high[0], high[0], operands[2]));
12811
12812       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
12813       if (TARGET_CMOVE && scratch)
12814         {
12815           ix86_expand_clear (scratch);
12816           emit_insn ((mode == DImode
12817                       ? gen_x86_shift_adj_1
12818                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12819                                                scratch));
12820         }
12821       else
12822         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
12823     }
12824 }
12825
12826 /* Predict just emitted jump instruction to be taken with probability PROB.  */
12827 static void
12828 predict_jump (int prob)
12829 {
12830   rtx insn = get_last_insn ();
12831   gcc_assert (GET_CODE (insn) == JUMP_INSN);
12832   REG_NOTES (insn)
12833     = gen_rtx_EXPR_LIST (REG_BR_PROB,
12834                          GEN_INT (prob),
12835                          REG_NOTES (insn));
12836 }
12837
12838 /* Helper function for the string operations below.  Dest VARIABLE whether
12839    it is aligned to VALUE bytes.  If true, jump to the label.  */
12840 static rtx
12841 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
12842 {
12843   rtx label = gen_label_rtx ();
12844   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
12845   if (GET_MODE (variable) == DImode)
12846     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
12847   else
12848     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
12849   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
12850                            1, label);
12851   if (epilogue)
12852     predict_jump (REG_BR_PROB_BASE * 50 / 100);
12853   else
12854     predict_jump (REG_BR_PROB_BASE * 90 / 100);
12855   return label;
12856 }
12857
12858 /* Adjust COUNTER by the VALUE.  */
12859 static void
12860 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
12861 {
12862   if (GET_MODE (countreg) == DImode)
12863     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
12864   else
12865     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
12866 }
12867
12868 /* Zero extend possibly SImode EXP to Pmode register.  */
12869 rtx
12870 ix86_zero_extend_to_Pmode (rtx exp)
12871 {
12872   rtx r;
12873   if (GET_MODE (exp) == VOIDmode)
12874     return force_reg (Pmode, exp);
12875   if (GET_MODE (exp) == Pmode)
12876     return copy_to_mode_reg (Pmode, exp);
12877   r = gen_reg_rtx (Pmode);
12878   emit_insn (gen_zero_extendsidi2 (r, exp));
12879   return r;
12880 }
12881
12882 /* Divide COUNTREG by SCALE.  */
12883 static rtx
12884 scale_counter (rtx countreg, int scale)
12885 {
12886   rtx sc;
12887   rtx piece_size_mask;
12888
12889   if (scale == 1)
12890     return countreg;
12891   if (GET_CODE (countreg) == CONST_INT)
12892     return GEN_INT (INTVAL (countreg) / scale);
12893   gcc_assert (REG_P (countreg));
12894
12895   piece_size_mask = GEN_INT (scale - 1);
12896   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
12897                             GEN_INT (exact_log2 (scale)),
12898                             NULL, 1, OPTAB_DIRECT);
12899   return sc;
12900 }
12901
12902 /* When SRCPTR is non-NULL, output simple loop to move memory
12903    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
12904    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
12905    equivalent loop to set memory by VALUE (supposed to be in MODE).
12906
12907    The size is rounded down to whole number of chunk size moved at once.
12908    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
12909   
12910
12911 static void
12912 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
12913                                rtx destptr, rtx srcptr, rtx value,
12914                                rtx count, enum machine_mode mode, int unroll,
12915                                int expected_size)
12916 {
12917   rtx out_label, top_label, iter, tmp;
12918   enum machine_mode iter_mode;
12919   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
12920   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
12921   rtx size;
12922   rtx x_addr;
12923   rtx y_addr;
12924   int i;
12925
12926   iter_mode = GET_MODE (count);
12927   if (iter_mode == VOIDmode)
12928     iter_mode = word_mode;
12929
12930   top_label = gen_label_rtx ();
12931   out_label = gen_label_rtx ();
12932   iter = gen_reg_rtx (iter_mode);
12933
12934   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
12935                               NULL, 1, OPTAB_DIRECT);
12936   /* Those two should combine.  */
12937   if (piece_size == const1_rtx)
12938     {
12939       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
12940                                true, out_label);
12941       predict_jump (REG_BR_PROB_BASE * 10 / 100);
12942     }
12943   emit_move_insn (iter, const0_rtx);
12944
12945   emit_label (top_label);
12946
12947   tmp = convert_modes (Pmode, iter_mode, iter, true);
12948   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
12949   destmem = change_address (destmem, mode, x_addr);
12950
12951   if (srcmem)
12952     {
12953       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
12954       srcmem = change_address (srcmem, mode, y_addr);
12955
12956       /* When unrolling for chips that reorder memory reads and writes,
12957          we can save registers by using single temporary.  
12958          Also using 4 temporaries is overkill in 32bit mode.  */
12959       if (!TARGET_64BIT && 0)
12960         {
12961           for (i = 0; i < unroll; i++)
12962             {
12963               if (i)
12964                 {
12965                   destmem =
12966                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
12967                   srcmem =
12968                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
12969                 }
12970               emit_move_insn (destmem, srcmem);
12971             }
12972         }
12973       else
12974         {
12975           rtx tmpreg[4];
12976           gcc_assert (unroll <= 4);
12977           for (i = 0; i < unroll; i++)
12978             {
12979               tmpreg[i] = gen_reg_rtx (mode);
12980               if (i)
12981                 {
12982                   srcmem =
12983                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
12984                 }
12985               emit_move_insn (tmpreg[i], srcmem);
12986             }
12987           for (i = 0; i < unroll; i++)
12988             {
12989               if (i)
12990                 {
12991                   destmem =
12992                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
12993                 }
12994               emit_move_insn (destmem, tmpreg[i]);
12995             }
12996         }
12997     }
12998   else
12999     for (i = 0; i < unroll; i++)
13000       {
13001         if (i)
13002           destmem =
13003             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13004         emit_move_insn (destmem, value);
13005       }
13006
13007   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
13008                              true, OPTAB_LIB_WIDEN);
13009   if (tmp != iter)
13010     emit_move_insn (iter, tmp);
13011
13012   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
13013                            true, top_label);
13014   if (expected_size != -1)
13015     {
13016       expected_size /= GET_MODE_SIZE (mode) * unroll;
13017       if (expected_size == 0)
13018         predict_jump (0);
13019       else if (expected_size > REG_BR_PROB_BASE)
13020         predict_jump (REG_BR_PROB_BASE - 1);
13021       else
13022         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
13023     }
13024   else
13025     predict_jump (REG_BR_PROB_BASE * 80 / 100);
13026   iter = ix86_zero_extend_to_Pmode (iter);
13027   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
13028                              true, OPTAB_LIB_WIDEN);
13029   if (tmp != destptr)
13030     emit_move_insn (destptr, tmp);
13031   if (srcptr)
13032     {
13033       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
13034                                  true, OPTAB_LIB_WIDEN);
13035       if (tmp != srcptr)
13036         emit_move_insn (srcptr, tmp);
13037     }
13038   emit_label (out_label);
13039 }
13040
13041 /* Output "rep; mov" instruction.  
13042    Arguments have same meaning as for previous function */
13043 static void
13044 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
13045                            rtx destptr, rtx srcptr,
13046                            rtx count,
13047                            enum machine_mode mode)
13048 {
13049   rtx destexp;
13050   rtx srcexp;
13051   rtx countreg;
13052
13053   /* If the size is known, it is shorter to use rep movs.  */
13054   if (mode == QImode && GET_CODE (count) == CONST_INT
13055       && !(INTVAL (count) & 3))
13056     mode = SImode;
13057
13058   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13059     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13060   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
13061     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
13062   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13063   if (mode != QImode)
13064     {
13065       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13066                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13067       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13068       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
13069                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13070       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
13071     }
13072   else
13073     {
13074       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13075       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
13076     }
13077   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
13078                           destexp, srcexp));
13079 }
13080
13081 /* Output "rep; stos" instruction.  
13082    Arguments have same meaning as for previous function */
13083 static void
13084 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
13085                             rtx count,
13086                             enum machine_mode mode)
13087 {
13088   rtx destexp;
13089   rtx countreg;
13090
13091   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13092     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13093   value = force_reg (mode, gen_lowpart (mode, value));
13094   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13095   if (mode != QImode)
13096     {
13097       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13098                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13099       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13100     }
13101   else
13102     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13103   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
13104 }
13105
13106 static void
13107 emit_strmov (rtx destmem, rtx srcmem,
13108              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
13109 {
13110   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
13111   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
13112   emit_insn (gen_strmov (destptr, dest, srcptr, src));
13113 }
13114
13115 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
13116 static void
13117 expand_movmem_epilogue (rtx destmem, rtx srcmem,
13118                         rtx destptr, rtx srcptr, rtx count, int max_size)
13119 {
13120   rtx src, dest;
13121   if (GET_CODE (count) == CONST_INT)
13122     {
13123       HOST_WIDE_INT countval = INTVAL (count);
13124       int offset = 0;
13125
13126       if ((countval & 0x16) && max_size > 16)
13127         {
13128           if (TARGET_64BIT)
13129             {
13130               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13131               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
13132             }
13133           else
13134             gcc_unreachable ();
13135           offset += 16;
13136         }
13137       if ((countval & 0x08) && max_size > 8)
13138         {
13139           if (TARGET_64BIT)
13140             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13141           else
13142             {
13143               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13144               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 4);
13145             }
13146           offset += 8;
13147         }
13148       if ((countval & 0x04) && max_size > 4)
13149         {
13150           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13151           offset += 4;
13152         }
13153       if ((countval & 0x02) && max_size > 2)
13154         {
13155           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
13156           offset += 2;
13157         }
13158       if ((countval & 0x01) && max_size > 1)
13159         {
13160           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
13161           offset += 1;
13162         }
13163       return;
13164     }
13165   if (max_size > 8)
13166     {
13167       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
13168                                     count, 1, OPTAB_DIRECT);
13169       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
13170                                      count, QImode, 1, 4);
13171       return;
13172     }
13173
13174   /* When there are stringops, we can cheaply increase dest and src pointers.
13175      Otherwise we save code size by maintaining offset (zero is readily
13176      available from preceding rep operation) and using x86 addressing modes.
13177    */
13178   if (TARGET_SINGLE_STRINGOP)
13179     {
13180       if (max_size > 4)
13181         {
13182           rtx label = ix86_expand_aligntest (count, 4, true);
13183           src = change_address (srcmem, SImode, srcptr);
13184           dest = change_address (destmem, SImode, destptr);
13185           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13186           emit_label (label);
13187           LABEL_NUSES (label) = 1;
13188         }
13189       if (max_size > 2)
13190         {
13191           rtx label = ix86_expand_aligntest (count, 2, true);
13192           src = change_address (srcmem, HImode, srcptr);
13193           dest = change_address (destmem, HImode, destptr);
13194           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13195           emit_label (label);
13196           LABEL_NUSES (label) = 1;
13197         }
13198       if (max_size > 1)
13199         {
13200           rtx label = ix86_expand_aligntest (count, 1, true);
13201           src = change_address (srcmem, QImode, srcptr);
13202           dest = change_address (destmem, QImode, destptr);
13203           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13204           emit_label (label);
13205           LABEL_NUSES (label) = 1;
13206         }
13207     }
13208   else
13209     {
13210       rtx offset = force_reg (Pmode, const0_rtx);
13211       rtx tmp;
13212
13213       if (max_size > 4)
13214         {
13215           rtx label = ix86_expand_aligntest (count, 4, true);
13216           src = change_address (srcmem, SImode, srcptr);
13217           dest = change_address (destmem, SImode, destptr);
13218           emit_move_insn (dest, src);
13219           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
13220                                      true, OPTAB_LIB_WIDEN);
13221           if (tmp != offset)
13222             emit_move_insn (offset, tmp);
13223           emit_label (label);
13224           LABEL_NUSES (label) = 1;
13225         }
13226       if (max_size > 2)
13227         {
13228           rtx label = ix86_expand_aligntest (count, 2, true);
13229           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13230           src = change_address (srcmem, HImode, tmp);
13231           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13232           dest = change_address (destmem, HImode, tmp);
13233           emit_move_insn (dest, src);
13234           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
13235                                      true, OPTAB_LIB_WIDEN);
13236           if (tmp != offset)
13237             emit_move_insn (offset, tmp);
13238           emit_label (label);
13239           LABEL_NUSES (label) = 1;
13240         }
13241       if (max_size > 1)
13242         {
13243           rtx label = ix86_expand_aligntest (count, 1, true);
13244           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13245           src = change_address (srcmem, QImode, tmp);
13246           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13247           dest = change_address (destmem, QImode, tmp);
13248           emit_move_insn (dest, src);
13249           emit_label (label);
13250           LABEL_NUSES (label) = 1;
13251         }
13252     }
13253 }
13254
13255 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13256 static void
13257 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
13258                                  rtx count, int max_size)
13259 {
13260   count =
13261     expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
13262                          count, 1, OPTAB_DIRECT);
13263   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
13264                                  gen_lowpart (QImode, value), count, QImode,
13265                                  1, max_size / 2);
13266 }
13267
13268 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13269 static void
13270 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
13271 {
13272   rtx dest;
13273   if (GET_CODE (count) == CONST_INT)
13274     {
13275       HOST_WIDE_INT countval = INTVAL (count);
13276       int offset = 0;
13277
13278       if ((countval & 0x16) && max_size > 16)
13279         {
13280           if (TARGET_64BIT)
13281             {
13282               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13283               emit_insn (gen_strset (destptr, dest, value));
13284               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
13285               emit_insn (gen_strset (destptr, dest, value));
13286             }
13287           else
13288             gcc_unreachable ();
13289           offset += 16;
13290         }
13291       if ((countval & 0x08) && max_size > 8)
13292         {
13293           if (TARGET_64BIT)
13294             {
13295               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13296               emit_insn (gen_strset (destptr, dest, value));
13297             }
13298           else
13299             {
13300               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13301               emit_insn (gen_strset (destptr, dest, value));
13302               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
13303               emit_insn (gen_strset (destptr, dest, value));
13304             }
13305           offset += 8;
13306         }
13307       if ((countval & 0x04) && max_size > 4)
13308         {
13309           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13310           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13311           offset += 4;
13312         }
13313       if ((countval & 0x02) && max_size > 2)
13314         {
13315           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
13316           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13317           offset += 2;
13318         }
13319       if ((countval & 0x01) && max_size > 1)
13320         {
13321           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
13322           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
13323           offset += 1;
13324         }
13325       return;
13326     }
13327   if (max_size > 32)
13328     {
13329       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
13330       return;
13331     }
13332   if (max_size > 16)
13333     {
13334       rtx label = ix86_expand_aligntest (count, 16, true);
13335       if (TARGET_64BIT)
13336         {
13337           dest = change_address (destmem, DImode, destptr);
13338           emit_insn (gen_strset (destptr, dest, value));
13339           emit_insn (gen_strset (destptr, dest, value));
13340         }
13341       else
13342         {
13343           dest = change_address (destmem, SImode, destptr);
13344           emit_insn (gen_strset (destptr, dest, value));
13345           emit_insn (gen_strset (destptr, dest, value));
13346           emit_insn (gen_strset (destptr, dest, value));
13347           emit_insn (gen_strset (destptr, dest, value));
13348         }
13349       emit_label (label);
13350       LABEL_NUSES (label) = 1;
13351     }
13352   if (max_size > 8)
13353     {
13354       rtx label = ix86_expand_aligntest (count, 8, true);
13355       if (TARGET_64BIT)
13356         {
13357           dest = change_address (destmem, DImode, destptr);
13358           emit_insn (gen_strset (destptr, dest, value));
13359         }
13360       else
13361         {
13362           dest = change_address (destmem, SImode, destptr);
13363           emit_insn (gen_strset (destptr, dest, value));
13364           emit_insn (gen_strset (destptr, dest, value));
13365         }
13366       emit_label (label);
13367       LABEL_NUSES (label) = 1;
13368     }
13369   if (max_size > 4)
13370     {
13371       rtx label = ix86_expand_aligntest (count, 4, true);
13372       dest = change_address (destmem, SImode, destptr);
13373       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13374       emit_label (label);
13375       LABEL_NUSES (label) = 1;
13376     }
13377   if (max_size > 2)
13378     {
13379       rtx label = ix86_expand_aligntest (count, 2, true);
13380       dest = change_address (destmem, HImode, destptr);
13381       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13382       emit_label (label);
13383       LABEL_NUSES (label) = 1;
13384     }
13385   if (max_size > 1)
13386     {
13387       rtx label = ix86_expand_aligntest (count, 1, true);
13388       dest = change_address (destmem, QImode, destptr);
13389       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
13390       emit_label (label);
13391       LABEL_NUSES (label) = 1;
13392     }
13393 }
13394
13395 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
13396    DESIRED_ALIGNMENT.  */
13397 static void
13398 expand_movmem_prologue (rtx destmem, rtx srcmem,
13399                         rtx destptr, rtx srcptr, rtx count,
13400                         int align, int desired_alignment)
13401 {
13402   if (align <= 1 && desired_alignment > 1)
13403     {
13404       rtx label = ix86_expand_aligntest (destptr, 1, false);
13405       srcmem = change_address (srcmem, QImode, srcptr);
13406       destmem = change_address (destmem, QImode, destptr);
13407       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13408       ix86_adjust_counter (count, 1);
13409       emit_label (label);
13410       LABEL_NUSES (label) = 1;
13411     }
13412   if (align <= 2 && desired_alignment > 2)
13413     {
13414       rtx label = ix86_expand_aligntest (destptr, 2, false);
13415       srcmem = change_address (srcmem, HImode, srcptr);
13416       destmem = change_address (destmem, HImode, destptr);
13417       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13418       ix86_adjust_counter (count, 2);
13419       emit_label (label);
13420       LABEL_NUSES (label) = 1;
13421     }
13422   if (align <= 4 && desired_alignment > 4)
13423     {
13424       rtx label = ix86_expand_aligntest (destptr, 4, false);
13425       srcmem = change_address (srcmem, SImode, srcptr);
13426       destmem = change_address (destmem, SImode, destptr);
13427       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13428       ix86_adjust_counter (count, 4);
13429       emit_label (label);
13430       LABEL_NUSES (label) = 1;
13431     }
13432   gcc_assert (desired_alignment <= 8);
13433 }
13434
13435 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
13436    DESIRED_ALIGNMENT.  */
13437 static void
13438 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
13439                         int align, int desired_alignment)
13440 {
13441   if (align <= 1 && desired_alignment > 1)
13442     {
13443       rtx label = ix86_expand_aligntest (destptr, 1, false);
13444       destmem = change_address (destmem, QImode, destptr);
13445       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
13446       ix86_adjust_counter (count, 1);
13447       emit_label (label);
13448       LABEL_NUSES (label) = 1;
13449     }
13450   if (align <= 2 && desired_alignment > 2)
13451     {
13452       rtx label = ix86_expand_aligntest (destptr, 2, false);
13453       destmem = change_address (destmem, HImode, destptr);
13454       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
13455       ix86_adjust_counter (count, 2);
13456       emit_label (label);
13457       LABEL_NUSES (label) = 1;
13458     }
13459   if (align <= 4 && desired_alignment > 4)
13460     {
13461       rtx label = ix86_expand_aligntest (destptr, 4, false);
13462       destmem = change_address (destmem, SImode, destptr);
13463       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
13464       ix86_adjust_counter (count, 4);
13465       emit_label (label);
13466       LABEL_NUSES (label) = 1;
13467     }
13468   gcc_assert (desired_alignment <= 8);
13469 }
13470
13471 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
13472 static enum stringop_alg
13473 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
13474             int *dynamic_check)
13475 {
13476   const struct stringop_algs * algs;
13477
13478   *dynamic_check = -1;
13479   if (memset)
13480     algs = &ix86_cost->memset[TARGET_64BIT != 0];
13481   else
13482     algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
13483   if (stringop_alg != no_stringop)
13484     return stringop_alg;
13485   /* rep; movq or rep; movl is the smallest variant.  */
13486   else if (optimize_size)
13487     {
13488       if (!count || (count & 3))
13489         return rep_prefix_1_byte;
13490       else
13491         return rep_prefix_4_byte;
13492     }
13493   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
13494    */
13495   else if (expected_size != -1 && expected_size < 4)
13496     return loop_1_byte;
13497   else if (expected_size != -1)
13498     {
13499       unsigned int i;
13500       enum stringop_alg alg = libcall;
13501       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
13502         {
13503           gcc_assert (algs->size[i].max);
13504           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
13505             {
13506               if (algs->size[i].alg != libcall)
13507                 alg = algs->size[i].alg;
13508               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
13509                  last non-libcall inline algorithm.  */
13510               if (TARGET_INLINE_ALL_STRINGOPS)
13511                 {
13512                   /* When the current size is best to be copied by a libcall,
13513                      but we are still forced to inline, run the heuristic bellow
13514                      that will pick code for medium sized blocks.  */
13515                   if (alg != libcall)
13516                     return alg;
13517                   break;
13518                 }
13519               else
13520                 return algs->size[i].alg;
13521             }
13522         }
13523       gcc_assert (TARGET_INLINE_ALL_STRINGOPS);
13524     }
13525   /* When asked to inline the call anyway, try to pick meaningful choice.
13526      We look for maximal size of block that is faster to copy by hand and
13527      take blocks of at most of that size guessing that average size will
13528      be roughly half of the block.  
13529
13530      If this turns out to be bad, we might simply specify the preferred
13531      choice in ix86_costs.  */
13532   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
13533       && algs->unknown_size == libcall)
13534     {
13535       int max = -1;
13536       enum stringop_alg alg;
13537       int i;
13538
13539       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
13540         if (algs->size[i].alg != libcall && algs->size[i].alg)
13541           max = algs->size[i].max;
13542       if (max == -1)
13543         max = 4096;
13544       alg = decide_alg (count, max / 2, memset, dynamic_check);
13545       gcc_assert (*dynamic_check == -1);
13546       gcc_assert (alg != libcall);
13547       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
13548         *dynamic_check = max;
13549       return alg;
13550     }
13551   return algs->unknown_size;
13552 }
13553
13554 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
13555    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
13556 static int
13557 decide_alignment (int align,
13558                   enum stringop_alg alg,
13559                   int expected_size)
13560 {
13561   int desired_align = 0;
13562   switch (alg)
13563     {
13564       case no_stringop:
13565         gcc_unreachable ();
13566       case loop:
13567       case unrolled_loop:
13568         desired_align = GET_MODE_SIZE (Pmode);
13569         break;
13570       case rep_prefix_8_byte:
13571         desired_align = 8;
13572         break;
13573       case rep_prefix_4_byte:
13574         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
13575            copying whole cacheline at once.  */
13576         if (TARGET_PENTIUMPRO)
13577           desired_align = 8;
13578         else
13579           desired_align = 4;
13580         break;
13581       case rep_prefix_1_byte:
13582         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
13583            copying whole cacheline at once.  */
13584         if (TARGET_PENTIUMPRO)
13585           desired_align = 8;
13586         else
13587           desired_align = 1;
13588         break;
13589       case loop_1_byte:
13590         desired_align = 1;
13591         break;
13592       case libcall:
13593         return 0;
13594     }
13595
13596   if (optimize_size)
13597     desired_align = 1;
13598   if (desired_align < align)
13599     desired_align = align;
13600   if (expected_size != -1 && expected_size < 4)
13601     desired_align = align;
13602   return desired_align;
13603 }
13604
13605 /* Expand string move (memcpy) operation.  Use i386 string operations when
13606    profitable.  expand_clrmem contains similar code.  */
13607 int
13608 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
13609                     rtx expected_align_exp, rtx expected_size_exp)
13610 {
13611   rtx destreg;
13612   rtx srcreg;
13613   rtx label = NULL;
13614   rtx tmp;
13615   rtx jump_around_label = NULL;
13616   HOST_WIDE_INT align = 1;
13617   unsigned HOST_WIDE_INT count = 0;
13618   HOST_WIDE_INT expected_size = -1;
13619   int size_needed = 0;
13620   int desired_align = 0;
13621   enum stringop_alg alg;
13622   int dynamic_check;
13623   /* Precise placement on cld depends whether stringops will be emit in
13624      prologue, main copying body or epilogue.  This variable keeps track
13625      if cld was already needed.  */
13626   bool cld_done = false;
13627
13628   if (GET_CODE (align_exp) == CONST_INT)
13629     align = INTVAL (align_exp);
13630   /* i386 can do misaligned access on reasonably increased cost.  */
13631   if (GET_CODE (expected_align_exp) == CONST_INT
13632       && INTVAL (expected_align_exp) > align)
13633     align = INTVAL (expected_align_exp);
13634   if (GET_CODE (count_exp) == CONST_INT)
13635     count = expected_size = INTVAL (count_exp);
13636   if (GET_CODE (expected_size_exp) == CONST_INT && count == 0)
13637     {
13638       expected_size = INTVAL (expected_size_exp);
13639     }
13640
13641   alg = decide_alg (count, expected_size, false, &dynamic_check);
13642   desired_align = decide_alignment (align, alg, expected_size);
13643
13644   if (!TARGET_ALIGN_STRINGOPS)
13645     align = desired_align;
13646
13647   if (alg == libcall)
13648     return 0;
13649   gcc_assert (alg != no_stringop);
13650   if (!count)
13651     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
13652   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
13653   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
13654   switch (alg)
13655     {
13656     case libcall:
13657     case no_stringop:
13658       gcc_unreachable ();
13659     case loop:
13660       size_needed = GET_MODE_SIZE (Pmode);
13661       break;
13662     case unrolled_loop:
13663       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
13664       break;
13665     case rep_prefix_8_byte:
13666       size_needed = 8;
13667       break;
13668     case rep_prefix_4_byte:
13669       size_needed = 4;
13670       break;
13671     case rep_prefix_1_byte:
13672     case loop_1_byte:
13673       size_needed = 1;
13674       break;
13675     }
13676
13677   /* Alignment code needs count to be in register.  */
13678   if (GET_CODE (count_exp) == CONST_INT && desired_align > align)
13679     {
13680       enum machine_mode mode = SImode;
13681       if (TARGET_64BIT && (count & ~0xffffffff))
13682         mode = DImode;
13683       count_exp = force_reg (mode, count_exp);
13684     }
13685   gcc_assert (desired_align >= 1 && align >= 1);
13686   /* Ensure that alignment prologue won't copy past end of block.  */
13687   if ((size_needed > 1 || (desired_align > 1 && desired_align > align))
13688       && !count)
13689     {
13690       int size = MAX (size_needed - 1, desired_align - align);
13691       if (TARGET_SINGLE_STRINGOP)
13692         emit_insn (gen_cld ()), cld_done = true;
13693       label = gen_label_rtx ();
13694       emit_cmp_and_jump_insns (count_exp,
13695                                GEN_INT (size),
13696                                LEU, 0, GET_MODE (count_exp), 1, label);
13697       if (expected_size == -1 || expected_size < size)
13698         predict_jump (REG_BR_PROB_BASE * 60 / 100);
13699       else
13700         predict_jump (REG_BR_PROB_BASE * 20 / 100);
13701     }
13702   /* Emit code to decide on runtime whether library call or inline should be
13703      used.  */
13704   if (dynamic_check != -1)
13705     {
13706       rtx hot_label = gen_label_rtx ();
13707       jump_around_label = gen_label_rtx ();
13708       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
13709                                LEU, 0, GET_MODE (count_exp), 1, hot_label);
13710       predict_jump (REG_BR_PROB_BASE * 90 / 100);
13711       emit_block_move_via_libcall (dst, src, count_exp, false);
13712       emit_jump (jump_around_label);
13713       emit_label (hot_label);
13714     }
13715
13716
13717   /* Alignment prologue.  */
13718   if (desired_align > align)
13719     {
13720       /* Except for the first move in epilogue, we no longer know
13721          constant offset in aliasing info.  It don't seems to worth
13722          the pain to maintain it for the first move, so throw away
13723          the info early.  */
13724       src = change_address (src, BLKmode, srcreg);
13725       dst = change_address (dst, BLKmode, destreg);
13726       if (TARGET_SINGLE_STRINGOP && !cld_done)
13727         emit_insn (gen_cld ()), cld_done = true;
13728       expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
13729                               desired_align);
13730     }
13731   if (label && size_needed == 1)
13732     {
13733       emit_label (label);
13734       LABEL_NUSES (label) = 1;
13735       label = NULL;
13736     }
13737
13738   /* Main body.  */
13739   switch (alg)
13740     {
13741     case libcall:
13742     case no_stringop:
13743       gcc_unreachable ();
13744     case loop_1_byte:
13745       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
13746                                      count_exp, QImode, 1, expected_size);
13747       break;
13748     case loop:
13749       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
13750                                      count_exp, Pmode, 1, expected_size);
13751       break;
13752     case unrolled_loop:
13753       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
13754          registers for 4 temporaries anyway.  */
13755       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
13756                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
13757                                      expected_size);
13758       break;
13759     case rep_prefix_8_byte:
13760       if (!cld_done)
13761         emit_insn (gen_cld ()), cld_done = true;
13762       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
13763                                  DImode);
13764       break;
13765     case rep_prefix_4_byte:
13766       if (!cld_done)
13767         emit_insn (gen_cld ()), cld_done = true;
13768       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
13769                                  SImode);
13770       break;
13771     case rep_prefix_1_byte:
13772       if (!cld_done)
13773         emit_insn (gen_cld ()), cld_done = true;
13774       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
13775                                  QImode);
13776       break;
13777     }
13778   /* Adjust properly the offset of src and dest memory for aliasing.  */
13779   if (GET_CODE (count_exp) == CONST_INT)
13780     {
13781       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
13782                                           (count / size_needed) * size_needed);
13783       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
13784                                           (count / size_needed) * size_needed);
13785     }
13786   else
13787     {
13788       src = change_address (src, BLKmode, srcreg);
13789       dst = change_address (dst, BLKmode, destreg);
13790     }
13791
13792   /* Epilogue to copy the remaining bytes.  */
13793   if (label)
13794     {
13795       if (size_needed < desired_align - align)
13796         {
13797           tmp =
13798             expand_simple_binop (GET_MODE (count_exp), AND, count_exp,
13799                                  GEN_INT (size_needed - 1), count_exp, 1,
13800                                  OPTAB_DIRECT);
13801           size_needed = desired_align - align + 1;
13802           if (tmp != count_exp)
13803             emit_move_insn (count_exp, tmp);
13804         }
13805       emit_label (label);
13806       LABEL_NUSES (label) = 1;
13807     }
13808   if (count_exp != const0_rtx && size_needed > 1)
13809     {
13810       if (TARGET_SINGLE_STRINGOP && !cld_done)
13811         emit_insn (gen_cld ()), cld_done = true;
13812       expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
13813                               size_needed);
13814     }
13815   if (jump_around_label)
13816     emit_label (jump_around_label);
13817   return 1;
13818 }
13819
13820 /* Helper function for memcpy.  For QImode value 0xXY produce
13821    0xXYXYXYXY of wide specified by MODE.  This is essentially
13822    a * 0x10101010, but we can do slightly better than
13823    synth_mult by unwinding the sequence by hand on CPUs with
13824    slow multiply.  */
13825 static rtx
13826 promote_duplicated_reg (enum machine_mode mode, rtx val)
13827 {
13828   enum machine_mode valmode = GET_MODE (val);
13829   rtx tmp;
13830   int nops = mode == DImode ? 3 : 2;
13831
13832   gcc_assert (mode == SImode || mode == DImode);
13833   if (val == const0_rtx)
13834     return copy_to_mode_reg (mode, const0_rtx);
13835   if (GET_CODE (val) == CONST_INT)
13836     {
13837       HOST_WIDE_INT v = INTVAL (val) & 255;
13838
13839       v |= v << 8;
13840       v |= v << 16;
13841       if (mode == DImode)
13842         v |= (v << 16) << 16;
13843       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
13844     }
13845
13846   if (valmode == VOIDmode)
13847     valmode = QImode;
13848   if (valmode != QImode)
13849     val = gen_lowpart (QImode, val);
13850   if (mode == QImode)
13851     return val;
13852   if (!TARGET_PARTIAL_REG_STALL)
13853     nops--;
13854   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
13855       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
13856       <= (ix86_cost->shift_const + ix86_cost->add) * nops
13857           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
13858     {
13859       rtx reg = convert_modes (mode, QImode, val, true);
13860       tmp = promote_duplicated_reg (mode, const1_rtx);
13861       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
13862                                   OPTAB_DIRECT);
13863     }
13864   else
13865     {
13866       rtx reg = convert_modes (mode, QImode, val, true);
13867
13868       if (!TARGET_PARTIAL_REG_STALL)
13869         if (mode == SImode)
13870           emit_insn (gen_movsi_insv_1 (reg, reg));
13871         else
13872           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
13873       else
13874         {
13875           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
13876                                      NULL, 1, OPTAB_DIRECT);
13877           reg =
13878             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
13879         }
13880       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
13881                                  NULL, 1, OPTAB_DIRECT);
13882       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
13883       if (mode == SImode)
13884         return reg;
13885       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
13886                                  NULL, 1, OPTAB_DIRECT);
13887       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
13888       return reg;
13889     }
13890 }
13891
13892 /* Expand string clear operation (bzero).  Use i386 string operations when
13893    profitable.  expand_movmem contains similar code.  */
13894 int
13895 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
13896                     rtx expected_align_exp, rtx expected_size_exp)
13897 {
13898   rtx destreg;
13899   rtx label = NULL;
13900   rtx tmp;
13901   rtx jump_around_label = NULL;
13902   HOST_WIDE_INT align = 1;
13903   unsigned HOST_WIDE_INT count = 0;
13904   HOST_WIDE_INT expected_size = -1;
13905   int size_needed = 0;
13906   int desired_align = 0;
13907   enum stringop_alg alg;
13908   /* Precise placement on cld depends whether stringops will be emit in
13909      prologue, main copying body or epilogue.  This variable keeps track
13910      if cld was already needed.  */
13911   bool cld_done = false;
13912   rtx promoted_val = val_exp;
13913   bool force_loopy_epilogue = false;
13914   int dynamic_check;
13915
13916   if (GET_CODE (align_exp) == CONST_INT)
13917     align = INTVAL (align_exp);
13918   /* i386 can do misaligned access on reasonably increased cost.  */
13919   if (GET_CODE (expected_align_exp) == CONST_INT
13920       && INTVAL (expected_align_exp) > align)
13921     align = INTVAL (expected_align_exp);
13922   if (GET_CODE (count_exp) == CONST_INT)
13923     count = expected_size = INTVAL (count_exp);
13924   if (GET_CODE (expected_size_exp) == CONST_INT && count == 0)
13925     expected_size = INTVAL (expected_size_exp);
13926
13927   alg = decide_alg (count, expected_size, true, &dynamic_check);
13928   desired_align = decide_alignment (align, alg, expected_size);
13929
13930   if (!TARGET_ALIGN_STRINGOPS)
13931     align = desired_align;
13932
13933   if (alg == libcall)
13934     return 0;
13935   gcc_assert (alg != no_stringop);
13936   if (!count)
13937     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
13938   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
13939   switch (alg)
13940     {
13941     case libcall:
13942     case no_stringop:
13943       gcc_unreachable ();
13944     case loop:
13945       size_needed = GET_MODE_SIZE (Pmode);
13946       break;
13947     case unrolled_loop:
13948       size_needed = GET_MODE_SIZE (Pmode) * 4;
13949       break;
13950     case rep_prefix_8_byte:
13951       size_needed = 8;
13952       break;
13953     case rep_prefix_4_byte:
13954       size_needed = 4;
13955       break;
13956     case rep_prefix_1_byte:
13957     case loop_1_byte:
13958       size_needed = 1;
13959       break;
13960     }
13961   /* Alignment code needs count to be in register.  */
13962   if (GET_CODE (count_exp) == CONST_INT && desired_align > align)
13963     {
13964       enum machine_mode mode = SImode;
13965       if (TARGET_64BIT && (count & ~0xffffffff))
13966         mode = DImode;
13967       count_exp = force_reg (mode, count_exp);
13968     }
13969   /* Ensure that alignment prologue won't copy past end of block.  */
13970   if ((size_needed > 1 || (desired_align > 1 && desired_align > align))
13971       && !count)
13972     {
13973       int size = MAX (size_needed - 1, desired_align - align);
13974       /* To improve performance of small blocks, we jump around the promoting
13975          code, so we need to use QImode accesses in epilogue.  */
13976       if (GET_CODE (val_exp) != CONST_INT && size_needed > 1)
13977         force_loopy_epilogue = true;
13978       else if (TARGET_SINGLE_STRINGOP)
13979         emit_insn (gen_cld ()), cld_done = true;
13980       label = gen_label_rtx ();
13981       emit_cmp_and_jump_insns (count_exp,
13982                                GEN_INT (size),
13983                                LEU, 0, GET_MODE (count_exp), 1, label);
13984       if (expected_size == -1 || expected_size <= size)
13985         predict_jump (REG_BR_PROB_BASE * 60 / 100);
13986       else
13987         predict_jump (REG_BR_PROB_BASE * 20 / 100);
13988     }
13989   if (dynamic_check != -1)
13990     {
13991       rtx hot_label = gen_label_rtx ();
13992       jump_around_label = gen_label_rtx ();
13993       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
13994                                LEU, 0, GET_MODE (count_exp), 1, hot_label);
13995       predict_jump (REG_BR_PROB_BASE * 90 / 100);
13996       set_storage_via_libcall (dst, count_exp, val_exp, false);
13997       emit_jump (jump_around_label);
13998       emit_label (hot_label);
13999     }
14000   if (TARGET_64BIT
14001       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
14002     promoted_val = promote_duplicated_reg (DImode, val_exp);
14003   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
14004     promoted_val = promote_duplicated_reg (SImode, val_exp);
14005   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
14006     promoted_val = promote_duplicated_reg (HImode, val_exp);
14007   else
14008     promoted_val = val_exp;
14009   gcc_assert (desired_align >= 1 && align >= 1);
14010   if ((size_needed > 1 || (desired_align > 1 && desired_align > align))
14011       && !count && !label)
14012     {
14013       int size = MAX (size_needed - 1, desired_align - align);
14014       if (TARGET_SINGLE_STRINGOP)
14015         emit_insn (gen_cld ()), cld_done = true;
14016       label = gen_label_rtx ();
14017       emit_cmp_and_jump_insns (count_exp,
14018                                GEN_INT (size),
14019                                LEU, 0, GET_MODE (count_exp), 1, label);
14020       if (expected_size == -1 || expected_size <= size)
14021         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14022       else
14023         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14024     }
14025   if (desired_align > align)
14026     {
14027       /* Except for the first move in epilogue, we no longer know
14028          constant offset in aliasing info.  It don't seems to worth
14029          the pain to maintain it for the first move, so throw away
14030          the info early.  */
14031       dst = change_address (dst, BLKmode, destreg);
14032       if (TARGET_SINGLE_STRINGOP && !cld_done)
14033         emit_insn (gen_cld ()), cld_done = true;
14034       expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
14035                               desired_align);
14036     }
14037   if (label && size_needed == 1)
14038     {
14039       emit_label (label);
14040       LABEL_NUSES (label) = 1;
14041       label = NULL;
14042     }
14043   switch (alg)
14044     {
14045     case libcall:
14046     case no_stringop:
14047       gcc_unreachable ();
14048     case loop_1_byte:
14049       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14050                                      count_exp, QImode, 1, expected_size);
14051       break;
14052     case loop:
14053       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14054                                      count_exp, Pmode, 1, expected_size);
14055       break;
14056     case unrolled_loop:
14057       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14058                                      count_exp, Pmode, 4, expected_size);
14059       break;
14060     case rep_prefix_8_byte:
14061       if (!cld_done)
14062         emit_insn (gen_cld ()), cld_done = true;
14063       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14064                                   DImode);
14065       break;
14066     case rep_prefix_4_byte:
14067       if (!cld_done)
14068         emit_insn (gen_cld ()), cld_done = true;
14069       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14070                                   SImode);
14071       break;
14072     case rep_prefix_1_byte:
14073       if (!cld_done)
14074         emit_insn (gen_cld ()), cld_done = true;
14075       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14076                                   QImode);
14077       break;
14078     }
14079   /* Adjust properly the offset of src and dest memory for aliasing.  */
14080   if (GET_CODE (count_exp) == CONST_INT)
14081     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14082                                         (count / size_needed) * size_needed);
14083   else
14084     dst = change_address (dst, BLKmode, destreg);
14085
14086   if (label)
14087     {
14088       if (size_needed < desired_align - align)
14089         {
14090           tmp =
14091             expand_simple_binop (GET_MODE (count_exp), AND, count_exp,
14092                                  GEN_INT (size_needed - 1), count_exp, 1,
14093                                  OPTAB_DIRECT);
14094           size_needed = desired_align - align + 1;
14095           if (tmp != count_exp)
14096             emit_move_insn (count_exp, tmp);
14097         }
14098       emit_label (label);
14099       LABEL_NUSES (label) = 1;
14100     }
14101   if (count_exp != const0_rtx && size_needed > 1)
14102     {
14103       if (force_loopy_epilogue)
14104         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
14105                                          size_needed);
14106       else
14107         {
14108           if (TARGET_SINGLE_STRINGOP && !cld_done)
14109             emit_insn (gen_cld ()), cld_done = true;
14110           expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
14111                                   size_needed);
14112         }
14113     }
14114   if (jump_around_label)
14115     emit_label (jump_around_label);
14116   return 1;
14117 }
14118
14119 /* Expand strlen.  */
14120 int
14121 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
14122 {
14123   rtx addr, scratch1, scratch2, scratch3, scratch4;
14124
14125   /* The generic case of strlen expander is long.  Avoid it's
14126      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
14127
14128   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
14129       && !TARGET_INLINE_ALL_STRINGOPS
14130       && !optimize_size
14131       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
14132     return 0;
14133
14134   addr = force_reg (Pmode, XEXP (src, 0));
14135   scratch1 = gen_reg_rtx (Pmode);
14136
14137   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
14138       && !optimize_size)
14139     {
14140       /* Well it seems that some optimizer does not combine a call like
14141          foo(strlen(bar), strlen(bar));
14142          when the move and the subtraction is done here.  It does calculate
14143          the length just once when these instructions are done inside of
14144          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
14145          often used and I use one fewer register for the lifetime of
14146          output_strlen_unroll() this is better.  */
14147
14148       emit_move_insn (out, addr);
14149
14150       ix86_expand_strlensi_unroll_1 (out, src, align);
14151
14152       /* strlensi_unroll_1 returns the address of the zero at the end of
14153          the string, like memchr(), so compute the length by subtracting
14154          the start address.  */
14155       if (TARGET_64BIT)
14156         emit_insn (gen_subdi3 (out, out, addr));
14157       else
14158         emit_insn (gen_subsi3 (out, out, addr));
14159     }
14160   else
14161     {
14162       rtx unspec;
14163       scratch2 = gen_reg_rtx (Pmode);
14164       scratch3 = gen_reg_rtx (Pmode);
14165       scratch4 = force_reg (Pmode, constm1_rtx);
14166
14167       emit_move_insn (scratch3, addr);
14168       eoschar = force_reg (QImode, eoschar);
14169
14170       emit_insn (gen_cld ());
14171       src = replace_equiv_address_nv (src, scratch3);
14172
14173       /* If .md starts supporting :P, this can be done in .md.  */
14174       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
14175                                                  scratch4), UNSPEC_SCAS);
14176       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
14177       if (TARGET_64BIT)
14178         {
14179           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
14180           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
14181         }
14182       else
14183         {
14184           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
14185           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
14186         }
14187     }
14188   return 1;
14189 }
14190
14191 /* Expand the appropriate insns for doing strlen if not just doing
14192    repnz; scasb
14193
14194    out = result, initialized with the start address
14195    align_rtx = alignment of the address.
14196    scratch = scratch register, initialized with the startaddress when
14197         not aligned, otherwise undefined
14198
14199    This is just the body. It needs the initializations mentioned above and
14200    some address computing at the end.  These things are done in i386.md.  */
14201
14202 static void
14203 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
14204 {
14205   int align;
14206   rtx tmp;
14207   rtx align_2_label = NULL_RTX;
14208   rtx align_3_label = NULL_RTX;
14209   rtx align_4_label = gen_label_rtx ();
14210   rtx end_0_label = gen_label_rtx ();
14211   rtx mem;
14212   rtx tmpreg = gen_reg_rtx (SImode);
14213   rtx scratch = gen_reg_rtx (SImode);
14214   rtx cmp;
14215
14216   align = 0;
14217   if (GET_CODE (align_rtx) == CONST_INT)
14218     align = INTVAL (align_rtx);
14219
14220   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
14221
14222   /* Is there a known alignment and is it less than 4?  */
14223   if (align < 4)
14224     {
14225       rtx scratch1 = gen_reg_rtx (Pmode);
14226       emit_move_insn (scratch1, out);
14227       /* Is there a known alignment and is it not 2? */
14228       if (align != 2)
14229         {
14230           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
14231           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
14232
14233           /* Leave just the 3 lower bits.  */
14234           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
14235                                     NULL_RTX, 0, OPTAB_WIDEN);
14236
14237           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14238                                    Pmode, 1, align_4_label);
14239           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
14240                                    Pmode, 1, align_2_label);
14241           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
14242                                    Pmode, 1, align_3_label);
14243         }
14244       else
14245         {
14246           /* Since the alignment is 2, we have to check 2 or 0 bytes;
14247              check if is aligned to 4 - byte.  */
14248
14249           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
14250                                     NULL_RTX, 0, OPTAB_WIDEN);
14251
14252           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14253                                    Pmode, 1, align_4_label);
14254         }
14255
14256       mem = change_address (src, QImode, out);
14257
14258       /* Now compare the bytes.  */
14259
14260       /* Compare the first n unaligned byte on a byte per byte basis.  */
14261       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
14262                                QImode, 1, end_0_label);
14263
14264       /* Increment the address.  */
14265       if (TARGET_64BIT)
14266         emit_insn (gen_adddi3 (out, out, const1_rtx));
14267       else
14268         emit_insn (gen_addsi3 (out, out, const1_rtx));
14269
14270       /* Not needed with an alignment of 2 */
14271       if (align != 2)
14272         {
14273           emit_label (align_2_label);
14274
14275           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14276                                    end_0_label);
14277
14278           if (TARGET_64BIT)
14279             emit_insn (gen_adddi3 (out, out, const1_rtx));
14280           else
14281             emit_insn (gen_addsi3 (out, out, const1_rtx));
14282
14283           emit_label (align_3_label);
14284         }
14285
14286       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14287                                end_0_label);
14288
14289       if (TARGET_64BIT)
14290         emit_insn (gen_adddi3 (out, out, const1_rtx));
14291       else
14292         emit_insn (gen_addsi3 (out, out, const1_rtx));
14293     }
14294
14295   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
14296      align this loop.  It gives only huge programs, but does not help to
14297      speed up.  */
14298   emit_label (align_4_label);
14299
14300   mem = change_address (src, SImode, out);
14301   emit_move_insn (scratch, mem);
14302   if (TARGET_64BIT)
14303     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
14304   else
14305     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
14306
14307   /* This formula yields a nonzero result iff one of the bytes is zero.
14308      This saves three branches inside loop and many cycles.  */
14309
14310   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
14311   emit_insn (gen_one_cmplsi2 (scratch, scratch));
14312   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
14313   emit_insn (gen_andsi3 (tmpreg, tmpreg,
14314                          gen_int_mode (0x80808080, SImode)));
14315   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
14316                            align_4_label);
14317
14318   if (TARGET_CMOVE)
14319     {
14320        rtx reg = gen_reg_rtx (SImode);
14321        rtx reg2 = gen_reg_rtx (Pmode);
14322        emit_move_insn (reg, tmpreg);
14323        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
14324
14325        /* If zero is not in the first two bytes, move two bytes forward.  */
14326        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14327        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14328        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14329        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
14330                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
14331                                                      reg,
14332                                                      tmpreg)));
14333        /* Emit lea manually to avoid clobbering of flags.  */
14334        emit_insn (gen_rtx_SET (SImode, reg2,
14335                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
14336
14337        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14338        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14339        emit_insn (gen_rtx_SET (VOIDmode, out,
14340                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
14341                                                      reg2,
14342                                                      out)));
14343
14344     }
14345   else
14346     {
14347        rtx end_2_label = gen_label_rtx ();
14348        /* Is zero in the first two bytes? */
14349
14350        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14351        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14352        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
14353        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
14354                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
14355                             pc_rtx);
14356        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
14357        JUMP_LABEL (tmp) = end_2_label;
14358
14359        /* Not in the first two.  Move two bytes forward.  */
14360        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
14361        if (TARGET_64BIT)
14362          emit_insn (gen_adddi3 (out, out, const2_rtx));
14363        else
14364          emit_insn (gen_addsi3 (out, out, const2_rtx));
14365
14366        emit_label (end_2_label);
14367
14368     }
14369
14370   /* Avoid branch in fixing the byte.  */
14371   tmpreg = gen_lowpart (QImode, tmpreg);
14372   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
14373   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
14374   if (TARGET_64BIT)
14375     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
14376   else
14377     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
14378
14379   emit_label (end_0_label);
14380 }
14381
14382 void
14383 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
14384                   rtx callarg2 ATTRIBUTE_UNUSED,
14385                   rtx pop, int sibcall)
14386 {
14387   rtx use = NULL, call;
14388
14389   if (pop == const0_rtx)
14390     pop = NULL;
14391   gcc_assert (!TARGET_64BIT || !pop);
14392
14393   if (TARGET_MACHO && !TARGET_64BIT)
14394     {
14395 #if TARGET_MACHO
14396       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
14397         fnaddr = machopic_indirect_call_target (fnaddr);
14398 #endif
14399     }
14400   else
14401     {
14402       /* Static functions and indirect calls don't need the pic register.  */
14403       if (! TARGET_64BIT && flag_pic
14404           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
14405           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
14406         use_reg (&use, pic_offset_table_rtx);
14407     }
14408
14409   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
14410     {
14411       rtx al = gen_rtx_REG (QImode, 0);
14412       emit_move_insn (al, callarg2);
14413       use_reg (&use, al);
14414     }
14415
14416   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
14417     {
14418       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
14419       fnaddr = gen_rtx_MEM (QImode, fnaddr);
14420     }
14421   if (sibcall && TARGET_64BIT
14422       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
14423     {
14424       rtx addr;
14425       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
14426       fnaddr = gen_rtx_REG (Pmode, R11_REG);
14427       emit_move_insn (fnaddr, addr);
14428       fnaddr = gen_rtx_MEM (QImode, fnaddr);
14429     }
14430
14431   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
14432   if (retval)
14433     call = gen_rtx_SET (VOIDmode, retval, call);
14434   if (pop)
14435     {
14436       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
14437       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
14438       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
14439     }
14440
14441   call = emit_call_insn (call);
14442   if (use)
14443     CALL_INSN_FUNCTION_USAGE (call) = use;
14444 }
14445
14446 \f
14447 /* Clear stack slot assignments remembered from previous functions.
14448    This is called from INIT_EXPANDERS once before RTL is emitted for each
14449    function.  */
14450
14451 static struct machine_function *
14452 ix86_init_machine_status (void)
14453 {
14454   struct machine_function *f;
14455
14456   f = ggc_alloc_cleared (sizeof (struct machine_function));
14457   f->use_fast_prologue_epilogue_nregs = -1;
14458   f->tls_descriptor_call_expanded_p = 0;
14459
14460   return f;
14461 }
14462
14463 /* Return a MEM corresponding to a stack slot with mode MODE.
14464    Allocate a new slot if necessary.
14465
14466    The RTL for a function can have several slots available: N is
14467    which slot to use.  */
14468
14469 rtx
14470 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
14471 {
14472   struct stack_local_entry *s;
14473
14474   gcc_assert (n < MAX_386_STACK_LOCALS);
14475
14476   for (s = ix86_stack_locals; s; s = s->next)
14477     if (s->mode == mode && s->n == n)
14478       return copy_rtx (s->rtl);
14479
14480   s = (struct stack_local_entry *)
14481     ggc_alloc (sizeof (struct stack_local_entry));
14482   s->n = n;
14483   s->mode = mode;
14484   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
14485
14486   s->next = ix86_stack_locals;
14487   ix86_stack_locals = s;
14488   return s->rtl;
14489 }
14490
14491 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
14492
14493 static GTY(()) rtx ix86_tls_symbol;
14494 rtx
14495 ix86_tls_get_addr (void)
14496 {
14497
14498   if (!ix86_tls_symbol)
14499     {
14500       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
14501                                             (TARGET_ANY_GNU_TLS
14502                                              && !TARGET_64BIT)
14503                                             ? "___tls_get_addr"
14504                                             : "__tls_get_addr");
14505     }
14506
14507   return ix86_tls_symbol;
14508 }
14509
14510 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
14511
14512 static GTY(()) rtx ix86_tls_module_base_symbol;
14513 rtx
14514 ix86_tls_module_base (void)
14515 {
14516
14517   if (!ix86_tls_module_base_symbol)
14518     {
14519       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
14520                                                         "_TLS_MODULE_BASE_");
14521       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
14522         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
14523     }
14524
14525   return ix86_tls_module_base_symbol;
14526 }
14527 \f
14528 /* Calculate the length of the memory address in the instruction
14529    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
14530
14531 int
14532 memory_address_length (rtx addr)
14533 {
14534   struct ix86_address parts;
14535   rtx base, index, disp;
14536   int len;
14537   int ok;
14538
14539   if (GET_CODE (addr) == PRE_DEC
14540       || GET_CODE (addr) == POST_INC
14541       || GET_CODE (addr) == PRE_MODIFY
14542       || GET_CODE (addr) == POST_MODIFY)
14543     return 0;
14544
14545   ok = ix86_decompose_address (addr, &parts);
14546   gcc_assert (ok);
14547
14548   if (parts.base && GET_CODE (parts.base) == SUBREG)
14549     parts.base = SUBREG_REG (parts.base);
14550   if (parts.index && GET_CODE (parts.index) == SUBREG)
14551     parts.index = SUBREG_REG (parts.index);
14552
14553   base = parts.base;
14554   index = parts.index;
14555   disp = parts.disp;
14556   len = 0;
14557
14558   /* Rule of thumb:
14559        - esp as the base always wants an index,
14560        - ebp as the base always wants a displacement.  */
14561
14562   /* Register Indirect.  */
14563   if (base && !index && !disp)
14564     {
14565       /* esp (for its index) and ebp (for its displacement) need
14566          the two-byte modrm form.  */
14567       if (addr == stack_pointer_rtx
14568           || addr == arg_pointer_rtx
14569           || addr == frame_pointer_rtx
14570           || addr == hard_frame_pointer_rtx)
14571         len = 1;
14572     }
14573
14574   /* Direct Addressing.  */
14575   else if (disp && !base && !index)
14576     len = 4;
14577
14578   else
14579     {
14580       /* Find the length of the displacement constant.  */
14581       if (disp)
14582         {
14583           if (base && satisfies_constraint_K (disp))
14584             len = 1;
14585           else
14586             len = 4;
14587         }
14588       /* ebp always wants a displacement.  */
14589       else if (base == hard_frame_pointer_rtx)
14590         len = 1;
14591
14592       /* An index requires the two-byte modrm form....  */
14593       if (index
14594           /* ...like esp, which always wants an index.  */
14595           || base == stack_pointer_rtx
14596           || base == arg_pointer_rtx
14597           || base == frame_pointer_rtx)
14598         len += 1;
14599     }
14600
14601   return len;
14602 }
14603
14604 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
14605    is set, expect that insn have 8bit immediate alternative.  */
14606 int
14607 ix86_attr_length_immediate_default (rtx insn, int shortform)
14608 {
14609   int len = 0;
14610   int i;
14611   extract_insn_cached (insn);
14612   for (i = recog_data.n_operands - 1; i >= 0; --i)
14613     if (CONSTANT_P (recog_data.operand[i]))
14614       {
14615         gcc_assert (!len);
14616         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
14617           len = 1;
14618         else
14619           {
14620             switch (get_attr_mode (insn))
14621               {
14622                 case MODE_QI:
14623                   len+=1;
14624                   break;
14625                 case MODE_HI:
14626                   len+=2;
14627                   break;
14628                 case MODE_SI:
14629                   len+=4;
14630                   break;
14631                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
14632                 case MODE_DI:
14633                   len+=4;
14634                   break;
14635                 default:
14636                   fatal_insn ("unknown insn mode", insn);
14637               }
14638           }
14639       }
14640   return len;
14641 }
14642 /* Compute default value for "length_address" attribute.  */
14643 int
14644 ix86_attr_length_address_default (rtx insn)
14645 {
14646   int i;
14647
14648   if (get_attr_type (insn) == TYPE_LEA)
14649     {
14650       rtx set = PATTERN (insn);
14651
14652       if (GET_CODE (set) == PARALLEL)
14653         set = XVECEXP (set, 0, 0);
14654
14655       gcc_assert (GET_CODE (set) == SET);
14656
14657       return memory_address_length (SET_SRC (set));
14658     }
14659
14660   extract_insn_cached (insn);
14661   for (i = recog_data.n_operands - 1; i >= 0; --i)
14662     if (GET_CODE (recog_data.operand[i]) == MEM)
14663       {
14664         return memory_address_length (XEXP (recog_data.operand[i], 0));
14665         break;
14666       }
14667   return 0;
14668 }
14669 \f
14670 /* Return the maximum number of instructions a cpu can issue.  */
14671
14672 static int
14673 ix86_issue_rate (void)
14674 {
14675   switch (ix86_tune)
14676     {
14677     case PROCESSOR_PENTIUM:
14678     case PROCESSOR_K6:
14679       return 2;
14680
14681     case PROCESSOR_PENTIUMPRO:
14682     case PROCESSOR_PENTIUM4:
14683     case PROCESSOR_ATHLON:
14684     case PROCESSOR_K8:
14685     case PROCESSOR_NOCONA:
14686     case PROCESSOR_GENERIC32:
14687     case PROCESSOR_GENERIC64:
14688       return 3;
14689
14690     case PROCESSOR_CORE2:
14691       return 4;
14692
14693     default:
14694       return 1;
14695     }
14696 }
14697
14698 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
14699    by DEP_INSN and nothing set by DEP_INSN.  */
14700
14701 static int
14702 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
14703 {
14704   rtx set, set2;
14705
14706   /* Simplify the test for uninteresting insns.  */
14707   if (insn_type != TYPE_SETCC
14708       && insn_type != TYPE_ICMOV
14709       && insn_type != TYPE_FCMOV
14710       && insn_type != TYPE_IBR)
14711     return 0;
14712
14713   if ((set = single_set (dep_insn)) != 0)
14714     {
14715       set = SET_DEST (set);
14716       set2 = NULL_RTX;
14717     }
14718   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
14719            && XVECLEN (PATTERN (dep_insn), 0) == 2
14720            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
14721            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
14722     {
14723       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
14724       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
14725     }
14726   else
14727     return 0;
14728
14729   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
14730     return 0;
14731
14732   /* This test is true if the dependent insn reads the flags but
14733      not any other potentially set register.  */
14734   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
14735     return 0;
14736
14737   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
14738     return 0;
14739
14740   return 1;
14741 }
14742
14743 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
14744    address with operands set by DEP_INSN.  */
14745
14746 static int
14747 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
14748 {
14749   rtx addr;
14750
14751   if (insn_type == TYPE_LEA
14752       && TARGET_PENTIUM)
14753     {
14754       addr = PATTERN (insn);
14755
14756       if (GET_CODE (addr) == PARALLEL)
14757         addr = XVECEXP (addr, 0, 0);
14758
14759       gcc_assert (GET_CODE (addr) == SET);
14760
14761       addr = SET_SRC (addr);
14762     }
14763   else
14764     {
14765       int i;
14766       extract_insn_cached (insn);
14767       for (i = recog_data.n_operands - 1; i >= 0; --i)
14768         if (GET_CODE (recog_data.operand[i]) == MEM)
14769           {
14770             addr = XEXP (recog_data.operand[i], 0);
14771             goto found;
14772           }
14773       return 0;
14774     found:;
14775     }
14776
14777   return modified_in_p (addr, dep_insn);
14778 }
14779
14780 static int
14781 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
14782 {
14783   enum attr_type insn_type, dep_insn_type;
14784   enum attr_memory memory;
14785   rtx set, set2;
14786   int dep_insn_code_number;
14787
14788   /* Anti and output dependencies have zero cost on all CPUs.  */
14789   if (REG_NOTE_KIND (link) != 0)
14790     return 0;
14791
14792   dep_insn_code_number = recog_memoized (dep_insn);
14793
14794   /* If we can't recognize the insns, we can't really do anything.  */
14795   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
14796     return cost;
14797
14798   insn_type = get_attr_type (insn);
14799   dep_insn_type = get_attr_type (dep_insn);
14800
14801   switch (ix86_tune)
14802     {
14803     case PROCESSOR_PENTIUM:
14804       /* Address Generation Interlock adds a cycle of latency.  */
14805       if (ix86_agi_dependent (insn, dep_insn, insn_type))
14806         cost += 1;
14807
14808       /* ??? Compares pair with jump/setcc.  */
14809       if (ix86_flags_dependent (insn, dep_insn, insn_type))
14810         cost = 0;
14811
14812       /* Floating point stores require value to be ready one cycle earlier.  */
14813       if (insn_type == TYPE_FMOV
14814           && get_attr_memory (insn) == MEMORY_STORE
14815           && !ix86_agi_dependent (insn, dep_insn, insn_type))
14816         cost += 1;
14817       break;
14818
14819     case PROCESSOR_PENTIUMPRO:
14820       memory = get_attr_memory (insn);
14821
14822       /* INT->FP conversion is expensive.  */
14823       if (get_attr_fp_int_src (dep_insn))
14824         cost += 5;
14825
14826       /* There is one cycle extra latency between an FP op and a store.  */
14827       if (insn_type == TYPE_FMOV
14828           && (set = single_set (dep_insn)) != NULL_RTX
14829           && (set2 = single_set (insn)) != NULL_RTX
14830           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
14831           && GET_CODE (SET_DEST (set2)) == MEM)
14832         cost += 1;
14833
14834       /* Show ability of reorder buffer to hide latency of load by executing
14835          in parallel with previous instruction in case
14836          previous instruction is not needed to compute the address.  */
14837       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
14838           && !ix86_agi_dependent (insn, dep_insn, insn_type))
14839         {
14840           /* Claim moves to take one cycle, as core can issue one load
14841              at time and the next load can start cycle later.  */
14842           if (dep_insn_type == TYPE_IMOV
14843               || dep_insn_type == TYPE_FMOV)
14844             cost = 1;
14845           else if (cost > 1)
14846             cost--;
14847         }
14848       break;
14849
14850     case PROCESSOR_K6:
14851       memory = get_attr_memory (insn);
14852
14853       /* The esp dependency is resolved before the instruction is really
14854          finished.  */
14855       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
14856           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
14857         return 1;
14858
14859       /* INT->FP conversion is expensive.  */
14860       if (get_attr_fp_int_src (dep_insn))
14861         cost += 5;
14862
14863       /* Show ability of reorder buffer to hide latency of load by executing
14864          in parallel with previous instruction in case
14865          previous instruction is not needed to compute the address.  */
14866       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
14867           && !ix86_agi_dependent (insn, dep_insn, insn_type))
14868         {
14869           /* Claim moves to take one cycle, as core can issue one load
14870              at time and the next load can start cycle later.  */
14871           if (dep_insn_type == TYPE_IMOV
14872               || dep_insn_type == TYPE_FMOV)
14873             cost = 1;
14874           else if (cost > 2)
14875             cost -= 2;
14876           else
14877             cost = 1;
14878         }
14879       break;
14880
14881     case PROCESSOR_ATHLON:
14882     case PROCESSOR_K8:
14883     case PROCESSOR_GENERIC32:
14884     case PROCESSOR_GENERIC64:
14885       memory = get_attr_memory (insn);
14886
14887       /* Show ability of reorder buffer to hide latency of load by executing
14888          in parallel with previous instruction in case
14889          previous instruction is not needed to compute the address.  */
14890       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
14891           && !ix86_agi_dependent (insn, dep_insn, insn_type))
14892         {
14893           enum attr_unit unit = get_attr_unit (insn);
14894           int loadcost = 3;
14895
14896           /* Because of the difference between the length of integer and
14897              floating unit pipeline preparation stages, the memory operands
14898              for floating point are cheaper.
14899
14900              ??? For Athlon it the difference is most probably 2.  */
14901           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
14902             loadcost = 3;
14903           else
14904             loadcost = TARGET_ATHLON ? 2 : 0;
14905
14906           if (cost >= loadcost)
14907             cost -= loadcost;
14908           else
14909             cost = 0;
14910         }
14911
14912     default:
14913       break;
14914     }
14915
14916   return cost;
14917 }
14918
14919 /* How many alternative schedules to try.  This should be as wide as the
14920    scheduling freedom in the DFA, but no wider.  Making this value too
14921    large results extra work for the scheduler.  */
14922
14923 static int
14924 ia32_multipass_dfa_lookahead (void)
14925 {
14926   if (ix86_tune == PROCESSOR_PENTIUM)
14927     return 2;
14928
14929   if (ix86_tune == PROCESSOR_PENTIUMPRO
14930       || ix86_tune == PROCESSOR_K6)
14931     return 1;
14932
14933   else
14934     return 0;
14935 }
14936
14937 \f
14938 /* Compute the alignment given to a constant that is being placed in memory.
14939    EXP is the constant and ALIGN is the alignment that the object would
14940    ordinarily have.
14941    The value of this function is used instead of that alignment to align
14942    the object.  */
14943
14944 int
14945 ix86_constant_alignment (tree exp, int align)
14946 {
14947   if (TREE_CODE (exp) == REAL_CST)
14948     {
14949       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
14950         return 64;
14951       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
14952         return 128;
14953     }
14954   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
14955            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
14956     return BITS_PER_WORD;
14957
14958   return align;
14959 }
14960
14961 /* Compute the alignment for a static variable.
14962    TYPE is the data type, and ALIGN is the alignment that
14963    the object would ordinarily have.  The value of this function is used
14964    instead of that alignment to align the object.  */
14965
14966 int
14967 ix86_data_alignment (tree type, int align)
14968 {
14969   int max_align = optimize_size ? BITS_PER_WORD : 256;
14970
14971   if (AGGREGATE_TYPE_P (type)
14972       && TYPE_SIZE (type)
14973       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
14974       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
14975           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
14976       && align < max_align)
14977     align = max_align;
14978
14979   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
14980      to 16byte boundary.  */
14981   if (TARGET_64BIT)
14982     {
14983       if (AGGREGATE_TYPE_P (type)
14984            && TYPE_SIZE (type)
14985            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
14986            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
14987                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
14988         return 128;
14989     }
14990
14991   if (TREE_CODE (type) == ARRAY_TYPE)
14992     {
14993       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
14994         return 64;
14995       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
14996         return 128;
14997     }
14998   else if (TREE_CODE (type) == COMPLEX_TYPE)
14999     {
15000
15001       if (TYPE_MODE (type) == DCmode && align < 64)
15002         return 64;
15003       if (TYPE_MODE (type) == XCmode && align < 128)
15004         return 128;
15005     }
15006   else if ((TREE_CODE (type) == RECORD_TYPE
15007             || TREE_CODE (type) == UNION_TYPE
15008             || TREE_CODE (type) == QUAL_UNION_TYPE)
15009            && TYPE_FIELDS (type))
15010     {
15011       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15012         return 64;
15013       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15014         return 128;
15015     }
15016   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15017            || TREE_CODE (type) == INTEGER_TYPE)
15018     {
15019       if (TYPE_MODE (type) == DFmode && align < 64)
15020         return 64;
15021       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15022         return 128;
15023     }
15024
15025   return align;
15026 }
15027
15028 /* Compute the alignment for a local variable.
15029    TYPE is the data type, and ALIGN is the alignment that
15030    the object would ordinarily have.  The value of this macro is used
15031    instead of that alignment to align the object.  */
15032
15033 int
15034 ix86_local_alignment (tree type, int align)
15035 {
15036   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15037      to 16byte boundary.  */
15038   if (TARGET_64BIT)
15039     {
15040       if (AGGREGATE_TYPE_P (type)
15041            && TYPE_SIZE (type)
15042            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15043            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
15044                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15045         return 128;
15046     }
15047   if (TREE_CODE (type) == ARRAY_TYPE)
15048     {
15049       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15050         return 64;
15051       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15052         return 128;
15053     }
15054   else if (TREE_CODE (type) == COMPLEX_TYPE)
15055     {
15056       if (TYPE_MODE (type) == DCmode && align < 64)
15057         return 64;
15058       if (TYPE_MODE (type) == XCmode && align < 128)
15059         return 128;
15060     }
15061   else if ((TREE_CODE (type) == RECORD_TYPE
15062             || TREE_CODE (type) == UNION_TYPE
15063             || TREE_CODE (type) == QUAL_UNION_TYPE)
15064            && TYPE_FIELDS (type))
15065     {
15066       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15067         return 64;
15068       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15069         return 128;
15070     }
15071   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15072            || TREE_CODE (type) == INTEGER_TYPE)
15073     {
15074
15075       if (TYPE_MODE (type) == DFmode && align < 64)
15076         return 64;
15077       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15078         return 128;
15079     }
15080   return align;
15081 }
15082 \f
15083 /* Emit RTL insns to initialize the variable parts of a trampoline.
15084    FNADDR is an RTX for the address of the function's pure code.
15085    CXT is an RTX for the static chain value for the function.  */
15086 void
15087 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
15088 {
15089   if (!TARGET_64BIT)
15090     {
15091       /* Compute offset from the end of the jmp to the target function.  */
15092       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
15093                                plus_constant (tramp, 10),
15094                                NULL_RTX, 1, OPTAB_DIRECT);
15095       emit_move_insn (gen_rtx_MEM (QImode, tramp),
15096                       gen_int_mode (0xb9, QImode));
15097       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
15098       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
15099                       gen_int_mode (0xe9, QImode));
15100       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
15101     }
15102   else
15103     {
15104       int offset = 0;
15105       /* Try to load address using shorter movl instead of movabs.
15106          We may want to support movq for kernel mode, but kernel does not use
15107          trampolines at the moment.  */
15108       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
15109         {
15110           fnaddr = copy_to_mode_reg (DImode, fnaddr);
15111           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15112                           gen_int_mode (0xbb41, HImode));
15113           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
15114                           gen_lowpart (SImode, fnaddr));
15115           offset += 6;
15116         }
15117       else
15118         {
15119           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15120                           gen_int_mode (0xbb49, HImode));
15121           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15122                           fnaddr);
15123           offset += 10;
15124         }
15125       /* Load static chain using movabs to r10.  */
15126       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15127                       gen_int_mode (0xba49, HImode));
15128       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15129                       cxt);
15130       offset += 10;
15131       /* Jump to the r11 */
15132       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15133                       gen_int_mode (0xff49, HImode));
15134       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
15135                       gen_int_mode (0xe3, QImode));
15136       offset += 3;
15137       gcc_assert (offset <= TRAMPOLINE_SIZE);
15138     }
15139
15140 #ifdef ENABLE_EXECUTE_STACK
15141   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
15142                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
15143 #endif
15144 }
15145 \f
15146 /* Codes for all the SSE/MMX builtins.  */
15147 enum ix86_builtins
15148 {
15149   IX86_BUILTIN_ADDPS,
15150   IX86_BUILTIN_ADDSS,
15151   IX86_BUILTIN_DIVPS,
15152   IX86_BUILTIN_DIVSS,
15153   IX86_BUILTIN_MULPS,
15154   IX86_BUILTIN_MULSS,
15155   IX86_BUILTIN_SUBPS,
15156   IX86_BUILTIN_SUBSS,
15157
15158   IX86_BUILTIN_CMPEQPS,
15159   IX86_BUILTIN_CMPLTPS,
15160   IX86_BUILTIN_CMPLEPS,
15161   IX86_BUILTIN_CMPGTPS,
15162   IX86_BUILTIN_CMPGEPS,
15163   IX86_BUILTIN_CMPNEQPS,
15164   IX86_BUILTIN_CMPNLTPS,
15165   IX86_BUILTIN_CMPNLEPS,
15166   IX86_BUILTIN_CMPNGTPS,
15167   IX86_BUILTIN_CMPNGEPS,
15168   IX86_BUILTIN_CMPORDPS,
15169   IX86_BUILTIN_CMPUNORDPS,
15170   IX86_BUILTIN_CMPEQSS,
15171   IX86_BUILTIN_CMPLTSS,
15172   IX86_BUILTIN_CMPLESS,
15173   IX86_BUILTIN_CMPNEQSS,
15174   IX86_BUILTIN_CMPNLTSS,
15175   IX86_BUILTIN_CMPNLESS,
15176   IX86_BUILTIN_CMPNGTSS,
15177   IX86_BUILTIN_CMPNGESS,
15178   IX86_BUILTIN_CMPORDSS,
15179   IX86_BUILTIN_CMPUNORDSS,
15180
15181   IX86_BUILTIN_COMIEQSS,
15182   IX86_BUILTIN_COMILTSS,
15183   IX86_BUILTIN_COMILESS,
15184   IX86_BUILTIN_COMIGTSS,
15185   IX86_BUILTIN_COMIGESS,
15186   IX86_BUILTIN_COMINEQSS,
15187   IX86_BUILTIN_UCOMIEQSS,
15188   IX86_BUILTIN_UCOMILTSS,
15189   IX86_BUILTIN_UCOMILESS,
15190   IX86_BUILTIN_UCOMIGTSS,
15191   IX86_BUILTIN_UCOMIGESS,
15192   IX86_BUILTIN_UCOMINEQSS,
15193
15194   IX86_BUILTIN_CVTPI2PS,
15195   IX86_BUILTIN_CVTPS2PI,
15196   IX86_BUILTIN_CVTSI2SS,
15197   IX86_BUILTIN_CVTSI642SS,
15198   IX86_BUILTIN_CVTSS2SI,
15199   IX86_BUILTIN_CVTSS2SI64,
15200   IX86_BUILTIN_CVTTPS2PI,
15201   IX86_BUILTIN_CVTTSS2SI,
15202   IX86_BUILTIN_CVTTSS2SI64,
15203
15204   IX86_BUILTIN_MAXPS,
15205   IX86_BUILTIN_MAXSS,
15206   IX86_BUILTIN_MINPS,
15207   IX86_BUILTIN_MINSS,
15208
15209   IX86_BUILTIN_LOADUPS,
15210   IX86_BUILTIN_STOREUPS,
15211   IX86_BUILTIN_MOVSS,
15212
15213   IX86_BUILTIN_MOVHLPS,
15214   IX86_BUILTIN_MOVLHPS,
15215   IX86_BUILTIN_LOADHPS,
15216   IX86_BUILTIN_LOADLPS,
15217   IX86_BUILTIN_STOREHPS,
15218   IX86_BUILTIN_STORELPS,
15219
15220   IX86_BUILTIN_MASKMOVQ,
15221   IX86_BUILTIN_MOVMSKPS,
15222   IX86_BUILTIN_PMOVMSKB,
15223
15224   IX86_BUILTIN_MOVNTPS,
15225   IX86_BUILTIN_MOVNTQ,
15226
15227   IX86_BUILTIN_LOADDQU,
15228   IX86_BUILTIN_STOREDQU,
15229
15230   IX86_BUILTIN_PACKSSWB,
15231   IX86_BUILTIN_PACKSSDW,
15232   IX86_BUILTIN_PACKUSWB,
15233
15234   IX86_BUILTIN_PADDB,
15235   IX86_BUILTIN_PADDW,
15236   IX86_BUILTIN_PADDD,
15237   IX86_BUILTIN_PADDQ,
15238   IX86_BUILTIN_PADDSB,
15239   IX86_BUILTIN_PADDSW,
15240   IX86_BUILTIN_PADDUSB,
15241   IX86_BUILTIN_PADDUSW,
15242   IX86_BUILTIN_PSUBB,
15243   IX86_BUILTIN_PSUBW,
15244   IX86_BUILTIN_PSUBD,
15245   IX86_BUILTIN_PSUBQ,
15246   IX86_BUILTIN_PSUBSB,
15247   IX86_BUILTIN_PSUBSW,
15248   IX86_BUILTIN_PSUBUSB,
15249   IX86_BUILTIN_PSUBUSW,
15250
15251   IX86_BUILTIN_PAND,
15252   IX86_BUILTIN_PANDN,
15253   IX86_BUILTIN_POR,
15254   IX86_BUILTIN_PXOR,
15255
15256   IX86_BUILTIN_PAVGB,
15257   IX86_BUILTIN_PAVGW,
15258
15259   IX86_BUILTIN_PCMPEQB,
15260   IX86_BUILTIN_PCMPEQW,
15261   IX86_BUILTIN_PCMPEQD,
15262   IX86_BUILTIN_PCMPGTB,
15263   IX86_BUILTIN_PCMPGTW,
15264   IX86_BUILTIN_PCMPGTD,
15265
15266   IX86_BUILTIN_PMADDWD,
15267
15268   IX86_BUILTIN_PMAXSW,
15269   IX86_BUILTIN_PMAXUB,
15270   IX86_BUILTIN_PMINSW,
15271   IX86_BUILTIN_PMINUB,
15272
15273   IX86_BUILTIN_PMULHUW,
15274   IX86_BUILTIN_PMULHW,
15275   IX86_BUILTIN_PMULLW,
15276
15277   IX86_BUILTIN_PSADBW,
15278   IX86_BUILTIN_PSHUFW,
15279
15280   IX86_BUILTIN_PSLLW,
15281   IX86_BUILTIN_PSLLD,
15282   IX86_BUILTIN_PSLLQ,
15283   IX86_BUILTIN_PSRAW,
15284   IX86_BUILTIN_PSRAD,
15285   IX86_BUILTIN_PSRLW,
15286   IX86_BUILTIN_PSRLD,
15287   IX86_BUILTIN_PSRLQ,
15288   IX86_BUILTIN_PSLLWI,
15289   IX86_BUILTIN_PSLLDI,
15290   IX86_BUILTIN_PSLLQI,
15291   IX86_BUILTIN_PSRAWI,
15292   IX86_BUILTIN_PSRADI,
15293   IX86_BUILTIN_PSRLWI,
15294   IX86_BUILTIN_PSRLDI,
15295   IX86_BUILTIN_PSRLQI,
15296
15297   IX86_BUILTIN_PUNPCKHBW,
15298   IX86_BUILTIN_PUNPCKHWD,
15299   IX86_BUILTIN_PUNPCKHDQ,
15300   IX86_BUILTIN_PUNPCKLBW,
15301   IX86_BUILTIN_PUNPCKLWD,
15302   IX86_BUILTIN_PUNPCKLDQ,
15303
15304   IX86_BUILTIN_SHUFPS,
15305
15306   IX86_BUILTIN_RCPPS,
15307   IX86_BUILTIN_RCPSS,
15308   IX86_BUILTIN_RSQRTPS,
15309   IX86_BUILTIN_RSQRTSS,
15310   IX86_BUILTIN_SQRTPS,
15311   IX86_BUILTIN_SQRTSS,
15312
15313   IX86_BUILTIN_UNPCKHPS,
15314   IX86_BUILTIN_UNPCKLPS,
15315
15316   IX86_BUILTIN_ANDPS,
15317   IX86_BUILTIN_ANDNPS,
15318   IX86_BUILTIN_ORPS,
15319   IX86_BUILTIN_XORPS,
15320
15321   IX86_BUILTIN_EMMS,
15322   IX86_BUILTIN_LDMXCSR,
15323   IX86_BUILTIN_STMXCSR,
15324   IX86_BUILTIN_SFENCE,
15325
15326   /* 3DNow! Original */
15327   IX86_BUILTIN_FEMMS,
15328   IX86_BUILTIN_PAVGUSB,
15329   IX86_BUILTIN_PF2ID,
15330   IX86_BUILTIN_PFACC,
15331   IX86_BUILTIN_PFADD,
15332   IX86_BUILTIN_PFCMPEQ,
15333   IX86_BUILTIN_PFCMPGE,
15334   IX86_BUILTIN_PFCMPGT,
15335   IX86_BUILTIN_PFMAX,
15336   IX86_BUILTIN_PFMIN,
15337   IX86_BUILTIN_PFMUL,
15338   IX86_BUILTIN_PFRCP,
15339   IX86_BUILTIN_PFRCPIT1,
15340   IX86_BUILTIN_PFRCPIT2,
15341   IX86_BUILTIN_PFRSQIT1,
15342   IX86_BUILTIN_PFRSQRT,
15343   IX86_BUILTIN_PFSUB,
15344   IX86_BUILTIN_PFSUBR,
15345   IX86_BUILTIN_PI2FD,
15346   IX86_BUILTIN_PMULHRW,
15347
15348   /* 3DNow! Athlon Extensions */
15349   IX86_BUILTIN_PF2IW,
15350   IX86_BUILTIN_PFNACC,
15351   IX86_BUILTIN_PFPNACC,
15352   IX86_BUILTIN_PI2FW,
15353   IX86_BUILTIN_PSWAPDSI,
15354   IX86_BUILTIN_PSWAPDSF,
15355
15356   /* SSE2 */
15357   IX86_BUILTIN_ADDPD,
15358   IX86_BUILTIN_ADDSD,
15359   IX86_BUILTIN_DIVPD,
15360   IX86_BUILTIN_DIVSD,
15361   IX86_BUILTIN_MULPD,
15362   IX86_BUILTIN_MULSD,
15363   IX86_BUILTIN_SUBPD,
15364   IX86_BUILTIN_SUBSD,
15365
15366   IX86_BUILTIN_CMPEQPD,
15367   IX86_BUILTIN_CMPLTPD,
15368   IX86_BUILTIN_CMPLEPD,
15369   IX86_BUILTIN_CMPGTPD,
15370   IX86_BUILTIN_CMPGEPD,
15371   IX86_BUILTIN_CMPNEQPD,
15372   IX86_BUILTIN_CMPNLTPD,
15373   IX86_BUILTIN_CMPNLEPD,
15374   IX86_BUILTIN_CMPNGTPD,
15375   IX86_BUILTIN_CMPNGEPD,
15376   IX86_BUILTIN_CMPORDPD,
15377   IX86_BUILTIN_CMPUNORDPD,
15378   IX86_BUILTIN_CMPNEPD,
15379   IX86_BUILTIN_CMPEQSD,
15380   IX86_BUILTIN_CMPLTSD,
15381   IX86_BUILTIN_CMPLESD,
15382   IX86_BUILTIN_CMPNEQSD,
15383   IX86_BUILTIN_CMPNLTSD,
15384   IX86_BUILTIN_CMPNLESD,
15385   IX86_BUILTIN_CMPORDSD,
15386   IX86_BUILTIN_CMPUNORDSD,
15387   IX86_BUILTIN_CMPNESD,
15388
15389   IX86_BUILTIN_COMIEQSD,
15390   IX86_BUILTIN_COMILTSD,
15391   IX86_BUILTIN_COMILESD,
15392   IX86_BUILTIN_COMIGTSD,
15393   IX86_BUILTIN_COMIGESD,
15394   IX86_BUILTIN_COMINEQSD,
15395   IX86_BUILTIN_UCOMIEQSD,
15396   IX86_BUILTIN_UCOMILTSD,
15397   IX86_BUILTIN_UCOMILESD,
15398   IX86_BUILTIN_UCOMIGTSD,
15399   IX86_BUILTIN_UCOMIGESD,
15400   IX86_BUILTIN_UCOMINEQSD,
15401
15402   IX86_BUILTIN_MAXPD,
15403   IX86_BUILTIN_MAXSD,
15404   IX86_BUILTIN_MINPD,
15405   IX86_BUILTIN_MINSD,
15406
15407   IX86_BUILTIN_ANDPD,
15408   IX86_BUILTIN_ANDNPD,
15409   IX86_BUILTIN_ORPD,
15410   IX86_BUILTIN_XORPD,
15411
15412   IX86_BUILTIN_SQRTPD,
15413   IX86_BUILTIN_SQRTSD,
15414
15415   IX86_BUILTIN_UNPCKHPD,
15416   IX86_BUILTIN_UNPCKLPD,
15417
15418   IX86_BUILTIN_SHUFPD,
15419
15420   IX86_BUILTIN_LOADUPD,
15421   IX86_BUILTIN_STOREUPD,
15422   IX86_BUILTIN_MOVSD,
15423
15424   IX86_BUILTIN_LOADHPD,
15425   IX86_BUILTIN_LOADLPD,
15426
15427   IX86_BUILTIN_CVTDQ2PD,
15428   IX86_BUILTIN_CVTDQ2PS,
15429
15430   IX86_BUILTIN_CVTPD2DQ,
15431   IX86_BUILTIN_CVTPD2PI,
15432   IX86_BUILTIN_CVTPD2PS,
15433   IX86_BUILTIN_CVTTPD2DQ,
15434   IX86_BUILTIN_CVTTPD2PI,
15435
15436   IX86_BUILTIN_CVTPI2PD,
15437   IX86_BUILTIN_CVTSI2SD,
15438   IX86_BUILTIN_CVTSI642SD,
15439
15440   IX86_BUILTIN_CVTSD2SI,
15441   IX86_BUILTIN_CVTSD2SI64,
15442   IX86_BUILTIN_CVTSD2SS,
15443   IX86_BUILTIN_CVTSS2SD,
15444   IX86_BUILTIN_CVTTSD2SI,
15445   IX86_BUILTIN_CVTTSD2SI64,
15446
15447   IX86_BUILTIN_CVTPS2DQ,
15448   IX86_BUILTIN_CVTPS2PD,
15449   IX86_BUILTIN_CVTTPS2DQ,
15450
15451   IX86_BUILTIN_MOVNTI,
15452   IX86_BUILTIN_MOVNTPD,
15453   IX86_BUILTIN_MOVNTDQ,
15454
15455   /* SSE2 MMX */
15456   IX86_BUILTIN_MASKMOVDQU,
15457   IX86_BUILTIN_MOVMSKPD,
15458   IX86_BUILTIN_PMOVMSKB128,
15459
15460   IX86_BUILTIN_PACKSSWB128,
15461   IX86_BUILTIN_PACKSSDW128,
15462   IX86_BUILTIN_PACKUSWB128,
15463
15464   IX86_BUILTIN_PADDB128,
15465   IX86_BUILTIN_PADDW128,
15466   IX86_BUILTIN_PADDD128,
15467   IX86_BUILTIN_PADDQ128,
15468   IX86_BUILTIN_PADDSB128,
15469   IX86_BUILTIN_PADDSW128,
15470   IX86_BUILTIN_PADDUSB128,
15471   IX86_BUILTIN_PADDUSW128,
15472   IX86_BUILTIN_PSUBB128,
15473   IX86_BUILTIN_PSUBW128,
15474   IX86_BUILTIN_PSUBD128,
15475   IX86_BUILTIN_PSUBQ128,
15476   IX86_BUILTIN_PSUBSB128,
15477   IX86_BUILTIN_PSUBSW128,
15478   IX86_BUILTIN_PSUBUSB128,
15479   IX86_BUILTIN_PSUBUSW128,
15480
15481   IX86_BUILTIN_PAND128,
15482   IX86_BUILTIN_PANDN128,
15483   IX86_BUILTIN_POR128,
15484   IX86_BUILTIN_PXOR128,
15485
15486   IX86_BUILTIN_PAVGB128,
15487   IX86_BUILTIN_PAVGW128,
15488
15489   IX86_BUILTIN_PCMPEQB128,
15490   IX86_BUILTIN_PCMPEQW128,
15491   IX86_BUILTIN_PCMPEQD128,
15492   IX86_BUILTIN_PCMPGTB128,
15493   IX86_BUILTIN_PCMPGTW128,
15494   IX86_BUILTIN_PCMPGTD128,
15495
15496   IX86_BUILTIN_PMADDWD128,
15497
15498   IX86_BUILTIN_PMAXSW128,
15499   IX86_BUILTIN_PMAXUB128,
15500   IX86_BUILTIN_PMINSW128,
15501   IX86_BUILTIN_PMINUB128,
15502
15503   IX86_BUILTIN_PMULUDQ,
15504   IX86_BUILTIN_PMULUDQ128,
15505   IX86_BUILTIN_PMULHUW128,
15506   IX86_BUILTIN_PMULHW128,
15507   IX86_BUILTIN_PMULLW128,
15508
15509   IX86_BUILTIN_PSADBW128,
15510   IX86_BUILTIN_PSHUFHW,
15511   IX86_BUILTIN_PSHUFLW,
15512   IX86_BUILTIN_PSHUFD,
15513
15514   IX86_BUILTIN_PSLLW128,
15515   IX86_BUILTIN_PSLLD128,
15516   IX86_BUILTIN_PSLLQ128,
15517   IX86_BUILTIN_PSRAW128,
15518   IX86_BUILTIN_PSRAD128,
15519   IX86_BUILTIN_PSRLW128,
15520   IX86_BUILTIN_PSRLD128,
15521   IX86_BUILTIN_PSRLQ128,
15522   IX86_BUILTIN_PSLLDQI128,
15523   IX86_BUILTIN_PSLLWI128,
15524   IX86_BUILTIN_PSLLDI128,
15525   IX86_BUILTIN_PSLLQI128,
15526   IX86_BUILTIN_PSRAWI128,
15527   IX86_BUILTIN_PSRADI128,
15528   IX86_BUILTIN_PSRLDQI128,
15529   IX86_BUILTIN_PSRLWI128,
15530   IX86_BUILTIN_PSRLDI128,
15531   IX86_BUILTIN_PSRLQI128,
15532
15533   IX86_BUILTIN_PUNPCKHBW128,
15534   IX86_BUILTIN_PUNPCKHWD128,
15535   IX86_BUILTIN_PUNPCKHDQ128,
15536   IX86_BUILTIN_PUNPCKHQDQ128,
15537   IX86_BUILTIN_PUNPCKLBW128,
15538   IX86_BUILTIN_PUNPCKLWD128,
15539   IX86_BUILTIN_PUNPCKLDQ128,
15540   IX86_BUILTIN_PUNPCKLQDQ128,
15541
15542   IX86_BUILTIN_CLFLUSH,
15543   IX86_BUILTIN_MFENCE,
15544   IX86_BUILTIN_LFENCE,
15545
15546   /* Prescott New Instructions.  */
15547   IX86_BUILTIN_ADDSUBPS,
15548   IX86_BUILTIN_HADDPS,
15549   IX86_BUILTIN_HSUBPS,
15550   IX86_BUILTIN_MOVSHDUP,
15551   IX86_BUILTIN_MOVSLDUP,
15552   IX86_BUILTIN_ADDSUBPD,
15553   IX86_BUILTIN_HADDPD,
15554   IX86_BUILTIN_HSUBPD,
15555   IX86_BUILTIN_LDDQU,
15556
15557   IX86_BUILTIN_MONITOR,
15558   IX86_BUILTIN_MWAIT,
15559
15560   /* SSSE3.  */
15561   IX86_BUILTIN_PHADDW,
15562   IX86_BUILTIN_PHADDD,
15563   IX86_BUILTIN_PHADDSW,
15564   IX86_BUILTIN_PHSUBW,
15565   IX86_BUILTIN_PHSUBD,
15566   IX86_BUILTIN_PHSUBSW,
15567   IX86_BUILTIN_PMADDUBSW,
15568   IX86_BUILTIN_PMULHRSW,
15569   IX86_BUILTIN_PSHUFB,
15570   IX86_BUILTIN_PSIGNB,
15571   IX86_BUILTIN_PSIGNW,
15572   IX86_BUILTIN_PSIGND,
15573   IX86_BUILTIN_PALIGNR,
15574   IX86_BUILTIN_PABSB,
15575   IX86_BUILTIN_PABSW,
15576   IX86_BUILTIN_PABSD,
15577
15578   IX86_BUILTIN_PHADDW128,
15579   IX86_BUILTIN_PHADDD128,
15580   IX86_BUILTIN_PHADDSW128,
15581   IX86_BUILTIN_PHSUBW128,
15582   IX86_BUILTIN_PHSUBD128,
15583   IX86_BUILTIN_PHSUBSW128,
15584   IX86_BUILTIN_PMADDUBSW128,
15585   IX86_BUILTIN_PMULHRSW128,
15586   IX86_BUILTIN_PSHUFB128,
15587   IX86_BUILTIN_PSIGNB128,
15588   IX86_BUILTIN_PSIGNW128,
15589   IX86_BUILTIN_PSIGND128,
15590   IX86_BUILTIN_PALIGNR128,
15591   IX86_BUILTIN_PABSB128,
15592   IX86_BUILTIN_PABSW128,
15593   IX86_BUILTIN_PABSD128,
15594
15595   IX86_BUILTIN_VEC_INIT_V2SI,
15596   IX86_BUILTIN_VEC_INIT_V4HI,
15597   IX86_BUILTIN_VEC_INIT_V8QI,
15598   IX86_BUILTIN_VEC_EXT_V2DF,
15599   IX86_BUILTIN_VEC_EXT_V2DI,
15600   IX86_BUILTIN_VEC_EXT_V4SF,
15601   IX86_BUILTIN_VEC_EXT_V4SI,
15602   IX86_BUILTIN_VEC_EXT_V8HI,
15603   IX86_BUILTIN_VEC_EXT_V2SI,
15604   IX86_BUILTIN_VEC_EXT_V4HI,
15605   IX86_BUILTIN_VEC_SET_V8HI,
15606   IX86_BUILTIN_VEC_SET_V4HI,
15607
15608   IX86_BUILTIN_MAX
15609 };
15610
15611 /* Table for the ix86 builtin decls.  */
15612 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
15613
15614 /* Add a ix86 target builtin function with CODE, NAME and TYPE.  Do so,
15615  * if the target_flags include one of MASK.  Stores the function decl
15616  * in the ix86_builtins array.
15617  * Returns the function decl or NULL_TREE, if the builtin was not added.  */
15618
15619 static inline tree
15620 def_builtin (int mask, const char *name, tree type, enum ix86_builtins code)
15621 {
15622   tree decl = NULL_TREE;
15623
15624   if (mask & target_flags
15625       && (!(mask & MASK_64BIT) || TARGET_64BIT))
15626     {
15627       decl = add_builtin_function (name, type, code, BUILT_IN_MD,
15628                                    NULL, NULL_TREE);
15629       ix86_builtins[(int) code] = decl;
15630     }
15631
15632   return decl;
15633 }
15634
15635 /* Like def_builtin, but also marks the function decl "const".  */
15636
15637 static inline tree
15638 def_builtin_const (int mask, const char *name, tree type,
15639                    enum ix86_builtins code)
15640 {
15641   tree decl = def_builtin (mask, name, type, code);
15642   if (decl)
15643     TREE_READONLY (decl) = 1;
15644   return decl;
15645 }
15646
15647 /* Bits for builtin_description.flag.  */
15648
15649 /* Set when we don't support the comparison natively, and should
15650    swap_comparison in order to support it.  */
15651 #define BUILTIN_DESC_SWAP_OPERANDS      1
15652
15653 struct builtin_description
15654 {
15655   const unsigned int mask;
15656   const enum insn_code icode;
15657   const char *const name;
15658   const enum ix86_builtins code;
15659   const enum rtx_code comparison;
15660   const unsigned int flag;
15661 };
15662
15663 static const struct builtin_description bdesc_comi[] =
15664 {
15665   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
15666   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
15667   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
15668   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
15669   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
15670   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
15671   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
15672   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
15673   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
15674   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
15675   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
15676   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
15677   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
15678   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
15679   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
15680   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
15681   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
15682   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
15683   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
15684   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
15685   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
15686   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
15687   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
15688   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
15689 };
15690
15691 static const struct builtin_description bdesc_2arg[] =
15692 {
15693   /* SSE */
15694   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
15695   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
15696   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
15697   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
15698   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
15699   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
15700   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
15701   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
15702
15703   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
15704   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
15705   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
15706   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
15707     BUILTIN_DESC_SWAP_OPERANDS },
15708   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
15709     BUILTIN_DESC_SWAP_OPERANDS },
15710   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
15711   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
15712   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
15713   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
15714   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
15715     BUILTIN_DESC_SWAP_OPERANDS },
15716   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
15717     BUILTIN_DESC_SWAP_OPERANDS },
15718   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
15719   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
15720   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
15721   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
15722   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
15723   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
15724   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
15725   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
15726   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
15727     BUILTIN_DESC_SWAP_OPERANDS },
15728   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
15729     BUILTIN_DESC_SWAP_OPERANDS },
15730   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
15731
15732   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
15733   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
15734   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
15735   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
15736
15737   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
15738   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
15739   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
15740   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
15741
15742   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
15743   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
15744   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
15745   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
15746   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
15747
15748   /* MMX */
15749   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
15750   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
15751   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
15752   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
15753   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
15754   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
15755   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
15756   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
15757
15758   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
15759   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
15760   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
15761   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
15762   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
15763   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
15764   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
15765   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
15766
15767   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
15768   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
15769   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
15770
15771   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
15772   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
15773   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
15774   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
15775
15776   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
15777   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
15778
15779   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
15780   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
15781   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
15782   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
15783   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
15784   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
15785
15786   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
15787   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
15788   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
15789   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
15790
15791   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
15792   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
15793   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
15794   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
15795   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
15796   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
15797
15798   /* Special.  */
15799   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
15800   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
15801   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
15802
15803   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
15804   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
15805   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
15806
15807   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
15808   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
15809   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
15810   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
15811   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
15812   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
15813
15814   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
15815   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
15816   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
15817   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
15818   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
15819   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
15820
15821   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
15822   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
15823   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
15824   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
15825
15826   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
15827   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
15828
15829   /* SSE2 */
15830   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
15831   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
15832   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
15833   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
15834   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
15835   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
15836   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
15837   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
15838
15839   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
15840   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
15841   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
15842   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
15843     BUILTIN_DESC_SWAP_OPERANDS },
15844   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
15845     BUILTIN_DESC_SWAP_OPERANDS },
15846   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
15847   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
15848   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
15849   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
15850   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
15851     BUILTIN_DESC_SWAP_OPERANDS },
15852   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
15853     BUILTIN_DESC_SWAP_OPERANDS },
15854   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
15855   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
15856   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
15857   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
15858   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
15859   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
15860   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
15861   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
15862   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
15863
15864   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
15865   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
15866   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
15867   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
15868
15869   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
15870   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
15871   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
15872   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
15873
15874   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
15875   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
15876   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
15877
15878   /* SSE2 MMX */
15879   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
15880   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
15881   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
15882   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
15883   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
15884   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
15885   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
15886   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
15887
15888   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
15889   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
15890   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
15891   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
15892   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
15893   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
15894   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
15895   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
15896
15897   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
15898   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
15899
15900   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
15901   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
15902   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
15903   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
15904
15905   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
15906   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
15907
15908   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
15909   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
15910   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
15911   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
15912   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
15913   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
15914
15915   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
15916   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
15917   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
15918   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
15919
15920   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
15921   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
15922   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
15923   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
15924   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
15925   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
15926   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
15927   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
15928
15929   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
15930   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
15931   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
15932
15933   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
15934   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
15935
15936   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
15937   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
15938
15939   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
15940   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
15941   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
15942
15943   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
15944   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
15945   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
15946
15947   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
15948   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
15949
15950   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
15951
15952   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
15953   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
15954   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
15955   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
15956
15957   /* SSE3 MMX */
15958   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
15959   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
15960   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
15961   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
15962   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
15963   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 },
15964
15965   /* SSSE3 */
15966   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, 0, 0 },
15967   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, 0, 0 },
15968   { MASK_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, 0, 0 },
15969   { MASK_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, 0, 0 },
15970   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, 0, 0 },
15971   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, 0, 0 },
15972   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, 0, 0 },
15973   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, 0, 0 },
15974   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, 0, 0 },
15975   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, 0, 0 },
15976   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, 0, 0 },
15977   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, 0, 0 },
15978   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, 0, 0 },
15979   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, 0, 0 },
15980   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, 0, 0 },
15981   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, 0, 0 },
15982   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, 0, 0 },
15983   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, 0, 0 },
15984   { MASK_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, 0, 0 },
15985   { MASK_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, 0, 0 },
15986   { MASK_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, 0, 0 },
15987   { MASK_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, 0, 0 },
15988   { MASK_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, 0, 0 },
15989   { MASK_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, 0, 0 }
15990 };
15991
15992 static const struct builtin_description bdesc_1arg[] =
15993 {
15994   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
15995   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
15996
15997   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
15998   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
15999   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
16000
16001   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
16002   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
16003   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
16004   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
16005   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
16006   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
16007
16008   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
16009   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
16010
16011   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
16012
16013   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
16014   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
16015
16016   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
16017   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
16018   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
16019   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
16020   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
16021
16022   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
16023
16024   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
16025   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
16026   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
16027   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
16028
16029   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
16030   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
16031   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
16032
16033   /* SSE3 */
16034   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
16035   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
16036
16037   /* SSSE3 */
16038   { MASK_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, 0, 0 },
16039   { MASK_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, 0, 0 },
16040   { MASK_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, 0, 0 },
16041   { MASK_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, 0, 0 },
16042   { MASK_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, 0, 0 },
16043   { MASK_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, 0, 0 },
16044 };
16045
16046 static void
16047 ix86_init_builtins (void)
16048 {
16049   if (TARGET_MMX)
16050     ix86_init_mmx_sse_builtins ();
16051 }
16052
16053 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
16054    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
16055    builtins.  */
16056 static void
16057 ix86_init_mmx_sse_builtins (void)
16058 {
16059   const struct builtin_description * d;
16060   size_t i;
16061
16062   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
16063   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
16064   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
16065   tree V2DI_type_node
16066     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
16067   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
16068   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
16069   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
16070   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
16071   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
16072   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
16073
16074   tree pchar_type_node = build_pointer_type (char_type_node);
16075   tree pcchar_type_node = build_pointer_type (
16076                              build_type_variant (char_type_node, 1, 0));
16077   tree pfloat_type_node = build_pointer_type (float_type_node);
16078   tree pcfloat_type_node = build_pointer_type (
16079                              build_type_variant (float_type_node, 1, 0));
16080   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
16081   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
16082   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
16083
16084   /* Comparisons.  */
16085   tree int_ftype_v4sf_v4sf
16086     = build_function_type_list (integer_type_node,
16087                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16088   tree v4si_ftype_v4sf_v4sf
16089     = build_function_type_list (V4SI_type_node,
16090                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16091   /* MMX/SSE/integer conversions.  */
16092   tree int_ftype_v4sf
16093     = build_function_type_list (integer_type_node,
16094                                 V4SF_type_node, NULL_TREE);
16095   tree int64_ftype_v4sf
16096     = build_function_type_list (long_long_integer_type_node,
16097                                 V4SF_type_node, NULL_TREE);
16098   tree int_ftype_v8qi
16099     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
16100   tree v4sf_ftype_v4sf_int
16101     = build_function_type_list (V4SF_type_node,
16102                                 V4SF_type_node, integer_type_node, NULL_TREE);
16103   tree v4sf_ftype_v4sf_int64
16104     = build_function_type_list (V4SF_type_node,
16105                                 V4SF_type_node, long_long_integer_type_node,
16106                                 NULL_TREE);
16107   tree v4sf_ftype_v4sf_v2si
16108     = build_function_type_list (V4SF_type_node,
16109                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
16110
16111   /* Miscellaneous.  */
16112   tree v8qi_ftype_v4hi_v4hi
16113     = build_function_type_list (V8QI_type_node,
16114                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16115   tree v4hi_ftype_v2si_v2si
16116     = build_function_type_list (V4HI_type_node,
16117                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16118   tree v4sf_ftype_v4sf_v4sf_int
16119     = build_function_type_list (V4SF_type_node,
16120                                 V4SF_type_node, V4SF_type_node,
16121                                 integer_type_node, NULL_TREE);
16122   tree v2si_ftype_v4hi_v4hi
16123     = build_function_type_list (V2SI_type_node,
16124                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16125   tree v4hi_ftype_v4hi_int
16126     = build_function_type_list (V4HI_type_node,
16127                                 V4HI_type_node, integer_type_node, NULL_TREE);
16128   tree v4hi_ftype_v4hi_di
16129     = build_function_type_list (V4HI_type_node,
16130                                 V4HI_type_node, long_long_unsigned_type_node,
16131                                 NULL_TREE);
16132   tree v2si_ftype_v2si_di
16133     = build_function_type_list (V2SI_type_node,
16134                                 V2SI_type_node, long_long_unsigned_type_node,
16135                                 NULL_TREE);
16136   tree void_ftype_void
16137     = build_function_type (void_type_node, void_list_node);
16138   tree void_ftype_unsigned
16139     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
16140   tree void_ftype_unsigned_unsigned
16141     = build_function_type_list (void_type_node, unsigned_type_node,
16142                                 unsigned_type_node, NULL_TREE);
16143   tree void_ftype_pcvoid_unsigned_unsigned
16144     = build_function_type_list (void_type_node, const_ptr_type_node,
16145                                 unsigned_type_node, unsigned_type_node,
16146                                 NULL_TREE);
16147   tree unsigned_ftype_void
16148     = build_function_type (unsigned_type_node, void_list_node);
16149   tree v2si_ftype_v4sf
16150     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
16151   /* Loads/stores.  */
16152   tree void_ftype_v8qi_v8qi_pchar
16153     = build_function_type_list (void_type_node,
16154                                 V8QI_type_node, V8QI_type_node,
16155                                 pchar_type_node, NULL_TREE);
16156   tree v4sf_ftype_pcfloat
16157     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
16158   /* @@@ the type is bogus */
16159   tree v4sf_ftype_v4sf_pv2si
16160     = build_function_type_list (V4SF_type_node,
16161                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
16162   tree void_ftype_pv2si_v4sf
16163     = build_function_type_list (void_type_node,
16164                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
16165   tree void_ftype_pfloat_v4sf
16166     = build_function_type_list (void_type_node,
16167                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
16168   tree void_ftype_pdi_di
16169     = build_function_type_list (void_type_node,
16170                                 pdi_type_node, long_long_unsigned_type_node,
16171                                 NULL_TREE);
16172   tree void_ftype_pv2di_v2di
16173     = build_function_type_list (void_type_node,
16174                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
16175   /* Normal vector unops.  */
16176   tree v4sf_ftype_v4sf
16177     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
16178   tree v16qi_ftype_v16qi
16179     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
16180   tree v8hi_ftype_v8hi
16181     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
16182   tree v4si_ftype_v4si
16183     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
16184   tree v8qi_ftype_v8qi
16185     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
16186   tree v4hi_ftype_v4hi
16187     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
16188
16189   /* Normal vector binops.  */
16190   tree v4sf_ftype_v4sf_v4sf
16191     = build_function_type_list (V4SF_type_node,
16192                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16193   tree v8qi_ftype_v8qi_v8qi
16194     = build_function_type_list (V8QI_type_node,
16195                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16196   tree v4hi_ftype_v4hi_v4hi
16197     = build_function_type_list (V4HI_type_node,
16198                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16199   tree v2si_ftype_v2si_v2si
16200     = build_function_type_list (V2SI_type_node,
16201                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16202   tree di_ftype_di_di
16203     = build_function_type_list (long_long_unsigned_type_node,
16204                                 long_long_unsigned_type_node,
16205                                 long_long_unsigned_type_node, NULL_TREE);
16206
16207   tree di_ftype_di_di_int
16208     = build_function_type_list (long_long_unsigned_type_node,
16209                                 long_long_unsigned_type_node,
16210                                 long_long_unsigned_type_node,
16211                                 integer_type_node, NULL_TREE);
16212
16213   tree v2si_ftype_v2sf
16214     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
16215   tree v2sf_ftype_v2si
16216     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
16217   tree v2si_ftype_v2si
16218     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
16219   tree v2sf_ftype_v2sf
16220     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
16221   tree v2sf_ftype_v2sf_v2sf
16222     = build_function_type_list (V2SF_type_node,
16223                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16224   tree v2si_ftype_v2sf_v2sf
16225     = build_function_type_list (V2SI_type_node,
16226                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16227   tree pint_type_node    = build_pointer_type (integer_type_node);
16228   tree pdouble_type_node = build_pointer_type (double_type_node);
16229   tree pcdouble_type_node = build_pointer_type (
16230                                 build_type_variant (double_type_node, 1, 0));
16231   tree int_ftype_v2df_v2df
16232     = build_function_type_list (integer_type_node,
16233                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16234
16235   tree void_ftype_pcvoid
16236     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
16237   tree v4sf_ftype_v4si
16238     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
16239   tree v4si_ftype_v4sf
16240     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
16241   tree v2df_ftype_v4si
16242     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
16243   tree v4si_ftype_v2df
16244     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
16245   tree v2si_ftype_v2df
16246     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
16247   tree v4sf_ftype_v2df
16248     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
16249   tree v2df_ftype_v2si
16250     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
16251   tree v2df_ftype_v4sf
16252     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
16253   tree int_ftype_v2df
16254     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
16255   tree int64_ftype_v2df
16256     = build_function_type_list (long_long_integer_type_node,
16257                                 V2DF_type_node, NULL_TREE);
16258   tree v2df_ftype_v2df_int
16259     = build_function_type_list (V2DF_type_node,
16260                                 V2DF_type_node, integer_type_node, NULL_TREE);
16261   tree v2df_ftype_v2df_int64
16262     = build_function_type_list (V2DF_type_node,
16263                                 V2DF_type_node, long_long_integer_type_node,
16264                                 NULL_TREE);
16265   tree v4sf_ftype_v4sf_v2df
16266     = build_function_type_list (V4SF_type_node,
16267                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
16268   tree v2df_ftype_v2df_v4sf
16269     = build_function_type_list (V2DF_type_node,
16270                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
16271   tree v2df_ftype_v2df_v2df_int
16272     = build_function_type_list (V2DF_type_node,
16273                                 V2DF_type_node, V2DF_type_node,
16274                                 integer_type_node,
16275                                 NULL_TREE);
16276   tree v2df_ftype_v2df_pcdouble
16277     = build_function_type_list (V2DF_type_node,
16278                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
16279   tree void_ftype_pdouble_v2df
16280     = build_function_type_list (void_type_node,
16281                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
16282   tree void_ftype_pint_int
16283     = build_function_type_list (void_type_node,
16284                                 pint_type_node, integer_type_node, NULL_TREE);
16285   tree void_ftype_v16qi_v16qi_pchar
16286     = build_function_type_list (void_type_node,
16287                                 V16QI_type_node, V16QI_type_node,
16288                                 pchar_type_node, NULL_TREE);
16289   tree v2df_ftype_pcdouble
16290     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
16291   tree v2df_ftype_v2df_v2df
16292     = build_function_type_list (V2DF_type_node,
16293                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16294   tree v16qi_ftype_v16qi_v16qi
16295     = build_function_type_list (V16QI_type_node,
16296                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
16297   tree v8hi_ftype_v8hi_v8hi
16298     = build_function_type_list (V8HI_type_node,
16299                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
16300   tree v4si_ftype_v4si_v4si
16301     = build_function_type_list (V4SI_type_node,
16302                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
16303   tree v2di_ftype_v2di_v2di
16304     = build_function_type_list (V2DI_type_node,
16305                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
16306   tree v2di_ftype_v2df_v2df
16307     = build_function_type_list (V2DI_type_node,
16308                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16309   tree v2df_ftype_v2df
16310     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
16311   tree v2di_ftype_v2di_int
16312     = build_function_type_list (V2DI_type_node,
16313                                 V2DI_type_node, integer_type_node, NULL_TREE);
16314   tree v2di_ftype_v2di_v2di_int
16315     = build_function_type_list (V2DI_type_node, V2DI_type_node,
16316                                 V2DI_type_node, integer_type_node, NULL_TREE);
16317   tree v4si_ftype_v4si_int
16318     = build_function_type_list (V4SI_type_node,
16319                                 V4SI_type_node, integer_type_node, NULL_TREE);
16320   tree v8hi_ftype_v8hi_int
16321     = build_function_type_list (V8HI_type_node,
16322                                 V8HI_type_node, integer_type_node, NULL_TREE);
16323   tree v8hi_ftype_v8hi_v2di
16324     = build_function_type_list (V8HI_type_node,
16325                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
16326   tree v4si_ftype_v4si_v2di
16327     = build_function_type_list (V4SI_type_node,
16328                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
16329   tree v4si_ftype_v8hi_v8hi
16330     = build_function_type_list (V4SI_type_node,
16331                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
16332   tree di_ftype_v8qi_v8qi
16333     = build_function_type_list (long_long_unsigned_type_node,
16334                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16335   tree di_ftype_v2si_v2si
16336     = build_function_type_list (long_long_unsigned_type_node,
16337                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16338   tree v2di_ftype_v16qi_v16qi
16339     = build_function_type_list (V2DI_type_node,
16340                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
16341   tree v2di_ftype_v4si_v4si
16342     = build_function_type_list (V2DI_type_node,
16343                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
16344   tree int_ftype_v16qi
16345     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
16346   tree v16qi_ftype_pcchar
16347     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
16348   tree void_ftype_pchar_v16qi
16349     = build_function_type_list (void_type_node,
16350                                 pchar_type_node, V16QI_type_node, NULL_TREE);
16351
16352   tree float80_type;
16353   tree float128_type;
16354   tree ftype;
16355
16356   /* The __float80 type.  */
16357   if (TYPE_MODE (long_double_type_node) == XFmode)
16358     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
16359                                                "__float80");
16360   else
16361     {
16362       /* The __float80 type.  */
16363       float80_type = make_node (REAL_TYPE);
16364       TYPE_PRECISION (float80_type) = 80;
16365       layout_type (float80_type);
16366       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
16367     }
16368
16369   if (TARGET_64BIT)
16370     {
16371       float128_type = make_node (REAL_TYPE);
16372       TYPE_PRECISION (float128_type) = 128;
16373       layout_type (float128_type);
16374       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
16375     }
16376
16377   /* Add all builtins that are more or less simple operations on two
16378      operands.  */
16379   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16380     {
16381       /* Use one of the operands; the target can have a different mode for
16382          mask-generating compares.  */
16383       enum machine_mode mode;
16384       tree type;
16385
16386       if (d->name == 0)
16387         continue;
16388       mode = insn_data[d->icode].operand[1].mode;
16389
16390       switch (mode)
16391         {
16392         case V16QImode:
16393           type = v16qi_ftype_v16qi_v16qi;
16394           break;
16395         case V8HImode:
16396           type = v8hi_ftype_v8hi_v8hi;
16397           break;
16398         case V4SImode:
16399           type = v4si_ftype_v4si_v4si;
16400           break;
16401         case V2DImode:
16402           type = v2di_ftype_v2di_v2di;
16403           break;
16404         case V2DFmode:
16405           type = v2df_ftype_v2df_v2df;
16406           break;
16407         case V4SFmode:
16408           type = v4sf_ftype_v4sf_v4sf;
16409           break;
16410         case V8QImode:
16411           type = v8qi_ftype_v8qi_v8qi;
16412           break;
16413         case V4HImode:
16414           type = v4hi_ftype_v4hi_v4hi;
16415           break;
16416         case V2SImode:
16417           type = v2si_ftype_v2si_v2si;
16418           break;
16419         case DImode:
16420           type = di_ftype_di_di;
16421           break;
16422
16423         default:
16424           gcc_unreachable ();
16425         }
16426
16427       /* Override for comparisons.  */
16428       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
16429           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
16430         type = v4si_ftype_v4sf_v4sf;
16431
16432       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
16433           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
16434         type = v2di_ftype_v2df_v2df;
16435
16436       def_builtin (d->mask, d->name, type, d->code);
16437     }
16438
16439   /* Add all builtins that are more or less simple operations on 1 operand.  */
16440   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16441     {
16442       enum machine_mode mode;
16443       tree type;
16444
16445       if (d->name == 0)
16446         continue;
16447       mode = insn_data[d->icode].operand[1].mode;
16448
16449       switch (mode)
16450         {
16451         case V16QImode:
16452           type = v16qi_ftype_v16qi;
16453           break;
16454         case V8HImode:
16455           type = v8hi_ftype_v8hi;
16456           break;
16457         case V4SImode:
16458           type = v4si_ftype_v4si;
16459           break;
16460         case V2DFmode:
16461           type = v2df_ftype_v2df;
16462           break;
16463         case V4SFmode:
16464           type = v4sf_ftype_v4sf;
16465           break;
16466         case V8QImode:
16467           type = v8qi_ftype_v8qi;
16468           break;
16469         case V4HImode:
16470           type = v4hi_ftype_v4hi;
16471           break;
16472         case V2SImode:
16473           type = v2si_ftype_v2si;
16474           break;
16475
16476         default:
16477           abort ();
16478         }
16479
16480       def_builtin (d->mask, d->name, type, d->code);
16481     }
16482
16483   /* Add the remaining MMX insns with somewhat more complicated types.  */
16484   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
16485   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
16486   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
16487   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
16488
16489   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
16490   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
16491   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
16492
16493   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
16494   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
16495
16496   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
16497   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
16498
16499   /* comi/ucomi insns.  */
16500   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
16501     if (d->mask == MASK_SSE2)
16502       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
16503     else
16504       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
16505
16506   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
16507   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
16508   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
16509
16510   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
16511   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
16512   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
16513   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
16514   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
16515   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
16516   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
16517   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
16518   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
16519   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
16520   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
16521
16522   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
16523
16524   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
16525   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
16526
16527   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
16528   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
16529   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
16530   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
16531
16532   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
16533   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
16534   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
16535   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
16536
16537   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
16538
16539   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
16540
16541   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
16542   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
16543   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
16544   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
16545   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
16546   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
16547
16548   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
16549
16550   /* Original 3DNow!  */
16551   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
16552   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
16553   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
16554   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
16555   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
16556   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
16557   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
16558   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
16559   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
16560   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
16561   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
16562   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
16563   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
16564   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
16565   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
16566   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
16567   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
16568   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
16569   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
16570   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
16571
16572   /* 3DNow! extension as used in the Athlon CPU.  */
16573   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
16574   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
16575   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
16576   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
16577   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
16578   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
16579
16580   /* SSE2 */
16581   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
16582
16583   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
16584   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
16585
16586   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
16587   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
16588
16589   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
16590   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
16591   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
16592   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
16593   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
16594
16595   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
16596   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
16597   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
16598   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
16599
16600   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
16601   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
16602
16603   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
16604
16605   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
16606   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
16607
16608   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
16609   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
16610   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
16611   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
16612   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
16613
16614   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
16615
16616   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
16617   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
16618   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
16619   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
16620
16621   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
16622   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
16623   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
16624
16625   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
16626   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
16627   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
16628   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
16629
16630   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
16631   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
16632   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
16633
16634   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
16635   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
16636
16637   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
16638   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
16639
16640   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
16641   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
16642   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
16643
16644   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
16645   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
16646   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
16647
16648   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
16649   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
16650
16651   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
16652   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
16653   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
16654   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
16655
16656   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
16657   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
16658   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
16659   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
16660
16661   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
16662   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
16663
16664   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
16665
16666   /* Prescott New Instructions.  */
16667   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
16668                void_ftype_pcvoid_unsigned_unsigned,
16669                IX86_BUILTIN_MONITOR);
16670   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
16671                void_ftype_unsigned_unsigned,
16672                IX86_BUILTIN_MWAIT);
16673   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
16674                v4sf_ftype_v4sf,
16675                IX86_BUILTIN_MOVSHDUP);
16676   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
16677                v4sf_ftype_v4sf,
16678                IX86_BUILTIN_MOVSLDUP);
16679   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
16680                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
16681
16682   /* SSSE3.  */
16683   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr128",
16684                v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
16685   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int,
16686                IX86_BUILTIN_PALIGNR);
16687
16688   /* Access to the vec_init patterns.  */
16689   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
16690                                     integer_type_node, NULL_TREE);
16691   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
16692                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
16693
16694   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
16695                                     short_integer_type_node,
16696                                     short_integer_type_node,
16697                                     short_integer_type_node, NULL_TREE);
16698   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
16699                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
16700
16701   ftype = build_function_type_list (V8QI_type_node, char_type_node,
16702                                     char_type_node, char_type_node,
16703                                     char_type_node, char_type_node,
16704                                     char_type_node, char_type_node,
16705                                     char_type_node, NULL_TREE);
16706   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
16707                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
16708
16709   /* Access to the vec_extract patterns.  */
16710   ftype = build_function_type_list (double_type_node, V2DF_type_node,
16711                                     integer_type_node, NULL_TREE);
16712   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
16713                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
16714
16715   ftype = build_function_type_list (long_long_integer_type_node,
16716                                     V2DI_type_node, integer_type_node,
16717                                     NULL_TREE);
16718   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
16719                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
16720
16721   ftype = build_function_type_list (float_type_node, V4SF_type_node,
16722                                     integer_type_node, NULL_TREE);
16723   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
16724                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
16725
16726   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
16727                                     integer_type_node, NULL_TREE);
16728   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
16729                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
16730
16731   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
16732                                     integer_type_node, NULL_TREE);
16733   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
16734                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
16735
16736   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
16737                                     integer_type_node, NULL_TREE);
16738   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
16739                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
16740
16741   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
16742                                     integer_type_node, NULL_TREE);
16743   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
16744                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
16745
16746   /* Access to the vec_set patterns.  */
16747   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
16748                                     intHI_type_node,
16749                                     integer_type_node, NULL_TREE);
16750   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
16751                ftype, IX86_BUILTIN_VEC_SET_V8HI);
16752
16753   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
16754                                     intHI_type_node,
16755                                     integer_type_node, NULL_TREE);
16756   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
16757                ftype, IX86_BUILTIN_VEC_SET_V4HI);
16758 }
16759
16760 /* Errors in the source file can cause expand_expr to return const0_rtx
16761    where we expect a vector.  To avoid crashing, use one of the vector
16762    clear instructions.  */
16763 static rtx
16764 safe_vector_operand (rtx x, enum machine_mode mode)
16765 {
16766   if (x == const0_rtx)
16767     x = CONST0_RTX (mode);
16768   return x;
16769 }
16770
16771 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
16772
16773 static rtx
16774 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
16775 {
16776   rtx pat, xops[3];
16777   tree arg0 = TREE_VALUE (arglist);
16778   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16779   rtx op0 = expand_normal (arg0);
16780   rtx op1 = expand_normal (arg1);
16781   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16782   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16783   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
16784
16785   if (VECTOR_MODE_P (mode0))
16786     op0 = safe_vector_operand (op0, mode0);
16787   if (VECTOR_MODE_P (mode1))
16788     op1 = safe_vector_operand (op1, mode1);
16789
16790   if (optimize || !target
16791       || GET_MODE (target) != tmode
16792       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16793     target = gen_reg_rtx (tmode);
16794
16795   if (GET_MODE (op1) == SImode && mode1 == TImode)
16796     {
16797       rtx x = gen_reg_rtx (V4SImode);
16798       emit_insn (gen_sse2_loadd (x, op1));
16799       op1 = gen_lowpart (TImode, x);
16800     }
16801
16802   /* The insn must want input operands in the same modes as the
16803      result.  */
16804   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
16805               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
16806
16807   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
16808     op0 = copy_to_mode_reg (mode0, op0);
16809   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
16810     op1 = copy_to_mode_reg (mode1, op1);
16811
16812   /* ??? Using ix86_fixup_binary_operands is problematic when
16813      we've got mismatched modes.  Fake it.  */
16814
16815   xops[0] = target;
16816   xops[1] = op0;
16817   xops[2] = op1;
16818
16819   if (tmode == mode0 && tmode == mode1)
16820     {
16821       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
16822       op0 = xops[1];
16823       op1 = xops[2];
16824     }
16825   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
16826     {
16827       op0 = force_reg (mode0, op0);
16828       op1 = force_reg (mode1, op1);
16829       target = gen_reg_rtx (tmode);
16830     }
16831
16832   pat = GEN_FCN (icode) (target, op0, op1);
16833   if (! pat)
16834     return 0;
16835   emit_insn (pat);
16836   return target;
16837 }
16838
16839 /* Subroutine of ix86_expand_builtin to take care of stores.  */
16840
16841 static rtx
16842 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
16843 {
16844   rtx pat;
16845   tree arg0 = TREE_VALUE (arglist);
16846   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16847   rtx op0 = expand_normal (arg0);
16848   rtx op1 = expand_normal (arg1);
16849   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
16850   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
16851
16852   if (VECTOR_MODE_P (mode1))
16853     op1 = safe_vector_operand (op1, mode1);
16854
16855   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16856   op1 = copy_to_mode_reg (mode1, op1);
16857
16858   pat = GEN_FCN (icode) (op0, op1);
16859   if (pat)
16860     emit_insn (pat);
16861   return 0;
16862 }
16863
16864 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
16865
16866 static rtx
16867 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
16868                           rtx target, int do_load)
16869 {
16870   rtx pat;
16871   tree arg0 = TREE_VALUE (arglist);
16872   rtx op0 = expand_normal (arg0);
16873   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16874   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16875
16876   if (optimize || !target
16877       || GET_MODE (target) != tmode
16878       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16879     target = gen_reg_rtx (tmode);
16880   if (do_load)
16881     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16882   else
16883     {
16884       if (VECTOR_MODE_P (mode0))
16885         op0 = safe_vector_operand (op0, mode0);
16886
16887       if ((optimize && !register_operand (op0, mode0))
16888           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16889         op0 = copy_to_mode_reg (mode0, op0);
16890     }
16891
16892   pat = GEN_FCN (icode) (target, op0);
16893   if (! pat)
16894     return 0;
16895   emit_insn (pat);
16896   return target;
16897 }
16898
16899 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
16900    sqrtss, rsqrtss, rcpss.  */
16901
16902 static rtx
16903 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
16904 {
16905   rtx pat;
16906   tree arg0 = TREE_VALUE (arglist);
16907   rtx op1, op0 = expand_normal (arg0);
16908   enum machine_mode tmode = insn_data[icode].operand[0].mode;
16909   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
16910
16911   if (optimize || !target
16912       || GET_MODE (target) != tmode
16913       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16914     target = gen_reg_rtx (tmode);
16915
16916   if (VECTOR_MODE_P (mode0))
16917     op0 = safe_vector_operand (op0, mode0);
16918
16919   if ((optimize && !register_operand (op0, mode0))
16920       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16921     op0 = copy_to_mode_reg (mode0, op0);
16922
16923   op1 = op0;
16924   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
16925     op1 = copy_to_mode_reg (mode0, op1);
16926
16927   pat = GEN_FCN (icode) (target, op0, op1);
16928   if (! pat)
16929     return 0;
16930   emit_insn (pat);
16931   return target;
16932 }
16933
16934 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
16935
16936 static rtx
16937 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
16938                          rtx target)
16939 {
16940   rtx pat;
16941   tree arg0 = TREE_VALUE (arglist);
16942   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16943   rtx op0 = expand_normal (arg0);
16944   rtx op1 = expand_normal (arg1);
16945   rtx op2;
16946   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
16947   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
16948   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
16949   enum rtx_code comparison = d->comparison;
16950
16951   if (VECTOR_MODE_P (mode0))
16952     op0 = safe_vector_operand (op0, mode0);
16953   if (VECTOR_MODE_P (mode1))
16954     op1 = safe_vector_operand (op1, mode1);
16955
16956   /* Swap operands if we have a comparison that isn't available in
16957      hardware.  */
16958   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
16959     {
16960       rtx tmp = gen_reg_rtx (mode1);
16961       emit_move_insn (tmp, op1);
16962       op1 = op0;
16963       op0 = tmp;
16964     }
16965
16966   if (optimize || !target
16967       || GET_MODE (target) != tmode
16968       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
16969     target = gen_reg_rtx (tmode);
16970
16971   if ((optimize && !register_operand (op0, mode0))
16972       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
16973     op0 = copy_to_mode_reg (mode0, op0);
16974   if ((optimize && !register_operand (op1, mode1))
16975       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
16976     op1 = copy_to_mode_reg (mode1, op1);
16977
16978   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
16979   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
16980   if (! pat)
16981     return 0;
16982   emit_insn (pat);
16983   return target;
16984 }
16985
16986 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
16987
16988 static rtx
16989 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
16990                       rtx target)
16991 {
16992   rtx pat;
16993   tree arg0 = TREE_VALUE (arglist);
16994   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16995   rtx op0 = expand_normal (arg0);
16996   rtx op1 = expand_normal (arg1);
16997   rtx op2;
16998   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
16999   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
17000   enum rtx_code comparison = d->comparison;
17001
17002   if (VECTOR_MODE_P (mode0))
17003     op0 = safe_vector_operand (op0, mode0);
17004   if (VECTOR_MODE_P (mode1))
17005     op1 = safe_vector_operand (op1, mode1);
17006
17007   /* Swap operands if we have a comparison that isn't available in
17008      hardware.  */
17009   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17010     {
17011       rtx tmp = op1;
17012       op1 = op0;
17013       op0 = tmp;
17014     }
17015
17016   target = gen_reg_rtx (SImode);
17017   emit_move_insn (target, const0_rtx);
17018   target = gen_rtx_SUBREG (QImode, target, 0);
17019
17020   if ((optimize && !register_operand (op0, mode0))
17021       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
17022     op0 = copy_to_mode_reg (mode0, op0);
17023   if ((optimize && !register_operand (op1, mode1))
17024       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
17025     op1 = copy_to_mode_reg (mode1, op1);
17026
17027   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17028   pat = GEN_FCN (d->icode) (op0, op1);
17029   if (! pat)
17030     return 0;
17031   emit_insn (pat);
17032   emit_insn (gen_rtx_SET (VOIDmode,
17033                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
17034                           gen_rtx_fmt_ee (comparison, QImode,
17035                                           SET_DEST (pat),
17036                                           const0_rtx)));
17037
17038   return SUBREG_REG (target);
17039 }
17040
17041 /* Return the integer constant in ARG.  Constrain it to be in the range
17042    of the subparts of VEC_TYPE; issue an error if not.  */
17043
17044 static int
17045 get_element_number (tree vec_type, tree arg)
17046 {
17047   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
17048
17049   if (!host_integerp (arg, 1)
17050       || (elt = tree_low_cst (arg, 1), elt > max))
17051     {
17052       error ("selector must be an integer constant in the range 0..%wi", max);
17053       return 0;
17054     }
17055
17056   return elt;
17057 }
17058
17059 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17060    ix86_expand_vector_init.  We DO have language-level syntax for this, in
17061    the form of  (type){ init-list }.  Except that since we can't place emms
17062    instructions from inside the compiler, we can't allow the use of MMX
17063    registers unless the user explicitly asks for it.  So we do *not* define
17064    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
17065    we have builtins invoked by mmintrin.h that gives us license to emit
17066    these sorts of instructions.  */
17067
17068 static rtx
17069 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
17070 {
17071   enum machine_mode tmode = TYPE_MODE (type);
17072   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
17073   int i, n_elt = GET_MODE_NUNITS (tmode);
17074   rtvec v = rtvec_alloc (n_elt);
17075
17076   gcc_assert (VECTOR_MODE_P (tmode));
17077
17078   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
17079     {
17080       rtx x = expand_normal (TREE_VALUE (arglist));
17081       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
17082     }
17083
17084   gcc_assert (arglist == NULL);
17085
17086   if (!target || !register_operand (target, tmode))
17087     target = gen_reg_rtx (tmode);
17088
17089   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
17090   return target;
17091 }
17092
17093 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17094    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
17095    had a language-level syntax for referencing vector elements.  */
17096
17097 static rtx
17098 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
17099 {
17100   enum machine_mode tmode, mode0;
17101   tree arg0, arg1;
17102   int elt;
17103   rtx op0;
17104
17105   arg0 = TREE_VALUE (arglist);
17106   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17107
17108   op0 = expand_normal (arg0);
17109   elt = get_element_number (TREE_TYPE (arg0), arg1);
17110
17111   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17112   mode0 = TYPE_MODE (TREE_TYPE (arg0));
17113   gcc_assert (VECTOR_MODE_P (mode0));
17114
17115   op0 = force_reg (mode0, op0);
17116
17117   if (optimize || !target || !register_operand (target, tmode))
17118     target = gen_reg_rtx (tmode);
17119
17120   ix86_expand_vector_extract (true, target, op0, elt);
17121
17122   return target;
17123 }
17124
17125 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17126    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
17127    a language-level syntax for referencing vector elements.  */
17128
17129 static rtx
17130 ix86_expand_vec_set_builtin (tree arglist)
17131 {
17132   enum machine_mode tmode, mode1;
17133   tree arg0, arg1, arg2;
17134   int elt;
17135   rtx op0, op1;
17136
17137   arg0 = TREE_VALUE (arglist);
17138   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17139   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
17140
17141   tmode = TYPE_MODE (TREE_TYPE (arg0));
17142   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17143   gcc_assert (VECTOR_MODE_P (tmode));
17144
17145   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
17146   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
17147   elt = get_element_number (TREE_TYPE (arg0), arg2);
17148
17149   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
17150     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
17151
17152   op0 = force_reg (tmode, op0);
17153   op1 = force_reg (mode1, op1);
17154
17155   ix86_expand_vector_set (true, op0, op1, elt);
17156
17157   return op0;
17158 }
17159
17160 /* Expand an expression EXP that calls a built-in function,
17161    with result going to TARGET if that's convenient
17162    (and in mode MODE if that's convenient).
17163    SUBTARGET may be used as the target for computing one of EXP's operands.
17164    IGNORE is nonzero if the value is to be ignored.  */
17165
17166 static rtx
17167 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17168                      enum machine_mode mode ATTRIBUTE_UNUSED,
17169                      int ignore ATTRIBUTE_UNUSED)
17170 {
17171   const struct builtin_description *d;
17172   size_t i;
17173   enum insn_code icode;
17174   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
17175   tree arglist = TREE_OPERAND (exp, 1);
17176   tree arg0, arg1, arg2;
17177   rtx op0, op1, op2, pat;
17178   enum machine_mode tmode, mode0, mode1, mode2, mode3;
17179   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
17180
17181   switch (fcode)
17182     {
17183     case IX86_BUILTIN_EMMS:
17184       emit_insn (gen_mmx_emms ());
17185       return 0;
17186
17187     case IX86_BUILTIN_SFENCE:
17188       emit_insn (gen_sse_sfence ());
17189       return 0;
17190
17191     case IX86_BUILTIN_MASKMOVQ:
17192     case IX86_BUILTIN_MASKMOVDQU:
17193       icode = (fcode == IX86_BUILTIN_MASKMOVQ
17194                ? CODE_FOR_mmx_maskmovq
17195                : CODE_FOR_sse2_maskmovdqu);
17196       /* Note the arg order is different from the operand order.  */
17197       arg1 = TREE_VALUE (arglist);
17198       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
17199       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
17200       op0 = expand_normal (arg0);
17201       op1 = expand_normal (arg1);
17202       op2 = expand_normal (arg2);
17203       mode0 = insn_data[icode].operand[0].mode;
17204       mode1 = insn_data[icode].operand[1].mode;
17205       mode2 = insn_data[icode].operand[2].mode;
17206
17207       op0 = force_reg (Pmode, op0);
17208       op0 = gen_rtx_MEM (mode1, op0);
17209
17210       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
17211         op0 = copy_to_mode_reg (mode0, op0);
17212       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17213         op1 = copy_to_mode_reg (mode1, op1);
17214       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
17215         op2 = copy_to_mode_reg (mode2, op2);
17216       pat = GEN_FCN (icode) (op0, op1, op2);
17217       if (! pat)
17218         return 0;
17219       emit_insn (pat);
17220       return 0;
17221
17222     case IX86_BUILTIN_SQRTSS:
17223       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
17224     case IX86_BUILTIN_RSQRTSS:
17225       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
17226     case IX86_BUILTIN_RCPSS:
17227       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
17228
17229     case IX86_BUILTIN_LOADUPS:
17230       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
17231
17232     case IX86_BUILTIN_STOREUPS:
17233       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
17234
17235     case IX86_BUILTIN_LOADHPS:
17236     case IX86_BUILTIN_LOADLPS:
17237     case IX86_BUILTIN_LOADHPD:
17238     case IX86_BUILTIN_LOADLPD:
17239       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
17240                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
17241                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
17242                : CODE_FOR_sse2_loadlpd);
17243       arg0 = TREE_VALUE (arglist);
17244       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17245       op0 = expand_normal (arg0);
17246       op1 = expand_normal (arg1);
17247       tmode = insn_data[icode].operand[0].mode;
17248       mode0 = insn_data[icode].operand[1].mode;
17249       mode1 = insn_data[icode].operand[2].mode;
17250
17251       op0 = force_reg (mode0, op0);
17252       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
17253       if (optimize || target == 0
17254           || GET_MODE (target) != tmode
17255           || !register_operand (target, tmode))
17256         target = gen_reg_rtx (tmode);
17257       pat = GEN_FCN (icode) (target, op0, op1);
17258       if (! pat)
17259         return 0;
17260       emit_insn (pat);
17261       return target;
17262
17263     case IX86_BUILTIN_STOREHPS:
17264     case IX86_BUILTIN_STORELPS:
17265       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
17266                : CODE_FOR_sse_storelps);
17267       arg0 = TREE_VALUE (arglist);
17268       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17269       op0 = expand_normal (arg0);
17270       op1 = expand_normal (arg1);
17271       mode0 = insn_data[icode].operand[0].mode;
17272       mode1 = insn_data[icode].operand[1].mode;
17273
17274       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17275       op1 = force_reg (mode1, op1);
17276
17277       pat = GEN_FCN (icode) (op0, op1);
17278       if (! pat)
17279         return 0;
17280       emit_insn (pat);
17281       return const0_rtx;
17282
17283     case IX86_BUILTIN_MOVNTPS:
17284       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
17285     case IX86_BUILTIN_MOVNTQ:
17286       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
17287
17288     case IX86_BUILTIN_LDMXCSR:
17289       op0 = expand_normal (TREE_VALUE (arglist));
17290       target = assign_386_stack_local (SImode, SLOT_TEMP);
17291       emit_move_insn (target, op0);
17292       emit_insn (gen_sse_ldmxcsr (target));
17293       return 0;
17294
17295     case IX86_BUILTIN_STMXCSR:
17296       target = assign_386_stack_local (SImode, SLOT_TEMP);
17297       emit_insn (gen_sse_stmxcsr (target));
17298       return copy_to_mode_reg (SImode, target);
17299
17300     case IX86_BUILTIN_SHUFPS:
17301     case IX86_BUILTIN_SHUFPD:
17302       icode = (fcode == IX86_BUILTIN_SHUFPS
17303                ? CODE_FOR_sse_shufps
17304                : CODE_FOR_sse2_shufpd);
17305       arg0 = TREE_VALUE (arglist);
17306       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17307       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
17308       op0 = expand_normal (arg0);
17309       op1 = expand_normal (arg1);
17310       op2 = expand_normal (arg2);
17311       tmode = insn_data[icode].operand[0].mode;
17312       mode0 = insn_data[icode].operand[1].mode;
17313       mode1 = insn_data[icode].operand[2].mode;
17314       mode2 = insn_data[icode].operand[3].mode;
17315
17316       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17317         op0 = copy_to_mode_reg (mode0, op0);
17318       if ((optimize && !register_operand (op1, mode1))
17319           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
17320         op1 = copy_to_mode_reg (mode1, op1);
17321       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
17322         {
17323           /* @@@ better error message */
17324           error ("mask must be an immediate");
17325           return gen_reg_rtx (tmode);
17326         }
17327       if (optimize || target == 0
17328           || GET_MODE (target) != tmode
17329           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17330         target = gen_reg_rtx (tmode);
17331       pat = GEN_FCN (icode) (target, op0, op1, op2);
17332       if (! pat)
17333         return 0;
17334       emit_insn (pat);
17335       return target;
17336
17337     case IX86_BUILTIN_PSHUFW:
17338     case IX86_BUILTIN_PSHUFD:
17339     case IX86_BUILTIN_PSHUFHW:
17340     case IX86_BUILTIN_PSHUFLW:
17341       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
17342                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
17343                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
17344                : CODE_FOR_mmx_pshufw);
17345       arg0 = TREE_VALUE (arglist);
17346       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17347       op0 = expand_normal (arg0);
17348       op1 = expand_normal (arg1);
17349       tmode = insn_data[icode].operand[0].mode;
17350       mode1 = insn_data[icode].operand[1].mode;
17351       mode2 = insn_data[icode].operand[2].mode;
17352
17353       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17354         op0 = copy_to_mode_reg (mode1, op0);
17355       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17356         {
17357           /* @@@ better error message */
17358           error ("mask must be an immediate");
17359           return const0_rtx;
17360         }
17361       if (target == 0
17362           || GET_MODE (target) != tmode
17363           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17364         target = gen_reg_rtx (tmode);
17365       pat = GEN_FCN (icode) (target, op0, op1);
17366       if (! pat)
17367         return 0;
17368       emit_insn (pat);
17369       return target;
17370
17371     case IX86_BUILTIN_PSLLDQI128:
17372     case IX86_BUILTIN_PSRLDQI128:
17373       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
17374                : CODE_FOR_sse2_lshrti3);
17375       arg0 = TREE_VALUE (arglist);
17376       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17377       op0 = expand_normal (arg0);
17378       op1 = expand_normal (arg1);
17379       tmode = insn_data[icode].operand[0].mode;
17380       mode1 = insn_data[icode].operand[1].mode;
17381       mode2 = insn_data[icode].operand[2].mode;
17382
17383       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17384         {
17385           op0 = copy_to_reg (op0);
17386           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
17387         }
17388       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17389         {
17390           error ("shift must be an immediate");
17391           return const0_rtx;
17392         }
17393       target = gen_reg_rtx (V2DImode);
17394       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
17395       if (! pat)
17396         return 0;
17397       emit_insn (pat);
17398       return target;
17399
17400     case IX86_BUILTIN_FEMMS:
17401       emit_insn (gen_mmx_femms ());
17402       return NULL_RTX;
17403
17404     case IX86_BUILTIN_PAVGUSB:
17405       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
17406
17407     case IX86_BUILTIN_PF2ID:
17408       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
17409
17410     case IX86_BUILTIN_PFACC:
17411       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
17412
17413     case IX86_BUILTIN_PFADD:
17414      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
17415
17416     case IX86_BUILTIN_PFCMPEQ:
17417       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
17418
17419     case IX86_BUILTIN_PFCMPGE:
17420       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
17421
17422     case IX86_BUILTIN_PFCMPGT:
17423       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
17424
17425     case IX86_BUILTIN_PFMAX:
17426       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
17427
17428     case IX86_BUILTIN_PFMIN:
17429       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
17430
17431     case IX86_BUILTIN_PFMUL:
17432       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
17433
17434     case IX86_BUILTIN_PFRCP:
17435       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
17436
17437     case IX86_BUILTIN_PFRCPIT1:
17438       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
17439
17440     case IX86_BUILTIN_PFRCPIT2:
17441       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
17442
17443     case IX86_BUILTIN_PFRSQIT1:
17444       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
17445
17446     case IX86_BUILTIN_PFRSQRT:
17447       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
17448
17449     case IX86_BUILTIN_PFSUB:
17450       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
17451
17452     case IX86_BUILTIN_PFSUBR:
17453       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
17454
17455     case IX86_BUILTIN_PI2FD:
17456       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
17457
17458     case IX86_BUILTIN_PMULHRW:
17459       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
17460
17461     case IX86_BUILTIN_PF2IW:
17462       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
17463
17464     case IX86_BUILTIN_PFNACC:
17465       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
17466
17467     case IX86_BUILTIN_PFPNACC:
17468       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
17469
17470     case IX86_BUILTIN_PI2FW:
17471       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
17472
17473     case IX86_BUILTIN_PSWAPDSI:
17474       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
17475
17476     case IX86_BUILTIN_PSWAPDSF:
17477       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
17478
17479     case IX86_BUILTIN_SQRTSD:
17480       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
17481     case IX86_BUILTIN_LOADUPD:
17482       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
17483     case IX86_BUILTIN_STOREUPD:
17484       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
17485
17486     case IX86_BUILTIN_MFENCE:
17487         emit_insn (gen_sse2_mfence ());
17488         return 0;
17489     case IX86_BUILTIN_LFENCE:
17490         emit_insn (gen_sse2_lfence ());
17491         return 0;
17492
17493     case IX86_BUILTIN_CLFLUSH:
17494         arg0 = TREE_VALUE (arglist);
17495         op0 = expand_normal (arg0);
17496         icode = CODE_FOR_sse2_clflush;
17497         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
17498             op0 = copy_to_mode_reg (Pmode, op0);
17499
17500         emit_insn (gen_sse2_clflush (op0));
17501         return 0;
17502
17503     case IX86_BUILTIN_MOVNTPD:
17504       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
17505     case IX86_BUILTIN_MOVNTDQ:
17506       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
17507     case IX86_BUILTIN_MOVNTI:
17508       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
17509
17510     case IX86_BUILTIN_LOADDQU:
17511       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
17512     case IX86_BUILTIN_STOREDQU:
17513       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
17514
17515     case IX86_BUILTIN_MONITOR:
17516       arg0 = TREE_VALUE (arglist);
17517       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17518       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
17519       op0 = expand_normal (arg0);
17520       op1 = expand_normal (arg1);
17521       op2 = expand_normal (arg2);
17522       if (!REG_P (op0))
17523         op0 = copy_to_mode_reg (Pmode, op0);
17524       if (!REG_P (op1))
17525         op1 = copy_to_mode_reg (SImode, op1);
17526       if (!REG_P (op2))
17527         op2 = copy_to_mode_reg (SImode, op2);
17528       if (!TARGET_64BIT)
17529         emit_insn (gen_sse3_monitor (op0, op1, op2));
17530       else
17531         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
17532       return 0;
17533
17534     case IX86_BUILTIN_MWAIT:
17535       arg0 = TREE_VALUE (arglist);
17536       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17537       op0 = expand_normal (arg0);
17538       op1 = expand_normal (arg1);
17539       if (!REG_P (op0))
17540         op0 = copy_to_mode_reg (SImode, op0);
17541       if (!REG_P (op1))
17542         op1 = copy_to_mode_reg (SImode, op1);
17543       emit_insn (gen_sse3_mwait (op0, op1));
17544       return 0;
17545
17546     case IX86_BUILTIN_LDDQU:
17547       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
17548                                        target, 1);
17549
17550     case IX86_BUILTIN_PALIGNR:
17551     case IX86_BUILTIN_PALIGNR128:
17552       if (fcode == IX86_BUILTIN_PALIGNR)
17553         {
17554           icode = CODE_FOR_ssse3_palignrdi;
17555           mode = DImode;
17556         }
17557       else
17558         {
17559           icode = CODE_FOR_ssse3_palignrti;
17560           mode = V2DImode;
17561         }
17562       arg0 = TREE_VALUE (arglist);
17563       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
17564       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
17565       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
17566       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
17567       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
17568       tmode = insn_data[icode].operand[0].mode;
17569       mode1 = insn_data[icode].operand[1].mode;
17570       mode2 = insn_data[icode].operand[2].mode;
17571       mode3 = insn_data[icode].operand[3].mode;
17572
17573       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17574         {
17575           op0 = copy_to_reg (op0);
17576           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
17577         }
17578       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17579         {
17580           op1 = copy_to_reg (op1);
17581           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
17582         }
17583       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
17584         {
17585           error ("shift must be an immediate");
17586           return const0_rtx;
17587         }
17588       target = gen_reg_rtx (mode);
17589       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
17590                              op0, op1, op2);
17591       if (! pat)
17592         return 0;
17593       emit_insn (pat);
17594       return target;
17595
17596     case IX86_BUILTIN_VEC_INIT_V2SI:
17597     case IX86_BUILTIN_VEC_INIT_V4HI:
17598     case IX86_BUILTIN_VEC_INIT_V8QI:
17599       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
17600
17601     case IX86_BUILTIN_VEC_EXT_V2DF:
17602     case IX86_BUILTIN_VEC_EXT_V2DI:
17603     case IX86_BUILTIN_VEC_EXT_V4SF:
17604     case IX86_BUILTIN_VEC_EXT_V4SI:
17605     case IX86_BUILTIN_VEC_EXT_V8HI:
17606     case IX86_BUILTIN_VEC_EXT_V2SI:
17607     case IX86_BUILTIN_VEC_EXT_V4HI:
17608       return ix86_expand_vec_ext_builtin (arglist, target);
17609
17610     case IX86_BUILTIN_VEC_SET_V8HI:
17611     case IX86_BUILTIN_VEC_SET_V4HI:
17612       return ix86_expand_vec_set_builtin (arglist);
17613
17614     default:
17615       break;
17616     }
17617
17618   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17619     if (d->code == fcode)
17620       {
17621         /* Compares are treated specially.  */
17622         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
17623             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
17624             || d->icode == CODE_FOR_sse2_maskcmpv2df3
17625             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
17626           return ix86_expand_sse_compare (d, arglist, target);
17627
17628         return ix86_expand_binop_builtin (d->icode, arglist, target);
17629       }
17630
17631   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17632     if (d->code == fcode)
17633       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
17634
17635   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
17636     if (d->code == fcode)
17637       return ix86_expand_sse_comi (d, arglist, target);
17638
17639   gcc_unreachable ();
17640 }
17641
17642 /* Store OPERAND to the memory after reload is completed.  This means
17643    that we can't easily use assign_stack_local.  */
17644 rtx
17645 ix86_force_to_memory (enum machine_mode mode, rtx operand)
17646 {
17647   rtx result;
17648
17649   gcc_assert (reload_completed);
17650   if (TARGET_RED_ZONE)
17651     {
17652       result = gen_rtx_MEM (mode,
17653                             gen_rtx_PLUS (Pmode,
17654                                           stack_pointer_rtx,
17655                                           GEN_INT (-RED_ZONE_SIZE)));
17656       emit_move_insn (result, operand);
17657     }
17658   else if (!TARGET_RED_ZONE && TARGET_64BIT)
17659     {
17660       switch (mode)
17661         {
17662         case HImode:
17663         case SImode:
17664           operand = gen_lowpart (DImode, operand);
17665           /* FALLTHRU */
17666         case DImode:
17667           emit_insn (
17668                       gen_rtx_SET (VOIDmode,
17669                                    gen_rtx_MEM (DImode,
17670                                                 gen_rtx_PRE_DEC (DImode,
17671                                                         stack_pointer_rtx)),
17672                                    operand));
17673           break;
17674         default:
17675           gcc_unreachable ();
17676         }
17677       result = gen_rtx_MEM (mode, stack_pointer_rtx);
17678     }
17679   else
17680     {
17681       switch (mode)
17682         {
17683         case DImode:
17684           {
17685             rtx operands[2];
17686             split_di (&operand, 1, operands, operands + 1);
17687             emit_insn (
17688                         gen_rtx_SET (VOIDmode,
17689                                      gen_rtx_MEM (SImode,
17690                                                   gen_rtx_PRE_DEC (Pmode,
17691                                                         stack_pointer_rtx)),
17692                                      operands[1]));
17693             emit_insn (
17694                         gen_rtx_SET (VOIDmode,
17695                                      gen_rtx_MEM (SImode,
17696                                                   gen_rtx_PRE_DEC (Pmode,
17697                                                         stack_pointer_rtx)),
17698                                      operands[0]));
17699           }
17700           break;
17701         case HImode:
17702           /* Store HImodes as SImodes.  */
17703           operand = gen_lowpart (SImode, operand);
17704           /* FALLTHRU */
17705         case SImode:
17706           emit_insn (
17707                       gen_rtx_SET (VOIDmode,
17708                                    gen_rtx_MEM (GET_MODE (operand),
17709                                                 gen_rtx_PRE_DEC (SImode,
17710                                                         stack_pointer_rtx)),
17711                                    operand));
17712           break;
17713         default:
17714           gcc_unreachable ();
17715         }
17716       result = gen_rtx_MEM (mode, stack_pointer_rtx);
17717     }
17718   return result;
17719 }
17720
17721 /* Free operand from the memory.  */
17722 void
17723 ix86_free_from_memory (enum machine_mode mode)
17724 {
17725   if (!TARGET_RED_ZONE)
17726     {
17727       int size;
17728
17729       if (mode == DImode || TARGET_64BIT)
17730         size = 8;
17731       else
17732         size = 4;
17733       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
17734          to pop or add instruction if registers are available.  */
17735       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
17736                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
17737                                             GEN_INT (size))));
17738     }
17739 }
17740
17741 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
17742    QImode must go into class Q_REGS.
17743    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
17744    movdf to do mem-to-mem moves through integer regs.  */
17745 enum reg_class
17746 ix86_preferred_reload_class (rtx x, enum reg_class class)
17747 {
17748   enum machine_mode mode = GET_MODE (x);
17749
17750   /* We're only allowed to return a subclass of CLASS.  Many of the
17751      following checks fail for NO_REGS, so eliminate that early.  */
17752   if (class == NO_REGS)
17753     return NO_REGS;
17754
17755   /* All classes can load zeros.  */
17756   if (x == CONST0_RTX (mode))
17757     return class;
17758
17759   /* Force constants into memory if we are loading a (nonzero) constant into
17760      an MMX or SSE register.  This is because there are no MMX/SSE instructions
17761      to load from a constant.  */
17762   if (CONSTANT_P (x)
17763       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
17764     return NO_REGS;
17765
17766   /* Prefer SSE regs only, if we can use them for math.  */
17767   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
17768     return SSE_CLASS_P (class) ? class : NO_REGS;
17769
17770   /* Floating-point constants need more complex checks.  */
17771   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
17772     {
17773       /* General regs can load everything.  */
17774       if (reg_class_subset_p (class, GENERAL_REGS))
17775         return class;
17776
17777       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
17778          zero above.  We only want to wind up preferring 80387 registers if
17779          we plan on doing computation with them.  */
17780       if (TARGET_80387
17781           && standard_80387_constant_p (x))
17782         {
17783           /* Limit class to non-sse.  */
17784           if (class == FLOAT_SSE_REGS)
17785             return FLOAT_REGS;
17786           if (class == FP_TOP_SSE_REGS)
17787             return FP_TOP_REG;
17788           if (class == FP_SECOND_SSE_REGS)
17789             return FP_SECOND_REG;
17790           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
17791             return class;
17792         }
17793
17794       return NO_REGS;
17795     }
17796
17797   /* Generally when we see PLUS here, it's the function invariant
17798      (plus soft-fp const_int).  Which can only be computed into general
17799      regs.  */
17800   if (GET_CODE (x) == PLUS)
17801     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
17802
17803   /* QImode constants are easy to load, but non-constant QImode data
17804      must go into Q_REGS.  */
17805   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
17806     {
17807       if (reg_class_subset_p (class, Q_REGS))
17808         return class;
17809       if (reg_class_subset_p (Q_REGS, class))
17810         return Q_REGS;
17811       return NO_REGS;
17812     }
17813
17814   return class;
17815 }
17816
17817 /* Discourage putting floating-point values in SSE registers unless
17818    SSE math is being used, and likewise for the 387 registers.  */
17819 enum reg_class
17820 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
17821 {
17822   enum machine_mode mode = GET_MODE (x);
17823
17824   /* Restrict the output reload class to the register bank that we are doing
17825      math on.  If we would like not to return a subset of CLASS, reject this
17826      alternative: if reload cannot do this, it will still use its choice.  */
17827   mode = GET_MODE (x);
17828   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
17829     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
17830
17831   if (TARGET_80387 && SCALAR_FLOAT_MODE_P (mode))
17832     {
17833       if (class == FP_TOP_SSE_REGS)
17834         return FP_TOP_REG;
17835       else if (class == FP_SECOND_SSE_REGS)
17836         return FP_SECOND_REG;
17837       else
17838         return FLOAT_CLASS_P (class) ? class : NO_REGS;
17839     }
17840
17841   return class;
17842 }
17843
17844 /* If we are copying between general and FP registers, we need a memory
17845    location. The same is true for SSE and MMX registers.
17846
17847    The macro can't work reliably when one of the CLASSES is class containing
17848    registers from multiple units (SSE, MMX, integer).  We avoid this by never
17849    combining those units in single alternative in the machine description.
17850    Ensure that this constraint holds to avoid unexpected surprises.
17851
17852    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
17853    enforce these sanity checks.  */
17854
17855 int
17856 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
17857                               enum machine_mode mode, int strict)
17858 {
17859   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
17860       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
17861       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
17862       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
17863       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
17864       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
17865     {
17866       gcc_assert (!strict);
17867       return true;
17868     }
17869
17870   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
17871     return true;
17872
17873   /* ??? This is a lie.  We do have moves between mmx/general, and for
17874      mmx/sse2.  But by saying we need secondary memory we discourage the
17875      register allocator from using the mmx registers unless needed.  */
17876   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
17877     return true;
17878
17879   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
17880     {
17881       /* SSE1 doesn't have any direct moves from other classes.  */
17882       if (!TARGET_SSE2)
17883         return true;
17884
17885       /* If the target says that inter-unit moves are more expensive
17886          than moving through memory, then don't generate them.  */
17887       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
17888         return true;
17889
17890       /* Between SSE and general, we have moves no larger than word size.  */
17891       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
17892         return true;
17893
17894       /* ??? For the cost of one register reformat penalty, we could use
17895          the same instructions to move SFmode and DFmode data, but the
17896          relevant move patterns don't support those alternatives.  */
17897       if (mode == SFmode || mode == DFmode)
17898         return true;
17899     }
17900
17901   return false;
17902 }
17903
17904 /* Return true if the registers in CLASS cannot represent the change from
17905    modes FROM to TO.  */
17906
17907 bool
17908 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
17909                                enum reg_class class)
17910 {
17911   if (from == to)
17912     return false;
17913
17914   /* x87 registers can't do subreg at all, as all values are reformatted
17915      to extended precision.  */
17916   if (MAYBE_FLOAT_CLASS_P (class))
17917     return true;
17918
17919   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
17920     {
17921       /* Vector registers do not support QI or HImode loads.  If we don't
17922          disallow a change to these modes, reload will assume it's ok to
17923          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
17924          the vec_dupv4hi pattern.  */
17925       if (GET_MODE_SIZE (from) < 4)
17926         return true;
17927
17928       /* Vector registers do not support subreg with nonzero offsets, which
17929          are otherwise valid for integer registers.  Since we can't see
17930          whether we have a nonzero offset from here, prohibit all
17931          nonparadoxical subregs changing size.  */
17932       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
17933         return true;
17934     }
17935
17936   return false;
17937 }
17938
17939 /* Return the cost of moving data from a register in class CLASS1 to
17940    one in class CLASS2.
17941
17942    It is not required that the cost always equal 2 when FROM is the same as TO;
17943    on some machines it is expensive to move between registers if they are not
17944    general registers.  */
17945
17946 int
17947 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
17948                          enum reg_class class2)
17949 {
17950   /* In case we require secondary memory, compute cost of the store followed
17951      by load.  In order to avoid bad register allocation choices, we need
17952      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
17953
17954   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
17955     {
17956       int cost = 1;
17957
17958       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
17959                    MEMORY_MOVE_COST (mode, class1, 1));
17960       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
17961                    MEMORY_MOVE_COST (mode, class2, 1));
17962
17963       /* In case of copying from general_purpose_register we may emit multiple
17964          stores followed by single load causing memory size mismatch stall.
17965          Count this as arbitrarily high cost of 20.  */
17966       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
17967         cost += 20;
17968
17969       /* In the case of FP/MMX moves, the registers actually overlap, and we
17970          have to switch modes in order to treat them differently.  */
17971       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
17972           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
17973         cost += 20;
17974
17975       return cost;
17976     }
17977
17978   /* Moves between SSE/MMX and integer unit are expensive.  */
17979   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
17980       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
17981     return ix86_cost->mmxsse_to_integer;
17982   if (MAYBE_FLOAT_CLASS_P (class1))
17983     return ix86_cost->fp_move;
17984   if (MAYBE_SSE_CLASS_P (class1))
17985     return ix86_cost->sse_move;
17986   if (MAYBE_MMX_CLASS_P (class1))
17987     return ix86_cost->mmx_move;
17988   return 2;
17989 }
17990
17991 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
17992
17993 bool
17994 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
17995 {
17996   /* Flags and only flags can only hold CCmode values.  */
17997   if (CC_REGNO_P (regno))
17998     return GET_MODE_CLASS (mode) == MODE_CC;
17999   if (GET_MODE_CLASS (mode) == MODE_CC
18000       || GET_MODE_CLASS (mode) == MODE_RANDOM
18001       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
18002     return 0;
18003   if (FP_REGNO_P (regno))
18004     return VALID_FP_MODE_P (mode);
18005   if (SSE_REGNO_P (regno))
18006     {
18007       /* We implement the move patterns for all vector modes into and
18008          out of SSE registers, even when no operation instructions
18009          are available.  */
18010       return (VALID_SSE_REG_MODE (mode)
18011               || VALID_SSE2_REG_MODE (mode)
18012               || VALID_MMX_REG_MODE (mode)
18013               || VALID_MMX_REG_MODE_3DNOW (mode));
18014     }
18015   if (MMX_REGNO_P (regno))
18016     {
18017       /* We implement the move patterns for 3DNOW modes even in MMX mode,
18018          so if the register is available at all, then we can move data of
18019          the given mode into or out of it.  */
18020       return (VALID_MMX_REG_MODE (mode)
18021               || VALID_MMX_REG_MODE_3DNOW (mode));
18022     }
18023
18024   if (mode == QImode)
18025     {
18026       /* Take care for QImode values - they can be in non-QI regs,
18027          but then they do cause partial register stalls.  */
18028       if (regno < 4 || TARGET_64BIT)
18029         return 1;
18030       if (!TARGET_PARTIAL_REG_STALL)
18031         return 1;
18032       return reload_in_progress || reload_completed;
18033     }
18034   /* We handle both integer and floats in the general purpose registers.  */
18035   else if (VALID_INT_MODE_P (mode))
18036     return 1;
18037   else if (VALID_FP_MODE_P (mode))
18038     return 1;
18039   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
18040      on to use that value in smaller contexts, this can easily force a
18041      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
18042      supporting DImode, allow it.  */
18043   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
18044     return 1;
18045
18046   return 0;
18047 }
18048
18049 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
18050    tieable integer mode.  */
18051
18052 static bool
18053 ix86_tieable_integer_mode_p (enum machine_mode mode)
18054 {
18055   switch (mode)
18056     {
18057     case HImode:
18058     case SImode:
18059       return true;
18060
18061     case QImode:
18062       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
18063
18064     case DImode:
18065       return TARGET_64BIT;
18066
18067     default:
18068       return false;
18069     }
18070 }
18071
18072 /* Return true if MODE1 is accessible in a register that can hold MODE2
18073    without copying.  That is, all register classes that can hold MODE2
18074    can also hold MODE1.  */
18075
18076 bool
18077 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
18078 {
18079   if (mode1 == mode2)
18080     return true;
18081
18082   if (ix86_tieable_integer_mode_p (mode1)
18083       && ix86_tieable_integer_mode_p (mode2))
18084     return true;
18085
18086   /* MODE2 being XFmode implies fp stack or general regs, which means we
18087      can tie any smaller floating point modes to it.  Note that we do not
18088      tie this with TFmode.  */
18089   if (mode2 == XFmode)
18090     return mode1 == SFmode || mode1 == DFmode;
18091
18092   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
18093      that we can tie it with SFmode.  */
18094   if (mode2 == DFmode)
18095     return mode1 == SFmode;
18096
18097   /* If MODE2 is only appropriate for an SSE register, then tie with
18098      any other mode acceptable to SSE registers.  */
18099   if (GET_MODE_SIZE (mode2) >= 8
18100       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
18101     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
18102
18103   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
18104      with any other mode acceptable to MMX registers.  */
18105   if (GET_MODE_SIZE (mode2) == 8
18106       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
18107     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
18108
18109   return false;
18110 }
18111
18112 /* Return the cost of moving data of mode M between a
18113    register and memory.  A value of 2 is the default; this cost is
18114    relative to those in `REGISTER_MOVE_COST'.
18115
18116    If moving between registers and memory is more expensive than
18117    between two registers, you should define this macro to express the
18118    relative cost.
18119
18120    Model also increased moving costs of QImode registers in non
18121    Q_REGS classes.
18122  */
18123 int
18124 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
18125 {
18126   if (FLOAT_CLASS_P (class))
18127     {
18128       int index;
18129       switch (mode)
18130         {
18131           case SFmode:
18132             index = 0;
18133             break;
18134           case DFmode:
18135             index = 1;
18136             break;
18137           case XFmode:
18138             index = 2;
18139             break;
18140           default:
18141             return 100;
18142         }
18143       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
18144     }
18145   if (SSE_CLASS_P (class))
18146     {
18147       int index;
18148       switch (GET_MODE_SIZE (mode))
18149         {
18150           case 4:
18151             index = 0;
18152             break;
18153           case 8:
18154             index = 1;
18155             break;
18156           case 16:
18157             index = 2;
18158             break;
18159           default:
18160             return 100;
18161         }
18162       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
18163     }
18164   if (MMX_CLASS_P (class))
18165     {
18166       int index;
18167       switch (GET_MODE_SIZE (mode))
18168         {
18169           case 4:
18170             index = 0;
18171             break;
18172           case 8:
18173             index = 1;
18174             break;
18175           default:
18176             return 100;
18177         }
18178       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
18179     }
18180   switch (GET_MODE_SIZE (mode))
18181     {
18182       case 1:
18183         if (in)
18184           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
18185                   : ix86_cost->movzbl_load);
18186         else
18187           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
18188                   : ix86_cost->int_store[0] + 4);
18189         break;
18190       case 2:
18191         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
18192       default:
18193         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
18194         if (mode == TFmode)
18195           mode = XFmode;
18196         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
18197                 * (((int) GET_MODE_SIZE (mode)
18198                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
18199     }
18200 }
18201
18202 /* Compute a (partial) cost for rtx X.  Return true if the complete
18203    cost has been computed, and false if subexpressions should be
18204    scanned.  In either case, *TOTAL contains the cost result.  */
18205
18206 static bool
18207 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
18208 {
18209   enum machine_mode mode = GET_MODE (x);
18210
18211   switch (code)
18212     {
18213     case CONST_INT:
18214     case CONST:
18215     case LABEL_REF:
18216     case SYMBOL_REF:
18217       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
18218         *total = 3;
18219       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
18220         *total = 2;
18221       else if (flag_pic && SYMBOLIC_CONST (x)
18222                && (!TARGET_64BIT
18223                    || (!GET_CODE (x) != LABEL_REF
18224                        && (GET_CODE (x) != SYMBOL_REF
18225                            || !SYMBOL_REF_LOCAL_P (x)))))
18226         *total = 1;
18227       else
18228         *total = 0;
18229       return true;
18230
18231     case CONST_DOUBLE:
18232       if (mode == VOIDmode)
18233         *total = 0;
18234       else
18235         switch (standard_80387_constant_p (x))
18236           {
18237           case 1: /* 0.0 */
18238             *total = 1;
18239             break;
18240           default: /* Other constants */
18241             *total = 2;
18242             break;
18243           case 0:
18244           case -1:
18245             /* Start with (MEM (SYMBOL_REF)), since that's where
18246                it'll probably end up.  Add a penalty for size.  */
18247             *total = (COSTS_N_INSNS (1)
18248                       + (flag_pic != 0 && !TARGET_64BIT)
18249                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
18250             break;
18251           }
18252       return true;
18253
18254     case ZERO_EXTEND:
18255       /* The zero extensions is often completely free on x86_64, so make
18256          it as cheap as possible.  */
18257       if (TARGET_64BIT && mode == DImode
18258           && GET_MODE (XEXP (x, 0)) == SImode)
18259         *total = 1;
18260       else if (TARGET_ZERO_EXTEND_WITH_AND)
18261         *total = ix86_cost->add;
18262       else
18263         *total = ix86_cost->movzx;
18264       return false;
18265
18266     case SIGN_EXTEND:
18267       *total = ix86_cost->movsx;
18268       return false;
18269
18270     case ASHIFT:
18271       if (GET_CODE (XEXP (x, 1)) == CONST_INT
18272           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
18273         {
18274           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
18275           if (value == 1)
18276             {
18277               *total = ix86_cost->add;
18278               return false;
18279             }
18280           if ((value == 2 || value == 3)
18281               && ix86_cost->lea <= ix86_cost->shift_const)
18282             {
18283               *total = ix86_cost->lea;
18284               return false;
18285             }
18286         }
18287       /* FALLTHRU */
18288
18289     case ROTATE:
18290     case ASHIFTRT:
18291     case LSHIFTRT:
18292     case ROTATERT:
18293       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
18294         {
18295           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
18296             {
18297               if (INTVAL (XEXP (x, 1)) > 32)
18298                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
18299               else
18300                 *total = ix86_cost->shift_const * 2;
18301             }
18302           else
18303             {
18304               if (GET_CODE (XEXP (x, 1)) == AND)
18305                 *total = ix86_cost->shift_var * 2;
18306               else
18307                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
18308             }
18309         }
18310       else
18311         {
18312           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
18313             *total = ix86_cost->shift_const;
18314           else
18315             *total = ix86_cost->shift_var;
18316         }
18317       return false;
18318
18319     case MULT:
18320       if (FLOAT_MODE_P (mode))
18321         {
18322           *total = ix86_cost->fmul;
18323           return false;
18324         }
18325       else
18326         {
18327           rtx op0 = XEXP (x, 0);
18328           rtx op1 = XEXP (x, 1);
18329           int nbits;
18330           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
18331             {
18332               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
18333               for (nbits = 0; value != 0; value &= value - 1)
18334                 nbits++;
18335             }
18336           else
18337             /* This is arbitrary.  */
18338             nbits = 7;
18339
18340           /* Compute costs correctly for widening multiplication.  */
18341           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
18342               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
18343                  == GET_MODE_SIZE (mode))
18344             {
18345               int is_mulwiden = 0;
18346               enum machine_mode inner_mode = GET_MODE (op0);
18347
18348               if (GET_CODE (op0) == GET_CODE (op1))
18349                 is_mulwiden = 1, op1 = XEXP (op1, 0);
18350               else if (GET_CODE (op1) == CONST_INT)
18351                 {
18352                   if (GET_CODE (op0) == SIGN_EXTEND)
18353                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
18354                                   == INTVAL (op1);
18355                   else
18356                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
18357                 }
18358
18359               if (is_mulwiden)
18360                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
18361             }
18362
18363           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
18364                     + nbits * ix86_cost->mult_bit
18365                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
18366
18367           return true;
18368         }
18369
18370     case DIV:
18371     case UDIV:
18372     case MOD:
18373     case UMOD:
18374       if (FLOAT_MODE_P (mode))
18375         *total = ix86_cost->fdiv;
18376       else
18377         *total = ix86_cost->divide[MODE_INDEX (mode)];
18378       return false;
18379
18380     case PLUS:
18381       if (FLOAT_MODE_P (mode))
18382         *total = ix86_cost->fadd;
18383       else if (GET_MODE_CLASS (mode) == MODE_INT
18384                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
18385         {
18386           if (GET_CODE (XEXP (x, 0)) == PLUS
18387               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
18388               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
18389               && CONSTANT_P (XEXP (x, 1)))
18390             {
18391               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
18392               if (val == 2 || val == 4 || val == 8)
18393                 {
18394                   *total = ix86_cost->lea;
18395                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
18396                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
18397                                       outer_code);
18398                   *total += rtx_cost (XEXP (x, 1), outer_code);
18399                   return true;
18400                 }
18401             }
18402           else if (GET_CODE (XEXP (x, 0)) == MULT
18403                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
18404             {
18405               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
18406               if (val == 2 || val == 4 || val == 8)
18407                 {
18408                   *total = ix86_cost->lea;
18409                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
18410                   *total += rtx_cost (XEXP (x, 1), outer_code);
18411                   return true;
18412                 }
18413             }
18414           else if (GET_CODE (XEXP (x, 0)) == PLUS)
18415             {
18416               *total = ix86_cost->lea;
18417               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
18418               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
18419               *total += rtx_cost (XEXP (x, 1), outer_code);
18420               return true;
18421             }
18422         }
18423       /* FALLTHRU */
18424
18425     case MINUS:
18426       if (FLOAT_MODE_P (mode))
18427         {
18428           *total = ix86_cost->fadd;
18429           return false;
18430         }
18431       /* FALLTHRU */
18432
18433     case AND:
18434     case IOR:
18435     case XOR:
18436       if (!TARGET_64BIT && mode == DImode)
18437         {
18438           *total = (ix86_cost->add * 2
18439                     + (rtx_cost (XEXP (x, 0), outer_code)
18440                        << (GET_MODE (XEXP (x, 0)) != DImode))
18441                     + (rtx_cost (XEXP (x, 1), outer_code)
18442                        << (GET_MODE (XEXP (x, 1)) != DImode)));
18443           return true;
18444         }
18445       /* FALLTHRU */
18446
18447     case NEG:
18448       if (FLOAT_MODE_P (mode))
18449         {
18450           *total = ix86_cost->fchs;
18451           return false;
18452         }
18453       /* FALLTHRU */
18454
18455     case NOT:
18456       if (!TARGET_64BIT && mode == DImode)
18457         *total = ix86_cost->add * 2;
18458       else
18459         *total = ix86_cost->add;
18460       return false;
18461
18462     case COMPARE:
18463       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
18464           && XEXP (XEXP (x, 0), 1) == const1_rtx
18465           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
18466           && XEXP (x, 1) == const0_rtx)
18467         {
18468           /* This kind of construct is implemented using test[bwl].
18469              Treat it as if we had an AND.  */
18470           *total = (ix86_cost->add
18471                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
18472                     + rtx_cost (const1_rtx, outer_code));
18473           return true;
18474         }
18475       return false;
18476
18477     case FLOAT_EXTEND:
18478       if (!TARGET_SSE_MATH
18479           || mode == XFmode
18480           || (mode == DFmode && !TARGET_SSE2))
18481         *total = 0;
18482       return false;
18483
18484     case ABS:
18485       if (FLOAT_MODE_P (mode))
18486         *total = ix86_cost->fabs;
18487       return false;
18488
18489     case SQRT:
18490       if (FLOAT_MODE_P (mode))
18491         *total = ix86_cost->fsqrt;
18492       return false;
18493
18494     case UNSPEC:
18495       if (XINT (x, 1) == UNSPEC_TP)
18496         *total = 0;
18497       return false;
18498
18499     default:
18500       return false;
18501     }
18502 }
18503
18504 #if TARGET_MACHO
18505
18506 static int current_machopic_label_num;
18507
18508 /* Given a symbol name and its associated stub, write out the
18509    definition of the stub.  */
18510
18511 void
18512 machopic_output_stub (FILE *file, const char *symb, const char *stub)
18513 {
18514   unsigned int length;
18515   char *binder_name, *symbol_name, lazy_ptr_name[32];
18516   int label = ++current_machopic_label_num;
18517
18518   /* For 64-bit we shouldn't get here.  */
18519   gcc_assert (!TARGET_64BIT);
18520
18521   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
18522   symb = (*targetm.strip_name_encoding) (symb);
18523
18524   length = strlen (stub);
18525   binder_name = alloca (length + 32);
18526   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
18527
18528   length = strlen (symb);
18529   symbol_name = alloca (length + 32);
18530   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
18531
18532   sprintf (lazy_ptr_name, "L%d$lz", label);
18533
18534   if (MACHOPIC_PURE)
18535     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
18536   else
18537     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
18538
18539   fprintf (file, "%s:\n", stub);
18540   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
18541
18542   if (MACHOPIC_PURE)
18543     {
18544       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
18545       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
18546       fprintf (file, "\tjmp\t*%%edx\n");
18547     }
18548   else
18549     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
18550
18551   fprintf (file, "%s:\n", binder_name);
18552
18553   if (MACHOPIC_PURE)
18554     {
18555       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
18556       fprintf (file, "\tpushl\t%%eax\n");
18557     }
18558   else
18559     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
18560
18561   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
18562
18563   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
18564   fprintf (file, "%s:\n", lazy_ptr_name);
18565   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
18566   fprintf (file, "\t.long %s\n", binder_name);
18567 }
18568
18569 void
18570 darwin_x86_file_end (void)
18571 {
18572   darwin_file_end ();
18573   ix86_file_end ();
18574 }
18575 #endif /* TARGET_MACHO */
18576
18577 /* Order the registers for register allocator.  */
18578
18579 void
18580 x86_order_regs_for_local_alloc (void)
18581 {
18582    int pos = 0;
18583    int i;
18584
18585    /* First allocate the local general purpose registers.  */
18586    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
18587      if (GENERAL_REGNO_P (i) && call_used_regs[i])
18588         reg_alloc_order [pos++] = i;
18589
18590    /* Global general purpose registers.  */
18591    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
18592      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
18593         reg_alloc_order [pos++] = i;
18594
18595    /* x87 registers come first in case we are doing FP math
18596       using them.  */
18597    if (!TARGET_SSE_MATH)
18598      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
18599        reg_alloc_order [pos++] = i;
18600
18601    /* SSE registers.  */
18602    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
18603      reg_alloc_order [pos++] = i;
18604    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
18605      reg_alloc_order [pos++] = i;
18606
18607    /* x87 registers.  */
18608    if (TARGET_SSE_MATH)
18609      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
18610        reg_alloc_order [pos++] = i;
18611
18612    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
18613      reg_alloc_order [pos++] = i;
18614
18615    /* Initialize the rest of array as we do not allocate some registers
18616       at all.  */
18617    while (pos < FIRST_PSEUDO_REGISTER)
18618      reg_alloc_order [pos++] = 0;
18619 }
18620
18621 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
18622    struct attribute_spec.handler.  */
18623 static tree
18624 ix86_handle_struct_attribute (tree *node, tree name,
18625                               tree args ATTRIBUTE_UNUSED,
18626                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
18627 {
18628   tree *type = NULL;
18629   if (DECL_P (*node))
18630     {
18631       if (TREE_CODE (*node) == TYPE_DECL)
18632         type = &TREE_TYPE (*node);
18633     }
18634   else
18635     type = node;
18636
18637   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
18638                  || TREE_CODE (*type) == UNION_TYPE)))
18639     {
18640       warning (OPT_Wattributes, "%qs attribute ignored",
18641                IDENTIFIER_POINTER (name));
18642       *no_add_attrs = true;
18643     }
18644
18645   else if ((is_attribute_p ("ms_struct", name)
18646             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
18647            || ((is_attribute_p ("gcc_struct", name)
18648                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
18649     {
18650       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
18651                IDENTIFIER_POINTER (name));
18652       *no_add_attrs = true;
18653     }
18654
18655   return NULL_TREE;
18656 }
18657
18658 static bool
18659 ix86_ms_bitfield_layout_p (tree record_type)
18660 {
18661   return (TARGET_MS_BITFIELD_LAYOUT &&
18662           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
18663     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
18664 }
18665
18666 /* Returns an expression indicating where the this parameter is
18667    located on entry to the FUNCTION.  */
18668
18669 static rtx
18670 x86_this_parameter (tree function)
18671 {
18672   tree type = TREE_TYPE (function);
18673
18674   if (TARGET_64BIT)
18675     {
18676       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
18677       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
18678     }
18679
18680   if (ix86_function_regparm (type, function) > 0)
18681     {
18682       tree parm;
18683
18684       parm = TYPE_ARG_TYPES (type);
18685       /* Figure out whether or not the function has a variable number of
18686          arguments.  */
18687       for (; parm; parm = TREE_CHAIN (parm))
18688         if (TREE_VALUE (parm) == void_type_node)
18689           break;
18690       /* If not, the this parameter is in the first argument.  */
18691       if (parm)
18692         {
18693           int regno = 0;
18694           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
18695             regno = 2;
18696           return gen_rtx_REG (SImode, regno);
18697         }
18698     }
18699
18700   if (aggregate_value_p (TREE_TYPE (type), type))
18701     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
18702   else
18703     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
18704 }
18705
18706 /* Determine whether x86_output_mi_thunk can succeed.  */
18707
18708 static bool
18709 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
18710                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
18711                          HOST_WIDE_INT vcall_offset, tree function)
18712 {
18713   /* 64-bit can handle anything.  */
18714   if (TARGET_64BIT)
18715     return true;
18716
18717   /* For 32-bit, everything's fine if we have one free register.  */
18718   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
18719     return true;
18720
18721   /* Need a free register for vcall_offset.  */
18722   if (vcall_offset)
18723     return false;
18724
18725   /* Need a free register for GOT references.  */
18726   if (flag_pic && !(*targetm.binds_local_p) (function))
18727     return false;
18728
18729   /* Otherwise ok.  */
18730   return true;
18731 }
18732
18733 /* Output the assembler code for a thunk function.  THUNK_DECL is the
18734    declaration for the thunk function itself, FUNCTION is the decl for
18735    the target function.  DELTA is an immediate constant offset to be
18736    added to THIS.  If VCALL_OFFSET is nonzero, the word at
18737    *(*this + vcall_offset) should be added to THIS.  */
18738
18739 static void
18740 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
18741                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
18742                      HOST_WIDE_INT vcall_offset, tree function)
18743 {
18744   rtx xops[3];
18745   rtx this = x86_this_parameter (function);
18746   rtx this_reg, tmp;
18747
18748   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
18749      pull it in now and let DELTA benefit.  */
18750   if (REG_P (this))
18751     this_reg = this;
18752   else if (vcall_offset)
18753     {
18754       /* Put the this parameter into %eax.  */
18755       xops[0] = this;
18756       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
18757       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
18758     }
18759   else
18760     this_reg = NULL_RTX;
18761
18762   /* Adjust the this parameter by a fixed constant.  */
18763   if (delta)
18764     {
18765       xops[0] = GEN_INT (delta);
18766       xops[1] = this_reg ? this_reg : this;
18767       if (TARGET_64BIT)
18768         {
18769           if (!x86_64_general_operand (xops[0], DImode))
18770             {
18771               tmp = gen_rtx_REG (DImode, R10_REG);
18772               xops[1] = tmp;
18773               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
18774               xops[0] = tmp;
18775               xops[1] = this;
18776             }
18777           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
18778         }
18779       else
18780         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
18781     }
18782
18783   /* Adjust the this parameter by a value stored in the vtable.  */
18784   if (vcall_offset)
18785     {
18786       if (TARGET_64BIT)
18787         tmp = gen_rtx_REG (DImode, R10_REG);
18788       else
18789         {
18790           int tmp_regno = 2 /* ECX */;
18791           if (lookup_attribute ("fastcall",
18792               TYPE_ATTRIBUTES (TREE_TYPE (function))))
18793             tmp_regno = 0 /* EAX */;
18794           tmp = gen_rtx_REG (SImode, tmp_regno);
18795         }
18796
18797       xops[0] = gen_rtx_MEM (Pmode, this_reg);
18798       xops[1] = tmp;
18799       if (TARGET_64BIT)
18800         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
18801       else
18802         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
18803
18804       /* Adjust the this parameter.  */
18805       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
18806       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
18807         {
18808           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
18809           xops[0] = GEN_INT (vcall_offset);
18810           xops[1] = tmp2;
18811           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
18812           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
18813         }
18814       xops[1] = this_reg;
18815       if (TARGET_64BIT)
18816         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
18817       else
18818         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
18819     }
18820
18821   /* If necessary, drop THIS back to its stack slot.  */
18822   if (this_reg && this_reg != this)
18823     {
18824       xops[0] = this_reg;
18825       xops[1] = this;
18826       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
18827     }
18828
18829   xops[0] = XEXP (DECL_RTL (function), 0);
18830   if (TARGET_64BIT)
18831     {
18832       if (!flag_pic || (*targetm.binds_local_p) (function))
18833         output_asm_insn ("jmp\t%P0", xops);
18834       else
18835         {
18836           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
18837           tmp = gen_rtx_CONST (Pmode, tmp);
18838           tmp = gen_rtx_MEM (QImode, tmp);
18839           xops[0] = tmp;
18840           output_asm_insn ("jmp\t%A0", xops);
18841         }
18842     }
18843   else
18844     {
18845       if (!flag_pic || (*targetm.binds_local_p) (function))
18846         output_asm_insn ("jmp\t%P0", xops);
18847       else
18848 #if TARGET_MACHO
18849         if (TARGET_MACHO)
18850           {
18851             rtx sym_ref = XEXP (DECL_RTL (function), 0);
18852             tmp = (gen_rtx_SYMBOL_REF
18853                    (Pmode,
18854                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
18855             tmp = gen_rtx_MEM (QImode, tmp);
18856             xops[0] = tmp;
18857             output_asm_insn ("jmp\t%0", xops);
18858           }
18859         else
18860 #endif /* TARGET_MACHO */
18861         {
18862           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
18863           output_set_got (tmp, NULL_RTX);
18864
18865           xops[1] = tmp;
18866           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
18867           output_asm_insn ("jmp\t{*}%1", xops);
18868         }
18869     }
18870 }
18871
18872 static void
18873 x86_file_start (void)
18874 {
18875   default_file_start ();
18876 #if TARGET_MACHO
18877   darwin_file_start ();
18878 #endif
18879   if (X86_FILE_START_VERSION_DIRECTIVE)
18880     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
18881   if (X86_FILE_START_FLTUSED)
18882     fputs ("\t.global\t__fltused\n", asm_out_file);
18883   if (ix86_asm_dialect == ASM_INTEL)
18884     fputs ("\t.intel_syntax\n", asm_out_file);
18885 }
18886
18887 int
18888 x86_field_alignment (tree field, int computed)
18889 {
18890   enum machine_mode mode;
18891   tree type = TREE_TYPE (field);
18892
18893   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
18894     return computed;
18895   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
18896                     ? get_inner_array_type (type) : type);
18897   if (mode == DFmode || mode == DCmode
18898       || GET_MODE_CLASS (mode) == MODE_INT
18899       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
18900     return MIN (32, computed);
18901   return computed;
18902 }
18903
18904 /* Output assembler code to FILE to increment profiler label # LABELNO
18905    for profiling a function entry.  */
18906 void
18907 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
18908 {
18909   if (TARGET_64BIT)
18910     if (flag_pic)
18911       {
18912 #ifndef NO_PROFILE_COUNTERS
18913         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
18914 #endif
18915         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
18916       }
18917     else
18918       {
18919 #ifndef NO_PROFILE_COUNTERS
18920         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
18921 #endif
18922         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
18923       }
18924   else if (flag_pic)
18925     {
18926 #ifndef NO_PROFILE_COUNTERS
18927       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
18928                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
18929 #endif
18930       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
18931     }
18932   else
18933     {
18934 #ifndef NO_PROFILE_COUNTERS
18935       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
18936                PROFILE_COUNT_REGISTER);
18937 #endif
18938       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
18939     }
18940 }
18941
18942 /* We don't have exact information about the insn sizes, but we may assume
18943    quite safely that we are informed about all 1 byte insns and memory
18944    address sizes.  This is enough to eliminate unnecessary padding in
18945    99% of cases.  */
18946
18947 static int
18948 min_insn_size (rtx insn)
18949 {
18950   int l = 0;
18951
18952   if (!INSN_P (insn) || !active_insn_p (insn))
18953     return 0;
18954
18955   /* Discard alignments we've emit and jump instructions.  */
18956   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
18957       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
18958     return 0;
18959   if (GET_CODE (insn) == JUMP_INSN
18960       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
18961           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
18962     return 0;
18963
18964   /* Important case - calls are always 5 bytes.
18965      It is common to have many calls in the row.  */
18966   if (GET_CODE (insn) == CALL_INSN
18967       && symbolic_reference_mentioned_p (PATTERN (insn))
18968       && !SIBLING_CALL_P (insn))
18969     return 5;
18970   if (get_attr_length (insn) <= 1)
18971     return 1;
18972
18973   /* For normal instructions we may rely on the sizes of addresses
18974      and the presence of symbol to require 4 bytes of encoding.
18975      This is not the case for jumps where references are PC relative.  */
18976   if (GET_CODE (insn) != JUMP_INSN)
18977     {
18978       l = get_attr_length_address (insn);
18979       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
18980         l = 4;
18981     }
18982   if (l)
18983     return 1+l;
18984   else
18985     return 2;
18986 }
18987
18988 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
18989    window.  */
18990
18991 static void
18992 ix86_avoid_jump_misspredicts (void)
18993 {
18994   rtx insn, start = get_insns ();
18995   int nbytes = 0, njumps = 0;
18996   int isjump = 0;
18997
18998   /* Look for all minimal intervals of instructions containing 4 jumps.
18999      The intervals are bounded by START and INSN.  NBYTES is the total
19000      size of instructions in the interval including INSN and not including
19001      START.  When the NBYTES is smaller than 16 bytes, it is possible
19002      that the end of START and INSN ends up in the same 16byte page.
19003
19004      The smallest offset in the page INSN can start is the case where START
19005      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
19006      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
19007      */
19008   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19009     {
19010
19011       nbytes += min_insn_size (insn);
19012       if (dump_file)
19013         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
19014                 INSN_UID (insn), min_insn_size (insn));
19015       if ((GET_CODE (insn) == JUMP_INSN
19016            && GET_CODE (PATTERN (insn)) != ADDR_VEC
19017            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
19018           || GET_CODE (insn) == CALL_INSN)
19019         njumps++;
19020       else
19021         continue;
19022
19023       while (njumps > 3)
19024         {
19025           start = NEXT_INSN (start);
19026           if ((GET_CODE (start) == JUMP_INSN
19027                && GET_CODE (PATTERN (start)) != ADDR_VEC
19028                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
19029               || GET_CODE (start) == CALL_INSN)
19030             njumps--, isjump = 1;
19031           else
19032             isjump = 0;
19033           nbytes -= min_insn_size (start);
19034         }
19035       gcc_assert (njumps >= 0);
19036       if (dump_file)
19037         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
19038                 INSN_UID (start), INSN_UID (insn), nbytes);
19039
19040       if (njumps == 3 && isjump && nbytes < 16)
19041         {
19042           int padsize = 15 - nbytes + min_insn_size (insn);
19043
19044           if (dump_file)
19045             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
19046                      INSN_UID (insn), padsize);
19047           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
19048         }
19049     }
19050 }
19051
19052 /* AMD Athlon works faster
19053    when RET is not destination of conditional jump or directly preceded
19054    by other jump instruction.  We avoid the penalty by inserting NOP just
19055    before the RET instructions in such cases.  */
19056 static void
19057 ix86_pad_returns (void)
19058 {
19059   edge e;
19060   edge_iterator ei;
19061
19062   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
19063     {
19064       basic_block bb = e->src;
19065       rtx ret = BB_END (bb);
19066       rtx prev;
19067       bool replace = false;
19068
19069       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
19070           || !maybe_hot_bb_p (bb))
19071         continue;
19072       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
19073         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
19074           break;
19075       if (prev && GET_CODE (prev) == CODE_LABEL)
19076         {
19077           edge e;
19078           edge_iterator ei;
19079
19080           FOR_EACH_EDGE (e, ei, bb->preds)
19081             if (EDGE_FREQUENCY (e) && e->src->index >= 0
19082                 && !(e->flags & EDGE_FALLTHRU))
19083               replace = true;
19084         }
19085       if (!replace)
19086         {
19087           prev = prev_active_insn (ret);
19088           if (prev
19089               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
19090                   || GET_CODE (prev) == CALL_INSN))
19091             replace = true;
19092           /* Empty functions get branch mispredict even when the jump destination
19093              is not visible to us.  */
19094           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
19095             replace = true;
19096         }
19097       if (replace)
19098         {
19099           emit_insn_before (gen_return_internal_long (), ret);
19100           delete_insn (ret);
19101         }
19102     }
19103 }
19104
19105 /* Implement machine specific optimizations.  We implement padding of returns
19106    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
19107 static void
19108 ix86_reorg (void)
19109 {
19110   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
19111     ix86_pad_returns ();
19112   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
19113     ix86_avoid_jump_misspredicts ();
19114 }
19115
19116 /* Return nonzero when QImode register that must be represented via REX prefix
19117    is used.  */
19118 bool
19119 x86_extended_QIreg_mentioned_p (rtx insn)
19120 {
19121   int i;
19122   extract_insn_cached (insn);
19123   for (i = 0; i < recog_data.n_operands; i++)
19124     if (REG_P (recog_data.operand[i])
19125         && REGNO (recog_data.operand[i]) >= 4)
19126        return true;
19127   return false;
19128 }
19129
19130 /* Return nonzero when P points to register encoded via REX prefix.
19131    Called via for_each_rtx.  */
19132 static int
19133 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
19134 {
19135    unsigned int regno;
19136    if (!REG_P (*p))
19137      return 0;
19138    regno = REGNO (*p);
19139    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
19140 }
19141
19142 /* Return true when INSN mentions register that must be encoded using REX
19143    prefix.  */
19144 bool
19145 x86_extended_reg_mentioned_p (rtx insn)
19146 {
19147   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
19148 }
19149
19150 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
19151    optabs would emit if we didn't have TFmode patterns.  */
19152
19153 void
19154 x86_emit_floatuns (rtx operands[2])
19155 {
19156   rtx neglab, donelab, i0, i1, f0, in, out;
19157   enum machine_mode mode, inmode;
19158
19159   inmode = GET_MODE (operands[1]);
19160   gcc_assert (inmode == SImode || inmode == DImode);
19161
19162   out = operands[0];
19163   in = force_reg (inmode, operands[1]);
19164   mode = GET_MODE (out);
19165   neglab = gen_label_rtx ();
19166   donelab = gen_label_rtx ();
19167   i1 = gen_reg_rtx (Pmode);
19168   f0 = gen_reg_rtx (mode);
19169
19170   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
19171
19172   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
19173   emit_jump_insn (gen_jump (donelab));
19174   emit_barrier ();
19175
19176   emit_label (neglab);
19177
19178   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
19179   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
19180   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
19181   expand_float (f0, i0, 0);
19182   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
19183
19184   emit_label (donelab);
19185 }
19186 \f
19187 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
19188    with all elements equal to VAR.  Return true if successful.  */
19189
19190 static bool
19191 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
19192                                    rtx target, rtx val)
19193 {
19194   enum machine_mode smode, wsmode, wvmode;
19195   rtx x;
19196
19197   switch (mode)
19198     {
19199     case V2SImode:
19200     case V2SFmode:
19201       if (!mmx_ok)
19202         return false;
19203       /* FALLTHRU */
19204
19205     case V2DFmode:
19206     case V2DImode:
19207     case V4SFmode:
19208     case V4SImode:
19209       val = force_reg (GET_MODE_INNER (mode), val);
19210       x = gen_rtx_VEC_DUPLICATE (mode, val);
19211       emit_insn (gen_rtx_SET (VOIDmode, target, x));
19212       return true;
19213
19214     case V4HImode:
19215       if (!mmx_ok)
19216         return false;
19217       if (TARGET_SSE || TARGET_3DNOW_A)
19218         {
19219           val = gen_lowpart (SImode, val);
19220           x = gen_rtx_TRUNCATE (HImode, val);
19221           x = gen_rtx_VEC_DUPLICATE (mode, x);
19222           emit_insn (gen_rtx_SET (VOIDmode, target, x));
19223           return true;
19224         }
19225       else
19226         {
19227           smode = HImode;
19228           wsmode = SImode;
19229           wvmode = V2SImode;
19230           goto widen;
19231         }
19232
19233     case V8QImode:
19234       if (!mmx_ok)
19235         return false;
19236       smode = QImode;
19237       wsmode = HImode;
19238       wvmode = V4HImode;
19239       goto widen;
19240     case V8HImode:
19241       if (TARGET_SSE2)
19242         {
19243           rtx tmp1, tmp2;
19244           /* Extend HImode to SImode using a paradoxical SUBREG.  */
19245           tmp1 = gen_reg_rtx (SImode);
19246           emit_move_insn (tmp1, gen_lowpart (SImode, val));
19247           /* Insert the SImode value as low element of V4SImode vector. */
19248           tmp2 = gen_reg_rtx (V4SImode);
19249           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
19250                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
19251                                     CONST0_RTX (V4SImode),
19252                                     const1_rtx);
19253           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
19254           /* Cast the V4SImode vector back to a V8HImode vector.  */
19255           tmp1 = gen_reg_rtx (V8HImode);
19256           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
19257           /* Duplicate the low short through the whole low SImode word.  */
19258           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
19259           /* Cast the V8HImode vector back to a V4SImode vector.  */
19260           tmp2 = gen_reg_rtx (V4SImode);
19261           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
19262           /* Replicate the low element of the V4SImode vector.  */
19263           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
19264           /* Cast the V2SImode back to V8HImode, and store in target.  */
19265           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
19266           return true;
19267         }
19268       smode = HImode;
19269       wsmode = SImode;
19270       wvmode = V4SImode;
19271       goto widen;
19272     case V16QImode:
19273       if (TARGET_SSE2)
19274         {
19275           rtx tmp1, tmp2;
19276           /* Extend QImode to SImode using a paradoxical SUBREG.  */
19277           tmp1 = gen_reg_rtx (SImode);
19278           emit_move_insn (tmp1, gen_lowpart (SImode, val));
19279           /* Insert the SImode value as low element of V4SImode vector. */
19280           tmp2 = gen_reg_rtx (V4SImode);
19281           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
19282                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
19283                                     CONST0_RTX (V4SImode),
19284                                     const1_rtx);
19285           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
19286           /* Cast the V4SImode vector back to a V16QImode vector.  */
19287           tmp1 = gen_reg_rtx (V16QImode);
19288           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
19289           /* Duplicate the low byte through the whole low SImode word.  */
19290           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
19291           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
19292           /* Cast the V16QImode vector back to a V4SImode vector.  */
19293           tmp2 = gen_reg_rtx (V4SImode);
19294           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
19295           /* Replicate the low element of the V4SImode vector.  */
19296           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
19297           /* Cast the V2SImode back to V16QImode, and store in target.  */
19298           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
19299           return true;
19300         }
19301       smode = QImode;
19302       wsmode = HImode;
19303       wvmode = V8HImode;
19304       goto widen;
19305     widen:
19306       /* Replicate the value once into the next wider mode and recurse.  */
19307       val = convert_modes (wsmode, smode, val, true);
19308       x = expand_simple_binop (wsmode, ASHIFT, val,
19309                                GEN_INT (GET_MODE_BITSIZE (smode)),
19310                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
19311       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
19312
19313       x = gen_reg_rtx (wvmode);
19314       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
19315         gcc_unreachable ();
19316       emit_move_insn (target, gen_lowpart (mode, x));
19317       return true;
19318
19319     default:
19320       return false;
19321     }
19322 }
19323
19324 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
19325    whose ONE_VAR element is VAR, and other elements are zero.  Return true
19326    if successful.  */
19327
19328 static bool
19329 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
19330                                      rtx target, rtx var, int one_var)
19331 {
19332   enum machine_mode vsimode;
19333   rtx new_target;
19334   rtx x, tmp;
19335
19336   switch (mode)
19337     {
19338     case V2SFmode:
19339     case V2SImode:
19340       if (!mmx_ok)
19341         return false;
19342       /* FALLTHRU */
19343
19344     case V2DFmode:
19345     case V2DImode:
19346       if (one_var != 0)
19347         return false;
19348       var = force_reg (GET_MODE_INNER (mode), var);
19349       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
19350       emit_insn (gen_rtx_SET (VOIDmode, target, x));
19351       return true;
19352
19353     case V4SFmode:
19354     case V4SImode:
19355       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
19356         new_target = gen_reg_rtx (mode);
19357       else
19358         new_target = target;
19359       var = force_reg (GET_MODE_INNER (mode), var);
19360       x = gen_rtx_VEC_DUPLICATE (mode, var);
19361       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
19362       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
19363       if (one_var != 0)
19364         {
19365           /* We need to shuffle the value to the correct position, so
19366              create a new pseudo to store the intermediate result.  */
19367
19368           /* With SSE2, we can use the integer shuffle insns.  */
19369           if (mode != V4SFmode && TARGET_SSE2)
19370             {
19371               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
19372                                             GEN_INT (1),
19373                                             GEN_INT (one_var == 1 ? 0 : 1),
19374                                             GEN_INT (one_var == 2 ? 0 : 1),
19375                                             GEN_INT (one_var == 3 ? 0 : 1)));
19376               if (target != new_target)
19377                 emit_move_insn (target, new_target);
19378               return true;
19379             }
19380
19381           /* Otherwise convert the intermediate result to V4SFmode and
19382              use the SSE1 shuffle instructions.  */
19383           if (mode != V4SFmode)
19384             {
19385               tmp = gen_reg_rtx (V4SFmode);
19386               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
19387             }
19388           else
19389             tmp = new_target;
19390
19391           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
19392                                        GEN_INT (1),
19393                                        GEN_INT (one_var == 1 ? 0 : 1),
19394                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
19395                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
19396
19397           if (mode != V4SFmode)
19398             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
19399           else if (tmp != target)
19400             emit_move_insn (target, tmp);
19401         }
19402       else if (target != new_target)
19403         emit_move_insn (target, new_target);
19404       return true;
19405
19406     case V8HImode:
19407     case V16QImode:
19408       vsimode = V4SImode;
19409       goto widen;
19410     case V4HImode:
19411     case V8QImode:
19412       if (!mmx_ok)
19413         return false;
19414       vsimode = V2SImode;
19415       goto widen;
19416     widen:
19417       if (one_var != 0)
19418         return false;
19419
19420       /* Zero extend the variable element to SImode and recurse.  */
19421       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
19422
19423       x = gen_reg_rtx (vsimode);
19424       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
19425                                                 var, one_var))
19426         gcc_unreachable ();
19427
19428       emit_move_insn (target, gen_lowpart (mode, x));
19429       return true;
19430
19431     default:
19432       return false;
19433     }
19434 }
19435
19436 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
19437    consisting of the values in VALS.  It is known that all elements
19438    except ONE_VAR are constants.  Return true if successful.  */
19439
19440 static bool
19441 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
19442                                  rtx target, rtx vals, int one_var)
19443 {
19444   rtx var = XVECEXP (vals, 0, one_var);
19445   enum machine_mode wmode;
19446   rtx const_vec, x;
19447
19448   const_vec = copy_rtx (vals);
19449   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
19450   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
19451
19452   switch (mode)
19453     {
19454     case V2DFmode:
19455     case V2DImode:
19456     case V2SFmode:
19457     case V2SImode:
19458       /* For the two element vectors, it's just as easy to use
19459          the general case.  */
19460       return false;
19461
19462     case V4SFmode:
19463     case V4SImode:
19464     case V8HImode:
19465     case V4HImode:
19466       break;
19467
19468     case V16QImode:
19469       wmode = V8HImode;
19470       goto widen;
19471     case V8QImode:
19472       wmode = V4HImode;
19473       goto widen;
19474     widen:
19475       /* There's no way to set one QImode entry easily.  Combine
19476          the variable value with its adjacent constant value, and
19477          promote to an HImode set.  */
19478       x = XVECEXP (vals, 0, one_var ^ 1);
19479       if (one_var & 1)
19480         {
19481           var = convert_modes (HImode, QImode, var, true);
19482           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
19483                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
19484           x = GEN_INT (INTVAL (x) & 0xff);
19485         }
19486       else
19487         {
19488           var = convert_modes (HImode, QImode, var, true);
19489           x = gen_int_mode (INTVAL (x) << 8, HImode);
19490         }
19491       if (x != const0_rtx)
19492         var = expand_simple_binop (HImode, IOR, var, x, var,
19493                                    1, OPTAB_LIB_WIDEN);
19494
19495       x = gen_reg_rtx (wmode);
19496       emit_move_insn (x, gen_lowpart (wmode, const_vec));
19497       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
19498
19499       emit_move_insn (target, gen_lowpart (mode, x));
19500       return true;
19501
19502     default:
19503       return false;
19504     }
19505
19506   emit_move_insn (target, const_vec);
19507   ix86_expand_vector_set (mmx_ok, target, var, one_var);
19508   return true;
19509 }
19510
19511 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
19512    all values variable, and none identical.  */
19513
19514 static void
19515 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
19516                                  rtx target, rtx vals)
19517 {
19518   enum machine_mode half_mode = GET_MODE_INNER (mode);
19519   rtx op0 = NULL, op1 = NULL;
19520   bool use_vec_concat = false;
19521
19522   switch (mode)
19523     {
19524     case V2SFmode:
19525     case V2SImode:
19526       if (!mmx_ok && !TARGET_SSE)
19527         break;
19528       /* FALLTHRU */
19529
19530     case V2DFmode:
19531     case V2DImode:
19532       /* For the two element vectors, we always implement VEC_CONCAT.  */
19533       op0 = XVECEXP (vals, 0, 0);
19534       op1 = XVECEXP (vals, 0, 1);
19535       use_vec_concat = true;
19536       break;
19537
19538     case V4SFmode:
19539       half_mode = V2SFmode;
19540       goto half;
19541     case V4SImode:
19542       half_mode = V2SImode;
19543       goto half;
19544     half:
19545       {
19546         rtvec v;
19547
19548         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
19549            Recurse to load the two halves.  */
19550
19551         op0 = gen_reg_rtx (half_mode);
19552         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
19553         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
19554
19555         op1 = gen_reg_rtx (half_mode);
19556         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
19557         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
19558
19559         use_vec_concat = true;
19560       }
19561       break;
19562
19563     case V8HImode:
19564     case V16QImode:
19565     case V4HImode:
19566     case V8QImode:
19567       break;
19568
19569     default:
19570       gcc_unreachable ();
19571     }
19572
19573   if (use_vec_concat)
19574     {
19575       if (!register_operand (op0, half_mode))
19576         op0 = force_reg (half_mode, op0);
19577       if (!register_operand (op1, half_mode))
19578         op1 = force_reg (half_mode, op1);
19579
19580       emit_insn (gen_rtx_SET (VOIDmode, target,
19581                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
19582     }
19583   else
19584     {
19585       int i, j, n_elts, n_words, n_elt_per_word;
19586       enum machine_mode inner_mode;
19587       rtx words[4], shift;
19588
19589       inner_mode = GET_MODE_INNER (mode);
19590       n_elts = GET_MODE_NUNITS (mode);
19591       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
19592       n_elt_per_word = n_elts / n_words;
19593       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
19594
19595       for (i = 0; i < n_words; ++i)
19596         {
19597           rtx word = NULL_RTX;
19598
19599           for (j = 0; j < n_elt_per_word; ++j)
19600             {
19601               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
19602               elt = convert_modes (word_mode, inner_mode, elt, true);
19603
19604               if (j == 0)
19605                 word = elt;
19606               else
19607                 {
19608                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
19609                                               word, 1, OPTAB_LIB_WIDEN);
19610                   word = expand_simple_binop (word_mode, IOR, word, elt,
19611                                               word, 1, OPTAB_LIB_WIDEN);
19612                 }
19613             }
19614
19615           words[i] = word;
19616         }
19617
19618       if (n_words == 1)
19619         emit_move_insn (target, gen_lowpart (mode, words[0]));
19620       else if (n_words == 2)
19621         {
19622           rtx tmp = gen_reg_rtx (mode);
19623           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
19624           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
19625           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
19626           emit_move_insn (target, tmp);
19627         }
19628       else if (n_words == 4)
19629         {
19630           rtx tmp = gen_reg_rtx (V4SImode);
19631           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
19632           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
19633           emit_move_insn (target, gen_lowpart (mode, tmp));
19634         }
19635       else
19636         gcc_unreachable ();
19637     }
19638 }
19639
19640 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
19641    instructions unless MMX_OK is true.  */
19642
19643 void
19644 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
19645 {
19646   enum machine_mode mode = GET_MODE (target);
19647   enum machine_mode inner_mode = GET_MODE_INNER (mode);
19648   int n_elts = GET_MODE_NUNITS (mode);
19649   int n_var = 0, one_var = -1;
19650   bool all_same = true, all_const_zero = true;
19651   int i;
19652   rtx x;
19653
19654   for (i = 0; i < n_elts; ++i)
19655     {
19656       x = XVECEXP (vals, 0, i);
19657       if (!CONSTANT_P (x))
19658         n_var++, one_var = i;
19659       else if (x != CONST0_RTX (inner_mode))
19660         all_const_zero = false;
19661       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
19662         all_same = false;
19663     }
19664
19665   /* Constants are best loaded from the constant pool.  */
19666   if (n_var == 0)
19667     {
19668       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
19669       return;
19670     }
19671
19672   /* If all values are identical, broadcast the value.  */
19673   if (all_same
19674       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
19675                                             XVECEXP (vals, 0, 0)))
19676     return;
19677
19678   /* Values where only one field is non-constant are best loaded from
19679      the pool and overwritten via move later.  */
19680   if (n_var == 1)
19681     {
19682       if (all_const_zero
19683           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
19684                                                   XVECEXP (vals, 0, one_var),
19685                                                   one_var))
19686         return;
19687
19688       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
19689         return;
19690     }
19691
19692   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
19693 }
19694
19695 void
19696 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
19697 {
19698   enum machine_mode mode = GET_MODE (target);
19699   enum machine_mode inner_mode = GET_MODE_INNER (mode);
19700   bool use_vec_merge = false;
19701   rtx tmp;
19702
19703   switch (mode)
19704     {
19705     case V2SFmode:
19706     case V2SImode:
19707       if (mmx_ok)
19708         {
19709           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
19710           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
19711           if (elt == 0)
19712             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
19713           else
19714             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
19715           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
19716           return;
19717         }
19718       break;
19719
19720     case V2DFmode:
19721     case V2DImode:
19722       {
19723         rtx op0, op1;
19724
19725         /* For the two element vectors, we implement a VEC_CONCAT with
19726            the extraction of the other element.  */
19727
19728         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
19729         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
19730
19731         if (elt == 0)
19732           op0 = val, op1 = tmp;
19733         else
19734           op0 = tmp, op1 = val;
19735
19736         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
19737         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
19738       }
19739       return;
19740
19741     case V4SFmode:
19742       switch (elt)
19743         {
19744         case 0:
19745           use_vec_merge = true;
19746           break;
19747
19748         case 1:
19749           /* tmp = target = A B C D */
19750           tmp = copy_to_reg (target);
19751           /* target = A A B B */
19752           emit_insn (gen_sse_unpcklps (target, target, target));
19753           /* target = X A B B */
19754           ix86_expand_vector_set (false, target, val, 0);
19755           /* target = A X C D  */
19756           emit_insn (gen_sse_shufps_1 (target, target, tmp,
19757                                        GEN_INT (1), GEN_INT (0),
19758                                        GEN_INT (2+4), GEN_INT (3+4)));
19759           return;
19760
19761         case 2:
19762           /* tmp = target = A B C D */
19763           tmp = copy_to_reg (target);
19764           /* tmp = X B C D */
19765           ix86_expand_vector_set (false, tmp, val, 0);
19766           /* target = A B X D */
19767           emit_insn (gen_sse_shufps_1 (target, target, tmp,
19768                                        GEN_INT (0), GEN_INT (1),
19769                                        GEN_INT (0+4), GEN_INT (3+4)));
19770           return;
19771
19772         case 3:
19773           /* tmp = target = A B C D */
19774           tmp = copy_to_reg (target);
19775           /* tmp = X B C D */
19776           ix86_expand_vector_set (false, tmp, val, 0);
19777           /* target = A B X D */
19778           emit_insn (gen_sse_shufps_1 (target, target, tmp,
19779                                        GEN_INT (0), GEN_INT (1),
19780                                        GEN_INT (2+4), GEN_INT (0+4)));
19781           return;
19782
19783         default:
19784           gcc_unreachable ();
19785         }
19786       break;
19787
19788     case V4SImode:
19789       /* Element 0 handled by vec_merge below.  */
19790       if (elt == 0)
19791         {
19792           use_vec_merge = true;
19793           break;
19794         }
19795
19796       if (TARGET_SSE2)
19797         {
19798           /* With SSE2, use integer shuffles to swap element 0 and ELT,
19799              store into element 0, then shuffle them back.  */
19800
19801           rtx order[4];
19802
19803           order[0] = GEN_INT (elt);
19804           order[1] = const1_rtx;
19805           order[2] = const2_rtx;
19806           order[3] = GEN_INT (3);
19807           order[elt] = const0_rtx;
19808
19809           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
19810                                         order[1], order[2], order[3]));
19811
19812           ix86_expand_vector_set (false, target, val, 0);
19813
19814           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
19815                                         order[1], order[2], order[3]));
19816         }
19817       else
19818         {
19819           /* For SSE1, we have to reuse the V4SF code.  */
19820           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
19821                                   gen_lowpart (SFmode, val), elt);
19822         }
19823       return;
19824
19825     case V8HImode:
19826       use_vec_merge = TARGET_SSE2;
19827       break;
19828     case V4HImode:
19829       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
19830       break;
19831
19832     case V16QImode:
19833     case V8QImode:
19834     default:
19835       break;
19836     }
19837
19838   if (use_vec_merge)
19839     {
19840       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
19841       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
19842       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
19843     }
19844   else
19845     {
19846       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
19847
19848       emit_move_insn (mem, target);
19849
19850       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
19851       emit_move_insn (tmp, val);
19852
19853       emit_move_insn (target, mem);
19854     }
19855 }
19856
19857 void
19858 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
19859 {
19860   enum machine_mode mode = GET_MODE (vec);
19861   enum machine_mode inner_mode = GET_MODE_INNER (mode);
19862   bool use_vec_extr = false;
19863   rtx tmp;
19864
19865   switch (mode)
19866     {
19867     case V2SImode:
19868     case V2SFmode:
19869       if (!mmx_ok)
19870         break;
19871       /* FALLTHRU */
19872
19873     case V2DFmode:
19874     case V2DImode:
19875       use_vec_extr = true;
19876       break;
19877
19878     case V4SFmode:
19879       switch (elt)
19880         {
19881         case 0:
19882           tmp = vec;
19883           break;
19884
19885         case 1:
19886         case 3:
19887           tmp = gen_reg_rtx (mode);
19888           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
19889                                        GEN_INT (elt), GEN_INT (elt),
19890                                        GEN_INT (elt+4), GEN_INT (elt+4)));
19891           break;
19892
19893         case 2:
19894           tmp = gen_reg_rtx (mode);
19895           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
19896           break;
19897
19898         default:
19899           gcc_unreachable ();
19900         }
19901       vec = tmp;
19902       use_vec_extr = true;
19903       elt = 0;
19904       break;
19905
19906     case V4SImode:
19907       if (TARGET_SSE2)
19908         {
19909           switch (elt)
19910             {
19911             case 0:
19912               tmp = vec;
19913               break;
19914
19915             case 1:
19916             case 3:
19917               tmp = gen_reg_rtx (mode);
19918               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
19919                                             GEN_INT (elt), GEN_INT (elt),
19920                                             GEN_INT (elt), GEN_INT (elt)));
19921               break;
19922
19923             case 2:
19924               tmp = gen_reg_rtx (mode);
19925               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
19926               break;
19927
19928             default:
19929               gcc_unreachable ();
19930             }
19931           vec = tmp;
19932           use_vec_extr = true;
19933           elt = 0;
19934         }
19935       else
19936         {
19937           /* For SSE1, we have to reuse the V4SF code.  */
19938           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
19939                                       gen_lowpart (V4SFmode, vec), elt);
19940           return;
19941         }
19942       break;
19943
19944     case V8HImode:
19945       use_vec_extr = TARGET_SSE2;
19946       break;
19947     case V4HImode:
19948       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
19949       break;
19950
19951     case V16QImode:
19952     case V8QImode:
19953       /* ??? Could extract the appropriate HImode element and shift.  */
19954     default:
19955       break;
19956     }
19957
19958   if (use_vec_extr)
19959     {
19960       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
19961       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
19962
19963       /* Let the rtl optimizers know about the zero extension performed.  */
19964       if (inner_mode == HImode)
19965         {
19966           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
19967           target = gen_lowpart (SImode, target);
19968         }
19969
19970       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
19971     }
19972   else
19973     {
19974       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
19975
19976       emit_move_insn (mem, vec);
19977
19978       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
19979       emit_move_insn (target, tmp);
19980     }
19981 }
19982
19983 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
19984    pattern to reduce; DEST is the destination; IN is the input vector.  */
19985
19986 void
19987 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
19988 {
19989   rtx tmp1, tmp2, tmp3;
19990
19991   tmp1 = gen_reg_rtx (V4SFmode);
19992   tmp2 = gen_reg_rtx (V4SFmode);
19993   tmp3 = gen_reg_rtx (V4SFmode);
19994
19995   emit_insn (gen_sse_movhlps (tmp1, in, in));
19996   emit_insn (fn (tmp2, tmp1, in));
19997
19998   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
19999                                GEN_INT (1), GEN_INT (1),
20000                                GEN_INT (1+4), GEN_INT (1+4)));
20001   emit_insn (fn (dest, tmp2, tmp3));
20002 }
20003 \f
20004 /* Target hook for scalar_mode_supported_p.  */
20005 static bool
20006 ix86_scalar_mode_supported_p (enum machine_mode mode)
20007 {
20008   if (DECIMAL_FLOAT_MODE_P (mode))
20009     return true;
20010   else
20011     return default_scalar_mode_supported_p (mode);
20012 }
20013
20014 /* Implements target hook vector_mode_supported_p.  */
20015 static bool
20016 ix86_vector_mode_supported_p (enum machine_mode mode)
20017 {
20018   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
20019     return true;
20020   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
20021     return true;
20022   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
20023     return true;
20024   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
20025     return true;
20026   return false;
20027 }
20028
20029 /* Worker function for TARGET_MD_ASM_CLOBBERS.
20030
20031    We do this in the new i386 backend to maintain source compatibility
20032    with the old cc0-based compiler.  */
20033
20034 static tree
20035 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
20036                       tree inputs ATTRIBUTE_UNUSED,
20037                       tree clobbers)
20038 {
20039   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
20040                         clobbers);
20041   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
20042                         clobbers);
20043   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
20044                         clobbers);
20045   return clobbers;
20046 }
20047
20048 /* Return true if this goes in small data/bss.  */
20049
20050 static bool
20051 ix86_in_large_data_p (tree exp)
20052 {
20053   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
20054     return false;
20055
20056   /* Functions are never large data.  */
20057   if (TREE_CODE (exp) == FUNCTION_DECL)
20058     return false;
20059
20060   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
20061     {
20062       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
20063       if (strcmp (section, ".ldata") == 0
20064           || strcmp (section, ".lbss") == 0)
20065         return true;
20066       return false;
20067     }
20068   else
20069     {
20070       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
20071
20072       /* If this is an incomplete type with size 0, then we can't put it
20073          in data because it might be too big when completed.  */
20074       if (!size || size > ix86_section_threshold)
20075         return true;
20076     }
20077
20078   return false;
20079 }
20080 static void
20081 ix86_encode_section_info (tree decl, rtx rtl, int first)
20082 {
20083   default_encode_section_info (decl, rtl, first);
20084
20085   if (TREE_CODE (decl) == VAR_DECL
20086       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
20087       && ix86_in_large_data_p (decl))
20088     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
20089 }
20090
20091 /* Worker function for REVERSE_CONDITION.  */
20092
20093 enum rtx_code
20094 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
20095 {
20096   return (mode != CCFPmode && mode != CCFPUmode
20097           ? reverse_condition (code)
20098           : reverse_condition_maybe_unordered (code));
20099 }
20100
20101 /* Output code to perform an x87 FP register move, from OPERANDS[1]
20102    to OPERANDS[0].  */
20103
20104 const char *
20105 output_387_reg_move (rtx insn, rtx *operands)
20106 {
20107   if (REG_P (operands[1])
20108       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
20109     {
20110       if (REGNO (operands[0]) == FIRST_STACK_REG)
20111         return output_387_ffreep (operands, 0);
20112       return "fstp\t%y0";
20113     }
20114   if (STACK_TOP_P (operands[0]))
20115     return "fld%z1\t%y1";
20116   return "fst\t%y0";
20117 }
20118
20119 /* Output code to perform a conditional jump to LABEL, if C2 flag in
20120    FP status register is set.  */
20121
20122 void
20123 ix86_emit_fp_unordered_jump (rtx label)
20124 {
20125   rtx reg = gen_reg_rtx (HImode);
20126   rtx temp;
20127
20128   emit_insn (gen_x86_fnstsw_1 (reg));
20129
20130   if (TARGET_USE_SAHF)
20131     {
20132       emit_insn (gen_x86_sahf_1 (reg));
20133
20134       temp = gen_rtx_REG (CCmode, FLAGS_REG);
20135       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
20136     }
20137   else
20138     {
20139       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
20140
20141       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
20142       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
20143     }
20144
20145   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
20146                               gen_rtx_LABEL_REF (VOIDmode, label),
20147                               pc_rtx);
20148   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
20149   emit_jump_insn (temp);
20150 }
20151
20152 /* Output code to perform a log1p XFmode calculation.  */
20153
20154 void ix86_emit_i387_log1p (rtx op0, rtx op1)
20155 {
20156   rtx label1 = gen_label_rtx ();
20157   rtx label2 = gen_label_rtx ();
20158
20159   rtx tmp = gen_reg_rtx (XFmode);
20160   rtx tmp2 = gen_reg_rtx (XFmode);
20161
20162   emit_insn (gen_absxf2 (tmp, op1));
20163   emit_insn (gen_cmpxf (tmp,
20164     CONST_DOUBLE_FROM_REAL_VALUE (
20165        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
20166        XFmode)));
20167   emit_jump_insn (gen_bge (label1));
20168
20169   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
20170   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
20171   emit_jump (label2);
20172
20173   emit_label (label1);
20174   emit_move_insn (tmp, CONST1_RTX (XFmode));
20175   emit_insn (gen_addxf3 (tmp, op1, tmp));
20176   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
20177   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
20178
20179   emit_label (label2);
20180 }
20181
20182 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
20183
20184 static void
20185 i386_solaris_elf_named_section (const char *name, unsigned int flags,
20186                                 tree decl)
20187 {
20188   /* With Binutils 2.15, the "@unwind" marker must be specified on
20189      every occurrence of the ".eh_frame" section, not just the first
20190      one.  */
20191   if (TARGET_64BIT
20192       && strcmp (name, ".eh_frame") == 0)
20193     {
20194       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
20195                flags & SECTION_WRITE ? "aw" : "a");
20196       return;
20197     }
20198   default_elf_asm_named_section (name, flags, decl);
20199 }
20200
20201 /* Return the mangling of TYPE if it is an extended fundamental type.  */
20202
20203 static const char *
20204 ix86_mangle_fundamental_type (tree type)
20205 {
20206   switch (TYPE_MODE (type))
20207     {
20208     case TFmode:
20209       /* __float128 is "g".  */
20210       return "g";
20211     case XFmode:
20212       /* "long double" or __float80 is "e".  */
20213       return "e";
20214     default:
20215       return NULL;
20216     }
20217 }
20218
20219 /* For 32-bit code we can save PIC register setup by using
20220    __stack_chk_fail_local hidden function instead of calling
20221    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
20222    register, so it is better to call __stack_chk_fail directly.  */
20223
20224 static tree
20225 ix86_stack_protect_fail (void)
20226 {
20227   return TARGET_64BIT
20228          ? default_external_stack_protect_fail ()
20229          : default_hidden_stack_protect_fail ();
20230 }
20231
20232 /* Select a format to encode pointers in exception handling data.  CODE
20233    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
20234    true if the symbol may be affected by dynamic relocations.
20235
20236    ??? All x86 object file formats are capable of representing this.
20237    After all, the relocation needed is the same as for the call insn.
20238    Whether or not a particular assembler allows us to enter such, I
20239    guess we'll have to see.  */
20240 int
20241 asm_preferred_eh_data_format (int code, int global)
20242 {
20243   if (flag_pic)
20244     {
20245       int type = DW_EH_PE_sdata8;
20246       if (!TARGET_64BIT
20247           || ix86_cmodel == CM_SMALL_PIC
20248           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
20249         type = DW_EH_PE_sdata4;
20250       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
20251     }
20252   if (ix86_cmodel == CM_SMALL
20253       || (ix86_cmodel == CM_MEDIUM && code))
20254     return DW_EH_PE_udata4;
20255   return DW_EH_PE_absptr;
20256 }
20257 \f
20258 /* Expand copysign from SIGN to the positive value ABS_VALUE
20259    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
20260    the sign-bit.  */
20261 static void
20262 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
20263 {
20264   enum machine_mode mode = GET_MODE (sign);
20265   rtx sgn = gen_reg_rtx (mode);
20266   if (mask == NULL_RTX)
20267     {
20268       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
20269       if (!VECTOR_MODE_P (mode))
20270         {
20271           /* We need to generate a scalar mode mask in this case.  */
20272           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
20273           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
20274           mask = gen_reg_rtx (mode);
20275           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
20276         }
20277     }
20278   else
20279     mask = gen_rtx_NOT (mode, mask);
20280   emit_insn (gen_rtx_SET (VOIDmode, sgn,
20281                           gen_rtx_AND (mode, mask, sign)));
20282   emit_insn (gen_rtx_SET (VOIDmode, result,
20283                           gen_rtx_IOR (mode, abs_value, sgn)));
20284 }
20285
20286 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
20287    mask for masking out the sign-bit is stored in *SMASK, if that is
20288    non-null.  */
20289 static rtx
20290 ix86_expand_sse_fabs (rtx op0, rtx *smask)
20291 {
20292   enum machine_mode mode = GET_MODE (op0);
20293   rtx xa, mask;
20294
20295   xa = gen_reg_rtx (mode);
20296   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
20297   if (!VECTOR_MODE_P (mode))
20298     {
20299       /* We need to generate a scalar mode mask in this case.  */
20300       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
20301       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
20302       mask = gen_reg_rtx (mode);
20303       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
20304     }
20305   emit_insn (gen_rtx_SET (VOIDmode, xa,
20306                           gen_rtx_AND (mode, op0, mask)));
20307
20308   if (smask)
20309     *smask = mask;
20310
20311   return xa;
20312 }
20313
20314 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
20315    swapping the operands if SWAP_OPERANDS is true.  The expanded
20316    code is a forward jump to a newly created label in case the
20317    comparison is true.  The generated label rtx is returned.  */
20318 static rtx
20319 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
20320                                   bool swap_operands)
20321 {
20322   rtx label, tmp;
20323
20324   if (swap_operands)
20325     {
20326       tmp = op0;
20327       op0 = op1;
20328       op1 = tmp;
20329     }
20330
20331   label = gen_label_rtx ();
20332   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
20333   emit_insn (gen_rtx_SET (VOIDmode, tmp,
20334                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
20335   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
20336   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
20337                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
20338   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
20339   JUMP_LABEL (tmp) = label;
20340
20341   return label;
20342 }
20343
20344 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
20345    using comparison code CODE.  Operands are swapped for the comparison if
20346    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
20347 static rtx
20348 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
20349                               bool swap_operands)
20350 {
20351   enum machine_mode mode = GET_MODE (op0);
20352   rtx mask = gen_reg_rtx (mode);
20353
20354   if (swap_operands)
20355     {
20356       rtx tmp = op0;
20357       op0 = op1;
20358       op1 = tmp;
20359     }
20360
20361   if (mode == DFmode)
20362     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
20363                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
20364   else
20365     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
20366                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
20367
20368   return mask;
20369 }
20370
20371 /* Generate and return a rtx of mode MODE for 2**n where n is the number
20372    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
20373 static rtx
20374 ix86_gen_TWO52 (enum machine_mode mode)
20375 {
20376   REAL_VALUE_TYPE TWO52r;
20377   rtx TWO52;
20378
20379   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
20380   TWO52 = const_double_from_real_value (TWO52r, mode);
20381   TWO52 = force_reg (mode, TWO52);
20382
20383   return TWO52;
20384 }
20385
20386 /* Expand SSE sequence for computing lround from OP1 storing
20387    into OP0.  */
20388 void
20389 ix86_expand_lround (rtx op0, rtx op1)
20390 {
20391   /* C code for the stuff we're doing below:
20392        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
20393        return (long)tmp;
20394    */
20395   enum machine_mode mode = GET_MODE (op1);
20396   const struct real_format *fmt;
20397   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
20398   rtx adj;
20399
20400   /* load nextafter (0.5, 0.0) */
20401   fmt = REAL_MODE_FORMAT (mode);
20402   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
20403   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
20404
20405   /* adj = copysign (0.5, op1) */
20406   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
20407   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
20408
20409   /* adj = op1 + adj */
20410   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
20411
20412   /* op0 = (imode)adj */
20413   expand_fix (op0, adj, 0);
20414 }
20415
20416 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
20417    into OPERAND0.  */
20418 void
20419 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
20420 {
20421   /* C code for the stuff we're doing below (for do_floor):
20422         xi = (long)op1;
20423         xi -= (double)xi > op1 ? 1 : 0;
20424         return xi;
20425    */
20426   enum machine_mode fmode = GET_MODE (op1);
20427   enum machine_mode imode = GET_MODE (op0);
20428   rtx ireg, freg, label, tmp;
20429
20430   /* reg = (long)op1 */
20431   ireg = gen_reg_rtx (imode);
20432   expand_fix (ireg, op1, 0);
20433
20434   /* freg = (double)reg */
20435   freg = gen_reg_rtx (fmode);
20436   expand_float (freg, ireg, 0);
20437
20438   /* ireg = (freg > op1) ? ireg - 1 : ireg */
20439   label = ix86_expand_sse_compare_and_jump (UNLE,
20440                                             freg, op1, !do_floor);
20441   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
20442                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
20443   emit_move_insn (ireg, tmp);
20444
20445   emit_label (label);
20446   LABEL_NUSES (label) = 1;
20447
20448   emit_move_insn (op0, ireg);
20449 }
20450
20451 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
20452    result in OPERAND0.  */
20453 void
20454 ix86_expand_rint (rtx operand0, rtx operand1)
20455 {
20456   /* C code for the stuff we're doing below:
20457         xa = fabs (operand1);
20458         if (!isless (xa, 2**52))
20459           return operand1;
20460         xa = xa + 2**52 - 2**52;
20461         return copysign (xa, operand1);
20462    */
20463   enum machine_mode mode = GET_MODE (operand0);
20464   rtx res, xa, label, TWO52, mask;
20465
20466   res = gen_reg_rtx (mode);
20467   emit_move_insn (res, operand1);
20468
20469   /* xa = abs (operand1) */
20470   xa = ix86_expand_sse_fabs (res, &mask);
20471
20472   /* if (!isless (xa, TWO52)) goto label; */
20473   TWO52 = ix86_gen_TWO52 (mode);
20474   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20475
20476   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
20477   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
20478
20479   ix86_sse_copysign_to_positive (res, xa, res, mask);
20480
20481   emit_label (label);
20482   LABEL_NUSES (label) = 1;
20483
20484   emit_move_insn (operand0, res);
20485 }
20486
20487 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
20488    into OPERAND0.  */
20489 void
20490 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
20491 {
20492   /* C code for the stuff we expand below.
20493         double xa = fabs (x), x2;
20494         if (!isless (xa, TWO52))
20495           return x;
20496         xa = xa + TWO52 - TWO52;
20497         x2 = copysign (xa, x);
20498      Compensate.  Floor:
20499         if (x2 > x)
20500           x2 -= 1;
20501      Compensate.  Ceil:
20502         if (x2 < x)
20503           x2 -= -1;
20504         return x2;
20505    */
20506   enum machine_mode mode = GET_MODE (operand0);
20507   rtx xa, TWO52, tmp, label, one, res, mask;
20508
20509   TWO52 = ix86_gen_TWO52 (mode);
20510
20511   /* Temporary for holding the result, initialized to the input
20512      operand to ease control flow.  */
20513   res = gen_reg_rtx (mode);
20514   emit_move_insn (res, operand1);
20515
20516   /* xa = abs (operand1) */
20517   xa = ix86_expand_sse_fabs (res, &mask);
20518
20519   /* if (!isless (xa, TWO52)) goto label; */
20520   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20521
20522   /* xa = xa + TWO52 - TWO52; */
20523   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
20524   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
20525
20526   /* xa = copysign (xa, operand1) */
20527   ix86_sse_copysign_to_positive (xa, xa, res, mask);
20528
20529   /* generate 1.0 or -1.0 */
20530   one = force_reg (mode,
20531                    const_double_from_real_value (do_floor
20532                                                  ? dconst1 : dconstm1, mode));
20533
20534   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
20535   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
20536   emit_insn (gen_rtx_SET (VOIDmode, tmp,
20537                           gen_rtx_AND (mode, one, tmp)));
20538   /* We always need to subtract here to preserve signed zero.  */
20539   tmp = expand_simple_binop (mode, MINUS,
20540                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
20541   emit_move_insn (res, tmp);
20542
20543   emit_label (label);
20544   LABEL_NUSES (label) = 1;
20545
20546   emit_move_insn (operand0, res);
20547 }
20548
20549 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
20550    into OPERAND0.  */
20551 void
20552 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
20553 {
20554   /* C code for the stuff we expand below.
20555         double xa = fabs (x), x2;
20556         if (!isless (xa, TWO52))
20557           return x;
20558         x2 = (double)(long)x;
20559      Compensate.  Floor:
20560         if (x2 > x)
20561           x2 -= 1;
20562      Compensate.  Ceil:
20563         if (x2 < x)
20564           x2 += 1;
20565         if (HONOR_SIGNED_ZEROS (mode))
20566           return copysign (x2, x);
20567         return x2;
20568    */
20569   enum machine_mode mode = GET_MODE (operand0);
20570   rtx xa, xi, TWO52, tmp, label, one, res, mask;
20571
20572   TWO52 = ix86_gen_TWO52 (mode);
20573
20574   /* Temporary for holding the result, initialized to the input
20575      operand to ease control flow.  */
20576   res = gen_reg_rtx (mode);
20577   emit_move_insn (res, operand1);
20578
20579   /* xa = abs (operand1) */
20580   xa = ix86_expand_sse_fabs (res, &mask);
20581
20582   /* if (!isless (xa, TWO52)) goto label; */
20583   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20584
20585   /* xa = (double)(long)x */
20586   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
20587   expand_fix (xi, res, 0);
20588   expand_float (xa, xi, 0);
20589
20590   /* generate 1.0 */
20591   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
20592
20593   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
20594   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
20595   emit_insn (gen_rtx_SET (VOIDmode, tmp,
20596                           gen_rtx_AND (mode, one, tmp)));
20597   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
20598                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
20599   emit_move_insn (res, tmp);
20600
20601   if (HONOR_SIGNED_ZEROS (mode))
20602     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
20603
20604   emit_label (label);
20605   LABEL_NUSES (label) = 1;
20606
20607   emit_move_insn (operand0, res);
20608 }
20609
20610 /* Expand SSE sequence for computing round from OPERAND1 storing
20611    into OPERAND0.  Sequence that works without relying on DImode truncation
20612    via cvttsd2siq that is only available on 64bit targets.  */
20613 void
20614 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
20615 {
20616   /* C code for the stuff we expand below.
20617         double xa = fabs (x), xa2, x2;
20618         if (!isless (xa, TWO52))
20619           return x;
20620      Using the absolute value and copying back sign makes
20621      -0.0 -> -0.0 correct.
20622         xa2 = xa + TWO52 - TWO52;
20623      Compensate.
20624         dxa = xa2 - xa;
20625         if (dxa <= -0.5)
20626           xa2 += 1;
20627         else if (dxa > 0.5)
20628           xa2 -= 1;
20629         x2 = copysign (xa2, x);
20630         return x2;
20631    */
20632   enum machine_mode mode = GET_MODE (operand0);
20633   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
20634
20635   TWO52 = ix86_gen_TWO52 (mode);
20636
20637   /* Temporary for holding the result, initialized to the input
20638      operand to ease control flow.  */
20639   res = gen_reg_rtx (mode);
20640   emit_move_insn (res, operand1);
20641
20642   /* xa = abs (operand1) */
20643   xa = ix86_expand_sse_fabs (res, &mask);
20644
20645   /* if (!isless (xa, TWO52)) goto label; */
20646   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20647
20648   /* xa2 = xa + TWO52 - TWO52; */
20649   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
20650   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
20651
20652   /* dxa = xa2 - xa; */
20653   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
20654
20655   /* generate 0.5, 1.0 and -0.5 */
20656   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
20657   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
20658   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
20659                                0, OPTAB_DIRECT);
20660
20661   /* Compensate.  */
20662   tmp = gen_reg_rtx (mode);
20663   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
20664   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
20665   emit_insn (gen_rtx_SET (VOIDmode, tmp,
20666                           gen_rtx_AND (mode, one, tmp)));
20667   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
20668   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
20669   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
20670   emit_insn (gen_rtx_SET (VOIDmode, tmp,
20671                           gen_rtx_AND (mode, one, tmp)));
20672   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
20673
20674   /* res = copysign (xa2, operand1) */
20675   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
20676
20677   emit_label (label);
20678   LABEL_NUSES (label) = 1;
20679
20680   emit_move_insn (operand0, res);
20681 }
20682
20683 /* Expand SSE sequence for computing trunc from OPERAND1 storing
20684    into OPERAND0.  */
20685 void
20686 ix86_expand_trunc (rtx operand0, rtx operand1)
20687 {
20688   /* C code for SSE variant we expand below.
20689         double xa = fabs (x), x2;
20690         if (!isless (xa, TWO52))
20691           return x;
20692         x2 = (double)(long)x;
20693         if (HONOR_SIGNED_ZEROS (mode))
20694           return copysign (x2, x);
20695         return x2;
20696    */
20697   enum machine_mode mode = GET_MODE (operand0);
20698   rtx xa, xi, TWO52, label, res, mask;
20699
20700   TWO52 = ix86_gen_TWO52 (mode);
20701
20702   /* Temporary for holding the result, initialized to the input
20703      operand to ease control flow.  */
20704   res = gen_reg_rtx (mode);
20705   emit_move_insn (res, operand1);
20706
20707   /* xa = abs (operand1) */
20708   xa = ix86_expand_sse_fabs (res, &mask);
20709
20710   /* if (!isless (xa, TWO52)) goto label; */
20711   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20712
20713   /* x = (double)(long)x */
20714   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
20715   expand_fix (xi, res, 0);
20716   expand_float (res, xi, 0);
20717
20718   if (HONOR_SIGNED_ZEROS (mode))
20719     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
20720
20721   emit_label (label);
20722   LABEL_NUSES (label) = 1;
20723
20724   emit_move_insn (operand0, res);
20725 }
20726
20727 /* Expand SSE sequence for computing trunc from OPERAND1 storing
20728    into OPERAND0.  */
20729 void
20730 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
20731 {
20732   enum machine_mode mode = GET_MODE (operand0);
20733   rtx xa, mask, TWO52, label, one, res, smask, tmp;
20734
20735   /* C code for SSE variant we expand below.
20736         double xa = fabs (x), x2;
20737         if (!isless (xa, TWO52))
20738           return x;
20739         xa2 = xa + TWO52 - TWO52;
20740      Compensate:
20741         if (xa2 > xa)
20742           xa2 -= 1.0;
20743         x2 = copysign (xa2, x);
20744         return x2;
20745    */
20746
20747   TWO52 = ix86_gen_TWO52 (mode);
20748
20749   /* Temporary for holding the result, initialized to the input
20750      operand to ease control flow.  */
20751   res = gen_reg_rtx (mode);
20752   emit_move_insn (res, operand1);
20753
20754   /* xa = abs (operand1) */
20755   xa = ix86_expand_sse_fabs (res, &smask);
20756
20757   /* if (!isless (xa, TWO52)) goto label; */
20758   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20759
20760   /* res = xa + TWO52 - TWO52; */
20761   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
20762   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
20763   emit_move_insn (res, tmp);
20764
20765   /* generate 1.0 */
20766   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
20767
20768   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
20769   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
20770   emit_insn (gen_rtx_SET (VOIDmode, mask,
20771                           gen_rtx_AND (mode, mask, one)));
20772   tmp = expand_simple_binop (mode, MINUS,
20773                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
20774   emit_move_insn (res, tmp);
20775
20776   /* res = copysign (res, operand1) */
20777   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
20778
20779   emit_label (label);
20780   LABEL_NUSES (label) = 1;
20781
20782   emit_move_insn (operand0, res);
20783 }
20784
20785 /* Expand SSE sequence for computing round from OPERAND1 storing
20786    into OPERAND0.  */
20787 void
20788 ix86_expand_round (rtx operand0, rtx operand1)
20789 {
20790   /* C code for the stuff we're doing below:
20791         double xa = fabs (x);
20792         if (!isless (xa, TWO52))
20793           return x;
20794         xa = (double)(long)(xa + nextafter (0.5, 0.0));
20795         return copysign (xa, x);
20796    */
20797   enum machine_mode mode = GET_MODE (operand0);
20798   rtx res, TWO52, xa, label, xi, half, mask;
20799   const struct real_format *fmt;
20800   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
20801
20802   /* Temporary for holding the result, initialized to the input
20803      operand to ease control flow.  */
20804   res = gen_reg_rtx (mode);
20805   emit_move_insn (res, operand1);
20806
20807   TWO52 = ix86_gen_TWO52 (mode);
20808   xa = ix86_expand_sse_fabs (res, &mask);
20809   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
20810
20811   /* load nextafter (0.5, 0.0) */
20812   fmt = REAL_MODE_FORMAT (mode);
20813   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
20814   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
20815
20816   /* xa = xa + 0.5 */
20817   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
20818   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
20819
20820   /* xa = (double)(int64_t)xa */
20821   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
20822   expand_fix (xi, xa, 0);
20823   expand_float (xa, xi, 0);
20824
20825   /* res = copysign (xa, operand1) */
20826   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
20827
20828   emit_label (label);
20829   LABEL_NUSES (label) = 1;
20830
20831   emit_move_insn (operand0, res);
20832 }
20833
20834 #include "gt-i386.h"