OSDN Git Service

* config/rs6000/darwin.h (SUBTARGET_OVERRIDE_OPTIONS): Add
[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 k6_cost = {
338   COSTS_N_INSNS (1),                    /* cost of an add instruction */
339   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
340   COSTS_N_INSNS (1),                    /* 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 (3),                   /*                               HI */
344    COSTS_N_INSNS (3),                   /*                               SI */
345    COSTS_N_INSNS (3),                   /*                               DI */
346    COSTS_N_INSNS (3)},                  /*                               other */
347   0,                                    /* cost of multiply per each bit set */
348   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
349    COSTS_N_INSNS (18),                  /*                          HI */
350    COSTS_N_INSNS (18),                  /*                          SI */
351    COSTS_N_INSNS (18),                  /*                          DI */
352    COSTS_N_INSNS (18)},                 /*                          other */
353   COSTS_N_INSNS (2),                    /* cost of movsx */
354   COSTS_N_INSNS (2),                    /* cost of movzx */
355   8,                                    /* "large" insn */
356   4,                                    /* MOVE_RATIO */
357   3,                                    /* cost for loading QImode using movzbl */
358   {4, 5, 4},                            /* cost of loading integer registers
359                                            in QImode, HImode and SImode.
360                                            Relative to reg-reg move (2).  */
361   {2, 3, 2},                            /* cost of storing integer registers */
362   4,                                    /* cost of reg,reg fld/fst */
363   {6, 6, 6},                            /* cost of loading fp registers
364                                            in SFmode, DFmode and XFmode */
365   {4, 4, 4},                            /* cost of storing fp registers
366                                            in SFmode, DFmode and XFmode */
367   2,                                    /* cost of moving MMX register */
368   {2, 2},                               /* cost of loading MMX registers
369                                            in SImode and DImode */
370   {2, 2},                               /* cost of storing MMX registers
371                                            in SImode and DImode */
372   2,                                    /* cost of moving SSE register */
373   {2, 2, 8},                            /* cost of loading SSE registers
374                                            in SImode, DImode and TImode */
375   {2, 2, 8},                            /* cost of storing SSE registers
376                                            in SImode, DImode and TImode */
377   6,                                    /* MMX or SSE register to integer */
378   32,                                   /* size of prefetch block */
379   1,                                    /* number of parallel prefetches */
380   1,                                    /* Branch cost */
381   COSTS_N_INSNS (2),                    /* cost of FADD and FSUB insns.  */
382   COSTS_N_INSNS (2),                    /* cost of FMUL instruction.  */
383   COSTS_N_INSNS (56),                   /* cost of FDIV instruction.  */
384   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
385   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
386   COSTS_N_INSNS (56),                   /* cost of FSQRT instruction.  */
387 };
388
389 static const
390 struct processor_costs athlon_cost = {
391   COSTS_N_INSNS (1),                    /* cost of an add instruction */
392   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
393   COSTS_N_INSNS (1),                    /* variable shift costs */
394   COSTS_N_INSNS (1),                    /* constant shift costs */
395   {COSTS_N_INSNS (5),                   /* cost of starting multiply for QI */
396    COSTS_N_INSNS (5),                   /*                               HI */
397    COSTS_N_INSNS (5),                   /*                               SI */
398    COSTS_N_INSNS (5),                   /*                               DI */
399    COSTS_N_INSNS (5)},                  /*                               other */
400   0,                                    /* cost of multiply per each bit set */
401   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
402    COSTS_N_INSNS (26),                  /*                          HI */
403    COSTS_N_INSNS (42),                  /*                          SI */
404    COSTS_N_INSNS (74),                  /*                          DI */
405    COSTS_N_INSNS (74)},                 /*                          other */
406   COSTS_N_INSNS (1),                    /* cost of movsx */
407   COSTS_N_INSNS (1),                    /* cost of movzx */
408   8,                                    /* "large" insn */
409   9,                                    /* MOVE_RATIO */
410   4,                                    /* cost for loading QImode using movzbl */
411   {3, 4, 3},                            /* cost of loading integer registers
412                                            in QImode, HImode and SImode.
413                                            Relative to reg-reg move (2).  */
414   {3, 4, 3},                            /* cost of storing integer registers */
415   4,                                    /* cost of reg,reg fld/fst */
416   {4, 4, 12},                           /* cost of loading fp registers
417                                            in SFmode, DFmode and XFmode */
418   {6, 6, 8},                            /* cost of storing fp registers
419                                            in SFmode, DFmode and XFmode */
420   2,                                    /* cost of moving MMX register */
421   {4, 4},                               /* cost of loading MMX registers
422                                            in SImode and DImode */
423   {4, 4},                               /* cost of storing MMX registers
424                                            in SImode and DImode */
425   2,                                    /* cost of moving SSE register */
426   {4, 4, 6},                            /* cost of loading SSE registers
427                                            in SImode, DImode and TImode */
428   {4, 4, 5},                            /* cost of storing SSE registers
429                                            in SImode, DImode and TImode */
430   5,                                    /* MMX or SSE register to integer */
431   64,                                   /* size of prefetch block */
432   6,                                    /* number of parallel prefetches */
433   5,                                    /* Branch cost */
434   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
435   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
436   COSTS_N_INSNS (24),                   /* cost of FDIV instruction.  */
437   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
438   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
439   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
440 };
441
442 static const
443 struct processor_costs k8_cost = {
444   COSTS_N_INSNS (1),                    /* cost of an add instruction */
445   COSTS_N_INSNS (2),                    /* cost of a lea instruction */
446   COSTS_N_INSNS (1),                    /* variable shift costs */
447   COSTS_N_INSNS (1),                    /* constant shift costs */
448   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
449    COSTS_N_INSNS (4),                   /*                               HI */
450    COSTS_N_INSNS (3),                   /*                               SI */
451    COSTS_N_INSNS (4),                   /*                               DI */
452    COSTS_N_INSNS (5)},                  /*                               other */
453   0,                                    /* cost of multiply per each bit set */
454   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
455    COSTS_N_INSNS (26),                  /*                          HI */
456    COSTS_N_INSNS (42),                  /*                          SI */
457    COSTS_N_INSNS (74),                  /*                          DI */
458    COSTS_N_INSNS (74)},                 /*                          other */
459   COSTS_N_INSNS (1),                    /* cost of movsx */
460   COSTS_N_INSNS (1),                    /* cost of movzx */
461   8,                                    /* "large" insn */
462   9,                                    /* MOVE_RATIO */
463   4,                                    /* cost for loading QImode using movzbl */
464   {3, 4, 3},                            /* cost of loading integer registers
465                                            in QImode, HImode and SImode.
466                                            Relative to reg-reg move (2).  */
467   {3, 4, 3},                            /* cost of storing integer registers */
468   4,                                    /* cost of reg,reg fld/fst */
469   {4, 4, 12},                           /* cost of loading fp registers
470                                            in SFmode, DFmode and XFmode */
471   {6, 6, 8},                            /* cost of storing fp registers
472                                            in SFmode, DFmode and XFmode */
473   2,                                    /* cost of moving MMX register */
474   {3, 3},                               /* cost of loading MMX registers
475                                            in SImode and DImode */
476   {4, 4},                               /* cost of storing MMX registers
477                                            in SImode and DImode */
478   2,                                    /* cost of moving SSE register */
479   {4, 3, 6},                            /* cost of loading SSE registers
480                                            in SImode, DImode and TImode */
481   {4, 4, 5},                            /* cost of storing SSE registers
482                                            in SImode, DImode and TImode */
483   5,                                    /* MMX or SSE register to integer */
484   64,                                   /* size of prefetch block */
485   6,                                    /* number of parallel prefetches */
486   5,                                    /* Branch cost */
487   COSTS_N_INSNS (4),                    /* cost of FADD and FSUB insns.  */
488   COSTS_N_INSNS (4),                    /* cost of FMUL instruction.  */
489   COSTS_N_INSNS (19),                   /* cost of FDIV instruction.  */
490   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
491   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
492   COSTS_N_INSNS (35),                   /* cost of FSQRT instruction.  */
493 };
494
495 static const
496 struct processor_costs pentium4_cost = {
497   COSTS_N_INSNS (1),                    /* cost of an add instruction */
498   COSTS_N_INSNS (3),                    /* cost of a lea instruction */
499   COSTS_N_INSNS (4),                    /* variable shift costs */
500   COSTS_N_INSNS (4),                    /* constant shift costs */
501   {COSTS_N_INSNS (15),                  /* cost of starting multiply for QI */
502    COSTS_N_INSNS (15),                  /*                               HI */
503    COSTS_N_INSNS (15),                  /*                               SI */
504    COSTS_N_INSNS (15),                  /*                               DI */
505    COSTS_N_INSNS (15)},                 /*                               other */
506   0,                                    /* cost of multiply per each bit set */
507   {COSTS_N_INSNS (56),                  /* cost of a divide/mod for QI */
508    COSTS_N_INSNS (56),                  /*                          HI */
509    COSTS_N_INSNS (56),                  /*                          SI */
510    COSTS_N_INSNS (56),                  /*                          DI */
511    COSTS_N_INSNS (56)},                 /*                          other */
512   COSTS_N_INSNS (1),                    /* cost of movsx */
513   COSTS_N_INSNS (1),                    /* cost of movzx */
514   16,                                   /* "large" insn */
515   6,                                    /* MOVE_RATIO */
516   2,                                    /* cost for loading QImode using movzbl */
517   {4, 5, 4},                            /* cost of loading integer registers
518                                            in QImode, HImode and SImode.
519                                            Relative to reg-reg move (2).  */
520   {2, 3, 2},                            /* cost of storing integer registers */
521   2,                                    /* cost of reg,reg fld/fst */
522   {2, 2, 6},                            /* cost of loading fp registers
523                                            in SFmode, DFmode and XFmode */
524   {4, 4, 6},                            /* cost of storing fp registers
525                                            in SFmode, DFmode and XFmode */
526   2,                                    /* cost of moving MMX register */
527   {2, 2},                               /* cost of loading MMX registers
528                                            in SImode and DImode */
529   {2, 2},                               /* cost of storing MMX registers
530                                            in SImode and DImode */
531   12,                                   /* cost of moving SSE register */
532   {12, 12, 12},                         /* cost of loading SSE registers
533                                            in SImode, DImode and TImode */
534   {2, 2, 8},                            /* cost of storing SSE registers
535                                            in SImode, DImode and TImode */
536   10,                                   /* MMX or SSE register to integer */
537   64,                                   /* size of prefetch block */
538   6,                                    /* number of parallel prefetches */
539   2,                                    /* Branch cost */
540   COSTS_N_INSNS (5),                    /* cost of FADD and FSUB insns.  */
541   COSTS_N_INSNS (7),                    /* cost of FMUL instruction.  */
542   COSTS_N_INSNS (43),                   /* cost of FDIV instruction.  */
543   COSTS_N_INSNS (2),                    /* cost of FABS instruction.  */
544   COSTS_N_INSNS (2),                    /* cost of FCHS instruction.  */
545   COSTS_N_INSNS (43),                   /* cost of FSQRT instruction.  */
546 };
547
548 static const
549 struct processor_costs nocona_cost = {
550   COSTS_N_INSNS (1),                    /* cost of an add instruction */
551   COSTS_N_INSNS (1),                    /* cost of a lea instruction */
552   COSTS_N_INSNS (1),                    /* variable shift costs */
553   COSTS_N_INSNS (1),                    /* constant shift costs */
554   {COSTS_N_INSNS (10),                  /* cost of starting multiply for QI */
555    COSTS_N_INSNS (10),                  /*                               HI */
556    COSTS_N_INSNS (10),                  /*                               SI */
557    COSTS_N_INSNS (10),                  /*                               DI */
558    COSTS_N_INSNS (10)},                 /*                               other */
559   0,                                    /* cost of multiply per each bit set */
560   {COSTS_N_INSNS (66),                  /* cost of a divide/mod for QI */
561    COSTS_N_INSNS (66),                  /*                          HI */
562    COSTS_N_INSNS (66),                  /*                          SI */
563    COSTS_N_INSNS (66),                  /*                          DI */
564    COSTS_N_INSNS (66)},                 /*                          other */
565   COSTS_N_INSNS (1),                    /* cost of movsx */
566   COSTS_N_INSNS (1),                    /* cost of movzx */
567   16,                                   /* "large" insn */
568   17,                                   /* MOVE_RATIO */
569   4,                                    /* cost for loading QImode using movzbl */
570   {4, 4, 4},                            /* cost of loading integer registers
571                                            in QImode, HImode and SImode.
572                                            Relative to reg-reg move (2).  */
573   {4, 4, 4},                            /* cost of storing integer registers */
574   3,                                    /* cost of reg,reg fld/fst */
575   {12, 12, 12},                         /* cost of loading fp registers
576                                            in SFmode, DFmode and XFmode */
577   {4, 4, 4},                            /* cost of storing fp registers
578                                            in SFmode, DFmode and XFmode */
579   6,                                    /* cost of moving MMX register */
580   {12, 12},                             /* cost of loading MMX registers
581                                            in SImode and DImode */
582   {12, 12},                             /* cost of storing MMX registers
583                                            in SImode and DImode */
584   6,                                    /* cost of moving SSE register */
585   {12, 12, 12},                         /* cost of loading SSE registers
586                                            in SImode, DImode and TImode */
587   {12, 12, 12},                         /* cost of storing SSE registers
588                                            in SImode, DImode and TImode */
589   8,                                    /* MMX or SSE register to integer */
590   128,                                  /* size of prefetch block */
591   8,                                    /* number of parallel prefetches */
592   1,                                    /* Branch cost */
593   COSTS_N_INSNS (6),                    /* cost of FADD and FSUB insns.  */
594   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
595   COSTS_N_INSNS (40),                   /* cost of FDIV instruction.  */
596   COSTS_N_INSNS (3),                    /* cost of FABS instruction.  */
597   COSTS_N_INSNS (3),                    /* cost of FCHS instruction.  */
598   COSTS_N_INSNS (44),                   /* cost of FSQRT instruction.  */
599 };
600
601 /* Generic64 should produce code tuned for Nocona and K8.  */
602 static const
603 struct processor_costs generic64_cost = {
604   COSTS_N_INSNS (1),                    /* cost of an add instruction */
605   /* On all chips taken into consideration lea is 2 cycles and more.  With
606      this cost however our current implementation of synth_mult results in
607      use of unnecessary temporary registers causing regression on several
608      SPECfp benchmarks.  */
609   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
610   COSTS_N_INSNS (1),                    /* variable shift costs */
611   COSTS_N_INSNS (1),                    /* constant shift costs */
612   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
613    COSTS_N_INSNS (4),                   /*                               HI */
614    COSTS_N_INSNS (3),                   /*                               SI */
615    COSTS_N_INSNS (4),                   /*                               DI */
616    COSTS_N_INSNS (2)},                  /*                               other */
617   0,                                    /* cost of multiply per each bit set */
618   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
619    COSTS_N_INSNS (26),                  /*                          HI */
620    COSTS_N_INSNS (42),                  /*                          SI */
621    COSTS_N_INSNS (74),                  /*                          DI */
622    COSTS_N_INSNS (74)},                 /*                          other */
623   COSTS_N_INSNS (1),                    /* cost of movsx */
624   COSTS_N_INSNS (1),                    /* cost of movzx */
625   8,                                    /* "large" insn */
626   17,                                   /* MOVE_RATIO */
627   4,                                    /* cost for loading QImode using movzbl */
628   {4, 4, 4},                            /* cost of loading integer registers
629                                            in QImode, HImode and SImode.
630                                            Relative to reg-reg move (2).  */
631   {4, 4, 4},                            /* cost of storing integer registers */
632   4,                                    /* cost of reg,reg fld/fst */
633   {12, 12, 12},                         /* cost of loading fp registers
634                                            in SFmode, DFmode and XFmode */
635   {6, 6, 8},                            /* cost of storing fp registers
636                                            in SFmode, DFmode and XFmode */
637   2,                                    /* cost of moving MMX register */
638   {8, 8},                               /* cost of loading MMX registers
639                                            in SImode and DImode */
640   {8, 8},                               /* cost of storing MMX registers
641                                            in SImode and DImode */
642   2,                                    /* cost of moving SSE register */
643   {8, 8, 8},                            /* cost of loading SSE registers
644                                            in SImode, DImode and TImode */
645   {8, 8, 8},                            /* cost of storing SSE registers
646                                            in SImode, DImode and TImode */
647   5,                                    /* MMX or SSE register to integer */
648   64,                                   /* size of prefetch block */
649   6,                                    /* number of parallel prefetches */
650   /* Benchmarks shows large regressions on K8 sixtrack benchmark when this value
651      is increased to perhaps more appropriate value of 5.  */
652   3,                                    /* Branch cost */
653   COSTS_N_INSNS (8),                    /* cost of FADD and FSUB insns.  */
654   COSTS_N_INSNS (8),                    /* cost of FMUL instruction.  */
655   COSTS_N_INSNS (20),                   /* cost of FDIV instruction.  */
656   COSTS_N_INSNS (8),                    /* cost of FABS instruction.  */
657   COSTS_N_INSNS (8),                    /* cost of FCHS instruction.  */
658   COSTS_N_INSNS (40),                   /* cost of FSQRT instruction.  */
659 };
660
661 /* Generic32 should produce code tuned for Athlon, PPro, Pentium4, Nocona and K8.  */
662 static const
663 struct processor_costs generic32_cost = {
664   COSTS_N_INSNS (1),                    /* cost of an add instruction */
665   COSTS_N_INSNS (1) + 1,                /* cost of a lea instruction */
666   COSTS_N_INSNS (1),                    /* variable shift costs */
667   COSTS_N_INSNS (1),                    /* constant shift costs */
668   {COSTS_N_INSNS (3),                   /* cost of starting multiply for QI */
669    COSTS_N_INSNS (4),                   /*                               HI */
670    COSTS_N_INSNS (3),                   /*                               SI */
671    COSTS_N_INSNS (4),                   /*                               DI */
672    COSTS_N_INSNS (2)},                  /*                               other */
673   0,                                    /* cost of multiply per each bit set */
674   {COSTS_N_INSNS (18),                  /* cost of a divide/mod for QI */
675    COSTS_N_INSNS (26),                  /*                          HI */
676    COSTS_N_INSNS (42),                  /*                          SI */
677    COSTS_N_INSNS (74),                  /*                          DI */
678    COSTS_N_INSNS (74)},                 /*                          other */
679   COSTS_N_INSNS (1),                    /* cost of movsx */
680   COSTS_N_INSNS (1),                    /* cost of movzx */
681   8,                                    /* "large" insn */
682   17,                                   /* MOVE_RATIO */
683   4,                                    /* cost for loading QImode using movzbl */
684   {4, 4, 4},                            /* cost of loading integer registers
685                                            in QImode, HImode and SImode.
686                                            Relative to reg-reg move (2).  */
687   {4, 4, 4},                            /* cost of storing integer registers */
688   4,                                    /* cost of reg,reg fld/fst */
689   {12, 12, 12},                         /* cost of loading fp registers
690                                            in SFmode, DFmode and XFmode */
691   {6, 6, 8},                            /* cost of storing fp registers
692                                            in SFmode, DFmode and XFmode */
693   2,                                    /* cost of moving MMX register */
694   {8, 8},                               /* cost of loading MMX registers
695                                            in SImode and DImode */
696   {8, 8},                               /* cost of storing MMX registers
697                                            in SImode and DImode */
698   2,                                    /* cost of moving SSE register */
699   {8, 8, 8},                            /* cost of loading SSE registers
700                                            in SImode, DImode and TImode */
701   {8, 8, 8},                            /* cost of storing SSE registers
702                                            in SImode, DImode and TImode */
703   5,                                    /* MMX or SSE register to integer */
704   64,                                   /* size of prefetch block */
705   6,                                    /* number of parallel prefetches */
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 const struct processor_costs *ix86_cost = &pentium_cost;
716
717 /* Processor feature/optimization bitmasks.  */
718 #define m_386 (1<<PROCESSOR_I386)
719 #define m_486 (1<<PROCESSOR_I486)
720 #define m_PENT (1<<PROCESSOR_PENTIUM)
721 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
722 #define m_K6  (1<<PROCESSOR_K6)
723 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
724 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
725 #define m_K8  (1<<PROCESSOR_K8)
726 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
727 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
728 #define m_GENERIC32 (1<<PROCESSOR_GENERIC32)
729 #define m_GENERIC64 (1<<PROCESSOR_GENERIC64)
730 #define m_GENERIC (m_GENERIC32 | m_GENERIC64)
731
732 /* Generic instruction choice should be common subset of supported CPUs
733    (PPro/PENT4/NOCONA/Athlon/K8).  */
734
735 /* Leave is not affecting Nocona SPEC2000 results negatively, so enabling for
736    Generic64 seems like good code size tradeoff.  We can't enable it for 32bit
737    generic because it is not working well with PPro base chips.  */
738 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8 | m_GENERIC64;
739 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
740 const int x86_zero_extend_with_and = m_486 | m_PENT;
741 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC /* m_386 | m_K6 */;
742 const int x86_double_with_add = ~m_386;
743 const int x86_use_bit_test = m_386;
744 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6 | m_GENERIC;
745 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
746 const int x86_3dnow_a = m_ATHLON_K8;
747 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
748 /* Branch hints were put in P4 based on simulation result. But
749    after P4 was made, no performance benefit was observed with
750    branch hints. It also increases the code size. As the result,
751    icc never generates branch hints.  */
752 const int x86_branch_hints = 0;
753 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4 | m_NOCONA | m_GENERIC32; /*m_GENERIC | m_ATHLON_K8 ? */
754 /* We probably ought to watch for partial register stalls on Generic32
755    compilation setting as well.  However in current implementation the
756    partial register stalls are not eliminated very well - they can
757    be introduced via subregs synthesized by combine and can happen
758    in caller/callee saving sequences.
759    Because this option pays back little on PPro based chips and is in conflict
760    with partial reg. dependencies used by Athlon/P4 based chips, it is better
761    to leave it off for generic32 for now.  */
762 const int x86_partial_reg_stall = m_PPRO;
763 const int x86_partial_flag_reg_stall = m_GENERIC;
764 const int x86_use_himode_fiop = m_386 | m_486 | m_K6;
765 const int x86_use_simode_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT | m_GENERIC);
766 const int x86_use_mov0 = m_K6;
767 const int x86_use_cltd = ~(m_PENT | m_K6 | m_GENERIC);
768 const int x86_read_modify_write = ~m_PENT;
769 const int x86_read_modify = ~(m_PENT | m_PPRO);
770 const int x86_split_long_moves = m_PPRO;
771 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8 | m_GENERIC; /* m_PENT4 ? */
772 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
773 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
774 const int x86_qimode_math = ~(0);
775 const int x86_promote_qi_regs = 0;
776 /* On PPro this flag is meant to avoid partial register stalls.  Just like
777    the x86_partial_reg_stall this option might be considered for Generic32
778    if our scheme for avoiding partial stalls was more effective.  */
779 const int x86_himode_math = ~(m_PPRO);
780 const int x86_promote_hi_regs = m_PPRO;
781 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA | m_GENERIC;
782 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
783 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4 | m_NOCONA | m_GENERIC;
784 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4 | m_NOCONA | m_GENERIC;
785 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC);
786 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
787 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
788 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
789 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
790 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO | m_GENERIC;
791 const int x86_shift1 = ~m_486;
792 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
793 /* In Generic model we have an conflict here in between PPro/Pentium4 based chips
794    that thread 128bit SSE registers as single units versus K8 based chips that
795    divide SSE registers to two 64bit halves.
796    x86_sse_partial_reg_dependency promote all store destinations to be 128bit
797    to allow register renaming on 128bit SSE units, but usually results in one
798    extra microop on 64bit SSE units.  Experimental results shows that disabling
799    this option on P4 brings over 20% SPECfp regression, while enabling it on
800    K8 brings roughly 2.4% regression that can be partly masked by careful scheduling
801    of moves.  */
802 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC;
803 /* Set for machines where the type and dependencies are resolved on SSE
804    register parts instead of whole registers, so we may maintain just
805    lower part of scalar values in proper format leaving the upper part
806    undefined.  */
807 const int x86_sse_split_regs = m_ATHLON_K8;
808 const int x86_sse_typeless_stores = m_ATHLON_K8;
809 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4 | m_NOCONA;
810 const int x86_use_ffreep = m_ATHLON_K8;
811 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6;
812 const int x86_use_incdec = ~(m_PENT4 | m_NOCONA | m_GENERIC);
813
814 /* ??? Allowing interunit moves makes it all too easy for the compiler to put
815    integer data in xmm registers.  Which results in pretty abysmal code.  */
816 const int x86_inter_unit_moves = 0 /* ~(m_ATHLON_K8) */;
817
818 const int x86_ext_80387_constants = m_K6 | m_ATHLON | m_PENT4 | m_NOCONA | m_PPRO | m_GENERIC32;
819 /* Some CPU cores are not able to predict more than 4 branch instructions in
820    the 16 byte window.  */
821 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_GENERIC;
822 const int x86_schedule = m_PPRO | m_ATHLON_K8 | m_K6 | m_PENT | m_GENERIC;
823 const int x86_use_bt = m_ATHLON_K8;
824 /* Compare and exchange was added for 80486.  */
825 const int x86_cmpxchg = ~m_386;
826 /* Compare and exchange 8 bytes was added for pentium.  */
827 const int x86_cmpxchg8b = ~(m_386 | m_486);
828 /* Compare and exchange 16 bytes was added for nocona.  */
829 const int x86_cmpxchg16b = m_NOCONA;
830 /* Exchange and add was added for 80486.  */
831 const int x86_xadd = ~m_386;
832 const int x86_pad_returns = m_ATHLON_K8 | m_GENERIC;
833
834 /* In case the average insn count for single function invocation is
835    lower than this constant, emit fast (but longer) prologue and
836    epilogue code.  */
837 #define FAST_PROLOGUE_INSN_COUNT 20
838
839 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
840 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
841 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
842 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
843
844 /* Array of the smallest class containing reg number REGNO, indexed by
845    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
846
847 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
848 {
849   /* ax, dx, cx, bx */
850   AREG, DREG, CREG, BREG,
851   /* si, di, bp, sp */
852   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
853   /* FP registers */
854   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
855   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
856   /* arg pointer */
857   NON_Q_REGS,
858   /* flags, fpsr, dirflag, frame */
859   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
860   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
861   SSE_REGS, SSE_REGS,
862   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
863   MMX_REGS, MMX_REGS,
864   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
865   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
866   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
867   SSE_REGS, SSE_REGS,
868 };
869
870 /* The "default" register map used in 32bit mode.  */
871
872 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
873 {
874   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
875   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
876   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
877   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
878   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
879   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
880   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
881 };
882
883 static int const x86_64_int_parameter_registers[6] =
884 {
885   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
886   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
887 };
888
889 static int const x86_64_int_return_registers[4] =
890 {
891   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
892 };
893
894 /* The "default" register map used in 64bit mode.  */
895 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
896 {
897   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
898   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
899   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
900   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
901   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
902   8,9,10,11,12,13,14,15,                /* extended integer registers */
903   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
904 };
905
906 /* Define the register numbers to be used in Dwarf debugging information.
907    The SVR4 reference port C compiler uses the following register numbers
908    in its Dwarf output code:
909         0 for %eax (gcc regno = 0)
910         1 for %ecx (gcc regno = 2)
911         2 for %edx (gcc regno = 1)
912         3 for %ebx (gcc regno = 3)
913         4 for %esp (gcc regno = 7)
914         5 for %ebp (gcc regno = 6)
915         6 for %esi (gcc regno = 4)
916         7 for %edi (gcc regno = 5)
917    The following three DWARF register numbers are never generated by
918    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
919    believes these numbers have these meanings.
920         8  for %eip    (no gcc equivalent)
921         9  for %eflags (gcc regno = 17)
922         10 for %trapno (no gcc equivalent)
923    It is not at all clear how we should number the FP stack registers
924    for the x86 architecture.  If the version of SDB on x86/svr4 were
925    a bit less brain dead with respect to floating-point then we would
926    have a precedent to follow with respect to DWARF register numbers
927    for x86 FP registers, but the SDB on x86/svr4 is so completely
928    broken with respect to FP registers that it is hardly worth thinking
929    of it as something to strive for compatibility with.
930    The version of x86/svr4 SDB I have at the moment does (partially)
931    seem to believe that DWARF register number 11 is associated with
932    the x86 register %st(0), but that's about all.  Higher DWARF
933    register numbers don't seem to be associated with anything in
934    particular, and even for DWARF regno 11, SDB only seems to under-
935    stand that it should say that a variable lives in %st(0) (when
936    asked via an `=' command) if we said it was in DWARF regno 11,
937    but SDB still prints garbage when asked for the value of the
938    variable in question (via a `/' command).
939    (Also note that the labels SDB prints for various FP stack regs
940    when doing an `x' command are all wrong.)
941    Note that these problems generally don't affect the native SVR4
942    C compiler because it doesn't allow the use of -O with -g and
943    because when it is *not* optimizing, it allocates a memory
944    location for each floating-point variable, and the memory
945    location is what gets described in the DWARF AT_location
946    attribute for the variable in question.
947    Regardless of the severe mental illness of the x86/svr4 SDB, we
948    do something sensible here and we use the following DWARF
949    register numbers.  Note that these are all stack-top-relative
950    numbers.
951         11 for %st(0) (gcc regno = 8)
952         12 for %st(1) (gcc regno = 9)
953         13 for %st(2) (gcc regno = 10)
954         14 for %st(3) (gcc regno = 11)
955         15 for %st(4) (gcc regno = 12)
956         16 for %st(5) (gcc regno = 13)
957         17 for %st(6) (gcc regno = 14)
958         18 for %st(7) (gcc regno = 15)
959 */
960 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
961 {
962   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
963   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
964   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
965   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
966   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
967   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
968   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
969 };
970
971 /* Test and compare insns in i386.md store the information needed to
972    generate branch and scc insns here.  */
973
974 rtx ix86_compare_op0 = NULL_RTX;
975 rtx ix86_compare_op1 = NULL_RTX;
976 rtx ix86_compare_emitted = NULL_RTX;
977
978 /* Size of the register save area.  */
979 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
980
981 /* Define the structure for the machine field in struct function.  */
982
983 struct stack_local_entry GTY(())
984 {
985   unsigned short mode;
986   unsigned short n;
987   rtx rtl;
988   struct stack_local_entry *next;
989 };
990
991 /* Structure describing stack frame layout.
992    Stack grows downward:
993
994    [arguments]
995                                               <- ARG_POINTER
996    saved pc
997
998    saved frame pointer if frame_pointer_needed
999                                               <- HARD_FRAME_POINTER
1000    [saved regs]
1001
1002    [padding1]          \
1003                         )
1004    [va_arg registers]  (
1005                         > to_allocate         <- FRAME_POINTER
1006    [frame]             (
1007                         )
1008    [padding2]          /
1009   */
1010 struct ix86_frame
1011 {
1012   int nregs;
1013   int padding1;
1014   int va_arg_size;
1015   HOST_WIDE_INT frame;
1016   int padding2;
1017   int outgoing_arguments_size;
1018   int red_zone_size;
1019
1020   HOST_WIDE_INT to_allocate;
1021   /* The offsets relative to ARG_POINTER.  */
1022   HOST_WIDE_INT frame_pointer_offset;
1023   HOST_WIDE_INT hard_frame_pointer_offset;
1024   HOST_WIDE_INT stack_pointer_offset;
1025
1026   /* When save_regs_using_mov is set, emit prologue using
1027      move instead of push instructions.  */
1028   bool save_regs_using_mov;
1029 };
1030
1031 /* Code model option.  */
1032 enum cmodel ix86_cmodel;
1033 /* Asm dialect.  */
1034 enum asm_dialect ix86_asm_dialect = ASM_ATT;
1035 /* TLS dialects.  */
1036 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
1037
1038 /* Which unit we are generating floating point math for.  */
1039 enum fpmath_unit ix86_fpmath;
1040
1041 /* Which cpu are we scheduling for.  */
1042 enum processor_type ix86_tune;
1043 /* Which instruction set architecture to use.  */
1044 enum processor_type ix86_arch;
1045
1046 /* true if sse prefetch instruction is not NOOP.  */
1047 int x86_prefetch_sse;
1048
1049 /* ix86_regparm_string as a number */
1050 static int ix86_regparm;
1051
1052 /* -mstackrealign option */
1053 extern int ix86_force_align_arg_pointer;
1054 static const char ix86_force_align_arg_pointer_string[] = "force_align_arg_pointer";
1055
1056 /* Preferred alignment for stack boundary in bits.  */
1057 unsigned int ix86_preferred_stack_boundary;
1058
1059 /* Values 1-5: see jump.c */
1060 int ix86_branch_cost;
1061
1062 /* Variables which are this size or smaller are put in the data/bss
1063    or ldata/lbss sections.  */
1064
1065 int ix86_section_threshold = 65536;
1066
1067 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
1068 char internal_label_prefix[16];
1069 int internal_label_prefix_len;
1070 \f
1071 static bool ix86_handle_option (size_t, const char *, int);
1072 static void output_pic_addr_const (FILE *, rtx, int);
1073 static void put_condition_code (enum rtx_code, enum machine_mode,
1074                                 int, int, FILE *);
1075 static const char *get_some_local_dynamic_name (void);
1076 static int get_some_local_dynamic_name_1 (rtx *, void *);
1077 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
1078 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
1079                                                    rtx *);
1080 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
1081 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
1082                                                    enum machine_mode);
1083 static rtx get_thread_pointer (int);
1084 static rtx legitimize_tls_address (rtx, enum tls_model, int);
1085 static void get_pc_thunk_name (char [32], unsigned int);
1086 static rtx gen_push (rtx);
1087 static int ix86_flags_dependent (rtx, rtx, enum attr_type);
1088 static int ix86_agi_dependent (rtx, rtx, enum attr_type);
1089 static struct machine_function * ix86_init_machine_status (void);
1090 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
1091 static int ix86_nsaved_regs (void);
1092 static void ix86_emit_save_regs (void);
1093 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
1094 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
1095 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
1096 static HOST_WIDE_INT ix86_GOT_alias_set (void);
1097 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
1098 static rtx ix86_expand_aligntest (rtx, int);
1099 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
1100 static int ix86_issue_rate (void);
1101 static int ix86_adjust_cost (rtx, rtx, rtx, int);
1102 static int ia32_multipass_dfa_lookahead (void);
1103 static void ix86_init_mmx_sse_builtins (void);
1104 static rtx x86_this_parameter (tree);
1105 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
1106                                  HOST_WIDE_INT, tree);
1107 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
1108 static void x86_file_start (void);
1109 static void ix86_reorg (void);
1110 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
1111 static tree ix86_build_builtin_va_list (void);
1112 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
1113                                          tree, int *, int);
1114 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
1115 static bool ix86_scalar_mode_supported_p (enum machine_mode);
1116 static bool ix86_vector_mode_supported_p (enum machine_mode);
1117
1118 static int ix86_address_cost (rtx);
1119 static bool ix86_cannot_force_const_mem (rtx);
1120 static rtx ix86_delegitimize_address (rtx);
1121
1122 static void i386_output_dwarf_dtprel (FILE *, int, rtx) ATTRIBUTE_UNUSED;
1123
1124 struct builtin_description;
1125 static rtx ix86_expand_sse_comi (const struct builtin_description *,
1126                                  tree, rtx);
1127 static rtx ix86_expand_sse_compare (const struct builtin_description *,
1128                                     tree, rtx);
1129 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
1130 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
1131 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
1132 static rtx ix86_expand_store_builtin (enum insn_code, tree);
1133 static rtx safe_vector_operand (rtx, enum machine_mode);
1134 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
1135 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
1136 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
1137 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
1138 static int ix86_fp_comparison_cost (enum rtx_code code);
1139 static unsigned int ix86_select_alt_pic_regnum (void);
1140 static int ix86_save_reg (unsigned int, int);
1141 static void ix86_compute_frame_layout (struct ix86_frame *);
1142 static int ix86_comp_type_attributes (tree, tree);
1143 static int ix86_function_regparm (tree, tree);
1144 const struct attribute_spec ix86_attribute_table[];
1145 static bool ix86_function_ok_for_sibcall (tree, tree);
1146 static tree ix86_handle_cconv_attribute (tree *, tree, tree, int, bool *);
1147 static int ix86_value_regno (enum machine_mode, tree, tree);
1148 static bool contains_128bit_aligned_vector_p (tree);
1149 static rtx ix86_struct_value_rtx (tree, int);
1150 static bool ix86_ms_bitfield_layout_p (tree);
1151 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
1152 static int extended_reg_mentioned_1 (rtx *, void *);
1153 static bool ix86_rtx_costs (rtx, int, int, int *);
1154 static int min_insn_size (rtx);
1155 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
1156 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
1157 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
1158                                     tree, bool);
1159 static void ix86_init_builtins (void);
1160 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
1161 static const char *ix86_mangle_fundamental_type (tree);
1162 static tree ix86_stack_protect_fail (void);
1163 static rtx ix86_internal_arg_pointer (void);
1164 static void ix86_dwarf_handle_frame_unspec (const char *, rtx, int);
1165
1166 /* This function is only used on Solaris.  */
1167 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
1168   ATTRIBUTE_UNUSED;
1169
1170 /* Register class used for passing given 64bit part of the argument.
1171    These represent classes as documented by the PS ABI, with the exception
1172    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
1173    use SF or DFmode move instead of DImode to avoid reformatting penalties.
1174
1175    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
1176    whenever possible (upper half does contain padding).
1177  */
1178 enum x86_64_reg_class
1179   {
1180     X86_64_NO_CLASS,
1181     X86_64_INTEGER_CLASS,
1182     X86_64_INTEGERSI_CLASS,
1183     X86_64_SSE_CLASS,
1184     X86_64_SSESF_CLASS,
1185     X86_64_SSEDF_CLASS,
1186     X86_64_SSEUP_CLASS,
1187     X86_64_X87_CLASS,
1188     X86_64_X87UP_CLASS,
1189     X86_64_COMPLEX_X87_CLASS,
1190     X86_64_MEMORY_CLASS
1191   };
1192 static const char * const x86_64_reg_class_name[] = {
1193   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
1194   "sseup", "x87", "x87up", "cplx87", "no"
1195 };
1196
1197 #define MAX_CLASSES 4
1198
1199 /* Table of constants used by fldpi, fldln2, etc....  */
1200 static REAL_VALUE_TYPE ext_80387_constants_table [5];
1201 static bool ext_80387_constants_init = 0;
1202 static void init_ext_80387_constants (void);
1203 static bool ix86_in_large_data_p (tree) ATTRIBUTE_UNUSED;
1204 static void ix86_encode_section_info (tree, rtx, int) ATTRIBUTE_UNUSED;
1205 static void x86_64_elf_unique_section (tree decl, int reloc) ATTRIBUTE_UNUSED;
1206 static section *x86_64_elf_select_section (tree decl, int reloc,
1207                                            unsigned HOST_WIDE_INT align)
1208                                              ATTRIBUTE_UNUSED;
1209 \f
1210 /* Initialize the GCC target structure.  */
1211 #undef TARGET_ATTRIBUTE_TABLE
1212 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
1213 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1214 #  undef TARGET_MERGE_DECL_ATTRIBUTES
1215 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
1216 #endif
1217
1218 #undef TARGET_COMP_TYPE_ATTRIBUTES
1219 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
1220
1221 #undef TARGET_INIT_BUILTINS
1222 #define TARGET_INIT_BUILTINS ix86_init_builtins
1223 #undef TARGET_EXPAND_BUILTIN
1224 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
1225
1226 #undef TARGET_ASM_FUNCTION_EPILOGUE
1227 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
1228
1229 #undef TARGET_ENCODE_SECTION_INFO
1230 #ifndef SUBTARGET_ENCODE_SECTION_INFO
1231 #define TARGET_ENCODE_SECTION_INFO ix86_encode_section_info
1232 #else
1233 #define TARGET_ENCODE_SECTION_INFO SUBTARGET_ENCODE_SECTION_INFO
1234 #endif
1235
1236 #undef TARGET_ASM_OPEN_PAREN
1237 #define TARGET_ASM_OPEN_PAREN ""
1238 #undef TARGET_ASM_CLOSE_PAREN
1239 #define TARGET_ASM_CLOSE_PAREN ""
1240
1241 #undef TARGET_ASM_ALIGNED_HI_OP
1242 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
1243 #undef TARGET_ASM_ALIGNED_SI_OP
1244 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
1245 #ifdef ASM_QUAD
1246 #undef TARGET_ASM_ALIGNED_DI_OP
1247 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1248 #endif
1249
1250 #undef TARGET_ASM_UNALIGNED_HI_OP
1251 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1252 #undef TARGET_ASM_UNALIGNED_SI_OP
1253 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1254 #undef TARGET_ASM_UNALIGNED_DI_OP
1255 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1256
1257 #undef TARGET_SCHED_ADJUST_COST
1258 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1259 #undef TARGET_SCHED_ISSUE_RATE
1260 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1261 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1262 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1263   ia32_multipass_dfa_lookahead
1264
1265 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1266 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1267
1268 #ifdef HAVE_AS_TLS
1269 #undef TARGET_HAVE_TLS
1270 #define TARGET_HAVE_TLS true
1271 #endif
1272 #undef TARGET_CANNOT_FORCE_CONST_MEM
1273 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1274 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
1275 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_rtx_true
1276
1277 #undef TARGET_DELEGITIMIZE_ADDRESS
1278 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1279
1280 #undef TARGET_MS_BITFIELD_LAYOUT_P
1281 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1282
1283 #if TARGET_MACHO
1284 #undef TARGET_BINDS_LOCAL_P
1285 #define TARGET_BINDS_LOCAL_P darwin_binds_local_p
1286 #endif
1287
1288 #undef TARGET_ASM_OUTPUT_MI_THUNK
1289 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1290 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1291 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1292
1293 #undef TARGET_ASM_FILE_START
1294 #define TARGET_ASM_FILE_START x86_file_start
1295
1296 #undef TARGET_DEFAULT_TARGET_FLAGS
1297 #define TARGET_DEFAULT_TARGET_FLAGS     \
1298   (TARGET_DEFAULT                       \
1299    | TARGET_64BIT_DEFAULT               \
1300    | TARGET_SUBTARGET_DEFAULT           \
1301    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
1302
1303 #undef TARGET_HANDLE_OPTION
1304 #define TARGET_HANDLE_OPTION ix86_handle_option
1305
1306 #undef TARGET_RTX_COSTS
1307 #define TARGET_RTX_COSTS ix86_rtx_costs
1308 #undef TARGET_ADDRESS_COST
1309 #define TARGET_ADDRESS_COST ix86_address_cost
1310
1311 #undef TARGET_FIXED_CONDITION_CODE_REGS
1312 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1313 #undef TARGET_CC_MODES_COMPATIBLE
1314 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1315
1316 #undef TARGET_MACHINE_DEPENDENT_REORG
1317 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1318
1319 #undef TARGET_BUILD_BUILTIN_VA_LIST
1320 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1321
1322 #undef TARGET_MD_ASM_CLOBBERS
1323 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1324
1325 #undef TARGET_PROMOTE_PROTOTYPES
1326 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1327 #undef TARGET_STRUCT_VALUE_RTX
1328 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1329 #undef TARGET_SETUP_INCOMING_VARARGS
1330 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1331 #undef TARGET_MUST_PASS_IN_STACK
1332 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1333 #undef TARGET_PASS_BY_REFERENCE
1334 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1335 #undef TARGET_INTERNAL_ARG_POINTER
1336 #define TARGET_INTERNAL_ARG_POINTER ix86_internal_arg_pointer
1337 #undef TARGET_DWARF_HANDLE_FRAME_UNSPEC
1338 #define TARGET_DWARF_HANDLE_FRAME_UNSPEC ix86_dwarf_handle_frame_unspec
1339
1340 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1341 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1342
1343 #undef TARGET_SCALAR_MODE_SUPPORTED_P
1344 #define TARGET_SCALAR_MODE_SUPPORTED_P ix86_scalar_mode_supported_p
1345
1346 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1347 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1348
1349 #ifdef HAVE_AS_TLS
1350 #undef TARGET_ASM_OUTPUT_DWARF_DTPREL
1351 #define TARGET_ASM_OUTPUT_DWARF_DTPREL i386_output_dwarf_dtprel
1352 #endif
1353
1354 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1355 #undef TARGET_INSERT_ATTRIBUTES
1356 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1357 #endif
1358
1359 #undef TARGET_MANGLE_FUNDAMENTAL_TYPE
1360 #define TARGET_MANGLE_FUNDAMENTAL_TYPE ix86_mangle_fundamental_type
1361
1362 #undef TARGET_STACK_PROTECT_FAIL
1363 #define TARGET_STACK_PROTECT_FAIL ix86_stack_protect_fail
1364
1365 #undef TARGET_FUNCTION_VALUE
1366 #define TARGET_FUNCTION_VALUE ix86_function_value
1367
1368 struct gcc_target targetm = TARGET_INITIALIZER;
1369
1370 \f
1371 /* The svr4 ABI for the i386 says that records and unions are returned
1372    in memory.  */
1373 #ifndef DEFAULT_PCC_STRUCT_RETURN
1374 #define DEFAULT_PCC_STRUCT_RETURN 1
1375 #endif
1376
1377 /* Implement TARGET_HANDLE_OPTION.  */
1378
1379 static bool
1380 ix86_handle_option (size_t code, const char *arg ATTRIBUTE_UNUSED, int value)
1381 {
1382   switch (code)
1383     {
1384     case OPT_m3dnow:
1385       if (!value)
1386         {
1387           target_flags &= ~MASK_3DNOW_A;
1388           target_flags_explicit |= MASK_3DNOW_A;
1389         }
1390       return true;
1391
1392     case OPT_mmmx:
1393       if (!value)
1394         {
1395           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1396           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1397         }
1398       return true;
1399
1400     case OPT_msse:
1401       if (!value)
1402         {
1403           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1404           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1405         }
1406       return true;
1407
1408     case OPT_msse2:
1409       if (!value)
1410         {
1411           target_flags &= ~MASK_SSE3;
1412           target_flags_explicit |= MASK_SSE3;
1413         }
1414       return true;
1415
1416     default:
1417       return true;
1418     }
1419 }
1420
1421 /* Sometimes certain combinations of command options do not make
1422    sense on a particular target machine.  You can define a macro
1423    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1424    defined, is executed once just after all the command options have
1425    been parsed.
1426
1427    Don't use this macro to turn on various extra optimizations for
1428    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1429
1430 void
1431 override_options (void)
1432 {
1433   int i;
1434   int ix86_tune_defaulted = 0;
1435
1436   /* Comes from final.c -- no real reason to change it.  */
1437 #define MAX_CODE_ALIGN 16
1438
1439   static struct ptt
1440     {
1441       const struct processor_costs *cost;       /* Processor costs */
1442       const int target_enable;                  /* Target flags to enable.  */
1443       const int target_disable;                 /* Target flags to disable.  */
1444       const int align_loop;                     /* Default alignments.  */
1445       const int align_loop_max_skip;
1446       const int align_jump;
1447       const int align_jump_max_skip;
1448       const int align_func;
1449     }
1450   const processor_target_table[PROCESSOR_max] =
1451     {
1452       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1453       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1454       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1455       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1456       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1457       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1458       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1459       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1460       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0},
1461       {&generic32_cost, 0, 0, 16, 7, 16, 7, 16},
1462       {&generic64_cost, 0, 0, 16, 7, 16, 7, 16}
1463     };
1464
1465   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1466   static struct pta
1467     {
1468       const char *const name;           /* processor name or nickname.  */
1469       const enum processor_type processor;
1470       const enum pta_flags
1471         {
1472           PTA_SSE = 1,
1473           PTA_SSE2 = 2,
1474           PTA_SSE3 = 4,
1475           PTA_MMX = 8,
1476           PTA_PREFETCH_SSE = 16,
1477           PTA_3DNOW = 32,
1478           PTA_3DNOW_A = 64,
1479           PTA_64BIT = 128
1480         } flags;
1481     }
1482   const processor_alias_table[] =
1483     {
1484       {"i386", PROCESSOR_I386, 0},
1485       {"i486", PROCESSOR_I486, 0},
1486       {"i586", PROCESSOR_PENTIUM, 0},
1487       {"pentium", PROCESSOR_PENTIUM, 0},
1488       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1489       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1490       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1491       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1492       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1493       {"i686", PROCESSOR_PENTIUMPRO, 0},
1494       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1495       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1496       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1497       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1498       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1499       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1500                                        | PTA_MMX | PTA_PREFETCH_SSE},
1501       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1502                                         | PTA_MMX | PTA_PREFETCH_SSE},
1503       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1504                                         | PTA_MMX | PTA_PREFETCH_SSE},
1505       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1506                                         | PTA_MMX | PTA_PREFETCH_SSE},
1507       {"k6", PROCESSOR_K6, PTA_MMX},
1508       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1509       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1510       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1511                                    | PTA_3DNOW_A},
1512       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1513                                          | PTA_3DNOW | PTA_3DNOW_A},
1514       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1515                                     | PTA_3DNOW_A | PTA_SSE},
1516       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1517                                       | PTA_3DNOW_A | PTA_SSE},
1518       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1519                                       | PTA_3DNOW_A | PTA_SSE},
1520       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1521                                | PTA_SSE | PTA_SSE2 },
1522       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1523                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1524       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1525                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1526       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1527                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1528       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1529                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1530       {"generic32", PROCESSOR_GENERIC32, 0 /* flags are only used for -march switch.  */ },
1531       {"generic64", PROCESSOR_GENERIC64, PTA_64BIT /* flags are only used for -march switch.  */ },
1532     };
1533
1534   int const pta_size = ARRAY_SIZE (processor_alias_table);
1535
1536 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1537   SUBTARGET_OVERRIDE_OPTIONS;
1538 #endif
1539
1540 #ifdef SUBSUBTARGET_OVERRIDE_OPTIONS
1541   SUBSUBTARGET_OVERRIDE_OPTIONS;
1542 #endif
1543
1544   /* -fPIC is the default for x86_64.  */
1545   if (TARGET_MACHO && TARGET_64BIT)
1546     flag_pic = 2;
1547
1548   /* Set the default values for switches whose default depends on TARGET_64BIT
1549      in case they weren't overwritten by command line options.  */
1550   if (TARGET_64BIT)
1551     {
1552       /* Mach-O doesn't support omitting the frame pointer for now.  */
1553       if (flag_omit_frame_pointer == 2)
1554         flag_omit_frame_pointer = (TARGET_MACHO ? 0 : 1);
1555       if (flag_asynchronous_unwind_tables == 2)
1556         flag_asynchronous_unwind_tables = 1;
1557       if (flag_pcc_struct_return == 2)
1558         flag_pcc_struct_return = 0;
1559     }
1560   else
1561     {
1562       if (flag_omit_frame_pointer == 2)
1563         flag_omit_frame_pointer = 0;
1564       if (flag_asynchronous_unwind_tables == 2)
1565         flag_asynchronous_unwind_tables = 0;
1566       if (flag_pcc_struct_return == 2)
1567         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1568     }
1569
1570   /* Need to check -mtune=generic first.  */
1571   if (ix86_tune_string)
1572     {
1573       if (!strcmp (ix86_tune_string, "generic")
1574           || !strcmp (ix86_tune_string, "i686")
1575           /* As special support for cross compilers we read -mtune=native
1576              as -mtune=generic.  With native compilers we won't see the
1577              -mtune=native, as it was changed by the driver.  */
1578           || !strcmp (ix86_tune_string, "native"))
1579         {
1580           if (TARGET_64BIT)
1581             ix86_tune_string = "generic64";
1582           else
1583             ix86_tune_string = "generic32";
1584         }
1585       else if (!strncmp (ix86_tune_string, "generic", 7))
1586         error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1587     }
1588   else
1589     {
1590       if (ix86_arch_string)
1591         ix86_tune_string = ix86_arch_string;
1592       if (!ix86_tune_string)
1593         {
1594           ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1595           ix86_tune_defaulted = 1;
1596         }
1597
1598       /* ix86_tune_string is set to ix86_arch_string or defaulted.  We
1599          need to use a sensible tune option.  */
1600       if (!strcmp (ix86_tune_string, "generic")
1601           || !strcmp (ix86_tune_string, "x86-64")
1602           || !strcmp (ix86_tune_string, "i686"))
1603         {
1604           if (TARGET_64BIT)
1605             ix86_tune_string = "generic64";
1606           else
1607             ix86_tune_string = "generic32";
1608         }
1609     }
1610   if (!strcmp (ix86_tune_string, "x86-64"))
1611     warning (OPT_Wdeprecated, "-mtune=x86-64 is deprecated.  Use -mtune=k8 or "
1612              "-mtune=generic instead as appropriate.");
1613
1614   if (!ix86_arch_string)
1615     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1616   if (!strcmp (ix86_arch_string, "generic"))
1617     error ("generic CPU can be used only for -mtune= switch");
1618   if (!strncmp (ix86_arch_string, "generic", 7))
1619     error ("bad value (%s) for -march= switch", ix86_arch_string);
1620
1621   if (ix86_cmodel_string != 0)
1622     {
1623       if (!strcmp (ix86_cmodel_string, "small"))
1624         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1625       else if (!strcmp (ix86_cmodel_string, "medium"))
1626         ix86_cmodel = flag_pic ? CM_MEDIUM_PIC : CM_MEDIUM;
1627       else if (flag_pic)
1628         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1629       else if (!strcmp (ix86_cmodel_string, "32"))
1630         ix86_cmodel = CM_32;
1631       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1632         ix86_cmodel = CM_KERNEL;
1633       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1634         ix86_cmodel = CM_LARGE;
1635       else
1636         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1637     }
1638   else
1639     {
1640       ix86_cmodel = CM_32;
1641       if (TARGET_64BIT)
1642         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1643     }
1644   if (ix86_asm_string != 0)
1645     {
1646       if (! TARGET_MACHO
1647           && !strcmp (ix86_asm_string, "intel"))
1648         ix86_asm_dialect = ASM_INTEL;
1649       else if (!strcmp (ix86_asm_string, "att"))
1650         ix86_asm_dialect = ASM_ATT;
1651       else
1652         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1653     }
1654   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1655     error ("code model %qs not supported in the %s bit mode",
1656            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1657   if (ix86_cmodel == CM_LARGE)
1658     sorry ("code model %<large%> not supported yet");
1659   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1660     sorry ("%i-bit mode not compiled in",
1661            (target_flags & MASK_64BIT) ? 64 : 32);
1662
1663   for (i = 0; i < pta_size; i++)
1664     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1665       {
1666         ix86_arch = processor_alias_table[i].processor;
1667         /* Default cpu tuning to the architecture.  */
1668         ix86_tune = ix86_arch;
1669         if (processor_alias_table[i].flags & PTA_MMX
1670             && !(target_flags_explicit & MASK_MMX))
1671           target_flags |= MASK_MMX;
1672         if (processor_alias_table[i].flags & PTA_3DNOW
1673             && !(target_flags_explicit & MASK_3DNOW))
1674           target_flags |= MASK_3DNOW;
1675         if (processor_alias_table[i].flags & PTA_3DNOW_A
1676             && !(target_flags_explicit & MASK_3DNOW_A))
1677           target_flags |= MASK_3DNOW_A;
1678         if (processor_alias_table[i].flags & PTA_SSE
1679             && !(target_flags_explicit & MASK_SSE))
1680           target_flags |= MASK_SSE;
1681         if (processor_alias_table[i].flags & PTA_SSE2
1682             && !(target_flags_explicit & MASK_SSE2))
1683           target_flags |= MASK_SSE2;
1684         if (processor_alias_table[i].flags & PTA_SSE3
1685             && !(target_flags_explicit & MASK_SSE3))
1686           target_flags |= MASK_SSE3;
1687         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1688           x86_prefetch_sse = true;
1689         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1690           error ("CPU you selected does not support x86-64 "
1691                  "instruction set");
1692         break;
1693       }
1694
1695   if (i == pta_size)
1696     error ("bad value (%s) for -march= switch", ix86_arch_string);
1697
1698   for (i = 0; i < pta_size; i++)
1699     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1700       {
1701         ix86_tune = processor_alias_table[i].processor;
1702         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1703           {
1704             if (ix86_tune_defaulted)
1705               {
1706                 ix86_tune_string = "x86-64";
1707                 for (i = 0; i < pta_size; i++)
1708                   if (! strcmp (ix86_tune_string,
1709                                 processor_alias_table[i].name))
1710                     break;
1711                 ix86_tune = processor_alias_table[i].processor;
1712               }
1713             else
1714               error ("CPU you selected does not support x86-64 "
1715                      "instruction set");
1716           }
1717         /* Intel CPUs have always interpreted SSE prefetch instructions as
1718            NOPs; so, we can enable SSE prefetch instructions even when
1719            -mtune (rather than -march) points us to a processor that has them.
1720            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1721            higher processors.  */
1722         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1723           x86_prefetch_sse = true;
1724         break;
1725       }
1726   if (i == pta_size)
1727     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1728
1729   if (optimize_size)
1730     ix86_cost = &size_cost;
1731   else
1732     ix86_cost = processor_target_table[ix86_tune].cost;
1733   target_flags |= processor_target_table[ix86_tune].target_enable;
1734   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1735
1736   /* Arrange to set up i386_stack_locals for all functions.  */
1737   init_machine_status = ix86_init_machine_status;
1738
1739   /* Validate -mregparm= value.  */
1740   if (ix86_regparm_string)
1741     {
1742       i = atoi (ix86_regparm_string);
1743       if (i < 0 || i > REGPARM_MAX)
1744         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1745       else
1746         ix86_regparm = i;
1747     }
1748   else
1749    if (TARGET_64BIT)
1750      ix86_regparm = REGPARM_MAX;
1751
1752   /* If the user has provided any of the -malign-* options,
1753      warn and use that value only if -falign-* is not set.
1754      Remove this code in GCC 3.2 or later.  */
1755   if (ix86_align_loops_string)
1756     {
1757       warning (0, "-malign-loops is obsolete, use -falign-loops");
1758       if (align_loops == 0)
1759         {
1760           i = atoi (ix86_align_loops_string);
1761           if (i < 0 || i > MAX_CODE_ALIGN)
1762             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1763           else
1764             align_loops = 1 << i;
1765         }
1766     }
1767
1768   if (ix86_align_jumps_string)
1769     {
1770       warning (0, "-malign-jumps is obsolete, use -falign-jumps");
1771       if (align_jumps == 0)
1772         {
1773           i = atoi (ix86_align_jumps_string);
1774           if (i < 0 || i > MAX_CODE_ALIGN)
1775             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1776           else
1777             align_jumps = 1 << i;
1778         }
1779     }
1780
1781   if (ix86_align_funcs_string)
1782     {
1783       warning (0, "-malign-functions is obsolete, use -falign-functions");
1784       if (align_functions == 0)
1785         {
1786           i = atoi (ix86_align_funcs_string);
1787           if (i < 0 || i > MAX_CODE_ALIGN)
1788             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1789           else
1790             align_functions = 1 << i;
1791         }
1792     }
1793
1794   /* Default align_* from the processor table.  */
1795   if (align_loops == 0)
1796     {
1797       align_loops = processor_target_table[ix86_tune].align_loop;
1798       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1799     }
1800   if (align_jumps == 0)
1801     {
1802       align_jumps = processor_target_table[ix86_tune].align_jump;
1803       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1804     }
1805   if (align_functions == 0)
1806     {
1807       align_functions = processor_target_table[ix86_tune].align_func;
1808     }
1809
1810   /* Validate -mbranch-cost= value, or provide default.  */
1811   ix86_branch_cost = ix86_cost->branch_cost;
1812   if (ix86_branch_cost_string)
1813     {
1814       i = atoi (ix86_branch_cost_string);
1815       if (i < 0 || i > 5)
1816         error ("-mbranch-cost=%d is not between 0 and 5", i);
1817       else
1818         ix86_branch_cost = i;
1819     }
1820   if (ix86_section_threshold_string)
1821     {
1822       i = atoi (ix86_section_threshold_string);
1823       if (i < 0)
1824         error ("-mlarge-data-threshold=%d is negative", i);
1825       else
1826         ix86_section_threshold = i;
1827     }
1828
1829   if (ix86_tls_dialect_string)
1830     {
1831       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1832         ix86_tls_dialect = TLS_DIALECT_GNU;
1833       else if (strcmp (ix86_tls_dialect_string, "gnu2") == 0)
1834         ix86_tls_dialect = TLS_DIALECT_GNU2;
1835       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1836         ix86_tls_dialect = TLS_DIALECT_SUN;
1837       else
1838         error ("bad value (%s) for -mtls-dialect= switch",
1839                ix86_tls_dialect_string);
1840     }
1841
1842   /* Keep nonleaf frame pointers.  */
1843   if (flag_omit_frame_pointer)
1844     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
1845   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
1846     flag_omit_frame_pointer = 1;
1847
1848   /* If we're doing fast math, we don't care about comparison order
1849      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1850   if (flag_unsafe_math_optimizations)
1851     target_flags &= ~MASK_IEEE_FP;
1852
1853   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1854      since the insns won't need emulation.  */
1855   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1856     target_flags &= ~MASK_NO_FANCY_MATH_387;
1857
1858   /* Likewise, if the target doesn't have a 387, or we've specified
1859      software floating point, don't use 387 inline intrinsics.  */
1860   if (!TARGET_80387)
1861     target_flags |= MASK_NO_FANCY_MATH_387;
1862
1863   /* Turn on SSE2 builtins for -msse3.  */
1864   if (TARGET_SSE3)
1865     target_flags |= MASK_SSE2;
1866
1867   /* Turn on SSE builtins for -msse2.  */
1868   if (TARGET_SSE2)
1869     target_flags |= MASK_SSE;
1870
1871   /* Turn on MMX builtins for -msse.  */
1872   if (TARGET_SSE)
1873     {
1874       target_flags |= MASK_MMX & ~target_flags_explicit;
1875       x86_prefetch_sse = true;
1876     }
1877
1878   /* Turn on MMX builtins for 3Dnow.  */
1879   if (TARGET_3DNOW)
1880     target_flags |= MASK_MMX;
1881
1882   if (TARGET_64BIT)
1883     {
1884       if (TARGET_ALIGN_DOUBLE)
1885         error ("-malign-double makes no sense in the 64bit mode");
1886       if (TARGET_RTD)
1887         error ("-mrtd calling convention not supported in the 64bit mode");
1888
1889       /* Enable by default the SSE and MMX builtins.  Do allow the user to
1890          explicitly disable any of these.  In particular, disabling SSE and
1891          MMX for kernel code is extremely useful.  */
1892       target_flags
1893         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
1894             & ~target_flags_explicit);
1895      }
1896   else
1897     {
1898       /* i386 ABI does not specify red zone.  It still makes sense to use it
1899          when programmer takes care to stack from being destroyed.  */
1900       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1901         target_flags |= MASK_NO_RED_ZONE;
1902     }
1903
1904   /* Validate -mpreferred-stack-boundary= value, or provide default.
1905      The default of 128 bits is for Pentium III's SSE __m128.  We can't
1906      change it because of optimize_size.  Otherwise, we can't mix object
1907      files compiled with -Os and -On.  */
1908   ix86_preferred_stack_boundary = 128;
1909   if (ix86_preferred_stack_boundary_string)
1910     {
1911       i = atoi (ix86_preferred_stack_boundary_string);
1912       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1913         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1914                TARGET_64BIT ? 4 : 2);
1915       else
1916         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1917     }
1918
1919   /* Accept -msseregparm only if at least SSE support is enabled.  */
1920   if (TARGET_SSEREGPARM
1921       && ! TARGET_SSE)
1922     error ("-msseregparm used without SSE enabled");
1923
1924   ix86_fpmath = TARGET_FPMATH_DEFAULT;
1925
1926   if (ix86_fpmath_string != 0)
1927     {
1928       if (! strcmp (ix86_fpmath_string, "387"))
1929         ix86_fpmath = FPMATH_387;
1930       else if (! strcmp (ix86_fpmath_string, "sse"))
1931         {
1932           if (!TARGET_SSE)
1933             {
1934               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1935               ix86_fpmath = FPMATH_387;
1936             }
1937           else
1938             ix86_fpmath = FPMATH_SSE;
1939         }
1940       else if (! strcmp (ix86_fpmath_string, "387,sse")
1941                || ! strcmp (ix86_fpmath_string, "sse,387"))
1942         {
1943           if (!TARGET_SSE)
1944             {
1945               warning (0, "SSE instruction set disabled, using 387 arithmetics");
1946               ix86_fpmath = FPMATH_387;
1947             }
1948           else if (!TARGET_80387)
1949             {
1950               warning (0, "387 instruction set disabled, using SSE arithmetics");
1951               ix86_fpmath = FPMATH_SSE;
1952             }
1953           else
1954             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1955         }
1956       else
1957         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1958     }
1959
1960   /* If the i387 is disabled, then do not return values in it. */
1961   if (!TARGET_80387)
1962     target_flags &= ~MASK_FLOAT_RETURNS;
1963
1964   if ((x86_accumulate_outgoing_args & TUNEMASK)
1965       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1966       && !optimize_size)
1967     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1968
1969   /* ??? Unwind info is not correct around the CFG unless either a frame
1970      pointer is present or M_A_O_A is set.  Fixing this requires rewriting
1971      unwind info generation to be aware of the CFG and propagating states
1972      around edges.  */
1973   if ((flag_unwind_tables || flag_asynchronous_unwind_tables
1974        || flag_exceptions || flag_non_call_exceptions)
1975       && flag_omit_frame_pointer
1976       && !(target_flags & MASK_ACCUMULATE_OUTGOING_ARGS))
1977     {
1978       if (target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1979         warning (0, "unwind tables currently require either a frame pointer "
1980                  "or -maccumulate-outgoing-args for correctness");
1981       target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1982     }
1983
1984   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1985   {
1986     char *p;
1987     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1988     p = strchr (internal_label_prefix, 'X');
1989     internal_label_prefix_len = p - internal_label_prefix;
1990     *p = '\0';
1991   }
1992
1993   /* When scheduling description is not available, disable scheduler pass
1994      so it won't slow down the compilation and make x87 code slower.  */
1995   if (!TARGET_SCHEDULE)
1996     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
1997 }
1998 \f
1999 /* switch to the appropriate section for output of DECL.
2000    DECL is either a `VAR_DECL' node or a constant of some sort.
2001    RELOC indicates whether forming the initial value of DECL requires
2002    link-time relocations.  */
2003
2004 static section *
2005 x86_64_elf_select_section (tree decl, int reloc,
2006                            unsigned HOST_WIDE_INT align)
2007 {
2008   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2009       && ix86_in_large_data_p (decl))
2010     {
2011       const char *sname = NULL;
2012       unsigned int flags = SECTION_WRITE;
2013       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2014         {
2015         case SECCAT_DATA:
2016           sname = ".ldata";
2017           break;
2018         case SECCAT_DATA_REL:
2019           sname = ".ldata.rel";
2020           break;
2021         case SECCAT_DATA_REL_LOCAL:
2022           sname = ".ldata.rel.local";
2023           break;
2024         case SECCAT_DATA_REL_RO:
2025           sname = ".ldata.rel.ro";
2026           break;
2027         case SECCAT_DATA_REL_RO_LOCAL:
2028           sname = ".ldata.rel.ro.local";
2029           break;
2030         case SECCAT_BSS:
2031           sname = ".lbss";
2032           flags |= SECTION_BSS;
2033           break;
2034         case SECCAT_RODATA:
2035         case SECCAT_RODATA_MERGE_STR:
2036         case SECCAT_RODATA_MERGE_STR_INIT:
2037         case SECCAT_RODATA_MERGE_CONST:
2038           sname = ".lrodata";
2039           flags = 0;
2040           break;
2041         case SECCAT_SRODATA:
2042         case SECCAT_SDATA:
2043         case SECCAT_SBSS:
2044           gcc_unreachable ();
2045         case SECCAT_TEXT:
2046         case SECCAT_TDATA:
2047         case SECCAT_TBSS:
2048           /* We don't split these for medium model.  Place them into
2049              default sections and hope for best.  */
2050           break;
2051         }
2052       if (sname)
2053         {
2054           /* We might get called with string constants, but get_named_section
2055              doesn't like them as they are not DECLs.  Also, we need to set
2056              flags in that case.  */
2057           if (!DECL_P (decl))
2058             return get_section (sname, flags, NULL);
2059           return get_named_section (decl, sname, reloc);
2060         }
2061     }
2062   return default_elf_select_section (decl, reloc, align);
2063 }
2064
2065 /* Build up a unique section name, expressed as a
2066    STRING_CST node, and assign it to DECL_SECTION_NAME (decl).
2067    RELOC indicates whether the initial value of EXP requires
2068    link-time relocations.  */
2069
2070 static void
2071 x86_64_elf_unique_section (tree decl, int reloc)
2072 {
2073   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2074       && ix86_in_large_data_p (decl))
2075     {
2076       const char *prefix = NULL;
2077       /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
2078       bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
2079
2080       switch (categorize_decl_for_section (decl, reloc, flag_pic))
2081         {
2082         case SECCAT_DATA:
2083         case SECCAT_DATA_REL:
2084         case SECCAT_DATA_REL_LOCAL:
2085         case SECCAT_DATA_REL_RO:
2086         case SECCAT_DATA_REL_RO_LOCAL:
2087           prefix = one_only ? ".gnu.linkonce.ld." : ".ldata.";
2088           break;
2089         case SECCAT_BSS:
2090           prefix = one_only ? ".gnu.linkonce.lb." : ".lbss.";
2091           break;
2092         case SECCAT_RODATA:
2093         case SECCAT_RODATA_MERGE_STR:
2094         case SECCAT_RODATA_MERGE_STR_INIT:
2095         case SECCAT_RODATA_MERGE_CONST:
2096           prefix = one_only ? ".gnu.linkonce.lr." : ".lrodata.";
2097           break;
2098         case SECCAT_SRODATA:
2099         case SECCAT_SDATA:
2100         case SECCAT_SBSS:
2101           gcc_unreachable ();
2102         case SECCAT_TEXT:
2103         case SECCAT_TDATA:
2104         case SECCAT_TBSS:
2105           /* We don't split these for medium model.  Place them into
2106              default sections and hope for best.  */
2107           break;
2108         }
2109       if (prefix)
2110         {
2111           const char *name;
2112           size_t nlen, plen;
2113           char *string;
2114           plen = strlen (prefix);
2115
2116           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2117           name = targetm.strip_name_encoding (name);
2118           nlen = strlen (name);
2119
2120           string = alloca (nlen + plen + 1);
2121           memcpy (string, prefix, plen);
2122           memcpy (string + plen, name, nlen + 1);
2123
2124           DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
2125           return;
2126         }
2127     }
2128   default_unique_section (decl, reloc);
2129 }
2130
2131 #ifdef COMMON_ASM_OP
2132 /* This says how to output assembler code to declare an
2133    uninitialized external linkage data object.
2134
2135    For medium model x86-64 we need to use .largecomm opcode for
2136    large objects.  */
2137 void
2138 x86_elf_aligned_common (FILE *file,
2139                         const char *name, unsigned HOST_WIDE_INT size,
2140                         int align)
2141 {
2142   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2143       && size > (unsigned int)ix86_section_threshold)
2144     fprintf (file, ".largecomm\t");
2145   else
2146     fprintf (file, "%s", COMMON_ASM_OP);
2147   assemble_name (file, name);
2148   fprintf (file, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2149            size, align / BITS_PER_UNIT);
2150 }
2151
2152 /* Utility function for targets to use in implementing
2153    ASM_OUTPUT_ALIGNED_BSS.  */
2154
2155 void
2156 x86_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
2157                         const char *name, unsigned HOST_WIDE_INT size,
2158                         int align)
2159 {
2160   if ((ix86_cmodel == CM_MEDIUM || ix86_cmodel == CM_MEDIUM_PIC)
2161       && size > (unsigned int)ix86_section_threshold)
2162     switch_to_section (get_named_section (decl, ".lbss", 0));
2163   else
2164     switch_to_section (bss_section);
2165   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
2166 #ifdef ASM_DECLARE_OBJECT_NAME
2167   last_assemble_variable_decl = decl;
2168   ASM_DECLARE_OBJECT_NAME (file, name, decl);
2169 #else
2170   /* Standard thing is just output label for the object.  */
2171   ASM_OUTPUT_LABEL (file, name);
2172 #endif /* ASM_DECLARE_OBJECT_NAME */
2173   ASM_OUTPUT_SKIP (file, size ? size : 1);
2174 }
2175 #endif
2176 \f
2177 void
2178 optimization_options (int level, int size ATTRIBUTE_UNUSED)
2179 {
2180   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
2181      make the problem with not enough registers even worse.  */
2182 #ifdef INSN_SCHEDULING
2183   if (level > 1)
2184     flag_schedule_insns = 0;
2185 #endif
2186
2187   if (TARGET_MACHO)
2188     /* The Darwin libraries never set errno, so we might as well
2189        avoid calling them when that's the only reason we would.  */
2190     flag_errno_math = 0;
2191
2192   /* The default values of these switches depend on the TARGET_64BIT
2193      that is not known at this moment.  Mark these values with 2 and
2194      let user the to override these.  In case there is no command line option
2195      specifying them, we will set the defaults in override_options.  */
2196   if (optimize >= 1)
2197     flag_omit_frame_pointer = 2;
2198   flag_pcc_struct_return = 2;
2199   flag_asynchronous_unwind_tables = 2;
2200 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
2201   SUBTARGET_OPTIMIZATION_OPTIONS;
2202 #endif
2203 }
2204 \f
2205 /* Table of valid machine attributes.  */
2206 const struct attribute_spec ix86_attribute_table[] =
2207 {
2208   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
2209   /* Stdcall attribute says callee is responsible for popping arguments
2210      if they are not variable.  */
2211   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2212   /* Fastcall attribute says callee is responsible for popping arguments
2213      if they are not variable.  */
2214   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2215   /* Cdecl attribute says the callee is a normal C declaration */
2216   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cconv_attribute },
2217   /* Regparm attribute specifies how many integer arguments are to be
2218      passed in registers.  */
2219   { "regparm",   1, 1, false, true,  true,  ix86_handle_cconv_attribute },
2220   /* Sseregparm attribute says we are using x86_64 calling conventions
2221      for FP arguments.  */
2222   { "sseregparm", 0, 0, false, true, true, ix86_handle_cconv_attribute },
2223   /* force_align_arg_pointer says this function realigns the stack at entry.  */
2224   { (const char *)&ix86_force_align_arg_pointer_string, 0, 0,
2225     false, true,  true, ix86_handle_cconv_attribute },
2226 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2227   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
2228   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
2229   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
2230 #endif
2231   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2232   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
2233 #ifdef SUBTARGET_ATTRIBUTE_TABLE
2234   SUBTARGET_ATTRIBUTE_TABLE,
2235 #endif
2236   { NULL,        0, 0, false, false, false, NULL }
2237 };
2238
2239 /* Decide whether we can make a sibling call to a function.  DECL is the
2240    declaration of the function being targeted by the call and EXP is the
2241    CALL_EXPR representing the call.  */
2242
2243 static bool
2244 ix86_function_ok_for_sibcall (tree decl, tree exp)
2245 {
2246   tree func;
2247   rtx a, b;
2248
2249   /* If we are generating position-independent code, we cannot sibcall
2250      optimize any indirect call, or a direct call to a global function,
2251      as the PLT requires %ebx be live.  */
2252   if (!TARGET_64BIT && flag_pic && (!decl || !targetm.binds_local_p (decl)))
2253     return false;
2254
2255   if (decl)
2256     func = decl;
2257   else
2258     {
2259       func = TREE_TYPE (TREE_OPERAND (exp, 0));
2260       if (POINTER_TYPE_P (func))
2261         func = TREE_TYPE (func);
2262     }
2263
2264   /* Check that the return value locations are the same.  Like
2265      if we are returning floats on the 80387 register stack, we cannot
2266      make a sibcall from a function that doesn't return a float to a
2267      function that does or, conversely, from a function that does return
2268      a float to a function that doesn't; the necessary stack adjustment
2269      would not be executed.  This is also the place we notice
2270      differences in the return value ABI.  Note that it is ok for one
2271      of the functions to have void return type as long as the return
2272      value of the other is passed in a register.  */
2273   a = ix86_function_value (TREE_TYPE (exp), func, false);
2274   b = ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
2275                            cfun->decl, false);
2276   if (STACK_REG_P (a) || STACK_REG_P (b))
2277     {
2278       if (!rtx_equal_p (a, b))
2279         return false;
2280     }
2281   else if (VOID_TYPE_P (TREE_TYPE (DECL_RESULT (cfun->decl))))
2282     ;
2283   else if (!rtx_equal_p (a, b))
2284     return false;
2285
2286   /* If this call is indirect, we'll need to be able to use a call-clobbered
2287      register for the address of the target function.  Make sure that all
2288      such registers are not used for passing parameters.  */
2289   if (!decl && !TARGET_64BIT)
2290     {
2291       tree type;
2292
2293       /* We're looking at the CALL_EXPR, we need the type of the function.  */
2294       type = TREE_OPERAND (exp, 0);             /* pointer expression */
2295       type = TREE_TYPE (type);                  /* pointer type */
2296       type = TREE_TYPE (type);                  /* function type */
2297
2298       if (ix86_function_regparm (type, NULL) >= 3)
2299         {
2300           /* ??? Need to count the actual number of registers to be used,
2301              not the possible number of registers.  Fix later.  */
2302           return false;
2303         }
2304     }
2305
2306 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
2307   /* Dllimport'd functions are also called indirectly.  */
2308   if (decl && DECL_DLLIMPORT_P (decl)
2309       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
2310     return false;
2311 #endif
2312
2313   /* If we forced aligned the stack, then sibcalling would unalign the
2314      stack, which may break the called function.  */
2315   if (cfun->machine->force_align_arg_pointer)
2316     return false;
2317
2318   /* Otherwise okay.  That also includes certain types of indirect calls.  */
2319   return true;
2320 }
2321
2322 /* Handle "cdecl", "stdcall", "fastcall", "regparm" and "sseregparm"
2323    calling convention attributes;
2324    arguments as in struct attribute_spec.handler.  */
2325
2326 static tree
2327 ix86_handle_cconv_attribute (tree *node, tree name,
2328                                    tree args,
2329                                    int flags ATTRIBUTE_UNUSED,
2330                                    bool *no_add_attrs)
2331 {
2332   if (TREE_CODE (*node) != FUNCTION_TYPE
2333       && TREE_CODE (*node) != METHOD_TYPE
2334       && TREE_CODE (*node) != FIELD_DECL
2335       && TREE_CODE (*node) != TYPE_DECL)
2336     {
2337       warning (OPT_Wattributes, "%qs attribute only applies to functions",
2338                IDENTIFIER_POINTER (name));
2339       *no_add_attrs = true;
2340       return NULL_TREE;
2341     }
2342
2343   /* Can combine regparm with all attributes but fastcall.  */
2344   if (is_attribute_p ("regparm", name))
2345     {
2346       tree cst;
2347
2348       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2349         {
2350           error ("fastcall and regparm attributes are not compatible");
2351         }
2352
2353       cst = TREE_VALUE (args);
2354       if (TREE_CODE (cst) != INTEGER_CST)
2355         {
2356           warning (OPT_Wattributes,
2357                    "%qs attribute requires an integer constant argument",
2358                    IDENTIFIER_POINTER (name));
2359           *no_add_attrs = true;
2360         }
2361       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
2362         {
2363           warning (OPT_Wattributes, "argument to %qs attribute larger than %d",
2364                    IDENTIFIER_POINTER (name), REGPARM_MAX);
2365           *no_add_attrs = true;
2366         }
2367
2368       if (!TARGET_64BIT
2369           && lookup_attribute (ix86_force_align_arg_pointer_string,
2370                                TYPE_ATTRIBUTES (*node))
2371           && compare_tree_int (cst, REGPARM_MAX-1))
2372         {
2373           error ("%s functions limited to %d register parameters",
2374                  ix86_force_align_arg_pointer_string, REGPARM_MAX-1);
2375         }
2376
2377       return NULL_TREE;
2378     }
2379
2380   if (TARGET_64BIT)
2381     {
2382       warning (OPT_Wattributes, "%qs attribute ignored",
2383                IDENTIFIER_POINTER (name));
2384       *no_add_attrs = true;
2385       return NULL_TREE;
2386     }
2387
2388   /* Can combine fastcall with stdcall (redundant) and sseregparm.  */
2389   if (is_attribute_p ("fastcall", name))
2390     {
2391       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2392         {
2393           error ("fastcall and cdecl attributes are not compatible");
2394         }
2395       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2396         {
2397           error ("fastcall and stdcall attributes are not compatible");
2398         }
2399       if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
2400         {
2401           error ("fastcall and regparm attributes are not compatible");
2402         }
2403     }
2404
2405   /* Can combine stdcall with fastcall (redundant), regparm and
2406      sseregparm.  */
2407   else if (is_attribute_p ("stdcall", name))
2408     {
2409       if (lookup_attribute ("cdecl", TYPE_ATTRIBUTES (*node)))
2410         {
2411           error ("stdcall and cdecl attributes are not compatible");
2412         }
2413       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2414         {
2415           error ("stdcall and fastcall attributes are not compatible");
2416         }
2417     }
2418
2419   /* Can combine cdecl with regparm and sseregparm.  */
2420   else if (is_attribute_p ("cdecl", name))
2421     {
2422       if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
2423         {
2424           error ("stdcall and cdecl attributes are not compatible");
2425         }
2426       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
2427         {
2428           error ("fastcall and cdecl attributes are not compatible");
2429         }
2430     }
2431
2432   /* Can combine sseregparm with all attributes.  */
2433
2434   return NULL_TREE;
2435 }
2436
2437 /* Return 0 if the attributes for two types are incompatible, 1 if they
2438    are compatible, and 2 if they are nearly compatible (which causes a
2439    warning to be generated).  */
2440
2441 static int
2442 ix86_comp_type_attributes (tree type1, tree type2)
2443 {
2444   /* Check for mismatch of non-default calling convention.  */
2445   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
2446
2447   if (TREE_CODE (type1) != FUNCTION_TYPE)
2448     return 1;
2449
2450   /* Check for mismatched fastcall/regparm types.  */
2451   if ((!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
2452        != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
2453       || (ix86_function_regparm (type1, NULL)
2454           != ix86_function_regparm (type2, NULL)))
2455     return 0;
2456
2457   /* Check for mismatched sseregparm types.  */
2458   if (!lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type1))
2459       != !lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type2)))
2460     return 0;
2461
2462   /* Check for mismatched return types (cdecl vs stdcall).  */
2463   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
2464       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
2465     return 0;
2466
2467   return 1;
2468 }
2469 \f
2470 /* Return the regparm value for a function with the indicated TYPE and DECL.
2471    DECL may be NULL when calling function indirectly
2472    or considering a libcall.  */
2473
2474 static int
2475 ix86_function_regparm (tree type, tree decl)
2476 {
2477   tree attr;
2478   int regparm = ix86_regparm;
2479   bool user_convention = false;
2480
2481   if (!TARGET_64BIT)
2482     {
2483       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
2484       if (attr)
2485         {
2486           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
2487           user_convention = true;
2488         }
2489
2490       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
2491         {
2492           regparm = 2;
2493           user_convention = true;
2494         }
2495
2496       /* Use register calling convention for local functions when possible.  */
2497       if (!TARGET_64BIT && !user_convention && decl
2498           && flag_unit_at_a_time && !profile_flag)
2499         {
2500           struct cgraph_local_info *i = cgraph_local_info (decl);
2501           if (i && i->local)
2502             {
2503               int local_regparm, globals = 0, regno;
2504
2505               /* Make sure no regparm register is taken by a global register
2506                  variable.  */
2507               for (local_regparm = 0; local_regparm < 3; local_regparm++)
2508                 if (global_regs[local_regparm])
2509                   break;
2510               /* We can't use regparm(3) for nested functions as these use
2511                  static chain pointer in third argument.  */
2512               if (local_regparm == 3
2513                   && decl_function_context (decl)
2514                   && !DECL_NO_STATIC_CHAIN (decl))
2515                 local_regparm = 2;
2516               /* If the function realigns its stackpointer, the
2517                  prologue will clobber %ecx.  If we've already
2518                  generated code for the callee, the callee
2519                  DECL_STRUCT_FUNCTION is gone, so we fall back to
2520                  scanning the attributes for the self-realigning
2521                  property.  */
2522               if ((DECL_STRUCT_FUNCTION (decl)
2523                    && DECL_STRUCT_FUNCTION (decl)->machine->force_align_arg_pointer)
2524                   || (!DECL_STRUCT_FUNCTION (decl)
2525                       && lookup_attribute (ix86_force_align_arg_pointer_string,
2526                                            TYPE_ATTRIBUTES (TREE_TYPE (decl)))))
2527                 local_regparm = 2;
2528               /* Each global register variable increases register preassure,
2529                  so the more global reg vars there are, the smaller regparm
2530                  optimization use, unless requested by the user explicitly.  */
2531               for (regno = 0; regno < 6; regno++)
2532                 if (global_regs[regno])
2533                   globals++;
2534               local_regparm
2535                 = globals < local_regparm ? local_regparm - globals : 0;
2536
2537               if (local_regparm > regparm)
2538                 regparm = local_regparm;
2539             }
2540         }
2541     }
2542   return regparm;
2543 }
2544
2545 /* Return 1 or 2, if we can pass up to 8 SFmode (1) and DFmode (2) arguments
2546    in SSE registers for a function with the indicated TYPE and DECL.
2547    DECL may be NULL when calling function indirectly
2548    or considering a libcall.  Otherwise return 0.  */
2549
2550 static int
2551 ix86_function_sseregparm (tree type, tree decl)
2552 {
2553   /* Use SSE registers to pass SFmode and DFmode arguments if requested
2554      by the sseregparm attribute.  */
2555   if (TARGET_SSEREGPARM
2556       || (type
2557           && lookup_attribute ("sseregparm", TYPE_ATTRIBUTES (type))))
2558     {
2559       if (!TARGET_SSE)
2560         {
2561           if (decl)
2562             error ("Calling %qD with attribute sseregparm without "
2563                    "SSE/SSE2 enabled", decl);
2564           else
2565             error ("Calling %qT with attribute sseregparm without "
2566                    "SSE/SSE2 enabled", type);
2567           return 0;
2568         }
2569
2570       return 2;
2571     }
2572
2573   /* For local functions, pass SFmode (and DFmode for SSE2) arguments
2574      in SSE registers even for 32-bit mode and not just 3, but up to
2575      8 SSE arguments in registers.  */
2576   if (!TARGET_64BIT && decl
2577       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2578     {
2579       struct cgraph_local_info *i = cgraph_local_info (decl);
2580       if (i && i->local)
2581         return TARGET_SSE2 ? 2 : 1;
2582     }
2583
2584   return 0;
2585 }
2586
2587 /* Return true if EAX is live at the start of the function.  Used by
2588    ix86_expand_prologue to determine if we need special help before
2589    calling allocate_stack_worker.  */
2590
2591 static bool
2592 ix86_eax_live_at_start_p (void)
2593 {
2594   /* Cheat.  Don't bother working forward from ix86_function_regparm
2595      to the function type to whether an actual argument is located in
2596      eax.  Instead just look at cfg info, which is still close enough
2597      to correct at this point.  This gives false positives for broken
2598      functions that might use uninitialized data that happens to be
2599      allocated in eax, but who cares?  */
2600   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->il.rtl->global_live_at_end, 0);
2601 }
2602
2603 /* Value is the number of bytes of arguments automatically
2604    popped when returning from a subroutine call.
2605    FUNDECL is the declaration node of the function (as a tree),
2606    FUNTYPE is the data type of the function (as a tree),
2607    or for a library call it is an identifier node for the subroutine name.
2608    SIZE is the number of bytes of arguments passed on the stack.
2609
2610    On the 80386, the RTD insn may be used to pop them if the number
2611      of args is fixed, but if the number is variable then the caller
2612      must pop them all.  RTD can't be used for library calls now
2613      because the library is compiled with the Unix compiler.
2614    Use of RTD is a selectable option, since it is incompatible with
2615    standard Unix calling sequences.  If the option is not selected,
2616    the caller must always pop the args.
2617
2618    The attribute stdcall is equivalent to RTD on a per module basis.  */
2619
2620 int
2621 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2622 {
2623   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2624
2625   /* Cdecl functions override -mrtd, and never pop the stack.  */
2626   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2627
2628     /* Stdcall and fastcall functions will pop the stack if not
2629        variable args.  */
2630     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2631         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2632       rtd = 1;
2633
2634     if (rtd
2635         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2636             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2637                 == void_type_node)))
2638       return size;
2639   }
2640
2641   /* Lose any fake structure return argument if it is passed on the stack.  */
2642   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2643       && !TARGET_64BIT
2644       && !KEEP_AGGREGATE_RETURN_POINTER)
2645     {
2646       int nregs = ix86_function_regparm (funtype, fundecl);
2647
2648       if (!nregs)
2649         return GET_MODE_SIZE (Pmode);
2650     }
2651
2652   return 0;
2653 }
2654 \f
2655 /* Argument support functions.  */
2656
2657 /* Return true when register may be used to pass function parameters.  */
2658 bool
2659 ix86_function_arg_regno_p (int regno)
2660 {
2661   int i;
2662   if (!TARGET_64BIT)
2663     return (regno < REGPARM_MAX
2664             || (TARGET_MMX && MMX_REGNO_P (regno)
2665                 && (regno < FIRST_MMX_REG + MMX_REGPARM_MAX))
2666             || (TARGET_SSE && SSE_REGNO_P (regno)
2667                 && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX)));
2668
2669   if (TARGET_SSE && SSE_REGNO_P (regno)
2670       && (regno < FIRST_SSE_REG + SSE_REGPARM_MAX))
2671     return true;
2672   /* RAX is used as hidden argument to va_arg functions.  */
2673   if (!regno)
2674     return true;
2675   for (i = 0; i < REGPARM_MAX; i++)
2676     if (regno == x86_64_int_parameter_registers[i])
2677       return true;
2678   return false;
2679 }
2680
2681 /* Return if we do not know how to pass TYPE solely in registers.  */
2682
2683 static bool
2684 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2685 {
2686   if (must_pass_in_stack_var_size_or_pad (mode, type))
2687     return true;
2688
2689   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2690      The layout_type routine is crafty and tries to trick us into passing
2691      currently unsupported vector types on the stack by using TImode.  */
2692   return (!TARGET_64BIT && mode == TImode
2693           && type && TREE_CODE (type) != VECTOR_TYPE);
2694 }
2695
2696 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2697    for a call to a function whose data type is FNTYPE.
2698    For a library call, FNTYPE is 0.  */
2699
2700 void
2701 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2702                       tree fntype,      /* tree ptr for function decl */
2703                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2704                       tree fndecl)
2705 {
2706   static CUMULATIVE_ARGS zero_cum;
2707   tree param, next_param;
2708
2709   if (TARGET_DEBUG_ARG)
2710     {
2711       fprintf (stderr, "\ninit_cumulative_args (");
2712       if (fntype)
2713         fprintf (stderr, "fntype code = %s, ret code = %s",
2714                  tree_code_name[(int) TREE_CODE (fntype)],
2715                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
2716       else
2717         fprintf (stderr, "no fntype");
2718
2719       if (libname)
2720         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
2721     }
2722
2723   *cum = zero_cum;
2724
2725   /* Set up the number of registers to use for passing arguments.  */
2726   cum->nregs = ix86_regparm;
2727   if (TARGET_SSE)
2728     cum->sse_nregs = SSE_REGPARM_MAX;
2729   if (TARGET_MMX)
2730     cum->mmx_nregs = MMX_REGPARM_MAX;
2731   cum->warn_sse = true;
2732   cum->warn_mmx = true;
2733   cum->maybe_vaarg = false;
2734
2735   /* Use ecx and edx registers if function has fastcall attribute,
2736      else look for regparm information.  */
2737   if (fntype && !TARGET_64BIT)
2738     {
2739       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
2740         {
2741           cum->nregs = 2;
2742           cum->fastcall = 1;
2743         }
2744       else
2745         cum->nregs = ix86_function_regparm (fntype, fndecl);
2746     }
2747
2748   /* Set up the number of SSE registers used for passing SFmode
2749      and DFmode arguments.  Warn for mismatching ABI.  */
2750   cum->float_in_sse = ix86_function_sseregparm (fntype, fndecl);
2751
2752   /* Determine if this function has variable arguments.  This is
2753      indicated by the last argument being 'void_type_mode' if there
2754      are no variable arguments.  If there are variable arguments, then
2755      we won't pass anything in registers in 32-bit mode. */
2756
2757   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
2758     {
2759       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
2760            param != 0; param = next_param)
2761         {
2762           next_param = TREE_CHAIN (param);
2763           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
2764             {
2765               if (!TARGET_64BIT)
2766                 {
2767                   cum->nregs = 0;
2768                   cum->sse_nregs = 0;
2769                   cum->mmx_nregs = 0;
2770                   cum->warn_sse = 0;
2771                   cum->warn_mmx = 0;
2772                   cum->fastcall = 0;
2773                   cum->float_in_sse = 0;
2774                 }
2775               cum->maybe_vaarg = true;
2776             }
2777         }
2778     }
2779   if ((!fntype && !libname)
2780       || (fntype && !TYPE_ARG_TYPES (fntype)))
2781     cum->maybe_vaarg = true;
2782
2783   if (TARGET_DEBUG_ARG)
2784     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2785
2786   return;
2787 }
2788
2789 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
2790    But in the case of vector types, it is some vector mode.
2791
2792    When we have only some of our vector isa extensions enabled, then there
2793    are some modes for which vector_mode_supported_p is false.  For these
2794    modes, the generic vector support in gcc will choose some non-vector mode
2795    in order to implement the type.  By computing the natural mode, we'll
2796    select the proper ABI location for the operand and not depend on whatever
2797    the middle-end decides to do with these vector types.  */
2798
2799 static enum machine_mode
2800 type_natural_mode (tree type)
2801 {
2802   enum machine_mode mode = TYPE_MODE (type);
2803
2804   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
2805     {
2806       HOST_WIDE_INT size = int_size_in_bytes (type);
2807       if ((size == 8 || size == 16)
2808           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
2809           && TYPE_VECTOR_SUBPARTS (type) > 1)
2810         {
2811           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2812
2813           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
2814             mode = MIN_MODE_VECTOR_FLOAT;
2815           else
2816             mode = MIN_MODE_VECTOR_INT;
2817
2818           /* Get the mode which has this inner mode and number of units.  */
2819           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2820             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
2821                 && GET_MODE_INNER (mode) == innermode)
2822               return mode;
2823
2824           gcc_unreachable ();
2825         }
2826     }
2827
2828   return mode;
2829 }
2830
2831 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
2832    this may not agree with the mode that the type system has chosen for the
2833    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
2834    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
2835
2836 static rtx
2837 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
2838                      unsigned int regno)
2839 {
2840   rtx tmp;
2841
2842   if (orig_mode != BLKmode)
2843     tmp = gen_rtx_REG (orig_mode, regno);
2844   else
2845     {
2846       tmp = gen_rtx_REG (mode, regno);
2847       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
2848       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
2849     }
2850
2851   return tmp;
2852 }
2853
2854 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2855    of this code is to classify each 8bytes of incoming argument by the register
2856    class and assign registers accordingly.  */
2857
2858 /* Return the union class of CLASS1 and CLASS2.
2859    See the x86-64 PS ABI for details.  */
2860
2861 static enum x86_64_reg_class
2862 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2863 {
2864   /* Rule #1: If both classes are equal, this is the resulting class.  */
2865   if (class1 == class2)
2866     return class1;
2867
2868   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2869      the other class.  */
2870   if (class1 == X86_64_NO_CLASS)
2871     return class2;
2872   if (class2 == X86_64_NO_CLASS)
2873     return class1;
2874
2875   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2876   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2877     return X86_64_MEMORY_CLASS;
2878
2879   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2880   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2881       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2882     return X86_64_INTEGERSI_CLASS;
2883   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2884       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2885     return X86_64_INTEGER_CLASS;
2886
2887   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
2888      MEMORY is used.  */
2889   if (class1 == X86_64_X87_CLASS
2890       || class1 == X86_64_X87UP_CLASS
2891       || class1 == X86_64_COMPLEX_X87_CLASS
2892       || class2 == X86_64_X87_CLASS
2893       || class2 == X86_64_X87UP_CLASS
2894       || class2 == X86_64_COMPLEX_X87_CLASS)
2895     return X86_64_MEMORY_CLASS;
2896
2897   /* Rule #6: Otherwise class SSE is used.  */
2898   return X86_64_SSE_CLASS;
2899 }
2900
2901 /* Classify the argument of type TYPE and mode MODE.
2902    CLASSES will be filled by the register class used to pass each word
2903    of the operand.  The number of words is returned.  In case the parameter
2904    should be passed in memory, 0 is returned. As a special case for zero
2905    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2906
2907    BIT_OFFSET is used internally for handling records and specifies offset
2908    of the offset in bits modulo 256 to avoid overflow cases.
2909
2910    See the x86-64 PS ABI for details.
2911 */
2912
2913 static int
2914 classify_argument (enum machine_mode mode, tree type,
2915                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2916 {
2917   HOST_WIDE_INT bytes =
2918     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2919   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2920
2921   /* Variable sized entities are always passed/returned in memory.  */
2922   if (bytes < 0)
2923     return 0;
2924
2925   if (mode != VOIDmode
2926       && targetm.calls.must_pass_in_stack (mode, type))
2927     return 0;
2928
2929   if (type && AGGREGATE_TYPE_P (type))
2930     {
2931       int i;
2932       tree field;
2933       enum x86_64_reg_class subclasses[MAX_CLASSES];
2934
2935       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
2936       if (bytes > 16)
2937         return 0;
2938
2939       for (i = 0; i < words; i++)
2940         classes[i] = X86_64_NO_CLASS;
2941
2942       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
2943          signalize memory class, so handle it as special case.  */
2944       if (!words)
2945         {
2946           classes[0] = X86_64_NO_CLASS;
2947           return 1;
2948         }
2949
2950       /* Classify each field of record and merge classes.  */
2951       switch (TREE_CODE (type))
2952         {
2953         case RECORD_TYPE:
2954           /* For classes first merge in the field of the subclasses.  */
2955           if (TYPE_BINFO (type))
2956             {
2957               tree binfo, base_binfo;
2958               int basenum;
2959
2960               for (binfo = TYPE_BINFO (type), basenum = 0;
2961                    BINFO_BASE_ITERATE (binfo, basenum, base_binfo); basenum++)
2962                 {
2963                    int num;
2964                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2965                    tree type = BINFO_TYPE (base_binfo);
2966
2967                    num = classify_argument (TYPE_MODE (type),
2968                                             type, subclasses,
2969                                             (offset + bit_offset) % 256);
2970                    if (!num)
2971                      return 0;
2972                    for (i = 0; i < num; i++)
2973                      {
2974                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2975                        classes[i + pos] =
2976                          merge_classes (subclasses[i], classes[i + pos]);
2977                      }
2978                 }
2979             }
2980           /* And now merge the fields of structure.  */
2981           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2982             {
2983               if (TREE_CODE (field) == FIELD_DECL)
2984                 {
2985                   int num;
2986
2987                   if (TREE_TYPE (field) == error_mark_node)
2988                     continue;
2989
2990                   /* Bitfields are always classified as integer.  Handle them
2991                      early, since later code would consider them to be
2992                      misaligned integers.  */
2993                   if (DECL_BIT_FIELD (field))
2994                     {
2995                       for (i = (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2996                            i < ((int_bit_position (field) + (bit_offset % 64))
2997                                 + tree_low_cst (DECL_SIZE (field), 0)
2998                                 + 63) / 8 / 8; i++)
2999                         classes[i] =
3000                           merge_classes (X86_64_INTEGER_CLASS,
3001                                          classes[i]);
3002                     }
3003                   else
3004                     {
3005                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3006                                                TREE_TYPE (field), subclasses,
3007                                                (int_bit_position (field)
3008                                                 + bit_offset) % 256);
3009                       if (!num)
3010                         return 0;
3011                       for (i = 0; i < num; i++)
3012                         {
3013                           int pos =
3014                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
3015                           classes[i + pos] =
3016                             merge_classes (subclasses[i], classes[i + pos]);
3017                         }
3018                     }
3019                 }
3020             }
3021           break;
3022
3023         case ARRAY_TYPE:
3024           /* Arrays are handled as small records.  */
3025           {
3026             int num;
3027             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
3028                                      TREE_TYPE (type), subclasses, bit_offset);
3029             if (!num)
3030               return 0;
3031
3032             /* The partial classes are now full classes.  */
3033             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
3034               subclasses[0] = X86_64_SSE_CLASS;
3035             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
3036               subclasses[0] = X86_64_INTEGER_CLASS;
3037
3038             for (i = 0; i < words; i++)
3039               classes[i] = subclasses[i % num];
3040
3041             break;
3042           }
3043         case UNION_TYPE:
3044         case QUAL_UNION_TYPE:
3045           /* Unions are similar to RECORD_TYPE but offset is always 0.
3046              */
3047
3048           /* Unions are not derived.  */
3049           gcc_assert (!TYPE_BINFO (type)
3050                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
3051           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3052             {
3053               if (TREE_CODE (field) == FIELD_DECL)
3054                 {
3055                   int num;
3056
3057                   if (TREE_TYPE (field) == error_mark_node)
3058                     continue;
3059
3060                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
3061                                            TREE_TYPE (field), subclasses,
3062                                            bit_offset);
3063                   if (!num)
3064                     return 0;
3065                   for (i = 0; i < num; i++)
3066                     classes[i] = merge_classes (subclasses[i], classes[i]);
3067                 }
3068             }
3069           break;
3070
3071         default:
3072           gcc_unreachable ();
3073         }
3074
3075       /* Final merger cleanup.  */
3076       for (i = 0; i < words; i++)
3077         {
3078           /* If one class is MEMORY, everything should be passed in
3079              memory.  */
3080           if (classes[i] == X86_64_MEMORY_CLASS)
3081             return 0;
3082
3083           /* The X86_64_SSEUP_CLASS should be always preceded by
3084              X86_64_SSE_CLASS.  */
3085           if (classes[i] == X86_64_SSEUP_CLASS
3086               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
3087             classes[i] = X86_64_SSE_CLASS;
3088
3089           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
3090           if (classes[i] == X86_64_X87UP_CLASS
3091               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
3092             classes[i] = X86_64_SSE_CLASS;
3093         }
3094       return words;
3095     }
3096
3097   /* Compute alignment needed.  We align all types to natural boundaries with
3098      exception of XFmode that is aligned to 64bits.  */
3099   if (mode != VOIDmode && mode != BLKmode)
3100     {
3101       int mode_alignment = GET_MODE_BITSIZE (mode);
3102
3103       if (mode == XFmode)
3104         mode_alignment = 128;
3105       else if (mode == XCmode)
3106         mode_alignment = 256;
3107       if (COMPLEX_MODE_P (mode))
3108         mode_alignment /= 2;
3109       /* Misaligned fields are always returned in memory.  */
3110       if (bit_offset % mode_alignment)
3111         return 0;
3112     }
3113
3114   /* for V1xx modes, just use the base mode */
3115   if (VECTOR_MODE_P (mode)
3116       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
3117     mode = GET_MODE_INNER (mode);
3118
3119   /* Classification of atomic types.  */
3120   switch (mode)
3121     {
3122     case SDmode:
3123     case DDmode:
3124       classes[0] = X86_64_SSE_CLASS;
3125       return 1;
3126     case TDmode:
3127       classes[0] = X86_64_SSE_CLASS;
3128       classes[1] = X86_64_SSEUP_CLASS;
3129       return 2;
3130     case DImode:
3131     case SImode:
3132     case HImode:
3133     case QImode:
3134     case CSImode:
3135     case CHImode:
3136     case CQImode:
3137       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3138         classes[0] = X86_64_INTEGERSI_CLASS;
3139       else
3140         classes[0] = X86_64_INTEGER_CLASS;
3141       return 1;
3142     case CDImode:
3143     case TImode:
3144       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
3145       return 2;
3146     case CTImode:
3147       return 0;
3148     case SFmode:
3149       if (!(bit_offset % 64))
3150         classes[0] = X86_64_SSESF_CLASS;
3151       else
3152         classes[0] = X86_64_SSE_CLASS;
3153       return 1;
3154     case DFmode:
3155       classes[0] = X86_64_SSEDF_CLASS;
3156       return 1;
3157     case XFmode:
3158       classes[0] = X86_64_X87_CLASS;
3159       classes[1] = X86_64_X87UP_CLASS;
3160       return 2;
3161     case TFmode:
3162       classes[0] = X86_64_SSE_CLASS;
3163       classes[1] = X86_64_SSEUP_CLASS;
3164       return 2;
3165     case SCmode:
3166       classes[0] = X86_64_SSE_CLASS;
3167       return 1;
3168     case DCmode:
3169       classes[0] = X86_64_SSEDF_CLASS;
3170       classes[1] = X86_64_SSEDF_CLASS;
3171       return 2;
3172     case XCmode:
3173       classes[0] = X86_64_COMPLEX_X87_CLASS;
3174       return 1;
3175     case TCmode:
3176       /* This modes is larger than 16 bytes.  */
3177       return 0;
3178     case V4SFmode:
3179     case V4SImode:
3180     case V16QImode:
3181     case V8HImode:
3182     case V2DFmode:
3183     case V2DImode:
3184       classes[0] = X86_64_SSE_CLASS;
3185       classes[1] = X86_64_SSEUP_CLASS;
3186       return 2;
3187     case V2SFmode:
3188     case V2SImode:
3189     case V4HImode:
3190     case V8QImode:
3191       classes[0] = X86_64_SSE_CLASS;
3192       return 1;
3193     case BLKmode:
3194     case VOIDmode:
3195       return 0;
3196     default:
3197       gcc_assert (VECTOR_MODE_P (mode));
3198
3199       if (bytes > 16)
3200         return 0;
3201
3202       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
3203
3204       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
3205         classes[0] = X86_64_INTEGERSI_CLASS;
3206       else
3207         classes[0] = X86_64_INTEGER_CLASS;
3208       classes[1] = X86_64_INTEGER_CLASS;
3209       return 1 + (bytes > 8);
3210     }
3211 }
3212
3213 /* Examine the argument and return set number of register required in each
3214    class.  Return 0 iff parameter should be passed in memory.  */
3215 static int
3216 examine_argument (enum machine_mode mode, tree type, int in_return,
3217                   int *int_nregs, int *sse_nregs)
3218 {
3219   enum x86_64_reg_class class[MAX_CLASSES];
3220   int n = classify_argument (mode, type, class, 0);
3221
3222   *int_nregs = 0;
3223   *sse_nregs = 0;
3224   if (!n)
3225     return 0;
3226   for (n--; n >= 0; n--)
3227     switch (class[n])
3228       {
3229       case X86_64_INTEGER_CLASS:
3230       case X86_64_INTEGERSI_CLASS:
3231         (*int_nregs)++;
3232         break;
3233       case X86_64_SSE_CLASS:
3234       case X86_64_SSESF_CLASS:
3235       case X86_64_SSEDF_CLASS:
3236         (*sse_nregs)++;
3237         break;
3238       case X86_64_NO_CLASS:
3239       case X86_64_SSEUP_CLASS:
3240         break;
3241       case X86_64_X87_CLASS:
3242       case X86_64_X87UP_CLASS:
3243         if (!in_return)
3244           return 0;
3245         break;
3246       case X86_64_COMPLEX_X87_CLASS:
3247         return in_return ? 2 : 0;
3248       case X86_64_MEMORY_CLASS:
3249         gcc_unreachable ();
3250       }
3251   return 1;
3252 }
3253
3254 /* Construct container for the argument used by GCC interface.  See
3255    FUNCTION_ARG for the detailed description.  */
3256
3257 static rtx
3258 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
3259                      tree type, int in_return, int nintregs, int nsseregs,
3260                      const int *intreg, int sse_regno)
3261 {
3262   /* The following variables hold the static issued_error state.  */
3263   static bool issued_sse_arg_error;
3264   static bool issued_sse_ret_error;
3265   static bool issued_x87_ret_error;
3266
3267   enum machine_mode tmpmode;
3268   int bytes =
3269     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3270   enum x86_64_reg_class class[MAX_CLASSES];
3271   int n;
3272   int i;
3273   int nexps = 0;
3274   int needed_sseregs, needed_intregs;
3275   rtx exp[MAX_CLASSES];
3276   rtx ret;
3277
3278   n = classify_argument (mode, type, class, 0);
3279   if (TARGET_DEBUG_ARG)
3280     {
3281       if (!n)
3282         fprintf (stderr, "Memory class\n");
3283       else
3284         {
3285           fprintf (stderr, "Classes:");
3286           for (i = 0; i < n; i++)
3287             {
3288               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
3289             }
3290            fprintf (stderr, "\n");
3291         }
3292     }
3293   if (!n)
3294     return NULL;
3295   if (!examine_argument (mode, type, in_return, &needed_intregs,
3296                          &needed_sseregs))
3297     return NULL;
3298   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
3299     return NULL;
3300
3301   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
3302      some less clueful developer tries to use floating-point anyway.  */
3303   if (needed_sseregs && !TARGET_SSE)
3304     {
3305       if (in_return)
3306         {
3307           if (!issued_sse_ret_error)
3308             {
3309               error ("SSE register return with SSE disabled");
3310               issued_sse_ret_error = true;
3311             }
3312         }
3313       else if (!issued_sse_arg_error)
3314         {
3315           error ("SSE register argument with SSE disabled");
3316           issued_sse_arg_error = true;
3317         }
3318       return NULL;
3319     }
3320
3321   /* Likewise, error if the ABI requires us to return values in the
3322      x87 registers and the user specified -mno-80387.  */
3323   if (!TARGET_80387 && in_return)
3324     for (i = 0; i < n; i++)
3325       if (class[i] == X86_64_X87_CLASS
3326           || class[i] == X86_64_X87UP_CLASS
3327           || class[i] == X86_64_COMPLEX_X87_CLASS)
3328         {
3329           if (!issued_x87_ret_error)
3330             {
3331               error ("x87 register return with x87 disabled");
3332               issued_x87_ret_error = true;
3333             }
3334           return NULL;
3335         }
3336
3337   /* First construct simple cases.  Avoid SCmode, since we want to use
3338      single register to pass this type.  */
3339   if (n == 1 && mode != SCmode)
3340     switch (class[0])
3341       {
3342       case X86_64_INTEGER_CLASS:
3343       case X86_64_INTEGERSI_CLASS:
3344         return gen_rtx_REG (mode, intreg[0]);
3345       case X86_64_SSE_CLASS:
3346       case X86_64_SSESF_CLASS:
3347       case X86_64_SSEDF_CLASS:
3348         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
3349       case X86_64_X87_CLASS:
3350       case X86_64_COMPLEX_X87_CLASS:
3351         return gen_rtx_REG (mode, FIRST_STACK_REG);
3352       case X86_64_NO_CLASS:
3353         /* Zero sized array, struct or class.  */
3354         return NULL;
3355       default:
3356         gcc_unreachable ();
3357       }
3358   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
3359       && mode != BLKmode)
3360     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
3361   if (n == 2
3362       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
3363     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
3364   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
3365       && class[1] == X86_64_INTEGER_CLASS
3366       && (mode == CDImode || mode == TImode || mode == TFmode)
3367       && intreg[0] + 1 == intreg[1])
3368     return gen_rtx_REG (mode, intreg[0]);
3369
3370   /* Otherwise figure out the entries of the PARALLEL.  */
3371   for (i = 0; i < n; i++)
3372     {
3373       switch (class[i])
3374         {
3375           case X86_64_NO_CLASS:
3376             break;
3377           case X86_64_INTEGER_CLASS:
3378           case X86_64_INTEGERSI_CLASS:
3379             /* Merge TImodes on aligned occasions here too.  */
3380             if (i * 8 + 8 > bytes)
3381               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
3382             else if (class[i] == X86_64_INTEGERSI_CLASS)
3383               tmpmode = SImode;
3384             else
3385               tmpmode = DImode;
3386             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
3387             if (tmpmode == BLKmode)
3388               tmpmode = DImode;
3389             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3390                                                gen_rtx_REG (tmpmode, *intreg),
3391                                                GEN_INT (i*8));
3392             intreg++;
3393             break;
3394           case X86_64_SSESF_CLASS:
3395             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3396                                                gen_rtx_REG (SFmode,
3397                                                             SSE_REGNO (sse_regno)),
3398                                                GEN_INT (i*8));
3399             sse_regno++;
3400             break;
3401           case X86_64_SSEDF_CLASS:
3402             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3403                                                gen_rtx_REG (DFmode,
3404                                                             SSE_REGNO (sse_regno)),
3405                                                GEN_INT (i*8));
3406             sse_regno++;
3407             break;
3408           case X86_64_SSE_CLASS:
3409             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
3410               tmpmode = TImode;
3411             else
3412               tmpmode = DImode;
3413             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
3414                                                gen_rtx_REG (tmpmode,
3415                                                             SSE_REGNO (sse_regno)),
3416                                                GEN_INT (i*8));
3417             if (tmpmode == TImode)
3418               i++;
3419             sse_regno++;
3420             break;
3421           default:
3422             gcc_unreachable ();
3423         }
3424     }
3425
3426   /* Empty aligned struct, union or class.  */
3427   if (nexps == 0)
3428     return NULL;
3429
3430   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
3431   for (i = 0; i < nexps; i++)
3432     XVECEXP (ret, 0, i) = exp [i];
3433   return ret;
3434 }
3435
3436 /* Update the data in CUM to advance over an argument
3437    of mode MODE and data type TYPE.
3438    (TYPE is null for libcalls where that information may not be available.)  */
3439
3440 void
3441 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3442                       tree type, int named)
3443 {
3444   int bytes =
3445     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3446   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3447
3448   if (type)
3449     mode = type_natural_mode (type);
3450
3451   if (TARGET_DEBUG_ARG)
3452     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
3453              "mode=%s, named=%d)\n\n",
3454              words, cum->words, cum->nregs, cum->sse_nregs,
3455              GET_MODE_NAME (mode), named);
3456
3457   if (TARGET_64BIT)
3458     {
3459       int int_nregs, sse_nregs;
3460       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
3461         cum->words += words;
3462       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
3463         {
3464           cum->nregs -= int_nregs;
3465           cum->sse_nregs -= sse_nregs;
3466           cum->regno += int_nregs;
3467           cum->sse_regno += sse_nregs;
3468         }
3469       else
3470         cum->words += words;
3471     }
3472   else
3473     {
3474       switch (mode)
3475         {
3476         default:
3477           break;
3478
3479         case BLKmode:
3480           if (bytes < 0)
3481             break;
3482           /* FALLTHRU */
3483
3484         case DImode:
3485         case SImode:
3486         case HImode:
3487         case QImode:
3488           cum->words += words;
3489           cum->nregs -= words;
3490           cum->regno += words;
3491
3492           if (cum->nregs <= 0)
3493             {
3494               cum->nregs = 0;
3495               cum->regno = 0;
3496             }
3497           break;
3498
3499         case DFmode:
3500           if (cum->float_in_sse < 2)
3501             break;
3502         case SFmode:
3503           if (cum->float_in_sse < 1)
3504             break;
3505           /* FALLTHRU */
3506
3507         case TImode:
3508         case V16QImode:
3509         case V8HImode:
3510         case V4SImode:
3511         case V2DImode:
3512         case V4SFmode:
3513         case V2DFmode:
3514           if (!type || !AGGREGATE_TYPE_P (type))
3515             {
3516               cum->sse_words += words;
3517               cum->sse_nregs -= 1;
3518               cum->sse_regno += 1;
3519               if (cum->sse_nregs <= 0)
3520                 {
3521                   cum->sse_nregs = 0;
3522                   cum->sse_regno = 0;
3523                 }
3524             }
3525           break;
3526
3527         case V8QImode:
3528         case V4HImode:
3529         case V2SImode:
3530         case V2SFmode:
3531           if (!type || !AGGREGATE_TYPE_P (type))
3532             {
3533               cum->mmx_words += words;
3534               cum->mmx_nregs -= 1;
3535               cum->mmx_regno += 1;
3536               if (cum->mmx_nregs <= 0)
3537                 {
3538                   cum->mmx_nregs = 0;
3539                   cum->mmx_regno = 0;
3540                 }
3541             }
3542           break;
3543         }
3544     }
3545 }
3546
3547 /* Define where to put the arguments to a function.
3548    Value is zero to push the argument on the stack,
3549    or a hard register in which to store the argument.
3550
3551    MODE is the argument's machine mode.
3552    TYPE is the data type of the argument (as a tree).
3553     This is null for libcalls where that information may
3554     not be available.
3555    CUM is a variable of type CUMULATIVE_ARGS which gives info about
3556     the preceding args and about the function being called.
3557    NAMED is nonzero if this argument is a named parameter
3558     (otherwise it is an extra parameter matching an ellipsis).  */
3559
3560 rtx
3561 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
3562               tree type, int named)
3563 {
3564   enum machine_mode mode = orig_mode;
3565   rtx ret = NULL_RTX;
3566   int bytes =
3567     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
3568   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3569   static bool warnedsse, warnedmmx;
3570
3571   /* To simplify the code below, represent vector types with a vector mode
3572      even if MMX/SSE are not active.  */
3573   if (type && TREE_CODE (type) == VECTOR_TYPE)
3574     mode = type_natural_mode (type);
3575
3576   /* Handle a hidden AL argument containing number of registers for varargs
3577      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
3578      any AL settings.  */
3579   if (mode == VOIDmode)
3580     {
3581       if (TARGET_64BIT)
3582         return GEN_INT (cum->maybe_vaarg
3583                         ? (cum->sse_nregs < 0
3584                            ? SSE_REGPARM_MAX
3585                            : cum->sse_regno)
3586                         : -1);
3587       else
3588         return constm1_rtx;
3589     }
3590   if (TARGET_64BIT)
3591     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
3592                                cum->sse_nregs,
3593                                &x86_64_int_parameter_registers [cum->regno],
3594                                cum->sse_regno);
3595   else
3596     switch (mode)
3597       {
3598         /* For now, pass fp/complex values on the stack.  */
3599       default:
3600         break;
3601
3602       case BLKmode:
3603         if (bytes < 0)
3604           break;
3605         /* FALLTHRU */
3606       case DImode:
3607       case SImode:
3608       case HImode:
3609       case QImode:
3610         if (words <= cum->nregs)
3611           {
3612             int regno = cum->regno;
3613
3614             /* Fastcall allocates the first two DWORD (SImode) or
3615                smaller arguments to ECX and EDX.  */
3616             if (cum->fastcall)
3617               {
3618                 if (mode == BLKmode || mode == DImode)
3619                   break;
3620
3621                 /* ECX not EAX is the first allocated register.  */
3622                 if (regno == 0)
3623                   regno = 2;
3624               }
3625             ret = gen_rtx_REG (mode, regno);
3626           }
3627         break;
3628       case DFmode:
3629         if (cum->float_in_sse < 2)
3630           break;
3631       case SFmode:
3632         if (cum->float_in_sse < 1)
3633           break;
3634         /* FALLTHRU */
3635       case TImode:
3636       case V16QImode:
3637       case V8HImode:
3638       case V4SImode:
3639       case V2DImode:
3640       case V4SFmode:
3641       case V2DFmode:
3642         if (!type || !AGGREGATE_TYPE_P (type))
3643           {
3644             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
3645               {
3646                 warnedsse = true;
3647                 warning (0, "SSE vector argument without SSE enabled "
3648                          "changes the ABI");
3649               }
3650             if (cum->sse_nregs)
3651               ret = gen_reg_or_parallel (mode, orig_mode,
3652                                          cum->sse_regno + FIRST_SSE_REG);
3653           }
3654         break;
3655       case V8QImode:
3656       case V4HImode:
3657       case V2SImode:
3658       case V2SFmode:
3659         if (!type || !AGGREGATE_TYPE_P (type))
3660           {
3661             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3662               {
3663                 warnedmmx = true;
3664                 warning (0, "MMX vector argument without MMX enabled "
3665                          "changes the ABI");
3666               }
3667             if (cum->mmx_nregs)
3668               ret = gen_reg_or_parallel (mode, orig_mode,
3669                                          cum->mmx_regno + FIRST_MMX_REG);
3670           }
3671         break;
3672       }
3673
3674   if (TARGET_DEBUG_ARG)
3675     {
3676       fprintf (stderr,
3677                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3678                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3679
3680       if (ret)
3681         print_simple_rtl (stderr, ret);
3682       else
3683         fprintf (stderr, ", stack");
3684
3685       fprintf (stderr, " )\n");
3686     }
3687
3688   return ret;
3689 }
3690
3691 /* A C expression that indicates when an argument must be passed by
3692    reference.  If nonzero for an argument, a copy of that argument is
3693    made in memory and a pointer to the argument is passed instead of
3694    the argument itself.  The pointer is passed in whatever way is
3695    appropriate for passing a pointer to that type.  */
3696
3697 static bool
3698 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3699                         enum machine_mode mode ATTRIBUTE_UNUSED,
3700                         tree type, bool named ATTRIBUTE_UNUSED)
3701 {
3702   if (!TARGET_64BIT)
3703     return 0;
3704
3705   if (type && int_size_in_bytes (type) == -1)
3706     {
3707       if (TARGET_DEBUG_ARG)
3708         fprintf (stderr, "function_arg_pass_by_reference\n");
3709       return 1;
3710     }
3711
3712   return 0;
3713 }
3714
3715 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3716    ABI.  Only called if TARGET_SSE.  */
3717 static bool
3718 contains_128bit_aligned_vector_p (tree type)
3719 {
3720   enum machine_mode mode = TYPE_MODE (type);
3721   if (SSE_REG_MODE_P (mode)
3722       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3723     return true;
3724   if (TYPE_ALIGN (type) < 128)
3725     return false;
3726
3727   if (AGGREGATE_TYPE_P (type))
3728     {
3729       /* Walk the aggregates recursively.  */
3730       switch (TREE_CODE (type))
3731         {
3732         case RECORD_TYPE:
3733         case UNION_TYPE:
3734         case QUAL_UNION_TYPE:
3735           {
3736             tree field;
3737
3738             if (TYPE_BINFO (type))
3739               {
3740                 tree binfo, base_binfo;
3741                 int i;
3742
3743                 for (binfo = TYPE_BINFO (type), i = 0;
3744                      BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3745                   if (contains_128bit_aligned_vector_p
3746                       (BINFO_TYPE (base_binfo)))
3747                     return true;
3748               }
3749             /* And now merge the fields of structure.  */
3750             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3751               {
3752                 if (TREE_CODE (field) == FIELD_DECL
3753                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3754                   return true;
3755               }
3756             break;
3757           }
3758
3759         case ARRAY_TYPE:
3760           /* Just for use if some languages passes arrays by value.  */
3761           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3762             return true;
3763           break;
3764
3765         default:
3766           gcc_unreachable ();
3767         }
3768     }
3769   return false;
3770 }
3771
3772 /* Gives the alignment boundary, in bits, of an argument with the
3773    specified mode and type.  */
3774
3775 int
3776 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3777 {
3778   int align;
3779   if (type)
3780     align = TYPE_ALIGN (type);
3781   else
3782     align = GET_MODE_ALIGNMENT (mode);
3783   if (align < PARM_BOUNDARY)
3784     align = PARM_BOUNDARY;
3785   if (!TARGET_64BIT)
3786     {
3787       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3788          make an exception for SSE modes since these require 128bit
3789          alignment.
3790
3791          The handling here differs from field_alignment.  ICC aligns MMX
3792          arguments to 4 byte boundaries, while structure fields are aligned
3793          to 8 byte boundaries.  */
3794       if (!TARGET_SSE)
3795         align = PARM_BOUNDARY;
3796       else if (!type)
3797         {
3798           if (!SSE_REG_MODE_P (mode))
3799             align = PARM_BOUNDARY;
3800         }
3801       else
3802         {
3803           if (!contains_128bit_aligned_vector_p (type))
3804             align = PARM_BOUNDARY;
3805         }
3806     }
3807   if (align > 128)
3808     align = 128;
3809   return align;
3810 }
3811
3812 /* Return true if N is a possible register number of function value.  */
3813 bool
3814 ix86_function_value_regno_p (int regno)
3815 {
3816   if (regno == 0
3817       || (regno == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
3818       || (regno == FIRST_SSE_REG && TARGET_SSE))
3819     return true;
3820
3821   if (!TARGET_64BIT
3822       && (regno == FIRST_MMX_REG && TARGET_MMX))
3823         return true;
3824
3825   return false;
3826 }
3827
3828 /* Define how to find the value returned by a function.
3829    VALTYPE is the data type of the value (as a tree).
3830    If the precise function being called is known, FUNC is its FUNCTION_DECL;
3831    otherwise, FUNC is 0.  */
3832 rtx
3833 ix86_function_value (tree valtype, tree fntype_or_decl,
3834                      bool outgoing ATTRIBUTE_UNUSED)
3835 {
3836   enum machine_mode natmode = type_natural_mode (valtype);
3837
3838   if (TARGET_64BIT)
3839     {
3840       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
3841                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
3842                                      x86_64_int_return_registers, 0);
3843       /* For zero sized structures, construct_container return NULL, but we
3844          need to keep rest of compiler happy by returning meaningful value.  */
3845       if (!ret)
3846         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
3847       return ret;
3848     }
3849   else
3850     {
3851       tree fn = NULL_TREE, fntype;
3852       if (fntype_or_decl
3853           && DECL_P (fntype_or_decl))
3854         fn = fntype_or_decl;
3855       fntype = fn ? TREE_TYPE (fn) : fntype_or_decl;
3856       return gen_rtx_REG (TYPE_MODE (valtype),
3857                           ix86_value_regno (natmode, fn, fntype));
3858     }
3859 }
3860
3861 /* Return true iff type is returned in memory.  */
3862 int
3863 ix86_return_in_memory (tree type)
3864 {
3865   int needed_intregs, needed_sseregs, size;
3866   enum machine_mode mode = type_natural_mode (type);
3867
3868   if (TARGET_64BIT)
3869     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
3870
3871   if (mode == BLKmode)
3872     return 1;
3873
3874   size = int_size_in_bytes (type);
3875
3876   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
3877     return 0;
3878
3879   if (VECTOR_MODE_P (mode) || mode == TImode)
3880     {
3881       /* User-created vectors small enough to fit in EAX.  */
3882       if (size < 8)
3883         return 0;
3884
3885       /* MMX/3dNow values are returned in MM0,
3886          except when it doesn't exits.  */
3887       if (size == 8)
3888         return (TARGET_MMX ? 0 : 1);
3889
3890       /* SSE values are returned in XMM0, except when it doesn't exist.  */
3891       if (size == 16)
3892         return (TARGET_SSE ? 0 : 1);
3893     }
3894
3895   if (mode == XFmode)
3896     return 0;
3897
3898   if (mode == TDmode)
3899     return 1;
3900
3901   if (size > 12)
3902     return 1;
3903   return 0;
3904 }
3905
3906 /* When returning SSE vector types, we have a choice of either
3907      (1) being abi incompatible with a -march switch, or
3908      (2) generating an error.
3909    Given no good solution, I think the safest thing is one warning.
3910    The user won't be able to use -Werror, but....
3911
3912    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
3913    called in response to actually generating a caller or callee that
3914    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3915    via aggregate_value_p for general type probing from tree-ssa.  */
3916
3917 static rtx
3918 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3919 {
3920   static bool warnedsse, warnedmmx;
3921
3922   if (type)
3923     {
3924       /* Look at the return type of the function, not the function type.  */
3925       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3926
3927       if (!TARGET_SSE && !warnedsse)
3928         {
3929           if (mode == TImode
3930               || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3931             {
3932               warnedsse = true;
3933               warning (0, "SSE vector return without SSE enabled "
3934                        "changes the ABI");
3935             }
3936         }
3937
3938       if (!TARGET_MMX && !warnedmmx)
3939         {
3940           if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
3941             {
3942               warnedmmx = true;
3943               warning (0, "MMX vector return without MMX enabled "
3944                        "changes the ABI");
3945             }
3946         }
3947     }
3948
3949   return NULL;
3950 }
3951
3952 /* Define how to find the value returned by a library function
3953    assuming the value has mode MODE.  */
3954 rtx
3955 ix86_libcall_value (enum machine_mode mode)
3956 {
3957   if (TARGET_64BIT)
3958     {
3959       switch (mode)
3960         {
3961         case SFmode:
3962         case SCmode:
3963         case DFmode:
3964         case DCmode:
3965         case TFmode:
3966         case SDmode:
3967         case DDmode:
3968         case TDmode:
3969           return gen_rtx_REG (mode, FIRST_SSE_REG);
3970         case XFmode:
3971         case XCmode:
3972           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
3973         case TCmode:
3974           return NULL;
3975         default:
3976           return gen_rtx_REG (mode, 0);
3977         }
3978     }
3979   else
3980     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL, NULL));
3981 }
3982
3983 /* Given a mode, return the register to use for a return value.  */
3984
3985 static int
3986 ix86_value_regno (enum machine_mode mode, tree func, tree fntype)
3987 {
3988   gcc_assert (!TARGET_64BIT);
3989
3990   /* 8-byte vector modes in %mm0. See ix86_return_in_memory for where
3991      we normally prevent this case when mmx is not available.  However
3992      some ABIs may require the result to be returned like DImode.  */
3993   if (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 8)
3994     return TARGET_MMX ? FIRST_MMX_REG : 0;
3995
3996   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
3997      we prevent this case when sse is not available.  However some ABIs
3998      may require the result to be returned like integer TImode.  */
3999   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
4000     return TARGET_SSE ? FIRST_SSE_REG : 0;
4001
4002   /* Decimal floating point values can go in %eax, unlike other float modes.  */
4003   if (DECIMAL_FLOAT_MODE_P (mode))
4004     return 0;
4005
4006   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
4007   if (!SCALAR_FLOAT_MODE_P (mode) || !TARGET_FLOAT_RETURNS_IN_80387)
4008     return 0;
4009
4010   /* Floating point return values in %st(0), except for local functions when
4011      SSE math is enabled or for functions with sseregparm attribute.  */
4012   if ((func || fntype)
4013       && (mode == SFmode || mode == DFmode))
4014     {
4015       int sse_level = ix86_function_sseregparm (fntype, func);
4016       if ((sse_level >= 1 && mode == SFmode)
4017           || (sse_level == 2 && mode == DFmode))
4018         return FIRST_SSE_REG;
4019     }
4020
4021   return FIRST_FLOAT_REG;
4022 }
4023 \f
4024 /* Create the va_list data type.  */
4025
4026 static tree
4027 ix86_build_builtin_va_list (void)
4028 {
4029   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
4030
4031   /* For i386 we use plain pointer to argument area.  */
4032   if (!TARGET_64BIT)
4033     return build_pointer_type (char_type_node);
4034
4035   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
4036   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
4037
4038   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
4039                       unsigned_type_node);
4040   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
4041                       unsigned_type_node);
4042   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
4043                       ptr_type_node);
4044   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
4045                       ptr_type_node);
4046
4047   va_list_gpr_counter_field = f_gpr;
4048   va_list_fpr_counter_field = f_fpr;
4049
4050   DECL_FIELD_CONTEXT (f_gpr) = record;
4051   DECL_FIELD_CONTEXT (f_fpr) = record;
4052   DECL_FIELD_CONTEXT (f_ovf) = record;
4053   DECL_FIELD_CONTEXT (f_sav) = record;
4054
4055   TREE_CHAIN (record) = type_decl;
4056   TYPE_NAME (record) = type_decl;
4057   TYPE_FIELDS (record) = f_gpr;
4058   TREE_CHAIN (f_gpr) = f_fpr;
4059   TREE_CHAIN (f_fpr) = f_ovf;
4060   TREE_CHAIN (f_ovf) = f_sav;
4061
4062   layout_type (record);
4063
4064   /* The correct type is an array type of one element.  */
4065   return build_array_type (record, build_index_type (size_zero_node));
4066 }
4067
4068 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
4069
4070 static void
4071 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
4072                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
4073                              int no_rtl)
4074 {
4075   CUMULATIVE_ARGS next_cum;
4076   rtx save_area = NULL_RTX, mem;
4077   rtx label;
4078   rtx label_ref;
4079   rtx tmp_reg;
4080   rtx nsse_reg;
4081   int set;
4082   tree fntype;
4083   int stdarg_p;
4084   int i;
4085
4086   if (!TARGET_64BIT)
4087     return;
4088
4089   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
4090     return;
4091
4092   /* Indicate to allocate space on the stack for varargs save area.  */
4093   ix86_save_varrargs_registers = 1;
4094
4095   cfun->stack_alignment_needed = 128;
4096
4097   fntype = TREE_TYPE (current_function_decl);
4098   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
4099               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
4100                   != void_type_node));
4101
4102   /* For varargs, we do not want to skip the dummy va_dcl argument.
4103      For stdargs, we do want to skip the last named argument.  */
4104   next_cum = *cum;
4105   if (stdarg_p)
4106     function_arg_advance (&next_cum, mode, type, 1);
4107
4108   if (!no_rtl)
4109     save_area = frame_pointer_rtx;
4110
4111   set = get_varargs_alias_set ();
4112
4113   for (i = next_cum.regno;
4114        i < ix86_regparm
4115        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
4116        i++)
4117     {
4118       mem = gen_rtx_MEM (Pmode,
4119                          plus_constant (save_area, i * UNITS_PER_WORD));
4120       MEM_NOTRAP_P (mem) = 1;
4121       set_mem_alias_set (mem, set);
4122       emit_move_insn (mem, gen_rtx_REG (Pmode,
4123                                         x86_64_int_parameter_registers[i]));
4124     }
4125
4126   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
4127     {
4128       /* Now emit code to save SSE registers.  The AX parameter contains number
4129          of SSE parameter registers used to call this function.  We use
4130          sse_prologue_save insn template that produces computed jump across
4131          SSE saves.  We need some preparation work to get this working.  */
4132
4133       label = gen_label_rtx ();
4134       label_ref = gen_rtx_LABEL_REF (Pmode, label);
4135
4136       /* Compute address to jump to :
4137          label - 5*eax + nnamed_sse_arguments*5  */
4138       tmp_reg = gen_reg_rtx (Pmode);
4139       nsse_reg = gen_reg_rtx (Pmode);
4140       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
4141       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4142                               gen_rtx_MULT (Pmode, nsse_reg,
4143                                             GEN_INT (4))));
4144       if (next_cum.sse_regno)
4145         emit_move_insn
4146           (nsse_reg,
4147            gen_rtx_CONST (DImode,
4148                           gen_rtx_PLUS (DImode,
4149                                         label_ref,
4150                                         GEN_INT (next_cum.sse_regno * 4))));
4151       else
4152         emit_move_insn (nsse_reg, label_ref);
4153       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
4154
4155       /* Compute address of memory block we save into.  We always use pointer
4156          pointing 127 bytes after first byte to store - this is needed to keep
4157          instruction size limited by 4 bytes.  */
4158       tmp_reg = gen_reg_rtx (Pmode);
4159       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
4160                               plus_constant (save_area,
4161                                              8 * REGPARM_MAX + 127)));
4162       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
4163       MEM_NOTRAP_P (mem) = 1;
4164       set_mem_alias_set (mem, set);
4165       set_mem_align (mem, BITS_PER_WORD);
4166
4167       /* And finally do the dirty job!  */
4168       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
4169                                         GEN_INT (next_cum.sse_regno), label));
4170     }
4171
4172 }
4173
4174 /* Implement va_start.  */
4175
4176 void
4177 ix86_va_start (tree valist, rtx nextarg)
4178 {
4179   HOST_WIDE_INT words, n_gpr, n_fpr;
4180   tree f_gpr, f_fpr, f_ovf, f_sav;
4181   tree gpr, fpr, ovf, sav, t;
4182   tree type;
4183
4184   /* Only 64bit target needs something special.  */
4185   if (!TARGET_64BIT)
4186     {
4187       std_expand_builtin_va_start (valist, nextarg);
4188       return;
4189     }
4190
4191   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4192   f_fpr = TREE_CHAIN (f_gpr);
4193   f_ovf = TREE_CHAIN (f_fpr);
4194   f_sav = TREE_CHAIN (f_ovf);
4195
4196   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
4197   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4198   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4199   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4200   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4201
4202   /* Count number of gp and fp argument registers used.  */
4203   words = current_function_args_info.words;
4204   n_gpr = current_function_args_info.regno;
4205   n_fpr = current_function_args_info.sse_regno;
4206
4207   if (TARGET_DEBUG_ARG)
4208     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
4209              (int) words, (int) n_gpr, (int) n_fpr);
4210
4211   if (cfun->va_list_gpr_size)
4212     {
4213       type = TREE_TYPE (gpr);
4214       t = build2 (MODIFY_EXPR, type, gpr,
4215                   build_int_cst (type, n_gpr * 8));
4216       TREE_SIDE_EFFECTS (t) = 1;
4217       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4218     }
4219
4220   if (cfun->va_list_fpr_size)
4221     {
4222       type = TREE_TYPE (fpr);
4223       t = build2 (MODIFY_EXPR, type, fpr,
4224                   build_int_cst (type, n_fpr * 16 + 8*REGPARM_MAX));
4225       TREE_SIDE_EFFECTS (t) = 1;
4226       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4227     }
4228
4229   /* Find the overflow area.  */
4230   type = TREE_TYPE (ovf);
4231   t = make_tree (type, virtual_incoming_args_rtx);
4232   if (words != 0)
4233     t = build2 (PLUS_EXPR, type, t,
4234                 build_int_cst (type, words * UNITS_PER_WORD));
4235   t = build2 (MODIFY_EXPR, type, ovf, t);
4236   TREE_SIDE_EFFECTS (t) = 1;
4237   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4238
4239   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
4240     {
4241       /* Find the register save area.
4242          Prologue of the function save it right above stack frame.  */
4243       type = TREE_TYPE (sav);
4244       t = make_tree (type, frame_pointer_rtx);
4245       t = build2 (MODIFY_EXPR, type, sav, t);
4246       TREE_SIDE_EFFECTS (t) = 1;
4247       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
4248     }
4249 }
4250
4251 /* Implement va_arg.  */
4252
4253 tree
4254 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
4255 {
4256   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
4257   tree f_gpr, f_fpr, f_ovf, f_sav;
4258   tree gpr, fpr, ovf, sav, t;
4259   int size, rsize;
4260   tree lab_false, lab_over = NULL_TREE;
4261   tree addr, t2;
4262   rtx container;
4263   int indirect_p = 0;
4264   tree ptrtype;
4265   enum machine_mode nat_mode;
4266
4267   /* Only 64bit target needs something special.  */
4268   if (!TARGET_64BIT)
4269     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
4270
4271   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
4272   f_fpr = TREE_CHAIN (f_gpr);
4273   f_ovf = TREE_CHAIN (f_fpr);
4274   f_sav = TREE_CHAIN (f_ovf);
4275
4276   valist = build_va_arg_indirect_ref (valist);
4277   gpr = build3 (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
4278   fpr = build3 (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
4279   ovf = build3 (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
4280   sav = build3 (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
4281
4282   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
4283   if (indirect_p)
4284     type = build_pointer_type (type);
4285   size = int_size_in_bytes (type);
4286   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
4287
4288   nat_mode = type_natural_mode (type);
4289   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
4290                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
4291
4292   /* Pull the value out of the saved registers.  */
4293
4294   addr = create_tmp_var (ptr_type_node, "addr");
4295   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
4296
4297   if (container)
4298     {
4299       int needed_intregs, needed_sseregs;
4300       bool need_temp;
4301       tree int_addr, sse_addr;
4302
4303       lab_false = create_artificial_label ();
4304       lab_over = create_artificial_label ();
4305
4306       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
4307
4308       need_temp = (!REG_P (container)
4309                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
4310                        || TYPE_ALIGN (type) > 128));
4311
4312       /* In case we are passing structure, verify that it is consecutive block
4313          on the register save area.  If not we need to do moves.  */
4314       if (!need_temp && !REG_P (container))
4315         {
4316           /* Verify that all registers are strictly consecutive  */
4317           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
4318             {
4319               int i;
4320
4321               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4322                 {
4323                   rtx slot = XVECEXP (container, 0, i);
4324                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
4325                       || INTVAL (XEXP (slot, 1)) != i * 16)
4326                     need_temp = 1;
4327                 }
4328             }
4329           else
4330             {
4331               int i;
4332
4333               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
4334                 {
4335                   rtx slot = XVECEXP (container, 0, i);
4336                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
4337                       || INTVAL (XEXP (slot, 1)) != i * 8)
4338                     need_temp = 1;
4339                 }
4340             }
4341         }
4342       if (!need_temp)
4343         {
4344           int_addr = addr;
4345           sse_addr = addr;
4346         }
4347       else
4348         {
4349           int_addr = create_tmp_var (ptr_type_node, "int_addr");
4350           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
4351           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
4352           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
4353         }
4354
4355       /* First ensure that we fit completely in registers.  */
4356       if (needed_intregs)
4357         {
4358           t = build_int_cst (TREE_TYPE (gpr),
4359                              (REGPARM_MAX - needed_intregs + 1) * 8);
4360           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
4361           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4362           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4363           gimplify_and_add (t, pre_p);
4364         }
4365       if (needed_sseregs)
4366         {
4367           t = build_int_cst (TREE_TYPE (fpr),
4368                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
4369                              + REGPARM_MAX * 8);
4370           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
4371           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
4372           t = build3 (COND_EXPR, void_type_node, t, t2, NULL_TREE);
4373           gimplify_and_add (t, pre_p);
4374         }
4375
4376       /* Compute index to start of area used for integer regs.  */
4377       if (needed_intregs)
4378         {
4379           /* int_addr = gpr + sav; */
4380           t = fold_convert (ptr_type_node, gpr);
4381           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4382           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
4383           gimplify_and_add (t, pre_p);
4384         }
4385       if (needed_sseregs)
4386         {
4387           /* sse_addr = fpr + sav; */
4388           t = fold_convert (ptr_type_node, fpr);
4389           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
4390           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
4391           gimplify_and_add (t, pre_p);
4392         }
4393       if (need_temp)
4394         {
4395           int i;
4396           tree temp = create_tmp_var (type, "va_arg_tmp");
4397
4398           /* addr = &temp; */
4399           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
4400           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
4401           gimplify_and_add (t, pre_p);
4402
4403           for (i = 0; i < XVECLEN (container, 0); i++)
4404             {
4405               rtx slot = XVECEXP (container, 0, i);
4406               rtx reg = XEXP (slot, 0);
4407               enum machine_mode mode = GET_MODE (reg);
4408               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
4409               tree addr_type = build_pointer_type (piece_type);
4410               tree src_addr, src;
4411               int src_offset;
4412               tree dest_addr, dest;
4413
4414               if (SSE_REGNO_P (REGNO (reg)))
4415                 {
4416                   src_addr = sse_addr;
4417                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
4418                 }
4419               else
4420                 {
4421                   src_addr = int_addr;
4422                   src_offset = REGNO (reg) * 8;
4423                 }
4424               src_addr = fold_convert (addr_type, src_addr);
4425               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
4426                                        size_int (src_offset)));
4427               src = build_va_arg_indirect_ref (src_addr);
4428
4429               dest_addr = fold_convert (addr_type, addr);
4430               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
4431                                         size_int (INTVAL (XEXP (slot, 1)))));
4432               dest = build_va_arg_indirect_ref (dest_addr);
4433
4434               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
4435               gimplify_and_add (t, pre_p);
4436             }
4437         }
4438
4439       if (needed_intregs)
4440         {
4441           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
4442                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
4443           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
4444           gimplify_and_add (t, pre_p);
4445         }
4446       if (needed_sseregs)
4447         {
4448           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
4449                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
4450           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
4451           gimplify_and_add (t, pre_p);
4452         }
4453
4454       t = build1 (GOTO_EXPR, void_type_node, lab_over);
4455       gimplify_and_add (t, pre_p);
4456
4457       t = build1 (LABEL_EXPR, void_type_node, lab_false);
4458       append_to_statement_list (t, pre_p);
4459     }
4460
4461   /* ... otherwise out of the overflow area.  */
4462
4463   /* Care for on-stack alignment if needed.  */
4464   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64
4465       || integer_zerop (TYPE_SIZE (type)))
4466     t = ovf;
4467   else
4468     {
4469       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
4470       t = build2 (PLUS_EXPR, TREE_TYPE (ovf), ovf,
4471                   build_int_cst (TREE_TYPE (ovf), align - 1));
4472       t = build2 (BIT_AND_EXPR, TREE_TYPE (t), t,
4473                   build_int_cst (TREE_TYPE (t), -align));
4474     }
4475   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
4476
4477   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
4478   gimplify_and_add (t2, pre_p);
4479
4480   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
4481               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
4482   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
4483   gimplify_and_add (t, pre_p);
4484
4485   if (container)
4486     {
4487       t = build1 (LABEL_EXPR, void_type_node, lab_over);
4488       append_to_statement_list (t, pre_p);
4489     }
4490
4491   ptrtype = build_pointer_type (type);
4492   addr = fold_convert (ptrtype, addr);
4493
4494   if (indirect_p)
4495     addr = build_va_arg_indirect_ref (addr);
4496   return build_va_arg_indirect_ref (addr);
4497 }
4498 \f
4499 /* Return nonzero if OPNUM's MEM should be matched
4500    in movabs* patterns.  */
4501
4502 int
4503 ix86_check_movabs (rtx insn, int opnum)
4504 {
4505   rtx set, mem;
4506
4507   set = PATTERN (insn);
4508   if (GET_CODE (set) == PARALLEL)
4509     set = XVECEXP (set, 0, 0);
4510   gcc_assert (GET_CODE (set) == SET);
4511   mem = XEXP (set, opnum);
4512   while (GET_CODE (mem) == SUBREG)
4513     mem = SUBREG_REG (mem);
4514   gcc_assert (GET_CODE (mem) == MEM);
4515   return (volatile_ok || !MEM_VOLATILE_P (mem));
4516 }
4517 \f
4518 /* Initialize the table of extra 80387 mathematical constants.  */
4519
4520 static void
4521 init_ext_80387_constants (void)
4522 {
4523   static const char * cst[5] =
4524   {
4525     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
4526     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
4527     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
4528     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
4529     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
4530   };
4531   int i;
4532
4533   for (i = 0; i < 5; i++)
4534     {
4535       real_from_string (&ext_80387_constants_table[i], cst[i]);
4536       /* Ensure each constant is rounded to XFmode precision.  */
4537       real_convert (&ext_80387_constants_table[i],
4538                     XFmode, &ext_80387_constants_table[i]);
4539     }
4540
4541   ext_80387_constants_init = 1;
4542 }
4543
4544 /* Return true if the constant is something that can be loaded with
4545    a special instruction.  */
4546
4547 int
4548 standard_80387_constant_p (rtx x)
4549 {
4550   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
4551     return -1;
4552
4553   if (x == CONST0_RTX (GET_MODE (x)))
4554     return 1;
4555   if (x == CONST1_RTX (GET_MODE (x)))
4556     return 2;
4557
4558   /* For XFmode constants, try to find a special 80387 instruction when
4559      optimizing for size or on those CPUs that benefit from them.  */
4560   if (GET_MODE (x) == XFmode
4561       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
4562     {
4563       REAL_VALUE_TYPE r;
4564       int i;
4565
4566       if (! ext_80387_constants_init)
4567         init_ext_80387_constants ();
4568
4569       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
4570       for (i = 0; i < 5; i++)
4571         if (real_identical (&r, &ext_80387_constants_table[i]))
4572           return i + 3;
4573     }
4574
4575   return 0;
4576 }
4577
4578 /* Return the opcode of the special instruction to be used to load
4579    the constant X.  */
4580
4581 const char *
4582 standard_80387_constant_opcode (rtx x)
4583 {
4584   switch (standard_80387_constant_p (x))
4585     {
4586     case 1:
4587       return "fldz";
4588     case 2:
4589       return "fld1";
4590     case 3:
4591       return "fldlg2";
4592     case 4:
4593       return "fldln2";
4594     case 5:
4595       return "fldl2e";
4596     case 6:
4597       return "fldl2t";
4598     case 7:
4599       return "fldpi";
4600     default:
4601       gcc_unreachable ();
4602     }
4603 }
4604
4605 /* Return the CONST_DOUBLE representing the 80387 constant that is
4606    loaded by the specified special instruction.  The argument IDX
4607    matches the return value from standard_80387_constant_p.  */
4608
4609 rtx
4610 standard_80387_constant_rtx (int idx)
4611 {
4612   int i;
4613
4614   if (! ext_80387_constants_init)
4615     init_ext_80387_constants ();
4616
4617   switch (idx)
4618     {
4619     case 3:
4620     case 4:
4621     case 5:
4622     case 6:
4623     case 7:
4624       i = idx - 3;
4625       break;
4626
4627     default:
4628       gcc_unreachable ();
4629     }
4630
4631   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
4632                                        XFmode);
4633 }
4634
4635 /* Return 1 if mode is a valid mode for sse.  */
4636 static int
4637 standard_sse_mode_p (enum machine_mode mode)
4638 {
4639   switch (mode)
4640     {
4641     case V16QImode:
4642     case V8HImode:
4643     case V4SImode:
4644     case V2DImode:
4645     case V4SFmode:
4646     case V2DFmode:
4647       return 1;
4648
4649     default:
4650       return 0;
4651     }
4652 }
4653
4654 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
4655  */
4656 int
4657 standard_sse_constant_p (rtx x)
4658 {
4659   enum machine_mode mode = GET_MODE (x);
4660
4661   if (x == const0_rtx || x == CONST0_RTX (GET_MODE (x)))
4662     return 1;
4663   if (vector_all_ones_operand (x, mode)
4664       && standard_sse_mode_p (mode))
4665     return TARGET_SSE2 ? 2 : -1;
4666
4667   return 0;
4668 }
4669
4670 /* Return the opcode of the special instruction to be used to load
4671    the constant X.  */
4672
4673 const char *
4674 standard_sse_constant_opcode (rtx insn, rtx x)
4675 {
4676   switch (standard_sse_constant_p (x))
4677     {
4678     case 1:
4679       if (get_attr_mode (insn) == MODE_V4SF)
4680         return "xorps\t%0, %0";
4681       else if (get_attr_mode (insn) == MODE_V2DF)
4682         return "xorpd\t%0, %0";
4683       else
4684         return "pxor\t%0, %0";
4685     case 2:
4686       return "pcmpeqd\t%0, %0";
4687     }
4688   gcc_unreachable ();
4689 }
4690
4691 /* Returns 1 if OP contains a symbol reference */
4692
4693 int
4694 symbolic_reference_mentioned_p (rtx op)
4695 {
4696   const char *fmt;
4697   int i;
4698
4699   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
4700     return 1;
4701
4702   fmt = GET_RTX_FORMAT (GET_CODE (op));
4703   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
4704     {
4705       if (fmt[i] == 'E')
4706         {
4707           int j;
4708
4709           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
4710             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
4711               return 1;
4712         }
4713
4714       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
4715         return 1;
4716     }
4717
4718   return 0;
4719 }
4720
4721 /* Return 1 if it is appropriate to emit `ret' instructions in the
4722    body of a function.  Do this only if the epilogue is simple, needing a
4723    couple of insns.  Prior to reloading, we can't tell how many registers
4724    must be saved, so return 0 then.  Return 0 if there is no frame
4725    marker to de-allocate.  */
4726
4727 int
4728 ix86_can_use_return_insn_p (void)
4729 {
4730   struct ix86_frame frame;
4731
4732   if (! reload_completed || frame_pointer_needed)
4733     return 0;
4734
4735   /* Don't allow more than 32 pop, since that's all we can do
4736      with one instruction.  */
4737   if (current_function_pops_args
4738       && current_function_args_size >= 32768)
4739     return 0;
4740
4741   ix86_compute_frame_layout (&frame);
4742   return frame.to_allocate == 0 && frame.nregs == 0;
4743 }
4744 \f
4745 /* Value should be nonzero if functions must have frame pointers.
4746    Zero means the frame pointer need not be set up (and parms may
4747    be accessed via the stack pointer) in functions that seem suitable.  */
4748
4749 int
4750 ix86_frame_pointer_required (void)
4751 {
4752   /* If we accessed previous frames, then the generated code expects
4753      to be able to access the saved ebp value in our frame.  */
4754   if (cfun->machine->accesses_prev_frame)
4755     return 1;
4756
4757   /* Several x86 os'es need a frame pointer for other reasons,
4758      usually pertaining to setjmp.  */
4759   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4760     return 1;
4761
4762   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4763      the frame pointer by default.  Turn it back on now if we've not
4764      got a leaf function.  */
4765   if (TARGET_OMIT_LEAF_FRAME_POINTER
4766       && (!current_function_is_leaf
4767           || ix86_current_function_calls_tls_descriptor))
4768     return 1;
4769
4770   if (current_function_profile)
4771     return 1;
4772
4773   return 0;
4774 }
4775
4776 /* Record that the current function accesses previous call frames.  */
4777
4778 void
4779 ix86_setup_frame_addresses (void)
4780 {
4781   cfun->machine->accesses_prev_frame = 1;
4782 }
4783 \f
4784 #if (defined(HAVE_GAS_HIDDEN) && (SUPPORTS_ONE_ONLY - 0)) || TARGET_MACHO
4785 # define USE_HIDDEN_LINKONCE 1
4786 #else
4787 # define USE_HIDDEN_LINKONCE 0
4788 #endif
4789
4790 static int pic_labels_used;
4791
4792 /* Fills in the label name that should be used for a pc thunk for
4793    the given register.  */
4794
4795 static void
4796 get_pc_thunk_name (char name[32], unsigned int regno)
4797 {
4798   gcc_assert (!TARGET_64BIT);
4799
4800   if (USE_HIDDEN_LINKONCE)
4801     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4802   else
4803     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4804 }
4805
4806
4807 /* This function generates code for -fpic that loads %ebx with
4808    the return address of the caller and then returns.  */
4809
4810 void
4811 ix86_file_end (void)
4812 {
4813   rtx xops[2];
4814   int regno;
4815
4816   for (regno = 0; regno < 8; ++regno)
4817     {
4818       char name[32];
4819
4820       if (! ((pic_labels_used >> regno) & 1))
4821         continue;
4822
4823       get_pc_thunk_name (name, regno);
4824
4825 #if TARGET_MACHO
4826       if (TARGET_MACHO)
4827         {
4828           switch_to_section (darwin_sections[text_coal_section]);
4829           fputs ("\t.weak_definition\t", asm_out_file);
4830           assemble_name (asm_out_file, name);
4831           fputs ("\n\t.private_extern\t", asm_out_file);
4832           assemble_name (asm_out_file, name);
4833           fputs ("\n", asm_out_file);
4834           ASM_OUTPUT_LABEL (asm_out_file, name);
4835         }
4836       else
4837 #endif
4838       if (USE_HIDDEN_LINKONCE)
4839         {
4840           tree decl;
4841
4842           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4843                              error_mark_node);
4844           TREE_PUBLIC (decl) = 1;
4845           TREE_STATIC (decl) = 1;
4846           DECL_ONE_ONLY (decl) = 1;
4847
4848           (*targetm.asm_out.unique_section) (decl, 0);
4849           switch_to_section (get_named_section (decl, NULL, 0));
4850
4851           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4852           fputs ("\t.hidden\t", asm_out_file);
4853           assemble_name (asm_out_file, name);
4854           fputc ('\n', asm_out_file);
4855           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4856         }
4857       else
4858         {
4859           switch_to_section (text_section);
4860           ASM_OUTPUT_LABEL (asm_out_file, name);
4861         }
4862
4863       xops[0] = gen_rtx_REG (SImode, regno);
4864       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4865       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4866       output_asm_insn ("ret", xops);
4867     }
4868
4869   if (NEED_INDICATE_EXEC_STACK)
4870     file_end_indicate_exec_stack ();
4871 }
4872
4873 /* Emit code for the SET_GOT patterns.  */
4874
4875 const char *
4876 output_set_got (rtx dest, rtx label ATTRIBUTE_UNUSED)
4877 {
4878   rtx xops[3];
4879
4880   xops[0] = dest;
4881   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4882
4883   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4884     {
4885       xops[2] = gen_rtx_LABEL_REF (Pmode, label ? label : gen_label_rtx ());
4886
4887       if (!flag_pic)
4888         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4889       else
4890         output_asm_insn ("call\t%a2", xops);
4891
4892 #if TARGET_MACHO
4893       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4894          is what will be referenced by the Mach-O PIC subsystem.  */
4895       if (!label)
4896         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4897 #endif
4898
4899       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4900                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4901
4902       if (flag_pic)
4903         output_asm_insn ("pop{l}\t%0", xops);
4904     }
4905   else
4906     {
4907       char name[32];
4908       get_pc_thunk_name (name, REGNO (dest));
4909       pic_labels_used |= 1 << REGNO (dest);
4910
4911       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4912       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4913       output_asm_insn ("call\t%X2", xops);
4914       /* Output the Mach-O "canonical" label name ("Lxx$pb") here too.  This
4915          is what will be referenced by the Mach-O PIC subsystem.  */
4916 #if TARGET_MACHO
4917       if (!label)
4918         ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4919       else
4920         targetm.asm_out.internal_label (asm_out_file, "L",
4921                                            CODE_LABEL_NUMBER (label));
4922 #endif
4923     }
4924
4925   if (TARGET_MACHO)
4926     return "";
4927
4928   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4929     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4930   else
4931     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %1+(.-%a2)}", xops);
4932
4933   return "";
4934 }
4935
4936 /* Generate an "push" pattern for input ARG.  */
4937
4938 static rtx
4939 gen_push (rtx arg)
4940 {
4941   return gen_rtx_SET (VOIDmode,
4942                       gen_rtx_MEM (Pmode,
4943                                    gen_rtx_PRE_DEC (Pmode,
4944                                                     stack_pointer_rtx)),
4945                       arg);
4946 }
4947
4948 /* Return >= 0 if there is an unused call-clobbered register available
4949    for the entire function.  */
4950
4951 static unsigned int
4952 ix86_select_alt_pic_regnum (void)
4953 {
4954   if (current_function_is_leaf && !current_function_profile
4955       && !ix86_current_function_calls_tls_descriptor)
4956     {
4957       int i;
4958       for (i = 2; i >= 0; --i)
4959         if (!regs_ever_live[i])
4960           return i;
4961     }
4962
4963   return INVALID_REGNUM;
4964 }
4965
4966 /* Return 1 if we need to save REGNO.  */
4967 static int
4968 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4969 {
4970   if (pic_offset_table_rtx
4971       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4972       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4973           || current_function_profile
4974           || current_function_calls_eh_return
4975           || current_function_uses_const_pool))
4976     {
4977       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4978         return 0;
4979       return 1;
4980     }
4981
4982   if (current_function_calls_eh_return && maybe_eh_return)
4983     {
4984       unsigned i;
4985       for (i = 0; ; i++)
4986         {
4987           unsigned test = EH_RETURN_DATA_REGNO (i);
4988           if (test == INVALID_REGNUM)
4989             break;
4990           if (test == regno)
4991             return 1;
4992         }
4993     }
4994
4995   if (cfun->machine->force_align_arg_pointer
4996       && regno == REGNO (cfun->machine->force_align_arg_pointer))
4997     return 1;
4998
4999   return (regs_ever_live[regno]
5000           && !call_used_regs[regno]
5001           && !fixed_regs[regno]
5002           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
5003 }
5004
5005 /* Return number of registers to be saved on the stack.  */
5006
5007 static int
5008 ix86_nsaved_regs (void)
5009 {
5010   int nregs = 0;
5011   int regno;
5012
5013   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
5014     if (ix86_save_reg (regno, true))
5015       nregs++;
5016   return nregs;
5017 }
5018
5019 /* Return the offset between two registers, one to be eliminated, and the other
5020    its replacement, at the start of a routine.  */
5021
5022 HOST_WIDE_INT
5023 ix86_initial_elimination_offset (int from, int to)
5024 {
5025   struct ix86_frame frame;
5026   ix86_compute_frame_layout (&frame);
5027
5028   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
5029     return frame.hard_frame_pointer_offset;
5030   else if (from == FRAME_POINTER_REGNUM
5031            && to == HARD_FRAME_POINTER_REGNUM)
5032     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
5033   else
5034     {
5035       gcc_assert (to == STACK_POINTER_REGNUM);
5036
5037       if (from == ARG_POINTER_REGNUM)
5038         return frame.stack_pointer_offset;
5039
5040       gcc_assert (from == FRAME_POINTER_REGNUM);
5041       return frame.stack_pointer_offset - frame.frame_pointer_offset;
5042     }
5043 }
5044
5045 /* Fill structure ix86_frame about frame of currently computed function.  */
5046
5047 static void
5048 ix86_compute_frame_layout (struct ix86_frame *frame)
5049 {
5050   HOST_WIDE_INT total_size;
5051   unsigned int stack_alignment_needed;
5052   HOST_WIDE_INT offset;
5053   unsigned int preferred_alignment;
5054   HOST_WIDE_INT size = get_frame_size ();
5055
5056   frame->nregs = ix86_nsaved_regs ();
5057   total_size = size;
5058
5059   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
5060   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
5061
5062   /* During reload iteration the amount of registers saved can change.
5063      Recompute the value as needed.  Do not recompute when amount of registers
5064      didn't change as reload does multiple calls to the function and does not
5065      expect the decision to change within single iteration.  */
5066   if (!optimize_size
5067       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
5068     {
5069       int count = frame->nregs;
5070
5071       cfun->machine->use_fast_prologue_epilogue_nregs = count;
5072       /* The fast prologue uses move instead of push to save registers.  This
5073          is significantly longer, but also executes faster as modern hardware
5074          can execute the moves in parallel, but can't do that for push/pop.
5075
5076          Be careful about choosing what prologue to emit:  When function takes
5077          many instructions to execute we may use slow version as well as in
5078          case function is known to be outside hot spot (this is known with
5079          feedback only).  Weight the size of function by number of registers
5080          to save as it is cheap to use one or two push instructions but very
5081          slow to use many of them.  */
5082       if (count)
5083         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
5084       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
5085           || (flag_branch_probabilities
5086               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
5087         cfun->machine->use_fast_prologue_epilogue = false;
5088       else
5089         cfun->machine->use_fast_prologue_epilogue
5090            = !expensive_function_p (count);
5091     }
5092   if (TARGET_PROLOGUE_USING_MOVE
5093       && cfun->machine->use_fast_prologue_epilogue)
5094     frame->save_regs_using_mov = true;
5095   else
5096     frame->save_regs_using_mov = false;
5097
5098
5099   /* Skip return address and saved base pointer.  */
5100   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
5101
5102   frame->hard_frame_pointer_offset = offset;
5103
5104   /* Do some sanity checking of stack_alignment_needed and
5105      preferred_alignment, since i386 port is the only using those features
5106      that may break easily.  */
5107
5108   gcc_assert (!size || stack_alignment_needed);
5109   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
5110   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5111   gcc_assert (stack_alignment_needed
5112               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
5113
5114   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
5115     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
5116
5117   /* Register save area */
5118   offset += frame->nregs * UNITS_PER_WORD;
5119
5120   /* Va-arg area */
5121   if (ix86_save_varrargs_registers)
5122     {
5123       offset += X86_64_VARARGS_SIZE;
5124       frame->va_arg_size = X86_64_VARARGS_SIZE;
5125     }
5126   else
5127     frame->va_arg_size = 0;
5128
5129   /* Align start of frame for local function.  */
5130   frame->padding1 = ((offset + stack_alignment_needed - 1)
5131                      & -stack_alignment_needed) - offset;
5132
5133   offset += frame->padding1;
5134
5135   /* Frame pointer points here.  */
5136   frame->frame_pointer_offset = offset;
5137
5138   offset += size;
5139
5140   /* Add outgoing arguments area.  Can be skipped if we eliminated
5141      all the function calls as dead code.
5142      Skipping is however impossible when function calls alloca.  Alloca
5143      expander assumes that last current_function_outgoing_args_size
5144      of stack frame are unused.  */
5145   if (ACCUMULATE_OUTGOING_ARGS
5146       && (!current_function_is_leaf || current_function_calls_alloca
5147           || ix86_current_function_calls_tls_descriptor))
5148     {
5149       offset += current_function_outgoing_args_size;
5150       frame->outgoing_arguments_size = current_function_outgoing_args_size;
5151     }
5152   else
5153     frame->outgoing_arguments_size = 0;
5154
5155   /* Align stack boundary.  Only needed if we're calling another function
5156      or using alloca.  */
5157   if (!current_function_is_leaf || current_function_calls_alloca
5158       || ix86_current_function_calls_tls_descriptor)
5159     frame->padding2 = ((offset + preferred_alignment - 1)
5160                        & -preferred_alignment) - offset;
5161   else
5162     frame->padding2 = 0;
5163
5164   offset += frame->padding2;
5165
5166   /* We've reached end of stack frame.  */
5167   frame->stack_pointer_offset = offset;
5168
5169   /* Size prologue needs to allocate.  */
5170   frame->to_allocate =
5171     (size + frame->padding1 + frame->padding2
5172      + frame->outgoing_arguments_size + frame->va_arg_size);
5173
5174   if ((!frame->to_allocate && frame->nregs <= 1)
5175       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
5176     frame->save_regs_using_mov = false;
5177
5178   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
5179       && current_function_is_leaf
5180       && !ix86_current_function_calls_tls_descriptor)
5181     {
5182       frame->red_zone_size = frame->to_allocate;
5183       if (frame->save_regs_using_mov)
5184         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
5185       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
5186         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
5187     }
5188   else
5189     frame->red_zone_size = 0;
5190   frame->to_allocate -= frame->red_zone_size;
5191   frame->stack_pointer_offset -= frame->red_zone_size;
5192 #if 0
5193   fprintf (stderr, "nregs: %i\n", frame->nregs);
5194   fprintf (stderr, "size: %i\n", size);
5195   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
5196   fprintf (stderr, "padding1: %i\n", frame->padding1);
5197   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
5198   fprintf (stderr, "padding2: %i\n", frame->padding2);
5199   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
5200   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
5201   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
5202   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
5203            frame->hard_frame_pointer_offset);
5204   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
5205 #endif
5206 }
5207
5208 /* Emit code to save registers in the prologue.  */
5209
5210 static void
5211 ix86_emit_save_regs (void)
5212 {
5213   unsigned int regno;
5214   rtx insn;
5215
5216   for (regno = FIRST_PSEUDO_REGISTER; regno-- > 0; )
5217     if (ix86_save_reg (regno, true))
5218       {
5219         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
5220         RTX_FRAME_RELATED_P (insn) = 1;
5221       }
5222 }
5223
5224 /* Emit code to save registers using MOV insns.  First register
5225    is restored from POINTER + OFFSET.  */
5226 static void
5227 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
5228 {
5229   unsigned int regno;
5230   rtx insn;
5231
5232   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5233     if (ix86_save_reg (regno, true))
5234       {
5235         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
5236                                                Pmode, offset),
5237                                gen_rtx_REG (Pmode, regno));
5238         RTX_FRAME_RELATED_P (insn) = 1;
5239         offset += UNITS_PER_WORD;
5240       }
5241 }
5242
5243 /* Expand prologue or epilogue stack adjustment.
5244    The pattern exist to put a dependency on all ebp-based memory accesses.
5245    STYLE should be negative if instructions should be marked as frame related,
5246    zero if %r11 register is live and cannot be freely used and positive
5247    otherwise.  */
5248
5249 static void
5250 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
5251 {
5252   rtx insn;
5253
5254   if (! TARGET_64BIT)
5255     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
5256   else if (x86_64_immediate_operand (offset, DImode))
5257     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
5258   else
5259     {
5260       rtx r11;
5261       /* r11 is used by indirect sibcall return as well, set before the
5262          epilogue and used after the epilogue.  ATM indirect sibcall
5263          shouldn't be used together with huge frame sizes in one
5264          function because of the frame_size check in sibcall.c.  */
5265       gcc_assert (style);
5266       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5267       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
5268       if (style < 0)
5269         RTX_FRAME_RELATED_P (insn) = 1;
5270       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
5271                                                                offset));
5272     }
5273   if (style < 0)
5274     RTX_FRAME_RELATED_P (insn) = 1;
5275 }
5276
5277 /* Handle the TARGET_INTERNAL_ARG_POINTER hook.  */
5278
5279 static rtx
5280 ix86_internal_arg_pointer (void)
5281 {
5282   bool has_force_align_arg_pointer =
5283     (0 != lookup_attribute (ix86_force_align_arg_pointer_string,
5284                             TYPE_ATTRIBUTES (TREE_TYPE (current_function_decl))));
5285   if ((FORCE_PREFERRED_STACK_BOUNDARY_IN_MAIN
5286        && DECL_NAME (current_function_decl)
5287        && MAIN_NAME_P (DECL_NAME (current_function_decl))
5288        && DECL_FILE_SCOPE_P (current_function_decl))
5289       || ix86_force_align_arg_pointer
5290       || has_force_align_arg_pointer)
5291     {
5292       /* Nested functions can't realign the stack due to a register
5293          conflict.  */
5294       if (DECL_CONTEXT (current_function_decl)
5295           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
5296         {
5297           if (ix86_force_align_arg_pointer)
5298             warning (0, "-mstackrealign ignored for nested functions");
5299           if (has_force_align_arg_pointer)
5300             error ("%s not supported for nested functions",
5301                    ix86_force_align_arg_pointer_string);
5302           return virtual_incoming_args_rtx;
5303         }
5304       cfun->machine->force_align_arg_pointer = gen_rtx_REG (Pmode, 2);
5305       return copy_to_reg (cfun->machine->force_align_arg_pointer);
5306     }
5307   else
5308     return virtual_incoming_args_rtx;
5309 }
5310
5311 /* Handle the TARGET_DWARF_HANDLE_FRAME_UNSPEC hook.
5312    This is called from dwarf2out.c to emit call frame instructions
5313    for frame-related insns containing UNSPECs and UNSPEC_VOLATILEs. */
5314 static void
5315 ix86_dwarf_handle_frame_unspec (const char *label, rtx pattern, int index)
5316 {
5317   rtx unspec = SET_SRC (pattern);
5318   gcc_assert (GET_CODE (unspec) == UNSPEC);
5319
5320   switch (index)
5321     {
5322     case UNSPEC_REG_SAVE:
5323       dwarf2out_reg_save_reg (label, XVECEXP (unspec, 0, 0),
5324                               SET_DEST (pattern));
5325       break;
5326     case UNSPEC_DEF_CFA:
5327       dwarf2out_def_cfa (label, REGNO (SET_DEST (pattern)),
5328                          INTVAL (XVECEXP (unspec, 0, 0)));
5329       break;
5330     default:
5331       gcc_unreachable ();
5332     }
5333 }
5334
5335 /* Expand the prologue into a bunch of separate insns.  */
5336
5337 void
5338 ix86_expand_prologue (void)
5339 {
5340   rtx insn;
5341   bool pic_reg_used;
5342   struct ix86_frame frame;
5343   HOST_WIDE_INT allocate;
5344
5345   ix86_compute_frame_layout (&frame);
5346
5347   if (cfun->machine->force_align_arg_pointer)
5348     {
5349       rtx x, y;
5350
5351       /* Grab the argument pointer.  */
5352       x = plus_constant (stack_pointer_rtx, 4);
5353       y = cfun->machine->force_align_arg_pointer;
5354       insn = emit_insn (gen_rtx_SET (VOIDmode, y, x));
5355       RTX_FRAME_RELATED_P (insn) = 1;
5356
5357       /* The unwind info consists of two parts: install the fafp as the cfa,
5358          and record the fafp as the "save register" of the stack pointer.
5359          The later is there in order that the unwinder can see where it
5360          should restore the stack pointer across the and insn.  */
5361       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, const0_rtx), UNSPEC_DEF_CFA);
5362       x = gen_rtx_SET (VOIDmode, y, x);
5363       RTX_FRAME_RELATED_P (x) = 1;
5364       y = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, stack_pointer_rtx),
5365                           UNSPEC_REG_SAVE);
5366       y = gen_rtx_SET (VOIDmode, cfun->machine->force_align_arg_pointer, y);
5367       RTX_FRAME_RELATED_P (y) = 1;
5368       x = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, x, y));
5369       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5370       REG_NOTES (insn) = x;
5371
5372       /* Align the stack.  */
5373       emit_insn (gen_andsi3 (stack_pointer_rtx, stack_pointer_rtx,
5374                              GEN_INT (-16)));
5375
5376       /* And here we cheat like madmen with the unwind info.  We force the
5377          cfa register back to sp+4, which is exactly what it was at the
5378          start of the function.  Re-pushing the return address results in
5379          the return at the same spot relative to the cfa, and thus is
5380          correct wrt the unwind info.  */
5381       x = cfun->machine->force_align_arg_pointer;
5382       x = gen_frame_mem (Pmode, plus_constant (x, -4));
5383       insn = emit_insn (gen_push (x));
5384       RTX_FRAME_RELATED_P (insn) = 1;
5385
5386       x = GEN_INT (4);
5387       x = gen_rtx_UNSPEC (VOIDmode, gen_rtvec (1, x), UNSPEC_DEF_CFA);
5388       x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
5389       x = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, x, NULL);
5390       REG_NOTES (insn) = x;
5391     }
5392
5393   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
5394      slower on all targets.  Also sdb doesn't like it.  */
5395
5396   if (frame_pointer_needed)
5397     {
5398       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
5399       RTX_FRAME_RELATED_P (insn) = 1;
5400
5401       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
5402       RTX_FRAME_RELATED_P (insn) = 1;
5403     }
5404
5405   allocate = frame.to_allocate;
5406
5407   if (!frame.save_regs_using_mov)
5408     ix86_emit_save_regs ();
5409   else
5410     allocate += frame.nregs * UNITS_PER_WORD;
5411
5412   /* When using red zone we may start register saving before allocating
5413      the stack frame saving one cycle of the prologue.  */
5414   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
5415     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
5416                                    : stack_pointer_rtx,
5417                                    -frame.nregs * UNITS_PER_WORD);
5418
5419   if (allocate == 0)
5420     ;
5421   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
5422     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5423                                GEN_INT (-allocate), -1);
5424   else
5425     {
5426       /* Only valid for Win32.  */
5427       rtx eax = gen_rtx_REG (SImode, 0);
5428       bool eax_live = ix86_eax_live_at_start_p ();
5429       rtx t;
5430
5431       gcc_assert (!TARGET_64BIT);
5432
5433       if (eax_live)
5434         {
5435           emit_insn (gen_push (eax));
5436           allocate -= 4;
5437         }
5438
5439       emit_move_insn (eax, GEN_INT (allocate));
5440
5441       insn = emit_insn (gen_allocate_stack_worker (eax));
5442       RTX_FRAME_RELATED_P (insn) = 1;
5443       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
5444       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
5445       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
5446                                             t, REG_NOTES (insn));
5447
5448       if (eax_live)
5449         {
5450           if (frame_pointer_needed)
5451             t = plus_constant (hard_frame_pointer_rtx,
5452                                allocate
5453                                - frame.to_allocate
5454                                - frame.nregs * UNITS_PER_WORD);
5455           else
5456             t = plus_constant (stack_pointer_rtx, allocate);
5457           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
5458         }
5459     }
5460
5461   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
5462     {
5463       if (!frame_pointer_needed || !frame.to_allocate)
5464         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
5465       else
5466         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
5467                                        -frame.nregs * UNITS_PER_WORD);
5468     }
5469
5470   pic_reg_used = false;
5471   if (pic_offset_table_rtx
5472       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
5473           || current_function_profile))
5474     {
5475       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
5476
5477       if (alt_pic_reg_used != INVALID_REGNUM)
5478         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
5479
5480       pic_reg_used = true;
5481     }
5482
5483   if (pic_reg_used)
5484     {
5485       if (TARGET_64BIT)
5486         insn = emit_insn (gen_set_got_rex64 (pic_offset_table_rtx));
5487       else
5488         insn = emit_insn (gen_set_got (pic_offset_table_rtx));
5489
5490       /* Even with accurate pre-reload life analysis, we can wind up
5491          deleting all references to the pic register after reload.
5492          Consider if cross-jumping unifies two sides of a branch
5493          controlled by a comparison vs the only read from a global.
5494          In which case, allow the set_got to be deleted, though we're
5495          too late to do anything about the ebx save in the prologue.  */
5496       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
5497     }
5498
5499   /* Prevent function calls from be scheduled before the call to mcount.
5500      In the pic_reg_used case, make sure that the got load isn't deleted.  */
5501   if (current_function_profile)
5502     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
5503 }
5504
5505 /* Emit code to restore saved registers using MOV insns.  First register
5506    is restored from POINTER + OFFSET.  */
5507 static void
5508 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
5509                                   int maybe_eh_return)
5510 {
5511   int regno;
5512   rtx base_address = gen_rtx_MEM (Pmode, pointer);
5513
5514   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5515     if (ix86_save_reg (regno, maybe_eh_return))
5516       {
5517         /* Ensure that adjust_address won't be forced to produce pointer
5518            out of range allowed by x86-64 instruction set.  */
5519         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
5520           {
5521             rtx r11;
5522
5523             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
5524             emit_move_insn (r11, GEN_INT (offset));
5525             emit_insn (gen_adddi3 (r11, r11, pointer));
5526             base_address = gen_rtx_MEM (Pmode, r11);
5527             offset = 0;
5528           }
5529         emit_move_insn (gen_rtx_REG (Pmode, regno),
5530                         adjust_address (base_address, Pmode, offset));
5531         offset += UNITS_PER_WORD;
5532       }
5533 }
5534
5535 /* Restore function stack, frame, and registers.  */
5536
5537 void
5538 ix86_expand_epilogue (int style)
5539 {
5540   int regno;
5541   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
5542   struct ix86_frame frame;
5543   HOST_WIDE_INT offset;
5544
5545   ix86_compute_frame_layout (&frame);
5546
5547   /* Calculate start of saved registers relative to ebp.  Special care
5548      must be taken for the normal return case of a function using
5549      eh_return: the eax and edx registers are marked as saved, but not
5550      restored along this path.  */
5551   offset = frame.nregs;
5552   if (current_function_calls_eh_return && style != 2)
5553     offset -= 2;
5554   offset *= -UNITS_PER_WORD;
5555
5556   /* If we're only restoring one register and sp is not valid then
5557      using a move instruction to restore the register since it's
5558      less work than reloading sp and popping the register.
5559
5560      The default code result in stack adjustment using add/lea instruction,
5561      while this code results in LEAVE instruction (or discrete equivalent),
5562      so it is profitable in some other cases as well.  Especially when there
5563      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
5564      and there is exactly one register to pop. This heuristic may need some
5565      tuning in future.  */
5566   if ((!sp_valid && frame.nregs <= 1)
5567       || (TARGET_EPILOGUE_USING_MOVE
5568           && cfun->machine->use_fast_prologue_epilogue
5569           && (frame.nregs > 1 || frame.to_allocate))
5570       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
5571       || (frame_pointer_needed && TARGET_USE_LEAVE
5572           && cfun->machine->use_fast_prologue_epilogue
5573           && frame.nregs == 1)
5574       || current_function_calls_eh_return)
5575     {
5576       /* Restore registers.  We can use ebp or esp to address the memory
5577          locations.  If both are available, default to ebp, since offsets
5578          are known to be small.  Only exception is esp pointing directly to the
5579          end of block of saved registers, where we may simplify addressing
5580          mode.  */
5581
5582       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
5583         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
5584                                           frame.to_allocate, style == 2);
5585       else
5586         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
5587                                           offset, style == 2);
5588
5589       /* eh_return epilogues need %ecx added to the stack pointer.  */
5590       if (style == 2)
5591         {
5592           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
5593
5594           if (frame_pointer_needed)
5595             {
5596               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
5597               tmp = plus_constant (tmp, UNITS_PER_WORD);
5598               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
5599
5600               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
5601               emit_move_insn (hard_frame_pointer_rtx, tmp);
5602
5603               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
5604                                          const0_rtx, style);
5605             }
5606           else
5607             {
5608               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
5609               tmp = plus_constant (tmp, (frame.to_allocate
5610                                          + frame.nregs * UNITS_PER_WORD));
5611               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
5612             }
5613         }
5614       else if (!frame_pointer_needed)
5615         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5616                                    GEN_INT (frame.to_allocate
5617                                             + frame.nregs * UNITS_PER_WORD),
5618                                    style);
5619       /* If not an i386, mov & pop is faster than "leave".  */
5620       else if (TARGET_USE_LEAVE || optimize_size
5621                || !cfun->machine->use_fast_prologue_epilogue)
5622         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5623       else
5624         {
5625           pro_epilogue_adjust_stack (stack_pointer_rtx,
5626                                      hard_frame_pointer_rtx,
5627                                      const0_rtx, style);
5628           if (TARGET_64BIT)
5629             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5630           else
5631             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5632         }
5633     }
5634   else
5635     {
5636       /* First step is to deallocate the stack frame so that we can
5637          pop the registers.  */
5638       if (!sp_valid)
5639         {
5640           gcc_assert (frame_pointer_needed);
5641           pro_epilogue_adjust_stack (stack_pointer_rtx,
5642                                      hard_frame_pointer_rtx,
5643                                      GEN_INT (offset), style);
5644         }
5645       else if (frame.to_allocate)
5646         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
5647                                    GEN_INT (frame.to_allocate), style);
5648
5649       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5650         if (ix86_save_reg (regno, false))
5651           {
5652             if (TARGET_64BIT)
5653               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
5654             else
5655               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
5656           }
5657       if (frame_pointer_needed)
5658         {
5659           /* Leave results in shorter dependency chains on CPUs that are
5660              able to grok it fast.  */
5661           if (TARGET_USE_LEAVE)
5662             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
5663           else if (TARGET_64BIT)
5664             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
5665           else
5666             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
5667         }
5668     }
5669
5670   if (cfun->machine->force_align_arg_pointer)
5671     {
5672       emit_insn (gen_addsi3 (stack_pointer_rtx,
5673                              cfun->machine->force_align_arg_pointer,
5674                              GEN_INT (-4)));
5675     }
5676
5677   /* Sibcall epilogues don't want a return instruction.  */
5678   if (style == 0)
5679     return;
5680
5681   if (current_function_pops_args && current_function_args_size)
5682     {
5683       rtx popc = GEN_INT (current_function_pops_args);
5684
5685       /* i386 can only pop 64K bytes.  If asked to pop more, pop
5686          return address, do explicit add, and jump indirectly to the
5687          caller.  */
5688
5689       if (current_function_pops_args >= 65536)
5690         {
5691           rtx ecx = gen_rtx_REG (SImode, 2);
5692
5693           /* There is no "pascal" calling convention in 64bit ABI.  */
5694           gcc_assert (!TARGET_64BIT);
5695
5696           emit_insn (gen_popsi1 (ecx));
5697           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
5698           emit_jump_insn (gen_return_indirect_internal (ecx));
5699         }
5700       else
5701         emit_jump_insn (gen_return_pop_internal (popc));
5702     }
5703   else
5704     emit_jump_insn (gen_return_internal ());
5705 }
5706
5707 /* Reset from the function's potential modifications.  */
5708
5709 static void
5710 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
5711                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
5712 {
5713   if (pic_offset_table_rtx)
5714     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
5715 #if TARGET_MACHO
5716   /* Mach-O doesn't support labels at the end of objects, so if
5717      it looks like we might want one, insert a NOP.  */
5718   {
5719     rtx insn = get_last_insn ();
5720     while (insn
5721            && NOTE_P (insn)
5722            && NOTE_LINE_NUMBER (insn) != NOTE_INSN_DELETED_LABEL)
5723       insn = PREV_INSN (insn);
5724     if (insn
5725         && (LABEL_P (insn)
5726             || (NOTE_P (insn)
5727                 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_DELETED_LABEL)))
5728       fputs ("\tnop\n", file);
5729   }
5730 #endif
5731
5732 }
5733 \f
5734 /* Extract the parts of an RTL expression that is a valid memory address
5735    for an instruction.  Return 0 if the structure of the address is
5736    grossly off.  Return -1 if the address contains ASHIFT, so it is not
5737    strictly valid, but still used for computing length of lea instruction.  */
5738
5739 int
5740 ix86_decompose_address (rtx addr, struct ix86_address *out)
5741 {
5742   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
5743   rtx base_reg, index_reg;
5744   HOST_WIDE_INT scale = 1;
5745   rtx scale_rtx = NULL_RTX;
5746   int retval = 1;
5747   enum ix86_address_seg seg = SEG_DEFAULT;
5748
5749   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
5750     base = addr;
5751   else if (GET_CODE (addr) == PLUS)
5752     {
5753       rtx addends[4], op;
5754       int n = 0, i;
5755
5756       op = addr;
5757       do
5758         {
5759           if (n >= 4)
5760             return 0;
5761           addends[n++] = XEXP (op, 1);
5762           op = XEXP (op, 0);
5763         }
5764       while (GET_CODE (op) == PLUS);
5765       if (n >= 4)
5766         return 0;
5767       addends[n] = op;
5768
5769       for (i = n; i >= 0; --i)
5770         {
5771           op = addends[i];
5772           switch (GET_CODE (op))
5773             {
5774             case MULT:
5775               if (index)
5776                 return 0;
5777               index = XEXP (op, 0);
5778               scale_rtx = XEXP (op, 1);
5779               break;
5780
5781             case UNSPEC:
5782               if (XINT (op, 1) == UNSPEC_TP
5783                   && TARGET_TLS_DIRECT_SEG_REFS
5784                   && seg == SEG_DEFAULT)
5785                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
5786               else
5787                 return 0;
5788               break;
5789
5790             case REG:
5791             case SUBREG:
5792               if (!base)
5793                 base = op;
5794               else if (!index)
5795                 index = op;
5796               else
5797                 return 0;
5798               break;
5799
5800             case CONST:
5801             case CONST_INT:
5802             case SYMBOL_REF:
5803             case LABEL_REF:
5804               if (disp)
5805                 return 0;
5806               disp = op;
5807               break;
5808
5809             default:
5810               return 0;
5811             }
5812         }
5813     }
5814   else if (GET_CODE (addr) == MULT)
5815     {
5816       index = XEXP (addr, 0);           /* index*scale */
5817       scale_rtx = XEXP (addr, 1);
5818     }
5819   else if (GET_CODE (addr) == ASHIFT)
5820     {
5821       rtx tmp;
5822
5823       /* We're called for lea too, which implements ashift on occasion.  */
5824       index = XEXP (addr, 0);
5825       tmp = XEXP (addr, 1);
5826       if (GET_CODE (tmp) != CONST_INT)
5827         return 0;
5828       scale = INTVAL (tmp);
5829       if ((unsigned HOST_WIDE_INT) scale > 3)
5830         return 0;
5831       scale = 1 << scale;
5832       retval = -1;
5833     }
5834   else
5835     disp = addr;                        /* displacement */
5836
5837   /* Extract the integral value of scale.  */
5838   if (scale_rtx)
5839     {
5840       if (GET_CODE (scale_rtx) != CONST_INT)
5841         return 0;
5842       scale = INTVAL (scale_rtx);
5843     }
5844
5845   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
5846   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
5847
5848   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
5849   if (base_reg && index_reg && scale == 1
5850       && (index_reg == arg_pointer_rtx
5851           || index_reg == frame_pointer_rtx
5852           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
5853     {
5854       rtx tmp;
5855       tmp = base, base = index, index = tmp;
5856       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
5857     }
5858
5859   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
5860   if ((base_reg == hard_frame_pointer_rtx
5861        || base_reg == frame_pointer_rtx
5862        || base_reg == arg_pointer_rtx) && !disp)
5863     disp = const0_rtx;
5864
5865   /* Special case: on K6, [%esi] makes the instruction vector decoded.
5866      Avoid this by transforming to [%esi+0].  */
5867   if (ix86_tune == PROCESSOR_K6 && !optimize_size
5868       && base_reg && !index_reg && !disp
5869       && REG_P (base_reg)
5870       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
5871     disp = const0_rtx;
5872
5873   /* Special case: encode reg+reg instead of reg*2.  */
5874   if (!base && index && scale && scale == 2)
5875     base = index, base_reg = index_reg, scale = 1;
5876
5877   /* Special case: scaling cannot be encoded without base or displacement.  */
5878   if (!base && !disp && index && scale != 1)
5879     disp = const0_rtx;
5880
5881   out->base = base;
5882   out->index = index;
5883   out->disp = disp;
5884   out->scale = scale;
5885   out->seg = seg;
5886
5887   return retval;
5888 }
5889 \f
5890 /* Return cost of the memory address x.
5891    For i386, it is better to use a complex address than let gcc copy
5892    the address into a reg and make a new pseudo.  But not if the address
5893    requires to two regs - that would mean more pseudos with longer
5894    lifetimes.  */
5895 static int
5896 ix86_address_cost (rtx x)
5897 {
5898   struct ix86_address parts;
5899   int cost = 1;
5900   int ok = ix86_decompose_address (x, &parts);
5901
5902   gcc_assert (ok);
5903
5904   if (parts.base && GET_CODE (parts.base) == SUBREG)
5905     parts.base = SUBREG_REG (parts.base);
5906   if (parts.index && GET_CODE (parts.index) == SUBREG)
5907     parts.index = SUBREG_REG (parts.index);
5908
5909   /* More complex memory references are better.  */
5910   if (parts.disp && parts.disp != const0_rtx)
5911     cost--;
5912   if (parts.seg != SEG_DEFAULT)
5913     cost--;
5914
5915   /* Attempt to minimize number of registers in the address.  */
5916   if ((parts.base
5917        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
5918       || (parts.index
5919           && (!REG_P (parts.index)
5920               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5921     cost++;
5922
5923   if (parts.base
5924       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5925       && parts.index
5926       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5927       && parts.base != parts.index)
5928     cost++;
5929
5930   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5931      since it's predecode logic can't detect the length of instructions
5932      and it degenerates to vector decoded.  Increase cost of such
5933      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5934      to split such addresses or even refuse such addresses at all.
5935
5936      Following addressing modes are affected:
5937       [base+scale*index]
5938       [scale*index+disp]
5939       [base+index]
5940
5941      The first and last case  may be avoidable by explicitly coding the zero in
5942      memory address, but I don't have AMD-K6 machine handy to check this
5943      theory.  */
5944
5945   if (TARGET_K6
5946       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5947           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5948           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5949     cost += 10;
5950
5951   return cost;
5952 }
5953 \f
5954 /* If X is a machine specific address (i.e. a symbol or label being
5955    referenced as a displacement from the GOT implemented using an
5956    UNSPEC), then return the base term.  Otherwise return X.  */
5957
5958 rtx
5959 ix86_find_base_term (rtx x)
5960 {
5961   rtx term;
5962
5963   if (TARGET_64BIT)
5964     {
5965       if (GET_CODE (x) != CONST)
5966         return x;
5967       term = XEXP (x, 0);
5968       if (GET_CODE (term) == PLUS
5969           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5970               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5971         term = XEXP (term, 0);
5972       if (GET_CODE (term) != UNSPEC
5973           || XINT (term, 1) != UNSPEC_GOTPCREL)
5974         return x;
5975
5976       term = XVECEXP (term, 0, 0);
5977
5978       if (GET_CODE (term) != SYMBOL_REF
5979           && GET_CODE (term) != LABEL_REF)
5980         return x;
5981
5982       return term;
5983     }
5984
5985   term = ix86_delegitimize_address (x);
5986
5987   if (GET_CODE (term) != SYMBOL_REF
5988       && GET_CODE (term) != LABEL_REF)
5989     return x;
5990
5991   return term;
5992 }
5993
5994 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
5995    this is used for to form addresses to local data when -fPIC is in
5996    use.  */
5997
5998 static bool
5999 darwin_local_data_pic (rtx disp)
6000 {
6001   if (GET_CODE (disp) == MINUS)
6002     {
6003       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
6004           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
6005         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
6006           {
6007             const char *sym_name = XSTR (XEXP (disp, 1), 0);
6008             if (! strcmp (sym_name, "<pic base>"))
6009               return true;
6010           }
6011     }
6012
6013   return false;
6014 }
6015 \f
6016 /* Determine if a given RTX is a valid constant.  We already know this
6017    satisfies CONSTANT_P.  */
6018
6019 bool
6020 legitimate_constant_p (rtx x)
6021 {
6022   switch (GET_CODE (x))
6023     {
6024     case CONST:
6025       x = XEXP (x, 0);
6026
6027       if (GET_CODE (x) == PLUS)
6028         {
6029           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6030             return false;
6031           x = XEXP (x, 0);
6032         }
6033
6034       if (TARGET_MACHO && darwin_local_data_pic (x))
6035         return true;
6036
6037       /* Only some unspecs are valid as "constants".  */
6038       if (GET_CODE (x) == UNSPEC)
6039         switch (XINT (x, 1))
6040           {
6041           case UNSPEC_GOTOFF:
6042             return TARGET_64BIT;
6043           case UNSPEC_TPOFF:
6044           case UNSPEC_NTPOFF:
6045             x = XVECEXP (x, 0, 0);
6046             return (GET_CODE (x) == SYMBOL_REF
6047                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6048           case UNSPEC_DTPOFF:
6049             x = XVECEXP (x, 0, 0);
6050             return (GET_CODE (x) == SYMBOL_REF
6051                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC);
6052           default:
6053             return false;
6054           }
6055
6056       /* We must have drilled down to a symbol.  */
6057       if (GET_CODE (x) == LABEL_REF)
6058         return true;
6059       if (GET_CODE (x) != SYMBOL_REF)
6060         return false;
6061       /* FALLTHRU */
6062
6063     case SYMBOL_REF:
6064       /* TLS symbols are never valid.  */
6065       if (SYMBOL_REF_TLS_MODEL (x))
6066         return false;
6067       break;
6068
6069     case CONST_DOUBLE:
6070       if (GET_MODE (x) == TImode
6071           && x != CONST0_RTX (TImode)
6072           && !TARGET_64BIT)
6073         return false;
6074       break;
6075
6076     case CONST_VECTOR:
6077       if (x == CONST0_RTX (GET_MODE (x)))
6078         return true;
6079       return false;
6080
6081     default:
6082       break;
6083     }
6084
6085   /* Otherwise we handle everything else in the move patterns.  */
6086   return true;
6087 }
6088
6089 /* Determine if it's legal to put X into the constant pool.  This
6090    is not possible for the address of thread-local symbols, which
6091    is checked above.  */
6092
6093 static bool
6094 ix86_cannot_force_const_mem (rtx x)
6095 {
6096   /* We can always put integral constants and vectors in memory.  */
6097   switch (GET_CODE (x))
6098     {
6099     case CONST_INT:
6100     case CONST_DOUBLE:
6101     case CONST_VECTOR:
6102       return false;
6103
6104     default:
6105       break;
6106     }
6107   return !legitimate_constant_p (x);
6108 }
6109
6110 /* Determine if a given RTX is a valid constant address.  */
6111
6112 bool
6113 constant_address_p (rtx x)
6114 {
6115   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
6116 }
6117
6118 /* Nonzero if the constant value X is a legitimate general operand
6119    when generating PIC code.  It is given that flag_pic is on and
6120    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
6121
6122 bool
6123 legitimate_pic_operand_p (rtx x)
6124 {
6125   rtx inner;
6126
6127   switch (GET_CODE (x))
6128     {
6129     case CONST:
6130       inner = XEXP (x, 0);
6131       if (GET_CODE (inner) == PLUS
6132           && GET_CODE (XEXP (inner, 1)) == CONST_INT)
6133         inner = XEXP (inner, 0);
6134
6135       /* Only some unspecs are valid as "constants".  */
6136       if (GET_CODE (inner) == UNSPEC)
6137         switch (XINT (inner, 1))
6138           {
6139           case UNSPEC_GOTOFF:
6140             return TARGET_64BIT;
6141           case UNSPEC_TPOFF:
6142             x = XVECEXP (inner, 0, 0);
6143             return (GET_CODE (x) == SYMBOL_REF
6144                     && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_EXEC);
6145           default:
6146             return false;
6147           }
6148       /* FALLTHRU */
6149
6150     case SYMBOL_REF:
6151     case LABEL_REF:
6152       return legitimate_pic_address_disp_p (x);
6153
6154     default:
6155       return true;
6156     }
6157 }
6158
6159 /* Determine if a given CONST RTX is a valid memory displacement
6160    in PIC mode.  */
6161
6162 int
6163 legitimate_pic_address_disp_p (rtx disp)
6164 {
6165   bool saw_plus;
6166
6167   /* In 64bit mode we can allow direct addresses of symbols and labels
6168      when they are not dynamic symbols.  */
6169   if (TARGET_64BIT)
6170     {
6171       rtx op0 = disp, op1;
6172
6173       switch (GET_CODE (disp))
6174         {
6175         case LABEL_REF:
6176           return true;
6177
6178         case CONST:
6179           if (GET_CODE (XEXP (disp, 0)) != PLUS)
6180             break;
6181           op0 = XEXP (XEXP (disp, 0), 0);
6182           op1 = XEXP (XEXP (disp, 0), 1);
6183           if (GET_CODE (op1) != CONST_INT
6184               || INTVAL (op1) >= 16*1024*1024
6185               || INTVAL (op1) < -16*1024*1024)
6186             break;
6187           if (GET_CODE (op0) == LABEL_REF)
6188             return true;
6189           if (GET_CODE (op0) != SYMBOL_REF)
6190             break;
6191           /* FALLTHRU */
6192
6193         case SYMBOL_REF:
6194           /* TLS references should always be enclosed in UNSPEC.  */
6195           if (SYMBOL_REF_TLS_MODEL (op0))
6196             return false;
6197           if (!SYMBOL_REF_FAR_ADDR_P (op0) && SYMBOL_REF_LOCAL_P (op0))
6198             return true;
6199           break;
6200
6201         default:
6202           break;
6203         }
6204     }
6205   if (GET_CODE (disp) != CONST)
6206     return 0;
6207   disp = XEXP (disp, 0);
6208
6209   if (TARGET_64BIT)
6210     {
6211       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
6212          of GOT tables.  We should not need these anyway.  */
6213       if (GET_CODE (disp) != UNSPEC
6214           || (XINT (disp, 1) != UNSPEC_GOTPCREL
6215               && XINT (disp, 1) != UNSPEC_GOTOFF))
6216         return 0;
6217
6218       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
6219           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
6220         return 0;
6221       return 1;
6222     }
6223
6224   saw_plus = false;
6225   if (GET_CODE (disp) == PLUS)
6226     {
6227       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
6228         return 0;
6229       disp = XEXP (disp, 0);
6230       saw_plus = true;
6231     }
6232
6233   if (TARGET_MACHO && darwin_local_data_pic (disp))
6234     return 1;
6235
6236   if (GET_CODE (disp) != UNSPEC)
6237     return 0;
6238
6239   switch (XINT (disp, 1))
6240     {
6241     case UNSPEC_GOT:
6242       if (saw_plus)
6243         return false;
6244       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
6245     case UNSPEC_GOTOFF:
6246       /* Refuse GOTOFF in 64bit mode since it is always 64bit when used.
6247          While ABI specify also 32bit relocation but we don't produce it in
6248          small PIC model at all.  */
6249       if ((GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
6250            || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
6251           && !TARGET_64BIT)
6252         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
6253       return false;
6254     case UNSPEC_GOTTPOFF:
6255     case UNSPEC_GOTNTPOFF:
6256     case UNSPEC_INDNTPOFF:
6257       if (saw_plus)
6258         return false;
6259       disp = XVECEXP (disp, 0, 0);
6260       return (GET_CODE (disp) == SYMBOL_REF
6261               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_INITIAL_EXEC);
6262     case UNSPEC_NTPOFF:
6263       disp = XVECEXP (disp, 0, 0);
6264       return (GET_CODE (disp) == SYMBOL_REF
6265               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_EXEC);
6266     case UNSPEC_DTPOFF:
6267       disp = XVECEXP (disp, 0, 0);
6268       return (GET_CODE (disp) == SYMBOL_REF
6269               && SYMBOL_REF_TLS_MODEL (disp) == TLS_MODEL_LOCAL_DYNAMIC);
6270     }
6271
6272   return 0;
6273 }
6274
6275 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
6276    memory address for an instruction.  The MODE argument is the machine mode
6277    for the MEM expression that wants to use this address.
6278
6279    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
6280    convert common non-canonical forms to canonical form so that they will
6281    be recognized.  */
6282
6283 int
6284 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
6285 {
6286   struct ix86_address parts;
6287   rtx base, index, disp;
6288   HOST_WIDE_INT scale;
6289   const char *reason = NULL;
6290   rtx reason_rtx = NULL_RTX;
6291
6292   if (TARGET_DEBUG_ADDR)
6293     {
6294       fprintf (stderr,
6295                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
6296                GET_MODE_NAME (mode), strict);
6297       debug_rtx (addr);
6298     }
6299
6300   if (ix86_decompose_address (addr, &parts) <= 0)
6301     {
6302       reason = "decomposition failed";
6303       goto report_error;
6304     }
6305
6306   base = parts.base;
6307   index = parts.index;
6308   disp = parts.disp;
6309   scale = parts.scale;
6310
6311   /* Validate base register.
6312
6313      Don't allow SUBREG's that span more than a word here.  It can lead to spill
6314      failures when the base is one word out of a two word structure, which is
6315      represented internally as a DImode int.  */
6316
6317   if (base)
6318     {
6319       rtx reg;
6320       reason_rtx = base;
6321
6322       if (REG_P (base))
6323         reg = base;
6324       else if (GET_CODE (base) == SUBREG
6325                && REG_P (SUBREG_REG (base))
6326                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
6327                   <= UNITS_PER_WORD)
6328         reg = SUBREG_REG (base);
6329       else
6330         {
6331           reason = "base is not a register";
6332           goto report_error;
6333         }
6334
6335       if (GET_MODE (base) != Pmode)
6336         {
6337           reason = "base is not in Pmode";
6338           goto report_error;
6339         }
6340
6341       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
6342           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
6343         {
6344           reason = "base is not valid";
6345           goto report_error;
6346         }
6347     }
6348
6349   /* Validate index register.
6350
6351      Don't allow SUBREG's that span more than a word here -- same as above.  */
6352
6353   if (index)
6354     {
6355       rtx reg;
6356       reason_rtx = index;
6357
6358       if (REG_P (index))
6359         reg = index;
6360       else if (GET_CODE (index) == SUBREG
6361                && REG_P (SUBREG_REG (index))
6362                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
6363                   <= UNITS_PER_WORD)
6364         reg = SUBREG_REG (index);
6365       else
6366         {
6367           reason = "index is not a register";
6368           goto report_error;
6369         }
6370
6371       if (GET_MODE (index) != Pmode)
6372         {
6373           reason = "index is not in Pmode";
6374           goto report_error;
6375         }
6376
6377       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
6378           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
6379         {
6380           reason = "index is not valid";
6381           goto report_error;
6382         }
6383     }
6384
6385   /* Validate scale factor.  */
6386   if (scale != 1)
6387     {
6388       reason_rtx = GEN_INT (scale);
6389       if (!index)
6390         {
6391           reason = "scale without index";
6392           goto report_error;
6393         }
6394
6395       if (scale != 2 && scale != 4 && scale != 8)
6396         {
6397           reason = "scale is not a valid multiplier";
6398           goto report_error;
6399         }
6400     }
6401
6402   /* Validate displacement.  */
6403   if (disp)
6404     {
6405       reason_rtx = disp;
6406
6407       if (GET_CODE (disp) == CONST
6408           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
6409         switch (XINT (XEXP (disp, 0), 1))
6410           {
6411           /* Refuse GOTOFF and GOT in 64bit mode since it is always 64bit when
6412              used.  While ABI specify also 32bit relocations, we don't produce
6413              them at all and use IP relative instead.  */
6414           case UNSPEC_GOT:
6415           case UNSPEC_GOTOFF:
6416             gcc_assert (flag_pic);
6417             if (!TARGET_64BIT)
6418               goto is_legitimate_pic;
6419             reason = "64bit address unspec";
6420             goto report_error;
6421
6422           case UNSPEC_GOTPCREL:
6423             gcc_assert (flag_pic);
6424             goto is_legitimate_pic;
6425
6426           case UNSPEC_GOTTPOFF:
6427           case UNSPEC_GOTNTPOFF:
6428           case UNSPEC_INDNTPOFF:
6429           case UNSPEC_NTPOFF:
6430           case UNSPEC_DTPOFF:
6431             break;
6432
6433           default:
6434             reason = "invalid address unspec";
6435             goto report_error;
6436           }
6437
6438       else if (SYMBOLIC_CONST (disp)
6439                && (flag_pic
6440                    || (TARGET_MACHO
6441 #if TARGET_MACHO
6442                        && MACHOPIC_INDIRECT
6443                        && !machopic_operand_p (disp)
6444 #endif
6445                )))
6446         {
6447
6448         is_legitimate_pic:
6449           if (TARGET_64BIT && (index || base))
6450             {
6451               /* foo@dtpoff(%rX) is ok.  */
6452               if (GET_CODE (disp) != CONST
6453                   || GET_CODE (XEXP (disp, 0)) != PLUS
6454                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
6455                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
6456                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
6457                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
6458                 {
6459                   reason = "non-constant pic memory reference";
6460                   goto report_error;
6461                 }
6462             }
6463           else if (! legitimate_pic_address_disp_p (disp))
6464             {
6465               reason = "displacement is an invalid pic construct";
6466               goto report_error;
6467             }
6468
6469           /* This code used to verify that a symbolic pic displacement
6470              includes the pic_offset_table_rtx register.
6471
6472              While this is good idea, unfortunately these constructs may
6473              be created by "adds using lea" optimization for incorrect
6474              code like:
6475
6476              int a;
6477              int foo(int i)
6478                {
6479                  return *(&a+i);
6480                }
6481
6482              This code is nonsensical, but results in addressing
6483              GOT table with pic_offset_table_rtx base.  We can't
6484              just refuse it easily, since it gets matched by
6485              "addsi3" pattern, that later gets split to lea in the
6486              case output register differs from input.  While this
6487              can be handled by separate addsi pattern for this case
6488              that never results in lea, this seems to be easier and
6489              correct fix for crash to disable this test.  */
6490         }
6491       else if (GET_CODE (disp) != LABEL_REF
6492                && GET_CODE (disp) != CONST_INT
6493                && (GET_CODE (disp) != CONST
6494                    || !legitimate_constant_p (disp))
6495                && (GET_CODE (disp) != SYMBOL_REF
6496                    || !legitimate_constant_p (disp)))
6497         {
6498           reason = "displacement is not constant";
6499           goto report_error;
6500         }
6501       else if (TARGET_64BIT
6502                && !x86_64_immediate_operand (disp, VOIDmode))
6503         {
6504           reason = "displacement is out of range";
6505           goto report_error;
6506         }
6507     }
6508
6509   /* Everything looks valid.  */
6510   if (TARGET_DEBUG_ADDR)
6511     fprintf (stderr, "Success.\n");
6512   return TRUE;
6513
6514  report_error:
6515   if (TARGET_DEBUG_ADDR)
6516     {
6517       fprintf (stderr, "Error: %s\n", reason);
6518       debug_rtx (reason_rtx);
6519     }
6520   return FALSE;
6521 }
6522 \f
6523 /* Return a unique alias set for the GOT.  */
6524
6525 static HOST_WIDE_INT
6526 ix86_GOT_alias_set (void)
6527 {
6528   static HOST_WIDE_INT set = -1;
6529   if (set == -1)
6530     set = new_alias_set ();
6531   return set;
6532 }
6533
6534 /* Return a legitimate reference for ORIG (an address) using the
6535    register REG.  If REG is 0, a new pseudo is generated.
6536
6537    There are two types of references that must be handled:
6538
6539    1. Global data references must load the address from the GOT, via
6540       the PIC reg.  An insn is emitted to do this load, and the reg is
6541       returned.
6542
6543    2. Static data references, constant pool addresses, and code labels
6544       compute the address as an offset from the GOT, whose base is in
6545       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
6546       differentiate them from global data objects.  The returned
6547       address is the PIC reg + an unspec constant.
6548
6549    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
6550    reg also appears in the address.  */
6551
6552 static rtx
6553 legitimize_pic_address (rtx orig, rtx reg)
6554 {
6555   rtx addr = orig;
6556   rtx new = orig;
6557   rtx base;
6558
6559 #if TARGET_MACHO
6560   if (TARGET_MACHO && !TARGET_64BIT)
6561     {
6562       if (reg == 0)
6563         reg = gen_reg_rtx (Pmode);
6564       /* Use the generic Mach-O PIC machinery.  */
6565       return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
6566     }
6567 #endif
6568
6569   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
6570     new = addr;
6571   else if (TARGET_64BIT
6572            && ix86_cmodel != CM_SMALL_PIC
6573            && local_symbolic_operand (addr, Pmode))
6574     {
6575       rtx tmpreg;
6576       /* This symbol may be referenced via a displacement from the PIC
6577          base address (@GOTOFF).  */
6578
6579       if (reload_in_progress)
6580         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6581       if (GET_CODE (addr) == CONST)
6582         addr = XEXP (addr, 0);
6583       if (GET_CODE (addr) == PLUS)
6584           {
6585             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6586             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6587           }
6588         else
6589           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6590       new = gen_rtx_CONST (Pmode, new);
6591       if (!reg)
6592         tmpreg = gen_reg_rtx (Pmode);
6593       else
6594         tmpreg = reg;
6595       emit_move_insn (tmpreg, new);
6596
6597       if (reg != 0)
6598         {
6599           new = expand_simple_binop (Pmode, PLUS, reg, pic_offset_table_rtx,
6600                                      tmpreg, 1, OPTAB_DIRECT);
6601           new = reg;
6602         }
6603       else new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, tmpreg);
6604     }
6605   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
6606     {
6607       /* This symbol may be referenced via a displacement from the PIC
6608          base address (@GOTOFF).  */
6609
6610       if (reload_in_progress)
6611         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6612       if (GET_CODE (addr) == CONST)
6613         addr = XEXP (addr, 0);
6614       if (GET_CODE (addr) == PLUS)
6615           {
6616             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
6617             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
6618           }
6619         else
6620           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
6621       new = gen_rtx_CONST (Pmode, new);
6622       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6623
6624       if (reg != 0)
6625         {
6626           emit_move_insn (reg, new);
6627           new = reg;
6628         }
6629     }
6630   else if (GET_CODE (addr) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (addr) == 0)
6631     {
6632       if (TARGET_64BIT)
6633         {
6634           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
6635           new = gen_rtx_CONST (Pmode, new);
6636           new = gen_const_mem (Pmode, new);
6637           set_mem_alias_set (new, ix86_GOT_alias_set ());
6638
6639           if (reg == 0)
6640             reg = gen_reg_rtx (Pmode);
6641           /* Use directly gen_movsi, otherwise the address is loaded
6642              into register for CSE.  We don't want to CSE this addresses,
6643              instead we CSE addresses from the GOT table, so skip this.  */
6644           emit_insn (gen_movsi (reg, new));
6645           new = reg;
6646         }
6647       else
6648         {
6649           /* This symbol must be referenced via a load from the
6650              Global Offset Table (@GOT).  */
6651
6652           if (reload_in_progress)
6653             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6654           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
6655           new = gen_rtx_CONST (Pmode, new);
6656           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6657           new = gen_const_mem (Pmode, new);
6658           set_mem_alias_set (new, ix86_GOT_alias_set ());
6659
6660           if (reg == 0)
6661             reg = gen_reg_rtx (Pmode);
6662           emit_move_insn (reg, new);
6663           new = reg;
6664         }
6665     }
6666   else
6667     {
6668       if (GET_CODE (addr) == CONST_INT
6669           && !x86_64_immediate_operand (addr, VOIDmode))
6670         {
6671           if (reg)
6672             {
6673               emit_move_insn (reg, addr);
6674               new = reg;
6675             }
6676           else
6677             new = force_reg (Pmode, addr);
6678         }
6679       else if (GET_CODE (addr) == CONST)
6680         {
6681           addr = XEXP (addr, 0);
6682
6683           /* We must match stuff we generate before.  Assume the only
6684              unspecs that can get here are ours.  Not that we could do
6685              anything with them anyway....  */
6686           if (GET_CODE (addr) == UNSPEC
6687               || (GET_CODE (addr) == PLUS
6688                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
6689             return orig;
6690           gcc_assert (GET_CODE (addr) == PLUS);
6691         }
6692       if (GET_CODE (addr) == PLUS)
6693         {
6694           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
6695
6696           /* Check first to see if this is a constant offset from a @GOTOFF
6697              symbol reference.  */
6698           if (local_symbolic_operand (op0, Pmode)
6699               && GET_CODE (op1) == CONST_INT)
6700             {
6701               if (!TARGET_64BIT)
6702                 {
6703                   if (reload_in_progress)
6704                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6705                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
6706                                         UNSPEC_GOTOFF);
6707                   new = gen_rtx_PLUS (Pmode, new, op1);
6708                   new = gen_rtx_CONST (Pmode, new);
6709                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
6710
6711                   if (reg != 0)
6712                     {
6713                       emit_move_insn (reg, new);
6714                       new = reg;
6715                     }
6716                 }
6717               else
6718                 {
6719                   if (INTVAL (op1) < -16*1024*1024
6720                       || INTVAL (op1) >= 16*1024*1024)
6721                     {
6722                       if (!x86_64_immediate_operand (op1, Pmode))
6723                         op1 = force_reg (Pmode, op1);
6724                       new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
6725                     }
6726                 }
6727             }
6728           else
6729             {
6730               base = legitimize_pic_address (XEXP (addr, 0), reg);
6731               new  = legitimize_pic_address (XEXP (addr, 1),
6732                                              base == reg ? NULL_RTX : reg);
6733
6734               if (GET_CODE (new) == CONST_INT)
6735                 new = plus_constant (base, INTVAL (new));
6736               else
6737                 {
6738                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
6739                     {
6740                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
6741                       new = XEXP (new, 1);
6742                     }
6743                   new = gen_rtx_PLUS (Pmode, base, new);
6744                 }
6745             }
6746         }
6747     }
6748   return new;
6749 }
6750 \f
6751 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
6752
6753 static rtx
6754 get_thread_pointer (int to_reg)
6755 {
6756   rtx tp, reg, insn;
6757
6758   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
6759   if (!to_reg)
6760     return tp;
6761
6762   reg = gen_reg_rtx (Pmode);
6763   insn = gen_rtx_SET (VOIDmode, reg, tp);
6764   insn = emit_insn (insn);
6765
6766   return reg;
6767 }
6768
6769 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
6770    false if we expect this to be used for a memory address and true if
6771    we expect to load the address into a register.  */
6772
6773 static rtx
6774 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
6775 {
6776   rtx dest, base, off, pic, tp;
6777   int type;
6778
6779   switch (model)
6780     {
6781     case TLS_MODEL_GLOBAL_DYNAMIC:
6782       dest = gen_reg_rtx (Pmode);
6783       tp = TARGET_GNU2_TLS ? get_thread_pointer (1) : 0;
6784
6785       if (TARGET_64BIT && ! TARGET_GNU2_TLS)
6786         {
6787           rtx rax = gen_rtx_REG (Pmode, 0), insns;
6788
6789           start_sequence ();
6790           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
6791           insns = get_insns ();
6792           end_sequence ();
6793
6794           emit_libcall_block (insns, dest, rax, x);
6795         }
6796       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6797         emit_insn (gen_tls_global_dynamic_64 (dest, x));
6798       else
6799         emit_insn (gen_tls_global_dynamic_32 (dest, x));
6800
6801       if (TARGET_GNU2_TLS)
6802         {
6803           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, tp, dest));
6804
6805           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6806         }
6807       break;
6808
6809     case TLS_MODEL_LOCAL_DYNAMIC:
6810       base = 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, note;
6816
6817           start_sequence ();
6818           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
6819           insns = get_insns ();
6820           end_sequence ();
6821
6822           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
6823           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
6824           emit_libcall_block (insns, base, rax, note);
6825         }
6826       else if (TARGET_64BIT && TARGET_GNU2_TLS)
6827         emit_insn (gen_tls_local_dynamic_base_64 (base));
6828       else
6829         emit_insn (gen_tls_local_dynamic_base_32 (base));
6830
6831       if (TARGET_GNU2_TLS)
6832         {
6833           rtx x = ix86_tls_module_base ();
6834
6835           set_unique_reg_note (get_last_insn (), REG_EQUIV,
6836                                gen_rtx_MINUS (Pmode, x, tp));
6837         }
6838
6839       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
6840       off = gen_rtx_CONST (Pmode, off);
6841
6842       dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, base, off));
6843
6844       if (TARGET_GNU2_TLS)
6845         {
6846           dest = force_reg (Pmode, gen_rtx_PLUS (Pmode, dest, tp));
6847
6848           set_unique_reg_note (get_last_insn (), REG_EQUIV, x);
6849         }
6850
6851       break;
6852
6853     case TLS_MODEL_INITIAL_EXEC:
6854       if (TARGET_64BIT)
6855         {
6856           pic = NULL;
6857           type = UNSPEC_GOTNTPOFF;
6858         }
6859       else if (flag_pic)
6860         {
6861           if (reload_in_progress)
6862             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
6863           pic = pic_offset_table_rtx;
6864           type = TARGET_ANY_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
6865         }
6866       else if (!TARGET_ANY_GNU_TLS)
6867         {
6868           pic = gen_reg_rtx (Pmode);
6869           emit_insn (gen_set_got (pic));
6870           type = UNSPEC_GOTTPOFF;
6871         }
6872       else
6873         {
6874           pic = NULL;
6875           type = UNSPEC_INDNTPOFF;
6876         }
6877
6878       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
6879       off = gen_rtx_CONST (Pmode, off);
6880       if (pic)
6881         off = gen_rtx_PLUS (Pmode, pic, off);
6882       off = gen_const_mem (Pmode, off);
6883       set_mem_alias_set (off, ix86_GOT_alias_set ());
6884
6885       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6886         {
6887           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6888           off = force_reg (Pmode, off);
6889           return gen_rtx_PLUS (Pmode, base, off);
6890         }
6891       else
6892         {
6893           base = get_thread_pointer (true);
6894           dest = gen_reg_rtx (Pmode);
6895           emit_insn (gen_subsi3 (dest, base, off));
6896         }
6897       break;
6898
6899     case TLS_MODEL_LOCAL_EXEC:
6900       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
6901                             (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6902                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
6903       off = gen_rtx_CONST (Pmode, off);
6904
6905       if (TARGET_64BIT || TARGET_ANY_GNU_TLS)
6906         {
6907           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
6908           return gen_rtx_PLUS (Pmode, base, off);
6909         }
6910       else
6911         {
6912           base = get_thread_pointer (true);
6913           dest = gen_reg_rtx (Pmode);
6914           emit_insn (gen_subsi3 (dest, base, off));
6915         }
6916       break;
6917
6918     default:
6919       gcc_unreachable ();
6920     }
6921
6922   return dest;
6923 }
6924
6925 /* Try machine-dependent ways of modifying an illegitimate address
6926    to be legitimate.  If we find one, return the new, valid address.
6927    This macro is used in only one place: `memory_address' in explow.c.
6928
6929    OLDX is the address as it was before break_out_memory_refs was called.
6930    In some cases it is useful to look at this to decide what needs to be done.
6931
6932    MODE and WIN are passed so that this macro can use
6933    GO_IF_LEGITIMATE_ADDRESS.
6934
6935    It is always safe for this macro to do nothing.  It exists to recognize
6936    opportunities to optimize the output.
6937
6938    For the 80386, we handle X+REG by loading X into a register R and
6939    using R+REG.  R will go in a general reg and indexing will be used.
6940    However, if REG is a broken-out memory address or multiplication,
6941    nothing needs to be done because REG can certainly go in a general reg.
6942
6943    When -fpic is used, special handling is needed for symbolic references.
6944    See comments by legitimize_pic_address in i386.c for details.  */
6945
6946 rtx
6947 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
6948 {
6949   int changed = 0;
6950   unsigned log;
6951
6952   if (TARGET_DEBUG_ADDR)
6953     {
6954       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
6955                GET_MODE_NAME (mode));
6956       debug_rtx (x);
6957     }
6958
6959   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
6960   if (log)
6961     return legitimize_tls_address (x, log, false);
6962   if (GET_CODE (x) == CONST
6963       && GET_CODE (XEXP (x, 0)) == PLUS
6964       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
6965       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
6966     {
6967       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
6968       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
6969     }
6970
6971   if (flag_pic && SYMBOLIC_CONST (x))
6972     return legitimize_pic_address (x, 0);
6973
6974   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
6975   if (GET_CODE (x) == ASHIFT
6976       && GET_CODE (XEXP (x, 1)) == CONST_INT
6977       && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) < 4)
6978     {
6979       changed = 1;
6980       log = INTVAL (XEXP (x, 1));
6981       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
6982                         GEN_INT (1 << log));
6983     }
6984
6985   if (GET_CODE (x) == PLUS)
6986     {
6987       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
6988
6989       if (GET_CODE (XEXP (x, 0)) == ASHIFT
6990           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6991           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 0), 1)) < 4)
6992         {
6993           changed = 1;
6994           log = INTVAL (XEXP (XEXP (x, 0), 1));
6995           XEXP (x, 0) = gen_rtx_MULT (Pmode,
6996                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
6997                                       GEN_INT (1 << log));
6998         }
6999
7000       if (GET_CODE (XEXP (x, 1)) == ASHIFT
7001           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
7002           && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (x, 1), 1)) < 4)
7003         {
7004           changed = 1;
7005           log = INTVAL (XEXP (XEXP (x, 1), 1));
7006           XEXP (x, 1) = gen_rtx_MULT (Pmode,
7007                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
7008                                       GEN_INT (1 << log));
7009         }
7010
7011       /* Put multiply first if it isn't already.  */
7012       if (GET_CODE (XEXP (x, 1)) == MULT)
7013         {
7014           rtx tmp = XEXP (x, 0);
7015           XEXP (x, 0) = XEXP (x, 1);
7016           XEXP (x, 1) = tmp;
7017           changed = 1;
7018         }
7019
7020       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
7021          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
7022          created by virtual register instantiation, register elimination, and
7023          similar optimizations.  */
7024       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
7025         {
7026           changed = 1;
7027           x = gen_rtx_PLUS (Pmode,
7028                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
7029                                           XEXP (XEXP (x, 1), 0)),
7030                             XEXP (XEXP (x, 1), 1));
7031         }
7032
7033       /* Canonicalize
7034          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
7035          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
7036       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
7037                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
7038                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
7039                && CONSTANT_P (XEXP (x, 1)))
7040         {
7041           rtx constant;
7042           rtx other = NULL_RTX;
7043
7044           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7045             {
7046               constant = XEXP (x, 1);
7047               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
7048             }
7049           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
7050             {
7051               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
7052               other = XEXP (x, 1);
7053             }
7054           else
7055             constant = 0;
7056
7057           if (constant)
7058             {
7059               changed = 1;
7060               x = gen_rtx_PLUS (Pmode,
7061                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
7062                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
7063                                 plus_constant (other, INTVAL (constant)));
7064             }
7065         }
7066
7067       if (changed && legitimate_address_p (mode, x, FALSE))
7068         return x;
7069
7070       if (GET_CODE (XEXP (x, 0)) == MULT)
7071         {
7072           changed = 1;
7073           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
7074         }
7075
7076       if (GET_CODE (XEXP (x, 1)) == MULT)
7077         {
7078           changed = 1;
7079           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
7080         }
7081
7082       if (changed
7083           && GET_CODE (XEXP (x, 1)) == REG
7084           && GET_CODE (XEXP (x, 0)) == REG)
7085         return x;
7086
7087       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
7088         {
7089           changed = 1;
7090           x = legitimize_pic_address (x, 0);
7091         }
7092
7093       if (changed && legitimate_address_p (mode, x, FALSE))
7094         return x;
7095
7096       if (GET_CODE (XEXP (x, 0)) == REG)
7097         {
7098           rtx temp = gen_reg_rtx (Pmode);
7099           rtx val  = force_operand (XEXP (x, 1), temp);
7100           if (val != temp)
7101             emit_move_insn (temp, val);
7102
7103           XEXP (x, 1) = temp;
7104           return x;
7105         }
7106
7107       else if (GET_CODE (XEXP (x, 1)) == REG)
7108         {
7109           rtx temp = gen_reg_rtx (Pmode);
7110           rtx val  = force_operand (XEXP (x, 0), temp);
7111           if (val != temp)
7112             emit_move_insn (temp, val);
7113
7114           XEXP (x, 0) = temp;
7115           return x;
7116         }
7117     }
7118
7119   return x;
7120 }
7121 \f
7122 /* Print an integer constant expression in assembler syntax.  Addition
7123    and subtraction are the only arithmetic that may appear in these
7124    expressions.  FILE is the stdio stream to write to, X is the rtx, and
7125    CODE is the operand print code from the output string.  */
7126
7127 static void
7128 output_pic_addr_const (FILE *file, rtx x, int code)
7129 {
7130   char buf[256];
7131
7132   switch (GET_CODE (x))
7133     {
7134     case PC:
7135       gcc_assert (flag_pic);
7136       putc ('.', file);
7137       break;
7138
7139     case SYMBOL_REF:
7140       output_addr_const (file, x);
7141       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
7142         fputs ("@PLT", file);
7143       break;
7144
7145     case LABEL_REF:
7146       x = XEXP (x, 0);
7147       /* FALLTHRU */
7148     case CODE_LABEL:
7149       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
7150       assemble_name (asm_out_file, buf);
7151       break;
7152
7153     case CONST_INT:
7154       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
7155       break;
7156
7157     case CONST:
7158       /* This used to output parentheses around the expression,
7159          but that does not work on the 386 (either ATT or BSD assembler).  */
7160       output_pic_addr_const (file, XEXP (x, 0), code);
7161       break;
7162
7163     case CONST_DOUBLE:
7164       if (GET_MODE (x) == VOIDmode)
7165         {
7166           /* We can use %d if the number is <32 bits and positive.  */
7167           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
7168             fprintf (file, "0x%lx%08lx",
7169                      (unsigned long) CONST_DOUBLE_HIGH (x),
7170                      (unsigned long) CONST_DOUBLE_LOW (x));
7171           else
7172             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
7173         }
7174       else
7175         /* We can't handle floating point constants;
7176            PRINT_OPERAND must handle them.  */
7177         output_operand_lossage ("floating constant misused");
7178       break;
7179
7180     case PLUS:
7181       /* Some assemblers need integer constants to appear first.  */
7182       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
7183         {
7184           output_pic_addr_const (file, XEXP (x, 0), code);
7185           putc ('+', file);
7186           output_pic_addr_const (file, XEXP (x, 1), code);
7187         }
7188       else
7189         {
7190           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
7191           output_pic_addr_const (file, XEXP (x, 1), code);
7192           putc ('+', file);
7193           output_pic_addr_const (file, XEXP (x, 0), code);
7194         }
7195       break;
7196
7197     case MINUS:
7198       if (!TARGET_MACHO)
7199         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
7200       output_pic_addr_const (file, XEXP (x, 0), code);
7201       putc ('-', file);
7202       output_pic_addr_const (file, XEXP (x, 1), code);
7203       if (!TARGET_MACHO)
7204         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
7205       break;
7206
7207      case UNSPEC:
7208        gcc_assert (XVECLEN (x, 0) == 1);
7209        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
7210        switch (XINT (x, 1))
7211         {
7212         case UNSPEC_GOT:
7213           fputs ("@GOT", file);
7214           break;
7215         case UNSPEC_GOTOFF:
7216           fputs ("@GOTOFF", file);
7217           break;
7218         case UNSPEC_GOTPCREL:
7219           fputs ("@GOTPCREL(%rip)", file);
7220           break;
7221         case UNSPEC_GOTTPOFF:
7222           /* FIXME: This might be @TPOFF in Sun ld too.  */
7223           fputs ("@GOTTPOFF", file);
7224           break;
7225         case UNSPEC_TPOFF:
7226           fputs ("@TPOFF", file);
7227           break;
7228         case UNSPEC_NTPOFF:
7229           if (TARGET_64BIT)
7230             fputs ("@TPOFF", file);
7231           else
7232             fputs ("@NTPOFF", file);
7233           break;
7234         case UNSPEC_DTPOFF:
7235           fputs ("@DTPOFF", file);
7236           break;
7237         case UNSPEC_GOTNTPOFF:
7238           if (TARGET_64BIT)
7239             fputs ("@GOTTPOFF(%rip)", file);
7240           else
7241             fputs ("@GOTNTPOFF", file);
7242           break;
7243         case UNSPEC_INDNTPOFF:
7244           fputs ("@INDNTPOFF", file);
7245           break;
7246         default:
7247           output_operand_lossage ("invalid UNSPEC as operand");
7248           break;
7249         }
7250        break;
7251
7252     default:
7253       output_operand_lossage ("invalid expression as operand");
7254     }
7255 }
7256
7257 /* This is called from dwarf2out.c via TARGET_ASM_OUTPUT_DWARF_DTPREL.
7258    We need to emit DTP-relative relocations.  */
7259
7260 static void
7261 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
7262 {
7263   fputs (ASM_LONG, file);
7264   output_addr_const (file, x);
7265   fputs ("@DTPOFF", file);
7266   switch (size)
7267     {
7268     case 4:
7269       break;
7270     case 8:
7271       fputs (", 0", file);
7272       break;
7273     default:
7274       gcc_unreachable ();
7275    }
7276 }
7277
7278 /* In the name of slightly smaller debug output, and to cater to
7279    general assembler lossage, recognize PIC+GOTOFF and turn it back
7280    into a direct symbol reference.
7281
7282    On Darwin, this is necessary to avoid a crash, because Darwin
7283    has a different PIC label for each routine but the DWARF debugging
7284    information is not associated with any particular routine, so it's
7285    necessary to remove references to the PIC label from RTL stored by
7286    the DWARF output code.  */
7287
7288 static rtx
7289 ix86_delegitimize_address (rtx orig_x)
7290 {
7291   rtx x = orig_x;
7292   /* reg_addend is NULL or a multiple of some register.  */
7293   rtx reg_addend = NULL_RTX;
7294   /* const_addend is NULL or a const_int.  */
7295   rtx const_addend = NULL_RTX;
7296   /* This is the result, or NULL.  */
7297   rtx result = NULL_RTX;
7298
7299   if (GET_CODE (x) == MEM)
7300     x = XEXP (x, 0);
7301
7302   if (TARGET_64BIT)
7303     {
7304       if (GET_CODE (x) != CONST
7305           || GET_CODE (XEXP (x, 0)) != UNSPEC
7306           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
7307           || GET_CODE (orig_x) != MEM)
7308         return orig_x;
7309       return XVECEXP (XEXP (x, 0), 0, 0);
7310     }
7311
7312   if (GET_CODE (x) != PLUS
7313       || GET_CODE (XEXP (x, 1)) != CONST)
7314     return orig_x;
7315
7316   if (GET_CODE (XEXP (x, 0)) == REG
7317       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
7318     /* %ebx + GOT/GOTOFF */
7319     ;
7320   else if (GET_CODE (XEXP (x, 0)) == PLUS)
7321     {
7322       /* %ebx + %reg * scale + GOT/GOTOFF */
7323       reg_addend = XEXP (x, 0);
7324       if (GET_CODE (XEXP (reg_addend, 0)) == REG
7325           && REGNO (XEXP (reg_addend, 0)) == PIC_OFFSET_TABLE_REGNUM)
7326         reg_addend = XEXP (reg_addend, 1);
7327       else if (GET_CODE (XEXP (reg_addend, 1)) == REG
7328                && REGNO (XEXP (reg_addend, 1)) == PIC_OFFSET_TABLE_REGNUM)
7329         reg_addend = XEXP (reg_addend, 0);
7330       else
7331         return orig_x;
7332       if (GET_CODE (reg_addend) != REG
7333           && GET_CODE (reg_addend) != MULT
7334           && GET_CODE (reg_addend) != ASHIFT)
7335         return orig_x;
7336     }
7337   else
7338     return orig_x;
7339
7340   x = XEXP (XEXP (x, 1), 0);
7341   if (GET_CODE (x) == PLUS
7342       && GET_CODE (XEXP (x, 1)) == CONST_INT)
7343     {
7344       const_addend = XEXP (x, 1);
7345       x = XEXP (x, 0);
7346     }
7347
7348   if (GET_CODE (x) == UNSPEC
7349       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
7350           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
7351     result = XVECEXP (x, 0, 0);
7352
7353   if (TARGET_MACHO && darwin_local_data_pic (x)
7354       && GET_CODE (orig_x) != MEM)
7355     result = XEXP (x, 0);
7356
7357   if (! result)
7358     return orig_x;
7359
7360   if (const_addend)
7361     result = gen_rtx_PLUS (Pmode, result, const_addend);
7362   if (reg_addend)
7363     result = gen_rtx_PLUS (Pmode, reg_addend, result);
7364   return result;
7365 }
7366 \f
7367 static void
7368 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
7369                     int fp, FILE *file)
7370 {
7371   const char *suffix;
7372
7373   if (mode == CCFPmode || mode == CCFPUmode)
7374     {
7375       enum rtx_code second_code, bypass_code;
7376       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
7377       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
7378       code = ix86_fp_compare_code_to_integer (code);
7379       mode = CCmode;
7380     }
7381   if (reverse)
7382     code = reverse_condition (code);
7383
7384   switch (code)
7385     {
7386     case EQ:
7387       suffix = "e";
7388       break;
7389     case NE:
7390       suffix = "ne";
7391       break;
7392     case GT:
7393       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
7394       suffix = "g";
7395       break;
7396     case GTU:
7397       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
7398          Those same assemblers have the same but opposite lossage on cmov.  */
7399       gcc_assert (mode == CCmode);
7400       suffix = fp ? "nbe" : "a";
7401       break;
7402     case LT:
7403       switch (mode)
7404         {
7405         case CCNOmode:
7406         case CCGOCmode:
7407           suffix = "s";
7408           break;
7409
7410         case CCmode:
7411         case CCGCmode:
7412           suffix = "l";
7413           break;
7414
7415         default:
7416           gcc_unreachable ();
7417         }
7418       break;
7419     case LTU:
7420       gcc_assert (mode == CCmode);
7421       suffix = "b";
7422       break;
7423     case GE:
7424       switch (mode)
7425         {
7426         case CCNOmode:
7427         case CCGOCmode:
7428           suffix = "ns";
7429           break;
7430
7431         case CCmode:
7432         case CCGCmode:
7433           suffix = "ge";
7434           break;
7435
7436         default:
7437           gcc_unreachable ();
7438         }
7439       break;
7440     case GEU:
7441       /* ??? As above.  */
7442       gcc_assert (mode == CCmode);
7443       suffix = fp ? "nb" : "ae";
7444       break;
7445     case LE:
7446       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
7447       suffix = "le";
7448       break;
7449     case LEU:
7450       gcc_assert (mode == CCmode);
7451       suffix = "be";
7452       break;
7453     case UNORDERED:
7454       suffix = fp ? "u" : "p";
7455       break;
7456     case ORDERED:
7457       suffix = fp ? "nu" : "np";
7458       break;
7459     default:
7460       gcc_unreachable ();
7461     }
7462   fputs (suffix, file);
7463 }
7464
7465 /* Print the name of register X to FILE based on its machine mode and number.
7466    If CODE is 'w', pretend the mode is HImode.
7467    If CODE is 'b', pretend the mode is QImode.
7468    If CODE is 'k', pretend the mode is SImode.
7469    If CODE is 'q', pretend the mode is DImode.
7470    If CODE is 'h', pretend the reg is the 'high' byte register.
7471    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
7472
7473 void
7474 print_reg (rtx x, int code, FILE *file)
7475 {
7476   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
7477               && REGNO (x) != FRAME_POINTER_REGNUM
7478               && REGNO (x) != FLAGS_REG
7479               && REGNO (x) != FPSR_REG);
7480
7481   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
7482     putc ('%', file);
7483
7484   if (code == 'w' || MMX_REG_P (x))
7485     code = 2;
7486   else if (code == 'b')
7487     code = 1;
7488   else if (code == 'k')
7489     code = 4;
7490   else if (code == 'q')
7491     code = 8;
7492   else if (code == 'y')
7493     code = 3;
7494   else if (code == 'h')
7495     code = 0;
7496   else
7497     code = GET_MODE_SIZE (GET_MODE (x));
7498
7499   /* Irritatingly, AMD extended registers use different naming convention
7500      from the normal registers.  */
7501   if (REX_INT_REG_P (x))
7502     {
7503       gcc_assert (TARGET_64BIT);
7504       switch (code)
7505         {
7506           case 0:
7507             error ("extended registers have no high halves");
7508             break;
7509           case 1:
7510             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
7511             break;
7512           case 2:
7513             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
7514             break;
7515           case 4:
7516             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
7517             break;
7518           case 8:
7519             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
7520             break;
7521           default:
7522             error ("unsupported operand size for extended register");
7523             break;
7524         }
7525       return;
7526     }
7527   switch (code)
7528     {
7529     case 3:
7530       if (STACK_TOP_P (x))
7531         {
7532           fputs ("st(0)", file);
7533           break;
7534         }
7535       /* FALLTHRU */
7536     case 8:
7537     case 4:
7538     case 12:
7539       if (! ANY_FP_REG_P (x))
7540         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
7541       /* FALLTHRU */
7542     case 16:
7543     case 2:
7544     normal:
7545       fputs (hi_reg_name[REGNO (x)], file);
7546       break;
7547     case 1:
7548       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
7549         goto normal;
7550       fputs (qi_reg_name[REGNO (x)], file);
7551       break;
7552     case 0:
7553       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
7554         goto normal;
7555       fputs (qi_high_reg_name[REGNO (x)], file);
7556       break;
7557     default:
7558       gcc_unreachable ();
7559     }
7560 }
7561
7562 /* Locate some local-dynamic symbol still in use by this function
7563    so that we can print its name in some tls_local_dynamic_base
7564    pattern.  */
7565
7566 static const char *
7567 get_some_local_dynamic_name (void)
7568 {
7569   rtx insn;
7570
7571   if (cfun->machine->some_ld_name)
7572     return cfun->machine->some_ld_name;
7573
7574   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
7575     if (INSN_P (insn)
7576         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
7577       return cfun->machine->some_ld_name;
7578
7579   gcc_unreachable ();
7580 }
7581
7582 static int
7583 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
7584 {
7585   rtx x = *px;
7586
7587   if (GET_CODE (x) == SYMBOL_REF
7588       && SYMBOL_REF_TLS_MODEL (x) == TLS_MODEL_LOCAL_DYNAMIC)
7589     {
7590       cfun->machine->some_ld_name = XSTR (x, 0);
7591       return 1;
7592     }
7593
7594   return 0;
7595 }
7596
7597 /* Meaning of CODE:
7598    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
7599    C -- print opcode suffix for set/cmov insn.
7600    c -- like C, but print reversed condition
7601    F,f -- likewise, but for floating-point.
7602    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
7603         otherwise nothing
7604    R -- print the prefix for register names.
7605    z -- print the opcode suffix for the size of the current operand.
7606    * -- print a star (in certain assembler syntax)
7607    A -- print an absolute memory reference.
7608    w -- print the operand as if it's a "word" (HImode) even if it isn't.
7609    s -- print a shift double count, followed by the assemblers argument
7610         delimiter.
7611    b -- print the QImode name of the register for the indicated operand.
7612         %b0 would print %al if operands[0] is reg 0.
7613    w --  likewise, print the HImode name of the register.
7614    k --  likewise, print the SImode name of the register.
7615    q --  likewise, print the DImode name of the register.
7616    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
7617    y -- print "st(0)" instead of "st" as a register.
7618    D -- print condition for SSE cmp instruction.
7619    P -- if PIC, print an @PLT suffix.
7620    X -- don't print any sort of PIC '@' suffix for a symbol.
7621    & -- print some in-use local-dynamic symbol name.
7622    H -- print a memory address offset by 8; used for sse high-parts
7623  */
7624
7625 void
7626 print_operand (FILE *file, rtx x, int code)
7627 {
7628   if (code)
7629     {
7630       switch (code)
7631         {
7632         case '*':
7633           if (ASSEMBLER_DIALECT == ASM_ATT)
7634             putc ('*', file);
7635           return;
7636
7637         case '&':
7638           assemble_name (file, get_some_local_dynamic_name ());
7639           return;
7640
7641         case 'A':
7642           switch (ASSEMBLER_DIALECT)
7643             {
7644             case ASM_ATT:
7645               putc ('*', file);
7646               break;
7647
7648             case ASM_INTEL:
7649               /* Intel syntax. For absolute addresses, registers should not
7650                  be surrounded by braces.  */
7651               if (GET_CODE (x) != REG)
7652                 {
7653                   putc ('[', file);
7654                   PRINT_OPERAND (file, x, 0);
7655                   putc (']', file);
7656                   return;
7657                 }
7658               break;
7659
7660             default:
7661               gcc_unreachable ();
7662             }
7663
7664           PRINT_OPERAND (file, x, 0);
7665           return;
7666
7667
7668         case 'L':
7669           if (ASSEMBLER_DIALECT == ASM_ATT)
7670             putc ('l', file);
7671           return;
7672
7673         case 'W':
7674           if (ASSEMBLER_DIALECT == ASM_ATT)
7675             putc ('w', file);
7676           return;
7677
7678         case 'B':
7679           if (ASSEMBLER_DIALECT == ASM_ATT)
7680             putc ('b', file);
7681           return;
7682
7683         case 'Q':
7684           if (ASSEMBLER_DIALECT == ASM_ATT)
7685             putc ('l', file);
7686           return;
7687
7688         case 'S':
7689           if (ASSEMBLER_DIALECT == ASM_ATT)
7690             putc ('s', file);
7691           return;
7692
7693         case 'T':
7694           if (ASSEMBLER_DIALECT == ASM_ATT)
7695             putc ('t', file);
7696           return;
7697
7698         case 'z':
7699           /* 387 opcodes don't get size suffixes if the operands are
7700              registers.  */
7701           if (STACK_REG_P (x))
7702             return;
7703
7704           /* Likewise if using Intel opcodes.  */
7705           if (ASSEMBLER_DIALECT == ASM_INTEL)
7706             return;
7707
7708           /* This is the size of op from size of operand.  */
7709           switch (GET_MODE_SIZE (GET_MODE (x)))
7710             {
7711             case 2:
7712 #ifdef HAVE_GAS_FILDS_FISTS
7713               putc ('s', file);
7714 #endif
7715               return;
7716
7717             case 4:
7718               if (GET_MODE (x) == SFmode)
7719                 {
7720                   putc ('s', file);
7721                   return;
7722                 }
7723               else
7724                 putc ('l', file);
7725               return;
7726
7727             case 12:
7728             case 16:
7729               putc ('t', file);
7730               return;
7731
7732             case 8:
7733               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
7734                 {
7735 #ifdef GAS_MNEMONICS
7736                   putc ('q', file);
7737 #else
7738                   putc ('l', file);
7739                   putc ('l', file);
7740 #endif
7741                 }
7742               else
7743                 putc ('l', file);
7744               return;
7745
7746             default:
7747               gcc_unreachable ();
7748             }
7749
7750         case 'b':
7751         case 'w':
7752         case 'k':
7753         case 'q':
7754         case 'h':
7755         case 'y':
7756         case 'X':
7757         case 'P':
7758           break;
7759
7760         case 's':
7761           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
7762             {
7763               PRINT_OPERAND (file, x, 0);
7764               putc (',', file);
7765             }
7766           return;
7767
7768         case 'D':
7769           /* Little bit of braindamage here.  The SSE compare instructions
7770              does use completely different names for the comparisons that the
7771              fp conditional moves.  */
7772           switch (GET_CODE (x))
7773             {
7774             case EQ:
7775             case UNEQ:
7776               fputs ("eq", file);
7777               break;
7778             case LT:
7779             case UNLT:
7780               fputs ("lt", file);
7781               break;
7782             case LE:
7783             case UNLE:
7784               fputs ("le", file);
7785               break;
7786             case UNORDERED:
7787               fputs ("unord", file);
7788               break;
7789             case NE:
7790             case LTGT:
7791               fputs ("neq", file);
7792               break;
7793             case UNGE:
7794             case GE:
7795               fputs ("nlt", file);
7796               break;
7797             case UNGT:
7798             case GT:
7799               fputs ("nle", file);
7800               break;
7801             case ORDERED:
7802               fputs ("ord", file);
7803               break;
7804             default:
7805               gcc_unreachable ();
7806             }
7807           return;
7808         case 'O':
7809 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7810           if (ASSEMBLER_DIALECT == ASM_ATT)
7811             {
7812               switch (GET_MODE (x))
7813                 {
7814                 case HImode: putc ('w', file); break;
7815                 case SImode:
7816                 case SFmode: putc ('l', file); break;
7817                 case DImode:
7818                 case DFmode: putc ('q', file); break;
7819                 default: gcc_unreachable ();
7820                 }
7821               putc ('.', file);
7822             }
7823 #endif
7824           return;
7825         case 'C':
7826           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
7827           return;
7828         case 'F':
7829 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7830           if (ASSEMBLER_DIALECT == ASM_ATT)
7831             putc ('.', file);
7832 #endif
7833           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
7834           return;
7835
7836           /* Like above, but reverse condition */
7837         case 'c':
7838           /* Check to see if argument to %c is really a constant
7839              and not a condition code which needs to be reversed.  */
7840           if (!COMPARISON_P (x))
7841           {
7842             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
7843              return;
7844           }
7845           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
7846           return;
7847         case 'f':
7848 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
7849           if (ASSEMBLER_DIALECT == ASM_ATT)
7850             putc ('.', file);
7851 #endif
7852           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
7853           return;
7854
7855         case 'H':
7856           /* It doesn't actually matter what mode we use here, as we're
7857              only going to use this for printing.  */
7858           x = adjust_address_nv (x, DImode, 8);
7859           break;
7860
7861         case '+':
7862           {
7863             rtx x;
7864
7865             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
7866               return;
7867
7868             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
7869             if (x)
7870               {
7871                 int pred_val = INTVAL (XEXP (x, 0));
7872
7873                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
7874                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
7875                   {
7876                     int taken = pred_val > REG_BR_PROB_BASE / 2;
7877                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
7878
7879                     /* Emit hints only in the case default branch prediction
7880                        heuristics would fail.  */
7881                     if (taken != cputaken)
7882                       {
7883                         /* We use 3e (DS) prefix for taken branches and
7884                            2e (CS) prefix for not taken branches.  */
7885                         if (taken)
7886                           fputs ("ds ; ", file);
7887                         else
7888                           fputs ("cs ; ", file);
7889                       }
7890                   }
7891               }
7892             return;
7893           }
7894         default:
7895             output_operand_lossage ("invalid operand code '%c'", code);
7896         }
7897     }
7898
7899   if (GET_CODE (x) == REG)
7900     print_reg (x, code, file);
7901
7902   else if (GET_CODE (x) == MEM)
7903     {
7904       /* No `byte ptr' prefix for call instructions.  */
7905       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
7906         {
7907           const char * size;
7908           switch (GET_MODE_SIZE (GET_MODE (x)))
7909             {
7910             case 1: size = "BYTE"; break;
7911             case 2: size = "WORD"; break;
7912             case 4: size = "DWORD"; break;
7913             case 8: size = "QWORD"; break;
7914             case 12: size = "XWORD"; break;
7915             case 16: size = "XMMWORD"; break;
7916             default:
7917               gcc_unreachable ();
7918             }
7919
7920           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
7921           if (code == 'b')
7922             size = "BYTE";
7923           else if (code == 'w')
7924             size = "WORD";
7925           else if (code == 'k')
7926             size = "DWORD";
7927
7928           fputs (size, file);
7929           fputs (" PTR ", file);
7930         }
7931
7932       x = XEXP (x, 0);
7933       /* Avoid (%rip) for call operands.  */
7934       if (CONSTANT_ADDRESS_P (x) && code == 'P'
7935                && GET_CODE (x) != CONST_INT)
7936         output_addr_const (file, x);
7937       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
7938         output_operand_lossage ("invalid constraints for operand");
7939       else
7940         output_address (x);
7941     }
7942
7943   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
7944     {
7945       REAL_VALUE_TYPE r;
7946       long l;
7947
7948       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7949       REAL_VALUE_TO_TARGET_SINGLE (r, l);
7950
7951       if (ASSEMBLER_DIALECT == ASM_ATT)
7952         putc ('$', file);
7953       fprintf (file, "0x%08lx", l);
7954     }
7955
7956   /* These float cases don't actually occur as immediate operands.  */
7957   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
7958     {
7959       char dstr[30];
7960
7961       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7962       fprintf (file, "%s", dstr);
7963     }
7964
7965   else if (GET_CODE (x) == CONST_DOUBLE
7966            && GET_MODE (x) == XFmode)
7967     {
7968       char dstr[30];
7969
7970       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
7971       fprintf (file, "%s", dstr);
7972     }
7973
7974   else
7975     {
7976       /* We have patterns that allow zero sets of memory, for instance.
7977          In 64-bit mode, we should probably support all 8-byte vectors,
7978          since we can in fact encode that into an immediate.  */
7979       if (GET_CODE (x) == CONST_VECTOR)
7980         {
7981           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
7982           x = const0_rtx;
7983         }
7984
7985       if (code != 'P')
7986         {
7987           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
7988             {
7989               if (ASSEMBLER_DIALECT == ASM_ATT)
7990                 putc ('$', file);
7991             }
7992           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
7993                    || GET_CODE (x) == LABEL_REF)
7994             {
7995               if (ASSEMBLER_DIALECT == ASM_ATT)
7996                 putc ('$', file);
7997               else
7998                 fputs ("OFFSET FLAT:", file);
7999             }
8000         }
8001       if (GET_CODE (x) == CONST_INT)
8002         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
8003       else if (flag_pic)
8004         output_pic_addr_const (file, x, code);
8005       else
8006         output_addr_const (file, x);
8007     }
8008 }
8009 \f
8010 /* Print a memory operand whose address is ADDR.  */
8011
8012 void
8013 print_operand_address (FILE *file, rtx addr)
8014 {
8015   struct ix86_address parts;
8016   rtx base, index, disp;
8017   int scale;
8018   int ok = ix86_decompose_address (addr, &parts);
8019
8020   gcc_assert (ok);
8021
8022   base = parts.base;
8023   index = parts.index;
8024   disp = parts.disp;
8025   scale = parts.scale;
8026
8027   switch (parts.seg)
8028     {
8029     case SEG_DEFAULT:
8030       break;
8031     case SEG_FS:
8032     case SEG_GS:
8033       if (USER_LABEL_PREFIX[0] == 0)
8034         putc ('%', file);
8035       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
8036       break;
8037     default:
8038       gcc_unreachable ();
8039     }
8040
8041   if (!base && !index)
8042     {
8043       /* Displacement only requires special attention.  */
8044
8045       if (GET_CODE (disp) == CONST_INT)
8046         {
8047           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
8048             {
8049               if (USER_LABEL_PREFIX[0] == 0)
8050                 putc ('%', file);
8051               fputs ("ds:", file);
8052             }
8053           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
8054         }
8055       else if (flag_pic)
8056         output_pic_addr_const (file, disp, 0);
8057       else
8058         output_addr_const (file, disp);
8059
8060       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
8061       if (TARGET_64BIT)
8062         {
8063           if (GET_CODE (disp) == CONST
8064               && GET_CODE (XEXP (disp, 0)) == PLUS
8065               && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8066             disp = XEXP (XEXP (disp, 0), 0);
8067           if (GET_CODE (disp) == LABEL_REF
8068               || (GET_CODE (disp) == SYMBOL_REF
8069                   && SYMBOL_REF_TLS_MODEL (disp) == 0))
8070             fputs ("(%rip)", file);
8071         }
8072     }
8073   else
8074     {
8075       if (ASSEMBLER_DIALECT == ASM_ATT)
8076         {
8077           if (disp)
8078             {
8079               if (flag_pic)
8080                 output_pic_addr_const (file, disp, 0);
8081               else if (GET_CODE (disp) == LABEL_REF)
8082                 output_asm_label (disp);
8083               else
8084                 output_addr_const (file, disp);
8085             }
8086
8087           putc ('(', file);
8088           if (base)
8089             print_reg (base, 0, file);
8090           if (index)
8091             {
8092               putc (',', file);
8093               print_reg (index, 0, file);
8094               if (scale != 1)
8095                 fprintf (file, ",%d", scale);
8096             }
8097           putc (')', file);
8098         }
8099       else
8100         {
8101           rtx offset = NULL_RTX;
8102
8103           if (disp)
8104             {
8105               /* Pull out the offset of a symbol; print any symbol itself.  */
8106               if (GET_CODE (disp) == CONST
8107                   && GET_CODE (XEXP (disp, 0)) == PLUS
8108                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
8109                 {
8110                   offset = XEXP (XEXP (disp, 0), 1);
8111                   disp = gen_rtx_CONST (VOIDmode,
8112                                         XEXP (XEXP (disp, 0), 0));
8113                 }
8114
8115               if (flag_pic)
8116                 output_pic_addr_const (file, disp, 0);
8117               else if (GET_CODE (disp) == LABEL_REF)
8118                 output_asm_label (disp);
8119               else if (GET_CODE (disp) == CONST_INT)
8120                 offset = disp;
8121               else
8122                 output_addr_const (file, disp);
8123             }
8124
8125           putc ('[', file);
8126           if (base)
8127             {
8128               print_reg (base, 0, file);
8129               if (offset)
8130                 {
8131                   if (INTVAL (offset) >= 0)
8132                     putc ('+', file);
8133                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8134                 }
8135             }
8136           else if (offset)
8137             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
8138           else
8139             putc ('0', file);
8140
8141           if (index)
8142             {
8143               putc ('+', file);
8144               print_reg (index, 0, file);
8145               if (scale != 1)
8146                 fprintf (file, "*%d", scale);
8147             }
8148           putc (']', file);
8149         }
8150     }
8151 }
8152
8153 bool
8154 output_addr_const_extra (FILE *file, rtx x)
8155 {
8156   rtx op;
8157
8158   if (GET_CODE (x) != UNSPEC)
8159     return false;
8160
8161   op = XVECEXP (x, 0, 0);
8162   switch (XINT (x, 1))
8163     {
8164     case UNSPEC_GOTTPOFF:
8165       output_addr_const (file, op);
8166       /* FIXME: This might be @TPOFF in Sun ld.  */
8167       fputs ("@GOTTPOFF", file);
8168       break;
8169     case UNSPEC_TPOFF:
8170       output_addr_const (file, op);
8171       fputs ("@TPOFF", file);
8172       break;
8173     case UNSPEC_NTPOFF:
8174       output_addr_const (file, op);
8175       if (TARGET_64BIT)
8176         fputs ("@TPOFF", file);
8177       else
8178         fputs ("@NTPOFF", file);
8179       break;
8180     case UNSPEC_DTPOFF:
8181       output_addr_const (file, op);
8182       fputs ("@DTPOFF", file);
8183       break;
8184     case UNSPEC_GOTNTPOFF:
8185       output_addr_const (file, op);
8186       if (TARGET_64BIT)
8187         fputs ("@GOTTPOFF(%rip)", file);
8188       else
8189         fputs ("@GOTNTPOFF", file);
8190       break;
8191     case UNSPEC_INDNTPOFF:
8192       output_addr_const (file, op);
8193       fputs ("@INDNTPOFF", file);
8194       break;
8195
8196     default:
8197       return false;
8198     }
8199
8200   return true;
8201 }
8202 \f
8203 /* Split one or more DImode RTL references into pairs of SImode
8204    references.  The RTL can be REG, offsettable MEM, integer constant, or
8205    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8206    split and "num" is its length.  lo_half and hi_half are output arrays
8207    that parallel "operands".  */
8208
8209 void
8210 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8211 {
8212   while (num--)
8213     {
8214       rtx op = operands[num];
8215
8216       /* simplify_subreg refuse to split volatile memory addresses,
8217          but we still have to handle it.  */
8218       if (GET_CODE (op) == MEM)
8219         {
8220           lo_half[num] = adjust_address (op, SImode, 0);
8221           hi_half[num] = adjust_address (op, SImode, 4);
8222         }
8223       else
8224         {
8225           lo_half[num] = simplify_gen_subreg (SImode, op,
8226                                               GET_MODE (op) == VOIDmode
8227                                               ? DImode : GET_MODE (op), 0);
8228           hi_half[num] = simplify_gen_subreg (SImode, op,
8229                                               GET_MODE (op) == VOIDmode
8230                                               ? DImode : GET_MODE (op), 4);
8231         }
8232     }
8233 }
8234 /* Split one or more TImode RTL references into pairs of DImode
8235    references.  The RTL can be REG, offsettable MEM, integer constant, or
8236    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
8237    split and "num" is its length.  lo_half and hi_half are output arrays
8238    that parallel "operands".  */
8239
8240 void
8241 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
8242 {
8243   while (num--)
8244     {
8245       rtx op = operands[num];
8246
8247       /* simplify_subreg refuse to split volatile memory addresses, but we
8248          still have to handle it.  */
8249       if (GET_CODE (op) == MEM)
8250         {
8251           lo_half[num] = adjust_address (op, DImode, 0);
8252           hi_half[num] = adjust_address (op, DImode, 8);
8253         }
8254       else
8255         {
8256           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
8257           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
8258         }
8259     }
8260 }
8261 \f
8262 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
8263    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
8264    is the expression of the binary operation.  The output may either be
8265    emitted here, or returned to the caller, like all output_* functions.
8266
8267    There is no guarantee that the operands are the same mode, as they
8268    might be within FLOAT or FLOAT_EXTEND expressions.  */
8269
8270 #ifndef SYSV386_COMPAT
8271 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
8272    wants to fix the assemblers because that causes incompatibility
8273    with gcc.  No-one wants to fix gcc because that causes
8274    incompatibility with assemblers...  You can use the option of
8275    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
8276 #define SYSV386_COMPAT 1
8277 #endif
8278
8279 const char *
8280 output_387_binary_op (rtx insn, rtx *operands)
8281 {
8282   static char buf[30];
8283   const char *p;
8284   const char *ssep;
8285   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
8286
8287 #ifdef ENABLE_CHECKING
8288   /* Even if we do not want to check the inputs, this documents input
8289      constraints.  Which helps in understanding the following code.  */
8290   if (STACK_REG_P (operands[0])
8291       && ((REG_P (operands[1])
8292            && REGNO (operands[0]) == REGNO (operands[1])
8293            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
8294           || (REG_P (operands[2])
8295               && REGNO (operands[0]) == REGNO (operands[2])
8296               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
8297       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
8298     ; /* ok */
8299   else
8300     gcc_assert (is_sse);
8301 #endif
8302
8303   switch (GET_CODE (operands[3]))
8304     {
8305     case PLUS:
8306       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8307           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8308         p = "fiadd";
8309       else
8310         p = "fadd";
8311       ssep = "add";
8312       break;
8313
8314     case MINUS:
8315       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8316           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8317         p = "fisub";
8318       else
8319         p = "fsub";
8320       ssep = "sub";
8321       break;
8322
8323     case MULT:
8324       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8325           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8326         p = "fimul";
8327       else
8328         p = "fmul";
8329       ssep = "mul";
8330       break;
8331
8332     case DIV:
8333       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
8334           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
8335         p = "fidiv";
8336       else
8337         p = "fdiv";
8338       ssep = "div";
8339       break;
8340
8341     default:
8342       gcc_unreachable ();
8343     }
8344
8345   if (is_sse)
8346    {
8347       strcpy (buf, ssep);
8348       if (GET_MODE (operands[0]) == SFmode)
8349         strcat (buf, "ss\t{%2, %0|%0, %2}");
8350       else
8351         strcat (buf, "sd\t{%2, %0|%0, %2}");
8352       return buf;
8353    }
8354   strcpy (buf, p);
8355
8356   switch (GET_CODE (operands[3]))
8357     {
8358     case MULT:
8359     case PLUS:
8360       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
8361         {
8362           rtx temp = operands[2];
8363           operands[2] = operands[1];
8364           operands[1] = temp;
8365         }
8366
8367       /* know operands[0] == operands[1].  */
8368
8369       if (GET_CODE (operands[2]) == MEM)
8370         {
8371           p = "%z2\t%2";
8372           break;
8373         }
8374
8375       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8376         {
8377           if (STACK_TOP_P (operands[0]))
8378             /* How is it that we are storing to a dead operand[2]?
8379                Well, presumably operands[1] is dead too.  We can't
8380                store the result to st(0) as st(0) gets popped on this
8381                instruction.  Instead store to operands[2] (which I
8382                think has to be st(1)).  st(1) will be popped later.
8383                gcc <= 2.8.1 didn't have this check and generated
8384                assembly code that the Unixware assembler rejected.  */
8385             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
8386           else
8387             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8388           break;
8389         }
8390
8391       if (STACK_TOP_P (operands[0]))
8392         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
8393       else
8394         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
8395       break;
8396
8397     case MINUS:
8398     case DIV:
8399       if (GET_CODE (operands[1]) == MEM)
8400         {
8401           p = "r%z1\t%1";
8402           break;
8403         }
8404
8405       if (GET_CODE (operands[2]) == MEM)
8406         {
8407           p = "%z2\t%2";
8408           break;
8409         }
8410
8411       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
8412         {
8413 #if SYSV386_COMPAT
8414           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
8415              derived assemblers, confusingly reverse the direction of
8416              the operation for fsub{r} and fdiv{r} when the
8417              destination register is not st(0).  The Intel assembler
8418              doesn't have this brain damage.  Read !SYSV386_COMPAT to
8419              figure out what the hardware really does.  */
8420           if (STACK_TOP_P (operands[0]))
8421             p = "{p\t%0, %2|rp\t%2, %0}";
8422           else
8423             p = "{rp\t%2, %0|p\t%0, %2}";
8424 #else
8425           if (STACK_TOP_P (operands[0]))
8426             /* As above for fmul/fadd, we can't store to st(0).  */
8427             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
8428           else
8429             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
8430 #endif
8431           break;
8432         }
8433
8434       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
8435         {
8436 #if SYSV386_COMPAT
8437           if (STACK_TOP_P (operands[0]))
8438             p = "{rp\t%0, %1|p\t%1, %0}";
8439           else
8440             p = "{p\t%1, %0|rp\t%0, %1}";
8441 #else
8442           if (STACK_TOP_P (operands[0]))
8443             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
8444           else
8445             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
8446 #endif
8447           break;
8448         }
8449
8450       if (STACK_TOP_P (operands[0]))
8451         {
8452           if (STACK_TOP_P (operands[1]))
8453             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
8454           else
8455             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
8456           break;
8457         }
8458       else if (STACK_TOP_P (operands[1]))
8459         {
8460 #if SYSV386_COMPAT
8461           p = "{\t%1, %0|r\t%0, %1}";
8462 #else
8463           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
8464 #endif
8465         }
8466       else
8467         {
8468 #if SYSV386_COMPAT
8469           p = "{r\t%2, %0|\t%0, %2}";
8470 #else
8471           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
8472 #endif
8473         }
8474       break;
8475
8476     default:
8477       gcc_unreachable ();
8478     }
8479
8480   strcat (buf, p);
8481   return buf;
8482 }
8483
8484 /* Return needed mode for entity in optimize_mode_switching pass.  */
8485
8486 int
8487 ix86_mode_needed (int entity, rtx insn)
8488 {
8489   enum attr_i387_cw mode;
8490
8491   /* The mode UNINITIALIZED is used to store control word after a
8492      function call or ASM pattern.  The mode ANY specify that function
8493      has no requirements on the control word and make no changes in the
8494      bits we are interested in.  */
8495
8496   if (CALL_P (insn)
8497       || (NONJUMP_INSN_P (insn)
8498           && (asm_noperands (PATTERN (insn)) >= 0
8499               || GET_CODE (PATTERN (insn)) == ASM_INPUT)))
8500     return I387_CW_UNINITIALIZED;
8501
8502   if (recog_memoized (insn) < 0)
8503     return I387_CW_ANY;
8504
8505   mode = get_attr_i387_cw (insn);
8506
8507   switch (entity)
8508     {
8509     case I387_TRUNC:
8510       if (mode == I387_CW_TRUNC)
8511         return mode;
8512       break;
8513
8514     case I387_FLOOR:
8515       if (mode == I387_CW_FLOOR)
8516         return mode;
8517       break;
8518
8519     case I387_CEIL:
8520       if (mode == I387_CW_CEIL)
8521         return mode;
8522       break;
8523
8524     case I387_MASK_PM:
8525       if (mode == I387_CW_MASK_PM)
8526         return mode;
8527       break;
8528
8529     default:
8530       gcc_unreachable ();
8531     }
8532
8533   return I387_CW_ANY;
8534 }
8535
8536 /* Output code to initialize control word copies used by trunc?f?i and
8537    rounding patterns.  CURRENT_MODE is set to current control word,
8538    while NEW_MODE is set to new control word.  */
8539
8540 void
8541 emit_i387_cw_initialization (int mode)
8542 {
8543   rtx stored_mode = assign_386_stack_local (HImode, SLOT_CW_STORED);
8544   rtx new_mode;
8545
8546   int slot;
8547
8548   rtx reg = gen_reg_rtx (HImode);
8549
8550   emit_insn (gen_x86_fnstcw_1 (stored_mode));
8551   emit_move_insn (reg, stored_mode);
8552
8553   if (TARGET_64BIT || TARGET_PARTIAL_REG_STALL || optimize_size)
8554     {
8555       switch (mode)
8556         {
8557         case I387_CW_TRUNC:
8558           /* round toward zero (truncate) */
8559           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
8560           slot = SLOT_CW_TRUNC;
8561           break;
8562
8563         case I387_CW_FLOOR:
8564           /* round down toward -oo */
8565           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8566           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
8567           slot = SLOT_CW_FLOOR;
8568           break;
8569
8570         case I387_CW_CEIL:
8571           /* round up toward +oo */
8572           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
8573           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
8574           slot = SLOT_CW_CEIL;
8575           break;
8576
8577         case I387_CW_MASK_PM:
8578           /* mask precision exception for nearbyint() */
8579           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8580           slot = SLOT_CW_MASK_PM;
8581           break;
8582
8583         default:
8584           gcc_unreachable ();
8585         }
8586     }
8587   else
8588     {
8589       switch (mode)
8590         {
8591         case I387_CW_TRUNC:
8592           /* round toward zero (truncate) */
8593           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
8594           slot = SLOT_CW_TRUNC;
8595           break;
8596
8597         case I387_CW_FLOOR:
8598           /* round down toward -oo */
8599           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
8600           slot = SLOT_CW_FLOOR;
8601           break;
8602
8603         case I387_CW_CEIL:
8604           /* round up toward +oo */
8605           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
8606           slot = SLOT_CW_CEIL;
8607           break;
8608
8609         case I387_CW_MASK_PM:
8610           /* mask precision exception for nearbyint() */
8611           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
8612           slot = SLOT_CW_MASK_PM;
8613           break;
8614
8615         default:
8616           gcc_unreachable ();
8617         }
8618     }
8619
8620   gcc_assert (slot < MAX_386_STACK_LOCALS);
8621
8622   new_mode = assign_386_stack_local (HImode, slot);
8623   emit_move_insn (new_mode, reg);
8624 }
8625
8626 /* Output code for INSN to convert a float to a signed int.  OPERANDS
8627    are the insn operands.  The output may be [HSD]Imode and the input
8628    operand may be [SDX]Fmode.  */
8629
8630 const char *
8631 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
8632 {
8633   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8634   int dimode_p = GET_MODE (operands[0]) == DImode;
8635   int round_mode = get_attr_i387_cw (insn);
8636
8637   /* Jump through a hoop or two for DImode, since the hardware has no
8638      non-popping instruction.  We used to do this a different way, but
8639      that was somewhat fragile and broke with post-reload splitters.  */
8640   if ((dimode_p || fisttp) && !stack_top_dies)
8641     output_asm_insn ("fld\t%y1", operands);
8642
8643   gcc_assert (STACK_TOP_P (operands[1]));
8644   gcc_assert (GET_CODE (operands[0]) == MEM);
8645
8646   if (fisttp)
8647       output_asm_insn ("fisttp%z0\t%0", operands);
8648   else
8649     {
8650       if (round_mode != I387_CW_ANY)
8651         output_asm_insn ("fldcw\t%3", operands);
8652       if (stack_top_dies || dimode_p)
8653         output_asm_insn ("fistp%z0\t%0", operands);
8654       else
8655         output_asm_insn ("fist%z0\t%0", operands);
8656       if (round_mode != I387_CW_ANY)
8657         output_asm_insn ("fldcw\t%2", operands);
8658     }
8659
8660   return "";
8661 }
8662
8663 /* Output code for x87 ffreep insn.  The OPNO argument, which may only
8664    have the values zero or one, indicates the ffreep insn's operand
8665    from the OPERANDS array.  */
8666
8667 static const char *
8668 output_387_ffreep (rtx *operands ATTRIBUTE_UNUSED, int opno)
8669 {
8670   if (TARGET_USE_FFREEP)
8671 #if HAVE_AS_IX86_FFREEP
8672     return opno ? "ffreep\t%y1" : "ffreep\t%y0";
8673 #else
8674     switch (REGNO (operands[opno]))
8675       {
8676       case FIRST_STACK_REG + 0: return ".word\t0xc0df";
8677       case FIRST_STACK_REG + 1: return ".word\t0xc1df";
8678       case FIRST_STACK_REG + 2: return ".word\t0xc2df";
8679       case FIRST_STACK_REG + 3: return ".word\t0xc3df";
8680       case FIRST_STACK_REG + 4: return ".word\t0xc4df";
8681       case FIRST_STACK_REG + 5: return ".word\t0xc5df";
8682       case FIRST_STACK_REG + 6: return ".word\t0xc6df";
8683       case FIRST_STACK_REG + 7: return ".word\t0xc7df";
8684       }
8685 #endif
8686
8687   return opno ? "fstp\t%y1" : "fstp\t%y0";
8688 }
8689
8690
8691 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
8692    should be used.  UNORDERED_P is true when fucom should be used.  */
8693
8694 const char *
8695 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
8696 {
8697   int stack_top_dies;
8698   rtx cmp_op0, cmp_op1;
8699   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
8700
8701   if (eflags_p)
8702     {
8703       cmp_op0 = operands[0];
8704       cmp_op1 = operands[1];
8705     }
8706   else
8707     {
8708       cmp_op0 = operands[1];
8709       cmp_op1 = operands[2];
8710     }
8711
8712   if (is_sse)
8713     {
8714       if (GET_MODE (operands[0]) == SFmode)
8715         if (unordered_p)
8716           return "ucomiss\t{%1, %0|%0, %1}";
8717         else
8718           return "comiss\t{%1, %0|%0, %1}";
8719       else
8720         if (unordered_p)
8721           return "ucomisd\t{%1, %0|%0, %1}";
8722         else
8723           return "comisd\t{%1, %0|%0, %1}";
8724     }
8725
8726   gcc_assert (STACK_TOP_P (cmp_op0));
8727
8728   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
8729
8730   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
8731     {
8732       if (stack_top_dies)
8733         {
8734           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
8735           return output_387_ffreep (operands, 1);
8736         }
8737       else
8738         return "ftst\n\tfnstsw\t%0";
8739     }
8740
8741   if (STACK_REG_P (cmp_op1)
8742       && stack_top_dies
8743       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
8744       && REGNO (cmp_op1) != FIRST_STACK_REG)
8745     {
8746       /* If both the top of the 387 stack dies, and the other operand
8747          is also a stack register that dies, then this must be a
8748          `fcompp' float compare */
8749
8750       if (eflags_p)
8751         {
8752           /* There is no double popping fcomi variant.  Fortunately,
8753              eflags is immune from the fstp's cc clobbering.  */
8754           if (unordered_p)
8755             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
8756           else
8757             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
8758           return output_387_ffreep (operands, 0);
8759         }
8760       else
8761         {
8762           if (unordered_p)
8763             return "fucompp\n\tfnstsw\t%0";
8764           else
8765             return "fcompp\n\tfnstsw\t%0";
8766         }
8767     }
8768   else
8769     {
8770       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
8771
8772       static const char * const alt[16] =
8773       {
8774         "fcom%z2\t%y2\n\tfnstsw\t%0",
8775         "fcomp%z2\t%y2\n\tfnstsw\t%0",
8776         "fucom%z2\t%y2\n\tfnstsw\t%0",
8777         "fucomp%z2\t%y2\n\tfnstsw\t%0",
8778
8779         "ficom%z2\t%y2\n\tfnstsw\t%0",
8780         "ficomp%z2\t%y2\n\tfnstsw\t%0",
8781         NULL,
8782         NULL,
8783
8784         "fcomi\t{%y1, %0|%0, %y1}",
8785         "fcomip\t{%y1, %0|%0, %y1}",
8786         "fucomi\t{%y1, %0|%0, %y1}",
8787         "fucomip\t{%y1, %0|%0, %y1}",
8788
8789         NULL,
8790         NULL,
8791         NULL,
8792         NULL
8793       };
8794
8795       int mask;
8796       const char *ret;
8797
8798       mask  = eflags_p << 3;
8799       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
8800       mask |= unordered_p << 1;
8801       mask |= stack_top_dies;
8802
8803       gcc_assert (mask < 16);
8804       ret = alt[mask];
8805       gcc_assert (ret);
8806
8807       return ret;
8808     }
8809 }
8810
8811 void
8812 ix86_output_addr_vec_elt (FILE *file, int value)
8813 {
8814   const char *directive = ASM_LONG;
8815
8816 #ifdef ASM_QUAD
8817   if (TARGET_64BIT)
8818     directive = ASM_QUAD;
8819 #else
8820   gcc_assert (!TARGET_64BIT);
8821 #endif
8822
8823   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
8824 }
8825
8826 void
8827 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
8828 {
8829   if (TARGET_64BIT)
8830     fprintf (file, "%s%s%d-%s%d\n",
8831              ASM_LONG, LPREFIX, value, LPREFIX, rel);
8832   else if (HAVE_AS_GOTOFF_IN_DATA)
8833     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
8834 #if TARGET_MACHO
8835   else if (TARGET_MACHO)
8836     {
8837       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
8838       machopic_output_function_base_name (file);
8839       fprintf(file, "\n");
8840     }
8841 #endif
8842   else
8843     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
8844                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
8845 }
8846 \f
8847 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
8848    for the target.  */
8849
8850 void
8851 ix86_expand_clear (rtx dest)
8852 {
8853   rtx tmp;
8854
8855   /* We play register width games, which are only valid after reload.  */
8856   gcc_assert (reload_completed);
8857
8858   /* Avoid HImode and its attendant prefix byte.  */
8859   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
8860     dest = gen_rtx_REG (SImode, REGNO (dest));
8861
8862   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
8863
8864   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
8865   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
8866     {
8867       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
8868       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
8869     }
8870
8871   emit_insn (tmp);
8872 }
8873
8874 /* X is an unchanging MEM.  If it is a constant pool reference, return
8875    the constant pool rtx, else NULL.  */
8876
8877 rtx
8878 maybe_get_pool_constant (rtx x)
8879 {
8880   x = ix86_delegitimize_address (XEXP (x, 0));
8881
8882   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
8883     return get_pool_constant (x);
8884
8885   return NULL_RTX;
8886 }
8887
8888 void
8889 ix86_expand_move (enum machine_mode mode, rtx operands[])
8890 {
8891   int strict = (reload_in_progress || reload_completed);
8892   rtx op0, op1;
8893   enum tls_model model;
8894
8895   op0 = operands[0];
8896   op1 = operands[1];
8897
8898   if (GET_CODE (op1) == SYMBOL_REF)
8899     {
8900       model = SYMBOL_REF_TLS_MODEL (op1);
8901       if (model)
8902         {
8903           op1 = legitimize_tls_address (op1, model, true);
8904           op1 = force_operand (op1, op0);
8905           if (op1 == op0)
8906             return;
8907         }
8908     }
8909   else if (GET_CODE (op1) == CONST
8910            && GET_CODE (XEXP (op1, 0)) == PLUS
8911            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
8912     {
8913       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
8914       if (model)
8915         {
8916           rtx addend = XEXP (XEXP (op1, 0), 1);
8917           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
8918           op1 = force_operand (op1, NULL);
8919           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
8920                                      op0, 1, OPTAB_DIRECT);
8921           if (op1 == op0)
8922             return;
8923         }
8924     }
8925
8926   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
8927     {
8928       if (TARGET_MACHO && !TARGET_64BIT)
8929         {
8930 #if TARGET_MACHO
8931           if (MACHOPIC_PURE)
8932             {
8933               rtx temp = ((reload_in_progress
8934                            || ((op0 && GET_CODE (op0) == REG)
8935                                && mode == Pmode))
8936                           ? op0 : gen_reg_rtx (Pmode));
8937               op1 = machopic_indirect_data_reference (op1, temp);
8938               op1 = machopic_legitimize_pic_address (op1, mode,
8939                                                      temp == op1 ? 0 : temp);
8940             }
8941           else if (MACHOPIC_INDIRECT)
8942             op1 = machopic_indirect_data_reference (op1, 0);
8943           if (op0 == op1)
8944             return;
8945 #endif
8946         }
8947       else
8948         {
8949           if (GET_CODE (op0) == MEM)
8950             op1 = force_reg (Pmode, op1);
8951           else
8952             op1 = legitimize_address (op1, op1, Pmode);
8953         }
8954     }
8955   else
8956     {
8957       if (GET_CODE (op0) == MEM
8958           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
8959               || !push_operand (op0, mode))
8960           && GET_CODE (op1) == MEM)
8961         op1 = force_reg (mode, op1);
8962
8963       if (push_operand (op0, mode)
8964           && ! general_no_elim_operand (op1, mode))
8965         op1 = copy_to_mode_reg (mode, op1);
8966
8967       /* Force large constants in 64bit compilation into register
8968          to get them CSEed.  */
8969       if (TARGET_64BIT && mode == DImode
8970           && immediate_operand (op1, mode)
8971           && !x86_64_zext_immediate_operand (op1, VOIDmode)
8972           && !register_operand (op0, mode)
8973           && optimize && !reload_completed && !reload_in_progress)
8974         op1 = copy_to_mode_reg (mode, op1);
8975
8976       if (FLOAT_MODE_P (mode))
8977         {
8978           /* If we are loading a floating point constant to a register,
8979              force the value to memory now, since we'll get better code
8980              out the back end.  */
8981
8982           if (strict)
8983             ;
8984           else if (GET_CODE (op1) == CONST_DOUBLE)
8985             {
8986               op1 = validize_mem (force_const_mem (mode, op1));
8987               if (!register_operand (op0, mode))
8988                 {
8989                   rtx temp = gen_reg_rtx (mode);
8990                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
8991                   emit_move_insn (op0, temp);
8992                   return;
8993                 }
8994             }
8995         }
8996     }
8997
8998   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
8999 }
9000
9001 void
9002 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
9003 {
9004   rtx op0 = operands[0], op1 = operands[1];
9005
9006   /* Force constants other than zero into memory.  We do not know how
9007      the instructions used to build constants modify the upper 64 bits
9008      of the register, once we have that information we may be able
9009      to handle some of them more efficiently.  */
9010   if ((reload_in_progress | reload_completed) == 0
9011       && register_operand (op0, mode)
9012       && CONSTANT_P (op1)
9013       && standard_sse_constant_p (op1) <= 0)
9014     op1 = validize_mem (force_const_mem (mode, op1));
9015
9016   /* Make operand1 a register if it isn't already.  */
9017   if (!no_new_pseudos
9018       && !register_operand (op0, mode)
9019       && !register_operand (op1, mode))
9020     {
9021       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
9022       return;
9023     }
9024
9025   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
9026 }
9027
9028 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go
9029    straight to ix86_expand_vector_move.  */
9030
9031 void
9032 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
9033 {
9034   rtx op0, op1, m;
9035
9036   op0 = operands[0];
9037   op1 = operands[1];
9038
9039   if (MEM_P (op1))
9040     {
9041       /* If we're optimizing for size, movups is the smallest.  */
9042       if (optimize_size)
9043         {
9044           op0 = gen_lowpart (V4SFmode, op0);
9045           op1 = gen_lowpart (V4SFmode, op1);
9046           emit_insn (gen_sse_movups (op0, op1));
9047           return;
9048         }
9049
9050       /* ??? If we have typed data, then it would appear that using
9051          movdqu is the only way to get unaligned data loaded with
9052          integer type.  */
9053       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9054         {
9055           op0 = gen_lowpart (V16QImode, op0);
9056           op1 = gen_lowpart (V16QImode, op1);
9057           emit_insn (gen_sse2_movdqu (op0, op1));
9058           return;
9059         }
9060
9061       if (TARGET_SSE2 && mode == V2DFmode)
9062         {
9063           rtx zero;
9064
9065           /* When SSE registers are split into halves, we can avoid
9066              writing to the top half twice.  */
9067           if (TARGET_SSE_SPLIT_REGS)
9068             {
9069               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9070               zero = op0;
9071             }
9072           else
9073             {
9074               /* ??? Not sure about the best option for the Intel chips.
9075                  The following would seem to satisfy; the register is
9076                  entirely cleared, breaking the dependency chain.  We
9077                  then store to the upper half, with a dependency depth
9078                  of one.  A rumor has it that Intel recommends two movsd
9079                  followed by an unpacklpd, but this is unconfirmed.  And
9080                  given that the dependency depth of the unpacklpd would
9081                  still be one, I'm not sure why this would be better.  */
9082               zero = CONST0_RTX (V2DFmode);
9083             }
9084
9085           m = adjust_address (op1, DFmode, 0);
9086           emit_insn (gen_sse2_loadlpd (op0, zero, m));
9087           m = adjust_address (op1, DFmode, 8);
9088           emit_insn (gen_sse2_loadhpd (op0, op0, m));
9089         }
9090       else
9091         {
9092           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
9093             emit_move_insn (op0, CONST0_RTX (mode));
9094           else
9095             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
9096
9097           if (mode != V4SFmode)
9098             op0 = gen_lowpart (V4SFmode, op0);
9099           m = adjust_address (op1, V2SFmode, 0);
9100           emit_insn (gen_sse_loadlps (op0, op0, m));
9101           m = adjust_address (op1, V2SFmode, 8);
9102           emit_insn (gen_sse_loadhps (op0, op0, m));
9103         }
9104     }
9105   else if (MEM_P (op0))
9106     {
9107       /* If we're optimizing for size, movups is the smallest.  */
9108       if (optimize_size)
9109         {
9110           op0 = gen_lowpart (V4SFmode, op0);
9111           op1 = gen_lowpart (V4SFmode, op1);
9112           emit_insn (gen_sse_movups (op0, op1));
9113           return;
9114         }
9115
9116       /* ??? Similar to above, only less clear because of quote
9117          typeless stores unquote.  */
9118       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
9119           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
9120         {
9121           op0 = gen_lowpart (V16QImode, op0);
9122           op1 = gen_lowpart (V16QImode, op1);
9123           emit_insn (gen_sse2_movdqu (op0, op1));
9124           return;
9125         }
9126
9127       if (TARGET_SSE2 && mode == V2DFmode)
9128         {
9129           m = adjust_address (op0, DFmode, 0);
9130           emit_insn (gen_sse2_storelpd (m, op1));
9131           m = adjust_address (op0, DFmode, 8);
9132           emit_insn (gen_sse2_storehpd (m, op1));
9133         }
9134       else
9135         {
9136           if (mode != V4SFmode)
9137             op1 = gen_lowpart (V4SFmode, op1);
9138           m = adjust_address (op0, V2SFmode, 0);
9139           emit_insn (gen_sse_storelps (m, op1));
9140           m = adjust_address (op0, V2SFmode, 8);
9141           emit_insn (gen_sse_storehps (m, op1));
9142         }
9143     }
9144   else
9145     gcc_unreachable ();
9146 }
9147
9148 /* Expand a push in MODE.  This is some mode for which we do not support
9149    proper push instructions, at least from the registers that we expect
9150    the value to live in.  */
9151
9152 void
9153 ix86_expand_push (enum machine_mode mode, rtx x)
9154 {
9155   rtx tmp;
9156
9157   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
9158                              GEN_INT (-GET_MODE_SIZE (mode)),
9159                              stack_pointer_rtx, 1, OPTAB_DIRECT);
9160   if (tmp != stack_pointer_rtx)
9161     emit_move_insn (stack_pointer_rtx, tmp);
9162
9163   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
9164   emit_move_insn (tmp, x);
9165 }
9166
9167 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
9168    destination to use for the operation.  If different from the true
9169    destination in operands[0], a copy operation will be required.  */
9170
9171 rtx
9172 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
9173                             rtx operands[])
9174 {
9175   int matching_memory;
9176   rtx src1, src2, dst;
9177
9178   dst = operands[0];
9179   src1 = operands[1];
9180   src2 = operands[2];
9181
9182   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
9183   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9184       && (rtx_equal_p (dst, src2)
9185           || immediate_operand (src1, mode)))
9186     {
9187       rtx temp = src1;
9188       src1 = src2;
9189       src2 = temp;
9190     }
9191
9192   /* If the destination is memory, and we do not have matching source
9193      operands, do things in registers.  */
9194   matching_memory = 0;
9195   if (GET_CODE (dst) == MEM)
9196     {
9197       if (rtx_equal_p (dst, src1))
9198         matching_memory = 1;
9199       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9200                && rtx_equal_p (dst, src2))
9201         matching_memory = 2;
9202       else
9203         dst = gen_reg_rtx (mode);
9204     }
9205
9206   /* Both source operands cannot be in memory.  */
9207   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
9208     {
9209       if (matching_memory != 2)
9210         src2 = force_reg (mode, src2);
9211       else
9212         src1 = force_reg (mode, src1);
9213     }
9214
9215   /* If the operation is not commutable, source 1 cannot be a constant
9216      or non-matching memory.  */
9217   if ((CONSTANT_P (src1)
9218        || (!matching_memory && GET_CODE (src1) == MEM))
9219       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9220     src1 = force_reg (mode, src1);
9221
9222   src1 = operands[1] = src1;
9223   src2 = operands[2] = src2;
9224   return dst;
9225 }
9226
9227 /* Similarly, but assume that the destination has already been
9228    set up properly.  */
9229
9230 void
9231 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
9232                                     enum machine_mode mode, rtx operands[])
9233 {
9234   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
9235   gcc_assert (dst == operands[0]);
9236 }
9237
9238 /* Attempt to expand a binary operator.  Make the expansion closer to the
9239    actual machine, then just general_operand, which will allow 3 separate
9240    memory references (one output, two input) in a single insn.  */
9241
9242 void
9243 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
9244                              rtx operands[])
9245 {
9246   rtx src1, src2, dst, op, clob;
9247
9248   dst = ix86_fixup_binary_operands (code, mode, operands);
9249   src1 = operands[1];
9250   src2 = operands[2];
9251
9252  /* Emit the instruction.  */
9253
9254   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
9255   if (reload_in_progress)
9256     {
9257       /* Reload doesn't know about the flags register, and doesn't know that
9258          it doesn't want to clobber it.  We can only do this with PLUS.  */
9259       gcc_assert (code == PLUS);
9260       emit_insn (op);
9261     }
9262   else
9263     {
9264       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9265       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9266     }
9267
9268   /* Fix up the destination if needed.  */
9269   if (dst != operands[0])
9270     emit_move_insn (operands[0], dst);
9271 }
9272
9273 /* Return TRUE or FALSE depending on whether the binary operator meets the
9274    appropriate constraints.  */
9275
9276 int
9277 ix86_binary_operator_ok (enum rtx_code code,
9278                          enum machine_mode mode ATTRIBUTE_UNUSED,
9279                          rtx operands[3])
9280 {
9281   /* Both source operands cannot be in memory.  */
9282   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
9283     return 0;
9284   /* If the operation is not commutable, source 1 cannot be a constant.  */
9285   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
9286     return 0;
9287   /* If the destination is memory, we must have a matching source operand.  */
9288   if (GET_CODE (operands[0]) == MEM
9289       && ! (rtx_equal_p (operands[0], operands[1])
9290             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
9291                 && rtx_equal_p (operands[0], operands[2]))))
9292     return 0;
9293   /* If the operation is not commutable and the source 1 is memory, we must
9294      have a matching destination.  */
9295   if (GET_CODE (operands[1]) == MEM
9296       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
9297       && ! rtx_equal_p (operands[0], operands[1]))
9298     return 0;
9299   return 1;
9300 }
9301
9302 /* Attempt to expand a unary operator.  Make the expansion closer to the
9303    actual machine, then just general_operand, which will allow 2 separate
9304    memory references (one output, one input) in a single insn.  */
9305
9306 void
9307 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
9308                             rtx operands[])
9309 {
9310   int matching_memory;
9311   rtx src, dst, op, clob;
9312
9313   dst = operands[0];
9314   src = operands[1];
9315
9316   /* If the destination is memory, and we do not have matching source
9317      operands, do things in registers.  */
9318   matching_memory = 0;
9319   if (MEM_P (dst))
9320     {
9321       if (rtx_equal_p (dst, src))
9322         matching_memory = 1;
9323       else
9324         dst = gen_reg_rtx (mode);
9325     }
9326
9327   /* When source operand is memory, destination must match.  */
9328   if (MEM_P (src) && !matching_memory)
9329     src = force_reg (mode, src);
9330
9331   /* Emit the instruction.  */
9332
9333   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
9334   if (reload_in_progress || code == NOT)
9335     {
9336       /* Reload doesn't know about the flags register, and doesn't know that
9337          it doesn't want to clobber it.  */
9338       gcc_assert (code == NOT);
9339       emit_insn (op);
9340     }
9341   else
9342     {
9343       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9344       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
9345     }
9346
9347   /* Fix up the destination if needed.  */
9348   if (dst != operands[0])
9349     emit_move_insn (operands[0], dst);
9350 }
9351
9352 /* Return TRUE or FALSE depending on whether the unary operator meets the
9353    appropriate constraints.  */
9354
9355 int
9356 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
9357                         enum machine_mode mode ATTRIBUTE_UNUSED,
9358                         rtx operands[2] ATTRIBUTE_UNUSED)
9359 {
9360   /* If one of operands is memory, source and destination must match.  */
9361   if ((GET_CODE (operands[0]) == MEM
9362        || GET_CODE (operands[1]) == MEM)
9363       && ! rtx_equal_p (operands[0], operands[1]))
9364     return FALSE;
9365   return TRUE;
9366 }
9367
9368 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
9369    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
9370    true, then replicate the mask for all elements of the vector register.
9371    If INVERT is true, then create a mask excluding the sign bit.  */
9372
9373 rtx
9374 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
9375 {
9376   enum machine_mode vec_mode;
9377   HOST_WIDE_INT hi, lo;
9378   int shift = 63;
9379   rtvec v;
9380   rtx mask;
9381
9382   /* Find the sign bit, sign extended to 2*HWI.  */
9383   if (mode == SFmode)
9384     lo = 0x80000000, hi = lo < 0;
9385   else if (HOST_BITS_PER_WIDE_INT >= 64)
9386     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
9387   else
9388     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
9389
9390   if (invert)
9391     lo = ~lo, hi = ~hi;
9392
9393   /* Force this value into the low part of a fp vector constant.  */
9394   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
9395   mask = gen_lowpart (mode, mask);
9396
9397   if (mode == SFmode)
9398     {
9399       if (vect)
9400         v = gen_rtvec (4, mask, mask, mask, mask);
9401       else
9402         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
9403                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9404       vec_mode = V4SFmode;
9405     }
9406   else
9407     {
9408       if (vect)
9409         v = gen_rtvec (2, mask, mask);
9410       else
9411         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
9412       vec_mode = V2DFmode;
9413     }
9414
9415   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
9416 }
9417
9418 /* Generate code for floating point ABS or NEG.  */
9419
9420 void
9421 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
9422                                 rtx operands[])
9423 {
9424   rtx mask, set, use, clob, dst, src;
9425   bool matching_memory;
9426   bool use_sse = false;
9427   bool vector_mode = VECTOR_MODE_P (mode);
9428   enum machine_mode elt_mode = mode;
9429
9430   if (vector_mode)
9431     {
9432       elt_mode = GET_MODE_INNER (mode);
9433       use_sse = true;
9434     }
9435   else if (TARGET_SSE_MATH)
9436     use_sse = SSE_FLOAT_MODE_P (mode);
9437
9438   /* NEG and ABS performed with SSE use bitwise mask operations.
9439      Create the appropriate mask now.  */
9440   if (use_sse)
9441     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
9442   else
9443     mask = NULL_RTX;
9444
9445   dst = operands[0];
9446   src = operands[1];
9447
9448   /* If the destination is memory, and we don't have matching source
9449      operands or we're using the x87, do things in registers.  */
9450   matching_memory = false;
9451   if (MEM_P (dst))
9452     {
9453       if (use_sse && rtx_equal_p (dst, src))
9454         matching_memory = true;
9455       else
9456         dst = gen_reg_rtx (mode);
9457     }
9458   if (MEM_P (src) && !matching_memory)
9459     src = force_reg (mode, src);
9460
9461   if (vector_mode)
9462     {
9463       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
9464       set = gen_rtx_SET (VOIDmode, dst, set);
9465       emit_insn (set);
9466     }
9467   else
9468     {
9469       set = gen_rtx_fmt_e (code, mode, src);
9470       set = gen_rtx_SET (VOIDmode, dst, set);
9471       if (mask)
9472         {
9473           use = gen_rtx_USE (VOIDmode, mask);
9474           clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
9475           emit_insn (gen_rtx_PARALLEL (VOIDmode,
9476                                        gen_rtvec (3, set, use, clob)));
9477         }
9478       else
9479         emit_insn (set);
9480     }
9481
9482   if (dst != operands[0])
9483     emit_move_insn (operands[0], dst);
9484 }
9485
9486 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
9487
9488 void
9489 ix86_expand_copysign (rtx operands[])
9490 {
9491   enum machine_mode mode, vmode;
9492   rtx dest, op0, op1, mask, nmask;
9493
9494   dest = operands[0];
9495   op0 = operands[1];
9496   op1 = operands[2];
9497
9498   mode = GET_MODE (dest);
9499   vmode = mode == SFmode ? V4SFmode : V2DFmode;
9500
9501   if (GET_CODE (op0) == CONST_DOUBLE)
9502     {
9503       rtvec v;
9504
9505       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
9506         op0 = simplify_unary_operation (ABS, mode, op0, mode);
9507
9508       if (op0 == CONST0_RTX (mode))
9509         op0 = CONST0_RTX (vmode);
9510       else
9511         {
9512           if (mode == SFmode)
9513             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
9514                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
9515           else
9516             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
9517           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
9518         }
9519
9520       mask = ix86_build_signbit_mask (mode, 0, 0);
9521
9522       if (mode == SFmode)
9523         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
9524       else
9525         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
9526     }
9527   else
9528     {
9529       nmask = ix86_build_signbit_mask (mode, 0, 1);
9530       mask = ix86_build_signbit_mask (mode, 0, 0);
9531
9532       if (mode == SFmode)
9533         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
9534       else
9535         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
9536     }
9537 }
9538
9539 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
9540    be a constant, and so has already been expanded into a vector constant.  */
9541
9542 void
9543 ix86_split_copysign_const (rtx operands[])
9544 {
9545   enum machine_mode mode, vmode;
9546   rtx dest, op0, op1, mask, x;
9547
9548   dest = operands[0];
9549   op0 = operands[1];
9550   op1 = operands[2];
9551   mask = operands[3];
9552
9553   mode = GET_MODE (dest);
9554   vmode = GET_MODE (mask);
9555
9556   dest = simplify_gen_subreg (vmode, dest, mode, 0);
9557   x = gen_rtx_AND (vmode, dest, mask);
9558   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9559
9560   if (op0 != CONST0_RTX (vmode))
9561     {
9562       x = gen_rtx_IOR (vmode, dest, op0);
9563       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9564     }
9565 }
9566
9567 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
9568    so we have to do two masks.  */
9569
9570 void
9571 ix86_split_copysign_var (rtx operands[])
9572 {
9573   enum machine_mode mode, vmode;
9574   rtx dest, scratch, op0, op1, mask, nmask, x;
9575
9576   dest = operands[0];
9577   scratch = operands[1];
9578   op0 = operands[2];
9579   op1 = operands[3];
9580   nmask = operands[4];
9581   mask = operands[5];
9582
9583   mode = GET_MODE (dest);
9584   vmode = GET_MODE (mask);
9585
9586   if (rtx_equal_p (op0, op1))
9587     {
9588       /* Shouldn't happen often (it's useless, obviously), but when it does
9589          we'd generate incorrect code if we continue below.  */
9590       emit_move_insn (dest, op0);
9591       return;
9592     }
9593
9594   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
9595     {
9596       gcc_assert (REGNO (op1) == REGNO (scratch));
9597
9598       x = gen_rtx_AND (vmode, scratch, mask);
9599       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9600
9601       dest = mask;
9602       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9603       x = gen_rtx_NOT (vmode, dest);
9604       x = gen_rtx_AND (vmode, x, op0);
9605       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9606     }
9607   else
9608     {
9609       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
9610         {
9611           x = gen_rtx_AND (vmode, scratch, mask);
9612         }
9613       else                                              /* alternative 2,4 */
9614         {
9615           gcc_assert (REGNO (mask) == REGNO (scratch));
9616           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
9617           x = gen_rtx_AND (vmode, scratch, op1);
9618         }
9619       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
9620
9621       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
9622         {
9623           dest = simplify_gen_subreg (vmode, op0, mode, 0);
9624           x = gen_rtx_AND (vmode, dest, nmask);
9625         }
9626       else                                              /* alternative 3,4 */
9627         {
9628           gcc_assert (REGNO (nmask) == REGNO (dest));
9629           dest = nmask;
9630           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
9631           x = gen_rtx_AND (vmode, dest, op0);
9632         }
9633       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9634     }
9635
9636   x = gen_rtx_IOR (vmode, dest, scratch);
9637   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
9638 }
9639
9640 /* Return TRUE or FALSE depending on whether the first SET in INSN
9641    has source and destination with matching CC modes, and that the
9642    CC mode is at least as constrained as REQ_MODE.  */
9643
9644 int
9645 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
9646 {
9647   rtx set;
9648   enum machine_mode set_mode;
9649
9650   set = PATTERN (insn);
9651   if (GET_CODE (set) == PARALLEL)
9652     set = XVECEXP (set, 0, 0);
9653   gcc_assert (GET_CODE (set) == SET);
9654   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
9655
9656   set_mode = GET_MODE (SET_DEST (set));
9657   switch (set_mode)
9658     {
9659     case CCNOmode:
9660       if (req_mode != CCNOmode
9661           && (req_mode != CCmode
9662               || XEXP (SET_SRC (set), 1) != const0_rtx))
9663         return 0;
9664       break;
9665     case CCmode:
9666       if (req_mode == CCGCmode)
9667         return 0;
9668       /* FALLTHRU */
9669     case CCGCmode:
9670       if (req_mode == CCGOCmode || req_mode == CCNOmode)
9671         return 0;
9672       /* FALLTHRU */
9673     case CCGOCmode:
9674       if (req_mode == CCZmode)
9675         return 0;
9676       /* FALLTHRU */
9677     case CCZmode:
9678       break;
9679
9680     default:
9681       gcc_unreachable ();
9682     }
9683
9684   return (GET_MODE (SET_SRC (set)) == set_mode);
9685 }
9686
9687 /* Generate insn patterns to do an integer compare of OPERANDS.  */
9688
9689 static rtx
9690 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
9691 {
9692   enum machine_mode cmpmode;
9693   rtx tmp, flags;
9694
9695   cmpmode = SELECT_CC_MODE (code, op0, op1);
9696   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
9697
9698   /* This is very simple, but making the interface the same as in the
9699      FP case makes the rest of the code easier.  */
9700   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
9701   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
9702
9703   /* Return the test that should be put into the flags user, i.e.
9704      the bcc, scc, or cmov instruction.  */
9705   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
9706 }
9707
9708 /* Figure out whether to use ordered or unordered fp comparisons.
9709    Return the appropriate mode to use.  */
9710
9711 enum machine_mode
9712 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
9713 {
9714   /* ??? In order to make all comparisons reversible, we do all comparisons
9715      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
9716      all forms trapping and nontrapping comparisons, we can make inequality
9717      comparisons trapping again, since it results in better code when using
9718      FCOM based compares.  */
9719   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
9720 }
9721
9722 enum machine_mode
9723 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
9724 {
9725   if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
9726     return ix86_fp_compare_mode (code);
9727   switch (code)
9728     {
9729       /* Only zero flag is needed.  */
9730     case EQ:                    /* ZF=0 */
9731     case NE:                    /* ZF!=0 */
9732       return CCZmode;
9733       /* Codes needing carry flag.  */
9734     case GEU:                   /* CF=0 */
9735     case GTU:                   /* CF=0 & ZF=0 */
9736     case LTU:                   /* CF=1 */
9737     case LEU:                   /* CF=1 | ZF=1 */
9738       return CCmode;
9739       /* Codes possibly doable only with sign flag when
9740          comparing against zero.  */
9741     case GE:                    /* SF=OF   or   SF=0 */
9742     case LT:                    /* SF<>OF  or   SF=1 */
9743       if (op1 == const0_rtx)
9744         return CCGOCmode;
9745       else
9746         /* For other cases Carry flag is not required.  */
9747         return CCGCmode;
9748       /* Codes doable only with sign flag when comparing
9749          against zero, but we miss jump instruction for it
9750          so we need to use relational tests against overflow
9751          that thus needs to be zero.  */
9752     case GT:                    /* ZF=0 & SF=OF */
9753     case LE:                    /* ZF=1 | SF<>OF */
9754       if (op1 == const0_rtx)
9755         return CCNOmode;
9756       else
9757         return CCGCmode;
9758       /* strcmp pattern do (use flags) and combine may ask us for proper
9759          mode.  */
9760     case USE:
9761       return CCmode;
9762     default:
9763       gcc_unreachable ();
9764     }
9765 }
9766
9767 /* Return the fixed registers used for condition codes.  */
9768
9769 static bool
9770 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
9771 {
9772   *p1 = FLAGS_REG;
9773   *p2 = FPSR_REG;
9774   return true;
9775 }
9776
9777 /* If two condition code modes are compatible, return a condition code
9778    mode which is compatible with both.  Otherwise, return
9779    VOIDmode.  */
9780
9781 static enum machine_mode
9782 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
9783 {
9784   if (m1 == m2)
9785     return m1;
9786
9787   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
9788     return VOIDmode;
9789
9790   if ((m1 == CCGCmode && m2 == CCGOCmode)
9791       || (m1 == CCGOCmode && m2 == CCGCmode))
9792     return CCGCmode;
9793
9794   switch (m1)
9795     {
9796     default:
9797       gcc_unreachable ();
9798
9799     case CCmode:
9800     case CCGCmode:
9801     case CCGOCmode:
9802     case CCNOmode:
9803     case CCZmode:
9804       switch (m2)
9805         {
9806         default:
9807           return VOIDmode;
9808
9809         case CCmode:
9810         case CCGCmode:
9811         case CCGOCmode:
9812         case CCNOmode:
9813         case CCZmode:
9814           return CCmode;
9815         }
9816
9817     case CCFPmode:
9818     case CCFPUmode:
9819       /* These are only compatible with themselves, which we already
9820          checked above.  */
9821       return VOIDmode;
9822     }
9823 }
9824
9825 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
9826
9827 int
9828 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
9829 {
9830   enum rtx_code swapped_code = swap_condition (code);
9831   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
9832           || (ix86_fp_comparison_cost (swapped_code)
9833               == ix86_fp_comparison_fcomi_cost (swapped_code)));
9834 }
9835
9836 /* Swap, force into registers, or otherwise massage the two operands
9837    to a fp comparison.  The operands are updated in place; the new
9838    comparison code is returned.  */
9839
9840 static enum rtx_code
9841 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
9842 {
9843   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
9844   rtx op0 = *pop0, op1 = *pop1;
9845   enum machine_mode op_mode = GET_MODE (op0);
9846   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
9847
9848   /* All of the unordered compare instructions only work on registers.
9849      The same is true of the fcomi compare instructions.  The XFmode
9850      compare instructions require registers except when comparing
9851      against zero or when converting operand 1 from fixed point to
9852      floating point.  */
9853
9854   if (!is_sse
9855       && (fpcmp_mode == CCFPUmode
9856           || (op_mode == XFmode
9857               && ! (standard_80387_constant_p (op0) == 1
9858                     || standard_80387_constant_p (op1) == 1)
9859               && GET_CODE (op1) != FLOAT)
9860           || ix86_use_fcomi_compare (code)))
9861     {
9862       op0 = force_reg (op_mode, op0);
9863       op1 = force_reg (op_mode, op1);
9864     }
9865   else
9866     {
9867       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
9868          things around if they appear profitable, otherwise force op0
9869          into a register.  */
9870
9871       if (standard_80387_constant_p (op0) == 0
9872           || (GET_CODE (op0) == MEM
9873               && ! (standard_80387_constant_p (op1) == 0
9874                     || GET_CODE (op1) == MEM)))
9875         {
9876           rtx tmp;
9877           tmp = op0, op0 = op1, op1 = tmp;
9878           code = swap_condition (code);
9879         }
9880
9881       if (GET_CODE (op0) != REG)
9882         op0 = force_reg (op_mode, op0);
9883
9884       if (CONSTANT_P (op1))
9885         {
9886           int tmp = standard_80387_constant_p (op1);
9887           if (tmp == 0)
9888             op1 = validize_mem (force_const_mem (op_mode, op1));
9889           else if (tmp == 1)
9890             {
9891               if (TARGET_CMOVE)
9892                 op1 = force_reg (op_mode, op1);
9893             }
9894           else
9895             op1 = force_reg (op_mode, op1);
9896         }
9897     }
9898
9899   /* Try to rearrange the comparison to make it cheaper.  */
9900   if (ix86_fp_comparison_cost (code)
9901       > ix86_fp_comparison_cost (swap_condition (code))
9902       && (GET_CODE (op1) == REG || !no_new_pseudos))
9903     {
9904       rtx tmp;
9905       tmp = op0, op0 = op1, op1 = tmp;
9906       code = swap_condition (code);
9907       if (GET_CODE (op0) != REG)
9908         op0 = force_reg (op_mode, op0);
9909     }
9910
9911   *pop0 = op0;
9912   *pop1 = op1;
9913   return code;
9914 }
9915
9916 /* Convert comparison codes we use to represent FP comparison to integer
9917    code that will result in proper branch.  Return UNKNOWN if no such code
9918    is available.  */
9919
9920 enum rtx_code
9921 ix86_fp_compare_code_to_integer (enum rtx_code code)
9922 {
9923   switch (code)
9924     {
9925     case GT:
9926       return GTU;
9927     case GE:
9928       return GEU;
9929     case ORDERED:
9930     case UNORDERED:
9931       return code;
9932       break;
9933     case UNEQ:
9934       return EQ;
9935       break;
9936     case UNLT:
9937       return LTU;
9938       break;
9939     case UNLE:
9940       return LEU;
9941       break;
9942     case LTGT:
9943       return NE;
9944       break;
9945     default:
9946       return UNKNOWN;
9947     }
9948 }
9949
9950 /* Split comparison code CODE into comparisons we can do using branch
9951    instructions.  BYPASS_CODE is comparison code for branch that will
9952    branch around FIRST_CODE and SECOND_CODE.  If some of branches
9953    is not required, set value to UNKNOWN.
9954    We never require more than two branches.  */
9955
9956 void
9957 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
9958                           enum rtx_code *first_code,
9959                           enum rtx_code *second_code)
9960 {
9961   *first_code = code;
9962   *bypass_code = UNKNOWN;
9963   *second_code = UNKNOWN;
9964
9965   /* The fcomi comparison sets flags as follows:
9966
9967      cmp    ZF PF CF
9968      >      0  0  0
9969      <      0  0  1
9970      =      1  0  0
9971      un     1  1  1 */
9972
9973   switch (code)
9974     {
9975     case GT:                    /* GTU - CF=0 & ZF=0 */
9976     case GE:                    /* GEU - CF=0 */
9977     case ORDERED:               /* PF=0 */
9978     case UNORDERED:             /* PF=1 */
9979     case UNEQ:                  /* EQ - ZF=1 */
9980     case UNLT:                  /* LTU - CF=1 */
9981     case UNLE:                  /* LEU - CF=1 | ZF=1 */
9982     case LTGT:                  /* EQ - ZF=0 */
9983       break;
9984     case LT:                    /* LTU - CF=1 - fails on unordered */
9985       *first_code = UNLT;
9986       *bypass_code = UNORDERED;
9987       break;
9988     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
9989       *first_code = UNLE;
9990       *bypass_code = UNORDERED;
9991       break;
9992     case EQ:                    /* EQ - ZF=1 - fails on unordered */
9993       *first_code = UNEQ;
9994       *bypass_code = UNORDERED;
9995       break;
9996     case NE:                    /* NE - ZF=0 - fails on unordered */
9997       *first_code = LTGT;
9998       *second_code = UNORDERED;
9999       break;
10000     case UNGE:                  /* GEU - CF=0 - fails on unordered */
10001       *first_code = GE;
10002       *second_code = UNORDERED;
10003       break;
10004     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
10005       *first_code = GT;
10006       *second_code = UNORDERED;
10007       break;
10008     default:
10009       gcc_unreachable ();
10010     }
10011   if (!TARGET_IEEE_FP)
10012     {
10013       *second_code = UNKNOWN;
10014       *bypass_code = UNKNOWN;
10015     }
10016 }
10017
10018 /* Return cost of comparison done fcom + arithmetics operations on AX.
10019    All following functions do use number of instructions as a cost metrics.
10020    In future this should be tweaked to compute bytes for optimize_size and
10021    take into account performance of various instructions on various CPUs.  */
10022 static int
10023 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
10024 {
10025   if (!TARGET_IEEE_FP)
10026     return 4;
10027   /* The cost of code output by ix86_expand_fp_compare.  */
10028   switch (code)
10029     {
10030     case UNLE:
10031     case UNLT:
10032     case LTGT:
10033     case GT:
10034     case GE:
10035     case UNORDERED:
10036     case ORDERED:
10037     case UNEQ:
10038       return 4;
10039       break;
10040     case LT:
10041     case NE:
10042     case EQ:
10043     case UNGE:
10044       return 5;
10045       break;
10046     case LE:
10047     case UNGT:
10048       return 6;
10049       break;
10050     default:
10051       gcc_unreachable ();
10052     }
10053 }
10054
10055 /* Return cost of comparison done using fcomi operation.
10056    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10057 static int
10058 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
10059 {
10060   enum rtx_code bypass_code, first_code, second_code;
10061   /* Return arbitrarily high cost when instruction is not supported - this
10062      prevents gcc from using it.  */
10063   if (!TARGET_CMOVE)
10064     return 1024;
10065   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10066   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
10067 }
10068
10069 /* Return cost of comparison done using sahf operation.
10070    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10071 static int
10072 ix86_fp_comparison_sahf_cost (enum rtx_code code)
10073 {
10074   enum rtx_code bypass_code, first_code, second_code;
10075   /* Return arbitrarily high cost when instruction is not preferred - this
10076      avoids gcc from using it.  */
10077   if (!TARGET_USE_SAHF && !optimize_size)
10078     return 1024;
10079   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10080   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
10081 }
10082
10083 /* Compute cost of the comparison done using any method.
10084    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
10085 static int
10086 ix86_fp_comparison_cost (enum rtx_code code)
10087 {
10088   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
10089   int min;
10090
10091   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
10092   sahf_cost = ix86_fp_comparison_sahf_cost (code);
10093
10094   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
10095   if (min > sahf_cost)
10096     min = sahf_cost;
10097   if (min > fcomi_cost)
10098     min = fcomi_cost;
10099   return min;
10100 }
10101
10102 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
10103
10104 static rtx
10105 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
10106                         rtx *second_test, rtx *bypass_test)
10107 {
10108   enum machine_mode fpcmp_mode, intcmp_mode;
10109   rtx tmp, tmp2;
10110   int cost = ix86_fp_comparison_cost (code);
10111   enum rtx_code bypass_code, first_code, second_code;
10112
10113   fpcmp_mode = ix86_fp_compare_mode (code);
10114   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
10115
10116   if (second_test)
10117     *second_test = NULL_RTX;
10118   if (bypass_test)
10119     *bypass_test = NULL_RTX;
10120
10121   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10122
10123   /* Do fcomi/sahf based test when profitable.  */
10124   if ((bypass_code == UNKNOWN || bypass_test)
10125       && (second_code == UNKNOWN || second_test)
10126       && ix86_fp_comparison_arithmetics_cost (code) > cost)
10127     {
10128       if (TARGET_CMOVE)
10129         {
10130           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10131           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
10132                              tmp);
10133           emit_insn (tmp);
10134         }
10135       else
10136         {
10137           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10138           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10139           if (!scratch)
10140             scratch = gen_reg_rtx (HImode);
10141           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10142           emit_insn (gen_x86_sahf_1 (scratch));
10143         }
10144
10145       /* The FP codes work out to act like unsigned.  */
10146       intcmp_mode = fpcmp_mode;
10147       code = first_code;
10148       if (bypass_code != UNKNOWN)
10149         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
10150                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10151                                        const0_rtx);
10152       if (second_code != UNKNOWN)
10153         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
10154                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
10155                                        const0_rtx);
10156     }
10157   else
10158     {
10159       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
10160       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
10161       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
10162       if (!scratch)
10163         scratch = gen_reg_rtx (HImode);
10164       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
10165
10166       /* In the unordered case, we have to check C2 for NaN's, which
10167          doesn't happen to work out to anything nice combination-wise.
10168          So do some bit twiddling on the value we've got in AH to come
10169          up with an appropriate set of condition codes.  */
10170
10171       intcmp_mode = CCNOmode;
10172       switch (code)
10173         {
10174         case GT:
10175         case UNGT:
10176           if (code == GT || !TARGET_IEEE_FP)
10177             {
10178               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10179               code = EQ;
10180             }
10181           else
10182             {
10183               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10184               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10185               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
10186               intcmp_mode = CCmode;
10187               code = GEU;
10188             }
10189           break;
10190         case LT:
10191         case UNLT:
10192           if (code == LT && TARGET_IEEE_FP)
10193             {
10194               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10195               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
10196               intcmp_mode = CCmode;
10197               code = EQ;
10198             }
10199           else
10200             {
10201               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
10202               code = NE;
10203             }
10204           break;
10205         case GE:
10206         case UNGE:
10207           if (code == GE || !TARGET_IEEE_FP)
10208             {
10209               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
10210               code = EQ;
10211             }
10212           else
10213             {
10214               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10215               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10216                                              GEN_INT (0x01)));
10217               code = NE;
10218             }
10219           break;
10220         case LE:
10221         case UNLE:
10222           if (code == LE && TARGET_IEEE_FP)
10223             {
10224               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10225               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
10226               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10227               intcmp_mode = CCmode;
10228               code = LTU;
10229             }
10230           else
10231             {
10232               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
10233               code = NE;
10234             }
10235           break;
10236         case EQ:
10237         case UNEQ:
10238           if (code == EQ && TARGET_IEEE_FP)
10239             {
10240               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10241               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
10242               intcmp_mode = CCmode;
10243               code = EQ;
10244             }
10245           else
10246             {
10247               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10248               code = NE;
10249               break;
10250             }
10251           break;
10252         case NE:
10253         case LTGT:
10254           if (code == NE && TARGET_IEEE_FP)
10255             {
10256               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
10257               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
10258                                              GEN_INT (0x40)));
10259               code = NE;
10260             }
10261           else
10262             {
10263               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
10264               code = EQ;
10265             }
10266           break;
10267
10268         case UNORDERED:
10269           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10270           code = NE;
10271           break;
10272         case ORDERED:
10273           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
10274           code = EQ;
10275           break;
10276
10277         default:
10278           gcc_unreachable ();
10279         }
10280     }
10281
10282   /* Return the test that should be put into the flags user, i.e.
10283      the bcc, scc, or cmov instruction.  */
10284   return gen_rtx_fmt_ee (code, VOIDmode,
10285                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
10286                          const0_rtx);
10287 }
10288
10289 rtx
10290 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
10291 {
10292   rtx op0, op1, ret;
10293   op0 = ix86_compare_op0;
10294   op1 = ix86_compare_op1;
10295
10296   if (second_test)
10297     *second_test = NULL_RTX;
10298   if (bypass_test)
10299     *bypass_test = NULL_RTX;
10300
10301   if (ix86_compare_emitted)
10302     {
10303       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
10304       ix86_compare_emitted = NULL_RTX;
10305     }
10306   else if (SCALAR_FLOAT_MODE_P (GET_MODE (op0)))
10307     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10308                                   second_test, bypass_test);
10309   else
10310     ret = ix86_expand_int_compare (code, op0, op1);
10311
10312   return ret;
10313 }
10314
10315 /* Return true if the CODE will result in nontrivial jump sequence.  */
10316 bool
10317 ix86_fp_jump_nontrivial_p (enum rtx_code code)
10318 {
10319   enum rtx_code bypass_code, first_code, second_code;
10320   if (!TARGET_CMOVE)
10321     return true;
10322   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10323   return bypass_code != UNKNOWN || second_code != UNKNOWN;
10324 }
10325
10326 void
10327 ix86_expand_branch (enum rtx_code code, rtx label)
10328 {
10329   rtx tmp;
10330
10331   /* If we have emitted a compare insn, go straight to simple.
10332      ix86_expand_compare won't emit anything if ix86_compare_emitted
10333      is non NULL.  */
10334   if (ix86_compare_emitted)
10335     goto simple;
10336
10337   switch (GET_MODE (ix86_compare_op0))
10338     {
10339     case QImode:
10340     case HImode:
10341     case SImode:
10342       simple:
10343       tmp = ix86_expand_compare (code, NULL, NULL);
10344       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10345                                   gen_rtx_LABEL_REF (VOIDmode, label),
10346                                   pc_rtx);
10347       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
10348       return;
10349
10350     case SFmode:
10351     case DFmode:
10352     case XFmode:
10353       {
10354         rtvec vec;
10355         int use_fcomi;
10356         enum rtx_code bypass_code, first_code, second_code;
10357
10358         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
10359                                              &ix86_compare_op1);
10360
10361         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
10362
10363         /* Check whether we will use the natural sequence with one jump.  If
10364            so, we can expand jump early.  Otherwise delay expansion by
10365            creating compound insn to not confuse optimizers.  */
10366         if (bypass_code == UNKNOWN && second_code == UNKNOWN
10367             && TARGET_CMOVE)
10368           {
10369             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
10370                                   gen_rtx_LABEL_REF (VOIDmode, label),
10371                                   pc_rtx, NULL_RTX, NULL_RTX);
10372           }
10373         else
10374           {
10375             tmp = gen_rtx_fmt_ee (code, VOIDmode,
10376                                   ix86_compare_op0, ix86_compare_op1);
10377             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
10378                                         gen_rtx_LABEL_REF (VOIDmode, label),
10379                                         pc_rtx);
10380             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
10381
10382             use_fcomi = ix86_use_fcomi_compare (code);
10383             vec = rtvec_alloc (3 + !use_fcomi);
10384             RTVEC_ELT (vec, 0) = tmp;
10385             RTVEC_ELT (vec, 1)
10386               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
10387             RTVEC_ELT (vec, 2)
10388               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
10389             if (! use_fcomi)
10390               RTVEC_ELT (vec, 3)
10391                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
10392
10393             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
10394           }
10395         return;
10396       }
10397
10398     case DImode:
10399       if (TARGET_64BIT)
10400         goto simple;
10401     case TImode:
10402       /* Expand DImode branch into multiple compare+branch.  */
10403       {
10404         rtx lo[2], hi[2], label2;
10405         enum rtx_code code1, code2, code3;
10406         enum machine_mode submode;
10407
10408         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
10409           {
10410             tmp = ix86_compare_op0;
10411             ix86_compare_op0 = ix86_compare_op1;
10412             ix86_compare_op1 = tmp;
10413             code = swap_condition (code);
10414           }
10415         if (GET_MODE (ix86_compare_op0) == DImode)
10416           {
10417             split_di (&ix86_compare_op0, 1, lo+0, hi+0);
10418             split_di (&ix86_compare_op1, 1, lo+1, hi+1);
10419             submode = SImode;
10420           }
10421         else
10422           {
10423             split_ti (&ix86_compare_op0, 1, lo+0, hi+0);
10424             split_ti (&ix86_compare_op1, 1, lo+1, hi+1);
10425             submode = DImode;
10426           }
10427
10428         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
10429            avoid two branches.  This costs one extra insn, so disable when
10430            optimizing for size.  */
10431
10432         if ((code == EQ || code == NE)
10433             && (!optimize_size
10434                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
10435           {
10436             rtx xor0, xor1;
10437
10438             xor1 = hi[0];
10439             if (hi[1] != const0_rtx)
10440               xor1 = expand_binop (submode, xor_optab, xor1, hi[1],
10441                                    NULL_RTX, 0, OPTAB_WIDEN);
10442
10443             xor0 = lo[0];
10444             if (lo[1] != const0_rtx)
10445               xor0 = expand_binop (submode, xor_optab, xor0, lo[1],
10446                                    NULL_RTX, 0, OPTAB_WIDEN);
10447
10448             tmp = expand_binop (submode, ior_optab, xor1, xor0,
10449                                 NULL_RTX, 0, OPTAB_WIDEN);
10450
10451             ix86_compare_op0 = tmp;
10452             ix86_compare_op1 = const0_rtx;
10453             ix86_expand_branch (code, label);
10454             return;
10455           }
10456
10457         /* Otherwise, if we are doing less-than or greater-or-equal-than,
10458            op1 is a constant and the low word is zero, then we can just
10459            examine the high word.  */
10460
10461         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
10462           switch (code)
10463             {
10464             case LT: case LTU: case GE: case GEU:
10465               ix86_compare_op0 = hi[0];
10466               ix86_compare_op1 = hi[1];
10467               ix86_expand_branch (code, label);
10468               return;
10469             default:
10470               break;
10471             }
10472
10473         /* Otherwise, we need two or three jumps.  */
10474
10475         label2 = gen_label_rtx ();
10476
10477         code1 = code;
10478         code2 = swap_condition (code);
10479         code3 = unsigned_condition (code);
10480
10481         switch (code)
10482           {
10483           case LT: case GT: case LTU: case GTU:
10484             break;
10485
10486           case LE:   code1 = LT;  code2 = GT;  break;
10487           case GE:   code1 = GT;  code2 = LT;  break;
10488           case LEU:  code1 = LTU; code2 = GTU; break;
10489           case GEU:  code1 = GTU; code2 = LTU; break;
10490
10491           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
10492           case NE:   code2 = UNKNOWN; break;
10493
10494           default:
10495             gcc_unreachable ();
10496           }
10497
10498         /*
10499          * a < b =>
10500          *    if (hi(a) < hi(b)) goto true;
10501          *    if (hi(a) > hi(b)) goto false;
10502          *    if (lo(a) < lo(b)) goto true;
10503          *  false:
10504          */
10505
10506         ix86_compare_op0 = hi[0];
10507         ix86_compare_op1 = hi[1];
10508
10509         if (code1 != UNKNOWN)
10510           ix86_expand_branch (code1, label);
10511         if (code2 != UNKNOWN)
10512           ix86_expand_branch (code2, label2);
10513
10514         ix86_compare_op0 = lo[0];
10515         ix86_compare_op1 = lo[1];
10516         ix86_expand_branch (code3, label);
10517
10518         if (code2 != UNKNOWN)
10519           emit_label (label2);
10520         return;
10521       }
10522
10523     default:
10524       gcc_unreachable ();
10525     }
10526 }
10527
10528 /* Split branch based on floating point condition.  */
10529 void
10530 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
10531                       rtx target1, rtx target2, rtx tmp, rtx pushed)
10532 {
10533   rtx second, bypass;
10534   rtx label = NULL_RTX;
10535   rtx condition;
10536   int bypass_probability = -1, second_probability = -1, probability = -1;
10537   rtx i;
10538
10539   if (target2 != pc_rtx)
10540     {
10541       rtx tmp = target2;
10542       code = reverse_condition_maybe_unordered (code);
10543       target2 = target1;
10544       target1 = tmp;
10545     }
10546
10547   condition = ix86_expand_fp_compare (code, op1, op2,
10548                                       tmp, &second, &bypass);
10549
10550   /* Remove pushed operand from stack.  */
10551   if (pushed)
10552     ix86_free_from_memory (GET_MODE (pushed));
10553
10554   if (split_branch_probability >= 0)
10555     {
10556       /* Distribute the probabilities across the jumps.
10557          Assume the BYPASS and SECOND to be always test
10558          for UNORDERED.  */
10559       probability = split_branch_probability;
10560
10561       /* Value of 1 is low enough to make no need for probability
10562          to be updated.  Later we may run some experiments and see
10563          if unordered values are more frequent in practice.  */
10564       if (bypass)
10565         bypass_probability = 1;
10566       if (second)
10567         second_probability = 1;
10568     }
10569   if (bypass != NULL_RTX)
10570     {
10571       label = gen_label_rtx ();
10572       i = emit_jump_insn (gen_rtx_SET
10573                           (VOIDmode, pc_rtx,
10574                            gen_rtx_IF_THEN_ELSE (VOIDmode,
10575                                                  bypass,
10576                                                  gen_rtx_LABEL_REF (VOIDmode,
10577                                                                     label),
10578                                                  pc_rtx)));
10579       if (bypass_probability >= 0)
10580         REG_NOTES (i)
10581           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10582                                GEN_INT (bypass_probability),
10583                                REG_NOTES (i));
10584     }
10585   i = emit_jump_insn (gen_rtx_SET
10586                       (VOIDmode, pc_rtx,
10587                        gen_rtx_IF_THEN_ELSE (VOIDmode,
10588                                              condition, target1, target2)));
10589   if (probability >= 0)
10590     REG_NOTES (i)
10591       = gen_rtx_EXPR_LIST (REG_BR_PROB,
10592                            GEN_INT (probability),
10593                            REG_NOTES (i));
10594   if (second != NULL_RTX)
10595     {
10596       i = emit_jump_insn (gen_rtx_SET
10597                           (VOIDmode, pc_rtx,
10598                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
10599                                                  target2)));
10600       if (second_probability >= 0)
10601         REG_NOTES (i)
10602           = gen_rtx_EXPR_LIST (REG_BR_PROB,
10603                                GEN_INT (second_probability),
10604                                REG_NOTES (i));
10605     }
10606   if (label != NULL_RTX)
10607     emit_label (label);
10608 }
10609
10610 int
10611 ix86_expand_setcc (enum rtx_code code, rtx dest)
10612 {
10613   rtx ret, tmp, tmpreg, equiv;
10614   rtx second_test, bypass_test;
10615
10616   if (GET_MODE (ix86_compare_op0) == (TARGET_64BIT ? TImode : DImode))
10617     return 0; /* FAIL */
10618
10619   gcc_assert (GET_MODE (dest) == QImode);
10620
10621   ret = ix86_expand_compare (code, &second_test, &bypass_test);
10622   PUT_MODE (ret, QImode);
10623
10624   tmp = dest;
10625   tmpreg = dest;
10626
10627   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
10628   if (bypass_test || second_test)
10629     {
10630       rtx test = second_test;
10631       int bypass = 0;
10632       rtx tmp2 = gen_reg_rtx (QImode);
10633       if (bypass_test)
10634         {
10635           gcc_assert (!second_test);
10636           test = bypass_test;
10637           bypass = 1;
10638           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
10639         }
10640       PUT_MODE (test, QImode);
10641       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
10642
10643       if (bypass)
10644         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
10645       else
10646         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
10647     }
10648
10649   /* Attach a REG_EQUAL note describing the comparison result.  */
10650   if (ix86_compare_op0 && ix86_compare_op1)
10651     {
10652       equiv = simplify_gen_relational (code, QImode,
10653                                        GET_MODE (ix86_compare_op0),
10654                                        ix86_compare_op0, ix86_compare_op1);
10655       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
10656     }
10657
10658   return 1; /* DONE */
10659 }
10660
10661 /* Expand comparison setting or clearing carry flag.  Return true when
10662    successful and set pop for the operation.  */
10663 static bool
10664 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
10665 {
10666   enum machine_mode mode =
10667     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
10668
10669   /* Do not handle DImode compares that go through special path.  Also we can't
10670      deal with FP compares yet.  This is possible to add.  */
10671   if (mode == (TARGET_64BIT ? TImode : DImode))
10672     return false;
10673   if (FLOAT_MODE_P (mode))
10674     {
10675       rtx second_test = NULL, bypass_test = NULL;
10676       rtx compare_op, compare_seq;
10677
10678       /* Shortcut:  following common codes never translate into carry flag compares.  */
10679       if (code == EQ || code == NE || code == UNEQ || code == LTGT
10680           || code == ORDERED || code == UNORDERED)
10681         return false;
10682
10683       /* These comparisons require zero flag; swap operands so they won't.  */
10684       if ((code == GT || code == UNLE || code == LE || code == UNGT)
10685           && !TARGET_IEEE_FP)
10686         {
10687           rtx tmp = op0;
10688           op0 = op1;
10689           op1 = tmp;
10690           code = swap_condition (code);
10691         }
10692
10693       /* Try to expand the comparison and verify that we end up with carry flag
10694          based comparison.  This is fails to be true only when we decide to expand
10695          comparison using arithmetic that is not too common scenario.  */
10696       start_sequence ();
10697       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
10698                                            &second_test, &bypass_test);
10699       compare_seq = get_insns ();
10700       end_sequence ();
10701
10702       if (second_test || bypass_test)
10703         return false;
10704       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10705           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10706         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
10707       else
10708         code = GET_CODE (compare_op);
10709       if (code != LTU && code != GEU)
10710         return false;
10711       emit_insn (compare_seq);
10712       *pop = compare_op;
10713       return true;
10714     }
10715   if (!INTEGRAL_MODE_P (mode))
10716     return false;
10717   switch (code)
10718     {
10719     case LTU:
10720     case GEU:
10721       break;
10722
10723     /* Convert a==0 into (unsigned)a<1.  */
10724     case EQ:
10725     case NE:
10726       if (op1 != const0_rtx)
10727         return false;
10728       op1 = const1_rtx;
10729       code = (code == EQ ? LTU : GEU);
10730       break;
10731
10732     /* Convert a>b into b<a or a>=b-1.  */
10733     case GTU:
10734     case LEU:
10735       if (GET_CODE (op1) == CONST_INT)
10736         {
10737           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
10738           /* Bail out on overflow.  We still can swap operands but that
10739              would force loading of the constant into register.  */
10740           if (op1 == const0_rtx
10741               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
10742             return false;
10743           code = (code == GTU ? GEU : LTU);
10744         }
10745       else
10746         {
10747           rtx tmp = op1;
10748           op1 = op0;
10749           op0 = tmp;
10750           code = (code == GTU ? LTU : GEU);
10751         }
10752       break;
10753
10754     /* Convert a>=0 into (unsigned)a<0x80000000.  */
10755     case LT:
10756     case GE:
10757       if (mode == DImode || op1 != const0_rtx)
10758         return false;
10759       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10760       code = (code == LT ? GEU : LTU);
10761       break;
10762     case LE:
10763     case GT:
10764       if (mode == DImode || op1 != constm1_rtx)
10765         return false;
10766       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
10767       code = (code == LE ? GEU : LTU);
10768       break;
10769
10770     default:
10771       return false;
10772     }
10773   /* Swapping operands may cause constant to appear as first operand.  */
10774   if (!nonimmediate_operand (op0, VOIDmode))
10775     {
10776       if (no_new_pseudos)
10777         return false;
10778       op0 = force_reg (mode, op0);
10779     }
10780   ix86_compare_op0 = op0;
10781   ix86_compare_op1 = op1;
10782   *pop = ix86_expand_compare (code, NULL, NULL);
10783   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
10784   return true;
10785 }
10786
10787 int
10788 ix86_expand_int_movcc (rtx operands[])
10789 {
10790   enum rtx_code code = GET_CODE (operands[1]), compare_code;
10791   rtx compare_seq, compare_op;
10792   rtx second_test, bypass_test;
10793   enum machine_mode mode = GET_MODE (operands[0]);
10794   bool sign_bit_compare_p = false;;
10795
10796   start_sequence ();
10797   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10798   compare_seq = get_insns ();
10799   end_sequence ();
10800
10801   compare_code = GET_CODE (compare_op);
10802
10803   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
10804       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
10805     sign_bit_compare_p = true;
10806
10807   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
10808      HImode insns, we'd be swallowed in word prefix ops.  */
10809
10810   if ((mode != HImode || TARGET_FAST_PREFIX)
10811       && (mode != (TARGET_64BIT ? TImode : DImode))
10812       && GET_CODE (operands[2]) == CONST_INT
10813       && GET_CODE (operands[3]) == CONST_INT)
10814     {
10815       rtx out = operands[0];
10816       HOST_WIDE_INT ct = INTVAL (operands[2]);
10817       HOST_WIDE_INT cf = INTVAL (operands[3]);
10818       HOST_WIDE_INT diff;
10819
10820       diff = ct - cf;
10821       /*  Sign bit compares are better done using shifts than we do by using
10822           sbb.  */
10823       if (sign_bit_compare_p
10824           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10825                                              ix86_compare_op1, &compare_op))
10826         {
10827           /* Detect overlap between destination and compare sources.  */
10828           rtx tmp = out;
10829
10830           if (!sign_bit_compare_p)
10831             {
10832               bool fpcmp = false;
10833
10834               compare_code = GET_CODE (compare_op);
10835
10836               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10837                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10838                 {
10839                   fpcmp = true;
10840                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
10841                 }
10842
10843               /* To simplify rest of code, restrict to the GEU case.  */
10844               if (compare_code == LTU)
10845                 {
10846                   HOST_WIDE_INT tmp = ct;
10847                   ct = cf;
10848                   cf = tmp;
10849                   compare_code = reverse_condition (compare_code);
10850                   code = reverse_condition (code);
10851                 }
10852               else
10853                 {
10854                   if (fpcmp)
10855                     PUT_CODE (compare_op,
10856                               reverse_condition_maybe_unordered
10857                                 (GET_CODE (compare_op)));
10858                   else
10859                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10860                 }
10861               diff = ct - cf;
10862
10863               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
10864                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
10865                 tmp = gen_reg_rtx (mode);
10866
10867               if (mode == DImode)
10868                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
10869               else
10870                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
10871             }
10872           else
10873             {
10874               if (code == GT || code == GE)
10875                 code = reverse_condition (code);
10876               else
10877                 {
10878                   HOST_WIDE_INT tmp = ct;
10879                   ct = cf;
10880                   cf = tmp;
10881                   diff = ct - cf;
10882                 }
10883               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
10884                                      ix86_compare_op1, VOIDmode, 0, -1);
10885             }
10886
10887           if (diff == 1)
10888             {
10889               /*
10890                * cmpl op0,op1
10891                * sbbl dest,dest
10892                * [addl dest, ct]
10893                *
10894                * Size 5 - 8.
10895                */
10896               if (ct)
10897                 tmp = expand_simple_binop (mode, PLUS,
10898                                            tmp, GEN_INT (ct),
10899                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10900             }
10901           else if (cf == -1)
10902             {
10903               /*
10904                * cmpl op0,op1
10905                * sbbl dest,dest
10906                * orl $ct, dest
10907                *
10908                * Size 8.
10909                */
10910               tmp = expand_simple_binop (mode, IOR,
10911                                          tmp, GEN_INT (ct),
10912                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10913             }
10914           else if (diff == -1 && ct)
10915             {
10916               /*
10917                * cmpl op0,op1
10918                * sbbl dest,dest
10919                * notl dest
10920                * [addl dest, cf]
10921                *
10922                * Size 8 - 11.
10923                */
10924               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10925               if (cf)
10926                 tmp = expand_simple_binop (mode, PLUS,
10927                                            copy_rtx (tmp), GEN_INT (cf),
10928                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10929             }
10930           else
10931             {
10932               /*
10933                * cmpl op0,op1
10934                * sbbl dest,dest
10935                * [notl dest]
10936                * andl cf - ct, dest
10937                * [addl dest, ct]
10938                *
10939                * Size 8 - 11.
10940                */
10941
10942               if (cf == 0)
10943                 {
10944                   cf = ct;
10945                   ct = 0;
10946                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
10947                 }
10948
10949               tmp = expand_simple_binop (mode, AND,
10950                                          copy_rtx (tmp),
10951                                          gen_int_mode (cf - ct, mode),
10952                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
10953               if (ct)
10954                 tmp = expand_simple_binop (mode, PLUS,
10955                                            copy_rtx (tmp), GEN_INT (ct),
10956                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
10957             }
10958
10959           if (!rtx_equal_p (tmp, out))
10960             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
10961
10962           return 1; /* DONE */
10963         }
10964
10965       if (diff < 0)
10966         {
10967           HOST_WIDE_INT tmp;
10968           tmp = ct, ct = cf, cf = tmp;
10969           diff = -diff;
10970           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
10971             {
10972               /* We may be reversing unordered compare to normal compare, that
10973                  is not valid in general (we may convert non-trapping condition
10974                  to trapping one), however on i386 we currently emit all
10975                  comparisons unordered.  */
10976               compare_code = reverse_condition_maybe_unordered (compare_code);
10977               code = reverse_condition_maybe_unordered (code);
10978             }
10979           else
10980             {
10981               compare_code = reverse_condition (compare_code);
10982               code = reverse_condition (code);
10983             }
10984         }
10985
10986       compare_code = UNKNOWN;
10987       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
10988           && GET_CODE (ix86_compare_op1) == CONST_INT)
10989         {
10990           if (ix86_compare_op1 == const0_rtx
10991               && (code == LT || code == GE))
10992             compare_code = code;
10993           else if (ix86_compare_op1 == constm1_rtx)
10994             {
10995               if (code == LE)
10996                 compare_code = LT;
10997               else if (code == GT)
10998                 compare_code = GE;
10999             }
11000         }
11001
11002       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
11003       if (compare_code != UNKNOWN
11004           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
11005           && (cf == -1 || ct == -1))
11006         {
11007           /* If lea code below could be used, only optimize
11008              if it results in a 2 insn sequence.  */
11009
11010           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
11011                  || diff == 3 || diff == 5 || diff == 9)
11012               || (compare_code == LT && ct == -1)
11013               || (compare_code == GE && cf == -1))
11014             {
11015               /*
11016                * notl op1       (if necessary)
11017                * sarl $31, op1
11018                * orl cf, op1
11019                */
11020               if (ct != -1)
11021                 {
11022                   cf = ct;
11023                   ct = -1;
11024                   code = reverse_condition (code);
11025                 }
11026
11027               out = emit_store_flag (out, code, ix86_compare_op0,
11028                                      ix86_compare_op1, VOIDmode, 0, -1);
11029
11030               out = expand_simple_binop (mode, IOR,
11031                                          out, GEN_INT (cf),
11032                                          out, 1, OPTAB_DIRECT);
11033               if (out != operands[0])
11034                 emit_move_insn (operands[0], out);
11035
11036               return 1; /* DONE */
11037             }
11038         }
11039
11040
11041       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
11042            || diff == 3 || diff == 5 || diff == 9)
11043           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
11044           && (mode != DImode
11045               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
11046         {
11047           /*
11048            * xorl dest,dest
11049            * cmpl op1,op2
11050            * setcc dest
11051            * lea cf(dest*(ct-cf)),dest
11052            *
11053            * Size 14.
11054            *
11055            * This also catches the degenerate setcc-only case.
11056            */
11057
11058           rtx tmp;
11059           int nops;
11060
11061           out = emit_store_flag (out, code, ix86_compare_op0,
11062                                  ix86_compare_op1, VOIDmode, 0, 1);
11063
11064           nops = 0;
11065           /* On x86_64 the lea instruction operates on Pmode, so we need
11066              to get arithmetics done in proper mode to match.  */
11067           if (diff == 1)
11068             tmp = copy_rtx (out);
11069           else
11070             {
11071               rtx out1;
11072               out1 = copy_rtx (out);
11073               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
11074               nops++;
11075               if (diff & 1)
11076                 {
11077                   tmp = gen_rtx_PLUS (mode, tmp, out1);
11078                   nops++;
11079                 }
11080             }
11081           if (cf != 0)
11082             {
11083               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
11084               nops++;
11085             }
11086           if (!rtx_equal_p (tmp, out))
11087             {
11088               if (nops == 1)
11089                 out = force_operand (tmp, copy_rtx (out));
11090               else
11091                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
11092             }
11093           if (!rtx_equal_p (out, operands[0]))
11094             emit_move_insn (operands[0], copy_rtx (out));
11095
11096           return 1; /* DONE */
11097         }
11098
11099       /*
11100        * General case:                  Jumpful:
11101        *   xorl dest,dest               cmpl op1, op2
11102        *   cmpl op1, op2                movl ct, dest
11103        *   setcc dest                   jcc 1f
11104        *   decl dest                    movl cf, dest
11105        *   andl (cf-ct),dest            1:
11106        *   addl ct,dest
11107        *
11108        * Size 20.                       Size 14.
11109        *
11110        * This is reasonably steep, but branch mispredict costs are
11111        * high on modern cpus, so consider failing only if optimizing
11112        * for space.
11113        */
11114
11115       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11116           && BRANCH_COST >= 2)
11117         {
11118           if (cf == 0)
11119             {
11120               cf = ct;
11121               ct = 0;
11122               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
11123                 /* We may be reversing unordered compare to normal compare,
11124                    that is not valid in general (we may convert non-trapping
11125                    condition to trapping one), however on i386 we currently
11126                    emit all comparisons unordered.  */
11127                 code = reverse_condition_maybe_unordered (code);
11128               else
11129                 {
11130                   code = reverse_condition (code);
11131                   if (compare_code != UNKNOWN)
11132                     compare_code = reverse_condition (compare_code);
11133                 }
11134             }
11135
11136           if (compare_code != UNKNOWN)
11137             {
11138               /* notl op1       (if needed)
11139                  sarl $31, op1
11140                  andl (cf-ct), op1
11141                  addl ct, op1
11142
11143                  For x < 0 (resp. x <= -1) there will be no notl,
11144                  so if possible swap the constants to get rid of the
11145                  complement.
11146                  True/false will be -1/0 while code below (store flag
11147                  followed by decrement) is 0/-1, so the constants need
11148                  to be exchanged once more.  */
11149
11150               if (compare_code == GE || !cf)
11151                 {
11152                   code = reverse_condition (code);
11153                   compare_code = LT;
11154                 }
11155               else
11156                 {
11157                   HOST_WIDE_INT tmp = cf;
11158                   cf = ct;
11159                   ct = tmp;
11160                 }
11161
11162               out = emit_store_flag (out, code, ix86_compare_op0,
11163                                      ix86_compare_op1, VOIDmode, 0, -1);
11164             }
11165           else
11166             {
11167               out = emit_store_flag (out, code, ix86_compare_op0,
11168                                      ix86_compare_op1, VOIDmode, 0, 1);
11169
11170               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
11171                                          copy_rtx (out), 1, OPTAB_DIRECT);
11172             }
11173
11174           out = expand_simple_binop (mode, AND, copy_rtx (out),
11175                                      gen_int_mode (cf - ct, mode),
11176                                      copy_rtx (out), 1, OPTAB_DIRECT);
11177           if (ct)
11178             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
11179                                        copy_rtx (out), 1, OPTAB_DIRECT);
11180           if (!rtx_equal_p (out, operands[0]))
11181             emit_move_insn (operands[0], copy_rtx (out));
11182
11183           return 1; /* DONE */
11184         }
11185     }
11186
11187   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
11188     {
11189       /* Try a few things more with specific constants and a variable.  */
11190
11191       optab op;
11192       rtx var, orig_out, out, tmp;
11193
11194       if (BRANCH_COST <= 2)
11195         return 0; /* FAIL */
11196
11197       /* If one of the two operands is an interesting constant, load a
11198          constant with the above and mask it in with a logical operation.  */
11199
11200       if (GET_CODE (operands[2]) == CONST_INT)
11201         {
11202           var = operands[3];
11203           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
11204             operands[3] = constm1_rtx, op = and_optab;
11205           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
11206             operands[3] = const0_rtx, op = ior_optab;
11207           else
11208             return 0; /* FAIL */
11209         }
11210       else if (GET_CODE (operands[3]) == CONST_INT)
11211         {
11212           var = operands[2];
11213           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
11214             operands[2] = constm1_rtx, op = and_optab;
11215           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
11216             operands[2] = const0_rtx, op = ior_optab;
11217           else
11218             return 0; /* FAIL */
11219         }
11220       else
11221         return 0; /* FAIL */
11222
11223       orig_out = operands[0];
11224       tmp = gen_reg_rtx (mode);
11225       operands[0] = tmp;
11226
11227       /* Recurse to get the constant loaded.  */
11228       if (ix86_expand_int_movcc (operands) == 0)
11229         return 0; /* FAIL */
11230
11231       /* Mask in the interesting variable.  */
11232       out = expand_binop (mode, op, var, tmp, orig_out, 0,
11233                           OPTAB_WIDEN);
11234       if (!rtx_equal_p (out, orig_out))
11235         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
11236
11237       return 1; /* DONE */
11238     }
11239
11240   /*
11241    * For comparison with above,
11242    *
11243    * movl cf,dest
11244    * movl ct,tmp
11245    * cmpl op1,op2
11246    * cmovcc tmp,dest
11247    *
11248    * Size 15.
11249    */
11250
11251   if (! nonimmediate_operand (operands[2], mode))
11252     operands[2] = force_reg (mode, operands[2]);
11253   if (! nonimmediate_operand (operands[3], mode))
11254     operands[3] = force_reg (mode, operands[3]);
11255
11256   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11257     {
11258       rtx tmp = gen_reg_rtx (mode);
11259       emit_move_insn (tmp, operands[3]);
11260       operands[3] = tmp;
11261     }
11262   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11263     {
11264       rtx tmp = gen_reg_rtx (mode);
11265       emit_move_insn (tmp, operands[2]);
11266       operands[2] = tmp;
11267     }
11268
11269   if (! register_operand (operands[2], VOIDmode)
11270       && (mode == QImode
11271           || ! register_operand (operands[3], VOIDmode)))
11272     operands[2] = force_reg (mode, operands[2]);
11273
11274   if (mode == QImode
11275       && ! register_operand (operands[3], VOIDmode))
11276     operands[3] = force_reg (mode, operands[3]);
11277
11278   emit_insn (compare_seq);
11279   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11280                           gen_rtx_IF_THEN_ELSE (mode,
11281                                                 compare_op, operands[2],
11282                                                 operands[3])));
11283   if (bypass_test)
11284     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11285                             gen_rtx_IF_THEN_ELSE (mode,
11286                                   bypass_test,
11287                                   copy_rtx (operands[3]),
11288                                   copy_rtx (operands[0]))));
11289   if (second_test)
11290     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
11291                             gen_rtx_IF_THEN_ELSE (mode,
11292                                   second_test,
11293                                   copy_rtx (operands[2]),
11294                                   copy_rtx (operands[0]))));
11295
11296   return 1; /* DONE */
11297 }
11298
11299 /* Swap, force into registers, or otherwise massage the two operands
11300    to an sse comparison with a mask result.  Thus we differ a bit from
11301    ix86_prepare_fp_compare_args which expects to produce a flags result.
11302
11303    The DEST operand exists to help determine whether to commute commutative
11304    operators.  The POP0/POP1 operands are updated in place.  The new
11305    comparison code is returned, or UNKNOWN if not implementable.  */
11306
11307 static enum rtx_code
11308 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
11309                                   rtx *pop0, rtx *pop1)
11310 {
11311   rtx tmp;
11312
11313   switch (code)
11314     {
11315     case LTGT:
11316     case UNEQ:
11317       /* We have no LTGT as an operator.  We could implement it with
11318          NE & ORDERED, but this requires an extra temporary.  It's
11319          not clear that it's worth it.  */
11320       return UNKNOWN;
11321
11322     case LT:
11323     case LE:
11324     case UNGT:
11325     case UNGE:
11326       /* These are supported directly.  */
11327       break;
11328
11329     case EQ:
11330     case NE:
11331     case UNORDERED:
11332     case ORDERED:
11333       /* For commutative operators, try to canonicalize the destination
11334          operand to be first in the comparison - this helps reload to
11335          avoid extra moves.  */
11336       if (!dest || !rtx_equal_p (dest, *pop1))
11337         break;
11338       /* FALLTHRU */
11339
11340     case GE:
11341     case GT:
11342     case UNLE:
11343     case UNLT:
11344       /* These are not supported directly.  Swap the comparison operands
11345          to transform into something that is supported.  */
11346       tmp = *pop0;
11347       *pop0 = *pop1;
11348       *pop1 = tmp;
11349       code = swap_condition (code);
11350       break;
11351
11352     default:
11353       gcc_unreachable ();
11354     }
11355
11356   return code;
11357 }
11358
11359 /* Detect conditional moves that exactly match min/max operational
11360    semantics.  Note that this is IEEE safe, as long as we don't
11361    interchange the operands.
11362
11363    Returns FALSE if this conditional move doesn't match a MIN/MAX,
11364    and TRUE if the operation is successful and instructions are emitted.  */
11365
11366 static bool
11367 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
11368                            rtx cmp_op1, rtx if_true, rtx if_false)
11369 {
11370   enum machine_mode mode;
11371   bool is_min;
11372   rtx tmp;
11373
11374   if (code == LT)
11375     ;
11376   else if (code == UNGE)
11377     {
11378       tmp = if_true;
11379       if_true = if_false;
11380       if_false = tmp;
11381     }
11382   else
11383     return false;
11384
11385   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
11386     is_min = true;
11387   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
11388     is_min = false;
11389   else
11390     return false;
11391
11392   mode = GET_MODE (dest);
11393
11394   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
11395      but MODE may be a vector mode and thus not appropriate.  */
11396   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
11397     {
11398       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
11399       rtvec v;
11400
11401       if_true = force_reg (mode, if_true);
11402       v = gen_rtvec (2, if_true, if_false);
11403       tmp = gen_rtx_UNSPEC (mode, v, u);
11404     }
11405   else
11406     {
11407       code = is_min ? SMIN : SMAX;
11408       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
11409     }
11410
11411   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
11412   return true;
11413 }
11414
11415 /* Expand an sse vector comparison.  Return the register with the result.  */
11416
11417 static rtx
11418 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
11419                      rtx op_true, rtx op_false)
11420 {
11421   enum machine_mode mode = GET_MODE (dest);
11422   rtx x;
11423
11424   cmp_op0 = force_reg (mode, cmp_op0);
11425   if (!nonimmediate_operand (cmp_op1, mode))
11426     cmp_op1 = force_reg (mode, cmp_op1);
11427
11428   if (optimize
11429       || reg_overlap_mentioned_p (dest, op_true)
11430       || reg_overlap_mentioned_p (dest, op_false))
11431     dest = gen_reg_rtx (mode);
11432
11433   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
11434   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11435
11436   return dest;
11437 }
11438
11439 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
11440    operations.  This is used for both scalar and vector conditional moves.  */
11441
11442 static void
11443 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
11444 {
11445   enum machine_mode mode = GET_MODE (dest);
11446   rtx t2, t3, x;
11447
11448   if (op_false == CONST0_RTX (mode))
11449     {
11450       op_true = force_reg (mode, op_true);
11451       x = gen_rtx_AND (mode, cmp, op_true);
11452       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11453     }
11454   else if (op_true == CONST0_RTX (mode))
11455     {
11456       op_false = force_reg (mode, op_false);
11457       x = gen_rtx_NOT (mode, cmp);
11458       x = gen_rtx_AND (mode, x, op_false);
11459       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11460     }
11461   else
11462     {
11463       op_true = force_reg (mode, op_true);
11464       op_false = force_reg (mode, op_false);
11465
11466       t2 = gen_reg_rtx (mode);
11467       if (optimize)
11468         t3 = gen_reg_rtx (mode);
11469       else
11470         t3 = dest;
11471
11472       x = gen_rtx_AND (mode, op_true, cmp);
11473       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
11474
11475       x = gen_rtx_NOT (mode, cmp);
11476       x = gen_rtx_AND (mode, x, op_false);
11477       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
11478
11479       x = gen_rtx_IOR (mode, t3, t2);
11480       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
11481     }
11482 }
11483
11484 /* Expand a floating-point conditional move.  Return true if successful.  */
11485
11486 int
11487 ix86_expand_fp_movcc (rtx operands[])
11488 {
11489   enum machine_mode mode = GET_MODE (operands[0]);
11490   enum rtx_code code = GET_CODE (operands[1]);
11491   rtx tmp, compare_op, second_test, bypass_test;
11492
11493   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
11494     {
11495       enum machine_mode cmode;
11496
11497       /* Since we've no cmove for sse registers, don't force bad register
11498          allocation just to gain access to it.  Deny movcc when the
11499          comparison mode doesn't match the move mode.  */
11500       cmode = GET_MODE (ix86_compare_op0);
11501       if (cmode == VOIDmode)
11502         cmode = GET_MODE (ix86_compare_op1);
11503       if (cmode != mode)
11504         return 0;
11505
11506       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11507                                                &ix86_compare_op0,
11508                                                &ix86_compare_op1);
11509       if (code == UNKNOWN)
11510         return 0;
11511
11512       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
11513                                      ix86_compare_op1, operands[2],
11514                                      operands[3]))
11515         return 1;
11516
11517       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
11518                                  ix86_compare_op1, operands[2], operands[3]);
11519       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
11520       return 1;
11521     }
11522
11523   /* The floating point conditional move instructions don't directly
11524      support conditions resulting from a signed integer comparison.  */
11525
11526   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
11527
11528   /* The floating point conditional move instructions don't directly
11529      support signed integer comparisons.  */
11530
11531   if (!fcmov_comparison_operator (compare_op, VOIDmode))
11532     {
11533       gcc_assert (!second_test && !bypass_test);
11534       tmp = gen_reg_rtx (QImode);
11535       ix86_expand_setcc (code, tmp);
11536       code = NE;
11537       ix86_compare_op0 = tmp;
11538       ix86_compare_op1 = const0_rtx;
11539       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
11540     }
11541   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
11542     {
11543       tmp = gen_reg_rtx (mode);
11544       emit_move_insn (tmp, operands[3]);
11545       operands[3] = tmp;
11546     }
11547   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
11548     {
11549       tmp = gen_reg_rtx (mode);
11550       emit_move_insn (tmp, operands[2]);
11551       operands[2] = tmp;
11552     }
11553
11554   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11555                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
11556                                                 operands[2], operands[3])));
11557   if (bypass_test)
11558     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11559                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
11560                                                   operands[3], operands[0])));
11561   if (second_test)
11562     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
11563                             gen_rtx_IF_THEN_ELSE (mode, second_test,
11564                                                   operands[2], operands[0])));
11565
11566   return 1;
11567 }
11568
11569 /* Expand a floating-point vector conditional move; a vcond operation
11570    rather than a movcc operation.  */
11571
11572 bool
11573 ix86_expand_fp_vcond (rtx operands[])
11574 {
11575   enum rtx_code code = GET_CODE (operands[3]);
11576   rtx cmp;
11577
11578   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
11579                                            &operands[4], &operands[5]);
11580   if (code == UNKNOWN)
11581     return false;
11582
11583   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
11584                                  operands[5], operands[1], operands[2]))
11585     return true;
11586
11587   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
11588                              operands[1], operands[2]);
11589   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
11590   return true;
11591 }
11592
11593 /* Expand a signed integral vector conditional move.  */
11594
11595 bool
11596 ix86_expand_int_vcond (rtx operands[])
11597 {
11598   enum machine_mode mode = GET_MODE (operands[0]);
11599   enum rtx_code code = GET_CODE (operands[3]);
11600   bool negate = false;
11601   rtx x, cop0, cop1;
11602
11603   cop0 = operands[4];
11604   cop1 = operands[5];
11605
11606   /* Canonicalize the comparison to EQ, GT, GTU.  */
11607   switch (code)
11608     {
11609     case EQ:
11610     case GT:
11611     case GTU:
11612       break;
11613
11614     case NE:
11615     case LE:
11616     case LEU:
11617       code = reverse_condition (code);
11618       negate = true;
11619       break;
11620
11621     case GE:
11622     case GEU:
11623       code = reverse_condition (code);
11624       negate = true;
11625       /* FALLTHRU */
11626
11627     case LT:
11628     case LTU:
11629       code = swap_condition (code);
11630       x = cop0, cop0 = cop1, cop1 = x;
11631       break;
11632
11633     default:
11634       gcc_unreachable ();
11635     }
11636
11637   /* Unsigned parallel compare is not supported by the hardware.  Play some
11638      tricks to turn this into a signed comparison against 0.  */
11639   if (code == GTU)
11640     {
11641       cop0 = force_reg (mode, cop0);
11642
11643       switch (mode)
11644         {
11645         case V4SImode:
11646           {
11647             rtx t1, t2, mask;
11648
11649             /* Perform a parallel modulo subtraction.  */
11650             t1 = gen_reg_rtx (mode);
11651             emit_insn (gen_subv4si3 (t1, cop0, cop1));
11652
11653             /* Extract the original sign bit of op0.  */
11654             mask = GEN_INT (-0x80000000);
11655             mask = gen_rtx_CONST_VECTOR (mode,
11656                         gen_rtvec (4, mask, mask, mask, mask));
11657             mask = force_reg (mode, mask);
11658             t2 = gen_reg_rtx (mode);
11659             emit_insn (gen_andv4si3 (t2, cop0, mask));
11660
11661             /* XOR it back into the result of the subtraction.  This results
11662                in the sign bit set iff we saw unsigned underflow.  */
11663             x = gen_reg_rtx (mode);
11664             emit_insn (gen_xorv4si3 (x, t1, t2));
11665
11666             code = GT;
11667           }
11668           break;
11669
11670         case V16QImode:
11671         case V8HImode:
11672           /* Perform a parallel unsigned saturating subtraction.  */
11673           x = gen_reg_rtx (mode);
11674           emit_insn (gen_rtx_SET (VOIDmode, x,
11675                                   gen_rtx_US_MINUS (mode, cop0, cop1)));
11676
11677           code = EQ;
11678           negate = !negate;
11679           break;
11680
11681         default:
11682           gcc_unreachable ();
11683         }
11684
11685       cop0 = x;
11686       cop1 = CONST0_RTX (mode);
11687     }
11688
11689   x = ix86_expand_sse_cmp (operands[0], code, cop0, cop1,
11690                            operands[1+negate], operands[2-negate]);
11691
11692   ix86_expand_sse_movcc (operands[0], x, operands[1+negate],
11693                          operands[2-negate]);
11694   return true;
11695 }
11696
11697 /* Expand conditional increment or decrement using adb/sbb instructions.
11698    The default case using setcc followed by the conditional move can be
11699    done by generic code.  */
11700 int
11701 ix86_expand_int_addcc (rtx operands[])
11702 {
11703   enum rtx_code code = GET_CODE (operands[1]);
11704   rtx compare_op;
11705   rtx val = const0_rtx;
11706   bool fpcmp = false;
11707   enum machine_mode mode = GET_MODE (operands[0]);
11708
11709   if (operands[3] != const1_rtx
11710       && operands[3] != constm1_rtx)
11711     return 0;
11712   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
11713                                        ix86_compare_op1, &compare_op))
11714      return 0;
11715   code = GET_CODE (compare_op);
11716
11717   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
11718       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
11719     {
11720       fpcmp = true;
11721       code = ix86_fp_compare_code_to_integer (code);
11722     }
11723
11724   if (code != LTU)
11725     {
11726       val = constm1_rtx;
11727       if (fpcmp)
11728         PUT_CODE (compare_op,
11729                   reverse_condition_maybe_unordered
11730                     (GET_CODE (compare_op)));
11731       else
11732         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
11733     }
11734   PUT_MODE (compare_op, mode);
11735
11736   /* Construct either adc or sbb insn.  */
11737   if ((code == LTU) == (operands[3] == constm1_rtx))
11738     {
11739       switch (GET_MODE (operands[0]))
11740         {
11741           case QImode:
11742             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
11743             break;
11744           case HImode:
11745             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
11746             break;
11747           case SImode:
11748             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
11749             break;
11750           case DImode:
11751             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11752             break;
11753           default:
11754             gcc_unreachable ();
11755         }
11756     }
11757   else
11758     {
11759       switch (GET_MODE (operands[0]))
11760         {
11761           case QImode:
11762             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
11763             break;
11764           case HImode:
11765             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
11766             break;
11767           case SImode:
11768             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
11769             break;
11770           case DImode:
11771             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
11772             break;
11773           default:
11774             gcc_unreachable ();
11775         }
11776     }
11777   return 1; /* DONE */
11778 }
11779
11780
11781 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
11782    works for floating pointer parameters and nonoffsetable memories.
11783    For pushes, it returns just stack offsets; the values will be saved
11784    in the right order.  Maximally three parts are generated.  */
11785
11786 static int
11787 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
11788 {
11789   int size;
11790
11791   if (!TARGET_64BIT)
11792     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
11793   else
11794     size = (GET_MODE_SIZE (mode) + 4) / 8;
11795
11796   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
11797   gcc_assert (size >= 2 && size <= 3);
11798
11799   /* Optimize constant pool reference to immediates.  This is used by fp
11800      moves, that force all constants to memory to allow combining.  */
11801   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
11802     {
11803       rtx tmp = maybe_get_pool_constant (operand);
11804       if (tmp)
11805         operand = tmp;
11806     }
11807
11808   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
11809     {
11810       /* The only non-offsetable memories we handle are pushes.  */
11811       int ok = push_operand (operand, VOIDmode);
11812
11813       gcc_assert (ok);
11814
11815       operand = copy_rtx (operand);
11816       PUT_MODE (operand, Pmode);
11817       parts[0] = parts[1] = parts[2] = operand;
11818       return size;
11819     }
11820
11821   if (GET_CODE (operand) == CONST_VECTOR)
11822     {
11823       enum machine_mode imode = int_mode_for_mode (mode);
11824       /* Caution: if we looked through a constant pool memory above,
11825          the operand may actually have a different mode now.  That's
11826          ok, since we want to pun this all the way back to an integer.  */
11827       operand = simplify_subreg (imode, operand, GET_MODE (operand), 0);
11828       gcc_assert (operand != NULL);
11829       mode = imode;
11830     }
11831
11832   if (!TARGET_64BIT)
11833     {
11834       if (mode == DImode)
11835         split_di (&operand, 1, &parts[0], &parts[1]);
11836       else
11837         {
11838           if (REG_P (operand))
11839             {
11840               gcc_assert (reload_completed);
11841               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
11842               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
11843               if (size == 3)
11844                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
11845             }
11846           else if (offsettable_memref_p (operand))
11847             {
11848               operand = adjust_address (operand, SImode, 0);
11849               parts[0] = operand;
11850               parts[1] = adjust_address (operand, SImode, 4);
11851               if (size == 3)
11852                 parts[2] = adjust_address (operand, SImode, 8);
11853             }
11854           else if (GET_CODE (operand) == CONST_DOUBLE)
11855             {
11856               REAL_VALUE_TYPE r;
11857               long l[4];
11858
11859               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11860               switch (mode)
11861                 {
11862                 case XFmode:
11863                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
11864                   parts[2] = gen_int_mode (l[2], SImode);
11865                   break;
11866                 case DFmode:
11867                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
11868                   break;
11869                 default:
11870                   gcc_unreachable ();
11871                 }
11872               parts[1] = gen_int_mode (l[1], SImode);
11873               parts[0] = gen_int_mode (l[0], SImode);
11874             }
11875           else
11876             gcc_unreachable ();
11877         }
11878     }
11879   else
11880     {
11881       if (mode == TImode)
11882         split_ti (&operand, 1, &parts[0], &parts[1]);
11883       if (mode == XFmode || mode == TFmode)
11884         {
11885           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
11886           if (REG_P (operand))
11887             {
11888               gcc_assert (reload_completed);
11889               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
11890               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
11891             }
11892           else if (offsettable_memref_p (operand))
11893             {
11894               operand = adjust_address (operand, DImode, 0);
11895               parts[0] = operand;
11896               parts[1] = adjust_address (operand, upper_mode, 8);
11897             }
11898           else if (GET_CODE (operand) == CONST_DOUBLE)
11899             {
11900               REAL_VALUE_TYPE r;
11901               long l[4];
11902
11903               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
11904               real_to_target (l, &r, mode);
11905
11906               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
11907               if (HOST_BITS_PER_WIDE_INT >= 64)
11908                 parts[0]
11909                   = gen_int_mode
11910                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
11911                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
11912                        DImode);
11913               else
11914                 parts[0] = immed_double_const (l[0], l[1], DImode);
11915
11916               if (upper_mode == SImode)
11917                 parts[1] = gen_int_mode (l[2], SImode);
11918               else if (HOST_BITS_PER_WIDE_INT >= 64)
11919                 parts[1]
11920                   = gen_int_mode
11921                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
11922                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
11923                        DImode);
11924               else
11925                 parts[1] = immed_double_const (l[2], l[3], DImode);
11926             }
11927           else
11928             gcc_unreachable ();
11929         }
11930     }
11931
11932   return size;
11933 }
11934
11935 /* Emit insns to perform a move or push of DI, DF, and XF values.
11936    Return false when normal moves are needed; true when all required
11937    insns have been emitted.  Operands 2-4 contain the input values
11938    int the correct order; operands 5-7 contain the output values.  */
11939
11940 void
11941 ix86_split_long_move (rtx operands[])
11942 {
11943   rtx part[2][3];
11944   int nparts;
11945   int push = 0;
11946   int collisions = 0;
11947   enum machine_mode mode = GET_MODE (operands[0]);
11948
11949   /* The DFmode expanders may ask us to move double.
11950      For 64bit target this is single move.  By hiding the fact
11951      here we simplify i386.md splitters.  */
11952   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
11953     {
11954       /* Optimize constant pool reference to immediates.  This is used by
11955          fp moves, that force all constants to memory to allow combining.  */
11956
11957       if (GET_CODE (operands[1]) == MEM
11958           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
11959           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
11960         operands[1] = get_pool_constant (XEXP (operands[1], 0));
11961       if (push_operand (operands[0], VOIDmode))
11962         {
11963           operands[0] = copy_rtx (operands[0]);
11964           PUT_MODE (operands[0], Pmode);
11965         }
11966       else
11967         operands[0] = gen_lowpart (DImode, operands[0]);
11968       operands[1] = gen_lowpart (DImode, operands[1]);
11969       emit_move_insn (operands[0], operands[1]);
11970       return;
11971     }
11972
11973   /* The only non-offsettable memory we handle is push.  */
11974   if (push_operand (operands[0], VOIDmode))
11975     push = 1;
11976   else
11977     gcc_assert (GET_CODE (operands[0]) != MEM
11978                 || offsettable_memref_p (operands[0]));
11979
11980   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
11981   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
11982
11983   /* When emitting push, take care for source operands on the stack.  */
11984   if (push && GET_CODE (operands[1]) == MEM
11985       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
11986     {
11987       if (nparts == 3)
11988         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
11989                                      XEXP (part[1][2], 0));
11990       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
11991                                    XEXP (part[1][1], 0));
11992     }
11993
11994   /* We need to do copy in the right order in case an address register
11995      of the source overlaps the destination.  */
11996   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
11997     {
11998       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
11999         collisions++;
12000       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12001         collisions++;
12002       if (nparts == 3
12003           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
12004         collisions++;
12005
12006       /* Collision in the middle part can be handled by reordering.  */
12007       if (collisions == 1 && nparts == 3
12008           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
12009         {
12010           rtx tmp;
12011           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
12012           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
12013         }
12014
12015       /* If there are more collisions, we can't handle it by reordering.
12016          Do an lea to the last part and use only one colliding move.  */
12017       else if (collisions > 1)
12018         {
12019           rtx base;
12020
12021           collisions = 1;
12022
12023           base = part[0][nparts - 1];
12024
12025           /* Handle the case when the last part isn't valid for lea.
12026              Happens in 64-bit mode storing the 12-byte XFmode.  */
12027           if (GET_MODE (base) != Pmode)
12028             base = gen_rtx_REG (Pmode, REGNO (base));
12029
12030           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
12031           part[1][0] = replace_equiv_address (part[1][0], base);
12032           part[1][1] = replace_equiv_address (part[1][1],
12033                                       plus_constant (base, UNITS_PER_WORD));
12034           if (nparts == 3)
12035             part[1][2] = replace_equiv_address (part[1][2],
12036                                       plus_constant (base, 8));
12037         }
12038     }
12039
12040   if (push)
12041     {
12042       if (!TARGET_64BIT)
12043         {
12044           if (nparts == 3)
12045             {
12046               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
12047                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
12048               emit_move_insn (part[0][2], part[1][2]);
12049             }
12050         }
12051       else
12052         {
12053           /* In 64bit mode we don't have 32bit push available.  In case this is
12054              register, it is OK - we will just use larger counterpart.  We also
12055              retype memory - these comes from attempt to avoid REX prefix on
12056              moving of second half of TFmode value.  */
12057           if (GET_MODE (part[1][1]) == SImode)
12058             {
12059               switch (GET_CODE (part[1][1]))
12060                 {
12061                 case MEM:
12062                   part[1][1] = adjust_address (part[1][1], DImode, 0);
12063                   break;
12064
12065                 case REG:
12066                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
12067                   break;
12068
12069                 default:
12070                   gcc_unreachable ();
12071                 }
12072
12073               if (GET_MODE (part[1][0]) == SImode)
12074                 part[1][0] = part[1][1];
12075             }
12076         }
12077       emit_move_insn (part[0][1], part[1][1]);
12078       emit_move_insn (part[0][0], part[1][0]);
12079       return;
12080     }
12081
12082   /* Choose correct order to not overwrite the source before it is copied.  */
12083   if ((REG_P (part[0][0])
12084        && REG_P (part[1][1])
12085        && (REGNO (part[0][0]) == REGNO (part[1][1])
12086            || (nparts == 3
12087                && REGNO (part[0][0]) == REGNO (part[1][2]))))
12088       || (collisions > 0
12089           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
12090     {
12091       if (nparts == 3)
12092         {
12093           operands[2] = part[0][2];
12094           operands[3] = part[0][1];
12095           operands[4] = part[0][0];
12096           operands[5] = part[1][2];
12097           operands[6] = part[1][1];
12098           operands[7] = part[1][0];
12099         }
12100       else
12101         {
12102           operands[2] = part[0][1];
12103           operands[3] = part[0][0];
12104           operands[5] = part[1][1];
12105           operands[6] = part[1][0];
12106         }
12107     }
12108   else
12109     {
12110       if (nparts == 3)
12111         {
12112           operands[2] = part[0][0];
12113           operands[3] = part[0][1];
12114           operands[4] = part[0][2];
12115           operands[5] = part[1][0];
12116           operands[6] = part[1][1];
12117           operands[7] = part[1][2];
12118         }
12119       else
12120         {
12121           operands[2] = part[0][0];
12122           operands[3] = part[0][1];
12123           operands[5] = part[1][0];
12124           operands[6] = part[1][1];
12125         }
12126     }
12127
12128   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
12129   if (optimize_size)
12130     {
12131       if (GET_CODE (operands[5]) == CONST_INT
12132           && operands[5] != const0_rtx
12133           && REG_P (operands[2]))
12134         {
12135           if (GET_CODE (operands[6]) == CONST_INT
12136               && INTVAL (operands[6]) == INTVAL (operands[5]))
12137             operands[6] = operands[2];
12138
12139           if (nparts == 3
12140               && GET_CODE (operands[7]) == CONST_INT
12141               && INTVAL (operands[7]) == INTVAL (operands[5]))
12142             operands[7] = operands[2];
12143         }
12144
12145       if (nparts == 3
12146           && GET_CODE (operands[6]) == CONST_INT
12147           && operands[6] != const0_rtx
12148           && REG_P (operands[3])
12149           && GET_CODE (operands[7]) == CONST_INT
12150           && INTVAL (operands[7]) == INTVAL (operands[6]))
12151         operands[7] = operands[3];
12152     }
12153
12154   emit_move_insn (operands[2], operands[5]);
12155   emit_move_insn (operands[3], operands[6]);
12156   if (nparts == 3)
12157     emit_move_insn (operands[4], operands[7]);
12158
12159   return;
12160 }
12161
12162 /* Helper function of ix86_split_ashl used to generate an SImode/DImode
12163    left shift by a constant, either using a single shift or
12164    a sequence of add instructions.  */
12165
12166 static void
12167 ix86_expand_ashl_const (rtx operand, int count, enum machine_mode mode)
12168 {
12169   if (count == 1)
12170     {
12171       emit_insn ((mode == DImode
12172                   ? gen_addsi3
12173                   : gen_adddi3) (operand, operand, operand));
12174     }
12175   else if (!optimize_size
12176            && count * ix86_cost->add <= ix86_cost->shift_const)
12177     {
12178       int i;
12179       for (i=0; i<count; i++)
12180         {
12181           emit_insn ((mode == DImode
12182                       ? gen_addsi3
12183                       : gen_adddi3) (operand, operand, operand));
12184         }
12185     }
12186   else
12187     emit_insn ((mode == DImode
12188                 ? gen_ashlsi3
12189                 : gen_ashldi3) (operand, operand, GEN_INT (count)));
12190 }
12191
12192 void
12193 ix86_split_ashl (rtx *operands, rtx scratch, enum machine_mode mode)
12194 {
12195   rtx low[2], high[2];
12196   int count;
12197   const int single_width = mode == DImode ? 32 : 64;
12198
12199   if (GET_CODE (operands[2]) == CONST_INT)
12200     {
12201       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12202       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12203
12204       if (count >= single_width)
12205         {
12206           emit_move_insn (high[0], low[1]);
12207           emit_move_insn (low[0], const0_rtx);
12208
12209           if (count > single_width)
12210             ix86_expand_ashl_const (high[0], count - single_width, mode);
12211         }
12212       else
12213         {
12214           if (!rtx_equal_p (operands[0], operands[1]))
12215             emit_move_insn (operands[0], operands[1]);
12216           emit_insn ((mode == DImode
12217                      ? gen_x86_shld_1
12218                      : gen_x86_64_shld) (high[0], low[0], GEN_INT (count)));
12219           ix86_expand_ashl_const (low[0], count, mode);
12220         }
12221       return;
12222     }
12223
12224   (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12225
12226   if (operands[1] == const1_rtx)
12227     {
12228       /* Assuming we've chosen a QImode capable registers, then 1 << N
12229          can be done with two 32/64-bit shifts, no branches, no cmoves.  */
12230       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
12231         {
12232           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
12233
12234           ix86_expand_clear (low[0]);
12235           ix86_expand_clear (high[0]);
12236           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (single_width)));
12237
12238           d = gen_lowpart (QImode, low[0]);
12239           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12240           s = gen_rtx_EQ (QImode, flags, const0_rtx);
12241           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12242
12243           d = gen_lowpart (QImode, high[0]);
12244           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
12245           s = gen_rtx_NE (QImode, flags, const0_rtx);
12246           emit_insn (gen_rtx_SET (VOIDmode, d, s));
12247         }
12248
12249       /* Otherwise, we can get the same results by manually performing
12250          a bit extract operation on bit 5/6, and then performing the two
12251          shifts.  The two methods of getting 0/1 into low/high are exactly
12252          the same size.  Avoiding the shift in the bit extract case helps
12253          pentium4 a bit; no one else seems to care much either way.  */
12254       else
12255         {
12256           rtx x;
12257
12258           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
12259             x = gen_rtx_ZERO_EXTEND (mode == DImode ? SImode : DImode, operands[2]);
12260           else
12261             x = gen_lowpart (mode == DImode ? SImode : DImode, operands[2]);
12262           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
12263
12264           emit_insn ((mode == DImode
12265                       ? gen_lshrsi3
12266                       : gen_lshrdi3) (high[0], high[0], GEN_INT (mode == DImode ? 5 : 6)));
12267           emit_insn ((mode == DImode
12268                       ? gen_andsi3
12269                       : gen_anddi3) (high[0], high[0], GEN_INT (1)));
12270           emit_move_insn (low[0], high[0]);
12271           emit_insn ((mode == DImode
12272                       ? gen_xorsi3
12273                       : gen_xordi3) (low[0], low[0], GEN_INT (1)));
12274         }
12275
12276       emit_insn ((mode == DImode
12277                     ? gen_ashlsi3
12278                     : gen_ashldi3) (low[0], low[0], operands[2]));
12279       emit_insn ((mode == DImode
12280                     ? gen_ashlsi3
12281                     : gen_ashldi3) (high[0], high[0], operands[2]));
12282       return;
12283     }
12284
12285   if (operands[1] == constm1_rtx)
12286     {
12287       /* For -1 << N, we can avoid the shld instruction, because we
12288          know that we're shifting 0...31/63 ones into a -1.  */
12289       emit_move_insn (low[0], constm1_rtx);
12290       if (optimize_size)
12291         emit_move_insn (high[0], low[0]);
12292       else
12293         emit_move_insn (high[0], constm1_rtx);
12294     }
12295   else
12296     {
12297       if (!rtx_equal_p (operands[0], operands[1]))
12298         emit_move_insn (operands[0], operands[1]);
12299
12300       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12301       emit_insn ((mode == DImode
12302                   ? gen_x86_shld_1
12303                   : gen_x86_64_shld) (high[0], low[0], operands[2]));
12304     }
12305
12306   emit_insn ((mode == DImode ? gen_ashlsi3 : gen_ashldi3) (low[0], low[0], operands[2]));
12307
12308   if (TARGET_CMOVE && scratch)
12309     {
12310       ix86_expand_clear (scratch);
12311       emit_insn ((mode == DImode
12312                   ? gen_x86_shift_adj_1
12313                   : gen_x86_64_shift_adj) (high[0], low[0], operands[2], scratch));
12314     }
12315   else
12316     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
12317 }
12318
12319 void
12320 ix86_split_ashr (rtx *operands, rtx scratch, enum machine_mode mode)
12321 {
12322   rtx low[2], high[2];
12323   int count;
12324   const int single_width = mode == DImode ? 32 : 64;
12325
12326   if (GET_CODE (operands[2]) == CONST_INT)
12327     {
12328       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12329       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12330
12331       if (count == single_width * 2 - 1)
12332         {
12333           emit_move_insn (high[0], high[1]);
12334           emit_insn ((mode == DImode
12335                       ? gen_ashrsi3
12336                       : gen_ashrdi3) (high[0], high[0],
12337                                       GEN_INT (single_width - 1)));
12338           emit_move_insn (low[0], high[0]);
12339
12340         }
12341       else if (count >= single_width)
12342         {
12343           emit_move_insn (low[0], high[1]);
12344           emit_move_insn (high[0], low[0]);
12345           emit_insn ((mode == DImode
12346                       ? gen_ashrsi3
12347                       : gen_ashrdi3) (high[0], high[0],
12348                                       GEN_INT (single_width - 1)));
12349           if (count > single_width)
12350             emit_insn ((mode == DImode
12351                         ? gen_ashrsi3
12352                         : gen_ashrdi3) (low[0], low[0],
12353                                         GEN_INT (count - single_width)));
12354         }
12355       else
12356         {
12357           if (!rtx_equal_p (operands[0], operands[1]))
12358             emit_move_insn (operands[0], operands[1]);
12359           emit_insn ((mode == DImode
12360                       ? gen_x86_shrd_1
12361                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12362           emit_insn ((mode == DImode
12363                       ? gen_ashrsi3
12364                       : gen_ashrdi3) (high[0], high[0], GEN_INT (count)));
12365         }
12366     }
12367   else
12368     {
12369       if (!rtx_equal_p (operands[0], operands[1]))
12370         emit_move_insn (operands[0], operands[1]);
12371
12372       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12373
12374       emit_insn ((mode == DImode
12375                   ? gen_x86_shrd_1
12376                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12377       emit_insn ((mode == DImode
12378                   ? gen_ashrsi3
12379                   : gen_ashrdi3)  (high[0], high[0], operands[2]));
12380
12381       if (TARGET_CMOVE && scratch)
12382         {
12383           emit_move_insn (scratch, high[0]);
12384           emit_insn ((mode == DImode
12385                       ? gen_ashrsi3
12386                       : gen_ashrdi3) (scratch, scratch,
12387                                       GEN_INT (single_width - 1)));
12388           emit_insn ((mode == DImode
12389                       ? gen_x86_shift_adj_1
12390                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12391                                          scratch));
12392         }
12393       else
12394         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
12395     }
12396 }
12397
12398 void
12399 ix86_split_lshr (rtx *operands, rtx scratch, enum machine_mode mode)
12400 {
12401   rtx low[2], high[2];
12402   int count;
12403   const int single_width = mode == DImode ? 32 : 64;
12404
12405   if (GET_CODE (operands[2]) == CONST_INT)
12406     {
12407       (mode == DImode ? split_di : split_ti) (operands, 2, low, high);
12408       count = INTVAL (operands[2]) & (single_width * 2 - 1);
12409
12410       if (count >= single_width)
12411         {
12412           emit_move_insn (low[0], high[1]);
12413           ix86_expand_clear (high[0]);
12414
12415           if (count > single_width)
12416             emit_insn ((mode == DImode
12417                         ? gen_lshrsi3
12418                         : gen_lshrdi3) (low[0], low[0],
12419                                         GEN_INT (count - single_width)));
12420         }
12421       else
12422         {
12423           if (!rtx_equal_p (operands[0], operands[1]))
12424             emit_move_insn (operands[0], operands[1]);
12425           emit_insn ((mode == DImode
12426                       ? gen_x86_shrd_1
12427                       : gen_x86_64_shrd) (low[0], high[0], GEN_INT (count)));
12428           emit_insn ((mode == DImode
12429                       ? gen_lshrsi3
12430                       : gen_lshrdi3) (high[0], high[0], GEN_INT (count)));
12431         }
12432     }
12433   else
12434     {
12435       if (!rtx_equal_p (operands[0], operands[1]))
12436         emit_move_insn (operands[0], operands[1]);
12437
12438       (mode == DImode ? split_di : split_ti) (operands, 1, low, high);
12439
12440       emit_insn ((mode == DImode
12441                   ? gen_x86_shrd_1
12442                   : gen_x86_64_shrd) (low[0], high[0], operands[2]));
12443       emit_insn ((mode == DImode
12444                   ? gen_lshrsi3
12445                   : gen_lshrdi3) (high[0], high[0], operands[2]));
12446
12447       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
12448       if (TARGET_CMOVE && scratch)
12449         {
12450           ix86_expand_clear (scratch);
12451           emit_insn ((mode == DImode
12452                       ? gen_x86_shift_adj_1
12453                       : gen_x86_64_shift_adj) (low[0], high[0], operands[2],
12454                                                scratch));
12455         }
12456       else
12457         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
12458     }
12459 }
12460
12461 /* Helper function for the string operations below.  Dest VARIABLE whether
12462    it is aligned to VALUE bytes.  If true, jump to the label.  */
12463 static rtx
12464 ix86_expand_aligntest (rtx variable, int value)
12465 {
12466   rtx label = gen_label_rtx ();
12467   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
12468   if (GET_MODE (variable) == DImode)
12469     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
12470   else
12471     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
12472   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
12473                            1, label);
12474   return label;
12475 }
12476
12477 /* Adjust COUNTER by the VALUE.  */
12478 static void
12479 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
12480 {
12481   if (GET_MODE (countreg) == DImode)
12482     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
12483   else
12484     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
12485 }
12486
12487 /* Zero extend possibly SImode EXP to Pmode register.  */
12488 rtx
12489 ix86_zero_extend_to_Pmode (rtx exp)
12490 {
12491   rtx r;
12492   if (GET_MODE (exp) == VOIDmode)
12493     return force_reg (Pmode, exp);
12494   if (GET_MODE (exp) == Pmode)
12495     return copy_to_mode_reg (Pmode, exp);
12496   r = gen_reg_rtx (Pmode);
12497   emit_insn (gen_zero_extendsidi2 (r, exp));
12498   return r;
12499 }
12500
12501 /* Expand string move (memcpy) operation.  Use i386 string operations when
12502    profitable.  expand_clrmem contains similar code.  */
12503 int
12504 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
12505 {
12506   rtx srcreg, destreg, countreg, srcexp, destexp;
12507   enum machine_mode counter_mode;
12508   HOST_WIDE_INT align = 0;
12509   unsigned HOST_WIDE_INT count = 0;
12510
12511   if (GET_CODE (align_exp) == CONST_INT)
12512     align = INTVAL (align_exp);
12513
12514   /* Can't use any of this if the user has appropriated esi or edi.  */
12515   if (global_regs[4] || global_regs[5])
12516     return 0;
12517
12518   /* This simple hack avoids all inlining code and simplifies code below.  */
12519   if (!TARGET_ALIGN_STRINGOPS)
12520     align = 64;
12521
12522   if (GET_CODE (count_exp) == CONST_INT)
12523     {
12524       count = INTVAL (count_exp);
12525       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12526         return 0;
12527     }
12528
12529   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12530      for 64bits use SImode when possible, otherwise DImode.
12531      Set count to number of bytes copied when known at compile time.  */
12532   if (!TARGET_64BIT
12533       || GET_MODE (count_exp) == SImode
12534       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12535     counter_mode = SImode;
12536   else
12537     counter_mode = DImode;
12538
12539   gcc_assert (counter_mode == SImode || counter_mode == DImode);
12540
12541   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12542   if (destreg != XEXP (dst, 0))
12543     dst = replace_equiv_address_nv (dst, destreg);
12544   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
12545   if (srcreg != XEXP (src, 0))
12546     src = replace_equiv_address_nv (src, srcreg);
12547
12548   /* When optimizing for size emit simple rep ; movsb instruction for
12549      counts not divisible by 4, except when (movsl;)*(movsw;)?(movsb;)?
12550      sequence is shorter than mov{b,l} $count, %{ecx,cl}; rep; movsb.
12551      Sice of (movsl;)*(movsw;)?(movsb;)? sequence is
12552      count / 4 + (count & 3), the other sequence is either 4 or 7 bytes,
12553      but we don't know whether upper 24 (resp. 56) bits of %ecx will be
12554      known to be zero or not.  The rep; movsb sequence causes higher
12555      register pressure though, so take that into account.  */
12556
12557   if ((!optimize || optimize_size)
12558       && (count == 0
12559           || ((count & 0x03)
12560               && (!optimize_size
12561                   || count > 5 * 4
12562                   || (count & 3) + count / 4 > 6))))
12563     {
12564       emit_insn (gen_cld ());
12565       countreg = ix86_zero_extend_to_Pmode (count_exp);
12566       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12567       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
12568       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
12569                               destexp, srcexp));
12570     }
12571
12572   /* For constant aligned (or small unaligned) copies use rep movsl
12573      followed by code copying the rest.  For PentiumPro ensure 8 byte
12574      alignment to allow rep movsl acceleration.  */
12575
12576   else if (count != 0
12577            && (align >= 8
12578                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12579                || optimize_size || count < (unsigned int) 64))
12580     {
12581       unsigned HOST_WIDE_INT offset = 0;
12582       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12583       rtx srcmem, dstmem;
12584
12585       emit_insn (gen_cld ());
12586       if (count & ~(size - 1))
12587         {
12588           if ((TARGET_SINGLE_STRINGOP || optimize_size) && count < 5 * 4)
12589             {
12590               enum machine_mode movs_mode = size == 4 ? SImode : DImode;
12591
12592               while (offset < (count & ~(size - 1)))
12593                 {
12594                   srcmem = adjust_automodify_address_nv (src, movs_mode,
12595                                                          srcreg, offset);
12596                   dstmem = adjust_automodify_address_nv (dst, movs_mode,
12597                                                          destreg, offset);
12598                   emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12599                   offset += size;
12600                 }
12601             }
12602           else
12603             {
12604               countreg = GEN_INT ((count >> (size == 4 ? 2 : 3))
12605                                   & (TARGET_64BIT ? -1 : 0x3fffffff));
12606               countreg = copy_to_mode_reg (counter_mode, countreg);
12607               countreg = ix86_zero_extend_to_Pmode (countreg);
12608
12609               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12610                                         GEN_INT (size == 4 ? 2 : 3));
12611               srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12612               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12613
12614               emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12615                                       countreg, destexp, srcexp));
12616               offset = count & ~(size - 1);
12617             }
12618         }
12619       if (size == 8 && (count & 0x04))
12620         {
12621           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
12622                                                  offset);
12623           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
12624                                                  offset);
12625           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12626           offset += 4;
12627         }
12628       if (count & 0x02)
12629         {
12630           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
12631                                                  offset);
12632           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
12633                                                  offset);
12634           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12635           offset += 2;
12636         }
12637       if (count & 0x01)
12638         {
12639           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
12640                                                  offset);
12641           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
12642                                                  offset);
12643           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12644         }
12645     }
12646   /* The generic code based on the glibc implementation:
12647      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
12648      allowing accelerated copying there)
12649      - copy the data using rep movsl
12650      - copy the rest.  */
12651   else
12652     {
12653       rtx countreg2;
12654       rtx label = NULL;
12655       rtx srcmem, dstmem;
12656       int desired_alignment = (TARGET_PENTIUMPRO
12657                                && (count == 0 || count >= (unsigned int) 260)
12658                                ? 8 : UNITS_PER_WORD);
12659       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
12660       dst = change_address (dst, BLKmode, destreg);
12661       src = change_address (src, BLKmode, srcreg);
12662
12663       /* In case we don't know anything about the alignment, default to
12664          library version, since it is usually equally fast and result in
12665          shorter code.
12666
12667          Also emit call when we know that the count is large and call overhead
12668          will not be important.  */
12669       if (!TARGET_INLINE_ALL_STRINGOPS
12670           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12671         return 0;
12672
12673       if (TARGET_SINGLE_STRINGOP)
12674         emit_insn (gen_cld ());
12675
12676       countreg2 = gen_reg_rtx (Pmode);
12677       countreg = copy_to_mode_reg (counter_mode, count_exp);
12678
12679       /* We don't use loops to align destination and to copy parts smaller
12680          than 4 bytes, because gcc is able to optimize such code better (in
12681          the case the destination or the count really is aligned, gcc is often
12682          able to predict the branches) and also it is friendlier to the
12683          hardware branch prediction.
12684
12685          Using loops is beneficial for generic case, because we can
12686          handle small counts using the loops.  Many CPUs (such as Athlon)
12687          have large REP prefix setup costs.
12688
12689          This is quite costly.  Maybe we can revisit this decision later or
12690          add some customizability to this code.  */
12691
12692       if (count == 0 && align < desired_alignment)
12693         {
12694           label = gen_label_rtx ();
12695           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12696                                    LEU, 0, counter_mode, 1, label);
12697         }
12698       if (align <= 1)
12699         {
12700           rtx label = ix86_expand_aligntest (destreg, 1);
12701           srcmem = change_address (src, QImode, srcreg);
12702           dstmem = change_address (dst, QImode, destreg);
12703           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12704           ix86_adjust_counter (countreg, 1);
12705           emit_label (label);
12706           LABEL_NUSES (label) = 1;
12707         }
12708       if (align <= 2)
12709         {
12710           rtx label = ix86_expand_aligntest (destreg, 2);
12711           srcmem = change_address (src, HImode, srcreg);
12712           dstmem = change_address (dst, HImode, destreg);
12713           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12714           ix86_adjust_counter (countreg, 2);
12715           emit_label (label);
12716           LABEL_NUSES (label) = 1;
12717         }
12718       if (align <= 4 && desired_alignment > 4)
12719         {
12720           rtx label = ix86_expand_aligntest (destreg, 4);
12721           srcmem = change_address (src, SImode, srcreg);
12722           dstmem = change_address (dst, SImode, destreg);
12723           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12724           ix86_adjust_counter (countreg, 4);
12725           emit_label (label);
12726           LABEL_NUSES (label) = 1;
12727         }
12728
12729       if (label && desired_alignment > 4 && !TARGET_64BIT)
12730         {
12731           emit_label (label);
12732           LABEL_NUSES (label) = 1;
12733           label = NULL_RTX;
12734         }
12735       if (!TARGET_SINGLE_STRINGOP)
12736         emit_insn (gen_cld ());
12737       if (TARGET_64BIT)
12738         {
12739           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
12740                                   GEN_INT (3)));
12741           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
12742         }
12743       else
12744         {
12745           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
12746           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
12747         }
12748       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
12749       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12750       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
12751                               countreg2, destexp, srcexp));
12752
12753       if (label)
12754         {
12755           emit_label (label);
12756           LABEL_NUSES (label) = 1;
12757         }
12758       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
12759         {
12760           srcmem = change_address (src, SImode, srcreg);
12761           dstmem = change_address (dst, SImode, destreg);
12762           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12763         }
12764       if ((align <= 4 || count == 0) && TARGET_64BIT)
12765         {
12766           rtx label = ix86_expand_aligntest (countreg, 4);
12767           srcmem = change_address (src, SImode, srcreg);
12768           dstmem = change_address (dst, SImode, destreg);
12769           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12770           emit_label (label);
12771           LABEL_NUSES (label) = 1;
12772         }
12773       if (align > 2 && count != 0 && (count & 2))
12774         {
12775           srcmem = change_address (src, HImode, srcreg);
12776           dstmem = change_address (dst, HImode, destreg);
12777           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12778         }
12779       if (align <= 2 || count == 0)
12780         {
12781           rtx label = ix86_expand_aligntest (countreg, 2);
12782           srcmem = change_address (src, HImode, srcreg);
12783           dstmem = change_address (dst, HImode, destreg);
12784           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12785           emit_label (label);
12786           LABEL_NUSES (label) = 1;
12787         }
12788       if (align > 1 && count != 0 && (count & 1))
12789         {
12790           srcmem = change_address (src, QImode, srcreg);
12791           dstmem = change_address (dst, QImode, destreg);
12792           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12793         }
12794       if (align <= 1 || count == 0)
12795         {
12796           rtx label = ix86_expand_aligntest (countreg, 1);
12797           srcmem = change_address (src, QImode, srcreg);
12798           dstmem = change_address (dst, QImode, destreg);
12799           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
12800           emit_label (label);
12801           LABEL_NUSES (label) = 1;
12802         }
12803     }
12804
12805   return 1;
12806 }
12807
12808 /* Expand string clear operation (bzero).  Use i386 string operations when
12809    profitable.  expand_movmem contains similar code.  */
12810 int
12811 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
12812 {
12813   rtx destreg, zeroreg, countreg, destexp;
12814   enum machine_mode counter_mode;
12815   HOST_WIDE_INT align = 0;
12816   unsigned HOST_WIDE_INT count = 0;
12817
12818   if (GET_CODE (align_exp) == CONST_INT)
12819     align = INTVAL (align_exp);
12820
12821   /* Can't use any of this if the user has appropriated esi.  */
12822   if (global_regs[4])
12823     return 0;
12824
12825   /* This simple hack avoids all inlining code and simplifies code below.  */
12826   if (!TARGET_ALIGN_STRINGOPS)
12827     align = 32;
12828
12829   if (GET_CODE (count_exp) == CONST_INT)
12830     {
12831       count = INTVAL (count_exp);
12832       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
12833         return 0;
12834     }
12835   /* Figure out proper mode for counter.  For 32bits it is always SImode,
12836      for 64bits use SImode when possible, otherwise DImode.
12837      Set count to number of bytes copied when known at compile time.  */
12838   if (!TARGET_64BIT
12839       || GET_MODE (count_exp) == SImode
12840       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
12841     counter_mode = SImode;
12842   else
12843     counter_mode = DImode;
12844
12845   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
12846   if (destreg != XEXP (dst, 0))
12847     dst = replace_equiv_address_nv (dst, destreg);
12848
12849
12850   /* When optimizing for size emit simple rep ; movsb instruction for
12851      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
12852      sequence is 7 bytes long, so if optimizing for size and count is
12853      small enough that some stosl, stosw and stosb instructions without
12854      rep are shorter, fall back into the next if.  */
12855
12856   if ((!optimize || optimize_size)
12857       && (count == 0
12858           || ((count & 0x03)
12859               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
12860     {
12861       emit_insn (gen_cld ());
12862
12863       countreg = ix86_zero_extend_to_Pmode (count_exp);
12864       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
12865       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
12866       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
12867     }
12868   else if (count != 0
12869            && (align >= 8
12870                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
12871                || optimize_size || count < (unsigned int) 64))
12872     {
12873       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
12874       unsigned HOST_WIDE_INT offset = 0;
12875
12876       emit_insn (gen_cld ());
12877
12878       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
12879       if (count & ~(size - 1))
12880         {
12881           unsigned HOST_WIDE_INT repcount;
12882           unsigned int max_nonrep;
12883
12884           repcount = count >> (size == 4 ? 2 : 3);
12885           if (!TARGET_64BIT)
12886             repcount &= 0x3fffffff;
12887
12888           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
12889              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
12890              bytes.  In both cases the latter seems to be faster for small
12891              values of N.  */
12892           max_nonrep = size == 4 ? 7 : 4;
12893           if (!optimize_size)
12894             switch (ix86_tune)
12895               {
12896               case PROCESSOR_PENTIUM4:
12897               case PROCESSOR_NOCONA:
12898                 max_nonrep = 3;
12899                 break;
12900               default:
12901                 break;
12902               }
12903
12904           if (repcount <= max_nonrep)
12905             while (repcount-- > 0)
12906               {
12907                 rtx mem = adjust_automodify_address_nv (dst,
12908                                                         GET_MODE (zeroreg),
12909                                                         destreg, offset);
12910                 emit_insn (gen_strset (destreg, mem, zeroreg));
12911                 offset += size;
12912               }
12913           else
12914             {
12915               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
12916               countreg = ix86_zero_extend_to_Pmode (countreg);
12917               destexp = gen_rtx_ASHIFT (Pmode, countreg,
12918                                         GEN_INT (size == 4 ? 2 : 3));
12919               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
12920               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
12921                                        destexp));
12922               offset = count & ~(size - 1);
12923             }
12924         }
12925       if (size == 8 && (count & 0x04))
12926         {
12927           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
12928                                                   offset);
12929           emit_insn (gen_strset (destreg, mem,
12930                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
12931           offset += 4;
12932         }
12933       if (count & 0x02)
12934         {
12935           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
12936                                                   offset);
12937           emit_insn (gen_strset (destreg, mem,
12938                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12939           offset += 2;
12940         }
12941       if (count & 0x01)
12942         {
12943           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
12944                                                   offset);
12945           emit_insn (gen_strset (destreg, mem,
12946                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12947         }
12948     }
12949   else
12950     {
12951       rtx countreg2;
12952       rtx label = NULL;
12953       /* Compute desired alignment of the string operation.  */
12954       int desired_alignment = (TARGET_PENTIUMPRO
12955                                && (count == 0 || count >= (unsigned int) 260)
12956                                ? 8 : UNITS_PER_WORD);
12957
12958       /* In case we don't know anything about the alignment, default to
12959          library version, since it is usually equally fast and result in
12960          shorter code.
12961
12962          Also emit call when we know that the count is large and call overhead
12963          will not be important.  */
12964       if (!TARGET_INLINE_ALL_STRINGOPS
12965           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
12966         return 0;
12967
12968       if (TARGET_SINGLE_STRINGOP)
12969         emit_insn (gen_cld ());
12970
12971       countreg2 = gen_reg_rtx (Pmode);
12972       countreg = copy_to_mode_reg (counter_mode, count_exp);
12973       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
12974       /* Get rid of MEM_OFFSET, it won't be accurate.  */
12975       dst = change_address (dst, BLKmode, destreg);
12976
12977       if (count == 0 && align < desired_alignment)
12978         {
12979           label = gen_label_rtx ();
12980           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
12981                                    LEU, 0, counter_mode, 1, label);
12982         }
12983       if (align <= 1)
12984         {
12985           rtx label = ix86_expand_aligntest (destreg, 1);
12986           emit_insn (gen_strset (destreg, dst,
12987                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
12988           ix86_adjust_counter (countreg, 1);
12989           emit_label (label);
12990           LABEL_NUSES (label) = 1;
12991         }
12992       if (align <= 2)
12993         {
12994           rtx label = ix86_expand_aligntest (destreg, 2);
12995           emit_insn (gen_strset (destreg, dst,
12996                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
12997           ix86_adjust_counter (countreg, 2);
12998           emit_label (label);
12999           LABEL_NUSES (label) = 1;
13000         }
13001       if (align <= 4 && desired_alignment > 4)
13002         {
13003           rtx label = ix86_expand_aligntest (destreg, 4);
13004           emit_insn (gen_strset (destreg, dst,
13005                                  (TARGET_64BIT
13006                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
13007                                   : zeroreg)));
13008           ix86_adjust_counter (countreg, 4);
13009           emit_label (label);
13010           LABEL_NUSES (label) = 1;
13011         }
13012
13013       if (label && desired_alignment > 4 && !TARGET_64BIT)
13014         {
13015           emit_label (label);
13016           LABEL_NUSES (label) = 1;
13017           label = NULL_RTX;
13018         }
13019
13020       if (!TARGET_SINGLE_STRINGOP)
13021         emit_insn (gen_cld ());
13022       if (TARGET_64BIT)
13023         {
13024           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
13025                                   GEN_INT (3)));
13026           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
13027         }
13028       else
13029         {
13030           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
13031           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
13032         }
13033       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
13034       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
13035
13036       if (label)
13037         {
13038           emit_label (label);
13039           LABEL_NUSES (label) = 1;
13040         }
13041
13042       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
13043         emit_insn (gen_strset (destreg, dst,
13044                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
13045       if (TARGET_64BIT && (align <= 4 || count == 0))
13046         {
13047           rtx label = ix86_expand_aligntest (countreg, 4);
13048           emit_insn (gen_strset (destreg, dst,
13049                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
13050           emit_label (label);
13051           LABEL_NUSES (label) = 1;
13052         }
13053       if (align > 2 && count != 0 && (count & 2))
13054         emit_insn (gen_strset (destreg, dst,
13055                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
13056       if (align <= 2 || count == 0)
13057         {
13058           rtx label = ix86_expand_aligntest (countreg, 2);
13059           emit_insn (gen_strset (destreg, dst,
13060                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
13061           emit_label (label);
13062           LABEL_NUSES (label) = 1;
13063         }
13064       if (align > 1 && count != 0 && (count & 1))
13065         emit_insn (gen_strset (destreg, dst,
13066                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
13067       if (align <= 1 || count == 0)
13068         {
13069           rtx label = ix86_expand_aligntest (countreg, 1);
13070           emit_insn (gen_strset (destreg, dst,
13071                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
13072           emit_label (label);
13073           LABEL_NUSES (label) = 1;
13074         }
13075     }
13076   return 1;
13077 }
13078
13079 /* Expand strlen.  */
13080 int
13081 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
13082 {
13083   rtx addr, scratch1, scratch2, scratch3, scratch4;
13084
13085   /* The generic case of strlen expander is long.  Avoid it's
13086      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
13087
13088   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
13089       && !TARGET_INLINE_ALL_STRINGOPS
13090       && !optimize_size
13091       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
13092     return 0;
13093
13094   addr = force_reg (Pmode, XEXP (src, 0));
13095   scratch1 = gen_reg_rtx (Pmode);
13096
13097   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
13098       && !optimize_size)
13099     {
13100       /* Well it seems that some optimizer does not combine a call like
13101          foo(strlen(bar), strlen(bar));
13102          when the move and the subtraction is done here.  It does calculate
13103          the length just once when these instructions are done inside of
13104          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
13105          often used and I use one fewer register for the lifetime of
13106          output_strlen_unroll() this is better.  */
13107
13108       emit_move_insn (out, addr);
13109
13110       ix86_expand_strlensi_unroll_1 (out, src, align);
13111
13112       /* strlensi_unroll_1 returns the address of the zero at the end of
13113          the string, like memchr(), so compute the length by subtracting
13114          the start address.  */
13115       if (TARGET_64BIT)
13116         emit_insn (gen_subdi3 (out, out, addr));
13117       else
13118         emit_insn (gen_subsi3 (out, out, addr));
13119     }
13120   else
13121     {
13122       rtx unspec;
13123       scratch2 = gen_reg_rtx (Pmode);
13124       scratch3 = gen_reg_rtx (Pmode);
13125       scratch4 = force_reg (Pmode, constm1_rtx);
13126
13127       emit_move_insn (scratch3, addr);
13128       eoschar = force_reg (QImode, eoschar);
13129
13130       emit_insn (gen_cld ());
13131       src = replace_equiv_address_nv (src, scratch3);
13132
13133       /* If .md starts supporting :P, this can be done in .md.  */
13134       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
13135                                                  scratch4), UNSPEC_SCAS);
13136       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
13137       if (TARGET_64BIT)
13138         {
13139           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
13140           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
13141         }
13142       else
13143         {
13144           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
13145           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
13146         }
13147     }
13148   return 1;
13149 }
13150
13151 /* Expand the appropriate insns for doing strlen if not just doing
13152    repnz; scasb
13153
13154    out = result, initialized with the start address
13155    align_rtx = alignment of the address.
13156    scratch = scratch register, initialized with the startaddress when
13157         not aligned, otherwise undefined
13158
13159    This is just the body. It needs the initializations mentioned above and
13160    some address computing at the end.  These things are done in i386.md.  */
13161
13162 static void
13163 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
13164 {
13165   int align;
13166   rtx tmp;
13167   rtx align_2_label = NULL_RTX;
13168   rtx align_3_label = NULL_RTX;
13169   rtx align_4_label = gen_label_rtx ();
13170   rtx end_0_label = gen_label_rtx ();
13171   rtx mem;
13172   rtx tmpreg = gen_reg_rtx (SImode);
13173   rtx scratch = gen_reg_rtx (SImode);
13174   rtx cmp;
13175
13176   align = 0;
13177   if (GET_CODE (align_rtx) == CONST_INT)
13178     align = INTVAL (align_rtx);
13179
13180   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
13181
13182   /* Is there a known alignment and is it less than 4?  */
13183   if (align < 4)
13184     {
13185       rtx scratch1 = gen_reg_rtx (Pmode);
13186       emit_move_insn (scratch1, out);
13187       /* Is there a known alignment and is it not 2? */
13188       if (align != 2)
13189         {
13190           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
13191           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
13192
13193           /* Leave just the 3 lower bits.  */
13194           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
13195                                     NULL_RTX, 0, OPTAB_WIDEN);
13196
13197           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
13198                                    Pmode, 1, align_4_label);
13199           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
13200                                    Pmode, 1, align_2_label);
13201           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
13202                                    Pmode, 1, align_3_label);
13203         }
13204       else
13205         {
13206           /* Since the alignment is 2, we have to check 2 or 0 bytes;
13207              check if is aligned to 4 - byte.  */
13208
13209           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
13210                                     NULL_RTX, 0, OPTAB_WIDEN);
13211
13212           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
13213                                    Pmode, 1, align_4_label);
13214         }
13215
13216       mem = change_address (src, QImode, out);
13217
13218       /* Now compare the bytes.  */
13219
13220       /* Compare the first n unaligned byte on a byte per byte basis.  */
13221       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
13222                                QImode, 1, end_0_label);
13223
13224       /* Increment the address.  */
13225       if (TARGET_64BIT)
13226         emit_insn (gen_adddi3 (out, out, const1_rtx));
13227       else
13228         emit_insn (gen_addsi3 (out, out, const1_rtx));
13229
13230       /* Not needed with an alignment of 2 */
13231       if (align != 2)
13232         {
13233           emit_label (align_2_label);
13234
13235           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
13236                                    end_0_label);
13237
13238           if (TARGET_64BIT)
13239             emit_insn (gen_adddi3 (out, out, const1_rtx));
13240           else
13241             emit_insn (gen_addsi3 (out, out, const1_rtx));
13242
13243           emit_label (align_3_label);
13244         }
13245
13246       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
13247                                end_0_label);
13248
13249       if (TARGET_64BIT)
13250         emit_insn (gen_adddi3 (out, out, const1_rtx));
13251       else
13252         emit_insn (gen_addsi3 (out, out, const1_rtx));
13253     }
13254
13255   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
13256      align this loop.  It gives only huge programs, but does not help to
13257      speed up.  */
13258   emit_label (align_4_label);
13259
13260   mem = change_address (src, SImode, out);
13261   emit_move_insn (scratch, mem);
13262   if (TARGET_64BIT)
13263     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
13264   else
13265     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
13266
13267   /* This formula yields a nonzero result iff one of the bytes is zero.
13268      This saves three branches inside loop and many cycles.  */
13269
13270   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
13271   emit_insn (gen_one_cmplsi2 (scratch, scratch));
13272   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
13273   emit_insn (gen_andsi3 (tmpreg, tmpreg,
13274                          gen_int_mode (0x80808080, SImode)));
13275   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
13276                            align_4_label);
13277
13278   if (TARGET_CMOVE)
13279     {
13280        rtx reg = gen_reg_rtx (SImode);
13281        rtx reg2 = gen_reg_rtx (Pmode);
13282        emit_move_insn (reg, tmpreg);
13283        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
13284
13285        /* If zero is not in the first two bytes, move two bytes forward.  */
13286        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13287        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13288        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13289        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
13290                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
13291                                                      reg,
13292                                                      tmpreg)));
13293        /* Emit lea manually to avoid clobbering of flags.  */
13294        emit_insn (gen_rtx_SET (SImode, reg2,
13295                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
13296
13297        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13298        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
13299        emit_insn (gen_rtx_SET (VOIDmode, out,
13300                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
13301                                                      reg2,
13302                                                      out)));
13303
13304     }
13305   else
13306     {
13307        rtx end_2_label = gen_label_rtx ();
13308        /* Is zero in the first two bytes? */
13309
13310        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
13311        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
13312        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
13313        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
13314                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
13315                             pc_rtx);
13316        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
13317        JUMP_LABEL (tmp) = end_2_label;
13318
13319        /* Not in the first two.  Move two bytes forward.  */
13320        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
13321        if (TARGET_64BIT)
13322          emit_insn (gen_adddi3 (out, out, const2_rtx));
13323        else
13324          emit_insn (gen_addsi3 (out, out, const2_rtx));
13325
13326        emit_label (end_2_label);
13327
13328     }
13329
13330   /* Avoid branch in fixing the byte.  */
13331   tmpreg = gen_lowpart (QImode, tmpreg);
13332   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
13333   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
13334   if (TARGET_64BIT)
13335     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
13336   else
13337     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
13338
13339   emit_label (end_0_label);
13340 }
13341
13342 void
13343 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
13344                   rtx callarg2 ATTRIBUTE_UNUSED,
13345                   rtx pop, int sibcall)
13346 {
13347   rtx use = NULL, call;
13348
13349   if (pop == const0_rtx)
13350     pop = NULL;
13351   gcc_assert (!TARGET_64BIT || !pop);
13352
13353   if (TARGET_MACHO && !TARGET_64BIT)
13354     {
13355 #if TARGET_MACHO
13356       if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
13357         fnaddr = machopic_indirect_call_target (fnaddr);
13358 #endif
13359     }
13360   else
13361     {
13362       /* Static functions and indirect calls don't need the pic register.  */
13363       if (! TARGET_64BIT && flag_pic
13364           && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
13365           && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
13366         use_reg (&use, pic_offset_table_rtx);
13367     }
13368
13369   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
13370     {
13371       rtx al = gen_rtx_REG (QImode, 0);
13372       emit_move_insn (al, callarg2);
13373       use_reg (&use, al);
13374     }
13375
13376   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
13377     {
13378       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13379       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13380     }
13381   if (sibcall && TARGET_64BIT
13382       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
13383     {
13384       rtx addr;
13385       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
13386       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
13387       emit_move_insn (fnaddr, addr);
13388       fnaddr = gen_rtx_MEM (QImode, fnaddr);
13389     }
13390
13391   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
13392   if (retval)
13393     call = gen_rtx_SET (VOIDmode, retval, call);
13394   if (pop)
13395     {
13396       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
13397       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
13398       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
13399     }
13400
13401   call = emit_call_insn (call);
13402   if (use)
13403     CALL_INSN_FUNCTION_USAGE (call) = use;
13404 }
13405
13406 \f
13407 /* Clear stack slot assignments remembered from previous functions.
13408    This is called from INIT_EXPANDERS once before RTL is emitted for each
13409    function.  */
13410
13411 static struct machine_function *
13412 ix86_init_machine_status (void)
13413 {
13414   struct machine_function *f;
13415
13416   f = ggc_alloc_cleared (sizeof (struct machine_function));
13417   f->use_fast_prologue_epilogue_nregs = -1;
13418   f->tls_descriptor_call_expanded_p = 0;
13419
13420   return f;
13421 }
13422
13423 /* Return a MEM corresponding to a stack slot with mode MODE.
13424    Allocate a new slot if necessary.
13425
13426    The RTL for a function can have several slots available: N is
13427    which slot to use.  */
13428
13429 rtx
13430 assign_386_stack_local (enum machine_mode mode, enum ix86_stack_slot n)
13431 {
13432   struct stack_local_entry *s;
13433
13434   gcc_assert (n < MAX_386_STACK_LOCALS);
13435
13436   for (s = ix86_stack_locals; s; s = s->next)
13437     if (s->mode == mode && s->n == n)
13438       return s->rtl;
13439
13440   s = (struct stack_local_entry *)
13441     ggc_alloc (sizeof (struct stack_local_entry));
13442   s->n = n;
13443   s->mode = mode;
13444   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
13445
13446   s->next = ix86_stack_locals;
13447   ix86_stack_locals = s;
13448   return s->rtl;
13449 }
13450
13451 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
13452
13453 static GTY(()) rtx ix86_tls_symbol;
13454 rtx
13455 ix86_tls_get_addr (void)
13456 {
13457
13458   if (!ix86_tls_symbol)
13459     {
13460       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
13461                                             (TARGET_ANY_GNU_TLS
13462                                              && !TARGET_64BIT)
13463                                             ? "___tls_get_addr"
13464                                             : "__tls_get_addr");
13465     }
13466
13467   return ix86_tls_symbol;
13468 }
13469
13470 /* Construct the SYMBOL_REF for the _TLS_MODULE_BASE_ symbol.  */
13471
13472 static GTY(()) rtx ix86_tls_module_base_symbol;
13473 rtx
13474 ix86_tls_module_base (void)
13475 {
13476
13477   if (!ix86_tls_module_base_symbol)
13478     {
13479       ix86_tls_module_base_symbol = gen_rtx_SYMBOL_REF (Pmode,
13480                                                         "_TLS_MODULE_BASE_");
13481       SYMBOL_REF_FLAGS (ix86_tls_module_base_symbol)
13482         |= TLS_MODEL_GLOBAL_DYNAMIC << SYMBOL_FLAG_TLS_SHIFT;
13483     }
13484
13485   return ix86_tls_module_base_symbol;
13486 }
13487 \f
13488 /* Calculate the length of the memory address in the instruction
13489    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
13490
13491 int
13492 memory_address_length (rtx addr)
13493 {
13494   struct ix86_address parts;
13495   rtx base, index, disp;
13496   int len;
13497   int ok;
13498
13499   if (GET_CODE (addr) == PRE_DEC
13500       || GET_CODE (addr) == POST_INC
13501       || GET_CODE (addr) == PRE_MODIFY
13502       || GET_CODE (addr) == POST_MODIFY)
13503     return 0;
13504
13505   ok = ix86_decompose_address (addr, &parts);
13506   gcc_assert (ok);
13507
13508   if (parts.base && GET_CODE (parts.base) == SUBREG)
13509     parts.base = SUBREG_REG (parts.base);
13510   if (parts.index && GET_CODE (parts.index) == SUBREG)
13511     parts.index = SUBREG_REG (parts.index);
13512
13513   base = parts.base;
13514   index = parts.index;
13515   disp = parts.disp;
13516   len = 0;
13517
13518   /* Rule of thumb:
13519        - esp as the base always wants an index,
13520        - ebp as the base always wants a displacement.  */
13521
13522   /* Register Indirect.  */
13523   if (base && !index && !disp)
13524     {
13525       /* esp (for its index) and ebp (for its displacement) need
13526          the two-byte modrm form.  */
13527       if (addr == stack_pointer_rtx
13528           || addr == arg_pointer_rtx
13529           || addr == frame_pointer_rtx
13530           || addr == hard_frame_pointer_rtx)
13531         len = 1;
13532     }
13533
13534   /* Direct Addressing.  */
13535   else if (disp && !base && !index)
13536     len = 4;
13537
13538   else
13539     {
13540       /* Find the length of the displacement constant.  */
13541       if (disp)
13542         {
13543           if (base && satisfies_constraint_K (disp))
13544             len = 1;
13545           else
13546             len = 4;
13547         }
13548       /* ebp always wants a displacement.  */
13549       else if (base == hard_frame_pointer_rtx)
13550         len = 1;
13551
13552       /* An index requires the two-byte modrm form....  */
13553       if (index
13554           /* ...like esp, which always wants an index.  */
13555           || base == stack_pointer_rtx
13556           || base == arg_pointer_rtx
13557           || base == frame_pointer_rtx)
13558         len += 1;
13559     }
13560
13561   return len;
13562 }
13563
13564 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
13565    is set, expect that insn have 8bit immediate alternative.  */
13566 int
13567 ix86_attr_length_immediate_default (rtx insn, int shortform)
13568 {
13569   int len = 0;
13570   int i;
13571   extract_insn_cached (insn);
13572   for (i = recog_data.n_operands - 1; i >= 0; --i)
13573     if (CONSTANT_P (recog_data.operand[i]))
13574       {
13575         gcc_assert (!len);
13576         if (shortform && satisfies_constraint_K (recog_data.operand[i]))
13577           len = 1;
13578         else
13579           {
13580             switch (get_attr_mode (insn))
13581               {
13582                 case MODE_QI:
13583                   len+=1;
13584                   break;
13585                 case MODE_HI:
13586                   len+=2;
13587                   break;
13588                 case MODE_SI:
13589                   len+=4;
13590                   break;
13591                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
13592                 case MODE_DI:
13593                   len+=4;
13594                   break;
13595                 default:
13596                   fatal_insn ("unknown insn mode", insn);
13597               }
13598           }
13599       }
13600   return len;
13601 }
13602 /* Compute default value for "length_address" attribute.  */
13603 int
13604 ix86_attr_length_address_default (rtx insn)
13605 {
13606   int i;
13607
13608   if (get_attr_type (insn) == TYPE_LEA)
13609     {
13610       rtx set = PATTERN (insn);
13611
13612       if (GET_CODE (set) == PARALLEL)
13613         set = XVECEXP (set, 0, 0);
13614
13615       gcc_assert (GET_CODE (set) == SET);
13616
13617       return memory_address_length (SET_SRC (set));
13618     }
13619
13620   extract_insn_cached (insn);
13621   for (i = recog_data.n_operands - 1; i >= 0; --i)
13622     if (GET_CODE (recog_data.operand[i]) == MEM)
13623       {
13624         return memory_address_length (XEXP (recog_data.operand[i], 0));
13625         break;
13626       }
13627   return 0;
13628 }
13629 \f
13630 /* Return the maximum number of instructions a cpu can issue.  */
13631
13632 static int
13633 ix86_issue_rate (void)
13634 {
13635   switch (ix86_tune)
13636     {
13637     case PROCESSOR_PENTIUM:
13638     case PROCESSOR_K6:
13639       return 2;
13640
13641     case PROCESSOR_PENTIUMPRO:
13642     case PROCESSOR_PENTIUM4:
13643     case PROCESSOR_ATHLON:
13644     case PROCESSOR_K8:
13645     case PROCESSOR_NOCONA:
13646     case PROCESSOR_GENERIC32:
13647     case PROCESSOR_GENERIC64:
13648       return 3;
13649
13650     default:
13651       return 1;
13652     }
13653 }
13654
13655 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
13656    by DEP_INSN and nothing set by DEP_INSN.  */
13657
13658 static int
13659 ix86_flags_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
13660 {
13661   rtx set, set2;
13662
13663   /* Simplify the test for uninteresting insns.  */
13664   if (insn_type != TYPE_SETCC
13665       && insn_type != TYPE_ICMOV
13666       && insn_type != TYPE_FCMOV
13667       && insn_type != TYPE_IBR)
13668     return 0;
13669
13670   if ((set = single_set (dep_insn)) != 0)
13671     {
13672       set = SET_DEST (set);
13673       set2 = NULL_RTX;
13674     }
13675   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
13676            && XVECLEN (PATTERN (dep_insn), 0) == 2
13677            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
13678            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
13679     {
13680       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13681       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
13682     }
13683   else
13684     return 0;
13685
13686   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
13687     return 0;
13688
13689   /* This test is true if the dependent insn reads the flags but
13690      not any other potentially set register.  */
13691   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
13692     return 0;
13693
13694   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
13695     return 0;
13696
13697   return 1;
13698 }
13699
13700 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
13701    address with operands set by DEP_INSN.  */
13702
13703 static int
13704 ix86_agi_dependent (rtx insn, rtx dep_insn, enum attr_type insn_type)
13705 {
13706   rtx addr;
13707
13708   if (insn_type == TYPE_LEA
13709       && TARGET_PENTIUM)
13710     {
13711       addr = PATTERN (insn);
13712
13713       if (GET_CODE (addr) == PARALLEL)
13714         addr = XVECEXP (addr, 0, 0);
13715
13716       gcc_assert (GET_CODE (addr) == SET);
13717
13718       addr = SET_SRC (addr);
13719     }
13720   else
13721     {
13722       int i;
13723       extract_insn_cached (insn);
13724       for (i = recog_data.n_operands - 1; i >= 0; --i)
13725         if (GET_CODE (recog_data.operand[i]) == MEM)
13726           {
13727             addr = XEXP (recog_data.operand[i], 0);
13728             goto found;
13729           }
13730       return 0;
13731     found:;
13732     }
13733
13734   return modified_in_p (addr, dep_insn);
13735 }
13736
13737 static int
13738 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
13739 {
13740   enum attr_type insn_type, dep_insn_type;
13741   enum attr_memory memory;
13742   rtx set, set2;
13743   int dep_insn_code_number;
13744
13745   /* Anti and output dependencies have zero cost on all CPUs.  */
13746   if (REG_NOTE_KIND (link) != 0)
13747     return 0;
13748
13749   dep_insn_code_number = recog_memoized (dep_insn);
13750
13751   /* If we can't recognize the insns, we can't really do anything.  */
13752   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
13753     return cost;
13754
13755   insn_type = get_attr_type (insn);
13756   dep_insn_type = get_attr_type (dep_insn);
13757
13758   switch (ix86_tune)
13759     {
13760     case PROCESSOR_PENTIUM:
13761       /* Address Generation Interlock adds a cycle of latency.  */
13762       if (ix86_agi_dependent (insn, dep_insn, insn_type))
13763         cost += 1;
13764
13765       /* ??? Compares pair with jump/setcc.  */
13766       if (ix86_flags_dependent (insn, dep_insn, insn_type))
13767         cost = 0;
13768
13769       /* Floating point stores require value to be ready one cycle earlier.  */
13770       if (insn_type == TYPE_FMOV
13771           && get_attr_memory (insn) == MEMORY_STORE
13772           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13773         cost += 1;
13774       break;
13775
13776     case PROCESSOR_PENTIUMPRO:
13777       memory = get_attr_memory (insn);
13778
13779       /* INT->FP conversion is expensive.  */
13780       if (get_attr_fp_int_src (dep_insn))
13781         cost += 5;
13782
13783       /* There is one cycle extra latency between an FP op and a store.  */
13784       if (insn_type == TYPE_FMOV
13785           && (set = single_set (dep_insn)) != NULL_RTX
13786           && (set2 = single_set (insn)) != NULL_RTX
13787           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
13788           && GET_CODE (SET_DEST (set2)) == MEM)
13789         cost += 1;
13790
13791       /* Show ability of reorder buffer to hide latency of load by executing
13792          in parallel with previous instruction in case
13793          previous instruction is not needed to compute the address.  */
13794       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13795           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13796         {
13797           /* Claim moves to take one cycle, as core can issue one load
13798              at time and the next load can start cycle later.  */
13799           if (dep_insn_type == TYPE_IMOV
13800               || dep_insn_type == TYPE_FMOV)
13801             cost = 1;
13802           else if (cost > 1)
13803             cost--;
13804         }
13805       break;
13806
13807     case PROCESSOR_K6:
13808       memory = get_attr_memory (insn);
13809
13810       /* The esp dependency is resolved before the instruction is really
13811          finished.  */
13812       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
13813           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
13814         return 1;
13815
13816       /* INT->FP conversion is expensive.  */
13817       if (get_attr_fp_int_src (dep_insn))
13818         cost += 5;
13819
13820       /* Show ability of reorder buffer to hide latency of load by executing
13821          in parallel with previous instruction in case
13822          previous instruction is not needed to compute the address.  */
13823       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13824           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13825         {
13826           /* Claim moves to take one cycle, as core can issue one load
13827              at time and the next load can start cycle later.  */
13828           if (dep_insn_type == TYPE_IMOV
13829               || dep_insn_type == TYPE_FMOV)
13830             cost = 1;
13831           else if (cost > 2)
13832             cost -= 2;
13833           else
13834             cost = 1;
13835         }
13836       break;
13837
13838     case PROCESSOR_ATHLON:
13839     case PROCESSOR_K8:
13840     case PROCESSOR_GENERIC32:
13841     case PROCESSOR_GENERIC64:
13842       memory = get_attr_memory (insn);
13843
13844       /* Show ability of reorder buffer to hide latency of load by executing
13845          in parallel with previous instruction in case
13846          previous instruction is not needed to compute the address.  */
13847       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
13848           && !ix86_agi_dependent (insn, dep_insn, insn_type))
13849         {
13850           enum attr_unit unit = get_attr_unit (insn);
13851           int loadcost = 3;
13852
13853           /* Because of the difference between the length of integer and
13854              floating unit pipeline preparation stages, the memory operands
13855              for floating point are cheaper.
13856
13857              ??? For Athlon it the difference is most probably 2.  */
13858           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
13859             loadcost = 3;
13860           else
13861             loadcost = TARGET_ATHLON ? 2 : 0;
13862
13863           if (cost >= loadcost)
13864             cost -= loadcost;
13865           else
13866             cost = 0;
13867         }
13868
13869     default:
13870       break;
13871     }
13872
13873   return cost;
13874 }
13875
13876 /* How many alternative schedules to try.  This should be as wide as the
13877    scheduling freedom in the DFA, but no wider.  Making this value too
13878    large results extra work for the scheduler.  */
13879
13880 static int
13881 ia32_multipass_dfa_lookahead (void)
13882 {
13883   if (ix86_tune == PROCESSOR_PENTIUM)
13884     return 2;
13885
13886   if (ix86_tune == PROCESSOR_PENTIUMPRO
13887       || ix86_tune == PROCESSOR_K6)
13888     return 1;
13889
13890   else
13891     return 0;
13892 }
13893
13894 \f
13895 /* Compute the alignment given to a constant that is being placed in memory.
13896    EXP is the constant and ALIGN is the alignment that the object would
13897    ordinarily have.
13898    The value of this function is used instead of that alignment to align
13899    the object.  */
13900
13901 int
13902 ix86_constant_alignment (tree exp, int align)
13903 {
13904   if (TREE_CODE (exp) == REAL_CST)
13905     {
13906       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
13907         return 64;
13908       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
13909         return 128;
13910     }
13911   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
13912            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
13913     return BITS_PER_WORD;
13914
13915   return align;
13916 }
13917
13918 /* Compute the alignment for a static variable.
13919    TYPE is the data type, and ALIGN is the alignment that
13920    the object would ordinarily have.  The value of this function is used
13921    instead of that alignment to align the object.  */
13922
13923 int
13924 ix86_data_alignment (tree type, int align)
13925 {
13926   int max_align = optimize_size ? BITS_PER_WORD : 256;
13927
13928   if (AGGREGATE_TYPE_P (type)
13929       && TYPE_SIZE (type)
13930       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13931       && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= (unsigned) max_align
13932           || TREE_INT_CST_HIGH (TYPE_SIZE (type)))
13933       && align < max_align)
13934     align = max_align;
13935
13936   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13937      to 16byte boundary.  */
13938   if (TARGET_64BIT)
13939     {
13940       if (AGGREGATE_TYPE_P (type)
13941            && TYPE_SIZE (type)
13942            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
13943            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
13944                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
13945         return 128;
13946     }
13947
13948   if (TREE_CODE (type) == ARRAY_TYPE)
13949     {
13950       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
13951         return 64;
13952       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
13953         return 128;
13954     }
13955   else if (TREE_CODE (type) == COMPLEX_TYPE)
13956     {
13957
13958       if (TYPE_MODE (type) == DCmode && align < 64)
13959         return 64;
13960       if (TYPE_MODE (type) == XCmode && align < 128)
13961         return 128;
13962     }
13963   else if ((TREE_CODE (type) == RECORD_TYPE
13964             || TREE_CODE (type) == UNION_TYPE
13965             || TREE_CODE (type) == QUAL_UNION_TYPE)
13966            && TYPE_FIELDS (type))
13967     {
13968       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
13969         return 64;
13970       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
13971         return 128;
13972     }
13973   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
13974            || TREE_CODE (type) == INTEGER_TYPE)
13975     {
13976       if (TYPE_MODE (type) == DFmode && align < 64)
13977         return 64;
13978       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
13979         return 128;
13980     }
13981
13982   return align;
13983 }
13984
13985 /* Compute the alignment for a local variable.
13986    TYPE is the data type, and ALIGN is the alignment that
13987    the object would ordinarily have.  The value of this macro is used
13988    instead of that alignment to align the object.  */
13989
13990 int
13991 ix86_local_alignment (tree type, int align)
13992 {
13993   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
13994      to 16byte boundary.  */
13995   if (TARGET_64BIT)
13996     {
13997       if (AGGREGATE_TYPE_P (type)
13998            && TYPE_SIZE (type)
13999            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
14000            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
14001                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
14002         return 128;
14003     }
14004   if (TREE_CODE (type) == ARRAY_TYPE)
14005     {
14006       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
14007         return 64;
14008       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
14009         return 128;
14010     }
14011   else if (TREE_CODE (type) == COMPLEX_TYPE)
14012     {
14013       if (TYPE_MODE (type) == DCmode && align < 64)
14014         return 64;
14015       if (TYPE_MODE (type) == XCmode && align < 128)
14016         return 128;
14017     }
14018   else if ((TREE_CODE (type) == RECORD_TYPE
14019             || TREE_CODE (type) == UNION_TYPE
14020             || TREE_CODE (type) == QUAL_UNION_TYPE)
14021            && TYPE_FIELDS (type))
14022     {
14023       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
14024         return 64;
14025       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
14026         return 128;
14027     }
14028   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
14029            || TREE_CODE (type) == INTEGER_TYPE)
14030     {
14031
14032       if (TYPE_MODE (type) == DFmode && align < 64)
14033         return 64;
14034       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
14035         return 128;
14036     }
14037   return align;
14038 }
14039 \f
14040 /* Emit RTL insns to initialize the variable parts of a trampoline.
14041    FNADDR is an RTX for the address of the function's pure code.
14042    CXT is an RTX for the static chain value for the function.  */
14043 void
14044 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
14045 {
14046   if (!TARGET_64BIT)
14047     {
14048       /* Compute offset from the end of the jmp to the target function.  */
14049       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
14050                                plus_constant (tramp, 10),
14051                                NULL_RTX, 1, OPTAB_DIRECT);
14052       emit_move_insn (gen_rtx_MEM (QImode, tramp),
14053                       gen_int_mode (0xb9, QImode));
14054       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
14055       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
14056                       gen_int_mode (0xe9, QImode));
14057       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
14058     }
14059   else
14060     {
14061       int offset = 0;
14062       /* Try to load address using shorter movl instead of movabs.
14063          We may want to support movq for kernel mode, but kernel does not use
14064          trampolines at the moment.  */
14065       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
14066         {
14067           fnaddr = copy_to_mode_reg (DImode, fnaddr);
14068           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14069                           gen_int_mode (0xbb41, HImode));
14070           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
14071                           gen_lowpart (SImode, fnaddr));
14072           offset += 6;
14073         }
14074       else
14075         {
14076           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14077                           gen_int_mode (0xbb49, HImode));
14078           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
14079                           fnaddr);
14080           offset += 10;
14081         }
14082       /* Load static chain using movabs to r10.  */
14083       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14084                       gen_int_mode (0xba49, HImode));
14085       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
14086                       cxt);
14087       offset += 10;
14088       /* Jump to the r11 */
14089       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
14090                       gen_int_mode (0xff49, HImode));
14091       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
14092                       gen_int_mode (0xe3, QImode));
14093       offset += 3;
14094       gcc_assert (offset <= TRAMPOLINE_SIZE);
14095     }
14096
14097 #ifdef ENABLE_EXECUTE_STACK
14098   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
14099                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
14100 #endif
14101 }
14102 \f
14103 /* Codes for all the SSE/MMX builtins.  */
14104 enum ix86_builtins
14105 {
14106   IX86_BUILTIN_ADDPS,
14107   IX86_BUILTIN_ADDSS,
14108   IX86_BUILTIN_DIVPS,
14109   IX86_BUILTIN_DIVSS,
14110   IX86_BUILTIN_MULPS,
14111   IX86_BUILTIN_MULSS,
14112   IX86_BUILTIN_SUBPS,
14113   IX86_BUILTIN_SUBSS,
14114
14115   IX86_BUILTIN_CMPEQPS,
14116   IX86_BUILTIN_CMPLTPS,
14117   IX86_BUILTIN_CMPLEPS,
14118   IX86_BUILTIN_CMPGTPS,
14119   IX86_BUILTIN_CMPGEPS,
14120   IX86_BUILTIN_CMPNEQPS,
14121   IX86_BUILTIN_CMPNLTPS,
14122   IX86_BUILTIN_CMPNLEPS,
14123   IX86_BUILTIN_CMPNGTPS,
14124   IX86_BUILTIN_CMPNGEPS,
14125   IX86_BUILTIN_CMPORDPS,
14126   IX86_BUILTIN_CMPUNORDPS,
14127   IX86_BUILTIN_CMPEQSS,
14128   IX86_BUILTIN_CMPLTSS,
14129   IX86_BUILTIN_CMPLESS,
14130   IX86_BUILTIN_CMPNEQSS,
14131   IX86_BUILTIN_CMPNLTSS,
14132   IX86_BUILTIN_CMPNLESS,
14133   IX86_BUILTIN_CMPNGTSS,
14134   IX86_BUILTIN_CMPNGESS,
14135   IX86_BUILTIN_CMPORDSS,
14136   IX86_BUILTIN_CMPUNORDSS,
14137
14138   IX86_BUILTIN_COMIEQSS,
14139   IX86_BUILTIN_COMILTSS,
14140   IX86_BUILTIN_COMILESS,
14141   IX86_BUILTIN_COMIGTSS,
14142   IX86_BUILTIN_COMIGESS,
14143   IX86_BUILTIN_COMINEQSS,
14144   IX86_BUILTIN_UCOMIEQSS,
14145   IX86_BUILTIN_UCOMILTSS,
14146   IX86_BUILTIN_UCOMILESS,
14147   IX86_BUILTIN_UCOMIGTSS,
14148   IX86_BUILTIN_UCOMIGESS,
14149   IX86_BUILTIN_UCOMINEQSS,
14150
14151   IX86_BUILTIN_CVTPI2PS,
14152   IX86_BUILTIN_CVTPS2PI,
14153   IX86_BUILTIN_CVTSI2SS,
14154   IX86_BUILTIN_CVTSI642SS,
14155   IX86_BUILTIN_CVTSS2SI,
14156   IX86_BUILTIN_CVTSS2SI64,
14157   IX86_BUILTIN_CVTTPS2PI,
14158   IX86_BUILTIN_CVTTSS2SI,
14159   IX86_BUILTIN_CVTTSS2SI64,
14160
14161   IX86_BUILTIN_MAXPS,
14162   IX86_BUILTIN_MAXSS,
14163   IX86_BUILTIN_MINPS,
14164   IX86_BUILTIN_MINSS,
14165
14166   IX86_BUILTIN_LOADUPS,
14167   IX86_BUILTIN_STOREUPS,
14168   IX86_BUILTIN_MOVSS,
14169
14170   IX86_BUILTIN_MOVHLPS,
14171   IX86_BUILTIN_MOVLHPS,
14172   IX86_BUILTIN_LOADHPS,
14173   IX86_BUILTIN_LOADLPS,
14174   IX86_BUILTIN_STOREHPS,
14175   IX86_BUILTIN_STORELPS,
14176
14177   IX86_BUILTIN_MASKMOVQ,
14178   IX86_BUILTIN_MOVMSKPS,
14179   IX86_BUILTIN_PMOVMSKB,
14180
14181   IX86_BUILTIN_MOVNTPS,
14182   IX86_BUILTIN_MOVNTQ,
14183
14184   IX86_BUILTIN_LOADDQU,
14185   IX86_BUILTIN_STOREDQU,
14186
14187   IX86_BUILTIN_PACKSSWB,
14188   IX86_BUILTIN_PACKSSDW,
14189   IX86_BUILTIN_PACKUSWB,
14190
14191   IX86_BUILTIN_PADDB,
14192   IX86_BUILTIN_PADDW,
14193   IX86_BUILTIN_PADDD,
14194   IX86_BUILTIN_PADDQ,
14195   IX86_BUILTIN_PADDSB,
14196   IX86_BUILTIN_PADDSW,
14197   IX86_BUILTIN_PADDUSB,
14198   IX86_BUILTIN_PADDUSW,
14199   IX86_BUILTIN_PSUBB,
14200   IX86_BUILTIN_PSUBW,
14201   IX86_BUILTIN_PSUBD,
14202   IX86_BUILTIN_PSUBQ,
14203   IX86_BUILTIN_PSUBSB,
14204   IX86_BUILTIN_PSUBSW,
14205   IX86_BUILTIN_PSUBUSB,
14206   IX86_BUILTIN_PSUBUSW,
14207
14208   IX86_BUILTIN_PAND,
14209   IX86_BUILTIN_PANDN,
14210   IX86_BUILTIN_POR,
14211   IX86_BUILTIN_PXOR,
14212
14213   IX86_BUILTIN_PAVGB,
14214   IX86_BUILTIN_PAVGW,
14215
14216   IX86_BUILTIN_PCMPEQB,
14217   IX86_BUILTIN_PCMPEQW,
14218   IX86_BUILTIN_PCMPEQD,
14219   IX86_BUILTIN_PCMPGTB,
14220   IX86_BUILTIN_PCMPGTW,
14221   IX86_BUILTIN_PCMPGTD,
14222
14223   IX86_BUILTIN_PMADDWD,
14224
14225   IX86_BUILTIN_PMAXSW,
14226   IX86_BUILTIN_PMAXUB,
14227   IX86_BUILTIN_PMINSW,
14228   IX86_BUILTIN_PMINUB,
14229
14230   IX86_BUILTIN_PMULHUW,
14231   IX86_BUILTIN_PMULHW,
14232   IX86_BUILTIN_PMULLW,
14233
14234   IX86_BUILTIN_PSADBW,
14235   IX86_BUILTIN_PSHUFW,
14236
14237   IX86_BUILTIN_PSLLW,
14238   IX86_BUILTIN_PSLLD,
14239   IX86_BUILTIN_PSLLQ,
14240   IX86_BUILTIN_PSRAW,
14241   IX86_BUILTIN_PSRAD,
14242   IX86_BUILTIN_PSRLW,
14243   IX86_BUILTIN_PSRLD,
14244   IX86_BUILTIN_PSRLQ,
14245   IX86_BUILTIN_PSLLWI,
14246   IX86_BUILTIN_PSLLDI,
14247   IX86_BUILTIN_PSLLQI,
14248   IX86_BUILTIN_PSRAWI,
14249   IX86_BUILTIN_PSRADI,
14250   IX86_BUILTIN_PSRLWI,
14251   IX86_BUILTIN_PSRLDI,
14252   IX86_BUILTIN_PSRLQI,
14253
14254   IX86_BUILTIN_PUNPCKHBW,
14255   IX86_BUILTIN_PUNPCKHWD,
14256   IX86_BUILTIN_PUNPCKHDQ,
14257   IX86_BUILTIN_PUNPCKLBW,
14258   IX86_BUILTIN_PUNPCKLWD,
14259   IX86_BUILTIN_PUNPCKLDQ,
14260
14261   IX86_BUILTIN_SHUFPS,
14262
14263   IX86_BUILTIN_RCPPS,
14264   IX86_BUILTIN_RCPSS,
14265   IX86_BUILTIN_RSQRTPS,
14266   IX86_BUILTIN_RSQRTSS,
14267   IX86_BUILTIN_SQRTPS,
14268   IX86_BUILTIN_SQRTSS,
14269
14270   IX86_BUILTIN_UNPCKHPS,
14271   IX86_BUILTIN_UNPCKLPS,
14272
14273   IX86_BUILTIN_ANDPS,
14274   IX86_BUILTIN_ANDNPS,
14275   IX86_BUILTIN_ORPS,
14276   IX86_BUILTIN_XORPS,
14277
14278   IX86_BUILTIN_EMMS,
14279   IX86_BUILTIN_LDMXCSR,
14280   IX86_BUILTIN_STMXCSR,
14281   IX86_BUILTIN_SFENCE,
14282
14283   /* 3DNow! Original */
14284   IX86_BUILTIN_FEMMS,
14285   IX86_BUILTIN_PAVGUSB,
14286   IX86_BUILTIN_PF2ID,
14287   IX86_BUILTIN_PFACC,
14288   IX86_BUILTIN_PFADD,
14289   IX86_BUILTIN_PFCMPEQ,
14290   IX86_BUILTIN_PFCMPGE,
14291   IX86_BUILTIN_PFCMPGT,
14292   IX86_BUILTIN_PFMAX,
14293   IX86_BUILTIN_PFMIN,
14294   IX86_BUILTIN_PFMUL,
14295   IX86_BUILTIN_PFRCP,
14296   IX86_BUILTIN_PFRCPIT1,
14297   IX86_BUILTIN_PFRCPIT2,
14298   IX86_BUILTIN_PFRSQIT1,
14299   IX86_BUILTIN_PFRSQRT,
14300   IX86_BUILTIN_PFSUB,
14301   IX86_BUILTIN_PFSUBR,
14302   IX86_BUILTIN_PI2FD,
14303   IX86_BUILTIN_PMULHRW,
14304
14305   /* 3DNow! Athlon Extensions */
14306   IX86_BUILTIN_PF2IW,
14307   IX86_BUILTIN_PFNACC,
14308   IX86_BUILTIN_PFPNACC,
14309   IX86_BUILTIN_PI2FW,
14310   IX86_BUILTIN_PSWAPDSI,
14311   IX86_BUILTIN_PSWAPDSF,
14312
14313   /* SSE2 */
14314   IX86_BUILTIN_ADDPD,
14315   IX86_BUILTIN_ADDSD,
14316   IX86_BUILTIN_DIVPD,
14317   IX86_BUILTIN_DIVSD,
14318   IX86_BUILTIN_MULPD,
14319   IX86_BUILTIN_MULSD,
14320   IX86_BUILTIN_SUBPD,
14321   IX86_BUILTIN_SUBSD,
14322
14323   IX86_BUILTIN_CMPEQPD,
14324   IX86_BUILTIN_CMPLTPD,
14325   IX86_BUILTIN_CMPLEPD,
14326   IX86_BUILTIN_CMPGTPD,
14327   IX86_BUILTIN_CMPGEPD,
14328   IX86_BUILTIN_CMPNEQPD,
14329   IX86_BUILTIN_CMPNLTPD,
14330   IX86_BUILTIN_CMPNLEPD,
14331   IX86_BUILTIN_CMPNGTPD,
14332   IX86_BUILTIN_CMPNGEPD,
14333   IX86_BUILTIN_CMPORDPD,
14334   IX86_BUILTIN_CMPUNORDPD,
14335   IX86_BUILTIN_CMPNEPD,
14336   IX86_BUILTIN_CMPEQSD,
14337   IX86_BUILTIN_CMPLTSD,
14338   IX86_BUILTIN_CMPLESD,
14339   IX86_BUILTIN_CMPNEQSD,
14340   IX86_BUILTIN_CMPNLTSD,
14341   IX86_BUILTIN_CMPNLESD,
14342   IX86_BUILTIN_CMPORDSD,
14343   IX86_BUILTIN_CMPUNORDSD,
14344   IX86_BUILTIN_CMPNESD,
14345
14346   IX86_BUILTIN_COMIEQSD,
14347   IX86_BUILTIN_COMILTSD,
14348   IX86_BUILTIN_COMILESD,
14349   IX86_BUILTIN_COMIGTSD,
14350   IX86_BUILTIN_COMIGESD,
14351   IX86_BUILTIN_COMINEQSD,
14352   IX86_BUILTIN_UCOMIEQSD,
14353   IX86_BUILTIN_UCOMILTSD,
14354   IX86_BUILTIN_UCOMILESD,
14355   IX86_BUILTIN_UCOMIGTSD,
14356   IX86_BUILTIN_UCOMIGESD,
14357   IX86_BUILTIN_UCOMINEQSD,
14358
14359   IX86_BUILTIN_MAXPD,
14360   IX86_BUILTIN_MAXSD,
14361   IX86_BUILTIN_MINPD,
14362   IX86_BUILTIN_MINSD,
14363
14364   IX86_BUILTIN_ANDPD,
14365   IX86_BUILTIN_ANDNPD,
14366   IX86_BUILTIN_ORPD,
14367   IX86_BUILTIN_XORPD,
14368
14369   IX86_BUILTIN_SQRTPD,
14370   IX86_BUILTIN_SQRTSD,
14371
14372   IX86_BUILTIN_UNPCKHPD,
14373   IX86_BUILTIN_UNPCKLPD,
14374
14375   IX86_BUILTIN_SHUFPD,
14376
14377   IX86_BUILTIN_LOADUPD,
14378   IX86_BUILTIN_STOREUPD,
14379   IX86_BUILTIN_MOVSD,
14380
14381   IX86_BUILTIN_LOADHPD,
14382   IX86_BUILTIN_LOADLPD,
14383
14384   IX86_BUILTIN_CVTDQ2PD,
14385   IX86_BUILTIN_CVTDQ2PS,
14386
14387   IX86_BUILTIN_CVTPD2DQ,
14388   IX86_BUILTIN_CVTPD2PI,
14389   IX86_BUILTIN_CVTPD2PS,
14390   IX86_BUILTIN_CVTTPD2DQ,
14391   IX86_BUILTIN_CVTTPD2PI,
14392
14393   IX86_BUILTIN_CVTPI2PD,
14394   IX86_BUILTIN_CVTSI2SD,
14395   IX86_BUILTIN_CVTSI642SD,
14396
14397   IX86_BUILTIN_CVTSD2SI,
14398   IX86_BUILTIN_CVTSD2SI64,
14399   IX86_BUILTIN_CVTSD2SS,
14400   IX86_BUILTIN_CVTSS2SD,
14401   IX86_BUILTIN_CVTTSD2SI,
14402   IX86_BUILTIN_CVTTSD2SI64,
14403
14404   IX86_BUILTIN_CVTPS2DQ,
14405   IX86_BUILTIN_CVTPS2PD,
14406   IX86_BUILTIN_CVTTPS2DQ,
14407
14408   IX86_BUILTIN_MOVNTI,
14409   IX86_BUILTIN_MOVNTPD,
14410   IX86_BUILTIN_MOVNTDQ,
14411
14412   /* SSE2 MMX */
14413   IX86_BUILTIN_MASKMOVDQU,
14414   IX86_BUILTIN_MOVMSKPD,
14415   IX86_BUILTIN_PMOVMSKB128,
14416
14417   IX86_BUILTIN_PACKSSWB128,
14418   IX86_BUILTIN_PACKSSDW128,
14419   IX86_BUILTIN_PACKUSWB128,
14420
14421   IX86_BUILTIN_PADDB128,
14422   IX86_BUILTIN_PADDW128,
14423   IX86_BUILTIN_PADDD128,
14424   IX86_BUILTIN_PADDQ128,
14425   IX86_BUILTIN_PADDSB128,
14426   IX86_BUILTIN_PADDSW128,
14427   IX86_BUILTIN_PADDUSB128,
14428   IX86_BUILTIN_PADDUSW128,
14429   IX86_BUILTIN_PSUBB128,
14430   IX86_BUILTIN_PSUBW128,
14431   IX86_BUILTIN_PSUBD128,
14432   IX86_BUILTIN_PSUBQ128,
14433   IX86_BUILTIN_PSUBSB128,
14434   IX86_BUILTIN_PSUBSW128,
14435   IX86_BUILTIN_PSUBUSB128,
14436   IX86_BUILTIN_PSUBUSW128,
14437
14438   IX86_BUILTIN_PAND128,
14439   IX86_BUILTIN_PANDN128,
14440   IX86_BUILTIN_POR128,
14441   IX86_BUILTIN_PXOR128,
14442
14443   IX86_BUILTIN_PAVGB128,
14444   IX86_BUILTIN_PAVGW128,
14445
14446   IX86_BUILTIN_PCMPEQB128,
14447   IX86_BUILTIN_PCMPEQW128,
14448   IX86_BUILTIN_PCMPEQD128,
14449   IX86_BUILTIN_PCMPGTB128,
14450   IX86_BUILTIN_PCMPGTW128,
14451   IX86_BUILTIN_PCMPGTD128,
14452
14453   IX86_BUILTIN_PMADDWD128,
14454
14455   IX86_BUILTIN_PMAXSW128,
14456   IX86_BUILTIN_PMAXUB128,
14457   IX86_BUILTIN_PMINSW128,
14458   IX86_BUILTIN_PMINUB128,
14459
14460   IX86_BUILTIN_PMULUDQ,
14461   IX86_BUILTIN_PMULUDQ128,
14462   IX86_BUILTIN_PMULHUW128,
14463   IX86_BUILTIN_PMULHW128,
14464   IX86_BUILTIN_PMULLW128,
14465
14466   IX86_BUILTIN_PSADBW128,
14467   IX86_BUILTIN_PSHUFHW,
14468   IX86_BUILTIN_PSHUFLW,
14469   IX86_BUILTIN_PSHUFD,
14470
14471   IX86_BUILTIN_PSLLW128,
14472   IX86_BUILTIN_PSLLD128,
14473   IX86_BUILTIN_PSLLQ128,
14474   IX86_BUILTIN_PSRAW128,
14475   IX86_BUILTIN_PSRAD128,
14476   IX86_BUILTIN_PSRLW128,
14477   IX86_BUILTIN_PSRLD128,
14478   IX86_BUILTIN_PSRLQ128,
14479   IX86_BUILTIN_PSLLDQI128,
14480   IX86_BUILTIN_PSLLWI128,
14481   IX86_BUILTIN_PSLLDI128,
14482   IX86_BUILTIN_PSLLQI128,
14483   IX86_BUILTIN_PSRAWI128,
14484   IX86_BUILTIN_PSRADI128,
14485   IX86_BUILTIN_PSRLDQI128,
14486   IX86_BUILTIN_PSRLWI128,
14487   IX86_BUILTIN_PSRLDI128,
14488   IX86_BUILTIN_PSRLQI128,
14489
14490   IX86_BUILTIN_PUNPCKHBW128,
14491   IX86_BUILTIN_PUNPCKHWD128,
14492   IX86_BUILTIN_PUNPCKHDQ128,
14493   IX86_BUILTIN_PUNPCKHQDQ128,
14494   IX86_BUILTIN_PUNPCKLBW128,
14495   IX86_BUILTIN_PUNPCKLWD128,
14496   IX86_BUILTIN_PUNPCKLDQ128,
14497   IX86_BUILTIN_PUNPCKLQDQ128,
14498
14499   IX86_BUILTIN_CLFLUSH,
14500   IX86_BUILTIN_MFENCE,
14501   IX86_BUILTIN_LFENCE,
14502
14503   /* Prescott New Instructions.  */
14504   IX86_BUILTIN_ADDSUBPS,
14505   IX86_BUILTIN_HADDPS,
14506   IX86_BUILTIN_HSUBPS,
14507   IX86_BUILTIN_MOVSHDUP,
14508   IX86_BUILTIN_MOVSLDUP,
14509   IX86_BUILTIN_ADDSUBPD,
14510   IX86_BUILTIN_HADDPD,
14511   IX86_BUILTIN_HSUBPD,
14512   IX86_BUILTIN_LDDQU,
14513
14514   IX86_BUILTIN_MONITOR,
14515   IX86_BUILTIN_MWAIT,
14516
14517   IX86_BUILTIN_VEC_INIT_V2SI,
14518   IX86_BUILTIN_VEC_INIT_V4HI,
14519   IX86_BUILTIN_VEC_INIT_V8QI,
14520   IX86_BUILTIN_VEC_EXT_V2DF,
14521   IX86_BUILTIN_VEC_EXT_V2DI,
14522   IX86_BUILTIN_VEC_EXT_V4SF,
14523   IX86_BUILTIN_VEC_EXT_V4SI,
14524   IX86_BUILTIN_VEC_EXT_V8HI,
14525   IX86_BUILTIN_VEC_EXT_V2SI,
14526   IX86_BUILTIN_VEC_EXT_V4HI,
14527   IX86_BUILTIN_VEC_SET_V8HI,
14528   IX86_BUILTIN_VEC_SET_V4HI,
14529
14530   IX86_BUILTIN_MAX
14531 };
14532
14533 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
14534 do {                                                                    \
14535   if ((MASK) & target_flags                                             \
14536       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
14537     lang_hooks.builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,   \
14538                                  NULL, NULL_TREE);                      \
14539 } while (0)
14540
14541 /* Bits for builtin_description.flag.  */
14542
14543 /* Set when we don't support the comparison natively, and should
14544    swap_comparison in order to support it.  */
14545 #define BUILTIN_DESC_SWAP_OPERANDS      1
14546
14547 struct builtin_description
14548 {
14549   const unsigned int mask;
14550   const enum insn_code icode;
14551   const char *const name;
14552   const enum ix86_builtins code;
14553   const enum rtx_code comparison;
14554   const unsigned int flag;
14555 };
14556
14557 static const struct builtin_description bdesc_comi[] =
14558 {
14559   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
14560   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
14561   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
14562   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
14563   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
14564   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
14565   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
14566   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
14567   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
14568   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
14569   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
14570   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
14571   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
14572   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
14573   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
14574   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
14575   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
14576   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
14577   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
14578   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
14579   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
14580   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
14581   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
14582   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
14583 };
14584
14585 static const struct builtin_description bdesc_2arg[] =
14586 {
14587   /* SSE */
14588   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
14589   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
14590   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
14591   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
14592   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
14593   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
14594   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
14595   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
14596
14597   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
14598   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
14599   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
14600   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
14601     BUILTIN_DESC_SWAP_OPERANDS },
14602   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
14603     BUILTIN_DESC_SWAP_OPERANDS },
14604   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
14605   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
14606   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
14607   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
14608   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
14609     BUILTIN_DESC_SWAP_OPERANDS },
14610   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
14611     BUILTIN_DESC_SWAP_OPERANDS },
14612   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
14613   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
14614   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
14615   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
14616   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
14617   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
14618   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
14619   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
14620   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
14621     BUILTIN_DESC_SWAP_OPERANDS },
14622   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
14623     BUILTIN_DESC_SWAP_OPERANDS },
14624   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
14625
14626   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
14627   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
14628   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
14629   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
14630
14631   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
14632   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
14633   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
14634   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
14635
14636   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
14637   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
14638   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
14639   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
14640   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
14641
14642   /* MMX */
14643   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
14644   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
14645   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
14646   { MASK_SSE2, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
14647   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
14648   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
14649   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
14650   { MASK_SSE2, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
14651
14652   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
14653   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
14654   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
14655   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
14656   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
14657   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
14658   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
14659   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
14660
14661   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
14662   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
14663   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
14664
14665   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
14666   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
14667   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
14668   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
14669
14670   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
14671   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
14672
14673   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
14674   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
14675   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
14676   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
14677   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
14678   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
14679
14680   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
14681   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
14682   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
14683   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
14684
14685   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
14686   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
14687   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
14688   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
14689   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
14690   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
14691
14692   /* Special.  */
14693   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
14694   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
14695   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
14696
14697   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
14698   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
14699   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
14700
14701   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
14702   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
14703   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
14704   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
14705   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
14706   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
14707
14708   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
14709   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
14710   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
14711   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
14712   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
14713   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
14714
14715   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
14716   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
14717   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
14718   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
14719
14720   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
14721   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
14722
14723   /* SSE2 */
14724   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
14725   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
14726   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
14727   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
14728   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
14729   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
14730   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
14731   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
14732
14733   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
14734   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
14735   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
14736   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
14737     BUILTIN_DESC_SWAP_OPERANDS },
14738   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
14739     BUILTIN_DESC_SWAP_OPERANDS },
14740   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
14741   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
14742   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
14743   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
14744   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
14745     BUILTIN_DESC_SWAP_OPERANDS },
14746   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
14747     BUILTIN_DESC_SWAP_OPERANDS },
14748   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
14749   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
14750   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
14751   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
14752   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
14753   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
14754   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
14755   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
14756   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
14757
14758   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
14759   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
14760   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
14761   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
14762
14763   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
14764   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
14765   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
14766   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
14767
14768   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
14769   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
14770   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
14771
14772   /* SSE2 MMX */
14773   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
14774   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
14775   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
14776   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
14777   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
14778   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
14779   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
14780   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
14781
14782   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
14783   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
14784   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
14785   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
14786   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
14787   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
14788   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
14789   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
14790
14791   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
14792   { MASK_SSE2, CODE_FOR_sse2_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
14793
14794   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
14795   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
14796   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
14797   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
14798
14799   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
14800   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
14801
14802   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
14803   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
14804   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
14805   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
14806   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
14807   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
14808
14809   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
14810   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
14811   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
14812   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
14813
14814   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
14815   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
14816   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
14817   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
14818   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
14819   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
14820   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
14821   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
14822
14823   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
14824   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
14825   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
14826
14827   { MASK_SSE2, CODE_FOR_sse2_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
14828   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
14829
14830   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
14831   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
14832
14833   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
14834   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
14835   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
14836
14837   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
14838   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
14839   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
14840
14841   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
14842   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
14843
14844   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
14845
14846   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
14847   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
14848   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
14849   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
14850
14851   /* SSE3 MMX */
14852   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
14853   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
14854   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
14855   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
14856   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
14857   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
14858 };
14859
14860 static const struct builtin_description bdesc_1arg[] =
14861 {
14862   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
14863   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
14864
14865   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
14866   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
14867   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
14868
14869   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
14870   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
14871   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
14872   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
14873   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
14874   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
14875
14876   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
14877   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
14878
14879   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
14880
14881   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
14882   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
14883
14884   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
14885   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
14886   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
14887   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
14888   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
14889
14890   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
14891
14892   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
14893   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
14894   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
14895   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
14896
14897   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
14898   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
14899   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
14900
14901   /* SSE3 */
14902   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
14903   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
14904 };
14905
14906 static void
14907 ix86_init_builtins (void)
14908 {
14909   if (TARGET_MMX)
14910     ix86_init_mmx_sse_builtins ();
14911 }
14912
14913 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
14914    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
14915    builtins.  */
14916 static void
14917 ix86_init_mmx_sse_builtins (void)
14918 {
14919   const struct builtin_description * d;
14920   size_t i;
14921
14922   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
14923   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
14924   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
14925   tree V2DI_type_node
14926     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
14927   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
14928   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
14929   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
14930   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
14931   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
14932   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
14933
14934   tree pchar_type_node = build_pointer_type (char_type_node);
14935   tree pcchar_type_node = build_pointer_type (
14936                              build_type_variant (char_type_node, 1, 0));
14937   tree pfloat_type_node = build_pointer_type (float_type_node);
14938   tree pcfloat_type_node = build_pointer_type (
14939                              build_type_variant (float_type_node, 1, 0));
14940   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
14941   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
14942   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
14943
14944   /* Comparisons.  */
14945   tree int_ftype_v4sf_v4sf
14946     = build_function_type_list (integer_type_node,
14947                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14948   tree v4si_ftype_v4sf_v4sf
14949     = build_function_type_list (V4SI_type_node,
14950                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
14951   /* MMX/SSE/integer conversions.  */
14952   tree int_ftype_v4sf
14953     = build_function_type_list (integer_type_node,
14954                                 V4SF_type_node, NULL_TREE);
14955   tree int64_ftype_v4sf
14956     = build_function_type_list (long_long_integer_type_node,
14957                                 V4SF_type_node, NULL_TREE);
14958   tree int_ftype_v8qi
14959     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
14960   tree v4sf_ftype_v4sf_int
14961     = build_function_type_list (V4SF_type_node,
14962                                 V4SF_type_node, integer_type_node, NULL_TREE);
14963   tree v4sf_ftype_v4sf_int64
14964     = build_function_type_list (V4SF_type_node,
14965                                 V4SF_type_node, long_long_integer_type_node,
14966                                 NULL_TREE);
14967   tree v4sf_ftype_v4sf_v2si
14968     = build_function_type_list (V4SF_type_node,
14969                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
14970
14971   /* Miscellaneous.  */
14972   tree v8qi_ftype_v4hi_v4hi
14973     = build_function_type_list (V8QI_type_node,
14974                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14975   tree v4hi_ftype_v2si_v2si
14976     = build_function_type_list (V4HI_type_node,
14977                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
14978   tree v4sf_ftype_v4sf_v4sf_int
14979     = build_function_type_list (V4SF_type_node,
14980                                 V4SF_type_node, V4SF_type_node,
14981                                 integer_type_node, NULL_TREE);
14982   tree v2si_ftype_v4hi_v4hi
14983     = build_function_type_list (V2SI_type_node,
14984                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
14985   tree v4hi_ftype_v4hi_int
14986     = build_function_type_list (V4HI_type_node,
14987                                 V4HI_type_node, integer_type_node, NULL_TREE);
14988   tree v4hi_ftype_v4hi_di
14989     = build_function_type_list (V4HI_type_node,
14990                                 V4HI_type_node, long_long_unsigned_type_node,
14991                                 NULL_TREE);
14992   tree v2si_ftype_v2si_di
14993     = build_function_type_list (V2SI_type_node,
14994                                 V2SI_type_node, long_long_unsigned_type_node,
14995                                 NULL_TREE);
14996   tree void_ftype_void
14997     = build_function_type (void_type_node, void_list_node);
14998   tree void_ftype_unsigned
14999     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
15000   tree void_ftype_unsigned_unsigned
15001     = build_function_type_list (void_type_node, unsigned_type_node,
15002                                 unsigned_type_node, NULL_TREE);
15003   tree void_ftype_pcvoid_unsigned_unsigned
15004     = build_function_type_list (void_type_node, const_ptr_type_node,
15005                                 unsigned_type_node, unsigned_type_node,
15006                                 NULL_TREE);
15007   tree unsigned_ftype_void
15008     = build_function_type (unsigned_type_node, void_list_node);
15009   tree v2si_ftype_v4sf
15010     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
15011   /* Loads/stores.  */
15012   tree void_ftype_v8qi_v8qi_pchar
15013     = build_function_type_list (void_type_node,
15014                                 V8QI_type_node, V8QI_type_node,
15015                                 pchar_type_node, NULL_TREE);
15016   tree v4sf_ftype_pcfloat
15017     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
15018   /* @@@ the type is bogus */
15019   tree v4sf_ftype_v4sf_pv2si
15020     = build_function_type_list (V4SF_type_node,
15021                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
15022   tree void_ftype_pv2si_v4sf
15023     = build_function_type_list (void_type_node,
15024                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
15025   tree void_ftype_pfloat_v4sf
15026     = build_function_type_list (void_type_node,
15027                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
15028   tree void_ftype_pdi_di
15029     = build_function_type_list (void_type_node,
15030                                 pdi_type_node, long_long_unsigned_type_node,
15031                                 NULL_TREE);
15032   tree void_ftype_pv2di_v2di
15033     = build_function_type_list (void_type_node,
15034                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
15035   /* Normal vector unops.  */
15036   tree v4sf_ftype_v4sf
15037     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
15038
15039   /* Normal vector binops.  */
15040   tree v4sf_ftype_v4sf_v4sf
15041     = build_function_type_list (V4SF_type_node,
15042                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
15043   tree v8qi_ftype_v8qi_v8qi
15044     = build_function_type_list (V8QI_type_node,
15045                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
15046   tree v4hi_ftype_v4hi_v4hi
15047     = build_function_type_list (V4HI_type_node,
15048                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
15049   tree v2si_ftype_v2si_v2si
15050     = build_function_type_list (V2SI_type_node,
15051                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
15052   tree di_ftype_di_di
15053     = build_function_type_list (long_long_unsigned_type_node,
15054                                 long_long_unsigned_type_node,
15055                                 long_long_unsigned_type_node, NULL_TREE);
15056
15057   tree v2si_ftype_v2sf
15058     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
15059   tree v2sf_ftype_v2si
15060     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
15061   tree v2si_ftype_v2si
15062     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
15063   tree v2sf_ftype_v2sf
15064     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
15065   tree v2sf_ftype_v2sf_v2sf
15066     = build_function_type_list (V2SF_type_node,
15067                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
15068   tree v2si_ftype_v2sf_v2sf
15069     = build_function_type_list (V2SI_type_node,
15070                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
15071   tree pint_type_node    = build_pointer_type (integer_type_node);
15072   tree pdouble_type_node = build_pointer_type (double_type_node);
15073   tree pcdouble_type_node = build_pointer_type (
15074                                 build_type_variant (double_type_node, 1, 0));
15075   tree int_ftype_v2df_v2df
15076     = build_function_type_list (integer_type_node,
15077                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15078
15079   tree void_ftype_pcvoid
15080     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
15081   tree v4sf_ftype_v4si
15082     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
15083   tree v4si_ftype_v4sf
15084     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
15085   tree v2df_ftype_v4si
15086     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
15087   tree v4si_ftype_v2df
15088     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
15089   tree v2si_ftype_v2df
15090     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
15091   tree v4sf_ftype_v2df
15092     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
15093   tree v2df_ftype_v2si
15094     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
15095   tree v2df_ftype_v4sf
15096     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
15097   tree int_ftype_v2df
15098     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
15099   tree int64_ftype_v2df
15100     = build_function_type_list (long_long_integer_type_node,
15101                                 V2DF_type_node, NULL_TREE);
15102   tree v2df_ftype_v2df_int
15103     = build_function_type_list (V2DF_type_node,
15104                                 V2DF_type_node, integer_type_node, NULL_TREE);
15105   tree v2df_ftype_v2df_int64
15106     = build_function_type_list (V2DF_type_node,
15107                                 V2DF_type_node, long_long_integer_type_node,
15108                                 NULL_TREE);
15109   tree v4sf_ftype_v4sf_v2df
15110     = build_function_type_list (V4SF_type_node,
15111                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
15112   tree v2df_ftype_v2df_v4sf
15113     = build_function_type_list (V2DF_type_node,
15114                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
15115   tree v2df_ftype_v2df_v2df_int
15116     = build_function_type_list (V2DF_type_node,
15117                                 V2DF_type_node, V2DF_type_node,
15118                                 integer_type_node,
15119                                 NULL_TREE);
15120   tree v2df_ftype_v2df_pcdouble
15121     = build_function_type_list (V2DF_type_node,
15122                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
15123   tree void_ftype_pdouble_v2df
15124     = build_function_type_list (void_type_node,
15125                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
15126   tree void_ftype_pint_int
15127     = build_function_type_list (void_type_node,
15128                                 pint_type_node, integer_type_node, NULL_TREE);
15129   tree void_ftype_v16qi_v16qi_pchar
15130     = build_function_type_list (void_type_node,
15131                                 V16QI_type_node, V16QI_type_node,
15132                                 pchar_type_node, NULL_TREE);
15133   tree v2df_ftype_pcdouble
15134     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
15135   tree v2df_ftype_v2df_v2df
15136     = build_function_type_list (V2DF_type_node,
15137                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15138   tree v16qi_ftype_v16qi_v16qi
15139     = build_function_type_list (V16QI_type_node,
15140                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
15141   tree v8hi_ftype_v8hi_v8hi
15142     = build_function_type_list (V8HI_type_node,
15143                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
15144   tree v4si_ftype_v4si_v4si
15145     = build_function_type_list (V4SI_type_node,
15146                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
15147   tree v2di_ftype_v2di_v2di
15148     = build_function_type_list (V2DI_type_node,
15149                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
15150   tree v2di_ftype_v2df_v2df
15151     = build_function_type_list (V2DI_type_node,
15152                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
15153   tree v2df_ftype_v2df
15154     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
15155   tree v2di_ftype_v2di_int
15156     = build_function_type_list (V2DI_type_node,
15157                                 V2DI_type_node, integer_type_node, NULL_TREE);
15158   tree v4si_ftype_v4si_int
15159     = build_function_type_list (V4SI_type_node,
15160                                 V4SI_type_node, integer_type_node, NULL_TREE);
15161   tree v8hi_ftype_v8hi_int
15162     = build_function_type_list (V8HI_type_node,
15163                                 V8HI_type_node, integer_type_node, NULL_TREE);
15164   tree v8hi_ftype_v8hi_v2di
15165     = build_function_type_list (V8HI_type_node,
15166                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
15167   tree v4si_ftype_v4si_v2di
15168     = build_function_type_list (V4SI_type_node,
15169                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
15170   tree v4si_ftype_v8hi_v8hi
15171     = build_function_type_list (V4SI_type_node,
15172                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
15173   tree di_ftype_v8qi_v8qi
15174     = build_function_type_list (long_long_unsigned_type_node,
15175                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
15176   tree di_ftype_v2si_v2si
15177     = build_function_type_list (long_long_unsigned_type_node,
15178                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
15179   tree v2di_ftype_v16qi_v16qi
15180     = build_function_type_list (V2DI_type_node,
15181                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
15182   tree v2di_ftype_v4si_v4si
15183     = build_function_type_list (V2DI_type_node,
15184                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
15185   tree int_ftype_v16qi
15186     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
15187   tree v16qi_ftype_pcchar
15188     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
15189   tree void_ftype_pchar_v16qi
15190     = build_function_type_list (void_type_node,
15191                                 pchar_type_node, V16QI_type_node, NULL_TREE);
15192
15193   tree float80_type;
15194   tree float128_type;
15195   tree ftype;
15196
15197   /* The __float80 type.  */
15198   if (TYPE_MODE (long_double_type_node) == XFmode)
15199     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
15200                                                "__float80");
15201   else
15202     {
15203       /* The __float80 type.  */
15204       float80_type = make_node (REAL_TYPE);
15205       TYPE_PRECISION (float80_type) = 80;
15206       layout_type (float80_type);
15207       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
15208     }
15209
15210   if (TARGET_64BIT)
15211     {
15212       float128_type = make_node (REAL_TYPE);
15213       TYPE_PRECISION (float128_type) = 128;
15214       layout_type (float128_type);
15215       (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
15216     }
15217
15218   /* Add all builtins that are more or less simple operations on two
15219      operands.  */
15220   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15221     {
15222       /* Use one of the operands; the target can have a different mode for
15223          mask-generating compares.  */
15224       enum machine_mode mode;
15225       tree type;
15226
15227       if (d->name == 0)
15228         continue;
15229       mode = insn_data[d->icode].operand[1].mode;
15230
15231       switch (mode)
15232         {
15233         case V16QImode:
15234           type = v16qi_ftype_v16qi_v16qi;
15235           break;
15236         case V8HImode:
15237           type = v8hi_ftype_v8hi_v8hi;
15238           break;
15239         case V4SImode:
15240           type = v4si_ftype_v4si_v4si;
15241           break;
15242         case V2DImode:
15243           type = v2di_ftype_v2di_v2di;
15244           break;
15245         case V2DFmode:
15246           type = v2df_ftype_v2df_v2df;
15247           break;
15248         case V4SFmode:
15249           type = v4sf_ftype_v4sf_v4sf;
15250           break;
15251         case V8QImode:
15252           type = v8qi_ftype_v8qi_v8qi;
15253           break;
15254         case V4HImode:
15255           type = v4hi_ftype_v4hi_v4hi;
15256           break;
15257         case V2SImode:
15258           type = v2si_ftype_v2si_v2si;
15259           break;
15260         case DImode:
15261           type = di_ftype_di_di;
15262           break;
15263
15264         default:
15265           gcc_unreachable ();
15266         }
15267
15268       /* Override for comparisons.  */
15269       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
15270           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
15271         type = v4si_ftype_v4sf_v4sf;
15272
15273       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
15274           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
15275         type = v2di_ftype_v2df_v2df;
15276
15277       def_builtin (d->mask, d->name, type, d->code);
15278     }
15279
15280   /* Add the remaining MMX insns with somewhat more complicated types.  */
15281   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
15282   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
15283   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
15284   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
15285
15286   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
15287   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
15288   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
15289
15290   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
15291   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
15292
15293   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
15294   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
15295
15296   /* comi/ucomi insns.  */
15297   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
15298     if (d->mask == MASK_SSE2)
15299       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
15300     else
15301       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
15302
15303   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
15304   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
15305   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
15306
15307   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
15308   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
15309   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
15310   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
15311   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
15312   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
15313   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
15314   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
15315   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
15316   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
15317   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
15318
15319   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
15320
15321   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
15322   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
15323
15324   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
15325   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
15326   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
15327   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
15328
15329   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
15330   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
15331   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
15332   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
15333
15334   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
15335
15336   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
15337
15338   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
15339   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
15340   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
15341   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
15342   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
15343   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
15344
15345   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
15346
15347   /* Original 3DNow!  */
15348   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
15349   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
15350   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
15351   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
15352   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
15353   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
15354   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
15355   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
15356   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
15357   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
15358   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
15359   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
15360   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
15361   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
15362   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
15363   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
15364   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
15365   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
15366   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
15367   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
15368
15369   /* 3DNow! extension as used in the Athlon CPU.  */
15370   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
15371   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
15372   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
15373   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
15374   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
15375   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
15376
15377   /* SSE2 */
15378   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
15379
15380   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
15381   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
15382
15383   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
15384   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
15385
15386   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
15387   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
15388   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
15389   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
15390   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
15391
15392   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
15393   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
15394   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
15395   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
15396
15397   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
15398   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
15399
15400   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
15401
15402   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
15403   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
15404
15405   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
15406   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
15407   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
15408   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
15409   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
15410
15411   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
15412
15413   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
15414   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
15415   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
15416   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
15417
15418   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
15419   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
15420   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
15421
15422   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
15423   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
15424   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
15425   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
15426
15427   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
15428   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
15429   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
15430
15431   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
15432   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
15433
15434   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
15435   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
15436
15437   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
15438   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
15439   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
15440
15441   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
15442   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
15443   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
15444
15445   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
15446   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
15447
15448   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
15449   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
15450   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
15451   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
15452
15453   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
15454   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
15455   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
15456   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
15457
15458   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
15459   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
15460
15461   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
15462
15463   /* Prescott New Instructions.  */
15464   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
15465                void_ftype_pcvoid_unsigned_unsigned,
15466                IX86_BUILTIN_MONITOR);
15467   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
15468                void_ftype_unsigned_unsigned,
15469                IX86_BUILTIN_MWAIT);
15470   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
15471                v4sf_ftype_v4sf,
15472                IX86_BUILTIN_MOVSHDUP);
15473   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
15474                v4sf_ftype_v4sf,
15475                IX86_BUILTIN_MOVSLDUP);
15476   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
15477                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
15478
15479   /* Access to the vec_init patterns.  */
15480   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
15481                                     integer_type_node, NULL_TREE);
15482   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
15483                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
15484
15485   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
15486                                     short_integer_type_node,
15487                                     short_integer_type_node,
15488                                     short_integer_type_node, NULL_TREE);
15489   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
15490                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
15491
15492   ftype = build_function_type_list (V8QI_type_node, char_type_node,
15493                                     char_type_node, char_type_node,
15494                                     char_type_node, char_type_node,
15495                                     char_type_node, char_type_node,
15496                                     char_type_node, NULL_TREE);
15497   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
15498                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
15499
15500   /* Access to the vec_extract patterns.  */
15501   ftype = build_function_type_list (double_type_node, V2DF_type_node,
15502                                     integer_type_node, NULL_TREE);
15503   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
15504                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
15505
15506   ftype = build_function_type_list (long_long_integer_type_node,
15507                                     V2DI_type_node, integer_type_node,
15508                                     NULL_TREE);
15509   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
15510                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
15511
15512   ftype = build_function_type_list (float_type_node, V4SF_type_node,
15513                                     integer_type_node, NULL_TREE);
15514   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
15515                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
15516
15517   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
15518                                     integer_type_node, NULL_TREE);
15519   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
15520                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
15521
15522   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
15523                                     integer_type_node, NULL_TREE);
15524   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
15525                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
15526
15527   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
15528                                     integer_type_node, NULL_TREE);
15529   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
15530                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
15531
15532   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
15533                                     integer_type_node, NULL_TREE);
15534   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
15535                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
15536
15537   /* Access to the vec_set patterns.  */
15538   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
15539                                     intHI_type_node,
15540                                     integer_type_node, NULL_TREE);
15541   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
15542                ftype, IX86_BUILTIN_VEC_SET_V8HI);
15543
15544   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
15545                                     intHI_type_node,
15546                                     integer_type_node, NULL_TREE);
15547   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
15548                ftype, IX86_BUILTIN_VEC_SET_V4HI);
15549 }
15550
15551 /* Errors in the source file can cause expand_expr to return const0_rtx
15552    where we expect a vector.  To avoid crashing, use one of the vector
15553    clear instructions.  */
15554 static rtx
15555 safe_vector_operand (rtx x, enum machine_mode mode)
15556 {
15557   if (x == const0_rtx)
15558     x = CONST0_RTX (mode);
15559   return x;
15560 }
15561
15562 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
15563
15564 static rtx
15565 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
15566 {
15567   rtx pat, xops[3];
15568   tree arg0 = TREE_VALUE (arglist);
15569   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15570   rtx op0 = expand_normal (arg0);
15571   rtx op1 = expand_normal (arg1);
15572   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15573   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15574   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
15575
15576   if (VECTOR_MODE_P (mode0))
15577     op0 = safe_vector_operand (op0, mode0);
15578   if (VECTOR_MODE_P (mode1))
15579     op1 = safe_vector_operand (op1, mode1);
15580
15581   if (optimize || !target
15582       || GET_MODE (target) != tmode
15583       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15584     target = gen_reg_rtx (tmode);
15585
15586   if (GET_MODE (op1) == SImode && mode1 == TImode)
15587     {
15588       rtx x = gen_reg_rtx (V4SImode);
15589       emit_insn (gen_sse2_loadd (x, op1));
15590       op1 = gen_lowpart (TImode, x);
15591     }
15592
15593   /* The insn must want input operands in the same modes as the
15594      result.  */
15595   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
15596               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
15597
15598   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
15599     op0 = copy_to_mode_reg (mode0, op0);
15600   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
15601     op1 = copy_to_mode_reg (mode1, op1);
15602
15603   /* ??? Using ix86_fixup_binary_operands is problematic when
15604      we've got mismatched modes.  Fake it.  */
15605
15606   xops[0] = target;
15607   xops[1] = op0;
15608   xops[2] = op1;
15609
15610   if (tmode == mode0 && tmode == mode1)
15611     {
15612       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
15613       op0 = xops[1];
15614       op1 = xops[2];
15615     }
15616   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
15617     {
15618       op0 = force_reg (mode0, op0);
15619       op1 = force_reg (mode1, op1);
15620       target = gen_reg_rtx (tmode);
15621     }
15622
15623   pat = GEN_FCN (icode) (target, op0, op1);
15624   if (! pat)
15625     return 0;
15626   emit_insn (pat);
15627   return target;
15628 }
15629
15630 /* Subroutine of ix86_expand_builtin to take care of stores.  */
15631
15632 static rtx
15633 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
15634 {
15635   rtx pat;
15636   tree arg0 = TREE_VALUE (arglist);
15637   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15638   rtx op0 = expand_normal (arg0);
15639   rtx op1 = expand_normal (arg1);
15640   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
15641   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
15642
15643   if (VECTOR_MODE_P (mode1))
15644     op1 = safe_vector_operand (op1, mode1);
15645
15646   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15647   op1 = copy_to_mode_reg (mode1, op1);
15648
15649   pat = GEN_FCN (icode) (op0, op1);
15650   if (pat)
15651     emit_insn (pat);
15652   return 0;
15653 }
15654
15655 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
15656
15657 static rtx
15658 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
15659                           rtx target, int do_load)
15660 {
15661   rtx pat;
15662   tree arg0 = TREE_VALUE (arglist);
15663   rtx op0 = expand_normal (arg0);
15664   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15665   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15666
15667   if (optimize || !target
15668       || GET_MODE (target) != tmode
15669       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15670     target = gen_reg_rtx (tmode);
15671   if (do_load)
15672     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
15673   else
15674     {
15675       if (VECTOR_MODE_P (mode0))
15676         op0 = safe_vector_operand (op0, mode0);
15677
15678       if ((optimize && !register_operand (op0, mode0))
15679           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15680         op0 = copy_to_mode_reg (mode0, op0);
15681     }
15682
15683   pat = GEN_FCN (icode) (target, op0);
15684   if (! pat)
15685     return 0;
15686   emit_insn (pat);
15687   return target;
15688 }
15689
15690 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
15691    sqrtss, rsqrtss, rcpss.  */
15692
15693 static rtx
15694 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
15695 {
15696   rtx pat;
15697   tree arg0 = TREE_VALUE (arglist);
15698   rtx op1, op0 = expand_normal (arg0);
15699   enum machine_mode tmode = insn_data[icode].operand[0].mode;
15700   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
15701
15702   if (optimize || !target
15703       || GET_MODE (target) != tmode
15704       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
15705     target = gen_reg_rtx (tmode);
15706
15707   if (VECTOR_MODE_P (mode0))
15708     op0 = safe_vector_operand (op0, mode0);
15709
15710   if ((optimize && !register_operand (op0, mode0))
15711       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
15712     op0 = copy_to_mode_reg (mode0, op0);
15713
15714   op1 = op0;
15715   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
15716     op1 = copy_to_mode_reg (mode0, op1);
15717
15718   pat = GEN_FCN (icode) (target, op0, op1);
15719   if (! pat)
15720     return 0;
15721   emit_insn (pat);
15722   return target;
15723 }
15724
15725 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
15726
15727 static rtx
15728 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
15729                          rtx target)
15730 {
15731   rtx pat;
15732   tree arg0 = TREE_VALUE (arglist);
15733   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15734   rtx op0 = expand_normal (arg0);
15735   rtx op1 = expand_normal (arg1);
15736   rtx op2;
15737   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
15738   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
15739   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
15740   enum rtx_code comparison = d->comparison;
15741
15742   if (VECTOR_MODE_P (mode0))
15743     op0 = safe_vector_operand (op0, mode0);
15744   if (VECTOR_MODE_P (mode1))
15745     op1 = safe_vector_operand (op1, mode1);
15746
15747   /* Swap operands if we have a comparison that isn't available in
15748      hardware.  */
15749   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15750     {
15751       rtx tmp = gen_reg_rtx (mode1);
15752       emit_move_insn (tmp, op1);
15753       op1 = op0;
15754       op0 = tmp;
15755     }
15756
15757   if (optimize || !target
15758       || GET_MODE (target) != tmode
15759       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
15760     target = gen_reg_rtx (tmode);
15761
15762   if ((optimize && !register_operand (op0, mode0))
15763       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
15764     op0 = copy_to_mode_reg (mode0, op0);
15765   if ((optimize && !register_operand (op1, mode1))
15766       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
15767     op1 = copy_to_mode_reg (mode1, op1);
15768
15769   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15770   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
15771   if (! pat)
15772     return 0;
15773   emit_insn (pat);
15774   return target;
15775 }
15776
15777 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
15778
15779 static rtx
15780 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
15781                       rtx target)
15782 {
15783   rtx pat;
15784   tree arg0 = TREE_VALUE (arglist);
15785   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15786   rtx op0 = expand_normal (arg0);
15787   rtx op1 = expand_normal (arg1);
15788   rtx op2;
15789   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
15790   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
15791   enum rtx_code comparison = d->comparison;
15792
15793   if (VECTOR_MODE_P (mode0))
15794     op0 = safe_vector_operand (op0, mode0);
15795   if (VECTOR_MODE_P (mode1))
15796     op1 = safe_vector_operand (op1, mode1);
15797
15798   /* Swap operands if we have a comparison that isn't available in
15799      hardware.  */
15800   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
15801     {
15802       rtx tmp = op1;
15803       op1 = op0;
15804       op0 = tmp;
15805     }
15806
15807   target = gen_reg_rtx (SImode);
15808   emit_move_insn (target, const0_rtx);
15809   target = gen_rtx_SUBREG (QImode, target, 0);
15810
15811   if ((optimize && !register_operand (op0, mode0))
15812       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
15813     op0 = copy_to_mode_reg (mode0, op0);
15814   if ((optimize && !register_operand (op1, mode1))
15815       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
15816     op1 = copy_to_mode_reg (mode1, op1);
15817
15818   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
15819   pat = GEN_FCN (d->icode) (op0, op1);
15820   if (! pat)
15821     return 0;
15822   emit_insn (pat);
15823   emit_insn (gen_rtx_SET (VOIDmode,
15824                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
15825                           gen_rtx_fmt_ee (comparison, QImode,
15826                                           SET_DEST (pat),
15827                                           const0_rtx)));
15828
15829   return SUBREG_REG (target);
15830 }
15831
15832 /* Return the integer constant in ARG.  Constrain it to be in the range
15833    of the subparts of VEC_TYPE; issue an error if not.  */
15834
15835 static int
15836 get_element_number (tree vec_type, tree arg)
15837 {
15838   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
15839
15840   if (!host_integerp (arg, 1)
15841       || (elt = tree_low_cst (arg, 1), elt > max))
15842     {
15843       error ("selector must be an integer constant in the range 0..%wi", max);
15844       return 0;
15845     }
15846
15847   return elt;
15848 }
15849
15850 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15851    ix86_expand_vector_init.  We DO have language-level syntax for this, in
15852    the form of  (type){ init-list }.  Except that since we can't place emms
15853    instructions from inside the compiler, we can't allow the use of MMX
15854    registers unless the user explicitly asks for it.  So we do *not* define
15855    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
15856    we have builtins invoked by mmintrin.h that gives us license to emit
15857    these sorts of instructions.  */
15858
15859 static rtx
15860 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
15861 {
15862   enum machine_mode tmode = TYPE_MODE (type);
15863   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
15864   int i, n_elt = GET_MODE_NUNITS (tmode);
15865   rtvec v = rtvec_alloc (n_elt);
15866
15867   gcc_assert (VECTOR_MODE_P (tmode));
15868
15869   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
15870     {
15871       rtx x = expand_normal (TREE_VALUE (arglist));
15872       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
15873     }
15874
15875   gcc_assert (arglist == NULL);
15876
15877   if (!target || !register_operand (target, tmode))
15878     target = gen_reg_rtx (tmode);
15879
15880   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
15881   return target;
15882 }
15883
15884 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15885    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
15886    had a language-level syntax for referencing vector elements.  */
15887
15888 static rtx
15889 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
15890 {
15891   enum machine_mode tmode, mode0;
15892   tree arg0, arg1;
15893   int elt;
15894   rtx op0;
15895
15896   arg0 = TREE_VALUE (arglist);
15897   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15898
15899   op0 = expand_normal (arg0);
15900   elt = get_element_number (TREE_TYPE (arg0), arg1);
15901
15902   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15903   mode0 = TYPE_MODE (TREE_TYPE (arg0));
15904   gcc_assert (VECTOR_MODE_P (mode0));
15905
15906   op0 = force_reg (mode0, op0);
15907
15908   if (optimize || !target || !register_operand (target, tmode))
15909     target = gen_reg_rtx (tmode);
15910
15911   ix86_expand_vector_extract (true, target, op0, elt);
15912
15913   return target;
15914 }
15915
15916 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
15917    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
15918    a language-level syntax for referencing vector elements.  */
15919
15920 static rtx
15921 ix86_expand_vec_set_builtin (tree arglist)
15922 {
15923   enum machine_mode tmode, mode1;
15924   tree arg0, arg1, arg2;
15925   int elt;
15926   rtx op0, op1;
15927
15928   arg0 = TREE_VALUE (arglist);
15929   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15930   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15931
15932   tmode = TYPE_MODE (TREE_TYPE (arg0));
15933   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
15934   gcc_assert (VECTOR_MODE_P (tmode));
15935
15936   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
15937   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
15938   elt = get_element_number (TREE_TYPE (arg0), arg2);
15939
15940   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
15941     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
15942
15943   op0 = force_reg (tmode, op0);
15944   op1 = force_reg (mode1, op1);
15945
15946   ix86_expand_vector_set (true, op0, op1, elt);
15947
15948   return op0;
15949 }
15950
15951 /* Expand an expression EXP that calls a built-in function,
15952    with result going to TARGET if that's convenient
15953    (and in mode MODE if that's convenient).
15954    SUBTARGET may be used as the target for computing one of EXP's operands.
15955    IGNORE is nonzero if the value is to be ignored.  */
15956
15957 static rtx
15958 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
15959                      enum machine_mode mode ATTRIBUTE_UNUSED,
15960                      int ignore ATTRIBUTE_UNUSED)
15961 {
15962   const struct builtin_description *d;
15963   size_t i;
15964   enum insn_code icode;
15965   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
15966   tree arglist = TREE_OPERAND (exp, 1);
15967   tree arg0, arg1, arg2;
15968   rtx op0, op1, op2, pat;
15969   enum machine_mode tmode, mode0, mode1, mode2;
15970   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
15971
15972   switch (fcode)
15973     {
15974     case IX86_BUILTIN_EMMS:
15975       emit_insn (gen_mmx_emms ());
15976       return 0;
15977
15978     case IX86_BUILTIN_SFENCE:
15979       emit_insn (gen_sse_sfence ());
15980       return 0;
15981
15982     case IX86_BUILTIN_MASKMOVQ:
15983     case IX86_BUILTIN_MASKMOVDQU:
15984       icode = (fcode == IX86_BUILTIN_MASKMOVQ
15985                ? CODE_FOR_mmx_maskmovq
15986                : CODE_FOR_sse2_maskmovdqu);
15987       /* Note the arg order is different from the operand order.  */
15988       arg1 = TREE_VALUE (arglist);
15989       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
15990       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
15991       op0 = expand_normal (arg0);
15992       op1 = expand_normal (arg1);
15993       op2 = expand_normal (arg2);
15994       mode0 = insn_data[icode].operand[0].mode;
15995       mode1 = insn_data[icode].operand[1].mode;
15996       mode2 = insn_data[icode].operand[2].mode;
15997
15998       op0 = force_reg (Pmode, op0);
15999       op0 = gen_rtx_MEM (mode1, op0);
16000
16001       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
16002         op0 = copy_to_mode_reg (mode0, op0);
16003       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
16004         op1 = copy_to_mode_reg (mode1, op1);
16005       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
16006         op2 = copy_to_mode_reg (mode2, op2);
16007       pat = GEN_FCN (icode) (op0, op1, op2);
16008       if (! pat)
16009         return 0;
16010       emit_insn (pat);
16011       return 0;
16012
16013     case IX86_BUILTIN_SQRTSS:
16014       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
16015     case IX86_BUILTIN_RSQRTSS:
16016       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
16017     case IX86_BUILTIN_RCPSS:
16018       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
16019
16020     case IX86_BUILTIN_LOADUPS:
16021       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
16022
16023     case IX86_BUILTIN_STOREUPS:
16024       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
16025
16026     case IX86_BUILTIN_LOADHPS:
16027     case IX86_BUILTIN_LOADLPS:
16028     case IX86_BUILTIN_LOADHPD:
16029     case IX86_BUILTIN_LOADLPD:
16030       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
16031                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
16032                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
16033                : CODE_FOR_sse2_loadlpd);
16034       arg0 = TREE_VALUE (arglist);
16035       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16036       op0 = expand_normal (arg0);
16037       op1 = expand_normal (arg1);
16038       tmode = insn_data[icode].operand[0].mode;
16039       mode0 = insn_data[icode].operand[1].mode;
16040       mode1 = insn_data[icode].operand[2].mode;
16041
16042       op0 = force_reg (mode0, op0);
16043       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
16044       if (optimize || target == 0
16045           || GET_MODE (target) != tmode
16046           || !register_operand (target, tmode))
16047         target = gen_reg_rtx (tmode);
16048       pat = GEN_FCN (icode) (target, op0, op1);
16049       if (! pat)
16050         return 0;
16051       emit_insn (pat);
16052       return target;
16053
16054     case IX86_BUILTIN_STOREHPS:
16055     case IX86_BUILTIN_STORELPS:
16056       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
16057                : CODE_FOR_sse_storelps);
16058       arg0 = TREE_VALUE (arglist);
16059       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16060       op0 = expand_normal (arg0);
16061       op1 = expand_normal (arg1);
16062       mode0 = insn_data[icode].operand[0].mode;
16063       mode1 = insn_data[icode].operand[1].mode;
16064
16065       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
16066       op1 = force_reg (mode1, op1);
16067
16068       pat = GEN_FCN (icode) (op0, op1);
16069       if (! pat)
16070         return 0;
16071       emit_insn (pat);
16072       return const0_rtx;
16073
16074     case IX86_BUILTIN_MOVNTPS:
16075       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
16076     case IX86_BUILTIN_MOVNTQ:
16077       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
16078
16079     case IX86_BUILTIN_LDMXCSR:
16080       op0 = expand_normal (TREE_VALUE (arglist));
16081       target = assign_386_stack_local (SImode, SLOT_TEMP);
16082       emit_move_insn (target, op0);
16083       emit_insn (gen_sse_ldmxcsr (target));
16084       return 0;
16085
16086     case IX86_BUILTIN_STMXCSR:
16087       target = assign_386_stack_local (SImode, SLOT_TEMP);
16088       emit_insn (gen_sse_stmxcsr (target));
16089       return copy_to_mode_reg (SImode, target);
16090
16091     case IX86_BUILTIN_SHUFPS:
16092     case IX86_BUILTIN_SHUFPD:
16093       icode = (fcode == IX86_BUILTIN_SHUFPS
16094                ? CODE_FOR_sse_shufps
16095                : CODE_FOR_sse2_shufpd);
16096       arg0 = TREE_VALUE (arglist);
16097       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16098       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16099       op0 = expand_normal (arg0);
16100       op1 = expand_normal (arg1);
16101       op2 = expand_normal (arg2);
16102       tmode = insn_data[icode].operand[0].mode;
16103       mode0 = insn_data[icode].operand[1].mode;
16104       mode1 = insn_data[icode].operand[2].mode;
16105       mode2 = insn_data[icode].operand[3].mode;
16106
16107       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
16108         op0 = copy_to_mode_reg (mode0, op0);
16109       if ((optimize && !register_operand (op1, mode1))
16110           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
16111         op1 = copy_to_mode_reg (mode1, op1);
16112       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
16113         {
16114           /* @@@ better error message */
16115           error ("mask must be an immediate");
16116           return gen_reg_rtx (tmode);
16117         }
16118       if (optimize || target == 0
16119           || GET_MODE (target) != tmode
16120           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16121         target = gen_reg_rtx (tmode);
16122       pat = GEN_FCN (icode) (target, op0, op1, op2);
16123       if (! pat)
16124         return 0;
16125       emit_insn (pat);
16126       return target;
16127
16128     case IX86_BUILTIN_PSHUFW:
16129     case IX86_BUILTIN_PSHUFD:
16130     case IX86_BUILTIN_PSHUFHW:
16131     case IX86_BUILTIN_PSHUFLW:
16132       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
16133                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
16134                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
16135                : CODE_FOR_mmx_pshufw);
16136       arg0 = TREE_VALUE (arglist);
16137       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16138       op0 = expand_normal (arg0);
16139       op1 = expand_normal (arg1);
16140       tmode = insn_data[icode].operand[0].mode;
16141       mode1 = insn_data[icode].operand[1].mode;
16142       mode2 = insn_data[icode].operand[2].mode;
16143
16144       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16145         op0 = copy_to_mode_reg (mode1, op0);
16146       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16147         {
16148           /* @@@ better error message */
16149           error ("mask must be an immediate");
16150           return const0_rtx;
16151         }
16152       if (target == 0
16153           || GET_MODE (target) != tmode
16154           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
16155         target = gen_reg_rtx (tmode);
16156       pat = GEN_FCN (icode) (target, op0, op1);
16157       if (! pat)
16158         return 0;
16159       emit_insn (pat);
16160       return target;
16161
16162     case IX86_BUILTIN_PSLLDQI128:
16163     case IX86_BUILTIN_PSRLDQI128:
16164       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
16165                : CODE_FOR_sse2_lshrti3);
16166       arg0 = TREE_VALUE (arglist);
16167       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16168       op0 = expand_normal (arg0);
16169       op1 = expand_normal (arg1);
16170       tmode = insn_data[icode].operand[0].mode;
16171       mode1 = insn_data[icode].operand[1].mode;
16172       mode2 = insn_data[icode].operand[2].mode;
16173
16174       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
16175         {
16176           op0 = copy_to_reg (op0);
16177           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
16178         }
16179       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
16180         {
16181           error ("shift must be an immediate");
16182           return const0_rtx;
16183         }
16184       target = gen_reg_rtx (V2DImode);
16185       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
16186       if (! pat)
16187         return 0;
16188       emit_insn (pat);
16189       return target;
16190
16191     case IX86_BUILTIN_FEMMS:
16192       emit_insn (gen_mmx_femms ());
16193       return NULL_RTX;
16194
16195     case IX86_BUILTIN_PAVGUSB:
16196       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
16197
16198     case IX86_BUILTIN_PF2ID:
16199       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
16200
16201     case IX86_BUILTIN_PFACC:
16202       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
16203
16204     case IX86_BUILTIN_PFADD:
16205      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
16206
16207     case IX86_BUILTIN_PFCMPEQ:
16208       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
16209
16210     case IX86_BUILTIN_PFCMPGE:
16211       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
16212
16213     case IX86_BUILTIN_PFCMPGT:
16214       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
16215
16216     case IX86_BUILTIN_PFMAX:
16217       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
16218
16219     case IX86_BUILTIN_PFMIN:
16220       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
16221
16222     case IX86_BUILTIN_PFMUL:
16223       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
16224
16225     case IX86_BUILTIN_PFRCP:
16226       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
16227
16228     case IX86_BUILTIN_PFRCPIT1:
16229       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
16230
16231     case IX86_BUILTIN_PFRCPIT2:
16232       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
16233
16234     case IX86_BUILTIN_PFRSQIT1:
16235       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
16236
16237     case IX86_BUILTIN_PFRSQRT:
16238       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
16239
16240     case IX86_BUILTIN_PFSUB:
16241       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
16242
16243     case IX86_BUILTIN_PFSUBR:
16244       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
16245
16246     case IX86_BUILTIN_PI2FD:
16247       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
16248
16249     case IX86_BUILTIN_PMULHRW:
16250       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
16251
16252     case IX86_BUILTIN_PF2IW:
16253       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
16254
16255     case IX86_BUILTIN_PFNACC:
16256       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
16257
16258     case IX86_BUILTIN_PFPNACC:
16259       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
16260
16261     case IX86_BUILTIN_PI2FW:
16262       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
16263
16264     case IX86_BUILTIN_PSWAPDSI:
16265       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
16266
16267     case IX86_BUILTIN_PSWAPDSF:
16268       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
16269
16270     case IX86_BUILTIN_SQRTSD:
16271       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
16272     case IX86_BUILTIN_LOADUPD:
16273       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
16274     case IX86_BUILTIN_STOREUPD:
16275       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
16276
16277     case IX86_BUILTIN_MFENCE:
16278         emit_insn (gen_sse2_mfence ());
16279         return 0;
16280     case IX86_BUILTIN_LFENCE:
16281         emit_insn (gen_sse2_lfence ());
16282         return 0;
16283
16284     case IX86_BUILTIN_CLFLUSH:
16285         arg0 = TREE_VALUE (arglist);
16286         op0 = expand_normal (arg0);
16287         icode = CODE_FOR_sse2_clflush;
16288         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
16289             op0 = copy_to_mode_reg (Pmode, op0);
16290
16291         emit_insn (gen_sse2_clflush (op0));
16292         return 0;
16293
16294     case IX86_BUILTIN_MOVNTPD:
16295       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
16296     case IX86_BUILTIN_MOVNTDQ:
16297       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
16298     case IX86_BUILTIN_MOVNTI:
16299       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
16300
16301     case IX86_BUILTIN_LOADDQU:
16302       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
16303     case IX86_BUILTIN_STOREDQU:
16304       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
16305
16306     case IX86_BUILTIN_MONITOR:
16307       arg0 = TREE_VALUE (arglist);
16308       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16309       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
16310       op0 = expand_normal (arg0);
16311       op1 = expand_normal (arg1);
16312       op2 = expand_normal (arg2);
16313       if (!REG_P (op0))
16314         op0 = copy_to_mode_reg (Pmode, op0);
16315       if (!REG_P (op1))
16316         op1 = copy_to_mode_reg (SImode, op1);
16317       if (!REG_P (op2))
16318         op2 = copy_to_mode_reg (SImode, op2);
16319       if (!TARGET_64BIT)
16320         emit_insn (gen_sse3_monitor (op0, op1, op2));
16321       else
16322         emit_insn (gen_sse3_monitor64 (op0, op1, op2));
16323       return 0;
16324
16325     case IX86_BUILTIN_MWAIT:
16326       arg0 = TREE_VALUE (arglist);
16327       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
16328       op0 = expand_normal (arg0);
16329       op1 = expand_normal (arg1);
16330       if (!REG_P (op0))
16331         op0 = copy_to_mode_reg (SImode, op0);
16332       if (!REG_P (op1))
16333         op1 = copy_to_mode_reg (SImode, op1);
16334       emit_insn (gen_sse3_mwait (op0, op1));
16335       return 0;
16336
16337     case IX86_BUILTIN_LDDQU:
16338       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
16339                                        target, 1);
16340
16341     case IX86_BUILTIN_VEC_INIT_V2SI:
16342     case IX86_BUILTIN_VEC_INIT_V4HI:
16343     case IX86_BUILTIN_VEC_INIT_V8QI:
16344       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
16345
16346     case IX86_BUILTIN_VEC_EXT_V2DF:
16347     case IX86_BUILTIN_VEC_EXT_V2DI:
16348     case IX86_BUILTIN_VEC_EXT_V4SF:
16349     case IX86_BUILTIN_VEC_EXT_V4SI:
16350     case IX86_BUILTIN_VEC_EXT_V8HI:
16351     case IX86_BUILTIN_VEC_EXT_V2SI:
16352     case IX86_BUILTIN_VEC_EXT_V4HI:
16353       return ix86_expand_vec_ext_builtin (arglist, target);
16354
16355     case IX86_BUILTIN_VEC_SET_V8HI:
16356     case IX86_BUILTIN_VEC_SET_V4HI:
16357       return ix86_expand_vec_set_builtin (arglist);
16358
16359     default:
16360       break;
16361     }
16362
16363   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
16364     if (d->code == fcode)
16365       {
16366         /* Compares are treated specially.  */
16367         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
16368             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
16369             || d->icode == CODE_FOR_sse2_maskcmpv2df3
16370             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
16371           return ix86_expand_sse_compare (d, arglist, target);
16372
16373         return ix86_expand_binop_builtin (d->icode, arglist, target);
16374       }
16375
16376   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
16377     if (d->code == fcode)
16378       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
16379
16380   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
16381     if (d->code == fcode)
16382       return ix86_expand_sse_comi (d, arglist, target);
16383
16384   gcc_unreachable ();
16385 }
16386
16387 /* Store OPERAND to the memory after reload is completed.  This means
16388    that we can't easily use assign_stack_local.  */
16389 rtx
16390 ix86_force_to_memory (enum machine_mode mode, rtx operand)
16391 {
16392   rtx result;
16393
16394   gcc_assert (reload_completed);
16395   if (TARGET_RED_ZONE)
16396     {
16397       result = gen_rtx_MEM (mode,
16398                             gen_rtx_PLUS (Pmode,
16399                                           stack_pointer_rtx,
16400                                           GEN_INT (-RED_ZONE_SIZE)));
16401       emit_move_insn (result, operand);
16402     }
16403   else if (!TARGET_RED_ZONE && TARGET_64BIT)
16404     {
16405       switch (mode)
16406         {
16407         case HImode:
16408         case SImode:
16409           operand = gen_lowpart (DImode, operand);
16410           /* FALLTHRU */
16411         case DImode:
16412           emit_insn (
16413                       gen_rtx_SET (VOIDmode,
16414                                    gen_rtx_MEM (DImode,
16415                                                 gen_rtx_PRE_DEC (DImode,
16416                                                         stack_pointer_rtx)),
16417                                    operand));
16418           break;
16419         default:
16420           gcc_unreachable ();
16421         }
16422       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16423     }
16424   else
16425     {
16426       switch (mode)
16427         {
16428         case DImode:
16429           {
16430             rtx operands[2];
16431             split_di (&operand, 1, operands, operands + 1);
16432             emit_insn (
16433                         gen_rtx_SET (VOIDmode,
16434                                      gen_rtx_MEM (SImode,
16435                                                   gen_rtx_PRE_DEC (Pmode,
16436                                                         stack_pointer_rtx)),
16437                                      operands[1]));
16438             emit_insn (
16439                         gen_rtx_SET (VOIDmode,
16440                                      gen_rtx_MEM (SImode,
16441                                                   gen_rtx_PRE_DEC (Pmode,
16442                                                         stack_pointer_rtx)),
16443                                      operands[0]));
16444           }
16445           break;
16446         case HImode:
16447           /* Store HImodes as SImodes.  */
16448           operand = gen_lowpart (SImode, operand);
16449           /* FALLTHRU */
16450         case SImode:
16451           emit_insn (
16452                       gen_rtx_SET (VOIDmode,
16453                                    gen_rtx_MEM (GET_MODE (operand),
16454                                                 gen_rtx_PRE_DEC (SImode,
16455                                                         stack_pointer_rtx)),
16456                                    operand));
16457           break;
16458         default:
16459           gcc_unreachable ();
16460         }
16461       result = gen_rtx_MEM (mode, stack_pointer_rtx);
16462     }
16463   return result;
16464 }
16465
16466 /* Free operand from the memory.  */
16467 void
16468 ix86_free_from_memory (enum machine_mode mode)
16469 {
16470   if (!TARGET_RED_ZONE)
16471     {
16472       int size;
16473
16474       if (mode == DImode || TARGET_64BIT)
16475         size = 8;
16476       else
16477         size = 4;
16478       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
16479          to pop or add instruction if registers are available.  */
16480       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
16481                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
16482                                             GEN_INT (size))));
16483     }
16484 }
16485
16486 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
16487    QImode must go into class Q_REGS.
16488    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
16489    movdf to do mem-to-mem moves through integer regs.  */
16490 enum reg_class
16491 ix86_preferred_reload_class (rtx x, enum reg_class class)
16492 {
16493   enum machine_mode mode = GET_MODE (x);
16494
16495   /* We're only allowed to return a subclass of CLASS.  Many of the
16496      following checks fail for NO_REGS, so eliminate that early.  */
16497   if (class == NO_REGS)
16498     return NO_REGS;
16499
16500   /* All classes can load zeros.  */
16501   if (x == CONST0_RTX (mode))
16502     return class;
16503
16504   /* Force constants into memory if we are loading a (nonzero) constant into
16505      an MMX or SSE register.  This is because there are no MMX/SSE instructions
16506      to load from a constant.  */
16507   if (CONSTANT_P (x)
16508       && (MAYBE_MMX_CLASS_P (class) || MAYBE_SSE_CLASS_P (class)))
16509     return NO_REGS;
16510
16511   /* Prefer SSE regs only, if we can use them for math.  */
16512   if (TARGET_SSE_MATH && !TARGET_MIX_SSE_I387 && SSE_FLOAT_MODE_P (mode))
16513     return SSE_CLASS_P (class) ? class : NO_REGS;
16514
16515   /* Floating-point constants need more complex checks.  */
16516   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
16517     {
16518       /* General regs can load everything.  */
16519       if (reg_class_subset_p (class, GENERAL_REGS))
16520         return class;
16521
16522       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
16523          zero above.  We only want to wind up preferring 80387 registers if
16524          we plan on doing computation with them.  */
16525       if (TARGET_80387
16526           && standard_80387_constant_p (x))
16527         {
16528           /* Limit class to non-sse.  */
16529           if (class == FLOAT_SSE_REGS)
16530             return FLOAT_REGS;
16531           if (class == FP_TOP_SSE_REGS)
16532             return FP_TOP_REG;
16533           if (class == FP_SECOND_SSE_REGS)
16534             return FP_SECOND_REG;
16535           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
16536             return class;
16537         }
16538
16539       return NO_REGS;
16540     }
16541
16542   /* Generally when we see PLUS here, it's the function invariant
16543      (plus soft-fp const_int).  Which can only be computed into general
16544      regs.  */
16545   if (GET_CODE (x) == PLUS)
16546     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
16547
16548   /* QImode constants are easy to load, but non-constant QImode data
16549      must go into Q_REGS.  */
16550   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
16551     {
16552       if (reg_class_subset_p (class, Q_REGS))
16553         return class;
16554       if (reg_class_subset_p (Q_REGS, class))
16555         return Q_REGS;
16556       return NO_REGS;
16557     }
16558
16559   return class;
16560 }
16561
16562 /* Discourage putting floating-point values in SSE registers unless
16563    SSE math is being used, and likewise for the 387 registers.  */
16564 enum reg_class
16565 ix86_preferred_output_reload_class (rtx x, enum reg_class class)
16566 {
16567   enum machine_mode mode = GET_MODE (x);
16568
16569   /* Restrict the output reload class to the register bank that we are doing
16570      math on.  If we would like not to return a subset of CLASS, reject this
16571      alternative: if reload cannot do this, it will still use its choice.  */
16572   mode = GET_MODE (x);
16573   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
16574     return MAYBE_SSE_CLASS_P (class) ? SSE_REGS : NO_REGS;
16575
16576   if (TARGET_80387 && SCALAR_FLOAT_MODE_P (mode))
16577     {
16578       if (class == FP_TOP_SSE_REGS)
16579         return FP_TOP_REG;
16580       else if (class == FP_SECOND_SSE_REGS)
16581         return FP_SECOND_REG;
16582       else
16583         return FLOAT_CLASS_P (class) ? class : NO_REGS;
16584     }
16585
16586   return class;
16587 }
16588
16589 /* If we are copying between general and FP registers, we need a memory
16590    location. The same is true for SSE and MMX registers.
16591
16592    The macro can't work reliably when one of the CLASSES is class containing
16593    registers from multiple units (SSE, MMX, integer).  We avoid this by never
16594    combining those units in single alternative in the machine description.
16595    Ensure that this constraint holds to avoid unexpected surprises.
16596
16597    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
16598    enforce these sanity checks.  */
16599
16600 int
16601 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
16602                               enum machine_mode mode, int strict)
16603 {
16604   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
16605       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
16606       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
16607       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
16608       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
16609       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
16610     {
16611       gcc_assert (!strict);
16612       return true;
16613     }
16614
16615   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
16616     return true;
16617
16618   /* ??? This is a lie.  We do have moves between mmx/general, and for
16619      mmx/sse2.  But by saying we need secondary memory we discourage the
16620      register allocator from using the mmx registers unless needed.  */
16621   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
16622     return true;
16623
16624   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16625     {
16626       /* SSE1 doesn't have any direct moves from other classes.  */
16627       if (!TARGET_SSE2)
16628         return true;
16629
16630       /* If the target says that inter-unit moves are more expensive
16631          than moving through memory, then don't generate them.  */
16632       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
16633         return true;
16634
16635       /* Between SSE and general, we have moves no larger than word size.  */
16636       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
16637         return true;
16638
16639       /* ??? For the cost of one register reformat penalty, we could use
16640          the same instructions to move SFmode and DFmode data, but the
16641          relevant move patterns don't support those alternatives.  */
16642       if (mode == SFmode || mode == DFmode)
16643         return true;
16644     }
16645
16646   return false;
16647 }
16648
16649 /* Return true if the registers in CLASS cannot represent the change from
16650    modes FROM to TO.  */
16651
16652 bool
16653 ix86_cannot_change_mode_class (enum machine_mode from, enum machine_mode to,
16654                                enum reg_class class)
16655 {
16656   if (from == to)
16657     return false;
16658
16659   /* x87 registers can't do subreg at all, as all values are reformatted
16660      to extended precision.  */
16661   if (MAYBE_FLOAT_CLASS_P (class))
16662     return true;
16663
16664   if (MAYBE_SSE_CLASS_P (class) || MAYBE_MMX_CLASS_P (class))
16665     {
16666       /* Vector registers do not support QI or HImode loads.  If we don't
16667          disallow a change to these modes, reload will assume it's ok to
16668          drop the subreg from (subreg:SI (reg:HI 100) 0).  This affects
16669          the vec_dupv4hi pattern.  */
16670       if (GET_MODE_SIZE (from) < 4)
16671         return true;
16672
16673       /* Vector registers do not support subreg with nonzero offsets, which
16674          are otherwise valid for integer registers.  Since we can't see
16675          whether we have a nonzero offset from here, prohibit all
16676          nonparadoxical subregs changing size.  */
16677       if (GET_MODE_SIZE (to) < GET_MODE_SIZE (from))
16678         return true;
16679     }
16680
16681   return false;
16682 }
16683
16684 /* Return the cost of moving data from a register in class CLASS1 to
16685    one in class CLASS2.
16686
16687    It is not required that the cost always equal 2 when FROM is the same as TO;
16688    on some machines it is expensive to move between registers if they are not
16689    general registers.  */
16690
16691 int
16692 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
16693                          enum reg_class class2)
16694 {
16695   /* In case we require secondary memory, compute cost of the store followed
16696      by load.  In order to avoid bad register allocation choices, we need
16697      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
16698
16699   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
16700     {
16701       int cost = 1;
16702
16703       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
16704                    MEMORY_MOVE_COST (mode, class1, 1));
16705       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
16706                    MEMORY_MOVE_COST (mode, class2, 1));
16707
16708       /* In case of copying from general_purpose_register we may emit multiple
16709          stores followed by single load causing memory size mismatch stall.
16710          Count this as arbitrarily high cost of 20.  */
16711       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
16712         cost += 20;
16713
16714       /* In the case of FP/MMX moves, the registers actually overlap, and we
16715          have to switch modes in order to treat them differently.  */
16716       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
16717           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
16718         cost += 20;
16719
16720       return cost;
16721     }
16722
16723   /* Moves between SSE/MMX and integer unit are expensive.  */
16724   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
16725       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
16726     return ix86_cost->mmxsse_to_integer;
16727   if (MAYBE_FLOAT_CLASS_P (class1))
16728     return ix86_cost->fp_move;
16729   if (MAYBE_SSE_CLASS_P (class1))
16730     return ix86_cost->sse_move;
16731   if (MAYBE_MMX_CLASS_P (class1))
16732     return ix86_cost->mmx_move;
16733   return 2;
16734 }
16735
16736 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
16737
16738 bool
16739 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
16740 {
16741   /* Flags and only flags can only hold CCmode values.  */
16742   if (CC_REGNO_P (regno))
16743     return GET_MODE_CLASS (mode) == MODE_CC;
16744   if (GET_MODE_CLASS (mode) == MODE_CC
16745       || GET_MODE_CLASS (mode) == MODE_RANDOM
16746       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
16747     return 0;
16748   if (FP_REGNO_P (regno))
16749     return VALID_FP_MODE_P (mode);
16750   if (SSE_REGNO_P (regno))
16751     {
16752       /* We implement the move patterns for all vector modes into and
16753          out of SSE registers, even when no operation instructions
16754          are available.  */
16755       return (VALID_SSE_REG_MODE (mode)
16756               || VALID_SSE2_REG_MODE (mode)
16757               || VALID_MMX_REG_MODE (mode)
16758               || VALID_MMX_REG_MODE_3DNOW (mode));
16759     }
16760   if (MMX_REGNO_P (regno))
16761     {
16762       /* We implement the move patterns for 3DNOW modes even in MMX mode,
16763          so if the register is available at all, then we can move data of
16764          the given mode into or out of it.  */
16765       return (VALID_MMX_REG_MODE (mode)
16766               || VALID_MMX_REG_MODE_3DNOW (mode));
16767     }
16768
16769   if (mode == QImode)
16770     {
16771       /* Take care for QImode values - they can be in non-QI regs,
16772          but then they do cause partial register stalls.  */
16773       if (regno < 4 || TARGET_64BIT)
16774         return 1;
16775       if (!TARGET_PARTIAL_REG_STALL)
16776         return 1;
16777       return reload_in_progress || reload_completed;
16778     }
16779   /* We handle both integer and floats in the general purpose registers.  */
16780   else if (VALID_INT_MODE_P (mode))
16781     return 1;
16782   else if (VALID_FP_MODE_P (mode))
16783     return 1;
16784   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
16785      on to use that value in smaller contexts, this can easily force a
16786      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
16787      supporting DImode, allow it.  */
16788   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
16789     return 1;
16790
16791   return 0;
16792 }
16793
16794 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a
16795    tieable integer mode.  */
16796
16797 static bool
16798 ix86_tieable_integer_mode_p (enum machine_mode mode)
16799 {
16800   switch (mode)
16801     {
16802     case HImode:
16803     case SImode:
16804       return true;
16805
16806     case QImode:
16807       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
16808
16809     case DImode:
16810       return TARGET_64BIT;
16811
16812     default:
16813       return false;
16814     }
16815 }
16816
16817 /* Return true if MODE1 is accessible in a register that can hold MODE2
16818    without copying.  That is, all register classes that can hold MODE2
16819    can also hold MODE1.  */
16820
16821 bool
16822 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
16823 {
16824   if (mode1 == mode2)
16825     return true;
16826
16827   if (ix86_tieable_integer_mode_p (mode1)
16828       && ix86_tieable_integer_mode_p (mode2))
16829     return true;
16830
16831   /* MODE2 being XFmode implies fp stack or general regs, which means we
16832      can tie any smaller floating point modes to it.  Note that we do not
16833      tie this with TFmode.  */
16834   if (mode2 == XFmode)
16835     return mode1 == SFmode || mode1 == DFmode;
16836
16837   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
16838      that we can tie it with SFmode.  */
16839   if (mode2 == DFmode)
16840     return mode1 == SFmode;
16841
16842   /* If MODE2 is only appropriate for an SSE register, then tie with
16843      any other mode acceptable to SSE registers.  */
16844   if (GET_MODE_SIZE (mode2) >= 8
16845       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
16846     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
16847
16848   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
16849      with any other mode acceptable to MMX registers.  */
16850   if (GET_MODE_SIZE (mode2) == 8
16851       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
16852     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
16853
16854   return false;
16855 }
16856
16857 /* Return the cost of moving data of mode M between a
16858    register and memory.  A value of 2 is the default; this cost is
16859    relative to those in `REGISTER_MOVE_COST'.
16860
16861    If moving between registers and memory is more expensive than
16862    between two registers, you should define this macro to express the
16863    relative cost.
16864
16865    Model also increased moving costs of QImode registers in non
16866    Q_REGS classes.
16867  */
16868 int
16869 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
16870 {
16871   if (FLOAT_CLASS_P (class))
16872     {
16873       int index;
16874       switch (mode)
16875         {
16876           case SFmode:
16877             index = 0;
16878             break;
16879           case DFmode:
16880             index = 1;
16881             break;
16882           case XFmode:
16883             index = 2;
16884             break;
16885           default:
16886             return 100;
16887         }
16888       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
16889     }
16890   if (SSE_CLASS_P (class))
16891     {
16892       int index;
16893       switch (GET_MODE_SIZE (mode))
16894         {
16895           case 4:
16896             index = 0;
16897             break;
16898           case 8:
16899             index = 1;
16900             break;
16901           case 16:
16902             index = 2;
16903             break;
16904           default:
16905             return 100;
16906         }
16907       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
16908     }
16909   if (MMX_CLASS_P (class))
16910     {
16911       int index;
16912       switch (GET_MODE_SIZE (mode))
16913         {
16914           case 4:
16915             index = 0;
16916             break;
16917           case 8:
16918             index = 1;
16919             break;
16920           default:
16921             return 100;
16922         }
16923       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
16924     }
16925   switch (GET_MODE_SIZE (mode))
16926     {
16927       case 1:
16928         if (in)
16929           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
16930                   : ix86_cost->movzbl_load);
16931         else
16932           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
16933                   : ix86_cost->int_store[0] + 4);
16934         break;
16935       case 2:
16936         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
16937       default:
16938         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
16939         if (mode == TFmode)
16940           mode = XFmode;
16941         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
16942                 * (((int) GET_MODE_SIZE (mode)
16943                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
16944     }
16945 }
16946
16947 /* Compute a (partial) cost for rtx X.  Return true if the complete
16948    cost has been computed, and false if subexpressions should be
16949    scanned.  In either case, *TOTAL contains the cost result.  */
16950
16951 static bool
16952 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
16953 {
16954   enum machine_mode mode = GET_MODE (x);
16955
16956   switch (code)
16957     {
16958     case CONST_INT:
16959     case CONST:
16960     case LABEL_REF:
16961     case SYMBOL_REF:
16962       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
16963         *total = 3;
16964       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
16965         *total = 2;
16966       else if (flag_pic && SYMBOLIC_CONST (x)
16967                && (!TARGET_64BIT
16968                    || (!GET_CODE (x) != LABEL_REF
16969                        && (GET_CODE (x) != SYMBOL_REF
16970                            || !SYMBOL_REF_LOCAL_P (x)))))
16971         *total = 1;
16972       else
16973         *total = 0;
16974       return true;
16975
16976     case CONST_DOUBLE:
16977       if (mode == VOIDmode)
16978         *total = 0;
16979       else
16980         switch (standard_80387_constant_p (x))
16981           {
16982           case 1: /* 0.0 */
16983             *total = 1;
16984             break;
16985           default: /* Other constants */
16986             *total = 2;
16987             break;
16988           case 0:
16989           case -1:
16990             /* Start with (MEM (SYMBOL_REF)), since that's where
16991                it'll probably end up.  Add a penalty for size.  */
16992             *total = (COSTS_N_INSNS (1)
16993                       + (flag_pic != 0 && !TARGET_64BIT)
16994                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
16995             break;
16996           }
16997       return true;
16998
16999     case ZERO_EXTEND:
17000       /* The zero extensions is often completely free on x86_64, so make
17001          it as cheap as possible.  */
17002       if (TARGET_64BIT && mode == DImode
17003           && GET_MODE (XEXP (x, 0)) == SImode)
17004         *total = 1;
17005       else if (TARGET_ZERO_EXTEND_WITH_AND)
17006         *total = ix86_cost->add;
17007       else
17008         *total = ix86_cost->movzx;
17009       return false;
17010
17011     case SIGN_EXTEND:
17012       *total = ix86_cost->movsx;
17013       return false;
17014
17015     case ASHIFT:
17016       if (GET_CODE (XEXP (x, 1)) == CONST_INT
17017           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
17018         {
17019           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
17020           if (value == 1)
17021             {
17022               *total = ix86_cost->add;
17023               return false;
17024             }
17025           if ((value == 2 || value == 3)
17026               && ix86_cost->lea <= ix86_cost->shift_const)
17027             {
17028               *total = ix86_cost->lea;
17029               return false;
17030             }
17031         }
17032       /* FALLTHRU */
17033
17034     case ROTATE:
17035     case ASHIFTRT:
17036     case LSHIFTRT:
17037     case ROTATERT:
17038       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
17039         {
17040           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17041             {
17042               if (INTVAL (XEXP (x, 1)) > 32)
17043                 *total = ix86_cost->shift_const + COSTS_N_INSNS (2);
17044               else
17045                 *total = ix86_cost->shift_const * 2;
17046             }
17047           else
17048             {
17049               if (GET_CODE (XEXP (x, 1)) == AND)
17050                 *total = ix86_cost->shift_var * 2;
17051               else
17052                 *total = ix86_cost->shift_var * 6 + COSTS_N_INSNS (2);
17053             }
17054         }
17055       else
17056         {
17057           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17058             *total = ix86_cost->shift_const;
17059           else
17060             *total = ix86_cost->shift_var;
17061         }
17062       return false;
17063
17064     case MULT:
17065       if (FLOAT_MODE_P (mode))
17066         {
17067           *total = ix86_cost->fmul;
17068           return false;
17069         }
17070       else
17071         {
17072           rtx op0 = XEXP (x, 0);
17073           rtx op1 = XEXP (x, 1);
17074           int nbits;
17075           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
17076             {
17077               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
17078               for (nbits = 0; value != 0; value &= value - 1)
17079                 nbits++;
17080             }
17081           else
17082             /* This is arbitrary.  */
17083             nbits = 7;
17084
17085           /* Compute costs correctly for widening multiplication.  */
17086           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
17087               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
17088                  == GET_MODE_SIZE (mode))
17089             {
17090               int is_mulwiden = 0;
17091               enum machine_mode inner_mode = GET_MODE (op0);
17092
17093               if (GET_CODE (op0) == GET_CODE (op1))
17094                 is_mulwiden = 1, op1 = XEXP (op1, 0);
17095               else if (GET_CODE (op1) == CONST_INT)
17096                 {
17097                   if (GET_CODE (op0) == SIGN_EXTEND)
17098                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
17099                                   == INTVAL (op1);
17100                   else
17101                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
17102                 }
17103
17104               if (is_mulwiden)
17105                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
17106             }
17107
17108           *total = (ix86_cost->mult_init[MODE_INDEX (mode)]
17109                     + nbits * ix86_cost->mult_bit
17110                     + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code));
17111
17112           return true;
17113         }
17114
17115     case DIV:
17116     case UDIV:
17117     case MOD:
17118     case UMOD:
17119       if (FLOAT_MODE_P (mode))
17120         *total = ix86_cost->fdiv;
17121       else
17122         *total = ix86_cost->divide[MODE_INDEX (mode)];
17123       return false;
17124
17125     case PLUS:
17126       if (FLOAT_MODE_P (mode))
17127         *total = ix86_cost->fadd;
17128       else if (GET_MODE_CLASS (mode) == MODE_INT
17129                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
17130         {
17131           if (GET_CODE (XEXP (x, 0)) == PLUS
17132               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
17133               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
17134               && CONSTANT_P (XEXP (x, 1)))
17135             {
17136               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
17137               if (val == 2 || val == 4 || val == 8)
17138                 {
17139                   *total = ix86_cost->lea;
17140                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
17141                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
17142                                       outer_code);
17143                   *total += rtx_cost (XEXP (x, 1), outer_code);
17144                   return true;
17145                 }
17146             }
17147           else if (GET_CODE (XEXP (x, 0)) == MULT
17148                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
17149             {
17150               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
17151               if (val == 2 || val == 4 || val == 8)
17152                 {
17153                   *total = ix86_cost->lea;
17154                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
17155                   *total += rtx_cost (XEXP (x, 1), outer_code);
17156                   return true;
17157                 }
17158             }
17159           else if (GET_CODE (XEXP (x, 0)) == PLUS)
17160             {
17161               *total = ix86_cost->lea;
17162               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
17163               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
17164               *total += rtx_cost (XEXP (x, 1), outer_code);
17165               return true;
17166             }
17167         }
17168       /* FALLTHRU */
17169
17170     case MINUS:
17171       if (FLOAT_MODE_P (mode))
17172         {
17173           *total = ix86_cost->fadd;
17174           return false;
17175         }
17176       /* FALLTHRU */
17177
17178     case AND:
17179     case IOR:
17180     case XOR:
17181       if (!TARGET_64BIT && mode == DImode)
17182         {
17183           *total = (ix86_cost->add * 2
17184                     + (rtx_cost (XEXP (x, 0), outer_code)
17185                        << (GET_MODE (XEXP (x, 0)) != DImode))
17186                     + (rtx_cost (XEXP (x, 1), outer_code)
17187                        << (GET_MODE (XEXP (x, 1)) != DImode)));
17188           return true;
17189         }
17190       /* FALLTHRU */
17191
17192     case NEG:
17193       if (FLOAT_MODE_P (mode))
17194         {
17195           *total = ix86_cost->fchs;
17196           return false;
17197         }
17198       /* FALLTHRU */
17199
17200     case NOT:
17201       if (!TARGET_64BIT && mode == DImode)
17202         *total = ix86_cost->add * 2;
17203       else
17204         *total = ix86_cost->add;
17205       return false;
17206
17207     case COMPARE:
17208       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
17209           && XEXP (XEXP (x, 0), 1) == const1_rtx
17210           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
17211           && XEXP (x, 1) == const0_rtx)
17212         {
17213           /* This kind of construct is implemented using test[bwl].
17214              Treat it as if we had an AND.  */
17215           *total = (ix86_cost->add
17216                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
17217                     + rtx_cost (const1_rtx, outer_code));
17218           return true;
17219         }
17220       return false;
17221
17222     case FLOAT_EXTEND:
17223       if (!TARGET_SSE_MATH
17224           || mode == XFmode
17225           || (mode == DFmode && !TARGET_SSE2))
17226         /* For standard 80387 constants, raise the cost to prevent
17227            compress_float_constant() to generate load from memory.  */
17228         switch (standard_80387_constant_p (XEXP (x, 0)))
17229           {
17230           case -1:
17231           case 0:
17232             *total = 0;
17233             break;
17234           case 1: /* 0.0 */
17235             *total = 1;
17236             break;
17237           default:
17238             *total = (x86_ext_80387_constants & TUNEMASK
17239                       || optimize_size
17240                       ? 1 : 0);
17241           }
17242       return false;
17243
17244     case ABS:
17245       if (FLOAT_MODE_P (mode))
17246         *total = ix86_cost->fabs;
17247       return false;
17248
17249     case SQRT:
17250       if (FLOAT_MODE_P (mode))
17251         *total = ix86_cost->fsqrt;
17252       return false;
17253
17254     case UNSPEC:
17255       if (XINT (x, 1) == UNSPEC_TP)
17256         *total = 0;
17257       return false;
17258
17259     default:
17260       return false;
17261     }
17262 }
17263
17264 #if TARGET_MACHO
17265
17266 static int current_machopic_label_num;
17267
17268 /* Given a symbol name and its associated stub, write out the
17269    definition of the stub.  */
17270
17271 void
17272 machopic_output_stub (FILE *file, const char *symb, const char *stub)
17273 {
17274   unsigned int length;
17275   char *binder_name, *symbol_name, lazy_ptr_name[32];
17276   int label = ++current_machopic_label_num;
17277
17278   /* For 64-bit we shouldn't get here.  */
17279   gcc_assert (!TARGET_64BIT);
17280
17281   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
17282   symb = (*targetm.strip_name_encoding) (symb);
17283
17284   length = strlen (stub);
17285   binder_name = alloca (length + 32);
17286   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
17287
17288   length = strlen (symb);
17289   symbol_name = alloca (length + 32);
17290   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
17291
17292   sprintf (lazy_ptr_name, "L%d$lz", label);
17293
17294   if (MACHOPIC_PURE)
17295     switch_to_section (darwin_sections[machopic_picsymbol_stub_section]);
17296   else
17297     switch_to_section (darwin_sections[machopic_symbol_stub_section]);
17298
17299   fprintf (file, "%s:\n", stub);
17300   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17301
17302   if (MACHOPIC_PURE)
17303     {
17304       fprintf (file, "\tcall\tLPC$%d\nLPC$%d:\tpopl\t%%eax\n", label, label);
17305       fprintf (file, "\tmovl\t%s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
17306       fprintf (file, "\tjmp\t*%%edx\n");
17307     }
17308   else
17309     fprintf (file, "\tjmp\t*%s\n", lazy_ptr_name);
17310
17311   fprintf (file, "%s:\n", binder_name);
17312
17313   if (MACHOPIC_PURE)
17314     {
17315       fprintf (file, "\tlea\t%s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
17316       fprintf (file, "\tpushl\t%%eax\n");
17317     }
17318   else
17319     fprintf (file, "\tpushl\t$%s\n", lazy_ptr_name);
17320
17321   fprintf (file, "\tjmp\tdyld_stub_binding_helper\n");
17322
17323   switch_to_section (darwin_sections[machopic_lazy_symbol_ptr_section]);
17324   fprintf (file, "%s:\n", lazy_ptr_name);
17325   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
17326   fprintf (file, "\t.long %s\n", binder_name);
17327 }
17328
17329 void
17330 darwin_x86_file_end (void)
17331 {
17332   darwin_file_end ();
17333   ix86_file_end ();
17334 }
17335 #endif /* TARGET_MACHO */
17336
17337 /* Order the registers for register allocator.  */
17338
17339 void
17340 x86_order_regs_for_local_alloc (void)
17341 {
17342    int pos = 0;
17343    int i;
17344
17345    /* First allocate the local general purpose registers.  */
17346    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17347      if (GENERAL_REGNO_P (i) && call_used_regs[i])
17348         reg_alloc_order [pos++] = i;
17349
17350    /* Global general purpose registers.  */
17351    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
17352      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
17353         reg_alloc_order [pos++] = i;
17354
17355    /* x87 registers come first in case we are doing FP math
17356       using them.  */
17357    if (!TARGET_SSE_MATH)
17358      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17359        reg_alloc_order [pos++] = i;
17360
17361    /* SSE registers.  */
17362    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
17363      reg_alloc_order [pos++] = i;
17364    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
17365      reg_alloc_order [pos++] = i;
17366
17367    /* x87 registers.  */
17368    if (TARGET_SSE_MATH)
17369      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
17370        reg_alloc_order [pos++] = i;
17371
17372    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
17373      reg_alloc_order [pos++] = i;
17374
17375    /* Initialize the rest of array as we do not allocate some registers
17376       at all.  */
17377    while (pos < FIRST_PSEUDO_REGISTER)
17378      reg_alloc_order [pos++] = 0;
17379 }
17380
17381 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
17382    struct attribute_spec.handler.  */
17383 static tree
17384 ix86_handle_struct_attribute (tree *node, tree name,
17385                               tree args ATTRIBUTE_UNUSED,
17386                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
17387 {
17388   tree *type = NULL;
17389   if (DECL_P (*node))
17390     {
17391       if (TREE_CODE (*node) == TYPE_DECL)
17392         type = &TREE_TYPE (*node);
17393     }
17394   else
17395     type = node;
17396
17397   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
17398                  || TREE_CODE (*type) == UNION_TYPE)))
17399     {
17400       warning (OPT_Wattributes, "%qs attribute ignored",
17401                IDENTIFIER_POINTER (name));
17402       *no_add_attrs = true;
17403     }
17404
17405   else if ((is_attribute_p ("ms_struct", name)
17406             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
17407            || ((is_attribute_p ("gcc_struct", name)
17408                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
17409     {
17410       warning (OPT_Wattributes, "%qs incompatible attribute ignored",
17411                IDENTIFIER_POINTER (name));
17412       *no_add_attrs = true;
17413     }
17414
17415   return NULL_TREE;
17416 }
17417
17418 static bool
17419 ix86_ms_bitfield_layout_p (tree record_type)
17420 {
17421   return (TARGET_MS_BITFIELD_LAYOUT &&
17422           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
17423     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
17424 }
17425
17426 /* Returns an expression indicating where the this parameter is
17427    located on entry to the FUNCTION.  */
17428
17429 static rtx
17430 x86_this_parameter (tree function)
17431 {
17432   tree type = TREE_TYPE (function);
17433
17434   if (TARGET_64BIT)
17435     {
17436       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
17437       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
17438     }
17439
17440   if (ix86_function_regparm (type, function) > 0)
17441     {
17442       tree parm;
17443
17444       parm = TYPE_ARG_TYPES (type);
17445       /* Figure out whether or not the function has a variable number of
17446          arguments.  */
17447       for (; parm; parm = TREE_CHAIN (parm))
17448         if (TREE_VALUE (parm) == void_type_node)
17449           break;
17450       /* If not, the this parameter is in the first argument.  */
17451       if (parm)
17452         {
17453           int regno = 0;
17454           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
17455             regno = 2;
17456           return gen_rtx_REG (SImode, regno);
17457         }
17458     }
17459
17460   if (aggregate_value_p (TREE_TYPE (type), type))
17461     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
17462   else
17463     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
17464 }
17465
17466 /* Determine whether x86_output_mi_thunk can succeed.  */
17467
17468 static bool
17469 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
17470                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
17471                          HOST_WIDE_INT vcall_offset, tree function)
17472 {
17473   /* 64-bit can handle anything.  */
17474   if (TARGET_64BIT)
17475     return true;
17476
17477   /* For 32-bit, everything's fine if we have one free register.  */
17478   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
17479     return true;
17480
17481   /* Need a free register for vcall_offset.  */
17482   if (vcall_offset)
17483     return false;
17484
17485   /* Need a free register for GOT references.  */
17486   if (flag_pic && !(*targetm.binds_local_p) (function))
17487     return false;
17488
17489   /* Otherwise ok.  */
17490   return true;
17491 }
17492
17493 /* Output the assembler code for a thunk function.  THUNK_DECL is the
17494    declaration for the thunk function itself, FUNCTION is the decl for
17495    the target function.  DELTA is an immediate constant offset to be
17496    added to THIS.  If VCALL_OFFSET is nonzero, the word at
17497    *(*this + vcall_offset) should be added to THIS.  */
17498
17499 static void
17500 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
17501                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
17502                      HOST_WIDE_INT vcall_offset, tree function)
17503 {
17504   rtx xops[3];
17505   rtx this = x86_this_parameter (function);
17506   rtx this_reg, tmp;
17507
17508   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
17509      pull it in now and let DELTA benefit.  */
17510   if (REG_P (this))
17511     this_reg = this;
17512   else if (vcall_offset)
17513     {
17514       /* Put the this parameter into %eax.  */
17515       xops[0] = this;
17516       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
17517       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17518     }
17519   else
17520     this_reg = NULL_RTX;
17521
17522   /* Adjust the this parameter by a fixed constant.  */
17523   if (delta)
17524     {
17525       xops[0] = GEN_INT (delta);
17526       xops[1] = this_reg ? this_reg : this;
17527       if (TARGET_64BIT)
17528         {
17529           if (!x86_64_general_operand (xops[0], DImode))
17530             {
17531               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17532               xops[1] = tmp;
17533               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
17534               xops[0] = tmp;
17535               xops[1] = this;
17536             }
17537           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17538         }
17539       else
17540         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17541     }
17542
17543   /* Adjust the this parameter by a value stored in the vtable.  */
17544   if (vcall_offset)
17545     {
17546       if (TARGET_64BIT)
17547         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
17548       else
17549         {
17550           int tmp_regno = 2 /* ECX */;
17551           if (lookup_attribute ("fastcall",
17552               TYPE_ATTRIBUTES (TREE_TYPE (function))))
17553             tmp_regno = 0 /* EAX */;
17554           tmp = gen_rtx_REG (SImode, tmp_regno);
17555         }
17556
17557       xops[0] = gen_rtx_MEM (Pmode, this_reg);
17558       xops[1] = tmp;
17559       if (TARGET_64BIT)
17560         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17561       else
17562         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17563
17564       /* Adjust the this parameter.  */
17565       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
17566       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
17567         {
17568           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
17569           xops[0] = GEN_INT (vcall_offset);
17570           xops[1] = tmp2;
17571           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
17572           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
17573         }
17574       xops[1] = this_reg;
17575       if (TARGET_64BIT)
17576         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
17577       else
17578         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
17579     }
17580
17581   /* If necessary, drop THIS back to its stack slot.  */
17582   if (this_reg && this_reg != this)
17583     {
17584       xops[0] = this_reg;
17585       xops[1] = this;
17586       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
17587     }
17588
17589   xops[0] = XEXP (DECL_RTL (function), 0);
17590   if (TARGET_64BIT)
17591     {
17592       if (!flag_pic || (*targetm.binds_local_p) (function))
17593         output_asm_insn ("jmp\t%P0", xops);
17594       else
17595         {
17596           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
17597           tmp = gen_rtx_CONST (Pmode, tmp);
17598           tmp = gen_rtx_MEM (QImode, tmp);
17599           xops[0] = tmp;
17600           output_asm_insn ("jmp\t%A0", xops);
17601         }
17602     }
17603   else
17604     {
17605       if (!flag_pic || (*targetm.binds_local_p) (function))
17606         output_asm_insn ("jmp\t%P0", xops);
17607       else
17608 #if TARGET_MACHO
17609         if (TARGET_MACHO)
17610           {
17611             rtx sym_ref = XEXP (DECL_RTL (function), 0);
17612             tmp = (gen_rtx_SYMBOL_REF
17613                    (Pmode,
17614                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
17615             tmp = gen_rtx_MEM (QImode, tmp);
17616             xops[0] = tmp;
17617             output_asm_insn ("jmp\t%0", xops);
17618           }
17619         else
17620 #endif /* TARGET_MACHO */
17621         {
17622           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
17623           output_set_got (tmp, NULL_RTX);
17624
17625           xops[1] = tmp;
17626           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
17627           output_asm_insn ("jmp\t{*}%1", xops);
17628         }
17629     }
17630 }
17631
17632 static void
17633 x86_file_start (void)
17634 {
17635   default_file_start ();
17636 #if TARGET_MACHO
17637   darwin_file_start ();
17638 #endif
17639   if (X86_FILE_START_VERSION_DIRECTIVE)
17640     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
17641   if (X86_FILE_START_FLTUSED)
17642     fputs ("\t.global\t__fltused\n", asm_out_file);
17643   if (ix86_asm_dialect == ASM_INTEL)
17644     fputs ("\t.intel_syntax\n", asm_out_file);
17645 }
17646
17647 int
17648 x86_field_alignment (tree field, int computed)
17649 {
17650   enum machine_mode mode;
17651   tree type = TREE_TYPE (field);
17652
17653   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
17654     return computed;
17655   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
17656                     ? get_inner_array_type (type) : type);
17657   if (mode == DFmode || mode == DCmode
17658       || GET_MODE_CLASS (mode) == MODE_INT
17659       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
17660     return MIN (32, computed);
17661   return computed;
17662 }
17663
17664 /* Output assembler code to FILE to increment profiler label # LABELNO
17665    for profiling a function entry.  */
17666 void
17667 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
17668 {
17669   if (TARGET_64BIT)
17670     if (flag_pic)
17671       {
17672 #ifndef NO_PROFILE_COUNTERS
17673         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
17674 #endif
17675         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
17676       }
17677     else
17678       {
17679 #ifndef NO_PROFILE_COUNTERS
17680         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
17681 #endif
17682         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17683       }
17684   else if (flag_pic)
17685     {
17686 #ifndef NO_PROFILE_COUNTERS
17687       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
17688                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
17689 #endif
17690       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
17691     }
17692   else
17693     {
17694 #ifndef NO_PROFILE_COUNTERS
17695       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
17696                PROFILE_COUNT_REGISTER);
17697 #endif
17698       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
17699     }
17700 }
17701
17702 /* We don't have exact information about the insn sizes, but we may assume
17703    quite safely that we are informed about all 1 byte insns and memory
17704    address sizes.  This is enough to eliminate unnecessary padding in
17705    99% of cases.  */
17706
17707 static int
17708 min_insn_size (rtx insn)
17709 {
17710   int l = 0;
17711
17712   if (!INSN_P (insn) || !active_insn_p (insn))
17713     return 0;
17714
17715   /* Discard alignments we've emit and jump instructions.  */
17716   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
17717       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
17718     return 0;
17719   if (GET_CODE (insn) == JUMP_INSN
17720       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
17721           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
17722     return 0;
17723
17724   /* Important case - calls are always 5 bytes.
17725      It is common to have many calls in the row.  */
17726   if (GET_CODE (insn) == CALL_INSN
17727       && symbolic_reference_mentioned_p (PATTERN (insn))
17728       && !SIBLING_CALL_P (insn))
17729     return 5;
17730   if (get_attr_length (insn) <= 1)
17731     return 1;
17732
17733   /* For normal instructions we may rely on the sizes of addresses
17734      and the presence of symbol to require 4 bytes of encoding.
17735      This is not the case for jumps where references are PC relative.  */
17736   if (GET_CODE (insn) != JUMP_INSN)
17737     {
17738       l = get_attr_length_address (insn);
17739       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
17740         l = 4;
17741     }
17742   if (l)
17743     return 1+l;
17744   else
17745     return 2;
17746 }
17747
17748 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
17749    window.  */
17750
17751 static void
17752 ix86_avoid_jump_misspredicts (void)
17753 {
17754   rtx insn, start = get_insns ();
17755   int nbytes = 0, njumps = 0;
17756   int isjump = 0;
17757
17758   /* Look for all minimal intervals of instructions containing 4 jumps.
17759      The intervals are bounded by START and INSN.  NBYTES is the total
17760      size of instructions in the interval including INSN and not including
17761      START.  When the NBYTES is smaller than 16 bytes, it is possible
17762      that the end of START and INSN ends up in the same 16byte page.
17763
17764      The smallest offset in the page INSN can start is the case where START
17765      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
17766      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
17767      */
17768   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
17769     {
17770
17771       nbytes += min_insn_size (insn);
17772       if (dump_file)
17773         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
17774                 INSN_UID (insn), min_insn_size (insn));
17775       if ((GET_CODE (insn) == JUMP_INSN
17776            && GET_CODE (PATTERN (insn)) != ADDR_VEC
17777            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
17778           || GET_CODE (insn) == CALL_INSN)
17779         njumps++;
17780       else
17781         continue;
17782
17783       while (njumps > 3)
17784         {
17785           start = NEXT_INSN (start);
17786           if ((GET_CODE (start) == JUMP_INSN
17787                && GET_CODE (PATTERN (start)) != ADDR_VEC
17788                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
17789               || GET_CODE (start) == CALL_INSN)
17790             njumps--, isjump = 1;
17791           else
17792             isjump = 0;
17793           nbytes -= min_insn_size (start);
17794         }
17795       gcc_assert (njumps >= 0);
17796       if (dump_file)
17797         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
17798                 INSN_UID (start), INSN_UID (insn), nbytes);
17799
17800       if (njumps == 3 && isjump && nbytes < 16)
17801         {
17802           int padsize = 15 - nbytes + min_insn_size (insn);
17803
17804           if (dump_file)
17805             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
17806                      INSN_UID (insn), padsize);
17807           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
17808         }
17809     }
17810 }
17811
17812 /* AMD Athlon works faster
17813    when RET is not destination of conditional jump or directly preceded
17814    by other jump instruction.  We avoid the penalty by inserting NOP just
17815    before the RET instructions in such cases.  */
17816 static void
17817 ix86_pad_returns (void)
17818 {
17819   edge e;
17820   edge_iterator ei;
17821
17822   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
17823     {
17824       basic_block bb = e->src;
17825       rtx ret = BB_END (bb);
17826       rtx prev;
17827       bool replace = false;
17828
17829       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
17830           || !maybe_hot_bb_p (bb))
17831         continue;
17832       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
17833         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
17834           break;
17835       if (prev && GET_CODE (prev) == CODE_LABEL)
17836         {
17837           edge e;
17838           edge_iterator ei;
17839
17840           FOR_EACH_EDGE (e, ei, bb->preds)
17841             if (EDGE_FREQUENCY (e) && e->src->index >= 0
17842                 && !(e->flags & EDGE_FALLTHRU))
17843               replace = true;
17844         }
17845       if (!replace)
17846         {
17847           prev = prev_active_insn (ret);
17848           if (prev
17849               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
17850                   || GET_CODE (prev) == CALL_INSN))
17851             replace = true;
17852           /* Empty functions get branch mispredict even when the jump destination
17853              is not visible to us.  */
17854           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
17855             replace = true;
17856         }
17857       if (replace)
17858         {
17859           emit_insn_before (gen_return_internal_long (), ret);
17860           delete_insn (ret);
17861         }
17862     }
17863 }
17864
17865 /* Implement machine specific optimizations.  We implement padding of returns
17866    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
17867 static void
17868 ix86_reorg (void)
17869 {
17870   if (TARGET_PAD_RETURNS && optimize && !optimize_size)
17871     ix86_pad_returns ();
17872   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
17873     ix86_avoid_jump_misspredicts ();
17874 }
17875
17876 /* Return nonzero when QImode register that must be represented via REX prefix
17877    is used.  */
17878 bool
17879 x86_extended_QIreg_mentioned_p (rtx insn)
17880 {
17881   int i;
17882   extract_insn_cached (insn);
17883   for (i = 0; i < recog_data.n_operands; i++)
17884     if (REG_P (recog_data.operand[i])
17885         && REGNO (recog_data.operand[i]) >= 4)
17886        return true;
17887   return false;
17888 }
17889
17890 /* Return nonzero when P points to register encoded via REX prefix.
17891    Called via for_each_rtx.  */
17892 static int
17893 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
17894 {
17895    unsigned int regno;
17896    if (!REG_P (*p))
17897      return 0;
17898    regno = REGNO (*p);
17899    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
17900 }
17901
17902 /* Return true when INSN mentions register that must be encoded using REX
17903    prefix.  */
17904 bool
17905 x86_extended_reg_mentioned_p (rtx insn)
17906 {
17907   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
17908 }
17909
17910 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
17911    optabs would emit if we didn't have TFmode patterns.  */
17912
17913 void
17914 x86_emit_floatuns (rtx operands[2])
17915 {
17916   rtx neglab, donelab, i0, i1, f0, in, out;
17917   enum machine_mode mode, inmode;
17918
17919   inmode = GET_MODE (operands[1]);
17920   gcc_assert (inmode == SImode || inmode == DImode);
17921
17922   out = operands[0];
17923   in = force_reg (inmode, operands[1]);
17924   mode = GET_MODE (out);
17925   neglab = gen_label_rtx ();
17926   donelab = gen_label_rtx ();
17927   i1 = gen_reg_rtx (Pmode);
17928   f0 = gen_reg_rtx (mode);
17929
17930   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
17931
17932   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
17933   emit_jump_insn (gen_jump (donelab));
17934   emit_barrier ();
17935
17936   emit_label (neglab);
17937
17938   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17939   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
17940   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
17941   expand_float (f0, i0, 0);
17942   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
17943
17944   emit_label (donelab);
17945 }
17946 \f
17947 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
17948    with all elements equal to VAR.  Return true if successful.  */
17949
17950 static bool
17951 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
17952                                    rtx target, rtx val)
17953 {
17954   enum machine_mode smode, wsmode, wvmode;
17955   rtx x;
17956
17957   switch (mode)
17958     {
17959     case V2SImode:
17960     case V2SFmode:
17961       if (!mmx_ok)
17962         return false;
17963       /* FALLTHRU */
17964
17965     case V2DFmode:
17966     case V2DImode:
17967     case V4SFmode:
17968     case V4SImode:
17969       val = force_reg (GET_MODE_INNER (mode), val);
17970       x = gen_rtx_VEC_DUPLICATE (mode, val);
17971       emit_insn (gen_rtx_SET (VOIDmode, target, x));
17972       return true;
17973
17974     case V4HImode:
17975       if (!mmx_ok)
17976         return false;
17977       if (TARGET_SSE || TARGET_3DNOW_A)
17978         {
17979           val = gen_lowpart (SImode, val);
17980           x = gen_rtx_TRUNCATE (HImode, val);
17981           x = gen_rtx_VEC_DUPLICATE (mode, x);
17982           emit_insn (gen_rtx_SET (VOIDmode, target, x));
17983           return true;
17984         }
17985       else
17986         {
17987           smode = HImode;
17988           wsmode = SImode;
17989           wvmode = V2SImode;
17990           goto widen;
17991         }
17992
17993     case V8QImode:
17994       if (!mmx_ok)
17995         return false;
17996       smode = QImode;
17997       wsmode = HImode;
17998       wvmode = V4HImode;
17999       goto widen;
18000     case V8HImode:
18001       if (TARGET_SSE2)
18002         {
18003           rtx tmp1, tmp2;
18004           /* Extend HImode to SImode using a paradoxical SUBREG.  */
18005           tmp1 = gen_reg_rtx (SImode);
18006           emit_move_insn (tmp1, gen_lowpart (SImode, val));
18007           /* Insert the SImode value as low element of V4SImode vector. */
18008           tmp2 = gen_reg_rtx (V4SImode);
18009           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
18010                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
18011                                     CONST0_RTX (V4SImode),
18012                                     const1_rtx);
18013           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
18014           /* Cast the V4SImode vector back to a V8HImode vector.  */
18015           tmp1 = gen_reg_rtx (V8HImode);
18016           emit_move_insn (tmp1, gen_lowpart (V8HImode, tmp2));
18017           /* Duplicate the low short through the whole low SImode word.  */
18018           emit_insn (gen_sse2_punpcklwd (tmp1, tmp1, tmp1));
18019           /* Cast the V8HImode vector back to a V4SImode vector.  */
18020           tmp2 = gen_reg_rtx (V4SImode);
18021           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
18022           /* Replicate the low element of the V4SImode vector.  */
18023           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
18024           /* Cast the V2SImode back to V8HImode, and store in target.  */
18025           emit_move_insn (target, gen_lowpart (V8HImode, tmp2));
18026           return true;
18027         }
18028       smode = HImode;
18029       wsmode = SImode;
18030       wvmode = V4SImode;
18031       goto widen;
18032     case V16QImode:
18033       if (TARGET_SSE2)
18034         {
18035           rtx tmp1, tmp2;
18036           /* Extend QImode to SImode using a paradoxical SUBREG.  */
18037           tmp1 = gen_reg_rtx (SImode);
18038           emit_move_insn (tmp1, gen_lowpart (SImode, val));
18039           /* Insert the SImode value as low element of V4SImode vector. */
18040           tmp2 = gen_reg_rtx (V4SImode);
18041           tmp1 = gen_rtx_VEC_MERGE (V4SImode,
18042                                     gen_rtx_VEC_DUPLICATE (V4SImode, tmp1),
18043                                     CONST0_RTX (V4SImode),
18044                                     const1_rtx);
18045           emit_insn (gen_rtx_SET (VOIDmode, tmp2, tmp1));
18046           /* Cast the V4SImode vector back to a V16QImode vector.  */
18047           tmp1 = gen_reg_rtx (V16QImode);
18048           emit_move_insn (tmp1, gen_lowpart (V16QImode, tmp2));
18049           /* Duplicate the low byte through the whole low SImode word.  */
18050           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
18051           emit_insn (gen_sse2_punpcklbw (tmp1, tmp1, tmp1));
18052           /* Cast the V16QImode vector back to a V4SImode vector.  */
18053           tmp2 = gen_reg_rtx (V4SImode);
18054           emit_move_insn (tmp2, gen_lowpart (V4SImode, tmp1));
18055           /* Replicate the low element of the V4SImode vector.  */
18056           emit_insn (gen_sse2_pshufd (tmp2, tmp2, const0_rtx));
18057           /* Cast the V2SImode back to V16QImode, and store in target.  */
18058           emit_move_insn (target, gen_lowpart (V16QImode, tmp2));
18059           return true;
18060         }
18061       smode = QImode;
18062       wsmode = HImode;
18063       wvmode = V8HImode;
18064       goto widen;
18065     widen:
18066       /* Replicate the value once into the next wider mode and recurse.  */
18067       val = convert_modes (wsmode, smode, val, true);
18068       x = expand_simple_binop (wsmode, ASHIFT, val,
18069                                GEN_INT (GET_MODE_BITSIZE (smode)),
18070                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
18071       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
18072
18073       x = gen_reg_rtx (wvmode);
18074       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
18075         gcc_unreachable ();
18076       emit_move_insn (target, gen_lowpart (mode, x));
18077       return true;
18078
18079     default:
18080       return false;
18081     }
18082 }
18083
18084 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
18085    whose ONE_VAR element is VAR, and other elements are zero.  Return true
18086    if successful.  */
18087
18088 static bool
18089 ix86_expand_vector_init_one_nonzero (bool mmx_ok, enum machine_mode mode,
18090                                      rtx target, rtx var, int one_var)
18091 {
18092   enum machine_mode vsimode;
18093   rtx new_target;
18094   rtx x, tmp;
18095
18096   switch (mode)
18097     {
18098     case V2SFmode:
18099     case V2SImode:
18100       if (!mmx_ok)
18101         return false;
18102       /* FALLTHRU */
18103
18104     case V2DFmode:
18105     case V2DImode:
18106       if (one_var != 0)
18107         return false;
18108       var = force_reg (GET_MODE_INNER (mode), var);
18109       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
18110       emit_insn (gen_rtx_SET (VOIDmode, target, x));
18111       return true;
18112
18113     case V4SFmode:
18114     case V4SImode:
18115       if (!REG_P (target) || REGNO (target) < FIRST_PSEUDO_REGISTER)
18116         new_target = gen_reg_rtx (mode);
18117       else
18118         new_target = target;
18119       var = force_reg (GET_MODE_INNER (mode), var);
18120       x = gen_rtx_VEC_DUPLICATE (mode, var);
18121       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
18122       emit_insn (gen_rtx_SET (VOIDmode, new_target, x));
18123       if (one_var != 0)
18124         {
18125           /* We need to shuffle the value to the correct position, so
18126              create a new pseudo to store the intermediate result.  */
18127
18128           /* With SSE2, we can use the integer shuffle insns.  */
18129           if (mode != V4SFmode && TARGET_SSE2)
18130             {
18131               emit_insn (gen_sse2_pshufd_1 (new_target, new_target,
18132                                             GEN_INT (1),
18133                                             GEN_INT (one_var == 1 ? 0 : 1),
18134                                             GEN_INT (one_var == 2 ? 0 : 1),
18135                                             GEN_INT (one_var == 3 ? 0 : 1)));
18136               if (target != new_target)
18137                 emit_move_insn (target, new_target);
18138               return true;
18139             }
18140
18141           /* Otherwise convert the intermediate result to V4SFmode and
18142              use the SSE1 shuffle instructions.  */
18143           if (mode != V4SFmode)
18144             {
18145               tmp = gen_reg_rtx (V4SFmode);
18146               emit_move_insn (tmp, gen_lowpart (V4SFmode, new_target));
18147             }
18148           else
18149             tmp = new_target;
18150
18151           emit_insn (gen_sse_shufps_1 (tmp, tmp, tmp,
18152                                        GEN_INT (1),
18153                                        GEN_INT (one_var == 1 ? 0 : 1),
18154                                        GEN_INT (one_var == 2 ? 0+4 : 1+4),
18155                                        GEN_INT (one_var == 3 ? 0+4 : 1+4)));
18156
18157           if (mode != V4SFmode)
18158             emit_move_insn (target, gen_lowpart (V4SImode, tmp));
18159           else if (tmp != target)
18160             emit_move_insn (target, tmp);
18161         }
18162       else if (target != new_target)
18163         emit_move_insn (target, new_target);
18164       return true;
18165
18166     case V8HImode:
18167     case V16QImode:
18168       vsimode = V4SImode;
18169       goto widen;
18170     case V4HImode:
18171     case V8QImode:
18172       if (!mmx_ok)
18173         return false;
18174       vsimode = V2SImode;
18175       goto widen;
18176     widen:
18177       if (one_var != 0)
18178         return false;
18179
18180       /* Zero extend the variable element to SImode and recurse.  */
18181       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
18182
18183       x = gen_reg_rtx (vsimode);
18184       if (!ix86_expand_vector_init_one_nonzero (mmx_ok, vsimode, x,
18185                                                 var, one_var))
18186         gcc_unreachable ();
18187
18188       emit_move_insn (target, gen_lowpart (mode, x));
18189       return true;
18190
18191     default:
18192       return false;
18193     }
18194 }
18195
18196 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
18197    consisting of the values in VALS.  It is known that all elements
18198    except ONE_VAR are constants.  Return true if successful.  */
18199
18200 static bool
18201 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
18202                                  rtx target, rtx vals, int one_var)
18203 {
18204   rtx var = XVECEXP (vals, 0, one_var);
18205   enum machine_mode wmode;
18206   rtx const_vec, x;
18207
18208   const_vec = copy_rtx (vals);
18209   XVECEXP (const_vec, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
18210   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (const_vec, 0));
18211
18212   switch (mode)
18213     {
18214     case V2DFmode:
18215     case V2DImode:
18216     case V2SFmode:
18217     case V2SImode:
18218       /* For the two element vectors, it's just as easy to use
18219          the general case.  */
18220       return false;
18221
18222     case V4SFmode:
18223     case V4SImode:
18224     case V8HImode:
18225     case V4HImode:
18226       break;
18227
18228     case V16QImode:
18229       wmode = V8HImode;
18230       goto widen;
18231     case V8QImode:
18232       wmode = V4HImode;
18233       goto widen;
18234     widen:
18235       /* There's no way to set one QImode entry easily.  Combine
18236          the variable value with its adjacent constant value, and
18237          promote to an HImode set.  */
18238       x = XVECEXP (vals, 0, one_var ^ 1);
18239       if (one_var & 1)
18240         {
18241           var = convert_modes (HImode, QImode, var, true);
18242           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
18243                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
18244           x = GEN_INT (INTVAL (x) & 0xff);
18245         }
18246       else
18247         {
18248           var = convert_modes (HImode, QImode, var, true);
18249           x = gen_int_mode (INTVAL (x) << 8, HImode);
18250         }
18251       if (x != const0_rtx)
18252         var = expand_simple_binop (HImode, IOR, var, x, var,
18253                                    1, OPTAB_LIB_WIDEN);
18254
18255       x = gen_reg_rtx (wmode);
18256       emit_move_insn (x, gen_lowpart (wmode, const_vec));
18257       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
18258
18259       emit_move_insn (target, gen_lowpart (mode, x));
18260       return true;
18261
18262     default:
18263       return false;
18264     }
18265
18266   emit_move_insn (target, const_vec);
18267   ix86_expand_vector_set (mmx_ok, target, var, one_var);
18268   return true;
18269 }
18270
18271 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
18272    all values variable, and none identical.  */
18273
18274 static void
18275 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
18276                                  rtx target, rtx vals)
18277 {
18278   enum machine_mode half_mode = GET_MODE_INNER (mode);
18279   rtx op0 = NULL, op1 = NULL;
18280   bool use_vec_concat = false;
18281
18282   switch (mode)
18283     {
18284     case V2SFmode:
18285     case V2SImode:
18286       if (!mmx_ok && !TARGET_SSE)
18287         break;
18288       /* FALLTHRU */
18289
18290     case V2DFmode:
18291     case V2DImode:
18292       /* For the two element vectors, we always implement VEC_CONCAT.  */
18293       op0 = XVECEXP (vals, 0, 0);
18294       op1 = XVECEXP (vals, 0, 1);
18295       use_vec_concat = true;
18296       break;
18297
18298     case V4SFmode:
18299       half_mode = V2SFmode;
18300       goto half;
18301     case V4SImode:
18302       half_mode = V2SImode;
18303       goto half;
18304     half:
18305       {
18306         rtvec v;
18307
18308         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
18309            Recurse to load the two halves.  */
18310
18311         op0 = gen_reg_rtx (half_mode);
18312         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
18313         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
18314
18315         op1 = gen_reg_rtx (half_mode);
18316         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
18317         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
18318
18319         use_vec_concat = true;
18320       }
18321       break;
18322
18323     case V8HImode:
18324     case V16QImode:
18325     case V4HImode:
18326     case V8QImode:
18327       break;
18328
18329     default:
18330       gcc_unreachable ();
18331     }
18332
18333   if (use_vec_concat)
18334     {
18335       if (!register_operand (op0, half_mode))
18336         op0 = force_reg (half_mode, op0);
18337       if (!register_operand (op1, half_mode))
18338         op1 = force_reg (half_mode, op1);
18339
18340       emit_insn (gen_rtx_SET (VOIDmode, target,
18341                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
18342     }
18343   else
18344     {
18345       int i, j, n_elts, n_words, n_elt_per_word;
18346       enum machine_mode inner_mode;
18347       rtx words[4], shift;
18348
18349       inner_mode = GET_MODE_INNER (mode);
18350       n_elts = GET_MODE_NUNITS (mode);
18351       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
18352       n_elt_per_word = n_elts / n_words;
18353       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
18354
18355       for (i = 0; i < n_words; ++i)
18356         {
18357           rtx word = NULL_RTX;
18358
18359           for (j = 0; j < n_elt_per_word; ++j)
18360             {
18361               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
18362               elt = convert_modes (word_mode, inner_mode, elt, true);
18363
18364               if (j == 0)
18365                 word = elt;
18366               else
18367                 {
18368                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
18369                                               word, 1, OPTAB_LIB_WIDEN);
18370                   word = expand_simple_binop (word_mode, IOR, word, elt,
18371                                               word, 1, OPTAB_LIB_WIDEN);
18372                 }
18373             }
18374
18375           words[i] = word;
18376         }
18377
18378       if (n_words == 1)
18379         emit_move_insn (target, gen_lowpart (mode, words[0]));
18380       else if (n_words == 2)
18381         {
18382           rtx tmp = gen_reg_rtx (mode);
18383           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
18384           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
18385           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
18386           emit_move_insn (target, tmp);
18387         }
18388       else if (n_words == 4)
18389         {
18390           rtx tmp = gen_reg_rtx (V4SImode);
18391           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
18392           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
18393           emit_move_insn (target, gen_lowpart (mode, tmp));
18394         }
18395       else
18396         gcc_unreachable ();
18397     }
18398 }
18399
18400 /* Initialize vector TARGET via VALS.  Suppress the use of MMX
18401    instructions unless MMX_OK is true.  */
18402
18403 void
18404 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
18405 {
18406   enum machine_mode mode = GET_MODE (target);
18407   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18408   int n_elts = GET_MODE_NUNITS (mode);
18409   int n_var = 0, one_var = -1;
18410   bool all_same = true, all_const_zero = true;
18411   int i;
18412   rtx x;
18413
18414   for (i = 0; i < n_elts; ++i)
18415     {
18416       x = XVECEXP (vals, 0, i);
18417       if (!CONSTANT_P (x))
18418         n_var++, one_var = i;
18419       else if (x != CONST0_RTX (inner_mode))
18420         all_const_zero = false;
18421       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
18422         all_same = false;
18423     }
18424
18425   /* Constants are best loaded from the constant pool.  */
18426   if (n_var == 0)
18427     {
18428       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
18429       return;
18430     }
18431
18432   /* If all values are identical, broadcast the value.  */
18433   if (all_same
18434       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
18435                                             XVECEXP (vals, 0, 0)))
18436     return;
18437
18438   /* Values where only one field is non-constant are best loaded from
18439      the pool and overwritten via move later.  */
18440   if (n_var == 1)
18441     {
18442       if (all_const_zero
18443           && ix86_expand_vector_init_one_nonzero (mmx_ok, mode, target,
18444                                                   XVECEXP (vals, 0, one_var),
18445                                                   one_var))
18446         return;
18447
18448       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
18449         return;
18450     }
18451
18452   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
18453 }
18454
18455 void
18456 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
18457 {
18458   enum machine_mode mode = GET_MODE (target);
18459   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18460   bool use_vec_merge = false;
18461   rtx tmp;
18462
18463   switch (mode)
18464     {
18465     case V2SFmode:
18466     case V2SImode:
18467       if (mmx_ok)
18468         {
18469           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
18470           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
18471           if (elt == 0)
18472             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
18473           else
18474             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
18475           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18476           return;
18477         }
18478       break;
18479
18480     case V2DFmode:
18481     case V2DImode:
18482       {
18483         rtx op0, op1;
18484
18485         /* For the two element vectors, we implement a VEC_CONCAT with
18486            the extraction of the other element.  */
18487
18488         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
18489         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
18490
18491         if (elt == 0)
18492           op0 = val, op1 = tmp;
18493         else
18494           op0 = tmp, op1 = val;
18495
18496         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
18497         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18498       }
18499       return;
18500
18501     case V4SFmode:
18502       switch (elt)
18503         {
18504         case 0:
18505           use_vec_merge = true;
18506           break;
18507
18508         case 1:
18509           /* tmp = target = A B C D */
18510           tmp = copy_to_reg (target);
18511           /* target = A A B B */
18512           emit_insn (gen_sse_unpcklps (target, target, target));
18513           /* target = X A B B */
18514           ix86_expand_vector_set (false, target, val, 0);
18515           /* target = A X C D  */
18516           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18517                                        GEN_INT (1), GEN_INT (0),
18518                                        GEN_INT (2+4), GEN_INT (3+4)));
18519           return;
18520
18521         case 2:
18522           /* tmp = target = A B C D */
18523           tmp = copy_to_reg (target);
18524           /* tmp = X B C D */
18525           ix86_expand_vector_set (false, tmp, val, 0);
18526           /* target = A B X D */
18527           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18528                                        GEN_INT (0), GEN_INT (1),
18529                                        GEN_INT (0+4), GEN_INT (3+4)));
18530           return;
18531
18532         case 3:
18533           /* tmp = target = A B C D */
18534           tmp = copy_to_reg (target);
18535           /* tmp = X B C D */
18536           ix86_expand_vector_set (false, tmp, val, 0);
18537           /* target = A B X D */
18538           emit_insn (gen_sse_shufps_1 (target, target, tmp,
18539                                        GEN_INT (0), GEN_INT (1),
18540                                        GEN_INT (2+4), GEN_INT (0+4)));
18541           return;
18542
18543         default:
18544           gcc_unreachable ();
18545         }
18546       break;
18547
18548     case V4SImode:
18549       /* Element 0 handled by vec_merge below.  */
18550       if (elt == 0)
18551         {
18552           use_vec_merge = true;
18553           break;
18554         }
18555
18556       if (TARGET_SSE2)
18557         {
18558           /* With SSE2, use integer shuffles to swap element 0 and ELT,
18559              store into element 0, then shuffle them back.  */
18560
18561           rtx order[4];
18562
18563           order[0] = GEN_INT (elt);
18564           order[1] = const1_rtx;
18565           order[2] = const2_rtx;
18566           order[3] = GEN_INT (3);
18567           order[elt] = const0_rtx;
18568
18569           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18570                                         order[1], order[2], order[3]));
18571
18572           ix86_expand_vector_set (false, target, val, 0);
18573
18574           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
18575                                         order[1], order[2], order[3]));
18576         }
18577       else
18578         {
18579           /* For SSE1, we have to reuse the V4SF code.  */
18580           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
18581                                   gen_lowpart (SFmode, val), elt);
18582         }
18583       return;
18584
18585     case V8HImode:
18586       use_vec_merge = TARGET_SSE2;
18587       break;
18588     case V4HImode:
18589       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18590       break;
18591
18592     case V16QImode:
18593     case V8QImode:
18594     default:
18595       break;
18596     }
18597
18598   if (use_vec_merge)
18599     {
18600       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
18601       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
18602       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18603     }
18604   else
18605     {
18606       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18607
18608       emit_move_insn (mem, target);
18609
18610       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18611       emit_move_insn (tmp, val);
18612
18613       emit_move_insn (target, mem);
18614     }
18615 }
18616
18617 void
18618 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
18619 {
18620   enum machine_mode mode = GET_MODE (vec);
18621   enum machine_mode inner_mode = GET_MODE_INNER (mode);
18622   bool use_vec_extr = false;
18623   rtx tmp;
18624
18625   switch (mode)
18626     {
18627     case V2SImode:
18628     case V2SFmode:
18629       if (!mmx_ok)
18630         break;
18631       /* FALLTHRU */
18632
18633     case V2DFmode:
18634     case V2DImode:
18635       use_vec_extr = true;
18636       break;
18637
18638     case V4SFmode:
18639       switch (elt)
18640         {
18641         case 0:
18642           tmp = vec;
18643           break;
18644
18645         case 1:
18646         case 3:
18647           tmp = gen_reg_rtx (mode);
18648           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
18649                                        GEN_INT (elt), GEN_INT (elt),
18650                                        GEN_INT (elt+4), GEN_INT (elt+4)));
18651           break;
18652
18653         case 2:
18654           tmp = gen_reg_rtx (mode);
18655           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
18656           break;
18657
18658         default:
18659           gcc_unreachable ();
18660         }
18661       vec = tmp;
18662       use_vec_extr = true;
18663       elt = 0;
18664       break;
18665
18666     case V4SImode:
18667       if (TARGET_SSE2)
18668         {
18669           switch (elt)
18670             {
18671             case 0:
18672               tmp = vec;
18673               break;
18674
18675             case 1:
18676             case 3:
18677               tmp = gen_reg_rtx (mode);
18678               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
18679                                             GEN_INT (elt), GEN_INT (elt),
18680                                             GEN_INT (elt), GEN_INT (elt)));
18681               break;
18682
18683             case 2:
18684               tmp = gen_reg_rtx (mode);
18685               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
18686               break;
18687
18688             default:
18689               gcc_unreachable ();
18690             }
18691           vec = tmp;
18692           use_vec_extr = true;
18693           elt = 0;
18694         }
18695       else
18696         {
18697           /* For SSE1, we have to reuse the V4SF code.  */
18698           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
18699                                       gen_lowpart (V4SFmode, vec), elt);
18700           return;
18701         }
18702       break;
18703
18704     case V8HImode:
18705       use_vec_extr = TARGET_SSE2;
18706       break;
18707     case V4HImode:
18708       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
18709       break;
18710
18711     case V16QImode:
18712     case V8QImode:
18713       /* ??? Could extract the appropriate HImode element and shift.  */
18714     default:
18715       break;
18716     }
18717
18718   if (use_vec_extr)
18719     {
18720       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
18721       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
18722
18723       /* Let the rtl optimizers know about the zero extension performed.  */
18724       if (inner_mode == HImode)
18725         {
18726           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
18727           target = gen_lowpart (SImode, target);
18728         }
18729
18730       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
18731     }
18732   else
18733     {
18734       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
18735
18736       emit_move_insn (mem, vec);
18737
18738       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
18739       emit_move_insn (target, tmp);
18740     }
18741 }
18742
18743 /* Expand a vector reduction on V4SFmode for SSE1.  FN is the binary
18744    pattern to reduce; DEST is the destination; IN is the input vector.  */
18745
18746 void
18747 ix86_expand_reduc_v4sf (rtx (*fn) (rtx, rtx, rtx), rtx dest, rtx in)
18748 {
18749   rtx tmp1, tmp2, tmp3;
18750
18751   tmp1 = gen_reg_rtx (V4SFmode);
18752   tmp2 = gen_reg_rtx (V4SFmode);
18753   tmp3 = gen_reg_rtx (V4SFmode);
18754
18755   emit_insn (gen_sse_movhlps (tmp1, in, in));
18756   emit_insn (fn (tmp2, tmp1, in));
18757
18758   emit_insn (gen_sse_shufps_1 (tmp3, tmp2, tmp2,
18759                                GEN_INT (1), GEN_INT (1),
18760                                GEN_INT (1+4), GEN_INT (1+4)));
18761   emit_insn (fn (dest, tmp2, tmp3));
18762 }
18763 \f
18764 /* Target hook for scalar_mode_supported_p.  */
18765 static bool
18766 ix86_scalar_mode_supported_p (enum machine_mode mode)
18767 {
18768   if (DECIMAL_FLOAT_MODE_P (mode))
18769     return true;
18770   else
18771     return default_scalar_mode_supported_p (mode);
18772 }
18773
18774 /* Implements target hook vector_mode_supported_p.  */
18775 static bool
18776 ix86_vector_mode_supported_p (enum machine_mode mode)
18777 {
18778   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
18779     return true;
18780   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
18781     return true;
18782   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
18783     return true;
18784   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
18785     return true;
18786   return false;
18787 }
18788
18789 /* Worker function for TARGET_MD_ASM_CLOBBERS.
18790
18791    We do this in the new i386 backend to maintain source compatibility
18792    with the old cc0-based compiler.  */
18793
18794 static tree
18795 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
18796                       tree inputs ATTRIBUTE_UNUSED,
18797                       tree clobbers)
18798 {
18799   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
18800                         clobbers);
18801   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
18802                         clobbers);
18803   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
18804                         clobbers);
18805   return clobbers;
18806 }
18807
18808 /* Return true if this goes in small data/bss.  */
18809
18810 static bool
18811 ix86_in_large_data_p (tree exp)
18812 {
18813   if (ix86_cmodel != CM_MEDIUM && ix86_cmodel != CM_MEDIUM_PIC)
18814     return false;
18815
18816   /* Functions are never large data.  */
18817   if (TREE_CODE (exp) == FUNCTION_DECL)
18818     return false;
18819
18820   if (TREE_CODE (exp) == VAR_DECL && DECL_SECTION_NAME (exp))
18821     {
18822       const char *section = TREE_STRING_POINTER (DECL_SECTION_NAME (exp));
18823       if (strcmp (section, ".ldata") == 0
18824           || strcmp (section, ".lbss") == 0)
18825         return true;
18826       return false;
18827     }
18828   else
18829     {
18830       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
18831
18832       /* If this is an incomplete type with size 0, then we can't put it
18833          in data because it might be too big when completed.  */
18834       if (!size || size > ix86_section_threshold)
18835         return true;
18836     }
18837
18838   return false;
18839 }
18840 static void
18841 ix86_encode_section_info (tree decl, rtx rtl, int first)
18842 {
18843   default_encode_section_info (decl, rtl, first);
18844
18845   if (TREE_CODE (decl) == VAR_DECL
18846       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
18847       && ix86_in_large_data_p (decl))
18848     SYMBOL_REF_FLAGS (XEXP (rtl, 0)) |= SYMBOL_FLAG_FAR_ADDR;
18849 }
18850
18851 /* Worker function for REVERSE_CONDITION.  */
18852
18853 enum rtx_code
18854 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
18855 {
18856   return (mode != CCFPmode && mode != CCFPUmode
18857           ? reverse_condition (code)
18858           : reverse_condition_maybe_unordered (code));
18859 }
18860
18861 /* Output code to perform an x87 FP register move, from OPERANDS[1]
18862    to OPERANDS[0].  */
18863
18864 const char *
18865 output_387_reg_move (rtx insn, rtx *operands)
18866 {
18867   if (REG_P (operands[1])
18868       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
18869     {
18870       if (REGNO (operands[0]) == FIRST_STACK_REG)
18871         return output_387_ffreep (operands, 0);
18872       return "fstp\t%y0";
18873     }
18874   if (STACK_TOP_P (operands[0]))
18875     return "fld%z1\t%y1";
18876   return "fst\t%y0";
18877 }
18878
18879 /* Output code to perform a conditional jump to LABEL, if C2 flag in
18880    FP status register is set.  */
18881
18882 void
18883 ix86_emit_fp_unordered_jump (rtx label)
18884 {
18885   rtx reg = gen_reg_rtx (HImode);
18886   rtx temp;
18887
18888   emit_insn (gen_x86_fnstsw_1 (reg));
18889
18890   if (TARGET_USE_SAHF)
18891     {
18892       emit_insn (gen_x86_sahf_1 (reg));
18893
18894       temp = gen_rtx_REG (CCmode, FLAGS_REG);
18895       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
18896     }
18897   else
18898     {
18899       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
18900
18901       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
18902       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
18903     }
18904
18905   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
18906                               gen_rtx_LABEL_REF (VOIDmode, label),
18907                               pc_rtx);
18908   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
18909   emit_jump_insn (temp);
18910 }
18911
18912 /* Output code to perform a log1p XFmode calculation.  */
18913
18914 void ix86_emit_i387_log1p (rtx op0, rtx op1)
18915 {
18916   rtx label1 = gen_label_rtx ();
18917   rtx label2 = gen_label_rtx ();
18918
18919   rtx tmp = gen_reg_rtx (XFmode);
18920   rtx tmp2 = gen_reg_rtx (XFmode);
18921
18922   emit_insn (gen_absxf2 (tmp, op1));
18923   emit_insn (gen_cmpxf (tmp,
18924     CONST_DOUBLE_FROM_REAL_VALUE (
18925        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
18926        XFmode)));
18927   emit_jump_insn (gen_bge (label1));
18928
18929   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18930   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
18931   emit_jump (label2);
18932
18933   emit_label (label1);
18934   emit_move_insn (tmp, CONST1_RTX (XFmode));
18935   emit_insn (gen_addxf3 (tmp, op1, tmp));
18936   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
18937   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
18938
18939   emit_label (label2);
18940 }
18941
18942 /* Solaris implementation of TARGET_ASM_NAMED_SECTION.  */
18943
18944 static void
18945 i386_solaris_elf_named_section (const char *name, unsigned int flags,
18946                                 tree decl)
18947 {
18948   /* With Binutils 2.15, the "@unwind" marker must be specified on
18949      every occurrence of the ".eh_frame" section, not just the first
18950      one.  */
18951   if (TARGET_64BIT
18952       && strcmp (name, ".eh_frame") == 0)
18953     {
18954       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
18955                flags & SECTION_WRITE ? "aw" : "a");
18956       return;
18957     }
18958   default_elf_asm_named_section (name, flags, decl);
18959 }
18960
18961 /* Return the mangling of TYPE if it is an extended fundamental type.  */
18962
18963 static const char *
18964 ix86_mangle_fundamental_type (tree type)
18965 {
18966   switch (TYPE_MODE (type))
18967     {
18968     case TFmode:
18969       /* __float128 is "g".  */
18970       return "g";
18971     case XFmode:
18972       /* "long double" or __float80 is "e".  */
18973       return "e";
18974     default:
18975       return NULL;
18976     }
18977 }
18978
18979 /* For 32-bit code we can save PIC register setup by using
18980    __stack_chk_fail_local hidden function instead of calling
18981    __stack_chk_fail directly.  64-bit code doesn't need to setup any PIC
18982    register, so it is better to call __stack_chk_fail directly.  */
18983
18984 static tree
18985 ix86_stack_protect_fail (void)
18986 {
18987   return TARGET_64BIT
18988          ? default_external_stack_protect_fail ()
18989          : default_hidden_stack_protect_fail ();
18990 }
18991
18992 /* Select a format to encode pointers in exception handling data.  CODE
18993    is 0 for data, 1 for code labels, 2 for function pointers.  GLOBAL is
18994    true if the symbol may be affected by dynamic relocations.
18995
18996    ??? All x86 object file formats are capable of representing this.
18997    After all, the relocation needed is the same as for the call insn.
18998    Whether or not a particular assembler allows us to enter such, I
18999    guess we'll have to see.  */
19000 int
19001 asm_preferred_eh_data_format (int code, int global)
19002 {
19003   if (flag_pic)
19004     {
19005       int type = DW_EH_PE_sdata8;
19006       if (!TARGET_64BIT
19007           || ix86_cmodel == CM_SMALL_PIC
19008           || (ix86_cmodel == CM_MEDIUM_PIC && (global || code)))
19009         type = DW_EH_PE_sdata4;
19010       return (global ? DW_EH_PE_indirect : 0) | DW_EH_PE_pcrel | type;
19011     }
19012   if (ix86_cmodel == CM_SMALL
19013       || (ix86_cmodel == CM_MEDIUM && code))
19014     return DW_EH_PE_udata4;
19015   return DW_EH_PE_absptr;
19016 }
19017
19018 #include "gt-i386.h"