OSDN Git Service

PR target/31361
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "output.h"
35 #include "insn-codes.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "except.h"
39 #include "function.h"
40 #include "recog.h"
41 #include "expr.h"
42 #include "optabs.h"
43 #include "toplev.h"
44 #include "basic-block.h"
45 #include "ggc.h"
46 #include "target.h"
47 #include "target-def.h"
48 #include "langhooks.h"
49 #include "cgraph.h"
50 #include "tree-gimple.h"
51 #include "dwarf2.h"
52 #include "tm-constrs.h"
53 #include "params.h"
54
55 #ifndef CHECK_STACK_LIMIT
56 #define CHECK_STACK_LIMIT (-1)
57 #endif
58
59 /* Return index of given mode in mult and division cost tables.  */
60 #define MODE_INDEX(mode)                                        \
61   ((mode) == QImode ? 0                                         \
62    : (mode) == HImode ? 1                                       \
63    : (mode) == SImode ? 2                                       \
64    : (mode) == DImode ? 3                                       \
65    : 4)
66
67 /* Processor costs (relative to an add) */
68 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
69 #define COSTS_N_BYTES(N) ((N) * 2)
70
71 #define DUMMY_STRINGOP_ALGS {libcall, {{-1, libcall}}}
72
73 static const
74 struct processor_costs size_cost = {    /* costs for tuning for size */
75   COSTS_N_BYTES (2),                    /* cost of an add instruction */
76   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
77   COSTS_N_BYTES (2),                    /* variable shift costs */
78   COSTS_N_BYTES (3),                    /* constant shift costs */
79   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
80    COSTS_N_BYTES (3),                   /*                               HI */
81    COSTS_N_BYTES (3),                   /*                               SI */
82    COSTS_N_BYTES (3),                   /*                               DI */
83    COSTS_N_BYTES (5)},                  /*                            other */
84   0,                                    /* cost of multiply per each bit set */
85   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
86    COSTS_N_BYTES (3),                   /*                          HI */
87    COSTS_N_BYTES (3),                   /*                          SI */
88    COSTS_N_BYTES (3),                   /*                          DI */
89    COSTS_N_BYTES (5)},                  /*                       other */
90   COSTS_N_BYTES (3),                    /* cost of movsx */
91   COSTS_N_BYTES (3),                    /* cost of movzx */
92   0,                                    /* "large" insn */
93   2,                                    /* MOVE_RATIO */
94   2,                                    /* cost for loading QImode using movzbl */
95   {2, 2, 2},                            /* cost of loading integer registers
96                                            in QImode, HImode and SImode.
97                                            Relative to reg-reg move (2).  */
98   {2, 2, 2},                            /* cost of storing integer registers */
99   2,                                    /* cost of reg,reg fld/fst */
100   {2, 2, 2},                            /* cost of loading fp registers
101                                            in SFmode, DFmode and XFmode */
102   {2, 2, 2},                            /* cost of storing fp registers
103                                            in SFmode, DFmode and XFmode */
104   3,                                    /* cost of moving MMX register */
105   {3, 3},                               /* cost of loading MMX registers
106                                            in SImode and DImode */
107   {3, 3},                               /* cost of storing MMX registers
108                                            in SImode and DImode */
109   3,                                    /* cost of moving SSE register */
110   {3, 3, 3},                            /* cost of loading SSE registers
111                                            in SImode, DImode and TImode */
112   {3, 3, 3},                            /* cost of storing SSE registers
113                                            in SImode, DImode and TImode */
114   3,                                    /* MMX or SSE register to integer */
115   0,                                    /* size of prefetch block */
116   0,                                    /* number of parallel prefetches */
117   2,                                    /* Branch cost */
118   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
119   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
120   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
121   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
122   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
123   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
124   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
125    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}},
126   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
127    {rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}}}
128 };
129
130 /* Processor costs (relative to an add) */
131 static const
132 struct processor_costs i386_cost = {    /* 386 specific costs */
133   COSTS_N_INSNS (1),                    /* cost of an add instruction */
134   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
135   COSTS_N_INSNS (3),                    /* variable shift costs */
136   COSTS_N_INSNS (2),                    /* constant shift costs */
137   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
138    COSTS_N_INSNS (6),                   /*                               HI */
139    COSTS_N_INSNS (6),                   /*                               SI */
140    COSTS_N_INSNS (6),                   /*                               DI */
141    COSTS_N_INSNS (6)},                  /*                               other */
142   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
143   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
144    COSTS_N_INSNS (23),                  /*                          HI */
145    COSTS_N_INSNS (23),                  /*                          SI */
146    COSTS_N_INSNS (23),                  /*                          DI */
147    COSTS_N_INSNS (23)},                 /*                          other */
148   COSTS_N_INSNS (3),                    /* cost of movsx */
149   COSTS_N_INSNS (2),                    /* cost of movzx */
150   15,                                   /* "large" insn */
151   3,                                    /* MOVE_RATIO */
152   4,                                    /* cost for loading QImode using movzbl */
153   {2, 4, 2},                            /* cost of loading integer registers
154                                            in QImode, HImode and SImode.
155                                            Relative to reg-reg move (2).  */
156   {2, 4, 2},                            /* cost of storing integer registers */
157   2,                                    /* cost of reg,reg fld/fst */
158   {8, 8, 8},                            /* cost of loading fp registers
159                                            in SFmode, DFmode and XFmode */
160   {8, 8, 8},                            /* cost of storing fp registers
161                                            in SFmode, DFmode and XFmode */
162   2,                                    /* cost of moving MMX register */
163   {4, 8},                               /* cost of loading MMX registers
164                                            in SImode and DImode */
165   {4, 8},                               /* cost of storing MMX registers
166                                            in SImode and DImode */
167   2,                                    /* cost of moving SSE register */
168   {4, 8, 16},                           /* cost of loading SSE registers
169                                            in SImode, DImode and TImode */
170   {4, 8, 16},                           /* cost of storing SSE registers
171                                            in SImode, DImode and TImode */
172   3,                                    /* MMX or SSE register to integer */
173   0,                                    /* size of prefetch block */
174   0,                                    /* number of parallel prefetches */
175   1,                                    /* Branch cost */
176   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
177   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
178   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
179   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
180   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
181   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
182   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
183    DUMMY_STRINGOP_ALGS},
184   {{rep_prefix_1_byte, {{-1, rep_prefix_1_byte}}},
185    DUMMY_STRINGOP_ALGS},
186 };
187
188 static const
189 struct processor_costs i486_cost = {    /* 486 specific costs */
190   COSTS_N_INSNS (1),                    /* cost of an add instruction */
191   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
192   COSTS_N_INSNS (3),                    /* variable shift costs */
193   COSTS_N_INSNS (2),                    /* constant shift costs */
194   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
195    COSTS_N_INSNS (12),                  /*                               HI */
196    COSTS_N_INSNS (12),                  /*                               SI */
197    COSTS_N_INSNS (12),                  /*                               DI */
198    COSTS_N_INSNS (12)},                 /*                               other */
199   1,                                    /* cost of multiply per each bit set */
200   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
201    COSTS_N_INSNS (40),                  /*                          HI */
202    COSTS_N_INSNS (40),                  /*                          SI */
203    COSTS_N_INSNS (40),                  /*                          DI */
204    COSTS_N_INSNS (40)},                 /*                          other */
205   COSTS_N_INSNS (3),                    /* cost of movsx */
206   COSTS_N_INSNS (2),                    /* cost of movzx */
207   15,                                   /* "large" insn */
208   3,                                    /* MOVE_RATIO */
209   4,                                    /* cost for loading QImode using movzbl */
210   {2, 4, 2},                            /* cost of loading integer registers
211                                            in QImode, HImode and SImode.
212                                            Relative to reg-reg move (2).  */
213   {2, 4, 2},                            /* cost of storing integer registers */
214   2,                                    /* cost of reg,reg fld/fst */
215   {8, 8, 8},                            /* cost of loading fp registers
216                                            in SFmode, DFmode and XFmode */
217   {8, 8, 8},                            /* cost of storing fp registers
218                                            in SFmode, DFmode and XFmode */
219   2,                                    /* cost of moving MMX register */
220   {4, 8},                               /* cost of loading MMX registers
221                                            in SImode and DImode */
222   {4, 8},                               /* cost of storing MMX registers
223                                            in SImode and DImode */
224   2,                                    /* cost of moving SSE register */
225   {4, 8, 16},                           /* cost of loading SSE registers
226                                            in SImode, DImode and TImode */
227   {4, 8, 16},                           /* cost of storing SSE registers
228                                            in SImode, DImode and TImode */
229   3,                                    /* MMX or SSE register to integer */
230   0,                                    /* size of prefetch block */
231   0,                                    /* number of parallel prefetches */
232   1,                                    /* Branch cost */
233   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
234   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
235   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
236   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
237   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
238   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
239   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
240    DUMMY_STRINGOP_ALGS},
241   {{rep_prefix_4_byte, {{-1, rep_prefix_4_byte}}},
242    DUMMY_STRINGOP_ALGS}
243 };
244
245 static const
246 struct processor_costs pentium_cost = {
247   COSTS_N_INSNS (1),                    /* cost of an add instruction */
248   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
249   COSTS_N_INSNS (4),                    /* variable shift costs */
250   COSTS_N_INSNS (1),                    /* constant shift costs */
251   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
252    COSTS_N_INSNS (11),                  /*                               HI */
253    COSTS_N_INSNS (11),                  /*                               SI */
254    COSTS_N_INSNS (11),                  /*                               DI */
255    COSTS_N_INSNS (11)},                 /*                               other */
256   0,                                    /* cost of multiply per each bit set */
257   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
258    COSTS_N_INSNS (25),                  /*                          HI */
259    COSTS_N_INSNS (25),                  /*                          SI */
260    COSTS_N_INSNS (25),                  /*                          DI */
261    COSTS_N_INSNS (25)},                 /*                          other */
262   COSTS_N_INSNS (3),                    /* cost of movsx */
263   COSTS_N_INSNS (2),                    /* cost of movzx */
264   8,                                    /* "large" insn */
265   6,                                    /* MOVE_RATIO */
266   6,                                    /* cost for loading QImode using movzbl */
267   {2, 4, 2},                            /* cost of loading integer registers
268                                            in QImode, HImode and SImode.
269                                            Relative to reg-reg move (2).  */
270   {2, 4, 2},                            /* cost of storing integer registers */
271   2,                                    /* cost of reg,reg fld/fst */
272   {2, 2, 6},                            /* cost of loading fp registers
273                                            in SFmode, DFmode and XFmode */
274   {4, 4, 6},                            /* cost of storing fp registers
275                                            in SFmode, DFmode and XFmode */
276   8,                                    /* cost of moving MMX register */
277   {8, 8},                               /* cost of loading MMX registers
278                                            in SImode and DImode */
279   {8, 8},                               /* cost of storing MMX registers
280                                            in SImode and DImode */
281   2,                                    /* cost of moving SSE register */
282   {4, 8, 16},                           /* cost of loading SSE registers
283                                            in SImode, DImode and TImode */
284   {4, 8, 16},                           /* cost of storing SSE registers
285                                            in SImode, DImode and TImode */
286   3,                                    /* MMX or SSE register to integer */
287   0,                                    /* size of prefetch block */
288   0,                                    /* number of parallel prefetches */
289   2,                                    /* Branch cost */
290   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
291   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
292   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
293   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
294   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
295   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
296   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
297    DUMMY_STRINGOP_ALGS},
298   {{libcall, {{-1, rep_prefix_4_byte}}},
299    DUMMY_STRINGOP_ALGS}
300 };
301
302 static const
303 struct processor_costs pentiumpro_cost = {
304   COSTS_N_INSNS (1),                    /* cost of an add instruction */
305   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
306   COSTS_N_INSNS (1),                    /* variable shift costs */
307   COSTS_N_INSNS (1),                    /* constant shift costs */
308   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
309    COSTS_N_INSNS (4),                   /*                               HI */
310    COSTS_N_INSNS (4),                   /*                               SI */
311    COSTS_N_INSNS (4),                   /*                               DI */
312    COSTS_N_INSNS (4)},                  /*                               other */
313   0,                                    /* cost of multiply per each bit set */
314   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
315    COSTS_N_INSNS (17),                  /*                          HI */
316    COSTS_N_INSNS (17),                  /*                          SI */
317    COSTS_N_INSNS (17),                  /*                          DI */
318    COSTS_N_INSNS (17)},                 /*                          other */
319   COSTS_N_INSNS (1),                    /* cost of movsx */
320   COSTS_N_INSNS (1),                    /* cost of movzx */
321   8,                                    /* "large" insn */
322   6,                                    /* MOVE_RATIO */
323   2,                                    /* cost for loading QImode using movzbl */
324   {4, 4, 4},                            /* cost of loading integer registers
325                                            in QImode, HImode and SImode.
326                                            Relative to reg-reg move (2).  */
327   {2, 2, 2},                            /* cost of storing integer registers */
328   2,                                    /* cost of reg,reg fld/fst */
329   {2, 2, 6},                            /* cost of loading fp registers
330                                            in SFmode, DFmode and XFmode */
331   {4, 4, 6},                            /* cost of storing fp registers
332                                            in SFmode, DFmode and XFmode */
333   2,                                    /* cost of moving MMX register */
334   {2, 2},                               /* cost of loading MMX registers
335                                            in SImode and DImode */
336   {2, 2},                               /* cost of storing MMX registers
337                                            in SImode and DImode */
338   2,                                    /* cost of moving SSE register */
339   {2, 2, 8},                            /* cost of loading SSE registers
340                                            in SImode, DImode and TImode */
341   {2, 2, 8},                            /* cost of storing SSE registers
342                                            in SImode, DImode and TImode */
343   3,                                    /* MMX or SSE register to integer */
344   32,                                   /* size of prefetch block */
345   6,                                    /* number of parallel prefetches */
346   2,                                    /* Branch cost */
347   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
348   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
349   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
350   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
351   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
352   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
353   /* PentiumPro has optimized rep instructions for blocks aligned by 8 bytes (we ensure
354      the alignment).  For small blocks inline loop is still a noticeable win, for bigger
355      blocks either rep movsl or rep movsb is way to go.  Rep movsb has apparently
356      more expensive startup time in CPU, but after 4K the difference is down in the noise.
357    */
358   {{rep_prefix_4_byte, {{128, loop}, {1024, unrolled_loop},
359                         {8192, rep_prefix_4_byte}, {-1, rep_prefix_1_byte}}},
360    DUMMY_STRINGOP_ALGS},
361   {{rep_prefix_4_byte, {{1024, unrolled_loop},
362                         {8192, rep_prefix_4_byte}, {-1, libcall}}},
363    DUMMY_STRINGOP_ALGS}
364 };
365
366 static const
367 struct processor_costs geode_cost = {
368   COSTS_N_INSNS (1),                    /* cost of an add instruction */
369   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
370   COSTS_N_INSNS (2),                    /* variable shift costs */
371   COSTS_N_INSNS (1),                    /* constant shift costs */
372   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
373    COSTS_N_INSNS (4),                   /*                               HI */
374    COSTS_N_INSNS (7),                   /*                               SI */
375    COSTS_N_INSNS (7),                   /*                               DI */
376    COSTS_N_INSNS (7)},                  /*                               other */
377   0,                                    /* cost of multiply per each bit set */
378   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
379    COSTS_N_INSNS (23),                  /*                          HI */
380    COSTS_N_INSNS (39),                  /*                          SI */
381    COSTS_N_INSNS (39),                  /*                          DI */
382    COSTS_N_INSNS (39)},                 /*                          other */
383   COSTS_N_INSNS (1),                    /* cost of movsx */
384   COSTS_N_INSNS (1),                    /* cost of movzx */
385   8,                                    /* "large" insn */
386   4,                                    /* MOVE_RATIO */
387   1,                                    /* cost for loading QImode using movzbl */
388   {1, 1, 1},                            /* cost of loading integer registers
389                                            in QImode, HImode and SImode.
390                                            Relative to reg-reg move (2).  */
391   {1, 1, 1},                            /* cost of storing integer registers */
392   1,                                    /* cost of reg,reg fld/fst */
393   {1, 1, 1},                            /* cost of loading fp registers
394                                            in SFmode, DFmode and XFmode */
395   {4, 6, 6},                            /* cost of storing fp registers
396                                            in SFmode, DFmode and XFmode */
397
398   1,                                    /* cost of moving MMX register */
399   {1, 1},                               /* cost of loading MMX registers
400                                            in SImode and DImode */
401   {1, 1},                               /* cost of storing MMX registers
402                                            in SImode and DImode */
403   1,                                    /* cost of moving SSE register */
404   {1, 1, 1},                            /* cost of loading SSE registers
405                                            in SImode, DImode and TImode */
406   {1, 1, 1},                            /* cost of storing SSE registers
407                                            in SImode, DImode and TImode */
408   1,                                    /* MMX or SSE register to integer */
409   32,                                   /* size of prefetch block */
410   1,                                    /* number of parallel prefetches */
411   1,                                    /* Branch cost */
412   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
413   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
414   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
415   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
416   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
417   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
418   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
419    DUMMY_STRINGOP_ALGS},
420   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
421    DUMMY_STRINGOP_ALGS}
422 };
423
424 static const
425 struct processor_costs k6_cost = {
426   COSTS_N_INSNS (1),                    /* cost of an add instruction */
427   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
428   COSTS_N_INSNS (1),                    /* variable shift costs */
429   COSTS_N_INSNS (1),                    /* constant shift costs */
430   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
431    COSTS_N_INSNS (3),                   /*                               HI */
432    COSTS_N_INSNS (3),                   /*                               SI */
433    COSTS_N_INSNS (3),                   /*                               DI */
434    COSTS_N_INSNS (3)},                  /*                               other */
435   0,                                    /* cost of multiply per each bit set */
436   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
437    COSTS_N_INSNS (18),                  /*                          HI */
438    COSTS_N_INSNS (18),                  /*                          SI */
439    COSTS_N_INSNS (18),                  /*                          DI */
440    COSTS_N_INSNS (18)},                 /*                          other */
441   COSTS_N_INSNS (2),                    /* cost of movsx */
442   COSTS_N_INSNS (2),                    /* cost of movzx */
443   8,                                    /* "large" insn */
444   4,                                    /* MOVE_RATIO */
445   3,                                    /* cost for loading QImode using movzbl */
446   {4, 5, 4},                            /* cost of loading integer registers
447                                            in QImode, HImode and SImode.
448                                            Relative to reg-reg move (2).  */
449   {2, 3, 2},                            /* cost of storing integer registers */
450   4,                                    /* cost of reg,reg fld/fst */
451   {6, 6, 6},                            /* cost of loading fp registers
452                                            in SFmode, DFmode and XFmode */
453   {4, 4, 4},                            /* cost of storing fp registers
454                                            in SFmode, DFmode and XFmode */
455   2,                                    /* cost of moving MMX register */
456   {2, 2},                               /* cost of loading MMX registers
457                                            in SImode and DImode */
458   {2, 2},                               /* cost of storing MMX registers
459                                            in SImode and DImode */
460   2,                                    /* cost of moving SSE register */
461   {2, 2, 8},                            /* cost of loading SSE registers
462                                            in SImode, DImode and TImode */
463   {2, 2, 8},                            /* cost of storing SSE registers
464                                            in SImode, DImode and TImode */
465   6,                                    /* MMX or SSE register to integer */
466   32,                                   /* size of prefetch block */
467   1,                                    /* number of parallel prefetches */
468   1,                                    /* Branch cost */
469   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
470   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
471   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
472   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
473   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
474   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
475   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
476    DUMMY_STRINGOP_ALGS},
477   {{libcall, {{256, rep_prefix_4_byte}, {-1, libcall}}},
478    DUMMY_STRINGOP_ALGS}
479 };
480
481 static const
482 struct processor_costs athlon_cost = {
483   COSTS_N_INSNS (1),                    /* cost of an add instruction */
484   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
485   COSTS_N_INSNS (1),                    /* variable shift costs */
486   COSTS_N_INSNS (1),                    /* constant shift costs */
487   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
488    COSTS_N_INSNS (5),                   /*                               HI */
489    COSTS_N_INSNS (5),                   /*                               SI */
490    COSTS_N_INSNS (5),                   /*                               DI */
491    COSTS_N_INSNS (5)},                  /*                               other */
492   0,                                    /* cost of multiply per each bit set */
493   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
494    COSTS_N_INSNS (26),                  /*                          HI */
495    COSTS_N_INSNS (42),                  /*                          SI */
496    COSTS_N_INSNS (74),                  /*                          DI */
497    COSTS_N_INSNS (74)},                 /*                          other */
498   COSTS_N_INSNS (1),                    /* cost of movsx */
499   COSTS_N_INSNS (1),                    /* cost of movzx */
500   8,                                    /* "large" insn */
501   9,                                    /* MOVE_RATIO */
502   4,                                    /* cost for loading QImode using movzbl */
503   {3, 4, 3},                            /* cost of loading integer registers
504                                            in QImode, HImode and SImode.
505                                            Relative to reg-reg move (2).  */
506   {3, 4, 3},                            /* cost of storing integer registers */
507   4,                                    /* cost of reg,reg fld/fst */
508   {4, 4, 12},                           /* cost of loading fp registers
509                                            in SFmode, DFmode and XFmode */
510   {6, 6, 8},                            /* cost of storing fp registers
511                                            in SFmode, DFmode and XFmode */
512   2,                                    /* cost of moving MMX register */
513   {4, 4},                               /* cost of loading MMX registers
514                                            in SImode and DImode */
515   {4, 4},                               /* cost of storing MMX registers
516                                            in SImode and DImode */
517   2,                                    /* cost of moving SSE register */
518   {4, 4, 6},                            /* cost of loading SSE registers
519                                            in SImode, DImode and TImode */
520   {4, 4, 5},                            /* cost of storing SSE registers
521                                            in SImode, DImode and TImode */
522   5,                                    /* MMX or SSE register to integer */
523   64,                                   /* size of prefetch block */
524   6,                                    /* number of parallel prefetches */
525   5,                                    /* Branch cost */
526   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
527   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
528   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
529   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
530   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
531   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
532   /* For some reason, Athlon deals better with REP prefix (relative to loops)
533      compared to K8. Alignment becomes important after 8 bytes for memcpy and
534      128 bytes for memset.  */
535   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
536    DUMMY_STRINGOP_ALGS},
537   {{libcall, {{2048, rep_prefix_4_byte}, {-1, libcall}}},
538    DUMMY_STRINGOP_ALGS}
539 };
540
541 static const
542 struct processor_costs k8_cost = {
543   COSTS_N_INSNS (1),                    /* cost of an add instruction */
544   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
545   COSTS_N_INSNS (1),                    /* variable shift costs */
546   COSTS_N_INSNS (1),                    /* constant shift costs */
547   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
548    COSTS_N_INSNS (4),                   /*                               HI */
549    COSTS_N_INSNS (3),                   /*                               SI */
550    COSTS_N_INSNS (4),                   /*                               DI */
551    COSTS_N_INSNS (5)},                  /*                               other */
552   0,                                    /* cost of multiply per each bit set */
553   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
554    COSTS_N_INSNS (26),                  /*                          HI */
555    COSTS_N_INSNS (42),                  /*                          SI */
556    COSTS_N_INSNS (74),                  /*                          DI */
557    COSTS_N_INSNS (74)},                 /*                          other */
558   COSTS_N_INSNS (1),                    /* cost of movsx */
559   COSTS_N_INSNS (1),                    /* cost of movzx */
560   8,                                    /* "large" insn */
561   9,                                    /* MOVE_RATIO */
562   4,                                    /* cost for loading QImode using movzbl */
563   {3, 4, 3},                            /* cost of loading integer registers
564                                            in QImode, HImode and SImode.
565                                            Relative to reg-reg move (2).  */
566   {3, 4, 3},                            /* cost of storing integer registers */
567   4,                                    /* cost of reg,reg fld/fst */
568   {4, 4, 12},                           /* cost of loading fp registers
569                                            in SFmode, DFmode and XFmode */
570   {6, 6, 8},                            /* cost of storing fp registers
571                                            in SFmode, DFmode and XFmode */
572   2,                                    /* cost of moving MMX register */
573   {3, 3},                               /* cost of loading MMX registers
574                                            in SImode and DImode */
575   {4, 4},                               /* cost of storing MMX registers
576                                            in SImode and DImode */
577   2,                                    /* cost of moving SSE register */
578   {4, 3, 6},                            /* cost of loading SSE registers
579                                            in SImode, DImode and TImode */
580   {4, 4, 5},                            /* cost of storing SSE registers
581                                            in SImode, DImode and TImode */
582   5,                                    /* MMX or SSE register to integer */
583   64,                                   /* size of prefetch block */
584   /* New AMD processors never drop prefetches; if they cannot be performed
585      immediately, they are queued.  We set number of simultaneous prefetches
586      to a large constant to reflect this (it probably is not a good idea not
587      to limit number of prefetches at all, as their execution also takes some
588      time).  */
589   100,                                  /* number of parallel prefetches */
590   5,                                    /* Branch cost */
591   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
592   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
593   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
594   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
595   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
596   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
597   /* K8 has optimized REP instruction for medium sized blocks, but for very small
598      blocks it is better to use loop. For large blocks, libcall can do
599      nontemporary accesses and beat inline considerably.  */
600   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
601    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
602   {{libcall, {{8, loop}, {24, unrolled_loop},
603               {2048, rep_prefix_4_byte}, {-1, libcall}}},
604    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
605 };
606
607 struct processor_costs amdfam10_cost = {
608   COSTS_N_INSNS (1),                    /* cost of an add instruction */
609   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
610   COSTS_N_INSNS (1),                    /* variable shift costs */
611   COSTS_N_INSNS (1),                    /* constant shift costs */
612   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
613    COSTS_N_INSNS (4),                   /*                               HI */
614    COSTS_N_INSNS (3),                   /*                               SI */
615    COSTS_N_INSNS (4),                   /*                               DI */
616    COSTS_N_INSNS (5)},                  /*                               other */
617   0,                                    /* cost of multiply per each bit set */
618   {COSTS_N_INSNS (19),                  /* cost of a divide/mod for QI */
619    COSTS_N_INSNS (35),                  /*                          HI */
620    COSTS_N_INSNS (51),                  /*                          SI */
621    COSTS_N_INSNS (83),                  /*                          DI */
622    COSTS_N_INSNS (83)},                 /*                          other */
623   COSTS_N_INSNS (1),                    /* cost of movsx */
624   COSTS_N_INSNS (1),                    /* cost of movzx */
625   8,                                    /* "large" insn */
626   9,                                    /* MOVE_RATIO */
627   4,                                    /* cost for loading QImode using movzbl */
628   {3, 4, 3},                            /* cost of loading integer registers
629                                            in QImode, HImode and SImode.
630                                            Relative to reg-reg move (2).  */
631   {3, 4, 3},                            /* cost of storing integer registers */
632   4,                                    /* cost of reg,reg fld/fst */
633   {4, 4, 12},                           /* cost of loading fp registers
634                                            in SFmode, DFmode and XFmode */
635   {6, 6, 8},                            /* cost of storing fp registers
636                                            in SFmode, DFmode and XFmode */
637   2,                                    /* cost of moving MMX register */
638   {3, 3},                               /* cost of loading MMX registers
639                                            in SImode and DImode */
640   {4, 4},                               /* cost of storing MMX registers
641                                            in SImode and DImode */
642   2,                                    /* cost of moving SSE register */
643   {4, 4, 3},                            /* cost of loading SSE registers
644                                            in SImode, DImode and TImode */
645   {4, 4, 5},                            /* cost of storing SSE registers
646                                            in SImode, DImode and TImode */
647   3,                                    /* MMX or SSE register to integer */
648                                         /* On K8
649                                             MOVD reg64, xmmreg  Double  FSTORE 4
650                                             MOVD reg32, xmmreg  Double  FSTORE 4
651                                            On AMDFAM10
652                                             MOVD reg64, xmmreg  Double  FADD 3
653                                                                 1/1  1/1
654                                             MOVD reg32, xmmreg  Double  FADD 3
655                                                                 1/1  1/1 */
656   64,                                   /* size of prefetch block */
657   /* New AMD processors never drop prefetches; if they cannot be performed
658      immediately, they are queued.  We set number of simultaneous prefetches
659      to a large constant to reflect this (it probably is not a good idea not
660      to limit number of prefetches at all, as their execution also takes some
661      time).  */
662   100,                                  /* number of parallel prefetches */
663   5,                                    /* Branch cost */
664   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
665   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
666   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
667   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
668   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
669   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
670
671   /* AMDFAM10 has optimized REP instruction for medium sized blocks, but for
672      very small blocks it is better to use loop. For large blocks, libcall can
673      do nontemporary accesses and beat inline considerably.  */
674   {{libcall, {{6, loop}, {14, unrolled_loop}, {-1, rep_prefix_4_byte}}},
675    {libcall, {{16, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
676   {{libcall, {{8, loop}, {24, unrolled_loop},
677               {2048, rep_prefix_4_byte}, {-1, libcall}}},
678    {libcall, {{48, unrolled_loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
679 };
680
681 static const
682 struct processor_costs pentium4_cost = {
683   COSTS_N_INSNS (1),                    /* cost of an add instruction */
684   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
685   COSTS_N_INSNS (4),                    /* variable shift costs */
686   COSTS_N_INSNS (4),                    /* constant shift costs */
687   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
688    COSTS_N_INSNS (15),                  /*                               HI */
689    COSTS_N_INSNS (15),                  /*                               SI */
690    COSTS_N_INSNS (15),                  /*                               DI */
691    COSTS_N_INSNS (15)},                 /*                               other */
692   0,                                    /* cost of multiply per each bit set */
693   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
694    COSTS_N_INSNS (56),                  /*                          HI */
695    COSTS_N_INSNS (56),                  /*                          SI */
696    COSTS_N_INSNS (56),                  /*                          DI */
697    COSTS_N_INSNS (56)},                 /*                          other */
698   COSTS_N_INSNS (1),                    /* cost of movsx */
699   COSTS_N_INSNS (1),                    /* cost of movzx */
700   16,                                   /* "large" insn */
701   6,                                    /* MOVE_RATIO */
702   2,                                    /* cost for loading QImode using movzbl */
703   {4, 5, 4},                            /* cost of loading integer registers
704                                            in QImode, HImode and SImode.
705                                            Relative to reg-reg move (2).  */
706   {2, 3, 2},                            /* cost of storing integer registers */
707   2,                                    /* cost of reg,reg fld/fst */
708   {2, 2, 6},                            /* cost of loading fp registers
709                                            in SFmode, DFmode and XFmode */
710   {4, 4, 6},                            /* cost of storing fp registers
711                                            in SFmode, DFmode and XFmode */
712   2,                                    /* cost of moving MMX register */
713   {2, 2},                               /* cost of loading MMX registers
714                                            in SImode and DImode */
715   {2, 2},                               /* cost of storing MMX registers
716                                            in SImode and DImode */
717   12,                                   /* cost of moving SSE register */
718   {12, 12, 12},                         /* cost of loading SSE registers
719                                            in SImode, DImode and TImode */
720   {2, 2, 8},                            /* cost of storing SSE registers
721                                            in SImode, DImode and TImode */
722   10,                                   /* MMX or SSE register to integer */
723   64,                                   /* size of prefetch block */
724   6,                                    /* number of parallel prefetches */
725   2,                                    /* Branch cost */
726   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
727   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
728   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
729   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
730   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
731   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
732   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
733    DUMMY_STRINGOP_ALGS},
734   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
735    {-1, libcall}}},
736    DUMMY_STRINGOP_ALGS},
737 };
738
739 static const
740 struct processor_costs nocona_cost = {
741   COSTS_N_INSNS (1),                    /* cost of an add instruction */
742   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
743   COSTS_N_INSNS (1),                    /* variable shift costs */
744   COSTS_N_INSNS (1),                    /* constant shift costs */
745   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
746    COSTS_N_INSNS (10),                  /*                               HI */
747    COSTS_N_INSNS (10),                  /*                               SI */
748    COSTS_N_INSNS (10),                  /*                               DI */
749    COSTS_N_INSNS (10)},                 /*                               other */
750   0,                                    /* cost of multiply per each bit set */
751   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
752    COSTS_N_INSNS (66),                  /*                          HI */
753    COSTS_N_INSNS (66),                  /*                          SI */
754    COSTS_N_INSNS (66),                  /*                          DI */
755    COSTS_N_INSNS (66)},                 /*                          other */
756   COSTS_N_INSNS (1),                    /* cost of movsx */
757   COSTS_N_INSNS (1),                    /* cost of movzx */
758   16,                                   /* "large" insn */
759   17,                                   /* MOVE_RATIO */
760   4,                                    /* cost for loading QImode using movzbl */
761   {4, 4, 4},                            /* cost of loading integer registers
762                                            in QImode, HImode and SImode.
763                                            Relative to reg-reg move (2).  */
764   {4, 4, 4},                            /* cost of storing integer registers */
765   3,                                    /* cost of reg,reg fld/fst */
766   {12, 12, 12},                         /* cost of loading fp registers
767                                            in SFmode, DFmode and XFmode */
768   {4, 4, 4},                            /* cost of storing fp registers
769                                            in SFmode, DFmode and XFmode */
770   6,                                    /* cost of moving MMX register */
771   {12, 12},                             /* cost of loading MMX registers
772                                            in SImode and DImode */
773   {12, 12},                             /* cost of storing MMX registers
774                                            in SImode and DImode */
775   6,                                    /* cost of moving SSE register */
776   {12, 12, 12},                         /* cost of loading SSE registers
777                                            in SImode, DImode and TImode */
778   {12, 12, 12},                         /* cost of storing SSE registers
779                                            in SImode, DImode and TImode */
780   8,                                    /* MMX or SSE register to integer */
781   128,                                  /* size of prefetch block */
782   8,                                    /* number of parallel prefetches */
783   1,                                    /* Branch cost */
784   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
785   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
786   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
787   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
788   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
789   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
790   {{libcall, {{12, loop_1_byte}, {-1, rep_prefix_4_byte}}},
791    {libcall, {{32, loop}, {20000, rep_prefix_8_byte},
792               {100000, unrolled_loop}, {-1, libcall}}}},
793   {{libcall, {{6, loop_1_byte}, {48, loop}, {20480, rep_prefix_4_byte},
794    {-1, libcall}}},
795    {libcall, {{24, loop}, {64, unrolled_loop},
796               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
797 };
798
799 static const
800 struct processor_costs core2_cost = {
801   COSTS_N_INSNS (1),                    /* cost of an add instruction */
802   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
803   COSTS_N_INSNS (1),                    /* variable shift costs */
804   COSTS_N_INSNS (1),                    /* constant shift costs */
805   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
806    COSTS_N_INSNS (3),                   /*                               HI */
807    COSTS_N_INSNS (3),                   /*                               SI */
808    COSTS_N_INSNS (3),                   /*                               DI */
809    COSTS_N_INSNS (3)},                  /*                               other */
810   0,                                    /* cost of multiply per each bit set */
811   {COSTS_N_INSNS (22),                  /* cost of a divide/mod for QI */
812    COSTS_N_INSNS (22),                  /*                          HI */
813    COSTS_N_INSNS (22),                  /*                          SI */
814    COSTS_N_INSNS (22),                  /*                          DI */
815    COSTS_N_INSNS (22)},                 /*                          other */
816   COSTS_N_INSNS (1),                    /* cost of movsx */
817   COSTS_N_INSNS (1),                    /* cost of movzx */
818   8,                                    /* "large" insn */
819   16,                                   /* MOVE_RATIO */
820   2,                                    /* cost for loading QImode using movzbl */
821   {6, 6, 6},                            /* cost of loading integer registers
822                                            in QImode, HImode and SImode.
823                                            Relative to reg-reg move (2).  */
824   {4, 4, 4},                            /* cost of storing integer registers */
825   2,                                    /* cost of reg,reg fld/fst */
826   {6, 6, 6},                            /* cost of loading fp registers
827                                            in SFmode, DFmode and XFmode */
828   {4, 4, 4},                            /* cost of loading integer registers */
829   2,                                    /* cost of moving MMX register */
830   {6, 6},                               /* cost of loading MMX registers
831                                            in SImode and DImode */
832   {4, 4},                               /* cost of storing MMX registers
833                                            in SImode and DImode */
834   2,                                    /* cost of moving SSE register */
835   {6, 6, 6},                            /* cost of loading SSE registers
836                                            in SImode, DImode and TImode */
837   {4, 4, 4},                            /* cost of storing SSE registers
838                                            in SImode, DImode and TImode */
839   2,                                    /* MMX or SSE register to integer */
840   128,                                  /* size of prefetch block */
841   8,                                    /* number of parallel prefetches */
842   3,                                    /* Branch cost */
843   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
844   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
845   COSTS_N_INSNS (32),                   /* cost of FDIV instruction.  */
846   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
847   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
848   COSTS_N_INSNS (58),                   /* cost of FSQRT instruction.  */
849   {{libcall, {{11, loop}, {-1, rep_prefix_4_byte}}},
850    {libcall, {{32, loop}, {64, rep_prefix_4_byte},
851               {8192, rep_prefix_8_byte}, {-1, libcall}}}},
852   {{libcall, {{8, loop}, {15, unrolled_loop},
853               {2048, rep_prefix_4_byte}, {-1, libcall}}},
854    {libcall, {{24, loop}, {32, unrolled_loop},
855               {8192, rep_prefix_8_byte}, {-1, libcall}}}}
856 };
857
858 /* Generic64 should produce code tuned for Nocona and K8.  */
859 static const
860 struct processor_costs generic64_cost = {
861   COSTS_N_INSNS (1),                    /* cost of an add instruction */
862   /* On all chips taken into consideration lea is 2 cycles and more.  With
863      this cost however our current implementation of synth_mult results in
864      use of unnecessary temporary registers causing regression on several
865      SPECfp benchmarks.  */
866   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
867   COSTS_N_INSNS (1),                    /* variable shift costs */
868   COSTS_N_INSNS (1),                    /* constant shift costs */
869   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
870    COSTS_N_INSNS (4),                   /*                               HI */
871    COSTS_N_INSNS (3),                   /*                               SI */
872    COSTS_N_INSNS (4),                   /*                               DI */
873    COSTS_N_INSNS (2)},                  /*                               other */
874   0,                                    /* cost of multiply per each bit set */
875   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
876    COSTS_N_INSNS (26),                  /*                          HI */
877    COSTS_N_INSNS (42),                  /*                          SI */
878    COSTS_N_INSNS (74),                  /*                          DI */
879    COSTS_N_INSNS (74)},                 /*                          other */
880   COSTS_N_INSNS (1),                    /* cost of movsx */
881   COSTS_N_INSNS (1),                    /* cost of movzx */
882   8,                                    /* "large" insn */
883   17,                                   /* MOVE_RATIO */
884   4,                                    /* cost for loading QImode using movzbl */
885   {4, 4, 4},                            /* cost of loading integer registers
886                                            in QImode, HImode and SImode.
887                                            Relative to reg-reg move (2).  */
888   {4, 4, 4},                            /* cost of storing integer registers */
889   4,                                    /* cost of reg,reg fld/fst */
890   {12, 12, 12},                         /* cost of loading fp registers
891                                            in SFmode, DFmode and XFmode */
892   {6, 6, 8},                            /* cost of storing fp registers
893                                            in SFmode, DFmode and XFmode */
894   2,                                    /* cost of moving MMX register */
895   {8, 8},                               /* cost of loading MMX registers
896                                            in SImode and DImode */
897   {8, 8},                               /* cost of storing MMX registers
898                                            in SImode and DImode */
899   2,                                    /* cost of moving SSE register */
900   {8, 8, 8},                            /* cost of loading SSE registers
901                                            in SImode, DImode and TImode */
902   {8, 8, 8},                            /* cost of storing SSE registers
903                                            in SImode, DImode and TImode */
904   5,                                    /* MMX or SSE register to integer */
905   64,                                   /* size of prefetch block */
906   6,                                    /* number of parallel prefetches */
907   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
908      is increased to perhaps more appropriate value of 5.  */
909   3,                                    /* Branch cost */
910   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
911   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
912   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
913   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
914   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
915   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
916   {DUMMY_STRINGOP_ALGS,
917    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}},
918   {DUMMY_STRINGOP_ALGS,
919    {libcall, {{32, loop}, {8192, rep_prefix_8_byte}, {-1, libcall}}}}
920 };
921
922 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
923 static const
924 struct processor_costs generic32_cost = {
925   COSTS_N_INSNS (1),                    /* cost of an add instruction */
926   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
927   COSTS_N_INSNS (1),                    /* variable shift costs */
928   COSTS_N_INSNS (1),                    /* constant shift costs */
929   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
930    COSTS_N_INSNS (4),                   /*                               HI */
931    COSTS_N_INSNS (3),                   /*                               SI */
932    COSTS_N_INSNS (4),                   /*                               DI */
933    COSTS_N_INSNS (2)},                  /*                               other */
934   0,                                    /* cost of multiply per each bit set */
935   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
936    COSTS_N_INSNS (26),                  /*                          HI */
937    COSTS_N_INSNS (42),                  /*                          SI */
938    COSTS_N_INSNS (74),                  /*                          DI */
939    COSTS_N_INSNS (74)},                 /*                          other */
940   COSTS_N_INSNS (1),                    /* cost of movsx */
941   COSTS_N_INSNS (1),                    /* cost of movzx */
942   8,                                    /* "large" insn */
943   17,                                   /* MOVE_RATIO */
944   4,                                    /* cost for loading QImode using movzbl */
945   {4, 4, 4},                            /* cost of loading integer registers
946                                            in QImode, HImode and SImode.
947                                            Relative to reg-reg move (2).  */
948   {4, 4, 4},                            /* cost of storing integer registers */
949   4,                                    /* cost of reg,reg fld/fst */
950   {12, 12, 12},                         /* cost of loading fp registers
951                                            in SFmode, DFmode and XFmode */
952   {6, 6, 8},                            /* cost of storing fp registers
953                                            in SFmode, DFmode and XFmode */
954   2,                                    /* cost of moving MMX register */
955   {8, 8},                               /* cost of loading MMX registers
956                                            in SImode and DImode */
957   {8, 8},                               /* cost of storing MMX registers
958                                            in SImode and DImode */
959   2,                                    /* cost of moving SSE register */
960   {8, 8, 8},                            /* cost of loading SSE registers
961                                            in SImode, DImode and TImode */
962   {8, 8, 8},                            /* cost of storing SSE registers
963                                            in SImode, DImode and TImode */
964   5,                                    /* MMX or SSE register to integer */
965   64,                                   /* size of prefetch block */
966   6,                                    /* number of parallel prefetches */
967   3,                                    /* Branch cost */
968   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
969   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
970   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
971   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
972   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
973   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
974   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
975    DUMMY_STRINGOP_ALGS},
976   {{libcall, {{32, loop}, {8192, rep_prefix_4_byte}, {-1, libcall}}},
977    DUMMY_STRINGOP_ALGS},
978 };
979
980 const struct processor_costs *ix86_cost = &pentium_cost;
981
982 /* Processor feature/optimization bitmasks.  */
983 #define m_386 (1<<PROCESSOR_I386)
984 #define m_486 (1<<PROCESSOR_I486)
985 #define m_PENT (1<<PROCESSOR_PENTIUM)
986 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
987 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
988 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
989 #define m_CORE2  (1<<PROCESSOR_CORE2)
990
991 #define m_GEODE  (1<<PROCESSOR_GEODE)
992 #define m_K6  (1<<PROCESSOR_K6)
993 #define m_K6_GEODE  (m_K6 | m_GEODE)
994 #define m_K8  (1<<PROCESSOR_K8)
995 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
996 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
997 #define m_AMDFAM10  (1<<PROCESSOR_AMDFAM10)
998 #define m_ATHLON_K8_AMDFAM10  (m_K8 | m_ATHLON | m_AMDFAM10)
999
1000 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
1001 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
1002
1003 /* Generic instruction choice should be common subset of supported CPUs
1004    (PPro/PENT4/NOCONA/CORE2/Athlon/K8).  */
1005 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
1006
1007 /* Feature tests against the various tunings.  */
1008 unsigned int ix86_tune_features[X86_TUNE_LAST] = {
1009   /* X86_TUNE_USE_LEAVE: Leave does not affect Nocona SPEC2000 results
1010      negatively, so enabling for Generic64 seems like good code size
1011      tradeoff.  We can't enable it for 32bit generic because it does not
1012      work well with PPro base chips.  */
1013   m_386 | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_CORE2 | m_GENERIC64,
1014
1015   /* X86_TUNE_PUSH_MEMORY */
1016   m_386 | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4
1017   | m_NOCONA | m_CORE2 | m_GENERIC,
1018
1019   /* X86_TUNE_ZERO_EXTEND_WITH_AND */
1020   m_486 | m_PENT,
1021
1022   /* X86_TUNE_USE_BIT_TEST */
1023   m_386,
1024
1025   /* X86_TUNE_UNROLL_STRLEN */
1026   m_486 | m_PENT | m_PPRO | m_ATHLON_K8_AMDFAM10 | m_K6 | m_CORE2 | m_GENERIC,
1027
1028   /* X86_TUNE_DEEP_BRANCH_PREDICTION */
1029   m_PPRO | m_K6_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4
1030   | m_NOCONA | m_CORE2 | m_GENERIC,
1031
1032   /* X86_TUNE_BRANCH_PREDICTION_HINTS: Branch hints were put in P4 based
1033      on simulation result. But after P4 was made, no performance benefit
1034      was observed with branch hints.  It also increases the code size.
1035      As a result, icc never generates branch hints.  */
1036   0,
1037
1038   /* X86_TUNE_DOUBLE_WITH_ADD */
1039   ~m_386,
1040   
1041   /* X86_TUNE_USE_SAHF */
1042   m_PPRO | m_K6_GEODE | m_K8 | m_AMDFAM10 | m_PENT4
1043   | m_NOCONA | m_CORE2 | m_GENERIC,
1044
1045   /* X86_TUNE_MOVX: Enable to zero extend integer registers to avoid
1046      partial dependencies.  */
1047   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_PENT4 | m_NOCONA
1048   | m_CORE2 | m_GENERIC | m_GEODE /* m_386 | m_K6 */,
1049
1050   /* X86_TUNE_PARTIAL_REG_STALL: We probably ought to watch for partial
1051      register stalls on Generic32 compilation setting as well.  However
1052      in current implementation the partial register stalls are not eliminated
1053      very well - they can be introduced via subregs synthesized by combine
1054      and can happen in caller/callee saving sequences.  Because this option
1055      pays back little on PPro based chips and is in conflict with partial reg
1056      dependencies used by Athlon/P4 based chips, it is better to leave it off
1057      for generic32 for now.  */
1058   m_PPRO,
1059
1060   /* X86_TUNE_PARTIAL_FLAG_REG_STALL */
1061   m_CORE2 | m_GENERIC,
1062   
1063   /* X86_TUNE_USE_HIMODE_FIOP */
1064   m_386 | m_486 | m_K6_GEODE,
1065
1066   /* X86_TUNE_USE_SIMODE_FIOP */
1067   ~(m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT | m_CORE2 | m_GENERIC),
1068
1069   /* X86_TUNE_USE_MOV0 */
1070   m_K6,
1071   
1072   /* X86_TUNE_USE_CLTD */
1073   ~(m_PENT | m_K6 | m_CORE2 | m_GENERIC),
1074
1075   /* X86_TUNE_USE_XCHGB: Use xchgb %rh,%rl instead of rolw/rorw $8,rx.  */
1076   m_PENT4,
1077
1078   /* X86_TUNE_SPLIT_LONG_MOVES */
1079   m_PPRO,
1080
1081   /* X86_TUNE_READ_MODIFY_WRITE */
1082   ~m_PENT,
1083
1084   /* X86_TUNE_READ_MODIFY */
1085   ~(m_PENT | m_PPRO),
1086
1087   /* X86_TUNE_PROMOTE_QIMODE */
1088   m_K6_GEODE | m_PENT | m_386 | m_486 | m_ATHLON_K8_AMDFAM10 | m_CORE2
1089   | m_GENERIC /* | m_PENT4 ? */,
1090
1091   /* X86_TUNE_FAST_PREFIX */
1092   ~(m_PENT | m_486 | m_386),
1093
1094   /* X86_TUNE_SINGLE_STRINGOP */
1095   m_386 | m_PENT4 | m_NOCONA,
1096   
1097   /* X86_TUNE_QIMODE_MATH */
1098   ~0,
1099   
1100   /* X86_TUNE_HIMODE_MATH: On PPro this flag is meant to avoid partial
1101      register stalls.  Just like X86_TUNE_PARTIAL_REG_STALL this option
1102      might be considered for Generic32 if our scheme for avoiding partial
1103      stalls was more effective.  */
1104   ~m_PPRO,
1105
1106   /* X86_TUNE_PROMOTE_QI_REGS */
1107   0,
1108
1109   /* X86_TUNE_PROMOTE_HI_REGS */
1110   m_PPRO,
1111
1112   /* X86_TUNE_ADD_ESP_4: Enable if add/sub is preferred over 1/2 push/pop.  */
1113   m_ATHLON_K8_AMDFAM10 | m_K6_GEODE | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1114
1115   /* X86_TUNE_ADD_ESP_8 */
1116   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_K6_GEODE | m_386
1117   | m_486 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1118
1119   /* X86_TUNE_SUB_ESP_4 */
1120   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1121
1122   /* X86_TUNE_SUB_ESP_8 */
1123   m_ATHLON_K8_AMDFAM10 | m_PPRO | m_386 | m_486
1124   | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1125
1126   /* X86_TUNE_INTEGER_DFMODE_MOVES: Enable if integer moves are preferred
1127      for DFmode copies */
1128   ~(m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2
1129     | m_GENERIC | m_GEODE),
1130
1131   /* X86_TUNE_PARTIAL_REG_DEPENDENCY */
1132   m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1133
1134   /* X86_TUNE_SSE_PARTIAL_REG_DEPENDENCY: In the Generic model we have a
1135      conflict here in between PPro/Pentium4 based chips that thread 128bit
1136      SSE registers as single units versus K8 based chips that divide SSE
1137      registers to two 64bit halves.  This knob promotes all store destinations
1138      to be 128bit to allow register renaming on 128bit SSE units, but usually
1139      results in one extra microop on 64bit SSE units.  Experimental results
1140      shows that disabling this option on P4 brings over 20% SPECfp regression,
1141      while enabling it on K8 brings roughly 2.4% regression that can be partly
1142      masked by careful scheduling of moves.  */
1143   m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC | m_AMDFAM10,
1144
1145   /* X86_TUNE_SSE_UNALIGNED_MOVE_OPTIMAL */
1146   m_AMDFAM10,
1147
1148   /* X86_TUNE_SSE_SPLIT_REGS: Set for machines where the type and dependencies
1149      are resolved on SSE register parts instead of whole registers, so we may
1150      maintain just lower part of scalar values in proper format leaving the
1151      upper part undefined.  */
1152   m_ATHLON_K8,
1153
1154   /* X86_TUNE_SSE_TYPELESS_STORES */
1155   m_ATHLON_K8_AMDFAM10,
1156
1157   /* X86_TUNE_SSE_LOAD0_BY_PXOR */
1158   m_PPRO | m_PENT4 | m_NOCONA,
1159
1160   /* X86_TUNE_MEMORY_MISMATCH_STALL */
1161   m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1162
1163   /* X86_TUNE_PROLOGUE_USING_MOVE */
1164   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1165
1166   /* X86_TUNE_EPILOGUE_USING_MOVE */
1167   m_ATHLON_K8 | m_PPRO | m_CORE2 | m_GENERIC,
1168
1169   /* X86_TUNE_SHIFT1 */
1170   ~m_486,
1171
1172   /* X86_TUNE_USE_FFREEP */
1173   m_ATHLON_K8_AMDFAM10,
1174
1175   /* X86_TUNE_INTER_UNIT_MOVES */
1176   ~(m_ATHLON_K8_AMDFAM10 | m_GENERIC),
1177
1178   /* X86_TUNE_FOUR_JUMP_LIMIT: Some CPU cores are not able to predict more
1179      than 4 branch instructions in the 16 byte window.  */
1180   m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC,
1181
1182   /* X86_TUNE_SCHEDULE */
1183   m_PPRO | m_ATHLON_K8_AMDFAM10 | m_K6_GEODE | m_PENT | m_CORE2 | m_GENERIC,
1184
1185   /* X86_TUNE_USE_BT */
1186   m_ATHLON_K8_AMDFAM10,
1187
1188   /* X86_TUNE_USE_INCDEC */
1189   ~(m_PENT4 | m_NOCONA | m_CORE2 | m_GENERIC),
1190
1191   /* X86_TUNE_PAD_RETURNS */
1192   m_ATHLON_K8_AMDFAM10 | m_CORE2 | m_GENERIC,
1193
1194   /* X86_TUNE_EXT_80387_CONSTANTS */
1195   m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC,
1196
1197   /* X86_TUNE_SHORTEN_X87_SSE */
1198   ~m_K8,
1199
1200   /* X86_TUNE_AVOID_VECTOR_DECODE */
1201   m_K8 | m_GENERIC64,
1202
1203   /* X86_TUNE_SLOW_IMUL_IMM32_MEM (imul of 32-bit constant and memory is vector
1204      path on AMD machines) */
1205   m_K8 | m_GENERIC64 | m_AMDFAM10,
1206
1207   /* X86_TUNE_SLOW_IMUL_IMM8 (imul of 8-bit constant is vector path on AMD
1208      machines)  */
1209   m_K8 | m_GENERIC64 | m_AMDFAM10,
1210
1211   /* X86_TUNE_MOVE_M1_VIA_OR (on pentiums, it is faster to load -1 via OR than
1212      a MOV) */
1213   m_PENT,
1214
1215   /* X86_TUNE_NOT_UNPAIRABLE (NOT is not pairable on Pentium, while XOR is, but
1216      one byte longer).  */
1217   m_PENT,
1218
1219   /* X86_TUNE_NOT_VECTORMODE (On AMD K6, NOT is vector decoded with memory
1220      operand that cannot be represented using a modRM byte.  The XOR
1221      replacement is long decoded, so this split helps here as well).  */
1222   m_K6,
1223 };
1224
1225 /* Feature tests against the various architecture variations.  */
1226 unsigned int ix86_arch_features[X86_ARCH_LAST] = {
1227   /* X86_ARCH_CMOVE */
1228   m_PPRO | m_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA,
1229
1230   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1231   ~m_386,
1232
1233   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1234   ~(m_386 | m_486),
1235
1236   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1237   ~m_386,
1238
1239   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1240   ~m_386,
1241 };
1242
1243 static const unsigned int x86_accumulate_outgoing_args
1244   = m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
1245
1246 static const unsigned int x86_arch_always_fancy_math_387
1247   = m_PENT | m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT4
1248     | m_NOCONA | m_CORE2 | m_GENERIC;
1249
1250 static enum stringop_alg stringop_alg = no_stringop;
1251
1252 /* In case the average insn count for single function invocation is
1253    lower than this constant, emit fast (but longer) prologue and
1254    epilogue code.  */
1255 #define FAST_PROLOGUE_INSN_COUNT 20
1256
1257 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1258 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1259 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1260 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1261
1262 /* Array of the smallest class containing reg number REGNO, indexed by
1263    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1264
1265 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1266 {
1267   /* ax, dx, cx, bx */
1268   AREG, DREG, CREG, BREG,
1269   /* si, di, bp, sp */
1270   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1271   /* FP registers */
1272   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1273   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1274   /* arg pointer */
1275   NON_Q_REGS,
1276   /* flags, fpsr, fpcr, frame */
1277   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1278   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1279   SSE_REGS, SSE_REGS,
1280   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1281   MMX_REGS, MMX_REGS,
1282   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1283   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1284   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1285   SSE_REGS, SSE_REGS,
1286 };
1287
1288 /* The "default" register map used in 32bit mode.  */
1289
1290 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1291 {
1292   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1293   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1294   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1295   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1296   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1297   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1298   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1299 };
1300
1301 static int const x86_64_int_parameter_registers[6] =
1302 {
1303   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
1304   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1305 };
1306
1307 static int const x86_64_int_return_registers[4] =
1308 {
1309   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
1310 };
1311
1312 /* The "default" register map used in 64bit mode.  */
1313 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1314 {
1315   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1316   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1317   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1318   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1319   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1320   8,9,10,11,12,13,14,15,                /* extended integer registers */
1321   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1322 };
1323
1324 /* Define the register numbers to be used in Dwarf debugging information.
1325    The SVR4 reference port C compiler uses the following register numbers
1326    in its Dwarf output code:
1327         0 for %eax (gcc regno = 0)
1328         1 for %ecx (gcc regno = 2)
1329         2 for %edx (gcc regno = 1)
1330         3 for %ebx (gcc regno = 3)
1331         4 for %esp (gcc regno = 7)
1332         5 for %ebp (gcc regno = 6)
1333         6 for %esi (gcc regno = 4)
1334         7 for %edi (gcc regno = 5)
1335    The following three DWARF register numbers are never generated by
1336    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1337    believes these numbers have these meanings.
1338         8  for %eip    (no gcc equivalent)
1339         9  for %eflags (gcc regno = 17)
1340         10 for %trapno (no gcc equivalent)
1341    It is not at all clear how we should number the FP stack registers
1342    for the x86 architecture.  If the version of SDB on x86/svr4 were
1343    a bit less brain dead with respect to floating-point then we would
1344    have a precedent to follow with respect to DWARF register numbers
1345    for x86 FP registers, but the SDB on x86/svr4 is so completely
1346    broken with respect to FP registers that it is hardly worth thinking
1347    of it as something to strive for compatibility with.
1348    The version of x86/svr4 SDB I have at the moment does (partially)
1349    seem to believe that DWARF register number 11 is associated with
1350    the x86 register %st(0), but that's about all.  Higher DWARF
1351    register numbers don't seem to be associated with anything in
1352    particular, and even for DWARF regno 11, SDB only seems to under-
1353    stand that it should say that a variable lives in %st(0) (when
1354    asked via an `=' command) if we said it was in DWARF regno 11,
1355    but SDB still prints garbage when asked for the value of the
1356    variable in question (via a `/' command).
1357    (Also note that the labels SDB prints for various FP stack regs
1358    when doing an `x' command are all wrong.)
1359    Note that these problems generally don't affect the native SVR4
1360    C compiler because it doesn't allow the use of -O with -g and
1361    because when it is *not* optimizing, it allocates a memory
1362    location for each floating-point variable, and the memory
1363    location is what gets described in the DWARF AT_location
1364    attribute for the variable in question.
1365    Regardless of the severe mental illness of the x86/svr4 SDB, we
1366    do something sensible here and we use the following DWARF
1367    register numbers.  Note that these are all stack-top-relative
1368    numbers.
1369         11 for %st(0) (gcc regno = 8)
1370         12 for %st(1) (gcc regno = 9)
1371         13 for %st(2) (gcc regno = 10)
1372         14 for %st(3) (gcc regno = 11)
1373         15 for %st(4) (gcc regno = 12)
1374         16 for %st(5) (gcc regno = 13)
1375         17 for %st(6) (gcc regno = 14)
1376         18 for %st(7) (gcc regno = 15)
1377 */
1378 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1379 {
1380   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1381   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1382   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1383   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1384   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1385   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1386   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1387 };
1388
1389 /* Test and compare insns in i386.md store the information needed to
1390    generate branch and scc insns here.  */
1391
1392 rtx ix86_compare_op0 = NULL_RTX;
1393 rtx ix86_compare_op1 = NULL_RTX;
1394 rtx ix86_compare_emitted = NULL_RTX;
1395
1396 /* Size of the register save area.  */
1397 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1398
1399 /* Define the structure for the machine field in struct function.  */
1400
1401 struct stack_local_entry GTY(())
1402 {
1403   unsigned short mode;
1404   unsigned short n;
1405   rtx rtl;
1406   struct stack_local_entry *next;
1407 };
1408
1409 /* Structure describing stack frame layout.
1410    Stack grows downward:
1411
1412    [arguments]
1413                                               <- ARG_POINTER
1414    saved pc
1415
1416    saved frame pointer if frame_pointer_needed
1417                                               <- HARD_FRAME_POINTER
1418    [saved regs]
1419
1420    [padding1]          \
1421                         )
1422    [va_arg registers]  (
1423                         > to_allocate         <- FRAME_POINTER
1424    [frame]             (
1425                         )
1426    [padding2]          /
1427   */
1428 struct ix86_frame
1429 {
1430   int nregs;
1431   int padding1;
1432   int va_arg_size;
1433   HOST_WIDE_INT frame;
1434   int padding2;
1435   int outgoing_arguments_size;
1436   int red_zone_size;
1437
1438   HOST_WIDE_INT to_allocate;
1439   /* The offsets relative to ARG_POINTER.  */
1440   HOST_WIDE_INT frame_pointer_offset;
1441   HOST_WIDE_INT hard_frame_pointer_offset;
1442   HOST_WIDE_INT stack_pointer_offset;
1443
1444   /* When save_regs_using_mov is set, emit prologue using
1445      move instead of push instructions.  */
1446   bool save_regs_using_mov;
1447 };
1448
1449 /* Code model option.  */
1450 enum cmodel ix86_cmodel;
1451 /* Asm dialect.  */
1452 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1453 /* TLS dialects.  */
1454 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1455
1456 /* Which unit we are generating floating point math for.  */
1457 enum fpmath_unit ix86_fpmath;
1458
1459 /* Which cpu are we scheduling for.  */
1460 enum processor_type ix86_tune;
1461
1462 /* Which instruction set architecture to use.  */
1463 enum processor_type ix86_arch;
1464
1465 /* true if sse prefetch instruction is not NOOP.  */
1466 int x86_prefetch_sse;
1467
1468 /* ix86_regparm_string as a number */
1469 static int ix86_regparm;
1470
1471 /* -mstackrealign option */
1472 extern int ix86_force_align_arg_pointer;
1473 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1474
1475 /* Preferred alignment for stack boundary in bits.  */
1476 unsigned int ix86_preferred_stack_boundary;
1477
1478 /* Values 1-5: see jump.c */
1479 int ix86_branch_cost;
1480
1481 /* Variables which are this size or smaller are put in the data/bss
1482    or ldata/lbss sections.  */
1483
1484 int ix86_section_threshold = 65536;
1485
1486 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1487 char internal_label_prefix[16];
1488 int internal_label_prefix_len;
1489
1490 /* Register class used for passing given 64bit part of the argument.
1491    These represent classes as documented by the PS ABI, with the exception
1492    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1493    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1494
1495    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1496    whenever possible (upper half does contain padding).  */
1497 enum x86_64_reg_class
1498   {
1499     X86_64_NO_CLASS,
1500     X86_64_INTEGER_CLASS,
1501     X86_64_INTEGERSI_CLASS,
1502     X86_64_SSE_CLASS,
1503     X86_64_SSESF_CLASS,
1504     X86_64_SSEDF_CLASS,
1505     X86_64_SSEUP_CLASS,
1506     X86_64_X87_CLASS,
1507     X86_64_X87UP_CLASS,
1508     X86_64_COMPLEX_X87_CLASS,
1509     X86_64_MEMORY_CLASS
1510   };
1511 static const char * const x86_64_reg_class_name[] =
1512 {
1513   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1514   "sseup", "x87", "x87up", "cplx87", "no"
1515 };
1516
1517 #define MAX_CLASSES 4
1518
1519 /* Table of constants used by fldpi, fldln2, etc....  */
1520 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1521 static bool ext_80387_constants_init = 0;
1522
1523 \f
1524 static struct machine_function * ix86_init_machine_status (void);
1525 static rtx ix86_function_value (tree, tree, bool);
1526 static int ix86_function_regparm (tree, tree);
1527 static void ix86_compute_frame_layout (struct ix86_frame *);
1528 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1529                                                  rtx, rtx, int);
1530
1531 \f
1532 /* The svr4 ABI for the i386 says that records and unions are returned
1533    in memory.  */
1534 #ifndef DEFAULT_PCC_STRUCT_RETURN
1535 #define DEFAULT_PCC_STRUCT_RETURN 1
1536 #endif
1537
1538 /* Implement TARGET_HANDLE_OPTION.  */
1539
1540 static bool
1541 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1542 {
1543   switch (code)
1544     {
1545     case OPT_m3dnow:
1546       if (!value)
1547         {
1548           target_flags &= ~MASK_3DNOW_A;
1549           target_flags_explicit |= MASK_3DNOW_A;
1550         }
1551       return true;
1552
1553     case OPT_mmmx:
1554       if (!value)
1555         {
1556           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1557           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1558         }
1559       return true;
1560
1561     case OPT_msse:
1562       if (!value)
1563         {
1564           target_flags &= ~(MASK_SSE2 | MASK_SSE3 | MASK_SSE4A);
1565           target_flags_explicit |= MASK_SSE2 | MASK_SSE3 | MASK_SSE4A;
1566         }
1567       return true;
1568
1569     case OPT_msse2:
1570       if (!value)
1571         {
1572           target_flags &= ~(MASK_SSE3 | MASK_SSE4A);
1573           target_flags_explicit |= MASK_SSE3 | MASK_SSE4A;
1574         }
1575       return true;
1576
1577     case OPT_msse3:
1578       if (!value)
1579         {
1580           target_flags &= ~MASK_SSE4A;
1581           target_flags_explicit |= MASK_SSE4A;
1582         }
1583       return true;
1584
1585     default:
1586       return true;
1587     }
1588 }
1589
1590 /* Sometimes certain combinations of command options do not make
1591    sense on a particular target machine.  You can define a macro
1592    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1593    defined, is executed once just after all the command options have
1594    been parsed.
1595
1596    Don't use this macro to turn on various extra optimizations for
1597    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1598
1599 void
1600 override_options (void)
1601 {
1602   int i;
1603   int ix86_tune_defaulted = 0;
1604   unsigned int ix86_arch_mask, ix86_tune_mask;
1605
1606   /* Comes from final.c -- no real reason to change it.  */
1607 #define MAX_CODE_ALIGN 16
1608
1609   static struct ptt
1610     {
1611       const struct processor_costs *cost;       /* Processor costs */
1612       const int target_enable;                  /* Target flags to enable.  */
1613       const int target_disable;                 /* Target flags to disable.  */
1614       const int align_loop;                     /* Default alignments.  */
1615       const int align_loop_max_skip;
1616       const int align_jump;
1617       const int align_jump_max_skip;
1618       const int align_func;
1619     }
1620   const processor_target_table[PROCESSOR_max] =
1621     {
1622       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1623       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1624       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1625       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1626       {&geode_cost, 0, 0, 0, 0, 0, 0, 0},
1627       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1628       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1629       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1630       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1631       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1632       {&core2_cost, 0, 0, 16, 7, 16, 7, 16},
1633       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1634       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16},
1635       {&amdfam10_cost, 0, 0, 32, 7, 32, 7, 32}
1636     };
1637
1638   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1639   static struct pta
1640     {
1641       const char *const name;           /* processor name or nickname.  */
1642       const enum processor_type processor;
1643       const enum pta_flags
1644         {
1645           PTA_SSE = 1 << 0,
1646           PTA_SSE2 = 1 << 1,
1647           PTA_SSE3 = 1 << 2,
1648           PTA_MMX = 1 << 3,
1649           PTA_PREFETCH_SSE = 1 << 4,
1650           PTA_3DNOW = 1 << 5,
1651           PTA_3DNOW_A = 1 << 6,
1652           PTA_64BIT = 1 << 7,
1653           PTA_SSSE3 = 1 << 8,
1654           PTA_CX16 = 1 << 9,
1655           PTA_POPCNT = 1 << 10,
1656           PTA_ABM = 1 << 11,
1657           PTA_SSE4A = 1 << 12,
1658           PTA_NO_SAHF = 1 << 13
1659         } flags;
1660     }
1661   const processor_alias_table[] =
1662     {
1663       {"i386", PROCESSOR_I386, 0},
1664       {"i486", PROCESSOR_I486, 0},
1665       {"i586", PROCESSOR_PENTIUM, 0},
1666       {"pentium", PROCESSOR_PENTIUM, 0},
1667       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1668       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1669       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1670       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1671       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1672       {"i686", PROCESSOR_PENTIUMPRO, 0},
1673       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1674       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1675       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1676       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1677       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1678       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1679                                        | PTA_MMX | PTA_PREFETCH_SSE},
1680       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1681                                         | PTA_MMX | PTA_PREFETCH_SSE},
1682       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1683                                         | PTA_MMX | PTA_PREFETCH_SSE},
1684       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1685                                         | PTA_MMX | PTA_PREFETCH_SSE
1686                                         | PTA_CX16 | PTA_NO_SAHF},
1687       {"core2", PROCESSOR_CORE2, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_SSSE3
1688                                         | PTA_64BIT | PTA_MMX
1689                                         | PTA_PREFETCH_SSE | PTA_CX16},
1690       {"geode", PROCESSOR_GEODE, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1691                                    | PTA_3DNOW_A},
1692       {"k6", PROCESSOR_K6, PTA_MMX},
1693       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1694       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1695       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1696                                    | PTA_3DNOW_A},
1697       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1698                                          | PTA_3DNOW | PTA_3DNOW_A},
1699       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1700                                     | PTA_3DNOW_A | PTA_SSE},
1701       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1702                                       | PTA_3DNOW_A | PTA_SSE},
1703       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1704                                       | PTA_3DNOW_A | PTA_SSE},
1705       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1706                                | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
1707       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1708                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2
1709                                       | PTA_NO_SAHF},
1710       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1711                                         | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1712                                         | PTA_SSE2 | PTA_NO_SAHF},
1713       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1714                                          | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1715                                          | PTA_SSE2 | PTA_NO_SAHF},
1716       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1717                                           | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1718                                           | PTA_SSE2 | PTA_NO_SAHF},
1719       {"amdfam10", PROCESSOR_AMDFAM10, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1720                                        | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1721                                        | PTA_SSE2 | PTA_SSE3 | PTA_POPCNT
1722                                        | PTA_ABM | PTA_SSE4A | PTA_CX16},
1723       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1724       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1725     };
1726
1727   int const pta_size = ARRAY_SIZE (processor_alias_table);
1728
1729 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1730   SUBTARGET_OVERRIDE_OPTIONS;
1731 #endif
1732
1733 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1734   SUBSUBTARGET_OVERRIDE_OPTIONS;
1735 #endif
1736
1737   /* -fPIC is the default for x86_64.  */
1738   if (TARGET_MACHO && TARGET_64BIT)
1739     flag_pic = 2;
1740
1741   /* Set the default values for switches whose default depends on TARGET_64BIT
1742      in case they weren't overwritten by command line options.  */
1743   if (TARGET_64BIT)
1744     {
1745       /* Mach-O doesn't support omitting the frame pointer for now.  */
1746       if (flag_omit_frame_pointer == 2)
1747         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1748       if (flag_asynchronous_unwind_tables == 2)
1749         flag_asynchronous_unwind_tables = 1;
1750       if (flag_pcc_struct_return == 2)
1751         flag_pcc_struct_return = 0;
1752     }
1753   else
1754     {
1755       if (flag_omit_frame_pointer == 2)
1756         flag_omit_frame_pointer = 0;
1757       if (flag_asynchronous_unwind_tables == 2)
1758         flag_asynchronous_unwind_tables = 0;
1759       if (flag_pcc_struct_return == 2)
1760         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1761     }
1762
1763   /* Need to check -mtune=generic first.  */
1764   if (ix86_tune_string)
1765     {
1766       if (!strcmp (ix86_tune_string, "generic")
1767           || !strcmp (ix86_tune_string, "i686")
1768           /* As special support for cross compilers we read -mtune=native
1769              as -mtune=generic.  With native compilers we won't see the
1770              -mtune=native, as it was changed by the driver.  */
1771           || !strcmp (ix86_tune_string, "native"))
1772         {
1773           if (TARGET_64BIT)
1774             ix86_tune_string = "generic64";
1775           else
1776             ix86_tune_string = "generic32";
1777         }
1778       else if (!strncmp (ix86_tune_string, "generic", 7))
1779         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1780     }
1781   else
1782     {
1783       if (ix86_arch_string)
1784         ix86_tune_string = ix86_arch_string;
1785       if (!ix86_tune_string)
1786         {
1787           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1788           ix86_tune_defaulted = 1;
1789         }
1790
1791       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1792          need to use a sensible tune option.  */
1793       if (!strcmp (ix86_tune_string, "generic")
1794           || !strcmp (ix86_tune_string, "x86-64")
1795           || !strcmp (ix86_tune_string, "i686"))
1796         {
1797           if (TARGET_64BIT)
1798             ix86_tune_string = "generic64";
1799           else
1800             ix86_tune_string = "generic32";
1801         }
1802     }
1803   if (ix86_stringop_string)
1804     {
1805       if (!strcmp (ix86_stringop_string, "rep_byte"))
1806         stringop_alg = rep_prefix_1_byte;
1807       else if (!strcmp (ix86_stringop_string, "libcall"))
1808         stringop_alg = libcall;
1809       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
1810         stringop_alg = rep_prefix_4_byte;
1811       else if (!strcmp (ix86_stringop_string, "rep_8byte"))
1812         stringop_alg = rep_prefix_8_byte;
1813       else if (!strcmp (ix86_stringop_string, "byte_loop"))
1814         stringop_alg = loop_1_byte;
1815       else if (!strcmp (ix86_stringop_string, "loop"))
1816         stringop_alg = loop;
1817       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
1818         stringop_alg = unrolled_loop;
1819       else
1820         error ("bad value (%s) for -mstringop-strategy= switch", ix86_stringop_string);
1821     }
1822   if (!strcmp (ix86_tune_string, "x86-64"))
1823     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1824              "-mtune=generic instead as appropriate.");
1825
1826   if (!ix86_arch_string)
1827     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1828   if (!strcmp (ix86_arch_string, "generic"))
1829     error ("generic CPU can be used only for -mtune= switch");
1830   if (!strncmp (ix86_arch_string, "generic", 7))
1831     error ("bad value (%s) for -march= switch", ix86_arch_string);
1832
1833   if (ix86_cmodel_string != 0)
1834     {
1835       if (!strcmp (ix86_cmodel_string, "small"))
1836         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1837       else if (!strcmp (ix86_cmodel_string, "medium"))
1838         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1839       else if (!strcmp (ix86_cmodel_string, "large"))
1840         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
1841       else if (flag_pic)
1842         error ("code model %s does not support PIC mode", ix86_cmodel_string);
1843       else if (!strcmp (ix86_cmodel_string, "32"))
1844         ix86_cmodel = CM_32;
1845       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1846         ix86_cmodel = CM_KERNEL;
1847       else
1848         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1849     }
1850   else
1851     {
1852       ix86_cmodel = CM_32;
1853       if (TARGET_64BIT)
1854         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1855     }
1856   if (ix86_asm_string != 0)
1857     {
1858       if (! TARGET_MACHO
1859           && !strcmp (ix86_asm_string, "intel"))
1860         ix86_asm_dialect = ASM_INTEL;
1861       else if (!strcmp (ix86_asm_string, "att"))
1862         ix86_asm_dialect = ASM_ATT;
1863       else
1864         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1865     }
1866   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1867     error ("code model %qs not supported in the %s bit mode",
1868            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1869   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1870     sorry ("%i-bit mode not compiled in",
1871            (target_flags & MASK_64BIT) ? 64 : 32);
1872
1873   for (i = 0; i < pta_size; i++)
1874     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1875       {
1876         ix86_arch = processor_alias_table[i].processor;
1877         /* Default cpu tuning to the architecture.  */
1878         ix86_tune = ix86_arch;
1879         if (processor_alias_table[i].flags & PTA_MMX
1880             && !(target_flags_explicit & MASK_MMX))
1881           target_flags |= MASK_MMX;
1882         if (processor_alias_table[i].flags & PTA_3DNOW
1883             && !(target_flags_explicit & MASK_3DNOW))
1884           target_flags |= MASK_3DNOW;
1885         if (processor_alias_table[i].flags & PTA_3DNOW_A
1886             && !(target_flags_explicit & MASK_3DNOW_A))
1887           target_flags |= MASK_3DNOW_A;
1888         if (processor_alias_table[i].flags & PTA_SSE
1889             && !(target_flags_explicit & MASK_SSE))
1890           target_flags |= MASK_SSE;
1891         if (processor_alias_table[i].flags & PTA_SSE2
1892             && !(target_flags_explicit & MASK_SSE2))
1893           target_flags |= MASK_SSE2;
1894         if (processor_alias_table[i].flags & PTA_SSE3
1895             && !(target_flags_explicit & MASK_SSE3))
1896           target_flags |= MASK_SSE3;
1897         if (processor_alias_table[i].flags & PTA_SSSE3
1898             && !(target_flags_explicit & MASK_SSSE3))
1899           target_flags |= MASK_SSSE3;
1900         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1901           x86_prefetch_sse = true;
1902         if (processor_alias_table[i].flags & PTA_CX16)
1903           x86_cmpxchg16b = true;
1904         if (processor_alias_table[i].flags & PTA_POPCNT
1905             && !(target_flags_explicit & MASK_POPCNT))
1906           target_flags |= MASK_POPCNT;
1907         if (processor_alias_table[i].flags & PTA_ABM
1908             && !(target_flags_explicit & MASK_ABM))
1909           target_flags |= MASK_ABM;
1910         if (processor_alias_table[i].flags & PTA_SSE4A
1911             && !(target_flags_explicit & MASK_SSE4A))
1912           target_flags |= MASK_SSE4A;
1913         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF)))
1914           x86_sahf = 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   ix86_arch_mask = 1u << ix86_arch;
1925   for (i = 0; i < X86_ARCH_LAST; ++i)
1926     ix86_arch_features[i] &= ix86_arch_mask;
1927
1928   for (i = 0; i < pta_size; i++)
1929     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1930       {
1931         ix86_tune = processor_alias_table[i].processor;
1932         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1933           {
1934             if (ix86_tune_defaulted)
1935               {
1936                 ix86_tune_string = "x86-64";
1937                 for (i = 0; i < pta_size; i++)
1938                   if (! strcmp (ix86_tune_string,
1939                                 processor_alias_table[i].name))
1940                     break;
1941                 ix86_tune = processor_alias_table[i].processor;
1942               }
1943             else
1944               error ("CPU you selected does not support x86-64 "
1945                      "instruction set");
1946           }
1947         /* Intel CPUs have always interpreted SSE prefetch instructions as
1948            NOPs; so, we can enable SSE prefetch instructions even when
1949            -mtune (rather than -march) points us to a processor that has them.
1950            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1951            higher processors.  */
1952         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1953           x86_prefetch_sse = true;
1954         break;
1955       }
1956   if (i == pta_size)
1957     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1958
1959   ix86_tune_mask = 1u << ix86_tune;
1960   for (i = 0; i < X86_TUNE_LAST; ++i)
1961     ix86_tune_features[i] &= ix86_tune_mask;
1962
1963   if (optimize_size)
1964     ix86_cost = &size_cost;
1965   else
1966     ix86_cost = processor_target_table[ix86_tune].cost;
1967   target_flags |= processor_target_table[ix86_tune].target_enable;
1968   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1969
1970   /* Arrange to set up i386_stack_locals for all functions.  */
1971   init_machine_status = ix86_init_machine_status;
1972
1973   /* Validate -mregparm= value.  */
1974   if (ix86_regparm_string)
1975     {
1976       i = atoi (ix86_regparm_string);
1977       if (i < 0 || i > REGPARM_MAX)
1978         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1979       else
1980         ix86_regparm = i;
1981     }
1982   else
1983    if (TARGET_64BIT)
1984      ix86_regparm = REGPARM_MAX;
1985
1986   /* If the user has provided any of the -malign-* options,
1987      warn and use that value only if -falign-* is not set.
1988      Remove this code in GCC 3.2 or later.  */
1989   if (ix86_align_loops_string)
1990     {
1991       warning (0, "-malign-loops is obsolete, use -falign-loops");
1992       if (align_loops == 0)
1993         {
1994           i = atoi (ix86_align_loops_string);
1995           if (i < 0 || i > MAX_CODE_ALIGN)
1996             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1997           else
1998             align_loops = 1 << i;
1999         }
2000     }
2001
2002   if (ix86_align_jumps_string)
2003     {
2004       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
2005       if (align_jumps == 0)
2006         {
2007           i = atoi (ix86_align_jumps_string);
2008           if (i < 0 || i > MAX_CODE_ALIGN)
2009             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2010           else
2011             align_jumps = 1 << i;
2012         }
2013     }
2014
2015   if (ix86_align_funcs_string)
2016     {
2017       warning (0, "-malign-functions is obsolete, use -falign-functions");
2018       if (align_functions == 0)
2019         {
2020           i = atoi (ix86_align_funcs_string);
2021           if (i < 0 || i > MAX_CODE_ALIGN)
2022             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2023           else
2024             align_functions = 1 << i;
2025         }
2026     }
2027
2028   /* Default align_* from the processor table.  */
2029   if (align_loops == 0)
2030     {
2031       align_loops = processor_target_table[ix86_tune].align_loop;
2032       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
2033     }
2034   if (align_jumps == 0)
2035     {
2036       align_jumps = processor_target_table[ix86_tune].align_jump;
2037       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
2038     }
2039   if (align_functions == 0)
2040     {
2041       align_functions = processor_target_table[ix86_tune].align_func;
2042     }
2043
2044   /* Validate -mbranch-cost= value, or provide default.  */
2045   ix86_branch_cost = ix86_cost->branch_cost;
2046   if (ix86_branch_cost_string)
2047     {
2048       i = atoi (ix86_branch_cost_string);
2049       if (i < 0 || i > 5)
2050         error ("-mbranch-cost=%d is not between 0 and 5", i);
2051       else
2052         ix86_branch_cost = i;
2053     }
2054   if (ix86_section_threshold_string)
2055     {
2056       i = atoi (ix86_section_threshold_string);
2057       if (i < 0)
2058         error ("-mlarge-data-threshold=%d is negative", i);
2059       else
2060         ix86_section_threshold = i;
2061     }
2062
2063   if (ix86_tls_dialect_string)
2064     {
2065       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
2066         ix86_tls_dialect = TLS_DIALECT_GNU;
2067       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
2068         ix86_tls_dialect = TLS_DIALECT_GNU2;
2069       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
2070         ix86_tls_dialect = TLS_DIALECT_SUN;
2071       else
2072         error ("bad value (%s) for -mtls-dialect= switch",
2073                ix86_tls_dialect_string);
2074     }
2075
2076   /* Keep nonleaf frame pointers.  */
2077   if (flag_omit_frame_pointer)
2078     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2079   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
2080     flag_omit_frame_pointer = 1;
2081
2082   /* If we're doing fast math, we don't care about comparison order
2083      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2084   if (flag_finite_math_only)
2085     target_flags &= ~MASK_IEEE_FP;
2086
2087   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2088      since the insns won't need emulation.  */
2089   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
2090     target_flags &= ~MASK_NO_FANCY_MATH_387;
2091
2092   /* Likewise, if the target doesn't have a 387, or we've specified
2093      software floating point, don't use 387 inline intrinsics.  */
2094   if (!TARGET_80387)
2095     target_flags |= MASK_NO_FANCY_MATH_387;
2096
2097   /* Turn on SSE3 builtins for -mssse3.  */
2098   if (TARGET_SSSE3)
2099     target_flags |= MASK_SSE3;
2100
2101   /* Turn on SSE3 builtins for -msse4a.  */
2102   if (TARGET_SSE4A)
2103     target_flags |= MASK_SSE3;
2104
2105   /* Turn on SSE2 builtins for -msse3.  */
2106   if (TARGET_SSE3)
2107     target_flags |= MASK_SSE2;
2108
2109   /* Turn on SSE builtins for -msse2.  */
2110   if (TARGET_SSE2)
2111     target_flags |= MASK_SSE;
2112
2113   /* Turn on MMX builtins for -msse.  */
2114   if (TARGET_SSE)
2115     {
2116       target_flags |= MASK_MMX & ~target_flags_explicit;
2117       x86_prefetch_sse = true;
2118     }
2119
2120   /* Turn on MMX builtins for 3Dnow.  */
2121   if (TARGET_3DNOW)
2122     target_flags |= MASK_MMX;
2123
2124   /* Turn on POPCNT builtins for -mabm.  */
2125   if (TARGET_ABM)
2126     target_flags |= MASK_POPCNT;
2127
2128   if (TARGET_64BIT)
2129     {
2130       if (TARGET_ALIGN_DOUBLE)
2131         error ("-malign-double makes no sense in the 64bit mode");
2132       if (TARGET_RTD)
2133         error ("-mrtd calling convention not supported in the 64bit mode");
2134
2135       /* Enable by default the SSE and MMX builtins.  Do allow the user to
2136          explicitly disable any of these.  In particular, disabling SSE and
2137          MMX for kernel code is extremely useful.  */
2138       target_flags
2139         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
2140             & ~target_flags_explicit);
2141      }
2142   else
2143     {
2144       /* i386 ABI does not specify red zone.  It still makes sense to use it
2145          when programmer takes care to stack from being destroyed.  */
2146       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
2147         target_flags |= MASK_NO_RED_ZONE;
2148     }
2149
2150   /* Validate -mpreferred-stack-boundary= value, or provide default.
2151      The default of 128 bits is for Pentium III's SSE __m128.  We can't
2152      change it because of optimize_size.  Otherwise, we can't mix object
2153      files compiled with -Os and -On.  */
2154   ix86_preferred_stack_boundary = 128;
2155   if (ix86_preferred_stack_boundary_string)
2156     {
2157       i = atoi (ix86_preferred_stack_boundary_string);
2158       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
2159         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
2160                TARGET_64BIT ? 4 : 2);
2161       else
2162         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
2163     }
2164
2165   /* Accept -msseregparm only if at least SSE support is enabled.  */
2166   if (TARGET_SSEREGPARM
2167       && ! TARGET_SSE)
2168     error ("-msseregparm used without SSE enabled");
2169
2170   ix86_fpmath = TARGET_FPMATH_DEFAULT;
2171   if (ix86_fpmath_string != 0)
2172     {
2173       if (! strcmp (ix86_fpmath_string, "387"))
2174         ix86_fpmath = FPMATH_387;
2175       else if (! strcmp (ix86_fpmath_string, "sse"))
2176         {
2177           if (!TARGET_SSE)
2178             {
2179               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2180               ix86_fpmath = FPMATH_387;
2181             }
2182           else
2183             ix86_fpmath = FPMATH_SSE;
2184         }
2185       else if (! strcmp (ix86_fpmath_string, "387,sse")
2186                || ! strcmp (ix86_fpmath_string, "sse,387"))
2187         {
2188           if (!TARGET_SSE)
2189             {
2190               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2191               ix86_fpmath = FPMATH_387;
2192             }
2193           else if (!TARGET_80387)
2194             {
2195               warning (0, "387 instruction set disabled, using SSE arithmetics");
2196               ix86_fpmath = FPMATH_SSE;
2197             }
2198           else
2199             ix86_fpmath = FPMATH_SSE | FPMATH_387;
2200         }
2201       else
2202         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2203     }
2204
2205   /* If the i387 is disabled, then do not return values in it. */
2206   if (!TARGET_80387)
2207     target_flags &= ~MASK_FLOAT_RETURNS;
2208
2209   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
2210       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2211       && !optimize_size)
2212     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2213
2214   /* ??? Unwind info is not correct around the CFG unless either a frame
2215      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2216      unwind info generation to be aware of the CFG and propagating states
2217      around edges.  */
2218   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2219        || flag_exceptions || flag_non_call_exceptions)
2220       && flag_omit_frame_pointer
2221       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2222     {
2223       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2224         warning (0, "unwind tables currently require either a frame pointer "
2225                  "or -maccumulate-outgoing-args for correctness");
2226       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2227     }
2228
2229   /* For sane SSE instruction set generation we need fcomi instruction.
2230      It is safe to enable all CMOVE instructions.  */
2231   if (TARGET_SSE)
2232     TARGET_CMOVE = 1;
2233
2234   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2235   {
2236     char *p;
2237     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2238     p = strchr (internal_label_prefix, 'X');
2239     internal_label_prefix_len = p - internal_label_prefix;
2240     *p = '\0';
2241   }
2242
2243   /* When scheduling description is not available, disable scheduler pass
2244      so it won't slow down the compilation and make x87 code slower.  */
2245   if (!TARGET_SCHEDULE)
2246     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2247
2248   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
2249     set_param_value ("simultaneous-prefetches",
2250                      ix86_cost->simultaneous_prefetches);
2251   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
2252     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
2253 }
2254 \f
2255 /* Return true if this goes in large data/bss.  */
2256
2257 static bool
2258 ix86_in_large_data_p (tree exp)
2259 {
2260   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
2261     return false;
2262
2263   /* Functions are never large data.  */
2264   if (TREE_CODE (exp) == FUNCTION_DECL)
2265     return false;
2266
2267   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
2268     {
2269       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
2270       if (strcmp (section, ".ldata") == 0
2271           || strcmp (section, ".lbss") == 0)
2272         return true;
2273       return false;
2274     }
2275   else
2276     {
2277       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2278
2279       /* If this is an incomplete type with size 0, then we can't put it
2280          in data because it might be too big when completed.  */
2281       if (!size || size > ix86_section_threshold)
2282         return true;
2283     }
2284
2285   return false;
2286 }
2287
2288 /* Switch to the appropriate section for output of DECL.
2289    DECL is either a `VAR_DECL' node or a constant of some sort.
2290    RELOC indicates whether forming the initial value of DECL requires
2291    link-time relocations.  */
2292
2293 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
2294         ATTRIBUTE_UNUSED;
2295
2296 static section *
2297 x86_64_elf_select_section (tree decl, int reloc,
2298                            unsigned HOST_WIDE_INT align)
2299 {
2300   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2301       && ix86_in_large_data_p (decl))
2302     {
2303       const char *sname = NULL;
2304       unsigned int flags = SECTION_WRITE;
2305       switch (categorize_decl_for_section (decl, reloc))
2306         {
2307         case SECCAT_DATA:
2308           sname = ".ldata";
2309           break;
2310         case SECCAT_DATA_REL:
2311           sname = ".ldata.rel";
2312           break;
2313         case SECCAT_DATA_REL_LOCAL:
2314           sname = ".ldata.rel.local";
2315           break;
2316         case SECCAT_DATA_REL_RO:
2317           sname = ".ldata.rel.ro";
2318           break;
2319         case SECCAT_DATA_REL_RO_LOCAL:
2320           sname = ".ldata.rel.ro.local";
2321           break;
2322         case SECCAT_BSS:
2323           sname = ".lbss";
2324           flags |= SECTION_BSS;
2325           break;
2326         case SECCAT_RODATA:
2327         case SECCAT_RODATA_MERGE_STR:
2328         case SECCAT_RODATA_MERGE_STR_INIT:
2329         case SECCAT_RODATA_MERGE_CONST:
2330           sname = ".lrodata";
2331           flags = 0;
2332           break;
2333         case SECCAT_SRODATA:
2334         case SECCAT_SDATA:
2335         case SECCAT_SBSS:
2336           gcc_unreachable ();
2337         case SECCAT_TEXT:
2338         case SECCAT_TDATA:
2339         case SECCAT_TBSS:
2340           /* We don't split these for medium model.  Place them into
2341              default sections and hope for best.  */
2342           break;
2343         }
2344       if (sname)
2345         {
2346           /* We might get called with string constants, but get_named_section
2347              doesn't like them as they are not DECLs.  Also, we need to set
2348              flags in that case.  */
2349           if (!DECL_P (decl))
2350             return get_section (sname, flags, NULL);
2351           return get_named_section (decl, sname, reloc);
2352         }
2353     }
2354   return default_elf_select_section (decl, reloc, align);
2355 }
2356
2357 /* Build up a unique section name, expressed as a
2358    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2359    RELOC indicates whether the initial value of EXP requires
2360    link-time relocations.  */
2361
2362 static void ATTRIBUTE_UNUSED
2363 x86_64_elf_unique_section (tree decl, int reloc)
2364 {
2365   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2366       && ix86_in_large_data_p (decl))
2367     {
2368       const char *prefix = NULL;
2369       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2370       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2371
2372       switch (categorize_decl_for_section (decl, reloc))
2373         {
2374         case SECCAT_DATA:
2375         case SECCAT_DATA_REL:
2376         case SECCAT_DATA_REL_LOCAL:
2377         case SECCAT_DATA_REL_RO:
2378         case SECCAT_DATA_REL_RO_LOCAL:
2379           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2380           break;
2381         case SECCAT_BSS:
2382           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2383           break;
2384         case SECCAT_RODATA:
2385         case SECCAT_RODATA_MERGE_STR:
2386         case SECCAT_RODATA_MERGE_STR_INIT:
2387         case SECCAT_RODATA_MERGE_CONST:
2388           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2389           break;
2390         case SECCAT_SRODATA:
2391         case SECCAT_SDATA:
2392         case SECCAT_SBSS:
2393           gcc_unreachable ();
2394         case SECCAT_TEXT:
2395         case SECCAT_TDATA:
2396         case SECCAT_TBSS:
2397           /* We don't split these for medium model.  Place them into
2398              default sections and hope for best.  */
2399           break;
2400         }
2401       if (prefix)
2402         {
2403           const char *name;
2404           size_t nlen, plen;
2405           char *string;
2406           plen = strlen (prefix);
2407
2408           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2409           name = targetm.strip_name_encoding (name);
2410           nlen = strlen (name);
2411
2412           string = alloca (nlen + plen + 1);
2413           memcpy (string, prefix, plen);
2414           memcpy (string + plen, name, nlen + 1);
2415
2416           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2417           return;
2418         }
2419     }
2420   default_unique_section (decl, reloc);
2421 }
2422
2423 #ifdef COMMON_ASM_OP
2424 /* This says how to output assembler code to declare an
2425    uninitialized external linkage data object.
2426
2427    For medium model x86-64 we need to use .largecomm opcode for
2428    large objects.  */
2429 void
2430 x86_elf_aligned_common (FILE *file,
2431                         const char *name, unsigned HOST_WIDE_INT size,
2432                         int align)
2433 {
2434   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2435       && size > (unsigned int)ix86_section_threshold)
2436     fprintf (file, ".largecomm\t");
2437   else
2438     fprintf (file, "%s", COMMON_ASM_OP);
2439   assemble_name (file, name);
2440   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2441            size, align / BITS_PER_UNIT);
2442 }
2443 #endif
2444
2445 /* Utility function for targets to use in implementing
2446    ASM_OUTPUT_ALIGNED_BSS.  */
2447
2448 void
2449 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2450                         const char *name, unsigned HOST_WIDE_INT size,
2451                         int align)
2452 {
2453   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2454       && size > (unsigned int)ix86_section_threshold)
2455     switch_to_section (get_named_section (decl, ".lbss", 0));
2456   else
2457     switch_to_section (bss_section);
2458   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2459 #ifdef ASM_DECLARE_OBJECT_NAME
2460   last_assemble_variable_decl = decl;
2461   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2462 #else
2463   /* Standard thing is just output label for the object.  */
2464   ASM_OUTPUT_LABEL (file, name);
2465 #endif /* ASM_DECLARE_OBJECT_NAME */
2466   ASM_OUTPUT_SKIP (file, size ? size : 1);
2467 }
2468 \f
2469 void
2470 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2471 {
2472   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2473      make the problem with not enough registers even worse.  */
2474 #ifdef INSN_SCHEDULING
2475   if (level > 1)
2476     flag_schedule_insns = 0;
2477 #endif
2478
2479   if (TARGET_MACHO)
2480     /* The Darwin libraries never set errno, so we might as well
2481        avoid calling them when that's the only reason we would.  */
2482     flag_errno_math = 0;
2483
2484   /* The default values of these switches depend on the TARGET_64BIT
2485      that is not known at this moment.  Mark these values with 2 and
2486      let user the to override these.  In case there is no command line option
2487      specifying them, we will set the defaults in override_options.  */
2488   if (optimize >= 1)
2489     flag_omit_frame_pointer = 2;
2490   flag_pcc_struct_return = 2;
2491   flag_asynchronous_unwind_tables = 2;
2492 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2493   SUBTARGET_OPTIMIZATION_OPTIONS;
2494 #endif
2495 }
2496 \f
2497 /* Decide whether we can make a sibling call to a function.  DECL is the
2498    declaration of the function being targeted by the call and EXP is the
2499    CALL_EXPR representing the call.  */
2500
2501 static bool
2502 ix86_function_ok_for_sibcall (tree decl, tree exp)
2503 {
2504   tree func;
2505   rtx a, b;
2506
2507   /* If we are generating position-independent code, we cannot sibcall
2508      optimize any indirect call, or a direct call to a global function,
2509      as the PLT requires %ebx be live.  */
2510   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2511     return false;
2512
2513   if (decl)
2514     func = decl;
2515   else
2516     {
2517       func = TREE_TYPE (CALL_EXPR_FN (exp));
2518       if (POINTER_TYPE_P (func))
2519         func = TREE_TYPE (func);
2520     }
2521
2522   /* Check that the return value locations are the same.  Like
2523      if we are returning floats on the 80387 register stack, we cannot
2524      make a sibcall from a function that doesn't return a float to a
2525      function that does or, conversely, from a function that does return
2526      a float to a function that doesn't; the necessary stack adjustment
2527      would not be executed.  This is also the place we notice
2528      differences in the return value ABI.  Note that it is ok for one
2529      of the functions to have void return type as long as the return
2530      value of the other is passed in a register.  */
2531   a = ix86_function_value (TREE_TYPE (exp), func, false);
2532   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2533                            cfun->decl, false);
2534   if (STACK_REG_P (a) || STACK_REG_P (b))
2535     {
2536       if (!rtx_equal_p (a, b))
2537         return false;
2538     }
2539   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2540     ;
2541   else if (!rtx_equal_p (a, b))
2542     return false;
2543
2544   /* If this call is indirect, we'll need to be able to use a call-clobbered
2545      register for the address of the target function.  Make sure that all
2546      such registers are not used for passing parameters.  */
2547   if (!decl && !TARGET_64BIT)
2548     {
2549       tree type;
2550
2551       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2552       type = CALL_EXPR_FN (exp);                /* pointer expression */
2553       type = TREE_TYPE (type);                  /* pointer type */
2554       type = TREE_TYPE (type);                  /* function type */
2555
2556       if (ix86_function_regparm (type, NULL) >= 3)
2557         {
2558           /* ??? Need to count the actual number of registers to be used,
2559              not the possible number of registers.  Fix later.  */
2560           return false;
2561         }
2562     }
2563
2564 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2565   /* Dllimport'd functions are also called indirectly.  */
2566   if (decl && DECL_DLLIMPORT_P (decl)
2567       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2568     return false;
2569 #endif
2570
2571   /* If we forced aligned the stack, then sibcalling would unalign the
2572      stack, which may break the called function.  */
2573   if (cfun->machine->force_align_arg_pointer)
2574     return false;
2575
2576   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2577   return true;
2578 }
2579
2580 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2581    calling convention attributes;
2582    arguments as in struct attribute_spec.handler.  */
2583
2584 static tree
2585 ix86_handle_cconv_attribute (tree *node, tree name,
2586                                    tree args,
2587                                    int flags ATTRIBUTE_UNUSED,
2588                                    bool *no_add_attrs)
2589 {
2590   if (TREE_CODE (*node) != FUNCTION_TYPE
2591       && TREE_CODE (*node) != METHOD_TYPE
2592       && TREE_CODE (*node) != FIELD_DECL
2593       && TREE_CODE (*node) != TYPE_DECL)
2594     {
2595       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2596                IDENTIFIER_POINTER (name));
2597       *no_add_attrs = true;
2598       return NULL_TREE;
2599     }
2600
2601   /* Can combine regparm with all attributes but fastcall.  */
2602   if (is_attribute_p ("regparm", name))
2603     {
2604       tree cst;
2605
2606       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2607         {
2608           error ("fastcall and regparm attributes are not compatible");
2609         }
2610
2611       cst = TREE_VALUE (args);
2612       if (TREE_CODE (cst) != INTEGER_CST)
2613         {
2614           warning (OPT_Wattributes,
2615                    "%qs attribute requires an integer constant argument",
2616                    IDENTIFIER_POINTER (name));
2617           *no_add_attrs = true;
2618         }
2619       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2620         {
2621           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2622                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2623           *no_add_attrs = true;
2624         }
2625
2626       if (!TARGET_64BIT
2627           && lookup_attribute (ix86_force_align_arg_pointer_string,
2628                                TYPE_ATTRIBUTES (*node))
2629           && compare_tree_int (cst, REGPARM_MAX-1))
2630         {
2631           error ("%s functions limited to %d register parameters",
2632                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2633         }
2634
2635       return NULL_TREE;
2636     }
2637
2638   if (TARGET_64BIT)
2639     {
2640       warning (OPT_Wattributes, "%qs attribute ignored",
2641                IDENTIFIER_POINTER (name));
2642       *no_add_attrs = true;
2643       return NULL_TREE;
2644     }
2645
2646   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2647   if (is_attribute_p ("fastcall", name))
2648     {
2649       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2650         {
2651           error ("fastcall and cdecl attributes are not compatible");
2652         }
2653       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2654         {
2655           error ("fastcall and stdcall attributes are not compatible");
2656         }
2657       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2658         {
2659           error ("fastcall and regparm attributes are not compatible");
2660         }
2661     }
2662
2663   /* Can combine stdcall with fastcall (redundant), regparm and
2664      sseregparm.  */
2665   else if (is_attribute_p ("stdcall", name))
2666     {
2667       if (lookup_attribute ("cdecl", 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 ("stdcall and fastcall attributes are not compatible");
2674         }
2675     }
2676
2677   /* Can combine cdecl with regparm and sseregparm.  */
2678   else if (is_attribute_p ("cdecl", name))
2679     {
2680       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2681         {
2682           error ("stdcall and cdecl attributes are not compatible");
2683         }
2684       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2685         {
2686           error ("fastcall and cdecl attributes are not compatible");
2687         }
2688     }
2689
2690   /* Can combine sseregparm with all attributes.  */
2691
2692   return NULL_TREE;
2693 }
2694
2695 /* Return 0 if the attributes for two types are incompatible, 1 if they
2696    are compatible, and 2 if they are nearly compatible (which causes a
2697    warning to be generated).  */
2698
2699 static int
2700 ix86_comp_type_attributes (tree type1, tree type2)
2701 {
2702   /* Check for mismatch of non-default calling convention.  */
2703   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2704
2705   if (TREE_CODE (type1) != FUNCTION_TYPE)
2706     return 1;
2707
2708   /* Check for mismatched fastcall/regparm types.  */
2709   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2710        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2711       || (ix86_function_regparm (type1, NULL)
2712           != ix86_function_regparm (type2, NULL)))
2713     return 0;
2714
2715   /* Check for mismatched sseregparm types.  */
2716   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2717       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2718     return 0;
2719
2720   /* Check for mismatched return types (cdecl vs stdcall).  */
2721   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2722       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2723     return 0;
2724
2725   return 1;
2726 }
2727 \f
2728 /* Return the regparm value for a function with the indicated TYPE and DECL.
2729    DECL may be NULL when calling function indirectly
2730    or considering a libcall.  */
2731
2732 static int
2733 ix86_function_regparm (tree type, tree decl)
2734 {
2735   tree attr;
2736   int regparm = ix86_regparm;
2737
2738   if (TARGET_64BIT)
2739     return regparm;
2740
2741   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2742   if (attr)
2743     return TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2744
2745   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2746     return 2;
2747
2748   /* Use register calling convention for local functions when possible.  */
2749   if (decl && flag_unit_at_a_time && !profile_flag)
2750     {
2751       struct cgraph_local_info *i = cgraph_local_info (decl);
2752       if (i && i->local)
2753         {
2754           int local_regparm, globals = 0, regno;
2755           struct function *f;
2756
2757           /* Make sure no regparm register is taken by a
2758              global register variable.  */
2759           for (local_regparm = 0; local_regparm < 3; local_regparm++)
2760             if (global_regs[local_regparm])
2761               break;
2762
2763           /* We can't use regparm(3) for nested functions as these use
2764              static chain pointer in third argument.  */
2765           if (local_regparm == 3
2766               && decl_function_context (decl)
2767               && !DECL_NO_STATIC_CHAIN (decl))
2768             local_regparm = 2;
2769
2770           /* If the function realigns its stackpointer, the prologue will
2771              clobber %ecx.  If we've already generated code for the callee,
2772              the callee DECL_STRUCT_FUNCTION is gone, so we fall back to
2773              scanning the attributes for the self-realigning property.  */
2774           f = DECL_STRUCT_FUNCTION (decl);
2775           if (local_regparm == 3
2776               && (f ? !!f->machine->force_align_arg_pointer
2777                   : !!lookup_attribute (ix86_force_align_arg_pointer_string,
2778                                         TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2779             local_regparm = 2;
2780
2781           /* Each global register variable increases register preassure,
2782              so the more global reg vars there are, the smaller regparm
2783              optimization use, unless requested by the user explicitly.  */
2784           for (regno = 0; regno < 6; regno++)
2785             if (global_regs[regno])
2786               globals++;
2787           local_regparm
2788             = globals < local_regparm ? local_regparm - globals : 0;
2789
2790           if (local_regparm > regparm)
2791             regparm = local_regparm;
2792         }
2793     }
2794
2795   return regparm;
2796 }
2797
2798 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
2799    DFmode (2) arguments in SSE registers for a function with the
2800    indicated TYPE and DECL.  DECL may be NULL when calling function
2801    indirectly or considering a libcall.  Otherwise return 0.  */
2802
2803 static int
2804 ix86_function_sseregparm (tree type, tree decl)
2805 {
2806   gcc_assert (!TARGET_64BIT);
2807
2808   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2809      by the sseregparm attribute.  */
2810   if (TARGET_SSEREGPARM
2811       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2812     {
2813       if (!TARGET_SSE)
2814         {
2815           if (decl)
2816             error ("Calling %qD with attribute sseregparm without "
2817                    "SSE/SSE2 enabled", decl);
2818           else
2819             error ("Calling %qT with attribute sseregparm without "
2820                    "SSE/SSE2 enabled", type);
2821           return 0;
2822         }
2823
2824       return 2;
2825     }
2826
2827   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
2828      (and DFmode for SSE2) arguments in SSE registers.  */
2829   if (decl && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2830     {
2831       struct cgraph_local_info *i = cgraph_local_info (decl);
2832       if (i && i->local)
2833         return TARGET_SSE2 ? 2 : 1;
2834     }
2835
2836   return 0;
2837 }
2838
2839 /* Return true if EAX is live at the start of the function.  Used by
2840    ix86_expand_prologue to determine if we need special help before
2841    calling allocate_stack_worker.  */
2842
2843 static bool
2844 ix86_eax_live_at_start_p (void)
2845 {
2846   /* Cheat.  Don't bother working forward from ix86_function_regparm
2847      to the function type to whether an actual argument is located in
2848      eax.  Instead just look at cfg info, which is still close enough
2849      to correct at this point.  This gives false positives for broken
2850      functions that might use uninitialized data that happens to be
2851      allocated in eax, but who cares?  */
2852   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2853 }
2854
2855 /* Return true if TYPE has a variable argument list.  */
2856
2857 static bool
2858 type_has_variadic_args_p (tree type)
2859 {
2860   tree t;
2861
2862   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
2863     if (t == void_list_node)
2864       return false;
2865   return true;
2866 }
2867
2868 /* Value is the number of bytes of arguments automatically
2869    popped when returning from a subroutine call.
2870    FUNDECL is the declaration node of the function (as a tree),
2871    FUNTYPE is the data type of the function (as a tree),
2872    or for a library call it is an identifier node for the subroutine name.
2873    SIZE is the number of bytes of arguments passed on the stack.
2874
2875    On the 80386, the RTD insn may be used to pop them if the number
2876      of args is fixed, but if the number is variable then the caller
2877      must pop them all.  RTD can't be used for library calls now
2878      because the library is compiled with the Unix compiler.
2879    Use of RTD is a selectable option, since it is incompatible with
2880    standard Unix calling sequences.  If the option is not selected,
2881    the caller must always pop the args.
2882
2883    The attribute stdcall is equivalent to RTD on a per module basis.  */
2884
2885 int
2886 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2887 {
2888   int rtd;
2889
2890   /* None of the 64-bit ABIs pop arguments.  */
2891   if (TARGET_64BIT)
2892     return 0;
2893
2894   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2895
2896   /* Cdecl functions override -mrtd, and never pop the stack.  */
2897   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
2898     {
2899       /* Stdcall and fastcall functions will pop the stack if not
2900          variable args.  */
2901       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2902           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2903         rtd = 1;
2904
2905       if (rtd && ! type_has_variadic_args_p (funtype))
2906         return size;
2907     }
2908
2909   /* Lose any fake structure return argument if it is passed on the stack.  */
2910   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2911       && !KEEP_AGGREGATE_RETURN_POINTER)
2912     {
2913       int nregs = ix86_function_regparm (funtype, fundecl);
2914       if (nregs == 0)
2915         return GET_MODE_SIZE (Pmode);
2916     }
2917
2918   return 0;
2919 }
2920 \f
2921 /* Argument support functions.  */
2922
2923 /* Return true when register may be used to pass function parameters.  */
2924 bool
2925 ix86_function_arg_regno_p (int regno)
2926 {
2927   int i;
2928
2929   if (!TARGET_64BIT)
2930     {
2931       if (TARGET_MACHO)
2932         return (regno < REGPARM_MAX
2933                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
2934       else
2935         return (regno < REGPARM_MAX
2936                 || (TARGET_MMX && MMX_REGNO_P (regno)
2937                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2938                 || (TARGET_SSE && SSE_REGNO_P (regno)
2939                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2940     }
2941
2942   if (TARGET_MACHO)
2943     {
2944       if (SSE_REGNO_P (regno) && TARGET_SSE)
2945         return true;
2946     }
2947   else
2948     {
2949       if (TARGET_SSE && SSE_REGNO_P (regno)
2950           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2951         return true;
2952     }
2953
2954   /* RAX is used as hidden argument to va_arg functions.  */
2955   if (regno == 0)
2956     return true;
2957
2958   for (i = 0; i < REGPARM_MAX; i++)
2959     if (regno == x86_64_int_parameter_registers[i])
2960       return true;
2961   return false;
2962 }
2963
2964 /* Return if we do not know how to pass TYPE solely in registers.  */
2965
2966 static bool
2967 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2968 {
2969   if (must_pass_in_stack_var_size_or_pad (mode, type))
2970     return true;
2971
2972   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2973      The layout_type routine is crafty and tries to trick us into passing
2974      currently unsupported vector types on the stack by using TImode.  */
2975   return (!TARGET_64BIT && mode == TImode
2976           && type && TREE_CODE (type) != VECTOR_TYPE);
2977 }
2978
2979 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2980    for a call to a function whose data type is FNTYPE.
2981    For a library call, FNTYPE is 0.  */
2982
2983 void
2984 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2985                       tree fntype,      /* tree ptr for function decl */
2986                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2987                       tree fndecl)
2988 {
2989   memset (cum, 0, sizeof (*cum));
2990
2991   /* Set up the number of registers to use for passing arguments.  */
2992   cum->nregs = ix86_regparm;
2993   if (TARGET_SSE)
2994     cum->sse_nregs = SSE_REGPARM_MAX;
2995   if (TARGET_MMX)
2996     cum->mmx_nregs = MMX_REGPARM_MAX;
2997   cum->warn_sse = true;
2998   cum->warn_mmx = true;
2999   cum->maybe_vaarg = (fntype ? type_has_variadic_args_p (fntype) : !libname);
3000
3001   if (!TARGET_64BIT)
3002     {
3003       /* If there are variable arguments, then we won't pass anything
3004          in registers in 32-bit mode. */
3005       if (cum->maybe_vaarg)
3006         {
3007           cum->nregs = 0;
3008           cum->sse_nregs = 0;
3009           cum->mmx_nregs = 0;
3010           cum->warn_sse = 0;
3011           cum->warn_mmx = 0;
3012           return;
3013         }
3014
3015       /* Use ecx and edx registers if function has fastcall attribute,
3016          else look for regparm information.  */
3017       if (fntype)
3018         {
3019           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
3020             {
3021               cum->nregs = 2;
3022               cum->fastcall = 1;
3023             }
3024           else
3025             cum->nregs = ix86_function_regparm (fntype, fndecl);
3026         }
3027
3028       /* Set up the number of SSE registers used for passing SFmode
3029          and DFmode arguments.  Warn for mismatching ABI.  */
3030       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
3031     }
3032 }
3033
3034 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
3035    But in the case of vector types, it is some vector mode.
3036
3037    When we have only some of our vector isa extensions enabled, then there
3038    are some modes for which vector_mode_supported_p is false.  For these
3039    modes, the generic vector support in gcc will choose some non-vector mode
3040    in order to implement the type.  By computing the natural mode, we'll
3041    select the proper ABI location for the operand and not depend on whatever
3042    the middle-end decides to do with these vector types.  */
3043
3044 static enum machine_mode
3045 type_natural_mode (tree type)
3046 {
3047   enum machine_mode mode = TYPE_MODE (type);
3048
3049   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
3050     {
3051       HOST_WIDE_INT size = int_size_in_bytes (type);
3052       if ((size == 8 || size == 16)
3053           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
3054           && TYPE_VECTOR_SUBPARTS (type) > 1)
3055         {
3056           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
3057
3058           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
3059             mode = MIN_MODE_VECTOR_FLOAT;
3060           else
3061             mode = MIN_MODE_VECTOR_INT;
3062
3063           /* Get the mode which has this inner mode and number of units.  */
3064           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3065             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
3066                 && GET_MODE_INNER (mode) == innermode)
3067               return mode;
3068
3069           gcc_unreachable ();
3070         }
3071     }
3072
3073   return mode;
3074 }
3075
3076 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
3077    this may not agree with the mode that the type system has chosen for the
3078    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
3079    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
3080
3081 static rtx
3082 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
3083                      unsigned int regno)
3084 {
3085   rtx tmp;
3086
3087   if (orig_mode != BLKmode)
3088     tmp = gen_rtx_REG (orig_mode, regno);
3089   else
3090     {
3091       tmp = gen_rtx_REG (mode, regno);
3092       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
3093       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
3094     }
3095
3096   return tmp;
3097 }
3098
3099 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
3100    of this code is to classify each 8bytes of incoming argument by the register
3101    class and assign registers accordingly.  */
3102
3103 /* Return the union class of CLASS1 and CLASS2.
3104    See the x86-64 PS ABI for details.  */
3105
3106 static enum x86_64_reg_class
3107 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
3108 {
3109   /* Rule #1: If both classes are equal, this is the resulting class.  */
3110   if (class1 == class2)
3111     return class1;
3112
3113   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
3114      the other class.  */
3115   if (class1 == X86_64_NO_CLASS)
3116     return class2;
3117   if (class2 == X86_64_NO_CLASS)
3118     return class1;
3119
3120   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
3121   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
3122     return X86_64_MEMORY_CLASS;
3123
3124   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
3125   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
3126       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
3127     return X86_64_INTEGERSI_CLASS;
3128   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
3129       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
3130     return X86_64_INTEGER_CLASS;
3131
3132   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
3133      MEMORY is used.  */
3134   if (class1 == X86_64_X87_CLASS
3135       || class1 == X86_64_X87UP_CLASS
3136       || class1 == X86_64_COMPLEX_X87_CLASS
3137       || class2 == X86_64_X87_CLASS
3138       || class2 == X86_64_X87UP_CLASS
3139       || class2 == X86_64_COMPLEX_X87_CLASS)
3140     return X86_64_MEMORY_CLASS;
3141
3142   /* Rule #6: Otherwise class SSE is used.  */
3143   return X86_64_SSE_CLASS;
3144 }
3145
3146 /* Classify the argument of type TYPE and mode MODE.
3147    CLASSES will be filled by the register class used to pass each word
3148    of the operand.  The number of words is returned.  In case the parameter
3149    should be passed in memory, 0 is returned. As a special case for zero
3150    sized containers, classes[0] will be NO_CLASS and 1 is returned.
3151
3152    BIT_OFFSET is used internally for handling records and specifies offset
3153    of the offset in bits modulo 256 to avoid overflow cases.
3154
3155    See the x86-64 PS ABI for details.
3156 */
3157
3158 static int
3159 classify_argument (enum machine_mode mode, tree type,
3160                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
3161 {
3162   HOST_WIDE_INT bytes =
3163     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3164   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3165
3166   /* Variable sized entities are always passed/returned in memory.  */
3167   if (bytes < 0)
3168     return 0;
3169
3170   if (mode != VOIDmode
3171       && targetm.calls.must_pass_in_stack (mode, type))
3172     return 0;
3173
3174   if (type && AGGREGATE_TYPE_P (type))
3175     {
3176       int i;
3177       tree field;
3178       enum x86_64_reg_class subclasses[MAX_CLASSES];
3179
3180       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3181       if (bytes > 16)
3182         return 0;
3183
3184       for (i = 0; i < words; i++)
3185         classes[i] = X86_64_NO_CLASS;
3186
3187       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3188          signalize memory class, so handle it as special case.  */
3189       if (!words)
3190         {
3191           classes[0] = X86_64_NO_CLASS;
3192           return 1;
3193         }
3194
3195       /* Classify each field of record and merge classes.  */
3196       switch (TREE_CODE (type))
3197         {
3198         case RECORD_TYPE:
3199           /* And now merge the fields of structure.  */
3200           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3201             {
3202               if (TREE_CODE (field) == FIELD_DECL)
3203                 {
3204                   int num;
3205
3206                   if (TREE_TYPE (field) == error_mark_node)
3207                     continue;
3208
3209                   /* Bitfields are always classified as integer.  Handle them
3210                      early, since later code would consider them to be
3211                      misaligned integers.  */
3212                   if (DECL_BIT_FIELD (field))
3213                     {
3214                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3215                            i < ((int_bit_position (field) + (bit_offset % 64))
3216                                 + tree_low_cst (DECL_SIZE (field), 0)
3217                                 + 63) / 8 / 8; i++)
3218                         classes[i] =
3219                           merge_classes (X86_64_INTEGER_CLASS,
3220                                          classes[i]);
3221                     }
3222                   else
3223                     {
3224                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3225                                                TREE_TYPE (field), subclasses,
3226                                                (int_bit_position (field)
3227                                                 + bit_offset) % 256);
3228                       if (!num)
3229                         return 0;
3230                       for (i = 0; i < num; i++)
3231                         {
3232                           int pos =
3233                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3234                           classes[i + pos] =
3235                             merge_classes (subclasses[i], classes[i + pos]);
3236                         }
3237                     }
3238                 }
3239             }
3240           break;
3241
3242         case ARRAY_TYPE:
3243           /* Arrays are handled as small records.  */
3244           {
3245             int num;
3246             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3247                                      TREE_TYPE (type), subclasses, bit_offset);
3248             if (!num)
3249               return 0;
3250
3251             /* The partial classes are now full classes.  */
3252             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3253               subclasses[0] = X86_64_SSE_CLASS;
3254             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3255               subclasses[0] = X86_64_INTEGER_CLASS;
3256
3257             for (i = 0; i < words; i++)
3258               classes[i] = subclasses[i % num];
3259
3260             break;
3261           }
3262         case UNION_TYPE:
3263         case QUAL_UNION_TYPE:
3264           /* Unions are similar to RECORD_TYPE but offset is always 0.
3265              */
3266           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3267             {
3268               if (TREE_CODE (field) == FIELD_DECL)
3269                 {
3270                   int num;
3271
3272                   if (TREE_TYPE (field) == error_mark_node)
3273                     continue;
3274
3275                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3276                                            TREE_TYPE (field), subclasses,
3277                                            bit_offset);
3278                   if (!num)
3279                     return 0;
3280                   for (i = 0; i < num; i++)
3281                     classes[i] = merge_classes (subclasses[i], classes[i]);
3282                 }
3283             }
3284           break;
3285
3286         default:
3287           gcc_unreachable ();
3288         }
3289
3290       /* Final merger cleanup.  */
3291       for (i = 0; i < words; i++)
3292         {
3293           /* If one class is MEMORY, everything should be passed in
3294              memory.  */
3295           if (classes[i] == X86_64_MEMORY_CLASS)
3296             return 0;
3297
3298           /* The X86_64_SSEUP_CLASS should be always preceded by
3299              X86_64_SSE_CLASS.  */
3300           if (classes[i] == X86_64_SSEUP_CLASS
3301               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3302             classes[i] = X86_64_SSE_CLASS;
3303
3304           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3305           if (classes[i] == X86_64_X87UP_CLASS
3306               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3307             classes[i] = X86_64_SSE_CLASS;
3308         }
3309       return words;
3310     }
3311
3312   /* Compute alignment needed.  We align all types to natural boundaries with
3313      exception of XFmode that is aligned to 64bits.  */
3314   if (mode != VOIDmode && mode != BLKmode)
3315     {
3316       int mode_alignment = GET_MODE_BITSIZE (mode);
3317
3318       if (mode == XFmode)
3319         mode_alignment = 128;
3320       else if (mode == XCmode)
3321         mode_alignment = 256;
3322       if (COMPLEX_MODE_P (mode))
3323         mode_alignment /= 2;
3324       /* Misaligned fields are always returned in memory.  */
3325       if (bit_offset % mode_alignment)
3326         return 0;
3327     }
3328
3329   /* for V1xx modes, just use the base mode */
3330   if (VECTOR_MODE_P (mode)
3331       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3332     mode = GET_MODE_INNER (mode);
3333
3334   /* Classification of atomic types.  */
3335   switch (mode)
3336     {
3337     case SDmode:
3338     case DDmode:
3339       classes[0] = X86_64_SSE_CLASS;
3340       return 1;
3341     case TDmode:
3342       classes[0] = X86_64_SSE_CLASS;
3343       classes[1] = X86_64_SSEUP_CLASS;
3344       return 2;
3345     case DImode:
3346     case SImode:
3347     case HImode:
3348     case QImode:
3349     case CSImode:
3350     case CHImode:
3351     case CQImode:
3352       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3353         classes[0] = X86_64_INTEGERSI_CLASS;
3354       else
3355         classes[0] = X86_64_INTEGER_CLASS;
3356       return 1;
3357     case CDImode:
3358     case TImode:
3359       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3360       return 2;
3361     case CTImode:
3362       return 0;
3363     case SFmode:
3364       if (!(bit_offset % 64))
3365         classes[0] = X86_64_SSESF_CLASS;
3366       else
3367         classes[0] = X86_64_SSE_CLASS;
3368       return 1;
3369     case DFmode:
3370       classes[0] = X86_64_SSEDF_CLASS;
3371       return 1;
3372     case XFmode:
3373       classes[0] = X86_64_X87_CLASS;
3374       classes[1] = X86_64_X87UP_CLASS;
3375       return 2;
3376     case TFmode:
3377       classes[0] = X86_64_SSE_CLASS;
3378       classes[1] = X86_64_SSEUP_CLASS;
3379       return 2;
3380     case SCmode:
3381       classes[0] = X86_64_SSE_CLASS;
3382       return 1;
3383     case DCmode:
3384       classes[0] = X86_64_SSEDF_CLASS;
3385       classes[1] = X86_64_SSEDF_CLASS;
3386       return 2;
3387     case XCmode:
3388       classes[0] = X86_64_COMPLEX_X87_CLASS;
3389       return 1;
3390     case TCmode:
3391       /* This modes is larger than 16 bytes.  */
3392       return 0;
3393     case V4SFmode:
3394     case V4SImode:
3395     case V16QImode:
3396     case V8HImode:
3397     case V2DFmode:
3398     case V2DImode:
3399       classes[0] = X86_64_SSE_CLASS;
3400       classes[1] = X86_64_SSEUP_CLASS;
3401       return 2;
3402     case V2SFmode:
3403     case V2SImode:
3404     case V4HImode:
3405     case V8QImode:
3406       classes[0] = X86_64_SSE_CLASS;
3407       return 1;
3408     case BLKmode:
3409     case VOIDmode:
3410       return 0;
3411     default:
3412       gcc_assert (VECTOR_MODE_P (mode));
3413
3414       if (bytes > 16)
3415         return 0;
3416
3417       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3418
3419       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3420         classes[0] = X86_64_INTEGERSI_CLASS;
3421       else
3422         classes[0] = X86_64_INTEGER_CLASS;
3423       classes[1] = X86_64_INTEGER_CLASS;
3424       return 1 + (bytes > 8);
3425     }
3426 }
3427
3428 /* Examine the argument and return set number of register required in each
3429    class.  Return 0 iff parameter should be passed in memory.  */
3430 static int
3431 examine_argument (enum machine_mode mode, tree type, int in_return,
3432                   int *int_nregs, int *sse_nregs)
3433 {
3434   enum x86_64_reg_class class[MAX_CLASSES];
3435   int n = classify_argument (mode, type, class, 0);
3436
3437   *int_nregs = 0;
3438   *sse_nregs = 0;
3439   if (!n)
3440     return 0;
3441   for (n--; n >= 0; n--)
3442     switch (class[n])
3443       {
3444       case X86_64_INTEGER_CLASS:
3445       case X86_64_INTEGERSI_CLASS:
3446         (*int_nregs)++;
3447         break;
3448       case X86_64_SSE_CLASS:
3449       case X86_64_SSESF_CLASS:
3450       case X86_64_SSEDF_CLASS:
3451         (*sse_nregs)++;
3452         break;
3453       case X86_64_NO_CLASS:
3454       case X86_64_SSEUP_CLASS:
3455         break;
3456       case X86_64_X87_CLASS:
3457       case X86_64_X87UP_CLASS:
3458         if (!in_return)
3459           return 0;
3460         break;
3461       case X86_64_COMPLEX_X87_CLASS:
3462         return in_return ? 2 : 0;
3463       case X86_64_MEMORY_CLASS:
3464         gcc_unreachable ();
3465       }
3466   return 1;
3467 }
3468
3469 /* Construct container for the argument used by GCC interface.  See
3470    FUNCTION_ARG for the detailed description.  */
3471
3472 static rtx
3473 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3474                      tree type, int in_return, int nintregs, int nsseregs,
3475                      const int *intreg, int sse_regno)
3476 {
3477   /* The following variables hold the static issued_error state.  */
3478   static bool issued_sse_arg_error;
3479   static bool issued_sse_ret_error;
3480   static bool issued_x87_ret_error;
3481
3482   enum machine_mode tmpmode;
3483   int bytes =
3484     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3485   enum x86_64_reg_class class[MAX_CLASSES];
3486   int n;
3487   int i;
3488   int nexps = 0;
3489   int needed_sseregs, needed_intregs;
3490   rtx exp[MAX_CLASSES];
3491   rtx ret;
3492
3493   n = classify_argument (mode, type, class, 0);
3494   if (!n)
3495     return NULL;
3496   if (!examine_argument (mode, type, in_return, &needed_intregs,
3497                          &needed_sseregs))
3498     return NULL;
3499   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3500     return NULL;
3501
3502   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3503      some less clueful developer tries to use floating-point anyway.  */
3504   if (needed_sseregs && !TARGET_SSE)
3505     {
3506       if (in_return)
3507         {
3508           if (!issued_sse_ret_error)
3509             {
3510               error ("SSE register return with SSE disabled");
3511               issued_sse_ret_error = true;
3512             }
3513         }
3514       else if (!issued_sse_arg_error)
3515         {
3516           error ("SSE register argument with SSE disabled");
3517           issued_sse_arg_error = true;
3518         }
3519       return NULL;
3520     }
3521
3522   /* Likewise, error if the ABI requires us to return values in the
3523      x87 registers and the user specified -mno-80387.  */
3524   if (!TARGET_80387 && in_return)
3525     for (i = 0; i < n; i++)
3526       if (class[i] == X86_64_X87_CLASS
3527           || class[i] == X86_64_X87UP_CLASS
3528           || class[i] == X86_64_COMPLEX_X87_CLASS)
3529         {
3530           if (!issued_x87_ret_error)
3531             {
3532               error ("x87 register return with x87 disabled");
3533               issued_x87_ret_error = true;
3534             }
3535           return NULL;
3536         }
3537
3538   /* First construct simple cases.  Avoid SCmode, since we want to use
3539      single register to pass this type.  */
3540   if (n == 1 && mode != SCmode)
3541     switch (class[0])
3542       {
3543       case X86_64_INTEGER_CLASS:
3544       case X86_64_INTEGERSI_CLASS:
3545         return gen_rtx_REG (mode, intreg[0]);
3546       case X86_64_SSE_CLASS:
3547       case X86_64_SSESF_CLASS:
3548       case X86_64_SSEDF_CLASS:
3549         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3550       case X86_64_X87_CLASS:
3551       case X86_64_COMPLEX_X87_CLASS:
3552         return gen_rtx_REG (mode, FIRST_STACK_REG);
3553       case X86_64_NO_CLASS:
3554         /* Zero sized array, struct or class.  */
3555         return NULL;
3556       default:
3557         gcc_unreachable ();
3558       }
3559   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3560       && mode != BLKmode)
3561     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3562
3563   if (n == 2
3564       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3565     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3566   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3567       && class[1] == X86_64_INTEGER_CLASS
3568       && (mode == CDImode || mode == TImode || mode == TFmode)
3569       && intreg[0] + 1 == intreg[1])
3570     return gen_rtx_REG (mode, intreg[0]);
3571
3572   /* Otherwise figure out the entries of the PARALLEL.  */
3573   for (i = 0; i < n; i++)
3574     {
3575       switch (class[i])
3576         {
3577           case X86_64_NO_CLASS:
3578             break;
3579           case X86_64_INTEGER_CLASS:
3580           case X86_64_INTEGERSI_CLASS:
3581             /* Merge TImodes on aligned occasions here too.  */
3582             if (i * 8 + 8 > bytes)
3583               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3584             else if (class[i] == X86_64_INTEGERSI_CLASS)
3585               tmpmode = SImode;
3586             else
3587               tmpmode = DImode;
3588             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3589             if (tmpmode == BLKmode)
3590               tmpmode = DImode;
3591             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3592                                                gen_rtx_REG (tmpmode, *intreg),
3593                                                GEN_INT (i*8));
3594             intreg++;
3595             break;
3596           case X86_64_SSESF_CLASS:
3597             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3598                                                gen_rtx_REG (SFmode,
3599                                                             SSE_REGNO (sse_regno)),
3600                                                GEN_INT (i*8));
3601             sse_regno++;
3602             break;
3603           case X86_64_SSEDF_CLASS:
3604             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3605                                                gen_rtx_REG (DFmode,
3606                                                             SSE_REGNO (sse_regno)),
3607                                                GEN_INT (i*8));
3608             sse_regno++;
3609             break;
3610           case X86_64_SSE_CLASS:
3611             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3612               tmpmode = TImode;
3613             else
3614               tmpmode = DImode;
3615             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3616                                                gen_rtx_REG (tmpmode,
3617                                                             SSE_REGNO (sse_regno)),
3618                                                GEN_INT (i*8));
3619             if (tmpmode == TImode)
3620               i++;
3621             sse_regno++;
3622             break;
3623           default:
3624             gcc_unreachable ();
3625         }
3626     }
3627
3628   /* Empty aligned struct, union or class.  */
3629   if (nexps == 0)
3630     return NULL;
3631
3632   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3633   for (i = 0; i < nexps; i++)
3634     XVECEXP (ret, 0, i) = exp [i];
3635   return ret;
3636 }
3637
3638 /* Update the data in CUM to advance over an argument of mode MODE
3639    and data type TYPE.  (TYPE is null for libcalls where that information
3640    may not be available.)  */
3641
3642 static void
3643 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3644                          tree type, HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3645 {
3646   switch (mode)
3647     {
3648     default:
3649       break;
3650
3651     case BLKmode:
3652       if (bytes < 0)
3653         break;
3654       /* FALLTHRU */
3655
3656     case DImode:
3657     case SImode:
3658     case HImode:
3659     case QImode:
3660       cum->words += words;
3661       cum->nregs -= words;
3662       cum->regno += words;
3663
3664       if (cum->nregs <= 0)
3665         {
3666           cum->nregs = 0;
3667           cum->regno = 0;
3668         }
3669       break;
3670
3671     case DFmode:
3672       if (cum->float_in_sse < 2)
3673         break;
3674     case SFmode:
3675       if (cum->float_in_sse < 1)
3676         break;
3677       /* FALLTHRU */
3678
3679     case TImode:
3680     case V16QImode:
3681     case V8HImode:
3682     case V4SImode:
3683     case V2DImode:
3684     case V4SFmode:
3685     case V2DFmode:
3686       if (!type || !AGGREGATE_TYPE_P (type))
3687         {
3688           cum->sse_words += words;
3689           cum->sse_nregs -= 1;
3690           cum->sse_regno += 1;
3691           if (cum->sse_nregs <= 0)
3692             {
3693               cum->sse_nregs = 0;
3694               cum->sse_regno = 0;
3695             }
3696         }
3697       break;
3698
3699     case V8QImode:
3700     case V4HImode:
3701     case V2SImode:
3702     case V2SFmode:
3703       if (!type || !AGGREGATE_TYPE_P (type))
3704         {
3705           cum->mmx_words += words;
3706           cum->mmx_nregs -= 1;
3707           cum->mmx_regno += 1;
3708           if (cum->mmx_nregs <= 0)
3709             {
3710               cum->mmx_nregs = 0;
3711               cum->mmx_regno = 0;
3712             }
3713         }
3714       break;
3715     }
3716 }
3717
3718 static void
3719 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3720                          tree type, HOST_WIDE_INT words)
3721 {
3722   int int_nregs, sse_nregs;
3723
3724   if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3725     cum->words += words;
3726   else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3727     {
3728       cum->nregs -= int_nregs;
3729       cum->sse_nregs -= sse_nregs;
3730       cum->regno += int_nregs;
3731       cum->sse_regno += sse_nregs;
3732     }
3733   else
3734     cum->words += words;
3735 }
3736
3737 void
3738 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3739                       tree type, int named ATTRIBUTE_UNUSED)
3740 {
3741   HOST_WIDE_INT bytes, words;
3742
3743   if (mode == BLKmode)
3744     bytes = int_size_in_bytes (type);
3745   else
3746     bytes = GET_MODE_SIZE (mode);
3747   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3748
3749   if (type)
3750     mode = type_natural_mode (type);
3751
3752   if (TARGET_64BIT)
3753     function_arg_advance_64 (cum, mode, type, words);
3754   else
3755     function_arg_advance_32 (cum, mode, type, bytes, words);
3756 }
3757
3758 /* Define where to put the arguments to a function.
3759    Value is zero to push the argument on the stack,
3760    or a hard register in which to store the argument.
3761
3762    MODE is the argument's machine mode.
3763    TYPE is the data type of the argument (as a tree).
3764     This is null for libcalls where that information may
3765     not be available.
3766    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3767     the preceding args and about the function being called.
3768    NAMED is nonzero if this argument is a named parameter
3769     (otherwise it is an extra parameter matching an ellipsis).  */
3770
3771 static rtx
3772 function_arg_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3773                  enum machine_mode orig_mode, tree type,
3774                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3775 {
3776   static bool warnedsse, warnedmmx;
3777
3778   /* Avoid the AL settings for the Unix64 ABI.  */
3779   if (mode == VOIDmode)
3780     return constm1_rtx;
3781
3782   switch (mode)
3783     {
3784     default:
3785       break;
3786
3787     case BLKmode:
3788       if (bytes < 0)
3789         break;
3790       /* FALLTHRU */
3791     case DImode:
3792     case SImode:
3793     case HImode:
3794     case QImode:
3795       if (words <= cum->nregs)
3796         {
3797           int regno = cum->regno;
3798
3799           /* Fastcall allocates the first two DWORD (SImode) or
3800              smaller arguments to ECX and EDX.  */
3801           if (cum->fastcall)
3802             {
3803               if (mode == BLKmode || mode == DImode)
3804                 break;
3805
3806               /* ECX not EAX is the first allocated register.  */
3807               if (regno == 0)
3808                 regno = 2;
3809             }
3810           return gen_rtx_REG (mode, regno);
3811         }
3812       break;
3813
3814     case DFmode:
3815       if (cum->float_in_sse < 2)
3816         break;
3817     case SFmode:
3818       if (cum->float_in_sse < 1)
3819         break;
3820       /* FALLTHRU */
3821     case TImode:
3822     case V16QImode:
3823     case V8HImode:
3824     case V4SImode:
3825     case V2DImode:
3826     case V4SFmode:
3827     case V2DFmode:
3828       if (!type || !AGGREGATE_TYPE_P (type))
3829         {
3830           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3831             {
3832               warnedsse = true;
3833               warning (0, "SSE vector argument without SSE enabled "
3834                        "changes the ABI");
3835             }
3836           if (cum->sse_nregs)
3837             return gen_reg_or_parallel (mode, orig_mode,
3838                                         cum->sse_regno + FIRST_SSE_REG);
3839         }
3840       break;
3841
3842     case V8QImode:
3843     case V4HImode:
3844     case V2SImode:
3845     case V2SFmode:
3846       if (!type || !AGGREGATE_TYPE_P (type))
3847         {
3848           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3849             {
3850               warnedmmx = true;
3851               warning (0, "MMX vector argument without MMX enabled "
3852                        "changes the ABI");
3853             }
3854           if (cum->mmx_nregs)
3855             return gen_reg_or_parallel (mode, orig_mode,
3856                                         cum->mmx_regno + FIRST_MMX_REG);
3857         }
3858       break;
3859     }
3860
3861   return NULL_RTX;
3862 }
3863
3864 static rtx
3865 function_arg_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3866                  enum machine_mode orig_mode, tree type)
3867 {
3868   /* Handle a hidden AL argument containing number of registers
3869      for varargs x86-64 functions.  */
3870   if (mode == VOIDmode)
3871     return GEN_INT (cum->maybe_vaarg
3872                     ? (cum->sse_nregs < 0
3873                        ? SSE_REGPARM_MAX
3874                        : cum->sse_regno)
3875                     : -1);
3876
3877   return construct_container (mode, orig_mode, type, 0, cum->nregs,
3878                               cum->sse_nregs,
3879                               &x86_64_int_parameter_registers [cum->regno],
3880                               cum->sse_regno);
3881 }
3882
3883 rtx
3884 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
3885               tree type, int named ATTRIBUTE_UNUSED)
3886 {
3887   enum machine_mode mode = omode;
3888   HOST_WIDE_INT bytes, words;
3889
3890   if (mode == BLKmode)
3891     bytes = int_size_in_bytes (type);
3892   else
3893     bytes = GET_MODE_SIZE (mode);
3894   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3895
3896   /* To simplify the code below, represent vector types with a vector mode
3897      even if MMX/SSE are not active.  */
3898   if (type && TREE_CODE (type) == VECTOR_TYPE)
3899     mode = type_natural_mode (type);
3900
3901   if (TARGET_64BIT)
3902     return function_arg_64 (cum, mode, omode, type);
3903   else
3904     return function_arg_32 (cum, mode, omode, type, bytes, words);
3905 }
3906
3907 /* A C expression that indicates when an argument must be passed by
3908    reference.  If nonzero for an argument, a copy of that argument is
3909    made in memory and a pointer to the argument is passed instead of
3910    the argument itself.  The pointer is passed in whatever way is
3911    appropriate for passing a pointer to that type.  */
3912
3913 static bool
3914 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3915                         enum machine_mode mode ATTRIBUTE_UNUSED,
3916                         tree type, bool named ATTRIBUTE_UNUSED)
3917 {
3918   if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
3919     return 1;
3920
3921   return 0;
3922 }
3923
3924 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3925    ABI.  Only called if TARGET_SSE.  */
3926 static bool
3927 contains_128bit_aligned_vector_p (tree type)
3928 {
3929   enum machine_mode mode = TYPE_MODE (type);
3930   if (SSE_REG_MODE_P (mode)
3931       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3932     return true;
3933   if (TYPE_ALIGN (type) < 128)
3934     return false;
3935
3936   if (AGGREGATE_TYPE_P (type))
3937     {
3938       /* Walk the aggregates recursively.  */
3939       switch (TREE_CODE (type))
3940         {
3941         case RECORD_TYPE:
3942         case UNION_TYPE:
3943         case QUAL_UNION_TYPE:
3944           {
3945             tree field;
3946
3947             /* Walk all the structure fields.  */
3948             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3949               {
3950                 if (TREE_CODE (field) == FIELD_DECL
3951                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3952                   return true;
3953               }
3954             break;
3955           }
3956
3957         case ARRAY_TYPE:
3958           /* Just for use if some languages passes arrays by value.  */
3959           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3960             return true;
3961           break;
3962
3963         default:
3964           gcc_unreachable ();
3965         }
3966     }
3967   return false;
3968 }
3969
3970 /* Gives the alignment boundary, in bits, of an argument with the
3971    specified mode and type.  */
3972
3973 int
3974 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3975 {
3976   int align;
3977   if (type)
3978     align = TYPE_ALIGN (type);
3979   else
3980     align = GET_MODE_ALIGNMENT (mode);
3981   if (align < PARM_BOUNDARY)
3982     align = PARM_BOUNDARY;
3983   if (!TARGET_64BIT)
3984     {
3985       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3986          make an exception for SSE modes since these require 128bit
3987          alignment.
3988
3989          The handling here differs from field_alignment.  ICC aligns MMX
3990          arguments to 4 byte boundaries, while structure fields are aligned
3991          to 8 byte boundaries.  */
3992       if (!TARGET_SSE)
3993         align = PARM_BOUNDARY;
3994       else if (!type)
3995         {
3996           if (!SSE_REG_MODE_P (mode))
3997             align = PARM_BOUNDARY;
3998         }
3999       else
4000         {
4001           if (!contains_128bit_aligned_vector_p (type))
4002             align = PARM_BOUNDARY;
4003         }
4004     }
4005   if (align > 128)
4006     align = 128;
4007   return align;
4008 }
4009
4010 /* Return true if N is a possible register number of function value.  */
4011
4012 bool
4013 ix86_function_value_regno_p (int regno)
4014 {
4015   switch (regno)
4016     {
4017     case 0:
4018       return true;
4019
4020     case FIRST_FLOAT_REG:
4021       return TARGET_FLOAT_RETURNS_IN_80387;
4022
4023     case FIRST_SSE_REG:
4024       return TARGET_SSE;
4025
4026     case FIRST_MMX_REG:
4027       if (TARGET_MACHO || TARGET_64BIT)
4028         return false;
4029       return TARGET_MMX;
4030     }
4031
4032   return false;
4033 }
4034
4035 /* Define how to find the value returned by a function.
4036    VALTYPE is the data type of the value (as a tree).
4037    If the precise function being called is known, FUNC is its FUNCTION_DECL;
4038    otherwise, FUNC is 0.  */
4039
4040 static rtx
4041 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
4042                    tree fntype, tree fn)
4043 {
4044   unsigned int regno;
4045
4046   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4047      we normally prevent this case when mmx is not available.  However
4048      some ABIs may require the result to be returned like DImode.  */
4049   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4050     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
4051
4052   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4053      we prevent this case when sse is not available.  However some ABIs
4054      may require the result to be returned like integer TImode.  */
4055   else if (mode == TImode
4056            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4057     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
4058
4059   /* Decimal floating point values can go in %eax, unlike other float modes.  */
4060   else if (DECIMAL_FLOAT_MODE_P (mode))
4061     regno = 0;
4062
4063   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
4064   else if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
4065     regno = 0;
4066
4067   /* Floating point return values in %st(0), except for local functions when
4068      SSE math is enabled or for functions with sseregparm attribute.  */
4069   else
4070     {
4071       regno = FIRST_FLOAT_REG;
4072
4073       if ((fn || fntype) && (mode == SFmode || mode == DFmode))
4074         {
4075           int sse_level = ix86_function_sseregparm (fntype, fn);
4076           if ((sse_level >= 1 && mode == SFmode)
4077               || (sse_level == 2 && mode == DFmode))
4078             regno = FIRST_SSE_REG;
4079         }
4080     }
4081
4082   return gen_rtx_REG (orig_mode, regno);
4083 }
4084
4085 static rtx
4086 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
4087                    tree valtype)
4088 {
4089   rtx ret;
4090
4091   /* Handle libcalls, which don't provide a type node.  */
4092   if (valtype == NULL)
4093     {
4094       switch (mode)
4095         {
4096         case SFmode:
4097         case SCmode:
4098         case DFmode:
4099         case DCmode:
4100         case TFmode:
4101         case SDmode:
4102         case DDmode:
4103         case TDmode:
4104           return gen_rtx_REG (mode, FIRST_SSE_REG);
4105         case XFmode:
4106         case XCmode:
4107           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4108         case TCmode:
4109           return NULL;
4110         default:
4111           return gen_rtx_REG (mode, 0);
4112         }
4113     }
4114
4115   ret = construct_container (mode, orig_mode, valtype, 1,
4116                              REGPARM_MAX, SSE_REGPARM_MAX,
4117                              x86_64_int_return_registers, 0);
4118
4119   /* For zero sized structures, construct_container returns NULL, but we
4120      need to keep rest of compiler happy by returning meaningful value.  */
4121   if (!ret)
4122     ret = gen_rtx_REG (orig_mode, 0);
4123
4124   return ret;
4125 }
4126
4127 static rtx
4128 ix86_function_value_1 (tree valtype, tree fntype_or_decl,
4129                        enum machine_mode orig_mode, enum machine_mode mode)
4130 {
4131   tree fn, fntype;
4132
4133   fn = NULL_TREE;
4134   if (fntype_or_decl && DECL_P (fntype_or_decl))
4135     fn = fntype_or_decl;
4136   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
4137
4138   if (TARGET_64BIT)
4139     return function_value_64 (orig_mode, mode, valtype);
4140   else
4141     return function_value_32 (orig_mode, mode, fntype, fn);
4142 }
4143
4144 static rtx
4145 ix86_function_value (tree valtype, tree fntype_or_decl,
4146                      bool outgoing ATTRIBUTE_UNUSED)
4147 {
4148   enum machine_mode mode, orig_mode;
4149
4150   orig_mode = TYPE_MODE (valtype);
4151   mode = type_natural_mode (valtype);
4152   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
4153 }
4154
4155 rtx
4156 ix86_libcall_value (enum machine_mode mode)
4157 {
4158   return ix86_function_value_1 (NULL, NULL, mode, mode);
4159 }
4160
4161 /* Return true iff type is returned in memory.  */
4162
4163 static int
4164 return_in_memory_32 (tree type, enum machine_mode mode)
4165 {
4166   HOST_WIDE_INT size;
4167
4168   if (mode == BLKmode)
4169     return 1;
4170
4171   size = int_size_in_bytes (type);
4172
4173   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
4174     return 0;
4175
4176   if (VECTOR_MODE_P (mode) || mode == TImode)
4177     {
4178       /* User-created vectors small enough to fit in EAX.  */
4179       if (size < 8)
4180         return 0;
4181
4182       /* MMX/3dNow values are returned in MM0,
4183          except when it doesn't exits.  */
4184       if (size == 8)
4185         return (TARGET_MMX ? 0 : 1);
4186
4187       /* SSE values are returned in XMM0, except when it doesn't exist.  */
4188       if (size == 16)
4189         return (TARGET_SSE ? 0 : 1);
4190     }
4191
4192   if (mode == XFmode)
4193     return 0;
4194
4195   if (mode == TDmode)
4196     return 1;
4197
4198   if (size > 12)
4199     return 1;
4200   return 0;
4201 }
4202
4203 static int
4204 return_in_memory_64 (tree type, enum machine_mode mode)
4205 {
4206   int needed_intregs, needed_sseregs;
4207   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
4208 }
4209
4210 int
4211 ix86_return_in_memory (tree type)
4212 {
4213   enum machine_mode mode = type_natural_mode (type);
4214
4215   if (TARGET_64BIT)
4216     return return_in_memory_64 (type, mode);
4217   else
4218     return return_in_memory_32 (type, mode);
4219 }
4220
4221 /* When returning SSE vector types, we have a choice of either
4222      (1) being abi incompatible with a -march switch, or
4223      (2) generating an error.
4224    Given no good solution, I think the safest thing is one warning.
4225    The user won't be able to use -Werror, but....
4226
4227    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
4228    called in response to actually generating a caller or callee that
4229    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
4230    via aggregate_value_p for general type probing from tree-ssa.  */
4231
4232 static rtx
4233 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
4234 {
4235   static bool warnedsse, warnedmmx;
4236
4237   if (!TARGET_64BIT && type)
4238     {
4239       /* Look at the return type of the function, not the function type.  */
4240       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
4241
4242       if (!TARGET_SSE && !warnedsse)
4243         {
4244           if (mode == TImode
4245               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4246             {
4247               warnedsse = true;
4248               warning (0, "SSE vector return without SSE enabled "
4249                        "changes the ABI");
4250             }
4251         }
4252
4253       if (!TARGET_MMX && !warnedmmx)
4254         {
4255           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4256             {
4257               warnedmmx = true;
4258               warning (0, "MMX vector return without MMX enabled "
4259                        "changes the ABI");
4260             }
4261         }
4262     }
4263
4264   return NULL;
4265 }
4266
4267 \f
4268 /* Create the va_list data type.  */
4269
4270 static tree
4271 ix86_build_builtin_va_list (void)
4272 {
4273   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4274
4275   /* For i386 we use plain pointer to argument area.  */
4276   if (!TARGET_64BIT)
4277     return build_pointer_type (char_type_node);
4278
4279   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4280   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4281
4282   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4283                       unsigned_type_node);
4284   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4285                       unsigned_type_node);
4286   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4287                       ptr_type_node);
4288   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4289                       ptr_type_node);
4290
4291   va_list_gpr_counter_field = f_gpr;
4292   va_list_fpr_counter_field = f_fpr;
4293
4294   DECL_FIELD_CONTEXT (f_gpr) = record;
4295   DECL_FIELD_CONTEXT (f_fpr) = record;
4296   DECL_FIELD_CONTEXT (f_ovf) = record;
4297   DECL_FIELD_CONTEXT (f_sav) = record;
4298
4299   TREE_CHAIN (record) = type_decl;
4300   TYPE_NAME (record) = type_decl;
4301   TYPE_FIELDS (record) = f_gpr;
4302   TREE_CHAIN (f_gpr) = f_fpr;
4303   TREE_CHAIN (f_fpr) = f_ovf;
4304   TREE_CHAIN (f_ovf) = f_sav;
4305
4306   layout_type (record);
4307
4308   /* The correct type is an array type of one element.  */
4309   return build_array_type (record, build_index_type (size_zero_node));
4310 }
4311
4312 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4313
4314 static void
4315 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
4316 {
4317   rtx save_area, mem;
4318   rtx label;
4319   rtx label_ref;
4320   rtx tmp_reg;
4321   rtx nsse_reg;
4322   int set;
4323   int i;
4324
4325   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4326     return;
4327
4328   /* Indicate to allocate space on the stack for varargs save area.  */
4329   ix86_save_varrargs_registers = 1;
4330   cfun->stack_alignment_needed = 128;
4331
4332   save_area = frame_pointer_rtx;
4333   set = get_varargs_alias_set ();
4334
4335   for (i = cum->regno;
4336        i < ix86_regparm
4337        && i < cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4338        i++)
4339     {
4340       mem = gen_rtx_MEM (Pmode,
4341                          plus_constant (save_area, i * UNITS_PER_WORD));
4342       MEM_NOTRAP_P (mem) = 1;
4343       set_mem_alias_set (mem, set);
4344       emit_move_insn (mem, gen_rtx_REG (Pmode,
4345                                         x86_64_int_parameter_registers[i]));
4346     }
4347
4348   if (cum->sse_nregs && cfun->va_list_fpr_size)
4349     {
4350       /* Now emit code to save SSE registers.  The AX parameter contains number
4351          of SSE parameter registers used to call this function.  We use
4352          sse_prologue_save insn template that produces computed jump across
4353          SSE saves.  We need some preparation work to get this working.  */
4354
4355       label = gen_label_rtx ();
4356       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4357
4358       /* Compute address to jump to :
4359          label - 5*eax + nnamed_sse_arguments*5  */
4360       tmp_reg = gen_reg_rtx (Pmode);
4361       nsse_reg = gen_reg_rtx (Pmode);
4362       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4363       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4364                               gen_rtx_MULT (Pmode, nsse_reg,
4365                                             GEN_INT (4))));
4366       if (cum->sse_regno)
4367         emit_move_insn
4368           (nsse_reg,
4369            gen_rtx_CONST (DImode,
4370                           gen_rtx_PLUS (DImode,
4371                                         label_ref,
4372                                         GEN_INT (cum->sse_regno * 4))));
4373       else
4374         emit_move_insn (nsse_reg, label_ref);
4375       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4376
4377       /* Compute address of memory block we save into.  We always use pointer
4378          pointing 127 bytes after first byte to store - this is needed to keep
4379          instruction size limited by 4 bytes.  */
4380       tmp_reg = gen_reg_rtx (Pmode);
4381       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4382                               plus_constant (save_area,
4383                                              8 * REGPARM_MAX + 127)));
4384       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4385       MEM_NOTRAP_P (mem) = 1;
4386       set_mem_alias_set (mem, set);
4387       set_mem_align (mem, BITS_PER_WORD);
4388
4389       /* And finally do the dirty job!  */
4390       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4391                                         GEN_INT (cum->sse_regno), label));
4392     }
4393 }
4394
4395 static void
4396 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4397                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4398                              int no_rtl)
4399 {
4400   CUMULATIVE_ARGS next_cum;
4401   tree fntype;
4402   int stdarg_p;
4403
4404   /* This argument doesn't appear to be used anymore.  Which is good,
4405      because the old code here didn't suppress rtl generation.  */
4406   gcc_assert (!no_rtl);
4407
4408   if (!TARGET_64BIT)
4409     return;
4410
4411   fntype = TREE_TYPE (current_function_decl);
4412   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4413               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4414                   != void_type_node));
4415
4416   /* For varargs, we do not want to skip the dummy va_dcl argument.
4417      For stdargs, we do want to skip the last named argument.  */
4418   next_cum = *cum;
4419   if (stdarg_p)
4420     function_arg_advance (&next_cum, mode, type, 1);
4421
4422   setup_incoming_varargs_64 (&next_cum);
4423 }
4424
4425 /* Implement va_start.  */
4426
4427 void
4428 ix86_va_start (tree valist, rtx nextarg)
4429 {
4430   HOST_WIDE_INT words, n_gpr, n_fpr;
4431   tree f_gpr, f_fpr, f_ovf, f_sav;
4432   tree gpr, fpr, ovf, sav, t;
4433   tree type;
4434
4435   /* Only 64bit target needs something special.  */
4436   if (!TARGET_64BIT)
4437     {
4438       std_expand_builtin_va_start (valist, nextarg);
4439       return;
4440     }
4441
4442   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4443   f_fpr = TREE_CHAIN (f_gpr);
4444   f_ovf = TREE_CHAIN (f_fpr);
4445   f_sav = TREE_CHAIN (f_ovf);
4446
4447   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4448   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4449   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4450   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4451   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4452
4453   /* Count number of gp and fp argument registers used.  */
4454   words = current_function_args_info.words;
4455   n_gpr = current_function_args_info.regno;
4456   n_fpr = current_function_args_info.sse_regno;
4457
4458   if (cfun->va_list_gpr_size)
4459     {
4460       type = TREE_TYPE (gpr);
4461       t = build2 (GIMPLE_MODIFY_STMT, type, gpr,
4462                   build_int_cst (type, n_gpr * 8));
4463       TREE_SIDE_EFFECTS (t) = 1;
4464       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4465     }
4466
4467   if (cfun->va_list_fpr_size)
4468     {
4469       type = TREE_TYPE (fpr);
4470       t = build2 (GIMPLE_MODIFY_STMT, type, fpr,
4471                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4472       TREE_SIDE_EFFECTS (t) = 1;
4473       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4474     }
4475
4476   /* Find the overflow area.  */
4477   type = TREE_TYPE (ovf);
4478   t = make_tree (type, virtual_incoming_args_rtx);
4479   if (words != 0)
4480     t = build2 (PLUS_EXPR, type, t,
4481                 build_int_cst (type, words * UNITS_PER_WORD));
4482   t = build2 (GIMPLE_MODIFY_STMT, type, ovf, t);
4483   TREE_SIDE_EFFECTS (t) = 1;
4484   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4485
4486   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4487     {
4488       /* Find the register save area.
4489          Prologue of the function save it right above stack frame.  */
4490       type = TREE_TYPE (sav);
4491       t = make_tree (type, frame_pointer_rtx);
4492       t = build2 (GIMPLE_MODIFY_STMT, type, sav, t);
4493       TREE_SIDE_EFFECTS (t) = 1;
4494       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4495     }
4496 }
4497
4498 /* Implement va_arg.  */
4499
4500 static tree
4501 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4502 {
4503   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4504   tree f_gpr, f_fpr, f_ovf, f_sav;
4505   tree gpr, fpr, ovf, sav, t;
4506   int size, rsize;
4507   tree lab_false, lab_over = NULL_TREE;
4508   tree addr, t2;
4509   rtx container;
4510   int indirect_p = 0;
4511   tree ptrtype;
4512   enum machine_mode nat_mode;
4513
4514   /* Only 64bit target needs something special.  */
4515   if (!TARGET_64BIT)
4516     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4517
4518   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4519   f_fpr = TREE_CHAIN (f_gpr);
4520   f_ovf = TREE_CHAIN (f_fpr);
4521   f_sav = TREE_CHAIN (f_ovf);
4522
4523   valist = build_va_arg_indirect_ref (valist);
4524   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4525   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4526   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4527   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4528
4529   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4530   if (indirect_p)
4531     type = build_pointer_type (type);
4532   size = int_size_in_bytes (type);
4533   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4534
4535   nat_mode = type_natural_mode (type);
4536   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4537                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4538
4539   /* Pull the value out of the saved registers.  */
4540
4541   addr = create_tmp_var (ptr_type_node, "addr");
4542   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4543
4544   if (container)
4545     {
4546       int needed_intregs, needed_sseregs;
4547       bool need_temp;
4548       tree int_addr, sse_addr;
4549
4550       lab_false = create_artificial_label ();
4551       lab_over = create_artificial_label ();
4552
4553       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4554
4555       need_temp = (!REG_P (container)
4556                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4557                        || TYPE_ALIGN (type) > 128));
4558
4559       /* In case we are passing structure, verify that it is consecutive block
4560          on the register save area.  If not we need to do moves.  */
4561       if (!need_temp && !REG_P (container))
4562         {
4563           /* Verify that all registers are strictly consecutive  */
4564           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4565             {
4566               int i;
4567
4568               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4569                 {
4570                   rtx slot = XVECEXP (container, 0, i);
4571                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4572                       || INTVAL (XEXP (slot, 1)) != i * 16)
4573                     need_temp = 1;
4574                 }
4575             }
4576           else
4577             {
4578               int i;
4579
4580               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4581                 {
4582                   rtx slot = XVECEXP (container, 0, i);
4583                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4584                       || INTVAL (XEXP (slot, 1)) != i * 8)
4585                     need_temp = 1;
4586                 }
4587             }
4588         }
4589       if (!need_temp)
4590         {
4591           int_addr = addr;
4592           sse_addr = addr;
4593         }
4594       else
4595         {
4596           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4597           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4598           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4599           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4600         }
4601
4602       /* First ensure that we fit completely in registers.  */
4603       if (needed_intregs)
4604         {
4605           t = build_int_cst (TREE_TYPE (gpr),
4606                              (REGPARM_MAX - needed_intregs + 1) * 8);
4607           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4608           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4609           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4610           gimplify_and_add (t, pre_p);
4611         }
4612       if (needed_sseregs)
4613         {
4614           t = build_int_cst (TREE_TYPE (fpr),
4615                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4616                              + REGPARM_MAX * 8);
4617           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4618           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4619           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4620           gimplify_and_add (t, pre_p);
4621         }
4622
4623       /* Compute index to start of area used for integer regs.  */
4624       if (needed_intregs)
4625         {
4626           /* int_addr = gpr + sav; */
4627           t = fold_convert (ptr_type_node, gpr);
4628           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4629           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, int_addr, t);
4630           gimplify_and_add (t, pre_p);
4631         }
4632       if (needed_sseregs)
4633         {
4634           /* sse_addr = fpr + sav; */
4635           t = fold_convert (ptr_type_node, fpr);
4636           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4637           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, sse_addr, t);
4638           gimplify_and_add (t, pre_p);
4639         }
4640       if (need_temp)
4641         {
4642           int i;
4643           tree temp = create_tmp_var (type, "va_arg_tmp");
4644
4645           /* addr = &temp; */
4646           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4647           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4648           gimplify_and_add (t, pre_p);
4649
4650           for (i = 0; i < XVECLEN (container, 0); i++)
4651             {
4652               rtx slot = XVECEXP (container, 0, i);
4653               rtx reg = XEXP (slot, 0);
4654               enum machine_mode mode = GET_MODE (reg);
4655               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4656               tree addr_type = build_pointer_type (piece_type);
4657               tree src_addr, src;
4658               int src_offset;
4659               tree dest_addr, dest;
4660
4661               if (SSE_REGNO_P (REGNO (reg)))
4662                 {
4663                   src_addr = sse_addr;
4664                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4665                 }
4666               else
4667                 {
4668                   src_addr = int_addr;
4669                   src_offset = REGNO (reg) * 8;
4670                 }
4671               src_addr = fold_convert (addr_type, src_addr);
4672               src_addr = fold_build2 (PLUS_EXPR, addr_type, src_addr,
4673                                       size_int (src_offset));
4674               src = build_va_arg_indirect_ref (src_addr);
4675
4676               dest_addr = fold_convert (addr_type, addr);
4677               dest_addr = fold_build2 (PLUS_EXPR, addr_type, dest_addr,
4678                                        size_int (INTVAL (XEXP (slot, 1))));
4679               dest = build_va_arg_indirect_ref (dest_addr);
4680
4681               t = build2 (GIMPLE_MODIFY_STMT, void_type_node, dest, src);
4682               gimplify_and_add (t, pre_p);
4683             }
4684         }
4685
4686       if (needed_intregs)
4687         {
4688           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4689                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4690           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (gpr), gpr, t);
4691           gimplify_and_add (t, pre_p);
4692         }
4693       if (needed_sseregs)
4694         {
4695           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4696                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4697           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (fpr), fpr, t);
4698           gimplify_and_add (t, pre_p);
4699         }
4700
4701       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4702       gimplify_and_add (t, pre_p);
4703
4704       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4705       append_to_statement_list (t, pre_p);
4706     }
4707
4708   /* ... otherwise out of the overflow area.  */
4709
4710   /* Care for on-stack alignment if needed.  */
4711   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4712       || integer_zerop (TYPE_SIZE (type)))
4713     t = ovf;
4714   else
4715     {
4716       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4717       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4718                   build_int_cst (TREE_TYPE (ovf), align - 1));
4719       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4720                   build_int_cst (TREE_TYPE (t), -align));
4721     }
4722   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4723
4724   t2 = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4725   gimplify_and_add (t2, pre_p);
4726
4727   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4728               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4729   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (ovf), ovf, t);
4730   gimplify_and_add (t, pre_p);
4731
4732   if (container)
4733     {
4734       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4735       append_to_statement_list (t, pre_p);
4736     }
4737
4738   ptrtype = build_pointer_type (type);
4739   addr = fold_convert (ptrtype, addr);
4740
4741   if (indirect_p)
4742     addr = build_va_arg_indirect_ref (addr);
4743   return build_va_arg_indirect_ref (addr);
4744 }
4745 \f
4746 /* Return nonzero if OPNUM's MEM should be matched
4747    in movabs* patterns.  */
4748
4749 int
4750 ix86_check_movabs (rtx insn, int opnum)
4751 {
4752   rtx set, mem;
4753
4754   set = PATTERN (insn);
4755   if (GET_CODE (set) == PARALLEL)
4756     set = XVECEXP (set, 0, 0);
4757   gcc_assert (GET_CODE (set) == SET);
4758   mem = XEXP (set, opnum);
4759   while (GET_CODE (mem) == SUBREG)
4760     mem = SUBREG_REG (mem);
4761   gcc_assert (MEM_P (mem));
4762   return (volatile_ok || !MEM_VOLATILE_P (mem));
4763 }
4764 \f
4765 /* Initialize the table of extra 80387 mathematical constants.  */
4766
4767 static void
4768 init_ext_80387_constants (void)
4769 {
4770   static const char * cst[5] =
4771   {
4772     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4773     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4774     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4775     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4776     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4777   };
4778   int i;
4779
4780   for (i = 0; i < 5; i++)
4781     {
4782       real_from_string (&ext_80387_constants_table[i], cst[i]);
4783       /* Ensure each constant is rounded to XFmode precision.  */
4784       real_convert (&ext_80387_constants_table[i],
4785                     XFmode, &ext_80387_constants_table[i]);
4786     }
4787
4788   ext_80387_constants_init = 1;
4789 }
4790
4791 /* Return true if the constant is something that can be loaded with
4792    a special instruction.  */
4793
4794 int
4795 standard_80387_constant_p (rtx x)
4796 {
4797   REAL_VALUE_TYPE r;
4798
4799   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4800     return -1;
4801
4802   if (x == CONST0_RTX (GET_MODE (x)))
4803     return 1;
4804   if (x == CONST1_RTX (GET_MODE (x)))
4805     return 2;
4806
4807   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4808
4809   /* For XFmode constants, try to find a special 80387 instruction when
4810      optimizing for size or on those CPUs that benefit from them.  */
4811   if (GET_MODE (x) == XFmode
4812       && (optimize_size || TARGET_EXT_80387_CONSTANTS))
4813     {
4814       int i;
4815
4816       if (! ext_80387_constants_init)
4817         init_ext_80387_constants ();
4818
4819       for (i = 0; i < 5; i++)
4820         if (real_identical (&r, &ext_80387_constants_table[i]))
4821           return i + 3;
4822     }
4823
4824   /* Load of the constant -0.0 or -1.0 will be split as
4825      fldz;fchs or fld1;fchs sequence.  */
4826   if (real_isnegzero (&r))
4827     return 8;
4828   if (real_identical (&r, &dconstm1))
4829     return 9;
4830
4831   return 0;
4832 }
4833
4834 /* Return the opcode of the special instruction to be used to load
4835    the constant X.  */
4836
4837 const char *
4838 standard_80387_constant_opcode (rtx x)
4839 {
4840   switch (standard_80387_constant_p (x))
4841     {
4842     case 1:
4843       return "fldz";
4844     case 2:
4845       return "fld1";
4846     case 3:
4847       return "fldlg2";
4848     case 4:
4849       return "fldln2";
4850     case 5:
4851       return "fldl2e";
4852     case 6:
4853       return "fldl2t";
4854     case 7:
4855       return "fldpi";
4856     case 8:
4857     case 9:
4858       return "#";
4859     default:
4860       gcc_unreachable ();
4861     }
4862 }
4863
4864 /* Return the CONST_DOUBLE representing the 80387 constant that is
4865    loaded by the specified special instruction.  The argument IDX
4866    matches the return value from standard_80387_constant_p.  */
4867
4868 rtx
4869 standard_80387_constant_rtx (int idx)
4870 {
4871   int i;
4872
4873   if (! ext_80387_constants_init)
4874     init_ext_80387_constants ();
4875
4876   switch (idx)
4877     {
4878     case 3:
4879     case 4:
4880     case 5:
4881     case 6:
4882     case 7:
4883       i = idx - 3;
4884       break;
4885
4886     default:
4887       gcc_unreachable ();
4888     }
4889
4890   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4891                                        XFmode);
4892 }
4893
4894 /* Return 1 if mode is a valid mode for sse.  */
4895 static int
4896 standard_sse_mode_p (enum machine_mode mode)
4897 {
4898   switch (mode)
4899     {
4900     case V16QImode:
4901     case V8HImode:
4902     case V4SImode:
4903     case V2DImode:
4904     case V4SFmode:
4905     case V2DFmode:
4906       return 1;
4907
4908     default:
4909       return 0;
4910     }
4911 }
4912
4913 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4914  */
4915 int
4916 standard_sse_constant_p (rtx x)
4917 {
4918   enum machine_mode mode = GET_MODE (x);
4919
4920   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
4921     return 1;
4922   if (vector_all_ones_operand (x, mode)
4923       && standard_sse_mode_p (mode))
4924     return TARGET_SSE2 ? 2 : -1;
4925
4926   return 0;
4927 }
4928
4929 /* Return the opcode of the special instruction to be used to load
4930    the constant X.  */
4931
4932 const char *
4933 standard_sse_constant_opcode (rtx insn, rtx x)
4934 {
4935   switch (standard_sse_constant_p (x))
4936     {
4937     case 1:
4938       if (get_attr_mode (insn) == MODE_V4SF)
4939         return "xorps\t%0, %0";
4940       else if (get_attr_mode (insn) == MODE_V2DF)
4941         return "xorpd\t%0, %0";
4942       else
4943         return "pxor\t%0, %0";
4944     case 2:
4945       return "pcmpeqd\t%0, %0";
4946     }
4947   gcc_unreachable ();
4948 }
4949
4950 /* Returns 1 if OP contains a symbol reference */
4951
4952 int
4953 symbolic_reference_mentioned_p (rtx op)
4954 {
4955   const char *fmt;
4956   int i;
4957
4958   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4959     return 1;
4960
4961   fmt = GET_RTX_FORMAT (GET_CODE (op));
4962   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4963     {
4964       if (fmt[i] == 'E')
4965         {
4966           int j;
4967
4968           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4969             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4970               return 1;
4971         }
4972
4973       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4974         return 1;
4975     }
4976
4977   return 0;
4978 }
4979
4980 /* Return 1 if it is appropriate to emit `ret' instructions in the
4981    body of a function.  Do this only if the epilogue is simple, needing a
4982    couple of insns.  Prior to reloading, we can't tell how many registers
4983    must be saved, so return 0 then.  Return 0 if there is no frame
4984    marker to de-allocate.  */
4985
4986 int
4987 ix86_can_use_return_insn_p (void)
4988 {
4989   struct ix86_frame frame;
4990
4991   if (! reload_completed || frame_pointer_needed)
4992     return 0;
4993
4994   /* Don't allow more than 32 pop, since that's all we can do
4995      with one instruction.  */
4996   if (current_function_pops_args
4997       && current_function_args_size >= 32768)
4998     return 0;
4999
5000   ix86_compute_frame_layout (&frame);
5001   return frame.to_allocate == 0 && frame.nregs == 0;
5002 }
5003 \f
5004 /* Value should be nonzero if functions must have frame pointers.
5005    Zero means the frame pointer need not be set up (and parms may
5006    be accessed via the stack pointer) in functions that seem suitable.  */
5007
5008 int
5009 ix86_frame_pointer_required (void)
5010 {
5011   /* If we accessed previous frames, then the generated code expects
5012      to be able to access the saved ebp value in our frame.  */
5013   if (cfun->machine->accesses_prev_frame)
5014     return 1;
5015
5016   /* Several x86 os'es need a frame pointer for other reasons,
5017      usually pertaining to setjmp.  */
5018   if (SUBTARGET_FRAME_POINTER_REQUIRED)
5019     return 1;
5020
5021   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
5022      the frame pointer by default.  Turn it back on now if we've not
5023      got a leaf function.  */
5024   if (TARGET_OMIT_LEAF_FRAME_POINTER
5025       && (!current_function_is_leaf
5026           || ix86_current_function_calls_tls_descriptor))
5027     return 1;
5028
5029   if (current_function_profile)
5030     return 1;
5031
5032   return 0;
5033 }
5034
5035 /* Record that the current function accesses previous call frames.  */
5036
5037 void
5038 ix86_setup_frame_addresses (void)
5039 {
5040   cfun->machine->accesses_prev_frame = 1;
5041 }
5042 \f
5043 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
5044 # define USE_HIDDEN_LINKONCE 1
5045 #else
5046 # define USE_HIDDEN_LINKONCE 0
5047 #endif
5048
5049 static int pic_labels_used;
5050
5051 /* Fills in the label name that should be used for a pc thunk for
5052    the given register.  */
5053
5054 static void
5055 get_pc_thunk_name (char name[32], unsigned int regno)
5056 {
5057   gcc_assert (!TARGET_64BIT);
5058
5059   if (USE_HIDDEN_LINKONCE)
5060     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
5061   else
5062     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
5063 }
5064
5065
5066 /* This function generates code for -fpic that loads %ebx with
5067    the return address of the caller and then returns.  */
5068
5069 void
5070 ix86_file_end (void)
5071 {
5072   rtx xops[2];
5073   int regno;
5074
5075   for (regno = 0; regno < 8; ++regno)
5076     {
5077       char name[32];
5078
5079       if (! ((pic_labels_used >> regno) & 1))
5080         continue;
5081
5082       get_pc_thunk_name (name, regno);
5083
5084 #if TARGET_MACHO
5085       if (TARGET_MACHO)
5086         {
5087           switch_to_section (darwin_sections[text_coal_section]);
5088           fputs ("\t.weak_definition\t", asm_out_file);
5089           assemble_name (asm_out_file, name);
5090           fputs ("\n\t.private_extern\t", asm_out_file);
5091           assemble_name (asm_out_file, name);
5092           fputs ("\n", asm_out_file);
5093           ASM_OUTPUT_LABEL (asm_out_file, name);
5094         }
5095       else
5096 #endif
5097       if (USE_HIDDEN_LINKONCE)
5098         {
5099           tree decl;
5100
5101           decl = build_decl (FUNCTION_DECL, get_identifier (name),
5102                              error_mark_node);
5103           TREE_PUBLIC (decl) = 1;
5104           TREE_STATIC (decl) = 1;
5105           DECL_ONE_ONLY (decl) = 1;
5106
5107           (*targetm.asm_out.unique_section) (decl, 0);
5108           switch_to_section (get_named_section (decl, NULL, 0));
5109
5110           (*targetm.asm_out.globalize_label) (asm_out_file, name);
5111           fputs ("\t.hidden\t", asm_out_file);
5112           assemble_name (asm_out_file, name);
5113           fputc ('\n', asm_out_file);
5114           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
5115         }
5116       else
5117         {
5118           switch_to_section (text_section);
5119           ASM_OUTPUT_LABEL (asm_out_file, name);
5120         }
5121
5122       xops[0] = gen_rtx_REG (SImode, regno);
5123       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
5124       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
5125       output_asm_insn ("ret", xops);
5126     }
5127
5128   if (NEED_INDICATE_EXEC_STACK)
5129     file_end_indicate_exec_stack ();
5130 }
5131
5132 /* Emit code for the SET_GOT patterns.  */
5133
5134 const char *
5135 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
5136 {
5137   rtx xops[3];
5138
5139   xops[0] = dest;
5140
5141   if (TARGET_VXWORKS_RTP && flag_pic)
5142     {
5143       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
5144       xops[2] = gen_rtx_MEM (Pmode,
5145                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
5146       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5147
5148       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
5149          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
5150          an unadorned address.  */
5151       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5152       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
5153       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
5154       return "";
5155     }
5156
5157   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
5158
5159   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
5160     {
5161       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
5162
5163       if (!flag_pic)
5164         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5165       else
5166         output_asm_insn ("call\t%a2", xops);
5167
5168 #if TARGET_MACHO
5169       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5170          is what will be referenced by the Mach-O PIC subsystem.  */
5171       if (!label)
5172         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5173 #endif
5174
5175       (*targetm.asm_out.internal_label) (asm_out_file, "L",
5176                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
5177
5178       if (flag_pic)
5179         output_asm_insn ("pop{l}\t%0", xops);
5180     }
5181   else
5182     {
5183       char name[32];
5184       get_pc_thunk_name (name, REGNO (dest));
5185       pic_labels_used |= 1 << REGNO (dest);
5186
5187       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5188       xops[2] = gen_rtx_MEM (QImode, xops[2]);
5189       output_asm_insn ("call\t%X2", xops);
5190       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5191          is what will be referenced by the Mach-O PIC subsystem.  */
5192 #if TARGET_MACHO
5193       if (!label)
5194         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5195       else
5196         targetm.asm_out.internal_label (asm_out_file, "L",
5197                                            CODE_LABEL_NUMBER (label));
5198 #endif
5199     }
5200
5201   if (TARGET_MACHO)
5202     return "";
5203
5204   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
5205     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
5206   else
5207     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
5208
5209   return "";
5210 }
5211
5212 /* Generate an "push" pattern for input ARG.  */
5213
5214 static rtx
5215 gen_push (rtx arg)
5216 {
5217   return gen_rtx_SET (VOIDmode,
5218                       gen_rtx_MEM (Pmode,
5219                                    gen_rtx_PRE_DEC (Pmode,
5220                                                     stack_pointer_rtx)),
5221                       arg);
5222 }
5223
5224 /* Return >= 0 if there is an unused call-clobbered register available
5225    for the entire function.  */
5226
5227 static unsigned int
5228 ix86_select_alt_pic_regnum (void)
5229 {
5230   if (current_function_is_leaf && !current_function_profile
5231       && !ix86_current_function_calls_tls_descriptor)
5232     {
5233       int i;
5234       for (i = 2; i >= 0; --i)
5235         if (!regs_ever_live[i])
5236           return i;
5237     }
5238
5239   return INVALID_REGNUM;
5240 }
5241
5242 /* Return 1 if we need to save REGNO.  */
5243 static int
5244 ix86_save_reg (unsigned int regno, int maybe_eh_return)
5245 {
5246   if (pic_offset_table_rtx
5247       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5248       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5249           || current_function_profile
5250           || current_function_calls_eh_return
5251           || current_function_uses_const_pool))
5252     {
5253       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5254         return 0;
5255       return 1;
5256     }
5257
5258   if (current_function_calls_eh_return && maybe_eh_return)
5259     {
5260       unsigned i;
5261       for (i = 0; ; i++)
5262         {
5263           unsigned test = EH_RETURN_DATA_REGNO (i);
5264           if (test == INVALID_REGNUM)
5265             break;
5266           if (test == regno)
5267             return 1;
5268         }
5269     }
5270
5271   if (cfun->machine->force_align_arg_pointer
5272       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5273     return 1;
5274
5275   return (regs_ever_live[regno]
5276           && !call_used_regs[regno]
5277           && !fixed_regs[regno]
5278           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5279 }
5280
5281 /* Return number of registers to be saved on the stack.  */
5282
5283 static int
5284 ix86_nsaved_regs (void)
5285 {
5286   int nregs = 0;
5287   int regno;
5288
5289   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5290     if (ix86_save_reg (regno, true))
5291       nregs++;
5292   return nregs;
5293 }
5294
5295 /* Return the offset between two registers, one to be eliminated, and the other
5296    its replacement, at the start of a routine.  */
5297
5298 HOST_WIDE_INT
5299 ix86_initial_elimination_offset (int from, int to)
5300 {
5301   struct ix86_frame frame;
5302   ix86_compute_frame_layout (&frame);
5303
5304   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5305     return frame.hard_frame_pointer_offset;
5306   else if (from == FRAME_POINTER_REGNUM
5307            && to == HARD_FRAME_POINTER_REGNUM)
5308     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5309   else
5310     {
5311       gcc_assert (to == STACK_POINTER_REGNUM);
5312
5313       if (from == ARG_POINTER_REGNUM)
5314         return frame.stack_pointer_offset;
5315
5316       gcc_assert (from == FRAME_POINTER_REGNUM);
5317       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5318     }
5319 }
5320
5321 /* Fill structure ix86_frame about frame of currently computed function.  */
5322
5323 static void
5324 ix86_compute_frame_layout (struct ix86_frame *frame)
5325 {
5326   HOST_WIDE_INT total_size;
5327   unsigned int stack_alignment_needed;
5328   HOST_WIDE_INT offset;
5329   unsigned int preferred_alignment;
5330   HOST_WIDE_INT size = get_frame_size ();
5331
5332   frame->nregs = ix86_nsaved_regs ();
5333   total_size = size;
5334
5335   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5336   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5337
5338   /* During reload iteration the amount of registers saved can change.
5339      Recompute the value as needed.  Do not recompute when amount of registers
5340      didn't change as reload does multiple calls to the function and does not
5341      expect the decision to change within single iteration.  */
5342   if (!optimize_size
5343       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5344     {
5345       int count = frame->nregs;
5346
5347       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5348       /* The fast prologue uses move instead of push to save registers.  This
5349          is significantly longer, but also executes faster as modern hardware
5350          can execute the moves in parallel, but can't do that for push/pop.
5351
5352          Be careful about choosing what prologue to emit:  When function takes
5353          many instructions to execute we may use slow version as well as in
5354          case function is known to be outside hot spot (this is known with
5355          feedback only).  Weight the size of function by number of registers
5356          to save as it is cheap to use one or two push instructions but very
5357          slow to use many of them.  */
5358       if (count)
5359         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5360       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5361           || (flag_branch_probabilities
5362               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5363         cfun->machine->use_fast_prologue_epilogue = false;
5364       else
5365         cfun->machine->use_fast_prologue_epilogue
5366            = !expensive_function_p (count);
5367     }
5368   if (TARGET_PROLOGUE_USING_MOVE
5369       && cfun->machine->use_fast_prologue_epilogue)
5370     frame->save_regs_using_mov = true;
5371   else
5372     frame->save_regs_using_mov = false;
5373
5374
5375   /* Skip return address and saved base pointer.  */
5376   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5377
5378   frame->hard_frame_pointer_offset = offset;
5379
5380   /* Do some sanity checking of stack_alignment_needed and
5381      preferred_alignment, since i386 port is the only using those features
5382      that may break easily.  */
5383
5384   gcc_assert (!size || stack_alignment_needed);
5385   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5386   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5387   gcc_assert (stack_alignment_needed
5388               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5389
5390   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5391     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5392
5393   /* Register save area */
5394   offset += frame->nregs * UNITS_PER_WORD;
5395
5396   /* Va-arg area */
5397   if (ix86_save_varrargs_registers)
5398     {
5399       offset += X86_64_VARARGS_SIZE;
5400       frame->va_arg_size = X86_64_VARARGS_SIZE;
5401     }
5402   else
5403     frame->va_arg_size = 0;
5404
5405   /* Align start of frame for local function.  */
5406   frame->padding1 = ((offset + stack_alignment_needed - 1)
5407                      & -stack_alignment_needed) - offset;
5408
5409   offset += frame->padding1;
5410
5411   /* Frame pointer points here.  */
5412   frame->frame_pointer_offset = offset;
5413
5414   offset += size;
5415
5416   /* Add outgoing arguments area.  Can be skipped if we eliminated
5417      all the function calls as dead code.
5418      Skipping is however impossible when function calls alloca.  Alloca
5419      expander assumes that last current_function_outgoing_args_size
5420      of stack frame are unused.  */
5421   if (ACCUMULATE_OUTGOING_ARGS
5422       && (!current_function_is_leaf || current_function_calls_alloca
5423           || ix86_current_function_calls_tls_descriptor))
5424     {
5425       offset += current_function_outgoing_args_size;
5426       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5427     }
5428   else
5429     frame->outgoing_arguments_size = 0;
5430
5431   /* Align stack boundary.  Only needed if we're calling another function
5432      or using alloca.  */
5433   if (!current_function_is_leaf || current_function_calls_alloca
5434       || ix86_current_function_calls_tls_descriptor)
5435     frame->padding2 = ((offset + preferred_alignment - 1)
5436                        & -preferred_alignment) - offset;
5437   else
5438     frame->padding2 = 0;
5439
5440   offset += frame->padding2;
5441
5442   /* We've reached end of stack frame.  */
5443   frame->stack_pointer_offset = offset;
5444
5445   /* Size prologue needs to allocate.  */
5446   frame->to_allocate =
5447     (size + frame->padding1 + frame->padding2
5448      + frame->outgoing_arguments_size + frame->va_arg_size);
5449
5450   if ((!frame->to_allocate && frame->nregs <= 1)
5451       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5452     frame->save_regs_using_mov = false;
5453
5454   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5455       && current_function_is_leaf
5456       && !ix86_current_function_calls_tls_descriptor)
5457     {
5458       frame->red_zone_size = frame->to_allocate;
5459       if (frame->save_regs_using_mov)
5460         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5461       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5462         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5463     }
5464   else
5465     frame->red_zone_size = 0;
5466   frame->to_allocate -= frame->red_zone_size;
5467   frame->stack_pointer_offset -= frame->red_zone_size;
5468 #if 0
5469   fprintf (stderr, "\n");
5470   fprintf (stderr, "nregs: %ld\n", (long)frame->nregs);
5471   fprintf (stderr, "size: %ld\n", (long)size);
5472   fprintf (stderr, "alignment1: %ld\n", (long)stack_alignment_needed);
5473   fprintf (stderr, "padding1: %ld\n", (long)frame->padding1);
5474   fprintf (stderr, "va_arg: %ld\n", (long)frame->va_arg_size);
5475   fprintf (stderr, "padding2: %ld\n", (long)frame->padding2);
5476   fprintf (stderr, "to_allocate: %ld\n", (long)frame->to_allocate);
5477   fprintf (stderr, "red_zone_size: %ld\n", (long)frame->red_zone_size);
5478   fprintf (stderr, "frame_pointer_offset: %ld\n", (long)frame->frame_pointer_offset);
5479   fprintf (stderr, "hard_frame_pointer_offset: %ld\n",
5480            (long)frame->hard_frame_pointer_offset);
5481   fprintf (stderr, "stack_pointer_offset: %ld\n", (long)frame->stack_pointer_offset);
5482   fprintf (stderr, "current_function_is_leaf: %ld\n", (long)current_function_is_leaf);
5483   fprintf (stderr, "current_function_calls_alloca: %ld\n", (long)current_function_calls_alloca);
5484   fprintf (stderr, "x86_current_function_calls_tls_descriptor: %ld\n", (long)ix86_current_function_calls_tls_descriptor);
5485 #endif
5486 }
5487
5488 /* Emit code to save registers in the prologue.  */
5489
5490 static void
5491 ix86_emit_save_regs (void)
5492 {
5493   unsigned int regno;
5494   rtx insn;
5495
5496   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5497     if (ix86_save_reg (regno, true))
5498       {
5499         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5500         RTX_FRAME_RELATED_P (insn) = 1;
5501       }
5502 }
5503
5504 /* Emit code to save registers using MOV insns.  First register
5505    is restored from POINTER + OFFSET.  */
5506 static void
5507 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5508 {
5509   unsigned int regno;
5510   rtx insn;
5511
5512   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5513     if (ix86_save_reg (regno, true))
5514       {
5515         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5516                                                Pmode, offset),
5517                                gen_rtx_REG (Pmode, regno));
5518         RTX_FRAME_RELATED_P (insn) = 1;
5519         offset += UNITS_PER_WORD;
5520       }
5521 }
5522
5523 /* Expand prologue or epilogue stack adjustment.
5524    The pattern exist to put a dependency on all ebp-based memory accesses.
5525    STYLE should be negative if instructions should be marked as frame related,
5526    zero if %r11 register is live and cannot be freely used and positive
5527    otherwise.  */
5528
5529 static void
5530 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5531 {
5532   rtx insn;
5533
5534   if (! TARGET_64BIT)
5535     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5536   else if (x86_64_immediate_operand (offset, DImode))
5537     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5538   else
5539     {
5540       rtx r11;
5541       /* r11 is used by indirect sibcall return as well, set before the
5542          epilogue and used after the epilogue.  ATM indirect sibcall
5543          shouldn't be used together with huge frame sizes in one
5544          function because of the frame_size check in sibcall.c.  */
5545       gcc_assert (style);
5546       r11 = gen_rtx_REG (DImode, R11_REG);
5547       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5548       if (style < 0)
5549         RTX_FRAME_RELATED_P (insn) = 1;
5550       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5551                                                                offset));
5552     }
5553   if (style < 0)
5554     RTX_FRAME_RELATED_P (insn) = 1;
5555 }
5556
5557 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5558
5559 static rtx
5560 ix86_internal_arg_pointer (void)
5561 {
5562   bool has_force_align_arg_pointer =
5563     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5564                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5565   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5566        && DECL_NAME (current_function_decl)
5567        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5568        && DECL_FILE_SCOPE_P (current_function_decl))
5569       || ix86_force_align_arg_pointer
5570       || has_force_align_arg_pointer)
5571     {
5572       /* Nested functions can't realign the stack due to a register
5573          conflict.  */
5574       if (DECL_CONTEXT (current_function_decl)
5575           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5576         {
5577           if (ix86_force_align_arg_pointer)
5578             warning (0, "-mstackrealign ignored for nested functions");
5579           if (has_force_align_arg_pointer)
5580             error ("%s not supported for nested functions",
5581                    ix86_force_align_arg_pointer_string);
5582           return virtual_incoming_args_rtx;
5583         }
5584       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5585       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5586     }
5587   else
5588     return virtual_incoming_args_rtx;
5589 }
5590
5591 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5592    This is called from dwarf2out.c to emit call frame instructions
5593    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5594 static void
5595 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5596 {
5597   rtx unspec = SET_SRC (pattern);
5598   gcc_assert (GET_CODE (unspec) == UNSPEC);
5599
5600   switch (index)
5601     {
5602     case UNSPEC_REG_SAVE:
5603       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5604                               SET_DEST (pattern));
5605       break;
5606     case UNSPEC_DEF_CFA:
5607       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5608                          INTVAL (XVECEXP (unspec, 0, 0)));
5609       break;
5610     default:
5611       gcc_unreachable ();
5612     }
5613 }
5614
5615 /* Expand the prologue into a bunch of separate insns.  */
5616
5617 void
5618 ix86_expand_prologue (void)
5619 {
5620   rtx insn;
5621   bool pic_reg_used;
5622   struct ix86_frame frame;
5623   HOST_WIDE_INT allocate;
5624
5625   ix86_compute_frame_layout (&frame);
5626
5627   if (cfun->machine->force_align_arg_pointer)
5628     {
5629       rtx x, y;
5630
5631       /* Grab the argument pointer.  */
5632       x = plus_constant (stack_pointer_rtx, 4);
5633       y = cfun->machine->force_align_arg_pointer;
5634       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5635       RTX_FRAME_RELATED_P (insn) = 1;
5636
5637       /* The unwind info consists of two parts: install the fafp as the cfa,
5638          and record the fafp as the "save register" of the stack pointer.
5639          The later is there in order that the unwinder can see where it
5640          should restore the stack pointer across the and insn.  */
5641       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5642       x = gen_rtx_SET (VOIDmode, y, x);
5643       RTX_FRAME_RELATED_P (x) = 1;
5644       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5645                           UNSPEC_REG_SAVE);
5646       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5647       RTX_FRAME_RELATED_P (y) = 1;
5648       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5649       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5650       REG_NOTES (insn) = x;
5651
5652       /* Align the stack.  */
5653       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5654                              GEN_INT (-16)));
5655
5656       /* And here we cheat like madmen with the unwind info.  We force the
5657          cfa register back to sp+4, which is exactly what it was at the
5658          start of the function.  Re-pushing the return address results in
5659          the return at the same spot relative to the cfa, and thus is
5660          correct wrt the unwind info.  */
5661       x = cfun->machine->force_align_arg_pointer;
5662       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5663       insn = emit_insn (gen_push (x));
5664       RTX_FRAME_RELATED_P (insn) = 1;
5665
5666       x = GEN_INT (4);
5667       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5668       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5669       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5670       REG_NOTES (insn) = x;
5671     }
5672
5673   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5674      slower on all targets.  Also sdb doesn't like it.  */
5675
5676   if (frame_pointer_needed)
5677     {
5678       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5679       RTX_FRAME_RELATED_P (insn) = 1;
5680
5681       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5682       RTX_FRAME_RELATED_P (insn) = 1;
5683     }
5684
5685   allocate = frame.to_allocate;
5686
5687   if (!frame.save_regs_using_mov)
5688     ix86_emit_save_regs ();
5689   else
5690     allocate += frame.nregs * UNITS_PER_WORD;
5691
5692   /* When using red zone we may start register saving before allocating
5693      the stack frame saving one cycle of the prologue.  */
5694   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5695     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5696                                    : stack_pointer_rtx,
5697                                    -frame.nregs * UNITS_PER_WORD);
5698
5699   if (allocate == 0)
5700     ;
5701   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5702     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5703                                GEN_INT (-allocate), -1);
5704   else
5705     {
5706       /* Only valid for Win32.  */
5707       rtx eax = gen_rtx_REG (SImode, 0);
5708       bool eax_live = ix86_eax_live_at_start_p ();
5709       rtx t;
5710
5711       gcc_assert (!TARGET_64BIT);
5712
5713       if (eax_live)
5714         {
5715           emit_insn (gen_push (eax));
5716           allocate -= 4;
5717         }
5718
5719       emit_move_insn (eax, GEN_INT (allocate));
5720
5721       insn = emit_insn (gen_allocate_stack_worker (eax));
5722       RTX_FRAME_RELATED_P (insn) = 1;
5723       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5724       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5725       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5726                                             t, REG_NOTES (insn));
5727
5728       if (eax_live)
5729         {
5730           if (frame_pointer_needed)
5731             t = plus_constant (hard_frame_pointer_rtx,
5732                                allocate
5733                                - frame.to_allocate
5734                                - frame.nregs * UNITS_PER_WORD);
5735           else
5736             t = plus_constant (stack_pointer_rtx, allocate);
5737           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5738         }
5739     }
5740
5741   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5742     {
5743       if (!frame_pointer_needed || !frame.to_allocate)
5744         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5745       else
5746         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5747                                        -frame.nregs * UNITS_PER_WORD);
5748     }
5749
5750   pic_reg_used = false;
5751   if (pic_offset_table_rtx
5752       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5753           || current_function_profile))
5754     {
5755       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5756
5757       if (alt_pic_reg_used != INVALID_REGNUM)
5758         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5759
5760       pic_reg_used = true;
5761     }
5762
5763   if (pic_reg_used)
5764     {
5765       if (TARGET_64BIT)
5766         {
5767           if (ix86_cmodel == CM_LARGE_PIC)
5768             {
5769               rtx tmp_reg = gen_rtx_REG (DImode,
5770                                          FIRST_REX_INT_REG + 3 /* R11 */);
5771               rtx label = gen_label_rtx ();
5772               emit_label (label);
5773               LABEL_PRESERVE_P (label) = 1;
5774               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
5775               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
5776               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5777               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
5778               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5779               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
5780                                             pic_offset_table_rtx, tmp_reg));
5781             }
5782           else
5783             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5784         }
5785       else
5786         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5787
5788       /* Even with accurate pre-reload life analysis, we can wind up
5789          deleting all references to the pic register after reload.
5790          Consider if cross-jumping unifies two sides of a branch
5791          controlled by a comparison vs the only read from a global.
5792          In which case, allow the set_got to be deleted, though we're
5793          too late to do anything about the ebx save in the prologue.  */
5794       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5795     }
5796
5797   /* Prevent function calls from be scheduled before the call to mcount.
5798      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5799   if (current_function_profile)
5800     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5801 }
5802
5803 /* Emit code to restore saved registers using MOV insns.  First register
5804    is restored from POINTER + OFFSET.  */
5805 static void
5806 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5807                                   int maybe_eh_return)
5808 {
5809   int regno;
5810   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5811
5812   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5813     if (ix86_save_reg (regno, maybe_eh_return))
5814       {
5815         /* Ensure that adjust_address won't be forced to produce pointer
5816            out of range allowed by x86-64 instruction set.  */
5817         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5818           {
5819             rtx r11;
5820
5821             r11 = gen_rtx_REG (DImode, R11_REG);
5822             emit_move_insn (r11, GEN_INT (offset));
5823             emit_insn (gen_adddi3 (r11, r11, pointer));
5824             base_address = gen_rtx_MEM (Pmode, r11);
5825             offset = 0;
5826           }
5827         emit_move_insn (gen_rtx_REG (Pmode, regno),
5828                         adjust_address (base_address, Pmode, offset));
5829         offset += UNITS_PER_WORD;
5830       }
5831 }
5832
5833 /* Restore function stack, frame, and registers.  */
5834
5835 void
5836 ix86_expand_epilogue (int style)
5837 {
5838   int regno;
5839   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5840   struct ix86_frame frame;
5841   HOST_WIDE_INT offset;
5842
5843   ix86_compute_frame_layout (&frame);
5844
5845   /* Calculate start of saved registers relative to ebp.  Special care
5846      must be taken for the normal return case of a function using
5847      eh_return: the eax and edx registers are marked as saved, but not
5848      restored along this path.  */
5849   offset = frame.nregs;
5850   if (current_function_calls_eh_return && style != 2)
5851     offset -= 2;
5852   offset *= -UNITS_PER_WORD;
5853
5854   /* If we're only restoring one register and sp is not valid then
5855      using a move instruction to restore the register since it's
5856      less work than reloading sp and popping the register.
5857
5858      The default code result in stack adjustment using add/lea instruction,
5859      while this code results in LEAVE instruction (or discrete equivalent),
5860      so it is profitable in some other cases as well.  Especially when there
5861      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5862      and there is exactly one register to pop. This heuristic may need some
5863      tuning in future.  */
5864   if ((!sp_valid && frame.nregs <= 1)
5865       || (TARGET_EPILOGUE_USING_MOVE
5866           && cfun->machine->use_fast_prologue_epilogue
5867           && (frame.nregs > 1 || frame.to_allocate))
5868       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5869       || (frame_pointer_needed && TARGET_USE_LEAVE
5870           && cfun->machine->use_fast_prologue_epilogue
5871           && frame.nregs == 1)
5872       || current_function_calls_eh_return)
5873     {
5874       /* Restore registers.  We can use ebp or esp to address the memory
5875          locations.  If both are available, default to ebp, since offsets
5876          are known to be small.  Only exception is esp pointing directly to the
5877          end of block of saved registers, where we may simplify addressing
5878          mode.  */
5879
5880       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5881         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5882                                           frame.to_allocate, style == 2);
5883       else
5884         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5885                                           offset, style == 2);
5886
5887       /* eh_return epilogues need %ecx added to the stack pointer.  */
5888       if (style == 2)
5889         {
5890           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5891
5892           if (frame_pointer_needed)
5893             {
5894               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5895               tmp = plus_constant (tmp, UNITS_PER_WORD);
5896               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5897
5898               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5899               emit_move_insn (hard_frame_pointer_rtx, tmp);
5900
5901               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5902                                          const0_rtx, style);
5903             }
5904           else
5905             {
5906               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5907               tmp = plus_constant (tmp, (frame.to_allocate
5908                                          + frame.nregs * UNITS_PER_WORD));
5909               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5910             }
5911         }
5912       else if (!frame_pointer_needed)
5913         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5914                                    GEN_INT (frame.to_allocate
5915                                             + frame.nregs * UNITS_PER_WORD),
5916                                    style);
5917       /* If not an i386, mov & pop is faster than "leave".  */
5918       else if (TARGET_USE_LEAVE || optimize_size
5919                || !cfun->machine->use_fast_prologue_epilogue)
5920         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5921       else
5922         {
5923           pro_epilogue_adjust_stack (stack_pointer_rtx,
5924                                      hard_frame_pointer_rtx,
5925                                      const0_rtx, style);
5926           if (TARGET_64BIT)
5927             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5928           else
5929             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5930         }
5931     }
5932   else
5933     {
5934       /* First step is to deallocate the stack frame so that we can
5935          pop the registers.  */
5936       if (!sp_valid)
5937         {
5938           gcc_assert (frame_pointer_needed);
5939           pro_epilogue_adjust_stack (stack_pointer_rtx,
5940                                      hard_frame_pointer_rtx,
5941                                      GEN_INT (offset), style);
5942         }
5943       else if (frame.to_allocate)
5944         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5945                                    GEN_INT (frame.to_allocate), style);
5946
5947       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5948         if (ix86_save_reg (regno, false))
5949           {
5950             if (TARGET_64BIT)
5951               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5952             else
5953               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5954           }
5955       if (frame_pointer_needed)
5956         {
5957           /* Leave results in shorter dependency chains on CPUs that are
5958              able to grok it fast.  */
5959           if (TARGET_USE_LEAVE)
5960             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5961           else if (TARGET_64BIT)
5962             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5963           else
5964             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5965         }
5966     }
5967
5968   if (cfun->machine->force_align_arg_pointer)
5969     {
5970       emit_insn (gen_addsi3 (stack_pointer_rtx,
5971                              cfun->machine->force_align_arg_pointer,
5972                              GEN_INT (-4)));
5973     }
5974
5975   /* Sibcall epilogues don't want a return instruction.  */
5976   if (style == 0)
5977     return;
5978
5979   if (current_function_pops_args && current_function_args_size)
5980     {
5981       rtx popc = GEN_INT (current_function_pops_args);
5982
5983       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5984          return address, do explicit add, and jump indirectly to the
5985          caller.  */
5986
5987       if (current_function_pops_args >= 65536)
5988         {
5989           rtx ecx = gen_rtx_REG (SImode, 2);
5990
5991           /* There is no "pascal" calling convention in 64bit ABI.  */
5992           gcc_assert (!TARGET_64BIT);
5993
5994           emit_insn (gen_popsi1 (ecx));
5995           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5996           emit_jump_insn (gen_return_indirect_internal (ecx));
5997         }
5998       else
5999         emit_jump_insn (gen_return_pop_internal (popc));
6000     }
6001   else
6002     emit_jump_insn (gen_return_internal ());
6003 }
6004
6005 /* Reset from the function's potential modifications.  */
6006
6007 static void
6008 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6009                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6010 {
6011   if (pic_offset_table_rtx)
6012     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
6013 #if TARGET_MACHO
6014   /* Mach-O doesn't support labels at the end of objects, so if
6015      it looks like we might want one, insert a NOP.  */
6016   {
6017     rtx insn = get_last_insn ();
6018     while (insn
6019            && NOTE_P (insn)
6020            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
6021       insn = PREV_INSN (insn);
6022     if (insn
6023         && (LABEL_P (insn)
6024             || (NOTE_P (insn)
6025                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
6026       fputs ("\tnop\n", file);
6027   }
6028 #endif
6029
6030 }
6031 \f
6032 /* Extract the parts of an RTL expression that is a valid memory address
6033    for an instruction.  Return 0 if the structure of the address is
6034    grossly off.  Return -1 if the address contains ASHIFT, so it is not
6035    strictly valid, but still used for computing length of lea instruction.  */
6036
6037 int
6038 ix86_decompose_address (rtx addr, struct ix86_address *out)
6039 {
6040   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
6041   rtx base_reg, index_reg;
6042   HOST_WIDE_INT scale = 1;
6043   rtx scale_rtx = NULL_RTX;
6044   int retval = 1;
6045   enum ix86_address_seg seg = SEG_DEFAULT;
6046
6047   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
6048     base = addr;
6049   else if (GET_CODE (addr) == PLUS)
6050     {
6051       rtx addends[4], op;
6052       int n = 0, i;
6053
6054       op = addr;
6055       do
6056         {
6057           if (n >= 4)
6058             return 0;
6059           addends[n++] = XEXP (op, 1);
6060           op = XEXP (op, 0);
6061         }
6062       while (GET_CODE (op) == PLUS);
6063       if (n >= 4)
6064         return 0;
6065       addends[n] = op;
6066
6067       for (i = n; i >= 0; --i)
6068         {
6069           op = addends[i];
6070           switch (GET_CODE (op))
6071             {
6072             case MULT:
6073               if (index)
6074                 return 0;
6075               index = XEXP (op, 0);
6076               scale_rtx = XEXP (op, 1);
6077               break;
6078
6079             case UNSPEC:
6080               if (XINT (op, 1) == UNSPEC_TP
6081                   && TARGET_TLS_DIRECT_SEG_REFS
6082                   && seg == SEG_DEFAULT)
6083                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
6084               else
6085                 return 0;
6086               break;
6087
6088             case REG:
6089             case SUBREG:
6090               if (!base)
6091                 base = op;
6092               else if (!index)
6093                 index = op;
6094               else
6095                 return 0;
6096               break;
6097
6098             case CONST:
6099             case CONST_INT:
6100             case SYMBOL_REF:
6101             case LABEL_REF:
6102               if (disp)
6103                 return 0;
6104               disp = op;
6105               break;
6106
6107             default:
6108               return 0;
6109             }
6110         }
6111     }
6112   else if (GET_CODE (addr) == MULT)
6113     {
6114       index = XEXP (addr, 0);           /* index*scale */
6115       scale_rtx = XEXP (addr, 1);
6116     }
6117   else if (GET_CODE (addr) == ASHIFT)
6118     {
6119       rtx tmp;
6120
6121       /* We're called for lea too, which implements ashift on occasion.  */
6122       index = XEXP (addr, 0);
6123       tmp = XEXP (addr, 1);
6124       if (!CONST_INT_P (tmp))
6125         return 0;
6126       scale = INTVAL (tmp);
6127       if ((unsigned HOST_WIDE_INT) scale > 3)
6128         return 0;
6129       scale = 1 << scale;
6130       retval = -1;
6131     }
6132   else
6133     disp = addr;                        /* displacement */
6134
6135   /* Extract the integral value of scale.  */
6136   if (scale_rtx)
6137     {
6138       if (!CONST_INT_P (scale_rtx))
6139         return 0;
6140       scale = INTVAL (scale_rtx);
6141     }
6142
6143   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
6144   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
6145
6146   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
6147   if (base_reg && index_reg && scale == 1
6148       && (index_reg == arg_pointer_rtx
6149           || index_reg == frame_pointer_rtx
6150           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
6151     {
6152       rtx tmp;
6153       tmp = base, base = index, index = tmp;
6154       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
6155     }
6156
6157   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
6158   if ((base_reg == hard_frame_pointer_rtx
6159        || base_reg == frame_pointer_rtx
6160        || base_reg == arg_pointer_rtx) && !disp)
6161     disp = const0_rtx;
6162
6163   /* Special case: on K6, [%esi] makes the instruction vector decoded.
6164      Avoid this by transforming to [%esi+0].  */
6165   if (ix86_tune == PROCESSOR_K6 && !optimize_size
6166       && base_reg && !index_reg && !disp
6167       && REG_P (base_reg)
6168       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
6169     disp = const0_rtx;
6170
6171   /* Special case: encode reg+reg instead of reg*2.  */
6172   if (!base && index && scale && scale == 2)
6173     base = index, base_reg = index_reg, scale = 1;
6174
6175   /* Special case: scaling cannot be encoded without base or displacement.  */
6176   if (!base && !disp && index && scale != 1)
6177     disp = const0_rtx;
6178
6179   out->base = base;
6180   out->index = index;
6181   out->disp = disp;
6182   out->scale = scale;
6183   out->seg = seg;
6184
6185   return retval;
6186 }
6187 \f
6188 /* Return cost of the memory address x.
6189    For i386, it is better to use a complex address than let gcc copy
6190    the address into a reg and make a new pseudo.  But not if the address
6191    requires to two regs - that would mean more pseudos with longer
6192    lifetimes.  */
6193 static int
6194 ix86_address_cost (rtx x)
6195 {
6196   struct ix86_address parts;
6197   int cost = 1;
6198   int ok = ix86_decompose_address (x, &parts);
6199
6200   gcc_assert (ok);
6201
6202   if (parts.base && GET_CODE (parts.base) == SUBREG)
6203     parts.base = SUBREG_REG (parts.base);
6204   if (parts.index && GET_CODE (parts.index) == SUBREG)
6205     parts.index = SUBREG_REG (parts.index);
6206
6207   /* More complex memory references are better.  */
6208   if (parts.disp && parts.disp != const0_rtx)
6209     cost--;
6210   if (parts.seg != SEG_DEFAULT)
6211     cost--;
6212
6213   /* Attempt to minimize number of registers in the address.  */
6214   if ((parts.base
6215        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
6216       || (parts.index
6217           && (!REG_P (parts.index)
6218               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
6219     cost++;
6220
6221   if (parts.base
6222       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
6223       && parts.index
6224       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
6225       && parts.base != parts.index)
6226     cost++;
6227
6228   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
6229      since it's predecode logic can't detect the length of instructions
6230      and it degenerates to vector decoded.  Increase cost of such
6231      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
6232      to split such addresses or even refuse such addresses at all.
6233
6234      Following addressing modes are affected:
6235       [base+scale*index]
6236       [scale*index+disp]
6237       [base+index]
6238
6239      The first and last case  may be avoidable by explicitly coding the zero in
6240      memory address, but I don't have AMD-K6 machine handy to check this
6241      theory.  */
6242
6243   if (TARGET_K6
6244       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
6245           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
6246           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
6247     cost += 10;
6248
6249   return cost;
6250 }
6251 \f
6252 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6253    this is used for to form addresses to local data when -fPIC is in
6254    use.  */
6255
6256 static bool
6257 darwin_local_data_pic (rtx disp)
6258 {
6259   if (GET_CODE (disp) == MINUS)
6260     {
6261       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6262           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6263         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6264           {
6265             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6266             if (! strcmp (sym_name, "<pic base>"))
6267               return true;
6268           }
6269     }
6270
6271   return false;
6272 }
6273
6274 /* Determine if a given RTX is a valid constant.  We already know this
6275    satisfies CONSTANT_P.  */
6276
6277 bool
6278 legitimate_constant_p (rtx x)
6279 {
6280   switch (GET_CODE (x))
6281     {
6282     case CONST:
6283       x = XEXP (x, 0);
6284
6285       if (GET_CODE (x) == PLUS)
6286         {
6287           if (!CONST_INT_P (XEXP (x, 1)))
6288             return false;
6289           x = XEXP (x, 0);
6290         }
6291
6292       if (TARGET_MACHO && darwin_local_data_pic (x))
6293         return true;
6294
6295       /* Only some unspecs are valid as "constants".  */
6296       if (GET_CODE (x) == UNSPEC)
6297         switch (XINT (x, 1))
6298           {
6299           case UNSPEC_GOT:
6300           case UNSPEC_GOTOFF:
6301           case UNSPEC_PLTOFF:
6302             return TARGET_64BIT;
6303           case UNSPEC_TPOFF:
6304           case UNSPEC_NTPOFF:
6305             x = XVECEXP (x, 0, 0);
6306             return (GET_CODE (x) == SYMBOL_REF
6307                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6308           case UNSPEC_DTPOFF:
6309             x = XVECEXP (x, 0, 0);
6310             return (GET_CODE (x) == SYMBOL_REF
6311                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6312           default:
6313             return false;
6314           }
6315
6316       /* We must have drilled down to a symbol.  */
6317       if (GET_CODE (x) == LABEL_REF)
6318         return true;
6319       if (GET_CODE (x) != SYMBOL_REF)
6320         return false;
6321       /* FALLTHRU */
6322
6323     case SYMBOL_REF:
6324       /* TLS symbols are never valid.  */
6325       if (SYMBOL_REF_TLS_MODEL (x))
6326         return false;
6327       break;
6328
6329     case CONST_DOUBLE:
6330       if (GET_MODE (x) == TImode
6331           && x != CONST0_RTX (TImode)
6332           && !TARGET_64BIT)
6333         return false;
6334       break;
6335
6336     case CONST_VECTOR:
6337       if (x == CONST0_RTX (GET_MODE (x)))
6338         return true;
6339       return false;
6340
6341     default:
6342       break;
6343     }
6344
6345   /* Otherwise we handle everything else in the move patterns.  */
6346   return true;
6347 }
6348
6349 /* Determine if it's legal to put X into the constant pool.  This
6350    is not possible for the address of thread-local symbols, which
6351    is checked above.  */
6352
6353 static bool
6354 ix86_cannot_force_const_mem (rtx x)
6355 {
6356   /* We can always put integral constants and vectors in memory.  */
6357   switch (GET_CODE (x))
6358     {
6359     case CONST_INT:
6360     case CONST_DOUBLE:
6361     case CONST_VECTOR:
6362       return false;
6363
6364     default:
6365       break;
6366     }
6367   return !legitimate_constant_p (x);
6368 }
6369
6370 /* Determine if a given RTX is a valid constant address.  */
6371
6372 bool
6373 constant_address_p (rtx x)
6374 {
6375   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6376 }
6377
6378 /* Nonzero if the constant value X is a legitimate general operand
6379    when generating PIC code.  It is given that flag_pic is on and
6380    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6381
6382 bool
6383 legitimate_pic_operand_p (rtx x)
6384 {
6385   rtx inner;
6386
6387   switch (GET_CODE (x))
6388     {
6389     case CONST:
6390       inner = XEXP (x, 0);
6391       if (GET_CODE (inner) == PLUS
6392           && CONST_INT_P (XEXP (inner, 1)))
6393         inner = XEXP (inner, 0);
6394
6395       /* Only some unspecs are valid as "constants".  */
6396       if (GET_CODE (inner) == UNSPEC)
6397         switch (XINT (inner, 1))
6398           {
6399           case UNSPEC_GOT:
6400           case UNSPEC_GOTOFF:
6401           case UNSPEC_PLTOFF:
6402             return TARGET_64BIT;
6403           case UNSPEC_TPOFF:
6404             x = XVECEXP (inner, 0, 0);
6405             return (GET_CODE (x) == SYMBOL_REF
6406                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6407           default:
6408             return false;
6409           }
6410       /* FALLTHRU */
6411
6412     case SYMBOL_REF:
6413     case LABEL_REF:
6414       return legitimate_pic_address_disp_p (x);
6415
6416     default:
6417       return true;
6418     }
6419 }
6420
6421 /* Determine if a given CONST RTX is a valid memory displacement
6422    in PIC mode.  */
6423
6424 int
6425 legitimate_pic_address_disp_p (rtx disp)
6426 {
6427   bool saw_plus;
6428
6429   /* In 64bit mode we can allow direct addresses of symbols and labels
6430      when they are not dynamic symbols.  */
6431   if (TARGET_64BIT)
6432     {
6433       rtx op0 = disp, op1;
6434
6435       switch (GET_CODE (disp))
6436         {
6437         case LABEL_REF:
6438           return true;
6439
6440         case CONST:
6441           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6442             break;
6443           op0 = XEXP (XEXP (disp, 0), 0);
6444           op1 = XEXP (XEXP (disp, 0), 1);
6445           if (!CONST_INT_P (op1)
6446               || INTVAL (op1) >= 16*1024*1024
6447               || INTVAL (op1) < -16*1024*1024)
6448             break;
6449           if (GET_CODE (op0) == LABEL_REF)
6450             return true;
6451           if (GET_CODE (op0) != SYMBOL_REF)
6452             break;
6453           /* FALLTHRU */
6454
6455         case SYMBOL_REF:
6456           /* TLS references should always be enclosed in UNSPEC.  */
6457           if (SYMBOL_REF_TLS_MODEL (op0))
6458             return false;
6459           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
6460               && ix86_cmodel != CM_LARGE_PIC)
6461             return true;
6462           break;
6463
6464         default:
6465           break;
6466         }
6467     }
6468   if (GET_CODE (disp) != CONST)
6469     return 0;
6470   disp = XEXP (disp, 0);
6471
6472   if (TARGET_64BIT)
6473     {
6474       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6475          of GOT tables.  We should not need these anyway.  */
6476       if (GET_CODE (disp) != UNSPEC
6477           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6478               && XINT (disp, 1) != UNSPEC_GOTOFF
6479               && XINT (disp, 1) != UNSPEC_PLTOFF))
6480         return 0;
6481
6482       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6483           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6484         return 0;
6485       return 1;
6486     }
6487
6488   saw_plus = false;
6489   if (GET_CODE (disp) == PLUS)
6490     {
6491       if (!CONST_INT_P (XEXP (disp, 1)))
6492         return 0;
6493       disp = XEXP (disp, 0);
6494       saw_plus = true;
6495     }
6496
6497   if (TARGET_MACHO && darwin_local_data_pic (disp))
6498     return 1;
6499
6500   if (GET_CODE (disp) != UNSPEC)
6501     return 0;
6502
6503   switch (XINT (disp, 1))
6504     {
6505     case UNSPEC_GOT:
6506       if (saw_plus)
6507         return false;
6508       /* We need to check for both symbols and labels because VxWorks loads
6509          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
6510          details.  */
6511       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6512               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
6513     case UNSPEC_GOTOFF:
6514       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6515          While ABI specify also 32bit relocation but we don't produce it in
6516          small PIC model at all.  */
6517       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6518            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6519           && !TARGET_64BIT)
6520         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
6521       return false;
6522     case UNSPEC_GOTTPOFF:
6523     case UNSPEC_GOTNTPOFF:
6524     case UNSPEC_INDNTPOFF:
6525       if (saw_plus)
6526         return false;
6527       disp = XVECEXP (disp, 0, 0);
6528       return (GET_CODE (disp) == SYMBOL_REF
6529               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6530     case UNSPEC_NTPOFF:
6531       disp = XVECEXP (disp, 0, 0);
6532       return (GET_CODE (disp) == SYMBOL_REF
6533               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6534     case UNSPEC_DTPOFF:
6535       disp = XVECEXP (disp, 0, 0);
6536       return (GET_CODE (disp) == SYMBOL_REF
6537               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6538     }
6539
6540   return 0;
6541 }
6542
6543 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6544    memory address for an instruction.  The MODE argument is the machine mode
6545    for the MEM expression that wants to use this address.
6546
6547    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6548    convert common non-canonical forms to canonical form so that they will
6549    be recognized.  */
6550
6551 int
6552 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
6553                       rtx addr, int strict)
6554 {
6555   struct ix86_address parts;
6556   rtx base, index, disp;
6557   HOST_WIDE_INT scale;
6558   const char *reason = NULL;
6559   rtx reason_rtx = NULL_RTX;
6560
6561   if (ix86_decompose_address (addr, &parts) <= 0)
6562     {
6563       reason = "decomposition failed";
6564       goto report_error;
6565     }
6566
6567   base = parts.base;
6568   index = parts.index;
6569   disp = parts.disp;
6570   scale = parts.scale;
6571
6572   /* Validate base register.
6573
6574      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6575      failures when the base is one word out of a two word structure, which is
6576      represented internally as a DImode int.  */
6577
6578   if (base)
6579     {
6580       rtx reg;
6581       reason_rtx = base;
6582
6583       if (REG_P (base))
6584         reg = base;
6585       else if (GET_CODE (base) == SUBREG
6586                && REG_P (SUBREG_REG (base))
6587                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6588                   <= UNITS_PER_WORD)
6589         reg = SUBREG_REG (base);
6590       else
6591         {
6592           reason = "base is not a register";
6593           goto report_error;
6594         }
6595
6596       if (GET_MODE (base) != Pmode)
6597         {
6598           reason = "base is not in Pmode";
6599           goto report_error;
6600         }
6601
6602       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6603           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6604         {
6605           reason = "base is not valid";
6606           goto report_error;
6607         }
6608     }
6609
6610   /* Validate index register.
6611
6612      Don't allow SUBREG's that span more than a word here -- same as above.  */
6613
6614   if (index)
6615     {
6616       rtx reg;
6617       reason_rtx = index;
6618
6619       if (REG_P (index))
6620         reg = index;
6621       else if (GET_CODE (index) == SUBREG
6622                && REG_P (SUBREG_REG (index))
6623                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6624                   <= UNITS_PER_WORD)
6625         reg = SUBREG_REG (index);
6626       else
6627         {
6628           reason = "index is not a register";
6629           goto report_error;
6630         }
6631
6632       if (GET_MODE (index) != Pmode)
6633         {
6634           reason = "index is not in Pmode";
6635           goto report_error;
6636         }
6637
6638       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6639           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6640         {
6641           reason = "index is not valid";
6642           goto report_error;
6643         }
6644     }
6645
6646   /* Validate scale factor.  */
6647   if (scale != 1)
6648     {
6649       reason_rtx = GEN_INT (scale);
6650       if (!index)
6651         {
6652           reason = "scale without index";
6653           goto report_error;
6654         }
6655
6656       if (scale != 2 && scale != 4 && scale != 8)
6657         {
6658           reason = "scale is not a valid multiplier";
6659           goto report_error;
6660         }
6661     }
6662
6663   /* Validate displacement.  */
6664   if (disp)
6665     {
6666       reason_rtx = disp;
6667
6668       if (GET_CODE (disp) == CONST
6669           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6670         switch (XINT (XEXP (disp, 0), 1))
6671           {
6672           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6673              used.  While ABI specify also 32bit relocations, we don't produce
6674              them at all and use IP relative instead.  */
6675           case UNSPEC_GOT:
6676           case UNSPEC_GOTOFF:
6677             gcc_assert (flag_pic);
6678             if (!TARGET_64BIT)
6679               goto is_legitimate_pic;
6680             reason = "64bit address unspec";
6681             goto report_error;
6682
6683           case UNSPEC_GOTPCREL:
6684             gcc_assert (flag_pic);
6685             goto is_legitimate_pic;
6686
6687           case UNSPEC_GOTTPOFF:
6688           case UNSPEC_GOTNTPOFF:
6689           case UNSPEC_INDNTPOFF:
6690           case UNSPEC_NTPOFF:
6691           case UNSPEC_DTPOFF:
6692             break;
6693
6694           default:
6695             reason = "invalid address unspec";
6696             goto report_error;
6697           }
6698
6699       else if (SYMBOLIC_CONST (disp)
6700                && (flag_pic
6701                    || (TARGET_MACHO
6702 #if TARGET_MACHO
6703                        && MACHOPIC_INDIRECT
6704                        && !machopic_operand_p (disp)
6705 #endif
6706                )))
6707         {
6708
6709         is_legitimate_pic:
6710           if (TARGET_64BIT && (index || base))
6711             {
6712               /* foo@dtpoff(%rX) is ok.  */
6713               if (GET_CODE (disp) != CONST
6714                   || GET_CODE (XEXP (disp, 0)) != PLUS
6715                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6716                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
6717                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6718                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6719                 {
6720                   reason = "non-constant pic memory reference";
6721                   goto report_error;
6722                 }
6723             }
6724           else if (! legitimate_pic_address_disp_p (disp))
6725             {
6726               reason = "displacement is an invalid pic construct";
6727               goto report_error;
6728             }
6729
6730           /* This code used to verify that a symbolic pic displacement
6731              includes the pic_offset_table_rtx register.
6732
6733              While this is good idea, unfortunately these constructs may
6734              be created by "adds using lea" optimization for incorrect
6735              code like:
6736
6737              int a;
6738              int foo(int i)
6739                {
6740                  return *(&a+i);
6741                }
6742
6743              This code is nonsensical, but results in addressing
6744              GOT table with pic_offset_table_rtx base.  We can't
6745              just refuse it easily, since it gets matched by
6746              "addsi3" pattern, that later gets split to lea in the
6747              case output register differs from input.  While this
6748              can be handled by separate addsi pattern for this case
6749              that never results in lea, this seems to be easier and
6750              correct fix for crash to disable this test.  */
6751         }
6752       else if (GET_CODE (disp) != LABEL_REF
6753                && !CONST_INT_P (disp)
6754                && (GET_CODE (disp) != CONST
6755                    || !legitimate_constant_p (disp))
6756                && (GET_CODE (disp) != SYMBOL_REF
6757                    || !legitimate_constant_p (disp)))
6758         {
6759           reason = "displacement is not constant";
6760           goto report_error;
6761         }
6762       else if (TARGET_64BIT
6763                && !x86_64_immediate_operand (disp, VOIDmode))
6764         {
6765           reason = "displacement is out of range";
6766           goto report_error;
6767         }
6768     }
6769
6770   /* Everything looks valid.  */
6771   return TRUE;
6772
6773  report_error:
6774   return FALSE;
6775 }
6776 \f
6777 /* Return a unique alias set for the GOT.  */
6778
6779 static HOST_WIDE_INT
6780 ix86_GOT_alias_set (void)
6781 {
6782   static HOST_WIDE_INT set = -1;
6783   if (set == -1)
6784     set = new_alias_set ();
6785   return set;
6786 }
6787
6788 /* Return a legitimate reference for ORIG (an address) using the
6789    register REG.  If REG is 0, a new pseudo is generated.
6790
6791    There are two types of references that must be handled:
6792
6793    1. Global data references must load the address from the GOT, via
6794       the PIC reg.  An insn is emitted to do this load, and the reg is
6795       returned.
6796
6797    2. Static data references, constant pool addresses, and code labels
6798       compute the address as an offset from the GOT, whose base is in
6799       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6800       differentiate them from global data objects.  The returned
6801       address is the PIC reg + an unspec constant.
6802
6803    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6804    reg also appears in the address.  */
6805
6806 static rtx
6807 legitimize_pic_address (rtx orig, rtx reg)
6808 {
6809   rtx addr = orig;
6810   rtx new = orig;
6811   rtx base;
6812
6813 #if TARGET_MACHO
6814   if (TARGET_MACHO && !TARGET_64BIT)
6815     {
6816       if (reg == 0)
6817         reg = gen_reg_rtx (Pmode);
6818       /* Use the generic Mach-O PIC machinery.  */
6819       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6820     }
6821 #endif
6822
6823   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6824     new = addr;
6825   else if (TARGET_64BIT
6826            && ix86_cmodel != CM_SMALL_PIC
6827            && gotoff_operand (addr, Pmode))
6828     {
6829       rtx tmpreg;
6830       /* This symbol may be referenced via a displacement from the PIC
6831          base address (@GOTOFF).  */
6832
6833       if (reload_in_progress)
6834         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6835       if (GET_CODE (addr) == CONST)
6836         addr = XEXP (addr, 0);
6837       if (GET_CODE (addr) == PLUS)
6838           {
6839             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6840             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6841           }
6842         else
6843           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6844       new = gen_rtx_CONST (Pmode, new);
6845       if (!reg)
6846         tmpreg = gen_reg_rtx (Pmode);
6847       else
6848         tmpreg = reg;
6849       emit_move_insn (tmpreg, new);
6850
6851       if (reg != 0)
6852         {
6853           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6854                                      tmpreg, 1, OPTAB_DIRECT);
6855           new = reg;
6856         }
6857       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6858     }
6859   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
6860     {
6861       /* This symbol may be referenced via a displacement from the PIC
6862          base address (@GOTOFF).  */
6863
6864       if (reload_in_progress)
6865         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6866       if (GET_CODE (addr) == CONST)
6867         addr = XEXP (addr, 0);
6868       if (GET_CODE (addr) == PLUS)
6869           {
6870             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6871             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6872           }
6873         else
6874           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6875       new = gen_rtx_CONST (Pmode, new);
6876       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6877
6878       if (reg != 0)
6879         {
6880           emit_move_insn (reg, new);
6881           new = reg;
6882         }
6883     }
6884   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
6885            /* We can't use @GOTOFF for text labels on VxWorks;
6886               see gotoff_operand.  */
6887            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
6888     {
6889       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
6890         {
6891           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6892           new = gen_rtx_CONST (Pmode, new);
6893           new = gen_const_mem (Pmode, new);
6894           set_mem_alias_set (new, ix86_GOT_alias_set ());
6895
6896           if (reg == 0)
6897             reg = gen_reg_rtx (Pmode);
6898           /* Use directly gen_movsi, otherwise the address is loaded
6899              into register for CSE.  We don't want to CSE this addresses,
6900              instead we CSE addresses from the GOT table, so skip this.  */
6901           emit_insn (gen_movsi (reg, new));
6902           new = reg;
6903         }
6904       else
6905         {
6906           /* This symbol must be referenced via a load from the
6907              Global Offset Table (@GOT).  */
6908
6909           if (reload_in_progress)
6910             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6911           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6912           new = gen_rtx_CONST (Pmode, new);
6913           if (TARGET_64BIT)
6914             new = force_reg (Pmode, new);
6915           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6916           new = gen_const_mem (Pmode, new);
6917           set_mem_alias_set (new, ix86_GOT_alias_set ());
6918
6919           if (reg == 0)
6920             reg = gen_reg_rtx (Pmode);
6921           emit_move_insn (reg, new);
6922           new = reg;
6923         }
6924     }
6925   else
6926     {
6927       if (CONST_INT_P (addr)
6928           && !x86_64_immediate_operand (addr, VOIDmode))
6929         {
6930           if (reg)
6931             {
6932               emit_move_insn (reg, addr);
6933               new = reg;
6934             }
6935           else
6936             new = force_reg (Pmode, addr);
6937         }
6938       else if (GET_CODE (addr) == CONST)
6939         {
6940           addr = XEXP (addr, 0);
6941
6942           /* We must match stuff we generate before.  Assume the only
6943              unspecs that can get here are ours.  Not that we could do
6944              anything with them anyway....  */
6945           if (GET_CODE (addr) == UNSPEC
6946               || (GET_CODE (addr) == PLUS
6947                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6948             return orig;
6949           gcc_assert (GET_CODE (addr) == PLUS);
6950         }
6951       if (GET_CODE (addr) == PLUS)
6952         {
6953           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6954
6955           /* Check first to see if this is a constant offset from a @GOTOFF
6956              symbol reference.  */
6957           if (gotoff_operand (op0, Pmode)
6958               && CONST_INT_P (op1))
6959             {
6960               if (!TARGET_64BIT)
6961                 {
6962                   if (reload_in_progress)
6963                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6964                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6965                                         UNSPEC_GOTOFF);
6966                   new = gen_rtx_PLUS (Pmode, new, op1);
6967                   new = gen_rtx_CONST (Pmode, new);
6968                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6969
6970                   if (reg != 0)
6971                     {
6972                       emit_move_insn (reg, new);
6973                       new = reg;
6974                     }
6975                 }
6976               else
6977                 {
6978                   if (INTVAL (op1) < -16*1024*1024
6979                       || INTVAL (op1) >= 16*1024*1024)
6980                     {
6981                       if (!x86_64_immediate_operand (op1, Pmode))
6982                         op1 = force_reg (Pmode, op1);
6983                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
6984                     }
6985                 }
6986             }
6987           else
6988             {
6989               base = legitimize_pic_address (XEXP (addr, 0), reg);
6990               new  = legitimize_pic_address (XEXP (addr, 1),
6991                                              base == reg ? NULL_RTX : reg);
6992
6993               if (CONST_INT_P (new))
6994                 new = plus_constant (base, INTVAL (new));
6995               else
6996                 {
6997                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6998                     {
6999                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
7000                       new = XEXP (new, 1);
7001                     }
7002                   new = gen_rtx_PLUS (Pmode, base, new);
7003                 }
7004             }
7005         }
7006     }
7007   return new;
7008 }
7009 \f
7010 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
7011
7012 static rtx
7013 get_thread_pointer (int to_reg)
7014 {
7015   rtx tp, reg, insn;
7016
7017   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
7018   if (!to_reg)
7019     return tp;
7020
7021   reg = gen_reg_rtx (Pmode);
7022   insn = gen_rtx_SET (VOIDmode, reg, tp);
7023   insn = emit_insn (insn);
7024
7025   return reg;
7026 }
7027
7028 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
7029    false if we expect this to be used for a memory address and true if
7030    we expect to load the address into a register.  */
7031
7032 static rtx
7033 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
7034 {
7035   rtx dest, base, off, pic, tp;
7036   int type;
7037
7038   switch (model)
7039     {
7040     case TLS_MODEL_GLOBAL_DYNAMIC:
7041       dest = gen_reg_rtx (Pmode);
7042       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7043
7044       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7045         {
7046           rtx rax = gen_rtx_REG (Pmode, 0), insns;
7047
7048           start_sequence ();
7049           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
7050           insns = get_insns ();
7051           end_sequence ();
7052
7053           emit_libcall_block (insns, dest, rax, x);
7054         }
7055       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7056         emit_insn (gen_tls_global_dynamic_64 (dest, x));
7057       else
7058         emit_insn (gen_tls_global_dynamic_32 (dest, x));
7059
7060       if (TARGET_GNU2_TLS)
7061         {
7062           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
7063
7064           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7065         }
7066       break;
7067
7068     case TLS_MODEL_LOCAL_DYNAMIC:
7069       base = gen_reg_rtx (Pmode);
7070       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7071
7072       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7073         {
7074           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
7075
7076           start_sequence ();
7077           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
7078           insns = get_insns ();
7079           end_sequence ();
7080
7081           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
7082           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
7083           emit_libcall_block (insns, base, rax, note);
7084         }
7085       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7086         emit_insn (gen_tls_local_dynamic_base_64 (base));
7087       else
7088         emit_insn (gen_tls_local_dynamic_base_32 (base));
7089
7090       if (TARGET_GNU2_TLS)
7091         {
7092           rtx x = ix86_tls_module_base ();
7093
7094           set_unique_reg_note (get_last_insn (), REG_EQUIV,
7095                                gen_rtx_MINUS (Pmode, x, tp));
7096         }
7097
7098       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
7099       off = gen_rtx_CONST (Pmode, off);
7100
7101       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
7102
7103       if (TARGET_GNU2_TLS)
7104         {
7105           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
7106
7107           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7108         }
7109
7110       break;
7111
7112     case TLS_MODEL_INITIAL_EXEC:
7113       if (TARGET_64BIT)
7114         {
7115           pic = NULL;
7116           type = UNSPEC_GOTNTPOFF;
7117         }
7118       else if (flag_pic)
7119         {
7120           if (reload_in_progress)
7121             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7122           pic = pic_offset_table_rtx;
7123           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
7124         }
7125       else if (!TARGET_ANY_GNU_TLS)
7126         {
7127           pic = gen_reg_rtx (Pmode);
7128           emit_insn (gen_set_got (pic));
7129           type = UNSPEC_GOTTPOFF;
7130         }
7131       else
7132         {
7133           pic = NULL;
7134           type = UNSPEC_INDNTPOFF;
7135         }
7136
7137       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
7138       off = gen_rtx_CONST (Pmode, off);
7139       if (pic)
7140         off = gen_rtx_PLUS (Pmode, pic, off);
7141       off = gen_const_mem (Pmode, off);
7142       set_mem_alias_set (off, ix86_GOT_alias_set ());
7143
7144       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7145         {
7146           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7147           off = force_reg (Pmode, off);
7148           return gen_rtx_PLUS (Pmode, base, off);
7149         }
7150       else
7151         {
7152           base = get_thread_pointer (true);
7153           dest = gen_reg_rtx (Pmode);
7154           emit_insn (gen_subsi3 (dest, base, off));
7155         }
7156       break;
7157
7158     case TLS_MODEL_LOCAL_EXEC:
7159       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
7160                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7161                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
7162       off = gen_rtx_CONST (Pmode, off);
7163
7164       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7165         {
7166           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7167           return gen_rtx_PLUS (Pmode, base, off);
7168         }
7169       else
7170         {
7171           base = get_thread_pointer (true);
7172           dest = gen_reg_rtx (Pmode);
7173           emit_insn (gen_subsi3 (dest, base, off));
7174         }
7175       break;
7176
7177     default:
7178       gcc_unreachable ();
7179     }
7180
7181   return dest;
7182 }
7183
7184 /* Try machine-dependent ways of modifying an illegitimate address
7185    to be legitimate.  If we find one, return the new, valid address.
7186    This macro is used in only one place: `memory_address' in explow.c.
7187
7188    OLDX is the address as it was before break_out_memory_refs was called.
7189    In some cases it is useful to look at this to decide what needs to be done.
7190
7191    MODE and WIN are passed so that this macro can use
7192    GO_IF_LEGITIMATE_ADDRESS.
7193
7194    It is always safe for this macro to do nothing.  It exists to recognize
7195    opportunities to optimize the output.
7196
7197    For the 80386, we handle X+REG by loading X into a register R and
7198    using R+REG.  R will go in a general reg and indexing will be used.
7199    However, if REG is a broken-out memory address or multiplication,
7200    nothing needs to be done because REG can certainly go in a general reg.
7201
7202    When -fpic is used, special handling is needed for symbolic references.
7203    See comments by legitimize_pic_address in i386.c for details.  */
7204
7205 rtx
7206 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
7207 {
7208   int changed = 0;
7209   unsigned log;
7210
7211   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
7212   if (log)
7213     return legitimize_tls_address (x, log, false);
7214   if (GET_CODE (x) == CONST
7215       && GET_CODE (XEXP (x, 0)) == PLUS
7216       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7217       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
7218     {
7219       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
7220       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7221     }
7222
7223   if (flag_pic && SYMBOLIC_CONST (x))
7224     return legitimize_pic_address (x, 0);
7225
7226   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
7227   if (GET_CODE (x) == ASHIFT
7228       && CONST_INT_P (XEXP (x, 1))
7229       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
7230     {
7231       changed = 1;
7232       log = INTVAL (XEXP (x, 1));
7233       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
7234                         GEN_INT (1 << log));
7235     }
7236
7237   if (GET_CODE (x) == PLUS)
7238     {
7239       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
7240
7241       if (GET_CODE (XEXP (x, 0)) == ASHIFT
7242           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7243           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
7244         {
7245           changed = 1;
7246           log = INTVAL (XEXP (XEXP (x, 0), 1));
7247           XEXP (x, 0) = gen_rtx_MULT (Pmode,
7248                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
7249                                       GEN_INT (1 << log));
7250         }
7251
7252       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7253           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
7254           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7255         {
7256           changed = 1;
7257           log = INTVAL (XEXP (XEXP (x, 1), 1));
7258           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7259                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7260                                       GEN_INT (1 << log));
7261         }
7262
7263       /* Put multiply first if it isn't already.  */
7264       if (GET_CODE (XEXP (x, 1)) == MULT)
7265         {
7266           rtx tmp = XEXP (x, 0);
7267           XEXP (x, 0) = XEXP (x, 1);
7268           XEXP (x, 1) = tmp;
7269           changed = 1;
7270         }
7271
7272       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7273          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7274          created by virtual register instantiation, register elimination, and
7275          similar optimizations.  */
7276       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7277         {
7278           changed = 1;
7279           x = gen_rtx_PLUS (Pmode,
7280                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7281                                           XEXP (XEXP (x, 1), 0)),
7282                             XEXP (XEXP (x, 1), 1));
7283         }
7284
7285       /* Canonicalize
7286          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7287          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7288       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7289                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7290                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7291                && CONSTANT_P (XEXP (x, 1)))
7292         {
7293           rtx constant;
7294           rtx other = NULL_RTX;
7295
7296           if (CONST_INT_P (XEXP (x, 1)))
7297             {
7298               constant = XEXP (x, 1);
7299               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7300             }
7301           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
7302             {
7303               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7304               other = XEXP (x, 1);
7305             }
7306           else
7307             constant = 0;
7308
7309           if (constant)
7310             {
7311               changed = 1;
7312               x = gen_rtx_PLUS (Pmode,
7313                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7314                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7315                                 plus_constant (other, INTVAL (constant)));
7316             }
7317         }
7318
7319       if (changed && legitimate_address_p (mode, x, FALSE))
7320         return x;
7321
7322       if (GET_CODE (XEXP (x, 0)) == MULT)
7323         {
7324           changed = 1;
7325           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7326         }
7327
7328       if (GET_CODE (XEXP (x, 1)) == MULT)
7329         {
7330           changed = 1;
7331           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7332         }
7333
7334       if (changed
7335           && REG_P (XEXP (x, 1))
7336           && REG_P (XEXP (x, 0)))
7337         return x;
7338
7339       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7340         {
7341           changed = 1;
7342           x = legitimize_pic_address (x, 0);
7343         }
7344
7345       if (changed && legitimate_address_p (mode, x, FALSE))
7346         return x;
7347
7348       if (REG_P (XEXP (x, 0)))
7349         {
7350           rtx temp = gen_reg_rtx (Pmode);
7351           rtx val  = force_operand (XEXP (x, 1), temp);
7352           if (val != temp)
7353             emit_move_insn (temp, val);
7354
7355           XEXP (x, 1) = temp;
7356           return x;
7357         }
7358
7359       else if (REG_P (XEXP (x, 1)))
7360         {
7361           rtx temp = gen_reg_rtx (Pmode);
7362           rtx val  = force_operand (XEXP (x, 0), temp);
7363           if (val != temp)
7364             emit_move_insn (temp, val);
7365
7366           XEXP (x, 0) = temp;
7367           return x;
7368         }
7369     }
7370
7371   return x;
7372 }
7373 \f
7374 /* Print an integer constant expression in assembler syntax.  Addition
7375    and subtraction are the only arithmetic that may appear in these
7376    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7377    CODE is the operand print code from the output string.  */
7378
7379 static void
7380 output_pic_addr_const (FILE *file, rtx x, int code)
7381 {
7382   char buf[256];
7383
7384   switch (GET_CODE (x))
7385     {
7386     case PC:
7387       gcc_assert (flag_pic);
7388       putc ('.', file);
7389       break;
7390
7391     case SYMBOL_REF:
7392       if (! TARGET_MACHO || TARGET_64BIT)
7393         output_addr_const (file, x);
7394       else
7395         {
7396           const char *name = XSTR (x, 0);
7397
7398           /* Mark the decl as referenced so that cgraph will output the function.  */
7399           if (SYMBOL_REF_DECL (x))
7400             mark_decl_referenced (SYMBOL_REF_DECL (x));
7401
7402 #if TARGET_MACHO
7403           if (MACHOPIC_INDIRECT
7404               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
7405             name = machopic_indirection_name (x, /*stub_p=*/true);
7406 #endif
7407           assemble_name (file, name);
7408         }
7409       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7410         fputs ("@PLT", file);
7411       break;
7412
7413     case LABEL_REF:
7414       x = XEXP (x, 0);
7415       /* FALLTHRU */
7416     case CODE_LABEL:
7417       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7418       assemble_name (asm_out_file, buf);
7419       break;
7420
7421     case CONST_INT:
7422       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7423       break;
7424
7425     case CONST:
7426       /* This used to output parentheses around the expression,
7427          but that does not work on the 386 (either ATT or BSD assembler).  */
7428       output_pic_addr_const (file, XEXP (x, 0), code);
7429       break;
7430
7431     case CONST_DOUBLE:
7432       if (GET_MODE (x) == VOIDmode)
7433         {
7434           /* We can use %d if the number is <32 bits and positive.  */
7435           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7436             fprintf (file, "0x%lx%08lx",
7437                      (unsigned long) CONST_DOUBLE_HIGH (x),
7438                      (unsigned long) CONST_DOUBLE_LOW (x));
7439           else
7440             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7441         }
7442       else
7443         /* We can't handle floating point constants;
7444            PRINT_OPERAND must handle them.  */
7445         output_operand_lossage ("floating constant misused");
7446       break;
7447
7448     case PLUS:
7449       /* Some assemblers need integer constants to appear first.  */
7450       if (CONST_INT_P (XEXP (x, 0)))
7451         {
7452           output_pic_addr_const (file, XEXP (x, 0), code);
7453           putc ('+', file);
7454           output_pic_addr_const (file, XEXP (x, 1), code);
7455         }
7456       else
7457         {
7458           gcc_assert (CONST_INT_P (XEXP (x, 1)));
7459           output_pic_addr_const (file, XEXP (x, 1), code);
7460           putc ('+', file);
7461           output_pic_addr_const (file, XEXP (x, 0), code);
7462         }
7463       break;
7464
7465     case MINUS:
7466       if (!TARGET_MACHO)
7467         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7468       output_pic_addr_const (file, XEXP (x, 0), code);
7469       putc ('-', file);
7470       output_pic_addr_const (file, XEXP (x, 1), code);
7471       if (!TARGET_MACHO)
7472         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7473       break;
7474
7475      case UNSPEC:
7476        gcc_assert (XVECLEN (x, 0) == 1);
7477        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7478        switch (XINT (x, 1))
7479         {
7480         case UNSPEC_GOT:
7481           fputs ("@GOT", file);
7482           break;
7483         case UNSPEC_GOTOFF:
7484           fputs ("@GOTOFF", file);
7485           break;
7486         case UNSPEC_PLTOFF:
7487           fputs ("@PLTOFF", file);
7488           break;
7489         case UNSPEC_GOTPCREL:
7490           fputs ("@GOTPCREL(%rip)", file);
7491           break;
7492         case UNSPEC_GOTTPOFF:
7493           /* FIXME: This might be @TPOFF in Sun ld too.  */
7494           fputs ("@GOTTPOFF", file);
7495           break;
7496         case UNSPEC_TPOFF:
7497           fputs ("@TPOFF", file);
7498           break;
7499         case UNSPEC_NTPOFF:
7500           if (TARGET_64BIT)
7501             fputs ("@TPOFF", file);
7502           else
7503             fputs ("@NTPOFF", file);
7504           break;
7505         case UNSPEC_DTPOFF:
7506           fputs ("@DTPOFF", file);
7507           break;
7508         case UNSPEC_GOTNTPOFF:
7509           if (TARGET_64BIT)
7510             fputs ("@GOTTPOFF(%rip)", file);
7511           else
7512             fputs ("@GOTNTPOFF", file);
7513           break;
7514         case UNSPEC_INDNTPOFF:
7515           fputs ("@INDNTPOFF", file);
7516           break;
7517         default:
7518           output_operand_lossage ("invalid UNSPEC as operand");
7519           break;
7520         }
7521        break;
7522
7523     default:
7524       output_operand_lossage ("invalid expression as operand");
7525     }
7526 }
7527
7528 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7529    We need to emit DTP-relative relocations.  */
7530
7531 static void ATTRIBUTE_UNUSED
7532 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7533 {
7534   fputs (ASM_LONG, file);
7535   output_addr_const (file, x);
7536   fputs ("@DTPOFF", file);
7537   switch (size)
7538     {
7539     case 4:
7540       break;
7541     case 8:
7542       fputs (", 0", file);
7543       break;
7544     default:
7545       gcc_unreachable ();
7546    }
7547 }
7548
7549 /* In the name of slightly smaller debug output, and to cater to
7550    general assembler lossage, recognize PIC+GOTOFF and turn it back
7551    into a direct symbol reference.
7552
7553    On Darwin, this is necessary to avoid a crash, because Darwin
7554    has a different PIC label for each routine but the DWARF debugging
7555    information is not associated with any particular routine, so it's
7556    necessary to remove references to the PIC label from RTL stored by
7557    the DWARF output code.  */
7558
7559 static rtx
7560 ix86_delegitimize_address (rtx orig_x)
7561 {
7562   rtx x = orig_x;
7563   /* reg_addend is NULL or a multiple of some register.  */
7564   rtx reg_addend = NULL_RTX;
7565   /* const_addend is NULL or a const_int.  */
7566   rtx const_addend = NULL_RTX;
7567   /* This is the result, or NULL.  */
7568   rtx result = NULL_RTX;
7569
7570   if (MEM_P (x))
7571     x = XEXP (x, 0);
7572
7573   if (TARGET_64BIT)
7574     {
7575       if (GET_CODE (x) != CONST
7576           || GET_CODE (XEXP (x, 0)) != UNSPEC
7577           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7578           || !MEM_P (orig_x))
7579         return orig_x;
7580       return XVECEXP (XEXP (x, 0), 0, 0);
7581     }
7582
7583   if (GET_CODE (x) != PLUS
7584       || GET_CODE (XEXP (x, 1)) != CONST)
7585     return orig_x;
7586
7587   if (REG_P (XEXP (x, 0))
7588       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7589     /* %ebx + GOT/GOTOFF */
7590     ;
7591   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7592     {
7593       /* %ebx + %reg * scale + GOT/GOTOFF */
7594       reg_addend = XEXP (x, 0);
7595       if (REG_P (XEXP (reg_addend, 0))
7596           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7597         reg_addend = XEXP (reg_addend, 1);
7598       else if (REG_P (XEXP (reg_addend, 1))
7599                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7600         reg_addend = XEXP (reg_addend, 0);
7601       else
7602         return orig_x;
7603       if (!REG_P (reg_addend)
7604           && GET_CODE (reg_addend) != MULT
7605           && GET_CODE (reg_addend) != ASHIFT)
7606         return orig_x;
7607     }
7608   else
7609     return orig_x;
7610
7611   x = XEXP (XEXP (x, 1), 0);
7612   if (GET_CODE (x) == PLUS
7613       && CONST_INT_P (XEXP (x, 1)))
7614     {
7615       const_addend = XEXP (x, 1);
7616       x = XEXP (x, 0);
7617     }
7618
7619   if (GET_CODE (x) == UNSPEC
7620       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x))
7621           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
7622     result = XVECEXP (x, 0, 0);
7623
7624   if (TARGET_MACHO && darwin_local_data_pic (x)
7625       && !MEM_P (orig_x))
7626     result = XEXP (x, 0);
7627
7628   if (! result)
7629     return orig_x;
7630
7631   if (const_addend)
7632     result = gen_rtx_PLUS (Pmode, result, const_addend);
7633   if (reg_addend)
7634     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7635   return result;
7636 }
7637
7638 /* If X is a machine specific address (i.e. a symbol or label being
7639    referenced as a displacement from the GOT implemented using an
7640    UNSPEC), then return the base term.  Otherwise return X.  */
7641
7642 rtx
7643 ix86_find_base_term (rtx x)
7644 {
7645   rtx term;
7646
7647   if (TARGET_64BIT)
7648     {
7649       if (GET_CODE (x) != CONST)
7650         return x;
7651       term = XEXP (x, 0);
7652       if (GET_CODE (term) == PLUS
7653           && (CONST_INT_P (XEXP (term, 1))
7654               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
7655         term = XEXP (term, 0);
7656       if (GET_CODE (term) != UNSPEC
7657           || XINT (term, 1) != UNSPEC_GOTPCREL)
7658         return x;
7659
7660       term = XVECEXP (term, 0, 0);
7661
7662       if (GET_CODE (term) != SYMBOL_REF
7663           && GET_CODE (term) != LABEL_REF)
7664         return x;
7665
7666       return term;
7667     }
7668
7669   term = ix86_delegitimize_address (x);
7670
7671   if (GET_CODE (term) != SYMBOL_REF
7672       && GET_CODE (term) != LABEL_REF)
7673     return x;
7674
7675   return term;
7676 }
7677 \f
7678 static void
7679 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7680                     int fp, FILE *file)
7681 {
7682   const char *suffix;
7683
7684   if (mode == CCFPmode || mode == CCFPUmode)
7685     {
7686       enum rtx_code second_code, bypass_code;
7687       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7688       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7689       code = ix86_fp_compare_code_to_integer (code);
7690       mode = CCmode;
7691     }
7692   if (reverse)
7693     code = reverse_condition (code);
7694
7695   switch (code)
7696     {
7697     case EQ:
7698       suffix = "e";
7699       break;
7700     case NE:
7701       suffix = "ne";
7702       break;
7703     case GT:
7704       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7705       suffix = "g";
7706       break;
7707     case GTU:
7708       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7709          Those same assemblers have the same but opposite lossage on cmov.  */
7710       gcc_assert (mode == CCmode);
7711       suffix = fp ? "nbe" : "a";
7712       break;
7713     case LT:
7714       switch (mode)
7715         {
7716         case CCNOmode:
7717         case CCGOCmode:
7718           suffix = "s";
7719           break;
7720
7721         case CCmode:
7722         case CCGCmode:
7723           suffix = "l";
7724           break;
7725
7726         default:
7727           gcc_unreachable ();
7728         }
7729       break;
7730     case LTU:
7731       gcc_assert (mode == CCmode);
7732       suffix = "b";
7733       break;
7734     case GE:
7735       switch (mode)
7736         {
7737         case CCNOmode:
7738         case CCGOCmode:
7739           suffix = "ns";
7740           break;
7741
7742         case CCmode:
7743         case CCGCmode:
7744           suffix = "ge";
7745           break;
7746
7747         default:
7748           gcc_unreachable ();
7749         }
7750       break;
7751     case GEU:
7752       /* ??? As above.  */
7753       gcc_assert (mode == CCmode);
7754       suffix = fp ? "nb" : "ae";
7755       break;
7756     case LE:
7757       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
7758       suffix = "le";
7759       break;
7760     case LEU:
7761       gcc_assert (mode == CCmode);
7762       suffix = "be";
7763       break;
7764     case UNORDERED:
7765       suffix = fp ? "u" : "p";
7766       break;
7767     case ORDERED:
7768       suffix = fp ? "nu" : "np";
7769       break;
7770     default:
7771       gcc_unreachable ();
7772     }
7773   fputs (suffix, file);
7774 }
7775
7776 /* Print the name of register X to FILE based on its machine mode and number.
7777    If CODE is 'w', pretend the mode is HImode.
7778    If CODE is 'b', pretend the mode is QImode.
7779    If CODE is 'k', pretend the mode is SImode.
7780    If CODE is 'q', pretend the mode is DImode.
7781    If CODE is 'h', pretend the reg is the 'high' byte register.
7782    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7783
7784 void
7785 print_reg (rtx x, int code, FILE *file)
7786 {
7787   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
7788               && REGNO (x) != FRAME_POINTER_REGNUM
7789               && REGNO (x) != FLAGS_REG
7790               && REGNO (x) != FPSR_REG
7791               && REGNO (x) != FPCR_REG);
7792
7793   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7794     putc ('%', file);
7795
7796   if (code == 'w' || MMX_REG_P (x))
7797     code = 2;
7798   else if (code == 'b')
7799     code = 1;
7800   else if (code == 'k')
7801     code = 4;
7802   else if (code == 'q')
7803     code = 8;
7804   else if (code == 'y')
7805     code = 3;
7806   else if (code == 'h')
7807     code = 0;
7808   else
7809     code = GET_MODE_SIZE (GET_MODE (x));
7810
7811   /* Irritatingly, AMD extended registers use different naming convention
7812      from the normal registers.  */
7813   if (REX_INT_REG_P (x))
7814     {
7815       gcc_assert (TARGET_64BIT);
7816       switch (code)
7817         {
7818           case 0:
7819             error ("extended registers have no high halves");
7820             break;
7821           case 1:
7822             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7823             break;
7824           case 2:
7825             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7826             break;
7827           case 4:
7828             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7829             break;
7830           case 8:
7831             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7832             break;
7833           default:
7834             error ("unsupported operand size for extended register");
7835             break;
7836         }
7837       return;
7838     }
7839   switch (code)
7840     {
7841     case 3:
7842       if (STACK_TOP_P (x))
7843         {
7844           fputs ("st(0)", file);
7845           break;
7846         }
7847       /* FALLTHRU */
7848     case 8:
7849     case 4:
7850     case 12:
7851       if (! ANY_FP_REG_P (x))
7852         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7853       /* FALLTHRU */
7854     case 16:
7855     case 2:
7856     normal:
7857       fputs (hi_reg_name[REGNO (x)], file);
7858       break;
7859     case 1:
7860       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7861         goto normal;
7862       fputs (qi_reg_name[REGNO (x)], file);
7863       break;
7864     case 0:
7865       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7866         goto normal;
7867       fputs (qi_high_reg_name[REGNO (x)], file);
7868       break;
7869     default:
7870       gcc_unreachable ();
7871     }
7872 }
7873
7874 /* Locate some local-dynamic symbol still in use by this function
7875    so that we can print its name in some tls_local_dynamic_base
7876    pattern.  */
7877
7878 static int
7879 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7880 {
7881   rtx x = *px;
7882
7883   if (GET_CODE (x) == SYMBOL_REF
7884       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7885     {
7886       cfun->machine->some_ld_name = XSTR (x, 0);
7887       return 1;
7888     }
7889
7890   return 0;
7891 }
7892
7893 static const char *
7894 get_some_local_dynamic_name (void)
7895 {
7896   rtx insn;
7897
7898   if (cfun->machine->some_ld_name)
7899     return cfun->machine->some_ld_name;
7900
7901   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7902     if (INSN_P (insn)
7903         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7904       return cfun->machine->some_ld_name;
7905
7906   gcc_unreachable ();
7907 }
7908
7909 /* Meaning of CODE:
7910    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7911    C -- print opcode suffix for set/cmov insn.
7912    c -- like C, but print reversed condition
7913    F,f -- likewise, but for floating-point.
7914    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7915         otherwise nothing
7916    R -- print the prefix for register names.
7917    z -- print the opcode suffix for the size of the current operand.
7918    * -- print a star (in certain assembler syntax)
7919    A -- print an absolute memory reference.
7920    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7921    s -- print a shift double count, followed by the assemblers argument
7922         delimiter.
7923    b -- print the QImode name of the register for the indicated operand.
7924         %b0 would print %al if operands[0] is reg 0.
7925    w --  likewise, print the HImode name of the register.
7926    k --  likewise, print the SImode name of the register.
7927    q --  likewise, print the DImode name of the register.
7928    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7929    y -- print "st(0)" instead of "st" as a register.
7930    D -- print condition for SSE cmp instruction.
7931    P -- if PIC, print an @PLT suffix.
7932    X -- don't print any sort of PIC '@' suffix for a symbol.
7933    & -- print some in-use local-dynamic symbol name.
7934    H -- print a memory address offset by 8; used for sse high-parts
7935  */
7936
7937 void
7938 print_operand (FILE *file, rtx x, int code)
7939 {
7940   if (code)
7941     {
7942       switch (code)
7943         {
7944         case '*':
7945           if (ASSEMBLER_DIALECT == ASM_ATT)
7946             putc ('*', file);
7947           return;
7948
7949         case '&':
7950           assemble_name (file, get_some_local_dynamic_name ());
7951           return;
7952
7953         case 'A':
7954           switch (ASSEMBLER_DIALECT)
7955             {
7956             case ASM_ATT:
7957               putc ('*', file);
7958               break;
7959
7960             case ASM_INTEL:
7961               /* Intel syntax. For absolute addresses, registers should not
7962                  be surrounded by braces.  */
7963               if (!REG_P (x))
7964                 {
7965                   putc ('[', file);
7966                   PRINT_OPERAND (file, x, 0);
7967                   putc (']', file);
7968                   return;
7969                 }
7970               break;
7971
7972             default:
7973               gcc_unreachable ();
7974             }
7975
7976           PRINT_OPERAND (file, x, 0);
7977           return;
7978
7979
7980         case 'L':
7981           if (ASSEMBLER_DIALECT == ASM_ATT)
7982             putc ('l', file);
7983           return;
7984
7985         case 'W':
7986           if (ASSEMBLER_DIALECT == ASM_ATT)
7987             putc ('w', file);
7988           return;
7989
7990         case 'B':
7991           if (ASSEMBLER_DIALECT == ASM_ATT)
7992             putc ('b', file);
7993           return;
7994
7995         case 'Q':
7996           if (ASSEMBLER_DIALECT == ASM_ATT)
7997             putc ('l', file);
7998           return;
7999
8000         case 'S':
8001           if (ASSEMBLER_DIALECT == ASM_ATT)
8002             putc ('s', file);
8003           return;
8004
8005         case 'T':
8006           if (ASSEMBLER_DIALECT == ASM_ATT)
8007             putc ('t', file);
8008           return;
8009
8010         case 'z':
8011           /* 387 opcodes don't get size suffixes if the operands are
8012              registers.  */
8013           if (STACK_REG_P (x))
8014             return;
8015
8016           /* Likewise if using Intel opcodes.  */
8017           if (ASSEMBLER_DIALECT == ASM_INTEL)
8018             return;
8019
8020           /* This is the size of op from size of operand.  */
8021           switch (GET_MODE_SIZE (GET_MODE (x)))
8022             {
8023             case 1:
8024               putc ('b', file);
8025               return;
8026
8027             case 2:
8028 #ifdef HAVE_GAS_FILDS_FISTS
8029               putc ('s', file);
8030 #endif
8031               return;
8032
8033             case 4:
8034               if (GET_MODE (x) == SFmode)
8035                 {
8036                   putc ('s', file);
8037                   return;
8038                 }
8039               else
8040                 putc ('l', file);
8041               return;
8042
8043             case 12:
8044             case 16:
8045               putc ('t', file);
8046               return;
8047
8048             case 8:
8049               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
8050                 {
8051 #ifdef GAS_MNEMONICS
8052                   putc ('q', file);
8053 #else
8054                   putc ('l', file);
8055                   putc ('l', file);
8056 #endif
8057                 }
8058               else
8059                 putc ('l', file);
8060               return;
8061
8062             default:
8063               gcc_unreachable ();
8064             }
8065
8066         case 'b':
8067         case 'w':
8068         case 'k':
8069         case 'q':
8070         case 'h':
8071         case 'y':
8072         case 'X':
8073         case 'P':
8074           break;
8075
8076         case 's':
8077           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
8078             {
8079               PRINT_OPERAND (file, x, 0);
8080               putc (',', file);
8081             }
8082           return;
8083
8084         case 'D':
8085           /* Little bit of braindamage here.  The SSE compare instructions
8086              does use completely different names for the comparisons that the
8087              fp conditional moves.  */
8088           switch (GET_CODE (x))
8089             {
8090             case EQ:
8091             case UNEQ:
8092               fputs ("eq", file);
8093               break;
8094             case LT:
8095             case UNLT:
8096               fputs ("lt", file);
8097               break;
8098             case LE:
8099             case UNLE:
8100               fputs ("le", file);
8101               break;
8102             case UNORDERED:
8103               fputs ("unord", file);
8104               break;
8105             case NE:
8106             case LTGT:
8107               fputs ("neq", file);
8108               break;
8109             case UNGE:
8110             case GE:
8111               fputs ("nlt", file);
8112               break;
8113             case UNGT:
8114             case GT:
8115               fputs ("nle", file);
8116               break;
8117             case ORDERED:
8118               fputs ("ord", file);
8119               break;
8120             default:
8121               gcc_unreachable ();
8122             }
8123           return;
8124         case 'O':
8125 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8126           if (ASSEMBLER_DIALECT == ASM_ATT)
8127             {
8128               switch (GET_MODE (x))
8129                 {
8130                 case HImode: putc ('w', file); break;
8131                 case SImode:
8132                 case SFmode: putc ('l', file); break;
8133                 case DImode:
8134                 case DFmode: putc ('q', file); break;
8135                 default: gcc_unreachable ();
8136                 }
8137               putc ('.', file);
8138             }
8139 #endif
8140           return;
8141         case 'C':
8142           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
8143           return;
8144         case 'F':
8145 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8146           if (ASSEMBLER_DIALECT == ASM_ATT)
8147             putc ('.', file);
8148 #endif
8149           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
8150           return;
8151
8152           /* Like above, but reverse condition */
8153         case 'c':
8154           /* Check to see if argument to %c is really a constant
8155              and not a condition code which needs to be reversed.  */
8156           if (!COMPARISON_P (x))
8157           {
8158             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
8159              return;
8160           }
8161           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
8162           return;
8163         case 'f':
8164 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8165           if (ASSEMBLER_DIALECT == ASM_ATT)
8166             putc ('.', file);
8167 #endif
8168           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
8169           return;
8170
8171         case 'H':
8172           /* It doesn't actually matter what mode we use here, as we're
8173              only going to use this for printing.  */
8174           x = adjust_address_nv (x, DImode, 8);
8175           break;
8176
8177         case '+':
8178           {
8179             rtx x;
8180
8181             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
8182               return;
8183
8184             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
8185             if (x)
8186               {
8187                 int pred_val = INTVAL (XEXP (x, 0));
8188
8189                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
8190                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
8191                   {
8192                     int taken = pred_val > REG_BR_PROB_BASE / 2;
8193                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
8194
8195                     /* Emit hints only in the case default branch prediction
8196                        heuristics would fail.  */
8197                     if (taken != cputaken)
8198                       {
8199                         /* We use 3e (DS) prefix for taken branches and
8200                            2e (CS) prefix for not taken branches.  */
8201                         if (taken)
8202                           fputs ("ds ; ", file);
8203                         else
8204                           fputs ("cs ; ", file);
8205                       }
8206                   }
8207               }
8208             return;
8209           }
8210         default:
8211             output_operand_lossage ("invalid operand code '%c'", code);
8212         }
8213     }
8214
8215   if (REG_P (x))
8216     print_reg (x, code, file);
8217
8218   else if (MEM_P (x))
8219     {
8220       /* No `byte ptr' prefix for call instructions.  */
8221       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
8222         {
8223           const char * size;
8224           switch (GET_MODE_SIZE (GET_MODE (x)))
8225             {
8226             case 1: size = "BYTE"; break;
8227             case 2: size = "WORD"; break;
8228             case 4: size = "DWORD"; break;
8229             case 8: size = "QWORD"; break;
8230             case 12: size = "XWORD"; break;
8231             case 16: size = "XMMWORD"; break;
8232             default:
8233               gcc_unreachable ();
8234             }
8235
8236           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
8237           if (code == 'b')
8238             size = "BYTE";
8239           else if (code == 'w')
8240             size = "WORD";
8241           else if (code == 'k')
8242             size = "DWORD";
8243
8244           fputs (size, file);
8245           fputs (" PTR ", file);
8246         }
8247
8248       x = XEXP (x, 0);
8249       /* Avoid (%rip) for call operands.  */
8250       if (CONSTANT_ADDRESS_P (x) && code == 'P'
8251           && !CONST_INT_P (x))
8252         output_addr_const (file, x);
8253       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
8254         output_operand_lossage ("invalid constraints for operand");
8255       else
8256         output_address (x);
8257     }
8258
8259   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
8260     {
8261       REAL_VALUE_TYPE r;
8262       long l;
8263
8264       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8265       REAL_VALUE_TO_TARGET_SINGLE (r, l);
8266
8267       if (ASSEMBLER_DIALECT == ASM_ATT)
8268         putc ('$', file);
8269       fprintf (file, "0x%08lx", l);
8270     }
8271
8272   /* These float cases don't actually occur as immediate operands.  */
8273   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
8274     {
8275       char dstr[30];
8276
8277       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8278       fprintf (file, "%s", dstr);
8279     }
8280
8281   else if (GET_CODE (x) == CONST_DOUBLE
8282            && GET_MODE (x) == XFmode)
8283     {
8284       char dstr[30];
8285
8286       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8287       fprintf (file, "%s", dstr);
8288     }
8289
8290   else
8291     {
8292       /* We have patterns that allow zero sets of memory, for instance.
8293          In 64-bit mode, we should probably support all 8-byte vectors,
8294          since we can in fact encode that into an immediate.  */
8295       if (GET_CODE (x) == CONST_VECTOR)
8296         {
8297           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
8298           x = const0_rtx;
8299         }
8300
8301       if (code != 'P')
8302         {
8303           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
8304             {
8305               if (ASSEMBLER_DIALECT == ASM_ATT)
8306                 putc ('$', file);
8307             }
8308           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
8309                    || GET_CODE (x) == LABEL_REF)
8310             {
8311               if (ASSEMBLER_DIALECT == ASM_ATT)
8312                 putc ('$', file);
8313               else
8314                 fputs ("OFFSET FLAT:", file);
8315             }
8316         }
8317       if (CONST_INT_P (x))
8318         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8319       else if (flag_pic)
8320         output_pic_addr_const (file, x, code);
8321       else
8322         output_addr_const (file, x);
8323     }
8324 }
8325 \f
8326 /* Print a memory operand whose address is ADDR.  */
8327
8328 void
8329 print_operand_address (FILE *file, rtx addr)
8330 {
8331   struct ix86_address parts;
8332   rtx base, index, disp;
8333   int scale;
8334   int ok = ix86_decompose_address (addr, &parts);
8335
8336   gcc_assert (ok);
8337
8338   base = parts.base;
8339   index = parts.index;
8340   disp = parts.disp;
8341   scale = parts.scale;
8342
8343   switch (parts.seg)
8344     {
8345     case SEG_DEFAULT:
8346       break;
8347     case SEG_FS:
8348     case SEG_GS:
8349       if (USER_LABEL_PREFIX[0] == 0)
8350         putc ('%', file);
8351       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8352       break;
8353     default:
8354       gcc_unreachable ();
8355     }
8356
8357   if (!base && !index)
8358     {
8359       /* Displacement only requires special attention.  */
8360
8361       if (CONST_INT_P (disp))
8362         {
8363           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8364             {
8365               if (USER_LABEL_PREFIX[0] == 0)
8366                 putc ('%', file);
8367               fputs ("ds:", file);
8368             }
8369           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8370         }
8371       else if (flag_pic)
8372         output_pic_addr_const (file, disp, 0);
8373       else
8374         output_addr_const (file, disp);
8375
8376       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8377       if (TARGET_64BIT)
8378         {
8379           if (GET_CODE (disp) == CONST
8380               && GET_CODE (XEXP (disp, 0)) == PLUS
8381               && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8382             disp = XEXP (XEXP (disp, 0), 0);
8383           if (GET_CODE (disp) == LABEL_REF
8384               || (GET_CODE (disp) == SYMBOL_REF
8385                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8386             fputs ("(%rip)", file);
8387         }
8388     }
8389   else
8390     {
8391       if (ASSEMBLER_DIALECT == ASM_ATT)
8392         {
8393           if (disp)
8394             {
8395               if (flag_pic)
8396                 output_pic_addr_const (file, disp, 0);
8397               else if (GET_CODE (disp) == LABEL_REF)
8398                 output_asm_label (disp);
8399               else
8400                 output_addr_const (file, disp);
8401             }
8402
8403           putc ('(', file);
8404           if (base)
8405             print_reg (base, 0, file);
8406           if (index)
8407             {
8408               putc (',', file);
8409               print_reg (index, 0, file);
8410               if (scale != 1)
8411                 fprintf (file, ",%d", scale);
8412             }
8413           putc (')', file);
8414         }
8415       else
8416         {
8417           rtx offset = NULL_RTX;
8418
8419           if (disp)
8420             {
8421               /* Pull out the offset of a symbol; print any symbol itself.  */
8422               if (GET_CODE (disp) == CONST
8423                   && GET_CODE (XEXP (disp, 0)) == PLUS
8424                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8425                 {
8426                   offset = XEXP (XEXP (disp, 0), 1);
8427                   disp = gen_rtx_CONST (VOIDmode,
8428                                         XEXP (XEXP (disp, 0), 0));
8429                 }
8430
8431               if (flag_pic)
8432                 output_pic_addr_const (file, disp, 0);
8433               else if (GET_CODE (disp) == LABEL_REF)
8434                 output_asm_label (disp);
8435               else if (CONST_INT_P (disp))
8436                 offset = disp;
8437               else
8438                 output_addr_const (file, disp);
8439             }
8440
8441           putc ('[', file);
8442           if (base)
8443             {
8444               print_reg (base, 0, file);
8445               if (offset)
8446                 {
8447                   if (INTVAL (offset) >= 0)
8448                     putc ('+', file);
8449                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8450                 }
8451             }
8452           else if (offset)
8453             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8454           else
8455             putc ('0', file);
8456
8457           if (index)
8458             {
8459               putc ('+', file);
8460               print_reg (index, 0, file);
8461               if (scale != 1)
8462                 fprintf (file, "*%d", scale);
8463             }
8464           putc (']', file);
8465         }
8466     }
8467 }
8468
8469 bool
8470 output_addr_const_extra (FILE *file, rtx x)
8471 {
8472   rtx op;
8473
8474   if (GET_CODE (x) != UNSPEC)
8475     return false;
8476
8477   op = XVECEXP (x, 0, 0);
8478   switch (XINT (x, 1))
8479     {
8480     case UNSPEC_GOTTPOFF:
8481       output_addr_const (file, op);
8482       /* FIXME: This might be @TPOFF in Sun ld.  */
8483       fputs ("@GOTTPOFF", file);
8484       break;
8485     case UNSPEC_TPOFF:
8486       output_addr_const (file, op);
8487       fputs ("@TPOFF", file);
8488       break;
8489     case UNSPEC_NTPOFF:
8490       output_addr_const (file, op);
8491       if (TARGET_64BIT)
8492         fputs ("@TPOFF", file);
8493       else
8494         fputs ("@NTPOFF", file);
8495       break;
8496     case UNSPEC_DTPOFF:
8497       output_addr_const (file, op);
8498       fputs ("@DTPOFF", file);
8499       break;
8500     case UNSPEC_GOTNTPOFF:
8501       output_addr_const (file, op);
8502       if (TARGET_64BIT)
8503         fputs ("@GOTTPOFF(%rip)", file);
8504       else
8505         fputs ("@GOTNTPOFF", file);
8506       break;
8507     case UNSPEC_INDNTPOFF:
8508       output_addr_const (file, op);
8509       fputs ("@INDNTPOFF", file);
8510       break;
8511
8512     default:
8513       return false;
8514     }
8515
8516   return true;
8517 }
8518 \f
8519 /* Split one or more DImode RTL references into pairs of SImode
8520    references.  The RTL can be REG, offsettable MEM, integer constant, or
8521    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8522    split and "num" is its length.  lo_half and hi_half are output arrays
8523    that parallel "operands".  */
8524
8525 void
8526 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8527 {
8528   while (num--)
8529     {
8530       rtx op = operands[num];
8531
8532       /* simplify_subreg refuse to split volatile memory addresses,
8533          but we still have to handle it.  */
8534       if (MEM_P (op))
8535         {
8536           lo_half[num] = adjust_address (op, SImode, 0);
8537           hi_half[num] = adjust_address (op, SImode, 4);
8538         }
8539       else
8540         {
8541           lo_half[num] = simplify_gen_subreg (SImode, op,
8542                                               GET_MODE (op) == VOIDmode
8543                                               ? DImode : GET_MODE (op), 0);
8544           hi_half[num] = simplify_gen_subreg (SImode, op,
8545                                               GET_MODE (op) == VOIDmode
8546                                               ? DImode : GET_MODE (op), 4);
8547         }
8548     }
8549 }
8550 /* Split one or more TImode RTL references into pairs of DImode
8551    references.  The RTL can be REG, offsettable MEM, integer constant, or
8552    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8553    split and "num" is its length.  lo_half and hi_half are output arrays
8554    that parallel "operands".  */
8555
8556 void
8557 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8558 {
8559   while (num--)
8560     {
8561       rtx op = operands[num];
8562
8563       /* simplify_subreg refuse to split volatile memory addresses, but we
8564          still have to handle it.  */
8565       if (MEM_P (op))
8566         {
8567           lo_half[num] = adjust_address (op, DImode, 0);
8568           hi_half[num] = adjust_address (op, DImode, 8);
8569         }
8570       else
8571         {
8572           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8573           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8574         }
8575     }
8576 }
8577 \f
8578 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8579    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8580    is the expression of the binary operation.  The output may either be
8581    emitted here, or returned to the caller, like all output_* functions.
8582
8583    There is no guarantee that the operands are the same mode, as they
8584    might be within FLOAT or FLOAT_EXTEND expressions.  */
8585
8586 #ifndef SYSV386_COMPAT
8587 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8588    wants to fix the assemblers because that causes incompatibility
8589    with gcc.  No-one wants to fix gcc because that causes
8590    incompatibility with assemblers...  You can use the option of
8591    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8592 #define SYSV386_COMPAT 1
8593 #endif
8594
8595 const char *
8596 output_387_binary_op (rtx insn, rtx *operands)
8597 {
8598   static char buf[30];
8599   const char *p;
8600   const char *ssep;
8601   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8602
8603 #ifdef ENABLE_CHECKING
8604   /* Even if we do not want to check the inputs, this documents input
8605      constraints.  Which helps in understanding the following code.  */
8606   if (STACK_REG_P (operands[0])
8607       && ((REG_P (operands[1])
8608            && REGNO (operands[0]) == REGNO (operands[1])
8609            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
8610           || (REG_P (operands[2])
8611               && REGNO (operands[0]) == REGNO (operands[2])
8612               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
8613       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8614     ; /* ok */
8615   else
8616     gcc_assert (is_sse);
8617 #endif
8618
8619   switch (GET_CODE (operands[3]))
8620     {
8621     case PLUS:
8622       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8623           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8624         p = "fiadd";
8625       else
8626         p = "fadd";
8627       ssep = "add";
8628       break;
8629
8630     case MINUS:
8631       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8632           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8633         p = "fisub";
8634       else
8635         p = "fsub";
8636       ssep = "sub";
8637       break;
8638
8639     case MULT:
8640       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8641           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8642         p = "fimul";
8643       else
8644         p = "fmul";
8645       ssep = "mul";
8646       break;
8647
8648     case DIV:
8649       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8650           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8651         p = "fidiv";
8652       else
8653         p = "fdiv";
8654       ssep = "div";
8655       break;
8656
8657     default:
8658       gcc_unreachable ();
8659     }
8660
8661   if (is_sse)
8662    {
8663       strcpy (buf, ssep);
8664       if (GET_MODE (operands[0]) == SFmode)
8665         strcat (buf, "ss\t{%2, %0|%0, %2}");
8666       else
8667         strcat (buf, "sd\t{%2, %0|%0, %2}");
8668       return buf;
8669    }
8670   strcpy (buf, p);
8671
8672   switch (GET_CODE (operands[3]))
8673     {
8674     case MULT:
8675     case PLUS:
8676       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8677         {
8678           rtx temp = operands[2];
8679           operands[2] = operands[1];
8680           operands[1] = temp;
8681         }
8682
8683       /* know operands[0] == operands[1].  */
8684
8685       if (MEM_P (operands[2]))
8686         {
8687           p = "%z2\t%2";
8688           break;
8689         }
8690
8691       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8692         {
8693           if (STACK_TOP_P (operands[0]))
8694             /* How is it that we are storing to a dead operand[2]?
8695                Well, presumably operands[1] is dead too.  We can't
8696                store the result to st(0) as st(0) gets popped on this
8697                instruction.  Instead store to operands[2] (which I
8698                think has to be st(1)).  st(1) will be popped later.
8699                gcc <= 2.8.1 didn't have this check and generated
8700                assembly code that the Unixware assembler rejected.  */
8701             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8702           else
8703             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8704           break;
8705         }
8706
8707       if (STACK_TOP_P (operands[0]))
8708         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8709       else
8710         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8711       break;
8712
8713     case MINUS:
8714     case DIV:
8715       if (MEM_P (operands[1]))
8716         {
8717           p = "r%z1\t%1";
8718           break;
8719         }
8720
8721       if (MEM_P (operands[2]))
8722         {
8723           p = "%z2\t%2";
8724           break;
8725         }
8726
8727       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8728         {
8729 #if SYSV386_COMPAT
8730           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
8731              derived assemblers, confusingly reverse the direction of
8732              the operation for fsub{r} and fdiv{r} when the
8733              destination register is not st(0).  The Intel assembler
8734              doesn't have this brain damage.  Read !SYSV386_COMPAT to
8735              figure out what the hardware really does.  */
8736           if (STACK_TOP_P (operands[0]))
8737             p = "{p\t%0, %2|rp\t%2, %0}";
8738           else
8739             p = "{rp\t%2, %0|p\t%0, %2}";
8740 #else
8741           if (STACK_TOP_P (operands[0]))
8742             /* As above for fmul/fadd, we can't store to st(0).  */
8743             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
8744           else
8745             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8746 #endif
8747           break;
8748         }
8749
8750       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
8751         {
8752 #if SYSV386_COMPAT
8753           if (STACK_TOP_P (operands[0]))
8754             p = "{rp\t%0, %1|p\t%1, %0}";
8755           else
8756             p = "{p\t%1, %0|rp\t%0, %1}";
8757 #else
8758           if (STACK_TOP_P (operands[0]))
8759             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
8760           else
8761             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
8762 #endif
8763           break;
8764         }
8765
8766       if (STACK_TOP_P (operands[0]))
8767         {
8768           if (STACK_TOP_P (operands[1]))
8769             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
8770           else
8771             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
8772           break;
8773         }
8774       else if (STACK_TOP_P (operands[1]))
8775         {
8776 #if SYSV386_COMPAT
8777           p = "{\t%1, %0|r\t%0, %1}";
8778 #else
8779           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8780 #endif
8781         }
8782       else
8783         {
8784 #if SYSV386_COMPAT
8785           p = "{r\t%2, %0|\t%0, %2}";
8786 #else
8787           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8788 #endif
8789         }
8790       break;
8791
8792     default:
8793       gcc_unreachable ();
8794     }
8795
8796   strcat (buf, p);
8797   return buf;
8798 }
8799
8800 /* Return needed mode for entity in optimize_mode_switching pass.  */
8801
8802 int
8803 ix86_mode_needed (int entity, rtx insn)
8804 {
8805   enum attr_i387_cw mode;
8806
8807   /* The mode UNINITIALIZED is used to store control word after a
8808      function call or ASM pattern.  The mode ANY specify that function
8809      has no requirements on the control word and make no changes in the
8810      bits we are interested in.  */
8811
8812   if (CALL_P (insn)
8813       || (NONJUMP_INSN_P (insn)
8814           && (asm_noperands (PATTERN (insn)) >= 0
8815               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
8816     return I387_CW_UNINITIALIZED;
8817
8818   if (recog_memoized (insn) < 0)
8819     return I387_CW_ANY;
8820
8821   mode = get_attr_i387_cw (insn);
8822
8823   switch (entity)
8824     {
8825     case I387_TRUNC:
8826       if (mode == I387_CW_TRUNC)
8827         return mode;
8828       break;
8829
8830     case I387_FLOOR:
8831       if (mode == I387_CW_FLOOR)
8832         return mode;
8833       break;
8834
8835     case I387_CEIL:
8836       if (mode == I387_CW_CEIL)
8837         return mode;
8838       break;
8839
8840     case I387_MASK_PM:
8841       if (mode == I387_CW_MASK_PM)
8842         return mode;
8843       break;
8844
8845     default:
8846       gcc_unreachable ();
8847     }
8848
8849   return I387_CW_ANY;
8850 }
8851
8852 /* Output code to initialize control word copies used by trunc?f?i and
8853    rounding patterns.  CURRENT_MODE is set to current control word,
8854    while NEW_MODE is set to new control word.  */
8855
8856 void
8857 emit_i387_cw_initialization (int mode)
8858 {
8859   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
8860   rtx new_mode;
8861
8862   int slot;
8863
8864   rtx reg = gen_reg_rtx (HImode);
8865
8866   emit_insn (gen_x86_fnstcw_1 (stored_mode));
8867   emit_move_insn (reg, copy_rtx (stored_mode));
8868
8869   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
8870     {
8871       switch (mode)
8872         {
8873         case I387_CW_TRUNC:
8874           /* round toward zero (truncate) */
8875           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
8876           slot = SLOT_CW_TRUNC;
8877           break;
8878
8879         case I387_CW_FLOOR:
8880           /* round down toward -oo */
8881           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8882           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
8883           slot = SLOT_CW_FLOOR;
8884           break;
8885
8886         case I387_CW_CEIL:
8887           /* round up toward +oo */
8888           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8889           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
8890           slot = SLOT_CW_CEIL;
8891           break;
8892
8893         case I387_CW_MASK_PM:
8894           /* mask precision exception for nearbyint() */
8895           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8896           slot = SLOT_CW_MASK_PM;
8897           break;
8898
8899         default:
8900           gcc_unreachable ();
8901         }
8902     }
8903   else
8904     {
8905       switch (mode)
8906         {
8907         case I387_CW_TRUNC:
8908           /* round toward zero (truncate) */
8909           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8910           slot = SLOT_CW_TRUNC;
8911           break;
8912
8913         case I387_CW_FLOOR:
8914           /* round down toward -oo */
8915           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8916           slot = SLOT_CW_FLOOR;
8917           break;
8918
8919         case I387_CW_CEIL:
8920           /* round up toward +oo */
8921           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8922           slot = SLOT_CW_CEIL;
8923           break;
8924
8925         case I387_CW_MASK_PM:
8926           /* mask precision exception for nearbyint() */
8927           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8928           slot = SLOT_CW_MASK_PM;
8929           break;
8930
8931         default:
8932           gcc_unreachable ();
8933         }
8934     }
8935
8936   gcc_assert (slot < MAX_386_STACK_LOCALS);
8937
8938   new_mode = assign_386_stack_local (HImode, slot);
8939   emit_move_insn (new_mode, reg);
8940 }
8941
8942 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8943    are the insn operands.  The output may be [HSD]Imode and the input
8944    operand may be [SDX]Fmode.  */
8945
8946 const char *
8947 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8948 {
8949   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8950   int dimode_p = GET_MODE (operands[0]) == DImode;
8951   int round_mode = get_attr_i387_cw (insn);
8952
8953   /* Jump through a hoop or two for DImode, since the hardware has no
8954      non-popping instruction.  We used to do this a different way, but
8955      that was somewhat fragile and broke with post-reload splitters.  */
8956   if ((dimode_p || fisttp) && !stack_top_dies)
8957     output_asm_insn ("fld\t%y1", operands);
8958
8959   gcc_assert (STACK_TOP_P (operands[1]));
8960   gcc_assert (MEM_P (operands[0]));
8961
8962   if (fisttp)
8963       output_asm_insn ("fisttp%z0\t%0", operands);
8964   else
8965     {
8966       if (round_mode != I387_CW_ANY)
8967         output_asm_insn ("fldcw\t%3", operands);
8968       if (stack_top_dies || dimode_p)
8969         output_asm_insn ("fistp%z0\t%0", operands);
8970       else
8971         output_asm_insn ("fist%z0\t%0", operands);
8972       if (round_mode != I387_CW_ANY)
8973         output_asm_insn ("fldcw\t%2", operands);
8974     }
8975
8976   return "";
8977 }
8978
8979 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
8980    have the values zero or one, indicates the ffreep insn's operand
8981    from the OPERANDS array.  */
8982
8983 static const char *
8984 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
8985 {
8986   if (TARGET_USE_FFREEP)
8987 #if HAVE_AS_IX86_FFREEP
8988     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
8989 #else
8990     {
8991       static char retval[] = ".word\t0xc_df";
8992       int regno = REGNO (operands[opno]);
8993       
8994       gcc_assert (FP_REGNO_P (regno));
8995
8996       retval[9] = '0' + (regno - FIRST_STACK_REG);
8997       return retval;
8998     }
8999 #endif
9000
9001   return opno ? "fstp\t%y1" : "fstp\t%y0";
9002 }
9003
9004
9005 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
9006    should be used.  UNORDERED_P is true when fucom should be used.  */
9007
9008 const char *
9009 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
9010 {
9011   int stack_top_dies;
9012   rtx cmp_op0, cmp_op1;
9013   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
9014
9015   if (eflags_p)
9016     {
9017       cmp_op0 = operands[0];
9018       cmp_op1 = operands[1];
9019     }
9020   else
9021     {
9022       cmp_op0 = operands[1];
9023       cmp_op1 = operands[2];
9024     }
9025
9026   if (is_sse)
9027     {
9028       if (GET_MODE (operands[0]) == SFmode)
9029         if (unordered_p)
9030           return "ucomiss\t{%1, %0|%0, %1}";
9031         else
9032           return "comiss\t{%1, %0|%0, %1}";
9033       else
9034         if (unordered_p)
9035           return "ucomisd\t{%1, %0|%0, %1}";
9036         else
9037           return "comisd\t{%1, %0|%0, %1}";
9038     }
9039
9040   gcc_assert (STACK_TOP_P (cmp_op0));
9041
9042   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9043
9044   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
9045     {
9046       if (stack_top_dies)
9047         {
9048           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
9049           return output_387_ffreep (operands, 1);
9050         }
9051       else
9052         return "ftst\n\tfnstsw\t%0";
9053     }
9054
9055   if (STACK_REG_P (cmp_op1)
9056       && stack_top_dies
9057       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
9058       && REGNO (cmp_op1) != FIRST_STACK_REG)
9059     {
9060       /* If both the top of the 387 stack dies, and the other operand
9061          is also a stack register that dies, then this must be a
9062          `fcompp' float compare */
9063
9064       if (eflags_p)
9065         {
9066           /* There is no double popping fcomi variant.  Fortunately,
9067              eflags is immune from the fstp's cc clobbering.  */
9068           if (unordered_p)
9069             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
9070           else
9071             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
9072           return output_387_ffreep (operands, 0);
9073         }
9074       else
9075         {
9076           if (unordered_p)
9077             return "fucompp\n\tfnstsw\t%0";
9078           else
9079             return "fcompp\n\tfnstsw\t%0";
9080         }
9081     }
9082   else
9083     {
9084       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
9085
9086       static const char * const alt[16] =
9087       {
9088         "fcom%z2\t%y2\n\tfnstsw\t%0",
9089         "fcomp%z2\t%y2\n\tfnstsw\t%0",
9090         "fucom%z2\t%y2\n\tfnstsw\t%0",
9091         "fucomp%z2\t%y2\n\tfnstsw\t%0",
9092
9093         "ficom%z2\t%y2\n\tfnstsw\t%0",
9094         "ficomp%z2\t%y2\n\tfnstsw\t%0",
9095         NULL,
9096         NULL,
9097
9098         "fcomi\t{%y1, %0|%0, %y1}",
9099         "fcomip\t{%y1, %0|%0, %y1}",
9100         "fucomi\t{%y1, %0|%0, %y1}",
9101         "fucomip\t{%y1, %0|%0, %y1}",
9102
9103         NULL,
9104         NULL,
9105         NULL,
9106         NULL
9107       };
9108
9109       int mask;
9110       const char *ret;
9111
9112       mask  = eflags_p << 3;
9113       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
9114       mask |= unordered_p << 1;
9115       mask |= stack_top_dies;
9116
9117       gcc_assert (mask < 16);
9118       ret = alt[mask];
9119       gcc_assert (ret);
9120
9121       return ret;
9122     }
9123 }
9124
9125 void
9126 ix86_output_addr_vec_elt (FILE *file, int value)
9127 {
9128   const char *directive = ASM_LONG;
9129
9130 #ifdef ASM_QUAD
9131   if (TARGET_64BIT)
9132     directive = ASM_QUAD;
9133 #else
9134   gcc_assert (!TARGET_64BIT);
9135 #endif
9136
9137   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
9138 }
9139
9140 void
9141 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
9142 {
9143   const char *directive = ASM_LONG;
9144
9145 #ifdef ASM_QUAD
9146   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
9147     directive = ASM_QUAD;
9148 #else
9149   gcc_assert (!TARGET_64BIT);
9150 #endif
9151   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
9152   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
9153     fprintf (file, "%s%s%d-%s%d\n",
9154              directive, LPREFIX, value, LPREFIX, rel);
9155   else if (HAVE_AS_GOTOFF_IN_DATA)
9156     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
9157 #if TARGET_MACHO
9158   else if (TARGET_MACHO)
9159     {
9160       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
9161       machopic_output_function_base_name (file);
9162       fprintf(file, "\n");
9163     }
9164 #endif
9165   else
9166     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
9167                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
9168 }
9169 \f
9170 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
9171    for the target.  */
9172
9173 void
9174 ix86_expand_clear (rtx dest)
9175 {
9176   rtx tmp;
9177
9178   /* We play register width games, which are only valid after reload.  */
9179   gcc_assert (reload_completed);
9180
9181   /* Avoid HImode and its attendant prefix byte.  */
9182   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
9183     dest = gen_rtx_REG (SImode, REGNO (dest));
9184
9185   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
9186
9187   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
9188   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
9189     {
9190       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
9191       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
9192     }
9193
9194   emit_insn (tmp);
9195 }
9196
9197 /* X is an unchanging MEM.  If it is a constant pool reference, return
9198    the constant pool rtx, else NULL.  */
9199
9200 rtx
9201 maybe_get_pool_constant (rtx x)
9202 {
9203   x = ix86_delegitimize_address (XEXP (x, 0));
9204
9205   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
9206     return get_pool_constant (x);
9207
9208   return NULL_RTX;
9209 }
9210
9211 void
9212 ix86_expand_move (enum machine_mode mode, rtx operands[])
9213 {
9214   int strict = (reload_in_progress || reload_completed);
9215   rtx op0, op1;
9216   enum tls_model model;
9217
9218   op0 = operands[0];
9219   op1 = operands[1];
9220
9221   if (GET_CODE (op1) == SYMBOL_REF)
9222     {
9223       model = SYMBOL_REF_TLS_MODEL (op1);
9224       if (model)
9225         {
9226           op1 = legitimize_tls_address (op1, model, true);
9227           op1 = force_operand (op1, op0);
9228           if (op1 == op0)
9229             return;
9230         }
9231     }
9232   else if (GET_CODE (op1) == CONST
9233            && GET_CODE (XEXP (op1, 0)) == PLUS
9234            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
9235     {
9236       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
9237       if (model)
9238         {
9239           rtx addend = XEXP (XEXP (op1, 0), 1);
9240           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
9241           op1 = force_operand (op1, NULL);
9242           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
9243                                      op0, 1, OPTAB_DIRECT);
9244           if (op1 == op0)
9245             return;
9246         }
9247     }
9248
9249   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
9250     {
9251       if (TARGET_MACHO && !TARGET_64BIT)
9252         {
9253 #if TARGET_MACHO
9254           if (MACHOPIC_PURE)
9255             {
9256               rtx temp = ((reload_in_progress
9257                            || ((op0 && REG_P (op0))
9258                                && mode == Pmode))
9259                           ? op0 : gen_reg_rtx (Pmode));
9260               op1 = machopic_indirect_data_reference (op1, temp);
9261               op1 = machopic_legitimize_pic_address (op1, mode,
9262                                                      temp == op1 ? 0 : temp);
9263             }
9264           else if (MACHOPIC_INDIRECT)
9265             op1 = machopic_indirect_data_reference (op1, 0);
9266           if (op0 == op1)
9267             return;
9268 #endif
9269         }
9270       else
9271         {
9272           if (MEM_P (op0))
9273             op1 = force_reg (Pmode, op1);
9274           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
9275             {
9276               rtx reg = no_new_pseudos ? op0 : NULL_RTX;
9277               op1 = legitimize_pic_address (op1, reg);
9278               if (op0 == op1)
9279                 return;
9280             }
9281         }
9282     }
9283   else
9284     {
9285       if (MEM_P (op0)
9286           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
9287               || !push_operand (op0, mode))
9288           && MEM_P (op1))
9289         op1 = force_reg (mode, op1);
9290
9291       if (push_operand (op0, mode)
9292           && ! general_no_elim_operand (op1, mode))
9293         op1 = copy_to_mode_reg (mode, op1);
9294
9295       /* Force large constants in 64bit compilation into register
9296          to get them CSEed.  */
9297       if (TARGET_64BIT && mode == DImode
9298           && immediate_operand (op1, mode)
9299           && !x86_64_zext_immediate_operand (op1, VOIDmode)
9300           && !register_operand (op0, mode)
9301           && optimize && !reload_completed && !reload_in_progress)
9302         op1 = copy_to_mode_reg (mode, op1);
9303
9304       if (FLOAT_MODE_P (mode))
9305         {
9306           /* If we are loading a floating point constant to a register,
9307              force the value to memory now, since we'll get better code
9308              out the back end.  */
9309
9310           if (strict)
9311             ;
9312           else if (GET_CODE (op1) == CONST_DOUBLE)
9313             {
9314               op1 = validize_mem (force_const_mem (mode, op1));
9315               if (!register_operand (op0, mode))
9316                 {
9317                   rtx temp = gen_reg_rtx (mode);
9318                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
9319                   emit_move_insn (op0, temp);
9320                   return;
9321                 }
9322             }
9323         }
9324     }
9325
9326   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9327 }
9328
9329 void
9330 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9331 {
9332   rtx op0 = operands[0], op1 = operands[1];
9333
9334   /* Force constants other than zero into memory.  We do not know how
9335      the instructions used to build constants modify the upper 64 bits
9336      of the register, once we have that information we may be able
9337      to handle some of them more efficiently.  */
9338   if ((reload_in_progress | reload_completed) == 0
9339       && register_operand (op0, mode)
9340       && CONSTANT_P (op1)
9341       && standard_sse_constant_p (op1) <= 0)
9342     op1 = validize_mem (force_const_mem (mode, op1));
9343
9344   /* Make operand1 a register if it isn't already.  */
9345   if (!no_new_pseudos
9346       && !register_operand (op0, mode)
9347       && !register_operand (op1, mode))
9348     {
9349       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9350       return;
9351     }
9352
9353   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9354 }
9355
9356 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9357    straight to ix86_expand_vector_move.  */
9358 /* Code generation for scalar reg-reg moves of single and double precision data:
9359      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
9360        movaps reg, reg
9361      else
9362        movss reg, reg
9363      if (x86_sse_partial_reg_dependency == true)
9364        movapd reg, reg
9365      else
9366        movsd reg, reg
9367
9368    Code generation for scalar loads of double precision data:
9369      if (x86_sse_split_regs == true)
9370        movlpd mem, reg      (gas syntax)
9371      else
9372        movsd mem, reg
9373  
9374    Code generation for unaligned packed loads of single precision data
9375    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
9376      if (x86_sse_unaligned_move_optimal)
9377        movups mem, reg
9378
9379      if (x86_sse_partial_reg_dependency == true)
9380        {
9381          xorps  reg, reg
9382          movlps mem, reg
9383          movhps mem+8, reg
9384        }
9385      else
9386        {
9387          movlps mem, reg
9388          movhps mem+8, reg
9389        }
9390
9391    Code generation for unaligned packed loads of double precision data
9392    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
9393      if (x86_sse_unaligned_move_optimal)
9394        movupd mem, reg
9395
9396      if (x86_sse_split_regs == true)
9397        {
9398          movlpd mem, reg
9399          movhpd mem+8, reg
9400        }
9401      else
9402        {
9403          movsd  mem, reg
9404          movhpd mem+8, reg
9405        }
9406  */
9407
9408 void
9409 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9410 {
9411   rtx op0, op1, m;
9412
9413   op0 = operands[0];
9414   op1 = operands[1];
9415
9416   if (MEM_P (op1))
9417     {
9418       /* If we're optimizing for size, movups is the smallest.  */
9419       if (optimize_size)
9420         {
9421           op0 = gen_lowpart (V4SFmode, op0);
9422           op1 = gen_lowpart (V4SFmode, op1);
9423           emit_insn (gen_sse_movups (op0, op1));
9424           return;
9425         }
9426
9427       /* ??? If we have typed data, then it would appear that using
9428          movdqu is the only way to get unaligned data loaded with
9429          integer type.  */
9430       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9431         {
9432           op0 = gen_lowpart (V16QImode, op0);
9433           op1 = gen_lowpart (V16QImode, op1);
9434           emit_insn (gen_sse2_movdqu (op0, op1));
9435           return;
9436         }
9437
9438       if (TARGET_SSE2 && mode == V2DFmode)
9439         {
9440           rtx zero;
9441
9442           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9443             {
9444               op0 = gen_lowpart (V2DFmode, op0);
9445               op1 = gen_lowpart (V2DFmode, op1);
9446               emit_insn (gen_sse2_movupd (op0, op1));
9447               return;
9448             }
9449
9450           /* When SSE registers are split into halves, we can avoid
9451              writing to the top half twice.  */
9452           if (TARGET_SSE_SPLIT_REGS)
9453             {
9454               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9455               zero = op0;
9456             }
9457           else
9458             {
9459               /* ??? Not sure about the best option for the Intel chips.
9460                  The following would seem to satisfy; the register is
9461                  entirely cleared, breaking the dependency chain.  We
9462                  then store to the upper half, with a dependency depth
9463                  of one.  A rumor has it that Intel recommends two movsd
9464                  followed by an unpacklpd, but this is unconfirmed.  And
9465                  given that the dependency depth of the unpacklpd would
9466                  still be one, I'm not sure why this would be better.  */
9467               zero = CONST0_RTX (V2DFmode);
9468             }
9469
9470           m = adjust_address (op1, DFmode, 0);
9471           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9472           m = adjust_address (op1, DFmode, 8);
9473           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9474         }
9475       else
9476         {
9477           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9478             {
9479               op0 = gen_lowpart (V4SFmode, op0);
9480               op1 = gen_lowpart (V4SFmode, op1);
9481               emit_insn (gen_sse_movups (op0, op1));
9482               return;
9483             }
9484
9485           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9486             emit_move_insn (op0, CONST0_RTX (mode));
9487           else
9488             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9489
9490           if (mode != V4SFmode)
9491             op0 = gen_lowpart (V4SFmode, op0);
9492           m = adjust_address (op1, V2SFmode, 0);
9493           emit_insn (gen_sse_loadlps (op0, op0, m));
9494           m = adjust_address (op1, V2SFmode, 8);
9495           emit_insn (gen_sse_loadhps (op0, op0, m));
9496         }
9497     }
9498   else if (MEM_P (op0))
9499     {
9500       /* If we're optimizing for size, movups is the smallest.  */
9501       if (optimize_size)
9502         {
9503           op0 = gen_lowpart (V4SFmode, op0);
9504           op1 = gen_lowpart (V4SFmode, op1);
9505           emit_insn (gen_sse_movups (op0, op1));
9506           return;
9507         }
9508
9509       /* ??? Similar to above, only less clear because of quote
9510          typeless stores unquote.  */
9511       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9512           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9513         {
9514           op0 = gen_lowpart (V16QImode, op0);
9515           op1 = gen_lowpart (V16QImode, op1);
9516           emit_insn (gen_sse2_movdqu (op0, op1));
9517           return;
9518         }
9519
9520       if (TARGET_SSE2 && mode == V2DFmode)
9521         {
9522           m = adjust_address (op0, DFmode, 0);
9523           emit_insn (gen_sse2_storelpd (m, op1));
9524           m = adjust_address (op0, DFmode, 8);
9525           emit_insn (gen_sse2_storehpd (m, op1));
9526         }
9527       else
9528         {
9529           if (mode != V4SFmode)
9530             op1 = gen_lowpart (V4SFmode, op1);
9531           m = adjust_address (op0, V2SFmode, 0);
9532           emit_insn (gen_sse_storelps (m, op1));
9533           m = adjust_address (op0, V2SFmode, 8);
9534           emit_insn (gen_sse_storehps (m, op1));
9535         }
9536     }
9537   else
9538     gcc_unreachable ();
9539 }
9540
9541 /* Expand a push in MODE.  This is some mode for which we do not support
9542    proper push instructions, at least from the registers that we expect
9543    the value to live in.  */
9544
9545 void
9546 ix86_expand_push (enum machine_mode mode, rtx x)
9547 {
9548   rtx tmp;
9549
9550   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9551                              GEN_INT (-GET_MODE_SIZE (mode)),
9552                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9553   if (tmp != stack_pointer_rtx)
9554     emit_move_insn (stack_pointer_rtx, tmp);
9555
9556   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9557   emit_move_insn (tmp, x);
9558 }
9559
9560 /* Helper function of ix86_fixup_binary_operands to canonicalize
9561    operand order.  Returns true if the operands should be swapped.  */
9562    
9563 static bool
9564 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
9565                              rtx operands[])
9566 {
9567   rtx dst = operands[0];
9568   rtx src1 = operands[1];
9569   rtx src2 = operands[2];
9570
9571   /* If the operation is not commutative, we can't do anything.  */
9572   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9573     return false;
9574
9575   /* Highest priority is that src1 should match dst.  */
9576   if (rtx_equal_p (dst, src1))
9577     return false;
9578   if (rtx_equal_p (dst, src2))
9579     return true;
9580
9581   /* Next highest priority is that immediate constants come second.  */
9582   if (immediate_operand (src2, mode))
9583     return false;
9584   if (immediate_operand (src1, mode))
9585     return true;
9586
9587   /* Lowest priority is that memory references should come second.  */
9588   if (MEM_P (src2))
9589     return false;
9590   if (MEM_P (src1))
9591     return true;
9592
9593   return false;
9594 }
9595
9596
9597 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9598    destination to use for the operation.  If different from the true
9599    destination in operands[0], a copy operation will be required.  */
9600
9601 rtx
9602 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9603                             rtx operands[])
9604 {
9605   rtx dst = operands[0];
9606   rtx src1 = operands[1];
9607   rtx src2 = operands[2];
9608
9609   /* Canonicalize operand order.  */
9610   if (ix86_swap_binary_operands_p (code, mode, operands))
9611     {
9612       rtx temp = src1;
9613       src1 = src2;
9614       src2 = temp;
9615     }
9616
9617   /* Both source operands cannot be in memory.  */
9618   if (MEM_P (src1) && MEM_P (src2))
9619     {
9620       /* Optimization: Only read from memory once.  */
9621       if (rtx_equal_p (src1, src2))
9622         {
9623           src2 = force_reg (mode, src2);
9624           src1 = src2;
9625         }
9626       else
9627         src2 = force_reg (mode, src2);
9628     }
9629
9630   /* If the destination is memory, and we do not have matching source
9631      operands, do things in registers.  */
9632   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
9633     dst = gen_reg_rtx (mode);
9634
9635   /* Source 1 cannot be a constant.  */
9636   if (CONSTANT_P (src1))
9637     src1 = force_reg (mode, src1);
9638
9639   /* Source 1 cannot be a non-matching memory.  */
9640   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
9641     src1 = force_reg (mode, src1);
9642
9643   operands[1] = src1;
9644   operands[2] = src2;
9645   return dst;
9646 }
9647
9648 /* Similarly, but assume that the destination has already been
9649    set up properly.  */
9650
9651 void
9652 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9653                                     enum machine_mode mode, rtx operands[])
9654 {
9655   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9656   gcc_assert (dst == operands[0]);
9657 }
9658
9659 /* Attempt to expand a binary operator.  Make the expansion closer to the
9660    actual machine, then just general_operand, which will allow 3 separate
9661    memory references (one output, two input) in a single insn.  */
9662
9663 void
9664 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9665                              rtx operands[])
9666 {
9667   rtx src1, src2, dst, op, clob;
9668
9669   dst = ix86_fixup_binary_operands (code, mode, operands);
9670   src1 = operands[1];
9671   src2 = operands[2];
9672
9673  /* Emit the instruction.  */
9674
9675   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9676   if (reload_in_progress)
9677     {
9678       /* Reload doesn't know about the flags register, and doesn't know that
9679          it doesn't want to clobber it.  We can only do this with PLUS.  */
9680       gcc_assert (code == PLUS);
9681       emit_insn (op);
9682     }
9683   else
9684     {
9685       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9686       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9687     }
9688
9689   /* Fix up the destination if needed.  */
9690   if (dst != operands[0])
9691     emit_move_insn (operands[0], dst);
9692 }
9693
9694 /* Return TRUE or FALSE depending on whether the binary operator meets the
9695    appropriate constraints.  */
9696
9697 int
9698 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
9699                          rtx operands[3])
9700 {
9701   rtx dst = operands[0];
9702   rtx src1 = operands[1];
9703   rtx src2 = operands[2];
9704
9705   /* Both source operands cannot be in memory.  */
9706   if (MEM_P (src1) && MEM_P (src2))
9707     return 0;
9708
9709   /* Canonicalize operand order for commutative operators.  */
9710   if (ix86_swap_binary_operands_p (code, mode, operands))
9711     {
9712       rtx temp = src1;
9713       src1 = src2;
9714       src2 = temp;
9715     }
9716
9717   /* If the destination is memory, we must have a matching source operand.  */
9718   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
9719       return 0;
9720
9721   /* Source 1 cannot be a constant.  */
9722   if (CONSTANT_P (src1))
9723     return 0;
9724
9725   /* Source 1 cannot be a non-matching memory.  */
9726   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
9727     return 0;
9728
9729   return 1;
9730 }
9731
9732 /* Attempt to expand a unary operator.  Make the expansion closer to the
9733    actual machine, then just general_operand, which will allow 2 separate
9734    memory references (one output, one input) in a single insn.  */
9735
9736 void
9737 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
9738                             rtx operands[])
9739 {
9740   int matching_memory;
9741   rtx src, dst, op, clob;
9742
9743   dst = operands[0];
9744   src = operands[1];
9745
9746   /* If the destination is memory, and we do not have matching source
9747      operands, do things in registers.  */
9748   matching_memory = 0;
9749   if (MEM_P (dst))
9750     {
9751       if (rtx_equal_p (dst, src))
9752         matching_memory = 1;
9753       else
9754         dst = gen_reg_rtx (mode);
9755     }
9756
9757   /* When source operand is memory, destination must match.  */
9758   if (MEM_P (src) && !matching_memory)
9759     src = force_reg (mode, src);
9760
9761   /* Emit the instruction.  */
9762
9763   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
9764   if (reload_in_progress || code == NOT)
9765     {
9766       /* Reload doesn't know about the flags register, and doesn't know that
9767          it doesn't want to clobber it.  */
9768       gcc_assert (code == NOT);
9769       emit_insn (op);
9770     }
9771   else
9772     {
9773       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9774       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9775     }
9776
9777   /* Fix up the destination if needed.  */
9778   if (dst != operands[0])
9779     emit_move_insn (operands[0], dst);
9780 }
9781
9782 /* Return TRUE or FALSE depending on whether the unary operator meets the
9783    appropriate constraints.  */
9784
9785 int
9786 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
9787                         enum machine_mode mode ATTRIBUTE_UNUSED,
9788                         rtx operands[2] ATTRIBUTE_UNUSED)
9789 {
9790   /* If one of operands is memory, source and destination must match.  */
9791   if ((MEM_P (operands[0])
9792        || MEM_P (operands[1]))
9793       && ! rtx_equal_p (operands[0], operands[1]))
9794     return FALSE;
9795   return TRUE;
9796 }
9797
9798 /* Post-reload splitter for converting an SF or DFmode value in an
9799    SSE register into an unsigned SImode.  */
9800
9801 void
9802 ix86_split_convert_uns_si_sse (rtx operands[])
9803 {
9804   enum machine_mode vecmode;
9805   rtx value, large, zero_or_two31, input, two31, x;
9806
9807   large = operands[1];
9808   zero_or_two31 = operands[2];
9809   input = operands[3];
9810   two31 = operands[4];
9811   vecmode = GET_MODE (large);
9812   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
9813
9814   /* Load up the value into the low element.  We must ensure that the other
9815      elements are valid floats -- zero is the easiest such value.  */
9816   if (MEM_P (input))
9817     {
9818       if (vecmode == V4SFmode)
9819         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
9820       else
9821         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
9822     }
9823   else
9824     {
9825       input = gen_rtx_REG (vecmode, REGNO (input));
9826       emit_move_insn (value, CONST0_RTX (vecmode));
9827       if (vecmode == V4SFmode)
9828         emit_insn (gen_sse_movss (value, value, input));
9829       else
9830         emit_insn (gen_sse2_movsd (value, value, input));
9831     }
9832
9833   emit_move_insn (large, two31);
9834   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
9835
9836   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
9837   emit_insn (gen_rtx_SET (VOIDmode, large, x));
9838
9839   x = gen_rtx_AND (vecmode, zero_or_two31, large);
9840   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
9841
9842   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
9843   emit_insn (gen_rtx_SET (VOIDmode, value, x));
9844
9845   large = gen_rtx_REG (V4SImode, REGNO (large));
9846   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
9847
9848   x = gen_rtx_REG (V4SImode, REGNO (value));
9849   if (vecmode == V4SFmode)
9850     emit_insn (gen_sse2_cvttps2dq (x, value));
9851   else
9852     emit_insn (gen_sse2_cvttpd2dq (x, value));
9853   value = x;
9854
9855   emit_insn (gen_xorv4si3 (value, value, large));
9856 }
9857
9858 /* Convert an unsigned DImode value into a DFmode, using only SSE.
9859    Expects the 64-bit DImode to be supplied in a pair of integral
9860    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
9861    -mfpmath=sse, !optimize_size only.  */
9862
9863 void
9864 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
9865 {
9866   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
9867   rtx int_xmm, fp_xmm;
9868   rtx biases, exponents;
9869   rtx x;
9870
9871   int_xmm = gen_reg_rtx (V4SImode);
9872   if (TARGET_INTER_UNIT_MOVES)
9873     emit_insn (gen_movdi_to_sse (int_xmm, input));
9874   else if (TARGET_SSE_SPLIT_REGS)
9875     {
9876       emit_insn (gen_rtx_CLOBBER (VOIDmode, int_xmm));
9877       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
9878     }
9879   else
9880     {
9881       x = gen_reg_rtx (V2DImode);
9882       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
9883       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
9884     }
9885
9886   x = gen_rtx_CONST_VECTOR (V4SImode,
9887                             gen_rtvec (4, GEN_INT (0x43300000UL),
9888                                        GEN_INT (0x45300000UL),
9889                                        const0_rtx, const0_rtx));
9890   exponents = validize_mem (force_const_mem (V4SImode, x));
9891
9892   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
9893   emit_insn (gen_sse2_punpckldq (int_xmm, int_xmm, exponents));
9894
9895   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
9896      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
9897      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
9898      (0x1.0p84 + double(fp_value_hi_xmm)).
9899      Note these exponents differ by 32.  */
9900
9901   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
9902
9903   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
9904      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
9905   real_ldexp (&bias_lo_rvt, &dconst1, 52);
9906   real_ldexp (&bias_hi_rvt, &dconst1, 84);
9907   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
9908   x = const_double_from_real_value (bias_hi_rvt, DFmode);
9909   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
9910   biases = validize_mem (force_const_mem (V2DFmode, biases));
9911   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
9912
9913   /* Add the upper and lower DFmode values together.  */
9914   if (TARGET_SSE3)
9915     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
9916   else
9917     {
9918       x = copy_to_mode_reg (V2DFmode, fp_xmm);
9919       emit_insn (gen_sse2_unpckhpd (fp_xmm, fp_xmm, fp_xmm));
9920       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
9921     }
9922
9923   ix86_expand_vector_extract (false, target, fp_xmm, 0);
9924 }
9925
9926 /* Convert an unsigned SImode value into a DFmode.  Only currently used
9927    for SSE, but applicable anywhere.  */
9928
9929 void
9930 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
9931 {
9932   REAL_VALUE_TYPE TWO31r;
9933   rtx x, fp;
9934
9935   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
9936                            NULL, 1, OPTAB_DIRECT);
9937
9938   fp = gen_reg_rtx (DFmode);
9939   emit_insn (gen_floatsidf2 (fp, x));
9940
9941   real_ldexp (&TWO31r, &dconst1, 31);
9942   x = const_double_from_real_value (TWO31r, DFmode);
9943
9944   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
9945   if (x != target)
9946     emit_move_insn (target, x);
9947 }
9948
9949 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
9950    32-bit mode; otherwise we have a direct convert instruction.  */
9951
9952 void
9953 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
9954 {
9955   REAL_VALUE_TYPE TWO32r;
9956   rtx fp_lo, fp_hi, x;
9957   
9958   fp_lo = gen_reg_rtx (DFmode);
9959   fp_hi = gen_reg_rtx (DFmode);
9960
9961   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
9962
9963   real_ldexp (&TWO32r, &dconst1, 32);
9964   x = const_double_from_real_value (TWO32r, DFmode);
9965   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
9966
9967   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
9968
9969   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
9970                            0, OPTAB_DIRECT);
9971   if (x != target)
9972     emit_move_insn (target, x);
9973 }
9974
9975 /* Convert an unsigned SImode value into a SFmode, using only SSE.
9976    For x86_32, -mfpmath=sse, !optimize_size only.  */
9977 void
9978 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
9979 {
9980   REAL_VALUE_TYPE ONE16r;
9981   rtx fp_hi, fp_lo, int_hi, int_lo, x;
9982
9983   real_ldexp (&ONE16r, &dconst1, 16);
9984   x = const_double_from_real_value (ONE16r, SFmode);
9985   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
9986                                       NULL, 0, OPTAB_DIRECT);
9987   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
9988                                       NULL, 0, OPTAB_DIRECT);
9989   fp_hi = gen_reg_rtx (SFmode);
9990   fp_lo = gen_reg_rtx (SFmode);
9991   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
9992   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
9993   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
9994                                0, OPTAB_DIRECT);
9995   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
9996                                0, OPTAB_DIRECT);
9997   if (!rtx_equal_p (target, fp_hi))
9998     emit_move_insn (target, fp_hi);
9999 }
10000
10001 /* A subroutine of ix86_build_signbit_mask_vector.  If VECT is true,
10002    then replicate the value for all elements of the vector
10003    register.  */
10004
10005 rtx
10006 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
10007 {
10008   rtvec v;
10009   switch (mode)
10010     {
10011     case SFmode:
10012       if (vect)
10013         v = gen_rtvec (4, value, value, value, value);
10014       else
10015         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
10016                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10017       return gen_rtx_CONST_VECTOR (V4SFmode, v);
10018
10019     case DFmode:
10020       if (vect)
10021         v = gen_rtvec (2, value, value);
10022       else
10023         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
10024       return gen_rtx_CONST_VECTOR (V2DFmode, v);
10025
10026     default:
10027       gcc_unreachable ();
10028     }
10029 }
10030
10031 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
10032    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
10033    true, then replicate the mask for all elements of the vector register.
10034    If INVERT is true, then create a mask excluding the sign bit.  */
10035
10036 rtx
10037 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
10038 {
10039   enum machine_mode vec_mode;
10040   HOST_WIDE_INT hi, lo;
10041   int shift = 63;
10042   rtx v;
10043   rtx mask;
10044
10045   /* Find the sign bit, sign extended to 2*HWI.  */
10046   if (mode == SFmode)
10047     lo = 0x80000000, hi = lo < 0;
10048   else if (HOST_BITS_PER_WIDE_INT >= 64)
10049     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
10050   else
10051     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
10052
10053   if (invert)
10054     lo = ~lo, hi = ~hi;
10055
10056   /* Force this value into the low part of a fp vector constant.  */
10057   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
10058   mask = gen_lowpart (mode, mask);
10059
10060   v = ix86_build_const_vector (mode, vect, mask);
10061   vec_mode = (mode == SFmode) ? V4SFmode : V2DFmode;
10062   return force_reg (vec_mode, v);
10063 }
10064
10065 /* Generate code for floating point ABS or NEG.  */
10066
10067 void
10068 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
10069                                 rtx operands[])
10070 {
10071   rtx mask, set, use, clob, dst, src;
10072   bool matching_memory;
10073   bool use_sse = false;
10074   bool vector_mode = VECTOR_MODE_P (mode);
10075   enum machine_mode elt_mode = mode;
10076
10077   if (vector_mode)
10078     {
10079       elt_mode = GET_MODE_INNER (mode);
10080       use_sse = true;
10081     }
10082   else if (TARGET_SSE_MATH)
10083     use_sse = SSE_FLOAT_MODE_P (mode);
10084
10085   /* NEG and ABS performed with SSE use bitwise mask operations.
10086      Create the appropriate mask now.  */
10087   if (use_sse)
10088     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
10089   else
10090     mask = NULL_RTX;
10091
10092   dst = operands[0];
10093   src = operands[1];
10094
10095   /* If the destination is memory, and we don't have matching source
10096      operands or we're using the x87, do things in registers.  */
10097   matching_memory = false;
10098   if (MEM_P (dst))
10099     {
10100       if (use_sse && rtx_equal_p (dst, src))
10101         matching_memory = true;
10102       else
10103         dst = gen_reg_rtx (mode);
10104     }
10105   if (MEM_P (src) && !matching_memory)
10106     src = force_reg (mode, src);
10107
10108   if (vector_mode)
10109     {
10110       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
10111       set = gen_rtx_SET (VOIDmode, dst, set);
10112       emit_insn (set);
10113     }
10114   else
10115     {
10116       set = gen_rtx_fmt_e (code, mode, src);
10117       set = gen_rtx_SET (VOIDmode, dst, set);
10118       if (mask)
10119         {
10120           use = gen_rtx_USE (VOIDmode, mask);
10121           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10122           emit_insn (gen_rtx_PARALLEL (VOIDmode,
10123                                        gen_rtvec (3, set, use, clob)));
10124         }
10125       else
10126         emit_insn (set);
10127     }
10128
10129   if (dst != operands[0])
10130     emit_move_insn (operands[0], dst);
10131 }
10132
10133 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
10134
10135 void
10136 ix86_expand_copysign (rtx operands[])
10137 {
10138   enum machine_mode mode, vmode;
10139   rtx dest, op0, op1, mask, nmask;
10140
10141   dest = operands[0];
10142   op0 = operands[1];
10143   op1 = operands[2];
10144
10145   mode = GET_MODE (dest);
10146   vmode = mode == SFmode ? V4SFmode : V2DFmode;
10147
10148   if (GET_CODE (op0) == CONST_DOUBLE)
10149     {
10150       rtvec v;
10151
10152       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
10153         op0 = simplify_unary_operation (ABS, mode, op0, mode);
10154
10155       if (op0 == CONST0_RTX (mode))
10156         op0 = CONST0_RTX (vmode);
10157       else
10158         {
10159           if (mode == SFmode)
10160             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
10161                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10162           else
10163             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
10164           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
10165         }
10166
10167       mask = ix86_build_signbit_mask (mode, 0, 0);
10168
10169       if (mode == SFmode)
10170         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
10171       else
10172         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
10173     }
10174   else
10175     {
10176       nmask = ix86_build_signbit_mask (mode, 0, 1);
10177       mask = ix86_build_signbit_mask (mode, 0, 0);
10178
10179       if (mode == SFmode)
10180         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
10181       else
10182         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
10183     }
10184 }
10185
10186 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
10187    be a constant, and so has already been expanded into a vector constant.  */
10188
10189 void
10190 ix86_split_copysign_const (rtx operands[])
10191 {
10192   enum machine_mode mode, vmode;
10193   rtx dest, op0, op1, mask, x;
10194
10195   dest = operands[0];
10196   op0 = operands[1];
10197   op1 = operands[2];
10198   mask = operands[3];
10199
10200   mode = GET_MODE (dest);
10201   vmode = GET_MODE (mask);
10202
10203   dest = simplify_gen_subreg (vmode, dest, mode, 0);
10204   x = gen_rtx_AND (vmode, dest, mask);
10205   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10206
10207   if (op0 != CONST0_RTX (vmode))
10208     {
10209       x = gen_rtx_IOR (vmode, dest, op0);
10210       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10211     }
10212 }
10213
10214 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
10215    so we have to do two masks.  */
10216
10217 void
10218 ix86_split_copysign_var (rtx operands[])
10219 {
10220   enum machine_mode mode, vmode;
10221   rtx dest, scratch, op0, op1, mask, nmask, x;
10222
10223   dest = operands[0];
10224   scratch = operands[1];
10225   op0 = operands[2];
10226   op1 = operands[3];
10227   nmask = operands[4];
10228   mask = operands[5];
10229
10230   mode = GET_MODE (dest);
10231   vmode = GET_MODE (mask);
10232
10233   if (rtx_equal_p (op0, op1))
10234     {
10235       /* Shouldn't happen often (it's useless, obviously), but when it does
10236          we'd generate incorrect code if we continue below.  */
10237       emit_move_insn (dest, op0);
10238       return;
10239     }
10240
10241   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
10242     {
10243       gcc_assert (REGNO (op1) == REGNO (scratch));
10244
10245       x = gen_rtx_AND (vmode, scratch, mask);
10246       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10247
10248       dest = mask;
10249       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10250       x = gen_rtx_NOT (vmode, dest);
10251       x = gen_rtx_AND (vmode, x, op0);
10252       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10253     }
10254   else
10255     {
10256       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
10257         {
10258           x = gen_rtx_AND (vmode, scratch, mask);
10259         }
10260       else                                              /* alternative 2,4 */
10261         {
10262           gcc_assert (REGNO (mask) == REGNO (scratch));
10263           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
10264           x = gen_rtx_AND (vmode, scratch, op1);
10265         }
10266       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10267
10268       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
10269         {
10270           dest = simplify_gen_subreg (vmode, op0, mode, 0);
10271           x = gen_rtx_AND (vmode, dest, nmask);
10272         }
10273       else                                              /* alternative 3,4 */
10274         {
10275           gcc_assert (REGNO (nmask) == REGNO (dest));
10276           dest = nmask;
10277           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10278           x = gen_rtx_AND (vmode, dest, op0);
10279         }
10280       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10281     }
10282
10283   x = gen_rtx_IOR (vmode, dest, scratch);
10284   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10285 }
10286
10287 /* Return TRUE or FALSE depending on whether the first SET in INSN
10288    has source and destination with matching CC modes, and that the
10289    CC mode is at least as constrained as REQ_MODE.  */
10290
10291 int
10292 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
10293 {
10294   rtx set;
10295   enum machine_mode set_mode;
10296
10297   set = PATTERN (insn);
10298   if (GET_CODE (set) == PARALLEL)
10299     set = XVECEXP (set, 0, 0);
10300   gcc_assert (GET_CODE (set) == SET);
10301   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
10302
10303   set_mode = GET_MODE (SET_DEST (set));
10304   switch (set_mode)
10305     {
10306     case CCNOmode:
10307       if (req_mode != CCNOmode
10308           && (req_mode != CCmode
10309               || XEXP (SET_SRC (set), 1) != const0_rtx))
10310         return 0;
10311       break;
10312     case CCmode:
10313       if (req_mode == CCGCmode)
10314         return 0;
10315       /* FALLTHRU */
10316     case CCGCmode:
10317       if (req_mode == CCGOCmode || req_mode == CCNOmode)
10318         return 0;
10319       /* FALLTHRU */
10320     case CCGOCmode:
10321       if (req_mode == CCZmode)
10322         return 0;
10323       /* FALLTHRU */
10324     case CCZmode:
10325       break;
10326
10327     default:
10328       gcc_unreachable ();
10329     }
10330
10331   return (GET_MODE (SET_SRC (set)) == set_mode);
10332 }
10333
10334 /* Generate insn patterns to do an integer compare of OPERANDS.  */
10335
10336 static rtx
10337 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
10338 {
10339   enum machine_mode cmpmode;
10340   rtx tmp, flags;
10341
10342   cmpmode = SELECT_CC_MODE (code, op0, op1);
10343   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
10344
10345   /* This is very simple, but making the interface the same as in the
10346      FP case makes the rest of the code easier.  */
10347   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
10348   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
10349
10350   /* Return the test that should be put into the flags user, i.e.
10351      the bcc, scc, or cmov instruction.  */
10352   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
10353 }
10354
10355 /* Figure out whether to use ordered or unordered fp comparisons.
10356    Return the appropriate mode to use.  */
10357
10358 enum machine_mode
10359 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
10360 {
10361   /* ??? In order to make all comparisons reversible, we do all comparisons
10362      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
10363      all forms trapping and nontrapping comparisons, we can make inequality
10364      comparisons trapping again, since it results in better code when using
10365      FCOM based compares.  */
10366   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
10367 }
10368
10369 enum machine_mode
10370 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
10371 {
10372   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10373     return ix86_fp_compare_mode (code);
10374   switch (code)
10375     {
10376       /* Only zero flag is needed.  */
10377     case EQ:                    /* ZF=0 */
10378     case NE:                    /* ZF!=0 */
10379       return CCZmode;
10380       /* Codes needing carry flag.  */
10381     case GEU:                   /* CF=0 */
10382     case GTU:                   /* CF=0 & ZF=0 */
10383     case LTU:                   /* CF=1 */
10384     case LEU:                   /* CF=1 | ZF=1 */
10385       return CCmode;
10386       /* Codes possibly doable only with sign flag when
10387          comparing against zero.  */
10388     case GE:                    /* SF=OF   or   SF=0 */
10389     case LT:                    /* SF<>OF  or   SF=1 */
10390       if (op1 == const0_rtx)
10391         return CCGOCmode;
10392       else
10393         /* For other cases Carry flag is not required.  */
10394         return CCGCmode;
10395       /* Codes doable only with sign flag when comparing
10396          against zero, but we miss jump instruction for it
10397          so we need to use relational tests against overflow
10398          that thus needs to be zero.  */
10399     case GT:                    /* ZF=0 & SF=OF */
10400     case LE:                    /* ZF=1 | SF<>OF */
10401       if (op1 == const0_rtx)
10402         return CCNOmode;
10403       else
10404         return CCGCmode;
10405       /* strcmp pattern do (use flags) and combine may ask us for proper
10406          mode.  */
10407     case USE:
10408       return CCmode;
10409     default:
10410       gcc_unreachable ();
10411     }
10412 }
10413
10414 /* Return the fixed registers used for condition codes.  */
10415
10416 static bool
10417 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
10418 {
10419   *p1 = FLAGS_REG;
10420   *p2 = FPSR_REG;
10421   return true;
10422 }
10423
10424 /* If two condition code modes are compatible, return a condition code
10425    mode which is compatible with both.  Otherwise, return
10426    VOIDmode.  */
10427
10428 static enum machine_mode
10429 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
10430 {
10431   if (m1 == m2)
10432     return m1;
10433
10434   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
10435     return VOIDmode;
10436
10437   if ((m1 == CCGCmode && m2 == CCGOCmode)
10438       || (m1 == CCGOCmode && m2 == CCGCmode))
10439     return CCGCmode;
10440
10441   switch (m1)
10442     {
10443     default:
10444       gcc_unreachable ();
10445
10446     case CCmode:
10447     case CCGCmode:
10448     case CCGOCmode:
10449     case CCNOmode:
10450     case CCZmode:
10451       switch (m2)
10452         {
10453         default:
10454           return VOIDmode;
10455
10456         case CCmode:
10457         case CCGCmode:
10458         case CCGOCmode:
10459         case CCNOmode:
10460         case CCZmode:
10461           return CCmode;
10462         }
10463
10464     case CCFPmode:
10465     case CCFPUmode:
10466       /* These are only compatible with themselves, which we already
10467          checked above.  */
10468       return VOIDmode;
10469     }
10470 }
10471
10472 /* Split comparison code CODE into comparisons we can do using branch
10473    instructions.  BYPASS_CODE is comparison code for branch that will
10474    branch around FIRST_CODE and SECOND_CODE.  If some of branches
10475    is not required, set value to UNKNOWN.
10476    We never require more than two branches.  */
10477
10478 void
10479 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
10480                           enum rtx_code *first_code,
10481                           enum rtx_code *second_code)
10482 {
10483   *first_code = code;
10484   *bypass_code = UNKNOWN;
10485   *second_code = UNKNOWN;
10486
10487   /* The fcomi comparison sets flags as follows:
10488
10489      cmp    ZF PF CF
10490      >      0  0  0
10491      <      0  0  1
10492      =      1  0  0
10493      un     1  1  1 */
10494
10495   switch (code)
10496     {
10497     case GT:                    /* GTU - CF=0 & ZF=0 */
10498     case GE:                    /* GEU - CF=0 */
10499     case ORDERED:               /* PF=0 */
10500     case UNORDERED:             /* PF=1 */
10501     case UNEQ:                  /* EQ - ZF=1 */
10502     case UNLT:                  /* LTU - CF=1 */
10503     case UNLE:                  /* LEU - CF=1 | ZF=1 */
10504     case LTGT:                  /* EQ - ZF=0 */
10505       break;
10506     case LT:                    /* LTU - CF=1 - fails on unordered */
10507       *first_code = UNLT;
10508       *bypass_code = UNORDERED;
10509       break;
10510     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
10511       *first_code = UNLE;
10512       *bypass_code = UNORDERED;
10513       break;
10514     case EQ:                    /* EQ - ZF=1 - fails on unordered */
10515       *first_code = UNEQ;
10516       *bypass_code = UNORDERED;
10517       break;
10518     case NE:                    /* NE - ZF=0 - fails on unordered */
10519       *first_code = LTGT;
10520       *second_code = UNORDERED;
10521       break;
10522     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10523       *first_code = GE;
10524       *second_code = UNORDERED;
10525       break;
10526     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10527       *first_code = GT;
10528       *second_code = UNORDERED;
10529       break;
10530     default:
10531       gcc_unreachable ();
10532     }
10533   if (!TARGET_IEEE_FP)
10534     {
10535       *second_code = UNKNOWN;
10536       *bypass_code = UNKNOWN;
10537     }
10538 }
10539
10540 /* Return cost of comparison done fcom + arithmetics operations on AX.
10541    All following functions do use number of instructions as a cost metrics.
10542    In future this should be tweaked to compute bytes for optimize_size and
10543    take into account performance of various instructions on various CPUs.  */
10544 static int
10545 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10546 {
10547   if (!TARGET_IEEE_FP)
10548     return 4;
10549   /* The cost of code output by ix86_expand_fp_compare.  */
10550   switch (code)
10551     {
10552     case UNLE:
10553     case UNLT:
10554     case LTGT:
10555     case GT:
10556     case GE:
10557     case UNORDERED:
10558     case ORDERED:
10559     case UNEQ:
10560       return 4;
10561       break;
10562     case LT:
10563     case NE:
10564     case EQ:
10565     case UNGE:
10566       return 5;
10567       break;
10568     case LE:
10569     case UNGT:
10570       return 6;
10571       break;
10572     default:
10573       gcc_unreachable ();
10574     }
10575 }
10576
10577 /* Return cost of comparison done using fcomi operation.
10578    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10579 static int
10580 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10581 {
10582   enum rtx_code bypass_code, first_code, second_code;
10583   /* Return arbitrarily high cost when instruction is not supported - this
10584      prevents gcc from using it.  */
10585   if (!TARGET_CMOVE)
10586     return 1024;
10587   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10588   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10589 }
10590
10591 /* Return cost of comparison done using sahf operation.
10592    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10593 static int
10594 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10595 {
10596   enum rtx_code bypass_code, first_code, second_code;
10597   /* Return arbitrarily high cost when instruction is not preferred - this
10598      avoids gcc from using it.  */
10599   if (!(TARGET_SAHF && (TARGET_USE_SAHF || optimize_size)))
10600     return 1024;
10601   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10602   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10603 }
10604
10605 /* Compute cost of the comparison done using any method.
10606    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10607 static int
10608 ix86_fp_comparison_cost (enum rtx_code code)
10609 {
10610   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10611   int min;
10612
10613   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10614   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10615
10616   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10617   if (min > sahf_cost)
10618     min = sahf_cost;
10619   if (min > fcomi_cost)
10620     min = fcomi_cost;
10621   return min;
10622 }
10623
10624 /* Return true if we should use an FCOMI instruction for this
10625    fp comparison.  */
10626
10627 int
10628 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
10629 {
10630   enum rtx_code swapped_code = swap_condition (code);
10631
10632   return ((ix86_fp_comparison_cost (code)
10633            == ix86_fp_comparison_fcomi_cost (code))
10634           || (ix86_fp_comparison_cost (swapped_code)
10635               == ix86_fp_comparison_fcomi_cost (swapped_code)));
10636 }
10637
10638 /* Swap, force into registers, or otherwise massage the two operands
10639    to a fp comparison.  The operands are updated in place; the new
10640    comparison code is returned.  */
10641
10642 static enum rtx_code
10643 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
10644 {
10645   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
10646   rtx op0 = *pop0, op1 = *pop1;
10647   enum machine_mode op_mode = GET_MODE (op0);
10648   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
10649
10650   /* All of the unordered compare instructions only work on registers.
10651      The same is true of the fcomi compare instructions.  The XFmode
10652      compare instructions require registers except when comparing
10653      against zero or when converting operand 1 from fixed point to
10654      floating point.  */
10655
10656   if (!is_sse
10657       && (fpcmp_mode == CCFPUmode
10658           || (op_mode == XFmode
10659               && ! (standard_80387_constant_p (op0) == 1
10660                     || standard_80387_constant_p (op1) == 1)
10661               && GET_CODE (op1) != FLOAT)
10662           || ix86_use_fcomi_compare (code)))
10663     {
10664       op0 = force_reg (op_mode, op0);
10665       op1 = force_reg (op_mode, op1);
10666     }
10667   else
10668     {
10669       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
10670          things around if they appear profitable, otherwise force op0
10671          into a register.  */
10672
10673       if (standard_80387_constant_p (op0) == 0
10674           || (MEM_P (op0)
10675               && ! (standard_80387_constant_p (op1) == 0
10676                     || MEM_P (op1))))
10677         {
10678           rtx tmp;
10679           tmp = op0, op0 = op1, op1 = tmp;
10680           code = swap_condition (code);
10681         }
10682
10683       if (!REG_P (op0))
10684         op0 = force_reg (op_mode, op0);
10685
10686       if (CONSTANT_P (op1))
10687         {
10688           int tmp = standard_80387_constant_p (op1);
10689           if (tmp == 0)
10690             op1 = validize_mem (force_const_mem (op_mode, op1));
10691           else if (tmp == 1)
10692             {
10693               if (TARGET_CMOVE)
10694                 op1 = force_reg (op_mode, op1);
10695             }
10696           else
10697             op1 = force_reg (op_mode, op1);
10698         }
10699     }
10700
10701   /* Try to rearrange the comparison to make it cheaper.  */
10702   if (ix86_fp_comparison_cost (code)
10703       > ix86_fp_comparison_cost (swap_condition (code))
10704       && (REG_P (op1) || !no_new_pseudos))
10705     {
10706       rtx tmp;
10707       tmp = op0, op0 = op1, op1 = tmp;
10708       code = swap_condition (code);
10709       if (!REG_P (op0))
10710         op0 = force_reg (op_mode, op0);
10711     }
10712
10713   *pop0 = op0;
10714   *pop1 = op1;
10715   return code;
10716 }
10717
10718 /* Convert comparison codes we use to represent FP comparison to integer
10719    code that will result in proper branch.  Return UNKNOWN if no such code
10720    is available.  */
10721
10722 enum rtx_code
10723 ix86_fp_compare_code_to_integer (enum rtx_code code)
10724 {
10725   switch (code)
10726     {
10727     case GT:
10728       return GTU;
10729     case GE:
10730       return GEU;
10731     case ORDERED:
10732     case UNORDERED:
10733       return code;
10734       break;
10735     case UNEQ:
10736       return EQ;
10737       break;
10738     case UNLT:
10739       return LTU;
10740       break;
10741     case UNLE:
10742       return LEU;
10743       break;
10744     case LTGT:
10745       return NE;
10746       break;
10747     default:
10748       return UNKNOWN;
10749     }
10750 }
10751
10752 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
10753
10754 static rtx
10755 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
10756                         rtx *second_test, rtx *bypass_test)
10757 {
10758   enum machine_mode fpcmp_mode, intcmp_mode;
10759   rtx tmp, tmp2;
10760   int cost = ix86_fp_comparison_cost (code);
10761   enum rtx_code bypass_code, first_code, second_code;
10762
10763   fpcmp_mode = ix86_fp_compare_mode (code);
10764   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
10765
10766   if (second_test)
10767     *second_test = NULL_RTX;
10768   if (bypass_test)
10769     *bypass_test = NULL_RTX;
10770
10771   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10772
10773   /* Do fcomi/sahf based test when profitable.  */
10774   if ((TARGET_CMOVE || TARGET_SAHF)
10775       && (bypass_code == UNKNOWN || bypass_test)
10776       && (second_code == UNKNOWN || second_test)
10777       && ix86_fp_comparison_arithmetics_cost (code) > cost)
10778     {
10779       if (TARGET_CMOVE)
10780         {
10781           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10782           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
10783                              tmp);
10784           emit_insn (tmp);
10785         }
10786       else
10787         {
10788           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10789           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10790           if (!scratch)
10791             scratch = gen_reg_rtx (HImode);
10792           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10793           emit_insn (gen_x86_sahf_1 (scratch));
10794         }
10795
10796       /* The FP codes work out to act like unsigned.  */
10797       intcmp_mode = fpcmp_mode;
10798       code = first_code;
10799       if (bypass_code != UNKNOWN)
10800         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
10801                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10802                                        const0_rtx);
10803       if (second_code != UNKNOWN)
10804         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
10805                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10806                                        const0_rtx);
10807     }
10808   else
10809     {
10810       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
10811       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10812       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10813       if (!scratch)
10814         scratch = gen_reg_rtx (HImode);
10815       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10816
10817       /* In the unordered case, we have to check C2 for NaN's, which
10818          doesn't happen to work out to anything nice combination-wise.
10819          So do some bit twiddling on the value we've got in AH to come
10820          up with an appropriate set of condition codes.  */
10821
10822       intcmp_mode = CCNOmode;
10823       switch (code)
10824         {
10825         case GT:
10826         case UNGT:
10827           if (code == GT || !TARGET_IEEE_FP)
10828             {
10829               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10830               code = EQ;
10831             }
10832           else
10833             {
10834               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10835               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10836               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
10837               intcmp_mode = CCmode;
10838               code = GEU;
10839             }
10840           break;
10841         case LT:
10842         case UNLT:
10843           if (code == LT && TARGET_IEEE_FP)
10844             {
10845               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10846               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
10847               intcmp_mode = CCmode;
10848               code = EQ;
10849             }
10850           else
10851             {
10852               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
10853               code = NE;
10854             }
10855           break;
10856         case GE:
10857         case UNGE:
10858           if (code == GE || !TARGET_IEEE_FP)
10859             {
10860               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
10861               code = EQ;
10862             }
10863           else
10864             {
10865               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10866               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10867                                              GEN_INT (0x01)));
10868               code = NE;
10869             }
10870           break;
10871         case LE:
10872         case UNLE:
10873           if (code == LE && TARGET_IEEE_FP)
10874             {
10875               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10876               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10877               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10878               intcmp_mode = CCmode;
10879               code = LTU;
10880             }
10881           else
10882             {
10883               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10884               code = NE;
10885             }
10886           break;
10887         case EQ:
10888         case UNEQ:
10889           if (code == EQ && TARGET_IEEE_FP)
10890             {
10891               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10892               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10893               intcmp_mode = CCmode;
10894               code = EQ;
10895             }
10896           else
10897             {
10898               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10899               code = NE;
10900               break;
10901             }
10902           break;
10903         case NE:
10904         case LTGT:
10905           if (code == NE && TARGET_IEEE_FP)
10906             {
10907               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10908               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10909                                              GEN_INT (0x40)));
10910               code = NE;
10911             }
10912           else
10913             {
10914               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10915               code = EQ;
10916             }
10917           break;
10918
10919         case UNORDERED:
10920           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10921           code = NE;
10922           break;
10923         case ORDERED:
10924           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10925           code = EQ;
10926           break;
10927
10928         default:
10929           gcc_unreachable ();
10930         }
10931     }
10932
10933   /* Return the test that should be put into the flags user, i.e.
10934      the bcc, scc, or cmov instruction.  */
10935   return gen_rtx_fmt_ee (code, VOIDmode,
10936                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
10937                          const0_rtx);
10938 }
10939
10940 rtx
10941 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
10942 {
10943   rtx op0, op1, ret;
10944   op0 = ix86_compare_op0;
10945   op1 = ix86_compare_op1;
10946
10947   if (second_test)
10948     *second_test = NULL_RTX;
10949   if (bypass_test)
10950     *bypass_test = NULL_RTX;
10951
10952   if (ix86_compare_emitted)
10953     {
10954       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
10955       ix86_compare_emitted = NULL_RTX;
10956     }
10957   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10958     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10959                                   second_test, bypass_test);
10960   else
10961     ret = ix86_expand_int_compare (code, op0, op1);
10962
10963   return ret;
10964 }
10965
10966 /* Return true if the CODE will result in nontrivial jump sequence.  */
10967 bool
10968 ix86_fp_jump_nontrivial_p (enum rtx_code code)
10969 {
10970   enum rtx_code bypass_code, first_code, second_code;
10971   if (!TARGET_CMOVE)
10972     return true;
10973   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10974   return bypass_code != UNKNOWN || second_code != UNKNOWN;
10975 }
10976
10977 void
10978 ix86_expand_branch (enum rtx_code code, rtx label)
10979 {
10980   rtx tmp;
10981
10982   /* If we have emitted a compare insn, go straight to simple.
10983      ix86_expand_compare won't emit anything if ix86_compare_emitted
10984      is non NULL.  */
10985   if (ix86_compare_emitted)
10986     goto simple;
10987
10988   switch (GET_MODE (ix86_compare_op0))
10989     {
10990     case QImode:
10991     case HImode:
10992     case SImode:
10993       simple:
10994       tmp = ix86_expand_compare (code, NULL, NULL);
10995       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10996                                   gen_rtx_LABEL_REF (VOIDmode, label),
10997                                   pc_rtx);
10998       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10999       return;
11000
11001     case SFmode:
11002     case DFmode:
11003     case XFmode:
11004       {
11005         rtvec vec;
11006         int use_fcomi;
11007         enum rtx_code bypass_code, first_code, second_code;
11008
11009         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
11010                                              &ix86_compare_op1);
11011
11012         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11013
11014         /* Check whether we will use the natural sequence with one jump.  If
11015            so, we can expand jump early.  Otherwise delay expansion by
11016            creating compound insn to not confuse optimizers.  */
11017         if (bypass_code == UNKNOWN && second_code == UNKNOWN
11018             && TARGET_CMOVE)
11019           {
11020             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
11021                                   gen_rtx_LABEL_REF (VOIDmode, label),
11022                                   pc_rtx, NULL_RTX, NULL_RTX);
11023           }
11024         else
11025           {
11026             tmp = gen_rtx_fmt_ee (code, VOIDmode,
11027                                   ix86_compare_op0, ix86_compare_op1);
11028             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11029                                         gen_rtx_LABEL_REF (VOIDmode, label),
11030                                         pc_rtx);
11031             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
11032
11033             use_fcomi = ix86_use_fcomi_compare (code);
11034             vec = rtvec_alloc (3 + !use_fcomi);
11035             RTVEC_ELT (vec, 0) = tmp;
11036             RTVEC_ELT (vec, 1)
11037               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
11038             RTVEC_ELT (vec, 2)
11039               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
11040             if (! use_fcomi)
11041               RTVEC_ELT (vec, 3)
11042                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
11043
11044             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
11045           }
11046         return;
11047       }
11048
11049     case DImode:
11050       if (TARGET_64BIT)
11051         goto simple;
11052     case TImode:
11053       /* Expand DImode branch into multiple compare+branch.  */
11054       {
11055         rtx lo[2], hi[2], label2;
11056         enum rtx_code code1, code2, code3;
11057         enum machine_mode submode;
11058
11059         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
11060           {
11061             tmp = ix86_compare_op0;
11062             ix86_compare_op0 = ix86_compare_op1;
11063             ix86_compare_op1 = tmp;
11064             code = swap_condition (code);
11065           }
11066         if (GET_MODE (ix86_compare_op0) == DImode)
11067           {
11068             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
11069             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
11070             submode = SImode;
11071           }
11072         else
11073           {
11074             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
11075             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
11076             submode = DImode;
11077           }
11078
11079         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
11080            avoid two branches.  This costs one extra insn, so disable when
11081            optimizing for size.  */
11082
11083         if ((code == EQ || code == NE)
11084             && (!optimize_size
11085                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
11086           {
11087             rtx xor0, xor1;
11088
11089             xor1 = hi[0];
11090             if (hi[1] != const0_rtx)
11091               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
11092                                    NULL_RTX, 0, OPTAB_WIDEN);
11093
11094             xor0 = lo[0];
11095             if (lo[1] != const0_rtx)
11096               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
11097                                    NULL_RTX, 0, OPTAB_WIDEN);
11098
11099             tmp = expand_binop (submode, ior_optab, xor1, xor0,
11100                                 NULL_RTX, 0, OPTAB_WIDEN);
11101
11102             ix86_compare_op0 = tmp;
11103             ix86_compare_op1 = const0_rtx;
11104             ix86_expand_branch (code, label);
11105             return;
11106           }
11107
11108         /* Otherwise, if we are doing less-than or greater-or-equal-than,
11109            op1 is a constant and the low word is zero, then we can just
11110            examine the high word.  */
11111
11112         if (CONST_INT_P (hi[1]) && lo[1] == const0_rtx)
11113           switch (code)
11114             {
11115             case LT: case LTU: case GE: case GEU:
11116               ix86_compare_op0 = hi[0];
11117               ix86_compare_op1 = hi[1];
11118               ix86_expand_branch (code, label);
11119               return;
11120             default:
11121               break;
11122             }
11123
11124         /* Otherwise, we need two or three jumps.  */
11125
11126         label2 = gen_label_rtx ();
11127
11128         code1 = code;
11129         code2 = swap_condition (code);
11130         code3 = unsigned_condition (code);
11131
11132         switch (code)
11133           {
11134           case LT: case GT: case LTU: case GTU:
11135             break;
11136
11137           case LE:   code1 = LT;  code2 = GT;  break;
11138           case GE:   code1 = GT;  code2 = LT;  break;
11139           case LEU:  code1 = LTU; code2 = GTU; break;
11140           case GEU:  code1 = GTU; code2 = LTU; break;
11141
11142           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
11143           case NE:   code2 = UNKNOWN; break;
11144
11145           default:
11146             gcc_unreachable ();
11147           }
11148
11149         /*
11150          * a < b =>
11151          *    if (hi(a) < hi(b)) goto true;
11152          *    if (hi(a) > hi(b)) goto false;
11153          *    if (lo(a) < lo(b)) goto true;
11154          *  false:
11155          */
11156
11157         ix86_compare_op0 = hi[0];
11158         ix86_compare_op1 = hi[1];
11159
11160         if (code1 != UNKNOWN)
11161           ix86_expand_branch (code1, label);
11162         if (code2 != UNKNOWN)
11163           ix86_expand_branch (code2, label2);
11164
11165         ix86_compare_op0 = lo[0];
11166         ix86_compare_op1 = lo[1];
11167         ix86_expand_branch (code3, label);
11168
11169         if (code2 != UNKNOWN)
11170           emit_label (label2);
11171         return;
11172       }
11173
11174     default:
11175       gcc_unreachable ();
11176     }
11177 }
11178
11179 /* Split branch based on floating point condition.  */
11180 void
11181 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
11182                       rtx target1, rtx target2, rtx tmp, rtx pushed)
11183 {
11184   rtx second, bypass;
11185   rtx label = NULL_RTX;
11186   rtx condition;
11187   int bypass_probability = -1, second_probability = -1, probability = -1;
11188   rtx i;
11189
11190   if (target2 != pc_rtx)
11191     {
11192       rtx tmp = target2;
11193       code = reverse_condition_maybe_unordered (code);
11194       target2 = target1;
11195       target1 = tmp;
11196     }
11197
11198   condition = ix86_expand_fp_compare (code, op1, op2,
11199                                       tmp, &second, &bypass);
11200
11201   /* Remove pushed operand from stack.  */
11202   if (pushed)
11203     ix86_free_from_memory (GET_MODE (pushed));
11204
11205   if (split_branch_probability >= 0)
11206     {
11207       /* Distribute the probabilities across the jumps.
11208          Assume the BYPASS and SECOND to be always test
11209          for UNORDERED.  */
11210       probability = split_branch_probability;
11211
11212       /* Value of 1 is low enough to make no need for probability
11213          to be updated.  Later we may run some experiments and see
11214          if unordered values are more frequent in practice.  */
11215       if (bypass)
11216         bypass_probability = 1;
11217       if (second)
11218         second_probability = 1;
11219     }
11220   if (bypass != NULL_RTX)
11221     {
11222       label = gen_label_rtx ();
11223       i = emit_jump_insn (gen_rtx_SET
11224                           (VOIDmode, pc_rtx,
11225                            gen_rtx_IF_THEN_ELSE (VOIDmode,
11226                                                  bypass,
11227                                                  gen_rtx_LABEL_REF (VOIDmode,
11228                                                                     label),
11229                                                  pc_rtx)));
11230       if (bypass_probability >= 0)
11231         REG_NOTES (i)
11232           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11233                                GEN_INT (bypass_probability),
11234                                REG_NOTES (i));
11235     }
11236   i = emit_jump_insn (gen_rtx_SET
11237                       (VOIDmode, pc_rtx,
11238                        gen_rtx_IF_THEN_ELSE (VOIDmode,
11239                                              condition, target1, target2)));
11240   if (probability >= 0)
11241     REG_NOTES (i)
11242       = gen_rtx_EXPR_LIST (REG_BR_PROB,
11243                            GEN_INT (probability),
11244                            REG_NOTES (i));
11245   if (second != NULL_RTX)
11246     {
11247       i = emit_jump_insn (gen_rtx_SET
11248                           (VOIDmode, pc_rtx,
11249                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
11250                                                  target2)));
11251       if (second_probability >= 0)
11252         REG_NOTES (i)
11253           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11254                                GEN_INT (second_probability),
11255                                REG_NOTES (i));
11256     }
11257   if (label != NULL_RTX)
11258     emit_label (label);
11259 }
11260
11261 int
11262 ix86_expand_setcc (enum rtx_code code, rtx dest)
11263 {
11264   rtx ret, tmp, tmpreg, equiv;
11265   rtx second_test, bypass_test;
11266
11267   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
11268     return 0; /* FAIL */
11269
11270   gcc_assert (GET_MODE (dest) == QImode);
11271
11272   ret = ix86_expand_compare (code, &second_test, &bypass_test);
11273   PUT_MODE (ret, QImode);
11274
11275   tmp = dest;
11276   tmpreg = dest;
11277
11278   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
11279   if (bypass_test || second_test)
11280     {
11281       rtx test = second_test;
11282       int bypass = 0;
11283       rtx tmp2 = gen_reg_rtx (QImode);
11284       if (bypass_test)
11285         {
11286           gcc_assert (!second_test);
11287           test = bypass_test;
11288           bypass = 1;
11289           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
11290         }
11291       PUT_MODE (test, QImode);
11292       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
11293
11294       if (bypass)
11295         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
11296       else
11297         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
11298     }
11299
11300   /* Attach a REG_EQUAL note describing the comparison result.  */
11301   if (ix86_compare_op0 && ix86_compare_op1)
11302     {
11303       equiv = simplify_gen_relational (code, QImode,
11304                                        GET_MODE (ix86_compare_op0),
11305                                        ix86_compare_op0, ix86_compare_op1);
11306       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
11307     }
11308
11309   return 1; /* DONE */
11310 }
11311
11312 /* Expand comparison setting or clearing carry flag.  Return true when
11313    successful and set pop for the operation.  */
11314 static bool
11315 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
11316 {
11317   enum machine_mode mode =
11318     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
11319
11320   /* Do not handle DImode compares that go through special path.  Also we can't
11321      deal with FP compares yet.  This is possible to add.  */
11322   if (mode == (TARGET_64BIT ? TImode : DImode))
11323     return false;
11324   if (FLOAT_MODE_P (mode))
11325     {
11326       rtx second_test = NULL, bypass_test = NULL;
11327       rtx compare_op, compare_seq;
11328
11329       /* Shortcut:  following common codes never translate into carry flag compares.  */
11330       if (code == EQ || code == NE || code == UNEQ || code == LTGT
11331           || code == ORDERED || code == UNORDERED)
11332         return false;
11333
11334       /* These comparisons require zero flag; swap operands so they won't.  */
11335       if ((code == GT || code == UNLE || code == LE || code == UNGT)
11336           && !TARGET_IEEE_FP)
11337         {
11338           rtx tmp = op0;
11339           op0 = op1;
11340           op1 = tmp;
11341           code = swap_condition (code);
11342         }
11343
11344       /* Try to expand the comparison and verify that we end up with carry flag
11345          based comparison.  This is fails to be true only when we decide to expand
11346          comparison using arithmetic that is not too common scenario.  */
11347       start_sequence ();
11348       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11349                                            &second_test, &bypass_test);
11350       compare_seq = get_insns ();
11351       end_sequence ();
11352
11353       if (second_test || bypass_test)
11354         return false;
11355       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11356           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11357         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
11358       else
11359         code = GET_CODE (compare_op);
11360       if (code != LTU && code != GEU)
11361         return false;
11362       emit_insn (compare_seq);
11363       *pop = compare_op;
11364       return true;
11365     }
11366   if (!INTEGRAL_MODE_P (mode))
11367     return false;
11368   switch (code)
11369     {
11370     case LTU:
11371     case GEU:
11372       break;
11373
11374     /* Convert a==0 into (unsigned)a<1.  */
11375     case EQ:
11376     case NE:
11377       if (op1 != const0_rtx)
11378         return false;
11379       op1 = const1_rtx;
11380       code = (code == EQ ? LTU : GEU);
11381       break;
11382
11383     /* Convert a>b into b<a or a>=b-1.  */
11384     case GTU:
11385     case LEU:
11386       if (CONST_INT_P (op1))
11387         {
11388           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
11389           /* Bail out on overflow.  We still can swap operands but that
11390              would force loading of the constant into register.  */
11391           if (op1 == const0_rtx
11392               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
11393             return false;
11394           code = (code == GTU ? GEU : LTU);
11395         }
11396       else
11397         {
11398           rtx tmp = op1;
11399           op1 = op0;
11400           op0 = tmp;
11401           code = (code == GTU ? LTU : GEU);
11402         }
11403       break;
11404
11405     /* Convert a>=0 into (unsigned)a<0x80000000.  */
11406     case LT:
11407     case GE:
11408       if (mode == DImode || op1 != const0_rtx)
11409         return false;
11410       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11411       code = (code == LT ? GEU : LTU);
11412       break;
11413     case LE:
11414     case GT:
11415       if (mode == DImode || op1 != constm1_rtx)
11416         return false;
11417       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11418       code = (code == LE ? GEU : LTU);
11419       break;
11420
11421     default:
11422       return false;
11423     }
11424   /* Swapping operands may cause constant to appear as first operand.  */
11425   if (!nonimmediate_operand (op0, VOIDmode))
11426     {
11427       if (no_new_pseudos)
11428         return false;
11429       op0 = force_reg (mode, op0);
11430     }
11431   ix86_compare_op0 = op0;
11432   ix86_compare_op1 = op1;
11433   *pop = ix86_expand_compare (code, NULL, NULL);
11434   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
11435   return true;
11436 }
11437
11438 int
11439 ix86_expand_int_movcc (rtx operands[])
11440 {
11441   enum rtx_code code = GET_CODE (operands[1]), compare_code;
11442   rtx compare_seq, compare_op;
11443   rtx second_test, bypass_test;
11444   enum machine_mode mode = GET_MODE (operands[0]);
11445   bool sign_bit_compare_p = false;;
11446
11447   start_sequence ();
11448   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11449   compare_seq = get_insns ();
11450   end_sequence ();
11451
11452   compare_code = GET_CODE (compare_op);
11453
11454   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
11455       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
11456     sign_bit_compare_p = true;
11457
11458   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
11459      HImode insns, we'd be swallowed in word prefix ops.  */
11460
11461   if ((mode != HImode || TARGET_FAST_PREFIX)
11462       && (mode != (TARGET_64BIT ? TImode : DImode))
11463       && CONST_INT_P (operands[2])
11464       && CONST_INT_P (operands[3]))
11465     {
11466       rtx out = operands[0];
11467       HOST_WIDE_INT ct = INTVAL (operands[2]);
11468       HOST_WIDE_INT cf = INTVAL (operands[3]);
11469       HOST_WIDE_INT diff;
11470
11471       diff = ct - cf;
11472       /*  Sign bit compares are better done using shifts than we do by using
11473           sbb.  */
11474       if (sign_bit_compare_p
11475           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11476                                              ix86_compare_op1, &compare_op))
11477         {
11478           /* Detect overlap between destination and compare sources.  */
11479           rtx tmp = out;
11480
11481           if (!sign_bit_compare_p)
11482             {
11483               bool fpcmp = false;
11484
11485               compare_code = GET_CODE (compare_op);
11486
11487               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11488                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11489                 {
11490                   fpcmp = true;
11491                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
11492                 }
11493
11494               /* To simplify rest of code, restrict to the GEU case.  */
11495               if (compare_code == LTU)
11496                 {
11497                   HOST_WIDE_INT tmp = ct;
11498                   ct = cf;
11499                   cf = tmp;
11500                   compare_code = reverse_condition (compare_code);
11501                   code = reverse_condition (code);
11502                 }
11503               else
11504                 {
11505                   if (fpcmp)
11506                     PUT_CODE (compare_op,
11507                               reverse_condition_maybe_unordered
11508                                 (GET_CODE (compare_op)));
11509                   else
11510                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11511                 }
11512               diff = ct - cf;
11513
11514               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
11515                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
11516                 tmp = gen_reg_rtx (mode);
11517
11518               if (mode == DImode)
11519                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
11520               else
11521                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
11522             }
11523           else
11524             {
11525               if (code == GT || code == GE)
11526                 code = reverse_condition (code);
11527               else
11528                 {
11529                   HOST_WIDE_INT tmp = ct;
11530                   ct = cf;
11531                   cf = tmp;
11532                   diff = ct - cf;
11533                 }
11534               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
11535                                      ix86_compare_op1, VOIDmode, 0, -1);
11536             }
11537
11538           if (diff == 1)
11539             {
11540               /*
11541                * cmpl op0,op1
11542                * sbbl dest,dest
11543                * [addl dest, ct]
11544                *
11545                * Size 5 - 8.
11546                */
11547               if (ct)
11548                 tmp = expand_simple_binop (mode, PLUS,
11549                                            tmp, GEN_INT (ct),
11550                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11551             }
11552           else if (cf == -1)
11553             {
11554               /*
11555                * cmpl op0,op1
11556                * sbbl dest,dest
11557                * orl $ct, dest
11558                *
11559                * Size 8.
11560                */
11561               tmp = expand_simple_binop (mode, IOR,
11562                                          tmp, GEN_INT (ct),
11563                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11564             }
11565           else if (diff == -1 && ct)
11566             {
11567               /*
11568                * cmpl op0,op1
11569                * sbbl dest,dest
11570                * notl dest
11571                * [addl dest, cf]
11572                *
11573                * Size 8 - 11.
11574                */
11575               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11576               if (cf)
11577                 tmp = expand_simple_binop (mode, PLUS,
11578                                            copy_rtx (tmp), GEN_INT (cf),
11579                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11580             }
11581           else
11582             {
11583               /*
11584                * cmpl op0,op1
11585                * sbbl dest,dest
11586                * [notl dest]
11587                * andl cf - ct, dest
11588                * [addl dest, ct]
11589                *
11590                * Size 8 - 11.
11591                */
11592
11593               if (cf == 0)
11594                 {
11595                   cf = ct;
11596                   ct = 0;
11597                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11598                 }
11599
11600               tmp = expand_simple_binop (mode, AND,
11601                                          copy_rtx (tmp),
11602                                          gen_int_mode (cf - ct, mode),
11603                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11604               if (ct)
11605                 tmp = expand_simple_binop (mode, PLUS,
11606                                            copy_rtx (tmp), GEN_INT (ct),
11607                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11608             }
11609
11610           if (!rtx_equal_p (tmp, out))
11611             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
11612
11613           return 1; /* DONE */
11614         }
11615
11616       if (diff < 0)
11617         {
11618           HOST_WIDE_INT tmp;
11619           tmp = ct, ct = cf, cf = tmp;
11620           diff = -diff;
11621           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11622             {
11623               /* We may be reversing unordered compare to normal compare, that
11624                  is not valid in general (we may convert non-trapping condition
11625                  to trapping one), however on i386 we currently emit all
11626                  comparisons unordered.  */
11627               compare_code = reverse_condition_maybe_unordered (compare_code);
11628               code = reverse_condition_maybe_unordered (code);
11629             }
11630           else
11631             {
11632               compare_code = reverse_condition (compare_code);
11633               code = reverse_condition (code);
11634             }
11635         }
11636
11637       compare_code = UNKNOWN;
11638       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
11639           && CONST_INT_P (ix86_compare_op1))
11640         {
11641           if (ix86_compare_op1 == const0_rtx
11642               && (code == LT || code == GE))
11643             compare_code = code;
11644           else if (ix86_compare_op1 == constm1_rtx)
11645             {
11646               if (code == LE)
11647                 compare_code = LT;
11648               else if (code == GT)
11649                 compare_code = GE;
11650             }
11651         }
11652
11653       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11654       if (compare_code != UNKNOWN
11655           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11656           && (cf == -1 || ct == -1))
11657         {
11658           /* If lea code below could be used, only optimize
11659              if it results in a 2 insn sequence.  */
11660
11661           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11662                  || diff == 3 || diff == 5 || diff == 9)
11663               || (compare_code == LT && ct == -1)
11664               || (compare_code == GE && cf == -1))
11665             {
11666               /*
11667                * notl op1       (if necessary)
11668                * sarl $31, op1
11669                * orl cf, op1
11670                */
11671               if (ct != -1)
11672                 {
11673                   cf = ct;
11674                   ct = -1;
11675                   code = reverse_condition (code);
11676                 }
11677
11678               out = emit_store_flag (out, code, ix86_compare_op0,
11679                                      ix86_compare_op1, VOIDmode, 0, -1);
11680
11681               out = expand_simple_binop (mode, IOR,
11682                                          out, GEN_INT (cf),
11683                                          out, 1, OPTAB_DIRECT);
11684               if (out != operands[0])
11685                 emit_move_insn (operands[0], out);
11686
11687               return 1; /* DONE */
11688             }
11689         }
11690
11691
11692       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
11693            || diff == 3 || diff == 5 || diff == 9)
11694           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
11695           && (mode != DImode
11696               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
11697         {
11698           /*
11699            * xorl dest,dest
11700            * cmpl op1,op2
11701            * setcc dest
11702            * lea cf(dest*(ct-cf)),dest
11703            *
11704            * Size 14.
11705            *
11706            * This also catches the degenerate setcc-only case.
11707            */
11708
11709           rtx tmp;
11710           int nops;
11711
11712           out = emit_store_flag (out, code, ix86_compare_op0,
11713                                  ix86_compare_op1, VOIDmode, 0, 1);
11714
11715           nops = 0;
11716           /* On x86_64 the lea instruction operates on Pmode, so we need
11717              to get arithmetics done in proper mode to match.  */
11718           if (diff == 1)
11719             tmp = copy_rtx (out);
11720           else
11721             {
11722               rtx out1;
11723               out1 = copy_rtx (out);
11724               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
11725               nops++;
11726               if (diff & 1)
11727                 {
11728                   tmp = gen_rtx_PLUS (mode, tmp, out1);
11729                   nops++;
11730                 }
11731             }
11732           if (cf != 0)
11733             {
11734               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
11735               nops++;
11736             }
11737           if (!rtx_equal_p (tmp, out))
11738             {
11739               if (nops == 1)
11740                 out = force_operand (tmp, copy_rtx (out));
11741               else
11742                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
11743             }
11744           if (!rtx_equal_p (out, operands[0]))
11745             emit_move_insn (operands[0], copy_rtx (out));
11746
11747           return 1; /* DONE */
11748         }
11749
11750       /*
11751        * General case:                  Jumpful:
11752        *   xorl dest,dest               cmpl op1, op2
11753        *   cmpl op1, op2                movl ct, dest
11754        *   setcc dest                   jcc 1f
11755        *   decl dest                    movl cf, dest
11756        *   andl (cf-ct),dest            1:
11757        *   addl ct,dest
11758        *
11759        * Size 20.                       Size 14.
11760        *
11761        * This is reasonably steep, but branch mispredict costs are
11762        * high on modern cpus, so consider failing only if optimizing
11763        * for space.
11764        */
11765
11766       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11767           && BRANCH_COST >= 2)
11768         {
11769           if (cf == 0)
11770             {
11771               cf = ct;
11772               ct = 0;
11773               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11774                 /* We may be reversing unordered compare to normal compare,
11775                    that is not valid in general (we may convert non-trapping
11776                    condition to trapping one), however on i386 we currently
11777                    emit all comparisons unordered.  */
11778                 code = reverse_condition_maybe_unordered (code);
11779               else
11780                 {
11781                   code = reverse_condition (code);
11782                   if (compare_code != UNKNOWN)
11783                     compare_code = reverse_condition (compare_code);
11784                 }
11785             }
11786
11787           if (compare_code != UNKNOWN)
11788             {
11789               /* notl op1       (if needed)
11790                  sarl $31, op1
11791                  andl (cf-ct), op1
11792                  addl ct, op1
11793
11794                  For x < 0 (resp. x <= -1) there will be no notl,
11795                  so if possible swap the constants to get rid of the
11796                  complement.
11797                  True/false will be -1/0 while code below (store flag
11798                  followed by decrement) is 0/-1, so the constants need
11799                  to be exchanged once more.  */
11800
11801               if (compare_code == GE || !cf)
11802                 {
11803                   code = reverse_condition (code);
11804                   compare_code = LT;
11805                 }
11806               else
11807                 {
11808                   HOST_WIDE_INT tmp = cf;
11809                   cf = ct;
11810                   ct = tmp;
11811                 }
11812
11813               out = emit_store_flag (out, code, ix86_compare_op0,
11814                                      ix86_compare_op1, VOIDmode, 0, -1);
11815             }
11816           else
11817             {
11818               out = emit_store_flag (out, code, ix86_compare_op0,
11819                                      ix86_compare_op1, VOIDmode, 0, 1);
11820
11821               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
11822                                          copy_rtx (out), 1, OPTAB_DIRECT);
11823             }
11824
11825           out = expand_simple_binop (mode, AND, copy_rtx (out),
11826                                      gen_int_mode (cf - ct, mode),
11827                                      copy_rtx (out), 1, OPTAB_DIRECT);
11828           if (ct)
11829             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
11830                                        copy_rtx (out), 1, OPTAB_DIRECT);
11831           if (!rtx_equal_p (out, operands[0]))
11832             emit_move_insn (operands[0], copy_rtx (out));
11833
11834           return 1; /* DONE */
11835         }
11836     }
11837
11838   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11839     {
11840       /* Try a few things more with specific constants and a variable.  */
11841
11842       optab op;
11843       rtx var, orig_out, out, tmp;
11844
11845       if (BRANCH_COST <= 2)
11846         return 0; /* FAIL */
11847
11848       /* If one of the two operands is an interesting constant, load a
11849          constant with the above and mask it in with a logical operation.  */
11850
11851       if (CONST_INT_P (operands[2]))
11852         {
11853           var = operands[3];
11854           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
11855             operands[3] = constm1_rtx, op = and_optab;
11856           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
11857             operands[3] = const0_rtx, op = ior_optab;
11858           else
11859             return 0; /* FAIL */
11860         }
11861       else if (CONST_INT_P (operands[3]))
11862         {
11863           var = operands[2];
11864           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
11865             operands[2] = constm1_rtx, op = and_optab;
11866           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
11867             operands[2] = const0_rtx, op = ior_optab;
11868           else
11869             return 0; /* FAIL */
11870         }
11871       else
11872         return 0; /* FAIL */
11873
11874       orig_out = operands[0];
11875       tmp = gen_reg_rtx (mode);
11876       operands[0] = tmp;
11877
11878       /* Recurse to get the constant loaded.  */
11879       if (ix86_expand_int_movcc (operands) == 0)
11880         return 0; /* FAIL */
11881
11882       /* Mask in the interesting variable.  */
11883       out = expand_binop (mode, op, var, tmp, orig_out, 0,
11884                           OPTAB_WIDEN);
11885       if (!rtx_equal_p (out, orig_out))
11886         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
11887
11888       return 1; /* DONE */
11889     }
11890
11891   /*
11892    * For comparison with above,
11893    *
11894    * movl cf,dest
11895    * movl ct,tmp
11896    * cmpl op1,op2
11897    * cmovcc tmp,dest
11898    *
11899    * Size 15.
11900    */
11901
11902   if (! nonimmediate_operand (operands[2], mode))
11903     operands[2] = force_reg (mode, operands[2]);
11904   if (! nonimmediate_operand (operands[3], mode))
11905     operands[3] = force_reg (mode, operands[3]);
11906
11907   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11908     {
11909       rtx tmp = gen_reg_rtx (mode);
11910       emit_move_insn (tmp, operands[3]);
11911       operands[3] = tmp;
11912     }
11913   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11914     {
11915       rtx tmp = gen_reg_rtx (mode);
11916       emit_move_insn (tmp, operands[2]);
11917       operands[2] = tmp;
11918     }
11919
11920   if (! register_operand (operands[2], VOIDmode)
11921       && (mode == QImode
11922           || ! register_operand (operands[3], VOIDmode)))
11923     operands[2] = force_reg (mode, operands[2]);
11924
11925   if (mode == QImode
11926       && ! register_operand (operands[3], VOIDmode))
11927     operands[3] = force_reg (mode, operands[3]);
11928
11929   emit_insn (compare_seq);
11930   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11931                           gen_rtx_IF_THEN_ELSE (mode,
11932                                                 compare_op, operands[2],
11933                                                 operands[3])));
11934   if (bypass_test)
11935     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11936                             gen_rtx_IF_THEN_ELSE (mode,
11937                                   bypass_test,
11938                                   copy_rtx (operands[3]),
11939                                   copy_rtx (operands[0]))));
11940   if (second_test)
11941     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11942                             gen_rtx_IF_THEN_ELSE (mode,
11943                                   second_test,
11944                                   copy_rtx (operands[2]),
11945                                   copy_rtx (operands[0]))));
11946
11947   return 1; /* DONE */
11948 }
11949
11950 /* Swap, force into registers, or otherwise massage the two operands
11951    to an sse comparison with a mask result.  Thus we differ a bit from
11952    ix86_prepare_fp_compare_args which expects to produce a flags result.
11953
11954    The DEST operand exists to help determine whether to commute commutative
11955    operators.  The POP0/POP1 operands are updated in place.  The new
11956    comparison code is returned, or UNKNOWN if not implementable.  */
11957
11958 static enum rtx_code
11959 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
11960                                   rtx *pop0, rtx *pop1)
11961 {
11962   rtx tmp;
11963
11964   switch (code)
11965     {
11966     case LTGT:
11967     case UNEQ:
11968       /* We have no LTGT as an operator.  We could implement it with
11969          NE & ORDERED, but this requires an extra temporary.  It's
11970          not clear that it's worth it.  */
11971       return UNKNOWN;
11972
11973     case LT:
11974     case LE:
11975     case UNGT:
11976     case UNGE:
11977       /* These are supported directly.  */
11978       break;
11979
11980     case EQ:
11981     case NE:
11982     case UNORDERED:
11983     case ORDERED:
11984       /* For commutative operators, try to canonicalize the destination
11985          operand to be first in the comparison - this helps reload to
11986          avoid extra moves.  */
11987       if (!dest || !rtx_equal_p (dest, *pop1))
11988         break;
11989       /* FALLTHRU */
11990
11991     case GE:
11992     case GT:
11993     case UNLE:
11994     case UNLT:
11995       /* These are not supported directly.  Swap the comparison operands
11996          to transform into something that is supported.  */
11997       tmp = *pop0;
11998       *pop0 = *pop1;
11999       *pop1 = tmp;
12000       code = swap_condition (code);
12001       break;
12002
12003     default:
12004       gcc_unreachable ();
12005     }
12006
12007   return code;
12008 }
12009
12010 /* Detect conditional moves that exactly match min/max operational
12011    semantics.  Note that this is IEEE safe, as long as we don't
12012    interchange the operands.
12013
12014    Returns FALSE if this conditional move doesn't match a MIN/MAX,
12015    and TRUE if the operation is successful and instructions are emitted.  */
12016
12017 static bool
12018 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
12019                            rtx cmp_op1, rtx if_true, rtx if_false)
12020 {
12021   enum machine_mode mode;
12022   bool is_min;
12023   rtx tmp;
12024
12025   if (code == LT)
12026     ;
12027   else if (code == UNGE)
12028     {
12029       tmp = if_true;
12030       if_true = if_false;
12031       if_false = tmp;
12032     }
12033   else
12034     return false;
12035
12036   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
12037     is_min = true;
12038   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
12039     is_min = false;
12040   else
12041     return false;
12042
12043   mode = GET_MODE (dest);
12044
12045   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
12046      but MODE may be a vector mode and thus not appropriate.  */
12047   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
12048     {
12049       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
12050       rtvec v;
12051
12052       if_true = force_reg (mode, if_true);
12053       v = gen_rtvec (2, if_true, if_false);
12054       tmp = gen_rtx_UNSPEC (mode, v, u);
12055     }
12056   else
12057     {
12058       code = is_min ? SMIN : SMAX;
12059       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
12060     }
12061
12062   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
12063   return true;
12064 }
12065
12066 /* Expand an sse vector comparison.  Return the register with the result.  */
12067
12068 static rtx
12069 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
12070                      rtx op_true, rtx op_false)
12071 {
12072   enum machine_mode mode = GET_MODE (dest);
12073   rtx x;
12074
12075   cmp_op0 = force_reg (mode, cmp_op0);
12076   if (!nonimmediate_operand (cmp_op1, mode))
12077     cmp_op1 = force_reg (mode, cmp_op1);
12078
12079   if (optimize
12080       || reg_overlap_mentioned_p (dest, op_true)
12081       || reg_overlap_mentioned_p (dest, op_false))
12082     dest = gen_reg_rtx (mode);
12083
12084   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
12085   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12086
12087   return dest;
12088 }
12089
12090 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
12091    operations.  This is used for both scalar and vector conditional moves.  */
12092
12093 static void
12094 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
12095 {
12096   enum machine_mode mode = GET_MODE (dest);
12097   rtx t2, t3, x;
12098
12099   if (op_false == CONST0_RTX (mode))
12100     {
12101       op_true = force_reg (mode, op_true);
12102       x = gen_rtx_AND (mode, cmp, op_true);
12103       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12104     }
12105   else if (op_true == CONST0_RTX (mode))
12106     {
12107       op_false = force_reg (mode, op_false);
12108       x = gen_rtx_NOT (mode, cmp);
12109       x = gen_rtx_AND (mode, x, op_false);
12110       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12111     }
12112   else
12113     {
12114       op_true = force_reg (mode, op_true);
12115       op_false = force_reg (mode, op_false);
12116
12117       t2 = gen_reg_rtx (mode);
12118       if (optimize)
12119         t3 = gen_reg_rtx (mode);
12120       else
12121         t3 = dest;
12122
12123       x = gen_rtx_AND (mode, op_true, cmp);
12124       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
12125
12126       x = gen_rtx_NOT (mode, cmp);
12127       x = gen_rtx_AND (mode, x, op_false);
12128       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
12129
12130       x = gen_rtx_IOR (mode, t3, t2);
12131       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12132     }
12133 }
12134
12135 /* Expand a floating-point conditional move.  Return true if successful.  */
12136
12137 int
12138 ix86_expand_fp_movcc (rtx operands[])
12139 {
12140   enum machine_mode mode = GET_MODE (operands[0]);
12141   enum rtx_code code = GET_CODE (operands[1]);
12142   rtx tmp, compare_op, second_test, bypass_test;
12143
12144   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
12145     {
12146       enum machine_mode cmode;
12147
12148       /* Since we've no cmove for sse registers, don't force bad register
12149          allocation just to gain access to it.  Deny movcc when the
12150          comparison mode doesn't match the move mode.  */
12151       cmode = GET_MODE (ix86_compare_op0);
12152       if (cmode == VOIDmode)
12153         cmode = GET_MODE (ix86_compare_op1);
12154       if (cmode != mode)
12155         return 0;
12156
12157       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12158                                                &ix86_compare_op0,
12159                                                &ix86_compare_op1);
12160       if (code == UNKNOWN)
12161         return 0;
12162
12163       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
12164                                      ix86_compare_op1, operands[2],
12165                                      operands[3]))
12166         return 1;
12167
12168       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
12169                                  ix86_compare_op1, operands[2], operands[3]);
12170       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
12171       return 1;
12172     }
12173
12174   /* The floating point conditional move instructions don't directly
12175      support conditions resulting from a signed integer comparison.  */
12176
12177   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
12178
12179   /* The floating point conditional move instructions don't directly
12180      support signed integer comparisons.  */
12181
12182   if (!fcmov_comparison_operator (compare_op, VOIDmode))
12183     {
12184       gcc_assert (!second_test && !bypass_test);
12185       tmp = gen_reg_rtx (QImode);
12186       ix86_expand_setcc (code, tmp);
12187       code = NE;
12188       ix86_compare_op0 = tmp;
12189       ix86_compare_op1 = const0_rtx;
12190       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
12191     }
12192   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12193     {
12194       tmp = gen_reg_rtx (mode);
12195       emit_move_insn (tmp, operands[3]);
12196       operands[3] = tmp;
12197     }
12198   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12199     {
12200       tmp = gen_reg_rtx (mode);
12201       emit_move_insn (tmp, operands[2]);
12202       operands[2] = tmp;
12203     }
12204
12205   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12206                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
12207                                                 operands[2], operands[3])));
12208   if (bypass_test)
12209     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12210                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
12211                                                   operands[3], operands[0])));
12212   if (second_test)
12213     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12214                             gen_rtx_IF_THEN_ELSE (mode, second_test,
12215                                                   operands[2], operands[0])));
12216
12217   return 1;
12218 }
12219
12220 /* Expand a floating-point vector conditional move; a vcond operation
12221    rather than a movcc operation.  */
12222
12223 bool
12224 ix86_expand_fp_vcond (rtx operands[])
12225 {
12226   enum rtx_code code = GET_CODE (operands[3]);
12227   rtx cmp;
12228
12229   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12230                                            &operands[4], &operands[5]);
12231   if (code == UNKNOWN)
12232     return false;
12233
12234   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
12235                                  operands[5], operands[1], operands[2]))
12236     return true;
12237
12238   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
12239                              operands[1], operands[2]);
12240   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
12241   return true;
12242 }
12243
12244 /* Expand a signed integral vector conditional move.  */
12245
12246 bool
12247 ix86_expand_int_vcond (rtx operands[])
12248 {
12249   enum machine_mode mode = GET_MODE (operands[0]);
12250   enum rtx_code code = GET_CODE (operands[3]);
12251   bool negate = false;
12252   rtx x, cop0, cop1;
12253
12254   cop0 = operands[4];
12255   cop1 = operands[5];
12256
12257   /* Canonicalize the comparison to EQ, GT, GTU.  */
12258   switch (code)
12259     {
12260     case EQ:
12261     case GT:
12262     case GTU:
12263       break;
12264
12265     case NE:
12266     case LE:
12267     case LEU:
12268       code = reverse_condition (code);
12269       negate = true;
12270       break;
12271
12272     case GE:
12273     case GEU:
12274       code = reverse_condition (code);
12275       negate = true;
12276       /* FALLTHRU */
12277
12278     case LT:
12279     case LTU:
12280       code = swap_condition (code);
12281       x = cop0, cop0 = cop1, cop1 = x;
12282       break;
12283
12284     default:
12285       gcc_unreachable ();
12286     }
12287
12288   /* Unsigned parallel compare is not supported by the hardware.  Play some
12289      tricks to turn this into a signed comparison against 0.  */
12290   if (code == GTU)
12291     {
12292       cop0 = force_reg (mode, cop0);
12293
12294       switch (mode)
12295         {
12296         case V4SImode:
12297           {
12298             rtx t1, t2, mask;
12299
12300             /* Perform a parallel modulo subtraction.  */
12301             t1 = gen_reg_rtx (mode);
12302             emit_insn (gen_subv4si3 (t1, cop0, cop1));
12303
12304             /* Extract the original sign bit of op0.  */
12305             mask = GEN_INT (-0x80000000);
12306             mask = gen_rtx_CONST_VECTOR (mode,
12307                         gen_rtvec (4, mask, mask, mask, mask));
12308             mask = force_reg (mode, mask);
12309             t2 = gen_reg_rtx (mode);
12310             emit_insn (gen_andv4si3 (t2, cop0, mask));
12311
12312             /* XOR it back into the result of the subtraction.  This results
12313                in the sign bit set iff we saw unsigned underflow.  */
12314             x = gen_reg_rtx (mode);
12315             emit_insn (gen_xorv4si3 (x, t1, t2));
12316
12317             code = GT;
12318           }
12319           break;
12320
12321         case V16QImode:
12322         case V8HImode:
12323           /* Perform a parallel unsigned saturating subtraction.  */
12324           x = gen_reg_rtx (mode);
12325           emit_insn (gen_rtx_SET (VOIDmode, x,
12326                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
12327
12328           code = EQ;
12329           negate = !negate;
12330           break;
12331
12332         default:
12333           gcc_unreachable ();
12334         }
12335
12336       cop0 = x;
12337       cop1 = CONST0_RTX (mode);
12338     }
12339
12340   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
12341                            operands[1+negate], operands[2-negate]);
12342
12343   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
12344                          operands[2-negate]);
12345   return true;
12346 }
12347
12348 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
12349    true if we should do zero extension, else sign extension.  HIGH_P is
12350    true if we want the N/2 high elements, else the low elements.  */
12351
12352 void
12353 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
12354 {
12355   enum machine_mode imode = GET_MODE (operands[1]);
12356   rtx (*unpack)(rtx, rtx, rtx);
12357   rtx se, dest;
12358
12359   switch (imode)
12360     {
12361     case V16QImode:
12362       if (high_p)
12363         unpack = gen_vec_interleave_highv16qi;
12364       else
12365         unpack = gen_vec_interleave_lowv16qi;
12366       break;
12367     case V8HImode:
12368       if (high_p)
12369         unpack = gen_vec_interleave_highv8hi;
12370       else
12371         unpack = gen_vec_interleave_lowv8hi;
12372       break;
12373     case V4SImode:
12374       if (high_p)
12375         unpack = gen_vec_interleave_highv4si;
12376       else 
12377         unpack = gen_vec_interleave_lowv4si;
12378       break;
12379     default:
12380       gcc_unreachable (); 
12381     }
12382
12383   dest = gen_lowpart (imode, operands[0]);
12384
12385   if (unsigned_p)
12386     se = force_reg (imode, CONST0_RTX (imode));
12387   else
12388     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
12389                               operands[1], pc_rtx, pc_rtx);
12390
12391   emit_insn (unpack (dest, operands[1], se));
12392 }
12393
12394 /* Expand conditional increment or decrement using adb/sbb instructions.
12395    The default case using setcc followed by the conditional move can be
12396    done by generic code.  */
12397 int
12398 ix86_expand_int_addcc (rtx operands[])
12399 {
12400   enum rtx_code code = GET_CODE (operands[1]);
12401   rtx compare_op;
12402   rtx val = const0_rtx;
12403   bool fpcmp = false;
12404   enum machine_mode mode = GET_MODE (operands[0]);
12405
12406   if (operands[3] != const1_rtx
12407       && operands[3] != constm1_rtx)
12408     return 0;
12409   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
12410                                        ix86_compare_op1, &compare_op))
12411      return 0;
12412   code = GET_CODE (compare_op);
12413
12414   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12415       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12416     {
12417       fpcmp = true;
12418       code = ix86_fp_compare_code_to_integer (code);
12419     }
12420
12421   if (code != LTU)
12422     {
12423       val = constm1_rtx;
12424       if (fpcmp)
12425         PUT_CODE (compare_op,
12426                   reverse_condition_maybe_unordered
12427                     (GET_CODE (compare_op)));
12428       else
12429         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
12430     }
12431   PUT_MODE (compare_op, mode);
12432
12433   /* Construct either adc or sbb insn.  */
12434   if ((code == LTU) == (operands[3] == constm1_rtx))
12435     {
12436       switch (GET_MODE (operands[0]))
12437         {
12438           case QImode:
12439             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
12440             break;
12441           case HImode:
12442             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
12443             break;
12444           case SImode:
12445             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
12446             break;
12447           case DImode:
12448             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12449             break;
12450           default:
12451             gcc_unreachable ();
12452         }
12453     }
12454   else
12455     {
12456       switch (GET_MODE (operands[0]))
12457         {
12458           case QImode:
12459             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
12460             break;
12461           case HImode:
12462             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
12463             break;
12464           case SImode:
12465             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
12466             break;
12467           case DImode:
12468             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12469             break;
12470           default:
12471             gcc_unreachable ();
12472         }
12473     }
12474   return 1; /* DONE */
12475 }
12476
12477
12478 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
12479    works for floating pointer parameters and nonoffsetable memories.
12480    For pushes, it returns just stack offsets; the values will be saved
12481    in the right order.  Maximally three parts are generated.  */
12482
12483 static int
12484 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
12485 {
12486   int size;
12487
12488   if (!TARGET_64BIT)
12489     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
12490   else
12491     size = (GET_MODE_SIZE (mode) + 4) / 8;
12492
12493   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
12494   gcc_assert (size >= 2 && size <= 3);
12495
12496   /* Optimize constant pool reference to immediates.  This is used by fp
12497      moves, that force all constants to memory to allow combining.  */
12498   if (MEM_P (operand) && MEM_READONLY_P (operand))
12499     {
12500       rtx tmp = maybe_get_pool_constant (operand);
12501       if (tmp)
12502         operand = tmp;
12503     }
12504
12505   if (MEM_P (operand) && !offsettable_memref_p (operand))
12506     {
12507       /* The only non-offsetable memories we handle are pushes.  */
12508       int ok = push_operand (operand, VOIDmode);
12509
12510       gcc_assert (ok);
12511
12512       operand = copy_rtx (operand);
12513       PUT_MODE (operand, Pmode);
12514       parts[0] = parts[1] = parts[2] = operand;
12515       return size;
12516     }
12517
12518   if (GET_CODE (operand) == CONST_VECTOR)
12519     {
12520       enum machine_mode imode = int_mode_for_mode (mode);
12521       /* Caution: if we looked through a constant pool memory above,
12522          the operand may actually have a different mode now.  That's
12523          ok, since we want to pun this all the way back to an integer.  */
12524       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
12525       gcc_assert (operand != NULL);
12526       mode = imode;
12527     }
12528
12529   if (!TARGET_64BIT)
12530     {
12531       if (mode == DImode)
12532         split_di (&operand, 1, &parts[0], &parts[1]);
12533       else
12534         {
12535           if (REG_P (operand))
12536             {
12537               gcc_assert (reload_completed);
12538               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
12539               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
12540               if (size == 3)
12541                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
12542             }
12543           else if (offsettable_memref_p (operand))
12544             {
12545               operand = adjust_address (operand, SImode, 0);
12546               parts[0] = operand;
12547               parts[1] = adjust_address (operand, SImode, 4);
12548               if (size == 3)
12549                 parts[2] = adjust_address (operand, SImode, 8);
12550             }
12551           else if (GET_CODE (operand) == CONST_DOUBLE)
12552             {
12553               REAL_VALUE_TYPE r;
12554               long l[4];
12555
12556               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12557               switch (mode)
12558                 {
12559                 case XFmode:
12560                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
12561                   parts[2] = gen_int_mode (l[2], SImode);
12562                   break;
12563                 case DFmode:
12564                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
12565                   break;
12566                 default:
12567                   gcc_unreachable ();
12568                 }
12569               parts[1] = gen_int_mode (l[1], SImode);
12570               parts[0] = gen_int_mode (l[0], SImode);
12571             }
12572           else
12573             gcc_unreachable ();
12574         }
12575     }
12576   else
12577     {
12578       if (mode == TImode)
12579         split_ti (&operand, 1, &parts[0], &parts[1]);
12580       if (mode == XFmode || mode == TFmode)
12581         {
12582           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
12583           if (REG_P (operand))
12584             {
12585               gcc_assert (reload_completed);
12586               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
12587               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
12588             }
12589           else if (offsettable_memref_p (operand))
12590             {
12591               operand = adjust_address (operand, DImode, 0);
12592               parts[0] = operand;
12593               parts[1] = adjust_address (operand, upper_mode, 8);
12594             }
12595           else if (GET_CODE (operand) == CONST_DOUBLE)
12596             {
12597               REAL_VALUE_TYPE r;
12598               long l[4];
12599
12600               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12601               real_to_target (l, &r, mode);
12602
12603               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
12604               if (HOST_BITS_PER_WIDE_INT >= 64)
12605                 parts[0]
12606                   = gen_int_mode
12607                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
12608                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
12609                        DImode);
12610               else
12611                 parts[0] = immed_double_const (l[0], l[1], DImode);
12612
12613               if (upper_mode == SImode)
12614                 parts[1] = gen_int_mode (l[2], SImode);
12615               else if (HOST_BITS_PER_WIDE_INT >= 64)
12616                 parts[1]
12617                   = gen_int_mode
12618                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
12619                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
12620                        DImode);
12621               else
12622                 parts[1] = immed_double_const (l[2], l[3], DImode);
12623             }
12624           else
12625             gcc_unreachable ();
12626         }
12627     }
12628
12629   return size;
12630 }
12631
12632 /* Emit insns to perform a move or push of DI, DF, and XF values.
12633    Return false when normal moves are needed; true when all required
12634    insns have been emitted.  Operands 2-4 contain the input values
12635    int the correct order; operands 5-7 contain the output values.  */
12636
12637 void
12638 ix86_split_long_move (rtx operands[])
12639 {
12640   rtx part[2][3];
12641   int nparts;
12642   int push = 0;
12643   int collisions = 0;
12644   enum machine_mode mode = GET_MODE (operands[0]);
12645
12646   /* The DFmode expanders may ask us to move double.
12647      For 64bit target this is single move.  By hiding the fact
12648      here we simplify i386.md splitters.  */
12649   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
12650     {
12651       /* Optimize constant pool reference to immediates.  This is used by
12652          fp moves, that force all constants to memory to allow combining.  */
12653
12654       if (MEM_P (operands[1])
12655           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
12656           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
12657         operands[1] = get_pool_constant (XEXP (operands[1], 0));
12658       if (push_operand (operands[0], VOIDmode))
12659         {
12660           operands[0] = copy_rtx (operands[0]);
12661           PUT_MODE (operands[0], Pmode);
12662         }
12663       else
12664         operands[0] = gen_lowpart (DImode, operands[0]);
12665       operands[1] = gen_lowpart (DImode, operands[1]);
12666       emit_move_insn (operands[0], operands[1]);
12667       return;
12668     }
12669
12670   /* The only non-offsettable memory we handle is push.  */
12671   if (push_operand (operands[0], VOIDmode))
12672     push = 1;
12673   else
12674     gcc_assert (!MEM_P (operands[0])
12675                 || offsettable_memref_p (operands[0]));
12676
12677   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
12678   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
12679
12680   /* When emitting push, take care for source operands on the stack.  */
12681   if (push && MEM_P (operands[1])
12682       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
12683     {
12684       if (nparts == 3)
12685         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
12686                                      XEXP (part[1][2], 0));
12687       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
12688                                    XEXP (part[1][1], 0));
12689     }
12690
12691   /* We need to do copy in the right order in case an address register
12692      of the source overlaps the destination.  */
12693   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
12694     {
12695       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
12696         collisions++;
12697       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12698         collisions++;
12699       if (nparts == 3
12700           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
12701         collisions++;
12702
12703       /* Collision in the middle part can be handled by reordering.  */
12704       if (collisions == 1 && nparts == 3
12705           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12706         {
12707           rtx tmp;
12708           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
12709           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
12710         }
12711
12712       /* If there are more collisions, we can't handle it by reordering.
12713          Do an lea to the last part and use only one colliding move.  */
12714       else if (collisions > 1)
12715         {
12716           rtx base;
12717
12718           collisions = 1;
12719
12720           base = part[0][nparts - 1];
12721
12722           /* Handle the case when the last part isn't valid for lea.
12723              Happens in 64-bit mode storing the 12-byte XFmode.  */
12724           if (GET_MODE (base) != Pmode)
12725             base = gen_rtx_REG (Pmode, REGNO (base));
12726
12727           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
12728           part[1][0] = replace_equiv_address (part[1][0], base);
12729           part[1][1] = replace_equiv_address (part[1][1],
12730                                       plus_constant (base, UNITS_PER_WORD));
12731           if (nparts == 3)
12732             part[1][2] = replace_equiv_address (part[1][2],
12733                                       plus_constant (base, 8));
12734         }
12735     }
12736
12737   if (push)
12738     {
12739       if (!TARGET_64BIT)
12740         {
12741           if (nparts == 3)
12742             {
12743               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
12744                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
12745               emit_move_insn (part[0][2], part[1][2]);
12746             }
12747         }
12748       else
12749         {
12750           /* In 64bit mode we don't have 32bit push available.  In case this is
12751              register, it is OK - we will just use larger counterpart.  We also
12752              retype memory - these comes from attempt to avoid REX prefix on
12753              moving of second half of TFmode value.  */
12754           if (GET_MODE (part[1][1]) == SImode)
12755             {
12756               switch (GET_CODE (part[1][1]))
12757                 {
12758                 case MEM:
12759                   part[1][1] = adjust_address (part[1][1], DImode, 0);
12760                   break;
12761
12762                 case REG:
12763                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
12764                   break;
12765
12766                 default:
12767                   gcc_unreachable ();
12768                 }
12769
12770               if (GET_MODE (part[1][0]) == SImode)
12771                 part[1][0] = part[1][1];
12772             }
12773         }
12774       emit_move_insn (part[0][1], part[1][1]);
12775       emit_move_insn (part[0][0], part[1][0]);
12776       return;
12777     }
12778
12779   /* Choose correct order to not overwrite the source before it is copied.  */
12780   if ((REG_P (part[0][0])
12781        && REG_P (part[1][1])
12782        && (REGNO (part[0][0]) == REGNO (part[1][1])
12783            || (nparts == 3
12784                && REGNO (part[0][0]) == REGNO (part[1][2]))))
12785       || (collisions > 0
12786           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
12787     {
12788       if (nparts == 3)
12789         {
12790           operands[2] = part[0][2];
12791           operands[3] = part[0][1];
12792           operands[4] = part[0][0];
12793           operands[5] = part[1][2];
12794           operands[6] = part[1][1];
12795           operands[7] = part[1][0];
12796         }
12797       else
12798         {
12799           operands[2] = part[0][1];
12800           operands[3] = part[0][0];
12801           operands[5] = part[1][1];
12802           operands[6] = part[1][0];
12803         }
12804     }
12805   else
12806     {
12807       if (nparts == 3)
12808         {
12809           operands[2] = part[0][0];
12810           operands[3] = part[0][1];
12811           operands[4] = part[0][2];
12812           operands[5] = part[1][0];
12813           operands[6] = part[1][1];
12814           operands[7] = part[1][2];
12815         }
12816       else
12817         {
12818           operands[2] = part[0][0];
12819           operands[3] = part[0][1];
12820           operands[5] = part[1][0];
12821           operands[6] = part[1][1];
12822         }
12823     }
12824
12825   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
12826   if (optimize_size)
12827     {
12828       if (CONST_INT_P (operands[5])
12829           && operands[5] != const0_rtx
12830           && REG_P (operands[2]))
12831         {
12832           if (CONST_INT_P (operands[6])
12833               && INTVAL (operands[6]) == INTVAL (operands[5]))
12834             operands[6] = operands[2];
12835
12836           if (nparts == 3
12837               && CONST_INT_P (operands[7])
12838               && INTVAL (operands[7]) == INTVAL (operands[5]))
12839             operands[7] = operands[2];
12840         }
12841
12842       if (nparts == 3
12843           && CONST_INT_P (operands[6])
12844           && operands[6] != const0_rtx
12845           && REG_P (operands[3])
12846           && CONST_INT_P (operands[7])
12847           && INTVAL (operands[7]) == INTVAL (operands[6]))
12848         operands[7] = operands[3];
12849     }
12850
12851   emit_move_insn (operands[2], operands[5]);
12852   emit_move_insn (operands[3], operands[6]);
12853   if (nparts == 3)
12854     emit_move_insn (operands[4], operands[7]);
12855
12856   return;
12857 }
12858
12859 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
12860    left shift by a constant, either using a single shift or
12861    a sequence of add instructions.  */
12862
12863 static void
12864 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
12865 {
12866   if (count == 1)
12867     {
12868       emit_insn ((mode == DImode
12869                   ? gen_addsi3
12870                   : gen_adddi3) (operand, operand, operand));
12871     }
12872   else if (!optimize_size
12873            && count * ix86_cost->add <= ix86_cost->shift_const)
12874     {
12875       int i;
12876       for (i=0; i<count; i++)
12877         {
12878           emit_insn ((mode == DImode
12879                       ? gen_addsi3
12880                       : gen_adddi3) (operand, operand, operand));
12881         }
12882     }
12883   else
12884     emit_insn ((mode == DImode
12885                 ? gen_ashlsi3
12886                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
12887 }
12888
12889 void
12890 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
12891 {
12892   rtx low[2], high[2];
12893   int count;
12894   const int single_width = mode == DImode ? 32 : 64;
12895
12896   if (CONST_INT_P (operands[2]))
12897     {
12898       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12899       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12900
12901       if (count >= single_width)
12902         {
12903           emit_move_insn (high[0], low[1]);
12904           emit_move_insn (low[0], const0_rtx);
12905
12906           if (count > single_width)
12907             ix86_expand_ashl_const (high[0], count - single_width, mode);
12908         }
12909       else
12910         {
12911           if (!rtx_equal_p (operands[0], operands[1]))
12912             emit_move_insn (operands[0], operands[1]);
12913           emit_insn ((mode == DImode
12914                      ? gen_x86_shld_1
12915                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
12916           ix86_expand_ashl_const (low[0], count, mode);
12917         }
12918       return;
12919     }
12920
12921   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12922
12923   if (operands[1] == const1_rtx)
12924     {
12925       /* Assuming we've chosen a QImode capable registers, then 1 << N
12926          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
12927       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
12928         {
12929           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
12930
12931           ix86_expand_clear (low[0]);
12932           ix86_expand_clear (high[0]);
12933           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
12934
12935           d = gen_lowpart (QImode, low[0]);
12936           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12937           s = gen_rtx_EQ (QImode, flags, const0_rtx);
12938           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12939
12940           d = gen_lowpart (QImode, high[0]);
12941           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12942           s = gen_rtx_NE (QImode, flags, const0_rtx);
12943           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12944         }
12945
12946       /* Otherwise, we can get the same results by manually performing
12947          a bit extract operation on bit 5/6, and then performing the two
12948          shifts.  The two methods of getting 0/1 into low/high are exactly
12949          the same size.  Avoiding the shift in the bit extract case helps
12950          pentium4 a bit; no one else seems to care much either way.  */
12951       else
12952         {
12953           rtx x;
12954
12955           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
12956             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
12957           else
12958             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
12959           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
12960
12961           emit_insn ((mode == DImode
12962                       ? gen_lshrsi3
12963                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
12964           emit_insn ((mode == DImode
12965                       ? gen_andsi3
12966                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
12967           emit_move_insn (low[0], high[0]);
12968           emit_insn ((mode == DImode
12969                       ? gen_xorsi3
12970                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
12971         }
12972
12973       emit_insn ((mode == DImode
12974                     ? gen_ashlsi3
12975                     : gen_ashldi3) (low[0], low[0], operands[2]));
12976       emit_insn ((mode == DImode
12977                     ? gen_ashlsi3
12978                     : gen_ashldi3) (high[0], high[0], operands[2]));
12979       return;
12980     }
12981
12982   if (operands[1] == constm1_rtx)
12983     {
12984       /* For -1 << N, we can avoid the shld instruction, because we
12985          know that we're shifting 0...31/63 ones into a -1.  */
12986       emit_move_insn (low[0], constm1_rtx);
12987       if (optimize_size)
12988         emit_move_insn (high[0], low[0]);
12989       else
12990         emit_move_insn (high[0], constm1_rtx);
12991     }
12992   else
12993     {
12994       if (!rtx_equal_p (operands[0], operands[1]))
12995         emit_move_insn (operands[0], operands[1]);
12996
12997       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12998       emit_insn ((mode == DImode
12999                   ? gen_x86_shld_1
13000                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
13001     }
13002
13003   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
13004
13005   if (TARGET_CMOVE && scratch)
13006     {
13007       ix86_expand_clear (scratch);
13008       emit_insn ((mode == DImode
13009                   ? gen_x86_shift_adj_1
13010                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
13011     }
13012   else
13013     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
13014 }
13015
13016 void
13017 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
13018 {
13019   rtx low[2], high[2];
13020   int count;
13021   const int single_width = mode == DImode ? 32 : 64;
13022
13023   if (CONST_INT_P (operands[2]))
13024     {
13025       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13026       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13027
13028       if (count == single_width * 2 - 1)
13029         {
13030           emit_move_insn (high[0], high[1]);
13031           emit_insn ((mode == DImode
13032                       ? gen_ashrsi3
13033                       : gen_ashrdi3) (high[0], high[0],
13034                                       GEN_INT (single_width - 1)));
13035           emit_move_insn (low[0], high[0]);
13036
13037         }
13038       else if (count >= single_width)
13039         {
13040           emit_move_insn (low[0], high[1]);
13041           emit_move_insn (high[0], low[0]);
13042           emit_insn ((mode == DImode
13043                       ? gen_ashrsi3
13044                       : gen_ashrdi3) (high[0], high[0],
13045                                       GEN_INT (single_width - 1)));
13046           if (count > single_width)
13047             emit_insn ((mode == DImode
13048                         ? gen_ashrsi3
13049                         : gen_ashrdi3) (low[0], low[0],
13050                                         GEN_INT (count - single_width)));
13051         }
13052       else
13053         {
13054           if (!rtx_equal_p (operands[0], operands[1]))
13055             emit_move_insn (operands[0], operands[1]);
13056           emit_insn ((mode == DImode
13057                       ? gen_x86_shrd_1
13058                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13059           emit_insn ((mode == DImode
13060                       ? gen_ashrsi3
13061                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
13062         }
13063     }
13064   else
13065     {
13066       if (!rtx_equal_p (operands[0], operands[1]))
13067         emit_move_insn (operands[0], operands[1]);
13068
13069       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13070
13071       emit_insn ((mode == DImode
13072                   ? gen_x86_shrd_1
13073                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13074       emit_insn ((mode == DImode
13075                   ? gen_ashrsi3
13076                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
13077
13078       if (TARGET_CMOVE && scratch)
13079         {
13080           emit_move_insn (scratch, high[0]);
13081           emit_insn ((mode == DImode
13082                       ? gen_ashrsi3
13083                       : gen_ashrdi3) (scratch, scratch,
13084                                       GEN_INT (single_width - 1)));
13085           emit_insn ((mode == DImode
13086                       ? gen_x86_shift_adj_1
13087                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13088                                          scratch));
13089         }
13090       else
13091         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
13092     }
13093 }
13094
13095 void
13096 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
13097 {
13098   rtx low[2], high[2];
13099   int count;
13100   const int single_width = mode == DImode ? 32 : 64;
13101
13102   if (CONST_INT_P (operands[2]))
13103     {
13104       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13105       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13106
13107       if (count >= single_width)
13108         {
13109           emit_move_insn (low[0], high[1]);
13110           ix86_expand_clear (high[0]);
13111
13112           if (count > single_width)
13113             emit_insn ((mode == DImode
13114                         ? gen_lshrsi3
13115                         : gen_lshrdi3) (low[0], low[0],
13116                                         GEN_INT (count - single_width)));
13117         }
13118       else
13119         {
13120           if (!rtx_equal_p (operands[0], operands[1]))
13121             emit_move_insn (operands[0], operands[1]);
13122           emit_insn ((mode == DImode
13123                       ? gen_x86_shrd_1
13124                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13125           emit_insn ((mode == DImode
13126                       ? gen_lshrsi3
13127                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
13128         }
13129     }
13130   else
13131     {
13132       if (!rtx_equal_p (operands[0], operands[1]))
13133         emit_move_insn (operands[0], operands[1]);
13134
13135       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13136
13137       emit_insn ((mode == DImode
13138                   ? gen_x86_shrd_1
13139                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13140       emit_insn ((mode == DImode
13141                   ? gen_lshrsi3
13142                   : gen_lshrdi3) (high[0], high[0], operands[2]));
13143
13144       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
13145       if (TARGET_CMOVE && scratch)
13146         {
13147           ix86_expand_clear (scratch);
13148           emit_insn ((mode == DImode
13149                       ? gen_x86_shift_adj_1
13150                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13151                                                scratch));
13152         }
13153       else
13154         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
13155     }
13156 }
13157
13158 /* Predict just emitted jump instruction to be taken with probability PROB.  */
13159 static void
13160 predict_jump (int prob)
13161 {
13162   rtx insn = get_last_insn ();
13163   gcc_assert (JUMP_P (insn));
13164   REG_NOTES (insn)
13165     = gen_rtx_EXPR_LIST (REG_BR_PROB,
13166                          GEN_INT (prob),
13167                          REG_NOTES (insn));
13168 }
13169
13170 /* Helper function for the string operations below.  Dest VARIABLE whether
13171    it is aligned to VALUE bytes.  If true, jump to the label.  */
13172 static rtx
13173 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
13174 {
13175   rtx label = gen_label_rtx ();
13176   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
13177   if (GET_MODE (variable) == DImode)
13178     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
13179   else
13180     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
13181   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
13182                            1, label);
13183   if (epilogue)
13184     predict_jump (REG_BR_PROB_BASE * 50 / 100);
13185   else
13186     predict_jump (REG_BR_PROB_BASE * 90 / 100);
13187   return label;
13188 }
13189
13190 /* Adjust COUNTER by the VALUE.  */
13191 static void
13192 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
13193 {
13194   if (GET_MODE (countreg) == DImode)
13195     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
13196   else
13197     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
13198 }
13199
13200 /* Zero extend possibly SImode EXP to Pmode register.  */
13201 rtx
13202 ix86_zero_extend_to_Pmode (rtx exp)
13203 {
13204   rtx r;
13205   if (GET_MODE (exp) == VOIDmode)
13206     return force_reg (Pmode, exp);
13207   if (GET_MODE (exp) == Pmode)
13208     return copy_to_mode_reg (Pmode, exp);
13209   r = gen_reg_rtx (Pmode);
13210   emit_insn (gen_zero_extendsidi2 (r, exp));
13211   return r;
13212 }
13213
13214 /* Divide COUNTREG by SCALE.  */
13215 static rtx
13216 scale_counter (rtx countreg, int scale)
13217 {
13218   rtx sc;
13219   rtx piece_size_mask;
13220
13221   if (scale == 1)
13222     return countreg;
13223   if (CONST_INT_P (countreg))
13224     return GEN_INT (INTVAL (countreg) / scale);
13225   gcc_assert (REG_P (countreg));
13226
13227   piece_size_mask = GEN_INT (scale - 1);
13228   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
13229                             GEN_INT (exact_log2 (scale)),
13230                             NULL, 1, OPTAB_DIRECT);
13231   return sc;
13232 }
13233
13234 /* Return mode for the memcpy/memset loop counter.  Preffer SImode over DImode
13235    for constant loop counts.  */
13236
13237 static enum machine_mode
13238 counter_mode (rtx count_exp)
13239 {
13240   if (GET_MODE (count_exp) != VOIDmode)
13241     return GET_MODE (count_exp);
13242   if (GET_CODE (count_exp) != CONST_INT)
13243     return Pmode;
13244   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
13245     return DImode;
13246   return SImode;
13247 }
13248
13249 /* When SRCPTR is non-NULL, output simple loop to move memory
13250    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
13251    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
13252    equivalent loop to set memory by VALUE (supposed to be in MODE).
13253
13254    The size is rounded down to whole number of chunk size moved at once.
13255    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
13256   
13257
13258 static void
13259 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
13260                                rtx destptr, rtx srcptr, rtx value,
13261                                rtx count, enum machine_mode mode, int unroll,
13262                                int expected_size)
13263 {
13264   rtx out_label, top_label, iter, tmp;
13265   enum machine_mode iter_mode = counter_mode (count);
13266   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
13267   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
13268   rtx size;
13269   rtx x_addr;
13270   rtx y_addr;
13271   int i;
13272
13273   top_label = gen_label_rtx ();
13274   out_label = gen_label_rtx ();
13275   iter = gen_reg_rtx (iter_mode);
13276
13277   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
13278                               NULL, 1, OPTAB_DIRECT);
13279   /* Those two should combine.  */
13280   if (piece_size == const1_rtx)
13281     {
13282       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
13283                                true, out_label);
13284       predict_jump (REG_BR_PROB_BASE * 10 / 100);
13285     }
13286   emit_move_insn (iter, const0_rtx);
13287
13288   emit_label (top_label);
13289
13290   tmp = convert_modes (Pmode, iter_mode, iter, true);
13291   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
13292   destmem = change_address (destmem, mode, x_addr);
13293
13294   if (srcmem)
13295     {
13296       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
13297       srcmem = change_address (srcmem, mode, y_addr);
13298
13299       /* When unrolling for chips that reorder memory reads and writes,
13300          we can save registers by using single temporary.  
13301          Also using 4 temporaries is overkill in 32bit mode.  */
13302       if (!TARGET_64BIT && 0)
13303         {
13304           for (i = 0; i < unroll; i++)
13305             {
13306               if (i)
13307                 {
13308                   destmem =
13309                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13310                   srcmem =
13311                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13312                 }
13313               emit_move_insn (destmem, srcmem);
13314             }
13315         }
13316       else
13317         {
13318           rtx tmpreg[4];
13319           gcc_assert (unroll <= 4);
13320           for (i = 0; i < unroll; i++)
13321             {
13322               tmpreg[i] = gen_reg_rtx (mode);
13323               if (i)
13324                 {
13325                   srcmem =
13326                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13327                 }
13328               emit_move_insn (tmpreg[i], srcmem);
13329             }
13330           for (i = 0; i < unroll; i++)
13331             {
13332               if (i)
13333                 {
13334                   destmem =
13335                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13336                 }
13337               emit_move_insn (destmem, tmpreg[i]);
13338             }
13339         }
13340     }
13341   else
13342     for (i = 0; i < unroll; i++)
13343       {
13344         if (i)
13345           destmem =
13346             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13347         emit_move_insn (destmem, value);
13348       }
13349
13350   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
13351                              true, OPTAB_LIB_WIDEN);
13352   if (tmp != iter)
13353     emit_move_insn (iter, tmp);
13354
13355   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
13356                            true, top_label);
13357   if (expected_size != -1)
13358     {
13359       expected_size /= GET_MODE_SIZE (mode) * unroll;
13360       if (expected_size == 0)
13361         predict_jump (0);
13362       else if (expected_size > REG_BR_PROB_BASE)
13363         predict_jump (REG_BR_PROB_BASE - 1);
13364       else
13365         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
13366     }
13367   else
13368     predict_jump (REG_BR_PROB_BASE * 80 / 100);
13369   iter = ix86_zero_extend_to_Pmode (iter);
13370   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
13371                              true, OPTAB_LIB_WIDEN);
13372   if (tmp != destptr)
13373     emit_move_insn (destptr, tmp);
13374   if (srcptr)
13375     {
13376       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
13377                                  true, OPTAB_LIB_WIDEN);
13378       if (tmp != srcptr)
13379         emit_move_insn (srcptr, tmp);
13380     }
13381   emit_label (out_label);
13382 }
13383
13384 /* Output "rep; mov" instruction.  
13385    Arguments have same meaning as for previous function */
13386 static void
13387 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
13388                            rtx destptr, rtx srcptr,
13389                            rtx count,
13390                            enum machine_mode mode)
13391 {
13392   rtx destexp;
13393   rtx srcexp;
13394   rtx countreg;
13395
13396   /* If the size is known, it is shorter to use rep movs.  */
13397   if (mode == QImode && CONST_INT_P (count)
13398       && !(INTVAL (count) & 3))
13399     mode = SImode;
13400
13401   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13402     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13403   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
13404     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
13405   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13406   if (mode != QImode)
13407     {
13408       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13409                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13410       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13411       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
13412                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13413       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
13414     }
13415   else
13416     {
13417       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13418       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
13419     }
13420   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
13421                           destexp, srcexp));
13422 }
13423
13424 /* Output "rep; stos" instruction.  
13425    Arguments have same meaning as for previous function */
13426 static void
13427 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
13428                             rtx count,
13429                             enum machine_mode mode)
13430 {
13431   rtx destexp;
13432   rtx countreg;
13433
13434   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13435     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13436   value = force_reg (mode, gen_lowpart (mode, value));
13437   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13438   if (mode != QImode)
13439     {
13440       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13441                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13442       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13443     }
13444   else
13445     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13446   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
13447 }
13448
13449 static void
13450 emit_strmov (rtx destmem, rtx srcmem,
13451              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
13452 {
13453   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
13454   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
13455   emit_insn (gen_strmov (destptr, dest, srcptr, src));
13456 }
13457
13458 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
13459 static void
13460 expand_movmem_epilogue (rtx destmem, rtx srcmem,
13461                         rtx destptr, rtx srcptr, rtx count, int max_size)
13462 {
13463   rtx src, dest;
13464   if (CONST_INT_P (count))
13465     {
13466       HOST_WIDE_INT countval = INTVAL (count);
13467       int offset = 0;
13468
13469       if ((countval & 0x10) && max_size > 16)
13470         {
13471           if (TARGET_64BIT)
13472             {
13473               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13474               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
13475             }
13476           else
13477             gcc_unreachable ();
13478           offset += 16;
13479         }
13480       if ((countval & 0x08) && max_size > 8)
13481         {
13482           if (TARGET_64BIT)
13483             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13484           else
13485             {
13486               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13487               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
13488             }
13489           offset += 8;
13490         }
13491       if ((countval & 0x04) && max_size > 4)
13492         {
13493           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13494           offset += 4;
13495         }
13496       if ((countval & 0x02) && max_size > 2)
13497         {
13498           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
13499           offset += 2;
13500         }
13501       if ((countval & 0x01) && max_size > 1)
13502         {
13503           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
13504           offset += 1;
13505         }
13506       return;
13507     }
13508   if (max_size > 8)
13509     {
13510       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
13511                                     count, 1, OPTAB_DIRECT);
13512       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
13513                                      count, QImode, 1, 4);
13514       return;
13515     }
13516
13517   /* When there are stringops, we can cheaply increase dest and src pointers.
13518      Otherwise we save code size by maintaining offset (zero is readily
13519      available from preceding rep operation) and using x86 addressing modes.
13520    */
13521   if (TARGET_SINGLE_STRINGOP)
13522     {
13523       if (max_size > 4)
13524         {
13525           rtx label = ix86_expand_aligntest (count, 4, true);
13526           src = change_address (srcmem, SImode, srcptr);
13527           dest = change_address (destmem, SImode, destptr);
13528           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13529           emit_label (label);
13530           LABEL_NUSES (label) = 1;
13531         }
13532       if (max_size > 2)
13533         {
13534           rtx label = ix86_expand_aligntest (count, 2, true);
13535           src = change_address (srcmem, HImode, srcptr);
13536           dest = change_address (destmem, HImode, destptr);
13537           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13538           emit_label (label);
13539           LABEL_NUSES (label) = 1;
13540         }
13541       if (max_size > 1)
13542         {
13543           rtx label = ix86_expand_aligntest (count, 1, true);
13544           src = change_address (srcmem, QImode, srcptr);
13545           dest = change_address (destmem, QImode, destptr);
13546           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13547           emit_label (label);
13548           LABEL_NUSES (label) = 1;
13549         }
13550     }
13551   else
13552     {
13553       rtx offset = force_reg (Pmode, const0_rtx);
13554       rtx tmp;
13555
13556       if (max_size > 4)
13557         {
13558           rtx label = ix86_expand_aligntest (count, 4, true);
13559           src = change_address (srcmem, SImode, srcptr);
13560           dest = change_address (destmem, SImode, destptr);
13561           emit_move_insn (dest, src);
13562           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
13563                                      true, OPTAB_LIB_WIDEN);
13564           if (tmp != offset)
13565             emit_move_insn (offset, tmp);
13566           emit_label (label);
13567           LABEL_NUSES (label) = 1;
13568         }
13569       if (max_size > 2)
13570         {
13571           rtx label = ix86_expand_aligntest (count, 2, true);
13572           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13573           src = change_address (srcmem, HImode, tmp);
13574           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13575           dest = change_address (destmem, HImode, tmp);
13576           emit_move_insn (dest, src);
13577           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
13578                                      true, OPTAB_LIB_WIDEN);
13579           if (tmp != offset)
13580             emit_move_insn (offset, tmp);
13581           emit_label (label);
13582           LABEL_NUSES (label) = 1;
13583         }
13584       if (max_size > 1)
13585         {
13586           rtx label = ix86_expand_aligntest (count, 1, true);
13587           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13588           src = change_address (srcmem, QImode, tmp);
13589           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13590           dest = change_address (destmem, QImode, tmp);
13591           emit_move_insn (dest, src);
13592           emit_label (label);
13593           LABEL_NUSES (label) = 1;
13594         }
13595     }
13596 }
13597
13598 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13599 static void
13600 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
13601                                  rtx count, int max_size)
13602 {
13603   count =
13604     expand_simple_binop (counter_mode (count), AND, count,
13605                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
13606   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
13607                                  gen_lowpart (QImode, value), count, QImode,
13608                                  1, max_size / 2);
13609 }
13610
13611 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13612 static void
13613 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
13614 {
13615   rtx dest;
13616
13617   if (CONST_INT_P (count))
13618     {
13619       HOST_WIDE_INT countval = INTVAL (count);
13620       int offset = 0;
13621
13622       if ((countval & 0x10) && max_size > 16)
13623         {
13624           if (TARGET_64BIT)
13625             {
13626               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13627               emit_insn (gen_strset (destptr, dest, value));
13628               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
13629               emit_insn (gen_strset (destptr, dest, value));
13630             }
13631           else
13632             gcc_unreachable ();
13633           offset += 16;
13634         }
13635       if ((countval & 0x08) && max_size > 8)
13636         {
13637           if (TARGET_64BIT)
13638             {
13639               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13640               emit_insn (gen_strset (destptr, dest, value));
13641             }
13642           else
13643             {
13644               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13645               emit_insn (gen_strset (destptr, dest, value));
13646               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
13647               emit_insn (gen_strset (destptr, dest, value));
13648             }
13649           offset += 8;
13650         }
13651       if ((countval & 0x04) && max_size > 4)
13652         {
13653           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13654           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13655           offset += 4;
13656         }
13657       if ((countval & 0x02) && max_size > 2)
13658         {
13659           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
13660           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13661           offset += 2;
13662         }
13663       if ((countval & 0x01) && max_size > 1)
13664         {
13665           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
13666           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
13667           offset += 1;
13668         }
13669       return;
13670     }
13671   if (max_size > 32)
13672     {
13673       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
13674       return;
13675     }
13676   if (max_size > 16)
13677     {
13678       rtx label = ix86_expand_aligntest (count, 16, true);
13679       if (TARGET_64BIT)
13680         {
13681           dest = change_address (destmem, DImode, destptr);
13682           emit_insn (gen_strset (destptr, dest, value));
13683           emit_insn (gen_strset (destptr, dest, value));
13684         }
13685       else
13686         {
13687           dest = change_address (destmem, SImode, destptr);
13688           emit_insn (gen_strset (destptr, dest, value));
13689           emit_insn (gen_strset (destptr, dest, value));
13690           emit_insn (gen_strset (destptr, dest, value));
13691           emit_insn (gen_strset (destptr, dest, value));
13692         }
13693       emit_label (label);
13694       LABEL_NUSES (label) = 1;
13695     }
13696   if (max_size > 8)
13697     {
13698       rtx label = ix86_expand_aligntest (count, 8, true);
13699       if (TARGET_64BIT)
13700         {
13701           dest = change_address (destmem, DImode, destptr);
13702           emit_insn (gen_strset (destptr, dest, value));
13703         }
13704       else
13705         {
13706           dest = change_address (destmem, SImode, destptr);
13707           emit_insn (gen_strset (destptr, dest, value));
13708           emit_insn (gen_strset (destptr, dest, value));
13709         }
13710       emit_label (label);
13711       LABEL_NUSES (label) = 1;
13712     }
13713   if (max_size > 4)
13714     {
13715       rtx label = ix86_expand_aligntest (count, 4, true);
13716       dest = change_address (destmem, SImode, destptr);
13717       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13718       emit_label (label);
13719       LABEL_NUSES (label) = 1;
13720     }
13721   if (max_size > 2)
13722     {
13723       rtx label = ix86_expand_aligntest (count, 2, true);
13724       dest = change_address (destmem, HImode, destptr);
13725       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13726       emit_label (label);
13727       LABEL_NUSES (label) = 1;
13728     }
13729   if (max_size > 1)
13730     {
13731       rtx label = ix86_expand_aligntest (count, 1, true);
13732       dest = change_address (destmem, QImode, destptr);
13733       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
13734       emit_label (label);
13735       LABEL_NUSES (label) = 1;
13736     }
13737 }
13738
13739 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
13740    DESIRED_ALIGNMENT.  */
13741 static void
13742 expand_movmem_prologue (rtx destmem, rtx srcmem,
13743                         rtx destptr, rtx srcptr, rtx count,
13744                         int align, int desired_alignment)
13745 {
13746   if (align <= 1 && desired_alignment > 1)
13747     {
13748       rtx label = ix86_expand_aligntest (destptr, 1, false);
13749       srcmem = change_address (srcmem, QImode, srcptr);
13750       destmem = change_address (destmem, QImode, destptr);
13751       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13752       ix86_adjust_counter (count, 1);
13753       emit_label (label);
13754       LABEL_NUSES (label) = 1;
13755     }
13756   if (align <= 2 && desired_alignment > 2)
13757     {
13758       rtx label = ix86_expand_aligntest (destptr, 2, false);
13759       srcmem = change_address (srcmem, HImode, srcptr);
13760       destmem = change_address (destmem, HImode, destptr);
13761       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13762       ix86_adjust_counter (count, 2);
13763       emit_label (label);
13764       LABEL_NUSES (label) = 1;
13765     }
13766   if (align <= 4 && desired_alignment > 4)
13767     {
13768       rtx label = ix86_expand_aligntest (destptr, 4, false);
13769       srcmem = change_address (srcmem, SImode, srcptr);
13770       destmem = change_address (destmem, SImode, destptr);
13771       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
13772       ix86_adjust_counter (count, 4);
13773       emit_label (label);
13774       LABEL_NUSES (label) = 1;
13775     }
13776   gcc_assert (desired_alignment <= 8);
13777 }
13778
13779 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
13780    DESIRED_ALIGNMENT.  */
13781 static void
13782 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
13783                         int align, int desired_alignment)
13784 {
13785   if (align <= 1 && desired_alignment > 1)
13786     {
13787       rtx label = ix86_expand_aligntest (destptr, 1, false);
13788       destmem = change_address (destmem, QImode, destptr);
13789       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
13790       ix86_adjust_counter (count, 1);
13791       emit_label (label);
13792       LABEL_NUSES (label) = 1;
13793     }
13794   if (align <= 2 && desired_alignment > 2)
13795     {
13796       rtx label = ix86_expand_aligntest (destptr, 2, false);
13797       destmem = change_address (destmem, HImode, destptr);
13798       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
13799       ix86_adjust_counter (count, 2);
13800       emit_label (label);
13801       LABEL_NUSES (label) = 1;
13802     }
13803   if (align <= 4 && desired_alignment > 4)
13804     {
13805       rtx label = ix86_expand_aligntest (destptr, 4, false);
13806       destmem = change_address (destmem, SImode, destptr);
13807       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
13808       ix86_adjust_counter (count, 4);
13809       emit_label (label);
13810       LABEL_NUSES (label) = 1;
13811     }
13812   gcc_assert (desired_alignment <= 8);
13813 }
13814
13815 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
13816 static enum stringop_alg
13817 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
13818             int *dynamic_check)
13819 {
13820   const struct stringop_algs * algs;
13821
13822   *dynamic_check = -1;
13823   if (memset)
13824     algs = &ix86_cost->memset[TARGET_64BIT != 0];
13825   else
13826     algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
13827   if (stringop_alg != no_stringop)
13828     return stringop_alg;
13829   /* rep; movq or rep; movl is the smallest variant.  */
13830   else if (optimize_size)
13831     {
13832       if (!count || (count & 3))
13833         return rep_prefix_1_byte;
13834       else
13835         return rep_prefix_4_byte;
13836     }
13837   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
13838    */
13839   else if (expected_size != -1 && expected_size < 4)
13840     return loop_1_byte;
13841   else if (expected_size != -1)
13842     {
13843       unsigned int i;
13844       enum stringop_alg alg = libcall;
13845       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
13846         {
13847           gcc_assert (algs->size[i].max);
13848           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
13849             {
13850               if (algs->size[i].alg != libcall)
13851                 alg = algs->size[i].alg;
13852               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
13853                  last non-libcall inline algorithm.  */
13854               if (TARGET_INLINE_ALL_STRINGOPS)
13855                 {
13856                   /* When the current size is best to be copied by a libcall,
13857                      but we are still forced to inline, run the heuristic bellow
13858                      that will pick code for medium sized blocks.  */
13859                   if (alg != libcall)
13860                     return alg;
13861                   break;
13862                 }
13863               else
13864                 return algs->size[i].alg;
13865             }
13866         }
13867       gcc_assert (TARGET_INLINE_ALL_STRINGOPS);
13868     }
13869   /* When asked to inline the call anyway, try to pick meaningful choice.
13870      We look for maximal size of block that is faster to copy by hand and
13871      take blocks of at most of that size guessing that average size will
13872      be roughly half of the block.  
13873
13874      If this turns out to be bad, we might simply specify the preferred
13875      choice in ix86_costs.  */
13876   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
13877       && algs->unknown_size == libcall)
13878     {
13879       int max = -1;
13880       enum stringop_alg alg;
13881       int i;
13882
13883       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
13884         if (algs->size[i].alg != libcall && algs->size[i].alg)
13885           max = algs->size[i].max;
13886       if (max == -1)
13887         max = 4096;
13888       alg = decide_alg (count, max / 2, memset, dynamic_check);
13889       gcc_assert (*dynamic_check == -1);
13890       gcc_assert (alg != libcall);
13891       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
13892         *dynamic_check = max;
13893       return alg;
13894     }
13895   return algs->unknown_size;
13896 }
13897
13898 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
13899    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
13900 static int
13901 decide_alignment (int align,
13902                   enum stringop_alg alg,
13903                   int expected_size)
13904 {
13905   int desired_align = 0;
13906   switch (alg)
13907     {
13908       case no_stringop:
13909         gcc_unreachable ();
13910       case loop:
13911       case unrolled_loop:
13912         desired_align = GET_MODE_SIZE (Pmode);
13913         break;
13914       case rep_prefix_8_byte:
13915         desired_align = 8;
13916         break;
13917       case rep_prefix_4_byte:
13918         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
13919            copying whole cacheline at once.  */
13920         if (TARGET_PENTIUMPRO)
13921           desired_align = 8;
13922         else
13923           desired_align = 4;
13924         break;
13925       case rep_prefix_1_byte:
13926         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
13927            copying whole cacheline at once.  */
13928         if (TARGET_PENTIUMPRO)
13929           desired_align = 8;
13930         else
13931           desired_align = 1;
13932         break;
13933       case loop_1_byte:
13934         desired_align = 1;
13935         break;
13936       case libcall:
13937         return 0;
13938     }
13939
13940   if (optimize_size)
13941     desired_align = 1;
13942   if (desired_align < align)
13943     desired_align = align;
13944   if (expected_size != -1 && expected_size < 4)
13945     desired_align = align;
13946   return desired_align;
13947 }
13948
13949 /* Return the smallest power of 2 greater than VAL.  */
13950 static int
13951 smallest_pow2_greater_than (int val)
13952 {
13953   int ret = 1;
13954   while (ret <= val)
13955     ret <<= 1;
13956   return ret;
13957 }
13958
13959 /* Expand string move (memcpy) operation.  Use i386 string operations when
13960    profitable.  expand_clrmem contains similar code. The code depends upon
13961    architecture, block size and alignment, but always has the same
13962    overall structure:
13963
13964    1) Prologue guard: Conditional that jumps up to epilogues for small
13965       blocks that can be handled by epilogue alone.  This is faster but
13966       also needed for correctness, since prologue assume the block is larger
13967       than the desired alignment.
13968
13969       Optional dynamic check for size and libcall for large
13970       blocks is emitted here too, with -minline-stringops-dynamically.
13971
13972    2) Prologue: copy first few bytes in order to get destination aligned
13973       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
13974       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
13975       We emit either a jump tree on power of two sized blocks, or a byte loop.
13976
13977    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
13978       with specified algorithm.
13979
13980    4) Epilogue: code copying tail of the block that is too small to be
13981       handled by main body (or up to size guarded by prologue guard).  */
13982    
13983 int
13984 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
13985                     rtx expected_align_exp, rtx expected_size_exp)
13986 {
13987   rtx destreg;
13988   rtx srcreg;
13989   rtx label = NULL;
13990   rtx tmp;
13991   rtx jump_around_label = NULL;
13992   HOST_WIDE_INT align = 1;
13993   unsigned HOST_WIDE_INT count = 0;
13994   HOST_WIDE_INT expected_size = -1;
13995   int size_needed = 0, epilogue_size_needed;
13996   int desired_align = 0;
13997   enum stringop_alg alg;
13998   int dynamic_check;
13999
14000   if (CONST_INT_P (align_exp))
14001     align = INTVAL (align_exp);
14002   /* i386 can do misaligned access on reasonably increased cost.  */
14003   if (CONST_INT_P (expected_align_exp)
14004       && INTVAL (expected_align_exp) > align)
14005     align = INTVAL (expected_align_exp);
14006   if (CONST_INT_P (count_exp))
14007     count = expected_size = INTVAL (count_exp);
14008   if (CONST_INT_P (expected_size_exp) && count == 0)
14009     expected_size = INTVAL (expected_size_exp);
14010
14011   /* Step 0: Decide on preferred algorithm, desired alignment and
14012      size of chunks to be copied by main loop.  */
14013
14014   alg = decide_alg (count, expected_size, false, &dynamic_check);
14015   desired_align = decide_alignment (align, alg, expected_size);
14016
14017   if (!TARGET_ALIGN_STRINGOPS)
14018     align = desired_align;
14019
14020   if (alg == libcall)
14021     return 0;
14022   gcc_assert (alg != no_stringop);
14023   if (!count)
14024     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
14025   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14026   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
14027   switch (alg)
14028     {
14029     case libcall:
14030     case no_stringop:
14031       gcc_unreachable ();
14032     case loop:
14033       size_needed = GET_MODE_SIZE (Pmode);
14034       break;
14035     case unrolled_loop:
14036       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
14037       break;
14038     case rep_prefix_8_byte:
14039       size_needed = 8;
14040       break;
14041     case rep_prefix_4_byte:
14042       size_needed = 4;
14043       break;
14044     case rep_prefix_1_byte:
14045     case loop_1_byte:
14046       size_needed = 1;
14047       break;
14048     }
14049
14050   epilogue_size_needed = size_needed;
14051
14052   /* Step 1: Prologue guard.  */
14053
14054   /* Alignment code needs count to be in register.  */
14055   if (CONST_INT_P (count_exp) && desired_align > align)
14056     {
14057       enum machine_mode mode = SImode;
14058       if (TARGET_64BIT && (count & ~0xffffffff))
14059         mode = DImode;
14060       count_exp = force_reg (mode, count_exp);
14061     }
14062   gcc_assert (desired_align >= 1 && align >= 1);
14063
14064   /* Ensure that alignment prologue won't copy past end of block.  */
14065   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14066     {
14067       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14068       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14069          Make sure it is power of 2.  */
14070       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14071
14072       label = gen_label_rtx ();
14073       emit_cmp_and_jump_insns (count_exp,
14074                                GEN_INT (epilogue_size_needed),
14075                                LTU, 0, counter_mode (count_exp), 1, label);
14076       if (GET_CODE (count_exp) == CONST_INT)
14077         ;
14078       else if (expected_size == -1 || expected_size < epilogue_size_needed)
14079         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14080       else
14081         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14082     }
14083   /* Emit code to decide on runtime whether library call or inline should be
14084      used.  */
14085   if (dynamic_check != -1)
14086     {
14087       rtx hot_label = gen_label_rtx ();
14088       jump_around_label = gen_label_rtx ();
14089       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14090                                LEU, 0, GET_MODE (count_exp), 1, hot_label);
14091       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14092       emit_block_move_via_libcall (dst, src, count_exp, false);
14093       emit_jump (jump_around_label);
14094       emit_label (hot_label);
14095     }
14096
14097   /* Step 2: Alignment prologue.  */
14098
14099   if (desired_align > align)
14100     {
14101       /* Except for the first move in epilogue, we no longer know
14102          constant offset in aliasing info.  It don't seems to worth
14103          the pain to maintain it for the first move, so throw away
14104          the info early.  */
14105       src = change_address (src, BLKmode, srcreg);
14106       dst = change_address (dst, BLKmode, destreg);
14107       expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
14108                               desired_align);
14109     }
14110   if (label && size_needed == 1)
14111     {
14112       emit_label (label);
14113       LABEL_NUSES (label) = 1;
14114       label = NULL;
14115     }
14116
14117   /* Step 3: Main loop.  */
14118
14119   switch (alg)
14120     {
14121     case libcall:
14122     case no_stringop:
14123       gcc_unreachable ();
14124     case loop_1_byte:
14125       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14126                                      count_exp, QImode, 1, expected_size);
14127       break;
14128     case loop:
14129       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14130                                      count_exp, Pmode, 1, expected_size);
14131       break;
14132     case unrolled_loop:
14133       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
14134          registers for 4 temporaries anyway.  */
14135       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14136                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
14137                                      expected_size);
14138       break;
14139     case rep_prefix_8_byte:
14140       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14141                                  DImode);
14142       break;
14143     case rep_prefix_4_byte:
14144       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14145                                  SImode);
14146       break;
14147     case rep_prefix_1_byte:
14148       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14149                                  QImode);
14150       break;
14151     }
14152   /* Adjust properly the offset of src and dest memory for aliasing.  */
14153   if (CONST_INT_P (count_exp))
14154     {
14155       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
14156                                           (count / size_needed) * size_needed);
14157       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14158                                           (count / size_needed) * size_needed);
14159     }
14160   else
14161     {
14162       src = change_address (src, BLKmode, srcreg);
14163       dst = change_address (dst, BLKmode, destreg);
14164     }
14165
14166   /* Step 4: Epilogue to copy the remaining bytes.  */
14167
14168   if (label)
14169     {
14170       /* When the main loop is done, COUNT_EXP might hold original count,
14171          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14172          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14173          bytes. Compensate if needed.  */
14174          
14175       if (size_needed < epilogue_size_needed)
14176         {
14177           tmp =
14178             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14179                                  GEN_INT (size_needed - 1), count_exp, 1,
14180                                  OPTAB_DIRECT);
14181           if (tmp != count_exp)
14182             emit_move_insn (count_exp, tmp);
14183         }
14184       emit_label (label);
14185       LABEL_NUSES (label) = 1;
14186     }
14187
14188   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14189     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
14190                             epilogue_size_needed);
14191   if (jump_around_label)
14192     emit_label (jump_around_label);
14193   return 1;
14194 }
14195
14196 /* Helper function for memcpy.  For QImode value 0xXY produce
14197    0xXYXYXYXY of wide specified by MODE.  This is essentially
14198    a * 0x10101010, but we can do slightly better than
14199    synth_mult by unwinding the sequence by hand on CPUs with
14200    slow multiply.  */
14201 static rtx
14202 promote_duplicated_reg (enum machine_mode mode, rtx val)
14203 {
14204   enum machine_mode valmode = GET_MODE (val);
14205   rtx tmp;
14206   int nops = mode == DImode ? 3 : 2;
14207
14208   gcc_assert (mode == SImode || mode == DImode);
14209   if (val == const0_rtx)
14210     return copy_to_mode_reg (mode, const0_rtx);
14211   if (CONST_INT_P (val))
14212     {
14213       HOST_WIDE_INT v = INTVAL (val) & 255;
14214
14215       v |= v << 8;
14216       v |= v << 16;
14217       if (mode == DImode)
14218         v |= (v << 16) << 16;
14219       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
14220     }
14221
14222   if (valmode == VOIDmode)
14223     valmode = QImode;
14224   if (valmode != QImode)
14225     val = gen_lowpart (QImode, val);
14226   if (mode == QImode)
14227     return val;
14228   if (!TARGET_PARTIAL_REG_STALL)
14229     nops--;
14230   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
14231       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
14232       <= (ix86_cost->shift_const + ix86_cost->add) * nops
14233           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
14234     {
14235       rtx reg = convert_modes (mode, QImode, val, true);
14236       tmp = promote_duplicated_reg (mode, const1_rtx);
14237       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
14238                                   OPTAB_DIRECT);
14239     }
14240   else
14241     {
14242       rtx reg = convert_modes (mode, QImode, val, true);
14243
14244       if (!TARGET_PARTIAL_REG_STALL)
14245         if (mode == SImode)
14246           emit_insn (gen_movsi_insv_1 (reg, reg));
14247         else
14248           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
14249       else
14250         {
14251           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
14252                                      NULL, 1, OPTAB_DIRECT);
14253           reg =
14254             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14255         }
14256       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
14257                                  NULL, 1, OPTAB_DIRECT);
14258       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14259       if (mode == SImode)
14260         return reg;
14261       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
14262                                  NULL, 1, OPTAB_DIRECT);
14263       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14264       return reg;
14265     }
14266 }
14267
14268 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
14269    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
14270    alignment from ALIGN to DESIRED_ALIGN.  */
14271 static rtx
14272 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
14273 {
14274   rtx promoted_val;
14275
14276   if (TARGET_64BIT
14277       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
14278     promoted_val = promote_duplicated_reg (DImode, val);
14279   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
14280     promoted_val = promote_duplicated_reg (SImode, val);
14281   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
14282     promoted_val = promote_duplicated_reg (HImode, val);
14283   else
14284     promoted_val = val;
14285
14286   return promoted_val;
14287 }
14288
14289 /* Expand string clear operation (bzero).  Use i386 string operations when
14290    profitable.  See expand_movmem comment for explanation of individual
14291    steps performed.  */
14292 int
14293 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
14294                     rtx expected_align_exp, rtx expected_size_exp)
14295 {
14296   rtx destreg;
14297   rtx label = NULL;
14298   rtx tmp;
14299   rtx jump_around_label = NULL;
14300   HOST_WIDE_INT align = 1;
14301   unsigned HOST_WIDE_INT count = 0;
14302   HOST_WIDE_INT expected_size = -1;
14303   int size_needed = 0, epilogue_size_needed;
14304   int desired_align = 0;
14305   enum stringop_alg alg;
14306   rtx promoted_val = NULL;
14307   bool force_loopy_epilogue = false;
14308   int dynamic_check;
14309
14310   if (CONST_INT_P (align_exp))
14311     align = INTVAL (align_exp);
14312   /* i386 can do misaligned access on reasonably increased cost.  */
14313   if (CONST_INT_P (expected_align_exp)
14314       && INTVAL (expected_align_exp) > align)
14315     align = INTVAL (expected_align_exp);
14316   if (CONST_INT_P (count_exp))
14317     count = expected_size = INTVAL (count_exp);
14318   if (CONST_INT_P (expected_size_exp) && count == 0)
14319     expected_size = INTVAL (expected_size_exp);
14320
14321   /* Step 0: Decide on preferred algorithm, desired alignment and
14322      size of chunks to be copied by main loop.  */
14323
14324   alg = decide_alg (count, expected_size, true, &dynamic_check);
14325   desired_align = decide_alignment (align, alg, expected_size);
14326
14327   if (!TARGET_ALIGN_STRINGOPS)
14328     align = desired_align;
14329
14330   if (alg == libcall)
14331     return 0;
14332   gcc_assert (alg != no_stringop);
14333   if (!count)
14334     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
14335   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14336   switch (alg)
14337     {
14338     case libcall:
14339     case no_stringop:
14340       gcc_unreachable ();
14341     case loop:
14342       size_needed = GET_MODE_SIZE (Pmode);
14343       break;
14344     case unrolled_loop:
14345       size_needed = GET_MODE_SIZE (Pmode) * 4;
14346       break;
14347     case rep_prefix_8_byte:
14348       size_needed = 8;
14349       break;
14350     case rep_prefix_4_byte:
14351       size_needed = 4;
14352       break;
14353     case rep_prefix_1_byte:
14354     case loop_1_byte:
14355       size_needed = 1;
14356       break;
14357     }
14358   epilogue_size_needed = size_needed;
14359
14360   /* Step 1: Prologue guard.  */
14361
14362   /* Alignment code needs count to be in register.  */
14363   if (CONST_INT_P (count_exp) && desired_align > align)
14364     {
14365       enum machine_mode mode = SImode;
14366       if (TARGET_64BIT && (count & ~0xffffffff))
14367         mode = DImode;
14368       count_exp = force_reg (mode, count_exp);
14369     }
14370   /* Do the cheap promotion to allow better CSE across the 
14371      main loop and epilogue (ie one load of the big constant in the
14372      front of all code.  */
14373   if (CONST_INT_P (val_exp))
14374     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14375                                                    desired_align, align);
14376   /* Ensure that alignment prologue won't copy past end of block.  */
14377   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14378     {
14379       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14380       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14381          Make sure it is power of 2.  */
14382       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14383
14384       /* To improve performance of small blocks, we jump around the VAL
14385          promoting mode.  This mean that if the promoted VAL is not constant,
14386          we might not use it in the epilogue and have to use byte
14387          loop variant.  */
14388       if (epilogue_size_needed > 2 && !promoted_val)
14389         force_loopy_epilogue = true;
14390       label = gen_label_rtx ();
14391       emit_cmp_and_jump_insns (count_exp,
14392                                GEN_INT (epilogue_size_needed),
14393                                LTU, 0, counter_mode (count_exp), 1, label);
14394       if (GET_CODE (count_exp) == CONST_INT)
14395         ;
14396       else if (expected_size == -1 || expected_size <= epilogue_size_needed)
14397         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14398       else
14399         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14400     }
14401   if (dynamic_check != -1)
14402     {
14403       rtx hot_label = gen_label_rtx ();
14404       jump_around_label = gen_label_rtx ();
14405       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14406                                LEU, 0, counter_mode (count_exp), 1, hot_label);
14407       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14408       set_storage_via_libcall (dst, count_exp, val_exp, false);
14409       emit_jump (jump_around_label);
14410       emit_label (hot_label);
14411     }
14412
14413   /* Step 2: Alignment prologue.  */
14414
14415   /* Do the expensive promotion once we branched off the small blocks.  */
14416   if (!promoted_val)
14417     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14418                                                    desired_align, align);
14419   gcc_assert (desired_align >= 1 && align >= 1);
14420
14421   if (desired_align > align)
14422     {
14423       /* Except for the first move in epilogue, we no longer know
14424          constant offset in aliasing info.  It don't seems to worth
14425          the pain to maintain it for the first move, so throw away
14426          the info early.  */
14427       dst = change_address (dst, BLKmode, destreg);
14428       expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
14429                               desired_align);
14430     }
14431   if (label && size_needed == 1)
14432     {
14433       emit_label (label);
14434       LABEL_NUSES (label) = 1;
14435       label = NULL;
14436     }
14437
14438   /* Step 3: Main loop.  */
14439
14440   switch (alg)
14441     {
14442     case libcall:
14443     case no_stringop:
14444       gcc_unreachable ();
14445     case loop_1_byte:
14446       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14447                                      count_exp, QImode, 1, expected_size);
14448       break;
14449     case loop:
14450       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14451                                      count_exp, Pmode, 1, expected_size);
14452       break;
14453     case unrolled_loop:
14454       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14455                                      count_exp, Pmode, 4, expected_size);
14456       break;
14457     case rep_prefix_8_byte:
14458       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14459                                   DImode);
14460       break;
14461     case rep_prefix_4_byte:
14462       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14463                                   SImode);
14464       break;
14465     case rep_prefix_1_byte:
14466       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14467                                   QImode);
14468       break;
14469     }
14470   /* Adjust properly the offset of src and dest memory for aliasing.  */
14471   if (CONST_INT_P (count_exp))
14472     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14473                                         (count / size_needed) * size_needed);
14474   else
14475     dst = change_address (dst, BLKmode, destreg);
14476
14477   /* Step 4: Epilogue to copy the remaining bytes.  */
14478
14479   if (label)
14480     {
14481       /* When the main loop is done, COUNT_EXP might hold original count,
14482          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14483          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14484          bytes. Compensate if needed.  */
14485
14486       if (size_needed < desired_align - align)
14487         {
14488           tmp =
14489             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14490                                  GEN_INT (size_needed - 1), count_exp, 1,
14491                                  OPTAB_DIRECT);
14492           size_needed = desired_align - align + 1;
14493           if (tmp != count_exp)
14494             emit_move_insn (count_exp, tmp);
14495         }
14496       emit_label (label);
14497       LABEL_NUSES (label) = 1;
14498     }
14499   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14500     {
14501       if (force_loopy_epilogue)
14502         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
14503                                          size_needed);
14504       else
14505         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
14506                                 size_needed);
14507     }
14508   if (jump_around_label)
14509     emit_label (jump_around_label);
14510   return 1;
14511 }
14512
14513 /* Expand the appropriate insns for doing strlen if not just doing
14514    repnz; scasb
14515
14516    out = result, initialized with the start address
14517    align_rtx = alignment of the address.
14518    scratch = scratch register, initialized with the startaddress when
14519         not aligned, otherwise undefined
14520
14521    This is just the body. It needs the initializations mentioned above and
14522    some address computing at the end.  These things are done in i386.md.  */
14523
14524 static void
14525 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
14526 {
14527   int align;
14528   rtx tmp;
14529   rtx align_2_label = NULL_RTX;
14530   rtx align_3_label = NULL_RTX;
14531   rtx align_4_label = gen_label_rtx ();
14532   rtx end_0_label = gen_label_rtx ();
14533   rtx mem;
14534   rtx tmpreg = gen_reg_rtx (SImode);
14535   rtx scratch = gen_reg_rtx (SImode);
14536   rtx cmp;
14537
14538   align = 0;
14539   if (CONST_INT_P (align_rtx))
14540     align = INTVAL (align_rtx);
14541
14542   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
14543
14544   /* Is there a known alignment and is it less than 4?  */
14545   if (align < 4)
14546     {
14547       rtx scratch1 = gen_reg_rtx (Pmode);
14548       emit_move_insn (scratch1, out);
14549       /* Is there a known alignment and is it not 2? */
14550       if (align != 2)
14551         {
14552           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
14553           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
14554
14555           /* Leave just the 3 lower bits.  */
14556           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
14557                                     NULL_RTX, 0, OPTAB_WIDEN);
14558
14559           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14560                                    Pmode, 1, align_4_label);
14561           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
14562                                    Pmode, 1, align_2_label);
14563           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
14564                                    Pmode, 1, align_3_label);
14565         }
14566       else
14567         {
14568           /* Since the alignment is 2, we have to check 2 or 0 bytes;
14569              check if is aligned to 4 - byte.  */
14570
14571           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
14572                                     NULL_RTX, 0, OPTAB_WIDEN);
14573
14574           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14575                                    Pmode, 1, align_4_label);
14576         }
14577
14578       mem = change_address (src, QImode, out);
14579
14580       /* Now compare the bytes.  */
14581
14582       /* Compare the first n unaligned byte on a byte per byte basis.  */
14583       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
14584                                QImode, 1, end_0_label);
14585
14586       /* Increment the address.  */
14587       if (TARGET_64BIT)
14588         emit_insn (gen_adddi3 (out, out, const1_rtx));
14589       else
14590         emit_insn (gen_addsi3 (out, out, const1_rtx));
14591
14592       /* Not needed with an alignment of 2 */
14593       if (align != 2)
14594         {
14595           emit_label (align_2_label);
14596
14597           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14598                                    end_0_label);
14599
14600           if (TARGET_64BIT)
14601             emit_insn (gen_adddi3 (out, out, const1_rtx));
14602           else
14603             emit_insn (gen_addsi3 (out, out, const1_rtx));
14604
14605           emit_label (align_3_label);
14606         }
14607
14608       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14609                                end_0_label);
14610
14611       if (TARGET_64BIT)
14612         emit_insn (gen_adddi3 (out, out, const1_rtx));
14613       else
14614         emit_insn (gen_addsi3 (out, out, const1_rtx));
14615     }
14616
14617   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
14618      align this loop.  It gives only huge programs, but does not help to
14619      speed up.  */
14620   emit_label (align_4_label);
14621
14622   mem = change_address (src, SImode, out);
14623   emit_move_insn (scratch, mem);
14624   if (TARGET_64BIT)
14625     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
14626   else
14627     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
14628
14629   /* This formula yields a nonzero result iff one of the bytes is zero.
14630      This saves three branches inside loop and many cycles.  */
14631
14632   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
14633   emit_insn (gen_one_cmplsi2 (scratch, scratch));
14634   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
14635   emit_insn (gen_andsi3 (tmpreg, tmpreg,
14636                          gen_int_mode (0x80808080, SImode)));
14637   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
14638                            align_4_label);
14639
14640   if (TARGET_CMOVE)
14641     {
14642        rtx reg = gen_reg_rtx (SImode);
14643        rtx reg2 = gen_reg_rtx (Pmode);
14644        emit_move_insn (reg, tmpreg);
14645        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
14646
14647        /* If zero is not in the first two bytes, move two bytes forward.  */
14648        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14649        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14650        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14651        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
14652                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
14653                                                      reg,
14654                                                      tmpreg)));
14655        /* Emit lea manually to avoid clobbering of flags.  */
14656        emit_insn (gen_rtx_SET (SImode, reg2,
14657                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
14658
14659        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14660        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14661        emit_insn (gen_rtx_SET (VOIDmode, out,
14662                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
14663                                                      reg2,
14664                                                      out)));
14665
14666     }
14667   else
14668     {
14669        rtx end_2_label = gen_label_rtx ();
14670        /* Is zero in the first two bytes? */
14671
14672        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14673        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14674        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
14675        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
14676                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
14677                             pc_rtx);
14678        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
14679        JUMP_LABEL (tmp) = end_2_label;
14680
14681        /* Not in the first two.  Move two bytes forward.  */
14682        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
14683        if (TARGET_64BIT)
14684          emit_insn (gen_adddi3 (out, out, const2_rtx));
14685        else
14686          emit_insn (gen_addsi3 (out, out, const2_rtx));
14687
14688        emit_label (end_2_label);
14689
14690     }
14691
14692   /* Avoid branch in fixing the byte.  */
14693   tmpreg = gen_lowpart (QImode, tmpreg);
14694   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
14695   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
14696   if (TARGET_64BIT)
14697     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
14698   else
14699     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
14700
14701   emit_label (end_0_label);
14702 }
14703
14704 /* Expand strlen.  */
14705
14706 int
14707 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
14708 {
14709   rtx addr, scratch1, scratch2, scratch3, scratch4;
14710
14711   /* The generic case of strlen expander is long.  Avoid it's
14712      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
14713
14714   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
14715       && !TARGET_INLINE_ALL_STRINGOPS
14716       && !optimize_size
14717       && (!CONST_INT_P (align) || INTVAL (align) < 4))
14718     return 0;
14719
14720   addr = force_reg (Pmode, XEXP (src, 0));
14721   scratch1 = gen_reg_rtx (Pmode);
14722
14723   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
14724       && !optimize_size)
14725     {
14726       /* Well it seems that some optimizer does not combine a call like
14727          foo(strlen(bar), strlen(bar));
14728          when the move and the subtraction is done here.  It does calculate
14729          the length just once when these instructions are done inside of
14730          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
14731          often used and I use one fewer register for the lifetime of
14732          output_strlen_unroll() this is better.  */
14733
14734       emit_move_insn (out, addr);
14735
14736       ix86_expand_strlensi_unroll_1 (out, src, align);
14737
14738       /* strlensi_unroll_1 returns the address of the zero at the end of
14739          the string, like memchr(), so compute the length by subtracting
14740          the start address.  */
14741       if (TARGET_64BIT)
14742         emit_insn (gen_subdi3 (out, out, addr));
14743       else
14744         emit_insn (gen_subsi3 (out, out, addr));
14745     }
14746   else
14747     {
14748       rtx unspec;
14749       scratch2 = gen_reg_rtx (Pmode);
14750       scratch3 = gen_reg_rtx (Pmode);
14751       scratch4 = force_reg (Pmode, constm1_rtx);
14752
14753       emit_move_insn (scratch3, addr);
14754       eoschar = force_reg (QImode, eoschar);
14755
14756       src = replace_equiv_address_nv (src, scratch3);
14757
14758       /* If .md starts supporting :P, this can be done in .md.  */
14759       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
14760                                                  scratch4), UNSPEC_SCAS);
14761       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
14762       if (TARGET_64BIT)
14763         {
14764           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
14765           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
14766         }
14767       else
14768         {
14769           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
14770           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
14771         }
14772     }
14773   return 1;
14774 }
14775
14776 /* For given symbol (function) construct code to compute address of it's PLT
14777    entry in large x86-64 PIC model.  */
14778 rtx
14779 construct_plt_address (rtx symbol)
14780 {
14781   rtx tmp = gen_reg_rtx (Pmode);
14782   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
14783
14784   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
14785   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
14786
14787   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
14788   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
14789   return tmp;
14790 }
14791
14792 void
14793 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
14794                   rtx callarg2 ATTRIBUTE_UNUSED,
14795                   rtx pop, int sibcall)
14796 {
14797   rtx use = NULL, call;
14798
14799   if (pop == const0_rtx)
14800     pop = NULL;
14801   gcc_assert (!TARGET_64BIT || !pop);
14802
14803   if (TARGET_MACHO && !TARGET_64BIT)
14804     {
14805 #if TARGET_MACHO
14806       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
14807         fnaddr = machopic_indirect_call_target (fnaddr);
14808 #endif
14809     }
14810   else
14811     {
14812       /* Static functions and indirect calls don't need the pic register.  */
14813       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
14814           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
14815           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
14816         use_reg (&use, pic_offset_table_rtx);
14817     }
14818
14819   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
14820     {
14821       rtx al = gen_rtx_REG (QImode, 0);
14822       emit_move_insn (al, callarg2);
14823       use_reg (&use, al);
14824     }
14825
14826   if (ix86_cmodel == CM_LARGE_PIC
14827       && GET_CODE (fnaddr) == MEM
14828       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
14829       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
14830     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
14831   else if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
14832     {
14833       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
14834       fnaddr = gen_rtx_MEM (QImode, fnaddr);
14835     }
14836   if (sibcall && TARGET_64BIT
14837       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
14838     {
14839       rtx addr;
14840       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
14841       fnaddr = gen_rtx_REG (Pmode, R11_REG);
14842       emit_move_insn (fnaddr, addr);
14843       fnaddr = gen_rtx_MEM (QImode, fnaddr);
14844     }
14845
14846   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
14847   if (retval)
14848     call = gen_rtx_SET (VOIDmode, retval, call);
14849   if (pop)
14850     {
14851       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
14852       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
14853       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
14854     }
14855
14856   call = emit_call_insn (call);
14857   if (use)
14858     CALL_INSN_FUNCTION_USAGE (call) = use;
14859 }
14860
14861 \f
14862 /* Clear stack slot assignments remembered from previous functions.
14863    This is called from INIT_EXPANDERS once before RTL is emitted for each
14864    function.  */
14865
14866 static struct machine_function *
14867 ix86_init_machine_status (void)
14868 {
14869   struct machine_function *f;
14870
14871   f = ggc_alloc_cleared (sizeof (struct machine_function));
14872   f->use_fast_prologue_epilogue_nregs = -1;
14873   f->tls_descriptor_call_expanded_p = 0;
14874
14875   return f;
14876 }
14877
14878 /* Return a MEM corresponding to a stack slot with mode MODE.
14879    Allocate a new slot if necessary.
14880
14881    The RTL for a function can have several slots available: N is
14882    which slot to use.  */
14883
14884 rtx
14885 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
14886 {
14887   struct stack_local_entry *s;
14888
14889   gcc_assert (n < MAX_386_STACK_LOCALS);
14890
14891   for (s = ix86_stack_locals; s; s = s->next)
14892     if (s->mode == mode && s->n == n)
14893       return copy_rtx (s->rtl);
14894
14895   s = (struct stack_local_entry *)
14896     ggc_alloc (sizeof (struct stack_local_entry));
14897   s->n = n;
14898   s->mode = mode;
14899   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
14900
14901   s->next = ix86_stack_locals;
14902   ix86_stack_locals = s;
14903   return s->rtl;
14904 }
14905
14906 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
14907
14908 static GTY(()) rtx ix86_tls_symbol;
14909 rtx
14910 ix86_tls_get_addr (void)
14911 {
14912
14913   if (!ix86_tls_symbol)
14914     {
14915       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
14916                                             (TARGET_ANY_GNU_TLS
14917                                              && !TARGET_64BIT)
14918                                             ? "___tls_get_addr"
14919                                             : "__tls_get_addr");
14920     }
14921
14922   return ix86_tls_symbol;
14923 }
14924
14925 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
14926
14927 static GTY(()) rtx ix86_tls_module_base_symbol;
14928 rtx
14929 ix86_tls_module_base (void)
14930 {
14931
14932   if (!ix86_tls_module_base_symbol)
14933     {
14934       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
14935                                                         "_TLS_MODULE_BASE_");
14936       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
14937         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
14938     }
14939
14940   return ix86_tls_module_base_symbol;
14941 }
14942 \f
14943 /* Calculate the length of the memory address in the instruction
14944    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
14945
14946 int
14947 memory_address_length (rtx addr)
14948 {
14949   struct ix86_address parts;
14950   rtx base, index, disp;
14951   int len;
14952   int ok;
14953
14954   if (GET_CODE (addr) == PRE_DEC
14955       || GET_CODE (addr) == POST_INC
14956       || GET_CODE (addr) == PRE_MODIFY
14957       || GET_CODE (addr) == POST_MODIFY)
14958     return 0;
14959
14960   ok = ix86_decompose_address (addr, &parts);
14961   gcc_assert (ok);
14962
14963   if (parts.base && GET_CODE (parts.base) == SUBREG)
14964     parts.base = SUBREG_REG (parts.base);
14965   if (parts.index && GET_CODE (parts.index) == SUBREG)
14966     parts.index = SUBREG_REG (parts.index);
14967
14968   base = parts.base;
14969   index = parts.index;
14970   disp = parts.disp;
14971   len = 0;
14972
14973   /* Rule of thumb:
14974        - esp as the base always wants an index,
14975        - ebp as the base always wants a displacement.  */
14976
14977   /* Register Indirect.  */
14978   if (base && !index && !disp)
14979     {
14980       /* esp (for its index) and ebp (for its displacement) need
14981          the two-byte modrm form.  */
14982       if (addr == stack_pointer_rtx
14983           || addr == arg_pointer_rtx
14984           || addr == frame_pointer_rtx
14985           || addr == hard_frame_pointer_rtx)
14986         len = 1;
14987     }
14988
14989   /* Direct Addressing.  */
14990   else if (disp && !base && !index)
14991     len = 4;
14992
14993   else
14994     {
14995       /* Find the length of the displacement constant.  */
14996       if (disp)
14997         {
14998           if (base && satisfies_constraint_K (disp))
14999             len = 1;
15000           else
15001             len = 4;
15002         }
15003       /* ebp always wants a displacement.  */
15004       else if (base == hard_frame_pointer_rtx)
15005         len = 1;
15006
15007       /* An index requires the two-byte modrm form....  */
15008       if (index
15009           /* ...like esp, which always wants an index.  */
15010           || base == stack_pointer_rtx
15011           || base == arg_pointer_rtx
15012           || base == frame_pointer_rtx)
15013         len += 1;
15014     }
15015
15016   return len;
15017 }
15018
15019 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
15020    is set, expect that insn have 8bit immediate alternative.  */
15021 int
15022 ix86_attr_length_immediate_default (rtx insn, int shortform)
15023 {
15024   int len = 0;
15025   int i;
15026   extract_insn_cached (insn);
15027   for (i = recog_data.n_operands - 1; i >= 0; --i)
15028     if (CONSTANT_P (recog_data.operand[i]))
15029       {
15030         gcc_assert (!len);
15031         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
15032           len = 1;
15033         else
15034           {
15035             switch (get_attr_mode (insn))
15036               {
15037                 case MODE_QI:
15038                   len+=1;
15039                   break;
15040                 case MODE_HI:
15041                   len+=2;
15042                   break;
15043                 case MODE_SI:
15044                   len+=4;
15045                   break;
15046                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
15047                 case MODE_DI:
15048                   len+=4;
15049                   break;
15050                 default:
15051                   fatal_insn ("unknown insn mode", insn);
15052               }
15053           }
15054       }
15055   return len;
15056 }
15057 /* Compute default value for "length_address" attribute.  */
15058 int
15059 ix86_attr_length_address_default (rtx insn)
15060 {
15061   int i;
15062
15063   if (get_attr_type (insn) == TYPE_LEA)
15064     {
15065       rtx set = PATTERN (insn);
15066
15067       if (GET_CODE (set) == PARALLEL)
15068         set = XVECEXP (set, 0, 0);
15069
15070       gcc_assert (GET_CODE (set) == SET);
15071
15072       return memory_address_length (SET_SRC (set));
15073     }
15074
15075   extract_insn_cached (insn);
15076   for (i = recog_data.n_operands - 1; i >= 0; --i)
15077     if (MEM_P (recog_data.operand[i]))
15078       {
15079         return memory_address_length (XEXP (recog_data.operand[i], 0));
15080         break;
15081       }
15082   return 0;
15083 }
15084 \f
15085 /* Return the maximum number of instructions a cpu can issue.  */
15086
15087 static int
15088 ix86_issue_rate (void)
15089 {
15090   switch (ix86_tune)
15091     {
15092     case PROCESSOR_PENTIUM:
15093     case PROCESSOR_K6:
15094       return 2;
15095
15096     case PROCESSOR_PENTIUMPRO:
15097     case PROCESSOR_PENTIUM4:
15098     case PROCESSOR_ATHLON:
15099     case PROCESSOR_K8:
15100     case PROCESSOR_AMDFAM10:
15101     case PROCESSOR_NOCONA:
15102     case PROCESSOR_GENERIC32:
15103     case PROCESSOR_GENERIC64:
15104       return 3;
15105
15106     case PROCESSOR_CORE2:
15107       return 4;
15108
15109     default:
15110       return 1;
15111     }
15112 }
15113
15114 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
15115    by DEP_INSN and nothing set by DEP_INSN.  */
15116
15117 static int
15118 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15119 {
15120   rtx set, set2;
15121
15122   /* Simplify the test for uninteresting insns.  */
15123   if (insn_type != TYPE_SETCC
15124       && insn_type != TYPE_ICMOV
15125       && insn_type != TYPE_FCMOV
15126       && insn_type != TYPE_IBR)
15127     return 0;
15128
15129   if ((set = single_set (dep_insn)) != 0)
15130     {
15131       set = SET_DEST (set);
15132       set2 = NULL_RTX;
15133     }
15134   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
15135            && XVECLEN (PATTERN (dep_insn), 0) == 2
15136            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
15137            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
15138     {
15139       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15140       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15141     }
15142   else
15143     return 0;
15144
15145   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
15146     return 0;
15147
15148   /* This test is true if the dependent insn reads the flags but
15149      not any other potentially set register.  */
15150   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
15151     return 0;
15152
15153   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
15154     return 0;
15155
15156   return 1;
15157 }
15158
15159 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
15160    address with operands set by DEP_INSN.  */
15161
15162 static int
15163 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15164 {
15165   rtx addr;
15166
15167   if (insn_type == TYPE_LEA
15168       && TARGET_PENTIUM)
15169     {
15170       addr = PATTERN (insn);
15171
15172       if (GET_CODE (addr) == PARALLEL)
15173         addr = XVECEXP (addr, 0, 0);
15174
15175       gcc_assert (GET_CODE (addr) == SET);
15176
15177       addr = SET_SRC (addr);
15178     }
15179   else
15180     {
15181       int i;
15182       extract_insn_cached (insn);
15183       for (i = recog_data.n_operands - 1; i >= 0; --i)
15184         if (MEM_P (recog_data.operand[i]))
15185           {
15186             addr = XEXP (recog_data.operand[i], 0);
15187             goto found;
15188           }
15189       return 0;
15190     found:;
15191     }
15192
15193   return modified_in_p (addr, dep_insn);
15194 }
15195
15196 static int
15197 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
15198 {
15199   enum attr_type insn_type, dep_insn_type;
15200   enum attr_memory memory;
15201   rtx set, set2;
15202   int dep_insn_code_number;
15203
15204   /* Anti and output dependencies have zero cost on all CPUs.  */
15205   if (REG_NOTE_KIND (link) != 0)
15206     return 0;
15207
15208   dep_insn_code_number = recog_memoized (dep_insn);
15209
15210   /* If we can't recognize the insns, we can't really do anything.  */
15211   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
15212     return cost;
15213
15214   insn_type = get_attr_type (insn);
15215   dep_insn_type = get_attr_type (dep_insn);
15216
15217   switch (ix86_tune)
15218     {
15219     case PROCESSOR_PENTIUM:
15220       /* Address Generation Interlock adds a cycle of latency.  */
15221       if (ix86_agi_dependent (insn, dep_insn, insn_type))
15222         cost += 1;
15223
15224       /* ??? Compares pair with jump/setcc.  */
15225       if (ix86_flags_dependent (insn, dep_insn, insn_type))
15226         cost = 0;
15227
15228       /* Floating point stores require value to be ready one cycle earlier.  */
15229       if (insn_type == TYPE_FMOV
15230           && get_attr_memory (insn) == MEMORY_STORE
15231           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15232         cost += 1;
15233       break;
15234
15235     case PROCESSOR_PENTIUMPRO:
15236       memory = get_attr_memory (insn);
15237
15238       /* INT->FP conversion is expensive.  */
15239       if (get_attr_fp_int_src (dep_insn))
15240         cost += 5;
15241
15242       /* There is one cycle extra latency between an FP op and a store.  */
15243       if (insn_type == TYPE_FMOV
15244           && (set = single_set (dep_insn)) != NULL_RTX
15245           && (set2 = single_set (insn)) != NULL_RTX
15246           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
15247           && MEM_P (SET_DEST (set2)))
15248         cost += 1;
15249
15250       /* Show ability of reorder buffer to hide latency of load by executing
15251          in parallel with previous instruction in case
15252          previous instruction is not needed to compute the address.  */
15253       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15254           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15255         {
15256           /* Claim moves to take one cycle, as core can issue one load
15257              at time and the next load can start cycle later.  */
15258           if (dep_insn_type == TYPE_IMOV
15259               || dep_insn_type == TYPE_FMOV)
15260             cost = 1;
15261           else if (cost > 1)
15262             cost--;
15263         }
15264       break;
15265
15266     case PROCESSOR_K6:
15267       memory = get_attr_memory (insn);
15268
15269       /* The esp dependency is resolved before the instruction is really
15270          finished.  */
15271       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
15272           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
15273         return 1;
15274
15275       /* INT->FP conversion is expensive.  */
15276       if (get_attr_fp_int_src (dep_insn))
15277         cost += 5;
15278
15279       /* Show ability of reorder buffer to hide latency of load by executing
15280          in parallel with previous instruction in case
15281          previous instruction is not needed to compute the address.  */
15282       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15283           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15284         {
15285           /* Claim moves to take one cycle, as core can issue one load
15286              at time and the next load can start cycle later.  */
15287           if (dep_insn_type == TYPE_IMOV
15288               || dep_insn_type == TYPE_FMOV)
15289             cost = 1;
15290           else if (cost > 2)
15291             cost -= 2;
15292           else
15293             cost = 1;
15294         }
15295       break;
15296
15297     case PROCESSOR_ATHLON:
15298     case PROCESSOR_K8:
15299     case PROCESSOR_AMDFAM10:
15300     case PROCESSOR_GENERIC32:
15301     case PROCESSOR_GENERIC64:
15302       memory = get_attr_memory (insn);
15303
15304       /* Show ability of reorder buffer to hide latency of load by executing
15305          in parallel with previous instruction in case
15306          previous instruction is not needed to compute the address.  */
15307       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15308           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15309         {
15310           enum attr_unit unit = get_attr_unit (insn);
15311           int loadcost = 3;
15312
15313           /* Because of the difference between the length of integer and
15314              floating unit pipeline preparation stages, the memory operands
15315              for floating point are cheaper.
15316
15317              ??? For Athlon it the difference is most probably 2.  */
15318           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
15319             loadcost = 3;
15320           else
15321             loadcost = TARGET_ATHLON ? 2 : 0;
15322
15323           if (cost >= loadcost)
15324             cost -= loadcost;
15325           else
15326             cost = 0;
15327         }
15328
15329     default:
15330       break;
15331     }
15332
15333   return cost;
15334 }
15335
15336 /* How many alternative schedules to try.  This should be as wide as the
15337    scheduling freedom in the DFA, but no wider.  Making this value too
15338    large results extra work for the scheduler.  */
15339
15340 static int
15341 ia32_multipass_dfa_lookahead (void)
15342 {
15343   if (ix86_tune == PROCESSOR_PENTIUM)
15344     return 2;
15345
15346   if (ix86_tune == PROCESSOR_PENTIUMPRO
15347       || ix86_tune == PROCESSOR_K6)
15348     return 1;
15349
15350   else
15351     return 0;
15352 }
15353
15354 \f
15355 /* Compute the alignment given to a constant that is being placed in memory.
15356    EXP is the constant and ALIGN is the alignment that the object would
15357    ordinarily have.
15358    The value of this function is used instead of that alignment to align
15359    the object.  */
15360
15361 int
15362 ix86_constant_alignment (tree exp, int align)
15363 {
15364   if (TREE_CODE (exp) == REAL_CST)
15365     {
15366       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
15367         return 64;
15368       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
15369         return 128;
15370     }
15371   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
15372            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
15373     return BITS_PER_WORD;
15374
15375   return align;
15376 }
15377
15378 /* Compute the alignment for a static variable.
15379    TYPE is the data type, and ALIGN is the alignment that
15380    the object would ordinarily have.  The value of this function is used
15381    instead of that alignment to align the object.  */
15382
15383 int
15384 ix86_data_alignment (tree type, int align)
15385 {
15386   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
15387
15388   if (AGGREGATE_TYPE_P (type)
15389       && TYPE_SIZE (type)
15390       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15391       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
15392           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
15393       && align < max_align)
15394     align = max_align;
15395
15396   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15397      to 16byte boundary.  */
15398   if (TARGET_64BIT)
15399     {
15400       if (AGGREGATE_TYPE_P (type)
15401            && TYPE_SIZE (type)
15402            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15403            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
15404                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15405         return 128;
15406     }
15407
15408   if (TREE_CODE (type) == ARRAY_TYPE)
15409     {
15410       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15411         return 64;
15412       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15413         return 128;
15414     }
15415   else if (TREE_CODE (type) == COMPLEX_TYPE)
15416     {
15417
15418       if (TYPE_MODE (type) == DCmode && align < 64)
15419         return 64;
15420       if (TYPE_MODE (type) == XCmode && align < 128)
15421         return 128;
15422     }
15423   else if ((TREE_CODE (type) == RECORD_TYPE
15424             || TREE_CODE (type) == UNION_TYPE
15425             || TREE_CODE (type) == QUAL_UNION_TYPE)
15426            && TYPE_FIELDS (type))
15427     {
15428       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15429         return 64;
15430       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15431         return 128;
15432     }
15433   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15434            || TREE_CODE (type) == INTEGER_TYPE)
15435     {
15436       if (TYPE_MODE (type) == DFmode && align < 64)
15437         return 64;
15438       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15439         return 128;
15440     }
15441
15442   return align;
15443 }
15444
15445 /* Compute the alignment for a local variable.
15446    TYPE is the data type, and ALIGN is the alignment that
15447    the object would ordinarily have.  The value of this macro is used
15448    instead of that alignment to align the object.  */
15449
15450 int
15451 ix86_local_alignment (tree type, int align)
15452 {
15453   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15454      to 16byte boundary.  */
15455   if (TARGET_64BIT)
15456     {
15457       if (AGGREGATE_TYPE_P (type)
15458            && TYPE_SIZE (type)
15459            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15460            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
15461                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15462         return 128;
15463     }
15464   if (TREE_CODE (type) == ARRAY_TYPE)
15465     {
15466       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15467         return 64;
15468       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15469         return 128;
15470     }
15471   else if (TREE_CODE (type) == COMPLEX_TYPE)
15472     {
15473       if (TYPE_MODE (type) == DCmode && align < 64)
15474         return 64;
15475       if (TYPE_MODE (type) == XCmode && align < 128)
15476         return 128;
15477     }
15478   else if ((TREE_CODE (type) == RECORD_TYPE
15479             || TREE_CODE (type) == UNION_TYPE
15480             || TREE_CODE (type) == QUAL_UNION_TYPE)
15481            && TYPE_FIELDS (type))
15482     {
15483       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15484         return 64;
15485       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15486         return 128;
15487     }
15488   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15489            || TREE_CODE (type) == INTEGER_TYPE)
15490     {
15491
15492       if (TYPE_MODE (type) == DFmode && align < 64)
15493         return 64;
15494       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15495         return 128;
15496     }
15497   return align;
15498 }
15499 \f
15500 /* Emit RTL insns to initialize the variable parts of a trampoline.
15501    FNADDR is an RTX for the address of the function's pure code.
15502    CXT is an RTX for the static chain value for the function.  */
15503 void
15504 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
15505 {
15506   if (!TARGET_64BIT)
15507     {
15508       /* Compute offset from the end of the jmp to the target function.  */
15509       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
15510                                plus_constant (tramp, 10),
15511                                NULL_RTX, 1, OPTAB_DIRECT);
15512       emit_move_insn (gen_rtx_MEM (QImode, tramp),
15513                       gen_int_mode (0xb9, QImode));
15514       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
15515       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
15516                       gen_int_mode (0xe9, QImode));
15517       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
15518     }
15519   else
15520     {
15521       int offset = 0;
15522       /* Try to load address using shorter movl instead of movabs.
15523          We may want to support movq for kernel mode, but kernel does not use
15524          trampolines at the moment.  */
15525       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
15526         {
15527           fnaddr = copy_to_mode_reg (DImode, fnaddr);
15528           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15529                           gen_int_mode (0xbb41, HImode));
15530           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
15531                           gen_lowpart (SImode, fnaddr));
15532           offset += 6;
15533         }
15534       else
15535         {
15536           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15537                           gen_int_mode (0xbb49, HImode));
15538           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15539                           fnaddr);
15540           offset += 10;
15541         }
15542       /* Load static chain using movabs to r10.  */
15543       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15544                       gen_int_mode (0xba49, HImode));
15545       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15546                       cxt);
15547       offset += 10;
15548       /* Jump to the r11 */
15549       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15550                       gen_int_mode (0xff49, HImode));
15551       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
15552                       gen_int_mode (0xe3, QImode));
15553       offset += 3;
15554       gcc_assert (offset <= TRAMPOLINE_SIZE);
15555     }
15556
15557 #ifdef ENABLE_EXECUTE_STACK
15558   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
15559                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
15560 #endif
15561 }
15562 \f
15563 /* Codes for all the SSE/MMX builtins.  */
15564 enum ix86_builtins
15565 {
15566   IX86_BUILTIN_ADDPS,
15567   IX86_BUILTIN_ADDSS,
15568   IX86_BUILTIN_DIVPS,
15569   IX86_BUILTIN_DIVSS,
15570   IX86_BUILTIN_MULPS,
15571   IX86_BUILTIN_MULSS,
15572   IX86_BUILTIN_SUBPS,
15573   IX86_BUILTIN_SUBSS,
15574
15575   IX86_BUILTIN_CMPEQPS,
15576   IX86_BUILTIN_CMPLTPS,
15577   IX86_BUILTIN_CMPLEPS,
15578   IX86_BUILTIN_CMPGTPS,
15579   IX86_BUILTIN_CMPGEPS,
15580   IX86_BUILTIN_CMPNEQPS,
15581   IX86_BUILTIN_CMPNLTPS,
15582   IX86_BUILTIN_CMPNLEPS,
15583   IX86_BUILTIN_CMPNGTPS,
15584   IX86_BUILTIN_CMPNGEPS,
15585   IX86_BUILTIN_CMPORDPS,
15586   IX86_BUILTIN_CMPUNORDPS,
15587   IX86_BUILTIN_CMPEQSS,
15588   IX86_BUILTIN_CMPLTSS,
15589   IX86_BUILTIN_CMPLESS,
15590   IX86_BUILTIN_CMPNEQSS,
15591   IX86_BUILTIN_CMPNLTSS,
15592   IX86_BUILTIN_CMPNLESS,
15593   IX86_BUILTIN_CMPNGTSS,
15594   IX86_BUILTIN_CMPNGESS,
15595   IX86_BUILTIN_CMPORDSS,
15596   IX86_BUILTIN_CMPUNORDSS,
15597
15598   IX86_BUILTIN_COMIEQSS,
15599   IX86_BUILTIN_COMILTSS,
15600   IX86_BUILTIN_COMILESS,
15601   IX86_BUILTIN_COMIGTSS,
15602   IX86_BUILTIN_COMIGESS,
15603   IX86_BUILTIN_COMINEQSS,
15604   IX86_BUILTIN_UCOMIEQSS,
15605   IX86_BUILTIN_UCOMILTSS,
15606   IX86_BUILTIN_UCOMILESS,
15607   IX86_BUILTIN_UCOMIGTSS,
15608   IX86_BUILTIN_UCOMIGESS,
15609   IX86_BUILTIN_UCOMINEQSS,
15610
15611   IX86_BUILTIN_CVTPI2PS,
15612   IX86_BUILTIN_CVTPS2PI,
15613   IX86_BUILTIN_CVTSI2SS,
15614   IX86_BUILTIN_CVTSI642SS,
15615   IX86_BUILTIN_CVTSS2SI,
15616   IX86_BUILTIN_CVTSS2SI64,
15617   IX86_BUILTIN_CVTTPS2PI,
15618   IX86_BUILTIN_CVTTSS2SI,
15619   IX86_BUILTIN_CVTTSS2SI64,
15620
15621   IX86_BUILTIN_MAXPS,
15622   IX86_BUILTIN_MAXSS,
15623   IX86_BUILTIN_MINPS,
15624   IX86_BUILTIN_MINSS,
15625
15626   IX86_BUILTIN_LOADUPS,
15627   IX86_BUILTIN_STOREUPS,
15628   IX86_BUILTIN_MOVSS,
15629
15630   IX86_BUILTIN_MOVHLPS,
15631   IX86_BUILTIN_MOVLHPS,
15632   IX86_BUILTIN_LOADHPS,
15633   IX86_BUILTIN_LOADLPS,
15634   IX86_BUILTIN_STOREHPS,
15635   IX86_BUILTIN_STORELPS,
15636
15637   IX86_BUILTIN_MASKMOVQ,
15638   IX86_BUILTIN_MOVMSKPS,
15639   IX86_BUILTIN_PMOVMSKB,
15640
15641   IX86_BUILTIN_MOVNTPS,
15642   IX86_BUILTIN_MOVNTQ,
15643
15644   IX86_BUILTIN_LOADDQU,
15645   IX86_BUILTIN_STOREDQU,
15646
15647   IX86_BUILTIN_PACKSSWB,
15648   IX86_BUILTIN_PACKSSDW,
15649   IX86_BUILTIN_PACKUSWB,
15650
15651   IX86_BUILTIN_PADDB,
15652   IX86_BUILTIN_PADDW,
15653   IX86_BUILTIN_PADDD,
15654   IX86_BUILTIN_PADDQ,
15655   IX86_BUILTIN_PADDSB,
15656   IX86_BUILTIN_PADDSW,
15657   IX86_BUILTIN_PADDUSB,
15658   IX86_BUILTIN_PADDUSW,
15659   IX86_BUILTIN_PSUBB,
15660   IX86_BUILTIN_PSUBW,
15661   IX86_BUILTIN_PSUBD,
15662   IX86_BUILTIN_PSUBQ,
15663   IX86_BUILTIN_PSUBSB,
15664   IX86_BUILTIN_PSUBSW,
15665   IX86_BUILTIN_PSUBUSB,
15666   IX86_BUILTIN_PSUBUSW,
15667
15668   IX86_BUILTIN_PAND,
15669   IX86_BUILTIN_PANDN,
15670   IX86_BUILTIN_POR,
15671   IX86_BUILTIN_PXOR,
15672
15673   IX86_BUILTIN_PAVGB,
15674   IX86_BUILTIN_PAVGW,
15675
15676   IX86_BUILTIN_PCMPEQB,
15677   IX86_BUILTIN_PCMPEQW,
15678   IX86_BUILTIN_PCMPEQD,
15679   IX86_BUILTIN_PCMPGTB,
15680   IX86_BUILTIN_PCMPGTW,
15681   IX86_BUILTIN_PCMPGTD,
15682
15683   IX86_BUILTIN_PMADDWD,
15684
15685   IX86_BUILTIN_PMAXSW,
15686   IX86_BUILTIN_PMAXUB,
15687   IX86_BUILTIN_PMINSW,
15688   IX86_BUILTIN_PMINUB,
15689
15690   IX86_BUILTIN_PMULHUW,
15691   IX86_BUILTIN_PMULHW,
15692   IX86_BUILTIN_PMULLW,
15693
15694   IX86_BUILTIN_PSADBW,
15695   IX86_BUILTIN_PSHUFW,
15696
15697   IX86_BUILTIN_PSLLW,
15698   IX86_BUILTIN_PSLLD,
15699   IX86_BUILTIN_PSLLQ,
15700   IX86_BUILTIN_PSRAW,
15701   IX86_BUILTIN_PSRAD,
15702   IX86_BUILTIN_PSRLW,
15703   IX86_BUILTIN_PSRLD,
15704   IX86_BUILTIN_PSRLQ,
15705   IX86_BUILTIN_PSLLWI,
15706   IX86_BUILTIN_PSLLDI,
15707   IX86_BUILTIN_PSLLQI,
15708   IX86_BUILTIN_PSRAWI,
15709   IX86_BUILTIN_PSRADI,
15710   IX86_BUILTIN_PSRLWI,
15711   IX86_BUILTIN_PSRLDI,
15712   IX86_BUILTIN_PSRLQI,
15713
15714   IX86_BUILTIN_PUNPCKHBW,
15715   IX86_BUILTIN_PUNPCKHWD,
15716   IX86_BUILTIN_PUNPCKHDQ,
15717   IX86_BUILTIN_PUNPCKLBW,
15718   IX86_BUILTIN_PUNPCKLWD,
15719   IX86_BUILTIN_PUNPCKLDQ,
15720
15721   IX86_BUILTIN_SHUFPS,
15722
15723   IX86_BUILTIN_RCPPS,
15724   IX86_BUILTIN_RCPSS,
15725   IX86_BUILTIN_RSQRTPS,
15726   IX86_BUILTIN_RSQRTSS,
15727   IX86_BUILTIN_SQRTPS,
15728   IX86_BUILTIN_SQRTSS,
15729
15730   IX86_BUILTIN_UNPCKHPS,
15731   IX86_BUILTIN_UNPCKLPS,
15732
15733   IX86_BUILTIN_ANDPS,
15734   IX86_BUILTIN_ANDNPS,
15735   IX86_BUILTIN_ORPS,
15736   IX86_BUILTIN_XORPS,
15737
15738   IX86_BUILTIN_EMMS,
15739   IX86_BUILTIN_LDMXCSR,
15740   IX86_BUILTIN_STMXCSR,
15741   IX86_BUILTIN_SFENCE,
15742
15743   /* 3DNow! Original */
15744   IX86_BUILTIN_FEMMS,
15745   IX86_BUILTIN_PAVGUSB,
15746   IX86_BUILTIN_PF2ID,
15747   IX86_BUILTIN_PFACC,
15748   IX86_BUILTIN_PFADD,
15749   IX86_BUILTIN_PFCMPEQ,
15750   IX86_BUILTIN_PFCMPGE,
15751   IX86_BUILTIN_PFCMPGT,
15752   IX86_BUILTIN_PFMAX,
15753   IX86_BUILTIN_PFMIN,
15754   IX86_BUILTIN_PFMUL,
15755   IX86_BUILTIN_PFRCP,
15756   IX86_BUILTIN_PFRCPIT1,
15757   IX86_BUILTIN_PFRCPIT2,
15758   IX86_BUILTIN_PFRSQIT1,
15759   IX86_BUILTIN_PFRSQRT,
15760   IX86_BUILTIN_PFSUB,
15761   IX86_BUILTIN_PFSUBR,
15762   IX86_BUILTIN_PI2FD,
15763   IX86_BUILTIN_PMULHRW,
15764
15765   /* 3DNow! Athlon Extensions */
15766   IX86_BUILTIN_PF2IW,
15767   IX86_BUILTIN_PFNACC,
15768   IX86_BUILTIN_PFPNACC,
15769   IX86_BUILTIN_PI2FW,
15770   IX86_BUILTIN_PSWAPDSI,
15771   IX86_BUILTIN_PSWAPDSF,
15772
15773   /* SSE2 */
15774   IX86_BUILTIN_ADDPD,
15775   IX86_BUILTIN_ADDSD,
15776   IX86_BUILTIN_DIVPD,
15777   IX86_BUILTIN_DIVSD,
15778   IX86_BUILTIN_MULPD,
15779   IX86_BUILTIN_MULSD,
15780   IX86_BUILTIN_SUBPD,
15781   IX86_BUILTIN_SUBSD,
15782
15783   IX86_BUILTIN_CMPEQPD,
15784   IX86_BUILTIN_CMPLTPD,
15785   IX86_BUILTIN_CMPLEPD,
15786   IX86_BUILTIN_CMPGTPD,
15787   IX86_BUILTIN_CMPGEPD,
15788   IX86_BUILTIN_CMPNEQPD,
15789   IX86_BUILTIN_CMPNLTPD,
15790   IX86_BUILTIN_CMPNLEPD,
15791   IX86_BUILTIN_CMPNGTPD,
15792   IX86_BUILTIN_CMPNGEPD,
15793   IX86_BUILTIN_CMPORDPD,
15794   IX86_BUILTIN_CMPUNORDPD,
15795   IX86_BUILTIN_CMPEQSD,
15796   IX86_BUILTIN_CMPLTSD,
15797   IX86_BUILTIN_CMPLESD,
15798   IX86_BUILTIN_CMPNEQSD,
15799   IX86_BUILTIN_CMPNLTSD,
15800   IX86_BUILTIN_CMPNLESD,
15801   IX86_BUILTIN_CMPORDSD,
15802   IX86_BUILTIN_CMPUNORDSD,
15803
15804   IX86_BUILTIN_COMIEQSD,
15805   IX86_BUILTIN_COMILTSD,
15806   IX86_BUILTIN_COMILESD,
15807   IX86_BUILTIN_COMIGTSD,
15808   IX86_BUILTIN_COMIGESD,
15809   IX86_BUILTIN_COMINEQSD,
15810   IX86_BUILTIN_UCOMIEQSD,
15811   IX86_BUILTIN_UCOMILTSD,
15812   IX86_BUILTIN_UCOMILESD,
15813   IX86_BUILTIN_UCOMIGTSD,
15814   IX86_BUILTIN_UCOMIGESD,
15815   IX86_BUILTIN_UCOMINEQSD,
15816
15817   IX86_BUILTIN_MAXPD,
15818   IX86_BUILTIN_MAXSD,
15819   IX86_BUILTIN_MINPD,
15820   IX86_BUILTIN_MINSD,
15821
15822   IX86_BUILTIN_ANDPD,
15823   IX86_BUILTIN_ANDNPD,
15824   IX86_BUILTIN_ORPD,
15825   IX86_BUILTIN_XORPD,
15826
15827   IX86_BUILTIN_SQRTPD,
15828   IX86_BUILTIN_SQRTSD,
15829
15830   IX86_BUILTIN_UNPCKHPD,
15831   IX86_BUILTIN_UNPCKLPD,
15832
15833   IX86_BUILTIN_SHUFPD,
15834
15835   IX86_BUILTIN_LOADUPD,
15836   IX86_BUILTIN_STOREUPD,
15837   IX86_BUILTIN_MOVSD,
15838
15839   IX86_BUILTIN_LOADHPD,
15840   IX86_BUILTIN_LOADLPD,
15841
15842   IX86_BUILTIN_CVTDQ2PD,
15843   IX86_BUILTIN_CVTDQ2PS,
15844
15845   IX86_BUILTIN_CVTPD2DQ,
15846   IX86_BUILTIN_CVTPD2PI,
15847   IX86_BUILTIN_CVTPD2PS,
15848   IX86_BUILTIN_CVTTPD2DQ,
15849   IX86_BUILTIN_CVTTPD2PI,
15850
15851   IX86_BUILTIN_CVTPI2PD,
15852   IX86_BUILTIN_CVTSI2SD,
15853   IX86_BUILTIN_CVTSI642SD,
15854
15855   IX86_BUILTIN_CVTSD2SI,
15856   IX86_BUILTIN_CVTSD2SI64,
15857   IX86_BUILTIN_CVTSD2SS,
15858   IX86_BUILTIN_CVTSS2SD,
15859   IX86_BUILTIN_CVTTSD2SI,
15860   IX86_BUILTIN_CVTTSD2SI64,
15861
15862   IX86_BUILTIN_CVTPS2DQ,
15863   IX86_BUILTIN_CVTPS2PD,
15864   IX86_BUILTIN_CVTTPS2DQ,
15865
15866   IX86_BUILTIN_MOVNTI,
15867   IX86_BUILTIN_MOVNTPD,
15868   IX86_BUILTIN_MOVNTDQ,
15869
15870   /* SSE2 MMX */
15871   IX86_BUILTIN_MASKMOVDQU,
15872   IX86_BUILTIN_MOVMSKPD,
15873   IX86_BUILTIN_PMOVMSKB128,
15874
15875   IX86_BUILTIN_PACKSSWB128,
15876   IX86_BUILTIN_PACKSSDW128,
15877   IX86_BUILTIN_PACKUSWB128,
15878
15879   IX86_BUILTIN_PADDB128,
15880   IX86_BUILTIN_PADDW128,
15881   IX86_BUILTIN_PADDD128,
15882   IX86_BUILTIN_PADDQ128,
15883   IX86_BUILTIN_PADDSB128,
15884   IX86_BUILTIN_PADDSW128,
15885   IX86_BUILTIN_PADDUSB128,
15886   IX86_BUILTIN_PADDUSW128,
15887   IX86_BUILTIN_PSUBB128,
15888   IX86_BUILTIN_PSUBW128,
15889   IX86_BUILTIN_PSUBD128,
15890   IX86_BUILTIN_PSUBQ128,
15891   IX86_BUILTIN_PSUBSB128,
15892   IX86_BUILTIN_PSUBSW128,
15893   IX86_BUILTIN_PSUBUSB128,
15894   IX86_BUILTIN_PSUBUSW128,
15895
15896   IX86_BUILTIN_PAND128,
15897   IX86_BUILTIN_PANDN128,
15898   IX86_BUILTIN_POR128,
15899   IX86_BUILTIN_PXOR128,
15900
15901   IX86_BUILTIN_PAVGB128,
15902   IX86_BUILTIN_PAVGW128,
15903
15904   IX86_BUILTIN_PCMPEQB128,
15905   IX86_BUILTIN_PCMPEQW128,
15906   IX86_BUILTIN_PCMPEQD128,
15907   IX86_BUILTIN_PCMPGTB128,
15908   IX86_BUILTIN_PCMPGTW128,
15909   IX86_BUILTIN_PCMPGTD128,
15910
15911   IX86_BUILTIN_PMADDWD128,
15912
15913   IX86_BUILTIN_PMAXSW128,
15914   IX86_BUILTIN_PMAXUB128,
15915   IX86_BUILTIN_PMINSW128,
15916   IX86_BUILTIN_PMINUB128,
15917
15918   IX86_BUILTIN_PMULUDQ,
15919   IX86_BUILTIN_PMULUDQ128,
15920   IX86_BUILTIN_PMULHUW128,
15921   IX86_BUILTIN_PMULHW128,
15922   IX86_BUILTIN_PMULLW128,
15923
15924   IX86_BUILTIN_PSADBW128,
15925   IX86_BUILTIN_PSHUFHW,
15926   IX86_BUILTIN_PSHUFLW,
15927   IX86_BUILTIN_PSHUFD,
15928
15929   IX86_BUILTIN_PSLLDQI128,
15930   IX86_BUILTIN_PSLLWI128,
15931   IX86_BUILTIN_PSLLDI128,
15932   IX86_BUILTIN_PSLLQI128,
15933   IX86_BUILTIN_PSRAWI128,
15934   IX86_BUILTIN_PSRADI128,
15935   IX86_BUILTIN_PSRLDQI128,
15936   IX86_BUILTIN_PSRLWI128,
15937   IX86_BUILTIN_PSRLDI128,
15938   IX86_BUILTIN_PSRLQI128,
15939
15940   IX86_BUILTIN_PSLLDQ128,
15941   IX86_BUILTIN_PSLLW128,
15942   IX86_BUILTIN_PSLLD128,
15943   IX86_BUILTIN_PSLLQ128,
15944   IX86_BUILTIN_PSRAW128,
15945   IX86_BUILTIN_PSRAD128,
15946   IX86_BUILTIN_PSRLW128,
15947   IX86_BUILTIN_PSRLD128,
15948   IX86_BUILTIN_PSRLQ128,
15949
15950   IX86_BUILTIN_PUNPCKHBW128,
15951   IX86_BUILTIN_PUNPCKHWD128,
15952   IX86_BUILTIN_PUNPCKHDQ128,
15953   IX86_BUILTIN_PUNPCKHQDQ128,
15954   IX86_BUILTIN_PUNPCKLBW128,
15955   IX86_BUILTIN_PUNPCKLWD128,
15956   IX86_BUILTIN_PUNPCKLDQ128,
15957   IX86_BUILTIN_PUNPCKLQDQ128,
15958
15959   IX86_BUILTIN_CLFLUSH,
15960   IX86_BUILTIN_MFENCE,
15961   IX86_BUILTIN_LFENCE,
15962
15963   /* Prescott New Instructions.  */
15964   IX86_BUILTIN_ADDSUBPS,
15965   IX86_BUILTIN_HADDPS,
15966   IX86_BUILTIN_HSUBPS,
15967   IX86_BUILTIN_MOVSHDUP,
15968   IX86_BUILTIN_MOVSLDUP,
15969   IX86_BUILTIN_ADDSUBPD,
15970   IX86_BUILTIN_HADDPD,
15971   IX86_BUILTIN_HSUBPD,
15972   IX86_BUILTIN_LDDQU,
15973
15974   IX86_BUILTIN_MONITOR,
15975   IX86_BUILTIN_MWAIT,
15976
15977   /* SSSE3.  */
15978   IX86_BUILTIN_PHADDW,
15979   IX86_BUILTIN_PHADDD,
15980   IX86_BUILTIN_PHADDSW,
15981   IX86_BUILTIN_PHSUBW,
15982   IX86_BUILTIN_PHSUBD,
15983   IX86_BUILTIN_PHSUBSW,
15984   IX86_BUILTIN_PMADDUBSW,
15985   IX86_BUILTIN_PMULHRSW,
15986   IX86_BUILTIN_PSHUFB,
15987   IX86_BUILTIN_PSIGNB,
15988   IX86_BUILTIN_PSIGNW,
15989   IX86_BUILTIN_PSIGND,
15990   IX86_BUILTIN_PALIGNR,
15991   IX86_BUILTIN_PABSB,
15992   IX86_BUILTIN_PABSW,
15993   IX86_BUILTIN_PABSD,
15994
15995   IX86_BUILTIN_PHADDW128,
15996   IX86_BUILTIN_PHADDD128,
15997   IX86_BUILTIN_PHADDSW128,
15998   IX86_BUILTIN_PHSUBW128,
15999   IX86_BUILTIN_PHSUBD128,
16000   IX86_BUILTIN_PHSUBSW128,
16001   IX86_BUILTIN_PMADDUBSW128,
16002   IX86_BUILTIN_PMULHRSW128,
16003   IX86_BUILTIN_PSHUFB128,
16004   IX86_BUILTIN_PSIGNB128,
16005   IX86_BUILTIN_PSIGNW128,
16006   IX86_BUILTIN_PSIGND128,
16007   IX86_BUILTIN_PALIGNR128,
16008   IX86_BUILTIN_PABSB128,
16009   IX86_BUILTIN_PABSW128,
16010   IX86_BUILTIN_PABSD128,
16011
16012   /* AMDFAM10 - SSE4A New Instructions.  */
16013   IX86_BUILTIN_MOVNTSD,
16014   IX86_BUILTIN_MOVNTSS,
16015   IX86_BUILTIN_EXTRQI,
16016   IX86_BUILTIN_EXTRQ,
16017   IX86_BUILTIN_INSERTQI,
16018   IX86_BUILTIN_INSERTQ,
16019
16020   IX86_BUILTIN_VEC_INIT_V2SI,
16021   IX86_BUILTIN_VEC_INIT_V4HI,
16022   IX86_BUILTIN_VEC_INIT_V8QI,
16023   IX86_BUILTIN_VEC_EXT_V2DF,
16024   IX86_BUILTIN_VEC_EXT_V2DI,
16025   IX86_BUILTIN_VEC_EXT_V4SF,
16026   IX86_BUILTIN_VEC_EXT_V4SI,
16027   IX86_BUILTIN_VEC_EXT_V8HI,
16028   IX86_BUILTIN_VEC_EXT_V2SI,
16029   IX86_BUILTIN_VEC_EXT_V4HI,
16030   IX86_BUILTIN_VEC_SET_V8HI,
16031   IX86_BUILTIN_VEC_SET_V4HI,
16032
16033   IX86_BUILTIN_MAX
16034 };
16035
16036 /* Table for the ix86 builtin decls.  */
16037 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
16038
16039 /* Add a ix86 target builtin function with CODE, NAME and TYPE.  Do so,
16040  * if the target_flags include one of MASK.  Stores the function decl
16041  * in the ix86_builtins array.
16042  * Returns the function decl or NULL_TREE, if the builtin was not added.  */
16043
16044 static inline tree
16045 def_builtin (int mask, const char *name, tree type, enum ix86_builtins code)
16046 {
16047   tree decl = NULL_TREE;
16048
16049   if (mask & target_flags
16050       && (!(mask & MASK_64BIT) || TARGET_64BIT))
16051     {
16052       decl = add_builtin_function (name, type, code, BUILT_IN_MD,
16053                                    NULL, NULL_TREE);
16054       ix86_builtins[(int) code] = decl;
16055     }
16056
16057   return decl;
16058 }
16059
16060 /* Like def_builtin, but also marks the function decl "const".  */
16061
16062 static inline tree
16063 def_builtin_const (int mask, const char *name, tree type,
16064                    enum ix86_builtins code)
16065 {
16066   tree decl = def_builtin (mask, name, type, code);
16067   if (decl)
16068     TREE_READONLY (decl) = 1;
16069   return decl;
16070 }
16071
16072 /* Bits for builtin_description.flag.  */
16073
16074 /* Set when we don't support the comparison natively, and should
16075    swap_comparison in order to support it.  */
16076 #define BUILTIN_DESC_SWAP_OPERANDS      1
16077
16078 struct builtin_description
16079 {
16080   const unsigned int mask;
16081   const enum insn_code icode;
16082   const char *const name;
16083   const enum ix86_builtins code;
16084   const enum rtx_code comparison;
16085   const unsigned int flag;
16086 };
16087
16088 static const struct builtin_description bdesc_comi[] =
16089 {
16090   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
16091   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
16092   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
16093   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
16094   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
16095   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
16096   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
16097   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
16098   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
16099   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
16100   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
16101   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
16102   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
16103   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
16104   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
16105   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
16106   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
16107   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
16108   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
16109   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
16110   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
16111   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
16112   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
16113   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
16114 };
16115
16116 static const struct builtin_description bdesc_2arg[] =
16117 {
16118   /* SSE */
16119   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
16120   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
16121   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
16122   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
16123   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
16124   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
16125   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
16126   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
16127
16128   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
16129   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
16130   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
16131   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
16132     BUILTIN_DESC_SWAP_OPERANDS },
16133   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
16134     BUILTIN_DESC_SWAP_OPERANDS },
16135   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
16136   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
16137   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
16138   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
16139   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
16140     BUILTIN_DESC_SWAP_OPERANDS },
16141   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
16142     BUILTIN_DESC_SWAP_OPERANDS },
16143   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
16144   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
16145   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
16146   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
16147   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
16148   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
16149   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
16150   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
16151   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
16152     BUILTIN_DESC_SWAP_OPERANDS },
16153   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
16154     BUILTIN_DESC_SWAP_OPERANDS },
16155   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
16156
16157   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
16158   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
16159   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
16160   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
16161
16162   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
16163   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
16164   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
16165   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
16166
16167   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
16168   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
16169   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
16170   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
16171   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
16172
16173   /* MMX */
16174   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
16175   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
16176   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
16177   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
16178   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
16179   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
16180   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
16181   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
16182
16183   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
16184   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
16185   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
16186   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
16187   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
16188   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
16189   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
16190   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
16191
16192   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
16193   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
16194   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
16195
16196   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
16197   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
16198   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
16199   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
16200
16201   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
16202   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
16203
16204   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
16205   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
16206   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
16207   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
16208   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
16209   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
16210
16211   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
16212   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
16213   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
16214   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
16215
16216   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
16217   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
16218   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
16219   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
16220   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
16221   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
16222
16223   /* Special.  */
16224   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
16225   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
16226   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
16227
16228   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
16229   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
16230   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
16231
16232   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
16233   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
16234   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
16235   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
16236   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
16237   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
16238
16239   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
16240   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
16241   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
16242   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
16243   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
16244   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
16245
16246   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
16247   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
16248   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
16249   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
16250
16251   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
16252   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
16253
16254   /* SSE2 */
16255   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
16256   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
16257   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
16258   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
16259   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
16260   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
16261   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
16262   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
16263
16264   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
16265   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
16266   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
16267   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
16268     BUILTIN_DESC_SWAP_OPERANDS },
16269   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
16270     BUILTIN_DESC_SWAP_OPERANDS },
16271   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
16272   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
16273   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
16274   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
16275   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
16276     BUILTIN_DESC_SWAP_OPERANDS },
16277   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
16278     BUILTIN_DESC_SWAP_OPERANDS },
16279   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
16280   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
16281   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
16282   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
16283   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
16284   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
16285   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
16286   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
16287   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
16288
16289   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
16290   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
16291   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
16292   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
16293
16294   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
16295   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
16296   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
16297   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
16298
16299   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
16300   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
16301   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
16302
16303   /* SSE2 MMX */
16304   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
16305   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
16306   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
16307   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
16308   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
16309   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
16310   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
16311   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
16312
16313   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
16314   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
16315   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
16316   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
16317   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
16318   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
16319   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
16320   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
16321
16322   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
16323   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
16324
16325   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
16326   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
16327   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
16328   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
16329
16330   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
16331   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
16332
16333   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
16334   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
16335   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
16336   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
16337   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
16338   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
16339
16340   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
16341   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
16342   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
16343   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
16344
16345   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
16346   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
16347   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
16348   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
16349   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
16350   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
16351   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
16352   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
16353
16354   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
16355   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
16356   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
16357
16358   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
16359   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
16360
16361   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
16362   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
16363
16364   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
16365   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
16366   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
16367
16368   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
16369   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
16370   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
16371
16372   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
16373   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
16374
16375   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
16376
16377   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
16378   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
16379   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
16380   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
16381
16382   /* SSE3 MMX */
16383   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
16384   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
16385   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
16386   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
16387   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
16388   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 },
16389
16390   /* SSSE3 */
16391   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, 0, 0 },
16392   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, 0, 0 },
16393   { MASK_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, 0, 0 },
16394   { MASK_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, 0, 0 },
16395   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, 0, 0 },
16396   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, 0, 0 },
16397   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, 0, 0 },
16398   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, 0, 0 },
16399   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, 0, 0 },
16400   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, 0, 0 },
16401   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, 0, 0 },
16402   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, 0, 0 },
16403   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, 0, 0 },
16404   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, 0, 0 },
16405   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, 0, 0 },
16406   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, 0, 0 },
16407   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, 0, 0 },
16408   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, 0, 0 },
16409   { MASK_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, 0, 0 },
16410   { MASK_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, 0, 0 },
16411   { MASK_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, 0, 0 },
16412   { MASK_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, 0, 0 },
16413   { MASK_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, 0, 0 },
16414   { MASK_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, 0, 0 }
16415 };
16416
16417 static const struct builtin_description bdesc_1arg[] =
16418 {
16419   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
16420   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
16421
16422   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
16423   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
16424   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
16425
16426   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
16427   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
16428   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
16429   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
16430   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
16431   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
16432
16433   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
16434   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
16435
16436   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
16437
16438   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
16439   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
16440
16441   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
16442   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
16443   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
16444   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
16445   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
16446
16447   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
16448
16449   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
16450   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
16451   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
16452   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
16453
16454   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
16455   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
16456   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
16457
16458   /* SSE3 */
16459   { MASK_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, 0, 0 },
16460   { MASK_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, 0, 0 },
16461
16462   /* SSSE3 */
16463   { MASK_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, 0, 0 },
16464   { MASK_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, 0, 0 },
16465   { MASK_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, 0, 0 },
16466   { MASK_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, 0, 0 },
16467   { MASK_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, 0, 0 },
16468   { MASK_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, 0, 0 },
16469 };
16470
16471 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
16472    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
16473    builtins.  */
16474 static void
16475 ix86_init_mmx_sse_builtins (void)
16476 {
16477   const struct builtin_description * d;
16478   size_t i;
16479
16480   tree V16QI_type_node = build_vector_type_for_mode (char_type_node, V16QImode);
16481   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
16482   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
16483   tree V2DI_type_node
16484     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
16485   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
16486   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
16487   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
16488   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
16489   tree V8QI_type_node = build_vector_type_for_mode (char_type_node, V8QImode);
16490   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
16491
16492   tree pchar_type_node = build_pointer_type (char_type_node);
16493   tree pcchar_type_node = build_pointer_type (
16494                              build_type_variant (char_type_node, 1, 0));
16495   tree pfloat_type_node = build_pointer_type (float_type_node);
16496   tree pcfloat_type_node = build_pointer_type (
16497                              build_type_variant (float_type_node, 1, 0));
16498   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
16499   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
16500   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
16501
16502   /* Comparisons.  */
16503   tree int_ftype_v4sf_v4sf
16504     = build_function_type_list (integer_type_node,
16505                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16506   tree v4si_ftype_v4sf_v4sf
16507     = build_function_type_list (V4SI_type_node,
16508                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16509   /* MMX/SSE/integer conversions.  */
16510   tree int_ftype_v4sf
16511     = build_function_type_list (integer_type_node,
16512                                 V4SF_type_node, NULL_TREE);
16513   tree int64_ftype_v4sf
16514     = build_function_type_list (long_long_integer_type_node,
16515                                 V4SF_type_node, NULL_TREE);
16516   tree int_ftype_v8qi
16517     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
16518   tree v4sf_ftype_v4sf_int
16519     = build_function_type_list (V4SF_type_node,
16520                                 V4SF_type_node, integer_type_node, NULL_TREE);
16521   tree v4sf_ftype_v4sf_int64
16522     = build_function_type_list (V4SF_type_node,
16523                                 V4SF_type_node, long_long_integer_type_node,
16524                                 NULL_TREE);
16525   tree v4sf_ftype_v4sf_v2si
16526     = build_function_type_list (V4SF_type_node,
16527                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
16528
16529   /* Miscellaneous.  */
16530   tree v8qi_ftype_v4hi_v4hi
16531     = build_function_type_list (V8QI_type_node,
16532                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16533   tree v4hi_ftype_v2si_v2si
16534     = build_function_type_list (V4HI_type_node,
16535                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16536   tree v4sf_ftype_v4sf_v4sf_int
16537     = build_function_type_list (V4SF_type_node,
16538                                 V4SF_type_node, V4SF_type_node,
16539                                 integer_type_node, NULL_TREE);
16540   tree v2si_ftype_v4hi_v4hi
16541     = build_function_type_list (V2SI_type_node,
16542                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16543   tree v4hi_ftype_v4hi_int
16544     = build_function_type_list (V4HI_type_node,
16545                                 V4HI_type_node, integer_type_node, NULL_TREE);
16546   tree v4hi_ftype_v4hi_di
16547     = build_function_type_list (V4HI_type_node,
16548                                 V4HI_type_node, long_long_unsigned_type_node,
16549                                 NULL_TREE);
16550   tree v2si_ftype_v2si_di
16551     = build_function_type_list (V2SI_type_node,
16552                                 V2SI_type_node, long_long_unsigned_type_node,
16553                                 NULL_TREE);
16554   tree void_ftype_void
16555     = build_function_type (void_type_node, void_list_node);
16556   tree void_ftype_unsigned
16557     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
16558   tree void_ftype_unsigned_unsigned
16559     = build_function_type_list (void_type_node, unsigned_type_node,
16560                                 unsigned_type_node, NULL_TREE);
16561   tree void_ftype_pcvoid_unsigned_unsigned
16562     = build_function_type_list (void_type_node, const_ptr_type_node,
16563                                 unsigned_type_node, unsigned_type_node,
16564                                 NULL_TREE);
16565   tree unsigned_ftype_void
16566     = build_function_type (unsigned_type_node, void_list_node);
16567   tree v2si_ftype_v4sf
16568     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
16569   /* Loads/stores.  */
16570   tree void_ftype_v8qi_v8qi_pchar
16571     = build_function_type_list (void_type_node,
16572                                 V8QI_type_node, V8QI_type_node,
16573                                 pchar_type_node, NULL_TREE);
16574   tree v4sf_ftype_pcfloat
16575     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
16576   /* @@@ the type is bogus */
16577   tree v4sf_ftype_v4sf_pv2si
16578     = build_function_type_list (V4SF_type_node,
16579                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
16580   tree void_ftype_pv2si_v4sf
16581     = build_function_type_list (void_type_node,
16582                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
16583   tree void_ftype_pfloat_v4sf
16584     = build_function_type_list (void_type_node,
16585                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
16586   tree void_ftype_pdi_di
16587     = build_function_type_list (void_type_node,
16588                                 pdi_type_node, long_long_unsigned_type_node,
16589                                 NULL_TREE);
16590   tree void_ftype_pv2di_v2di
16591     = build_function_type_list (void_type_node,
16592                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
16593   /* Normal vector unops.  */
16594   tree v4sf_ftype_v4sf
16595     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
16596   tree v16qi_ftype_v16qi
16597     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
16598   tree v8hi_ftype_v8hi
16599     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
16600   tree v4si_ftype_v4si
16601     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
16602   tree v8qi_ftype_v8qi
16603     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
16604   tree v4hi_ftype_v4hi
16605     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
16606
16607   /* Normal vector binops.  */
16608   tree v4sf_ftype_v4sf_v4sf
16609     = build_function_type_list (V4SF_type_node,
16610                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16611   tree v8qi_ftype_v8qi_v8qi
16612     = build_function_type_list (V8QI_type_node,
16613                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16614   tree v4hi_ftype_v4hi_v4hi
16615     = build_function_type_list (V4HI_type_node,
16616                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16617   tree v2si_ftype_v2si_v2si
16618     = build_function_type_list (V2SI_type_node,
16619                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16620   tree di_ftype_di_di
16621     = build_function_type_list (long_long_unsigned_type_node,
16622                                 long_long_unsigned_type_node,
16623                                 long_long_unsigned_type_node, NULL_TREE);
16624
16625   tree di_ftype_di_di_int
16626     = build_function_type_list (long_long_unsigned_type_node,
16627                                 long_long_unsigned_type_node,
16628                                 long_long_unsigned_type_node,
16629                                 integer_type_node, NULL_TREE);
16630
16631   tree v2si_ftype_v2sf
16632     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
16633   tree v2sf_ftype_v2si
16634     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
16635   tree v2si_ftype_v2si
16636     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
16637   tree v2sf_ftype_v2sf
16638     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
16639   tree v2sf_ftype_v2sf_v2sf
16640     = build_function_type_list (V2SF_type_node,
16641                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16642   tree v2si_ftype_v2sf_v2sf
16643     = build_function_type_list (V2SI_type_node,
16644                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16645   tree pint_type_node    = build_pointer_type (integer_type_node);
16646   tree pdouble_type_node = build_pointer_type (double_type_node);
16647   tree pcdouble_type_node = build_pointer_type (
16648                                 build_type_variant (double_type_node, 1, 0));
16649   tree int_ftype_v2df_v2df
16650     = build_function_type_list (integer_type_node,
16651                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16652
16653   tree void_ftype_pcvoid
16654     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
16655   tree v4sf_ftype_v4si
16656     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
16657   tree v4si_ftype_v4sf
16658     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
16659   tree v2df_ftype_v4si
16660     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
16661   tree v4si_ftype_v2df
16662     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
16663   tree v2si_ftype_v2df
16664     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
16665   tree v4sf_ftype_v2df
16666     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
16667   tree v2df_ftype_v2si
16668     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
16669   tree v2df_ftype_v4sf
16670     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
16671   tree int_ftype_v2df
16672     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
16673   tree int64_ftype_v2df
16674     = build_function_type_list (long_long_integer_type_node,
16675                                 V2DF_type_node, NULL_TREE);
16676   tree v2df_ftype_v2df_int
16677     = build_function_type_list (V2DF_type_node,
16678                                 V2DF_type_node, integer_type_node, NULL_TREE);
16679   tree v2df_ftype_v2df_int64
16680     = build_function_type_list (V2DF_type_node,
16681                                 V2DF_type_node, long_long_integer_type_node,
16682                                 NULL_TREE);
16683   tree v4sf_ftype_v4sf_v2df
16684     = build_function_type_list (V4SF_type_node,
16685                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
16686   tree v2df_ftype_v2df_v4sf
16687     = build_function_type_list (V2DF_type_node,
16688                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
16689   tree v2df_ftype_v2df_v2df_int
16690     = build_function_type_list (V2DF_type_node,
16691                                 V2DF_type_node, V2DF_type_node,
16692                                 integer_type_node,
16693                                 NULL_TREE);
16694   tree v2df_ftype_v2df_pcdouble
16695     = build_function_type_list (V2DF_type_node,
16696                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
16697   tree void_ftype_pdouble_v2df
16698     = build_function_type_list (void_type_node,
16699                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
16700   tree void_ftype_pint_int
16701     = build_function_type_list (void_type_node,
16702                                 pint_type_node, integer_type_node, NULL_TREE);
16703   tree void_ftype_v16qi_v16qi_pchar
16704     = build_function_type_list (void_type_node,
16705                                 V16QI_type_node, V16QI_type_node,
16706                                 pchar_type_node, NULL_TREE);
16707   tree v2df_ftype_pcdouble
16708     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
16709   tree v2df_ftype_v2df_v2df
16710     = build_function_type_list (V2DF_type_node,
16711                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16712   tree v16qi_ftype_v16qi_v16qi
16713     = build_function_type_list (V16QI_type_node,
16714                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
16715   tree v8hi_ftype_v8hi_v8hi
16716     = build_function_type_list (V8HI_type_node,
16717                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
16718   tree v4si_ftype_v4si_v4si
16719     = build_function_type_list (V4SI_type_node,
16720                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
16721   tree v2di_ftype_v2di_v2di
16722     = build_function_type_list (V2DI_type_node,
16723                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
16724   tree v2di_ftype_v2df_v2df
16725     = build_function_type_list (V2DI_type_node,
16726                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16727   tree v2df_ftype_v2df
16728     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
16729   tree v2di_ftype_v2di_int
16730     = build_function_type_list (V2DI_type_node,
16731                                 V2DI_type_node, integer_type_node, NULL_TREE);
16732   tree v2di_ftype_v2di_v2di_int
16733     = build_function_type_list (V2DI_type_node, V2DI_type_node,
16734                                 V2DI_type_node, integer_type_node, NULL_TREE);
16735   tree v4si_ftype_v4si_int
16736     = build_function_type_list (V4SI_type_node,
16737                                 V4SI_type_node, integer_type_node, NULL_TREE);
16738   tree v8hi_ftype_v8hi_int
16739     = build_function_type_list (V8HI_type_node,
16740                                 V8HI_type_node, integer_type_node, NULL_TREE);
16741   tree v4si_ftype_v8hi_v8hi
16742     = build_function_type_list (V4SI_type_node,
16743                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
16744   tree di_ftype_v8qi_v8qi
16745     = build_function_type_list (long_long_unsigned_type_node,
16746                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16747   tree di_ftype_v2si_v2si
16748     = build_function_type_list (long_long_unsigned_type_node,
16749                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16750   tree v2di_ftype_v16qi_v16qi
16751     = build_function_type_list (V2DI_type_node,
16752                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
16753   tree v2di_ftype_v4si_v4si
16754     = build_function_type_list (V2DI_type_node,
16755                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
16756   tree int_ftype_v16qi
16757     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
16758   tree v16qi_ftype_pcchar
16759     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
16760   tree void_ftype_pchar_v16qi
16761     = build_function_type_list (void_type_node,
16762                                 pchar_type_node, V16QI_type_node, NULL_TREE);
16763
16764   tree v2di_ftype_v2di_unsigned_unsigned
16765     = build_function_type_list (V2DI_type_node, V2DI_type_node,
16766                                 unsigned_type_node, unsigned_type_node,
16767                                 NULL_TREE);
16768   tree v2di_ftype_v2di_v2di_unsigned_unsigned
16769     = build_function_type_list (V2DI_type_node, V2DI_type_node, V2DI_type_node,
16770                                 unsigned_type_node, unsigned_type_node,
16771                                 NULL_TREE);
16772   tree v2di_ftype_v2di_v16qi
16773     = build_function_type_list (V2DI_type_node, V2DI_type_node, V16QI_type_node,
16774                                 NULL_TREE);
16775
16776   tree float80_type;
16777   tree float128_type;
16778   tree ftype;
16779
16780   /* The __float80 type.  */
16781   if (TYPE_MODE (long_double_type_node) == XFmode)
16782     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
16783                                                "__float80");
16784   else
16785     {
16786       /* The __float80 type.  */
16787       float80_type = make_node (REAL_TYPE);
16788       TYPE_PRECISION (float80_type) = 80;
16789       layout_type (float80_type);
16790       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
16791     }
16792
16793   if (TARGET_64BIT)
16794     {
16795       float128_type = make_node (REAL_TYPE);
16796       TYPE_PRECISION (float128_type) = 128;
16797       layout_type (float128_type);
16798       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
16799     }
16800
16801   /* Add all builtins that are more or less simple operations on two
16802      operands.  */
16803   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16804     {
16805       /* Use one of the operands; the target can have a different mode for
16806          mask-generating compares.  */
16807       enum machine_mode mode;
16808       tree type;
16809
16810       if (d->name == 0)
16811         continue;
16812       mode = insn_data[d->icode].operand[1].mode;
16813
16814       switch (mode)
16815         {
16816         case V16QImode:
16817           type = v16qi_ftype_v16qi_v16qi;
16818           break;
16819         case V8HImode:
16820           type = v8hi_ftype_v8hi_v8hi;
16821           break;
16822         case V4SImode:
16823           type = v4si_ftype_v4si_v4si;
16824           break;
16825         case V2DImode:
16826           type = v2di_ftype_v2di_v2di;
16827           break;
16828         case V2DFmode:
16829           type = v2df_ftype_v2df_v2df;
16830           break;
16831         case V4SFmode:
16832           type = v4sf_ftype_v4sf_v4sf;
16833           break;
16834         case V8QImode:
16835           type = v8qi_ftype_v8qi_v8qi;
16836           break;
16837         case V4HImode:
16838           type = v4hi_ftype_v4hi_v4hi;
16839           break;
16840         case V2SImode:
16841           type = v2si_ftype_v2si_v2si;
16842           break;
16843         case DImode:
16844           type = di_ftype_di_di;
16845           break;
16846
16847         default:
16848           gcc_unreachable ();
16849         }
16850
16851       /* Override for comparisons.  */
16852       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
16853           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
16854         type = v4si_ftype_v4sf_v4sf;
16855
16856       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
16857           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
16858         type = v2di_ftype_v2df_v2df;
16859
16860       def_builtin (d->mask, d->name, type, d->code);
16861     }
16862
16863   /* Add all builtins that are more or less simple operations on 1 operand.  */
16864   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16865     {
16866       enum machine_mode mode;
16867       tree type;
16868
16869       if (d->name == 0)
16870         continue;
16871       mode = insn_data[d->icode].operand[1].mode;
16872
16873       switch (mode)
16874         {
16875         case V16QImode:
16876           type = v16qi_ftype_v16qi;
16877           break;
16878         case V8HImode:
16879           type = v8hi_ftype_v8hi;
16880           break;
16881         case V4SImode:
16882           type = v4si_ftype_v4si;
16883           break;
16884         case V2DFmode:
16885           type = v2df_ftype_v2df;
16886           break;
16887         case V4SFmode:
16888           type = v4sf_ftype_v4sf;
16889           break;
16890         case V8QImode:
16891           type = v8qi_ftype_v8qi;
16892           break;
16893         case V4HImode:
16894           type = v4hi_ftype_v4hi;
16895           break;
16896         case V2SImode:
16897           type = v2si_ftype_v2si;
16898           break;
16899
16900         default:
16901           abort ();
16902         }
16903
16904       def_builtin (d->mask, d->name, type, d->code);
16905     }
16906
16907   /* Add the remaining MMX insns with somewhat more complicated types.  */
16908   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
16909   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
16910   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
16911   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
16912
16913   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
16914   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
16915   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
16916
16917   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
16918   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
16919
16920   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
16921   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
16922
16923   /* comi/ucomi insns.  */
16924   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
16925     if (d->mask == MASK_SSE2)
16926       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
16927     else
16928       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
16929
16930   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
16931   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
16932   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
16933
16934   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
16935   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
16936   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
16937   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
16938   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
16939   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
16940   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
16941   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
16942   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
16943   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
16944   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
16945
16946   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
16947
16948   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
16949   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
16950
16951   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
16952   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
16953   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
16954   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
16955
16956   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
16957   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
16958   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
16959   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
16960
16961   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
16962
16963   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
16964
16965   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
16966   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
16967   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
16968   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
16969   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
16970   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
16971
16972   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
16973
16974   /* Original 3DNow!  */
16975   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
16976   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
16977   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
16978   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
16979   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
16980   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
16981   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
16982   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
16983   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
16984   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
16985   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
16986   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
16987   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
16988   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
16989   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
16990   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
16991   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
16992   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
16993   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
16994   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
16995
16996   /* 3DNow! extension as used in the Athlon CPU.  */
16997   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
16998   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
16999   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
17000   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
17001   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
17002   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
17003
17004   /* SSE2 */
17005   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
17006
17007   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
17008   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
17009
17010   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
17011   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
17012
17013   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
17014   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
17015   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
17016   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
17017   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
17018
17019   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
17020   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
17021   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
17022   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
17023
17024   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
17025   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
17026
17027   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
17028
17029   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
17030   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
17031
17032   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
17033   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
17034   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
17035   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
17036   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
17037
17038   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
17039
17040   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
17041   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
17042   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
17043   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
17044
17045   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
17046   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
17047   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
17048
17049   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
17050   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
17051   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
17052   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
17053
17054   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
17055   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
17056   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
17057
17058   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
17059   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
17060
17061   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
17062   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
17063
17064   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
17065   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
17066   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
17067   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
17068   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSLLW128);
17069   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSLLD128);
17070   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
17071
17072   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
17073   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
17074   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
17075   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
17076   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRLW128);
17077   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRLD128);
17078   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
17079
17080   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
17081   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
17082   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRAW128);
17083   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRAD128);
17084
17085   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
17086
17087   /* Prescott New Instructions.  */
17088   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
17089                void_ftype_pcvoid_unsigned_unsigned,
17090                IX86_BUILTIN_MONITOR);
17091   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
17092                void_ftype_unsigned_unsigned,
17093                IX86_BUILTIN_MWAIT);
17094   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
17095                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
17096
17097   /* SSSE3.  */
17098   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr128",
17099                v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
17100   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int,
17101                IX86_BUILTIN_PALIGNR);
17102
17103   /* AMDFAM10 SSE4A New built-ins  */
17104   def_builtin (MASK_SSE4A, "__builtin_ia32_movntsd", 
17105                void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTSD);
17106   def_builtin (MASK_SSE4A, "__builtin_ia32_movntss", 
17107                void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTSS);
17108   def_builtin (MASK_SSE4A, "__builtin_ia32_extrqi", 
17109                v2di_ftype_v2di_unsigned_unsigned, IX86_BUILTIN_EXTRQI);
17110   def_builtin (MASK_SSE4A, "__builtin_ia32_extrq",
17111                v2di_ftype_v2di_v16qi,  IX86_BUILTIN_EXTRQ);
17112   def_builtin (MASK_SSE4A, "__builtin_ia32_insertqi",
17113                v2di_ftype_v2di_v2di_unsigned_unsigned, IX86_BUILTIN_INSERTQI);
17114   def_builtin (MASK_SSE4A, "__builtin_ia32_insertq",
17115                v2di_ftype_v2di_v2di, IX86_BUILTIN_INSERTQ);
17116
17117   /* Access to the vec_init patterns.  */
17118   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
17119                                     integer_type_node, NULL_TREE);
17120   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
17121                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
17122
17123   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
17124                                     short_integer_type_node,
17125                                     short_integer_type_node,
17126                                     short_integer_type_node, NULL_TREE);
17127   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
17128                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
17129
17130   ftype = build_function_type_list (V8QI_type_node, char_type_node,
17131                                     char_type_node, char_type_node,
17132                                     char_type_node, char_type_node,
17133                                     char_type_node, char_type_node,
17134                                     char_type_node, NULL_TREE);
17135   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
17136                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
17137
17138   /* Access to the vec_extract patterns.  */
17139   ftype = build_function_type_list (double_type_node, V2DF_type_node,
17140                                     integer_type_node, NULL_TREE);
17141   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
17142                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
17143
17144   ftype = build_function_type_list (long_long_integer_type_node,
17145                                     V2DI_type_node, integer_type_node,
17146                                     NULL_TREE);
17147   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
17148                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
17149
17150   ftype = build_function_type_list (float_type_node, V4SF_type_node,
17151                                     integer_type_node, NULL_TREE);
17152   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
17153                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
17154
17155   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
17156                                     integer_type_node, NULL_TREE);
17157   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
17158                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
17159
17160   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
17161                                     integer_type_node, NULL_TREE);
17162   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
17163                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
17164
17165   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
17166                                     integer_type_node, NULL_TREE);
17167   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
17168                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
17169
17170   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
17171                                     integer_type_node, NULL_TREE);
17172   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
17173                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
17174
17175   /* Access to the vec_set patterns.  */
17176   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
17177                                     intHI_type_node,
17178                                     integer_type_node, NULL_TREE);
17179   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
17180                ftype, IX86_BUILTIN_VEC_SET_V8HI);
17181
17182   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
17183                                     intHI_type_node,
17184                                     integer_type_node, NULL_TREE);
17185   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
17186                ftype, IX86_BUILTIN_VEC_SET_V4HI);
17187 }
17188
17189 static void
17190 ix86_init_builtins (void)
17191 {
17192   if (TARGET_MMX)
17193     ix86_init_mmx_sse_builtins ();
17194 }
17195
17196 /* Errors in the source file can cause expand_expr to return const0_rtx
17197    where we expect a vector.  To avoid crashing, use one of the vector
17198    clear instructions.  */
17199 static rtx
17200 safe_vector_operand (rtx x, enum machine_mode mode)
17201 {
17202   if (x == const0_rtx)
17203     x = CONST0_RTX (mode);
17204   return x;
17205 }
17206
17207 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
17208
17209 static rtx
17210 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
17211 {
17212   rtx pat, xops[3];
17213   tree arg0 = CALL_EXPR_ARG (exp, 0);
17214   tree arg1 = CALL_EXPR_ARG (exp, 1);
17215   rtx op0 = expand_normal (arg0);
17216   rtx op1 = expand_normal (arg1);
17217   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17218   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17219   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
17220
17221   if (VECTOR_MODE_P (mode0))
17222     op0 = safe_vector_operand (op0, mode0);
17223   if (VECTOR_MODE_P (mode1))
17224     op1 = safe_vector_operand (op1, mode1);
17225
17226   if (optimize || !target
17227       || GET_MODE (target) != tmode
17228       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17229     target = gen_reg_rtx (tmode);
17230
17231   if (GET_MODE (op1) == SImode && mode1 == TImode)
17232     {
17233       rtx x = gen_reg_rtx (V4SImode);
17234       emit_insn (gen_sse2_loadd (x, op1));
17235       op1 = gen_lowpart (TImode, x);
17236     }
17237
17238   /* The insn must want input operands in the same modes as the
17239      result.  */
17240   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
17241               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
17242
17243   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
17244     op0 = copy_to_mode_reg (mode0, op0);
17245   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
17246     op1 = copy_to_mode_reg (mode1, op1);
17247
17248   /* ??? Using ix86_fixup_binary_operands is problematic when
17249      we've got mismatched modes.  Fake it.  */
17250
17251   xops[0] = target;
17252   xops[1] = op0;
17253   xops[2] = op1;
17254
17255   if (tmode == mode0 && tmode == mode1)
17256     {
17257       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
17258       op0 = xops[1];
17259       op1 = xops[2];
17260     }
17261   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
17262     {
17263       op0 = force_reg (mode0, op0);
17264       op1 = force_reg (mode1, op1);
17265       target = gen_reg_rtx (tmode);
17266     }
17267
17268   pat = GEN_FCN (icode) (target, op0, op1);
17269   if (! pat)
17270     return 0;
17271   emit_insn (pat);
17272   return target;
17273 }
17274
17275 /* Subroutine of ix86_expand_builtin to take care of stores.  */
17276
17277 static rtx
17278 ix86_expand_store_builtin (enum insn_code icode, tree exp)
17279 {
17280   rtx pat;
17281   tree arg0 = CALL_EXPR_ARG (exp, 0);
17282   tree arg1 = CALL_EXPR_ARG (exp, 1);
17283   rtx op0 = expand_normal (arg0);
17284   rtx op1 = expand_normal (arg1);
17285   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
17286   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
17287
17288   if (VECTOR_MODE_P (mode1))
17289     op1 = safe_vector_operand (op1, mode1);
17290
17291   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17292   op1 = copy_to_mode_reg (mode1, op1);
17293
17294   pat = GEN_FCN (icode) (op0, op1);
17295   if (pat)
17296     emit_insn (pat);
17297   return 0;
17298 }
17299
17300 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
17301
17302 static rtx
17303 ix86_expand_unop_builtin (enum insn_code icode, tree exp,
17304                           rtx target, int do_load)
17305 {
17306   rtx pat;
17307   tree arg0 = CALL_EXPR_ARG (exp, 0);
17308   rtx op0 = expand_normal (arg0);
17309   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17310   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17311
17312   if (optimize || !target
17313       || GET_MODE (target) != tmode
17314       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17315     target = gen_reg_rtx (tmode);
17316   if (do_load)
17317     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17318   else
17319     {
17320       if (VECTOR_MODE_P (mode0))
17321         op0 = safe_vector_operand (op0, mode0);
17322
17323       if ((optimize && !register_operand (op0, mode0))
17324           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17325         op0 = copy_to_mode_reg (mode0, op0);
17326     }
17327
17328   pat = GEN_FCN (icode) (target, op0);
17329   if (! pat)
17330     return 0;
17331   emit_insn (pat);
17332   return target;
17333 }
17334
17335 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
17336    sqrtss, rsqrtss, rcpss.  */
17337
17338 static rtx
17339 ix86_expand_unop1_builtin (enum insn_code icode, tree exp, rtx target)
17340 {
17341   rtx pat;
17342   tree arg0 = CALL_EXPR_ARG (exp, 0);
17343   rtx op1, op0 = expand_normal (arg0);
17344   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17345   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17346
17347   if (optimize || !target
17348       || GET_MODE (target) != tmode
17349       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17350     target = gen_reg_rtx (tmode);
17351
17352   if (VECTOR_MODE_P (mode0))
17353     op0 = safe_vector_operand (op0, mode0);
17354
17355   if ((optimize && !register_operand (op0, mode0))
17356       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17357     op0 = copy_to_mode_reg (mode0, op0);
17358
17359   op1 = op0;
17360   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
17361     op1 = copy_to_mode_reg (mode0, op1);
17362
17363   pat = GEN_FCN (icode) (target, op0, op1);
17364   if (! pat)
17365     return 0;
17366   emit_insn (pat);
17367   return target;
17368 }
17369
17370 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
17371
17372 static rtx
17373 ix86_expand_sse_compare (const struct builtin_description *d, tree exp,
17374                          rtx target)
17375 {
17376   rtx pat;
17377   tree arg0 = CALL_EXPR_ARG (exp, 0);
17378   tree arg1 = CALL_EXPR_ARG (exp, 1);
17379   rtx op0 = expand_normal (arg0);
17380   rtx op1 = expand_normal (arg1);
17381   rtx op2;
17382   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
17383   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
17384   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
17385   enum rtx_code comparison = d->comparison;
17386
17387   if (VECTOR_MODE_P (mode0))
17388     op0 = safe_vector_operand (op0, mode0);
17389   if (VECTOR_MODE_P (mode1))
17390     op1 = safe_vector_operand (op1, mode1);
17391
17392   /* Swap operands if we have a comparison that isn't available in
17393      hardware.  */
17394   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17395     {
17396       rtx tmp = gen_reg_rtx (mode1);
17397       emit_move_insn (tmp, op1);
17398       op1 = op0;
17399       op0 = tmp;
17400     }
17401
17402   if (optimize || !target
17403       || GET_MODE (target) != tmode
17404       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
17405     target = gen_reg_rtx (tmode);
17406
17407   if ((optimize && !register_operand (op0, mode0))
17408       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
17409     op0 = copy_to_mode_reg (mode0, op0);
17410   if ((optimize && !register_operand (op1, mode1))
17411       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
17412     op1 = copy_to_mode_reg (mode1, op1);
17413
17414   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17415   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
17416   if (! pat)
17417     return 0;
17418   emit_insn (pat);
17419   return target;
17420 }
17421
17422 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
17423
17424 static rtx
17425 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
17426                       rtx target)
17427 {
17428   rtx pat;
17429   tree arg0 = CALL_EXPR_ARG (exp, 0);
17430   tree arg1 = CALL_EXPR_ARG (exp, 1);
17431   rtx op0 = expand_normal (arg0);
17432   rtx op1 = expand_normal (arg1);
17433   rtx op2;
17434   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
17435   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
17436   enum rtx_code comparison = d->comparison;
17437
17438   if (VECTOR_MODE_P (mode0))
17439     op0 = safe_vector_operand (op0, mode0);
17440   if (VECTOR_MODE_P (mode1))
17441     op1 = safe_vector_operand (op1, mode1);
17442
17443   /* Swap operands if we have a comparison that isn't available in
17444      hardware.  */
17445   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17446     {
17447       rtx tmp = op1;
17448       op1 = op0;
17449       op0 = tmp;
17450     }
17451
17452   target = gen_reg_rtx (SImode);
17453   emit_move_insn (target, const0_rtx);
17454   target = gen_rtx_SUBREG (QImode, target, 0);
17455
17456   if ((optimize && !register_operand (op0, mode0))
17457       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
17458     op0 = copy_to_mode_reg (mode0, op0);
17459   if ((optimize && !register_operand (op1, mode1))
17460       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
17461     op1 = copy_to_mode_reg (mode1, op1);
17462
17463   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17464   pat = GEN_FCN (d->icode) (op0, op1);
17465   if (! pat)
17466     return 0;
17467   emit_insn (pat);
17468   emit_insn (gen_rtx_SET (VOIDmode,
17469                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
17470                           gen_rtx_fmt_ee (comparison, QImode,
17471                                           SET_DEST (pat),
17472                                           const0_rtx)));
17473
17474   return SUBREG_REG (target);
17475 }
17476
17477 /* Return the integer constant in ARG.  Constrain it to be in the range
17478    of the subparts of VEC_TYPE; issue an error if not.  */
17479
17480 static int
17481 get_element_number (tree vec_type, tree arg)
17482 {
17483   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
17484
17485   if (!host_integerp (arg, 1)
17486       || (elt = tree_low_cst (arg, 1), elt > max))
17487     {
17488       error ("selector must be an integer constant in the range 0..%wi", max);
17489       return 0;
17490     }
17491
17492   return elt;
17493 }
17494
17495 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17496    ix86_expand_vector_init.  We DO have language-level syntax for this, in
17497    the form of  (type){ init-list }.  Except that since we can't place emms
17498    instructions from inside the compiler, we can't allow the use of MMX
17499    registers unless the user explicitly asks for it.  So we do *not* define
17500    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
17501    we have builtins invoked by mmintrin.h that gives us license to emit
17502    these sorts of instructions.  */
17503
17504 static rtx
17505 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
17506 {
17507   enum machine_mode tmode = TYPE_MODE (type);
17508   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
17509   int i, n_elt = GET_MODE_NUNITS (tmode);
17510   rtvec v = rtvec_alloc (n_elt);
17511
17512   gcc_assert (VECTOR_MODE_P (tmode));
17513   gcc_assert (call_expr_nargs (exp) == n_elt);
17514
17515   for (i = 0; i < n_elt; ++i)
17516     {
17517       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
17518       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
17519     }
17520
17521   if (!target || !register_operand (target, tmode))
17522     target = gen_reg_rtx (tmode);
17523
17524   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
17525   return target;
17526 }
17527
17528 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17529    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
17530    had a language-level syntax for referencing vector elements.  */
17531
17532 static rtx
17533 ix86_expand_vec_ext_builtin (tree exp, rtx target)
17534 {
17535   enum machine_mode tmode, mode0;
17536   tree arg0, arg1;
17537   int elt;
17538   rtx op0;
17539
17540   arg0 = CALL_EXPR_ARG (exp, 0);
17541   arg1 = CALL_EXPR_ARG (exp, 1);
17542
17543   op0 = expand_normal (arg0);
17544   elt = get_element_number (TREE_TYPE (arg0), arg1);
17545
17546   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17547   mode0 = TYPE_MODE (TREE_TYPE (arg0));
17548   gcc_assert (VECTOR_MODE_P (mode0));
17549
17550   op0 = force_reg (mode0, op0);
17551
17552   if (optimize || !target || !register_operand (target, tmode))
17553     target = gen_reg_rtx (tmode);
17554
17555   ix86_expand_vector_extract (true, target, op0, elt);
17556
17557   return target;
17558 }
17559
17560 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17561    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
17562    a language-level syntax for referencing vector elements.  */
17563
17564 static rtx
17565 ix86_expand_vec_set_builtin (tree exp)
17566 {
17567   enum machine_mode tmode, mode1;
17568   tree arg0, arg1, arg2;
17569   int elt;
17570   rtx op0, op1;
17571
17572   arg0 = CALL_EXPR_ARG (exp, 0);
17573   arg1 = CALL_EXPR_ARG (exp, 1);
17574   arg2 = CALL_EXPR_ARG (exp, 2);
17575
17576   tmode = TYPE_MODE (TREE_TYPE (arg0));
17577   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17578   gcc_assert (VECTOR_MODE_P (tmode));
17579
17580   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
17581   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
17582   elt = get_element_number (TREE_TYPE (arg0), arg2);
17583
17584   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
17585     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
17586
17587   op0 = force_reg (tmode, op0);
17588   op1 = force_reg (mode1, op1);
17589
17590   ix86_expand_vector_set (true, op0, op1, elt);
17591
17592   return op0;
17593 }
17594
17595 /* Expand an expression EXP that calls a built-in function,
17596    with result going to TARGET if that's convenient
17597    (and in mode MODE if that's convenient).
17598    SUBTARGET may be used as the target for computing one of EXP's operands.
17599    IGNORE is nonzero if the value is to be ignored.  */
17600
17601 static rtx
17602 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17603                      enum machine_mode mode ATTRIBUTE_UNUSED,
17604                      int ignore ATTRIBUTE_UNUSED)
17605 {
17606   const struct builtin_description *d;
17607   size_t i;
17608   enum insn_code icode;
17609   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17610   tree arg0, arg1, arg2, arg3;
17611   rtx op0, op1, op2, op3, pat;
17612   enum machine_mode tmode, mode0, mode1, mode2, mode3, mode4;
17613   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
17614
17615   switch (fcode)
17616     {
17617     case IX86_BUILTIN_EMMS:
17618       emit_insn (gen_mmx_emms ());
17619       return 0;
17620
17621     case IX86_BUILTIN_SFENCE:
17622       emit_insn (gen_sse_sfence ());
17623       return 0;
17624
17625     case IX86_BUILTIN_MASKMOVQ:
17626     case IX86_BUILTIN_MASKMOVDQU:
17627       icode = (fcode == IX86_BUILTIN_MASKMOVQ
17628                ? CODE_FOR_mmx_maskmovq
17629                : CODE_FOR_sse2_maskmovdqu);
17630       /* Note the arg order is different from the operand order.  */
17631       arg1 = CALL_EXPR_ARG (exp, 0);
17632       arg2 = CALL_EXPR_ARG (exp, 1);
17633       arg0 = CALL_EXPR_ARG (exp, 2);
17634       op0 = expand_normal (arg0);
17635       op1 = expand_normal (arg1);
17636       op2 = expand_normal (arg2);
17637       mode0 = insn_data[icode].operand[0].mode;
17638       mode1 = insn_data[icode].operand[1].mode;
17639       mode2 = insn_data[icode].operand[2].mode;
17640
17641       op0 = force_reg (Pmode, op0);
17642       op0 = gen_rtx_MEM (mode1, op0);
17643
17644       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
17645         op0 = copy_to_mode_reg (mode0, op0);
17646       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17647         op1 = copy_to_mode_reg (mode1, op1);
17648       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
17649         op2 = copy_to_mode_reg (mode2, op2);
17650       pat = GEN_FCN (icode) (op0, op1, op2);
17651       if (! pat)
17652         return 0;
17653       emit_insn (pat);
17654       return 0;
17655
17656     case IX86_BUILTIN_SQRTSS:
17657       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, exp, target);
17658     case IX86_BUILTIN_RSQRTSS:
17659       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, exp, target);
17660     case IX86_BUILTIN_RCPSS:
17661       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, exp, target);
17662
17663     case IX86_BUILTIN_LOADUPS:
17664       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, exp, target, 1);
17665
17666     case IX86_BUILTIN_STOREUPS:
17667       return ix86_expand_store_builtin (CODE_FOR_sse_movups, exp);
17668
17669     case IX86_BUILTIN_LOADHPS:
17670     case IX86_BUILTIN_LOADLPS:
17671     case IX86_BUILTIN_LOADHPD:
17672     case IX86_BUILTIN_LOADLPD:
17673       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
17674                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
17675                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
17676                : CODE_FOR_sse2_loadlpd);
17677       arg0 = CALL_EXPR_ARG (exp, 0);
17678       arg1 = CALL_EXPR_ARG (exp, 1);
17679       op0 = expand_normal (arg0);
17680       op1 = expand_normal (arg1);
17681       tmode = insn_data[icode].operand[0].mode;
17682       mode0 = insn_data[icode].operand[1].mode;
17683       mode1 = insn_data[icode].operand[2].mode;
17684
17685       op0 = force_reg (mode0, op0);
17686       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
17687       if (optimize || target == 0
17688           || GET_MODE (target) != tmode
17689           || !register_operand (target, tmode))
17690         target = gen_reg_rtx (tmode);
17691       pat = GEN_FCN (icode) (target, op0, op1);
17692       if (! pat)
17693         return 0;
17694       emit_insn (pat);
17695       return target;
17696
17697     case IX86_BUILTIN_STOREHPS:
17698     case IX86_BUILTIN_STORELPS:
17699       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
17700                : CODE_FOR_sse_storelps);
17701       arg0 = CALL_EXPR_ARG (exp, 0);
17702       arg1 = CALL_EXPR_ARG (exp, 1);
17703       op0 = expand_normal (arg0);
17704       op1 = expand_normal (arg1);
17705       mode0 = insn_data[icode].operand[0].mode;
17706       mode1 = insn_data[icode].operand[1].mode;
17707
17708       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17709       op1 = force_reg (mode1, op1);
17710
17711       pat = GEN_FCN (icode) (op0, op1);
17712       if (! pat)
17713         return 0;
17714       emit_insn (pat);
17715       return const0_rtx;
17716
17717     case IX86_BUILTIN_MOVNTPS:
17718       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, exp);
17719     case IX86_BUILTIN_MOVNTQ:
17720       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, exp);
17721
17722     case IX86_BUILTIN_LDMXCSR:
17723       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
17724       target = assign_386_stack_local (SImode, SLOT_TEMP);
17725       emit_move_insn (target, op0);
17726       emit_insn (gen_sse_ldmxcsr (target));
17727       return 0;
17728
17729     case IX86_BUILTIN_STMXCSR:
17730       target = assign_386_stack_local (SImode, SLOT_TEMP);
17731       emit_insn (gen_sse_stmxcsr (target));
17732       return copy_to_mode_reg (SImode, target);
17733
17734     case IX86_BUILTIN_SHUFPS:
17735     case IX86_BUILTIN_SHUFPD:
17736       icode = (fcode == IX86_BUILTIN_SHUFPS
17737                ? CODE_FOR_sse_shufps
17738                : CODE_FOR_sse2_shufpd);
17739       arg0 = CALL_EXPR_ARG (exp, 0);
17740       arg1 = CALL_EXPR_ARG (exp, 1);
17741       arg2 = CALL_EXPR_ARG (exp, 2);
17742       op0 = expand_normal (arg0);
17743       op1 = expand_normal (arg1);
17744       op2 = expand_normal (arg2);
17745       tmode = insn_data[icode].operand[0].mode;
17746       mode0 = insn_data[icode].operand[1].mode;
17747       mode1 = insn_data[icode].operand[2].mode;
17748       mode2 = insn_data[icode].operand[3].mode;
17749
17750       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17751         op0 = copy_to_mode_reg (mode0, op0);
17752       if ((optimize && !register_operand (op1, mode1))
17753           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
17754         op1 = copy_to_mode_reg (mode1, op1);
17755       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
17756         {
17757           /* @@@ better error message */
17758           error ("mask must be an immediate");
17759           return gen_reg_rtx (tmode);
17760         }
17761       if (optimize || target == 0
17762           || GET_MODE (target) != tmode
17763           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17764         target = gen_reg_rtx (tmode);
17765       pat = GEN_FCN (icode) (target, op0, op1, op2);
17766       if (! pat)
17767         return 0;
17768       emit_insn (pat);
17769       return target;
17770
17771     case IX86_BUILTIN_PSHUFW:
17772     case IX86_BUILTIN_PSHUFD:
17773     case IX86_BUILTIN_PSHUFHW:
17774     case IX86_BUILTIN_PSHUFLW:
17775       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
17776                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
17777                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
17778                : CODE_FOR_mmx_pshufw);
17779       arg0 = CALL_EXPR_ARG (exp, 0);
17780       arg1 = CALL_EXPR_ARG (exp, 1);
17781       op0 = expand_normal (arg0);
17782       op1 = expand_normal (arg1);
17783       tmode = insn_data[icode].operand[0].mode;
17784       mode1 = insn_data[icode].operand[1].mode;
17785       mode2 = insn_data[icode].operand[2].mode;
17786
17787       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17788         op0 = copy_to_mode_reg (mode1, op0);
17789       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17790         {
17791           /* @@@ better error message */
17792           error ("mask must be an immediate");
17793           return const0_rtx;
17794         }
17795       if (target == 0
17796           || GET_MODE (target) != tmode
17797           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17798         target = gen_reg_rtx (tmode);
17799       pat = GEN_FCN (icode) (target, op0, op1);
17800       if (! pat)
17801         return 0;
17802       emit_insn (pat);
17803       return target;
17804
17805     case IX86_BUILTIN_PSLLWI128:
17806       icode = CODE_FOR_ashlv8hi3;
17807       goto do_pshifti;
17808     case IX86_BUILTIN_PSLLDI128:
17809       icode = CODE_FOR_ashlv4si3;
17810       goto do_pshifti;
17811     case IX86_BUILTIN_PSLLQI128:
17812       icode = CODE_FOR_ashlv2di3;
17813       goto do_pshifti;
17814     case IX86_BUILTIN_PSRAWI128:
17815       icode = CODE_FOR_ashrv8hi3;
17816       goto do_pshifti;
17817     case IX86_BUILTIN_PSRADI128:
17818       icode = CODE_FOR_ashrv4si3;
17819       goto do_pshifti;
17820     case IX86_BUILTIN_PSRLWI128:
17821       icode = CODE_FOR_lshrv8hi3;
17822       goto do_pshifti;
17823     case IX86_BUILTIN_PSRLDI128:
17824       icode = CODE_FOR_lshrv4si3;
17825       goto do_pshifti;
17826     case IX86_BUILTIN_PSRLQI128:
17827       icode = CODE_FOR_lshrv2di3;
17828       goto do_pshifti;
17829     do_pshifti:
17830       arg0 = CALL_EXPR_ARG (exp, 0);
17831       arg1 = CALL_EXPR_ARG (exp, 1);
17832       op0 = expand_normal (arg0);
17833       op1 = expand_normal (arg1);
17834
17835       if (!CONST_INT_P (op1))
17836         {
17837           error ("shift must be an immediate");
17838           return const0_rtx;
17839         }
17840       if (INTVAL (op1) < 0 || INTVAL (op1) > 255)
17841         op1 = GEN_INT (255);
17842
17843       tmode = insn_data[icode].operand[0].mode;
17844       mode1 = insn_data[icode].operand[1].mode;
17845       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17846         op0 = copy_to_reg (op0);
17847
17848       target = gen_reg_rtx (tmode);
17849       pat = GEN_FCN (icode) (target, op0, op1);
17850       if (!pat)
17851         return 0;
17852       emit_insn (pat);
17853       return target;
17854
17855     case IX86_BUILTIN_PSLLW128:
17856       icode = CODE_FOR_ashlv8hi3;
17857       goto do_pshift;
17858     case IX86_BUILTIN_PSLLD128:
17859       icode = CODE_FOR_ashlv4si3;
17860       goto do_pshift;
17861     case IX86_BUILTIN_PSLLQ128:
17862       icode = CODE_FOR_ashlv2di3;
17863       goto do_pshift;
17864     case IX86_BUILTIN_PSRAW128:
17865       icode = CODE_FOR_ashrv8hi3;
17866       goto do_pshift;
17867     case IX86_BUILTIN_PSRAD128:
17868       icode = CODE_FOR_ashrv4si3;
17869       goto do_pshift;
17870     case IX86_BUILTIN_PSRLW128:
17871       icode = CODE_FOR_lshrv8hi3;
17872       goto do_pshift;
17873     case IX86_BUILTIN_PSRLD128:
17874       icode = CODE_FOR_lshrv4si3;
17875       goto do_pshift;
17876     case IX86_BUILTIN_PSRLQ128:
17877       icode = CODE_FOR_lshrv2di3;
17878       goto do_pshift;
17879     do_pshift:
17880       arg0 = CALL_EXPR_ARG (exp, 0);
17881       arg1 = CALL_EXPR_ARG (exp, 1);
17882       op0 = expand_normal (arg0);
17883       op1 = expand_normal (arg1);
17884
17885       tmode = insn_data[icode].operand[0].mode;
17886       mode1 = insn_data[icode].operand[1].mode;
17887
17888       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17889         op0 = copy_to_reg (op0);
17890
17891       op1 = simplify_gen_subreg (TImode, op1, GET_MODE (op1), 0);
17892       if (! (*insn_data[icode].operand[2].predicate) (op1, TImode))
17893         op1 = copy_to_reg (op1);
17894
17895       target = gen_reg_rtx (tmode);
17896       pat = GEN_FCN (icode) (target, op0, op1);
17897       if (!pat)
17898         return 0;
17899       emit_insn (pat);
17900       return target;
17901
17902     case IX86_BUILTIN_PSLLDQI128:
17903     case IX86_BUILTIN_PSRLDQI128:
17904       icode = (fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
17905                : CODE_FOR_sse2_lshrti3);
17906       arg0 = CALL_EXPR_ARG (exp, 0);
17907       arg1 = CALL_EXPR_ARG (exp, 1);
17908       op0 = expand_normal (arg0);
17909       op1 = expand_normal (arg1);
17910       tmode = insn_data[icode].operand[0].mode;
17911       mode1 = insn_data[icode].operand[1].mode;
17912       mode2 = insn_data[icode].operand[2].mode;
17913
17914       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
17915         {
17916           op0 = copy_to_reg (op0);
17917           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
17918         }
17919       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
17920         {
17921           error ("shift must be an immediate");
17922           return const0_rtx;
17923         }
17924       target = gen_reg_rtx (V2DImode);
17925       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0),
17926                              op0, op1);
17927       if (! pat)
17928         return 0;
17929       emit_insn (pat);
17930       return target;
17931
17932     case IX86_BUILTIN_FEMMS:
17933       emit_insn (gen_mmx_femms ());
17934       return NULL_RTX;
17935
17936     case IX86_BUILTIN_PAVGUSB:
17937       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, exp, target);
17938
17939     case IX86_BUILTIN_PF2ID:
17940       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, exp, target, 0);
17941
17942     case IX86_BUILTIN_PFACC:
17943       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, exp, target);
17944
17945     case IX86_BUILTIN_PFADD:
17946      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, exp, target);
17947
17948     case IX86_BUILTIN_PFCMPEQ:
17949       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, exp, target);
17950
17951     case IX86_BUILTIN_PFCMPGE:
17952       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, exp, target);
17953
17954     case IX86_BUILTIN_PFCMPGT:
17955       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, exp, target);
17956
17957     case IX86_BUILTIN_PFMAX:
17958       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, exp, target);
17959
17960     case IX86_BUILTIN_PFMIN:
17961       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, exp, target);
17962
17963     case IX86_BUILTIN_PFMUL:
17964       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, exp, target);
17965
17966     case IX86_BUILTIN_PFRCP:
17967       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, exp, target, 0);
17968
17969     case IX86_BUILTIN_PFRCPIT1:
17970       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, exp, target);
17971
17972     case IX86_BUILTIN_PFRCPIT2:
17973       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, exp, target);
17974
17975     case IX86_BUILTIN_PFRSQIT1:
17976       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, exp, target);
17977
17978     case IX86_BUILTIN_PFRSQRT:
17979       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, exp, target, 0);
17980
17981     case IX86_BUILTIN_PFSUB:
17982       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, exp, target);
17983
17984     case IX86_BUILTIN_PFSUBR:
17985       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, exp, target);
17986
17987     case IX86_BUILTIN_PI2FD:
17988       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, exp, target, 0);
17989
17990     case IX86_BUILTIN_PMULHRW:
17991       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, exp, target);
17992
17993     case IX86_BUILTIN_PF2IW:
17994       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, exp, target, 0);
17995
17996     case IX86_BUILTIN_PFNACC:
17997       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, exp, target);
17998
17999     case IX86_BUILTIN_PFPNACC:
18000       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, exp, target);
18001
18002     case IX86_BUILTIN_PI2FW:
18003       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, exp, target, 0);
18004
18005     case IX86_BUILTIN_PSWAPDSI:
18006       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, exp, target, 0);
18007
18008     case IX86_BUILTIN_PSWAPDSF:
18009       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, exp, target, 0);
18010
18011     case IX86_BUILTIN_SQRTSD:
18012       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, exp, target);
18013     case IX86_BUILTIN_LOADUPD:
18014       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, exp, target, 1);
18015     case IX86_BUILTIN_STOREUPD:
18016       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, exp);
18017
18018     case IX86_BUILTIN_MFENCE:
18019         emit_insn (gen_sse2_mfence ());
18020         return 0;
18021     case IX86_BUILTIN_LFENCE:
18022         emit_insn (gen_sse2_lfence ());
18023         return 0;
18024
18025     case IX86_BUILTIN_CLFLUSH:
18026         arg0 = CALL_EXPR_ARG (exp, 0);
18027         op0 = expand_normal (arg0);
18028         icode = CODE_FOR_sse2_clflush;
18029         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
18030             op0 = copy_to_mode_reg (Pmode, op0);
18031
18032         emit_insn (gen_sse2_clflush (op0));
18033         return 0;
18034
18035     case IX86_BUILTIN_MOVNTPD:
18036       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, exp);
18037     case IX86_BUILTIN_MOVNTDQ:
18038       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, exp);
18039     case IX86_BUILTIN_MOVNTI:
18040       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, exp);
18041
18042     case IX86_BUILTIN_LOADDQU:
18043       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, exp, target, 1);
18044     case IX86_BUILTIN_STOREDQU:
18045       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, exp);
18046
18047     case IX86_BUILTIN_MONITOR:
18048       arg0 = CALL_EXPR_ARG (exp, 0);
18049       arg1 = CALL_EXPR_ARG (exp, 1);
18050       arg2 = CALL_EXPR_ARG (exp, 2);
18051       op0 = expand_normal (arg0);
18052       op1 = expand_normal (arg1);
18053       op2 = expand_normal (arg2);
18054       if (!REG_P (op0))
18055         op0 = copy_to_mode_reg (Pmode, op0);
18056       if (!REG_P (op1))
18057         op1 = copy_to_mode_reg (SImode, op1);
18058       if (!REG_P (op2))
18059         op2 = copy_to_mode_reg (SImode, op2);
18060       if (!TARGET_64BIT)
18061         emit_insn (gen_sse3_monitor (op0, op1, op2));
18062       else
18063         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
18064       return 0;
18065
18066     case IX86_BUILTIN_MWAIT:
18067       arg0 = CALL_EXPR_ARG (exp, 0);
18068       arg1 = CALL_EXPR_ARG (exp, 1);
18069       op0 = expand_normal (arg0);
18070       op1 = expand_normal (arg1);
18071       if (!REG_P (op0))
18072         op0 = copy_to_mode_reg (SImode, op0);
18073       if (!REG_P (op1))
18074         op1 = copy_to_mode_reg (SImode, op1);
18075       emit_insn (gen_sse3_mwait (op0, op1));
18076       return 0;
18077
18078     case IX86_BUILTIN_LDDQU:
18079       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, exp,
18080                                        target, 1);
18081
18082     case IX86_BUILTIN_PALIGNR:
18083     case IX86_BUILTIN_PALIGNR128:
18084       if (fcode == IX86_BUILTIN_PALIGNR)
18085         {
18086           icode = CODE_FOR_ssse3_palignrdi;
18087           mode = DImode;
18088         }
18089       else
18090         {
18091           icode = CODE_FOR_ssse3_palignrti;
18092           mode = V2DImode;
18093         }
18094       arg0 = CALL_EXPR_ARG (exp, 0);
18095       arg1 = CALL_EXPR_ARG (exp, 1);
18096       arg2 = CALL_EXPR_ARG (exp, 2);
18097       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
18098       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
18099       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
18100       tmode = insn_data[icode].operand[0].mode;
18101       mode1 = insn_data[icode].operand[1].mode;
18102       mode2 = insn_data[icode].operand[2].mode;
18103       mode3 = insn_data[icode].operand[3].mode;
18104
18105       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18106         {
18107           op0 = copy_to_reg (op0);
18108           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
18109         }
18110       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18111         {
18112           op1 = copy_to_reg (op1);
18113           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
18114         }
18115       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18116         {
18117           error ("shift must be an immediate");
18118           return const0_rtx;
18119         }
18120       target = gen_reg_rtx (mode);
18121       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
18122                              op0, op1, op2);
18123       if (! pat)
18124         return 0;
18125       emit_insn (pat);
18126       return target;
18127
18128     case IX86_BUILTIN_MOVNTSD:
18129       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv2df, exp);
18130
18131     case IX86_BUILTIN_MOVNTSS:
18132       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv4sf, exp);
18133
18134     case IX86_BUILTIN_INSERTQ:
18135     case IX86_BUILTIN_EXTRQ:
18136       icode = (fcode == IX86_BUILTIN_EXTRQ
18137                ? CODE_FOR_sse4a_extrq
18138                : CODE_FOR_sse4a_insertq);
18139       arg0 = CALL_EXPR_ARG (exp, 0);
18140       arg1 = CALL_EXPR_ARG (exp, 1);
18141       op0 = expand_normal (arg0);
18142       op1 = expand_normal (arg1);
18143       tmode = insn_data[icode].operand[0].mode;
18144       mode1 = insn_data[icode].operand[1].mode;
18145       mode2 = insn_data[icode].operand[2].mode;
18146       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18147         op0 = copy_to_mode_reg (mode1, op0);
18148       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18149         op1 = copy_to_mode_reg (mode2, op1);
18150       if (optimize || target == 0
18151           || GET_MODE (target) != tmode
18152           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18153         target = gen_reg_rtx (tmode);
18154       pat = GEN_FCN (icode) (target, op0, op1);
18155       if (! pat)
18156         return NULL_RTX;
18157       emit_insn (pat);
18158       return target;
18159
18160     case IX86_BUILTIN_EXTRQI:
18161       icode = CODE_FOR_sse4a_extrqi;
18162       arg0 = CALL_EXPR_ARG (exp, 0);
18163       arg1 = CALL_EXPR_ARG (exp, 1);
18164       arg2 = CALL_EXPR_ARG (exp, 2);
18165       op0 = expand_normal (arg0);
18166       op1 = expand_normal (arg1);
18167       op2 = expand_normal (arg2);
18168       tmode = insn_data[icode].operand[0].mode;
18169       mode1 = insn_data[icode].operand[1].mode;
18170       mode2 = insn_data[icode].operand[2].mode;
18171       mode3 = insn_data[icode].operand[3].mode;
18172       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18173         op0 = copy_to_mode_reg (mode1, op0);
18174       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18175         {
18176           error ("index mask must be an immediate");
18177           return gen_reg_rtx (tmode);
18178         }
18179       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18180         {
18181           error ("length mask must be an immediate");
18182           return gen_reg_rtx (tmode);
18183         }
18184       if (optimize || target == 0
18185           || GET_MODE (target) != tmode
18186           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18187         target = gen_reg_rtx (tmode);
18188       pat = GEN_FCN (icode) (target, op0, op1, op2);
18189       if (! pat)
18190         return NULL_RTX;
18191       emit_insn (pat);
18192       return target;
18193
18194     case IX86_BUILTIN_INSERTQI:
18195       icode = CODE_FOR_sse4a_insertqi;
18196       arg0 = CALL_EXPR_ARG (exp, 0);
18197       arg1 = CALL_EXPR_ARG (exp, 1);
18198       arg2 = CALL_EXPR_ARG (exp, 2);
18199       arg3 = CALL_EXPR_ARG (exp, 3);
18200       op0 = expand_normal (arg0);
18201       op1 = expand_normal (arg1);
18202       op2 = expand_normal (arg2);
18203       op3 = expand_normal (arg3);
18204       tmode = insn_data[icode].operand[0].mode;
18205       mode1 = insn_data[icode].operand[1].mode;
18206       mode2 = insn_data[icode].operand[2].mode;
18207       mode3 = insn_data[icode].operand[3].mode;
18208       mode4 = insn_data[icode].operand[4].mode;
18209
18210       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18211         op0 = copy_to_mode_reg (mode1, op0);
18212
18213       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18214         op1 = copy_to_mode_reg (mode2, op1);
18215
18216       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18217         {
18218           error ("index mask must be an immediate");
18219           return gen_reg_rtx (tmode);
18220         }
18221       if (! (*insn_data[icode].operand[4].predicate) (op3, mode4))
18222         {
18223           error ("length mask must be an immediate");
18224           return gen_reg_rtx (tmode);
18225         }
18226       if (optimize || target == 0
18227           || GET_MODE (target) != tmode
18228           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18229         target = gen_reg_rtx (tmode);
18230       pat = GEN_FCN (icode) (target, op0, op1, op2, op3);
18231       if (! pat)
18232         return NULL_RTX;
18233       emit_insn (pat);
18234       return target;
18235
18236     case IX86_BUILTIN_VEC_INIT_V2SI:
18237     case IX86_BUILTIN_VEC_INIT_V4HI:
18238     case IX86_BUILTIN_VEC_INIT_V8QI:
18239       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
18240
18241     case IX86_BUILTIN_VEC_EXT_V2DF:
18242     case IX86_BUILTIN_VEC_EXT_V2DI:
18243     case IX86_BUILTIN_VEC_EXT_V4SF:
18244     case IX86_BUILTIN_VEC_EXT_V4SI:
18245     case IX86_BUILTIN_VEC_EXT_V8HI:
18246     case IX86_BUILTIN_VEC_EXT_V2SI:
18247     case IX86_BUILTIN_VEC_EXT_V4HI:
18248       return ix86_expand_vec_ext_builtin (exp, target);
18249
18250     case IX86_BUILTIN_VEC_SET_V8HI:
18251     case IX86_BUILTIN_VEC_SET_V4HI:
18252       return ix86_expand_vec_set_builtin (exp);
18253
18254     default:
18255       break;
18256     }
18257
18258   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
18259     if (d->code == fcode)
18260       {
18261         /* Compares are treated specially.  */
18262         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
18263             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
18264             || d->icode == CODE_FOR_sse2_maskcmpv2df3
18265             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
18266           return ix86_expand_sse_compare (d, exp, target);
18267
18268         return ix86_expand_binop_builtin (d->icode, exp, target);
18269       }
18270
18271   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
18272     if (d->code == fcode)
18273       return ix86_expand_unop_builtin (d->icode, exp, target, 0);
18274
18275   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
18276     if (d->code == fcode)
18277       return ix86_expand_sse_comi (d, exp, target);
18278
18279   gcc_unreachable ();
18280 }
18281
18282 /* Returns a function decl for a vectorized version of the builtin function
18283    with builtin function code FN and the result vector type TYPE, or NULL_TREE
18284    if it is not available.  */
18285
18286 static tree
18287 ix86_builtin_vectorized_function (enum built_in_function fn, tree type_out,
18288                                   tree type_in)
18289 {
18290   enum machine_mode in_mode, out_mode;
18291   int in_n, out_n;
18292
18293   if (TREE_CODE (type_out) != VECTOR_TYPE
18294       || TREE_CODE (type_in) != VECTOR_TYPE)
18295     return NULL_TREE;
18296
18297   out_mode = TYPE_MODE (TREE_TYPE (type_out));
18298   out_n = TYPE_VECTOR_SUBPARTS (type_out);
18299   in_mode = TYPE_MODE (TREE_TYPE (type_in));
18300   in_n = TYPE_VECTOR_SUBPARTS (type_in);
18301
18302   switch (fn)
18303     {
18304     case BUILT_IN_SQRT:
18305       if (out_mode == DFmode && out_n == 2
18306           && in_mode == DFmode && in_n == 2)
18307         return ix86_builtins[IX86_BUILTIN_SQRTPD];
18308       return NULL_TREE;
18309
18310     case BUILT_IN_SQRTF:
18311       if (out_mode == SFmode && out_n == 4
18312           && in_mode == SFmode && in_n == 4)
18313         return ix86_builtins[IX86_BUILTIN_SQRTPS];
18314       return NULL_TREE;
18315
18316     case BUILT_IN_LRINTF:
18317       if (out_mode == SImode && out_n == 4
18318           && in_mode == SFmode && in_n == 4)
18319         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
18320       return NULL_TREE;
18321
18322     default:
18323       ;
18324     }
18325
18326   return NULL_TREE;
18327 }
18328
18329 /* Returns a decl of a function that implements conversion of the
18330    input vector of type TYPE, or NULL_TREE if it is not available.  */
18331
18332 static tree
18333 ix86_builtin_conversion (enum tree_code code, tree type)
18334 {
18335   if (TREE_CODE (type) != VECTOR_TYPE)
18336     return NULL_TREE;
18337   
18338   switch (code)
18339     {
18340     case FLOAT_EXPR:
18341       switch (TYPE_MODE (type))
18342         {
18343         case V4SImode:
18344           return ix86_builtins[IX86_BUILTIN_CVTDQ2PS];
18345         default:
18346           return NULL_TREE;
18347         }
18348
18349     case FIX_TRUNC_EXPR:
18350       switch (TYPE_MODE (type))
18351         {
18352         case V4SFmode:
18353           return ix86_builtins[IX86_BUILTIN_CVTTPS2DQ];
18354         default:
18355           return NULL_TREE;
18356         }
18357     default:
18358       return NULL_TREE;
18359
18360     }
18361 }
18362
18363 /* Store OPERAND to the memory after reload is completed.  This means
18364    that we can't easily use assign_stack_local.  */
18365 rtx
18366 ix86_force_to_memory (enum machine_mode mode, rtx operand)
18367 {
18368   rtx result;
18369
18370   gcc_assert (reload_completed);
18371   if (TARGET_RED_ZONE)
18372     {
18373       result = gen_rtx_MEM (mode,
18374                             gen_rtx_PLUS (Pmode,
18375                                           stack_pointer_rtx,
18376                                           GEN_INT (-RED_ZONE_SIZE)));
18377       emit_move_insn (result, operand);
18378     }
18379   else if (!TARGET_RED_ZONE && TARGET_64BIT)
18380     {
18381       switch (mode)
18382         {
18383         case HImode:
18384         case SImode:
18385           operand = gen_lowpart (DImode, operand);
18386           /* FALLTHRU */
18387         case DImode:
18388           emit_insn (
18389                       gen_rtx_SET (VOIDmode,
18390                                    gen_rtx_MEM (DImode,
18391                                                 gen_rtx_PRE_DEC (DImode,
18392                                                         stack_pointer_rtx)),
18393                                    operand));
18394           break;
18395         default:
18396           gcc_unreachable ();
18397         }
18398       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18399     }
18400   else
18401     {
18402       switch (mode)
18403         {
18404         case DImode:
18405           {
18406             rtx operands[2];
18407             split_di (&operand, 1, operands, operands + 1);
18408             emit_insn (
18409                         gen_rtx_SET (VOIDmode,
18410                                      gen_rtx_MEM (SImode,
18411                                                   gen_rtx_PRE_DEC (Pmode,
18412                                                         stack_pointer_rtx)),
18413                                      operands[1]));
18414             emit_insn (
18415                         gen_rtx_SET (VOIDmode,
18416                                      gen_rtx_MEM (SImode,
18417                                                   gen_rtx_PRE_DEC (Pmode,
18418                                                         stack_pointer_rtx)),
18419                                      operands[0]));
18420           }
18421           break;
18422         case HImode:
18423           /* Store HImodes as SImodes.  */
18424           operand = gen_lowpart (SImode, operand);
18425           /* FALLTHRU */
18426         case SImode:
18427           emit_insn (
18428                       gen_rtx_SET (VOIDmode,
18429                                    gen_rtx_MEM (GET_MODE (operand),
18430                                                 gen_rtx_PRE_DEC (SImode,
18431                                                         stack_pointer_rtx)),
18432                                    operand));
18433           break;
18434         default:
18435           gcc_unreachable ();
18436         }
18437       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18438     }
18439   return result;
18440 }
18441
18442 /* Free operand from the memory.  */
18443 void
18444 ix86_free_from_memory (enum machine_mode mode)
18445 {
18446   if (!TARGET_RED_ZONE)
18447     {
18448       int size;
18449
18450       if (mode == DImode || TARGET_64BIT)
18451         size = 8;
18452       else
18453         size = 4;
18454       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
18455          to pop or add instruction if registers are available.  */
18456       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
18457                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
18458                                             GEN_INT (size))));
18459     }
18460 }
18461
18462 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
18463    QImode must go into class Q_REGS.
18464    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
18465    movdf to do mem-to-mem moves through integer regs.  */
18466 enum reg_class
18467 ix86_preferred_reload_class (rtx x, enum reg_class class)
18468 {
18469   enum machine_mode mode = GET_MODE (x);
18470
18471   /* We're only allowed to return a subclass of CLASS.  Many of the
18472      following checks fail for NO_REGS, so eliminate that early.  */
18473   if (class == NO_REGS)
18474     return NO_REGS;
18475
18476   /* All classes can load zeros.  */
18477   if (x == CONST0_RTX (mode))
18478     return class;
18479
18480   /* Force constants into memory if we are loading a (nonzero) constant into
18481      an MMX or SSE register.  This is because there are no MMX/SSE instructions
18482      to load from a constant.  */
18483   if (CONSTANT_P (x)
18484       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
18485     return NO_REGS;
18486
18487   /* Prefer SSE regs only, if we can use them for math.  */
18488   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
18489     return SSE_CLASS_P (class) ? class : NO_REGS;
18490
18491   /* Floating-point constants need more complex checks.  */
18492   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
18493     {
18494       /* General regs can load everything.  */
18495       if (reg_class_subset_p (class, GENERAL_REGS))
18496         return class;
18497
18498       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
18499          zero above.  We only want to wind up preferring 80387 registers if
18500          we plan on doing computation with them.  */
18501       if (TARGET_80387
18502           && standard_80387_constant_p (x))
18503         {
18504           /* Limit class to non-sse.  */
18505           if (class == FLOAT_SSE_REGS)
18506             return FLOAT_REGS;
18507           if (class == FP_TOP_SSE_REGS)
18508             return FP_TOP_REG;
18509           if (class == FP_SECOND_SSE_REGS)
18510             return FP_SECOND_REG;
18511           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
18512             return class;
18513         }
18514
18515       return NO_REGS;
18516     }
18517
18518   /* Generally when we see PLUS here, it's the function invariant
18519      (plus soft-fp const_int).  Which can only be computed into general
18520      regs.  */
18521   if (GET_CODE (x) == PLUS)
18522     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
18523
18524   /* QImode constants are easy to load, but non-constant QImode data
18525      must go into Q_REGS.  */
18526   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
18527     {
18528       if (reg_class_subset_p (class, Q_REGS))
18529         return class;
18530       if (reg_class_subset_p (Q_REGS, class))
18531         return Q_REGS;
18532       return NO_REGS;
18533     }
18534
18535   return class;
18536 }
18537
18538 /* Discourage putting floating-point values in SSE registers unless
18539    SSE math is being used, and likewise for the 387 registers.  */
18540 enum reg_class
18541 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
18542 {
18543   enum machine_mode mode = GET_MODE (x);
18544
18545   /* Restrict the output reload class to the register bank that we are doing
18546      math on.  If we would like not to return a subset of CLASS, reject this
18547      alternative: if reload cannot do this, it will still use its choice.  */
18548   mode = GET_MODE (x);
18549   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
18550     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
18551
18552   if (TARGET_80387 && SCALAR_FLOAT_MODE_P (mode))
18553     {
18554       if (class == FP_TOP_SSE_REGS)
18555         return FP_TOP_REG;
18556       else if (class == FP_SECOND_SSE_REGS)
18557         return FP_SECOND_REG;
18558       else
18559         return FLOAT_CLASS_P (class) ? class : NO_REGS;
18560     }
18561
18562   return class;
18563 }
18564
18565 /* If we are copying between general and FP registers, we need a memory
18566    location. The same is true for SSE and MMX registers.
18567
18568    The macro can't work reliably when one of the CLASSES is class containing
18569    registers from multiple units (SSE, MMX, integer).  We avoid this by never
18570    combining those units in single alternative in the machine description.
18571    Ensure that this constraint holds to avoid unexpected surprises.
18572
18573    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
18574    enforce these sanity checks.  */
18575
18576 int
18577 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
18578                               enum machine_mode mode, int strict)
18579 {
18580   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
18581       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
18582       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
18583       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
18584       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
18585       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
18586     {
18587       gcc_assert (!strict);
18588       return true;
18589     }
18590
18591   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
18592     return true;
18593
18594   /* ??? This is a lie.  We do have moves between mmx/general, and for
18595      mmx/sse2.  But by saying we need secondary memory we discourage the
18596      register allocator from using the mmx registers unless needed.  */
18597   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
18598     return true;
18599
18600   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
18601     {
18602       /* SSE1 doesn't have any direct moves from other classes.  */
18603       if (!TARGET_SSE2)
18604         return true;
18605
18606       /* If the target says that inter-unit moves are more expensive
18607          than moving through memory, then don't generate them.  */
18608       if (!TARGET_INTER_UNIT_MOVES)
18609         return true;
18610
18611       /* Between SSE and general, we have moves no larger than word size.  */
18612       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
18613         return true;
18614     }
18615
18616   return false;
18617 }
18618
18619 /* Return true if the registers in CLASS cannot represent the change from
18620    modes FROM to TO.  */
18621
18622 bool
18623 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
18624                                enum reg_class class)
18625 {
18626   if (from == to)
18627     return false;
18628
18629   /* x87 registers can't do subreg at all, as all values are reformatted
18630      to extended precision.  */
18631   if (MAYBE_FLOAT_CLASS_P (class))
18632     return true;
18633
18634   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
18635     {
18636       /* Vector registers do not support QI or HImode loads.  If we don't
18637          disallow a change to these modes, reload will assume it's ok to
18638          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
18639          the vec_dupv4hi pattern.  */
18640       if (GET_MODE_SIZE (from) < 4)
18641         return true;
18642
18643       /* Vector registers do not support subreg with nonzero offsets, which
18644          are otherwise valid for integer registers.  Since we can't see
18645          whether we have a nonzero offset from here, prohibit all
18646          nonparadoxical subregs changing size.  */
18647       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
18648         return true;
18649     }
18650
18651   return false;
18652 }
18653
18654 /* Return the cost of moving data from a register in class CLASS1 to
18655    one in class CLASS2.
18656
18657    It is not required that the cost always equal 2 when FROM is the same as TO;
18658    on some machines it is expensive to move between registers if they are not
18659    general registers.  */
18660
18661 int
18662 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
18663                          enum reg_class class2)
18664 {
18665   /* In case we require secondary memory, compute cost of the store followed
18666      by load.  In order to avoid bad register allocation choices, we need
18667      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
18668
18669   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
18670     {
18671       int cost = 1;
18672
18673       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
18674                    MEMORY_MOVE_COST (mode, class1, 1));
18675       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
18676                    MEMORY_MOVE_COST (mode, class2, 1));
18677
18678       /* In case of copying from general_purpose_register we may emit multiple
18679          stores followed by single load causing memory size mismatch stall.
18680          Count this as arbitrarily high cost of 20.  */
18681       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
18682         cost += 20;
18683
18684       /* In the case of FP/MMX moves, the registers actually overlap, and we
18685          have to switch modes in order to treat them differently.  */
18686       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
18687           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
18688         cost += 20;
18689
18690       return cost;
18691     }
18692
18693   /* Moves between SSE/MMX and integer unit are expensive.  */
18694   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
18695       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
18696     return ix86_cost->mmxsse_to_integer;
18697   if (MAYBE_FLOAT_CLASS_P (class1))
18698     return ix86_cost->fp_move;
18699   if (MAYBE_SSE_CLASS_P (class1))
18700     return ix86_cost->sse_move;
18701   if (MAYBE_MMX_CLASS_P (class1))
18702     return ix86_cost->mmx_move;
18703   return 2;
18704 }
18705
18706 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
18707
18708 bool
18709 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
18710 {
18711   /* Flags and only flags can only hold CCmode values.  */
18712   if (CC_REGNO_P (regno))
18713     return GET_MODE_CLASS (mode) == MODE_CC;
18714   if (GET_MODE_CLASS (mode) == MODE_CC
18715       || GET_MODE_CLASS (mode) == MODE_RANDOM
18716       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
18717     return 0;
18718   if (FP_REGNO_P (regno))
18719     return VALID_FP_MODE_P (mode);
18720   if (SSE_REGNO_P (regno))
18721     {
18722       /* We implement the move patterns for all vector modes into and
18723          out of SSE registers, even when no operation instructions
18724          are available.  */
18725       return (VALID_SSE_REG_MODE (mode)
18726               || VALID_SSE2_REG_MODE (mode)
18727               || VALID_MMX_REG_MODE (mode)
18728               || VALID_MMX_REG_MODE_3DNOW (mode));
18729     }
18730   if (MMX_REGNO_P (regno))
18731     {
18732       /* We implement the move patterns for 3DNOW modes even in MMX mode,
18733          so if the register is available at all, then we can move data of
18734          the given mode into or out of it.  */
18735       return (VALID_MMX_REG_MODE (mode)
18736               || VALID_MMX_REG_MODE_3DNOW (mode));
18737     }
18738
18739   if (mode == QImode)
18740     {
18741       /* Take care for QImode values - they can be in non-QI regs,
18742          but then they do cause partial register stalls.  */
18743       if (regno < 4 || TARGET_64BIT)
18744         return 1;
18745       if (!TARGET_PARTIAL_REG_STALL)
18746         return 1;
18747       return reload_in_progress || reload_completed;
18748     }
18749   /* We handle both integer and floats in the general purpose registers.  */
18750   else if (VALID_INT_MODE_P (mode))
18751     return 1;
18752   else if (VALID_FP_MODE_P (mode))
18753     return 1;
18754   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
18755      on to use that value in smaller contexts, this can easily force a
18756      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
18757      supporting DImode, allow it.  */
18758   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
18759     return 1;
18760
18761   return 0;
18762 }
18763
18764 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
18765    tieable integer mode.  */
18766
18767 static bool
18768 ix86_tieable_integer_mode_p (enum machine_mode mode)
18769 {
18770   switch (mode)
18771     {
18772     case HImode:
18773     case SImode:
18774       return true;
18775
18776     case QImode:
18777       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
18778
18779     case DImode:
18780       return TARGET_64BIT;
18781
18782     default:
18783       return false;
18784     }
18785 }
18786
18787 /* Return true if MODE1 is accessible in a register that can hold MODE2
18788    without copying.  That is, all register classes that can hold MODE2
18789    can also hold MODE1.  */
18790
18791 bool
18792 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
18793 {
18794   if (mode1 == mode2)
18795     return true;
18796
18797   if (ix86_tieable_integer_mode_p (mode1)
18798       && ix86_tieable_integer_mode_p (mode2))
18799     return true;
18800
18801   /* MODE2 being XFmode implies fp stack or general regs, which means we
18802      can tie any smaller floating point modes to it.  Note that we do not
18803      tie this with TFmode.  */
18804   if (mode2 == XFmode)
18805     return mode1 == SFmode || mode1 == DFmode;
18806
18807   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
18808      that we can tie it with SFmode.  */
18809   if (mode2 == DFmode)
18810     return mode1 == SFmode;
18811
18812   /* If MODE2 is only appropriate for an SSE register, then tie with
18813      any other mode acceptable to SSE registers.  */
18814   if (GET_MODE_SIZE (mode2) == 16
18815       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
18816     return (GET_MODE_SIZE (mode1) == 16
18817             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
18818
18819   /* If MODE2 is appropriate for an MMX register, then tie
18820      with any other mode acceptable to MMX registers.  */
18821   if (GET_MODE_SIZE (mode2) == 8
18822       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
18823     return (GET_MODE_SIZE (mode1) == 8
18824             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
18825
18826   return false;
18827 }
18828
18829 /* Return the cost of moving data of mode M between a
18830    register and memory.  A value of 2 is the default; this cost is
18831    relative to those in `REGISTER_MOVE_COST'.
18832
18833    If moving between registers and memory is more expensive than
18834    between two registers, you should define this macro to express the
18835    relative cost.
18836
18837    Model also increased moving costs of QImode registers in non
18838    Q_REGS classes.
18839  */
18840 int
18841 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
18842 {
18843   if (FLOAT_CLASS_P (class))
18844     {
18845       int index;
18846       switch (mode)
18847         {
18848           case SFmode:
18849             index = 0;
18850             break;
18851           case DFmode:
18852             index = 1;
18853             break;
18854           case XFmode:
18855             index = 2;
18856             break;
18857           default:
18858             return 100;
18859         }
18860       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
18861     }
18862   if (SSE_CLASS_P (class))
18863     {
18864       int index;
18865       switch (GET_MODE_SIZE (mode))
18866         {
18867           case 4:
18868             index = 0;
18869             break;
18870           case 8:
18871             index = 1;
18872             break;
18873           case 16:
18874             index = 2;
18875             break;
18876           default:
18877             return 100;
18878         }
18879       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
18880     }
18881   if (MMX_CLASS_P (class))
18882     {
18883       int index;
18884       switch (GET_MODE_SIZE (mode))
18885         {
18886           case 4:
18887             index = 0;
18888             break;
18889           case 8:
18890             index = 1;
18891             break;
18892           default:
18893             return 100;
18894         }
18895       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
18896     }
18897   switch (GET_MODE_SIZE (mode))
18898     {
18899       case 1:
18900         if (in)
18901           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
18902                   : ix86_cost->movzbl_load);
18903         else
18904           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
18905                   : ix86_cost->int_store[0] + 4);
18906         break;
18907       case 2:
18908         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
18909       default:
18910         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
18911         if (mode == TFmode)
18912           mode = XFmode;
18913         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
18914                 * (((int) GET_MODE_SIZE (mode)
18915                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
18916     }
18917 }
18918
18919 /* Compute a (partial) cost for rtx X.  Return true if the complete
18920    cost has been computed, and false if subexpressions should be
18921    scanned.  In either case, *TOTAL contains the cost result.  */
18922
18923 static bool
18924 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
18925 {
18926   enum machine_mode mode = GET_MODE (x);
18927
18928   switch (code)
18929     {
18930     case CONST_INT:
18931     case CONST:
18932     case LABEL_REF:
18933     case SYMBOL_REF:
18934       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
18935         *total = 3;
18936       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
18937         *total = 2;
18938       else if (flag_pic && SYMBOLIC_CONST (x)
18939                && (!TARGET_64BIT
18940                    || (!GET_CODE (x) != LABEL_REF
18941                        && (GET_CODE (x) != SYMBOL_REF
18942                            || !SYMBOL_REF_LOCAL_P (x)))))
18943         *total = 1;
18944       else
18945         *total = 0;
18946       return true;
18947
18948     case CONST_DOUBLE:
18949       if (mode == VOIDmode)
18950         *total = 0;
18951       else
18952         switch (standard_80387_constant_p (x))
18953           {
18954           case 1: /* 0.0 */
18955             *total = 1;
18956             break;
18957           default: /* Other constants */
18958             *total = 2;
18959             break;
18960           case 0:
18961           case -1:
18962             /* Start with (MEM (SYMBOL_REF)), since that's where
18963                it'll probably end up.  Add a penalty for size.  */
18964             *total = (COSTS_N_INSNS (1)
18965                       + (flag_pic != 0 && !TARGET_64BIT)
18966                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
18967             break;
18968           }
18969       return true;
18970
18971     case ZERO_EXTEND:
18972       /* The zero extensions is often completely free on x86_64, so make
18973          it as cheap as possible.  */
18974       if (TARGET_64BIT && mode == DImode
18975           && GET_MODE (XEXP (x, 0)) == SImode)
18976         *total = 1;
18977       else if (TARGET_ZERO_EXTEND_WITH_AND)
18978         *total = ix86_cost->add;
18979       else
18980         *total = ix86_cost->movzx;
18981       return false;
18982
18983     case SIGN_EXTEND:
18984       *total = ix86_cost->movsx;
18985       return false;
18986
18987     case ASHIFT:
18988       if (CONST_INT_P (XEXP (x, 1))
18989           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
18990         {
18991           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
18992           if (value == 1)
18993             {
18994               *total = ix86_cost->add;
18995               return false;
18996             }
18997           if ((value == 2 || value == 3)
18998               && ix86_cost->lea <= ix86_cost->shift_const)
18999             {
19000               *total = ix86_cost->lea;
19001               return false;
19002             }
19003         }
19004       /* FALLTHRU */
19005
19006     case ROTATE:
19007     case ASHIFTRT:
19008     case LSHIFTRT:
19009     case ROTATERT:
19010       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
19011         {
19012           if (CONST_INT_P (XEXP (x, 1)))
19013             {
19014               if (INTVAL (XEXP (x, 1)) > 32)
19015                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
19016               else
19017                 *total = ix86_cost->shift_const * 2;
19018             }
19019           else
19020             {
19021               if (GET_CODE (XEXP (x, 1)) == AND)
19022                 *total = ix86_cost->shift_var * 2;
19023               else
19024                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
19025             }
19026         }
19027       else
19028         {
19029           if (CONST_INT_P (XEXP (x, 1)))
19030             *total = ix86_cost->shift_const;
19031           else
19032             *total = ix86_cost->shift_var;
19033         }
19034       return false;
19035
19036     case MULT:
19037       if (FLOAT_MODE_P (mode))
19038         {
19039           *total = ix86_cost->fmul;
19040           return false;
19041         }
19042       else
19043         {
19044           rtx op0 = XEXP (x, 0);
19045           rtx op1 = XEXP (x, 1);
19046           int nbits;
19047           if (CONST_INT_P (XEXP (x, 1)))
19048             {
19049               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
19050               for (nbits = 0; value != 0; value &= value - 1)
19051                 nbits++;
19052             }
19053           else
19054             /* This is arbitrary.  */
19055             nbits = 7;
19056
19057           /* Compute costs correctly for widening multiplication.  */
19058           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
19059               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
19060                  == GET_MODE_SIZE (mode))
19061             {
19062               int is_mulwiden = 0;
19063               enum machine_mode inner_mode = GET_MODE (op0);
19064
19065               if (GET_CODE (op0) == GET_CODE (op1))
19066                 is_mulwiden = 1, op1 = XEXP (op1, 0);
19067               else if (CONST_INT_P (op1))
19068                 {
19069                   if (GET_CODE (op0) == SIGN_EXTEND)
19070                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
19071                                   == INTVAL (op1);
19072                   else
19073                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
19074                 }
19075
19076               if (is_mulwiden)
19077                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
19078             }
19079
19080           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
19081                     + nbits * ix86_cost->mult_bit
19082                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
19083
19084           return true;
19085         }
19086
19087     case DIV:
19088     case UDIV:
19089     case MOD:
19090     case UMOD:
19091       if (FLOAT_MODE_P (mode))
19092         *total = ix86_cost->fdiv;
19093       else
19094         *total = ix86_cost->divide[MODE_INDEX (mode)];
19095       return false;
19096
19097     case PLUS:
19098       if (FLOAT_MODE_P (mode))
19099         *total = ix86_cost->fadd;
19100       else if (GET_MODE_CLASS (mode) == MODE_INT
19101                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
19102         {
19103           if (GET_CODE (XEXP (x, 0)) == PLUS
19104               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
19105               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
19106               && CONSTANT_P (XEXP (x, 1)))
19107             {
19108               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
19109               if (val == 2 || val == 4 || val == 8)
19110                 {
19111                   *total = ix86_cost->lea;
19112                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19113                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
19114                                       outer_code);
19115                   *total += rtx_cost (XEXP (x, 1), outer_code);
19116                   return true;
19117                 }
19118             }
19119           else if (GET_CODE (XEXP (x, 0)) == MULT
19120                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
19121             {
19122               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
19123               if (val == 2 || val == 4 || val == 8)
19124                 {
19125                   *total = ix86_cost->lea;
19126                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19127                   *total += rtx_cost (XEXP (x, 1), outer_code);
19128                   return true;
19129                 }
19130             }
19131           else if (GET_CODE (XEXP (x, 0)) == PLUS)
19132             {
19133               *total = ix86_cost->lea;
19134               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19135               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19136               *total += rtx_cost (XEXP (x, 1), outer_code);
19137               return true;
19138             }
19139         }
19140       /* FALLTHRU */
19141
19142     case MINUS:
19143       if (FLOAT_MODE_P (mode))
19144         {
19145           *total = ix86_cost->fadd;
19146           return false;
19147         }
19148       /* FALLTHRU */
19149
19150     case AND:
19151     case IOR:
19152     case XOR:
19153       if (!TARGET_64BIT && mode == DImode)
19154         {
19155           *total = (ix86_cost->add * 2
19156                     + (rtx_cost (XEXP (x, 0), outer_code)
19157                        << (GET_MODE (XEXP (x, 0)) != DImode))
19158                     + (rtx_cost (XEXP (x, 1), outer_code)
19159                        << (GET_MODE (XEXP (x, 1)) != DImode)));
19160           return true;
19161         }
19162       /* FALLTHRU */
19163
19164     case NEG:
19165       if (FLOAT_MODE_P (mode))
19166         {
19167           *total = ix86_cost->fchs;
19168           return false;
19169         }
19170       /* FALLTHRU */
19171
19172     case NOT:
19173       if (!TARGET_64BIT && mode == DImode)
19174         *total = ix86_cost->add * 2;
19175       else
19176         *total = ix86_cost->add;
19177       return false;
19178
19179     case COMPARE:
19180       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
19181           && XEXP (XEXP (x, 0), 1) == const1_rtx
19182           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
19183           && XEXP (x, 1) == const0_rtx)
19184         {
19185           /* This kind of construct is implemented using test[bwl].
19186              Treat it as if we had an AND.  */
19187           *total = (ix86_cost->add
19188                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
19189                     + rtx_cost (const1_rtx, outer_code));
19190           return true;
19191         }
19192       return false;
19193
19194     case FLOAT_EXTEND:
19195       if (!TARGET_SSE_MATH
19196           || mode == XFmode
19197           || (mode == DFmode && !TARGET_SSE2))
19198         *total = 0;
19199       return false;
19200
19201     case ABS:
19202       if (FLOAT_MODE_P (mode))
19203         *total = ix86_cost->fabs;
19204       return false;
19205
19206     case SQRT:
19207       if (FLOAT_MODE_P (mode))
19208         *total = ix86_cost->fsqrt;
19209       return false;
19210
19211     case UNSPEC:
19212       if (XINT (x, 1) == UNSPEC_TP)
19213         *total = 0;
19214       return false;
19215
19216     default:
19217       return false;
19218     }
19219 }
19220
19221 #if TARGET_MACHO
19222
19223 static int current_machopic_label_num;
19224
19225 /* Given a symbol name and its associated stub, write out the
19226    definition of the stub.  */
19227
19228 void
19229 machopic_output_stub (FILE *file, const char *symb, const char *stub)
19230 {
19231   unsigned int length;
19232   char *binder_name, *symbol_name, lazy_ptr_name[32];
19233   int label = ++current_machopic_label_num;
19234
19235   /* For 64-bit we shouldn't get here.  */
19236   gcc_assert (!TARGET_64BIT);
19237
19238   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
19239   symb = (*targetm.strip_name_encoding) (symb);
19240
19241   length = strlen (stub);
19242   binder_name = alloca (length + 32);
19243   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
19244
19245   length = strlen (symb);
19246   symbol_name = alloca (length + 32);
19247   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
19248
19249   sprintf (lazy_ptr_name, "L%d$lz", label);
19250
19251   if (MACHOPIC_PURE)
19252     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
19253   else
19254     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
19255
19256   fprintf (file, "%s:\n", stub);
19257   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19258
19259   if (MACHOPIC_PURE)
19260     {
19261       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
19262       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
19263       fprintf (file, "\tjmp\t*%%edx\n");
19264     }
19265   else
19266     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
19267
19268   fprintf (file, "%s:\n", binder_name);
19269
19270   if (MACHOPIC_PURE)
19271     {
19272       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
19273       fprintf (file, "\tpushl\t%%eax\n");
19274     }
19275   else
19276     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
19277
19278   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
19279
19280   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
19281   fprintf (file, "%s:\n", lazy_ptr_name);
19282   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19283   fprintf (file, "\t.long %s\n", binder_name);
19284 }
19285
19286 void
19287 darwin_x86_file_end (void)
19288 {
19289   darwin_file_end ();
19290   ix86_file_end ();
19291 }
19292 #endif /* TARGET_MACHO */
19293
19294 /* Order the registers for register allocator.  */
19295
19296 void
19297 x86_order_regs_for_local_alloc (void)
19298 {
19299    int pos = 0;
19300    int i;
19301
19302    /* First allocate the local general purpose registers.  */
19303    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19304      if (GENERAL_REGNO_P (i) && call_used_regs[i])
19305         reg_alloc_order [pos++] = i;
19306
19307    /* Global general purpose registers.  */
19308    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19309      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
19310         reg_alloc_order [pos++] = i;
19311
19312    /* x87 registers come first in case we are doing FP math
19313       using them.  */
19314    if (!TARGET_SSE_MATH)
19315      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19316        reg_alloc_order [pos++] = i;
19317
19318    /* SSE registers.  */
19319    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
19320      reg_alloc_order [pos++] = i;
19321    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
19322      reg_alloc_order [pos++] = i;
19323
19324    /* x87 registers.  */
19325    if (TARGET_SSE_MATH)
19326      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19327        reg_alloc_order [pos++] = i;
19328
19329    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
19330      reg_alloc_order [pos++] = i;
19331
19332    /* Initialize the rest of array as we do not allocate some registers
19333       at all.  */
19334    while (pos < FIRST_PSEUDO_REGISTER)
19335      reg_alloc_order [pos++] = 0;
19336 }
19337
19338 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
19339    struct attribute_spec.handler.  */
19340 static tree
19341 ix86_handle_struct_attribute (tree *node, tree name,
19342                               tree args ATTRIBUTE_UNUSED,
19343                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
19344 {
19345   tree *type = NULL;
19346   if (DECL_P (*node))
19347     {
19348       if (TREE_CODE (*node) == TYPE_DECL)
19349         type = &TREE_TYPE (*node);
19350     }
19351   else
19352     type = node;
19353
19354   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
19355                  || TREE_CODE (*type) == UNION_TYPE)))
19356     {
19357       warning (OPT_Wattributes, "%qs attribute ignored",
19358                IDENTIFIER_POINTER (name));
19359       *no_add_attrs = true;
19360     }
19361
19362   else if ((is_attribute_p ("ms_struct", name)
19363             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
19364            || ((is_attribute_p ("gcc_struct", name)
19365                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
19366     {
19367       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
19368                IDENTIFIER_POINTER (name));
19369       *no_add_attrs = true;
19370     }
19371
19372   return NULL_TREE;
19373 }
19374
19375 static bool
19376 ix86_ms_bitfield_layout_p (tree record_type)
19377 {
19378   return (TARGET_MS_BITFIELD_LAYOUT &&
19379           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
19380     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
19381 }
19382
19383 /* Returns an expression indicating where the this parameter is
19384    located on entry to the FUNCTION.  */
19385
19386 static rtx
19387 x86_this_parameter (tree function)
19388 {
19389   tree type = TREE_TYPE (function);
19390
19391   if (TARGET_64BIT)
19392     {
19393       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
19394       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
19395     }
19396
19397   if (ix86_function_regparm (type, function) > 0)
19398     {
19399       tree parm;
19400
19401       parm = TYPE_ARG_TYPES (type);
19402       /* Figure out whether or not the function has a variable number of
19403          arguments.  */
19404       for (; parm; parm = TREE_CHAIN (parm))
19405         if (TREE_VALUE (parm) == void_type_node)
19406           break;
19407       /* If not, the this parameter is in the first argument.  */
19408       if (parm)
19409         {
19410           int regno = 0;
19411           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
19412             regno = 2;
19413           return gen_rtx_REG (SImode, regno);
19414         }
19415     }
19416
19417   if (aggregate_value_p (TREE_TYPE (type), type))
19418     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
19419   else
19420     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
19421 }
19422
19423 /* Determine whether x86_output_mi_thunk can succeed.  */
19424
19425 static bool
19426 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
19427                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
19428                          HOST_WIDE_INT vcall_offset, tree function)
19429 {
19430   /* 64-bit can handle anything.  */
19431   if (TARGET_64BIT)
19432     return true;
19433
19434   /* For 32-bit, everything's fine if we have one free register.  */
19435   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
19436     return true;
19437
19438   /* Need a free register for vcall_offset.  */
19439   if (vcall_offset)
19440     return false;
19441
19442   /* Need a free register for GOT references.  */
19443   if (flag_pic && !(*targetm.binds_local_p) (function))
19444     return false;
19445
19446   /* Otherwise ok.  */
19447   return true;
19448 }
19449
19450 /* Output the assembler code for a thunk function.  THUNK_DECL is the
19451    declaration for the thunk function itself, FUNCTION is the decl for
19452    the target function.  DELTA is an immediate constant offset to be
19453    added to THIS.  If VCALL_OFFSET is nonzero, the word at
19454    *(*this + vcall_offset) should be added to THIS.  */
19455
19456 static void
19457 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
19458                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
19459                      HOST_WIDE_INT vcall_offset, tree function)
19460 {
19461   rtx xops[3];
19462   rtx this = x86_this_parameter (function);
19463   rtx this_reg, tmp;
19464
19465   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
19466      pull it in now and let DELTA benefit.  */
19467   if (REG_P (this))
19468     this_reg = this;
19469   else if (vcall_offset)
19470     {
19471       /* Put the this parameter into %eax.  */
19472       xops[0] = this;
19473       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
19474       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19475     }
19476   else
19477     this_reg = NULL_RTX;
19478
19479   /* Adjust the this parameter by a fixed constant.  */
19480   if (delta)
19481     {
19482       xops[0] = GEN_INT (delta);
19483       xops[1] = this_reg ? this_reg : this;
19484       if (TARGET_64BIT)
19485         {
19486           if (!x86_64_general_operand (xops[0], DImode))
19487             {
19488               tmp = gen_rtx_REG (DImode, R10_REG);
19489               xops[1] = tmp;
19490               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
19491               xops[0] = tmp;
19492               xops[1] = this;
19493             }
19494           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19495         }
19496       else
19497         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19498     }
19499
19500   /* Adjust the this parameter by a value stored in the vtable.  */
19501   if (vcall_offset)
19502     {
19503       if (TARGET_64BIT)
19504         tmp = gen_rtx_REG (DImode, R10_REG);
19505       else
19506         {
19507           int tmp_regno = 2 /* ECX */;
19508           if (lookup_attribute ("fastcall",
19509               TYPE_ATTRIBUTES (TREE_TYPE (function))))
19510             tmp_regno = 0 /* EAX */;
19511           tmp = gen_rtx_REG (SImode, tmp_regno);
19512         }
19513
19514       xops[0] = gen_rtx_MEM (Pmode, this_reg);
19515       xops[1] = tmp;
19516       if (TARGET_64BIT)
19517         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19518       else
19519         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19520
19521       /* Adjust the this parameter.  */
19522       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
19523       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
19524         {
19525           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
19526           xops[0] = GEN_INT (vcall_offset);
19527           xops[1] = tmp2;
19528           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19529           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
19530         }
19531       xops[1] = this_reg;
19532       if (TARGET_64BIT)
19533         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19534       else
19535         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19536     }
19537
19538   /* If necessary, drop THIS back to its stack slot.  */
19539   if (this_reg && this_reg != this)
19540     {
19541       xops[0] = this_reg;
19542       xops[1] = this;
19543       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19544     }
19545
19546   xops[0] = XEXP (DECL_RTL (function), 0);
19547   if (TARGET_64BIT)
19548     {
19549       if (!flag_pic || (*targetm.binds_local_p) (function))
19550         output_asm_insn ("jmp\t%P0", xops);
19551       else
19552         {
19553           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
19554           tmp = gen_rtx_CONST (Pmode, tmp);
19555           tmp = gen_rtx_MEM (QImode, tmp);
19556           xops[0] = tmp;
19557           output_asm_insn ("jmp\t%A0", xops);
19558         }
19559     }
19560   else
19561     {
19562       if (!flag_pic || (*targetm.binds_local_p) (function))
19563         output_asm_insn ("jmp\t%P0", xops);
19564       else
19565 #if TARGET_MACHO
19566         if (TARGET_MACHO)
19567           {
19568             rtx sym_ref = XEXP (DECL_RTL (function), 0);
19569             tmp = (gen_rtx_SYMBOL_REF
19570                    (Pmode,
19571                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
19572             tmp = gen_rtx_MEM (QImode, tmp);
19573             xops[0] = tmp;
19574             output_asm_insn ("jmp\t%0", xops);
19575           }
19576         else
19577 #endif /* TARGET_MACHO */
19578         {
19579           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
19580           output_set_got (tmp, NULL_RTX);
19581
19582           xops[1] = tmp;
19583           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
19584           output_asm_insn ("jmp\t{*}%1", xops);
19585         }
19586     }
19587 }
19588
19589 static void
19590 x86_file_start (void)
19591 {
19592   default_file_start ();
19593 #if TARGET_MACHO
19594   darwin_file_start ();
19595 #endif
19596   if (X86_FILE_START_VERSION_DIRECTIVE)
19597     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
19598   if (X86_FILE_START_FLTUSED)
19599     fputs ("\t.global\t__fltused\n", asm_out_file);
19600   if (ix86_asm_dialect == ASM_INTEL)
19601     fputs ("\t.intel_syntax\n", asm_out_file);
19602 }
19603
19604 int
19605 x86_field_alignment (tree field, int computed)
19606 {
19607   enum machine_mode mode;
19608   tree type = TREE_TYPE (field);
19609
19610   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
19611     return computed;
19612   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
19613                     ? get_inner_array_type (type) : type);
19614   if (mode == DFmode || mode == DCmode
19615       || GET_MODE_CLASS (mode) == MODE_INT
19616       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
19617     return MIN (32, computed);
19618   return computed;
19619 }
19620
19621 /* Output assembler code to FILE to increment profiler label # LABELNO
19622    for profiling a function entry.  */
19623 void
19624 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
19625 {
19626   if (TARGET_64BIT)
19627     if (flag_pic)
19628       {
19629 #ifndef NO_PROFILE_COUNTERS
19630         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
19631 #endif
19632         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
19633       }
19634     else
19635       {
19636 #ifndef NO_PROFILE_COUNTERS
19637         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
19638 #endif
19639         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
19640       }
19641   else if (flag_pic)
19642     {
19643 #ifndef NO_PROFILE_COUNTERS
19644       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
19645                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
19646 #endif
19647       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
19648     }
19649   else
19650     {
19651 #ifndef NO_PROFILE_COUNTERS
19652       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
19653                PROFILE_COUNT_REGISTER);
19654 #endif
19655       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
19656     }
19657 }
19658
19659 /* We don't have exact information about the insn sizes, but we may assume
19660    quite safely that we are informed about all 1 byte insns and memory
19661    address sizes.  This is enough to eliminate unnecessary padding in
19662    99% of cases.  */
19663
19664 static int
19665 min_insn_size (rtx insn)
19666 {
19667   int l = 0;
19668
19669   if (!INSN_P (insn) || !active_insn_p (insn))
19670     return 0;
19671
19672   /* Discard alignments we've emit and jump instructions.  */
19673   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
19674       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
19675     return 0;
19676   if (JUMP_P (insn)
19677       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
19678           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
19679     return 0;
19680
19681   /* Important case - calls are always 5 bytes.
19682      It is common to have many calls in the row.  */
19683   if (CALL_P (insn)
19684       && symbolic_reference_mentioned_p (PATTERN (insn))
19685       && !SIBLING_CALL_P (insn))
19686     return 5;
19687   if (get_attr_length (insn) <= 1)
19688     return 1;
19689
19690   /* For normal instructions we may rely on the sizes of addresses
19691      and the presence of symbol to require 4 bytes of encoding.
19692      This is not the case for jumps where references are PC relative.  */
19693   if (!JUMP_P (insn))
19694     {
19695       l = get_attr_length_address (insn);
19696       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
19697         l = 4;
19698     }
19699   if (l)
19700     return 1+l;
19701   else
19702     return 2;
19703 }
19704
19705 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
19706    window.  */
19707
19708 static void
19709 ix86_avoid_jump_misspredicts (void)
19710 {
19711   rtx insn, start = get_insns ();
19712   int nbytes = 0, njumps = 0;
19713   int isjump = 0;
19714
19715   /* Look for all minimal intervals of instructions containing 4 jumps.
19716      The intervals are bounded by START and INSN.  NBYTES is the total
19717      size of instructions in the interval including INSN and not including
19718      START.  When the NBYTES is smaller than 16 bytes, it is possible
19719      that the end of START and INSN ends up in the same 16byte page.
19720
19721      The smallest offset in the page INSN can start is the case where START
19722      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
19723      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
19724      */
19725   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
19726     {
19727
19728       nbytes += min_insn_size (insn);
19729       if (dump_file)
19730         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
19731                 INSN_UID (insn), min_insn_size (insn));
19732       if ((JUMP_P (insn)
19733            && GET_CODE (PATTERN (insn)) != ADDR_VEC
19734            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
19735           || CALL_P (insn))
19736         njumps++;
19737       else
19738         continue;
19739
19740       while (njumps > 3)
19741         {
19742           start = NEXT_INSN (start);
19743           if ((JUMP_P (start)
19744                && GET_CODE (PATTERN (start)) != ADDR_VEC
19745                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
19746               || CALL_P (start))
19747             njumps--, isjump = 1;
19748           else
19749             isjump = 0;
19750           nbytes -= min_insn_size (start);
19751         }
19752       gcc_assert (njumps >= 0);
19753       if (dump_file)
19754         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
19755                 INSN_UID (start), INSN_UID (insn), nbytes);
19756
19757       if (njumps == 3 && isjump && nbytes < 16)
19758         {
19759           int padsize = 15 - nbytes + min_insn_size (insn);
19760
19761           if (dump_file)
19762             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
19763                      INSN_UID (insn), padsize);
19764           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
19765         }
19766     }
19767 }
19768
19769 /* AMD Athlon works faster
19770    when RET is not destination of conditional jump or directly preceded
19771    by other jump instruction.  We avoid the penalty by inserting NOP just
19772    before the RET instructions in such cases.  */
19773 static void
19774 ix86_pad_returns (void)
19775 {
19776   edge e;
19777   edge_iterator ei;
19778
19779   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
19780     {
19781       basic_block bb = e->src;
19782       rtx ret = BB_END (bb);
19783       rtx prev;
19784       bool replace = false;
19785
19786       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
19787           || !maybe_hot_bb_p (bb))
19788         continue;
19789       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
19790         if (active_insn_p (prev) || LABEL_P (prev))
19791           break;
19792       if (prev && LABEL_P (prev))
19793         {
19794           edge e;
19795           edge_iterator ei;
19796
19797           FOR_EACH_EDGE (e, ei, bb->preds)
19798             if (EDGE_FREQUENCY (e) && e->src->index >= 0
19799                 && !(e->flags & EDGE_FALLTHRU))
19800               replace = true;
19801         }
19802       if (!replace)
19803         {
19804           prev = prev_active_insn (ret);
19805           if (prev
19806               && ((JUMP_P (prev) && any_condjump_p (prev))
19807                   || CALL_P (prev)))
19808             replace = true;
19809           /* Empty functions get branch mispredict even when the jump destination
19810              is not visible to us.  */
19811           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
19812             replace = true;
19813         }
19814       if (replace)
19815         {
19816           emit_insn_before (gen_return_internal_long (), ret);
19817           delete_insn (ret);
19818         }
19819     }
19820 }
19821
19822 /* Implement machine specific optimizations.  We implement padding of returns
19823    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
19824 static void
19825 ix86_reorg (void)
19826 {
19827   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
19828     ix86_pad_returns ();
19829   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
19830     ix86_avoid_jump_misspredicts ();
19831 }
19832
19833 /* Return nonzero when QImode register that must be represented via REX prefix
19834    is used.  */
19835 bool
19836 x86_extended_QIreg_mentioned_p (rtx insn)
19837 {
19838   int i;
19839   extract_insn_cached (insn);
19840   for (i = 0; i < recog_data.n_operands; i++)
19841     if (REG_P (recog_data.operand[i])
19842         && REGNO (recog_data.operand[i]) >= 4)
19843        return true;
19844   return false;
19845 }
19846
19847 /* Return nonzero when P points to register encoded via REX prefix.
19848    Called via for_each_rtx.  */
19849 static int
19850 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
19851 {
19852    unsigned int regno;
19853    if (!REG_P (*p))
19854      return 0;
19855    regno = REGNO (*p);
19856    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
19857 }
19858
19859 /* Return true when INSN mentions register that must be encoded using REX
19860    prefix.  */
19861 bool
19862 x86_extended_reg_mentioned_p (rtx insn)
19863 {
19864   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
19865 }
19866
19867 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
19868    optabs would emit if we didn't have TFmode patterns.  */
19869
19870 void
19871 x86_emit_floatuns (rtx operands[2])
19872 {
19873   rtx neglab, donelab, i0, i1, f0, in, out;
19874   enum machine_mode mode, inmode;
19875
19876   inmode = GET_MODE (operands[1]);
19877   gcc_assert (inmode == SImode || inmode == DImode);
19878
19879   out = operands[0];
19880   in = force_reg (inmode, operands[1]);
19881   mode = GET_MODE (out);
19882   neglab = gen_label_rtx ();
19883   donelab = gen_label_rtx ();
19884   f0 = gen_reg_rtx (mode);
19885
19886   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
19887
19888   expand_float (out, in, 0);
19889
19890   emit_jump_insn (gen_jump (donelab));
19891   emit_barrier ();
19892
19893   emit_label (neglab);
19894
19895   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
19896                             1, OPTAB_DIRECT);
19897   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
19898                             1, OPTAB_DIRECT);
19899   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
19900
19901   expand_float (f0, i0, 0);
19902
19903   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
19904
19905   emit_label (donelab);
19906 }
19907 \f
19908 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
19909    with all elements equal to VAR.  Return true if successful.  */
19910
19911 static bool
19912 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
19913                                    rtx target, rtx val)
19914 {
19915   enum machine_mode smode, wsmode, wvmode;
19916   rtx x;
19917
19918   switch (mode)
19919     {
19920     case V2SImode:
19921     case V2SFmode:
19922       if (!mmx_ok)
19923         return false;
19924       /* FALLTHRU */
19925
19926     case V2DFmode:
19927     case V2DImode:
19928     case V4SFmode:
19929     case V4SImode:
19930       val = force_reg (GET_MODE_INNER (mode), val);
19931       x = gen_rtx_VEC_DUPLICATE (mode, val);
19932       emit_insn (gen_rtx_SET (VOIDmode, target, x));
19933       return true;
19934
19935     case V4HImode:
19936       if (!mmx_ok)
19937         return false;
19938       if (TARGET_SSE || TARGET_3DNOW_A)
19939         {
19940           val = gen_lowpart (SImode, val);
19941           x = gen_rtx_TRUNCATE (HImode, val);
19942           x = gen_rtx_VEC_DUPLICATE (mode, x);
19943           emit_insn (gen_rtx_SET (VOIDmode, target, x));
19944           return true;
19945         }
19946       else
19947         {
19948           smode = HImode;
19949           wsmode = SImode;
19950           wvmode = V2SImode;
19951           goto widen;
19952         }
19953
19954     case V8QImode:
19955       if (!mmx_ok)
19956         return false;
19957       smode = QImode;
19958       wsmode = HImode;
19959       wvmode = V4HImode;
19960       goto widen;
19961     case V8HImode:
19962       if (TARGET_SSE2)
19963         {
19964           rtx tmp1, tmp2;
19965           /* Extend HImode to SImode using a paradoxical SUBREG.  */
19966           tmp1 = gen_reg_rtx (SImode);
19967           emit_move_insn (tmp1, gen_lowpart (SImode, val));
19968           /* Insert the SImode value as low element of V4SImode vector. */
19969           tmp2 = gen_reg_rtx (V4SImode);
19970           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
19971                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
19972                                     CONST0_RTX (V4SImode),
19973                                     const1_rtx);
19974           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
19975           /* Cast the V4SImode vector back to a V8HImode vector.  */
19976           tmp1 = gen_reg_rtx (V8HImode);
19977           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
19978           /* Duplicate the low short through the whole low SImode word.  */
19979           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
19980           /* Cast the V8HImode vector back to a V4SImode vector.  */
19981           tmp2 = gen_reg_rtx (V4SImode);
19982           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
19983           /* Replicate the low element of the V4SImode vector.  */
19984           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
19985           /* Cast the V2SImode back to V8HImode, and store in target.  */
19986           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
19987           return true;
19988         }
19989       smode = HImode;
19990       wsmode = SImode;
19991       wvmode = V4SImode;
19992       goto widen;
19993     case V16QImode:
19994       if (TARGET_SSE2)
19995         {
19996           rtx tmp1, tmp2;
19997           /* Extend QImode to SImode using a paradoxical SUBREG.  */
19998           tmp1 = gen_reg_rtx (SImode);
19999           emit_move_insn (tmp1, gen_lowpart (SImode, val));
20000           /* Insert the SImode value as low element of V4SImode vector. */
20001           tmp2 = gen_reg_rtx (V4SImode);
20002           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
20003                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
20004                                     CONST0_RTX (V4SImode),
20005                                     const1_rtx);
20006           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
20007           /* Cast the V4SImode vector back to a V16QImode vector.  */
20008           tmp1 = gen_reg_rtx (V16QImode);
20009           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
20010           /* Duplicate the low byte through the whole low SImode word.  */
20011           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20012           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20013           /* Cast the V16QImode vector back to a V4SImode vector.  */
20014           tmp2 = gen_reg_rtx (V4SImode);
20015           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
20016           /* Replicate the low element of the V4SImode vector.  */
20017           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
20018           /* Cast the V2SImode back to V16QImode, and store in target.  */
20019           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
20020           return true;
20021         }
20022       smode = QImode;
20023       wsmode = HImode;
20024       wvmode = V8HImode;
20025       goto widen;
20026     widen:
20027       /* Replicate the value once into the next wider mode and recurse.  */
20028       val = convert_modes (wsmode, smode, val, true);
20029       x = expand_simple_binop (wsmode, ASHIFT, val,
20030                                GEN_INT (GET_MODE_BITSIZE (smode)),
20031                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
20032       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
20033
20034       x = gen_reg_rtx (wvmode);
20035       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
20036         gcc_unreachable ();
20037       emit_move_insn (target, gen_lowpart (mode, x));
20038       return true;
20039
20040     default:
20041       return false;
20042     }
20043 }
20044
20045 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20046    whose ONE_VAR element is VAR, and other elements are zero.  Return true
20047    if successful.  */
20048
20049 static bool
20050 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
20051                                      rtx target, rtx var, int one_var)
20052 {
20053   enum machine_mode vsimode;
20054   rtx new_target;
20055   rtx x, tmp;
20056
20057   switch (mode)
20058     {
20059     case V2SFmode:
20060     case V2SImode:
20061       if (!mmx_ok)
20062         return false;
20063       /* FALLTHRU */
20064
20065     case V2DFmode:
20066     case V2DImode:
20067       if (one_var != 0)
20068         return false;
20069       var = force_reg (GET_MODE_INNER (mode), var);
20070       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
20071       emit_insn (gen_rtx_SET (VOIDmode, target, x));
20072       return true;
20073
20074     case V4SFmode:
20075     case V4SImode:
20076       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
20077         new_target = gen_reg_rtx (mode);
20078       else
20079         new_target = target;
20080       var = force_reg (GET_MODE_INNER (mode), var);
20081       x = gen_rtx_VEC_DUPLICATE (mode, var);
20082       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
20083       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
20084       if (one_var != 0)
20085         {
20086           /* We need to shuffle the value to the correct position, so
20087              create a new pseudo to store the intermediate result.  */
20088
20089           /* With SSE2, we can use the integer shuffle insns.  */
20090           if (mode != V4SFmode && TARGET_SSE2)
20091             {
20092               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
20093                                             GEN_INT (1),
20094                                             GEN_INT (one_var == 1 ? 0 : 1),
20095                                             GEN_INT (one_var == 2 ? 0 : 1),
20096                                             GEN_INT (one_var == 3 ? 0 : 1)));
20097               if (target != new_target)
20098                 emit_move_insn (target, new_target);
20099               return true;
20100             }
20101
20102           /* Otherwise convert the intermediate result to V4SFmode and
20103              use the SSE1 shuffle instructions.  */
20104           if (mode != V4SFmode)
20105             {
20106               tmp = gen_reg_rtx (V4SFmode);
20107               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
20108             }
20109           else
20110             tmp = new_target;
20111
20112           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
20113                                        GEN_INT (1),
20114                                        GEN_INT (one_var == 1 ? 0 : 1),
20115                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
20116                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
20117
20118           if (mode != V4SFmode)
20119             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
20120           else if (tmp != target)
20121             emit_move_insn (target, tmp);
20122         }
20123       else if (target != new_target)
20124         emit_move_insn (target, new_target);
20125       return true;
20126
20127     case V8HImode:
20128     case V16QImode:
20129       vsimode = V4SImode;
20130       goto widen;
20131     case V4HImode:
20132     case V8QImode:
20133       if (!mmx_ok)
20134         return false;
20135       vsimode = V2SImode;
20136       goto widen;
20137     widen:
20138       if (one_var != 0)
20139         return false;
20140
20141       /* Zero extend the variable element to SImode and recurse.  */
20142       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
20143
20144       x = gen_reg_rtx (vsimode);
20145       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
20146                                                 var, one_var))
20147         gcc_unreachable ();
20148
20149       emit_move_insn (target, gen_lowpart (mode, x));
20150       return true;
20151
20152     default:
20153       return false;
20154     }
20155 }
20156
20157 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20158    consisting of the values in VALS.  It is known that all elements
20159    except ONE_VAR are constants.  Return true if successful.  */
20160
20161 static bool
20162 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
20163                                  rtx target, rtx vals, int one_var)
20164 {
20165   rtx var = XVECEXP (vals, 0, one_var);
20166   enum machine_mode wmode;
20167   rtx const_vec, x;
20168
20169   const_vec = copy_rtx (vals);
20170   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
20171   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
20172
20173   switch (mode)
20174     {
20175     case V2DFmode:
20176     case V2DImode:
20177     case V2SFmode:
20178     case V2SImode:
20179       /* For the two element vectors, it's just as easy to use
20180          the general case.  */
20181       return false;
20182
20183     case V4SFmode:
20184     case V4SImode:
20185     case V8HImode:
20186     case V4HImode:
20187       break;
20188
20189     case V16QImode:
20190       wmode = V8HImode;
20191       goto widen;
20192     case V8QImode:
20193       wmode = V4HImode;
20194       goto widen;
20195     widen:
20196       /* There's no way to set one QImode entry easily.  Combine
20197          the variable value with its adjacent constant value, and
20198          promote to an HImode set.  */
20199       x = XVECEXP (vals, 0, one_var ^ 1);
20200       if (one_var & 1)
20201         {
20202           var = convert_modes (HImode, QImode, var, true);
20203           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
20204                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
20205           x = GEN_INT (INTVAL (x) & 0xff);
20206         }
20207       else
20208         {
20209           var = convert_modes (HImode, QImode, var, true);
20210           x = gen_int_mode (INTVAL (x) << 8, HImode);
20211         }
20212       if (x != const0_rtx)
20213         var = expand_simple_binop (HImode, IOR, var, x, var,
20214                                    1, OPTAB_LIB_WIDEN);
20215
20216       x = gen_reg_rtx (wmode);
20217       emit_move_insn (x, gen_lowpart (wmode, const_vec));
20218       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
20219
20220       emit_move_insn (target, gen_lowpart (mode, x));
20221       return true;
20222
20223     default:
20224       return false;
20225     }
20226
20227   emit_move_insn (target, const_vec);
20228   ix86_expand_vector_set (mmx_ok, target, var, one_var);
20229   return true;
20230 }
20231
20232 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
20233    all values variable, and none identical.  */
20234
20235 static void
20236 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
20237                                  rtx target, rtx vals)
20238 {
20239   enum machine_mode half_mode = GET_MODE_INNER (mode);
20240   rtx op0 = NULL, op1 = NULL;
20241   bool use_vec_concat = false;
20242
20243   switch (mode)
20244     {
20245     case V2SFmode:
20246     case V2SImode:
20247       if (!mmx_ok && !TARGET_SSE)
20248         break;
20249       /* FALLTHRU */
20250
20251     case V2DFmode:
20252     case V2DImode:
20253       /* For the two element vectors, we always implement VEC_CONCAT.  */
20254       op0 = XVECEXP (vals, 0, 0);
20255       op1 = XVECEXP (vals, 0, 1);
20256       use_vec_concat = true;
20257       break;
20258
20259     case V4SFmode:
20260       half_mode = V2SFmode;
20261       goto half;
20262     case V4SImode:
20263       half_mode = V2SImode;
20264       goto half;
20265     half:
20266       {
20267         rtvec v;
20268
20269         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
20270            Recurse to load the two halves.  */
20271
20272         op0 = gen_reg_rtx (half_mode);
20273         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
20274         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
20275
20276         op1 = gen_reg_rtx (half_mode);
20277         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
20278         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
20279
20280         use_vec_concat = true;
20281       }
20282       break;
20283
20284     case V8HImode:
20285     case V16QImode:
20286     case V4HImode:
20287     case V8QImode:
20288       break;
20289
20290     default:
20291       gcc_unreachable ();
20292     }
20293
20294   if (use_vec_concat)
20295     {
20296       if (!register_operand (op0, half_mode))
20297         op0 = force_reg (half_mode, op0);
20298       if (!register_operand (op1, half_mode))
20299         op1 = force_reg (half_mode, op1);
20300
20301       emit_insn (gen_rtx_SET (VOIDmode, target,
20302                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
20303     }
20304   else
20305     {
20306       int i, j, n_elts, n_words, n_elt_per_word;
20307       enum machine_mode inner_mode;
20308       rtx words[4], shift;
20309
20310       inner_mode = GET_MODE_INNER (mode);
20311       n_elts = GET_MODE_NUNITS (mode);
20312       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
20313       n_elt_per_word = n_elts / n_words;
20314       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
20315
20316       for (i = 0; i < n_words; ++i)
20317         {
20318           rtx word = NULL_RTX;
20319
20320           for (j = 0; j < n_elt_per_word; ++j)
20321             {
20322               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
20323               elt = convert_modes (word_mode, inner_mode, elt, true);
20324
20325               if (j == 0)
20326                 word = elt;
20327               else
20328                 {
20329                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
20330                                               word, 1, OPTAB_LIB_WIDEN);
20331                   word = expand_simple_binop (word_mode, IOR, word, elt,
20332                                               word, 1, OPTAB_LIB_WIDEN);
20333                 }
20334             }
20335
20336           words[i] = word;
20337         }
20338
20339       if (n_words == 1)
20340         emit_move_insn (target, gen_lowpart (mode, words[0]));
20341       else if (n_words == 2)
20342         {
20343           rtx tmp = gen_reg_rtx (mode);
20344           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
20345           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
20346           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
20347           emit_move_insn (target, tmp);
20348         }
20349       else if (n_words == 4)
20350         {
20351           rtx tmp = gen_reg_rtx (V4SImode);
20352           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
20353           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
20354           emit_move_insn (target, gen_lowpart (mode, tmp));
20355         }
20356       else
20357         gcc_unreachable ();
20358     }
20359 }
20360
20361 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
20362    instructions unless MMX_OK is true.  */
20363
20364 void
20365 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
20366 {
20367   enum machine_mode mode = GET_MODE (target);
20368   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20369   int n_elts = GET_MODE_NUNITS (mode);
20370   int n_var = 0, one_var = -1;
20371   bool all_same = true, all_const_zero = true;
20372   int i;
20373   rtx x;
20374
20375   for (i = 0; i < n_elts; ++i)
20376     {
20377       x = XVECEXP (vals, 0, i);
20378       if (!CONSTANT_P (x))
20379         n_var++, one_var = i;
20380       else if (x != CONST0_RTX (inner_mode))
20381         all_const_zero = false;
20382       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
20383         all_same = false;
20384     }
20385
20386   /* Constants are best loaded from the constant pool.  */
20387   if (n_var == 0)
20388     {
20389       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
20390       return;
20391     }
20392
20393   /* If all values are identical, broadcast the value.  */
20394   if (all_same
20395       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
20396                                             XVECEXP (vals, 0, 0)))
20397     return;
20398
20399   /* Values where only one field is non-constant are best loaded from
20400      the pool and overwritten via move later.  */
20401   if (n_var == 1)
20402     {
20403       if (all_const_zero
20404           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
20405                                                   XVECEXP (vals, 0, one_var),
20406                                                   one_var))
20407         return;
20408
20409       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
20410         return;
20411     }
20412
20413   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
20414 }
20415
20416 void
20417 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
20418 {
20419   enum machine_mode mode = GET_MODE (target);
20420   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20421   bool use_vec_merge = false;
20422   rtx tmp;
20423
20424   switch (mode)
20425     {
20426     case V2SFmode:
20427     case V2SImode:
20428       if (mmx_ok)
20429         {
20430           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
20431           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
20432           if (elt == 0)
20433             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
20434           else
20435             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
20436           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20437           return;
20438         }
20439       break;
20440
20441     case V2DFmode:
20442     case V2DImode:
20443       {
20444         rtx op0, op1;
20445
20446         /* For the two element vectors, we implement a VEC_CONCAT with
20447            the extraction of the other element.  */
20448
20449         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
20450         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
20451
20452         if (elt == 0)
20453           op0 = val, op1 = tmp;
20454         else
20455           op0 = tmp, op1 = val;
20456
20457         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
20458         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20459       }
20460       return;
20461
20462     case V4SFmode:
20463       switch (elt)
20464         {
20465         case 0:
20466           use_vec_merge = true;
20467           break;
20468
20469         case 1:
20470           /* tmp = target = A B C D */
20471           tmp = copy_to_reg (target);
20472           /* target = A A B B */
20473           emit_insn (gen_sse_unpcklps (target, target, target));
20474           /* target = X A B B */
20475           ix86_expand_vector_set (false, target, val, 0);
20476           /* target = A X C D  */
20477           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20478                                        GEN_INT (1), GEN_INT (0),
20479                                        GEN_INT (2+4), GEN_INT (3+4)));
20480           return;
20481
20482         case 2:
20483           /* tmp = target = A B C D */
20484           tmp = copy_to_reg (target);
20485           /* tmp = X B C D */
20486           ix86_expand_vector_set (false, tmp, val, 0);
20487           /* target = A B X D */
20488           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20489                                        GEN_INT (0), GEN_INT (1),
20490                                        GEN_INT (0+4), GEN_INT (3+4)));
20491           return;
20492
20493         case 3:
20494           /* tmp = target = A B C D */
20495           tmp = copy_to_reg (target);
20496           /* tmp = X B C D */
20497           ix86_expand_vector_set (false, tmp, val, 0);
20498           /* target = A B X D */
20499           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20500                                        GEN_INT (0), GEN_INT (1),
20501                                        GEN_INT (2+4), GEN_INT (0+4)));
20502           return;
20503
20504         default:
20505           gcc_unreachable ();
20506         }
20507       break;
20508
20509     case V4SImode:
20510       /* Element 0 handled by vec_merge below.  */
20511       if (elt == 0)
20512         {
20513           use_vec_merge = true;
20514           break;
20515         }
20516
20517       if (TARGET_SSE2)
20518         {
20519           /* With SSE2, use integer shuffles to swap element 0 and ELT,
20520              store into element 0, then shuffle them back.  */
20521
20522           rtx order[4];
20523
20524           order[0] = GEN_INT (elt);
20525           order[1] = const1_rtx;
20526           order[2] = const2_rtx;
20527           order[3] = GEN_INT (3);
20528           order[elt] = const0_rtx;
20529
20530           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20531                                         order[1], order[2], order[3]));
20532
20533           ix86_expand_vector_set (false, target, val, 0);
20534
20535           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20536                                         order[1], order[2], order[3]));
20537         }
20538       else
20539         {
20540           /* For SSE1, we have to reuse the V4SF code.  */
20541           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
20542                                   gen_lowpart (SFmode, val), elt);
20543         }
20544       return;
20545
20546     case V8HImode:
20547       use_vec_merge = TARGET_SSE2;
20548       break;
20549     case V4HImode:
20550       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
20551       break;
20552
20553     case V16QImode:
20554     case V8QImode:
20555     default:
20556       break;
20557     }
20558
20559   if (use_vec_merge)
20560     {
20561       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
20562       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
20563       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20564     }
20565   else
20566     {
20567       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
20568
20569       emit_move_insn (mem, target);
20570
20571       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
20572       emit_move_insn (tmp, val);
20573
20574       emit_move_insn (target, mem);
20575     }
20576 }
20577
20578 void
20579 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
20580 {
20581   enum machine_mode mode = GET_MODE (vec);
20582   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20583   bool use_vec_extr = false;
20584   rtx tmp;
20585
20586   switch (mode)
20587     {
20588     case V2SImode:
20589     case V2SFmode:
20590       if (!mmx_ok)
20591         break;
20592       /* FALLTHRU */
20593
20594     case V2DFmode:
20595     case V2DImode:
20596       use_vec_extr = true;
20597       break;
20598
20599     case V4SFmode:
20600       switch (elt)
20601         {
20602         case 0:
20603           tmp = vec;
20604           break;
20605
20606         case 1:
20607         case 3:
20608           tmp = gen_reg_rtx (mode);
20609           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
20610                                        GEN_INT (elt), GEN_INT (elt),
20611                                        GEN_INT (elt+4), GEN_INT (elt+4)));
20612           break;
20613
20614         case 2:
20615           tmp = gen_reg_rtx (mode);
20616           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
20617           break;
20618
20619         default:
20620           gcc_unreachable ();
20621         }
20622       vec = tmp;
20623       use_vec_extr = true;
20624       elt = 0;
20625       break;
20626
20627     case V4SImode:
20628       if (TARGET_SSE2)
20629         {
20630           switch (elt)
20631             {
20632             case 0:
20633               tmp = vec;
20634               break;
20635
20636             case 1:
20637             case 3:
20638               tmp = gen_reg_rtx (mode);
20639               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
20640                                             GEN_INT (elt), GEN_INT (elt),
20641                                             GEN_INT (elt), GEN_INT (elt)));
20642               break;
20643
20644             case 2:
20645               tmp = gen_reg_rtx (mode);
20646               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
20647               break;
20648
20649             default:
20650               gcc_unreachable ();
20651             }
20652           vec = tmp;
20653           use_vec_extr = true;
20654           elt = 0;
20655         }
20656       else
20657         {
20658           /* For SSE1, we have to reuse the V4SF code.  */
20659           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
20660                                       gen_lowpart (V4SFmode, vec), elt);
20661           return;
20662         }
20663       break;
20664
20665     case V8HImode:
20666       use_vec_extr = TARGET_SSE2;
20667       break;
20668     case V4HImode:
20669       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
20670       break;
20671
20672     case V16QImode:
20673     case V8QImode:
20674       /* ??? Could extract the appropriate HImode element and shift.  */
20675     default:
20676       break;
20677     }
20678
20679   if (use_vec_extr)
20680     {
20681       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
20682       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
20683
20684       /* Let the rtl optimizers know about the zero extension performed.  */
20685       if (inner_mode == HImode)
20686         {
20687           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
20688           target = gen_lowpart (SImode, target);
20689         }
20690
20691       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20692     }
20693   else
20694     {
20695       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
20696
20697       emit_move_insn (mem, vec);
20698
20699       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
20700       emit_move_insn (target, tmp);
20701     }
20702 }
20703
20704 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
20705    pattern to reduce; DEST is the destination; IN is the input vector.  */
20706
20707 void
20708 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
20709 {
20710   rtx tmp1, tmp2, tmp3;
20711
20712   tmp1 = gen_reg_rtx (V4SFmode);
20713   tmp2 = gen_reg_rtx (V4SFmode);
20714   tmp3 = gen_reg_rtx (V4SFmode);
20715
20716   emit_insn (gen_sse_movhlps (tmp1, in, in));
20717   emit_insn (fn (tmp2, tmp1, in));
20718
20719   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
20720                                GEN_INT (1), GEN_INT (1),
20721                                GEN_INT (1+4), GEN_INT (1+4)));
20722   emit_insn (fn (dest, tmp2, tmp3));
20723 }
20724 \f
20725 /* Target hook for scalar_mode_supported_p.  */
20726 static bool
20727 ix86_scalar_mode_supported_p (enum machine_mode mode)
20728 {
20729   if (DECIMAL_FLOAT_MODE_P (mode))
20730     return true;
20731   else
20732     return default_scalar_mode_supported_p (mode);
20733 }
20734
20735 /* Implements target hook vector_mode_supported_p.  */
20736 static bool
20737 ix86_vector_mode_supported_p (enum machine_mode mode)
20738 {
20739   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
20740     return true;
20741   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
20742     return true;
20743   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
20744     return true;
20745   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
20746     return true;
20747   return false;
20748 }
20749
20750 /* Worker function for TARGET_MD_ASM_CLOBBERS.
20751
20752    We do this in the new i386 backend to maintain source compatibility
20753    with the old cc0-based compiler.  */
20754
20755 static tree
20756 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
20757                       tree inputs ATTRIBUTE_UNUSED,
20758                       tree clobbers)
20759 {
20760   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
20761                         clobbers);
20762   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
20763                         clobbers);
20764   return clobbers;
20765 }
20766
20767 /* Implementes target vector targetm.asm.encode_section_info.  This
20768    is not used by netware.  */
20769
20770 static void ATTRIBUTE_UNUSED
20771 ix86_encode_section_info (tree decl, rtx rtl, int first)
20772 {
20773   default_encode_section_info (decl, rtl, first);
20774
20775   if (TREE_CODE (decl) == VAR_DECL
20776       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
20777       && ix86_in_large_data_p (decl))
20778     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
20779 }
20780
20781 /* Worker function for REVERSE_CONDITION.  */
20782
20783 enum rtx_code
20784 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
20785 {
20786   return (mode != CCFPmode && mode != CCFPUmode
20787           ? reverse_condition (code)
20788           : reverse_condition_maybe_unordered (code));
20789 }
20790
20791 /* Output code to perform an x87 FP register move, from OPERANDS[1]
20792    to OPERANDS[0].  */
20793
20794 const char *
20795 output_387_reg_move (rtx insn, rtx *operands)
20796 {
20797   if (REG_P (operands[1])
20798       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
20799     {
20800       if (REGNO (operands[0]) == FIRST_STACK_REG)
20801         return output_387_ffreep (operands, 0);
20802       return "fstp\t%y0";
20803     }
20804   if (STACK_TOP_P (operands[0]))
20805     return "fld%z1\t%y1";
20806   return "fst\t%y0";
20807 }
20808
20809 /* Output code to perform a conditional jump to LABEL, if C2 flag in
20810    FP status register is set.  */
20811
20812 void
20813 ix86_emit_fp_unordered_jump (rtx label)
20814 {
20815   rtx reg = gen_reg_rtx (HImode);
20816   rtx temp;
20817
20818   emit_insn (gen_x86_fnstsw_1 (reg));
20819
20820   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_size))
20821     {
20822       emit_insn (gen_x86_sahf_1 (reg));
20823
20824       temp = gen_rtx_REG (CCmode, FLAGS_REG);
20825       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
20826     }
20827   else
20828     {
20829       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
20830
20831       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
20832       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
20833     }
20834
20835   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
20836                               gen_rtx_LABEL_REF (VOIDmode, label),
20837                               pc_rtx);
20838   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
20839
20840   emit_jump_insn (temp);
20841   predict_jump (REG_BR_PROB_BASE * 10 / 100);
20842 }
20843
20844 /* Output code to perform a log1p XFmode calculation.  */
20845
20846 void ix86_emit_i387_log1p (rtx op0, rtx op1)
20847 {
20848   rtx label1 = gen_label_rtx ();
20849   rtx label2 = gen_label_rtx ();
20850
20851   rtx tmp = gen_reg_rtx (XFmode);
20852   rtx tmp2 = gen_reg_rtx (XFmode);
20853
20854   emit_insn (gen_absxf2 (tmp, op1));
20855   emit_insn (gen_cmpxf (tmp,
20856     CONST_DOUBLE_FROM_REAL_VALUE (
20857        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
20858        XFmode)));
20859   emit_jump_insn (gen_bge (label1));
20860
20861   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
20862   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
20863   emit_jump (label2);
20864
20865   emit_label (label1);
20866   emit_move_insn (tmp, CONST1_RTX (XFmode));
20867   emit_insn (gen_addxf3 (tmp, op1, tmp));
20868   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
20869   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
20870
20871   emit_label (label2);
20872 }
20873
20874 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
20875
20876 static void ATTRIBUTE_UNUSED
20877 i386_solaris_elf_named_section (const char *name, unsigned int flags,
20878                                 tree decl)
20879 {
20880   /* With Binutils 2.15, the "@unwind" marker must be specified on
20881      every occurrence of the ".eh_frame" section, not just the first
20882      one.  */
20883   if (TARGET_64BIT
20884       && strcmp (name, ".eh_frame") == 0)
20885     {
20886       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
20887                flags & SECTION_WRITE ? "aw" : "a");
20888       return;
20889     }
20890   default_elf_asm_named_section (name, flags, decl);
20891 }
20892
20893 /* Return the mangling of TYPE if it is an extended fundamental type.  */
20894
20895 static const char *
20896 ix86_mangle_fundamental_type (tree type)
20897 {
20898   switch (TYPE_MODE (type))
20899     {
20900     case TFmode:
20901       /* __float128 is "g".  */
20902       return "g";
20903     case XFmode:
20904       /* "long double" or __float80 is "e".  */
20905       return "e";
20906     default:
20907       return NULL;
20908     }
20909 }
20910
20911 /* For 32-bit code we can save PIC register setup by using
20912    __stack_chk_fail_local hidden function instead of calling
20913    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
20914    register, so it is better to call __stack_chk_fail directly.  */
20915
20916 static tree
20917 ix86_stack_protect_fail (void)
20918 {
20919   return TARGET_64BIT
20920          ? default_external_stack_protect_fail ()
20921          : default_hidden_stack_protect_fail ();
20922 }
20923
20924 /* Select a format to encode pointers in exception handling data.  CODE
20925    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
20926    true if the symbol may be affected by dynamic relocations.
20927
20928    ??? All x86 object file formats are capable of representing this.
20929    After all, the relocation needed is the same as for the call insn.
20930    Whether or not a particular assembler allows us to enter such, I
20931    guess we'll have to see.  */
20932 int
20933 asm_preferred_eh_data_format (int code, int global)
20934 {
20935   if (flag_pic)
20936     {
20937       int type = DW_EH_PE_sdata8;
20938       if (!TARGET_64BIT
20939           || ix86_cmodel == CM_SMALL_PIC
20940           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
20941         type = DW_EH_PE_sdata4;
20942       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
20943     }
20944   if (ix86_cmodel == CM_SMALL
20945       || (ix86_cmodel == CM_MEDIUM && code))
20946     return DW_EH_PE_udata4;
20947   return DW_EH_PE_absptr;
20948 }
20949 \f
20950 /* Expand copysign from SIGN to the positive value ABS_VALUE
20951    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
20952    the sign-bit.  */
20953 static void
20954 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
20955 {
20956   enum machine_mode mode = GET_MODE (sign);
20957   rtx sgn = gen_reg_rtx (mode);
20958   if (mask == NULL_RTX)
20959     {
20960       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
20961       if (!VECTOR_MODE_P (mode))
20962         {
20963           /* We need to generate a scalar mode mask in this case.  */
20964           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
20965           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
20966           mask = gen_reg_rtx (mode);
20967           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
20968         }
20969     }
20970   else
20971     mask = gen_rtx_NOT (mode, mask);
20972   emit_insn (gen_rtx_SET (VOIDmode, sgn,
20973                           gen_rtx_AND (mode, mask, sign)));
20974   emit_insn (gen_rtx_SET (VOIDmode, result,
20975                           gen_rtx_IOR (mode, abs_value, sgn)));
20976 }
20977
20978 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
20979    mask for masking out the sign-bit is stored in *SMASK, if that is
20980    non-null.  */
20981 static rtx
20982 ix86_expand_sse_fabs (rtx op0, rtx *smask)
20983 {
20984   enum machine_mode mode = GET_MODE (op0);
20985   rtx xa, mask;
20986
20987   xa = gen_reg_rtx (mode);
20988   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
20989   if (!VECTOR_MODE_P (mode))
20990     {
20991       /* We need to generate a scalar mode mask in this case.  */
20992       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
20993       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
20994       mask = gen_reg_rtx (mode);
20995       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
20996     }
20997   emit_insn (gen_rtx_SET (VOIDmode, xa,
20998                           gen_rtx_AND (mode, op0, mask)));
20999
21000   if (smask)
21001     *smask = mask;
21002
21003   return xa;
21004 }
21005
21006 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
21007    swapping the operands if SWAP_OPERANDS is true.  The expanded
21008    code is a forward jump to a newly created label in case the
21009    comparison is true.  The generated label rtx is returned.  */
21010 static rtx
21011 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
21012                                   bool swap_operands)
21013 {
21014   rtx label, tmp;
21015
21016   if (swap_operands)
21017     {
21018       tmp = op0;
21019       op0 = op1;
21020       op1 = tmp;
21021     }
21022
21023   label = gen_label_rtx ();
21024   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
21025   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21026                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
21027   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
21028   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
21029                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
21030   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
21031   JUMP_LABEL (tmp) = label;
21032
21033   return label;
21034 }
21035
21036 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
21037    using comparison code CODE.  Operands are swapped for the comparison if
21038    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
21039 static rtx
21040 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
21041                               bool swap_operands)
21042 {
21043   enum machine_mode mode = GET_MODE (op0);
21044   rtx mask = gen_reg_rtx (mode);
21045
21046   if (swap_operands)
21047     {
21048       rtx tmp = op0;
21049       op0 = op1;
21050       op1 = tmp;
21051     }
21052
21053   if (mode == DFmode)
21054     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
21055                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
21056   else
21057     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
21058                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
21059
21060   return mask;
21061 }
21062
21063 /* Generate and return a rtx of mode MODE for 2**n where n is the number
21064    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
21065 static rtx
21066 ix86_gen_TWO52 (enum machine_mode mode)
21067 {
21068   REAL_VALUE_TYPE TWO52r;
21069   rtx TWO52;
21070
21071   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
21072   TWO52 = const_double_from_real_value (TWO52r, mode);
21073   TWO52 = force_reg (mode, TWO52);
21074
21075   return TWO52;
21076 }
21077
21078 /* Expand SSE sequence for computing lround from OP1 storing
21079    into OP0.  */
21080 void
21081 ix86_expand_lround (rtx op0, rtx op1)
21082 {
21083   /* C code for the stuff we're doing below:
21084        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
21085        return (long)tmp;
21086    */
21087   enum machine_mode mode = GET_MODE (op1);
21088   const struct real_format *fmt;
21089   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21090   rtx adj;
21091
21092   /* load nextafter (0.5, 0.0) */
21093   fmt = REAL_MODE_FORMAT (mode);
21094   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21095   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21096
21097   /* adj = copysign (0.5, op1) */
21098   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
21099   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
21100
21101   /* adj = op1 + adj */
21102   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
21103
21104   /* op0 = (imode)adj */
21105   expand_fix (op0, adj, 0);
21106 }
21107
21108 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
21109    into OPERAND0.  */
21110 void
21111 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
21112 {
21113   /* C code for the stuff we're doing below (for do_floor):
21114         xi = (long)op1;
21115         xi -= (double)xi > op1 ? 1 : 0;
21116         return xi;
21117    */
21118   enum machine_mode fmode = GET_MODE (op1);
21119   enum machine_mode imode = GET_MODE (op0);
21120   rtx ireg, freg, label, tmp;
21121
21122   /* reg = (long)op1 */
21123   ireg = gen_reg_rtx (imode);
21124   expand_fix (ireg, op1, 0);
21125
21126   /* freg = (double)reg */
21127   freg = gen_reg_rtx (fmode);
21128   expand_float (freg, ireg, 0);
21129
21130   /* ireg = (freg > op1) ? ireg - 1 : ireg */
21131   label = ix86_expand_sse_compare_and_jump (UNLE,
21132                                             freg, op1, !do_floor);
21133   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
21134                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
21135   emit_move_insn (ireg, tmp);
21136
21137   emit_label (label);
21138   LABEL_NUSES (label) = 1;
21139
21140   emit_move_insn (op0, ireg);
21141 }
21142
21143 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
21144    result in OPERAND0.  */
21145 void
21146 ix86_expand_rint (rtx operand0, rtx operand1)
21147 {
21148   /* C code for the stuff we're doing below:
21149         xa = fabs (operand1);
21150         if (!isless (xa, 2**52))
21151           return operand1;
21152         xa = xa + 2**52 - 2**52;
21153         return copysign (xa, operand1);
21154    */
21155   enum machine_mode mode = GET_MODE (operand0);
21156   rtx res, xa, label, TWO52, mask;
21157
21158   res = gen_reg_rtx (mode);
21159   emit_move_insn (res, operand1);
21160
21161   /* xa = abs (operand1) */
21162   xa = ix86_expand_sse_fabs (res, &mask);
21163
21164   /* if (!isless (xa, TWO52)) goto label; */
21165   TWO52 = ix86_gen_TWO52 (mode);
21166   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21167
21168   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21169   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21170
21171   ix86_sse_copysign_to_positive (res, xa, res, mask);
21172
21173   emit_label (label);
21174   LABEL_NUSES (label) = 1;
21175
21176   emit_move_insn (operand0, res);
21177 }
21178
21179 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21180    into OPERAND0.  */
21181 void
21182 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
21183 {
21184   /* C code for the stuff we expand below.
21185         double xa = fabs (x), x2;
21186         if (!isless (xa, TWO52))
21187           return x;
21188         xa = xa + TWO52 - TWO52;
21189         x2 = copysign (xa, x);
21190      Compensate.  Floor:
21191         if (x2 > x)
21192           x2 -= 1;
21193      Compensate.  Ceil:
21194         if (x2 < x)
21195           x2 -= -1;
21196         return x2;
21197    */
21198   enum machine_mode mode = GET_MODE (operand0);
21199   rtx xa, TWO52, tmp, label, one, res, mask;
21200
21201   TWO52 = ix86_gen_TWO52 (mode);
21202
21203   /* Temporary for holding the result, initialized to the input
21204      operand to ease control flow.  */
21205   res = gen_reg_rtx (mode);
21206   emit_move_insn (res, operand1);
21207
21208   /* xa = abs (operand1) */
21209   xa = ix86_expand_sse_fabs (res, &mask);
21210
21211   /* if (!isless (xa, TWO52)) goto label; */
21212   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21213
21214   /* xa = xa + TWO52 - TWO52; */
21215   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21216   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21217
21218   /* xa = copysign (xa, operand1) */
21219   ix86_sse_copysign_to_positive (xa, xa, res, mask);
21220
21221   /* generate 1.0 or -1.0 */
21222   one = force_reg (mode,
21223                    const_double_from_real_value (do_floor
21224                                                  ? dconst1 : dconstm1, mode));
21225
21226   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21227   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21228   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21229                           gen_rtx_AND (mode, one, tmp)));
21230   /* We always need to subtract here to preserve signed zero.  */
21231   tmp = expand_simple_binop (mode, MINUS,
21232                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21233   emit_move_insn (res, tmp);
21234
21235   emit_label (label);
21236   LABEL_NUSES (label) = 1;
21237
21238   emit_move_insn (operand0, res);
21239 }
21240
21241 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21242    into OPERAND0.  */
21243 void
21244 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
21245 {
21246   /* C code for the stuff we expand below.
21247         double xa = fabs (x), x2;
21248         if (!isless (xa, TWO52))
21249           return x;
21250         x2 = (double)(long)x;
21251      Compensate.  Floor:
21252         if (x2 > x)
21253           x2 -= 1;
21254      Compensate.  Ceil:
21255         if (x2 < x)
21256           x2 += 1;
21257         if (HONOR_SIGNED_ZEROS (mode))
21258           return copysign (x2, x);
21259         return x2;
21260    */
21261   enum machine_mode mode = GET_MODE (operand0);
21262   rtx xa, xi, TWO52, tmp, label, one, res, mask;
21263
21264   TWO52 = ix86_gen_TWO52 (mode);
21265
21266   /* Temporary for holding the result, initialized to the input
21267      operand to ease control flow.  */
21268   res = gen_reg_rtx (mode);
21269   emit_move_insn (res, operand1);
21270
21271   /* xa = abs (operand1) */
21272   xa = ix86_expand_sse_fabs (res, &mask);
21273
21274   /* if (!isless (xa, TWO52)) goto label; */
21275   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21276
21277   /* xa = (double)(long)x */
21278   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21279   expand_fix (xi, res, 0);
21280   expand_float (xa, xi, 0);
21281
21282   /* generate 1.0 */
21283   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21284
21285   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21286   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21287   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21288                           gen_rtx_AND (mode, one, tmp)));
21289   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
21290                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21291   emit_move_insn (res, tmp);
21292
21293   if (HONOR_SIGNED_ZEROS (mode))
21294     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21295
21296   emit_label (label);
21297   LABEL_NUSES (label) = 1;
21298
21299   emit_move_insn (operand0, res);
21300 }
21301
21302 /* Expand SSE sequence for computing round from OPERAND1 storing
21303    into OPERAND0.  Sequence that works without relying on DImode truncation
21304    via cvttsd2siq that is only available on 64bit targets.  */
21305 void
21306 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
21307 {
21308   /* C code for the stuff we expand below.
21309         double xa = fabs (x), xa2, x2;
21310         if (!isless (xa, TWO52))
21311           return x;
21312      Using the absolute value and copying back sign makes
21313      -0.0 -> -0.0 correct.
21314         xa2 = xa + TWO52 - TWO52;
21315      Compensate.
21316         dxa = xa2 - xa;
21317         if (dxa <= -0.5)
21318           xa2 += 1;
21319         else if (dxa > 0.5)
21320           xa2 -= 1;
21321         x2 = copysign (xa2, x);
21322         return x2;
21323    */
21324   enum machine_mode mode = GET_MODE (operand0);
21325   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
21326
21327   TWO52 = ix86_gen_TWO52 (mode);
21328
21329   /* Temporary for holding the result, initialized to the input
21330      operand to ease control flow.  */
21331   res = gen_reg_rtx (mode);
21332   emit_move_insn (res, operand1);
21333
21334   /* xa = abs (operand1) */
21335   xa = ix86_expand_sse_fabs (res, &mask);
21336
21337   /* if (!isless (xa, TWO52)) goto label; */
21338   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21339
21340   /* xa2 = xa + TWO52 - TWO52; */
21341   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21342   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
21343
21344   /* dxa = xa2 - xa; */
21345   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
21346
21347   /* generate 0.5, 1.0 and -0.5 */
21348   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
21349   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
21350   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
21351                                0, OPTAB_DIRECT);
21352
21353   /* Compensate.  */
21354   tmp = gen_reg_rtx (mode);
21355   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
21356   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
21357   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21358                           gen_rtx_AND (mode, one, tmp)));
21359   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21360   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
21361   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
21362   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21363                           gen_rtx_AND (mode, one, tmp)));
21364   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21365
21366   /* res = copysign (xa2, operand1) */
21367   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
21368
21369   emit_label (label);
21370   LABEL_NUSES (label) = 1;
21371
21372   emit_move_insn (operand0, res);
21373 }
21374
21375 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21376    into OPERAND0.  */
21377 void
21378 ix86_expand_trunc (rtx operand0, rtx operand1)
21379 {
21380   /* C code for SSE variant we expand below.
21381         double xa = fabs (x), x2;
21382         if (!isless (xa, TWO52))
21383           return x;
21384         x2 = (double)(long)x;
21385         if (HONOR_SIGNED_ZEROS (mode))
21386           return copysign (x2, x);
21387         return x2;
21388    */
21389   enum machine_mode mode = GET_MODE (operand0);
21390   rtx xa, xi, TWO52, label, res, mask;
21391
21392   TWO52 = ix86_gen_TWO52 (mode);
21393
21394   /* Temporary for holding the result, initialized to the input
21395      operand to ease control flow.  */
21396   res = gen_reg_rtx (mode);
21397   emit_move_insn (res, operand1);
21398
21399   /* xa = abs (operand1) */
21400   xa = ix86_expand_sse_fabs (res, &mask);
21401
21402   /* if (!isless (xa, TWO52)) goto label; */
21403   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21404
21405   /* x = (double)(long)x */
21406   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21407   expand_fix (xi, res, 0);
21408   expand_float (res, xi, 0);
21409
21410   if (HONOR_SIGNED_ZEROS (mode))
21411     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21412
21413   emit_label (label);
21414   LABEL_NUSES (label) = 1;
21415
21416   emit_move_insn (operand0, res);
21417 }
21418
21419 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21420    into OPERAND0.  */
21421 void
21422 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
21423 {
21424   enum machine_mode mode = GET_MODE (operand0);
21425   rtx xa, mask, TWO52, label, one, res, smask, tmp;
21426
21427   /* C code for SSE variant we expand below.
21428         double xa = fabs (x), x2;
21429         if (!isless (xa, TWO52))
21430           return x;
21431         xa2 = xa + TWO52 - TWO52;
21432      Compensate:
21433         if (xa2 > xa)
21434           xa2 -= 1.0;
21435         x2 = copysign (xa2, x);
21436         return x2;
21437    */
21438
21439   TWO52 = ix86_gen_TWO52 (mode);
21440
21441   /* Temporary for holding the result, initialized to the input
21442      operand to ease control flow.  */
21443   res = gen_reg_rtx (mode);
21444   emit_move_insn (res, operand1);
21445
21446   /* xa = abs (operand1) */
21447   xa = ix86_expand_sse_fabs (res, &smask);
21448
21449   /* if (!isless (xa, TWO52)) goto label; */
21450   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21451
21452   /* res = xa + TWO52 - TWO52; */
21453   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21454   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
21455   emit_move_insn (res, tmp);
21456
21457   /* generate 1.0 */
21458   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21459
21460   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
21461   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
21462   emit_insn (gen_rtx_SET (VOIDmode, mask,
21463                           gen_rtx_AND (mode, mask, one)));
21464   tmp = expand_simple_binop (mode, MINUS,
21465                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
21466   emit_move_insn (res, tmp);
21467
21468   /* res = copysign (res, operand1) */
21469   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
21470
21471   emit_label (label);
21472   LABEL_NUSES (label) = 1;
21473
21474   emit_move_insn (operand0, res);
21475 }
21476
21477 /* Expand SSE sequence for computing round from OPERAND1 storing
21478    into OPERAND0.  */
21479 void
21480 ix86_expand_round (rtx operand0, rtx operand1)
21481 {
21482   /* C code for the stuff we're doing below:
21483         double xa = fabs (x);
21484         if (!isless (xa, TWO52))
21485           return x;
21486         xa = (double)(long)(xa + nextafter (0.5, 0.0));
21487         return copysign (xa, x);
21488    */
21489   enum machine_mode mode = GET_MODE (operand0);
21490   rtx res, TWO52, xa, label, xi, half, mask;
21491   const struct real_format *fmt;
21492   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21493
21494   /* Temporary for holding the result, initialized to the input
21495      operand to ease control flow.  */
21496   res = gen_reg_rtx (mode);
21497   emit_move_insn (res, operand1);
21498
21499   TWO52 = ix86_gen_TWO52 (mode);
21500   xa = ix86_expand_sse_fabs (res, &mask);
21501   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21502
21503   /* load nextafter (0.5, 0.0) */
21504   fmt = REAL_MODE_FORMAT (mode);
21505   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21506   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21507
21508   /* xa = xa + 0.5 */
21509   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
21510   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
21511
21512   /* xa = (double)(int64_t)xa */
21513   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21514   expand_fix (xi, xa, 0);
21515   expand_float (xa, xi, 0);
21516
21517   /* res = copysign (xa, operand1) */
21518   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
21519
21520   emit_label (label);
21521   LABEL_NUSES (label) = 1;
21522
21523   emit_move_insn (operand0, res);
21524 }
21525
21526 \f
21527 /* Table of valid machine attributes.  */
21528 static const struct attribute_spec ix86_attribute_table[] =
21529 {
21530   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
21531   /* Stdcall attribute says callee is responsible for popping arguments
21532      if they are not variable.  */
21533   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21534   /* Fastcall attribute says callee is responsible for popping arguments
21535      if they are not variable.  */
21536   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21537   /* Cdecl attribute says the callee is a normal C declaration */
21538   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21539   /* Regparm attribute specifies how many integer arguments are to be
21540      passed in registers.  */
21541   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
21542   /* Sseregparm attribute says we are using x86_64 calling conventions
21543      for FP arguments.  */
21544   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
21545   /* force_align_arg_pointer says this function realigns the stack at entry.  */
21546   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
21547     false, true,  true, ix86_handle_cconv_attribute },
21548 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21549   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
21550   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
21551   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
21552 #endif
21553   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21554   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21555 #ifdef SUBTARGET_ATTRIBUTE_TABLE
21556   SUBTARGET_ATTRIBUTE_TABLE,
21557 #endif
21558   { NULL,        0, 0, false, false, false, NULL }
21559 };
21560
21561 /* Initialize the GCC target structure.  */
21562 #undef TARGET_ATTRIBUTE_TABLE
21563 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
21564 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21565 #  undef TARGET_MERGE_DECL_ATTRIBUTES
21566 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
21567 #endif
21568
21569 #undef TARGET_COMP_TYPE_ATTRIBUTES
21570 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
21571
21572 #undef TARGET_INIT_BUILTINS
21573 #define TARGET_INIT_BUILTINS ix86_init_builtins
21574 #undef TARGET_EXPAND_BUILTIN
21575 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
21576
21577 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
21578 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION ix86_builtin_vectorized_function
21579 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
21580 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_builtin_conversion
21581
21582 #undef TARGET_ASM_FUNCTION_EPILOGUE
21583 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
21584
21585 #undef TARGET_ENCODE_SECTION_INFO
21586 #ifndef SUBTARGET_ENCODE_SECTION_INFO
21587 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
21588 #else
21589 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
21590 #endif
21591
21592 #undef TARGET_ASM_OPEN_PAREN
21593 #define TARGET_ASM_OPEN_PAREN ""
21594 #undef TARGET_ASM_CLOSE_PAREN
21595 #define TARGET_ASM_CLOSE_PAREN ""
21596
21597 #undef TARGET_ASM_ALIGNED_HI_OP
21598 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
21599 #undef TARGET_ASM_ALIGNED_SI_OP
21600 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
21601 #ifdef ASM_QUAD
21602 #undef TARGET_ASM_ALIGNED_DI_OP
21603 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
21604 #endif
21605
21606 #undef TARGET_ASM_UNALIGNED_HI_OP
21607 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
21608 #undef TARGET_ASM_UNALIGNED_SI_OP
21609 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
21610 #undef TARGET_ASM_UNALIGNED_DI_OP
21611 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
21612
21613 #undef TARGET_SCHED_ADJUST_COST
21614 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
21615 #undef TARGET_SCHED_ISSUE_RATE
21616 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
21617 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
21618 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
21619   ia32_multipass_dfa_lookahead
21620
21621 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
21622 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
21623
21624 #ifdef HAVE_AS_TLS
21625 #undef TARGET_HAVE_TLS
21626 #define TARGET_HAVE_TLS true
21627 #endif
21628 #undef TARGET_CANNOT_FORCE_CONST_MEM
21629 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
21630 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
21631 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
21632
21633 #undef TARGET_DELEGITIMIZE_ADDRESS
21634 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
21635
21636 #undef TARGET_MS_BITFIELD_LAYOUT_P
21637 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
21638
21639 #if TARGET_MACHO
21640 #undef TARGET_BINDS_LOCAL_P
21641 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
21642 #endif
21643
21644 #undef TARGET_ASM_OUTPUT_MI_THUNK
21645 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
21646 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
21647 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
21648
21649 #undef TARGET_ASM_FILE_START
21650 #define TARGET_ASM_FILE_START x86_file_start
21651
21652 #undef TARGET_DEFAULT_TARGET_FLAGS
21653 #define TARGET_DEFAULT_TARGET_FLAGS     \
21654   (TARGET_DEFAULT                       \
21655    | TARGET_64BIT_DEFAULT               \
21656    | TARGET_SUBTARGET_DEFAULT           \
21657    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
21658
21659 #undef TARGET_HANDLE_OPTION
21660 #define TARGET_HANDLE_OPTION ix86_handle_option
21661
21662 #undef TARGET_RTX_COSTS
21663 #define TARGET_RTX_COSTS ix86_rtx_costs
21664 #undef TARGET_ADDRESS_COST
21665 #define TARGET_ADDRESS_COST ix86_address_cost
21666
21667 #undef TARGET_FIXED_CONDITION_CODE_REGS
21668 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
21669 #undef TARGET_CC_MODES_COMPATIBLE
21670 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
21671
21672 #undef TARGET_MACHINE_DEPENDENT_REORG
21673 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
21674
21675 #undef TARGET_BUILD_BUILTIN_VA_LIST
21676 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
21677
21678 #undef TARGET_MD_ASM_CLOBBERS
21679 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
21680
21681 #undef TARGET_PROMOTE_PROTOTYPES
21682 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
21683 #undef TARGET_STRUCT_VALUE_RTX
21684 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
21685 #undef TARGET_SETUP_INCOMING_VARARGS
21686 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
21687 #undef TARGET_MUST_PASS_IN_STACK
21688 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
21689 #undef TARGET_PASS_BY_REFERENCE
21690 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
21691 #undef TARGET_INTERNAL_ARG_POINTER
21692 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
21693 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
21694 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
21695
21696 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
21697 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
21698
21699 #undef TARGET_SCALAR_MODE_SUPPORTED_P
21700 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
21701
21702 #undef TARGET_VECTOR_MODE_SUPPORTED_P
21703 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
21704
21705 #ifdef HAVE_AS_TLS
21706 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
21707 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
21708 #endif
21709
21710 #ifdef SUBTARGET_INSERT_ATTRIBUTES
21711 #undef TARGET_INSERT_ATTRIBUTES
21712 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
21713 #endif
21714
21715 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
21716 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
21717
21718 #undef TARGET_STACK_PROTECT_FAIL
21719 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
21720
21721 #undef TARGET_FUNCTION_VALUE
21722 #define TARGET_FUNCTION_VALUE ix86_function_value
21723
21724 struct gcc_target targetm = TARGET_INITIALIZER;
21725 \f
21726 #include "gt-i386.h"