OSDN Git Service

2006-09-07 Eric Christopher <echristo@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / config / i386 / i386.c
1 /* Subroutines used for code generation on IA-32.
2    Copyright (C) 1988, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006 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
54 #ifndef CHECK_STACK_LIMIT
55 #define CHECK_STACK_LIMIT (-1)
56 #endif
57
58 /* Return index of given mode in mult and division cost tables.  */
59 #define MODE_INDEX(mode)                                        \
60   ((mode) == QImode ? 0                                         \
61    : (mode) == HImode ? 1                                       \
62    : (mode) == SImode ? 2                                       \
63    : (mode) == DImode ? 3                                       \
64    : 4)
65
66 /* Processor costs (relative to an add) */
67 /* We assume COSTS_N_INSNS is defined as (N)*4 and an addition is 2 bytes.  */
68 #define COSTS_N_BYTES(N) ((N) * 2)
69
70 static const
71 struct processor_costs size_cost = {    /* costs for tuning for size */
72   COSTS_N_BYTES (2),                    /* cost of an add instruction */
73   COSTS_N_BYTES (3),                    /* cost of a lea instruction */
74   COSTS_N_BYTES (2),                    /* variable shift costs */
75   COSTS_N_BYTES (3),                    /* constant shift costs */
76   {COSTS_N_BYTES (3),                   /* cost of starting multiply for QI */
77    COSTS_N_BYTES (3),                   /*                               HI */
78    COSTS_N_BYTES (3),                   /*                               SI */
79    COSTS_N_BYTES (3),                   /*                               DI */
80    COSTS_N_BYTES (5)},                  /*                            other */
81   0,                                    /* cost of multiply per each bit set */
82   {COSTS_N_BYTES (3),                   /* cost of a divide/mod for QI */
83    COSTS_N_BYTES (3),                   /*                          HI */
84    COSTS_N_BYTES (3),                   /*                          SI */
85    COSTS_N_BYTES (3),                   /*                          DI */
86    COSTS_N_BYTES (5)},                  /*                       other */
87   COSTS_N_BYTES (3),                    /* cost of movsx */
88   COSTS_N_BYTES (3),                    /* cost of movzx */
89   0,                                    /* "large" insn */
90   2,                                    /* MOVE_RATIO */
91   2,                                    /* cost for loading QImode using movzbl */
92   {2, 2, 2},                            /* cost of loading integer registers
93                                            in QImode, HImode and SImode.
94                                            Relative to reg-reg move (2).  */
95   {2, 2, 2},                            /* cost of storing integer registers */
96   2,                                    /* cost of reg,reg fld/fst */
97   {2, 2, 2},                            /* cost of loading fp registers
98                                            in SFmode, DFmode and XFmode */
99   {2, 2, 2},                            /* cost of storing fp registers
100                                            in SFmode, DFmode and XFmode */
101   3,                                    /* cost of moving MMX register */
102   {3, 3},                               /* cost of loading MMX registers
103                                            in SImode and DImode */
104   {3, 3},                               /* cost of storing MMX registers
105                                            in SImode and DImode */
106   3,                                    /* cost of moving SSE register */
107   {3, 3, 3},                            /* cost of loading SSE registers
108                                            in SImode, DImode and TImode */
109   {3, 3, 3},                            /* cost of storing SSE registers
110                                            in SImode, DImode and TImode */
111   3,                                    /* MMX or SSE register to integer */
112   0,                                    /* size of prefetch block */
113   0,                                    /* number of parallel prefetches */
114   2,                                    /* Branch cost */
115   COSTS_N_BYTES (2),                    /* cost of FADD and FSUB insns.  */
116   COSTS_N_BYTES (2),                    /* cost of FMUL instruction.  */
117   COSTS_N_BYTES (2),                    /* cost of FDIV instruction.  */
118   COSTS_N_BYTES (2),                    /* cost of FABS instruction.  */
119   COSTS_N_BYTES (2),                    /* cost of FCHS instruction.  */
120   COSTS_N_BYTES (2),                    /* cost of FSQRT instruction.  */
121 };
122
123 /* Processor costs (relative to an add) */
124 static const
125 struct processor_costs i386_cost = {    /* 386 specific costs */
126   COSTS_N_INSNS (1),                    /* cost of an add instruction */
127   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
128   COSTS_N_INSNS (3),                    /* variable shift costs */
129   COSTS_N_INSNS (2),                    /* constant shift costs */
130   {COSTS_N_INSNS (6),                   /* cost of starting multiply for QI */
131    COSTS_N_INSNS (6),                   /*                               HI */
132    COSTS_N_INSNS (6),                   /*                               SI */
133    COSTS_N_INSNS (6),                   /*                               DI */
134    COSTS_N_INSNS (6)},                  /*                               other */
135   COSTS_N_INSNS (1),                    /* cost of multiply per each bit set */
136   {COSTS_N_INSNS (23),                  /* cost of a divide/mod for QI */
137    COSTS_N_INSNS (23),                  /*                          HI */
138    COSTS_N_INSNS (23),                  /*                          SI */
139    COSTS_N_INSNS (23),                  /*                          DI */
140    COSTS_N_INSNS (23)},                 /*                          other */
141   COSTS_N_INSNS (3),                    /* cost of movsx */
142   COSTS_N_INSNS (2),                    /* cost of movzx */
143   15,                                   /* "large" insn */
144   3,                                    /* MOVE_RATIO */
145   4,                                    /* cost for loading QImode using movzbl */
146   {2, 4, 2},                            /* cost of loading integer registers
147                                            in QImode, HImode and SImode.
148                                            Relative to reg-reg move (2).  */
149   {2, 4, 2},                            /* cost of storing integer registers */
150   2,                                    /* cost of reg,reg fld/fst */
151   {8, 8, 8},                            /* cost of loading fp registers
152                                            in SFmode, DFmode and XFmode */
153   {8, 8, 8},                            /* cost of storing fp registers
154                                            in SFmode, DFmode and XFmode */
155   2,                                    /* cost of moving MMX register */
156   {4, 8},                               /* cost of loading MMX registers
157                                            in SImode and DImode */
158   {4, 8},                               /* cost of storing MMX registers
159                                            in SImode and DImode */
160   2,                                    /* cost of moving SSE register */
161   {4, 8, 16},                           /* cost of loading SSE registers
162                                            in SImode, DImode and TImode */
163   {4, 8, 16},                           /* cost of storing SSE registers
164                                            in SImode, DImode and TImode */
165   3,                                    /* MMX or SSE register to integer */
166   0,                                    /* size of prefetch block */
167   0,                                    /* number of parallel prefetches */
168   1,                                    /* Branch cost */
169   COSTS_N_INSNS (23),                   /* cost of FADD and FSUB insns.  */
170   COSTS_N_INSNS (27),                   /* cost of FMUL instruction.  */
171   COSTS_N_INSNS (88),                   /* cost of FDIV instruction.  */
172   COSTS_N_INSNS (22),                   /* cost of FABS instruction.  */
173   COSTS_N_INSNS (24),                   /* cost of FCHS instruction.  */
174   COSTS_N_INSNS (122),                  /* cost of FSQRT instruction.  */
175 };
176
177 static const
178 struct processor_costs i486_cost = {    /* 486 specific costs */
179   COSTS_N_INSNS (1),                    /* cost of an add instruction */
180   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
181   COSTS_N_INSNS (3),                    /* variable shift costs */
182   COSTS_N_INSNS (2),                    /* constant shift costs */
183   {COSTS_N_INSNS (12),                  /* cost of starting multiply for QI */
184    COSTS_N_INSNS (12),                  /*                               HI */
185    COSTS_N_INSNS (12),                  /*                               SI */
186    COSTS_N_INSNS (12),                  /*                               DI */
187    COSTS_N_INSNS (12)},                 /*                               other */
188   1,                                    /* cost of multiply per each bit set */
189   {COSTS_N_INSNS (40),                  /* cost of a divide/mod for QI */
190    COSTS_N_INSNS (40),                  /*                          HI */
191    COSTS_N_INSNS (40),                  /*                          SI */
192    COSTS_N_INSNS (40),                  /*                          DI */
193    COSTS_N_INSNS (40)},                 /*                          other */
194   COSTS_N_INSNS (3),                    /* cost of movsx */
195   COSTS_N_INSNS (2),                    /* cost of movzx */
196   15,                                   /* "large" insn */
197   3,                                    /* MOVE_RATIO */
198   4,                                    /* cost for loading QImode using movzbl */
199   {2, 4, 2},                            /* cost of loading integer registers
200                                            in QImode, HImode and SImode.
201                                            Relative to reg-reg move (2).  */
202   {2, 4, 2},                            /* cost of storing integer registers */
203   2,                                    /* cost of reg,reg fld/fst */
204   {8, 8, 8},                            /* cost of loading fp registers
205                                            in SFmode, DFmode and XFmode */
206   {8, 8, 8},                            /* cost of storing fp registers
207                                            in SFmode, DFmode and XFmode */
208   2,                                    /* cost of moving MMX register */
209   {4, 8},                               /* cost of loading MMX registers
210                                            in SImode and DImode */
211   {4, 8},                               /* cost of storing MMX registers
212                                            in SImode and DImode */
213   2,                                    /* cost of moving SSE register */
214   {4, 8, 16},                           /* cost of loading SSE registers
215                                            in SImode, DImode and TImode */
216   {4, 8, 16},                           /* cost of storing SSE registers
217                                            in SImode, DImode and TImode */
218   3,                                    /* MMX or SSE register to integer */
219   0,                                    /* size of prefetch block */
220   0,                                    /* number of parallel prefetches */
221   1,                                    /* Branch cost */
222   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
223   COSTS_N_INSNS (16),                   /* cost of FMUL instruction.  */
224   COSTS_N_INSNS (73),                   /* cost of FDIV instruction.  */
225   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
226   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
227   COSTS_N_INSNS (83),                   /* cost of FSQRT instruction.  */
228 };
229
230 static const
231 struct processor_costs pentium_cost = {
232   COSTS_N_INSNS (1),                    /* cost of an add instruction */
233   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
234   COSTS_N_INSNS (4),                    /* variable shift costs */
235   COSTS_N_INSNS (1),                    /* constant shift costs */
236   {COSTS_N_INSNS (11),                  /* cost of starting multiply for QI */
237    COSTS_N_INSNS (11),                  /*                               HI */
238    COSTS_N_INSNS (11),                  /*                               SI */
239    COSTS_N_INSNS (11),                  /*                               DI */
240    COSTS_N_INSNS (11)},                 /*                               other */
241   0,                                    /* cost of multiply per each bit set */
242   {COSTS_N_INSNS (25),                  /* cost of a divide/mod for QI */
243    COSTS_N_INSNS (25),                  /*                          HI */
244    COSTS_N_INSNS (25),                  /*                          SI */
245    COSTS_N_INSNS (25),                  /*                          DI */
246    COSTS_N_INSNS (25)},                 /*                          other */
247   COSTS_N_INSNS (3),                    /* cost of movsx */
248   COSTS_N_INSNS (2),                    /* cost of movzx */
249   8,                                    /* "large" insn */
250   6,                                    /* MOVE_RATIO */
251   6,                                    /* cost for loading QImode using movzbl */
252   {2, 4, 2},                            /* cost of loading integer registers
253                                            in QImode, HImode and SImode.
254                                            Relative to reg-reg move (2).  */
255   {2, 4, 2},                            /* cost of storing integer registers */
256   2,                                    /* cost of reg,reg fld/fst */
257   {2, 2, 6},                            /* cost of loading fp registers
258                                            in SFmode, DFmode and XFmode */
259   {4, 4, 6},                            /* cost of storing fp registers
260                                            in SFmode, DFmode and XFmode */
261   8,                                    /* cost of moving MMX register */
262   {8, 8},                               /* cost of loading MMX registers
263                                            in SImode and DImode */
264   {8, 8},                               /* cost of storing MMX registers
265                                            in SImode and DImode */
266   2,                                    /* cost of moving SSE register */
267   {4, 8, 16},                           /* cost of loading SSE registers
268                                            in SImode, DImode and TImode */
269   {4, 8, 16},                           /* cost of storing SSE registers
270                                            in SImode, DImode and TImode */
271   3,                                    /* MMX or SSE register to integer */
272   0,                                    /* size of prefetch block */
273   0,                                    /* number of parallel prefetches */
274   2,                                    /* Branch cost */
275   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
276   COSTS_N_INSNS (3),                    /* cost of FMUL instruction.  */
277   COSTS_N_INSNS (39),                   /* cost of FDIV instruction.  */
278   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
279   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
280   COSTS_N_INSNS (70),                   /* cost of FSQRT instruction.  */
281 };
282
283 static const
284 struct processor_costs pentiumpro_cost = {
285   COSTS_N_INSNS (1),                    /* cost of an add instruction */
286   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
287   COSTS_N_INSNS (1),                    /* variable shift costs */
288   COSTS_N_INSNS (1),                    /* constant shift costs */
289   {COSTS_N_INSNS (4),                   /* cost of starting multiply for QI */
290    COSTS_N_INSNS (4),                   /*                               HI */
291    COSTS_N_INSNS (4),                   /*                               SI */
292    COSTS_N_INSNS (4),                   /*                               DI */
293    COSTS_N_INSNS (4)},                  /*                               other */
294   0,                                    /* cost of multiply per each bit set */
295   {COSTS_N_INSNS (17),                  /* cost of a divide/mod for QI */
296    COSTS_N_INSNS (17),                  /*                          HI */
297    COSTS_N_INSNS (17),                  /*                          SI */
298    COSTS_N_INSNS (17),                  /*                          DI */
299    COSTS_N_INSNS (17)},                 /*                          other */
300   COSTS_N_INSNS (1),                    /* cost of movsx */
301   COSTS_N_INSNS (1),                    /* cost of movzx */
302   8,                                    /* "large" insn */
303   6,                                    /* MOVE_RATIO */
304   2,                                    /* cost for loading QImode using movzbl */
305   {4, 4, 4},                            /* cost of loading integer registers
306                                            in QImode, HImode and SImode.
307                                            Relative to reg-reg move (2).  */
308   {2, 2, 2},                            /* cost of storing integer registers */
309   2,                                    /* cost of reg,reg fld/fst */
310   {2, 2, 6},                            /* cost of loading fp registers
311                                            in SFmode, DFmode and XFmode */
312   {4, 4, 6},                            /* cost of storing fp registers
313                                            in SFmode, DFmode and XFmode */
314   2,                                    /* cost of moving MMX register */
315   {2, 2},                               /* cost of loading MMX registers
316                                            in SImode and DImode */
317   {2, 2},                               /* cost of storing MMX registers
318                                            in SImode and DImode */
319   2,                                    /* cost of moving SSE register */
320   {2, 2, 8},                            /* cost of loading SSE registers
321                                            in SImode, DImode and TImode */
322   {2, 2, 8},                            /* cost of storing SSE registers
323                                            in SImode, DImode and TImode */
324   3,                                    /* MMX or SSE register to integer */
325   32,                                   /* size of prefetch block */
326   6,                                    /* number of parallel prefetches */
327   2,                                    /* Branch cost */
328   COSTS_N_INSNS (3),                    /* cost of FADD and FSUB insns.  */
329   COSTS_N_INSNS (5),                    /* cost of FMUL instruction.  */
330   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
331   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
332   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
333   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
334 };
335
336 static const
337 struct processor_costs geode_cost = {
338   COSTS_N_INSNS (1),                    /* cost of an add instruction */
339   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
340   COSTS_N_INSNS (2),                    /* variable shift costs */
341   COSTS_N_INSNS (1),                    /* constant shift costs */
342   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
343    COSTS_N_INSNS (4),                   /*                               HI */
344    COSTS_N_INSNS (7),                   /*                               SI */
345    COSTS_N_INSNS (7),                   /*                               DI */
346    COSTS_N_INSNS (7)},                  /*                               other */
347   0,                                    /* cost of multiply per each bit set */
348   {COSTS_N_INSNS (15),                  /* cost of a divide/mod for QI */
349    COSTS_N_INSNS (23),                  /*                          HI */
350    COSTS_N_INSNS (39),                  /*                          SI */
351    COSTS_N_INSNS (39),                  /*                          DI */
352    COSTS_N_INSNS (39)},                 /*                          other */
353   COSTS_N_INSNS (1),                    /* cost of movsx */
354   COSTS_N_INSNS (1),                    /* cost of movzx */
355   8,                                    /* "large" insn */
356   4,                                    /* MOVE_RATIO */
357   1,                                    /* cost for loading QImode using movzbl */
358   {1, 1, 1},                            /* cost of loading integer registers
359                                            in QImode, HImode and SImode.
360                                            Relative to reg-reg move (2).  */
361   {1, 1, 1},                            /* cost of storing integer registers */
362   1,                                    /* cost of reg,reg fld/fst */
363   {1, 1, 1},                            /* cost of loading fp registers
364                                            in SFmode, DFmode and XFmode */
365   {4, 6, 6},                            /* cost of storing fp registers
366                                            in SFmode, DFmode and XFmode */
367
368   1,                                    /* cost of moving MMX register */
369   {1, 1},                               /* cost of loading MMX registers
370                                            in SImode and DImode */
371   {1, 1},                               /* cost of storing MMX registers
372                                            in SImode and DImode */
373   1,                                    /* cost of moving SSE register */
374   {1, 1, 1},                            /* cost of loading SSE registers
375                                            in SImode, DImode and TImode */
376   {1, 1, 1},                            /* cost of storing SSE registers
377                                            in SImode, DImode and TImode */
378   1,                                    /* MMX or SSE register to integer */
379   32,                                   /* size of prefetch block */
380   1,                                    /* number of parallel prefetches */
381   1,                                    /* Branch cost */
382   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
383   COSTS_N_INSNS (11),                   /* cost of FMUL instruction.  */
384   COSTS_N_INSNS (47),                   /* cost of FDIV instruction.  */
385   COSTS_N_INSNS (1),                    /* cost of FABS instruction.  */
386   COSTS_N_INSNS (1),                    /* cost of FCHS instruction.  */
387   COSTS_N_INSNS (54),                   /* cost of FSQRT instruction.  */
388 };
389
390 static const
391 struct processor_costs k6_cost = {
392   COSTS_N_INSNS (1),                    /* cost of an add instruction */
393   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
394   COSTS_N_INSNS (1),                    /* variable shift costs */
395   COSTS_N_INSNS (1),                    /* constant shift costs */
396   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
397    COSTS_N_INSNS (3),                   /*                               HI */
398    COSTS_N_INSNS (3),                   /*                               SI */
399    COSTS_N_INSNS (3),                   /*                               DI */
400    COSTS_N_INSNS (3)},                  /*                               other */
401   0,                                    /* cost of multiply per each bit set */
402   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
403    COSTS_N_INSNS (18),                  /*                          HI */
404    COSTS_N_INSNS (18),                  /*                          SI */
405    COSTS_N_INSNS (18),                  /*                          DI */
406    COSTS_N_INSNS (18)},                 /*                          other */
407   COSTS_N_INSNS (2),                    /* cost of movsx */
408   COSTS_N_INSNS (2),                    /* cost of movzx */
409   8,                                    /* "large" insn */
410   4,                                    /* MOVE_RATIO */
411   3,                                    /* cost for loading QImode using movzbl */
412   {4, 5, 4},                            /* cost of loading integer registers
413                                            in QImode, HImode and SImode.
414                                            Relative to reg-reg move (2).  */
415   {2, 3, 2},                            /* cost of storing integer registers */
416   4,                                    /* cost of reg,reg fld/fst */
417   {6, 6, 6},                            /* cost of loading fp registers
418                                            in SFmode, DFmode and XFmode */
419   {4, 4, 4},                            /* cost of storing fp registers
420                                            in SFmode, DFmode and XFmode */
421   2,                                    /* cost of moving MMX register */
422   {2, 2},                               /* cost of loading MMX registers
423                                            in SImode and DImode */
424   {2, 2},                               /* cost of storing MMX registers
425                                            in SImode and DImode */
426   2,                                    /* cost of moving SSE register */
427   {2, 2, 8},                            /* cost of loading SSE registers
428                                            in SImode, DImode and TImode */
429   {2, 2, 8},                            /* cost of storing SSE registers
430                                            in SImode, DImode and TImode */
431   6,                                    /* MMX or SSE register to integer */
432   32,                                   /* size of prefetch block */
433   1,                                    /* number of parallel prefetches */
434   1,                                    /* Branch cost */
435   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
436   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
437   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
438   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
439   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
440   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
441 };
442
443 static const
444 struct processor_costs athlon_cost = {
445   COSTS_N_INSNS (1),                    /* cost of an add instruction */
446   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
447   COSTS_N_INSNS (1),                    /* variable shift costs */
448   COSTS_N_INSNS (1),                    /* constant shift costs */
449   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
450    COSTS_N_INSNS (5),                   /*                               HI */
451    COSTS_N_INSNS (5),                   /*                               SI */
452    COSTS_N_INSNS (5),                   /*                               DI */
453    COSTS_N_INSNS (5)},                  /*                               other */
454   0,                                    /* cost of multiply per each bit set */
455   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
456    COSTS_N_INSNS (26),                  /*                          HI */
457    COSTS_N_INSNS (42),                  /*                          SI */
458    COSTS_N_INSNS (74),                  /*                          DI */
459    COSTS_N_INSNS (74)},                 /*                          other */
460   COSTS_N_INSNS (1),                    /* cost of movsx */
461   COSTS_N_INSNS (1),                    /* cost of movzx */
462   8,                                    /* "large" insn */
463   9,                                    /* MOVE_RATIO */
464   4,                                    /* cost for loading QImode using movzbl */
465   {3, 4, 3},                            /* cost of loading integer registers
466                                            in QImode, HImode and SImode.
467                                            Relative to reg-reg move (2).  */
468   {3, 4, 3},                            /* cost of storing integer registers */
469   4,                                    /* cost of reg,reg fld/fst */
470   {4, 4, 12},                           /* cost of loading fp registers
471                                            in SFmode, DFmode and XFmode */
472   {6, 6, 8},                            /* cost of storing fp registers
473                                            in SFmode, DFmode and XFmode */
474   2,                                    /* cost of moving MMX register */
475   {4, 4},                               /* cost of loading MMX registers
476                                            in SImode and DImode */
477   {4, 4},                               /* cost of storing MMX registers
478                                            in SImode and DImode */
479   2,                                    /* cost of moving SSE register */
480   {4, 4, 6},                            /* cost of loading SSE registers
481                                            in SImode, DImode and TImode */
482   {4, 4, 5},                            /* cost of storing SSE registers
483                                            in SImode, DImode and TImode */
484   5,                                    /* MMX or SSE register to integer */
485   64,                                   /* size of prefetch block */
486   6,                                    /* number of parallel prefetches */
487   5,                                    /* Branch cost */
488   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
489   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
490   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
491   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
492   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
493   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
494 };
495
496 static const
497 struct processor_costs k8_cost = {
498   COSTS_N_INSNS (1),                    /* cost of an add instruction */
499   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
500   COSTS_N_INSNS (1),                    /* variable shift costs */
501   COSTS_N_INSNS (1),                    /* constant shift costs */
502   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
503    COSTS_N_INSNS (4),                   /*                               HI */
504    COSTS_N_INSNS (3),                   /*                               SI */
505    COSTS_N_INSNS (4),                   /*                               DI */
506    COSTS_N_INSNS (5)},                  /*                               other */
507   0,                                    /* cost of multiply per each bit set */
508   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
509    COSTS_N_INSNS (26),                  /*                          HI */
510    COSTS_N_INSNS (42),                  /*                          SI */
511    COSTS_N_INSNS (74),                  /*                          DI */
512    COSTS_N_INSNS (74)},                 /*                          other */
513   COSTS_N_INSNS (1),                    /* cost of movsx */
514   COSTS_N_INSNS (1),                    /* cost of movzx */
515   8,                                    /* "large" insn */
516   9,                                    /* MOVE_RATIO */
517   4,                                    /* cost for loading QImode using movzbl */
518   {3, 4, 3},                            /* cost of loading integer registers
519                                            in QImode, HImode and SImode.
520                                            Relative to reg-reg move (2).  */
521   {3, 4, 3},                            /* cost of storing integer registers */
522   4,                                    /* cost of reg,reg fld/fst */
523   {4, 4, 12},                           /* cost of loading fp registers
524                                            in SFmode, DFmode and XFmode */
525   {6, 6, 8},                            /* cost of storing fp registers
526                                            in SFmode, DFmode and XFmode */
527   2,                                    /* cost of moving MMX register */
528   {3, 3},                               /* cost of loading MMX registers
529                                            in SImode and DImode */
530   {4, 4},                               /* cost of storing MMX registers
531                                            in SImode and DImode */
532   2,                                    /* cost of moving SSE register */
533   {4, 3, 6},                            /* cost of loading SSE registers
534                                            in SImode, DImode and TImode */
535   {4, 4, 5},                            /* cost of storing SSE registers
536                                            in SImode, DImode and TImode */
537   5,                                    /* MMX or SSE register to integer */
538   64,                                   /* size of prefetch block */
539   6,                                    /* number of parallel prefetches */
540   5,                                    /* Branch cost */
541   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
542   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
543   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
544   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
545   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
546   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
547 };
548
549 static const
550 struct processor_costs pentium4_cost = {
551   COSTS_N_INSNS (1),                    /* cost of an add instruction */
552   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
553   COSTS_N_INSNS (4),                    /* variable shift costs */
554   COSTS_N_INSNS (4),                    /* constant shift costs */
555   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
556    COSTS_N_INSNS (15),                  /*                               HI */
557    COSTS_N_INSNS (15),                  /*                               SI */
558    COSTS_N_INSNS (15),                  /*                               DI */
559    COSTS_N_INSNS (15)},                 /*                               other */
560   0,                                    /* cost of multiply per each bit set */
561   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
562    COSTS_N_INSNS (56),                  /*                          HI */
563    COSTS_N_INSNS (56),                  /*                          SI */
564    COSTS_N_INSNS (56),                  /*                          DI */
565    COSTS_N_INSNS (56)},                 /*                          other */
566   COSTS_N_INSNS (1),                    /* cost of movsx */
567   COSTS_N_INSNS (1),                    /* cost of movzx */
568   16,                                   /* "large" insn */
569   6,                                    /* MOVE_RATIO */
570   2,                                    /* cost for loading QImode using movzbl */
571   {4, 5, 4},                            /* cost of loading integer registers
572                                            in QImode, HImode and SImode.
573                                            Relative to reg-reg move (2).  */
574   {2, 3, 2},                            /* cost of storing integer registers */
575   2,                                    /* cost of reg,reg fld/fst */
576   {2, 2, 6},                            /* cost of loading fp registers
577                                            in SFmode, DFmode and XFmode */
578   {4, 4, 6},                            /* cost of storing fp registers
579                                            in SFmode, DFmode and XFmode */
580   2,                                    /* cost of moving MMX register */
581   {2, 2},                               /* cost of loading MMX registers
582                                            in SImode and DImode */
583   {2, 2},                               /* cost of storing MMX registers
584                                            in SImode and DImode */
585   12,                                   /* cost of moving SSE register */
586   {12, 12, 12},                         /* cost of loading SSE registers
587                                            in SImode, DImode and TImode */
588   {2, 2, 8},                            /* cost of storing SSE registers
589                                            in SImode, DImode and TImode */
590   10,                                   /* MMX or SSE register to integer */
591   64,                                   /* size of prefetch block */
592   6,                                    /* number of parallel prefetches */
593   2,                                    /* Branch cost */
594   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
595   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
596   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
597   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
598   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
599   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
600 };
601
602 static const
603 struct processor_costs nocona_cost = {
604   COSTS_N_INSNS (1),                    /* cost of an add instruction */
605   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
606   COSTS_N_INSNS (1),                    /* variable shift costs */
607   COSTS_N_INSNS (1),                    /* constant shift costs */
608   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
609    COSTS_N_INSNS (10),                  /*                               HI */
610    COSTS_N_INSNS (10),                  /*                               SI */
611    COSTS_N_INSNS (10),                  /*                               DI */
612    COSTS_N_INSNS (10)},                 /*                               other */
613   0,                                    /* cost of multiply per each bit set */
614   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
615    COSTS_N_INSNS (66),                  /*                          HI */
616    COSTS_N_INSNS (66),                  /*                          SI */
617    COSTS_N_INSNS (66),                  /*                          DI */
618    COSTS_N_INSNS (66)},                 /*                          other */
619   COSTS_N_INSNS (1),                    /* cost of movsx */
620   COSTS_N_INSNS (1),                    /* cost of movzx */
621   16,                                   /* "large" insn */
622   17,                                   /* MOVE_RATIO */
623   4,                                    /* cost for loading QImode using movzbl */
624   {4, 4, 4},                            /* cost of loading integer registers
625                                            in QImode, HImode and SImode.
626                                            Relative to reg-reg move (2).  */
627   {4, 4, 4},                            /* cost of storing integer registers */
628   3,                                    /* cost of reg,reg fld/fst */
629   {12, 12, 12},                         /* cost of loading fp registers
630                                            in SFmode, DFmode and XFmode */
631   {4, 4, 4},                            /* cost of storing fp registers
632                                            in SFmode, DFmode and XFmode */
633   6,                                    /* cost of moving MMX register */
634   {12, 12},                             /* cost of loading MMX registers
635                                            in SImode and DImode */
636   {12, 12},                             /* cost of storing MMX registers
637                                            in SImode and DImode */
638   6,                                    /* cost of moving SSE register */
639   {12, 12, 12},                         /* cost of loading SSE registers
640                                            in SImode, DImode and TImode */
641   {12, 12, 12},                         /* cost of storing SSE registers
642                                            in SImode, DImode and TImode */
643   8,                                    /* MMX or SSE register to integer */
644   128,                                  /* size of prefetch block */
645   8,                                    /* number of parallel prefetches */
646   1,                                    /* Branch cost */
647   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
648   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
649   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
650   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
651   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
652   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
653 };
654
655 /* Generic64 should produce code tuned for Nocona and K8.  */
656 static const
657 struct processor_costs generic64_cost = {
658   COSTS_N_INSNS (1),                    /* cost of an add instruction */
659   /* On all chips taken into consideration lea is 2 cycles and more.  With
660      this cost however our current implementation of synth_mult results in
661      use of unnecessary temporary registers causing regression on several
662      SPECfp benchmarks.  */
663   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
664   COSTS_N_INSNS (1),                    /* variable shift costs */
665   COSTS_N_INSNS (1),                    /* constant shift costs */
666   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
667    COSTS_N_INSNS (4),                   /*                               HI */
668    COSTS_N_INSNS (3),                   /*                               SI */
669    COSTS_N_INSNS (4),                   /*                               DI */
670    COSTS_N_INSNS (2)},                  /*                               other */
671   0,                                    /* cost of multiply per each bit set */
672   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
673    COSTS_N_INSNS (26),                  /*                          HI */
674    COSTS_N_INSNS (42),                  /*                          SI */
675    COSTS_N_INSNS (74),                  /*                          DI */
676    COSTS_N_INSNS (74)},                 /*                          other */
677   COSTS_N_INSNS (1),                    /* cost of movsx */
678   COSTS_N_INSNS (1),                    /* cost of movzx */
679   8,                                    /* "large" insn */
680   17,                                   /* MOVE_RATIO */
681   4,                                    /* cost for loading QImode using movzbl */
682   {4, 4, 4},                            /* cost of loading integer registers
683                                            in QImode, HImode and SImode.
684                                            Relative to reg-reg move (2).  */
685   {4, 4, 4},                            /* cost of storing integer registers */
686   4,                                    /* cost of reg,reg fld/fst */
687   {12, 12, 12},                         /* cost of loading fp registers
688                                            in SFmode, DFmode and XFmode */
689   {6, 6, 8},                            /* cost of storing fp registers
690                                            in SFmode, DFmode and XFmode */
691   2,                                    /* cost of moving MMX register */
692   {8, 8},                               /* cost of loading MMX registers
693                                            in SImode and DImode */
694   {8, 8},                               /* cost of storing MMX registers
695                                            in SImode and DImode */
696   2,                                    /* cost of moving SSE register */
697   {8, 8, 8},                            /* cost of loading SSE registers
698                                            in SImode, DImode and TImode */
699   {8, 8, 8},                            /* cost of storing SSE registers
700                                            in SImode, DImode and TImode */
701   5,                                    /* MMX or SSE register to integer */
702   64,                                   /* size of prefetch block */
703   6,                                    /* number of parallel prefetches */
704   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
705      is increased to perhaps more appropriate value of 5.  */
706   3,                                    /* Branch cost */
707   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
708   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
709   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
710   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
711   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
712   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
713 };
714
715 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
716 static const
717 struct processor_costs generic32_cost = {
718   COSTS_N_INSNS (1),                    /* cost of an add instruction */
719   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
720   COSTS_N_INSNS (1),                    /* variable shift costs */
721   COSTS_N_INSNS (1),                    /* constant shift costs */
722   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
723    COSTS_N_INSNS (4),                   /*                               HI */
724    COSTS_N_INSNS (3),                   /*                               SI */
725    COSTS_N_INSNS (4),                   /*                               DI */
726    COSTS_N_INSNS (2)},                  /*                               other */
727   0,                                    /* cost of multiply per each bit set */
728   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
729    COSTS_N_INSNS (26),                  /*                          HI */
730    COSTS_N_INSNS (42),                  /*                          SI */
731    COSTS_N_INSNS (74),                  /*                          DI */
732    COSTS_N_INSNS (74)},                 /*                          other */
733   COSTS_N_INSNS (1),                    /* cost of movsx */
734   COSTS_N_INSNS (1),                    /* cost of movzx */
735   8,                                    /* "large" insn */
736   17,                                   /* MOVE_RATIO */
737   4,                                    /* cost for loading QImode using movzbl */
738   {4, 4, 4},                            /* cost of loading integer registers
739                                            in QImode, HImode and SImode.
740                                            Relative to reg-reg move (2).  */
741   {4, 4, 4},                            /* cost of storing integer registers */
742   4,                                    /* cost of reg,reg fld/fst */
743   {12, 12, 12},                         /* cost of loading fp registers
744                                            in SFmode, DFmode and XFmode */
745   {6, 6, 8},                            /* cost of storing fp registers
746                                            in SFmode, DFmode and XFmode */
747   2,                                    /* cost of moving MMX register */
748   {8, 8},                               /* cost of loading MMX registers
749                                            in SImode and DImode */
750   {8, 8},                               /* cost of storing MMX registers
751                                            in SImode and DImode */
752   2,                                    /* cost of moving SSE register */
753   {8, 8, 8},                            /* cost of loading SSE registers
754                                            in SImode, DImode and TImode */
755   {8, 8, 8},                            /* cost of storing SSE registers
756                                            in SImode, DImode and TImode */
757   5,                                    /* MMX or SSE register to integer */
758   64,                                   /* size of prefetch block */
759   6,                                    /* number of parallel prefetches */
760   3,                                    /* Branch cost */
761   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
762   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
763   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
764   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
765   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
766   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
767 };
768
769 const struct processor_costs *ix86_cost = &pentium_cost;
770
771 /* Processor feature/optimization bitmasks.  */
772 #define m_386 (1<<PROCESSOR_I386)
773 #define m_486 (1<<PROCESSOR_I486)
774 #define m_PENT (1<<PROCESSOR_PENTIUM)
775 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
776 #define m_GEODE  (1<<PROCESSOR_GEODE)
777 #define m_K6_GEODE  (m_K6 | m_GEODE)
778 #define m_K6  (1<<PROCESSOR_K6)
779 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
780 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
781 #define m_K8  (1<<PROCESSOR_K8)
782 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
783 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
784 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
785 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
786 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
787
788 /* Generic instruction choice should be common subset of supported CPUs
789    (PPro/PENT4/NOCONA/Athlon/K8).  */
790
791 /* Leave is not affecting Nocona SPEC2000 results negatively, so enabling for
792    Generic64 seems like good code size tradeoff.  We can't enable it for 32bit
793    generic because it is not working well with PPro base chips.  */
794 const int x86_use_leave = m_386 | m_K6_GEODE | m_ATHLON_K8 | m_GENERIC64;
795 const int x86_push_memory = m_386 | m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
796 const int x86_zero_extend_with_and = m_486 | m_PENT;
797 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC | m_GEODE /* m_386 | m_K6 */;
798 const int x86_double_with_add = ~m_386;
799 const int x86_use_bit_test = m_386;
800 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6 | m_GENERIC;
801 const int x86_cmove = m_PPRO | m_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
802 const int x86_3dnow_a = m_ATHLON_K8;
803 const int x86_deep_branch = m_PPRO | m_K6_GEODE | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
804 /* Branch hints were put in P4 based on simulation result. But
805    after P4 was made, no performance benefit was observed with
806    branch hints. It also increases the code size. As the result,
807    icc never generates branch hints.  */
808 const int x86_branch_hints = 0;
809 const int x86_use_sahf = m_PPRO | m_K6_GEODE | m_PENT4 | m_NOCONA | m_GENERIC32; /*m_GENERIC | m_ATHLON_K8 ? */
810 /* We probably ought to watch for partial register stalls on Generic32
811    compilation setting as well.  However in current implementation the
812    partial register stalls are not eliminated very well - they can
813    be introduced via subregs synthesized by combine and can happen
814    in caller/callee saving sequences.
815    Because this option pays back little on PPro based chips and is in conflict
816    with partial reg. dependencies used by Athlon/P4 based chips, it is better
817    to leave it off for generic32 for now.  */
818 const int x86_partial_reg_stall = m_PPRO;
819 const int x86_partial_flag_reg_stall = m_GENERIC;
820 const int x86_use_himode_fiop = m_386 | m_486 | m_K6_GEODE;
821 const int x86_use_simode_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT | m_GENERIC);
822 const int x86_use_mov0 = m_K6;
823 const int x86_use_cltd = ~(m_PENT | m_K6 | m_GENERIC);
824 const int x86_read_modify_write = ~m_PENT;
825 const int x86_read_modify = ~(m_PENT | m_PPRO);
826 const int x86_split_long_moves = m_PPRO;
827 const int x86_promote_QImode = m_K6_GEODE | m_PENT | m_386 | m_486 | m_ATHLON_K8 | m_GENERIC; /* m_PENT4 ? */
828 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
829 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
830 const int x86_qimode_math = ~(0);
831 const int x86_promote_qi_regs = 0;
832 /* On PPro this flag is meant to avoid partial register stalls.  Just like
833    the x86_partial_reg_stall this option might be considered for Generic32
834    if our scheme for avoiding partial stalls was more effective.  */
835 const int x86_himode_math = ~(m_PPRO);
836 const int x86_promote_hi_regs = m_PPRO;
837 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC;
838 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
839 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6_GEODE | m_PENT4 | m_NOCONA | m_GENERIC;
840 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6_GEODE | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
841 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC | m_GEODE);
842 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
843 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
844 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
845 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
846 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
847 const int x86_shift1 = ~m_486;
848 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
849 /* In Generic model we have an conflict here in between PPro/Pentium4 based chips
850    that thread 128bit SSE registers as single units versus K8 based chips that
851    divide SSE registers to two 64bit halves.
852    x86_sse_partial_reg_dependency promote all store destinations to be 128bit
853    to allow register renaming on 128bit SSE units, but usually results in one
854    extra microop on 64bit SSE units.  Experimental results shows that disabling
855    this option on P4 brings over 20% SPECfp regression, while enabling it on
856    K8 brings roughly 2.4% regression that can be partly masked by careful scheduling
857    of moves.  */
858 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
859 /* Set for machines where the type and dependencies are resolved on SSE
860    register parts instead of whole registers, so we may maintain just
861    lower part of scalar values in proper format leaving the upper part
862    undefined.  */
863 const int x86_sse_split_regs = m_ATHLON_K8;
864 const int x86_sse_typeless_stores = m_ATHLON_K8;
865 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4 | m_NOCONA;
866 const int x86_use_ffreep = m_ATHLON_K8;
867 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6_GEODE;
868 const int x86_use_incdec = ~(m_PENT4 | m_NOCONA | m_GENERIC);
869
870 /* ??? Allowing interunit moves makes it all too easy for the compiler to put
871    integer data in xmm registers.  Which results in pretty abysmal code.  */
872 const int x86_inter_unit_moves = 0 /* ~(m_ATHLON_K8) */;
873
874 const int x86_ext_80387_constants = m_K6_GEODE | m_ATHLON | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC32;
875 /* Some CPU cores are not able to predict more than 4 branch instructions in
876    the 16 byte window.  */
877 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
878 const int x86_schedule = m_PPRO | m_ATHLON_K8 | m_K6_GEODE | m_PENT | m_GENERIC;
879 const int x86_use_bt = m_ATHLON_K8;
880 /* Compare and exchange was added for 80486.  */
881 const int x86_cmpxchg = ~m_386;
882 /* Compare and exchange 8 bytes was added for pentium.  */
883 const int x86_cmpxchg8b = ~(m_386 | m_486);
884 /* Compare and exchange 16 bytes was added for nocona.  */
885 const int x86_cmpxchg16b = m_NOCONA;
886 /* Exchange and add was added for 80486.  */
887 const int x86_xadd = ~m_386;
888 /* Byteswap was added for 80486.  */
889 const int x86_bswap = ~m_386;
890 const int x86_pad_returns = m_ATHLON_K8 | m_GENERIC;
891
892 /* In case the average insn count for single function invocation is
893    lower than this constant, emit fast (but longer) prologue and
894    epilogue code.  */
895 #define FAST_PROLOGUE_INSN_COUNT 20
896
897 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
898 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
899 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
900 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
901
902 /* Array of the smallest class containing reg number REGNO, indexed by
903    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
904
905 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
906 {
907   /* ax, dx, cx, bx */
908   AREG, DREG, CREG, BREG,
909   /* si, di, bp, sp */
910   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
911   /* FP registers */
912   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
913   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
914   /* arg pointer */
915   NON_Q_REGS,
916   /* flags, fpsr, fpcr, dirflag, frame */
917   NO_REGS, NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
918   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
919   SSE_REGS, SSE_REGS,
920   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
921   MMX_REGS, MMX_REGS,
922   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
923   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
924   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
925   SSE_REGS, SSE_REGS,
926 };
927
928 /* The "default" register map used in 32bit mode.  */
929
930 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
931 {
932   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
933   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
934   -1, -1, -1, -1, -1, -1,               /* arg, flags, fpsr, fpcr, dir, frame */
935   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
936   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
937   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
938   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
939 };
940
941 static int const x86_64_int_parameter_registers[6] =
942 {
943   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
944   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
945 };
946
947 static int const x86_64_int_return_registers[4] =
948 {
949   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
950 };
951
952 /* The "default" register map used in 64bit mode.  */
953 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
954 {
955   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
956   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
957   -1, -1, -1, -1, -1, -1,               /* arg, flags, fpsr, fpcr, dir, frame */
958   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
959   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
960   8,9,10,11,12,13,14,15,                /* extended integer registers */
961   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
962 };
963
964 /* Define the register numbers to be used in Dwarf debugging information.
965    The SVR4 reference port C compiler uses the following register numbers
966    in its Dwarf output code:
967         0 for %eax (gcc regno = 0)
968         1 for %ecx (gcc regno = 2)
969         2 for %edx (gcc regno = 1)
970         3 for %ebx (gcc regno = 3)
971         4 for %esp (gcc regno = 7)
972         5 for %ebp (gcc regno = 6)
973         6 for %esi (gcc regno = 4)
974         7 for %edi (gcc regno = 5)
975    The following three DWARF register numbers are never generated by
976    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
977    believes these numbers have these meanings.
978         8  for %eip    (no gcc equivalent)
979         9  for %eflags (gcc regno = 17)
980         10 for %trapno (no gcc equivalent)
981    It is not at all clear how we should number the FP stack registers
982    for the x86 architecture.  If the version of SDB on x86/svr4 were
983    a bit less brain dead with respect to floating-point then we would
984    have a precedent to follow with respect to DWARF register numbers
985    for x86 FP registers, but the SDB on x86/svr4 is so completely
986    broken with respect to FP registers that it is hardly worth thinking
987    of it as something to strive for compatibility with.
988    The version of x86/svr4 SDB I have at the moment does (partially)
989    seem to believe that DWARF register number 11 is associated with
990    the x86 register %st(0), but that's about all.  Higher DWARF
991    register numbers don't seem to be associated with anything in
992    particular, and even for DWARF regno 11, SDB only seems to under-
993    stand that it should say that a variable lives in %st(0) (when
994    asked via an `=' command) if we said it was in DWARF regno 11,
995    but SDB still prints garbage when asked for the value of the
996    variable in question (via a `/' command).
997    (Also note that the labels SDB prints for various FP stack regs
998    when doing an `x' command are all wrong.)
999    Note that these problems generally don't affect the native SVR4
1000    C compiler because it doesn't allow the use of -O with -g and
1001    because when it is *not* optimizing, it allocates a memory
1002    location for each floating-point variable, and the memory
1003    location is what gets described in the DWARF AT_location
1004    attribute for the variable in question.
1005    Regardless of the severe mental illness of the x86/svr4 SDB, we
1006    do something sensible here and we use the following DWARF
1007    register numbers.  Note that these are all stack-top-relative
1008    numbers.
1009         11 for %st(0) (gcc regno = 8)
1010         12 for %st(1) (gcc regno = 9)
1011         13 for %st(2) (gcc regno = 10)
1012         14 for %st(3) (gcc regno = 11)
1013         15 for %st(4) (gcc regno = 12)
1014         16 for %st(5) (gcc regno = 13)
1015         17 for %st(6) (gcc regno = 14)
1016         18 for %st(7) (gcc regno = 15)
1017 */
1018 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
1019 {
1020   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
1021   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
1022   -1, 9, -1, -1, -1, -1,                /* arg, flags, fpsr, fpcr, dir, frame */
1023   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
1024   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
1025   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
1026   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
1027 };
1028
1029 /* Test and compare insns in i386.md store the information needed to
1030    generate branch and scc insns here.  */
1031
1032 rtx ix86_compare_op0 = NULL_RTX;
1033 rtx ix86_compare_op1 = NULL_RTX;
1034 rtx ix86_compare_emitted = NULL_RTX;
1035
1036 /* Size of the register save area.  */
1037 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
1038
1039 /* Define the structure for the machine field in struct function.  */
1040
1041 struct stack_local_entry GTY(())
1042 {
1043   unsigned short mode;
1044   unsigned short n;
1045   rtx rtl;
1046   struct stack_local_entry *next;
1047 };
1048
1049 /* Structure describing stack frame layout.
1050    Stack grows downward:
1051
1052    [arguments]
1053                                               <- ARG_POINTER
1054    saved pc
1055
1056    saved frame pointer if frame_pointer_needed
1057                                               <- HARD_FRAME_POINTER
1058    [saved regs]
1059
1060    [padding1]          \
1061                         )
1062    [va_arg registers]  (
1063                         > to_allocate         <- FRAME_POINTER
1064    [frame]             (
1065                         )
1066    [padding2]          /
1067   */
1068 struct ix86_frame
1069 {
1070   int nregs;
1071   int padding1;
1072   int va_arg_size;
1073   HOST_WIDE_INT frame;
1074   int padding2;
1075   int outgoing_arguments_size;
1076   int red_zone_size;
1077
1078   HOST_WIDE_INT to_allocate;
1079   /* The offsets relative to ARG_POINTER.  */
1080   HOST_WIDE_INT frame_pointer_offset;
1081   HOST_WIDE_INT hard_frame_pointer_offset;
1082   HOST_WIDE_INT stack_pointer_offset;
1083
1084   /* When save_regs_using_mov is set, emit prologue using
1085      move instead of push instructions.  */
1086   bool save_regs_using_mov;
1087 };
1088
1089 /* Code model option.  */
1090 enum cmodel ix86_cmodel;
1091 /* Asm dialect.  */
1092 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1093 /* TLS dialects.  */
1094 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1095
1096 /* Which unit we are generating floating point math for.  */
1097 enum fpmath_unit ix86_fpmath;
1098
1099 /* Which cpu are we scheduling for.  */
1100 enum processor_type ix86_tune;
1101 /* Which instruction set architecture to use.  */
1102 enum processor_type ix86_arch;
1103
1104 /* true if sse prefetch instruction is not NOOP.  */
1105 int x86_prefetch_sse;
1106
1107 /* ix86_regparm_string as a number */
1108 static int ix86_regparm;
1109
1110 /* -mstackrealign option */
1111 extern int ix86_force_align_arg_pointer;
1112 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1113
1114 /* Preferred alignment for stack boundary in bits.  */
1115 unsigned int ix86_preferred_stack_boundary;
1116
1117 /* Values 1-5: see jump.c */
1118 int ix86_branch_cost;
1119
1120 /* Variables which are this size or smaller are put in the data/bss
1121    or ldata/lbss sections.  */
1122
1123 int ix86_section_threshold = 65536;
1124
1125 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1126 char internal_label_prefix[16];
1127 int internal_label_prefix_len;
1128 \f
1129 static bool ix86_handle_option (size_t, const char *, int);
1130 static void output_pic_addr_const (FILE *, rtx, int);
1131 static void put_condition_code (enum rtx_code, enum machine_mode,
1132                                 int, int, FILE *);
1133 static const char *get_some_local_dynamic_name (void);
1134 static int get_some_local_dynamic_name_1 (rtx *, void *);
1135 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
1136 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
1137                                                    rtx *);
1138 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
1139 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
1140                                                    enum machine_mode);
1141 static rtx get_thread_pointer (int);
1142 static rtx legitimize_tls_address (rtx, enum tls_model, int);
1143 static void get_pc_thunk_name (char [32], unsigned int);
1144 static rtx gen_push (rtx);
1145 static int ix86_flags_dependent (rtx, rtx, enum attr_type);
1146 static int ix86_agi_dependent (rtx, rtx, enum attr_type);
1147 static struct machine_function * ix86_init_machine_status (void);
1148 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
1149 static int ix86_nsaved_regs (void);
1150 static void ix86_emit_save_regs (void);
1151 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
1152 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
1153 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
1154 static HOST_WIDE_INT ix86_GOT_alias_set (void);
1155 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
1156 static rtx ix86_expand_aligntest (rtx, int);
1157 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
1158 static int ix86_issue_rate (void);
1159 static int ix86_adjust_cost (rtx, rtx, rtx, int);
1160 static int ia32_multipass_dfa_lookahead (void);
1161 static void ix86_init_mmx_sse_builtins (void);
1162 static rtx x86_this_parameter (tree);
1163 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
1164                                  HOST_WIDE_INT, tree);
1165 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
1166 static void x86_file_start (void);
1167 static void ix86_reorg (void);
1168 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
1169 static tree ix86_build_builtin_va_list (void);
1170 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
1171                                          tree, int *, int);
1172 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
1173 static bool ix86_scalar_mode_supported_p (enum machine_mode);
1174 static bool ix86_vector_mode_supported_p (enum machine_mode);
1175
1176 static int ix86_address_cost (rtx);
1177 static bool ix86_cannot_force_const_mem (rtx);
1178 static rtx ix86_delegitimize_address (rtx);
1179
1180 static void i386_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1181
1182 struct builtin_description;
1183 static rtx ix86_expand_sse_comi (const struct builtin_description *,
1184                                  tree, rtx);
1185 static rtx ix86_expand_sse_compare (const struct builtin_description *,
1186                                     tree, rtx);
1187 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
1188 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
1189 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
1190 static rtx ix86_expand_store_builtin (enum insn_code, tree);
1191 static rtx safe_vector_operand (rtx, enum machine_mode);
1192 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
1193 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
1194 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
1195 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
1196 static int ix86_fp_comparison_cost (enum rtx_code code);
1197 static unsigned int ix86_select_alt_pic_regnum (void);
1198 static int ix86_save_reg (unsigned int, int);
1199 static void ix86_compute_frame_layout (struct ix86_frame *);
1200 static int ix86_comp_type_attributes (tree, tree);
1201 static int ix86_function_regparm (tree, tree);
1202 const struct attribute_spec ix86_attribute_table[];
1203 static bool ix86_function_ok_for_sibcall (tree, tree);
1204 static tree ix86_handle_cconv_attribute (tree *, tree, tree, int, bool *);
1205 static int ix86_value_regno (enum machine_mode, tree, tree);
1206 static bool contains_128bit_aligned_vector_p (tree);
1207 static rtx ix86_struct_value_rtx (tree, int);
1208 static bool ix86_ms_bitfield_layout_p (tree);
1209 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
1210 static int extended_reg_mentioned_1 (rtx *, void *);
1211 static bool ix86_rtx_costs (rtx, int, int, int *);
1212 static int min_insn_size (rtx);
1213 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
1214 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
1215 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
1216                                     tree, bool);
1217 static void ix86_init_builtins (void);
1218 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1219 static const char *ix86_mangle_fundamental_type (tree);
1220 static tree ix86_stack_protect_fail (void);
1221 static rtx ix86_internal_arg_pointer (void);
1222 static void ix86_dwarf_handle_frame_unspec (const char *, rtx, int);
1223
1224 /* This function is only used on Solaris.  */
1225 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
1226   ATTRIBUTE_UNUSED;
1227
1228 /* Register class used for passing given 64bit part of the argument.
1229    These represent classes as documented by the PS ABI, with the exception
1230    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1231    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1232
1233    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1234    whenever possible (upper half does contain padding).
1235  */
1236 enum x86_64_reg_class
1237   {
1238     X86_64_NO_CLASS,
1239     X86_64_INTEGER_CLASS,
1240     X86_64_INTEGERSI_CLASS,
1241     X86_64_SSE_CLASS,
1242     X86_64_SSESF_CLASS,
1243     X86_64_SSEDF_CLASS,
1244     X86_64_SSEUP_CLASS,
1245     X86_64_X87_CLASS,
1246     X86_64_X87UP_CLASS,
1247     X86_64_COMPLEX_X87_CLASS,
1248     X86_64_MEMORY_CLASS
1249   };
1250 static const char * const x86_64_reg_class_name[] = {
1251   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1252   "sseup", "x87", "x87up", "cplx87", "no"
1253 };
1254
1255 #define MAX_CLASSES 4
1256
1257 /* Table of constants used by fldpi, fldln2, etc....  */
1258 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1259 static bool ext_80387_constants_init = 0;
1260 static void init_ext_80387_constants (void);
1261 static bool ix86_in_large_data_p (tree) ATTRIBUTE_UNUSED;
1262 static void ix86_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
1263 static void x86_64_elf_unique_section (tree decl, int reloc) ATTRIBUTE_UNUSED;
1264 static section *x86_64_elf_select_section (tree decl, int reloc,
1265                                            unsigned HOST_WIDE_INT align)
1266                                              ATTRIBUTE_UNUSED;
1267 \f
1268 /* Initialize the GCC target structure.  */
1269 #undef TARGET_ATTRIBUTE_TABLE
1270 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
1271 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1272 #  undef TARGET_MERGE_DECL_ATTRIBUTES
1273 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
1274 #endif
1275
1276 #undef TARGET_COMP_TYPE_ATTRIBUTES
1277 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
1278
1279 #undef TARGET_INIT_BUILTINS
1280 #define TARGET_INIT_BUILTINS ix86_init_builtins
1281 #undef TARGET_EXPAND_BUILTIN
1282 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
1283
1284 #undef TARGET_ASM_FUNCTION_EPILOGUE
1285 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
1286
1287 #undef TARGET_ENCODE_SECTION_INFO
1288 #ifndef SUBTARGET_ENCODE_SECTION_INFO
1289 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
1290 #else
1291 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
1292 #endif
1293
1294 #undef TARGET_ASM_OPEN_PAREN
1295 #define TARGET_ASM_OPEN_PAREN ""
1296 #undef TARGET_ASM_CLOSE_PAREN
1297 #define TARGET_ASM_CLOSE_PAREN ""
1298
1299 #undef TARGET_ASM_ALIGNED_HI_OP
1300 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
1301 #undef TARGET_ASM_ALIGNED_SI_OP
1302 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
1303 #ifdef ASM_QUAD
1304 #undef TARGET_ASM_ALIGNED_DI_OP
1305 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1306 #endif
1307
1308 #undef TARGET_ASM_UNALIGNED_HI_OP
1309 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1310 #undef TARGET_ASM_UNALIGNED_SI_OP
1311 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1312 #undef TARGET_ASM_UNALIGNED_DI_OP
1313 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1314
1315 #undef TARGET_SCHED_ADJUST_COST
1316 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1317 #undef TARGET_SCHED_ISSUE_RATE
1318 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1319 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1320 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1321   ia32_multipass_dfa_lookahead
1322
1323 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1324 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1325
1326 #ifdef HAVE_AS_TLS
1327 #undef TARGET_HAVE_TLS
1328 #define TARGET_HAVE_TLS true
1329 #endif
1330 #undef TARGET_CANNOT_FORCE_CONST_MEM
1331 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1332 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1333 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
1334
1335 #undef TARGET_DELEGITIMIZE_ADDRESS
1336 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1337
1338 #undef TARGET_MS_BITFIELD_LAYOUT_P
1339 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1340
1341 #if TARGET_MACHO
1342 #undef TARGET_BINDS_LOCAL_P
1343 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1344 #endif
1345
1346 #undef TARGET_ASM_OUTPUT_MI_THUNK
1347 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1348 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1349 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1350
1351 #undef TARGET_ASM_FILE_START
1352 #define TARGET_ASM_FILE_START x86_file_start
1353
1354 #undef TARGET_DEFAULT_TARGET_FLAGS
1355 #define TARGET_DEFAULT_TARGET_FLAGS     \
1356   (TARGET_DEFAULT                       \
1357    | TARGET_64BIT_DEFAULT               \
1358    | TARGET_SUBTARGET_DEFAULT           \
1359    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
1360
1361 #undef TARGET_HANDLE_OPTION
1362 #define TARGET_HANDLE_OPTION ix86_handle_option
1363
1364 #undef TARGET_RTX_COSTS
1365 #define TARGET_RTX_COSTS ix86_rtx_costs
1366 #undef TARGET_ADDRESS_COST
1367 #define TARGET_ADDRESS_COST ix86_address_cost
1368
1369 #undef TARGET_FIXED_CONDITION_CODE_REGS
1370 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1371 #undef TARGET_CC_MODES_COMPATIBLE
1372 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1373
1374 #undef TARGET_MACHINE_DEPENDENT_REORG
1375 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1376
1377 #undef TARGET_BUILD_BUILTIN_VA_LIST
1378 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1379
1380 #undef TARGET_MD_ASM_CLOBBERS
1381 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1382
1383 #undef TARGET_PROMOTE_PROTOTYPES
1384 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1385 #undef TARGET_STRUCT_VALUE_RTX
1386 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1387 #undef TARGET_SETUP_INCOMING_VARARGS
1388 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1389 #undef TARGET_MUST_PASS_IN_STACK
1390 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1391 #undef TARGET_PASS_BY_REFERENCE
1392 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1393 #undef TARGET_INTERNAL_ARG_POINTER
1394 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
1395 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
1396 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
1397
1398 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1399 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1400
1401 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1402 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
1403
1404 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1405 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1406
1407 #ifdef HAVE_AS_TLS
1408 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1409 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
1410 #endif
1411
1412 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1413 #undef TARGET_INSERT_ATTRIBUTES
1414 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1415 #endif
1416
1417 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
1418 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
1419
1420 #undef TARGET_STACK_PROTECT_FAIL
1421 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
1422
1423 #undef TARGET_FUNCTION_VALUE
1424 #define TARGET_FUNCTION_VALUE ix86_function_value
1425
1426 struct gcc_target targetm = TARGET_INITIALIZER;
1427
1428 \f
1429 /* The svr4 ABI for the i386 says that records and unions are returned
1430    in memory.  */
1431 #ifndef DEFAULT_PCC_STRUCT_RETURN
1432 #define DEFAULT_PCC_STRUCT_RETURN 1
1433 #endif
1434
1435 /* Implement TARGET_HANDLE_OPTION.  */
1436
1437 static bool
1438 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1439 {
1440   switch (code)
1441     {
1442     case OPT_m3dnow:
1443       if (!value)
1444         {
1445           target_flags &= ~MASK_3DNOW_A;
1446           target_flags_explicit |= MASK_3DNOW_A;
1447         }
1448       return true;
1449
1450     case OPT_mmmx:
1451       if (!value)
1452         {
1453           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1454           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1455         }
1456       return true;
1457
1458     case OPT_msse:
1459       if (!value)
1460         {
1461           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1462           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1463         }
1464       return true;
1465
1466     case OPT_msse2:
1467       if (!value)
1468         {
1469           target_flags &= ~MASK_SSE3;
1470           target_flags_explicit |= MASK_SSE3;
1471         }
1472       return true;
1473
1474     default:
1475       return true;
1476     }
1477 }
1478
1479 /* Sometimes certain combinations of command options do not make
1480    sense on a particular target machine.  You can define a macro
1481    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1482    defined, is executed once just after all the command options have
1483    been parsed.
1484
1485    Don't use this macro to turn on various extra optimizations for
1486    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1487
1488 void
1489 override_options (void)
1490 {
1491   int i;
1492   int ix86_tune_defaulted = 0;
1493
1494   /* Comes from final.c -- no real reason to change it.  */
1495 #define MAX_CODE_ALIGN 16
1496
1497   static struct ptt
1498     {
1499       const struct processor_costs *cost;       /* Processor costs */
1500       const int target_enable;                  /* Target flags to enable.  */
1501       const int target_disable;                 /* Target flags to disable.  */
1502       const int align_loop;                     /* Default alignments.  */
1503       const int align_loop_max_skip;
1504       const int align_jump;
1505       const int align_jump_max_skip;
1506       const int align_func;
1507     }
1508   const processor_target_table[PROCESSOR_max] =
1509     {
1510       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1511       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1512       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1513       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1514       {&geode_cost, 0, 0, 0, 0, 0, 0, 0},
1515       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1516       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1517       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1518       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1519       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1520       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1521       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16}
1522     };
1523
1524   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1525   static struct pta
1526     {
1527       const char *const name;           /* processor name or nickname.  */
1528       const enum processor_type processor;
1529       const enum pta_flags
1530         {
1531           PTA_SSE = 1,
1532           PTA_SSE2 = 2,
1533           PTA_SSE3 = 4,
1534           PTA_MMX = 8,
1535           PTA_PREFETCH_SSE = 16,
1536           PTA_3DNOW = 32,
1537           PTA_3DNOW_A = 64,
1538           PTA_64BIT = 128,
1539           PTA_SSSE3 = 256
1540         } flags;
1541     }
1542   const processor_alias_table[] =
1543     {
1544       {"i386", PROCESSOR_I386, 0},
1545       {"i486", PROCESSOR_I486, 0},
1546       {"i586", PROCESSOR_PENTIUM, 0},
1547       {"pentium", PROCESSOR_PENTIUM, 0},
1548       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1549       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1550       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1551       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1552       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1553       {"i686", PROCESSOR_PENTIUMPRO, 0},
1554       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1555       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1556       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1557       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1558       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1559       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1560                                        | PTA_MMX | PTA_PREFETCH_SSE},
1561       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1562                                         | PTA_MMX | PTA_PREFETCH_SSE},
1563       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1564                                         | PTA_MMX | PTA_PREFETCH_SSE},
1565       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1566                                         | PTA_MMX | PTA_PREFETCH_SSE},
1567       {"geode", PROCESSOR_GEODE, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1568                                    | PTA_3DNOW_A},
1569       {"k6", PROCESSOR_K6, PTA_MMX},
1570       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1571       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1572       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1573                                    | PTA_3DNOW_A},
1574       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1575                                          | PTA_3DNOW | PTA_3DNOW_A},
1576       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1577                                     | PTA_3DNOW_A | PTA_SSE},
1578       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1579                                       | PTA_3DNOW_A | PTA_SSE},
1580       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1581                                       | PTA_3DNOW_A | PTA_SSE},
1582       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1583                                | PTA_SSE | PTA_SSE2 },
1584       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1585                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1586       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1587                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1588       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1589                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1590       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1591                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1592       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1593       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1594     };
1595
1596   int const pta_size = ARRAY_SIZE (processor_alias_table);
1597
1598 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1599   SUBTARGET_OVERRIDE_OPTIONS;
1600 #endif
1601
1602 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1603   SUBSUBTARGET_OVERRIDE_OPTIONS;
1604 #endif
1605
1606   /* -fPIC is the default for x86_64.  */
1607   if (TARGET_MACHO && TARGET_64BIT)
1608     flag_pic = 2;
1609
1610   /* Set the default values for switches whose default depends on TARGET_64BIT
1611      in case they weren't overwritten by command line options.  */
1612   if (TARGET_64BIT)
1613     {
1614       /* Mach-O doesn't support omitting the frame pointer for now.  */
1615       if (flag_omit_frame_pointer == 2)
1616         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1617       if (flag_asynchronous_unwind_tables == 2)
1618         flag_asynchronous_unwind_tables = 1;
1619       if (flag_pcc_struct_return == 2)
1620         flag_pcc_struct_return = 0;
1621     }
1622   else
1623     {
1624       if (flag_omit_frame_pointer == 2)
1625         flag_omit_frame_pointer = 0;
1626       if (flag_asynchronous_unwind_tables == 2)
1627         flag_asynchronous_unwind_tables = 0;
1628       if (flag_pcc_struct_return == 2)
1629         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1630     }
1631
1632   /* Need to check -mtune=generic first.  */
1633   if (ix86_tune_string)
1634     {
1635       if (!strcmp (ix86_tune_string, "generic")
1636           || !strcmp (ix86_tune_string, "i686")
1637           /* As special support for cross compilers we read -mtune=native
1638              as -mtune=generic.  With native compilers we won't see the
1639              -mtune=native, as it was changed by the driver.  */
1640           || !strcmp (ix86_tune_string, "native"))
1641         {
1642           if (TARGET_64BIT)
1643             ix86_tune_string = "generic64";
1644           else
1645             ix86_tune_string = "generic32";
1646         }
1647       else if (!strncmp (ix86_tune_string, "generic", 7))
1648         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1649     }
1650   else
1651     {
1652       if (ix86_arch_string)
1653         ix86_tune_string = ix86_arch_string;
1654       if (!ix86_tune_string)
1655         {
1656           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1657           ix86_tune_defaulted = 1;
1658         }
1659
1660       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1661          need to use a sensible tune option.  */
1662       if (!strcmp (ix86_tune_string, "generic")
1663           || !strcmp (ix86_tune_string, "x86-64")
1664           || !strcmp (ix86_tune_string, "i686"))
1665         {
1666           if (TARGET_64BIT)
1667             ix86_tune_string = "generic64";
1668           else
1669             ix86_tune_string = "generic32";
1670         }
1671     }
1672   if (!strcmp (ix86_tune_string, "x86-64"))
1673     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1674              "-mtune=generic instead as appropriate.");
1675
1676   if (!ix86_arch_string)
1677     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1678   if (!strcmp (ix86_arch_string, "generic"))
1679     error ("generic CPU can be used only for -mtune= switch");
1680   if (!strncmp (ix86_arch_string, "generic", 7))
1681     error ("bad value (%s) for -march= switch", ix86_arch_string);
1682
1683   if (ix86_cmodel_string != 0)
1684     {
1685       if (!strcmp (ix86_cmodel_string, "small"))
1686         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1687       else if (!strcmp (ix86_cmodel_string, "medium"))
1688         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1689       else if (flag_pic)
1690         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1691       else if (!strcmp (ix86_cmodel_string, "32"))
1692         ix86_cmodel = CM_32;
1693       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1694         ix86_cmodel = CM_KERNEL;
1695       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1696         ix86_cmodel = CM_LARGE;
1697       else
1698         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1699     }
1700   else
1701     {
1702       ix86_cmodel = CM_32;
1703       if (TARGET_64BIT)
1704         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1705     }
1706   if (ix86_asm_string != 0)
1707     {
1708       if (! TARGET_MACHO
1709           && !strcmp (ix86_asm_string, "intel"))
1710         ix86_asm_dialect = ASM_INTEL;
1711       else if (!strcmp (ix86_asm_string, "att"))
1712         ix86_asm_dialect = ASM_ATT;
1713       else
1714         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1715     }
1716   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1717     error ("code model %qs not supported in the %s bit mode",
1718            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1719   if (ix86_cmodel == CM_LARGE)
1720     sorry ("code model %<large%> not supported yet");
1721   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1722     sorry ("%i-bit mode not compiled in",
1723            (target_flags & MASK_64BIT) ? 64 : 32);
1724
1725   for (i = 0; i < pta_size; i++)
1726     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1727       {
1728         ix86_arch = processor_alias_table[i].processor;
1729         /* Default cpu tuning to the architecture.  */
1730         ix86_tune = ix86_arch;
1731         if (processor_alias_table[i].flags & PTA_MMX
1732             && !(target_flags_explicit & MASK_MMX))
1733           target_flags |= MASK_MMX;
1734         if (processor_alias_table[i].flags & PTA_3DNOW
1735             && !(target_flags_explicit & MASK_3DNOW))
1736           target_flags |= MASK_3DNOW;
1737         if (processor_alias_table[i].flags & PTA_3DNOW_A
1738             && !(target_flags_explicit & MASK_3DNOW_A))
1739           target_flags |= MASK_3DNOW_A;
1740         if (processor_alias_table[i].flags & PTA_SSE
1741             && !(target_flags_explicit & MASK_SSE))
1742           target_flags |= MASK_SSE;
1743         if (processor_alias_table[i].flags & PTA_SSE2
1744             && !(target_flags_explicit & MASK_SSE2))
1745           target_flags |= MASK_SSE2;
1746         if (processor_alias_table[i].flags & PTA_SSE3
1747             && !(target_flags_explicit & MASK_SSE3))
1748           target_flags |= MASK_SSE3;
1749         if (processor_alias_table[i].flags & PTA_SSSE3
1750             && !(target_flags_explicit & MASK_SSSE3))
1751           target_flags |= MASK_SSSE3;
1752         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1753           x86_prefetch_sse = true;
1754         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1755           error ("CPU you selected does not support x86-64 "
1756                  "instruction set");
1757         break;
1758       }
1759
1760   if (i == pta_size)
1761     error ("bad value (%s) for -march= switch", ix86_arch_string);
1762
1763   for (i = 0; i < pta_size; i++)
1764     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1765       {
1766         ix86_tune = processor_alias_table[i].processor;
1767         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1768           {
1769             if (ix86_tune_defaulted)
1770               {
1771                 ix86_tune_string = "x86-64";
1772                 for (i = 0; i < pta_size; i++)
1773                   if (! strcmp (ix86_tune_string,
1774                                 processor_alias_table[i].name))
1775                     break;
1776                 ix86_tune = processor_alias_table[i].processor;
1777               }
1778             else
1779               error ("CPU you selected does not support x86-64 "
1780                      "instruction set");
1781           }
1782         /* Intel CPUs have always interpreted SSE prefetch instructions as
1783            NOPs; so, we can enable SSE prefetch instructions even when
1784            -mtune (rather than -march) points us to a processor that has them.
1785            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1786            higher processors.  */
1787         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1788           x86_prefetch_sse = true;
1789         break;
1790       }
1791   if (i == pta_size)
1792     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1793
1794   if (optimize_size)
1795     ix86_cost = &size_cost;
1796   else
1797     ix86_cost = processor_target_table[ix86_tune].cost;
1798   target_flags |= processor_target_table[ix86_tune].target_enable;
1799   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1800
1801   /* Arrange to set up i386_stack_locals for all functions.  */
1802   init_machine_status = ix86_init_machine_status;
1803
1804   /* Validate -mregparm= value.  */
1805   if (ix86_regparm_string)
1806     {
1807       i = atoi (ix86_regparm_string);
1808       if (i < 0 || i > REGPARM_MAX)
1809         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1810       else
1811         ix86_regparm = i;
1812     }
1813   else
1814    if (TARGET_64BIT)
1815      ix86_regparm = REGPARM_MAX;
1816
1817   /* If the user has provided any of the -malign-* options,
1818      warn and use that value only if -falign-* is not set.
1819      Remove this code in GCC 3.2 or later.  */
1820   if (ix86_align_loops_string)
1821     {
1822       warning (0, "-malign-loops is obsolete, use -falign-loops");
1823       if (align_loops == 0)
1824         {
1825           i = atoi (ix86_align_loops_string);
1826           if (i < 0 || i > MAX_CODE_ALIGN)
1827             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1828           else
1829             align_loops = 1 << i;
1830         }
1831     }
1832
1833   if (ix86_align_jumps_string)
1834     {
1835       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
1836       if (align_jumps == 0)
1837         {
1838           i = atoi (ix86_align_jumps_string);
1839           if (i < 0 || i > MAX_CODE_ALIGN)
1840             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1841           else
1842             align_jumps = 1 << i;
1843         }
1844     }
1845
1846   if (ix86_align_funcs_string)
1847     {
1848       warning (0, "-malign-functions is obsolete, use -falign-functions");
1849       if (align_functions == 0)
1850         {
1851           i = atoi (ix86_align_funcs_string);
1852           if (i < 0 || i > MAX_CODE_ALIGN)
1853             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1854           else
1855             align_functions = 1 << i;
1856         }
1857     }
1858
1859   /* Default align_* from the processor table.  */
1860   if (align_loops == 0)
1861     {
1862       align_loops = processor_target_table[ix86_tune].align_loop;
1863       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1864     }
1865   if (align_jumps == 0)
1866     {
1867       align_jumps = processor_target_table[ix86_tune].align_jump;
1868       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1869     }
1870   if (align_functions == 0)
1871     {
1872       align_functions = processor_target_table[ix86_tune].align_func;
1873     }
1874
1875   /* Validate -mbranch-cost= value, or provide default.  */
1876   ix86_branch_cost = ix86_cost->branch_cost;
1877   if (ix86_branch_cost_string)
1878     {
1879       i = atoi (ix86_branch_cost_string);
1880       if (i < 0 || i > 5)
1881         error ("-mbranch-cost=%d is not between 0 and 5", i);
1882       else
1883         ix86_branch_cost = i;
1884     }
1885   if (ix86_section_threshold_string)
1886     {
1887       i = atoi (ix86_section_threshold_string);
1888       if (i < 0)
1889         error ("-mlarge-data-threshold=%d is negative", i);
1890       else
1891         ix86_section_threshold = i;
1892     }
1893
1894   if (ix86_tls_dialect_string)
1895     {
1896       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1897         ix86_tls_dialect = TLS_DIALECT_GNU;
1898       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
1899         ix86_tls_dialect = TLS_DIALECT_GNU2;
1900       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1901         ix86_tls_dialect = TLS_DIALECT_SUN;
1902       else
1903         error ("bad value (%s) for -mtls-dialect= switch",
1904                ix86_tls_dialect_string);
1905     }
1906
1907   /* Keep nonleaf frame pointers.  */
1908   if (flag_omit_frame_pointer)
1909     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
1910   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
1911     flag_omit_frame_pointer = 1;
1912
1913   /* If we're doing fast math, we don't care about comparison order
1914      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1915   if (flag_finite_math_only)
1916     target_flags &= ~MASK_IEEE_FP;
1917
1918   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1919      since the insns won't need emulation.  */
1920   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1921     target_flags &= ~MASK_NO_FANCY_MATH_387;
1922
1923   /* Likewise, if the target doesn't have a 387, or we've specified
1924      software floating point, don't use 387 inline intrinsics.  */
1925   if (!TARGET_80387)
1926     target_flags |= MASK_NO_FANCY_MATH_387;
1927
1928   /* Turn on SSE3 builtins for -mssse3.  */
1929   if (TARGET_SSSE3)
1930     target_flags |= MASK_SSE3;
1931
1932   /* Turn on SSE2 builtins for -msse3.  */
1933   if (TARGET_SSE3)
1934     target_flags |= MASK_SSE2;
1935
1936   /* Turn on SSE builtins for -msse2.  */
1937   if (TARGET_SSE2)
1938     target_flags |= MASK_SSE;
1939
1940   /* Turn on MMX builtins for -msse.  */
1941   if (TARGET_SSE)
1942     {
1943       target_flags |= MASK_MMX & ~target_flags_explicit;
1944       x86_prefetch_sse = true;
1945     }
1946
1947   /* Turn on MMX builtins for 3Dnow.  */
1948   if (TARGET_3DNOW)
1949     target_flags |= MASK_MMX;
1950
1951   if (TARGET_64BIT)
1952     {
1953       if (TARGET_ALIGN_DOUBLE)
1954         error ("-malign-double makes no sense in the 64bit mode");
1955       if (TARGET_RTD)
1956         error ("-mrtd calling convention not supported in the 64bit mode");
1957
1958       /* Enable by default the SSE and MMX builtins.  Do allow the user to
1959          explicitly disable any of these.  In particular, disabling SSE and
1960          MMX for kernel code is extremely useful.  */
1961       target_flags
1962         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
1963             & ~target_flags_explicit);
1964      }
1965   else
1966     {
1967       /* i386 ABI does not specify red zone.  It still makes sense to use it
1968          when programmer takes care to stack from being destroyed.  */
1969       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1970         target_flags |= MASK_NO_RED_ZONE;
1971     }
1972
1973   /* Validate -mpreferred-stack-boundary= value, or provide default.
1974      The default of 128 bits is for Pentium III's SSE __m128.  We can't
1975      change it because of optimize_size.  Otherwise, we can't mix object
1976      files compiled with -Os and -On.  */
1977   ix86_preferred_stack_boundary = 128;
1978   if (ix86_preferred_stack_boundary_string)
1979     {
1980       i = atoi (ix86_preferred_stack_boundary_string);
1981       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1982         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1983                TARGET_64BIT ? 4 : 2);
1984       else
1985         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1986     }
1987
1988   /* Accept -msseregparm only if at least SSE support is enabled.  */
1989   if (TARGET_SSEREGPARM
1990       && ! TARGET_SSE)
1991     error ("-msseregparm used without SSE enabled");
1992
1993   ix86_fpmath = TARGET_FPMATH_DEFAULT;
1994
1995   if (ix86_fpmath_string != 0)
1996     {
1997       if (! strcmp (ix86_fpmath_string, "387"))
1998         ix86_fpmath = FPMATH_387;
1999       else if (! strcmp (ix86_fpmath_string, "sse"))
2000         {
2001           if (!TARGET_SSE)
2002             {
2003               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2004               ix86_fpmath = FPMATH_387;
2005             }
2006           else
2007             ix86_fpmath = FPMATH_SSE;
2008         }
2009       else if (! strcmp (ix86_fpmath_string, "387,sse")
2010                || ! strcmp (ix86_fpmath_string, "sse,387"))
2011         {
2012           if (!TARGET_SSE)
2013             {
2014               warning (0, "SSE instruction set disabled, using 387 arithmetics");
2015               ix86_fpmath = FPMATH_387;
2016             }
2017           else if (!TARGET_80387)
2018             {
2019               warning (0, "387 instruction set disabled, using SSE arithmetics");
2020               ix86_fpmath = FPMATH_SSE;
2021             }
2022           else
2023             ix86_fpmath = FPMATH_SSE | FPMATH_387;
2024         }
2025       else
2026         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
2027     }
2028
2029   /* If the i387 is disabled, then do not return values in it. */
2030   if (!TARGET_80387)
2031     target_flags &= ~MASK_FLOAT_RETURNS;
2032
2033   if ((x86_accumulate_outgoing_args & TUNEMASK)
2034       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2035       && !optimize_size)
2036     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2037
2038   /* ??? Unwind info is not correct around the CFG unless either a frame
2039      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
2040      unwind info generation to be aware of the CFG and propagating states
2041      around edges.  */
2042   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
2043        || flag_exceptions || flag_non_call_exceptions)
2044       && flag_omit_frame_pointer
2045       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
2046     {
2047       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
2048         warning (0, "unwind tables currently require either a frame pointer "
2049                  "or -maccumulate-outgoing-args for correctness");
2050       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
2051     }
2052
2053   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
2054   {
2055     char *p;
2056     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
2057     p = strchr (internal_label_prefix, 'X');
2058     internal_label_prefix_len = p - internal_label_prefix;
2059     *p = '\0';
2060   }
2061
2062   /* When scheduling description is not available, disable scheduler pass
2063      so it won't slow down the compilation and make x87 code slower.  */
2064   if (!TARGET_SCHEDULE)
2065     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
2066 }
2067 \f
2068 /* switch to the appropriate section for output of DECL.
2069    DECL is either a `VAR_DECL' node or a constant of some sort.
2070    RELOC indicates whether forming the initial value of DECL requires
2071    link-time relocations.  */
2072
2073 static section *
2074 x86_64_elf_select_section (tree decl, int reloc,
2075                            unsigned HOST_WIDE_INT align)
2076 {
2077   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2078       && ix86_in_large_data_p (decl))
2079     {
2080       const char *sname = NULL;
2081       unsigned int flags = SECTION_WRITE;
2082       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2083         {
2084         case SECCAT_DATA:
2085           sname = ".ldata";
2086           break;
2087         case SECCAT_DATA_REL:
2088           sname = ".ldata.rel";
2089           break;
2090         case SECCAT_DATA_REL_LOCAL:
2091           sname = ".ldata.rel.local";
2092           break;
2093         case SECCAT_DATA_REL_RO:
2094           sname = ".ldata.rel.ro";
2095           break;
2096         case SECCAT_DATA_REL_RO_LOCAL:
2097           sname = ".ldata.rel.ro.local";
2098           break;
2099         case SECCAT_BSS:
2100           sname = ".lbss";
2101           flags |= SECTION_BSS;
2102           break;
2103         case SECCAT_RODATA:
2104         case SECCAT_RODATA_MERGE_STR:
2105         case SECCAT_RODATA_MERGE_STR_INIT:
2106         case SECCAT_RODATA_MERGE_CONST:
2107           sname = ".lrodata";
2108           flags = 0;
2109           break;
2110         case SECCAT_SRODATA:
2111         case SECCAT_SDATA:
2112         case SECCAT_SBSS:
2113           gcc_unreachable ();
2114         case SECCAT_TEXT:
2115         case SECCAT_TDATA:
2116         case SECCAT_TBSS:
2117           /* We don't split these for medium model.  Place them into
2118              default sections and hope for best.  */
2119           break;
2120         }
2121       if (sname)
2122         {
2123           /* We might get called with string constants, but get_named_section
2124              doesn't like them as they are not DECLs.  Also, we need to set
2125              flags in that case.  */
2126           if (!DECL_P (decl))
2127             return get_section (sname, flags, NULL);
2128           return get_named_section (decl, sname, reloc);
2129         }
2130     }
2131   return default_elf_select_section (decl, reloc, align);
2132 }
2133
2134 /* Build up a unique section name, expressed as a
2135    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2136    RELOC indicates whether the initial value of EXP requires
2137    link-time relocations.  */
2138
2139 static void
2140 x86_64_elf_unique_section (tree decl, int reloc)
2141 {
2142   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2143       && ix86_in_large_data_p (decl))
2144     {
2145       const char *prefix = NULL;
2146       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2147       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2148
2149       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2150         {
2151         case SECCAT_DATA:
2152         case SECCAT_DATA_REL:
2153         case SECCAT_DATA_REL_LOCAL:
2154         case SECCAT_DATA_REL_RO:
2155         case SECCAT_DATA_REL_RO_LOCAL:
2156           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2157           break;
2158         case SECCAT_BSS:
2159           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2160           break;
2161         case SECCAT_RODATA:
2162         case SECCAT_RODATA_MERGE_STR:
2163         case SECCAT_RODATA_MERGE_STR_INIT:
2164         case SECCAT_RODATA_MERGE_CONST:
2165           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2166           break;
2167         case SECCAT_SRODATA:
2168         case SECCAT_SDATA:
2169         case SECCAT_SBSS:
2170           gcc_unreachable ();
2171         case SECCAT_TEXT:
2172         case SECCAT_TDATA:
2173         case SECCAT_TBSS:
2174           /* We don't split these for medium model.  Place them into
2175              default sections and hope for best.  */
2176           break;
2177         }
2178       if (prefix)
2179         {
2180           const char *name;
2181           size_t nlen, plen;
2182           char *string;
2183           plen = strlen (prefix);
2184
2185           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2186           name = targetm.strip_name_encoding (name);
2187           nlen = strlen (name);
2188
2189           string = alloca (nlen + plen + 1);
2190           memcpy (string, prefix, plen);
2191           memcpy (string + plen, name, nlen + 1);
2192
2193           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2194           return;
2195         }
2196     }
2197   default_unique_section (decl, reloc);
2198 }
2199
2200 #ifdef COMMON_ASM_OP
2201 /* This says how to output assembler code to declare an
2202    uninitialized external linkage data object.
2203
2204    For medium model x86-64 we need to use .largecomm opcode for
2205    large objects.  */
2206 void
2207 x86_elf_aligned_common (FILE *file,
2208                         const char *name, unsigned HOST_WIDE_INT size,
2209                         int align)
2210 {
2211   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2212       && size > (unsigned int)ix86_section_threshold)
2213     fprintf (file, ".largecomm\t");
2214   else
2215     fprintf (file, "%s", COMMON_ASM_OP);
2216   assemble_name (file, name);
2217   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2218            size, align / BITS_PER_UNIT);
2219 }
2220
2221 /* Utility function for targets to use in implementing
2222    ASM_OUTPUT_ALIGNED_BSS.  */
2223
2224 void
2225 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2226                         const char *name, unsigned HOST_WIDE_INT size,
2227                         int align)
2228 {
2229   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2230       && size > (unsigned int)ix86_section_threshold)
2231     switch_to_section (get_named_section (decl, ".lbss", 0));
2232   else
2233     switch_to_section (bss_section);
2234   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2235 #ifdef ASM_DECLARE_OBJECT_NAME
2236   last_assemble_variable_decl = decl;
2237   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2238 #else
2239   /* Standard thing is just output label for the object.  */
2240   ASM_OUTPUT_LABEL (file, name);
2241 #endif /* ASM_DECLARE_OBJECT_NAME */
2242   ASM_OUTPUT_SKIP (file, size ? size : 1);
2243 }
2244 #endif
2245 \f
2246 void
2247 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2248 {
2249   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2250      make the problem with not enough registers even worse.  */
2251 #ifdef INSN_SCHEDULING
2252   if (level > 1)
2253     flag_schedule_insns = 0;
2254 #endif
2255
2256   if (TARGET_MACHO)
2257     /* The Darwin libraries never set errno, so we might as well
2258        avoid calling them when that's the only reason we would.  */
2259     flag_errno_math = 0;
2260
2261   /* The default values of these switches depend on the TARGET_64BIT
2262      that is not known at this moment.  Mark these values with 2 and
2263      let user the to override these.  In case there is no command line option
2264      specifying them, we will set the defaults in override_options.  */
2265   if (optimize >= 1)
2266     flag_omit_frame_pointer = 2;
2267   flag_pcc_struct_return = 2;
2268   flag_asynchronous_unwind_tables = 2;
2269 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2270   SUBTARGET_OPTIMIZATION_OPTIONS;
2271 #endif
2272 }
2273 \f
2274 /* Table of valid machine attributes.  */
2275 const struct attribute_spec ix86_attribute_table[] =
2276 {
2277   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2278   /* Stdcall attribute says callee is responsible for popping arguments
2279      if they are not variable.  */
2280   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2281   /* Fastcall attribute says callee is responsible for popping arguments
2282      if they are not variable.  */
2283   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2284   /* Cdecl attribute says the callee is a normal C declaration */
2285   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2286   /* Regparm attribute specifies how many integer arguments are to be
2287      passed in registers.  */
2288   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
2289   /* Sseregparm attribute says we are using x86_64 calling conventions
2290      for FP arguments.  */
2291   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
2292   /* force_align_arg_pointer says this function realigns the stack at entry.  */
2293   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
2294     false, true,  true, ix86_handle_cconv_attribute },
2295 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2296   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2297   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2298   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
2299 #endif
2300   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2301   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2302 #ifdef SUBTARGET_ATTRIBUTE_TABLE
2303   SUBTARGET_ATTRIBUTE_TABLE,
2304 #endif
2305   { NULL,        0, 0, false, false, false, NULL }
2306 };
2307
2308 /* Decide whether we can make a sibling call to a function.  DECL is the
2309    declaration of the function being targeted by the call and EXP is the
2310    CALL_EXPR representing the call.  */
2311
2312 static bool
2313 ix86_function_ok_for_sibcall (tree decl, tree exp)
2314 {
2315   tree func;
2316   rtx a, b;
2317
2318   /* If we are generating position-independent code, we cannot sibcall
2319      optimize any indirect call, or a direct call to a global function,
2320      as the PLT requires %ebx be live.  */
2321   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2322     return false;
2323
2324   if (decl)
2325     func = decl;
2326   else
2327     {
2328       func = TREE_TYPE (TREE_OPERAND (exp, 0));
2329       if (POINTER_TYPE_P (func))
2330         func = TREE_TYPE (func);
2331     }
2332
2333   /* Check that the return value locations are the same.  Like
2334      if we are returning floats on the 80387 register stack, we cannot
2335      make a sibcall from a function that doesn't return a float to a
2336      function that does or, conversely, from a function that does return
2337      a float to a function that doesn't; the necessary stack adjustment
2338      would not be executed.  This is also the place we notice
2339      differences in the return value ABI.  Note that it is ok for one
2340      of the functions to have void return type as long as the return
2341      value of the other is passed in a register.  */
2342   a = ix86_function_value (TREE_TYPE (exp), func, false);
2343   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2344                            cfun->decl, false);
2345   if (STACK_REG_P (a) || STACK_REG_P (b))
2346     {
2347       if (!rtx_equal_p (a, b))
2348         return false;
2349     }
2350   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2351     ;
2352   else if (!rtx_equal_p (a, b))
2353     return false;
2354
2355   /* If this call is indirect, we'll need to be able to use a call-clobbered
2356      register for the address of the target function.  Make sure that all
2357      such registers are not used for passing parameters.  */
2358   if (!decl && !TARGET_64BIT)
2359     {
2360       tree type;
2361
2362       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2363       type = TREE_OPERAND (exp, 0);             /* pointer expression */
2364       type = TREE_TYPE (type);                  /* pointer type */
2365       type = TREE_TYPE (type);                  /* function type */
2366
2367       if (ix86_function_regparm (type, NULL) >= 3)
2368         {
2369           /* ??? Need to count the actual number of registers to be used,
2370              not the possible number of registers.  Fix later.  */
2371           return false;
2372         }
2373     }
2374
2375 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2376   /* Dllimport'd functions are also called indirectly.  */
2377   if (decl && DECL_DLLIMPORT_P (decl)
2378       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2379     return false;
2380 #endif
2381
2382   /* If we forced aligned the stack, then sibcalling would unalign the
2383      stack, which may break the called function.  */
2384   if (cfun->machine->force_align_arg_pointer)
2385     return false;
2386
2387   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2388   return true;
2389 }
2390
2391 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2392    calling convention attributes;
2393    arguments as in struct attribute_spec.handler.  */
2394
2395 static tree
2396 ix86_handle_cconv_attribute (tree *node, tree name,
2397                                    tree args,
2398                                    int flags ATTRIBUTE_UNUSED,
2399                                    bool *no_add_attrs)
2400 {
2401   if (TREE_CODE (*node) != FUNCTION_TYPE
2402       && TREE_CODE (*node) != METHOD_TYPE
2403       && TREE_CODE (*node) != FIELD_DECL
2404       && TREE_CODE (*node) != TYPE_DECL)
2405     {
2406       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2407                IDENTIFIER_POINTER (name));
2408       *no_add_attrs = true;
2409       return NULL_TREE;
2410     }
2411
2412   /* Can combine regparm with all attributes but fastcall.  */
2413   if (is_attribute_p ("regparm", name))
2414     {
2415       tree cst;
2416
2417       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2418         {
2419           error ("fastcall and regparm attributes are not compatible");
2420         }
2421
2422       cst = TREE_VALUE (args);
2423       if (TREE_CODE (cst) != INTEGER_CST)
2424         {
2425           warning (OPT_Wattributes,
2426                    "%qs attribute requires an integer constant argument",
2427                    IDENTIFIER_POINTER (name));
2428           *no_add_attrs = true;
2429         }
2430       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2431         {
2432           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2433                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2434           *no_add_attrs = true;
2435         }
2436
2437       if (!TARGET_64BIT
2438           && lookup_attribute (ix86_force_align_arg_pointer_string,
2439                                TYPE_ATTRIBUTES (*node))
2440           && compare_tree_int (cst, REGPARM_MAX-1))
2441         {
2442           error ("%s functions limited to %d register parameters",
2443                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2444         }
2445
2446       return NULL_TREE;
2447     }
2448
2449   if (TARGET_64BIT)
2450     {
2451       warning (OPT_Wattributes, "%qs attribute ignored",
2452                IDENTIFIER_POINTER (name));
2453       *no_add_attrs = true;
2454       return NULL_TREE;
2455     }
2456
2457   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2458   if (is_attribute_p ("fastcall", name))
2459     {
2460       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2461         {
2462           error ("fastcall and cdecl attributes are not compatible");
2463         }
2464       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2465         {
2466           error ("fastcall and stdcall attributes are not compatible");
2467         }
2468       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2469         {
2470           error ("fastcall and regparm attributes are not compatible");
2471         }
2472     }
2473
2474   /* Can combine stdcall with fastcall (redundant), regparm and
2475      sseregparm.  */
2476   else if (is_attribute_p ("stdcall", name))
2477     {
2478       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2479         {
2480           error ("stdcall and cdecl attributes are not compatible");
2481         }
2482       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2483         {
2484           error ("stdcall and fastcall attributes are not compatible");
2485         }
2486     }
2487
2488   /* Can combine cdecl with regparm and sseregparm.  */
2489   else if (is_attribute_p ("cdecl", name))
2490     {
2491       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2492         {
2493           error ("stdcall and cdecl attributes are not compatible");
2494         }
2495       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2496         {
2497           error ("fastcall and cdecl attributes are not compatible");
2498         }
2499     }
2500
2501   /* Can combine sseregparm with all attributes.  */
2502
2503   return NULL_TREE;
2504 }
2505
2506 /* Return 0 if the attributes for two types are incompatible, 1 if they
2507    are compatible, and 2 if they are nearly compatible (which causes a
2508    warning to be generated).  */
2509
2510 static int
2511 ix86_comp_type_attributes (tree type1, tree type2)
2512 {
2513   /* Check for mismatch of non-default calling convention.  */
2514   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2515
2516   if (TREE_CODE (type1) != FUNCTION_TYPE)
2517     return 1;
2518
2519   /* Check for mismatched fastcall/regparm types.  */
2520   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2521        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2522       || (ix86_function_regparm (type1, NULL)
2523           != ix86_function_regparm (type2, NULL)))
2524     return 0;
2525
2526   /* Check for mismatched sseregparm types.  */
2527   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2528       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2529     return 0;
2530
2531   /* Check for mismatched return types (cdecl vs stdcall).  */
2532   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2533       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2534     return 0;
2535
2536   return 1;
2537 }
2538 \f
2539 /* Return the regparm value for a function with the indicated TYPE and DECL.
2540    DECL may be NULL when calling function indirectly
2541    or considering a libcall.  */
2542
2543 static int
2544 ix86_function_regparm (tree type, tree decl)
2545 {
2546   tree attr;
2547   int regparm = ix86_regparm;
2548   bool user_convention = false;
2549
2550   if (!TARGET_64BIT)
2551     {
2552       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2553       if (attr)
2554         {
2555           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2556           user_convention = true;
2557         }
2558
2559       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2560         {
2561           regparm = 2;
2562           user_convention = true;
2563         }
2564
2565       /* Use register calling convention for local functions when possible.  */
2566       if (!TARGET_64BIT && !user_convention && decl
2567           && flag_unit_at_a_time && !profile_flag)
2568         {
2569           struct cgraph_local_info *i = cgraph_local_info (decl);
2570           if (i && i->local)
2571             {
2572               int local_regparm, globals = 0, regno;
2573
2574               /* Make sure no regparm register is taken by a global register
2575                  variable.  */
2576               for (local_regparm = 0; local_regparm < 3; local_regparm++)
2577                 if (global_regs[local_regparm])
2578                   break;
2579               /* We can't use regparm(3) for nested functions as these use
2580                  static chain pointer in third argument.  */
2581               if (local_regparm == 3
2582                   && decl_function_context (decl)
2583                   && !DECL_NO_STATIC_CHAIN (decl))
2584                 local_regparm = 2;
2585               /* If the function realigns its stackpointer, the
2586                  prologue will clobber %ecx.  If we've already
2587                  generated code for the callee, the callee
2588                  DECL_STRUCT_FUNCTION is gone, so we fall back to
2589                  scanning the attributes for the self-realigning
2590                  property.  */
2591               if ((DECL_STRUCT_FUNCTION (decl)
2592                    && DECL_STRUCT_FUNCTION (decl)->machine->force_align_arg_pointer)
2593                   || (!DECL_STRUCT_FUNCTION (decl)
2594                       && lookup_attribute (ix86_force_align_arg_pointer_string,
2595                                            TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2596                 local_regparm = 2;
2597               /* Each global register variable increases register preassure,
2598                  so the more global reg vars there are, the smaller regparm
2599                  optimization use, unless requested by the user explicitly.  */
2600               for (regno = 0; regno < 6; regno++)
2601                 if (global_regs[regno])
2602                   globals++;
2603               local_regparm
2604                 = globals < local_regparm ? local_regparm - globals : 0;
2605
2606               if (local_regparm > regparm)
2607                 regparm = local_regparm;
2608             }
2609         }
2610     }
2611   return regparm;
2612 }
2613
2614 /* Return 1 or 2, if we can pass up to 8 SFmode (1) and DFmode (2) arguments
2615    in SSE registers for a function with the indicated TYPE and DECL.
2616    DECL may be NULL when calling function indirectly
2617    or considering a libcall.  Otherwise return 0.  */
2618
2619 static int
2620 ix86_function_sseregparm (tree type, tree decl)
2621 {
2622   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2623      by the sseregparm attribute.  */
2624   if (TARGET_SSEREGPARM
2625       || (type
2626           && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2627     {
2628       if (!TARGET_SSE)
2629         {
2630           if (decl)
2631             error ("Calling %qD with attribute sseregparm without "
2632                    "SSE/SSE2 enabled", decl);
2633           else
2634             error ("Calling %qT with attribute sseregparm without "
2635                    "SSE/SSE2 enabled", type);
2636           return 0;
2637         }
2638
2639       return 2;
2640     }
2641
2642   /* For local functions, pass SFmode (and DFmode for SSE2) arguments
2643      in SSE registers even for 32-bit mode and not just 3, but up to
2644      8 SSE arguments in registers.  */
2645   if (!TARGET_64BIT && decl
2646       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2647     {
2648       struct cgraph_local_info *i = cgraph_local_info (decl);
2649       if (i && i->local)
2650         return TARGET_SSE2 ? 2 : 1;
2651     }
2652
2653   return 0;
2654 }
2655
2656 /* Return true if EAX is live at the start of the function.  Used by
2657    ix86_expand_prologue to determine if we need special help before
2658    calling allocate_stack_worker.  */
2659
2660 static bool
2661 ix86_eax_live_at_start_p (void)
2662 {
2663   /* Cheat.  Don't bother working forward from ix86_function_regparm
2664      to the function type to whether an actual argument is located in
2665      eax.  Instead just look at cfg info, which is still close enough
2666      to correct at this point.  This gives false positives for broken
2667      functions that might use uninitialized data that happens to be
2668      allocated in eax, but who cares?  */
2669   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2670 }
2671
2672 /* Value is the number of bytes of arguments automatically
2673    popped when returning from a subroutine call.
2674    FUNDECL is the declaration node of the function (as a tree),
2675    FUNTYPE is the data type of the function (as a tree),
2676    or for a library call it is an identifier node for the subroutine name.
2677    SIZE is the number of bytes of arguments passed on the stack.
2678
2679    On the 80386, the RTD insn may be used to pop them if the number
2680      of args is fixed, but if the number is variable then the caller
2681      must pop them all.  RTD can't be used for library calls now
2682      because the library is compiled with the Unix compiler.
2683    Use of RTD is a selectable option, since it is incompatible with
2684    standard Unix calling sequences.  If the option is not selected,
2685    the caller must always pop the args.
2686
2687    The attribute stdcall is equivalent to RTD on a per module basis.  */
2688
2689 int
2690 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2691 {
2692   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2693
2694   /* Cdecl functions override -mrtd, and never pop the stack.  */
2695   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2696
2697     /* Stdcall and fastcall functions will pop the stack if not
2698        variable args.  */
2699     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2700         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2701       rtd = 1;
2702
2703     if (rtd
2704         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2705             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2706                 == void_type_node)))
2707       return size;
2708   }
2709
2710   /* Lose any fake structure return argument if it is passed on the stack.  */
2711   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2712       && !TARGET_64BIT
2713       && !KEEP_AGGREGATE_RETURN_POINTER)
2714     {
2715       int nregs = ix86_function_regparm (funtype, fundecl);
2716
2717       if (!nregs)
2718         return GET_MODE_SIZE (Pmode);
2719     }
2720
2721   return 0;
2722 }
2723 \f
2724 /* Argument support functions.  */
2725
2726 /* Return true when register may be used to pass function parameters.  */
2727 bool
2728 ix86_function_arg_regno_p (int regno)
2729 {
2730   int i;
2731   if (!TARGET_64BIT)
2732     return (regno < REGPARM_MAX
2733             || (TARGET_MMX && MMX_REGNO_P (regno)
2734                 && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2735             || (TARGET_SSE && SSE_REGNO_P (regno)
2736                 && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2737
2738   if (TARGET_SSE && SSE_REGNO_P (regno)
2739       && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2740     return true;
2741   /* RAX is used as hidden argument to va_arg functions.  */
2742   if (!regno)
2743     return true;
2744   for (i = 0; i < REGPARM_MAX; i++)
2745     if (regno == x86_64_int_parameter_registers[i])
2746       return true;
2747   return false;
2748 }
2749
2750 /* Return if we do not know how to pass TYPE solely in registers.  */
2751
2752 static bool
2753 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2754 {
2755   if (must_pass_in_stack_var_size_or_pad (mode, type))
2756     return true;
2757
2758   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2759      The layout_type routine is crafty and tries to trick us into passing
2760      currently unsupported vector types on the stack by using TImode.  */
2761   return (!TARGET_64BIT && mode == TImode
2762           && type && TREE_CODE (type) != VECTOR_TYPE);
2763 }
2764
2765 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2766    for a call to a function whose data type is FNTYPE.
2767    For a library call, FNTYPE is 0.  */
2768
2769 void
2770 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2771                       tree fntype,      /* tree ptr for function decl */
2772                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2773                       tree fndecl)
2774 {
2775   static CUMULATIVE_ARGS zero_cum;
2776   tree param, next_param;
2777
2778   if (TARGET_DEBUG_ARG)
2779     {
2780       fprintf (stderr, "\ninit_cumulative_args (");
2781       if (fntype)
2782         fprintf (stderr, "fntype code = %s, ret code = %s",
2783                  tree_code_name[(int) TREE_CODE (fntype)],
2784                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
2785       else
2786         fprintf (stderr, "no fntype");
2787
2788       if (libname)
2789         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
2790     }
2791
2792   *cum = zero_cum;
2793
2794   /* Set up the number of registers to use for passing arguments.  */
2795   cum->nregs = ix86_regparm;
2796   if (TARGET_SSE)
2797     cum->sse_nregs = SSE_REGPARM_MAX;
2798   if (TARGET_MMX)
2799     cum->mmx_nregs = MMX_REGPARM_MAX;
2800   cum->warn_sse = true;
2801   cum->warn_mmx = true;
2802   cum->maybe_vaarg = false;
2803
2804   /* Use ecx and edx registers if function has fastcall attribute,
2805      else look for regparm information.  */
2806   if (fntype && !TARGET_64BIT)
2807     {
2808       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
2809         {
2810           cum->nregs = 2;
2811           cum->fastcall = 1;
2812         }
2813       else
2814         cum->nregs = ix86_function_regparm (fntype, fndecl);
2815     }
2816
2817   /* Set up the number of SSE registers used for passing SFmode
2818      and DFmode arguments.  Warn for mismatching ABI.  */
2819   cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
2820
2821   /* Determine if this function has variable arguments.  This is
2822      indicated by the last argument being 'void_type_mode' if there
2823      are no variable arguments.  If there are variable arguments, then
2824      we won't pass anything in registers in 32-bit mode. */
2825
2826   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
2827     {
2828       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
2829            param != 0; param = next_param)
2830         {
2831           next_param = TREE_CHAIN (param);
2832           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
2833             {
2834               if (!TARGET_64BIT)
2835                 {
2836                   cum->nregs = 0;
2837                   cum->sse_nregs = 0;
2838                   cum->mmx_nregs = 0;
2839                   cum->warn_sse = 0;
2840                   cum->warn_mmx = 0;
2841                   cum->fastcall = 0;
2842                   cum->float_in_sse = 0;
2843                 }
2844               cum->maybe_vaarg = true;
2845             }
2846         }
2847     }
2848   if ((!fntype && !libname)
2849       || (fntype && !TYPE_ARG_TYPES (fntype)))
2850     cum->maybe_vaarg = true;
2851
2852   if (TARGET_DEBUG_ARG)
2853     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2854
2855   return;
2856 }
2857
2858 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
2859    But in the case of vector types, it is some vector mode.
2860
2861    When we have only some of our vector isa extensions enabled, then there
2862    are some modes for which vector_mode_supported_p is false.  For these
2863    modes, the generic vector support in gcc will choose some non-vector mode
2864    in order to implement the type.  By computing the natural mode, we'll
2865    select the proper ABI location for the operand and not depend on whatever
2866    the middle-end decides to do with these vector types.  */
2867
2868 static enum machine_mode
2869 type_natural_mode (tree type)
2870 {
2871   enum machine_mode mode = TYPE_MODE (type);
2872
2873   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
2874     {
2875       HOST_WIDE_INT size = int_size_in_bytes (type);
2876       if ((size == 8 || size == 16)
2877           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
2878           && TYPE_VECTOR_SUBPARTS (type) > 1)
2879         {
2880           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2881
2882           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
2883             mode = MIN_MODE_VECTOR_FLOAT;
2884           else
2885             mode = MIN_MODE_VECTOR_INT;
2886
2887           /* Get the mode which has this inner mode and number of units.  */
2888           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2889             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
2890                 && GET_MODE_INNER (mode) == innermode)
2891               return mode;
2892
2893           gcc_unreachable ();
2894         }
2895     }
2896
2897   return mode;
2898 }
2899
2900 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
2901    this may not agree with the mode that the type system has chosen for the
2902    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
2903    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
2904
2905 static rtx
2906 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
2907                      unsigned int regno)
2908 {
2909   rtx tmp;
2910
2911   if (orig_mode != BLKmode)
2912     tmp = gen_rtx_REG (orig_mode, regno);
2913   else
2914     {
2915       tmp = gen_rtx_REG (mode, regno);
2916       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
2917       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
2918     }
2919
2920   return tmp;
2921 }
2922
2923 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2924    of this code is to classify each 8bytes of incoming argument by the register
2925    class and assign registers accordingly.  */
2926
2927 /* Return the union class of CLASS1 and CLASS2.
2928    See the x86-64 PS ABI for details.  */
2929
2930 static enum x86_64_reg_class
2931 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2932 {
2933   /* Rule #1: If both classes are equal, this is the resulting class.  */
2934   if (class1 == class2)
2935     return class1;
2936
2937   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2938      the other class.  */
2939   if (class1 == X86_64_NO_CLASS)
2940     return class2;
2941   if (class2 == X86_64_NO_CLASS)
2942     return class1;
2943
2944   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2945   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2946     return X86_64_MEMORY_CLASS;
2947
2948   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2949   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2950       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2951     return X86_64_INTEGERSI_CLASS;
2952   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2953       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2954     return X86_64_INTEGER_CLASS;
2955
2956   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
2957      MEMORY is used.  */
2958   if (class1 == X86_64_X87_CLASS
2959       || class1 == X86_64_X87UP_CLASS
2960       || class1 == X86_64_COMPLEX_X87_CLASS
2961       || class2 == X86_64_X87_CLASS
2962       || class2 == X86_64_X87UP_CLASS
2963       || class2 == X86_64_COMPLEX_X87_CLASS)
2964     return X86_64_MEMORY_CLASS;
2965
2966   /* Rule #6: Otherwise class SSE is used.  */
2967   return X86_64_SSE_CLASS;
2968 }
2969
2970 /* Classify the argument of type TYPE and mode MODE.
2971    CLASSES will be filled by the register class used to pass each word
2972    of the operand.  The number of words is returned.  In case the parameter
2973    should be passed in memory, 0 is returned. As a special case for zero
2974    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2975
2976    BIT_OFFSET is used internally for handling records and specifies offset
2977    of the offset in bits modulo 256 to avoid overflow cases.
2978
2979    See the x86-64 PS ABI for details.
2980 */
2981
2982 static int
2983 classify_argument (enum machine_mode mode, tree type,
2984                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2985 {
2986   HOST_WIDE_INT bytes =
2987     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2988   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2989
2990   /* Variable sized entities are always passed/returned in memory.  */
2991   if (bytes < 0)
2992     return 0;
2993
2994   if (mode != VOIDmode
2995       && targetm.calls.must_pass_in_stack (mode, type))
2996     return 0;
2997
2998   if (type && AGGREGATE_TYPE_P (type))
2999     {
3000       int i;
3001       tree field;
3002       enum x86_64_reg_class subclasses[MAX_CLASSES];
3003
3004       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
3005       if (bytes > 16)
3006         return 0;
3007
3008       for (i = 0; i < words; i++)
3009         classes[i] = X86_64_NO_CLASS;
3010
3011       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
3012          signalize memory class, so handle it as special case.  */
3013       if (!words)
3014         {
3015           classes[0] = X86_64_NO_CLASS;
3016           return 1;
3017         }
3018
3019       /* Classify each field of record and merge classes.  */
3020       switch (TREE_CODE (type))
3021         {
3022         case RECORD_TYPE:
3023           /* And now merge the fields of structure.  */
3024           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3025             {
3026               if (TREE_CODE (field) == FIELD_DECL)
3027                 {
3028                   int num;
3029
3030                   if (TREE_TYPE (field) == error_mark_node)
3031                     continue;
3032
3033                   /* Bitfields are always classified as integer.  Handle them
3034                      early, since later code would consider them to be
3035                      misaligned integers.  */
3036                   if (DECL_BIT_FIELD (field))
3037                     {
3038                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3039                            i < ((int_bit_position (field) + (bit_offset % 64))
3040                                 + tree_low_cst (DECL_SIZE (field), 0)
3041                                 + 63) / 8 / 8; i++)
3042                         classes[i] =
3043                           merge_classes (X86_64_INTEGER_CLASS,
3044                                          classes[i]);
3045                     }
3046                   else
3047                     {
3048                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3049                                                TREE_TYPE (field), subclasses,
3050                                                (int_bit_position (field)
3051                                                 + bit_offset) % 256);
3052                       if (!num)
3053                         return 0;
3054                       for (i = 0; i < num; i++)
3055                         {
3056                           int pos =
3057                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3058                           classes[i + pos] =
3059                             merge_classes (subclasses[i], classes[i + pos]);
3060                         }
3061                     }
3062                 }
3063             }
3064           break;
3065
3066         case ARRAY_TYPE:
3067           /* Arrays are handled as small records.  */
3068           {
3069             int num;
3070             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3071                                      TREE_TYPE (type), subclasses, bit_offset);
3072             if (!num)
3073               return 0;
3074
3075             /* The partial classes are now full classes.  */
3076             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3077               subclasses[0] = X86_64_SSE_CLASS;
3078             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3079               subclasses[0] = X86_64_INTEGER_CLASS;
3080
3081             for (i = 0; i < words; i++)
3082               classes[i] = subclasses[i % num];
3083
3084             break;
3085           }
3086         case UNION_TYPE:
3087         case QUAL_UNION_TYPE:
3088           /* Unions are similar to RECORD_TYPE but offset is always 0.
3089              */
3090           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3091             {
3092               if (TREE_CODE (field) == FIELD_DECL)
3093                 {
3094                   int num;
3095
3096                   if (TREE_TYPE (field) == error_mark_node)
3097                     continue;
3098
3099                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3100                                            TREE_TYPE (field), subclasses,
3101                                            bit_offset);
3102                   if (!num)
3103                     return 0;
3104                   for (i = 0; i < num; i++)
3105                     classes[i] = merge_classes (subclasses[i], classes[i]);
3106                 }
3107             }
3108           break;
3109
3110         default:
3111           gcc_unreachable ();
3112         }
3113
3114       /* Final merger cleanup.  */
3115       for (i = 0; i < words; i++)
3116         {
3117           /* If one class is MEMORY, everything should be passed in
3118              memory.  */
3119           if (classes[i] == X86_64_MEMORY_CLASS)
3120             return 0;
3121
3122           /* The X86_64_SSEUP_CLASS should be always preceded by
3123              X86_64_SSE_CLASS.  */
3124           if (classes[i] == X86_64_SSEUP_CLASS
3125               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3126             classes[i] = X86_64_SSE_CLASS;
3127
3128           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3129           if (classes[i] == X86_64_X87UP_CLASS
3130               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3131             classes[i] = X86_64_SSE_CLASS;
3132         }
3133       return words;
3134     }
3135
3136   /* Compute alignment needed.  We align all types to natural boundaries with
3137      exception of XFmode that is aligned to 64bits.  */
3138   if (mode != VOIDmode && mode != BLKmode)
3139     {
3140       int mode_alignment = GET_MODE_BITSIZE (mode);
3141
3142       if (mode == XFmode)
3143         mode_alignment = 128;
3144       else if (mode == XCmode)
3145         mode_alignment = 256;
3146       if (COMPLEX_MODE_P (mode))
3147         mode_alignment /= 2;
3148       /* Misaligned fields are always returned in memory.  */
3149       if (bit_offset % mode_alignment)
3150         return 0;
3151     }
3152
3153   /* for V1xx modes, just use the base mode */
3154   if (VECTOR_MODE_P (mode)
3155       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3156     mode = GET_MODE_INNER (mode);
3157
3158   /* Classification of atomic types.  */
3159   switch (mode)
3160     {
3161     case SDmode:
3162     case DDmode:
3163       classes[0] = X86_64_SSE_CLASS;
3164       return 1;
3165     case TDmode:
3166       classes[0] = X86_64_SSE_CLASS;
3167       classes[1] = X86_64_SSEUP_CLASS;
3168       return 2;
3169     case DImode:
3170     case SImode:
3171     case HImode:
3172     case QImode:
3173     case CSImode:
3174     case CHImode:
3175     case CQImode:
3176       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3177         classes[0] = X86_64_INTEGERSI_CLASS;
3178       else
3179         classes[0] = X86_64_INTEGER_CLASS;
3180       return 1;
3181     case CDImode:
3182     case TImode:
3183       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3184       return 2;
3185     case CTImode:
3186       return 0;
3187     case SFmode:
3188       if (!(bit_offset % 64))
3189         classes[0] = X86_64_SSESF_CLASS;
3190       else
3191         classes[0] = X86_64_SSE_CLASS;
3192       return 1;
3193     case DFmode:
3194       classes[0] = X86_64_SSEDF_CLASS;
3195       return 1;
3196     case XFmode:
3197       classes[0] = X86_64_X87_CLASS;
3198       classes[1] = X86_64_X87UP_CLASS;
3199       return 2;
3200     case TFmode:
3201       classes[0] = X86_64_SSE_CLASS;
3202       classes[1] = X86_64_SSEUP_CLASS;
3203       return 2;
3204     case SCmode:
3205       classes[0] = X86_64_SSE_CLASS;
3206       return 1;
3207     case DCmode:
3208       classes[0] = X86_64_SSEDF_CLASS;
3209       classes[1] = X86_64_SSEDF_CLASS;
3210       return 2;
3211     case XCmode:
3212       classes[0] = X86_64_COMPLEX_X87_CLASS;
3213       return 1;
3214     case TCmode:
3215       /* This modes is larger than 16 bytes.  */
3216       return 0;
3217     case V4SFmode:
3218     case V4SImode:
3219     case V16QImode:
3220     case V8HImode:
3221     case V2DFmode:
3222     case V2DImode:
3223       classes[0] = X86_64_SSE_CLASS;
3224       classes[1] = X86_64_SSEUP_CLASS;
3225       return 2;
3226     case V2SFmode:
3227     case V2SImode:
3228     case V4HImode:
3229     case V8QImode:
3230       classes[0] = X86_64_SSE_CLASS;
3231       return 1;
3232     case BLKmode:
3233     case VOIDmode:
3234       return 0;
3235     default:
3236       gcc_assert (VECTOR_MODE_P (mode));
3237
3238       if (bytes > 16)
3239         return 0;
3240
3241       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3242
3243       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3244         classes[0] = X86_64_INTEGERSI_CLASS;
3245       else
3246         classes[0] = X86_64_INTEGER_CLASS;
3247       classes[1] = X86_64_INTEGER_CLASS;
3248       return 1 + (bytes > 8);
3249     }
3250 }
3251
3252 /* Examine the argument and return set number of register required in each
3253    class.  Return 0 iff parameter should be passed in memory.  */
3254 static int
3255 examine_argument (enum machine_mode mode, tree type, int in_return,
3256                   int *int_nregs, int *sse_nregs)
3257 {
3258   enum x86_64_reg_class class[MAX_CLASSES];
3259   int n = classify_argument (mode, type, class, 0);
3260
3261   *int_nregs = 0;
3262   *sse_nregs = 0;
3263   if (!n)
3264     return 0;
3265   for (n--; n >= 0; n--)
3266     switch (class[n])
3267       {
3268       case X86_64_INTEGER_CLASS:
3269       case X86_64_INTEGERSI_CLASS:
3270         (*int_nregs)++;
3271         break;
3272       case X86_64_SSE_CLASS:
3273       case X86_64_SSESF_CLASS:
3274       case X86_64_SSEDF_CLASS:
3275         (*sse_nregs)++;
3276         break;
3277       case X86_64_NO_CLASS:
3278       case X86_64_SSEUP_CLASS:
3279         break;
3280       case X86_64_X87_CLASS:
3281       case X86_64_X87UP_CLASS:
3282         if (!in_return)
3283           return 0;
3284         break;
3285       case X86_64_COMPLEX_X87_CLASS:
3286         return in_return ? 2 : 0;
3287       case X86_64_MEMORY_CLASS:
3288         gcc_unreachable ();
3289       }
3290   return 1;
3291 }
3292
3293 /* Construct container for the argument used by GCC interface.  See
3294    FUNCTION_ARG for the detailed description.  */
3295
3296 static rtx
3297 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3298                      tree type, int in_return, int nintregs, int nsseregs,
3299                      const int *intreg, int sse_regno)
3300 {
3301   /* The following variables hold the static issued_error state.  */
3302   static bool issued_sse_arg_error;
3303   static bool issued_sse_ret_error;
3304   static bool issued_x87_ret_error;
3305
3306   enum machine_mode tmpmode;
3307   int bytes =
3308     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3309   enum x86_64_reg_class class[MAX_CLASSES];
3310   int n;
3311   int i;
3312   int nexps = 0;
3313   int needed_sseregs, needed_intregs;
3314   rtx exp[MAX_CLASSES];
3315   rtx ret;
3316
3317   n = classify_argument (mode, type, class, 0);
3318   if (TARGET_DEBUG_ARG)
3319     {
3320       if (!n)
3321         fprintf (stderr, "Memory class\n");
3322       else
3323         {
3324           fprintf (stderr, "Classes:");
3325           for (i = 0; i < n; i++)
3326             {
3327               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
3328             }
3329            fprintf (stderr, "\n");
3330         }
3331     }
3332   if (!n)
3333     return NULL;
3334   if (!examine_argument (mode, type, in_return, &needed_intregs,
3335                          &needed_sseregs))
3336     return NULL;
3337   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3338     return NULL;
3339
3340   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3341      some less clueful developer tries to use floating-point anyway.  */
3342   if (needed_sseregs && !TARGET_SSE)
3343     {
3344       if (in_return)
3345         {
3346           if (!issued_sse_ret_error)
3347             {
3348               error ("SSE register return with SSE disabled");
3349               issued_sse_ret_error = true;
3350             }
3351         }
3352       else if (!issued_sse_arg_error)
3353         {
3354           error ("SSE register argument with SSE disabled");
3355           issued_sse_arg_error = true;
3356         }
3357       return NULL;
3358     }
3359
3360   /* Likewise, error if the ABI requires us to return values in the
3361      x87 registers and the user specified -mno-80387.  */
3362   if (!TARGET_80387 && in_return)
3363     for (i = 0; i < n; i++)
3364       if (class[i] == X86_64_X87_CLASS
3365           || class[i] == X86_64_X87UP_CLASS
3366           || class[i] == X86_64_COMPLEX_X87_CLASS)
3367         {
3368           if (!issued_x87_ret_error)
3369             {
3370               error ("x87 register return with x87 disabled");
3371               issued_x87_ret_error = true;
3372             }
3373           return NULL;
3374         }
3375
3376   /* First construct simple cases.  Avoid SCmode, since we want to use
3377      single register to pass this type.  */
3378   if (n == 1 && mode != SCmode)
3379     switch (class[0])
3380       {
3381       case X86_64_INTEGER_CLASS:
3382       case X86_64_INTEGERSI_CLASS:
3383         return gen_rtx_REG (mode, intreg[0]);
3384       case X86_64_SSE_CLASS:
3385       case X86_64_SSESF_CLASS:
3386       case X86_64_SSEDF_CLASS:
3387         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3388       case X86_64_X87_CLASS:
3389       case X86_64_COMPLEX_X87_CLASS:
3390         return gen_rtx_REG (mode, FIRST_STACK_REG);
3391       case X86_64_NO_CLASS:
3392         /* Zero sized array, struct or class.  */
3393         return NULL;
3394       default:
3395         gcc_unreachable ();
3396       }
3397   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3398       && mode != BLKmode)
3399     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3400   if (n == 2
3401       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3402     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3403   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3404       && class[1] == X86_64_INTEGER_CLASS
3405       && (mode == CDImode || mode == TImode || mode == TFmode)
3406       && intreg[0] + 1 == intreg[1])
3407     return gen_rtx_REG (mode, intreg[0]);
3408
3409   /* Otherwise figure out the entries of the PARALLEL.  */
3410   for (i = 0; i < n; i++)
3411     {
3412       switch (class[i])
3413         {
3414           case X86_64_NO_CLASS:
3415             break;
3416           case X86_64_INTEGER_CLASS:
3417           case X86_64_INTEGERSI_CLASS:
3418             /* Merge TImodes on aligned occasions here too.  */
3419             if (i * 8 + 8 > bytes)
3420               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3421             else if (class[i] == X86_64_INTEGERSI_CLASS)
3422               tmpmode = SImode;
3423             else
3424               tmpmode = DImode;
3425             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3426             if (tmpmode == BLKmode)
3427               tmpmode = DImode;
3428             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3429                                                gen_rtx_REG (tmpmode, *intreg),
3430                                                GEN_INT (i*8));
3431             intreg++;
3432             break;
3433           case X86_64_SSESF_CLASS:
3434             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3435                                                gen_rtx_REG (SFmode,
3436                                                             SSE_REGNO (sse_regno)),
3437                                                GEN_INT (i*8));
3438             sse_regno++;
3439             break;
3440           case X86_64_SSEDF_CLASS:
3441             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3442                                                gen_rtx_REG (DFmode,
3443                                                             SSE_REGNO (sse_regno)),
3444                                                GEN_INT (i*8));
3445             sse_regno++;
3446             break;
3447           case X86_64_SSE_CLASS:
3448             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3449               tmpmode = TImode;
3450             else
3451               tmpmode = DImode;
3452             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3453                                                gen_rtx_REG (tmpmode,
3454                                                             SSE_REGNO (sse_regno)),
3455                                                GEN_INT (i*8));
3456             if (tmpmode == TImode)
3457               i++;
3458             sse_regno++;
3459             break;
3460           default:
3461             gcc_unreachable ();
3462         }
3463     }
3464
3465   /* Empty aligned struct, union or class.  */
3466   if (nexps == 0)
3467     return NULL;
3468
3469   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3470   for (i = 0; i < nexps; i++)
3471     XVECEXP (ret, 0, i) = exp [i];
3472   return ret;
3473 }
3474
3475 /* Update the data in CUM to advance over an argument
3476    of mode MODE and data type TYPE.
3477    (TYPE is null for libcalls where that information may not be available.)  */
3478
3479 void
3480 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3481                       tree type, int named)
3482 {
3483   int bytes =
3484     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3485   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3486
3487   if (type)
3488     mode = type_natural_mode (type);
3489
3490   if (TARGET_DEBUG_ARG)
3491     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
3492              "mode=%s, named=%d)\n\n",
3493              words, cum->words, cum->nregs, cum->sse_nregs,
3494              GET_MODE_NAME (mode), named);
3495
3496   if (TARGET_64BIT)
3497     {
3498       int int_nregs, sse_nregs;
3499       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3500         cum->words += words;
3501       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3502         {
3503           cum->nregs -= int_nregs;
3504           cum->sse_nregs -= sse_nregs;
3505           cum->regno += int_nregs;
3506           cum->sse_regno += sse_nregs;
3507         }
3508       else
3509         cum->words += words;
3510     }
3511   else
3512     {
3513       switch (mode)
3514         {
3515         default:
3516           break;
3517
3518         case BLKmode:
3519           if (bytes < 0)
3520             break;
3521           /* FALLTHRU */
3522
3523         case DImode:
3524         case SImode:
3525         case HImode:
3526         case QImode:
3527           cum->words += words;
3528           cum->nregs -= words;
3529           cum->regno += words;
3530
3531           if (cum->nregs <= 0)
3532             {
3533               cum->nregs = 0;
3534               cum->regno = 0;
3535             }
3536           break;
3537
3538         case DFmode:
3539           if (cum->float_in_sse < 2)
3540             break;
3541         case SFmode:
3542           if (cum->float_in_sse < 1)
3543             break;
3544           /* FALLTHRU */
3545
3546         case TImode:
3547         case V16QImode:
3548         case V8HImode:
3549         case V4SImode:
3550         case V2DImode:
3551         case V4SFmode:
3552         case V2DFmode:
3553           if (!type || !AGGREGATE_TYPE_P (type))
3554             {
3555               cum->sse_words += words;
3556               cum->sse_nregs -= 1;
3557               cum->sse_regno += 1;
3558               if (cum->sse_nregs <= 0)
3559                 {
3560                   cum->sse_nregs = 0;
3561                   cum->sse_regno = 0;
3562                 }
3563             }
3564           break;
3565
3566         case V8QImode:
3567         case V4HImode:
3568         case V2SImode:
3569         case V2SFmode:
3570           if (!type || !AGGREGATE_TYPE_P (type))
3571             {
3572               cum->mmx_words += words;
3573               cum->mmx_nregs -= 1;
3574               cum->mmx_regno += 1;
3575               if (cum->mmx_nregs <= 0)
3576                 {
3577                   cum->mmx_nregs = 0;
3578                   cum->mmx_regno = 0;
3579                 }
3580             }
3581           break;
3582         }
3583     }
3584 }
3585
3586 /* Define where to put the arguments to a function.
3587    Value is zero to push the argument on the stack,
3588    or a hard register in which to store the argument.
3589
3590    MODE is the argument's machine mode.
3591    TYPE is the data type of the argument (as a tree).
3592     This is null for libcalls where that information may
3593     not be available.
3594    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3595     the preceding args and about the function being called.
3596    NAMED is nonzero if this argument is a named parameter
3597     (otherwise it is an extra parameter matching an ellipsis).  */
3598
3599 rtx
3600 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
3601               tree type, int named)
3602 {
3603   enum machine_mode mode = orig_mode;
3604   rtx ret = NULL_RTX;
3605   int bytes =
3606     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3607   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3608   static bool warnedsse, warnedmmx;
3609
3610   /* To simplify the code below, represent vector types with a vector mode
3611      even if MMX/SSE are not active.  */
3612   if (type && TREE_CODE (type) == VECTOR_TYPE)
3613     mode = type_natural_mode (type);
3614
3615   /* Handle a hidden AL argument containing number of registers for varargs
3616      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
3617      any AL settings.  */
3618   if (mode == VOIDmode)
3619     {
3620       if (TARGET_64BIT)
3621         return GEN_INT (cum->maybe_vaarg
3622                         ? (cum->sse_nregs < 0
3623                            ? SSE_REGPARM_MAX
3624                            : cum->sse_regno)
3625                         : -1);
3626       else
3627         return constm1_rtx;
3628     }
3629   if (TARGET_64BIT)
3630     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
3631                                cum->sse_nregs,
3632                                &x86_64_int_parameter_registers [cum->regno],
3633                                cum->sse_regno);
3634   else
3635     switch (mode)
3636       {
3637         /* For now, pass fp/complex values on the stack.  */
3638       default:
3639         break;
3640
3641       case BLKmode:
3642         if (bytes < 0)
3643           break;
3644         /* FALLTHRU */
3645       case DImode:
3646       case SImode:
3647       case HImode:
3648       case QImode:
3649         if (words <= cum->nregs)
3650           {
3651             int regno = cum->regno;
3652
3653             /* Fastcall allocates the first two DWORD (SImode) or
3654                smaller arguments to ECX and EDX.  */
3655             if (cum->fastcall)
3656               {
3657                 if (mode == BLKmode || mode == DImode)
3658                   break;
3659
3660                 /* ECX not EAX is the first allocated register.  */
3661                 if (regno == 0)
3662                   regno = 2;
3663               }
3664             ret = gen_rtx_REG (mode, regno);
3665           }
3666         break;
3667       case DFmode:
3668         if (cum->float_in_sse < 2)
3669           break;
3670       case SFmode:
3671         if (cum->float_in_sse < 1)
3672           break;
3673         /* FALLTHRU */
3674       case TImode:
3675       case V16QImode:
3676       case V8HImode:
3677       case V4SImode:
3678       case V2DImode:
3679       case V4SFmode:
3680       case V2DFmode:
3681         if (!type || !AGGREGATE_TYPE_P (type))
3682           {
3683             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3684               {
3685                 warnedsse = true;
3686                 warning (0, "SSE vector argument without SSE enabled "
3687                          "changes the ABI");
3688               }
3689             if (cum->sse_nregs)
3690               ret = gen_reg_or_parallel (mode, orig_mode,
3691                                          cum->sse_regno + FIRST_SSE_REG);
3692           }
3693         break;
3694       case V8QImode:
3695       case V4HImode:
3696       case V2SImode:
3697       case V2SFmode:
3698         if (!type || !AGGREGATE_TYPE_P (type))
3699           {
3700             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3701               {
3702                 warnedmmx = true;
3703                 warning (0, "MMX vector argument without MMX enabled "
3704                          "changes the ABI");
3705               }
3706             if (cum->mmx_nregs)
3707               ret = gen_reg_or_parallel (mode, orig_mode,
3708                                          cum->mmx_regno + FIRST_MMX_REG);
3709           }
3710         break;
3711       }
3712
3713   if (TARGET_DEBUG_ARG)
3714     {
3715       fprintf (stderr,
3716                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3717                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3718
3719       if (ret)
3720         print_simple_rtl (stderr, ret);
3721       else
3722         fprintf (stderr, ", stack");
3723
3724       fprintf (stderr, " )\n");
3725     }
3726
3727   return ret;
3728 }
3729
3730 /* A C expression that indicates when an argument must be passed by
3731    reference.  If nonzero for an argument, a copy of that argument is
3732    made in memory and a pointer to the argument is passed instead of
3733    the argument itself.  The pointer is passed in whatever way is
3734    appropriate for passing a pointer to that type.  */
3735
3736 static bool
3737 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3738                         enum machine_mode mode ATTRIBUTE_UNUSED,
3739                         tree type, bool named ATTRIBUTE_UNUSED)
3740 {
3741   if (!TARGET_64BIT)
3742     return 0;
3743
3744   if (type && int_size_in_bytes (type) == -1)
3745     {
3746       if (TARGET_DEBUG_ARG)
3747         fprintf (stderr, "function_arg_pass_by_reference\n");
3748       return 1;
3749     }
3750
3751   return 0;
3752 }
3753
3754 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3755    ABI.  Only called if TARGET_SSE.  */
3756 static bool
3757 contains_128bit_aligned_vector_p (tree type)
3758 {
3759   enum machine_mode mode = TYPE_MODE (type);
3760   if (SSE_REG_MODE_P (mode)
3761       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3762     return true;
3763   if (TYPE_ALIGN (type) < 128)
3764     return false;
3765
3766   if (AGGREGATE_TYPE_P (type))
3767     {
3768       /* Walk the aggregates recursively.  */
3769       switch (TREE_CODE (type))
3770         {
3771         case RECORD_TYPE:
3772         case UNION_TYPE:
3773         case QUAL_UNION_TYPE:
3774           {
3775             tree field;
3776
3777             /* Walk all the structure fields.  */
3778             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3779               {
3780                 if (TREE_CODE (field) == FIELD_DECL
3781                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3782                   return true;
3783               }
3784             break;
3785           }
3786
3787         case ARRAY_TYPE:
3788           /* Just for use if some languages passes arrays by value.  */
3789           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3790             return true;
3791           break;
3792
3793         default:
3794           gcc_unreachable ();
3795         }
3796     }
3797   return false;
3798 }
3799
3800 /* Gives the alignment boundary, in bits, of an argument with the
3801    specified mode and type.  */
3802
3803 int
3804 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3805 {
3806   int align;
3807   if (type)
3808     align = TYPE_ALIGN (type);
3809   else
3810     align = GET_MODE_ALIGNMENT (mode);
3811   if (align < PARM_BOUNDARY)
3812     align = PARM_BOUNDARY;
3813   if (!TARGET_64BIT)
3814     {
3815       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3816          make an exception for SSE modes since these require 128bit
3817          alignment.
3818
3819          The handling here differs from field_alignment.  ICC aligns MMX
3820          arguments to 4 byte boundaries, while structure fields are aligned
3821          to 8 byte boundaries.  */
3822       if (!TARGET_SSE)
3823         align = PARM_BOUNDARY;
3824       else if (!type)
3825         {
3826           if (!SSE_REG_MODE_P (mode))
3827             align = PARM_BOUNDARY;
3828         }
3829       else
3830         {
3831           if (!contains_128bit_aligned_vector_p (type))
3832             align = PARM_BOUNDARY;
3833         }
3834     }
3835   if (align > 128)
3836     align = 128;
3837   return align;
3838 }
3839
3840 /* Return true if N is a possible register number of function value.  */
3841 bool
3842 ix86_function_value_regno_p (int regno)
3843 {
3844   if (regno == 0
3845       || (regno == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
3846       || (regno == FIRST_SSE_REG && TARGET_SSE))
3847     return true;
3848
3849   if (!TARGET_64BIT
3850       && (regno == FIRST_MMX_REG && TARGET_MMX))
3851         return true;
3852
3853   return false;
3854 }
3855
3856 /* Define how to find the value returned by a function.
3857    VALTYPE is the data type of the value (as a tree).
3858    If the precise function being called is known, FUNC is its FUNCTION_DECL;
3859    otherwise, FUNC is 0.  */
3860 rtx
3861 ix86_function_value (tree valtype, tree fntype_or_decl,
3862                      bool outgoing ATTRIBUTE_UNUSED)
3863 {
3864   enum machine_mode natmode = type_natural_mode (valtype);
3865
3866   if (TARGET_64BIT)
3867     {
3868       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
3869                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
3870                                      x86_64_int_return_registers, 0);
3871       /* For zero sized structures, construct_container return NULL, but we
3872          need to keep rest of compiler happy by returning meaningful value.  */
3873       if (!ret)
3874         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
3875       return ret;
3876     }
3877   else
3878     {
3879       tree fn = NULL_TREE, fntype;
3880       if (fntype_or_decl
3881           && DECL_P (fntype_or_decl))
3882         fn = fntype_or_decl;
3883       fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
3884       return gen_rtx_REG (TYPE_MODE (valtype),
3885                           ix86_value_regno (natmode, fn, fntype));
3886     }
3887 }
3888
3889 /* Return true iff type is returned in memory.  */
3890 int
3891 ix86_return_in_memory (tree type)
3892 {
3893   int needed_intregs, needed_sseregs, size;
3894   enum machine_mode mode = type_natural_mode (type);
3895
3896   if (TARGET_64BIT)
3897     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
3898
3899   if (mode == BLKmode)
3900     return 1;
3901
3902   size = int_size_in_bytes (type);
3903
3904   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
3905     return 0;
3906
3907   if (VECTOR_MODE_P (mode) || mode == TImode)
3908     {
3909       /* User-created vectors small enough to fit in EAX.  */
3910       if (size < 8)
3911         return 0;
3912
3913       /* MMX/3dNow values are returned in MM0,
3914          except when it doesn't exits.  */
3915       if (size == 8)
3916         return (TARGET_MMX ? 0 : 1);
3917
3918       /* SSE values are returned in XMM0, except when it doesn't exist.  */
3919       if (size == 16)
3920         return (TARGET_SSE ? 0 : 1);
3921     }
3922
3923   if (mode == XFmode)
3924     return 0;
3925
3926   if (mode == TDmode)
3927     return 1;
3928
3929   if (size > 12)
3930     return 1;
3931   return 0;
3932 }
3933
3934 /* When returning SSE vector types, we have a choice of either
3935      (1) being abi incompatible with a -march switch, or
3936      (2) generating an error.
3937    Given no good solution, I think the safest thing is one warning.
3938    The user won't be able to use -Werror, but....
3939
3940    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
3941    called in response to actually generating a caller or callee that
3942    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3943    via aggregate_value_p for general type probing from tree-ssa.  */
3944
3945 static rtx
3946 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3947 {
3948   static bool warnedsse, warnedmmx;
3949
3950   if (type)
3951     {
3952       /* Look at the return type of the function, not the function type.  */
3953       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3954
3955       if (!TARGET_SSE && !warnedsse)
3956         {
3957           if (mode == TImode
3958               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3959             {
3960               warnedsse = true;
3961               warning (0, "SSE vector return without SSE enabled "
3962                        "changes the ABI");
3963             }
3964         }
3965
3966       if (!TARGET_MMX && !warnedmmx)
3967         {
3968           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
3969             {
3970               warnedmmx = true;
3971               warning (0, "MMX vector return without MMX enabled "
3972                        "changes the ABI");
3973             }
3974         }
3975     }
3976
3977   return NULL;
3978 }
3979
3980 /* Define how to find the value returned by a library function
3981    assuming the value has mode MODE.  */
3982 rtx
3983 ix86_libcall_value (enum machine_mode mode)
3984 {
3985   if (TARGET_64BIT)
3986     {
3987       switch (mode)
3988         {
3989         case SFmode:
3990         case SCmode:
3991         case DFmode:
3992         case DCmode:
3993         case TFmode:
3994         case SDmode:
3995         case DDmode:
3996         case TDmode:
3997           return gen_rtx_REG (mode, FIRST_SSE_REG);
3998         case XFmode:
3999         case XCmode:
4000           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
4001         case TCmode:
4002           return NULL;
4003         default:
4004           return gen_rtx_REG (mode, 0);
4005         }
4006     }
4007   else
4008     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL, NULL));
4009 }
4010
4011 /* Given a mode, return the register to use for a return value.  */
4012
4013 static int
4014 ix86_value_regno (enum machine_mode mode, tree func, tree fntype)
4015 {
4016   gcc_assert (!TARGET_64BIT);
4017
4018   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
4019      we normally prevent this case when mmx is not available.  However
4020      some ABIs may require the result to be returned like DImode.  */
4021   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
4022     return TARGET_MMX ? FIRST_MMX_REG : 0;
4023
4024   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
4025      we prevent this case when sse is not available.  However some ABIs
4026      may require the result to be returned like integer TImode.  */
4027   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4028     return TARGET_SSE ? FIRST_SSE_REG : 0;
4029
4030   /* Decimal floating point values can go in %eax, unlike other float modes.  */
4031   if (DECIMAL_FLOAT_MODE_P (mode))
4032     return 0;
4033
4034   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
4035   if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
4036     return 0;
4037
4038   /* Floating point return values in %st(0), except for local functions when
4039      SSE math is enabled or for functions with sseregparm attribute.  */
4040   if ((func || fntype)
4041       && (mode == SFmode || mode == DFmode))
4042     {
4043       int sse_level = ix86_function_sseregparm (fntype, func);
4044       if ((sse_level >= 1 && mode == SFmode)
4045           || (sse_level == 2 && mode == DFmode))
4046         return FIRST_SSE_REG;
4047     }
4048
4049   return FIRST_FLOAT_REG;
4050 }
4051 \f
4052 /* Create the va_list data type.  */
4053
4054 static tree
4055 ix86_build_builtin_va_list (void)
4056 {
4057   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4058
4059   /* For i386 we use plain pointer to argument area.  */
4060   if (!TARGET_64BIT)
4061     return build_pointer_type (char_type_node);
4062
4063   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4064   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4065
4066   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4067                       unsigned_type_node);
4068   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4069                       unsigned_type_node);
4070   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4071                       ptr_type_node);
4072   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4073                       ptr_type_node);
4074
4075   va_list_gpr_counter_field = f_gpr;
4076   va_list_fpr_counter_field = f_fpr;
4077
4078   DECL_FIELD_CONTEXT (f_gpr) = record;
4079   DECL_FIELD_CONTEXT (f_fpr) = record;
4080   DECL_FIELD_CONTEXT (f_ovf) = record;
4081   DECL_FIELD_CONTEXT (f_sav) = record;
4082
4083   TREE_CHAIN (record) = type_decl;
4084   TYPE_NAME (record) = type_decl;
4085   TYPE_FIELDS (record) = f_gpr;
4086   TREE_CHAIN (f_gpr) = f_fpr;
4087   TREE_CHAIN (f_fpr) = f_ovf;
4088   TREE_CHAIN (f_ovf) = f_sav;
4089
4090   layout_type (record);
4091
4092   /* The correct type is an array type of one element.  */
4093   return build_array_type (record, build_index_type (size_zero_node));
4094 }
4095
4096 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4097
4098 static void
4099 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4100                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4101                              int no_rtl)
4102 {
4103   CUMULATIVE_ARGS next_cum;
4104   rtx save_area = NULL_RTX, mem;
4105   rtx label;
4106   rtx label_ref;
4107   rtx tmp_reg;
4108   rtx nsse_reg;
4109   int set;
4110   tree fntype;
4111   int stdarg_p;
4112   int i;
4113
4114   if (!TARGET_64BIT)
4115     return;
4116
4117   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4118     return;
4119
4120   /* Indicate to allocate space on the stack for varargs save area.  */
4121   ix86_save_varrargs_registers = 1;
4122
4123   cfun->stack_alignment_needed = 128;
4124
4125   fntype = TREE_TYPE (current_function_decl);
4126   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4127               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4128                   != void_type_node));
4129
4130   /* For varargs, we do not want to skip the dummy va_dcl argument.
4131      For stdargs, we do want to skip the last named argument.  */
4132   next_cum = *cum;
4133   if (stdarg_p)
4134     function_arg_advance (&next_cum, mode, type, 1);
4135
4136   if (!no_rtl)
4137     save_area = frame_pointer_rtx;
4138
4139   set = get_varargs_alias_set ();
4140
4141   for (i = next_cum.regno;
4142        i < ix86_regparm
4143        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4144        i++)
4145     {
4146       mem = gen_rtx_MEM (Pmode,
4147                          plus_constant (save_area, i * UNITS_PER_WORD));
4148       MEM_NOTRAP_P (mem) = 1;
4149       set_mem_alias_set (mem, set);
4150       emit_move_insn (mem, gen_rtx_REG (Pmode,
4151                                         x86_64_int_parameter_registers[i]));
4152     }
4153
4154   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
4155     {
4156       /* Now emit code to save SSE registers.  The AX parameter contains number
4157          of SSE parameter registers used to call this function.  We use
4158          sse_prologue_save insn template that produces computed jump across
4159          SSE saves.  We need some preparation work to get this working.  */
4160
4161       label = gen_label_rtx ();
4162       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4163
4164       /* Compute address to jump to :
4165          label - 5*eax + nnamed_sse_arguments*5  */
4166       tmp_reg = gen_reg_rtx (Pmode);
4167       nsse_reg = gen_reg_rtx (Pmode);
4168       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4169       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4170                               gen_rtx_MULT (Pmode, nsse_reg,
4171                                             GEN_INT (4))));
4172       if (next_cum.sse_regno)
4173         emit_move_insn
4174           (nsse_reg,
4175            gen_rtx_CONST (DImode,
4176                           gen_rtx_PLUS (DImode,
4177                                         label_ref,
4178                                         GEN_INT (next_cum.sse_regno * 4))));
4179       else
4180         emit_move_insn (nsse_reg, label_ref);
4181       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4182
4183       /* Compute address of memory block we save into.  We always use pointer
4184          pointing 127 bytes after first byte to store - this is needed to keep
4185          instruction size limited by 4 bytes.  */
4186       tmp_reg = gen_reg_rtx (Pmode);
4187       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4188                               plus_constant (save_area,
4189                                              8 * REGPARM_MAX + 127)));
4190       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4191       MEM_NOTRAP_P (mem) = 1;
4192       set_mem_alias_set (mem, set);
4193       set_mem_align (mem, BITS_PER_WORD);
4194
4195       /* And finally do the dirty job!  */
4196       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4197                                         GEN_INT (next_cum.sse_regno), label));
4198     }
4199
4200 }
4201
4202 /* Implement va_start.  */
4203
4204 void
4205 ix86_va_start (tree valist, rtx nextarg)
4206 {
4207   HOST_WIDE_INT words, n_gpr, n_fpr;
4208   tree f_gpr, f_fpr, f_ovf, f_sav;
4209   tree gpr, fpr, ovf, sav, t;
4210   tree type;
4211
4212   /* Only 64bit target needs something special.  */
4213   if (!TARGET_64BIT)
4214     {
4215       std_expand_builtin_va_start (valist, nextarg);
4216       return;
4217     }
4218
4219   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4220   f_fpr = TREE_CHAIN (f_gpr);
4221   f_ovf = TREE_CHAIN (f_fpr);
4222   f_sav = TREE_CHAIN (f_ovf);
4223
4224   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4225   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4226   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4227   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4228   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4229
4230   /* Count number of gp and fp argument registers used.  */
4231   words = current_function_args_info.words;
4232   n_gpr = current_function_args_info.regno;
4233   n_fpr = current_function_args_info.sse_regno;
4234
4235   if (TARGET_DEBUG_ARG)
4236     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
4237              (int) words, (int) n_gpr, (int) n_fpr);
4238
4239   if (cfun->va_list_gpr_size)
4240     {
4241       type = TREE_TYPE (gpr);
4242       t = build2 (MODIFY_EXPR, type, gpr,
4243                   build_int_cst (type, n_gpr * 8));
4244       TREE_SIDE_EFFECTS (t) = 1;
4245       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4246     }
4247
4248   if (cfun->va_list_fpr_size)
4249     {
4250       type = TREE_TYPE (fpr);
4251       t = build2 (MODIFY_EXPR, type, fpr,
4252                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4253       TREE_SIDE_EFFECTS (t) = 1;
4254       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4255     }
4256
4257   /* Find the overflow area.  */
4258   type = TREE_TYPE (ovf);
4259   t = make_tree (type, virtual_incoming_args_rtx);
4260   if (words != 0)
4261     t = build2 (PLUS_EXPR, type, t,
4262                 build_int_cst (type, words * UNITS_PER_WORD));
4263   t = build2 (MODIFY_EXPR, type, ovf, t);
4264   TREE_SIDE_EFFECTS (t) = 1;
4265   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4266
4267   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4268     {
4269       /* Find the register save area.
4270          Prologue of the function save it right above stack frame.  */
4271       type = TREE_TYPE (sav);
4272       t = make_tree (type, frame_pointer_rtx);
4273       t = build2 (MODIFY_EXPR, type, sav, t);
4274       TREE_SIDE_EFFECTS (t) = 1;
4275       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4276     }
4277 }
4278
4279 /* Implement va_arg.  */
4280
4281 tree
4282 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4283 {
4284   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4285   tree f_gpr, f_fpr, f_ovf, f_sav;
4286   tree gpr, fpr, ovf, sav, t;
4287   int size, rsize;
4288   tree lab_false, lab_over = NULL_TREE;
4289   tree addr, t2;
4290   rtx container;
4291   int indirect_p = 0;
4292   tree ptrtype;
4293   enum machine_mode nat_mode;
4294
4295   /* Only 64bit target needs something special.  */
4296   if (!TARGET_64BIT)
4297     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4298
4299   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4300   f_fpr = TREE_CHAIN (f_gpr);
4301   f_ovf = TREE_CHAIN (f_fpr);
4302   f_sav = TREE_CHAIN (f_ovf);
4303
4304   valist = build_va_arg_indirect_ref (valist);
4305   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4306   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4307   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4308   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4309
4310   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4311   if (indirect_p)
4312     type = build_pointer_type (type);
4313   size = int_size_in_bytes (type);
4314   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4315
4316   nat_mode = type_natural_mode (type);
4317   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4318                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4319
4320   /* Pull the value out of the saved registers.  */
4321
4322   addr = create_tmp_var (ptr_type_node, "addr");
4323   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4324
4325   if (container)
4326     {
4327       int needed_intregs, needed_sseregs;
4328       bool need_temp;
4329       tree int_addr, sse_addr;
4330
4331       lab_false = create_artificial_label ();
4332       lab_over = create_artificial_label ();
4333
4334       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4335
4336       need_temp = (!REG_P (container)
4337                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4338                        || TYPE_ALIGN (type) > 128));
4339
4340       /* In case we are passing structure, verify that it is consecutive block
4341          on the register save area.  If not we need to do moves.  */
4342       if (!need_temp && !REG_P (container))
4343         {
4344           /* Verify that all registers are strictly consecutive  */
4345           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4346             {
4347               int i;
4348
4349               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4350                 {
4351                   rtx slot = XVECEXP (container, 0, i);
4352                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4353                       || INTVAL (XEXP (slot, 1)) != i * 16)
4354                     need_temp = 1;
4355                 }
4356             }
4357           else
4358             {
4359               int i;
4360
4361               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4362                 {
4363                   rtx slot = XVECEXP (container, 0, i);
4364                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4365                       || INTVAL (XEXP (slot, 1)) != i * 8)
4366                     need_temp = 1;
4367                 }
4368             }
4369         }
4370       if (!need_temp)
4371         {
4372           int_addr = addr;
4373           sse_addr = addr;
4374         }
4375       else
4376         {
4377           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4378           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4379           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4380           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4381         }
4382
4383       /* First ensure that we fit completely in registers.  */
4384       if (needed_intregs)
4385         {
4386           t = build_int_cst (TREE_TYPE (gpr),
4387                              (REGPARM_MAX - needed_intregs + 1) * 8);
4388           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4389           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4390           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4391           gimplify_and_add (t, pre_p);
4392         }
4393       if (needed_sseregs)
4394         {
4395           t = build_int_cst (TREE_TYPE (fpr),
4396                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4397                              + REGPARM_MAX * 8);
4398           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4399           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4400           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4401           gimplify_and_add (t, pre_p);
4402         }
4403
4404       /* Compute index to start of area used for integer regs.  */
4405       if (needed_intregs)
4406         {
4407           /* int_addr = gpr + sav; */
4408           t = fold_convert (ptr_type_node, gpr);
4409           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4410           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
4411           gimplify_and_add (t, pre_p);
4412         }
4413       if (needed_sseregs)
4414         {
4415           /* sse_addr = fpr + sav; */
4416           t = fold_convert (ptr_type_node, fpr);
4417           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4418           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
4419           gimplify_and_add (t, pre_p);
4420         }
4421       if (need_temp)
4422         {
4423           int i;
4424           tree temp = create_tmp_var (type, "va_arg_tmp");
4425
4426           /* addr = &temp; */
4427           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4428           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
4429           gimplify_and_add (t, pre_p);
4430
4431           for (i = 0; i < XVECLEN (container, 0); i++)
4432             {
4433               rtx slot = XVECEXP (container, 0, i);
4434               rtx reg = XEXP (slot, 0);
4435               enum machine_mode mode = GET_MODE (reg);
4436               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4437               tree addr_type = build_pointer_type (piece_type);
4438               tree src_addr, src;
4439               int src_offset;
4440               tree dest_addr, dest;
4441
4442               if (SSE_REGNO_P (REGNO (reg)))
4443                 {
4444                   src_addr = sse_addr;
4445                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4446                 }
4447               else
4448                 {
4449                   src_addr = int_addr;
4450                   src_offset = REGNO (reg) * 8;
4451                 }
4452               src_addr = fold_convert (addr_type, src_addr);
4453               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
4454                                        size_int (src_offset)));
4455               src = build_va_arg_indirect_ref (src_addr);
4456
4457               dest_addr = fold_convert (addr_type, addr);
4458               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
4459                                         size_int (INTVAL (XEXP (slot, 1)))));
4460               dest = build_va_arg_indirect_ref (dest_addr);
4461
4462               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
4463               gimplify_and_add (t, pre_p);
4464             }
4465         }
4466
4467       if (needed_intregs)
4468         {
4469           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4470                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4471           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4472           gimplify_and_add (t, pre_p);
4473         }
4474       if (needed_sseregs)
4475         {
4476           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4477                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4478           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
4479           gimplify_and_add (t, pre_p);
4480         }
4481
4482       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4483       gimplify_and_add (t, pre_p);
4484
4485       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4486       append_to_statement_list (t, pre_p);
4487     }
4488
4489   /* ... otherwise out of the overflow area.  */
4490
4491   /* Care for on-stack alignment if needed.  */
4492   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4493       || integer_zerop (TYPE_SIZE (type)))
4494     t = ovf;
4495   else
4496     {
4497       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4498       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4499                   build_int_cst (TREE_TYPE (ovf), align - 1));
4500       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4501                   build_int_cst (TREE_TYPE (t), -align));
4502     }
4503   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4504
4505   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
4506   gimplify_and_add (t2, pre_p);
4507
4508   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4509               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4510   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4511   gimplify_and_add (t, pre_p);
4512
4513   if (container)
4514     {
4515       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4516       append_to_statement_list (t, pre_p);
4517     }
4518
4519   ptrtype = build_pointer_type (type);
4520   addr = fold_convert (ptrtype, addr);
4521
4522   if (indirect_p)
4523     addr = build_va_arg_indirect_ref (addr);
4524   return build_va_arg_indirect_ref (addr);
4525 }
4526 \f
4527 /* Return nonzero if OPNUM's MEM should be matched
4528    in movabs* patterns.  */
4529
4530 int
4531 ix86_check_movabs (rtx insn, int opnum)
4532 {
4533   rtx set, mem;
4534
4535   set = PATTERN (insn);
4536   if (GET_CODE (set) == PARALLEL)
4537     set = XVECEXP (set, 0, 0);
4538   gcc_assert (GET_CODE (set) == SET);
4539   mem = XEXP (set, opnum);
4540   while (GET_CODE (mem) == SUBREG)
4541     mem = SUBREG_REG (mem);
4542   gcc_assert (GET_CODE (mem) == MEM);
4543   return (volatile_ok || !MEM_VOLATILE_P (mem));
4544 }
4545 \f
4546 /* Initialize the table of extra 80387 mathematical constants.  */
4547
4548 static void
4549 init_ext_80387_constants (void)
4550 {
4551   static const char * cst[5] =
4552   {
4553     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4554     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4555     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4556     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4557     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4558   };
4559   int i;
4560
4561   for (i = 0; i < 5; i++)
4562     {
4563       real_from_string (&ext_80387_constants_table[i], cst[i]);
4564       /* Ensure each constant is rounded to XFmode precision.  */
4565       real_convert (&ext_80387_constants_table[i],
4566                     XFmode, &ext_80387_constants_table[i]);
4567     }
4568
4569   ext_80387_constants_init = 1;
4570 }
4571
4572 /* Return true if the constant is something that can be loaded with
4573    a special instruction.  */
4574
4575 int
4576 standard_80387_constant_p (rtx x)
4577 {
4578   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4579     return -1;
4580
4581   if (x == CONST0_RTX (GET_MODE (x)))
4582     return 1;
4583   if (x == CONST1_RTX (GET_MODE (x)))
4584     return 2;
4585
4586   /* For XFmode constants, try to find a special 80387 instruction when
4587      optimizing for size or on those CPUs that benefit from them.  */
4588   if (GET_MODE (x) == XFmode
4589       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
4590     {
4591       REAL_VALUE_TYPE r;
4592       int i;
4593
4594       if (! ext_80387_constants_init)
4595         init_ext_80387_constants ();
4596
4597       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4598       for (i = 0; i < 5; i++)
4599         if (real_identical (&r, &ext_80387_constants_table[i]))
4600           return i + 3;
4601     }
4602
4603   return 0;
4604 }
4605
4606 /* Return the opcode of the special instruction to be used to load
4607    the constant X.  */
4608
4609 const char *
4610 standard_80387_constant_opcode (rtx x)
4611 {
4612   switch (standard_80387_constant_p (x))
4613     {
4614     case 1:
4615       return "fldz";
4616     case 2:
4617       return "fld1";
4618     case 3:
4619       return "fldlg2";
4620     case 4:
4621       return "fldln2";
4622     case 5:
4623       return "fldl2e";
4624     case 6:
4625       return "fldl2t";
4626     case 7:
4627       return "fldpi";
4628     default:
4629       gcc_unreachable ();
4630     }
4631 }
4632
4633 /* Return the CONST_DOUBLE representing the 80387 constant that is
4634    loaded by the specified special instruction.  The argument IDX
4635    matches the return value from standard_80387_constant_p.  */
4636
4637 rtx
4638 standard_80387_constant_rtx (int idx)
4639 {
4640   int i;
4641
4642   if (! ext_80387_constants_init)
4643     init_ext_80387_constants ();
4644
4645   switch (idx)
4646     {
4647     case 3:
4648     case 4:
4649     case 5:
4650     case 6:
4651     case 7:
4652       i = idx - 3;
4653       break;
4654
4655     default:
4656       gcc_unreachable ();
4657     }
4658
4659   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4660                                        XFmode);
4661 }
4662
4663 /* Return 1 if mode is a valid mode for sse.  */
4664 static int
4665 standard_sse_mode_p (enum machine_mode mode)
4666 {
4667   switch (mode)
4668     {
4669     case V16QImode:
4670     case V8HImode:
4671     case V4SImode:
4672     case V2DImode:
4673     case V4SFmode:
4674     case V2DFmode:
4675       return 1;
4676
4677     default:
4678       return 0;
4679     }
4680 }
4681
4682 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4683  */
4684 int
4685 standard_sse_constant_p (rtx x)
4686 {
4687   enum machine_mode mode = GET_MODE (x);
4688
4689   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
4690     return 1;
4691   if (vector_all_ones_operand (x, mode)
4692       && standard_sse_mode_p (mode))
4693     return TARGET_SSE2 ? 2 : -1;
4694
4695   return 0;
4696 }
4697
4698 /* Return the opcode of the special instruction to be used to load
4699    the constant X.  */
4700
4701 const char *
4702 standard_sse_constant_opcode (rtx insn, rtx x)
4703 {
4704   switch (standard_sse_constant_p (x))
4705     {
4706     case 1:
4707       if (get_attr_mode (insn) == MODE_V4SF)
4708         return "xorps\t%0, %0";
4709       else if (get_attr_mode (insn) == MODE_V2DF)
4710         return "xorpd\t%0, %0";
4711       else
4712         return "pxor\t%0, %0";
4713     case 2:
4714       return "pcmpeqd\t%0, %0";
4715     }
4716   gcc_unreachable ();
4717 }
4718
4719 /* Returns 1 if OP contains a symbol reference */
4720
4721 int
4722 symbolic_reference_mentioned_p (rtx op)
4723 {
4724   const char *fmt;
4725   int i;
4726
4727   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4728     return 1;
4729
4730   fmt = GET_RTX_FORMAT (GET_CODE (op));
4731   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4732     {
4733       if (fmt[i] == 'E')
4734         {
4735           int j;
4736
4737           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4738             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4739               return 1;
4740         }
4741
4742       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4743         return 1;
4744     }
4745
4746   return 0;
4747 }
4748
4749 /* Return 1 if it is appropriate to emit `ret' instructions in the
4750    body of a function.  Do this only if the epilogue is simple, needing a
4751    couple of insns.  Prior to reloading, we can't tell how many registers
4752    must be saved, so return 0 then.  Return 0 if there is no frame
4753    marker to de-allocate.  */
4754
4755 int
4756 ix86_can_use_return_insn_p (void)
4757 {
4758   struct ix86_frame frame;
4759
4760   if (! reload_completed || frame_pointer_needed)
4761     return 0;
4762
4763   /* Don't allow more than 32 pop, since that's all we can do
4764      with one instruction.  */
4765   if (current_function_pops_args
4766       && current_function_args_size >= 32768)
4767     return 0;
4768
4769   ix86_compute_frame_layout (&frame);
4770   return frame.to_allocate == 0 && frame.nregs == 0;
4771 }
4772 \f
4773 /* Value should be nonzero if functions must have frame pointers.
4774    Zero means the frame pointer need not be set up (and parms may
4775    be accessed via the stack pointer) in functions that seem suitable.  */
4776
4777 int
4778 ix86_frame_pointer_required (void)
4779 {
4780   /* If we accessed previous frames, then the generated code expects
4781      to be able to access the saved ebp value in our frame.  */
4782   if (cfun->machine->accesses_prev_frame)
4783     return 1;
4784
4785   /* Several x86 os'es need a frame pointer for other reasons,
4786      usually pertaining to setjmp.  */
4787   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4788     return 1;
4789
4790   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4791      the frame pointer by default.  Turn it back on now if we've not
4792      got a leaf function.  */
4793   if (TARGET_OMIT_LEAF_FRAME_POINTER
4794       && (!current_function_is_leaf
4795           || ix86_current_function_calls_tls_descriptor))
4796     return 1;
4797
4798   if (current_function_profile)
4799     return 1;
4800
4801   return 0;
4802 }
4803
4804 /* Record that the current function accesses previous call frames.  */
4805
4806 void
4807 ix86_setup_frame_addresses (void)
4808 {
4809   cfun->machine->accesses_prev_frame = 1;
4810 }
4811 \f
4812 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
4813 # define USE_HIDDEN_LINKONCE 1
4814 #else
4815 # define USE_HIDDEN_LINKONCE 0
4816 #endif
4817
4818 static int pic_labels_used;
4819
4820 /* Fills in the label name that should be used for a pc thunk for
4821    the given register.  */
4822
4823 static void
4824 get_pc_thunk_name (char name[32], unsigned int regno)
4825 {
4826   gcc_assert (!TARGET_64BIT);
4827
4828   if (USE_HIDDEN_LINKONCE)
4829     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4830   else
4831     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4832 }
4833
4834
4835 /* This function generates code for -fpic that loads %ebx with
4836    the return address of the caller and then returns.  */
4837
4838 void
4839 ix86_file_end (void)
4840 {
4841   rtx xops[2];
4842   int regno;
4843
4844   for (regno = 0; regno < 8; ++regno)
4845     {
4846       char name[32];
4847
4848       if (! ((pic_labels_used >> regno) & 1))
4849         continue;
4850
4851       get_pc_thunk_name (name, regno);
4852
4853 #if TARGET_MACHO
4854       if (TARGET_MACHO)
4855         {
4856           switch_to_section (darwin_sections[text_coal_section]);
4857           fputs ("\t.weak_definition\t", asm_out_file);
4858           assemble_name (asm_out_file, name);
4859           fputs ("\n\t.private_extern\t", asm_out_file);
4860           assemble_name (asm_out_file, name);
4861           fputs ("\n", asm_out_file);
4862           ASM_OUTPUT_LABEL (asm_out_file, name);
4863         }
4864       else
4865 #endif
4866       if (USE_HIDDEN_LINKONCE)
4867         {
4868           tree decl;
4869
4870           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4871                              error_mark_node);
4872           TREE_PUBLIC (decl) = 1;
4873           TREE_STATIC (decl) = 1;
4874           DECL_ONE_ONLY (decl) = 1;
4875
4876           (*targetm.asm_out.unique_section) (decl, 0);
4877           switch_to_section (get_named_section (decl, NULL, 0));
4878
4879           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4880           fputs ("\t.hidden\t", asm_out_file);
4881           assemble_name (asm_out_file, name);
4882           fputc ('\n', asm_out_file);
4883           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4884         }
4885       else
4886         {
4887           switch_to_section (text_section);
4888           ASM_OUTPUT_LABEL (asm_out_file, name);
4889         }
4890
4891       xops[0] = gen_rtx_REG (SImode, regno);
4892       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4893       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4894       output_asm_insn ("ret", xops);
4895     }
4896
4897   if (NEED_INDICATE_EXEC_STACK)
4898     file_end_indicate_exec_stack ();
4899 }
4900
4901 /* Emit code for the SET_GOT patterns.  */
4902
4903 const char *
4904 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
4905 {
4906   rtx xops[3];
4907
4908   xops[0] = dest;
4909   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4910
4911   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4912     {
4913       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
4914
4915       if (!flag_pic)
4916         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4917       else
4918         output_asm_insn ("call\t%a2", xops);
4919
4920 #if TARGET_MACHO
4921       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4922          is what will be referenced by the Mach-O PIC subsystem.  */
4923       if (!label)
4924         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4925 #endif
4926
4927       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4928                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4929
4930       if (flag_pic)
4931         output_asm_insn ("pop{l}\t%0", xops);
4932     }
4933   else
4934     {
4935       char name[32];
4936       get_pc_thunk_name (name, REGNO (dest));
4937       pic_labels_used |= 1 << REGNO (dest);
4938
4939       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4940       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4941       output_asm_insn ("call\t%X2", xops);
4942       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4943          is what will be referenced by the Mach-O PIC subsystem.  */
4944 #if TARGET_MACHO
4945       if (!label)
4946         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4947       else
4948         targetm.asm_out.internal_label (asm_out_file, "L",
4949                                            CODE_LABEL_NUMBER (label));
4950 #endif
4951     }
4952
4953   if (TARGET_MACHO)
4954     return "";
4955
4956   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4957     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4958   else
4959     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
4960
4961   return "";
4962 }
4963
4964 /* Generate an "push" pattern for input ARG.  */
4965
4966 static rtx
4967 gen_push (rtx arg)
4968 {
4969   return gen_rtx_SET (VOIDmode,
4970                       gen_rtx_MEM (Pmode,
4971                                    gen_rtx_PRE_DEC (Pmode,
4972                                                     stack_pointer_rtx)),
4973                       arg);
4974 }
4975
4976 /* Return >= 0 if there is an unused call-clobbered register available
4977    for the entire function.  */
4978
4979 static unsigned int
4980 ix86_select_alt_pic_regnum (void)
4981 {
4982   if (current_function_is_leaf && !current_function_profile
4983       && !ix86_current_function_calls_tls_descriptor)
4984     {
4985       int i;
4986       for (i = 2; i >= 0; --i)
4987         if (!regs_ever_live[i])
4988           return i;
4989     }
4990
4991   return INVALID_REGNUM;
4992 }
4993
4994 /* Return 1 if we need to save REGNO.  */
4995 static int
4996 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4997 {
4998   if (pic_offset_table_rtx
4999       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
5000       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5001           || current_function_profile
5002           || current_function_calls_eh_return
5003           || current_function_uses_const_pool))
5004     {
5005       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
5006         return 0;
5007       return 1;
5008     }
5009
5010   if (current_function_calls_eh_return && maybe_eh_return)
5011     {
5012       unsigned i;
5013       for (i = 0; ; i++)
5014         {
5015           unsigned test = EH_RETURN_DATA_REGNO (i);
5016           if (test == INVALID_REGNUM)
5017             break;
5018           if (test == regno)
5019             return 1;
5020         }
5021     }
5022
5023   if (cfun->machine->force_align_arg_pointer
5024       && regno == REGNO (cfun->machine->force_align_arg_pointer))
5025     return 1;
5026
5027   return (regs_ever_live[regno]
5028           && !call_used_regs[regno]
5029           && !fixed_regs[regno]
5030           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5031 }
5032
5033 /* Return number of registers to be saved on the stack.  */
5034
5035 static int
5036 ix86_nsaved_regs (void)
5037 {
5038   int nregs = 0;
5039   int regno;
5040
5041   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5042     if (ix86_save_reg (regno, true))
5043       nregs++;
5044   return nregs;
5045 }
5046
5047 /* Return the offset between two registers, one to be eliminated, and the other
5048    its replacement, at the start of a routine.  */
5049
5050 HOST_WIDE_INT
5051 ix86_initial_elimination_offset (int from, int to)
5052 {
5053   struct ix86_frame frame;
5054   ix86_compute_frame_layout (&frame);
5055
5056   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5057     return frame.hard_frame_pointer_offset;
5058   else if (from == FRAME_POINTER_REGNUM
5059            && to == HARD_FRAME_POINTER_REGNUM)
5060     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5061   else
5062     {
5063       gcc_assert (to == STACK_POINTER_REGNUM);
5064
5065       if (from == ARG_POINTER_REGNUM)
5066         return frame.stack_pointer_offset;
5067
5068       gcc_assert (from == FRAME_POINTER_REGNUM);
5069       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5070     }
5071 }
5072
5073 /* Fill structure ix86_frame about frame of currently computed function.  */
5074
5075 static void
5076 ix86_compute_frame_layout (struct ix86_frame *frame)
5077 {
5078   HOST_WIDE_INT total_size;
5079   unsigned int stack_alignment_needed;
5080   HOST_WIDE_INT offset;
5081   unsigned int preferred_alignment;
5082   HOST_WIDE_INT size = get_frame_size ();
5083
5084   frame->nregs = ix86_nsaved_regs ();
5085   total_size = size;
5086
5087   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5088   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5089
5090   /* During reload iteration the amount of registers saved can change.
5091      Recompute the value as needed.  Do not recompute when amount of registers
5092      didn't change as reload does multiple calls to the function and does not
5093      expect the decision to change within single iteration.  */
5094   if (!optimize_size
5095       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5096     {
5097       int count = frame->nregs;
5098
5099       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5100       /* The fast prologue uses move instead of push to save registers.  This
5101          is significantly longer, but also executes faster as modern hardware
5102          can execute the moves in parallel, but can't do that for push/pop.
5103
5104          Be careful about choosing what prologue to emit:  When function takes
5105          many instructions to execute we may use slow version as well as in
5106          case function is known to be outside hot spot (this is known with
5107          feedback only).  Weight the size of function by number of registers
5108          to save as it is cheap to use one or two push instructions but very
5109          slow to use many of them.  */
5110       if (count)
5111         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5112       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5113           || (flag_branch_probabilities
5114               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5115         cfun->machine->use_fast_prologue_epilogue = false;
5116       else
5117         cfun->machine->use_fast_prologue_epilogue
5118            = !expensive_function_p (count);
5119     }
5120   if (TARGET_PROLOGUE_USING_MOVE
5121       && cfun->machine->use_fast_prologue_epilogue)
5122     frame->save_regs_using_mov = true;
5123   else
5124     frame->save_regs_using_mov = false;
5125
5126
5127   /* Skip return address and saved base pointer.  */
5128   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5129
5130   frame->hard_frame_pointer_offset = offset;
5131
5132   /* Do some sanity checking of stack_alignment_needed and
5133      preferred_alignment, since i386 port is the only using those features
5134      that may break easily.  */
5135
5136   gcc_assert (!size || stack_alignment_needed);
5137   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5138   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5139   gcc_assert (stack_alignment_needed
5140               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5141
5142   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5143     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5144
5145   /* Register save area */
5146   offset += frame->nregs * UNITS_PER_WORD;
5147
5148   /* Va-arg area */
5149   if (ix86_save_varrargs_registers)
5150     {
5151       offset += X86_64_VARARGS_SIZE;
5152       frame->va_arg_size = X86_64_VARARGS_SIZE;
5153     }
5154   else
5155     frame->va_arg_size = 0;
5156
5157   /* Align start of frame for local function.  */
5158   frame->padding1 = ((offset + stack_alignment_needed - 1)
5159                      & -stack_alignment_needed) - offset;
5160
5161   offset += frame->padding1;
5162
5163   /* Frame pointer points here.  */
5164   frame->frame_pointer_offset = offset;
5165
5166   offset += size;
5167
5168   /* Add outgoing arguments area.  Can be skipped if we eliminated
5169      all the function calls as dead code.
5170      Skipping is however impossible when function calls alloca.  Alloca
5171      expander assumes that last current_function_outgoing_args_size
5172      of stack frame are unused.  */
5173   if (ACCUMULATE_OUTGOING_ARGS
5174       && (!current_function_is_leaf || current_function_calls_alloca
5175           || ix86_current_function_calls_tls_descriptor))
5176     {
5177       offset += current_function_outgoing_args_size;
5178       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5179     }
5180   else
5181     frame->outgoing_arguments_size = 0;
5182
5183   /* Align stack boundary.  Only needed if we're calling another function
5184      or using alloca.  */
5185   if (!current_function_is_leaf || current_function_calls_alloca
5186       || ix86_current_function_calls_tls_descriptor)
5187     frame->padding2 = ((offset + preferred_alignment - 1)
5188                        & -preferred_alignment) - offset;
5189   else
5190     frame->padding2 = 0;
5191
5192   offset += frame->padding2;
5193
5194   /* We've reached end of stack frame.  */
5195   frame->stack_pointer_offset = offset;
5196
5197   /* Size prologue needs to allocate.  */
5198   frame->to_allocate =
5199     (size + frame->padding1 + frame->padding2
5200      + frame->outgoing_arguments_size + frame->va_arg_size);
5201
5202   if ((!frame->to_allocate && frame->nregs <= 1)
5203       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5204     frame->save_regs_using_mov = false;
5205
5206   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5207       && current_function_is_leaf
5208       && !ix86_current_function_calls_tls_descriptor)
5209     {
5210       frame->red_zone_size = frame->to_allocate;
5211       if (frame->save_regs_using_mov)
5212         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5213       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5214         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5215     }
5216   else
5217     frame->red_zone_size = 0;
5218   frame->to_allocate -= frame->red_zone_size;
5219   frame->stack_pointer_offset -= frame->red_zone_size;
5220 #if 0
5221   fprintf (stderr, "nregs: %i\n", frame->nregs);
5222   fprintf (stderr, "size: %i\n", size);
5223   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
5224   fprintf (stderr, "padding1: %i\n", frame->padding1);
5225   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
5226   fprintf (stderr, "padding2: %i\n", frame->padding2);
5227   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
5228   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
5229   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
5230   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
5231            frame->hard_frame_pointer_offset);
5232   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
5233 #endif
5234 }
5235
5236 /* Emit code to save registers in the prologue.  */
5237
5238 static void
5239 ix86_emit_save_regs (void)
5240 {
5241   unsigned int regno;
5242   rtx insn;
5243
5244   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5245     if (ix86_save_reg (regno, true))
5246       {
5247         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5248         RTX_FRAME_RELATED_P (insn) = 1;
5249       }
5250 }
5251
5252 /* Emit code to save registers using MOV insns.  First register
5253    is restored from POINTER + OFFSET.  */
5254 static void
5255 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5256 {
5257   unsigned int regno;
5258   rtx insn;
5259
5260   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5261     if (ix86_save_reg (regno, true))
5262       {
5263         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5264                                                Pmode, offset),
5265                                gen_rtx_REG (Pmode, regno));
5266         RTX_FRAME_RELATED_P (insn) = 1;
5267         offset += UNITS_PER_WORD;
5268       }
5269 }
5270
5271 /* Expand prologue or epilogue stack adjustment.
5272    The pattern exist to put a dependency on all ebp-based memory accesses.
5273    STYLE should be negative if instructions should be marked as frame related,
5274    zero if %r11 register is live and cannot be freely used and positive
5275    otherwise.  */
5276
5277 static void
5278 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5279 {
5280   rtx insn;
5281
5282   if (! TARGET_64BIT)
5283     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5284   else if (x86_64_immediate_operand (offset, DImode))
5285     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5286   else
5287     {
5288       rtx r11;
5289       /* r11 is used by indirect sibcall return as well, set before the
5290          epilogue and used after the epilogue.  ATM indirect sibcall
5291          shouldn't be used together with huge frame sizes in one
5292          function because of the frame_size check in sibcall.c.  */
5293       gcc_assert (style);
5294       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5295       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5296       if (style < 0)
5297         RTX_FRAME_RELATED_P (insn) = 1;
5298       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5299                                                                offset));
5300     }
5301   if (style < 0)
5302     RTX_FRAME_RELATED_P (insn) = 1;
5303 }
5304
5305 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5306
5307 static rtx
5308 ix86_internal_arg_pointer (void)
5309 {
5310   bool has_force_align_arg_pointer =
5311     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5312                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5313   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5314        && DECL_NAME (current_function_decl)
5315        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5316        && DECL_FILE_SCOPE_P (current_function_decl))
5317       || ix86_force_align_arg_pointer
5318       || has_force_align_arg_pointer)
5319     {
5320       /* Nested functions can't realign the stack due to a register
5321          conflict.  */
5322       if (DECL_CONTEXT (current_function_decl)
5323           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5324         {
5325           if (ix86_force_align_arg_pointer)
5326             warning (0, "-mstackrealign ignored for nested functions");
5327           if (has_force_align_arg_pointer)
5328             error ("%s not supported for nested functions",
5329                    ix86_force_align_arg_pointer_string);
5330           return virtual_incoming_args_rtx;
5331         }
5332       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5333       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5334     }
5335   else
5336     return virtual_incoming_args_rtx;
5337 }
5338
5339 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5340    This is called from dwarf2out.c to emit call frame instructions
5341    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5342 static void
5343 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5344 {
5345   rtx unspec = SET_SRC (pattern);
5346   gcc_assert (GET_CODE (unspec) == UNSPEC);
5347
5348   switch (index)
5349     {
5350     case UNSPEC_REG_SAVE:
5351       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5352                               SET_DEST (pattern));
5353       break;
5354     case UNSPEC_DEF_CFA:
5355       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5356                          INTVAL (XVECEXP (unspec, 0, 0)));
5357       break;
5358     default:
5359       gcc_unreachable ();
5360     }
5361 }
5362
5363 /* Expand the prologue into a bunch of separate insns.  */
5364
5365 void
5366 ix86_expand_prologue (void)
5367 {
5368   rtx insn;
5369   bool pic_reg_used;
5370   struct ix86_frame frame;
5371   HOST_WIDE_INT allocate;
5372
5373   ix86_compute_frame_layout (&frame);
5374
5375   if (cfun->machine->force_align_arg_pointer)
5376     {
5377       rtx x, y;
5378
5379       /* Grab the argument pointer.  */
5380       x = plus_constant (stack_pointer_rtx, 4);
5381       y = cfun->machine->force_align_arg_pointer;
5382       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5383       RTX_FRAME_RELATED_P (insn) = 1;
5384
5385       /* The unwind info consists of two parts: install the fafp as the cfa,
5386          and record the fafp as the "save register" of the stack pointer.
5387          The later is there in order that the unwinder can see where it
5388          should restore the stack pointer across the and insn.  */
5389       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5390       x = gen_rtx_SET (VOIDmode, y, x);
5391       RTX_FRAME_RELATED_P (x) = 1;
5392       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5393                           UNSPEC_REG_SAVE);
5394       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5395       RTX_FRAME_RELATED_P (y) = 1;
5396       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5397       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5398       REG_NOTES (insn) = x;
5399
5400       /* Align the stack.  */
5401       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5402                              GEN_INT (-16)));
5403
5404       /* And here we cheat like madmen with the unwind info.  We force the
5405          cfa register back to sp+4, which is exactly what it was at the
5406          start of the function.  Re-pushing the return address results in
5407          the return at the same spot relative to the cfa, and thus is
5408          correct wrt the unwind info.  */
5409       x = cfun->machine->force_align_arg_pointer;
5410       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5411       insn = emit_insn (gen_push (x));
5412       RTX_FRAME_RELATED_P (insn) = 1;
5413
5414       x = GEN_INT (4);
5415       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5416       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5417       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5418       REG_NOTES (insn) = x;
5419     }
5420
5421   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5422      slower on all targets.  Also sdb doesn't like it.  */
5423
5424   if (frame_pointer_needed)
5425     {
5426       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5427       RTX_FRAME_RELATED_P (insn) = 1;
5428
5429       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5430       RTX_FRAME_RELATED_P (insn) = 1;
5431     }
5432
5433   allocate = frame.to_allocate;
5434
5435   if (!frame.save_regs_using_mov)
5436     ix86_emit_save_regs ();
5437   else
5438     allocate += frame.nregs * UNITS_PER_WORD;
5439
5440   /* When using red zone we may start register saving before allocating
5441      the stack frame saving one cycle of the prologue.  */
5442   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5443     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5444                                    : stack_pointer_rtx,
5445                                    -frame.nregs * UNITS_PER_WORD);
5446
5447   if (allocate == 0)
5448     ;
5449   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5450     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5451                                GEN_INT (-allocate), -1);
5452   else
5453     {
5454       /* Only valid for Win32.  */
5455       rtx eax = gen_rtx_REG (SImode, 0);
5456       bool eax_live = ix86_eax_live_at_start_p ();
5457       rtx t;
5458
5459       gcc_assert (!TARGET_64BIT);
5460
5461       if (eax_live)
5462         {
5463           emit_insn (gen_push (eax));
5464           allocate -= 4;
5465         }
5466
5467       emit_move_insn (eax, GEN_INT (allocate));
5468
5469       insn = emit_insn (gen_allocate_stack_worker (eax));
5470       RTX_FRAME_RELATED_P (insn) = 1;
5471       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5472       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5473       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5474                                             t, REG_NOTES (insn));
5475
5476       if (eax_live)
5477         {
5478           if (frame_pointer_needed)
5479             t = plus_constant (hard_frame_pointer_rtx,
5480                                allocate
5481                                - frame.to_allocate
5482                                - frame.nregs * UNITS_PER_WORD);
5483           else
5484             t = plus_constant (stack_pointer_rtx, allocate);
5485           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5486         }
5487     }
5488
5489   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5490     {
5491       if (!frame_pointer_needed || !frame.to_allocate)
5492         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5493       else
5494         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5495                                        -frame.nregs * UNITS_PER_WORD);
5496     }
5497
5498   pic_reg_used = false;
5499   if (pic_offset_table_rtx
5500       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5501           || current_function_profile))
5502     {
5503       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5504
5505       if (alt_pic_reg_used != INVALID_REGNUM)
5506         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5507
5508       pic_reg_used = true;
5509     }
5510
5511   if (pic_reg_used)
5512     {
5513       if (TARGET_64BIT)
5514         insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5515       else
5516         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5517
5518       /* Even with accurate pre-reload life analysis, we can wind up
5519          deleting all references to the pic register after reload.
5520          Consider if cross-jumping unifies two sides of a branch
5521          controlled by a comparison vs the only read from a global.
5522          In which case, allow the set_got to be deleted, though we're
5523          too late to do anything about the ebx save in the prologue.  */
5524       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5525     }
5526
5527   /* Prevent function calls from be scheduled before the call to mcount.
5528      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5529   if (current_function_profile)
5530     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5531 }
5532
5533 /* Emit code to restore saved registers using MOV insns.  First register
5534    is restored from POINTER + OFFSET.  */
5535 static void
5536 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5537                                   int maybe_eh_return)
5538 {
5539   int regno;
5540   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5541
5542   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5543     if (ix86_save_reg (regno, maybe_eh_return))
5544       {
5545         /* Ensure that adjust_address won't be forced to produce pointer
5546            out of range allowed by x86-64 instruction set.  */
5547         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5548           {
5549             rtx r11;
5550
5551             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5552             emit_move_insn (r11, GEN_INT (offset));
5553             emit_insn (gen_adddi3 (r11, r11, pointer));
5554             base_address = gen_rtx_MEM (Pmode, r11);
5555             offset = 0;
5556           }
5557         emit_move_insn (gen_rtx_REG (Pmode, regno),
5558                         adjust_address (base_address, Pmode, offset));
5559         offset += UNITS_PER_WORD;
5560       }
5561 }
5562
5563 /* Restore function stack, frame, and registers.  */
5564
5565 void
5566 ix86_expand_epilogue (int style)
5567 {
5568   int regno;
5569   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5570   struct ix86_frame frame;
5571   HOST_WIDE_INT offset;
5572
5573   ix86_compute_frame_layout (&frame);
5574
5575   /* Calculate start of saved registers relative to ebp.  Special care
5576      must be taken for the normal return case of a function using
5577      eh_return: the eax and edx registers are marked as saved, but not
5578      restored along this path.  */
5579   offset = frame.nregs;
5580   if (current_function_calls_eh_return && style != 2)
5581     offset -= 2;
5582   offset *= -UNITS_PER_WORD;
5583
5584   /* If we're only restoring one register and sp is not valid then
5585      using a move instruction to restore the register since it's
5586      less work than reloading sp and popping the register.
5587
5588      The default code result in stack adjustment using add/lea instruction,
5589      while this code results in LEAVE instruction (or discrete equivalent),
5590      so it is profitable in some other cases as well.  Especially when there
5591      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5592      and there is exactly one register to pop. This heuristic may need some
5593      tuning in future.  */
5594   if ((!sp_valid && frame.nregs <= 1)
5595       || (TARGET_EPILOGUE_USING_MOVE
5596           && cfun->machine->use_fast_prologue_epilogue
5597           && (frame.nregs > 1 || frame.to_allocate))
5598       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5599       || (frame_pointer_needed && TARGET_USE_LEAVE
5600           && cfun->machine->use_fast_prologue_epilogue
5601           && frame.nregs == 1)
5602       || current_function_calls_eh_return)
5603     {
5604       /* Restore registers.  We can use ebp or esp to address the memory
5605          locations.  If both are available, default to ebp, since offsets
5606          are known to be small.  Only exception is esp pointing directly to the
5607          end of block of saved registers, where we may simplify addressing
5608          mode.  */
5609
5610       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5611         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5612                                           frame.to_allocate, style == 2);
5613       else
5614         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5615                                           offset, style == 2);
5616
5617       /* eh_return epilogues need %ecx added to the stack pointer.  */
5618       if (style == 2)
5619         {
5620           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5621
5622           if (frame_pointer_needed)
5623             {
5624               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5625               tmp = plus_constant (tmp, UNITS_PER_WORD);
5626               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5627
5628               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5629               emit_move_insn (hard_frame_pointer_rtx, tmp);
5630
5631               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5632                                          const0_rtx, style);
5633             }
5634           else
5635             {
5636               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5637               tmp = plus_constant (tmp, (frame.to_allocate
5638                                          + frame.nregs * UNITS_PER_WORD));
5639               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5640             }
5641         }
5642       else if (!frame_pointer_needed)
5643         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5644                                    GEN_INT (frame.to_allocate
5645                                             + frame.nregs * UNITS_PER_WORD),
5646                                    style);
5647       /* If not an i386, mov & pop is faster than "leave".  */
5648       else if (TARGET_USE_LEAVE || optimize_size
5649                || !cfun->machine->use_fast_prologue_epilogue)
5650         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5651       else
5652         {
5653           pro_epilogue_adjust_stack (stack_pointer_rtx,
5654                                      hard_frame_pointer_rtx,
5655                                      const0_rtx, style);
5656           if (TARGET_64BIT)
5657             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5658           else
5659             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5660         }
5661     }
5662   else
5663     {
5664       /* First step is to deallocate the stack frame so that we can
5665          pop the registers.  */
5666       if (!sp_valid)
5667         {
5668           gcc_assert (frame_pointer_needed);
5669           pro_epilogue_adjust_stack (stack_pointer_rtx,
5670                                      hard_frame_pointer_rtx,
5671                                      GEN_INT (offset), style);
5672         }
5673       else if (frame.to_allocate)
5674         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5675                                    GEN_INT (frame.to_allocate), style);
5676
5677       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5678         if (ix86_save_reg (regno, false))
5679           {
5680             if (TARGET_64BIT)
5681               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5682             else
5683               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5684           }
5685       if (frame_pointer_needed)
5686         {
5687           /* Leave results in shorter dependency chains on CPUs that are
5688              able to grok it fast.  */
5689           if (TARGET_USE_LEAVE)
5690             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5691           else if (TARGET_64BIT)
5692             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5693           else
5694             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5695         }
5696     }
5697
5698   if (cfun->machine->force_align_arg_pointer)
5699     {
5700       emit_insn (gen_addsi3 (stack_pointer_rtx,
5701                              cfun->machine->force_align_arg_pointer,
5702                              GEN_INT (-4)));
5703     }
5704
5705   /* Sibcall epilogues don't want a return instruction.  */
5706   if (style == 0)
5707     return;
5708
5709   if (current_function_pops_args && current_function_args_size)
5710     {
5711       rtx popc = GEN_INT (current_function_pops_args);
5712
5713       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5714          return address, do explicit add, and jump indirectly to the
5715          caller.  */
5716
5717       if (current_function_pops_args >= 65536)
5718         {
5719           rtx ecx = gen_rtx_REG (SImode, 2);
5720
5721           /* There is no "pascal" calling convention in 64bit ABI.  */
5722           gcc_assert (!TARGET_64BIT);
5723
5724           emit_insn (gen_popsi1 (ecx));
5725           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5726           emit_jump_insn (gen_return_indirect_internal (ecx));
5727         }
5728       else
5729         emit_jump_insn (gen_return_pop_internal (popc));
5730     }
5731   else
5732     emit_jump_insn (gen_return_internal ());
5733 }
5734
5735 /* Reset from the function's potential modifications.  */
5736
5737 static void
5738 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5739                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5740 {
5741   if (pic_offset_table_rtx)
5742     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5743 #if TARGET_MACHO
5744   /* Mach-O doesn't support labels at the end of objects, so if
5745      it looks like we might want one, insert a NOP.  */
5746   {
5747     rtx insn = get_last_insn ();
5748     while (insn
5749            && NOTE_P (insn)
5750            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
5751       insn = PREV_INSN (insn);
5752     if (insn
5753         && (LABEL_P (insn)
5754             || (NOTE_P (insn)
5755                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
5756       fputs ("\tnop\n", file);
5757   }
5758 #endif
5759
5760 }
5761 \f
5762 /* Extract the parts of an RTL expression that is a valid memory address
5763    for an instruction.  Return 0 if the structure of the address is
5764    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5765    strictly valid, but still used for computing length of lea instruction.  */
5766
5767 int
5768 ix86_decompose_address (rtx addr, struct ix86_address *out)
5769 {
5770   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
5771   rtx base_reg, index_reg;
5772   HOST_WIDE_INT scale = 1;
5773   rtx scale_rtx = NULL_RTX;
5774   int retval = 1;
5775   enum ix86_address_seg seg = SEG_DEFAULT;
5776
5777   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
5778     base = addr;
5779   else if (GET_CODE (addr) == PLUS)
5780     {
5781       rtx addends[4], op;
5782       int n = 0, i;
5783
5784       op = addr;
5785       do
5786         {
5787           if (n >= 4)
5788             return 0;
5789           addends[n++] = XEXP (op, 1);
5790           op = XEXP (op, 0);
5791         }
5792       while (GET_CODE (op) == PLUS);
5793       if (n >= 4)
5794         return 0;
5795       addends[n] = op;
5796
5797       for (i = n; i >= 0; --i)
5798         {
5799           op = addends[i];
5800           switch (GET_CODE (op))
5801             {
5802             case MULT:
5803               if (index)
5804                 return 0;
5805               index = XEXP (op, 0);
5806               scale_rtx = XEXP (op, 1);
5807               break;
5808
5809             case UNSPEC:
5810               if (XINT (op, 1) == UNSPEC_TP
5811                   && TARGET_TLS_DIRECT_SEG_REFS
5812                   && seg == SEG_DEFAULT)
5813                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
5814               else
5815                 return 0;
5816               break;
5817
5818             case REG:
5819             case SUBREG:
5820               if (!base)
5821                 base = op;
5822               else if (!index)
5823                 index = op;
5824               else
5825                 return 0;
5826               break;
5827
5828             case CONST:
5829             case CONST_INT:
5830             case SYMBOL_REF:
5831             case LABEL_REF:
5832               if (disp)
5833                 return 0;
5834               disp = op;
5835               break;
5836
5837             default:
5838               return 0;
5839             }
5840         }
5841     }
5842   else if (GET_CODE (addr) == MULT)
5843     {
5844       index = XEXP (addr, 0);           /* index*scale */
5845       scale_rtx = XEXP (addr, 1);
5846     }
5847   else if (GET_CODE (addr) == ASHIFT)
5848     {
5849       rtx tmp;
5850
5851       /* We're called for lea too, which implements ashift on occasion.  */
5852       index = XEXP (addr, 0);
5853       tmp = XEXP (addr, 1);
5854       if (GET_CODE (tmp) != CONST_INT)
5855         return 0;
5856       scale = INTVAL (tmp);
5857       if ((unsigned HOST_WIDE_INT) scale > 3)
5858         return 0;
5859       scale = 1 << scale;
5860       retval = -1;
5861     }
5862   else
5863     disp = addr;                        /* displacement */
5864
5865   /* Extract the integral value of scale.  */
5866   if (scale_rtx)
5867     {
5868       if (GET_CODE (scale_rtx) != CONST_INT)
5869         return 0;
5870       scale = INTVAL (scale_rtx);
5871     }
5872
5873   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
5874   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
5875
5876   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
5877   if (base_reg && index_reg && scale == 1
5878       && (index_reg == arg_pointer_rtx
5879           || index_reg == frame_pointer_rtx
5880           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
5881     {
5882       rtx tmp;
5883       tmp = base, base = index, index = tmp;
5884       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
5885     }
5886
5887   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5888   if ((base_reg == hard_frame_pointer_rtx
5889        || base_reg == frame_pointer_rtx
5890        || base_reg == arg_pointer_rtx) && !disp)
5891     disp = const0_rtx;
5892
5893   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5894      Avoid this by transforming to [%esi+0].  */
5895   if (ix86_tune == PROCESSOR_K6 && !optimize_size
5896       && base_reg && !index_reg && !disp
5897       && REG_P (base_reg)
5898       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
5899     disp = const0_rtx;
5900
5901   /* Special case: encode reg+reg instead of reg*2.  */
5902   if (!base && index && scale && scale == 2)
5903     base = index, base_reg = index_reg, scale = 1;
5904
5905   /* Special case: scaling cannot be encoded without base or displacement.  */
5906   if (!base && !disp && index && scale != 1)
5907     disp = const0_rtx;
5908
5909   out->base = base;
5910   out->index = index;
5911   out->disp = disp;
5912   out->scale = scale;
5913   out->seg = seg;
5914
5915   return retval;
5916 }
5917 \f
5918 /* Return cost of the memory address x.
5919    For i386, it is better to use a complex address than let gcc copy
5920    the address into a reg and make a new pseudo.  But not if the address
5921    requires to two regs - that would mean more pseudos with longer
5922    lifetimes.  */
5923 static int
5924 ix86_address_cost (rtx x)
5925 {
5926   struct ix86_address parts;
5927   int cost = 1;
5928   int ok = ix86_decompose_address (x, &parts);
5929
5930   gcc_assert (ok);
5931
5932   if (parts.base && GET_CODE (parts.base) == SUBREG)
5933     parts.base = SUBREG_REG (parts.base);
5934   if (parts.index && GET_CODE (parts.index) == SUBREG)
5935     parts.index = SUBREG_REG (parts.index);
5936
5937   /* More complex memory references are better.  */
5938   if (parts.disp && parts.disp != const0_rtx)
5939     cost--;
5940   if (parts.seg != SEG_DEFAULT)
5941     cost--;
5942
5943   /* Attempt to minimize number of registers in the address.  */
5944   if ((parts.base
5945        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5946       || (parts.index
5947           && (!REG_P (parts.index)
5948               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5949     cost++;
5950
5951   if (parts.base
5952       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5953       && parts.index
5954       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5955       && parts.base != parts.index)
5956     cost++;
5957
5958   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5959      since it's predecode logic can't detect the length of instructions
5960      and it degenerates to vector decoded.  Increase cost of such
5961      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5962      to split such addresses or even refuse such addresses at all.
5963
5964      Following addressing modes are affected:
5965       [base+scale*index]
5966       [scale*index+disp]
5967       [base+index]
5968
5969      The first and last case  may be avoidable by explicitly coding the zero in
5970      memory address, but I don't have AMD-K6 machine handy to check this
5971      theory.  */
5972
5973   if (TARGET_K6
5974       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5975           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5976           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5977     cost += 10;
5978
5979   return cost;
5980 }
5981 \f
5982 /* If X is a machine specific address (i.e. a symbol or label being
5983    referenced as a displacement from the GOT implemented using an
5984    UNSPEC), then return the base term.  Otherwise return X.  */
5985
5986 rtx
5987 ix86_find_base_term (rtx x)
5988 {
5989   rtx term;
5990
5991   if (TARGET_64BIT)
5992     {
5993       if (GET_CODE (x) != CONST)
5994         return x;
5995       term = XEXP (x, 0);
5996       if (GET_CODE (term) == PLUS
5997           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5998               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5999         term = XEXP (term, 0);
6000       if (GET_CODE (term) != UNSPEC
6001           || XINT (term, 1) != UNSPEC_GOTPCREL)
6002         return x;
6003
6004       term = XVECEXP (term, 0, 0);
6005
6006       if (GET_CODE (term) != SYMBOL_REF
6007           && GET_CODE (term) != LABEL_REF)
6008         return x;
6009
6010       return term;
6011     }
6012
6013   term = ix86_delegitimize_address (x);
6014
6015   if (GET_CODE (term) != SYMBOL_REF
6016       && GET_CODE (term) != LABEL_REF)
6017     return x;
6018
6019   return term;
6020 }
6021
6022 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
6023    this is used for to form addresses to local data when -fPIC is in
6024    use.  */
6025
6026 static bool
6027 darwin_local_data_pic (rtx disp)
6028 {
6029   if (GET_CODE (disp) == MINUS)
6030     {
6031       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6032           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6033         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6034           {
6035             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6036             if (! strcmp (sym_name, "<pic base>"))
6037               return true;
6038           }
6039     }
6040
6041   return false;
6042 }
6043 \f
6044 /* Determine if a given RTX is a valid constant.  We already know this
6045    satisfies CONSTANT_P.  */
6046
6047 bool
6048 legitimate_constant_p (rtx x)
6049 {
6050   switch (GET_CODE (x))
6051     {
6052     case CONST:
6053       x = XEXP (x, 0);
6054
6055       if (GET_CODE (x) == PLUS)
6056         {
6057           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6058             return false;
6059           x = XEXP (x, 0);
6060         }
6061
6062       if (TARGET_MACHO && darwin_local_data_pic (x))
6063         return true;
6064
6065       /* Only some unspecs are valid as "constants".  */
6066       if (GET_CODE (x) == UNSPEC)
6067         switch (XINT (x, 1))
6068           {
6069           case UNSPEC_GOTOFF:
6070             return TARGET_64BIT;
6071           case UNSPEC_TPOFF:
6072           case UNSPEC_NTPOFF:
6073             x = XVECEXP (x, 0, 0);
6074             return (GET_CODE (x) == SYMBOL_REF
6075                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6076           case UNSPEC_DTPOFF:
6077             x = XVECEXP (x, 0, 0);
6078             return (GET_CODE (x) == SYMBOL_REF
6079                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6080           default:
6081             return false;
6082           }
6083
6084       /* We must have drilled down to a symbol.  */
6085       if (GET_CODE (x) == LABEL_REF)
6086         return true;
6087       if (GET_CODE (x) != SYMBOL_REF)
6088         return false;
6089       /* FALLTHRU */
6090
6091     case SYMBOL_REF:
6092       /* TLS symbols are never valid.  */
6093       if (SYMBOL_REF_TLS_MODEL (x))
6094         return false;
6095       break;
6096
6097     case CONST_DOUBLE:
6098       if (GET_MODE (x) == TImode
6099           && x != CONST0_RTX (TImode)
6100           && !TARGET_64BIT)
6101         return false;
6102       break;
6103
6104     case CONST_VECTOR:
6105       if (x == CONST0_RTX (GET_MODE (x)))
6106         return true;
6107       return false;
6108
6109     default:
6110       break;
6111     }
6112
6113   /* Otherwise we handle everything else in the move patterns.  */
6114   return true;
6115 }
6116
6117 /* Determine if it's legal to put X into the constant pool.  This
6118    is not possible for the address of thread-local symbols, which
6119    is checked above.  */
6120
6121 static bool
6122 ix86_cannot_force_const_mem (rtx x)
6123 {
6124   /* We can always put integral constants and vectors in memory.  */
6125   switch (GET_CODE (x))
6126     {
6127     case CONST_INT:
6128     case CONST_DOUBLE:
6129     case CONST_VECTOR:
6130       return false;
6131
6132     default:
6133       break;
6134     }
6135   return !legitimate_constant_p (x);
6136 }
6137
6138 /* Determine if a given RTX is a valid constant address.  */
6139
6140 bool
6141 constant_address_p (rtx x)
6142 {
6143   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6144 }
6145
6146 /* Nonzero if the constant value X is a legitimate general operand
6147    when generating PIC code.  It is given that flag_pic is on and
6148    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6149
6150 bool
6151 legitimate_pic_operand_p (rtx x)
6152 {
6153   rtx inner;
6154
6155   switch (GET_CODE (x))
6156     {
6157     case CONST:
6158       inner = XEXP (x, 0);
6159       if (GET_CODE (inner) == PLUS
6160           && GET_CODE (XEXP (inner, 1)) == CONST_INT)
6161         inner = XEXP (inner, 0);
6162
6163       /* Only some unspecs are valid as "constants".  */
6164       if (GET_CODE (inner) == UNSPEC)
6165         switch (XINT (inner, 1))
6166           {
6167           case UNSPEC_GOTOFF:
6168             return TARGET_64BIT;
6169           case UNSPEC_TPOFF:
6170             x = XVECEXP (inner, 0, 0);
6171             return (GET_CODE (x) == SYMBOL_REF
6172                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6173           default:
6174             return false;
6175           }
6176       /* FALLTHRU */
6177
6178     case SYMBOL_REF:
6179     case LABEL_REF:
6180       return legitimate_pic_address_disp_p (x);
6181
6182     default:
6183       return true;
6184     }
6185 }
6186
6187 /* Determine if a given CONST RTX is a valid memory displacement
6188    in PIC mode.  */
6189
6190 int
6191 legitimate_pic_address_disp_p (rtx disp)
6192 {
6193   bool saw_plus;
6194
6195   /* In 64bit mode we can allow direct addresses of symbols and labels
6196      when they are not dynamic symbols.  */
6197   if (TARGET_64BIT)
6198     {
6199       rtx op0 = disp, op1;
6200
6201       switch (GET_CODE (disp))
6202         {
6203         case LABEL_REF:
6204           return true;
6205
6206         case CONST:
6207           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6208             break;
6209           op0 = XEXP (XEXP (disp, 0), 0);
6210           op1 = XEXP (XEXP (disp, 0), 1);
6211           if (GET_CODE (op1) != CONST_INT
6212               || INTVAL (op1) >= 16*1024*1024
6213               || INTVAL (op1) < -16*1024*1024)
6214             break;
6215           if (GET_CODE (op0) == LABEL_REF)
6216             return true;
6217           if (GET_CODE (op0) != SYMBOL_REF)
6218             break;
6219           /* FALLTHRU */
6220
6221         case SYMBOL_REF:
6222           /* TLS references should always be enclosed in UNSPEC.  */
6223           if (SYMBOL_REF_TLS_MODEL (op0))
6224             return false;
6225           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0))
6226             return true;
6227           break;
6228
6229         default:
6230           break;
6231         }
6232     }
6233   if (GET_CODE (disp) != CONST)
6234     return 0;
6235   disp = XEXP (disp, 0);
6236
6237   if (TARGET_64BIT)
6238     {
6239       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6240          of GOT tables.  We should not need these anyway.  */
6241       if (GET_CODE (disp) != UNSPEC
6242           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6243               && XINT (disp, 1) != UNSPEC_GOTOFF))
6244         return 0;
6245
6246       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6247           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6248         return 0;
6249       return 1;
6250     }
6251
6252   saw_plus = false;
6253   if (GET_CODE (disp) == PLUS)
6254     {
6255       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
6256         return 0;
6257       disp = XEXP (disp, 0);
6258       saw_plus = true;
6259     }
6260
6261   if (TARGET_MACHO && darwin_local_data_pic (disp))
6262     return 1;
6263
6264   if (GET_CODE (disp) != UNSPEC)
6265     return 0;
6266
6267   switch (XINT (disp, 1))
6268     {
6269     case UNSPEC_GOT:
6270       if (saw_plus)
6271         return false;
6272       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
6273     case UNSPEC_GOTOFF:
6274       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6275          While ABI specify also 32bit relocation but we don't produce it in
6276          small PIC model at all.  */
6277       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6278            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6279           && !TARGET_64BIT)
6280         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
6281       return false;
6282     case UNSPEC_GOTTPOFF:
6283     case UNSPEC_GOTNTPOFF:
6284     case UNSPEC_INDNTPOFF:
6285       if (saw_plus)
6286         return false;
6287       disp = XVECEXP (disp, 0, 0);
6288       return (GET_CODE (disp) == SYMBOL_REF
6289               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6290     case UNSPEC_NTPOFF:
6291       disp = XVECEXP (disp, 0, 0);
6292       return (GET_CODE (disp) == SYMBOL_REF
6293               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6294     case UNSPEC_DTPOFF:
6295       disp = XVECEXP (disp, 0, 0);
6296       return (GET_CODE (disp) == SYMBOL_REF
6297               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6298     }
6299
6300   return 0;
6301 }
6302
6303 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6304    memory address for an instruction.  The MODE argument is the machine mode
6305    for the MEM expression that wants to use this address.
6306
6307    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6308    convert common non-canonical forms to canonical form so that they will
6309    be recognized.  */
6310
6311 int
6312 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
6313 {
6314   struct ix86_address parts;
6315   rtx base, index, disp;
6316   HOST_WIDE_INT scale;
6317   const char *reason = NULL;
6318   rtx reason_rtx = NULL_RTX;
6319
6320   if (TARGET_DEBUG_ADDR)
6321     {
6322       fprintf (stderr,
6323                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
6324                GET_MODE_NAME (mode), strict);
6325       debug_rtx (addr);
6326     }
6327
6328   if (ix86_decompose_address (addr, &parts) <= 0)
6329     {
6330       reason = "decomposition failed";
6331       goto report_error;
6332     }
6333
6334   base = parts.base;
6335   index = parts.index;
6336   disp = parts.disp;
6337   scale = parts.scale;
6338
6339   /* Validate base register.
6340
6341      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6342      failures when the base is one word out of a two word structure, which is
6343      represented internally as a DImode int.  */
6344
6345   if (base)
6346     {
6347       rtx reg;
6348       reason_rtx = base;
6349
6350       if (REG_P (base))
6351         reg = base;
6352       else if (GET_CODE (base) == SUBREG
6353                && REG_P (SUBREG_REG (base))
6354                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6355                   <= UNITS_PER_WORD)
6356         reg = SUBREG_REG (base);
6357       else
6358         {
6359           reason = "base is not a register";
6360           goto report_error;
6361         }
6362
6363       if (GET_MODE (base) != Pmode)
6364         {
6365           reason = "base is not in Pmode";
6366           goto report_error;
6367         }
6368
6369       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6370           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6371         {
6372           reason = "base is not valid";
6373           goto report_error;
6374         }
6375     }
6376
6377   /* Validate index register.
6378
6379      Don't allow SUBREG's that span more than a word here -- same as above.  */
6380
6381   if (index)
6382     {
6383       rtx reg;
6384       reason_rtx = index;
6385
6386       if (REG_P (index))
6387         reg = index;
6388       else if (GET_CODE (index) == SUBREG
6389                && REG_P (SUBREG_REG (index))
6390                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6391                   <= UNITS_PER_WORD)
6392         reg = SUBREG_REG (index);
6393       else
6394         {
6395           reason = "index is not a register";
6396           goto report_error;
6397         }
6398
6399       if (GET_MODE (index) != Pmode)
6400         {
6401           reason = "index is not in Pmode";
6402           goto report_error;
6403         }
6404
6405       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6406           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6407         {
6408           reason = "index is not valid";
6409           goto report_error;
6410         }
6411     }
6412
6413   /* Validate scale factor.  */
6414   if (scale != 1)
6415     {
6416       reason_rtx = GEN_INT (scale);
6417       if (!index)
6418         {
6419           reason = "scale without index";
6420           goto report_error;
6421         }
6422
6423       if (scale != 2 && scale != 4 && scale != 8)
6424         {
6425           reason = "scale is not a valid multiplier";
6426           goto report_error;
6427         }
6428     }
6429
6430   /* Validate displacement.  */
6431   if (disp)
6432     {
6433       reason_rtx = disp;
6434
6435       if (GET_CODE (disp) == CONST
6436           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6437         switch (XINT (XEXP (disp, 0), 1))
6438           {
6439           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6440              used.  While ABI specify also 32bit relocations, we don't produce
6441              them at all and use IP relative instead.  */
6442           case UNSPEC_GOT:
6443           case UNSPEC_GOTOFF:
6444             gcc_assert (flag_pic);
6445             if (!TARGET_64BIT)
6446               goto is_legitimate_pic;
6447             reason = "64bit address unspec";
6448             goto report_error;
6449
6450           case UNSPEC_GOTPCREL:
6451             gcc_assert (flag_pic);
6452             goto is_legitimate_pic;
6453
6454           case UNSPEC_GOTTPOFF:
6455           case UNSPEC_GOTNTPOFF:
6456           case UNSPEC_INDNTPOFF:
6457           case UNSPEC_NTPOFF:
6458           case UNSPEC_DTPOFF:
6459             break;
6460
6461           default:
6462             reason = "invalid address unspec";
6463             goto report_error;
6464           }
6465
6466       else if (SYMBOLIC_CONST (disp)
6467                && (flag_pic
6468                    || (TARGET_MACHO
6469 #if TARGET_MACHO
6470                        && MACHOPIC_INDIRECT
6471                        && !machopic_operand_p (disp)
6472 #endif
6473                )))
6474         {
6475
6476         is_legitimate_pic:
6477           if (TARGET_64BIT && (index || base))
6478             {
6479               /* foo@dtpoff(%rX) is ok.  */
6480               if (GET_CODE (disp) != CONST
6481                   || GET_CODE (XEXP (disp, 0)) != PLUS
6482                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6483                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
6484                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6485                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6486                 {
6487                   reason = "non-constant pic memory reference";
6488                   goto report_error;
6489                 }
6490             }
6491           else if (! legitimate_pic_address_disp_p (disp))
6492             {
6493               reason = "displacement is an invalid pic construct";
6494               goto report_error;
6495             }
6496
6497           /* This code used to verify that a symbolic pic displacement
6498              includes the pic_offset_table_rtx register.
6499
6500              While this is good idea, unfortunately these constructs may
6501              be created by "adds using lea" optimization for incorrect
6502              code like:
6503
6504              int a;
6505              int foo(int i)
6506                {
6507                  return *(&a+i);
6508                }
6509
6510              This code is nonsensical, but results in addressing
6511              GOT table with pic_offset_table_rtx base.  We can't
6512              just refuse it easily, since it gets matched by
6513              "addsi3" pattern, that later gets split to lea in the
6514              case output register differs from input.  While this
6515              can be handled by separate addsi pattern for this case
6516              that never results in lea, this seems to be easier and
6517              correct fix for crash to disable this test.  */
6518         }
6519       else if (GET_CODE (disp) != LABEL_REF
6520                && GET_CODE (disp) != CONST_INT
6521                && (GET_CODE (disp) != CONST
6522                    || !legitimate_constant_p (disp))
6523                && (GET_CODE (disp) != SYMBOL_REF
6524                    || !legitimate_constant_p (disp)))
6525         {
6526           reason = "displacement is not constant";
6527           goto report_error;
6528         }
6529       else if (TARGET_64BIT
6530                && !x86_64_immediate_operand (disp, VOIDmode))
6531         {
6532           reason = "displacement is out of range";
6533           goto report_error;
6534         }
6535     }
6536
6537   /* Everything looks valid.  */
6538   if (TARGET_DEBUG_ADDR)
6539     fprintf (stderr, "Success.\n");
6540   return TRUE;
6541
6542  report_error:
6543   if (TARGET_DEBUG_ADDR)
6544     {
6545       fprintf (stderr, "Error: %s\n", reason);
6546       debug_rtx (reason_rtx);
6547     }
6548   return FALSE;
6549 }
6550 \f
6551 /* Return a unique alias set for the GOT.  */
6552
6553 static HOST_WIDE_INT
6554 ix86_GOT_alias_set (void)
6555 {
6556   static HOST_WIDE_INT set = -1;
6557   if (set == -1)
6558     set = new_alias_set ();
6559   return set;
6560 }
6561
6562 /* Return a legitimate reference for ORIG (an address) using the
6563    register REG.  If REG is 0, a new pseudo is generated.
6564
6565    There are two types of references that must be handled:
6566
6567    1. Global data references must load the address from the GOT, via
6568       the PIC reg.  An insn is emitted to do this load, and the reg is
6569       returned.
6570
6571    2. Static data references, constant pool addresses, and code labels
6572       compute the address as an offset from the GOT, whose base is in
6573       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6574       differentiate them from global data objects.  The returned
6575       address is the PIC reg + an unspec constant.
6576
6577    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6578    reg also appears in the address.  */
6579
6580 static rtx
6581 legitimize_pic_address (rtx orig, rtx reg)
6582 {
6583   rtx addr = orig;
6584   rtx new = orig;
6585   rtx base;
6586
6587 #if TARGET_MACHO
6588   if (TARGET_MACHO && !TARGET_64BIT)
6589     {
6590       if (reg == 0)
6591         reg = gen_reg_rtx (Pmode);
6592       /* Use the generic Mach-O PIC machinery.  */
6593       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6594     }
6595 #endif
6596
6597   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6598     new = addr;
6599   else if (TARGET_64BIT
6600            && ix86_cmodel != CM_SMALL_PIC
6601            && local_symbolic_operand (addr, Pmode))
6602     {
6603       rtx tmpreg;
6604       /* This symbol may be referenced via a displacement from the PIC
6605          base address (@GOTOFF).  */
6606
6607       if (reload_in_progress)
6608         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6609       if (GET_CODE (addr) == CONST)
6610         addr = XEXP (addr, 0);
6611       if (GET_CODE (addr) == PLUS)
6612           {
6613             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6614             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6615           }
6616         else
6617           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6618       new = gen_rtx_CONST (Pmode, new);
6619       if (!reg)
6620         tmpreg = gen_reg_rtx (Pmode);
6621       else
6622         tmpreg = reg;
6623       emit_move_insn (tmpreg, new);
6624
6625       if (reg != 0)
6626         {
6627           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6628                                      tmpreg, 1, OPTAB_DIRECT);
6629           new = reg;
6630         }
6631       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6632     }
6633   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6634     {
6635       /* This symbol may be referenced via a displacement from the PIC
6636          base address (@GOTOFF).  */
6637
6638       if (reload_in_progress)
6639         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6640       if (GET_CODE (addr) == CONST)
6641         addr = XEXP (addr, 0);
6642       if (GET_CODE (addr) == PLUS)
6643           {
6644             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6645             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6646           }
6647         else
6648           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6649       new = gen_rtx_CONST (Pmode, new);
6650       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6651
6652       if (reg != 0)
6653         {
6654           emit_move_insn (reg, new);
6655           new = reg;
6656         }
6657     }
6658   else if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
6659     {
6660       if (TARGET_64BIT)
6661         {
6662           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6663           new = gen_rtx_CONST (Pmode, new);
6664           new = gen_const_mem (Pmode, new);
6665           set_mem_alias_set (new, ix86_GOT_alias_set ());
6666
6667           if (reg == 0)
6668             reg = gen_reg_rtx (Pmode);
6669           /* Use directly gen_movsi, otherwise the address is loaded
6670              into register for CSE.  We don't want to CSE this addresses,
6671              instead we CSE addresses from the GOT table, so skip this.  */
6672           emit_insn (gen_movsi (reg, new));
6673           new = reg;
6674         }
6675       else
6676         {
6677           /* This symbol must be referenced via a load from the
6678              Global Offset Table (@GOT).  */
6679
6680           if (reload_in_progress)
6681             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6682           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6683           new = gen_rtx_CONST (Pmode, new);
6684           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6685           new = gen_const_mem (Pmode, new);
6686           set_mem_alias_set (new, ix86_GOT_alias_set ());
6687
6688           if (reg == 0)
6689             reg = gen_reg_rtx (Pmode);
6690           emit_move_insn (reg, new);
6691           new = reg;
6692         }
6693     }
6694   else
6695     {
6696       if (GET_CODE (addr) == CONST_INT
6697           && !x86_64_immediate_operand (addr, VOIDmode))
6698         {
6699           if (reg)
6700             {
6701               emit_move_insn (reg, addr);
6702               new = reg;
6703             }
6704           else
6705             new = force_reg (Pmode, addr);
6706         }
6707       else if (GET_CODE (addr) == CONST)
6708         {
6709           addr = XEXP (addr, 0);
6710
6711           /* We must match stuff we generate before.  Assume the only
6712              unspecs that can get here are ours.  Not that we could do
6713              anything with them anyway....  */
6714           if (GET_CODE (addr) == UNSPEC
6715               || (GET_CODE (addr) == PLUS
6716                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6717             return orig;
6718           gcc_assert (GET_CODE (addr) == PLUS);
6719         }
6720       if (GET_CODE (addr) == PLUS)
6721         {
6722           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6723
6724           /* Check first to see if this is a constant offset from a @GOTOFF
6725              symbol reference.  */
6726           if (local_symbolic_operand (op0, Pmode)
6727               && GET_CODE (op1) == CONST_INT)
6728             {
6729               if (!TARGET_64BIT)
6730                 {
6731                   if (reload_in_progress)
6732                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6733                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6734                                         UNSPEC_GOTOFF);
6735                   new = gen_rtx_PLUS (Pmode, new, op1);
6736                   new = gen_rtx_CONST (Pmode, new);
6737                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6738
6739                   if (reg != 0)
6740                     {
6741                       emit_move_insn (reg, new);
6742                       new = reg;
6743                     }
6744                 }
6745               else
6746                 {
6747                   if (INTVAL (op1) < -16*1024*1024
6748                       || INTVAL (op1) >= 16*1024*1024)
6749                     {
6750                       if (!x86_64_immediate_operand (op1, Pmode))
6751                         op1 = force_reg (Pmode, op1);
6752                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
6753                     }
6754                 }
6755             }
6756           else
6757             {
6758               base = legitimize_pic_address (XEXP (addr, 0), reg);
6759               new  = legitimize_pic_address (XEXP (addr, 1),
6760                                              base == reg ? NULL_RTX : reg);
6761
6762               if (GET_CODE (new) == CONST_INT)
6763                 new = plus_constant (base, INTVAL (new));
6764               else
6765                 {
6766                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6767                     {
6768                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
6769                       new = XEXP (new, 1);
6770                     }
6771                   new = gen_rtx_PLUS (Pmode, base, new);
6772                 }
6773             }
6774         }
6775     }
6776   return new;
6777 }
6778 \f
6779 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
6780
6781 static rtx
6782 get_thread_pointer (int to_reg)
6783 {
6784   rtx tp, reg, insn;
6785
6786   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6787   if (!to_reg)
6788     return tp;
6789
6790   reg = gen_reg_rtx (Pmode);
6791   insn = gen_rtx_SET (VOIDmode, reg, tp);
6792   insn = emit_insn (insn);
6793
6794   return reg;
6795 }
6796
6797 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
6798    false if we expect this to be used for a memory address and true if
6799    we expect to load the address into a register.  */
6800
6801 static rtx
6802 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
6803 {
6804   rtx dest, base, off, pic, tp;
6805   int type;
6806
6807   switch (model)
6808     {
6809     case TLS_MODEL_GLOBAL_DYNAMIC:
6810       dest = gen_reg_rtx (Pmode);
6811       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
6812
6813       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
6814         {
6815           rtx rax = gen_rtx_REG (Pmode, 0), insns;
6816
6817           start_sequence ();
6818           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6819           insns = get_insns ();
6820           end_sequence ();
6821
6822           emit_libcall_block (insns, dest, rax, x);
6823         }
6824       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6825         emit_insn (gen_tls_global_dynamic_64 (dest, x));
6826       else
6827         emit_insn (gen_tls_global_dynamic_32 (dest, x));
6828
6829       if (TARGET_GNU2_TLS)
6830         {
6831           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
6832
6833           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6834         }
6835       break;
6836
6837     case TLS_MODEL_LOCAL_DYNAMIC:
6838       base = gen_reg_rtx (Pmode);
6839       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
6840
6841       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
6842         {
6843           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
6844
6845           start_sequence ();
6846           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6847           insns = get_insns ();
6848           end_sequence ();
6849
6850           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6851           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6852           emit_libcall_block (insns, base, rax, note);
6853         }
6854       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6855         emit_insn (gen_tls_local_dynamic_base_64 (base));
6856       else
6857         emit_insn (gen_tls_local_dynamic_base_32 (base));
6858
6859       if (TARGET_GNU2_TLS)
6860         {
6861           rtx x = ix86_tls_module_base ();
6862
6863           set_unique_reg_note (get_last_insn (), REG_EQUIV,
6864                                gen_rtx_MINUS (Pmode, x, tp));
6865         }
6866
6867       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6868       off = gen_rtx_CONST (Pmode, off);
6869
6870       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
6871
6872       if (TARGET_GNU2_TLS)
6873         {
6874           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
6875
6876           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6877         }
6878
6879       break;
6880
6881     case TLS_MODEL_INITIAL_EXEC:
6882       if (TARGET_64BIT)
6883         {
6884           pic = NULL;
6885           type = UNSPEC_GOTNTPOFF;
6886         }
6887       else if (flag_pic)
6888         {
6889           if (reload_in_progress)
6890             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6891           pic = pic_offset_table_rtx;
6892           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6893         }
6894       else if (!TARGET_ANY_GNU_TLS)
6895         {
6896           pic = gen_reg_rtx (Pmode);
6897           emit_insn (gen_set_got (pic));
6898           type = UNSPEC_GOTTPOFF;
6899         }
6900       else
6901         {
6902           pic = NULL;
6903           type = UNSPEC_INDNTPOFF;
6904         }
6905
6906       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6907       off = gen_rtx_CONST (Pmode, off);
6908       if (pic)
6909         off = gen_rtx_PLUS (Pmode, pic, off);
6910       off = gen_const_mem (Pmode, off);
6911       set_mem_alias_set (off, ix86_GOT_alias_set ());
6912
6913       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6914         {
6915           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6916           off = force_reg (Pmode, off);
6917           return gen_rtx_PLUS (Pmode, base, off);
6918         }
6919       else
6920         {
6921           base = get_thread_pointer (true);
6922           dest = gen_reg_rtx (Pmode);
6923           emit_insn (gen_subsi3 (dest, base, off));
6924         }
6925       break;
6926
6927     case TLS_MODEL_LOCAL_EXEC:
6928       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6929                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6930                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6931       off = gen_rtx_CONST (Pmode, off);
6932
6933       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6934         {
6935           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6936           return gen_rtx_PLUS (Pmode, base, off);
6937         }
6938       else
6939         {
6940           base = get_thread_pointer (true);
6941           dest = gen_reg_rtx (Pmode);
6942           emit_insn (gen_subsi3 (dest, base, off));
6943         }
6944       break;
6945
6946     default:
6947       gcc_unreachable ();
6948     }
6949
6950   return dest;
6951 }
6952
6953 /* Try machine-dependent ways of modifying an illegitimate address
6954    to be legitimate.  If we find one, return the new, valid address.
6955    This macro is used in only one place: `memory_address' in explow.c.
6956
6957    OLDX is the address as it was before break_out_memory_refs was called.
6958    In some cases it is useful to look at this to decide what needs to be done.
6959
6960    MODE and WIN are passed so that this macro can use
6961    GO_IF_LEGITIMATE_ADDRESS.
6962
6963    It is always safe for this macro to do nothing.  It exists to recognize
6964    opportunities to optimize the output.
6965
6966    For the 80386, we handle X+REG by loading X into a register R and
6967    using R+REG.  R will go in a general reg and indexing will be used.
6968    However, if REG is a broken-out memory address or multiplication,
6969    nothing needs to be done because REG can certainly go in a general reg.
6970
6971    When -fpic is used, special handling is needed for symbolic references.
6972    See comments by legitimize_pic_address in i386.c for details.  */
6973
6974 rtx
6975 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
6976 {
6977   int changed = 0;
6978   unsigned log;
6979
6980   if (TARGET_DEBUG_ADDR)
6981     {
6982       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6983                GET_MODE_NAME (mode));
6984       debug_rtx (x);
6985     }
6986
6987   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
6988   if (log)
6989     return legitimize_tls_address (x, log, false);
6990   if (GET_CODE (x) == CONST
6991       && GET_CODE (XEXP (x, 0)) == PLUS
6992       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6993       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
6994     {
6995       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
6996       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
6997     }
6998
6999   if (flag_pic && SYMBOLIC_CONST (x))
7000     return legitimize_pic_address (x, 0);
7001
7002   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
7003   if (GET_CODE (x) == ASHIFT
7004       && GET_CODE (XEXP (x, 1)) == CONST_INT
7005       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
7006     {
7007       changed = 1;
7008       log = INTVAL (XEXP (x, 1));
7009       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
7010                         GEN_INT (1 << log));
7011     }
7012
7013   if (GET_CODE (x) == PLUS)
7014     {
7015       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
7016
7017       if (GET_CODE (XEXP (x, 0)) == ASHIFT
7018           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7019           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
7020         {
7021           changed = 1;
7022           log = INTVAL (XEXP (XEXP (x, 0), 1));
7023           XEXP (x, 0) = gen_rtx_MULT (Pmode,
7024                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
7025                                       GEN_INT (1 << log));
7026         }
7027
7028       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7029           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
7030           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7031         {
7032           changed = 1;
7033           log = INTVAL (XEXP (XEXP (x, 1), 1));
7034           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7035                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7036                                       GEN_INT (1 << log));
7037         }
7038
7039       /* Put multiply first if it isn't already.  */
7040       if (GET_CODE (XEXP (x, 1)) == MULT)
7041         {
7042           rtx tmp = XEXP (x, 0);
7043           XEXP (x, 0) = XEXP (x, 1);
7044           XEXP (x, 1) = tmp;
7045           changed = 1;
7046         }
7047
7048       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7049          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7050          created by virtual register instantiation, register elimination, and
7051          similar optimizations.  */
7052       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7053         {
7054           changed = 1;
7055           x = gen_rtx_PLUS (Pmode,
7056                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7057                                           XEXP (XEXP (x, 1), 0)),
7058                             XEXP (XEXP (x, 1), 1));
7059         }
7060
7061       /* Canonicalize
7062          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7063          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7064       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7065                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7066                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7067                && CONSTANT_P (XEXP (x, 1)))
7068         {
7069           rtx constant;
7070           rtx other = NULL_RTX;
7071
7072           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7073             {
7074               constant = XEXP (x, 1);
7075               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7076             }
7077           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
7078             {
7079               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7080               other = XEXP (x, 1);
7081             }
7082           else
7083             constant = 0;
7084
7085           if (constant)
7086             {
7087               changed = 1;
7088               x = gen_rtx_PLUS (Pmode,
7089                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7090                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7091                                 plus_constant (other, INTVAL (constant)));
7092             }
7093         }
7094
7095       if (changed && legitimate_address_p (mode, x, FALSE))
7096         return x;
7097
7098       if (GET_CODE (XEXP (x, 0)) == MULT)
7099         {
7100           changed = 1;
7101           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7102         }
7103
7104       if (GET_CODE (XEXP (x, 1)) == MULT)
7105         {
7106           changed = 1;
7107           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7108         }
7109
7110       if (changed
7111           && GET_CODE (XEXP (x, 1)) == REG
7112           && GET_CODE (XEXP (x, 0)) == REG)
7113         return x;
7114
7115       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7116         {
7117           changed = 1;
7118           x = legitimize_pic_address (x, 0);
7119         }
7120
7121       if (changed && legitimate_address_p (mode, x, FALSE))
7122         return x;
7123
7124       if (GET_CODE (XEXP (x, 0)) == REG)
7125         {
7126           rtx temp = gen_reg_rtx (Pmode);
7127           rtx val  = force_operand (XEXP (x, 1), temp);
7128           if (val != temp)
7129             emit_move_insn (temp, val);
7130
7131           XEXP (x, 1) = temp;
7132           return x;
7133         }
7134
7135       else if (GET_CODE (XEXP (x, 1)) == REG)
7136         {
7137           rtx temp = gen_reg_rtx (Pmode);
7138           rtx val  = force_operand (XEXP (x, 0), temp);
7139           if (val != temp)
7140             emit_move_insn (temp, val);
7141
7142           XEXP (x, 0) = temp;
7143           return x;
7144         }
7145     }
7146
7147   return x;
7148 }
7149 \f
7150 /* Print an integer constant expression in assembler syntax.  Addition
7151    and subtraction are the only arithmetic that may appear in these
7152    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7153    CODE is the operand print code from the output string.  */
7154
7155 static void
7156 output_pic_addr_const (FILE *file, rtx x, int code)
7157 {
7158   char buf[256];
7159
7160   switch (GET_CODE (x))
7161     {
7162     case PC:
7163       gcc_assert (flag_pic);
7164       putc ('.', file);
7165       break;
7166
7167     case SYMBOL_REF:
7168       output_addr_const (file, x);
7169       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7170         fputs ("@PLT", file);
7171       break;
7172
7173     case LABEL_REF:
7174       x = XEXP (x, 0);
7175       /* FALLTHRU */
7176     case CODE_LABEL:
7177       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7178       assemble_name (asm_out_file, buf);
7179       break;
7180
7181     case CONST_INT:
7182       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7183       break;
7184
7185     case CONST:
7186       /* This used to output parentheses around the expression,
7187          but that does not work on the 386 (either ATT or BSD assembler).  */
7188       output_pic_addr_const (file, XEXP (x, 0), code);
7189       break;
7190
7191     case CONST_DOUBLE:
7192       if (GET_MODE (x) == VOIDmode)
7193         {
7194           /* We can use %d if the number is <32 bits and positive.  */
7195           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7196             fprintf (file, "0x%lx%08lx",
7197                      (unsigned long) CONST_DOUBLE_HIGH (x),
7198                      (unsigned long) CONST_DOUBLE_LOW (x));
7199           else
7200             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7201         }
7202       else
7203         /* We can't handle floating point constants;
7204            PRINT_OPERAND must handle them.  */
7205         output_operand_lossage ("floating constant misused");
7206       break;
7207
7208     case PLUS:
7209       /* Some assemblers need integer constants to appear first.  */
7210       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
7211         {
7212           output_pic_addr_const (file, XEXP (x, 0), code);
7213           putc ('+', file);
7214           output_pic_addr_const (file, XEXP (x, 1), code);
7215         }
7216       else
7217         {
7218           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
7219           output_pic_addr_const (file, XEXP (x, 1), code);
7220           putc ('+', file);
7221           output_pic_addr_const (file, XEXP (x, 0), code);
7222         }
7223       break;
7224
7225     case MINUS:
7226       if (!TARGET_MACHO)
7227         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7228       output_pic_addr_const (file, XEXP (x, 0), code);
7229       putc ('-', file);
7230       output_pic_addr_const (file, XEXP (x, 1), code);
7231       if (!TARGET_MACHO)
7232         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7233       break;
7234
7235      case UNSPEC:
7236        gcc_assert (XVECLEN (x, 0) == 1);
7237        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7238        switch (XINT (x, 1))
7239         {
7240         case UNSPEC_GOT:
7241           fputs ("@GOT", file);
7242           break;
7243         case UNSPEC_GOTOFF:
7244           fputs ("@GOTOFF", file);
7245           break;
7246         case UNSPEC_GOTPCREL:
7247           fputs ("@GOTPCREL(%rip)", file);
7248           break;
7249         case UNSPEC_GOTTPOFF:
7250           /* FIXME: This might be @TPOFF in Sun ld too.  */
7251           fputs ("@GOTTPOFF", file);
7252           break;
7253         case UNSPEC_TPOFF:
7254           fputs ("@TPOFF", file);
7255           break;
7256         case UNSPEC_NTPOFF:
7257           if (TARGET_64BIT)
7258             fputs ("@TPOFF", file);
7259           else
7260             fputs ("@NTPOFF", file);
7261           break;
7262         case UNSPEC_DTPOFF:
7263           fputs ("@DTPOFF", file);
7264           break;
7265         case UNSPEC_GOTNTPOFF:
7266           if (TARGET_64BIT)
7267             fputs ("@GOTTPOFF(%rip)", file);
7268           else
7269             fputs ("@GOTNTPOFF", file);
7270           break;
7271         case UNSPEC_INDNTPOFF:
7272           fputs ("@INDNTPOFF", file);
7273           break;
7274         default:
7275           output_operand_lossage ("invalid UNSPEC as operand");
7276           break;
7277         }
7278        break;
7279
7280     default:
7281       output_operand_lossage ("invalid expression as operand");
7282     }
7283 }
7284
7285 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7286    We need to emit DTP-relative relocations.  */
7287
7288 static void
7289 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7290 {
7291   fputs (ASM_LONG, file);
7292   output_addr_const (file, x);
7293   fputs ("@DTPOFF", file);
7294   switch (size)
7295     {
7296     case 4:
7297       break;
7298     case 8:
7299       fputs (", 0", file);
7300       break;
7301     default:
7302       gcc_unreachable ();
7303    }
7304 }
7305
7306 /* In the name of slightly smaller debug output, and to cater to
7307    general assembler lossage, recognize PIC+GOTOFF and turn it back
7308    into a direct symbol reference.
7309
7310    On Darwin, this is necessary to avoid a crash, because Darwin
7311    has a different PIC label for each routine but the DWARF debugging
7312    information is not associated with any particular routine, so it's
7313    necessary to remove references to the PIC label from RTL stored by
7314    the DWARF output code.  */
7315
7316 static rtx
7317 ix86_delegitimize_address (rtx orig_x)
7318 {
7319   rtx x = orig_x;
7320   /* reg_addend is NULL or a multiple of some register.  */
7321   rtx reg_addend = NULL_RTX;
7322   /* const_addend is NULL or a const_int.  */
7323   rtx const_addend = NULL_RTX;
7324   /* This is the result, or NULL.  */
7325   rtx result = NULL_RTX;
7326
7327   if (GET_CODE (x) == MEM)
7328     x = XEXP (x, 0);
7329
7330   if (TARGET_64BIT)
7331     {
7332       if (GET_CODE (x) != CONST
7333           || GET_CODE (XEXP (x, 0)) != UNSPEC
7334           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7335           || GET_CODE (orig_x) != MEM)
7336         return orig_x;
7337       return XVECEXP (XEXP (x, 0), 0, 0);
7338     }
7339
7340   if (GET_CODE (x) != PLUS
7341       || GET_CODE (XEXP (x, 1)) != CONST)
7342     return orig_x;
7343
7344   if (GET_CODE (XEXP (x, 0)) == REG
7345       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7346     /* %ebx + GOT/GOTOFF */
7347     ;
7348   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7349     {
7350       /* %ebx + %reg * scale + GOT/GOTOFF */
7351       reg_addend = XEXP (x, 0);
7352       if (GET_CODE (XEXP (reg_addend, 0)) == REG
7353           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7354         reg_addend = XEXP (reg_addend, 1);
7355       else if (GET_CODE (XEXP (reg_addend, 1)) == REG
7356                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7357         reg_addend = XEXP (reg_addend, 0);
7358       else
7359         return orig_x;
7360       if (GET_CODE (reg_addend) != REG
7361           && GET_CODE (reg_addend) != MULT
7362           && GET_CODE (reg_addend) != ASHIFT)
7363         return orig_x;
7364     }
7365   else
7366     return orig_x;
7367
7368   x = XEXP (XEXP (x, 1), 0);
7369   if (GET_CODE (x) == PLUS
7370       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7371     {
7372       const_addend = XEXP (x, 1);
7373       x = XEXP (x, 0);
7374     }
7375
7376   if (GET_CODE (x) == UNSPEC
7377       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
7378           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
7379     result = XVECEXP (x, 0, 0);
7380
7381   if (TARGET_MACHO && darwin_local_data_pic (x)
7382       && GET_CODE (orig_x) != MEM)
7383     result = XEXP (x, 0);
7384
7385   if (! result)
7386     return orig_x;
7387
7388   if (const_addend)
7389     result = gen_rtx_PLUS (Pmode, result, const_addend);
7390   if (reg_addend)
7391     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7392   return result;
7393 }
7394 \f
7395 static void
7396 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7397                     int fp, FILE *file)
7398 {
7399   const char *suffix;
7400
7401   if (mode == CCFPmode || mode == CCFPUmode)
7402     {
7403       enum rtx_code second_code, bypass_code;
7404       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7405       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7406       code = ix86_fp_compare_code_to_integer (code);
7407       mode = CCmode;
7408     }
7409   if (reverse)
7410     code = reverse_condition (code);
7411
7412   switch (code)
7413     {
7414     case EQ:
7415       suffix = "e";
7416       break;
7417     case NE:
7418       suffix = "ne";
7419       break;
7420     case GT:
7421       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7422       suffix = "g";
7423       break;
7424     case GTU:
7425       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7426          Those same assemblers have the same but opposite lossage on cmov.  */
7427       gcc_assert (mode == CCmode);
7428       suffix = fp ? "nbe" : "a";
7429       break;
7430     case LT:
7431       switch (mode)
7432         {
7433         case CCNOmode:
7434         case CCGOCmode:
7435           suffix = "s";
7436           break;
7437
7438         case CCmode:
7439         case CCGCmode:
7440           suffix = "l";
7441           break;
7442
7443         default:
7444           gcc_unreachable ();
7445         }
7446       break;
7447     case LTU:
7448       gcc_assert (mode == CCmode);
7449       suffix = "b";
7450       break;
7451     case GE:
7452       switch (mode)
7453         {
7454         case CCNOmode:
7455         case CCGOCmode:
7456           suffix = "ns";
7457           break;
7458
7459         case CCmode:
7460         case CCGCmode:
7461           suffix = "ge";
7462           break;
7463
7464         default:
7465           gcc_unreachable ();
7466         }
7467       break;
7468     case GEU:
7469       /* ??? As above.  */
7470       gcc_assert (mode == CCmode);
7471       suffix = fp ? "nb" : "ae";
7472       break;
7473     case LE:
7474       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
7475       suffix = "le";
7476       break;
7477     case LEU:
7478       gcc_assert (mode == CCmode);
7479       suffix = "be";
7480       break;
7481     case UNORDERED:
7482       suffix = fp ? "u" : "p";
7483       break;
7484     case ORDERED:
7485       suffix = fp ? "nu" : "np";
7486       break;
7487     default:
7488       gcc_unreachable ();
7489     }
7490   fputs (suffix, file);
7491 }
7492
7493 /* Print the name of register X to FILE based on its machine mode and number.
7494    If CODE is 'w', pretend the mode is HImode.
7495    If CODE is 'b', pretend the mode is QImode.
7496    If CODE is 'k', pretend the mode is SImode.
7497    If CODE is 'q', pretend the mode is DImode.
7498    If CODE is 'h', pretend the reg is the 'high' byte register.
7499    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7500
7501 void
7502 print_reg (rtx x, int code, FILE *file)
7503 {
7504   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
7505               && REGNO (x) != FRAME_POINTER_REGNUM
7506               && REGNO (x) != FLAGS_REG
7507               && REGNO (x) != FPSR_REG
7508               && REGNO (x) != FPCR_REG);
7509
7510   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7511     putc ('%', file);
7512
7513   if (code == 'w' || MMX_REG_P (x))
7514     code = 2;
7515   else if (code == 'b')
7516     code = 1;
7517   else if (code == 'k')
7518     code = 4;
7519   else if (code == 'q')
7520     code = 8;
7521   else if (code == 'y')
7522     code = 3;
7523   else if (code == 'h')
7524     code = 0;
7525   else
7526     code = GET_MODE_SIZE (GET_MODE (x));
7527
7528   /* Irritatingly, AMD extended registers use different naming convention
7529      from the normal registers.  */
7530   if (REX_INT_REG_P (x))
7531     {
7532       gcc_assert (TARGET_64BIT);
7533       switch (code)
7534         {
7535           case 0:
7536             error ("extended registers have no high halves");
7537             break;
7538           case 1:
7539             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7540             break;
7541           case 2:
7542             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7543             break;
7544           case 4:
7545             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7546             break;
7547           case 8:
7548             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7549             break;
7550           default:
7551             error ("unsupported operand size for extended register");
7552             break;
7553         }
7554       return;
7555     }
7556   switch (code)
7557     {
7558     case 3:
7559       if (STACK_TOP_P (x))
7560         {
7561           fputs ("st(0)", file);
7562           break;
7563         }
7564       /* FALLTHRU */
7565     case 8:
7566     case 4:
7567     case 12:
7568       if (! ANY_FP_REG_P (x))
7569         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7570       /* FALLTHRU */
7571     case 16:
7572     case 2:
7573     normal:
7574       fputs (hi_reg_name[REGNO (x)], file);
7575       break;
7576     case 1:
7577       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7578         goto normal;
7579       fputs (qi_reg_name[REGNO (x)], file);
7580       break;
7581     case 0:
7582       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7583         goto normal;
7584       fputs (qi_high_reg_name[REGNO (x)], file);
7585       break;
7586     default:
7587       gcc_unreachable ();
7588     }
7589 }
7590
7591 /* Locate some local-dynamic symbol still in use by this function
7592    so that we can print its name in some tls_local_dynamic_base
7593    pattern.  */
7594
7595 static const char *
7596 get_some_local_dynamic_name (void)
7597 {
7598   rtx insn;
7599
7600   if (cfun->machine->some_ld_name)
7601     return cfun->machine->some_ld_name;
7602
7603   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7604     if (INSN_P (insn)
7605         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7606       return cfun->machine->some_ld_name;
7607
7608   gcc_unreachable ();
7609 }
7610
7611 static int
7612 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7613 {
7614   rtx x = *px;
7615
7616   if (GET_CODE (x) == SYMBOL_REF
7617       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7618     {
7619       cfun->machine->some_ld_name = XSTR (x, 0);
7620       return 1;
7621     }
7622
7623   return 0;
7624 }
7625
7626 /* Meaning of CODE:
7627    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7628    C -- print opcode suffix for set/cmov insn.
7629    c -- like C, but print reversed condition
7630    F,f -- likewise, but for floating-point.
7631    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7632         otherwise nothing
7633    R -- print the prefix for register names.
7634    z -- print the opcode suffix for the size of the current operand.
7635    * -- print a star (in certain assembler syntax)
7636    A -- print an absolute memory reference.
7637    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7638    s -- print a shift double count, followed by the assemblers argument
7639         delimiter.
7640    b -- print the QImode name of the register for the indicated operand.
7641         %b0 would print %al if operands[0] is reg 0.
7642    w --  likewise, print the HImode name of the register.
7643    k --  likewise, print the SImode name of the register.
7644    q --  likewise, print the DImode name of the register.
7645    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7646    y -- print "st(0)" instead of "st" as a register.
7647    D -- print condition for SSE cmp instruction.
7648    P -- if PIC, print an @PLT suffix.
7649    X -- don't print any sort of PIC '@' suffix for a symbol.
7650    & -- print some in-use local-dynamic symbol name.
7651    H -- print a memory address offset by 8; used for sse high-parts
7652  */
7653
7654 void
7655 print_operand (FILE *file, rtx x, int code)
7656 {
7657   if (code)
7658     {
7659       switch (code)
7660         {
7661         case '*':
7662           if (ASSEMBLER_DIALECT == ASM_ATT)
7663             putc ('*', file);
7664           return;
7665
7666         case '&':
7667           assemble_name (file, get_some_local_dynamic_name ());
7668           return;
7669
7670         case 'A':
7671           switch (ASSEMBLER_DIALECT)
7672             {
7673             case ASM_ATT:
7674               putc ('*', file);
7675               break;
7676
7677             case ASM_INTEL:
7678               /* Intel syntax. For absolute addresses, registers should not
7679                  be surrounded by braces.  */
7680               if (GET_CODE (x) != REG)
7681                 {
7682                   putc ('[', file);
7683                   PRINT_OPERAND (file, x, 0);
7684                   putc (']', file);
7685                   return;
7686                 }
7687               break;
7688
7689             default:
7690               gcc_unreachable ();
7691             }
7692
7693           PRINT_OPERAND (file, x, 0);
7694           return;
7695
7696
7697         case 'L':
7698           if (ASSEMBLER_DIALECT == ASM_ATT)
7699             putc ('l', file);
7700           return;
7701
7702         case 'W':
7703           if (ASSEMBLER_DIALECT == ASM_ATT)
7704             putc ('w', file);
7705           return;
7706
7707         case 'B':
7708           if (ASSEMBLER_DIALECT == ASM_ATT)
7709             putc ('b', file);
7710           return;
7711
7712         case 'Q':
7713           if (ASSEMBLER_DIALECT == ASM_ATT)
7714             putc ('l', file);
7715           return;
7716
7717         case 'S':
7718           if (ASSEMBLER_DIALECT == ASM_ATT)
7719             putc ('s', file);
7720           return;
7721
7722         case 'T':
7723           if (ASSEMBLER_DIALECT == ASM_ATT)
7724             putc ('t', file);
7725           return;
7726
7727         case 'z':
7728           /* 387 opcodes don't get size suffixes if the operands are
7729              registers.  */
7730           if (STACK_REG_P (x))
7731             return;
7732
7733           /* Likewise if using Intel opcodes.  */
7734           if (ASSEMBLER_DIALECT == ASM_INTEL)
7735             return;
7736
7737           /* This is the size of op from size of operand.  */
7738           switch (GET_MODE_SIZE (GET_MODE (x)))
7739             {
7740             case 2:
7741 #ifdef HAVE_GAS_FILDS_FISTS
7742               putc ('s', file);
7743 #endif
7744               return;
7745
7746             case 4:
7747               if (GET_MODE (x) == SFmode)
7748                 {
7749                   putc ('s', file);
7750                   return;
7751                 }
7752               else
7753                 putc ('l', file);
7754               return;
7755
7756             case 12:
7757             case 16:
7758               putc ('t', file);
7759               return;
7760
7761             case 8:
7762               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7763                 {
7764 #ifdef GAS_MNEMONICS
7765                   putc ('q', file);
7766 #else
7767                   putc ('l', file);
7768                   putc ('l', file);
7769 #endif
7770                 }
7771               else
7772                 putc ('l', file);
7773               return;
7774
7775             default:
7776               gcc_unreachable ();
7777             }
7778
7779         case 'b':
7780         case 'w':
7781         case 'k':
7782         case 'q':
7783         case 'h':
7784         case 'y':
7785         case 'X':
7786         case 'P':
7787           break;
7788
7789         case 's':
7790           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7791             {
7792               PRINT_OPERAND (file, x, 0);
7793               putc (',', file);
7794             }
7795           return;
7796
7797         case 'D':
7798           /* Little bit of braindamage here.  The SSE compare instructions
7799              does use completely different names for the comparisons that the
7800              fp conditional moves.  */
7801           switch (GET_CODE (x))
7802             {
7803             case EQ:
7804             case UNEQ:
7805               fputs ("eq", file);
7806               break;
7807             case LT:
7808             case UNLT:
7809               fputs ("lt", file);
7810               break;
7811             case LE:
7812             case UNLE:
7813               fputs ("le", file);
7814               break;
7815             case UNORDERED:
7816               fputs ("unord", file);
7817               break;
7818             case NE:
7819             case LTGT:
7820               fputs ("neq", file);
7821               break;
7822             case UNGE:
7823             case GE:
7824               fputs ("nlt", file);
7825               break;
7826             case UNGT:
7827             case GT:
7828               fputs ("nle", file);
7829               break;
7830             case ORDERED:
7831               fputs ("ord", file);
7832               break;
7833             default:
7834               gcc_unreachable ();
7835             }
7836           return;
7837         case 'O':
7838 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7839           if (ASSEMBLER_DIALECT == ASM_ATT)
7840             {
7841               switch (GET_MODE (x))
7842                 {
7843                 case HImode: putc ('w', file); break;
7844                 case SImode:
7845                 case SFmode: putc ('l', file); break;
7846                 case DImode:
7847                 case DFmode: putc ('q', file); break;
7848                 default: gcc_unreachable ();
7849                 }
7850               putc ('.', file);
7851             }
7852 #endif
7853           return;
7854         case 'C':
7855           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7856           return;
7857         case 'F':
7858 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7859           if (ASSEMBLER_DIALECT == ASM_ATT)
7860             putc ('.', file);
7861 #endif
7862           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7863           return;
7864
7865           /* Like above, but reverse condition */
7866         case 'c':
7867           /* Check to see if argument to %c is really a constant
7868              and not a condition code which needs to be reversed.  */
7869           if (!COMPARISON_P (x))
7870           {
7871             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7872              return;
7873           }
7874           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7875           return;
7876         case 'f':
7877 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7878           if (ASSEMBLER_DIALECT == ASM_ATT)
7879             putc ('.', file);
7880 #endif
7881           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7882           return;
7883
7884         case 'H':
7885           /* It doesn't actually matter what mode we use here, as we're
7886              only going to use this for printing.  */
7887           x = adjust_address_nv (x, DImode, 8);
7888           break;
7889
7890         case '+':
7891           {
7892             rtx x;
7893
7894             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7895               return;
7896
7897             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7898             if (x)
7899               {
7900                 int pred_val = INTVAL (XEXP (x, 0));
7901
7902                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7903                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7904                   {
7905                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7906                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7907
7908                     /* Emit hints only in the case default branch prediction
7909                        heuristics would fail.  */
7910                     if (taken != cputaken)
7911                       {
7912                         /* We use 3e (DS) prefix for taken branches and
7913                            2e (CS) prefix for not taken branches.  */
7914                         if (taken)
7915                           fputs ("ds ; ", file);
7916                         else
7917                           fputs ("cs ; ", file);
7918                       }
7919                   }
7920               }
7921             return;
7922           }
7923         default:
7924             output_operand_lossage ("invalid operand code '%c'", code);
7925         }
7926     }
7927
7928   if (GET_CODE (x) == REG)
7929     print_reg (x, code, file);
7930
7931   else if (GET_CODE (x) == MEM)
7932     {
7933       /* No `byte ptr' prefix for call instructions.  */
7934       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7935         {
7936           const char * size;
7937           switch (GET_MODE_SIZE (GET_MODE (x)))
7938             {
7939             case 1: size = "BYTE"; break;
7940             case 2: size = "WORD"; break;
7941             case 4: size = "DWORD"; break;
7942             case 8: size = "QWORD"; break;
7943             case 12: size = "XWORD"; break;
7944             case 16: size = "XMMWORD"; break;
7945             default:
7946               gcc_unreachable ();
7947             }
7948
7949           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7950           if (code == 'b')
7951             size = "BYTE";
7952           else if (code == 'w')
7953             size = "WORD";
7954           else if (code == 'k')
7955             size = "DWORD";
7956
7957           fputs (size, file);
7958           fputs (" PTR ", file);
7959         }
7960
7961       x = XEXP (x, 0);
7962       /* Avoid (%rip) for call operands.  */
7963       if (CONSTANT_ADDRESS_P (x) && code == 'P'
7964                && GET_CODE (x) != CONST_INT)
7965         output_addr_const (file, x);
7966       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7967         output_operand_lossage ("invalid constraints for operand");
7968       else
7969         output_address (x);
7970     }
7971
7972   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7973     {
7974       REAL_VALUE_TYPE r;
7975       long l;
7976
7977       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7978       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7979
7980       if (ASSEMBLER_DIALECT == ASM_ATT)
7981         putc ('$', file);
7982       fprintf (file, "0x%08lx", l);
7983     }
7984
7985   /* These float cases don't actually occur as immediate operands.  */
7986   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7987     {
7988       char dstr[30];
7989
7990       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7991       fprintf (file, "%s", dstr);
7992     }
7993
7994   else if (GET_CODE (x) == CONST_DOUBLE
7995            && GET_MODE (x) == XFmode)
7996     {
7997       char dstr[30];
7998
7999       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
8000       fprintf (file, "%s", dstr);
8001     }
8002
8003   else
8004     {
8005       /* We have patterns that allow zero sets of memory, for instance.
8006          In 64-bit mode, we should probably support all 8-byte vectors,
8007          since we can in fact encode that into an immediate.  */
8008       if (GET_CODE (x) == CONST_VECTOR)
8009         {
8010           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
8011           x = const0_rtx;
8012         }
8013
8014       if (code != 'P')
8015         {
8016           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
8017             {
8018               if (ASSEMBLER_DIALECT == ASM_ATT)
8019                 putc ('$', file);
8020             }
8021           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
8022                    || GET_CODE (x) == LABEL_REF)
8023             {
8024               if (ASSEMBLER_DIALECT == ASM_ATT)
8025                 putc ('$', file);
8026               else
8027                 fputs ("OFFSET FLAT:", file);
8028             }
8029         }
8030       if (GET_CODE (x) == CONST_INT)
8031         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8032       else if (flag_pic)
8033         output_pic_addr_const (file, x, code);
8034       else
8035         output_addr_const (file, x);
8036     }
8037 }
8038 \f
8039 /* Print a memory operand whose address is ADDR.  */
8040
8041 void
8042 print_operand_address (FILE *file, rtx addr)
8043 {
8044   struct ix86_address parts;
8045   rtx base, index, disp;
8046   int scale;
8047   int ok = ix86_decompose_address (addr, &parts);
8048
8049   gcc_assert (ok);
8050
8051   base = parts.base;
8052   index = parts.index;
8053   disp = parts.disp;
8054   scale = parts.scale;
8055
8056   switch (parts.seg)
8057     {
8058     case SEG_DEFAULT:
8059       break;
8060     case SEG_FS:
8061     case SEG_GS:
8062       if (USER_LABEL_PREFIX[0] == 0)
8063         putc ('%', file);
8064       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8065       break;
8066     default:
8067       gcc_unreachable ();
8068     }
8069
8070   if (!base && !index)
8071     {
8072       /* Displacement only requires special attention.  */
8073
8074       if (GET_CODE (disp) == CONST_INT)
8075         {
8076           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8077             {
8078               if (USER_LABEL_PREFIX[0] == 0)
8079                 putc ('%', file);
8080               fputs ("ds:", file);
8081             }
8082           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8083         }
8084       else if (flag_pic)
8085         output_pic_addr_const (file, disp, 0);
8086       else
8087         output_addr_const (file, disp);
8088
8089       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8090       if (TARGET_64BIT)
8091         {
8092           if (GET_CODE (disp) == CONST
8093               && GET_CODE (XEXP (disp, 0)) == PLUS
8094               && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8095             disp = XEXP (XEXP (disp, 0), 0);
8096           if (GET_CODE (disp) == LABEL_REF
8097               || (GET_CODE (disp) == SYMBOL_REF
8098                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8099             fputs ("(%rip)", file);
8100         }
8101     }
8102   else
8103     {
8104       if (ASSEMBLER_DIALECT == ASM_ATT)
8105         {
8106           if (disp)
8107             {
8108               if (flag_pic)
8109                 output_pic_addr_const (file, disp, 0);
8110               else if (GET_CODE (disp) == LABEL_REF)
8111                 output_asm_label (disp);
8112               else
8113                 output_addr_const (file, disp);
8114             }
8115
8116           putc ('(', file);
8117           if (base)
8118             print_reg (base, 0, file);
8119           if (index)
8120             {
8121               putc (',', file);
8122               print_reg (index, 0, file);
8123               if (scale != 1)
8124                 fprintf (file, ",%d", scale);
8125             }
8126           putc (')', file);
8127         }
8128       else
8129         {
8130           rtx offset = NULL_RTX;
8131
8132           if (disp)
8133             {
8134               /* Pull out the offset of a symbol; print any symbol itself.  */
8135               if (GET_CODE (disp) == CONST
8136                   && GET_CODE (XEXP (disp, 0)) == PLUS
8137                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8138                 {
8139                   offset = XEXP (XEXP (disp, 0), 1);
8140                   disp = gen_rtx_CONST (VOIDmode,
8141                                         XEXP (XEXP (disp, 0), 0));
8142                 }
8143
8144               if (flag_pic)
8145                 output_pic_addr_const (file, disp, 0);
8146               else if (GET_CODE (disp) == LABEL_REF)
8147                 output_asm_label (disp);
8148               else if (GET_CODE (disp) == CONST_INT)
8149                 offset = disp;
8150               else
8151                 output_addr_const (file, disp);
8152             }
8153
8154           putc ('[', file);
8155           if (base)
8156             {
8157               print_reg (base, 0, file);
8158               if (offset)
8159                 {
8160                   if (INTVAL (offset) >= 0)
8161                     putc ('+', file);
8162                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8163                 }
8164             }
8165           else if (offset)
8166             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8167           else
8168             putc ('0', file);
8169
8170           if (index)
8171             {
8172               putc ('+', file);
8173               print_reg (index, 0, file);
8174               if (scale != 1)
8175                 fprintf (file, "*%d", scale);
8176             }
8177           putc (']', file);
8178         }
8179     }
8180 }
8181
8182 bool
8183 output_addr_const_extra (FILE *file, rtx x)
8184 {
8185   rtx op;
8186
8187   if (GET_CODE (x) != UNSPEC)
8188     return false;
8189
8190   op = XVECEXP (x, 0, 0);
8191   switch (XINT (x, 1))
8192     {
8193     case UNSPEC_GOTTPOFF:
8194       output_addr_const (file, op);
8195       /* FIXME: This might be @TPOFF in Sun ld.  */
8196       fputs ("@GOTTPOFF", file);
8197       break;
8198     case UNSPEC_TPOFF:
8199       output_addr_const (file, op);
8200       fputs ("@TPOFF", file);
8201       break;
8202     case UNSPEC_NTPOFF:
8203       output_addr_const (file, op);
8204       if (TARGET_64BIT)
8205         fputs ("@TPOFF", file);
8206       else
8207         fputs ("@NTPOFF", file);
8208       break;
8209     case UNSPEC_DTPOFF:
8210       output_addr_const (file, op);
8211       fputs ("@DTPOFF", file);
8212       break;
8213     case UNSPEC_GOTNTPOFF:
8214       output_addr_const (file, op);
8215       if (TARGET_64BIT)
8216         fputs ("@GOTTPOFF(%rip)", file);
8217       else
8218         fputs ("@GOTNTPOFF", file);
8219       break;
8220     case UNSPEC_INDNTPOFF:
8221       output_addr_const (file, op);
8222       fputs ("@INDNTPOFF", file);
8223       break;
8224
8225     default:
8226       return false;
8227     }
8228
8229   return true;
8230 }
8231 \f
8232 /* Split one or more DImode RTL references into pairs of SImode
8233    references.  The RTL can be REG, offsettable MEM, integer constant, or
8234    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8235    split and "num" is its length.  lo_half and hi_half are output arrays
8236    that parallel "operands".  */
8237
8238 void
8239 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8240 {
8241   while (num--)
8242     {
8243       rtx op = operands[num];
8244
8245       /* simplify_subreg refuse to split volatile memory addresses,
8246          but we still have to handle it.  */
8247       if (GET_CODE (op) == MEM)
8248         {
8249           lo_half[num] = adjust_address (op, SImode, 0);
8250           hi_half[num] = adjust_address (op, SImode, 4);
8251         }
8252       else
8253         {
8254           lo_half[num] = simplify_gen_subreg (SImode, op,
8255                                               GET_MODE (op) == VOIDmode
8256                                               ? DImode : GET_MODE (op), 0);
8257           hi_half[num] = simplify_gen_subreg (SImode, op,
8258                                               GET_MODE (op) == VOIDmode
8259                                               ? DImode : GET_MODE (op), 4);
8260         }
8261     }
8262 }
8263 /* Split one or more TImode RTL references into pairs of DImode
8264    references.  The RTL can be REG, offsettable MEM, integer constant, or
8265    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8266    split and "num" is its length.  lo_half and hi_half are output arrays
8267    that parallel "operands".  */
8268
8269 void
8270 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8271 {
8272   while (num--)
8273     {
8274       rtx op = operands[num];
8275
8276       /* simplify_subreg refuse to split volatile memory addresses, but we
8277          still have to handle it.  */
8278       if (GET_CODE (op) == MEM)
8279         {
8280           lo_half[num] = adjust_address (op, DImode, 0);
8281           hi_half[num] = adjust_address (op, DImode, 8);
8282         }
8283       else
8284         {
8285           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8286           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8287         }
8288     }
8289 }
8290 \f
8291 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8292    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8293    is the expression of the binary operation.  The output may either be
8294    emitted here, or returned to the caller, like all output_* functions.
8295
8296    There is no guarantee that the operands are the same mode, as they
8297    might be within FLOAT or FLOAT_EXTEND expressions.  */
8298
8299 #ifndef SYSV386_COMPAT
8300 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8301    wants to fix the assemblers because that causes incompatibility
8302    with gcc.  No-one wants to fix gcc because that causes
8303    incompatibility with assemblers...  You can use the option of
8304    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8305 #define SYSV386_COMPAT 1
8306 #endif
8307
8308 const char *
8309 output_387_binary_op (rtx insn, rtx *operands)
8310 {
8311   static char buf[30];
8312   const char *p;
8313   const char *ssep;
8314   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8315
8316 #ifdef ENABLE_CHECKING
8317   /* Even if we do not want to check the inputs, this documents input
8318      constraints.  Which helps in understanding the following code.  */
8319   if (STACK_REG_P (operands[0])
8320       && ((REG_P (operands[1])
8321            && REGNO (operands[0]) == REGNO (operands[1])
8322            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
8323           || (REG_P (operands[2])
8324               && REGNO (operands[0]) == REGNO (operands[2])
8325               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
8326       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8327     ; /* ok */
8328   else
8329     gcc_assert (is_sse);
8330 #endif
8331
8332   switch (GET_CODE (operands[3]))
8333     {
8334     case PLUS:
8335       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8336           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8337         p = "fiadd";
8338       else
8339         p = "fadd";
8340       ssep = "add";
8341       break;
8342
8343     case MINUS:
8344       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8345           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8346         p = "fisub";
8347       else
8348         p = "fsub";
8349       ssep = "sub";
8350       break;
8351
8352     case MULT:
8353       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8354           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8355         p = "fimul";
8356       else
8357         p = "fmul";
8358       ssep = "mul";
8359       break;
8360
8361     case DIV:
8362       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8363           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8364         p = "fidiv";
8365       else
8366         p = "fdiv";
8367       ssep = "div";
8368       break;
8369
8370     default:
8371       gcc_unreachable ();
8372     }
8373
8374   if (is_sse)
8375    {
8376       strcpy (buf, ssep);
8377       if (GET_MODE (operands[0]) == SFmode)
8378         strcat (buf, "ss\t{%2, %0|%0, %2}");
8379       else
8380         strcat (buf, "sd\t{%2, %0|%0, %2}");
8381       return buf;
8382    }
8383   strcpy (buf, p);
8384
8385   switch (GET_CODE (operands[3]))
8386     {
8387     case MULT:
8388     case PLUS:
8389       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8390         {
8391           rtx temp = operands[2];
8392           operands[2] = operands[1];
8393           operands[1] = temp;
8394         }
8395
8396       /* know operands[0] == operands[1].  */
8397
8398       if (GET_CODE (operands[2]) == MEM)
8399         {
8400           p = "%z2\t%2";
8401           break;
8402         }
8403
8404       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8405         {
8406           if (STACK_TOP_P (operands[0]))
8407             /* How is it that we are storing to a dead operand[2]?
8408                Well, presumably operands[1] is dead too.  We can't
8409                store the result to st(0) as st(0) gets popped on this
8410                instruction.  Instead store to operands[2] (which I
8411                think has to be st(1)).  st(1) will be popped later.
8412                gcc <= 2.8.1 didn't have this check and generated
8413                assembly code that the Unixware assembler rejected.  */
8414             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8415           else
8416             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8417           break;
8418         }
8419
8420       if (STACK_TOP_P (operands[0]))
8421         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8422       else
8423         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8424       break;
8425
8426     case MINUS:
8427     case DIV:
8428       if (GET_CODE (operands[1]) == MEM)
8429         {
8430           p = "r%z1\t%1";
8431           break;
8432         }
8433
8434       if (GET_CODE (operands[2]) == MEM)
8435         {
8436           p = "%z2\t%2";
8437           break;
8438         }
8439
8440       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8441         {
8442 #if SYSV386_COMPAT
8443           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
8444              derived assemblers, confusingly reverse the direction of
8445              the operation for fsub{r} and fdiv{r} when the
8446              destination register is not st(0).  The Intel assembler
8447              doesn't have this brain damage.  Read !SYSV386_COMPAT to
8448              figure out what the hardware really does.  */
8449           if (STACK_TOP_P (operands[0]))
8450             p = "{p\t%0, %2|rp\t%2, %0}";
8451           else
8452             p = "{rp\t%2, %0|p\t%0, %2}";
8453 #else
8454           if (STACK_TOP_P (operands[0]))
8455             /* As above for fmul/fadd, we can't store to st(0).  */
8456             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
8457           else
8458             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8459 #endif
8460           break;
8461         }
8462
8463       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
8464         {
8465 #if SYSV386_COMPAT
8466           if (STACK_TOP_P (operands[0]))
8467             p = "{rp\t%0, %1|p\t%1, %0}";
8468           else
8469             p = "{p\t%1, %0|rp\t%0, %1}";
8470 #else
8471           if (STACK_TOP_P (operands[0]))
8472             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
8473           else
8474             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
8475 #endif
8476           break;
8477         }
8478
8479       if (STACK_TOP_P (operands[0]))
8480         {
8481           if (STACK_TOP_P (operands[1]))
8482             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
8483           else
8484             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
8485           break;
8486         }
8487       else if (STACK_TOP_P (operands[1]))
8488         {
8489 #if SYSV386_COMPAT
8490           p = "{\t%1, %0|r\t%0, %1}";
8491 #else
8492           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8493 #endif
8494         }
8495       else
8496         {
8497 #if SYSV386_COMPAT
8498           p = "{r\t%2, %0|\t%0, %2}";
8499 #else
8500           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8501 #endif
8502         }
8503       break;
8504
8505     default:
8506       gcc_unreachable ();
8507     }
8508
8509   strcat (buf, p);
8510   return buf;
8511 }
8512
8513 /* Return needed mode for entity in optimize_mode_switching pass.  */
8514
8515 int
8516 ix86_mode_needed (int entity, rtx insn)
8517 {
8518   enum attr_i387_cw mode;
8519
8520   /* The mode UNINITIALIZED is used to store control word after a
8521      function call or ASM pattern.  The mode ANY specify that function
8522      has no requirements on the control word and make no changes in the
8523      bits we are interested in.  */
8524
8525   if (CALL_P (insn)
8526       || (NONJUMP_INSN_P (insn)
8527           && (asm_noperands (PATTERN (insn)) >= 0
8528               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
8529     return I387_CW_UNINITIALIZED;
8530
8531   if (recog_memoized (insn) < 0)
8532     return I387_CW_ANY;
8533
8534   mode = get_attr_i387_cw (insn);
8535
8536   switch (entity)
8537     {
8538     case I387_TRUNC:
8539       if (mode == I387_CW_TRUNC)
8540         return mode;
8541       break;
8542
8543     case I387_FLOOR:
8544       if (mode == I387_CW_FLOOR)
8545         return mode;
8546       break;
8547
8548     case I387_CEIL:
8549       if (mode == I387_CW_CEIL)
8550         return mode;
8551       break;
8552
8553     case I387_MASK_PM:
8554       if (mode == I387_CW_MASK_PM)
8555         return mode;
8556       break;
8557
8558     default:
8559       gcc_unreachable ();
8560     }
8561
8562   return I387_CW_ANY;
8563 }
8564
8565 /* Output code to initialize control word copies used by trunc?f?i and
8566    rounding patterns.  CURRENT_MODE is set to current control word,
8567    while NEW_MODE is set to new control word.  */
8568
8569 void
8570 emit_i387_cw_initialization (int mode)
8571 {
8572   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
8573   rtx new_mode;
8574
8575   int slot;
8576
8577   rtx reg = gen_reg_rtx (HImode);
8578
8579   emit_insn (gen_x86_fnstcw_1 (stored_mode));
8580   emit_move_insn (reg, stored_mode);
8581
8582   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
8583     {
8584       switch (mode)
8585         {
8586         case I387_CW_TRUNC:
8587           /* round toward zero (truncate) */
8588           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
8589           slot = SLOT_CW_TRUNC;
8590           break;
8591
8592         case I387_CW_FLOOR:
8593           /* round down toward -oo */
8594           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8595           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
8596           slot = SLOT_CW_FLOOR;
8597           break;
8598
8599         case I387_CW_CEIL:
8600           /* round up toward +oo */
8601           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8602           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
8603           slot = SLOT_CW_CEIL;
8604           break;
8605
8606         case I387_CW_MASK_PM:
8607           /* mask precision exception for nearbyint() */
8608           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8609           slot = SLOT_CW_MASK_PM;
8610           break;
8611
8612         default:
8613           gcc_unreachable ();
8614         }
8615     }
8616   else
8617     {
8618       switch (mode)
8619         {
8620         case I387_CW_TRUNC:
8621           /* round toward zero (truncate) */
8622           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8623           slot = SLOT_CW_TRUNC;
8624           break;
8625
8626         case I387_CW_FLOOR:
8627           /* round down toward -oo */
8628           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8629           slot = SLOT_CW_FLOOR;
8630           break;
8631
8632         case I387_CW_CEIL:
8633           /* round up toward +oo */
8634           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8635           slot = SLOT_CW_CEIL;
8636           break;
8637
8638         case I387_CW_MASK_PM:
8639           /* mask precision exception for nearbyint() */
8640           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8641           slot = SLOT_CW_MASK_PM;
8642           break;
8643
8644         default:
8645           gcc_unreachable ();
8646         }
8647     }
8648
8649   gcc_assert (slot < MAX_386_STACK_LOCALS);
8650
8651   new_mode = assign_386_stack_local (HImode, slot);
8652   emit_move_insn (new_mode, reg);
8653 }
8654
8655 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8656    are the insn operands.  The output may be [HSD]Imode and the input
8657    operand may be [SDX]Fmode.  */
8658
8659 const char *
8660 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8661 {
8662   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8663   int dimode_p = GET_MODE (operands[0]) == DImode;
8664   int round_mode = get_attr_i387_cw (insn);
8665
8666   /* Jump through a hoop or two for DImode, since the hardware has no
8667      non-popping instruction.  We used to do this a different way, but
8668      that was somewhat fragile and broke with post-reload splitters.  */
8669   if ((dimode_p || fisttp) && !stack_top_dies)
8670     output_asm_insn ("fld\t%y1", operands);
8671
8672   gcc_assert (STACK_TOP_P (operands[1]));
8673   gcc_assert (GET_CODE (operands[0]) == MEM);
8674
8675   if (fisttp)
8676       output_asm_insn ("fisttp%z0\t%0", operands);
8677   else
8678     {
8679       if (round_mode != I387_CW_ANY)
8680         output_asm_insn ("fldcw\t%3", operands);
8681       if (stack_top_dies || dimode_p)
8682         output_asm_insn ("fistp%z0\t%0", operands);
8683       else
8684         output_asm_insn ("fist%z0\t%0", operands);
8685       if (round_mode != I387_CW_ANY)
8686         output_asm_insn ("fldcw\t%2", operands);
8687     }
8688
8689   return "";
8690 }
8691
8692 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
8693    have the values zero or one, indicates the ffreep insn's operand
8694    from the OPERANDS array.  */
8695
8696 static const char *
8697 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
8698 {
8699   if (TARGET_USE_FFREEP)
8700 #if HAVE_AS_IX86_FFREEP
8701     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
8702 #else
8703     {
8704       static char retval[] = ".word\t0xc_df";
8705       int regno = REGNO (operands[opno]);
8706       
8707       gcc_assert (FP_REGNO_P (regno));
8708
8709       retval[9] = '0' + (regno - FIRST_STACK_REG);
8710       return retval;
8711     }
8712 #endif
8713
8714   return opno ? "fstp\t%y1" : "fstp\t%y0";
8715 }
8716
8717
8718 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
8719    should be used.  UNORDERED_P is true when fucom should be used.  */
8720
8721 const char *
8722 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
8723 {
8724   int stack_top_dies;
8725   rtx cmp_op0, cmp_op1;
8726   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
8727
8728   if (eflags_p)
8729     {
8730       cmp_op0 = operands[0];
8731       cmp_op1 = operands[1];
8732     }
8733   else
8734     {
8735       cmp_op0 = operands[1];
8736       cmp_op1 = operands[2];
8737     }
8738
8739   if (is_sse)
8740     {
8741       if (GET_MODE (operands[0]) == SFmode)
8742         if (unordered_p)
8743           return "ucomiss\t{%1, %0|%0, %1}";
8744         else
8745           return "comiss\t{%1, %0|%0, %1}";
8746       else
8747         if (unordered_p)
8748           return "ucomisd\t{%1, %0|%0, %1}";
8749         else
8750           return "comisd\t{%1, %0|%0, %1}";
8751     }
8752
8753   gcc_assert (STACK_TOP_P (cmp_op0));
8754
8755   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8756
8757   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
8758     {
8759       if (stack_top_dies)
8760         {
8761           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
8762           return output_387_ffreep (operands, 1);
8763         }
8764       else
8765         return "ftst\n\tfnstsw\t%0";
8766     }
8767
8768   if (STACK_REG_P (cmp_op1)
8769       && stack_top_dies
8770       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
8771       && REGNO (cmp_op1) != FIRST_STACK_REG)
8772     {
8773       /* If both the top of the 387 stack dies, and the other operand
8774          is also a stack register that dies, then this must be a
8775          `fcompp' float compare */
8776
8777       if (eflags_p)
8778         {
8779           /* There is no double popping fcomi variant.  Fortunately,
8780              eflags is immune from the fstp's cc clobbering.  */
8781           if (unordered_p)
8782             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
8783           else
8784             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
8785           return output_387_ffreep (operands, 0);
8786         }
8787       else
8788         {
8789           if (unordered_p)
8790             return "fucompp\n\tfnstsw\t%0";
8791           else
8792             return "fcompp\n\tfnstsw\t%0";
8793         }
8794     }
8795   else
8796     {
8797       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
8798
8799       static const char * const alt[16] =
8800       {
8801         "fcom%z2\t%y2\n\tfnstsw\t%0",
8802         "fcomp%z2\t%y2\n\tfnstsw\t%0",
8803         "fucom%z2\t%y2\n\tfnstsw\t%0",
8804         "fucomp%z2\t%y2\n\tfnstsw\t%0",
8805
8806         "ficom%z2\t%y2\n\tfnstsw\t%0",
8807         "ficomp%z2\t%y2\n\tfnstsw\t%0",
8808         NULL,
8809         NULL,
8810
8811         "fcomi\t{%y1, %0|%0, %y1}",
8812         "fcomip\t{%y1, %0|%0, %y1}",
8813         "fucomi\t{%y1, %0|%0, %y1}",
8814         "fucomip\t{%y1, %0|%0, %y1}",
8815
8816         NULL,
8817         NULL,
8818         NULL,
8819         NULL
8820       };
8821
8822       int mask;
8823       const char *ret;
8824
8825       mask  = eflags_p << 3;
8826       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
8827       mask |= unordered_p << 1;
8828       mask |= stack_top_dies;
8829
8830       gcc_assert (mask < 16);
8831       ret = alt[mask];
8832       gcc_assert (ret);
8833
8834       return ret;
8835     }
8836 }
8837
8838 void
8839 ix86_output_addr_vec_elt (FILE *file, int value)
8840 {
8841   const char *directive = ASM_LONG;
8842
8843 #ifdef ASM_QUAD
8844   if (TARGET_64BIT)
8845     directive = ASM_QUAD;
8846 #else
8847   gcc_assert (!TARGET_64BIT);
8848 #endif
8849
8850   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
8851 }
8852
8853 void
8854 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
8855 {
8856   if (TARGET_64BIT)
8857     fprintf (file, "%s%s%d-%s%d\n",
8858              ASM_LONG, LPREFIX, value, LPREFIX, rel);
8859   else if (HAVE_AS_GOTOFF_IN_DATA)
8860     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
8861 #if TARGET_MACHO
8862   else if (TARGET_MACHO)
8863     {
8864       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
8865       machopic_output_function_base_name (file);
8866       fprintf(file, "\n");
8867     }
8868 #endif
8869   else
8870     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
8871                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
8872 }
8873 \f
8874 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
8875    for the target.  */
8876
8877 void
8878 ix86_expand_clear (rtx dest)
8879 {
8880   rtx tmp;
8881
8882   /* We play register width games, which are only valid after reload.  */
8883   gcc_assert (reload_completed);
8884
8885   /* Avoid HImode and its attendant prefix byte.  */
8886   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
8887     dest = gen_rtx_REG (SImode, REGNO (dest));
8888
8889   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
8890
8891   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
8892   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
8893     {
8894       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
8895       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8896     }
8897
8898   emit_insn (tmp);
8899 }
8900
8901 /* X is an unchanging MEM.  If it is a constant pool reference, return
8902    the constant pool rtx, else NULL.  */
8903
8904 rtx
8905 maybe_get_pool_constant (rtx x)
8906 {
8907   x = ix86_delegitimize_address (XEXP (x, 0));
8908
8909   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8910     return get_pool_constant (x);
8911
8912   return NULL_RTX;
8913 }
8914
8915 void
8916 ix86_expand_move (enum machine_mode mode, rtx operands[])
8917 {
8918   int strict = (reload_in_progress || reload_completed);
8919   rtx op0, op1;
8920   enum tls_model model;
8921
8922   op0 = operands[0];
8923   op1 = operands[1];
8924
8925   if (GET_CODE (op1) == SYMBOL_REF)
8926     {
8927       model = SYMBOL_REF_TLS_MODEL (op1);
8928       if (model)
8929         {
8930           op1 = legitimize_tls_address (op1, model, true);
8931           op1 = force_operand (op1, op0);
8932           if (op1 == op0)
8933             return;
8934         }
8935     }
8936   else if (GET_CODE (op1) == CONST
8937            && GET_CODE (XEXP (op1, 0)) == PLUS
8938            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
8939     {
8940       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
8941       if (model)
8942         {
8943           rtx addend = XEXP (XEXP (op1, 0), 1);
8944           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
8945           op1 = force_operand (op1, NULL);
8946           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
8947                                      op0, 1, OPTAB_DIRECT);
8948           if (op1 == op0)
8949             return;
8950         }
8951     }
8952
8953   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8954     {
8955       if (TARGET_MACHO && !TARGET_64BIT)
8956         {
8957 #if TARGET_MACHO
8958           if (MACHOPIC_PURE)
8959             {
8960               rtx temp = ((reload_in_progress
8961                            || ((op0 && GET_CODE (op0) == REG)
8962                                && mode == Pmode))
8963                           ? op0 : gen_reg_rtx (Pmode));
8964               op1 = machopic_indirect_data_reference (op1, temp);
8965               op1 = machopic_legitimize_pic_address (op1, mode,
8966                                                      temp == op1 ? 0 : temp);
8967             }
8968           else if (MACHOPIC_INDIRECT)
8969             op1 = machopic_indirect_data_reference (op1, 0);
8970           if (op0 == op1)
8971             return;
8972 #endif
8973         }
8974       else
8975         {
8976           if (GET_CODE (op0) == MEM)
8977             op1 = force_reg (Pmode, op1);
8978           else
8979             op1 = legitimize_address (op1, op1, Pmode);
8980         }
8981     }
8982   else
8983     {
8984       if (GET_CODE (op0) == MEM
8985           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8986               || !push_operand (op0, mode))
8987           && GET_CODE (op1) == MEM)
8988         op1 = force_reg (mode, op1);
8989
8990       if (push_operand (op0, mode)
8991           && ! general_no_elim_operand (op1, mode))
8992         op1 = copy_to_mode_reg (mode, op1);
8993
8994       /* Force large constants in 64bit compilation into register
8995          to get them CSEed.  */
8996       if (TARGET_64BIT && mode == DImode
8997           && immediate_operand (op1, mode)
8998           && !x86_64_zext_immediate_operand (op1, VOIDmode)
8999           && !register_operand (op0, mode)
9000           && optimize && !reload_completed && !reload_in_progress)
9001         op1 = copy_to_mode_reg (mode, op1);
9002
9003       if (FLOAT_MODE_P (mode))
9004         {
9005           /* If we are loading a floating point constant to a register,
9006              force the value to memory now, since we'll get better code
9007              out the back end.  */
9008
9009           if (strict)
9010             ;
9011           else if (GET_CODE (op1) == CONST_DOUBLE)
9012             {
9013               op1 = validize_mem (force_const_mem (mode, op1));
9014               if (!register_operand (op0, mode))
9015                 {
9016                   rtx temp = gen_reg_rtx (mode);
9017                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
9018                   emit_move_insn (op0, temp);
9019                   return;
9020                 }
9021             }
9022         }
9023     }
9024
9025   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9026 }
9027
9028 void
9029 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9030 {
9031   rtx op0 = operands[0], op1 = operands[1];
9032
9033   /* Force constants other than zero into memory.  We do not know how
9034      the instructions used to build constants modify the upper 64 bits
9035      of the register, once we have that information we may be able
9036      to handle some of them more efficiently.  */
9037   if ((reload_in_progress | reload_completed) == 0
9038       && register_operand (op0, mode)
9039       && CONSTANT_P (op1)
9040       && standard_sse_constant_p (op1) <= 0)
9041     op1 = validize_mem (force_const_mem (mode, op1));
9042
9043   /* Make operand1 a register if it isn't already.  */
9044   if (!no_new_pseudos
9045       && !register_operand (op0, mode)
9046       && !register_operand (op1, mode))
9047     {
9048       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9049       return;
9050     }
9051
9052   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9053 }
9054
9055 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9056    straight to ix86_expand_vector_move.  */
9057
9058 void
9059 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9060 {
9061   rtx op0, op1, m;
9062
9063   op0 = operands[0];
9064   op1 = operands[1];
9065
9066   if (MEM_P (op1))
9067     {
9068       /* If we're optimizing for size, movups is the smallest.  */
9069       if (optimize_size)
9070         {
9071           op0 = gen_lowpart (V4SFmode, op0);
9072           op1 = gen_lowpart (V4SFmode, op1);
9073           emit_insn (gen_sse_movups (op0, op1));
9074           return;
9075         }
9076
9077       /* ??? If we have typed data, then it would appear that using
9078          movdqu is the only way to get unaligned data loaded with
9079          integer type.  */
9080       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9081         {
9082           op0 = gen_lowpart (V16QImode, op0);
9083           op1 = gen_lowpart (V16QImode, op1);
9084           emit_insn (gen_sse2_movdqu (op0, op1));
9085           return;
9086         }
9087
9088       if (TARGET_SSE2 && mode == V2DFmode)
9089         {
9090           rtx zero;
9091
9092           /* When SSE registers are split into halves, we can avoid
9093              writing to the top half twice.  */
9094           if (TARGET_SSE_SPLIT_REGS)
9095             {
9096               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9097               zero = op0;
9098             }
9099           else
9100             {
9101               /* ??? Not sure about the best option for the Intel chips.
9102                  The following would seem to satisfy; the register is
9103                  entirely cleared, breaking the dependency chain.  We
9104                  then store to the upper half, with a dependency depth
9105                  of one.  A rumor has it that Intel recommends two movsd
9106                  followed by an unpacklpd, but this is unconfirmed.  And
9107                  given that the dependency depth of the unpacklpd would
9108                  still be one, I'm not sure why this would be better.  */
9109               zero = CONST0_RTX (V2DFmode);
9110             }
9111
9112           m = adjust_address (op1, DFmode, 0);
9113           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9114           m = adjust_address (op1, DFmode, 8);
9115           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9116         }
9117       else
9118         {
9119           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9120             emit_move_insn (op0, CONST0_RTX (mode));
9121           else
9122             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9123
9124           if (mode != V4SFmode)
9125             op0 = gen_lowpart (V4SFmode, op0);
9126           m = adjust_address (op1, V2SFmode, 0);
9127           emit_insn (gen_sse_loadlps (op0, op0, m));
9128           m = adjust_address (op1, V2SFmode, 8);
9129           emit_insn (gen_sse_loadhps (op0, op0, m));
9130         }
9131     }
9132   else if (MEM_P (op0))
9133     {
9134       /* If we're optimizing for size, movups is the smallest.  */
9135       if (optimize_size)
9136         {
9137           op0 = gen_lowpart (V4SFmode, op0);
9138           op1 = gen_lowpart (V4SFmode, op1);
9139           emit_insn (gen_sse_movups (op0, op1));
9140           return;
9141         }
9142
9143       /* ??? Similar to above, only less clear because of quote
9144          typeless stores unquote.  */
9145       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9146           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9147         {
9148           op0 = gen_lowpart (V16QImode, op0);
9149           op1 = gen_lowpart (V16QImode, op1);
9150           emit_insn (gen_sse2_movdqu (op0, op1));
9151           return;
9152         }
9153
9154       if (TARGET_SSE2 && mode == V2DFmode)
9155         {
9156           m = adjust_address (op0, DFmode, 0);
9157           emit_insn (gen_sse2_storelpd (m, op1));
9158           m = adjust_address (op0, DFmode, 8);
9159           emit_insn (gen_sse2_storehpd (m, op1));
9160         }
9161       else
9162         {
9163           if (mode != V4SFmode)
9164             op1 = gen_lowpart (V4SFmode, op1);
9165           m = adjust_address (op0, V2SFmode, 0);
9166           emit_insn (gen_sse_storelps (m, op1));
9167           m = adjust_address (op0, V2SFmode, 8);
9168           emit_insn (gen_sse_storehps (m, op1));
9169         }
9170     }
9171   else
9172     gcc_unreachable ();
9173 }
9174
9175 /* Expand a push in MODE.  This is some mode for which we do not support
9176    proper push instructions, at least from the registers that we expect
9177    the value to live in.  */
9178
9179 void
9180 ix86_expand_push (enum machine_mode mode, rtx x)
9181 {
9182   rtx tmp;
9183
9184   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9185                              GEN_INT (-GET_MODE_SIZE (mode)),
9186                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9187   if (tmp != stack_pointer_rtx)
9188     emit_move_insn (stack_pointer_rtx, tmp);
9189
9190   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9191   emit_move_insn (tmp, x);
9192 }
9193
9194 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9195    destination to use for the operation.  If different from the true
9196    destination in operands[0], a copy operation will be required.  */
9197
9198 rtx
9199 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9200                             rtx operands[])
9201 {
9202   int matching_memory;
9203   rtx src1, src2, dst;
9204
9205   dst = operands[0];
9206   src1 = operands[1];
9207   src2 = operands[2];
9208
9209   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
9210   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9211       && (rtx_equal_p (dst, src2)
9212           || immediate_operand (src1, mode)))
9213     {
9214       rtx temp = src1;
9215       src1 = src2;
9216       src2 = temp;
9217     }
9218
9219   /* If the destination is memory, and we do not have matching source
9220      operands, do things in registers.  */
9221   matching_memory = 0;
9222   if (GET_CODE (dst) == MEM)
9223     {
9224       if (rtx_equal_p (dst, src1))
9225         matching_memory = 1;
9226       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9227                && rtx_equal_p (dst, src2))
9228         matching_memory = 2;
9229       else
9230         dst = gen_reg_rtx (mode);
9231     }
9232
9233   /* Both source operands cannot be in memory.  */
9234   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
9235     {
9236       if (matching_memory != 2)
9237         src2 = force_reg (mode, src2);
9238       else
9239         src1 = force_reg (mode, src1);
9240     }
9241
9242   /* If the operation is not commutable, source 1 cannot be a constant
9243      or non-matching memory.  */
9244   if ((CONSTANT_P (src1)
9245        || (!matching_memory && GET_CODE (src1) == MEM))
9246       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9247     src1 = force_reg (mode, src1);
9248
9249   src1 = operands[1] = src1;
9250   src2 = operands[2] = src2;
9251   return dst;
9252 }
9253
9254 /* Similarly, but assume that the destination has already been
9255    set up properly.  */
9256
9257 void
9258 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9259                                     enum machine_mode mode, rtx operands[])
9260 {
9261   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9262   gcc_assert (dst == operands[0]);
9263 }
9264
9265 /* Attempt to expand a binary operator.  Make the expansion closer to the
9266    actual machine, then just general_operand, which will allow 3 separate
9267    memory references (one output, two input) in a single insn.  */
9268
9269 void
9270 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9271                              rtx operands[])
9272 {
9273   rtx src1, src2, dst, op, clob;
9274
9275   dst = ix86_fixup_binary_operands (code, mode, operands);
9276   src1 = operands[1];
9277   src2 = operands[2];
9278
9279  /* Emit the instruction.  */
9280
9281   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9282   if (reload_in_progress)
9283     {
9284       /* Reload doesn't know about the flags register, and doesn't know that
9285          it doesn't want to clobber it.  We can only do this with PLUS.  */
9286       gcc_assert (code == PLUS);
9287       emit_insn (op);
9288     }
9289   else
9290     {
9291       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9292       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9293     }
9294
9295   /* Fix up the destination if needed.  */
9296   if (dst != operands[0])
9297     emit_move_insn (operands[0], dst);
9298 }
9299
9300 /* Return TRUE or FALSE depending on whether the binary operator meets the
9301    appropriate constraints.  */
9302
9303 int
9304 ix86_binary_operator_ok (enum rtx_code code,
9305                          enum machine_mode mode ATTRIBUTE_UNUSED,
9306                          rtx operands[3])
9307 {
9308   /* Both source operands cannot be in memory.  */
9309   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
9310     return 0;
9311   /* If the operation is not commutable, source 1 cannot be a constant.  */
9312   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9313     return 0;
9314   /* If the destination is memory, we must have a matching source operand.  */
9315   if (GET_CODE (operands[0]) == MEM
9316       && ! (rtx_equal_p (operands[0], operands[1])
9317             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9318                 && rtx_equal_p (operands[0], operands[2]))))
9319     return 0;
9320   /* If the operation is not commutable and the source 1 is memory, we must
9321      have a matching destination.  */
9322   if (GET_CODE (operands[1]) == MEM
9323       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
9324       && ! rtx_equal_p (operands[0], operands[1]))
9325     return 0;
9326   return 1;
9327 }
9328
9329 /* Attempt to expand a unary operator.  Make the expansion closer to the
9330    actual machine, then just general_operand, which will allow 2 separate
9331    memory references (one output, one input) in a single insn.  */
9332
9333 void
9334 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
9335                             rtx operands[])
9336 {
9337   int matching_memory;
9338   rtx src, dst, op, clob;
9339
9340   dst = operands[0];
9341   src = operands[1];
9342
9343   /* If the destination is memory, and we do not have matching source
9344      operands, do things in registers.  */
9345   matching_memory = 0;
9346   if (MEM_P (dst))
9347     {
9348       if (rtx_equal_p (dst, src))
9349         matching_memory = 1;
9350       else
9351         dst = gen_reg_rtx (mode);
9352     }
9353
9354   /* When source operand is memory, destination must match.  */
9355   if (MEM_P (src) && !matching_memory)
9356     src = force_reg (mode, src);
9357
9358   /* Emit the instruction.  */
9359
9360   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
9361   if (reload_in_progress || code == NOT)
9362     {
9363       /* Reload doesn't know about the flags register, and doesn't know that
9364          it doesn't want to clobber it.  */
9365       gcc_assert (code == NOT);
9366       emit_insn (op);
9367     }
9368   else
9369     {
9370       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9371       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9372     }
9373
9374   /* Fix up the destination if needed.  */
9375   if (dst != operands[0])
9376     emit_move_insn (operands[0], dst);
9377 }
9378
9379 /* Return TRUE or FALSE depending on whether the unary operator meets the
9380    appropriate constraints.  */
9381
9382 int
9383 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
9384                         enum machine_mode mode ATTRIBUTE_UNUSED,
9385                         rtx operands[2] ATTRIBUTE_UNUSED)
9386 {
9387   /* If one of operands is memory, source and destination must match.  */
9388   if ((GET_CODE (operands[0]) == MEM
9389        || GET_CODE (operands[1]) == MEM)
9390       && ! rtx_equal_p (operands[0], operands[1]))
9391     return FALSE;
9392   return TRUE;
9393 }
9394
9395 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
9396    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
9397    true, then replicate the mask for all elements of the vector register.
9398    If INVERT is true, then create a mask excluding the sign bit.  */
9399
9400 rtx
9401 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
9402 {
9403   enum machine_mode vec_mode;
9404   HOST_WIDE_INT hi, lo;
9405   int shift = 63;
9406   rtvec v;
9407   rtx mask;
9408
9409   /* Find the sign bit, sign extended to 2*HWI.  */
9410   if (mode == SFmode)
9411     lo = 0x80000000, hi = lo < 0;
9412   else if (HOST_BITS_PER_WIDE_INT >= 64)
9413     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
9414   else
9415     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
9416
9417   if (invert)
9418     lo = ~lo, hi = ~hi;
9419
9420   /* Force this value into the low part of a fp vector constant.  */
9421   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
9422   mask = gen_lowpart (mode, mask);
9423
9424   if (mode == SFmode)
9425     {
9426       if (vect)
9427         v = gen_rtvec (4, mask, mask, mask, mask);
9428       else
9429         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
9430                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9431       vec_mode = V4SFmode;
9432     }
9433   else
9434     {
9435       if (vect)
9436         v = gen_rtvec (2, mask, mask);
9437       else
9438         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
9439       vec_mode = V2DFmode;
9440     }
9441
9442   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
9443 }
9444
9445 /* Generate code for floating point ABS or NEG.  */
9446
9447 void
9448 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
9449                                 rtx operands[])
9450 {
9451   rtx mask, set, use, clob, dst, src;
9452   bool matching_memory;
9453   bool use_sse = false;
9454   bool vector_mode = VECTOR_MODE_P (mode);
9455   enum machine_mode elt_mode = mode;
9456
9457   if (vector_mode)
9458     {
9459       elt_mode = GET_MODE_INNER (mode);
9460       use_sse = true;
9461     }
9462   else if (TARGET_SSE_MATH)
9463     use_sse = SSE_FLOAT_MODE_P (mode);
9464
9465   /* NEG and ABS performed with SSE use bitwise mask operations.
9466      Create the appropriate mask now.  */
9467   if (use_sse)
9468     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
9469   else
9470     mask = NULL_RTX;
9471
9472   dst = operands[0];
9473   src = operands[1];
9474
9475   /* If the destination is memory, and we don't have matching source
9476      operands or we're using the x87, do things in registers.  */
9477   matching_memory = false;
9478   if (MEM_P (dst))
9479     {
9480       if (use_sse && rtx_equal_p (dst, src))
9481         matching_memory = true;
9482       else
9483         dst = gen_reg_rtx (mode);
9484     }
9485   if (MEM_P (src) && !matching_memory)
9486     src = force_reg (mode, src);
9487
9488   if (vector_mode)
9489     {
9490       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
9491       set = gen_rtx_SET (VOIDmode, dst, set);
9492       emit_insn (set);
9493     }
9494   else
9495     {
9496       set = gen_rtx_fmt_e (code, mode, src);
9497       set = gen_rtx_SET (VOIDmode, dst, set);
9498       if (mask)
9499         {
9500           use = gen_rtx_USE (VOIDmode, mask);
9501           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9502           emit_insn (gen_rtx_PARALLEL (VOIDmode,
9503                                        gen_rtvec (3, set, use, clob)));
9504         }
9505       else
9506         emit_insn (set);
9507     }
9508
9509   if (dst != operands[0])
9510     emit_move_insn (operands[0], dst);
9511 }
9512
9513 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
9514
9515 void
9516 ix86_expand_copysign (rtx operands[])
9517 {
9518   enum machine_mode mode, vmode;
9519   rtx dest, op0, op1, mask, nmask;
9520
9521   dest = operands[0];
9522   op0 = operands[1];
9523   op1 = operands[2];
9524
9525   mode = GET_MODE (dest);
9526   vmode = mode == SFmode ? V4SFmode : V2DFmode;
9527
9528   if (GET_CODE (op0) == CONST_DOUBLE)
9529     {
9530       rtvec v;
9531
9532       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
9533         op0 = simplify_unary_operation (ABS, mode, op0, mode);
9534
9535       if (op0 == CONST0_RTX (mode))
9536         op0 = CONST0_RTX (vmode);
9537       else
9538         {
9539           if (mode == SFmode)
9540             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
9541                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9542           else
9543             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
9544           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
9545         }
9546
9547       mask = ix86_build_signbit_mask (mode, 0, 0);
9548
9549       if (mode == SFmode)
9550         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
9551       else
9552         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
9553     }
9554   else
9555     {
9556       nmask = ix86_build_signbit_mask (mode, 0, 1);
9557       mask = ix86_build_signbit_mask (mode, 0, 0);
9558
9559       if (mode == SFmode)
9560         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
9561       else
9562         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
9563     }
9564 }
9565
9566 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
9567    be a constant, and so has already been expanded into a vector constant.  */
9568
9569 void
9570 ix86_split_copysign_const (rtx operands[])
9571 {
9572   enum machine_mode mode, vmode;
9573   rtx dest, op0, op1, mask, x;
9574
9575   dest = operands[0];
9576   op0 = operands[1];
9577   op1 = operands[2];
9578   mask = operands[3];
9579
9580   mode = GET_MODE (dest);
9581   vmode = GET_MODE (mask);
9582
9583   dest = simplify_gen_subreg (vmode, dest, mode, 0);
9584   x = gen_rtx_AND (vmode, dest, mask);
9585   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9586
9587   if (op0 != CONST0_RTX (vmode))
9588     {
9589       x = gen_rtx_IOR (vmode, dest, op0);
9590       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9591     }
9592 }
9593
9594 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
9595    so we have to do two masks.  */
9596
9597 void
9598 ix86_split_copysign_var (rtx operands[])
9599 {
9600   enum machine_mode mode, vmode;
9601   rtx dest, scratch, op0, op1, mask, nmask, x;
9602
9603   dest = operands[0];
9604   scratch = operands[1];
9605   op0 = operands[2];
9606   op1 = operands[3];
9607   nmask = operands[4];
9608   mask = operands[5];
9609
9610   mode = GET_MODE (dest);
9611   vmode = GET_MODE (mask);
9612
9613   if (rtx_equal_p (op0, op1))
9614     {
9615       /* Shouldn't happen often (it's useless, obviously), but when it does
9616          we'd generate incorrect code if we continue below.  */
9617       emit_move_insn (dest, op0);
9618       return;
9619     }
9620
9621   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
9622     {
9623       gcc_assert (REGNO (op1) == REGNO (scratch));
9624
9625       x = gen_rtx_AND (vmode, scratch, mask);
9626       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9627
9628       dest = mask;
9629       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9630       x = gen_rtx_NOT (vmode, dest);
9631       x = gen_rtx_AND (vmode, x, op0);
9632       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9633     }
9634   else
9635     {
9636       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
9637         {
9638           x = gen_rtx_AND (vmode, scratch, mask);
9639         }
9640       else                                              /* alternative 2,4 */
9641         {
9642           gcc_assert (REGNO (mask) == REGNO (scratch));
9643           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
9644           x = gen_rtx_AND (vmode, scratch, op1);
9645         }
9646       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9647
9648       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
9649         {
9650           dest = simplify_gen_subreg (vmode, op0, mode, 0);
9651           x = gen_rtx_AND (vmode, dest, nmask);
9652         }
9653       else                                              /* alternative 3,4 */
9654         {
9655           gcc_assert (REGNO (nmask) == REGNO (dest));
9656           dest = nmask;
9657           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9658           x = gen_rtx_AND (vmode, dest, op0);
9659         }
9660       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9661     }
9662
9663   x = gen_rtx_IOR (vmode, dest, scratch);
9664   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9665 }
9666
9667 /* Return TRUE or FALSE depending on whether the first SET in INSN
9668    has source and destination with matching CC modes, and that the
9669    CC mode is at least as constrained as REQ_MODE.  */
9670
9671 int
9672 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
9673 {
9674   rtx set;
9675   enum machine_mode set_mode;
9676
9677   set = PATTERN (insn);
9678   if (GET_CODE (set) == PARALLEL)
9679     set = XVECEXP (set, 0, 0);
9680   gcc_assert (GET_CODE (set) == SET);
9681   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
9682
9683   set_mode = GET_MODE (SET_DEST (set));
9684   switch (set_mode)
9685     {
9686     case CCNOmode:
9687       if (req_mode != CCNOmode
9688           && (req_mode != CCmode
9689               || XEXP (SET_SRC (set), 1) != const0_rtx))
9690         return 0;
9691       break;
9692     case CCmode:
9693       if (req_mode == CCGCmode)
9694         return 0;
9695       /* FALLTHRU */
9696     case CCGCmode:
9697       if (req_mode == CCGOCmode || req_mode == CCNOmode)
9698         return 0;
9699       /* FALLTHRU */
9700     case CCGOCmode:
9701       if (req_mode == CCZmode)
9702         return 0;
9703       /* FALLTHRU */
9704     case CCZmode:
9705       break;
9706
9707     default:
9708       gcc_unreachable ();
9709     }
9710
9711   return (GET_MODE (SET_SRC (set)) == set_mode);
9712 }
9713
9714 /* Generate insn patterns to do an integer compare of OPERANDS.  */
9715
9716 static rtx
9717 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
9718 {
9719   enum machine_mode cmpmode;
9720   rtx tmp, flags;
9721
9722   cmpmode = SELECT_CC_MODE (code, op0, op1);
9723   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
9724
9725   /* This is very simple, but making the interface the same as in the
9726      FP case makes the rest of the code easier.  */
9727   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
9728   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
9729
9730   /* Return the test that should be put into the flags user, i.e.
9731      the bcc, scc, or cmov instruction.  */
9732   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
9733 }
9734
9735 /* Figure out whether to use ordered or unordered fp comparisons.
9736    Return the appropriate mode to use.  */
9737
9738 enum machine_mode
9739 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
9740 {
9741   /* ??? In order to make all comparisons reversible, we do all comparisons
9742      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
9743      all forms trapping and nontrapping comparisons, we can make inequality
9744      comparisons trapping again, since it results in better code when using
9745      FCOM based compares.  */
9746   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
9747 }
9748
9749 enum machine_mode
9750 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
9751 {
9752   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
9753     return ix86_fp_compare_mode (code);
9754   switch (code)
9755     {
9756       /* Only zero flag is needed.  */
9757     case EQ:                    /* ZF=0 */
9758     case NE:                    /* ZF!=0 */
9759       return CCZmode;
9760       /* Codes needing carry flag.  */
9761     case GEU:                   /* CF=0 */
9762     case GTU:                   /* CF=0 & ZF=0 */
9763     case LTU:                   /* CF=1 */
9764     case LEU:                   /* CF=1 | ZF=1 */
9765       return CCmode;
9766       /* Codes possibly doable only with sign flag when
9767          comparing against zero.  */
9768     case GE:                    /* SF=OF   or   SF=0 */
9769     case LT:                    /* SF<>OF  or   SF=1 */
9770       if (op1 == const0_rtx)
9771         return CCGOCmode;
9772       else
9773         /* For other cases Carry flag is not required.  */
9774         return CCGCmode;
9775       /* Codes doable only with sign flag when comparing
9776          against zero, but we miss jump instruction for it
9777          so we need to use relational tests against overflow
9778          that thus needs to be zero.  */
9779     case GT:                    /* ZF=0 & SF=OF */
9780     case LE:                    /* ZF=1 | SF<>OF */
9781       if (op1 == const0_rtx)
9782         return CCNOmode;
9783       else
9784         return CCGCmode;
9785       /* strcmp pattern do (use flags) and combine may ask us for proper
9786          mode.  */
9787     case USE:
9788       return CCmode;
9789     default:
9790       gcc_unreachable ();
9791     }
9792 }
9793
9794 /* Return the fixed registers used for condition codes.  */
9795
9796 static bool
9797 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
9798 {
9799   *p1 = FLAGS_REG;
9800   *p2 = FPSR_REG;
9801   return true;
9802 }
9803
9804 /* If two condition code modes are compatible, return a condition code
9805    mode which is compatible with both.  Otherwise, return
9806    VOIDmode.  */
9807
9808 static enum machine_mode
9809 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
9810 {
9811   if (m1 == m2)
9812     return m1;
9813
9814   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
9815     return VOIDmode;
9816
9817   if ((m1 == CCGCmode && m2 == CCGOCmode)
9818       || (m1 == CCGOCmode && m2 == CCGCmode))
9819     return CCGCmode;
9820
9821   switch (m1)
9822     {
9823     default:
9824       gcc_unreachable ();
9825
9826     case CCmode:
9827     case CCGCmode:
9828     case CCGOCmode:
9829     case CCNOmode:
9830     case CCZmode:
9831       switch (m2)
9832         {
9833         default:
9834           return VOIDmode;
9835
9836         case CCmode:
9837         case CCGCmode:
9838         case CCGOCmode:
9839         case CCNOmode:
9840         case CCZmode:
9841           return CCmode;
9842         }
9843
9844     case CCFPmode:
9845     case CCFPUmode:
9846       /* These are only compatible with themselves, which we already
9847          checked above.  */
9848       return VOIDmode;
9849     }
9850 }
9851
9852 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
9853
9854 int
9855 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
9856 {
9857   enum rtx_code swapped_code = swap_condition (code);
9858   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
9859           || (ix86_fp_comparison_cost (swapped_code)
9860               == ix86_fp_comparison_fcomi_cost (swapped_code)));
9861 }
9862
9863 /* Swap, force into registers, or otherwise massage the two operands
9864    to a fp comparison.  The operands are updated in place; the new
9865    comparison code is returned.  */
9866
9867 static enum rtx_code
9868 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
9869 {
9870   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
9871   rtx op0 = *pop0, op1 = *pop1;
9872   enum machine_mode op_mode = GET_MODE (op0);
9873   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
9874
9875   /* All of the unordered compare instructions only work on registers.
9876      The same is true of the fcomi compare instructions.  The XFmode
9877      compare instructions require registers except when comparing
9878      against zero or when converting operand 1 from fixed point to
9879      floating point.  */
9880
9881   if (!is_sse
9882       && (fpcmp_mode == CCFPUmode
9883           || (op_mode == XFmode
9884               && ! (standard_80387_constant_p (op0) == 1
9885                     || standard_80387_constant_p (op1) == 1)
9886               && GET_CODE (op1) != FLOAT)
9887           || ix86_use_fcomi_compare (code)))
9888     {
9889       op0 = force_reg (op_mode, op0);
9890       op1 = force_reg (op_mode, op1);
9891     }
9892   else
9893     {
9894       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
9895          things around if they appear profitable, otherwise force op0
9896          into a register.  */
9897
9898       if (standard_80387_constant_p (op0) == 0
9899           || (GET_CODE (op0) == MEM
9900               && ! (standard_80387_constant_p (op1) == 0
9901                     || GET_CODE (op1) == MEM)))
9902         {
9903           rtx tmp;
9904           tmp = op0, op0 = op1, op1 = tmp;
9905           code = swap_condition (code);
9906         }
9907
9908       if (GET_CODE (op0) != REG)
9909         op0 = force_reg (op_mode, op0);
9910
9911       if (CONSTANT_P (op1))
9912         {
9913           int tmp = standard_80387_constant_p (op1);
9914           if (tmp == 0)
9915             op1 = validize_mem (force_const_mem (op_mode, op1));
9916           else if (tmp == 1)
9917             {
9918               if (TARGET_CMOVE)
9919                 op1 = force_reg (op_mode, op1);
9920             }
9921           else
9922             op1 = force_reg (op_mode, op1);
9923         }
9924     }
9925
9926   /* Try to rearrange the comparison to make it cheaper.  */
9927   if (ix86_fp_comparison_cost (code)
9928       > ix86_fp_comparison_cost (swap_condition (code))
9929       && (GET_CODE (op1) == REG || !no_new_pseudos))
9930     {
9931       rtx tmp;
9932       tmp = op0, op0 = op1, op1 = tmp;
9933       code = swap_condition (code);
9934       if (GET_CODE (op0) != REG)
9935         op0 = force_reg (op_mode, op0);
9936     }
9937
9938   *pop0 = op0;
9939   *pop1 = op1;
9940   return code;
9941 }
9942
9943 /* Convert comparison codes we use to represent FP comparison to integer
9944    code that will result in proper branch.  Return UNKNOWN if no such code
9945    is available.  */
9946
9947 enum rtx_code
9948 ix86_fp_compare_code_to_integer (enum rtx_code code)
9949 {
9950   switch (code)
9951     {
9952     case GT:
9953       return GTU;
9954     case GE:
9955       return GEU;
9956     case ORDERED:
9957     case UNORDERED:
9958       return code;
9959       break;
9960     case UNEQ:
9961       return EQ;
9962       break;
9963     case UNLT:
9964       return LTU;
9965       break;
9966     case UNLE:
9967       return LEU;
9968       break;
9969     case LTGT:
9970       return NE;
9971       break;
9972     default:
9973       return UNKNOWN;
9974     }
9975 }
9976
9977 /* Split comparison code CODE into comparisons we can do using branch
9978    instructions.  BYPASS_CODE is comparison code for branch that will
9979    branch around FIRST_CODE and SECOND_CODE.  If some of branches
9980    is not required, set value to UNKNOWN.
9981    We never require more than two branches.  */
9982
9983 void
9984 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
9985                           enum rtx_code *first_code,
9986                           enum rtx_code *second_code)
9987 {
9988   *first_code = code;
9989   *bypass_code = UNKNOWN;
9990   *second_code = UNKNOWN;
9991
9992   /* The fcomi comparison sets flags as follows:
9993
9994      cmp    ZF PF CF
9995      >      0  0  0
9996      <      0  0  1
9997      =      1  0  0
9998      un     1  1  1 */
9999
10000   switch (code)
10001     {
10002     case GT:                    /* GTU - CF=0 & ZF=0 */
10003     case GE:                    /* GEU - CF=0 */
10004     case ORDERED:               /* PF=0 */
10005     case UNORDERED:             /* PF=1 */
10006     case UNEQ:                  /* EQ - ZF=1 */
10007     case UNLT:                  /* LTU - CF=1 */
10008     case UNLE:                  /* LEU - CF=1 | ZF=1 */
10009     case LTGT:                  /* EQ - ZF=0 */
10010       break;
10011     case LT:                    /* LTU - CF=1 - fails on unordered */
10012       *first_code = UNLT;
10013       *bypass_code = UNORDERED;
10014       break;
10015     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
10016       *first_code = UNLE;
10017       *bypass_code = UNORDERED;
10018       break;
10019     case EQ:                    /* EQ - ZF=1 - fails on unordered */
10020       *first_code = UNEQ;
10021       *bypass_code = UNORDERED;
10022       break;
10023     case NE:                    /* NE - ZF=0 - fails on unordered */
10024       *first_code = LTGT;
10025       *second_code = UNORDERED;
10026       break;
10027     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10028       *first_code = GE;
10029       *second_code = UNORDERED;
10030       break;
10031     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10032       *first_code = GT;
10033       *second_code = UNORDERED;
10034       break;
10035     default:
10036       gcc_unreachable ();
10037     }
10038   if (!TARGET_IEEE_FP)
10039     {
10040       *second_code = UNKNOWN;
10041       *bypass_code = UNKNOWN;
10042     }
10043 }
10044
10045 /* Return cost of comparison done fcom + arithmetics operations on AX.
10046    All following functions do use number of instructions as a cost metrics.
10047    In future this should be tweaked to compute bytes for optimize_size and
10048    take into account performance of various instructions on various CPUs.  */
10049 static int
10050 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10051 {
10052   if (!TARGET_IEEE_FP)
10053     return 4;
10054   /* The cost of code output by ix86_expand_fp_compare.  */
10055   switch (code)
10056     {
10057     case UNLE:
10058     case UNLT:
10059     case LTGT:
10060     case GT:
10061     case GE:
10062     case UNORDERED:
10063     case ORDERED:
10064     case UNEQ:
10065       return 4;
10066       break;
10067     case LT:
10068     case NE:
10069     case EQ:
10070     case UNGE:
10071       return 5;
10072       break;
10073     case LE:
10074     case UNGT:
10075       return 6;
10076       break;
10077     default:
10078       gcc_unreachable ();
10079     }
10080 }
10081
10082 /* Return cost of comparison done using fcomi operation.
10083    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10084 static int
10085 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10086 {
10087   enum rtx_code bypass_code, first_code, second_code;
10088   /* Return arbitrarily high cost when instruction is not supported - this
10089      prevents gcc from using it.  */
10090   if (!TARGET_CMOVE)
10091     return 1024;
10092   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10093   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10094 }
10095
10096 /* Return cost of comparison done using sahf operation.
10097    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10098 static int
10099 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10100 {
10101   enum rtx_code bypass_code, first_code, second_code;
10102   /* Return arbitrarily high cost when instruction is not preferred - this
10103      avoids gcc from using it.  */
10104   if (!TARGET_USE_SAHF && !optimize_size)
10105     return 1024;
10106   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10107   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10108 }
10109
10110 /* Compute cost of the comparison done using any method.
10111    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10112 static int
10113 ix86_fp_comparison_cost (enum rtx_code code)
10114 {
10115   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10116   int min;
10117
10118   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10119   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10120
10121   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10122   if (min > sahf_cost)
10123     min = sahf_cost;
10124   if (min > fcomi_cost)
10125     min = fcomi_cost;
10126   return min;
10127 }
10128
10129 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
10130
10131 static rtx
10132 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
10133                         rtx *second_test, rtx *bypass_test)
10134 {
10135   enum machine_mode fpcmp_mode, intcmp_mode;
10136   rtx tmp, tmp2;
10137   int cost = ix86_fp_comparison_cost (code);
10138   enum rtx_code bypass_code, first_code, second_code;
10139
10140   fpcmp_mode = ix86_fp_compare_mode (code);
10141   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
10142
10143   if (second_test)
10144     *second_test = NULL_RTX;
10145   if (bypass_test)
10146     *bypass_test = NULL_RTX;
10147
10148   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10149
10150   /* Do fcomi/sahf based test when profitable.  */
10151   if ((bypass_code == UNKNOWN || bypass_test)
10152       && (second_code == UNKNOWN || second_test)
10153       && ix86_fp_comparison_arithmetics_cost (code) > cost)
10154     {
10155       if (TARGET_CMOVE)
10156         {
10157           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10158           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
10159                              tmp);
10160           emit_insn (tmp);
10161         }
10162       else
10163         {
10164           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10165           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10166           if (!scratch)
10167             scratch = gen_reg_rtx (HImode);
10168           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10169           emit_insn (gen_x86_sahf_1 (scratch));
10170         }
10171
10172       /* The FP codes work out to act like unsigned.  */
10173       intcmp_mode = fpcmp_mode;
10174       code = first_code;
10175       if (bypass_code != UNKNOWN)
10176         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
10177                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10178                                        const0_rtx);
10179       if (second_code != UNKNOWN)
10180         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
10181                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10182                                        const0_rtx);
10183     }
10184   else
10185     {
10186       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
10187       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10188       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10189       if (!scratch)
10190         scratch = gen_reg_rtx (HImode);
10191       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10192
10193       /* In the unordered case, we have to check C2 for NaN's, which
10194          doesn't happen to work out to anything nice combination-wise.
10195          So do some bit twiddling on the value we've got in AH to come
10196          up with an appropriate set of condition codes.  */
10197
10198       intcmp_mode = CCNOmode;
10199       switch (code)
10200         {
10201         case GT:
10202         case UNGT:
10203           if (code == GT || !TARGET_IEEE_FP)
10204             {
10205               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10206               code = EQ;
10207             }
10208           else
10209             {
10210               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10211               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10212               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
10213               intcmp_mode = CCmode;
10214               code = GEU;
10215             }
10216           break;
10217         case LT:
10218         case UNLT:
10219           if (code == LT && TARGET_IEEE_FP)
10220             {
10221               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10222               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
10223               intcmp_mode = CCmode;
10224               code = EQ;
10225             }
10226           else
10227             {
10228               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
10229               code = NE;
10230             }
10231           break;
10232         case GE:
10233         case UNGE:
10234           if (code == GE || !TARGET_IEEE_FP)
10235             {
10236               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
10237               code = EQ;
10238             }
10239           else
10240             {
10241               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10242               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10243                                              GEN_INT (0x01)));
10244               code = NE;
10245             }
10246           break;
10247         case LE:
10248         case UNLE:
10249           if (code == LE && TARGET_IEEE_FP)
10250             {
10251               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10252               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10253               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10254               intcmp_mode = CCmode;
10255               code = LTU;
10256             }
10257           else
10258             {
10259               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10260               code = NE;
10261             }
10262           break;
10263         case EQ:
10264         case UNEQ:
10265           if (code == EQ && TARGET_IEEE_FP)
10266             {
10267               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10268               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10269               intcmp_mode = CCmode;
10270               code = EQ;
10271             }
10272           else
10273             {
10274               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10275               code = NE;
10276               break;
10277             }
10278           break;
10279         case NE:
10280         case LTGT:
10281           if (code == NE && TARGET_IEEE_FP)
10282             {
10283               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10284               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10285                                              GEN_INT (0x40)));
10286               code = NE;
10287             }
10288           else
10289             {
10290               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10291               code = EQ;
10292             }
10293           break;
10294
10295         case UNORDERED:
10296           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10297           code = NE;
10298           break;
10299         case ORDERED:
10300           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10301           code = EQ;
10302           break;
10303
10304         default:
10305           gcc_unreachable ();
10306         }
10307     }
10308
10309   /* Return the test that should be put into the flags user, i.e.
10310      the bcc, scc, or cmov instruction.  */
10311   return gen_rtx_fmt_ee (code, VOIDmode,
10312                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
10313                          const0_rtx);
10314 }
10315
10316 rtx
10317 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
10318 {
10319   rtx op0, op1, ret;
10320   op0 = ix86_compare_op0;
10321   op1 = ix86_compare_op1;
10322
10323   if (second_test)
10324     *second_test = NULL_RTX;
10325   if (bypass_test)
10326     *bypass_test = NULL_RTX;
10327
10328   if (ix86_compare_emitted)
10329     {
10330       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
10331       ix86_compare_emitted = NULL_RTX;
10332     }
10333   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10334     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10335                                   second_test, bypass_test);
10336   else
10337     ret = ix86_expand_int_compare (code, op0, op1);
10338
10339   return ret;
10340 }
10341
10342 /* Return true if the CODE will result in nontrivial jump sequence.  */
10343 bool
10344 ix86_fp_jump_nontrivial_p (enum rtx_code code)
10345 {
10346   enum rtx_code bypass_code, first_code, second_code;
10347   if (!TARGET_CMOVE)
10348     return true;
10349   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10350   return bypass_code != UNKNOWN || second_code != UNKNOWN;
10351 }
10352
10353 void
10354 ix86_expand_branch (enum rtx_code code, rtx label)
10355 {
10356   rtx tmp;
10357
10358   /* If we have emitted a compare insn, go straight to simple.
10359      ix86_expand_compare won't emit anything if ix86_compare_emitted
10360      is non NULL.  */
10361   if (ix86_compare_emitted)
10362     goto simple;
10363
10364   switch (GET_MODE (ix86_compare_op0))
10365     {
10366     case QImode:
10367     case HImode:
10368     case SImode:
10369       simple:
10370       tmp = ix86_expand_compare (code, NULL, NULL);
10371       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10372                                   gen_rtx_LABEL_REF (VOIDmode, label),
10373                                   pc_rtx);
10374       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10375       return;
10376
10377     case SFmode:
10378     case DFmode:
10379     case XFmode:
10380       {
10381         rtvec vec;
10382         int use_fcomi;
10383         enum rtx_code bypass_code, first_code, second_code;
10384
10385         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
10386                                              &ix86_compare_op1);
10387
10388         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10389
10390         /* Check whether we will use the natural sequence with one jump.  If
10391            so, we can expand jump early.  Otherwise delay expansion by
10392            creating compound insn to not confuse optimizers.  */
10393         if (bypass_code == UNKNOWN && second_code == UNKNOWN
10394             && TARGET_CMOVE)
10395           {
10396             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
10397                                   gen_rtx_LABEL_REF (VOIDmode, label),
10398                                   pc_rtx, NULL_RTX, NULL_RTX);
10399           }
10400         else
10401           {
10402             tmp = gen_rtx_fmt_ee (code, VOIDmode,
10403                                   ix86_compare_op0, ix86_compare_op1);
10404             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10405                                         gen_rtx_LABEL_REF (VOIDmode, label),
10406                                         pc_rtx);
10407             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
10408
10409             use_fcomi = ix86_use_fcomi_compare (code);
10410             vec = rtvec_alloc (3 + !use_fcomi);
10411             RTVEC_ELT (vec, 0) = tmp;
10412             RTVEC_ELT (vec, 1)
10413               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
10414             RTVEC_ELT (vec, 2)
10415               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
10416             if (! use_fcomi)
10417               RTVEC_ELT (vec, 3)
10418                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
10419
10420             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
10421           }
10422         return;
10423       }
10424
10425     case DImode:
10426       if (TARGET_64BIT)
10427         goto simple;
10428     case TImode:
10429       /* Expand DImode branch into multiple compare+branch.  */
10430       {
10431         rtx lo[2], hi[2], label2;
10432         enum rtx_code code1, code2, code3;
10433         enum machine_mode submode;
10434
10435         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
10436           {
10437             tmp = ix86_compare_op0;
10438             ix86_compare_op0 = ix86_compare_op1;
10439             ix86_compare_op1 = tmp;
10440             code = swap_condition (code);
10441           }
10442         if (GET_MODE (ix86_compare_op0) == DImode)
10443           {
10444             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
10445             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
10446             submode = SImode;
10447           }
10448         else
10449           {
10450             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
10451             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
10452             submode = DImode;
10453           }
10454
10455         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
10456            avoid two branches.  This costs one extra insn, so disable when
10457            optimizing for size.  */
10458
10459         if ((code == EQ || code == NE)
10460             && (!optimize_size
10461                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
10462           {
10463             rtx xor0, xor1;
10464
10465             xor1 = hi[0];
10466             if (hi[1] != const0_rtx)
10467               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
10468                                    NULL_RTX, 0, OPTAB_WIDEN);
10469
10470             xor0 = lo[0];
10471             if (lo[1] != const0_rtx)
10472               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
10473                                    NULL_RTX, 0, OPTAB_WIDEN);
10474
10475             tmp = expand_binop (submode, ior_optab, xor1, xor0,
10476                                 NULL_RTX, 0, OPTAB_WIDEN);
10477
10478             ix86_compare_op0 = tmp;
10479             ix86_compare_op1 = const0_rtx;
10480             ix86_expand_branch (code, label);
10481             return;
10482           }
10483
10484         /* Otherwise, if we are doing less-than or greater-or-equal-than,
10485            op1 is a constant and the low word is zero, then we can just
10486            examine the high word.  */
10487
10488         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
10489           switch (code)
10490             {
10491             case LT: case LTU: case GE: case GEU:
10492               ix86_compare_op0 = hi[0];
10493               ix86_compare_op1 = hi[1];
10494               ix86_expand_branch (code, label);
10495               return;
10496             default:
10497               break;
10498             }
10499
10500         /* Otherwise, we need two or three jumps.  */
10501
10502         label2 = gen_label_rtx ();
10503
10504         code1 = code;
10505         code2 = swap_condition (code);
10506         code3 = unsigned_condition (code);
10507
10508         switch (code)
10509           {
10510           case LT: case GT: case LTU: case GTU:
10511             break;
10512
10513           case LE:   code1 = LT;  code2 = GT;  break;
10514           case GE:   code1 = GT;  code2 = LT;  break;
10515           case LEU:  code1 = LTU; code2 = GTU; break;
10516           case GEU:  code1 = GTU; code2 = LTU; break;
10517
10518           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
10519           case NE:   code2 = UNKNOWN; break;
10520
10521           default:
10522             gcc_unreachable ();
10523           }
10524
10525         /*
10526          * a < b =>
10527          *    if (hi(a) < hi(b)) goto true;
10528          *    if (hi(a) > hi(b)) goto false;
10529          *    if (lo(a) < lo(b)) goto true;
10530          *  false:
10531          */
10532
10533         ix86_compare_op0 = hi[0];
10534         ix86_compare_op1 = hi[1];
10535
10536         if (code1 != UNKNOWN)
10537           ix86_expand_branch (code1, label);
10538         if (code2 != UNKNOWN)
10539           ix86_expand_branch (code2, label2);
10540
10541         ix86_compare_op0 = lo[0];
10542         ix86_compare_op1 = lo[1];
10543         ix86_expand_branch (code3, label);
10544
10545         if (code2 != UNKNOWN)
10546           emit_label (label2);
10547         return;
10548       }
10549
10550     default:
10551       gcc_unreachable ();
10552     }
10553 }
10554
10555 /* Split branch based on floating point condition.  */
10556 void
10557 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
10558                       rtx target1, rtx target2, rtx tmp, rtx pushed)
10559 {
10560   rtx second, bypass;
10561   rtx label = NULL_RTX;
10562   rtx condition;
10563   int bypass_probability = -1, second_probability = -1, probability = -1;
10564   rtx i;
10565
10566   if (target2 != pc_rtx)
10567     {
10568       rtx tmp = target2;
10569       code = reverse_condition_maybe_unordered (code);
10570       target2 = target1;
10571       target1 = tmp;
10572     }
10573
10574   condition = ix86_expand_fp_compare (code, op1, op2,
10575                                       tmp, &second, &bypass);
10576
10577   /* Remove pushed operand from stack.  */
10578   if (pushed)
10579     ix86_free_from_memory (GET_MODE (pushed));
10580
10581   if (split_branch_probability >= 0)
10582     {
10583       /* Distribute the probabilities across the jumps.
10584          Assume the BYPASS and SECOND to be always test
10585          for UNORDERED.  */
10586       probability = split_branch_probability;
10587
10588       /* Value of 1 is low enough to make no need for probability
10589          to be updated.  Later we may run some experiments and see
10590          if unordered values are more frequent in practice.  */
10591       if (bypass)
10592         bypass_probability = 1;
10593       if (second)
10594         second_probability = 1;
10595     }
10596   if (bypass != NULL_RTX)
10597     {
10598       label = gen_label_rtx ();
10599       i = emit_jump_insn (gen_rtx_SET
10600                           (VOIDmode, pc_rtx,
10601                            gen_rtx_IF_THEN_ELSE (VOIDmode,
10602                                                  bypass,
10603                                                  gen_rtx_LABEL_REF (VOIDmode,
10604                                                                     label),
10605                                                  pc_rtx)));
10606       if (bypass_probability >= 0)
10607         REG_NOTES (i)
10608           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10609                                GEN_INT (bypass_probability),
10610                                REG_NOTES (i));
10611     }
10612   i = emit_jump_insn (gen_rtx_SET
10613                       (VOIDmode, pc_rtx,
10614                        gen_rtx_IF_THEN_ELSE (VOIDmode,
10615                                              condition, target1, target2)));
10616   if (probability >= 0)
10617     REG_NOTES (i)
10618       = gen_rtx_EXPR_LIST (REG_BR_PROB,
10619                            GEN_INT (probability),
10620                            REG_NOTES (i));
10621   if (second != NULL_RTX)
10622     {
10623       i = emit_jump_insn (gen_rtx_SET
10624                           (VOIDmode, pc_rtx,
10625                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
10626                                                  target2)));
10627       if (second_probability >= 0)
10628         REG_NOTES (i)
10629           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10630                                GEN_INT (second_probability),
10631                                REG_NOTES (i));
10632     }
10633   if (label != NULL_RTX)
10634     emit_label (label);
10635 }
10636
10637 int
10638 ix86_expand_setcc (enum rtx_code code, rtx dest)
10639 {
10640   rtx ret, tmp, tmpreg, equiv;
10641   rtx second_test, bypass_test;
10642
10643   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
10644     return 0; /* FAIL */
10645
10646   gcc_assert (GET_MODE (dest) == QImode);
10647
10648   ret = ix86_expand_compare (code, &second_test, &bypass_test);
10649   PUT_MODE (ret, QImode);
10650
10651   tmp = dest;
10652   tmpreg = dest;
10653
10654   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
10655   if (bypass_test || second_test)
10656     {
10657       rtx test = second_test;
10658       int bypass = 0;
10659       rtx tmp2 = gen_reg_rtx (QImode);
10660       if (bypass_test)
10661         {
10662           gcc_assert (!second_test);
10663           test = bypass_test;
10664           bypass = 1;
10665           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
10666         }
10667       PUT_MODE (test, QImode);
10668       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
10669
10670       if (bypass)
10671         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
10672       else
10673         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
10674     }
10675
10676   /* Attach a REG_EQUAL note describing the comparison result.  */
10677   if (ix86_compare_op0 && ix86_compare_op1)
10678     {
10679       equiv = simplify_gen_relational (code, QImode,
10680                                        GET_MODE (ix86_compare_op0),
10681                                        ix86_compare_op0, ix86_compare_op1);
10682       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
10683     }
10684
10685   return 1; /* DONE */
10686 }
10687
10688 /* Expand comparison setting or clearing carry flag.  Return true when
10689    successful and set pop for the operation.  */
10690 static bool
10691 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
10692 {
10693   enum machine_mode mode =
10694     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
10695
10696   /* Do not handle DImode compares that go through special path.  Also we can't
10697      deal with FP compares yet.  This is possible to add.  */
10698   if (mode == (TARGET_64BIT ? TImode : DImode))
10699     return false;
10700   if (FLOAT_MODE_P (mode))
10701     {
10702       rtx second_test = NULL, bypass_test = NULL;
10703       rtx compare_op, compare_seq;
10704
10705       /* Shortcut:  following common codes never translate into carry flag compares.  */
10706       if (code == EQ || code == NE || code == UNEQ || code == LTGT
10707           || code == ORDERED || code == UNORDERED)
10708         return false;
10709
10710       /* These comparisons require zero flag; swap operands so they won't.  */
10711       if ((code == GT || code == UNLE || code == LE || code == UNGT)
10712           && !TARGET_IEEE_FP)
10713         {
10714           rtx tmp = op0;
10715           op0 = op1;
10716           op1 = tmp;
10717           code = swap_condition (code);
10718         }
10719
10720       /* Try to expand the comparison and verify that we end up with carry flag
10721          based comparison.  This is fails to be true only when we decide to expand
10722          comparison using arithmetic that is not too common scenario.  */
10723       start_sequence ();
10724       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10725                                            &second_test, &bypass_test);
10726       compare_seq = get_insns ();
10727       end_sequence ();
10728
10729       if (second_test || bypass_test)
10730         return false;
10731       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10732           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10733         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
10734       else
10735         code = GET_CODE (compare_op);
10736       if (code != LTU && code != GEU)
10737         return false;
10738       emit_insn (compare_seq);
10739       *pop = compare_op;
10740       return true;
10741     }
10742   if (!INTEGRAL_MODE_P (mode))
10743     return false;
10744   switch (code)
10745     {
10746     case LTU:
10747     case GEU:
10748       break;
10749
10750     /* Convert a==0 into (unsigned)a<1.  */
10751     case EQ:
10752     case NE:
10753       if (op1 != const0_rtx)
10754         return false;
10755       op1 = const1_rtx;
10756       code = (code == EQ ? LTU : GEU);
10757       break;
10758
10759     /* Convert a>b into b<a or a>=b-1.  */
10760     case GTU:
10761     case LEU:
10762       if (GET_CODE (op1) == CONST_INT)
10763         {
10764           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
10765           /* Bail out on overflow.  We still can swap operands but that
10766              would force loading of the constant into register.  */
10767           if (op1 == const0_rtx
10768               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
10769             return false;
10770           code = (code == GTU ? GEU : LTU);
10771         }
10772       else
10773         {
10774           rtx tmp = op1;
10775           op1 = op0;
10776           op0 = tmp;
10777           code = (code == GTU ? LTU : GEU);
10778         }
10779       break;
10780
10781     /* Convert a>=0 into (unsigned)a<0x80000000.  */
10782     case LT:
10783     case GE:
10784       if (mode == DImode || op1 != const0_rtx)
10785         return false;
10786       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10787       code = (code == LT ? GEU : LTU);
10788       break;
10789     case LE:
10790     case GT:
10791       if (mode == DImode || op1 != constm1_rtx)
10792         return false;
10793       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10794       code = (code == LE ? GEU : LTU);
10795       break;
10796
10797     default:
10798       return false;
10799     }
10800   /* Swapping operands may cause constant to appear as first operand.  */
10801   if (!nonimmediate_operand (op0, VOIDmode))
10802     {
10803       if (no_new_pseudos)
10804         return false;
10805       op0 = force_reg (mode, op0);
10806     }
10807   ix86_compare_op0 = op0;
10808   ix86_compare_op1 = op1;
10809   *pop = ix86_expand_compare (code, NULL, NULL);
10810   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
10811   return true;
10812 }
10813
10814 int
10815 ix86_expand_int_movcc (rtx operands[])
10816 {
10817   enum rtx_code code = GET_CODE (operands[1]), compare_code;
10818   rtx compare_seq, compare_op;
10819   rtx second_test, bypass_test;
10820   enum machine_mode mode = GET_MODE (operands[0]);
10821   bool sign_bit_compare_p = false;;
10822
10823   start_sequence ();
10824   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10825   compare_seq = get_insns ();
10826   end_sequence ();
10827
10828   compare_code = GET_CODE (compare_op);
10829
10830   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
10831       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
10832     sign_bit_compare_p = true;
10833
10834   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
10835      HImode insns, we'd be swallowed in word prefix ops.  */
10836
10837   if ((mode != HImode || TARGET_FAST_PREFIX)
10838       && (mode != (TARGET_64BIT ? TImode : DImode))
10839       && GET_CODE (operands[2]) == CONST_INT
10840       && GET_CODE (operands[3]) == CONST_INT)
10841     {
10842       rtx out = operands[0];
10843       HOST_WIDE_INT ct = INTVAL (operands[2]);
10844       HOST_WIDE_INT cf = INTVAL (operands[3]);
10845       HOST_WIDE_INT diff;
10846
10847       diff = ct - cf;
10848       /*  Sign bit compares are better done using shifts than we do by using
10849           sbb.  */
10850       if (sign_bit_compare_p
10851           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10852                                              ix86_compare_op1, &compare_op))
10853         {
10854           /* Detect overlap between destination and compare sources.  */
10855           rtx tmp = out;
10856
10857           if (!sign_bit_compare_p)
10858             {
10859               bool fpcmp = false;
10860
10861               compare_code = GET_CODE (compare_op);
10862
10863               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10864                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10865                 {
10866                   fpcmp = true;
10867                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
10868                 }
10869
10870               /* To simplify rest of code, restrict to the GEU case.  */
10871               if (compare_code == LTU)
10872                 {
10873                   HOST_WIDE_INT tmp = ct;
10874                   ct = cf;
10875                   cf = tmp;
10876                   compare_code = reverse_condition (compare_code);
10877                   code = reverse_condition (code);
10878                 }
10879               else
10880                 {
10881                   if (fpcmp)
10882                     PUT_CODE (compare_op,
10883                               reverse_condition_maybe_unordered
10884                                 (GET_CODE (compare_op)));
10885                   else
10886                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10887                 }
10888               diff = ct - cf;
10889
10890               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
10891                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
10892                 tmp = gen_reg_rtx (mode);
10893
10894               if (mode == DImode)
10895                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
10896               else
10897                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
10898             }
10899           else
10900             {
10901               if (code == GT || code == GE)
10902                 code = reverse_condition (code);
10903               else
10904                 {
10905                   HOST_WIDE_INT tmp = ct;
10906                   ct = cf;
10907                   cf = tmp;
10908                   diff = ct - cf;
10909                 }
10910               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
10911                                      ix86_compare_op1, VOIDmode, 0, -1);
10912             }
10913
10914           if (diff == 1)
10915             {
10916               /*
10917                * cmpl op0,op1
10918                * sbbl dest,dest
10919                * [addl dest, ct]
10920                *
10921                * Size 5 - 8.
10922                */
10923               if (ct)
10924                 tmp = expand_simple_binop (mode, PLUS,
10925                                            tmp, GEN_INT (ct),
10926                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10927             }
10928           else if (cf == -1)
10929             {
10930               /*
10931                * cmpl op0,op1
10932                * sbbl dest,dest
10933                * orl $ct, dest
10934                *
10935                * Size 8.
10936                */
10937               tmp = expand_simple_binop (mode, IOR,
10938                                          tmp, GEN_INT (ct),
10939                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10940             }
10941           else if (diff == -1 && ct)
10942             {
10943               /*
10944                * cmpl op0,op1
10945                * sbbl dest,dest
10946                * notl dest
10947                * [addl dest, cf]
10948                *
10949                * Size 8 - 11.
10950                */
10951               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10952               if (cf)
10953                 tmp = expand_simple_binop (mode, PLUS,
10954                                            copy_rtx (tmp), GEN_INT (cf),
10955                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10956             }
10957           else
10958             {
10959               /*
10960                * cmpl op0,op1
10961                * sbbl dest,dest
10962                * [notl dest]
10963                * andl cf - ct, dest
10964                * [addl dest, ct]
10965                *
10966                * Size 8 - 11.
10967                */
10968
10969               if (cf == 0)
10970                 {
10971                   cf = ct;
10972                   ct = 0;
10973                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10974                 }
10975
10976               tmp = expand_simple_binop (mode, AND,
10977                                          copy_rtx (tmp),
10978                                          gen_int_mode (cf - ct, mode),
10979                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10980               if (ct)
10981                 tmp = expand_simple_binop (mode, PLUS,
10982                                            copy_rtx (tmp), GEN_INT (ct),
10983                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10984             }
10985
10986           if (!rtx_equal_p (tmp, out))
10987             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
10988
10989           return 1; /* DONE */
10990         }
10991
10992       if (diff < 0)
10993         {
10994           HOST_WIDE_INT tmp;
10995           tmp = ct, ct = cf, cf = tmp;
10996           diff = -diff;
10997           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10998             {
10999               /* We may be reversing unordered compare to normal compare, that
11000                  is not valid in general (we may convert non-trapping condition
11001                  to trapping one), however on i386 we currently emit all
11002                  comparisons unordered.  */
11003               compare_code = reverse_condition_maybe_unordered (compare_code);
11004               code = reverse_condition_maybe_unordered (code);
11005             }
11006           else
11007             {
11008               compare_code = reverse_condition (compare_code);
11009               code = reverse_condition (code);
11010             }
11011         }
11012
11013       compare_code = UNKNOWN;
11014       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
11015           && GET_CODE (ix86_compare_op1) == CONST_INT)
11016         {
11017           if (ix86_compare_op1 == const0_rtx
11018               && (code == LT || code == GE))
11019             compare_code = code;
11020           else if (ix86_compare_op1 == constm1_rtx)
11021             {
11022               if (code == LE)
11023                 compare_code = LT;
11024               else if (code == GT)
11025                 compare_code = GE;
11026             }
11027         }
11028
11029       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11030       if (compare_code != UNKNOWN
11031           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11032           && (cf == -1 || ct == -1))
11033         {
11034           /* If lea code below could be used, only optimize
11035              if it results in a 2 insn sequence.  */
11036
11037           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11038                  || diff == 3 || diff == 5 || diff == 9)
11039               || (compare_code == LT && ct == -1)
11040               || (compare_code == GE && cf == -1))
11041             {
11042               /*
11043                * notl op1       (if necessary)
11044                * sarl $31, op1
11045                * orl cf, op1
11046                */
11047               if (ct != -1)
11048                 {
11049                   cf = ct;
11050                   ct = -1;
11051                   code = reverse_condition (code);
11052                 }
11053
11054               out = emit_store_flag (out, code, ix86_compare_op0,
11055                                      ix86_compare_op1, VOIDmode, 0, -1);
11056
11057               out = expand_simple_binop (mode, IOR,
11058                                          out, GEN_INT (cf),
11059                                          out, 1, OPTAB_DIRECT);
11060               if (out != operands[0])
11061                 emit_move_insn (operands[0], out);
11062
11063               return 1; /* DONE */
11064             }
11065         }
11066
11067
11068       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
11069            || diff == 3 || diff == 5 || diff == 9)
11070           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
11071           && (mode != DImode
11072               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
11073         {
11074           /*
11075            * xorl dest,dest
11076            * cmpl op1,op2
11077            * setcc dest
11078            * lea cf(dest*(ct-cf)),dest
11079            *
11080            * Size 14.
11081            *
11082            * This also catches the degenerate setcc-only case.
11083            */
11084
11085           rtx tmp;
11086           int nops;
11087
11088           out = emit_store_flag (out, code, ix86_compare_op0,
11089                                  ix86_compare_op1, VOIDmode, 0, 1);
11090
11091           nops = 0;
11092           /* On x86_64 the lea instruction operates on Pmode, so we need
11093              to get arithmetics done in proper mode to match.  */
11094           if (diff == 1)
11095             tmp = copy_rtx (out);
11096           else
11097             {
11098               rtx out1;
11099               out1 = copy_rtx (out);
11100               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
11101               nops++;
11102               if (diff & 1)
11103                 {
11104                   tmp = gen_rtx_PLUS (mode, tmp, out1);
11105                   nops++;
11106                 }
11107             }
11108           if (cf != 0)
11109             {
11110               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
11111               nops++;
11112             }
11113           if (!rtx_equal_p (tmp, out))
11114             {
11115               if (nops == 1)
11116                 out = force_operand (tmp, copy_rtx (out));
11117               else
11118                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
11119             }
11120           if (!rtx_equal_p (out, operands[0]))
11121             emit_move_insn (operands[0], copy_rtx (out));
11122
11123           return 1; /* DONE */
11124         }
11125
11126       /*
11127        * General case:                  Jumpful:
11128        *   xorl dest,dest               cmpl op1, op2
11129        *   cmpl op1, op2                movl ct, dest
11130        *   setcc dest                   jcc 1f
11131        *   decl dest                    movl cf, dest
11132        *   andl (cf-ct),dest            1:
11133        *   addl ct,dest
11134        *
11135        * Size 20.                       Size 14.
11136        *
11137        * This is reasonably steep, but branch mispredict costs are
11138        * high on modern cpus, so consider failing only if optimizing
11139        * for space.
11140        */
11141
11142       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11143           && BRANCH_COST >= 2)
11144         {
11145           if (cf == 0)
11146             {
11147               cf = ct;
11148               ct = 0;
11149               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11150                 /* We may be reversing unordered compare to normal compare,
11151                    that is not valid in general (we may convert non-trapping
11152                    condition to trapping one), however on i386 we currently
11153                    emit all comparisons unordered.  */
11154                 code = reverse_condition_maybe_unordered (code);
11155               else
11156                 {
11157                   code = reverse_condition (code);
11158                   if (compare_code != UNKNOWN)
11159                     compare_code = reverse_condition (compare_code);
11160                 }
11161             }
11162
11163           if (compare_code != UNKNOWN)
11164             {
11165               /* notl op1       (if needed)
11166                  sarl $31, op1
11167                  andl (cf-ct), op1
11168                  addl ct, op1
11169
11170                  For x < 0 (resp. x <= -1) there will be no notl,
11171                  so if possible swap the constants to get rid of the
11172                  complement.
11173                  True/false will be -1/0 while code below (store flag
11174                  followed by decrement) is 0/-1, so the constants need
11175                  to be exchanged once more.  */
11176
11177               if (compare_code == GE || !cf)
11178                 {
11179                   code = reverse_condition (code);
11180                   compare_code = LT;
11181                 }
11182               else
11183                 {
11184                   HOST_WIDE_INT tmp = cf;
11185                   cf = ct;
11186                   ct = tmp;
11187                 }
11188
11189               out = emit_store_flag (out, code, ix86_compare_op0,
11190                                      ix86_compare_op1, VOIDmode, 0, -1);
11191             }
11192           else
11193             {
11194               out = emit_store_flag (out, code, ix86_compare_op0,
11195                                      ix86_compare_op1, VOIDmode, 0, 1);
11196
11197               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
11198                                          copy_rtx (out), 1, OPTAB_DIRECT);
11199             }
11200
11201           out = expand_simple_binop (mode, AND, copy_rtx (out),
11202                                      gen_int_mode (cf - ct, mode),
11203                                      copy_rtx (out), 1, OPTAB_DIRECT);
11204           if (ct)
11205             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
11206                                        copy_rtx (out), 1, OPTAB_DIRECT);
11207           if (!rtx_equal_p (out, operands[0]))
11208             emit_move_insn (operands[0], copy_rtx (out));
11209
11210           return 1; /* DONE */
11211         }
11212     }
11213
11214   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11215     {
11216       /* Try a few things more with specific constants and a variable.  */
11217
11218       optab op;
11219       rtx var, orig_out, out, tmp;
11220
11221       if (BRANCH_COST <= 2)
11222         return 0; /* FAIL */
11223
11224       /* If one of the two operands is an interesting constant, load a
11225          constant with the above and mask it in with a logical operation.  */
11226
11227       if (GET_CODE (operands[2]) == CONST_INT)
11228         {
11229           var = operands[3];
11230           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
11231             operands[3] = constm1_rtx, op = and_optab;
11232           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
11233             operands[3] = const0_rtx, op = ior_optab;
11234           else
11235             return 0; /* FAIL */
11236         }
11237       else if (GET_CODE (operands[3]) == CONST_INT)
11238         {
11239           var = operands[2];
11240           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
11241             operands[2] = constm1_rtx, op = and_optab;
11242           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
11243             operands[2] = const0_rtx, op = ior_optab;
11244           else
11245             return 0; /* FAIL */
11246         }
11247       else
11248         return 0; /* FAIL */
11249
11250       orig_out = operands[0];
11251       tmp = gen_reg_rtx (mode);
11252       operands[0] = tmp;
11253
11254       /* Recurse to get the constant loaded.  */
11255       if (ix86_expand_int_movcc (operands) == 0)
11256         return 0; /* FAIL */
11257
11258       /* Mask in the interesting variable.  */
11259       out = expand_binop (mode, op, var, tmp, orig_out, 0,
11260                           OPTAB_WIDEN);
11261       if (!rtx_equal_p (out, orig_out))
11262         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
11263
11264       return 1; /* DONE */
11265     }
11266
11267   /*
11268    * For comparison with above,
11269    *
11270    * movl cf,dest
11271    * movl ct,tmp
11272    * cmpl op1,op2
11273    * cmovcc tmp,dest
11274    *
11275    * Size 15.
11276    */
11277
11278   if (! nonimmediate_operand (operands[2], mode))
11279     operands[2] = force_reg (mode, operands[2]);
11280   if (! nonimmediate_operand (operands[3], mode))
11281     operands[3] = force_reg (mode, operands[3]);
11282
11283   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11284     {
11285       rtx tmp = gen_reg_rtx (mode);
11286       emit_move_insn (tmp, operands[3]);
11287       operands[3] = tmp;
11288     }
11289   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11290     {
11291       rtx tmp = gen_reg_rtx (mode);
11292       emit_move_insn (tmp, operands[2]);
11293       operands[2] = tmp;
11294     }
11295
11296   if (! register_operand (operands[2], VOIDmode)
11297       && (mode == QImode
11298           || ! register_operand (operands[3], VOIDmode)))
11299     operands[2] = force_reg (mode, operands[2]);
11300
11301   if (mode == QImode
11302       && ! register_operand (operands[3], VOIDmode))
11303     operands[3] = force_reg (mode, operands[3]);
11304
11305   emit_insn (compare_seq);
11306   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11307                           gen_rtx_IF_THEN_ELSE (mode,
11308                                                 compare_op, operands[2],
11309                                                 operands[3])));
11310   if (bypass_test)
11311     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11312                             gen_rtx_IF_THEN_ELSE (mode,
11313                                   bypass_test,
11314                                   copy_rtx (operands[3]),
11315                                   copy_rtx (operands[0]))));
11316   if (second_test)
11317     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11318                             gen_rtx_IF_THEN_ELSE (mode,
11319                                   second_test,
11320                                   copy_rtx (operands[2]),
11321                                   copy_rtx (operands[0]))));
11322
11323   return 1; /* DONE */
11324 }
11325
11326 /* Swap, force into registers, or otherwise massage the two operands
11327    to an sse comparison with a mask result.  Thus we differ a bit from
11328    ix86_prepare_fp_compare_args which expects to produce a flags result.
11329
11330    The DEST operand exists to help determine whether to commute commutative
11331    operators.  The POP0/POP1 operands are updated in place.  The new
11332    comparison code is returned, or UNKNOWN if not implementable.  */
11333
11334 static enum rtx_code
11335 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
11336                                   rtx *pop0, rtx *pop1)
11337 {
11338   rtx tmp;
11339
11340   switch (code)
11341     {
11342     case LTGT:
11343     case UNEQ:
11344       /* We have no LTGT as an operator.  We could implement it with
11345          NE & ORDERED, but this requires an extra temporary.  It's
11346          not clear that it's worth it.  */
11347       return UNKNOWN;
11348
11349     case LT:
11350     case LE:
11351     case UNGT:
11352     case UNGE:
11353       /* These are supported directly.  */
11354       break;
11355
11356     case EQ:
11357     case NE:
11358     case UNORDERED:
11359     case ORDERED:
11360       /* For commutative operators, try to canonicalize the destination
11361          operand to be first in the comparison - this helps reload to
11362          avoid extra moves.  */
11363       if (!dest || !rtx_equal_p (dest, *pop1))
11364         break;
11365       /* FALLTHRU */
11366
11367     case GE:
11368     case GT:
11369     case UNLE:
11370     case UNLT:
11371       /* These are not supported directly.  Swap the comparison operands
11372          to transform into something that is supported.  */
11373       tmp = *pop0;
11374       *pop0 = *pop1;
11375       *pop1 = tmp;
11376       code = swap_condition (code);
11377       break;
11378
11379     default:
11380       gcc_unreachable ();
11381     }
11382
11383   return code;
11384 }
11385
11386 /* Detect conditional moves that exactly match min/max operational
11387    semantics.  Note that this is IEEE safe, as long as we don't
11388    interchange the operands.
11389
11390    Returns FALSE if this conditional move doesn't match a MIN/MAX,
11391    and TRUE if the operation is successful and instructions are emitted.  */
11392
11393 static bool
11394 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
11395                            rtx cmp_op1, rtx if_true, rtx if_false)
11396 {
11397   enum machine_mode mode;
11398   bool is_min;
11399   rtx tmp;
11400
11401   if (code == LT)
11402     ;
11403   else if (code == UNGE)
11404     {
11405       tmp = if_true;
11406       if_true = if_false;
11407       if_false = tmp;
11408     }
11409   else
11410     return false;
11411
11412   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
11413     is_min = true;
11414   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
11415     is_min = false;
11416   else
11417     return false;
11418
11419   mode = GET_MODE (dest);
11420
11421   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
11422      but MODE may be a vector mode and thus not appropriate.  */
11423   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
11424     {
11425       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
11426       rtvec v;
11427
11428       if_true = force_reg (mode, if_true);
11429       v = gen_rtvec (2, if_true, if_false);
11430       tmp = gen_rtx_UNSPEC (mode, v, u);
11431     }
11432   else
11433     {
11434       code = is_min ? SMIN : SMAX;
11435       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
11436     }
11437
11438   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
11439   return true;
11440 }
11441
11442 /* Expand an sse vector comparison.  Return the register with the result.  */
11443
11444 static rtx
11445 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
11446                      rtx op_true, rtx op_false)
11447 {
11448   enum machine_mode mode = GET_MODE (dest);
11449   rtx x;
11450
11451   cmp_op0 = force_reg (mode, cmp_op0);
11452   if (!nonimmediate_operand (cmp_op1, mode))
11453     cmp_op1 = force_reg (mode, cmp_op1);
11454
11455   if (optimize
11456       || reg_overlap_mentioned_p (dest, op_true)
11457       || reg_overlap_mentioned_p (dest, op_false))
11458     dest = gen_reg_rtx (mode);
11459
11460   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
11461   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11462
11463   return dest;
11464 }
11465
11466 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
11467    operations.  This is used for both scalar and vector conditional moves.  */
11468
11469 static void
11470 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
11471 {
11472   enum machine_mode mode = GET_MODE (dest);
11473   rtx t2, t3, x;
11474
11475   if (op_false == CONST0_RTX (mode))
11476     {
11477       op_true = force_reg (mode, op_true);
11478       x = gen_rtx_AND (mode, cmp, op_true);
11479       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11480     }
11481   else if (op_true == CONST0_RTX (mode))
11482     {
11483       op_false = force_reg (mode, op_false);
11484       x = gen_rtx_NOT (mode, cmp);
11485       x = gen_rtx_AND (mode, x, op_false);
11486       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11487     }
11488   else
11489     {
11490       op_true = force_reg (mode, op_true);
11491       op_false = force_reg (mode, op_false);
11492
11493       t2 = gen_reg_rtx (mode);
11494       if (optimize)
11495         t3 = gen_reg_rtx (mode);
11496       else
11497         t3 = dest;
11498
11499       x = gen_rtx_AND (mode, op_true, cmp);
11500       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
11501
11502       x = gen_rtx_NOT (mode, cmp);
11503       x = gen_rtx_AND (mode, x, op_false);
11504       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
11505
11506       x = gen_rtx_IOR (mode, t3, t2);
11507       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11508     }
11509 }
11510
11511 /* Expand a floating-point conditional move.  Return true if successful.  */
11512
11513 int
11514 ix86_expand_fp_movcc (rtx operands[])
11515 {
11516   enum machine_mode mode = GET_MODE (operands[0]);
11517   enum rtx_code code = GET_CODE (operands[1]);
11518   rtx tmp, compare_op, second_test, bypass_test;
11519
11520   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
11521     {
11522       enum machine_mode cmode;
11523
11524       /* Since we've no cmove for sse registers, don't force bad register
11525          allocation just to gain access to it.  Deny movcc when the
11526          comparison mode doesn't match the move mode.  */
11527       cmode = GET_MODE (ix86_compare_op0);
11528       if (cmode == VOIDmode)
11529         cmode = GET_MODE (ix86_compare_op1);
11530       if (cmode != mode)
11531         return 0;
11532
11533       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11534                                                &ix86_compare_op0,
11535                                                &ix86_compare_op1);
11536       if (code == UNKNOWN)
11537         return 0;
11538
11539       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
11540                                      ix86_compare_op1, operands[2],
11541                                      operands[3]))
11542         return 1;
11543
11544       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
11545                                  ix86_compare_op1, operands[2], operands[3]);
11546       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
11547       return 1;
11548     }
11549
11550   /* The floating point conditional move instructions don't directly
11551      support conditions resulting from a signed integer comparison.  */
11552
11553   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11554
11555   /* The floating point conditional move instructions don't directly
11556      support signed integer comparisons.  */
11557
11558   if (!fcmov_comparison_operator (compare_op, VOIDmode))
11559     {
11560       gcc_assert (!second_test && !bypass_test);
11561       tmp = gen_reg_rtx (QImode);
11562       ix86_expand_setcc (code, tmp);
11563       code = NE;
11564       ix86_compare_op0 = tmp;
11565       ix86_compare_op1 = const0_rtx;
11566       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
11567     }
11568   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11569     {
11570       tmp = gen_reg_rtx (mode);
11571       emit_move_insn (tmp, operands[3]);
11572       operands[3] = tmp;
11573     }
11574   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11575     {
11576       tmp = gen_reg_rtx (mode);
11577       emit_move_insn (tmp, operands[2]);
11578       operands[2] = tmp;
11579     }
11580
11581   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11582                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
11583                                                 operands[2], operands[3])));
11584   if (bypass_test)
11585     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11586                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
11587                                                   operands[3], operands[0])));
11588   if (second_test)
11589     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11590                             gen_rtx_IF_THEN_ELSE (mode, second_test,
11591                                                   operands[2], operands[0])));
11592
11593   return 1;
11594 }
11595
11596 /* Expand a floating-point vector conditional move; a vcond operation
11597    rather than a movcc operation.  */
11598
11599 bool
11600 ix86_expand_fp_vcond (rtx operands[])
11601 {
11602   enum rtx_code code = GET_CODE (operands[3]);
11603   rtx cmp;
11604
11605   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11606                                            &operands[4], &operands[5]);
11607   if (code == UNKNOWN)
11608     return false;
11609
11610   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
11611                                  operands[5], operands[1], operands[2]))
11612     return true;
11613
11614   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
11615                              operands[1], operands[2]);
11616   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
11617   return true;
11618 }
11619
11620 /* Expand a signed integral vector conditional move.  */
11621
11622 bool
11623 ix86_expand_int_vcond (rtx operands[])
11624 {
11625   enum machine_mode mode = GET_MODE (operands[0]);
11626   enum rtx_code code = GET_CODE (operands[3]);
11627   bool negate = false;
11628   rtx x, cop0, cop1;
11629
11630   cop0 = operands[4];
11631   cop1 = operands[5];
11632
11633   /* Canonicalize the comparison to EQ, GT, GTU.  */
11634   switch (code)
11635     {
11636     case EQ:
11637     case GT:
11638     case GTU:
11639       break;
11640
11641     case NE:
11642     case LE:
11643     case LEU:
11644       code = reverse_condition (code);
11645       negate = true;
11646       break;
11647
11648     case GE:
11649     case GEU:
11650       code = reverse_condition (code);
11651       negate = true;
11652       /* FALLTHRU */
11653
11654     case LT:
11655     case LTU:
11656       code = swap_condition (code);
11657       x = cop0, cop0 = cop1, cop1 = x;
11658       break;
11659
11660     default:
11661       gcc_unreachable ();
11662     }
11663
11664   /* Unsigned parallel compare is not supported by the hardware.  Play some
11665      tricks to turn this into a signed comparison against 0.  */
11666   if (code == GTU)
11667     {
11668       cop0 = force_reg (mode, cop0);
11669
11670       switch (mode)
11671         {
11672         case V4SImode:
11673           {
11674             rtx t1, t2, mask;
11675
11676             /* Perform a parallel modulo subtraction.  */
11677             t1 = gen_reg_rtx (mode);
11678             emit_insn (gen_subv4si3 (t1, cop0, cop1));
11679
11680             /* Extract the original sign bit of op0.  */
11681             mask = GEN_INT (-0x80000000);
11682             mask = gen_rtx_CONST_VECTOR (mode,
11683                         gen_rtvec (4, mask, mask, mask, mask));
11684             mask = force_reg (mode, mask);
11685             t2 = gen_reg_rtx (mode);
11686             emit_insn (gen_andv4si3 (t2, cop0, mask));
11687
11688             /* XOR it back into the result of the subtraction.  This results
11689                in the sign bit set iff we saw unsigned underflow.  */
11690             x = gen_reg_rtx (mode);
11691             emit_insn (gen_xorv4si3 (x, t1, t2));
11692
11693             code = GT;
11694           }
11695           break;
11696
11697         case V16QImode:
11698         case V8HImode:
11699           /* Perform a parallel unsigned saturating subtraction.  */
11700           x = gen_reg_rtx (mode);
11701           emit_insn (gen_rtx_SET (VOIDmode, x,
11702                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
11703
11704           code = EQ;
11705           negate = !negate;
11706           break;
11707
11708         default:
11709           gcc_unreachable ();
11710         }
11711
11712       cop0 = x;
11713       cop1 = CONST0_RTX (mode);
11714     }
11715
11716   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
11717                            operands[1+negate], operands[2-negate]);
11718
11719   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
11720                          operands[2-negate]);
11721   return true;
11722 }
11723
11724 /* Expand conditional increment or decrement using adb/sbb instructions.
11725    The default case using setcc followed by the conditional move can be
11726    done by generic code.  */
11727 int
11728 ix86_expand_int_addcc (rtx operands[])
11729 {
11730   enum rtx_code code = GET_CODE (operands[1]);
11731   rtx compare_op;
11732   rtx val = const0_rtx;
11733   bool fpcmp = false;
11734   enum machine_mode mode = GET_MODE (operands[0]);
11735
11736   if (operands[3] != const1_rtx
11737       && operands[3] != constm1_rtx)
11738     return 0;
11739   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11740                                        ix86_compare_op1, &compare_op))
11741      return 0;
11742   code = GET_CODE (compare_op);
11743
11744   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11745       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11746     {
11747       fpcmp = true;
11748       code = ix86_fp_compare_code_to_integer (code);
11749     }
11750
11751   if (code != LTU)
11752     {
11753       val = constm1_rtx;
11754       if (fpcmp)
11755         PUT_CODE (compare_op,
11756                   reverse_condition_maybe_unordered
11757                     (GET_CODE (compare_op)));
11758       else
11759         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11760     }
11761   PUT_MODE (compare_op, mode);
11762
11763   /* Construct either adc or sbb insn.  */
11764   if ((code == LTU) == (operands[3] == constm1_rtx))
11765     {
11766       switch (GET_MODE (operands[0]))
11767         {
11768           case QImode:
11769             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
11770             break;
11771           case HImode:
11772             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
11773             break;
11774           case SImode:
11775             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
11776             break;
11777           case DImode:
11778             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11779             break;
11780           default:
11781             gcc_unreachable ();
11782         }
11783     }
11784   else
11785     {
11786       switch (GET_MODE (operands[0]))
11787         {
11788           case QImode:
11789             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
11790             break;
11791           case HImode:
11792             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
11793             break;
11794           case SImode:
11795             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
11796             break;
11797           case DImode:
11798             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11799             break;
11800           default:
11801             gcc_unreachable ();
11802         }
11803     }
11804   return 1; /* DONE */
11805 }
11806
11807
11808 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
11809    works for floating pointer parameters and nonoffsetable memories.
11810    For pushes, it returns just stack offsets; the values will be saved
11811    in the right order.  Maximally three parts are generated.  */
11812
11813 static int
11814 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
11815 {
11816   int size;
11817
11818   if (!TARGET_64BIT)
11819     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
11820   else
11821     size = (GET_MODE_SIZE (mode) + 4) / 8;
11822
11823   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
11824   gcc_assert (size >= 2 && size <= 3);
11825
11826   /* Optimize constant pool reference to immediates.  This is used by fp
11827      moves, that force all constants to memory to allow combining.  */
11828   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
11829     {
11830       rtx tmp = maybe_get_pool_constant (operand);
11831       if (tmp)
11832         operand = tmp;
11833     }
11834
11835   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
11836     {
11837       /* The only non-offsetable memories we handle are pushes.  */
11838       int ok = push_operand (operand, VOIDmode);
11839
11840       gcc_assert (ok);
11841
11842       operand = copy_rtx (operand);
11843       PUT_MODE (operand, Pmode);
11844       parts[0] = parts[1] = parts[2] = operand;
11845       return size;
11846     }
11847
11848   if (GET_CODE (operand) == CONST_VECTOR)
11849     {
11850       enum machine_mode imode = int_mode_for_mode (mode);
11851       /* Caution: if we looked through a constant pool memory above,
11852          the operand may actually have a different mode now.  That's
11853          ok, since we want to pun this all the way back to an integer.  */
11854       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
11855       gcc_assert (operand != NULL);
11856       mode = imode;
11857     }
11858
11859   if (!TARGET_64BIT)
11860     {
11861       if (mode == DImode)
11862         split_di (&operand, 1, &parts[0], &parts[1]);
11863       else
11864         {
11865           if (REG_P (operand))
11866             {
11867               gcc_assert (reload_completed);
11868               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
11869               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
11870               if (size == 3)
11871                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
11872             }
11873           else if (offsettable_memref_p (operand))
11874             {
11875               operand = adjust_address (operand, SImode, 0);
11876               parts[0] = operand;
11877               parts[1] = adjust_address (operand, SImode, 4);
11878               if (size == 3)
11879                 parts[2] = adjust_address (operand, SImode, 8);
11880             }
11881           else if (GET_CODE (operand) == CONST_DOUBLE)
11882             {
11883               REAL_VALUE_TYPE r;
11884               long l[4];
11885
11886               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11887               switch (mode)
11888                 {
11889                 case XFmode:
11890                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
11891                   parts[2] = gen_int_mode (l[2], SImode);
11892                   break;
11893                 case DFmode:
11894                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
11895                   break;
11896                 default:
11897                   gcc_unreachable ();
11898                 }
11899               parts[1] = gen_int_mode (l[1], SImode);
11900               parts[0] = gen_int_mode (l[0], SImode);
11901             }
11902           else
11903             gcc_unreachable ();
11904         }
11905     }
11906   else
11907     {
11908       if (mode == TImode)
11909         split_ti (&operand, 1, &parts[0], &parts[1]);
11910       if (mode == XFmode || mode == TFmode)
11911         {
11912           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
11913           if (REG_P (operand))
11914             {
11915               gcc_assert (reload_completed);
11916               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
11917               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
11918             }
11919           else if (offsettable_memref_p (operand))
11920             {
11921               operand = adjust_address (operand, DImode, 0);
11922               parts[0] = operand;
11923               parts[1] = adjust_address (operand, upper_mode, 8);
11924             }
11925           else if (GET_CODE (operand) == CONST_DOUBLE)
11926             {
11927               REAL_VALUE_TYPE r;
11928               long l[4];
11929
11930               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11931               real_to_target (l, &r, mode);
11932
11933               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
11934               if (HOST_BITS_PER_WIDE_INT >= 64)
11935                 parts[0]
11936                   = gen_int_mode
11937                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
11938                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
11939                        DImode);
11940               else
11941                 parts[0] = immed_double_const (l[0], l[1], DImode);
11942
11943               if (upper_mode == SImode)
11944                 parts[1] = gen_int_mode (l[2], SImode);
11945               else if (HOST_BITS_PER_WIDE_INT >= 64)
11946                 parts[1]
11947                   = gen_int_mode
11948                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
11949                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
11950                        DImode);
11951               else
11952                 parts[1] = immed_double_const (l[2], l[3], DImode);
11953             }
11954           else
11955             gcc_unreachable ();
11956         }
11957     }
11958
11959   return size;
11960 }
11961
11962 /* Emit insns to perform a move or push of DI, DF, and XF values.
11963    Return false when normal moves are needed; true when all required
11964    insns have been emitted.  Operands 2-4 contain the input values
11965    int the correct order; operands 5-7 contain the output values.  */
11966
11967 void
11968 ix86_split_long_move (rtx operands[])
11969 {
11970   rtx part[2][3];
11971   int nparts;
11972   int push = 0;
11973   int collisions = 0;
11974   enum machine_mode mode = GET_MODE (operands[0]);
11975
11976   /* The DFmode expanders may ask us to move double.
11977      For 64bit target this is single move.  By hiding the fact
11978      here we simplify i386.md splitters.  */
11979   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
11980     {
11981       /* Optimize constant pool reference to immediates.  This is used by
11982          fp moves, that force all constants to memory to allow combining.  */
11983
11984       if (GET_CODE (operands[1]) == MEM
11985           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11986           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
11987         operands[1] = get_pool_constant (XEXP (operands[1], 0));
11988       if (push_operand (operands[0], VOIDmode))
11989         {
11990           operands[0] = copy_rtx (operands[0]);
11991           PUT_MODE (operands[0], Pmode);
11992         }
11993       else
11994         operands[0] = gen_lowpart (DImode, operands[0]);
11995       operands[1] = gen_lowpart (DImode, operands[1]);
11996       emit_move_insn (operands[0], operands[1]);
11997       return;
11998     }
11999
12000   /* The only non-offsettable memory we handle is push.  */
12001   if (push_operand (operands[0], VOIDmode))
12002     push = 1;
12003   else
12004     gcc_assert (GET_CODE (operands[0]) != MEM
12005                 || offsettable_memref_p (operands[0]));
12006
12007   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
12008   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
12009
12010   /* When emitting push, take care for source operands on the stack.  */
12011   if (push && GET_CODE (operands[1]) == MEM
12012       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
12013     {
12014       if (nparts == 3)
12015         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
12016                                      XEXP (part[1][2], 0));
12017       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
12018                                    XEXP (part[1][1], 0));
12019     }
12020
12021   /* We need to do copy in the right order in case an address register
12022      of the source overlaps the destination.  */
12023   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
12024     {
12025       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
12026         collisions++;
12027       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12028         collisions++;
12029       if (nparts == 3
12030           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
12031         collisions++;
12032
12033       /* Collision in the middle part can be handled by reordering.  */
12034       if (collisions == 1 && nparts == 3
12035           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12036         {
12037           rtx tmp;
12038           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
12039           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
12040         }
12041
12042       /* If there are more collisions, we can't handle it by reordering.
12043          Do an lea to the last part and use only one colliding move.  */
12044       else if (collisions > 1)
12045         {
12046           rtx base;
12047
12048           collisions = 1;
12049
12050           base = part[0][nparts - 1];
12051
12052           /* Handle the case when the last part isn't valid for lea.
12053              Happens in 64-bit mode storing the 12-byte XFmode.  */
12054           if (GET_MODE (base) != Pmode)
12055             base = gen_rtx_REG (Pmode, REGNO (base));
12056
12057           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
12058           part[1][0] = replace_equiv_address (part[1][0], base);
12059           part[1][1] = replace_equiv_address (part[1][1],
12060                                       plus_constant (base, UNITS_PER_WORD));
12061           if (nparts == 3)
12062             part[1][2] = replace_equiv_address (part[1][2],
12063                                       plus_constant (base, 8));
12064         }
12065     }
12066
12067   if (push)
12068     {
12069       if (!TARGET_64BIT)
12070         {
12071           if (nparts == 3)
12072             {
12073               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
12074                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
12075               emit_move_insn (part[0][2], part[1][2]);
12076             }
12077         }
12078       else
12079         {
12080           /* In 64bit mode we don't have 32bit push available.  In case this is
12081              register, it is OK - we will just use larger counterpart.  We also
12082              retype memory - these comes from attempt to avoid REX prefix on
12083              moving of second half of TFmode value.  */
12084           if (GET_MODE (part[1][1]) == SImode)
12085             {
12086               switch (GET_CODE (part[1][1]))
12087                 {
12088                 case MEM:
12089                   part[1][1] = adjust_address (part[1][1], DImode, 0);
12090                   break;
12091
12092                 case REG:
12093                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
12094                   break;
12095
12096                 default:
12097                   gcc_unreachable ();
12098                 }
12099
12100               if (GET_MODE (part[1][0]) == SImode)
12101                 part[1][0] = part[1][1];
12102             }
12103         }
12104       emit_move_insn (part[0][1], part[1][1]);
12105       emit_move_insn (part[0][0], part[1][0]);
12106       return;
12107     }
12108
12109   /* Choose correct order to not overwrite the source before it is copied.  */
12110   if ((REG_P (part[0][0])
12111        && REG_P (part[1][1])
12112        && (REGNO (part[0][0]) == REGNO (part[1][1])
12113            || (nparts == 3
12114                && REGNO (part[0][0]) == REGNO (part[1][2]))))
12115       || (collisions > 0
12116           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
12117     {
12118       if (nparts == 3)
12119         {
12120           operands[2] = part[0][2];
12121           operands[3] = part[0][1];
12122           operands[4] = part[0][0];
12123           operands[5] = part[1][2];
12124           operands[6] = part[1][1];
12125           operands[7] = part[1][0];
12126         }
12127       else
12128         {
12129           operands[2] = part[0][1];
12130           operands[3] = part[0][0];
12131           operands[5] = part[1][1];
12132           operands[6] = part[1][0];
12133         }
12134     }
12135   else
12136     {
12137       if (nparts == 3)
12138         {
12139           operands[2] = part[0][0];
12140           operands[3] = part[0][1];
12141           operands[4] = part[0][2];
12142           operands[5] = part[1][0];
12143           operands[6] = part[1][1];
12144           operands[7] = part[1][2];
12145         }
12146       else
12147         {
12148           operands[2] = part[0][0];
12149           operands[3] = part[0][1];
12150           operands[5] = part[1][0];
12151           operands[6] = part[1][1];
12152         }
12153     }
12154
12155   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
12156   if (optimize_size)
12157     {
12158       if (GET_CODE (operands[5]) == CONST_INT
12159           && operands[5] != const0_rtx
12160           && REG_P (operands[2]))
12161         {
12162           if (GET_CODE (operands[6]) == CONST_INT
12163               && INTVAL (operands[6]) == INTVAL (operands[5]))
12164             operands[6] = operands[2];
12165
12166           if (nparts == 3
12167               && GET_CODE (operands[7]) == CONST_INT
12168               && INTVAL (operands[7]) == INTVAL (operands[5]))
12169             operands[7] = operands[2];
12170         }
12171
12172       if (nparts == 3
12173           && GET_CODE (operands[6]) == CONST_INT
12174           && operands[6] != const0_rtx
12175           && REG_P (operands[3])
12176           && GET_CODE (operands[7]) == CONST_INT
12177           && INTVAL (operands[7]) == INTVAL (operands[6]))
12178         operands[7] = operands[3];
12179     }
12180
12181   emit_move_insn (operands[2], operands[5]);
12182   emit_move_insn (operands[3], operands[6]);
12183   if (nparts == 3)
12184     emit_move_insn (operands[4], operands[7]);
12185
12186   return;
12187 }
12188
12189 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
12190    left shift by a constant, either using a single shift or
12191    a sequence of add instructions.  */
12192
12193 static void
12194 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
12195 {
12196   if (count == 1)
12197     {
12198       emit_insn ((mode == DImode
12199                   ? gen_addsi3
12200                   : gen_adddi3) (operand, operand, operand));
12201     }
12202   else if (!optimize_size
12203            && count * ix86_cost->add <= ix86_cost->shift_const)
12204     {
12205       int i;
12206       for (i=0; i<count; i++)
12207         {
12208           emit_insn ((mode == DImode
12209                       ? gen_addsi3
12210                       : gen_adddi3) (operand, operand, operand));
12211         }
12212     }
12213   else
12214     emit_insn ((mode == DImode
12215                 ? gen_ashlsi3
12216                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
12217 }
12218
12219 void
12220 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
12221 {
12222   rtx low[2], high[2];
12223   int count;
12224   const int single_width = mode == DImode ? 32 : 64;
12225
12226   if (GET_CODE (operands[2]) == CONST_INT)
12227     {
12228       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12229       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12230
12231       if (count >= single_width)
12232         {
12233           emit_move_insn (high[0], low[1]);
12234           emit_move_insn (low[0], const0_rtx);
12235
12236           if (count > single_width)
12237             ix86_expand_ashl_const (high[0], count - single_width, mode);
12238         }
12239       else
12240         {
12241           if (!rtx_equal_p (operands[0], operands[1]))
12242             emit_move_insn (operands[0], operands[1]);
12243           emit_insn ((mode == DImode
12244                      ? gen_x86_shld_1
12245                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
12246           ix86_expand_ashl_const (low[0], count, mode);
12247         }
12248       return;
12249     }
12250
12251   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12252
12253   if (operands[1] == const1_rtx)
12254     {
12255       /* Assuming we've chosen a QImode capable registers, then 1 << N
12256          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
12257       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
12258         {
12259           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
12260
12261           ix86_expand_clear (low[0]);
12262           ix86_expand_clear (high[0]);
12263           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
12264
12265           d = gen_lowpart (QImode, low[0]);
12266           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12267           s = gen_rtx_EQ (QImode, flags, const0_rtx);
12268           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12269
12270           d = gen_lowpart (QImode, high[0]);
12271           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12272           s = gen_rtx_NE (QImode, flags, const0_rtx);
12273           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12274         }
12275
12276       /* Otherwise, we can get the same results by manually performing
12277          a bit extract operation on bit 5/6, and then performing the two
12278          shifts.  The two methods of getting 0/1 into low/high are exactly
12279          the same size.  Avoiding the shift in the bit extract case helps
12280          pentium4 a bit; no one else seems to care much either way.  */
12281       else
12282         {
12283           rtx x;
12284
12285           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
12286             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
12287           else
12288             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
12289           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
12290
12291           emit_insn ((mode == DImode
12292                       ? gen_lshrsi3
12293                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
12294           emit_insn ((mode == DImode
12295                       ? gen_andsi3
12296                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
12297           emit_move_insn (low[0], high[0]);
12298           emit_insn ((mode == DImode
12299                       ? gen_xorsi3
12300                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
12301         }
12302
12303       emit_insn ((mode == DImode
12304                     ? gen_ashlsi3
12305                     : gen_ashldi3) (low[0], low[0], operands[2]));
12306       emit_insn ((mode == DImode
12307                     ? gen_ashlsi3
12308                     : gen_ashldi3) (high[0], high[0], operands[2]));
12309       return;
12310     }
12311
12312   if (operands[1] == constm1_rtx)
12313     {
12314       /* For -1 << N, we can avoid the shld instruction, because we
12315          know that we're shifting 0...31/63 ones into a -1.  */
12316       emit_move_insn (low[0], constm1_rtx);
12317       if (optimize_size)
12318         emit_move_insn (high[0], low[0]);
12319       else
12320         emit_move_insn (high[0], constm1_rtx);
12321     }
12322   else
12323     {
12324       if (!rtx_equal_p (operands[0], operands[1]))
12325         emit_move_insn (operands[0], operands[1]);
12326
12327       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12328       emit_insn ((mode == DImode
12329                   ? gen_x86_shld_1
12330                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
12331     }
12332
12333   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
12334
12335   if (TARGET_CMOVE && scratch)
12336     {
12337       ix86_expand_clear (scratch);
12338       emit_insn ((mode == DImode
12339                   ? gen_x86_shift_adj_1
12340                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
12341     }
12342   else
12343     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
12344 }
12345
12346 void
12347 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
12348 {
12349   rtx low[2], high[2];
12350   int count;
12351   const int single_width = mode == DImode ? 32 : 64;
12352
12353   if (GET_CODE (operands[2]) == CONST_INT)
12354     {
12355       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12356       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12357
12358       if (count == single_width * 2 - 1)
12359         {
12360           emit_move_insn (high[0], high[1]);
12361           emit_insn ((mode == DImode
12362                       ? gen_ashrsi3
12363                       : gen_ashrdi3) (high[0], high[0],
12364                                       GEN_INT (single_width - 1)));
12365           emit_move_insn (low[0], high[0]);
12366
12367         }
12368       else if (count >= single_width)
12369         {
12370           emit_move_insn (low[0], high[1]);
12371           emit_move_insn (high[0], low[0]);
12372           emit_insn ((mode == DImode
12373                       ? gen_ashrsi3
12374                       : gen_ashrdi3) (high[0], high[0],
12375                                       GEN_INT (single_width - 1)));
12376           if (count > single_width)
12377             emit_insn ((mode == DImode
12378                         ? gen_ashrsi3
12379                         : gen_ashrdi3) (low[0], low[0],
12380                                         GEN_INT (count - single_width)));
12381         }
12382       else
12383         {
12384           if (!rtx_equal_p (operands[0], operands[1]))
12385             emit_move_insn (operands[0], operands[1]);
12386           emit_insn ((mode == DImode
12387                       ? gen_x86_shrd_1
12388                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12389           emit_insn ((mode == DImode
12390                       ? gen_ashrsi3
12391                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
12392         }
12393     }
12394   else
12395     {
12396       if (!rtx_equal_p (operands[0], operands[1]))
12397         emit_move_insn (operands[0], operands[1]);
12398
12399       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12400
12401       emit_insn ((mode == DImode
12402                   ? gen_x86_shrd_1
12403                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12404       emit_insn ((mode == DImode
12405                   ? gen_ashrsi3
12406                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
12407
12408       if (TARGET_CMOVE && scratch)
12409         {
12410           emit_move_insn (scratch, high[0]);
12411           emit_insn ((mode == DImode
12412                       ? gen_ashrsi3
12413                       : gen_ashrdi3) (scratch, scratch,
12414                                       GEN_INT (single_width - 1)));
12415           emit_insn ((mode == DImode
12416                       ? gen_x86_shift_adj_1
12417                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12418                                          scratch));
12419         }
12420       else
12421         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
12422     }
12423 }
12424
12425 void
12426 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
12427 {
12428   rtx low[2], high[2];
12429   int count;
12430   const int single_width = mode == DImode ? 32 : 64;
12431
12432   if (GET_CODE (operands[2]) == CONST_INT)
12433     {
12434       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12435       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12436
12437       if (count >= single_width)
12438         {
12439           emit_move_insn (low[0], high[1]);
12440           ix86_expand_clear (high[0]);
12441
12442           if (count > single_width)
12443             emit_insn ((mode == DImode
12444                         ? gen_lshrsi3
12445                         : gen_lshrdi3) (low[0], low[0],
12446                                         GEN_INT (count - single_width)));
12447         }
12448       else
12449         {
12450           if (!rtx_equal_p (operands[0], operands[1]))
12451             emit_move_insn (operands[0], operands[1]);
12452           emit_insn ((mode == DImode
12453                       ? gen_x86_shrd_1
12454                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12455           emit_insn ((mode == DImode
12456                       ? gen_lshrsi3
12457                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
12458         }
12459     }
12460   else
12461     {
12462       if (!rtx_equal_p (operands[0], operands[1]))
12463         emit_move_insn (operands[0], operands[1]);
12464
12465       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12466
12467       emit_insn ((mode == DImode
12468                   ? gen_x86_shrd_1
12469                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12470       emit_insn ((mode == DImode
12471                   ? gen_lshrsi3
12472                   : gen_lshrdi3) (high[0], high[0], operands[2]));
12473
12474       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
12475       if (TARGET_CMOVE && scratch)
12476         {
12477           ix86_expand_clear (scratch);
12478           emit_insn ((mode == DImode
12479                       ? gen_x86_shift_adj_1
12480                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12481                                                scratch));
12482         }
12483       else
12484         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
12485     }
12486 }
12487
12488 /* Helper function for the string operations below.  Dest VARIABLE whether
12489    it is aligned to VALUE bytes.  If true, jump to the label.  */
12490 static rtx
12491 ix86_expand_aligntest (rtx variable, int value)
12492 {
12493   rtx label = gen_label_rtx ();
12494   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
12495   if (GET_MODE (variable) == DImode)
12496     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
12497   else
12498     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
12499   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
12500                            1, label);
12501   return label;
12502 }
12503
12504 /* Adjust COUNTER by the VALUE.  */
12505 static void
12506 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
12507 {
12508   if (GET_MODE (countreg) == DImode)
12509     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
12510   else
12511     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
12512 }
12513
12514 /* Zero extend possibly SImode EXP to Pmode register.  */
12515 rtx
12516 ix86_zero_extend_to_Pmode (rtx exp)
12517 {
12518   rtx r;
12519   if (GET_MODE (exp) == VOIDmode)
12520     return force_reg (Pmode, exp);
12521   if (GET_MODE (exp) == Pmode)
12522     return copy_to_mode_reg (Pmode, exp);
12523   r = gen_reg_rtx (Pmode);
12524   emit_insn (gen_zero_extendsidi2 (r, exp));
12525   return r;
12526 }
12527
12528 /* Expand string move (memcpy) operation.  Use i386 string operations when
12529    profitable.  expand_clrmem contains similar code.  */
12530 int
12531 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
12532 {
12533   rtx srcreg, destreg, countreg, srcexp, destexp;
12534   enum machine_mode counter_mode;
12535   HOST_WIDE_INT align = 0;
12536   unsigned HOST_WIDE_INT count = 0;
12537
12538   if (GET_CODE (align_exp) == CONST_INT)
12539     align = INTVAL (align_exp);
12540
12541   /* Can't use any of this if the user has appropriated esi or edi.  */
12542   if (global_regs[4] || global_regs[5])
12543     return 0;
12544
12545   /* This simple hack avoids all inlining code and simplifies code below.  */
12546   if (!TARGET_ALIGN_STRINGOPS)
12547     align = 64;
12548
12549   if (GET_CODE (count_exp) == CONST_INT)
12550     {
12551       count = INTVAL (count_exp);
12552       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12553         return 0;
12554     }
12555
12556   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12557      for 64bits use SImode when possible, otherwise DImode.
12558      Set count to number of bytes copied when known at compile time.  */
12559   if (!TARGET_64BIT
12560       || GET_MODE (count_exp) == SImode
12561       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12562     counter_mode = SImode;
12563   else
12564     counter_mode = DImode;
12565
12566   gcc_assert (counter_mode == SImode || counter_mode == DImode);
12567
12568   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12569   if (destreg != XEXP (dst, 0))
12570     dst = replace_equiv_address_nv (dst, destreg);
12571   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
12572   if (srcreg != XEXP (src, 0))
12573     src = replace_equiv_address_nv (src, srcreg);
12574
12575   /* When optimizing for size emit simple rep ; movsb instruction for
12576      counts not divisible by 4, except when (movsl;)*(movsw;)?(movsb;)?
12577      sequence is shorter than mov{b,l} $count, %{ecx,cl}; rep; movsb.
12578      Sice of (movsl;)*(movsw;)?(movsb;)? sequence is
12579      count / 4 + (count & 3), the other sequence is either 4 or 7 bytes,
12580      but we don't know whether upper 24 (resp. 56) bits of %ecx will be
12581      known to be zero or not.  The rep; movsb sequence causes higher
12582      register pressure though, so take that into account.  */
12583
12584   if ((!optimize || optimize_size)
12585       && (count == 0
12586           || ((count & 0x03)
12587               && (!optimize_size
12588                   || count > 5 * 4
12589                   || (count & 3) + count / 4 > 6))))
12590     {
12591       emit_insn (gen_cld ());
12592       countreg = ix86_zero_extend_to_Pmode (count_exp);
12593       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12594       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
12595       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
12596                               destexp, srcexp));
12597     }
12598
12599   /* For constant aligned (or small unaligned) copies use rep movsl
12600      followed by code copying the rest.  For PentiumPro ensure 8 byte
12601      alignment to allow rep movsl acceleration.  */
12602
12603   else if (count != 0
12604            && (align >= 8
12605                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12606                || optimize_size || count < (unsigned int) 64))
12607     {
12608       unsigned HOST_WIDE_INT offset = 0;
12609       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12610       rtx srcmem, dstmem;
12611
12612       emit_insn (gen_cld ());
12613       if (count & ~(size - 1))
12614         {
12615           if ((TARGET_SINGLE_STRINGOP || optimize_size) && count < 5 * 4)
12616             {
12617               enum machine_mode movs_mode = size == 4 ? SImode : DImode;
12618
12619               while (offset < (count & ~(size - 1)))
12620                 {
12621                   srcmem = adjust_automodify_address_nv (src, movs_mode,
12622                                                          srcreg, offset);
12623                   dstmem = adjust_automodify_address_nv (dst, movs_mode,
12624                                                          destreg, offset);
12625                   emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12626                   offset += size;
12627                 }
12628             }
12629           else
12630             {
12631               countreg = GEN_INT ((count >> (size == 4 ? 2 : 3))
12632                                   & (TARGET_64BIT ? -1 : 0x3fffffff));
12633               countreg = copy_to_mode_reg (counter_mode, countreg);
12634               countreg = ix86_zero_extend_to_Pmode (countreg);
12635
12636               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12637                                         GEN_INT (size == 4 ? 2 : 3));
12638               srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12639               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12640
12641               emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12642                                       countreg, destexp, srcexp));
12643               offset = count & ~(size - 1);
12644             }
12645         }
12646       if (size == 8 && (count & 0x04))
12647         {
12648           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
12649                                                  offset);
12650           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
12651                                                  offset);
12652           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12653           offset += 4;
12654         }
12655       if (count & 0x02)
12656         {
12657           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
12658                                                  offset);
12659           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
12660                                                  offset);
12661           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12662           offset += 2;
12663         }
12664       if (count & 0x01)
12665         {
12666           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
12667                                                  offset);
12668           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
12669                                                  offset);
12670           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12671         }
12672     }
12673   /* The generic code based on the glibc implementation:
12674      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
12675      allowing accelerated copying there)
12676      - copy the data using rep movsl
12677      - copy the rest.  */
12678   else
12679     {
12680       rtx countreg2;
12681       rtx label = NULL;
12682       rtx srcmem, dstmem;
12683       int desired_alignment = (TARGET_PENTIUMPRO
12684                                && (count == 0 || count >= (unsigned int) 260)
12685                                ? 8 : UNITS_PER_WORD);
12686       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
12687       dst = change_address (dst, BLKmode, destreg);
12688       src = change_address (src, BLKmode, srcreg);
12689
12690       /* In case we don't know anything about the alignment, default to
12691          library version, since it is usually equally fast and result in
12692          shorter code.
12693
12694          Also emit call when we know that the count is large and call overhead
12695          will not be important.  */
12696       if (!TARGET_INLINE_ALL_STRINGOPS
12697           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12698         return 0;
12699
12700       if (TARGET_SINGLE_STRINGOP)
12701         emit_insn (gen_cld ());
12702
12703       countreg2 = gen_reg_rtx (Pmode);
12704       countreg = copy_to_mode_reg (counter_mode, count_exp);
12705
12706       /* We don't use loops to align destination and to copy parts smaller
12707          than 4 bytes, because gcc is able to optimize such code better (in
12708          the case the destination or the count really is aligned, gcc is often
12709          able to predict the branches) and also it is friendlier to the
12710          hardware branch prediction.
12711
12712          Using loops is beneficial for generic case, because we can
12713          handle small counts using the loops.  Many CPUs (such as Athlon)
12714          have large REP prefix setup costs.
12715
12716          This is quite costly.  Maybe we can revisit this decision later or
12717          add some customizability to this code.  */
12718
12719       if (count == 0 && align < desired_alignment)
12720         {
12721           label = gen_label_rtx ();
12722           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12723                                    LEU, 0, counter_mode, 1, label);
12724         }
12725       if (align <= 1)
12726         {
12727           rtx label = ix86_expand_aligntest (destreg, 1);
12728           srcmem = change_address (src, QImode, srcreg);
12729           dstmem = change_address (dst, QImode, destreg);
12730           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12731           ix86_adjust_counter (countreg, 1);
12732           emit_label (label);
12733           LABEL_NUSES (label) = 1;
12734         }
12735       if (align <= 2)
12736         {
12737           rtx label = ix86_expand_aligntest (destreg, 2);
12738           srcmem = change_address (src, HImode, srcreg);
12739           dstmem = change_address (dst, HImode, destreg);
12740           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12741           ix86_adjust_counter (countreg, 2);
12742           emit_label (label);
12743           LABEL_NUSES (label) = 1;
12744         }
12745       if (align <= 4 && desired_alignment > 4)
12746         {
12747           rtx label = ix86_expand_aligntest (destreg, 4);
12748           srcmem = change_address (src, SImode, srcreg);
12749           dstmem = change_address (dst, SImode, destreg);
12750           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12751           ix86_adjust_counter (countreg, 4);
12752           emit_label (label);
12753           LABEL_NUSES (label) = 1;
12754         }
12755
12756       if (label && desired_alignment > 4 && !TARGET_64BIT)
12757         {
12758           emit_label (label);
12759           LABEL_NUSES (label) = 1;
12760           label = NULL_RTX;
12761         }
12762       if (!TARGET_SINGLE_STRINGOP)
12763         emit_insn (gen_cld ());
12764       if (TARGET_64BIT)
12765         {
12766           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12767                                   GEN_INT (3)));
12768           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12769         }
12770       else
12771         {
12772           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12773           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12774         }
12775       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12776       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12777       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12778                               countreg2, destexp, srcexp));
12779
12780       if (label)
12781         {
12782           emit_label (label);
12783           LABEL_NUSES (label) = 1;
12784         }
12785       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12786         {
12787           srcmem = change_address (src, SImode, srcreg);
12788           dstmem = change_address (dst, SImode, destreg);
12789           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12790         }
12791       if ((align <= 4 || count == 0) && TARGET_64BIT)
12792         {
12793           rtx label = ix86_expand_aligntest (countreg, 4);
12794           srcmem = change_address (src, SImode, srcreg);
12795           dstmem = change_address (dst, SImode, destreg);
12796           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12797           emit_label (label);
12798           LABEL_NUSES (label) = 1;
12799         }
12800       if (align > 2 && count != 0 && (count & 2))
12801         {
12802           srcmem = change_address (src, HImode, srcreg);
12803           dstmem = change_address (dst, HImode, destreg);
12804           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12805         }
12806       if (align <= 2 || count == 0)
12807         {
12808           rtx label = ix86_expand_aligntest (countreg, 2);
12809           srcmem = change_address (src, HImode, srcreg);
12810           dstmem = change_address (dst, HImode, destreg);
12811           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12812           emit_label (label);
12813           LABEL_NUSES (label) = 1;
12814         }
12815       if (align > 1 && count != 0 && (count & 1))
12816         {
12817           srcmem = change_address (src, QImode, srcreg);
12818           dstmem = change_address (dst, QImode, destreg);
12819           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12820         }
12821       if (align <= 1 || count == 0)
12822         {
12823           rtx label = ix86_expand_aligntest (countreg, 1);
12824           srcmem = change_address (src, QImode, srcreg);
12825           dstmem = change_address (dst, QImode, destreg);
12826           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12827           emit_label (label);
12828           LABEL_NUSES (label) = 1;
12829         }
12830     }
12831
12832   return 1;
12833 }
12834
12835 /* Expand string clear operation (bzero).  Use i386 string operations when
12836    profitable.  expand_movmem contains similar code.  */
12837 int
12838 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
12839 {
12840   rtx destreg, zeroreg, countreg, destexp;
12841   enum machine_mode counter_mode;
12842   HOST_WIDE_INT align = 0;
12843   unsigned HOST_WIDE_INT count = 0;
12844
12845   if (GET_CODE (align_exp) == CONST_INT)
12846     align = INTVAL (align_exp);
12847
12848   /* Can't use any of this if the user has appropriated esi.  */
12849   if (global_regs[4])
12850     return 0;
12851
12852   /* This simple hack avoids all inlining code and simplifies code below.  */
12853   if (!TARGET_ALIGN_STRINGOPS)
12854     align = 32;
12855
12856   if (GET_CODE (count_exp) == CONST_INT)
12857     {
12858       count = INTVAL (count_exp);
12859       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12860         return 0;
12861     }
12862   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12863      for 64bits use SImode when possible, otherwise DImode.
12864      Set count to number of bytes copied when known at compile time.  */
12865   if (!TARGET_64BIT
12866       || GET_MODE (count_exp) == SImode
12867       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12868     counter_mode = SImode;
12869   else
12870     counter_mode = DImode;
12871
12872   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12873   if (destreg != XEXP (dst, 0))
12874     dst = replace_equiv_address_nv (dst, destreg);
12875
12876
12877   /* When optimizing for size emit simple rep ; movsb instruction for
12878      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
12879      sequence is 7 bytes long, so if optimizing for size and count is
12880      small enough that some stosl, stosw and stosb instructions without
12881      rep are shorter, fall back into the next if.  */
12882
12883   if ((!optimize || optimize_size)
12884       && (count == 0
12885           || ((count & 0x03)
12886               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
12887     {
12888       emit_insn (gen_cld ());
12889
12890       countreg = ix86_zero_extend_to_Pmode (count_exp);
12891       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
12892       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12893       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
12894     }
12895   else if (count != 0
12896            && (align >= 8
12897                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12898                || optimize_size || count < (unsigned int) 64))
12899     {
12900       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12901       unsigned HOST_WIDE_INT offset = 0;
12902
12903       emit_insn (gen_cld ());
12904
12905       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
12906       if (count & ~(size - 1))
12907         {
12908           unsigned HOST_WIDE_INT repcount;
12909           unsigned int max_nonrep;
12910
12911           repcount = count >> (size == 4 ? 2 : 3);
12912           if (!TARGET_64BIT)
12913             repcount &= 0x3fffffff;
12914
12915           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
12916              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
12917              bytes.  In both cases the latter seems to be faster for small
12918              values of N.  */
12919           max_nonrep = size == 4 ? 7 : 4;
12920           if (!optimize_size)
12921             switch (ix86_tune)
12922               {
12923               case PROCESSOR_PENTIUM4:
12924               case PROCESSOR_NOCONA:
12925                 max_nonrep = 3;
12926                 break;
12927               default:
12928                 break;
12929               }
12930
12931           if (repcount <= max_nonrep)
12932             while (repcount-- > 0)
12933               {
12934                 rtx mem = adjust_automodify_address_nv (dst,
12935                                                         GET_MODE (zeroreg),
12936                                                         destreg, offset);
12937                 emit_insn (gen_strset (destreg, mem, zeroreg));
12938                 offset += size;
12939               }
12940           else
12941             {
12942               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
12943               countreg = ix86_zero_extend_to_Pmode (countreg);
12944               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12945                                         GEN_INT (size == 4 ? 2 : 3));
12946               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12947               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
12948                                        destexp));
12949               offset = count & ~(size - 1);
12950             }
12951         }
12952       if (size == 8 && (count & 0x04))
12953         {
12954           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
12955                                                   offset);
12956           emit_insn (gen_strset (destreg, mem,
12957                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12958           offset += 4;
12959         }
12960       if (count & 0x02)
12961         {
12962           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
12963                                                   offset);
12964           emit_insn (gen_strset (destreg, mem,
12965                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12966           offset += 2;
12967         }
12968       if (count & 0x01)
12969         {
12970           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
12971                                                   offset);
12972           emit_insn (gen_strset (destreg, mem,
12973                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12974         }
12975     }
12976   else
12977     {
12978       rtx countreg2;
12979       rtx label = NULL;
12980       /* Compute desired alignment of the string operation.  */
12981       int desired_alignment = (TARGET_PENTIUMPRO
12982                                && (count == 0 || count >= (unsigned int) 260)
12983                                ? 8 : UNITS_PER_WORD);
12984
12985       /* In case we don't know anything about the alignment, default to
12986          library version, since it is usually equally fast and result in
12987          shorter code.
12988
12989          Also emit call when we know that the count is large and call overhead
12990          will not be important.  */
12991       if (!TARGET_INLINE_ALL_STRINGOPS
12992           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12993         return 0;
12994
12995       if (TARGET_SINGLE_STRINGOP)
12996         emit_insn (gen_cld ());
12997
12998       countreg2 = gen_reg_rtx (Pmode);
12999       countreg = copy_to_mode_reg (counter_mode, count_exp);
13000       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
13001       /* Get rid of MEM_OFFSET, it won't be accurate.  */
13002       dst = change_address (dst, BLKmode, destreg);
13003
13004       if (count == 0 && align < desired_alignment)
13005         {
13006           label = gen_label_rtx ();
13007           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
13008                                    LEU, 0, counter_mode, 1, label);
13009         }
13010       if (align <= 1)
13011         {
13012           rtx label = ix86_expand_aligntest (destreg, 1);
13013           emit_insn (gen_strset (destreg, dst,
13014                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
13015           ix86_adjust_counter (countreg, 1);
13016           emit_label (label);
13017           LABEL_NUSES (label) = 1;
13018         }
13019       if (align <= 2)
13020         {
13021           rtx label = ix86_expand_aligntest (destreg, 2);
13022           emit_insn (gen_strset (destreg, dst,
13023                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
13024           ix86_adjust_counter (countreg, 2);
13025           emit_label (label);
13026           LABEL_NUSES (label) = 1;
13027         }
13028       if (align <= 4 && desired_alignment > 4)
13029         {
13030           rtx label = ix86_expand_aligntest (destreg, 4);
13031           emit_insn (gen_strset (destreg, dst,
13032                                  (TARGET_64BIT
13033                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
13034                                   : zeroreg)));
13035           ix86_adjust_counter (countreg, 4);
13036           emit_label (label);
13037           LABEL_NUSES (label) = 1;
13038         }
13039
13040       if (label && desired_alignment > 4 && !TARGET_64BIT)
13041         {
13042           emit_label (label);
13043           LABEL_NUSES (label) = 1;
13044           label = NULL_RTX;
13045         }
13046
13047       if (!TARGET_SINGLE_STRINGOP)
13048         emit_insn (gen_cld ());
13049       if (TARGET_64BIT)
13050         {
13051           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
13052                                   GEN_INT (3)));
13053           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
13054         }
13055       else
13056         {
13057           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
13058           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
13059         }
13060       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
13061       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
13062
13063       if (label)
13064         {
13065           emit_label (label);
13066           LABEL_NUSES (label) = 1;
13067         }
13068
13069       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
13070         emit_insn (gen_strset (destreg, dst,
13071                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
13072       if (TARGET_64BIT && (align <= 4 || count == 0))
13073         {
13074           rtx label = ix86_expand_aligntest (countreg, 4);
13075           emit_insn (gen_strset (destreg, dst,
13076                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
13077           emit_label (label);
13078           LABEL_NUSES (label) = 1;
13079         }
13080       if (align > 2 && count != 0 && (count & 2))
13081         emit_insn (gen_strset (destreg, dst,
13082                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
13083       if (align <= 2 || count == 0)
13084         {
13085           rtx label = ix86_expand_aligntest (countreg, 2);
13086           emit_insn (gen_strset (destreg, dst,
13087                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
13088           emit_label (label);
13089           LABEL_NUSES (label) = 1;
13090         }
13091       if (align > 1 && count != 0 && (count & 1))
13092         emit_insn (gen_strset (destreg, dst,
13093                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
13094       if (align <= 1 || count == 0)
13095         {
13096           rtx label = ix86_expand_aligntest (countreg, 1);
13097           emit_insn (gen_strset (destreg, dst,
13098                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
13099           emit_label (label);
13100           LABEL_NUSES (label) = 1;
13101         }
13102     }
13103   return 1;
13104 }
13105
13106 /* Expand strlen.  */
13107 int
13108 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
13109 {
13110   rtx addr, scratch1, scratch2, scratch3, scratch4;
13111
13112   /* The generic case of strlen expander is long.  Avoid it's
13113      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
13114
13115   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
13116       && !TARGET_INLINE_ALL_STRINGOPS
13117       && !optimize_size
13118       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
13119     return 0;
13120
13121   addr = force_reg (Pmode, XEXP (src, 0));
13122   scratch1 = gen_reg_rtx (Pmode);
13123
13124   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
13125       && !optimize_size)
13126     {
13127       /* Well it seems that some optimizer does not combine a call like
13128          foo(strlen(bar), strlen(bar));
13129          when the move and the subtraction is done here.  It does calculate
13130          the length just once when these instructions are done inside of
13131          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
13132          often used and I use one fewer register for the lifetime of
13133          output_strlen_unroll() this is better.  */
13134
13135       emit_move_insn (out, addr);
13136
13137       ix86_expand_strlensi_unroll_1 (out, src, align);
13138
13139       /* strlensi_unroll_1 returns the address of the zero at the end of
13140          the string, like memchr(), so compute the length by subtracting
13141          the start address.  */
13142       if (TARGET_64BIT)
13143         emit_insn (gen_subdi3 (out, out, addr));
13144       else
13145         emit_insn (gen_subsi3 (out, out, addr));
13146     }
13147   else
13148     {
13149       rtx unspec;
13150       scratch2 = gen_reg_rtx (Pmode);
13151       scratch3 = gen_reg_rtx (Pmode);
13152       scratch4 = force_reg (Pmode, constm1_rtx);
13153
13154       emit_move_insn (scratch3, addr);
13155       eoschar = force_reg (QImode, eoschar);
13156
13157       emit_insn (gen_cld ());
13158       src = replace_equiv_address_nv (src, scratch3);
13159
13160       /* If .md starts supporting :P, this can be done in .md.  */
13161       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
13162                                                  scratch4), UNSPEC_SCAS);
13163       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
13164       if (TARGET_64BIT)
13165         {
13166           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
13167           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
13168         }
13169       else
13170         {
13171           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
13172           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
13173         }
13174     }
13175   return 1;
13176 }
13177
13178 /* Expand the appropriate insns for doing strlen if not just doing
13179    repnz; scasb
13180
13181    out = result, initialized with the start address
13182    align_rtx = alignment of the address.
13183    scratch = scratch register, initialized with the startaddress when
13184         not aligned, otherwise undefined
13185
13186    This is just the body. It needs the initializations mentioned above and
13187    some address computing at the end.  These things are done in i386.md.  */
13188
13189 static void
13190 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
13191 {
13192   int align;
13193   rtx tmp;
13194   rtx align_2_label = NULL_RTX;
13195   rtx align_3_label = NULL_RTX;
13196   rtx align_4_label = gen_label_rtx ();
13197   rtx end_0_label = gen_label_rtx ();
13198   rtx mem;
13199   rtx tmpreg = gen_reg_rtx (SImode);
13200   rtx scratch = gen_reg_rtx (SImode);
13201   rtx cmp;
13202
13203   align = 0;
13204   if (GET_CODE (align_rtx) == CONST_INT)
13205     align = INTVAL (align_rtx);
13206
13207   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
13208
13209   /* Is there a known alignment and is it less than 4?  */
13210   if (align < 4)
13211     {
13212       rtx scratch1 = gen_reg_rtx (Pmode);
13213       emit_move_insn (scratch1, out);
13214       /* Is there a known alignment and is it not 2? */
13215       if (align != 2)
13216         {
13217           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
13218           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
13219
13220           /* Leave just the 3 lower bits.  */
13221           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
13222                                     NULL_RTX, 0, OPTAB_WIDEN);
13223
13224           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
13225                                    Pmode, 1, align_4_label);
13226           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
13227                                    Pmode, 1, align_2_label);
13228           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
13229                                    Pmode, 1, align_3_label);
13230         }
13231       else
13232         {
13233           /* Since the alignment is 2, we have to check 2 or 0 bytes;
13234              check if is aligned to 4 - byte.  */
13235
13236           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
13237                                     NULL_RTX, 0, OPTAB_WIDEN);
13238
13239           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
13240                                    Pmode, 1, align_4_label);
13241         }
13242
13243       mem = change_address (src, QImode, out);
13244
13245       /* Now compare the bytes.  */
13246
13247       /* Compare the first n unaligned byte on a byte per byte basis.  */
13248       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
13249                                QImode, 1, end_0_label);
13250
13251       /* Increment the address.  */
13252       if (TARGET_64BIT)
13253         emit_insn (gen_adddi3 (out, out, const1_rtx));
13254       else
13255         emit_insn (gen_addsi3 (out, out, const1_rtx));
13256
13257       /* Not needed with an alignment of 2 */
13258       if (align != 2)
13259         {
13260           emit_label (align_2_label);
13261
13262           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
13263                                    end_0_label);
13264
13265           if (TARGET_64BIT)
13266             emit_insn (gen_adddi3 (out, out, const1_rtx));
13267           else
13268             emit_insn (gen_addsi3 (out, out, const1_rtx));
13269
13270           emit_label (align_3_label);
13271         }
13272
13273       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
13274                                end_0_label);
13275
13276       if (TARGET_64BIT)
13277         emit_insn (gen_adddi3 (out, out, const1_rtx));
13278       else
13279         emit_insn (gen_addsi3 (out, out, const1_rtx));
13280     }
13281
13282   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
13283      align this loop.  It gives only huge programs, but does not help to
13284      speed up.  */
13285   emit_label (align_4_label);
13286
13287   mem = change_address (src, SImode, out);
13288   emit_move_insn (scratch, mem);
13289   if (TARGET_64BIT)
13290     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
13291   else
13292     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
13293
13294   /* This formula yields a nonzero result iff one of the bytes is zero.
13295      This saves three branches inside loop and many cycles.  */
13296
13297   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
13298   emit_insn (gen_one_cmplsi2 (scratch, scratch));
13299   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
13300   emit_insn (gen_andsi3 (tmpreg, tmpreg,
13301                          gen_int_mode (0x80808080, SImode)));
13302   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
13303                            align_4_label);
13304
13305   if (TARGET_CMOVE)
13306     {
13307        rtx reg = gen_reg_rtx (SImode);
13308        rtx reg2 = gen_reg_rtx (Pmode);
13309        emit_move_insn (reg, tmpreg);
13310        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
13311
13312        /* If zero is not in the first two bytes, move two bytes forward.  */
13313        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13314        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13315        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13316        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
13317                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
13318                                                      reg,
13319                                                      tmpreg)));
13320        /* Emit lea manually to avoid clobbering of flags.  */
13321        emit_insn (gen_rtx_SET (SImode, reg2,
13322                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
13323
13324        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13325        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13326        emit_insn (gen_rtx_SET (VOIDmode, out,
13327                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
13328                                                      reg2,
13329                                                      out)));
13330
13331     }
13332   else
13333     {
13334        rtx end_2_label = gen_label_rtx ();
13335        /* Is zero in the first two bytes? */
13336
13337        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13338        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13339        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
13340        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
13341                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
13342                             pc_rtx);
13343        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
13344        JUMP_LABEL (tmp) = end_2_label;
13345
13346        /* Not in the first two.  Move two bytes forward.  */
13347        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
13348        if (TARGET_64BIT)
13349          emit_insn (gen_adddi3 (out, out, const2_rtx));
13350        else
13351          emit_insn (gen_addsi3 (out, out, const2_rtx));
13352
13353        emit_label (end_2_label);
13354
13355     }
13356
13357   /* Avoid branch in fixing the byte.  */
13358   tmpreg = gen_lowpart (QImode, tmpreg);
13359   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
13360   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
13361   if (TARGET_64BIT)
13362     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
13363   else
13364     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
13365
13366   emit_label (end_0_label);
13367 }
13368
13369 void
13370 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
13371                   rtx callarg2 ATTRIBUTE_UNUSED,
13372                   rtx pop, int sibcall)
13373 {
13374   rtx use = NULL, call;
13375
13376   if (pop == const0_rtx)
13377     pop = NULL;
13378   gcc_assert (!TARGET_64BIT || !pop);
13379
13380   if (TARGET_MACHO && !TARGET_64BIT)
13381     {
13382 #if TARGET_MACHO
13383       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
13384         fnaddr = machopic_indirect_call_target (fnaddr);
13385 #endif
13386     }
13387   else
13388     {
13389       /* Static functions and indirect calls don't need the pic register.  */
13390       if (! TARGET_64BIT && flag_pic
13391           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
13392           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
13393         use_reg (&use, pic_offset_table_rtx);
13394     }
13395
13396   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
13397     {
13398       rtx al = gen_rtx_REG (QImode, 0);
13399       emit_move_insn (al, callarg2);
13400       use_reg (&use, al);
13401     }
13402
13403   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
13404     {
13405       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13406       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13407     }
13408   if (sibcall && TARGET_64BIT
13409       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
13410     {
13411       rtx addr;
13412       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13413       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
13414       emit_move_insn (fnaddr, addr);
13415       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13416     }
13417
13418   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
13419   if (retval)
13420     call = gen_rtx_SET (VOIDmode, retval, call);
13421   if (pop)
13422     {
13423       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
13424       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
13425       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
13426     }
13427
13428   call = emit_call_insn (call);
13429   if (use)
13430     CALL_INSN_FUNCTION_USAGE (call) = use;
13431 }
13432
13433 \f
13434 /* Clear stack slot assignments remembered from previous functions.
13435    This is called from INIT_EXPANDERS once before RTL is emitted for each
13436    function.  */
13437
13438 static struct machine_function *
13439 ix86_init_machine_status (void)
13440 {
13441   struct machine_function *f;
13442
13443   f = ggc_alloc_cleared (sizeof (struct machine_function));
13444   f->use_fast_prologue_epilogue_nregs = -1;
13445   f->tls_descriptor_call_expanded_p = 0;
13446
13447   return f;
13448 }
13449
13450 /* Return a MEM corresponding to a stack slot with mode MODE.
13451    Allocate a new slot if necessary.
13452
13453    The RTL for a function can have several slots available: N is
13454    which slot to use.  */
13455
13456 rtx
13457 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
13458 {
13459   struct stack_local_entry *s;
13460
13461   gcc_assert (n < MAX_386_STACK_LOCALS);
13462
13463   for (s = ix86_stack_locals; s; s = s->next)
13464     if (s->mode == mode && s->n == n)
13465       return s->rtl;
13466
13467   s = (struct stack_local_entry *)
13468     ggc_alloc (sizeof (struct stack_local_entry));
13469   s->n = n;
13470   s->mode = mode;
13471   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13472
13473   s->next = ix86_stack_locals;
13474   ix86_stack_locals = s;
13475   return s->rtl;
13476 }
13477
13478 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
13479
13480 static GTY(()) rtx ix86_tls_symbol;
13481 rtx
13482 ix86_tls_get_addr (void)
13483 {
13484
13485   if (!ix86_tls_symbol)
13486     {
13487       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
13488                                             (TARGET_ANY_GNU_TLS
13489                                              && !TARGET_64BIT)
13490                                             ? "___tls_get_addr"
13491                                             : "__tls_get_addr");
13492     }
13493
13494   return ix86_tls_symbol;
13495 }
13496
13497 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
13498
13499 static GTY(()) rtx ix86_tls_module_base_symbol;
13500 rtx
13501 ix86_tls_module_base (void)
13502 {
13503
13504   if (!ix86_tls_module_base_symbol)
13505     {
13506       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
13507                                                         "_TLS_MODULE_BASE_");
13508       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
13509         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
13510     }
13511
13512   return ix86_tls_module_base_symbol;
13513 }
13514 \f
13515 /* Calculate the length of the memory address in the instruction
13516    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
13517
13518 int
13519 memory_address_length (rtx addr)
13520 {
13521   struct ix86_address parts;
13522   rtx base, index, disp;
13523   int len;
13524   int ok;
13525
13526   if (GET_CODE (addr) == PRE_DEC
13527       || GET_CODE (addr) == POST_INC
13528       || GET_CODE (addr) == PRE_MODIFY
13529       || GET_CODE (addr) == POST_MODIFY)
13530     return 0;
13531
13532   ok = ix86_decompose_address (addr, &parts);
13533   gcc_assert (ok);
13534
13535   if (parts.base && GET_CODE (parts.base) == SUBREG)
13536     parts.base = SUBREG_REG (parts.base);
13537   if (parts.index && GET_CODE (parts.index) == SUBREG)
13538     parts.index = SUBREG_REG (parts.index);
13539
13540   base = parts.base;
13541   index = parts.index;
13542   disp = parts.disp;
13543   len = 0;
13544
13545   /* Rule of thumb:
13546        - esp as the base always wants an index,
13547        - ebp as the base always wants a displacement.  */
13548
13549   /* Register Indirect.  */
13550   if (base && !index && !disp)
13551     {
13552       /* esp (for its index) and ebp (for its displacement) need
13553          the two-byte modrm form.  */
13554       if (addr == stack_pointer_rtx
13555           || addr == arg_pointer_rtx
13556           || addr == frame_pointer_rtx
13557           || addr == hard_frame_pointer_rtx)
13558         len = 1;
13559     }
13560
13561   /* Direct Addressing.  */
13562   else if (disp && !base && !index)
13563     len = 4;
13564
13565   else
13566     {
13567       /* Find the length of the displacement constant.  */
13568       if (disp)
13569         {
13570           if (base && satisfies_constraint_K (disp))
13571             len = 1;
13572           else
13573             len = 4;
13574         }
13575       /* ebp always wants a displacement.  */
13576       else if (base == hard_frame_pointer_rtx)
13577         len = 1;
13578
13579       /* An index requires the two-byte modrm form....  */
13580       if (index
13581           /* ...like esp, which always wants an index.  */
13582           || base == stack_pointer_rtx
13583           || base == arg_pointer_rtx
13584           || base == frame_pointer_rtx)
13585         len += 1;
13586     }
13587
13588   return len;
13589 }
13590
13591 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
13592    is set, expect that insn have 8bit immediate alternative.  */
13593 int
13594 ix86_attr_length_immediate_default (rtx insn, int shortform)
13595 {
13596   int len = 0;
13597   int i;
13598   extract_insn_cached (insn);
13599   for (i = recog_data.n_operands - 1; i >= 0; --i)
13600     if (CONSTANT_P (recog_data.operand[i]))
13601       {
13602         gcc_assert (!len);
13603         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
13604           len = 1;
13605         else
13606           {
13607             switch (get_attr_mode (insn))
13608               {
13609                 case MODE_QI:
13610                   len+=1;
13611                   break;
13612                 case MODE_HI:
13613                   len+=2;
13614                   break;
13615                 case MODE_SI:
13616                   len+=4;
13617                   break;
13618                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
13619                 case MODE_DI:
13620                   len+=4;
13621                   break;
13622                 default:
13623                   fatal_insn ("unknown insn mode", insn);
13624               }
13625           }
13626       }
13627   return len;
13628 }
13629 /* Compute default value for "length_address" attribute.  */
13630 int
13631 ix86_attr_length_address_default (rtx insn)
13632 {
13633   int i;
13634
13635   if (get_attr_type (insn) == TYPE_LEA)
13636     {
13637       rtx set = PATTERN (insn);
13638
13639       if (GET_CODE (set) == PARALLEL)
13640         set = XVECEXP (set, 0, 0);
13641
13642       gcc_assert (GET_CODE (set) == SET);
13643
13644       return memory_address_length (SET_SRC (set));
13645     }
13646
13647   extract_insn_cached (insn);
13648   for (i = recog_data.n_operands - 1; i >= 0; --i)
13649     if (GET_CODE (recog_data.operand[i]) == MEM)
13650       {
13651         return memory_address_length (XEXP (recog_data.operand[i], 0));
13652         break;
13653       }
13654   return 0;
13655 }
13656 \f
13657 /* Return the maximum number of instructions a cpu can issue.  */
13658
13659 static int
13660 ix86_issue_rate (void)
13661 {
13662   switch (ix86_tune)
13663     {
13664     case PROCESSOR_PENTIUM:
13665     case PROCESSOR_K6:
13666       return 2;
13667
13668     case PROCESSOR_PENTIUMPRO:
13669     case PROCESSOR_PENTIUM4:
13670     case PROCESSOR_ATHLON:
13671     case PROCESSOR_K8:
13672     case PROCESSOR_NOCONA:
13673     case PROCESSOR_GENERIC32:
13674     case PROCESSOR_GENERIC64:
13675       return 3;
13676
13677     default:
13678       return 1;
13679     }
13680 }
13681
13682 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
13683    by DEP_INSN and nothing set by DEP_INSN.  */
13684
13685 static int
13686 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
13687 {
13688   rtx set, set2;
13689
13690   /* Simplify the test for uninteresting insns.  */
13691   if (insn_type != TYPE_SETCC
13692       && insn_type != TYPE_ICMOV
13693       && insn_type != TYPE_FCMOV
13694       && insn_type != TYPE_IBR)
13695     return 0;
13696
13697   if ((set = single_set (dep_insn)) != 0)
13698     {
13699       set = SET_DEST (set);
13700       set2 = NULL_RTX;
13701     }
13702   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
13703            && XVECLEN (PATTERN (dep_insn), 0) == 2
13704            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
13705            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
13706     {
13707       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13708       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13709     }
13710   else
13711     return 0;
13712
13713   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
13714     return 0;
13715
13716   /* This test is true if the dependent insn reads the flags but
13717      not any other potentially set register.  */
13718   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
13719     return 0;
13720
13721   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
13722     return 0;
13723
13724   return 1;
13725 }
13726
13727 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
13728    address with operands set by DEP_INSN.  */
13729
13730 static int
13731 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
13732 {
13733   rtx addr;
13734
13735   if (insn_type == TYPE_LEA
13736       && TARGET_PENTIUM)
13737     {
13738       addr = PATTERN (insn);
13739
13740       if (GET_CODE (addr) == PARALLEL)
13741         addr = XVECEXP (addr, 0, 0);
13742
13743       gcc_assert (GET_CODE (addr) == SET);
13744
13745       addr = SET_SRC (addr);
13746     }
13747   else
13748     {
13749       int i;
13750       extract_insn_cached (insn);
13751       for (i = recog_data.n_operands - 1; i >= 0; --i)
13752         if (GET_CODE (recog_data.operand[i]) == MEM)
13753           {
13754             addr = XEXP (recog_data.operand[i], 0);
13755             goto found;
13756           }
13757       return 0;
13758     found:;
13759     }
13760
13761   return modified_in_p (addr, dep_insn);
13762 }
13763
13764 static int
13765 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
13766 {
13767   enum attr_type insn_type, dep_insn_type;
13768   enum attr_memory memory;
13769   rtx set, set2;
13770   int dep_insn_code_number;
13771
13772   /* Anti and output dependencies have zero cost on all CPUs.  */
13773   if (REG_NOTE_KIND (link) != 0)
13774     return 0;
13775
13776   dep_insn_code_number = recog_memoized (dep_insn);
13777
13778   /* If we can't recognize the insns, we can't really do anything.  */
13779   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
13780     return cost;
13781
13782   insn_type = get_attr_type (insn);
13783   dep_insn_type = get_attr_type (dep_insn);
13784
13785   switch (ix86_tune)
13786     {
13787     case PROCESSOR_PENTIUM:
13788       /* Address Generation Interlock adds a cycle of latency.  */
13789       if (ix86_agi_dependent (insn, dep_insn, insn_type))
13790         cost += 1;
13791
13792       /* ??? Compares pair with jump/setcc.  */
13793       if (ix86_flags_dependent (insn, dep_insn, insn_type))
13794         cost = 0;
13795
13796       /* Floating point stores require value to be ready one cycle earlier.  */
13797       if (insn_type == TYPE_FMOV
13798           && get_attr_memory (insn) == MEMORY_STORE
13799           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13800         cost += 1;
13801       break;
13802
13803     case PROCESSOR_PENTIUMPRO:
13804       memory = get_attr_memory (insn);
13805
13806       /* INT->FP conversion is expensive.  */
13807       if (get_attr_fp_int_src (dep_insn))
13808         cost += 5;
13809
13810       /* There is one cycle extra latency between an FP op and a store.  */
13811       if (insn_type == TYPE_FMOV
13812           && (set = single_set (dep_insn)) != NULL_RTX
13813           && (set2 = single_set (insn)) != NULL_RTX
13814           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
13815           && GET_CODE (SET_DEST (set2)) == MEM)
13816         cost += 1;
13817
13818       /* Show ability of reorder buffer to hide latency of load by executing
13819          in parallel with previous instruction in case
13820          previous instruction is not needed to compute the address.  */
13821       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13822           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13823         {
13824           /* Claim moves to take one cycle, as core can issue one load
13825              at time and the next load can start cycle later.  */
13826           if (dep_insn_type == TYPE_IMOV
13827               || dep_insn_type == TYPE_FMOV)
13828             cost = 1;
13829           else if (cost > 1)
13830             cost--;
13831         }
13832       break;
13833
13834     case PROCESSOR_K6:
13835       memory = get_attr_memory (insn);
13836
13837       /* The esp dependency is resolved before the instruction is really
13838          finished.  */
13839       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
13840           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
13841         return 1;
13842
13843       /* INT->FP conversion is expensive.  */
13844       if (get_attr_fp_int_src (dep_insn))
13845         cost += 5;
13846
13847       /* Show ability of reorder buffer to hide latency of load by executing
13848          in parallel with previous instruction in case
13849          previous instruction is not needed to compute the address.  */
13850       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13851           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13852         {
13853           /* Claim moves to take one cycle, as core can issue one load
13854              at time and the next load can start cycle later.  */
13855           if (dep_insn_type == TYPE_IMOV
13856               || dep_insn_type == TYPE_FMOV)
13857             cost = 1;
13858           else if (cost > 2)
13859             cost -= 2;
13860           else
13861             cost = 1;
13862         }
13863       break;
13864
13865     case PROCESSOR_ATHLON:
13866     case PROCESSOR_K8:
13867     case PROCESSOR_GENERIC32:
13868     case PROCESSOR_GENERIC64:
13869       memory = get_attr_memory (insn);
13870
13871       /* Show ability of reorder buffer to hide latency of load by executing
13872          in parallel with previous instruction in case
13873          previous instruction is not needed to compute the address.  */
13874       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13875           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13876         {
13877           enum attr_unit unit = get_attr_unit (insn);
13878           int loadcost = 3;
13879
13880           /* Because of the difference between the length of integer and
13881              floating unit pipeline preparation stages, the memory operands
13882              for floating point are cheaper.
13883
13884              ??? For Athlon it the difference is most probably 2.  */
13885           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
13886             loadcost = 3;
13887           else
13888             loadcost = TARGET_ATHLON ? 2 : 0;
13889
13890           if (cost >= loadcost)
13891             cost -= loadcost;
13892           else
13893             cost = 0;
13894         }
13895
13896     default:
13897       break;
13898     }
13899
13900   return cost;
13901 }
13902
13903 /* How many alternative schedules to try.  This should be as wide as the
13904    scheduling freedom in the DFA, but no wider.  Making this value too
13905    large results extra work for the scheduler.  */
13906
13907 static int
13908 ia32_multipass_dfa_lookahead (void)
13909 {
13910   if (ix86_tune == PROCESSOR_PENTIUM)
13911     return 2;
13912
13913   if (ix86_tune == PROCESSOR_PENTIUMPRO
13914       || ix86_tune == PROCESSOR_K6)
13915     return 1;
13916
13917   else
13918     return 0;
13919 }
13920
13921 \f
13922 /* Compute the alignment given to a constant that is being placed in memory.
13923    EXP is the constant and ALIGN is the alignment that the object would
13924    ordinarily have.
13925    The value of this function is used instead of that alignment to align
13926    the object.  */
13927
13928 int
13929 ix86_constant_alignment (tree exp, int align)
13930 {
13931   if (TREE_CODE (exp) == REAL_CST)
13932     {
13933       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
13934         return 64;
13935       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
13936         return 128;
13937     }
13938   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
13939            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
13940     return BITS_PER_WORD;
13941
13942   return align;
13943 }
13944
13945 /* Compute the alignment for a static variable.
13946    TYPE is the data type, and ALIGN is the alignment that
13947    the object would ordinarily have.  The value of this function is used
13948    instead of that alignment to align the object.  */
13949
13950 int
13951 ix86_data_alignment (tree type, int align)
13952 {
13953   int max_align = optimize_size ? BITS_PER_WORD : 256;
13954
13955   if (AGGREGATE_TYPE_P (type)
13956       && TYPE_SIZE (type)
13957       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13958       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
13959           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
13960       && align < max_align)
13961     align = max_align;
13962
13963   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13964      to 16byte boundary.  */
13965   if (TARGET_64BIT)
13966     {
13967       if (AGGREGATE_TYPE_P (type)
13968            && TYPE_SIZE (type)
13969            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13970            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
13971                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13972         return 128;
13973     }
13974
13975   if (TREE_CODE (type) == ARRAY_TYPE)
13976     {
13977       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13978         return 64;
13979       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13980         return 128;
13981     }
13982   else if (TREE_CODE (type) == COMPLEX_TYPE)
13983     {
13984
13985       if (TYPE_MODE (type) == DCmode && align < 64)
13986         return 64;
13987       if (TYPE_MODE (type) == XCmode && align < 128)
13988         return 128;
13989     }
13990   else if ((TREE_CODE (type) == RECORD_TYPE
13991             || TREE_CODE (type) == UNION_TYPE
13992             || TREE_CODE (type) == QUAL_UNION_TYPE)
13993            && TYPE_FIELDS (type))
13994     {
13995       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13996         return 64;
13997       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13998         return 128;
13999     }
14000   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
14001            || TREE_CODE (type) == INTEGER_TYPE)
14002     {
14003       if (TYPE_MODE (type) == DFmode && align < 64)
14004         return 64;
14005       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
14006         return 128;
14007     }
14008
14009   return align;
14010 }
14011
14012 /* Compute the alignment for a local variable.
14013    TYPE is the data type, and ALIGN is the alignment that
14014    the object would ordinarily have.  The value of this macro is used
14015    instead of that alignment to align the object.  */
14016
14017 int
14018 ix86_local_alignment (tree type, int align)
14019 {
14020   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
14021      to 16byte boundary.  */
14022   if (TARGET_64BIT)
14023     {
14024       if (AGGREGATE_TYPE_P (type)
14025            && TYPE_SIZE (type)
14026            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
14027            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
14028                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
14029         return 128;
14030     }
14031   if (TREE_CODE (type) == ARRAY_TYPE)
14032     {
14033       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
14034         return 64;
14035       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
14036         return 128;
14037     }
14038   else if (TREE_CODE (type) == COMPLEX_TYPE)
14039     {
14040       if (TYPE_MODE (type) == DCmode && align < 64)
14041         return 64;
14042       if (TYPE_MODE (type) == XCmode && align < 128)
14043         return 128;
14044     }
14045   else if ((TREE_CODE (type) == RECORD_TYPE
14046             || TREE_CODE (type) == UNION_TYPE
14047             || TREE_CODE (type) == QUAL_UNION_TYPE)
14048            && TYPE_FIELDS (type))
14049     {
14050       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
14051         return 64;
14052       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
14053         return 128;
14054     }
14055   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
14056            || TREE_CODE (type) == INTEGER_TYPE)
14057     {
14058
14059       if (TYPE_MODE (type) == DFmode && align < 64)
14060         return 64;
14061       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
14062         return 128;
14063     }
14064   return align;
14065 }
14066 \f
14067 /* Emit RTL insns to initialize the variable parts of a trampoline.
14068    FNADDR is an RTX for the address of the function's pure code.
14069    CXT is an RTX for the static chain value for the function.  */
14070 void
14071 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
14072 {
14073   if (!TARGET_64BIT)
14074     {
14075       /* Compute offset from the end of the jmp to the target function.  */
14076       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
14077                                plus_constant (tramp, 10),
14078                                NULL_RTX, 1, OPTAB_DIRECT);
14079       emit_move_insn (gen_rtx_MEM (QImode, tramp),
14080                       gen_int_mode (0xb9, QImode));
14081       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
14082       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
14083                       gen_int_mode (0xe9, QImode));
14084       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
14085     }
14086   else
14087     {
14088       int offset = 0;
14089       /* Try to load address using shorter movl instead of movabs.
14090          We may want to support movq for kernel mode, but kernel does not use
14091          trampolines at the moment.  */
14092       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
14093         {
14094           fnaddr = copy_to_mode_reg (DImode, fnaddr);
14095           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14096                           gen_int_mode (0xbb41, HImode));
14097           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
14098                           gen_lowpart (SImode, fnaddr));
14099           offset += 6;
14100         }
14101       else
14102         {
14103           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14104                           gen_int_mode (0xbb49, HImode));
14105           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
14106                           fnaddr);
14107           offset += 10;
14108         }
14109       /* Load static chain using movabs to r10.  */
14110       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14111                       gen_int_mode (0xba49, HImode));
14112       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
14113                       cxt);
14114       offset += 10;
14115       /* Jump to the r11 */
14116       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14117                       gen_int_mode (0xff49, HImode));
14118       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
14119                       gen_int_mode (0xe3, QImode));
14120       offset += 3;
14121       gcc_assert (offset <= TRAMPOLINE_SIZE);
14122     }
14123
14124 #ifdef ENABLE_EXECUTE_STACK
14125   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
14126                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
14127 #endif
14128 }
14129 \f
14130 /* Codes for all the SSE/MMX builtins.  */
14131 enum ix86_builtins
14132 {
14133   IX86_BUILTIN_ADDPS,
14134   IX86_BUILTIN_ADDSS,
14135   IX86_BUILTIN_DIVPS,
14136   IX86_BUILTIN_DIVSS,
14137   IX86_BUILTIN_MULPS,
14138   IX86_BUILTIN_MULSS,
14139   IX86_BUILTIN_SUBPS,
14140   IX86_BUILTIN_SUBSS,
14141
14142   IX86_BUILTIN_CMPEQPS,
14143   IX86_BUILTIN_CMPLTPS,
14144   IX86_BUILTIN_CMPLEPS,
14145   IX86_BUILTIN_CMPGTPS,
14146   IX86_BUILTIN_CMPGEPS,
14147   IX86_BUILTIN_CMPNEQPS,
14148   IX86_BUILTIN_CMPNLTPS,
14149   IX86_BUILTIN_CMPNLEPS,
14150   IX86_BUILTIN_CMPNGTPS,
14151   IX86_BUILTIN_CMPNGEPS,
14152   IX86_BUILTIN_CMPORDPS,
14153   IX86_BUILTIN_CMPUNORDPS,
14154   IX86_BUILTIN_CMPEQSS,
14155   IX86_BUILTIN_CMPLTSS,
14156   IX86_BUILTIN_CMPLESS,
14157   IX86_BUILTIN_CMPNEQSS,
14158   IX86_BUILTIN_CMPNLTSS,
14159   IX86_BUILTIN_CMPNLESS,
14160   IX86_BUILTIN_CMPNGTSS,
14161   IX86_BUILTIN_CMPNGESS,
14162   IX86_BUILTIN_CMPORDSS,
14163   IX86_BUILTIN_CMPUNORDSS,
14164
14165   IX86_BUILTIN_COMIEQSS,
14166   IX86_BUILTIN_COMILTSS,
14167   IX86_BUILTIN_COMILESS,
14168   IX86_BUILTIN_COMIGTSS,
14169   IX86_BUILTIN_COMIGESS,
14170   IX86_BUILTIN_COMINEQSS,
14171   IX86_BUILTIN_UCOMIEQSS,
14172   IX86_BUILTIN_UCOMILTSS,
14173   IX86_BUILTIN_UCOMILESS,
14174   IX86_BUILTIN_UCOMIGTSS,
14175   IX86_BUILTIN_UCOMIGESS,
14176   IX86_BUILTIN_UCOMINEQSS,
14177
14178   IX86_BUILTIN_CVTPI2PS,
14179   IX86_BUILTIN_CVTPS2PI,
14180   IX86_BUILTIN_CVTSI2SS,
14181   IX86_BUILTIN_CVTSI642SS,
14182   IX86_BUILTIN_CVTSS2SI,
14183   IX86_BUILTIN_CVTSS2SI64,
14184   IX86_BUILTIN_CVTTPS2PI,
14185   IX86_BUILTIN_CVTTSS2SI,
14186   IX86_BUILTIN_CVTTSS2SI64,
14187
14188   IX86_BUILTIN_MAXPS,
14189   IX86_BUILTIN_MAXSS,
14190   IX86_BUILTIN_MINPS,
14191   IX86_BUILTIN_MINSS,
14192
14193   IX86_BUILTIN_LOADUPS,
14194   IX86_BUILTIN_STOREUPS,
14195   IX86_BUILTIN_MOVSS,
14196
14197   IX86_BUILTIN_MOVHLPS,
14198   IX86_BUILTIN_MOVLHPS,
14199   IX86_BUILTIN_LOADHPS,
14200   IX86_BUILTIN_LOADLPS,
14201   IX86_BUILTIN_STOREHPS,
14202   IX86_BUILTIN_STORELPS,
14203
14204   IX86_BUILTIN_MASKMOVQ,
14205   IX86_BUILTIN_MOVMSKPS,
14206   IX86_BUILTIN_PMOVMSKB,
14207
14208   IX86_BUILTIN_MOVNTPS,
14209   IX86_BUILTIN_MOVNTQ,
14210
14211   IX86_BUILTIN_LOADDQU,
14212   IX86_BUILTIN_STOREDQU,
14213
14214   IX86_BUILTIN_PACKSSWB,
14215   IX86_BUILTIN_PACKSSDW,
14216   IX86_BUILTIN_PACKUSWB,
14217
14218   IX86_BUILTIN_PADDB,
14219   IX86_BUILTIN_PADDW,
14220   IX86_BUILTIN_PADDD,
14221   IX86_BUILTIN_PADDQ,
14222   IX86_BUILTIN_PADDSB,
14223   IX86_BUILTIN_PADDSW,
14224   IX86_BUILTIN_PADDUSB,
14225   IX86_BUILTIN_PADDUSW,
14226   IX86_BUILTIN_PSUBB,
14227   IX86_BUILTIN_PSUBW,
14228   IX86_BUILTIN_PSUBD,
14229   IX86_BUILTIN_PSUBQ,
14230   IX86_BUILTIN_PSUBSB,
14231   IX86_BUILTIN_PSUBSW,
14232   IX86_BUILTIN_PSUBUSB,
14233   IX86_BUILTIN_PSUBUSW,
14234
14235   IX86_BUILTIN_PAND,
14236   IX86_BUILTIN_PANDN,
14237   IX86_BUILTIN_POR,
14238   IX86_BUILTIN_PXOR,
14239
14240   IX86_BUILTIN_PAVGB,
14241   IX86_BUILTIN_PAVGW,
14242
14243   IX86_BUILTIN_PCMPEQB,
14244   IX86_BUILTIN_PCMPEQW,
14245   IX86_BUILTIN_PCMPEQD,
14246   IX86_BUILTIN_PCMPGTB,
14247   IX86_BUILTIN_PCMPGTW,
14248   IX86_BUILTIN_PCMPGTD,
14249
14250   IX86_BUILTIN_PMADDWD,
14251
14252   IX86_BUILTIN_PMAXSW,
14253   IX86_BUILTIN_PMAXUB,
14254   IX86_BUILTIN_PMINSW,
14255   IX86_BUILTIN_PMINUB,
14256
14257   IX86_BUILTIN_PMULHUW,
14258   IX86_BUILTIN_PMULHW,
14259   IX86_BUILTIN_PMULLW,
14260
14261   IX86_BUILTIN_PSADBW,
14262   IX86_BUILTIN_PSHUFW,
14263
14264   IX86_BUILTIN_PSLLW,
14265   IX86_BUILTIN_PSLLD,
14266   IX86_BUILTIN_PSLLQ,
14267   IX86_BUILTIN_PSRAW,
14268   IX86_BUILTIN_PSRAD,
14269   IX86_BUILTIN_PSRLW,
14270   IX86_BUILTIN_PSRLD,
14271   IX86_BUILTIN_PSRLQ,
14272   IX86_BUILTIN_PSLLWI,
14273   IX86_BUILTIN_PSLLDI,
14274   IX86_BUILTIN_PSLLQI,
14275   IX86_BUILTIN_PSRAWI,
14276   IX86_BUILTIN_PSRADI,
14277   IX86_BUILTIN_PSRLWI,
14278   IX86_BUILTIN_PSRLDI,
14279   IX86_BUILTIN_PSRLQI,
14280
14281   IX86_BUILTIN_PUNPCKHBW,
14282   IX86_BUILTIN_PUNPCKHWD,
14283   IX86_BUILTIN_PUNPCKHDQ,
14284   IX86_BUILTIN_PUNPCKLBW,
14285   IX86_BUILTIN_PUNPCKLWD,
14286   IX86_BUILTIN_PUNPCKLDQ,
14287
14288   IX86_BUILTIN_SHUFPS,
14289
14290   IX86_BUILTIN_RCPPS,
14291   IX86_BUILTIN_RCPSS,
14292   IX86_BUILTIN_RSQRTPS,
14293   IX86_BUILTIN_RSQRTSS,
14294   IX86_BUILTIN_SQRTPS,
14295   IX86_BUILTIN_SQRTSS,
14296
14297   IX86_BUILTIN_UNPCKHPS,
14298   IX86_BUILTIN_UNPCKLPS,
14299
14300   IX86_BUILTIN_ANDPS,
14301   IX86_BUILTIN_ANDNPS,
14302   IX86_BUILTIN_ORPS,
14303   IX86_BUILTIN_XORPS,
14304
14305   IX86_BUILTIN_EMMS,
14306   IX86_BUILTIN_LDMXCSR,
14307   IX86_BUILTIN_STMXCSR,
14308   IX86_BUILTIN_SFENCE,
14309
14310   /* 3DNow! Original */
14311   IX86_BUILTIN_FEMMS,
14312   IX86_BUILTIN_PAVGUSB,
14313   IX86_BUILTIN_PF2ID,
14314   IX86_BUILTIN_PFACC,
14315   IX86_BUILTIN_PFADD,
14316   IX86_BUILTIN_PFCMPEQ,
14317   IX86_BUILTIN_PFCMPGE,
14318   IX86_BUILTIN_PFCMPGT,
14319   IX86_BUILTIN_PFMAX,
14320   IX86_BUILTIN_PFMIN,
14321   IX86_BUILTIN_PFMUL,
14322   IX86_BUILTIN_PFRCP,
14323   IX86_BUILTIN_PFRCPIT1,
14324   IX86_BUILTIN_PFRCPIT2,
14325   IX86_BUILTIN_PFRSQIT1,
14326   IX86_BUILTIN_PFRSQRT,
14327   IX86_BUILTIN_PFSUB,
14328   IX86_BUILTIN_PFSUBR,
14329   IX86_BUILTIN_PI2FD,
14330   IX86_BUILTIN_PMULHRW,
14331
14332   /* 3DNow! Athlon Extensions */
14333   IX86_BUILTIN_PF2IW,
14334   IX86_BUILTIN_PFNACC,
14335   IX86_BUILTIN_PFPNACC,
14336   IX86_BUILTIN_PI2FW,
14337   IX86_BUILTIN_PSWAPDSI,
14338   IX86_BUILTIN_PSWAPDSF,
14339
14340   /* SSE2 */
14341   IX86_BUILTIN_ADDPD,
14342   IX86_BUILTIN_ADDSD,
14343   IX86_BUILTIN_DIVPD,
14344   IX86_BUILTIN_DIVSD,
14345   IX86_BUILTIN_MULPD,
14346   IX86_BUILTIN_MULSD,
14347   IX86_BUILTIN_SUBPD,
14348   IX86_BUILTIN_SUBSD,
14349
14350   IX86_BUILTIN_CMPEQPD,
14351   IX86_BUILTIN_CMPLTPD,
14352   IX86_BUILTIN_CMPLEPD,
14353   IX86_BUILTIN_CMPGTPD,
14354   IX86_BUILTIN_CMPGEPD,
14355   IX86_BUILTIN_CMPNEQPD,
14356   IX86_BUILTIN_CMPNLTPD,
14357   IX86_BUILTIN_CMPNLEPD,
14358   IX86_BUILTIN_CMPNGTPD,
14359   IX86_BUILTIN_CMPNGEPD,
14360   IX86_BUILTIN_CMPORDPD,
14361   IX86_BUILTIN_CMPUNORDPD,
14362   IX86_BUILTIN_CMPNEPD,
14363   IX86_BUILTIN_CMPEQSD,
14364   IX86_BUILTIN_CMPLTSD,
14365   IX86_BUILTIN_CMPLESD,
14366   IX86_BUILTIN_CMPNEQSD,
14367   IX86_BUILTIN_CMPNLTSD,
14368   IX86_BUILTIN_CMPNLESD,
14369   IX86_BUILTIN_CMPORDSD,
14370   IX86_BUILTIN_CMPUNORDSD,
14371   IX86_BUILTIN_CMPNESD,
14372
14373   IX86_BUILTIN_COMIEQSD,
14374   IX86_BUILTIN_COMILTSD,
14375   IX86_BUILTIN_COMILESD,
14376   IX86_BUILTIN_COMIGTSD,
14377   IX86_BUILTIN_COMIGESD,
14378   IX86_BUILTIN_COMINEQSD,
14379   IX86_BUILTIN_UCOMIEQSD,
14380   IX86_BUILTIN_UCOMILTSD,
14381   IX86_BUILTIN_UCOMILESD,
14382   IX86_BUILTIN_UCOMIGTSD,
14383   IX86_BUILTIN_UCOMIGESD,
14384   IX86_BUILTIN_UCOMINEQSD,
14385
14386   IX86_BUILTIN_MAXPD,
14387   IX86_BUILTIN_MAXSD,
14388   IX86_BUILTIN_MINPD,
14389   IX86_BUILTIN_MINSD,
14390
14391   IX86_BUILTIN_ANDPD,
14392   IX86_BUILTIN_ANDNPD,
14393   IX86_BUILTIN_ORPD,
14394   IX86_BUILTIN_XORPD,
14395
14396   IX86_BUILTIN_SQRTPD,
14397   IX86_BUILTIN_SQRTSD,
14398
14399   IX86_BUILTIN_UNPCKHPD,
14400   IX86_BUILTIN_UNPCKLPD,
14401
14402   IX86_BUILTIN_SHUFPD,
14403
14404   IX86_BUILTIN_LOADUPD,
14405   IX86_BUILTIN_STOREUPD,
14406   IX86_BUILTIN_MOVSD,
14407
14408   IX86_BUILTIN_LOADHPD,
14409   IX86_BUILTIN_LOADLPD,
14410
14411   IX86_BUILTIN_CVTDQ2PD,
14412   IX86_BUILTIN_CVTDQ2PS,
14413
14414   IX86_BUILTIN_CVTPD2DQ,
14415   IX86_BUILTIN_CVTPD2PI,
14416   IX86_BUILTIN_CVTPD2PS,
14417   IX86_BUILTIN_CVTTPD2DQ,
14418   IX86_BUILTIN_CVTTPD2PI,
14419
14420   IX86_BUILTIN_CVTPI2PD,
14421   IX86_BUILTIN_CVTSI2SD,
14422   IX86_BUILTIN_CVTSI642SD,
14423
14424   IX86_BUILTIN_CVTSD2SI,
14425   IX86_BUILTIN_CVTSD2SI64,
14426   IX86_BUILTIN_CVTSD2SS,
14427   IX86_BUILTIN_CVTSS2SD,
14428   IX86_BUILTIN_CVTTSD2SI,
14429   IX86_BUILTIN_CVTTSD2SI64,
14430
14431   IX86_BUILTIN_CVTPS2DQ,
14432   IX86_BUILTIN_CVTPS2PD,
14433   IX86_BUILTIN_CVTTPS2DQ,
14434
14435   IX86_BUILTIN_MOVNTI,
14436   IX86_BUILTIN_MOVNTPD,
14437   IX86_BUILTIN_MOVNTDQ,
14438
14439   /* SSE2 MMX */
14440   IX86_BUILTIN_MASKMOVDQU,
14441   IX86_BUILTIN_MOVMSKPD,
14442   IX86_BUILTIN_PMOVMSKB128,
14443
14444   IX86_BUILTIN_PACKSSWB128,
14445   IX86_BUILTIN_PACKSSDW128,
14446   IX86_BUILTIN_PACKUSWB128,
14447
14448   IX86_BUILTIN_PADDB128,
14449   IX86_BUILTIN_PADDW128,
14450   IX86_BUILTIN_PADDD128,
14451   IX86_BUILTIN_PADDQ128,
14452   IX86_BUILTIN_PADDSB128,
14453   IX86_BUILTIN_PADDSW128,
14454   IX86_BUILTIN_PADDUSB128,
14455   IX86_BUILTIN_PADDUSW128,
14456   IX86_BUILTIN_PSUBB128,
14457   IX86_BUILTIN_PSUBW128,
14458   IX86_BUILTIN_PSUBD128,
14459   IX86_BUILTIN_PSUBQ128,
14460   IX86_BUILTIN_PSUBSB128,
14461   IX86_BUILTIN_PSUBSW128,
14462   IX86_BUILTIN_PSUBUSB128,
14463   IX86_BUILTIN_PSUBUSW128,
14464
14465   IX86_BUILTIN_PAND128,
14466   IX86_BUILTIN_PANDN128,
14467   IX86_BUILTIN_POR128,
14468   IX86_BUILTIN_PXOR128,
14469
14470   IX86_BUILTIN_PAVGB128,
14471   IX86_BUILTIN_PAVGW128,
14472
14473   IX86_BUILTIN_PCMPEQB128,
14474   IX86_BUILTIN_PCMPEQW128,
14475   IX86_BUILTIN_PCMPEQD128,
14476   IX86_BUILTIN_PCMPGTB128,
14477   IX86_BUILTIN_PCMPGTW128,
14478   IX86_BUILTIN_PCMPGTD128,
14479
14480   IX86_BUILTIN_PMADDWD128,
14481
14482   IX86_BUILTIN_PMAXSW128,
14483   IX86_BUILTIN_PMAXUB128,
14484   IX86_BUILTIN_PMINSW128,
14485   IX86_BUILTIN_PMINUB128,
14486
14487   IX86_BUILTIN_PMULUDQ,
14488   IX86_BUILTIN_PMULUDQ128,
14489   IX86_BUILTIN_PMULHUW128,
14490   IX86_BUILTIN_PMULHW128,
14491   IX86_BUILTIN_PMULLW128,
14492
14493   IX86_BUILTIN_PSADBW128,
14494   IX86_BUILTIN_PSHUFHW,
14495   IX86_BUILTIN_PSHUFLW,
14496   IX86_BUILTIN_PSHUFD,
14497
14498   IX86_BUILTIN_PSLLW128,
14499   IX86_BUILTIN_PSLLD128,
14500   IX86_BUILTIN_PSLLQ128,
14501   IX86_BUILTIN_PSRAW128,
14502   IX86_BUILTIN_PSRAD128,
14503   IX86_BUILTIN_PSRLW128,
14504   IX86_BUILTIN_PSRLD128,
14505   IX86_BUILTIN_PSRLQ128,
14506   IX86_BUILTIN_PSLLDQI128,
14507   IX86_BUILTIN_PSLLWI128,
14508   IX86_BUILTIN_PSLLDI128,
14509   IX86_BUILTIN_PSLLQI128,
14510   IX86_BUILTIN_PSRAWI128,
14511   IX86_BUILTIN_PSRADI128,
14512   IX86_BUILTIN_PSRLDQI128,
14513   IX86_BUILTIN_PSRLWI128,
14514   IX86_BUILTIN_PSRLDI128,
14515   IX86_BUILTIN_PSRLQI128,
14516
14517   IX86_BUILTIN_PUNPCKHBW128,
14518   IX86_BUILTIN_PUNPCKHWD128,
14519   IX86_BUILTIN_PUNPCKHDQ128,
14520   IX86_BUILTIN_PUNPCKHQDQ128,
14521   IX86_BUILTIN_PUNPCKLBW128,
14522   IX86_BUILTIN_PUNPCKLWD128,
14523   IX86_BUILTIN_PUNPCKLDQ128,
14524   IX86_BUILTIN_PUNPCKLQDQ128,
14525
14526   IX86_BUILTIN_CLFLUSH,
14527   IX86_BUILTIN_MFENCE,
14528   IX86_BUILTIN_LFENCE,
14529
14530   /* Prescott New Instructions.  */
14531   IX86_BUILTIN_ADDSUBPS,
14532   IX86_BUILTIN_HADDPS,
14533   IX86_BUILTIN_HSUBPS,
14534   IX86_BUILTIN_MOVSHDUP,
14535   IX86_BUILTIN_MOVSLDUP,
14536   IX86_BUILTIN_ADDSUBPD,
14537   IX86_BUILTIN_HADDPD,
14538   IX86_BUILTIN_HSUBPD,
14539   IX86_BUILTIN_LDDQU,
14540
14541   IX86_BUILTIN_MONITOR,
14542   IX86_BUILTIN_MWAIT,
14543
14544   /* SSSE3.  */
14545   IX86_BUILTIN_PHADDW,
14546   IX86_BUILTIN_PHADDD,
14547   IX86_BUILTIN_PHADDSW,
14548   IX86_BUILTIN_PHSUBW,
14549   IX86_BUILTIN_PHSUBD,
14550   IX86_BUILTIN_PHSUBSW,
14551   IX86_BUILTIN_PMADDUBSW,
14552   IX86_BUILTIN_PMULHRSW,
14553   IX86_BUILTIN_PSHUFB,
14554   IX86_BUILTIN_PSIGNB,
14555   IX86_BUILTIN_PSIGNW,
14556   IX86_BUILTIN_PSIGND,
14557   IX86_BUILTIN_PALIGNR,
14558   IX86_BUILTIN_PABSB,
14559   IX86_BUILTIN_PABSW,
14560   IX86_BUILTIN_PABSD,
14561
14562   IX86_BUILTIN_PHADDW128,
14563   IX86_BUILTIN_PHADDD128,
14564   IX86_BUILTIN_PHADDSW128,
14565   IX86_BUILTIN_PHSUBW128,
14566   IX86_BUILTIN_PHSUBD128,
14567   IX86_BUILTIN_PHSUBSW128,
14568   IX86_BUILTIN_PMADDUBSW128,
14569   IX86_BUILTIN_PMULHRSW128,
14570   IX86_BUILTIN_PSHUFB128,
14571   IX86_BUILTIN_PSIGNB128,
14572   IX86_BUILTIN_PSIGNW128,
14573   IX86_BUILTIN_PSIGND128,
14574   IX86_BUILTIN_PALIGNR128,
14575   IX86_BUILTIN_PABSB128,
14576   IX86_BUILTIN_PABSW128,
14577   IX86_BUILTIN_PABSD128,
14578
14579   IX86_BUILTIN_VEC_INIT_V2SI,
14580   IX86_BUILTIN_VEC_INIT_V4HI,
14581   IX86_BUILTIN_VEC_INIT_V8QI,
14582   IX86_BUILTIN_VEC_EXT_V2DF,
14583   IX86_BUILTIN_VEC_EXT_V2DI,
14584   IX86_BUILTIN_VEC_EXT_V4SF,
14585   IX86_BUILTIN_VEC_EXT_V4SI,
14586   IX86_BUILTIN_VEC_EXT_V8HI,
14587   IX86_BUILTIN_VEC_EXT_V2SI,
14588   IX86_BUILTIN_VEC_EXT_V4HI,
14589   IX86_BUILTIN_VEC_SET_V8HI,
14590   IX86_BUILTIN_VEC_SET_V4HI,
14591
14592   IX86_BUILTIN_MAX
14593 };
14594
14595 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
14596 do {                                                                    \
14597   if ((MASK) & target_flags                                             \
14598       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
14599     add_builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,  \
14600                          NULL, NULL_TREE);                      \
14601 } while (0)
14602
14603 /* Bits for builtin_description.flag.  */
14604
14605 /* Set when we don't support the comparison natively, and should
14606    swap_comparison in order to support it.  */
14607 #define BUILTIN_DESC_SWAP_OPERANDS      1
14608
14609 struct builtin_description
14610 {
14611   const unsigned int mask;
14612   const enum insn_code icode;
14613   const char *const name;
14614   const enum ix86_builtins code;
14615   const enum rtx_code comparison;
14616   const unsigned int flag;
14617 };
14618
14619 static const struct builtin_description bdesc_comi[] =
14620 {
14621   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
14622   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
14623   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
14624   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
14625   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
14626   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
14627   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
14628   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
14629   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
14630   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
14631   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
14632   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
14633   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
14634   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
14635   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
14636   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
14637   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
14638   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
14639   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
14640   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
14641   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
14642   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
14643   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
14644   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
14645 };
14646
14647 static const struct builtin_description bdesc_2arg[] =
14648 {
14649   /* SSE */
14650   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
14651   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
14652   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
14653   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
14654   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
14655   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
14656   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
14657   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
14658
14659   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
14660   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
14661   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
14662   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
14663     BUILTIN_DESC_SWAP_OPERANDS },
14664   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
14665     BUILTIN_DESC_SWAP_OPERANDS },
14666   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
14667   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
14668   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
14669   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
14670   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
14671     BUILTIN_DESC_SWAP_OPERANDS },
14672   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
14673     BUILTIN_DESC_SWAP_OPERANDS },
14674   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
14675   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
14676   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
14677   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
14678   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
14679   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
14680   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
14681   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
14682   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
14683     BUILTIN_DESC_SWAP_OPERANDS },
14684   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
14685     BUILTIN_DESC_SWAP_OPERANDS },
14686   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
14687
14688   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
14689   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
14690   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
14691   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
14692
14693   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
14694   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
14695   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
14696   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
14697
14698   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
14699   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
14700   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
14701   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
14702   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
14703
14704   /* MMX */
14705   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
14706   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
14707   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
14708   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
14709   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
14710   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
14711   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
14712   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
14713
14714   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
14715   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
14716   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
14717   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
14718   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
14719   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
14720   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
14721   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
14722
14723   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
14724   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
14725   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
14726
14727   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
14728   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
14729   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
14730   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
14731
14732   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
14733   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
14734
14735   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
14736   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
14737   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
14738   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
14739   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
14740   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
14741
14742   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
14743   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
14744   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
14745   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
14746
14747   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
14748   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
14749   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
14750   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
14751   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
14752   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
14753
14754   /* Special.  */
14755   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
14756   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
14757   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
14758
14759   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
14760   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
14761   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
14762
14763   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
14764   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
14765   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
14766   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
14767   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
14768   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
14769
14770   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
14771   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
14772   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
14773   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
14774   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
14775   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
14776
14777   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
14778   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
14779   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
14780   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
14781
14782   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
14783   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
14784
14785   /* SSE2 */
14786   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
14787   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
14788   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
14789   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
14790   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
14791   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
14792   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
14793   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
14794
14795   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
14796   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
14797   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
14798   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
14799     BUILTIN_DESC_SWAP_OPERANDS },
14800   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
14801     BUILTIN_DESC_SWAP_OPERANDS },
14802   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
14803   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
14804   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
14805   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
14806   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
14807     BUILTIN_DESC_SWAP_OPERANDS },
14808   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
14809     BUILTIN_DESC_SWAP_OPERANDS },
14810   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
14811   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
14812   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
14813   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
14814   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
14815   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
14816   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
14817   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
14818   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
14819
14820   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
14821   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
14822   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
14823   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
14824
14825   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
14826   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
14827   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
14828   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
14829
14830   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
14831   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
14832   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
14833
14834   /* SSE2 MMX */
14835   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
14836   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
14837   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
14838   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
14839   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
14840   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
14841   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
14842   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
14843
14844   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
14845   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
14846   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
14847   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
14848   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
14849   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
14850   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
14851   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
14852
14853   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
14854   { MASK_SSE2, CODE_FOR_sse2_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
14855
14856   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
14857   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
14858   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
14859   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
14860
14861   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
14862   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
14863
14864   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
14865   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
14866   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
14867   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
14868   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
14869   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
14870
14871   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
14872   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
14873   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
14874   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
14875
14876   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
14877   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
14878   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
14879   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
14880   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
14881   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
14882   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
14883   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
14884
14885   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
14886   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
14887   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
14888
14889   { MASK_SSE2, CODE_FOR_sse2_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
14890   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
14891
14892   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
14893   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
14894
14895   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
14896   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
14897   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
14898
14899   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
14900   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
14901   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
14902
14903   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
14904   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
14905
14906   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
14907
14908   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
14909   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
14910   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
14911   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
14912
14913   /* SSE3 MMX */
14914   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
14915   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
14916   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
14917   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
14918   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
14919   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 },
14920
14921   /* SSSE3 */
14922   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv8hi3, "__builtin_ia32_phaddw128", IX86_BUILTIN_PHADDW128, 0, 0 },
14923   { MASK_SSSE3, CODE_FOR_ssse3_phaddwv4hi3, "__builtin_ia32_phaddw", IX86_BUILTIN_PHADDW, 0, 0 },
14924   { MASK_SSSE3, CODE_FOR_ssse3_phadddv4si3, "__builtin_ia32_phaddd128", IX86_BUILTIN_PHADDD128, 0, 0 },
14925   { MASK_SSSE3, CODE_FOR_ssse3_phadddv2si3, "__builtin_ia32_phaddd", IX86_BUILTIN_PHADDD, 0, 0 },
14926   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv8hi3, "__builtin_ia32_phaddsw128", IX86_BUILTIN_PHADDSW128, 0, 0 },
14927   { MASK_SSSE3, CODE_FOR_ssse3_phaddswv4hi3, "__builtin_ia32_phaddsw", IX86_BUILTIN_PHADDSW, 0, 0 },
14928   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv8hi3, "__builtin_ia32_phsubw128", IX86_BUILTIN_PHSUBW128, 0, 0 },
14929   { MASK_SSSE3, CODE_FOR_ssse3_phsubwv4hi3, "__builtin_ia32_phsubw", IX86_BUILTIN_PHSUBW, 0, 0 },
14930   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv4si3, "__builtin_ia32_phsubd128", IX86_BUILTIN_PHSUBD128, 0, 0 },
14931   { MASK_SSSE3, CODE_FOR_ssse3_phsubdv2si3, "__builtin_ia32_phsubd", IX86_BUILTIN_PHSUBD, 0, 0 },
14932   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv8hi3, "__builtin_ia32_phsubsw128", IX86_BUILTIN_PHSUBSW128, 0, 0 },
14933   { MASK_SSSE3, CODE_FOR_ssse3_phsubswv4hi3, "__builtin_ia32_phsubsw", IX86_BUILTIN_PHSUBSW, 0, 0 },
14934   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv8hi3, "__builtin_ia32_pmaddubsw128", IX86_BUILTIN_PMADDUBSW128, 0, 0 },
14935   { MASK_SSSE3, CODE_FOR_ssse3_pmaddubswv4hi3, "__builtin_ia32_pmaddubsw", IX86_BUILTIN_PMADDUBSW, 0, 0 },
14936   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv8hi3, "__builtin_ia32_pmulhrsw128", IX86_BUILTIN_PMULHRSW128, 0, 0 },
14937   { MASK_SSSE3, CODE_FOR_ssse3_pmulhrswv4hi3, "__builtin_ia32_pmulhrsw", IX86_BUILTIN_PMULHRSW, 0, 0 },
14938   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv16qi3, "__builtin_ia32_pshufb128", IX86_BUILTIN_PSHUFB128, 0, 0 },
14939   { MASK_SSSE3, CODE_FOR_ssse3_pshufbv8qi3, "__builtin_ia32_pshufb", IX86_BUILTIN_PSHUFB, 0, 0 },
14940   { MASK_SSSE3, CODE_FOR_ssse3_psignv16qi3, "__builtin_ia32_psignb128", IX86_BUILTIN_PSIGNB128, 0, 0 },
14941   { MASK_SSSE3, CODE_FOR_ssse3_psignv8qi3, "__builtin_ia32_psignb", IX86_BUILTIN_PSIGNB, 0, 0 },
14942   { MASK_SSSE3, CODE_FOR_ssse3_psignv8hi3, "__builtin_ia32_psignw128", IX86_BUILTIN_PSIGNW128, 0, 0 },
14943   { MASK_SSSE3, CODE_FOR_ssse3_psignv4hi3, "__builtin_ia32_psignw", IX86_BUILTIN_PSIGNW, 0, 0 },
14944   { MASK_SSSE3, CODE_FOR_ssse3_psignv4si3, "__builtin_ia32_psignd128", IX86_BUILTIN_PSIGND128, 0, 0 },
14945   { MASK_SSSE3, CODE_FOR_ssse3_psignv2si3, "__builtin_ia32_psignd", IX86_BUILTIN_PSIGND, 0, 0 }
14946 };
14947
14948 static const struct builtin_description bdesc_1arg[] =
14949 {
14950   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
14951   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
14952
14953   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
14954   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
14955   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
14956
14957   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
14958   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
14959   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
14960   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
14961   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
14962   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
14963
14964   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
14965   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
14966
14967   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
14968
14969   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
14970   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
14971
14972   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
14973   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
14974   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
14975   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
14976   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
14977
14978   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
14979
14980   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
14981   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
14982   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
14983   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
14984
14985   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
14986   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
14987   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
14988
14989   /* SSE3 */
14990   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
14991   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
14992
14993   /* SSSE3 */
14994   { MASK_SSSE3, CODE_FOR_absv16qi2, "__builtin_ia32_pabsb128", IX86_BUILTIN_PABSB128, 0, 0 },
14995   { MASK_SSSE3, CODE_FOR_absv8qi2, "__builtin_ia32_pabsb", IX86_BUILTIN_PABSB, 0, 0 },
14996   { MASK_SSSE3, CODE_FOR_absv8hi2, "__builtin_ia32_pabsw128", IX86_BUILTIN_PABSW128, 0, 0 },
14997   { MASK_SSSE3, CODE_FOR_absv4hi2, "__builtin_ia32_pabsw", IX86_BUILTIN_PABSW, 0, 0 },
14998   { MASK_SSSE3, CODE_FOR_absv4si2, "__builtin_ia32_pabsd128", IX86_BUILTIN_PABSD128, 0, 0 },
14999   { MASK_SSSE3, CODE_FOR_absv2si2, "__builtin_ia32_pabsd", IX86_BUILTIN_PABSD, 0, 0 },
15000 };
15001
15002 static void
15003 ix86_init_builtins (void)
15004 {
15005   if (TARGET_MMX)
15006     ix86_init_mmx_sse_builtins ();
15007 }
15008
15009 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
15010    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
15011    builtins.  */
15012 static void
15013 ix86_init_mmx_sse_builtins (void)
15014 {
15015   const struct builtin_description * d;
15016   size_t i;
15017
15018   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
15019   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
15020   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
15021   tree V2DI_type_node
15022     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
15023   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
15024   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
15025   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
15026   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
15027   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
15028   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
15029
15030   tree pchar_type_node = build_pointer_type (char_type_node);
15031   tree pcchar_type_node = build_pointer_type (
15032                              build_type_variant (char_type_node, 1, 0));
15033   tree pfloat_type_node = build_pointer_type (float_type_node);
15034   tree pcfloat_type_node = build_pointer_type (
15035                              build_type_variant (float_type_node, 1, 0));
15036   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
15037   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
15038   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
15039
15040   /* Comparisons.  */
15041   tree int_ftype_v4sf_v4sf
15042     = build_function_type_list (integer_type_node,
15043                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
15044   tree v4si_ftype_v4sf_v4sf
15045     = build_function_type_list (V4SI_type_node,
15046                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
15047   /* MMX/SSE/integer conversions.  */
15048   tree int_ftype_v4sf
15049     = build_function_type_list (integer_type_node,
15050                                 V4SF_type_node, NULL_TREE);
15051   tree int64_ftype_v4sf
15052     = build_function_type_list (long_long_integer_type_node,
15053                                 V4SF_type_node, NULL_TREE);
15054   tree int_ftype_v8qi
15055     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
15056   tree v4sf_ftype_v4sf_int
15057     = build_function_type_list (V4SF_type_node,
15058                                 V4SF_type_node, integer_type_node, NULL_TREE);
15059   tree v4sf_ftype_v4sf_int64
15060     = build_function_type_list (V4SF_type_node,
15061                                 V4SF_type_node, long_long_integer_type_node,
15062                                 NULL_TREE);
15063   tree v4sf_ftype_v4sf_v2si
15064     = build_function_type_list (V4SF_type_node,
15065                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
15066
15067   /* Miscellaneous.  */
15068   tree v8qi_ftype_v4hi_v4hi
15069     = build_function_type_list (V8QI_type_node,
15070                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
15071   tree v4hi_ftype_v2si_v2si
15072     = build_function_type_list (V4HI_type_node,
15073                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
15074   tree v4sf_ftype_v4sf_v4sf_int
15075     = build_function_type_list (V4SF_type_node,
15076                                 V4SF_type_node, V4SF_type_node,
15077                                 integer_type_node, NULL_TREE);
15078   tree v2si_ftype_v4hi_v4hi
15079     = build_function_type_list (V2SI_type_node,
15080                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
15081   tree v4hi_ftype_v4hi_int
15082     = build_function_type_list (V4HI_type_node,
15083                                 V4HI_type_node, integer_type_node, NULL_TREE);
15084   tree v4hi_ftype_v4hi_di
15085     = build_function_type_list (V4HI_type_node,
15086                                 V4HI_type_node, long_long_unsigned_type_node,
15087                                 NULL_TREE);
15088   tree v2si_ftype_v2si_di
15089     = build_function_type_list (V2SI_type_node,
15090                                 V2SI_type_node, long_long_unsigned_type_node,
15091                                 NULL_TREE);
15092   tree void_ftype_void
15093     = build_function_type (void_type_node, void_list_node);
15094   tree void_ftype_unsigned
15095     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
15096   tree void_ftype_unsigned_unsigned
15097     = build_function_type_list (void_type_node, unsigned_type_node,
15098                                 unsigned_type_node, NULL_TREE);
15099   tree void_ftype_pcvoid_unsigned_unsigned
15100     = build_function_type_list (void_type_node, const_ptr_type_node,
15101                                 unsigned_type_node, unsigned_type_node,
15102                                 NULL_TREE);
15103   tree unsigned_ftype_void
15104     = build_function_type (unsigned_type_node, void_list_node);
15105   tree v2si_ftype_v4sf
15106     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
15107   /* Loads/stores.  */
15108   tree void_ftype_v8qi_v8qi_pchar
15109     = build_function_type_list (void_type_node,
15110                                 V8QI_type_node, V8QI_type_node,
15111                                 pchar_type_node, NULL_TREE);
15112   tree v4sf_ftype_pcfloat
15113     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
15114   /* @@@ the type is bogus */
15115   tree v4sf_ftype_v4sf_pv2si
15116     = build_function_type_list (V4SF_type_node,
15117                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
15118   tree void_ftype_pv2si_v4sf
15119     = build_function_type_list (void_type_node,
15120                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
15121   tree void_ftype_pfloat_v4sf
15122     = build_function_type_list (void_type_node,
15123                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
15124   tree void_ftype_pdi_di
15125     = build_function_type_list (void_type_node,
15126                                 pdi_type_node, long_long_unsigned_type_node,
15127                                 NULL_TREE);
15128   tree void_ftype_pv2di_v2di
15129     = build_function_type_list (void_type_node,
15130                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
15131   /* Normal vector unops.  */
15132   tree v4sf_ftype_v4sf
15133     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
15134   tree v16qi_ftype_v16qi
15135     = build_function_type_list (V16QI_type_node, V16QI_type_node, NULL_TREE);
15136   tree v8hi_ftype_v8hi
15137     = build_function_type_list (V8HI_type_node, V8HI_type_node, NULL_TREE);
15138   tree v4si_ftype_v4si
15139     = build_function_type_list (V4SI_type_node, V4SI_type_node, NULL_TREE);
15140   tree v8qi_ftype_v8qi
15141     = build_function_type_list (V8QI_type_node, V8QI_type_node, NULL_TREE);
15142   tree v4hi_ftype_v4hi
15143     = build_function_type_list (V4HI_type_node, V4HI_type_node, NULL_TREE);
15144
15145   /* Normal vector binops.  */
15146   tree v4sf_ftype_v4sf_v4sf
15147     = build_function_type_list (V4SF_type_node,
15148                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
15149   tree v8qi_ftype_v8qi_v8qi
15150     = build_function_type_list (V8QI_type_node,
15151                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
15152   tree v4hi_ftype_v4hi_v4hi
15153     = build_function_type_list (V4HI_type_node,
15154                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
15155   tree v2si_ftype_v2si_v2si
15156     = build_function_type_list (V2SI_type_node,
15157                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
15158   tree di_ftype_di_di
15159     = build_function_type_list (long_long_unsigned_type_node,
15160                                 long_long_unsigned_type_node,
15161                                 long_long_unsigned_type_node, NULL_TREE);
15162
15163   tree di_ftype_di_di_int
15164     = build_function_type_list (long_long_unsigned_type_node,
15165                                 long_long_unsigned_type_node,
15166                                 long_long_unsigned_type_node,
15167                                 integer_type_node, NULL_TREE);
15168
15169   tree v2si_ftype_v2sf
15170     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
15171   tree v2sf_ftype_v2si
15172     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
15173   tree v2si_ftype_v2si
15174     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
15175   tree v2sf_ftype_v2sf
15176     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
15177   tree v2sf_ftype_v2sf_v2sf
15178     = build_function_type_list (V2SF_type_node,
15179                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
15180   tree v2si_ftype_v2sf_v2sf
15181     = build_function_type_list (V2SI_type_node,
15182                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
15183   tree pint_type_node    = build_pointer_type (integer_type_node);
15184   tree pdouble_type_node = build_pointer_type (double_type_node);
15185   tree pcdouble_type_node = build_pointer_type (
15186                                 build_type_variant (double_type_node, 1, 0));
15187   tree int_ftype_v2df_v2df
15188     = build_function_type_list (integer_type_node,
15189                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15190
15191   tree void_ftype_pcvoid
15192     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
15193   tree v4sf_ftype_v4si
15194     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
15195   tree v4si_ftype_v4sf
15196     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
15197   tree v2df_ftype_v4si
15198     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
15199   tree v4si_ftype_v2df
15200     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
15201   tree v2si_ftype_v2df
15202     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
15203   tree v4sf_ftype_v2df
15204     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
15205   tree v2df_ftype_v2si
15206     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
15207   tree v2df_ftype_v4sf
15208     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
15209   tree int_ftype_v2df
15210     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
15211   tree int64_ftype_v2df
15212     = build_function_type_list (long_long_integer_type_node,
15213                                 V2DF_type_node, NULL_TREE);
15214   tree v2df_ftype_v2df_int
15215     = build_function_type_list (V2DF_type_node,
15216                                 V2DF_type_node, integer_type_node, NULL_TREE);
15217   tree v2df_ftype_v2df_int64
15218     = build_function_type_list (V2DF_type_node,
15219                                 V2DF_type_node, long_long_integer_type_node,
15220                                 NULL_TREE);
15221   tree v4sf_ftype_v4sf_v2df
15222     = build_function_type_list (V4SF_type_node,
15223                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
15224   tree v2df_ftype_v2df_v4sf
15225     = build_function_type_list (V2DF_type_node,
15226                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
15227   tree v2df_ftype_v2df_v2df_int
15228     = build_function_type_list (V2DF_type_node,
15229                                 V2DF_type_node, V2DF_type_node,
15230                                 integer_type_node,
15231                                 NULL_TREE);
15232   tree v2df_ftype_v2df_pcdouble
15233     = build_function_type_list (V2DF_type_node,
15234                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
15235   tree void_ftype_pdouble_v2df
15236     = build_function_type_list (void_type_node,
15237                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
15238   tree void_ftype_pint_int
15239     = build_function_type_list (void_type_node,
15240                                 pint_type_node, integer_type_node, NULL_TREE);
15241   tree void_ftype_v16qi_v16qi_pchar
15242     = build_function_type_list (void_type_node,
15243                                 V16QI_type_node, V16QI_type_node,
15244                                 pchar_type_node, NULL_TREE);
15245   tree v2df_ftype_pcdouble
15246     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
15247   tree v2df_ftype_v2df_v2df
15248     = build_function_type_list (V2DF_type_node,
15249                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15250   tree v16qi_ftype_v16qi_v16qi
15251     = build_function_type_list (V16QI_type_node,
15252                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
15253   tree v8hi_ftype_v8hi_v8hi
15254     = build_function_type_list (V8HI_type_node,
15255                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
15256   tree v4si_ftype_v4si_v4si
15257     = build_function_type_list (V4SI_type_node,
15258                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
15259   tree v2di_ftype_v2di_v2di
15260     = build_function_type_list (V2DI_type_node,
15261                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
15262   tree v2di_ftype_v2df_v2df
15263     = build_function_type_list (V2DI_type_node,
15264                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15265   tree v2df_ftype_v2df
15266     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
15267   tree v2di_ftype_v2di_int
15268     = build_function_type_list (V2DI_type_node,
15269                                 V2DI_type_node, integer_type_node, NULL_TREE);
15270   tree v2di_ftype_v2di_v2di_int
15271     = build_function_type_list (V2DI_type_node, V2DI_type_node,
15272                                 V2DI_type_node, integer_type_node, NULL_TREE);
15273   tree v4si_ftype_v4si_int
15274     = build_function_type_list (V4SI_type_node,
15275                                 V4SI_type_node, integer_type_node, NULL_TREE);
15276   tree v8hi_ftype_v8hi_int
15277     = build_function_type_list (V8HI_type_node,
15278                                 V8HI_type_node, integer_type_node, NULL_TREE);
15279   tree v8hi_ftype_v8hi_v2di
15280     = build_function_type_list (V8HI_type_node,
15281                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
15282   tree v4si_ftype_v4si_v2di
15283     = build_function_type_list (V4SI_type_node,
15284                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
15285   tree v4si_ftype_v8hi_v8hi
15286     = build_function_type_list (V4SI_type_node,
15287                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
15288   tree di_ftype_v8qi_v8qi
15289     = build_function_type_list (long_long_unsigned_type_node,
15290                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
15291   tree di_ftype_v2si_v2si
15292     = build_function_type_list (long_long_unsigned_type_node,
15293                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
15294   tree v2di_ftype_v16qi_v16qi
15295     = build_function_type_list (V2DI_type_node,
15296                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
15297   tree v2di_ftype_v4si_v4si
15298     = build_function_type_list (V2DI_type_node,
15299                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
15300   tree int_ftype_v16qi
15301     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
15302   tree v16qi_ftype_pcchar
15303     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
15304   tree void_ftype_pchar_v16qi
15305     = build_function_type_list (void_type_node,
15306                                 pchar_type_node, V16QI_type_node, NULL_TREE);
15307
15308   tree float80_type;
15309   tree float128_type;
15310   tree ftype;
15311
15312   /* The __float80 type.  */
15313   if (TYPE_MODE (long_double_type_node) == XFmode)
15314     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
15315                                                "__float80");
15316   else
15317     {
15318       /* The __float80 type.  */
15319       float80_type = make_node (REAL_TYPE);
15320       TYPE_PRECISION (float80_type) = 80;
15321       layout_type (float80_type);
15322       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
15323     }
15324
15325   if (TARGET_64BIT)
15326     {
15327       float128_type = make_node (REAL_TYPE);
15328       TYPE_PRECISION (float128_type) = 128;
15329       layout_type (float128_type);
15330       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
15331     }
15332
15333   /* Add all builtins that are more or less simple operations on two
15334      operands.  */
15335   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15336     {
15337       /* Use one of the operands; the target can have a different mode for
15338          mask-generating compares.  */
15339       enum machine_mode mode;
15340       tree type;
15341
15342       if (d->name == 0)
15343         continue;
15344       mode = insn_data[d->icode].operand[1].mode;
15345
15346       switch (mode)
15347         {
15348         case V16QImode:
15349           type = v16qi_ftype_v16qi_v16qi;
15350           break;
15351         case V8HImode:
15352           type = v8hi_ftype_v8hi_v8hi;
15353           break;
15354         case V4SImode:
15355           type = v4si_ftype_v4si_v4si;
15356           break;
15357         case V2DImode:
15358           type = v2di_ftype_v2di_v2di;
15359           break;
15360         case V2DFmode:
15361           type = v2df_ftype_v2df_v2df;
15362           break;
15363         case V4SFmode:
15364           type = v4sf_ftype_v4sf_v4sf;
15365           break;
15366         case V8QImode:
15367           type = v8qi_ftype_v8qi_v8qi;
15368           break;
15369         case V4HImode:
15370           type = v4hi_ftype_v4hi_v4hi;
15371           break;
15372         case V2SImode:
15373           type = v2si_ftype_v2si_v2si;
15374           break;
15375         case DImode:
15376           type = di_ftype_di_di;
15377           break;
15378
15379         default:
15380           gcc_unreachable ();
15381         }
15382
15383       /* Override for comparisons.  */
15384       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
15385           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
15386         type = v4si_ftype_v4sf_v4sf;
15387
15388       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
15389           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
15390         type = v2di_ftype_v2df_v2df;
15391
15392       def_builtin (d->mask, d->name, type, d->code);
15393     }
15394
15395   /* Add all builtins that are more or less simple operations on 1 operand.  */
15396   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15397     {
15398       enum machine_mode mode;
15399       tree type;
15400
15401       if (d->name == 0)
15402         continue;
15403       mode = insn_data[d->icode].operand[1].mode;
15404
15405       switch (mode)
15406         {
15407         case V16QImode:
15408           type = v16qi_ftype_v16qi;
15409           break;
15410         case V8HImode:
15411           type = v8hi_ftype_v8hi;
15412           break;
15413         case V4SImode:
15414           type = v4si_ftype_v4si;
15415           break;
15416         case V2DFmode:
15417           type = v2df_ftype_v2df;
15418           break;
15419         case V4SFmode:
15420           type = v4sf_ftype_v4sf;
15421           break;
15422         case V8QImode:
15423           type = v8qi_ftype_v8qi;
15424           break;
15425         case V4HImode:
15426           type = v4hi_ftype_v4hi;
15427           break;
15428         case V2SImode:
15429           type = v2si_ftype_v2si;
15430           break;
15431
15432         default:
15433           abort ();
15434         }
15435
15436       def_builtin (d->mask, d->name, type, d->code);
15437     }
15438
15439   /* Add the remaining MMX insns with somewhat more complicated types.  */
15440   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
15441   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
15442   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
15443   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
15444
15445   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
15446   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
15447   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
15448
15449   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
15450   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
15451
15452   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
15453   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
15454
15455   /* comi/ucomi insns.  */
15456   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
15457     if (d->mask == MASK_SSE2)
15458       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
15459     else
15460       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
15461
15462   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
15463   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
15464   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
15465
15466   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
15467   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
15468   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
15469   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
15470   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
15471   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
15472   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
15473   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
15474   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
15475   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
15476   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
15477
15478   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
15479
15480   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
15481   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
15482
15483   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
15484   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
15485   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
15486   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
15487
15488   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
15489   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
15490   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
15491   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
15492
15493   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
15494
15495   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
15496
15497   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
15498   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
15499   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
15500   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
15501   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
15502   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
15503
15504   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
15505
15506   /* Original 3DNow!  */
15507   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
15508   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
15509   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
15510   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
15511   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
15512   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
15513   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
15514   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
15515   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
15516   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
15517   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
15518   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
15519   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
15520   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
15521   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
15522   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
15523   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
15524   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
15525   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
15526   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
15527
15528   /* 3DNow! extension as used in the Athlon CPU.  */
15529   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
15530   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
15531   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
15532   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
15533   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
15534   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
15535
15536   /* SSE2 */
15537   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
15538
15539   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
15540   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
15541
15542   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
15543   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
15544
15545   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
15546   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
15547   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
15548   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
15549   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
15550
15551   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
15552   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
15553   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
15554   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
15555
15556   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
15557   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
15558
15559   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
15560
15561   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
15562   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
15563
15564   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
15565   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
15566   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
15567   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
15568   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
15569
15570   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
15571
15572   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
15573   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
15574   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
15575   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
15576
15577   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
15578   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
15579   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
15580
15581   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
15582   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
15583   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
15584   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
15585
15586   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
15587   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
15588   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
15589
15590   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
15591   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
15592
15593   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
15594   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
15595
15596   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
15597   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
15598   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
15599
15600   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
15601   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
15602   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
15603
15604   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
15605   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
15606
15607   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
15608   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
15609   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
15610   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
15611
15612   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
15613   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
15614   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
15615   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
15616
15617   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
15618   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
15619
15620   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
15621
15622   /* Prescott New Instructions.  */
15623   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
15624                void_ftype_pcvoid_unsigned_unsigned,
15625                IX86_BUILTIN_MONITOR);
15626   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
15627                void_ftype_unsigned_unsigned,
15628                IX86_BUILTIN_MWAIT);
15629   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
15630                v4sf_ftype_v4sf,
15631                IX86_BUILTIN_MOVSHDUP);
15632   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
15633                v4sf_ftype_v4sf,
15634                IX86_BUILTIN_MOVSLDUP);
15635   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
15636                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
15637
15638   /* SSSE3.  */
15639   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr128",
15640                v2di_ftype_v2di_v2di_int, IX86_BUILTIN_PALIGNR128);
15641   def_builtin (MASK_SSSE3, "__builtin_ia32_palignr", di_ftype_di_di_int,
15642                IX86_BUILTIN_PALIGNR);
15643
15644   /* Access to the vec_init patterns.  */
15645   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
15646                                     integer_type_node, NULL_TREE);
15647   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
15648                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
15649
15650   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
15651                                     short_integer_type_node,
15652                                     short_integer_type_node,
15653                                     short_integer_type_node, NULL_TREE);
15654   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
15655                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
15656
15657   ftype = build_function_type_list (V8QI_type_node, char_type_node,
15658                                     char_type_node, char_type_node,
15659                                     char_type_node, char_type_node,
15660                                     char_type_node, char_type_node,
15661                                     char_type_node, NULL_TREE);
15662   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
15663                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
15664
15665   /* Access to the vec_extract patterns.  */
15666   ftype = build_function_type_list (double_type_node, V2DF_type_node,
15667                                     integer_type_node, NULL_TREE);
15668   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
15669                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
15670
15671   ftype = build_function_type_list (long_long_integer_type_node,
15672                                     V2DI_type_node, integer_type_node,
15673                                     NULL_TREE);
15674   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
15675                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
15676
15677   ftype = build_function_type_list (float_type_node, V4SF_type_node,
15678                                     integer_type_node, NULL_TREE);
15679   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
15680                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
15681
15682   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
15683                                     integer_type_node, NULL_TREE);
15684   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
15685                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
15686
15687   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
15688                                     integer_type_node, NULL_TREE);
15689   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
15690                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
15691
15692   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
15693                                     integer_type_node, NULL_TREE);
15694   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
15695                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
15696
15697   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
15698                                     integer_type_node, NULL_TREE);
15699   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
15700                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
15701
15702   /* Access to the vec_set patterns.  */
15703   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
15704                                     intHI_type_node,
15705                                     integer_type_node, NULL_TREE);
15706   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
15707                ftype, IX86_BUILTIN_VEC_SET_V8HI);
15708
15709   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
15710                                     intHI_type_node,
15711                                     integer_type_node, NULL_TREE);
15712   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
15713                ftype, IX86_BUILTIN_VEC_SET_V4HI);
15714 }
15715
15716 /* Errors in the source file can cause expand_expr to return const0_rtx
15717    where we expect a vector.  To avoid crashing, use one of the vector
15718    clear instructions.  */
15719 static rtx
15720 safe_vector_operand (rtx x, enum machine_mode mode)
15721 {
15722   if (x == const0_rtx)
15723     x = CONST0_RTX (mode);
15724   return x;
15725 }
15726
15727 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
15728
15729 static rtx
15730 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
15731 {
15732   rtx pat, xops[3];
15733   tree arg0 = TREE_VALUE (arglist);
15734   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15735   rtx op0 = expand_normal (arg0);
15736   rtx op1 = expand_normal (arg1);
15737   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15738   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15739   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15740
15741   if (VECTOR_MODE_P (mode0))
15742     op0 = safe_vector_operand (op0, mode0);
15743   if (VECTOR_MODE_P (mode1))
15744     op1 = safe_vector_operand (op1, mode1);
15745
15746   if (optimize || !target
15747       || GET_MODE (target) != tmode
15748       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15749     target = gen_reg_rtx (tmode);
15750
15751   if (GET_MODE (op1) == SImode && mode1 == TImode)
15752     {
15753       rtx x = gen_reg_rtx (V4SImode);
15754       emit_insn (gen_sse2_loadd (x, op1));
15755       op1 = gen_lowpart (TImode, x);
15756     }
15757
15758   /* The insn must want input operands in the same modes as the
15759      result.  */
15760   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
15761               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
15762
15763   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
15764     op0 = copy_to_mode_reg (mode0, op0);
15765   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
15766     op1 = copy_to_mode_reg (mode1, op1);
15767
15768   /* ??? Using ix86_fixup_binary_operands is problematic when
15769      we've got mismatched modes.  Fake it.  */
15770
15771   xops[0] = target;
15772   xops[1] = op0;
15773   xops[2] = op1;
15774
15775   if (tmode == mode0 && tmode == mode1)
15776     {
15777       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
15778       op0 = xops[1];
15779       op1 = xops[2];
15780     }
15781   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
15782     {
15783       op0 = force_reg (mode0, op0);
15784       op1 = force_reg (mode1, op1);
15785       target = gen_reg_rtx (tmode);
15786     }
15787
15788   pat = GEN_FCN (icode) (target, op0, op1);
15789   if (! pat)
15790     return 0;
15791   emit_insn (pat);
15792   return target;
15793 }
15794
15795 /* Subroutine of ix86_expand_builtin to take care of stores.  */
15796
15797 static rtx
15798 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
15799 {
15800   rtx pat;
15801   tree arg0 = TREE_VALUE (arglist);
15802   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15803   rtx op0 = expand_normal (arg0);
15804   rtx op1 = expand_normal (arg1);
15805   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
15806   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
15807
15808   if (VECTOR_MODE_P (mode1))
15809     op1 = safe_vector_operand (op1, mode1);
15810
15811   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15812   op1 = copy_to_mode_reg (mode1, op1);
15813
15814   pat = GEN_FCN (icode) (op0, op1);
15815   if (pat)
15816     emit_insn (pat);
15817   return 0;
15818 }
15819
15820 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
15821
15822 static rtx
15823 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
15824                           rtx target, int do_load)
15825 {
15826   rtx pat;
15827   tree arg0 = TREE_VALUE (arglist);
15828   rtx op0 = expand_normal (arg0);
15829   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15830   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15831
15832   if (optimize || !target
15833       || GET_MODE (target) != tmode
15834       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15835     target = gen_reg_rtx (tmode);
15836   if (do_load)
15837     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15838   else
15839     {
15840       if (VECTOR_MODE_P (mode0))
15841         op0 = safe_vector_operand (op0, mode0);
15842
15843       if ((optimize && !register_operand (op0, mode0))
15844           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15845         op0 = copy_to_mode_reg (mode0, op0);
15846     }
15847
15848   pat = GEN_FCN (icode) (target, op0);
15849   if (! pat)
15850     return 0;
15851   emit_insn (pat);
15852   return target;
15853 }
15854
15855 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
15856    sqrtss, rsqrtss, rcpss.  */
15857
15858 static rtx
15859 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
15860 {
15861   rtx pat;
15862   tree arg0 = TREE_VALUE (arglist);
15863   rtx op1, op0 = expand_normal (arg0);
15864   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15865   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15866
15867   if (optimize || !target
15868       || GET_MODE (target) != tmode
15869       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15870     target = gen_reg_rtx (tmode);
15871
15872   if (VECTOR_MODE_P (mode0))
15873     op0 = safe_vector_operand (op0, mode0);
15874
15875   if ((optimize && !register_operand (op0, mode0))
15876       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15877     op0 = copy_to_mode_reg (mode0, op0);
15878
15879   op1 = op0;
15880   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
15881     op1 = copy_to_mode_reg (mode0, op1);
15882
15883   pat = GEN_FCN (icode) (target, op0, op1);
15884   if (! pat)
15885     return 0;
15886   emit_insn (pat);
15887   return target;
15888 }
15889
15890 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
15891
15892 static rtx
15893 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
15894                          rtx target)
15895 {
15896   rtx pat;
15897   tree arg0 = TREE_VALUE (arglist);
15898   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15899   rtx op0 = expand_normal (arg0);
15900   rtx op1 = expand_normal (arg1);
15901   rtx op2;
15902   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
15903   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
15904   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
15905   enum rtx_code comparison = d->comparison;
15906
15907   if (VECTOR_MODE_P (mode0))
15908     op0 = safe_vector_operand (op0, mode0);
15909   if (VECTOR_MODE_P (mode1))
15910     op1 = safe_vector_operand (op1, mode1);
15911
15912   /* Swap operands if we have a comparison that isn't available in
15913      hardware.  */
15914   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15915     {
15916       rtx tmp = gen_reg_rtx (mode1);
15917       emit_move_insn (tmp, op1);
15918       op1 = op0;
15919       op0 = tmp;
15920     }
15921
15922   if (optimize || !target
15923       || GET_MODE (target) != tmode
15924       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
15925     target = gen_reg_rtx (tmode);
15926
15927   if ((optimize && !register_operand (op0, mode0))
15928       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
15929     op0 = copy_to_mode_reg (mode0, op0);
15930   if ((optimize && !register_operand (op1, mode1))
15931       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
15932     op1 = copy_to_mode_reg (mode1, op1);
15933
15934   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15935   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
15936   if (! pat)
15937     return 0;
15938   emit_insn (pat);
15939   return target;
15940 }
15941
15942 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
15943
15944 static rtx
15945 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
15946                       rtx target)
15947 {
15948   rtx pat;
15949   tree arg0 = TREE_VALUE (arglist);
15950   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15951   rtx op0 = expand_normal (arg0);
15952   rtx op1 = expand_normal (arg1);
15953   rtx op2;
15954   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
15955   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
15956   enum rtx_code comparison = d->comparison;
15957
15958   if (VECTOR_MODE_P (mode0))
15959     op0 = safe_vector_operand (op0, mode0);
15960   if (VECTOR_MODE_P (mode1))
15961     op1 = safe_vector_operand (op1, mode1);
15962
15963   /* Swap operands if we have a comparison that isn't available in
15964      hardware.  */
15965   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15966     {
15967       rtx tmp = op1;
15968       op1 = op0;
15969       op0 = tmp;
15970     }
15971
15972   target = gen_reg_rtx (SImode);
15973   emit_move_insn (target, const0_rtx);
15974   target = gen_rtx_SUBREG (QImode, target, 0);
15975
15976   if ((optimize && !register_operand (op0, mode0))
15977       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
15978     op0 = copy_to_mode_reg (mode0, op0);
15979   if ((optimize && !register_operand (op1, mode1))
15980       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
15981     op1 = copy_to_mode_reg (mode1, op1);
15982
15983   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15984   pat = GEN_FCN (d->icode) (op0, op1);
15985   if (! pat)
15986     return 0;
15987   emit_insn (pat);
15988   emit_insn (gen_rtx_SET (VOIDmode,
15989                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
15990                           gen_rtx_fmt_ee (comparison, QImode,
15991                                           SET_DEST (pat),
15992                                           const0_rtx)));
15993
15994   return SUBREG_REG (target);
15995 }
15996
15997 /* Return the integer constant in ARG.  Constrain it to be in the range
15998    of the subparts of VEC_TYPE; issue an error if not.  */
15999
16000 static int
16001 get_element_number (tree vec_type, tree arg)
16002 {
16003   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
16004
16005   if (!host_integerp (arg, 1)
16006       || (elt = tree_low_cst (arg, 1), elt > max))
16007     {
16008       error ("selector must be an integer constant in the range 0..%wi", max);
16009       return 0;
16010     }
16011
16012   return elt;
16013 }
16014
16015 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
16016    ix86_expand_vector_init.  We DO have language-level syntax for this, in
16017    the form of  (type){ init-list }.  Except that since we can't place emms
16018    instructions from inside the compiler, we can't allow the use of MMX
16019    registers unless the user explicitly asks for it.  So we do *not* define
16020    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
16021    we have builtins invoked by mmintrin.h that gives us license to emit
16022    these sorts of instructions.  */
16023
16024 static rtx
16025 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
16026 {
16027   enum machine_mode tmode = TYPE_MODE (type);
16028   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
16029   int i, n_elt = GET_MODE_NUNITS (tmode);
16030   rtvec v = rtvec_alloc (n_elt);
16031
16032   gcc_assert (VECTOR_MODE_P (tmode));
16033
16034   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
16035     {
16036       rtx x = expand_normal (TREE_VALUE (arglist));
16037       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
16038     }
16039
16040   gcc_assert (arglist == NULL);
16041
16042   if (!target || !register_operand (target, tmode))
16043     target = gen_reg_rtx (tmode);
16044
16045   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
16046   return target;
16047 }
16048
16049 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
16050    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
16051    had a language-level syntax for referencing vector elements.  */
16052
16053 static rtx
16054 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
16055 {
16056   enum machine_mode tmode, mode0;
16057   tree arg0, arg1;
16058   int elt;
16059   rtx op0;
16060
16061   arg0 = TREE_VALUE (arglist);
16062   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16063
16064   op0 = expand_normal (arg0);
16065   elt = get_element_number (TREE_TYPE (arg0), arg1);
16066
16067   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16068   mode0 = TYPE_MODE (TREE_TYPE (arg0));
16069   gcc_assert (VECTOR_MODE_P (mode0));
16070
16071   op0 = force_reg (mode0, op0);
16072
16073   if (optimize || !target || !register_operand (target, tmode))
16074     target = gen_reg_rtx (tmode);
16075
16076   ix86_expand_vector_extract (true, target, op0, elt);
16077
16078   return target;
16079 }
16080
16081 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
16082    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
16083    a language-level syntax for referencing vector elements.  */
16084
16085 static rtx
16086 ix86_expand_vec_set_builtin (tree arglist)
16087 {
16088   enum machine_mode tmode, mode1;
16089   tree arg0, arg1, arg2;
16090   int elt;
16091   rtx op0, op1;
16092
16093   arg0 = TREE_VALUE (arglist);
16094   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16095   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16096
16097   tmode = TYPE_MODE (TREE_TYPE (arg0));
16098   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
16099   gcc_assert (VECTOR_MODE_P (tmode));
16100
16101   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
16102   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
16103   elt = get_element_number (TREE_TYPE (arg0), arg2);
16104
16105   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
16106     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
16107
16108   op0 = force_reg (tmode, op0);
16109   op1 = force_reg (mode1, op1);
16110
16111   ix86_expand_vector_set (true, op0, op1, elt);
16112
16113   return op0;
16114 }
16115
16116 /* Expand an expression EXP that calls a built-in function,
16117    with result going to TARGET if that's convenient
16118    (and in mode MODE if that's convenient).
16119    SUBTARGET may be used as the target for computing one of EXP's operands.
16120    IGNORE is nonzero if the value is to be ignored.  */
16121
16122 static rtx
16123 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
16124                      enum machine_mode mode ATTRIBUTE_UNUSED,
16125                      int ignore ATTRIBUTE_UNUSED)
16126 {
16127   const struct builtin_description *d;
16128   size_t i;
16129   enum insn_code icode;
16130   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
16131   tree arglist = TREE_OPERAND (exp, 1);
16132   tree arg0, arg1, arg2;
16133   rtx op0, op1, op2, pat;
16134   enum machine_mode tmode, mode0, mode1, mode2, mode3;
16135   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
16136
16137   switch (fcode)
16138     {
16139     case IX86_BUILTIN_EMMS:
16140       emit_insn (gen_mmx_emms ());
16141       return 0;
16142
16143     case IX86_BUILTIN_SFENCE:
16144       emit_insn (gen_sse_sfence ());
16145       return 0;
16146
16147     case IX86_BUILTIN_MASKMOVQ:
16148     case IX86_BUILTIN_MASKMOVDQU:
16149       icode = (fcode == IX86_BUILTIN_MASKMOVQ
16150                ? CODE_FOR_mmx_maskmovq
16151                : CODE_FOR_sse2_maskmovdqu);
16152       /* Note the arg order is different from the operand order.  */
16153       arg1 = TREE_VALUE (arglist);
16154       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
16155       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16156       op0 = expand_normal (arg0);
16157       op1 = expand_normal (arg1);
16158       op2 = expand_normal (arg2);
16159       mode0 = insn_data[icode].operand[0].mode;
16160       mode1 = insn_data[icode].operand[1].mode;
16161       mode2 = insn_data[icode].operand[2].mode;
16162
16163       op0 = force_reg (Pmode, op0);
16164       op0 = gen_rtx_MEM (mode1, op0);
16165
16166       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16167         op0 = copy_to_mode_reg (mode0, op0);
16168       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
16169         op1 = copy_to_mode_reg (mode1, op1);
16170       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
16171         op2 = copy_to_mode_reg (mode2, op2);
16172       pat = GEN_FCN (icode) (op0, op1, op2);
16173       if (! pat)
16174         return 0;
16175       emit_insn (pat);
16176       return 0;
16177
16178     case IX86_BUILTIN_SQRTSS:
16179       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
16180     case IX86_BUILTIN_RSQRTSS:
16181       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
16182     case IX86_BUILTIN_RCPSS:
16183       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
16184
16185     case IX86_BUILTIN_LOADUPS:
16186       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
16187
16188     case IX86_BUILTIN_STOREUPS:
16189       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
16190
16191     case IX86_BUILTIN_LOADHPS:
16192     case IX86_BUILTIN_LOADLPS:
16193     case IX86_BUILTIN_LOADHPD:
16194     case IX86_BUILTIN_LOADLPD:
16195       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
16196                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
16197                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
16198                : CODE_FOR_sse2_loadlpd);
16199       arg0 = TREE_VALUE (arglist);
16200       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16201       op0 = expand_normal (arg0);
16202       op1 = expand_normal (arg1);
16203       tmode = insn_data[icode].operand[0].mode;
16204       mode0 = insn_data[icode].operand[1].mode;
16205       mode1 = insn_data[icode].operand[2].mode;
16206
16207       op0 = force_reg (mode0, op0);
16208       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
16209       if (optimize || target == 0
16210           || GET_MODE (target) != tmode
16211           || !register_operand (target, tmode))
16212         target = gen_reg_rtx (tmode);
16213       pat = GEN_FCN (icode) (target, op0, op1);
16214       if (! pat)
16215         return 0;
16216       emit_insn (pat);
16217       return target;
16218
16219     case IX86_BUILTIN_STOREHPS:
16220     case IX86_BUILTIN_STORELPS:
16221       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
16222                : CODE_FOR_sse_storelps);
16223       arg0 = TREE_VALUE (arglist);
16224       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16225       op0 = expand_normal (arg0);
16226       op1 = expand_normal (arg1);
16227       mode0 = insn_data[icode].operand[0].mode;
16228       mode1 = insn_data[icode].operand[1].mode;
16229
16230       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16231       op1 = force_reg (mode1, op1);
16232
16233       pat = GEN_FCN (icode) (op0, op1);
16234       if (! pat)
16235         return 0;
16236       emit_insn (pat);
16237       return const0_rtx;
16238
16239     case IX86_BUILTIN_MOVNTPS:
16240       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
16241     case IX86_BUILTIN_MOVNTQ:
16242       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
16243
16244     case IX86_BUILTIN_LDMXCSR:
16245       op0 = expand_normal (TREE_VALUE (arglist));
16246       target = assign_386_stack_local (SImode, SLOT_TEMP);
16247       emit_move_insn (target, op0);
16248       emit_insn (gen_sse_ldmxcsr (target));
16249       return 0;
16250
16251     case IX86_BUILTIN_STMXCSR:
16252       target = assign_386_stack_local (SImode, SLOT_TEMP);
16253       emit_insn (gen_sse_stmxcsr (target));
16254       return copy_to_mode_reg (SImode, target);
16255
16256     case IX86_BUILTIN_SHUFPS:
16257     case IX86_BUILTIN_SHUFPD:
16258       icode = (fcode == IX86_BUILTIN_SHUFPS
16259                ? CODE_FOR_sse_shufps
16260                : CODE_FOR_sse2_shufpd);
16261       arg0 = TREE_VALUE (arglist);
16262       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16263       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16264       op0 = expand_normal (arg0);
16265       op1 = expand_normal (arg1);
16266       op2 = expand_normal (arg2);
16267       tmode = insn_data[icode].operand[0].mode;
16268       mode0 = insn_data[icode].operand[1].mode;
16269       mode1 = insn_data[icode].operand[2].mode;
16270       mode2 = insn_data[icode].operand[3].mode;
16271
16272       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16273         op0 = copy_to_mode_reg (mode0, op0);
16274       if ((optimize && !register_operand (op1, mode1))
16275           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
16276         op1 = copy_to_mode_reg (mode1, op1);
16277       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16278         {
16279           /* @@@ better error message */
16280           error ("mask must be an immediate");
16281           return gen_reg_rtx (tmode);
16282         }
16283       if (optimize || target == 0
16284           || GET_MODE (target) != tmode
16285           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16286         target = gen_reg_rtx (tmode);
16287       pat = GEN_FCN (icode) (target, op0, op1, op2);
16288       if (! pat)
16289         return 0;
16290       emit_insn (pat);
16291       return target;
16292
16293     case IX86_BUILTIN_PSHUFW:
16294     case IX86_BUILTIN_PSHUFD:
16295     case IX86_BUILTIN_PSHUFHW:
16296     case IX86_BUILTIN_PSHUFLW:
16297       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
16298                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
16299                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
16300                : CODE_FOR_mmx_pshufw);
16301       arg0 = TREE_VALUE (arglist);
16302       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16303       op0 = expand_normal (arg0);
16304       op1 = expand_normal (arg1);
16305       tmode = insn_data[icode].operand[0].mode;
16306       mode1 = insn_data[icode].operand[1].mode;
16307       mode2 = insn_data[icode].operand[2].mode;
16308
16309       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16310         op0 = copy_to_mode_reg (mode1, op0);
16311       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16312         {
16313           /* @@@ better error message */
16314           error ("mask must be an immediate");
16315           return const0_rtx;
16316         }
16317       if (target == 0
16318           || GET_MODE (target) != tmode
16319           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16320         target = gen_reg_rtx (tmode);
16321       pat = GEN_FCN (icode) (target, op0, op1);
16322       if (! pat)
16323         return 0;
16324       emit_insn (pat);
16325       return target;
16326
16327     case IX86_BUILTIN_PSLLDQI128:
16328     case IX86_BUILTIN_PSRLDQI128:
16329       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
16330                : CODE_FOR_sse2_lshrti3);
16331       arg0 = TREE_VALUE (arglist);
16332       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16333       op0 = expand_normal (arg0);
16334       op1 = expand_normal (arg1);
16335       tmode = insn_data[icode].operand[0].mode;
16336       mode1 = insn_data[icode].operand[1].mode;
16337       mode2 = insn_data[icode].operand[2].mode;
16338
16339       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16340         {
16341           op0 = copy_to_reg (op0);
16342           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
16343         }
16344       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16345         {
16346           error ("shift must be an immediate");
16347           return const0_rtx;
16348         }
16349       target = gen_reg_rtx (V2DImode);
16350       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
16351       if (! pat)
16352         return 0;
16353       emit_insn (pat);
16354       return target;
16355
16356     case IX86_BUILTIN_FEMMS:
16357       emit_insn (gen_mmx_femms ());
16358       return NULL_RTX;
16359
16360     case IX86_BUILTIN_PAVGUSB:
16361       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
16362
16363     case IX86_BUILTIN_PF2ID:
16364       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
16365
16366     case IX86_BUILTIN_PFACC:
16367       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
16368
16369     case IX86_BUILTIN_PFADD:
16370      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
16371
16372     case IX86_BUILTIN_PFCMPEQ:
16373       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
16374
16375     case IX86_BUILTIN_PFCMPGE:
16376       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
16377
16378     case IX86_BUILTIN_PFCMPGT:
16379       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
16380
16381     case IX86_BUILTIN_PFMAX:
16382       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
16383
16384     case IX86_BUILTIN_PFMIN:
16385       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
16386
16387     case IX86_BUILTIN_PFMUL:
16388       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
16389
16390     case IX86_BUILTIN_PFRCP:
16391       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
16392
16393     case IX86_BUILTIN_PFRCPIT1:
16394       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
16395
16396     case IX86_BUILTIN_PFRCPIT2:
16397       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
16398
16399     case IX86_BUILTIN_PFRSQIT1:
16400       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
16401
16402     case IX86_BUILTIN_PFRSQRT:
16403       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
16404
16405     case IX86_BUILTIN_PFSUB:
16406       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
16407
16408     case IX86_BUILTIN_PFSUBR:
16409       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
16410
16411     case IX86_BUILTIN_PI2FD:
16412       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
16413
16414     case IX86_BUILTIN_PMULHRW:
16415       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
16416
16417     case IX86_BUILTIN_PF2IW:
16418       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
16419
16420     case IX86_BUILTIN_PFNACC:
16421       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
16422
16423     case IX86_BUILTIN_PFPNACC:
16424       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
16425
16426     case IX86_BUILTIN_PI2FW:
16427       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
16428
16429     case IX86_BUILTIN_PSWAPDSI:
16430       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
16431
16432     case IX86_BUILTIN_PSWAPDSF:
16433       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
16434
16435     case IX86_BUILTIN_SQRTSD:
16436       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
16437     case IX86_BUILTIN_LOADUPD:
16438       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
16439     case IX86_BUILTIN_STOREUPD:
16440       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
16441
16442     case IX86_BUILTIN_MFENCE:
16443         emit_insn (gen_sse2_mfence ());
16444         return 0;
16445     case IX86_BUILTIN_LFENCE:
16446         emit_insn (gen_sse2_lfence ());
16447         return 0;
16448
16449     case IX86_BUILTIN_CLFLUSH:
16450         arg0 = TREE_VALUE (arglist);
16451         op0 = expand_normal (arg0);
16452         icode = CODE_FOR_sse2_clflush;
16453         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
16454             op0 = copy_to_mode_reg (Pmode, op0);
16455
16456         emit_insn (gen_sse2_clflush (op0));
16457         return 0;
16458
16459     case IX86_BUILTIN_MOVNTPD:
16460       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
16461     case IX86_BUILTIN_MOVNTDQ:
16462       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
16463     case IX86_BUILTIN_MOVNTI:
16464       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
16465
16466     case IX86_BUILTIN_LOADDQU:
16467       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
16468     case IX86_BUILTIN_STOREDQU:
16469       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
16470
16471     case IX86_BUILTIN_MONITOR:
16472       arg0 = TREE_VALUE (arglist);
16473       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16474       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16475       op0 = expand_normal (arg0);
16476       op1 = expand_normal (arg1);
16477       op2 = expand_normal (arg2);
16478       if (!REG_P (op0))
16479         op0 = copy_to_mode_reg (Pmode, op0);
16480       if (!REG_P (op1))
16481         op1 = copy_to_mode_reg (SImode, op1);
16482       if (!REG_P (op2))
16483         op2 = copy_to_mode_reg (SImode, op2);
16484       if (!TARGET_64BIT)
16485         emit_insn (gen_sse3_monitor (op0, op1, op2));
16486       else
16487         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
16488       return 0;
16489
16490     case IX86_BUILTIN_MWAIT:
16491       arg0 = TREE_VALUE (arglist);
16492       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16493       op0 = expand_normal (arg0);
16494       op1 = expand_normal (arg1);
16495       if (!REG_P (op0))
16496         op0 = copy_to_mode_reg (SImode, op0);
16497       if (!REG_P (op1))
16498         op1 = copy_to_mode_reg (SImode, op1);
16499       emit_insn (gen_sse3_mwait (op0, op1));
16500       return 0;
16501
16502     case IX86_BUILTIN_LDDQU:
16503       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
16504                                        target, 1);
16505
16506     case IX86_BUILTIN_PALIGNR:
16507     case IX86_BUILTIN_PALIGNR128:
16508       if (fcode == IX86_BUILTIN_PALIGNR)
16509         {
16510           icode = CODE_FOR_ssse3_palignrdi;
16511           mode = DImode;
16512         }
16513       else
16514         {
16515           icode = CODE_FOR_ssse3_palignrti;
16516           mode = V2DImode;
16517         }
16518       arg0 = TREE_VALUE (arglist);
16519       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16520       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16521       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
16522       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
16523       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
16524       tmode = insn_data[icode].operand[0].mode;
16525       mode1 = insn_data[icode].operand[1].mode;
16526       mode2 = insn_data[icode].operand[2].mode;
16527       mode3 = insn_data[icode].operand[3].mode;
16528
16529       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16530         {
16531           op0 = copy_to_reg (op0);
16532           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
16533         }
16534       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16535         {
16536           op1 = copy_to_reg (op1);
16537           op1 = simplify_gen_subreg (mode2, op1, GET_MODE (op1), 0);
16538         }
16539       if (! (*insn_data[icode].operand[3].predicate) (op2, mode3))
16540         {
16541           error ("shift must be an immediate");
16542           return const0_rtx;
16543         }
16544       target = gen_reg_rtx (mode);
16545       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, mode, 0),
16546                              op0, op1, op2);
16547       if (! pat)
16548         return 0;
16549       emit_insn (pat);
16550       return target;
16551
16552     case IX86_BUILTIN_VEC_INIT_V2SI:
16553     case IX86_BUILTIN_VEC_INIT_V4HI:
16554     case IX86_BUILTIN_VEC_INIT_V8QI:
16555       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
16556
16557     case IX86_BUILTIN_VEC_EXT_V2DF:
16558     case IX86_BUILTIN_VEC_EXT_V2DI:
16559     case IX86_BUILTIN_VEC_EXT_V4SF:
16560     case IX86_BUILTIN_VEC_EXT_V4SI:
16561     case IX86_BUILTIN_VEC_EXT_V8HI:
16562     case IX86_BUILTIN_VEC_EXT_V2SI:
16563     case IX86_BUILTIN_VEC_EXT_V4HI:
16564       return ix86_expand_vec_ext_builtin (arglist, target);
16565
16566     case IX86_BUILTIN_VEC_SET_V8HI:
16567     case IX86_BUILTIN_VEC_SET_V4HI:
16568       return ix86_expand_vec_set_builtin (arglist);
16569
16570     default:
16571       break;
16572     }
16573
16574   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16575     if (d->code == fcode)
16576       {
16577         /* Compares are treated specially.  */
16578         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
16579             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
16580             || d->icode == CODE_FOR_sse2_maskcmpv2df3
16581             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
16582           return ix86_expand_sse_compare (d, arglist, target);
16583
16584         return ix86_expand_binop_builtin (d->icode, arglist, target);
16585       }
16586
16587   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16588     if (d->code == fcode)
16589       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
16590
16591   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
16592     if (d->code == fcode)
16593       return ix86_expand_sse_comi (d, arglist, target);
16594
16595   gcc_unreachable ();
16596 }
16597
16598 /* Store OPERAND to the memory after reload is completed.  This means
16599    that we can't easily use assign_stack_local.  */
16600 rtx
16601 ix86_force_to_memory (enum machine_mode mode, rtx operand)
16602 {
16603   rtx result;
16604
16605   gcc_assert (reload_completed);
16606   if (TARGET_RED_ZONE)
16607     {
16608       result = gen_rtx_MEM (mode,
16609                             gen_rtx_PLUS (Pmode,
16610                                           stack_pointer_rtx,
16611                                           GEN_INT (-RED_ZONE_SIZE)));
16612       emit_move_insn (result, operand);
16613     }
16614   else if (!TARGET_RED_ZONE && TARGET_64BIT)
16615     {
16616       switch (mode)
16617         {
16618         case HImode:
16619         case SImode:
16620           operand = gen_lowpart (DImode, operand);
16621           /* FALLTHRU */
16622         case DImode:
16623           emit_insn (
16624                       gen_rtx_SET (VOIDmode,
16625                                    gen_rtx_MEM (DImode,
16626                                                 gen_rtx_PRE_DEC (DImode,
16627                                                         stack_pointer_rtx)),
16628                                    operand));
16629           break;
16630         default:
16631           gcc_unreachable ();
16632         }
16633       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16634     }
16635   else
16636     {
16637       switch (mode)
16638         {
16639         case DImode:
16640           {
16641             rtx operands[2];
16642             split_di (&operand, 1, operands, operands + 1);
16643             emit_insn (
16644                         gen_rtx_SET (VOIDmode,
16645                                      gen_rtx_MEM (SImode,
16646                                                   gen_rtx_PRE_DEC (Pmode,
16647                                                         stack_pointer_rtx)),
16648                                      operands[1]));
16649             emit_insn (
16650                         gen_rtx_SET (VOIDmode,
16651                                      gen_rtx_MEM (SImode,
16652                                                   gen_rtx_PRE_DEC (Pmode,
16653                                                         stack_pointer_rtx)),
16654                                      operands[0]));
16655           }
16656           break;
16657         case HImode:
16658           /* Store HImodes as SImodes.  */
16659           operand = gen_lowpart (SImode, operand);
16660           /* FALLTHRU */
16661         case SImode:
16662           emit_insn (
16663                       gen_rtx_SET (VOIDmode,
16664                                    gen_rtx_MEM (GET_MODE (operand),
16665                                                 gen_rtx_PRE_DEC (SImode,
16666                                                         stack_pointer_rtx)),
16667                                    operand));
16668           break;
16669         default:
16670           gcc_unreachable ();
16671         }
16672       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16673     }
16674   return result;
16675 }
16676
16677 /* Free operand from the memory.  */
16678 void
16679 ix86_free_from_memory (enum machine_mode mode)
16680 {
16681   if (!TARGET_RED_ZONE)
16682     {
16683       int size;
16684
16685       if (mode == DImode || TARGET_64BIT)
16686         size = 8;
16687       else
16688         size = 4;
16689       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
16690          to pop or add instruction if registers are available.  */
16691       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16692                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
16693                                             GEN_INT (size))));
16694     }
16695 }
16696
16697 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
16698    QImode must go into class Q_REGS.
16699    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
16700    movdf to do mem-to-mem moves through integer regs.  */
16701 enum reg_class
16702 ix86_preferred_reload_class (rtx x, enum reg_class class)
16703 {
16704   enum machine_mode mode = GET_MODE (x);
16705
16706   /* We're only allowed to return a subclass of CLASS.  Many of the
16707      following checks fail for NO_REGS, so eliminate that early.  */
16708   if (class == NO_REGS)
16709     return NO_REGS;
16710
16711   /* All classes can load zeros.  */
16712   if (x == CONST0_RTX (mode))
16713     return class;
16714
16715   /* Force constants into memory if we are loading a (nonzero) constant into
16716      an MMX or SSE register.  This is because there are no MMX/SSE instructions
16717      to load from a constant.  */
16718   if (CONSTANT_P (x)
16719       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
16720     return NO_REGS;
16721
16722   /* Prefer SSE regs only, if we can use them for math.  */
16723   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
16724     return SSE_CLASS_P (class) ? class : NO_REGS;
16725
16726   /* Floating-point constants need more complex checks.  */
16727   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
16728     {
16729       /* General regs can load everything.  */
16730       if (reg_class_subset_p (class, GENERAL_REGS))
16731         return class;
16732
16733       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
16734          zero above.  We only want to wind up preferring 80387 registers if
16735          we plan on doing computation with them.  */
16736       if (TARGET_80387
16737           && standard_80387_constant_p (x))
16738         {
16739           /* Limit class to non-sse.  */
16740           if (class == FLOAT_SSE_REGS)
16741             return FLOAT_REGS;
16742           if (class == FP_TOP_SSE_REGS)
16743             return FP_TOP_REG;
16744           if (class == FP_SECOND_SSE_REGS)
16745             return FP_SECOND_REG;
16746           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
16747             return class;
16748         }
16749
16750       return NO_REGS;
16751     }
16752
16753   /* Generally when we see PLUS here, it's the function invariant
16754      (plus soft-fp const_int).  Which can only be computed into general
16755      regs.  */
16756   if (GET_CODE (x) == PLUS)
16757     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
16758
16759   /* QImode constants are easy to load, but non-constant QImode data
16760      must go into Q_REGS.  */
16761   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
16762     {
16763       if (reg_class_subset_p (class, Q_REGS))
16764         return class;
16765       if (reg_class_subset_p (Q_REGS, class))
16766         return Q_REGS;
16767       return NO_REGS;
16768     }
16769
16770   return class;
16771 }
16772
16773 /* Discourage putting floating-point values in SSE registers unless
16774    SSE math is being used, and likewise for the 387 registers.  */
16775 enum reg_class
16776 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
16777 {
16778   enum machine_mode mode = GET_MODE (x);
16779
16780   /* Restrict the output reload class to the register bank that we are doing
16781      math on.  If we would like not to return a subset of CLASS, reject this
16782      alternative: if reload cannot do this, it will still use its choice.  */
16783   mode = GET_MODE (x);
16784   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
16785     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
16786
16787   if (TARGET_80387 && SCALAR_FLOAT_MODE_P (mode))
16788     {
16789       if (class == FP_TOP_SSE_REGS)
16790         return FP_TOP_REG;
16791       else if (class == FP_SECOND_SSE_REGS)
16792         return FP_SECOND_REG;
16793       else
16794         return FLOAT_CLASS_P (class) ? class : NO_REGS;
16795     }
16796
16797   return class;
16798 }
16799
16800 /* If we are copying between general and FP registers, we need a memory
16801    location. The same is true for SSE and MMX registers.
16802
16803    The macro can't work reliably when one of the CLASSES is class containing
16804    registers from multiple units (SSE, MMX, integer).  We avoid this by never
16805    combining those units in single alternative in the machine description.
16806    Ensure that this constraint holds to avoid unexpected surprises.
16807
16808    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
16809    enforce these sanity checks.  */
16810
16811 int
16812 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
16813                               enum machine_mode mode, int strict)
16814 {
16815   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
16816       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
16817       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
16818       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
16819       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
16820       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
16821     {
16822       gcc_assert (!strict);
16823       return true;
16824     }
16825
16826   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
16827     return true;
16828
16829   /* ??? This is a lie.  We do have moves between mmx/general, and for
16830      mmx/sse2.  But by saying we need secondary memory we discourage the
16831      register allocator from using the mmx registers unless needed.  */
16832   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
16833     return true;
16834
16835   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16836     {
16837       /* SSE1 doesn't have any direct moves from other classes.  */
16838       if (!TARGET_SSE2)
16839         return true;
16840
16841       /* If the target says that inter-unit moves are more expensive
16842          than moving through memory, then don't generate them.  */
16843       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
16844         return true;
16845
16846       /* Between SSE and general, we have moves no larger than word size.  */
16847       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
16848         return true;
16849
16850       /* ??? For the cost of one register reformat penalty, we could use
16851          the same instructions to move SFmode and DFmode data, but the
16852          relevant move patterns don't support those alternatives.  */
16853       if (mode == SFmode || mode == DFmode)
16854         return true;
16855     }
16856
16857   return false;
16858 }
16859
16860 /* Return true if the registers in CLASS cannot represent the change from
16861    modes FROM to TO.  */
16862
16863 bool
16864 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
16865                                enum reg_class class)
16866 {
16867   if (from == to)
16868     return false;
16869
16870   /* x87 registers can't do subreg at all, as all values are reformatted
16871      to extended precision.  */
16872   if (MAYBE_FLOAT_CLASS_P (class))
16873     return true;
16874
16875   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
16876     {
16877       /* Vector registers do not support QI or HImode loads.  If we don't
16878          disallow a change to these modes, reload will assume it's ok to
16879          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
16880          the vec_dupv4hi pattern.  */
16881       if (GET_MODE_SIZE (from) < 4)
16882         return true;
16883
16884       /* Vector registers do not support subreg with nonzero offsets, which
16885          are otherwise valid for integer registers.  Since we can't see
16886          whether we have a nonzero offset from here, prohibit all
16887          nonparadoxical subregs changing size.  */
16888       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
16889         return true;
16890     }
16891
16892   return false;
16893 }
16894
16895 /* Return the cost of moving data from a register in class CLASS1 to
16896    one in class CLASS2.
16897
16898    It is not required that the cost always equal 2 when FROM is the same as TO;
16899    on some machines it is expensive to move between registers if they are not
16900    general registers.  */
16901
16902 int
16903 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
16904                          enum reg_class class2)
16905 {
16906   /* In case we require secondary memory, compute cost of the store followed
16907      by load.  In order to avoid bad register allocation choices, we need
16908      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
16909
16910   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
16911     {
16912       int cost = 1;
16913
16914       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
16915                    MEMORY_MOVE_COST (mode, class1, 1));
16916       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
16917                    MEMORY_MOVE_COST (mode, class2, 1));
16918
16919       /* In case of copying from general_purpose_register we may emit multiple
16920          stores followed by single load causing memory size mismatch stall.
16921          Count this as arbitrarily high cost of 20.  */
16922       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
16923         cost += 20;
16924
16925       /* In the case of FP/MMX moves, the registers actually overlap, and we
16926          have to switch modes in order to treat them differently.  */
16927       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
16928           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
16929         cost += 20;
16930
16931       return cost;
16932     }
16933
16934   /* Moves between SSE/MMX and integer unit are expensive.  */
16935   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
16936       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16937     return ix86_cost->mmxsse_to_integer;
16938   if (MAYBE_FLOAT_CLASS_P (class1))
16939     return ix86_cost->fp_move;
16940   if (MAYBE_SSE_CLASS_P (class1))
16941     return ix86_cost->sse_move;
16942   if (MAYBE_MMX_CLASS_P (class1))
16943     return ix86_cost->mmx_move;
16944   return 2;
16945 }
16946
16947 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
16948
16949 bool
16950 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
16951 {
16952   /* Flags and only flags can only hold CCmode values.  */
16953   if (CC_REGNO_P (regno))
16954     return GET_MODE_CLASS (mode) == MODE_CC;
16955   if (GET_MODE_CLASS (mode) == MODE_CC
16956       || GET_MODE_CLASS (mode) == MODE_RANDOM
16957       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
16958     return 0;
16959   if (FP_REGNO_P (regno))
16960     return VALID_FP_MODE_P (mode);
16961   if (SSE_REGNO_P (regno))
16962     {
16963       /* We implement the move patterns for all vector modes into and
16964          out of SSE registers, even when no operation instructions
16965          are available.  */
16966       return (VALID_SSE_REG_MODE (mode)
16967               || VALID_SSE2_REG_MODE (mode)
16968               || VALID_MMX_REG_MODE (mode)
16969               || VALID_MMX_REG_MODE_3DNOW (mode));
16970     }
16971   if (MMX_REGNO_P (regno))
16972     {
16973       /* We implement the move patterns for 3DNOW modes even in MMX mode,
16974          so if the register is available at all, then we can move data of
16975          the given mode into or out of it.  */
16976       return (VALID_MMX_REG_MODE (mode)
16977               || VALID_MMX_REG_MODE_3DNOW (mode));
16978     }
16979
16980   if (mode == QImode)
16981     {
16982       /* Take care for QImode values - they can be in non-QI regs,
16983          but then they do cause partial register stalls.  */
16984       if (regno < 4 || TARGET_64BIT)
16985         return 1;
16986       if (!TARGET_PARTIAL_REG_STALL)
16987         return 1;
16988       return reload_in_progress || reload_completed;
16989     }
16990   /* We handle both integer and floats in the general purpose registers.  */
16991   else if (VALID_INT_MODE_P (mode))
16992     return 1;
16993   else if (VALID_FP_MODE_P (mode))
16994     return 1;
16995   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
16996      on to use that value in smaller contexts, this can easily force a
16997      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
16998      supporting DImode, allow it.  */
16999   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
17000     return 1;
17001
17002   return 0;
17003 }
17004
17005 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
17006    tieable integer mode.  */
17007
17008 static bool
17009 ix86_tieable_integer_mode_p (enum machine_mode mode)
17010 {
17011   switch (mode)
17012     {
17013     case HImode:
17014     case SImode:
17015       return true;
17016
17017     case QImode:
17018       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
17019
17020     case DImode:
17021       return TARGET_64BIT;
17022
17023     default:
17024       return false;
17025     }
17026 }
17027
17028 /* Return true if MODE1 is accessible in a register that can hold MODE2
17029    without copying.  That is, all register classes that can hold MODE2
17030    can also hold MODE1.  */
17031
17032 bool
17033 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
17034 {
17035   if (mode1 == mode2)
17036     return true;
17037
17038   if (ix86_tieable_integer_mode_p (mode1)
17039       && ix86_tieable_integer_mode_p (mode2))
17040     return true;
17041
17042   /* MODE2 being XFmode implies fp stack or general regs, which means we
17043      can tie any smaller floating point modes to it.  Note that we do not
17044      tie this with TFmode.  */
17045   if (mode2 == XFmode)
17046     return mode1 == SFmode || mode1 == DFmode;
17047
17048   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
17049      that we can tie it with SFmode.  */
17050   if (mode2 == DFmode)
17051     return mode1 == SFmode;
17052
17053   /* If MODE2 is only appropriate for an SSE register, then tie with
17054      any other mode acceptable to SSE registers.  */
17055   if (GET_MODE_SIZE (mode2) >= 8
17056       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
17057     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
17058
17059   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
17060      with any other mode acceptable to MMX registers.  */
17061   if (GET_MODE_SIZE (mode2) == 8
17062       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
17063     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
17064
17065   return false;
17066 }
17067
17068 /* Return the cost of moving data of mode M between a
17069    register and memory.  A value of 2 is the default; this cost is
17070    relative to those in `REGISTER_MOVE_COST'.
17071
17072    If moving between registers and memory is more expensive than
17073    between two registers, you should define this macro to express the
17074    relative cost.
17075
17076    Model also increased moving costs of QImode registers in non
17077    Q_REGS classes.
17078  */
17079 int
17080 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
17081 {
17082   if (FLOAT_CLASS_P (class))
17083     {
17084       int index;
17085       switch (mode)
17086         {
17087           case SFmode:
17088             index = 0;
17089             break;
17090           case DFmode:
17091             index = 1;
17092             break;
17093           case XFmode:
17094             index = 2;
17095             break;
17096           default:
17097             return 100;
17098         }
17099       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
17100     }
17101   if (SSE_CLASS_P (class))
17102     {
17103       int index;
17104       switch (GET_MODE_SIZE (mode))
17105         {
17106           case 4:
17107             index = 0;
17108             break;
17109           case 8:
17110             index = 1;
17111             break;
17112           case 16:
17113             index = 2;
17114             break;
17115           default:
17116             return 100;
17117         }
17118       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
17119     }
17120   if (MMX_CLASS_P (class))
17121     {
17122       int index;
17123       switch (GET_MODE_SIZE (mode))
17124         {
17125           case 4:
17126             index = 0;
17127             break;
17128           case 8:
17129             index = 1;
17130             break;
17131           default:
17132             return 100;
17133         }
17134       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
17135     }
17136   switch (GET_MODE_SIZE (mode))
17137     {
17138       case 1:
17139         if (in)
17140           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
17141                   : ix86_cost->movzbl_load);
17142         else
17143           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
17144                   : ix86_cost->int_store[0] + 4);
17145         break;
17146       case 2:
17147         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
17148       default:
17149         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
17150         if (mode == TFmode)
17151           mode = XFmode;
17152         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
17153                 * (((int) GET_MODE_SIZE (mode)
17154                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
17155     }
17156 }
17157
17158 /* Compute a (partial) cost for rtx X.  Return true if the complete
17159    cost has been computed, and false if subexpressions should be
17160    scanned.  In either case, *TOTAL contains the cost result.  */
17161
17162 static bool
17163 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
17164 {
17165   enum machine_mode mode = GET_MODE (x);
17166
17167   switch (code)
17168     {
17169     case CONST_INT:
17170     case CONST:
17171     case LABEL_REF:
17172     case SYMBOL_REF:
17173       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
17174         *total = 3;
17175       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
17176         *total = 2;
17177       else if (flag_pic && SYMBOLIC_CONST (x)
17178                && (!TARGET_64BIT
17179                    || (!GET_CODE (x) != LABEL_REF
17180                        && (GET_CODE (x) != SYMBOL_REF
17181                            || !SYMBOL_REF_LOCAL_P (x)))))
17182         *total = 1;
17183       else
17184         *total = 0;
17185       return true;
17186
17187     case CONST_DOUBLE:
17188       if (mode == VOIDmode)
17189         *total = 0;
17190       else
17191         switch (standard_80387_constant_p (x))
17192           {
17193           case 1: /* 0.0 */
17194             *total = 1;
17195             break;
17196           default: /* Other constants */
17197             *total = 2;
17198             break;
17199           case 0:
17200           case -1:
17201             /* Start with (MEM (SYMBOL_REF)), since that's where
17202                it'll probably end up.  Add a penalty for size.  */
17203             *total = (COSTS_N_INSNS (1)
17204                       + (flag_pic != 0 && !TARGET_64BIT)
17205                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
17206             break;
17207           }
17208       return true;
17209
17210     case ZERO_EXTEND:
17211       /* The zero extensions is often completely free on x86_64, so make
17212          it as cheap as possible.  */
17213       if (TARGET_64BIT && mode == DImode
17214           && GET_MODE (XEXP (x, 0)) == SImode)
17215         *total = 1;
17216       else if (TARGET_ZERO_EXTEND_WITH_AND)
17217         *total = ix86_cost->add;
17218       else
17219         *total = ix86_cost->movzx;
17220       return false;
17221
17222     case SIGN_EXTEND:
17223       *total = ix86_cost->movsx;
17224       return false;
17225
17226     case ASHIFT:
17227       if (GET_CODE (XEXP (x, 1)) == CONST_INT
17228           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
17229         {
17230           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
17231           if (value == 1)
17232             {
17233               *total = ix86_cost->add;
17234               return false;
17235             }
17236           if ((value == 2 || value == 3)
17237               && ix86_cost->lea <= ix86_cost->shift_const)
17238             {
17239               *total = ix86_cost->lea;
17240               return false;
17241             }
17242         }
17243       /* FALLTHRU */
17244
17245     case ROTATE:
17246     case ASHIFTRT:
17247     case LSHIFTRT:
17248     case ROTATERT:
17249       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
17250         {
17251           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17252             {
17253               if (INTVAL (XEXP (x, 1)) > 32)
17254                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
17255               else
17256                 *total = ix86_cost->shift_const * 2;
17257             }
17258           else
17259             {
17260               if (GET_CODE (XEXP (x, 1)) == AND)
17261                 *total = ix86_cost->shift_var * 2;
17262               else
17263                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
17264             }
17265         }
17266       else
17267         {
17268           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17269             *total = ix86_cost->shift_const;
17270           else
17271             *total = ix86_cost->shift_var;
17272         }
17273       return false;
17274
17275     case MULT:
17276       if (FLOAT_MODE_P (mode))
17277         {
17278           *total = ix86_cost->fmul;
17279           return false;
17280         }
17281       else
17282         {
17283           rtx op0 = XEXP (x, 0);
17284           rtx op1 = XEXP (x, 1);
17285           int nbits;
17286           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17287             {
17288               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
17289               for (nbits = 0; value != 0; value &= value - 1)
17290                 nbits++;
17291             }
17292           else
17293             /* This is arbitrary.  */
17294             nbits = 7;
17295
17296           /* Compute costs correctly for widening multiplication.  */
17297           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
17298               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
17299                  == GET_MODE_SIZE (mode))
17300             {
17301               int is_mulwiden = 0;
17302               enum machine_mode inner_mode = GET_MODE (op0);
17303
17304               if (GET_CODE (op0) == GET_CODE (op1))
17305                 is_mulwiden = 1, op1 = XEXP (op1, 0);
17306               else if (GET_CODE (op1) == CONST_INT)
17307                 {
17308                   if (GET_CODE (op0) == SIGN_EXTEND)
17309                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
17310                                   == INTVAL (op1);
17311                   else
17312                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
17313                 }
17314
17315               if (is_mulwiden)
17316                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
17317             }
17318
17319           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
17320                     + nbits * ix86_cost->mult_bit
17321                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
17322
17323           return true;
17324         }
17325
17326     case DIV:
17327     case UDIV:
17328     case MOD:
17329     case UMOD:
17330       if (FLOAT_MODE_P (mode))
17331         *total = ix86_cost->fdiv;
17332       else
17333         *total = ix86_cost->divide[MODE_INDEX (mode)];
17334       return false;
17335
17336     case PLUS:
17337       if (FLOAT_MODE_P (mode))
17338         *total = ix86_cost->fadd;
17339       else if (GET_MODE_CLASS (mode) == MODE_INT
17340                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
17341         {
17342           if (GET_CODE (XEXP (x, 0)) == PLUS
17343               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
17344               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
17345               && CONSTANT_P (XEXP (x, 1)))
17346             {
17347               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
17348               if (val == 2 || val == 4 || val == 8)
17349                 {
17350                   *total = ix86_cost->lea;
17351                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
17352                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
17353                                       outer_code);
17354                   *total += rtx_cost (XEXP (x, 1), outer_code);
17355                   return true;
17356                 }
17357             }
17358           else if (GET_CODE (XEXP (x, 0)) == MULT
17359                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
17360             {
17361               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
17362               if (val == 2 || val == 4 || val == 8)
17363                 {
17364                   *total = ix86_cost->lea;
17365                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
17366                   *total += rtx_cost (XEXP (x, 1), outer_code);
17367                   return true;
17368                 }
17369             }
17370           else if (GET_CODE (XEXP (x, 0)) == PLUS)
17371             {
17372               *total = ix86_cost->lea;
17373               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
17374               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
17375               *total += rtx_cost (XEXP (x, 1), outer_code);
17376               return true;
17377             }
17378         }
17379       /* FALLTHRU */
17380
17381     case MINUS:
17382       if (FLOAT_MODE_P (mode))
17383         {
17384           *total = ix86_cost->fadd;
17385           return false;
17386         }
17387       /* FALLTHRU */
17388
17389     case AND:
17390     case IOR:
17391     case XOR:
17392       if (!TARGET_64BIT && mode == DImode)
17393         {
17394           *total = (ix86_cost->add * 2
17395                     + (rtx_cost (XEXP (x, 0), outer_code)
17396                        << (GET_MODE (XEXP (x, 0)) != DImode))
17397                     + (rtx_cost (XEXP (x, 1), outer_code)
17398                        << (GET_MODE (XEXP (x, 1)) != DImode)));
17399           return true;
17400         }
17401       /* FALLTHRU */
17402
17403     case NEG:
17404       if (FLOAT_MODE_P (mode))
17405         {
17406           *total = ix86_cost->fchs;
17407           return false;
17408         }
17409       /* FALLTHRU */
17410
17411     case NOT:
17412       if (!TARGET_64BIT && mode == DImode)
17413         *total = ix86_cost->add * 2;
17414       else
17415         *total = ix86_cost->add;
17416       return false;
17417
17418     case COMPARE:
17419       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
17420           && XEXP (XEXP (x, 0), 1) == const1_rtx
17421           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
17422           && XEXP (x, 1) == const0_rtx)
17423         {
17424           /* This kind of construct is implemented using test[bwl].
17425              Treat it as if we had an AND.  */
17426           *total = (ix86_cost->add
17427                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
17428                     + rtx_cost (const1_rtx, outer_code));
17429           return true;
17430         }
17431       return false;
17432
17433     case FLOAT_EXTEND:
17434       if (!TARGET_SSE_MATH
17435           || mode == XFmode
17436           || (mode == DFmode && !TARGET_SSE2))
17437         /* For standard 80387 constants, raise the cost to prevent
17438            compress_float_constant() to generate load from memory.  */
17439         switch (standard_80387_constant_p (XEXP (x, 0)))
17440           {
17441           case -1:
17442           case 0:
17443             *total = 0;
17444             break;
17445           case 1: /* 0.0 */
17446             *total = 1;
17447             break;
17448           default:
17449             *total = (x86_ext_80387_constants & TUNEMASK
17450                       || optimize_size
17451                       ? 1 : 0);
17452           }
17453       return false;
17454
17455     case ABS:
17456       if (FLOAT_MODE_P (mode))
17457         *total = ix86_cost->fabs;
17458       return false;
17459
17460     case SQRT:
17461       if (FLOAT_MODE_P (mode))
17462         *total = ix86_cost->fsqrt;
17463       return false;
17464
17465     case UNSPEC:
17466       if (XINT (x, 1) == UNSPEC_TP)
17467         *total = 0;
17468       return false;
17469
17470     default:
17471       return false;
17472     }
17473 }
17474
17475 #if TARGET_MACHO
17476
17477 static int current_machopic_label_num;
17478
17479 /* Given a symbol name and its associated stub, write out the
17480    definition of the stub.  */
17481
17482 void
17483 machopic_output_stub (FILE *file, const char *symb, const char *stub)
17484 {
17485   unsigned int length;
17486   char *binder_name, *symbol_name, lazy_ptr_name[32];
17487   int label = ++current_machopic_label_num;
17488
17489   /* For 64-bit we shouldn't get here.  */
17490   gcc_assert (!TARGET_64BIT);
17491
17492   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
17493   symb = (*targetm.strip_name_encoding) (symb);
17494
17495   length = strlen (stub);
17496   binder_name = alloca (length + 32);
17497   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
17498
17499   length = strlen (symb);
17500   symbol_name = alloca (length + 32);
17501   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
17502
17503   sprintf (lazy_ptr_name, "L%d$lz", label);
17504
17505   if (MACHOPIC_PURE)
17506     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
17507   else
17508     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
17509
17510   fprintf (file, "%s:\n", stub);
17511   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17512
17513   if (MACHOPIC_PURE)
17514     {
17515       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
17516       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
17517       fprintf (file, "\tjmp\t*%%edx\n");
17518     }
17519   else
17520     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
17521
17522   fprintf (file, "%s:\n", binder_name);
17523
17524   if (MACHOPIC_PURE)
17525     {
17526       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
17527       fprintf (file, "\tpushl\t%%eax\n");
17528     }
17529   else
17530     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
17531
17532   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
17533
17534   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
17535   fprintf (file, "%s:\n", lazy_ptr_name);
17536   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17537   fprintf (file, "\t.long %s\n", binder_name);
17538 }
17539
17540 void
17541 darwin_x86_file_end (void)
17542 {
17543   darwin_file_end ();
17544   ix86_file_end ();
17545 }
17546 #endif /* TARGET_MACHO */
17547
17548 /* Order the registers for register allocator.  */
17549
17550 void
17551 x86_order_regs_for_local_alloc (void)
17552 {
17553    int pos = 0;
17554    int i;
17555
17556    /* First allocate the local general purpose registers.  */
17557    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17558      if (GENERAL_REGNO_P (i) && call_used_regs[i])
17559         reg_alloc_order [pos++] = i;
17560
17561    /* Global general purpose registers.  */
17562    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17563      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
17564         reg_alloc_order [pos++] = i;
17565
17566    /* x87 registers come first in case we are doing FP math
17567       using them.  */
17568    if (!TARGET_SSE_MATH)
17569      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17570        reg_alloc_order [pos++] = i;
17571
17572    /* SSE registers.  */
17573    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
17574      reg_alloc_order [pos++] = i;
17575    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
17576      reg_alloc_order [pos++] = i;
17577
17578    /* x87 registers.  */
17579    if (TARGET_SSE_MATH)
17580      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17581        reg_alloc_order [pos++] = i;
17582
17583    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
17584      reg_alloc_order [pos++] = i;
17585
17586    /* Initialize the rest of array as we do not allocate some registers
17587       at all.  */
17588    while (pos < FIRST_PSEUDO_REGISTER)
17589      reg_alloc_order [pos++] = 0;
17590 }
17591
17592 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
17593    struct attribute_spec.handler.  */
17594 static tree
17595 ix86_handle_struct_attribute (tree *node, tree name,
17596                               tree args ATTRIBUTE_UNUSED,
17597                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
17598 {
17599   tree *type = NULL;
17600   if (DECL_P (*node))
17601     {
17602       if (TREE_CODE (*node) == TYPE_DECL)
17603         type = &TREE_TYPE (*node);
17604     }
17605   else
17606     type = node;
17607
17608   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
17609                  || TREE_CODE (*type) == UNION_TYPE)))
17610     {
17611       warning (OPT_Wattributes, "%qs attribute ignored",
17612                IDENTIFIER_POINTER (name));
17613       *no_add_attrs = true;
17614     }
17615
17616   else if ((is_attribute_p ("ms_struct", name)
17617             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
17618            || ((is_attribute_p ("gcc_struct", name)
17619                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
17620     {
17621       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
17622                IDENTIFIER_POINTER (name));
17623       *no_add_attrs = true;
17624     }
17625
17626   return NULL_TREE;
17627 }
17628
17629 static bool
17630 ix86_ms_bitfield_layout_p (tree record_type)
17631 {
17632   return (TARGET_MS_BITFIELD_LAYOUT &&
17633           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
17634     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
17635 }
17636
17637 /* Returns an expression indicating where the this parameter is
17638    located on entry to the FUNCTION.  */
17639
17640 static rtx
17641 x86_this_parameter (tree function)
17642 {
17643   tree type = TREE_TYPE (function);
17644
17645   if (TARGET_64BIT)
17646     {
17647       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
17648       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
17649     }
17650
17651   if (ix86_function_regparm (type, function) > 0)
17652     {
17653       tree parm;
17654
17655       parm = TYPE_ARG_TYPES (type);
17656       /* Figure out whether or not the function has a variable number of
17657          arguments.  */
17658       for (; parm; parm = TREE_CHAIN (parm))
17659         if (TREE_VALUE (parm) == void_type_node)
17660           break;
17661       /* If not, the this parameter is in the first argument.  */
17662       if (parm)
17663         {
17664           int regno = 0;
17665           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
17666             regno = 2;
17667           return gen_rtx_REG (SImode, regno);
17668         }
17669     }
17670
17671   if (aggregate_value_p (TREE_TYPE (type), type))
17672     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
17673   else
17674     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
17675 }
17676
17677 /* Determine whether x86_output_mi_thunk can succeed.  */
17678
17679 static bool
17680 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
17681                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
17682                          HOST_WIDE_INT vcall_offset, tree function)
17683 {
17684   /* 64-bit can handle anything.  */
17685   if (TARGET_64BIT)
17686     return true;
17687
17688   /* For 32-bit, everything's fine if we have one free register.  */
17689   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
17690     return true;
17691
17692   /* Need a free register for vcall_offset.  */
17693   if (vcall_offset)
17694     return false;
17695
17696   /* Need a free register for GOT references.  */
17697   if (flag_pic && !(*targetm.binds_local_p) (function))
17698     return false;
17699
17700   /* Otherwise ok.  */
17701   return true;
17702 }
17703
17704 /* Output the assembler code for a thunk function.  THUNK_DECL is the
17705    declaration for the thunk function itself, FUNCTION is the decl for
17706    the target function.  DELTA is an immediate constant offset to be
17707    added to THIS.  If VCALL_OFFSET is nonzero, the word at
17708    *(*this + vcall_offset) should be added to THIS.  */
17709
17710 static void
17711 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
17712                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
17713                      HOST_WIDE_INT vcall_offset, tree function)
17714 {
17715   rtx xops[3];
17716   rtx this = x86_this_parameter (function);
17717   rtx this_reg, tmp;
17718
17719   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
17720      pull it in now and let DELTA benefit.  */
17721   if (REG_P (this))
17722     this_reg = this;
17723   else if (vcall_offset)
17724     {
17725       /* Put the this parameter into %eax.  */
17726       xops[0] = this;
17727       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
17728       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17729     }
17730   else
17731     this_reg = NULL_RTX;
17732
17733   /* Adjust the this parameter by a fixed constant.  */
17734   if (delta)
17735     {
17736       xops[0] = GEN_INT (delta);
17737       xops[1] = this_reg ? this_reg : this;
17738       if (TARGET_64BIT)
17739         {
17740           if (!x86_64_general_operand (xops[0], DImode))
17741             {
17742               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17743               xops[1] = tmp;
17744               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
17745               xops[0] = tmp;
17746               xops[1] = this;
17747             }
17748           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17749         }
17750       else
17751         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17752     }
17753
17754   /* Adjust the this parameter by a value stored in the vtable.  */
17755   if (vcall_offset)
17756     {
17757       if (TARGET_64BIT)
17758         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17759       else
17760         {
17761           int tmp_regno = 2 /* ECX */;
17762           if (lookup_attribute ("fastcall",
17763               TYPE_ATTRIBUTES (TREE_TYPE (function))))
17764             tmp_regno = 0 /* EAX */;
17765           tmp = gen_rtx_REG (SImode, tmp_regno);
17766         }
17767
17768       xops[0] = gen_rtx_MEM (Pmode, this_reg);
17769       xops[1] = tmp;
17770       if (TARGET_64BIT)
17771         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17772       else
17773         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17774
17775       /* Adjust the this parameter.  */
17776       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
17777       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
17778         {
17779           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
17780           xops[0] = GEN_INT (vcall_offset);
17781           xops[1] = tmp2;
17782           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17783           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
17784         }
17785       xops[1] = this_reg;
17786       if (TARGET_64BIT)
17787         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17788       else
17789         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17790     }
17791
17792   /* If necessary, drop THIS back to its stack slot.  */
17793   if (this_reg && this_reg != this)
17794     {
17795       xops[0] = this_reg;
17796       xops[1] = this;
17797       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17798     }
17799
17800   xops[0] = XEXP (DECL_RTL (function), 0);
17801   if (TARGET_64BIT)
17802     {
17803       if (!flag_pic || (*targetm.binds_local_p) (function))
17804         output_asm_insn ("jmp\t%P0", xops);
17805       else
17806         {
17807           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
17808           tmp = gen_rtx_CONST (Pmode, tmp);
17809           tmp = gen_rtx_MEM (QImode, tmp);
17810           xops[0] = tmp;
17811           output_asm_insn ("jmp\t%A0", xops);
17812         }
17813     }
17814   else
17815     {
17816       if (!flag_pic || (*targetm.binds_local_p) (function))
17817         output_asm_insn ("jmp\t%P0", xops);
17818       else
17819 #if TARGET_MACHO
17820         if (TARGET_MACHO)
17821           {
17822             rtx sym_ref = XEXP (DECL_RTL (function), 0);
17823             tmp = (gen_rtx_SYMBOL_REF
17824                    (Pmode,
17825                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
17826             tmp = gen_rtx_MEM (QImode, tmp);
17827             xops[0] = tmp;
17828             output_asm_insn ("jmp\t%0", xops);
17829           }
17830         else
17831 #endif /* TARGET_MACHO */
17832         {
17833           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
17834           output_set_got (tmp, NULL_RTX);
17835
17836           xops[1] = tmp;
17837           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
17838           output_asm_insn ("jmp\t{*}%1", xops);
17839         }
17840     }
17841 }
17842
17843 static void
17844 x86_file_start (void)
17845 {
17846   default_file_start ();
17847 #if TARGET_MACHO
17848   darwin_file_start ();
17849 #endif
17850   if (X86_FILE_START_VERSION_DIRECTIVE)
17851     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
17852   if (X86_FILE_START_FLTUSED)
17853     fputs ("\t.global\t__fltused\n", asm_out_file);
17854   if (ix86_asm_dialect == ASM_INTEL)
17855     fputs ("\t.intel_syntax\n", asm_out_file);
17856 }
17857
17858 int
17859 x86_field_alignment (tree field, int computed)
17860 {
17861   enum machine_mode mode;
17862   tree type = TREE_TYPE (field);
17863
17864   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
17865     return computed;
17866   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
17867                     ? get_inner_array_type (type) : type);
17868   if (mode == DFmode || mode == DCmode
17869       || GET_MODE_CLASS (mode) == MODE_INT
17870       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
17871     return MIN (32, computed);
17872   return computed;
17873 }
17874
17875 /* Output assembler code to FILE to increment profiler label # LABELNO
17876    for profiling a function entry.  */
17877 void
17878 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
17879 {
17880   if (TARGET_64BIT)
17881     if (flag_pic)
17882       {
17883 #ifndef NO_PROFILE_COUNTERS
17884         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
17885 #endif
17886         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
17887       }
17888     else
17889       {
17890 #ifndef NO_PROFILE_COUNTERS
17891         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
17892 #endif
17893         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17894       }
17895   else if (flag_pic)
17896     {
17897 #ifndef NO_PROFILE_COUNTERS
17898       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
17899                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
17900 #endif
17901       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
17902     }
17903   else
17904     {
17905 #ifndef NO_PROFILE_COUNTERS
17906       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
17907                PROFILE_COUNT_REGISTER);
17908 #endif
17909       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17910     }
17911 }
17912
17913 /* We don't have exact information about the insn sizes, but we may assume
17914    quite safely that we are informed about all 1 byte insns and memory
17915    address sizes.  This is enough to eliminate unnecessary padding in
17916    99% of cases.  */
17917
17918 static int
17919 min_insn_size (rtx insn)
17920 {
17921   int l = 0;
17922
17923   if (!INSN_P (insn) || !active_insn_p (insn))
17924     return 0;
17925
17926   /* Discard alignments we've emit and jump instructions.  */
17927   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
17928       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
17929     return 0;
17930   if (GET_CODE (insn) == JUMP_INSN
17931       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
17932           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
17933     return 0;
17934
17935   /* Important case - calls are always 5 bytes.
17936      It is common to have many calls in the row.  */
17937   if (GET_CODE (insn) == CALL_INSN
17938       && symbolic_reference_mentioned_p (PATTERN (insn))
17939       && !SIBLING_CALL_P (insn))
17940     return 5;
17941   if (get_attr_length (insn) <= 1)
17942     return 1;
17943
17944   /* For normal instructions we may rely on the sizes of addresses
17945      and the presence of symbol to require 4 bytes of encoding.
17946      This is not the case for jumps where references are PC relative.  */
17947   if (GET_CODE (insn) != JUMP_INSN)
17948     {
17949       l = get_attr_length_address (insn);
17950       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
17951         l = 4;
17952     }
17953   if (l)
17954     return 1+l;
17955   else
17956     return 2;
17957 }
17958
17959 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
17960    window.  */
17961
17962 static void
17963 ix86_avoid_jump_misspredicts (void)
17964 {
17965   rtx insn, start = get_insns ();
17966   int nbytes = 0, njumps = 0;
17967   int isjump = 0;
17968
17969   /* Look for all minimal intervals of instructions containing 4 jumps.
17970      The intervals are bounded by START and INSN.  NBYTES is the total
17971      size of instructions in the interval including INSN and not including
17972      START.  When the NBYTES is smaller than 16 bytes, it is possible
17973      that the end of START and INSN ends up in the same 16byte page.
17974
17975      The smallest offset in the page INSN can start is the case where START
17976      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
17977      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
17978      */
17979   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17980     {
17981
17982       nbytes += min_insn_size (insn);
17983       if (dump_file)
17984         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
17985                 INSN_UID (insn), min_insn_size (insn));
17986       if ((GET_CODE (insn) == JUMP_INSN
17987            && GET_CODE (PATTERN (insn)) != ADDR_VEC
17988            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
17989           || GET_CODE (insn) == CALL_INSN)
17990         njumps++;
17991       else
17992         continue;
17993
17994       while (njumps > 3)
17995         {
17996           start = NEXT_INSN (start);
17997           if ((GET_CODE (start) == JUMP_INSN
17998                && GET_CODE (PATTERN (start)) != ADDR_VEC
17999                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
18000               || GET_CODE (start) == CALL_INSN)
18001             njumps--, isjump = 1;
18002           else
18003             isjump = 0;
18004           nbytes -= min_insn_size (start);
18005         }
18006       gcc_assert (njumps >= 0);
18007       if (dump_file)
18008         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
18009                 INSN_UID (start), INSN_UID (insn), nbytes);
18010
18011       if (njumps == 3 && isjump && nbytes < 16)
18012         {
18013           int padsize = 15 - nbytes + min_insn_size (insn);
18014
18015           if (dump_file)
18016             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
18017                      INSN_UID (insn), padsize);
18018           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
18019         }
18020     }
18021 }
18022
18023 /* AMD Athlon works faster
18024    when RET is not destination of conditional jump or directly preceded
18025    by other jump instruction.  We avoid the penalty by inserting NOP just
18026    before the RET instructions in such cases.  */
18027 static void
18028 ix86_pad_returns (void)
18029 {
18030   edge e;
18031   edge_iterator ei;
18032
18033   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
18034     {
18035       basic_block bb = e->src;
18036       rtx ret = BB_END (bb);
18037       rtx prev;
18038       bool replace = false;
18039
18040       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
18041           || !maybe_hot_bb_p (bb))
18042         continue;
18043       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
18044         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
18045           break;
18046       if (prev && GET_CODE (prev) == CODE_LABEL)
18047         {
18048           edge e;
18049           edge_iterator ei;
18050
18051           FOR_EACH_EDGE (e, ei, bb->preds)
18052             if (EDGE_FREQUENCY (e) && e->src->index >= 0
18053                 && !(e->flags & EDGE_FALLTHRU))
18054               replace = true;
18055         }
18056       if (!replace)
18057         {
18058           prev = prev_active_insn (ret);
18059           if (prev
18060               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
18061                   || GET_CODE (prev) == CALL_INSN))
18062             replace = true;
18063           /* Empty functions get branch mispredict even when the jump destination
18064              is not visible to us.  */
18065           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
18066             replace = true;
18067         }
18068       if (replace)
18069         {
18070           emit_insn_before (gen_return_internal_long (), ret);
18071           delete_insn (ret);
18072         }
18073     }
18074 }
18075
18076 /* Implement machine specific optimizations.  We implement padding of returns
18077    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
18078 static void
18079 ix86_reorg (void)
18080 {
18081   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
18082     ix86_pad_returns ();
18083   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
18084     ix86_avoid_jump_misspredicts ();
18085 }
18086
18087 /* Return nonzero when QImode register that must be represented via REX prefix
18088    is used.  */
18089 bool
18090 x86_extended_QIreg_mentioned_p (rtx insn)
18091 {
18092   int i;
18093   extract_insn_cached (insn);
18094   for (i = 0; i < recog_data.n_operands; i++)
18095     if (REG_P (recog_data.operand[i])
18096         && REGNO (recog_data.operand[i]) >= 4)
18097        return true;
18098   return false;
18099 }
18100
18101 /* Return nonzero when P points to register encoded via REX prefix.
18102    Called via for_each_rtx.  */
18103 static int
18104 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
18105 {
18106    unsigned int regno;
18107    if (!REG_P (*p))
18108      return 0;
18109    regno = REGNO (*p);
18110    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
18111 }
18112
18113 /* Return true when INSN mentions register that must be encoded using REX
18114    prefix.  */
18115 bool
18116 x86_extended_reg_mentioned_p (rtx insn)
18117 {
18118   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
18119 }
18120
18121 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
18122    optabs would emit if we didn't have TFmode patterns.  */
18123
18124 void
18125 x86_emit_floatuns (rtx operands[2])
18126 {
18127   rtx neglab, donelab, i0, i1, f0, in, out;
18128   enum machine_mode mode, inmode;
18129
18130   inmode = GET_MODE (operands[1]);
18131   gcc_assert (inmode == SImode || inmode == DImode);
18132
18133   out = operands[0];
18134   in = force_reg (inmode, operands[1]);
18135   mode = GET_MODE (out);
18136   neglab = gen_label_rtx ();
18137   donelab = gen_label_rtx ();
18138   i1 = gen_reg_rtx (Pmode);
18139   f0 = gen_reg_rtx (mode);
18140
18141   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
18142
18143   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
18144   emit_jump_insn (gen_jump (donelab));
18145   emit_barrier ();
18146
18147   emit_label (neglab);
18148
18149   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
18150   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
18151   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
18152   expand_float (f0, i0, 0);
18153   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
18154
18155   emit_label (donelab);
18156 }
18157 \f
18158 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
18159    with all elements equal to VAR.  Return true if successful.  */
18160
18161 static bool
18162 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
18163                                    rtx target, rtx val)
18164 {
18165   enum machine_mode smode, wsmode, wvmode;
18166   rtx x;
18167
18168   switch (mode)
18169     {
18170     case V2SImode:
18171     case V2SFmode:
18172       if (!mmx_ok)
18173         return false;
18174       /* FALLTHRU */
18175
18176     case V2DFmode:
18177     case V2DImode:
18178     case V4SFmode:
18179     case V4SImode:
18180       val = force_reg (GET_MODE_INNER (mode), val);
18181       x = gen_rtx_VEC_DUPLICATE (mode, val);
18182       emit_insn (gen_rtx_SET (VOIDmode, target, x));
18183       return true;
18184
18185     case V4HImode:
18186       if (!mmx_ok)
18187         return false;
18188       if (TARGET_SSE || TARGET_3DNOW_A)
18189         {
18190           val = gen_lowpart (SImode, val);
18191           x = gen_rtx_TRUNCATE (HImode, val);
18192           x = gen_rtx_VEC_DUPLICATE (mode, x);
18193           emit_insn (gen_rtx_SET (VOIDmode, target, x));
18194           return true;
18195         }
18196       else
18197         {
18198           smode = HImode;
18199           wsmode = SImode;
18200           wvmode = V2SImode;
18201           goto widen;
18202         }
18203
18204     case V8QImode:
18205       if (!mmx_ok)
18206         return false;
18207       smode = QImode;
18208       wsmode = HImode;
18209       wvmode = V4HImode;
18210       goto widen;
18211     case V8HImode:
18212       if (TARGET_SSE2)
18213         {
18214           rtx tmp1, tmp2;
18215           /* Extend HImode to SImode using a paradoxical SUBREG.  */
18216           tmp1 = gen_reg_rtx (SImode);
18217           emit_move_insn (tmp1, gen_lowpart (SImode, val));
18218           /* Insert the SImode value as low element of V4SImode vector. */
18219           tmp2 = gen_reg_rtx (V4SImode);
18220           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
18221                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
18222                                     CONST0_RTX (V4SImode),
18223                                     const1_rtx);
18224           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
18225           /* Cast the V4SImode vector back to a V8HImode vector.  */
18226           tmp1 = gen_reg_rtx (V8HImode);
18227           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
18228           /* Duplicate the low short through the whole low SImode word.  */
18229           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
18230           /* Cast the V8HImode vector back to a V4SImode vector.  */
18231           tmp2 = gen_reg_rtx (V4SImode);
18232           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
18233           /* Replicate the low element of the V4SImode vector.  */
18234           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
18235           /* Cast the V2SImode back to V8HImode, and store in target.  */
18236           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
18237           return true;
18238         }
18239       smode = HImode;
18240       wsmode = SImode;
18241       wvmode = V4SImode;
18242       goto widen;
18243     case V16QImode:
18244       if (TARGET_SSE2)
18245         {
18246           rtx tmp1, tmp2;
18247           /* Extend QImode to SImode using a paradoxical SUBREG.  */
18248           tmp1 = gen_reg_rtx (SImode);
18249           emit_move_insn (tmp1, gen_lowpart (SImode, val));
18250           /* Insert the SImode value as low element of V4SImode vector. */
18251           tmp2 = gen_reg_rtx (V4SImode);
18252           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
18253                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
18254                                     CONST0_RTX (V4SImode),
18255                                     const1_rtx);
18256           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
18257           /* Cast the V4SImode vector back to a V16QImode vector.  */
18258           tmp1 = gen_reg_rtx (V16QImode);
18259           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
18260           /* Duplicate the low byte through the whole low SImode word.  */
18261           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
18262           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
18263           /* Cast the V16QImode vector back to a V4SImode vector.  */
18264           tmp2 = gen_reg_rtx (V4SImode);
18265           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
18266           /* Replicate the low element of the V4SImode vector.  */
18267           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
18268           /* Cast the V2SImode back to V16QImode, and store in target.  */
18269           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
18270           return true;
18271         }
18272       smode = QImode;
18273       wsmode = HImode;
18274       wvmode = V8HImode;
18275       goto widen;
18276     widen:
18277       /* Replicate the value once into the next wider mode and recurse.  */
18278       val = convert_modes (wsmode, smode, val, true);
18279       x = expand_simple_binop (wsmode, ASHIFT, val,
18280                                GEN_INT (GET_MODE_BITSIZE (smode)),
18281                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
18282       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
18283
18284       x = gen_reg_rtx (wvmode);
18285       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
18286         gcc_unreachable ();
18287       emit_move_insn (target, gen_lowpart (mode, x));
18288       return true;
18289
18290     default:
18291       return false;
18292     }
18293 }
18294
18295 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
18296    whose ONE_VAR element is VAR, and other elements are zero.  Return true
18297    if successful.  */
18298
18299 static bool
18300 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
18301                                      rtx target, rtx var, int one_var)
18302 {
18303   enum machine_mode vsimode;
18304   rtx new_target;
18305   rtx x, tmp;
18306
18307   switch (mode)
18308     {
18309     case V2SFmode:
18310     case V2SImode:
18311       if (!mmx_ok)
18312         return false;
18313       /* FALLTHRU */
18314
18315     case V2DFmode:
18316     case V2DImode:
18317       if (one_var != 0)
18318         return false;
18319       var = force_reg (GET_MODE_INNER (mode), var);
18320       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
18321       emit_insn (gen_rtx_SET (VOIDmode, target, x));
18322       return true;
18323
18324     case V4SFmode:
18325     case V4SImode:
18326       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
18327         new_target = gen_reg_rtx (mode);
18328       else
18329         new_target = target;
18330       var = force_reg (GET_MODE_INNER (mode), var);
18331       x = gen_rtx_VEC_DUPLICATE (mode, var);
18332       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
18333       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
18334       if (one_var != 0)
18335         {
18336           /* We need to shuffle the value to the correct position, so
18337              create a new pseudo to store the intermediate result.  */
18338
18339           /* With SSE2, we can use the integer shuffle insns.  */
18340           if (mode != V4SFmode && TARGET_SSE2)
18341             {
18342               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
18343                                             GEN_INT (1),
18344                                             GEN_INT (one_var == 1 ? 0 : 1),
18345                                             GEN_INT (one_var == 2 ? 0 : 1),
18346                                             GEN_INT (one_var == 3 ? 0 : 1)));
18347               if (target != new_target)
18348                 emit_move_insn (target, new_target);
18349               return true;
18350             }
18351
18352           /* Otherwise convert the intermediate result to V4SFmode and
18353              use the SSE1 shuffle instructions.  */
18354           if (mode != V4SFmode)
18355             {
18356               tmp = gen_reg_rtx (V4SFmode);
18357               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
18358             }
18359           else
18360             tmp = new_target;
18361
18362           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
18363                                        GEN_INT (1),
18364                                        GEN_INT (one_var == 1 ? 0 : 1),
18365                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
18366                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
18367
18368           if (mode != V4SFmode)
18369             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
18370           else if (tmp != target)
18371             emit_move_insn (target, tmp);
18372         }
18373       else if (target != new_target)
18374         emit_move_insn (target, new_target);
18375       return true;
18376
18377     case V8HImode:
18378     case V16QImode:
18379       vsimode = V4SImode;
18380       goto widen;
18381     case V4HImode:
18382     case V8QImode:
18383       if (!mmx_ok)
18384         return false;
18385       vsimode = V2SImode;
18386       goto widen;
18387     widen:
18388       if (one_var != 0)
18389         return false;
18390
18391       /* Zero extend the variable element to SImode and recurse.  */
18392       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
18393
18394       x = gen_reg_rtx (vsimode);
18395       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
18396                                                 var, one_var))
18397         gcc_unreachable ();
18398
18399       emit_move_insn (target, gen_lowpart (mode, x));
18400       return true;
18401
18402     default:
18403       return false;
18404     }
18405 }
18406
18407 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
18408    consisting of the values in VALS.  It is known that all elements
18409    except ONE_VAR are constants.  Return true if successful.  */
18410
18411 static bool
18412 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
18413                                  rtx target, rtx vals, int one_var)
18414 {
18415   rtx var = XVECEXP (vals, 0, one_var);
18416   enum machine_mode wmode;
18417   rtx const_vec, x;
18418
18419   const_vec = copy_rtx (vals);
18420   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
18421   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
18422
18423   switch (mode)
18424     {
18425     case V2DFmode:
18426     case V2DImode:
18427     case V2SFmode:
18428     case V2SImode:
18429       /* For the two element vectors, it's just as easy to use
18430          the general case.  */
18431       return false;
18432
18433     case V4SFmode:
18434     case V4SImode:
18435     case V8HImode:
18436     case V4HImode:
18437       break;
18438
18439     case V16QImode:
18440       wmode = V8HImode;
18441       goto widen;
18442     case V8QImode:
18443       wmode = V4HImode;
18444       goto widen;
18445     widen:
18446       /* There's no way to set one QImode entry easily.  Combine
18447          the variable value with its adjacent constant value, and
18448          promote to an HImode set.  */
18449       x = XVECEXP (vals, 0, one_var ^ 1);
18450       if (one_var & 1)
18451         {
18452           var = convert_modes (HImode, QImode, var, true);
18453           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
18454                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
18455           x = GEN_INT (INTVAL (x) & 0xff);
18456         }
18457       else
18458         {
18459           var = convert_modes (HImode, QImode, var, true);
18460           x = gen_int_mode (INTVAL (x) << 8, HImode);
18461         }
18462       if (x != const0_rtx)
18463         var = expand_simple_binop (HImode, IOR, var, x, var,
18464                                    1, OPTAB_LIB_WIDEN);
18465
18466       x = gen_reg_rtx (wmode);
18467       emit_move_insn (x, gen_lowpart (wmode, const_vec));
18468       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
18469
18470       emit_move_insn (target, gen_lowpart (mode, x));
18471       return true;
18472
18473     default:
18474       return false;
18475     }
18476
18477   emit_move_insn (target, const_vec);
18478   ix86_expand_vector_set (mmx_ok, target, var, one_var);
18479   return true;
18480 }
18481
18482 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
18483    all values variable, and none identical.  */
18484
18485 static void
18486 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
18487                                  rtx target, rtx vals)
18488 {
18489   enum machine_mode half_mode = GET_MODE_INNER (mode);
18490   rtx op0 = NULL, op1 = NULL;
18491   bool use_vec_concat = false;
18492
18493   switch (mode)
18494     {
18495     case V2SFmode:
18496     case V2SImode:
18497       if (!mmx_ok && !TARGET_SSE)
18498         break;
18499       /* FALLTHRU */
18500
18501     case V2DFmode:
18502     case V2DImode:
18503       /* For the two element vectors, we always implement VEC_CONCAT.  */
18504       op0 = XVECEXP (vals, 0, 0);
18505       op1 = XVECEXP (vals, 0, 1);
18506       use_vec_concat = true;
18507       break;
18508
18509     case V4SFmode:
18510       half_mode = V2SFmode;
18511       goto half;
18512     case V4SImode:
18513       half_mode = V2SImode;
18514       goto half;
18515     half:
18516       {
18517         rtvec v;
18518
18519         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
18520            Recurse to load the two halves.  */
18521
18522         op0 = gen_reg_rtx (half_mode);
18523         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
18524         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
18525
18526         op1 = gen_reg_rtx (half_mode);
18527         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
18528         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
18529
18530         use_vec_concat = true;
18531       }
18532       break;
18533
18534     case V8HImode:
18535     case V16QImode:
18536     case V4HImode:
18537     case V8QImode:
18538       break;
18539
18540     default:
18541       gcc_unreachable ();
18542     }
18543
18544   if (use_vec_concat)
18545     {
18546       if (!register_operand (op0, half_mode))
18547         op0 = force_reg (half_mode, op0);
18548       if (!register_operand (op1, half_mode))
18549         op1 = force_reg (half_mode, op1);
18550
18551       emit_insn (gen_rtx_SET (VOIDmode, target,
18552                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
18553     }
18554   else
18555     {
18556       int i, j, n_elts, n_words, n_elt_per_word;
18557       enum machine_mode inner_mode;
18558       rtx words[4], shift;
18559
18560       inner_mode = GET_MODE_INNER (mode);
18561       n_elts = GET_MODE_NUNITS (mode);
18562       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
18563       n_elt_per_word = n_elts / n_words;
18564       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
18565
18566       for (i = 0; i < n_words; ++i)
18567         {
18568           rtx word = NULL_RTX;
18569
18570           for (j = 0; j < n_elt_per_word; ++j)
18571             {
18572               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
18573               elt = convert_modes (word_mode, inner_mode, elt, true);
18574
18575               if (j == 0)
18576                 word = elt;
18577               else
18578                 {
18579                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
18580                                               word, 1, OPTAB_LIB_WIDEN);
18581                   word = expand_simple_binop (word_mode, IOR, word, elt,
18582                                               word, 1, OPTAB_LIB_WIDEN);
18583                 }
18584             }
18585
18586           words[i] = word;
18587         }
18588
18589       if (n_words == 1)
18590         emit_move_insn (target, gen_lowpart (mode, words[0]));
18591       else if (n_words == 2)
18592         {
18593           rtx tmp = gen_reg_rtx (mode);
18594           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
18595           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
18596           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
18597           emit_move_insn (target, tmp);
18598         }
18599       else if (n_words == 4)
18600         {
18601           rtx tmp = gen_reg_rtx (V4SImode);
18602           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
18603           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
18604           emit_move_insn (target, gen_lowpart (mode, tmp));
18605         }
18606       else
18607         gcc_unreachable ();
18608     }
18609 }
18610
18611 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
18612    instructions unless MMX_OK is true.  */
18613
18614 void
18615 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
18616 {
18617   enum machine_mode mode = GET_MODE (target);
18618   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18619   int n_elts = GET_MODE_NUNITS (mode);
18620   int n_var = 0, one_var = -1;
18621   bool all_same = true, all_const_zero = true;
18622   int i;
18623   rtx x;
18624
18625   for (i = 0; i < n_elts; ++i)
18626     {
18627       x = XVECEXP (vals, 0, i);
18628       if (!CONSTANT_P (x))
18629         n_var++, one_var = i;
18630       else if (x != CONST0_RTX (inner_mode))
18631         all_const_zero = false;
18632       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18633         all_same = false;
18634     }
18635
18636   /* Constants are best loaded from the constant pool.  */
18637   if (n_var == 0)
18638     {
18639       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
18640       return;
18641     }
18642
18643   /* If all values are identical, broadcast the value.  */
18644   if (all_same
18645       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
18646                                             XVECEXP (vals, 0, 0)))
18647     return;
18648
18649   /* Values where only one field is non-constant are best loaded from
18650      the pool and overwritten via move later.  */
18651   if (n_var == 1)
18652     {
18653       if (all_const_zero
18654           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
18655                                                   XVECEXP (vals, 0, one_var),
18656                                                   one_var))
18657         return;
18658
18659       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
18660         return;
18661     }
18662
18663   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
18664 }
18665
18666 void
18667 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
18668 {
18669   enum machine_mode mode = GET_MODE (target);
18670   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18671   bool use_vec_merge = false;
18672   rtx tmp;
18673
18674   switch (mode)
18675     {
18676     case V2SFmode:
18677     case V2SImode:
18678       if (mmx_ok)
18679         {
18680           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
18681           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
18682           if (elt == 0)
18683             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
18684           else
18685             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
18686           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18687           return;
18688         }
18689       break;
18690
18691     case V2DFmode:
18692     case V2DImode:
18693       {
18694         rtx op0, op1;
18695
18696         /* For the two element vectors, we implement a VEC_CONCAT with
18697            the extraction of the other element.  */
18698
18699         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
18700         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
18701
18702         if (elt == 0)
18703           op0 = val, op1 = tmp;
18704         else
18705           op0 = tmp, op1 = val;
18706
18707         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
18708         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18709       }
18710       return;
18711
18712     case V4SFmode:
18713       switch (elt)
18714         {
18715         case 0:
18716           use_vec_merge = true;
18717           break;
18718
18719         case 1:
18720           /* tmp = target = A B C D */
18721           tmp = copy_to_reg (target);
18722           /* target = A A B B */
18723           emit_insn (gen_sse_unpcklps (target, target, target));
18724           /* target = X A B B */
18725           ix86_expand_vector_set (false, target, val, 0);
18726           /* target = A X C D  */
18727           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18728                                        GEN_INT (1), GEN_INT (0),
18729                                        GEN_INT (2+4), GEN_INT (3+4)));
18730           return;
18731
18732         case 2:
18733           /* tmp = target = A B C D */
18734           tmp = copy_to_reg (target);
18735           /* tmp = X B C D */
18736           ix86_expand_vector_set (false, tmp, val, 0);
18737           /* target = A B X D */
18738           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18739                                        GEN_INT (0), GEN_INT (1),
18740                                        GEN_INT (0+4), GEN_INT (3+4)));
18741           return;
18742
18743         case 3:
18744           /* tmp = target = A B C D */
18745           tmp = copy_to_reg (target);
18746           /* tmp = X B C D */
18747           ix86_expand_vector_set (false, tmp, val, 0);
18748           /* target = A B X D */
18749           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18750                                        GEN_INT (0), GEN_INT (1),
18751                                        GEN_INT (2+4), GEN_INT (0+4)));
18752           return;
18753
18754         default:
18755           gcc_unreachable ();
18756         }
18757       break;
18758
18759     case V4SImode:
18760       /* Element 0 handled by vec_merge below.  */
18761       if (elt == 0)
18762         {
18763           use_vec_merge = true;
18764           break;
18765         }
18766
18767       if (TARGET_SSE2)
18768         {
18769           /* With SSE2, use integer shuffles to swap element 0 and ELT,
18770              store into element 0, then shuffle them back.  */
18771
18772           rtx order[4];
18773
18774           order[0] = GEN_INT (elt);
18775           order[1] = const1_rtx;
18776           order[2] = const2_rtx;
18777           order[3] = GEN_INT (3);
18778           order[elt] = const0_rtx;
18779
18780           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18781                                         order[1], order[2], order[3]));
18782
18783           ix86_expand_vector_set (false, target, val, 0);
18784
18785           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18786                                         order[1], order[2], order[3]));
18787         }
18788       else
18789         {
18790           /* For SSE1, we have to reuse the V4SF code.  */
18791           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
18792                                   gen_lowpart (SFmode, val), elt);
18793         }
18794       return;
18795
18796     case V8HImode:
18797       use_vec_merge = TARGET_SSE2;
18798       break;
18799     case V4HImode:
18800       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18801       break;
18802
18803     case V16QImode:
18804     case V8QImode:
18805     default:
18806       break;
18807     }
18808
18809   if (use_vec_merge)
18810     {
18811       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
18812       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
18813       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18814     }
18815   else
18816     {
18817       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18818
18819       emit_move_insn (mem, target);
18820
18821       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18822       emit_move_insn (tmp, val);
18823
18824       emit_move_insn (target, mem);
18825     }
18826 }
18827
18828 void
18829 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
18830 {
18831   enum machine_mode mode = GET_MODE (vec);
18832   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18833   bool use_vec_extr = false;
18834   rtx tmp;
18835
18836   switch (mode)
18837     {
18838     case V2SImode:
18839     case V2SFmode:
18840       if (!mmx_ok)
18841         break;
18842       /* FALLTHRU */
18843
18844     case V2DFmode:
18845     case V2DImode:
18846       use_vec_extr = true;
18847       break;
18848
18849     case V4SFmode:
18850       switch (elt)
18851         {
18852         case 0:
18853           tmp = vec;
18854           break;
18855
18856         case 1:
18857         case 3:
18858           tmp = gen_reg_rtx (mode);
18859           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
18860                                        GEN_INT (elt), GEN_INT (elt),
18861                                        GEN_INT (elt+4), GEN_INT (elt+4)));
18862           break;
18863
18864         case 2:
18865           tmp = gen_reg_rtx (mode);
18866           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
18867           break;
18868
18869         default:
18870           gcc_unreachable ();
18871         }
18872       vec = tmp;
18873       use_vec_extr = true;
18874       elt = 0;
18875       break;
18876
18877     case V4SImode:
18878       if (TARGET_SSE2)
18879         {
18880           switch (elt)
18881             {
18882             case 0:
18883               tmp = vec;
18884               break;
18885
18886             case 1:
18887             case 3:
18888               tmp = gen_reg_rtx (mode);
18889               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
18890                                             GEN_INT (elt), GEN_INT (elt),
18891                                             GEN_INT (elt), GEN_INT (elt)));
18892               break;
18893
18894             case 2:
18895               tmp = gen_reg_rtx (mode);
18896               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
18897               break;
18898
18899             default:
18900               gcc_unreachable ();
18901             }
18902           vec = tmp;
18903           use_vec_extr = true;
18904           elt = 0;
18905         }
18906       else
18907         {
18908           /* For SSE1, we have to reuse the V4SF code.  */
18909           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
18910                                       gen_lowpart (V4SFmode, vec), elt);
18911           return;
18912         }
18913       break;
18914
18915     case V8HImode:
18916       use_vec_extr = TARGET_SSE2;
18917       break;
18918     case V4HImode:
18919       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18920       break;
18921
18922     case V16QImode:
18923     case V8QImode:
18924       /* ??? Could extract the appropriate HImode element and shift.  */
18925     default:
18926       break;
18927     }
18928
18929   if (use_vec_extr)
18930     {
18931       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
18932       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
18933
18934       /* Let the rtl optimizers know about the zero extension performed.  */
18935       if (inner_mode == HImode)
18936         {
18937           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
18938           target = gen_lowpart (SImode, target);
18939         }
18940
18941       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18942     }
18943   else
18944     {
18945       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18946
18947       emit_move_insn (mem, vec);
18948
18949       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18950       emit_move_insn (target, tmp);
18951     }
18952 }
18953
18954 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
18955    pattern to reduce; DEST is the destination; IN is the input vector.  */
18956
18957 void
18958 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
18959 {
18960   rtx tmp1, tmp2, tmp3;
18961
18962   tmp1 = gen_reg_rtx (V4SFmode);
18963   tmp2 = gen_reg_rtx (V4SFmode);
18964   tmp3 = gen_reg_rtx (V4SFmode);
18965
18966   emit_insn (gen_sse_movhlps (tmp1, in, in));
18967   emit_insn (fn (tmp2, tmp1, in));
18968
18969   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
18970                                GEN_INT (1), GEN_INT (1),
18971                                GEN_INT (1+4), GEN_INT (1+4)));
18972   emit_insn (fn (dest, tmp2, tmp3));
18973 }
18974 \f
18975 /* Target hook for scalar_mode_supported_p.  */
18976 static bool
18977 ix86_scalar_mode_supported_p (enum machine_mode mode)
18978 {
18979   if (DECIMAL_FLOAT_MODE_P (mode))
18980     return true;
18981   else
18982     return default_scalar_mode_supported_p (mode);
18983 }
18984
18985 /* Implements target hook vector_mode_supported_p.  */
18986 static bool
18987 ix86_vector_mode_supported_p (enum machine_mode mode)
18988 {
18989   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
18990     return true;
18991   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
18992     return true;
18993   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
18994     return true;
18995   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
18996     return true;
18997   return false;
18998 }
18999
19000 /* Worker function for TARGET_MD_ASM_CLOBBERS.
19001
19002    We do this in the new i386 backend to maintain source compatibility
19003    with the old cc0-based compiler.  */
19004
19005 static tree
19006 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
19007                       tree inputs ATTRIBUTE_UNUSED,
19008                       tree clobbers)
19009 {
19010   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
19011                         clobbers);
19012   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
19013                         clobbers);
19014   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
19015                         clobbers);
19016   return clobbers;
19017 }
19018
19019 /* Return true if this goes in small data/bss.  */
19020
19021 static bool
19022 ix86_in_large_data_p (tree exp)
19023 {
19024   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
19025     return false;
19026
19027   /* Functions are never large data.  */
19028   if (TREE_CODE (exp) == FUNCTION_DECL)
19029     return false;
19030
19031   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
19032     {
19033       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
19034       if (strcmp (section, ".ldata") == 0
19035           || strcmp (section, ".lbss") == 0)
19036         return true;
19037       return false;
19038     }
19039   else
19040     {
19041       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
19042
19043       /* If this is an incomplete type with size 0, then we can't put it
19044          in data because it might be too big when completed.  */
19045       if (!size || size > ix86_section_threshold)
19046         return true;
19047     }
19048
19049   return false;
19050 }
19051 static void
19052 ix86_encode_section_info (tree decl, rtx rtl, int first)
19053 {
19054   default_encode_section_info (decl, rtl, first);
19055
19056   if (TREE_CODE (decl) == VAR_DECL
19057       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
19058       && ix86_in_large_data_p (decl))
19059     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
19060 }
19061
19062 /* Worker function for REVERSE_CONDITION.  */
19063
19064 enum rtx_code
19065 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
19066 {
19067   return (mode != CCFPmode && mode != CCFPUmode
19068           ? reverse_condition (code)
19069           : reverse_condition_maybe_unordered (code));
19070 }
19071
19072 /* Output code to perform an x87 FP register move, from OPERANDS[1]
19073    to OPERANDS[0].  */
19074
19075 const char *
19076 output_387_reg_move (rtx insn, rtx *operands)
19077 {
19078   if (REG_P (operands[1])
19079       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
19080     {
19081       if (REGNO (operands[0]) == FIRST_STACK_REG)
19082         return output_387_ffreep (operands, 0);
19083       return "fstp\t%y0";
19084     }
19085   if (STACK_TOP_P (operands[0]))
19086     return "fld%z1\t%y1";
19087   return "fst\t%y0";
19088 }
19089
19090 /* Output code to perform a conditional jump to LABEL, if C2 flag in
19091    FP status register is set.  */
19092
19093 void
19094 ix86_emit_fp_unordered_jump (rtx label)
19095 {
19096   rtx reg = gen_reg_rtx (HImode);
19097   rtx temp;
19098
19099   emit_insn (gen_x86_fnstsw_1 (reg));
19100
19101   if (TARGET_USE_SAHF)
19102     {
19103       emit_insn (gen_x86_sahf_1 (reg));
19104
19105       temp = gen_rtx_REG (CCmode, FLAGS_REG);
19106       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
19107     }
19108   else
19109     {
19110       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
19111
19112       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
19113       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
19114     }
19115
19116   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
19117                               gen_rtx_LABEL_REF (VOIDmode, label),
19118                               pc_rtx);
19119   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
19120   emit_jump_insn (temp);
19121 }
19122
19123 /* Output code to perform a log1p XFmode calculation.  */
19124
19125 void ix86_emit_i387_log1p (rtx op0, rtx op1)
19126 {
19127   rtx label1 = gen_label_rtx ();
19128   rtx label2 = gen_label_rtx ();
19129
19130   rtx tmp = gen_reg_rtx (XFmode);
19131   rtx tmp2 = gen_reg_rtx (XFmode);
19132
19133   emit_insn (gen_absxf2 (tmp, op1));
19134   emit_insn (gen_cmpxf (tmp,
19135     CONST_DOUBLE_FROM_REAL_VALUE (
19136        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
19137        XFmode)));
19138   emit_jump_insn (gen_bge (label1));
19139
19140   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
19141   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
19142   emit_jump (label2);
19143
19144   emit_label (label1);
19145   emit_move_insn (tmp, CONST1_RTX (XFmode));
19146   emit_insn (gen_addxf3 (tmp, op1, tmp));
19147   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
19148   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
19149
19150   emit_label (label2);
19151 }
19152
19153 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
19154
19155 static void
19156 i386_solaris_elf_named_section (const char *name, unsigned int flags,
19157                                 tree decl)
19158 {
19159   /* With Binutils 2.15, the "@unwind" marker must be specified on
19160      every occurrence of the ".eh_frame" section, not just the first
19161      one.  */
19162   if (TARGET_64BIT
19163       && strcmp (name, ".eh_frame") == 0)
19164     {
19165       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
19166                flags & SECTION_WRITE ? "aw" : "a");
19167       return;
19168     }
19169   default_elf_asm_named_section (name, flags, decl);
19170 }
19171
19172 /* Return the mangling of TYPE if it is an extended fundamental type.  */
19173
19174 static const char *
19175 ix86_mangle_fundamental_type (tree type)
19176 {
19177   switch (TYPE_MODE (type))
19178     {
19179     case TFmode:
19180       /* __float128 is "g".  */
19181       return "g";
19182     case XFmode:
19183       /* "long double" or __float80 is "e".  */
19184       return "e";
19185     default:
19186       return NULL;
19187     }
19188 }
19189
19190 /* For 32-bit code we can save PIC register setup by using
19191    __stack_chk_fail_local hidden function instead of calling
19192    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
19193    register, so it is better to call __stack_chk_fail directly.  */
19194
19195 static tree
19196 ix86_stack_protect_fail (void)
19197 {
19198   return TARGET_64BIT
19199          ? default_external_stack_protect_fail ()
19200          : default_hidden_stack_protect_fail ();
19201 }
19202
19203 /* Select a format to encode pointers in exception handling data.  CODE
19204    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
19205    true if the symbol may be affected by dynamic relocations.
19206
19207    ??? All x86 object file formats are capable of representing this.
19208    After all, the relocation needed is the same as for the call insn.
19209    Whether or not a particular assembler allows us to enter such, I
19210    guess we'll have to see.  */
19211 int
19212 asm_preferred_eh_data_format (int code, int global)
19213 {
19214   if (flag_pic)
19215     {
19216       int type = DW_EH_PE_sdata8;
19217       if (!TARGET_64BIT
19218           || ix86_cmodel == CM_SMALL_PIC
19219           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
19220         type = DW_EH_PE_sdata4;
19221       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
19222     }
19223   if (ix86_cmodel == CM_SMALL
19224       || (ix86_cmodel == CM_MEDIUM && code))
19225     return DW_EH_PE_udata4;
19226   return DW_EH_PE_absptr;
19227 }
19228 \f
19229 /* Expand copysign from SIGN to the positive value ABS_VALUE
19230    storing in RESULT.  If MASK is non-null, it shall be a mask to mask out
19231    the sign-bit.  */
19232 static void
19233 ix86_sse_copysign_to_positive (rtx result, rtx abs_value, rtx sign, rtx mask)
19234 {
19235   enum machine_mode mode = GET_MODE (sign);
19236   rtx sgn = gen_reg_rtx (mode);
19237   if (mask == NULL_RTX)
19238     {
19239       mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), false);
19240       if (!VECTOR_MODE_P (mode))
19241         {
19242           /* We need to generate a scalar mode mask in this case.  */
19243           rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
19244           tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
19245           mask = gen_reg_rtx (mode);
19246           emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
19247         }
19248     }
19249   else
19250     mask = gen_rtx_NOT (mode, mask);
19251   emit_insn (gen_rtx_SET (VOIDmode, sgn,
19252                           gen_rtx_AND (mode, mask, sign)));
19253   emit_insn (gen_rtx_SET (VOIDmode, result,
19254                           gen_rtx_IOR (mode, abs_value, sgn)));
19255 }
19256
19257 /* Expand fabs (OP0) and return a new rtx that holds the result.  The
19258    mask for masking out the sign-bit is stored in *SMASK, if that is
19259    non-null.  */
19260 static rtx
19261 ix86_expand_sse_fabs (rtx op0, rtx *smask)
19262 {
19263   enum machine_mode mode = GET_MODE (op0);
19264   rtx xa, mask;
19265
19266   xa = gen_reg_rtx (mode);
19267   mask = ix86_build_signbit_mask (mode, VECTOR_MODE_P (mode), true);
19268   if (!VECTOR_MODE_P (mode))
19269     {
19270       /* We need to generate a scalar mode mask in this case.  */
19271       rtx tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, const0_rtx));
19272       tmp = gen_rtx_VEC_SELECT (mode, mask, tmp);
19273       mask = gen_reg_rtx (mode);
19274       emit_insn (gen_rtx_SET (VOIDmode, mask, tmp));
19275     }
19276   emit_insn (gen_rtx_SET (VOIDmode, xa,
19277                           gen_rtx_AND (mode, op0, mask)));
19278
19279   if (smask)
19280     *smask = mask;
19281
19282   return xa;
19283 }
19284
19285 /* Expands a comparison of OP0 with OP1 using comparison code CODE,
19286    swapping the operands if SWAP_OPERANDS is true.  The expanded
19287    code is a forward jump to a newly created label in case the
19288    comparison is true.  The generated label rtx is returned.  */
19289 static rtx
19290 ix86_expand_sse_compare_and_jump (enum rtx_code code, rtx op0, rtx op1,
19291                                   bool swap_operands)
19292 {
19293   rtx label, tmp;
19294
19295   if (swap_operands)
19296     {
19297       tmp = op0;
19298       op0 = op1;
19299       op1 = tmp;
19300     }
19301
19302   label = gen_label_rtx ();
19303   tmp = gen_rtx_REG (CCFPUmode, FLAGS_REG);
19304   emit_insn (gen_rtx_SET (VOIDmode, tmp,
19305                           gen_rtx_COMPARE (CCFPUmode, op0, op1)));
19306   tmp = gen_rtx_fmt_ee (code, VOIDmode, tmp, const0_rtx);
19307   tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
19308                               gen_rtx_LABEL_REF (VOIDmode, label), pc_rtx);
19309   tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
19310   JUMP_LABEL (tmp) = label;
19311
19312   return label;
19313 }
19314
19315 /* Expand a mask generating SSE comparison instruction comparing OP0 with OP1
19316    using comparison code CODE.  Operands are swapped for the comparison if
19317    SWAP_OPERANDS is true.  Returns a rtx for the generated mask.  */
19318 static rtx
19319 ix86_expand_sse_compare_mask (enum rtx_code code, rtx op0, rtx op1,
19320                               bool swap_operands)
19321 {
19322   enum machine_mode mode = GET_MODE (op0);
19323   rtx mask = gen_reg_rtx (mode);
19324
19325   if (swap_operands)
19326     {
19327       rtx tmp = op0;
19328       op0 = op1;
19329       op1 = tmp;
19330     }
19331
19332   if (mode == DFmode)
19333     emit_insn (gen_sse2_maskcmpdf3 (mask, op0, op1,
19334                                     gen_rtx_fmt_ee (code, mode, op0, op1)));
19335   else
19336     emit_insn (gen_sse_maskcmpsf3 (mask, op0, op1,
19337                                    gen_rtx_fmt_ee (code, mode, op0, op1)));
19338
19339   return mask;
19340 }
19341
19342 /* Generate and return a rtx of mode MODE for 2**n where n is the number
19343    of bits of the mantissa of MODE, which must be one of DFmode or SFmode.  */
19344 static rtx
19345 ix86_gen_TWO52 (enum machine_mode mode)
19346 {
19347   REAL_VALUE_TYPE TWO52r;
19348   rtx TWO52;
19349
19350   real_ldexp (&TWO52r, &dconst1, mode == DFmode ? 52 : 23);
19351   TWO52 = const_double_from_real_value (TWO52r, mode);
19352   TWO52 = force_reg (mode, TWO52);
19353
19354   return TWO52;
19355 }
19356
19357 /* Expand SSE sequence for computing lround from OP1 storing
19358    into OP0.  */
19359 void
19360 ix86_expand_lround (rtx op0, rtx op1)
19361 {
19362   /* C code for the stuff we're doing below:
19363        tmp = op1 + copysign (nextafter (0.5, 0.0), op1)
19364        return (long)tmp;
19365    */
19366   enum machine_mode mode = GET_MODE (op1);
19367   const struct real_format *fmt;
19368   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
19369   rtx adj;
19370
19371   /* load nextafter (0.5, 0.0) */
19372   fmt = REAL_MODE_FORMAT (mode);
19373   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
19374   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
19375
19376   /* adj = copysign (0.5, op1) */
19377   adj = force_reg (mode, const_double_from_real_value (pred_half, mode));
19378   ix86_sse_copysign_to_positive (adj, adj, force_reg (mode, op1), NULL_RTX);
19379
19380   /* adj = op1 + adj */
19381   expand_simple_binop (mode, PLUS, adj, op1, adj, 0, OPTAB_DIRECT);
19382
19383   /* op0 = (imode)adj */
19384   expand_fix (op0, adj, 0);
19385 }
19386
19387 /* Expand SSE2 sequence for computing lround from OPERAND1 storing
19388    into OPERAND0.  */
19389 void
19390 ix86_expand_lfloorceil (rtx op0, rtx op1, bool do_floor)
19391 {
19392   /* C code for the stuff we're doing below (for do_floor):
19393         xi = (long)op1;
19394         xi -= (double)xi > op1 ? 1 : 0;
19395         return xi;
19396    */
19397   enum machine_mode fmode = GET_MODE (op1);
19398   enum machine_mode imode = GET_MODE (op0);
19399   rtx ireg, freg, label;
19400
19401   /* reg = (long)op1 */
19402   ireg = gen_reg_rtx (imode);
19403   expand_fix (ireg, op1, 0);
19404
19405   /* freg = (double)reg */
19406   freg = gen_reg_rtx (fmode);
19407   expand_float (freg, ireg, 0);
19408
19409   /* ireg = (freg > op1) ? ireg - 1 : ireg */
19410   label = ix86_expand_sse_compare_and_jump (UNLE,
19411                                             freg, op1, !do_floor);
19412   expand_simple_binop (imode, do_floor ? MINUS : PLUS,
19413                        ireg, const1_rtx, ireg, 0, OPTAB_DIRECT);
19414   emit_label (label);
19415   LABEL_NUSES (label) = 1;
19416
19417   emit_move_insn (op0, ireg);
19418 }
19419
19420 /* Expand rint (IEEE round to nearest) rounding OPERAND1 and storing the
19421    result in OPERAND0.  */
19422 void
19423 ix86_expand_rint (rtx operand0, rtx operand1)
19424 {
19425   /* C code for the stuff we're doing below:
19426         if (!isless (fabs (operand1), 2**52))
19427           return operand1;
19428         tmp = copysign (2**52, operand1);
19429         return operand1 + tmp - tmp;
19430    */
19431   enum machine_mode mode = GET_MODE (operand0);
19432   rtx res, xa, label, TWO52, mask;
19433
19434   res = gen_reg_rtx (mode);
19435   emit_move_insn (res, operand1);
19436
19437   /* xa = abs (operand1) */
19438   xa = ix86_expand_sse_fabs (res, &mask);
19439
19440   /* if (!isless (xa, TWO52)) goto label; */
19441   TWO52 = ix86_gen_TWO52 (mode);
19442   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19443
19444   ix86_sse_copysign_to_positive (TWO52, TWO52, res, mask);
19445
19446   expand_simple_binop (mode, PLUS, res, TWO52, res, 0, OPTAB_DIRECT);
19447   expand_simple_binop (mode, MINUS, res, TWO52, res, 0, OPTAB_DIRECT);
19448
19449   emit_label (label);
19450   LABEL_NUSES (label) = 1;
19451
19452   emit_move_insn (operand0, res);
19453 }
19454
19455 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
19456    into OPERAND0.  */
19457 void
19458 ix86_expand_floorceildf_32 (rtx operand0, rtx operand1, bool do_floor)
19459 {
19460   /* C code for the stuff we expand below.
19461         double xa = fabs (x), x2;
19462         if (!isless (xa, TWO52))
19463           return x;
19464         xa = xa + TWO52 - TWO52;
19465         x2 = copysign (xa, x);
19466      Compensate.  Floor:
19467         if (x2 > x)
19468           x2 -= 1;
19469      Compensate.  Ceil:
19470         if (x2 < x)
19471           x2 += 1;
19472         return x2;
19473    */
19474   enum machine_mode mode = GET_MODE (operand0);
19475   rtx xa, TWO52, tmp, label, one, res, mask;
19476
19477   TWO52 = ix86_gen_TWO52 (mode);
19478
19479   /* Temporary for holding the result, initialized to the input
19480      operand to ease control flow.  */
19481   res = gen_reg_rtx (mode);
19482   emit_move_insn (res, operand1);
19483
19484   /* xa = abs (operand1) */
19485   xa = ix86_expand_sse_fabs (res, &mask);
19486
19487   /* if (!isless (xa, TWO52)) goto label; */
19488   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19489
19490   /* xa = xa + TWO52 - TWO52; */
19491   expand_simple_binop (mode, PLUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
19492   expand_simple_binop (mode, MINUS, xa, TWO52, xa, 0, OPTAB_DIRECT);
19493
19494   /* xa = copysign (xa, operand1) */
19495   ix86_sse_copysign_to_positive (xa, xa, res, mask);
19496
19497   /* generate 1.0 */
19498   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
19499
19500   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
19501   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
19502   emit_insn (gen_rtx_SET (VOIDmode, tmp,
19503                           gen_rtx_AND (mode, one, tmp)));
19504   expand_simple_binop (mode, do_floor ? MINUS : PLUS,
19505                        xa, tmp, res, 0, OPTAB_DIRECT);
19506
19507   emit_label (label);
19508   LABEL_NUSES (label) = 1;
19509
19510   emit_move_insn (operand0, res);
19511 }
19512
19513 /* Expand SSE2 sequence for computing floor or ceil from OPERAND1 storing
19514    into OPERAND0.  */
19515 void
19516 ix86_expand_floorceil (rtx operand0, rtx operand1, bool do_floor)
19517 {
19518   /* C code for the stuff we expand below.
19519         double xa = fabs (x), x2;
19520         if (!isless (xa, TWO52))
19521           return x;
19522         x2 = (double)(long)x;
19523      Compensate.  Floor:
19524         if (x2 > x)
19525           x2 -= 1;
19526      Compensate.  Ceil:
19527         if (x2 < x)
19528           x2 += 1;
19529         return x2;
19530    */
19531   enum machine_mode mode = GET_MODE (operand0);
19532   rtx xa, xi, TWO52, tmp, label, one, res;
19533
19534   TWO52 = ix86_gen_TWO52 (mode);
19535
19536   /* Temporary for holding the result, initialized to the input
19537      operand to ease control flow.  */
19538   res = gen_reg_rtx (mode);
19539   emit_move_insn (res, operand1);
19540
19541   /* xa = abs (operand1) */
19542   xa = ix86_expand_sse_fabs (res, NULL);
19543
19544   /* if (!isless (xa, TWO52)) goto label; */
19545   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19546
19547   /* xa = (double)(long)x */
19548   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
19549   expand_fix (xi, res, 0);
19550   expand_float (xa, xi, 0);
19551
19552   /* generate 1.0 */
19553   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
19554
19555   /* Compensate: xa = xa - (xa > operand1 ? 1 : 0) */
19556   tmp = ix86_expand_sse_compare_mask (UNGT, xa, res, !do_floor);
19557   emit_insn (gen_rtx_SET (VOIDmode, tmp,
19558                           gen_rtx_AND (mode, one, tmp)));
19559   expand_simple_binop (mode, do_floor ? MINUS : PLUS,
19560                        xa, tmp, res, 0, OPTAB_DIRECT);
19561
19562   emit_label (label);
19563   LABEL_NUSES (label) = 1;
19564
19565   emit_move_insn (operand0, res);
19566 }
19567
19568 /* Expand SSE sequence for computing round from OPERAND1 storing
19569    into OPERAND0.  Sequence that works without relying on DImode truncation
19570    via cvttsd2siq that is only available on 64bit targets.  */
19571 void
19572 ix86_expand_rounddf_32 (rtx operand0, rtx operand1)
19573 {
19574   /* C code for the stuff we expand below.
19575         double xa = fabs (x), xa2, x2;
19576         if (!isless (xa, TWO52))
19577           return x;
19578      Using the absolute value and copying back sign makes
19579      -0.0 -> -0.0 correct.
19580         xa2 = xa + TWO52 - TWO52;
19581      Compensate.
19582         dxa = xa2 - xa;
19583         if (dxa <= -0.5)
19584           xa2 += 1;
19585         else if (dxa > 0.5)
19586           xa2 -= 1;
19587         x2 = copysign (xa2, x);
19588         return x2;
19589    */
19590   enum machine_mode mode = GET_MODE (operand0);
19591   rtx xa, xa2, dxa, TWO52, tmp, label, half, mhalf, one, res, mask;
19592
19593   TWO52 = ix86_gen_TWO52 (mode);
19594
19595   /* Temporary for holding the result, initialized to the input
19596      operand to ease control flow.  */
19597   res = gen_reg_rtx (mode);
19598   emit_move_insn (res, operand1);
19599
19600   /* xa = abs (operand1) */
19601   xa = ix86_expand_sse_fabs (res, &mask);
19602
19603   /* if (!isless (xa, TWO52)) goto label; */
19604   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19605
19606   /* xa2 = xa + TWO52 - TWO52; */
19607   xa2 = gen_reg_rtx (mode);
19608   expand_simple_binop (mode, PLUS, xa, TWO52, xa2, 0, OPTAB_DIRECT);
19609   expand_simple_binop (mode, MINUS, xa2, TWO52, xa2, 0, OPTAB_DIRECT);
19610
19611   /* dxa = xa2 - xa; */
19612   dxa = gen_reg_rtx (mode);
19613   expand_simple_binop (mode, MINUS, xa2, xa, dxa, 0, OPTAB_DIRECT);
19614
19615   /* generate 0.5, 1.0 and -0.5 */
19616   half = force_reg (mode, const_double_from_real_value (dconsthalf, mode));
19617   one = gen_reg_rtx (mode);
19618   expand_simple_binop (mode, PLUS, half, half, one, 0, OPTAB_DIRECT);
19619   mhalf = gen_reg_rtx (mode);
19620   expand_simple_binop (mode, MINUS, half, one, mhalf, 0, OPTAB_DIRECT);
19621
19622   /* Compensate.  */
19623   tmp = gen_reg_rtx (mode);
19624   /* xa2 = xa2 - (dxa > 0.5 ? 1 : 0) */
19625   tmp = ix86_expand_sse_compare_mask (UNGT, dxa, half, false);
19626   emit_insn (gen_rtx_SET (VOIDmode, tmp,
19627                           gen_rtx_AND (mode, one, tmp)));
19628   expand_simple_binop (mode, MINUS, xa2, tmp, xa2, 0, OPTAB_DIRECT);
19629   /* xa2 = xa2 + (dxa <= -0.5 ? 1 : 0) */
19630   tmp = ix86_expand_sse_compare_mask (UNGE, mhalf, dxa, false);
19631   emit_insn (gen_rtx_SET (VOIDmode, tmp,
19632                           gen_rtx_AND (mode, one, tmp)));
19633   expand_simple_binop (mode, PLUS, xa2, tmp, xa2, 0, OPTAB_DIRECT);
19634
19635   /* res = copysign (xa2, operand1) */
19636   ix86_sse_copysign_to_positive (res, xa2, force_reg (mode, operand1), mask);
19637
19638   emit_label (label);
19639   LABEL_NUSES (label) = 1;
19640
19641   emit_move_insn (operand0, res);
19642 }
19643
19644 /* Expand SSE sequence for computing trunc from OPERAND1 storing
19645    into OPERAND0.  */
19646 void
19647 ix86_expand_trunc (rtx operand0, rtx operand1)
19648 {
19649   /* C code for SSE variant we expand below.
19650         double xa = fabs (x), x2;
19651         if (!isless (xa, TWO52))
19652           return x;
19653         return (double)(long)x;
19654    */
19655   enum machine_mode mode = GET_MODE (operand0);
19656   rtx xa, xi, TWO52, label, res;
19657
19658   TWO52 = ix86_gen_TWO52 (mode);
19659
19660   /* Temporary for holding the result, initialized to the input
19661      operand to ease control flow.  */
19662   res = gen_reg_rtx (mode);
19663   emit_move_insn (res, operand1);
19664
19665   /* xa = abs (operand1) */
19666   xa = ix86_expand_sse_fabs (res, NULL);
19667
19668   /* if (!isless (xa, TWO52)) goto label; */
19669   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19670
19671   /* x = (double)(long)x */
19672   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
19673   expand_fix (xi, res, 0);
19674   expand_float (res, xi, 0);
19675
19676   emit_label (label);
19677   LABEL_NUSES (label) = 1;
19678
19679   emit_move_insn (operand0, res);
19680 }
19681
19682 /* Expand SSE sequence for computing trunc from OPERAND1 storing
19683    into OPERAND0.  */
19684 void
19685 ix86_expand_truncdf_32 (rtx operand0, rtx operand1)
19686 {
19687   enum machine_mode mode = GET_MODE (operand0);
19688   rtx xa, mask, TWO52, label, one, res, smask;
19689
19690   /* C code for SSE variant we expand below.
19691         double xa = fabs (x), x2;
19692         if (!isless (xa, TWO52))
19693           return x;
19694         xa2 = xa + TWO52 - TWO52;
19695      Compensate:
19696         if (xa2 > xa)
19697           xa2 -= 1.0;
19698         x2 = copysign (xa2, x);
19699         return x2;
19700    */
19701
19702   TWO52 = ix86_gen_TWO52 (mode);
19703
19704   /* Temporary for holding the result, initialized to the input
19705      operand to ease control flow.  */
19706   res = gen_reg_rtx (mode);
19707   emit_move_insn (res, operand1);
19708
19709   /* xa = abs (operand1) */
19710   xa = ix86_expand_sse_fabs (res, &smask);
19711
19712   /* if (!isless (xa, TWO52)) goto label; */
19713   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19714
19715   /* res = xa + TWO52 - TWO52; */
19716   expand_simple_binop (mode, PLUS, xa, TWO52, res, 0, OPTAB_DIRECT);
19717   expand_simple_binop (mode, MINUS, res, TWO52, res, 0, OPTAB_DIRECT);
19718
19719   /* generate 1.0 */
19720   one = force_reg (mode, const_double_from_real_value (dconst1, mode));
19721
19722   /* Compensate: res = xa2 - (res > xa ? 1 : 0)  */
19723   mask = ix86_expand_sse_compare_mask (UNGT, res, xa, false);
19724   emit_insn (gen_rtx_SET (VOIDmode, mask,
19725                           gen_rtx_AND (mode, mask, one)));
19726   expand_simple_binop (mode, MINUS,
19727                        res, mask, res, 0, OPTAB_DIRECT);
19728
19729   /* res = copysign (res, operand1) */
19730   ix86_sse_copysign_to_positive (res, res, force_reg (mode, operand1), smask);
19731
19732   emit_label (label);
19733   LABEL_NUSES (label) = 1;
19734
19735   emit_move_insn (operand0, res);
19736 }
19737
19738 /* Expand SSE sequence for computing round from OPERAND1 storing
19739    into OPERAND0.  */
19740 void
19741 ix86_expand_round (rtx operand0, rtx operand1)
19742 {
19743   /* C code for the stuff we're doing below:
19744         double xa = fabs (x);
19745         if (!isless (xa, TWO52))
19746           return x;
19747         xa = (double)(long)(xa + nextafter (0.5, 0.0));
19748         return copysign (xa, x);
19749    */
19750   enum machine_mode mode = GET_MODE (operand0);
19751   rtx res, TWO52, xa, label, xi, half, mask;
19752   const struct real_format *fmt;
19753   REAL_VALUE_TYPE pred_half, half_minus_pred_half;
19754
19755   /* Temporary for holding the result, initialized to the input
19756      operand to ease control flow.  */
19757   res = gen_reg_rtx (mode);
19758   emit_move_insn (res, operand1);
19759
19760   TWO52 = ix86_gen_TWO52 (mode);
19761   xa = ix86_expand_sse_fabs (res, &mask);
19762   label = ix86_expand_sse_compare_and_jump (UNLE, TWO52, xa, false);
19763
19764   /* load nextafter (0.5, 0.0) */
19765   fmt = REAL_MODE_FORMAT (mode);
19766   real_2expN (&half_minus_pred_half, -(fmt->p) - 1);
19767   REAL_ARITHMETIC (pred_half, MINUS_EXPR, dconsthalf, half_minus_pred_half);
19768
19769   /* xa = xa + 0.5 */
19770   half = force_reg (mode, const_double_from_real_value (pred_half, mode));
19771   expand_simple_binop (mode, PLUS, xa, half, xa, 0, OPTAB_DIRECT);
19772
19773   /* xa = (double)(int64_t)xa */
19774   xi = gen_reg_rtx (mode == DFmode ? DImode : SImode);
19775   expand_fix (xi, xa, 0);
19776   expand_float (xa, xi, 0);
19777
19778   /* res = copysign (xa, operand1) */
19779   ix86_sse_copysign_to_positive (res, xa, force_reg (mode, operand1), mask);
19780
19781   emit_label (label);
19782   LABEL_NUSES (label) = 1;
19783
19784   emit_move_insn (operand0, res);
19785 }
19786
19787 #include "gt-i386.h"