OSDN Git Service

gcc/
[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_PROMOTE_HIMODE_IMUL: Modern CPUs have same latency for HImode
1204      and SImode multiply, but 386 and 486 do HImode multiply faster.  */
1205   ~(m_386 | m_486),
1206
1207   /* X86_TUNE_SLOW_IMUL_IMM32_MEM: Imul of 32-bit constant and memory is
1208      vector path on AMD machines.  */
1209   m_K8 | m_GENERIC64 | m_AMDFAM10,
1210
1211   /* X86_TUNE_SLOW_IMUL_IMM8: Imul of 8-bit constant is vector path on AMD
1212      machines.  */
1213   m_K8 | m_GENERIC64 | m_AMDFAM10,
1214
1215   /* X86_TUNE_MOVE_M1_VIA_OR: On pentiums, it is faster to load -1 via OR
1216      than a MOV.  */
1217   m_PENT,
1218
1219   /* X86_TUNE_NOT_UNPAIRABLE: NOT is not pairable on Pentium, while XOR is,
1220      but one byte longer.  */
1221   m_PENT,
1222
1223   /* X86_TUNE_NOT_VECTORMODE: On AMD K6, NOT is vector decoded with memory
1224      operand that cannot be represented using a modRM byte.  The XOR
1225      replacement is long decoded, so this split helps here as well.  */
1226   m_K6,
1227 };
1228
1229 /* Feature tests against the various architecture variations.  */
1230 unsigned int ix86_arch_features[X86_ARCH_LAST] = {
1231   /* X86_ARCH_CMOVE */
1232   m_PPRO | m_GEODE | m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA,
1233
1234   /* X86_ARCH_CMPXCHG: Compare and exchange was added for 80486.  */
1235   ~m_386,
1236
1237   /* X86_ARCH_CMPXCHG8B: Compare and exchange 8 bytes was added for pentium. */
1238   ~(m_386 | m_486),
1239
1240   /* X86_ARCH_XADD: Exchange and add was added for 80486.  */
1241   ~m_386,
1242
1243   /* X86_ARCH_BSWAP: Byteswap was added for 80486.  */
1244   ~m_386,
1245 };
1246
1247 static const unsigned int x86_accumulate_outgoing_args
1248   = m_ATHLON_K8_AMDFAM10 | m_PENT4 | m_NOCONA | m_PPRO | m_CORE2 | m_GENERIC;
1249
1250 static const unsigned int x86_arch_always_fancy_math_387
1251   = m_PENT | m_PPRO | m_ATHLON_K8_AMDFAM10 | m_PENT4
1252     | m_NOCONA | m_CORE2 | m_GENERIC;
1253
1254 static enum stringop_alg stringop_alg = no_stringop;
1255
1256 /* In case the average insn count for single function invocation is
1257    lower than this constant, emit fast (but longer) prologue and
1258    epilogue code.  */
1259 #define FAST_PROLOGUE_INSN_COUNT 20
1260
1261 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
1262 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
1263 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
1264 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
1265
1266 /* Array of the smallest class containing reg number REGNO, indexed by
1267    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
1268
1269 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
1270 {
1271   /* ax, dx, cx, bx */
1272   AREG, DREG, CREG, BREG,
1273   /* si, di, bp, sp */
1274   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
1275   /* FP registers */
1276   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
1277   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
1278   /* arg pointer */
1279   NON_Q_REGS,
1280   /* flags, fpsr, fpcr, frame */
1281   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
1282   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1283   SSE_REGS, SSE_REGS,
1284   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
1285   MMX_REGS, MMX_REGS,
1286   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1287   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
1288   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
1289   SSE_REGS, SSE_REGS,
1290 };
1291
1292 /* The "default" register map used in 32bit mode.  */
1293
1294 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
1295 {
1296   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
1297   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
1298   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1299   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
1300   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
1301   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1302   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1303 };
1304
1305 static int const x86_64_int_parameter_registers[6] =
1306 {
1307   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
1308   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1309 };
1310
1311 static int const x86_64_ms_abi_int_parameter_registers[4] =
1312 {
1313   2 /*RCX*/, 1 /*RDX*/,
1314   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
1315 };
1316
1317 static int const x86_64_int_return_registers[4] =
1318 {
1319   0 /*RAX*/, 1 /*RDX*/, 5 /*RDI*/, 4 /*RSI*/
1320 };
1321
1322 /* The "default" register map used in 64bit mode.  */
1323 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
1324 {
1325   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
1326   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
1327   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, fpcr, frame */
1328   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
1329   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
1330   8,9,10,11,12,13,14,15,                /* extended integer registers */
1331   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
1332 };
1333
1334 /* Define the register numbers to be used in Dwarf debugging information.
1335    The SVR4 reference port C compiler uses the following register numbers
1336    in its Dwarf output code:
1337         0 for %eax (gcc regno = 0)
1338         1 for %ecx (gcc regno = 2)
1339         2 for %edx (gcc regno = 1)
1340         3 for %ebx (gcc regno = 3)
1341         4 for %esp (gcc regno = 7)
1342         5 for %ebp (gcc regno = 6)
1343         6 for %esi (gcc regno = 4)
1344         7 for %edi (gcc regno = 5)
1345    The following three DWARF register numbers are never generated by
1346    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
1347    believes these numbers have these meanings.
1348         8  for %eip    (no gcc equivalent)
1349         9  for %eflags (gcc regno = 17)
1350         10 for %trapno (no gcc equivalent)
1351    It is not at all clear how we should number the FP stack registers
1352    for the x86 architecture.  If the version of SDB on x86/svr4 were
1353    a bit less brain dead with respect to floating-point then we would
1354    have a precedent to follow with respect to DWARF register numbers
1355    for x86 FP registers, but the SDB on x86/svr4 is so completely
1356    broken with respect to FP registers that it is hardly worth thinking
1357    of it as something to strive for compatibility with.
1358    The version of x86/svr4 SDB I have at the moment does (partially)
1359    seem to believe that DWARF register number 11 is associated with
1360    the x86 register %st(0), but that's about all.  Higher DWARF
1361    register numbers don't seem to be associated with anything in
1362    particular, and even for DWARF regno 11, SDB only seems to under-
1363    stand that it should say that a variable lives in %st(0) (when
1364    asked via an `=' command) if we said it was in DWARF regno 11,
1365    but SDB still prints garbage when asked for the value of the
1366    variable in question (via a `/' command).
1367    (Also note that the labels SDB prints for various FP stack regs
1368    when doing an `x' command are all wrong.)
1369    Note that these problems generally don't affect the native SVR4
1370    C compiler because it doesn't allow the use of -O with -g and
1371    because when it is *not* optimizing, it allocates a memory
1372    location for each floating-point variable, and the memory
1373    location is what gets described in the DWARF AT_location
1374    attribute for the variable in question.
1375    Regardless of the severe mental illness of the x86/svr4 SDB, we
1376    do something sensible here and we use the following DWARF
1377    register numbers.  Note that these are all stack-top-relative
1378    numbers.
1379         11 for %st(0) (gcc regno = 8)
1380         12 for %st(1) (gcc regno = 9)
1381         13 for %st(2) (gcc regno = 10)
1382         14 for %st(3) (gcc regno = 11)
1383         15 for %st(4) (gcc regno = 12)
1384         16 for %st(5) (gcc regno = 13)
1385         17 for %st(6) (gcc regno = 14)
1386         18 for %st(7) (gcc regno = 15)
1387 */
1388 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1389 {
1390   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1391   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1392   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, fpcr, frame */
1393   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1394   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1395   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1396   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1397 };
1398
1399 /* Test and compare insns in i386.md store the information needed to
1400    generate branch and scc insns here.  */
1401
1402 rtx ix86_compare_op0 = NULL_RTX;
1403 rtx ix86_compare_op1 = NULL_RTX;
1404 rtx ix86_compare_emitted = NULL_RTX;
1405
1406 /* Size of the register save area.  */
1407 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1408
1409 /* Define the structure for the machine field in struct function.  */
1410
1411 struct stack_local_entry GTY(())
1412 {
1413   unsigned short mode;
1414   unsigned short n;
1415   rtx rtl;
1416   struct stack_local_entry *next;
1417 };
1418
1419 /* Structure describing stack frame layout.
1420    Stack grows downward:
1421
1422    [arguments]
1423                                               <- ARG_POINTER
1424    saved pc
1425
1426    saved frame pointer if frame_pointer_needed
1427                                               <- HARD_FRAME_POINTER
1428    [saved regs]
1429
1430    [padding1]          \
1431                         )
1432    [va_arg registers]  (
1433                         > to_allocate         <- FRAME_POINTER
1434    [frame]             (
1435                         )
1436    [padding2]          /
1437   */
1438 struct ix86_frame
1439 {
1440   int nregs;
1441   int padding1;
1442   int va_arg_size;
1443   HOST_WIDE_INT frame;
1444   int padding2;
1445   int outgoing_arguments_size;
1446   int red_zone_size;
1447
1448   HOST_WIDE_INT to_allocate;
1449   /* The offsets relative to ARG_POINTER.  */
1450   HOST_WIDE_INT frame_pointer_offset;
1451   HOST_WIDE_INT hard_frame_pointer_offset;
1452   HOST_WIDE_INT stack_pointer_offset;
1453
1454   /* When save_regs_using_mov is set, emit prologue using
1455      move instead of push instructions.  */
1456   bool save_regs_using_mov;
1457 };
1458
1459 /* Code model option.  */
1460 enum cmodel ix86_cmodel;
1461 /* Asm dialect.  */
1462 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1463 /* TLS dialects.  */
1464 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1465
1466 /* Which unit we are generating floating point math for.  */
1467 enum fpmath_unit ix86_fpmath;
1468
1469 /* Which cpu are we scheduling for.  */
1470 enum processor_type ix86_tune;
1471
1472 /* Which instruction set architecture to use.  */
1473 enum processor_type ix86_arch;
1474
1475 /* true if sse prefetch instruction is not NOOP.  */
1476 int x86_prefetch_sse;
1477
1478 /* ix86_regparm_string as a number */
1479 static int ix86_regparm;
1480
1481 /* -mstackrealign option */
1482 extern int ix86_force_align_arg_pointer;
1483 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1484
1485 /* Preferred alignment for stack boundary in bits.  */
1486 unsigned int ix86_preferred_stack_boundary;
1487
1488 /* Values 1-5: see jump.c */
1489 int ix86_branch_cost;
1490
1491 /* Variables which are this size or smaller are put in the data/bss
1492    or ldata/lbss sections.  */
1493
1494 int ix86_section_threshold = 65536;
1495
1496 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1497 char internal_label_prefix[16];
1498 int internal_label_prefix_len;
1499
1500 /* Register class used for passing given 64bit part of the argument.
1501    These represent classes as documented by the PS ABI, with the exception
1502    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1503    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1504
1505    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1506    whenever possible (upper half does contain padding).  */
1507 enum x86_64_reg_class
1508   {
1509     X86_64_NO_CLASS,
1510     X86_64_INTEGER_CLASS,
1511     X86_64_INTEGERSI_CLASS,
1512     X86_64_SSE_CLASS,
1513     X86_64_SSESF_CLASS,
1514     X86_64_SSEDF_CLASS,
1515     X86_64_SSEUP_CLASS,
1516     X86_64_X87_CLASS,
1517     X86_64_X87UP_CLASS,
1518     X86_64_COMPLEX_X87_CLASS,
1519     X86_64_MEMORY_CLASS
1520   };
1521 static const char * const x86_64_reg_class_name[] =
1522 {
1523   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1524   "sseup", "x87", "x87up", "cplx87", "no"
1525 };
1526
1527 #define MAX_CLASSES 4
1528
1529 /* Table of constants used by fldpi, fldln2, etc....  */
1530 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1531 static bool ext_80387_constants_init = 0;
1532
1533 \f
1534 static struct machine_function * ix86_init_machine_status (void);
1535 static rtx ix86_function_value (tree, tree, bool);
1536 static int ix86_function_regparm (tree, tree);
1537 static void ix86_compute_frame_layout (struct ix86_frame *);
1538 static bool ix86_expand_vector_init_one_nonzero (bool, enum machine_mode,
1539                                                  rtx, rtx, int);
1540
1541 \f
1542 /* The svr4 ABI for the i386 says that records and unions are returned
1543    in memory.  */
1544 #ifndef DEFAULT_PCC_STRUCT_RETURN
1545 #define DEFAULT_PCC_STRUCT_RETURN 1
1546 #endif
1547
1548 /* Implement TARGET_HANDLE_OPTION.  */
1549
1550 static bool
1551 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1552 {
1553   switch (code)
1554     {
1555     case OPT_m3dnow:
1556       if (!value)
1557         {
1558           target_flags &= ~MASK_3DNOW_A;
1559           target_flags_explicit |= MASK_3DNOW_A;
1560         }
1561       return true;
1562
1563     case OPT_mmmx:
1564       if (!value)
1565         {
1566           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1567           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1568         }
1569       return true;
1570
1571     case OPT_msse:
1572       if (!value)
1573         {
1574           target_flags &= ~(MASK_SSE2 | MASK_SSE3 | MASK_SSE4A);
1575           target_flags_explicit |= MASK_SSE2 | MASK_SSE3 | MASK_SSE4A;
1576         }
1577       return true;
1578
1579     case OPT_msse2:
1580       if (!value)
1581         {
1582           target_flags &= ~(MASK_SSE3 | MASK_SSE4A);
1583           target_flags_explicit |= MASK_SSE3 | MASK_SSE4A;
1584         }
1585       return true;
1586
1587     case OPT_msse3:
1588       if (!value)
1589         {
1590           target_flags &= ~MASK_SSE4A;
1591           target_flags_explicit |= MASK_SSE4A;
1592         }
1593       return true;
1594
1595     default:
1596       return true;
1597     }
1598 }
1599
1600 /* Sometimes certain combinations of command options do not make
1601    sense on a particular target machine.  You can define a macro
1602    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1603    defined, is executed once just after all the command options have
1604    been parsed.
1605
1606    Don't use this macro to turn on various extra optimizations for
1607    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1608
1609 void
1610 override_options (void)
1611 {
1612   int i;
1613   int ix86_tune_defaulted = 0;
1614   unsigned int ix86_arch_mask, ix86_tune_mask;
1615
1616   /* Comes from final.c -- no real reason to change it.  */
1617 #define MAX_CODE_ALIGN 16
1618
1619   static struct ptt
1620     {
1621       const struct processor_costs *cost;       /* Processor costs */
1622       const int target_enable;                  /* Target flags to enable.  */
1623       const int target_disable;                 /* Target flags to disable.  */
1624       const int align_loop;                     /* Default alignments.  */
1625       const int align_loop_max_skip;
1626       const int align_jump;
1627       const int align_jump_max_skip;
1628       const int align_func;
1629     }
1630   const processor_target_table[PROCESSOR_max] =
1631     {
1632       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1633       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1634       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1635       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1636       {&geode_cost, 0, 0, 0, 0, 0, 0, 0},
1637       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1638       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1639       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1640       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1641       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1642       {&core2_cost, 0, 0, 16, 7, 16, 7, 16},
1643       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1644       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16},
1645       {&amdfam10_cost, 0, 0, 32, 7, 32, 7, 32}
1646     };
1647
1648   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1649   static struct pta
1650     {
1651       const char *const name;           /* processor name or nickname.  */
1652       const enum processor_type processor;
1653       const enum pta_flags
1654         {
1655           PTA_SSE = 1 << 0,
1656           PTA_SSE2 = 1 << 1,
1657           PTA_SSE3 = 1 << 2,
1658           PTA_MMX = 1 << 3,
1659           PTA_PREFETCH_SSE = 1 << 4,
1660           PTA_3DNOW = 1 << 5,
1661           PTA_3DNOW_A = 1 << 6,
1662           PTA_64BIT = 1 << 7,
1663           PTA_SSSE3 = 1 << 8,
1664           PTA_CX16 = 1 << 9,
1665           PTA_POPCNT = 1 << 10,
1666           PTA_ABM = 1 << 11,
1667           PTA_SSE4A = 1 << 12,
1668           PTA_NO_SAHF = 1 << 13
1669         } flags;
1670     }
1671   const processor_alias_table[] =
1672     {
1673       {"i386", PROCESSOR_I386, 0},
1674       {"i486", PROCESSOR_I486, 0},
1675       {"i586", PROCESSOR_PENTIUM, 0},
1676       {"pentium", PROCESSOR_PENTIUM, 0},
1677       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1678       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1679       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1680       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1681       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1682       {"i686", PROCESSOR_PENTIUMPRO, 0},
1683       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1684       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1685       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1686       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1687       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1688       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1689                                        | PTA_MMX | PTA_PREFETCH_SSE},
1690       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1691                                         | PTA_MMX | PTA_PREFETCH_SSE},
1692       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1693                                         | PTA_MMX | PTA_PREFETCH_SSE},
1694       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1695                                         | PTA_MMX | PTA_PREFETCH_SSE
1696                                         | PTA_CX16 | PTA_NO_SAHF},
1697       {"core2", PROCESSOR_CORE2, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_SSSE3
1698                                         | PTA_64BIT | PTA_MMX
1699                                         | PTA_PREFETCH_SSE | PTA_CX16},
1700       {"geode", PROCESSOR_GEODE, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1701                                    | PTA_3DNOW_A},
1702       {"k6", PROCESSOR_K6, PTA_MMX},
1703       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1704       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1705       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1706                                    | PTA_3DNOW_A},
1707       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1708                                          | PTA_3DNOW | PTA_3DNOW_A},
1709       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1710                                     | PTA_3DNOW_A | PTA_SSE},
1711       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1712                                       | PTA_3DNOW_A | PTA_SSE},
1713       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1714                                       | PTA_3DNOW_A | PTA_SSE},
1715       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1716                                | PTA_SSE | PTA_SSE2 | PTA_NO_SAHF},
1717       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1718                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2
1719                                       | PTA_NO_SAHF},
1720       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1721                                         | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1722                                         | PTA_SSE2 | PTA_NO_SAHF},
1723       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1724                                          | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1725                                          | PTA_SSE2 | PTA_NO_SAHF},
1726       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1727                                           | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1728                                           | PTA_SSE2 | PTA_NO_SAHF},
1729       {"amdfam10", PROCESSOR_AMDFAM10, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1730                                        | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1731                                        | PTA_SSE2 | PTA_SSE3 | PTA_POPCNT
1732                                        | PTA_ABM | PTA_SSE4A | PTA_CX16},
1733       {"barcelona", PROCESSOR_AMDFAM10, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1734                                        | PTA_64BIT | PTA_3DNOW_A | PTA_SSE
1735                                        | PTA_SSE2 | PTA_SSE3 | PTA_POPCNT
1736                                        | PTA_ABM | PTA_SSE4A | PTA_CX16},
1737       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1738       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1739     };
1740
1741   int const pta_size = ARRAY_SIZE (processor_alias_table);
1742
1743 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1744   SUBTARGET_OVERRIDE_OPTIONS;
1745 #endif
1746
1747 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1748   SUBSUBTARGET_OVERRIDE_OPTIONS;
1749 #endif
1750
1751   /* -fPIC is the default for x86_64.  */
1752   if (TARGET_MACHO && TARGET_64BIT)
1753     flag_pic = 2;
1754
1755   /* Set the default values for switches whose default depends on TARGET_64BIT
1756      in case they weren't overwritten by command line options.  */
1757   if (TARGET_64BIT)
1758     {
1759       /* Mach-O doesn't support omitting the frame pointer for now.  */
1760       if (flag_omit_frame_pointer == 2)
1761         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1762       if (flag_asynchronous_unwind_tables == 2)
1763         flag_asynchronous_unwind_tables = 1;
1764       if (flag_pcc_struct_return == 2)
1765         flag_pcc_struct_return = 0;
1766     }
1767   else
1768     {
1769       if (flag_omit_frame_pointer == 2)
1770         flag_omit_frame_pointer = 0;
1771       if (flag_asynchronous_unwind_tables == 2)
1772         flag_asynchronous_unwind_tables = 0;
1773       if (flag_pcc_struct_return == 2)
1774         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1775     }
1776
1777   /* Need to check -mtune=generic first.  */
1778   if (ix86_tune_string)
1779     {
1780       if (!strcmp (ix86_tune_string, "generic")
1781           || !strcmp (ix86_tune_string, "i686")
1782           /* As special support for cross compilers we read -mtune=native
1783              as -mtune=generic.  With native compilers we won't see the
1784              -mtune=native, as it was changed by the driver.  */
1785           || !strcmp (ix86_tune_string, "native"))
1786         {
1787           if (TARGET_64BIT)
1788             ix86_tune_string = "generic64";
1789           else
1790             ix86_tune_string = "generic32";
1791         }
1792       else if (!strncmp (ix86_tune_string, "generic", 7))
1793         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1794     }
1795   else
1796     {
1797       if (ix86_arch_string)
1798         ix86_tune_string = ix86_arch_string;
1799       if (!ix86_tune_string)
1800         {
1801           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1802           ix86_tune_defaulted = 1;
1803         }
1804
1805       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1806          need to use a sensible tune option.  */
1807       if (!strcmp (ix86_tune_string, "generic")
1808           || !strcmp (ix86_tune_string, "x86-64")
1809           || !strcmp (ix86_tune_string, "i686"))
1810         {
1811           if (TARGET_64BIT)
1812             ix86_tune_string = "generic64";
1813           else
1814             ix86_tune_string = "generic32";
1815         }
1816     }
1817   if (ix86_stringop_string)
1818     {
1819       if (!strcmp (ix86_stringop_string, "rep_byte"))
1820         stringop_alg = rep_prefix_1_byte;
1821       else if (!strcmp (ix86_stringop_string, "libcall"))
1822         stringop_alg = libcall;
1823       else if (!strcmp (ix86_stringop_string, "rep_4byte"))
1824         stringop_alg = rep_prefix_4_byte;
1825       else if (!strcmp (ix86_stringop_string, "rep_8byte"))
1826         stringop_alg = rep_prefix_8_byte;
1827       else if (!strcmp (ix86_stringop_string, "byte_loop"))
1828         stringop_alg = loop_1_byte;
1829       else if (!strcmp (ix86_stringop_string, "loop"))
1830         stringop_alg = loop;
1831       else if (!strcmp (ix86_stringop_string, "unrolled_loop"))
1832         stringop_alg = unrolled_loop;
1833       else
1834         error ("bad value (%s) for -mstringop-strategy= switch", ix86_stringop_string);
1835     }
1836   if (!strcmp (ix86_tune_string, "x86-64"))
1837     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1838              "-mtune=generic instead as appropriate.");
1839
1840   if (!ix86_arch_string)
1841     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1842   if (!strcmp (ix86_arch_string, "generic"))
1843     error ("generic CPU can be used only for -mtune= switch");
1844   if (!strncmp (ix86_arch_string, "generic", 7))
1845     error ("bad value (%s) for -march= switch", ix86_arch_string);
1846
1847   if (ix86_cmodel_string != 0)
1848     {
1849       if (!strcmp (ix86_cmodel_string, "small"))
1850         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1851       else if (!strcmp (ix86_cmodel_string, "medium"))
1852         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1853       else if (!strcmp (ix86_cmodel_string, "large"))
1854         ix86_cmodel = flag_pic ? CM_LARGE_PIC : CM_LARGE;
1855       else if (flag_pic)
1856         error ("code model %s does not support PIC mode", ix86_cmodel_string);
1857       else if (!strcmp (ix86_cmodel_string, "32"))
1858         ix86_cmodel = CM_32;
1859       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1860         ix86_cmodel = CM_KERNEL;
1861       else
1862         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1863     }
1864   else
1865     {
1866       /* For TARGET_64BIT_MS_ABI, force pic on, in order to enable the
1867          use of rip-relative addressing.  This eliminates fixups that
1868          would otherwise be needed if this object is to be placed in a
1869          DLL, and is essentially just as efficient as direct addressing.  */
1870       if (TARGET_64BIT_MS_ABI)
1871         ix86_cmodel = CM_SMALL_PIC, flag_pic = 1;
1872       else if (TARGET_64BIT)
1873         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1874       else
1875         ix86_cmodel = CM_32;
1876     }
1877   if (ix86_asm_string != 0)
1878     {
1879       if (! TARGET_MACHO
1880           && !strcmp (ix86_asm_string, "intel"))
1881         ix86_asm_dialect = ASM_INTEL;
1882       else if (!strcmp (ix86_asm_string, "att"))
1883         ix86_asm_dialect = ASM_ATT;
1884       else
1885         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1886     }
1887   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1888     error ("code model %qs not supported in the %s bit mode",
1889            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1890   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1891     sorry ("%i-bit mode not compiled in",
1892            (target_flags & MASK_64BIT) ? 64 : 32);
1893
1894   for (i = 0; i < pta_size; i++)
1895     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1896       {
1897         ix86_arch = processor_alias_table[i].processor;
1898         /* Default cpu tuning to the architecture.  */
1899         ix86_tune = ix86_arch;
1900         if (processor_alias_table[i].flags & PTA_MMX
1901             && !(target_flags_explicit & MASK_MMX))
1902           target_flags |= MASK_MMX;
1903         if (processor_alias_table[i].flags & PTA_3DNOW
1904             && !(target_flags_explicit & MASK_3DNOW))
1905           target_flags |= MASK_3DNOW;
1906         if (processor_alias_table[i].flags & PTA_3DNOW_A
1907             && !(target_flags_explicit & MASK_3DNOW_A))
1908           target_flags |= MASK_3DNOW_A;
1909         if (processor_alias_table[i].flags & PTA_SSE
1910             && !(target_flags_explicit & MASK_SSE))
1911           target_flags |= MASK_SSE;
1912         if (processor_alias_table[i].flags & PTA_SSE2
1913             && !(target_flags_explicit & MASK_SSE2))
1914           target_flags |= MASK_SSE2;
1915         if (processor_alias_table[i].flags & PTA_SSE3
1916             && !(target_flags_explicit & MASK_SSE3))
1917           target_flags |= MASK_SSE3;
1918         if (processor_alias_table[i].flags & PTA_SSSE3
1919             && !(target_flags_explicit & MASK_SSSE3))
1920           target_flags |= MASK_SSSE3;
1921         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1922           x86_prefetch_sse = true;
1923         if (processor_alias_table[i].flags & PTA_CX16)
1924           x86_cmpxchg16b = true;
1925         if (processor_alias_table[i].flags & PTA_POPCNT
1926             && !(target_flags_explicit & MASK_POPCNT))
1927           target_flags |= MASK_POPCNT;
1928         if (processor_alias_table[i].flags & PTA_ABM
1929             && !(target_flags_explicit & MASK_ABM))
1930           target_flags |= MASK_ABM;
1931         if (processor_alias_table[i].flags & PTA_SSE4A
1932             && !(target_flags_explicit & MASK_SSE4A))
1933           target_flags |= MASK_SSE4A;
1934         if (!(TARGET_64BIT && (processor_alias_table[i].flags & PTA_NO_SAHF)))
1935           x86_sahf = true;
1936         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1937           error ("CPU you selected does not support x86-64 "
1938                  "instruction set");
1939         break;
1940       }
1941
1942   if (i == pta_size)
1943     error ("bad value (%s) for -march= switch", ix86_arch_string);
1944
1945   ix86_arch_mask = 1u << ix86_arch;
1946   for (i = 0; i < X86_ARCH_LAST; ++i)
1947     ix86_arch_features[i] &= ix86_arch_mask;
1948
1949   for (i = 0; i < pta_size; i++)
1950     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1951       {
1952         ix86_tune = processor_alias_table[i].processor;
1953         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1954           {
1955             if (ix86_tune_defaulted)
1956               {
1957                 ix86_tune_string = "x86-64";
1958                 for (i = 0; i < pta_size; i++)
1959                   if (! strcmp (ix86_tune_string,
1960                                 processor_alias_table[i].name))
1961                     break;
1962                 ix86_tune = processor_alias_table[i].processor;
1963               }
1964             else
1965               error ("CPU you selected does not support x86-64 "
1966                      "instruction set");
1967           }
1968         /* Intel CPUs have always interpreted SSE prefetch instructions as
1969            NOPs; so, we can enable SSE prefetch instructions even when
1970            -mtune (rather than -march) points us to a processor that has them.
1971            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1972            higher processors.  */
1973         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1974           x86_prefetch_sse = true;
1975         break;
1976       }
1977   if (i == pta_size)
1978     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1979
1980   ix86_tune_mask = 1u << ix86_tune;
1981   for (i = 0; i < X86_TUNE_LAST; ++i)
1982     ix86_tune_features[i] &= ix86_tune_mask;
1983
1984   if (optimize_size)
1985     ix86_cost = &size_cost;
1986   else
1987     ix86_cost = processor_target_table[ix86_tune].cost;
1988   target_flags |= processor_target_table[ix86_tune].target_enable;
1989   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1990
1991   /* Arrange to set up i386_stack_locals for all functions.  */
1992   init_machine_status = ix86_init_machine_status;
1993
1994   /* Validate -mregparm= value.  */
1995   if (ix86_regparm_string)
1996     {
1997       if (TARGET_64BIT)
1998         warning (0, "-mregparm is ignored in 64-bit mode");
1999       i = atoi (ix86_regparm_string);
2000       if (i < 0 || i > REGPARM_MAX)
2001         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
2002       else
2003         ix86_regparm = i;
2004     }
2005   if (TARGET_64BIT)
2006     ix86_regparm = REGPARM_MAX;
2007
2008   /* If the user has provided any of the -malign-* options,
2009      warn and use that value only if -falign-* is not set.
2010      Remove this code in GCC 3.2 or later.  */
2011   if (ix86_align_loops_string)
2012     {
2013       warning (0, "-malign-loops is obsolete, use -falign-loops");
2014       if (align_loops == 0)
2015         {
2016           i = atoi (ix86_align_loops_string);
2017           if (i < 0 || i > MAX_CODE_ALIGN)
2018             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2019           else
2020             align_loops = 1 << i;
2021         }
2022     }
2023
2024   if (ix86_align_jumps_string)
2025     {
2026       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
2027       if (align_jumps == 0)
2028         {
2029           i = atoi (ix86_align_jumps_string);
2030           if (i < 0 || i > MAX_CODE_ALIGN)
2031             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2032           else
2033             align_jumps = 1 << i;
2034         }
2035     }
2036
2037   if (ix86_align_funcs_string)
2038     {
2039       warning (0, "-malign-functions is obsolete, use -falign-functions");
2040       if (align_functions == 0)
2041         {
2042           i = atoi (ix86_align_funcs_string);
2043           if (i < 0 || i > MAX_CODE_ALIGN)
2044             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
2045           else
2046             align_functions = 1 << i;
2047         }
2048     }
2049
2050   /* Default align_* from the processor table.  */
2051   if (align_loops == 0)
2052     {
2053       align_loops = processor_target_table[ix86_tune].align_loop;
2054       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
2055     }
2056   if (align_jumps == 0)
2057     {
2058       align_jumps = processor_target_table[ix86_tune].align_jump;
2059       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
2060     }
2061   if (align_functions == 0)
2062     {
2063       align_functions = processor_target_table[ix86_tune].align_func;
2064     }
2065
2066   /* Validate -mbranch-cost= value, or provide default.  */
2067   ix86_branch_cost = ix86_cost->branch_cost;
2068   if (ix86_branch_cost_string)
2069     {
2070       i = atoi (ix86_branch_cost_string);
2071       if (i < 0 || i > 5)
2072         error ("-mbranch-cost=%d is not between 0 and 5", i);
2073       else
2074         ix86_branch_cost = i;
2075     }
2076   if (ix86_section_threshold_string)
2077     {
2078       i = atoi (ix86_section_threshold_string);
2079       if (i < 0)
2080         error ("-mlarge-data-threshold=%d is negative", i);
2081       else
2082         ix86_section_threshold = i;
2083     }
2084
2085   if (ix86_tls_dialect_string)
2086     {
2087       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
2088         ix86_tls_dialect = TLS_DIALECT_GNU;
2089       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
2090         ix86_tls_dialect = TLS_DIALECT_GNU2;
2091       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
2092         ix86_tls_dialect = TLS_DIALECT_SUN;
2093       else
2094         error ("bad value (%s) for -mtls-dialect= switch",
2095                ix86_tls_dialect_string);
2096     }
2097
2098   /* Keep nonleaf frame pointers.  */
2099   if (flag_omit_frame_pointer)
2100     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
2101   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
2102     flag_omit_frame_pointer = 1;
2103
2104   /* If we're doing fast math, we don't care about comparison order
2105      wrt NaNs.  This lets us use a shorter comparison sequence.  */
2106   if (flag_finite_math_only)
2107     target_flags &= ~MASK_IEEE_FP;
2108
2109   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
2110      since the insns won't need emulation.  */
2111   if (x86_arch_always_fancy_math_387 & ix86_arch_mask)
2112     target_flags &= ~MASK_NO_FANCY_MATH_387;
2113
2114   /* Likewise, if the target doesn't have a 387, or we've specified
2115      software floating point, don't use 387 inline intrinsics.  */
2116   if (!TARGET_80387)
2117     target_flags |= MASK_NO_FANCY_MATH_387;
2118
2119   /* Turn on SSE3 builtins for -mssse3.  */
2120   if (TARGET_SSSE3)
2121     target_flags |= MASK_SSE3;
2122
2123   /* Turn on SSE3 builtins for -msse4a.  */
2124   if (TARGET_SSE4A)
2125     target_flags |= MASK_SSE3;
2126
2127   /* Turn on SSE2 builtins for -msse3.  */
2128   if (TARGET_SSE3)
2129     target_flags |= MASK_SSE2;
2130
2131   /* Turn on SSE builtins for -msse2.  */
2132   if (TARGET_SSE2)
2133     target_flags |= MASK_SSE;
2134
2135   /* Turn on MMX builtins for -msse.  */
2136   if (TARGET_SSE)
2137     {
2138       target_flags |= MASK_MMX & ~target_flags_explicit;
2139       x86_prefetch_sse = true;
2140     }
2141
2142   /* Turn on MMX builtins for 3Dnow.  */
2143   if (TARGET_3DNOW)
2144     target_flags |= MASK_MMX;
2145
2146   /* Turn on POPCNT builtins for -mabm.  */
2147   if (TARGET_ABM)
2148     target_flags |= MASK_POPCNT;
2149
2150   if (TARGET_64BIT)
2151     {
2152       if (TARGET_RTD)
2153         warning (0, "-mrtd is ignored in 64bit mode");
2154
2155       /* Enable by default the SSE and MMX builtins.  Do allow the user to
2156          explicitly disable any of these.  In particular, disabling SSE and
2157          MMX for kernel code is extremely useful.  */
2158       target_flags
2159         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | TARGET_SUBTARGET64_DEFAULT)
2160             & ~target_flags_explicit);
2161     }
2162   else
2163     {
2164       /* i386 ABI does not specify red zone.  It still makes sense to use it
2165          when programmer takes care to stack from being destroyed.  */
2166       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
2167         target_flags |= MASK_NO_RED_ZONE;
2168     }
2169
2170   /* Validate -mpreferred-stack-boundary= value, or provide default.
2171      The default of 128 bits is for Pentium III's SSE __m128.  We can't
2172      change it because of optimize_size.  Otherwise, we can't mix object
2173      files compiled with -Os and -On.  */
2174   ix86_preferred_stack_boundary = 128;
2175   if (ix86_preferred_stack_boundary_string)
2176     {
2177       i = atoi (ix86_preferred_stack_boundary_string);
2178       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
2179         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
2180                TARGET_64BIT ? 4 : 2);
2181       else
2182         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
2183     }
2184
2185   /* Accept -msseregparm only if at least SSE support is enabled.  */
2186   if (TARGET_SSEREGPARM
2187       && ! TARGET_SSE)
2188     error ("-msseregparm used without SSE enabled");
2189
2190   ix86_fpmath = TARGET_FPMATH_DEFAULT;
2191   if (ix86_fpmath_string != 0)
2192     {
2193       if (! strcmp (ix86_fpmath_string, "387"))
2194         ix86_fpmath = FPMATH_387;
2195       else if (! strcmp (ix86_fpmath_string, "sse"))
2196         {
2197           if (!TARGET_SSE)
2198             {
2199               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2200               ix86_fpmath = FPMATH_387;
2201             }
2202           else
2203             ix86_fpmath = FPMATH_SSE;
2204         }
2205       else if (! strcmp (ix86_fpmath_string, "387,sse")
2206                || ! strcmp (ix86_fpmath_string, "sse,387"))
2207         {
2208           if (!TARGET_SSE)
2209             {
2210               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2211               ix86_fpmath = FPMATH_387;
2212             }
2213           else if (!TARGET_80387)
2214             {
2215               warning (0, "387 instruction set disabled, using SSE arithmetics");
2216               ix86_fpmath = FPMATH_SSE;
2217             }
2218           else
2219             ix86_fpmath = FPMATH_SSE | FPMATH_387;
2220         }
2221       else
2222         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2223     }
2224
2225   /* If the i387 is disabled, then do not return values in it. */
2226   if (!TARGET_80387)
2227     target_flags &= ~MASK_FLOAT_RETURNS;
2228
2229   if ((x86_accumulate_outgoing_args & ix86_tune_mask)
2230       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2231       && !optimize_size)
2232     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2233
2234   /* ??? Unwind info is not correct around the CFG unless either a frame
2235      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2236      unwind info generation to be aware of the CFG and propagating states
2237      around edges.  */
2238   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2239        || flag_exceptions || flag_non_call_exceptions)
2240       && flag_omit_frame_pointer
2241       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2242     {
2243       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2244         warning (0, "unwind tables currently require either a frame pointer "
2245                  "or -maccumulate-outgoing-args for correctness");
2246       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2247     }
2248
2249   /* For sane SSE instruction set generation we need fcomi instruction.
2250      It is safe to enable all CMOVE instructions.  */
2251   if (TARGET_SSE)
2252     TARGET_CMOVE = 1;
2253
2254   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2255   {
2256     char *p;
2257     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2258     p = strchr (internal_label_prefix, 'X');
2259     internal_label_prefix_len = p - internal_label_prefix;
2260     *p = '\0';
2261   }
2262
2263   /* When scheduling description is not available, disable scheduler pass
2264      so it won't slow down the compilation and make x87 code slower.  */
2265   if (!TARGET_SCHEDULE)
2266     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2267
2268   if (!PARAM_SET_P (PARAM_SIMULTANEOUS_PREFETCHES))
2269     set_param_value ("simultaneous-prefetches",
2270                      ix86_cost->simultaneous_prefetches);
2271   if (!PARAM_SET_P (PARAM_L1_CACHE_LINE_SIZE))
2272     set_param_value ("l1-cache-line-size", ix86_cost->prefetch_block);
2273 }
2274 \f
2275 /* Return true if this goes in large data/bss.  */
2276
2277 static bool
2278 ix86_in_large_data_p (tree exp)
2279 {
2280   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
2281     return false;
2282
2283   /* Functions are never large data.  */
2284   if (TREE_CODE (exp) == FUNCTION_DECL)
2285     return false;
2286
2287   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
2288     {
2289       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
2290       if (strcmp (section, ".ldata") == 0
2291           || strcmp (section, ".lbss") == 0)
2292         return true;
2293       return false;
2294     }
2295   else
2296     {
2297       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
2298
2299       /* If this is an incomplete type with size 0, then we can't put it
2300          in data because it might be too big when completed.  */
2301       if (!size || size > ix86_section_threshold)
2302         return true;
2303     }
2304
2305   return false;
2306 }
2307
2308 /* Switch to the appropriate section for output of DECL.
2309    DECL is either a `VAR_DECL' node or a constant of some sort.
2310    RELOC indicates whether forming the initial value of DECL requires
2311    link-time relocations.  */
2312
2313 static section * x86_64_elf_select_section (tree, int, unsigned HOST_WIDE_INT)
2314         ATTRIBUTE_UNUSED;
2315
2316 static section *
2317 x86_64_elf_select_section (tree decl, int reloc,
2318                            unsigned HOST_WIDE_INT align)
2319 {
2320   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2321       && ix86_in_large_data_p (decl))
2322     {
2323       const char *sname = NULL;
2324       unsigned int flags = SECTION_WRITE;
2325       switch (categorize_decl_for_section (decl, reloc))
2326         {
2327         case SECCAT_DATA:
2328           sname = ".ldata";
2329           break;
2330         case SECCAT_DATA_REL:
2331           sname = ".ldata.rel";
2332           break;
2333         case SECCAT_DATA_REL_LOCAL:
2334           sname = ".ldata.rel.local";
2335           break;
2336         case SECCAT_DATA_REL_RO:
2337           sname = ".ldata.rel.ro";
2338           break;
2339         case SECCAT_DATA_REL_RO_LOCAL:
2340           sname = ".ldata.rel.ro.local";
2341           break;
2342         case SECCAT_BSS:
2343           sname = ".lbss";
2344           flags |= SECTION_BSS;
2345           break;
2346         case SECCAT_RODATA:
2347         case SECCAT_RODATA_MERGE_STR:
2348         case SECCAT_RODATA_MERGE_STR_INIT:
2349         case SECCAT_RODATA_MERGE_CONST:
2350           sname = ".lrodata";
2351           flags = 0;
2352           break;
2353         case SECCAT_SRODATA:
2354         case SECCAT_SDATA:
2355         case SECCAT_SBSS:
2356           gcc_unreachable ();
2357         case SECCAT_TEXT:
2358         case SECCAT_TDATA:
2359         case SECCAT_TBSS:
2360           /* We don't split these for medium model.  Place them into
2361              default sections and hope for best.  */
2362           break;
2363         }
2364       if (sname)
2365         {
2366           /* We might get called with string constants, but get_named_section
2367              doesn't like them as they are not DECLs.  Also, we need to set
2368              flags in that case.  */
2369           if (!DECL_P (decl))
2370             return get_section (sname, flags, NULL);
2371           return get_named_section (decl, sname, reloc);
2372         }
2373     }
2374   return default_elf_select_section (decl, reloc, align);
2375 }
2376
2377 /* Build up a unique section name, expressed as a
2378    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2379    RELOC indicates whether the initial value of EXP requires
2380    link-time relocations.  */
2381
2382 static void ATTRIBUTE_UNUSED
2383 x86_64_elf_unique_section (tree decl, int reloc)
2384 {
2385   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2386       && ix86_in_large_data_p (decl))
2387     {
2388       const char *prefix = NULL;
2389       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2390       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2391
2392       switch (categorize_decl_for_section (decl, reloc))
2393         {
2394         case SECCAT_DATA:
2395         case SECCAT_DATA_REL:
2396         case SECCAT_DATA_REL_LOCAL:
2397         case SECCAT_DATA_REL_RO:
2398         case SECCAT_DATA_REL_RO_LOCAL:
2399           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2400           break;
2401         case SECCAT_BSS:
2402           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2403           break;
2404         case SECCAT_RODATA:
2405         case SECCAT_RODATA_MERGE_STR:
2406         case SECCAT_RODATA_MERGE_STR_INIT:
2407         case SECCAT_RODATA_MERGE_CONST:
2408           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2409           break;
2410         case SECCAT_SRODATA:
2411         case SECCAT_SDATA:
2412         case SECCAT_SBSS:
2413           gcc_unreachable ();
2414         case SECCAT_TEXT:
2415         case SECCAT_TDATA:
2416         case SECCAT_TBSS:
2417           /* We don't split these for medium model.  Place them into
2418              default sections and hope for best.  */
2419           break;
2420         }
2421       if (prefix)
2422         {
2423           const char *name;
2424           size_t nlen, plen;
2425           char *string;
2426           plen = strlen (prefix);
2427
2428           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2429           name = targetm.strip_name_encoding (name);
2430           nlen = strlen (name);
2431
2432           string = alloca (nlen + plen + 1);
2433           memcpy (string, prefix, plen);
2434           memcpy (string + plen, name, nlen + 1);
2435
2436           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2437           return;
2438         }
2439     }
2440   default_unique_section (decl, reloc);
2441 }
2442
2443 #ifdef COMMON_ASM_OP
2444 /* This says how to output assembler code to declare an
2445    uninitialized external linkage data object.
2446
2447    For medium model x86-64 we need to use .largecomm opcode for
2448    large objects.  */
2449 void
2450 x86_elf_aligned_common (FILE *file,
2451                         const char *name, unsigned HOST_WIDE_INT size,
2452                         int align)
2453 {
2454   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2455       && size > (unsigned int)ix86_section_threshold)
2456     fprintf (file, ".largecomm\t");
2457   else
2458     fprintf (file, "%s", COMMON_ASM_OP);
2459   assemble_name (file, name);
2460   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2461            size, align / BITS_PER_UNIT);
2462 }
2463 #endif
2464
2465 /* Utility function for targets to use in implementing
2466    ASM_OUTPUT_ALIGNED_BSS.  */
2467
2468 void
2469 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2470                         const char *name, unsigned HOST_WIDE_INT size,
2471                         int align)
2472 {
2473   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2474       && size > (unsigned int)ix86_section_threshold)
2475     switch_to_section (get_named_section (decl, ".lbss", 0));
2476   else
2477     switch_to_section (bss_section);
2478   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2479 #ifdef ASM_DECLARE_OBJECT_NAME
2480   last_assemble_variable_decl = decl;
2481   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2482 #else
2483   /* Standard thing is just output label for the object.  */
2484   ASM_OUTPUT_LABEL (file, name);
2485 #endif /* ASM_DECLARE_OBJECT_NAME */
2486   ASM_OUTPUT_SKIP (file, size ? size : 1);
2487 }
2488 \f
2489 void
2490 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2491 {
2492   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2493      make the problem with not enough registers even worse.  */
2494 #ifdef INSN_SCHEDULING
2495   if (level > 1)
2496     flag_schedule_insns = 0;
2497 #endif
2498
2499   if (TARGET_MACHO)
2500     /* The Darwin libraries never set errno, so we might as well
2501        avoid calling them when that's the only reason we would.  */
2502     flag_errno_math = 0;
2503
2504   /* The default values of these switches depend on the TARGET_64BIT
2505      that is not known at this moment.  Mark these values with 2 and
2506      let user the to override these.  In case there is no command line option
2507      specifying them, we will set the defaults in override_options.  */
2508   if (optimize >= 1)
2509     flag_omit_frame_pointer = 2;
2510   flag_pcc_struct_return = 2;
2511   flag_asynchronous_unwind_tables = 2;
2512 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2513   SUBTARGET_OPTIMIZATION_OPTIONS;
2514 #endif
2515 }
2516 \f
2517 /* Decide whether we can make a sibling call to a function.  DECL is the
2518    declaration of the function being targeted by the call and EXP is the
2519    CALL_EXPR representing the call.  */
2520
2521 static bool
2522 ix86_function_ok_for_sibcall (tree decl, tree exp)
2523 {
2524   tree func;
2525   rtx a, b;
2526
2527   /* If we are generating position-independent code, we cannot sibcall
2528      optimize any indirect call, or a direct call to a global function,
2529      as the PLT requires %ebx be live.  */
2530   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2531     return false;
2532
2533   if (decl)
2534     func = decl;
2535   else
2536     {
2537       func = TREE_TYPE (CALL_EXPR_FN (exp));
2538       if (POINTER_TYPE_P (func))
2539         func = TREE_TYPE (func);
2540     }
2541
2542   /* Check that the return value locations are the same.  Like
2543      if we are returning floats on the 80387 register stack, we cannot
2544      make a sibcall from a function that doesn't return a float to a
2545      function that does or, conversely, from a function that does return
2546      a float to a function that doesn't; the necessary stack adjustment
2547      would not be executed.  This is also the place we notice
2548      differences in the return value ABI.  Note that it is ok for one
2549      of the functions to have void return type as long as the return
2550      value of the other is passed in a register.  */
2551   a = ix86_function_value (TREE_TYPE (exp), func, false);
2552   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2553                            cfun->decl, false);
2554   if (STACK_REG_P (a) || STACK_REG_P (b))
2555     {
2556       if (!rtx_equal_p (a, b))
2557         return false;
2558     }
2559   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2560     ;
2561   else if (!rtx_equal_p (a, b))
2562     return false;
2563
2564   /* If this call is indirect, we'll need to be able to use a call-clobbered
2565      register for the address of the target function.  Make sure that all
2566      such registers are not used for passing parameters.  */
2567   if (!decl && !TARGET_64BIT)
2568     {
2569       tree type;
2570
2571       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2572       type = CALL_EXPR_FN (exp);                /* pointer expression */
2573       type = TREE_TYPE (type);                  /* pointer type */
2574       type = TREE_TYPE (type);                  /* function type */
2575
2576       if (ix86_function_regparm (type, NULL) >= 3)
2577         {
2578           /* ??? Need to count the actual number of registers to be used,
2579              not the possible number of registers.  Fix later.  */
2580           return false;
2581         }
2582     }
2583
2584   /* Dllimport'd functions are also called indirectly.  */
2585   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
2586       && decl && DECL_DLLIMPORT_P (decl)
2587       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2588     return false;
2589
2590   /* If we forced aligned the stack, then sibcalling would unalign the
2591      stack, which may break the called function.  */
2592   if (cfun->machine->force_align_arg_pointer)
2593     return false;
2594
2595   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2596   return true;
2597 }
2598
2599 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2600    calling convention attributes;
2601    arguments as in struct attribute_spec.handler.  */
2602
2603 static tree
2604 ix86_handle_cconv_attribute (tree *node, tree name,
2605                                    tree args,
2606                                    int flags ATTRIBUTE_UNUSED,
2607                                    bool *no_add_attrs)
2608 {
2609   if (TREE_CODE (*node) != FUNCTION_TYPE
2610       && TREE_CODE (*node) != METHOD_TYPE
2611       && TREE_CODE (*node) != FIELD_DECL
2612       && TREE_CODE (*node) != TYPE_DECL)
2613     {
2614       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2615                IDENTIFIER_POINTER (name));
2616       *no_add_attrs = true;
2617       return NULL_TREE;
2618     }
2619
2620   /* Can combine regparm with all attributes but fastcall.  */
2621   if (is_attribute_p ("regparm", name))
2622     {
2623       tree cst;
2624
2625       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2626         {
2627           error ("fastcall and regparm attributes are not compatible");
2628         }
2629
2630       cst = TREE_VALUE (args);
2631       if (TREE_CODE (cst) != INTEGER_CST)
2632         {
2633           warning (OPT_Wattributes,
2634                    "%qs attribute requires an integer constant argument",
2635                    IDENTIFIER_POINTER (name));
2636           *no_add_attrs = true;
2637         }
2638       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2639         {
2640           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2641                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2642           *no_add_attrs = true;
2643         }
2644
2645       if (!TARGET_64BIT
2646           && lookup_attribute (ix86_force_align_arg_pointer_string,
2647                                TYPE_ATTRIBUTES (*node))
2648           && compare_tree_int (cst, REGPARM_MAX-1))
2649         {
2650           error ("%s functions limited to %d register parameters",
2651                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2652         }
2653
2654       return NULL_TREE;
2655     }
2656
2657   if (TARGET_64BIT)
2658     {
2659       /* Do not warn when emulating the MS ABI.  */
2660       if (!TARGET_64BIT_MS_ABI)
2661         warning (OPT_Wattributes, "%qs attribute ignored",
2662                  IDENTIFIER_POINTER (name));
2663       *no_add_attrs = true;
2664       return NULL_TREE;
2665     }
2666
2667   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2668   if (is_attribute_p ("fastcall", name))
2669     {
2670       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2671         {
2672           error ("fastcall and cdecl attributes are not compatible");
2673         }
2674       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2675         {
2676           error ("fastcall and stdcall attributes are not compatible");
2677         }
2678       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2679         {
2680           error ("fastcall and regparm attributes are not compatible");
2681         }
2682     }
2683
2684   /* Can combine stdcall with fastcall (redundant), regparm and
2685      sseregparm.  */
2686   else if (is_attribute_p ("stdcall", name))
2687     {
2688       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2689         {
2690           error ("stdcall and cdecl attributes are not compatible");
2691         }
2692       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2693         {
2694           error ("stdcall and fastcall attributes are not compatible");
2695         }
2696     }
2697
2698   /* Can combine cdecl with regparm and sseregparm.  */
2699   else if (is_attribute_p ("cdecl", name))
2700     {
2701       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2702         {
2703           error ("stdcall and cdecl attributes are not compatible");
2704         }
2705       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2706         {
2707           error ("fastcall and cdecl attributes are not compatible");
2708         }
2709     }
2710
2711   /* Can combine sseregparm with all attributes.  */
2712
2713   return NULL_TREE;
2714 }
2715
2716 /* Return 0 if the attributes for two types are incompatible, 1 if they
2717    are compatible, and 2 if they are nearly compatible (which causes a
2718    warning to be generated).  */
2719
2720 static int
2721 ix86_comp_type_attributes (tree type1, tree type2)
2722 {
2723   /* Check for mismatch of non-default calling convention.  */
2724   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2725
2726   if (TREE_CODE (type1) != FUNCTION_TYPE)
2727     return 1;
2728
2729   /* Check for mismatched fastcall/regparm types.  */
2730   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2731        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2732       || (ix86_function_regparm (type1, NULL)
2733           != ix86_function_regparm (type2, NULL)))
2734     return 0;
2735
2736   /* Check for mismatched sseregparm types.  */
2737   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2738       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2739     return 0;
2740
2741   /* Check for mismatched return types (cdecl vs stdcall).  */
2742   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2743       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2744     return 0;
2745
2746   return 1;
2747 }
2748 \f
2749 /* Return the regparm value for a function with the indicated TYPE and DECL.
2750    DECL may be NULL when calling function indirectly
2751    or considering a libcall.  */
2752
2753 static int
2754 ix86_function_regparm (tree type, tree decl)
2755 {
2756   tree attr;
2757   int regparm = ix86_regparm;
2758
2759   if (TARGET_64BIT)
2760     return regparm;
2761
2762   attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2763   if (attr)
2764     return TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2765
2766   if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2767     return 2;
2768
2769   /* Use register calling convention for local functions when possible.  */
2770   if (decl && flag_unit_at_a_time && !profile_flag)
2771     {
2772       struct cgraph_local_info *i = cgraph_local_info (decl);
2773       if (i && i->local)
2774         {
2775           int local_regparm, globals = 0, regno;
2776           struct function *f;
2777
2778           /* Make sure no regparm register is taken by a
2779              global register variable.  */
2780           for (local_regparm = 0; local_regparm < 3; local_regparm++)
2781             if (global_regs[local_regparm])
2782               break;
2783
2784           /* We can't use regparm(3) for nested functions as these use
2785              static chain pointer in third argument.  */
2786           if (local_regparm == 3
2787               && decl_function_context (decl)
2788               && !DECL_NO_STATIC_CHAIN (decl))
2789             local_regparm = 2;
2790
2791           /* If the function realigns its stackpointer, the prologue will
2792              clobber %ecx.  If we've already generated code for the callee,
2793              the callee DECL_STRUCT_FUNCTION is gone, so we fall back to
2794              scanning the attributes for the self-realigning property.  */
2795           f = DECL_STRUCT_FUNCTION (decl);
2796           if (local_regparm == 3
2797               && (f ? !!f->machine->force_align_arg_pointer
2798                   : !!lookup_attribute (ix86_force_align_arg_pointer_string,
2799                                         TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2800             local_regparm = 2;
2801
2802           /* Each global register variable increases register preassure,
2803              so the more global reg vars there are, the smaller regparm
2804              optimization use, unless requested by the user explicitly.  */
2805           for (regno = 0; regno < 6; regno++)
2806             if (global_regs[regno])
2807               globals++;
2808           local_regparm
2809             = globals < local_regparm ? local_regparm - globals : 0;
2810
2811           if (local_regparm > regparm)
2812             regparm = local_regparm;
2813         }
2814     }
2815
2816   return regparm;
2817 }
2818
2819 /* Return 1 or 2, if we can pass up to SSE_REGPARM_MAX SFmode (1) and
2820    DFmode (2) arguments in SSE registers for a function with the
2821    indicated TYPE and DECL.  DECL may be NULL when calling function
2822    indirectly or considering a libcall.  Otherwise return 0.  */
2823
2824 static int
2825 ix86_function_sseregparm (tree type, tree decl)
2826 {
2827   gcc_assert (!TARGET_64BIT);
2828
2829   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2830      by the sseregparm attribute.  */
2831   if (TARGET_SSEREGPARM
2832       || (type && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2833     {
2834       if (!TARGET_SSE)
2835         {
2836           if (decl)
2837             error ("Calling %qD with attribute sseregparm without "
2838                    "SSE/SSE2 enabled", decl);
2839           else
2840             error ("Calling %qT with attribute sseregparm without "
2841                    "SSE/SSE2 enabled", type);
2842           return 0;
2843         }
2844
2845       return 2;
2846     }
2847
2848   /* For local functions, pass up to SSE_REGPARM_MAX SFmode
2849      (and DFmode for SSE2) arguments in SSE registers.  */
2850   if (decl && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2851     {
2852       struct cgraph_local_info *i = cgraph_local_info (decl);
2853       if (i && i->local)
2854         return TARGET_SSE2 ? 2 : 1;
2855     }
2856
2857   return 0;
2858 }
2859
2860 /* Return true if EAX is live at the start of the function.  Used by
2861    ix86_expand_prologue to determine if we need special help before
2862    calling allocate_stack_worker.  */
2863
2864 static bool
2865 ix86_eax_live_at_start_p (void)
2866 {
2867   /* Cheat.  Don't bother working forward from ix86_function_regparm
2868      to the function type to whether an actual argument is located in
2869      eax.  Instead just look at cfg info, which is still close enough
2870      to correct at this point.  This gives false positives for broken
2871      functions that might use uninitialized data that happens to be
2872      allocated in eax, but who cares?  */
2873   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2874 }
2875
2876 /* Return true if TYPE has a variable argument list.  */
2877
2878 static bool
2879 type_has_variadic_args_p (tree type)
2880 {
2881   tree t;
2882
2883   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
2884     if (t == void_list_node)
2885       return false;
2886   return true;
2887 }
2888
2889 /* Value is the number of bytes of arguments automatically
2890    popped when returning from a subroutine call.
2891    FUNDECL is the declaration node of the function (as a tree),
2892    FUNTYPE is the data type of the function (as a tree),
2893    or for a library call it is an identifier node for the subroutine name.
2894    SIZE is the number of bytes of arguments passed on the stack.
2895
2896    On the 80386, the RTD insn may be used to pop them if the number
2897      of args is fixed, but if the number is variable then the caller
2898      must pop them all.  RTD can't be used for library calls now
2899      because the library is compiled with the Unix compiler.
2900    Use of RTD is a selectable option, since it is incompatible with
2901    standard Unix calling sequences.  If the option is not selected,
2902    the caller must always pop the args.
2903
2904    The attribute stdcall is equivalent to RTD on a per module basis.  */
2905
2906 int
2907 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2908 {
2909   int rtd;
2910
2911   /* None of the 64-bit ABIs pop arguments.  */
2912   if (TARGET_64BIT)
2913     return 0;
2914
2915   rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2916
2917   /* Cdecl functions override -mrtd, and never pop the stack.  */
2918   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype)))
2919     {
2920       /* Stdcall and fastcall functions will pop the stack if not
2921          variable args.  */
2922       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2923           || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2924         rtd = 1;
2925
2926       if (rtd && ! type_has_variadic_args_p (funtype))
2927         return size;
2928     }
2929
2930   /* Lose any fake structure return argument if it is passed on the stack.  */
2931   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2932       && !KEEP_AGGREGATE_RETURN_POINTER)
2933     {
2934       int nregs = ix86_function_regparm (funtype, fundecl);
2935       if (nregs == 0)
2936         return GET_MODE_SIZE (Pmode);
2937     }
2938
2939   return 0;
2940 }
2941 \f
2942 /* Argument support functions.  */
2943
2944 /* Return true when register may be used to pass function parameters.  */
2945 bool
2946 ix86_function_arg_regno_p (int regno)
2947 {
2948   int i;
2949   const int *parm_regs;
2950
2951   if (!TARGET_64BIT)
2952     {
2953       if (TARGET_MACHO)
2954         return (regno < REGPARM_MAX
2955                 || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
2956       else
2957         return (regno < REGPARM_MAX
2958                 || (TARGET_MMX && MMX_REGNO_P (regno)
2959                     && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2960                 || (TARGET_SSE && SSE_REGNO_P (regno)
2961                     && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2962     }
2963
2964   if (TARGET_MACHO)
2965     {
2966       if (SSE_REGNO_P (regno) && TARGET_SSE)
2967         return true;
2968     }
2969   else
2970     {
2971       if (TARGET_SSE && SSE_REGNO_P (regno)
2972           && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2973         return true;
2974     }
2975
2976   /* RAX is used as hidden argument to va_arg functions.  */
2977   if (!TARGET_64BIT_MS_ABI && regno == 0)
2978     return true;
2979
2980   if (TARGET_64BIT_MS_ABI)
2981     parm_regs = x86_64_ms_abi_int_parameter_registers;
2982   else
2983     parm_regs = x86_64_int_parameter_registers;
2984   for (i = 0; i < REGPARM_MAX; i++)
2985     if (regno == parm_regs[i])
2986       return true;
2987   return false;
2988 }
2989
2990 /* Return if we do not know how to pass TYPE solely in registers.  */
2991
2992 static bool
2993 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2994 {
2995   if (must_pass_in_stack_var_size_or_pad (mode, type))
2996     return true;
2997
2998   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2999      The layout_type routine is crafty and tries to trick us into passing
3000      currently unsupported vector types on the stack by using TImode.  */
3001   return (!TARGET_64BIT && mode == TImode
3002           && type && TREE_CODE (type) != VECTOR_TYPE);
3003 }
3004
3005 /* Initialize a variable CUM of type CUMULATIVE_ARGS
3006    for a call to a function whose data type is FNTYPE.
3007    For a library call, FNTYPE is 0.  */
3008
3009 void
3010 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
3011                       tree fntype,      /* tree ptr for function decl */
3012                       rtx libname,      /* SYMBOL_REF of library name or 0 */
3013                       tree fndecl)
3014 {
3015   memset (cum, 0, sizeof (*cum));
3016
3017   /* Set up the number of registers to use for passing arguments.  */
3018   cum->nregs = ix86_regparm;
3019   if (TARGET_SSE)
3020     cum->sse_nregs = SSE_REGPARM_MAX;
3021   if (TARGET_MMX)
3022     cum->mmx_nregs = MMX_REGPARM_MAX;
3023   cum->warn_sse = true;
3024   cum->warn_mmx = true;
3025   cum->maybe_vaarg = (fntype ? type_has_variadic_args_p (fntype) : !libname);
3026
3027   if (!TARGET_64BIT)
3028     {
3029       /* If there are variable arguments, then we won't pass anything
3030          in registers in 32-bit mode. */
3031       if (cum->maybe_vaarg)
3032         {
3033           cum->nregs = 0;
3034           cum->sse_nregs = 0;
3035           cum->mmx_nregs = 0;
3036           cum->warn_sse = 0;
3037           cum->warn_mmx = 0;
3038           return;
3039         }
3040
3041       /* Use ecx and edx registers if function has fastcall attribute,
3042          else look for regparm information.  */
3043       if (fntype)
3044         {
3045           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
3046             {
3047               cum->nregs = 2;
3048               cum->fastcall = 1;
3049             }
3050           else
3051             cum->nregs = ix86_function_regparm (fntype, fndecl);
3052         }
3053
3054       /* Set up the number of SSE registers used for passing SFmode
3055          and DFmode arguments.  Warn for mismatching ABI.  */
3056       cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
3057     }
3058 }
3059
3060 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
3061    But in the case of vector types, it is some vector mode.
3062
3063    When we have only some of our vector isa extensions enabled, then there
3064    are some modes for which vector_mode_supported_p is false.  For these
3065    modes, the generic vector support in gcc will choose some non-vector mode
3066    in order to implement the type.  By computing the natural mode, we'll
3067    select the proper ABI location for the operand and not depend on whatever
3068    the middle-end decides to do with these vector types.  */
3069
3070 static enum machine_mode
3071 type_natural_mode (tree type)
3072 {
3073   enum machine_mode mode = TYPE_MODE (type);
3074
3075   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
3076     {
3077       HOST_WIDE_INT size = int_size_in_bytes (type);
3078       if ((size == 8 || size == 16)
3079           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
3080           && TYPE_VECTOR_SUBPARTS (type) > 1)
3081         {
3082           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
3083
3084           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
3085             mode = MIN_MODE_VECTOR_FLOAT;
3086           else
3087             mode = MIN_MODE_VECTOR_INT;
3088
3089           /* Get the mode which has this inner mode and number of units.  */
3090           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3091             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
3092                 && GET_MODE_INNER (mode) == innermode)
3093               return mode;
3094
3095           gcc_unreachable ();
3096         }
3097     }
3098
3099   return mode;
3100 }
3101
3102 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
3103    this may not agree with the mode that the type system has chosen for the
3104    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
3105    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
3106
3107 static rtx
3108 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
3109                      unsigned int regno)
3110 {
3111   rtx tmp;
3112
3113   if (orig_mode != BLKmode)
3114     tmp = gen_rtx_REG (orig_mode, regno);
3115   else
3116     {
3117       tmp = gen_rtx_REG (mode, regno);
3118       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
3119       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
3120     }
3121
3122   return tmp;
3123 }
3124
3125 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
3126    of this code is to classify each 8bytes of incoming argument by the register
3127    class and assign registers accordingly.  */
3128
3129 /* Return the union class of CLASS1 and CLASS2.
3130    See the x86-64 PS ABI for details.  */
3131
3132 static enum x86_64_reg_class
3133 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
3134 {
3135   /* Rule #1: If both classes are equal, this is the resulting class.  */
3136   if (class1 == class2)
3137     return class1;
3138
3139   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
3140      the other class.  */
3141   if (class1 == X86_64_NO_CLASS)
3142     return class2;
3143   if (class2 == X86_64_NO_CLASS)
3144     return class1;
3145
3146   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
3147   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
3148     return X86_64_MEMORY_CLASS;
3149
3150   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
3151   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
3152       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
3153     return X86_64_INTEGERSI_CLASS;
3154   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
3155       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
3156     return X86_64_INTEGER_CLASS;
3157
3158   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
3159      MEMORY is used.  */
3160   if (class1 == X86_64_X87_CLASS
3161       || class1 == X86_64_X87UP_CLASS
3162       || class1 == X86_64_COMPLEX_X87_CLASS
3163       || class2 == X86_64_X87_CLASS
3164       || class2 == X86_64_X87UP_CLASS
3165       || class2 == X86_64_COMPLEX_X87_CLASS)
3166     return X86_64_MEMORY_CLASS;
3167
3168   /* Rule #6: Otherwise class SSE is used.  */
3169   return X86_64_SSE_CLASS;
3170 }
3171
3172 /* Classify the argument of type TYPE and mode MODE.
3173    CLASSES will be filled by the register class used to pass each word
3174    of the operand.  The number of words is returned.  In case the parameter
3175    should be passed in memory, 0 is returned. As a special case for zero
3176    sized containers, classes[0] will be NO_CLASS and 1 is returned.
3177
3178    BIT_OFFSET is used internally for handling records and specifies offset
3179    of the offset in bits modulo 256 to avoid overflow cases.
3180
3181    See the x86-64 PS ABI for details.
3182 */
3183
3184 static int
3185 classify_argument (enum machine_mode mode, tree type,
3186                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
3187 {
3188   HOST_WIDE_INT bytes =
3189     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3190   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3191
3192   /* Variable sized entities are always passed/returned in memory.  */
3193   if (bytes < 0)
3194     return 0;
3195
3196   if (mode != VOIDmode
3197       && targetm.calls.must_pass_in_stack (mode, type))
3198     return 0;
3199
3200   if (type && AGGREGATE_TYPE_P (type))
3201     {
3202       int i;
3203       tree field;
3204       enum x86_64_reg_class subclasses[MAX_CLASSES];
3205
3206       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3207       if (bytes > 16)
3208         return 0;
3209
3210       for (i = 0; i < words; i++)
3211         classes[i] = X86_64_NO_CLASS;
3212
3213       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3214          signalize memory class, so handle it as special case.  */
3215       if (!words)
3216         {
3217           classes[0] = X86_64_NO_CLASS;
3218           return 1;
3219         }
3220
3221       /* Classify each field of record and merge classes.  */
3222       switch (TREE_CODE (type))
3223         {
3224         case RECORD_TYPE:
3225           /* And now merge the fields of structure.  */
3226           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3227             {
3228               if (TREE_CODE (field) == FIELD_DECL)
3229                 {
3230                   int num;
3231
3232                   if (TREE_TYPE (field) == error_mark_node)
3233                     continue;
3234
3235                   /* Bitfields are always classified as integer.  Handle them
3236                      early, since later code would consider them to be
3237                      misaligned integers.  */
3238                   if (DECL_BIT_FIELD (field))
3239                     {
3240                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3241                            i < ((int_bit_position (field) + (bit_offset % 64))
3242                                 + tree_low_cst (DECL_SIZE (field), 0)
3243                                 + 63) / 8 / 8; i++)
3244                         classes[i] =
3245                           merge_classes (X86_64_INTEGER_CLASS,
3246                                          classes[i]);
3247                     }
3248                   else
3249                     {
3250                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3251                                                TREE_TYPE (field), subclasses,
3252                                                (int_bit_position (field)
3253                                                 + bit_offset) % 256);
3254                       if (!num)
3255                         return 0;
3256                       for (i = 0; i < num; i++)
3257                         {
3258                           int pos =
3259                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3260                           classes[i + pos] =
3261                             merge_classes (subclasses[i], classes[i + pos]);
3262                         }
3263                     }
3264                 }
3265             }
3266           break;
3267
3268         case ARRAY_TYPE:
3269           /* Arrays are handled as small records.  */
3270           {
3271             int num;
3272             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3273                                      TREE_TYPE (type), subclasses, bit_offset);
3274             if (!num)
3275               return 0;
3276
3277             /* The partial classes are now full classes.  */
3278             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3279               subclasses[0] = X86_64_SSE_CLASS;
3280             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3281               subclasses[0] = X86_64_INTEGER_CLASS;
3282
3283             for (i = 0; i < words; i++)
3284               classes[i] = subclasses[i % num];
3285
3286             break;
3287           }
3288         case UNION_TYPE:
3289         case QUAL_UNION_TYPE:
3290           /* Unions are similar to RECORD_TYPE but offset is always 0.
3291              */
3292           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3293             {
3294               if (TREE_CODE (field) == FIELD_DECL)
3295                 {
3296                   int num;
3297
3298                   if (TREE_TYPE (field) == error_mark_node)
3299                     continue;
3300
3301                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3302                                            TREE_TYPE (field), subclasses,
3303                                            bit_offset);
3304                   if (!num)
3305                     return 0;
3306                   for (i = 0; i < num; i++)
3307                     classes[i] = merge_classes (subclasses[i], classes[i]);
3308                 }
3309             }
3310           break;
3311
3312         default:
3313           gcc_unreachable ();
3314         }
3315
3316       /* Final merger cleanup.  */
3317       for (i = 0; i < words; i++)
3318         {
3319           /* If one class is MEMORY, everything should be passed in
3320              memory.  */
3321           if (classes[i] == X86_64_MEMORY_CLASS)
3322             return 0;
3323
3324           /* The X86_64_SSEUP_CLASS should be always preceded by
3325              X86_64_SSE_CLASS.  */
3326           if (classes[i] == X86_64_SSEUP_CLASS
3327               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3328             classes[i] = X86_64_SSE_CLASS;
3329
3330           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3331           if (classes[i] == X86_64_X87UP_CLASS
3332               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3333             classes[i] = X86_64_SSE_CLASS;
3334         }
3335       return words;
3336     }
3337
3338   /* Compute alignment needed.  We align all types to natural boundaries with
3339      exception of XFmode that is aligned to 64bits.  */
3340   if (mode != VOIDmode && mode != BLKmode)
3341     {
3342       int mode_alignment = GET_MODE_BITSIZE (mode);
3343
3344       if (mode == XFmode)
3345         mode_alignment = 128;
3346       else if (mode == XCmode)
3347         mode_alignment = 256;
3348       if (COMPLEX_MODE_P (mode))
3349         mode_alignment /= 2;
3350       /* Misaligned fields are always returned in memory.  */
3351       if (bit_offset % mode_alignment)
3352         return 0;
3353     }
3354
3355   /* for V1xx modes, just use the base mode */
3356   if (VECTOR_MODE_P (mode)
3357       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3358     mode = GET_MODE_INNER (mode);
3359
3360   /* Classification of atomic types.  */
3361   switch (mode)
3362     {
3363     case SDmode:
3364     case DDmode:
3365       classes[0] = X86_64_SSE_CLASS;
3366       return 1;
3367     case TDmode:
3368       classes[0] = X86_64_SSE_CLASS;
3369       classes[1] = X86_64_SSEUP_CLASS;
3370       return 2;
3371     case DImode:
3372     case SImode:
3373     case HImode:
3374     case QImode:
3375     case CSImode:
3376     case CHImode:
3377     case CQImode:
3378       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3379         classes[0] = X86_64_INTEGERSI_CLASS;
3380       else
3381         classes[0] = X86_64_INTEGER_CLASS;
3382       return 1;
3383     case CDImode:
3384     case TImode:
3385       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3386       return 2;
3387     case CTImode:
3388       return 0;
3389     case SFmode:
3390       if (!(bit_offset % 64))
3391         classes[0] = X86_64_SSESF_CLASS;
3392       else
3393         classes[0] = X86_64_SSE_CLASS;
3394       return 1;
3395     case DFmode:
3396       classes[0] = X86_64_SSEDF_CLASS;
3397       return 1;
3398     case XFmode:
3399       classes[0] = X86_64_X87_CLASS;
3400       classes[1] = X86_64_X87UP_CLASS;
3401       return 2;
3402     case TFmode:
3403       classes[0] = X86_64_SSE_CLASS;
3404       classes[1] = X86_64_SSEUP_CLASS;
3405       return 2;
3406     case SCmode:
3407       classes[0] = X86_64_SSE_CLASS;
3408       return 1;
3409     case DCmode:
3410       classes[0] = X86_64_SSEDF_CLASS;
3411       classes[1] = X86_64_SSEDF_CLASS;
3412       return 2;
3413     case XCmode:
3414       classes[0] = X86_64_COMPLEX_X87_CLASS;
3415       return 1;
3416     case TCmode:
3417       /* This modes is larger than 16 bytes.  */
3418       return 0;
3419     case V4SFmode:
3420     case V4SImode:
3421     case V16QImode:
3422     case V8HImode:
3423     case V2DFmode:
3424     case V2DImode:
3425       classes[0] = X86_64_SSE_CLASS;
3426       classes[1] = X86_64_SSEUP_CLASS;
3427       return 2;
3428     case V2SFmode:
3429     case V2SImode:
3430     case V4HImode:
3431     case V8QImode:
3432       classes[0] = X86_64_SSE_CLASS;
3433       return 1;
3434     case BLKmode:
3435     case VOIDmode:
3436       return 0;
3437     default:
3438       gcc_assert (VECTOR_MODE_P (mode));
3439
3440       if (bytes > 16)
3441         return 0;
3442
3443       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3444
3445       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3446         classes[0] = X86_64_INTEGERSI_CLASS;
3447       else
3448         classes[0] = X86_64_INTEGER_CLASS;
3449       classes[1] = X86_64_INTEGER_CLASS;
3450       return 1 + (bytes > 8);
3451     }
3452 }
3453
3454 /* Examine the argument and return set number of register required in each
3455    class.  Return 0 iff parameter should be passed in memory.  */
3456 static int
3457 examine_argument (enum machine_mode mode, tree type, int in_return,
3458                   int *int_nregs, int *sse_nregs)
3459 {
3460   enum x86_64_reg_class class[MAX_CLASSES];
3461   int n = classify_argument (mode, type, class, 0);
3462
3463   *int_nregs = 0;
3464   *sse_nregs = 0;
3465   if (!n)
3466     return 0;
3467   for (n--; n >= 0; n--)
3468     switch (class[n])
3469       {
3470       case X86_64_INTEGER_CLASS:
3471       case X86_64_INTEGERSI_CLASS:
3472         (*int_nregs)++;
3473         break;
3474       case X86_64_SSE_CLASS:
3475       case X86_64_SSESF_CLASS:
3476       case X86_64_SSEDF_CLASS:
3477         (*sse_nregs)++;
3478         break;
3479       case X86_64_NO_CLASS:
3480       case X86_64_SSEUP_CLASS:
3481         break;
3482       case X86_64_X87_CLASS:
3483       case X86_64_X87UP_CLASS:
3484         if (!in_return)
3485           return 0;
3486         break;
3487       case X86_64_COMPLEX_X87_CLASS:
3488         return in_return ? 2 : 0;
3489       case X86_64_MEMORY_CLASS:
3490         gcc_unreachable ();
3491       }
3492   return 1;
3493 }
3494
3495 /* Construct container for the argument used by GCC interface.  See
3496    FUNCTION_ARG for the detailed description.  */
3497
3498 static rtx
3499 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3500                      tree type, int in_return, int nintregs, int nsseregs,
3501                      const int *intreg, int sse_regno)
3502 {
3503   /* The following variables hold the static issued_error state.  */
3504   static bool issued_sse_arg_error;
3505   static bool issued_sse_ret_error;
3506   static bool issued_x87_ret_error;
3507
3508   enum machine_mode tmpmode;
3509   int bytes =
3510     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3511   enum x86_64_reg_class class[MAX_CLASSES];
3512   int n;
3513   int i;
3514   int nexps = 0;
3515   int needed_sseregs, needed_intregs;
3516   rtx exp[MAX_CLASSES];
3517   rtx ret;
3518
3519   n = classify_argument (mode, type, class, 0);
3520   if (!n)
3521     return NULL;
3522   if (!examine_argument (mode, type, in_return, &needed_intregs,
3523                          &needed_sseregs))
3524     return NULL;
3525   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3526     return NULL;
3527
3528   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3529      some less clueful developer tries to use floating-point anyway.  */
3530   if (needed_sseregs && !TARGET_SSE)
3531     {
3532       if (in_return)
3533         {
3534           if (!issued_sse_ret_error)
3535             {
3536               error ("SSE register return with SSE disabled");
3537               issued_sse_ret_error = true;
3538             }
3539         }
3540       else if (!issued_sse_arg_error)
3541         {
3542           error ("SSE register argument with SSE disabled");
3543           issued_sse_arg_error = true;
3544         }
3545       return NULL;
3546     }
3547
3548   /* Likewise, error if the ABI requires us to return values in the
3549      x87 registers and the user specified -mno-80387.  */
3550   if (!TARGET_80387 && in_return)
3551     for (i = 0; i < n; i++)
3552       if (class[i] == X86_64_X87_CLASS
3553           || class[i] == X86_64_X87UP_CLASS
3554           || class[i] == X86_64_COMPLEX_X87_CLASS)
3555         {
3556           if (!issued_x87_ret_error)
3557             {
3558               error ("x87 register return with x87 disabled");
3559               issued_x87_ret_error = true;
3560             }
3561           return NULL;
3562         }
3563
3564   /* First construct simple cases.  Avoid SCmode, since we want to use
3565      single register to pass this type.  */
3566   if (n == 1 && mode != SCmode)
3567     switch (class[0])
3568       {
3569       case X86_64_INTEGER_CLASS:
3570       case X86_64_INTEGERSI_CLASS:
3571         return gen_rtx_REG (mode, intreg[0]);
3572       case X86_64_SSE_CLASS:
3573       case X86_64_SSESF_CLASS:
3574       case X86_64_SSEDF_CLASS:
3575         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3576       case X86_64_X87_CLASS:
3577       case X86_64_COMPLEX_X87_CLASS:
3578         return gen_rtx_REG (mode, FIRST_STACK_REG);
3579       case X86_64_NO_CLASS:
3580         /* Zero sized array, struct or class.  */
3581         return NULL;
3582       default:
3583         gcc_unreachable ();
3584       }
3585   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3586       && mode != BLKmode)
3587     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3588
3589   if (n == 2
3590       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3591     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3592   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3593       && class[1] == X86_64_INTEGER_CLASS
3594       && (mode == CDImode || mode == TImode || mode == TFmode)
3595       && intreg[0] + 1 == intreg[1])
3596     return gen_rtx_REG (mode, intreg[0]);
3597
3598   /* Otherwise figure out the entries of the PARALLEL.  */
3599   for (i = 0; i < n; i++)
3600     {
3601       switch (class[i])
3602         {
3603           case X86_64_NO_CLASS:
3604             break;
3605           case X86_64_INTEGER_CLASS:
3606           case X86_64_INTEGERSI_CLASS:
3607             /* Merge TImodes on aligned occasions here too.  */
3608             if (i * 8 + 8 > bytes)
3609               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3610             else if (class[i] == X86_64_INTEGERSI_CLASS)
3611               tmpmode = SImode;
3612             else
3613               tmpmode = DImode;
3614             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3615             if (tmpmode == BLKmode)
3616               tmpmode = DImode;
3617             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3618                                                gen_rtx_REG (tmpmode, *intreg),
3619                                                GEN_INT (i*8));
3620             intreg++;
3621             break;
3622           case X86_64_SSESF_CLASS:
3623             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3624                                                gen_rtx_REG (SFmode,
3625                                                             SSE_REGNO (sse_regno)),
3626                                                GEN_INT (i*8));
3627             sse_regno++;
3628             break;
3629           case X86_64_SSEDF_CLASS:
3630             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3631                                                gen_rtx_REG (DFmode,
3632                                                             SSE_REGNO (sse_regno)),
3633                                                GEN_INT (i*8));
3634             sse_regno++;
3635             break;
3636           case X86_64_SSE_CLASS:
3637             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3638               tmpmode = TImode;
3639             else
3640               tmpmode = DImode;
3641             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3642                                                gen_rtx_REG (tmpmode,
3643                                                             SSE_REGNO (sse_regno)),
3644                                                GEN_INT (i*8));
3645             if (tmpmode == TImode)
3646               i++;
3647             sse_regno++;
3648             break;
3649           default:
3650             gcc_unreachable ();
3651         }
3652     }
3653
3654   /* Empty aligned struct, union or class.  */
3655   if (nexps == 0)
3656     return NULL;
3657
3658   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3659   for (i = 0; i < nexps; i++)
3660     XVECEXP (ret, 0, i) = exp [i];
3661   return ret;
3662 }
3663
3664 /* Update the data in CUM to advance over an argument of mode MODE
3665    and data type TYPE.  (TYPE is null for libcalls where that information
3666    may not be available.)  */
3667
3668 static void
3669 function_arg_advance_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3670                          tree type, HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3671 {
3672   switch (mode)
3673     {
3674     default:
3675       break;
3676
3677     case BLKmode:
3678       if (bytes < 0)
3679         break;
3680       /* FALLTHRU */
3681
3682     case DImode:
3683     case SImode:
3684     case HImode:
3685     case QImode:
3686       cum->words += words;
3687       cum->nregs -= words;
3688       cum->regno += words;
3689
3690       if (cum->nregs <= 0)
3691         {
3692           cum->nregs = 0;
3693           cum->regno = 0;
3694         }
3695       break;
3696
3697     case DFmode:
3698       if (cum->float_in_sse < 2)
3699         break;
3700     case SFmode:
3701       if (cum->float_in_sse < 1)
3702         break;
3703       /* FALLTHRU */
3704
3705     case TImode:
3706     case V16QImode:
3707     case V8HImode:
3708     case V4SImode:
3709     case V2DImode:
3710     case V4SFmode:
3711     case V2DFmode:
3712       if (!type || !AGGREGATE_TYPE_P (type))
3713         {
3714           cum->sse_words += words;
3715           cum->sse_nregs -= 1;
3716           cum->sse_regno += 1;
3717           if (cum->sse_nregs <= 0)
3718             {
3719               cum->sse_nregs = 0;
3720               cum->sse_regno = 0;
3721             }
3722         }
3723       break;
3724
3725     case V8QImode:
3726     case V4HImode:
3727     case V2SImode:
3728     case V2SFmode:
3729       if (!type || !AGGREGATE_TYPE_P (type))
3730         {
3731           cum->mmx_words += words;
3732           cum->mmx_nregs -= 1;
3733           cum->mmx_regno += 1;
3734           if (cum->mmx_nregs <= 0)
3735             {
3736               cum->mmx_nregs = 0;
3737               cum->mmx_regno = 0;
3738             }
3739         }
3740       break;
3741     }
3742 }
3743
3744 static void
3745 function_arg_advance_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3746                          tree type, HOST_WIDE_INT words)
3747 {
3748   int int_nregs, sse_nregs;
3749
3750   if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3751     cum->words += words;
3752   else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3753     {
3754       cum->nregs -= int_nregs;
3755       cum->sse_nregs -= sse_nregs;
3756       cum->regno += int_nregs;
3757       cum->sse_regno += sse_nregs;
3758     }
3759   else
3760     cum->words += words;
3761 }
3762
3763 static void
3764 function_arg_advance_ms_64 (CUMULATIVE_ARGS *cum, HOST_WIDE_INT bytes,
3765                             HOST_WIDE_INT words)
3766 {
3767   /* Otherwise, this should be passed indirect.  */
3768   gcc_assert (bytes == 1 || bytes == 2 || bytes == 4 || bytes == 8);
3769
3770   cum->words += words;
3771   if (cum->nregs > 0)
3772     {
3773       cum->nregs -= 1;
3774       cum->regno += 1;
3775     }
3776 }
3777
3778 void
3779 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3780                       tree type, int named ATTRIBUTE_UNUSED)
3781 {
3782   HOST_WIDE_INT bytes, words;
3783
3784   if (mode == BLKmode)
3785     bytes = int_size_in_bytes (type);
3786   else
3787     bytes = GET_MODE_SIZE (mode);
3788   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3789
3790   if (type)
3791     mode = type_natural_mode (type);
3792
3793   if (TARGET_64BIT_MS_ABI)
3794     function_arg_advance_ms_64 (cum, bytes, words);
3795   else if (TARGET_64BIT)
3796     function_arg_advance_64 (cum, mode, type, words);
3797   else
3798     function_arg_advance_32 (cum, mode, type, bytes, words);
3799 }
3800
3801 /* Define where to put the arguments to a function.
3802    Value is zero to push the argument on the stack,
3803    or a hard register in which to store the argument.
3804
3805    MODE is the argument's machine mode.
3806    TYPE is the data type of the argument (as a tree).
3807     This is null for libcalls where that information may
3808     not be available.
3809    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3810     the preceding args and about the function being called.
3811    NAMED is nonzero if this argument is a named parameter
3812     (otherwise it is an extra parameter matching an ellipsis).  */
3813
3814 static rtx
3815 function_arg_32 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3816                  enum machine_mode orig_mode, tree type,
3817                  HOST_WIDE_INT bytes, HOST_WIDE_INT words)
3818 {
3819   static bool warnedsse, warnedmmx;
3820
3821   /* Avoid the AL settings for the Unix64 ABI.  */
3822   if (mode == VOIDmode)
3823     return constm1_rtx;
3824
3825   switch (mode)
3826     {
3827     default:
3828       break;
3829
3830     case BLKmode:
3831       if (bytes < 0)
3832         break;
3833       /* FALLTHRU */
3834     case DImode:
3835     case SImode:
3836     case HImode:
3837     case QImode:
3838       if (words <= cum->nregs)
3839         {
3840           int regno = cum->regno;
3841
3842           /* Fastcall allocates the first two DWORD (SImode) or
3843              smaller arguments to ECX and EDX.  */
3844           if (cum->fastcall)
3845             {
3846               if (mode == BLKmode || mode == DImode)
3847                 break;
3848
3849               /* ECX not EAX is the first allocated register.  */
3850               if (regno == 0)
3851                 regno = 2;
3852             }
3853           return gen_rtx_REG (mode, regno);
3854         }
3855       break;
3856
3857     case DFmode:
3858       if (cum->float_in_sse < 2)
3859         break;
3860     case SFmode:
3861       if (cum->float_in_sse < 1)
3862         break;
3863       /* FALLTHRU */
3864     case TImode:
3865     case V16QImode:
3866     case V8HImode:
3867     case V4SImode:
3868     case V2DImode:
3869     case V4SFmode:
3870     case V2DFmode:
3871       if (!type || !AGGREGATE_TYPE_P (type))
3872         {
3873           if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3874             {
3875               warnedsse = true;
3876               warning (0, "SSE vector argument without SSE enabled "
3877                        "changes the ABI");
3878             }
3879           if (cum->sse_nregs)
3880             return gen_reg_or_parallel (mode, orig_mode,
3881                                         cum->sse_regno + FIRST_SSE_REG);
3882         }
3883       break;
3884
3885     case V8QImode:
3886     case V4HImode:
3887     case V2SImode:
3888     case V2SFmode:
3889       if (!type || !AGGREGATE_TYPE_P (type))
3890         {
3891           if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3892             {
3893               warnedmmx = true;
3894               warning (0, "MMX vector argument without MMX enabled "
3895                        "changes the ABI");
3896             }
3897           if (cum->mmx_nregs)
3898             return gen_reg_or_parallel (mode, orig_mode,
3899                                         cum->mmx_regno + FIRST_MMX_REG);
3900         }
3901       break;
3902     }
3903
3904   return NULL_RTX;
3905 }
3906
3907 static rtx
3908 function_arg_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3909                  enum machine_mode orig_mode, tree type)
3910 {
3911   /* Handle a hidden AL argument containing number of registers
3912      for varargs x86-64 functions.  */
3913   if (mode == VOIDmode)
3914     return GEN_INT (cum->maybe_vaarg
3915                     ? (cum->sse_nregs < 0
3916                        ? SSE_REGPARM_MAX
3917                        : cum->sse_regno)
3918                     : -1);
3919
3920   return construct_container (mode, orig_mode, type, 0, cum->nregs,
3921                               cum->sse_nregs,
3922                               &x86_64_int_parameter_registers [cum->regno],
3923                               cum->sse_regno);
3924 }
3925
3926 static rtx
3927 function_arg_ms_64 (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3928                     enum machine_mode orig_mode, int named)
3929 {
3930   unsigned int regno;
3931
3932   /* Avoid the AL settings for the Unix64 ABI.  */
3933   if (mode == VOIDmode)
3934     return constm1_rtx;
3935
3936   /* If we've run out of registers, it goes on the stack.  */
3937   if (cum->nregs == 0)
3938     return NULL_RTX;
3939
3940   regno = x86_64_ms_abi_int_parameter_registers[cum->regno];
3941
3942   /* Only floating point modes are passed in anything but integer regs.  */
3943   if (TARGET_SSE && (mode == SFmode || mode == DFmode))
3944     {
3945       if (named)
3946         regno = cum->regno + FIRST_SSE_REG;
3947       else
3948         {
3949           rtx t1, t2;
3950
3951           /* Unnamed floating parameters are passed in both the
3952              SSE and integer registers.  */
3953           t1 = gen_rtx_REG (mode, cum->regno + FIRST_SSE_REG);
3954           t2 = gen_rtx_REG (mode, regno);
3955           t1 = gen_rtx_EXPR_LIST (VOIDmode, t1, const0_rtx);
3956           t2 = gen_rtx_EXPR_LIST (VOIDmode, t2, const0_rtx);
3957           return gen_rtx_PARALLEL (mode, gen_rtvec (2, t1, t2));
3958         }
3959     }
3960
3961   return gen_reg_or_parallel (mode, orig_mode, regno);
3962 }
3963
3964 rtx
3965 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode omode,
3966               tree type, int named)
3967 {
3968   enum machine_mode mode = omode;
3969   HOST_WIDE_INT bytes, words;
3970
3971   if (mode == BLKmode)
3972     bytes = int_size_in_bytes (type);
3973   else
3974     bytes = GET_MODE_SIZE (mode);
3975   words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3976
3977   /* To simplify the code below, represent vector types with a vector mode
3978      even if MMX/SSE are not active.  */
3979   if (type && TREE_CODE (type) == VECTOR_TYPE)
3980     mode = type_natural_mode (type);
3981
3982   if (TARGET_64BIT_MS_ABI)
3983     return function_arg_ms_64 (cum, mode, omode, named);
3984   else if (TARGET_64BIT)
3985     return function_arg_64 (cum, mode, omode, type);
3986   else
3987     return function_arg_32 (cum, mode, omode, type, bytes, words);
3988 }
3989
3990 /* A C expression that indicates when an argument must be passed by
3991    reference.  If nonzero for an argument, a copy of that argument is
3992    made in memory and a pointer to the argument is passed instead of
3993    the argument itself.  The pointer is passed in whatever way is
3994    appropriate for passing a pointer to that type.  */
3995
3996 static bool
3997 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3998                         enum machine_mode mode ATTRIBUTE_UNUSED,
3999                         tree type, bool named ATTRIBUTE_UNUSED)
4000 {
4001   if (TARGET_64BIT_MS_ABI)
4002     {
4003       if (type)
4004         {
4005           /* Arrays are passed by reference.  */
4006           if (TREE_CODE (type) == ARRAY_TYPE)
4007             return true;
4008
4009           if (AGGREGATE_TYPE_P (type))
4010             {
4011               /* Structs/unions of sizes other than 8, 16, 32, or 64 bits
4012                  are passed by reference.  */
4013               int el2 = exact_log2 (int_size_in_bytes (type));
4014               return !(el2 >= 0 && el2 <= 3);
4015             }
4016         }
4017
4018       /* __m128 is passed by reference.  */
4019       /* ??? How to handle complex?  For now treat them as structs,
4020          and pass them by reference if they're too large.  */
4021       if (GET_MODE_SIZE (mode) > 8)
4022         return true;
4023     }
4024   else if (TARGET_64BIT && type && int_size_in_bytes (type) == -1)
4025     return 1;
4026
4027   return 0;
4028 }
4029
4030 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
4031    ABI.  Only called if TARGET_SSE.  */
4032 static bool
4033 contains_128bit_aligned_vector_p (tree type)
4034 {
4035   enum machine_mode mode = TYPE_MODE (type);
4036   if (SSE_REG_MODE_P (mode)
4037       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
4038     return true;
4039   if (TYPE_ALIGN (type) < 128)
4040     return false;
4041
4042   if (AGGREGATE_TYPE_P (type))
4043     {
4044       /* Walk the aggregates recursively.  */
4045       switch (TREE_CODE (type))
4046         {
4047         case RECORD_TYPE:
4048         case UNION_TYPE:
4049         case QUAL_UNION_TYPE:
4050           {
4051             tree field;
4052
4053             /* Walk all the structure fields.  */
4054             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4055               {
4056                 if (TREE_CODE (field) == FIELD_DECL
4057                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
4058                   return true;
4059               }
4060             break;
4061           }
4062
4063         case ARRAY_TYPE:
4064           /* Just for use if some languages passes arrays by value.  */
4065           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
4066             return true;
4067           break;
4068
4069         default:
4070           gcc_unreachable ();
4071         }
4072     }
4073   return false;
4074 }
4075
4076 /* Gives the alignment boundary, in bits, of an argument with the
4077    specified mode and type.  */
4078
4079 int
4080 ix86_function_arg_boundary (enum machine_mode mode, tree type)
4081 {
4082   int align;
4083   if (type)
4084     align = TYPE_ALIGN (type);
4085   else
4086     align = GET_MODE_ALIGNMENT (mode);
4087   if (align < PARM_BOUNDARY)
4088     align = PARM_BOUNDARY;
4089   if (!TARGET_64BIT)
4090     {
4091       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
4092          make an exception for SSE modes since these require 128bit
4093          alignment.
4094
4095          The handling here differs from field_alignment.  ICC aligns MMX
4096          arguments to 4 byte boundaries, while structure fields are aligned
4097          to 8 byte boundaries.  */
4098       if (!TARGET_SSE)
4099         align = PARM_BOUNDARY;
4100       else if (!type)
4101         {
4102           if (!SSE_REG_MODE_P (mode))
4103             align = PARM_BOUNDARY;
4104         }
4105       else
4106         {
4107           if (!contains_128bit_aligned_vector_p (type))
4108             align = PARM_BOUNDARY;
4109         }
4110     }
4111   if (align > 128)
4112     align = 128;
4113   return align;
4114 }
4115
4116 /* Return true if N is a possible register number of function value.  */
4117
4118 bool
4119 ix86_function_value_regno_p (int regno)
4120 {
4121   switch (regno)
4122     {
4123     case 0:
4124       return true;
4125
4126     case FIRST_FLOAT_REG:
4127       if (TARGET_64BIT_MS_ABI)
4128         return false;
4129       return TARGET_FLOAT_RETURNS_IN_80387;
4130
4131     case FIRST_SSE_REG:
4132       return TARGET_SSE;
4133
4134     case FIRST_MMX_REG:
4135       if (TARGET_MACHO || TARGET_64BIT)
4136         return false;
4137       return TARGET_MMX;
4138     }
4139
4140   return false;
4141 }
4142
4143 /* Define how to find the value returned by a function.
4144    VALTYPE is the data type of the value (as a tree).
4145    If the precise function being called is known, FUNC is its FUNCTION_DECL;
4146    otherwise, FUNC is 0.  */
4147
4148 static rtx
4149 function_value_32 (enum machine_mode orig_mode, enum machine_mode mode,
4150                    tree fntype, tree fn)
4151 {
4152   unsigned int regno;
4153
4154   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4155      we normally prevent this case when mmx is not available.  However
4156      some ABIs may require the result to be returned like DImode.  */
4157   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4158     regno = TARGET_MMX ? FIRST_MMX_REG : 0;
4159
4160   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4161      we prevent this case when sse is not available.  However some ABIs
4162      may require the result to be returned like integer TImode.  */
4163   else if (mode == TImode
4164            || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4165     regno = TARGET_SSE ? FIRST_SSE_REG : 0;
4166
4167   /* Decimal floating point values can go in %eax, unlike other float modes.  */
4168   else if (DECIMAL_FLOAT_MODE_P (mode))
4169     regno = 0;
4170
4171   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
4172   else if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
4173     regno = 0;
4174
4175   /* Floating point return values in %st(0), except for local functions when
4176      SSE math is enabled or for functions with sseregparm attribute.  */
4177   else
4178     {
4179       regno = FIRST_FLOAT_REG;
4180
4181       if ((fn || fntype) && (mode == SFmode || mode == DFmode))
4182         {
4183           int sse_level = ix86_function_sseregparm (fntype, fn);
4184           if ((sse_level >= 1 && mode == SFmode)
4185               || (sse_level == 2 && mode == DFmode))
4186             regno = FIRST_SSE_REG;
4187         }
4188     }
4189
4190   return gen_rtx_REG (orig_mode, regno);
4191 }
4192
4193 static rtx
4194 function_value_64 (enum machine_mode orig_mode, enum machine_mode mode,
4195                    tree valtype)
4196 {
4197   rtx ret;
4198
4199   /* Handle libcalls, which don't provide a type node.  */
4200   if (valtype == NULL)
4201     {
4202       switch (mode)
4203         {
4204         case SFmode:
4205         case SCmode:
4206         case DFmode:
4207         case DCmode:
4208         case TFmode:
4209         case SDmode:
4210         case DDmode:
4211         case TDmode:
4212           return gen_rtx_REG (mode, FIRST_SSE_REG);
4213         case XFmode:
4214         case XCmode:
4215           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4216         case TCmode:
4217           return NULL;
4218         default:
4219           return gen_rtx_REG (mode, 0);
4220         }
4221     }
4222
4223   ret = construct_container (mode, orig_mode, valtype, 1,
4224                              REGPARM_MAX, SSE_REGPARM_MAX,
4225                              x86_64_int_return_registers, 0);
4226
4227   /* For zero sized structures, construct_container returns NULL, but we
4228      need to keep rest of compiler happy by returning meaningful value.  */
4229   if (!ret)
4230     ret = gen_rtx_REG (orig_mode, 0);
4231
4232   return ret;
4233 }
4234
4235 static rtx
4236 function_value_ms_64 (enum machine_mode orig_mode, enum machine_mode mode)
4237 {
4238   unsigned int regno = 0;
4239
4240   if (TARGET_SSE)
4241     {
4242       if (mode == SFmode || mode == DFmode)
4243         regno = FIRST_SSE_REG;
4244       else if (VECTOR_MODE_P (mode) || GET_MODE_SIZE (mode) == 16)
4245         regno = FIRST_SSE_REG;
4246     }
4247
4248   return gen_rtx_REG (orig_mode, regno);
4249 }
4250
4251 static rtx
4252 ix86_function_value_1 (tree valtype, tree fntype_or_decl,
4253                        enum machine_mode orig_mode, enum machine_mode mode)
4254 {
4255   tree fn, fntype;
4256
4257   fn = NULL_TREE;
4258   if (fntype_or_decl && DECL_P (fntype_or_decl))
4259     fn = fntype_or_decl;
4260   fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
4261
4262   if (TARGET_64BIT_MS_ABI)
4263     return function_value_ms_64 (orig_mode, mode);
4264   else if (TARGET_64BIT)
4265     return function_value_64 (orig_mode, mode, valtype);
4266   else
4267     return function_value_32 (orig_mode, mode, fntype, fn);
4268 }
4269
4270 static rtx
4271 ix86_function_value (tree valtype, tree fntype_or_decl,
4272                      bool outgoing ATTRIBUTE_UNUSED)
4273 {
4274   enum machine_mode mode, orig_mode;
4275
4276   orig_mode = TYPE_MODE (valtype);
4277   mode = type_natural_mode (valtype);
4278   return ix86_function_value_1 (valtype, fntype_or_decl, orig_mode, mode);
4279 }
4280
4281 rtx
4282 ix86_libcall_value (enum machine_mode mode)
4283 {
4284   return ix86_function_value_1 (NULL, NULL, mode, mode);
4285 }
4286
4287 /* Return true iff type is returned in memory.  */
4288
4289 static int
4290 return_in_memory_32 (tree type, enum machine_mode mode)
4291 {
4292   HOST_WIDE_INT size;
4293
4294   if (mode == BLKmode)
4295     return 1;
4296
4297   size = int_size_in_bytes (type);
4298
4299   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
4300     return 0;
4301
4302   if (VECTOR_MODE_P (mode) || mode == TImode)
4303     {
4304       /* User-created vectors small enough to fit in EAX.  */
4305       if (size < 8)
4306         return 0;
4307
4308       /* MMX/3dNow values are returned in MM0,
4309          except when it doesn't exits.  */
4310       if (size == 8)
4311         return (TARGET_MMX ? 0 : 1);
4312
4313       /* SSE values are returned in XMM0, except when it doesn't exist.  */
4314       if (size == 16)
4315         return (TARGET_SSE ? 0 : 1);
4316     }
4317
4318   if (mode == XFmode)
4319     return 0;
4320
4321   if (mode == TDmode)
4322     return 1;
4323
4324   if (size > 12)
4325     return 1;
4326   return 0;
4327 }
4328
4329 static int
4330 return_in_memory_64 (tree type, enum machine_mode mode)
4331 {
4332   int needed_intregs, needed_sseregs;
4333   return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
4334 }
4335
4336 static int
4337 return_in_memory_ms_64 (tree type, enum machine_mode mode)
4338 {
4339   HOST_WIDE_INT size = int_size_in_bytes (type);
4340
4341   /* __m128 and friends are returned in xmm0.  */
4342   if (size == 16 && VECTOR_MODE_P (mode))
4343     return 0;
4344
4345   /* Otherwise, the size must be exactly in [1248].  */
4346   return (size != 1 && size != 2 && size != 4 && size != 8);
4347 }
4348
4349 int
4350 ix86_return_in_memory (tree type)
4351 {
4352   enum machine_mode mode = type_natural_mode (type);
4353
4354   if (TARGET_64BIT_MS_ABI)
4355     return return_in_memory_ms_64 (type, mode);
4356   else if (TARGET_64BIT)
4357     return return_in_memory_64 (type, mode);
4358   else
4359     return return_in_memory_32 (type, mode);
4360 }
4361
4362 /* When returning SSE vector types, we have a choice of either
4363      (1) being abi incompatible with a -march switch, or
4364      (2) generating an error.
4365    Given no good solution, I think the safest thing is one warning.
4366    The user won't be able to use -Werror, but....
4367
4368    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
4369    called in response to actually generating a caller or callee that
4370    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
4371    via aggregate_value_p for general type probing from tree-ssa.  */
4372
4373 static rtx
4374 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
4375 {
4376   static bool warnedsse, warnedmmx;
4377
4378   if (!TARGET_64BIT && type)
4379     {
4380       /* Look at the return type of the function, not the function type.  */
4381       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
4382
4383       if (!TARGET_SSE && !warnedsse)
4384         {
4385           if (mode == TImode
4386               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4387             {
4388               warnedsse = true;
4389               warning (0, "SSE vector return without SSE enabled "
4390                        "changes the ABI");
4391             }
4392         }
4393
4394       if (!TARGET_MMX && !warnedmmx)
4395         {
4396           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4397             {
4398               warnedmmx = true;
4399               warning (0, "MMX vector return without MMX enabled "
4400                        "changes the ABI");
4401             }
4402         }
4403     }
4404
4405   return NULL;
4406 }
4407
4408 \f
4409 /* Create the va_list data type.  */
4410
4411 static tree
4412 ix86_build_builtin_va_list (void)
4413 {
4414   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4415
4416   /* For i386 we use plain pointer to argument area.  */
4417   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4418     return build_pointer_type (char_type_node);
4419
4420   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4421   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4422
4423   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4424                       unsigned_type_node);
4425   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4426                       unsigned_type_node);
4427   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4428                       ptr_type_node);
4429   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4430                       ptr_type_node);
4431
4432   va_list_gpr_counter_field = f_gpr;
4433   va_list_fpr_counter_field = f_fpr;
4434
4435   DECL_FIELD_CONTEXT (f_gpr) = record;
4436   DECL_FIELD_CONTEXT (f_fpr) = record;
4437   DECL_FIELD_CONTEXT (f_ovf) = record;
4438   DECL_FIELD_CONTEXT (f_sav) = record;
4439
4440   TREE_CHAIN (record) = type_decl;
4441   TYPE_NAME (record) = type_decl;
4442   TYPE_FIELDS (record) = f_gpr;
4443   TREE_CHAIN (f_gpr) = f_fpr;
4444   TREE_CHAIN (f_fpr) = f_ovf;
4445   TREE_CHAIN (f_ovf) = f_sav;
4446
4447   layout_type (record);
4448
4449   /* The correct type is an array type of one element.  */
4450   return build_array_type (record, build_index_type (size_zero_node));
4451 }
4452
4453 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4454
4455 static void
4456 setup_incoming_varargs_64 (CUMULATIVE_ARGS *cum)
4457 {
4458   rtx save_area, mem;
4459   rtx label;
4460   rtx label_ref;
4461   rtx tmp_reg;
4462   rtx nsse_reg;
4463   int set;
4464   int i;
4465
4466   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4467     return;
4468
4469   /* Indicate to allocate space on the stack for varargs save area.  */
4470   ix86_save_varrargs_registers = 1;
4471   cfun->stack_alignment_needed = 128;
4472
4473   save_area = frame_pointer_rtx;
4474   set = get_varargs_alias_set ();
4475
4476   for (i = cum->regno;
4477        i < ix86_regparm
4478        && i < cum->regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4479        i++)
4480     {
4481       mem = gen_rtx_MEM (Pmode,
4482                          plus_constant (save_area, i * UNITS_PER_WORD));
4483       MEM_NOTRAP_P (mem) = 1;
4484       set_mem_alias_set (mem, set);
4485       emit_move_insn (mem, gen_rtx_REG (Pmode,
4486                                         x86_64_int_parameter_registers[i]));
4487     }
4488
4489   if (cum->sse_nregs && cfun->va_list_fpr_size)
4490     {
4491       /* Now emit code to save SSE registers.  The AX parameter contains number
4492          of SSE parameter registers used to call this function.  We use
4493          sse_prologue_save insn template that produces computed jump across
4494          SSE saves.  We need some preparation work to get this working.  */
4495
4496       label = gen_label_rtx ();
4497       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4498
4499       /* Compute address to jump to :
4500          label - 5*eax + nnamed_sse_arguments*5  */
4501       tmp_reg = gen_reg_rtx (Pmode);
4502       nsse_reg = gen_reg_rtx (Pmode);
4503       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4504       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4505                               gen_rtx_MULT (Pmode, nsse_reg,
4506                                             GEN_INT (4))));
4507       if (cum->sse_regno)
4508         emit_move_insn
4509           (nsse_reg,
4510            gen_rtx_CONST (DImode,
4511                           gen_rtx_PLUS (DImode,
4512                                         label_ref,
4513                                         GEN_INT (cum->sse_regno * 4))));
4514       else
4515         emit_move_insn (nsse_reg, label_ref);
4516       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4517
4518       /* Compute address of memory block we save into.  We always use pointer
4519          pointing 127 bytes after first byte to store - this is needed to keep
4520          instruction size limited by 4 bytes.  */
4521       tmp_reg = gen_reg_rtx (Pmode);
4522       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4523                               plus_constant (save_area,
4524                                              8 * REGPARM_MAX + 127)));
4525       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4526       MEM_NOTRAP_P (mem) = 1;
4527       set_mem_alias_set (mem, set);
4528       set_mem_align (mem, BITS_PER_WORD);
4529
4530       /* And finally do the dirty job!  */
4531       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4532                                         GEN_INT (cum->sse_regno), label));
4533     }
4534 }
4535
4536 static void
4537 setup_incoming_varargs_ms_64 (CUMULATIVE_ARGS *cum)
4538 {
4539   int set = get_varargs_alias_set ();
4540   int i;
4541
4542   for (i = cum->regno; i < REGPARM_MAX; i++)
4543     {
4544       rtx reg, mem;
4545
4546       mem = gen_rtx_MEM (Pmode,
4547                          plus_constant (virtual_incoming_args_rtx,
4548                                         i * UNITS_PER_WORD));
4549       MEM_NOTRAP_P (mem) = 1;
4550       set_mem_alias_set (mem, set);
4551
4552       reg = gen_rtx_REG (Pmode, x86_64_ms_abi_int_parameter_registers[i]);
4553       emit_move_insn (mem, reg);
4554     }
4555 }
4556
4557 static void
4558 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4559                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4560                              int no_rtl)
4561 {
4562   CUMULATIVE_ARGS next_cum;
4563   tree fntype;
4564   int stdarg_p;
4565
4566   /* This argument doesn't appear to be used anymore.  Which is good,
4567      because the old code here didn't suppress rtl generation.  */
4568   gcc_assert (!no_rtl);
4569
4570   if (!TARGET_64BIT)
4571     return;
4572
4573   fntype = TREE_TYPE (current_function_decl);
4574   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4575               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4576                   != void_type_node));
4577
4578   /* For varargs, we do not want to skip the dummy va_dcl argument.
4579      For stdargs, we do want to skip the last named argument.  */
4580   next_cum = *cum;
4581   if (stdarg_p)
4582     function_arg_advance (&next_cum, mode, type, 1);
4583
4584   if (TARGET_64BIT_MS_ABI)
4585     setup_incoming_varargs_ms_64 (&next_cum);
4586   else
4587     setup_incoming_varargs_64 (&next_cum);
4588 }
4589
4590 /* Implement va_start.  */
4591
4592 void
4593 ix86_va_start (tree valist, rtx nextarg)
4594 {
4595   HOST_WIDE_INT words, n_gpr, n_fpr;
4596   tree f_gpr, f_fpr, f_ovf, f_sav;
4597   tree gpr, fpr, ovf, sav, t;
4598   tree type;
4599
4600   /* Only 64bit target needs something special.  */
4601   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4602     {
4603       std_expand_builtin_va_start (valist, nextarg);
4604       return;
4605     }
4606
4607   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4608   f_fpr = TREE_CHAIN (f_gpr);
4609   f_ovf = TREE_CHAIN (f_fpr);
4610   f_sav = TREE_CHAIN (f_ovf);
4611
4612   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4613   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4614   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4615   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4616   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4617
4618   /* Count number of gp and fp argument registers used.  */
4619   words = current_function_args_info.words;
4620   n_gpr = current_function_args_info.regno;
4621   n_fpr = current_function_args_info.sse_regno;
4622
4623   if (cfun->va_list_gpr_size)
4624     {
4625       type = TREE_TYPE (gpr);
4626       t = build2 (GIMPLE_MODIFY_STMT, type, gpr,
4627                   build_int_cst (type, n_gpr * 8));
4628       TREE_SIDE_EFFECTS (t) = 1;
4629       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4630     }
4631
4632   if (cfun->va_list_fpr_size)
4633     {
4634       type = TREE_TYPE (fpr);
4635       t = build2 (GIMPLE_MODIFY_STMT, type, fpr,
4636                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4637       TREE_SIDE_EFFECTS (t) = 1;
4638       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4639     }
4640
4641   /* Find the overflow area.  */
4642   type = TREE_TYPE (ovf);
4643   t = make_tree (type, virtual_incoming_args_rtx);
4644   if (words != 0)
4645     t = build2 (PLUS_EXPR, type, t,
4646                 build_int_cst (type, words * UNITS_PER_WORD));
4647   t = build2 (GIMPLE_MODIFY_STMT, type, ovf, t);
4648   TREE_SIDE_EFFECTS (t) = 1;
4649   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4650
4651   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4652     {
4653       /* Find the register save area.
4654          Prologue of the function save it right above stack frame.  */
4655       type = TREE_TYPE (sav);
4656       t = make_tree (type, frame_pointer_rtx);
4657       t = build2 (GIMPLE_MODIFY_STMT, type, sav, t);
4658       TREE_SIDE_EFFECTS (t) = 1;
4659       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4660     }
4661 }
4662
4663 /* Implement va_arg.  */
4664
4665 static tree
4666 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4667 {
4668   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4669   tree f_gpr, f_fpr, f_ovf, f_sav;
4670   tree gpr, fpr, ovf, sav, t;
4671   int size, rsize;
4672   tree lab_false, lab_over = NULL_TREE;
4673   tree addr, t2;
4674   rtx container;
4675   int indirect_p = 0;
4676   tree ptrtype;
4677   enum machine_mode nat_mode;
4678
4679   /* Only 64bit target needs something special.  */
4680   if (!TARGET_64BIT || TARGET_64BIT_MS_ABI)
4681     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4682
4683   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4684   f_fpr = TREE_CHAIN (f_gpr);
4685   f_ovf = TREE_CHAIN (f_fpr);
4686   f_sav = TREE_CHAIN (f_ovf);
4687
4688   valist = build_va_arg_indirect_ref (valist);
4689   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4690   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4691   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4692   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4693
4694   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4695   if (indirect_p)
4696     type = build_pointer_type (type);
4697   size = int_size_in_bytes (type);
4698   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4699
4700   nat_mode = type_natural_mode (type);
4701   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4702                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4703
4704   /* Pull the value out of the saved registers.  */
4705
4706   addr = create_tmp_var (ptr_type_node, "addr");
4707   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4708
4709   if (container)
4710     {
4711       int needed_intregs, needed_sseregs;
4712       bool need_temp;
4713       tree int_addr, sse_addr;
4714
4715       lab_false = create_artificial_label ();
4716       lab_over = create_artificial_label ();
4717
4718       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4719
4720       need_temp = (!REG_P (container)
4721                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4722                        || TYPE_ALIGN (type) > 128));
4723
4724       /* In case we are passing structure, verify that it is consecutive block
4725          on the register save area.  If not we need to do moves.  */
4726       if (!need_temp && !REG_P (container))
4727         {
4728           /* Verify that all registers are strictly consecutive  */
4729           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4730             {
4731               int i;
4732
4733               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4734                 {
4735                   rtx slot = XVECEXP (container, 0, i);
4736                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4737                       || INTVAL (XEXP (slot, 1)) != i * 16)
4738                     need_temp = 1;
4739                 }
4740             }
4741           else
4742             {
4743               int i;
4744
4745               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4746                 {
4747                   rtx slot = XVECEXP (container, 0, i);
4748                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4749                       || INTVAL (XEXP (slot, 1)) != i * 8)
4750                     need_temp = 1;
4751                 }
4752             }
4753         }
4754       if (!need_temp)
4755         {
4756           int_addr = addr;
4757           sse_addr = addr;
4758         }
4759       else
4760         {
4761           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4762           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4763           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4764           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4765         }
4766
4767       /* First ensure that we fit completely in registers.  */
4768       if (needed_intregs)
4769         {
4770           t = build_int_cst (TREE_TYPE (gpr),
4771                              (REGPARM_MAX - needed_intregs + 1) * 8);
4772           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4773           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4774           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4775           gimplify_and_add (t, pre_p);
4776         }
4777       if (needed_sseregs)
4778         {
4779           t = build_int_cst (TREE_TYPE (fpr),
4780                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4781                              + REGPARM_MAX * 8);
4782           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4783           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4784           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4785           gimplify_and_add (t, pre_p);
4786         }
4787
4788       /* Compute index to start of area used for integer regs.  */
4789       if (needed_intregs)
4790         {
4791           /* int_addr = gpr + sav; */
4792           t = fold_convert (ptr_type_node, gpr);
4793           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4794           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, int_addr, t);
4795           gimplify_and_add (t, pre_p);
4796         }
4797       if (needed_sseregs)
4798         {
4799           /* sse_addr = fpr + sav; */
4800           t = fold_convert (ptr_type_node, fpr);
4801           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4802           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, sse_addr, t);
4803           gimplify_and_add (t, pre_p);
4804         }
4805       if (need_temp)
4806         {
4807           int i;
4808           tree temp = create_tmp_var (type, "va_arg_tmp");
4809
4810           /* addr = &temp; */
4811           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4812           t = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4813           gimplify_and_add (t, pre_p);
4814
4815           for (i = 0; i < XVECLEN (container, 0); i++)
4816             {
4817               rtx slot = XVECEXP (container, 0, i);
4818               rtx reg = XEXP (slot, 0);
4819               enum machine_mode mode = GET_MODE (reg);
4820               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4821               tree addr_type = build_pointer_type (piece_type);
4822               tree src_addr, src;
4823               int src_offset;
4824               tree dest_addr, dest;
4825
4826               if (SSE_REGNO_P (REGNO (reg)))
4827                 {
4828                   src_addr = sse_addr;
4829                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4830                 }
4831               else
4832                 {
4833                   src_addr = int_addr;
4834                   src_offset = REGNO (reg) * 8;
4835                 }
4836               src_addr = fold_convert (addr_type, src_addr);
4837               src_addr = fold_build2 (PLUS_EXPR, addr_type, src_addr,
4838                                       size_int (src_offset));
4839               src = build_va_arg_indirect_ref (src_addr);
4840
4841               dest_addr = fold_convert (addr_type, addr);
4842               dest_addr = fold_build2 (PLUS_EXPR, addr_type, dest_addr,
4843                                        size_int (INTVAL (XEXP (slot, 1))));
4844               dest = build_va_arg_indirect_ref (dest_addr);
4845
4846               t = build2 (GIMPLE_MODIFY_STMT, void_type_node, dest, src);
4847               gimplify_and_add (t, pre_p);
4848             }
4849         }
4850
4851       if (needed_intregs)
4852         {
4853           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4854                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4855           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (gpr), gpr, t);
4856           gimplify_and_add (t, pre_p);
4857         }
4858       if (needed_sseregs)
4859         {
4860           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4861                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4862           t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (fpr), fpr, t);
4863           gimplify_and_add (t, pre_p);
4864         }
4865
4866       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4867       gimplify_and_add (t, pre_p);
4868
4869       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4870       append_to_statement_list (t, pre_p);
4871     }
4872
4873   /* ... otherwise out of the overflow area.  */
4874
4875   /* Care for on-stack alignment if needed.  */
4876   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4877       || integer_zerop (TYPE_SIZE (type)))
4878     t = ovf;
4879   else
4880     {
4881       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4882       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4883                   build_int_cst (TREE_TYPE (ovf), align - 1));
4884       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4885                   build_int_cst (TREE_TYPE (t), -align));
4886     }
4887   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4888
4889   t2 = build2 (GIMPLE_MODIFY_STMT, void_type_node, addr, t);
4890   gimplify_and_add (t2, pre_p);
4891
4892   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4893               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4894   t = build2 (GIMPLE_MODIFY_STMT, TREE_TYPE (ovf), ovf, t);
4895   gimplify_and_add (t, pre_p);
4896
4897   if (container)
4898     {
4899       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4900       append_to_statement_list (t, pre_p);
4901     }
4902
4903   ptrtype = build_pointer_type (type);
4904   addr = fold_convert (ptrtype, addr);
4905
4906   if (indirect_p)
4907     addr = build_va_arg_indirect_ref (addr);
4908   return build_va_arg_indirect_ref (addr);
4909 }
4910 \f
4911 /* Return nonzero if OPNUM's MEM should be matched
4912    in movabs* patterns.  */
4913
4914 int
4915 ix86_check_movabs (rtx insn, int opnum)
4916 {
4917   rtx set, mem;
4918
4919   set = PATTERN (insn);
4920   if (GET_CODE (set) == PARALLEL)
4921     set = XVECEXP (set, 0, 0);
4922   gcc_assert (GET_CODE (set) == SET);
4923   mem = XEXP (set, opnum);
4924   while (GET_CODE (mem) == SUBREG)
4925     mem = SUBREG_REG (mem);
4926   gcc_assert (MEM_P (mem));
4927   return (volatile_ok || !MEM_VOLATILE_P (mem));
4928 }
4929 \f
4930 /* Initialize the table of extra 80387 mathematical constants.  */
4931
4932 static void
4933 init_ext_80387_constants (void)
4934 {
4935   static const char * cst[5] =
4936   {
4937     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4938     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4939     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4940     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4941     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4942   };
4943   int i;
4944
4945   for (i = 0; i < 5; i++)
4946     {
4947       real_from_string (&ext_80387_constants_table[i], cst[i]);
4948       /* Ensure each constant is rounded to XFmode precision.  */
4949       real_convert (&ext_80387_constants_table[i],
4950                     XFmode, &ext_80387_constants_table[i]);
4951     }
4952
4953   ext_80387_constants_init = 1;
4954 }
4955
4956 /* Return true if the constant is something that can be loaded with
4957    a special instruction.  */
4958
4959 int
4960 standard_80387_constant_p (rtx x)
4961 {
4962   REAL_VALUE_TYPE r;
4963
4964   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4965     return -1;
4966
4967   if (x == CONST0_RTX (GET_MODE (x)))
4968     return 1;
4969   if (x == CONST1_RTX (GET_MODE (x)))
4970     return 2;
4971
4972   REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4973
4974   /* For XFmode constants, try to find a special 80387 instruction when
4975      optimizing for size or on those CPUs that benefit from them.  */
4976   if (GET_MODE (x) == XFmode
4977       && (optimize_size || TARGET_EXT_80387_CONSTANTS))
4978     {
4979       int i;
4980
4981       if (! ext_80387_constants_init)
4982         init_ext_80387_constants ();
4983
4984       for (i = 0; i < 5; i++)
4985         if (real_identical (&r, &ext_80387_constants_table[i]))
4986           return i + 3;
4987     }
4988
4989   /* Load of the constant -0.0 or -1.0 will be split as
4990      fldz;fchs or fld1;fchs sequence.  */
4991   if (real_isnegzero (&r))
4992     return 8;
4993   if (real_identical (&r, &dconstm1))
4994     return 9;
4995
4996   return 0;
4997 }
4998
4999 /* Return the opcode of the special instruction to be used to load
5000    the constant X.  */
5001
5002 const char *
5003 standard_80387_constant_opcode (rtx x)
5004 {
5005   switch (standard_80387_constant_p (x))
5006     {
5007     case 1:
5008       return "fldz";
5009     case 2:
5010       return "fld1";
5011     case 3:
5012       return "fldlg2";
5013     case 4:
5014       return "fldln2";
5015     case 5:
5016       return "fldl2e";
5017     case 6:
5018       return "fldl2t";
5019     case 7:
5020       return "fldpi";
5021     case 8:
5022     case 9:
5023       return "#";
5024     default:
5025       gcc_unreachable ();
5026     }
5027 }
5028
5029 /* Return the CONST_DOUBLE representing the 80387 constant that is
5030    loaded by the specified special instruction.  The argument IDX
5031    matches the return value from standard_80387_constant_p.  */
5032
5033 rtx
5034 standard_80387_constant_rtx (int idx)
5035 {
5036   int i;
5037
5038   if (! ext_80387_constants_init)
5039     init_ext_80387_constants ();
5040
5041   switch (idx)
5042     {
5043     case 3:
5044     case 4:
5045     case 5:
5046     case 6:
5047     case 7:
5048       i = idx - 3;
5049       break;
5050
5051     default:
5052       gcc_unreachable ();
5053     }
5054
5055   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
5056                                        XFmode);
5057 }
5058
5059 /* Return 1 if mode is a valid mode for sse.  */
5060 static int
5061 standard_sse_mode_p (enum machine_mode mode)
5062 {
5063   switch (mode)
5064     {
5065     case V16QImode:
5066     case V8HImode:
5067     case V4SImode:
5068     case V2DImode:
5069     case V4SFmode:
5070     case V2DFmode:
5071       return 1;
5072
5073     default:
5074       return 0;
5075     }
5076 }
5077
5078 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
5079  */
5080 int
5081 standard_sse_constant_p (rtx x)
5082 {
5083   enum machine_mode mode = GET_MODE (x);
5084
5085   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
5086     return 1;
5087   if (vector_all_ones_operand (x, mode)
5088       && standard_sse_mode_p (mode))
5089     return TARGET_SSE2 ? 2 : -1;
5090
5091   return 0;
5092 }
5093
5094 /* Return the opcode of the special instruction to be used to load
5095    the constant X.  */
5096
5097 const char *
5098 standard_sse_constant_opcode (rtx insn, rtx x)
5099 {
5100   switch (standard_sse_constant_p (x))
5101     {
5102     case 1:
5103       if (get_attr_mode (insn) == MODE_V4SF)
5104         return "xorps\t%0, %0";
5105       else if (get_attr_mode (insn) == MODE_V2DF)
5106         return "xorpd\t%0, %0";
5107       else
5108         return "pxor\t%0, %0";
5109     case 2:
5110       return "pcmpeqd\t%0, %0";
5111     }
5112   gcc_unreachable ();
5113 }
5114
5115 /* Returns 1 if OP contains a symbol reference */
5116
5117 int
5118 symbolic_reference_mentioned_p (rtx op)
5119 {
5120   const char *fmt;
5121   int i;
5122
5123   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
5124     return 1;
5125
5126   fmt = GET_RTX_FORMAT (GET_CODE (op));
5127   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5128     {
5129       if (fmt[i] == 'E')
5130         {
5131           int j;
5132
5133           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5134             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
5135               return 1;
5136         }
5137
5138       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
5139         return 1;
5140     }
5141
5142   return 0;
5143 }
5144
5145 /* Return 1 if it is appropriate to emit `ret' instructions in the
5146    body of a function.  Do this only if the epilogue is simple, needing a
5147    couple of insns.  Prior to reloading, we can't tell how many registers
5148    must be saved, so return 0 then.  Return 0 if there is no frame
5149    marker to de-allocate.  */
5150
5151 int
5152 ix86_can_use_return_insn_p (void)
5153 {
5154   struct ix86_frame frame;
5155
5156   if (! reload_completed || frame_pointer_needed)
5157     return 0;
5158
5159   /* Don't allow more than 32 pop, since that's all we can do
5160      with one instruction.  */
5161   if (current_function_pops_args
5162       && current_function_args_size >= 32768)
5163     return 0;
5164
5165   ix86_compute_frame_layout (&frame);
5166   return frame.to_allocate == 0 && frame.nregs == 0;
5167 }
5168 \f
5169 /* Value should be nonzero if functions must have frame pointers.
5170    Zero means the frame pointer need not be set up (and parms may
5171    be accessed via the stack pointer) in functions that seem suitable.  */
5172
5173 int
5174 ix86_frame_pointer_required (void)
5175 {
5176   /* If we accessed previous frames, then the generated code expects
5177      to be able to access the saved ebp value in our frame.  */
5178   if (cfun->machine->accesses_prev_frame)
5179     return 1;
5180
5181   /* Several x86 os'es need a frame pointer for other reasons,
5182      usually pertaining to setjmp.  */
5183   if (SUBTARGET_FRAME_POINTER_REQUIRED)
5184     return 1;
5185
5186   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
5187      the frame pointer by default.  Turn it back on now if we've not
5188      got a leaf function.  */
5189   if (TARGET_OMIT_LEAF_FRAME_POINTER
5190       && (!current_function_is_leaf
5191           || ix86_current_function_calls_tls_descriptor))
5192     return 1;
5193
5194   if (current_function_profile)
5195     return 1;
5196
5197   return 0;
5198 }
5199
5200 /* Record that the current function accesses previous call frames.  */
5201
5202 void
5203 ix86_setup_frame_addresses (void)
5204 {
5205   cfun->machine->accesses_prev_frame = 1;
5206 }
5207 \f
5208 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
5209 # define USE_HIDDEN_LINKONCE 1
5210 #else
5211 # define USE_HIDDEN_LINKONCE 0
5212 #endif
5213
5214 static int pic_labels_used;
5215
5216 /* Fills in the label name that should be used for a pc thunk for
5217    the given register.  */
5218
5219 static void
5220 get_pc_thunk_name (char name[32], unsigned int regno)
5221 {
5222   gcc_assert (!TARGET_64BIT);
5223
5224   if (USE_HIDDEN_LINKONCE)
5225     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
5226   else
5227     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
5228 }
5229
5230
5231 /* This function generates code for -fpic that loads %ebx with
5232    the return address of the caller and then returns.  */
5233
5234 void
5235 ix86_file_end (void)
5236 {
5237   rtx xops[2];
5238   int regno;
5239
5240   for (regno = 0; regno < 8; ++regno)
5241     {
5242       char name[32];
5243
5244       if (! ((pic_labels_used >> regno) & 1))
5245         continue;
5246
5247       get_pc_thunk_name (name, regno);
5248
5249 #if TARGET_MACHO
5250       if (TARGET_MACHO)
5251         {
5252           switch_to_section (darwin_sections[text_coal_section]);
5253           fputs ("\t.weak_definition\t", asm_out_file);
5254           assemble_name (asm_out_file, name);
5255           fputs ("\n\t.private_extern\t", asm_out_file);
5256           assemble_name (asm_out_file, name);
5257           fputs ("\n", asm_out_file);
5258           ASM_OUTPUT_LABEL (asm_out_file, name);
5259         }
5260       else
5261 #endif
5262       if (USE_HIDDEN_LINKONCE)
5263         {
5264           tree decl;
5265
5266           decl = build_decl (FUNCTION_DECL, get_identifier (name),
5267                              error_mark_node);
5268           TREE_PUBLIC (decl) = 1;
5269           TREE_STATIC (decl) = 1;
5270           DECL_ONE_ONLY (decl) = 1;
5271
5272           (*targetm.asm_out.unique_section) (decl, 0);
5273           switch_to_section (get_named_section (decl, NULL, 0));
5274
5275           (*targetm.asm_out.globalize_label) (asm_out_file, name);
5276           fputs ("\t.hidden\t", asm_out_file);
5277           assemble_name (asm_out_file, name);
5278           fputc ('\n', asm_out_file);
5279           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
5280         }
5281       else
5282         {
5283           switch_to_section (text_section);
5284           ASM_OUTPUT_LABEL (asm_out_file, name);
5285         }
5286
5287       xops[0] = gen_rtx_REG (SImode, regno);
5288       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
5289       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
5290       output_asm_insn ("ret", xops);
5291     }
5292
5293   if (NEED_INDICATE_EXEC_STACK)
5294     file_end_indicate_exec_stack ();
5295 }
5296
5297 /* Emit code for the SET_GOT patterns.  */
5298
5299 const char *
5300 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
5301 {
5302   rtx xops[3];
5303
5304   xops[0] = dest;
5305
5306   if (TARGET_VXWORKS_RTP && flag_pic)
5307     {
5308       /* Load (*VXWORKS_GOTT_BASE) into the PIC register.  */
5309       xops[2] = gen_rtx_MEM (Pmode,
5310                              gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_BASE));
5311       output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5312
5313       /* Load (*VXWORKS_GOTT_BASE)[VXWORKS_GOTT_INDEX] into the PIC register.
5314          Use %P and a local symbol in order to print VXWORKS_GOTT_INDEX as
5315          an unadorned address.  */
5316       xops[2] = gen_rtx_SYMBOL_REF (Pmode, VXWORKS_GOTT_INDEX);
5317       SYMBOL_REF_FLAGS (xops[2]) |= SYMBOL_FLAG_LOCAL;
5318       output_asm_insn ("mov{l}\t{%P2(%0), %0|%0, DWORD PTR %P2[%0]}", xops);
5319       return "";
5320     }
5321
5322   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
5323
5324   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
5325     {
5326       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
5327
5328       if (!flag_pic)
5329         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
5330       else
5331         output_asm_insn ("call\t%a2", xops);
5332
5333 #if TARGET_MACHO
5334       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5335          is what will be referenced by the Mach-O PIC subsystem.  */
5336       if (!label)
5337         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5338 #endif
5339
5340       (*targetm.asm_out.internal_label) (asm_out_file, "L",
5341                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
5342
5343       if (flag_pic)
5344         output_asm_insn ("pop{l}\t%0", xops);
5345     }
5346   else
5347     {
5348       char name[32];
5349       get_pc_thunk_name (name, REGNO (dest));
5350       pic_labels_used |= 1 << REGNO (dest);
5351
5352       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
5353       xops[2] = gen_rtx_MEM (QImode, xops[2]);
5354       output_asm_insn ("call\t%X2", xops);
5355       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
5356          is what will be referenced by the Mach-O PIC subsystem.  */
5357 #if TARGET_MACHO
5358       if (!label)
5359         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
5360       else
5361         targetm.asm_out.internal_label (asm_out_file, "L",
5362                                            CODE_LABEL_NUMBER (label));
5363 #endif
5364     }
5365
5366   if (TARGET_MACHO)
5367     return "";
5368
5369   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
5370     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
5371   else
5372     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
5373
5374   return "";
5375 }
5376
5377 /* Generate an "push" pattern for input ARG.  */
5378
5379 static rtx
5380 gen_push (rtx arg)
5381 {
5382   return gen_rtx_SET (VOIDmode,
5383                       gen_rtx_MEM (Pmode,
5384                                    gen_rtx_PRE_DEC (Pmode,
5385                                                     stack_pointer_rtx)),
5386                       arg);
5387 }
5388
5389 /* Return >= 0 if there is an unused call-clobbered register available
5390    for the entire function.  */
5391
5392 static unsigned int
5393 ix86_select_alt_pic_regnum (void)
5394 {
5395   if (current_function_is_leaf && !current_function_profile
5396       && !ix86_current_function_calls_tls_descriptor)
5397     {
5398       int i;
5399       for (i = 2; i >= 0; --i)
5400         if (!regs_ever_live[i])
5401           return i;
5402     }
5403
5404   return INVALID_REGNUM;
5405 }
5406
5407 /* Return 1 if we need to save REGNO.  */
5408 static int
5409 ix86_save_reg (unsigned int regno, int maybe_eh_return)
5410 {
5411   if (pic_offset_table_rtx
5412       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5413       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5414           || current_function_profile
5415           || current_function_calls_eh_return
5416           || current_function_uses_const_pool))
5417     {
5418       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5419         return 0;
5420       return 1;
5421     }
5422
5423   if (current_function_calls_eh_return && maybe_eh_return)
5424     {
5425       unsigned i;
5426       for (i = 0; ; i++)
5427         {
5428           unsigned test = EH_RETURN_DATA_REGNO (i);
5429           if (test == INVALID_REGNUM)
5430             break;
5431           if (test == regno)
5432             return 1;
5433         }
5434     }
5435
5436   if (cfun->machine->force_align_arg_pointer
5437       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5438     return 1;
5439
5440   return (regs_ever_live[regno]
5441           && !call_used_regs[regno]
5442           && !fixed_regs[regno]
5443           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5444 }
5445
5446 /* Return number of registers to be saved on the stack.  */
5447
5448 static int
5449 ix86_nsaved_regs (void)
5450 {
5451   int nregs = 0;
5452   int regno;
5453
5454   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5455     if (ix86_save_reg (regno, true))
5456       nregs++;
5457   return nregs;
5458 }
5459
5460 /* Return the offset between two registers, one to be eliminated, and the other
5461    its replacement, at the start of a routine.  */
5462
5463 HOST_WIDE_INT
5464 ix86_initial_elimination_offset (int from, int to)
5465 {
5466   struct ix86_frame frame;
5467   ix86_compute_frame_layout (&frame);
5468
5469   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5470     return frame.hard_frame_pointer_offset;
5471   else if (from == FRAME_POINTER_REGNUM
5472            && to == HARD_FRAME_POINTER_REGNUM)
5473     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5474   else
5475     {
5476       gcc_assert (to == STACK_POINTER_REGNUM);
5477
5478       if (from == ARG_POINTER_REGNUM)
5479         return frame.stack_pointer_offset;
5480
5481       gcc_assert (from == FRAME_POINTER_REGNUM);
5482       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5483     }
5484 }
5485
5486 /* Fill structure ix86_frame about frame of currently computed function.  */
5487
5488 static void
5489 ix86_compute_frame_layout (struct ix86_frame *frame)
5490 {
5491   HOST_WIDE_INT total_size;
5492   unsigned int stack_alignment_needed;
5493   HOST_WIDE_INT offset;
5494   unsigned int preferred_alignment;
5495   HOST_WIDE_INT size = get_frame_size ();
5496
5497   frame->nregs = ix86_nsaved_regs ();
5498   total_size = size;
5499
5500   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5501   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5502
5503   /* During reload iteration the amount of registers saved can change.
5504      Recompute the value as needed.  Do not recompute when amount of registers
5505      didn't change as reload does multiple calls to the function and does not
5506      expect the decision to change within single iteration.  */
5507   if (!optimize_size
5508       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5509     {
5510       int count = frame->nregs;
5511
5512       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5513       /* The fast prologue uses move instead of push to save registers.  This
5514          is significantly longer, but also executes faster as modern hardware
5515          can execute the moves in parallel, but can't do that for push/pop.
5516
5517          Be careful about choosing what prologue to emit:  When function takes
5518          many instructions to execute we may use slow version as well as in
5519          case function is known to be outside hot spot (this is known with
5520          feedback only).  Weight the size of function by number of registers
5521          to save as it is cheap to use one or two push instructions but very
5522          slow to use many of them.  */
5523       if (count)
5524         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5525       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5526           || (flag_branch_probabilities
5527               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5528         cfun->machine->use_fast_prologue_epilogue = false;
5529       else
5530         cfun->machine->use_fast_prologue_epilogue
5531            = !expensive_function_p (count);
5532     }
5533   if (TARGET_PROLOGUE_USING_MOVE
5534       && cfun->machine->use_fast_prologue_epilogue)
5535     frame->save_regs_using_mov = true;
5536   else
5537     frame->save_regs_using_mov = false;
5538
5539
5540   /* Skip return address and saved base pointer.  */
5541   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5542
5543   frame->hard_frame_pointer_offset = offset;
5544
5545   /* Do some sanity checking of stack_alignment_needed and
5546      preferred_alignment, since i386 port is the only using those features
5547      that may break easily.  */
5548
5549   gcc_assert (!size || stack_alignment_needed);
5550   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5551   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5552   gcc_assert (stack_alignment_needed
5553               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5554
5555   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5556     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5557
5558   /* Register save area */
5559   offset += frame->nregs * UNITS_PER_WORD;
5560
5561   /* Va-arg area */
5562   if (ix86_save_varrargs_registers)
5563     {
5564       offset += X86_64_VARARGS_SIZE;
5565       frame->va_arg_size = X86_64_VARARGS_SIZE;
5566     }
5567   else
5568     frame->va_arg_size = 0;
5569
5570   /* Align start of frame for local function.  */
5571   frame->padding1 = ((offset + stack_alignment_needed - 1)
5572                      & -stack_alignment_needed) - offset;
5573
5574   offset += frame->padding1;
5575
5576   /* Frame pointer points here.  */
5577   frame->frame_pointer_offset = offset;
5578
5579   offset += size;
5580
5581   /* Add outgoing arguments area.  Can be skipped if we eliminated
5582      all the function calls as dead code.
5583      Skipping is however impossible when function calls alloca.  Alloca
5584      expander assumes that last current_function_outgoing_args_size
5585      of stack frame are unused.  */
5586   if (ACCUMULATE_OUTGOING_ARGS
5587       && (!current_function_is_leaf || current_function_calls_alloca
5588           || ix86_current_function_calls_tls_descriptor))
5589     {
5590       offset += current_function_outgoing_args_size;
5591       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5592     }
5593   else
5594     frame->outgoing_arguments_size = 0;
5595
5596   /* Align stack boundary.  Only needed if we're calling another function
5597      or using alloca.  */
5598   if (!current_function_is_leaf || current_function_calls_alloca
5599       || ix86_current_function_calls_tls_descriptor)
5600     frame->padding2 = ((offset + preferred_alignment - 1)
5601                        & -preferred_alignment) - offset;
5602   else
5603     frame->padding2 = 0;
5604
5605   offset += frame->padding2;
5606
5607   /* We've reached end of stack frame.  */
5608   frame->stack_pointer_offset = offset;
5609
5610   /* Size prologue needs to allocate.  */
5611   frame->to_allocate =
5612     (size + frame->padding1 + frame->padding2
5613      + frame->outgoing_arguments_size + frame->va_arg_size);
5614
5615   if ((!frame->to_allocate && frame->nregs <= 1)
5616       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5617     frame->save_regs_using_mov = false;
5618
5619   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5620       && current_function_is_leaf
5621       && !ix86_current_function_calls_tls_descriptor)
5622     {
5623       frame->red_zone_size = frame->to_allocate;
5624       if (frame->save_regs_using_mov)
5625         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5626       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5627         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5628     }
5629   else
5630     frame->red_zone_size = 0;
5631   frame->to_allocate -= frame->red_zone_size;
5632   frame->stack_pointer_offset -= frame->red_zone_size;
5633 #if 0
5634   fprintf (stderr, "\n");
5635   fprintf (stderr, "nregs: %ld\n", (long)frame->nregs);
5636   fprintf (stderr, "size: %ld\n", (long)size);
5637   fprintf (stderr, "alignment1: %ld\n", (long)stack_alignment_needed);
5638   fprintf (stderr, "padding1: %ld\n", (long)frame->padding1);
5639   fprintf (stderr, "va_arg: %ld\n", (long)frame->va_arg_size);
5640   fprintf (stderr, "padding2: %ld\n", (long)frame->padding2);
5641   fprintf (stderr, "to_allocate: %ld\n", (long)frame->to_allocate);
5642   fprintf (stderr, "red_zone_size: %ld\n", (long)frame->red_zone_size);
5643   fprintf (stderr, "frame_pointer_offset: %ld\n", (long)frame->frame_pointer_offset);
5644   fprintf (stderr, "hard_frame_pointer_offset: %ld\n",
5645            (long)frame->hard_frame_pointer_offset);
5646   fprintf (stderr, "stack_pointer_offset: %ld\n", (long)frame->stack_pointer_offset);
5647   fprintf (stderr, "current_function_is_leaf: %ld\n", (long)current_function_is_leaf);
5648   fprintf (stderr, "current_function_calls_alloca: %ld\n", (long)current_function_calls_alloca);
5649   fprintf (stderr, "x86_current_function_calls_tls_descriptor: %ld\n", (long)ix86_current_function_calls_tls_descriptor);
5650 #endif
5651 }
5652
5653 /* Emit code to save registers in the prologue.  */
5654
5655 static void
5656 ix86_emit_save_regs (void)
5657 {
5658   unsigned int regno;
5659   rtx insn;
5660
5661   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5662     if (ix86_save_reg (regno, true))
5663       {
5664         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5665         RTX_FRAME_RELATED_P (insn) = 1;
5666       }
5667 }
5668
5669 /* Emit code to save registers using MOV insns.  First register
5670    is restored from POINTER + OFFSET.  */
5671 static void
5672 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5673 {
5674   unsigned int regno;
5675   rtx insn;
5676
5677   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5678     if (ix86_save_reg (regno, true))
5679       {
5680         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5681                                                Pmode, offset),
5682                                gen_rtx_REG (Pmode, regno));
5683         RTX_FRAME_RELATED_P (insn) = 1;
5684         offset += UNITS_PER_WORD;
5685       }
5686 }
5687
5688 /* Expand prologue or epilogue stack adjustment.
5689    The pattern exist to put a dependency on all ebp-based memory accesses.
5690    STYLE should be negative if instructions should be marked as frame related,
5691    zero if %r11 register is live and cannot be freely used and positive
5692    otherwise.  */
5693
5694 static void
5695 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5696 {
5697   rtx insn;
5698
5699   if (! TARGET_64BIT)
5700     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5701   else if (x86_64_immediate_operand (offset, DImode))
5702     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5703   else
5704     {
5705       rtx r11;
5706       /* r11 is used by indirect sibcall return as well, set before the
5707          epilogue and used after the epilogue.  ATM indirect sibcall
5708          shouldn't be used together with huge frame sizes in one
5709          function because of the frame_size check in sibcall.c.  */
5710       gcc_assert (style);
5711       r11 = gen_rtx_REG (DImode, R11_REG);
5712       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5713       if (style < 0)
5714         RTX_FRAME_RELATED_P (insn) = 1;
5715       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5716                                                                offset));
5717     }
5718   if (style < 0)
5719     RTX_FRAME_RELATED_P (insn) = 1;
5720 }
5721
5722 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5723
5724 static rtx
5725 ix86_internal_arg_pointer (void)
5726 {
5727   bool has_force_align_arg_pointer =
5728     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5729                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5730   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5731        && DECL_NAME (current_function_decl)
5732        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5733        && DECL_FILE_SCOPE_P (current_function_decl))
5734       || ix86_force_align_arg_pointer
5735       || has_force_align_arg_pointer)
5736     {
5737       /* Nested functions can't realign the stack due to a register
5738          conflict.  */
5739       if (DECL_CONTEXT (current_function_decl)
5740           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5741         {
5742           if (ix86_force_align_arg_pointer)
5743             warning (0, "-mstackrealign ignored for nested functions");
5744           if (has_force_align_arg_pointer)
5745             error ("%s not supported for nested functions",
5746                    ix86_force_align_arg_pointer_string);
5747           return virtual_incoming_args_rtx;
5748         }
5749       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5750       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5751     }
5752   else
5753     return virtual_incoming_args_rtx;
5754 }
5755
5756 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5757    This is called from dwarf2out.c to emit call frame instructions
5758    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5759 static void
5760 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5761 {
5762   rtx unspec = SET_SRC (pattern);
5763   gcc_assert (GET_CODE (unspec) == UNSPEC);
5764
5765   switch (index)
5766     {
5767     case UNSPEC_REG_SAVE:
5768       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5769                               SET_DEST (pattern));
5770       break;
5771     case UNSPEC_DEF_CFA:
5772       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5773                          INTVAL (XVECEXP (unspec, 0, 0)));
5774       break;
5775     default:
5776       gcc_unreachable ();
5777     }
5778 }
5779
5780 /* Expand the prologue into a bunch of separate insns.  */
5781
5782 void
5783 ix86_expand_prologue (void)
5784 {
5785   rtx insn;
5786   bool pic_reg_used;
5787   struct ix86_frame frame;
5788   HOST_WIDE_INT allocate;
5789
5790   ix86_compute_frame_layout (&frame);
5791
5792   if (cfun->machine->force_align_arg_pointer)
5793     {
5794       rtx x, y;
5795
5796       /* Grab the argument pointer.  */
5797       x = plus_constant (stack_pointer_rtx, 4);
5798       y = cfun->machine->force_align_arg_pointer;
5799       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5800       RTX_FRAME_RELATED_P (insn) = 1;
5801
5802       /* The unwind info consists of two parts: install the fafp as the cfa,
5803          and record the fafp as the "save register" of the stack pointer.
5804          The later is there in order that the unwinder can see where it
5805          should restore the stack pointer across the and insn.  */
5806       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5807       x = gen_rtx_SET (VOIDmode, y, x);
5808       RTX_FRAME_RELATED_P (x) = 1;
5809       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5810                           UNSPEC_REG_SAVE);
5811       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5812       RTX_FRAME_RELATED_P (y) = 1;
5813       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5814       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5815       REG_NOTES (insn) = x;
5816
5817       /* Align the stack.  */
5818       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5819                              GEN_INT (-16)));
5820
5821       /* And here we cheat like madmen with the unwind info.  We force the
5822          cfa register back to sp+4, which is exactly what it was at the
5823          start of the function.  Re-pushing the return address results in
5824          the return at the same spot relative to the cfa, and thus is
5825          correct wrt the unwind info.  */
5826       x = cfun->machine->force_align_arg_pointer;
5827       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5828       insn = emit_insn (gen_push (x));
5829       RTX_FRAME_RELATED_P (insn) = 1;
5830
5831       x = GEN_INT (4);
5832       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5833       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5834       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5835       REG_NOTES (insn) = x;
5836     }
5837
5838   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5839      slower on all targets.  Also sdb doesn't like it.  */
5840
5841   if (frame_pointer_needed)
5842     {
5843       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5844       RTX_FRAME_RELATED_P (insn) = 1;
5845
5846       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5847       RTX_FRAME_RELATED_P (insn) = 1;
5848     }
5849
5850   allocate = frame.to_allocate;
5851
5852   if (!frame.save_regs_using_mov)
5853     ix86_emit_save_regs ();
5854   else
5855     allocate += frame.nregs * UNITS_PER_WORD;
5856
5857   /* When using red zone we may start register saving before allocating
5858      the stack frame saving one cycle of the prologue.  */
5859   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5860     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5861                                    : stack_pointer_rtx,
5862                                    -frame.nregs * UNITS_PER_WORD);
5863
5864   if (allocate == 0)
5865     ;
5866   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5867     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5868                                GEN_INT (-allocate), -1);
5869   else
5870     {
5871       /* Only valid for Win32.  */
5872       rtx eax = gen_rtx_REG (Pmode, 0);
5873       bool eax_live;
5874       rtx t;
5875
5876       gcc_assert (!TARGET_64BIT || TARGET_64BIT_MS_ABI);
5877
5878       if (TARGET_64BIT_MS_ABI)
5879         eax_live = false;
5880       else
5881         eax_live = ix86_eax_live_at_start_p ();
5882
5883       if (eax_live)
5884         {
5885           emit_insn (gen_push (eax));
5886           allocate -= UNITS_PER_WORD;
5887         }
5888
5889       emit_move_insn (eax, GEN_INT (allocate));
5890
5891       if (TARGET_64BIT)
5892         insn = gen_allocate_stack_worker_64 (eax);
5893       else
5894         insn = gen_allocate_stack_worker_32 (eax);
5895       insn = emit_insn (insn);
5896       RTX_FRAME_RELATED_P (insn) = 1;
5897       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5898       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5899       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5900                                             t, REG_NOTES (insn));
5901
5902       if (eax_live)
5903         {
5904           if (frame_pointer_needed)
5905             t = plus_constant (hard_frame_pointer_rtx,
5906                                allocate
5907                                - frame.to_allocate
5908                                - frame.nregs * UNITS_PER_WORD);
5909           else
5910             t = plus_constant (stack_pointer_rtx, allocate);
5911           emit_move_insn (eax, gen_rtx_MEM (Pmode, t));
5912         }
5913     }
5914
5915   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5916     {
5917       if (!frame_pointer_needed || !frame.to_allocate)
5918         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5919       else
5920         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5921                                        -frame.nregs * UNITS_PER_WORD);
5922     }
5923
5924   pic_reg_used = false;
5925   if (pic_offset_table_rtx
5926       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5927           || current_function_profile))
5928     {
5929       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5930
5931       if (alt_pic_reg_used != INVALID_REGNUM)
5932         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5933
5934       pic_reg_used = true;
5935     }
5936
5937   if (pic_reg_used)
5938     {
5939       if (TARGET_64BIT)
5940         {
5941           if (ix86_cmodel == CM_LARGE_PIC)
5942             {
5943               rtx tmp_reg = gen_rtx_REG (DImode,
5944                                          FIRST_REX_INT_REG + 3 /* R11 */);
5945               rtx label = gen_label_rtx ();
5946               emit_label (label);
5947               LABEL_PRESERVE_P (label) = 1;
5948               gcc_assert (REGNO (pic_offset_table_rtx) != REGNO (tmp_reg));
5949               insn = emit_insn (gen_set_rip_rex64 (pic_offset_table_rtx, label));
5950               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5951               insn = emit_insn (gen_set_got_offset_rex64 (tmp_reg, label));
5952               REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5953               insn = emit_insn (gen_adddi3 (pic_offset_table_rtx,
5954                                             pic_offset_table_rtx, tmp_reg));
5955             }
5956           else
5957             insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5958         }
5959       else
5960         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5961
5962       /* Even with accurate pre-reload life analysis, we can wind up
5963          deleting all references to the pic register after reload.
5964          Consider if cross-jumping unifies two sides of a branch
5965          controlled by a comparison vs the only read from a global.
5966          In which case, allow the set_got to be deleted, though we're
5967          too late to do anything about the ebx save in the prologue.  */
5968       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5969     }
5970
5971   /* Prevent function calls from be scheduled before the call to mcount.
5972      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5973   if (current_function_profile)
5974     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5975 }
5976
5977 /* Emit code to restore saved registers using MOV insns.  First register
5978    is restored from POINTER + OFFSET.  */
5979 static void
5980 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5981                                   int maybe_eh_return)
5982 {
5983   int regno;
5984   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5985
5986   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5987     if (ix86_save_reg (regno, maybe_eh_return))
5988       {
5989         /* Ensure that adjust_address won't be forced to produce pointer
5990            out of range allowed by x86-64 instruction set.  */
5991         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5992           {
5993             rtx r11;
5994
5995             r11 = gen_rtx_REG (DImode, R11_REG);
5996             emit_move_insn (r11, GEN_INT (offset));
5997             emit_insn (gen_adddi3 (r11, r11, pointer));
5998             base_address = gen_rtx_MEM (Pmode, r11);
5999             offset = 0;
6000           }
6001         emit_move_insn (gen_rtx_REG (Pmode, regno),
6002                         adjust_address (base_address, Pmode, offset));
6003         offset += UNITS_PER_WORD;
6004       }
6005 }
6006
6007 /* Restore function stack, frame, and registers.  */
6008
6009 void
6010 ix86_expand_epilogue (int style)
6011 {
6012   int regno;
6013   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
6014   struct ix86_frame frame;
6015   HOST_WIDE_INT offset;
6016
6017   ix86_compute_frame_layout (&frame);
6018
6019   /* Calculate start of saved registers relative to ebp.  Special care
6020      must be taken for the normal return case of a function using
6021      eh_return: the eax and edx registers are marked as saved, but not
6022      restored along this path.  */
6023   offset = frame.nregs;
6024   if (current_function_calls_eh_return && style != 2)
6025     offset -= 2;
6026   offset *= -UNITS_PER_WORD;
6027
6028   /* If we're only restoring one register and sp is not valid then
6029      using a move instruction to restore the register since it's
6030      less work than reloading sp and popping the register.
6031
6032      The default code result in stack adjustment using add/lea instruction,
6033      while this code results in LEAVE instruction (or discrete equivalent),
6034      so it is profitable in some other cases as well.  Especially when there
6035      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
6036      and there is exactly one register to pop. This heuristic may need some
6037      tuning in future.  */
6038   if ((!sp_valid && frame.nregs <= 1)
6039       || (TARGET_EPILOGUE_USING_MOVE
6040           && cfun->machine->use_fast_prologue_epilogue
6041           && (frame.nregs > 1 || frame.to_allocate))
6042       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
6043       || (frame_pointer_needed && TARGET_USE_LEAVE
6044           && cfun->machine->use_fast_prologue_epilogue
6045           && frame.nregs == 1)
6046       || current_function_calls_eh_return)
6047     {
6048       /* Restore registers.  We can use ebp or esp to address the memory
6049          locations.  If both are available, default to ebp, since offsets
6050          are known to be small.  Only exception is esp pointing directly to the
6051          end of block of saved registers, where we may simplify addressing
6052          mode.  */
6053
6054       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
6055         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
6056                                           frame.to_allocate, style == 2);
6057       else
6058         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
6059                                           offset, style == 2);
6060
6061       /* eh_return epilogues need %ecx added to the stack pointer.  */
6062       if (style == 2)
6063         {
6064           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
6065
6066           if (frame_pointer_needed)
6067             {
6068               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
6069               tmp = plus_constant (tmp, UNITS_PER_WORD);
6070               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
6071
6072               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
6073               emit_move_insn (hard_frame_pointer_rtx, tmp);
6074
6075               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
6076                                          const0_rtx, style);
6077             }
6078           else
6079             {
6080               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
6081               tmp = plus_constant (tmp, (frame.to_allocate
6082                                          + frame.nregs * UNITS_PER_WORD));
6083               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
6084             }
6085         }
6086       else if (!frame_pointer_needed)
6087         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6088                                    GEN_INT (frame.to_allocate
6089                                             + frame.nregs * UNITS_PER_WORD),
6090                                    style);
6091       /* If not an i386, mov & pop is faster than "leave".  */
6092       else if (TARGET_USE_LEAVE || optimize_size
6093                || !cfun->machine->use_fast_prologue_epilogue)
6094         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6095       else
6096         {
6097           pro_epilogue_adjust_stack (stack_pointer_rtx,
6098                                      hard_frame_pointer_rtx,
6099                                      const0_rtx, style);
6100           if (TARGET_64BIT)
6101             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6102           else
6103             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6104         }
6105     }
6106   else
6107     {
6108       /* First step is to deallocate the stack frame so that we can
6109          pop the registers.  */
6110       if (!sp_valid)
6111         {
6112           gcc_assert (frame_pointer_needed);
6113           pro_epilogue_adjust_stack (stack_pointer_rtx,
6114                                      hard_frame_pointer_rtx,
6115                                      GEN_INT (offset), style);
6116         }
6117       else if (frame.to_allocate)
6118         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
6119                                    GEN_INT (frame.to_allocate), style);
6120
6121       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6122         if (ix86_save_reg (regno, false))
6123           {
6124             if (TARGET_64BIT)
6125               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
6126             else
6127               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
6128           }
6129       if (frame_pointer_needed)
6130         {
6131           /* Leave results in shorter dependency chains on CPUs that are
6132              able to grok it fast.  */
6133           if (TARGET_USE_LEAVE)
6134             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
6135           else if (TARGET_64BIT)
6136             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
6137           else
6138             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
6139         }
6140     }
6141
6142   if (cfun->machine->force_align_arg_pointer)
6143     {
6144       emit_insn (gen_addsi3 (stack_pointer_rtx,
6145                              cfun->machine->force_align_arg_pointer,
6146                              GEN_INT (-4)));
6147     }
6148
6149   /* Sibcall epilogues don't want a return instruction.  */
6150   if (style == 0)
6151     return;
6152
6153   if (current_function_pops_args && current_function_args_size)
6154     {
6155       rtx popc = GEN_INT (current_function_pops_args);
6156
6157       /* i386 can only pop 64K bytes.  If asked to pop more, pop
6158          return address, do explicit add, and jump indirectly to the
6159          caller.  */
6160
6161       if (current_function_pops_args >= 65536)
6162         {
6163           rtx ecx = gen_rtx_REG (SImode, 2);
6164
6165           /* There is no "pascal" calling convention in any 64bit ABI.  */
6166           gcc_assert (!TARGET_64BIT);
6167
6168           emit_insn (gen_popsi1 (ecx));
6169           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
6170           emit_jump_insn (gen_return_indirect_internal (ecx));
6171         }
6172       else
6173         emit_jump_insn (gen_return_pop_internal (popc));
6174     }
6175   else
6176     emit_jump_insn (gen_return_internal ());
6177 }
6178
6179 /* Reset from the function's potential modifications.  */
6180
6181 static void
6182 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
6183                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
6184 {
6185   if (pic_offset_table_rtx)
6186     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
6187 #if TARGET_MACHO
6188   /* Mach-O doesn't support labels at the end of objects, so if
6189      it looks like we might want one, insert a NOP.  */
6190   {
6191     rtx insn = get_last_insn ();
6192     while (insn
6193            && NOTE_P (insn)
6194            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
6195       insn = PREV_INSN (insn);
6196     if (insn
6197         && (LABEL_P (insn)
6198             || (NOTE_P (insn)
6199                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
6200       fputs ("\tnop\n", file);
6201   }
6202 #endif
6203
6204 }
6205 \f
6206 /* Extract the parts of an RTL expression that is a valid memory address
6207    for an instruction.  Return 0 if the structure of the address is
6208    grossly off.  Return -1 if the address contains ASHIFT, so it is not
6209    strictly valid, but still used for computing length of lea instruction.  */
6210
6211 int
6212 ix86_decompose_address (rtx addr, struct ix86_address *out)
6213 {
6214   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
6215   rtx base_reg, index_reg;
6216   HOST_WIDE_INT scale = 1;
6217   rtx scale_rtx = NULL_RTX;
6218   int retval = 1;
6219   enum ix86_address_seg seg = SEG_DEFAULT;
6220
6221   if (REG_P (addr) || GET_CODE (addr) == SUBREG)
6222     base = addr;
6223   else if (GET_CODE (addr) == PLUS)
6224     {
6225       rtx addends[4], op;
6226       int n = 0, i;
6227
6228       op = addr;
6229       do
6230         {
6231           if (n >= 4)
6232             return 0;
6233           addends[n++] = XEXP (op, 1);
6234           op = XEXP (op, 0);
6235         }
6236       while (GET_CODE (op) == PLUS);
6237       if (n >= 4)
6238         return 0;
6239       addends[n] = op;
6240
6241       for (i = n; i >= 0; --i)
6242         {
6243           op = addends[i];
6244           switch (GET_CODE (op))
6245             {
6246             case MULT:
6247               if (index)
6248                 return 0;
6249               index = XEXP (op, 0);
6250               scale_rtx = XEXP (op, 1);
6251               break;
6252
6253             case UNSPEC:
6254               if (XINT (op, 1) == UNSPEC_TP
6255                   && TARGET_TLS_DIRECT_SEG_REFS
6256                   && seg == SEG_DEFAULT)
6257                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
6258               else
6259                 return 0;
6260               break;
6261
6262             case REG:
6263             case SUBREG:
6264               if (!base)
6265                 base = op;
6266               else if (!index)
6267                 index = op;
6268               else
6269                 return 0;
6270               break;
6271
6272             case CONST:
6273             case CONST_INT:
6274             case SYMBOL_REF:
6275             case LABEL_REF:
6276               if (disp)
6277                 return 0;
6278               disp = op;
6279               break;
6280
6281             default:
6282               return 0;
6283             }
6284         }
6285     }
6286   else if (GET_CODE (addr) == MULT)
6287     {
6288       index = XEXP (addr, 0);           /* index*scale */
6289       scale_rtx = XEXP (addr, 1);
6290     }
6291   else if (GET_CODE (addr) == ASHIFT)
6292     {
6293       rtx tmp;
6294
6295       /* We're called for lea too, which implements ashift on occasion.  */
6296       index = XEXP (addr, 0);
6297       tmp = XEXP (addr, 1);
6298       if (!CONST_INT_P (tmp))
6299         return 0;
6300       scale = INTVAL (tmp);
6301       if ((unsigned HOST_WIDE_INT) scale > 3)
6302         return 0;
6303       scale = 1 << scale;
6304       retval = -1;
6305     }
6306   else
6307     disp = addr;                        /* displacement */
6308
6309   /* Extract the integral value of scale.  */
6310   if (scale_rtx)
6311     {
6312       if (!CONST_INT_P (scale_rtx))
6313         return 0;
6314       scale = INTVAL (scale_rtx);
6315     }
6316
6317   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
6318   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
6319
6320   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
6321   if (base_reg && index_reg && scale == 1
6322       && (index_reg == arg_pointer_rtx
6323           || index_reg == frame_pointer_rtx
6324           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
6325     {
6326       rtx tmp;
6327       tmp = base, base = index, index = tmp;
6328       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
6329     }
6330
6331   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
6332   if ((base_reg == hard_frame_pointer_rtx
6333        || base_reg == frame_pointer_rtx
6334        || base_reg == arg_pointer_rtx) && !disp)
6335     disp = const0_rtx;
6336
6337   /* Special case: on K6, [%esi] makes the instruction vector decoded.
6338      Avoid this by transforming to [%esi+0].  */
6339   if (ix86_tune == PROCESSOR_K6 && !optimize_size
6340       && base_reg && !index_reg && !disp
6341       && REG_P (base_reg)
6342       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
6343     disp = const0_rtx;
6344
6345   /* Special case: encode reg+reg instead of reg*2.  */
6346   if (!base && index && scale && scale == 2)
6347     base = index, base_reg = index_reg, scale = 1;
6348
6349   /* Special case: scaling cannot be encoded without base or displacement.  */
6350   if (!base && !disp && index && scale != 1)
6351     disp = const0_rtx;
6352
6353   out->base = base;
6354   out->index = index;
6355   out->disp = disp;
6356   out->scale = scale;
6357   out->seg = seg;
6358
6359   return retval;
6360 }
6361 \f
6362 /* Return cost of the memory address x.
6363    For i386, it is better to use a complex address than let gcc copy
6364    the address into a reg and make a new pseudo.  But not if the address
6365    requires to two regs - that would mean more pseudos with longer
6366    lifetimes.  */
6367 static int
6368 ix86_address_cost (rtx x)
6369 {
6370   struct ix86_address parts;
6371   int cost = 1;
6372   int ok = ix86_decompose_address (x, &parts);
6373
6374   gcc_assert (ok);
6375
6376   if (parts.base && GET_CODE (parts.base) == SUBREG)
6377     parts.base = SUBREG_REG (parts.base);
6378   if (parts.index && GET_CODE (parts.index) == SUBREG)
6379     parts.index = SUBREG_REG (parts.index);
6380
6381   /* More complex memory references are better.  */
6382   if (parts.disp && parts.disp != const0_rtx)
6383     cost--;
6384   if (parts.seg != SEG_DEFAULT)
6385     cost--;
6386
6387   /* Attempt to minimize number of registers in the address.  */
6388   if ((parts.base
6389        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
6390       || (parts.index
6391           && (!REG_P (parts.index)
6392               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
6393     cost++;
6394
6395   if (parts.base
6396       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
6397       && parts.index
6398       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
6399       && parts.base != parts.index)
6400     cost++;
6401
6402   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
6403      since it's predecode logic can't detect the length of instructions
6404      and it degenerates to vector decoded.  Increase cost of such
6405      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
6406      to split such addresses or even refuse such addresses at all.
6407
6408      Following addressing modes are affected:
6409       [base+scale*index]
6410       [scale*index+disp]
6411       [base+index]
6412
6413      The first and last case  may be avoidable by explicitly coding the zero in
6414      memory address, but I don't have AMD-K6 machine handy to check this
6415      theory.  */
6416
6417   if (TARGET_K6
6418       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
6419           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
6420           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
6421     cost += 10;
6422
6423   return cost;
6424 }
6425 \f
6426 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6427    this is used for to form addresses to local data when -fPIC is in
6428    use.  */
6429
6430 static bool
6431 darwin_local_data_pic (rtx disp)
6432 {
6433   if (GET_CODE (disp) == MINUS)
6434     {
6435       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6436           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6437         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6438           {
6439             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6440             if (! strcmp (sym_name, "<pic base>"))
6441               return true;
6442           }
6443     }
6444
6445   return false;
6446 }
6447
6448 /* Determine if a given RTX is a valid constant.  We already know this
6449    satisfies CONSTANT_P.  */
6450
6451 bool
6452 legitimate_constant_p (rtx x)
6453 {
6454   switch (GET_CODE (x))
6455     {
6456     case CONST:
6457       x = XEXP (x, 0);
6458
6459       if (GET_CODE (x) == PLUS)
6460         {
6461           if (!CONST_INT_P (XEXP (x, 1)))
6462             return false;
6463           x = XEXP (x, 0);
6464         }
6465
6466       if (TARGET_MACHO && darwin_local_data_pic (x))
6467         return true;
6468
6469       /* Only some unspecs are valid as "constants".  */
6470       if (GET_CODE (x) == UNSPEC)
6471         switch (XINT (x, 1))
6472           {
6473           case UNSPEC_GOT:
6474           case UNSPEC_GOTOFF:
6475           case UNSPEC_PLTOFF:
6476             return TARGET_64BIT;
6477           case UNSPEC_TPOFF:
6478           case UNSPEC_NTPOFF:
6479             x = XVECEXP (x, 0, 0);
6480             return (GET_CODE (x) == SYMBOL_REF
6481                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6482           case UNSPEC_DTPOFF:
6483             x = XVECEXP (x, 0, 0);
6484             return (GET_CODE (x) == SYMBOL_REF
6485                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6486           default:
6487             return false;
6488           }
6489
6490       /* We must have drilled down to a symbol.  */
6491       if (GET_CODE (x) == LABEL_REF)
6492         return true;
6493       if (GET_CODE (x) != SYMBOL_REF)
6494         return false;
6495       /* FALLTHRU */
6496
6497     case SYMBOL_REF:
6498       /* TLS symbols are never valid.  */
6499       if (SYMBOL_REF_TLS_MODEL (x))
6500         return false;
6501
6502       /* DLLIMPORT symbols are never valid.  */
6503       if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
6504           && SYMBOL_REF_DLLIMPORT_P (x))
6505         return false;
6506       break;
6507
6508     case CONST_DOUBLE:
6509       if (GET_MODE (x) == TImode
6510           && x != CONST0_RTX (TImode)
6511           && !TARGET_64BIT)
6512         return false;
6513       break;
6514
6515     case CONST_VECTOR:
6516       if (x == CONST0_RTX (GET_MODE (x)))
6517         return true;
6518       return false;
6519
6520     default:
6521       break;
6522     }
6523
6524   /* Otherwise we handle everything else in the move patterns.  */
6525   return true;
6526 }
6527
6528 /* Determine if it's legal to put X into the constant pool.  This
6529    is not possible for the address of thread-local symbols, which
6530    is checked above.  */
6531
6532 static bool
6533 ix86_cannot_force_const_mem (rtx x)
6534 {
6535   /* We can always put integral constants and vectors in memory.  */
6536   switch (GET_CODE (x))
6537     {
6538     case CONST_INT:
6539     case CONST_DOUBLE:
6540     case CONST_VECTOR:
6541       return false;
6542
6543     default:
6544       break;
6545     }
6546   return !legitimate_constant_p (x);
6547 }
6548
6549 /* Determine if a given RTX is a valid constant address.  */
6550
6551 bool
6552 constant_address_p (rtx x)
6553 {
6554   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6555 }
6556
6557 /* Nonzero if the constant value X is a legitimate general operand
6558    when generating PIC code.  It is given that flag_pic is on and
6559    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6560
6561 bool
6562 legitimate_pic_operand_p (rtx x)
6563 {
6564   rtx inner;
6565
6566   switch (GET_CODE (x))
6567     {
6568     case CONST:
6569       inner = XEXP (x, 0);
6570       if (GET_CODE (inner) == PLUS
6571           && CONST_INT_P (XEXP (inner, 1)))
6572         inner = XEXP (inner, 0);
6573
6574       /* Only some unspecs are valid as "constants".  */
6575       if (GET_CODE (inner) == UNSPEC)
6576         switch (XINT (inner, 1))
6577           {
6578           case UNSPEC_GOT:
6579           case UNSPEC_GOTOFF:
6580           case UNSPEC_PLTOFF:
6581             return TARGET_64BIT;
6582           case UNSPEC_TPOFF:
6583             x = XVECEXP (inner, 0, 0);
6584             return (GET_CODE (x) == SYMBOL_REF
6585                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6586           default:
6587             return false;
6588           }
6589       /* FALLTHRU */
6590
6591     case SYMBOL_REF:
6592     case LABEL_REF:
6593       return legitimate_pic_address_disp_p (x);
6594
6595     default:
6596       return true;
6597     }
6598 }
6599
6600 /* Determine if a given CONST RTX is a valid memory displacement
6601    in PIC mode.  */
6602
6603 int
6604 legitimate_pic_address_disp_p (rtx disp)
6605 {
6606   bool saw_plus;
6607
6608   /* In 64bit mode we can allow direct addresses of symbols and labels
6609      when they are not dynamic symbols.  */
6610   if (TARGET_64BIT)
6611     {
6612       rtx op0 = disp, op1;
6613
6614       switch (GET_CODE (disp))
6615         {
6616         case LABEL_REF:
6617           return true;
6618
6619         case CONST:
6620           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6621             break;
6622           op0 = XEXP (XEXP (disp, 0), 0);
6623           op1 = XEXP (XEXP (disp, 0), 1);
6624           if (!CONST_INT_P (op1)
6625               || INTVAL (op1) >= 16*1024*1024
6626               || INTVAL (op1) < -16*1024*1024)
6627             break;
6628           if (GET_CODE (op0) == LABEL_REF)
6629             return true;
6630           if (GET_CODE (op0) != SYMBOL_REF)
6631             break;
6632           /* FALLTHRU */
6633
6634         case SYMBOL_REF:
6635           /* TLS references should always be enclosed in UNSPEC.  */
6636           if (SYMBOL_REF_TLS_MODEL (op0))
6637             return false;
6638           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0)
6639               && ix86_cmodel != CM_LARGE_PIC)
6640             return true;
6641           break;
6642
6643         default:
6644           break;
6645         }
6646     }
6647   if (GET_CODE (disp) != CONST)
6648     return 0;
6649   disp = XEXP (disp, 0);
6650
6651   if (TARGET_64BIT)
6652     {
6653       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6654          of GOT tables.  We should not need these anyway.  */
6655       if (GET_CODE (disp) != UNSPEC
6656           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6657               && XINT (disp, 1) != UNSPEC_GOTOFF
6658               && XINT (disp, 1) != UNSPEC_PLTOFF))
6659         return 0;
6660
6661       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6662           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6663         return 0;
6664       return 1;
6665     }
6666
6667   saw_plus = false;
6668   if (GET_CODE (disp) == PLUS)
6669     {
6670       if (!CONST_INT_P (XEXP (disp, 1)))
6671         return 0;
6672       disp = XEXP (disp, 0);
6673       saw_plus = true;
6674     }
6675
6676   if (TARGET_MACHO && darwin_local_data_pic (disp))
6677     return 1;
6678
6679   if (GET_CODE (disp) != UNSPEC)
6680     return 0;
6681
6682   switch (XINT (disp, 1))
6683     {
6684     case UNSPEC_GOT:
6685       if (saw_plus)
6686         return false;
6687       /* We need to check for both symbols and labels because VxWorks loads
6688          text labels with @GOT rather than @GOTOFF.  See gotoff_operand for
6689          details.  */
6690       return (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6691               || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF);
6692     case UNSPEC_GOTOFF:
6693       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6694          While ABI specify also 32bit relocation but we don't produce it in
6695          small PIC model at all.  */
6696       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6697            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6698           && !TARGET_64BIT)
6699         return gotoff_operand (XVECEXP (disp, 0, 0), Pmode);
6700       return false;
6701     case UNSPEC_GOTTPOFF:
6702     case UNSPEC_GOTNTPOFF:
6703     case UNSPEC_INDNTPOFF:
6704       if (saw_plus)
6705         return false;
6706       disp = XVECEXP (disp, 0, 0);
6707       return (GET_CODE (disp) == SYMBOL_REF
6708               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6709     case UNSPEC_NTPOFF:
6710       disp = XVECEXP (disp, 0, 0);
6711       return (GET_CODE (disp) == SYMBOL_REF
6712               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6713     case UNSPEC_DTPOFF:
6714       disp = XVECEXP (disp, 0, 0);
6715       return (GET_CODE (disp) == SYMBOL_REF
6716               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6717     }
6718
6719   return 0;
6720 }
6721
6722 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6723    memory address for an instruction.  The MODE argument is the machine mode
6724    for the MEM expression that wants to use this address.
6725
6726    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6727    convert common non-canonical forms to canonical form so that they will
6728    be recognized.  */
6729
6730 int
6731 legitimate_address_p (enum machine_mode mode ATTRIBUTE_UNUSED,
6732                       rtx addr, int strict)
6733 {
6734   struct ix86_address parts;
6735   rtx base, index, disp;
6736   HOST_WIDE_INT scale;
6737   const char *reason = NULL;
6738   rtx reason_rtx = NULL_RTX;
6739
6740   if (ix86_decompose_address (addr, &parts) <= 0)
6741     {
6742       reason = "decomposition failed";
6743       goto report_error;
6744     }
6745
6746   base = parts.base;
6747   index = parts.index;
6748   disp = parts.disp;
6749   scale = parts.scale;
6750
6751   /* Validate base register.
6752
6753      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6754      failures when the base is one word out of a two word structure, which is
6755      represented internally as a DImode int.  */
6756
6757   if (base)
6758     {
6759       rtx reg;
6760       reason_rtx = base;
6761
6762       if (REG_P (base))
6763         reg = base;
6764       else if (GET_CODE (base) == SUBREG
6765                && REG_P (SUBREG_REG (base))
6766                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6767                   <= UNITS_PER_WORD)
6768         reg = SUBREG_REG (base);
6769       else
6770         {
6771           reason = "base is not a register";
6772           goto report_error;
6773         }
6774
6775       if (GET_MODE (base) != Pmode)
6776         {
6777           reason = "base is not in Pmode";
6778           goto report_error;
6779         }
6780
6781       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6782           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6783         {
6784           reason = "base is not valid";
6785           goto report_error;
6786         }
6787     }
6788
6789   /* Validate index register.
6790
6791      Don't allow SUBREG's that span more than a word here -- same as above.  */
6792
6793   if (index)
6794     {
6795       rtx reg;
6796       reason_rtx = index;
6797
6798       if (REG_P (index))
6799         reg = index;
6800       else if (GET_CODE (index) == SUBREG
6801                && REG_P (SUBREG_REG (index))
6802                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6803                   <= UNITS_PER_WORD)
6804         reg = SUBREG_REG (index);
6805       else
6806         {
6807           reason = "index is not a register";
6808           goto report_error;
6809         }
6810
6811       if (GET_MODE (index) != Pmode)
6812         {
6813           reason = "index is not in Pmode";
6814           goto report_error;
6815         }
6816
6817       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6818           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6819         {
6820           reason = "index is not valid";
6821           goto report_error;
6822         }
6823     }
6824
6825   /* Validate scale factor.  */
6826   if (scale != 1)
6827     {
6828       reason_rtx = GEN_INT (scale);
6829       if (!index)
6830         {
6831           reason = "scale without index";
6832           goto report_error;
6833         }
6834
6835       if (scale != 2 && scale != 4 && scale != 8)
6836         {
6837           reason = "scale is not a valid multiplier";
6838           goto report_error;
6839         }
6840     }
6841
6842   /* Validate displacement.  */
6843   if (disp)
6844     {
6845       reason_rtx = disp;
6846
6847       if (GET_CODE (disp) == CONST
6848           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6849         switch (XINT (XEXP (disp, 0), 1))
6850           {
6851           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6852              used.  While ABI specify also 32bit relocations, we don't produce
6853              them at all and use IP relative instead.  */
6854           case UNSPEC_GOT:
6855           case UNSPEC_GOTOFF:
6856             gcc_assert (flag_pic);
6857             if (!TARGET_64BIT)
6858               goto is_legitimate_pic;
6859             reason = "64bit address unspec";
6860             goto report_error;
6861
6862           case UNSPEC_GOTPCREL:
6863             gcc_assert (flag_pic);
6864             goto is_legitimate_pic;
6865
6866           case UNSPEC_GOTTPOFF:
6867           case UNSPEC_GOTNTPOFF:
6868           case UNSPEC_INDNTPOFF:
6869           case UNSPEC_NTPOFF:
6870           case UNSPEC_DTPOFF:
6871             break;
6872
6873           default:
6874             reason = "invalid address unspec";
6875             goto report_error;
6876           }
6877
6878       else if (SYMBOLIC_CONST (disp)
6879                && (flag_pic
6880                    || (TARGET_MACHO
6881 #if TARGET_MACHO
6882                        && MACHOPIC_INDIRECT
6883                        && !machopic_operand_p (disp)
6884 #endif
6885                )))
6886         {
6887
6888         is_legitimate_pic:
6889           if (TARGET_64BIT && (index || base))
6890             {
6891               /* foo@dtpoff(%rX) is ok.  */
6892               if (GET_CODE (disp) != CONST
6893                   || GET_CODE (XEXP (disp, 0)) != PLUS
6894                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6895                   || !CONST_INT_P (XEXP (XEXP (disp, 0), 1))
6896                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6897                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6898                 {
6899                   reason = "non-constant pic memory reference";
6900                   goto report_error;
6901                 }
6902             }
6903           else if (! legitimate_pic_address_disp_p (disp))
6904             {
6905               reason = "displacement is an invalid pic construct";
6906               goto report_error;
6907             }
6908
6909           /* This code used to verify that a symbolic pic displacement
6910              includes the pic_offset_table_rtx register.
6911
6912              While this is good idea, unfortunately these constructs may
6913              be created by "adds using lea" optimization for incorrect
6914              code like:
6915
6916              int a;
6917              int foo(int i)
6918                {
6919                  return *(&a+i);
6920                }
6921
6922              This code is nonsensical, but results in addressing
6923              GOT table with pic_offset_table_rtx base.  We can't
6924              just refuse it easily, since it gets matched by
6925              "addsi3" pattern, that later gets split to lea in the
6926              case output register differs from input.  While this
6927              can be handled by separate addsi pattern for this case
6928              that never results in lea, this seems to be easier and
6929              correct fix for crash to disable this test.  */
6930         }
6931       else if (GET_CODE (disp) != LABEL_REF
6932                && !CONST_INT_P (disp)
6933                && (GET_CODE (disp) != CONST
6934                    || !legitimate_constant_p (disp))
6935                && (GET_CODE (disp) != SYMBOL_REF
6936                    || !legitimate_constant_p (disp)))
6937         {
6938           reason = "displacement is not constant";
6939           goto report_error;
6940         }
6941       else if (TARGET_64BIT
6942                && !x86_64_immediate_operand (disp, VOIDmode))
6943         {
6944           reason = "displacement is out of range";
6945           goto report_error;
6946         }
6947     }
6948
6949   /* Everything looks valid.  */
6950   return TRUE;
6951
6952  report_error:
6953   return FALSE;
6954 }
6955 \f
6956 /* Return a unique alias set for the GOT.  */
6957
6958 static HOST_WIDE_INT
6959 ix86_GOT_alias_set (void)
6960 {
6961   static HOST_WIDE_INT set = -1;
6962   if (set == -1)
6963     set = new_alias_set ();
6964   return set;
6965 }
6966
6967 /* Return a legitimate reference for ORIG (an address) using the
6968    register REG.  If REG is 0, a new pseudo is generated.
6969
6970    There are two types of references that must be handled:
6971
6972    1. Global data references must load the address from the GOT, via
6973       the PIC reg.  An insn is emitted to do this load, and the reg is
6974       returned.
6975
6976    2. Static data references, constant pool addresses, and code labels
6977       compute the address as an offset from the GOT, whose base is in
6978       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6979       differentiate them from global data objects.  The returned
6980       address is the PIC reg + an unspec constant.
6981
6982    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6983    reg also appears in the address.  */
6984
6985 static rtx
6986 legitimize_pic_address (rtx orig, rtx reg)
6987 {
6988   rtx addr = orig;
6989   rtx new = orig;
6990   rtx base;
6991
6992 #if TARGET_MACHO
6993   if (TARGET_MACHO && !TARGET_64BIT)
6994     {
6995       if (reg == 0)
6996         reg = gen_reg_rtx (Pmode);
6997       /* Use the generic Mach-O PIC machinery.  */
6998       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6999     }
7000 #endif
7001
7002   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
7003     new = addr;
7004   else if (TARGET_64BIT
7005            && ix86_cmodel != CM_SMALL_PIC
7006            && gotoff_operand (addr, Pmode))
7007     {
7008       rtx tmpreg;
7009       /* This symbol may be referenced via a displacement from the PIC
7010          base address (@GOTOFF).  */
7011
7012       if (reload_in_progress)
7013         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7014       if (GET_CODE (addr) == CONST)
7015         addr = XEXP (addr, 0);
7016       if (GET_CODE (addr) == PLUS)
7017           {
7018             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7019                                   UNSPEC_GOTOFF);
7020             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
7021           }
7022         else
7023           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7024       new = gen_rtx_CONST (Pmode, new);
7025       if (!reg)
7026         tmpreg = gen_reg_rtx (Pmode);
7027       else
7028         tmpreg = reg;
7029       emit_move_insn (tmpreg, new);
7030
7031       if (reg != 0)
7032         {
7033           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
7034                                      tmpreg, 1, OPTAB_DIRECT);
7035           new = reg;
7036         }
7037       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
7038     }
7039   else if (!TARGET_64BIT && gotoff_operand (addr, Pmode))
7040     {
7041       /* This symbol may be referenced via a displacement from the PIC
7042          base address (@GOTOFF).  */
7043
7044       if (reload_in_progress)
7045         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7046       if (GET_CODE (addr) == CONST)
7047         addr = XEXP (addr, 0);
7048       if (GET_CODE (addr) == PLUS)
7049           {
7050             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)),
7051                                   UNSPEC_GOTOFF);
7052             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
7053           }
7054         else
7055           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
7056       new = gen_rtx_CONST (Pmode, new);
7057       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7058
7059       if (reg != 0)
7060         {
7061           emit_move_insn (reg, new);
7062           new = reg;
7063         }
7064     }
7065   else if ((GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
7066            /* We can't use @GOTOFF for text labels on VxWorks;
7067               see gotoff_operand.  */
7068            || (TARGET_VXWORKS_RTP && GET_CODE (addr) == LABEL_REF))
7069     {
7070       /* Given that we've already handled dllimport variables separately
7071          in legitimize_address, and all other variables should satisfy
7072          legitimate_pic_address_disp_p, we should never arrive here.  */
7073       gcc_assert (!TARGET_64BIT_MS_ABI);
7074
7075       if (TARGET_64BIT && ix86_cmodel != CM_LARGE_PIC)
7076         {
7077           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
7078           new = gen_rtx_CONST (Pmode, new);
7079           new = gen_const_mem (Pmode, new);
7080           set_mem_alias_set (new, ix86_GOT_alias_set ());
7081
7082           if (reg == 0)
7083             reg = gen_reg_rtx (Pmode);
7084           /* Use directly gen_movsi, otherwise the address is loaded
7085              into register for CSE.  We don't want to CSE this addresses,
7086              instead we CSE addresses from the GOT table, so skip this.  */
7087           emit_insn (gen_movsi (reg, new));
7088           new = reg;
7089         }
7090       else
7091         {
7092           /* This symbol must be referenced via a load from the
7093              Global Offset Table (@GOT).  */
7094
7095           if (reload_in_progress)
7096             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7097           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
7098           new = gen_rtx_CONST (Pmode, new);
7099           if (TARGET_64BIT)
7100             new = force_reg (Pmode, new);
7101           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7102           new = gen_const_mem (Pmode, new);
7103           set_mem_alias_set (new, ix86_GOT_alias_set ());
7104
7105           if (reg == 0)
7106             reg = gen_reg_rtx (Pmode);
7107           emit_move_insn (reg, new);
7108           new = reg;
7109         }
7110     }
7111   else
7112     {
7113       if (CONST_INT_P (addr)
7114           && !x86_64_immediate_operand (addr, VOIDmode))
7115         {
7116           if (reg)
7117             {
7118               emit_move_insn (reg, addr);
7119               new = reg;
7120             }
7121           else
7122             new = force_reg (Pmode, addr);
7123         }
7124       else if (GET_CODE (addr) == CONST)
7125         {
7126           addr = XEXP (addr, 0);
7127
7128           /* We must match stuff we generate before.  Assume the only
7129              unspecs that can get here are ours.  Not that we could do
7130              anything with them anyway....  */
7131           if (GET_CODE (addr) == UNSPEC
7132               || (GET_CODE (addr) == PLUS
7133                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
7134             return orig;
7135           gcc_assert (GET_CODE (addr) == PLUS);
7136         }
7137       if (GET_CODE (addr) == PLUS)
7138         {
7139           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
7140
7141           /* Check first to see if this is a constant offset from a @GOTOFF
7142              symbol reference.  */
7143           if (gotoff_operand (op0, Pmode)
7144               && CONST_INT_P (op1))
7145             {
7146               if (!TARGET_64BIT)
7147                 {
7148                   if (reload_in_progress)
7149                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7150                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
7151                                         UNSPEC_GOTOFF);
7152                   new = gen_rtx_PLUS (Pmode, new, op1);
7153                   new = gen_rtx_CONST (Pmode, new);
7154                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
7155
7156                   if (reg != 0)
7157                     {
7158                       emit_move_insn (reg, new);
7159                       new = reg;
7160                     }
7161                 }
7162               else
7163                 {
7164                   if (INTVAL (op1) < -16*1024*1024
7165                       || INTVAL (op1) >= 16*1024*1024)
7166                     {
7167                       if (!x86_64_immediate_operand (op1, Pmode))
7168                         op1 = force_reg (Pmode, op1);
7169                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
7170                     }
7171                 }
7172             }
7173           else
7174             {
7175               base = legitimize_pic_address (XEXP (addr, 0), reg);
7176               new  = legitimize_pic_address (XEXP (addr, 1),
7177                                              base == reg ? NULL_RTX : reg);
7178
7179               if (CONST_INT_P (new))
7180                 new = plus_constant (base, INTVAL (new));
7181               else
7182                 {
7183                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
7184                     {
7185                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
7186                       new = XEXP (new, 1);
7187                     }
7188                   new = gen_rtx_PLUS (Pmode, base, new);
7189                 }
7190             }
7191         }
7192     }
7193   return new;
7194 }
7195 \f
7196 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
7197
7198 static rtx
7199 get_thread_pointer (int to_reg)
7200 {
7201   rtx tp, reg, insn;
7202
7203   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
7204   if (!to_reg)
7205     return tp;
7206
7207   reg = gen_reg_rtx (Pmode);
7208   insn = gen_rtx_SET (VOIDmode, reg, tp);
7209   insn = emit_insn (insn);
7210
7211   return reg;
7212 }
7213
7214 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
7215    false if we expect this to be used for a memory address and true if
7216    we expect to load the address into a register.  */
7217
7218 static rtx
7219 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
7220 {
7221   rtx dest, base, off, pic, tp;
7222   int type;
7223
7224   switch (model)
7225     {
7226     case TLS_MODEL_GLOBAL_DYNAMIC:
7227       dest = gen_reg_rtx (Pmode);
7228       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7229
7230       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7231         {
7232           rtx rax = gen_rtx_REG (Pmode, 0), insns;
7233
7234           start_sequence ();
7235           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
7236           insns = get_insns ();
7237           end_sequence ();
7238
7239           emit_libcall_block (insns, dest, rax, x);
7240         }
7241       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7242         emit_insn (gen_tls_global_dynamic_64 (dest, x));
7243       else
7244         emit_insn (gen_tls_global_dynamic_32 (dest, x));
7245
7246       if (TARGET_GNU2_TLS)
7247         {
7248           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
7249
7250           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7251         }
7252       break;
7253
7254     case TLS_MODEL_LOCAL_DYNAMIC:
7255       base = gen_reg_rtx (Pmode);
7256       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
7257
7258       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
7259         {
7260           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
7261
7262           start_sequence ();
7263           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
7264           insns = get_insns ();
7265           end_sequence ();
7266
7267           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
7268           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
7269           emit_libcall_block (insns, base, rax, note);
7270         }
7271       else if (TARGET_64BIT && TARGET_GNU2_TLS)
7272         emit_insn (gen_tls_local_dynamic_base_64 (base));
7273       else
7274         emit_insn (gen_tls_local_dynamic_base_32 (base));
7275
7276       if (TARGET_GNU2_TLS)
7277         {
7278           rtx x = ix86_tls_module_base ();
7279
7280           set_unique_reg_note (get_last_insn (), REG_EQUIV,
7281                                gen_rtx_MINUS (Pmode, x, tp));
7282         }
7283
7284       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
7285       off = gen_rtx_CONST (Pmode, off);
7286
7287       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
7288
7289       if (TARGET_GNU2_TLS)
7290         {
7291           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
7292
7293           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
7294         }
7295
7296       break;
7297
7298     case TLS_MODEL_INITIAL_EXEC:
7299       if (TARGET_64BIT)
7300         {
7301           pic = NULL;
7302           type = UNSPEC_GOTNTPOFF;
7303         }
7304       else if (flag_pic)
7305         {
7306           if (reload_in_progress)
7307             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
7308           pic = pic_offset_table_rtx;
7309           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
7310         }
7311       else if (!TARGET_ANY_GNU_TLS)
7312         {
7313           pic = gen_reg_rtx (Pmode);
7314           emit_insn (gen_set_got (pic));
7315           type = UNSPEC_GOTTPOFF;
7316         }
7317       else
7318         {
7319           pic = NULL;
7320           type = UNSPEC_INDNTPOFF;
7321         }
7322
7323       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
7324       off = gen_rtx_CONST (Pmode, off);
7325       if (pic)
7326         off = gen_rtx_PLUS (Pmode, pic, off);
7327       off = gen_const_mem (Pmode, off);
7328       set_mem_alias_set (off, ix86_GOT_alias_set ());
7329
7330       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7331         {
7332           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7333           off = force_reg (Pmode, off);
7334           return gen_rtx_PLUS (Pmode, base, off);
7335         }
7336       else
7337         {
7338           base = get_thread_pointer (true);
7339           dest = gen_reg_rtx (Pmode);
7340           emit_insn (gen_subsi3 (dest, base, off));
7341         }
7342       break;
7343
7344     case TLS_MODEL_LOCAL_EXEC:
7345       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
7346                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7347                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
7348       off = gen_rtx_CONST (Pmode, off);
7349
7350       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
7351         {
7352           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
7353           return gen_rtx_PLUS (Pmode, base, off);
7354         }
7355       else
7356         {
7357           base = get_thread_pointer (true);
7358           dest = gen_reg_rtx (Pmode);
7359           emit_insn (gen_subsi3 (dest, base, off));
7360         }
7361       break;
7362
7363     default:
7364       gcc_unreachable ();
7365     }
7366
7367   return dest;
7368 }
7369
7370 /* Create or return the unique __imp_DECL dllimport symbol corresponding
7371    to symbol DECL.  */
7372
7373 static GTY((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
7374   htab_t dllimport_map;
7375
7376 static tree
7377 get_dllimport_decl (tree decl)
7378 {
7379   struct tree_map *h, in;
7380   void **loc;
7381   const char *name;
7382   const char *prefix;
7383   size_t namelen, prefixlen;
7384   char *imp_name;
7385   tree to;
7386   rtx rtl;
7387
7388   if (!dllimport_map)
7389     dllimport_map = htab_create_ggc (512, tree_map_hash, tree_map_eq, 0);
7390
7391   in.hash = htab_hash_pointer (decl);
7392   in.base.from = decl;
7393   loc = htab_find_slot_with_hash (dllimport_map, &in, in.hash, INSERT);
7394   h = *loc;
7395   if (h)
7396     return h->to;
7397
7398   *loc = h = ggc_alloc (sizeof (struct tree_map));
7399   h->hash = in.hash;
7400   h->base.from = decl;
7401   h->to = to = build_decl (VAR_DECL, NULL, ptr_type_node);
7402   DECL_ARTIFICIAL (to) = 1;
7403   DECL_IGNORED_P (to) = 1;
7404   DECL_EXTERNAL (to) = 1;
7405   TREE_READONLY (to) = 1;
7406
7407   name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
7408   name = targetm.strip_name_encoding (name);
7409   if (name[0] == FASTCALL_PREFIX)
7410     {
7411       name++;
7412       prefix = "*__imp_";
7413     }
7414   else
7415     prefix = "*__imp__";
7416
7417   namelen = strlen (name);
7418   prefixlen = strlen (prefix);
7419   imp_name = alloca (namelen + prefixlen + 1);
7420   memcpy (imp_name, prefix, prefixlen);
7421   memcpy (imp_name + prefixlen, name, namelen + 1);
7422
7423   name = ggc_alloc_string (imp_name, namelen + prefixlen);
7424   rtl = gen_rtx_SYMBOL_REF (Pmode, name);
7425   SET_SYMBOL_REF_DECL (rtl, to);
7426   SYMBOL_REF_FLAGS (rtl) = SYMBOL_FLAG_LOCAL;
7427
7428   rtl = gen_const_mem (Pmode, rtl);
7429   set_mem_alias_set (rtl, ix86_GOT_alias_set ());
7430
7431   SET_DECL_RTL (to, rtl);
7432
7433   return to;
7434 }
7435
7436 /* Expand SYMBOL into its corresponding dllimport symbol.  WANT_REG is
7437    true if we require the result be a register.  */
7438
7439 static rtx
7440 legitimize_dllimport_symbol (rtx symbol, bool want_reg)
7441 {
7442   tree imp_decl;
7443   rtx x;
7444
7445   gcc_assert (SYMBOL_REF_DECL (symbol));
7446   imp_decl = get_dllimport_decl (SYMBOL_REF_DECL (symbol));
7447
7448   x = DECL_RTL (imp_decl);
7449   if (want_reg)
7450     x = force_reg (Pmode, x);
7451   return x;
7452 }
7453
7454 /* Try machine-dependent ways of modifying an illegitimate address
7455    to be legitimate.  If we find one, return the new, valid address.
7456    This macro is used in only one place: `memory_address' in explow.c.
7457
7458    OLDX is the address as it was before break_out_memory_refs was called.
7459    In some cases it is useful to look at this to decide what needs to be done.
7460
7461    MODE and WIN are passed so that this macro can use
7462    GO_IF_LEGITIMATE_ADDRESS.
7463
7464    It is always safe for this macro to do nothing.  It exists to recognize
7465    opportunities to optimize the output.
7466
7467    For the 80386, we handle X+REG by loading X into a register R and
7468    using R+REG.  R will go in a general reg and indexing will be used.
7469    However, if REG is a broken-out memory address or multiplication,
7470    nothing needs to be done because REG can certainly go in a general reg.
7471
7472    When -fpic is used, special handling is needed for symbolic references.
7473    See comments by legitimize_pic_address in i386.c for details.  */
7474
7475 rtx
7476 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
7477 {
7478   int changed = 0;
7479   unsigned log;
7480
7481   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
7482   if (log)
7483     return legitimize_tls_address (x, log, false);
7484   if (GET_CODE (x) == CONST
7485       && GET_CODE (XEXP (x, 0)) == PLUS
7486       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7487       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
7488     {
7489       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
7490       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7491     }
7492
7493   if (flag_pic && SYMBOLIC_CONST (x))
7494     return legitimize_pic_address (x, 0);
7495
7496   if (TARGET_DLLIMPORT_DECL_ATTRIBUTES)
7497     {
7498       if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_DLLIMPORT_P (x))
7499         return legitimize_dllimport_symbol (x, true);
7500       if (GET_CODE (x) == CONST
7501           && GET_CODE (XEXP (x, 0)) == PLUS
7502           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
7503           && SYMBOL_REF_DLLIMPORT_P (XEXP (XEXP (x, 0), 0)))
7504         {
7505           rtx t = legitimize_dllimport_symbol (XEXP (XEXP (x, 0), 0), true);
7506           return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
7507         }
7508     }
7509
7510   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
7511   if (GET_CODE (x) == ASHIFT
7512       && CONST_INT_P (XEXP (x, 1))
7513       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
7514     {
7515       changed = 1;
7516       log = INTVAL (XEXP (x, 1));
7517       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
7518                         GEN_INT (1 << log));
7519     }
7520
7521   if (GET_CODE (x) == PLUS)
7522     {
7523       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
7524
7525       if (GET_CODE (XEXP (x, 0)) == ASHIFT
7526           && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7527           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
7528         {
7529           changed = 1;
7530           log = INTVAL (XEXP (XEXP (x, 0), 1));
7531           XEXP (x, 0) = gen_rtx_MULT (Pmode,
7532                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
7533                                       GEN_INT (1 << log));
7534         }
7535
7536       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7537           && CONST_INT_P (XEXP (XEXP (x, 1), 1))
7538           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7539         {
7540           changed = 1;
7541           log = INTVAL (XEXP (XEXP (x, 1), 1));
7542           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7543                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7544                                       GEN_INT (1 << log));
7545         }
7546
7547       /* Put multiply first if it isn't already.  */
7548       if (GET_CODE (XEXP (x, 1)) == MULT)
7549         {
7550           rtx tmp = XEXP (x, 0);
7551           XEXP (x, 0) = XEXP (x, 1);
7552           XEXP (x, 1) = tmp;
7553           changed = 1;
7554         }
7555
7556       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7557          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7558          created by virtual register instantiation, register elimination, and
7559          similar optimizations.  */
7560       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7561         {
7562           changed = 1;
7563           x = gen_rtx_PLUS (Pmode,
7564                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7565                                           XEXP (XEXP (x, 1), 0)),
7566                             XEXP (XEXP (x, 1), 1));
7567         }
7568
7569       /* Canonicalize
7570          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7571          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7572       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7573                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7574                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7575                && CONSTANT_P (XEXP (x, 1)))
7576         {
7577           rtx constant;
7578           rtx other = NULL_RTX;
7579
7580           if (CONST_INT_P (XEXP (x, 1)))
7581             {
7582               constant = XEXP (x, 1);
7583               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7584             }
7585           else if (CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 1), 1)))
7586             {
7587               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7588               other = XEXP (x, 1);
7589             }
7590           else
7591             constant = 0;
7592
7593           if (constant)
7594             {
7595               changed = 1;
7596               x = gen_rtx_PLUS (Pmode,
7597                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7598                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7599                                 plus_constant (other, INTVAL (constant)));
7600             }
7601         }
7602
7603       if (changed && legitimate_address_p (mode, x, FALSE))
7604         return x;
7605
7606       if (GET_CODE (XEXP (x, 0)) == MULT)
7607         {
7608           changed = 1;
7609           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7610         }
7611
7612       if (GET_CODE (XEXP (x, 1)) == MULT)
7613         {
7614           changed = 1;
7615           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7616         }
7617
7618       if (changed
7619           && REG_P (XEXP (x, 1))
7620           && REG_P (XEXP (x, 0)))
7621         return x;
7622
7623       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7624         {
7625           changed = 1;
7626           x = legitimize_pic_address (x, 0);
7627         }
7628
7629       if (changed && legitimate_address_p (mode, x, FALSE))
7630         return x;
7631
7632       if (REG_P (XEXP (x, 0)))
7633         {
7634           rtx temp = gen_reg_rtx (Pmode);
7635           rtx val  = force_operand (XEXP (x, 1), temp);
7636           if (val != temp)
7637             emit_move_insn (temp, val);
7638
7639           XEXP (x, 1) = temp;
7640           return x;
7641         }
7642
7643       else if (REG_P (XEXP (x, 1)))
7644         {
7645           rtx temp = gen_reg_rtx (Pmode);
7646           rtx val  = force_operand (XEXP (x, 0), temp);
7647           if (val != temp)
7648             emit_move_insn (temp, val);
7649
7650           XEXP (x, 0) = temp;
7651           return x;
7652         }
7653     }
7654
7655   return x;
7656 }
7657 \f
7658 /* Print an integer constant expression in assembler syntax.  Addition
7659    and subtraction are the only arithmetic that may appear in these
7660    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7661    CODE is the operand print code from the output string.  */
7662
7663 static void
7664 output_pic_addr_const (FILE *file, rtx x, int code)
7665 {
7666   char buf[256];
7667
7668   switch (GET_CODE (x))
7669     {
7670     case PC:
7671       gcc_assert (flag_pic);
7672       putc ('.', file);
7673       break;
7674
7675     case SYMBOL_REF:
7676       if (! TARGET_MACHO || TARGET_64BIT)
7677         output_addr_const (file, x);
7678       else
7679         {
7680           const char *name = XSTR (x, 0);
7681
7682           /* Mark the decl as referenced so that cgraph will
7683              output the function.  */
7684           if (SYMBOL_REF_DECL (x))
7685             mark_decl_referenced (SYMBOL_REF_DECL (x));
7686
7687 #if TARGET_MACHO
7688           if (MACHOPIC_INDIRECT
7689               && machopic_classify_symbol (x) == MACHOPIC_UNDEFINED_FUNCTION)
7690             name = machopic_indirection_name (x, /*stub_p=*/true);
7691 #endif
7692           assemble_name (file, name);
7693         }
7694       if (!TARGET_MACHO && !TARGET_64BIT_MS_ABI
7695           && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7696         fputs ("@PLT", file);
7697       break;
7698
7699     case LABEL_REF:
7700       x = XEXP (x, 0);
7701       /* FALLTHRU */
7702     case CODE_LABEL:
7703       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7704       assemble_name (asm_out_file, buf);
7705       break;
7706
7707     case CONST_INT:
7708       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7709       break;
7710
7711     case CONST:
7712       /* This used to output parentheses around the expression,
7713          but that does not work on the 386 (either ATT or BSD assembler).  */
7714       output_pic_addr_const (file, XEXP (x, 0), code);
7715       break;
7716
7717     case CONST_DOUBLE:
7718       if (GET_MODE (x) == VOIDmode)
7719         {
7720           /* We can use %d if the number is <32 bits and positive.  */
7721           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7722             fprintf (file, "0x%lx%08lx",
7723                      (unsigned long) CONST_DOUBLE_HIGH (x),
7724                      (unsigned long) CONST_DOUBLE_LOW (x));
7725           else
7726             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7727         }
7728       else
7729         /* We can't handle floating point constants;
7730            PRINT_OPERAND must handle them.  */
7731         output_operand_lossage ("floating constant misused");
7732       break;
7733
7734     case PLUS:
7735       /* Some assemblers need integer constants to appear first.  */
7736       if (CONST_INT_P (XEXP (x, 0)))
7737         {
7738           output_pic_addr_const (file, XEXP (x, 0), code);
7739           putc ('+', file);
7740           output_pic_addr_const (file, XEXP (x, 1), code);
7741         }
7742       else
7743         {
7744           gcc_assert (CONST_INT_P (XEXP (x, 1)));
7745           output_pic_addr_const (file, XEXP (x, 1), code);
7746           putc ('+', file);
7747           output_pic_addr_const (file, XEXP (x, 0), code);
7748         }
7749       break;
7750
7751     case MINUS:
7752       if (!TARGET_MACHO)
7753         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7754       output_pic_addr_const (file, XEXP (x, 0), code);
7755       putc ('-', file);
7756       output_pic_addr_const (file, XEXP (x, 1), code);
7757       if (!TARGET_MACHO)
7758         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7759       break;
7760
7761      case UNSPEC:
7762        gcc_assert (XVECLEN (x, 0) == 1);
7763        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7764        switch (XINT (x, 1))
7765         {
7766         case UNSPEC_GOT:
7767           fputs ("@GOT", file);
7768           break;
7769         case UNSPEC_GOTOFF:
7770           fputs ("@GOTOFF", file);
7771           break;
7772         case UNSPEC_PLTOFF:
7773           fputs ("@PLTOFF", file);
7774           break;
7775         case UNSPEC_GOTPCREL:
7776           fputs ("@GOTPCREL(%rip)", file);
7777           break;
7778         case UNSPEC_GOTTPOFF:
7779           /* FIXME: This might be @TPOFF in Sun ld too.  */
7780           fputs ("@GOTTPOFF", file);
7781           break;
7782         case UNSPEC_TPOFF:
7783           fputs ("@TPOFF", file);
7784           break;
7785         case UNSPEC_NTPOFF:
7786           if (TARGET_64BIT)
7787             fputs ("@TPOFF", file);
7788           else
7789             fputs ("@NTPOFF", file);
7790           break;
7791         case UNSPEC_DTPOFF:
7792           fputs ("@DTPOFF", file);
7793           break;
7794         case UNSPEC_GOTNTPOFF:
7795           if (TARGET_64BIT)
7796             fputs ("@GOTTPOFF(%rip)", file);
7797           else
7798             fputs ("@GOTNTPOFF", file);
7799           break;
7800         case UNSPEC_INDNTPOFF:
7801           fputs ("@INDNTPOFF", file);
7802           break;
7803         default:
7804           output_operand_lossage ("invalid UNSPEC as operand");
7805           break;
7806         }
7807        break;
7808
7809     default:
7810       output_operand_lossage ("invalid expression as operand");
7811     }
7812 }
7813
7814 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7815    We need to emit DTP-relative relocations.  */
7816
7817 static void ATTRIBUTE_UNUSED
7818 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7819 {
7820   fputs (ASM_LONG, file);
7821   output_addr_const (file, x);
7822   fputs ("@DTPOFF", file);
7823   switch (size)
7824     {
7825     case 4:
7826       break;
7827     case 8:
7828       fputs (", 0", file);
7829       break;
7830     default:
7831       gcc_unreachable ();
7832    }
7833 }
7834
7835 /* In the name of slightly smaller debug output, and to cater to
7836    general assembler lossage, recognize PIC+GOTOFF and turn it back
7837    into a direct symbol reference.
7838
7839    On Darwin, this is necessary to avoid a crash, because Darwin
7840    has a different PIC label for each routine but the DWARF debugging
7841    information is not associated with any particular routine, so it's
7842    necessary to remove references to the PIC label from RTL stored by
7843    the DWARF output code.  */
7844
7845 static rtx
7846 ix86_delegitimize_address (rtx orig_x)
7847 {
7848   rtx x = orig_x;
7849   /* reg_addend is NULL or a multiple of some register.  */
7850   rtx reg_addend = NULL_RTX;
7851   /* const_addend is NULL or a const_int.  */
7852   rtx const_addend = NULL_RTX;
7853   /* This is the result, or NULL.  */
7854   rtx result = NULL_RTX;
7855
7856   if (MEM_P (x))
7857     x = XEXP (x, 0);
7858
7859   if (TARGET_64BIT)
7860     {
7861       if (GET_CODE (x) != CONST
7862           || GET_CODE (XEXP (x, 0)) != UNSPEC
7863           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7864           || !MEM_P (orig_x))
7865         return orig_x;
7866       return XVECEXP (XEXP (x, 0), 0, 0);
7867     }
7868
7869   if (GET_CODE (x) != PLUS
7870       || GET_CODE (XEXP (x, 1)) != CONST)
7871     return orig_x;
7872
7873   if (REG_P (XEXP (x, 0))
7874       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7875     /* %ebx + GOT/GOTOFF */
7876     ;
7877   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7878     {
7879       /* %ebx + %reg * scale + GOT/GOTOFF */
7880       reg_addend = XEXP (x, 0);
7881       if (REG_P (XEXP (reg_addend, 0))
7882           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7883         reg_addend = XEXP (reg_addend, 1);
7884       else if (REG_P (XEXP (reg_addend, 1))
7885                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7886         reg_addend = XEXP (reg_addend, 0);
7887       else
7888         return orig_x;
7889       if (!REG_P (reg_addend)
7890           && GET_CODE (reg_addend) != MULT
7891           && GET_CODE (reg_addend) != ASHIFT)
7892         return orig_x;
7893     }
7894   else
7895     return orig_x;
7896
7897   x = XEXP (XEXP (x, 1), 0);
7898   if (GET_CODE (x) == PLUS
7899       && CONST_INT_P (XEXP (x, 1)))
7900     {
7901       const_addend = XEXP (x, 1);
7902       x = XEXP (x, 0);
7903     }
7904
7905   if (GET_CODE (x) == UNSPEC
7906       && ((XINT (x, 1) == UNSPEC_GOT && MEM_P (orig_x))
7907           || (XINT (x, 1) == UNSPEC_GOTOFF && !MEM_P (orig_x))))
7908     result = XVECEXP (x, 0, 0);
7909
7910   if (TARGET_MACHO && darwin_local_data_pic (x)
7911       && !MEM_P (orig_x))
7912     result = XEXP (x, 0);
7913
7914   if (! result)
7915     return orig_x;
7916
7917   if (const_addend)
7918     result = gen_rtx_PLUS (Pmode, result, const_addend);
7919   if (reg_addend)
7920     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7921   return result;
7922 }
7923
7924 /* If X is a machine specific address (i.e. a symbol or label being
7925    referenced as a displacement from the GOT implemented using an
7926    UNSPEC), then return the base term.  Otherwise return X.  */
7927
7928 rtx
7929 ix86_find_base_term (rtx x)
7930 {
7931   rtx term;
7932
7933   if (TARGET_64BIT)
7934     {
7935       if (GET_CODE (x) != CONST)
7936         return x;
7937       term = XEXP (x, 0);
7938       if (GET_CODE (term) == PLUS
7939           && (CONST_INT_P (XEXP (term, 1))
7940               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
7941         term = XEXP (term, 0);
7942       if (GET_CODE (term) != UNSPEC
7943           || XINT (term, 1) != UNSPEC_GOTPCREL)
7944         return x;
7945
7946       term = XVECEXP (term, 0, 0);
7947
7948       if (GET_CODE (term) != SYMBOL_REF
7949           && GET_CODE (term) != LABEL_REF)
7950         return x;
7951
7952       return term;
7953     }
7954
7955   term = ix86_delegitimize_address (x);
7956
7957   if (GET_CODE (term) != SYMBOL_REF
7958       && GET_CODE (term) != LABEL_REF)
7959     return x;
7960
7961   return term;
7962 }
7963 \f
7964 static void
7965 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7966                     int fp, FILE *file)
7967 {
7968   const char *suffix;
7969
7970   if (mode == CCFPmode || mode == CCFPUmode)
7971     {
7972       enum rtx_code second_code, bypass_code;
7973       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7974       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7975       code = ix86_fp_compare_code_to_integer (code);
7976       mode = CCmode;
7977     }
7978   if (reverse)
7979     code = reverse_condition (code);
7980
7981   switch (code)
7982     {
7983     case EQ:
7984       suffix = "e";
7985       break;
7986     case NE:
7987       suffix = "ne";
7988       break;
7989     case GT:
7990       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7991       suffix = "g";
7992       break;
7993     case GTU:
7994       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7995          Those same assemblers have the same but opposite lossage on cmov.  */
7996       gcc_assert (mode == CCmode);
7997       suffix = fp ? "nbe" : "a";
7998       break;
7999     case LT:
8000       switch (mode)
8001         {
8002         case CCNOmode:
8003         case CCGOCmode:
8004           suffix = "s";
8005           break;
8006
8007         case CCmode:
8008         case CCGCmode:
8009           suffix = "l";
8010           break;
8011
8012         default:
8013           gcc_unreachable ();
8014         }
8015       break;
8016     case LTU:
8017       gcc_assert (mode == CCmode);
8018       suffix = "b";
8019       break;
8020     case GE:
8021       switch (mode)
8022         {
8023         case CCNOmode:
8024         case CCGOCmode:
8025           suffix = "ns";
8026           break;
8027
8028         case CCmode:
8029         case CCGCmode:
8030           suffix = "ge";
8031           break;
8032
8033         default:
8034           gcc_unreachable ();
8035         }
8036       break;
8037     case GEU:
8038       /* ??? As above.  */
8039       gcc_assert (mode == CCmode);
8040       suffix = fp ? "nb" : "ae";
8041       break;
8042     case LE:
8043       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
8044       suffix = "le";
8045       break;
8046     case LEU:
8047       gcc_assert (mode == CCmode);
8048       suffix = "be";
8049       break;
8050     case UNORDERED:
8051       suffix = fp ? "u" : "p";
8052       break;
8053     case ORDERED:
8054       suffix = fp ? "nu" : "np";
8055       break;
8056     default:
8057       gcc_unreachable ();
8058     }
8059   fputs (suffix, file);
8060 }
8061
8062 /* Print the name of register X to FILE based on its machine mode and number.
8063    If CODE is 'w', pretend the mode is HImode.
8064    If CODE is 'b', pretend the mode is QImode.
8065    If CODE is 'k', pretend the mode is SImode.
8066    If CODE is 'q', pretend the mode is DImode.
8067    If CODE is 'h', pretend the reg is the 'high' byte register.
8068    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
8069
8070 void
8071 print_reg (rtx x, int code, FILE *file)
8072 {
8073   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
8074               && REGNO (x) != FRAME_POINTER_REGNUM
8075               && REGNO (x) != FLAGS_REG
8076               && REGNO (x) != FPSR_REG
8077               && REGNO (x) != FPCR_REG);
8078
8079   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
8080     putc ('%', file);
8081
8082   if (code == 'w' || MMX_REG_P (x))
8083     code = 2;
8084   else if (code == 'b')
8085     code = 1;
8086   else if (code == 'k')
8087     code = 4;
8088   else if (code == 'q')
8089     code = 8;
8090   else if (code == 'y')
8091     code = 3;
8092   else if (code == 'h')
8093     code = 0;
8094   else
8095     code = GET_MODE_SIZE (GET_MODE (x));
8096
8097   /* Irritatingly, AMD extended registers use different naming convention
8098      from the normal registers.  */
8099   if (REX_INT_REG_P (x))
8100     {
8101       gcc_assert (TARGET_64BIT);
8102       switch (code)
8103         {
8104           case 0:
8105             error ("extended registers have no high halves");
8106             break;
8107           case 1:
8108             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
8109             break;
8110           case 2:
8111             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
8112             break;
8113           case 4:
8114             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
8115             break;
8116           case 8:
8117             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
8118             break;
8119           default:
8120             error ("unsupported operand size for extended register");
8121             break;
8122         }
8123       return;
8124     }
8125   switch (code)
8126     {
8127     case 3:
8128       if (STACK_TOP_P (x))
8129         {
8130           fputs ("st(0)", file);
8131           break;
8132         }
8133       /* FALLTHRU */
8134     case 8:
8135     case 4:
8136     case 12:
8137       if (! ANY_FP_REG_P (x))
8138         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
8139       /* FALLTHRU */
8140     case 16:
8141     case 2:
8142     normal:
8143       fputs (hi_reg_name[REGNO (x)], file);
8144       break;
8145     case 1:
8146       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
8147         goto normal;
8148       fputs (qi_reg_name[REGNO (x)], file);
8149       break;
8150     case 0:
8151       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
8152         goto normal;
8153       fputs (qi_high_reg_name[REGNO (x)], file);
8154       break;
8155     default:
8156       gcc_unreachable ();
8157     }
8158 }
8159
8160 /* Locate some local-dynamic symbol still in use by this function
8161    so that we can print its name in some tls_local_dynamic_base
8162    pattern.  */
8163
8164 static int
8165 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
8166 {
8167   rtx x = *px;
8168
8169   if (GET_CODE (x) == SYMBOL_REF
8170       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
8171     {
8172       cfun->machine->some_ld_name = XSTR (x, 0);
8173       return 1;
8174     }
8175
8176   return 0;
8177 }
8178
8179 static const char *
8180 get_some_local_dynamic_name (void)
8181 {
8182   rtx insn;
8183
8184   if (cfun->machine->some_ld_name)
8185     return cfun->machine->some_ld_name;
8186
8187   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
8188     if (INSN_P (insn)
8189         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
8190       return cfun->machine->some_ld_name;
8191
8192   gcc_unreachable ();
8193 }
8194
8195 /* Meaning of CODE:
8196    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
8197    C -- print opcode suffix for set/cmov insn.
8198    c -- like C, but print reversed condition
8199    F,f -- likewise, but for floating-point.
8200    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
8201         otherwise nothing
8202    R -- print the prefix for register names.
8203    z -- print the opcode suffix for the size of the current operand.
8204    * -- print a star (in certain assembler syntax)
8205    A -- print an absolute memory reference.
8206    w -- print the operand as if it's a "word" (HImode) even if it isn't.
8207    s -- print a shift double count, followed by the assemblers argument
8208         delimiter.
8209    b -- print the QImode name of the register for the indicated operand.
8210         %b0 would print %al if operands[0] is reg 0.
8211    w --  likewise, print the HImode name of the register.
8212    k --  likewise, print the SImode name of the register.
8213    q --  likewise, print the DImode name of the register.
8214    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
8215    y -- print "st(0)" instead of "st" as a register.
8216    D -- print condition for SSE cmp instruction.
8217    P -- if PIC, print an @PLT suffix.
8218    X -- don't print any sort of PIC '@' suffix for a symbol.
8219    & -- print some in-use local-dynamic symbol name.
8220    H -- print a memory address offset by 8; used for sse high-parts
8221  */
8222
8223 void
8224 print_operand (FILE *file, rtx x, int code)
8225 {
8226   if (code)
8227     {
8228       switch (code)
8229         {
8230         case '*':
8231           if (ASSEMBLER_DIALECT == ASM_ATT)
8232             putc ('*', file);
8233           return;
8234
8235         case '&':
8236           assemble_name (file, get_some_local_dynamic_name ());
8237           return;
8238
8239         case 'A':
8240           switch (ASSEMBLER_DIALECT)
8241             {
8242             case ASM_ATT:
8243               putc ('*', file);
8244               break;
8245
8246             case ASM_INTEL:
8247               /* Intel syntax. For absolute addresses, registers should not
8248                  be surrounded by braces.  */
8249               if (!REG_P (x))
8250                 {
8251                   putc ('[', file);
8252                   PRINT_OPERAND (file, x, 0);
8253                   putc (']', file);
8254                   return;
8255                 }
8256               break;
8257
8258             default:
8259               gcc_unreachable ();
8260             }
8261
8262           PRINT_OPERAND (file, x, 0);
8263           return;
8264
8265
8266         case 'L':
8267           if (ASSEMBLER_DIALECT == ASM_ATT)
8268             putc ('l', file);
8269           return;
8270
8271         case 'W':
8272           if (ASSEMBLER_DIALECT == ASM_ATT)
8273             putc ('w', file);
8274           return;
8275
8276         case 'B':
8277           if (ASSEMBLER_DIALECT == ASM_ATT)
8278             putc ('b', file);
8279           return;
8280
8281         case 'Q':
8282           if (ASSEMBLER_DIALECT == ASM_ATT)
8283             putc ('l', file);
8284           return;
8285
8286         case 'S':
8287           if (ASSEMBLER_DIALECT == ASM_ATT)
8288             putc ('s', file);
8289           return;
8290
8291         case 'T':
8292           if (ASSEMBLER_DIALECT == ASM_ATT)
8293             putc ('t', file);
8294           return;
8295
8296         case 'z':
8297           /* 387 opcodes don't get size suffixes if the operands are
8298              registers.  */
8299           if (STACK_REG_P (x))
8300             return;
8301
8302           /* Likewise if using Intel opcodes.  */
8303           if (ASSEMBLER_DIALECT == ASM_INTEL)
8304             return;
8305
8306           /* This is the size of op from size of operand.  */
8307           switch (GET_MODE_SIZE (GET_MODE (x)))
8308             {
8309             case 1:
8310               putc ('b', file);
8311               return;
8312
8313             case 2:
8314 #ifdef HAVE_GAS_FILDS_FISTS
8315               putc ('s', file);
8316 #endif
8317               return;
8318
8319             case 4:
8320               if (GET_MODE (x) == SFmode)
8321                 {
8322                   putc ('s', file);
8323                   return;
8324                 }
8325               else
8326                 putc ('l', file);
8327               return;
8328
8329             case 12:
8330             case 16:
8331               putc ('t', file);
8332               return;
8333
8334             case 8:
8335               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
8336                 {
8337 #ifdef GAS_MNEMONICS
8338                   putc ('q', file);
8339 #else
8340                   putc ('l', file);
8341                   putc ('l', file);
8342 #endif
8343                 }
8344               else
8345                 putc ('l', file);
8346               return;
8347
8348             default:
8349               gcc_unreachable ();
8350             }
8351
8352         case 'b':
8353         case 'w':
8354         case 'k':
8355         case 'q':
8356         case 'h':
8357         case 'y':
8358         case 'X':
8359         case 'P':
8360           break;
8361
8362         case 's':
8363           if (CONST_INT_P (x) || ! SHIFT_DOUBLE_OMITS_COUNT)
8364             {
8365               PRINT_OPERAND (file, x, 0);
8366               putc (',', file);
8367             }
8368           return;
8369
8370         case 'D':
8371           /* Little bit of braindamage here.  The SSE compare instructions
8372              does use completely different names for the comparisons that the
8373              fp conditional moves.  */
8374           switch (GET_CODE (x))
8375             {
8376             case EQ:
8377             case UNEQ:
8378               fputs ("eq", file);
8379               break;
8380             case LT:
8381             case UNLT:
8382               fputs ("lt", file);
8383               break;
8384             case LE:
8385             case UNLE:
8386               fputs ("le", file);
8387               break;
8388             case UNORDERED:
8389               fputs ("unord", file);
8390               break;
8391             case NE:
8392             case LTGT:
8393               fputs ("neq", file);
8394               break;
8395             case UNGE:
8396             case GE:
8397               fputs ("nlt", file);
8398               break;
8399             case UNGT:
8400             case GT:
8401               fputs ("nle", file);
8402               break;
8403             case ORDERED:
8404               fputs ("ord", file);
8405               break;
8406             default:
8407               gcc_unreachable ();
8408             }
8409           return;
8410         case 'O':
8411 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8412           if (ASSEMBLER_DIALECT == ASM_ATT)
8413             {
8414               switch (GET_MODE (x))
8415                 {
8416                 case HImode: putc ('w', file); break;
8417                 case SImode:
8418                 case SFmode: putc ('l', file); break;
8419                 case DImode:
8420                 case DFmode: putc ('q', file); break;
8421                 default: gcc_unreachable ();
8422                 }
8423               putc ('.', file);
8424             }
8425 #endif
8426           return;
8427         case 'C':
8428           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
8429           return;
8430         case 'F':
8431 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8432           if (ASSEMBLER_DIALECT == ASM_ATT)
8433             putc ('.', file);
8434 #endif
8435           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
8436           return;
8437
8438           /* Like above, but reverse condition */
8439         case 'c':
8440           /* Check to see if argument to %c is really a constant
8441              and not a condition code which needs to be reversed.  */
8442           if (!COMPARISON_P (x))
8443           {
8444             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
8445              return;
8446           }
8447           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
8448           return;
8449         case 'f':
8450 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
8451           if (ASSEMBLER_DIALECT == ASM_ATT)
8452             putc ('.', file);
8453 #endif
8454           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
8455           return;
8456
8457         case 'H':
8458           /* It doesn't actually matter what mode we use here, as we're
8459              only going to use this for printing.  */
8460           x = adjust_address_nv (x, DImode, 8);
8461           break;
8462
8463         case '+':
8464           {
8465             rtx x;
8466
8467             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
8468               return;
8469
8470             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
8471             if (x)
8472               {
8473                 int pred_val = INTVAL (XEXP (x, 0));
8474
8475                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
8476                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
8477                   {
8478                     int taken = pred_val > REG_BR_PROB_BASE / 2;
8479                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
8480
8481                     /* Emit hints only in the case default branch prediction
8482                        heuristics would fail.  */
8483                     if (taken != cputaken)
8484                       {
8485                         /* We use 3e (DS) prefix for taken branches and
8486                            2e (CS) prefix for not taken branches.  */
8487                         if (taken)
8488                           fputs ("ds ; ", file);
8489                         else
8490                           fputs ("cs ; ", file);
8491                       }
8492                   }
8493               }
8494             return;
8495           }
8496         default:
8497             output_operand_lossage ("invalid operand code '%c'", code);
8498         }
8499     }
8500
8501   if (REG_P (x))
8502     print_reg (x, code, file);
8503
8504   else if (MEM_P (x))
8505     {
8506       /* No `byte ptr' prefix for call instructions.  */
8507       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
8508         {
8509           const char * size;
8510           switch (GET_MODE_SIZE (GET_MODE (x)))
8511             {
8512             case 1: size = "BYTE"; break;
8513             case 2: size = "WORD"; break;
8514             case 4: size = "DWORD"; break;
8515             case 8: size = "QWORD"; break;
8516             case 12: size = "XWORD"; break;
8517             case 16: size = "XMMWORD"; break;
8518             default:
8519               gcc_unreachable ();
8520             }
8521
8522           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
8523           if (code == 'b')
8524             size = "BYTE";
8525           else if (code == 'w')
8526             size = "WORD";
8527           else if (code == 'k')
8528             size = "DWORD";
8529
8530           fputs (size, file);
8531           fputs (" PTR ", file);
8532         }
8533
8534       x = XEXP (x, 0);
8535       /* Avoid (%rip) for call operands.  */
8536       if (CONSTANT_ADDRESS_P (x) && code == 'P'
8537           && !CONST_INT_P (x))
8538         output_addr_const (file, x);
8539       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
8540         output_operand_lossage ("invalid constraints for operand");
8541       else
8542         output_address (x);
8543     }
8544
8545   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
8546     {
8547       REAL_VALUE_TYPE r;
8548       long l;
8549
8550       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
8551       REAL_VALUE_TO_TARGET_SINGLE (r, l);
8552
8553       if (ASSEMBLER_DIALECT == ASM_ATT)
8554         putc ('$', file);
8555       fprintf (file, "0x%08lx", l);
8556     }
8557
8558   /* These float cases don't actually occur as immediate operands.  */
8559   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
8560     {
8561       char dstr[30];
8562
8563       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8564       fprintf (file, "%s", dstr);
8565     }
8566
8567   else if (GET_CODE (x) == CONST_DOUBLE
8568            && GET_MODE (x) == XFmode)
8569     {
8570       char dstr[30];
8571
8572       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8573       fprintf (file, "%s", dstr);
8574     }
8575
8576   else
8577     {
8578       /* We have patterns that allow zero sets of memory, for instance.
8579          In 64-bit mode, we should probably support all 8-byte vectors,
8580          since we can in fact encode that into an immediate.  */
8581       if (GET_CODE (x) == CONST_VECTOR)
8582         {
8583           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
8584           x = const0_rtx;
8585         }
8586
8587       if (code != 'P')
8588         {
8589           if (CONST_INT_P (x) || GET_CODE (x) == CONST_DOUBLE)
8590             {
8591               if (ASSEMBLER_DIALECT == ASM_ATT)
8592                 putc ('$', file);
8593             }
8594           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
8595                    || GET_CODE (x) == LABEL_REF)
8596             {
8597               if (ASSEMBLER_DIALECT == ASM_ATT)
8598                 putc ('$', file);
8599               else
8600                 fputs ("OFFSET FLAT:", file);
8601             }
8602         }
8603       if (CONST_INT_P (x))
8604         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8605       else if (flag_pic)
8606         output_pic_addr_const (file, x, code);
8607       else
8608         output_addr_const (file, x);
8609     }
8610 }
8611 \f
8612 /* Print a memory operand whose address is ADDR.  */
8613
8614 void
8615 print_operand_address (FILE *file, rtx addr)
8616 {
8617   struct ix86_address parts;
8618   rtx base, index, disp;
8619   int scale;
8620   int ok = ix86_decompose_address (addr, &parts);
8621
8622   gcc_assert (ok);
8623
8624   base = parts.base;
8625   index = parts.index;
8626   disp = parts.disp;
8627   scale = parts.scale;
8628
8629   switch (parts.seg)
8630     {
8631     case SEG_DEFAULT:
8632       break;
8633     case SEG_FS:
8634     case SEG_GS:
8635       if (USER_LABEL_PREFIX[0] == 0)
8636         putc ('%', file);
8637       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8638       break;
8639     default:
8640       gcc_unreachable ();
8641     }
8642
8643   if (!base && !index)
8644     {
8645       /* Displacement only requires special attention.  */
8646
8647       if (CONST_INT_P (disp))
8648         {
8649           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8650             {
8651               if (USER_LABEL_PREFIX[0] == 0)
8652                 putc ('%', file);
8653               fputs ("ds:", file);
8654             }
8655           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8656         }
8657       else if (flag_pic)
8658         output_pic_addr_const (file, disp, 0);
8659       else
8660         output_addr_const (file, disp);
8661
8662       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8663       if (TARGET_64BIT)
8664         {
8665           if (GET_CODE (disp) == CONST
8666               && GET_CODE (XEXP (disp, 0)) == PLUS
8667               && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8668             disp = XEXP (XEXP (disp, 0), 0);
8669           if (GET_CODE (disp) == LABEL_REF
8670               || (GET_CODE (disp) == SYMBOL_REF
8671                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8672             fputs ("(%rip)", file);
8673         }
8674     }
8675   else
8676     {
8677       if (ASSEMBLER_DIALECT == ASM_ATT)
8678         {
8679           if (disp)
8680             {
8681               if (flag_pic)
8682                 output_pic_addr_const (file, disp, 0);
8683               else if (GET_CODE (disp) == LABEL_REF)
8684                 output_asm_label (disp);
8685               else
8686                 output_addr_const (file, disp);
8687             }
8688
8689           putc ('(', file);
8690           if (base)
8691             print_reg (base, 0, file);
8692           if (index)
8693             {
8694               putc (',', file);
8695               print_reg (index, 0, file);
8696               if (scale != 1)
8697                 fprintf (file, ",%d", scale);
8698             }
8699           putc (')', file);
8700         }
8701       else
8702         {
8703           rtx offset = NULL_RTX;
8704
8705           if (disp)
8706             {
8707               /* Pull out the offset of a symbol; print any symbol itself.  */
8708               if (GET_CODE (disp) == CONST
8709                   && GET_CODE (XEXP (disp, 0)) == PLUS
8710                   && CONST_INT_P (XEXP (XEXP (disp, 0), 1)))
8711                 {
8712                   offset = XEXP (XEXP (disp, 0), 1);
8713                   disp = gen_rtx_CONST (VOIDmode,
8714                                         XEXP (XEXP (disp, 0), 0));
8715                 }
8716
8717               if (flag_pic)
8718                 output_pic_addr_const (file, disp, 0);
8719               else if (GET_CODE (disp) == LABEL_REF)
8720                 output_asm_label (disp);
8721               else if (CONST_INT_P (disp))
8722                 offset = disp;
8723               else
8724                 output_addr_const (file, disp);
8725             }
8726
8727           putc ('[', file);
8728           if (base)
8729             {
8730               print_reg (base, 0, file);
8731               if (offset)
8732                 {
8733                   if (INTVAL (offset) >= 0)
8734                     putc ('+', file);
8735                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8736                 }
8737             }
8738           else if (offset)
8739             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8740           else
8741             putc ('0', file);
8742
8743           if (index)
8744             {
8745               putc ('+', file);
8746               print_reg (index, 0, file);
8747               if (scale != 1)
8748                 fprintf (file, "*%d", scale);
8749             }
8750           putc (']', file);
8751         }
8752     }
8753 }
8754
8755 bool
8756 output_addr_const_extra (FILE *file, rtx x)
8757 {
8758   rtx op;
8759
8760   if (GET_CODE (x) != UNSPEC)
8761     return false;
8762
8763   op = XVECEXP (x, 0, 0);
8764   switch (XINT (x, 1))
8765     {
8766     case UNSPEC_GOTTPOFF:
8767       output_addr_const (file, op);
8768       /* FIXME: This might be @TPOFF in Sun ld.  */
8769       fputs ("@GOTTPOFF", file);
8770       break;
8771     case UNSPEC_TPOFF:
8772       output_addr_const (file, op);
8773       fputs ("@TPOFF", file);
8774       break;
8775     case UNSPEC_NTPOFF:
8776       output_addr_const (file, op);
8777       if (TARGET_64BIT)
8778         fputs ("@TPOFF", file);
8779       else
8780         fputs ("@NTPOFF", file);
8781       break;
8782     case UNSPEC_DTPOFF:
8783       output_addr_const (file, op);
8784       fputs ("@DTPOFF", file);
8785       break;
8786     case UNSPEC_GOTNTPOFF:
8787       output_addr_const (file, op);
8788       if (TARGET_64BIT)
8789         fputs ("@GOTTPOFF(%rip)", file);
8790       else
8791         fputs ("@GOTNTPOFF", file);
8792       break;
8793     case UNSPEC_INDNTPOFF:
8794       output_addr_const (file, op);
8795       fputs ("@INDNTPOFF", file);
8796       break;
8797
8798     default:
8799       return false;
8800     }
8801
8802   return true;
8803 }
8804 \f
8805 /* Split one or more DImode RTL references into pairs of SImode
8806    references.  The RTL can be REG, offsettable MEM, integer constant, or
8807    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8808    split and "num" is its length.  lo_half and hi_half are output arrays
8809    that parallel "operands".  */
8810
8811 void
8812 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8813 {
8814   while (num--)
8815     {
8816       rtx op = operands[num];
8817
8818       /* simplify_subreg refuse to split volatile memory addresses,
8819          but we still have to handle it.  */
8820       if (MEM_P (op))
8821         {
8822           lo_half[num] = adjust_address (op, SImode, 0);
8823           hi_half[num] = adjust_address (op, SImode, 4);
8824         }
8825       else
8826         {
8827           lo_half[num] = simplify_gen_subreg (SImode, op,
8828                                               GET_MODE (op) == VOIDmode
8829                                               ? DImode : GET_MODE (op), 0);
8830           hi_half[num] = simplify_gen_subreg (SImode, op,
8831                                               GET_MODE (op) == VOIDmode
8832                                               ? DImode : GET_MODE (op), 4);
8833         }
8834     }
8835 }
8836 /* Split one or more TImode RTL references into pairs of DImode
8837    references.  The RTL can be REG, offsettable MEM, integer constant, or
8838    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8839    split and "num" is its length.  lo_half and hi_half are output arrays
8840    that parallel "operands".  */
8841
8842 void
8843 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8844 {
8845   while (num--)
8846     {
8847       rtx op = operands[num];
8848
8849       /* simplify_subreg refuse to split volatile memory addresses, but we
8850          still have to handle it.  */
8851       if (MEM_P (op))
8852         {
8853           lo_half[num] = adjust_address (op, DImode, 0);
8854           hi_half[num] = adjust_address (op, DImode, 8);
8855         }
8856       else
8857         {
8858           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8859           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8860         }
8861     }
8862 }
8863 \f
8864 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8865    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8866    is the expression of the binary operation.  The output may either be
8867    emitted here, or returned to the caller, like all output_* functions.
8868
8869    There is no guarantee that the operands are the same mode, as they
8870    might be within FLOAT or FLOAT_EXTEND expressions.  */
8871
8872 #ifndef SYSV386_COMPAT
8873 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8874    wants to fix the assemblers because that causes incompatibility
8875    with gcc.  No-one wants to fix gcc because that causes
8876    incompatibility with assemblers...  You can use the option of
8877    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8878 #define SYSV386_COMPAT 1
8879 #endif
8880
8881 const char *
8882 output_387_binary_op (rtx insn, rtx *operands)
8883 {
8884   static char buf[30];
8885   const char *p;
8886   const char *ssep;
8887   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8888
8889 #ifdef ENABLE_CHECKING
8890   /* Even if we do not want to check the inputs, this documents input
8891      constraints.  Which helps in understanding the following code.  */
8892   if (STACK_REG_P (operands[0])
8893       && ((REG_P (operands[1])
8894            && REGNO (operands[0]) == REGNO (operands[1])
8895            && (STACK_REG_P (operands[2]) || MEM_P (operands[2])))
8896           || (REG_P (operands[2])
8897               && REGNO (operands[0]) == REGNO (operands[2])
8898               && (STACK_REG_P (operands[1]) || MEM_P (operands[1]))))
8899       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8900     ; /* ok */
8901   else
8902     gcc_assert (is_sse);
8903 #endif
8904
8905   switch (GET_CODE (operands[3]))
8906     {
8907     case PLUS:
8908       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8909           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8910         p = "fiadd";
8911       else
8912         p = "fadd";
8913       ssep = "add";
8914       break;
8915
8916     case MINUS:
8917       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8918           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8919         p = "fisub";
8920       else
8921         p = "fsub";
8922       ssep = "sub";
8923       break;
8924
8925     case MULT:
8926       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8927           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8928         p = "fimul";
8929       else
8930         p = "fmul";
8931       ssep = "mul";
8932       break;
8933
8934     case DIV:
8935       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8936           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8937         p = "fidiv";
8938       else
8939         p = "fdiv";
8940       ssep = "div";
8941       break;
8942
8943     default:
8944       gcc_unreachable ();
8945     }
8946
8947   if (is_sse)
8948    {
8949       strcpy (buf, ssep);
8950       if (GET_MODE (operands[0]) == SFmode)
8951         strcat (buf, "ss\t{%2, %0|%0, %2}");
8952       else
8953         strcat (buf, "sd\t{%2, %0|%0, %2}");
8954       return buf;
8955    }
8956   strcpy (buf, p);
8957
8958   switch (GET_CODE (operands[3]))
8959     {
8960     case MULT:
8961     case PLUS:
8962       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8963         {
8964           rtx temp = operands[2];
8965           operands[2] = operands[1];
8966           operands[1] = temp;
8967         }
8968
8969       /* know operands[0] == operands[1].  */
8970
8971       if (MEM_P (operands[2]))
8972         {
8973           p = "%z2\t%2";
8974           break;
8975         }
8976
8977       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8978         {
8979           if (STACK_TOP_P (operands[0]))
8980             /* How is it that we are storing to a dead operand[2]?
8981                Well, presumably operands[1] is dead too.  We can't
8982                store the result to st(0) as st(0) gets popped on this
8983                instruction.  Instead store to operands[2] (which I
8984                think has to be st(1)).  st(1) will be popped later.
8985                gcc <= 2.8.1 didn't have this check and generated
8986                assembly code that the Unixware assembler rejected.  */
8987             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8988           else
8989             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8990           break;
8991         }
8992
8993       if (STACK_TOP_P (operands[0]))
8994         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8995       else
8996         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8997       break;
8998
8999     case MINUS:
9000     case DIV:
9001       if (MEM_P (operands[1]))
9002         {
9003           p = "r%z1\t%1";
9004           break;
9005         }
9006
9007       if (MEM_P (operands[2]))
9008         {
9009           p = "%z2\t%2";
9010           break;
9011         }
9012
9013       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
9014         {
9015 #if SYSV386_COMPAT
9016           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
9017              derived assemblers, confusingly reverse the direction of
9018              the operation for fsub{r} and fdiv{r} when the
9019              destination register is not st(0).  The Intel assembler
9020              doesn't have this brain damage.  Read !SYSV386_COMPAT to
9021              figure out what the hardware really does.  */
9022           if (STACK_TOP_P (operands[0]))
9023             p = "{p\t%0, %2|rp\t%2, %0}";
9024           else
9025             p = "{rp\t%2, %0|p\t%0, %2}";
9026 #else
9027           if (STACK_TOP_P (operands[0]))
9028             /* As above for fmul/fadd, we can't store to st(0).  */
9029             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
9030           else
9031             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
9032 #endif
9033           break;
9034         }
9035
9036       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
9037         {
9038 #if SYSV386_COMPAT
9039           if (STACK_TOP_P (operands[0]))
9040             p = "{rp\t%0, %1|p\t%1, %0}";
9041           else
9042             p = "{p\t%1, %0|rp\t%0, %1}";
9043 #else
9044           if (STACK_TOP_P (operands[0]))
9045             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
9046           else
9047             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
9048 #endif
9049           break;
9050         }
9051
9052       if (STACK_TOP_P (operands[0]))
9053         {
9054           if (STACK_TOP_P (operands[1]))
9055             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
9056           else
9057             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
9058           break;
9059         }
9060       else if (STACK_TOP_P (operands[1]))
9061         {
9062 #if SYSV386_COMPAT
9063           p = "{\t%1, %0|r\t%0, %1}";
9064 #else
9065           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
9066 #endif
9067         }
9068       else
9069         {
9070 #if SYSV386_COMPAT
9071           p = "{r\t%2, %0|\t%0, %2}";
9072 #else
9073           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
9074 #endif
9075         }
9076       break;
9077
9078     default:
9079       gcc_unreachable ();
9080     }
9081
9082   strcat (buf, p);
9083   return buf;
9084 }
9085
9086 /* Return needed mode for entity in optimize_mode_switching pass.  */
9087
9088 int
9089 ix86_mode_needed (int entity, rtx insn)
9090 {
9091   enum attr_i387_cw mode;
9092
9093   /* The mode UNINITIALIZED is used to store control word after a
9094      function call or ASM pattern.  The mode ANY specify that function
9095      has no requirements on the control word and make no changes in the
9096      bits we are interested in.  */
9097
9098   if (CALL_P (insn)
9099       || (NONJUMP_INSN_P (insn)
9100           && (asm_noperands (PATTERN (insn)) >= 0
9101               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
9102     return I387_CW_UNINITIALIZED;
9103
9104   if (recog_memoized (insn) < 0)
9105     return I387_CW_ANY;
9106
9107   mode = get_attr_i387_cw (insn);
9108
9109   switch (entity)
9110     {
9111     case I387_TRUNC:
9112       if (mode == I387_CW_TRUNC)
9113         return mode;
9114       break;
9115
9116     case I387_FLOOR:
9117       if (mode == I387_CW_FLOOR)
9118         return mode;
9119       break;
9120
9121     case I387_CEIL:
9122       if (mode == I387_CW_CEIL)
9123         return mode;
9124       break;
9125
9126     case I387_MASK_PM:
9127       if (mode == I387_CW_MASK_PM)
9128         return mode;
9129       break;
9130
9131     default:
9132       gcc_unreachable ();
9133     }
9134
9135   return I387_CW_ANY;
9136 }
9137
9138 /* Output code to initialize control word copies used by trunc?f?i and
9139    rounding patterns.  CURRENT_MODE is set to current control word,
9140    while NEW_MODE is set to new control word.  */
9141
9142 void
9143 emit_i387_cw_initialization (int mode)
9144 {
9145   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
9146   rtx new_mode;
9147
9148   int slot;
9149
9150   rtx reg = gen_reg_rtx (HImode);
9151
9152   emit_insn (gen_x86_fnstcw_1 (stored_mode));
9153   emit_move_insn (reg, copy_rtx (stored_mode));
9154
9155   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
9156     {
9157       switch (mode)
9158         {
9159         case I387_CW_TRUNC:
9160           /* round toward zero (truncate) */
9161           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
9162           slot = SLOT_CW_TRUNC;
9163           break;
9164
9165         case I387_CW_FLOOR:
9166           /* round down toward -oo */
9167           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9168           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
9169           slot = SLOT_CW_FLOOR;
9170           break;
9171
9172         case I387_CW_CEIL:
9173           /* round up toward +oo */
9174           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
9175           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
9176           slot = SLOT_CW_CEIL;
9177           break;
9178
9179         case I387_CW_MASK_PM:
9180           /* mask precision exception for nearbyint() */
9181           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9182           slot = SLOT_CW_MASK_PM;
9183           break;
9184
9185         default:
9186           gcc_unreachable ();
9187         }
9188     }
9189   else
9190     {
9191       switch (mode)
9192         {
9193         case I387_CW_TRUNC:
9194           /* round toward zero (truncate) */
9195           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
9196           slot = SLOT_CW_TRUNC;
9197           break;
9198
9199         case I387_CW_FLOOR:
9200           /* round down toward -oo */
9201           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
9202           slot = SLOT_CW_FLOOR;
9203           break;
9204
9205         case I387_CW_CEIL:
9206           /* round up toward +oo */
9207           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
9208           slot = SLOT_CW_CEIL;
9209           break;
9210
9211         case I387_CW_MASK_PM:
9212           /* mask precision exception for nearbyint() */
9213           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
9214           slot = SLOT_CW_MASK_PM;
9215           break;
9216
9217         default:
9218           gcc_unreachable ();
9219         }
9220     }
9221
9222   gcc_assert (slot < MAX_386_STACK_LOCALS);
9223
9224   new_mode = assign_386_stack_local (HImode, slot);
9225   emit_move_insn (new_mode, reg);
9226 }
9227
9228 /* Output code for INSN to convert a float to a signed int.  OPERANDS
9229    are the insn operands.  The output may be [HSD]Imode and the input
9230    operand may be [SDX]Fmode.  */
9231
9232 const char *
9233 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
9234 {
9235   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9236   int dimode_p = GET_MODE (operands[0]) == DImode;
9237   int round_mode = get_attr_i387_cw (insn);
9238
9239   /* Jump through a hoop or two for DImode, since the hardware has no
9240      non-popping instruction.  We used to do this a different way, but
9241      that was somewhat fragile and broke with post-reload splitters.  */
9242   if ((dimode_p || fisttp) && !stack_top_dies)
9243     output_asm_insn ("fld\t%y1", operands);
9244
9245   gcc_assert (STACK_TOP_P (operands[1]));
9246   gcc_assert (MEM_P (operands[0]));
9247
9248   if (fisttp)
9249       output_asm_insn ("fisttp%z0\t%0", operands);
9250   else
9251     {
9252       if (round_mode != I387_CW_ANY)
9253         output_asm_insn ("fldcw\t%3", operands);
9254       if (stack_top_dies || dimode_p)
9255         output_asm_insn ("fistp%z0\t%0", operands);
9256       else
9257         output_asm_insn ("fist%z0\t%0", operands);
9258       if (round_mode != I387_CW_ANY)
9259         output_asm_insn ("fldcw\t%2", operands);
9260     }
9261
9262   return "";
9263 }
9264
9265 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
9266    have the values zero or one, indicates the ffreep insn's operand
9267    from the OPERANDS array.  */
9268
9269 static const char *
9270 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
9271 {
9272   if (TARGET_USE_FFREEP)
9273 #if HAVE_AS_IX86_FFREEP
9274     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
9275 #else
9276     {
9277       static char retval[] = ".word\t0xc_df";
9278       int regno = REGNO (operands[opno]);
9279       
9280       gcc_assert (FP_REGNO_P (regno));
9281
9282       retval[9] = '0' + (regno - FIRST_STACK_REG);
9283       return retval;
9284     }
9285 #endif
9286
9287   return opno ? "fstp\t%y1" : "fstp\t%y0";
9288 }
9289
9290
9291 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
9292    should be used.  UNORDERED_P is true when fucom should be used.  */
9293
9294 const char *
9295 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
9296 {
9297   int stack_top_dies;
9298   rtx cmp_op0, cmp_op1;
9299   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
9300
9301   if (eflags_p)
9302     {
9303       cmp_op0 = operands[0];
9304       cmp_op1 = operands[1];
9305     }
9306   else
9307     {
9308       cmp_op0 = operands[1];
9309       cmp_op1 = operands[2];
9310     }
9311
9312   if (is_sse)
9313     {
9314       if (GET_MODE (operands[0]) == SFmode)
9315         if (unordered_p)
9316           return "ucomiss\t{%1, %0|%0, %1}";
9317         else
9318           return "comiss\t{%1, %0|%0, %1}";
9319       else
9320         if (unordered_p)
9321           return "ucomisd\t{%1, %0|%0, %1}";
9322         else
9323           return "comisd\t{%1, %0|%0, %1}";
9324     }
9325
9326   gcc_assert (STACK_TOP_P (cmp_op0));
9327
9328   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
9329
9330   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
9331     {
9332       if (stack_top_dies)
9333         {
9334           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
9335           return output_387_ffreep (operands, 1);
9336         }
9337       else
9338         return "ftst\n\tfnstsw\t%0";
9339     }
9340
9341   if (STACK_REG_P (cmp_op1)
9342       && stack_top_dies
9343       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
9344       && REGNO (cmp_op1) != FIRST_STACK_REG)
9345     {
9346       /* If both the top of the 387 stack dies, and the other operand
9347          is also a stack register that dies, then this must be a
9348          `fcompp' float compare */
9349
9350       if (eflags_p)
9351         {
9352           /* There is no double popping fcomi variant.  Fortunately,
9353              eflags is immune from the fstp's cc clobbering.  */
9354           if (unordered_p)
9355             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
9356           else
9357             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
9358           return output_387_ffreep (operands, 0);
9359         }
9360       else
9361         {
9362           if (unordered_p)
9363             return "fucompp\n\tfnstsw\t%0";
9364           else
9365             return "fcompp\n\tfnstsw\t%0";
9366         }
9367     }
9368   else
9369     {
9370       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
9371
9372       static const char * const alt[16] =
9373       {
9374         "fcom%z2\t%y2\n\tfnstsw\t%0",
9375         "fcomp%z2\t%y2\n\tfnstsw\t%0",
9376         "fucom%z2\t%y2\n\tfnstsw\t%0",
9377         "fucomp%z2\t%y2\n\tfnstsw\t%0",
9378
9379         "ficom%z2\t%y2\n\tfnstsw\t%0",
9380         "ficomp%z2\t%y2\n\tfnstsw\t%0",
9381         NULL,
9382         NULL,
9383
9384         "fcomi\t{%y1, %0|%0, %y1}",
9385         "fcomip\t{%y1, %0|%0, %y1}",
9386         "fucomi\t{%y1, %0|%0, %y1}",
9387         "fucomip\t{%y1, %0|%0, %y1}",
9388
9389         NULL,
9390         NULL,
9391         NULL,
9392         NULL
9393       };
9394
9395       int mask;
9396       const char *ret;
9397
9398       mask  = eflags_p << 3;
9399       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
9400       mask |= unordered_p << 1;
9401       mask |= stack_top_dies;
9402
9403       gcc_assert (mask < 16);
9404       ret = alt[mask];
9405       gcc_assert (ret);
9406
9407       return ret;
9408     }
9409 }
9410
9411 void
9412 ix86_output_addr_vec_elt (FILE *file, int value)
9413 {
9414   const char *directive = ASM_LONG;
9415
9416 #ifdef ASM_QUAD
9417   if (TARGET_64BIT)
9418     directive = ASM_QUAD;
9419 #else
9420   gcc_assert (!TARGET_64BIT);
9421 #endif
9422
9423   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
9424 }
9425
9426 void
9427 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
9428 {
9429   const char *directive = ASM_LONG;
9430
9431 #ifdef ASM_QUAD
9432   if (TARGET_64BIT && CASE_VECTOR_MODE == DImode)
9433     directive = ASM_QUAD;
9434 #else
9435   gcc_assert (!TARGET_64BIT);
9436 #endif
9437   /* We can't use @GOTOFF for text labels on VxWorks; see gotoff_operand.  */
9438   if (TARGET_64BIT || TARGET_VXWORKS_RTP)
9439     fprintf (file, "%s%s%d-%s%d\n",
9440              directive, LPREFIX, value, LPREFIX, rel);
9441   else if (HAVE_AS_GOTOFF_IN_DATA)
9442     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
9443 #if TARGET_MACHO
9444   else if (TARGET_MACHO)
9445     {
9446       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
9447       machopic_output_function_base_name (file);
9448       fprintf(file, "\n");
9449     }
9450 #endif
9451   else
9452     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
9453                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
9454 }
9455 \f
9456 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
9457    for the target.  */
9458
9459 void
9460 ix86_expand_clear (rtx dest)
9461 {
9462   rtx tmp;
9463
9464   /* We play register width games, which are only valid after reload.  */
9465   gcc_assert (reload_completed);
9466
9467   /* Avoid HImode and its attendant prefix byte.  */
9468   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
9469     dest = gen_rtx_REG (SImode, REGNO (dest));
9470   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
9471
9472   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
9473   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
9474     {
9475       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
9476       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
9477     }
9478
9479   emit_insn (tmp);
9480 }
9481
9482 /* X is an unchanging MEM.  If it is a constant pool reference, return
9483    the constant pool rtx, else NULL.  */
9484
9485 rtx
9486 maybe_get_pool_constant (rtx x)
9487 {
9488   x = ix86_delegitimize_address (XEXP (x, 0));
9489
9490   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
9491     return get_pool_constant (x);
9492
9493   return NULL_RTX;
9494 }
9495
9496 void
9497 ix86_expand_move (enum machine_mode mode, rtx operands[])
9498 {
9499   int strict = (reload_in_progress || reload_completed);
9500   rtx op0, op1;
9501   enum tls_model model;
9502
9503   op0 = operands[0];
9504   op1 = operands[1];
9505
9506   if (GET_CODE (op1) == SYMBOL_REF)
9507     {
9508       model = SYMBOL_REF_TLS_MODEL (op1);
9509       if (model)
9510         {
9511           op1 = legitimize_tls_address (op1, model, true);
9512           op1 = force_operand (op1, op0);
9513           if (op1 == op0)
9514             return;
9515         }
9516       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
9517                && SYMBOL_REF_DLLIMPORT_P (op1))
9518         op1 = legitimize_dllimport_symbol (op1, false);
9519     }
9520   else if (GET_CODE (op1) == CONST
9521            && GET_CODE (XEXP (op1, 0)) == PLUS
9522            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
9523     {
9524       rtx addend = XEXP (XEXP (op1, 0), 1);
9525       rtx symbol = XEXP (XEXP (op1, 0), 0);
9526       rtx tmp = NULL;
9527
9528       model = SYMBOL_REF_TLS_MODEL (symbol);
9529       if (model)
9530         tmp = legitimize_tls_address (symbol, model, true);
9531       else if (TARGET_DLLIMPORT_DECL_ATTRIBUTES
9532                && SYMBOL_REF_DLLIMPORT_P (symbol))
9533         tmp = legitimize_dllimport_symbol (symbol, true);
9534
9535       if (tmp)
9536         {
9537           tmp = force_operand (tmp, NULL);
9538           tmp = expand_simple_binop (Pmode, PLUS, tmp, addend,
9539                                      op0, 1, OPTAB_DIRECT);
9540           if (tmp == op0)
9541             return;
9542         }
9543     }
9544
9545   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
9546     {
9547       if (TARGET_MACHO && !TARGET_64BIT)
9548         {
9549 #if TARGET_MACHO
9550           if (MACHOPIC_PURE)
9551             {
9552               rtx temp = ((reload_in_progress
9553                            || ((op0 && REG_P (op0))
9554                                && mode == Pmode))
9555                           ? op0 : gen_reg_rtx (Pmode));
9556               op1 = machopic_indirect_data_reference (op1, temp);
9557               op1 = machopic_legitimize_pic_address (op1, mode,
9558                                                      temp == op1 ? 0 : temp);
9559             }
9560           else if (MACHOPIC_INDIRECT)
9561             op1 = machopic_indirect_data_reference (op1, 0);
9562           if (op0 == op1)
9563             return;
9564 #endif
9565         }
9566       else
9567         {
9568           if (MEM_P (op0))
9569             op1 = force_reg (Pmode, op1);
9570           else if (!TARGET_64BIT || !x86_64_movabs_operand (op1, Pmode))
9571             {
9572               rtx reg = no_new_pseudos ? op0 : NULL_RTX;
9573               op1 = legitimize_pic_address (op1, reg);
9574               if (op0 == op1)
9575                 return;
9576             }
9577         }
9578     }
9579   else
9580     {
9581       if (MEM_P (op0)
9582           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
9583               || !push_operand (op0, mode))
9584           && MEM_P (op1))
9585         op1 = force_reg (mode, op1);
9586
9587       if (push_operand (op0, mode)
9588           && ! general_no_elim_operand (op1, mode))
9589         op1 = copy_to_mode_reg (mode, op1);
9590
9591       /* Force large constants in 64bit compilation into register
9592          to get them CSEed.  */
9593       if (TARGET_64BIT && mode == DImode
9594           && immediate_operand (op1, mode)
9595           && !x86_64_zext_immediate_operand (op1, VOIDmode)
9596           && !register_operand (op0, mode)
9597           && optimize && !reload_completed && !reload_in_progress)
9598         op1 = copy_to_mode_reg (mode, op1);
9599
9600       if (FLOAT_MODE_P (mode))
9601         {
9602           /* If we are loading a floating point constant to a register,
9603              force the value to memory now, since we'll get better code
9604              out the back end.  */
9605
9606           if (strict)
9607             ;
9608           else if (GET_CODE (op1) == CONST_DOUBLE)
9609             {
9610               op1 = validize_mem (force_const_mem (mode, op1));
9611               if (!register_operand (op0, mode))
9612                 {
9613                   rtx temp = gen_reg_rtx (mode);
9614                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
9615                   emit_move_insn (op0, temp);
9616                   return;
9617                 }
9618             }
9619         }
9620     }
9621
9622   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9623 }
9624
9625 void
9626 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9627 {
9628   rtx op0 = operands[0], op1 = operands[1];
9629
9630   /* Force constants other than zero into memory.  We do not know how
9631      the instructions used to build constants modify the upper 64 bits
9632      of the register, once we have that information we may be able
9633      to handle some of them more efficiently.  */
9634   if ((reload_in_progress | reload_completed) == 0
9635       && register_operand (op0, mode)
9636       && CONSTANT_P (op1)
9637       && standard_sse_constant_p (op1) <= 0)
9638     op1 = validize_mem (force_const_mem (mode, op1));
9639
9640   /* Make operand1 a register if it isn't already.  */
9641   if (!no_new_pseudos
9642       && !register_operand (op0, mode)
9643       && !register_operand (op1, mode))
9644     {
9645       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9646       return;
9647     }
9648
9649   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9650 }
9651
9652 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9653    straight to ix86_expand_vector_move.  */
9654 /* Code generation for scalar reg-reg moves of single and double precision data:
9655      if (x86_sse_partial_reg_dependency == true | x86_sse_split_regs == true)
9656        movaps reg, reg
9657      else
9658        movss reg, reg
9659      if (x86_sse_partial_reg_dependency == true)
9660        movapd reg, reg
9661      else
9662        movsd reg, reg
9663
9664    Code generation for scalar loads of double precision data:
9665      if (x86_sse_split_regs == true)
9666        movlpd mem, reg      (gas syntax)
9667      else
9668        movsd mem, reg
9669  
9670    Code generation for unaligned packed loads of single precision data
9671    (x86_sse_unaligned_move_optimal overrides x86_sse_partial_reg_dependency):
9672      if (x86_sse_unaligned_move_optimal)
9673        movups mem, reg
9674
9675      if (x86_sse_partial_reg_dependency == true)
9676        {
9677          xorps  reg, reg
9678          movlps mem, reg
9679          movhps mem+8, reg
9680        }
9681      else
9682        {
9683          movlps mem, reg
9684          movhps mem+8, reg
9685        }
9686
9687    Code generation for unaligned packed loads of double precision data
9688    (x86_sse_unaligned_move_optimal overrides x86_sse_split_regs):
9689      if (x86_sse_unaligned_move_optimal)
9690        movupd mem, reg
9691
9692      if (x86_sse_split_regs == true)
9693        {
9694          movlpd mem, reg
9695          movhpd mem+8, reg
9696        }
9697      else
9698        {
9699          movsd  mem, reg
9700          movhpd mem+8, reg
9701        }
9702  */
9703
9704 void
9705 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9706 {
9707   rtx op0, op1, m;
9708
9709   op0 = operands[0];
9710   op1 = operands[1];
9711
9712   if (MEM_P (op1))
9713     {
9714       /* If we're optimizing for size, movups is the smallest.  */
9715       if (optimize_size)
9716         {
9717           op0 = gen_lowpart (V4SFmode, op0);
9718           op1 = gen_lowpart (V4SFmode, op1);
9719           emit_insn (gen_sse_movups (op0, op1));
9720           return;
9721         }
9722
9723       /* ??? If we have typed data, then it would appear that using
9724          movdqu is the only way to get unaligned data loaded with
9725          integer type.  */
9726       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9727         {
9728           op0 = gen_lowpart (V16QImode, op0);
9729           op1 = gen_lowpart (V16QImode, op1);
9730           emit_insn (gen_sse2_movdqu (op0, op1));
9731           return;
9732         }
9733
9734       if (TARGET_SSE2 && mode == V2DFmode)
9735         {
9736           rtx zero;
9737
9738           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9739             {
9740               op0 = gen_lowpart (V2DFmode, op0);
9741               op1 = gen_lowpart (V2DFmode, op1);
9742               emit_insn (gen_sse2_movupd (op0, op1));
9743               return;
9744             }
9745
9746           /* When SSE registers are split into halves, we can avoid
9747              writing to the top half twice.  */
9748           if (TARGET_SSE_SPLIT_REGS)
9749             {
9750               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9751               zero = op0;
9752             }
9753           else
9754             {
9755               /* ??? Not sure about the best option for the Intel chips.
9756                  The following would seem to satisfy; the register is
9757                  entirely cleared, breaking the dependency chain.  We
9758                  then store to the upper half, with a dependency depth
9759                  of one.  A rumor has it that Intel recommends two movsd
9760                  followed by an unpacklpd, but this is unconfirmed.  And
9761                  given that the dependency depth of the unpacklpd would
9762                  still be one, I'm not sure why this would be better.  */
9763               zero = CONST0_RTX (V2DFmode);
9764             }
9765
9766           m = adjust_address (op1, DFmode, 0);
9767           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9768           m = adjust_address (op1, DFmode, 8);
9769           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9770         }
9771       else
9772         {
9773           if (TARGET_SSE_UNALIGNED_MOVE_OPTIMAL)
9774             {
9775               op0 = gen_lowpart (V4SFmode, op0);
9776               op1 = gen_lowpart (V4SFmode, op1);
9777               emit_insn (gen_sse_movups (op0, op1));
9778               return;
9779             }
9780
9781           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9782             emit_move_insn (op0, CONST0_RTX (mode));
9783           else
9784             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9785
9786           if (mode != V4SFmode)
9787             op0 = gen_lowpart (V4SFmode, op0);
9788           m = adjust_address (op1, V2SFmode, 0);
9789           emit_insn (gen_sse_loadlps (op0, op0, m));
9790           m = adjust_address (op1, V2SFmode, 8);
9791           emit_insn (gen_sse_loadhps (op0, op0, m));
9792         }
9793     }
9794   else if (MEM_P (op0))
9795     {
9796       /* If we're optimizing for size, movups is the smallest.  */
9797       if (optimize_size)
9798         {
9799           op0 = gen_lowpart (V4SFmode, op0);
9800           op1 = gen_lowpart (V4SFmode, op1);
9801           emit_insn (gen_sse_movups (op0, op1));
9802           return;
9803         }
9804
9805       /* ??? Similar to above, only less clear because of quote
9806          typeless stores unquote.  */
9807       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9808           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9809         {
9810           op0 = gen_lowpart (V16QImode, op0);
9811           op1 = gen_lowpart (V16QImode, op1);
9812           emit_insn (gen_sse2_movdqu (op0, op1));
9813           return;
9814         }
9815
9816       if (TARGET_SSE2 && mode == V2DFmode)
9817         {
9818           m = adjust_address (op0, DFmode, 0);
9819           emit_insn (gen_sse2_storelpd (m, op1));
9820           m = adjust_address (op0, DFmode, 8);
9821           emit_insn (gen_sse2_storehpd (m, op1));
9822         }
9823       else
9824         {
9825           if (mode != V4SFmode)
9826             op1 = gen_lowpart (V4SFmode, op1);
9827           m = adjust_address (op0, V2SFmode, 0);
9828           emit_insn (gen_sse_storelps (m, op1));
9829           m = adjust_address (op0, V2SFmode, 8);
9830           emit_insn (gen_sse_storehps (m, op1));
9831         }
9832     }
9833   else
9834     gcc_unreachable ();
9835 }
9836
9837 /* Expand a push in MODE.  This is some mode for which we do not support
9838    proper push instructions, at least from the registers that we expect
9839    the value to live in.  */
9840
9841 void
9842 ix86_expand_push (enum machine_mode mode, rtx x)
9843 {
9844   rtx tmp;
9845
9846   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9847                              GEN_INT (-GET_MODE_SIZE (mode)),
9848                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9849   if (tmp != stack_pointer_rtx)
9850     emit_move_insn (stack_pointer_rtx, tmp);
9851
9852   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9853   emit_move_insn (tmp, x);
9854 }
9855
9856 /* Helper function of ix86_fixup_binary_operands to canonicalize
9857    operand order.  Returns true if the operands should be swapped.  */
9858    
9859 static bool
9860 ix86_swap_binary_operands_p (enum rtx_code code, enum machine_mode mode,
9861                              rtx operands[])
9862 {
9863   rtx dst = operands[0];
9864   rtx src1 = operands[1];
9865   rtx src2 = operands[2];
9866
9867   /* If the operation is not commutative, we can't do anything.  */
9868   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9869     return false;
9870
9871   /* Highest priority is that src1 should match dst.  */
9872   if (rtx_equal_p (dst, src1))
9873     return false;
9874   if (rtx_equal_p (dst, src2))
9875     return true;
9876
9877   /* Next highest priority is that immediate constants come second.  */
9878   if (immediate_operand (src2, mode))
9879     return false;
9880   if (immediate_operand (src1, mode))
9881     return true;
9882
9883   /* Lowest priority is that memory references should come second.  */
9884   if (MEM_P (src2))
9885     return false;
9886   if (MEM_P (src1))
9887     return true;
9888
9889   return false;
9890 }
9891
9892
9893 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9894    destination to use for the operation.  If different from the true
9895    destination in operands[0], a copy operation will be required.  */
9896
9897 rtx
9898 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9899                             rtx operands[])
9900 {
9901   rtx dst = operands[0];
9902   rtx src1 = operands[1];
9903   rtx src2 = operands[2];
9904
9905   /* Canonicalize operand order.  */
9906   if (ix86_swap_binary_operands_p (code, mode, operands))
9907     {
9908       rtx temp = src1;
9909       src1 = src2;
9910       src2 = temp;
9911     }
9912
9913   /* Both source operands cannot be in memory.  */
9914   if (MEM_P (src1) && MEM_P (src2))
9915     {
9916       /* Optimization: Only read from memory once.  */
9917       if (rtx_equal_p (src1, src2))
9918         {
9919           src2 = force_reg (mode, src2);
9920           src1 = src2;
9921         }
9922       else
9923         src2 = force_reg (mode, src2);
9924     }
9925
9926   /* If the destination is memory, and we do not have matching source
9927      operands, do things in registers.  */
9928   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
9929     dst = gen_reg_rtx (mode);
9930
9931   /* Source 1 cannot be a constant.  */
9932   if (CONSTANT_P (src1))
9933     src1 = force_reg (mode, src1);
9934
9935   /* Source 1 cannot be a non-matching memory.  */
9936   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
9937     src1 = force_reg (mode, src1);
9938
9939   operands[1] = src1;
9940   operands[2] = src2;
9941   return dst;
9942 }
9943
9944 /* Similarly, but assume that the destination has already been
9945    set up properly.  */
9946
9947 void
9948 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9949                                     enum machine_mode mode, rtx operands[])
9950 {
9951   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9952   gcc_assert (dst == operands[0]);
9953 }
9954
9955 /* Attempt to expand a binary operator.  Make the expansion closer to the
9956    actual machine, then just general_operand, which will allow 3 separate
9957    memory references (one output, two input) in a single insn.  */
9958
9959 void
9960 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9961                              rtx operands[])
9962 {
9963   rtx src1, src2, dst, op, clob;
9964
9965   dst = ix86_fixup_binary_operands (code, mode, operands);
9966   src1 = operands[1];
9967   src2 = operands[2];
9968
9969  /* Emit the instruction.  */
9970
9971   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9972   if (reload_in_progress)
9973     {
9974       /* Reload doesn't know about the flags register, and doesn't know that
9975          it doesn't want to clobber it.  We can only do this with PLUS.  */
9976       gcc_assert (code == PLUS);
9977       emit_insn (op);
9978     }
9979   else
9980     {
9981       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9982       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9983     }
9984
9985   /* Fix up the destination if needed.  */
9986   if (dst != operands[0])
9987     emit_move_insn (operands[0], dst);
9988 }
9989
9990 /* Return TRUE or FALSE depending on whether the binary operator meets the
9991    appropriate constraints.  */
9992
9993 int
9994 ix86_binary_operator_ok (enum rtx_code code, enum machine_mode mode,
9995                          rtx operands[3])
9996 {
9997   rtx dst = operands[0];
9998   rtx src1 = operands[1];
9999   rtx src2 = operands[2];
10000
10001   /* Both source operands cannot be in memory.  */
10002   if (MEM_P (src1) && MEM_P (src2))
10003     return 0;
10004
10005   /* Canonicalize operand order for commutative operators.  */
10006   if (ix86_swap_binary_operands_p (code, mode, operands))
10007     {
10008       rtx temp = src1;
10009       src1 = src2;
10010       src2 = temp;
10011     }
10012
10013   /* If the destination is memory, we must have a matching source operand.  */
10014   if (MEM_P (dst) && !rtx_equal_p (dst, src1))
10015       return 0;
10016
10017   /* Source 1 cannot be a constant.  */
10018   if (CONSTANT_P (src1))
10019     return 0;
10020
10021   /* Source 1 cannot be a non-matching memory.  */
10022   if (MEM_P (src1) && !rtx_equal_p (dst, src1))
10023     return 0;
10024
10025   return 1;
10026 }
10027
10028 /* Attempt to expand a unary operator.  Make the expansion closer to the
10029    actual machine, then just general_operand, which will allow 2 separate
10030    memory references (one output, one input) in a single insn.  */
10031
10032 void
10033 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
10034                             rtx operands[])
10035 {
10036   int matching_memory;
10037   rtx src, dst, op, clob;
10038
10039   dst = operands[0];
10040   src = operands[1];
10041
10042   /* If the destination is memory, and we do not have matching source
10043      operands, do things in registers.  */
10044   matching_memory = 0;
10045   if (MEM_P (dst))
10046     {
10047       if (rtx_equal_p (dst, src))
10048         matching_memory = 1;
10049       else
10050         dst = gen_reg_rtx (mode);
10051     }
10052
10053   /* When source operand is memory, destination must match.  */
10054   if (MEM_P (src) && !matching_memory)
10055     src = force_reg (mode, src);
10056
10057   /* Emit the instruction.  */
10058
10059   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
10060   if (reload_in_progress || code == NOT)
10061     {
10062       /* Reload doesn't know about the flags register, and doesn't know that
10063          it doesn't want to clobber it.  */
10064       gcc_assert (code == NOT);
10065       emit_insn (op);
10066     }
10067   else
10068     {
10069       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10070       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
10071     }
10072
10073   /* Fix up the destination if needed.  */
10074   if (dst != operands[0])
10075     emit_move_insn (operands[0], dst);
10076 }
10077
10078 /* Return TRUE or FALSE depending on whether the unary operator meets the
10079    appropriate constraints.  */
10080
10081 int
10082 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
10083                         enum machine_mode mode ATTRIBUTE_UNUSED,
10084                         rtx operands[2] ATTRIBUTE_UNUSED)
10085 {
10086   /* If one of operands is memory, source and destination must match.  */
10087   if ((MEM_P (operands[0])
10088        || MEM_P (operands[1]))
10089       && ! rtx_equal_p (operands[0], operands[1]))
10090     return FALSE;
10091   return TRUE;
10092 }
10093
10094 /* Post-reload splitter for converting an SF or DFmode value in an
10095    SSE register into an unsigned SImode.  */
10096
10097 void
10098 ix86_split_convert_uns_si_sse (rtx operands[])
10099 {
10100   enum machine_mode vecmode;
10101   rtx value, large, zero_or_two31, input, two31, x;
10102
10103   large = operands[1];
10104   zero_or_two31 = operands[2];
10105   input = operands[3];
10106   two31 = operands[4];
10107   vecmode = GET_MODE (large);
10108   value = gen_rtx_REG (vecmode, REGNO (operands[0]));
10109
10110   /* Load up the value into the low element.  We must ensure that the other
10111      elements are valid floats -- zero is the easiest such value.  */
10112   if (MEM_P (input))
10113     {
10114       if (vecmode == V4SFmode)
10115         emit_insn (gen_vec_setv4sf_0 (value, CONST0_RTX (V4SFmode), input));
10116       else
10117         emit_insn (gen_sse2_loadlpd (value, CONST0_RTX (V2DFmode), input));
10118     }
10119   else
10120     {
10121       input = gen_rtx_REG (vecmode, REGNO (input));
10122       emit_move_insn (value, CONST0_RTX (vecmode));
10123       if (vecmode == V4SFmode)
10124         emit_insn (gen_sse_movss (value, value, input));
10125       else
10126         emit_insn (gen_sse2_movsd (value, value, input));
10127     }
10128
10129   emit_move_insn (large, two31);
10130   emit_move_insn (zero_or_two31, MEM_P (two31) ? large : two31);
10131
10132   x = gen_rtx_fmt_ee (LE, vecmode, large, value);
10133   emit_insn (gen_rtx_SET (VOIDmode, large, x));
10134
10135   x = gen_rtx_AND (vecmode, zero_or_two31, large);
10136   emit_insn (gen_rtx_SET (VOIDmode, zero_or_two31, x));
10137
10138   x = gen_rtx_MINUS (vecmode, value, zero_or_two31);
10139   emit_insn (gen_rtx_SET (VOIDmode, value, x));
10140
10141   large = gen_rtx_REG (V4SImode, REGNO (large));
10142   emit_insn (gen_ashlv4si3 (large, large, GEN_INT (31)));
10143
10144   x = gen_rtx_REG (V4SImode, REGNO (value));
10145   if (vecmode == V4SFmode)
10146     emit_insn (gen_sse2_cvttps2dq (x, value));
10147   else
10148     emit_insn (gen_sse2_cvttpd2dq (x, value));
10149   value = x;
10150
10151   emit_insn (gen_xorv4si3 (value, value, large));
10152 }
10153
10154 /* Convert an unsigned DImode value into a DFmode, using only SSE.
10155    Expects the 64-bit DImode to be supplied in a pair of integral
10156    registers.  Requires SSE2; will use SSE3 if available.  For x86_32,
10157    -mfpmath=sse, !optimize_size only.  */
10158
10159 void
10160 ix86_expand_convert_uns_didf_sse (rtx target, rtx input)
10161 {
10162   REAL_VALUE_TYPE bias_lo_rvt, bias_hi_rvt;
10163   rtx int_xmm, fp_xmm;
10164   rtx biases, exponents;
10165   rtx x;
10166
10167   int_xmm = gen_reg_rtx (V4SImode);
10168   if (TARGET_INTER_UNIT_MOVES)
10169     emit_insn (gen_movdi_to_sse (int_xmm, input));
10170   else if (TARGET_SSE_SPLIT_REGS)
10171     {
10172       emit_insn (gen_rtx_CLOBBER (VOIDmode, int_xmm));
10173       emit_move_insn (gen_lowpart (DImode, int_xmm), input);
10174     }
10175   else
10176     {
10177       x = gen_reg_rtx (V2DImode);
10178       ix86_expand_vector_init_one_nonzero (false, V2DImode, x, input, 0);
10179       emit_move_insn (int_xmm, gen_lowpart (V4SImode, x));
10180     }
10181
10182   x = gen_rtx_CONST_VECTOR (V4SImode,
10183                             gen_rtvec (4, GEN_INT (0x43300000UL),
10184                                        GEN_INT (0x45300000UL),
10185                                        const0_rtx, const0_rtx));
10186   exponents = validize_mem (force_const_mem (V4SImode, x));
10187
10188   /* int_xmm = {0x45300000UL, fp_xmm/hi, 0x43300000, fp_xmm/lo } */
10189   emit_insn (gen_sse2_punpckldq (int_xmm, int_xmm, exponents));
10190
10191   /* Concatenating (juxtaposing) (0x43300000UL ## fp_value_low_xmm)
10192      yields a valid DF value equal to (0x1.0p52 + double(fp_value_lo_xmm)).
10193      Similarly (0x45300000UL ## fp_value_hi_xmm) yields
10194      (0x1.0p84 + double(fp_value_hi_xmm)).
10195      Note these exponents differ by 32.  */
10196
10197   fp_xmm = copy_to_mode_reg (V2DFmode, gen_lowpart (V2DFmode, int_xmm));
10198
10199   /* Subtract off those 0x1.0p52 and 0x1.0p84 biases, to produce values
10200      in [0,2**32-1] and [0]+[2**32,2**64-1] respectively.  */
10201   real_ldexp (&bias_lo_rvt, &dconst1, 52);
10202   real_ldexp (&bias_hi_rvt, &dconst1, 84);
10203   biases = const_double_from_real_value (bias_lo_rvt, DFmode);
10204   x = const_double_from_real_value (bias_hi_rvt, DFmode);
10205   biases = gen_rtx_CONST_VECTOR (V2DFmode, gen_rtvec (2, biases, x));
10206   biases = validize_mem (force_const_mem (V2DFmode, biases));
10207   emit_insn (gen_subv2df3 (fp_xmm, fp_xmm, biases));
10208
10209   /* Add the upper and lower DFmode values together.  */
10210   if (TARGET_SSE3)
10211     emit_insn (gen_sse3_haddv2df3 (fp_xmm, fp_xmm, fp_xmm));
10212   else
10213     {
10214       x = copy_to_mode_reg (V2DFmode, fp_xmm);
10215       emit_insn (gen_sse2_unpckhpd (fp_xmm, fp_xmm, fp_xmm));
10216       emit_insn (gen_addv2df3 (fp_xmm, fp_xmm, x));
10217     }
10218
10219   ix86_expand_vector_extract (false, target, fp_xmm, 0);
10220 }
10221
10222 /* Convert an unsigned SImode value into a DFmode.  Only currently used
10223    for SSE, but applicable anywhere.  */
10224
10225 void
10226 ix86_expand_convert_uns_sidf_sse (rtx target, rtx input)
10227 {
10228   REAL_VALUE_TYPE TWO31r;
10229   rtx x, fp;
10230
10231   x = expand_simple_binop (SImode, PLUS, input, GEN_INT (-2147483647 - 1),
10232                            NULL, 1, OPTAB_DIRECT);
10233
10234   fp = gen_reg_rtx (DFmode);
10235   emit_insn (gen_floatsidf2 (fp, x));
10236
10237   real_ldexp (&TWO31r, &dconst1, 31);
10238   x = const_double_from_real_value (TWO31r, DFmode);
10239
10240   x = expand_simple_binop (DFmode, PLUS, fp, x, target, 0, OPTAB_DIRECT);
10241   if (x != target)
10242     emit_move_insn (target, x);
10243 }
10244
10245 /* Convert a signed DImode value into a DFmode.  Only used for SSE in
10246    32-bit mode; otherwise we have a direct convert instruction.  */
10247
10248 void
10249 ix86_expand_convert_sign_didf_sse (rtx target, rtx input)
10250 {
10251   REAL_VALUE_TYPE TWO32r;
10252   rtx fp_lo, fp_hi, x;
10253   
10254   fp_lo = gen_reg_rtx (DFmode);
10255   fp_hi = gen_reg_rtx (DFmode);
10256
10257   emit_insn (gen_floatsidf2 (fp_hi, gen_highpart (SImode, input)));
10258
10259   real_ldexp (&TWO32r, &dconst1, 32);
10260   x = const_double_from_real_value (TWO32r, DFmode);
10261   fp_hi = expand_simple_binop (DFmode, MULT, fp_hi, x, fp_hi, 0, OPTAB_DIRECT);
10262
10263   ix86_expand_convert_uns_sidf_sse (fp_lo, gen_lowpart (SImode, input));
10264
10265   x = expand_simple_binop (DFmode, PLUS, fp_hi, fp_lo, target,
10266                            0, OPTAB_DIRECT);
10267   if (x != target)
10268     emit_move_insn (target, x);
10269 }
10270
10271 /* Convert an unsigned SImode value into a SFmode, using only SSE.
10272    For x86_32, -mfpmath=sse, !optimize_size only.  */
10273 void
10274 ix86_expand_convert_uns_sisf_sse (rtx target, rtx input)
10275 {
10276   REAL_VALUE_TYPE ONE16r;
10277   rtx fp_hi, fp_lo, int_hi, int_lo, x;
10278
10279   real_ldexp (&ONE16r, &dconst1, 16);
10280   x = const_double_from_real_value (ONE16r, SFmode);
10281   int_lo = expand_simple_binop (SImode, AND, input, GEN_INT(0xffff),
10282                                       NULL, 0, OPTAB_DIRECT);
10283   int_hi = expand_simple_binop (SImode, LSHIFTRT, input, GEN_INT(16),
10284                                       NULL, 0, OPTAB_DIRECT);
10285   fp_hi = gen_reg_rtx (SFmode);
10286   fp_lo = gen_reg_rtx (SFmode);
10287   emit_insn (gen_floatsisf2 (fp_hi, int_hi));
10288   emit_insn (gen_floatsisf2 (fp_lo, int_lo));
10289   fp_hi = expand_simple_binop (SFmode, MULT, fp_hi, x, fp_hi,
10290                                0, OPTAB_DIRECT);
10291   fp_hi = expand_simple_binop (SFmode, PLUS, fp_hi, fp_lo, target,
10292                                0, OPTAB_DIRECT);
10293   if (!rtx_equal_p (target, fp_hi))
10294     emit_move_insn (target, fp_hi);
10295 }
10296
10297 /* A subroutine of ix86_build_signbit_mask_vector.  If VECT is true,
10298    then replicate the value for all elements of the vector
10299    register.  */
10300
10301 rtx
10302 ix86_build_const_vector (enum machine_mode mode, bool vect, rtx value)
10303 {
10304   rtvec v;
10305   switch (mode)
10306     {
10307     case SFmode:
10308       if (vect)
10309         v = gen_rtvec (4, value, value, value, value);
10310       else
10311         v = gen_rtvec (4, value, CONST0_RTX (SFmode),
10312                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10313       return gen_rtx_CONST_VECTOR (V4SFmode, v);
10314
10315     case DFmode:
10316       if (vect)
10317         v = gen_rtvec (2, value, value);
10318       else
10319         v = gen_rtvec (2, value, CONST0_RTX (DFmode));
10320       return gen_rtx_CONST_VECTOR (V2DFmode, v);
10321
10322     default:
10323       gcc_unreachable ();
10324     }
10325 }
10326
10327 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
10328    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
10329    true, then replicate the mask for all elements of the vector register.
10330    If INVERT is true, then create a mask excluding the sign bit.  */
10331
10332 rtx
10333 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
10334 {
10335   enum machine_mode vec_mode;
10336   HOST_WIDE_INT hi, lo;
10337   int shift = 63;
10338   rtx v;
10339   rtx mask;
10340
10341   /* Find the sign bit, sign extended to 2*HWI.  */
10342   if (mode == SFmode)
10343     lo = 0x80000000, hi = lo < 0;
10344   else if (HOST_BITS_PER_WIDE_INT >= 64)
10345     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
10346   else
10347     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
10348
10349   if (invert)
10350     lo = ~lo, hi = ~hi;
10351
10352   /* Force this value into the low part of a fp vector constant.  */
10353   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
10354   mask = gen_lowpart (mode, mask);
10355
10356   v = ix86_build_const_vector (mode, vect, mask);
10357   vec_mode = (mode == SFmode) ? V4SFmode : V2DFmode;
10358   return force_reg (vec_mode, v);
10359 }
10360
10361 /* Generate code for floating point ABS or NEG.  */
10362
10363 void
10364 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
10365                                 rtx operands[])
10366 {
10367   rtx mask, set, use, clob, dst, src;
10368   bool matching_memory;
10369   bool use_sse = false;
10370   bool vector_mode = VECTOR_MODE_P (mode);
10371   enum machine_mode elt_mode = mode;
10372
10373   if (vector_mode)
10374     {
10375       elt_mode = GET_MODE_INNER (mode);
10376       use_sse = true;
10377     }
10378   else if (TARGET_SSE_MATH)
10379     use_sse = SSE_FLOAT_MODE_P (mode);
10380
10381   /* NEG and ABS performed with SSE use bitwise mask operations.
10382      Create the appropriate mask now.  */
10383   if (use_sse)
10384     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
10385   else
10386     mask = NULL_RTX;
10387
10388   dst = operands[0];
10389   src = operands[1];
10390
10391   /* If the destination is memory, and we don't have matching source
10392      operands or we're using the x87, do things in registers.  */
10393   matching_memory = false;
10394   if (MEM_P (dst))
10395     {
10396       if (use_sse && rtx_equal_p (dst, src))
10397         matching_memory = true;
10398       else
10399         dst = gen_reg_rtx (mode);
10400     }
10401   if (MEM_P (src) && !matching_memory)
10402     src = force_reg (mode, src);
10403
10404   if (vector_mode)
10405     {
10406       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
10407       set = gen_rtx_SET (VOIDmode, dst, set);
10408       emit_insn (set);
10409     }
10410   else
10411     {
10412       set = gen_rtx_fmt_e (code, mode, src);
10413       set = gen_rtx_SET (VOIDmode, dst, set);
10414       if (mask)
10415         {
10416           use = gen_rtx_USE (VOIDmode, mask);
10417           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
10418           emit_insn (gen_rtx_PARALLEL (VOIDmode,
10419                                        gen_rtvec (3, set, use, clob)));
10420         }
10421       else
10422         emit_insn (set);
10423     }
10424
10425   if (dst != operands[0])
10426     emit_move_insn (operands[0], dst);
10427 }
10428
10429 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
10430
10431 void
10432 ix86_expand_copysign (rtx operands[])
10433 {
10434   enum machine_mode mode, vmode;
10435   rtx dest, op0, op1, mask, nmask;
10436
10437   dest = operands[0];
10438   op0 = operands[1];
10439   op1 = operands[2];
10440
10441   mode = GET_MODE (dest);
10442   vmode = mode == SFmode ? V4SFmode : V2DFmode;
10443
10444   if (GET_CODE (op0) == CONST_DOUBLE)
10445     {
10446       rtvec v;
10447
10448       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
10449         op0 = simplify_unary_operation (ABS, mode, op0, mode);
10450
10451       if (op0 == CONST0_RTX (mode))
10452         op0 = CONST0_RTX (vmode);
10453       else
10454         {
10455           if (mode == SFmode)
10456             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
10457                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
10458           else
10459             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
10460           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
10461         }
10462
10463       mask = ix86_build_signbit_mask (mode, 0, 0);
10464
10465       if (mode == SFmode)
10466         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
10467       else
10468         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
10469     }
10470   else
10471     {
10472       nmask = ix86_build_signbit_mask (mode, 0, 1);
10473       mask = ix86_build_signbit_mask (mode, 0, 0);
10474
10475       if (mode == SFmode)
10476         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
10477       else
10478         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
10479     }
10480 }
10481
10482 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
10483    be a constant, and so has already been expanded into a vector constant.  */
10484
10485 void
10486 ix86_split_copysign_const (rtx operands[])
10487 {
10488   enum machine_mode mode, vmode;
10489   rtx dest, op0, op1, mask, x;
10490
10491   dest = operands[0];
10492   op0 = operands[1];
10493   op1 = operands[2];
10494   mask = operands[3];
10495
10496   mode = GET_MODE (dest);
10497   vmode = GET_MODE (mask);
10498
10499   dest = simplify_gen_subreg (vmode, dest, mode, 0);
10500   x = gen_rtx_AND (vmode, dest, mask);
10501   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10502
10503   if (op0 != CONST0_RTX (vmode))
10504     {
10505       x = gen_rtx_IOR (vmode, dest, op0);
10506       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10507     }
10508 }
10509
10510 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
10511    so we have to do two masks.  */
10512
10513 void
10514 ix86_split_copysign_var (rtx operands[])
10515 {
10516   enum machine_mode mode, vmode;
10517   rtx dest, scratch, op0, op1, mask, nmask, x;
10518
10519   dest = operands[0];
10520   scratch = operands[1];
10521   op0 = operands[2];
10522   op1 = operands[3];
10523   nmask = operands[4];
10524   mask = operands[5];
10525
10526   mode = GET_MODE (dest);
10527   vmode = GET_MODE (mask);
10528
10529   if (rtx_equal_p (op0, op1))
10530     {
10531       /* Shouldn't happen often (it's useless, obviously), but when it does
10532          we'd generate incorrect code if we continue below.  */
10533       emit_move_insn (dest, op0);
10534       return;
10535     }
10536
10537   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
10538     {
10539       gcc_assert (REGNO (op1) == REGNO (scratch));
10540
10541       x = gen_rtx_AND (vmode, scratch, mask);
10542       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10543
10544       dest = mask;
10545       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10546       x = gen_rtx_NOT (vmode, dest);
10547       x = gen_rtx_AND (vmode, x, op0);
10548       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10549     }
10550   else
10551     {
10552       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
10553         {
10554           x = gen_rtx_AND (vmode, scratch, mask);
10555         }
10556       else                                              /* alternative 2,4 */
10557         {
10558           gcc_assert (REGNO (mask) == REGNO (scratch));
10559           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
10560           x = gen_rtx_AND (vmode, scratch, op1);
10561         }
10562       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
10563
10564       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
10565         {
10566           dest = simplify_gen_subreg (vmode, op0, mode, 0);
10567           x = gen_rtx_AND (vmode, dest, nmask);
10568         }
10569       else                                              /* alternative 3,4 */
10570         {
10571           gcc_assert (REGNO (nmask) == REGNO (dest));
10572           dest = nmask;
10573           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
10574           x = gen_rtx_AND (vmode, dest, op0);
10575         }
10576       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10577     }
10578
10579   x = gen_rtx_IOR (vmode, dest, scratch);
10580   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10581 }
10582
10583 /* Return TRUE or FALSE depending on whether the first SET in INSN
10584    has source and destination with matching CC modes, and that the
10585    CC mode is at least as constrained as REQ_MODE.  */
10586
10587 int
10588 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
10589 {
10590   rtx set;
10591   enum machine_mode set_mode;
10592
10593   set = PATTERN (insn);
10594   if (GET_CODE (set) == PARALLEL)
10595     set = XVECEXP (set, 0, 0);
10596   gcc_assert (GET_CODE (set) == SET);
10597   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
10598
10599   set_mode = GET_MODE (SET_DEST (set));
10600   switch (set_mode)
10601     {
10602     case CCNOmode:
10603       if (req_mode != CCNOmode
10604           && (req_mode != CCmode
10605               || XEXP (SET_SRC (set), 1) != const0_rtx))
10606         return 0;
10607       break;
10608     case CCmode:
10609       if (req_mode == CCGCmode)
10610         return 0;
10611       /* FALLTHRU */
10612     case CCGCmode:
10613       if (req_mode == CCGOCmode || req_mode == CCNOmode)
10614         return 0;
10615       /* FALLTHRU */
10616     case CCGOCmode:
10617       if (req_mode == CCZmode)
10618         return 0;
10619       /* FALLTHRU */
10620     case CCZmode:
10621       break;
10622
10623     default:
10624       gcc_unreachable ();
10625     }
10626
10627   return (GET_MODE (SET_SRC (set)) == set_mode);
10628 }
10629
10630 /* Generate insn patterns to do an integer compare of OPERANDS.  */
10631
10632 static rtx
10633 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
10634 {
10635   enum machine_mode cmpmode;
10636   rtx tmp, flags;
10637
10638   cmpmode = SELECT_CC_MODE (code, op0, op1);
10639   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
10640
10641   /* This is very simple, but making the interface the same as in the
10642      FP case makes the rest of the code easier.  */
10643   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
10644   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
10645
10646   /* Return the test that should be put into the flags user, i.e.
10647      the bcc, scc, or cmov instruction.  */
10648   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
10649 }
10650
10651 /* Figure out whether to use ordered or unordered fp comparisons.
10652    Return the appropriate mode to use.  */
10653
10654 enum machine_mode
10655 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
10656 {
10657   /* ??? In order to make all comparisons reversible, we do all comparisons
10658      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
10659      all forms trapping and nontrapping comparisons, we can make inequality
10660      comparisons trapping again, since it results in better code when using
10661      FCOM based compares.  */
10662   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
10663 }
10664
10665 enum machine_mode
10666 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
10667 {
10668   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10669     return ix86_fp_compare_mode (code);
10670   switch (code)
10671     {
10672       /* Only zero flag is needed.  */
10673     case EQ:                    /* ZF=0 */
10674     case NE:                    /* ZF!=0 */
10675       return CCZmode;
10676       /* Codes needing carry flag.  */
10677     case GEU:                   /* CF=0 */
10678     case GTU:                   /* CF=0 & ZF=0 */
10679     case LTU:                   /* CF=1 */
10680     case LEU:                   /* CF=1 | ZF=1 */
10681       return CCmode;
10682       /* Codes possibly doable only with sign flag when
10683          comparing against zero.  */
10684     case GE:                    /* SF=OF   or   SF=0 */
10685     case LT:                    /* SF<>OF  or   SF=1 */
10686       if (op1 == const0_rtx)
10687         return CCGOCmode;
10688       else
10689         /* For other cases Carry flag is not required.  */
10690         return CCGCmode;
10691       /* Codes doable only with sign flag when comparing
10692          against zero, but we miss jump instruction for it
10693          so we need to use relational tests against overflow
10694          that thus needs to be zero.  */
10695     case GT:                    /* ZF=0 & SF=OF */
10696     case LE:                    /* ZF=1 | SF<>OF */
10697       if (op1 == const0_rtx)
10698         return CCNOmode;
10699       else
10700         return CCGCmode;
10701       /* strcmp pattern do (use flags) and combine may ask us for proper
10702          mode.  */
10703     case USE:
10704       return CCmode;
10705     default:
10706       gcc_unreachable ();
10707     }
10708 }
10709
10710 /* Return the fixed registers used for condition codes.  */
10711
10712 static bool
10713 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
10714 {
10715   *p1 = FLAGS_REG;
10716   *p2 = FPSR_REG;
10717   return true;
10718 }
10719
10720 /* If two condition code modes are compatible, return a condition code
10721    mode which is compatible with both.  Otherwise, return
10722    VOIDmode.  */
10723
10724 static enum machine_mode
10725 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
10726 {
10727   if (m1 == m2)
10728     return m1;
10729
10730   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
10731     return VOIDmode;
10732
10733   if ((m1 == CCGCmode && m2 == CCGOCmode)
10734       || (m1 == CCGOCmode && m2 == CCGCmode))
10735     return CCGCmode;
10736
10737   switch (m1)
10738     {
10739     default:
10740       gcc_unreachable ();
10741
10742     case CCmode:
10743     case CCGCmode:
10744     case CCGOCmode:
10745     case CCNOmode:
10746     case CCZmode:
10747       switch (m2)
10748         {
10749         default:
10750           return VOIDmode;
10751
10752         case CCmode:
10753         case CCGCmode:
10754         case CCGOCmode:
10755         case CCNOmode:
10756         case CCZmode:
10757           return CCmode;
10758         }
10759
10760     case CCFPmode:
10761     case CCFPUmode:
10762       /* These are only compatible with themselves, which we already
10763          checked above.  */
10764       return VOIDmode;
10765     }
10766 }
10767
10768 /* Split comparison code CODE into comparisons we can do using branch
10769    instructions.  BYPASS_CODE is comparison code for branch that will
10770    branch around FIRST_CODE and SECOND_CODE.  If some of branches
10771    is not required, set value to UNKNOWN.
10772    We never require more than two branches.  */
10773
10774 void
10775 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
10776                           enum rtx_code *first_code,
10777                           enum rtx_code *second_code)
10778 {
10779   *first_code = code;
10780   *bypass_code = UNKNOWN;
10781   *second_code = UNKNOWN;
10782
10783   /* The fcomi comparison sets flags as follows:
10784
10785      cmp    ZF PF CF
10786      >      0  0  0
10787      <      0  0  1
10788      =      1  0  0
10789      un     1  1  1 */
10790
10791   switch (code)
10792     {
10793     case GT:                    /* GTU - CF=0 & ZF=0 */
10794     case GE:                    /* GEU - CF=0 */
10795     case ORDERED:               /* PF=0 */
10796     case UNORDERED:             /* PF=1 */
10797     case UNEQ:                  /* EQ - ZF=1 */
10798     case UNLT:                  /* LTU - CF=1 */
10799     case UNLE:                  /* LEU - CF=1 | ZF=1 */
10800     case LTGT:                  /* EQ - ZF=0 */
10801       break;
10802     case LT:                    /* LTU - CF=1 - fails on unordered */
10803       *first_code = UNLT;
10804       *bypass_code = UNORDERED;
10805       break;
10806     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
10807       *first_code = UNLE;
10808       *bypass_code = UNORDERED;
10809       break;
10810     case EQ:                    /* EQ - ZF=1 - fails on unordered */
10811       *first_code = UNEQ;
10812       *bypass_code = UNORDERED;
10813       break;
10814     case NE:                    /* NE - ZF=0 - fails on unordered */
10815       *first_code = LTGT;
10816       *second_code = UNORDERED;
10817       break;
10818     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10819       *first_code = GE;
10820       *second_code = UNORDERED;
10821       break;
10822     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10823       *first_code = GT;
10824       *second_code = UNORDERED;
10825       break;
10826     default:
10827       gcc_unreachable ();
10828     }
10829   if (!TARGET_IEEE_FP)
10830     {
10831       *second_code = UNKNOWN;
10832       *bypass_code = UNKNOWN;
10833     }
10834 }
10835
10836 /* Return cost of comparison done fcom + arithmetics operations on AX.
10837    All following functions do use number of instructions as a cost metrics.
10838    In future this should be tweaked to compute bytes for optimize_size and
10839    take into account performance of various instructions on various CPUs.  */
10840 static int
10841 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10842 {
10843   if (!TARGET_IEEE_FP)
10844     return 4;
10845   /* The cost of code output by ix86_expand_fp_compare.  */
10846   switch (code)
10847     {
10848     case UNLE:
10849     case UNLT:
10850     case LTGT:
10851     case GT:
10852     case GE:
10853     case UNORDERED:
10854     case ORDERED:
10855     case UNEQ:
10856       return 4;
10857       break;
10858     case LT:
10859     case NE:
10860     case EQ:
10861     case UNGE:
10862       return 5;
10863       break;
10864     case LE:
10865     case UNGT:
10866       return 6;
10867       break;
10868     default:
10869       gcc_unreachable ();
10870     }
10871 }
10872
10873 /* Return cost of comparison done using fcomi operation.
10874    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10875 static int
10876 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10877 {
10878   enum rtx_code bypass_code, first_code, second_code;
10879   /* Return arbitrarily high cost when instruction is not supported - this
10880      prevents gcc from using it.  */
10881   if (!TARGET_CMOVE)
10882     return 1024;
10883   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10884   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10885 }
10886
10887 /* Return cost of comparison done using sahf operation.
10888    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10889 static int
10890 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10891 {
10892   enum rtx_code bypass_code, first_code, second_code;
10893   /* Return arbitrarily high cost when instruction is not preferred - this
10894      avoids gcc from using it.  */
10895   if (!(TARGET_SAHF && (TARGET_USE_SAHF || optimize_size)))
10896     return 1024;
10897   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10898   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10899 }
10900
10901 /* Compute cost of the comparison done using any method.
10902    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10903 static int
10904 ix86_fp_comparison_cost (enum rtx_code code)
10905 {
10906   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10907   int min;
10908
10909   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10910   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10911
10912   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10913   if (min > sahf_cost)
10914     min = sahf_cost;
10915   if (min > fcomi_cost)
10916     min = fcomi_cost;
10917   return min;
10918 }
10919
10920 /* Return true if we should use an FCOMI instruction for this
10921    fp comparison.  */
10922
10923 int
10924 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
10925 {
10926   enum rtx_code swapped_code = swap_condition (code);
10927
10928   return ((ix86_fp_comparison_cost (code)
10929            == ix86_fp_comparison_fcomi_cost (code))
10930           || (ix86_fp_comparison_cost (swapped_code)
10931               == ix86_fp_comparison_fcomi_cost (swapped_code)));
10932 }
10933
10934 /* Swap, force into registers, or otherwise massage the two operands
10935    to a fp comparison.  The operands are updated in place; the new
10936    comparison code is returned.  */
10937
10938 static enum rtx_code
10939 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
10940 {
10941   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
10942   rtx op0 = *pop0, op1 = *pop1;
10943   enum machine_mode op_mode = GET_MODE (op0);
10944   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
10945
10946   /* All of the unordered compare instructions only work on registers.
10947      The same is true of the fcomi compare instructions.  The XFmode
10948      compare instructions require registers except when comparing
10949      against zero or when converting operand 1 from fixed point to
10950      floating point.  */
10951
10952   if (!is_sse
10953       && (fpcmp_mode == CCFPUmode
10954           || (op_mode == XFmode
10955               && ! (standard_80387_constant_p (op0) == 1
10956                     || standard_80387_constant_p (op1) == 1)
10957               && GET_CODE (op1) != FLOAT)
10958           || ix86_use_fcomi_compare (code)))
10959     {
10960       op0 = force_reg (op_mode, op0);
10961       op1 = force_reg (op_mode, op1);
10962     }
10963   else
10964     {
10965       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
10966          things around if they appear profitable, otherwise force op0
10967          into a register.  */
10968
10969       if (standard_80387_constant_p (op0) == 0
10970           || (MEM_P (op0)
10971               && ! (standard_80387_constant_p (op1) == 0
10972                     || MEM_P (op1))))
10973         {
10974           rtx tmp;
10975           tmp = op0, op0 = op1, op1 = tmp;
10976           code = swap_condition (code);
10977         }
10978
10979       if (!REG_P (op0))
10980         op0 = force_reg (op_mode, op0);
10981
10982       if (CONSTANT_P (op1))
10983         {
10984           int tmp = standard_80387_constant_p (op1);
10985           if (tmp == 0)
10986             op1 = validize_mem (force_const_mem (op_mode, op1));
10987           else if (tmp == 1)
10988             {
10989               if (TARGET_CMOVE)
10990                 op1 = force_reg (op_mode, op1);
10991             }
10992           else
10993             op1 = force_reg (op_mode, op1);
10994         }
10995     }
10996
10997   /* Try to rearrange the comparison to make it cheaper.  */
10998   if (ix86_fp_comparison_cost (code)
10999       > ix86_fp_comparison_cost (swap_condition (code))
11000       && (REG_P (op1) || !no_new_pseudos))
11001     {
11002       rtx tmp;
11003       tmp = op0, op0 = op1, op1 = tmp;
11004       code = swap_condition (code);
11005       if (!REG_P (op0))
11006         op0 = force_reg (op_mode, op0);
11007     }
11008
11009   *pop0 = op0;
11010   *pop1 = op1;
11011   return code;
11012 }
11013
11014 /* Convert comparison codes we use to represent FP comparison to integer
11015    code that will result in proper branch.  Return UNKNOWN if no such code
11016    is available.  */
11017
11018 enum rtx_code
11019 ix86_fp_compare_code_to_integer (enum rtx_code code)
11020 {
11021   switch (code)
11022     {
11023     case GT:
11024       return GTU;
11025     case GE:
11026       return GEU;
11027     case ORDERED:
11028     case UNORDERED:
11029       return code;
11030       break;
11031     case UNEQ:
11032       return EQ;
11033       break;
11034     case UNLT:
11035       return LTU;
11036       break;
11037     case UNLE:
11038       return LEU;
11039       break;
11040     case LTGT:
11041       return NE;
11042       break;
11043     default:
11044       return UNKNOWN;
11045     }
11046 }
11047
11048 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
11049
11050 static rtx
11051 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
11052                         rtx *second_test, rtx *bypass_test)
11053 {
11054   enum machine_mode fpcmp_mode, intcmp_mode;
11055   rtx tmp, tmp2;
11056   int cost = ix86_fp_comparison_cost (code);
11057   enum rtx_code bypass_code, first_code, second_code;
11058
11059   fpcmp_mode = ix86_fp_compare_mode (code);
11060   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
11061
11062   if (second_test)
11063     *second_test = NULL_RTX;
11064   if (bypass_test)
11065     *bypass_test = NULL_RTX;
11066
11067   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11068
11069   /* Do fcomi/sahf based test when profitable.  */
11070   if ((TARGET_CMOVE || TARGET_SAHF)
11071       && (bypass_code == UNKNOWN || bypass_test)
11072       && (second_code == UNKNOWN || second_test)
11073       && ix86_fp_comparison_arithmetics_cost (code) > cost)
11074     {
11075       if (TARGET_CMOVE)
11076         {
11077           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11078           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
11079                              tmp);
11080           emit_insn (tmp);
11081         }
11082       else
11083         {
11084           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11085           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
11086           if (!scratch)
11087             scratch = gen_reg_rtx (HImode);
11088           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
11089           emit_insn (gen_x86_sahf_1 (scratch));
11090         }
11091
11092       /* The FP codes work out to act like unsigned.  */
11093       intcmp_mode = fpcmp_mode;
11094       code = first_code;
11095       if (bypass_code != UNKNOWN)
11096         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
11097                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11098                                        const0_rtx);
11099       if (second_code != UNKNOWN)
11100         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
11101                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
11102                                        const0_rtx);
11103     }
11104   else
11105     {
11106       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
11107       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
11108       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
11109       if (!scratch)
11110         scratch = gen_reg_rtx (HImode);
11111       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
11112
11113       /* In the unordered case, we have to check C2 for NaN's, which
11114          doesn't happen to work out to anything nice combination-wise.
11115          So do some bit twiddling on the value we've got in AH to come
11116          up with an appropriate set of condition codes.  */
11117
11118       intcmp_mode = CCNOmode;
11119       switch (code)
11120         {
11121         case GT:
11122         case UNGT:
11123           if (code == GT || !TARGET_IEEE_FP)
11124             {
11125               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11126               code = EQ;
11127             }
11128           else
11129             {
11130               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11131               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11132               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
11133               intcmp_mode = CCmode;
11134               code = GEU;
11135             }
11136           break;
11137         case LT:
11138         case UNLT:
11139           if (code == LT && TARGET_IEEE_FP)
11140             {
11141               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11142               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
11143               intcmp_mode = CCmode;
11144               code = EQ;
11145             }
11146           else
11147             {
11148               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
11149               code = NE;
11150             }
11151           break;
11152         case GE:
11153         case UNGE:
11154           if (code == GE || !TARGET_IEEE_FP)
11155             {
11156               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
11157               code = EQ;
11158             }
11159           else
11160             {
11161               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11162               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11163                                              GEN_INT (0x01)));
11164               code = NE;
11165             }
11166           break;
11167         case LE:
11168         case UNLE:
11169           if (code == LE && TARGET_IEEE_FP)
11170             {
11171               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11172               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
11173               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11174               intcmp_mode = CCmode;
11175               code = LTU;
11176             }
11177           else
11178             {
11179               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
11180               code = NE;
11181             }
11182           break;
11183         case EQ:
11184         case UNEQ:
11185           if (code == EQ && TARGET_IEEE_FP)
11186             {
11187               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11188               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
11189               intcmp_mode = CCmode;
11190               code = EQ;
11191             }
11192           else
11193             {
11194               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11195               code = NE;
11196               break;
11197             }
11198           break;
11199         case NE:
11200         case LTGT:
11201           if (code == NE && TARGET_IEEE_FP)
11202             {
11203               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
11204               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
11205                                              GEN_INT (0x40)));
11206               code = NE;
11207             }
11208           else
11209             {
11210               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
11211               code = EQ;
11212             }
11213           break;
11214
11215         case UNORDERED:
11216           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11217           code = NE;
11218           break;
11219         case ORDERED:
11220           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
11221           code = EQ;
11222           break;
11223
11224         default:
11225           gcc_unreachable ();
11226         }
11227     }
11228
11229   /* Return the test that should be put into the flags user, i.e.
11230      the bcc, scc, or cmov instruction.  */
11231   return gen_rtx_fmt_ee (code, VOIDmode,
11232                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
11233                          const0_rtx);
11234 }
11235
11236 rtx
11237 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
11238 {
11239   rtx op0, op1, ret;
11240   op0 = ix86_compare_op0;
11241   op1 = ix86_compare_op1;
11242
11243   if (second_test)
11244     *second_test = NULL_RTX;
11245   if (bypass_test)
11246     *bypass_test = NULL_RTX;
11247
11248   if (ix86_compare_emitted)
11249     {
11250       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
11251       ix86_compare_emitted = NULL_RTX;
11252     }
11253   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
11254     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11255                                   second_test, bypass_test);
11256   else
11257     ret = ix86_expand_int_compare (code, op0, op1);
11258
11259   return ret;
11260 }
11261
11262 /* Return true if the CODE will result in nontrivial jump sequence.  */
11263 bool
11264 ix86_fp_jump_nontrivial_p (enum rtx_code code)
11265 {
11266   enum rtx_code bypass_code, first_code, second_code;
11267   if (!TARGET_CMOVE)
11268     return true;
11269   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11270   return bypass_code != UNKNOWN || second_code != UNKNOWN;
11271 }
11272
11273 void
11274 ix86_expand_branch (enum rtx_code code, rtx label)
11275 {
11276   rtx tmp;
11277
11278   /* If we have emitted a compare insn, go straight to simple.
11279      ix86_expand_compare won't emit anything if ix86_compare_emitted
11280      is non NULL.  */
11281   if (ix86_compare_emitted)
11282     goto simple;
11283
11284   switch (GET_MODE (ix86_compare_op0))
11285     {
11286     case QImode:
11287     case HImode:
11288     case SImode:
11289       simple:
11290       tmp = ix86_expand_compare (code, NULL, NULL);
11291       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11292                                   gen_rtx_LABEL_REF (VOIDmode, label),
11293                                   pc_rtx);
11294       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
11295       return;
11296
11297     case SFmode:
11298     case DFmode:
11299     case XFmode:
11300       {
11301         rtvec vec;
11302         int use_fcomi;
11303         enum rtx_code bypass_code, first_code, second_code;
11304
11305         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
11306                                              &ix86_compare_op1);
11307
11308         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
11309
11310         /* Check whether we will use the natural sequence with one jump.  If
11311            so, we can expand jump early.  Otherwise delay expansion by
11312            creating compound insn to not confuse optimizers.  */
11313         if (bypass_code == UNKNOWN && second_code == UNKNOWN
11314             && TARGET_CMOVE)
11315           {
11316             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
11317                                   gen_rtx_LABEL_REF (VOIDmode, label),
11318                                   pc_rtx, NULL_RTX, NULL_RTX);
11319           }
11320         else
11321           {
11322             tmp = gen_rtx_fmt_ee (code, VOIDmode,
11323                                   ix86_compare_op0, ix86_compare_op1);
11324             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
11325                                         gen_rtx_LABEL_REF (VOIDmode, label),
11326                                         pc_rtx);
11327             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
11328
11329             use_fcomi = ix86_use_fcomi_compare (code);
11330             vec = rtvec_alloc (3 + !use_fcomi);
11331             RTVEC_ELT (vec, 0) = tmp;
11332             RTVEC_ELT (vec, 1)
11333               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
11334             RTVEC_ELT (vec, 2)
11335               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
11336             if (! use_fcomi)
11337               RTVEC_ELT (vec, 3)
11338                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
11339
11340             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
11341           }
11342         return;
11343       }
11344
11345     case DImode:
11346       if (TARGET_64BIT)
11347         goto simple;
11348     case TImode:
11349       /* Expand DImode branch into multiple compare+branch.  */
11350       {
11351         rtx lo[2], hi[2], label2;
11352         enum rtx_code code1, code2, code3;
11353         enum machine_mode submode;
11354
11355         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
11356           {
11357             tmp = ix86_compare_op0;
11358             ix86_compare_op0 = ix86_compare_op1;
11359             ix86_compare_op1 = tmp;
11360             code = swap_condition (code);
11361           }
11362         if (GET_MODE (ix86_compare_op0) == DImode)
11363           {
11364             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
11365             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
11366             submode = SImode;
11367           }
11368         else
11369           {
11370             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
11371             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
11372             submode = DImode;
11373           }
11374
11375         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
11376            avoid two branches.  This costs one extra insn, so disable when
11377            optimizing for size.  */
11378
11379         if ((code == EQ || code == NE)
11380             && (!optimize_size
11381                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
11382           {
11383             rtx xor0, xor1;
11384
11385             xor1 = hi[0];
11386             if (hi[1] != const0_rtx)
11387               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
11388                                    NULL_RTX, 0, OPTAB_WIDEN);
11389
11390             xor0 = lo[0];
11391             if (lo[1] != const0_rtx)
11392               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
11393                                    NULL_RTX, 0, OPTAB_WIDEN);
11394
11395             tmp = expand_binop (submode, ior_optab, xor1, xor0,
11396                                 NULL_RTX, 0, OPTAB_WIDEN);
11397
11398             ix86_compare_op0 = tmp;
11399             ix86_compare_op1 = const0_rtx;
11400             ix86_expand_branch (code, label);
11401             return;
11402           }
11403
11404         /* Otherwise, if we are doing less-than or greater-or-equal-than,
11405            op1 is a constant and the low word is zero, then we can just
11406            examine the high word.  */
11407
11408         if (CONST_INT_P (hi[1]) && lo[1] == const0_rtx)
11409           switch (code)
11410             {
11411             case LT: case LTU: case GE: case GEU:
11412               ix86_compare_op0 = hi[0];
11413               ix86_compare_op1 = hi[1];
11414               ix86_expand_branch (code, label);
11415               return;
11416             default:
11417               break;
11418             }
11419
11420         /* Otherwise, we need two or three jumps.  */
11421
11422         label2 = gen_label_rtx ();
11423
11424         code1 = code;
11425         code2 = swap_condition (code);
11426         code3 = unsigned_condition (code);
11427
11428         switch (code)
11429           {
11430           case LT: case GT: case LTU: case GTU:
11431             break;
11432
11433           case LE:   code1 = LT;  code2 = GT;  break;
11434           case GE:   code1 = GT;  code2 = LT;  break;
11435           case LEU:  code1 = LTU; code2 = GTU; break;
11436           case GEU:  code1 = GTU; code2 = LTU; break;
11437
11438           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
11439           case NE:   code2 = UNKNOWN; break;
11440
11441           default:
11442             gcc_unreachable ();
11443           }
11444
11445         /*
11446          * a < b =>
11447          *    if (hi(a) < hi(b)) goto true;
11448          *    if (hi(a) > hi(b)) goto false;
11449          *    if (lo(a) < lo(b)) goto true;
11450          *  false:
11451          */
11452
11453         ix86_compare_op0 = hi[0];
11454         ix86_compare_op1 = hi[1];
11455
11456         if (code1 != UNKNOWN)
11457           ix86_expand_branch (code1, label);
11458         if (code2 != UNKNOWN)
11459           ix86_expand_branch (code2, label2);
11460
11461         ix86_compare_op0 = lo[0];
11462         ix86_compare_op1 = lo[1];
11463         ix86_expand_branch (code3, label);
11464
11465         if (code2 != UNKNOWN)
11466           emit_label (label2);
11467         return;
11468       }
11469
11470     default:
11471       gcc_unreachable ();
11472     }
11473 }
11474
11475 /* Split branch based on floating point condition.  */
11476 void
11477 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
11478                       rtx target1, rtx target2, rtx tmp, rtx pushed)
11479 {
11480   rtx second, bypass;
11481   rtx label = NULL_RTX;
11482   rtx condition;
11483   int bypass_probability = -1, second_probability = -1, probability = -1;
11484   rtx i;
11485
11486   if (target2 != pc_rtx)
11487     {
11488       rtx tmp = target2;
11489       code = reverse_condition_maybe_unordered (code);
11490       target2 = target1;
11491       target1 = tmp;
11492     }
11493
11494   condition = ix86_expand_fp_compare (code, op1, op2,
11495                                       tmp, &second, &bypass);
11496
11497   /* Remove pushed operand from stack.  */
11498   if (pushed)
11499     ix86_free_from_memory (GET_MODE (pushed));
11500
11501   if (split_branch_probability >= 0)
11502     {
11503       /* Distribute the probabilities across the jumps.
11504          Assume the BYPASS and SECOND to be always test
11505          for UNORDERED.  */
11506       probability = split_branch_probability;
11507
11508       /* Value of 1 is low enough to make no need for probability
11509          to be updated.  Later we may run some experiments and see
11510          if unordered values are more frequent in practice.  */
11511       if (bypass)
11512         bypass_probability = 1;
11513       if (second)
11514         second_probability = 1;
11515     }
11516   if (bypass != NULL_RTX)
11517     {
11518       label = gen_label_rtx ();
11519       i = emit_jump_insn (gen_rtx_SET
11520                           (VOIDmode, pc_rtx,
11521                            gen_rtx_IF_THEN_ELSE (VOIDmode,
11522                                                  bypass,
11523                                                  gen_rtx_LABEL_REF (VOIDmode,
11524                                                                     label),
11525                                                  pc_rtx)));
11526       if (bypass_probability >= 0)
11527         REG_NOTES (i)
11528           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11529                                GEN_INT (bypass_probability),
11530                                REG_NOTES (i));
11531     }
11532   i = emit_jump_insn (gen_rtx_SET
11533                       (VOIDmode, pc_rtx,
11534                        gen_rtx_IF_THEN_ELSE (VOIDmode,
11535                                              condition, target1, target2)));
11536   if (probability >= 0)
11537     REG_NOTES (i)
11538       = gen_rtx_EXPR_LIST (REG_BR_PROB,
11539                            GEN_INT (probability),
11540                            REG_NOTES (i));
11541   if (second != NULL_RTX)
11542     {
11543       i = emit_jump_insn (gen_rtx_SET
11544                           (VOIDmode, pc_rtx,
11545                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
11546                                                  target2)));
11547       if (second_probability >= 0)
11548         REG_NOTES (i)
11549           = gen_rtx_EXPR_LIST (REG_BR_PROB,
11550                                GEN_INT (second_probability),
11551                                REG_NOTES (i));
11552     }
11553   if (label != NULL_RTX)
11554     emit_label (label);
11555 }
11556
11557 int
11558 ix86_expand_setcc (enum rtx_code code, rtx dest)
11559 {
11560   rtx ret, tmp, tmpreg, equiv;
11561   rtx second_test, bypass_test;
11562
11563   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
11564     return 0; /* FAIL */
11565
11566   gcc_assert (GET_MODE (dest) == QImode);
11567
11568   ret = ix86_expand_compare (code, &second_test, &bypass_test);
11569   PUT_MODE (ret, QImode);
11570
11571   tmp = dest;
11572   tmpreg = dest;
11573
11574   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
11575   if (bypass_test || second_test)
11576     {
11577       rtx test = second_test;
11578       int bypass = 0;
11579       rtx tmp2 = gen_reg_rtx (QImode);
11580       if (bypass_test)
11581         {
11582           gcc_assert (!second_test);
11583           test = bypass_test;
11584           bypass = 1;
11585           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
11586         }
11587       PUT_MODE (test, QImode);
11588       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
11589
11590       if (bypass)
11591         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
11592       else
11593         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
11594     }
11595
11596   /* Attach a REG_EQUAL note describing the comparison result.  */
11597   if (ix86_compare_op0 && ix86_compare_op1)
11598     {
11599       equiv = simplify_gen_relational (code, QImode,
11600                                        GET_MODE (ix86_compare_op0),
11601                                        ix86_compare_op0, ix86_compare_op1);
11602       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
11603     }
11604
11605   return 1; /* DONE */
11606 }
11607
11608 /* Expand comparison setting or clearing carry flag.  Return true when
11609    successful and set pop for the operation.  */
11610 static bool
11611 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
11612 {
11613   enum machine_mode mode =
11614     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
11615
11616   /* Do not handle DImode compares that go through special path.  Also we can't
11617      deal with FP compares yet.  This is possible to add.  */
11618   if (mode == (TARGET_64BIT ? TImode : DImode))
11619     return false;
11620   if (FLOAT_MODE_P (mode))
11621     {
11622       rtx second_test = NULL, bypass_test = NULL;
11623       rtx compare_op, compare_seq;
11624
11625       /* Shortcut:  following common codes never translate into carry flag compares.  */
11626       if (code == EQ || code == NE || code == UNEQ || code == LTGT
11627           || code == ORDERED || code == UNORDERED)
11628         return false;
11629
11630       /* These comparisons require zero flag; swap operands so they won't.  */
11631       if ((code == GT || code == UNLE || code == LE || code == UNGT)
11632           && !TARGET_IEEE_FP)
11633         {
11634           rtx tmp = op0;
11635           op0 = op1;
11636           op1 = tmp;
11637           code = swap_condition (code);
11638         }
11639
11640       /* Try to expand the comparison and verify that we end up with carry flag
11641          based comparison.  This is fails to be true only when we decide to expand
11642          comparison using arithmetic that is not too common scenario.  */
11643       start_sequence ();
11644       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
11645                                            &second_test, &bypass_test);
11646       compare_seq = get_insns ();
11647       end_sequence ();
11648
11649       if (second_test || bypass_test)
11650         return false;
11651       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11652           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11653         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
11654       else
11655         code = GET_CODE (compare_op);
11656       if (code != LTU && code != GEU)
11657         return false;
11658       emit_insn (compare_seq);
11659       *pop = compare_op;
11660       return true;
11661     }
11662   if (!INTEGRAL_MODE_P (mode))
11663     return false;
11664   switch (code)
11665     {
11666     case LTU:
11667     case GEU:
11668       break;
11669
11670     /* Convert a==0 into (unsigned)a<1.  */
11671     case EQ:
11672     case NE:
11673       if (op1 != const0_rtx)
11674         return false;
11675       op1 = const1_rtx;
11676       code = (code == EQ ? LTU : GEU);
11677       break;
11678
11679     /* Convert a>b into b<a or a>=b-1.  */
11680     case GTU:
11681     case LEU:
11682       if (CONST_INT_P (op1))
11683         {
11684           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
11685           /* Bail out on overflow.  We still can swap operands but that
11686              would force loading of the constant into register.  */
11687           if (op1 == const0_rtx
11688               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
11689             return false;
11690           code = (code == GTU ? GEU : LTU);
11691         }
11692       else
11693         {
11694           rtx tmp = op1;
11695           op1 = op0;
11696           op0 = tmp;
11697           code = (code == GTU ? LTU : GEU);
11698         }
11699       break;
11700
11701     /* Convert a>=0 into (unsigned)a<0x80000000.  */
11702     case LT:
11703     case GE:
11704       if (mode == DImode || op1 != const0_rtx)
11705         return false;
11706       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11707       code = (code == LT ? GEU : LTU);
11708       break;
11709     case LE:
11710     case GT:
11711       if (mode == DImode || op1 != constm1_rtx)
11712         return false;
11713       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
11714       code = (code == LE ? GEU : LTU);
11715       break;
11716
11717     default:
11718       return false;
11719     }
11720   /* Swapping operands may cause constant to appear as first operand.  */
11721   if (!nonimmediate_operand (op0, VOIDmode))
11722     {
11723       if (no_new_pseudos)
11724         return false;
11725       op0 = force_reg (mode, op0);
11726     }
11727   ix86_compare_op0 = op0;
11728   ix86_compare_op1 = op1;
11729   *pop = ix86_expand_compare (code, NULL, NULL);
11730   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
11731   return true;
11732 }
11733
11734 int
11735 ix86_expand_int_movcc (rtx operands[])
11736 {
11737   enum rtx_code code = GET_CODE (operands[1]), compare_code;
11738   rtx compare_seq, compare_op;
11739   rtx second_test, bypass_test;
11740   enum machine_mode mode = GET_MODE (operands[0]);
11741   bool sign_bit_compare_p = false;;
11742
11743   start_sequence ();
11744   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11745   compare_seq = get_insns ();
11746   end_sequence ();
11747
11748   compare_code = GET_CODE (compare_op);
11749
11750   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
11751       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
11752     sign_bit_compare_p = true;
11753
11754   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
11755      HImode insns, we'd be swallowed in word prefix ops.  */
11756
11757   if ((mode != HImode || TARGET_FAST_PREFIX)
11758       && (mode != (TARGET_64BIT ? TImode : DImode))
11759       && CONST_INT_P (operands[2])
11760       && CONST_INT_P (operands[3]))
11761     {
11762       rtx out = operands[0];
11763       HOST_WIDE_INT ct = INTVAL (operands[2]);
11764       HOST_WIDE_INT cf = INTVAL (operands[3]);
11765       HOST_WIDE_INT diff;
11766
11767       diff = ct - cf;
11768       /*  Sign bit compares are better done using shifts than we do by using
11769           sbb.  */
11770       if (sign_bit_compare_p
11771           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11772                                              ix86_compare_op1, &compare_op))
11773         {
11774           /* Detect overlap between destination and compare sources.  */
11775           rtx tmp = out;
11776
11777           if (!sign_bit_compare_p)
11778             {
11779               bool fpcmp = false;
11780
11781               compare_code = GET_CODE (compare_op);
11782
11783               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11784                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11785                 {
11786                   fpcmp = true;
11787                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
11788                 }
11789
11790               /* To simplify rest of code, restrict to the GEU case.  */
11791               if (compare_code == LTU)
11792                 {
11793                   HOST_WIDE_INT tmp = ct;
11794                   ct = cf;
11795                   cf = tmp;
11796                   compare_code = reverse_condition (compare_code);
11797                   code = reverse_condition (code);
11798                 }
11799               else
11800                 {
11801                   if (fpcmp)
11802                     PUT_CODE (compare_op,
11803                               reverse_condition_maybe_unordered
11804                                 (GET_CODE (compare_op)));
11805                   else
11806                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11807                 }
11808               diff = ct - cf;
11809
11810               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
11811                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
11812                 tmp = gen_reg_rtx (mode);
11813
11814               if (mode == DImode)
11815                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
11816               else
11817                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
11818             }
11819           else
11820             {
11821               if (code == GT || code == GE)
11822                 code = reverse_condition (code);
11823               else
11824                 {
11825                   HOST_WIDE_INT tmp = ct;
11826                   ct = cf;
11827                   cf = tmp;
11828                   diff = ct - cf;
11829                 }
11830               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
11831                                      ix86_compare_op1, VOIDmode, 0, -1);
11832             }
11833
11834           if (diff == 1)
11835             {
11836               /*
11837                * cmpl op0,op1
11838                * sbbl dest,dest
11839                * [addl dest, ct]
11840                *
11841                * Size 5 - 8.
11842                */
11843               if (ct)
11844                 tmp = expand_simple_binop (mode, PLUS,
11845                                            tmp, GEN_INT (ct),
11846                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11847             }
11848           else if (cf == -1)
11849             {
11850               /*
11851                * cmpl op0,op1
11852                * sbbl dest,dest
11853                * orl $ct, dest
11854                *
11855                * Size 8.
11856                */
11857               tmp = expand_simple_binop (mode, IOR,
11858                                          tmp, GEN_INT (ct),
11859                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11860             }
11861           else if (diff == -1 && ct)
11862             {
11863               /*
11864                * cmpl op0,op1
11865                * sbbl dest,dest
11866                * notl dest
11867                * [addl dest, cf]
11868                *
11869                * Size 8 - 11.
11870                */
11871               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11872               if (cf)
11873                 tmp = expand_simple_binop (mode, PLUS,
11874                                            copy_rtx (tmp), GEN_INT (cf),
11875                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11876             }
11877           else
11878             {
11879               /*
11880                * cmpl op0,op1
11881                * sbbl dest,dest
11882                * [notl dest]
11883                * andl cf - ct, dest
11884                * [addl dest, ct]
11885                *
11886                * Size 8 - 11.
11887                */
11888
11889               if (cf == 0)
11890                 {
11891                   cf = ct;
11892                   ct = 0;
11893                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
11894                 }
11895
11896               tmp = expand_simple_binop (mode, AND,
11897                                          copy_rtx (tmp),
11898                                          gen_int_mode (cf - ct, mode),
11899                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
11900               if (ct)
11901                 tmp = expand_simple_binop (mode, PLUS,
11902                                            copy_rtx (tmp), GEN_INT (ct),
11903                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
11904             }
11905
11906           if (!rtx_equal_p (tmp, out))
11907             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
11908
11909           return 1; /* DONE */
11910         }
11911
11912       if (diff < 0)
11913         {
11914           HOST_WIDE_INT tmp;
11915           tmp = ct, ct = cf, cf = tmp;
11916           diff = -diff;
11917           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11918             {
11919               /* We may be reversing unordered compare to normal compare, that
11920                  is not valid in general (we may convert non-trapping condition
11921                  to trapping one), however on i386 we currently emit all
11922                  comparisons unordered.  */
11923               compare_code = reverse_condition_maybe_unordered (compare_code);
11924               code = reverse_condition_maybe_unordered (code);
11925             }
11926           else
11927             {
11928               compare_code = reverse_condition (compare_code);
11929               code = reverse_condition (code);
11930             }
11931         }
11932
11933       compare_code = UNKNOWN;
11934       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
11935           && CONST_INT_P (ix86_compare_op1))
11936         {
11937           if (ix86_compare_op1 == const0_rtx
11938               && (code == LT || code == GE))
11939             compare_code = code;
11940           else if (ix86_compare_op1 == constm1_rtx)
11941             {
11942               if (code == LE)
11943                 compare_code = LT;
11944               else if (code == GT)
11945                 compare_code = GE;
11946             }
11947         }
11948
11949       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11950       if (compare_code != UNKNOWN
11951           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11952           && (cf == -1 || ct == -1))
11953         {
11954           /* If lea code below could be used, only optimize
11955              if it results in a 2 insn sequence.  */
11956
11957           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11958                  || diff == 3 || diff == 5 || diff == 9)
11959               || (compare_code == LT && ct == -1)
11960               || (compare_code == GE && cf == -1))
11961             {
11962               /*
11963                * notl op1       (if necessary)
11964                * sarl $31, op1
11965                * orl cf, op1
11966                */
11967               if (ct != -1)
11968                 {
11969                   cf = ct;
11970                   ct = -1;
11971                   code = reverse_condition (code);
11972                 }
11973
11974               out = emit_store_flag (out, code, ix86_compare_op0,
11975                                      ix86_compare_op1, VOIDmode, 0, -1);
11976
11977               out = expand_simple_binop (mode, IOR,
11978                                          out, GEN_INT (cf),
11979                                          out, 1, OPTAB_DIRECT);
11980               if (out != operands[0])
11981                 emit_move_insn (operands[0], out);
11982
11983               return 1; /* DONE */
11984             }
11985         }
11986
11987
11988       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
11989            || diff == 3 || diff == 5 || diff == 9)
11990           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
11991           && (mode != DImode
11992               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
11993         {
11994           /*
11995            * xorl dest,dest
11996            * cmpl op1,op2
11997            * setcc dest
11998            * lea cf(dest*(ct-cf)),dest
11999            *
12000            * Size 14.
12001            *
12002            * This also catches the degenerate setcc-only case.
12003            */
12004
12005           rtx tmp;
12006           int nops;
12007
12008           out = emit_store_flag (out, code, ix86_compare_op0,
12009                                  ix86_compare_op1, VOIDmode, 0, 1);
12010
12011           nops = 0;
12012           /* On x86_64 the lea instruction operates on Pmode, so we need
12013              to get arithmetics done in proper mode to match.  */
12014           if (diff == 1)
12015             tmp = copy_rtx (out);
12016           else
12017             {
12018               rtx out1;
12019               out1 = copy_rtx (out);
12020               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
12021               nops++;
12022               if (diff & 1)
12023                 {
12024                   tmp = gen_rtx_PLUS (mode, tmp, out1);
12025                   nops++;
12026                 }
12027             }
12028           if (cf != 0)
12029             {
12030               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
12031               nops++;
12032             }
12033           if (!rtx_equal_p (tmp, out))
12034             {
12035               if (nops == 1)
12036                 out = force_operand (tmp, copy_rtx (out));
12037               else
12038                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
12039             }
12040           if (!rtx_equal_p (out, operands[0]))
12041             emit_move_insn (operands[0], copy_rtx (out));
12042
12043           return 1; /* DONE */
12044         }
12045
12046       /*
12047        * General case:                  Jumpful:
12048        *   xorl dest,dest               cmpl op1, op2
12049        *   cmpl op1, op2                movl ct, dest
12050        *   setcc dest                   jcc 1f
12051        *   decl dest                    movl cf, dest
12052        *   andl (cf-ct),dest            1:
12053        *   addl ct,dest
12054        *
12055        * Size 20.                       Size 14.
12056        *
12057        * This is reasonably steep, but branch mispredict costs are
12058        * high on modern cpus, so consider failing only if optimizing
12059        * for space.
12060        */
12061
12062       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12063           && BRANCH_COST >= 2)
12064         {
12065           if (cf == 0)
12066             {
12067               cf = ct;
12068               ct = 0;
12069               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
12070                 /* We may be reversing unordered compare to normal compare,
12071                    that is not valid in general (we may convert non-trapping
12072                    condition to trapping one), however on i386 we currently
12073                    emit all comparisons unordered.  */
12074                 code = reverse_condition_maybe_unordered (code);
12075               else
12076                 {
12077                   code = reverse_condition (code);
12078                   if (compare_code != UNKNOWN)
12079                     compare_code = reverse_condition (compare_code);
12080                 }
12081             }
12082
12083           if (compare_code != UNKNOWN)
12084             {
12085               /* notl op1       (if needed)
12086                  sarl $31, op1
12087                  andl (cf-ct), op1
12088                  addl ct, op1
12089
12090                  For x < 0 (resp. x <= -1) there will be no notl,
12091                  so if possible swap the constants to get rid of the
12092                  complement.
12093                  True/false will be -1/0 while code below (store flag
12094                  followed by decrement) is 0/-1, so the constants need
12095                  to be exchanged once more.  */
12096
12097               if (compare_code == GE || !cf)
12098                 {
12099                   code = reverse_condition (code);
12100                   compare_code = LT;
12101                 }
12102               else
12103                 {
12104                   HOST_WIDE_INT tmp = cf;
12105                   cf = ct;
12106                   ct = tmp;
12107                 }
12108
12109               out = emit_store_flag (out, code, ix86_compare_op0,
12110                                      ix86_compare_op1, VOIDmode, 0, -1);
12111             }
12112           else
12113             {
12114               out = emit_store_flag (out, code, ix86_compare_op0,
12115                                      ix86_compare_op1, VOIDmode, 0, 1);
12116
12117               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
12118                                          copy_rtx (out), 1, OPTAB_DIRECT);
12119             }
12120
12121           out = expand_simple_binop (mode, AND, copy_rtx (out),
12122                                      gen_int_mode (cf - ct, mode),
12123                                      copy_rtx (out), 1, OPTAB_DIRECT);
12124           if (ct)
12125             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
12126                                        copy_rtx (out), 1, OPTAB_DIRECT);
12127           if (!rtx_equal_p (out, operands[0]))
12128             emit_move_insn (operands[0], copy_rtx (out));
12129
12130           return 1; /* DONE */
12131         }
12132     }
12133
12134   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
12135     {
12136       /* Try a few things more with specific constants and a variable.  */
12137
12138       optab op;
12139       rtx var, orig_out, out, tmp;
12140
12141       if (BRANCH_COST <= 2)
12142         return 0; /* FAIL */
12143
12144       /* If one of the two operands is an interesting constant, load a
12145          constant with the above and mask it in with a logical operation.  */
12146
12147       if (CONST_INT_P (operands[2]))
12148         {
12149           var = operands[3];
12150           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
12151             operands[3] = constm1_rtx, op = and_optab;
12152           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
12153             operands[3] = const0_rtx, op = ior_optab;
12154           else
12155             return 0; /* FAIL */
12156         }
12157       else if (CONST_INT_P (operands[3]))
12158         {
12159           var = operands[2];
12160           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
12161             operands[2] = constm1_rtx, op = and_optab;
12162           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
12163             operands[2] = const0_rtx, op = ior_optab;
12164           else
12165             return 0; /* FAIL */
12166         }
12167       else
12168         return 0; /* FAIL */
12169
12170       orig_out = operands[0];
12171       tmp = gen_reg_rtx (mode);
12172       operands[0] = tmp;
12173
12174       /* Recurse to get the constant loaded.  */
12175       if (ix86_expand_int_movcc (operands) == 0)
12176         return 0; /* FAIL */
12177
12178       /* Mask in the interesting variable.  */
12179       out = expand_binop (mode, op, var, tmp, orig_out, 0,
12180                           OPTAB_WIDEN);
12181       if (!rtx_equal_p (out, orig_out))
12182         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
12183
12184       return 1; /* DONE */
12185     }
12186
12187   /*
12188    * For comparison with above,
12189    *
12190    * movl cf,dest
12191    * movl ct,tmp
12192    * cmpl op1,op2
12193    * cmovcc tmp,dest
12194    *
12195    * Size 15.
12196    */
12197
12198   if (! nonimmediate_operand (operands[2], mode))
12199     operands[2] = force_reg (mode, operands[2]);
12200   if (! nonimmediate_operand (operands[3], mode))
12201     operands[3] = force_reg (mode, operands[3]);
12202
12203   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12204     {
12205       rtx tmp = gen_reg_rtx (mode);
12206       emit_move_insn (tmp, operands[3]);
12207       operands[3] = tmp;
12208     }
12209   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12210     {
12211       rtx tmp = gen_reg_rtx (mode);
12212       emit_move_insn (tmp, operands[2]);
12213       operands[2] = tmp;
12214     }
12215
12216   if (! register_operand (operands[2], VOIDmode)
12217       && (mode == QImode
12218           || ! register_operand (operands[3], VOIDmode)))
12219     operands[2] = force_reg (mode, operands[2]);
12220
12221   if (mode == QImode
12222       && ! register_operand (operands[3], VOIDmode))
12223     operands[3] = force_reg (mode, operands[3]);
12224
12225   emit_insn (compare_seq);
12226   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12227                           gen_rtx_IF_THEN_ELSE (mode,
12228                                                 compare_op, operands[2],
12229                                                 operands[3])));
12230   if (bypass_test)
12231     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
12232                             gen_rtx_IF_THEN_ELSE (mode,
12233                                   bypass_test,
12234                                   copy_rtx (operands[3]),
12235                                   copy_rtx (operands[0]))));
12236   if (second_test)
12237     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
12238                             gen_rtx_IF_THEN_ELSE (mode,
12239                                   second_test,
12240                                   copy_rtx (operands[2]),
12241                                   copy_rtx (operands[0]))));
12242
12243   return 1; /* DONE */
12244 }
12245
12246 /* Swap, force into registers, or otherwise massage the two operands
12247    to an sse comparison with a mask result.  Thus we differ a bit from
12248    ix86_prepare_fp_compare_args which expects to produce a flags result.
12249
12250    The DEST operand exists to help determine whether to commute commutative
12251    operators.  The POP0/POP1 operands are updated in place.  The new
12252    comparison code is returned, or UNKNOWN if not implementable.  */
12253
12254 static enum rtx_code
12255 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
12256                                   rtx *pop0, rtx *pop1)
12257 {
12258   rtx tmp;
12259
12260   switch (code)
12261     {
12262     case LTGT:
12263     case UNEQ:
12264       /* We have no LTGT as an operator.  We could implement it with
12265          NE & ORDERED, but this requires an extra temporary.  It's
12266          not clear that it's worth it.  */
12267       return UNKNOWN;
12268
12269     case LT:
12270     case LE:
12271     case UNGT:
12272     case UNGE:
12273       /* These are supported directly.  */
12274       break;
12275
12276     case EQ:
12277     case NE:
12278     case UNORDERED:
12279     case ORDERED:
12280       /* For commutative operators, try to canonicalize the destination
12281          operand to be first in the comparison - this helps reload to
12282          avoid extra moves.  */
12283       if (!dest || !rtx_equal_p (dest, *pop1))
12284         break;
12285       /* FALLTHRU */
12286
12287     case GE:
12288     case GT:
12289     case UNLE:
12290     case UNLT:
12291       /* These are not supported directly.  Swap the comparison operands
12292          to transform into something that is supported.  */
12293       tmp = *pop0;
12294       *pop0 = *pop1;
12295       *pop1 = tmp;
12296       code = swap_condition (code);
12297       break;
12298
12299     default:
12300       gcc_unreachable ();
12301     }
12302
12303   return code;
12304 }
12305
12306 /* Detect conditional moves that exactly match min/max operational
12307    semantics.  Note that this is IEEE safe, as long as we don't
12308    interchange the operands.
12309
12310    Returns FALSE if this conditional move doesn't match a MIN/MAX,
12311    and TRUE if the operation is successful and instructions are emitted.  */
12312
12313 static bool
12314 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
12315                            rtx cmp_op1, rtx if_true, rtx if_false)
12316 {
12317   enum machine_mode mode;
12318   bool is_min;
12319   rtx tmp;
12320
12321   if (code == LT)
12322     ;
12323   else if (code == UNGE)
12324     {
12325       tmp = if_true;
12326       if_true = if_false;
12327       if_false = tmp;
12328     }
12329   else
12330     return false;
12331
12332   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
12333     is_min = true;
12334   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
12335     is_min = false;
12336   else
12337     return false;
12338
12339   mode = GET_MODE (dest);
12340
12341   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
12342      but MODE may be a vector mode and thus not appropriate.  */
12343   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
12344     {
12345       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
12346       rtvec v;
12347
12348       if_true = force_reg (mode, if_true);
12349       v = gen_rtvec (2, if_true, if_false);
12350       tmp = gen_rtx_UNSPEC (mode, v, u);
12351     }
12352   else
12353     {
12354       code = is_min ? SMIN : SMAX;
12355       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
12356     }
12357
12358   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
12359   return true;
12360 }
12361
12362 /* Expand an sse vector comparison.  Return the register with the result.  */
12363
12364 static rtx
12365 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
12366                      rtx op_true, rtx op_false)
12367 {
12368   enum machine_mode mode = GET_MODE (dest);
12369   rtx x;
12370
12371   cmp_op0 = force_reg (mode, cmp_op0);
12372   if (!nonimmediate_operand (cmp_op1, mode))
12373     cmp_op1 = force_reg (mode, cmp_op1);
12374
12375   if (optimize
12376       || reg_overlap_mentioned_p (dest, op_true)
12377       || reg_overlap_mentioned_p (dest, op_false))
12378     dest = gen_reg_rtx (mode);
12379
12380   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
12381   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12382
12383   return dest;
12384 }
12385
12386 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
12387    operations.  This is used for both scalar and vector conditional moves.  */
12388
12389 static void
12390 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
12391 {
12392   enum machine_mode mode = GET_MODE (dest);
12393   rtx t2, t3, x;
12394
12395   if (op_false == CONST0_RTX (mode))
12396     {
12397       op_true = force_reg (mode, op_true);
12398       x = gen_rtx_AND (mode, cmp, op_true);
12399       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12400     }
12401   else if (op_true == CONST0_RTX (mode))
12402     {
12403       op_false = force_reg (mode, op_false);
12404       x = gen_rtx_NOT (mode, cmp);
12405       x = gen_rtx_AND (mode, x, op_false);
12406       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12407     }
12408   else
12409     {
12410       op_true = force_reg (mode, op_true);
12411       op_false = force_reg (mode, op_false);
12412
12413       t2 = gen_reg_rtx (mode);
12414       if (optimize)
12415         t3 = gen_reg_rtx (mode);
12416       else
12417         t3 = dest;
12418
12419       x = gen_rtx_AND (mode, op_true, cmp);
12420       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
12421
12422       x = gen_rtx_NOT (mode, cmp);
12423       x = gen_rtx_AND (mode, x, op_false);
12424       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
12425
12426       x = gen_rtx_IOR (mode, t3, t2);
12427       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
12428     }
12429 }
12430
12431 /* Expand a floating-point conditional move.  Return true if successful.  */
12432
12433 int
12434 ix86_expand_fp_movcc (rtx operands[])
12435 {
12436   enum machine_mode mode = GET_MODE (operands[0]);
12437   enum rtx_code code = GET_CODE (operands[1]);
12438   rtx tmp, compare_op, second_test, bypass_test;
12439
12440   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
12441     {
12442       enum machine_mode cmode;
12443
12444       /* Since we've no cmove for sse registers, don't force bad register
12445          allocation just to gain access to it.  Deny movcc when the
12446          comparison mode doesn't match the move mode.  */
12447       cmode = GET_MODE (ix86_compare_op0);
12448       if (cmode == VOIDmode)
12449         cmode = GET_MODE (ix86_compare_op1);
12450       if (cmode != mode)
12451         return 0;
12452
12453       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12454                                                &ix86_compare_op0,
12455                                                &ix86_compare_op1);
12456       if (code == UNKNOWN)
12457         return 0;
12458
12459       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
12460                                      ix86_compare_op1, operands[2],
12461                                      operands[3]))
12462         return 1;
12463
12464       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
12465                                  ix86_compare_op1, operands[2], operands[3]);
12466       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
12467       return 1;
12468     }
12469
12470   /* The floating point conditional move instructions don't directly
12471      support conditions resulting from a signed integer comparison.  */
12472
12473   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
12474
12475   /* The floating point conditional move instructions don't directly
12476      support signed integer comparisons.  */
12477
12478   if (!fcmov_comparison_operator (compare_op, VOIDmode))
12479     {
12480       gcc_assert (!second_test && !bypass_test);
12481       tmp = gen_reg_rtx (QImode);
12482       ix86_expand_setcc (code, tmp);
12483       code = NE;
12484       ix86_compare_op0 = tmp;
12485       ix86_compare_op1 = const0_rtx;
12486       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
12487     }
12488   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
12489     {
12490       tmp = gen_reg_rtx (mode);
12491       emit_move_insn (tmp, operands[3]);
12492       operands[3] = tmp;
12493     }
12494   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
12495     {
12496       tmp = gen_reg_rtx (mode);
12497       emit_move_insn (tmp, operands[2]);
12498       operands[2] = tmp;
12499     }
12500
12501   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12502                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
12503                                                 operands[2], operands[3])));
12504   if (bypass_test)
12505     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12506                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
12507                                                   operands[3], operands[0])));
12508   if (second_test)
12509     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
12510                             gen_rtx_IF_THEN_ELSE (mode, second_test,
12511                                                   operands[2], operands[0])));
12512
12513   return 1;
12514 }
12515
12516 /* Expand a floating-point vector conditional move; a vcond operation
12517    rather than a movcc operation.  */
12518
12519 bool
12520 ix86_expand_fp_vcond (rtx operands[])
12521 {
12522   enum rtx_code code = GET_CODE (operands[3]);
12523   rtx cmp;
12524
12525   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
12526                                            &operands[4], &operands[5]);
12527   if (code == UNKNOWN)
12528     return false;
12529
12530   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
12531                                  operands[5], operands[1], operands[2]))
12532     return true;
12533
12534   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
12535                              operands[1], operands[2]);
12536   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
12537   return true;
12538 }
12539
12540 /* Expand a signed integral vector conditional move.  */
12541
12542 bool
12543 ix86_expand_int_vcond (rtx operands[])
12544 {
12545   enum machine_mode mode = GET_MODE (operands[0]);
12546   enum rtx_code code = GET_CODE (operands[3]);
12547   bool negate = false;
12548   rtx x, cop0, cop1;
12549
12550   cop0 = operands[4];
12551   cop1 = operands[5];
12552
12553   /* Canonicalize the comparison to EQ, GT, GTU.  */
12554   switch (code)
12555     {
12556     case EQ:
12557     case GT:
12558     case GTU:
12559       break;
12560
12561     case NE:
12562     case LE:
12563     case LEU:
12564       code = reverse_condition (code);
12565       negate = true;
12566       break;
12567
12568     case GE:
12569     case GEU:
12570       code = reverse_condition (code);
12571       negate = true;
12572       /* FALLTHRU */
12573
12574     case LT:
12575     case LTU:
12576       code = swap_condition (code);
12577       x = cop0, cop0 = cop1, cop1 = x;
12578       break;
12579
12580     default:
12581       gcc_unreachable ();
12582     }
12583
12584   /* Unsigned parallel compare is not supported by the hardware.  Play some
12585      tricks to turn this into a signed comparison against 0.  */
12586   if (code == GTU)
12587     {
12588       cop0 = force_reg (mode, cop0);
12589
12590       switch (mode)
12591         {
12592         case V4SImode:
12593           {
12594             rtx t1, t2, mask;
12595
12596             /* Perform a parallel modulo subtraction.  */
12597             t1 = gen_reg_rtx (mode);
12598             emit_insn (gen_subv4si3 (t1, cop0, cop1));
12599
12600             /* Extract the original sign bit of op0.  */
12601             mask = GEN_INT (-0x80000000);
12602             mask = gen_rtx_CONST_VECTOR (mode,
12603                         gen_rtvec (4, mask, mask, mask, mask));
12604             mask = force_reg (mode, mask);
12605             t2 = gen_reg_rtx (mode);
12606             emit_insn (gen_andv4si3 (t2, cop0, mask));
12607
12608             /* XOR it back into the result of the subtraction.  This results
12609                in the sign bit set iff we saw unsigned underflow.  */
12610             x = gen_reg_rtx (mode);
12611             emit_insn (gen_xorv4si3 (x, t1, t2));
12612
12613             code = GT;
12614           }
12615           break;
12616
12617         case V16QImode:
12618         case V8HImode:
12619           /* Perform a parallel unsigned saturating subtraction.  */
12620           x = gen_reg_rtx (mode);
12621           emit_insn (gen_rtx_SET (VOIDmode, x,
12622                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
12623
12624           code = EQ;
12625           negate = !negate;
12626           break;
12627
12628         default:
12629           gcc_unreachable ();
12630         }
12631
12632       cop0 = x;
12633       cop1 = CONST0_RTX (mode);
12634     }
12635
12636   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
12637                            operands[1+negate], operands[2-negate]);
12638
12639   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
12640                          operands[2-negate]);
12641   return true;
12642 }
12643
12644 /* Unpack OP[1] into the next wider integer vector type.  UNSIGNED_P is
12645    true if we should do zero extension, else sign extension.  HIGH_P is
12646    true if we want the N/2 high elements, else the low elements.  */
12647
12648 void
12649 ix86_expand_sse_unpack (rtx operands[2], bool unsigned_p, bool high_p)
12650 {
12651   enum machine_mode imode = GET_MODE (operands[1]);
12652   rtx (*unpack)(rtx, rtx, rtx);
12653   rtx se, dest;
12654
12655   switch (imode)
12656     {
12657     case V16QImode:
12658       if (high_p)
12659         unpack = gen_vec_interleave_highv16qi;
12660       else
12661         unpack = gen_vec_interleave_lowv16qi;
12662       break;
12663     case V8HImode:
12664       if (high_p)
12665         unpack = gen_vec_interleave_highv8hi;
12666       else
12667         unpack = gen_vec_interleave_lowv8hi;
12668       break;
12669     case V4SImode:
12670       if (high_p)
12671         unpack = gen_vec_interleave_highv4si;
12672       else 
12673         unpack = gen_vec_interleave_lowv4si;
12674       break;
12675     default:
12676       gcc_unreachable (); 
12677     }
12678
12679   dest = gen_lowpart (imode, operands[0]);
12680
12681   if (unsigned_p)
12682     se = force_reg (imode, CONST0_RTX (imode));
12683   else
12684     se = ix86_expand_sse_cmp (gen_reg_rtx (imode), GT, CONST0_RTX (imode),
12685                               operands[1], pc_rtx, pc_rtx);
12686
12687   emit_insn (unpack (dest, operands[1], se));
12688 }
12689
12690 /* Expand conditional increment or decrement using adb/sbb instructions.
12691    The default case using setcc followed by the conditional move can be
12692    done by generic code.  */
12693 int
12694 ix86_expand_int_addcc (rtx operands[])
12695 {
12696   enum rtx_code code = GET_CODE (operands[1]);
12697   rtx compare_op;
12698   rtx val = const0_rtx;
12699   bool fpcmp = false;
12700   enum machine_mode mode = GET_MODE (operands[0]);
12701
12702   if (operands[3] != const1_rtx
12703       && operands[3] != constm1_rtx)
12704     return 0;
12705   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
12706                                        ix86_compare_op1, &compare_op))
12707      return 0;
12708   code = GET_CODE (compare_op);
12709
12710   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
12711       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
12712     {
12713       fpcmp = true;
12714       code = ix86_fp_compare_code_to_integer (code);
12715     }
12716
12717   if (code != LTU)
12718     {
12719       val = constm1_rtx;
12720       if (fpcmp)
12721         PUT_CODE (compare_op,
12722                   reverse_condition_maybe_unordered
12723                     (GET_CODE (compare_op)));
12724       else
12725         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
12726     }
12727   PUT_MODE (compare_op, mode);
12728
12729   /* Construct either adc or sbb insn.  */
12730   if ((code == LTU) == (operands[3] == constm1_rtx))
12731     {
12732       switch (GET_MODE (operands[0]))
12733         {
12734           case QImode:
12735             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
12736             break;
12737           case HImode:
12738             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
12739             break;
12740           case SImode:
12741             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
12742             break;
12743           case DImode:
12744             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12745             break;
12746           default:
12747             gcc_unreachable ();
12748         }
12749     }
12750   else
12751     {
12752       switch (GET_MODE (operands[0]))
12753         {
12754           case QImode:
12755             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
12756             break;
12757           case HImode:
12758             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
12759             break;
12760           case SImode:
12761             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
12762             break;
12763           case DImode:
12764             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
12765             break;
12766           default:
12767             gcc_unreachable ();
12768         }
12769     }
12770   return 1; /* DONE */
12771 }
12772
12773
12774 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
12775    works for floating pointer parameters and nonoffsetable memories.
12776    For pushes, it returns just stack offsets; the values will be saved
12777    in the right order.  Maximally three parts are generated.  */
12778
12779 static int
12780 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
12781 {
12782   int size;
12783
12784   if (!TARGET_64BIT)
12785     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
12786   else
12787     size = (GET_MODE_SIZE (mode) + 4) / 8;
12788
12789   gcc_assert (!REG_P (operand) || !MMX_REGNO_P (REGNO (operand)));
12790   gcc_assert (size >= 2 && size <= 3);
12791
12792   /* Optimize constant pool reference to immediates.  This is used by fp
12793      moves, that force all constants to memory to allow combining.  */
12794   if (MEM_P (operand) && MEM_READONLY_P (operand))
12795     {
12796       rtx tmp = maybe_get_pool_constant (operand);
12797       if (tmp)
12798         operand = tmp;
12799     }
12800
12801   if (MEM_P (operand) && !offsettable_memref_p (operand))
12802     {
12803       /* The only non-offsetable memories we handle are pushes.  */
12804       int ok = push_operand (operand, VOIDmode);
12805
12806       gcc_assert (ok);
12807
12808       operand = copy_rtx (operand);
12809       PUT_MODE (operand, Pmode);
12810       parts[0] = parts[1] = parts[2] = operand;
12811       return size;
12812     }
12813
12814   if (GET_CODE (operand) == CONST_VECTOR)
12815     {
12816       enum machine_mode imode = int_mode_for_mode (mode);
12817       /* Caution: if we looked through a constant pool memory above,
12818          the operand may actually have a different mode now.  That's
12819          ok, since we want to pun this all the way back to an integer.  */
12820       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
12821       gcc_assert (operand != NULL);
12822       mode = imode;
12823     }
12824
12825   if (!TARGET_64BIT)
12826     {
12827       if (mode == DImode)
12828         split_di (&operand, 1, &parts[0], &parts[1]);
12829       else
12830         {
12831           if (REG_P (operand))
12832             {
12833               gcc_assert (reload_completed);
12834               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
12835               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
12836               if (size == 3)
12837                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
12838             }
12839           else if (offsettable_memref_p (operand))
12840             {
12841               operand = adjust_address (operand, SImode, 0);
12842               parts[0] = operand;
12843               parts[1] = adjust_address (operand, SImode, 4);
12844               if (size == 3)
12845                 parts[2] = adjust_address (operand, SImode, 8);
12846             }
12847           else if (GET_CODE (operand) == CONST_DOUBLE)
12848             {
12849               REAL_VALUE_TYPE r;
12850               long l[4];
12851
12852               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12853               switch (mode)
12854                 {
12855                 case XFmode:
12856                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
12857                   parts[2] = gen_int_mode (l[2], SImode);
12858                   break;
12859                 case DFmode:
12860                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
12861                   break;
12862                 default:
12863                   gcc_unreachable ();
12864                 }
12865               parts[1] = gen_int_mode (l[1], SImode);
12866               parts[0] = gen_int_mode (l[0], SImode);
12867             }
12868           else
12869             gcc_unreachable ();
12870         }
12871     }
12872   else
12873     {
12874       if (mode == TImode)
12875         split_ti (&operand, 1, &parts[0], &parts[1]);
12876       if (mode == XFmode || mode == TFmode)
12877         {
12878           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
12879           if (REG_P (operand))
12880             {
12881               gcc_assert (reload_completed);
12882               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
12883               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
12884             }
12885           else if (offsettable_memref_p (operand))
12886             {
12887               operand = adjust_address (operand, DImode, 0);
12888               parts[0] = operand;
12889               parts[1] = adjust_address (operand, upper_mode, 8);
12890             }
12891           else if (GET_CODE (operand) == CONST_DOUBLE)
12892             {
12893               REAL_VALUE_TYPE r;
12894               long l[4];
12895
12896               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
12897               real_to_target (l, &r, mode);
12898
12899               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
12900               if (HOST_BITS_PER_WIDE_INT >= 64)
12901                 parts[0]
12902                   = gen_int_mode
12903                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
12904                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
12905                        DImode);
12906               else
12907                 parts[0] = immed_double_const (l[0], l[1], DImode);
12908
12909               if (upper_mode == SImode)
12910                 parts[1] = gen_int_mode (l[2], SImode);
12911               else if (HOST_BITS_PER_WIDE_INT >= 64)
12912                 parts[1]
12913                   = gen_int_mode
12914                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
12915                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
12916                        DImode);
12917               else
12918                 parts[1] = immed_double_const (l[2], l[3], DImode);
12919             }
12920           else
12921             gcc_unreachable ();
12922         }
12923     }
12924
12925   return size;
12926 }
12927
12928 /* Emit insns to perform a move or push of DI, DF, and XF values.
12929    Return false when normal moves are needed; true when all required
12930    insns have been emitted.  Operands 2-4 contain the input values
12931    int the correct order; operands 5-7 contain the output values.  */
12932
12933 void
12934 ix86_split_long_move (rtx operands[])
12935 {
12936   rtx part[2][3];
12937   int nparts;
12938   int push = 0;
12939   int collisions = 0;
12940   enum machine_mode mode = GET_MODE (operands[0]);
12941
12942   /* The DFmode expanders may ask us to move double.
12943      For 64bit target this is single move.  By hiding the fact
12944      here we simplify i386.md splitters.  */
12945   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
12946     {
12947       /* Optimize constant pool reference to immediates.  This is used by
12948          fp moves, that force all constants to memory to allow combining.  */
12949
12950       if (MEM_P (operands[1])
12951           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
12952           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
12953         operands[1] = get_pool_constant (XEXP (operands[1], 0));
12954       if (push_operand (operands[0], VOIDmode))
12955         {
12956           operands[0] = copy_rtx (operands[0]);
12957           PUT_MODE (operands[0], Pmode);
12958         }
12959       else
12960         operands[0] = gen_lowpart (DImode, operands[0]);
12961       operands[1] = gen_lowpart (DImode, operands[1]);
12962       emit_move_insn (operands[0], operands[1]);
12963       return;
12964     }
12965
12966   /* The only non-offsettable memory we handle is push.  */
12967   if (push_operand (operands[0], VOIDmode))
12968     push = 1;
12969   else
12970     gcc_assert (!MEM_P (operands[0])
12971                 || offsettable_memref_p (operands[0]));
12972
12973   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
12974   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
12975
12976   /* When emitting push, take care for source operands on the stack.  */
12977   if (push && MEM_P (operands[1])
12978       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
12979     {
12980       if (nparts == 3)
12981         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
12982                                      XEXP (part[1][2], 0));
12983       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
12984                                    XEXP (part[1][1], 0));
12985     }
12986
12987   /* We need to do copy in the right order in case an address register
12988      of the source overlaps the destination.  */
12989   if (REG_P (part[0][0]) && MEM_P (part[1][0]))
12990     {
12991       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
12992         collisions++;
12993       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12994         collisions++;
12995       if (nparts == 3
12996           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
12997         collisions++;
12998
12999       /* Collision in the middle part can be handled by reordering.  */
13000       if (collisions == 1 && nparts == 3
13001           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
13002         {
13003           rtx tmp;
13004           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
13005           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
13006         }
13007
13008       /* If there are more collisions, we can't handle it by reordering.
13009          Do an lea to the last part and use only one colliding move.  */
13010       else if (collisions > 1)
13011         {
13012           rtx base;
13013
13014           collisions = 1;
13015
13016           base = part[0][nparts - 1];
13017
13018           /* Handle the case when the last part isn't valid for lea.
13019              Happens in 64-bit mode storing the 12-byte XFmode.  */
13020           if (GET_MODE (base) != Pmode)
13021             base = gen_rtx_REG (Pmode, REGNO (base));
13022
13023           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
13024           part[1][0] = replace_equiv_address (part[1][0], base);
13025           part[1][1] = replace_equiv_address (part[1][1],
13026                                       plus_constant (base, UNITS_PER_WORD));
13027           if (nparts == 3)
13028             part[1][2] = replace_equiv_address (part[1][2],
13029                                       plus_constant (base, 8));
13030         }
13031     }
13032
13033   if (push)
13034     {
13035       if (!TARGET_64BIT)
13036         {
13037           if (nparts == 3)
13038             {
13039               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
13040                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
13041               emit_move_insn (part[0][2], part[1][2]);
13042             }
13043         }
13044       else
13045         {
13046           /* In 64bit mode we don't have 32bit push available.  In case this is
13047              register, it is OK - we will just use larger counterpart.  We also
13048              retype memory - these comes from attempt to avoid REX prefix on
13049              moving of second half of TFmode value.  */
13050           if (GET_MODE (part[1][1]) == SImode)
13051             {
13052               switch (GET_CODE (part[1][1]))
13053                 {
13054                 case MEM:
13055                   part[1][1] = adjust_address (part[1][1], DImode, 0);
13056                   break;
13057
13058                 case REG:
13059                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
13060                   break;
13061
13062                 default:
13063                   gcc_unreachable ();
13064                 }
13065
13066               if (GET_MODE (part[1][0]) == SImode)
13067                 part[1][0] = part[1][1];
13068             }
13069         }
13070       emit_move_insn (part[0][1], part[1][1]);
13071       emit_move_insn (part[0][0], part[1][0]);
13072       return;
13073     }
13074
13075   /* Choose correct order to not overwrite the source before it is copied.  */
13076   if ((REG_P (part[0][0])
13077        && REG_P (part[1][1])
13078        && (REGNO (part[0][0]) == REGNO (part[1][1])
13079            || (nparts == 3
13080                && REGNO (part[0][0]) == REGNO (part[1][2]))))
13081       || (collisions > 0
13082           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
13083     {
13084       if (nparts == 3)
13085         {
13086           operands[2] = part[0][2];
13087           operands[3] = part[0][1];
13088           operands[4] = part[0][0];
13089           operands[5] = part[1][2];
13090           operands[6] = part[1][1];
13091           operands[7] = part[1][0];
13092         }
13093       else
13094         {
13095           operands[2] = part[0][1];
13096           operands[3] = part[0][0];
13097           operands[5] = part[1][1];
13098           operands[6] = part[1][0];
13099         }
13100     }
13101   else
13102     {
13103       if (nparts == 3)
13104         {
13105           operands[2] = part[0][0];
13106           operands[3] = part[0][1];
13107           operands[4] = part[0][2];
13108           operands[5] = part[1][0];
13109           operands[6] = part[1][1];
13110           operands[7] = part[1][2];
13111         }
13112       else
13113         {
13114           operands[2] = part[0][0];
13115           operands[3] = part[0][1];
13116           operands[5] = part[1][0];
13117           operands[6] = part[1][1];
13118         }
13119     }
13120
13121   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
13122   if (optimize_size)
13123     {
13124       if (CONST_INT_P (operands[5])
13125           && operands[5] != const0_rtx
13126           && REG_P (operands[2]))
13127         {
13128           if (CONST_INT_P (operands[6])
13129               && INTVAL (operands[6]) == INTVAL (operands[5]))
13130             operands[6] = operands[2];
13131
13132           if (nparts == 3
13133               && CONST_INT_P (operands[7])
13134               && INTVAL (operands[7]) == INTVAL (operands[5]))
13135             operands[7] = operands[2];
13136         }
13137
13138       if (nparts == 3
13139           && CONST_INT_P (operands[6])
13140           && operands[6] != const0_rtx
13141           && REG_P (operands[3])
13142           && CONST_INT_P (operands[7])
13143           && INTVAL (operands[7]) == INTVAL (operands[6]))
13144         operands[7] = operands[3];
13145     }
13146
13147   emit_move_insn (operands[2], operands[5]);
13148   emit_move_insn (operands[3], operands[6]);
13149   if (nparts == 3)
13150     emit_move_insn (operands[4], operands[7]);
13151
13152   return;
13153 }
13154
13155 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
13156    left shift by a constant, either using a single shift or
13157    a sequence of add instructions.  */
13158
13159 static void
13160 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
13161 {
13162   if (count == 1)
13163     {
13164       emit_insn ((mode == DImode
13165                   ? gen_addsi3
13166                   : gen_adddi3) (operand, operand, operand));
13167     }
13168   else if (!optimize_size
13169            && count * ix86_cost->add <= ix86_cost->shift_const)
13170     {
13171       int i;
13172       for (i=0; i<count; i++)
13173         {
13174           emit_insn ((mode == DImode
13175                       ? gen_addsi3
13176                       : gen_adddi3) (operand, operand, operand));
13177         }
13178     }
13179   else
13180     emit_insn ((mode == DImode
13181                 ? gen_ashlsi3
13182                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
13183 }
13184
13185 void
13186 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
13187 {
13188   rtx low[2], high[2];
13189   int count;
13190   const int single_width = mode == DImode ? 32 : 64;
13191
13192   if (CONST_INT_P (operands[2]))
13193     {
13194       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13195       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13196
13197       if (count >= single_width)
13198         {
13199           emit_move_insn (high[0], low[1]);
13200           emit_move_insn (low[0], const0_rtx);
13201
13202           if (count > single_width)
13203             ix86_expand_ashl_const (high[0], count - single_width, mode);
13204         }
13205       else
13206         {
13207           if (!rtx_equal_p (operands[0], operands[1]))
13208             emit_move_insn (operands[0], operands[1]);
13209           emit_insn ((mode == DImode
13210                      ? gen_x86_shld_1
13211                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
13212           ix86_expand_ashl_const (low[0], count, mode);
13213         }
13214       return;
13215     }
13216
13217   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13218
13219   if (operands[1] == const1_rtx)
13220     {
13221       /* Assuming we've chosen a QImode capable registers, then 1 << N
13222          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
13223       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
13224         {
13225           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
13226
13227           ix86_expand_clear (low[0]);
13228           ix86_expand_clear (high[0]);
13229           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
13230
13231           d = gen_lowpart (QImode, low[0]);
13232           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
13233           s = gen_rtx_EQ (QImode, flags, const0_rtx);
13234           emit_insn (gen_rtx_SET (VOIDmode, d, s));
13235
13236           d = gen_lowpart (QImode, high[0]);
13237           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
13238           s = gen_rtx_NE (QImode, flags, const0_rtx);
13239           emit_insn (gen_rtx_SET (VOIDmode, d, s));
13240         }
13241
13242       /* Otherwise, we can get the same results by manually performing
13243          a bit extract operation on bit 5/6, and then performing the two
13244          shifts.  The two methods of getting 0/1 into low/high are exactly
13245          the same size.  Avoiding the shift in the bit extract case helps
13246          pentium4 a bit; no one else seems to care much either way.  */
13247       else
13248         {
13249           rtx x;
13250
13251           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
13252             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
13253           else
13254             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
13255           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
13256
13257           emit_insn ((mode == DImode
13258                       ? gen_lshrsi3
13259                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
13260           emit_insn ((mode == DImode
13261                       ? gen_andsi3
13262                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
13263           emit_move_insn (low[0], high[0]);
13264           emit_insn ((mode == DImode
13265                       ? gen_xorsi3
13266                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
13267         }
13268
13269       emit_insn ((mode == DImode
13270                     ? gen_ashlsi3
13271                     : gen_ashldi3) (low[0], low[0], operands[2]));
13272       emit_insn ((mode == DImode
13273                     ? gen_ashlsi3
13274                     : gen_ashldi3) (high[0], high[0], operands[2]));
13275       return;
13276     }
13277
13278   if (operands[1] == constm1_rtx)
13279     {
13280       /* For -1 << N, we can avoid the shld instruction, because we
13281          know that we're shifting 0...31/63 ones into a -1.  */
13282       emit_move_insn (low[0], constm1_rtx);
13283       if (optimize_size)
13284         emit_move_insn (high[0], low[0]);
13285       else
13286         emit_move_insn (high[0], constm1_rtx);
13287     }
13288   else
13289     {
13290       if (!rtx_equal_p (operands[0], operands[1]))
13291         emit_move_insn (operands[0], operands[1]);
13292
13293       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13294       emit_insn ((mode == DImode
13295                   ? gen_x86_shld_1
13296                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
13297     }
13298
13299   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
13300
13301   if (TARGET_CMOVE && scratch)
13302     {
13303       ix86_expand_clear (scratch);
13304       emit_insn ((mode == DImode
13305                   ? gen_x86_shift_adj_1
13306                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
13307     }
13308   else
13309     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
13310 }
13311
13312 void
13313 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
13314 {
13315   rtx low[2], high[2];
13316   int count;
13317   const int single_width = mode == DImode ? 32 : 64;
13318
13319   if (CONST_INT_P (operands[2]))
13320     {
13321       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13322       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13323
13324       if (count == single_width * 2 - 1)
13325         {
13326           emit_move_insn (high[0], high[1]);
13327           emit_insn ((mode == DImode
13328                       ? gen_ashrsi3
13329                       : gen_ashrdi3) (high[0], high[0],
13330                                       GEN_INT (single_width - 1)));
13331           emit_move_insn (low[0], high[0]);
13332
13333         }
13334       else if (count >= single_width)
13335         {
13336           emit_move_insn (low[0], high[1]);
13337           emit_move_insn (high[0], low[0]);
13338           emit_insn ((mode == DImode
13339                       ? gen_ashrsi3
13340                       : gen_ashrdi3) (high[0], high[0],
13341                                       GEN_INT (single_width - 1)));
13342           if (count > single_width)
13343             emit_insn ((mode == DImode
13344                         ? gen_ashrsi3
13345                         : gen_ashrdi3) (low[0], low[0],
13346                                         GEN_INT (count - single_width)));
13347         }
13348       else
13349         {
13350           if (!rtx_equal_p (operands[0], operands[1]))
13351             emit_move_insn (operands[0], operands[1]);
13352           emit_insn ((mode == DImode
13353                       ? gen_x86_shrd_1
13354                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13355           emit_insn ((mode == DImode
13356                       ? gen_ashrsi3
13357                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
13358         }
13359     }
13360   else
13361     {
13362       if (!rtx_equal_p (operands[0], operands[1]))
13363         emit_move_insn (operands[0], operands[1]);
13364
13365       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13366
13367       emit_insn ((mode == DImode
13368                   ? gen_x86_shrd_1
13369                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13370       emit_insn ((mode == DImode
13371                   ? gen_ashrsi3
13372                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
13373
13374       if (TARGET_CMOVE && scratch)
13375         {
13376           emit_move_insn (scratch, high[0]);
13377           emit_insn ((mode == DImode
13378                       ? gen_ashrsi3
13379                       : gen_ashrdi3) (scratch, scratch,
13380                                       GEN_INT (single_width - 1)));
13381           emit_insn ((mode == DImode
13382                       ? gen_x86_shift_adj_1
13383                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13384                                          scratch));
13385         }
13386       else
13387         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
13388     }
13389 }
13390
13391 void
13392 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
13393 {
13394   rtx low[2], high[2];
13395   int count;
13396   const int single_width = mode == DImode ? 32 : 64;
13397
13398   if (CONST_INT_P (operands[2]))
13399     {
13400       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
13401       count = INTVAL (operands[2]) & (single_width * 2 - 1);
13402
13403       if (count >= single_width)
13404         {
13405           emit_move_insn (low[0], high[1]);
13406           ix86_expand_clear (high[0]);
13407
13408           if (count > single_width)
13409             emit_insn ((mode == DImode
13410                         ? gen_lshrsi3
13411                         : gen_lshrdi3) (low[0], low[0],
13412                                         GEN_INT (count - single_width)));
13413         }
13414       else
13415         {
13416           if (!rtx_equal_p (operands[0], operands[1]))
13417             emit_move_insn (operands[0], operands[1]);
13418           emit_insn ((mode == DImode
13419                       ? gen_x86_shrd_1
13420                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
13421           emit_insn ((mode == DImode
13422                       ? gen_lshrsi3
13423                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
13424         }
13425     }
13426   else
13427     {
13428       if (!rtx_equal_p (operands[0], operands[1]))
13429         emit_move_insn (operands[0], operands[1]);
13430
13431       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
13432
13433       emit_insn ((mode == DImode
13434                   ? gen_x86_shrd_1
13435                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
13436       emit_insn ((mode == DImode
13437                   ? gen_lshrsi3
13438                   : gen_lshrdi3) (high[0], high[0], operands[2]));
13439
13440       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
13441       if (TARGET_CMOVE && scratch)
13442         {
13443           ix86_expand_clear (scratch);
13444           emit_insn ((mode == DImode
13445                       ? gen_x86_shift_adj_1
13446                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
13447                                                scratch));
13448         }
13449       else
13450         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
13451     }
13452 }
13453
13454 /* Predict just emitted jump instruction to be taken with probability PROB.  */
13455 static void
13456 predict_jump (int prob)
13457 {
13458   rtx insn = get_last_insn ();
13459   gcc_assert (JUMP_P (insn));
13460   REG_NOTES (insn)
13461     = gen_rtx_EXPR_LIST (REG_BR_PROB,
13462                          GEN_INT (prob),
13463                          REG_NOTES (insn));
13464 }
13465
13466 /* Helper function for the string operations below.  Dest VARIABLE whether
13467    it is aligned to VALUE bytes.  If true, jump to the label.  */
13468 static rtx
13469 ix86_expand_aligntest (rtx variable, int value, bool epilogue)
13470 {
13471   rtx label = gen_label_rtx ();
13472   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
13473   if (GET_MODE (variable) == DImode)
13474     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
13475   else
13476     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
13477   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
13478                            1, label);
13479   if (epilogue)
13480     predict_jump (REG_BR_PROB_BASE * 50 / 100);
13481   else
13482     predict_jump (REG_BR_PROB_BASE * 90 / 100);
13483   return label;
13484 }
13485
13486 /* Adjust COUNTER by the VALUE.  */
13487 static void
13488 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
13489 {
13490   if (GET_MODE (countreg) == DImode)
13491     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
13492   else
13493     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
13494 }
13495
13496 /* Zero extend possibly SImode EXP to Pmode register.  */
13497 rtx
13498 ix86_zero_extend_to_Pmode (rtx exp)
13499 {
13500   rtx r;
13501   if (GET_MODE (exp) == VOIDmode)
13502     return force_reg (Pmode, exp);
13503   if (GET_MODE (exp) == Pmode)
13504     return copy_to_mode_reg (Pmode, exp);
13505   r = gen_reg_rtx (Pmode);
13506   emit_insn (gen_zero_extendsidi2 (r, exp));
13507   return r;
13508 }
13509
13510 /* Divide COUNTREG by SCALE.  */
13511 static rtx
13512 scale_counter (rtx countreg, int scale)
13513 {
13514   rtx sc;
13515   rtx piece_size_mask;
13516
13517   if (scale == 1)
13518     return countreg;
13519   if (CONST_INT_P (countreg))
13520     return GEN_INT (INTVAL (countreg) / scale);
13521   gcc_assert (REG_P (countreg));
13522
13523   piece_size_mask = GEN_INT (scale - 1);
13524   sc = expand_simple_binop (GET_MODE (countreg), LSHIFTRT, countreg,
13525                             GEN_INT (exact_log2 (scale)),
13526                             NULL, 1, OPTAB_DIRECT);
13527   return sc;
13528 }
13529
13530 /* Return mode for the memcpy/memset loop counter.  Preffer SImode over DImode
13531    for constant loop counts.  */
13532
13533 static enum machine_mode
13534 counter_mode (rtx count_exp)
13535 {
13536   if (GET_MODE (count_exp) != VOIDmode)
13537     return GET_MODE (count_exp);
13538   if (GET_CODE (count_exp) != CONST_INT)
13539     return Pmode;
13540   if (TARGET_64BIT && (INTVAL (count_exp) & ~0xffffffff))
13541     return DImode;
13542   return SImode;
13543 }
13544
13545 /* When SRCPTR is non-NULL, output simple loop to move memory
13546    pointer to SRCPTR to DESTPTR via chunks of MODE unrolled UNROLL times,
13547    overall size is COUNT specified in bytes.  When SRCPTR is NULL, output the
13548    equivalent loop to set memory by VALUE (supposed to be in MODE).
13549
13550    The size is rounded down to whole number of chunk size moved at once.
13551    SRCMEM and DESTMEM provide MEMrtx to feed proper aliasing info.  */
13552   
13553
13554 static void
13555 expand_set_or_movmem_via_loop (rtx destmem, rtx srcmem,
13556                                rtx destptr, rtx srcptr, rtx value,
13557                                rtx count, enum machine_mode mode, int unroll,
13558                                int expected_size)
13559 {
13560   rtx out_label, top_label, iter, tmp;
13561   enum machine_mode iter_mode = counter_mode (count);
13562   rtx piece_size = GEN_INT (GET_MODE_SIZE (mode) * unroll);
13563   rtx piece_size_mask = GEN_INT (~((GET_MODE_SIZE (mode) * unroll) - 1));
13564   rtx size;
13565   rtx x_addr;
13566   rtx y_addr;
13567   int i;
13568
13569   top_label = gen_label_rtx ();
13570   out_label = gen_label_rtx ();
13571   iter = gen_reg_rtx (iter_mode);
13572
13573   size = expand_simple_binop (iter_mode, AND, count, piece_size_mask,
13574                               NULL, 1, OPTAB_DIRECT);
13575   /* Those two should combine.  */
13576   if (piece_size == const1_rtx)
13577     {
13578       emit_cmp_and_jump_insns (size, const0_rtx, EQ, NULL_RTX, iter_mode,
13579                                true, out_label);
13580       predict_jump (REG_BR_PROB_BASE * 10 / 100);
13581     }
13582   emit_move_insn (iter, const0_rtx);
13583
13584   emit_label (top_label);
13585
13586   tmp = convert_modes (Pmode, iter_mode, iter, true);
13587   x_addr = gen_rtx_PLUS (Pmode, destptr, tmp);
13588   destmem = change_address (destmem, mode, x_addr);
13589
13590   if (srcmem)
13591     {
13592       y_addr = gen_rtx_PLUS (Pmode, srcptr, copy_rtx (tmp));
13593       srcmem = change_address (srcmem, mode, y_addr);
13594
13595       /* When unrolling for chips that reorder memory reads and writes,
13596          we can save registers by using single temporary.  
13597          Also using 4 temporaries is overkill in 32bit mode.  */
13598       if (!TARGET_64BIT && 0)
13599         {
13600           for (i = 0; i < unroll; i++)
13601             {
13602               if (i)
13603                 {
13604                   destmem =
13605                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13606                   srcmem =
13607                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13608                 }
13609               emit_move_insn (destmem, srcmem);
13610             }
13611         }
13612       else
13613         {
13614           rtx tmpreg[4];
13615           gcc_assert (unroll <= 4);
13616           for (i = 0; i < unroll; i++)
13617             {
13618               tmpreg[i] = gen_reg_rtx (mode);
13619               if (i)
13620                 {
13621                   srcmem =
13622                     adjust_address (copy_rtx (srcmem), mode, GET_MODE_SIZE (mode));
13623                 }
13624               emit_move_insn (tmpreg[i], srcmem);
13625             }
13626           for (i = 0; i < unroll; i++)
13627             {
13628               if (i)
13629                 {
13630                   destmem =
13631                     adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13632                 }
13633               emit_move_insn (destmem, tmpreg[i]);
13634             }
13635         }
13636     }
13637   else
13638     for (i = 0; i < unroll; i++)
13639       {
13640         if (i)
13641           destmem =
13642             adjust_address (copy_rtx (destmem), mode, GET_MODE_SIZE (mode));
13643         emit_move_insn (destmem, value);
13644       }
13645
13646   tmp = expand_simple_binop (iter_mode, PLUS, iter, piece_size, iter,
13647                              true, OPTAB_LIB_WIDEN);
13648   if (tmp != iter)
13649     emit_move_insn (iter, tmp);
13650
13651   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
13652                            true, top_label);
13653   if (expected_size != -1)
13654     {
13655       expected_size /= GET_MODE_SIZE (mode) * unroll;
13656       if (expected_size == 0)
13657         predict_jump (0);
13658       else if (expected_size > REG_BR_PROB_BASE)
13659         predict_jump (REG_BR_PROB_BASE - 1);
13660       else
13661         predict_jump (REG_BR_PROB_BASE - (REG_BR_PROB_BASE + expected_size / 2) / expected_size);
13662     }
13663   else
13664     predict_jump (REG_BR_PROB_BASE * 80 / 100);
13665   iter = ix86_zero_extend_to_Pmode (iter);
13666   tmp = expand_simple_binop (Pmode, PLUS, destptr, iter, destptr,
13667                              true, OPTAB_LIB_WIDEN);
13668   if (tmp != destptr)
13669     emit_move_insn (destptr, tmp);
13670   if (srcptr)
13671     {
13672       tmp = expand_simple_binop (Pmode, PLUS, srcptr, iter, srcptr,
13673                                  true, OPTAB_LIB_WIDEN);
13674       if (tmp != srcptr)
13675         emit_move_insn (srcptr, tmp);
13676     }
13677   emit_label (out_label);
13678 }
13679
13680 /* Output "rep; mov" instruction.  
13681    Arguments have same meaning as for previous function */
13682 static void
13683 expand_movmem_via_rep_mov (rtx destmem, rtx srcmem,
13684                            rtx destptr, rtx srcptr,
13685                            rtx count,
13686                            enum machine_mode mode)
13687 {
13688   rtx destexp;
13689   rtx srcexp;
13690   rtx countreg;
13691
13692   /* If the size is known, it is shorter to use rep movs.  */
13693   if (mode == QImode && CONST_INT_P (count)
13694       && !(INTVAL (count) & 3))
13695     mode = SImode;
13696
13697   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13698     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13699   if (srcptr != XEXP (srcmem, 0) || GET_MODE (srcmem) != BLKmode)
13700     srcmem = adjust_automodify_address_nv (srcmem, BLKmode, srcptr, 0);
13701   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13702   if (mode != QImode)
13703     {
13704       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13705                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13706       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13707       srcexp = gen_rtx_ASHIFT (Pmode, countreg,
13708                                GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13709       srcexp = gen_rtx_PLUS (Pmode, srcexp, srcptr);
13710     }
13711   else
13712     {
13713       destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13714       srcexp = gen_rtx_PLUS (Pmode, srcptr, countreg);
13715     }
13716   emit_insn (gen_rep_mov (destptr, destmem, srcptr, srcmem, countreg,
13717                           destexp, srcexp));
13718 }
13719
13720 /* Output "rep; stos" instruction.  
13721    Arguments have same meaning as for previous function */
13722 static void
13723 expand_setmem_via_rep_stos (rtx destmem, rtx destptr, rtx value,
13724                             rtx count,
13725                             enum machine_mode mode)
13726 {
13727   rtx destexp;
13728   rtx countreg;
13729
13730   if (destptr != XEXP (destmem, 0) || GET_MODE (destmem) != BLKmode)
13731     destmem = adjust_automodify_address_nv (destmem, BLKmode, destptr, 0);
13732   value = force_reg (mode, gen_lowpart (mode, value));
13733   countreg = ix86_zero_extend_to_Pmode (scale_counter (count, GET_MODE_SIZE (mode)));
13734   if (mode != QImode)
13735     {
13736       destexp = gen_rtx_ASHIFT (Pmode, countreg,
13737                                 GEN_INT (exact_log2 (GET_MODE_SIZE (mode))));
13738       destexp = gen_rtx_PLUS (Pmode, destexp, destptr);
13739     }
13740   else
13741     destexp = gen_rtx_PLUS (Pmode, destptr, countreg);
13742   emit_insn (gen_rep_stos (destptr, countreg, destmem, value, destexp));
13743 }
13744
13745 static void
13746 emit_strmov (rtx destmem, rtx srcmem,
13747              rtx destptr, rtx srcptr, enum machine_mode mode, int offset)
13748 {
13749   rtx src = adjust_automodify_address_nv (srcmem, mode, srcptr, offset);
13750   rtx dest = adjust_automodify_address_nv (destmem, mode, destptr, offset);
13751   emit_insn (gen_strmov (destptr, dest, srcptr, src));
13752 }
13753
13754 /* Output code to copy at most count & (max_size - 1) bytes from SRC to DEST.  */
13755 static void
13756 expand_movmem_epilogue (rtx destmem, rtx srcmem,
13757                         rtx destptr, rtx srcptr, rtx count, int max_size)
13758 {
13759   rtx src, dest;
13760   if (CONST_INT_P (count))
13761     {
13762       HOST_WIDE_INT countval = INTVAL (count);
13763       int offset = 0;
13764
13765       if ((countval & 0x10) && max_size > 16)
13766         {
13767           if (TARGET_64BIT)
13768             {
13769               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13770               emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset + 8);
13771             }
13772           else
13773             gcc_unreachable ();
13774           offset += 16;
13775         }
13776       if ((countval & 0x08) && max_size > 8)
13777         {
13778           if (TARGET_64BIT)
13779             emit_strmov (destmem, srcmem, destptr, srcptr, DImode, offset);
13780           else
13781             {
13782               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13783               emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset + 4);
13784             }
13785           offset += 8;
13786         }
13787       if ((countval & 0x04) && max_size > 4)
13788         {
13789           emit_strmov (destmem, srcmem, destptr, srcptr, SImode, offset);
13790           offset += 4;
13791         }
13792       if ((countval & 0x02) && max_size > 2)
13793         {
13794           emit_strmov (destmem, srcmem, destptr, srcptr, HImode, offset);
13795           offset += 2;
13796         }
13797       if ((countval & 0x01) && max_size > 1)
13798         {
13799           emit_strmov (destmem, srcmem, destptr, srcptr, QImode, offset);
13800           offset += 1;
13801         }
13802       return;
13803     }
13804   if (max_size > 8)
13805     {
13806       count = expand_simple_binop (GET_MODE (count), AND, count, GEN_INT (max_size - 1),
13807                                     count, 1, OPTAB_DIRECT);
13808       expand_set_or_movmem_via_loop (destmem, srcmem, destptr, srcptr, NULL,
13809                                      count, QImode, 1, 4);
13810       return;
13811     }
13812
13813   /* When there are stringops, we can cheaply increase dest and src pointers.
13814      Otherwise we save code size by maintaining offset (zero is readily
13815      available from preceding rep operation) and using x86 addressing modes.
13816    */
13817   if (TARGET_SINGLE_STRINGOP)
13818     {
13819       if (max_size > 4)
13820         {
13821           rtx label = ix86_expand_aligntest (count, 4, true);
13822           src = change_address (srcmem, SImode, srcptr);
13823           dest = change_address (destmem, SImode, destptr);
13824           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13825           emit_label (label);
13826           LABEL_NUSES (label) = 1;
13827         }
13828       if (max_size > 2)
13829         {
13830           rtx label = ix86_expand_aligntest (count, 2, true);
13831           src = change_address (srcmem, HImode, srcptr);
13832           dest = change_address (destmem, HImode, destptr);
13833           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13834           emit_label (label);
13835           LABEL_NUSES (label) = 1;
13836         }
13837       if (max_size > 1)
13838         {
13839           rtx label = ix86_expand_aligntest (count, 1, true);
13840           src = change_address (srcmem, QImode, srcptr);
13841           dest = change_address (destmem, QImode, destptr);
13842           emit_insn (gen_strmov (destptr, dest, srcptr, src));
13843           emit_label (label);
13844           LABEL_NUSES (label) = 1;
13845         }
13846     }
13847   else
13848     {
13849       rtx offset = force_reg (Pmode, const0_rtx);
13850       rtx tmp;
13851
13852       if (max_size > 4)
13853         {
13854           rtx label = ix86_expand_aligntest (count, 4, true);
13855           src = change_address (srcmem, SImode, srcptr);
13856           dest = change_address (destmem, SImode, destptr);
13857           emit_move_insn (dest, src);
13858           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (4), NULL,
13859                                      true, OPTAB_LIB_WIDEN);
13860           if (tmp != offset)
13861             emit_move_insn (offset, tmp);
13862           emit_label (label);
13863           LABEL_NUSES (label) = 1;
13864         }
13865       if (max_size > 2)
13866         {
13867           rtx label = ix86_expand_aligntest (count, 2, true);
13868           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13869           src = change_address (srcmem, HImode, tmp);
13870           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13871           dest = change_address (destmem, HImode, tmp);
13872           emit_move_insn (dest, src);
13873           tmp = expand_simple_binop (Pmode, PLUS, offset, GEN_INT (2), tmp,
13874                                      true, OPTAB_LIB_WIDEN);
13875           if (tmp != offset)
13876             emit_move_insn (offset, tmp);
13877           emit_label (label);
13878           LABEL_NUSES (label) = 1;
13879         }
13880       if (max_size > 1)
13881         {
13882           rtx label = ix86_expand_aligntest (count, 1, true);
13883           tmp = gen_rtx_PLUS (Pmode, srcptr, offset);
13884           src = change_address (srcmem, QImode, tmp);
13885           tmp = gen_rtx_PLUS (Pmode, destptr, offset);
13886           dest = change_address (destmem, QImode, tmp);
13887           emit_move_insn (dest, src);
13888           emit_label (label);
13889           LABEL_NUSES (label) = 1;
13890         }
13891     }
13892 }
13893
13894 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13895 static void
13896 expand_setmem_epilogue_via_loop (rtx destmem, rtx destptr, rtx value,
13897                                  rtx count, int max_size)
13898 {
13899   count =
13900     expand_simple_binop (counter_mode (count), AND, count,
13901                          GEN_INT (max_size - 1), count, 1, OPTAB_DIRECT);
13902   expand_set_or_movmem_via_loop (destmem, NULL, destptr, NULL,
13903                                  gen_lowpart (QImode, value), count, QImode,
13904                                  1, max_size / 2);
13905 }
13906
13907 /* Output code to set at most count & (max_size - 1) bytes starting by DEST.  */
13908 static void
13909 expand_setmem_epilogue (rtx destmem, rtx destptr, rtx value, rtx count, int max_size)
13910 {
13911   rtx dest;
13912
13913   if (CONST_INT_P (count))
13914     {
13915       HOST_WIDE_INT countval = INTVAL (count);
13916       int offset = 0;
13917
13918       if ((countval & 0x10) && max_size > 16)
13919         {
13920           if (TARGET_64BIT)
13921             {
13922               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13923               emit_insn (gen_strset (destptr, dest, value));
13924               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset + 8);
13925               emit_insn (gen_strset (destptr, dest, value));
13926             }
13927           else
13928             gcc_unreachable ();
13929           offset += 16;
13930         }
13931       if ((countval & 0x08) && max_size > 8)
13932         {
13933           if (TARGET_64BIT)
13934             {
13935               dest = adjust_automodify_address_nv (destmem, DImode, destptr, offset);
13936               emit_insn (gen_strset (destptr, dest, value));
13937             }
13938           else
13939             {
13940               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13941               emit_insn (gen_strset (destptr, dest, value));
13942               dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset + 4);
13943               emit_insn (gen_strset (destptr, dest, value));
13944             }
13945           offset += 8;
13946         }
13947       if ((countval & 0x04) && max_size > 4)
13948         {
13949           dest = adjust_automodify_address_nv (destmem, SImode, destptr, offset);
13950           emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
13951           offset += 4;
13952         }
13953       if ((countval & 0x02) && max_size > 2)
13954         {
13955           dest = adjust_automodify_address_nv (destmem, HImode, destptr, offset);
13956           emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
13957           offset += 2;
13958         }
13959       if ((countval & 0x01) && max_size > 1)
13960         {
13961           dest = adjust_automodify_address_nv (destmem, QImode, destptr, offset);
13962           emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
13963           offset += 1;
13964         }
13965       return;
13966     }
13967   if (max_size > 32)
13968     {
13969       expand_setmem_epilogue_via_loop (destmem, destptr, value, count, max_size);
13970       return;
13971     }
13972   if (max_size > 16)
13973     {
13974       rtx label = ix86_expand_aligntest (count, 16, true);
13975       if (TARGET_64BIT)
13976         {
13977           dest = change_address (destmem, DImode, destptr);
13978           emit_insn (gen_strset (destptr, dest, value));
13979           emit_insn (gen_strset (destptr, dest, value));
13980         }
13981       else
13982         {
13983           dest = change_address (destmem, SImode, destptr);
13984           emit_insn (gen_strset (destptr, dest, value));
13985           emit_insn (gen_strset (destptr, dest, value));
13986           emit_insn (gen_strset (destptr, dest, value));
13987           emit_insn (gen_strset (destptr, dest, value));
13988         }
13989       emit_label (label);
13990       LABEL_NUSES (label) = 1;
13991     }
13992   if (max_size > 8)
13993     {
13994       rtx label = ix86_expand_aligntest (count, 8, true);
13995       if (TARGET_64BIT)
13996         {
13997           dest = change_address (destmem, DImode, destptr);
13998           emit_insn (gen_strset (destptr, dest, value));
13999         }
14000       else
14001         {
14002           dest = change_address (destmem, SImode, destptr);
14003           emit_insn (gen_strset (destptr, dest, value));
14004           emit_insn (gen_strset (destptr, dest, value));
14005         }
14006       emit_label (label);
14007       LABEL_NUSES (label) = 1;
14008     }
14009   if (max_size > 4)
14010     {
14011       rtx label = ix86_expand_aligntest (count, 4, true);
14012       dest = change_address (destmem, SImode, destptr);
14013       emit_insn (gen_strset (destptr, dest, gen_lowpart (SImode, value)));
14014       emit_label (label);
14015       LABEL_NUSES (label) = 1;
14016     }
14017   if (max_size > 2)
14018     {
14019       rtx label = ix86_expand_aligntest (count, 2, true);
14020       dest = change_address (destmem, HImode, destptr);
14021       emit_insn (gen_strset (destptr, dest, gen_lowpart (HImode, value)));
14022       emit_label (label);
14023       LABEL_NUSES (label) = 1;
14024     }
14025   if (max_size > 1)
14026     {
14027       rtx label = ix86_expand_aligntest (count, 1, true);
14028       dest = change_address (destmem, QImode, destptr);
14029       emit_insn (gen_strset (destptr, dest, gen_lowpart (QImode, value)));
14030       emit_label (label);
14031       LABEL_NUSES (label) = 1;
14032     }
14033 }
14034
14035 /* Copy enough from DEST to SRC to align DEST known to by aligned by ALIGN to
14036    DESIRED_ALIGNMENT.  */
14037 static void
14038 expand_movmem_prologue (rtx destmem, rtx srcmem,
14039                         rtx destptr, rtx srcptr, rtx count,
14040                         int align, int desired_alignment)
14041 {
14042   if (align <= 1 && desired_alignment > 1)
14043     {
14044       rtx label = ix86_expand_aligntest (destptr, 1, false);
14045       srcmem = change_address (srcmem, QImode, srcptr);
14046       destmem = change_address (destmem, QImode, destptr);
14047       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14048       ix86_adjust_counter (count, 1);
14049       emit_label (label);
14050       LABEL_NUSES (label) = 1;
14051     }
14052   if (align <= 2 && desired_alignment > 2)
14053     {
14054       rtx label = ix86_expand_aligntest (destptr, 2, false);
14055       srcmem = change_address (srcmem, HImode, srcptr);
14056       destmem = change_address (destmem, HImode, destptr);
14057       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14058       ix86_adjust_counter (count, 2);
14059       emit_label (label);
14060       LABEL_NUSES (label) = 1;
14061     }
14062   if (align <= 4 && desired_alignment > 4)
14063     {
14064       rtx label = ix86_expand_aligntest (destptr, 4, false);
14065       srcmem = change_address (srcmem, SImode, srcptr);
14066       destmem = change_address (destmem, SImode, destptr);
14067       emit_insn (gen_strmov (destptr, destmem, srcptr, srcmem));
14068       ix86_adjust_counter (count, 4);
14069       emit_label (label);
14070       LABEL_NUSES (label) = 1;
14071     }
14072   gcc_assert (desired_alignment <= 8);
14073 }
14074
14075 /* Set enough from DEST to align DEST known to by aligned by ALIGN to
14076    DESIRED_ALIGNMENT.  */
14077 static void
14078 expand_setmem_prologue (rtx destmem, rtx destptr, rtx value, rtx count,
14079                         int align, int desired_alignment)
14080 {
14081   if (align <= 1 && desired_alignment > 1)
14082     {
14083       rtx label = ix86_expand_aligntest (destptr, 1, false);
14084       destmem = change_address (destmem, QImode, destptr);
14085       emit_insn (gen_strset (destptr, destmem, gen_lowpart (QImode, value)));
14086       ix86_adjust_counter (count, 1);
14087       emit_label (label);
14088       LABEL_NUSES (label) = 1;
14089     }
14090   if (align <= 2 && desired_alignment > 2)
14091     {
14092       rtx label = ix86_expand_aligntest (destptr, 2, false);
14093       destmem = change_address (destmem, HImode, destptr);
14094       emit_insn (gen_strset (destptr, destmem, gen_lowpart (HImode, value)));
14095       ix86_adjust_counter (count, 2);
14096       emit_label (label);
14097       LABEL_NUSES (label) = 1;
14098     }
14099   if (align <= 4 && desired_alignment > 4)
14100     {
14101       rtx label = ix86_expand_aligntest (destptr, 4, false);
14102       destmem = change_address (destmem, SImode, destptr);
14103       emit_insn (gen_strset (destptr, destmem, gen_lowpart (SImode, value)));
14104       ix86_adjust_counter (count, 4);
14105       emit_label (label);
14106       LABEL_NUSES (label) = 1;
14107     }
14108   gcc_assert (desired_alignment <= 8);
14109 }
14110
14111 /* Given COUNT and EXPECTED_SIZE, decide on codegen of string operation.  */
14112 static enum stringop_alg
14113 decide_alg (HOST_WIDE_INT count, HOST_WIDE_INT expected_size, bool memset,
14114             int *dynamic_check)
14115 {
14116   const struct stringop_algs * algs;
14117
14118   *dynamic_check = -1;
14119   if (memset)
14120     algs = &ix86_cost->memset[TARGET_64BIT != 0];
14121   else
14122     algs = &ix86_cost->memcpy[TARGET_64BIT != 0];
14123   if (stringop_alg != no_stringop)
14124     return stringop_alg;
14125   /* rep; movq or rep; movl is the smallest variant.  */
14126   else if (optimize_size)
14127     {
14128       if (!count || (count & 3))
14129         return rep_prefix_1_byte;
14130       else
14131         return rep_prefix_4_byte;
14132     }
14133   /* Very tiny blocks are best handled via the loop, REP is expensive to setup.
14134    */
14135   else if (expected_size != -1 && expected_size < 4)
14136     return loop_1_byte;
14137   else if (expected_size != -1)
14138     {
14139       unsigned int i;
14140       enum stringop_alg alg = libcall;
14141       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
14142         {
14143           gcc_assert (algs->size[i].max);
14144           if (algs->size[i].max >= expected_size || algs->size[i].max == -1)
14145             {
14146               if (algs->size[i].alg != libcall)
14147                 alg = algs->size[i].alg;
14148               /* Honor TARGET_INLINE_ALL_STRINGOPS by picking
14149                  last non-libcall inline algorithm.  */
14150               if (TARGET_INLINE_ALL_STRINGOPS)
14151                 {
14152                   /* When the current size is best to be copied by a libcall,
14153                      but we are still forced to inline, run the heuristic bellow
14154                      that will pick code for medium sized blocks.  */
14155                   if (alg != libcall)
14156                     return alg;
14157                   break;
14158                 }
14159               else
14160                 return algs->size[i].alg;
14161             }
14162         }
14163       gcc_assert (TARGET_INLINE_ALL_STRINGOPS);
14164     }
14165   /* When asked to inline the call anyway, try to pick meaningful choice.
14166      We look for maximal size of block that is faster to copy by hand and
14167      take blocks of at most of that size guessing that average size will
14168      be roughly half of the block.  
14169
14170      If this turns out to be bad, we might simply specify the preferred
14171      choice in ix86_costs.  */
14172   if ((TARGET_INLINE_ALL_STRINGOPS || TARGET_INLINE_STRINGOPS_DYNAMICALLY)
14173       && algs->unknown_size == libcall)
14174     {
14175       int max = -1;
14176       enum stringop_alg alg;
14177       int i;
14178
14179       for (i = 0; i < NAX_STRINGOP_ALGS; i++)
14180         if (algs->size[i].alg != libcall && algs->size[i].alg)
14181           max = algs->size[i].max;
14182       if (max == -1)
14183         max = 4096;
14184       alg = decide_alg (count, max / 2, memset, dynamic_check);
14185       gcc_assert (*dynamic_check == -1);
14186       gcc_assert (alg != libcall);
14187       if (TARGET_INLINE_STRINGOPS_DYNAMICALLY)
14188         *dynamic_check = max;
14189       return alg;
14190     }
14191   return algs->unknown_size;
14192 }
14193
14194 /* Decide on alignment.  We know that the operand is already aligned to ALIGN
14195    (ALIGN can be based on profile feedback and thus it is not 100% guaranteed).  */
14196 static int
14197 decide_alignment (int align,
14198                   enum stringop_alg alg,
14199                   int expected_size)
14200 {
14201   int desired_align = 0;
14202   switch (alg)
14203     {
14204       case no_stringop:
14205         gcc_unreachable ();
14206       case loop:
14207       case unrolled_loop:
14208         desired_align = GET_MODE_SIZE (Pmode);
14209         break;
14210       case rep_prefix_8_byte:
14211         desired_align = 8;
14212         break;
14213       case rep_prefix_4_byte:
14214         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
14215            copying whole cacheline at once.  */
14216         if (TARGET_PENTIUMPRO)
14217           desired_align = 8;
14218         else
14219           desired_align = 4;
14220         break;
14221       case rep_prefix_1_byte:
14222         /* PentiumPro has special logic triggering for 8 byte aligned blocks.
14223            copying whole cacheline at once.  */
14224         if (TARGET_PENTIUMPRO)
14225           desired_align = 8;
14226         else
14227           desired_align = 1;
14228         break;
14229       case loop_1_byte:
14230         desired_align = 1;
14231         break;
14232       case libcall:
14233         return 0;
14234     }
14235
14236   if (optimize_size)
14237     desired_align = 1;
14238   if (desired_align < align)
14239     desired_align = align;
14240   if (expected_size != -1 && expected_size < 4)
14241     desired_align = align;
14242   return desired_align;
14243 }
14244
14245 /* Return the smallest power of 2 greater than VAL.  */
14246 static int
14247 smallest_pow2_greater_than (int val)
14248 {
14249   int ret = 1;
14250   while (ret <= val)
14251     ret <<= 1;
14252   return ret;
14253 }
14254
14255 /* Expand string move (memcpy) operation.  Use i386 string operations when
14256    profitable.  expand_clrmem contains similar code. The code depends upon
14257    architecture, block size and alignment, but always has the same
14258    overall structure:
14259
14260    1) Prologue guard: Conditional that jumps up to epilogues for small
14261       blocks that can be handled by epilogue alone.  This is faster but
14262       also needed for correctness, since prologue assume the block is larger
14263       than the desired alignment.
14264
14265       Optional dynamic check for size and libcall for large
14266       blocks is emitted here too, with -minline-stringops-dynamically.
14267
14268    2) Prologue: copy first few bytes in order to get destination aligned
14269       to DESIRED_ALIGN.  It is emitted only when ALIGN is less than
14270       DESIRED_ALIGN and and up to DESIRED_ALIGN - ALIGN bytes can be copied.
14271       We emit either a jump tree on power of two sized blocks, or a byte loop.
14272
14273    3) Main body: the copying loop itself, copying in SIZE_NEEDED chunks
14274       with specified algorithm.
14275
14276    4) Epilogue: code copying tail of the block that is too small to be
14277       handled by main body (or up to size guarded by prologue guard).  */
14278    
14279 int
14280 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp,
14281                     rtx expected_align_exp, rtx expected_size_exp)
14282 {
14283   rtx destreg;
14284   rtx srcreg;
14285   rtx label = NULL;
14286   rtx tmp;
14287   rtx jump_around_label = NULL;
14288   HOST_WIDE_INT align = 1;
14289   unsigned HOST_WIDE_INT count = 0;
14290   HOST_WIDE_INT expected_size = -1;
14291   int size_needed = 0, epilogue_size_needed;
14292   int desired_align = 0;
14293   enum stringop_alg alg;
14294   int dynamic_check;
14295
14296   if (CONST_INT_P (align_exp))
14297     align = INTVAL (align_exp);
14298   /* i386 can do misaligned access on reasonably increased cost.  */
14299   if (CONST_INT_P (expected_align_exp)
14300       && INTVAL (expected_align_exp) > align)
14301     align = INTVAL (expected_align_exp);
14302   if (CONST_INT_P (count_exp))
14303     count = expected_size = INTVAL (count_exp);
14304   if (CONST_INT_P (expected_size_exp) && count == 0)
14305     expected_size = INTVAL (expected_size_exp);
14306
14307   /* Step 0: Decide on preferred algorithm, desired alignment and
14308      size of chunks to be copied by main loop.  */
14309
14310   alg = decide_alg (count, expected_size, false, &dynamic_check);
14311   desired_align = decide_alignment (align, alg, expected_size);
14312
14313   if (!TARGET_ALIGN_STRINGOPS)
14314     align = desired_align;
14315
14316   if (alg == libcall)
14317     return 0;
14318   gcc_assert (alg != no_stringop);
14319   if (!count)
14320     count_exp = copy_to_mode_reg (GET_MODE (count_exp), count_exp);
14321   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14322   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
14323   switch (alg)
14324     {
14325     case libcall:
14326     case no_stringop:
14327       gcc_unreachable ();
14328     case loop:
14329       size_needed = GET_MODE_SIZE (Pmode);
14330       break;
14331     case unrolled_loop:
14332       size_needed = GET_MODE_SIZE (Pmode) * (TARGET_64BIT ? 4 : 2);
14333       break;
14334     case rep_prefix_8_byte:
14335       size_needed = 8;
14336       break;
14337     case rep_prefix_4_byte:
14338       size_needed = 4;
14339       break;
14340     case rep_prefix_1_byte:
14341     case loop_1_byte:
14342       size_needed = 1;
14343       break;
14344     }
14345
14346   epilogue_size_needed = size_needed;
14347
14348   /* Step 1: Prologue guard.  */
14349
14350   /* Alignment code needs count to be in register.  */
14351   if (CONST_INT_P (count_exp) && desired_align > align)
14352     {
14353       enum machine_mode mode = SImode;
14354       if (TARGET_64BIT && (count & ~0xffffffff))
14355         mode = DImode;
14356       count_exp = force_reg (mode, count_exp);
14357     }
14358   gcc_assert (desired_align >= 1 && align >= 1);
14359
14360   /* Ensure that alignment prologue won't copy past end of block.  */
14361   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14362     {
14363       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14364       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14365          Make sure it is power of 2.  */
14366       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14367
14368       label = gen_label_rtx ();
14369       emit_cmp_and_jump_insns (count_exp,
14370                                GEN_INT (epilogue_size_needed),
14371                                LTU, 0, counter_mode (count_exp), 1, label);
14372       if (GET_CODE (count_exp) == CONST_INT)
14373         ;
14374       else if (expected_size == -1 || expected_size < epilogue_size_needed)
14375         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14376       else
14377         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14378     }
14379   /* Emit code to decide on runtime whether library call or inline should be
14380      used.  */
14381   if (dynamic_check != -1)
14382     {
14383       rtx hot_label = gen_label_rtx ();
14384       jump_around_label = gen_label_rtx ();
14385       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14386                                LEU, 0, GET_MODE (count_exp), 1, hot_label);
14387       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14388       emit_block_move_via_libcall (dst, src, count_exp, false);
14389       emit_jump (jump_around_label);
14390       emit_label (hot_label);
14391     }
14392
14393   /* Step 2: Alignment prologue.  */
14394
14395   if (desired_align > align)
14396     {
14397       /* Except for the first move in epilogue, we no longer know
14398          constant offset in aliasing info.  It don't seems to worth
14399          the pain to maintain it for the first move, so throw away
14400          the info early.  */
14401       src = change_address (src, BLKmode, srcreg);
14402       dst = change_address (dst, BLKmode, destreg);
14403       expand_movmem_prologue (dst, src, destreg, srcreg, count_exp, align,
14404                               desired_align);
14405     }
14406   if (label && size_needed == 1)
14407     {
14408       emit_label (label);
14409       LABEL_NUSES (label) = 1;
14410       label = NULL;
14411     }
14412
14413   /* Step 3: Main loop.  */
14414
14415   switch (alg)
14416     {
14417     case libcall:
14418     case no_stringop:
14419       gcc_unreachable ();
14420     case loop_1_byte:
14421       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14422                                      count_exp, QImode, 1, expected_size);
14423       break;
14424     case loop:
14425       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14426                                      count_exp, Pmode, 1, expected_size);
14427       break;
14428     case unrolled_loop:
14429       /* Unroll only by factor of 2 in 32bit mode, since we don't have enough
14430          registers for 4 temporaries anyway.  */
14431       expand_set_or_movmem_via_loop (dst, src, destreg, srcreg, NULL,
14432                                      count_exp, Pmode, TARGET_64BIT ? 4 : 2,
14433                                      expected_size);
14434       break;
14435     case rep_prefix_8_byte:
14436       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14437                                  DImode);
14438       break;
14439     case rep_prefix_4_byte:
14440       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14441                                  SImode);
14442       break;
14443     case rep_prefix_1_byte:
14444       expand_movmem_via_rep_mov (dst, src, destreg, srcreg, count_exp,
14445                                  QImode);
14446       break;
14447     }
14448   /* Adjust properly the offset of src and dest memory for aliasing.  */
14449   if (CONST_INT_P (count_exp))
14450     {
14451       src = adjust_automodify_address_nv (src, BLKmode, srcreg,
14452                                           (count / size_needed) * size_needed);
14453       dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14454                                           (count / size_needed) * size_needed);
14455     }
14456   else
14457     {
14458       src = change_address (src, BLKmode, srcreg);
14459       dst = change_address (dst, BLKmode, destreg);
14460     }
14461
14462   /* Step 4: Epilogue to copy the remaining bytes.  */
14463
14464   if (label)
14465     {
14466       /* When the main loop is done, COUNT_EXP might hold original count,
14467          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14468          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14469          bytes. Compensate if needed.  */
14470          
14471       if (size_needed < epilogue_size_needed)
14472         {
14473           tmp =
14474             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14475                                  GEN_INT (size_needed - 1), count_exp, 1,
14476                                  OPTAB_DIRECT);
14477           if (tmp != count_exp)
14478             emit_move_insn (count_exp, tmp);
14479         }
14480       emit_label (label);
14481       LABEL_NUSES (label) = 1;
14482     }
14483
14484   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14485     expand_movmem_epilogue (dst, src, destreg, srcreg, count_exp,
14486                             epilogue_size_needed);
14487   if (jump_around_label)
14488     emit_label (jump_around_label);
14489   return 1;
14490 }
14491
14492 /* Helper function for memcpy.  For QImode value 0xXY produce
14493    0xXYXYXYXY of wide specified by MODE.  This is essentially
14494    a * 0x10101010, but we can do slightly better than
14495    synth_mult by unwinding the sequence by hand on CPUs with
14496    slow multiply.  */
14497 static rtx
14498 promote_duplicated_reg (enum machine_mode mode, rtx val)
14499 {
14500   enum machine_mode valmode = GET_MODE (val);
14501   rtx tmp;
14502   int nops = mode == DImode ? 3 : 2;
14503
14504   gcc_assert (mode == SImode || mode == DImode);
14505   if (val == const0_rtx)
14506     return copy_to_mode_reg (mode, const0_rtx);
14507   if (CONST_INT_P (val))
14508     {
14509       HOST_WIDE_INT v = INTVAL (val) & 255;
14510
14511       v |= v << 8;
14512       v |= v << 16;
14513       if (mode == DImode)
14514         v |= (v << 16) << 16;
14515       return copy_to_mode_reg (mode, gen_int_mode (v, mode));
14516     }
14517
14518   if (valmode == VOIDmode)
14519     valmode = QImode;
14520   if (valmode != QImode)
14521     val = gen_lowpart (QImode, val);
14522   if (mode == QImode)
14523     return val;
14524   if (!TARGET_PARTIAL_REG_STALL)
14525     nops--;
14526   if (ix86_cost->mult_init[mode == DImode ? 3 : 2]
14527       + ix86_cost->mult_bit * (mode == DImode ? 8 : 4)
14528       <= (ix86_cost->shift_const + ix86_cost->add) * nops
14529           + (COSTS_N_INSNS (TARGET_PARTIAL_REG_STALL == 0)))
14530     {
14531       rtx reg = convert_modes (mode, QImode, val, true);
14532       tmp = promote_duplicated_reg (mode, const1_rtx);
14533       return expand_simple_binop (mode, MULT, reg, tmp, NULL, 1,
14534                                   OPTAB_DIRECT);
14535     }
14536   else
14537     {
14538       rtx reg = convert_modes (mode, QImode, val, true);
14539
14540       if (!TARGET_PARTIAL_REG_STALL)
14541         if (mode == SImode)
14542           emit_insn (gen_movsi_insv_1 (reg, reg));
14543         else
14544           emit_insn (gen_movdi_insv_1_rex64 (reg, reg));
14545       else
14546         {
14547           tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (8),
14548                                      NULL, 1, OPTAB_DIRECT);
14549           reg =
14550             expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14551         }
14552       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (16),
14553                                  NULL, 1, OPTAB_DIRECT);
14554       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14555       if (mode == SImode)
14556         return reg;
14557       tmp = expand_simple_binop (mode, ASHIFT, reg, GEN_INT (32),
14558                                  NULL, 1, OPTAB_DIRECT);
14559       reg = expand_simple_binop (mode, IOR, reg, tmp, reg, 1, OPTAB_DIRECT);
14560       return reg;
14561     }
14562 }
14563
14564 /* Duplicate value VAL using promote_duplicated_reg into maximal size that will
14565    be needed by main loop copying SIZE_NEEDED chunks and prologue getting
14566    alignment from ALIGN to DESIRED_ALIGN.  */
14567 static rtx
14568 promote_duplicated_reg_to_size (rtx val, int size_needed, int desired_align, int align)
14569 {
14570   rtx promoted_val;
14571
14572   if (TARGET_64BIT
14573       && (size_needed > 4 || (desired_align > align && desired_align > 4)))
14574     promoted_val = promote_duplicated_reg (DImode, val);
14575   else if (size_needed > 2 || (desired_align > align && desired_align > 2))
14576     promoted_val = promote_duplicated_reg (SImode, val);
14577   else if (size_needed > 1 || (desired_align > align && desired_align > 1))
14578     promoted_val = promote_duplicated_reg (HImode, val);
14579   else
14580     promoted_val = val;
14581
14582   return promoted_val;
14583 }
14584
14585 /* Expand string clear operation (bzero).  Use i386 string operations when
14586    profitable.  See expand_movmem comment for explanation of individual
14587    steps performed.  */
14588 int
14589 ix86_expand_setmem (rtx dst, rtx count_exp, rtx val_exp, rtx align_exp,
14590                     rtx expected_align_exp, rtx expected_size_exp)
14591 {
14592   rtx destreg;
14593   rtx label = NULL;
14594   rtx tmp;
14595   rtx jump_around_label = NULL;
14596   HOST_WIDE_INT align = 1;
14597   unsigned HOST_WIDE_INT count = 0;
14598   HOST_WIDE_INT expected_size = -1;
14599   int size_needed = 0, epilogue_size_needed;
14600   int desired_align = 0;
14601   enum stringop_alg alg;
14602   rtx promoted_val = NULL;
14603   bool force_loopy_epilogue = false;
14604   int dynamic_check;
14605
14606   if (CONST_INT_P (align_exp))
14607     align = INTVAL (align_exp);
14608   /* i386 can do misaligned access on reasonably increased cost.  */
14609   if (CONST_INT_P (expected_align_exp)
14610       && INTVAL (expected_align_exp) > align)
14611     align = INTVAL (expected_align_exp);
14612   if (CONST_INT_P (count_exp))
14613     count = expected_size = INTVAL (count_exp);
14614   if (CONST_INT_P (expected_size_exp) && count == 0)
14615     expected_size = INTVAL (expected_size_exp);
14616
14617   /* Step 0: Decide on preferred algorithm, desired alignment and
14618      size of chunks to be copied by main loop.  */
14619
14620   alg = decide_alg (count, expected_size, true, &dynamic_check);
14621   desired_align = decide_alignment (align, alg, expected_size);
14622
14623   if (!TARGET_ALIGN_STRINGOPS)
14624     align = desired_align;
14625
14626   if (alg == libcall)
14627     return 0;
14628   gcc_assert (alg != no_stringop);
14629   if (!count)
14630     count_exp = copy_to_mode_reg (counter_mode (count_exp), count_exp);
14631   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
14632   switch (alg)
14633     {
14634     case libcall:
14635     case no_stringop:
14636       gcc_unreachable ();
14637     case loop:
14638       size_needed = GET_MODE_SIZE (Pmode);
14639       break;
14640     case unrolled_loop:
14641       size_needed = GET_MODE_SIZE (Pmode) * 4;
14642       break;
14643     case rep_prefix_8_byte:
14644       size_needed = 8;
14645       break;
14646     case rep_prefix_4_byte:
14647       size_needed = 4;
14648       break;
14649     case rep_prefix_1_byte:
14650     case loop_1_byte:
14651       size_needed = 1;
14652       break;
14653     }
14654   epilogue_size_needed = size_needed;
14655
14656   /* Step 1: Prologue guard.  */
14657
14658   /* Alignment code needs count to be in register.  */
14659   if (CONST_INT_P (count_exp) && desired_align > align)
14660     {
14661       enum machine_mode mode = SImode;
14662       if (TARGET_64BIT && (count & ~0xffffffff))
14663         mode = DImode;
14664       count_exp = force_reg (mode, count_exp);
14665     }
14666   /* Do the cheap promotion to allow better CSE across the 
14667      main loop and epilogue (ie one load of the big constant in the
14668      front of all code.  */
14669   if (CONST_INT_P (val_exp))
14670     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14671                                                    desired_align, align);
14672   /* Ensure that alignment prologue won't copy past end of block.  */
14673   if (size_needed > 1 || (desired_align > 1 && desired_align > align))
14674     {
14675       epilogue_size_needed = MAX (size_needed - 1, desired_align - align);
14676       /* Epilogue always copies COUNT_EXP & EPILOGUE_SIZE_NEEDED bytes.
14677          Make sure it is power of 2.  */
14678       epilogue_size_needed = smallest_pow2_greater_than (epilogue_size_needed);
14679
14680       /* To improve performance of small blocks, we jump around the VAL
14681          promoting mode.  This mean that if the promoted VAL is not constant,
14682          we might not use it in the epilogue and have to use byte
14683          loop variant.  */
14684       if (epilogue_size_needed > 2 && !promoted_val)
14685         force_loopy_epilogue = true;
14686       label = gen_label_rtx ();
14687       emit_cmp_and_jump_insns (count_exp,
14688                                GEN_INT (epilogue_size_needed),
14689                                LTU, 0, counter_mode (count_exp), 1, label);
14690       if (GET_CODE (count_exp) == CONST_INT)
14691         ;
14692       else if (expected_size == -1 || expected_size <= epilogue_size_needed)
14693         predict_jump (REG_BR_PROB_BASE * 60 / 100);
14694       else
14695         predict_jump (REG_BR_PROB_BASE * 20 / 100);
14696     }
14697   if (dynamic_check != -1)
14698     {
14699       rtx hot_label = gen_label_rtx ();
14700       jump_around_label = gen_label_rtx ();
14701       emit_cmp_and_jump_insns (count_exp, GEN_INT (dynamic_check - 1),
14702                                LEU, 0, counter_mode (count_exp), 1, hot_label);
14703       predict_jump (REG_BR_PROB_BASE * 90 / 100);
14704       set_storage_via_libcall (dst, count_exp, val_exp, false);
14705       emit_jump (jump_around_label);
14706       emit_label (hot_label);
14707     }
14708
14709   /* Step 2: Alignment prologue.  */
14710
14711   /* Do the expensive promotion once we branched off the small blocks.  */
14712   if (!promoted_val)
14713     promoted_val = promote_duplicated_reg_to_size (val_exp, size_needed,
14714                                                    desired_align, align);
14715   gcc_assert (desired_align >= 1 && align >= 1);
14716
14717   if (desired_align > align)
14718     {
14719       /* Except for the first move in epilogue, we no longer know
14720          constant offset in aliasing info.  It don't seems to worth
14721          the pain to maintain it for the first move, so throw away
14722          the info early.  */
14723       dst = change_address (dst, BLKmode, destreg);
14724       expand_setmem_prologue (dst, destreg, promoted_val, count_exp, align,
14725                               desired_align);
14726     }
14727   if (label && size_needed == 1)
14728     {
14729       emit_label (label);
14730       LABEL_NUSES (label) = 1;
14731       label = NULL;
14732     }
14733
14734   /* Step 3: Main loop.  */
14735
14736   switch (alg)
14737     {
14738     case libcall:
14739     case no_stringop:
14740       gcc_unreachable ();
14741     case loop_1_byte:
14742       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14743                                      count_exp, QImode, 1, expected_size);
14744       break;
14745     case loop:
14746       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14747                                      count_exp, Pmode, 1, expected_size);
14748       break;
14749     case unrolled_loop:
14750       expand_set_or_movmem_via_loop (dst, NULL, destreg, NULL, promoted_val,
14751                                      count_exp, Pmode, 4, expected_size);
14752       break;
14753     case rep_prefix_8_byte:
14754       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14755                                   DImode);
14756       break;
14757     case rep_prefix_4_byte:
14758       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14759                                   SImode);
14760       break;
14761     case rep_prefix_1_byte:
14762       expand_setmem_via_rep_stos (dst, destreg, promoted_val, count_exp,
14763                                   QImode);
14764       break;
14765     }
14766   /* Adjust properly the offset of src and dest memory for aliasing.  */
14767   if (CONST_INT_P (count_exp))
14768     dst = adjust_automodify_address_nv (dst, BLKmode, destreg,
14769                                         (count / size_needed) * size_needed);
14770   else
14771     dst = change_address (dst, BLKmode, destreg);
14772
14773   /* Step 4: Epilogue to copy the remaining bytes.  */
14774
14775   if (label)
14776     {
14777       /* When the main loop is done, COUNT_EXP might hold original count,
14778          while we want to copy only COUNT_EXP & SIZE_NEEDED bytes.
14779          Epilogue code will actually copy COUNT_EXP & EPILOGUE_SIZE_NEEDED
14780          bytes. Compensate if needed.  */
14781
14782       if (size_needed < desired_align - align)
14783         {
14784           tmp =
14785             expand_simple_binop (counter_mode (count_exp), AND, count_exp,
14786                                  GEN_INT (size_needed - 1), count_exp, 1,
14787                                  OPTAB_DIRECT);
14788           size_needed = desired_align - align + 1;
14789           if (tmp != count_exp)
14790             emit_move_insn (count_exp, tmp);
14791         }
14792       emit_label (label);
14793       LABEL_NUSES (label) = 1;
14794     }
14795   if (count_exp != const0_rtx && epilogue_size_needed > 1)
14796     {
14797       if (force_loopy_epilogue)
14798         expand_setmem_epilogue_via_loop (dst, destreg, val_exp, count_exp,
14799                                          size_needed);
14800       else
14801         expand_setmem_epilogue (dst, destreg, promoted_val, count_exp,
14802                                 size_needed);
14803     }
14804   if (jump_around_label)
14805     emit_label (jump_around_label);
14806   return 1;
14807 }
14808
14809 /* Expand the appropriate insns for doing strlen if not just doing
14810    repnz; scasb
14811
14812    out = result, initialized with the start address
14813    align_rtx = alignment of the address.
14814    scratch = scratch register, initialized with the startaddress when
14815         not aligned, otherwise undefined
14816
14817    This is just the body. It needs the initializations mentioned above and
14818    some address computing at the end.  These things are done in i386.md.  */
14819
14820 static void
14821 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
14822 {
14823   int align;
14824   rtx tmp;
14825   rtx align_2_label = NULL_RTX;
14826   rtx align_3_label = NULL_RTX;
14827   rtx align_4_label = gen_label_rtx ();
14828   rtx end_0_label = gen_label_rtx ();
14829   rtx mem;
14830   rtx tmpreg = gen_reg_rtx (SImode);
14831   rtx scratch = gen_reg_rtx (SImode);
14832   rtx cmp;
14833
14834   align = 0;
14835   if (CONST_INT_P (align_rtx))
14836     align = INTVAL (align_rtx);
14837
14838   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
14839
14840   /* Is there a known alignment and is it less than 4?  */
14841   if (align < 4)
14842     {
14843       rtx scratch1 = gen_reg_rtx (Pmode);
14844       emit_move_insn (scratch1, out);
14845       /* Is there a known alignment and is it not 2? */
14846       if (align != 2)
14847         {
14848           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
14849           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
14850
14851           /* Leave just the 3 lower bits.  */
14852           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
14853                                     NULL_RTX, 0, OPTAB_WIDEN);
14854
14855           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14856                                    Pmode, 1, align_4_label);
14857           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
14858                                    Pmode, 1, align_2_label);
14859           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
14860                                    Pmode, 1, align_3_label);
14861         }
14862       else
14863         {
14864           /* Since the alignment is 2, we have to check 2 or 0 bytes;
14865              check if is aligned to 4 - byte.  */
14866
14867           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
14868                                     NULL_RTX, 0, OPTAB_WIDEN);
14869
14870           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
14871                                    Pmode, 1, align_4_label);
14872         }
14873
14874       mem = change_address (src, QImode, out);
14875
14876       /* Now compare the bytes.  */
14877
14878       /* Compare the first n unaligned byte on a byte per byte basis.  */
14879       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
14880                                QImode, 1, end_0_label);
14881
14882       /* Increment the address.  */
14883       if (TARGET_64BIT)
14884         emit_insn (gen_adddi3 (out, out, const1_rtx));
14885       else
14886         emit_insn (gen_addsi3 (out, out, const1_rtx));
14887
14888       /* Not needed with an alignment of 2 */
14889       if (align != 2)
14890         {
14891           emit_label (align_2_label);
14892
14893           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14894                                    end_0_label);
14895
14896           if (TARGET_64BIT)
14897             emit_insn (gen_adddi3 (out, out, const1_rtx));
14898           else
14899             emit_insn (gen_addsi3 (out, out, const1_rtx));
14900
14901           emit_label (align_3_label);
14902         }
14903
14904       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
14905                                end_0_label);
14906
14907       if (TARGET_64BIT)
14908         emit_insn (gen_adddi3 (out, out, const1_rtx));
14909       else
14910         emit_insn (gen_addsi3 (out, out, const1_rtx));
14911     }
14912
14913   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
14914      align this loop.  It gives only huge programs, but does not help to
14915      speed up.  */
14916   emit_label (align_4_label);
14917
14918   mem = change_address (src, SImode, out);
14919   emit_move_insn (scratch, mem);
14920   if (TARGET_64BIT)
14921     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
14922   else
14923     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
14924
14925   /* This formula yields a nonzero result iff one of the bytes is zero.
14926      This saves three branches inside loop and many cycles.  */
14927
14928   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
14929   emit_insn (gen_one_cmplsi2 (scratch, scratch));
14930   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
14931   emit_insn (gen_andsi3 (tmpreg, tmpreg,
14932                          gen_int_mode (0x80808080, SImode)));
14933   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
14934                            align_4_label);
14935
14936   if (TARGET_CMOVE)
14937     {
14938        rtx reg = gen_reg_rtx (SImode);
14939        rtx reg2 = gen_reg_rtx (Pmode);
14940        emit_move_insn (reg, tmpreg);
14941        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
14942
14943        /* If zero is not in the first two bytes, move two bytes forward.  */
14944        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14945        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14946        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14947        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
14948                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
14949                                                      reg,
14950                                                      tmpreg)));
14951        /* Emit lea manually to avoid clobbering of flags.  */
14952        emit_insn (gen_rtx_SET (SImode, reg2,
14953                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
14954
14955        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14956        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
14957        emit_insn (gen_rtx_SET (VOIDmode, out,
14958                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
14959                                                      reg2,
14960                                                      out)));
14961
14962     }
14963   else
14964     {
14965        rtx end_2_label = gen_label_rtx ();
14966        /* Is zero in the first two bytes? */
14967
14968        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
14969        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
14970        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
14971        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
14972                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
14973                             pc_rtx);
14974        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
14975        JUMP_LABEL (tmp) = end_2_label;
14976
14977        /* Not in the first two.  Move two bytes forward.  */
14978        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
14979        if (TARGET_64BIT)
14980          emit_insn (gen_adddi3 (out, out, const2_rtx));
14981        else
14982          emit_insn (gen_addsi3 (out, out, const2_rtx));
14983
14984        emit_label (end_2_label);
14985
14986     }
14987
14988   /* Avoid branch in fixing the byte.  */
14989   tmpreg = gen_lowpart (QImode, tmpreg);
14990   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
14991   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
14992   if (TARGET_64BIT)
14993     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
14994   else
14995     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
14996
14997   emit_label (end_0_label);
14998 }
14999
15000 /* Expand strlen.  */
15001
15002 int
15003 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
15004 {
15005   rtx addr, scratch1, scratch2, scratch3, scratch4;
15006
15007   /* The generic case of strlen expander is long.  Avoid it's
15008      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
15009
15010   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
15011       && !TARGET_INLINE_ALL_STRINGOPS
15012       && !optimize_size
15013       && (!CONST_INT_P (align) || INTVAL (align) < 4))
15014     return 0;
15015
15016   addr = force_reg (Pmode, XEXP (src, 0));
15017   scratch1 = gen_reg_rtx (Pmode);
15018
15019   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
15020       && !optimize_size)
15021     {
15022       /* Well it seems that some optimizer does not combine a call like
15023          foo(strlen(bar), strlen(bar));
15024          when the move and the subtraction is done here.  It does calculate
15025          the length just once when these instructions are done inside of
15026          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
15027          often used and I use one fewer register for the lifetime of
15028          output_strlen_unroll() this is better.  */
15029
15030       emit_move_insn (out, addr);
15031
15032       ix86_expand_strlensi_unroll_1 (out, src, align);
15033
15034       /* strlensi_unroll_1 returns the address of the zero at the end of
15035          the string, like memchr(), so compute the length by subtracting
15036          the start address.  */
15037       if (TARGET_64BIT)
15038         emit_insn (gen_subdi3 (out, out, addr));
15039       else
15040         emit_insn (gen_subsi3 (out, out, addr));
15041     }
15042   else
15043     {
15044       rtx unspec;
15045       scratch2 = gen_reg_rtx (Pmode);
15046       scratch3 = gen_reg_rtx (Pmode);
15047       scratch4 = force_reg (Pmode, constm1_rtx);
15048
15049       emit_move_insn (scratch3, addr);
15050       eoschar = force_reg (QImode, eoschar);
15051
15052       src = replace_equiv_address_nv (src, scratch3);
15053
15054       /* If .md starts supporting :P, this can be done in .md.  */
15055       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
15056                                                  scratch4), UNSPEC_SCAS);
15057       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
15058       if (TARGET_64BIT)
15059         {
15060           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
15061           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
15062         }
15063       else
15064         {
15065           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
15066           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
15067         }
15068     }
15069   return 1;
15070 }
15071
15072 /* For given symbol (function) construct code to compute address of it's PLT
15073    entry in large x86-64 PIC model.  */
15074 rtx
15075 construct_plt_address (rtx symbol)
15076 {
15077   rtx tmp = gen_reg_rtx (Pmode);
15078   rtx unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, symbol), UNSPEC_PLTOFF);
15079
15080   gcc_assert (GET_CODE (symbol) == SYMBOL_REF);
15081   gcc_assert (ix86_cmodel == CM_LARGE_PIC);
15082
15083   emit_move_insn (tmp, gen_rtx_CONST (Pmode, unspec));
15084   emit_insn (gen_adddi3 (tmp, tmp, pic_offset_table_rtx));
15085   return tmp;
15086 }
15087
15088 void
15089 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
15090                   rtx callarg2 ATTRIBUTE_UNUSED,
15091                   rtx pop, int sibcall)
15092 {
15093   rtx use = NULL, call;
15094
15095   if (pop == const0_rtx)
15096     pop = NULL;
15097   gcc_assert (!TARGET_64BIT || !pop);
15098
15099   if (TARGET_MACHO && !TARGET_64BIT)
15100     {
15101 #if TARGET_MACHO
15102       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
15103         fnaddr = machopic_indirect_call_target (fnaddr);
15104 #endif
15105     }
15106   else
15107     {
15108       /* Static functions and indirect calls don't need the pic register.  */
15109       if (flag_pic && (!TARGET_64BIT || ix86_cmodel == CM_LARGE_PIC)
15110           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
15111           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
15112         use_reg (&use, pic_offset_table_rtx);
15113     }
15114
15115   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
15116     {
15117       rtx al = gen_rtx_REG (QImode, 0);
15118       emit_move_insn (al, callarg2);
15119       use_reg (&use, al);
15120     }
15121
15122   if (ix86_cmodel == CM_LARGE_PIC
15123       && GET_CODE (fnaddr) == MEM
15124       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
15125       && !local_symbolic_operand (XEXP (fnaddr, 0), VOIDmode))
15126     fnaddr = gen_rtx_MEM (QImode, construct_plt_address (XEXP (fnaddr, 0)));
15127   else if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
15128     {
15129       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
15130       fnaddr = gen_rtx_MEM (QImode, fnaddr);
15131     }
15132   if (sibcall && TARGET_64BIT
15133       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
15134     {
15135       rtx addr;
15136       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
15137       fnaddr = gen_rtx_REG (Pmode, R11_REG);
15138       emit_move_insn (fnaddr, addr);
15139       fnaddr = gen_rtx_MEM (QImode, fnaddr);
15140     }
15141
15142   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
15143   if (retval)
15144     call = gen_rtx_SET (VOIDmode, retval, call);
15145   if (pop)
15146     {
15147       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
15148       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
15149       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
15150     }
15151
15152   call = emit_call_insn (call);
15153   if (use)
15154     CALL_INSN_FUNCTION_USAGE (call) = use;
15155 }
15156
15157 \f
15158 /* Clear stack slot assignments remembered from previous functions.
15159    This is called from INIT_EXPANDERS once before RTL is emitted for each
15160    function.  */
15161
15162 static struct machine_function *
15163 ix86_init_machine_status (void)
15164 {
15165   struct machine_function *f;
15166
15167   f = ggc_alloc_cleared (sizeof (struct machine_function));
15168   f->use_fast_prologue_epilogue_nregs = -1;
15169   f->tls_descriptor_call_expanded_p = 0;
15170
15171   return f;
15172 }
15173
15174 /* Return a MEM corresponding to a stack slot with mode MODE.
15175    Allocate a new slot if necessary.
15176
15177    The RTL for a function can have several slots available: N is
15178    which slot to use.  */
15179
15180 rtx
15181 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
15182 {
15183   struct stack_local_entry *s;
15184
15185   gcc_assert (n < MAX_386_STACK_LOCALS);
15186
15187   for (s = ix86_stack_locals; s; s = s->next)
15188     if (s->mode == mode && s->n == n)
15189       return copy_rtx (s->rtl);
15190
15191   s = (struct stack_local_entry *)
15192     ggc_alloc (sizeof (struct stack_local_entry));
15193   s->n = n;
15194   s->mode = mode;
15195   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
15196
15197   s->next = ix86_stack_locals;
15198   ix86_stack_locals = s;
15199   return s->rtl;
15200 }
15201
15202 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
15203
15204 static GTY(()) rtx ix86_tls_symbol;
15205 rtx
15206 ix86_tls_get_addr (void)
15207 {
15208
15209   if (!ix86_tls_symbol)
15210     {
15211       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
15212                                             (TARGET_ANY_GNU_TLS
15213                                              && !TARGET_64BIT)
15214                                             ? "___tls_get_addr"
15215                                             : "__tls_get_addr");
15216     }
15217
15218   return ix86_tls_symbol;
15219 }
15220
15221 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
15222
15223 static GTY(()) rtx ix86_tls_module_base_symbol;
15224 rtx
15225 ix86_tls_module_base (void)
15226 {
15227
15228   if (!ix86_tls_module_base_symbol)
15229     {
15230       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
15231                                                         "_TLS_MODULE_BASE_");
15232       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
15233         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
15234     }
15235
15236   return ix86_tls_module_base_symbol;
15237 }
15238 \f
15239 /* Calculate the length of the memory address in the instruction
15240    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
15241
15242 int
15243 memory_address_length (rtx addr)
15244 {
15245   struct ix86_address parts;
15246   rtx base, index, disp;
15247   int len;
15248   int ok;
15249
15250   if (GET_CODE (addr) == PRE_DEC
15251       || GET_CODE (addr) == POST_INC
15252       || GET_CODE (addr) == PRE_MODIFY
15253       || GET_CODE (addr) == POST_MODIFY)
15254     return 0;
15255
15256   ok = ix86_decompose_address (addr, &parts);
15257   gcc_assert (ok);
15258
15259   if (parts.base && GET_CODE (parts.base) == SUBREG)
15260     parts.base = SUBREG_REG (parts.base);
15261   if (parts.index && GET_CODE (parts.index) == SUBREG)
15262     parts.index = SUBREG_REG (parts.index);
15263
15264   base = parts.base;
15265   index = parts.index;
15266   disp = parts.disp;
15267   len = 0;
15268
15269   /* Rule of thumb:
15270        - esp as the base always wants an index,
15271        - ebp as the base always wants a displacement.  */
15272
15273   /* Register Indirect.  */
15274   if (base && !index && !disp)
15275     {
15276       /* esp (for its index) and ebp (for its displacement) need
15277          the two-byte modrm form.  */
15278       if (addr == stack_pointer_rtx
15279           || addr == arg_pointer_rtx
15280           || addr == frame_pointer_rtx
15281           || addr == hard_frame_pointer_rtx)
15282         len = 1;
15283     }
15284
15285   /* Direct Addressing.  */
15286   else if (disp && !base && !index)
15287     len = 4;
15288
15289   else
15290     {
15291       /* Find the length of the displacement constant.  */
15292       if (disp)
15293         {
15294           if (base && satisfies_constraint_K (disp))
15295             len = 1;
15296           else
15297             len = 4;
15298         }
15299       /* ebp always wants a displacement.  */
15300       else if (base == hard_frame_pointer_rtx)
15301         len = 1;
15302
15303       /* An index requires the two-byte modrm form....  */
15304       if (index
15305           /* ...like esp, which always wants an index.  */
15306           || base == stack_pointer_rtx
15307           || base == arg_pointer_rtx
15308           || base == frame_pointer_rtx)
15309         len += 1;
15310     }
15311
15312   return len;
15313 }
15314
15315 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
15316    is set, expect that insn have 8bit immediate alternative.  */
15317 int
15318 ix86_attr_length_immediate_default (rtx insn, int shortform)
15319 {
15320   int len = 0;
15321   int i;
15322   extract_insn_cached (insn);
15323   for (i = recog_data.n_operands - 1; i >= 0; --i)
15324     if (CONSTANT_P (recog_data.operand[i]))
15325       {
15326         gcc_assert (!len);
15327         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
15328           len = 1;
15329         else
15330           {
15331             switch (get_attr_mode (insn))
15332               {
15333                 case MODE_QI:
15334                   len+=1;
15335                   break;
15336                 case MODE_HI:
15337                   len+=2;
15338                   break;
15339                 case MODE_SI:
15340                   len+=4;
15341                   break;
15342                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
15343                 case MODE_DI:
15344                   len+=4;
15345                   break;
15346                 default:
15347                   fatal_insn ("unknown insn mode", insn);
15348               }
15349           }
15350       }
15351   return len;
15352 }
15353 /* Compute default value for "length_address" attribute.  */
15354 int
15355 ix86_attr_length_address_default (rtx insn)
15356 {
15357   int i;
15358
15359   if (get_attr_type (insn) == TYPE_LEA)
15360     {
15361       rtx set = PATTERN (insn);
15362
15363       if (GET_CODE (set) == PARALLEL)
15364         set = XVECEXP (set, 0, 0);
15365
15366       gcc_assert (GET_CODE (set) == SET);
15367
15368       return memory_address_length (SET_SRC (set));
15369     }
15370
15371   extract_insn_cached (insn);
15372   for (i = recog_data.n_operands - 1; i >= 0; --i)
15373     if (MEM_P (recog_data.operand[i]))
15374       {
15375         return memory_address_length (XEXP (recog_data.operand[i], 0));
15376         break;
15377       }
15378   return 0;
15379 }
15380 \f
15381 /* Return the maximum number of instructions a cpu can issue.  */
15382
15383 static int
15384 ix86_issue_rate (void)
15385 {
15386   switch (ix86_tune)
15387     {
15388     case PROCESSOR_PENTIUM:
15389     case PROCESSOR_K6:
15390       return 2;
15391
15392     case PROCESSOR_PENTIUMPRO:
15393     case PROCESSOR_PENTIUM4:
15394     case PROCESSOR_ATHLON:
15395     case PROCESSOR_K8:
15396     case PROCESSOR_AMDFAM10:
15397     case PROCESSOR_NOCONA:
15398     case PROCESSOR_GENERIC32:
15399     case PROCESSOR_GENERIC64:
15400       return 3;
15401
15402     case PROCESSOR_CORE2:
15403       return 4;
15404
15405     default:
15406       return 1;
15407     }
15408 }
15409
15410 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
15411    by DEP_INSN and nothing set by DEP_INSN.  */
15412
15413 static int
15414 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15415 {
15416   rtx set, set2;
15417
15418   /* Simplify the test for uninteresting insns.  */
15419   if (insn_type != TYPE_SETCC
15420       && insn_type != TYPE_ICMOV
15421       && insn_type != TYPE_FCMOV
15422       && insn_type != TYPE_IBR)
15423     return 0;
15424
15425   if ((set = single_set (dep_insn)) != 0)
15426     {
15427       set = SET_DEST (set);
15428       set2 = NULL_RTX;
15429     }
15430   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
15431            && XVECLEN (PATTERN (dep_insn), 0) == 2
15432            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
15433            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
15434     {
15435       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15436       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
15437     }
15438   else
15439     return 0;
15440
15441   if (!REG_P (set) || REGNO (set) != FLAGS_REG)
15442     return 0;
15443
15444   /* This test is true if the dependent insn reads the flags but
15445      not any other potentially set register.  */
15446   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
15447     return 0;
15448
15449   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
15450     return 0;
15451
15452   return 1;
15453 }
15454
15455 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
15456    address with operands set by DEP_INSN.  */
15457
15458 static int
15459 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
15460 {
15461   rtx addr;
15462
15463   if (insn_type == TYPE_LEA
15464       && TARGET_PENTIUM)
15465     {
15466       addr = PATTERN (insn);
15467
15468       if (GET_CODE (addr) == PARALLEL)
15469         addr = XVECEXP (addr, 0, 0);
15470
15471       gcc_assert (GET_CODE (addr) == SET);
15472
15473       addr = SET_SRC (addr);
15474     }
15475   else
15476     {
15477       int i;
15478       extract_insn_cached (insn);
15479       for (i = recog_data.n_operands - 1; i >= 0; --i)
15480         if (MEM_P (recog_data.operand[i]))
15481           {
15482             addr = XEXP (recog_data.operand[i], 0);
15483             goto found;
15484           }
15485       return 0;
15486     found:;
15487     }
15488
15489   return modified_in_p (addr, dep_insn);
15490 }
15491
15492 static int
15493 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
15494 {
15495   enum attr_type insn_type, dep_insn_type;
15496   enum attr_memory memory;
15497   rtx set, set2;
15498   int dep_insn_code_number;
15499
15500   /* Anti and output dependencies have zero cost on all CPUs.  */
15501   if (REG_NOTE_KIND (link) != 0)
15502     return 0;
15503
15504   dep_insn_code_number = recog_memoized (dep_insn);
15505
15506   /* If we can't recognize the insns, we can't really do anything.  */
15507   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
15508     return cost;
15509
15510   insn_type = get_attr_type (insn);
15511   dep_insn_type = get_attr_type (dep_insn);
15512
15513   switch (ix86_tune)
15514     {
15515     case PROCESSOR_PENTIUM:
15516       /* Address Generation Interlock adds a cycle of latency.  */
15517       if (ix86_agi_dependent (insn, dep_insn, insn_type))
15518         cost += 1;
15519
15520       /* ??? Compares pair with jump/setcc.  */
15521       if (ix86_flags_dependent (insn, dep_insn, insn_type))
15522         cost = 0;
15523
15524       /* Floating point stores require value to be ready one cycle earlier.  */
15525       if (insn_type == TYPE_FMOV
15526           && get_attr_memory (insn) == MEMORY_STORE
15527           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15528         cost += 1;
15529       break;
15530
15531     case PROCESSOR_PENTIUMPRO:
15532       memory = get_attr_memory (insn);
15533
15534       /* INT->FP conversion is expensive.  */
15535       if (get_attr_fp_int_src (dep_insn))
15536         cost += 5;
15537
15538       /* There is one cycle extra latency between an FP op and a store.  */
15539       if (insn_type == TYPE_FMOV
15540           && (set = single_set (dep_insn)) != NULL_RTX
15541           && (set2 = single_set (insn)) != NULL_RTX
15542           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
15543           && MEM_P (SET_DEST (set2)))
15544         cost += 1;
15545
15546       /* Show ability of reorder buffer to hide latency of load by executing
15547          in parallel with previous instruction in case
15548          previous instruction is not needed to compute the address.  */
15549       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15550           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15551         {
15552           /* Claim moves to take one cycle, as core can issue one load
15553              at time and the next load can start cycle later.  */
15554           if (dep_insn_type == TYPE_IMOV
15555               || dep_insn_type == TYPE_FMOV)
15556             cost = 1;
15557           else if (cost > 1)
15558             cost--;
15559         }
15560       break;
15561
15562     case PROCESSOR_K6:
15563       memory = get_attr_memory (insn);
15564
15565       /* The esp dependency is resolved before the instruction is really
15566          finished.  */
15567       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
15568           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
15569         return 1;
15570
15571       /* INT->FP conversion is expensive.  */
15572       if (get_attr_fp_int_src (dep_insn))
15573         cost += 5;
15574
15575       /* Show ability of reorder buffer to hide latency of load by executing
15576          in parallel with previous instruction in case
15577          previous instruction is not needed to compute the address.  */
15578       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15579           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15580         {
15581           /* Claim moves to take one cycle, as core can issue one load
15582              at time and the next load can start cycle later.  */
15583           if (dep_insn_type == TYPE_IMOV
15584               || dep_insn_type == TYPE_FMOV)
15585             cost = 1;
15586           else if (cost > 2)
15587             cost -= 2;
15588           else
15589             cost = 1;
15590         }
15591       break;
15592
15593     case PROCESSOR_ATHLON:
15594     case PROCESSOR_K8:
15595     case PROCESSOR_AMDFAM10:
15596     case PROCESSOR_GENERIC32:
15597     case PROCESSOR_GENERIC64:
15598       memory = get_attr_memory (insn);
15599
15600       /* Show ability of reorder buffer to hide latency of load by executing
15601          in parallel with previous instruction in case
15602          previous instruction is not needed to compute the address.  */
15603       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
15604           && !ix86_agi_dependent (insn, dep_insn, insn_type))
15605         {
15606           enum attr_unit unit = get_attr_unit (insn);
15607           int loadcost = 3;
15608
15609           /* Because of the difference between the length of integer and
15610              floating unit pipeline preparation stages, the memory operands
15611              for floating point are cheaper.
15612
15613              ??? For Athlon it the difference is most probably 2.  */
15614           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
15615             loadcost = 3;
15616           else
15617             loadcost = TARGET_ATHLON ? 2 : 0;
15618
15619           if (cost >= loadcost)
15620             cost -= loadcost;
15621           else
15622             cost = 0;
15623         }
15624
15625     default:
15626       break;
15627     }
15628
15629   return cost;
15630 }
15631
15632 /* How many alternative schedules to try.  This should be as wide as the
15633    scheduling freedom in the DFA, but no wider.  Making this value too
15634    large results extra work for the scheduler.  */
15635
15636 static int
15637 ia32_multipass_dfa_lookahead (void)
15638 {
15639   if (ix86_tune == PROCESSOR_PENTIUM)
15640     return 2;
15641
15642   if (ix86_tune == PROCESSOR_PENTIUMPRO
15643       || ix86_tune == PROCESSOR_K6)
15644     return 1;
15645
15646   else
15647     return 0;
15648 }
15649
15650 \f
15651 /* Compute the alignment given to a constant that is being placed in memory.
15652    EXP is the constant and ALIGN is the alignment that the object would
15653    ordinarily have.
15654    The value of this function is used instead of that alignment to align
15655    the object.  */
15656
15657 int
15658 ix86_constant_alignment (tree exp, int align)
15659 {
15660   if (TREE_CODE (exp) == REAL_CST)
15661     {
15662       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
15663         return 64;
15664       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
15665         return 128;
15666     }
15667   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
15668            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
15669     return BITS_PER_WORD;
15670
15671   return align;
15672 }
15673
15674 /* Compute the alignment for a static variable.
15675    TYPE is the data type, and ALIGN is the alignment that
15676    the object would ordinarily have.  The value of this function is used
15677    instead of that alignment to align the object.  */
15678
15679 int
15680 ix86_data_alignment (tree type, int align)
15681 {
15682   int max_align = optimize_size ? BITS_PER_WORD : MIN (256, MAX_OFILE_ALIGNMENT);
15683
15684   if (AGGREGATE_TYPE_P (type)
15685       && TYPE_SIZE (type)
15686       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15687       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
15688           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
15689       && align < max_align)
15690     align = max_align;
15691
15692   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15693      to 16byte boundary.  */
15694   if (TARGET_64BIT)
15695     {
15696       if (AGGREGATE_TYPE_P (type)
15697            && TYPE_SIZE (type)
15698            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15699            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
15700                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15701         return 128;
15702     }
15703
15704   if (TREE_CODE (type) == ARRAY_TYPE)
15705     {
15706       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15707         return 64;
15708       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15709         return 128;
15710     }
15711   else if (TREE_CODE (type) == COMPLEX_TYPE)
15712     {
15713
15714       if (TYPE_MODE (type) == DCmode && align < 64)
15715         return 64;
15716       if (TYPE_MODE (type) == XCmode && align < 128)
15717         return 128;
15718     }
15719   else if ((TREE_CODE (type) == RECORD_TYPE
15720             || TREE_CODE (type) == UNION_TYPE
15721             || TREE_CODE (type) == QUAL_UNION_TYPE)
15722            && TYPE_FIELDS (type))
15723     {
15724       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15725         return 64;
15726       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15727         return 128;
15728     }
15729   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15730            || TREE_CODE (type) == INTEGER_TYPE)
15731     {
15732       if (TYPE_MODE (type) == DFmode && align < 64)
15733         return 64;
15734       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15735         return 128;
15736     }
15737
15738   return align;
15739 }
15740
15741 /* Compute the alignment for a local variable.
15742    TYPE is the data type, and ALIGN is the alignment that
15743    the object would ordinarily have.  The value of this macro is used
15744    instead of that alignment to align the object.  */
15745
15746 int
15747 ix86_local_alignment (tree type, int align)
15748 {
15749   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
15750      to 16byte boundary.  */
15751   if (TARGET_64BIT)
15752     {
15753       if (AGGREGATE_TYPE_P (type)
15754            && TYPE_SIZE (type)
15755            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
15756            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
15757                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
15758         return 128;
15759     }
15760   if (TREE_CODE (type) == ARRAY_TYPE)
15761     {
15762       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
15763         return 64;
15764       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
15765         return 128;
15766     }
15767   else if (TREE_CODE (type) == COMPLEX_TYPE)
15768     {
15769       if (TYPE_MODE (type) == DCmode && align < 64)
15770         return 64;
15771       if (TYPE_MODE (type) == XCmode && align < 128)
15772         return 128;
15773     }
15774   else if ((TREE_CODE (type) == RECORD_TYPE
15775             || TREE_CODE (type) == UNION_TYPE
15776             || TREE_CODE (type) == QUAL_UNION_TYPE)
15777            && TYPE_FIELDS (type))
15778     {
15779       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
15780         return 64;
15781       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
15782         return 128;
15783     }
15784   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
15785            || TREE_CODE (type) == INTEGER_TYPE)
15786     {
15787
15788       if (TYPE_MODE (type) == DFmode && align < 64)
15789         return 64;
15790       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
15791         return 128;
15792     }
15793   return align;
15794 }
15795 \f
15796 /* Emit RTL insns to initialize the variable parts of a trampoline.
15797    FNADDR is an RTX for the address of the function's pure code.
15798    CXT is an RTX for the static chain value for the function.  */
15799 void
15800 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
15801 {
15802   if (!TARGET_64BIT)
15803     {
15804       /* Compute offset from the end of the jmp to the target function.  */
15805       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
15806                                plus_constant (tramp, 10),
15807                                NULL_RTX, 1, OPTAB_DIRECT);
15808       emit_move_insn (gen_rtx_MEM (QImode, tramp),
15809                       gen_int_mode (0xb9, QImode));
15810       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
15811       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
15812                       gen_int_mode (0xe9, QImode));
15813       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
15814     }
15815   else
15816     {
15817       int offset = 0;
15818       /* Try to load address using shorter movl instead of movabs.
15819          We may want to support movq for kernel mode, but kernel does not use
15820          trampolines at the moment.  */
15821       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
15822         {
15823           fnaddr = copy_to_mode_reg (DImode, fnaddr);
15824           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15825                           gen_int_mode (0xbb41, HImode));
15826           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
15827                           gen_lowpart (SImode, fnaddr));
15828           offset += 6;
15829         }
15830       else
15831         {
15832           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15833                           gen_int_mode (0xbb49, HImode));
15834           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15835                           fnaddr);
15836           offset += 10;
15837         }
15838       /* Load static chain using movabs to r10.  */
15839       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15840                       gen_int_mode (0xba49, HImode));
15841       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
15842                       cxt);
15843       offset += 10;
15844       /* Jump to the r11 */
15845       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
15846                       gen_int_mode (0xff49, HImode));
15847       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
15848                       gen_int_mode (0xe3, QImode));
15849       offset += 3;
15850       gcc_assert (offset <= TRAMPOLINE_SIZE);
15851     }
15852
15853 #ifdef ENABLE_EXECUTE_STACK
15854   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
15855                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
15856 #endif
15857 }
15858 \f
15859 /* Codes for all the SSE/MMX builtins.  */
15860 enum ix86_builtins
15861 {
15862   IX86_BUILTIN_ADDPS,
15863   IX86_BUILTIN_ADDSS,
15864   IX86_BUILTIN_DIVPS,
15865   IX86_BUILTIN_DIVSS,
15866   IX86_BUILTIN_MULPS,
15867   IX86_BUILTIN_MULSS,
15868   IX86_BUILTIN_SUBPS,
15869   IX86_BUILTIN_SUBSS,
15870
15871   IX86_BUILTIN_CMPEQPS,
15872   IX86_BUILTIN_CMPLTPS,
15873   IX86_BUILTIN_CMPLEPS,
15874   IX86_BUILTIN_CMPGTPS,
15875   IX86_BUILTIN_CMPGEPS,
15876   IX86_BUILTIN_CMPNEQPS,
15877   IX86_BUILTIN_CMPNLTPS,
15878   IX86_BUILTIN_CMPNLEPS,
15879   IX86_BUILTIN_CMPNGTPS,
15880   IX86_BUILTIN_CMPNGEPS,
15881   IX86_BUILTIN_CMPORDPS,
15882   IX86_BUILTIN_CMPUNORDPS,
15883   IX86_BUILTIN_CMPEQSS,
15884   IX86_BUILTIN_CMPLTSS,
15885   IX86_BUILTIN_CMPLESS,
15886   IX86_BUILTIN_CMPNEQSS,
15887   IX86_BUILTIN_CMPNLTSS,
15888   IX86_BUILTIN_CMPNLESS,
15889   IX86_BUILTIN_CMPNGTSS,
15890   IX86_BUILTIN_CMPNGESS,
15891   IX86_BUILTIN_CMPORDSS,
15892   IX86_BUILTIN_CMPUNORDSS,
15893
15894   IX86_BUILTIN_COMIEQSS,
15895   IX86_BUILTIN_COMILTSS,
15896   IX86_BUILTIN_COMILESS,
15897   IX86_BUILTIN_COMIGTSS,
15898   IX86_BUILTIN_COMIGESS,
15899   IX86_BUILTIN_COMINEQSS,
15900   IX86_BUILTIN_UCOMIEQSS,
15901   IX86_BUILTIN_UCOMILTSS,
15902   IX86_BUILTIN_UCOMILESS,
15903   IX86_BUILTIN_UCOMIGTSS,
15904   IX86_BUILTIN_UCOMIGESS,
15905   IX86_BUILTIN_UCOMINEQSS,
15906
15907   IX86_BUILTIN_CVTPI2PS,
15908   IX86_BUILTIN_CVTPS2PI,
15909   IX86_BUILTIN_CVTSI2SS,
15910   IX86_BUILTIN_CVTSI642SS,
15911   IX86_BUILTIN_CVTSS2SI,
15912   IX86_BUILTIN_CVTSS2SI64,
15913   IX86_BUILTIN_CVTTPS2PI,
15914   IX86_BUILTIN_CVTTSS2SI,
15915   IX86_BUILTIN_CVTTSS2SI64,
15916
15917   IX86_BUILTIN_MAXPS,
15918   IX86_BUILTIN_MAXSS,
15919   IX86_BUILTIN_MINPS,
15920   IX86_BUILTIN_MINSS,
15921
15922   IX86_BUILTIN_LOADUPS,
15923   IX86_BUILTIN_STOREUPS,
15924   IX86_BUILTIN_MOVSS,
15925
15926   IX86_BUILTIN_MOVHLPS,
15927   IX86_BUILTIN_MOVLHPS,
15928   IX86_BUILTIN_LOADHPS,
15929   IX86_BUILTIN_LOADLPS,
15930   IX86_BUILTIN_STOREHPS,
15931   IX86_BUILTIN_STORELPS,
15932
15933   IX86_BUILTIN_MASKMOVQ,
15934   IX86_BUILTIN_MOVMSKPS,
15935   IX86_BUILTIN_PMOVMSKB,
15936
15937   IX86_BUILTIN_MOVNTPS,
15938   IX86_BUILTIN_MOVNTQ,
15939
15940   IX86_BUILTIN_LOADDQU,
15941   IX86_BUILTIN_STOREDQU,
15942
15943   IX86_BUILTIN_PACKSSWB,
15944   IX86_BUILTIN_PACKSSDW,
15945   IX86_BUILTIN_PACKUSWB,
15946
15947   IX86_BUILTIN_PADDB,
15948   IX86_BUILTIN_PADDW,
15949   IX86_BUILTIN_PADDD,
15950   IX86_BUILTIN_PADDQ,
15951   IX86_BUILTIN_PADDSB,
15952   IX86_BUILTIN_PADDSW,
15953   IX86_BUILTIN_PADDUSB,
15954   IX86_BUILTIN_PADDUSW,
15955   IX86_BUILTIN_PSUBB,
15956   IX86_BUILTIN_PSUBW,
15957   IX86_BUILTIN_PSUBD,
15958   IX86_BUILTIN_PSUBQ,
15959   IX86_BUILTIN_PSUBSB,
15960   IX86_BUILTIN_PSUBSW,
15961   IX86_BUILTIN_PSUBUSB,
15962   IX86_BUILTIN_PSUBUSW,
15963
15964   IX86_BUILTIN_PAND,
15965   IX86_BUILTIN_PANDN,
15966   IX86_BUILTIN_POR,
15967   IX86_BUILTIN_PXOR,
15968
15969   IX86_BUILTIN_PAVGB,
15970   IX86_BUILTIN_PAVGW,
15971
15972   IX86_BUILTIN_PCMPEQB,
15973   IX86_BUILTIN_PCMPEQW,
15974   IX86_BUILTIN_PCMPEQD,
15975   IX86_BUILTIN_PCMPGTB,
15976   IX86_BUILTIN_PCMPGTW,
15977   IX86_BUILTIN_PCMPGTD,
15978
15979   IX86_BUILTIN_PMADDWD,
15980
15981   IX86_BUILTIN_PMAXSW,
15982   IX86_BUILTIN_PMAXUB,
15983   IX86_BUILTIN_PMINSW,
15984   IX86_BUILTIN_PMINUB,
15985
15986   IX86_BUILTIN_PMULHUW,
15987   IX86_BUILTIN_PMULHW,
15988   IX86_BUILTIN_PMULLW,
15989
15990   IX86_BUILTIN_PSADBW,
15991   IX86_BUILTIN_PSHUFW,
15992
15993   IX86_BUILTIN_PSLLW,
15994   IX86_BUILTIN_PSLLD,
15995   IX86_BUILTIN_PSLLQ,
15996   IX86_BUILTIN_PSRAW,
15997   IX86_BUILTIN_PSRAD,
15998   IX86_BUILTIN_PSRLW,
15999   IX86_BUILTIN_PSRLD,
16000   IX86_BUILTIN_PSRLQ,
16001   IX86_BUILTIN_PSLLWI,
16002   IX86_BUILTIN_PSLLDI,
16003   IX86_BUILTIN_PSLLQI,
16004   IX86_BUILTIN_PSRAWI,
16005   IX86_BUILTIN_PSRADI,
16006   IX86_BUILTIN_PSRLWI,
16007   IX86_BUILTIN_PSRLDI,
16008   IX86_BUILTIN_PSRLQI,
16009
16010   IX86_BUILTIN_PUNPCKHBW,
16011   IX86_BUILTIN_PUNPCKHWD,
16012   IX86_BUILTIN_PUNPCKHDQ,
16013   IX86_BUILTIN_PUNPCKLBW,
16014   IX86_BUILTIN_PUNPCKLWD,
16015   IX86_BUILTIN_PUNPCKLDQ,
16016
16017   IX86_BUILTIN_SHUFPS,
16018
16019   IX86_BUILTIN_RCPPS,
16020   IX86_BUILTIN_RCPSS,
16021   IX86_BUILTIN_RSQRTPS,
16022   IX86_BUILTIN_RSQRTSS,
16023   IX86_BUILTIN_SQRTPS,
16024   IX86_BUILTIN_SQRTSS,
16025
16026   IX86_BUILTIN_UNPCKHPS,
16027   IX86_BUILTIN_UNPCKLPS,
16028
16029   IX86_BUILTIN_ANDPS,
16030   IX86_BUILTIN_ANDNPS,
16031   IX86_BUILTIN_ORPS,
16032   IX86_BUILTIN_XORPS,
16033
16034   IX86_BUILTIN_EMMS,
16035   IX86_BUILTIN_LDMXCSR,
16036   IX86_BUILTIN_STMXCSR,
16037   IX86_BUILTIN_SFENCE,
16038
16039   /* 3DNow! Original */
16040   IX86_BUILTIN_FEMMS,
16041   IX86_BUILTIN_PAVGUSB,
16042   IX86_BUILTIN_PF2ID,
16043   IX86_BUILTIN_PFACC,
16044   IX86_BUILTIN_PFADD,
16045   IX86_BUILTIN_PFCMPEQ,
16046   IX86_BUILTIN_PFCMPGE,
16047   IX86_BUILTIN_PFCMPGT,
16048   IX86_BUILTIN_PFMAX,
16049   IX86_BUILTIN_PFMIN,
16050   IX86_BUILTIN_PFMUL,
16051   IX86_BUILTIN_PFRCP,
16052   IX86_BUILTIN_PFRCPIT1,
16053   IX86_BUILTIN_PFRCPIT2,
16054   IX86_BUILTIN_PFRSQIT1,
16055   IX86_BUILTIN_PFRSQRT,
16056   IX86_BUILTIN_PFSUB,
16057   IX86_BUILTIN_PFSUBR,
16058   IX86_BUILTIN_PI2FD,
16059   IX86_BUILTIN_PMULHRW,
16060
16061   /* 3DNow! Athlon Extensions */
16062   IX86_BUILTIN_PF2IW,
16063   IX86_BUILTIN_PFNACC,
16064   IX86_BUILTIN_PFPNACC,
16065   IX86_BUILTIN_PI2FW,
16066   IX86_BUILTIN_PSWAPDSI,
16067   IX86_BUILTIN_PSWAPDSF,
16068
16069   /* SSE2 */
16070   IX86_BUILTIN_ADDPD,
16071   IX86_BUILTIN_ADDSD,
16072   IX86_BUILTIN_DIVPD,
16073   IX86_BUILTIN_DIVSD,
16074   IX86_BUILTIN_MULPD,
16075   IX86_BUILTIN_MULSD,
16076   IX86_BUILTIN_SUBPD,
16077   IX86_BUILTIN_SUBSD,
16078
16079   IX86_BUILTIN_CMPEQPD,
16080   IX86_BUILTIN_CMPLTPD,
16081   IX86_BUILTIN_CMPLEPD,
16082   IX86_BUILTIN_CMPGTPD,
16083   IX86_BUILTIN_CMPGEPD,
16084   IX86_BUILTIN_CMPNEQPD,
16085   IX86_BUILTIN_CMPNLTPD,
16086   IX86_BUILTIN_CMPNLEPD,
16087   IX86_BUILTIN_CMPNGTPD,
16088   IX86_BUILTIN_CMPNGEPD,
16089   IX86_BUILTIN_CMPORDPD,
16090   IX86_BUILTIN_CMPUNORDPD,
16091   IX86_BUILTIN_CMPEQSD,
16092   IX86_BUILTIN_CMPLTSD,
16093   IX86_BUILTIN_CMPLESD,
16094   IX86_BUILTIN_CMPNEQSD,
16095   IX86_BUILTIN_CMPNLTSD,
16096   IX86_BUILTIN_CMPNLESD,
16097   IX86_BUILTIN_CMPORDSD,
16098   IX86_BUILTIN_CMPUNORDSD,
16099
16100   IX86_BUILTIN_COMIEQSD,
16101   IX86_BUILTIN_COMILTSD,
16102   IX86_BUILTIN_COMILESD,
16103   IX86_BUILTIN_COMIGTSD,
16104   IX86_BUILTIN_COMIGESD,
16105   IX86_BUILTIN_COMINEQSD,
16106   IX86_BUILTIN_UCOMIEQSD,
16107   IX86_BUILTIN_UCOMILTSD,
16108   IX86_BUILTIN_UCOMILESD,
16109   IX86_BUILTIN_UCOMIGTSD,
16110   IX86_BUILTIN_UCOMIGESD,
16111   IX86_BUILTIN_UCOMINEQSD,
16112
16113   IX86_BUILTIN_MAXPD,
16114   IX86_BUILTIN_MAXSD,
16115   IX86_BUILTIN_MINPD,
16116   IX86_BUILTIN_MINSD,
16117
16118   IX86_BUILTIN_ANDPD,
16119   IX86_BUILTIN_ANDNPD,
16120   IX86_BUILTIN_ORPD,
16121   IX86_BUILTIN_XORPD,
16122
16123   IX86_BUILTIN_SQRTPD,
16124   IX86_BUILTIN_SQRTSD,
16125
16126   IX86_BUILTIN_UNPCKHPD,
16127   IX86_BUILTIN_UNPCKLPD,
16128
16129   IX86_BUILTIN_SHUFPD,
16130
16131   IX86_BUILTIN_LOADUPD,
16132   IX86_BUILTIN_STOREUPD,
16133   IX86_BUILTIN_MOVSD,
16134
16135   IX86_BUILTIN_LOADHPD,
16136   IX86_BUILTIN_LOADLPD,
16137
16138   IX86_BUILTIN_CVTDQ2PD,
16139   IX86_BUILTIN_CVTDQ2PS,
16140
16141   IX86_BUILTIN_CVTPD2DQ,
16142   IX86_BUILTIN_CVTPD2PI,
16143   IX86_BUILTIN_CVTPD2PS,
16144   IX86_BUILTIN_CVTTPD2DQ,
16145   IX86_BUILTIN_CVTTPD2PI,
16146
16147   IX86_BUILTIN_CVTPI2PD,
16148   IX86_BUILTIN_CVTSI2SD,
16149   IX86_BUILTIN_CVTSI642SD,
16150
16151   IX86_BUILTIN_CVTSD2SI,
16152   IX86_BUILTIN_CVTSD2SI64,
16153   IX86_BUILTIN_CVTSD2SS,
16154   IX86_BUILTIN_CVTSS2SD,
16155   IX86_BUILTIN_CVTTSD2SI,
16156   IX86_BUILTIN_CVTTSD2SI64,
16157
16158   IX86_BUILTIN_CVTPS2DQ,
16159   IX86_BUILTIN_CVTPS2PD,
16160   IX86_BUILTIN_CVTTPS2DQ,
16161
16162   IX86_BUILTIN_MOVNTI,
16163   IX86_BUILTIN_MOVNTPD,
16164   IX86_BUILTIN_MOVNTDQ,
16165
16166   /* SSE2 MMX */
16167   IX86_BUILTIN_MASKMOVDQU,
16168   IX86_BUILTIN_MOVMSKPD,
16169   IX86_BUILTIN_PMOVMSKB128,
16170
16171   IX86_BUILTIN_PACKSSWB128,
16172   IX86_BUILTIN_PACKSSDW128,
16173   IX86_BUILTIN_PACKUSWB128,
16174
16175   IX86_BUILTIN_PADDB128,
16176   IX86_BUILTIN_PADDW128,
16177   IX86_BUILTIN_PADDD128,
16178   IX86_BUILTIN_PADDQ128,
16179   IX86_BUILTIN_PADDSB128,
16180   IX86_BUILTIN_PADDSW128,
16181   IX86_BUILTIN_PADDUSB128,
16182   IX86_BUILTIN_PADDUSW128,
16183   IX86_BUILTIN_PSUBB128,
16184   IX86_BUILTIN_PSUBW128,
16185   IX86_BUILTIN_PSUBD128,
16186   IX86_BUILTIN_PSUBQ128,
16187   IX86_BUILTIN_PSUBSB128,
16188   IX86_BUILTIN_PSUBSW128,
16189   IX86_BUILTIN_PSUBUSB128,
16190   IX86_BUILTIN_PSUBUSW128,
16191
16192   IX86_BUILTIN_PAND128,
16193   IX86_BUILTIN_PANDN128,
16194   IX86_BUILTIN_POR128,
16195   IX86_BUILTIN_PXOR128,
16196
16197   IX86_BUILTIN_PAVGB128,
16198   IX86_BUILTIN_PAVGW128,
16199
16200   IX86_BUILTIN_PCMPEQB128,
16201   IX86_BUILTIN_PCMPEQW128,
16202   IX86_BUILTIN_PCMPEQD128,
16203   IX86_BUILTIN_PCMPGTB128,
16204   IX86_BUILTIN_PCMPGTW128,
16205   IX86_BUILTIN_PCMPGTD128,
16206
16207   IX86_BUILTIN_PMADDWD128,
16208
16209   IX86_BUILTIN_PMAXSW128,
16210   IX86_BUILTIN_PMAXUB128,
16211   IX86_BUILTIN_PMINSW128,
16212   IX86_BUILTIN_PMINUB128,
16213
16214   IX86_BUILTIN_PMULUDQ,
16215   IX86_BUILTIN_PMULUDQ128,
16216   IX86_BUILTIN_PMULHUW128,
16217   IX86_BUILTIN_PMULHW128,
16218   IX86_BUILTIN_PMULLW128,
16219
16220   IX86_BUILTIN_PSADBW128,
16221   IX86_BUILTIN_PSHUFHW,
16222   IX86_BUILTIN_PSHUFLW,
16223   IX86_BUILTIN_PSHUFD,
16224
16225   IX86_BUILTIN_PSLLDQI128,
16226   IX86_BUILTIN_PSLLWI128,
16227   IX86_BUILTIN_PSLLDI128,
16228   IX86_BUILTIN_PSLLQI128,
16229   IX86_BUILTIN_PSRAWI128,
16230   IX86_BUILTIN_PSRADI128,
16231   IX86_BUILTIN_PSRLDQI128,
16232   IX86_BUILTIN_PSRLWI128,
16233   IX86_BUILTIN_PSRLDI128,
16234   IX86_BUILTIN_PSRLQI128,
16235
16236   IX86_BUILTIN_PSLLDQ128,
16237   IX86_BUILTIN_PSLLW128,
16238   IX86_BUILTIN_PSLLD128,
16239   IX86_BUILTIN_PSLLQ128,
16240   IX86_BUILTIN_PSRAW128,
16241   IX86_BUILTIN_PSRAD128,
16242   IX86_BUILTIN_PSRLW128,
16243   IX86_BUILTIN_PSRLD128,
16244   IX86_BUILTIN_PSRLQ128,
16245
16246   IX86_BUILTIN_PUNPCKHBW128,
16247   IX86_BUILTIN_PUNPCKHWD128,
16248   IX86_BUILTIN_PUNPCKHDQ128,
16249   IX86_BUILTIN_PUNPCKHQDQ128,
16250   IX86_BUILTIN_PUNPCKLBW128,
16251   IX86_BUILTIN_PUNPCKLWD128,
16252   IX86_BUILTIN_PUNPCKLDQ128,
16253   IX86_BUILTIN_PUNPCKLQDQ128,
16254
16255   IX86_BUILTIN_CLFLUSH,
16256   IX86_BUILTIN_MFENCE,
16257   IX86_BUILTIN_LFENCE,
16258
16259   /* Prescott New Instructions.  */
16260   IX86_BUILTIN_ADDSUBPS,
16261   IX86_BUILTIN_HADDPS,
16262   IX86_BUILTIN_HSUBPS,
16263   IX86_BUILTIN_MOVSHDUP,
16264   IX86_BUILTIN_MOVSLDUP,
16265   IX86_BUILTIN_ADDSUBPD,
16266   IX86_BUILTIN_HADDPD,
16267   IX86_BUILTIN_HSUBPD,
16268   IX86_BUILTIN_LDDQU,
16269
16270   IX86_BUILTIN_MONITOR,
16271   IX86_BUILTIN_MWAIT,
16272
16273   /* SSSE3.  */
16274   IX86_BUILTIN_PHADDW,
16275   IX86_BUILTIN_PHADDD,
16276   IX86_BUILTIN_PHADDSW,
16277   IX86_BUILTIN_PHSUBW,
16278   IX86_BUILTIN_PHSUBD,
16279   IX86_BUILTIN_PHSUBSW,
16280   IX86_BUILTIN_PMADDUBSW,
16281   IX86_BUILTIN_PMULHRSW,
16282   IX86_BUILTIN_PSHUFB,
16283   IX86_BUILTIN_PSIGNB,
16284   IX86_BUILTIN_PSIGNW,
16285   IX86_BUILTIN_PSIGND,
16286   IX86_BUILTIN_PALIGNR,
16287   IX86_BUILTIN_PABSB,
16288   IX86_BUILTIN_PABSW,
16289   IX86_BUILTIN_PABSD,
16290
16291   IX86_BUILTIN_PHADDW128,
16292   IX86_BUILTIN_PHADDD128,
16293   IX86_BUILTIN_PHADDSW128,
16294   IX86_BUILTIN_PHSUBW128,
16295   IX86_BUILTIN_PHSUBD128,
16296   IX86_BUILTIN_PHSUBSW128,
16297   IX86_BUILTIN_PMADDUBSW128,
16298   IX86_BUILTIN_PMULHRSW128,
16299   IX86_BUILTIN_PSHUFB128,
16300   IX86_BUILTIN_PSIGNB128,
16301   IX86_BUILTIN_PSIGNW128,
16302   IX86_BUILTIN_PSIGND128,
16303   IX86_BUILTIN_PALIGNR128,
16304   IX86_BUILTIN_PABSB128,
16305   IX86_BUILTIN_PABSW128,
16306   IX86_BUILTIN_PABSD128,
16307
16308   /* AMDFAM10 - SSE4A New Instructions.  */
16309   IX86_BUILTIN_MOVNTSD,
16310   IX86_BUILTIN_MOVNTSS,
16311   IX86_BUILTIN_EXTRQI,
16312   IX86_BUILTIN_EXTRQ,
16313   IX86_BUILTIN_INSERTQI,
16314   IX86_BUILTIN_INSERTQ,
16315
16316   IX86_BUILTIN_VEC_INIT_V2SI,
16317   IX86_BUILTIN_VEC_INIT_V4HI,
16318   IX86_BUILTIN_VEC_INIT_V8QI,
16319   IX86_BUILTIN_VEC_EXT_V2DF,
16320   IX86_BUILTIN_VEC_EXT_V2DI,
16321   IX86_BUILTIN_VEC_EXT_V4SF,
16322   IX86_BUILTIN_VEC_EXT_V4SI,
16323   IX86_BUILTIN_VEC_EXT_V8HI,
16324   IX86_BUILTIN_VEC_EXT_V2SI,
16325   IX86_BUILTIN_VEC_EXT_V4HI,
16326   IX86_BUILTIN_VEC_SET_V8HI,
16327   IX86_BUILTIN_VEC_SET_V4HI,
16328
16329   IX86_BUILTIN_MAX
16330 };
16331
16332 /* Table for the ix86 builtin decls.  */
16333 static GTY(()) tree ix86_builtins[(int) IX86_BUILTIN_MAX];
16334
16335 /* Add a ix86 target builtin function with CODE, NAME and TYPE.  Do so,
16336  * if the target_flags include one of MASK.  Stores the function decl
16337  * in the ix86_builtins array.
16338  * Returns the function decl or NULL_TREE, if the builtin was not added.  */
16339
16340 static inline tree
16341 def_builtin (int mask, const char *name, tree type, enum ix86_builtins code)
16342 {
16343   tree decl = NULL_TREE;
16344
16345   if (mask & target_flags
16346       && (!(mask & MASK_64BIT) || TARGET_64BIT))
16347     {
16348       decl = add_builtin_function (name, type, code, BUILT_IN_MD,
16349                                    NULL, NULL_TREE);
16350       ix86_builtins[(int) code] = decl;
16351     }
16352
16353   return decl;
16354 }
16355
16356 /* Like def_builtin, but also marks the function decl "const".  */
16357
16358 static inline tree
16359 def_builtin_const (int mask, const char *name, tree type,
16360                    enum ix86_builtins code)
16361 {
16362   tree decl = def_builtin (mask, name, type, code);
16363   if (decl)
16364     TREE_READONLY (decl) = 1;
16365   return decl;
16366 }
16367
16368 /* Bits for builtin_description.flag.  */
16369
16370 /* Set when we don't support the comparison natively, and should
16371    swap_comparison in order to support it.  */
16372 #define BUILTIN_DESC_SWAP_OPERANDS      1
16373
16374 struct builtin_description
16375 {
16376   const unsigned int mask;
16377   const enum insn_code icode;
16378   const char *const name;
16379   const enum ix86_builtins code;
16380   const enum rtx_code comparison;
16381   const unsigned int flag;
16382 };
16383
16384 static const struct builtin_description bdesc_comi[] =
16385 {
16386   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
16387   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
16388   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
16389   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
16390   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
16391   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
16392   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
16393   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
16394   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
16395   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
16396   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
16397   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
16398   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
16399   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
16400   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
16401   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
16402   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
16403   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
16404   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
16405   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
16406   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
16407   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
16408   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
16409   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
16410 };
16411
16412 static const struct builtin_description bdesc_2arg[] =
16413 {
16414   /* SSE */
16415   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
16416   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
16417   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
16418   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
16419   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
16420   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
16421   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
16422   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
16423
16424   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
16425   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
16426   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
16427   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
16428     BUILTIN_DESC_SWAP_OPERANDS },
16429   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
16430     BUILTIN_DESC_SWAP_OPERANDS },
16431   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
16432   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
16433   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
16434   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
16435   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
16436     BUILTIN_DESC_SWAP_OPERANDS },
16437   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
16438     BUILTIN_DESC_SWAP_OPERANDS },
16439   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
16440   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
16441   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
16442   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
16443   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
16444   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
16445   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
16446   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
16447   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
16448     BUILTIN_DESC_SWAP_OPERANDS },
16449   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
16450     BUILTIN_DESC_SWAP_OPERANDS },
16451   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
16452
16453   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
16454   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
16455   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
16456   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
16457
16458   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
16459   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
16460   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
16461   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
16462
16463   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
16464   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
16465   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
16466   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
16467   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
16468
16469   /* MMX */
16470   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
16471   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
16472   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
16473   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
16474   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
16475   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
16476   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
16477   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
16478
16479   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
16480   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
16481   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
16482   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
16483   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
16484   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
16485   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
16486   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
16487
16488   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
16489   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
16490   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
16491
16492   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
16493   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
16494   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
16495   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
16496
16497   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
16498   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
16499
16500   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
16501   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
16502   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
16503   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
16504   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
16505   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
16506
16507   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
16508   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
16509   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
16510   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
16511
16512   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
16513   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
16514   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
16515   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
16516   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
16517   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
16518
16519   /* Special.  */
16520   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
16521   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
16522   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
16523
16524   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
16525   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
16526   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
16527
16528   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
16529   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
16530   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
16531   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
16532   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
16533   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
16534
16535   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
16536   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
16537   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
16538   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
16539   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
16540   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
16541
16542   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
16543   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
16544   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
16545   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
16546
16547   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
16548   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
16549
16550   /* SSE2 */
16551   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
16552   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
16553   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
16554   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
16555   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
16556   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
16557   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
16558   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
16559
16560   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
16561   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
16562   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
16563   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
16564     BUILTIN_DESC_SWAP_OPERANDS },
16565   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
16566     BUILTIN_DESC_SWAP_OPERANDS },
16567   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
16568   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
16569   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
16570   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
16571   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
16572     BUILTIN_DESC_SWAP_OPERANDS },
16573   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
16574     BUILTIN_DESC_SWAP_OPERANDS },
16575   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
16576   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
16577   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
16578   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
16579   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
16580   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
16581   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
16582   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
16583   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
16584
16585   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
16586   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
16587   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
16588   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
16589
16590   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
16591   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
16592   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
16593   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
16594
16595   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
16596   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
16597   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
16598
16599   /* SSE2 MMX */
16600   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
16601   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
16602   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
16603   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
16604   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
16605   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
16606   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
16607   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
16608
16609   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
16610   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
16611   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
16612   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
16613   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
16614   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
16615   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
16616   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
16617
16618   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
16619   { MASK_SSE2, CODE_FOR_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
16620
16621   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
16622   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
16623   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
16624   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
16625
16626   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
16627   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
16628
16629   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
16630   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
16631   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
16632   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
16633   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
16634   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
16635
16636   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
16637   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
16638   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
16639   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
16640
16641   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
16642   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
16643   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
16644   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
16645   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
16646   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
16647   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
16648   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
16649
16650   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
16651   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
16652   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
16653
16654   { MASK_SSE2, CODE_FOR_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
16655   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
16656
16657   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
16658   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
16659
16660   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
16661   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
16662   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
16663
16664   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
16665   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
16666   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
16667
16668   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
16669   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
16670
16671   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
16672
16673   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
16674   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
16675   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
16676   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
16677
16678   /* SSE3 MMX */
16679   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
16680   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
16681   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
16682   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
16683   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
16684   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 },
16685
16686   /* SSSE3 */
16687   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, 0, 0 },
16688   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, 0, 0 },
16689   { MASK_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, 0, 0 },
16690   { MASK_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, 0, 0 },
16691   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, 0, 0 },
16692   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, 0, 0 },
16693   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, 0, 0 },
16694   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, 0, 0 },
16695   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, 0, 0 },
16696   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, 0, 0 },
16697   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, 0, 0 },
16698   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, 0, 0 },
16699   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, 0, 0 },
16700   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, 0, 0 },
16701   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, 0, 0 },
16702   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, 0, 0 },
16703   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, 0, 0 },
16704   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, 0, 0 },
16705   { MASK_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, 0, 0 },
16706   { MASK_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, 0, 0 },
16707   { MASK_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, 0, 0 },
16708   { MASK_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, 0, 0 },
16709   { MASK_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, 0, 0 },
16710   { MASK_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, 0, 0 }
16711 };
16712
16713 static const struct builtin_description bdesc_1arg[] =
16714 {
16715   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
16716   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
16717
16718   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
16719   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
16720   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
16721
16722   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
16723   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
16724   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
16725   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
16726   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
16727   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
16728
16729   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
16730   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
16731
16732   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
16733
16734   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
16735   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
16736
16737   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
16738   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
16739   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
16740   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
16741   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
16742
16743   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
16744
16745   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
16746   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
16747   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
16748   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
16749
16750   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
16751   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
16752   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
16753
16754   /* SSE3 */
16755   { MASK_SSE3, CODE_FOR_sse3_movshdup, "__builtin_ia32_movshdup", IX86_BUILTIN_MOVSHDUP, 0, 0 },
16756   { MASK_SSE3, CODE_FOR_sse3_movsldup, "__builtin_ia32_movsldup", IX86_BUILTIN_MOVSLDUP, 0, 0 },
16757
16758   /* SSSE3 */
16759   { MASK_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, 0, 0 },
16760   { MASK_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, 0, 0 },
16761   { MASK_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, 0, 0 },
16762   { MASK_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, 0, 0 },
16763   { MASK_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, 0, 0 },
16764   { MASK_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, 0, 0 },
16765 };
16766
16767 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
16768    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
16769    builtins.  */
16770 static void
16771 ix86_init_mmx_sse_builtins (void)
16772 {
16773   const struct builtin_description * d;
16774   size_t i;
16775
16776   tree V16QI_type_node = build_vector_type_for_mode (char_type_node, V16QImode);
16777   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
16778   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
16779   tree V2DI_type_node
16780     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
16781   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
16782   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
16783   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
16784   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
16785   tree V8QI_type_node = build_vector_type_for_mode (char_type_node, V8QImode);
16786   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
16787
16788   tree pchar_type_node = build_pointer_type (char_type_node);
16789   tree pcchar_type_node = build_pointer_type (
16790                              build_type_variant (char_type_node, 1, 0));
16791   tree pfloat_type_node = build_pointer_type (float_type_node);
16792   tree pcfloat_type_node = build_pointer_type (
16793                              build_type_variant (float_type_node, 1, 0));
16794   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
16795   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
16796   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
16797
16798   /* Comparisons.  */
16799   tree int_ftype_v4sf_v4sf
16800     = build_function_type_list (integer_type_node,
16801                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16802   tree v4si_ftype_v4sf_v4sf
16803     = build_function_type_list (V4SI_type_node,
16804                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16805   /* MMX/SSE/integer conversions.  */
16806   tree int_ftype_v4sf
16807     = build_function_type_list (integer_type_node,
16808                                 V4SF_type_node, NULL_TREE);
16809   tree int64_ftype_v4sf
16810     = build_function_type_list (long_long_integer_type_node,
16811                                 V4SF_type_node, NULL_TREE);
16812   tree int_ftype_v8qi
16813     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
16814   tree v4sf_ftype_v4sf_int
16815     = build_function_type_list (V4SF_type_node,
16816                                 V4SF_type_node, integer_type_node, NULL_TREE);
16817   tree v4sf_ftype_v4sf_int64
16818     = build_function_type_list (V4SF_type_node,
16819                                 V4SF_type_node, long_long_integer_type_node,
16820                                 NULL_TREE);
16821   tree v4sf_ftype_v4sf_v2si
16822     = build_function_type_list (V4SF_type_node,
16823                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
16824
16825   /* Miscellaneous.  */
16826   tree v8qi_ftype_v4hi_v4hi
16827     = build_function_type_list (V8QI_type_node,
16828                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16829   tree v4hi_ftype_v2si_v2si
16830     = build_function_type_list (V4HI_type_node,
16831                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16832   tree v4sf_ftype_v4sf_v4sf_int
16833     = build_function_type_list (V4SF_type_node,
16834                                 V4SF_type_node, V4SF_type_node,
16835                                 integer_type_node, NULL_TREE);
16836   tree v2si_ftype_v4hi_v4hi
16837     = build_function_type_list (V2SI_type_node,
16838                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16839   tree v4hi_ftype_v4hi_int
16840     = build_function_type_list (V4HI_type_node,
16841                                 V4HI_type_node, integer_type_node, NULL_TREE);
16842   tree v4hi_ftype_v4hi_di
16843     = build_function_type_list (V4HI_type_node,
16844                                 V4HI_type_node, long_long_unsigned_type_node,
16845                                 NULL_TREE);
16846   tree v2si_ftype_v2si_di
16847     = build_function_type_list (V2SI_type_node,
16848                                 V2SI_type_node, long_long_unsigned_type_node,
16849                                 NULL_TREE);
16850   tree void_ftype_void
16851     = build_function_type (void_type_node, void_list_node);
16852   tree void_ftype_unsigned
16853     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
16854   tree void_ftype_unsigned_unsigned
16855     = build_function_type_list (void_type_node, unsigned_type_node,
16856                                 unsigned_type_node, NULL_TREE);
16857   tree void_ftype_pcvoid_unsigned_unsigned
16858     = build_function_type_list (void_type_node, const_ptr_type_node,
16859                                 unsigned_type_node, unsigned_type_node,
16860                                 NULL_TREE);
16861   tree unsigned_ftype_void
16862     = build_function_type (unsigned_type_node, void_list_node);
16863   tree v2si_ftype_v4sf
16864     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
16865   /* Loads/stores.  */
16866   tree void_ftype_v8qi_v8qi_pchar
16867     = build_function_type_list (void_type_node,
16868                                 V8QI_type_node, V8QI_type_node,
16869                                 pchar_type_node, NULL_TREE);
16870   tree v4sf_ftype_pcfloat
16871     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
16872   /* @@@ the type is bogus */
16873   tree v4sf_ftype_v4sf_pv2si
16874     = build_function_type_list (V4SF_type_node,
16875                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
16876   tree void_ftype_pv2si_v4sf
16877     = build_function_type_list (void_type_node,
16878                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
16879   tree void_ftype_pfloat_v4sf
16880     = build_function_type_list (void_type_node,
16881                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
16882   tree void_ftype_pdi_di
16883     = build_function_type_list (void_type_node,
16884                                 pdi_type_node, long_long_unsigned_type_node,
16885                                 NULL_TREE);
16886   tree void_ftype_pv2di_v2di
16887     = build_function_type_list (void_type_node,
16888                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
16889   /* Normal vector unops.  */
16890   tree v4sf_ftype_v4sf
16891     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
16892   tree v16qi_ftype_v16qi
16893     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
16894   tree v8hi_ftype_v8hi
16895     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
16896   tree v4si_ftype_v4si
16897     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
16898   tree v8qi_ftype_v8qi
16899     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
16900   tree v4hi_ftype_v4hi
16901     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
16902
16903   /* Normal vector binops.  */
16904   tree v4sf_ftype_v4sf_v4sf
16905     = build_function_type_list (V4SF_type_node,
16906                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
16907   tree v8qi_ftype_v8qi_v8qi
16908     = build_function_type_list (V8QI_type_node,
16909                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
16910   tree v4hi_ftype_v4hi_v4hi
16911     = build_function_type_list (V4HI_type_node,
16912                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
16913   tree v2si_ftype_v2si_v2si
16914     = build_function_type_list (V2SI_type_node,
16915                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
16916   tree di_ftype_di_di
16917     = build_function_type_list (long_long_unsigned_type_node,
16918                                 long_long_unsigned_type_node,
16919                                 long_long_unsigned_type_node, NULL_TREE);
16920
16921   tree di_ftype_di_di_int
16922     = build_function_type_list (long_long_unsigned_type_node,
16923                                 long_long_unsigned_type_node,
16924                                 long_long_unsigned_type_node,
16925                                 integer_type_node, NULL_TREE);
16926
16927   tree v2si_ftype_v2sf
16928     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
16929   tree v2sf_ftype_v2si
16930     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
16931   tree v2si_ftype_v2si
16932     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
16933   tree v2sf_ftype_v2sf
16934     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
16935   tree v2sf_ftype_v2sf_v2sf
16936     = build_function_type_list (V2SF_type_node,
16937                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16938   tree v2si_ftype_v2sf_v2sf
16939     = build_function_type_list (V2SI_type_node,
16940                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
16941   tree pint_type_node    = build_pointer_type (integer_type_node);
16942   tree pdouble_type_node = build_pointer_type (double_type_node);
16943   tree pcdouble_type_node = build_pointer_type (
16944                                 build_type_variant (double_type_node, 1, 0));
16945   tree int_ftype_v2df_v2df
16946     = build_function_type_list (integer_type_node,
16947                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
16948
16949   tree void_ftype_pcvoid
16950     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
16951   tree v4sf_ftype_v4si
16952     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
16953   tree v4si_ftype_v4sf
16954     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
16955   tree v2df_ftype_v4si
16956     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
16957   tree v4si_ftype_v2df
16958     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
16959   tree v2si_ftype_v2df
16960     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
16961   tree v4sf_ftype_v2df
16962     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
16963   tree v2df_ftype_v2si
16964     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
16965   tree v2df_ftype_v4sf
16966     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
16967   tree int_ftype_v2df
16968     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
16969   tree int64_ftype_v2df
16970     = build_function_type_list (long_long_integer_type_node,
16971                                 V2DF_type_node, NULL_TREE);
16972   tree v2df_ftype_v2df_int
16973     = build_function_type_list (V2DF_type_node,
16974                                 V2DF_type_node, integer_type_node, NULL_TREE);
16975   tree v2df_ftype_v2df_int64
16976     = build_function_type_list (V2DF_type_node,
16977                                 V2DF_type_node, long_long_integer_type_node,
16978                                 NULL_TREE);
16979   tree v4sf_ftype_v4sf_v2df
16980     = build_function_type_list (V4SF_type_node,
16981                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
16982   tree v2df_ftype_v2df_v4sf
16983     = build_function_type_list (V2DF_type_node,
16984                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
16985   tree v2df_ftype_v2df_v2df_int
16986     = build_function_type_list (V2DF_type_node,
16987                                 V2DF_type_node, V2DF_type_node,
16988                                 integer_type_node,
16989                                 NULL_TREE);
16990   tree v2df_ftype_v2df_pcdouble
16991     = build_function_type_list (V2DF_type_node,
16992                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
16993   tree void_ftype_pdouble_v2df
16994     = build_function_type_list (void_type_node,
16995                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
16996   tree void_ftype_pint_int
16997     = build_function_type_list (void_type_node,
16998                                 pint_type_node, integer_type_node, NULL_TREE);
16999   tree void_ftype_v16qi_v16qi_pchar
17000     = build_function_type_list (void_type_node,
17001                                 V16QI_type_node, V16QI_type_node,
17002                                 pchar_type_node, NULL_TREE);
17003   tree v2df_ftype_pcdouble
17004     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
17005   tree v2df_ftype_v2df_v2df
17006     = build_function_type_list (V2DF_type_node,
17007                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
17008   tree v16qi_ftype_v16qi_v16qi
17009     = build_function_type_list (V16QI_type_node,
17010                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
17011   tree v8hi_ftype_v8hi_v8hi
17012     = build_function_type_list (V8HI_type_node,
17013                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
17014   tree v4si_ftype_v4si_v4si
17015     = build_function_type_list (V4SI_type_node,
17016                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
17017   tree v2di_ftype_v2di_v2di
17018     = build_function_type_list (V2DI_type_node,
17019                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
17020   tree v2di_ftype_v2df_v2df
17021     = build_function_type_list (V2DI_type_node,
17022                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
17023   tree v2df_ftype_v2df
17024     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
17025   tree v2di_ftype_v2di_int
17026     = build_function_type_list (V2DI_type_node,
17027                                 V2DI_type_node, integer_type_node, NULL_TREE);
17028   tree v2di_ftype_v2di_v2di_int
17029     = build_function_type_list (V2DI_type_node, V2DI_type_node,
17030                                 V2DI_type_node, integer_type_node, NULL_TREE);
17031   tree v4si_ftype_v4si_int
17032     = build_function_type_list (V4SI_type_node,
17033                                 V4SI_type_node, integer_type_node, NULL_TREE);
17034   tree v8hi_ftype_v8hi_int
17035     = build_function_type_list (V8HI_type_node,
17036                                 V8HI_type_node, integer_type_node, NULL_TREE);
17037   tree v4si_ftype_v8hi_v8hi
17038     = build_function_type_list (V4SI_type_node,
17039                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
17040   tree di_ftype_v8qi_v8qi
17041     = build_function_type_list (long_long_unsigned_type_node,
17042                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
17043   tree di_ftype_v2si_v2si
17044     = build_function_type_list (long_long_unsigned_type_node,
17045                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
17046   tree v2di_ftype_v16qi_v16qi
17047     = build_function_type_list (V2DI_type_node,
17048                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
17049   tree v2di_ftype_v4si_v4si
17050     = build_function_type_list (V2DI_type_node,
17051                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
17052   tree int_ftype_v16qi
17053     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
17054   tree v16qi_ftype_pcchar
17055     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
17056   tree void_ftype_pchar_v16qi
17057     = build_function_type_list (void_type_node,
17058                                 pchar_type_node, V16QI_type_node, NULL_TREE);
17059
17060   tree v2di_ftype_v2di_unsigned_unsigned
17061     = build_function_type_list (V2DI_type_node, V2DI_type_node,
17062                                 unsigned_type_node, unsigned_type_node,
17063                                 NULL_TREE);
17064   tree v2di_ftype_v2di_v2di_unsigned_unsigned
17065     = build_function_type_list (V2DI_type_node, V2DI_type_node, V2DI_type_node,
17066                                 unsigned_type_node, unsigned_type_node,
17067                                 NULL_TREE);
17068   tree v2di_ftype_v2di_v16qi
17069     = build_function_type_list (V2DI_type_node, V2DI_type_node, V16QI_type_node,
17070                                 NULL_TREE);
17071
17072   tree float80_type;
17073   tree float128_type;
17074   tree ftype;
17075
17076   /* The __float80 type.  */
17077   if (TYPE_MODE (long_double_type_node) == XFmode)
17078     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
17079                                                "__float80");
17080   else
17081     {
17082       /* The __float80 type.  */
17083       float80_type = make_node (REAL_TYPE);
17084       TYPE_PRECISION (float80_type) = 80;
17085       layout_type (float80_type);
17086       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
17087     }
17088
17089   if (TARGET_64BIT)
17090     {
17091       float128_type = make_node (REAL_TYPE);
17092       TYPE_PRECISION (float128_type) = 128;
17093       layout_type (float128_type);
17094       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
17095     }
17096
17097   /* Add all builtins that are more or less simple operations on two
17098      operands.  */
17099   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
17100     {
17101       /* Use one of the operands; the target can have a different mode for
17102          mask-generating compares.  */
17103       enum machine_mode mode;
17104       tree type;
17105
17106       if (d->name == 0)
17107         continue;
17108       mode = insn_data[d->icode].operand[1].mode;
17109
17110       switch (mode)
17111         {
17112         case V16QImode:
17113           type = v16qi_ftype_v16qi_v16qi;
17114           break;
17115         case V8HImode:
17116           type = v8hi_ftype_v8hi_v8hi;
17117           break;
17118         case V4SImode:
17119           type = v4si_ftype_v4si_v4si;
17120           break;
17121         case V2DImode:
17122           type = v2di_ftype_v2di_v2di;
17123           break;
17124         case V2DFmode:
17125           type = v2df_ftype_v2df_v2df;
17126           break;
17127         case V4SFmode:
17128           type = v4sf_ftype_v4sf_v4sf;
17129           break;
17130         case V8QImode:
17131           type = v8qi_ftype_v8qi_v8qi;
17132           break;
17133         case V4HImode:
17134           type = v4hi_ftype_v4hi_v4hi;
17135           break;
17136         case V2SImode:
17137           type = v2si_ftype_v2si_v2si;
17138           break;
17139         case DImode:
17140           type = di_ftype_di_di;
17141           break;
17142
17143         default:
17144           gcc_unreachable ();
17145         }
17146
17147       /* Override for comparisons.  */
17148       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
17149           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
17150         type = v4si_ftype_v4sf_v4sf;
17151
17152       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
17153           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
17154         type = v2di_ftype_v2df_v2df;
17155
17156       def_builtin (d->mask, d->name, type, d->code);
17157     }
17158
17159   /* Add all builtins that are more or less simple operations on 1 operand.  */
17160   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
17161     {
17162       enum machine_mode mode;
17163       tree type;
17164
17165       if (d->name == 0)
17166         continue;
17167       mode = insn_data[d->icode].operand[1].mode;
17168
17169       switch (mode)
17170         {
17171         case V16QImode:
17172           type = v16qi_ftype_v16qi;
17173           break;
17174         case V8HImode:
17175           type = v8hi_ftype_v8hi;
17176           break;
17177         case V4SImode:
17178           type = v4si_ftype_v4si;
17179           break;
17180         case V2DFmode:
17181           type = v2df_ftype_v2df;
17182           break;
17183         case V4SFmode:
17184           type = v4sf_ftype_v4sf;
17185           break;
17186         case V8QImode:
17187           type = v8qi_ftype_v8qi;
17188           break;
17189         case V4HImode:
17190           type = v4hi_ftype_v4hi;
17191           break;
17192         case V2SImode:
17193           type = v2si_ftype_v2si;
17194           break;
17195
17196         default:
17197           abort ();
17198         }
17199
17200       def_builtin (d->mask, d->name, type, d->code);
17201     }
17202
17203   /* Add the remaining MMX insns with somewhat more complicated types.  */
17204   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
17205   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
17206   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
17207   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
17208
17209   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
17210   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
17211   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
17212
17213   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
17214   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
17215
17216   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
17217   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
17218
17219   /* comi/ucomi insns.  */
17220   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
17221     if (d->mask == MASK_SSE2)
17222       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
17223     else
17224       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
17225
17226   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
17227   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
17228   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
17229
17230   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
17231   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
17232   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
17233   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
17234   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
17235   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
17236   def_builtin_const (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
17237   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
17238   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
17239   def_builtin_const (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
17240   def_builtin_const (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
17241
17242   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
17243
17244   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
17245   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
17246
17247   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
17248   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
17249   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
17250   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
17251
17252   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
17253   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
17254   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
17255   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
17256
17257   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
17258
17259   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
17260
17261   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
17262   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
17263   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
17264   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
17265   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
17266   def_builtin_const (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
17267
17268   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
17269
17270   /* Original 3DNow!  */
17271   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
17272   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
17273   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
17274   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
17275   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
17276   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
17277   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
17278   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
17279   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
17280   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
17281   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
17282   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
17283   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
17284   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
17285   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
17286   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
17287   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
17288   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
17289   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
17290   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
17291
17292   /* 3DNow! extension as used in the Athlon CPU.  */
17293   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
17294   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
17295   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
17296   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
17297   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
17298   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
17299
17300   /* SSE2 */
17301   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
17302
17303   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
17304   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
17305
17306   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
17307   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
17308
17309   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
17310   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
17311   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
17312   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
17313   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
17314
17315   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
17316   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
17317   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
17318   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
17319
17320   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
17321   def_builtin_const (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
17322
17323   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
17324
17325   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
17326   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
17327
17328   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
17329   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
17330   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
17331   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
17332   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
17333
17334   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
17335
17336   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
17337   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
17338   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
17339   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
17340
17341   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
17342   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
17343   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
17344
17345   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
17346   def_builtin_const (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
17347   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
17348   def_builtin_const (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
17349
17350   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
17351   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
17352   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
17353
17354   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
17355   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
17356
17357   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
17358   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
17359
17360   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
17361   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
17362   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
17363   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
17364   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSLLW128);
17365   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSLLD128);
17366   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
17367
17368   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
17369   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
17370   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
17371   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
17372   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRLW128);
17373   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRLD128);
17374   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
17375
17376   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
17377   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
17378   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v8hi, IX86_BUILTIN_PSRAW128);
17379   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v4si, IX86_BUILTIN_PSRAD128);
17380
17381   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
17382
17383   /* Prescott New Instructions.  */
17384   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
17385                void_ftype_pcvoid_unsigned_unsigned,
17386                IX86_BUILTIN_MONITOR);
17387   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
17388                void_ftype_unsigned_unsigned,
17389                IX86_BUILTIN_MWAIT);
17390   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
17391                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
17392
17393   /* SSSE3.  */
17394   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr128",
17395                v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
17396   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int,
17397                IX86_BUILTIN_PALIGNR);
17398
17399   /* AMDFAM10 SSE4A New built-ins  */
17400   def_builtin (MASK_SSE4A, "__builtin_ia32_movntsd", 
17401                void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTSD);
17402   def_builtin (MASK_SSE4A, "__builtin_ia32_movntss", 
17403                void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTSS);
17404   def_builtin (MASK_SSE4A, "__builtin_ia32_extrqi", 
17405                v2di_ftype_v2di_unsigned_unsigned, IX86_BUILTIN_EXTRQI);
17406   def_builtin (MASK_SSE4A, "__builtin_ia32_extrq",
17407                v2di_ftype_v2di_v16qi,  IX86_BUILTIN_EXTRQ);
17408   def_builtin (MASK_SSE4A, "__builtin_ia32_insertqi",
17409                v2di_ftype_v2di_v2di_unsigned_unsigned, IX86_BUILTIN_INSERTQI);
17410   def_builtin (MASK_SSE4A, "__builtin_ia32_insertq",
17411                v2di_ftype_v2di_v2di, IX86_BUILTIN_INSERTQ);
17412
17413   /* Access to the vec_init patterns.  */
17414   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
17415                                     integer_type_node, NULL_TREE);
17416   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
17417                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
17418
17419   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
17420                                     short_integer_type_node,
17421                                     short_integer_type_node,
17422                                     short_integer_type_node, NULL_TREE);
17423   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
17424                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
17425
17426   ftype = build_function_type_list (V8QI_type_node, char_type_node,
17427                                     char_type_node, char_type_node,
17428                                     char_type_node, char_type_node,
17429                                     char_type_node, char_type_node,
17430                                     char_type_node, NULL_TREE);
17431   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
17432                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
17433
17434   /* Access to the vec_extract patterns.  */
17435   ftype = build_function_type_list (double_type_node, V2DF_type_node,
17436                                     integer_type_node, NULL_TREE);
17437   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
17438                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
17439
17440   ftype = build_function_type_list (long_long_integer_type_node,
17441                                     V2DI_type_node, integer_type_node,
17442                                     NULL_TREE);
17443   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
17444                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
17445
17446   ftype = build_function_type_list (float_type_node, V4SF_type_node,
17447                                     integer_type_node, NULL_TREE);
17448   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
17449                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
17450
17451   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
17452                                     integer_type_node, NULL_TREE);
17453   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
17454                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
17455
17456   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
17457                                     integer_type_node, NULL_TREE);
17458   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
17459                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
17460
17461   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
17462                                     integer_type_node, NULL_TREE);
17463   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
17464                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
17465
17466   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
17467                                     integer_type_node, NULL_TREE);
17468   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
17469                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
17470
17471   /* Access to the vec_set patterns.  */
17472   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
17473                                     intHI_type_node,
17474                                     integer_type_node, NULL_TREE);
17475   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
17476                ftype, IX86_BUILTIN_VEC_SET_V8HI);
17477
17478   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
17479                                     intHI_type_node,
17480                                     integer_type_node, NULL_TREE);
17481   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
17482                ftype, IX86_BUILTIN_VEC_SET_V4HI);
17483 }
17484
17485 static void
17486 ix86_init_builtins (void)
17487 {
17488   if (TARGET_MMX)
17489     ix86_init_mmx_sse_builtins ();
17490 }
17491
17492 /* Errors in the source file can cause expand_expr to return const0_rtx
17493    where we expect a vector.  To avoid crashing, use one of the vector
17494    clear instructions.  */
17495 static rtx
17496 safe_vector_operand (rtx x, enum machine_mode mode)
17497 {
17498   if (x == const0_rtx)
17499     x = CONST0_RTX (mode);
17500   return x;
17501 }
17502
17503 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
17504
17505 static rtx
17506 ix86_expand_binop_builtin (enum insn_code icode, tree exp, rtx target)
17507 {
17508   rtx pat, xops[3];
17509   tree arg0 = CALL_EXPR_ARG (exp, 0);
17510   tree arg1 = CALL_EXPR_ARG (exp, 1);
17511   rtx op0 = expand_normal (arg0);
17512   rtx op1 = expand_normal (arg1);
17513   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17514   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17515   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
17516
17517   if (VECTOR_MODE_P (mode0))
17518     op0 = safe_vector_operand (op0, mode0);
17519   if (VECTOR_MODE_P (mode1))
17520     op1 = safe_vector_operand (op1, mode1);
17521
17522   if (optimize || !target
17523       || GET_MODE (target) != tmode
17524       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17525     target = gen_reg_rtx (tmode);
17526
17527   if (GET_MODE (op1) == SImode && mode1 == TImode)
17528     {
17529       rtx x = gen_reg_rtx (V4SImode);
17530       emit_insn (gen_sse2_loadd (x, op1));
17531       op1 = gen_lowpart (TImode, x);
17532     }
17533
17534   /* The insn must want input operands in the same modes as the
17535      result.  */
17536   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
17537               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
17538
17539   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
17540     op0 = copy_to_mode_reg (mode0, op0);
17541   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
17542     op1 = copy_to_mode_reg (mode1, op1);
17543
17544   /* ??? Using ix86_fixup_binary_operands is problematic when
17545      we've got mismatched modes.  Fake it.  */
17546
17547   xops[0] = target;
17548   xops[1] = op0;
17549   xops[2] = op1;
17550
17551   if (tmode == mode0 && tmode == mode1)
17552     {
17553       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
17554       op0 = xops[1];
17555       op1 = xops[2];
17556     }
17557   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
17558     {
17559       op0 = force_reg (mode0, op0);
17560       op1 = force_reg (mode1, op1);
17561       target = gen_reg_rtx (tmode);
17562     }
17563
17564   pat = GEN_FCN (icode) (target, op0, op1);
17565   if (! pat)
17566     return 0;
17567   emit_insn (pat);
17568   return target;
17569 }
17570
17571 /* Subroutine of ix86_expand_builtin to take care of stores.  */
17572
17573 static rtx
17574 ix86_expand_store_builtin (enum insn_code icode, tree exp)
17575 {
17576   rtx pat;
17577   tree arg0 = CALL_EXPR_ARG (exp, 0);
17578   tree arg1 = CALL_EXPR_ARG (exp, 1);
17579   rtx op0 = expand_normal (arg0);
17580   rtx op1 = expand_normal (arg1);
17581   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
17582   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
17583
17584   if (VECTOR_MODE_P (mode1))
17585     op1 = safe_vector_operand (op1, mode1);
17586
17587   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17588   op1 = copy_to_mode_reg (mode1, op1);
17589
17590   pat = GEN_FCN (icode) (op0, op1);
17591   if (pat)
17592     emit_insn (pat);
17593   return 0;
17594 }
17595
17596 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
17597
17598 static rtx
17599 ix86_expand_unop_builtin (enum insn_code icode, tree exp,
17600                           rtx target, int do_load)
17601 {
17602   rtx pat;
17603   tree arg0 = CALL_EXPR_ARG (exp, 0);
17604   rtx op0 = expand_normal (arg0);
17605   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17606   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17607
17608   if (optimize || !target
17609       || GET_MODE (target) != tmode
17610       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17611     target = gen_reg_rtx (tmode);
17612   if (do_load)
17613     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
17614   else
17615     {
17616       if (VECTOR_MODE_P (mode0))
17617         op0 = safe_vector_operand (op0, mode0);
17618
17619       if ((optimize && !register_operand (op0, mode0))
17620           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17621         op0 = copy_to_mode_reg (mode0, op0);
17622     }
17623
17624   pat = GEN_FCN (icode) (target, op0);
17625   if (! pat)
17626     return 0;
17627   emit_insn (pat);
17628   return target;
17629 }
17630
17631 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
17632    sqrtss, rsqrtss, rcpss.  */
17633
17634 static rtx
17635 ix86_expand_unop1_builtin (enum insn_code icode, tree exp, rtx target)
17636 {
17637   rtx pat;
17638   tree arg0 = CALL_EXPR_ARG (exp, 0);
17639   rtx op1, op0 = expand_normal (arg0);
17640   enum machine_mode tmode = insn_data[icode].operand[0].mode;
17641   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
17642
17643   if (optimize || !target
17644       || GET_MODE (target) != tmode
17645       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
17646     target = gen_reg_rtx (tmode);
17647
17648   if (VECTOR_MODE_P (mode0))
17649     op0 = safe_vector_operand (op0, mode0);
17650
17651   if ((optimize && !register_operand (op0, mode0))
17652       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
17653     op0 = copy_to_mode_reg (mode0, op0);
17654
17655   op1 = op0;
17656   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
17657     op1 = copy_to_mode_reg (mode0, op1);
17658
17659   pat = GEN_FCN (icode) (target, op0, op1);
17660   if (! pat)
17661     return 0;
17662   emit_insn (pat);
17663   return target;
17664 }
17665
17666 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
17667
17668 static rtx
17669 ix86_expand_sse_compare (const struct builtin_description *d, tree exp,
17670                          rtx target)
17671 {
17672   rtx pat;
17673   tree arg0 = CALL_EXPR_ARG (exp, 0);
17674   tree arg1 = CALL_EXPR_ARG (exp, 1);
17675   rtx op0 = expand_normal (arg0);
17676   rtx op1 = expand_normal (arg1);
17677   rtx op2;
17678   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
17679   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
17680   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
17681   enum rtx_code comparison = d->comparison;
17682
17683   if (VECTOR_MODE_P (mode0))
17684     op0 = safe_vector_operand (op0, mode0);
17685   if (VECTOR_MODE_P (mode1))
17686     op1 = safe_vector_operand (op1, mode1);
17687
17688   /* Swap operands if we have a comparison that isn't available in
17689      hardware.  */
17690   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17691     {
17692       rtx tmp = gen_reg_rtx (mode1);
17693       emit_move_insn (tmp, op1);
17694       op1 = op0;
17695       op0 = tmp;
17696     }
17697
17698   if (optimize || !target
17699       || GET_MODE (target) != tmode
17700       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
17701     target = gen_reg_rtx (tmode);
17702
17703   if ((optimize && !register_operand (op0, mode0))
17704       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
17705     op0 = copy_to_mode_reg (mode0, op0);
17706   if ((optimize && !register_operand (op1, mode1))
17707       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
17708     op1 = copy_to_mode_reg (mode1, op1);
17709
17710   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17711   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
17712   if (! pat)
17713     return 0;
17714   emit_insn (pat);
17715   return target;
17716 }
17717
17718 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
17719
17720 static rtx
17721 ix86_expand_sse_comi (const struct builtin_description *d, tree exp,
17722                       rtx target)
17723 {
17724   rtx pat;
17725   tree arg0 = CALL_EXPR_ARG (exp, 0);
17726   tree arg1 = CALL_EXPR_ARG (exp, 1);
17727   rtx op0 = expand_normal (arg0);
17728   rtx op1 = expand_normal (arg1);
17729   rtx op2;
17730   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
17731   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
17732   enum rtx_code comparison = d->comparison;
17733
17734   if (VECTOR_MODE_P (mode0))
17735     op0 = safe_vector_operand (op0, mode0);
17736   if (VECTOR_MODE_P (mode1))
17737     op1 = safe_vector_operand (op1, mode1);
17738
17739   /* Swap operands if we have a comparison that isn't available in
17740      hardware.  */
17741   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
17742     {
17743       rtx tmp = op1;
17744       op1 = op0;
17745       op0 = tmp;
17746     }
17747
17748   target = gen_reg_rtx (SImode);
17749   emit_move_insn (target, const0_rtx);
17750   target = gen_rtx_SUBREG (QImode, target, 0);
17751
17752   if ((optimize && !register_operand (op0, mode0))
17753       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
17754     op0 = copy_to_mode_reg (mode0, op0);
17755   if ((optimize && !register_operand (op1, mode1))
17756       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
17757     op1 = copy_to_mode_reg (mode1, op1);
17758
17759   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
17760   pat = GEN_FCN (d->icode) (op0, op1);
17761   if (! pat)
17762     return 0;
17763   emit_insn (pat);
17764   emit_insn (gen_rtx_SET (VOIDmode,
17765                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
17766                           gen_rtx_fmt_ee (comparison, QImode,
17767                                           SET_DEST (pat),
17768                                           const0_rtx)));
17769
17770   return SUBREG_REG (target);
17771 }
17772
17773 /* Return the integer constant in ARG.  Constrain it to be in the range
17774    of the subparts of VEC_TYPE; issue an error if not.  */
17775
17776 static int
17777 get_element_number (tree vec_type, tree arg)
17778 {
17779   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
17780
17781   if (!host_integerp (arg, 1)
17782       || (elt = tree_low_cst (arg, 1), elt > max))
17783     {
17784       error ("selector must be an integer constant in the range 0..%wi", max);
17785       return 0;
17786     }
17787
17788   return elt;
17789 }
17790
17791 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17792    ix86_expand_vector_init.  We DO have language-level syntax for this, in
17793    the form of  (type){ init-list }.  Except that since we can't place emms
17794    instructions from inside the compiler, we can't allow the use of MMX
17795    registers unless the user explicitly asks for it.  So we do *not* define
17796    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
17797    we have builtins invoked by mmintrin.h that gives us license to emit
17798    these sorts of instructions.  */
17799
17800 static rtx
17801 ix86_expand_vec_init_builtin (tree type, tree exp, rtx target)
17802 {
17803   enum machine_mode tmode = TYPE_MODE (type);
17804   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
17805   int i, n_elt = GET_MODE_NUNITS (tmode);
17806   rtvec v = rtvec_alloc (n_elt);
17807
17808   gcc_assert (VECTOR_MODE_P (tmode));
17809   gcc_assert (call_expr_nargs (exp) == n_elt);
17810
17811   for (i = 0; i < n_elt; ++i)
17812     {
17813       rtx x = expand_normal (CALL_EXPR_ARG (exp, i));
17814       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
17815     }
17816
17817   if (!target || !register_operand (target, tmode))
17818     target = gen_reg_rtx (tmode);
17819
17820   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
17821   return target;
17822 }
17823
17824 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17825    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
17826    had a language-level syntax for referencing vector elements.  */
17827
17828 static rtx
17829 ix86_expand_vec_ext_builtin (tree exp, rtx target)
17830 {
17831   enum machine_mode tmode, mode0;
17832   tree arg0, arg1;
17833   int elt;
17834   rtx op0;
17835
17836   arg0 = CALL_EXPR_ARG (exp, 0);
17837   arg1 = CALL_EXPR_ARG (exp, 1);
17838
17839   op0 = expand_normal (arg0);
17840   elt = get_element_number (TREE_TYPE (arg0), arg1);
17841
17842   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17843   mode0 = TYPE_MODE (TREE_TYPE (arg0));
17844   gcc_assert (VECTOR_MODE_P (mode0));
17845
17846   op0 = force_reg (mode0, op0);
17847
17848   if (optimize || !target || !register_operand (target, tmode))
17849     target = gen_reg_rtx (tmode);
17850
17851   ix86_expand_vector_extract (true, target, op0, elt);
17852
17853   return target;
17854 }
17855
17856 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
17857    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
17858    a language-level syntax for referencing vector elements.  */
17859
17860 static rtx
17861 ix86_expand_vec_set_builtin (tree exp)
17862 {
17863   enum machine_mode tmode, mode1;
17864   tree arg0, arg1, arg2;
17865   int elt;
17866   rtx op0, op1;
17867
17868   arg0 = CALL_EXPR_ARG (exp, 0);
17869   arg1 = CALL_EXPR_ARG (exp, 1);
17870   arg2 = CALL_EXPR_ARG (exp, 2);
17871
17872   tmode = TYPE_MODE (TREE_TYPE (arg0));
17873   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
17874   gcc_assert (VECTOR_MODE_P (tmode));
17875
17876   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
17877   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
17878   elt = get_element_number (TREE_TYPE (arg0), arg2);
17879
17880   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
17881     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
17882
17883   op0 = force_reg (tmode, op0);
17884   op1 = force_reg (mode1, op1);
17885
17886   ix86_expand_vector_set (true, op0, op1, elt);
17887
17888   return op0;
17889 }
17890
17891 /* Expand an expression EXP that calls a built-in function,
17892    with result going to TARGET if that's convenient
17893    (and in mode MODE if that's convenient).
17894    SUBTARGET may be used as the target for computing one of EXP's operands.
17895    IGNORE is nonzero if the value is to be ignored.  */
17896
17897 static rtx
17898 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
17899                      enum machine_mode mode ATTRIBUTE_UNUSED,
17900                      int ignore ATTRIBUTE_UNUSED)
17901 {
17902   const struct builtin_description *d;
17903   size_t i;
17904   enum insn_code icode;
17905   tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
17906   tree arg0, arg1, arg2, arg3;
17907   rtx op0, op1, op2, op3, pat;
17908   enum machine_mode tmode, mode0, mode1, mode2, mode3, mode4;
17909   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
17910
17911   switch (fcode)
17912     {
17913     case IX86_BUILTIN_EMMS:
17914       emit_insn (gen_mmx_emms ());
17915       return 0;
17916
17917     case IX86_BUILTIN_SFENCE:
17918       emit_insn (gen_sse_sfence ());
17919       return 0;
17920
17921     case IX86_BUILTIN_MASKMOVQ:
17922     case IX86_BUILTIN_MASKMOVDQU:
17923       icode = (fcode == IX86_BUILTIN_MASKMOVQ
17924                ? CODE_FOR_mmx_maskmovq
17925                : CODE_FOR_sse2_maskmovdqu);
17926       /* Note the arg order is different from the operand order.  */
17927       arg1 = CALL_EXPR_ARG (exp, 0);
17928       arg2 = CALL_EXPR_ARG (exp, 1);
17929       arg0 = CALL_EXPR_ARG (exp, 2);
17930       op0 = expand_normal (arg0);
17931       op1 = expand_normal (arg1);
17932       op2 = expand_normal (arg2);
17933       mode0 = insn_data[icode].operand[0].mode;
17934       mode1 = insn_data[icode].operand[1].mode;
17935       mode2 = insn_data[icode].operand[2].mode;
17936
17937       op0 = force_reg (Pmode, op0);
17938       op0 = gen_rtx_MEM (mode1, op0);
17939
17940       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
17941         op0 = copy_to_mode_reg (mode0, op0);
17942       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
17943         op1 = copy_to_mode_reg (mode1, op1);
17944       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
17945         op2 = copy_to_mode_reg (mode2, op2);
17946       pat = GEN_FCN (icode) (op0, op1, op2);
17947       if (! pat)
17948         return 0;
17949       emit_insn (pat);
17950       return 0;
17951
17952     case IX86_BUILTIN_SQRTSS:
17953       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, exp, target);
17954     case IX86_BUILTIN_RSQRTSS:
17955       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, exp, target);
17956     case IX86_BUILTIN_RCPSS:
17957       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, exp, target);
17958
17959     case IX86_BUILTIN_LOADUPS:
17960       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, exp, target, 1);
17961
17962     case IX86_BUILTIN_STOREUPS:
17963       return ix86_expand_store_builtin (CODE_FOR_sse_movups, exp);
17964
17965     case IX86_BUILTIN_LOADHPS:
17966     case IX86_BUILTIN_LOADLPS:
17967     case IX86_BUILTIN_LOADHPD:
17968     case IX86_BUILTIN_LOADLPD:
17969       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
17970                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
17971                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
17972                : CODE_FOR_sse2_loadlpd);
17973       arg0 = CALL_EXPR_ARG (exp, 0);
17974       arg1 = CALL_EXPR_ARG (exp, 1);
17975       op0 = expand_normal (arg0);
17976       op1 = expand_normal (arg1);
17977       tmode = insn_data[icode].operand[0].mode;
17978       mode0 = insn_data[icode].operand[1].mode;
17979       mode1 = insn_data[icode].operand[2].mode;
17980
17981       op0 = force_reg (mode0, op0);
17982       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
17983       if (optimize || target == 0
17984           || GET_MODE (target) != tmode
17985           || !register_operand (target, tmode))
17986         target = gen_reg_rtx (tmode);
17987       pat = GEN_FCN (icode) (target, op0, op1);
17988       if (! pat)
17989         return 0;
17990       emit_insn (pat);
17991       return target;
17992
17993     case IX86_BUILTIN_STOREHPS:
17994     case IX86_BUILTIN_STORELPS:
17995       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
17996                : CODE_FOR_sse_storelps);
17997       arg0 = CALL_EXPR_ARG (exp, 0);
17998       arg1 = CALL_EXPR_ARG (exp, 1);
17999       op0 = expand_normal (arg0);
18000       op1 = expand_normal (arg1);
18001       mode0 = insn_data[icode].operand[0].mode;
18002       mode1 = insn_data[icode].operand[1].mode;
18003
18004       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
18005       op1 = force_reg (mode1, op1);
18006
18007       pat = GEN_FCN (icode) (op0, op1);
18008       if (! pat)
18009         return 0;
18010       emit_insn (pat);
18011       return const0_rtx;
18012
18013     case IX86_BUILTIN_MOVNTPS:
18014       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, exp);
18015     case IX86_BUILTIN_MOVNTQ:
18016       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, exp);
18017
18018     case IX86_BUILTIN_LDMXCSR:
18019       op0 = expand_normal (CALL_EXPR_ARG (exp, 0));
18020       target = assign_386_stack_local (SImode, SLOT_TEMP);
18021       emit_move_insn (target, op0);
18022       emit_insn (gen_sse_ldmxcsr (target));
18023       return 0;
18024
18025     case IX86_BUILTIN_STMXCSR:
18026       target = assign_386_stack_local (SImode, SLOT_TEMP);
18027       emit_insn (gen_sse_stmxcsr (target));
18028       return copy_to_mode_reg (SImode, target);
18029
18030     case IX86_BUILTIN_SHUFPS:
18031     case IX86_BUILTIN_SHUFPD:
18032       icode = (fcode == IX86_BUILTIN_SHUFPS
18033                ? CODE_FOR_sse_shufps
18034                : CODE_FOR_sse2_shufpd);
18035       arg0 = CALL_EXPR_ARG (exp, 0);
18036       arg1 = CALL_EXPR_ARG (exp, 1);
18037       arg2 = CALL_EXPR_ARG (exp, 2);
18038       op0 = expand_normal (arg0);
18039       op1 = expand_normal (arg1);
18040       op2 = expand_normal (arg2);
18041       tmode = insn_data[icode].operand[0].mode;
18042       mode0 = insn_data[icode].operand[1].mode;
18043       mode1 = insn_data[icode].operand[2].mode;
18044       mode2 = insn_data[icode].operand[3].mode;
18045
18046       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
18047         op0 = copy_to_mode_reg (mode0, op0);
18048       if ((optimize && !register_operand (op1, mode1))
18049           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
18050         op1 = copy_to_mode_reg (mode1, op1);
18051       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
18052         {
18053           /* @@@ better error message */
18054           error ("mask must be an immediate");
18055           return gen_reg_rtx (tmode);
18056         }
18057       if (optimize || target == 0
18058           || GET_MODE (target) != tmode
18059           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18060         target = gen_reg_rtx (tmode);
18061       pat = GEN_FCN (icode) (target, op0, op1, op2);
18062       if (! pat)
18063         return 0;
18064       emit_insn (pat);
18065       return target;
18066
18067     case IX86_BUILTIN_PSHUFW:
18068     case IX86_BUILTIN_PSHUFD:
18069     case IX86_BUILTIN_PSHUFHW:
18070     case IX86_BUILTIN_PSHUFLW:
18071       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
18072                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
18073                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
18074                : CODE_FOR_mmx_pshufw);
18075       arg0 = CALL_EXPR_ARG (exp, 0);
18076       arg1 = CALL_EXPR_ARG (exp, 1);
18077       op0 = expand_normal (arg0);
18078       op1 = expand_normal (arg1);
18079       tmode = insn_data[icode].operand[0].mode;
18080       mode1 = insn_data[icode].operand[1].mode;
18081       mode2 = insn_data[icode].operand[2].mode;
18082
18083       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18084         op0 = copy_to_mode_reg (mode1, op0);
18085       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18086         {
18087           /* @@@ better error message */
18088           error ("mask must be an immediate");
18089           return const0_rtx;
18090         }
18091       if (target == 0
18092           || GET_MODE (target) != tmode
18093           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18094         target = gen_reg_rtx (tmode);
18095       pat = GEN_FCN (icode) (target, op0, op1);
18096       if (! pat)
18097         return 0;
18098       emit_insn (pat);
18099       return target;
18100
18101     case IX86_BUILTIN_PSLLWI128:
18102       icode = CODE_FOR_ashlv8hi3;
18103       goto do_pshifti;
18104     case IX86_BUILTIN_PSLLDI128:
18105       icode = CODE_FOR_ashlv4si3;
18106       goto do_pshifti;
18107     case IX86_BUILTIN_PSLLQI128:
18108       icode = CODE_FOR_ashlv2di3;
18109       goto do_pshifti;
18110     case IX86_BUILTIN_PSRAWI128:
18111       icode = CODE_FOR_ashrv8hi3;
18112       goto do_pshifti;
18113     case IX86_BUILTIN_PSRADI128:
18114       icode = CODE_FOR_ashrv4si3;
18115       goto do_pshifti;
18116     case IX86_BUILTIN_PSRLWI128:
18117       icode = CODE_FOR_lshrv8hi3;
18118       goto do_pshifti;
18119     case IX86_BUILTIN_PSRLDI128:
18120       icode = CODE_FOR_lshrv4si3;
18121       goto do_pshifti;
18122     case IX86_BUILTIN_PSRLQI128:
18123       icode = CODE_FOR_lshrv2di3;
18124       goto do_pshifti;
18125     do_pshifti:
18126       arg0 = CALL_EXPR_ARG (exp, 0);
18127       arg1 = CALL_EXPR_ARG (exp, 1);
18128       op0 = expand_normal (arg0);
18129       op1 = expand_normal (arg1);
18130
18131       if (!CONST_INT_P (op1))
18132         {
18133           error ("shift must be an immediate");
18134           return const0_rtx;
18135         }
18136       if (INTVAL (op1) < 0 || INTVAL (op1) > 255)
18137         op1 = GEN_INT (255);
18138
18139       tmode = insn_data[icode].operand[0].mode;
18140       mode1 = insn_data[icode].operand[1].mode;
18141       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18142         op0 = copy_to_reg (op0);
18143
18144       target = gen_reg_rtx (tmode);
18145       pat = GEN_FCN (icode) (target, op0, op1);
18146       if (!pat)
18147         return 0;
18148       emit_insn (pat);
18149       return target;
18150
18151     case IX86_BUILTIN_PSLLW128:
18152       icode = CODE_FOR_ashlv8hi3;
18153       goto do_pshift;
18154     case IX86_BUILTIN_PSLLD128:
18155       icode = CODE_FOR_ashlv4si3;
18156       goto do_pshift;
18157     case IX86_BUILTIN_PSLLQ128:
18158       icode = CODE_FOR_ashlv2di3;
18159       goto do_pshift;
18160     case IX86_BUILTIN_PSRAW128:
18161       icode = CODE_FOR_ashrv8hi3;
18162       goto do_pshift;
18163     case IX86_BUILTIN_PSRAD128:
18164       icode = CODE_FOR_ashrv4si3;
18165       goto do_pshift;
18166     case IX86_BUILTIN_PSRLW128:
18167       icode = CODE_FOR_lshrv8hi3;
18168       goto do_pshift;
18169     case IX86_BUILTIN_PSRLD128:
18170       icode = CODE_FOR_lshrv4si3;
18171       goto do_pshift;
18172     case IX86_BUILTIN_PSRLQ128:
18173       icode = CODE_FOR_lshrv2di3;
18174       goto do_pshift;
18175     do_pshift:
18176       arg0 = CALL_EXPR_ARG (exp, 0);
18177       arg1 = CALL_EXPR_ARG (exp, 1);
18178       op0 = expand_normal (arg0);
18179       op1 = expand_normal (arg1);
18180
18181       tmode = insn_data[icode].operand[0].mode;
18182       mode1 = insn_data[icode].operand[1].mode;
18183
18184       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18185         op0 = copy_to_reg (op0);
18186
18187       op1 = simplify_gen_subreg (TImode, op1, GET_MODE (op1), 0);
18188       if (! (*insn_data[icode].operand[2].predicate) (op1, TImode))
18189         op1 = copy_to_reg (op1);
18190
18191       target = gen_reg_rtx (tmode);
18192       pat = GEN_FCN (icode) (target, op0, op1);
18193       if (!pat)
18194         return 0;
18195       emit_insn (pat);
18196       return target;
18197
18198     case IX86_BUILTIN_PSLLDQI128:
18199     case IX86_BUILTIN_PSRLDQI128:
18200       icode = (fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
18201                : CODE_FOR_sse2_lshrti3);
18202       arg0 = CALL_EXPR_ARG (exp, 0);
18203       arg1 = CALL_EXPR_ARG (exp, 1);
18204       op0 = expand_normal (arg0);
18205       op1 = expand_normal (arg1);
18206       tmode = insn_data[icode].operand[0].mode;
18207       mode1 = insn_data[icode].operand[1].mode;
18208       mode2 = insn_data[icode].operand[2].mode;
18209
18210       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18211         {
18212           op0 = copy_to_reg (op0);
18213           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
18214         }
18215       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18216         {
18217           error ("shift must be an immediate");
18218           return const0_rtx;
18219         }
18220       target = gen_reg_rtx (V2DImode);
18221       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0),
18222                              op0, op1);
18223       if (! pat)
18224         return 0;
18225       emit_insn (pat);
18226       return target;
18227
18228     case IX86_BUILTIN_FEMMS:
18229       emit_insn (gen_mmx_femms ());
18230       return NULL_RTX;
18231
18232     case IX86_BUILTIN_PAVGUSB:
18233       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, exp, target);
18234
18235     case IX86_BUILTIN_PF2ID:
18236       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, exp, target, 0);
18237
18238     case IX86_BUILTIN_PFACC:
18239       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, exp, target);
18240
18241     case IX86_BUILTIN_PFADD:
18242      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, exp, target);
18243
18244     case IX86_BUILTIN_PFCMPEQ:
18245       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, exp, target);
18246
18247     case IX86_BUILTIN_PFCMPGE:
18248       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, exp, target);
18249
18250     case IX86_BUILTIN_PFCMPGT:
18251       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, exp, target);
18252
18253     case IX86_BUILTIN_PFMAX:
18254       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, exp, target);
18255
18256     case IX86_BUILTIN_PFMIN:
18257       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, exp, target);
18258
18259     case IX86_BUILTIN_PFMUL:
18260       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, exp, target);
18261
18262     case IX86_BUILTIN_PFRCP:
18263       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, exp, target, 0);
18264
18265     case IX86_BUILTIN_PFRCPIT1:
18266       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, exp, target);
18267
18268     case IX86_BUILTIN_PFRCPIT2:
18269       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, exp, target);
18270
18271     case IX86_BUILTIN_PFRSQIT1:
18272       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, exp, target);
18273
18274     case IX86_BUILTIN_PFRSQRT:
18275       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, exp, target, 0);
18276
18277     case IX86_BUILTIN_PFSUB:
18278       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, exp, target);
18279
18280     case IX86_BUILTIN_PFSUBR:
18281       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, exp, target);
18282
18283     case IX86_BUILTIN_PI2FD:
18284       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, exp, target, 0);
18285
18286     case IX86_BUILTIN_PMULHRW:
18287       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, exp, target);
18288
18289     case IX86_BUILTIN_PF2IW:
18290       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, exp, target, 0);
18291
18292     case IX86_BUILTIN_PFNACC:
18293       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, exp, target);
18294
18295     case IX86_BUILTIN_PFPNACC:
18296       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, exp, target);
18297
18298     case IX86_BUILTIN_PI2FW:
18299       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, exp, target, 0);
18300
18301     case IX86_BUILTIN_PSWAPDSI:
18302       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, exp, target, 0);
18303
18304     case IX86_BUILTIN_PSWAPDSF:
18305       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, exp, target, 0);
18306
18307     case IX86_BUILTIN_SQRTSD:
18308       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, exp, target);
18309     case IX86_BUILTIN_LOADUPD:
18310       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, exp, target, 1);
18311     case IX86_BUILTIN_STOREUPD:
18312       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, exp);
18313
18314     case IX86_BUILTIN_MFENCE:
18315         emit_insn (gen_sse2_mfence ());
18316         return 0;
18317     case IX86_BUILTIN_LFENCE:
18318         emit_insn (gen_sse2_lfence ());
18319         return 0;
18320
18321     case IX86_BUILTIN_CLFLUSH:
18322         arg0 = CALL_EXPR_ARG (exp, 0);
18323         op0 = expand_normal (arg0);
18324         icode = CODE_FOR_sse2_clflush;
18325         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
18326             op0 = copy_to_mode_reg (Pmode, op0);
18327
18328         emit_insn (gen_sse2_clflush (op0));
18329         return 0;
18330
18331     case IX86_BUILTIN_MOVNTPD:
18332       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, exp);
18333     case IX86_BUILTIN_MOVNTDQ:
18334       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, exp);
18335     case IX86_BUILTIN_MOVNTI:
18336       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, exp);
18337
18338     case IX86_BUILTIN_LOADDQU:
18339       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, exp, target, 1);
18340     case IX86_BUILTIN_STOREDQU:
18341       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, exp);
18342
18343     case IX86_BUILTIN_MONITOR:
18344       arg0 = CALL_EXPR_ARG (exp, 0);
18345       arg1 = CALL_EXPR_ARG (exp, 1);
18346       arg2 = CALL_EXPR_ARG (exp, 2);
18347       op0 = expand_normal (arg0);
18348       op1 = expand_normal (arg1);
18349       op2 = expand_normal (arg2);
18350       if (!REG_P (op0))
18351         op0 = copy_to_mode_reg (Pmode, op0);
18352       if (!REG_P (op1))
18353         op1 = copy_to_mode_reg (SImode, op1);
18354       if (!REG_P (op2))
18355         op2 = copy_to_mode_reg (SImode, op2);
18356       if (!TARGET_64BIT)
18357         emit_insn (gen_sse3_monitor (op0, op1, op2));
18358       else
18359         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
18360       return 0;
18361
18362     case IX86_BUILTIN_MWAIT:
18363       arg0 = CALL_EXPR_ARG (exp, 0);
18364       arg1 = CALL_EXPR_ARG (exp, 1);
18365       op0 = expand_normal (arg0);
18366       op1 = expand_normal (arg1);
18367       if (!REG_P (op0))
18368         op0 = copy_to_mode_reg (SImode, op0);
18369       if (!REG_P (op1))
18370         op1 = copy_to_mode_reg (SImode, op1);
18371       emit_insn (gen_sse3_mwait (op0, op1));
18372       return 0;
18373
18374     case IX86_BUILTIN_LDDQU:
18375       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, exp,
18376                                        target, 1);
18377
18378     case IX86_BUILTIN_PALIGNR:
18379     case IX86_BUILTIN_PALIGNR128:
18380       if (fcode == IX86_BUILTIN_PALIGNR)
18381         {
18382           icode = CODE_FOR_ssse3_palignrdi;
18383           mode = DImode;
18384         }
18385       else
18386         {
18387           icode = CODE_FOR_ssse3_palignrti;
18388           mode = V2DImode;
18389         }
18390       arg0 = CALL_EXPR_ARG (exp, 0);
18391       arg1 = CALL_EXPR_ARG (exp, 1);
18392       arg2 = CALL_EXPR_ARG (exp, 2);
18393       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
18394       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
18395       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
18396       tmode = insn_data[icode].operand[0].mode;
18397       mode1 = insn_data[icode].operand[1].mode;
18398       mode2 = insn_data[icode].operand[2].mode;
18399       mode3 = insn_data[icode].operand[3].mode;
18400
18401       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18402         {
18403           op0 = copy_to_reg (op0);
18404           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
18405         }
18406       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18407         {
18408           op1 = copy_to_reg (op1);
18409           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
18410         }
18411       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18412         {
18413           error ("shift must be an immediate");
18414           return const0_rtx;
18415         }
18416       target = gen_reg_rtx (mode);
18417       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
18418                              op0, op1, op2);
18419       if (! pat)
18420         return 0;
18421       emit_insn (pat);
18422       return target;
18423
18424     case IX86_BUILTIN_MOVNTSD:
18425       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv2df, exp);
18426
18427     case IX86_BUILTIN_MOVNTSS:
18428       return ix86_expand_store_builtin (CODE_FOR_sse4a_vmmovntv4sf, exp);
18429
18430     case IX86_BUILTIN_INSERTQ:
18431     case IX86_BUILTIN_EXTRQ:
18432       icode = (fcode == IX86_BUILTIN_EXTRQ
18433                ? CODE_FOR_sse4a_extrq
18434                : CODE_FOR_sse4a_insertq);
18435       arg0 = CALL_EXPR_ARG (exp, 0);
18436       arg1 = CALL_EXPR_ARG (exp, 1);
18437       op0 = expand_normal (arg0);
18438       op1 = expand_normal (arg1);
18439       tmode = insn_data[icode].operand[0].mode;
18440       mode1 = insn_data[icode].operand[1].mode;
18441       mode2 = insn_data[icode].operand[2].mode;
18442       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18443         op0 = copy_to_mode_reg (mode1, op0);
18444       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18445         op1 = copy_to_mode_reg (mode2, op1);
18446       if (optimize || target == 0
18447           || GET_MODE (target) != tmode
18448           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18449         target = gen_reg_rtx (tmode);
18450       pat = GEN_FCN (icode) (target, op0, op1);
18451       if (! pat)
18452         return NULL_RTX;
18453       emit_insn (pat);
18454       return target;
18455
18456     case IX86_BUILTIN_EXTRQI:
18457       icode = CODE_FOR_sse4a_extrqi;
18458       arg0 = CALL_EXPR_ARG (exp, 0);
18459       arg1 = CALL_EXPR_ARG (exp, 1);
18460       arg2 = CALL_EXPR_ARG (exp, 2);
18461       op0 = expand_normal (arg0);
18462       op1 = expand_normal (arg1);
18463       op2 = expand_normal (arg2);
18464       tmode = insn_data[icode].operand[0].mode;
18465       mode1 = insn_data[icode].operand[1].mode;
18466       mode2 = insn_data[icode].operand[2].mode;
18467       mode3 = insn_data[icode].operand[3].mode;
18468       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18469         op0 = copy_to_mode_reg (mode1, op0);
18470       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18471         {
18472           error ("index mask must be an immediate");
18473           return gen_reg_rtx (tmode);
18474         }
18475       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18476         {
18477           error ("length mask must be an immediate");
18478           return gen_reg_rtx (tmode);
18479         }
18480       if (optimize || target == 0
18481           || GET_MODE (target) != tmode
18482           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18483         target = gen_reg_rtx (tmode);
18484       pat = GEN_FCN (icode) (target, op0, op1, op2);
18485       if (! pat)
18486         return NULL_RTX;
18487       emit_insn (pat);
18488       return target;
18489
18490     case IX86_BUILTIN_INSERTQI:
18491       icode = CODE_FOR_sse4a_insertqi;
18492       arg0 = CALL_EXPR_ARG (exp, 0);
18493       arg1 = CALL_EXPR_ARG (exp, 1);
18494       arg2 = CALL_EXPR_ARG (exp, 2);
18495       arg3 = CALL_EXPR_ARG (exp, 3);
18496       op0 = expand_normal (arg0);
18497       op1 = expand_normal (arg1);
18498       op2 = expand_normal (arg2);
18499       op3 = expand_normal (arg3);
18500       tmode = insn_data[icode].operand[0].mode;
18501       mode1 = insn_data[icode].operand[1].mode;
18502       mode2 = insn_data[icode].operand[2].mode;
18503       mode3 = insn_data[icode].operand[3].mode;
18504       mode4 = insn_data[icode].operand[4].mode;
18505
18506       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
18507         op0 = copy_to_mode_reg (mode1, op0);
18508
18509       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
18510         op1 = copy_to_mode_reg (mode2, op1);
18511
18512       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
18513         {
18514           error ("index mask must be an immediate");
18515           return gen_reg_rtx (tmode);
18516         }
18517       if (! (*insn_data[icode].operand[4].predicate) (op3, mode4))
18518         {
18519           error ("length mask must be an immediate");
18520           return gen_reg_rtx (tmode);
18521         }
18522       if (optimize || target == 0
18523           || GET_MODE (target) != tmode
18524           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
18525         target = gen_reg_rtx (tmode);
18526       pat = GEN_FCN (icode) (target, op0, op1, op2, op3);
18527       if (! pat)
18528         return NULL_RTX;
18529       emit_insn (pat);
18530       return target;
18531
18532     case IX86_BUILTIN_VEC_INIT_V2SI:
18533     case IX86_BUILTIN_VEC_INIT_V4HI:
18534     case IX86_BUILTIN_VEC_INIT_V8QI:
18535       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), exp, target);
18536
18537     case IX86_BUILTIN_VEC_EXT_V2DF:
18538     case IX86_BUILTIN_VEC_EXT_V2DI:
18539     case IX86_BUILTIN_VEC_EXT_V4SF:
18540     case IX86_BUILTIN_VEC_EXT_V4SI:
18541     case IX86_BUILTIN_VEC_EXT_V8HI:
18542     case IX86_BUILTIN_VEC_EXT_V2SI:
18543     case IX86_BUILTIN_VEC_EXT_V4HI:
18544       return ix86_expand_vec_ext_builtin (exp, target);
18545
18546     case IX86_BUILTIN_VEC_SET_V8HI:
18547     case IX86_BUILTIN_VEC_SET_V4HI:
18548       return ix86_expand_vec_set_builtin (exp);
18549
18550     default:
18551       break;
18552     }
18553
18554   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
18555     if (d->code == fcode)
18556       {
18557         /* Compares are treated specially.  */
18558         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
18559             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
18560             || d->icode == CODE_FOR_sse2_maskcmpv2df3
18561             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
18562           return ix86_expand_sse_compare (d, exp, target);
18563
18564         return ix86_expand_binop_builtin (d->icode, exp, target);
18565       }
18566
18567   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
18568     if (d->code == fcode)
18569       return ix86_expand_unop_builtin (d->icode, exp, target, 0);
18570
18571   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
18572     if (d->code == fcode)
18573       return ix86_expand_sse_comi (d, exp, target);
18574
18575   gcc_unreachable ();
18576 }
18577
18578 /* Returns a function decl for a vectorized version of the builtin function
18579    with builtin function code FN and the result vector type TYPE, or NULL_TREE
18580    if it is not available.  */
18581
18582 static tree
18583 ix86_builtin_vectorized_function (enum built_in_function fn, tree type_out,
18584                                   tree type_in)
18585 {
18586   enum machine_mode in_mode, out_mode;
18587   int in_n, out_n;
18588
18589   if (TREE_CODE (type_out) != VECTOR_TYPE
18590       || TREE_CODE (type_in) != VECTOR_TYPE)
18591     return NULL_TREE;
18592
18593   out_mode = TYPE_MODE (TREE_TYPE (type_out));
18594   out_n = TYPE_VECTOR_SUBPARTS (type_out);
18595   in_mode = TYPE_MODE (TREE_TYPE (type_in));
18596   in_n = TYPE_VECTOR_SUBPARTS (type_in);
18597
18598   switch (fn)
18599     {
18600     case BUILT_IN_SQRT:
18601       if (out_mode == DFmode && out_n == 2
18602           && in_mode == DFmode && in_n == 2)
18603         return ix86_builtins[IX86_BUILTIN_SQRTPD];
18604       return NULL_TREE;
18605
18606     case BUILT_IN_SQRTF:
18607       if (out_mode == SFmode && out_n == 4
18608           && in_mode == SFmode && in_n == 4)
18609         return ix86_builtins[IX86_BUILTIN_SQRTPS];
18610       return NULL_TREE;
18611
18612     case BUILT_IN_LRINTF:
18613       if (out_mode == SImode && out_n == 4
18614           && in_mode == SFmode && in_n == 4)
18615         return ix86_builtins[IX86_BUILTIN_CVTPS2DQ];
18616       return NULL_TREE;
18617
18618     default:
18619       ;
18620     }
18621
18622   return NULL_TREE;
18623 }
18624
18625 /* Returns a decl of a function that implements conversion of the
18626    input vector of type TYPE, or NULL_TREE if it is not available.  */
18627
18628 static tree
18629 ix86_builtin_conversion (enum tree_code code, tree type)
18630 {
18631   if (TREE_CODE (type) != VECTOR_TYPE)
18632     return NULL_TREE;
18633   
18634   switch (code)
18635     {
18636     case FLOAT_EXPR:
18637       switch (TYPE_MODE (type))
18638         {
18639         case V4SImode:
18640           return ix86_builtins[IX86_BUILTIN_CVTDQ2PS];
18641         default:
18642           return NULL_TREE;
18643         }
18644
18645     case FIX_TRUNC_EXPR:
18646       switch (TYPE_MODE (type))
18647         {
18648         case V4SFmode:
18649           return ix86_builtins[IX86_BUILTIN_CVTTPS2DQ];
18650         default:
18651           return NULL_TREE;
18652         }
18653     default:
18654       return NULL_TREE;
18655
18656     }
18657 }
18658
18659 /* Store OPERAND to the memory after reload is completed.  This means
18660    that we can't easily use assign_stack_local.  */
18661 rtx
18662 ix86_force_to_memory (enum machine_mode mode, rtx operand)
18663 {
18664   rtx result;
18665
18666   gcc_assert (reload_completed);
18667   if (TARGET_RED_ZONE)
18668     {
18669       result = gen_rtx_MEM (mode,
18670                             gen_rtx_PLUS (Pmode,
18671                                           stack_pointer_rtx,
18672                                           GEN_INT (-RED_ZONE_SIZE)));
18673       emit_move_insn (result, operand);
18674     }
18675   else if (!TARGET_RED_ZONE && TARGET_64BIT)
18676     {
18677       switch (mode)
18678         {
18679         case HImode:
18680         case SImode:
18681           operand = gen_lowpart (DImode, operand);
18682           /* FALLTHRU */
18683         case DImode:
18684           emit_insn (
18685                       gen_rtx_SET (VOIDmode,
18686                                    gen_rtx_MEM (DImode,
18687                                                 gen_rtx_PRE_DEC (DImode,
18688                                                         stack_pointer_rtx)),
18689                                    operand));
18690           break;
18691         default:
18692           gcc_unreachable ();
18693         }
18694       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18695     }
18696   else
18697     {
18698       switch (mode)
18699         {
18700         case DImode:
18701           {
18702             rtx operands[2];
18703             split_di (&operand, 1, operands, operands + 1);
18704             emit_insn (
18705                         gen_rtx_SET (VOIDmode,
18706                                      gen_rtx_MEM (SImode,
18707                                                   gen_rtx_PRE_DEC (Pmode,
18708                                                         stack_pointer_rtx)),
18709                                      operands[1]));
18710             emit_insn (
18711                         gen_rtx_SET (VOIDmode,
18712                                      gen_rtx_MEM (SImode,
18713                                                   gen_rtx_PRE_DEC (Pmode,
18714                                                         stack_pointer_rtx)),
18715                                      operands[0]));
18716           }
18717           break;
18718         case HImode:
18719           /* Store HImodes as SImodes.  */
18720           operand = gen_lowpart (SImode, operand);
18721           /* FALLTHRU */
18722         case SImode:
18723           emit_insn (
18724                       gen_rtx_SET (VOIDmode,
18725                                    gen_rtx_MEM (GET_MODE (operand),
18726                                                 gen_rtx_PRE_DEC (SImode,
18727                                                         stack_pointer_rtx)),
18728                                    operand));
18729           break;
18730         default:
18731           gcc_unreachable ();
18732         }
18733       result = gen_rtx_MEM (mode, stack_pointer_rtx);
18734     }
18735   return result;
18736 }
18737
18738 /* Free operand from the memory.  */
18739 void
18740 ix86_free_from_memory (enum machine_mode mode)
18741 {
18742   if (!TARGET_RED_ZONE)
18743     {
18744       int size;
18745
18746       if (mode == DImode || TARGET_64BIT)
18747         size = 8;
18748       else
18749         size = 4;
18750       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
18751          to pop or add instruction if registers are available.  */
18752       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
18753                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
18754                                             GEN_INT (size))));
18755     }
18756 }
18757
18758 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
18759    QImode must go into class Q_REGS.
18760    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
18761    movdf to do mem-to-mem moves through integer regs.  */
18762 enum reg_class
18763 ix86_preferred_reload_class (rtx x, enum reg_class class)
18764 {
18765   enum machine_mode mode = GET_MODE (x);
18766
18767   /* We're only allowed to return a subclass of CLASS.  Many of the
18768      following checks fail for NO_REGS, so eliminate that early.  */
18769   if (class == NO_REGS)
18770     return NO_REGS;
18771
18772   /* All classes can load zeros.  */
18773   if (x == CONST0_RTX (mode))
18774     return class;
18775
18776   /* Force constants into memory if we are loading a (nonzero) constant into
18777      an MMX or SSE register.  This is because there are no MMX/SSE instructions
18778      to load from a constant.  */
18779   if (CONSTANT_P (x)
18780       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
18781     return NO_REGS;
18782
18783   /* Prefer SSE regs only, if we can use them for math.  */
18784   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
18785     return SSE_CLASS_P (class) ? class : NO_REGS;
18786
18787   /* Floating-point constants need more complex checks.  */
18788   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
18789     {
18790       /* General regs can load everything.  */
18791       if (reg_class_subset_p (class, GENERAL_REGS))
18792         return class;
18793
18794       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
18795          zero above.  We only want to wind up preferring 80387 registers if
18796          we plan on doing computation with them.  */
18797       if (TARGET_80387
18798           && standard_80387_constant_p (x))
18799         {
18800           /* Limit class to non-sse.  */
18801           if (class == FLOAT_SSE_REGS)
18802             return FLOAT_REGS;
18803           if (class == FP_TOP_SSE_REGS)
18804             return FP_TOP_REG;
18805           if (class == FP_SECOND_SSE_REGS)
18806             return FP_SECOND_REG;
18807           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
18808             return class;
18809         }
18810
18811       return NO_REGS;
18812     }
18813
18814   /* Generally when we see PLUS here, it's the function invariant
18815      (plus soft-fp const_int).  Which can only be computed into general
18816      regs.  */
18817   if (GET_CODE (x) == PLUS)
18818     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
18819
18820   /* QImode constants are easy to load, but non-constant QImode data
18821      must go into Q_REGS.  */
18822   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
18823     {
18824       if (reg_class_subset_p (class, Q_REGS))
18825         return class;
18826       if (reg_class_subset_p (Q_REGS, class))
18827         return Q_REGS;
18828       return NO_REGS;
18829     }
18830
18831   return class;
18832 }
18833
18834 /* Discourage putting floating-point values in SSE registers unless
18835    SSE math is being used, and likewise for the 387 registers.  */
18836 enum reg_class
18837 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
18838 {
18839   enum machine_mode mode = GET_MODE (x);
18840
18841   /* Restrict the output reload class to the register bank that we are doing
18842      math on.  If we would like not to return a subset of CLASS, reject this
18843      alternative: if reload cannot do this, it will still use its choice.  */
18844   mode = GET_MODE (x);
18845   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
18846     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
18847
18848   if (TARGET_80387 && SCALAR_FLOAT_MODE_P (mode))
18849     {
18850       if (class == FP_TOP_SSE_REGS)
18851         return FP_TOP_REG;
18852       else if (class == FP_SECOND_SSE_REGS)
18853         return FP_SECOND_REG;
18854       else
18855         return FLOAT_CLASS_P (class) ? class : NO_REGS;
18856     }
18857
18858   return class;
18859 }
18860
18861 /* If we are copying between general and FP registers, we need a memory
18862    location. The same is true for SSE and MMX registers.
18863
18864    The macro can't work reliably when one of the CLASSES is class containing
18865    registers from multiple units (SSE, MMX, integer).  We avoid this by never
18866    combining those units in single alternative in the machine description.
18867    Ensure that this constraint holds to avoid unexpected surprises.
18868
18869    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
18870    enforce these sanity checks.  */
18871
18872 int
18873 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
18874                               enum machine_mode mode, int strict)
18875 {
18876   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
18877       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
18878       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
18879       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
18880       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
18881       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
18882     {
18883       gcc_assert (!strict);
18884       return true;
18885     }
18886
18887   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
18888     return true;
18889
18890   /* ??? This is a lie.  We do have moves between mmx/general, and for
18891      mmx/sse2.  But by saying we need secondary memory we discourage the
18892      register allocator from using the mmx registers unless needed.  */
18893   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
18894     return true;
18895
18896   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
18897     {
18898       /* SSE1 doesn't have any direct moves from other classes.  */
18899       if (!TARGET_SSE2)
18900         return true;
18901
18902       /* If the target says that inter-unit moves are more expensive
18903          than moving through memory, then don't generate them.  */
18904       if (!TARGET_INTER_UNIT_MOVES)
18905         return true;
18906
18907       /* Between SSE and general, we have moves no larger than word size.  */
18908       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
18909         return true;
18910     }
18911
18912   return false;
18913 }
18914
18915 /* Return true if the registers in CLASS cannot represent the change from
18916    modes FROM to TO.  */
18917
18918 bool
18919 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
18920                                enum reg_class class)
18921 {
18922   if (from == to)
18923     return false;
18924
18925   /* x87 registers can't do subreg at all, as all values are reformatted
18926      to extended precision.  */
18927   if (MAYBE_FLOAT_CLASS_P (class))
18928     return true;
18929
18930   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
18931     {
18932       /* Vector registers do not support QI or HImode loads.  If we don't
18933          disallow a change to these modes, reload will assume it's ok to
18934          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
18935          the vec_dupv4hi pattern.  */
18936       if (GET_MODE_SIZE (from) < 4)
18937         return true;
18938
18939       /* Vector registers do not support subreg with nonzero offsets, which
18940          are otherwise valid for integer registers.  Since we can't see
18941          whether we have a nonzero offset from here, prohibit all
18942          nonparadoxical subregs changing size.  */
18943       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
18944         return true;
18945     }
18946
18947   return false;
18948 }
18949
18950 /* Return the cost of moving data from a register in class CLASS1 to
18951    one in class CLASS2.
18952
18953    It is not required that the cost always equal 2 when FROM is the same as TO;
18954    on some machines it is expensive to move between registers if they are not
18955    general registers.  */
18956
18957 int
18958 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
18959                          enum reg_class class2)
18960 {
18961   /* In case we require secondary memory, compute cost of the store followed
18962      by load.  In order to avoid bad register allocation choices, we need
18963      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
18964
18965   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
18966     {
18967       int cost = 1;
18968
18969       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
18970                    MEMORY_MOVE_COST (mode, class1, 1));
18971       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
18972                    MEMORY_MOVE_COST (mode, class2, 1));
18973
18974       /* In case of copying from general_purpose_register we may emit multiple
18975          stores followed by single load causing memory size mismatch stall.
18976          Count this as arbitrarily high cost of 20.  */
18977       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
18978         cost += 20;
18979
18980       /* In the case of FP/MMX moves, the registers actually overlap, and we
18981          have to switch modes in order to treat them differently.  */
18982       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
18983           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
18984         cost += 20;
18985
18986       return cost;
18987     }
18988
18989   /* Moves between SSE/MMX and integer unit are expensive.  */
18990   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
18991       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
18992     return ix86_cost->mmxsse_to_integer;
18993   if (MAYBE_FLOAT_CLASS_P (class1))
18994     return ix86_cost->fp_move;
18995   if (MAYBE_SSE_CLASS_P (class1))
18996     return ix86_cost->sse_move;
18997   if (MAYBE_MMX_CLASS_P (class1))
18998     return ix86_cost->mmx_move;
18999   return 2;
19000 }
19001
19002 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
19003
19004 bool
19005 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
19006 {
19007   /* Flags and only flags can only hold CCmode values.  */
19008   if (CC_REGNO_P (regno))
19009     return GET_MODE_CLASS (mode) == MODE_CC;
19010   if (GET_MODE_CLASS (mode) == MODE_CC
19011       || GET_MODE_CLASS (mode) == MODE_RANDOM
19012       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
19013     return 0;
19014   if (FP_REGNO_P (regno))
19015     return VALID_FP_MODE_P (mode);
19016   if (SSE_REGNO_P (regno))
19017     {
19018       /* We implement the move patterns for all vector modes into and
19019          out of SSE registers, even when no operation instructions
19020          are available.  */
19021       return (VALID_SSE_REG_MODE (mode)
19022               || VALID_SSE2_REG_MODE (mode)
19023               || VALID_MMX_REG_MODE (mode)
19024               || VALID_MMX_REG_MODE_3DNOW (mode));
19025     }
19026   if (MMX_REGNO_P (regno))
19027     {
19028       /* We implement the move patterns for 3DNOW modes even in MMX mode,
19029          so if the register is available at all, then we can move data of
19030          the given mode into or out of it.  */
19031       return (VALID_MMX_REG_MODE (mode)
19032               || VALID_MMX_REG_MODE_3DNOW (mode));
19033     }
19034
19035   if (mode == QImode)
19036     {
19037       /* Take care for QImode values - they can be in non-QI regs,
19038          but then they do cause partial register stalls.  */
19039       if (regno < 4 || TARGET_64BIT)
19040         return 1;
19041       if (!TARGET_PARTIAL_REG_STALL)
19042         return 1;
19043       return reload_in_progress || reload_completed;
19044     }
19045   /* We handle both integer and floats in the general purpose registers.  */
19046   else if (VALID_INT_MODE_P (mode))
19047     return 1;
19048   else if (VALID_FP_MODE_P (mode))
19049     return 1;
19050   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
19051      on to use that value in smaller contexts, this can easily force a
19052      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
19053      supporting DImode, allow it.  */
19054   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
19055     return 1;
19056
19057   return 0;
19058 }
19059
19060 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
19061    tieable integer mode.  */
19062
19063 static bool
19064 ix86_tieable_integer_mode_p (enum machine_mode mode)
19065 {
19066   switch (mode)
19067     {
19068     case HImode:
19069     case SImode:
19070       return true;
19071
19072     case QImode:
19073       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
19074
19075     case DImode:
19076       return TARGET_64BIT;
19077
19078     default:
19079       return false;
19080     }
19081 }
19082
19083 /* Return true if MODE1 is accessible in a register that can hold MODE2
19084    without copying.  That is, all register classes that can hold MODE2
19085    can also hold MODE1.  */
19086
19087 bool
19088 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
19089 {
19090   if (mode1 == mode2)
19091     return true;
19092
19093   if (ix86_tieable_integer_mode_p (mode1)
19094       && ix86_tieable_integer_mode_p (mode2))
19095     return true;
19096
19097   /* MODE2 being XFmode implies fp stack or general regs, which means we
19098      can tie any smaller floating point modes to it.  Note that we do not
19099      tie this with TFmode.  */
19100   if (mode2 == XFmode)
19101     return mode1 == SFmode || mode1 == DFmode;
19102
19103   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
19104      that we can tie it with SFmode.  */
19105   if (mode2 == DFmode)
19106     return mode1 == SFmode;
19107
19108   /* If MODE2 is only appropriate for an SSE register, then tie with
19109      any other mode acceptable to SSE registers.  */
19110   if (GET_MODE_SIZE (mode2) == 16
19111       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
19112     return (GET_MODE_SIZE (mode1) == 16
19113             && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1));
19114
19115   /* If MODE2 is appropriate for an MMX register, then tie
19116      with any other mode acceptable to MMX registers.  */
19117   if (GET_MODE_SIZE (mode2) == 8
19118       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
19119     return (GET_MODE_SIZE (mode1) == 8
19120             && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1));
19121
19122   return false;
19123 }
19124
19125 /* Return the cost of moving data of mode M between a
19126    register and memory.  A value of 2 is the default; this cost is
19127    relative to those in `REGISTER_MOVE_COST'.
19128
19129    If moving between registers and memory is more expensive than
19130    between two registers, you should define this macro to express the
19131    relative cost.
19132
19133    Model also increased moving costs of QImode registers in non
19134    Q_REGS classes.
19135  */
19136 int
19137 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
19138 {
19139   if (FLOAT_CLASS_P (class))
19140     {
19141       int index;
19142       switch (mode)
19143         {
19144           case SFmode:
19145             index = 0;
19146             break;
19147           case DFmode:
19148             index = 1;
19149             break;
19150           case XFmode:
19151             index = 2;
19152             break;
19153           default:
19154             return 100;
19155         }
19156       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
19157     }
19158   if (SSE_CLASS_P (class))
19159     {
19160       int index;
19161       switch (GET_MODE_SIZE (mode))
19162         {
19163           case 4:
19164             index = 0;
19165             break;
19166           case 8:
19167             index = 1;
19168             break;
19169           case 16:
19170             index = 2;
19171             break;
19172           default:
19173             return 100;
19174         }
19175       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
19176     }
19177   if (MMX_CLASS_P (class))
19178     {
19179       int index;
19180       switch (GET_MODE_SIZE (mode))
19181         {
19182           case 4:
19183             index = 0;
19184             break;
19185           case 8:
19186             index = 1;
19187             break;
19188           default:
19189             return 100;
19190         }
19191       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
19192     }
19193   switch (GET_MODE_SIZE (mode))
19194     {
19195       case 1:
19196         if (in)
19197           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
19198                   : ix86_cost->movzbl_load);
19199         else
19200           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
19201                   : ix86_cost->int_store[0] + 4);
19202         break;
19203       case 2:
19204         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
19205       default:
19206         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
19207         if (mode == TFmode)
19208           mode = XFmode;
19209         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
19210                 * (((int) GET_MODE_SIZE (mode)
19211                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
19212     }
19213 }
19214
19215 /* Compute a (partial) cost for rtx X.  Return true if the complete
19216    cost has been computed, and false if subexpressions should be
19217    scanned.  In either case, *TOTAL contains the cost result.  */
19218
19219 static bool
19220 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
19221 {
19222   enum machine_mode mode = GET_MODE (x);
19223
19224   switch (code)
19225     {
19226     case CONST_INT:
19227     case CONST:
19228     case LABEL_REF:
19229     case SYMBOL_REF:
19230       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
19231         *total = 3;
19232       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
19233         *total = 2;
19234       else if (flag_pic && SYMBOLIC_CONST (x)
19235                && (!TARGET_64BIT
19236                    || (!GET_CODE (x) != LABEL_REF
19237                        && (GET_CODE (x) != SYMBOL_REF
19238                            || !SYMBOL_REF_LOCAL_P (x)))))
19239         *total = 1;
19240       else
19241         *total = 0;
19242       return true;
19243
19244     case CONST_DOUBLE:
19245       if (mode == VOIDmode)
19246         *total = 0;
19247       else
19248         switch (standard_80387_constant_p (x))
19249           {
19250           case 1: /* 0.0 */
19251             *total = 1;
19252             break;
19253           default: /* Other constants */
19254             *total = 2;
19255             break;
19256           case 0:
19257           case -1:
19258             /* Start with (MEM (SYMBOL_REF)), since that's where
19259                it'll probably end up.  Add a penalty for size.  */
19260             *total = (COSTS_N_INSNS (1)
19261                       + (flag_pic != 0 && !TARGET_64BIT)
19262                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
19263             break;
19264           }
19265       return true;
19266
19267     case ZERO_EXTEND:
19268       /* The zero extensions is often completely free on x86_64, so make
19269          it as cheap as possible.  */
19270       if (TARGET_64BIT && mode == DImode
19271           && GET_MODE (XEXP (x, 0)) == SImode)
19272         *total = 1;
19273       else if (TARGET_ZERO_EXTEND_WITH_AND)
19274         *total = ix86_cost->add;
19275       else
19276         *total = ix86_cost->movzx;
19277       return false;
19278
19279     case SIGN_EXTEND:
19280       *total = ix86_cost->movsx;
19281       return false;
19282
19283     case ASHIFT:
19284       if (CONST_INT_P (XEXP (x, 1))
19285           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
19286         {
19287           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
19288           if (value == 1)
19289             {
19290               *total = ix86_cost->add;
19291               return false;
19292             }
19293           if ((value == 2 || value == 3)
19294               && ix86_cost->lea <= ix86_cost->shift_const)
19295             {
19296               *total = ix86_cost->lea;
19297               return false;
19298             }
19299         }
19300       /* FALLTHRU */
19301
19302     case ROTATE:
19303     case ASHIFTRT:
19304     case LSHIFTRT:
19305     case ROTATERT:
19306       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
19307         {
19308           if (CONST_INT_P (XEXP (x, 1)))
19309             {
19310               if (INTVAL (XEXP (x, 1)) > 32)
19311                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
19312               else
19313                 *total = ix86_cost->shift_const * 2;
19314             }
19315           else
19316             {
19317               if (GET_CODE (XEXP (x, 1)) == AND)
19318                 *total = ix86_cost->shift_var * 2;
19319               else
19320                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
19321             }
19322         }
19323       else
19324         {
19325           if (CONST_INT_P (XEXP (x, 1)))
19326             *total = ix86_cost->shift_const;
19327           else
19328             *total = ix86_cost->shift_var;
19329         }
19330       return false;
19331
19332     case MULT:
19333       if (FLOAT_MODE_P (mode))
19334         {
19335           *total = ix86_cost->fmul;
19336           return false;
19337         }
19338       else
19339         {
19340           rtx op0 = XEXP (x, 0);
19341           rtx op1 = XEXP (x, 1);
19342           int nbits;
19343           if (CONST_INT_P (XEXP (x, 1)))
19344             {
19345               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
19346               for (nbits = 0; value != 0; value &= value - 1)
19347                 nbits++;
19348             }
19349           else
19350             /* This is arbitrary.  */
19351             nbits = 7;
19352
19353           /* Compute costs correctly for widening multiplication.  */
19354           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
19355               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
19356                  == GET_MODE_SIZE (mode))
19357             {
19358               int is_mulwiden = 0;
19359               enum machine_mode inner_mode = GET_MODE (op0);
19360
19361               if (GET_CODE (op0) == GET_CODE (op1))
19362                 is_mulwiden = 1, op1 = XEXP (op1, 0);
19363               else if (CONST_INT_P (op1))
19364                 {
19365                   if (GET_CODE (op0) == SIGN_EXTEND)
19366                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
19367                                   == INTVAL (op1);
19368                   else
19369                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
19370                 }
19371
19372               if (is_mulwiden)
19373                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
19374             }
19375
19376           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
19377                     + nbits * ix86_cost->mult_bit
19378                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
19379
19380           return true;
19381         }
19382
19383     case DIV:
19384     case UDIV:
19385     case MOD:
19386     case UMOD:
19387       if (FLOAT_MODE_P (mode))
19388         *total = ix86_cost->fdiv;
19389       else
19390         *total = ix86_cost->divide[MODE_INDEX (mode)];
19391       return false;
19392
19393     case PLUS:
19394       if (FLOAT_MODE_P (mode))
19395         *total = ix86_cost->fadd;
19396       else if (GET_MODE_CLASS (mode) == MODE_INT
19397                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
19398         {
19399           if (GET_CODE (XEXP (x, 0)) == PLUS
19400               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
19401               && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
19402               && CONSTANT_P (XEXP (x, 1)))
19403             {
19404               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
19405               if (val == 2 || val == 4 || val == 8)
19406                 {
19407                   *total = ix86_cost->lea;
19408                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19409                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
19410                                       outer_code);
19411                   *total += rtx_cost (XEXP (x, 1), outer_code);
19412                   return true;
19413                 }
19414             }
19415           else if (GET_CODE (XEXP (x, 0)) == MULT
19416                    && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
19417             {
19418               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
19419               if (val == 2 || val == 4 || val == 8)
19420                 {
19421                   *total = ix86_cost->lea;
19422                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19423                   *total += rtx_cost (XEXP (x, 1), outer_code);
19424                   return true;
19425                 }
19426             }
19427           else if (GET_CODE (XEXP (x, 0)) == PLUS)
19428             {
19429               *total = ix86_cost->lea;
19430               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
19431               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
19432               *total += rtx_cost (XEXP (x, 1), outer_code);
19433               return true;
19434             }
19435         }
19436       /* FALLTHRU */
19437
19438     case MINUS:
19439       if (FLOAT_MODE_P (mode))
19440         {
19441           *total = ix86_cost->fadd;
19442           return false;
19443         }
19444       /* FALLTHRU */
19445
19446     case AND:
19447     case IOR:
19448     case XOR:
19449       if (!TARGET_64BIT && mode == DImode)
19450         {
19451           *total = (ix86_cost->add * 2
19452                     + (rtx_cost (XEXP (x, 0), outer_code)
19453                        << (GET_MODE (XEXP (x, 0)) != DImode))
19454                     + (rtx_cost (XEXP (x, 1), outer_code)
19455                        << (GET_MODE (XEXP (x, 1)) != DImode)));
19456           return true;
19457         }
19458       /* FALLTHRU */
19459
19460     case NEG:
19461       if (FLOAT_MODE_P (mode))
19462         {
19463           *total = ix86_cost->fchs;
19464           return false;
19465         }
19466       /* FALLTHRU */
19467
19468     case NOT:
19469       if (!TARGET_64BIT && mode == DImode)
19470         *total = ix86_cost->add * 2;
19471       else
19472         *total = ix86_cost->add;
19473       return false;
19474
19475     case COMPARE:
19476       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
19477           && XEXP (XEXP (x, 0), 1) == const1_rtx
19478           && CONST_INT_P (XEXP (XEXP (x, 0), 2))
19479           && XEXP (x, 1) == const0_rtx)
19480         {
19481           /* This kind of construct is implemented using test[bwl].
19482              Treat it as if we had an AND.  */
19483           *total = (ix86_cost->add
19484                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
19485                     + rtx_cost (const1_rtx, outer_code));
19486           return true;
19487         }
19488       return false;
19489
19490     case FLOAT_EXTEND:
19491       if (!TARGET_SSE_MATH
19492           || mode == XFmode
19493           || (mode == DFmode && !TARGET_SSE2))
19494         *total = 0;
19495       return false;
19496
19497     case ABS:
19498       if (FLOAT_MODE_P (mode))
19499         *total = ix86_cost->fabs;
19500       return false;
19501
19502     case SQRT:
19503       if (FLOAT_MODE_P (mode))
19504         *total = ix86_cost->fsqrt;
19505       return false;
19506
19507     case UNSPEC:
19508       if (XINT (x, 1) == UNSPEC_TP)
19509         *total = 0;
19510       return false;
19511
19512     default:
19513       return false;
19514     }
19515 }
19516
19517 #if TARGET_MACHO
19518
19519 static int current_machopic_label_num;
19520
19521 /* Given a symbol name and its associated stub, write out the
19522    definition of the stub.  */
19523
19524 void
19525 machopic_output_stub (FILE *file, const char *symb, const char *stub)
19526 {
19527   unsigned int length;
19528   char *binder_name, *symbol_name, lazy_ptr_name[32];
19529   int label = ++current_machopic_label_num;
19530
19531   /* For 64-bit we shouldn't get here.  */
19532   gcc_assert (!TARGET_64BIT);
19533
19534   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
19535   symb = (*targetm.strip_name_encoding) (symb);
19536
19537   length = strlen (stub);
19538   binder_name = alloca (length + 32);
19539   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
19540
19541   length = strlen (symb);
19542   symbol_name = alloca (length + 32);
19543   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
19544
19545   sprintf (lazy_ptr_name, "L%d$lz", label);
19546
19547   if (MACHOPIC_PURE)
19548     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
19549   else
19550     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
19551
19552   fprintf (file, "%s:\n", stub);
19553   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19554
19555   if (MACHOPIC_PURE)
19556     {
19557       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
19558       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
19559       fprintf (file, "\tjmp\t*%%edx\n");
19560     }
19561   else
19562     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
19563
19564   fprintf (file, "%s:\n", binder_name);
19565
19566   if (MACHOPIC_PURE)
19567     {
19568       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
19569       fprintf (file, "\tpushl\t%%eax\n");
19570     }
19571   else
19572     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
19573
19574   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
19575
19576   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
19577   fprintf (file, "%s:\n", lazy_ptr_name);
19578   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
19579   fprintf (file, "\t.long %s\n", binder_name);
19580 }
19581
19582 void
19583 darwin_x86_file_end (void)
19584 {
19585   darwin_file_end ();
19586   ix86_file_end ();
19587 }
19588 #endif /* TARGET_MACHO */
19589
19590 /* Order the registers for register allocator.  */
19591
19592 void
19593 x86_order_regs_for_local_alloc (void)
19594 {
19595    int pos = 0;
19596    int i;
19597
19598    /* First allocate the local general purpose registers.  */
19599    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19600      if (GENERAL_REGNO_P (i) && call_used_regs[i])
19601         reg_alloc_order [pos++] = i;
19602
19603    /* Global general purpose registers.  */
19604    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
19605      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
19606         reg_alloc_order [pos++] = i;
19607
19608    /* x87 registers come first in case we are doing FP math
19609       using them.  */
19610    if (!TARGET_SSE_MATH)
19611      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19612        reg_alloc_order [pos++] = i;
19613
19614    /* SSE registers.  */
19615    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
19616      reg_alloc_order [pos++] = i;
19617    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
19618      reg_alloc_order [pos++] = i;
19619
19620    /* x87 registers.  */
19621    if (TARGET_SSE_MATH)
19622      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
19623        reg_alloc_order [pos++] = i;
19624
19625    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
19626      reg_alloc_order [pos++] = i;
19627
19628    /* Initialize the rest of array as we do not allocate some registers
19629       at all.  */
19630    while (pos < FIRST_PSEUDO_REGISTER)
19631      reg_alloc_order [pos++] = 0;
19632 }
19633
19634 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
19635    struct attribute_spec.handler.  */
19636 static tree
19637 ix86_handle_struct_attribute (tree *node, tree name,
19638                               tree args ATTRIBUTE_UNUSED,
19639                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
19640 {
19641   tree *type = NULL;
19642   if (DECL_P (*node))
19643     {
19644       if (TREE_CODE (*node) == TYPE_DECL)
19645         type = &TREE_TYPE (*node);
19646     }
19647   else
19648     type = node;
19649
19650   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
19651                  || TREE_CODE (*type) == UNION_TYPE)))
19652     {
19653       warning (OPT_Wattributes, "%qs attribute ignored",
19654                IDENTIFIER_POINTER (name));
19655       *no_add_attrs = true;
19656     }
19657
19658   else if ((is_attribute_p ("ms_struct", name)
19659             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
19660            || ((is_attribute_p ("gcc_struct", name)
19661                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
19662     {
19663       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
19664                IDENTIFIER_POINTER (name));
19665       *no_add_attrs = true;
19666     }
19667
19668   return NULL_TREE;
19669 }
19670
19671 static bool
19672 ix86_ms_bitfield_layout_p (tree record_type)
19673 {
19674   return (TARGET_MS_BITFIELD_LAYOUT &&
19675           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
19676     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
19677 }
19678
19679 /* Returns an expression indicating where the this parameter is
19680    located on entry to the FUNCTION.  */
19681
19682 static rtx
19683 x86_this_parameter (tree function)
19684 {
19685   tree type = TREE_TYPE (function);
19686   bool aggr = aggregate_value_p (TREE_TYPE (type), type) != 0;
19687
19688   if (TARGET_64BIT)
19689     {
19690       const int *parm_regs;
19691
19692       if (TARGET_64BIT_MS_ABI)
19693         parm_regs = x86_64_ms_abi_int_parameter_registers;
19694       else
19695         parm_regs = x86_64_int_parameter_registers;
19696       return gen_rtx_REG (DImode, parm_regs[aggr]);
19697     }
19698
19699   if (ix86_function_regparm (type, function) > 0
19700       && !type_has_variadic_args_p (type))
19701     {
19702       int regno = 0;
19703       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
19704         regno = 2;
19705       return gen_rtx_REG (SImode, regno);
19706     }
19707
19708   return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, aggr ? 8 : 4));
19709 }
19710
19711 /* Determine whether x86_output_mi_thunk can succeed.  */
19712
19713 static bool
19714 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
19715                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
19716                          HOST_WIDE_INT vcall_offset, tree function)
19717 {
19718   /* 64-bit can handle anything.  */
19719   if (TARGET_64BIT)
19720     return true;
19721
19722   /* For 32-bit, everything's fine if we have one free register.  */
19723   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
19724     return true;
19725
19726   /* Need a free register for vcall_offset.  */
19727   if (vcall_offset)
19728     return false;
19729
19730   /* Need a free register for GOT references.  */
19731   if (flag_pic && !(*targetm.binds_local_p) (function))
19732     return false;
19733
19734   /* Otherwise ok.  */
19735   return true;
19736 }
19737
19738 /* Output the assembler code for a thunk function.  THUNK_DECL is the
19739    declaration for the thunk function itself, FUNCTION is the decl for
19740    the target function.  DELTA is an immediate constant offset to be
19741    added to THIS.  If VCALL_OFFSET is nonzero, the word at
19742    *(*this + vcall_offset) should be added to THIS.  */
19743
19744 static void
19745 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
19746                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
19747                      HOST_WIDE_INT vcall_offset, tree function)
19748 {
19749   rtx xops[3];
19750   rtx this = x86_this_parameter (function);
19751   rtx this_reg, tmp;
19752
19753   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
19754      pull it in now and let DELTA benefit.  */
19755   if (REG_P (this))
19756     this_reg = this;
19757   else if (vcall_offset)
19758     {
19759       /* Put the this parameter into %eax.  */
19760       xops[0] = this;
19761       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
19762       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19763     }
19764   else
19765     this_reg = NULL_RTX;
19766
19767   /* Adjust the this parameter by a fixed constant.  */
19768   if (delta)
19769     {
19770       xops[0] = GEN_INT (delta);
19771       xops[1] = this_reg ? this_reg : this;
19772       if (TARGET_64BIT)
19773         {
19774           if (!x86_64_general_operand (xops[0], DImode))
19775             {
19776               tmp = gen_rtx_REG (DImode, R10_REG);
19777               xops[1] = tmp;
19778               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
19779               xops[0] = tmp;
19780               xops[1] = this;
19781             }
19782           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19783         }
19784       else
19785         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19786     }
19787
19788   /* Adjust the this parameter by a value stored in the vtable.  */
19789   if (vcall_offset)
19790     {
19791       if (TARGET_64BIT)
19792         tmp = gen_rtx_REG (DImode, R10_REG);
19793       else
19794         {
19795           int tmp_regno = 2 /* ECX */;
19796           if (lookup_attribute ("fastcall",
19797                                 TYPE_ATTRIBUTES (TREE_TYPE (function))))
19798             tmp_regno = 0 /* EAX */;
19799           tmp = gen_rtx_REG (SImode, tmp_regno);
19800         }
19801
19802       xops[0] = gen_rtx_MEM (Pmode, this_reg);
19803       xops[1] = tmp;
19804       if (TARGET_64BIT)
19805         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19806       else
19807         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19808
19809       /* Adjust the this parameter.  */
19810       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
19811       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
19812         {
19813           rtx tmp2 = gen_rtx_REG (DImode, R11_REG);
19814           xops[0] = GEN_INT (vcall_offset);
19815           xops[1] = tmp2;
19816           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
19817           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
19818         }
19819       xops[1] = this_reg;
19820       if (TARGET_64BIT)
19821         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
19822       else
19823         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
19824     }
19825
19826   /* If necessary, drop THIS back to its stack slot.  */
19827   if (this_reg && this_reg != this)
19828     {
19829       xops[0] = this_reg;
19830       xops[1] = this;
19831       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
19832     }
19833
19834   xops[0] = XEXP (DECL_RTL (function), 0);
19835   if (TARGET_64BIT)
19836     {
19837       if (!flag_pic || (*targetm.binds_local_p) (function))
19838         output_asm_insn ("jmp\t%P0", xops);
19839       /* All thunks should be in the same object as their target,
19840          and thus binds_local_p should be true.  */
19841       else if (TARGET_64BIT_MS_ABI)
19842         gcc_unreachable ();
19843       else
19844         {
19845           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
19846           tmp = gen_rtx_CONST (Pmode, tmp);
19847           tmp = gen_rtx_MEM (QImode, tmp);
19848           xops[0] = tmp;
19849           output_asm_insn ("jmp\t%A0", xops);
19850         }
19851     }
19852   else
19853     {
19854       if (!flag_pic || (*targetm.binds_local_p) (function))
19855         output_asm_insn ("jmp\t%P0", xops);
19856       else
19857 #if TARGET_MACHO
19858         if (TARGET_MACHO)
19859           {
19860             rtx sym_ref = XEXP (DECL_RTL (function), 0);
19861             tmp = (gen_rtx_SYMBOL_REF
19862                    (Pmode,
19863                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
19864             tmp = gen_rtx_MEM (QImode, tmp);
19865             xops[0] = tmp;
19866             output_asm_insn ("jmp\t%0", xops);
19867           }
19868         else
19869 #endif /* TARGET_MACHO */
19870         {
19871           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
19872           output_set_got (tmp, NULL_RTX);
19873
19874           xops[1] = tmp;
19875           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
19876           output_asm_insn ("jmp\t{*}%1", xops);
19877         }
19878     }
19879 }
19880
19881 static void
19882 x86_file_start (void)
19883 {
19884   default_file_start ();
19885 #if TARGET_MACHO
19886   darwin_file_start ();
19887 #endif
19888   if (X86_FILE_START_VERSION_DIRECTIVE)
19889     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
19890   if (X86_FILE_START_FLTUSED)
19891     fputs ("\t.global\t__fltused\n", asm_out_file);
19892   if (ix86_asm_dialect == ASM_INTEL)
19893     fputs ("\t.intel_syntax\n", asm_out_file);
19894 }
19895
19896 int
19897 x86_field_alignment (tree field, int computed)
19898 {
19899   enum machine_mode mode;
19900   tree type = TREE_TYPE (field);
19901
19902   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
19903     return computed;
19904   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
19905                     ? get_inner_array_type (type) : type);
19906   if (mode == DFmode || mode == DCmode
19907       || GET_MODE_CLASS (mode) == MODE_INT
19908       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
19909     return MIN (32, computed);
19910   return computed;
19911 }
19912
19913 /* Output assembler code to FILE to increment profiler label # LABELNO
19914    for profiling a function entry.  */
19915 void
19916 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
19917 {
19918   if (TARGET_64BIT)
19919     {
19920 #ifndef NO_PROFILE_COUNTERS
19921       fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
19922 #endif
19923
19924       if (!TARGET_64BIT_MS_ABI && flag_pic)
19925         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
19926       else
19927         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
19928     }
19929   else if (flag_pic)
19930     {
19931 #ifndef NO_PROFILE_COUNTERS
19932       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
19933                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
19934 #endif
19935       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
19936     }
19937   else
19938     {
19939 #ifndef NO_PROFILE_COUNTERS
19940       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
19941                PROFILE_COUNT_REGISTER);
19942 #endif
19943       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
19944     }
19945 }
19946
19947 /* We don't have exact information about the insn sizes, but we may assume
19948    quite safely that we are informed about all 1 byte insns and memory
19949    address sizes.  This is enough to eliminate unnecessary padding in
19950    99% of cases.  */
19951
19952 static int
19953 min_insn_size (rtx insn)
19954 {
19955   int l = 0;
19956
19957   if (!INSN_P (insn) || !active_insn_p (insn))
19958     return 0;
19959
19960   /* Discard alignments we've emit and jump instructions.  */
19961   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
19962       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
19963     return 0;
19964   if (JUMP_P (insn)
19965       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
19966           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
19967     return 0;
19968
19969   /* Important case - calls are always 5 bytes.
19970      It is common to have many calls in the row.  */
19971   if (CALL_P (insn)
19972       && symbolic_reference_mentioned_p (PATTERN (insn))
19973       && !SIBLING_CALL_P (insn))
19974     return 5;
19975   if (get_attr_length (insn) <= 1)
19976     return 1;
19977
19978   /* For normal instructions we may rely on the sizes of addresses
19979      and the presence of symbol to require 4 bytes of encoding.
19980      This is not the case for jumps where references are PC relative.  */
19981   if (!JUMP_P (insn))
19982     {
19983       l = get_attr_length_address (insn);
19984       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
19985         l = 4;
19986     }
19987   if (l)
19988     return 1+l;
19989   else
19990     return 2;
19991 }
19992
19993 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
19994    window.  */
19995
19996 static void
19997 ix86_avoid_jump_misspredicts (void)
19998 {
19999   rtx insn, start = get_insns ();
20000   int nbytes = 0, njumps = 0;
20001   int isjump = 0;
20002
20003   /* Look for all minimal intervals of instructions containing 4 jumps.
20004      The intervals are bounded by START and INSN.  NBYTES is the total
20005      size of instructions in the interval including INSN and not including
20006      START.  When the NBYTES is smaller than 16 bytes, it is possible
20007      that the end of START and INSN ends up in the same 16byte page.
20008
20009      The smallest offset in the page INSN can start is the case where START
20010      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
20011      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
20012      */
20013   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
20014     {
20015
20016       nbytes += min_insn_size (insn);
20017       if (dump_file)
20018         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
20019                 INSN_UID (insn), min_insn_size (insn));
20020       if ((JUMP_P (insn)
20021            && GET_CODE (PATTERN (insn)) != ADDR_VEC
20022            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
20023           || CALL_P (insn))
20024         njumps++;
20025       else
20026         continue;
20027
20028       while (njumps > 3)
20029         {
20030           start = NEXT_INSN (start);
20031           if ((JUMP_P (start)
20032                && GET_CODE (PATTERN (start)) != ADDR_VEC
20033                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
20034               || CALL_P (start))
20035             njumps--, isjump = 1;
20036           else
20037             isjump = 0;
20038           nbytes -= min_insn_size (start);
20039         }
20040       gcc_assert (njumps >= 0);
20041       if (dump_file)
20042         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
20043                 INSN_UID (start), INSN_UID (insn), nbytes);
20044
20045       if (njumps == 3 && isjump && nbytes < 16)
20046         {
20047           int padsize = 15 - nbytes + min_insn_size (insn);
20048
20049           if (dump_file)
20050             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
20051                      INSN_UID (insn), padsize);
20052           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
20053         }
20054     }
20055 }
20056
20057 /* AMD Athlon works faster
20058    when RET is not destination of conditional jump or directly preceded
20059    by other jump instruction.  We avoid the penalty by inserting NOP just
20060    before the RET instructions in such cases.  */
20061 static void
20062 ix86_pad_returns (void)
20063 {
20064   edge e;
20065   edge_iterator ei;
20066
20067   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
20068     {
20069       basic_block bb = e->src;
20070       rtx ret = BB_END (bb);
20071       rtx prev;
20072       bool replace = false;
20073
20074       if (!JUMP_P (ret) || GET_CODE (PATTERN (ret)) != RETURN
20075           || !maybe_hot_bb_p (bb))
20076         continue;
20077       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
20078         if (active_insn_p (prev) || LABEL_P (prev))
20079           break;
20080       if (prev && LABEL_P (prev))
20081         {
20082           edge e;
20083           edge_iterator ei;
20084
20085           FOR_EACH_EDGE (e, ei, bb->preds)
20086             if (EDGE_FREQUENCY (e) && e->src->index >= 0
20087                 && !(e->flags & EDGE_FALLTHRU))
20088               replace = true;
20089         }
20090       if (!replace)
20091         {
20092           prev = prev_active_insn (ret);
20093           if (prev
20094               && ((JUMP_P (prev) && any_condjump_p (prev))
20095                   || CALL_P (prev)))
20096             replace = true;
20097           /* Empty functions get branch mispredict even when the jump destination
20098              is not visible to us.  */
20099           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
20100             replace = true;
20101         }
20102       if (replace)
20103         {
20104           emit_insn_before (gen_return_internal_long (), ret);
20105           delete_insn (ret);
20106         }
20107     }
20108 }
20109
20110 /* Implement machine specific optimizations.  We implement padding of returns
20111    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
20112 static void
20113 ix86_reorg (void)
20114 {
20115   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
20116     ix86_pad_returns ();
20117   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
20118     ix86_avoid_jump_misspredicts ();
20119 }
20120
20121 /* Return nonzero when QImode register that must be represented via REX prefix
20122    is used.  */
20123 bool
20124 x86_extended_QIreg_mentioned_p (rtx insn)
20125 {
20126   int i;
20127   extract_insn_cached (insn);
20128   for (i = 0; i < recog_data.n_operands; i++)
20129     if (REG_P (recog_data.operand[i])
20130         && REGNO (recog_data.operand[i]) >= 4)
20131        return true;
20132   return false;
20133 }
20134
20135 /* Return nonzero when P points to register encoded via REX prefix.
20136    Called via for_each_rtx.  */
20137 static int
20138 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
20139 {
20140    unsigned int regno;
20141    if (!REG_P (*p))
20142      return 0;
20143    regno = REGNO (*p);
20144    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
20145 }
20146
20147 /* Return true when INSN mentions register that must be encoded using REX
20148    prefix.  */
20149 bool
20150 x86_extended_reg_mentioned_p (rtx insn)
20151 {
20152   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
20153 }
20154
20155 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
20156    optabs would emit if we didn't have TFmode patterns.  */
20157
20158 void
20159 x86_emit_floatuns (rtx operands[2])
20160 {
20161   rtx neglab, donelab, i0, i1, f0, in, out;
20162   enum machine_mode mode, inmode;
20163
20164   inmode = GET_MODE (operands[1]);
20165   gcc_assert (inmode == SImode || inmode == DImode);
20166
20167   out = operands[0];
20168   in = force_reg (inmode, operands[1]);
20169   mode = GET_MODE (out);
20170   neglab = gen_label_rtx ();
20171   donelab = gen_label_rtx ();
20172   f0 = gen_reg_rtx (mode);
20173
20174   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, inmode, 0, neglab);
20175
20176   expand_float (out, in, 0);
20177
20178   emit_jump_insn (gen_jump (donelab));
20179   emit_barrier ();
20180
20181   emit_label (neglab);
20182
20183   i0 = expand_simple_binop (inmode, LSHIFTRT, in, const1_rtx, NULL,
20184                             1, OPTAB_DIRECT);
20185   i1 = expand_simple_binop (inmode, AND, in, const1_rtx, NULL,
20186                             1, OPTAB_DIRECT);
20187   i0 = expand_simple_binop (inmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
20188
20189   expand_float (f0, i0, 0);
20190
20191   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
20192
20193   emit_label (donelab);
20194 }
20195 \f
20196 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20197    with all elements equal to VAR.  Return true if successful.  */
20198
20199 static bool
20200 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
20201                                    rtx target, rtx val)
20202 {
20203   enum machine_mode smode, wsmode, wvmode;
20204   rtx x;
20205
20206   switch (mode)
20207     {
20208     case V2SImode:
20209     case V2SFmode:
20210       if (!mmx_ok)
20211         return false;
20212       /* FALLTHRU */
20213
20214     case V2DFmode:
20215     case V2DImode:
20216     case V4SFmode:
20217     case V4SImode:
20218       val = force_reg (GET_MODE_INNER (mode), val);
20219       x = gen_rtx_VEC_DUPLICATE (mode, val);
20220       emit_insn (gen_rtx_SET (VOIDmode, target, x));
20221       return true;
20222
20223     case V4HImode:
20224       if (!mmx_ok)
20225         return false;
20226       if (TARGET_SSE || TARGET_3DNOW_A)
20227         {
20228           val = gen_lowpart (SImode, val);
20229           x = gen_rtx_TRUNCATE (HImode, val);
20230           x = gen_rtx_VEC_DUPLICATE (mode, x);
20231           emit_insn (gen_rtx_SET (VOIDmode, target, x));
20232           return true;
20233         }
20234       else
20235         {
20236           smode = HImode;
20237           wsmode = SImode;
20238           wvmode = V2SImode;
20239           goto widen;
20240         }
20241
20242     case V8QImode:
20243       if (!mmx_ok)
20244         return false;
20245       smode = QImode;
20246       wsmode = HImode;
20247       wvmode = V4HImode;
20248       goto widen;
20249     case V8HImode:
20250       if (TARGET_SSE2)
20251         {
20252           rtx tmp1, tmp2;
20253           /* Extend HImode to SImode using a paradoxical SUBREG.  */
20254           tmp1 = gen_reg_rtx (SImode);
20255           emit_move_insn (tmp1, gen_lowpart (SImode, val));
20256           /* Insert the SImode value as low element of V4SImode vector. */
20257           tmp2 = gen_reg_rtx (V4SImode);
20258           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
20259                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
20260                                     CONST0_RTX (V4SImode),
20261                                     const1_rtx);
20262           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
20263           /* Cast the V4SImode vector back to a V8HImode vector.  */
20264           tmp1 = gen_reg_rtx (V8HImode);
20265           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
20266           /* Duplicate the low short through the whole low SImode word.  */
20267           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
20268           /* Cast the V8HImode vector back to a V4SImode vector.  */
20269           tmp2 = gen_reg_rtx (V4SImode);
20270           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
20271           /* Replicate the low element of the V4SImode vector.  */
20272           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
20273           /* Cast the V2SImode back to V8HImode, and store in target.  */
20274           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
20275           return true;
20276         }
20277       smode = HImode;
20278       wsmode = SImode;
20279       wvmode = V4SImode;
20280       goto widen;
20281     case V16QImode:
20282       if (TARGET_SSE2)
20283         {
20284           rtx tmp1, tmp2;
20285           /* Extend QImode to SImode using a paradoxical SUBREG.  */
20286           tmp1 = gen_reg_rtx (SImode);
20287           emit_move_insn (tmp1, gen_lowpart (SImode, val));
20288           /* Insert the SImode value as low element of V4SImode vector. */
20289           tmp2 = gen_reg_rtx (V4SImode);
20290           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
20291                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
20292                                     CONST0_RTX (V4SImode),
20293                                     const1_rtx);
20294           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
20295           /* Cast the V4SImode vector back to a V16QImode vector.  */
20296           tmp1 = gen_reg_rtx (V16QImode);
20297           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
20298           /* Duplicate the low byte through the whole low SImode word.  */
20299           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20300           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
20301           /* Cast the V16QImode vector back to a V4SImode vector.  */
20302           tmp2 = gen_reg_rtx (V4SImode);
20303           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
20304           /* Replicate the low element of the V4SImode vector.  */
20305           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
20306           /* Cast the V2SImode back to V16QImode, and store in target.  */
20307           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
20308           return true;
20309         }
20310       smode = QImode;
20311       wsmode = HImode;
20312       wvmode = V8HImode;
20313       goto widen;
20314     widen:
20315       /* Replicate the value once into the next wider mode and recurse.  */
20316       val = convert_modes (wsmode, smode, val, true);
20317       x = expand_simple_binop (wsmode, ASHIFT, val,
20318                                GEN_INT (GET_MODE_BITSIZE (smode)),
20319                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
20320       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
20321
20322       x = gen_reg_rtx (wvmode);
20323       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
20324         gcc_unreachable ();
20325       emit_move_insn (target, gen_lowpart (mode, x));
20326       return true;
20327
20328     default:
20329       return false;
20330     }
20331 }
20332
20333 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20334    whose ONE_VAR element is VAR, and other elements are zero.  Return true
20335    if successful.  */
20336
20337 static bool
20338 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
20339                                      rtx target, rtx var, int one_var)
20340 {
20341   enum machine_mode vsimode;
20342   rtx new_target;
20343   rtx x, tmp;
20344
20345   switch (mode)
20346     {
20347     case V2SFmode:
20348     case V2SImode:
20349       if (!mmx_ok)
20350         return false;
20351       /* FALLTHRU */
20352
20353     case V2DFmode:
20354     case V2DImode:
20355       if (one_var != 0)
20356         return false;
20357       var = force_reg (GET_MODE_INNER (mode), var);
20358       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
20359       emit_insn (gen_rtx_SET (VOIDmode, target, x));
20360       return true;
20361
20362     case V4SFmode:
20363     case V4SImode:
20364       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
20365         new_target = gen_reg_rtx (mode);
20366       else
20367         new_target = target;
20368       var = force_reg (GET_MODE_INNER (mode), var);
20369       x = gen_rtx_VEC_DUPLICATE (mode, var);
20370       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
20371       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
20372       if (one_var != 0)
20373         {
20374           /* We need to shuffle the value to the correct position, so
20375              create a new pseudo to store the intermediate result.  */
20376
20377           /* With SSE2, we can use the integer shuffle insns.  */
20378           if (mode != V4SFmode && TARGET_SSE2)
20379             {
20380               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
20381                                             GEN_INT (1),
20382                                             GEN_INT (one_var == 1 ? 0 : 1),
20383                                             GEN_INT (one_var == 2 ? 0 : 1),
20384                                             GEN_INT (one_var == 3 ? 0 : 1)));
20385               if (target != new_target)
20386                 emit_move_insn (target, new_target);
20387               return true;
20388             }
20389
20390           /* Otherwise convert the intermediate result to V4SFmode and
20391              use the SSE1 shuffle instructions.  */
20392           if (mode != V4SFmode)
20393             {
20394               tmp = gen_reg_rtx (V4SFmode);
20395               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
20396             }
20397           else
20398             tmp = new_target;
20399
20400           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
20401                                        GEN_INT (1),
20402                                        GEN_INT (one_var == 1 ? 0 : 1),
20403                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
20404                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
20405
20406           if (mode != V4SFmode)
20407             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
20408           else if (tmp != target)
20409             emit_move_insn (target, tmp);
20410         }
20411       else if (target != new_target)
20412         emit_move_insn (target, new_target);
20413       return true;
20414
20415     case V8HImode:
20416     case V16QImode:
20417       vsimode = V4SImode;
20418       goto widen;
20419     case V4HImode:
20420     case V8QImode:
20421       if (!mmx_ok)
20422         return false;
20423       vsimode = V2SImode;
20424       goto widen;
20425     widen:
20426       if (one_var != 0)
20427         return false;
20428
20429       /* Zero extend the variable element to SImode and recurse.  */
20430       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
20431
20432       x = gen_reg_rtx (vsimode);
20433       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
20434                                                 var, one_var))
20435         gcc_unreachable ();
20436
20437       emit_move_insn (target, gen_lowpart (mode, x));
20438       return true;
20439
20440     default:
20441       return false;
20442     }
20443 }
20444
20445 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
20446    consisting of the values in VALS.  It is known that all elements
20447    except ONE_VAR are constants.  Return true if successful.  */
20448
20449 static bool
20450 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
20451                                  rtx target, rtx vals, int one_var)
20452 {
20453   rtx var = XVECEXP (vals, 0, one_var);
20454   enum machine_mode wmode;
20455   rtx const_vec, x;
20456
20457   const_vec = copy_rtx (vals);
20458   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
20459   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
20460
20461   switch (mode)
20462     {
20463     case V2DFmode:
20464     case V2DImode:
20465     case V2SFmode:
20466     case V2SImode:
20467       /* For the two element vectors, it's just as easy to use
20468          the general case.  */
20469       return false;
20470
20471     case V4SFmode:
20472     case V4SImode:
20473     case V8HImode:
20474     case V4HImode:
20475       break;
20476
20477     case V16QImode:
20478       wmode = V8HImode;
20479       goto widen;
20480     case V8QImode:
20481       wmode = V4HImode;
20482       goto widen;
20483     widen:
20484       /* There's no way to set one QImode entry easily.  Combine
20485          the variable value with its adjacent constant value, and
20486          promote to an HImode set.  */
20487       x = XVECEXP (vals, 0, one_var ^ 1);
20488       if (one_var & 1)
20489         {
20490           var = convert_modes (HImode, QImode, var, true);
20491           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
20492                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
20493           x = GEN_INT (INTVAL (x) & 0xff);
20494         }
20495       else
20496         {
20497           var = convert_modes (HImode, QImode, var, true);
20498           x = gen_int_mode (INTVAL (x) << 8, HImode);
20499         }
20500       if (x != const0_rtx)
20501         var = expand_simple_binop (HImode, IOR, var, x, var,
20502                                    1, OPTAB_LIB_WIDEN);
20503
20504       x = gen_reg_rtx (wmode);
20505       emit_move_insn (x, gen_lowpart (wmode, const_vec));
20506       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
20507
20508       emit_move_insn (target, gen_lowpart (mode, x));
20509       return true;
20510
20511     default:
20512       return false;
20513     }
20514
20515   emit_move_insn (target, const_vec);
20516   ix86_expand_vector_set (mmx_ok, target, var, one_var);
20517   return true;
20518 }
20519
20520 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
20521    all values variable, and none identical.  */
20522
20523 static void
20524 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
20525                                  rtx target, rtx vals)
20526 {
20527   enum machine_mode half_mode = GET_MODE_INNER (mode);
20528   rtx op0 = NULL, op1 = NULL;
20529   bool use_vec_concat = false;
20530
20531   switch (mode)
20532     {
20533     case V2SFmode:
20534     case V2SImode:
20535       if (!mmx_ok && !TARGET_SSE)
20536         break;
20537       /* FALLTHRU */
20538
20539     case V2DFmode:
20540     case V2DImode:
20541       /* For the two element vectors, we always implement VEC_CONCAT.  */
20542       op0 = XVECEXP (vals, 0, 0);
20543       op1 = XVECEXP (vals, 0, 1);
20544       use_vec_concat = true;
20545       break;
20546
20547     case V4SFmode:
20548       half_mode = V2SFmode;
20549       goto half;
20550     case V4SImode:
20551       half_mode = V2SImode;
20552       goto half;
20553     half:
20554       {
20555         rtvec v;
20556
20557         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
20558            Recurse to load the two halves.  */
20559
20560         op0 = gen_reg_rtx (half_mode);
20561         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
20562         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
20563
20564         op1 = gen_reg_rtx (half_mode);
20565         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
20566         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
20567
20568         use_vec_concat = true;
20569       }
20570       break;
20571
20572     case V8HImode:
20573     case V16QImode:
20574     case V4HImode:
20575     case V8QImode:
20576       break;
20577
20578     default:
20579       gcc_unreachable ();
20580     }
20581
20582   if (use_vec_concat)
20583     {
20584       if (!register_operand (op0, half_mode))
20585         op0 = force_reg (half_mode, op0);
20586       if (!register_operand (op1, half_mode))
20587         op1 = force_reg (half_mode, op1);
20588
20589       emit_insn (gen_rtx_SET (VOIDmode, target,
20590                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
20591     }
20592   else
20593     {
20594       int i, j, n_elts, n_words, n_elt_per_word;
20595       enum machine_mode inner_mode;
20596       rtx words[4], shift;
20597
20598       inner_mode = GET_MODE_INNER (mode);
20599       n_elts = GET_MODE_NUNITS (mode);
20600       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
20601       n_elt_per_word = n_elts / n_words;
20602       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
20603
20604       for (i = 0; i < n_words; ++i)
20605         {
20606           rtx word = NULL_RTX;
20607
20608           for (j = 0; j < n_elt_per_word; ++j)
20609             {
20610               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
20611               elt = convert_modes (word_mode, inner_mode, elt, true);
20612
20613               if (j == 0)
20614                 word = elt;
20615               else
20616                 {
20617                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
20618                                               word, 1, OPTAB_LIB_WIDEN);
20619                   word = expand_simple_binop (word_mode, IOR, word, elt,
20620                                               word, 1, OPTAB_LIB_WIDEN);
20621                 }
20622             }
20623
20624           words[i] = word;
20625         }
20626
20627       if (n_words == 1)
20628         emit_move_insn (target, gen_lowpart (mode, words[0]));
20629       else if (n_words == 2)
20630         {
20631           rtx tmp = gen_reg_rtx (mode);
20632           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
20633           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
20634           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
20635           emit_move_insn (target, tmp);
20636         }
20637       else if (n_words == 4)
20638         {
20639           rtx tmp = gen_reg_rtx (V4SImode);
20640           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
20641           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
20642           emit_move_insn (target, gen_lowpart (mode, tmp));
20643         }
20644       else
20645         gcc_unreachable ();
20646     }
20647 }
20648
20649 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
20650    instructions unless MMX_OK is true.  */
20651
20652 void
20653 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
20654 {
20655   enum machine_mode mode = GET_MODE (target);
20656   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20657   int n_elts = GET_MODE_NUNITS (mode);
20658   int n_var = 0, one_var = -1;
20659   bool all_same = true, all_const_zero = true;
20660   int i;
20661   rtx x;
20662
20663   for (i = 0; i < n_elts; ++i)
20664     {
20665       x = XVECEXP (vals, 0, i);
20666       if (!CONSTANT_P (x))
20667         n_var++, one_var = i;
20668       else if (x != CONST0_RTX (inner_mode))
20669         all_const_zero = false;
20670       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
20671         all_same = false;
20672     }
20673
20674   /* Constants are best loaded from the constant pool.  */
20675   if (n_var == 0)
20676     {
20677       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
20678       return;
20679     }
20680
20681   /* If all values are identical, broadcast the value.  */
20682   if (all_same
20683       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
20684                                             XVECEXP (vals, 0, 0)))
20685     return;
20686
20687   /* Values where only one field is non-constant are best loaded from
20688      the pool and overwritten via move later.  */
20689   if (n_var == 1)
20690     {
20691       if (all_const_zero
20692           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
20693                                                   XVECEXP (vals, 0, one_var),
20694                                                   one_var))
20695         return;
20696
20697       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
20698         return;
20699     }
20700
20701   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
20702 }
20703
20704 void
20705 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
20706 {
20707   enum machine_mode mode = GET_MODE (target);
20708   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20709   bool use_vec_merge = false;
20710   rtx tmp;
20711
20712   switch (mode)
20713     {
20714     case V2SFmode:
20715     case V2SImode:
20716       if (mmx_ok)
20717         {
20718           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
20719           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
20720           if (elt == 0)
20721             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
20722           else
20723             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
20724           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20725           return;
20726         }
20727       break;
20728
20729     case V2DFmode:
20730     case V2DImode:
20731       {
20732         rtx op0, op1;
20733
20734         /* For the two element vectors, we implement a VEC_CONCAT with
20735            the extraction of the other element.  */
20736
20737         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
20738         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
20739
20740         if (elt == 0)
20741           op0 = val, op1 = tmp;
20742         else
20743           op0 = tmp, op1 = val;
20744
20745         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
20746         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20747       }
20748       return;
20749
20750     case V4SFmode:
20751       switch (elt)
20752         {
20753         case 0:
20754           use_vec_merge = true;
20755           break;
20756
20757         case 1:
20758           /* tmp = target = A B C D */
20759           tmp = copy_to_reg (target);
20760           /* target = A A B B */
20761           emit_insn (gen_sse_unpcklps (target, target, target));
20762           /* target = X A B B */
20763           ix86_expand_vector_set (false, target, val, 0);
20764           /* target = A X C D  */
20765           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20766                                        GEN_INT (1), GEN_INT (0),
20767                                        GEN_INT (2+4), GEN_INT (3+4)));
20768           return;
20769
20770         case 2:
20771           /* tmp = target = A B C D */
20772           tmp = copy_to_reg (target);
20773           /* tmp = X B C D */
20774           ix86_expand_vector_set (false, tmp, val, 0);
20775           /* target = A B X D */
20776           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20777                                        GEN_INT (0), GEN_INT (1),
20778                                        GEN_INT (0+4), GEN_INT (3+4)));
20779           return;
20780
20781         case 3:
20782           /* tmp = target = A B C D */
20783           tmp = copy_to_reg (target);
20784           /* tmp = X B C D */
20785           ix86_expand_vector_set (false, tmp, val, 0);
20786           /* target = A B X D */
20787           emit_insn (gen_sse_shufps_1 (target, target, tmp,
20788                                        GEN_INT (0), GEN_INT (1),
20789                                        GEN_INT (2+4), GEN_INT (0+4)));
20790           return;
20791
20792         default:
20793           gcc_unreachable ();
20794         }
20795       break;
20796
20797     case V4SImode:
20798       /* Element 0 handled by vec_merge below.  */
20799       if (elt == 0)
20800         {
20801           use_vec_merge = true;
20802           break;
20803         }
20804
20805       if (TARGET_SSE2)
20806         {
20807           /* With SSE2, use integer shuffles to swap element 0 and ELT,
20808              store into element 0, then shuffle them back.  */
20809
20810           rtx order[4];
20811
20812           order[0] = GEN_INT (elt);
20813           order[1] = const1_rtx;
20814           order[2] = const2_rtx;
20815           order[3] = GEN_INT (3);
20816           order[elt] = const0_rtx;
20817
20818           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20819                                         order[1], order[2], order[3]));
20820
20821           ix86_expand_vector_set (false, target, val, 0);
20822
20823           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
20824                                         order[1], order[2], order[3]));
20825         }
20826       else
20827         {
20828           /* For SSE1, we have to reuse the V4SF code.  */
20829           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
20830                                   gen_lowpart (SFmode, val), elt);
20831         }
20832       return;
20833
20834     case V8HImode:
20835       use_vec_merge = TARGET_SSE2;
20836       break;
20837     case V4HImode:
20838       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
20839       break;
20840
20841     case V16QImode:
20842     case V8QImode:
20843     default:
20844       break;
20845     }
20846
20847   if (use_vec_merge)
20848     {
20849       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
20850       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
20851       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20852     }
20853   else
20854     {
20855       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
20856
20857       emit_move_insn (mem, target);
20858
20859       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
20860       emit_move_insn (tmp, val);
20861
20862       emit_move_insn (target, mem);
20863     }
20864 }
20865
20866 void
20867 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
20868 {
20869   enum machine_mode mode = GET_MODE (vec);
20870   enum machine_mode inner_mode = GET_MODE_INNER (mode);
20871   bool use_vec_extr = false;
20872   rtx tmp;
20873
20874   switch (mode)
20875     {
20876     case V2SImode:
20877     case V2SFmode:
20878       if (!mmx_ok)
20879         break;
20880       /* FALLTHRU */
20881
20882     case V2DFmode:
20883     case V2DImode:
20884       use_vec_extr = true;
20885       break;
20886
20887     case V4SFmode:
20888       switch (elt)
20889         {
20890         case 0:
20891           tmp = vec;
20892           break;
20893
20894         case 1:
20895         case 3:
20896           tmp = gen_reg_rtx (mode);
20897           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
20898                                        GEN_INT (elt), GEN_INT (elt),
20899                                        GEN_INT (elt+4), GEN_INT (elt+4)));
20900           break;
20901
20902         case 2:
20903           tmp = gen_reg_rtx (mode);
20904           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
20905           break;
20906
20907         default:
20908           gcc_unreachable ();
20909         }
20910       vec = tmp;
20911       use_vec_extr = true;
20912       elt = 0;
20913       break;
20914
20915     case V4SImode:
20916       if (TARGET_SSE2)
20917         {
20918           switch (elt)
20919             {
20920             case 0:
20921               tmp = vec;
20922               break;
20923
20924             case 1:
20925             case 3:
20926               tmp = gen_reg_rtx (mode);
20927               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
20928                                             GEN_INT (elt), GEN_INT (elt),
20929                                             GEN_INT (elt), GEN_INT (elt)));
20930               break;
20931
20932             case 2:
20933               tmp = gen_reg_rtx (mode);
20934               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
20935               break;
20936
20937             default:
20938               gcc_unreachable ();
20939             }
20940           vec = tmp;
20941           use_vec_extr = true;
20942           elt = 0;
20943         }
20944       else
20945         {
20946           /* For SSE1, we have to reuse the V4SF code.  */
20947           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
20948                                       gen_lowpart (V4SFmode, vec), elt);
20949           return;
20950         }
20951       break;
20952
20953     case V8HImode:
20954       use_vec_extr = TARGET_SSE2;
20955       break;
20956     case V4HImode:
20957       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
20958       break;
20959
20960     case V16QImode:
20961     case V8QImode:
20962       /* ??? Could extract the appropriate HImode element and shift.  */
20963     default:
20964       break;
20965     }
20966
20967   if (use_vec_extr)
20968     {
20969       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
20970       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
20971
20972       /* Let the rtl optimizers know about the zero extension performed.  */
20973       if (inner_mode == HImode)
20974         {
20975           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
20976           target = gen_lowpart (SImode, target);
20977         }
20978
20979       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
20980     }
20981   else
20982     {
20983       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
20984
20985       emit_move_insn (mem, vec);
20986
20987       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
20988       emit_move_insn (target, tmp);
20989     }
20990 }
20991
20992 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
20993    pattern to reduce; DEST is the destination; IN is the input vector.  */
20994
20995 void
20996 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
20997 {
20998   rtx tmp1, tmp2, tmp3;
20999
21000   tmp1 = gen_reg_rtx (V4SFmode);
21001   tmp2 = gen_reg_rtx (V4SFmode);
21002   tmp3 = gen_reg_rtx (V4SFmode);
21003
21004   emit_insn (gen_sse_movhlps (tmp1, in, in));
21005   emit_insn (fn (tmp2, tmp1, in));
21006
21007   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
21008                                GEN_INT (1), GEN_INT (1),
21009                                GEN_INT (1+4), GEN_INT (1+4)));
21010   emit_insn (fn (dest, tmp2, tmp3));
21011 }
21012 \f
21013 /* Target hook for scalar_mode_supported_p.  */
21014 static bool
21015 ix86_scalar_mode_supported_p (enum machine_mode mode)
21016 {
21017   if (DECIMAL_FLOAT_MODE_P (mode))
21018     return true;
21019   else
21020     return default_scalar_mode_supported_p (mode);
21021 }
21022
21023 /* Implements target hook vector_mode_supported_p.  */
21024 static bool
21025 ix86_vector_mode_supported_p (enum machine_mode mode)
21026 {
21027   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
21028     return true;
21029   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
21030     return true;
21031   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
21032     return true;
21033   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
21034     return true;
21035   return false;
21036 }
21037
21038 /* Worker function for TARGET_MD_ASM_CLOBBERS.
21039
21040    We do this in the new i386 backend to maintain source compatibility
21041    with the old cc0-based compiler.  */
21042
21043 static tree
21044 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
21045                       tree inputs ATTRIBUTE_UNUSED,
21046                       tree clobbers)
21047 {
21048   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
21049                         clobbers);
21050   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
21051                         clobbers);
21052   return clobbers;
21053 }
21054
21055 /* Implementes target vector targetm.asm.encode_section_info.  This
21056    is not used by netware.  */
21057
21058 static void ATTRIBUTE_UNUSED
21059 ix86_encode_section_info (tree decl, rtx rtl, int first)
21060 {
21061   default_encode_section_info (decl, rtl, first);
21062
21063   if (TREE_CODE (decl) == VAR_DECL
21064       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
21065       && ix86_in_large_data_p (decl))
21066     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
21067 }
21068
21069 /* Worker function for REVERSE_CONDITION.  */
21070
21071 enum rtx_code
21072 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
21073 {
21074   return (mode != CCFPmode && mode != CCFPUmode
21075           ? reverse_condition (code)
21076           : reverse_condition_maybe_unordered (code));
21077 }
21078
21079 /* Output code to perform an x87 FP register move, from OPERANDS[1]
21080    to OPERANDS[0].  */
21081
21082 const char *
21083 output_387_reg_move (rtx insn, rtx *operands)
21084 {
21085   if (REG_P (operands[1])
21086       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
21087     {
21088       if (REGNO (operands[0]) == FIRST_STACK_REG)
21089         return output_387_ffreep (operands, 0);
21090       return "fstp\t%y0";
21091     }
21092   if (STACK_TOP_P (operands[0]))
21093     return "fld%z1\t%y1";
21094   return "fst\t%y0";
21095 }
21096
21097 /* Output code to perform a conditional jump to LABEL, if C2 flag in
21098    FP status register is set.  */
21099
21100 void
21101 ix86_emit_fp_unordered_jump (rtx label)
21102 {
21103   rtx reg = gen_reg_rtx (HImode);
21104   rtx temp;
21105
21106   emit_insn (gen_x86_fnstsw_1 (reg));
21107
21108   if (TARGET_SAHF && (TARGET_USE_SAHF || optimize_size))
21109     {
21110       emit_insn (gen_x86_sahf_1 (reg));
21111
21112       temp = gen_rtx_REG (CCmode, FLAGS_REG);
21113       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
21114     }
21115   else
21116     {
21117       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
21118
21119       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
21120       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
21121     }
21122
21123   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
21124                               gen_rtx_LABEL_REF (VOIDmode, label),
21125                               pc_rtx);
21126   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
21127
21128   emit_jump_insn (temp);
21129   predict_jump (REG_BR_PROB_BASE * 10 / 100);
21130 }
21131
21132 /* Output code to perform a log1p XFmode calculation.  */
21133
21134 void ix86_emit_i387_log1p (rtx op0, rtx op1)
21135 {
21136   rtx label1 = gen_label_rtx ();
21137   rtx label2 = gen_label_rtx ();
21138
21139   rtx tmp = gen_reg_rtx (XFmode);
21140   rtx tmp2 = gen_reg_rtx (XFmode);
21141
21142   emit_insn (gen_absxf2 (tmp, op1));
21143   emit_insn (gen_cmpxf (tmp,
21144     CONST_DOUBLE_FROM_REAL_VALUE (
21145        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
21146        XFmode)));
21147   emit_jump_insn (gen_bge (label1));
21148
21149   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
21150   emit_insn (gen_fyl2xp1xf3_i387 (op0, op1, tmp2));
21151   emit_jump (label2);
21152
21153   emit_label (label1);
21154   emit_move_insn (tmp, CONST1_RTX (XFmode));
21155   emit_insn (gen_addxf3 (tmp, op1, tmp));
21156   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
21157   emit_insn (gen_fyl2xxf3_i387 (op0, tmp, tmp2));
21158
21159   emit_label (label2);
21160 }
21161
21162 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
21163
21164 static void ATTRIBUTE_UNUSED
21165 i386_solaris_elf_named_section (const char *name, unsigned int flags,
21166                                 tree decl)
21167 {
21168   /* With Binutils 2.15, the "@unwind" marker must be specified on
21169      every occurrence of the ".eh_frame" section, not just the first
21170      one.  */
21171   if (TARGET_64BIT
21172       && strcmp (name, ".eh_frame") == 0)
21173     {
21174       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
21175                flags & SECTION_WRITE ? "aw" : "a");
21176       return;
21177     }
21178   default_elf_asm_named_section (name, flags, decl);
21179 }
21180
21181 /* Return the mangling of TYPE if it is an extended fundamental type.  */
21182
21183 static const char *
21184 ix86_mangle_fundamental_type (tree type)
21185 {
21186   switch (TYPE_MODE (type))
21187     {
21188     case TFmode:
21189       /* __float128 is "g".  */
21190       return "g";
21191     case XFmode:
21192       /* "long double" or __float80 is "e".  */
21193       return "e";
21194     default:
21195       return NULL;
21196     }
21197 }
21198
21199 /* For 32-bit code we can save PIC register setup by using
21200    __stack_chk_fail_local hidden function instead of calling
21201    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
21202    register, so it is better to call __stack_chk_fail directly.  */
21203
21204 static tree
21205 ix86_stack_protect_fail (void)
21206 {
21207   return TARGET_64BIT
21208          ? default_external_stack_protect_fail ()
21209          : default_hidden_stack_protect_fail ();
21210 }
21211
21212 /* Select a format to encode pointers in exception handling data.  CODE
21213    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
21214    true if the symbol may be affected by dynamic relocations.
21215
21216    ??? All x86 object file formats are capable of representing this.
21217    After all, the relocation needed is the same as for the call insn.
21218    Whether or not a particular assembler allows us to enter such, I
21219    guess we'll have to see.  */
21220 int
21221 asm_preferred_eh_data_format (int code, int global)
21222 {
21223   if (flag_pic)
21224     {
21225       int type = DW_EH_PE_sdata8;
21226       if (!TARGET_64BIT
21227           || ix86_cmodel == CM_SMALL_PIC
21228           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
21229         type = DW_EH_PE_sdata4;
21230       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
21231     }
21232   if (ix86_cmodel == CM_SMALL
21233       || (ix86_cmodel == CM_MEDIUM && code))
21234     return DW_EH_PE_udata4;
21235   return DW_EH_PE_absptr;
21236 }
21237 \f
21238 /* Expand copysign from SIGN to the positive value ABS_VALUE
21239    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
21240    the sign-bit.  */
21241 static void
21242 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
21243 {
21244   enum machine_mode mode = GET_MODE (sign);
21245   rtx sgn = gen_reg_rtx (mode);
21246   if (mask == NULL_RTX)
21247     {
21248       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
21249       if (!VECTOR_MODE_P (mode))
21250         {
21251           /* We need to generate a scalar mode mask in this case.  */
21252           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
21253           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
21254           mask = gen_reg_rtx (mode);
21255           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
21256         }
21257     }
21258   else
21259     mask = gen_rtx_NOT (mode, mask);
21260   emit_insn (gen_rtx_SET (VOIDmode, sgn,
21261                           gen_rtx_AND (mode, mask, sign)));
21262   emit_insn (gen_rtx_SET (VOIDmode, result,
21263                           gen_rtx_IOR (mode, abs_value, sgn)));
21264 }
21265
21266 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
21267    mask for masking out the sign-bit is stored in *SMASK, if that is
21268    non-null.  */
21269 static rtx
21270 ix86_expand_sse_fabs (rtx op0, rtx *smask)
21271 {
21272   enum machine_mode mode = GET_MODE (op0);
21273   rtx xa, mask;
21274
21275   xa = gen_reg_rtx (mode);
21276   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
21277   if (!VECTOR_MODE_P (mode))
21278     {
21279       /* We need to generate a scalar mode mask in this case.  */
21280       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
21281       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
21282       mask = gen_reg_rtx (mode);
21283       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
21284     }
21285   emit_insn (gen_rtx_SET (VOIDmode, xa,
21286                           gen_rtx_AND (mode, op0, mask)));
21287
21288   if (smask)
21289     *smask = mask;
21290
21291   return xa;
21292 }
21293
21294 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
21295    swapping the operands if SWAP_OPERANDS is true.  The expanded
21296    code is a forward jump to a newly created label in case the
21297    comparison is true.  The generated label rtx is returned.  */
21298 static rtx
21299 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
21300                                   bool swap_operands)
21301 {
21302   rtx label, tmp;
21303
21304   if (swap_operands)
21305     {
21306       tmp = op0;
21307       op0 = op1;
21308       op1 = tmp;
21309     }
21310
21311   label = gen_label_rtx ();
21312   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
21313   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21314                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
21315   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
21316   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
21317                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
21318   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
21319   JUMP_LABEL (tmp) = label;
21320
21321   return label;
21322 }
21323
21324 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
21325    using comparison code CODE.  Operands are swapped for the comparison if
21326    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
21327 static rtx
21328 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
21329                               bool swap_operands)
21330 {
21331   enum machine_mode mode = GET_MODE (op0);
21332   rtx mask = gen_reg_rtx (mode);
21333
21334   if (swap_operands)
21335     {
21336       rtx tmp = op0;
21337       op0 = op1;
21338       op1 = tmp;
21339     }
21340
21341   if (mode == DFmode)
21342     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
21343                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
21344   else
21345     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
21346                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
21347
21348   return mask;
21349 }
21350
21351 /* Generate and return a rtx of mode MODE for 2**n where n is the number
21352    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
21353 static rtx
21354 ix86_gen_TWO52 (enum machine_mode mode)
21355 {
21356   REAL_VALUE_TYPE TWO52r;
21357   rtx TWO52;
21358
21359   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
21360   TWO52 = const_double_from_real_value (TWO52r, mode);
21361   TWO52 = force_reg (mode, TWO52);
21362
21363   return TWO52;
21364 }
21365
21366 /* Expand SSE sequence for computing lround from OP1 storing
21367    into OP0.  */
21368 void
21369 ix86_expand_lround (rtx op0, rtx op1)
21370 {
21371   /* C code for the stuff we're doing below:
21372        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
21373        return (long)tmp;
21374    */
21375   enum machine_mode mode = GET_MODE (op1);
21376   const struct real_format *fmt;
21377   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21378   rtx adj;
21379
21380   /* load nextafter (0.5, 0.0) */
21381   fmt = REAL_MODE_FORMAT (mode);
21382   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21383   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21384
21385   /* adj = copysign (0.5, op1) */
21386   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
21387   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
21388
21389   /* adj = op1 + adj */
21390   adj = expand_simple_binop (mode, PLUS, adj, op1, NULL_RTX, 0, OPTAB_DIRECT);
21391
21392   /* op0 = (imode)adj */
21393   expand_fix (op0, adj, 0);
21394 }
21395
21396 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
21397    into OPERAND0.  */
21398 void
21399 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
21400 {
21401   /* C code for the stuff we're doing below (for do_floor):
21402         xi = (long)op1;
21403         xi -= (double)xi > op1 ? 1 : 0;
21404         return xi;
21405    */
21406   enum machine_mode fmode = GET_MODE (op1);
21407   enum machine_mode imode = GET_MODE (op0);
21408   rtx ireg, freg, label, tmp;
21409
21410   /* reg = (long)op1 */
21411   ireg = gen_reg_rtx (imode);
21412   expand_fix (ireg, op1, 0);
21413
21414   /* freg = (double)reg */
21415   freg = gen_reg_rtx (fmode);
21416   expand_float (freg, ireg, 0);
21417
21418   /* ireg = (freg > op1) ? ireg - 1 : ireg */
21419   label = ix86_expand_sse_compare_and_jump (UNLE,
21420                                             freg, op1, !do_floor);
21421   tmp = expand_simple_binop (imode, do_floor ? MINUS : PLUS,
21422                              ireg, const1_rtx, NULL_RTX, 0, OPTAB_DIRECT);
21423   emit_move_insn (ireg, tmp);
21424
21425   emit_label (label);
21426   LABEL_NUSES (label) = 1;
21427
21428   emit_move_insn (op0, ireg);
21429 }
21430
21431 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
21432    result in OPERAND0.  */
21433 void
21434 ix86_expand_rint (rtx operand0, rtx operand1)
21435 {
21436   /* C code for the stuff we're doing below:
21437         xa = fabs (operand1);
21438         if (!isless (xa, 2**52))
21439           return operand1;
21440         xa = xa + 2**52 - 2**52;
21441         return copysign (xa, operand1);
21442    */
21443   enum machine_mode mode = GET_MODE (operand0);
21444   rtx res, xa, label, TWO52, mask;
21445
21446   res = gen_reg_rtx (mode);
21447   emit_move_insn (res, operand1);
21448
21449   /* xa = abs (operand1) */
21450   xa = ix86_expand_sse_fabs (res, &mask);
21451
21452   /* if (!isless (xa, TWO52)) goto label; */
21453   TWO52 = ix86_gen_TWO52 (mode);
21454   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21455
21456   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21457   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21458
21459   ix86_sse_copysign_to_positive (res, xa, res, mask);
21460
21461   emit_label (label);
21462   LABEL_NUSES (label) = 1;
21463
21464   emit_move_insn (operand0, res);
21465 }
21466
21467 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21468    into OPERAND0.  */
21469 void
21470 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
21471 {
21472   /* C code for the stuff we expand below.
21473         double xa = fabs (x), x2;
21474         if (!isless (xa, TWO52))
21475           return x;
21476         xa = xa + TWO52 - TWO52;
21477         x2 = copysign (xa, x);
21478      Compensate.  Floor:
21479         if (x2 > x)
21480           x2 -= 1;
21481      Compensate.  Ceil:
21482         if (x2 < x)
21483           x2 -= -1;
21484         return x2;
21485    */
21486   enum machine_mode mode = GET_MODE (operand0);
21487   rtx xa, TWO52, tmp, label, one, res, mask;
21488
21489   TWO52 = ix86_gen_TWO52 (mode);
21490
21491   /* Temporary for holding the result, initialized to the input
21492      operand to ease control flow.  */
21493   res = gen_reg_rtx (mode);
21494   emit_move_insn (res, operand1);
21495
21496   /* xa = abs (operand1) */
21497   xa = ix86_expand_sse_fabs (res, &mask);
21498
21499   /* if (!isless (xa, TWO52)) goto label; */
21500   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21501
21502   /* xa = xa + TWO52 - TWO52; */
21503   xa = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21504   xa = expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
21505
21506   /* xa = copysign (xa, operand1) */
21507   ix86_sse_copysign_to_positive (xa, xa, res, mask);
21508
21509   /* generate 1.0 or -1.0 */
21510   one = force_reg (mode,
21511                    const_double_from_real_value (do_floor
21512                                                  ? dconst1 : dconstm1, mode));
21513
21514   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21515   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21516   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21517                           gen_rtx_AND (mode, one, tmp)));
21518   /* We always need to subtract here to preserve signed zero.  */
21519   tmp = expand_simple_binop (mode, MINUS,
21520                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21521   emit_move_insn (res, tmp);
21522
21523   emit_label (label);
21524   LABEL_NUSES (label) = 1;
21525
21526   emit_move_insn (operand0, res);
21527 }
21528
21529 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
21530    into OPERAND0.  */
21531 void
21532 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
21533 {
21534   /* C code for the stuff we expand below.
21535         double xa = fabs (x), x2;
21536         if (!isless (xa, TWO52))
21537           return x;
21538         x2 = (double)(long)x;
21539      Compensate.  Floor:
21540         if (x2 > x)
21541           x2 -= 1;
21542      Compensate.  Ceil:
21543         if (x2 < x)
21544           x2 += 1;
21545         if (HONOR_SIGNED_ZEROS (mode))
21546           return copysign (x2, x);
21547         return x2;
21548    */
21549   enum machine_mode mode = GET_MODE (operand0);
21550   rtx xa, xi, TWO52, tmp, label, one, res, mask;
21551
21552   TWO52 = ix86_gen_TWO52 (mode);
21553
21554   /* Temporary for holding the result, initialized to the input
21555      operand to ease control flow.  */
21556   res = gen_reg_rtx (mode);
21557   emit_move_insn (res, operand1);
21558
21559   /* xa = abs (operand1) */
21560   xa = ix86_expand_sse_fabs (res, &mask);
21561
21562   /* if (!isless (xa, TWO52)) goto label; */
21563   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21564
21565   /* xa = (double)(long)x */
21566   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21567   expand_fix (xi, res, 0);
21568   expand_float (xa, xi, 0);
21569
21570   /* generate 1.0 */
21571   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21572
21573   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
21574   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
21575   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21576                           gen_rtx_AND (mode, one, tmp)));
21577   tmp = expand_simple_binop (mode, do_floor ? MINUS : PLUS,
21578                              xa, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21579   emit_move_insn (res, tmp);
21580
21581   if (HONOR_SIGNED_ZEROS (mode))
21582     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21583
21584   emit_label (label);
21585   LABEL_NUSES (label) = 1;
21586
21587   emit_move_insn (operand0, res);
21588 }
21589
21590 /* Expand SSE sequence for computing round from OPERAND1 storing
21591    into OPERAND0.  Sequence that works without relying on DImode truncation
21592    via cvttsd2siq that is only available on 64bit targets.  */
21593 void
21594 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
21595 {
21596   /* C code for the stuff we expand below.
21597         double xa = fabs (x), xa2, x2;
21598         if (!isless (xa, TWO52))
21599           return x;
21600      Using the absolute value and copying back sign makes
21601      -0.0 -> -0.0 correct.
21602         xa2 = xa + TWO52 - TWO52;
21603      Compensate.
21604         dxa = xa2 - xa;
21605         if (dxa <= -0.5)
21606           xa2 += 1;
21607         else if (dxa > 0.5)
21608           xa2 -= 1;
21609         x2 = copysign (xa2, x);
21610         return x2;
21611    */
21612   enum machine_mode mode = GET_MODE (operand0);
21613   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
21614
21615   TWO52 = ix86_gen_TWO52 (mode);
21616
21617   /* Temporary for holding the result, initialized to the input
21618      operand to ease control flow.  */
21619   res = gen_reg_rtx (mode);
21620   emit_move_insn (res, operand1);
21621
21622   /* xa = abs (operand1) */
21623   xa = ix86_expand_sse_fabs (res, &mask);
21624
21625   /* if (!isless (xa, TWO52)) goto label; */
21626   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21627
21628   /* xa2 = xa + TWO52 - TWO52; */
21629   xa2 = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21630   xa2 = expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
21631
21632   /* dxa = xa2 - xa; */
21633   dxa = expand_simple_binop (mode, MINUS, xa2, xa, NULL_RTX, 0, OPTAB_DIRECT);
21634
21635   /* generate 0.5, 1.0 and -0.5 */
21636   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
21637   one = expand_simple_binop (mode, PLUS, half, half, NULL_RTX, 0, OPTAB_DIRECT);
21638   mhalf = expand_simple_binop (mode, MINUS, half, one, NULL_RTX,
21639                                0, OPTAB_DIRECT);
21640
21641   /* Compensate.  */
21642   tmp = gen_reg_rtx (mode);
21643   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
21644   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
21645   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21646                           gen_rtx_AND (mode, one, tmp)));
21647   xa2 = expand_simple_binop (mode, MINUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21648   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
21649   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
21650   emit_insn (gen_rtx_SET (VOIDmode, tmp,
21651                           gen_rtx_AND (mode, one, tmp)));
21652   xa2 = expand_simple_binop (mode, PLUS, xa2, tmp, NULL_RTX, 0, OPTAB_DIRECT);
21653
21654   /* res = copysign (xa2, operand1) */
21655   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
21656
21657   emit_label (label);
21658   LABEL_NUSES (label) = 1;
21659
21660   emit_move_insn (operand0, res);
21661 }
21662
21663 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21664    into OPERAND0.  */
21665 void
21666 ix86_expand_trunc (rtx operand0, rtx operand1)
21667 {
21668   /* C code for SSE variant we expand below.
21669         double xa = fabs (x), x2;
21670         if (!isless (xa, TWO52))
21671           return x;
21672         x2 = (double)(long)x;
21673         if (HONOR_SIGNED_ZEROS (mode))
21674           return copysign (x2, x);
21675         return x2;
21676    */
21677   enum machine_mode mode = GET_MODE (operand0);
21678   rtx xa, xi, TWO52, label, res, mask;
21679
21680   TWO52 = ix86_gen_TWO52 (mode);
21681
21682   /* Temporary for holding the result, initialized to the input
21683      operand to ease control flow.  */
21684   res = gen_reg_rtx (mode);
21685   emit_move_insn (res, operand1);
21686
21687   /* xa = abs (operand1) */
21688   xa = ix86_expand_sse_fabs (res, &mask);
21689
21690   /* if (!isless (xa, TWO52)) goto label; */
21691   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21692
21693   /* x = (double)(long)x */
21694   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21695   expand_fix (xi, res, 0);
21696   expand_float (res, xi, 0);
21697
21698   if (HONOR_SIGNED_ZEROS (mode))
21699     ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), mask);
21700
21701   emit_label (label);
21702   LABEL_NUSES (label) = 1;
21703
21704   emit_move_insn (operand0, res);
21705 }
21706
21707 /* Expand SSE sequence for computing trunc from OPERAND1 storing
21708    into OPERAND0.  */
21709 void
21710 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
21711 {
21712   enum machine_mode mode = GET_MODE (operand0);
21713   rtx xa, mask, TWO52, label, one, res, smask, tmp;
21714
21715   /* C code for SSE variant we expand below.
21716         double xa = fabs (x), x2;
21717         if (!isless (xa, TWO52))
21718           return x;
21719         xa2 = xa + TWO52 - TWO52;
21720      Compensate:
21721         if (xa2 > xa)
21722           xa2 -= 1.0;
21723         x2 = copysign (xa2, x);
21724         return x2;
21725    */
21726
21727   TWO52 = ix86_gen_TWO52 (mode);
21728
21729   /* Temporary for holding the result, initialized to the input
21730      operand to ease control flow.  */
21731   res = gen_reg_rtx (mode);
21732   emit_move_insn (res, operand1);
21733
21734   /* xa = abs (operand1) */
21735   xa = ix86_expand_sse_fabs (res, &smask);
21736
21737   /* if (!isless (xa, TWO52)) goto label; */
21738   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21739
21740   /* res = xa + TWO52 - TWO52; */
21741   tmp = expand_simple_binop (mode, PLUS, xa, TWO52, NULL_RTX, 0, OPTAB_DIRECT);
21742   tmp = expand_simple_binop (mode, MINUS, tmp, TWO52, tmp, 0, OPTAB_DIRECT);
21743   emit_move_insn (res, tmp);
21744
21745   /* generate 1.0 */
21746   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
21747
21748   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
21749   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
21750   emit_insn (gen_rtx_SET (VOIDmode, mask,
21751                           gen_rtx_AND (mode, mask, one)));
21752   tmp = expand_simple_binop (mode, MINUS,
21753                              res, mask, NULL_RTX, 0, OPTAB_DIRECT);
21754   emit_move_insn (res, tmp);
21755
21756   /* res = copysign (res, operand1) */
21757   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
21758
21759   emit_label (label);
21760   LABEL_NUSES (label) = 1;
21761
21762   emit_move_insn (operand0, res);
21763 }
21764
21765 /* Expand SSE sequence for computing round from OPERAND1 storing
21766    into OPERAND0.  */
21767 void
21768 ix86_expand_round (rtx operand0, rtx operand1)
21769 {
21770   /* C code for the stuff we're doing below:
21771         double xa = fabs (x);
21772         if (!isless (xa, TWO52))
21773           return x;
21774         xa = (double)(long)(xa + nextafter (0.5, 0.0));
21775         return copysign (xa, x);
21776    */
21777   enum machine_mode mode = GET_MODE (operand0);
21778   rtx res, TWO52, xa, label, xi, half, mask;
21779   const struct real_format *fmt;
21780   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
21781
21782   /* Temporary for holding the result, initialized to the input
21783      operand to ease control flow.  */
21784   res = gen_reg_rtx (mode);
21785   emit_move_insn (res, operand1);
21786
21787   TWO52 = ix86_gen_TWO52 (mode);
21788   xa = ix86_expand_sse_fabs (res, &mask);
21789   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
21790
21791   /* load nextafter (0.5, 0.0) */
21792   fmt = REAL_MODE_FORMAT (mode);
21793   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
21794   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
21795
21796   /* xa = xa + 0.5 */
21797   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
21798   xa = expand_simple_binop (mode, PLUS, xa, half, NULL_RTX, 0, OPTAB_DIRECT);
21799
21800   /* xa = (double)(int64_t)xa */
21801   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
21802   expand_fix (xi, xa, 0);
21803   expand_float (xa, xi, 0);
21804
21805   /* res = copysign (xa, operand1) */
21806   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
21807
21808   emit_label (label);
21809   LABEL_NUSES (label) = 1;
21810
21811   emit_move_insn (operand0, res);
21812 }
21813
21814 \f
21815 /* Table of valid machine attributes.  */
21816 static const struct attribute_spec ix86_attribute_table[] =
21817 {
21818   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
21819   /* Stdcall attribute says callee is responsible for popping arguments
21820      if they are not variable.  */
21821   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21822   /* Fastcall attribute says callee is responsible for popping arguments
21823      if they are not variable.  */
21824   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21825   /* Cdecl attribute says the callee is a normal C declaration */
21826   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
21827   /* Regparm attribute specifies how many integer arguments are to be
21828      passed in registers.  */
21829   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
21830   /* Sseregparm attribute says we are using x86_64 calling conventions
21831      for FP arguments.  */
21832   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
21833   /* force_align_arg_pointer says this function realigns the stack at entry.  */
21834   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
21835     false, true,  true, ix86_handle_cconv_attribute },
21836 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21837   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
21838   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
21839   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
21840 #endif
21841   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21842   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
21843 #ifdef SUBTARGET_ATTRIBUTE_TABLE
21844   SUBTARGET_ATTRIBUTE_TABLE,
21845 #endif
21846   { NULL,        0, 0, false, false, false, NULL }
21847 };
21848
21849 /* Initialize the GCC target structure.  */
21850 #undef TARGET_ATTRIBUTE_TABLE
21851 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
21852 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21853 #  undef TARGET_MERGE_DECL_ATTRIBUTES
21854 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
21855 #endif
21856
21857 #undef TARGET_COMP_TYPE_ATTRIBUTES
21858 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
21859
21860 #undef TARGET_INIT_BUILTINS
21861 #define TARGET_INIT_BUILTINS ix86_init_builtins
21862 #undef TARGET_EXPAND_BUILTIN
21863 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
21864
21865 #undef TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION
21866 #define TARGET_VECTORIZE_BUILTIN_VECTORIZED_FUNCTION ix86_builtin_vectorized_function
21867 #undef TARGET_VECTORIZE_BUILTIN_CONVERSION
21868 #define TARGET_VECTORIZE_BUILTIN_CONVERSION ix86_builtin_conversion
21869
21870 #undef TARGET_ASM_FUNCTION_EPILOGUE
21871 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
21872
21873 #undef TARGET_ENCODE_SECTION_INFO
21874 #ifndef SUBTARGET_ENCODE_SECTION_INFO
21875 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
21876 #else
21877 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
21878 #endif
21879
21880 #undef TARGET_ASM_OPEN_PAREN
21881 #define TARGET_ASM_OPEN_PAREN ""
21882 #undef TARGET_ASM_CLOSE_PAREN
21883 #define TARGET_ASM_CLOSE_PAREN ""
21884
21885 #undef TARGET_ASM_ALIGNED_HI_OP
21886 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
21887 #undef TARGET_ASM_ALIGNED_SI_OP
21888 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
21889 #ifdef ASM_QUAD
21890 #undef TARGET_ASM_ALIGNED_DI_OP
21891 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
21892 #endif
21893
21894 #undef TARGET_ASM_UNALIGNED_HI_OP
21895 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
21896 #undef TARGET_ASM_UNALIGNED_SI_OP
21897 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
21898 #undef TARGET_ASM_UNALIGNED_DI_OP
21899 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
21900
21901 #undef TARGET_SCHED_ADJUST_COST
21902 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
21903 #undef TARGET_SCHED_ISSUE_RATE
21904 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
21905 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
21906 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
21907   ia32_multipass_dfa_lookahead
21908
21909 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
21910 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
21911
21912 #ifdef HAVE_AS_TLS
21913 #undef TARGET_HAVE_TLS
21914 #define TARGET_HAVE_TLS true
21915 #endif
21916 #undef TARGET_CANNOT_FORCE_CONST_MEM
21917 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
21918 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
21919 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
21920
21921 #undef TARGET_DELEGITIMIZE_ADDRESS
21922 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
21923
21924 #undef TARGET_MS_BITFIELD_LAYOUT_P
21925 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
21926
21927 #if TARGET_MACHO
21928 #undef TARGET_BINDS_LOCAL_P
21929 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
21930 #endif
21931 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
21932 #undef TARGET_BINDS_LOCAL_P
21933 #define TARGET_BINDS_LOCAL_P i386_pe_binds_local_p
21934 #endif
21935
21936 #undef TARGET_ASM_OUTPUT_MI_THUNK
21937 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
21938 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
21939 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
21940
21941 #undef TARGET_ASM_FILE_START
21942 #define TARGET_ASM_FILE_START x86_file_start
21943
21944 #undef TARGET_DEFAULT_TARGET_FLAGS
21945 #define TARGET_DEFAULT_TARGET_FLAGS     \
21946   (TARGET_DEFAULT                       \
21947    | TARGET_64BIT_DEFAULT               \
21948    | TARGET_SUBTARGET_DEFAULT           \
21949    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
21950
21951 #undef TARGET_HANDLE_OPTION
21952 #define TARGET_HANDLE_OPTION ix86_handle_option
21953
21954 #undef TARGET_RTX_COSTS
21955 #define TARGET_RTX_COSTS ix86_rtx_costs
21956 #undef TARGET_ADDRESS_COST
21957 #define TARGET_ADDRESS_COST ix86_address_cost
21958
21959 #undef TARGET_FIXED_CONDITION_CODE_REGS
21960 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
21961 #undef TARGET_CC_MODES_COMPATIBLE
21962 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
21963
21964 #undef TARGET_MACHINE_DEPENDENT_REORG
21965 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
21966
21967 #undef TARGET_BUILD_BUILTIN_VA_LIST
21968 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
21969
21970 #undef TARGET_MD_ASM_CLOBBERS
21971 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
21972
21973 #undef TARGET_PROMOTE_PROTOTYPES
21974 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
21975 #undef TARGET_STRUCT_VALUE_RTX
21976 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
21977 #undef TARGET_SETUP_INCOMING_VARARGS
21978 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
21979 #undef TARGET_MUST_PASS_IN_STACK
21980 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
21981 #undef TARGET_PASS_BY_REFERENCE
21982 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
21983 #undef TARGET_INTERNAL_ARG_POINTER
21984 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
21985 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
21986 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
21987 #undef TARGET_STRICT_ARGUMENT_NAMING
21988 #define TARGET_STRICT_ARGUMENT_NAMING hook_bool_CUMULATIVE_ARGS_true
21989
21990 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
21991 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
21992
21993 #undef TARGET_SCALAR_MODE_SUPPORTED_P
21994 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
21995
21996 #undef TARGET_VECTOR_MODE_SUPPORTED_P
21997 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
21998
21999 #ifdef HAVE_AS_TLS
22000 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
22001 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
22002 #endif
22003
22004 #ifdef SUBTARGET_INSERT_ATTRIBUTES
22005 #undef TARGET_INSERT_ATTRIBUTES
22006 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
22007 #endif
22008
22009 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
22010 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
22011
22012 #undef TARGET_STACK_PROTECT_FAIL
22013 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
22014
22015 #undef TARGET_FUNCTION_VALUE
22016 #define TARGET_FUNCTION_VALUE ix86_function_value
22017
22018 struct gcc_target targetm = TARGET_INITIALIZER;
22019 \f
22020 #include "gt-i386.h"