OSDN Git Service

* config/i386/i386.c (type_natural_mode): Use gcc_unreachable and
[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 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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
52 #ifndef CHECK_STACK_LIMIT
53 #define CHECK_STACK_LIMIT (-1)
54 #endif
55
56 /* Return index of given mode in mult and division cost tables.  */
57 #define MODE_INDEX(mode)                                        \
58   ((mode) == QImode ? 0                                         \
59    : (mode) == HImode ? 1                                       \
60    : (mode) == SImode ? 2                                       \
61    : (mode) == DImode ? 3                                       \
62    : 4)
63
64 /* Processor costs (relative to an add) */
65 static const
66 struct processor_costs size_cost = {    /* costs for tunning for size */
67   2,                                    /* cost of an add instruction */
68   3,                                    /* cost of a lea instruction */
69   2,                                    /* variable shift costs */
70   3,                                    /* constant shift costs */
71   {3, 3, 3, 3, 5},                      /* cost of starting a multiply */
72   0,                                    /* cost of multiply per each bit set */
73   {3, 3, 3, 3, 5},                      /* cost of a divide/mod */
74   3,                                    /* cost of movsx */
75   3,                                    /* cost of movzx */
76   0,                                    /* "large" insn */
77   2,                                    /* MOVE_RATIO */
78   2,                                    /* cost for loading QImode using movzbl */
79   {2, 2, 2},                            /* cost of loading integer registers
80                                            in QImode, HImode and SImode.
81                                            Relative to reg-reg move (2).  */
82   {2, 2, 2},                            /* cost of storing integer registers */
83   2,                                    /* cost of reg,reg fld/fst */
84   {2, 2, 2},                            /* cost of loading fp registers
85                                            in SFmode, DFmode and XFmode */
86   {2, 2, 2},                            /* cost of loading integer registers */
87   3,                                    /* cost of moving MMX register */
88   {3, 3},                               /* cost of loading MMX registers
89                                            in SImode and DImode */
90   {3, 3},                               /* cost of storing MMX registers
91                                            in SImode and DImode */
92   3,                                    /* cost of moving SSE register */
93   {3, 3, 3},                            /* cost of loading SSE registers
94                                            in SImode, DImode and TImode */
95   {3, 3, 3},                            /* cost of storing SSE registers
96                                            in SImode, DImode and TImode */
97   3,                                    /* MMX or SSE register to integer */
98   0,                                    /* size of prefetch block */
99   0,                                    /* number of parallel prefetches */
100   1,                                    /* Branch cost */
101   2,                                    /* cost of FADD and FSUB insns.  */
102   2,                                    /* cost of FMUL instruction.  */
103   2,                                    /* cost of FDIV instruction.  */
104   2,                                    /* cost of FABS instruction.  */
105   2,                                    /* cost of FCHS instruction.  */
106   2,                                    /* cost of FSQRT instruction.  */
107 };
108
109 /* Processor costs (relative to an add) */
110 static const
111 struct processor_costs i386_cost = {    /* 386 specific costs */
112   1,                                    /* cost of an add instruction */
113   1,                                    /* cost of a lea instruction */
114   3,                                    /* variable shift costs */
115   2,                                    /* constant shift costs */
116   {6, 6, 6, 6, 6},                      /* cost of starting a multiply */
117   1,                                    /* cost of multiply per each bit set */
118   {23, 23, 23, 23, 23},                 /* cost of a divide/mod */
119   3,                                    /* cost of movsx */
120   2,                                    /* cost of movzx */
121   15,                                   /* "large" insn */
122   3,                                    /* MOVE_RATIO */
123   4,                                    /* cost for loading QImode using movzbl */
124   {2, 4, 2},                            /* cost of loading integer registers
125                                            in QImode, HImode and SImode.
126                                            Relative to reg-reg move (2).  */
127   {2, 4, 2},                            /* cost of storing integer registers */
128   2,                                    /* cost of reg,reg fld/fst */
129   {8, 8, 8},                            /* cost of loading fp registers
130                                            in SFmode, DFmode and XFmode */
131   {8, 8, 8},                            /* cost of loading integer registers */
132   2,                                    /* cost of moving MMX register */
133   {4, 8},                               /* cost of loading MMX registers
134                                            in SImode and DImode */
135   {4, 8},                               /* cost of storing MMX registers
136                                            in SImode and DImode */
137   2,                                    /* cost of moving SSE register */
138   {4, 8, 16},                           /* cost of loading SSE registers
139                                            in SImode, DImode and TImode */
140   {4, 8, 16},                           /* cost of storing SSE registers
141                                            in SImode, DImode and TImode */
142   3,                                    /* MMX or SSE register to integer */
143   0,                                    /* size of prefetch block */
144   0,                                    /* number of parallel prefetches */
145   1,                                    /* Branch cost */
146   23,                                   /* cost of FADD and FSUB insns.  */
147   27,                                   /* cost of FMUL instruction.  */
148   88,                                   /* cost of FDIV instruction.  */
149   22,                                   /* cost of FABS instruction.  */
150   24,                                   /* cost of FCHS instruction.  */
151   122,                                  /* cost of FSQRT instruction.  */
152 };
153
154 static const
155 struct processor_costs i486_cost = {    /* 486 specific costs */
156   1,                                    /* cost of an add instruction */
157   1,                                    /* cost of a lea instruction */
158   3,                                    /* variable shift costs */
159   2,                                    /* constant shift costs */
160   {12, 12, 12, 12, 12},                 /* cost of starting a multiply */
161   1,                                    /* cost of multiply per each bit set */
162   {40, 40, 40, 40, 40},                 /* cost of a divide/mod */
163   3,                                    /* cost of movsx */
164   2,                                    /* cost of movzx */
165   15,                                   /* "large" insn */
166   3,                                    /* MOVE_RATIO */
167   4,                                    /* cost for loading QImode using movzbl */
168   {2, 4, 2},                            /* cost of loading integer registers
169                                            in QImode, HImode and SImode.
170                                            Relative to reg-reg move (2).  */
171   {2, 4, 2},                            /* cost of storing integer registers */
172   2,                                    /* cost of reg,reg fld/fst */
173   {8, 8, 8},                            /* cost of loading fp registers
174                                            in SFmode, DFmode and XFmode */
175   {8, 8, 8},                            /* cost of loading integer registers */
176   2,                                    /* cost of moving MMX register */
177   {4, 8},                               /* cost of loading MMX registers
178                                            in SImode and DImode */
179   {4, 8},                               /* cost of storing MMX registers
180                                            in SImode and DImode */
181   2,                                    /* cost of moving SSE register */
182   {4, 8, 16},                           /* cost of loading SSE registers
183                                            in SImode, DImode and TImode */
184   {4, 8, 16},                           /* cost of storing SSE registers
185                                            in SImode, DImode and TImode */
186   3,                                    /* MMX or SSE register to integer */
187   0,                                    /* size of prefetch block */
188   0,                                    /* number of parallel prefetches */
189   1,                                    /* Branch cost */
190   8,                                    /* cost of FADD and FSUB insns.  */
191   16,                                   /* cost of FMUL instruction.  */
192   73,                                   /* cost of FDIV instruction.  */
193   3,                                    /* cost of FABS instruction.  */
194   3,                                    /* cost of FCHS instruction.  */
195   83,                                   /* cost of FSQRT instruction.  */
196 };
197
198 static const
199 struct processor_costs pentium_cost = {
200   1,                                    /* cost of an add instruction */
201   1,                                    /* cost of a lea instruction */
202   4,                                    /* variable shift costs */
203   1,                                    /* constant shift costs */
204   {11, 11, 11, 11, 11},                 /* cost of starting a multiply */
205   0,                                    /* cost of multiply per each bit set */
206   {25, 25, 25, 25, 25},                 /* cost of a divide/mod */
207   3,                                    /* cost of movsx */
208   2,                                    /* cost of movzx */
209   8,                                    /* "large" insn */
210   6,                                    /* MOVE_RATIO */
211   6,                                    /* cost for loading QImode using movzbl */
212   {2, 4, 2},                            /* cost of loading integer registers
213                                            in QImode, HImode and SImode.
214                                            Relative to reg-reg move (2).  */
215   {2, 4, 2},                            /* cost of storing integer registers */
216   2,                                    /* cost of reg,reg fld/fst */
217   {2, 2, 6},                            /* cost of loading fp registers
218                                            in SFmode, DFmode and XFmode */
219   {4, 4, 6},                            /* cost of loading integer registers */
220   8,                                    /* cost of moving MMX register */
221   {8, 8},                               /* cost of loading MMX registers
222                                            in SImode and DImode */
223   {8, 8},                               /* cost of storing MMX registers
224                                            in SImode and DImode */
225   2,                                    /* cost of moving SSE register */
226   {4, 8, 16},                           /* cost of loading SSE registers
227                                            in SImode, DImode and TImode */
228   {4, 8, 16},                           /* cost of storing SSE registers
229                                            in SImode, DImode and TImode */
230   3,                                    /* MMX or SSE register to integer */
231   0,                                    /* size of prefetch block */
232   0,                                    /* number of parallel prefetches */
233   2,                                    /* Branch cost */
234   3,                                    /* cost of FADD and FSUB insns.  */
235   3,                                    /* cost of FMUL instruction.  */
236   39,                                   /* cost of FDIV instruction.  */
237   1,                                    /* cost of FABS instruction.  */
238   1,                                    /* cost of FCHS instruction.  */
239   70,                                   /* cost of FSQRT instruction.  */
240 };
241
242 static const
243 struct processor_costs pentiumpro_cost = {
244   1,                                    /* cost of an add instruction */
245   1,                                    /* cost of a lea instruction */
246   1,                                    /* variable shift costs */
247   1,                                    /* constant shift costs */
248   {4, 4, 4, 4, 4},                      /* cost of starting a multiply */
249   0,                                    /* cost of multiply per each bit set */
250   {17, 17, 17, 17, 17},                 /* cost of a divide/mod */
251   1,                                    /* cost of movsx */
252   1,                                    /* cost of movzx */
253   8,                                    /* "large" insn */
254   6,                                    /* MOVE_RATIO */
255   2,                                    /* cost for loading QImode using movzbl */
256   {4, 4, 4},                            /* cost of loading integer registers
257                                            in QImode, HImode and SImode.
258                                            Relative to reg-reg move (2).  */
259   {2, 2, 2},                            /* cost of storing integer registers */
260   2,                                    /* cost of reg,reg fld/fst */
261   {2, 2, 6},                            /* cost of loading fp registers
262                                            in SFmode, DFmode and XFmode */
263   {4, 4, 6},                            /* cost of loading integer registers */
264   2,                                    /* cost of moving MMX register */
265   {2, 2},                               /* cost of loading MMX registers
266                                            in SImode and DImode */
267   {2, 2},                               /* cost of storing MMX registers
268                                            in SImode and DImode */
269   2,                                    /* cost of moving SSE register */
270   {2, 2, 8},                            /* cost of loading SSE registers
271                                            in SImode, DImode and TImode */
272   {2, 2, 8},                            /* cost of storing SSE registers
273                                            in SImode, DImode and TImode */
274   3,                                    /* MMX or SSE register to integer */
275   32,                                   /* size of prefetch block */
276   6,                                    /* number of parallel prefetches */
277   2,                                    /* Branch cost */
278   3,                                    /* cost of FADD and FSUB insns.  */
279   5,                                    /* cost of FMUL instruction.  */
280   56,                                   /* cost of FDIV instruction.  */
281   2,                                    /* cost of FABS instruction.  */
282   2,                                    /* cost of FCHS instruction.  */
283   56,                                   /* cost of FSQRT instruction.  */
284 };
285
286 static const
287 struct processor_costs k6_cost = {
288   1,                                    /* cost of an add instruction */
289   2,                                    /* cost of a lea instruction */
290   1,                                    /* variable shift costs */
291   1,                                    /* constant shift costs */
292   {3, 3, 3, 3, 3},                      /* cost of starting a multiply */
293   0,                                    /* cost of multiply per each bit set */
294   {18, 18, 18, 18, 18},                 /* cost of a divide/mod */
295   2,                                    /* cost of movsx */
296   2,                                    /* cost of movzx */
297   8,                                    /* "large" insn */
298   4,                                    /* MOVE_RATIO */
299   3,                                    /* cost for loading QImode using movzbl */
300   {4, 5, 4},                            /* cost of loading integer registers
301                                            in QImode, HImode and SImode.
302                                            Relative to reg-reg move (2).  */
303   {2, 3, 2},                            /* cost of storing integer registers */
304   4,                                    /* cost of reg,reg fld/fst */
305   {6, 6, 6},                            /* cost of loading fp registers
306                                            in SFmode, DFmode and XFmode */
307   {4, 4, 4},                            /* cost of loading integer registers */
308   2,                                    /* cost of moving MMX register */
309   {2, 2},                               /* cost of loading MMX registers
310                                            in SImode and DImode */
311   {2, 2},                               /* cost of storing MMX registers
312                                            in SImode and DImode */
313   2,                                    /* cost of moving SSE register */
314   {2, 2, 8},                            /* cost of loading SSE registers
315                                            in SImode, DImode and TImode */
316   {2, 2, 8},                            /* cost of storing SSE registers
317                                            in SImode, DImode and TImode */
318   6,                                    /* MMX or SSE register to integer */
319   32,                                   /* size of prefetch block */
320   1,                                    /* number of parallel prefetches */
321   1,                                    /* Branch cost */
322   2,                                    /* cost of FADD and FSUB insns.  */
323   2,                                    /* cost of FMUL instruction.  */
324   56,                                   /* cost of FDIV instruction.  */
325   2,                                    /* cost of FABS instruction.  */
326   2,                                    /* cost of FCHS instruction.  */
327   56,                                   /* cost of FSQRT instruction.  */
328 };
329
330 static const
331 struct processor_costs athlon_cost = {
332   1,                                    /* cost of an add instruction */
333   2,                                    /* cost of a lea instruction */
334   1,                                    /* variable shift costs */
335   1,                                    /* constant shift costs */
336   {5, 5, 5, 5, 5},                      /* cost of starting a multiply */
337   0,                                    /* cost of multiply per each bit set */
338   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
339   1,                                    /* cost of movsx */
340   1,                                    /* cost of movzx */
341   8,                                    /* "large" insn */
342   9,                                    /* MOVE_RATIO */
343   4,                                    /* cost for loading QImode using movzbl */
344   {3, 4, 3},                            /* cost of loading integer registers
345                                            in QImode, HImode and SImode.
346                                            Relative to reg-reg move (2).  */
347   {3, 4, 3},                            /* cost of storing integer registers */
348   4,                                    /* cost of reg,reg fld/fst */
349   {4, 4, 12},                           /* cost of loading fp registers
350                                            in SFmode, DFmode and XFmode */
351   {6, 6, 8},                            /* cost of loading integer registers */
352   2,                                    /* cost of moving MMX register */
353   {4, 4},                               /* cost of loading MMX registers
354                                            in SImode and DImode */
355   {4, 4},                               /* cost of storing MMX registers
356                                            in SImode and DImode */
357   2,                                    /* cost of moving SSE register */
358   {4, 4, 6},                            /* cost of loading SSE registers
359                                            in SImode, DImode and TImode */
360   {4, 4, 5},                            /* cost of storing SSE registers
361                                            in SImode, DImode and TImode */
362   5,                                    /* MMX or SSE register to integer */
363   64,                                   /* size of prefetch block */
364   6,                                    /* number of parallel prefetches */
365   5,                                    /* Branch cost */
366   4,                                    /* cost of FADD and FSUB insns.  */
367   4,                                    /* cost of FMUL instruction.  */
368   24,                                   /* cost of FDIV instruction.  */
369   2,                                    /* cost of FABS instruction.  */
370   2,                                    /* cost of FCHS instruction.  */
371   35,                                   /* cost of FSQRT instruction.  */
372 };
373
374 static const
375 struct processor_costs k8_cost = {
376   1,                                    /* cost of an add instruction */
377   2,                                    /* cost of a lea instruction */
378   1,                                    /* variable shift costs */
379   1,                                    /* constant shift costs */
380   {3, 4, 3, 4, 5},                      /* cost of starting a multiply */
381   0,                                    /* cost of multiply per each bit set */
382   {18, 26, 42, 74, 74},                 /* cost of a divide/mod */
383   1,                                    /* cost of movsx */
384   1,                                    /* cost of movzx */
385   8,                                    /* "large" insn */
386   9,                                    /* MOVE_RATIO */
387   4,                                    /* cost for loading QImode using movzbl */
388   {3, 4, 3},                            /* cost of loading integer registers
389                                            in QImode, HImode and SImode.
390                                            Relative to reg-reg move (2).  */
391   {3, 4, 3},                            /* cost of storing integer registers */
392   4,                                    /* cost of reg,reg fld/fst */
393   {4, 4, 12},                           /* cost of loading fp registers
394                                            in SFmode, DFmode and XFmode */
395   {6, 6, 8},                            /* cost of loading integer registers */
396   2,                                    /* cost of moving MMX register */
397   {3, 3},                               /* cost of loading MMX registers
398                                            in SImode and DImode */
399   {4, 4},                               /* cost of storing MMX registers
400                                            in SImode and DImode */
401   2,                                    /* cost of moving SSE register */
402   {4, 3, 6},                            /* cost of loading SSE registers
403                                            in SImode, DImode and TImode */
404   {4, 4, 5},                            /* cost of storing SSE registers
405                                            in SImode, DImode and TImode */
406   5,                                    /* MMX or SSE register to integer */
407   64,                                   /* size of prefetch block */
408   6,                                    /* number of parallel prefetches */
409   5,                                    /* Branch cost */
410   4,                                    /* cost of FADD and FSUB insns.  */
411   4,                                    /* cost of FMUL instruction.  */
412   19,                                   /* cost of FDIV instruction.  */
413   2,                                    /* cost of FABS instruction.  */
414   2,                                    /* cost of FCHS instruction.  */
415   35,                                   /* cost of FSQRT instruction.  */
416 };
417
418 static const
419 struct processor_costs pentium4_cost = {
420   1,                                    /* cost of an add instruction */
421   3,                                    /* cost of a lea instruction */
422   4,                                    /* variable shift costs */
423   4,                                    /* constant shift costs */
424   {15, 15, 15, 15, 15},                 /* cost of starting a multiply */
425   0,                                    /* cost of multiply per each bit set */
426   {56, 56, 56, 56, 56},                 /* cost of a divide/mod */
427   1,                                    /* cost of movsx */
428   1,                                    /* cost of movzx */
429   16,                                   /* "large" insn */
430   6,                                    /* MOVE_RATIO */
431   2,                                    /* cost for loading QImode using movzbl */
432   {4, 5, 4},                            /* cost of loading integer registers
433                                            in QImode, HImode and SImode.
434                                            Relative to reg-reg move (2).  */
435   {2, 3, 2},                            /* cost of storing integer registers */
436   2,                                    /* cost of reg,reg fld/fst */
437   {2, 2, 6},                            /* cost of loading fp registers
438                                            in SFmode, DFmode and XFmode */
439   {4, 4, 6},                            /* cost of loading integer registers */
440   2,                                    /* cost of moving MMX register */
441   {2, 2},                               /* cost of loading MMX registers
442                                            in SImode and DImode */
443   {2, 2},                               /* cost of storing MMX registers
444                                            in SImode and DImode */
445   12,                                   /* cost of moving SSE register */
446   {12, 12, 12},                         /* cost of loading SSE registers
447                                            in SImode, DImode and TImode */
448   {2, 2, 8},                            /* cost of storing SSE registers
449                                            in SImode, DImode and TImode */
450   10,                                   /* MMX or SSE register to integer */
451   64,                                   /* size of prefetch block */
452   6,                                    /* number of parallel prefetches */
453   2,                                    /* Branch cost */
454   5,                                    /* cost of FADD and FSUB insns.  */
455   7,                                    /* cost of FMUL instruction.  */
456   43,                                   /* cost of FDIV instruction.  */
457   2,                                    /* cost of FABS instruction.  */
458   2,                                    /* cost of FCHS instruction.  */
459   43,                                   /* cost of FSQRT instruction.  */
460 };
461
462 static const
463 struct processor_costs nocona_cost = {
464   1,                                    /* cost of an add instruction */
465   1,                                    /* cost of a lea instruction */
466   1,                                    /* variable shift costs */
467   1,                                    /* constant shift costs */
468   {10, 10, 10, 10, 10},                 /* cost of starting a multiply */
469   0,                                    /* cost of multiply per each bit set */
470   {66, 66, 66, 66, 66},                 /* cost of a divide/mod */
471   1,                                    /* cost of movsx */
472   1,                                    /* cost of movzx */
473   16,                                   /* "large" insn */
474   9,                                    /* MOVE_RATIO */
475   4,                                    /* cost for loading QImode using movzbl */
476   {4, 4, 4},                            /* cost of loading integer registers
477                                            in QImode, HImode and SImode.
478                                            Relative to reg-reg move (2).  */
479   {4, 4, 4},                            /* cost of storing integer registers */
480   3,                                    /* cost of reg,reg fld/fst */
481   {12, 12, 12},                         /* cost of loading fp registers
482                                            in SFmode, DFmode and XFmode */
483   {4, 4, 4},                            /* cost of loading integer registers */
484   6,                                    /* cost of moving MMX register */
485   {12, 12},                             /* cost of loading MMX registers
486                                            in SImode and DImode */
487   {12, 12},                             /* cost of storing MMX registers
488                                            in SImode and DImode */
489   6,                                    /* cost of moving SSE register */
490   {12, 12, 12},                         /* cost of loading SSE registers
491                                            in SImode, DImode and TImode */
492   {12, 12, 12},                         /* cost of storing SSE registers
493                                            in SImode, DImode and TImode */
494   8,                                    /* MMX or SSE register to integer */
495   128,                                  /* size of prefetch block */
496   8,                                    /* number of parallel prefetches */
497   1,                                    /* Branch cost */
498   6,                                    /* cost of FADD and FSUB insns.  */
499   8,                                    /* cost of FMUL instruction.  */
500   40,                                   /* cost of FDIV instruction.  */
501   3,                                    /* cost of FABS instruction.  */
502   3,                                    /* cost of FCHS instruction.  */
503   44,                                   /* cost of FSQRT instruction.  */
504 };
505
506 const struct processor_costs *ix86_cost = &pentium_cost;
507
508 /* Processor feature/optimization bitmasks.  */
509 #define m_386 (1<<PROCESSOR_I386)
510 #define m_486 (1<<PROCESSOR_I486)
511 #define m_PENT (1<<PROCESSOR_PENTIUM)
512 #define m_PPRO (1<<PROCESSOR_PENTIUMPRO)
513 #define m_K6  (1<<PROCESSOR_K6)
514 #define m_ATHLON  (1<<PROCESSOR_ATHLON)
515 #define m_PENT4  (1<<PROCESSOR_PENTIUM4)
516 #define m_K8  (1<<PROCESSOR_K8)
517 #define m_ATHLON_K8  (m_K8 | m_ATHLON)
518 #define m_NOCONA  (1<<PROCESSOR_NOCONA)
519
520 const int x86_use_leave = m_386 | m_K6 | m_ATHLON_K8;
521 const int x86_push_memory = m_386 | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
522 const int x86_zero_extend_with_and = m_486 | m_PENT;
523 const int x86_movx = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA /* m_386 | m_K6 */;
524 const int x86_double_with_add = ~m_386;
525 const int x86_use_bit_test = m_386;
526 const int x86_unroll_strlen = m_486 | m_PENT | m_PPRO | m_ATHLON_K8 | m_K6;
527 const int x86_cmove = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
528 const int x86_fisttp = m_NOCONA;
529 const int x86_3dnow_a = m_ATHLON_K8;
530 const int x86_deep_branch = m_PPRO | m_K6 | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
531 /* Branch hints were put in P4 based on simulation result. But
532    after P4 was made, no performance benefit was observed with
533    branch hints. It also increases the code size. As the result,
534    icc never generates branch hints.  */
535 const int x86_branch_hints = 0;
536 const int x86_use_sahf = m_PPRO | m_K6 | m_PENT4 | m_NOCONA;
537 const int x86_partial_reg_stall = m_PPRO;
538 const int x86_use_loop = m_K6;
539 const int x86_use_himode_fiop = m_386 | m_486 | m_K6;
540 const int x86_use_simode_fiop = ~(m_PPRO | m_ATHLON_K8 | m_PENT);
541 const int x86_use_mov0 = m_K6;
542 const int x86_use_cltd = ~(m_PENT | m_K6);
543 const int x86_read_modify_write = ~m_PENT;
544 const int x86_read_modify = ~(m_PENT | m_PPRO);
545 const int x86_split_long_moves = m_PPRO;
546 const int x86_promote_QImode = m_K6 | m_PENT | m_386 | m_486 | m_ATHLON_K8;
547 const int x86_fast_prefix = ~(m_PENT | m_486 | m_386);
548 const int x86_single_stringop = m_386 | m_PENT4 | m_NOCONA;
549 const int x86_qimode_math = ~(0);
550 const int x86_promote_qi_regs = 0;
551 const int x86_himode_math = ~(m_PPRO);
552 const int x86_promote_hi_regs = m_PPRO;
553 const int x86_sub_esp_4 = m_ATHLON_K8 | m_PPRO | m_PENT4 | m_NOCONA;
554 const int x86_sub_esp_8 = m_ATHLON_K8 | m_PPRO | m_386 | m_486 | m_PENT4 | m_NOCONA;
555 const int x86_add_esp_4 = m_ATHLON_K8 | m_K6 | m_PENT4 | m_NOCONA;
556 const int x86_add_esp_8 = m_ATHLON_K8 | m_PPRO | m_K6 | m_386 | m_486 | m_PENT4 | m_NOCONA;
557 const int x86_integer_DFmode_moves = ~(m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO);
558 const int x86_partial_reg_dependency = m_ATHLON_K8 | m_PENT4 | m_NOCONA;
559 const int x86_memory_mismatch_stall = m_ATHLON_K8 | m_PENT4 | m_NOCONA;
560 const int x86_accumulate_outgoing_args = m_ATHLON_K8 | m_PENT4 | m_NOCONA | m_PPRO;
561 const int x86_prologue_using_move = m_ATHLON_K8 | m_PPRO;
562 const int x86_epilogue_using_move = m_ATHLON_K8 | m_PPRO;
563 const int x86_decompose_lea = m_PENT4 | m_NOCONA;
564 const int x86_shift1 = ~m_486;
565 const int x86_arch_always_fancy_math_387 = m_PENT | m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
566 const int x86_sse_partial_reg_dependency = m_PENT4 | m_NOCONA | m_PPRO;
567 /* Set for machines where the type and dependencies are resolved on SSE
568    register parts instead of whole registers, so we may maintain just
569    lower part of scalar values in proper format leaving the upper part
570    undefined.  */
571 const int x86_sse_split_regs = m_ATHLON_K8;
572 const int x86_sse_typeless_stores = m_ATHLON_K8;
573 const int x86_sse_load0_by_pxor = m_PPRO | m_PENT4 | m_NOCONA;
574 const int x86_use_ffreep = m_ATHLON_K8;
575 const int x86_rep_movl_optimal = m_386 | m_PENT | m_PPRO | m_K6;
576
577 /* ??? Allowing interunit moves makes it all too easy for the compiler to put
578    integer data in xmm registers.  Which results in pretty abysmal code.  */
579 const int x86_inter_unit_moves = 0 /* ~(m_ATHLON_K8) */;
580
581 const int x86_ext_80387_constants = m_K6 | m_ATHLON | m_PENT4 | m_NOCONA | m_PPRO;
582 /* Some CPU cores are not able to predict more than 4 branch instructions in
583    the 16 byte window.  */
584 const int x86_four_jump_limit = m_PPRO | m_ATHLON_K8 | m_PENT4 | m_NOCONA;
585 const int x86_schedule = m_PPRO | m_ATHLON_K8 | m_K6 | m_PENT;
586 const int x86_use_bt = m_ATHLON_K8;
587 /* Compare and exchange was added for 80486.  */
588 const int x86_cmpxchg = ~m_386;
589 /* Exchange and add was added for 80486.  */
590 const int x86_xadd = ~m_386;
591
592 /* In case the average insn count for single function invocation is
593    lower than this constant, emit fast (but longer) prologue and
594    epilogue code.  */
595 #define FAST_PROLOGUE_INSN_COUNT 20
596
597 /* Names for 8 (low), 8 (high), and 16-bit registers, respectively.  */
598 static const char *const qi_reg_name[] = QI_REGISTER_NAMES;
599 static const char *const qi_high_reg_name[] = QI_HIGH_REGISTER_NAMES;
600 static const char *const hi_reg_name[] = HI_REGISTER_NAMES;
601
602 /* Array of the smallest class containing reg number REGNO, indexed by
603    REGNO.  Used by REGNO_REG_CLASS in i386.h.  */
604
605 enum reg_class const regclass_map[FIRST_PSEUDO_REGISTER] =
606 {
607   /* ax, dx, cx, bx */
608   AREG, DREG, CREG, BREG,
609   /* si, di, bp, sp */
610   SIREG, DIREG, NON_Q_REGS, NON_Q_REGS,
611   /* FP registers */
612   FP_TOP_REG, FP_SECOND_REG, FLOAT_REGS, FLOAT_REGS,
613   FLOAT_REGS, FLOAT_REGS, FLOAT_REGS, FLOAT_REGS,
614   /* arg pointer */
615   NON_Q_REGS,
616   /* flags, fpsr, dirflag, frame */
617   NO_REGS, NO_REGS, NO_REGS, NON_Q_REGS,
618   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
619   SSE_REGS, SSE_REGS,
620   MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS, MMX_REGS,
621   MMX_REGS, MMX_REGS,
622   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
623   NON_Q_REGS, NON_Q_REGS, NON_Q_REGS, NON_Q_REGS,
624   SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS, SSE_REGS,
625   SSE_REGS, SSE_REGS,
626 };
627
628 /* The "default" register map used in 32bit mode.  */
629
630 int const dbx_register_map[FIRST_PSEUDO_REGISTER] =
631 {
632   0, 2, 1, 3, 6, 7, 4, 5,               /* general regs */
633   12, 13, 14, 15, 16, 17, 18, 19,       /* fp regs */
634   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
635   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE */
636   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX */
637   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
638   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
639 };
640
641 static int const x86_64_int_parameter_registers[6] =
642 {
643   5 /*RDI*/, 4 /*RSI*/, 1 /*RDX*/, 2 /*RCX*/,
644   FIRST_REX_INT_REG /*R8 */, FIRST_REX_INT_REG + 1 /*R9 */
645 };
646
647 static int const x86_64_int_return_registers[4] =
648 {
649   0 /*RAX*/, 1 /*RDI*/, 5 /*RDI*/, 4 /*RSI*/
650 };
651
652 /* The "default" register map used in 64bit mode.  */
653 int const dbx64_register_map[FIRST_PSEUDO_REGISTER] =
654 {
655   0, 1, 2, 3, 4, 5, 6, 7,               /* general regs */
656   33, 34, 35, 36, 37, 38, 39, 40,       /* fp regs */
657   -1, -1, -1, -1, -1,                   /* arg, flags, fpsr, dir, frame */
658   17, 18, 19, 20, 21, 22, 23, 24,       /* SSE */
659   41, 42, 43, 44, 45, 46, 47, 48,       /* MMX */
660   8,9,10,11,12,13,14,15,                /* extended integer registers */
661   25, 26, 27, 28, 29, 30, 31, 32,       /* extended SSE registers */
662 };
663
664 /* Define the register numbers to be used in Dwarf debugging information.
665    The SVR4 reference port C compiler uses the following register numbers
666    in its Dwarf output code:
667         0 for %eax (gcc regno = 0)
668         1 for %ecx (gcc regno = 2)
669         2 for %edx (gcc regno = 1)
670         3 for %ebx (gcc regno = 3)
671         4 for %esp (gcc regno = 7)
672         5 for %ebp (gcc regno = 6)
673         6 for %esi (gcc regno = 4)
674         7 for %edi (gcc regno = 5)
675    The following three DWARF register numbers are never generated by
676    the SVR4 C compiler or by the GNU compilers, but SDB on x86/svr4
677    believes these numbers have these meanings.
678         8  for %eip    (no gcc equivalent)
679         9  for %eflags (gcc regno = 17)
680         10 for %trapno (no gcc equivalent)
681    It is not at all clear how we should number the FP stack registers
682    for the x86 architecture.  If the version of SDB on x86/svr4 were
683    a bit less brain dead with respect to floating-point then we would
684    have a precedent to follow with respect to DWARF register numbers
685    for x86 FP registers, but the SDB on x86/svr4 is so completely
686    broken with respect to FP registers that it is hardly worth thinking
687    of it as something to strive for compatibility with.
688    The version of x86/svr4 SDB I have at the moment does (partially)
689    seem to believe that DWARF register number 11 is associated with
690    the x86 register %st(0), but that's about all.  Higher DWARF
691    register numbers don't seem to be associated with anything in
692    particular, and even for DWARF regno 11, SDB only seems to under-
693    stand that it should say that a variable lives in %st(0) (when
694    asked via an `=' command) if we said it was in DWARF regno 11,
695    but SDB still prints garbage when asked for the value of the
696    variable in question (via a `/' command).
697    (Also note that the labels SDB prints for various FP stack regs
698    when doing an `x' command are all wrong.)
699    Note that these problems generally don't affect the native SVR4
700    C compiler because it doesn't allow the use of -O with -g and
701    because when it is *not* optimizing, it allocates a memory
702    location for each floating-point variable, and the memory
703    location is what gets described in the DWARF AT_location
704    attribute for the variable in question.
705    Regardless of the severe mental illness of the x86/svr4 SDB, we
706    do something sensible here and we use the following DWARF
707    register numbers.  Note that these are all stack-top-relative
708    numbers.
709         11 for %st(0) (gcc regno = 8)
710         12 for %st(1) (gcc regno = 9)
711         13 for %st(2) (gcc regno = 10)
712         14 for %st(3) (gcc regno = 11)
713         15 for %st(4) (gcc regno = 12)
714         16 for %st(5) (gcc regno = 13)
715         17 for %st(6) (gcc regno = 14)
716         18 for %st(7) (gcc regno = 15)
717 */
718 int const svr4_dbx_register_map[FIRST_PSEUDO_REGISTER] =
719 {
720   0, 2, 1, 3, 6, 7, 5, 4,               /* general regs */
721   11, 12, 13, 14, 15, 16, 17, 18,       /* fp regs */
722   -1, 9, -1, -1, -1,                    /* arg, flags, fpsr, dir, frame */
723   21, 22, 23, 24, 25, 26, 27, 28,       /* SSE registers */
724   29, 30, 31, 32, 33, 34, 35, 36,       /* MMX registers */
725   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended integer registers */
726   -1, -1, -1, -1, -1, -1, -1, -1,       /* extended SSE registers */
727 };
728
729 /* Test and compare insns in i386.md store the information needed to
730    generate branch and scc insns here.  */
731
732 rtx ix86_compare_op0 = NULL_RTX;
733 rtx ix86_compare_op1 = NULL_RTX;
734 rtx ix86_compare_emitted = NULL_RTX;
735
736 #define MAX_386_STACK_LOCALS 3
737 /* Size of the register save area.  */
738 #define X86_64_VARARGS_SIZE (REGPARM_MAX * UNITS_PER_WORD + SSE_REGPARM_MAX * 16)
739
740 /* Define the structure for the machine field in struct function.  */
741
742 struct stack_local_entry GTY(())
743 {
744   unsigned short mode;
745   unsigned short n;
746   rtx rtl;
747   struct stack_local_entry *next;
748 };
749
750 /* Structure describing stack frame layout.
751    Stack grows downward:
752
753    [arguments]
754                                               <- ARG_POINTER
755    saved pc
756
757    saved frame pointer if frame_pointer_needed
758                                               <- HARD_FRAME_POINTER
759    [saved regs]
760
761    [padding1]          \
762                         )
763    [va_arg registers]  (
764                         > to_allocate         <- FRAME_POINTER
765    [frame]             (
766                         )
767    [padding2]          /
768   */
769 struct ix86_frame
770 {
771   int nregs;
772   int padding1;
773   int va_arg_size;
774   HOST_WIDE_INT frame;
775   int padding2;
776   int outgoing_arguments_size;
777   int red_zone_size;
778
779   HOST_WIDE_INT to_allocate;
780   /* The offsets relative to ARG_POINTER.  */
781   HOST_WIDE_INT frame_pointer_offset;
782   HOST_WIDE_INT hard_frame_pointer_offset;
783   HOST_WIDE_INT stack_pointer_offset;
784
785   /* When save_regs_using_mov is set, emit prologue using
786      move instead of push instructions.  */
787   bool save_regs_using_mov;
788 };
789
790 /* Code model option as passed by user.  */
791 static const char *ix86_cmodel_string;
792 /* Parsed value.  */
793 enum cmodel ix86_cmodel;
794 /* Asm dialect.  */
795 static const char *ix86_asm_string;
796 enum asm_dialect ix86_asm_dialect = ASM_ATT;
797 /* TLS dialext.  */
798 static const char *ix86_tls_dialect_string;
799 enum tls_dialect ix86_tls_dialect = TLS_DIALECT_GNU;
800
801 /* Which unit we are generating floating point math for.  */
802 enum fpmath_unit ix86_fpmath;
803
804 /* Which cpu are we scheduling for.  */
805 enum processor_type ix86_tune;
806 /* Which instruction set architecture to use.  */
807 enum processor_type ix86_arch;
808
809 /* Strings to hold which cpu and instruction set architecture  to use.  */
810 const char *ix86_tune_string;           /* for -mtune=<xxx> */
811 const char *ix86_arch_string;           /* for -march=<xxx> */
812 static const char *ix86_fpmath_string;  /* for -mfpmath=<xxx> */
813
814 /* # of registers to use to pass arguments.  */
815 static const char *ix86_regparm_string;
816
817 /* true if sse prefetch instruction is not NOOP.  */
818 int x86_prefetch_sse;
819
820 /* ix86_regparm_string as a number */
821 static int ix86_regparm;
822
823 /* Alignment to use for loops and jumps:  */
824
825 /* Power of two alignment for loops.  */
826 static const char *ix86_align_loops_string;
827
828 /* Power of two alignment for non-loop jumps.  */
829 static const char *ix86_align_jumps_string;
830
831 /* Power of two alignment for stack boundary in bytes.  */
832 static const char *ix86_preferred_stack_boundary_string;
833
834 /* Preferred alignment for stack boundary in bits.  */
835 unsigned int ix86_preferred_stack_boundary;
836
837 /* Values 1-5: see jump.c */
838 int ix86_branch_cost;
839 static const char *ix86_branch_cost_string;
840
841 /* Power of two alignment for functions.  */
842 static const char *ix86_align_funcs_string;
843
844 /* Prefix built by ASM_GENERATE_INTERNAL_LABEL.  */
845 char internal_label_prefix[16];
846 int internal_label_prefix_len;
847 \f
848 static bool ix86_handle_option (size_t, const char *, int);
849 static void output_pic_addr_const (FILE *, rtx, int);
850 static void put_condition_code (enum rtx_code, enum machine_mode,
851                                 int, int, FILE *);
852 static const char *get_some_local_dynamic_name (void);
853 static int get_some_local_dynamic_name_1 (rtx *, void *);
854 static rtx ix86_expand_int_compare (enum rtx_code, rtx, rtx);
855 static enum rtx_code ix86_prepare_fp_compare_args (enum rtx_code, rtx *,
856                                                    rtx *);
857 static bool ix86_fixed_condition_code_regs (unsigned int *, unsigned int *);
858 static enum machine_mode ix86_cc_modes_compatible (enum machine_mode,
859                                                    enum machine_mode);
860 static rtx get_thread_pointer (int);
861 static rtx legitimize_tls_address (rtx, enum tls_model, int);
862 static void get_pc_thunk_name (char [32], unsigned int);
863 static rtx gen_push (rtx);
864 static int ix86_flags_dependant (rtx, rtx, enum attr_type);
865 static int ix86_agi_dependant (rtx, rtx, enum attr_type);
866 static struct machine_function * ix86_init_machine_status (void);
867 static int ix86_split_to_parts (rtx, rtx *, enum machine_mode);
868 static int ix86_nsaved_regs (void);
869 static void ix86_emit_save_regs (void);
870 static void ix86_emit_save_regs_using_mov (rtx, HOST_WIDE_INT);
871 static void ix86_emit_restore_regs_using_mov (rtx, HOST_WIDE_INT, int);
872 static void ix86_output_function_epilogue (FILE *, HOST_WIDE_INT);
873 static HOST_WIDE_INT ix86_GOT_alias_set (void);
874 static void ix86_adjust_counter (rtx, HOST_WIDE_INT);
875 static rtx ix86_expand_aligntest (rtx, int);
876 static void ix86_expand_strlensi_unroll_1 (rtx, rtx, rtx);
877 static int ix86_issue_rate (void);
878 static int ix86_adjust_cost (rtx, rtx, rtx, int);
879 static int ia32_multipass_dfa_lookahead (void);
880 static void ix86_init_mmx_sse_builtins (void);
881 static rtx x86_this_parameter (tree);
882 static void x86_output_mi_thunk (FILE *, tree, HOST_WIDE_INT,
883                                  HOST_WIDE_INT, tree);
884 static bool x86_can_output_mi_thunk (tree, HOST_WIDE_INT, HOST_WIDE_INT, tree);
885 static void x86_file_start (void);
886 static void ix86_reorg (void);
887 static bool ix86_expand_carry_flag_compare (enum rtx_code, rtx, rtx, rtx*);
888 static tree ix86_build_builtin_va_list (void);
889 static void ix86_setup_incoming_varargs (CUMULATIVE_ARGS *, enum machine_mode,
890                                          tree, int *, int);
891 static tree ix86_gimplify_va_arg (tree, tree, tree *, tree *);
892 static bool ix86_vector_mode_supported_p (enum machine_mode);
893
894 static int ix86_address_cost (rtx);
895 static bool ix86_cannot_force_const_mem (rtx);
896 static rtx ix86_delegitimize_address (rtx);
897
898 struct builtin_description;
899 static rtx ix86_expand_sse_comi (const struct builtin_description *,
900                                  tree, rtx);
901 static rtx ix86_expand_sse_compare (const struct builtin_description *,
902                                     tree, rtx);
903 static rtx ix86_expand_unop1_builtin (enum insn_code, tree, rtx);
904 static rtx ix86_expand_unop_builtin (enum insn_code, tree, rtx, int);
905 static rtx ix86_expand_binop_builtin (enum insn_code, tree, rtx);
906 static rtx ix86_expand_store_builtin (enum insn_code, tree);
907 static rtx safe_vector_operand (rtx, enum machine_mode);
908 static rtx ix86_expand_fp_compare (enum rtx_code, rtx, rtx, rtx, rtx *, rtx *);
909 static int ix86_fp_comparison_arithmetics_cost (enum rtx_code code);
910 static int ix86_fp_comparison_fcomi_cost (enum rtx_code code);
911 static int ix86_fp_comparison_sahf_cost (enum rtx_code code);
912 static int ix86_fp_comparison_cost (enum rtx_code code);
913 static unsigned int ix86_select_alt_pic_regnum (void);
914 static int ix86_save_reg (unsigned int, int);
915 static void ix86_compute_frame_layout (struct ix86_frame *);
916 static int ix86_comp_type_attributes (tree, tree);
917 static int ix86_function_regparm (tree, tree);
918 const struct attribute_spec ix86_attribute_table[];
919 static bool ix86_function_ok_for_sibcall (tree, tree);
920 static tree ix86_handle_cdecl_attribute (tree *, tree, tree, int, bool *);
921 static tree ix86_handle_regparm_attribute (tree *, tree, tree, int, bool *);
922 static int ix86_value_regno (enum machine_mode, tree);
923 static bool contains_128bit_aligned_vector_p (tree);
924 static rtx ix86_struct_value_rtx (tree, int);
925 static bool ix86_ms_bitfield_layout_p (tree);
926 static tree ix86_handle_struct_attribute (tree *, tree, tree, int, bool *);
927 static int extended_reg_mentioned_1 (rtx *, void *);
928 static bool ix86_rtx_costs (rtx, int, int, int *);
929 static int min_insn_size (rtx);
930 static tree ix86_md_asm_clobbers (tree outputs, tree inputs, tree clobbers);
931 static bool ix86_must_pass_in_stack (enum machine_mode mode, tree type);
932 static bool ix86_pass_by_reference (CUMULATIVE_ARGS *, enum machine_mode,
933                                     tree, bool);
934 static void ix86_init_builtins (void);
935 static rtx ix86_expand_builtin (tree, rtx, rtx, enum machine_mode, int);
936
937 /* This function is only used on Solaris.  */
938 static void i386_solaris_elf_named_section (const char *, unsigned int, tree)
939   ATTRIBUTE_UNUSED;
940
941 /* Register class used for passing given 64bit part of the argument.
942    These represent classes as documented by the PS ABI, with the exception
943    of SSESF, SSEDF classes, that are basically SSE class, just gcc will
944    use SF or DFmode move instead of DImode to avoid reformatting penalties.
945
946    Similarly we play games with INTEGERSI_CLASS to use cheaper SImode moves
947    whenever possible (upper half does contain padding).
948  */
949 enum x86_64_reg_class
950   {
951     X86_64_NO_CLASS,
952     X86_64_INTEGER_CLASS,
953     X86_64_INTEGERSI_CLASS,
954     X86_64_SSE_CLASS,
955     X86_64_SSESF_CLASS,
956     X86_64_SSEDF_CLASS,
957     X86_64_SSEUP_CLASS,
958     X86_64_X87_CLASS,
959     X86_64_X87UP_CLASS,
960     X86_64_COMPLEX_X87_CLASS,
961     X86_64_MEMORY_CLASS
962   };
963 static const char * const x86_64_reg_class_name[] = {
964   "no", "integer", "integerSI", "sse", "sseSF", "sseDF",
965   "sseup", "x87", "x87up", "cplx87", "no"
966 };
967
968 #define MAX_CLASSES 4
969
970 /* Table of constants used by fldpi, fldln2, etc....  */
971 static REAL_VALUE_TYPE ext_80387_constants_table [5];
972 static bool ext_80387_constants_init = 0;
973 static void init_ext_80387_constants (void);
974 \f
975 /* Initialize the GCC target structure.  */
976 #undef TARGET_ATTRIBUTE_TABLE
977 #define TARGET_ATTRIBUTE_TABLE ix86_attribute_table
978 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
979 #  undef TARGET_MERGE_DECL_ATTRIBUTES
980 #  define TARGET_MERGE_DECL_ATTRIBUTES merge_dllimport_decl_attributes
981 #endif
982
983 #undef TARGET_COMP_TYPE_ATTRIBUTES
984 #define TARGET_COMP_TYPE_ATTRIBUTES ix86_comp_type_attributes
985
986 #undef TARGET_INIT_BUILTINS
987 #define TARGET_INIT_BUILTINS ix86_init_builtins
988 #undef TARGET_EXPAND_BUILTIN
989 #define TARGET_EXPAND_BUILTIN ix86_expand_builtin
990
991 #undef TARGET_ASM_FUNCTION_EPILOGUE
992 #define TARGET_ASM_FUNCTION_EPILOGUE ix86_output_function_epilogue
993
994 #undef TARGET_ASM_OPEN_PAREN
995 #define TARGET_ASM_OPEN_PAREN ""
996 #undef TARGET_ASM_CLOSE_PAREN
997 #define TARGET_ASM_CLOSE_PAREN ""
998
999 #undef TARGET_ASM_ALIGNED_HI_OP
1000 #define TARGET_ASM_ALIGNED_HI_OP ASM_SHORT
1001 #undef TARGET_ASM_ALIGNED_SI_OP
1002 #define TARGET_ASM_ALIGNED_SI_OP ASM_LONG
1003 #ifdef ASM_QUAD
1004 #undef TARGET_ASM_ALIGNED_DI_OP
1005 #define TARGET_ASM_ALIGNED_DI_OP ASM_QUAD
1006 #endif
1007
1008 #undef TARGET_ASM_UNALIGNED_HI_OP
1009 #define TARGET_ASM_UNALIGNED_HI_OP TARGET_ASM_ALIGNED_HI_OP
1010 #undef TARGET_ASM_UNALIGNED_SI_OP
1011 #define TARGET_ASM_UNALIGNED_SI_OP TARGET_ASM_ALIGNED_SI_OP
1012 #undef TARGET_ASM_UNALIGNED_DI_OP
1013 #define TARGET_ASM_UNALIGNED_DI_OP TARGET_ASM_ALIGNED_DI_OP
1014
1015 #undef TARGET_SCHED_ADJUST_COST
1016 #define TARGET_SCHED_ADJUST_COST ix86_adjust_cost
1017 #undef TARGET_SCHED_ISSUE_RATE
1018 #define TARGET_SCHED_ISSUE_RATE ix86_issue_rate
1019 #undef TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD
1020 #define TARGET_SCHED_FIRST_CYCLE_MULTIPASS_DFA_LOOKAHEAD \
1021   ia32_multipass_dfa_lookahead
1022
1023 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
1024 #define TARGET_FUNCTION_OK_FOR_SIBCALL ix86_function_ok_for_sibcall
1025
1026 #ifdef HAVE_AS_TLS
1027 #undef TARGET_HAVE_TLS
1028 #define TARGET_HAVE_TLS true
1029 #endif
1030 #undef TARGET_CANNOT_FORCE_CONST_MEM
1031 #define TARGET_CANNOT_FORCE_CONST_MEM ix86_cannot_force_const_mem
1032
1033 #undef TARGET_DELEGITIMIZE_ADDRESS
1034 #define TARGET_DELEGITIMIZE_ADDRESS ix86_delegitimize_address
1035
1036 #undef TARGET_MS_BITFIELD_LAYOUT_P
1037 #define TARGET_MS_BITFIELD_LAYOUT_P ix86_ms_bitfield_layout_p
1038
1039 #undef TARGET_ASM_OUTPUT_MI_THUNK
1040 #define TARGET_ASM_OUTPUT_MI_THUNK x86_output_mi_thunk
1041 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
1042 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK x86_can_output_mi_thunk
1043
1044 #undef TARGET_ASM_FILE_START
1045 #define TARGET_ASM_FILE_START x86_file_start
1046
1047 #undef TARGET_DEFAULT_TARGET_FLAGS
1048 #define TARGET_DEFAULT_TARGET_FLAGS     \
1049   (TARGET_DEFAULT                       \
1050    | TARGET_64BIT_DEFAULT               \
1051    | TARGET_SUBTARGET_DEFAULT           \
1052    | TARGET_TLS_DIRECT_SEG_REFS_DEFAULT)
1053
1054 #undef TARGET_HANDLE_OPTION
1055 #define TARGET_HANDLE_OPTION ix86_handle_option
1056
1057 #undef TARGET_RTX_COSTS
1058 #define TARGET_RTX_COSTS ix86_rtx_costs
1059 #undef TARGET_ADDRESS_COST
1060 #define TARGET_ADDRESS_COST ix86_address_cost
1061
1062 #undef TARGET_FIXED_CONDITION_CODE_REGS
1063 #define TARGET_FIXED_CONDITION_CODE_REGS ix86_fixed_condition_code_regs
1064 #undef TARGET_CC_MODES_COMPATIBLE
1065 #define TARGET_CC_MODES_COMPATIBLE ix86_cc_modes_compatible
1066
1067 #undef TARGET_MACHINE_DEPENDENT_REORG
1068 #define TARGET_MACHINE_DEPENDENT_REORG ix86_reorg
1069
1070 #undef TARGET_BUILD_BUILTIN_VA_LIST
1071 #define TARGET_BUILD_BUILTIN_VA_LIST ix86_build_builtin_va_list
1072
1073 #undef TARGET_MD_ASM_CLOBBERS
1074 #define TARGET_MD_ASM_CLOBBERS ix86_md_asm_clobbers
1075
1076 #undef TARGET_PROMOTE_PROTOTYPES
1077 #define TARGET_PROMOTE_PROTOTYPES hook_bool_tree_true
1078 #undef TARGET_STRUCT_VALUE_RTX
1079 #define TARGET_STRUCT_VALUE_RTX ix86_struct_value_rtx
1080 #undef TARGET_SETUP_INCOMING_VARARGS
1081 #define TARGET_SETUP_INCOMING_VARARGS ix86_setup_incoming_varargs
1082 #undef TARGET_MUST_PASS_IN_STACK
1083 #define TARGET_MUST_PASS_IN_STACK ix86_must_pass_in_stack
1084 #undef TARGET_PASS_BY_REFERENCE
1085 #define TARGET_PASS_BY_REFERENCE ix86_pass_by_reference
1086
1087 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
1088 #define TARGET_GIMPLIFY_VA_ARG_EXPR ix86_gimplify_va_arg
1089
1090 #undef TARGET_VECTOR_MODE_SUPPORTED_P
1091 #define TARGET_VECTOR_MODE_SUPPORTED_P ix86_vector_mode_supported_p
1092
1093 #ifdef SUBTARGET_INSERT_ATTRIBUTES
1094 #undef TARGET_INSERT_ATTRIBUTES
1095 #define TARGET_INSERT_ATTRIBUTES SUBTARGET_INSERT_ATTRIBUTES
1096 #endif
1097
1098 struct gcc_target targetm = TARGET_INITIALIZER;
1099
1100 \f
1101 /* The svr4 ABI for the i386 says that records and unions are returned
1102    in memory.  */
1103 #ifndef DEFAULT_PCC_STRUCT_RETURN
1104 #define DEFAULT_PCC_STRUCT_RETURN 1
1105 #endif
1106
1107 /* Implement TARGET_HANDLE_OPTION.  */
1108
1109 static bool
1110 ix86_handle_option (size_t code, const char *arg, int value)
1111 {
1112   switch (code)
1113     {
1114     case OPT_m3dnow:
1115       if (!value)
1116         {
1117           target_flags &= ~MASK_3DNOW_A;
1118           target_flags_explicit |= MASK_3DNOW_A;
1119         }
1120       return true;
1121
1122     case OPT_malign_functions_:
1123       ix86_align_funcs_string = arg;
1124       return true;
1125
1126     case OPT_malign_jumps_:
1127       ix86_align_jumps_string = arg;
1128       return true;
1129
1130     case OPT_malign_loops_:
1131       ix86_align_loops_string = arg;
1132       return true;
1133
1134     case OPT_march_:
1135       ix86_arch_string = arg;
1136       return true;
1137
1138     case OPT_masm_:
1139       ix86_asm_string = arg;
1140       return true;
1141
1142     case OPT_mbranch_cost_:
1143       ix86_branch_cost_string = arg;
1144       return true;
1145
1146     case OPT_mcmodel_:
1147       ix86_cmodel_string = arg;
1148       return true;
1149
1150     case OPT_mfpmath_:
1151       ix86_fpmath_string = arg;
1152       return true;
1153
1154     case OPT_mmmx:
1155       if (!value)
1156         {
1157           target_flags &= ~(MASK_3DNOW | MASK_3DNOW_A);
1158           target_flags_explicit |= MASK_3DNOW | MASK_3DNOW_A;
1159         }
1160       return true;
1161
1162     case OPT_mpreferred_stack_boundary_:
1163       ix86_preferred_stack_boundary_string = arg;
1164       return true;
1165
1166     case OPT_mregparm_:
1167       ix86_regparm_string = arg;
1168       return true;
1169
1170     case OPT_msse:
1171       if (!value)
1172         {
1173           target_flags &= ~(MASK_SSE2 | MASK_SSE3);
1174           target_flags_explicit |= MASK_SSE2 | MASK_SSE3;
1175         }
1176       return true;
1177
1178     case OPT_msse2:
1179       if (!value)
1180         {
1181           target_flags &= ~MASK_SSE3;
1182           target_flags_explicit |= MASK_SSE3;
1183         }
1184       return true;
1185
1186     case OPT_mtls_dialect_:
1187       ix86_tls_dialect_string = arg;
1188       return true;
1189
1190     case OPT_mtune_:
1191       ix86_tune_string = arg;
1192       return true;
1193
1194     default:
1195       return true;
1196     }
1197 }
1198
1199 /* Sometimes certain combinations of command options do not make
1200    sense on a particular target machine.  You can define a macro
1201    `OVERRIDE_OPTIONS' to take account of this.  This macro, if
1202    defined, is executed once just after all the command options have
1203    been parsed.
1204
1205    Don't use this macro to turn on various extra optimizations for
1206    `-O'.  That is what `OPTIMIZATION_OPTIONS' is for.  */
1207
1208 void
1209 override_options (void)
1210 {
1211   int i;
1212   int ix86_tune_defaulted = 0;
1213
1214   /* Comes from final.c -- no real reason to change it.  */
1215 #define MAX_CODE_ALIGN 16
1216
1217   static struct ptt
1218     {
1219       const struct processor_costs *cost;       /* Processor costs */
1220       const int target_enable;                  /* Target flags to enable.  */
1221       const int target_disable;                 /* Target flags to disable.  */
1222       const int align_loop;                     /* Default alignments.  */
1223       const int align_loop_max_skip;
1224       const int align_jump;
1225       const int align_jump_max_skip;
1226       const int align_func;
1227     }
1228   const processor_target_table[PROCESSOR_max] =
1229     {
1230       {&i386_cost, 0, 0, 4, 3, 4, 3, 4},
1231       {&i486_cost, 0, 0, 16, 15, 16, 15, 16},
1232       {&pentium_cost, 0, 0, 16, 7, 16, 7, 16},
1233       {&pentiumpro_cost, 0, 0, 16, 15, 16, 7, 16},
1234       {&k6_cost, 0, 0, 32, 7, 32, 7, 32},
1235       {&athlon_cost, 0, 0, 16, 7, 16, 7, 16},
1236       {&pentium4_cost, 0, 0, 0, 0, 0, 0, 0},
1237       {&k8_cost, 0, 0, 16, 7, 16, 7, 16},
1238       {&nocona_cost, 0, 0, 0, 0, 0, 0, 0}
1239     };
1240
1241   static const char * const cpu_names[] = TARGET_CPU_DEFAULT_NAMES;
1242   static struct pta
1243     {
1244       const char *const name;           /* processor name or nickname.  */
1245       const enum processor_type processor;
1246       const enum pta_flags
1247         {
1248           PTA_SSE = 1,
1249           PTA_SSE2 = 2,
1250           PTA_SSE3 = 4,
1251           PTA_MMX = 8,
1252           PTA_PREFETCH_SSE = 16,
1253           PTA_3DNOW = 32,
1254           PTA_3DNOW_A = 64,
1255           PTA_64BIT = 128
1256         } flags;
1257     }
1258   const processor_alias_table[] =
1259     {
1260       {"i386", PROCESSOR_I386, 0},
1261       {"i486", PROCESSOR_I486, 0},
1262       {"i586", PROCESSOR_PENTIUM, 0},
1263       {"pentium", PROCESSOR_PENTIUM, 0},
1264       {"pentium-mmx", PROCESSOR_PENTIUM, PTA_MMX},
1265       {"winchip-c6", PROCESSOR_I486, PTA_MMX},
1266       {"winchip2", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1267       {"c3", PROCESSOR_I486, PTA_MMX | PTA_3DNOW},
1268       {"c3-2", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_PREFETCH_SSE | PTA_SSE},
1269       {"i686", PROCESSOR_PENTIUMPRO, 0},
1270       {"pentiumpro", PROCESSOR_PENTIUMPRO, 0},
1271       {"pentium2", PROCESSOR_PENTIUMPRO, PTA_MMX},
1272       {"pentium3", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1273       {"pentium3m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE},
1274       {"pentium-m", PROCESSOR_PENTIUMPRO, PTA_MMX | PTA_SSE | PTA_PREFETCH_SSE | PTA_SSE2},
1275       {"pentium4", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1276                                        | PTA_MMX | PTA_PREFETCH_SSE},
1277       {"pentium4m", PROCESSOR_PENTIUM4, PTA_SSE | PTA_SSE2
1278                                         | PTA_MMX | PTA_PREFETCH_SSE},
1279       {"prescott", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3
1280                                         | PTA_MMX | PTA_PREFETCH_SSE},
1281       {"nocona", PROCESSOR_NOCONA, PTA_SSE | PTA_SSE2 | PTA_SSE3 | PTA_64BIT
1282                                         | PTA_MMX | PTA_PREFETCH_SSE},
1283       {"k6", PROCESSOR_K6, PTA_MMX},
1284       {"k6-2", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1285       {"k6-3", PROCESSOR_K6, PTA_MMX | PTA_3DNOW},
1286       {"athlon", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1287                                    | PTA_3DNOW_A},
1288       {"athlon-tbird", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE
1289                                          | PTA_3DNOW | PTA_3DNOW_A},
1290       {"athlon-4", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1291                                     | PTA_3DNOW_A | PTA_SSE},
1292       {"athlon-xp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1293                                       | PTA_3DNOW_A | PTA_SSE},
1294       {"athlon-mp", PROCESSOR_ATHLON, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW
1295                                       | PTA_3DNOW_A | PTA_SSE},
1296       {"x86-64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_64BIT
1297                                | PTA_SSE | PTA_SSE2 },
1298       {"k8", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1299                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1300       {"opteron", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1301                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1302       {"athlon64", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1303                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1304       {"athlon-fx", PROCESSOR_K8, PTA_MMX | PTA_PREFETCH_SSE | PTA_3DNOW | PTA_64BIT
1305                                       | PTA_3DNOW_A | PTA_SSE | PTA_SSE2},
1306     };
1307
1308   int const pta_size = ARRAY_SIZE (processor_alias_table);
1309
1310 #ifdef SUBTARGET_OVERRIDE_OPTIONS
1311   SUBTARGET_OVERRIDE_OPTIONS;
1312 #endif
1313
1314   /* Set the default values for switches whose default depends on TARGET_64BIT
1315      in case they weren't overwritten by command line options.  */
1316   if (TARGET_64BIT)
1317     {
1318       if (flag_omit_frame_pointer == 2)
1319         flag_omit_frame_pointer = 1;
1320       if (flag_asynchronous_unwind_tables == 2)
1321         flag_asynchronous_unwind_tables = 1;
1322       if (flag_pcc_struct_return == 2)
1323         flag_pcc_struct_return = 0;
1324     }
1325   else
1326     {
1327       if (flag_omit_frame_pointer == 2)
1328         flag_omit_frame_pointer = 0;
1329       if (flag_asynchronous_unwind_tables == 2)
1330         flag_asynchronous_unwind_tables = 0;
1331       if (flag_pcc_struct_return == 2)
1332         flag_pcc_struct_return = DEFAULT_PCC_STRUCT_RETURN;
1333     }
1334
1335   if (!ix86_tune_string && ix86_arch_string)
1336     ix86_tune_string = ix86_arch_string;
1337   if (!ix86_tune_string)
1338     {
1339       ix86_tune_string = cpu_names [TARGET_CPU_DEFAULT];
1340       ix86_tune_defaulted = 1;
1341     }
1342   if (!ix86_arch_string)
1343     ix86_arch_string = TARGET_64BIT ? "x86-64" : "i386";
1344
1345   if (ix86_cmodel_string != 0)
1346     {
1347       if (!strcmp (ix86_cmodel_string, "small"))
1348         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1349       else if (flag_pic)
1350         sorry ("code model %s not supported in PIC mode", ix86_cmodel_string);
1351       else if (!strcmp (ix86_cmodel_string, "32"))
1352         ix86_cmodel = CM_32;
1353       else if (!strcmp (ix86_cmodel_string, "kernel") && !flag_pic)
1354         ix86_cmodel = CM_KERNEL;
1355       else if (!strcmp (ix86_cmodel_string, "medium") && !flag_pic)
1356         ix86_cmodel = CM_MEDIUM;
1357       else if (!strcmp (ix86_cmodel_string, "large") && !flag_pic)
1358         ix86_cmodel = CM_LARGE;
1359       else
1360         error ("bad value (%s) for -mcmodel= switch", ix86_cmodel_string);
1361     }
1362   else
1363     {
1364       ix86_cmodel = CM_32;
1365       if (TARGET_64BIT)
1366         ix86_cmodel = flag_pic ? CM_SMALL_PIC : CM_SMALL;
1367     }
1368   if (ix86_asm_string != 0)
1369     {
1370       if (!strcmp (ix86_asm_string, "intel"))
1371         ix86_asm_dialect = ASM_INTEL;
1372       else if (!strcmp (ix86_asm_string, "att"))
1373         ix86_asm_dialect = ASM_ATT;
1374       else
1375         error ("bad value (%s) for -masm= switch", ix86_asm_string);
1376     }
1377   if ((TARGET_64BIT == 0) != (ix86_cmodel == CM_32))
1378     error ("code model %qs not supported in the %s bit mode",
1379            ix86_cmodel_string, TARGET_64BIT ? "64" : "32");
1380   if (ix86_cmodel == CM_LARGE)
1381     sorry ("code model %<large%> not supported yet");
1382   if ((TARGET_64BIT != 0) != ((target_flags & MASK_64BIT) != 0))
1383     sorry ("%i-bit mode not compiled in",
1384            (target_flags & MASK_64BIT) ? 64 : 32);
1385
1386   for (i = 0; i < pta_size; i++)
1387     if (! strcmp (ix86_arch_string, processor_alias_table[i].name))
1388       {
1389         ix86_arch = processor_alias_table[i].processor;
1390         /* Default cpu tuning to the architecture.  */
1391         ix86_tune = ix86_arch;
1392         if (processor_alias_table[i].flags & PTA_MMX
1393             && !(target_flags_explicit & MASK_MMX))
1394           target_flags |= MASK_MMX;
1395         if (processor_alias_table[i].flags & PTA_3DNOW
1396             && !(target_flags_explicit & MASK_3DNOW))
1397           target_flags |= MASK_3DNOW;
1398         if (processor_alias_table[i].flags & PTA_3DNOW_A
1399             && !(target_flags_explicit & MASK_3DNOW_A))
1400           target_flags |= MASK_3DNOW_A;
1401         if (processor_alias_table[i].flags & PTA_SSE
1402             && !(target_flags_explicit & MASK_SSE))
1403           target_flags |= MASK_SSE;
1404         if (processor_alias_table[i].flags & PTA_SSE2
1405             && !(target_flags_explicit & MASK_SSE2))
1406           target_flags |= MASK_SSE2;
1407         if (processor_alias_table[i].flags & PTA_SSE3
1408             && !(target_flags_explicit & MASK_SSE3))
1409           target_flags |= MASK_SSE3;
1410         if (processor_alias_table[i].flags & PTA_PREFETCH_SSE)
1411           x86_prefetch_sse = true;
1412         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1413           error ("CPU you selected does not support x86-64 "
1414                  "instruction set");
1415         break;
1416       }
1417
1418   if (i == pta_size)
1419     error ("bad value (%s) for -march= switch", ix86_arch_string);
1420
1421   for (i = 0; i < pta_size; i++)
1422     if (! strcmp (ix86_tune_string, processor_alias_table[i].name))
1423       {
1424         ix86_tune = processor_alias_table[i].processor;
1425         if (TARGET_64BIT && !(processor_alias_table[i].flags & PTA_64BIT))
1426           {
1427             if (ix86_tune_defaulted)
1428               {
1429                 ix86_tune_string = "x86-64";
1430                 for (i = 0; i < pta_size; i++)
1431                   if (! strcmp (ix86_tune_string,
1432                                 processor_alias_table[i].name))
1433                     break;
1434                 ix86_tune = processor_alias_table[i].processor;
1435               }
1436             else
1437               error ("CPU you selected does not support x86-64 "
1438                      "instruction set");
1439           }
1440         /* Intel CPUs have always interpreted SSE prefetch instructions as
1441            NOPs; so, we can enable SSE prefetch instructions even when
1442            -mtune (rather than -march) points us to a processor that has them.
1443            However, the VIA C3 gives a SIGILL, so we only do that for i686 and
1444            higher processors.  */
1445         if (TARGET_CMOVE && (processor_alias_table[i].flags & PTA_PREFETCH_SSE))
1446           x86_prefetch_sse = true;
1447         break;
1448       }
1449   if (i == pta_size)
1450     error ("bad value (%s) for -mtune= switch", ix86_tune_string);
1451
1452   if (optimize_size)
1453     ix86_cost = &size_cost;
1454   else
1455     ix86_cost = processor_target_table[ix86_tune].cost;
1456   target_flags |= processor_target_table[ix86_tune].target_enable;
1457   target_flags &= ~processor_target_table[ix86_tune].target_disable;
1458
1459   /* Arrange to set up i386_stack_locals for all functions.  */
1460   init_machine_status = ix86_init_machine_status;
1461
1462   /* Validate -mregparm= value.  */
1463   if (ix86_regparm_string)
1464     {
1465       i = atoi (ix86_regparm_string);
1466       if (i < 0 || i > REGPARM_MAX)
1467         error ("-mregparm=%d is not between 0 and %d", i, REGPARM_MAX);
1468       else
1469         ix86_regparm = i;
1470     }
1471   else
1472    if (TARGET_64BIT)
1473      ix86_regparm = REGPARM_MAX;
1474
1475   /* If the user has provided any of the -malign-* options,
1476      warn and use that value only if -falign-* is not set.
1477      Remove this code in GCC 3.2 or later.  */
1478   if (ix86_align_loops_string)
1479     {
1480       warning ("-malign-loops is obsolete, use -falign-loops");
1481       if (align_loops == 0)
1482         {
1483           i = atoi (ix86_align_loops_string);
1484           if (i < 0 || i > MAX_CODE_ALIGN)
1485             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1486           else
1487             align_loops = 1 << i;
1488         }
1489     }
1490
1491   if (ix86_align_jumps_string)
1492     {
1493       warning ("-malign-jumps is obsolete, use -falign-jumps");
1494       if (align_jumps == 0)
1495         {
1496           i = atoi (ix86_align_jumps_string);
1497           if (i < 0 || i > MAX_CODE_ALIGN)
1498             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1499           else
1500             align_jumps = 1 << i;
1501         }
1502     }
1503
1504   if (ix86_align_funcs_string)
1505     {
1506       warning ("-malign-functions is obsolete, use -falign-functions");
1507       if (align_functions == 0)
1508         {
1509           i = atoi (ix86_align_funcs_string);
1510           if (i < 0 || i > MAX_CODE_ALIGN)
1511             error ("-malign-loops=%d is not between 0 and %d", i, MAX_CODE_ALIGN);
1512           else
1513             align_functions = 1 << i;
1514         }
1515     }
1516
1517   /* Default align_* from the processor table.  */
1518   if (align_loops == 0)
1519     {
1520       align_loops = processor_target_table[ix86_tune].align_loop;
1521       align_loops_max_skip = processor_target_table[ix86_tune].align_loop_max_skip;
1522     }
1523   if (align_jumps == 0)
1524     {
1525       align_jumps = processor_target_table[ix86_tune].align_jump;
1526       align_jumps_max_skip = processor_target_table[ix86_tune].align_jump_max_skip;
1527     }
1528   if (align_functions == 0)
1529     {
1530       align_functions = processor_target_table[ix86_tune].align_func;
1531     }
1532
1533   /* Validate -mpreferred-stack-boundary= value, or provide default.
1534      The default of 128 bits is for Pentium III's SSE __m128, but we
1535      don't want additional code to keep the stack aligned when
1536      optimizing for code size.  */
1537   ix86_preferred_stack_boundary = (optimize_size
1538                                    ? TARGET_64BIT ? 128 : 32
1539                                    : 128);
1540   if (ix86_preferred_stack_boundary_string)
1541     {
1542       i = atoi (ix86_preferred_stack_boundary_string);
1543       if (i < (TARGET_64BIT ? 4 : 2) || i > 12)
1544         error ("-mpreferred-stack-boundary=%d is not between %d and 12", i,
1545                TARGET_64BIT ? 4 : 2);
1546       else
1547         ix86_preferred_stack_boundary = (1 << i) * BITS_PER_UNIT;
1548     }
1549
1550   /* Validate -mbranch-cost= value, or provide default.  */
1551   ix86_branch_cost = processor_target_table[ix86_tune].cost->branch_cost;
1552   if (ix86_branch_cost_string)
1553     {
1554       i = atoi (ix86_branch_cost_string);
1555       if (i < 0 || i > 5)
1556         error ("-mbranch-cost=%d is not between 0 and 5", i);
1557       else
1558         ix86_branch_cost = i;
1559     }
1560
1561   if (ix86_tls_dialect_string)
1562     {
1563       if (strcmp (ix86_tls_dialect_string, "gnu") == 0)
1564         ix86_tls_dialect = TLS_DIALECT_GNU;
1565       else if (strcmp (ix86_tls_dialect_string, "sun") == 0)
1566         ix86_tls_dialect = TLS_DIALECT_SUN;
1567       else
1568         error ("bad value (%s) for -mtls-dialect= switch",
1569                ix86_tls_dialect_string);
1570     }
1571
1572   /* Keep nonleaf frame pointers.  */
1573   if (flag_omit_frame_pointer)
1574     target_flags &= ~MASK_OMIT_LEAF_FRAME_POINTER;
1575   else if (TARGET_OMIT_LEAF_FRAME_POINTER)
1576     flag_omit_frame_pointer = 1;
1577
1578   /* If we're doing fast math, we don't care about comparison order
1579      wrt NaNs.  This lets us use a shorter comparison sequence.  */
1580   if (flag_unsafe_math_optimizations)
1581     target_flags &= ~MASK_IEEE_FP;
1582
1583   /* If the architecture always has an FPU, turn off NO_FANCY_MATH_387,
1584      since the insns won't need emulation.  */
1585   if (x86_arch_always_fancy_math_387 & (1 << ix86_arch))
1586     target_flags &= ~MASK_NO_FANCY_MATH_387;
1587
1588   /* Likewise, if the target doesn't have a 387, or we've specified
1589      software floating point, don't use 387 inline instrinsics.  */
1590   if (!TARGET_80387)
1591     target_flags |= MASK_NO_FANCY_MATH_387;
1592
1593   /* Turn on SSE2 builtins for -msse3.  */
1594   if (TARGET_SSE3)
1595     target_flags |= MASK_SSE2;
1596
1597   /* Turn on SSE builtins for -msse2.  */
1598   if (TARGET_SSE2)
1599     target_flags |= MASK_SSE;
1600
1601   /* Turn on MMX builtins for -msse.  */
1602   if (TARGET_SSE)
1603     {
1604       target_flags |= MASK_MMX & ~target_flags_explicit;
1605       x86_prefetch_sse = true;
1606     }
1607
1608   /* Turn on MMX builtins for 3Dnow.  */
1609   if (TARGET_3DNOW)
1610     target_flags |= MASK_MMX;
1611
1612   if (TARGET_64BIT)
1613     {
1614       if (TARGET_ALIGN_DOUBLE)
1615         error ("-malign-double makes no sense in the 64bit mode");
1616       if (TARGET_RTD)
1617         error ("-mrtd calling convention not supported in the 64bit mode");
1618
1619       /* Enable by default the SSE and MMX builtins.  Do allow the user to
1620          explicitly disable any of these.  In particular, disabling SSE and
1621          MMX for kernel code is extremely useful.  */
1622       target_flags
1623         |= ((MASK_SSE2 | MASK_SSE | MASK_MMX | MASK_128BIT_LONG_DOUBLE)
1624             & ~target_flags_explicit);
1625      }
1626   else
1627     {
1628       /* i386 ABI does not specify red zone.  It still makes sense to use it
1629          when programmer takes care to stack from being destroyed.  */
1630       if (!(target_flags_explicit & MASK_NO_RED_ZONE))
1631         target_flags |= MASK_NO_RED_ZONE;
1632     }
1633
1634   ix86_fpmath = TARGET_FPMATH_DEFAULT;
1635
1636   if (ix86_fpmath_string != 0)
1637     {
1638       if (! strcmp (ix86_fpmath_string, "387"))
1639         ix86_fpmath = FPMATH_387;
1640       else if (! strcmp (ix86_fpmath_string, "sse"))
1641         {
1642           if (!TARGET_SSE)
1643             {
1644               warning ("SSE instruction set disabled, using 387 arithmetics");
1645               ix86_fpmath = FPMATH_387;
1646             }
1647           else
1648             ix86_fpmath = FPMATH_SSE;
1649         }
1650       else if (! strcmp (ix86_fpmath_string, "387,sse")
1651                || ! strcmp (ix86_fpmath_string, "sse,387"))
1652         {
1653           if (!TARGET_SSE)
1654             {
1655               warning ("SSE instruction set disabled, using 387 arithmetics");
1656               ix86_fpmath = FPMATH_387;
1657             }
1658           else if (!TARGET_80387)
1659             {
1660               warning ("387 instruction set disabled, using SSE arithmetics");
1661               ix86_fpmath = FPMATH_SSE;
1662             }
1663           else
1664             ix86_fpmath = FPMATH_SSE | FPMATH_387;
1665         }
1666       else
1667         error ("bad value (%s) for -mfpmath= switch", ix86_fpmath_string);
1668     }
1669
1670   /* If the i387 is disabled, then do not return values in it. */
1671   if (!TARGET_80387)
1672     target_flags &= ~MASK_FLOAT_RETURNS;
1673
1674   if ((x86_accumulate_outgoing_args & TUNEMASK)
1675       && !(target_flags_explicit & MASK_ACCUMULATE_OUTGOING_ARGS)
1676       && !optimize_size)
1677     target_flags |= MASK_ACCUMULATE_OUTGOING_ARGS;
1678
1679   /* Figure out what ASM_GENERATE_INTERNAL_LABEL builds as a prefix.  */
1680   {
1681     char *p;
1682     ASM_GENERATE_INTERNAL_LABEL (internal_label_prefix, "LX", 0);
1683     p = strchr (internal_label_prefix, 'X');
1684     internal_label_prefix_len = p - internal_label_prefix;
1685     *p = '\0';
1686   }
1687
1688   /* When scheduling description is not available, disable scheduler pass
1689      so it won't slow down the compilation and make x87 code slower.  */
1690   if (!TARGET_SCHEDULE)
1691     flag_schedule_insns_after_reload = flag_schedule_insns = 0;
1692 }
1693 \f
1694 void
1695 optimization_options (int level, int size ATTRIBUTE_UNUSED)
1696 {
1697   /* For -O2 and beyond, turn off -fschedule-insns by default.  It tends to
1698      make the problem with not enough registers even worse.  */
1699 #ifdef INSN_SCHEDULING
1700   if (level > 1)
1701     flag_schedule_insns = 0;
1702 #endif
1703
1704   /* The default values of these switches depend on the TARGET_64BIT
1705      that is not known at this moment.  Mark these values with 2 and
1706      let user the to override these.  In case there is no command line option
1707      specifying them, we will set the defaults in override_options.  */
1708   if (optimize >= 1)
1709     flag_omit_frame_pointer = 2;
1710   flag_pcc_struct_return = 2;
1711   flag_asynchronous_unwind_tables = 2;
1712 #ifdef SUBTARGET_OPTIMIZATION_OPTIONS
1713   SUBTARGET_OPTIMIZATION_OPTIONS;
1714 #endif
1715 }
1716 \f
1717 /* Table of valid machine attributes.  */
1718 const struct attribute_spec ix86_attribute_table[] =
1719 {
1720   /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler } */
1721   /* Stdcall attribute says callee is responsible for popping arguments
1722      if they are not variable.  */
1723   { "stdcall",   0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1724   /* Fastcall attribute says callee is responsible for popping arguments
1725      if they are not variable.  */
1726   { "fastcall",  0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1727   /* Cdecl attribute says the callee is a normal C declaration */
1728   { "cdecl",     0, 0, false, true,  true,  ix86_handle_cdecl_attribute },
1729   /* Regparm attribute specifies how many integer arguments are to be
1730      passed in registers.  */
1731   { "regparm",   1, 1, false, true,  true,  ix86_handle_regparm_attribute },
1732 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1733   { "dllimport", 0, 0, false, false, false, handle_dll_attribute },
1734   { "dllexport", 0, 0, false, false, false, handle_dll_attribute },
1735   { "shared",    0, 0, true,  false, false, ix86_handle_shared_attribute },
1736 #endif
1737   { "ms_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1738   { "gcc_struct", 0, 0, false, false,  false, ix86_handle_struct_attribute },
1739 #ifdef SUBTARGET_ATTRIBUTE_TABLE
1740   SUBTARGET_ATTRIBUTE_TABLE,
1741 #endif
1742   { NULL,        0, 0, false, false, false, NULL }
1743 };
1744
1745 /* Decide whether we can make a sibling call to a function.  DECL is the
1746    declaration of the function being targeted by the call and EXP is the
1747    CALL_EXPR representing the call.  */
1748
1749 static bool
1750 ix86_function_ok_for_sibcall (tree decl, tree exp)
1751 {
1752   tree func;
1753
1754   /* If we are generating position-independent code, we cannot sibcall
1755      optimize any indirect call, or a direct call to a global function,
1756      as the PLT requires %ebx be live.  */
1757   if (!TARGET_64BIT && flag_pic && (!decl || TREE_PUBLIC (decl)))
1758     return false;
1759
1760   if (decl)
1761     func = decl;
1762   else
1763     func = NULL;
1764
1765   /* If we are returning floats on the 80387 register stack, we cannot
1766      make a sibcall from a function that doesn't return a float to a
1767      function that does or, conversely, from a function that does return
1768      a float to a function that doesn't; the necessary stack adjustment
1769      would not be executed.  */
1770   if (STACK_REG_P (ix86_function_value (TREE_TYPE (exp), func))
1771       != STACK_REG_P (ix86_function_value (TREE_TYPE (DECL_RESULT (cfun->decl)),
1772                                            cfun->decl)))
1773     return false;
1774
1775   /* If this call is indirect, we'll need to be able to use a call-clobbered
1776      register for the address of the target function.  Make sure that all
1777      such registers are not used for passing parameters.  */
1778   if (!decl && !TARGET_64BIT)
1779     {
1780       tree type;
1781
1782       /* We're looking at the CALL_EXPR, we need the type of the function.  */
1783       type = TREE_OPERAND (exp, 0);             /* pointer expression */
1784       type = TREE_TYPE (type);                  /* pointer type */
1785       type = TREE_TYPE (type);                  /* function type */
1786
1787       if (ix86_function_regparm (type, NULL) >= 3)
1788         {
1789           /* ??? Need to count the actual number of registers to be used,
1790              not the possible number of registers.  Fix later.  */
1791           return false;
1792         }
1793     }
1794
1795 #if TARGET_DLLIMPORT_DECL_ATTRIBUTES
1796   /* Dllimport'd functions are also called indirectly.  */
1797   if (decl && lookup_attribute ("dllimport", DECL_ATTRIBUTES (decl))
1798       && ix86_function_regparm (TREE_TYPE (decl), NULL) >= 3)
1799     return false;
1800 #endif
1801
1802   /* Otherwise okay.  That also includes certain types of indirect calls.  */
1803   return true;
1804 }
1805
1806 /* Handle a "cdecl", "stdcall", or "fastcall" attribute;
1807    arguments as in struct attribute_spec.handler.  */
1808 static tree
1809 ix86_handle_cdecl_attribute (tree *node, tree name,
1810                              tree args ATTRIBUTE_UNUSED,
1811                              int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1812 {
1813   if (TREE_CODE (*node) != FUNCTION_TYPE
1814       && TREE_CODE (*node) != METHOD_TYPE
1815       && TREE_CODE (*node) != FIELD_DECL
1816       && TREE_CODE (*node) != TYPE_DECL)
1817     {
1818       warning ("%qs attribute only applies to functions",
1819                IDENTIFIER_POINTER (name));
1820       *no_add_attrs = true;
1821     }
1822   else
1823     {
1824       if (is_attribute_p ("fastcall", name))
1825         {
1826           if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (*node)))
1827             {
1828               error ("fastcall and stdcall attributes are not compatible");
1829             }
1830            else if (lookup_attribute ("regparm", TYPE_ATTRIBUTES (*node)))
1831             {
1832               error ("fastcall and regparm attributes are not compatible");
1833             }
1834         }
1835       else if (is_attribute_p ("stdcall", name))
1836         {
1837           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1838             {
1839               error ("fastcall and stdcall attributes are not compatible");
1840             }
1841         }
1842     }
1843
1844   if (TARGET_64BIT)
1845     {
1846       warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
1847       *no_add_attrs = true;
1848     }
1849
1850   return NULL_TREE;
1851 }
1852
1853 /* Handle a "regparm" attribute;
1854    arguments as in struct attribute_spec.handler.  */
1855 static tree
1856 ix86_handle_regparm_attribute (tree *node, tree name, tree args,
1857                                int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1858 {
1859   if (TREE_CODE (*node) != FUNCTION_TYPE
1860       && TREE_CODE (*node) != METHOD_TYPE
1861       && TREE_CODE (*node) != FIELD_DECL
1862       && TREE_CODE (*node) != TYPE_DECL)
1863     {
1864       warning ("%qs attribute only applies to functions",
1865                IDENTIFIER_POINTER (name));
1866       *no_add_attrs = true;
1867     }
1868   else
1869     {
1870       tree cst;
1871
1872       cst = TREE_VALUE (args);
1873       if (TREE_CODE (cst) != INTEGER_CST)
1874         {
1875           warning ("%qs attribute requires an integer constant argument",
1876                    IDENTIFIER_POINTER (name));
1877           *no_add_attrs = true;
1878         }
1879       else if (compare_tree_int (cst, REGPARM_MAX) > 0)
1880         {
1881           warning ("argument to %qs attribute larger than %d",
1882                    IDENTIFIER_POINTER (name), REGPARM_MAX);
1883           *no_add_attrs = true;
1884         }
1885
1886       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (*node)))
1887         {
1888           error ("fastcall and regparm attributes are not compatible");
1889         }
1890     }
1891
1892   return NULL_TREE;
1893 }
1894
1895 /* Return 0 if the attributes for two types are incompatible, 1 if they
1896    are compatible, and 2 if they are nearly compatible (which causes a
1897    warning to be generated).  */
1898
1899 static int
1900 ix86_comp_type_attributes (tree type1, tree type2)
1901 {
1902   /* Check for mismatch of non-default calling convention.  */
1903   const char *const rtdstr = TARGET_RTD ? "cdecl" : "stdcall";
1904
1905   if (TREE_CODE (type1) != FUNCTION_TYPE)
1906     return 1;
1907
1908   /*  Check for mismatched fastcall types */
1909   if (!lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type1))
1910       != !lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type2)))
1911     return 0;
1912
1913   /* Check for mismatched return types (cdecl vs stdcall).  */
1914   if (!lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type1))
1915       != !lookup_attribute (rtdstr, TYPE_ATTRIBUTES (type2)))
1916     return 0;
1917   if (ix86_function_regparm (type1, NULL)
1918       != ix86_function_regparm (type2, NULL))
1919     return 0;
1920   return 1;
1921 }
1922 \f
1923 /* Return the regparm value for a fuctio with the indicated TYPE and DECL.
1924    DECL may be NULL when calling function indirectly
1925    or considering a libcall.  */
1926
1927 static int
1928 ix86_function_regparm (tree type, tree decl)
1929 {
1930   tree attr;
1931   int regparm = ix86_regparm;
1932   bool user_convention = false;
1933
1934   if (!TARGET_64BIT)
1935     {
1936       attr = lookup_attribute ("regparm", TYPE_ATTRIBUTES (type));
1937       if (attr)
1938         {
1939           regparm = TREE_INT_CST_LOW (TREE_VALUE (TREE_VALUE (attr)));
1940           user_convention = true;
1941         }
1942
1943       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
1944         {
1945           regparm = 2;
1946           user_convention = true;
1947         }
1948
1949       /* Use register calling convention for local functions when possible.  */
1950       if (!TARGET_64BIT && !user_convention && decl
1951           && flag_unit_at_a_time && !profile_flag)
1952         {
1953           struct cgraph_local_info *i = cgraph_local_info (decl);
1954           if (i && i->local)
1955             {
1956               /* We can't use regparm(3) for nested functions as these use
1957                  static chain pointer in third argument.  */
1958               if (DECL_CONTEXT (decl) && !DECL_NO_STATIC_CHAIN (decl))
1959                 regparm = 2;
1960               else
1961                 regparm = 3;
1962             }
1963         }
1964     }
1965   return regparm;
1966 }
1967
1968 /* Return true if EAX is live at the start of the function.  Used by
1969    ix86_expand_prologue to determine if we need special help before
1970    calling allocate_stack_worker.  */
1971
1972 static bool
1973 ix86_eax_live_at_start_p (void)
1974 {
1975   /* Cheat.  Don't bother working forward from ix86_function_regparm
1976      to the function type to whether an actual argument is located in
1977      eax.  Instead just look at cfg info, which is still close enough
1978      to correct at this point.  This gives false positives for broken
1979      functions that might use uninitialized data that happens to be
1980      allocated in eax, but who cares?  */
1981   return REGNO_REG_SET_P (ENTRY_BLOCK_PTR->global_live_at_end, 0);
1982 }
1983
1984 /* Value is the number of bytes of arguments automatically
1985    popped when returning from a subroutine call.
1986    FUNDECL is the declaration node of the function (as a tree),
1987    FUNTYPE is the data type of the function (as a tree),
1988    or for a library call it is an identifier node for the subroutine name.
1989    SIZE is the number of bytes of arguments passed on the stack.
1990
1991    On the 80386, the RTD insn may be used to pop them if the number
1992      of args is fixed, but if the number is variable then the caller
1993      must pop them all.  RTD can't be used for library calls now
1994      because the library is compiled with the Unix compiler.
1995    Use of RTD is a selectable option, since it is incompatible with
1996    standard Unix calling sequences.  If the option is not selected,
1997    the caller must always pop the args.
1998
1999    The attribute stdcall is equivalent to RTD on a per module basis.  */
2000
2001 int
2002 ix86_return_pops_args (tree fundecl, tree funtype, int size)
2003 {
2004   int rtd = TARGET_RTD && (!fundecl || TREE_CODE (fundecl) != IDENTIFIER_NODE);
2005
2006   /* Cdecl functions override -mrtd, and never pop the stack.  */
2007   if (! lookup_attribute ("cdecl", TYPE_ATTRIBUTES (funtype))) {
2008
2009     /* Stdcall and fastcall functions will pop the stack if not
2010        variable args.  */
2011     if (lookup_attribute ("stdcall", TYPE_ATTRIBUTES (funtype))
2012         || lookup_attribute ("fastcall", TYPE_ATTRIBUTES (funtype)))
2013       rtd = 1;
2014
2015     if (rtd
2016         && (TYPE_ARG_TYPES (funtype) == NULL_TREE
2017             || (TREE_VALUE (tree_last (TYPE_ARG_TYPES (funtype)))
2018                 == void_type_node)))
2019       return size;
2020   }
2021
2022   /* Lose any fake structure return argument if it is passed on the stack.  */
2023   if (aggregate_value_p (TREE_TYPE (funtype), fundecl)
2024       && !TARGET_64BIT
2025       && !KEEP_AGGREGATE_RETURN_POINTER)
2026     {
2027       int nregs = ix86_function_regparm (funtype, fundecl);
2028
2029       if (!nregs)
2030         return GET_MODE_SIZE (Pmode);
2031     }
2032
2033   return 0;
2034 }
2035 \f
2036 /* Argument support functions.  */
2037
2038 /* Return true when register may be used to pass function parameters.  */
2039 bool
2040 ix86_function_arg_regno_p (int regno)
2041 {
2042   int i;
2043   if (!TARGET_64BIT)
2044     return (regno < REGPARM_MAX
2045             || (TARGET_SSE && SSE_REGNO_P (regno) && !fixed_regs[regno]));
2046   if (SSE_REGNO_P (regno) && TARGET_SSE)
2047     return true;
2048   /* RAX is used as hidden argument to va_arg functions.  */
2049   if (!regno)
2050     return true;
2051   for (i = 0; i < REGPARM_MAX; i++)
2052     if (regno == x86_64_int_parameter_registers[i])
2053       return true;
2054   return false;
2055 }
2056
2057 /* Return if we do not know how to pass TYPE solely in registers.  */
2058
2059 static bool
2060 ix86_must_pass_in_stack (enum machine_mode mode, tree type)
2061 {
2062   if (must_pass_in_stack_var_size_or_pad (mode, type))
2063     return true;
2064
2065   /* For 32-bit, we want TImode aggregates to go on the stack.  But watch out!
2066      The layout_type routine is crafty and tries to trick us into passing
2067      currently unsupported vector types on the stack by using TImode.  */
2068   return (!TARGET_64BIT && mode == TImode
2069           && type && TREE_CODE (type) != VECTOR_TYPE);
2070 }
2071
2072 /* Initialize a variable CUM of type CUMULATIVE_ARGS
2073    for a call to a function whose data type is FNTYPE.
2074    For a library call, FNTYPE is 0.  */
2075
2076 void
2077 init_cumulative_args (CUMULATIVE_ARGS *cum,  /* Argument info to initialize */
2078                       tree fntype,      /* tree ptr for function decl */
2079                       rtx libname,      /* SYMBOL_REF of library name or 0 */
2080                       tree fndecl)
2081 {
2082   static CUMULATIVE_ARGS zero_cum;
2083   tree param, next_param;
2084
2085   if (TARGET_DEBUG_ARG)
2086     {
2087       fprintf (stderr, "\ninit_cumulative_args (");
2088       if (fntype)
2089         fprintf (stderr, "fntype code = %s, ret code = %s",
2090                  tree_code_name[(int) TREE_CODE (fntype)],
2091                  tree_code_name[(int) TREE_CODE (TREE_TYPE (fntype))]);
2092       else
2093         fprintf (stderr, "no fntype");
2094
2095       if (libname)
2096         fprintf (stderr, ", libname = %s", XSTR (libname, 0));
2097     }
2098
2099   *cum = zero_cum;
2100
2101   /* Set up the number of registers to use for passing arguments.  */
2102   if (fntype)
2103     cum->nregs = ix86_function_regparm (fntype, fndecl);
2104   else
2105     cum->nregs = ix86_regparm;
2106   if (TARGET_SSE)
2107     cum->sse_nregs = SSE_REGPARM_MAX;
2108   if (TARGET_MMX)
2109     cum->mmx_nregs = MMX_REGPARM_MAX;
2110   cum->warn_sse = true;
2111   cum->warn_mmx = true;
2112   cum->maybe_vaarg = false;
2113
2114   /* Use ecx and edx registers if function has fastcall attribute */
2115   if (fntype && !TARGET_64BIT)
2116     {
2117       if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (fntype)))
2118         {
2119           cum->nregs = 2;
2120           cum->fastcall = 1;
2121         }
2122     }
2123
2124   /* Determine if this function has variable arguments.  This is
2125      indicated by the last argument being 'void_type_mode' if there
2126      are no variable arguments.  If there are variable arguments, then
2127      we won't pass anything in registers in 32-bit mode. */
2128
2129   if (cum->nregs || cum->mmx_nregs || cum->sse_nregs)
2130     {
2131       for (param = (fntype) ? TYPE_ARG_TYPES (fntype) : 0;
2132            param != 0; param = next_param)
2133         {
2134           next_param = TREE_CHAIN (param);
2135           if (next_param == 0 && TREE_VALUE (param) != void_type_node)
2136             {
2137               if (!TARGET_64BIT)
2138                 {
2139                   cum->nregs = 0;
2140                   cum->sse_nregs = 0;
2141                   cum->mmx_nregs = 0;
2142                   cum->warn_sse = 0;
2143                   cum->warn_mmx = 0;
2144                   cum->fastcall = 0;
2145                 }
2146               cum->maybe_vaarg = true;
2147             }
2148         }
2149     }
2150   if ((!fntype && !libname)
2151       || (fntype && !TYPE_ARG_TYPES (fntype)))
2152     cum->maybe_vaarg = true;
2153
2154   /* For local functions, pass SFmode (and DFmode for SSE2) arguments
2155      in SSE registers even for 32-bit mode and not just 3, but up to
2156      8 SSE arguments in registers.  */
2157   if (!TARGET_64BIT && !cum->maybe_vaarg && !cum->fastcall
2158       && cum->sse_nregs == SSE_REGPARM_MAX && fndecl
2159       && TARGET_SSE_MATH && flag_unit_at_a_time && !profile_flag)
2160     {
2161       struct cgraph_local_info *i = cgraph_local_info (fndecl);
2162       if (i && i->local)
2163         {
2164           cum->sse_nregs = 8;
2165           cum->float_in_sse = true;
2166         }
2167     }
2168
2169   if (TARGET_DEBUG_ARG)
2170     fprintf (stderr, ", nregs=%d )\n", cum->nregs);
2171
2172   return;
2173 }
2174
2175 /* Return the "natural" mode for TYPE.  In most cases, this is just TYPE_MODE.
2176    But in the case of vector types, it is some vector mode.
2177
2178    When we have only some of our vector isa extensions enabled, then there
2179    are some modes for which vector_mode_supported_p is false.  For these
2180    modes, the generic vector support in gcc will choose some non-vector mode
2181    in order to implement the type.  By computing the natural mode, we'll 
2182    select the proper ABI location for the operand and not depend on whatever
2183    the middle-end decides to do with these vector types.  */
2184
2185 static enum machine_mode
2186 type_natural_mode (tree type)
2187 {
2188   enum machine_mode mode = TYPE_MODE (type);
2189
2190   if (TREE_CODE (type) == VECTOR_TYPE && !VECTOR_MODE_P (mode))
2191     {
2192       HOST_WIDE_INT size = int_size_in_bytes (type);
2193       if ((size == 8 || size == 16)
2194           /* ??? Generic code allows us to create width 1 vectors.  Ignore.  */
2195           && TYPE_VECTOR_SUBPARTS (type) > 1)
2196         {
2197           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (type));
2198
2199           if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
2200             mode = MIN_MODE_VECTOR_FLOAT;
2201           else
2202             mode = MIN_MODE_VECTOR_INT;
2203
2204           /* Get the mode which has this inner mode and number of units.  */
2205           for (; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2206             if (GET_MODE_NUNITS (mode) == TYPE_VECTOR_SUBPARTS (type)
2207                 && GET_MODE_INNER (mode) == innermode)
2208               return mode;
2209
2210           gcc_unreachable ();
2211         }
2212     }
2213
2214   return mode;
2215 }
2216
2217 /* We want to pass a value in REGNO whose "natural" mode is MODE.  However,
2218    this may not agree with the mode that the type system has chosen for the
2219    register, which is ORIG_MODE.  If ORIG_MODE is not BLKmode, then we can
2220    go ahead and use it.  Otherwise we have to build a PARALLEL instead.  */
2221
2222 static rtx
2223 gen_reg_or_parallel (enum machine_mode mode, enum machine_mode orig_mode,
2224                      unsigned int regno)
2225 {
2226   rtx tmp;
2227
2228   if (orig_mode != BLKmode)
2229     tmp = gen_rtx_REG (orig_mode, regno);
2230   else
2231     {
2232       tmp = gen_rtx_REG (mode, regno);
2233       tmp = gen_rtx_EXPR_LIST (VOIDmode, tmp, const0_rtx);
2234       tmp = gen_rtx_PARALLEL (orig_mode, gen_rtvec (1, tmp));
2235     }
2236
2237   return tmp;
2238 }
2239
2240 /* x86-64 register passing implementation.  See x86-64 ABI for details.  Goal
2241    of this code is to classify each 8bytes of incoming argument by the register
2242    class and assign registers accordingly.  */
2243
2244 /* Return the union class of CLASS1 and CLASS2.
2245    See the x86-64 PS ABI for details.  */
2246
2247 static enum x86_64_reg_class
2248 merge_classes (enum x86_64_reg_class class1, enum x86_64_reg_class class2)
2249 {
2250   /* Rule #1: If both classes are equal, this is the resulting class.  */
2251   if (class1 == class2)
2252     return class1;
2253
2254   /* Rule #2: If one of the classes is NO_CLASS, the resulting class is
2255      the other class.  */
2256   if (class1 == X86_64_NO_CLASS)
2257     return class2;
2258   if (class2 == X86_64_NO_CLASS)
2259     return class1;
2260
2261   /* Rule #3: If one of the classes is MEMORY, the result is MEMORY.  */
2262   if (class1 == X86_64_MEMORY_CLASS || class2 == X86_64_MEMORY_CLASS)
2263     return X86_64_MEMORY_CLASS;
2264
2265   /* Rule #4: If one of the classes is INTEGER, the result is INTEGER.  */
2266   if ((class1 == X86_64_INTEGERSI_CLASS && class2 == X86_64_SSESF_CLASS)
2267       || (class2 == X86_64_INTEGERSI_CLASS && class1 == X86_64_SSESF_CLASS))
2268     return X86_64_INTEGERSI_CLASS;
2269   if (class1 == X86_64_INTEGER_CLASS || class1 == X86_64_INTEGERSI_CLASS
2270       || class2 == X86_64_INTEGER_CLASS || class2 == X86_64_INTEGERSI_CLASS)
2271     return X86_64_INTEGER_CLASS;
2272
2273   /* Rule #5: If one of the classes is X87, X87UP, or COMPLEX_X87 class,
2274      MEMORY is used.  */
2275   if (class1 == X86_64_X87_CLASS
2276       || class1 == X86_64_X87UP_CLASS
2277       || class1 == X86_64_COMPLEX_X87_CLASS
2278       || class2 == X86_64_X87_CLASS
2279       || class2 == X86_64_X87UP_CLASS
2280       || class2 == X86_64_COMPLEX_X87_CLASS)
2281     return X86_64_MEMORY_CLASS;
2282
2283   /* Rule #6: Otherwise class SSE is used.  */
2284   return X86_64_SSE_CLASS;
2285 }
2286
2287 /* Classify the argument of type TYPE and mode MODE.
2288    CLASSES will be filled by the register class used to pass each word
2289    of the operand.  The number of words is returned.  In case the parameter
2290    should be passed in memory, 0 is returned. As a special case for zero
2291    sized containers, classes[0] will be NO_CLASS and 1 is returned.
2292
2293    BIT_OFFSET is used internally for handling records and specifies offset
2294    of the offset in bits modulo 256 to avoid overflow cases.
2295
2296    See the x86-64 PS ABI for details.
2297 */
2298
2299 static int
2300 classify_argument (enum machine_mode mode, tree type,
2301                    enum x86_64_reg_class classes[MAX_CLASSES], int bit_offset)
2302 {
2303   HOST_WIDE_INT bytes =
2304     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2305   int words = (bytes + (bit_offset % 64) / 8 + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2306
2307   /* Variable sized entities are always passed/returned in memory.  */
2308   if (bytes < 0)
2309     return 0;
2310
2311   if (mode != VOIDmode
2312       && targetm.calls.must_pass_in_stack (mode, type))
2313     return 0;
2314
2315   if (type && AGGREGATE_TYPE_P (type))
2316     {
2317       int i;
2318       tree field;
2319       enum x86_64_reg_class subclasses[MAX_CLASSES];
2320
2321       /* On x86-64 we pass structures larger than 16 bytes on the stack.  */
2322       if (bytes > 16)
2323         return 0;
2324
2325       for (i = 0; i < words; i++)
2326         classes[i] = X86_64_NO_CLASS;
2327
2328       /* Zero sized arrays or structures are NO_CLASS.  We return 0 to
2329          signalize memory class, so handle it as special case.  */
2330       if (!words)
2331         {
2332           classes[0] = X86_64_NO_CLASS;
2333           return 1;
2334         }
2335
2336       /* Classify each field of record and merge classes.  */
2337       switch (TREE_CODE (type))
2338         {
2339         case RECORD_TYPE:
2340           /* For classes first merge in the field of the subclasses.  */
2341           if (TYPE_BINFO (type))
2342             {
2343               tree binfo, base_binfo;
2344               int basenum;
2345
2346               for (binfo = TYPE_BINFO (type), basenum = 0;
2347                    BINFO_BASE_ITERATE (binfo, basenum, base_binfo); basenum++)
2348                 {
2349                    int num;
2350                    int offset = tree_low_cst (BINFO_OFFSET (base_binfo), 0) * 8;
2351                    tree type = BINFO_TYPE (base_binfo);
2352
2353                    num = classify_argument (TYPE_MODE (type),
2354                                             type, subclasses,
2355                                             (offset + bit_offset) % 256);
2356                    if (!num)
2357                      return 0;
2358                    for (i = 0; i < num; i++)
2359                      {
2360                        int pos = (offset + (bit_offset % 64)) / 8 / 8;
2361                        classes[i + pos] =
2362                          merge_classes (subclasses[i], classes[i + pos]);
2363                      }
2364                 }
2365             }
2366           /* And now merge the fields of structure.  */
2367           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2368             {
2369               if (TREE_CODE (field) == FIELD_DECL)
2370                 {
2371                   int num;
2372
2373                   /* Bitfields are always classified as integer.  Handle them
2374                      early, since later code would consider them to be
2375                      misaligned integers.  */
2376                   if (DECL_BIT_FIELD (field))
2377                     {
2378                       for (i = int_bit_position (field) / 8 / 8;
2379                            i < (int_bit_position (field)
2380                                 + tree_low_cst (DECL_SIZE (field), 0)
2381                                 + 63) / 8 / 8; i++)
2382                         classes[i] =
2383                           merge_classes (X86_64_INTEGER_CLASS,
2384                                          classes[i]);
2385                     }
2386                   else
2387                     {
2388                       num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2389                                                TREE_TYPE (field), subclasses,
2390                                                (int_bit_position (field)
2391                                                 + bit_offset) % 256);
2392                       if (!num)
2393                         return 0;
2394                       for (i = 0; i < num; i++)
2395                         {
2396                           int pos =
2397                             (int_bit_position (field) + (bit_offset % 64)) / 8 / 8;
2398                           classes[i + pos] =
2399                             merge_classes (subclasses[i], classes[i + pos]);
2400                         }
2401                     }
2402                 }
2403             }
2404           break;
2405
2406         case ARRAY_TYPE:
2407           /* Arrays are handled as small records.  */
2408           {
2409             int num;
2410             num = classify_argument (TYPE_MODE (TREE_TYPE (type)),
2411                                      TREE_TYPE (type), subclasses, bit_offset);
2412             if (!num)
2413               return 0;
2414
2415             /* The partial classes are now full classes.  */
2416             if (subclasses[0] == X86_64_SSESF_CLASS && bytes != 4)
2417               subclasses[0] = X86_64_SSE_CLASS;
2418             if (subclasses[0] == X86_64_INTEGERSI_CLASS && bytes != 4)
2419               subclasses[0] = X86_64_INTEGER_CLASS;
2420             
2421             for (i = 0; i < words; i++)
2422               classes[i] = subclasses[i % num];
2423             
2424             break;
2425           }
2426         case UNION_TYPE:
2427         case QUAL_UNION_TYPE:
2428           /* Unions are similar to RECORD_TYPE but offset is always 0.
2429              */
2430
2431           /* Unions are not derived.  */
2432           gcc_assert (!TYPE_BINFO (type)
2433                       || !BINFO_N_BASE_BINFOS (TYPE_BINFO (type)));
2434           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
2435             {
2436               if (TREE_CODE (field) == FIELD_DECL)
2437                 {
2438                   int num;
2439                   num = classify_argument (TYPE_MODE (TREE_TYPE (field)),
2440                                            TREE_TYPE (field), subclasses,
2441                                            bit_offset);
2442                   if (!num)
2443                     return 0;
2444                   for (i = 0; i < num; i++)
2445                     classes[i] = merge_classes (subclasses[i], classes[i]);
2446                 }
2447             }
2448           break;
2449
2450         default:
2451           gcc_unreachable ();
2452         }
2453
2454       /* Final merger cleanup.  */
2455       for (i = 0; i < words; i++)
2456         {
2457           /* If one class is MEMORY, everything should be passed in
2458              memory.  */
2459           if (classes[i] == X86_64_MEMORY_CLASS)
2460             return 0;
2461
2462           /* The X86_64_SSEUP_CLASS should be always preceded by
2463              X86_64_SSE_CLASS.  */
2464           if (classes[i] == X86_64_SSEUP_CLASS
2465               && (i == 0 || classes[i - 1] != X86_64_SSE_CLASS))
2466             classes[i] = X86_64_SSE_CLASS;
2467
2468           /*  X86_64_X87UP_CLASS should be preceded by X86_64_X87_CLASS.  */
2469           if (classes[i] == X86_64_X87UP_CLASS
2470               && (i == 0 || classes[i - 1] != X86_64_X87_CLASS))
2471             classes[i] = X86_64_SSE_CLASS;
2472         }
2473       return words;
2474     }
2475
2476   /* Compute alignment needed.  We align all types to natural boundaries with
2477      exception of XFmode that is aligned to 64bits.  */
2478   if (mode != VOIDmode && mode != BLKmode)
2479     {
2480       int mode_alignment = GET_MODE_BITSIZE (mode);
2481
2482       if (mode == XFmode)
2483         mode_alignment = 128;
2484       else if (mode == XCmode)
2485         mode_alignment = 256;
2486       if (COMPLEX_MODE_P (mode))
2487         mode_alignment /= 2;
2488       /* Misaligned fields are always returned in memory.  */
2489       if (bit_offset % mode_alignment)
2490         return 0;
2491     }
2492
2493   /* for V1xx modes, just use the base mode */
2494   if (VECTOR_MODE_P (mode)
2495       && GET_MODE_SIZE (GET_MODE_INNER (mode)) == bytes)
2496     mode = GET_MODE_INNER (mode);
2497
2498   /* Classification of atomic types.  */
2499   switch (mode)
2500     {
2501     case DImode:
2502     case SImode:
2503     case HImode:
2504     case QImode:
2505     case CSImode:
2506     case CHImode:
2507     case CQImode:
2508       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2509         classes[0] = X86_64_INTEGERSI_CLASS;
2510       else
2511         classes[0] = X86_64_INTEGER_CLASS;
2512       return 1;
2513     case CDImode:
2514     case TImode:
2515       classes[0] = classes[1] = X86_64_INTEGER_CLASS;
2516       return 2;
2517     case CTImode:
2518       return 0;
2519     case SFmode:
2520       if (!(bit_offset % 64))
2521         classes[0] = X86_64_SSESF_CLASS;
2522       else
2523         classes[0] = X86_64_SSE_CLASS;
2524       return 1;
2525     case DFmode:
2526       classes[0] = X86_64_SSEDF_CLASS;
2527       return 1;
2528     case XFmode:
2529       classes[0] = X86_64_X87_CLASS;
2530       classes[1] = X86_64_X87UP_CLASS;
2531       return 2;
2532     case TFmode:
2533       classes[0] = X86_64_SSE_CLASS;
2534       classes[1] = X86_64_SSEUP_CLASS;
2535       return 2;
2536     case SCmode:
2537       classes[0] = X86_64_SSE_CLASS;
2538       return 1;
2539     case DCmode:
2540       classes[0] = X86_64_SSEDF_CLASS;
2541       classes[1] = X86_64_SSEDF_CLASS;
2542       return 2;
2543     case XCmode:
2544       classes[0] = X86_64_COMPLEX_X87_CLASS;
2545       return 1;
2546     case TCmode:
2547       /* This modes is larger than 16 bytes.  */
2548       return 0;
2549     case V4SFmode:
2550     case V4SImode:
2551     case V16QImode:
2552     case V8HImode:
2553     case V2DFmode:
2554     case V2DImode:
2555       classes[0] = X86_64_SSE_CLASS;
2556       classes[1] = X86_64_SSEUP_CLASS;
2557       return 2;
2558     case V2SFmode:
2559     case V2SImode:
2560     case V4HImode:
2561     case V8QImode:
2562       classes[0] = X86_64_SSE_CLASS;
2563       return 1;
2564     case BLKmode:
2565     case VOIDmode:
2566       return 0;
2567     default:
2568       gcc_assert (VECTOR_MODE_P (mode));
2569       
2570       if (bytes > 16)
2571         return 0;
2572       
2573       gcc_assert (GET_MODE_CLASS (GET_MODE_INNER (mode)) == MODE_INT);
2574       
2575       if (bit_offset + GET_MODE_BITSIZE (mode) <= 32)
2576         classes[0] = X86_64_INTEGERSI_CLASS;
2577       else
2578         classes[0] = X86_64_INTEGER_CLASS;
2579       classes[1] = X86_64_INTEGER_CLASS;
2580       return 1 + (bytes > 8);
2581     }
2582 }
2583
2584 /* Examine the argument and return set number of register required in each
2585    class.  Return 0 iff parameter should be passed in memory.  */
2586 static int
2587 examine_argument (enum machine_mode mode, tree type, int in_return,
2588                   int *int_nregs, int *sse_nregs)
2589 {
2590   enum x86_64_reg_class class[MAX_CLASSES];
2591   int n = classify_argument (mode, type, class, 0);
2592
2593   *int_nregs = 0;
2594   *sse_nregs = 0;
2595   if (!n)
2596     return 0;
2597   for (n--; n >= 0; n--)
2598     switch (class[n])
2599       {
2600       case X86_64_INTEGER_CLASS:
2601       case X86_64_INTEGERSI_CLASS:
2602         (*int_nregs)++;
2603         break;
2604       case X86_64_SSE_CLASS:
2605       case X86_64_SSESF_CLASS:
2606       case X86_64_SSEDF_CLASS:
2607         (*sse_nregs)++;
2608         break;
2609       case X86_64_NO_CLASS:
2610       case X86_64_SSEUP_CLASS:
2611         break;
2612       case X86_64_X87_CLASS:
2613       case X86_64_X87UP_CLASS:
2614         if (!in_return)
2615           return 0;
2616         break;
2617       case X86_64_COMPLEX_X87_CLASS:
2618         return in_return ? 2 : 0;
2619       case X86_64_MEMORY_CLASS:
2620         gcc_unreachable ();
2621       }
2622   return 1;
2623 }
2624
2625 /* Construct container for the argument used by GCC interface.  See
2626    FUNCTION_ARG for the detailed description.  */
2627
2628 static rtx
2629 construct_container (enum machine_mode mode, enum machine_mode orig_mode,
2630                      tree type, int in_return, int nintregs, int nsseregs,
2631                      const int *intreg, int sse_regno)
2632 {
2633   enum machine_mode tmpmode;
2634   int bytes =
2635     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2636   enum x86_64_reg_class class[MAX_CLASSES];
2637   int n;
2638   int i;
2639   int nexps = 0;
2640   int needed_sseregs, needed_intregs;
2641   rtx exp[MAX_CLASSES];
2642   rtx ret;
2643
2644   n = classify_argument (mode, type, class, 0);
2645   if (TARGET_DEBUG_ARG)
2646     {
2647       if (!n)
2648         fprintf (stderr, "Memory class\n");
2649       else
2650         {
2651           fprintf (stderr, "Classes:");
2652           for (i = 0; i < n; i++)
2653             {
2654               fprintf (stderr, " %s", x86_64_reg_class_name[class[i]]);
2655             }
2656            fprintf (stderr, "\n");
2657         }
2658     }
2659   if (!n)
2660     return NULL;
2661   if (!examine_argument (mode, type, in_return, &needed_intregs,
2662                          &needed_sseregs))
2663     return NULL;
2664   if (needed_intregs > nintregs || needed_sseregs > nsseregs)
2665     return NULL;
2666
2667   /* We allowed the user to turn off SSE for kernel mode.  Don't crash if
2668      some less clueful developer tries to use floating-point anyway.  */
2669   if (needed_sseregs && !TARGET_SSE)
2670     {
2671       static bool issued_error;
2672       if (!issued_error)
2673         {
2674           issued_error = true;
2675           if (in_return)
2676             error ("SSE register return with SSE disabled");
2677           else
2678             error ("SSE register argument with SSE disabled");
2679         }
2680       return NULL;
2681     }
2682
2683   /* First construct simple cases.  Avoid SCmode, since we want to use
2684      single register to pass this type.  */
2685   if (n == 1 && mode != SCmode)
2686     switch (class[0])
2687       {
2688       case X86_64_INTEGER_CLASS:
2689       case X86_64_INTEGERSI_CLASS:
2690         return gen_rtx_REG (mode, intreg[0]);
2691       case X86_64_SSE_CLASS:
2692       case X86_64_SSESF_CLASS:
2693       case X86_64_SSEDF_CLASS:
2694         return gen_reg_or_parallel (mode, orig_mode, SSE_REGNO (sse_regno));
2695       case X86_64_X87_CLASS:
2696       case X86_64_COMPLEX_X87_CLASS:
2697         return gen_rtx_REG (mode, FIRST_STACK_REG);
2698       case X86_64_NO_CLASS:
2699         /* Zero sized array, struct or class.  */
2700         return NULL;
2701       default:
2702         gcc_unreachable ();
2703       }
2704   if (n == 2 && class[0] == X86_64_SSE_CLASS && class[1] == X86_64_SSEUP_CLASS
2705       && mode != BLKmode)
2706     return gen_rtx_REG (mode, SSE_REGNO (sse_regno));
2707   if (n == 2
2708       && class[0] == X86_64_X87_CLASS && class[1] == X86_64_X87UP_CLASS)
2709     return gen_rtx_REG (XFmode, FIRST_STACK_REG);
2710   if (n == 2 && class[0] == X86_64_INTEGER_CLASS
2711       && class[1] == X86_64_INTEGER_CLASS
2712       && (mode == CDImode || mode == TImode || mode == TFmode)
2713       && intreg[0] + 1 == intreg[1])
2714     return gen_rtx_REG (mode, intreg[0]);
2715
2716   /* Otherwise figure out the entries of the PARALLEL.  */
2717   for (i = 0; i < n; i++)
2718     {
2719       switch (class[i])
2720         {
2721           case X86_64_NO_CLASS:
2722             break;
2723           case X86_64_INTEGER_CLASS:
2724           case X86_64_INTEGERSI_CLASS:
2725             /* Merge TImodes on aligned occasions here too.  */
2726             if (i * 8 + 8 > bytes)
2727               tmpmode = mode_for_size ((bytes - i * 8) * BITS_PER_UNIT, MODE_INT, 0);
2728             else if (class[i] == X86_64_INTEGERSI_CLASS)
2729               tmpmode = SImode;
2730             else
2731               tmpmode = DImode;
2732             /* We've requested 24 bytes we don't have mode for.  Use DImode.  */
2733             if (tmpmode == BLKmode)
2734               tmpmode = DImode;
2735             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2736                                                gen_rtx_REG (tmpmode, *intreg),
2737                                                GEN_INT (i*8));
2738             intreg++;
2739             break;
2740           case X86_64_SSESF_CLASS:
2741             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2742                                                gen_rtx_REG (SFmode,
2743                                                             SSE_REGNO (sse_regno)),
2744                                                GEN_INT (i*8));
2745             sse_regno++;
2746             break;
2747           case X86_64_SSEDF_CLASS:
2748             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2749                                                gen_rtx_REG (DFmode,
2750                                                             SSE_REGNO (sse_regno)),
2751                                                GEN_INT (i*8));
2752             sse_regno++;
2753             break;
2754           case X86_64_SSE_CLASS:
2755             if (i < n - 1 && class[i + 1] == X86_64_SSEUP_CLASS)
2756               tmpmode = TImode;
2757             else
2758               tmpmode = DImode;
2759             exp [nexps++] = gen_rtx_EXPR_LIST (VOIDmode,
2760                                                gen_rtx_REG (tmpmode,
2761                                                             SSE_REGNO (sse_regno)),
2762                                                GEN_INT (i*8));
2763             if (tmpmode == TImode)
2764               i++;
2765             sse_regno++;
2766             break;
2767           default:
2768             gcc_unreachable ();
2769         }
2770     }
2771
2772   /* Empty aligned struct, union or class.  */
2773   if (nexps == 0)
2774     return NULL;
2775
2776   ret =  gen_rtx_PARALLEL (mode, rtvec_alloc (nexps));
2777   for (i = 0; i < nexps; i++)
2778     XVECEXP (ret, 0, i) = exp [i];
2779   return ret;
2780 }
2781
2782 /* Update the data in CUM to advance over an argument
2783    of mode MODE and data type TYPE.
2784    (TYPE is null for libcalls where that information may not be available.)  */
2785
2786 void
2787 function_arg_advance (CUMULATIVE_ARGS *cum, enum machine_mode mode,
2788                       tree type, int named)
2789 {
2790   int bytes =
2791     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2792   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2793
2794   if (type)
2795     mode = type_natural_mode (type);
2796
2797   if (TARGET_DEBUG_ARG)
2798     fprintf (stderr, "function_adv (sz=%d, wds=%2d, nregs=%d, ssenregs=%d, "
2799              "mode=%s, named=%d)\n\n",
2800              words, cum->words, cum->nregs, cum->sse_nregs,
2801              GET_MODE_NAME (mode), named);
2802
2803   if (TARGET_64BIT)
2804     {
2805       int int_nregs, sse_nregs;
2806       if (!examine_argument (mode, type, 0, &int_nregs, &sse_nregs))
2807         cum->words += words;
2808       else if (sse_nregs <= cum->sse_nregs && int_nregs <= cum->nregs)
2809         {
2810           cum->nregs -= int_nregs;
2811           cum->sse_nregs -= sse_nregs;
2812           cum->regno += int_nregs;
2813           cum->sse_regno += sse_nregs;
2814         }
2815       else
2816         cum->words += words;
2817     }
2818   else
2819     {
2820       switch (mode)
2821         {
2822         default:
2823           break;
2824
2825         case BLKmode:
2826           if (bytes < 0)
2827             break;
2828           /* FALLTHRU */
2829
2830         case DImode:
2831         case SImode:
2832         case HImode:
2833         case QImode:
2834           cum->words += words;
2835           cum->nregs -= words;
2836           cum->regno += words;
2837
2838           if (cum->nregs <= 0)
2839             {
2840               cum->nregs = 0;
2841               cum->regno = 0;
2842             }
2843           break;
2844
2845         case DFmode:
2846           if (!TARGET_SSE2)
2847             break;
2848         case SFmode:
2849           if (!cum->float_in_sse)
2850             break;
2851           /* FALLTHRU */
2852
2853         case TImode:
2854         case V16QImode:
2855         case V8HImode:
2856         case V4SImode:
2857         case V2DImode:
2858         case V4SFmode:
2859         case V2DFmode:
2860           if (!type || !AGGREGATE_TYPE_P (type))
2861             {
2862               cum->sse_words += words;
2863               cum->sse_nregs -= 1;
2864               cum->sse_regno += 1;
2865               if (cum->sse_nregs <= 0)
2866                 {
2867                   cum->sse_nregs = 0;
2868                   cum->sse_regno = 0;
2869                 }
2870             }
2871           break;
2872
2873         case V8QImode:
2874         case V4HImode:
2875         case V2SImode:
2876         case V2SFmode:
2877           if (!type || !AGGREGATE_TYPE_P (type))
2878             {
2879               cum->mmx_words += words;
2880               cum->mmx_nregs -= 1;
2881               cum->mmx_regno += 1;
2882               if (cum->mmx_nregs <= 0)
2883                 {
2884                   cum->mmx_nregs = 0;
2885                   cum->mmx_regno = 0;
2886                 }
2887             }
2888           break;
2889         }
2890     }
2891 }
2892
2893 /* Define where to put the arguments to a function.
2894    Value is zero to push the argument on the stack,
2895    or a hard register in which to store the argument.
2896
2897    MODE is the argument's machine mode.
2898    TYPE is the data type of the argument (as a tree).
2899     This is null for libcalls where that information may
2900     not be available.
2901    CUM is a variable of type CUMULATIVE_ARGS which gives info about
2902     the preceding args and about the function being called.
2903    NAMED is nonzero if this argument is a named parameter
2904     (otherwise it is an extra parameter matching an ellipsis).  */
2905
2906 rtx
2907 function_arg (CUMULATIVE_ARGS *cum, enum machine_mode orig_mode,
2908               tree type, int named)
2909 {
2910   enum machine_mode mode = orig_mode;
2911   rtx ret = NULL_RTX;
2912   int bytes =
2913     (mode == BLKmode) ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode);
2914   int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2915   static bool warnedsse, warnedmmx;
2916
2917   /* To simplify the code below, represent vector types with a vector mode
2918      even if MMX/SSE are not active.  */
2919   if (type && TREE_CODE (type) == VECTOR_TYPE)
2920     mode = type_natural_mode (type);
2921
2922   /* Handle a hidden AL argument containing number of registers for varargs
2923      x86-64 functions.  For i386 ABI just return constm1_rtx to avoid
2924      any AL settings.  */
2925   if (mode == VOIDmode)
2926     {
2927       if (TARGET_64BIT)
2928         return GEN_INT (cum->maybe_vaarg
2929                         ? (cum->sse_nregs < 0
2930                            ? SSE_REGPARM_MAX
2931                            : cum->sse_regno)
2932                         : -1);
2933       else
2934         return constm1_rtx;
2935     }
2936   if (TARGET_64BIT)
2937     ret = construct_container (mode, orig_mode, type, 0, cum->nregs,
2938                                cum->sse_nregs,
2939                                &x86_64_int_parameter_registers [cum->regno],
2940                                cum->sse_regno);
2941   else
2942     switch (mode)
2943       {
2944         /* For now, pass fp/complex values on the stack.  */
2945       default:
2946         break;
2947
2948       case BLKmode:
2949         if (bytes < 0)
2950           break;
2951         /* FALLTHRU */
2952       case DImode:
2953       case SImode:
2954       case HImode:
2955       case QImode:
2956         if (words <= cum->nregs)
2957           {
2958             int regno = cum->regno;
2959
2960             /* Fastcall allocates the first two DWORD (SImode) or
2961                smaller arguments to ECX and EDX.  */
2962             if (cum->fastcall)
2963               {
2964                 if (mode == BLKmode || mode == DImode)
2965                   break;
2966
2967                 /* ECX not EAX is the first allocated register.  */
2968                 if (regno == 0)
2969                   regno = 2;
2970               }
2971             ret = gen_rtx_REG (mode, regno);
2972           }
2973         break;
2974       case DFmode:
2975         if (!TARGET_SSE2)
2976           break;
2977       case SFmode:
2978         if (!cum->float_in_sse)
2979           break;
2980         /* FALLTHRU */
2981       case TImode:
2982       case V16QImode:
2983       case V8HImode:
2984       case V4SImode:
2985       case V2DImode:
2986       case V4SFmode:
2987       case V2DFmode:
2988         if (!type || !AGGREGATE_TYPE_P (type))
2989           {
2990             if (!TARGET_SSE && !warnedsse && cum->warn_sse)
2991               {
2992                 warnedsse = true;
2993                 warning ("SSE vector argument without SSE enabled "
2994                          "changes the ABI");
2995               }
2996             if (cum->sse_nregs)
2997               ret = gen_reg_or_parallel (mode, orig_mode,
2998                                          cum->sse_regno + FIRST_SSE_REG);
2999           }
3000         break;
3001       case V8QImode:
3002       case V4HImode:
3003       case V2SImode:
3004       case V2SFmode:
3005         if (!type || !AGGREGATE_TYPE_P (type))
3006           {
3007             if (!TARGET_MMX && !warnedmmx && cum->warn_mmx)
3008               {
3009                 warnedmmx = true;
3010                 warning ("MMX vector argument without MMX enabled "
3011                          "changes the ABI");
3012               }
3013             if (cum->mmx_nregs)
3014               ret = gen_reg_or_parallel (mode, orig_mode,
3015                                          cum->mmx_regno + FIRST_MMX_REG);
3016           }
3017         break;
3018       }
3019
3020   if (TARGET_DEBUG_ARG)
3021     {
3022       fprintf (stderr,
3023                "function_arg (size=%d, wds=%2d, nregs=%d, mode=%4s, named=%d, ",
3024                words, cum->words, cum->nregs, GET_MODE_NAME (mode), named);
3025
3026       if (ret)
3027         print_simple_rtl (stderr, ret);
3028       else
3029         fprintf (stderr, ", stack");
3030
3031       fprintf (stderr, " )\n");
3032     }
3033
3034   return ret;
3035 }
3036
3037 /* A C expression that indicates when an argument must be passed by
3038    reference.  If nonzero for an argument, a copy of that argument is
3039    made in memory and a pointer to the argument is passed instead of
3040    the argument itself.  The pointer is passed in whatever way is
3041    appropriate for passing a pointer to that type.  */
3042
3043 static bool
3044 ix86_pass_by_reference (CUMULATIVE_ARGS *cum ATTRIBUTE_UNUSED,
3045                         enum machine_mode mode ATTRIBUTE_UNUSED,
3046                         tree type, bool named ATTRIBUTE_UNUSED)
3047 {
3048   if (!TARGET_64BIT)
3049     return 0;
3050
3051   if (type && int_size_in_bytes (type) == -1)
3052     {
3053       if (TARGET_DEBUG_ARG)
3054         fprintf (stderr, "function_arg_pass_by_reference\n");
3055       return 1;
3056     }
3057
3058   return 0;
3059 }
3060
3061 /* Return true when TYPE should be 128bit aligned for 32bit argument passing
3062    ABI.  Only called if TARGET_SSE.  */
3063 static bool
3064 contains_128bit_aligned_vector_p (tree type)
3065 {
3066   enum machine_mode mode = TYPE_MODE (type);
3067   if (SSE_REG_MODE_P (mode)
3068       && (!TYPE_USER_ALIGN (type) || TYPE_ALIGN (type) > 128))
3069     return true;
3070   if (TYPE_ALIGN (type) < 128)
3071     return false;
3072
3073   if (AGGREGATE_TYPE_P (type))
3074     {
3075       /* Walk the aggregates recursively.  */
3076       switch (TREE_CODE (type))
3077         {
3078         case RECORD_TYPE:
3079         case UNION_TYPE:
3080         case QUAL_UNION_TYPE:
3081           {
3082             tree field;
3083             
3084             if (TYPE_BINFO (type))
3085               {
3086                 tree binfo, base_binfo;
3087                 int i;
3088                 
3089                 for (binfo = TYPE_BINFO (type), i = 0;
3090                      BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
3091                   if (contains_128bit_aligned_vector_p
3092                       (BINFO_TYPE (base_binfo)))
3093                     return true;
3094               }
3095             /* And now merge the fields of structure.  */
3096             for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3097               {
3098                 if (TREE_CODE (field) == FIELD_DECL
3099                     && contains_128bit_aligned_vector_p (TREE_TYPE (field)))
3100                   return true;
3101               }
3102             break;
3103           }
3104
3105         case ARRAY_TYPE:
3106           /* Just for use if some languages passes arrays by value.  */
3107           if (contains_128bit_aligned_vector_p (TREE_TYPE (type)))
3108             return true;
3109           
3110         default:
3111           gcc_unreachable ();
3112         }
3113     }
3114   return false;
3115 }
3116
3117 /* Gives the alignment boundary, in bits, of an argument with the
3118    specified mode and type.  */
3119
3120 int
3121 ix86_function_arg_boundary (enum machine_mode mode, tree type)
3122 {
3123   int align;
3124   if (type)
3125     align = TYPE_ALIGN (type);
3126   else
3127     align = GET_MODE_ALIGNMENT (mode);
3128   if (align < PARM_BOUNDARY)
3129     align = PARM_BOUNDARY;
3130   if (!TARGET_64BIT)
3131     {
3132       /* i386 ABI defines all arguments to be 4 byte aligned.  We have to
3133          make an exception for SSE modes since these require 128bit
3134          alignment.
3135
3136          The handling here differs from field_alignment.  ICC aligns MMX
3137          arguments to 4 byte boundaries, while structure fields are aligned
3138          to 8 byte boundaries.  */
3139       if (!TARGET_SSE)
3140         align = PARM_BOUNDARY;
3141       else if (!type)
3142         {
3143           if (!SSE_REG_MODE_P (mode))
3144             align = PARM_BOUNDARY;
3145         }
3146       else
3147         {
3148           if (!contains_128bit_aligned_vector_p (type))
3149             align = PARM_BOUNDARY;
3150         }
3151     }
3152   if (align > 128)
3153     align = 128;
3154   return align;
3155 }
3156
3157 /* Return true if N is a possible register number of function value.  */
3158 bool
3159 ix86_function_value_regno_p (int regno)
3160 {
3161   if (!TARGET_64BIT)
3162     {
3163       return ((regno) == 0
3164               || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387)
3165               || ((regno) == FIRST_SSE_REG && TARGET_SSE));
3166     }
3167   return ((regno) == 0 || (regno) == FIRST_FLOAT_REG
3168           || ((regno) == FIRST_SSE_REG && TARGET_SSE)
3169           || ((regno) == FIRST_FLOAT_REG && TARGET_FLOAT_RETURNS_IN_80387));
3170 }
3171
3172 /* Define how to find the value returned by a function.
3173    VALTYPE is the data type of the value (as a tree).
3174    If the precise function being called is known, FUNC is its FUNCTION_DECL;
3175    otherwise, FUNC is 0.  */
3176 rtx
3177 ix86_function_value (tree valtype, tree func)
3178 {
3179   enum machine_mode natmode = type_natural_mode (valtype);
3180
3181   if (TARGET_64BIT)
3182     {
3183       rtx ret = construct_container (natmode, TYPE_MODE (valtype), valtype,
3184                                      1, REGPARM_MAX, SSE_REGPARM_MAX,
3185                                      x86_64_int_return_registers, 0);
3186       /* For zero sized structures, construct_container return NULL, but we
3187          need to keep rest of compiler happy by returning meaningful value.  */
3188       if (!ret)
3189         ret = gen_rtx_REG (TYPE_MODE (valtype), 0);
3190       return ret;
3191     }
3192   else
3193     return gen_rtx_REG (TYPE_MODE (valtype), ix86_value_regno (natmode, func));
3194 }
3195
3196 /* Return false iff type is returned in memory.  */
3197 int
3198 ix86_return_in_memory (tree type)
3199 {
3200   int needed_intregs, needed_sseregs, size;
3201   enum machine_mode mode = type_natural_mode (type);
3202
3203   if (TARGET_64BIT)
3204     return !examine_argument (mode, type, 1, &needed_intregs, &needed_sseregs);
3205
3206   if (mode == BLKmode)
3207     return 1;
3208
3209   size = int_size_in_bytes (type);
3210
3211   if (MS_AGGREGATE_RETURN && AGGREGATE_TYPE_P (type) && size <= 8)
3212     return 0;
3213
3214   if (VECTOR_MODE_P (mode) || mode == TImode)
3215     {
3216       /* User-created vectors small enough to fit in EAX.  */
3217       if (size < 8)
3218         return 0;
3219
3220       /* MMX/3dNow values are returned on the stack, since we've
3221          got to EMMS/FEMMS before returning.  */
3222       if (size == 8)
3223         return 1;
3224
3225       /* SSE values are returned in XMM0, except when it doesn't exist.  */
3226       if (size == 16)
3227         return (TARGET_SSE ? 0 : 1);
3228     }
3229
3230   if (mode == XFmode)
3231     return 0;
3232
3233   if (size > 12)
3234     return 1;
3235   return 0;
3236 }
3237
3238 /* When returning SSE vector types, we have a choice of either
3239      (1) being abi incompatible with a -march switch, or
3240      (2) generating an error.
3241    Given no good solution, I think the safest thing is one warning.
3242    The user won't be able to use -Werror, but....
3243
3244    Choose the STRUCT_VALUE_RTX hook because that's (at present) only
3245    called in response to actually generating a caller or callee that
3246    uses such a type.  As opposed to RETURN_IN_MEMORY, which is called
3247    via aggregate_value_p for general type probing from tree-ssa.  */
3248
3249 static rtx
3250 ix86_struct_value_rtx (tree type, int incoming ATTRIBUTE_UNUSED)
3251 {
3252   static bool warned;
3253
3254   if (!TARGET_SSE && type && !warned)
3255     {
3256       /* Look at the return type of the function, not the function type.  */
3257       enum machine_mode mode = TYPE_MODE (TREE_TYPE (type));
3258
3259       if (mode == TImode
3260           || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3261         {
3262           warned = true;
3263           warning ("SSE vector return without SSE enabled changes the ABI");
3264         }
3265     }
3266
3267   return NULL;
3268 }
3269
3270 /* Define how to find the value returned by a library function
3271    assuming the value has mode MODE.  */
3272 rtx
3273 ix86_libcall_value (enum machine_mode mode)
3274 {
3275   if (TARGET_64BIT)
3276     {
3277       switch (mode)
3278         {
3279         case SFmode:
3280         case SCmode:
3281         case DFmode:
3282         case DCmode:
3283         case TFmode:
3284           return gen_rtx_REG (mode, FIRST_SSE_REG);
3285         case XFmode:
3286         case XCmode:
3287           return gen_rtx_REG (mode, FIRST_FLOAT_REG);
3288         case TCmode:
3289           return NULL;
3290         default:
3291           return gen_rtx_REG (mode, 0);
3292         }
3293     }
3294   else
3295     return gen_rtx_REG (mode, ix86_value_regno (mode, NULL));
3296 }
3297
3298 /* Given a mode, return the register to use for a return value.  */
3299
3300 static int
3301 ix86_value_regno (enum machine_mode mode, tree func)
3302 {
3303   gcc_assert (!TARGET_64BIT);
3304
3305   /* 16-byte vector modes in %xmm0.  See ix86_return_in_memory for where
3306      we prevent this case when sse is not available.  */
3307   if (mode == TImode || (VECTOR_MODE_P (mode) && GET_MODE_SIZE (mode) == 16))
3308     return FIRST_SSE_REG;
3309
3310   /* Most things go in %eax, except (unless -mno-fp-ret-in-387) fp values.  */
3311   if (GET_MODE_CLASS (mode) != MODE_FLOAT || !TARGET_FLOAT_RETURNS_IN_80387)
3312     return 0;
3313
3314   /* Floating point return values in %st(0), except for local functions when
3315      SSE math is enabled.  */
3316   if (func && SSE_FLOAT_MODE_P (mode) && TARGET_SSE_MATH
3317       && flag_unit_at_a_time)
3318     {
3319       struct cgraph_local_info *i = cgraph_local_info (func);
3320       if (i && i->local)
3321         return FIRST_SSE_REG;
3322     }
3323
3324   return FIRST_FLOAT_REG;
3325 }
3326 \f
3327 /* Create the va_list data type.  */
3328
3329 static tree
3330 ix86_build_builtin_va_list (void)
3331 {
3332   tree f_gpr, f_fpr, f_ovf, f_sav, record, type_decl;
3333
3334   /* For i386 we use plain pointer to argument area.  */
3335   if (!TARGET_64BIT)
3336     return build_pointer_type (char_type_node);
3337
3338   record = (*lang_hooks.types.make_type) (RECORD_TYPE);
3339   type_decl = build_decl (TYPE_DECL, get_identifier ("__va_list_tag"), record);
3340
3341   f_gpr = build_decl (FIELD_DECL, get_identifier ("gp_offset"),
3342                       unsigned_type_node);
3343   f_fpr = build_decl (FIELD_DECL, get_identifier ("fp_offset"),
3344                       unsigned_type_node);
3345   f_ovf = build_decl (FIELD_DECL, get_identifier ("overflow_arg_area"),
3346                       ptr_type_node);
3347   f_sav = build_decl (FIELD_DECL, get_identifier ("reg_save_area"),
3348                       ptr_type_node);
3349
3350   va_list_gpr_counter_field = f_gpr;
3351   va_list_fpr_counter_field = f_fpr;
3352
3353   DECL_FIELD_CONTEXT (f_gpr) = record;
3354   DECL_FIELD_CONTEXT (f_fpr) = record;
3355   DECL_FIELD_CONTEXT (f_ovf) = record;
3356   DECL_FIELD_CONTEXT (f_sav) = record;
3357
3358   TREE_CHAIN (record) = type_decl;
3359   TYPE_NAME (record) = type_decl;
3360   TYPE_FIELDS (record) = f_gpr;
3361   TREE_CHAIN (f_gpr) = f_fpr;
3362   TREE_CHAIN (f_fpr) = f_ovf;
3363   TREE_CHAIN (f_ovf) = f_sav;
3364
3365   layout_type (record);
3366
3367   /* The correct type is an array type of one element.  */
3368   return build_array_type (record, build_index_type (size_zero_node));
3369 }
3370
3371 /* Worker function for TARGET_SETUP_INCOMING_VARARGS.  */
3372
3373 static void
3374 ix86_setup_incoming_varargs (CUMULATIVE_ARGS *cum, enum machine_mode mode,
3375                              tree type, int *pretend_size ATTRIBUTE_UNUSED,
3376                              int no_rtl)
3377 {
3378   CUMULATIVE_ARGS next_cum;
3379   rtx save_area = NULL_RTX, mem;
3380   rtx label;
3381   rtx label_ref;
3382   rtx tmp_reg;
3383   rtx nsse_reg;
3384   int set;
3385   tree fntype;
3386   int stdarg_p;
3387   int i;
3388
3389   if (!TARGET_64BIT)
3390     return;
3391
3392   if (! cfun->va_list_gpr_size && ! cfun->va_list_fpr_size)
3393     return;
3394
3395   /* Indicate to allocate space on the stack for varargs save area.  */
3396   ix86_save_varrargs_registers = 1;
3397
3398   cfun->stack_alignment_needed = 128;
3399
3400   fntype = TREE_TYPE (current_function_decl);
3401   stdarg_p = (TYPE_ARG_TYPES (fntype) != 0
3402               && (TREE_VALUE (tree_last (TYPE_ARG_TYPES (fntype)))
3403                   != void_type_node));
3404
3405   /* For varargs, we do not want to skip the dummy va_dcl argument.
3406      For stdargs, we do want to skip the last named argument.  */
3407   next_cum = *cum;
3408   if (stdarg_p)
3409     function_arg_advance (&next_cum, mode, type, 1);
3410
3411   if (!no_rtl)
3412     save_area = frame_pointer_rtx;
3413
3414   set = get_varargs_alias_set ();
3415
3416   for (i = next_cum.regno;
3417        i < ix86_regparm
3418        && i < next_cum.regno + cfun->va_list_gpr_size / UNITS_PER_WORD;
3419        i++)
3420     {
3421       mem = gen_rtx_MEM (Pmode,
3422                          plus_constant (save_area, i * UNITS_PER_WORD));
3423       set_mem_alias_set (mem, set);
3424       emit_move_insn (mem, gen_rtx_REG (Pmode,
3425                                         x86_64_int_parameter_registers[i]));
3426     }
3427
3428   if (next_cum.sse_nregs && cfun->va_list_fpr_size)
3429     {
3430       /* Now emit code to save SSE registers.  The AX parameter contains number
3431          of SSE parameter registers used to call this function.  We use
3432          sse_prologue_save insn template that produces computed jump across
3433          SSE saves.  We need some preparation work to get this working.  */
3434
3435       label = gen_label_rtx ();
3436       label_ref = gen_rtx_LABEL_REF (Pmode, label);
3437
3438       /* Compute address to jump to :
3439          label - 5*eax + nnamed_sse_arguments*5  */
3440       tmp_reg = gen_reg_rtx (Pmode);
3441       nsse_reg = gen_reg_rtx (Pmode);
3442       emit_insn (gen_zero_extendqidi2 (nsse_reg, gen_rtx_REG (QImode, 0)));
3443       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3444                               gen_rtx_MULT (Pmode, nsse_reg,
3445                                             GEN_INT (4))));
3446       if (next_cum.sse_regno)
3447         emit_move_insn
3448           (nsse_reg,
3449            gen_rtx_CONST (DImode,
3450                           gen_rtx_PLUS (DImode,
3451                                         label_ref,
3452                                         GEN_INT (next_cum.sse_regno * 4))));
3453       else
3454         emit_move_insn (nsse_reg, label_ref);
3455       emit_insn (gen_subdi3 (nsse_reg, nsse_reg, tmp_reg));
3456
3457       /* Compute address of memory block we save into.  We always use pointer
3458          pointing 127 bytes after first byte to store - this is needed to keep
3459          instruction size limited by 4 bytes.  */
3460       tmp_reg = gen_reg_rtx (Pmode);
3461       emit_insn (gen_rtx_SET (VOIDmode, tmp_reg,
3462                               plus_constant (save_area,
3463                                              8 * REGPARM_MAX + 127)));
3464       mem = gen_rtx_MEM (BLKmode, plus_constant (tmp_reg, -127));
3465       set_mem_alias_set (mem, set);
3466       set_mem_align (mem, BITS_PER_WORD);
3467
3468       /* And finally do the dirty job!  */
3469       emit_insn (gen_sse_prologue_save (mem, nsse_reg,
3470                                         GEN_INT (next_cum.sse_regno), label));
3471     }
3472
3473 }
3474
3475 /* Implement va_start.  */
3476
3477 void
3478 ix86_va_start (tree valist, rtx nextarg)
3479 {
3480   HOST_WIDE_INT words, n_gpr, n_fpr;
3481   tree f_gpr, f_fpr, f_ovf, f_sav;
3482   tree gpr, fpr, ovf, sav, t;
3483
3484   /* Only 64bit target needs something special.  */
3485   if (!TARGET_64BIT)
3486     {
3487       std_expand_builtin_va_start (valist, nextarg);
3488       return;
3489     }
3490
3491   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3492   f_fpr = TREE_CHAIN (f_gpr);
3493   f_ovf = TREE_CHAIN (f_fpr);
3494   f_sav = TREE_CHAIN (f_ovf);
3495
3496   valist = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (valist)), valist);
3497   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3498   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3499   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3500   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3501
3502   /* Count number of gp and fp argument registers used.  */
3503   words = current_function_args_info.words;
3504   n_gpr = current_function_args_info.regno;
3505   n_fpr = current_function_args_info.sse_regno;
3506
3507   if (TARGET_DEBUG_ARG)
3508     fprintf (stderr, "va_start: words = %d, n_gpr = %d, n_fpr = %d\n",
3509              (int) words, (int) n_gpr, (int) n_fpr);
3510
3511   if (cfun->va_list_gpr_size)
3512     {
3513       t = build (MODIFY_EXPR, TREE_TYPE (gpr), gpr,
3514                  build_int_cst (NULL_TREE, n_gpr * 8));
3515       TREE_SIDE_EFFECTS (t) = 1;
3516       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3517     }
3518
3519   if (cfun->va_list_fpr_size)
3520     {
3521       t = build (MODIFY_EXPR, TREE_TYPE (fpr), fpr,
3522                  build_int_cst (NULL_TREE, n_fpr * 16 + 8*REGPARM_MAX));
3523       TREE_SIDE_EFFECTS (t) = 1;
3524       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3525     }
3526
3527   /* Find the overflow area.  */
3528   t = make_tree (TREE_TYPE (ovf), virtual_incoming_args_rtx);
3529   if (words != 0)
3530     t = build (PLUS_EXPR, TREE_TYPE (ovf), t,
3531                build_int_cst (NULL_TREE, words * UNITS_PER_WORD));
3532   t = build (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3533   TREE_SIDE_EFFECTS (t) = 1;
3534   expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3535
3536   if (cfun->va_list_gpr_size || cfun->va_list_fpr_size)
3537     {
3538       /* Find the register save area.
3539          Prologue of the function save it right above stack frame.  */
3540       t = make_tree (TREE_TYPE (sav), frame_pointer_rtx);
3541       t = build (MODIFY_EXPR, TREE_TYPE (sav), sav, t);
3542       TREE_SIDE_EFFECTS (t) = 1;
3543       expand_expr (t, const0_rtx, VOIDmode, EXPAND_NORMAL);
3544     }
3545 }
3546
3547 /* Implement va_arg.  */
3548
3549 tree
3550 ix86_gimplify_va_arg (tree valist, tree type, tree *pre_p, tree *post_p)
3551 {
3552   static const int intreg[6] = { 0, 1, 2, 3, 4, 5 };
3553   tree f_gpr, f_fpr, f_ovf, f_sav;
3554   tree gpr, fpr, ovf, sav, t;
3555   int size, rsize;
3556   tree lab_false, lab_over = NULL_TREE;
3557   tree addr, t2;
3558   rtx container;
3559   int indirect_p = 0;
3560   tree ptrtype;
3561   enum machine_mode nat_mode;
3562
3563   /* Only 64bit target needs something special.  */
3564   if (!TARGET_64BIT)
3565     return std_gimplify_va_arg_expr (valist, type, pre_p, post_p);
3566
3567   f_gpr = TYPE_FIELDS (TREE_TYPE (va_list_type_node));
3568   f_fpr = TREE_CHAIN (f_gpr);
3569   f_ovf = TREE_CHAIN (f_fpr);
3570   f_sav = TREE_CHAIN (f_ovf);
3571
3572   valist = build_va_arg_indirect_ref (valist);
3573   gpr = build (COMPONENT_REF, TREE_TYPE (f_gpr), valist, f_gpr, NULL_TREE);
3574   fpr = build (COMPONENT_REF, TREE_TYPE (f_fpr), valist, f_fpr, NULL_TREE);
3575   ovf = build (COMPONENT_REF, TREE_TYPE (f_ovf), valist, f_ovf, NULL_TREE);
3576   sav = build (COMPONENT_REF, TREE_TYPE (f_sav), valist, f_sav, NULL_TREE);
3577
3578   indirect_p = pass_by_reference (NULL, TYPE_MODE (type), type, false);
3579   if (indirect_p)
3580     type = build_pointer_type (type);
3581   size = int_size_in_bytes (type);
3582   rsize = (size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
3583
3584   nat_mode = type_natural_mode (type);
3585   container = construct_container (nat_mode, TYPE_MODE (type), type, 0,
3586                                    REGPARM_MAX, SSE_REGPARM_MAX, intreg, 0);
3587
3588   /* Pull the value out of the saved registers.  */
3589
3590   addr = create_tmp_var (ptr_type_node, "addr");
3591   DECL_POINTER_ALIAS_SET (addr) = get_varargs_alias_set ();
3592
3593   if (container)
3594     {
3595       int needed_intregs, needed_sseregs;
3596       bool need_temp;
3597       tree int_addr, sse_addr;
3598
3599       lab_false = create_artificial_label ();
3600       lab_over = create_artificial_label ();
3601
3602       examine_argument (nat_mode, type, 0, &needed_intregs, &needed_sseregs);
3603
3604       need_temp = (!REG_P (container)
3605                    && ((needed_intregs && TYPE_ALIGN (type) > 64)
3606                        || TYPE_ALIGN (type) > 128));
3607
3608       /* In case we are passing structure, verify that it is consecutive block
3609          on the register save area.  If not we need to do moves.  */
3610       if (!need_temp && !REG_P (container))
3611         {
3612           /* Verify that all registers are strictly consecutive  */
3613           if (SSE_REGNO_P (REGNO (XEXP (XVECEXP (container, 0, 0), 0))))
3614             {
3615               int i;
3616
3617               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3618                 {
3619                   rtx slot = XVECEXP (container, 0, i);
3620                   if (REGNO (XEXP (slot, 0)) != FIRST_SSE_REG + (unsigned int) i
3621                       || INTVAL (XEXP (slot, 1)) != i * 16)
3622                     need_temp = 1;
3623                 }
3624             }
3625           else
3626             {
3627               int i;
3628
3629               for (i = 0; i < XVECLEN (container, 0) && !need_temp; i++)
3630                 {
3631                   rtx slot = XVECEXP (container, 0, i);
3632                   if (REGNO (XEXP (slot, 0)) != (unsigned int) i
3633                       || INTVAL (XEXP (slot, 1)) != i * 8)
3634                     need_temp = 1;
3635                 }
3636             }
3637         }
3638       if (!need_temp)
3639         {
3640           int_addr = addr;
3641           sse_addr = addr;
3642         }
3643       else
3644         {
3645           int_addr = create_tmp_var (ptr_type_node, "int_addr");
3646           DECL_POINTER_ALIAS_SET (int_addr) = get_varargs_alias_set ();
3647           sse_addr = create_tmp_var (ptr_type_node, "sse_addr");
3648           DECL_POINTER_ALIAS_SET (sse_addr) = get_varargs_alias_set ();
3649         }
3650
3651       /* First ensure that we fit completely in registers.  */
3652       if (needed_intregs)
3653         {
3654           t = build_int_cst (TREE_TYPE (gpr),
3655                              (REGPARM_MAX - needed_intregs + 1) * 8);
3656           t = build2 (GE_EXPR, boolean_type_node, gpr, t);
3657           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3658           t = build (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3659           gimplify_and_add (t, pre_p);
3660         }
3661       if (needed_sseregs)
3662         {
3663           t = build_int_cst (TREE_TYPE (fpr),
3664                              (SSE_REGPARM_MAX - needed_sseregs + 1) * 16
3665                              + REGPARM_MAX * 8);
3666           t = build2 (GE_EXPR, boolean_type_node, fpr, t);
3667           t2 = build1 (GOTO_EXPR, void_type_node, lab_false);
3668           t = build (COND_EXPR, void_type_node, t, t2, NULL_TREE);
3669           gimplify_and_add (t, pre_p);
3670         }
3671
3672       /* Compute index to start of area used for integer regs.  */
3673       if (needed_intregs)
3674         {
3675           /* int_addr = gpr + sav; */
3676           t = fold_convert (ptr_type_node, gpr);
3677           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
3678           t = build2 (MODIFY_EXPR, void_type_node, int_addr, t);
3679           gimplify_and_add (t, pre_p);
3680         }
3681       if (needed_sseregs)
3682         {
3683           /* sse_addr = fpr + sav; */
3684           t = fold_convert (ptr_type_node, fpr);
3685           t = build2 (PLUS_EXPR, ptr_type_node, sav, t);
3686           t = build2 (MODIFY_EXPR, void_type_node, sse_addr, t);
3687           gimplify_and_add (t, pre_p);
3688         }
3689       if (need_temp)
3690         {
3691           int i;
3692           tree temp = create_tmp_var (type, "va_arg_tmp");
3693
3694           /* addr = &temp; */
3695           t = build1 (ADDR_EXPR, build_pointer_type (type), temp);
3696           t = build2 (MODIFY_EXPR, void_type_node, addr, t);
3697           gimplify_and_add (t, pre_p);
3698
3699           for (i = 0; i < XVECLEN (container, 0); i++)
3700             {
3701               rtx slot = XVECEXP (container, 0, i);
3702               rtx reg = XEXP (slot, 0);
3703               enum machine_mode mode = GET_MODE (reg);
3704               tree piece_type = lang_hooks.types.type_for_mode (mode, 1);
3705               tree addr_type = build_pointer_type (piece_type);
3706               tree src_addr, src;
3707               int src_offset;
3708               tree dest_addr, dest;
3709
3710               if (SSE_REGNO_P (REGNO (reg)))
3711                 {
3712                   src_addr = sse_addr;
3713                   src_offset = (REGNO (reg) - FIRST_SSE_REG) * 16;
3714                 }
3715               else
3716                 {
3717                   src_addr = int_addr;
3718                   src_offset = REGNO (reg) * 8;
3719                 }
3720               src_addr = fold_convert (addr_type, src_addr);
3721               src_addr = fold (build2 (PLUS_EXPR, addr_type, src_addr,
3722                                        size_int (src_offset)));
3723               src = build_va_arg_indirect_ref (src_addr);
3724
3725               dest_addr = fold_convert (addr_type, addr);
3726               dest_addr = fold (build2 (PLUS_EXPR, addr_type, dest_addr,
3727                                         size_int (INTVAL (XEXP (slot, 1)))));
3728               dest = build_va_arg_indirect_ref (dest_addr);
3729
3730               t = build2 (MODIFY_EXPR, void_type_node, dest, src);
3731               gimplify_and_add (t, pre_p);
3732             }
3733         }
3734
3735       if (needed_intregs)
3736         {
3737           t = build2 (PLUS_EXPR, TREE_TYPE (gpr), gpr,
3738                       build_int_cst (TREE_TYPE (gpr), needed_intregs * 8));
3739           t = build2 (MODIFY_EXPR, TREE_TYPE (gpr), gpr, t);
3740           gimplify_and_add (t, pre_p);
3741         }
3742       if (needed_sseregs)
3743         {
3744           t = build2 (PLUS_EXPR, TREE_TYPE (fpr), fpr,
3745                       build_int_cst (TREE_TYPE (fpr), needed_sseregs * 16));
3746           t = build2 (MODIFY_EXPR, TREE_TYPE (fpr), fpr, t);
3747           gimplify_and_add (t, pre_p);
3748         }
3749
3750       t = build1 (GOTO_EXPR, void_type_node, lab_over);
3751       gimplify_and_add (t, pre_p);
3752
3753       t = build1 (LABEL_EXPR, void_type_node, lab_false);
3754       append_to_statement_list (t, pre_p);
3755     }
3756
3757   /* ... otherwise out of the overflow area.  */
3758
3759   /* Care for on-stack alignment if needed.  */
3760   if (FUNCTION_ARG_BOUNDARY (VOIDmode, type) <= 64)
3761     t = ovf;
3762   else
3763     {
3764       HOST_WIDE_INT align = FUNCTION_ARG_BOUNDARY (VOIDmode, type) / 8;
3765       t = build (PLUS_EXPR, TREE_TYPE (ovf), ovf,
3766                  build_int_cst (TREE_TYPE (ovf), align - 1));
3767       t = build (BIT_AND_EXPR, TREE_TYPE (t), t,
3768                  build_int_cst (TREE_TYPE (t), -align));
3769     }
3770   gimplify_expr (&t, pre_p, NULL, is_gimple_val, fb_rvalue);
3771
3772   t2 = build2 (MODIFY_EXPR, void_type_node, addr, t);
3773   gimplify_and_add (t2, pre_p);
3774
3775   t = build2 (PLUS_EXPR, TREE_TYPE (t), t,
3776               build_int_cst (TREE_TYPE (t), rsize * UNITS_PER_WORD));
3777   t = build2 (MODIFY_EXPR, TREE_TYPE (ovf), ovf, t);
3778   gimplify_and_add (t, pre_p);
3779
3780   if (container)
3781     {
3782       t = build1 (LABEL_EXPR, void_type_node, lab_over);
3783       append_to_statement_list (t, pre_p);
3784     }
3785
3786   ptrtype = build_pointer_type (type);
3787   addr = fold_convert (ptrtype, addr);
3788
3789   if (indirect_p)
3790     addr = build_va_arg_indirect_ref (addr);
3791   return build_va_arg_indirect_ref (addr);
3792 }
3793 \f
3794 /* Return nonzero if OPNUM's MEM should be matched
3795    in movabs* patterns.  */
3796
3797 int
3798 ix86_check_movabs (rtx insn, int opnum)
3799 {
3800   rtx set, mem;
3801
3802   set = PATTERN (insn);
3803   if (GET_CODE (set) == PARALLEL)
3804     set = XVECEXP (set, 0, 0);
3805   gcc_assert (GET_CODE (set) == SET);
3806   mem = XEXP (set, opnum);
3807   while (GET_CODE (mem) == SUBREG)
3808     mem = SUBREG_REG (mem);
3809   gcc_assert (GET_CODE (mem) == MEM);
3810   return (volatile_ok || !MEM_VOLATILE_P (mem));
3811 }
3812 \f
3813 /* Initialize the table of extra 80387 mathematical constants.  */
3814
3815 static void
3816 init_ext_80387_constants (void)
3817 {
3818   static const char * cst[5] =
3819   {
3820     "0.3010299956639811952256464283594894482",  /* 0: fldlg2  */
3821     "0.6931471805599453094286904741849753009",  /* 1: fldln2  */
3822     "1.4426950408889634073876517827983434472",  /* 2: fldl2e  */
3823     "3.3219280948873623478083405569094566090",  /* 3: fldl2t  */
3824     "3.1415926535897932385128089594061862044",  /* 4: fldpi   */
3825   };
3826   int i;
3827
3828   for (i = 0; i < 5; i++)
3829     {
3830       real_from_string (&ext_80387_constants_table[i], cst[i]);
3831       /* Ensure each constant is rounded to XFmode precision.  */
3832       real_convert (&ext_80387_constants_table[i],
3833                     XFmode, &ext_80387_constants_table[i]);
3834     }
3835
3836   ext_80387_constants_init = 1;
3837 }
3838
3839 /* Return true if the constant is something that can be loaded with
3840    a special instruction.  */
3841
3842 int
3843 standard_80387_constant_p (rtx x)
3844 {
3845   if (GET_CODE (x) != CONST_DOUBLE || !FLOAT_MODE_P (GET_MODE (x)))
3846     return -1;
3847
3848   if (x == CONST0_RTX (GET_MODE (x)))
3849     return 1;
3850   if (x == CONST1_RTX (GET_MODE (x)))
3851     return 2;
3852
3853   /* For XFmode constants, try to find a special 80387 instruction when
3854      optimizing for size or on those CPUs that benefit from them.  */
3855   if (GET_MODE (x) == XFmode
3856       && (optimize_size || x86_ext_80387_constants & TUNEMASK))
3857     {
3858       REAL_VALUE_TYPE r;
3859       int i;
3860
3861       if (! ext_80387_constants_init)
3862         init_ext_80387_constants ();
3863
3864       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3865       for (i = 0; i < 5; i++)
3866         if (real_identical (&r, &ext_80387_constants_table[i]))
3867           return i + 3;
3868     }
3869
3870   return 0;
3871 }
3872
3873 /* Return the opcode of the special instruction to be used to load
3874    the constant X.  */
3875
3876 const char *
3877 standard_80387_constant_opcode (rtx x)
3878 {
3879   switch (standard_80387_constant_p (x))
3880     {
3881     case 1:
3882       return "fldz";
3883     case 2:
3884       return "fld1";
3885     case 3:
3886       return "fldlg2";
3887     case 4:
3888       return "fldln2";
3889     case 5:
3890       return "fldl2e";
3891     case 6:
3892       return "fldl2t";
3893     case 7:
3894       return "fldpi";
3895     default:
3896       gcc_unreachable ();
3897     }
3898 }
3899
3900 /* Return the CONST_DOUBLE representing the 80387 constant that is
3901    loaded by the specified special instruction.  The argument IDX
3902    matches the return value from standard_80387_constant_p.  */
3903
3904 rtx
3905 standard_80387_constant_rtx (int idx)
3906 {
3907   int i;
3908
3909   if (! ext_80387_constants_init)
3910     init_ext_80387_constants ();
3911
3912   switch (idx)
3913     {
3914     case 3:
3915     case 4:
3916     case 5:
3917     case 6:
3918     case 7:
3919       i = idx - 3;
3920       break;
3921
3922     default:
3923       gcc_unreachable ();
3924     }
3925
3926   return CONST_DOUBLE_FROM_REAL_VALUE (ext_80387_constants_table[i],
3927                                        XFmode);
3928 }
3929
3930 /* Return 1 if X is FP constant we can load to SSE register w/o using memory.
3931  */
3932 int
3933 standard_sse_constant_p (rtx x)
3934 {
3935   if (x == const0_rtx)
3936     return 1;
3937   return (x == CONST0_RTX (GET_MODE (x)));
3938 }
3939
3940 /* Returns 1 if OP contains a symbol reference */
3941
3942 int
3943 symbolic_reference_mentioned_p (rtx op)
3944 {
3945   const char *fmt;
3946   int i;
3947
3948   if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
3949     return 1;
3950
3951   fmt = GET_RTX_FORMAT (GET_CODE (op));
3952   for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
3953     {
3954       if (fmt[i] == 'E')
3955         {
3956           int j;
3957
3958           for (j = XVECLEN (op, i) - 1; j >= 0; j--)
3959             if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
3960               return 1;
3961         }
3962
3963       else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
3964         return 1;
3965     }
3966
3967   return 0;
3968 }
3969
3970 /* Return 1 if it is appropriate to emit `ret' instructions in the
3971    body of a function.  Do this only if the epilogue is simple, needing a
3972    couple of insns.  Prior to reloading, we can't tell how many registers
3973    must be saved, so return 0 then.  Return 0 if there is no frame
3974    marker to de-allocate.  */
3975
3976 int
3977 ix86_can_use_return_insn_p (void)
3978 {
3979   struct ix86_frame frame;
3980
3981   if (! reload_completed || frame_pointer_needed)
3982     return 0;
3983
3984   /* Don't allow more than 32 pop, since that's all we can do
3985      with one instruction.  */
3986   if (current_function_pops_args
3987       && current_function_args_size >= 32768)
3988     return 0;
3989
3990   ix86_compute_frame_layout (&frame);
3991   return frame.to_allocate == 0 && frame.nregs == 0;
3992 }
3993 \f
3994 /* Value should be nonzero if functions must have frame pointers.
3995    Zero means the frame pointer need not be set up (and parms may
3996    be accessed via the stack pointer) in functions that seem suitable.  */
3997
3998 int
3999 ix86_frame_pointer_required (void)
4000 {
4001   /* If we accessed previous frames, then the generated code expects
4002      to be able to access the saved ebp value in our frame.  */
4003   if (cfun->machine->accesses_prev_frame)
4004     return 1;
4005
4006   /* Several x86 os'es need a frame pointer for other reasons,
4007      usually pertaining to setjmp.  */
4008   if (SUBTARGET_FRAME_POINTER_REQUIRED)
4009     return 1;
4010
4011   /* In override_options, TARGET_OMIT_LEAF_FRAME_POINTER turns off
4012      the frame pointer by default.  Turn it back on now if we've not
4013      got a leaf function.  */
4014   if (TARGET_OMIT_LEAF_FRAME_POINTER
4015       && (!current_function_is_leaf))
4016     return 1;
4017
4018   if (current_function_profile)
4019     return 1;
4020
4021   return 0;
4022 }
4023
4024 /* Record that the current function accesses previous call frames.  */
4025
4026 void
4027 ix86_setup_frame_addresses (void)
4028 {
4029   cfun->machine->accesses_prev_frame = 1;
4030 }
4031 \f
4032 #if defined(HAVE_GAS_HIDDEN) && defined(SUPPORTS_ONE_ONLY)
4033 # define USE_HIDDEN_LINKONCE 1
4034 #else
4035 # define USE_HIDDEN_LINKONCE 0
4036 #endif
4037
4038 static int pic_labels_used;
4039
4040 /* Fills in the label name that should be used for a pc thunk for
4041    the given register.  */
4042
4043 static void
4044 get_pc_thunk_name (char name[32], unsigned int regno)
4045 {
4046   if (USE_HIDDEN_LINKONCE)
4047     sprintf (name, "__i686.get_pc_thunk.%s", reg_names[regno]);
4048   else
4049     ASM_GENERATE_INTERNAL_LABEL (name, "LPR", regno);
4050 }
4051
4052
4053 /* This function generates code for -fpic that loads %ebx with
4054    the return address of the caller and then returns.  */
4055
4056 void
4057 ix86_file_end (void)
4058 {
4059   rtx xops[2];
4060   int regno;
4061
4062   for (regno = 0; regno < 8; ++regno)
4063     {
4064       char name[32];
4065
4066       if (! ((pic_labels_used >> regno) & 1))
4067         continue;
4068
4069       get_pc_thunk_name (name, regno);
4070
4071       if (USE_HIDDEN_LINKONCE)
4072         {
4073           tree decl;
4074
4075           decl = build_decl (FUNCTION_DECL, get_identifier (name),
4076                              error_mark_node);
4077           TREE_PUBLIC (decl) = 1;
4078           TREE_STATIC (decl) = 1;
4079           DECL_ONE_ONLY (decl) = 1;
4080
4081           (*targetm.asm_out.unique_section) (decl, 0);
4082           named_section (decl, NULL, 0);
4083
4084           (*targetm.asm_out.globalize_label) (asm_out_file, name);
4085           fputs ("\t.hidden\t", asm_out_file);
4086           assemble_name (asm_out_file, name);
4087           fputc ('\n', asm_out_file);
4088           ASM_DECLARE_FUNCTION_NAME (asm_out_file, name, decl);
4089         }
4090       else
4091         {
4092           text_section ();
4093           ASM_OUTPUT_LABEL (asm_out_file, name);
4094         }
4095
4096       xops[0] = gen_rtx_REG (SImode, regno);
4097       xops[1] = gen_rtx_MEM (SImode, stack_pointer_rtx);
4098       output_asm_insn ("mov{l}\t{%1, %0|%0, %1}", xops);
4099       output_asm_insn ("ret", xops);
4100     }
4101
4102   if (NEED_INDICATE_EXEC_STACK)
4103     file_end_indicate_exec_stack ();
4104 }
4105
4106 /* Emit code for the SET_GOT patterns.  */
4107
4108 const char *
4109 output_set_got (rtx dest)
4110 {
4111   rtx xops[3];
4112
4113   xops[0] = dest;
4114   xops[1] = gen_rtx_SYMBOL_REF (Pmode, GOT_SYMBOL_NAME);
4115
4116   if (! TARGET_DEEP_BRANCH_PREDICTION || !flag_pic)
4117     {
4118       xops[2] = gen_rtx_LABEL_REF (Pmode, gen_label_rtx ());
4119
4120       if (!flag_pic)
4121         output_asm_insn ("mov{l}\t{%2, %0|%0, %2}", xops);
4122       else
4123         output_asm_insn ("call\t%a2", xops);
4124
4125 #if TARGET_MACHO
4126       /* Output the "canonical" label name ("Lxx$pb") here too.  This
4127          is what will be referred to by the Mach-O PIC subsystem.  */
4128       ASM_OUTPUT_LABEL (asm_out_file, machopic_function_base_name ());
4129 #endif
4130       (*targetm.asm_out.internal_label) (asm_out_file, "L",
4131                                  CODE_LABEL_NUMBER (XEXP (xops[2], 0)));
4132
4133       if (flag_pic)
4134         output_asm_insn ("pop{l}\t%0", xops);
4135     }
4136   else
4137     {
4138       char name[32];
4139       get_pc_thunk_name (name, REGNO (dest));
4140       pic_labels_used |= 1 << REGNO (dest);
4141
4142       xops[2] = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (name));
4143       xops[2] = gen_rtx_MEM (QImode, xops[2]);
4144       output_asm_insn ("call\t%X2", xops);
4145     }
4146
4147   if (!flag_pic || TARGET_DEEP_BRANCH_PREDICTION)
4148     output_asm_insn ("add{l}\t{%1, %0|%0, %1}", xops);
4149   else if (!TARGET_MACHO)
4150     output_asm_insn ("add{l}\t{%1+[.-%a2], %0|%0, %a1+(.-%a2)}", xops);
4151
4152   return "";
4153 }
4154
4155 /* Generate an "push" pattern for input ARG.  */
4156
4157 static rtx
4158 gen_push (rtx arg)
4159 {
4160   return gen_rtx_SET (VOIDmode,
4161                       gen_rtx_MEM (Pmode,
4162                                    gen_rtx_PRE_DEC (Pmode,
4163                                                     stack_pointer_rtx)),
4164                       arg);
4165 }
4166
4167 /* Return >= 0 if there is an unused call-clobbered register available
4168    for the entire function.  */
4169
4170 static unsigned int
4171 ix86_select_alt_pic_regnum (void)
4172 {
4173   if (current_function_is_leaf && !current_function_profile)
4174     {
4175       int i;
4176       for (i = 2; i >= 0; --i)
4177         if (!regs_ever_live[i])
4178           return i;
4179     }
4180
4181   return INVALID_REGNUM;
4182 }
4183
4184 /* Return 1 if we need to save REGNO.  */
4185 static int
4186 ix86_save_reg (unsigned int regno, int maybe_eh_return)
4187 {
4188   if (pic_offset_table_rtx
4189       && regno == REAL_PIC_OFFSET_TABLE_REGNUM
4190       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4191           || current_function_profile
4192           || current_function_calls_eh_return
4193           || current_function_uses_const_pool))
4194     {
4195       if (ix86_select_alt_pic_regnum () != INVALID_REGNUM)
4196         return 0;
4197       return 1;
4198     }
4199
4200   if (current_function_calls_eh_return && maybe_eh_return)
4201     {
4202       unsigned i;
4203       for (i = 0; ; i++)
4204         {
4205           unsigned test = EH_RETURN_DATA_REGNO (i);
4206           if (test == INVALID_REGNUM)
4207             break;
4208           if (test == regno)
4209             return 1;
4210         }
4211     }
4212
4213   return (regs_ever_live[regno]
4214           && !call_used_regs[regno]
4215           && !fixed_regs[regno]
4216           && (regno != HARD_FRAME_POINTER_REGNUM || !frame_pointer_needed));
4217 }
4218
4219 /* Return number of registers to be saved on the stack.  */
4220
4221 static int
4222 ix86_nsaved_regs (void)
4223 {
4224   int nregs = 0;
4225   int regno;
4226
4227   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4228     if (ix86_save_reg (regno, true))
4229       nregs++;
4230   return nregs;
4231 }
4232
4233 /* Return the offset between two registers, one to be eliminated, and the other
4234    its replacement, at the start of a routine.  */
4235
4236 HOST_WIDE_INT
4237 ix86_initial_elimination_offset (int from, int to)
4238 {
4239   struct ix86_frame frame;
4240   ix86_compute_frame_layout (&frame);
4241
4242   if (from == ARG_POINTER_REGNUM && to == HARD_FRAME_POINTER_REGNUM)
4243     return frame.hard_frame_pointer_offset;
4244   else if (from == FRAME_POINTER_REGNUM
4245            && to == HARD_FRAME_POINTER_REGNUM)
4246     return frame.hard_frame_pointer_offset - frame.frame_pointer_offset;
4247   else
4248     {
4249       gcc_assert (to == STACK_POINTER_REGNUM);
4250
4251       if (from == ARG_POINTER_REGNUM)
4252         return frame.stack_pointer_offset;
4253       
4254       gcc_assert (from == FRAME_POINTER_REGNUM);
4255       return frame.stack_pointer_offset - frame.frame_pointer_offset;
4256     }
4257 }
4258
4259 /* Fill structure ix86_frame about frame of currently computed function.  */
4260
4261 static void
4262 ix86_compute_frame_layout (struct ix86_frame *frame)
4263 {
4264   HOST_WIDE_INT total_size;
4265   unsigned int stack_alignment_needed;
4266   HOST_WIDE_INT offset;
4267   unsigned int preferred_alignment;
4268   HOST_WIDE_INT size = get_frame_size ();
4269
4270   frame->nregs = ix86_nsaved_regs ();
4271   total_size = size;
4272
4273   stack_alignment_needed = cfun->stack_alignment_needed / BITS_PER_UNIT;
4274   preferred_alignment = cfun->preferred_stack_boundary / BITS_PER_UNIT;
4275
4276   /* During reload iteration the amount of registers saved can change.
4277      Recompute the value as needed.  Do not recompute when amount of registers
4278      didn't change as reload does multiple calls to the function and does not
4279      expect the decision to change within single iteration.  */
4280   if (!optimize_size
4281       && cfun->machine->use_fast_prologue_epilogue_nregs != frame->nregs)
4282     {
4283       int count = frame->nregs;
4284
4285       cfun->machine->use_fast_prologue_epilogue_nregs = count;
4286       /* The fast prologue uses move instead of push to save registers.  This
4287          is significantly longer, but also executes faster as modern hardware
4288          can execute the moves in parallel, but can't do that for push/pop.
4289
4290          Be careful about choosing what prologue to emit:  When function takes
4291          many instructions to execute we may use slow version as well as in
4292          case function is known to be outside hot spot (this is known with
4293          feedback only).  Weight the size of function by number of registers
4294          to save as it is cheap to use one or two push instructions but very
4295          slow to use many of them.  */
4296       if (count)
4297         count = (count - 1) * FAST_PROLOGUE_INSN_COUNT;
4298       if (cfun->function_frequency < FUNCTION_FREQUENCY_NORMAL
4299           || (flag_branch_probabilities
4300               && cfun->function_frequency < FUNCTION_FREQUENCY_HOT))
4301         cfun->machine->use_fast_prologue_epilogue = false;
4302       else
4303         cfun->machine->use_fast_prologue_epilogue
4304            = !expensive_function_p (count);
4305     }
4306   if (TARGET_PROLOGUE_USING_MOVE
4307       && cfun->machine->use_fast_prologue_epilogue)
4308     frame->save_regs_using_mov = true;
4309   else
4310     frame->save_regs_using_mov = false;
4311
4312
4313   /* Skip return address and saved base pointer.  */
4314   offset = frame_pointer_needed ? UNITS_PER_WORD * 2 : UNITS_PER_WORD;
4315
4316   frame->hard_frame_pointer_offset = offset;
4317
4318   /* Do some sanity checking of stack_alignment_needed and
4319      preferred_alignment, since i386 port is the only using those features
4320      that may break easily.  */
4321
4322   gcc_assert (!size || stack_alignment_needed);
4323   gcc_assert (preferred_alignment >= STACK_BOUNDARY / BITS_PER_UNIT);
4324   gcc_assert (preferred_alignment <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4325   gcc_assert (stack_alignment_needed
4326               <= PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT);
4327
4328   if (stack_alignment_needed < STACK_BOUNDARY / BITS_PER_UNIT)
4329     stack_alignment_needed = STACK_BOUNDARY / BITS_PER_UNIT;
4330
4331   /* Register save area */
4332   offset += frame->nregs * UNITS_PER_WORD;
4333
4334   /* Va-arg area */
4335   if (ix86_save_varrargs_registers)
4336     {
4337       offset += X86_64_VARARGS_SIZE;
4338       frame->va_arg_size = X86_64_VARARGS_SIZE;
4339     }
4340   else
4341     frame->va_arg_size = 0;
4342
4343   /* Align start of frame for local function.  */
4344   frame->padding1 = ((offset + stack_alignment_needed - 1)
4345                      & -stack_alignment_needed) - offset;
4346
4347   offset += frame->padding1;
4348
4349   /* Frame pointer points here.  */
4350   frame->frame_pointer_offset = offset;
4351
4352   offset += size;
4353
4354   /* Add outgoing arguments area.  Can be skipped if we eliminated
4355      all the function calls as dead code.
4356      Skipping is however impossible when function calls alloca.  Alloca
4357      expander assumes that last current_function_outgoing_args_size
4358      of stack frame are unused.  */
4359   if (ACCUMULATE_OUTGOING_ARGS
4360       && (!current_function_is_leaf || current_function_calls_alloca))
4361     {
4362       offset += current_function_outgoing_args_size;
4363       frame->outgoing_arguments_size = current_function_outgoing_args_size;
4364     }
4365   else
4366     frame->outgoing_arguments_size = 0;
4367
4368   /* Align stack boundary.  Only needed if we're calling another function
4369      or using alloca.  */
4370   if (!current_function_is_leaf || current_function_calls_alloca)
4371     frame->padding2 = ((offset + preferred_alignment - 1)
4372                        & -preferred_alignment) - offset;
4373   else
4374     frame->padding2 = 0;
4375
4376   offset += frame->padding2;
4377
4378   /* We've reached end of stack frame.  */
4379   frame->stack_pointer_offset = offset;
4380
4381   /* Size prologue needs to allocate.  */
4382   frame->to_allocate =
4383     (size + frame->padding1 + frame->padding2
4384      + frame->outgoing_arguments_size + frame->va_arg_size);
4385
4386   if ((!frame->to_allocate && frame->nregs <= 1)
4387       || (TARGET_64BIT && frame->to_allocate >= (HOST_WIDE_INT) 0x80000000))
4388     frame->save_regs_using_mov = false;
4389
4390   if (TARGET_RED_ZONE && current_function_sp_is_unchanging
4391       && current_function_is_leaf)
4392     {
4393       frame->red_zone_size = frame->to_allocate;
4394       if (frame->save_regs_using_mov)
4395         frame->red_zone_size += frame->nregs * UNITS_PER_WORD;
4396       if (frame->red_zone_size > RED_ZONE_SIZE - RED_ZONE_RESERVE)
4397         frame->red_zone_size = RED_ZONE_SIZE - RED_ZONE_RESERVE;
4398     }
4399   else
4400     frame->red_zone_size = 0;
4401   frame->to_allocate -= frame->red_zone_size;
4402   frame->stack_pointer_offset -= frame->red_zone_size;
4403 #if 0
4404   fprintf (stderr, "nregs: %i\n", frame->nregs);
4405   fprintf (stderr, "size: %i\n", size);
4406   fprintf (stderr, "alignment1: %i\n", stack_alignment_needed);
4407   fprintf (stderr, "padding1: %i\n", frame->padding1);
4408   fprintf (stderr, "va_arg: %i\n", frame->va_arg_size);
4409   fprintf (stderr, "padding2: %i\n", frame->padding2);
4410   fprintf (stderr, "to_allocate: %i\n", frame->to_allocate);
4411   fprintf (stderr, "red_zone_size: %i\n", frame->red_zone_size);
4412   fprintf (stderr, "frame_pointer_offset: %i\n", frame->frame_pointer_offset);
4413   fprintf (stderr, "hard_frame_pointer_offset: %i\n",
4414            frame->hard_frame_pointer_offset);
4415   fprintf (stderr, "stack_pointer_offset: %i\n", frame->stack_pointer_offset);
4416 #endif
4417 }
4418
4419 /* Emit code to save registers in the prologue.  */
4420
4421 static void
4422 ix86_emit_save_regs (void)
4423 {
4424   int regno;
4425   rtx insn;
4426
4427   for (regno = FIRST_PSEUDO_REGISTER - 1; regno >= 0; regno--)
4428     if (ix86_save_reg (regno, true))
4429       {
4430         insn = emit_insn (gen_push (gen_rtx_REG (Pmode, regno)));
4431         RTX_FRAME_RELATED_P (insn) = 1;
4432       }
4433 }
4434
4435 /* Emit code to save registers using MOV insns.  First register
4436    is restored from POINTER + OFFSET.  */
4437 static void
4438 ix86_emit_save_regs_using_mov (rtx pointer, HOST_WIDE_INT offset)
4439 {
4440   int regno;
4441   rtx insn;
4442
4443   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4444     if (ix86_save_reg (regno, true))
4445       {
4446         insn = emit_move_insn (adjust_address (gen_rtx_MEM (Pmode, pointer),
4447                                                Pmode, offset),
4448                                gen_rtx_REG (Pmode, regno));
4449         RTX_FRAME_RELATED_P (insn) = 1;
4450         offset += UNITS_PER_WORD;
4451       }
4452 }
4453
4454 /* Expand prologue or epilogue stack adjustment.
4455    The pattern exist to put a dependency on all ebp-based memory accesses.
4456    STYLE should be negative if instructions should be marked as frame related,
4457    zero if %r11 register is live and cannot be freely used and positive
4458    otherwise.  */
4459
4460 static void
4461 pro_epilogue_adjust_stack (rtx dest, rtx src, rtx offset, int style)
4462 {
4463   rtx insn;
4464
4465   if (! TARGET_64BIT)
4466     insn = emit_insn (gen_pro_epilogue_adjust_stack_1 (dest, src, offset));
4467   else if (x86_64_immediate_operand (offset, DImode))
4468     insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64 (dest, src, offset));
4469   else
4470     {
4471       rtx r11;
4472       /* r11 is used by indirect sibcall return as well, set before the
4473          epilogue and used after the epilogue.  ATM indirect sibcall
4474          shouldn't be used together with huge frame sizes in one
4475          function because of the frame_size check in sibcall.c.  */
4476       gcc_assert (style);
4477       r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
4478       insn = emit_insn (gen_rtx_SET (DImode, r11, offset));
4479       if (style < 0)
4480         RTX_FRAME_RELATED_P (insn) = 1;
4481       insn = emit_insn (gen_pro_epilogue_adjust_stack_rex64_2 (dest, src, r11,
4482                                                                offset));
4483     }
4484   if (style < 0)
4485     RTX_FRAME_RELATED_P (insn) = 1;
4486 }
4487
4488 /* Expand the prologue into a bunch of separate insns.  */
4489
4490 void
4491 ix86_expand_prologue (void)
4492 {
4493   rtx insn;
4494   bool pic_reg_used;
4495   struct ix86_frame frame;
4496   HOST_WIDE_INT allocate;
4497
4498   ix86_compute_frame_layout (&frame);
4499
4500   /* Note: AT&T enter does NOT have reversed args.  Enter is probably
4501      slower on all targets.  Also sdb doesn't like it.  */
4502
4503   if (frame_pointer_needed)
4504     {
4505       insn = emit_insn (gen_push (hard_frame_pointer_rtx));
4506       RTX_FRAME_RELATED_P (insn) = 1;
4507
4508       insn = emit_move_insn (hard_frame_pointer_rtx, stack_pointer_rtx);
4509       RTX_FRAME_RELATED_P (insn) = 1;
4510     }
4511
4512   allocate = frame.to_allocate;
4513
4514   if (!frame.save_regs_using_mov)
4515     ix86_emit_save_regs ();
4516   else
4517     allocate += frame.nregs * UNITS_PER_WORD;
4518
4519   /* When using red zone we may start register saving before allocating
4520      the stack frame saving one cycle of the prologue.  */
4521   if (TARGET_RED_ZONE && frame.save_regs_using_mov)
4522     ix86_emit_save_regs_using_mov (frame_pointer_needed ? hard_frame_pointer_rtx
4523                                    : stack_pointer_rtx,
4524                                    -frame.nregs * UNITS_PER_WORD);
4525
4526   if (allocate == 0)
4527     ;
4528   else if (! TARGET_STACK_PROBE || allocate < CHECK_STACK_LIMIT)
4529     pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4530                                GEN_INT (-allocate), -1);
4531   else
4532     {
4533       /* Only valid for Win32.  */
4534       rtx eax = gen_rtx_REG (SImode, 0);
4535       bool eax_live = ix86_eax_live_at_start_p ();
4536       rtx t;
4537
4538       gcc_assert (!TARGET_64BIT);
4539
4540       if (eax_live)
4541         {
4542           emit_insn (gen_push (eax));
4543           allocate -= 4;
4544         }
4545
4546       emit_move_insn (eax, GEN_INT (allocate));
4547
4548       insn = emit_insn (gen_allocate_stack_worker (eax));
4549       RTX_FRAME_RELATED_P (insn) = 1;
4550       t = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (-allocate));
4551       t = gen_rtx_SET (VOIDmode, stack_pointer_rtx, t);
4552       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR,
4553                                             t, REG_NOTES (insn));
4554
4555       if (eax_live)
4556         {
4557           if (frame_pointer_needed)
4558             t = plus_constant (hard_frame_pointer_rtx,
4559                                allocate
4560                                - frame.to_allocate
4561                                - frame.nregs * UNITS_PER_WORD);
4562           else
4563             t = plus_constant (stack_pointer_rtx, allocate);
4564           emit_move_insn (eax, gen_rtx_MEM (SImode, t));
4565         }
4566     }
4567
4568   if (frame.save_regs_using_mov && !TARGET_RED_ZONE)
4569     {
4570       if (!frame_pointer_needed || !frame.to_allocate)
4571         ix86_emit_save_regs_using_mov (stack_pointer_rtx, frame.to_allocate);
4572       else
4573         ix86_emit_save_regs_using_mov (hard_frame_pointer_rtx,
4574                                        -frame.nregs * UNITS_PER_WORD);
4575     }
4576
4577   pic_reg_used = false;
4578   if (pic_offset_table_rtx
4579       && (regs_ever_live[REAL_PIC_OFFSET_TABLE_REGNUM]
4580           || current_function_profile))
4581     {
4582       unsigned int alt_pic_reg_used = ix86_select_alt_pic_regnum ();
4583
4584       if (alt_pic_reg_used != INVALID_REGNUM)
4585         REGNO (pic_offset_table_rtx) = alt_pic_reg_used;
4586
4587       pic_reg_used = true;
4588     }
4589
4590   if (pic_reg_used)
4591     {
4592       insn = emit_insn (gen_set_got (pic_offset_table_rtx));
4593
4594       /* Even with accurate pre-reload life analysis, we can wind up
4595          deleting all references to the pic register after reload.
4596          Consider if cross-jumping unifies two sides of a branch
4597          controlled by a comparison vs the only read from a global.
4598          In which case, allow the set_got to be deleted, though we're
4599          too late to do anything about the ebx save in the prologue.  */
4600       REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_MAYBE_DEAD, const0_rtx, NULL);
4601     }
4602
4603   /* Prevent function calls from be scheduled before the call to mcount.
4604      In the pic_reg_used case, make sure that the got load isn't deleted.  */
4605   if (current_function_profile)
4606     emit_insn (gen_blockage (pic_reg_used ? pic_offset_table_rtx : const0_rtx));
4607 }
4608
4609 /* Emit code to restore saved registers using MOV insns.  First register
4610    is restored from POINTER + OFFSET.  */
4611 static void
4612 ix86_emit_restore_regs_using_mov (rtx pointer, HOST_WIDE_INT offset,
4613                                   int maybe_eh_return)
4614 {
4615   int regno;
4616   rtx base_address = gen_rtx_MEM (Pmode, pointer);
4617
4618   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4619     if (ix86_save_reg (regno, maybe_eh_return))
4620       {
4621         /* Ensure that adjust_address won't be forced to produce pointer
4622            out of range allowed by x86-64 instruction set.  */
4623         if (TARGET_64BIT && offset != trunc_int_for_mode (offset, SImode))
4624           {
4625             rtx r11;
4626
4627             r11 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
4628             emit_move_insn (r11, GEN_INT (offset));
4629             emit_insn (gen_adddi3 (r11, r11, pointer));
4630             base_address = gen_rtx_MEM (Pmode, r11);
4631             offset = 0;
4632           }
4633         emit_move_insn (gen_rtx_REG (Pmode, regno),
4634                         adjust_address (base_address, Pmode, offset));
4635         offset += UNITS_PER_WORD;
4636       }
4637 }
4638
4639 /* Restore function stack, frame, and registers.  */
4640
4641 void
4642 ix86_expand_epilogue (int style)
4643 {
4644   int regno;
4645   int sp_valid = !frame_pointer_needed || current_function_sp_is_unchanging;
4646   struct ix86_frame frame;
4647   HOST_WIDE_INT offset;
4648
4649   ix86_compute_frame_layout (&frame);
4650
4651   /* Calculate start of saved registers relative to ebp.  Special care
4652      must be taken for the normal return case of a function using
4653      eh_return: the eax and edx registers are marked as saved, but not
4654      restored along this path.  */
4655   offset = frame.nregs;
4656   if (current_function_calls_eh_return && style != 2)
4657     offset -= 2;
4658   offset *= -UNITS_PER_WORD;
4659
4660   /* If we're only restoring one register and sp is not valid then
4661      using a move instruction to restore the register since it's
4662      less work than reloading sp and popping the register.
4663
4664      The default code result in stack adjustment using add/lea instruction,
4665      while this code results in LEAVE instruction (or discrete equivalent),
4666      so it is profitable in some other cases as well.  Especially when there
4667      are no registers to restore.  We also use this code when TARGET_USE_LEAVE
4668      and there is exactly one register to pop. This heuristic may need some
4669      tuning in future.  */
4670   if ((!sp_valid && frame.nregs <= 1)
4671       || (TARGET_EPILOGUE_USING_MOVE
4672           && cfun->machine->use_fast_prologue_epilogue
4673           && (frame.nregs > 1 || frame.to_allocate))
4674       || (frame_pointer_needed && !frame.nregs && frame.to_allocate)
4675       || (frame_pointer_needed && TARGET_USE_LEAVE
4676           && cfun->machine->use_fast_prologue_epilogue
4677           && frame.nregs == 1)
4678       || current_function_calls_eh_return)
4679     {
4680       /* Restore registers.  We can use ebp or esp to address the memory
4681          locations.  If both are available, default to ebp, since offsets
4682          are known to be small.  Only exception is esp pointing directly to the
4683          end of block of saved registers, where we may simplify addressing
4684          mode.  */
4685
4686       if (!frame_pointer_needed || (sp_valid && !frame.to_allocate))
4687         ix86_emit_restore_regs_using_mov (stack_pointer_rtx,
4688                                           frame.to_allocate, style == 2);
4689       else
4690         ix86_emit_restore_regs_using_mov (hard_frame_pointer_rtx,
4691                                           offset, style == 2);
4692
4693       /* eh_return epilogues need %ecx added to the stack pointer.  */
4694       if (style == 2)
4695         {
4696           rtx tmp, sa = EH_RETURN_STACKADJ_RTX;
4697
4698           if (frame_pointer_needed)
4699             {
4700               tmp = gen_rtx_PLUS (Pmode, hard_frame_pointer_rtx, sa);
4701               tmp = plus_constant (tmp, UNITS_PER_WORD);
4702               emit_insn (gen_rtx_SET (VOIDmode, sa, tmp));
4703
4704               tmp = gen_rtx_MEM (Pmode, hard_frame_pointer_rtx);
4705               emit_move_insn (hard_frame_pointer_rtx, tmp);
4706
4707               pro_epilogue_adjust_stack (stack_pointer_rtx, sa,
4708                                          const0_rtx, style);
4709             }
4710           else
4711             {
4712               tmp = gen_rtx_PLUS (Pmode, stack_pointer_rtx, sa);
4713               tmp = plus_constant (tmp, (frame.to_allocate
4714                                          + frame.nregs * UNITS_PER_WORD));
4715               emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx, tmp));
4716             }
4717         }
4718       else if (!frame_pointer_needed)
4719         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4720                                    GEN_INT (frame.to_allocate
4721                                             + frame.nregs * UNITS_PER_WORD),
4722                                    style);
4723       /* If not an i386, mov & pop is faster than "leave".  */
4724       else if (TARGET_USE_LEAVE || optimize_size
4725                || !cfun->machine->use_fast_prologue_epilogue)
4726         emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4727       else
4728         {
4729           pro_epilogue_adjust_stack (stack_pointer_rtx,
4730                                      hard_frame_pointer_rtx,
4731                                      const0_rtx, style);
4732           if (TARGET_64BIT)
4733             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4734           else
4735             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4736         }
4737     }
4738   else
4739     {
4740       /* First step is to deallocate the stack frame so that we can
4741          pop the registers.  */
4742       if (!sp_valid)
4743         {
4744           gcc_assert (frame_pointer_needed);
4745           pro_epilogue_adjust_stack (stack_pointer_rtx,
4746                                      hard_frame_pointer_rtx,
4747                                      GEN_INT (offset), style);
4748         }
4749       else if (frame.to_allocate)
4750         pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx,
4751                                    GEN_INT (frame.to_allocate), style);
4752
4753       for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
4754         if (ix86_save_reg (regno, false))
4755           {
4756             if (TARGET_64BIT)
4757               emit_insn (gen_popdi1 (gen_rtx_REG (Pmode, regno)));
4758             else
4759               emit_insn (gen_popsi1 (gen_rtx_REG (Pmode, regno)));
4760           }
4761       if (frame_pointer_needed)
4762         {
4763           /* Leave results in shorter dependency chains on CPUs that are
4764              able to grok it fast.  */
4765           if (TARGET_USE_LEAVE)
4766             emit_insn (TARGET_64BIT ? gen_leave_rex64 () : gen_leave ());
4767           else if (TARGET_64BIT)
4768             emit_insn (gen_popdi1 (hard_frame_pointer_rtx));
4769           else
4770             emit_insn (gen_popsi1 (hard_frame_pointer_rtx));
4771         }
4772     }
4773
4774   /* Sibcall epilogues don't want a return instruction.  */
4775   if (style == 0)
4776     return;
4777
4778   if (current_function_pops_args && current_function_args_size)
4779     {
4780       rtx popc = GEN_INT (current_function_pops_args);
4781
4782       /* i386 can only pop 64K bytes.  If asked to pop more, pop
4783          return address, do explicit add, and jump indirectly to the
4784          caller.  */
4785
4786       if (current_function_pops_args >= 65536)
4787         {
4788           rtx ecx = gen_rtx_REG (SImode, 2);
4789
4790           /* There is no "pascal" calling convention in 64bit ABI.  */
4791           gcc_assert (!TARGET_64BIT);
4792
4793           emit_insn (gen_popsi1 (ecx));
4794           emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, popc));
4795           emit_jump_insn (gen_return_indirect_internal (ecx));
4796         }
4797       else
4798         emit_jump_insn (gen_return_pop_internal (popc));
4799     }
4800   else
4801     emit_jump_insn (gen_return_internal ());
4802 }
4803
4804 /* Reset from the function's potential modifications.  */
4805
4806 static void
4807 ix86_output_function_epilogue (FILE *file ATTRIBUTE_UNUSED,
4808                                HOST_WIDE_INT size ATTRIBUTE_UNUSED)
4809 {
4810   if (pic_offset_table_rtx)
4811     REGNO (pic_offset_table_rtx) = REAL_PIC_OFFSET_TABLE_REGNUM;
4812 }
4813 \f
4814 /* Extract the parts of an RTL expression that is a valid memory address
4815    for an instruction.  Return 0 if the structure of the address is
4816    grossly off.  Return -1 if the address contains ASHIFT, so it is not
4817    strictly valid, but still used for computing length of lea instruction.  */
4818
4819 int
4820 ix86_decompose_address (rtx addr, struct ix86_address *out)
4821 {
4822   rtx base = NULL_RTX, index = NULL_RTX, disp = NULL_RTX;
4823   rtx base_reg, index_reg;
4824   HOST_WIDE_INT scale = 1;
4825   rtx scale_rtx = NULL_RTX;
4826   int retval = 1;
4827   enum ix86_address_seg seg = SEG_DEFAULT;
4828
4829   if (GET_CODE (addr) == REG || GET_CODE (addr) == SUBREG)
4830     base = addr;
4831   else if (GET_CODE (addr) == PLUS)
4832     {
4833       rtx addends[4], op;
4834       int n = 0, i;
4835
4836       op = addr;
4837       do
4838         {
4839           if (n >= 4)
4840             return 0;
4841           addends[n++] = XEXP (op, 1);
4842           op = XEXP (op, 0);
4843         }
4844       while (GET_CODE (op) == PLUS);
4845       if (n >= 4)
4846         return 0;
4847       addends[n] = op;
4848
4849       for (i = n; i >= 0; --i)
4850         {
4851           op = addends[i];
4852           switch (GET_CODE (op))
4853             {
4854             case MULT:
4855               if (index)
4856                 return 0;
4857               index = XEXP (op, 0);
4858               scale_rtx = XEXP (op, 1);
4859               break;
4860
4861             case UNSPEC:
4862               if (XINT (op, 1) == UNSPEC_TP
4863                   && TARGET_TLS_DIRECT_SEG_REFS
4864                   && seg == SEG_DEFAULT)
4865                 seg = TARGET_64BIT ? SEG_FS : SEG_GS;
4866               else
4867                 return 0;
4868               break;
4869
4870             case REG:
4871             case SUBREG:
4872               if (!base)
4873                 base = op;
4874               else if (!index)
4875                 index = op;
4876               else
4877                 return 0;
4878               break;
4879
4880             case CONST:
4881             case CONST_INT:
4882             case SYMBOL_REF:
4883             case LABEL_REF:
4884               if (disp)
4885                 return 0;
4886               disp = op;
4887               break;
4888
4889             default:
4890               return 0;
4891             }
4892         }
4893     }
4894   else if (GET_CODE (addr) == MULT)
4895     {
4896       index = XEXP (addr, 0);           /* index*scale */
4897       scale_rtx = XEXP (addr, 1);
4898     }
4899   else if (GET_CODE (addr) == ASHIFT)
4900     {
4901       rtx tmp;
4902
4903       /* We're called for lea too, which implements ashift on occasion.  */
4904       index = XEXP (addr, 0);
4905       tmp = XEXP (addr, 1);
4906       if (GET_CODE (tmp) != CONST_INT)
4907         return 0;
4908       scale = INTVAL (tmp);
4909       if ((unsigned HOST_WIDE_INT) scale > 3)
4910         return 0;
4911       scale = 1 << scale;
4912       retval = -1;
4913     }
4914   else
4915     disp = addr;                        /* displacement */
4916
4917   /* Extract the integral value of scale.  */
4918   if (scale_rtx)
4919     {
4920       if (GET_CODE (scale_rtx) != CONST_INT)
4921         return 0;
4922       scale = INTVAL (scale_rtx);
4923     }
4924
4925   base_reg = base && GET_CODE (base) == SUBREG ? SUBREG_REG (base) : base;
4926   index_reg = index && GET_CODE (index) == SUBREG ? SUBREG_REG (index) : index;
4927
4928   /* Allow arg pointer and stack pointer as index if there is not scaling.  */
4929   if (base_reg && index_reg && scale == 1
4930       && (index_reg == arg_pointer_rtx
4931           || index_reg == frame_pointer_rtx
4932           || (REG_P (index_reg) && REGNO (index_reg) == STACK_POINTER_REGNUM)))
4933     {
4934       rtx tmp;
4935       tmp = base, base = index, index = tmp;
4936       tmp = base_reg, base_reg = index_reg, index_reg = tmp;
4937     }
4938
4939   /* Special case: %ebp cannot be encoded as a base without a displacement.  */
4940   if ((base_reg == hard_frame_pointer_rtx
4941        || base_reg == frame_pointer_rtx
4942        || base_reg == arg_pointer_rtx) && !disp)
4943     disp = const0_rtx;
4944
4945   /* Special case: on K6, [%esi] makes the instruction vector decoded.
4946      Avoid this by transforming to [%esi+0].  */
4947   if (ix86_tune == PROCESSOR_K6 && !optimize_size
4948       && base_reg && !index_reg && !disp
4949       && REG_P (base_reg)
4950       && REGNO_REG_CLASS (REGNO (base_reg)) == SIREG)
4951     disp = const0_rtx;
4952
4953   /* Special case: encode reg+reg instead of reg*2.  */
4954   if (!base && index && scale && scale == 2)
4955     base = index, base_reg = index_reg, scale = 1;
4956
4957   /* Special case: scaling cannot be encoded without base or displacement.  */
4958   if (!base && !disp && index && scale != 1)
4959     disp = const0_rtx;
4960
4961   out->base = base;
4962   out->index = index;
4963   out->disp = disp;
4964   out->scale = scale;
4965   out->seg = seg;
4966
4967   return retval;
4968 }
4969 \f
4970 /* Return cost of the memory address x.
4971    For i386, it is better to use a complex address than let gcc copy
4972    the address into a reg and make a new pseudo.  But not if the address
4973    requires to two regs - that would mean more pseudos with longer
4974    lifetimes.  */
4975 static int
4976 ix86_address_cost (rtx x)
4977 {
4978   struct ix86_address parts;
4979   int cost = 1;
4980   int ok = ix86_decompose_address (x, &parts);
4981
4982   gcc_assert (ok);
4983
4984   if (parts.base && GET_CODE (parts.base) == SUBREG)
4985     parts.base = SUBREG_REG (parts.base);
4986   if (parts.index && GET_CODE (parts.index) == SUBREG)
4987     parts.index = SUBREG_REG (parts.index);
4988
4989   /* More complex memory references are better.  */
4990   if (parts.disp && parts.disp != const0_rtx)
4991     cost--;
4992   if (parts.seg != SEG_DEFAULT)
4993     cost--;
4994
4995   /* Attempt to minimize number of registers in the address.  */
4996   if ((parts.base
4997        && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER))
4998       || (parts.index
4999           && (!REG_P (parts.index)
5000               || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)))
5001     cost++;
5002
5003   if (parts.base
5004       && (!REG_P (parts.base) || REGNO (parts.base) >= FIRST_PSEUDO_REGISTER)
5005       && parts.index
5006       && (!REG_P (parts.index) || REGNO (parts.index) >= FIRST_PSEUDO_REGISTER)
5007       && parts.base != parts.index)
5008     cost++;
5009
5010   /* AMD-K6 don't like addresses with ModR/M set to 00_xxx_100b,
5011      since it's predecode logic can't detect the length of instructions
5012      and it degenerates to vector decoded.  Increase cost of such
5013      addresses here.  The penalty is minimally 2 cycles.  It may be worthwhile
5014      to split such addresses or even refuse such addresses at all.
5015
5016      Following addressing modes are affected:
5017       [base+scale*index]
5018       [scale*index+disp]
5019       [base+index]
5020
5021      The first and last case  may be avoidable by explicitly coding the zero in
5022      memory address, but I don't have AMD-K6 machine handy to check this
5023      theory.  */
5024
5025   if (TARGET_K6
5026       && ((!parts.disp && parts.base && parts.index && parts.scale != 1)
5027           || (parts.disp && !parts.base && parts.index && parts.scale != 1)
5028           || (!parts.disp && parts.base && parts.index && parts.scale == 1)))
5029     cost += 10;
5030
5031   return cost;
5032 }
5033 \f
5034 /* If X is a machine specific address (i.e. a symbol or label being
5035    referenced as a displacement from the GOT implemented using an
5036    UNSPEC), then return the base term.  Otherwise return X.  */
5037
5038 rtx
5039 ix86_find_base_term (rtx x)
5040 {
5041   rtx term;
5042
5043   if (TARGET_64BIT)
5044     {
5045       if (GET_CODE (x) != CONST)
5046         return x;
5047       term = XEXP (x, 0);
5048       if (GET_CODE (term) == PLUS
5049           && (GET_CODE (XEXP (term, 1)) == CONST_INT
5050               || GET_CODE (XEXP (term, 1)) == CONST_DOUBLE))
5051         term = XEXP (term, 0);
5052       if (GET_CODE (term) != UNSPEC
5053           || XINT (term, 1) != UNSPEC_GOTPCREL)
5054         return x;
5055
5056       term = XVECEXP (term, 0, 0);
5057
5058       if (GET_CODE (term) != SYMBOL_REF
5059           && GET_CODE (term) != LABEL_REF)
5060         return x;
5061
5062       return term;
5063     }
5064
5065   term = ix86_delegitimize_address (x);
5066
5067   if (GET_CODE (term) != SYMBOL_REF
5068       && GET_CODE (term) != LABEL_REF)
5069     return x;
5070
5071   return term;
5072 }
5073
5074 /* Allow {LABEL | SYMBOL}_REF - SYMBOL_REF-FOR-PICBASE for Mach-O as
5075    this is used for to form addresses to local data when -fPIC is in
5076    use.  */
5077
5078 static bool
5079 darwin_local_data_pic (rtx disp)
5080 {
5081   if (GET_CODE (disp) == MINUS)
5082     {
5083       if (GET_CODE (XEXP (disp, 0)) == LABEL_REF
5084           || GET_CODE (XEXP (disp, 0)) == SYMBOL_REF)
5085         if (GET_CODE (XEXP (disp, 1)) == SYMBOL_REF)
5086           {
5087             const char *sym_name = XSTR (XEXP (disp, 1), 0);
5088             if (! strcmp (sym_name, "<pic base>"))
5089               return true;
5090           }
5091     }
5092
5093   return false;
5094 }
5095 \f
5096 /* Determine if a given RTX is a valid constant.  We already know this
5097    satisfies CONSTANT_P.  */
5098
5099 bool
5100 legitimate_constant_p (rtx x)
5101 {
5102   switch (GET_CODE (x))
5103     {
5104     case CONST:
5105       x = XEXP (x, 0);
5106
5107       if (GET_CODE (x) == PLUS)
5108         {
5109           if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5110             return false;
5111           x = XEXP (x, 0);
5112         }
5113
5114       if (TARGET_MACHO && darwin_local_data_pic (x))
5115         return true;
5116
5117       /* Only some unspecs are valid as "constants".  */
5118       if (GET_CODE (x) == UNSPEC)
5119         switch (XINT (x, 1))
5120           {
5121           case UNSPEC_TPOFF:
5122           case UNSPEC_NTPOFF:
5123             return local_exec_symbolic_operand (XVECEXP (x, 0, 0), Pmode);
5124           case UNSPEC_DTPOFF:
5125             return local_dynamic_symbolic_operand (XVECEXP (x, 0, 0), Pmode);
5126           default:
5127             return false;
5128           }
5129
5130       /* We must have drilled down to a symbol.  */
5131       if (!symbolic_operand (x, Pmode))
5132         return false;
5133       /* FALLTHRU */
5134
5135     case SYMBOL_REF:
5136       /* TLS symbols are never valid.  */
5137       if (tls_symbolic_operand (x, Pmode))
5138         return false;
5139       break;
5140
5141     default:
5142       break;
5143     }
5144
5145   /* Otherwise we handle everything else in the move patterns.  */
5146   return true;
5147 }
5148
5149 /* Determine if it's legal to put X into the constant pool.  This
5150    is not possible for the address of thread-local symbols, which
5151    is checked above.  */
5152
5153 static bool
5154 ix86_cannot_force_const_mem (rtx x)
5155 {
5156   return !legitimate_constant_p (x);
5157 }
5158
5159 /* Determine if a given RTX is a valid constant address.  */
5160
5161 bool
5162 constant_address_p (rtx x)
5163 {
5164   return CONSTANT_P (x) && legitimate_address_p (Pmode, x, 1);
5165 }
5166
5167 /* Nonzero if the constant value X is a legitimate general operand
5168    when generating PIC code.  It is given that flag_pic is on and
5169    that X satisfies CONSTANT_P or is a CONST_DOUBLE.  */
5170
5171 bool
5172 legitimate_pic_operand_p (rtx x)
5173 {
5174   rtx inner;
5175
5176   switch (GET_CODE (x))
5177     {
5178     case CONST:
5179       inner = XEXP (x, 0);
5180
5181       /* Only some unspecs are valid as "constants".  */
5182       if (GET_CODE (inner) == UNSPEC)
5183         switch (XINT (inner, 1))
5184           {
5185           case UNSPEC_TPOFF:
5186             return local_exec_symbolic_operand (XVECEXP (inner, 0, 0), Pmode);
5187           default:
5188             return false;
5189           }
5190       /* FALLTHRU */
5191
5192     case SYMBOL_REF:
5193     case LABEL_REF:
5194       return legitimate_pic_address_disp_p (x);
5195
5196     default:
5197       return true;
5198     }
5199 }
5200
5201 /* Determine if a given CONST RTX is a valid memory displacement
5202    in PIC mode.  */
5203
5204 int
5205 legitimate_pic_address_disp_p (rtx disp)
5206 {
5207   bool saw_plus;
5208
5209   /* In 64bit mode we can allow direct addresses of symbols and labels
5210      when they are not dynamic symbols.  */
5211   if (TARGET_64BIT)
5212     {
5213       /* TLS references should always be enclosed in UNSPEC.  */
5214       if (tls_symbolic_operand (disp, GET_MODE (disp)))
5215         return 0;
5216       if (GET_CODE (disp) == SYMBOL_REF
5217           && ix86_cmodel == CM_SMALL_PIC
5218           && SYMBOL_REF_LOCAL_P (disp))
5219         return 1;
5220       if (GET_CODE (disp) == LABEL_REF)
5221         return 1;
5222       if (GET_CODE (disp) == CONST
5223           && GET_CODE (XEXP (disp, 0)) == PLUS)
5224         {
5225           rtx op0 = XEXP (XEXP (disp, 0), 0);
5226           rtx op1 = XEXP (XEXP (disp, 0), 1);
5227
5228           /* TLS references should always be enclosed in UNSPEC.  */
5229           if (tls_symbolic_operand (op0, GET_MODE (op0)))
5230             return 0;
5231           if (((GET_CODE (op0) == SYMBOL_REF
5232                 && ix86_cmodel == CM_SMALL_PIC
5233                 && SYMBOL_REF_LOCAL_P (op0))
5234                || GET_CODE (op0) == LABEL_REF)
5235               && GET_CODE (op1) == CONST_INT
5236               && INTVAL (op1) < 16*1024*1024
5237               && INTVAL (op1) >= -16*1024*1024)
5238             return 1;
5239         }
5240     }
5241   if (GET_CODE (disp) != CONST)
5242     return 0;
5243   disp = XEXP (disp, 0);
5244
5245   if (TARGET_64BIT)
5246     {
5247       /* We are unsafe to allow PLUS expressions.  This limit allowed distance
5248          of GOT tables.  We should not need these anyway.  */
5249       if (GET_CODE (disp) != UNSPEC
5250           || XINT (disp, 1) != UNSPEC_GOTPCREL)
5251         return 0;
5252
5253       if (GET_CODE (XVECEXP (disp, 0, 0)) != SYMBOL_REF
5254           && GET_CODE (XVECEXP (disp, 0, 0)) != LABEL_REF)
5255         return 0;
5256       return 1;
5257     }
5258
5259   saw_plus = false;
5260   if (GET_CODE (disp) == PLUS)
5261     {
5262       if (GET_CODE (XEXP (disp, 1)) != CONST_INT)
5263         return 0;
5264       disp = XEXP (disp, 0);
5265       saw_plus = true;
5266     }
5267
5268   if (TARGET_MACHO && darwin_local_data_pic (disp))
5269     return 1;
5270
5271   if (GET_CODE (disp) != UNSPEC)
5272     return 0;
5273
5274   switch (XINT (disp, 1))
5275     {
5276     case UNSPEC_GOT:
5277       if (saw_plus)
5278         return false;
5279       return GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF;
5280     case UNSPEC_GOTOFF:
5281       if (GET_CODE (XVECEXP (disp, 0, 0)) == SYMBOL_REF
5282           || GET_CODE (XVECEXP (disp, 0, 0)) == LABEL_REF)
5283         return local_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5284       return false;
5285     case UNSPEC_GOTTPOFF:
5286     case UNSPEC_GOTNTPOFF:
5287     case UNSPEC_INDNTPOFF:
5288       if (saw_plus)
5289         return false;
5290       return initial_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5291     case UNSPEC_NTPOFF:
5292       return local_exec_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5293     case UNSPEC_DTPOFF:
5294       return local_dynamic_symbolic_operand (XVECEXP (disp, 0, 0), Pmode);
5295     }
5296
5297   return 0;
5298 }
5299
5300 /* GO_IF_LEGITIMATE_ADDRESS recognizes an RTL expression that is a valid
5301    memory address for an instruction.  The MODE argument is the machine mode
5302    for the MEM expression that wants to use this address.
5303
5304    It only recognizes address in canonical form.  LEGITIMIZE_ADDRESS should
5305    convert common non-canonical forms to canonical form so that they will
5306    be recognized.  */
5307
5308 int
5309 legitimate_address_p (enum machine_mode mode, rtx addr, int strict)
5310 {
5311   struct ix86_address parts;
5312   rtx base, index, disp;
5313   HOST_WIDE_INT scale;
5314   const char *reason = NULL;
5315   rtx reason_rtx = NULL_RTX;
5316
5317   if (TARGET_DEBUG_ADDR)
5318     {
5319       fprintf (stderr,
5320                "\n======\nGO_IF_LEGITIMATE_ADDRESS, mode = %s, strict = %d\n",
5321                GET_MODE_NAME (mode), strict);
5322       debug_rtx (addr);
5323     }
5324
5325   if (ix86_decompose_address (addr, &parts) <= 0)
5326     {
5327       reason = "decomposition failed";
5328       goto report_error;
5329     }
5330
5331   base = parts.base;
5332   index = parts.index;
5333   disp = parts.disp;
5334   scale = parts.scale;
5335
5336   /* Validate base register.
5337
5338      Don't allow SUBREG's that span more than a word here.  It can lead to spill
5339      failures when the base is one word out of a two word structure, which is
5340      represented internally as a DImode int.  */
5341
5342   if (base)
5343     {
5344       rtx reg;
5345       reason_rtx = base;
5346   
5347       if (REG_P (base))
5348         reg = base;
5349       else if (GET_CODE (base) == SUBREG
5350                && REG_P (SUBREG_REG (base))
5351                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (base)))
5352                   <= UNITS_PER_WORD)
5353         reg = SUBREG_REG (base);
5354       else
5355         {
5356           reason = "base is not a register";
5357           goto report_error;
5358         }
5359
5360       if (GET_MODE (base) != Pmode)
5361         {
5362           reason = "base is not in Pmode";
5363           goto report_error;
5364         }
5365
5366       if ((strict && ! REG_OK_FOR_BASE_STRICT_P (reg))
5367           || (! strict && ! REG_OK_FOR_BASE_NONSTRICT_P (reg)))
5368         {
5369           reason = "base is not valid";
5370           goto report_error;
5371         }
5372     }
5373
5374   /* Validate index register.
5375
5376      Don't allow SUBREG's that span more than a word here -- same as above.  */
5377
5378   if (index)
5379     {
5380       rtx reg;
5381       reason_rtx = index;
5382
5383       if (REG_P (index))
5384         reg = index;
5385       else if (GET_CODE (index) == SUBREG
5386                && REG_P (SUBREG_REG (index))
5387                && GET_MODE_SIZE (GET_MODE (SUBREG_REG (index)))
5388                   <= UNITS_PER_WORD)
5389         reg = SUBREG_REG (index);
5390       else
5391         {
5392           reason = "index is not a register";
5393           goto report_error;
5394         }
5395
5396       if (GET_MODE (index) != Pmode)
5397         {
5398           reason = "index is not in Pmode";
5399           goto report_error;
5400         }
5401
5402       if ((strict && ! REG_OK_FOR_INDEX_STRICT_P (reg))
5403           || (! strict && ! REG_OK_FOR_INDEX_NONSTRICT_P (reg)))
5404         {
5405           reason = "index is not valid";
5406           goto report_error;
5407         }
5408     }
5409
5410   /* Validate scale factor.  */
5411   if (scale != 1)
5412     {
5413       reason_rtx = GEN_INT (scale);
5414       if (!index)
5415         {
5416           reason = "scale without index";
5417           goto report_error;
5418         }
5419
5420       if (scale != 2 && scale != 4 && scale != 8)
5421         {
5422           reason = "scale is not a valid multiplier";
5423           goto report_error;
5424         }
5425     }
5426
5427   /* Validate displacement.  */
5428   if (disp)
5429     {
5430       reason_rtx = disp;
5431
5432       if (GET_CODE (disp) == CONST
5433           && GET_CODE (XEXP (disp, 0)) == UNSPEC)
5434         switch (XINT (XEXP (disp, 0), 1))
5435           {
5436           case UNSPEC_GOT:
5437           case UNSPEC_GOTOFF:
5438           case UNSPEC_GOTPCREL:
5439             gcc_assert (flag_pic);
5440             goto is_legitimate_pic;
5441
5442           case UNSPEC_GOTTPOFF:
5443           case UNSPEC_GOTNTPOFF:
5444           case UNSPEC_INDNTPOFF:
5445           case UNSPEC_NTPOFF:
5446           case UNSPEC_DTPOFF:
5447             break;
5448
5449           default:
5450             reason = "invalid address unspec";
5451             goto report_error;
5452           }
5453
5454       else if (flag_pic && (SYMBOLIC_CONST (disp)
5455 #if TARGET_MACHO
5456                             && !machopic_operand_p (disp)
5457 #endif
5458                             ))
5459         {
5460         is_legitimate_pic:
5461           if (TARGET_64BIT && (index || base))
5462             {
5463               /* foo@dtpoff(%rX) is ok.  */
5464               if (GET_CODE (disp) != CONST
5465                   || GET_CODE (XEXP (disp, 0)) != PLUS
5466                   || GET_CODE (XEXP (XEXP (disp, 0), 0)) != UNSPEC
5467                   || GET_CODE (XEXP (XEXP (disp, 0), 1)) != CONST_INT
5468                   || (XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_DTPOFF
5469                       && XINT (XEXP (XEXP (disp, 0), 0), 1) != UNSPEC_NTPOFF))
5470                 {
5471                   reason = "non-constant pic memory reference";
5472                   goto report_error;
5473                 }
5474             }
5475           else if (! legitimate_pic_address_disp_p (disp))
5476             {
5477               reason = "displacement is an invalid pic construct";
5478               goto report_error;
5479             }
5480
5481           /* This code used to verify that a symbolic pic displacement
5482              includes the pic_offset_table_rtx register.
5483
5484              While this is good idea, unfortunately these constructs may
5485              be created by "adds using lea" optimization for incorrect
5486              code like:
5487
5488              int a;
5489              int foo(int i)
5490                {
5491                  return *(&a+i);
5492                }
5493
5494              This code is nonsensical, but results in addressing
5495              GOT table with pic_offset_table_rtx base.  We can't
5496              just refuse it easily, since it gets matched by
5497              "addsi3" pattern, that later gets split to lea in the
5498              case output register differs from input.  While this
5499              can be handled by separate addsi pattern for this case
5500              that never results in lea, this seems to be easier and
5501              correct fix for crash to disable this test.  */
5502         }
5503       else if (GET_CODE (disp) != LABEL_REF
5504                && GET_CODE (disp) != CONST_INT
5505                && (GET_CODE (disp) != CONST
5506                    || !legitimate_constant_p (disp))
5507                && (GET_CODE (disp) != SYMBOL_REF
5508                    || !legitimate_constant_p (disp)))
5509         {
5510           reason = "displacement is not constant";
5511           goto report_error;
5512         }
5513       else if (TARGET_64BIT
5514                && !x86_64_immediate_operand (disp, VOIDmode))
5515         {
5516           reason = "displacement is out of range";
5517           goto report_error;
5518         }
5519     }
5520
5521   /* Everything looks valid.  */
5522   if (TARGET_DEBUG_ADDR)
5523     fprintf (stderr, "Success.\n");
5524   return TRUE;
5525
5526  report_error:
5527   if (TARGET_DEBUG_ADDR)
5528     {
5529       fprintf (stderr, "Error: %s\n", reason);
5530       debug_rtx (reason_rtx);
5531     }
5532   return FALSE;
5533 }
5534 \f
5535 /* Return an unique alias set for the GOT.  */
5536
5537 static HOST_WIDE_INT
5538 ix86_GOT_alias_set (void)
5539 {
5540   static HOST_WIDE_INT set = -1;
5541   if (set == -1)
5542     set = new_alias_set ();
5543   return set;
5544 }
5545
5546 /* Return a legitimate reference for ORIG (an address) using the
5547    register REG.  If REG is 0, a new pseudo is generated.
5548
5549    There are two types of references that must be handled:
5550
5551    1. Global data references must load the address from the GOT, via
5552       the PIC reg.  An insn is emitted to do this load, and the reg is
5553       returned.
5554
5555    2. Static data references, constant pool addresses, and code labels
5556       compute the address as an offset from the GOT, whose base is in
5557       the PIC reg.  Static data objects have SYMBOL_FLAG_LOCAL set to
5558       differentiate them from global data objects.  The returned
5559       address is the PIC reg + an unspec constant.
5560
5561    GO_IF_LEGITIMATE_ADDRESS rejects symbolic references unless the PIC
5562    reg also appears in the address.  */
5563
5564 static rtx
5565 legitimize_pic_address (rtx orig, rtx reg)
5566 {
5567   rtx addr = orig;
5568   rtx new = orig;
5569   rtx base;
5570
5571 #if TARGET_MACHO
5572   if (reg == 0)
5573     reg = gen_reg_rtx (Pmode);
5574   /* Use the generic Mach-O PIC machinery.  */
5575   return machopic_legitimize_pic_address (orig, GET_MODE (orig), reg);
5576 #endif
5577
5578   if (TARGET_64BIT && legitimate_pic_address_disp_p (addr))
5579     new = addr;
5580   else if (!TARGET_64BIT && local_symbolic_operand (addr, Pmode))
5581     {
5582       /* This symbol may be referenced via a displacement from the PIC
5583          base address (@GOTOFF).  */
5584
5585       if (reload_in_progress)
5586         regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5587       if (GET_CODE (addr) == CONST)
5588         addr = XEXP (addr, 0);
5589       if (GET_CODE (addr) == PLUS)
5590           {
5591             new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, XEXP (addr, 0)), UNSPEC_GOTOFF);
5592             new = gen_rtx_PLUS (Pmode, new, XEXP (addr, 1));
5593           }
5594         else
5595           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTOFF);
5596       new = gen_rtx_CONST (Pmode, new);
5597       new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5598
5599       if (reg != 0)
5600         {
5601           emit_move_insn (reg, new);
5602           new = reg;
5603         }
5604     }
5605   else if (GET_CODE (addr) == SYMBOL_REF)
5606     {
5607       if (TARGET_64BIT)
5608         {
5609           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOTPCREL);
5610           new = gen_rtx_CONST (Pmode, new);
5611           new = gen_const_mem (Pmode, new);
5612           set_mem_alias_set (new, ix86_GOT_alias_set ());
5613
5614           if (reg == 0)
5615             reg = gen_reg_rtx (Pmode);
5616           /* Use directly gen_movsi, otherwise the address is loaded
5617              into register for CSE.  We don't want to CSE this addresses,
5618              instead we CSE addresses from the GOT table, so skip this.  */
5619           emit_insn (gen_movsi (reg, new));
5620           new = reg;
5621         }
5622       else
5623         {
5624           /* This symbol must be referenced via a load from the
5625              Global Offset Table (@GOT).  */
5626
5627           if (reload_in_progress)
5628             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5629           new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, addr), UNSPEC_GOT);
5630           new = gen_rtx_CONST (Pmode, new);
5631           new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5632           new = gen_const_mem (Pmode, new);
5633           set_mem_alias_set (new, ix86_GOT_alias_set ());
5634
5635           if (reg == 0)
5636             reg = gen_reg_rtx (Pmode);
5637           emit_move_insn (reg, new);
5638           new = reg;
5639         }
5640     }
5641   else
5642     {
5643       if (GET_CODE (addr) == CONST)
5644         {
5645           addr = XEXP (addr, 0);
5646
5647           /* We must match stuff we generate before.  Assume the only
5648              unspecs that can get here are ours.  Not that we could do
5649              anything with them anyway....  */
5650           if (GET_CODE (addr) == UNSPEC
5651               || (GET_CODE (addr) == PLUS
5652                   && GET_CODE (XEXP (addr, 0)) == UNSPEC))
5653             return orig;
5654           gcc_assert (GET_CODE (addr) == PLUS);
5655         }
5656       if (GET_CODE (addr) == PLUS)
5657         {
5658           rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
5659
5660           /* Check first to see if this is a constant offset from a @GOTOFF
5661              symbol reference.  */
5662           if (local_symbolic_operand (op0, Pmode)
5663               && GET_CODE (op1) == CONST_INT)
5664             {
5665               if (!TARGET_64BIT)
5666                 {
5667                   if (reload_in_progress)
5668                     regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5669                   new = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, op0),
5670                                         UNSPEC_GOTOFF);
5671                   new = gen_rtx_PLUS (Pmode, new, op1);
5672                   new = gen_rtx_CONST (Pmode, new);
5673                   new = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, new);
5674
5675                   if (reg != 0)
5676                     {
5677                       emit_move_insn (reg, new);
5678                       new = reg;
5679                     }
5680                 }
5681               else
5682                 {
5683                   if (INTVAL (op1) < -16*1024*1024
5684                       || INTVAL (op1) >= 16*1024*1024)
5685                     new = gen_rtx_PLUS (Pmode, force_reg (Pmode, op0), op1);
5686                 }
5687             }
5688           else
5689             {
5690               base = legitimize_pic_address (XEXP (addr, 0), reg);
5691               new  = legitimize_pic_address (XEXP (addr, 1),
5692                                              base == reg ? NULL_RTX : reg);
5693
5694               if (GET_CODE (new) == CONST_INT)
5695                 new = plus_constant (base, INTVAL (new));
5696               else
5697                 {
5698                   if (GET_CODE (new) == PLUS && CONSTANT_P (XEXP (new, 1)))
5699                     {
5700                       base = gen_rtx_PLUS (Pmode, base, XEXP (new, 0));
5701                       new = XEXP (new, 1);
5702                     }
5703                   new = gen_rtx_PLUS (Pmode, base, new);
5704                 }
5705             }
5706         }
5707     }
5708   return new;
5709 }
5710 \f
5711 /* Load the thread pointer.  If TO_REG is true, force it into a register.  */
5712
5713 static rtx
5714 get_thread_pointer (int to_reg)
5715 {
5716   rtx tp, reg, insn;
5717
5718   tp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, const0_rtx), UNSPEC_TP);
5719   if (!to_reg)
5720     return tp;
5721
5722   reg = gen_reg_rtx (Pmode);
5723   insn = gen_rtx_SET (VOIDmode, reg, tp);
5724   insn = emit_insn (insn);
5725
5726   return reg;
5727 }
5728
5729 /* A subroutine of legitimize_address and ix86_expand_move.  FOR_MOV is
5730    false if we expect this to be used for a memory address and true if
5731    we expect to load the address into a register.  */
5732
5733 static rtx
5734 legitimize_tls_address (rtx x, enum tls_model model, int for_mov)
5735 {
5736   rtx dest, base, off, pic;
5737   int type;
5738
5739   switch (model)
5740     {
5741     case TLS_MODEL_GLOBAL_DYNAMIC:
5742       dest = gen_reg_rtx (Pmode);
5743       if (TARGET_64BIT)
5744         {
5745           rtx rax = gen_rtx_REG (Pmode, 0), insns;
5746
5747           start_sequence ();
5748           emit_call_insn (gen_tls_global_dynamic_64 (rax, x));
5749           insns = get_insns ();
5750           end_sequence ();
5751
5752           emit_libcall_block (insns, dest, rax, x);
5753         }
5754       else
5755         emit_insn (gen_tls_global_dynamic_32 (dest, x));
5756       break;
5757
5758     case TLS_MODEL_LOCAL_DYNAMIC:
5759       base = gen_reg_rtx (Pmode);
5760       if (TARGET_64BIT)
5761         {
5762           rtx rax = gen_rtx_REG (Pmode, 0), insns, note;
5763
5764           start_sequence ();
5765           emit_call_insn (gen_tls_local_dynamic_base_64 (rax));
5766           insns = get_insns ();
5767           end_sequence ();
5768
5769           note = gen_rtx_EXPR_LIST (VOIDmode, const0_rtx, NULL);
5770           note = gen_rtx_EXPR_LIST (VOIDmode, ix86_tls_get_addr (), note);
5771           emit_libcall_block (insns, base, rax, note);
5772         }
5773       else
5774         emit_insn (gen_tls_local_dynamic_base_32 (base));
5775
5776       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), UNSPEC_DTPOFF);
5777       off = gen_rtx_CONST (Pmode, off);
5778
5779       return gen_rtx_PLUS (Pmode, base, off);
5780
5781     case TLS_MODEL_INITIAL_EXEC:
5782       if (TARGET_64BIT)
5783         {
5784           pic = NULL;
5785           type = UNSPEC_GOTNTPOFF;
5786         }
5787       else if (flag_pic)
5788         {
5789           if (reload_in_progress)
5790             regs_ever_live[PIC_OFFSET_TABLE_REGNUM] = 1;
5791           pic = pic_offset_table_rtx;
5792           type = TARGET_GNU_TLS ? UNSPEC_GOTNTPOFF : UNSPEC_GOTTPOFF;
5793         }
5794       else if (!TARGET_GNU_TLS)
5795         {
5796           pic = gen_reg_rtx (Pmode);
5797           emit_insn (gen_set_got (pic));
5798           type = UNSPEC_GOTTPOFF;
5799         }
5800       else
5801         {
5802           pic = NULL;
5803           type = UNSPEC_INDNTPOFF;
5804         }
5805
5806       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x), type);
5807       off = gen_rtx_CONST (Pmode, off);
5808       if (pic)
5809         off = gen_rtx_PLUS (Pmode, pic, off);
5810       off = gen_const_mem (Pmode, off);
5811       set_mem_alias_set (off, ix86_GOT_alias_set ());
5812
5813       if (TARGET_64BIT || TARGET_GNU_TLS)
5814         {
5815           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
5816           off = force_reg (Pmode, off);
5817           return gen_rtx_PLUS (Pmode, base, off);
5818         }
5819       else
5820         {
5821           base = get_thread_pointer (true);
5822           dest = gen_reg_rtx (Pmode);
5823           emit_insn (gen_subsi3 (dest, base, off));
5824         }
5825       break;
5826
5827     case TLS_MODEL_LOCAL_EXEC:
5828       off = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, x),
5829                             (TARGET_64BIT || TARGET_GNU_TLS)
5830                             ? UNSPEC_NTPOFF : UNSPEC_TPOFF);
5831       off = gen_rtx_CONST (Pmode, off);
5832
5833       if (TARGET_64BIT || TARGET_GNU_TLS)
5834         {
5835           base = get_thread_pointer (for_mov || !TARGET_TLS_DIRECT_SEG_REFS);
5836           return gen_rtx_PLUS (Pmode, base, off);
5837         }
5838       else
5839         {
5840           base = get_thread_pointer (true);
5841           dest = gen_reg_rtx (Pmode);
5842           emit_insn (gen_subsi3 (dest, base, off));
5843         }
5844       break;
5845
5846     default:
5847       gcc_unreachable ();
5848     }
5849
5850   return dest;
5851 }
5852
5853 /* Try machine-dependent ways of modifying an illegitimate address
5854    to be legitimate.  If we find one, return the new, valid address.
5855    This macro is used in only one place: `memory_address' in explow.c.
5856
5857    OLDX is the address as it was before break_out_memory_refs was called.
5858    In some cases it is useful to look at this to decide what needs to be done.
5859
5860    MODE and WIN are passed so that this macro can use
5861    GO_IF_LEGITIMATE_ADDRESS.
5862
5863    It is always safe for this macro to do nothing.  It exists to recognize
5864    opportunities to optimize the output.
5865
5866    For the 80386, we handle X+REG by loading X into a register R and
5867    using R+REG.  R will go in a general reg and indexing will be used.
5868    However, if REG is a broken-out memory address or multiplication,
5869    nothing needs to be done because REG can certainly go in a general reg.
5870
5871    When -fpic is used, special handling is needed for symbolic references.
5872    See comments by legitimize_pic_address in i386.c for details.  */
5873
5874 rtx
5875 legitimize_address (rtx x, rtx oldx ATTRIBUTE_UNUSED, enum machine_mode mode)
5876 {
5877   int changed = 0;
5878   unsigned log;
5879
5880   if (TARGET_DEBUG_ADDR)
5881     {
5882       fprintf (stderr, "\n==========\nLEGITIMIZE_ADDRESS, mode = %s\n",
5883                GET_MODE_NAME (mode));
5884       debug_rtx (x);
5885     }
5886
5887   log = GET_CODE (x) == SYMBOL_REF ? SYMBOL_REF_TLS_MODEL (x) : 0;
5888   if (log)
5889     return legitimize_tls_address (x, log, false);
5890   if (GET_CODE (x) == CONST
5891       && GET_CODE (XEXP (x, 0)) == PLUS
5892       && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
5893       && (log = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0))))
5894     {
5895       rtx t = legitimize_tls_address (XEXP (XEXP (x, 0), 0), log, false);
5896       return gen_rtx_PLUS (Pmode, t, XEXP (XEXP (x, 0), 1));
5897     }
5898
5899   if (flag_pic && SYMBOLIC_CONST (x))
5900     return legitimize_pic_address (x, 0);
5901
5902   /* Canonicalize shifts by 0, 1, 2, 3 into multiply */
5903   if (GET_CODE (x) == ASHIFT
5904       && GET_CODE (XEXP (x, 1)) == CONST_INT
5905       && (log = (unsigned) exact_log2 (INTVAL (XEXP (x, 1)))) < 4)
5906     {
5907       changed = 1;
5908       x = gen_rtx_MULT (Pmode, force_reg (Pmode, XEXP (x, 0)),
5909                         GEN_INT (1 << log));
5910     }
5911
5912   if (GET_CODE (x) == PLUS)
5913     {
5914       /* Canonicalize shifts by 0, 1, 2, 3 into multiply.  */
5915
5916       if (GET_CODE (XEXP (x, 0)) == ASHIFT
5917           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5918           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) < 4)
5919         {
5920           changed = 1;
5921           XEXP (x, 0) = gen_rtx_MULT (Pmode,
5922                                       force_reg (Pmode, XEXP (XEXP (x, 0), 0)),
5923                                       GEN_INT (1 << log));
5924         }
5925
5926       if (GET_CODE (XEXP (x, 1)) == ASHIFT
5927           && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5928           && (log = (unsigned) exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1)))) < 4)
5929         {
5930           changed = 1;
5931           XEXP (x, 1) = gen_rtx_MULT (Pmode,
5932                                       force_reg (Pmode, XEXP (XEXP (x, 1), 0)),
5933                                       GEN_INT (1 << log));
5934         }
5935
5936       /* Put multiply first if it isn't already.  */
5937       if (GET_CODE (XEXP (x, 1)) == MULT)
5938         {
5939           rtx tmp = XEXP (x, 0);
5940           XEXP (x, 0) = XEXP (x, 1);
5941           XEXP (x, 1) = tmp;
5942           changed = 1;
5943         }
5944
5945       /* Canonicalize (plus (mult (reg) (const)) (plus (reg) (const)))
5946          into (plus (plus (mult (reg) (const)) (reg)) (const)).  This can be
5947          created by virtual register instantiation, register elimination, and
5948          similar optimizations.  */
5949       if (GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == PLUS)
5950         {
5951           changed = 1;
5952           x = gen_rtx_PLUS (Pmode,
5953                             gen_rtx_PLUS (Pmode, XEXP (x, 0),
5954                                           XEXP (XEXP (x, 1), 0)),
5955                             XEXP (XEXP (x, 1), 1));
5956         }
5957
5958       /* Canonicalize
5959          (plus (plus (mult (reg) (const)) (plus (reg) (const))) const)
5960          into (plus (plus (mult (reg) (const)) (reg)) (const)).  */
5961       else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
5962                && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
5963                && GET_CODE (XEXP (XEXP (x, 0), 1)) == PLUS
5964                && CONSTANT_P (XEXP (x, 1)))
5965         {
5966           rtx constant;
5967           rtx other = NULL_RTX;
5968
5969           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5970             {
5971               constant = XEXP (x, 1);
5972               other = XEXP (XEXP (XEXP (x, 0), 1), 1);
5973             }
5974           else if (GET_CODE (XEXP (XEXP (XEXP (x, 0), 1), 1)) == CONST_INT)
5975             {
5976               constant = XEXP (XEXP (XEXP (x, 0), 1), 1);
5977               other = XEXP (x, 1);
5978             }
5979           else
5980             constant = 0;
5981
5982           if (constant)
5983             {
5984               changed = 1;
5985               x = gen_rtx_PLUS (Pmode,
5986                                 gen_rtx_PLUS (Pmode, XEXP (XEXP (x, 0), 0),
5987                                               XEXP (XEXP (XEXP (x, 0), 1), 0)),
5988                                 plus_constant (other, INTVAL (constant)));
5989             }
5990         }
5991
5992       if (changed && legitimate_address_p (mode, x, FALSE))
5993         return x;
5994
5995       if (GET_CODE (XEXP (x, 0)) == MULT)
5996         {
5997           changed = 1;
5998           XEXP (x, 0) = force_operand (XEXP (x, 0), 0);
5999         }
6000
6001       if (GET_CODE (XEXP (x, 1)) == MULT)
6002         {
6003           changed = 1;
6004           XEXP (x, 1) = force_operand (XEXP (x, 1), 0);
6005         }
6006
6007       if (changed
6008           && GET_CODE (XEXP (x, 1)) == REG
6009           && GET_CODE (XEXP (x, 0)) == REG)
6010         return x;
6011
6012       if (flag_pic && SYMBOLIC_CONST (XEXP (x, 1)))
6013         {
6014           changed = 1;
6015           x = legitimize_pic_address (x, 0);
6016         }
6017
6018       if (changed && legitimate_address_p (mode, x, FALSE))
6019         return x;
6020
6021       if (GET_CODE (XEXP (x, 0)) == REG)
6022         {
6023           rtx temp = gen_reg_rtx (Pmode);
6024           rtx val  = force_operand (XEXP (x, 1), temp);
6025           if (val != temp)
6026             emit_move_insn (temp, val);
6027
6028           XEXP (x, 1) = temp;
6029           return x;
6030         }
6031
6032       else if (GET_CODE (XEXP (x, 1)) == REG)
6033         {
6034           rtx temp = gen_reg_rtx (Pmode);
6035           rtx val  = force_operand (XEXP (x, 0), temp);
6036           if (val != temp)
6037             emit_move_insn (temp, val);
6038
6039           XEXP (x, 0) = temp;
6040           return x;
6041         }
6042     }
6043
6044   return x;
6045 }
6046 \f
6047 /* Print an integer constant expression in assembler syntax.  Addition
6048    and subtraction are the only arithmetic that may appear in these
6049    expressions.  FILE is the stdio stream to write to, X is the rtx, and
6050    CODE is the operand print code from the output string.  */
6051
6052 static void
6053 output_pic_addr_const (FILE *file, rtx x, int code)
6054 {
6055   char buf[256];
6056
6057   switch (GET_CODE (x))
6058     {
6059     case PC:
6060       gcc_assert (flag_pic);
6061       putc ('.', file);
6062       break;
6063
6064     case SYMBOL_REF:
6065       assemble_name (file, XSTR (x, 0));
6066       if (!TARGET_MACHO && code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
6067         fputs ("@PLT", file);
6068       break;
6069
6070     case LABEL_REF:
6071       x = XEXP (x, 0);
6072       /* FALLTHRU */
6073     case CODE_LABEL:
6074       ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
6075       assemble_name (asm_out_file, buf);
6076       break;
6077
6078     case CONST_INT:
6079       fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6080       break;
6081
6082     case CONST:
6083       /* This used to output parentheses around the expression,
6084          but that does not work on the 386 (either ATT or BSD assembler).  */
6085       output_pic_addr_const (file, XEXP (x, 0), code);
6086       break;
6087
6088     case CONST_DOUBLE:
6089       if (GET_MODE (x) == VOIDmode)
6090         {
6091           /* We can use %d if the number is <32 bits and positive.  */
6092           if (CONST_DOUBLE_HIGH (x) || CONST_DOUBLE_LOW (x) < 0)
6093             fprintf (file, "0x%lx%08lx",
6094                      (unsigned long) CONST_DOUBLE_HIGH (x),
6095                      (unsigned long) CONST_DOUBLE_LOW (x));
6096           else
6097             fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
6098         }
6099       else
6100         /* We can't handle floating point constants;
6101            PRINT_OPERAND must handle them.  */
6102         output_operand_lossage ("floating constant misused");
6103       break;
6104
6105     case PLUS:
6106       /* Some assemblers need integer constants to appear first.  */
6107       if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6108         {
6109           output_pic_addr_const (file, XEXP (x, 0), code);
6110           putc ('+', file);
6111           output_pic_addr_const (file, XEXP (x, 1), code);
6112         }
6113       else 
6114         {
6115           gcc_assert (GET_CODE (XEXP (x, 1)) == CONST_INT);
6116           output_pic_addr_const (file, XEXP (x, 1), code);
6117           putc ('+', file);
6118           output_pic_addr_const (file, XEXP (x, 0), code);
6119         }
6120       break;
6121
6122     case MINUS:
6123       if (!TARGET_MACHO)
6124         putc (ASSEMBLER_DIALECT == ASM_INTEL ? '(' : '[', file);
6125       output_pic_addr_const (file, XEXP (x, 0), code);
6126       putc ('-', file);
6127       output_pic_addr_const (file, XEXP (x, 1), code);
6128       if (!TARGET_MACHO)
6129         putc (ASSEMBLER_DIALECT == ASM_INTEL ? ')' : ']', file);
6130       break;
6131
6132      case UNSPEC:
6133        gcc_assert (XVECLEN (x, 0) == 1);
6134        output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
6135        switch (XINT (x, 1))
6136         {
6137         case UNSPEC_GOT:
6138           fputs ("@GOT", file);
6139           break;
6140         case UNSPEC_GOTOFF:
6141           fputs ("@GOTOFF", file);
6142           break;
6143         case UNSPEC_GOTPCREL:
6144           fputs ("@GOTPCREL(%rip)", file);
6145           break;
6146         case UNSPEC_GOTTPOFF:
6147           /* FIXME: This might be @TPOFF in Sun ld too.  */
6148           fputs ("@GOTTPOFF", file);
6149           break;
6150         case UNSPEC_TPOFF:
6151           fputs ("@TPOFF", file);
6152           break;
6153         case UNSPEC_NTPOFF:
6154           if (TARGET_64BIT)
6155             fputs ("@TPOFF", file);
6156           else
6157             fputs ("@NTPOFF", file);
6158           break;
6159         case UNSPEC_DTPOFF:
6160           fputs ("@DTPOFF", file);
6161           break;
6162         case UNSPEC_GOTNTPOFF:
6163           if (TARGET_64BIT)
6164             fputs ("@GOTTPOFF(%rip)", file);
6165           else
6166             fputs ("@GOTNTPOFF", file);
6167           break;
6168         case UNSPEC_INDNTPOFF:
6169           fputs ("@INDNTPOFF", file);
6170           break;
6171         default:
6172           output_operand_lossage ("invalid UNSPEC as operand");
6173           break;
6174         }
6175        break;
6176
6177     default:
6178       output_operand_lossage ("invalid expression as operand");
6179     }
6180 }
6181
6182 /* This is called from dwarf2out.c via ASM_OUTPUT_DWARF_DTPREL.
6183    We need to emit DTP-relative relocations.  */
6184
6185 void
6186 i386_output_dwarf_dtprel (FILE *file, int size, rtx x)
6187 {
6188   fputs (ASM_LONG, file);
6189   output_addr_const (file, x);
6190   fputs ("@DTPOFF", file);
6191   switch (size)
6192     {
6193     case 4:
6194       break;
6195     case 8:
6196       fputs (", 0", file);
6197       break;
6198     default:
6199       gcc_unreachable ();
6200    }
6201 }
6202
6203 /* In the name of slightly smaller debug output, and to cater to
6204    general assembler lossage, recognize PIC+GOTOFF and turn it back
6205    into a direct symbol reference.  */
6206
6207 static rtx
6208 ix86_delegitimize_address (rtx orig_x)
6209 {
6210   rtx x = orig_x, y;
6211
6212   if (GET_CODE (x) == MEM)
6213     x = XEXP (x, 0);
6214
6215   if (TARGET_64BIT)
6216     {
6217       if (GET_CODE (x) != CONST
6218           || GET_CODE (XEXP (x, 0)) != UNSPEC
6219           || XINT (XEXP (x, 0), 1) != UNSPEC_GOTPCREL
6220           || GET_CODE (orig_x) != MEM)
6221         return orig_x;
6222       return XVECEXP (XEXP (x, 0), 0, 0);
6223     }
6224
6225   if (GET_CODE (x) != PLUS
6226       || GET_CODE (XEXP (x, 1)) != CONST)
6227     return orig_x;
6228
6229   if (GET_CODE (XEXP (x, 0)) == REG
6230       && REGNO (XEXP (x, 0)) == PIC_OFFSET_TABLE_REGNUM)
6231     /* %ebx + GOT/GOTOFF */
6232     y = NULL;
6233   else if (GET_CODE (XEXP (x, 0)) == PLUS)
6234     {
6235       /* %ebx + %reg * scale + GOT/GOTOFF */
6236       y = XEXP (x, 0);
6237       if (GET_CODE (XEXP (y, 0)) == REG
6238           && REGNO (XEXP (y, 0)) == PIC_OFFSET_TABLE_REGNUM)
6239         y = XEXP (y, 1);
6240       else if (GET_CODE (XEXP (y, 1)) == REG
6241                && REGNO (XEXP (y, 1)) == PIC_OFFSET_TABLE_REGNUM)
6242         y = XEXP (y, 0);
6243       else
6244         return orig_x;
6245       if (GET_CODE (y) != REG
6246           && GET_CODE (y) != MULT
6247           && GET_CODE (y) != ASHIFT)
6248         return orig_x;
6249     }
6250   else
6251     return orig_x;
6252
6253   x = XEXP (XEXP (x, 1), 0);
6254   if (GET_CODE (x) == UNSPEC
6255       && ((XINT (x, 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6256           || (XINT (x, 1) == UNSPEC_GOTOFF && GET_CODE (orig_x) != MEM)))
6257     {
6258       if (y)
6259         return gen_rtx_PLUS (Pmode, y, XVECEXP (x, 0, 0));
6260       return XVECEXP (x, 0, 0);
6261     }
6262
6263   if (GET_CODE (x) == PLUS
6264       && GET_CODE (XEXP (x, 0)) == UNSPEC
6265       && GET_CODE (XEXP (x, 1)) == CONST_INT
6266       && ((XINT (XEXP (x, 0), 1) == UNSPEC_GOT && GET_CODE (orig_x) == MEM)
6267           || (XINT (XEXP (x, 0), 1) == UNSPEC_GOTOFF
6268               && GET_CODE (orig_x) != MEM)))
6269     {
6270       x = gen_rtx_PLUS (VOIDmode, XVECEXP (XEXP (x, 0), 0, 0), XEXP (x, 1));
6271       if (y)
6272         return gen_rtx_PLUS (Pmode, y, x);
6273       return x;
6274     }
6275
6276   return orig_x;
6277 }
6278 \f
6279 static void
6280 put_condition_code (enum rtx_code code, enum machine_mode mode, int reverse,
6281                     int fp, FILE *file)
6282 {
6283   const char *suffix;
6284
6285   if (mode == CCFPmode || mode == CCFPUmode)
6286     {
6287       enum rtx_code second_code, bypass_code;
6288       ix86_fp_comparison_codes (code, &bypass_code, &code, &second_code);
6289       gcc_assert (bypass_code == UNKNOWN && second_code == UNKNOWN);
6290       code = ix86_fp_compare_code_to_integer (code);
6291       mode = CCmode;
6292     }
6293   if (reverse)
6294     code = reverse_condition (code);
6295
6296   switch (code)
6297     {
6298     case EQ:
6299       suffix = "e";
6300       break;
6301     case NE:
6302       suffix = "ne";
6303       break;
6304     case GT:
6305       gcc_assert (mode == CCmode || mode == CCNOmode || mode == CCGCmode);
6306       suffix = "g";
6307       break;
6308     case GTU:
6309       /* ??? Use "nbe" instead of "a" for fcmov lossage on some assemblers.
6310          Those same assemblers have the same but opposite lossage on cmov.  */
6311       gcc_assert (mode == CCmode);
6312       suffix = fp ? "nbe" : "a";
6313       break;
6314     case LT:
6315       switch (mode)
6316         {
6317         case CCNOmode:
6318         case CCGOCmode:
6319           suffix = "s";
6320           break;
6321
6322         case CCmode:
6323         case CCGCmode:
6324           suffix = "l";
6325           break;
6326
6327         default:
6328           gcc_unreachable ();
6329         }
6330       break;
6331     case LTU:
6332       gcc_assert (mode == CCmode);
6333       suffix = "b";
6334       break;
6335     case GE:
6336       switch (mode)
6337         {
6338         case CCNOmode:
6339         case CCGOCmode:
6340           suffix = "ns";
6341           break;
6342
6343         case CCmode:
6344         case CCGCmode:
6345           suffix = "ge";
6346           break;
6347
6348         default:
6349           gcc_unreachable ();
6350         }
6351       break;
6352     case GEU:
6353       /* ??? As above.  */
6354       gcc_assert (mode == CCmode);
6355       suffix = fp ? "nb" : "ae";
6356       break;
6357     case LE:
6358       gcc_assert (mode == CCmode || mode == CCGCmode || mode == CCNOmode);
6359       suffix = "le";
6360       break;
6361     case LEU:
6362       gcc_assert (mode == CCmode);
6363       suffix = "be";
6364       break;
6365     case UNORDERED:
6366       suffix = fp ? "u" : "p";
6367       break;
6368     case ORDERED:
6369       suffix = fp ? "nu" : "np";
6370       break;
6371     default:
6372       gcc_unreachable ();
6373     }
6374   fputs (suffix, file);
6375 }
6376
6377 /* Print the name of register X to FILE based on its machine mode and number.
6378    If CODE is 'w', pretend the mode is HImode.
6379    If CODE is 'b', pretend the mode is QImode.
6380    If CODE is 'k', pretend the mode is SImode.
6381    If CODE is 'q', pretend the mode is DImode.
6382    If CODE is 'h', pretend the reg is the 'high' byte register.
6383    If CODE is 'y', print "st(0)" instead of "st", if the reg is stack op.  */
6384
6385 void
6386 print_reg (rtx x, int code, FILE *file)
6387 {
6388   gcc_assert (REGNO (x) != ARG_POINTER_REGNUM
6389               && REGNO (x) != FRAME_POINTER_REGNUM
6390               && REGNO (x) != FLAGS_REG
6391               && REGNO (x) != FPSR_REG);
6392
6393   if (ASSEMBLER_DIALECT == ASM_ATT || USER_LABEL_PREFIX[0] == 0)
6394     putc ('%', file);
6395
6396   if (code == 'w' || MMX_REG_P (x))
6397     code = 2;
6398   else if (code == 'b')
6399     code = 1;
6400   else if (code == 'k')
6401     code = 4;
6402   else if (code == 'q')
6403     code = 8;
6404   else if (code == 'y')
6405     code = 3;
6406   else if (code == 'h')
6407     code = 0;
6408   else
6409     code = GET_MODE_SIZE (GET_MODE (x));
6410
6411   /* Irritatingly, AMD extended registers use different naming convention
6412      from the normal registers.  */
6413   if (REX_INT_REG_P (x))
6414     {
6415       gcc_assert (TARGET_64BIT);
6416       switch (code)
6417         {
6418           case 0:
6419             error ("extended registers have no high halves");
6420             break;
6421           case 1:
6422             fprintf (file, "r%ib", REGNO (x) - FIRST_REX_INT_REG + 8);
6423             break;
6424           case 2:
6425             fprintf (file, "r%iw", REGNO (x) - FIRST_REX_INT_REG + 8);
6426             break;
6427           case 4:
6428             fprintf (file, "r%id", REGNO (x) - FIRST_REX_INT_REG + 8);
6429             break;
6430           case 8:
6431             fprintf (file, "r%i", REGNO (x) - FIRST_REX_INT_REG + 8);
6432             break;
6433           default:
6434             error ("unsupported operand size for extended register");
6435             break;
6436         }
6437       return;
6438     }
6439   switch (code)
6440     {
6441     case 3:
6442       if (STACK_TOP_P (x))
6443         {
6444           fputs ("st(0)", file);
6445           break;
6446         }
6447       /* FALLTHRU */
6448     case 8:
6449     case 4:
6450     case 12:
6451       if (! ANY_FP_REG_P (x))
6452         putc (code == 8 && TARGET_64BIT ? 'r' : 'e', file);
6453       /* FALLTHRU */
6454     case 16:
6455     case 2:
6456     normal:
6457       fputs (hi_reg_name[REGNO (x)], file);
6458       break;
6459     case 1:
6460       if (REGNO (x) >= ARRAY_SIZE (qi_reg_name))
6461         goto normal;
6462       fputs (qi_reg_name[REGNO (x)], file);
6463       break;
6464     case 0:
6465       if (REGNO (x) >= ARRAY_SIZE (qi_high_reg_name))
6466         goto normal;
6467       fputs (qi_high_reg_name[REGNO (x)], file);
6468       break;
6469     default:
6470       gcc_unreachable ();
6471     }
6472 }
6473
6474 /* Locate some local-dynamic symbol still in use by this function
6475    so that we can print its name in some tls_local_dynamic_base
6476    pattern.  */
6477
6478 static const char *
6479 get_some_local_dynamic_name (void)
6480 {
6481   rtx insn;
6482
6483   if (cfun->machine->some_ld_name)
6484     return cfun->machine->some_ld_name;
6485
6486   for (insn = get_insns (); insn ; insn = NEXT_INSN (insn))
6487     if (INSN_P (insn)
6488         && for_each_rtx (&PATTERN (insn), get_some_local_dynamic_name_1, 0))
6489       return cfun->machine->some_ld_name;
6490
6491   gcc_unreachable ();
6492 }
6493
6494 static int
6495 get_some_local_dynamic_name_1 (rtx *px, void *data ATTRIBUTE_UNUSED)
6496 {
6497   rtx x = *px;
6498
6499   if (GET_CODE (x) == SYMBOL_REF
6500       && local_dynamic_symbolic_operand (x, Pmode))
6501     {
6502       cfun->machine->some_ld_name = XSTR (x, 0);
6503       return 1;
6504     }
6505
6506   return 0;
6507 }
6508
6509 /* Meaning of CODE:
6510    L,W,B,Q,S,T -- print the opcode suffix for specified size of operand.
6511    C -- print opcode suffix for set/cmov insn.
6512    c -- like C, but print reversed condition
6513    F,f -- likewise, but for floating-point.
6514    O -- if HAVE_AS_IX86_CMOV_SUN_SYNTAX, expand to "w.", "l." or "q.",
6515         otherwise nothing
6516    R -- print the prefix for register names.
6517    z -- print the opcode suffix for the size of the current operand.
6518    * -- print a star (in certain assembler syntax)
6519    A -- print an absolute memory reference.
6520    w -- print the operand as if it's a "word" (HImode) even if it isn't.
6521    s -- print a shift double count, followed by the assemblers argument
6522         delimiter.
6523    b -- print the QImode name of the register for the indicated operand.
6524         %b0 would print %al if operands[0] is reg 0.
6525    w --  likewise, print the HImode name of the register.
6526    k --  likewise, print the SImode name of the register.
6527    q --  likewise, print the DImode name of the register.
6528    h -- print the QImode name for a "high" register, either ah, bh, ch or dh.
6529    y -- print "st(0)" instead of "st" as a register.
6530    D -- print condition for SSE cmp instruction.
6531    P -- if PIC, print an @PLT suffix.
6532    X -- don't print any sort of PIC '@' suffix for a symbol.
6533    & -- print some in-use local-dynamic symbol name.
6534    H -- print a memory address offset by 8; used for sse high-parts
6535  */
6536
6537 void
6538 print_operand (FILE *file, rtx x, int code)
6539 {
6540   if (code)
6541     {
6542       switch (code)
6543         {
6544         case '*':
6545           if (ASSEMBLER_DIALECT == ASM_ATT)
6546             putc ('*', file);
6547           return;
6548
6549         case '&':
6550           assemble_name (file, get_some_local_dynamic_name ());
6551           return;
6552
6553         case 'A':
6554           switch (ASSEMBLER_DIALECT)
6555             {
6556             case ASM_ATT:
6557               putc ('*', file);
6558               break;
6559
6560             case ASM_INTEL:
6561               /* Intel syntax. For absolute addresses, registers should not
6562                  be surrounded by braces.  */
6563               if (GET_CODE (x) != REG)
6564                 {
6565                   putc ('[', file);
6566                   PRINT_OPERAND (file, x, 0);
6567                   putc (']', file);
6568                   return;
6569                 }
6570               break;
6571
6572             default:
6573               gcc_unreachable ();
6574             }
6575
6576           PRINT_OPERAND (file, x, 0);
6577           return;
6578
6579
6580         case 'L':
6581           if (ASSEMBLER_DIALECT == ASM_ATT)
6582             putc ('l', file);
6583           return;
6584
6585         case 'W':
6586           if (ASSEMBLER_DIALECT == ASM_ATT)
6587             putc ('w', file);
6588           return;
6589
6590         case 'B':
6591           if (ASSEMBLER_DIALECT == ASM_ATT)
6592             putc ('b', file);
6593           return;
6594
6595         case 'Q':
6596           if (ASSEMBLER_DIALECT == ASM_ATT)
6597             putc ('l', file);
6598           return;
6599
6600         case 'S':
6601           if (ASSEMBLER_DIALECT == ASM_ATT)
6602             putc ('s', file);
6603           return;
6604
6605         case 'T':
6606           if (ASSEMBLER_DIALECT == ASM_ATT)
6607             putc ('t', file);
6608           return;
6609
6610         case 'z':
6611           /* 387 opcodes don't get size suffixes if the operands are
6612              registers.  */
6613           if (STACK_REG_P (x))
6614             return;
6615
6616           /* Likewise if using Intel opcodes.  */
6617           if (ASSEMBLER_DIALECT == ASM_INTEL)
6618             return;
6619
6620           /* This is the size of op from size of operand.  */
6621           switch (GET_MODE_SIZE (GET_MODE (x)))
6622             {
6623             case 2:
6624 #ifdef HAVE_GAS_FILDS_FISTS
6625               putc ('s', file);
6626 #endif
6627               return;
6628
6629             case 4:
6630               if (GET_MODE (x) == SFmode)
6631                 {
6632                   putc ('s', file);
6633                   return;
6634                 }
6635               else
6636                 putc ('l', file);
6637               return;
6638
6639             case 12:
6640             case 16:
6641               putc ('t', file);
6642               return;
6643
6644             case 8:
6645               if (GET_MODE_CLASS (GET_MODE (x)) == MODE_INT)
6646                 {
6647 #ifdef GAS_MNEMONICS
6648                   putc ('q', file);
6649 #else
6650                   putc ('l', file);
6651                   putc ('l', file);
6652 #endif
6653                 }
6654               else
6655                 putc ('l', file);
6656               return;
6657
6658             default:
6659               gcc_unreachable ();
6660             }
6661
6662         case 'b':
6663         case 'w':
6664         case 'k':
6665         case 'q':
6666         case 'h':
6667         case 'y':
6668         case 'X':
6669         case 'P':
6670           break;
6671
6672         case 's':
6673           if (GET_CODE (x) == CONST_INT || ! SHIFT_DOUBLE_OMITS_COUNT)
6674             {
6675               PRINT_OPERAND (file, x, 0);
6676               putc (',', file);
6677             }
6678           return;
6679
6680         case 'D':
6681           /* Little bit of braindamage here.  The SSE compare instructions
6682              does use completely different names for the comparisons that the
6683              fp conditional moves.  */
6684           switch (GET_CODE (x))
6685             {
6686             case EQ:
6687             case UNEQ:
6688               fputs ("eq", file);
6689               break;
6690             case LT:
6691             case UNLT:
6692               fputs ("lt", file);
6693               break;
6694             case LE:
6695             case UNLE:
6696               fputs ("le", file);
6697               break;
6698             case UNORDERED:
6699               fputs ("unord", file);
6700               break;
6701             case NE:
6702             case LTGT:
6703               fputs ("neq", file);
6704               break;
6705             case UNGE:
6706             case GE:
6707               fputs ("nlt", file);
6708               break;
6709             case UNGT:
6710             case GT:
6711               fputs ("nle", file);
6712               break;
6713             case ORDERED:
6714               fputs ("ord", file);
6715               break;
6716             default:
6717               gcc_unreachable ();
6718             }
6719           return;
6720         case 'O':
6721 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6722           if (ASSEMBLER_DIALECT == ASM_ATT)
6723             {
6724               switch (GET_MODE (x))
6725                 {
6726                 case HImode: putc ('w', file); break;
6727                 case SImode:
6728                 case SFmode: putc ('l', file); break;
6729                 case DImode:
6730                 case DFmode: putc ('q', file); break;
6731                 default: gcc_unreachable ();
6732                 }
6733               putc ('.', file);
6734             }
6735 #endif
6736           return;
6737         case 'C':
6738           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 0, file);
6739           return;
6740         case 'F':
6741 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6742           if (ASSEMBLER_DIALECT == ASM_ATT)
6743             putc ('.', file);
6744 #endif
6745           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 0, 1, file);
6746           return;
6747
6748           /* Like above, but reverse condition */
6749         case 'c':
6750           /* Check to see if argument to %c is really a constant
6751              and not a condition code which needs to be reversed.  */
6752           if (!COMPARISON_P (x))
6753           {
6754             output_operand_lossage ("operand is neither a constant nor a condition code, invalid operand code 'c'");
6755              return;
6756           }
6757           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 0, file);
6758           return;
6759         case 'f':
6760 #ifdef HAVE_AS_IX86_CMOV_SUN_SYNTAX
6761           if (ASSEMBLER_DIALECT == ASM_ATT)
6762             putc ('.', file);
6763 #endif
6764           put_condition_code (GET_CODE (x), GET_MODE (XEXP (x, 0)), 1, 1, file);
6765           return;
6766
6767         case 'H':
6768           /* It doesn't actually matter what mode we use here, as we're
6769              only going to use this for printing.  */
6770           x = adjust_address_nv (x, DImode, 8);
6771           break;
6772
6773         case '+':
6774           {
6775             rtx x;
6776
6777             if (!optimize || optimize_size || !TARGET_BRANCH_PREDICTION_HINTS)
6778               return;
6779
6780             x = find_reg_note (current_output_insn, REG_BR_PROB, 0);
6781             if (x)
6782               {
6783                 int pred_val = INTVAL (XEXP (x, 0));
6784
6785                 if (pred_val < REG_BR_PROB_BASE * 45 / 100
6786                     || pred_val > REG_BR_PROB_BASE * 55 / 100)
6787                   {
6788                     int taken = pred_val > REG_BR_PROB_BASE / 2;
6789                     int cputaken = final_forward_branch_p (current_output_insn) == 0;
6790
6791                     /* Emit hints only in the case default branch prediction
6792                        heuristics would fail.  */
6793                     if (taken != cputaken)
6794                       {
6795                         /* We use 3e (DS) prefix for taken branches and
6796                            2e (CS) prefix for not taken branches.  */
6797                         if (taken)
6798                           fputs ("ds ; ", file);
6799                         else
6800                           fputs ("cs ; ", file);
6801                       }
6802                   }
6803               }
6804             return;
6805           }
6806         default:
6807             output_operand_lossage ("invalid operand code '%c'", code);
6808         }
6809     }
6810
6811   if (GET_CODE (x) == REG)
6812     print_reg (x, code, file);
6813
6814   else if (GET_CODE (x) == MEM)
6815     {
6816       /* No `byte ptr' prefix for call instructions.  */
6817       if (ASSEMBLER_DIALECT == ASM_INTEL && code != 'X' && code != 'P')
6818         {
6819           const char * size;
6820           switch (GET_MODE_SIZE (GET_MODE (x)))
6821             {
6822             case 1: size = "BYTE"; break;
6823             case 2: size = "WORD"; break;
6824             case 4: size = "DWORD"; break;
6825             case 8: size = "QWORD"; break;
6826             case 12: size = "XWORD"; break;
6827             case 16: size = "XMMWORD"; break;
6828             default:
6829               gcc_unreachable ();
6830             }
6831
6832           /* Check for explicit size override (codes 'b', 'w' and 'k')  */
6833           if (code == 'b')
6834             size = "BYTE";
6835           else if (code == 'w')
6836             size = "WORD";
6837           else if (code == 'k')
6838             size = "DWORD";
6839
6840           fputs (size, file);
6841           fputs (" PTR ", file);
6842         }
6843
6844       x = XEXP (x, 0);
6845       /* Avoid (%rip) for call operands.  */
6846       if (CONSTANT_ADDRESS_P (x) && code == 'P'
6847                && GET_CODE (x) != CONST_INT)
6848         output_addr_const (file, x);
6849       else if (this_is_asm_operands && ! address_operand (x, VOIDmode))
6850         output_operand_lossage ("invalid constraints for operand");
6851       else
6852         output_address (x);
6853     }
6854
6855   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == SFmode)
6856     {
6857       REAL_VALUE_TYPE r;
6858       long l;
6859
6860       REAL_VALUE_FROM_CONST_DOUBLE (r, x);
6861       REAL_VALUE_TO_TARGET_SINGLE (r, l);
6862
6863       if (ASSEMBLER_DIALECT == ASM_ATT)
6864         putc ('$', file);
6865       fprintf (file, "0x%08lx", l);
6866     }
6867
6868   /* These float cases don't actually occur as immediate operands.  */
6869   else if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) == DFmode)
6870     {
6871       char dstr[30];
6872
6873       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
6874       fprintf (file, "%s", dstr);
6875     }
6876
6877   else if (GET_CODE (x) == CONST_DOUBLE
6878            && GET_MODE (x) == XFmode)
6879     {
6880       char dstr[30];
6881
6882       real_to_decimal (dstr, CONST_DOUBLE_REAL_VALUE (x), sizeof (dstr), 0, 1);
6883       fprintf (file, "%s", dstr);
6884     }
6885
6886   else
6887     {
6888       /* We have patterns that allow zero sets of memory, for instance.
6889          In 64-bit mode, we should probably support all 8-byte vectors,
6890          since we can in fact encode that into an immediate.  */
6891       if (GET_CODE (x) == CONST_VECTOR)
6892         {
6893           gcc_assert (x == CONST0_RTX (GET_MODE (x)));
6894           x = const0_rtx;
6895         }
6896
6897       if (code != 'P')
6898         {
6899           if (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
6900             {
6901               if (ASSEMBLER_DIALECT == ASM_ATT)
6902                 putc ('$', file);
6903             }
6904           else if (GET_CODE (x) == CONST || GET_CODE (x) == SYMBOL_REF
6905                    || GET_CODE (x) == LABEL_REF)
6906             {
6907               if (ASSEMBLER_DIALECT == ASM_ATT)
6908                 putc ('$', file);
6909               else
6910                 fputs ("OFFSET FLAT:", file);
6911             }
6912         }
6913       if (GET_CODE (x) == CONST_INT)
6914         fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
6915       else if (flag_pic)
6916         output_pic_addr_const (file, x, code);
6917       else
6918         output_addr_const (file, x);
6919     }
6920 }
6921 \f
6922 /* Print a memory operand whose address is ADDR.  */
6923
6924 void
6925 print_operand_address (FILE *file, rtx addr)
6926 {
6927   struct ix86_address parts;
6928   rtx base, index, disp;
6929   int scale;
6930   int ok = ix86_decompose_address (addr, &parts);
6931
6932   gcc_assert (ok);
6933
6934   base = parts.base;
6935   index = parts.index;
6936   disp = parts.disp;
6937   scale = parts.scale;
6938
6939   switch (parts.seg)
6940     {
6941     case SEG_DEFAULT:
6942       break;
6943     case SEG_FS:
6944     case SEG_GS:
6945       if (USER_LABEL_PREFIX[0] == 0)
6946         putc ('%', file);
6947       fputs ((parts.seg == SEG_FS ? "fs:" : "gs:"), file);
6948       break;
6949     default:
6950       gcc_unreachable ();
6951     }
6952
6953   if (!base && !index)
6954     {
6955       /* Displacement only requires special attention.  */
6956
6957       if (GET_CODE (disp) == CONST_INT)
6958         {
6959           if (ASSEMBLER_DIALECT == ASM_INTEL && parts.seg == SEG_DEFAULT)
6960             {
6961               if (USER_LABEL_PREFIX[0] == 0)
6962                 putc ('%', file);
6963               fputs ("ds:", file);
6964             }
6965           fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (disp));
6966         }
6967       else if (flag_pic)
6968         output_pic_addr_const (file, disp, 0);
6969       else
6970         output_addr_const (file, disp);
6971
6972       /* Use one byte shorter RIP relative addressing for 64bit mode.  */
6973       if (TARGET_64BIT
6974           && ((GET_CODE (disp) == SYMBOL_REF
6975                && ! tls_symbolic_operand (disp, GET_MODE (disp)))
6976               || GET_CODE (disp) == LABEL_REF
6977               || (GET_CODE (disp) == CONST
6978                   && GET_CODE (XEXP (disp, 0)) == PLUS
6979                   && (GET_CODE (XEXP (XEXP (disp, 0), 0)) == SYMBOL_REF
6980                       || GET_CODE (XEXP (XEXP (disp, 0), 0)) == LABEL_REF)
6981                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)))
6982         fputs ("(%rip)", file);
6983     }
6984   else
6985     {
6986       if (ASSEMBLER_DIALECT == ASM_ATT)
6987         {
6988           if (disp)
6989             {
6990               if (flag_pic)
6991                 output_pic_addr_const (file, disp, 0);
6992               else if (GET_CODE (disp) == LABEL_REF)
6993                 output_asm_label (disp);
6994               else
6995                 output_addr_const (file, disp);
6996             }
6997
6998           putc ('(', file);
6999           if (base)
7000             print_reg (base, 0, file);
7001           if (index)
7002             {
7003               putc (',', file);
7004               print_reg (index, 0, file);
7005               if (scale != 1)
7006                 fprintf (file, ",%d", scale);
7007             }
7008           putc (')', file);
7009         }
7010       else
7011         {
7012           rtx offset = NULL_RTX;
7013
7014           if (disp)
7015             {
7016               /* Pull out the offset of a symbol; print any symbol itself.  */
7017               if (GET_CODE (disp) == CONST
7018                   && GET_CODE (XEXP (disp, 0)) == PLUS
7019                   && GET_CODE (XEXP (XEXP (disp, 0), 1)) == CONST_INT)
7020                 {
7021                   offset = XEXP (XEXP (disp, 0), 1);
7022                   disp = gen_rtx_CONST (VOIDmode,
7023                                         XEXP (XEXP (disp, 0), 0));
7024                 }
7025
7026               if (flag_pic)
7027                 output_pic_addr_const (file, disp, 0);
7028               else if (GET_CODE (disp) == LABEL_REF)
7029                 output_asm_label (disp);
7030               else if (GET_CODE (disp) == CONST_INT)
7031                 offset = disp;
7032               else
7033                 output_addr_const (file, disp);
7034             }
7035
7036           putc ('[', file);
7037           if (base)
7038             {
7039               print_reg (base, 0, file);
7040               if (offset)
7041                 {
7042                   if (INTVAL (offset) >= 0)
7043                     putc ('+', file);
7044                   fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7045                 }
7046             }
7047           else if (offset)
7048             fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (offset));
7049           else
7050             putc ('0', file);
7051
7052           if (index)
7053             {
7054               putc ('+', file);
7055               print_reg (index, 0, file);
7056               if (scale != 1)
7057                 fprintf (file, "*%d", scale);
7058             }
7059           putc (']', file);
7060         }
7061     }
7062 }
7063
7064 bool
7065 output_addr_const_extra (FILE *file, rtx x)
7066 {
7067   rtx op;
7068
7069   if (GET_CODE (x) != UNSPEC)
7070     return false;
7071
7072   op = XVECEXP (x, 0, 0);
7073   switch (XINT (x, 1))
7074     {
7075     case UNSPEC_GOTTPOFF:
7076       output_addr_const (file, op);
7077       /* FIXME: This might be @TPOFF in Sun ld.  */
7078       fputs ("@GOTTPOFF", file);
7079       break;
7080     case UNSPEC_TPOFF:
7081       output_addr_const (file, op);
7082       fputs ("@TPOFF", file);
7083       break;
7084     case UNSPEC_NTPOFF:
7085       output_addr_const (file, op);
7086       if (TARGET_64BIT)
7087         fputs ("@TPOFF", file);
7088       else
7089         fputs ("@NTPOFF", file);
7090       break;
7091     case UNSPEC_DTPOFF:
7092       output_addr_const (file, op);
7093       fputs ("@DTPOFF", file);
7094       break;
7095     case UNSPEC_GOTNTPOFF:
7096       output_addr_const (file, op);
7097       if (TARGET_64BIT)
7098         fputs ("@GOTTPOFF(%rip)", file);
7099       else
7100         fputs ("@GOTNTPOFF", file);
7101       break;
7102     case UNSPEC_INDNTPOFF:
7103       output_addr_const (file, op);
7104       fputs ("@INDNTPOFF", file);
7105       break;
7106
7107     default:
7108       return false;
7109     }
7110
7111   return true;
7112 }
7113 \f
7114 /* Split one or more DImode RTL references into pairs of SImode
7115    references.  The RTL can be REG, offsettable MEM, integer constant, or
7116    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7117    split and "num" is its length.  lo_half and hi_half are output arrays
7118    that parallel "operands".  */
7119
7120 void
7121 split_di (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7122 {
7123   while (num--)
7124     {
7125       rtx op = operands[num];
7126
7127       /* simplify_subreg refuse to split volatile memory addresses,
7128          but we still have to handle it.  */
7129       if (GET_CODE (op) == MEM)
7130         {
7131           lo_half[num] = adjust_address (op, SImode, 0);
7132           hi_half[num] = adjust_address (op, SImode, 4);
7133         }
7134       else
7135         {
7136           lo_half[num] = simplify_gen_subreg (SImode, op,
7137                                               GET_MODE (op) == VOIDmode
7138                                               ? DImode : GET_MODE (op), 0);
7139           hi_half[num] = simplify_gen_subreg (SImode, op,
7140                                               GET_MODE (op) == VOIDmode
7141                                               ? DImode : GET_MODE (op), 4);
7142         }
7143     }
7144 }
7145 /* Split one or more TImode RTL references into pairs of SImode
7146    references.  The RTL can be REG, offsettable MEM, integer constant, or
7147    CONST_DOUBLE.  "operands" is a pointer to an array of DImode RTL to
7148    split and "num" is its length.  lo_half and hi_half are output arrays
7149    that parallel "operands".  */
7150
7151 void
7152 split_ti (rtx operands[], int num, rtx lo_half[], rtx hi_half[])
7153 {
7154   while (num--)
7155     {
7156       rtx op = operands[num];
7157
7158       /* simplify_subreg refuse to split volatile memory addresses, but we
7159          still have to handle it.  */
7160       if (GET_CODE (op) == MEM)
7161         {
7162           lo_half[num] = adjust_address (op, DImode, 0);
7163           hi_half[num] = adjust_address (op, DImode, 8);
7164         }
7165       else
7166         {
7167           lo_half[num] = simplify_gen_subreg (DImode, op, TImode, 0);
7168           hi_half[num] = simplify_gen_subreg (DImode, op, TImode, 8);
7169         }
7170     }
7171 }
7172 \f
7173 /* Output code to perform a 387 binary operation in INSN, one of PLUS,
7174    MINUS, MULT or DIV.  OPERANDS are the insn operands, where operands[3]
7175    is the expression of the binary operation.  The output may either be
7176    emitted here, or returned to the caller, like all output_* functions.
7177
7178    There is no guarantee that the operands are the same mode, as they
7179    might be within FLOAT or FLOAT_EXTEND expressions.  */
7180
7181 #ifndef SYSV386_COMPAT
7182 /* Set to 1 for compatibility with brain-damaged assemblers.  No-one
7183    wants to fix the assemblers because that causes incompatibility
7184    with gcc.  No-one wants to fix gcc because that causes
7185    incompatibility with assemblers...  You can use the option of
7186    -DSYSV386_COMPAT=0 if you recompile both gcc and gas this way.  */
7187 #define SYSV386_COMPAT 1
7188 #endif
7189
7190 const char *
7191 output_387_binary_op (rtx insn, rtx *operands)
7192 {
7193   static char buf[30];
7194   const char *p;
7195   const char *ssep;
7196   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]) || SSE_REG_P (operands[2]);
7197
7198 #ifdef ENABLE_CHECKING
7199   /* Even if we do not want to check the inputs, this documents input
7200      constraints.  Which helps in understanding the following code.  */
7201   if (STACK_REG_P (operands[0])
7202       && ((REG_P (operands[1])
7203            && REGNO (operands[0]) == REGNO (operands[1])
7204            && (STACK_REG_P (operands[2]) || GET_CODE (operands[2]) == MEM))
7205           || (REG_P (operands[2])
7206               && REGNO (operands[0]) == REGNO (operands[2])
7207               && (STACK_REG_P (operands[1]) || GET_CODE (operands[1]) == MEM)))
7208       && (STACK_TOP_P (operands[1]) || STACK_TOP_P (operands[2])))
7209     ; /* ok */
7210   else
7211     gcc_assert (is_sse);
7212 #endif
7213
7214   switch (GET_CODE (operands[3]))
7215     {
7216     case PLUS:
7217       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7218           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7219         p = "fiadd";
7220       else
7221         p = "fadd";
7222       ssep = "add";
7223       break;
7224
7225     case MINUS:
7226       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7227           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7228         p = "fisub";
7229       else
7230         p = "fsub";
7231       ssep = "sub";
7232       break;
7233
7234     case MULT:
7235       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7236           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7237         p = "fimul";
7238       else
7239         p = "fmul";
7240       ssep = "mul";
7241       break;
7242
7243     case DIV:
7244       if (GET_MODE_CLASS (GET_MODE (operands[1])) == MODE_INT
7245           || GET_MODE_CLASS (GET_MODE (operands[2])) == MODE_INT)
7246         p = "fidiv";
7247       else
7248         p = "fdiv";
7249       ssep = "div";
7250       break;
7251
7252     default:
7253       gcc_unreachable ();
7254     }
7255
7256   if (is_sse)
7257    {
7258       strcpy (buf, ssep);
7259       if (GET_MODE (operands[0]) == SFmode)
7260         strcat (buf, "ss\t{%2, %0|%0, %2}");
7261       else
7262         strcat (buf, "sd\t{%2, %0|%0, %2}");
7263       return buf;
7264    }
7265   strcpy (buf, p);
7266
7267   switch (GET_CODE (operands[3]))
7268     {
7269     case MULT:
7270     case PLUS:
7271       if (REG_P (operands[2]) && REGNO (operands[0]) == REGNO (operands[2]))
7272         {
7273           rtx temp = operands[2];
7274           operands[2] = operands[1];
7275           operands[1] = temp;
7276         }
7277
7278       /* know operands[0] == operands[1].  */
7279
7280       if (GET_CODE (operands[2]) == MEM)
7281         {
7282           p = "%z2\t%2";
7283           break;
7284         }
7285
7286       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7287         {
7288           if (STACK_TOP_P (operands[0]))
7289             /* How is it that we are storing to a dead operand[2]?
7290                Well, presumably operands[1] is dead too.  We can't
7291                store the result to st(0) as st(0) gets popped on this
7292                instruction.  Instead store to operands[2] (which I
7293                think has to be st(1)).  st(1) will be popped later.
7294                gcc <= 2.8.1 didn't have this check and generated
7295                assembly code that the Unixware assembler rejected.  */
7296             p = "p\t{%0, %2|%2, %0}";   /* st(1) = st(0) op st(1); pop */
7297           else
7298             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7299           break;
7300         }
7301
7302       if (STACK_TOP_P (operands[0]))
7303         p = "\t{%y2, %0|%0, %y2}";      /* st(0) = st(0) op st(r2) */
7304       else
7305         p = "\t{%2, %0|%0, %2}";        /* st(r1) = st(r1) op st(0) */
7306       break;
7307
7308     case MINUS:
7309     case DIV:
7310       if (GET_CODE (operands[1]) == MEM)
7311         {
7312           p = "r%z1\t%1";
7313           break;
7314         }
7315
7316       if (GET_CODE (operands[2]) == MEM)
7317         {
7318           p = "%z2\t%2";
7319           break;
7320         }
7321
7322       if (find_regno_note (insn, REG_DEAD, REGNO (operands[2])))
7323         {
7324 #if SYSV386_COMPAT
7325           /* The SystemV/386 SVR3.2 assembler, and probably all AT&T
7326              derived assemblers, confusingly reverse the direction of
7327              the operation for fsub{r} and fdiv{r} when the
7328              destination register is not st(0).  The Intel assembler
7329              doesn't have this brain damage.  Read !SYSV386_COMPAT to
7330              figure out what the hardware really does.  */
7331           if (STACK_TOP_P (operands[0]))
7332             p = "{p\t%0, %2|rp\t%2, %0}";
7333           else
7334             p = "{rp\t%2, %0|p\t%0, %2}";
7335 #else
7336           if (STACK_TOP_P (operands[0]))
7337             /* As above for fmul/fadd, we can't store to st(0).  */
7338             p = "rp\t{%0, %2|%2, %0}";  /* st(1) = st(0) op st(1); pop */
7339           else
7340             p = "p\t{%2, %0|%0, %2}";   /* st(r1) = st(r1) op st(0); pop */
7341 #endif
7342           break;
7343         }
7344
7345       if (find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
7346         {
7347 #if SYSV386_COMPAT
7348           if (STACK_TOP_P (operands[0]))
7349             p = "{rp\t%0, %1|p\t%1, %0}";
7350           else
7351             p = "{p\t%1, %0|rp\t%0, %1}";
7352 #else
7353           if (STACK_TOP_P (operands[0]))
7354             p = "p\t{%0, %1|%1, %0}";   /* st(1) = st(1) op st(0); pop */
7355           else
7356             p = "rp\t{%1, %0|%0, %1}";  /* st(r2) = st(0) op st(r2); pop */
7357 #endif
7358           break;
7359         }
7360
7361       if (STACK_TOP_P (operands[0]))
7362         {
7363           if (STACK_TOP_P (operands[1]))
7364             p = "\t{%y2, %0|%0, %y2}";  /* st(0) = st(0) op st(r2) */
7365           else
7366             p = "r\t{%y1, %0|%0, %y1}"; /* st(0) = st(r1) op st(0) */
7367           break;
7368         }
7369       else if (STACK_TOP_P (operands[1]))
7370         {
7371 #if SYSV386_COMPAT
7372           p = "{\t%1, %0|r\t%0, %1}";
7373 #else
7374           p = "r\t{%1, %0|%0, %1}";     /* st(r2) = st(0) op st(r2) */
7375 #endif
7376         }
7377       else
7378         {
7379 #if SYSV386_COMPAT
7380           p = "{r\t%2, %0|\t%0, %2}";
7381 #else
7382           p = "\t{%2, %0|%0, %2}";      /* st(r1) = st(r1) op st(0) */
7383 #endif
7384         }
7385       break;
7386
7387     default:
7388       gcc_unreachable ();
7389     }
7390
7391   strcat (buf, p);
7392   return buf;
7393 }
7394
7395 /* Output code to initialize control word copies used by trunc?f?i and
7396    rounding patterns.  CURRENT_MODE is set to current control word,
7397    while NEW_MODE is set to new control word.  */
7398
7399 void
7400 emit_i387_cw_initialization (rtx current_mode, rtx new_mode, int mode)
7401 {
7402   rtx reg = gen_reg_rtx (HImode);
7403
7404   emit_insn (gen_x86_fnstcw_1 (current_mode));
7405   emit_move_insn (reg, current_mode);
7406
7407   if (!TARGET_PARTIAL_REG_STALL && !optimize_size
7408       && !TARGET_64BIT)
7409     {
7410       switch (mode)
7411         {
7412         case I387_CW_FLOOR:
7413           /* round down toward -oo */
7414           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x4)));
7415           break;
7416
7417         case I387_CW_CEIL:
7418           /* round up toward +oo */
7419           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0x8)));
7420           break;
7421
7422         case I387_CW_TRUNC:
7423           /* round toward zero (truncate) */
7424           emit_insn (gen_movsi_insv_1 (reg, GEN_INT (0xc)));
7425           break;
7426  
7427         case I387_CW_MASK_PM:
7428           /* mask precision exception for nearbyint() */
7429           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
7430           break;
7431
7432         default:
7433           gcc_unreachable ();
7434         }
7435     }
7436   else
7437     {
7438       switch (mode)
7439         {
7440         case I387_CW_FLOOR:
7441           /* round down toward -oo */
7442           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7443           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0400)));
7444           break;
7445
7446         case I387_CW_CEIL:
7447           /* round up toward +oo */
7448           emit_insn (gen_andhi3 (reg, reg, GEN_INT (~0x0c00)));
7449           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0800)));
7450           break;
7451
7452         case I387_CW_TRUNC:
7453           /* round toward zero (truncate) */
7454           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0c00)));
7455           break;
7456
7457         case I387_CW_MASK_PM:
7458           /* mask precision exception for nearbyint() */
7459           emit_insn (gen_iorhi3 (reg, reg, GEN_INT (0x0020)));
7460           break;
7461
7462         default:
7463           gcc_unreachable ();
7464         }
7465     }
7466
7467   emit_move_insn (new_mode, reg);
7468 }
7469
7470 /* Output code for INSN to convert a float to a signed int.  OPERANDS
7471    are the insn operands.  The output may be [HSD]Imode and the input
7472    operand may be [SDX]Fmode.  */
7473
7474 const char *
7475 output_fix_trunc (rtx insn, rtx *operands, int fisttp)
7476 {
7477   int stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7478   int dimode_p = GET_MODE (operands[0]) == DImode;
7479   int round_mode = get_attr_i387_cw (insn);
7480
7481   /* Jump through a hoop or two for DImode, since the hardware has no
7482      non-popping instruction.  We used to do this a different way, but
7483      that was somewhat fragile and broke with post-reload splitters.  */
7484   if ((dimode_p || fisttp) && !stack_top_dies)
7485     output_asm_insn ("fld\t%y1", operands);
7486
7487   gcc_assert (STACK_TOP_P (operands[1]));
7488   gcc_assert (GET_CODE (operands[0]) == MEM);
7489
7490   if (fisttp)
7491       output_asm_insn ("fisttp%z0\t%0", operands);
7492   else
7493     {
7494       if (round_mode != I387_CW_ANY)
7495         output_asm_insn ("fldcw\t%3", operands);
7496       if (stack_top_dies || dimode_p)
7497         output_asm_insn ("fistp%z0\t%0", operands);
7498       else
7499         output_asm_insn ("fist%z0\t%0", operands);
7500       if (round_mode != I387_CW_ANY)
7501         output_asm_insn ("fldcw\t%2", operands);
7502     }
7503
7504   return "";
7505 }
7506
7507 /* Output code for INSN to compare OPERANDS.  EFLAGS_P is 1 when fcomi
7508    should be used.  UNORDERED_P is true when fucom should be used.  */
7509
7510 const char *
7511 output_fp_compare (rtx insn, rtx *operands, int eflags_p, int unordered_p)
7512 {
7513   int stack_top_dies;
7514   rtx cmp_op0, cmp_op1;
7515   int is_sse = SSE_REG_P (operands[0]) || SSE_REG_P (operands[1]);
7516
7517   if (eflags_p)
7518     {
7519       cmp_op0 = operands[0];
7520       cmp_op1 = operands[1];
7521     }
7522   else
7523     {
7524       cmp_op0 = operands[1];
7525       cmp_op1 = operands[2];
7526     }
7527
7528   if (is_sse)
7529     {
7530       if (GET_MODE (operands[0]) == SFmode)
7531         if (unordered_p)
7532           return "ucomiss\t{%1, %0|%0, %1}";
7533         else
7534           return "comiss\t{%1, %0|%0, %1}";
7535       else
7536         if (unordered_p)
7537           return "ucomisd\t{%1, %0|%0, %1}";
7538         else
7539           return "comisd\t{%1, %0|%0, %1}";
7540     }
7541
7542   gcc_assert (STACK_TOP_P (cmp_op0));
7543
7544   stack_top_dies = find_regno_note (insn, REG_DEAD, FIRST_STACK_REG) != 0;
7545
7546   if (cmp_op1 == CONST0_RTX (GET_MODE (cmp_op1)))
7547     {
7548       if (stack_top_dies)
7549         {
7550           output_asm_insn ("ftst\n\tfnstsw\t%0", operands);
7551           return TARGET_USE_FFREEP ? "ffreep\t%y1" : "fstp\t%y1";
7552         }
7553       else
7554         return "ftst\n\tfnstsw\t%0";
7555     }
7556
7557   if (STACK_REG_P (cmp_op1)
7558       && stack_top_dies
7559       && find_regno_note (insn, REG_DEAD, REGNO (cmp_op1))
7560       && REGNO (cmp_op1) != FIRST_STACK_REG)
7561     {
7562       /* If both the top of the 387 stack dies, and the other operand
7563          is also a stack register that dies, then this must be a
7564          `fcompp' float compare */
7565
7566       if (eflags_p)
7567         {
7568           /* There is no double popping fcomi variant.  Fortunately,
7569              eflags is immune from the fstp's cc clobbering.  */
7570           if (unordered_p)
7571             output_asm_insn ("fucomip\t{%y1, %0|%0, %y1}", operands);
7572           else
7573             output_asm_insn ("fcomip\t{%y1, %0|%0, %y1}", operands);
7574           return TARGET_USE_FFREEP ? "ffreep\t%y0" : "fstp\t%y0";
7575         }
7576       else
7577         {
7578           if (unordered_p)
7579             return "fucompp\n\tfnstsw\t%0";
7580           else
7581             return "fcompp\n\tfnstsw\t%0";
7582         }
7583     }
7584   else
7585     {
7586       /* Encoded here as eflags_p | intmode | unordered_p | stack_top_dies.  */
7587
7588       static const char * const alt[16] =
7589       {
7590         "fcom%z2\t%y2\n\tfnstsw\t%0",
7591         "fcomp%z2\t%y2\n\tfnstsw\t%0",
7592         "fucom%z2\t%y2\n\tfnstsw\t%0",
7593         "fucomp%z2\t%y2\n\tfnstsw\t%0",
7594
7595         "ficom%z2\t%y2\n\tfnstsw\t%0",
7596         "ficomp%z2\t%y2\n\tfnstsw\t%0",
7597         NULL,
7598         NULL,
7599
7600         "fcomi\t{%y1, %0|%0, %y1}",
7601         "fcomip\t{%y1, %0|%0, %y1}",
7602         "fucomi\t{%y1, %0|%0, %y1}",
7603         "fucomip\t{%y1, %0|%0, %y1}",
7604
7605         NULL,
7606         NULL,
7607         NULL,
7608         NULL
7609       };
7610
7611       int mask;
7612       const char *ret;
7613
7614       mask  = eflags_p << 3;
7615       mask |= (GET_MODE_CLASS (GET_MODE (cmp_op1)) == MODE_INT) << 2;
7616       mask |= unordered_p << 1;
7617       mask |= stack_top_dies;
7618
7619       gcc_assert (mask < 16);
7620       ret = alt[mask];
7621       gcc_assert (ret);
7622
7623       return ret;
7624     }
7625 }
7626
7627 void
7628 ix86_output_addr_vec_elt (FILE *file, int value)
7629 {
7630   const char *directive = ASM_LONG;
7631
7632 #ifdef ASM_QUAD
7633   if (TARGET_64BIT)
7634     directive = ASM_QUAD;
7635 #else
7636   gcc_assert (!TARGET_64BIT);
7637 #endif
7638
7639   fprintf (file, "%s%s%d\n", directive, LPREFIX, value);
7640 }
7641
7642 void
7643 ix86_output_addr_diff_elt (FILE *file, int value, int rel)
7644 {
7645   if (TARGET_64BIT)
7646     fprintf (file, "%s%s%d-%s%d\n",
7647              ASM_LONG, LPREFIX, value, LPREFIX, rel);
7648   else if (HAVE_AS_GOTOFF_IN_DATA)
7649     fprintf (file, "%s%s%d@GOTOFF\n", ASM_LONG, LPREFIX, value);
7650 #if TARGET_MACHO
7651   else if (TARGET_MACHO)
7652     {
7653       fprintf (file, "%s%s%d-", ASM_LONG, LPREFIX, value);
7654       machopic_output_function_base_name (file);
7655       fprintf(file, "\n");
7656     }
7657 #endif
7658   else
7659     asm_fprintf (file, "%s%U%s+[.-%s%d]\n",
7660                  ASM_LONG, GOT_SYMBOL_NAME, LPREFIX, value);
7661 }
7662 \f
7663 /* Generate either "mov $0, reg" or "xor reg, reg", as appropriate
7664    for the target.  */
7665
7666 void
7667 ix86_expand_clear (rtx dest)
7668 {
7669   rtx tmp;
7670
7671   /* We play register width games, which are only valid after reload.  */
7672   gcc_assert (reload_completed);
7673
7674   /* Avoid HImode and its attendant prefix byte.  */
7675   if (GET_MODE_SIZE (GET_MODE (dest)) < 4)
7676     dest = gen_rtx_REG (SImode, REGNO (dest));
7677
7678   tmp = gen_rtx_SET (VOIDmode, dest, const0_rtx);
7679
7680   /* This predicate should match that for movsi_xor and movdi_xor_rex64.  */
7681   if (reload_completed && (!TARGET_USE_MOV0 || optimize_size))
7682     {
7683       rtx clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, 17));
7684       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, tmp, clob));
7685     }
7686
7687   emit_insn (tmp);
7688 }
7689
7690 /* X is an unchanging MEM.  If it is a constant pool reference, return
7691    the constant pool rtx, else NULL.  */
7692
7693 rtx
7694 maybe_get_pool_constant (rtx x)
7695 {
7696   x = ix86_delegitimize_address (XEXP (x, 0));
7697
7698   if (GET_CODE (x) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (x))
7699     return get_pool_constant (x);
7700
7701   return NULL_RTX;
7702 }
7703
7704 void
7705 ix86_expand_move (enum machine_mode mode, rtx operands[])
7706 {
7707   int strict = (reload_in_progress || reload_completed);
7708   rtx op0, op1;
7709   enum tls_model model;
7710
7711   op0 = operands[0];
7712   op1 = operands[1];
7713
7714   if (GET_CODE (op1) == SYMBOL_REF)
7715     {
7716       model = SYMBOL_REF_TLS_MODEL (op1);
7717       if (model)
7718         {
7719           op1 = legitimize_tls_address (op1, model, true);
7720           op1 = force_operand (op1, op0);
7721           if (op1 == op0)
7722             return;
7723         }
7724     }
7725   else if (GET_CODE (op1) == CONST
7726            && GET_CODE (XEXP (op1, 0)) == PLUS
7727            && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SYMBOL_REF)
7728     {
7729       model = SYMBOL_REF_TLS_MODEL (XEXP (XEXP (op1, 0), 0));
7730       if (model)
7731         {
7732           rtx addend = XEXP (XEXP (op1, 0), 1);
7733           op1 = legitimize_tls_address (XEXP (XEXP (op1, 0), 0), model, true);
7734           op1 = force_operand (op1, NULL);
7735           op1 = expand_simple_binop (Pmode, PLUS, op1, addend,
7736                                      op0, 1, OPTAB_DIRECT);
7737           if (op1 == op0)
7738             return;
7739         }
7740     }
7741
7742   if (flag_pic && mode == Pmode && symbolic_operand (op1, Pmode))
7743     {
7744 #if TARGET_MACHO
7745       if (MACHOPIC_PURE)
7746         {
7747           rtx temp = ((reload_in_progress
7748                        || ((op0 && GET_CODE (op0) == REG)
7749                            && mode == Pmode))
7750                       ? op0 : gen_reg_rtx (Pmode));
7751           op1 = machopic_indirect_data_reference (op1, temp);
7752           op1 = machopic_legitimize_pic_address (op1, mode,
7753                                                  temp == op1 ? 0 : temp);
7754         }
7755       else if (MACHOPIC_INDIRECT)
7756         op1 = machopic_indirect_data_reference (op1, 0);
7757       if (op0 == op1)
7758         return;
7759 #else
7760       if (GET_CODE (op0) == MEM)
7761         op1 = force_reg (Pmode, op1);
7762       else
7763         op1 = legitimize_address (op1, op1, Pmode);
7764 #endif /* TARGET_MACHO */
7765     }
7766   else
7767     {
7768       if (GET_CODE (op0) == MEM
7769           && (PUSH_ROUNDING (GET_MODE_SIZE (mode)) != GET_MODE_SIZE (mode)
7770               || !push_operand (op0, mode))
7771           && GET_CODE (op1) == MEM)
7772         op1 = force_reg (mode, op1);
7773
7774       if (push_operand (op0, mode)
7775           && ! general_no_elim_operand (op1, mode))
7776         op1 = copy_to_mode_reg (mode, op1);
7777
7778       /* Force large constants in 64bit compilation into register
7779          to get them CSEed.  */
7780       if (TARGET_64BIT && mode == DImode
7781           && immediate_operand (op1, mode)
7782           && !x86_64_zext_immediate_operand (op1, VOIDmode)
7783           && !register_operand (op0, mode)
7784           && optimize && !reload_completed && !reload_in_progress)
7785         op1 = copy_to_mode_reg (mode, op1);
7786
7787       if (FLOAT_MODE_P (mode))
7788         {
7789           /* If we are loading a floating point constant to a register,
7790              force the value to memory now, since we'll get better code
7791              out the back end.  */
7792
7793           if (strict)
7794             ;
7795           else if (GET_CODE (op1) == CONST_DOUBLE)
7796             {
7797               op1 = validize_mem (force_const_mem (mode, op1));
7798               if (!register_operand (op0, mode))
7799                 {
7800                   rtx temp = gen_reg_rtx (mode);
7801                   emit_insn (gen_rtx_SET (VOIDmode, temp, op1));
7802                   emit_move_insn (op0, temp);
7803                   return;
7804                 }
7805             }
7806         }
7807     }
7808
7809   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
7810 }
7811
7812 void
7813 ix86_expand_vector_move (enum machine_mode mode, rtx operands[])
7814 {
7815   rtx op0 = operands[0], op1 = operands[1];
7816
7817   /* Force constants other than zero into memory.  We do not know how
7818      the instructions used to build constants modify the upper 64 bits
7819      of the register, once we have that information we may be able
7820      to handle some of them more efficiently.  */
7821   if ((reload_in_progress | reload_completed) == 0
7822       && register_operand (op0, mode)
7823       && CONSTANT_P (op1) && op1 != CONST0_RTX (mode))
7824     op1 = validize_mem (force_const_mem (mode, op1));
7825
7826   /* Make operand1 a register if it isn't already.  */
7827   if (!no_new_pseudos
7828       && !register_operand (op0, mode)
7829       && !register_operand (op1, mode))
7830     {
7831       emit_move_insn (op0, force_reg (GET_MODE (op0), op1));
7832       return;
7833     }
7834
7835   emit_insn (gen_rtx_SET (VOIDmode, op0, op1));
7836 }
7837
7838 /* Implement the movmisalign patterns for SSE.  Non-SSE modes go 
7839    straight to ix86_expand_vector_move.  */
7840
7841 void
7842 ix86_expand_vector_move_misalign (enum machine_mode mode, rtx operands[])
7843 {
7844   rtx op0, op1, m;
7845
7846   op0 = operands[0];
7847   op1 = operands[1];
7848
7849   if (MEM_P (op1))
7850     {
7851       /* If we're optimizing for size, movups is the smallest.  */
7852       if (optimize_size)
7853         {
7854           op0 = gen_lowpart (V4SFmode, op0);
7855           op1 = gen_lowpart (V4SFmode, op1);
7856           emit_insn (gen_sse_movups (op0, op1));
7857           return;
7858         }
7859
7860       /* ??? If we have typed data, then it would appear that using
7861          movdqu is the only way to get unaligned data loaded with
7862          integer type.  */
7863       if (TARGET_SSE2 && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
7864         {
7865           op0 = gen_lowpart (V16QImode, op0);
7866           op1 = gen_lowpart (V16QImode, op1);
7867           emit_insn (gen_sse2_movdqu (op0, op1));
7868           return;
7869         }
7870
7871       if (TARGET_SSE2 && mode == V2DFmode)
7872         {
7873           rtx zero;
7874
7875           /* When SSE registers are split into halves, we can avoid
7876              writing to the top half twice.  */
7877           if (TARGET_SSE_SPLIT_REGS)
7878             {
7879               emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
7880               zero = op0;
7881             }
7882           else
7883             {
7884               /* ??? Not sure about the best option for the Intel chips.
7885                  The following would seem to satisfy; the register is
7886                  entirely cleared, breaking the dependency chain.  We
7887                  then store to the upper half, with a dependency depth
7888                  of one.  A rumor has it that Intel recommends two movsd
7889                  followed by an unpacklpd, but this is unconfirmed.  And
7890                  given that the dependency depth of the unpacklpd would
7891                  still be one, I'm not sure why this would be better.  */
7892               zero = CONST0_RTX (V2DFmode);
7893             }
7894
7895           m = adjust_address (op1, DFmode, 0);
7896           emit_insn (gen_sse2_loadlpd (op0, zero, m));
7897           m = adjust_address (op1, DFmode, 8);
7898           emit_insn (gen_sse2_loadhpd (op0, op0, m));
7899         }
7900       else
7901         {
7902           if (TARGET_SSE_PARTIAL_REG_DEPENDENCY)
7903             emit_move_insn (op0, CONST0_RTX (mode));
7904           else
7905             emit_insn (gen_rtx_CLOBBER (VOIDmode, op0));
7906
7907           if (mode != V4SFmode)
7908             op0 = gen_lowpart (V4SFmode, op0);
7909           m = adjust_address (op1, V2SFmode, 0);
7910           emit_insn (gen_sse_loadlps (op0, op0, m));
7911           m = adjust_address (op1, V2SFmode, 8);
7912           emit_insn (gen_sse_loadhps (op0, op0, m));
7913         }
7914     }
7915   else if (MEM_P (op0))
7916     {
7917       /* If we're optimizing for size, movups is the smallest.  */
7918       if (optimize_size)
7919         {
7920           op0 = gen_lowpart (V4SFmode, op0);
7921           op1 = gen_lowpart (V4SFmode, op1);
7922           emit_insn (gen_sse_movups (op0, op1));
7923           return;
7924         }
7925
7926       /* ??? Similar to above, only less clear because of quote
7927          typeless stores unquote.  */
7928       if (TARGET_SSE2 && !TARGET_SSE_TYPELESS_STORES
7929           && GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
7930         {
7931           op0 = gen_lowpart (V16QImode, op0);
7932           op1 = gen_lowpart (V16QImode, op1);
7933           emit_insn (gen_sse2_movdqu (op0, op1));
7934           return;
7935         }
7936
7937       if (TARGET_SSE2 && mode == V2DFmode)
7938         {
7939           m = adjust_address (op0, DFmode, 0);
7940           emit_insn (gen_sse2_storelpd (m, op1));
7941           m = adjust_address (op0, DFmode, 8);
7942           emit_insn (gen_sse2_storehpd (m, op1));
7943         }
7944       else
7945         {
7946           if (mode != V4SFmode)
7947             op1 = gen_lowpart (V4SFmode, op1);
7948           m = adjust_address (op0, V2SFmode, 0);
7949           emit_insn (gen_sse_storelps (m, op1));
7950           m = adjust_address (op0, V2SFmode, 8);
7951           emit_insn (gen_sse_storehps (m, op1));
7952         }
7953     }
7954   else
7955     gcc_unreachable ();
7956 }
7957
7958 /* Expand a push in MODE.  This is some mode for which we do not support
7959    proper push instructions, at least from the registers that we expect
7960    the value to live in.  */
7961
7962 void
7963 ix86_expand_push (enum machine_mode mode, rtx x)
7964 {
7965   rtx tmp;
7966
7967   tmp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
7968                              GEN_INT (-GET_MODE_SIZE (mode)),
7969                              stack_pointer_rtx, 1, OPTAB_DIRECT);
7970   if (tmp != stack_pointer_rtx)
7971     emit_move_insn (stack_pointer_rtx, tmp);
7972
7973   tmp = gen_rtx_MEM (mode, stack_pointer_rtx);
7974   emit_move_insn (tmp, x);
7975 }
7976
7977 /* Fix up OPERANDS to satisfy ix86_binary_operator_ok.  Return the
7978    destination to use for the operation.  If different from the true
7979    destination in operands[0], a copy operation will be required.  */
7980
7981 rtx
7982 ix86_fixup_binary_operands (enum rtx_code code, enum machine_mode mode,
7983                             rtx operands[])
7984 {
7985   int matching_memory;
7986   rtx src1, src2, dst;
7987
7988   dst = operands[0];
7989   src1 = operands[1];
7990   src2 = operands[2];
7991
7992   /* Recognize <var1> = <value> <op> <var1> for commutative operators */
7993   if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
7994       && (rtx_equal_p (dst, src2)
7995           || immediate_operand (src1, mode)))
7996     {
7997       rtx temp = src1;
7998       src1 = src2;
7999       src2 = temp;
8000     }
8001
8002   /* If the destination is memory, and we do not have matching source
8003      operands, do things in registers.  */
8004   matching_memory = 0;
8005   if (GET_CODE (dst) == MEM)
8006     {
8007       if (rtx_equal_p (dst, src1))
8008         matching_memory = 1;
8009       else if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8010                && rtx_equal_p (dst, src2))
8011         matching_memory = 2;
8012       else
8013         dst = gen_reg_rtx (mode);
8014     }
8015
8016   /* Both source operands cannot be in memory.  */
8017   if (GET_CODE (src1) == MEM && GET_CODE (src2) == MEM)
8018     {
8019       if (matching_memory != 2)
8020         src2 = force_reg (mode, src2);
8021       else
8022         src1 = force_reg (mode, src1);
8023     }
8024
8025   /* If the operation is not commutable, source 1 cannot be a constant
8026      or non-matching memory.  */
8027   if ((CONSTANT_P (src1)
8028        || (!matching_memory && GET_CODE (src1) == MEM))
8029       && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
8030     src1 = force_reg (mode, src1);
8031
8032   /* If optimizing, copy to regs to improve CSE */
8033   if (optimize && ! no_new_pseudos)
8034     {
8035       if (GET_CODE (dst) == MEM)
8036         dst = gen_reg_rtx (mode);
8037       if (GET_CODE (src1) == MEM)
8038         src1 = force_reg (mode, src1);
8039       if (GET_CODE (src2) == MEM)
8040         src2 = force_reg (mode, src2);
8041     }
8042
8043   src1 = operands[1] = src1;
8044   src2 = operands[2] = src2;
8045   return dst;
8046 }
8047
8048 /* Similarly, but assume that the destination has already been
8049    set up properly.  */
8050
8051 void
8052 ix86_fixup_binary_operands_no_copy (enum rtx_code code,
8053                                     enum machine_mode mode, rtx operands[])
8054 {
8055   rtx dst = ix86_fixup_binary_operands (code, mode, operands);
8056   gcc_assert (dst == operands[0]);
8057 }
8058
8059 /* Attempt to expand a binary operator.  Make the expansion closer to the
8060    actual machine, then just general_operand, which will allow 3 separate
8061    memory references (one output, two input) in a single insn.  */
8062
8063 void
8064 ix86_expand_binary_operator (enum rtx_code code, enum machine_mode mode,
8065                              rtx operands[])
8066 {
8067   rtx src1, src2, dst, op, clob;
8068
8069   dst = ix86_fixup_binary_operands (code, mode, operands);
8070   src1 = operands[1];
8071   src2 = operands[2];
8072
8073  /* Emit the instruction.  */
8074
8075   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_ee (code, mode, src1, src2));
8076   if (reload_in_progress)
8077     {
8078       /* Reload doesn't know about the flags register, and doesn't know that
8079          it doesn't want to clobber it.  We can only do this with PLUS.  */
8080       gcc_assert (code == PLUS);
8081       emit_insn (op);
8082     }
8083   else
8084     {
8085       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8086       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8087     }
8088
8089   /* Fix up the destination if needed.  */
8090   if (dst != operands[0])
8091     emit_move_insn (operands[0], dst);
8092 }
8093
8094 /* Return TRUE or FALSE depending on whether the binary operator meets the
8095    appropriate constraints.  */
8096
8097 int
8098 ix86_binary_operator_ok (enum rtx_code code,
8099                          enum machine_mode mode ATTRIBUTE_UNUSED,
8100                          rtx operands[3])
8101 {
8102   /* Both source operands cannot be in memory.  */
8103   if (GET_CODE (operands[1]) == MEM && GET_CODE (operands[2]) == MEM)
8104     return 0;
8105   /* If the operation is not commutable, source 1 cannot be a constant.  */
8106   if (CONSTANT_P (operands[1]) && GET_RTX_CLASS (code) != RTX_COMM_ARITH)
8107     return 0;
8108   /* If the destination is memory, we must have a matching source operand.  */
8109   if (GET_CODE (operands[0]) == MEM
8110       && ! (rtx_equal_p (operands[0], operands[1])
8111             || (GET_RTX_CLASS (code) == RTX_COMM_ARITH
8112                 && rtx_equal_p (operands[0], operands[2]))))
8113     return 0;
8114   /* If the operation is not commutable and the source 1 is memory, we must
8115      have a matching destination.  */
8116   if (GET_CODE (operands[1]) == MEM
8117       && GET_RTX_CLASS (code) != RTX_COMM_ARITH
8118       && ! rtx_equal_p (operands[0], operands[1]))
8119     return 0;
8120   return 1;
8121 }
8122
8123 /* Attempt to expand a unary operator.  Make the expansion closer to the
8124    actual machine, then just general_operand, which will allow 2 separate
8125    memory references (one output, one input) in a single insn.  */
8126
8127 void
8128 ix86_expand_unary_operator (enum rtx_code code, enum machine_mode mode,
8129                             rtx operands[])
8130 {
8131   int matching_memory;
8132   rtx src, dst, op, clob;
8133
8134   dst = operands[0];
8135   src = operands[1];
8136
8137   /* If the destination is memory, and we do not have matching source
8138      operands, do things in registers.  */
8139   matching_memory = 0;
8140   if (MEM_P (dst))
8141     {
8142       if (rtx_equal_p (dst, src))
8143         matching_memory = 1;
8144       else
8145         dst = gen_reg_rtx (mode);
8146     }
8147
8148   /* When source operand is memory, destination must match.  */
8149   if (MEM_P (src) && !matching_memory)
8150     src = force_reg (mode, src);
8151
8152   /* If optimizing, copy to regs to improve CSE.  */
8153   if (optimize && ! no_new_pseudos)
8154     {
8155       if (GET_CODE (dst) == MEM)
8156         dst = gen_reg_rtx (mode);
8157       if (GET_CODE (src) == MEM)
8158         src = force_reg (mode, src);
8159     }
8160
8161   /* Emit the instruction.  */
8162
8163   op = gen_rtx_SET (VOIDmode, dst, gen_rtx_fmt_e (code, mode, src));
8164   if (reload_in_progress || code == NOT)
8165     {
8166       /* Reload doesn't know about the flags register, and doesn't know that
8167          it doesn't want to clobber it.  */
8168       gcc_assert (code == NOT);
8169       emit_insn (op);
8170     }
8171   else
8172     {
8173       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8174       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, op, clob)));
8175     }
8176
8177   /* Fix up the destination if needed.  */
8178   if (dst != operands[0])
8179     emit_move_insn (operands[0], dst);
8180 }
8181
8182 /* Return TRUE or FALSE depending on whether the unary operator meets the
8183    appropriate constraints.  */
8184
8185 int
8186 ix86_unary_operator_ok (enum rtx_code code ATTRIBUTE_UNUSED,
8187                         enum machine_mode mode ATTRIBUTE_UNUSED,
8188                         rtx operands[2] ATTRIBUTE_UNUSED)
8189 {
8190   /* If one of operands is memory, source and destination must match.  */
8191   if ((GET_CODE (operands[0]) == MEM
8192        || GET_CODE (operands[1]) == MEM)
8193       && ! rtx_equal_p (operands[0], operands[1]))
8194     return FALSE;
8195   return TRUE;
8196 }
8197
8198 /* A subroutine of ix86_expand_fp_absneg_operator and copysign expanders.
8199    Create a mask for the sign bit in MODE for an SSE register.  If VECT is
8200    true, then replicate the mask for all elements of the vector register.
8201    If INVERT is true, then create a mask excluding the sign bit.  */
8202
8203 rtx
8204 ix86_build_signbit_mask (enum machine_mode mode, bool vect, bool invert)
8205 {
8206   enum machine_mode vec_mode;
8207   HOST_WIDE_INT hi, lo;
8208   int shift = 63;
8209   rtvec v;
8210   rtx mask;
8211
8212   /* Find the sign bit, sign extended to 2*HWI.  */
8213   if (mode == SFmode)
8214     lo = 0x80000000, hi = lo < 0;
8215   else if (HOST_BITS_PER_WIDE_INT >= 64)
8216     lo = (HOST_WIDE_INT)1 << shift, hi = -1;
8217   else
8218     lo = 0, hi = (HOST_WIDE_INT)1 << (shift - HOST_BITS_PER_WIDE_INT);
8219
8220   if (invert)
8221     lo = ~lo, hi = ~hi;
8222
8223   /* Force this value into the low part of a fp vector constant.  */
8224   mask = immed_double_const (lo, hi, mode == SFmode ? SImode : DImode);
8225   mask = gen_lowpart (mode, mask);
8226
8227   if (mode == SFmode)
8228     {
8229       if (vect)
8230         v = gen_rtvec (4, mask, mask, mask, mask);
8231       else
8232         v = gen_rtvec (4, mask, CONST0_RTX (SFmode),
8233                        CONST0_RTX (SFmode), CONST0_RTX (SFmode));
8234       vec_mode = V4SFmode;
8235     }
8236   else
8237     {
8238       if (vect)
8239         v = gen_rtvec (2, mask, mask);
8240       else
8241         v = gen_rtvec (2, mask, CONST0_RTX (DFmode));
8242       vec_mode = V2DFmode;
8243     }
8244
8245   return force_reg (vec_mode, gen_rtx_CONST_VECTOR (vec_mode, v));
8246 }
8247
8248 /* Generate code for floating point ABS or NEG.  */
8249
8250 void
8251 ix86_expand_fp_absneg_operator (enum rtx_code code, enum machine_mode mode,
8252                                 rtx operands[])
8253 {
8254   rtx mask, set, use, clob, dst, src;
8255   bool matching_memory;
8256   bool use_sse = false;
8257   bool vector_mode = VECTOR_MODE_P (mode);
8258   enum machine_mode elt_mode = mode;
8259
8260   if (vector_mode)
8261     {
8262       elt_mode = GET_MODE_INNER (mode);
8263       use_sse = true;
8264     }
8265   else if (TARGET_SSE_MATH)
8266     use_sse = SSE_FLOAT_MODE_P (mode);
8267
8268   /* NEG and ABS performed with SSE use bitwise mask operations.
8269      Create the appropriate mask now.  */
8270   if (use_sse)
8271     mask = ix86_build_signbit_mask (elt_mode, vector_mode, code == ABS);
8272   else
8273     {
8274       /* When not using SSE, we don't use the mask, but prefer to keep the
8275          same general form of the insn pattern to reduce duplication when
8276          it comes time to split.  */
8277       mask = const0_rtx;
8278     }
8279
8280   dst = operands[0];
8281   src = operands[1];
8282
8283   /* If the destination is memory, and we don't have matching source
8284      operands, do things in registers.  */
8285   matching_memory = false;
8286   if (MEM_P (dst))
8287     {
8288       if (rtx_equal_p (dst, src) && (!optimize || no_new_pseudos))
8289         matching_memory = true;
8290       else
8291         dst = gen_reg_rtx (mode);
8292     }
8293   if (MEM_P (src) && !matching_memory)
8294     src = force_reg (mode, src);
8295
8296   if (vector_mode)
8297     {
8298       set = gen_rtx_fmt_ee (code == NEG ? XOR : AND, mode, src, mask);
8299       set = gen_rtx_SET (VOIDmode, dst, set);
8300       emit_insn (set);
8301     }
8302   else
8303     {
8304       set = gen_rtx_fmt_e (code, mode, src);
8305       set = gen_rtx_SET (VOIDmode, dst, set);
8306       use = gen_rtx_USE (VOIDmode, mask);
8307       clob = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCmode, FLAGS_REG));
8308       emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (3, set, use, clob)));
8309     }
8310
8311   if (dst != operands[0])
8312     emit_move_insn (operands[0], dst);
8313 }
8314
8315 /* Expand a copysign operation.  Special case operand 0 being a constant.  */
8316
8317 void
8318 ix86_expand_copysign (rtx operands[])
8319 {
8320   enum machine_mode mode, vmode;
8321   rtx dest, op0, op1, mask, nmask;
8322
8323   dest = operands[0];
8324   op0 = operands[1];
8325   op1 = operands[2];
8326
8327   mode = GET_MODE (dest);
8328   vmode = mode == SFmode ? V4SFmode : V2DFmode;
8329
8330   if (GET_CODE (op0) == CONST_DOUBLE)
8331     {
8332       rtvec v;
8333
8334       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
8335         op0 = simplify_unary_operation (ABS, mode, op0, mode);
8336
8337       if (op0 == CONST0_RTX (mode))
8338         op0 = CONST0_RTX (vmode);
8339       else
8340         {
8341           if (mode == SFmode)
8342             v = gen_rtvec (4, op0, CONST0_RTX (SFmode),
8343                            CONST0_RTX (SFmode), CONST0_RTX (SFmode));
8344           else
8345             v = gen_rtvec (2, op0, CONST0_RTX (DFmode));
8346           op0 = force_reg (vmode, gen_rtx_CONST_VECTOR (vmode, v));
8347         }
8348
8349       mask = ix86_build_signbit_mask (mode, 0, 0);
8350
8351       if (mode == SFmode)
8352         emit_insn (gen_copysignsf3_const (dest, op0, op1, mask));
8353       else
8354         emit_insn (gen_copysigndf3_const (dest, op0, op1, mask));
8355     }
8356   else
8357     {
8358       nmask = ix86_build_signbit_mask (mode, 0, 1);
8359       mask = ix86_build_signbit_mask (mode, 0, 0);
8360
8361       if (mode == SFmode)
8362         emit_insn (gen_copysignsf3_var (dest, NULL, op0, op1, nmask, mask));
8363       else
8364         emit_insn (gen_copysigndf3_var (dest, NULL, op0, op1, nmask, mask));
8365     }
8366 }
8367
8368 /* Deconstruct a copysign operation into bit masks.  Operand 0 is known to
8369    be a constant, and so has already been expanded into a vector constant.  */
8370
8371 void
8372 ix86_split_copysign_const (rtx operands[])
8373 {
8374   enum machine_mode mode, vmode;
8375   rtx dest, op0, op1, mask, x;
8376
8377   dest = operands[0];
8378   op0 = operands[1];
8379   op1 = operands[2];
8380   mask = operands[3];
8381
8382   mode = GET_MODE (dest);
8383   vmode = GET_MODE (mask);
8384
8385   dest = simplify_gen_subreg (vmode, dest, mode, 0);
8386   x = gen_rtx_AND (vmode, dest, mask);
8387   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8388
8389   if (op0 != CONST0_RTX (vmode))
8390     {
8391       x = gen_rtx_IOR (vmode, dest, op0);
8392       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8393     }
8394 }
8395
8396 /* Deconstruct a copysign operation into bit masks.  Operand 0 is variable,
8397    so we have to do two masks.  */
8398
8399 void
8400 ix86_split_copysign_var (rtx operands[])
8401 {
8402   enum machine_mode mode, vmode;
8403   rtx dest, scratch, op0, op1, mask, nmask, x;
8404
8405   dest = operands[0];
8406   scratch = operands[1];
8407   op0 = operands[2];
8408   op1 = operands[3];
8409   nmask = operands[4];
8410   mask = operands[5];
8411
8412   mode = GET_MODE (dest);
8413   vmode = GET_MODE (mask);
8414
8415   if (rtx_equal_p (op0, op1))
8416     {
8417       /* Shouldn't happen often (it's useless, obviously), but when it does
8418          we'd generate incorrect code if we continue below.  */
8419       emit_move_insn (dest, op0);
8420       return;
8421     }
8422
8423   if (REG_P (mask) && REGNO (dest) == REGNO (mask))     /* alternative 0 */
8424     {
8425       gcc_assert (REGNO (op1) == REGNO (scratch));
8426
8427       x = gen_rtx_AND (vmode, scratch, mask);
8428       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
8429
8430       dest = mask;
8431       op0 = simplify_gen_subreg (vmode, op0, mode, 0);
8432       x = gen_rtx_NOT (vmode, dest);
8433       x = gen_rtx_AND (vmode, x, op0);
8434       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8435     }
8436   else
8437     {
8438       if (REGNO (op1) == REGNO (scratch))               /* alternative 1,3 */
8439         {
8440           x = gen_rtx_AND (vmode, scratch, mask);
8441         }
8442       else                                              /* alternative 2,4 */
8443         {
8444           gcc_assert (REGNO (mask) == REGNO (scratch));
8445           op1 = simplify_gen_subreg (vmode, op1, mode, 0);
8446           x = gen_rtx_AND (vmode, scratch, op1);
8447         }
8448       emit_insn (gen_rtx_SET (VOIDmode, scratch, x));
8449
8450       if (REGNO (op0) == REGNO (dest))                  /* alternative 1,2 */
8451         {
8452           dest = simplify_gen_subreg (vmode, op0, mode, 0);
8453           x = gen_rtx_AND (vmode, dest, nmask);
8454         }
8455       else                                              /* alternative 3,4 */
8456         {
8457           gcc_assert (REGNO (nmask) == REGNO (dest));
8458           dest = nmask;
8459           op0 = simplify_gen_subreg (vmode, op0, mode, 0);
8460           x = gen_rtx_AND (vmode, dest, op0);
8461         }
8462       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8463     }
8464
8465   x = gen_rtx_IOR (vmode, dest, scratch);
8466   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
8467 }
8468
8469 /* Return TRUE or FALSE depending on whether the first SET in INSN
8470    has source and destination with matching CC modes, and that the
8471    CC mode is at least as constrained as REQ_MODE.  */
8472
8473 int
8474 ix86_match_ccmode (rtx insn, enum machine_mode req_mode)
8475 {
8476   rtx set;
8477   enum machine_mode set_mode;
8478
8479   set = PATTERN (insn);
8480   if (GET_CODE (set) == PARALLEL)
8481     set = XVECEXP (set, 0, 0);
8482   gcc_assert (GET_CODE (set) == SET);
8483   gcc_assert (GET_CODE (SET_SRC (set)) == COMPARE);
8484
8485   set_mode = GET_MODE (SET_DEST (set));
8486   switch (set_mode)
8487     {
8488     case CCNOmode:
8489       if (req_mode != CCNOmode
8490           && (req_mode != CCmode
8491               || XEXP (SET_SRC (set), 1) != const0_rtx))
8492         return 0;
8493       break;
8494     case CCmode:
8495       if (req_mode == CCGCmode)
8496         return 0;
8497       /* FALLTHRU */
8498     case CCGCmode:
8499       if (req_mode == CCGOCmode || req_mode == CCNOmode)
8500         return 0;
8501       /* FALLTHRU */
8502     case CCGOCmode:
8503       if (req_mode == CCZmode)
8504         return 0;
8505       /* FALLTHRU */
8506     case CCZmode:
8507       break;
8508
8509     default:
8510       gcc_unreachable ();
8511     }
8512
8513   return (GET_MODE (SET_SRC (set)) == set_mode);
8514 }
8515
8516 /* Generate insn patterns to do an integer compare of OPERANDS.  */
8517
8518 static rtx
8519 ix86_expand_int_compare (enum rtx_code code, rtx op0, rtx op1)
8520 {
8521   enum machine_mode cmpmode;
8522   rtx tmp, flags;
8523
8524   cmpmode = SELECT_CC_MODE (code, op0, op1);
8525   flags = gen_rtx_REG (cmpmode, FLAGS_REG);
8526
8527   /* This is very simple, but making the interface the same as in the
8528      FP case makes the rest of the code easier.  */
8529   tmp = gen_rtx_COMPARE (cmpmode, op0, op1);
8530   emit_insn (gen_rtx_SET (VOIDmode, flags, tmp));
8531
8532   /* Return the test that should be put into the flags user, i.e.
8533      the bcc, scc, or cmov instruction.  */
8534   return gen_rtx_fmt_ee (code, VOIDmode, flags, const0_rtx);
8535 }
8536
8537 /* Figure out whether to use ordered or unordered fp comparisons.
8538    Return the appropriate mode to use.  */
8539
8540 enum machine_mode
8541 ix86_fp_compare_mode (enum rtx_code code ATTRIBUTE_UNUSED)
8542 {
8543   /* ??? In order to make all comparisons reversible, we do all comparisons
8544      non-trapping when compiling for IEEE.  Once gcc is able to distinguish
8545      all forms trapping and nontrapping comparisons, we can make inequality
8546      comparisons trapping again, since it results in better code when using
8547      FCOM based compares.  */
8548   return TARGET_IEEE_FP ? CCFPUmode : CCFPmode;
8549 }
8550
8551 enum machine_mode
8552 ix86_cc_mode (enum rtx_code code, rtx op0, rtx op1)
8553 {
8554   if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
8555     return ix86_fp_compare_mode (code);
8556   switch (code)
8557     {
8558       /* Only zero flag is needed.  */
8559     case EQ:                    /* ZF=0 */
8560     case NE:                    /* ZF!=0 */
8561       return CCZmode;
8562       /* Codes needing carry flag.  */
8563     case GEU:                   /* CF=0 */
8564     case GTU:                   /* CF=0 & ZF=0 */
8565     case LTU:                   /* CF=1 */
8566     case LEU:                   /* CF=1 | ZF=1 */
8567       return CCmode;
8568       /* Codes possibly doable only with sign flag when
8569          comparing against zero.  */
8570     case GE:                    /* SF=OF   or   SF=0 */
8571     case LT:                    /* SF<>OF  or   SF=1 */
8572       if (op1 == const0_rtx)
8573         return CCGOCmode;
8574       else
8575         /* For other cases Carry flag is not required.  */
8576         return CCGCmode;
8577       /* Codes doable only with sign flag when comparing
8578          against zero, but we miss jump instruction for it
8579          so we need to use relational tests against overflow
8580          that thus needs to be zero.  */
8581     case GT:                    /* ZF=0 & SF=OF */
8582     case LE:                    /* ZF=1 | SF<>OF */
8583       if (op1 == const0_rtx)
8584         return CCNOmode;
8585       else
8586         return CCGCmode;
8587       /* strcmp pattern do (use flags) and combine may ask us for proper
8588          mode.  */
8589     case USE:
8590       return CCmode;
8591     default:
8592       gcc_unreachable ();
8593     }
8594 }
8595
8596 /* Return the fixed registers used for condition codes.  */
8597
8598 static bool
8599 ix86_fixed_condition_code_regs (unsigned int *p1, unsigned int *p2)
8600 {
8601   *p1 = FLAGS_REG;
8602   *p2 = FPSR_REG;
8603   return true;
8604 }
8605
8606 /* If two condition code modes are compatible, return a condition code
8607    mode which is compatible with both.  Otherwise, return
8608    VOIDmode.  */
8609
8610 static enum machine_mode
8611 ix86_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
8612 {
8613   if (m1 == m2)
8614     return m1;
8615
8616   if (GET_MODE_CLASS (m1) != MODE_CC || GET_MODE_CLASS (m2) != MODE_CC)
8617     return VOIDmode;
8618
8619   if ((m1 == CCGCmode && m2 == CCGOCmode)
8620       || (m1 == CCGOCmode && m2 == CCGCmode))
8621     return CCGCmode;
8622
8623   switch (m1)
8624     {
8625     default:
8626       gcc_unreachable ();
8627
8628     case CCmode:
8629     case CCGCmode:
8630     case CCGOCmode:
8631     case CCNOmode:
8632     case CCZmode:
8633       switch (m2)
8634         {
8635         default:
8636           return VOIDmode;
8637
8638         case CCmode:
8639         case CCGCmode:
8640         case CCGOCmode:
8641         case CCNOmode:
8642         case CCZmode:
8643           return CCmode;
8644         }
8645
8646     case CCFPmode:
8647     case CCFPUmode:
8648       /* These are only compatible with themselves, which we already
8649          checked above.  */
8650       return VOIDmode;
8651     }
8652 }
8653
8654 /* Return true if we should use an FCOMI instruction for this fp comparison.  */
8655
8656 int
8657 ix86_use_fcomi_compare (enum rtx_code code ATTRIBUTE_UNUSED)
8658 {
8659   enum rtx_code swapped_code = swap_condition (code);
8660   return ((ix86_fp_comparison_cost (code) == ix86_fp_comparison_fcomi_cost (code))
8661           || (ix86_fp_comparison_cost (swapped_code)
8662               == ix86_fp_comparison_fcomi_cost (swapped_code)));
8663 }
8664
8665 /* Swap, force into registers, or otherwise massage the two operands
8666    to a fp comparison.  The operands are updated in place; the new
8667    comparison code is returned.  */
8668
8669 static enum rtx_code
8670 ix86_prepare_fp_compare_args (enum rtx_code code, rtx *pop0, rtx *pop1)
8671 {
8672   enum machine_mode fpcmp_mode = ix86_fp_compare_mode (code);
8673   rtx op0 = *pop0, op1 = *pop1;
8674   enum machine_mode op_mode = GET_MODE (op0);
8675   int is_sse = TARGET_SSE_MATH && SSE_FLOAT_MODE_P (op_mode);
8676
8677   /* All of the unordered compare instructions only work on registers.
8678      The same is true of the fcomi compare instructions.  The same is
8679      true of the XFmode compare instructions if not comparing with
8680      zero (ftst insn is used in this case).  */
8681
8682   if (!is_sse
8683       && (fpcmp_mode == CCFPUmode
8684           || (op_mode == XFmode
8685               && ! (standard_80387_constant_p (op0) == 1
8686                     || standard_80387_constant_p (op1) == 1))
8687           || ix86_use_fcomi_compare (code)))
8688     {
8689       op0 = force_reg (op_mode, op0);
8690       op1 = force_reg (op_mode, op1);
8691     }
8692   else
8693     {
8694       /* %%% We only allow op1 in memory; op0 must be st(0).  So swap
8695          things around if they appear profitable, otherwise force op0
8696          into a register.  */
8697
8698       if (standard_80387_constant_p (op0) == 0
8699           || (GET_CODE (op0) == MEM
8700               && ! (standard_80387_constant_p (op1) == 0
8701                     || GET_CODE (op1) == MEM)))
8702         {
8703           rtx tmp;
8704           tmp = op0, op0 = op1, op1 = tmp;
8705           code = swap_condition (code);
8706         }
8707
8708       if (GET_CODE (op0) != REG)
8709         op0 = force_reg (op_mode, op0);
8710
8711       if (CONSTANT_P (op1))
8712         {
8713           int tmp = standard_80387_constant_p (op1);
8714           if (tmp == 0)
8715             op1 = validize_mem (force_const_mem (op_mode, op1));
8716           else if (tmp == 1)
8717             {
8718               if (TARGET_CMOVE)
8719                 op1 = force_reg (op_mode, op1);
8720             }
8721           else
8722             op1 = force_reg (op_mode, op1);
8723         }
8724     }
8725
8726   /* Try to rearrange the comparison to make it cheaper.  */
8727   if (ix86_fp_comparison_cost (code)
8728       > ix86_fp_comparison_cost (swap_condition (code))
8729       && (GET_CODE (op1) == REG || !no_new_pseudos))
8730     {
8731       rtx tmp;
8732       tmp = op0, op0 = op1, op1 = tmp;
8733       code = swap_condition (code);
8734       if (GET_CODE (op0) != REG)
8735         op0 = force_reg (op_mode, op0);
8736     }
8737
8738   *pop0 = op0;
8739   *pop1 = op1;
8740   return code;
8741 }
8742
8743 /* Convert comparison codes we use to represent FP comparison to integer
8744    code that will result in proper branch.  Return UNKNOWN if no such code
8745    is available.  */
8746
8747 enum rtx_code
8748 ix86_fp_compare_code_to_integer (enum rtx_code code)
8749 {
8750   switch (code)
8751     {
8752     case GT:
8753       return GTU;
8754     case GE:
8755       return GEU;
8756     case ORDERED:
8757     case UNORDERED:
8758       return code;
8759       break;
8760     case UNEQ:
8761       return EQ;
8762       break;
8763     case UNLT:
8764       return LTU;
8765       break;
8766     case UNLE:
8767       return LEU;
8768       break;
8769     case LTGT:
8770       return NE;
8771       break;
8772     default:
8773       return UNKNOWN;
8774     }
8775 }
8776
8777 /* Split comparison code CODE into comparisons we can do using branch
8778    instructions.  BYPASS_CODE is comparison code for branch that will
8779    branch around FIRST_CODE and SECOND_CODE.  If some of branches
8780    is not required, set value to UNKNOWN.
8781    We never require more than two branches.  */
8782
8783 void
8784 ix86_fp_comparison_codes (enum rtx_code code, enum rtx_code *bypass_code,
8785                           enum rtx_code *first_code,
8786                           enum rtx_code *second_code)
8787 {
8788   *first_code = code;
8789   *bypass_code = UNKNOWN;
8790   *second_code = UNKNOWN;
8791
8792   /* The fcomi comparison sets flags as follows:
8793
8794      cmp    ZF PF CF
8795      >      0  0  0
8796      <      0  0  1
8797      =      1  0  0
8798      un     1  1  1 */
8799
8800   switch (code)
8801     {
8802     case GT:                    /* GTU - CF=0 & ZF=0 */
8803     case GE:                    /* GEU - CF=0 */
8804     case ORDERED:               /* PF=0 */
8805     case UNORDERED:             /* PF=1 */
8806     case UNEQ:                  /* EQ - ZF=1 */
8807     case UNLT:                  /* LTU - CF=1 */
8808     case UNLE:                  /* LEU - CF=1 | ZF=1 */
8809     case LTGT:                  /* EQ - ZF=0 */
8810       break;
8811     case LT:                    /* LTU - CF=1 - fails on unordered */
8812       *first_code = UNLT;
8813       *bypass_code = UNORDERED;
8814       break;
8815     case LE:                    /* LEU - CF=1 | ZF=1 - fails on unordered */
8816       *first_code = UNLE;
8817       *bypass_code = UNORDERED;
8818       break;
8819     case EQ:                    /* EQ - ZF=1 - fails on unordered */
8820       *first_code = UNEQ;
8821       *bypass_code = UNORDERED;
8822       break;
8823     case NE:                    /* NE - ZF=0 - fails on unordered */
8824       *first_code = LTGT;
8825       *second_code = UNORDERED;
8826       break;
8827     case UNGE:                  /* GEU - CF=0 - fails on unordered */
8828       *first_code = GE;
8829       *second_code = UNORDERED;
8830       break;
8831     case UNGT:                  /* GTU - CF=0 & ZF=0 - fails on unordered */
8832       *first_code = GT;
8833       *second_code = UNORDERED;
8834       break;
8835     default:
8836       gcc_unreachable ();
8837     }
8838   if (!TARGET_IEEE_FP)
8839     {
8840       *second_code = UNKNOWN;
8841       *bypass_code = UNKNOWN;
8842     }
8843 }
8844
8845 /* Return cost of comparison done fcom + arithmetics operations on AX.
8846    All following functions do use number of instructions as a cost metrics.
8847    In future this should be tweaked to compute bytes for optimize_size and
8848    take into account performance of various instructions on various CPUs.  */
8849 static int
8850 ix86_fp_comparison_arithmetics_cost (enum rtx_code code)
8851 {
8852   if (!TARGET_IEEE_FP)
8853     return 4;
8854   /* The cost of code output by ix86_expand_fp_compare.  */
8855   switch (code)
8856     {
8857     case UNLE:
8858     case UNLT:
8859     case LTGT:
8860     case GT:
8861     case GE:
8862     case UNORDERED:
8863     case ORDERED:
8864     case UNEQ:
8865       return 4;
8866       break;
8867     case LT:
8868     case NE:
8869     case EQ:
8870     case UNGE:
8871       return 5;
8872       break;
8873     case LE:
8874     case UNGT:
8875       return 6;
8876       break;
8877     default:
8878       gcc_unreachable ();
8879     }
8880 }
8881
8882 /* Return cost of comparison done using fcomi operation.
8883    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8884 static int
8885 ix86_fp_comparison_fcomi_cost (enum rtx_code code)
8886 {
8887   enum rtx_code bypass_code, first_code, second_code;
8888   /* Return arbitrarily high cost when instruction is not supported - this
8889      prevents gcc from using it.  */
8890   if (!TARGET_CMOVE)
8891     return 1024;
8892   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8893   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 2;
8894 }
8895
8896 /* Return cost of comparison done using sahf operation.
8897    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8898 static int
8899 ix86_fp_comparison_sahf_cost (enum rtx_code code)
8900 {
8901   enum rtx_code bypass_code, first_code, second_code;
8902   /* Return arbitrarily high cost when instruction is not preferred - this
8903      avoids gcc from using it.  */
8904   if (!TARGET_USE_SAHF && !optimize_size)
8905     return 1024;
8906   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8907   return (bypass_code != UNKNOWN || second_code != UNKNOWN) + 3;
8908 }
8909
8910 /* Compute cost of the comparison done using any method.
8911    See ix86_fp_comparison_arithmetics_cost for the metrics.  */
8912 static int
8913 ix86_fp_comparison_cost (enum rtx_code code)
8914 {
8915   int fcomi_cost, sahf_cost, arithmetics_cost = 1024;
8916   int min;
8917
8918   fcomi_cost = ix86_fp_comparison_fcomi_cost (code);
8919   sahf_cost = ix86_fp_comparison_sahf_cost (code);
8920
8921   min = arithmetics_cost = ix86_fp_comparison_arithmetics_cost (code);
8922   if (min > sahf_cost)
8923     min = sahf_cost;
8924   if (min > fcomi_cost)
8925     min = fcomi_cost;
8926   return min;
8927 }
8928
8929 /* Generate insn patterns to do a floating point compare of OPERANDS.  */
8930
8931 static rtx
8932 ix86_expand_fp_compare (enum rtx_code code, rtx op0, rtx op1, rtx scratch,
8933                         rtx *second_test, rtx *bypass_test)
8934 {
8935   enum machine_mode fpcmp_mode, intcmp_mode;
8936   rtx tmp, tmp2;
8937   int cost = ix86_fp_comparison_cost (code);
8938   enum rtx_code bypass_code, first_code, second_code;
8939
8940   fpcmp_mode = ix86_fp_compare_mode (code);
8941   code = ix86_prepare_fp_compare_args (code, &op0, &op1);
8942
8943   if (second_test)
8944     *second_test = NULL_RTX;
8945   if (bypass_test)
8946     *bypass_test = NULL_RTX;
8947
8948   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
8949
8950   /* Do fcomi/sahf based test when profitable.  */
8951   if ((bypass_code == UNKNOWN || bypass_test)
8952       && (second_code == UNKNOWN || second_test)
8953       && ix86_fp_comparison_arithmetics_cost (code) > cost)
8954     {
8955       if (TARGET_CMOVE)
8956         {
8957           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8958           tmp = gen_rtx_SET (VOIDmode, gen_rtx_REG (fpcmp_mode, FLAGS_REG),
8959                              tmp);
8960           emit_insn (tmp);
8961         }
8962       else
8963         {
8964           tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8965           tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8966           if (!scratch)
8967             scratch = gen_reg_rtx (HImode);
8968           emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8969           emit_insn (gen_x86_sahf_1 (scratch));
8970         }
8971
8972       /* The FP codes work out to act like unsigned.  */
8973       intcmp_mode = fpcmp_mode;
8974       code = first_code;
8975       if (bypass_code != UNKNOWN)
8976         *bypass_test = gen_rtx_fmt_ee (bypass_code, VOIDmode,
8977                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8978                                        const0_rtx);
8979       if (second_code != UNKNOWN)
8980         *second_test = gen_rtx_fmt_ee (second_code, VOIDmode,
8981                                        gen_rtx_REG (intcmp_mode, FLAGS_REG),
8982                                        const0_rtx);
8983     }
8984   else
8985     {
8986       /* Sadness wrt reg-stack pops killing fpsr -- gotta get fnstsw first.  */
8987       tmp = gen_rtx_COMPARE (fpcmp_mode, op0, op1);
8988       tmp2 = gen_rtx_UNSPEC (HImode, gen_rtvec (1, tmp), UNSPEC_FNSTSW);
8989       if (!scratch)
8990         scratch = gen_reg_rtx (HImode);
8991       emit_insn (gen_rtx_SET (VOIDmode, scratch, tmp2));
8992
8993       /* In the unordered case, we have to check C2 for NaN's, which
8994          doesn't happen to work out to anything nice combination-wise.
8995          So do some bit twiddling on the value we've got in AH to come
8996          up with an appropriate set of condition codes.  */
8997
8998       intcmp_mode = CCNOmode;
8999       switch (code)
9000         {
9001         case GT:
9002         case UNGT:
9003           if (code == GT || !TARGET_IEEE_FP)
9004             {
9005               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9006               code = EQ;
9007             }
9008           else
9009             {
9010               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9011               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9012               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x44)));
9013               intcmp_mode = CCmode;
9014               code = GEU;
9015             }
9016           break;
9017         case LT:
9018         case UNLT:
9019           if (code == LT && TARGET_IEEE_FP)
9020             {
9021               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9022               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x01)));
9023               intcmp_mode = CCmode;
9024               code = EQ;
9025             }
9026           else
9027             {
9028               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x01)));
9029               code = NE;
9030             }
9031           break;
9032         case GE:
9033         case UNGE:
9034           if (code == GE || !TARGET_IEEE_FP)
9035             {
9036               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x05)));
9037               code = EQ;
9038             }
9039           else
9040             {
9041               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9042               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9043                                              GEN_INT (0x01)));
9044               code = NE;
9045             }
9046           break;
9047         case LE:
9048         case UNLE:
9049           if (code == LE && TARGET_IEEE_FP)
9050             {
9051               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9052               emit_insn (gen_addqi_ext_1 (scratch, scratch, constm1_rtx));
9053               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9054               intcmp_mode = CCmode;
9055               code = LTU;
9056             }
9057           else
9058             {
9059               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x45)));
9060               code = NE;
9061             }
9062           break;
9063         case EQ:
9064         case UNEQ:
9065           if (code == EQ && TARGET_IEEE_FP)
9066             {
9067               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9068               emit_insn (gen_cmpqi_ext_3 (scratch, GEN_INT (0x40)));
9069               intcmp_mode = CCmode;
9070               code = EQ;
9071             }
9072           else
9073             {
9074               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9075               code = NE;
9076               break;
9077             }
9078           break;
9079         case NE:
9080         case LTGT:
9081           if (code == NE && TARGET_IEEE_FP)
9082             {
9083               emit_insn (gen_andqi_ext_0 (scratch, scratch, GEN_INT (0x45)));
9084               emit_insn (gen_xorqi_cc_ext_1 (scratch, scratch,
9085                                              GEN_INT (0x40)));
9086               code = NE;
9087             }
9088           else
9089             {
9090               emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x40)));
9091               code = EQ;
9092             }
9093           break;
9094
9095         case UNORDERED:
9096           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9097           code = NE;
9098           break;
9099         case ORDERED:
9100           emit_insn (gen_testqi_ext_ccno_0 (scratch, GEN_INT (0x04)));
9101           code = EQ;
9102           break;
9103
9104         default:
9105           gcc_unreachable ();
9106         }
9107     }
9108
9109   /* Return the test that should be put into the flags user, i.e.
9110      the bcc, scc, or cmov instruction.  */
9111   return gen_rtx_fmt_ee (code, VOIDmode,
9112                          gen_rtx_REG (intcmp_mode, FLAGS_REG),
9113                          const0_rtx);
9114 }
9115
9116 rtx
9117 ix86_expand_compare (enum rtx_code code, rtx *second_test, rtx *bypass_test)
9118 {
9119   rtx op0, op1, ret;
9120   op0 = ix86_compare_op0;
9121   op1 = ix86_compare_op1;
9122
9123   if (second_test)
9124     *second_test = NULL_RTX;
9125   if (bypass_test)
9126     *bypass_test = NULL_RTX;
9127
9128   if (ix86_compare_emitted)
9129     {
9130       ret = gen_rtx_fmt_ee (code, VOIDmode, ix86_compare_emitted, const0_rtx);
9131       ix86_compare_emitted = NULL_RTX;
9132     }
9133   else if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_FLOAT)
9134     ret = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
9135                                   second_test, bypass_test);
9136   else
9137     ret = ix86_expand_int_compare (code, op0, op1);
9138
9139   return ret;
9140 }
9141
9142 /* Return true if the CODE will result in nontrivial jump sequence.  */
9143 bool
9144 ix86_fp_jump_nontrivial_p (enum rtx_code code)
9145 {
9146   enum rtx_code bypass_code, first_code, second_code;
9147   if (!TARGET_CMOVE)
9148     return true;
9149   ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9150   return bypass_code != UNKNOWN || second_code != UNKNOWN;
9151 }
9152
9153 void
9154 ix86_expand_branch (enum rtx_code code, rtx label)
9155 {
9156   rtx tmp;
9157
9158   switch (GET_MODE (ix86_compare_op0))
9159     {
9160     case QImode:
9161     case HImode:
9162     case SImode:
9163       simple:
9164       tmp = ix86_expand_compare (code, NULL, NULL);
9165       tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9166                                   gen_rtx_LABEL_REF (VOIDmode, label),
9167                                   pc_rtx);
9168       emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
9169       return;
9170
9171     case SFmode:
9172     case DFmode:
9173     case XFmode:
9174       {
9175         rtvec vec;
9176         int use_fcomi;
9177         enum rtx_code bypass_code, first_code, second_code;
9178
9179         code = ix86_prepare_fp_compare_args (code, &ix86_compare_op0,
9180                                              &ix86_compare_op1);
9181
9182         ix86_fp_comparison_codes (code, &bypass_code, &first_code, &second_code);
9183
9184         /* Check whether we will use the natural sequence with one jump.  If
9185            so, we can expand jump early.  Otherwise delay expansion by
9186            creating compound insn to not confuse optimizers.  */
9187         if (bypass_code == UNKNOWN && second_code == UNKNOWN
9188             && TARGET_CMOVE)
9189           {
9190             ix86_split_fp_branch (code, ix86_compare_op0, ix86_compare_op1,
9191                                   gen_rtx_LABEL_REF (VOIDmode, label),
9192                                   pc_rtx, NULL_RTX, NULL_RTX);
9193           }
9194         else
9195           {
9196             tmp = gen_rtx_fmt_ee (code, VOIDmode,
9197                                   ix86_compare_op0, ix86_compare_op1);
9198             tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
9199                                         gen_rtx_LABEL_REF (VOIDmode, label),
9200                                         pc_rtx);
9201             tmp = gen_rtx_SET (VOIDmode, pc_rtx, tmp);
9202
9203             use_fcomi = ix86_use_fcomi_compare (code);
9204             vec = rtvec_alloc (3 + !use_fcomi);
9205             RTVEC_ELT (vec, 0) = tmp;
9206             RTVEC_ELT (vec, 1)
9207               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 18));
9208             RTVEC_ELT (vec, 2)
9209               = gen_rtx_CLOBBER (VOIDmode, gen_rtx_REG (CCFPmode, 17));
9210             if (! use_fcomi)
9211               RTVEC_ELT (vec, 3)
9212                 = gen_rtx_CLOBBER (VOIDmode, gen_rtx_SCRATCH (HImode));
9213
9214             emit_jump_insn (gen_rtx_PARALLEL (VOIDmode, vec));
9215           }
9216         return;
9217       }
9218
9219     case DImode:
9220       if (TARGET_64BIT)
9221         goto simple;
9222       /* Expand DImode branch into multiple compare+branch.  */
9223       {
9224         rtx lo[2], hi[2], label2;
9225         enum rtx_code code1, code2, code3;
9226
9227         if (CONSTANT_P (ix86_compare_op0) && ! CONSTANT_P (ix86_compare_op1))
9228           {
9229             tmp = ix86_compare_op0;
9230             ix86_compare_op0 = ix86_compare_op1;
9231             ix86_compare_op1 = tmp;
9232             code = swap_condition (code);
9233           }
9234         split_di (&ix86_compare_op0, 1, lo+0, hi+0);
9235         split_di (&ix86_compare_op1, 1, lo+1, hi+1);
9236
9237         /* When comparing for equality, we can use (hi0^hi1)|(lo0^lo1) to
9238            avoid two branches.  This costs one extra insn, so disable when
9239            optimizing for size.  */
9240
9241         if ((code == EQ || code == NE)
9242             && (!optimize_size
9243                 || hi[1] == const0_rtx || lo[1] == const0_rtx))
9244           {
9245             rtx xor0, xor1;
9246
9247             xor1 = hi[0];
9248             if (hi[1] != const0_rtx)
9249               xor1 = expand_binop (SImode, xor_optab, xor1, hi[1],
9250                                    NULL_RTX, 0, OPTAB_WIDEN);
9251
9252             xor0 = lo[0];
9253             if (lo[1] != const0_rtx)
9254               xor0 = expand_binop (SImode, xor_optab, xor0, lo[1],
9255                                    NULL_RTX, 0, OPTAB_WIDEN);
9256
9257             tmp = expand_binop (SImode, ior_optab, xor1, xor0,
9258                                 NULL_RTX, 0, OPTAB_WIDEN);
9259
9260             ix86_compare_op0 = tmp;
9261             ix86_compare_op1 = const0_rtx;
9262             ix86_expand_branch (code, label);
9263             return;
9264           }
9265
9266         /* Otherwise, if we are doing less-than or greater-or-equal-than,
9267            op1 is a constant and the low word is zero, then we can just
9268            examine the high word.  */
9269
9270         if (GET_CODE (hi[1]) == CONST_INT && lo[1] == const0_rtx)
9271           switch (code)
9272             {
9273             case LT: case LTU: case GE: case GEU:
9274               ix86_compare_op0 = hi[0];
9275               ix86_compare_op1 = hi[1];
9276               ix86_expand_branch (code, label);
9277               return;
9278             default:
9279               break;
9280             }
9281
9282         /* Otherwise, we need two or three jumps.  */
9283
9284         label2 = gen_label_rtx ();
9285
9286         code1 = code;
9287         code2 = swap_condition (code);
9288         code3 = unsigned_condition (code);
9289
9290         switch (code)
9291           {
9292           case LT: case GT: case LTU: case GTU:
9293             break;
9294
9295           case LE:   code1 = LT;  code2 = GT;  break;
9296           case GE:   code1 = GT;  code2 = LT;  break;
9297           case LEU:  code1 = LTU; code2 = GTU; break;
9298           case GEU:  code1 = GTU; code2 = LTU; break;
9299
9300           case EQ:   code1 = UNKNOWN; code2 = NE;  break;
9301           case NE:   code2 = UNKNOWN; break;
9302
9303           default:
9304             gcc_unreachable ();
9305           }
9306
9307         /*
9308          * a < b =>
9309          *    if (hi(a) < hi(b)) goto true;
9310          *    if (hi(a) > hi(b)) goto false;
9311          *    if (lo(a) < lo(b)) goto true;
9312          *  false:
9313          */
9314
9315         ix86_compare_op0 = hi[0];
9316         ix86_compare_op1 = hi[1];
9317
9318         if (code1 != UNKNOWN)
9319           ix86_expand_branch (code1, label);
9320         if (code2 != UNKNOWN)
9321           ix86_expand_branch (code2, label2);
9322
9323         ix86_compare_op0 = lo[0];
9324         ix86_compare_op1 = lo[1];
9325         ix86_expand_branch (code3, label);
9326
9327         if (code2 != UNKNOWN)
9328           emit_label (label2);
9329         return;
9330       }
9331
9332     default:
9333       gcc_unreachable ();
9334     }
9335 }
9336
9337 /* Split branch based on floating point condition.  */
9338 void
9339 ix86_split_fp_branch (enum rtx_code code, rtx op1, rtx op2,
9340                       rtx target1, rtx target2, rtx tmp, rtx pushed)
9341 {
9342   rtx second, bypass;
9343   rtx label = NULL_RTX;
9344   rtx condition;
9345   int bypass_probability = -1, second_probability = -1, probability = -1;
9346   rtx i;
9347
9348   if (target2 != pc_rtx)
9349     {
9350       rtx tmp = target2;
9351       code = reverse_condition_maybe_unordered (code);
9352       target2 = target1;
9353       target1 = tmp;
9354     }
9355
9356   condition = ix86_expand_fp_compare (code, op1, op2,
9357                                       tmp, &second, &bypass);
9358
9359   /* Remove pushed operand from stack.  */
9360   if (pushed)
9361     ix86_free_from_memory (GET_MODE (pushed));
9362
9363   if (split_branch_probability >= 0)
9364     {
9365       /* Distribute the probabilities across the jumps.
9366          Assume the BYPASS and SECOND to be always test
9367          for UNORDERED.  */
9368       probability = split_branch_probability;
9369
9370       /* Value of 1 is low enough to make no need for probability
9371          to be updated.  Later we may run some experiments and see
9372          if unordered values are more frequent in practice.  */
9373       if (bypass)
9374         bypass_probability = 1;
9375       if (second)
9376         second_probability = 1;
9377     }
9378   if (bypass != NULL_RTX)
9379     {
9380       label = gen_label_rtx ();
9381       i = emit_jump_insn (gen_rtx_SET
9382                           (VOIDmode, pc_rtx,
9383                            gen_rtx_IF_THEN_ELSE (VOIDmode,
9384                                                  bypass,
9385                                                  gen_rtx_LABEL_REF (VOIDmode,
9386                                                                     label),
9387                                                  pc_rtx)));
9388       if (bypass_probability >= 0)
9389         REG_NOTES (i)
9390           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9391                                GEN_INT (bypass_probability),
9392                                REG_NOTES (i));
9393     }
9394   i = emit_jump_insn (gen_rtx_SET
9395                       (VOIDmode, pc_rtx,
9396                        gen_rtx_IF_THEN_ELSE (VOIDmode,
9397                                              condition, target1, target2)));
9398   if (probability >= 0)
9399     REG_NOTES (i)
9400       = gen_rtx_EXPR_LIST (REG_BR_PROB,
9401                            GEN_INT (probability),
9402                            REG_NOTES (i));
9403   if (second != NULL_RTX)
9404     {
9405       i = emit_jump_insn (gen_rtx_SET
9406                           (VOIDmode, pc_rtx,
9407                            gen_rtx_IF_THEN_ELSE (VOIDmode, second, target1,
9408                                                  target2)));
9409       if (second_probability >= 0)
9410         REG_NOTES (i)
9411           = gen_rtx_EXPR_LIST (REG_BR_PROB,
9412                                GEN_INT (second_probability),
9413                                REG_NOTES (i));
9414     }
9415   if (label != NULL_RTX)
9416     emit_label (label);
9417 }
9418
9419 int
9420 ix86_expand_setcc (enum rtx_code code, rtx dest)
9421 {
9422   rtx ret, tmp, tmpreg, equiv;
9423   rtx second_test, bypass_test;
9424
9425   if (GET_MODE (ix86_compare_op0) == DImode
9426       && !TARGET_64BIT)
9427     return 0; /* FAIL */
9428
9429   gcc_assert (GET_MODE (dest) == QImode);
9430
9431   ret = ix86_expand_compare (code, &second_test, &bypass_test);
9432   PUT_MODE (ret, QImode);
9433
9434   tmp = dest;
9435   tmpreg = dest;
9436
9437   emit_insn (gen_rtx_SET (VOIDmode, tmp, ret));
9438   if (bypass_test || second_test)
9439     {
9440       rtx test = second_test;
9441       int bypass = 0;
9442       rtx tmp2 = gen_reg_rtx (QImode);
9443       if (bypass_test)
9444         {
9445           gcc_assert (!second_test);
9446           test = bypass_test;
9447           bypass = 1;
9448           PUT_CODE (test, reverse_condition_maybe_unordered (GET_CODE (test)));
9449         }
9450       PUT_MODE (test, QImode);
9451       emit_insn (gen_rtx_SET (VOIDmode, tmp2, test));
9452
9453       if (bypass)
9454         emit_insn (gen_andqi3 (tmp, tmpreg, tmp2));
9455       else
9456         emit_insn (gen_iorqi3 (tmp, tmpreg, tmp2));
9457     }
9458
9459   /* Attach a REG_EQUAL note describing the comparison result.  */
9460   if (ix86_compare_op0 && ix86_compare_op1)
9461     {
9462       equiv = simplify_gen_relational (code, QImode,
9463                                        GET_MODE (ix86_compare_op0),
9464                                        ix86_compare_op0, ix86_compare_op1);
9465       set_unique_reg_note (get_last_insn (), REG_EQUAL, equiv);
9466     }
9467
9468   return 1; /* DONE */
9469 }
9470
9471 /* Expand comparison setting or clearing carry flag.  Return true when
9472    successful and set pop for the operation.  */
9473 static bool
9474 ix86_expand_carry_flag_compare (enum rtx_code code, rtx op0, rtx op1, rtx *pop)
9475 {
9476   enum machine_mode mode =
9477     GET_MODE (op0) != VOIDmode ? GET_MODE (op0) : GET_MODE (op1);
9478
9479   /* Do not handle DImode compares that go trought special path.  Also we can't
9480      deal with FP compares yet.  This is possible to add.  */
9481   if ((mode == DImode && !TARGET_64BIT))
9482     return false;
9483   if (FLOAT_MODE_P (mode))
9484     {
9485       rtx second_test = NULL, bypass_test = NULL;
9486       rtx compare_op, compare_seq;
9487
9488       /* Shortcut:  following common codes never translate into carry flag compares.  */
9489       if (code == EQ || code == NE || code == UNEQ || code == LTGT
9490           || code == ORDERED || code == UNORDERED)
9491         return false;
9492
9493       /* These comparisons require zero flag; swap operands so they won't.  */
9494       if ((code == GT || code == UNLE || code == LE || code == UNGT)
9495           && !TARGET_IEEE_FP)
9496         {
9497           rtx tmp = op0;
9498           op0 = op1;
9499           op1 = tmp;
9500           code = swap_condition (code);
9501         }
9502
9503       /* Try to expand the comparison and verify that we end up with carry flag
9504          based comparison.  This is fails to be true only when we decide to expand
9505          comparison using arithmetic that is not too common scenario.  */
9506       start_sequence ();
9507       compare_op = ix86_expand_fp_compare (code, op0, op1, NULL_RTX,
9508                                            &second_test, &bypass_test);
9509       compare_seq = get_insns ();
9510       end_sequence ();
9511
9512       if (second_test || bypass_test)
9513         return false;
9514       if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
9515           || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
9516         code = ix86_fp_compare_code_to_integer (GET_CODE (compare_op));
9517       else
9518         code = GET_CODE (compare_op);
9519       if (code != LTU && code != GEU)
9520         return false;
9521       emit_insn (compare_seq);
9522       *pop = compare_op;
9523       return true;
9524     }
9525   if (!INTEGRAL_MODE_P (mode))
9526     return false;
9527   switch (code)
9528     {
9529     case LTU:
9530     case GEU:
9531       break;
9532
9533     /* Convert a==0 into (unsigned)a<1.  */
9534     case EQ:
9535     case NE:
9536       if (op1 != const0_rtx)
9537         return false;
9538       op1 = const1_rtx;
9539       code = (code == EQ ? LTU : GEU);
9540       break;
9541
9542     /* Convert a>b into b<a or a>=b-1.  */
9543     case GTU:
9544     case LEU:
9545       if (GET_CODE (op1) == CONST_INT)
9546         {
9547           op1 = gen_int_mode (INTVAL (op1) + 1, GET_MODE (op0));
9548           /* Bail out on overflow.  We still can swap operands but that
9549              would force loading of the constant into register.  */
9550           if (op1 == const0_rtx
9551               || !x86_64_immediate_operand (op1, GET_MODE (op1)))
9552             return false;
9553           code = (code == GTU ? GEU : LTU);
9554         }
9555       else
9556         {
9557           rtx tmp = op1;
9558           op1 = op0;
9559           op0 = tmp;
9560           code = (code == GTU ? LTU : GEU);
9561         }
9562       break;
9563
9564     /* Convert a>=0 into (unsigned)a<0x80000000.  */
9565     case LT:
9566     case GE:
9567       if (mode == DImode || op1 != const0_rtx)
9568         return false;
9569       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
9570       code = (code == LT ? GEU : LTU);
9571       break;
9572     case LE:
9573     case GT:
9574       if (mode == DImode || op1 != constm1_rtx)
9575         return false;
9576       op1 = gen_int_mode (1 << (GET_MODE_BITSIZE (mode) - 1), mode);
9577       code = (code == LE ? GEU : LTU);
9578       break;
9579
9580     default:
9581       return false;
9582     }
9583   /* Swapping operands may cause constant to appear as first operand.  */
9584   if (!nonimmediate_operand (op0, VOIDmode))
9585     {
9586       if (no_new_pseudos)
9587         return false;
9588       op0 = force_reg (mode, op0);
9589     }
9590   ix86_compare_op0 = op0;
9591   ix86_compare_op1 = op1;
9592   *pop = ix86_expand_compare (code, NULL, NULL);
9593   gcc_assert (GET_CODE (*pop) == LTU || GET_CODE (*pop) == GEU);
9594   return true;
9595 }
9596
9597 int
9598 ix86_expand_int_movcc (rtx operands[])
9599 {
9600   enum rtx_code code = GET_CODE (operands[1]), compare_code;
9601   rtx compare_seq, compare_op;
9602   rtx second_test, bypass_test;
9603   enum machine_mode mode = GET_MODE (operands[0]);
9604   bool sign_bit_compare_p = false;;
9605
9606   start_sequence ();
9607   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
9608   compare_seq = get_insns ();
9609   end_sequence ();
9610
9611   compare_code = GET_CODE (compare_op);
9612
9613   if ((ix86_compare_op1 == const0_rtx && (code == GE || code == LT))
9614       || (ix86_compare_op1 == constm1_rtx && (code == GT || code == LE)))
9615     sign_bit_compare_p = true;
9616
9617   /* Don't attempt mode expansion here -- if we had to expand 5 or 6
9618      HImode insns, we'd be swallowed in word prefix ops.  */
9619
9620   if ((mode != HImode || TARGET_FAST_PREFIX)
9621       && (mode != DImode || TARGET_64BIT)
9622       && GET_CODE (operands[2]) == CONST_INT
9623       && GET_CODE (operands[3]) == CONST_INT)
9624     {
9625       rtx out = operands[0];
9626       HOST_WIDE_INT ct = INTVAL (operands[2]);
9627       HOST_WIDE_INT cf = INTVAL (operands[3]);
9628       HOST_WIDE_INT diff;
9629
9630       diff = ct - cf;
9631       /*  Sign bit compares are better done using shifts than we do by using
9632           sbb.  */
9633       if (sign_bit_compare_p
9634           || ix86_expand_carry_flag_compare (code, ix86_compare_op0,
9635                                              ix86_compare_op1, &compare_op))
9636         {
9637           /* Detect overlap between destination and compare sources.  */
9638           rtx tmp = out;
9639
9640           if (!sign_bit_compare_p)
9641             {
9642               bool fpcmp = false;
9643
9644               compare_code = GET_CODE (compare_op);
9645
9646               if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
9647                   || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
9648                 {
9649                   fpcmp = true;
9650                   compare_code = ix86_fp_compare_code_to_integer (compare_code);
9651                 }
9652
9653               /* To simplify rest of code, restrict to the GEU case.  */
9654               if (compare_code == LTU)
9655                 {
9656                   HOST_WIDE_INT tmp = ct;
9657                   ct = cf;
9658                   cf = tmp;
9659                   compare_code = reverse_condition (compare_code);
9660                   code = reverse_condition (code);
9661                 }
9662               else
9663                 {
9664                   if (fpcmp)
9665                     PUT_CODE (compare_op,
9666                               reverse_condition_maybe_unordered
9667                                 (GET_CODE (compare_op)));
9668                   else
9669                     PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
9670                 }
9671               diff = ct - cf;
9672
9673               if (reg_overlap_mentioned_p (out, ix86_compare_op0)
9674                   || reg_overlap_mentioned_p (out, ix86_compare_op1))
9675                 tmp = gen_reg_rtx (mode);
9676
9677               if (mode == DImode)
9678                 emit_insn (gen_x86_movdicc_0_m1_rex64 (tmp, compare_op));
9679               else
9680                 emit_insn (gen_x86_movsicc_0_m1 (gen_lowpart (SImode, tmp), compare_op));
9681             }
9682           else
9683             {
9684               if (code == GT || code == GE)
9685                 code = reverse_condition (code);
9686               else
9687                 {
9688                   HOST_WIDE_INT tmp = ct;
9689                   ct = cf;
9690                   cf = tmp;
9691                   diff = ct - cf;
9692                 }
9693               tmp = emit_store_flag (tmp, code, ix86_compare_op0,
9694                                      ix86_compare_op1, VOIDmode, 0, -1);
9695             }
9696
9697           if (diff == 1)
9698             {
9699               /*
9700                * cmpl op0,op1
9701                * sbbl dest,dest
9702                * [addl dest, ct]
9703                *
9704                * Size 5 - 8.
9705                */
9706               if (ct)
9707                 tmp = expand_simple_binop (mode, PLUS,
9708                                            tmp, GEN_INT (ct),
9709                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9710             }
9711           else if (cf == -1)
9712             {
9713               /*
9714                * cmpl op0,op1
9715                * sbbl dest,dest
9716                * orl $ct, dest
9717                *
9718                * Size 8.
9719                */
9720               tmp = expand_simple_binop (mode, IOR,
9721                                          tmp, GEN_INT (ct),
9722                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9723             }
9724           else if (diff == -1 && ct)
9725             {
9726               /*
9727                * cmpl op0,op1
9728                * sbbl dest,dest
9729                * notl dest
9730                * [addl dest, cf]
9731                *
9732                * Size 8 - 11.
9733                */
9734               tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9735               if (cf)
9736                 tmp = expand_simple_binop (mode, PLUS,
9737                                            copy_rtx (tmp), GEN_INT (cf),
9738                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9739             }
9740           else
9741             {
9742               /*
9743                * cmpl op0,op1
9744                * sbbl dest,dest
9745                * [notl dest]
9746                * andl cf - ct, dest
9747                * [addl dest, ct]
9748                *
9749                * Size 8 - 11.
9750                */
9751
9752               if (cf == 0)
9753                 {
9754                   cf = ct;
9755                   ct = 0;
9756                   tmp = expand_simple_unop (mode, NOT, tmp, copy_rtx (tmp), 1);
9757                 }
9758
9759               tmp = expand_simple_binop (mode, AND,
9760                                          copy_rtx (tmp),
9761                                          gen_int_mode (cf - ct, mode),
9762                                          copy_rtx (tmp), 1, OPTAB_DIRECT);
9763               if (ct)
9764                 tmp = expand_simple_binop (mode, PLUS,
9765                                            copy_rtx (tmp), GEN_INT (ct),
9766                                            copy_rtx (tmp), 1, OPTAB_DIRECT);
9767             }
9768
9769           if (!rtx_equal_p (tmp, out))
9770             emit_move_insn (copy_rtx (out), copy_rtx (tmp));
9771
9772           return 1; /* DONE */
9773         }
9774
9775       if (diff < 0)
9776         {
9777           HOST_WIDE_INT tmp;
9778           tmp = ct, ct = cf, cf = tmp;
9779           diff = -diff;
9780           if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9781             {
9782               /* We may be reversing unordered compare to normal compare, that
9783                  is not valid in general (we may convert non-trapping condition
9784                  to trapping one), however on i386 we currently emit all
9785                  comparisons unordered.  */
9786               compare_code = reverse_condition_maybe_unordered (compare_code);
9787               code = reverse_condition_maybe_unordered (code);
9788             }
9789           else
9790             {
9791               compare_code = reverse_condition (compare_code);
9792               code = reverse_condition (code);
9793             }
9794         }
9795
9796       compare_code = UNKNOWN;
9797       if (GET_MODE_CLASS (GET_MODE (ix86_compare_op0)) == MODE_INT
9798           && GET_CODE (ix86_compare_op1) == CONST_INT)
9799         {
9800           if (ix86_compare_op1 == const0_rtx
9801               && (code == LT || code == GE))
9802             compare_code = code;
9803           else if (ix86_compare_op1 == constm1_rtx)
9804             {
9805               if (code == LE)
9806                 compare_code = LT;
9807               else if (code == GT)
9808                 compare_code = GE;
9809             }
9810         }
9811
9812       /* Optimize dest = (op0 < 0) ? -1 : cf.  */
9813       if (compare_code != UNKNOWN
9814           && GET_MODE (ix86_compare_op0) == GET_MODE (out)
9815           && (cf == -1 || ct == -1))
9816         {
9817           /* If lea code below could be used, only optimize
9818              if it results in a 2 insn sequence.  */
9819
9820           if (! (diff == 1 || diff == 2 || diff == 4 || diff == 8
9821                  || diff == 3 || diff == 5 || diff == 9)
9822               || (compare_code == LT && ct == -1)
9823               || (compare_code == GE && cf == -1))
9824             {
9825               /*
9826                * notl op1       (if necessary)
9827                * sarl $31, op1
9828                * orl cf, op1
9829                */
9830               if (ct != -1)
9831                 {
9832                   cf = ct;
9833                   ct = -1;
9834                   code = reverse_condition (code);
9835                 }
9836
9837               out = emit_store_flag (out, code, ix86_compare_op0,
9838                                      ix86_compare_op1, VOIDmode, 0, -1);
9839
9840               out = expand_simple_binop (mode, IOR,
9841                                          out, GEN_INT (cf),
9842                                          out, 1, OPTAB_DIRECT);
9843               if (out != operands[0])
9844                 emit_move_insn (operands[0], out);
9845
9846               return 1; /* DONE */
9847             }
9848         }
9849
9850
9851       if ((diff == 1 || diff == 2 || diff == 4 || diff == 8
9852            || diff == 3 || diff == 5 || diff == 9)
9853           && ((mode != QImode && mode != HImode) || !TARGET_PARTIAL_REG_STALL)
9854           && (mode != DImode
9855               || x86_64_immediate_operand (GEN_INT (cf), VOIDmode)))
9856         {
9857           /*
9858            * xorl dest,dest
9859            * cmpl op1,op2
9860            * setcc dest
9861            * lea cf(dest*(ct-cf)),dest
9862            *
9863            * Size 14.
9864            *
9865            * This also catches the degenerate setcc-only case.
9866            */
9867
9868           rtx tmp;
9869           int nops;
9870
9871           out = emit_store_flag (out, code, ix86_compare_op0,
9872                                  ix86_compare_op1, VOIDmode, 0, 1);
9873
9874           nops = 0;
9875           /* On x86_64 the lea instruction operates on Pmode, so we need
9876              to get arithmetics done in proper mode to match.  */
9877           if (diff == 1)
9878             tmp = copy_rtx (out);
9879           else
9880             {
9881               rtx out1;
9882               out1 = copy_rtx (out);
9883               tmp = gen_rtx_MULT (mode, out1, GEN_INT (diff & ~1));
9884               nops++;
9885               if (diff & 1)
9886                 {
9887                   tmp = gen_rtx_PLUS (mode, tmp, out1);
9888                   nops++;
9889                 }
9890             }
9891           if (cf != 0)
9892             {
9893               tmp = gen_rtx_PLUS (mode, tmp, GEN_INT (cf));
9894               nops++;
9895             }
9896           if (!rtx_equal_p (tmp, out))
9897             {
9898               if (nops == 1)
9899                 out = force_operand (tmp, copy_rtx (out));
9900               else
9901                 emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (out), copy_rtx (tmp)));
9902             }
9903           if (!rtx_equal_p (out, operands[0]))
9904             emit_move_insn (operands[0], copy_rtx (out));
9905
9906           return 1; /* DONE */
9907         }
9908
9909       /*
9910        * General case:                  Jumpful:
9911        *   xorl dest,dest               cmpl op1, op2
9912        *   cmpl op1, op2                movl ct, dest
9913        *   setcc dest                   jcc 1f
9914        *   decl dest                    movl cf, dest
9915        *   andl (cf-ct),dest            1:
9916        *   addl ct,dest
9917        *
9918        * Size 20.                       Size 14.
9919        *
9920        * This is reasonably steep, but branch mispredict costs are
9921        * high on modern cpus, so consider failing only if optimizing
9922        * for space.
9923        */
9924
9925       if ((!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9926           && BRANCH_COST >= 2)
9927         {
9928           if (cf == 0)
9929             {
9930               cf = ct;
9931               ct = 0;
9932               if (FLOAT_MODE_P (GET_MODE (ix86_compare_op0)))
9933                 /* We may be reversing unordered compare to normal compare,
9934                    that is not valid in general (we may convert non-trapping
9935                    condition to trapping one), however on i386 we currently
9936                    emit all comparisons unordered.  */
9937                 code = reverse_condition_maybe_unordered (code);
9938               else
9939                 {
9940                   code = reverse_condition (code);
9941                   if (compare_code != UNKNOWN)
9942                     compare_code = reverse_condition (compare_code);
9943                 }
9944             }
9945
9946           if (compare_code != UNKNOWN)
9947             {
9948               /* notl op1       (if needed)
9949                  sarl $31, op1
9950                  andl (cf-ct), op1
9951                  addl ct, op1
9952
9953                  For x < 0 (resp. x <= -1) there will be no notl,
9954                  so if possible swap the constants to get rid of the
9955                  complement.
9956                  True/false will be -1/0 while code below (store flag
9957                  followed by decrement) is 0/-1, so the constants need
9958                  to be exchanged once more.  */
9959
9960               if (compare_code == GE || !cf)
9961                 {
9962                   code = reverse_condition (code);
9963                   compare_code = LT;
9964                 }
9965               else
9966                 {
9967                   HOST_WIDE_INT tmp = cf;
9968                   cf = ct;
9969                   ct = tmp;
9970                 }
9971
9972               out = emit_store_flag (out, code, ix86_compare_op0,
9973                                      ix86_compare_op1, VOIDmode, 0, -1);
9974             }
9975           else
9976             {
9977               out = emit_store_flag (out, code, ix86_compare_op0,
9978                                      ix86_compare_op1, VOIDmode, 0, 1);
9979
9980               out = expand_simple_binop (mode, PLUS, copy_rtx (out), constm1_rtx,
9981                                          copy_rtx (out), 1, OPTAB_DIRECT);
9982             }
9983
9984           out = expand_simple_binop (mode, AND, copy_rtx (out),
9985                                      gen_int_mode (cf - ct, mode),
9986                                      copy_rtx (out), 1, OPTAB_DIRECT);
9987           if (ct)
9988             out = expand_simple_binop (mode, PLUS, copy_rtx (out), GEN_INT (ct),
9989                                        copy_rtx (out), 1, OPTAB_DIRECT);
9990           if (!rtx_equal_p (out, operands[0]))
9991             emit_move_insn (operands[0], copy_rtx (out));
9992
9993           return 1; /* DONE */
9994         }
9995     }
9996
9997   if (!TARGET_CMOVE || (mode == QImode && TARGET_PARTIAL_REG_STALL))
9998     {
9999       /* Try a few things more with specific constants and a variable.  */
10000
10001       optab op;
10002       rtx var, orig_out, out, tmp;
10003
10004       if (BRANCH_COST <= 2)
10005         return 0; /* FAIL */
10006
10007       /* If one of the two operands is an interesting constant, load a
10008          constant with the above and mask it in with a logical operation.  */
10009
10010       if (GET_CODE (operands[2]) == CONST_INT)
10011         {
10012           var = operands[3];
10013           if (INTVAL (operands[2]) == 0 && operands[3] != constm1_rtx)
10014             operands[3] = constm1_rtx, op = and_optab;
10015           else if (INTVAL (operands[2]) == -1 && operands[3] != const0_rtx)
10016             operands[3] = const0_rtx, op = ior_optab;
10017           else
10018             return 0; /* FAIL */
10019         }
10020       else if (GET_CODE (operands[3]) == CONST_INT)
10021         {
10022           var = operands[2];
10023           if (INTVAL (operands[3]) == 0 && operands[2] != constm1_rtx)
10024             operands[2] = constm1_rtx, op = and_optab;
10025           else if (INTVAL (operands[3]) == -1 && operands[3] != const0_rtx)
10026             operands[2] = const0_rtx, op = ior_optab;
10027           else
10028             return 0; /* FAIL */
10029         }
10030       else
10031         return 0; /* FAIL */
10032
10033       orig_out = operands[0];
10034       tmp = gen_reg_rtx (mode);
10035       operands[0] = tmp;
10036
10037       /* Recurse to get the constant loaded.  */
10038       if (ix86_expand_int_movcc (operands) == 0)
10039         return 0; /* FAIL */
10040
10041       /* Mask in the interesting variable.  */
10042       out = expand_binop (mode, op, var, tmp, orig_out, 0,
10043                           OPTAB_WIDEN);
10044       if (!rtx_equal_p (out, orig_out))
10045         emit_move_insn (copy_rtx (orig_out), copy_rtx (out));
10046
10047       return 1; /* DONE */
10048     }
10049
10050   /*
10051    * For comparison with above,
10052    *
10053    * movl cf,dest
10054    * movl ct,tmp
10055    * cmpl op1,op2
10056    * cmovcc tmp,dest
10057    *
10058    * Size 15.
10059    */
10060
10061   if (! nonimmediate_operand (operands[2], mode))
10062     operands[2] = force_reg (mode, operands[2]);
10063   if (! nonimmediate_operand (operands[3], mode))
10064     operands[3] = force_reg (mode, operands[3]);
10065
10066   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10067     {
10068       rtx tmp = gen_reg_rtx (mode);
10069       emit_move_insn (tmp, operands[3]);
10070       operands[3] = tmp;
10071     }
10072   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10073     {
10074       rtx tmp = gen_reg_rtx (mode);
10075       emit_move_insn (tmp, operands[2]);
10076       operands[2] = tmp;
10077     }
10078
10079   if (! register_operand (operands[2], VOIDmode)
10080       && (mode == QImode
10081           || ! register_operand (operands[3], VOIDmode)))
10082     operands[2] = force_reg (mode, operands[2]);
10083
10084   if (mode == QImode
10085       && ! register_operand (operands[3], VOIDmode))
10086     operands[3] = force_reg (mode, operands[3]);
10087
10088   emit_insn (compare_seq);
10089   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10090                           gen_rtx_IF_THEN_ELSE (mode,
10091                                                 compare_op, operands[2],
10092                                                 operands[3])));
10093   if (bypass_test)
10094     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10095                             gen_rtx_IF_THEN_ELSE (mode,
10096                                   bypass_test,
10097                                   copy_rtx (operands[3]),
10098                                   copy_rtx (operands[0]))));
10099   if (second_test)
10100     emit_insn (gen_rtx_SET (VOIDmode, copy_rtx (operands[0]),
10101                             gen_rtx_IF_THEN_ELSE (mode,
10102                                   second_test,
10103                                   copy_rtx (operands[2]),
10104                                   copy_rtx (operands[0]))));
10105
10106   return 1; /* DONE */
10107 }
10108
10109 /* Swap, force into registers, or otherwise massage the two operands
10110    to an sse comparison with a mask result.  Thus we differ a bit from
10111    ix86_prepare_fp_compare_args which expects to produce a flags result.
10112
10113    The DEST operand exists to help determine whether to commute commutative
10114    operators.  The POP0/POP1 operands are updated in place.  The new
10115    comparison code is returned, or UNKNOWN if not implementable.  */
10116
10117 static enum rtx_code
10118 ix86_prepare_sse_fp_compare_args (rtx dest, enum rtx_code code,
10119                                   rtx *pop0, rtx *pop1)
10120 {
10121   rtx tmp;
10122
10123   switch (code)
10124     {
10125     case LTGT:
10126     case UNEQ:
10127       /* We have no LTGT as an operator.  We could implement it with
10128          NE & ORDERED, but this requires an extra temporary.  It's
10129          not clear that it's worth it.  */
10130       return UNKNOWN;
10131
10132     case LT:
10133     case LE:
10134     case UNGT:
10135     case UNGE:
10136       /* These are supported directly.  */
10137       break;
10138
10139     case EQ:
10140     case NE:
10141     case UNORDERED:
10142     case ORDERED:
10143       /* For commutative operators, try to canonicalize the destination
10144          operand to be first in the comparison - this helps reload to
10145          avoid extra moves.  */
10146       if (!dest || !rtx_equal_p (dest, *pop1))
10147         break;
10148       /* FALLTHRU */
10149
10150     case GE:
10151     case GT:
10152     case UNLE:
10153     case UNLT:
10154       /* These are not supported directly.  Swap the comparison operands
10155          to transform into something that is supported.  */
10156       tmp = *pop0;
10157       *pop0 = *pop1;
10158       *pop1 = tmp;
10159       code = swap_condition (code);
10160       break;
10161
10162     default:
10163       gcc_unreachable ();
10164     }
10165
10166   return code;
10167 }
10168
10169 /* Detect conditional moves that exactly match min/max operational
10170    semantics.  Note that this is IEEE safe, as long as we don't
10171    interchange the operands.
10172
10173    Returns FALSE if this conditional move doesn't match a MIN/MAX,
10174    and TRUE if the operation is successful and instructions are emitted.  */
10175
10176 static bool
10177 ix86_expand_sse_fp_minmax (rtx dest, enum rtx_code code, rtx cmp_op0,
10178                            rtx cmp_op1, rtx if_true, rtx if_false)
10179 {
10180   enum machine_mode mode;
10181   bool is_min;
10182   rtx tmp;
10183
10184   if (code == LT)
10185     ;
10186   else if (code == UNGE)
10187     {
10188       tmp = if_true;
10189       if_true = if_false;
10190       if_false = tmp;
10191     }
10192   else
10193     return false;
10194
10195   if (rtx_equal_p (cmp_op0, if_true) && rtx_equal_p (cmp_op1, if_false))
10196     is_min = true;
10197   else if (rtx_equal_p (cmp_op1, if_true) && rtx_equal_p (cmp_op0, if_false))
10198     is_min = false;
10199   else
10200     return false;
10201
10202   mode = GET_MODE (dest);
10203
10204   /* We want to check HONOR_NANS and HONOR_SIGNED_ZEROS here,
10205      but MODE may be a vector mode and thus not appropriate.  */
10206   if (!flag_finite_math_only || !flag_unsafe_math_optimizations)
10207     {
10208       int u = is_min ? UNSPEC_IEEE_MIN : UNSPEC_IEEE_MAX;
10209       rtvec v;
10210
10211       if_true = force_reg (mode, if_true);
10212       v = gen_rtvec (2, if_true, if_false);
10213       tmp = gen_rtx_UNSPEC (mode, v, u);
10214     }
10215   else
10216     {
10217       code = is_min ? SMIN : SMAX;
10218       tmp = gen_rtx_fmt_ee (code, mode, if_true, if_false);
10219     }
10220
10221   emit_insn (gen_rtx_SET (VOIDmode, dest, tmp));
10222   return true;
10223 }
10224
10225 /* Expand an sse vector comparison.  Return the register with the result.  */
10226
10227 static rtx
10228 ix86_expand_sse_cmp (rtx dest, enum rtx_code code, rtx cmp_op0, rtx cmp_op1,
10229                      rtx op_true, rtx op_false)
10230 {
10231   enum machine_mode mode = GET_MODE (dest);
10232   rtx x;
10233
10234   cmp_op0 = force_reg (mode, cmp_op0);
10235   if (!nonimmediate_operand (cmp_op1, mode))
10236     cmp_op1 = force_reg (mode, cmp_op1);
10237
10238   if (optimize
10239       || reg_overlap_mentioned_p (dest, op_true)
10240       || reg_overlap_mentioned_p (dest, op_false))
10241     dest = gen_reg_rtx (mode);
10242
10243   x = gen_rtx_fmt_ee (code, mode, cmp_op0, cmp_op1);
10244   emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10245
10246   return dest;
10247 }
10248
10249 /* Expand DEST = CMP ? OP_TRUE : OP_FALSE into a sequence of logical
10250    operations.  This is used for both scalar and vector conditional moves.  */
10251
10252 static void
10253 ix86_expand_sse_movcc (rtx dest, rtx cmp, rtx op_true, rtx op_false)
10254 {
10255   enum machine_mode mode = GET_MODE (dest);
10256   rtx t2, t3, x;
10257
10258   if (op_false == CONST0_RTX (mode))
10259     {
10260       op_true = force_reg (mode, op_true);
10261       x = gen_rtx_AND (mode, cmp, op_true);
10262       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10263     }
10264   else if (op_true == CONST0_RTX (mode))
10265     {
10266       op_false = force_reg (mode, op_false);
10267       x = gen_rtx_NOT (mode, cmp);
10268       x = gen_rtx_AND (mode, x, op_false);
10269       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10270     }
10271   else
10272     {
10273       op_true = force_reg (mode, op_true);
10274       op_false = force_reg (mode, op_false);
10275
10276       t2 = gen_reg_rtx (mode);
10277       if (optimize)
10278         t3 = gen_reg_rtx (mode);
10279       else
10280         t3 = dest;
10281
10282       x = gen_rtx_AND (mode, op_true, cmp);
10283       emit_insn (gen_rtx_SET (VOIDmode, t2, x));
10284
10285       x = gen_rtx_NOT (mode, cmp);
10286       x = gen_rtx_AND (mode, x, op_false);
10287       emit_insn (gen_rtx_SET (VOIDmode, t3, x));
10288
10289       x = gen_rtx_IOR (mode, t3, t2);
10290       emit_insn (gen_rtx_SET (VOIDmode, dest, x));
10291     }
10292 }
10293
10294 /* Expand a floating-point conditional move.  Return true if successful.  */
10295
10296 int
10297 ix86_expand_fp_movcc (rtx operands[])
10298 {
10299   enum machine_mode mode = GET_MODE (operands[0]);
10300   enum rtx_code code = GET_CODE (operands[1]);
10301   rtx tmp, compare_op, second_test, bypass_test;
10302
10303   if (TARGET_SSE_MATH && SSE_FLOAT_MODE_P (mode))
10304     {
10305       enum machine_mode cmode;
10306
10307       /* Since we've no cmove for sse registers, don't force bad register
10308          allocation just to gain access to it.  Deny movcc when the
10309          comparison mode doesn't match the move mode.  */
10310       cmode = GET_MODE (ix86_compare_op0);
10311       if (cmode == VOIDmode)
10312         cmode = GET_MODE (ix86_compare_op1);
10313       if (cmode != mode)
10314         return 0;
10315
10316       code = ix86_prepare_sse_fp_compare_args (operands[0], code,
10317                                                &ix86_compare_op0,
10318                                                &ix86_compare_op1);
10319       if (code == UNKNOWN)
10320         return 0;
10321
10322       if (ix86_expand_sse_fp_minmax (operands[0], code, ix86_compare_op0,
10323                                      ix86_compare_op1, operands[2],
10324                                      operands[3]))
10325         return 1;
10326
10327       tmp = ix86_expand_sse_cmp (operands[0], code, ix86_compare_op0,
10328                                  ix86_compare_op1, operands[2], operands[3]);
10329       ix86_expand_sse_movcc (operands[0], tmp, operands[2], operands[3]);
10330       return 1;
10331     }
10332
10333   /* The floating point conditional move instructions don't directly
10334      support conditions resulting from a signed integer comparison.  */
10335
10336   compare_op = ix86_expand_compare (code, &second_test, &bypass_test);
10337
10338   /* The floating point conditional move instructions don't directly
10339      support signed integer comparisons.  */
10340
10341   if (!fcmov_comparison_operator (compare_op, VOIDmode))
10342     {
10343       gcc_assert (!second_test && !bypass_test);
10344       tmp = gen_reg_rtx (QImode);
10345       ix86_expand_setcc (code, tmp);
10346       code = NE;
10347       ix86_compare_op0 = tmp;
10348       ix86_compare_op1 = const0_rtx;
10349       compare_op = ix86_expand_compare (code,  &second_test, &bypass_test);
10350     }
10351   if (bypass_test && reg_overlap_mentioned_p (operands[0], operands[3]))
10352     {
10353       tmp = gen_reg_rtx (mode);
10354       emit_move_insn (tmp, operands[3]);
10355       operands[3] = tmp;
10356     }
10357   if (second_test && reg_overlap_mentioned_p (operands[0], operands[2]))
10358     {
10359       tmp = gen_reg_rtx (mode);
10360       emit_move_insn (tmp, operands[2]);
10361       operands[2] = tmp;
10362     }
10363
10364   emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10365                           gen_rtx_IF_THEN_ELSE (mode, compare_op,
10366                                                 operands[2], operands[3])));
10367   if (bypass_test)
10368     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10369                             gen_rtx_IF_THEN_ELSE (mode, bypass_test,
10370                                                   operands[3], operands[0])));
10371   if (second_test)
10372     emit_insn (gen_rtx_SET (VOIDmode, operands[0],
10373                             gen_rtx_IF_THEN_ELSE (mode, second_test,
10374                                                   operands[2], operands[0])));
10375
10376   return 1;
10377 }
10378
10379 /* Expand a floating-point vector conditional move; a vcond operation
10380    rather than a movcc operation.  */
10381
10382 bool
10383 ix86_expand_fp_vcond (rtx operands[])
10384 {
10385   enum rtx_code code = GET_CODE (operands[3]);
10386   rtx cmp;
10387
10388   code = ix86_prepare_sse_fp_compare_args (operands[0], code,
10389                                            &operands[4], &operands[5]);
10390   if (code == UNKNOWN)
10391     return false;
10392
10393   if (ix86_expand_sse_fp_minmax (operands[0], code, operands[4],
10394                                  operands[5], operands[1], operands[2]))
10395     return true;
10396
10397   cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
10398                              operands[1], operands[2]);
10399   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
10400   return true;
10401 }
10402
10403 /* Expand a signed integral vector conditional move.  */
10404
10405 bool
10406 ix86_expand_int_vcond (rtx operands[], bool unsignedp)
10407 {
10408   enum machine_mode mode = GET_MODE (operands[0]);
10409   enum rtx_code code = GET_CODE (operands[3]);
10410   rtx cmp, x;
10411
10412   if (unsignedp)
10413     code = signed_condition (code);
10414   if (code == NE || code == LE || code == GE)
10415     {
10416       /* Inverse of a supported code.  */
10417       x = operands[1];
10418       operands[1] = operands[2];
10419       operands[2] = x;
10420       code = reverse_condition (code);
10421     }
10422   if (code == LT)
10423     {
10424       /* Swap of a supported code.  */
10425       x = operands[4];
10426       operands[4] = operands[5];
10427       operands[5] = x;
10428       code = swap_condition (code);
10429     }
10430   gcc_assert (code == EQ || code == GT);
10431
10432   /* Unlike floating-point, we can rely on the optimizers to have already
10433      converted to MIN/MAX expressions, so we don't have to handle that.  */
10434
10435   /* Unsigned GT is not directly supported.  We can zero-extend QI and
10436      HImode elements to the next wider element size, use a signed compare,
10437      then repack.  For three extra instructions, this is definitely a win.  */
10438   if (code == GT && unsignedp)
10439     {
10440       rtx o0l, o0h, o1l, o1h, cl, ch, zero;
10441       enum machine_mode wider;
10442       rtx (*unpackl) (rtx, rtx, rtx);
10443       rtx (*unpackh) (rtx, rtx, rtx);
10444       rtx (*pack) (rtx, rtx, rtx);
10445
10446       switch (mode)
10447         {
10448         case V16QImode:
10449           wider = V8HImode;
10450           unpackl = gen_sse2_punpcklbw;
10451           unpackh = gen_sse2_punpckhbw;
10452           pack = gen_sse2_packsswb;
10453           break;
10454         case V8HImode:
10455           wider = V4SImode;
10456           unpackl = gen_sse2_punpcklwd;
10457           unpackh = gen_sse2_punpckhwd;
10458           pack = gen_sse2_packssdw;
10459           break;
10460         default:
10461           gcc_unreachable ();
10462         }
10463
10464       operands[4] = force_reg (mode, operands[4]);
10465       operands[5] = force_reg (mode, operands[5]);
10466
10467       o0l = gen_reg_rtx (wider);
10468       o0h = gen_reg_rtx (wider);
10469       o1l = gen_reg_rtx (wider);
10470       o1h = gen_reg_rtx (wider);
10471       cl = gen_reg_rtx (wider);
10472       ch = gen_reg_rtx (wider);
10473       cmp = gen_reg_rtx (mode);
10474       zero = force_reg (mode, CONST0_RTX (mode));
10475
10476       emit_insn (unpackl (gen_lowpart (mode, o0l), operands[4], zero));
10477       emit_insn (unpackh (gen_lowpart (mode, o0h), operands[4], zero));
10478       emit_insn (unpackl (gen_lowpart (mode, o1l), operands[5], zero));
10479       emit_insn (unpackh (gen_lowpart (mode, o1h), operands[5], zero));
10480
10481       x = gen_rtx_GT (wider, o0l, o1l);
10482       emit_insn (gen_rtx_SET (VOIDmode, cl, x));
10483
10484       x = gen_rtx_GT (wider, o0h, o1h);
10485       emit_insn (gen_rtx_SET (VOIDmode, ch, x));
10486
10487       emit_insn (pack (cmp, cl, ch));
10488     }
10489   else
10490     cmp = ix86_expand_sse_cmp (operands[0], code, operands[4], operands[5],
10491                                operands[1], operands[2]);
10492
10493   ix86_expand_sse_movcc (operands[0], cmp, operands[1], operands[2]);
10494   return true;
10495 }
10496
10497 /* Expand conditional increment or decrement using adb/sbb instructions.
10498    The default case using setcc followed by the conditional move can be
10499    done by generic code.  */
10500 int
10501 ix86_expand_int_addcc (rtx operands[])
10502 {
10503   enum rtx_code code = GET_CODE (operands[1]);
10504   rtx compare_op;
10505   rtx val = const0_rtx;
10506   bool fpcmp = false;
10507   enum machine_mode mode = GET_MODE (operands[0]);
10508
10509   if (operands[3] != const1_rtx
10510       && operands[3] != constm1_rtx)
10511     return 0;
10512   if (!ix86_expand_carry_flag_compare (code, ix86_compare_op0,
10513                                        ix86_compare_op1, &compare_op))
10514      return 0;
10515   code = GET_CODE (compare_op);
10516
10517   if (GET_MODE (XEXP (compare_op, 0)) == CCFPmode
10518       || GET_MODE (XEXP (compare_op, 0)) == CCFPUmode)
10519     {
10520       fpcmp = true;
10521       code = ix86_fp_compare_code_to_integer (code);
10522     }
10523
10524   if (code != LTU)
10525     {
10526       val = constm1_rtx;
10527       if (fpcmp)
10528         PUT_CODE (compare_op,
10529                   reverse_condition_maybe_unordered
10530                     (GET_CODE (compare_op)));
10531       else
10532         PUT_CODE (compare_op, reverse_condition (GET_CODE (compare_op)));
10533     }
10534   PUT_MODE (compare_op, mode);
10535
10536   /* Construct either adc or sbb insn.  */
10537   if ((code == LTU) == (operands[3] == constm1_rtx))
10538     {
10539       switch (GET_MODE (operands[0]))
10540         {
10541           case QImode:
10542             emit_insn (gen_subqi3_carry (operands[0], operands[2], val, compare_op));
10543             break;
10544           case HImode:
10545             emit_insn (gen_subhi3_carry (operands[0], operands[2], val, compare_op));
10546             break;
10547           case SImode:
10548             emit_insn (gen_subsi3_carry (operands[0], operands[2], val, compare_op));
10549             break;
10550           case DImode:
10551             emit_insn (gen_subdi3_carry_rex64 (operands[0], operands[2], val, compare_op));
10552             break;
10553           default:
10554             gcc_unreachable ();
10555         }
10556     }
10557   else
10558     {
10559       switch (GET_MODE (operands[0]))
10560         {
10561           case QImode:
10562             emit_insn (gen_addqi3_carry (operands[0], operands[2], val, compare_op));
10563             break;
10564           case HImode:
10565             emit_insn (gen_addhi3_carry (operands[0], operands[2], val, compare_op));
10566             break;
10567           case SImode:
10568             emit_insn (gen_addsi3_carry (operands[0], operands[2], val, compare_op));
10569             break;
10570           case DImode:
10571             emit_insn (gen_adddi3_carry_rex64 (operands[0], operands[2], val, compare_op));
10572             break;
10573           default:
10574             gcc_unreachable ();
10575         }
10576     }
10577   return 1; /* DONE */
10578 }
10579
10580
10581 /* Split operands 0 and 1 into SImode parts.  Similar to split_di, but
10582    works for floating pointer parameters and nonoffsetable memories.
10583    For pushes, it returns just stack offsets; the values will be saved
10584    in the right order.  Maximally three parts are generated.  */
10585
10586 static int
10587 ix86_split_to_parts (rtx operand, rtx *parts, enum machine_mode mode)
10588 {
10589   int size;
10590
10591   if (!TARGET_64BIT)
10592     size = mode==XFmode ? 3 : GET_MODE_SIZE (mode) / 4;
10593   else
10594     size = (GET_MODE_SIZE (mode) + 4) / 8;
10595
10596   gcc_assert (GET_CODE (operand) != REG || !MMX_REGNO_P (REGNO (operand)));
10597   gcc_assert (size >= 2 && size <= 3);
10598
10599   /* Optimize constant pool reference to immediates.  This is used by fp
10600      moves, that force all constants to memory to allow combining.  */
10601   if (GET_CODE (operand) == MEM && MEM_READONLY_P (operand))
10602     {
10603       rtx tmp = maybe_get_pool_constant (operand);
10604       if (tmp)
10605         operand = tmp;
10606     }
10607
10608   if (GET_CODE (operand) == MEM && !offsettable_memref_p (operand))
10609     {
10610       /* The only non-offsetable memories we handle are pushes.  */
10611       int ok = push_operand (operand, VOIDmode);
10612       
10613       gcc_assert (ok);
10614       
10615       operand = copy_rtx (operand);
10616       PUT_MODE (operand, Pmode);
10617       parts[0] = parts[1] = parts[2] = operand;
10618       return size;
10619     }
10620
10621   if (GET_CODE (operand) == CONST_VECTOR)
10622     {
10623       enum machine_mode imode = int_mode_for_mode (mode);
10624       operand = simplify_subreg (imode, operand, mode, 0);
10625       gcc_assert (operand != NULL);
10626       mode = imode;
10627     }
10628
10629   if (!TARGET_64BIT)
10630     {
10631       if (mode == DImode)
10632         split_di (&operand, 1, &parts[0], &parts[1]);
10633       else
10634         {
10635           if (REG_P (operand))
10636             {
10637               gcc_assert (reload_completed);
10638               parts[0] = gen_rtx_REG (SImode, REGNO (operand) + 0);
10639               parts[1] = gen_rtx_REG (SImode, REGNO (operand) + 1);
10640               if (size == 3)
10641                 parts[2] = gen_rtx_REG (SImode, REGNO (operand) + 2);
10642             }
10643           else if (offsettable_memref_p (operand))
10644             {
10645               operand = adjust_address (operand, SImode, 0);
10646               parts[0] = operand;
10647               parts[1] = adjust_address (operand, SImode, 4);
10648               if (size == 3)
10649                 parts[2] = adjust_address (operand, SImode, 8);
10650             }
10651           else if (GET_CODE (operand) == CONST_DOUBLE)
10652             {
10653               REAL_VALUE_TYPE r;
10654               long l[4];
10655
10656               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10657               switch (mode)
10658                 {
10659                 case XFmode:
10660                   REAL_VALUE_TO_TARGET_LONG_DOUBLE (r, l);
10661                   parts[2] = gen_int_mode (l[2], SImode);
10662                   break;
10663                 case DFmode:
10664                   REAL_VALUE_TO_TARGET_DOUBLE (r, l);
10665                   break;
10666                 default:
10667                   gcc_unreachable ();
10668                 }
10669               parts[1] = gen_int_mode (l[1], SImode);
10670               parts[0] = gen_int_mode (l[0], SImode);
10671             }
10672           else
10673             gcc_unreachable ();
10674         }
10675     }
10676   else
10677     {
10678       if (mode == TImode)
10679         split_ti (&operand, 1, &parts[0], &parts[1]);
10680       if (mode == XFmode || mode == TFmode)
10681         {
10682           enum machine_mode upper_mode = mode==XFmode ? SImode : DImode;
10683           if (REG_P (operand))
10684             {
10685               gcc_assert (reload_completed);
10686               parts[0] = gen_rtx_REG (DImode, REGNO (operand) + 0);
10687               parts[1] = gen_rtx_REG (upper_mode, REGNO (operand) + 1);
10688             }
10689           else if (offsettable_memref_p (operand))
10690             {
10691               operand = adjust_address (operand, DImode, 0);
10692               parts[0] = operand;
10693               parts[1] = adjust_address (operand, upper_mode, 8);
10694             }
10695           else if (GET_CODE (operand) == CONST_DOUBLE)
10696             {
10697               REAL_VALUE_TYPE r;
10698               long l[4];
10699
10700               REAL_VALUE_FROM_CONST_DOUBLE (r, operand);
10701               real_to_target (l, &r, mode);
10702
10703               /* Do not use shift by 32 to avoid warning on 32bit systems.  */
10704               if (HOST_BITS_PER_WIDE_INT >= 64)
10705                 parts[0]
10706                   = gen_int_mode
10707                       ((l[0] & (((HOST_WIDE_INT) 2 << 31) - 1))
10708                        + ((((HOST_WIDE_INT) l[1]) << 31) << 1),
10709                        DImode);
10710               else
10711                 parts[0] = immed_double_const (l[0], l[1], DImode);
10712
10713               if (upper_mode == SImode)
10714                 parts[1] = gen_int_mode (l[2], SImode);
10715               else if (HOST_BITS_PER_WIDE_INT >= 64)
10716                 parts[1]
10717                   = gen_int_mode
10718                       ((l[2] & (((HOST_WIDE_INT) 2 << 31) - 1))
10719                        + ((((HOST_WIDE_INT) l[3]) << 31) << 1),
10720                        DImode);
10721               else
10722                 parts[1] = immed_double_const (l[2], l[3], DImode);
10723             }
10724           else
10725             gcc_unreachable ();
10726         }
10727     }
10728
10729   return size;
10730 }
10731
10732 /* Emit insns to perform a move or push of DI, DF, and XF values.
10733    Return false when normal moves are needed; true when all required
10734    insns have been emitted.  Operands 2-4 contain the input values
10735    int the correct order; operands 5-7 contain the output values.  */
10736
10737 void
10738 ix86_split_long_move (rtx operands[])
10739 {
10740   rtx part[2][3];
10741   int nparts;
10742   int push = 0;
10743   int collisions = 0;
10744   enum machine_mode mode = GET_MODE (operands[0]);
10745
10746   /* The DFmode expanders may ask us to move double.
10747      For 64bit target this is single move.  By hiding the fact
10748      here we simplify i386.md splitters.  */
10749   if (GET_MODE_SIZE (GET_MODE (operands[0])) == 8 && TARGET_64BIT)
10750     {
10751       /* Optimize constant pool reference to immediates.  This is used by
10752          fp moves, that force all constants to memory to allow combining.  */
10753
10754       if (GET_CODE (operands[1]) == MEM
10755           && GET_CODE (XEXP (operands[1], 0)) == SYMBOL_REF
10756           && CONSTANT_POOL_ADDRESS_P (XEXP (operands[1], 0)))
10757         operands[1] = get_pool_constant (XEXP (operands[1], 0));
10758       if (push_operand (operands[0], VOIDmode))
10759         {
10760           operands[0] = copy_rtx (operands[0]);
10761           PUT_MODE (operands[0], Pmode);
10762         }
10763       else
10764         operands[0] = gen_lowpart (DImode, operands[0]);
10765       operands[1] = gen_lowpart (DImode, operands[1]);
10766       emit_move_insn (operands[0], operands[1]);
10767       return;
10768     }
10769
10770   /* The only non-offsettable memory we handle is push.  */
10771   if (push_operand (operands[0], VOIDmode))
10772     push = 1;
10773   else
10774     gcc_assert (GET_CODE (operands[0]) != MEM
10775                 || offsettable_memref_p (operands[0]));
10776
10777   nparts = ix86_split_to_parts (operands[1], part[1], GET_MODE (operands[0]));
10778   ix86_split_to_parts (operands[0], part[0], GET_MODE (operands[0]));
10779
10780   /* When emitting push, take care for source operands on the stack.  */
10781   if (push && GET_CODE (operands[1]) == MEM
10782       && reg_overlap_mentioned_p (stack_pointer_rtx, operands[1]))
10783     {
10784       if (nparts == 3)
10785         part[1][1] = change_address (part[1][1], GET_MODE (part[1][1]),
10786                                      XEXP (part[1][2], 0));
10787       part[1][0] = change_address (part[1][0], GET_MODE (part[1][0]),
10788                                    XEXP (part[1][1], 0));
10789     }
10790
10791   /* We need to do copy in the right order in case an address register
10792      of the source overlaps the destination.  */
10793   if (REG_P (part[0][0]) && GET_CODE (part[1][0]) == MEM)
10794     {
10795       if (reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0)))
10796         collisions++;
10797       if (reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10798         collisions++;
10799       if (nparts == 3
10800           && reg_overlap_mentioned_p (part[0][2], XEXP (part[1][0], 0)))
10801         collisions++;
10802
10803       /* Collision in the middle part can be handled by reordering.  */
10804       if (collisions == 1 && nparts == 3
10805           && reg_overlap_mentioned_p (part[0][1], XEXP (part[1][0], 0)))
10806         {
10807           rtx tmp;
10808           tmp = part[0][1]; part[0][1] = part[0][2]; part[0][2] = tmp;
10809           tmp = part[1][1]; part[1][1] = part[1][2]; part[1][2] = tmp;
10810         }
10811
10812       /* If there are more collisions, we can't handle it by reordering.
10813          Do an lea to the last part and use only one colliding move.  */
10814       else if (collisions > 1)
10815         {
10816           rtx base;
10817
10818           collisions = 1;
10819
10820           base = part[0][nparts - 1];
10821
10822           /* Handle the case when the last part isn't valid for lea.
10823              Happens in 64-bit mode storing the 12-byte XFmode.  */
10824           if (GET_MODE (base) != Pmode)
10825             base = gen_rtx_REG (Pmode, REGNO (base));
10826
10827           emit_insn (gen_rtx_SET (VOIDmode, base, XEXP (part[1][0], 0)));
10828           part[1][0] = replace_equiv_address (part[1][0], base);
10829           part[1][1] = replace_equiv_address (part[1][1],
10830                                       plus_constant (base, UNITS_PER_WORD));
10831           if (nparts == 3)
10832             part[1][2] = replace_equiv_address (part[1][2],
10833                                       plus_constant (base, 8));
10834         }
10835     }
10836
10837   if (push)
10838     {
10839       if (!TARGET_64BIT)
10840         {
10841           if (nparts == 3)
10842             {
10843               if (TARGET_128BIT_LONG_DOUBLE && mode == XFmode)
10844                 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, GEN_INT (-4)));
10845               emit_move_insn (part[0][2], part[1][2]);
10846             }
10847         }
10848       else
10849         {
10850           /* In 64bit mode we don't have 32bit push available.  In case this is
10851              register, it is OK - we will just use larger counterpart.  We also
10852              retype memory - these comes from attempt to avoid REX prefix on
10853              moving of second half of TFmode value.  */
10854           if (GET_MODE (part[1][1]) == SImode)
10855             {
10856               switch (GET_CODE (part[1][1]))
10857                 {
10858                 case MEM:
10859                   part[1][1] = adjust_address (part[1][1], DImode, 0);
10860                   break;
10861
10862                 case REG:
10863                   part[1][1] = gen_rtx_REG (DImode, REGNO (part[1][1]));
10864                   break;
10865
10866                 default:
10867                   gcc_unreachable ();
10868                 }
10869               
10870               if (GET_MODE (part[1][0]) == SImode)
10871                 part[1][0] = part[1][1];
10872             }
10873         }
10874       emit_move_insn (part[0][1], part[1][1]);
10875       emit_move_insn (part[0][0], part[1][0]);
10876       return;
10877     }
10878
10879   /* Choose correct order to not overwrite the source before it is copied.  */
10880   if ((REG_P (part[0][0])
10881        && REG_P (part[1][1])
10882        && (REGNO (part[0][0]) == REGNO (part[1][1])
10883            || (nparts == 3
10884                && REGNO (part[0][0]) == REGNO (part[1][2]))))
10885       || (collisions > 0
10886           && reg_overlap_mentioned_p (part[0][0], XEXP (part[1][0], 0))))
10887     {
10888       if (nparts == 3)
10889         {
10890           operands[2] = part[0][2];
10891           operands[3] = part[0][1];
10892           operands[4] = part[0][0];
10893           operands[5] = part[1][2];
10894           operands[6] = part[1][1];
10895           operands[7] = part[1][0];
10896         }
10897       else
10898         {
10899           operands[2] = part[0][1];
10900           operands[3] = part[0][0];
10901           operands[5] = part[1][1];
10902           operands[6] = part[1][0];
10903         }
10904     }
10905   else
10906     {
10907       if (nparts == 3)
10908         {
10909           operands[2] = part[0][0];
10910           operands[3] = part[0][1];
10911           operands[4] = part[0][2];
10912           operands[5] = part[1][0];
10913           operands[6] = part[1][1];
10914           operands[7] = part[1][2];
10915         }
10916       else
10917         {
10918           operands[2] = part[0][0];
10919           operands[3] = part[0][1];
10920           operands[5] = part[1][0];
10921           operands[6] = part[1][1];
10922         }
10923     }
10924
10925   /* If optimizing for size, attempt to locally unCSE nonzero constants.  */
10926   if (optimize_size)
10927     {
10928       if (GET_CODE (operands[5]) == CONST_INT
10929           && operands[5] != const0_rtx
10930           && REG_P (operands[2]))
10931         {
10932           if (GET_CODE (operands[6]) == CONST_INT
10933               && INTVAL (operands[6]) == INTVAL (operands[5]))
10934             operands[6] = operands[2];
10935
10936           if (nparts == 3
10937               && GET_CODE (operands[7]) == CONST_INT
10938               && INTVAL (operands[7]) == INTVAL (operands[5]))
10939             operands[7] = operands[2];
10940         }
10941
10942       if (nparts == 3
10943           && GET_CODE (operands[6]) == CONST_INT
10944           && operands[6] != const0_rtx
10945           && REG_P (operands[3])
10946           && GET_CODE (operands[7]) == CONST_INT
10947           && INTVAL (operands[7]) == INTVAL (operands[6]))
10948         operands[7] = operands[3];
10949     }
10950
10951   emit_move_insn (operands[2], operands[5]);
10952   emit_move_insn (operands[3], operands[6]);
10953   if (nparts == 3)
10954     emit_move_insn (operands[4], operands[7]);
10955
10956   return;
10957 }
10958
10959 /* Helper function of ix86_split_ashldi used to generate an SImode
10960    left shift by a constant, either using a single shift or
10961    a sequence of add instructions.  */
10962
10963 static void
10964 ix86_expand_ashlsi3_const (rtx operand, int count)
10965 {
10966   if (count == 1)
10967     emit_insn (gen_addsi3 (operand, operand, operand));
10968   else if (!optimize_size
10969            && count * ix86_cost->add <= ix86_cost->shift_const)
10970     {
10971       int i;
10972       for (i=0; i<count; i++)
10973         emit_insn (gen_addsi3 (operand, operand, operand));
10974     }
10975   else
10976     emit_insn (gen_ashlsi3 (operand, operand, GEN_INT (count)));
10977 }
10978
10979 void
10980 ix86_split_ashldi (rtx *operands, rtx scratch)
10981 {
10982   rtx low[2], high[2];
10983   int count;
10984
10985   if (GET_CODE (operands[2]) == CONST_INT)
10986     {
10987       split_di (operands, 2, low, high);
10988       count = INTVAL (operands[2]) & 63;
10989
10990       if (count >= 32)
10991         {
10992           emit_move_insn (high[0], low[1]);
10993           emit_move_insn (low[0], const0_rtx);
10994
10995           if (count > 32)
10996             ix86_expand_ashlsi3_const (high[0], count - 32);
10997         }
10998       else
10999         {
11000           if (!rtx_equal_p (operands[0], operands[1]))
11001             emit_move_insn (operands[0], operands[1]);
11002           emit_insn (gen_x86_shld_1 (high[0], low[0], GEN_INT (count)));
11003           ix86_expand_ashlsi3_const (low[0], count);
11004         }
11005       return;
11006     }
11007
11008   split_di (operands, 1, low, high);
11009
11010   if (operands[1] == const1_rtx)
11011     {
11012       /* Assuming we've chosen a QImode capable registers, then 1LL << N
11013          can be done with two 32-bit shifts, no branches, no cmoves.  */
11014       if (ANY_QI_REG_P (low[0]) && ANY_QI_REG_P (high[0]))
11015         {
11016           rtx s, d, flags = gen_rtx_REG (CCZmode, FLAGS_REG);
11017
11018           ix86_expand_clear (low[0]);
11019           ix86_expand_clear (high[0]);
11020           emit_insn (gen_testqi_ccz_1 (operands[2], GEN_INT (32)));
11021           
11022           d = gen_lowpart (QImode, low[0]);
11023           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
11024           s = gen_rtx_EQ (QImode, flags, const0_rtx);
11025           emit_insn (gen_rtx_SET (VOIDmode, d, s));
11026
11027           d = gen_lowpart (QImode, high[0]);
11028           d = gen_rtx_STRICT_LOW_PART (VOIDmode, d);
11029           s = gen_rtx_NE (QImode, flags, const0_rtx);
11030           emit_insn (gen_rtx_SET (VOIDmode, d, s));
11031         }
11032
11033       /* Otherwise, we can get the same results by manually performing
11034          a bit extract operation on bit 5, and then performing the two
11035          shifts.  The two methods of getting 0/1 into low/high are exactly
11036          the same size.  Avoiding the shift in the bit extract case helps
11037          pentium4 a bit; no one else seems to care much either way.  */
11038       else
11039         {
11040           rtx x;
11041
11042           if (TARGET_PARTIAL_REG_STALL && !optimize_size)
11043             x = gen_rtx_ZERO_EXTEND (SImode, operands[2]);
11044           else
11045             x = gen_lowpart (SImode, operands[2]);
11046           emit_insn (gen_rtx_SET (VOIDmode, high[0], x));
11047
11048           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (5)));
11049           emit_insn (gen_andsi3 (high[0], high[0], GEN_INT (1)));
11050           emit_move_insn (low[0], high[0]);
11051           emit_insn (gen_xorsi3 (low[0], low[0], GEN_INT (1)));
11052         }
11053
11054       emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
11055       emit_insn (gen_ashlsi3 (high[0], high[0], operands[2]));
11056       return;
11057     }
11058
11059   if (operands[1] == constm1_rtx)
11060     {
11061       /* For -1LL << N, we can avoid the shld instruction, because we
11062          know that we're shifting 0...31 ones into a -1.  */
11063       emit_move_insn (low[0], constm1_rtx);
11064       if (optimize_size)
11065         emit_move_insn (high[0], low[0]);
11066       else
11067         emit_move_insn (high[0], constm1_rtx);
11068     }
11069   else
11070     {
11071       if (!rtx_equal_p (operands[0], operands[1]))
11072         emit_move_insn (operands[0], operands[1]);
11073
11074       split_di (operands, 1, low, high);
11075       emit_insn (gen_x86_shld_1 (high[0], low[0], operands[2]));
11076     }
11077
11078   emit_insn (gen_ashlsi3 (low[0], low[0], operands[2]));
11079
11080   if (TARGET_CMOVE && scratch)
11081     {
11082       ix86_expand_clear (scratch);
11083       emit_insn (gen_x86_shift_adj_1 (high[0], low[0], operands[2], scratch));
11084     }
11085   else
11086     emit_insn (gen_x86_shift_adj_2 (high[0], low[0], operands[2]));
11087 }
11088
11089 void
11090 ix86_split_ashrdi (rtx *operands, rtx scratch)
11091 {
11092   rtx low[2], high[2];
11093   int count;
11094
11095   if (GET_CODE (operands[2]) == CONST_INT)
11096     {
11097       split_di (operands, 2, low, high);
11098       count = INTVAL (operands[2]) & 63;
11099
11100       if (count == 63)
11101         {
11102           emit_move_insn (high[0], high[1]);
11103           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
11104           emit_move_insn (low[0], high[0]);
11105
11106         }
11107       else if (count >= 32)
11108         {
11109           emit_move_insn (low[0], high[1]);
11110           emit_move_insn (high[0], low[0]);
11111           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (31)));
11112           if (count > 32)
11113             emit_insn (gen_ashrsi3 (low[0], low[0], GEN_INT (count - 32)));
11114         }
11115       else
11116         {
11117           if (!rtx_equal_p (operands[0], operands[1]))
11118             emit_move_insn (operands[0], operands[1]);
11119           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
11120           emit_insn (gen_ashrsi3 (high[0], high[0], GEN_INT (count)));
11121         }
11122     }
11123   else
11124     {
11125       if (!rtx_equal_p (operands[0], operands[1]))
11126         emit_move_insn (operands[0], operands[1]);
11127
11128       split_di (operands, 1, low, high);
11129
11130       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
11131       emit_insn (gen_ashrsi3 (high[0], high[0], operands[2]));
11132
11133       if (TARGET_CMOVE && scratch)
11134         {
11135           emit_move_insn (scratch, high[0]);
11136           emit_insn (gen_ashrsi3 (scratch, scratch, GEN_INT (31)));
11137           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
11138                                           scratch));
11139         }
11140       else
11141         emit_insn (gen_x86_shift_adj_3 (low[0], high[0], operands[2]));
11142     }
11143 }
11144
11145 void
11146 ix86_split_lshrdi (rtx *operands, rtx scratch)
11147 {
11148   rtx low[2], high[2];
11149   int count;
11150
11151   if (GET_CODE (operands[2]) == CONST_INT)
11152     {
11153       split_di (operands, 2, low, high);
11154       count = INTVAL (operands[2]) & 63;
11155
11156       if (count >= 32)
11157         {
11158           emit_move_insn (low[0], high[1]);
11159           ix86_expand_clear (high[0]);
11160
11161           if (count > 32)
11162             emit_insn (gen_lshrsi3 (low[0], low[0], GEN_INT (count - 32)));
11163         }
11164       else
11165         {
11166           if (!rtx_equal_p (operands[0], operands[1]))
11167             emit_move_insn (operands[0], operands[1]);
11168           emit_insn (gen_x86_shrd_1 (low[0], high[0], GEN_INT (count)));
11169           emit_insn (gen_lshrsi3 (high[0], high[0], GEN_INT (count)));
11170         }
11171     }
11172   else
11173     {
11174       if (!rtx_equal_p (operands[0], operands[1]))
11175         emit_move_insn (operands[0], operands[1]);
11176
11177       split_di (operands, 1, low, high);
11178
11179       emit_insn (gen_x86_shrd_1 (low[0], high[0], operands[2]));
11180       emit_insn (gen_lshrsi3 (high[0], high[0], operands[2]));
11181
11182       /* Heh.  By reversing the arguments, we can reuse this pattern.  */
11183       if (TARGET_CMOVE && scratch)
11184         {
11185           ix86_expand_clear (scratch);
11186           emit_insn (gen_x86_shift_adj_1 (low[0], high[0], operands[2],
11187                                           scratch));
11188         }
11189       else
11190         emit_insn (gen_x86_shift_adj_2 (low[0], high[0], operands[2]));
11191     }
11192 }
11193
11194 /* Helper function for the string operations below.  Dest VARIABLE whether
11195    it is aligned to VALUE bytes.  If true, jump to the label.  */
11196 static rtx
11197 ix86_expand_aligntest (rtx variable, int value)
11198 {
11199   rtx label = gen_label_rtx ();
11200   rtx tmpcount = gen_reg_rtx (GET_MODE (variable));
11201   if (GET_MODE (variable) == DImode)
11202     emit_insn (gen_anddi3 (tmpcount, variable, GEN_INT (value)));
11203   else
11204     emit_insn (gen_andsi3 (tmpcount, variable, GEN_INT (value)));
11205   emit_cmp_and_jump_insns (tmpcount, const0_rtx, EQ, 0, GET_MODE (variable),
11206                            1, label);
11207   return label;
11208 }
11209
11210 /* Adjust COUNTER by the VALUE.  */
11211 static void
11212 ix86_adjust_counter (rtx countreg, HOST_WIDE_INT value)
11213 {
11214   if (GET_MODE (countreg) == DImode)
11215     emit_insn (gen_adddi3 (countreg, countreg, GEN_INT (-value)));
11216   else
11217     emit_insn (gen_addsi3 (countreg, countreg, GEN_INT (-value)));
11218 }
11219
11220 /* Zero extend possibly SImode EXP to Pmode register.  */
11221 rtx
11222 ix86_zero_extend_to_Pmode (rtx exp)
11223 {
11224   rtx r;
11225   if (GET_MODE (exp) == VOIDmode)
11226     return force_reg (Pmode, exp);
11227   if (GET_MODE (exp) == Pmode)
11228     return copy_to_mode_reg (Pmode, exp);
11229   r = gen_reg_rtx (Pmode);
11230   emit_insn (gen_zero_extendsidi2 (r, exp));
11231   return r;
11232 }
11233
11234 /* Expand string move (memcpy) operation.  Use i386 string operations when
11235    profitable.  expand_clrmem contains similar code.  */
11236 int
11237 ix86_expand_movmem (rtx dst, rtx src, rtx count_exp, rtx align_exp)
11238 {
11239   rtx srcreg, destreg, countreg, srcexp, destexp;
11240   enum machine_mode counter_mode;
11241   HOST_WIDE_INT align = 0;
11242   unsigned HOST_WIDE_INT count = 0;
11243
11244   if (GET_CODE (align_exp) == CONST_INT)
11245     align = INTVAL (align_exp);
11246
11247   /* Can't use any of this if the user has appropriated esi or edi.  */
11248   if (global_regs[4] || global_regs[5])
11249     return 0;
11250
11251   /* This simple hack avoids all inlining code and simplifies code below.  */
11252   if (!TARGET_ALIGN_STRINGOPS)
11253     align = 64;
11254
11255   if (GET_CODE (count_exp) == CONST_INT)
11256     {
11257       count = INTVAL (count_exp);
11258       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
11259         return 0;
11260     }
11261
11262   /* Figure out proper mode for counter.  For 32bits it is always SImode,
11263      for 64bits use SImode when possible, otherwise DImode.
11264      Set count to number of bytes copied when known at compile time.  */
11265   if (!TARGET_64BIT
11266       || GET_MODE (count_exp) == SImode
11267       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
11268     counter_mode = SImode;
11269   else
11270     counter_mode = DImode;
11271
11272   gcc_assert (counter_mode == SImode || counter_mode == DImode);
11273
11274   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
11275   if (destreg != XEXP (dst, 0))
11276     dst = replace_equiv_address_nv (dst, destreg);
11277   srcreg = copy_to_mode_reg (Pmode, XEXP (src, 0));
11278   if (srcreg != XEXP (src, 0))
11279     src = replace_equiv_address_nv (src, srcreg);
11280
11281   /* When optimizing for size emit simple rep ; movsb instruction for
11282      counts not divisible by 4.  */
11283
11284   if ((!optimize || optimize_size) && (count == 0 || (count & 0x03)))
11285     {
11286       emit_insn (gen_cld ());
11287       countreg = ix86_zero_extend_to_Pmode (count_exp);
11288       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
11289       srcexp = gen_rtx_PLUS (Pmode, srcreg, countreg);
11290       emit_insn (gen_rep_mov (destreg, dst, srcreg, src, countreg,
11291                               destexp, srcexp));
11292     }
11293
11294   /* For constant aligned (or small unaligned) copies use rep movsl
11295      followed by code copying the rest.  For PentiumPro ensure 8 byte
11296      alignment to allow rep movsl acceleration.  */
11297
11298   else if (count != 0
11299            && (align >= 8
11300                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
11301                || optimize_size || count < (unsigned int) 64))
11302     {
11303       unsigned HOST_WIDE_INT offset = 0;
11304       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
11305       rtx srcmem, dstmem;
11306
11307       emit_insn (gen_cld ());
11308       if (count & ~(size - 1))
11309         {
11310           countreg = copy_to_mode_reg (counter_mode,
11311                                        GEN_INT ((count >> (size == 4 ? 2 : 3))
11312                                                 & (TARGET_64BIT ? -1 : 0x3fffffff)));
11313           countreg = ix86_zero_extend_to_Pmode (countreg);
11314
11315           destexp = gen_rtx_ASHIFT (Pmode, countreg,
11316                                     GEN_INT (size == 4 ? 2 : 3));
11317           srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
11318           destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
11319
11320           emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
11321                                   countreg, destexp, srcexp));
11322           offset = count & ~(size - 1);
11323         }
11324       if (size == 8 && (count & 0x04))
11325         {
11326           srcmem = adjust_automodify_address_nv (src, SImode, srcreg,
11327                                                  offset);
11328           dstmem = adjust_automodify_address_nv (dst, SImode, destreg,
11329                                                  offset);
11330           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11331           offset += 4;
11332         }
11333       if (count & 0x02)
11334         {
11335           srcmem = adjust_automodify_address_nv (src, HImode, srcreg,
11336                                                  offset);
11337           dstmem = adjust_automodify_address_nv (dst, HImode, destreg,
11338                                                  offset);
11339           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11340           offset += 2;
11341         }
11342       if (count & 0x01)
11343         {
11344           srcmem = adjust_automodify_address_nv (src, QImode, srcreg,
11345                                                  offset);
11346           dstmem = adjust_automodify_address_nv (dst, QImode, destreg,
11347                                                  offset);
11348           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11349         }
11350     }
11351   /* The generic code based on the glibc implementation:
11352      - align destination to 4 bytes (8 byte alignment is used for PentiumPro
11353      allowing accelerated copying there)
11354      - copy the data using rep movsl
11355      - copy the rest.  */
11356   else
11357     {
11358       rtx countreg2;
11359       rtx label = NULL;
11360       rtx srcmem, dstmem;
11361       int desired_alignment = (TARGET_PENTIUMPRO
11362                                && (count == 0 || count >= (unsigned int) 260)
11363                                ? 8 : UNITS_PER_WORD);
11364       /* Get rid of MEM_OFFSETs, they won't be accurate.  */
11365       dst = change_address (dst, BLKmode, destreg);
11366       src = change_address (src, BLKmode, srcreg);
11367
11368       /* In case we don't know anything about the alignment, default to
11369          library version, since it is usually equally fast and result in
11370          shorter code.
11371
11372          Also emit call when we know that the count is large and call overhead
11373          will not be important.  */
11374       if (!TARGET_INLINE_ALL_STRINGOPS
11375           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
11376         return 0;
11377
11378       if (TARGET_SINGLE_STRINGOP)
11379         emit_insn (gen_cld ());
11380
11381       countreg2 = gen_reg_rtx (Pmode);
11382       countreg = copy_to_mode_reg (counter_mode, count_exp);
11383
11384       /* We don't use loops to align destination and to copy parts smaller
11385          than 4 bytes, because gcc is able to optimize such code better (in
11386          the case the destination or the count really is aligned, gcc is often
11387          able to predict the branches) and also it is friendlier to the
11388          hardware branch prediction.
11389
11390          Using loops is beneficial for generic case, because we can
11391          handle small counts using the loops.  Many CPUs (such as Athlon)
11392          have large REP prefix setup costs.
11393
11394          This is quite costly.  Maybe we can revisit this decision later or
11395          add some customizability to this code.  */
11396
11397       if (count == 0 && align < desired_alignment)
11398         {
11399           label = gen_label_rtx ();
11400           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
11401                                    LEU, 0, counter_mode, 1, label);
11402         }
11403       if (align <= 1)
11404         {
11405           rtx label = ix86_expand_aligntest (destreg, 1);
11406           srcmem = change_address (src, QImode, srcreg);
11407           dstmem = change_address (dst, QImode, destreg);
11408           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11409           ix86_adjust_counter (countreg, 1);
11410           emit_label (label);
11411           LABEL_NUSES (label) = 1;
11412         }
11413       if (align <= 2)
11414         {
11415           rtx label = ix86_expand_aligntest (destreg, 2);
11416           srcmem = change_address (src, HImode, srcreg);
11417           dstmem = change_address (dst, HImode, destreg);
11418           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11419           ix86_adjust_counter (countreg, 2);
11420           emit_label (label);
11421           LABEL_NUSES (label) = 1;
11422         }
11423       if (align <= 4 && desired_alignment > 4)
11424         {
11425           rtx label = ix86_expand_aligntest (destreg, 4);
11426           srcmem = change_address (src, SImode, srcreg);
11427           dstmem = change_address (dst, SImode, destreg);
11428           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11429           ix86_adjust_counter (countreg, 4);
11430           emit_label (label);
11431           LABEL_NUSES (label) = 1;
11432         }
11433
11434       if (label && desired_alignment > 4 && !TARGET_64BIT)
11435         {
11436           emit_label (label);
11437           LABEL_NUSES (label) = 1;
11438           label = NULL_RTX;
11439         }
11440       if (!TARGET_SINGLE_STRINGOP)
11441         emit_insn (gen_cld ());
11442       if (TARGET_64BIT)
11443         {
11444           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
11445                                   GEN_INT (3)));
11446           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
11447         }
11448       else
11449         {
11450           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
11451           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
11452         }
11453       srcexp = gen_rtx_PLUS (Pmode, destexp, srcreg);
11454       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
11455       emit_insn (gen_rep_mov (destreg, dst, srcreg, src,
11456                               countreg2, destexp, srcexp));
11457
11458       if (label)
11459         {
11460           emit_label (label);
11461           LABEL_NUSES (label) = 1;
11462         }
11463       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
11464         {
11465           srcmem = change_address (src, SImode, srcreg);
11466           dstmem = change_address (dst, SImode, destreg);
11467           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11468         }
11469       if ((align <= 4 || count == 0) && TARGET_64BIT)
11470         {
11471           rtx label = ix86_expand_aligntest (countreg, 4);
11472           srcmem = change_address (src, SImode, srcreg);
11473           dstmem = change_address (dst, SImode, destreg);
11474           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11475           emit_label (label);
11476           LABEL_NUSES (label) = 1;
11477         }
11478       if (align > 2 && count != 0 && (count & 2))
11479         {
11480           srcmem = change_address (src, HImode, srcreg);
11481           dstmem = change_address (dst, HImode, destreg);
11482           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11483         }
11484       if (align <= 2 || count == 0)
11485         {
11486           rtx label = ix86_expand_aligntest (countreg, 2);
11487           srcmem = change_address (src, HImode, srcreg);
11488           dstmem = change_address (dst, HImode, destreg);
11489           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11490           emit_label (label);
11491           LABEL_NUSES (label) = 1;
11492         }
11493       if (align > 1 && count != 0 && (count & 1))
11494         {
11495           srcmem = change_address (src, QImode, srcreg);
11496           dstmem = change_address (dst, QImode, destreg);
11497           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11498         }
11499       if (align <= 1 || count == 0)
11500         {
11501           rtx label = ix86_expand_aligntest (countreg, 1);
11502           srcmem = change_address (src, QImode, srcreg);
11503           dstmem = change_address (dst, QImode, destreg);
11504           emit_insn (gen_strmov (destreg, dstmem, srcreg, srcmem));
11505           emit_label (label);
11506           LABEL_NUSES (label) = 1;
11507         }
11508     }
11509
11510   return 1;
11511 }
11512
11513 /* Expand string clear operation (bzero).  Use i386 string operations when
11514    profitable.  expand_movmem contains similar code.  */
11515 int
11516 ix86_expand_clrmem (rtx dst, rtx count_exp, rtx align_exp)
11517 {
11518   rtx destreg, zeroreg, countreg, destexp;
11519   enum machine_mode counter_mode;
11520   HOST_WIDE_INT align = 0;
11521   unsigned HOST_WIDE_INT count = 0;
11522
11523   if (GET_CODE (align_exp) == CONST_INT)
11524     align = INTVAL (align_exp);
11525
11526   /* Can't use any of this if the user has appropriated esi.  */
11527   if (global_regs[4])
11528     return 0;
11529
11530   /* This simple hack avoids all inlining code and simplifies code below.  */
11531   if (!TARGET_ALIGN_STRINGOPS)
11532     align = 32;
11533
11534   if (GET_CODE (count_exp) == CONST_INT)
11535     {
11536       count = INTVAL (count_exp);
11537       if (!TARGET_INLINE_ALL_STRINGOPS && count > 64)
11538         return 0;
11539     }
11540   /* Figure out proper mode for counter.  For 32bits it is always SImode,
11541      for 64bits use SImode when possible, otherwise DImode.
11542      Set count to number of bytes copied when known at compile time.  */
11543   if (!TARGET_64BIT
11544       || GET_MODE (count_exp) == SImode
11545       || x86_64_zext_immediate_operand (count_exp, VOIDmode))
11546     counter_mode = SImode;
11547   else
11548     counter_mode = DImode;
11549
11550   destreg = copy_to_mode_reg (Pmode, XEXP (dst, 0));
11551   if (destreg != XEXP (dst, 0))
11552     dst = replace_equiv_address_nv (dst, destreg);
11553
11554
11555   /* When optimizing for size emit simple rep ; movsb instruction for
11556      counts not divisible by 4.  The movl $N, %ecx; rep; stosb
11557      sequence is 7 bytes long, so if optimizing for size and count is
11558      small enough that some stosl, stosw and stosb instructions without
11559      rep are shorter, fall back into the next if.  */
11560
11561   if ((!optimize || optimize_size)
11562       && (count == 0
11563           || ((count & 0x03)
11564               && (!optimize_size || (count & 0x03) + (count >> 2) > 7))))
11565     {
11566       emit_insn (gen_cld ());
11567
11568       countreg = ix86_zero_extend_to_Pmode (count_exp);
11569       zeroreg = copy_to_mode_reg (QImode, const0_rtx);
11570       destexp = gen_rtx_PLUS (Pmode, destreg, countreg);
11571       emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg, destexp));
11572     }
11573   else if (count != 0
11574            && (align >= 8
11575                || (!TARGET_PENTIUMPRO && !TARGET_64BIT && align >= 4)
11576                || optimize_size || count < (unsigned int) 64))
11577     {
11578       int size = TARGET_64BIT && !optimize_size ? 8 : 4;
11579       unsigned HOST_WIDE_INT offset = 0;
11580
11581       emit_insn (gen_cld ());
11582
11583       zeroreg = copy_to_mode_reg (size == 4 ? SImode : DImode, const0_rtx);
11584       if (count & ~(size - 1))
11585         {
11586           unsigned HOST_WIDE_INT repcount;
11587           unsigned int max_nonrep;
11588
11589           repcount = count >> (size == 4 ? 2 : 3);
11590           if (!TARGET_64BIT)
11591             repcount &= 0x3fffffff;
11592
11593           /* movl $N, %ecx; rep; stosl is 7 bytes, while N x stosl is N bytes.
11594              movl $N, %ecx; rep; stosq is 8 bytes, while N x stosq is 2xN
11595              bytes.  In both cases the latter seems to be faster for small
11596              values of N.  */
11597           max_nonrep = size == 4 ? 7 : 4;
11598           if (!optimize_size)
11599             switch (ix86_tune)
11600               {
11601               case PROCESSOR_PENTIUM4:
11602               case PROCESSOR_NOCONA:
11603                 max_nonrep = 3;
11604                 break;
11605               default:
11606                 break;
11607               }
11608
11609           if (repcount <= max_nonrep)
11610             while (repcount-- > 0)
11611               {
11612                 rtx mem = adjust_automodify_address_nv (dst,
11613                                                         GET_MODE (zeroreg),
11614                                                         destreg, offset);
11615                 emit_insn (gen_strset (destreg, mem, zeroreg));
11616                 offset += size;
11617               }
11618           else
11619             {
11620               countreg = copy_to_mode_reg (counter_mode, GEN_INT (repcount));
11621               countreg = ix86_zero_extend_to_Pmode (countreg);
11622               destexp = gen_rtx_ASHIFT (Pmode, countreg,
11623                                         GEN_INT (size == 4 ? 2 : 3));
11624               destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
11625               emit_insn (gen_rep_stos (destreg, countreg, dst, zeroreg,
11626                                        destexp));
11627               offset = count & ~(size - 1);
11628             }
11629         }
11630       if (size == 8 && (count & 0x04))
11631         {
11632           rtx mem = adjust_automodify_address_nv (dst, SImode, destreg,
11633                                                   offset);
11634           emit_insn (gen_strset (destreg, mem,
11635                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
11636           offset += 4;
11637         }
11638       if (count & 0x02)
11639         {
11640           rtx mem = adjust_automodify_address_nv (dst, HImode, destreg,
11641                                                   offset);
11642           emit_insn (gen_strset (destreg, mem,
11643                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11644           offset += 2;
11645         }
11646       if (count & 0x01)
11647         {
11648           rtx mem = adjust_automodify_address_nv (dst, QImode, destreg,
11649                                                   offset);
11650           emit_insn (gen_strset (destreg, mem,
11651                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11652         }
11653     }
11654   else
11655     {
11656       rtx countreg2;
11657       rtx label = NULL;
11658       /* Compute desired alignment of the string operation.  */
11659       int desired_alignment = (TARGET_PENTIUMPRO
11660                                && (count == 0 || count >= (unsigned int) 260)
11661                                ? 8 : UNITS_PER_WORD);
11662
11663       /* In case we don't know anything about the alignment, default to
11664          library version, since it is usually equally fast and result in
11665          shorter code.
11666
11667          Also emit call when we know that the count is large and call overhead
11668          will not be important.  */
11669       if (!TARGET_INLINE_ALL_STRINGOPS
11670           && (align < UNITS_PER_WORD || !TARGET_REP_MOVL_OPTIMAL))
11671         return 0;
11672
11673       if (TARGET_SINGLE_STRINGOP)
11674         emit_insn (gen_cld ());
11675
11676       countreg2 = gen_reg_rtx (Pmode);
11677       countreg = copy_to_mode_reg (counter_mode, count_exp);
11678       zeroreg = copy_to_mode_reg (Pmode, const0_rtx);
11679       /* Get rid of MEM_OFFSET, it won't be accurate.  */
11680       dst = change_address (dst, BLKmode, destreg);
11681
11682       if (count == 0 && align < desired_alignment)
11683         {
11684           label = gen_label_rtx ();
11685           emit_cmp_and_jump_insns (countreg, GEN_INT (desired_alignment - 1),
11686                                    LEU, 0, counter_mode, 1, label);
11687         }
11688       if (align <= 1)
11689         {
11690           rtx label = ix86_expand_aligntest (destreg, 1);
11691           emit_insn (gen_strset (destreg, dst,
11692                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11693           ix86_adjust_counter (countreg, 1);
11694           emit_label (label);
11695           LABEL_NUSES (label) = 1;
11696         }
11697       if (align <= 2)
11698         {
11699           rtx label = ix86_expand_aligntest (destreg, 2);
11700           emit_insn (gen_strset (destreg, dst,
11701                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11702           ix86_adjust_counter (countreg, 2);
11703           emit_label (label);
11704           LABEL_NUSES (label) = 1;
11705         }
11706       if (align <= 4 && desired_alignment > 4)
11707         {
11708           rtx label = ix86_expand_aligntest (destreg, 4);
11709           emit_insn (gen_strset (destreg, dst,
11710                                  (TARGET_64BIT
11711                                   ? gen_rtx_SUBREG (SImode, zeroreg, 0)
11712                                   : zeroreg)));
11713           ix86_adjust_counter (countreg, 4);
11714           emit_label (label);
11715           LABEL_NUSES (label) = 1;
11716         }
11717
11718       if (label && desired_alignment > 4 && !TARGET_64BIT)
11719         {
11720           emit_label (label);
11721           LABEL_NUSES (label) = 1;
11722           label = NULL_RTX;
11723         }
11724
11725       if (!TARGET_SINGLE_STRINGOP)
11726         emit_insn (gen_cld ());
11727       if (TARGET_64BIT)
11728         {
11729           emit_insn (gen_lshrdi3 (countreg2, ix86_zero_extend_to_Pmode (countreg),
11730                                   GEN_INT (3)));
11731           destexp = gen_rtx_ASHIFT (Pmode, countreg2, GEN_INT (3));
11732         }
11733       else
11734         {
11735           emit_insn (gen_lshrsi3 (countreg2, countreg, const2_rtx));
11736           destexp = gen_rtx_ASHIFT (Pmode, countreg2, const2_rtx);
11737         }
11738       destexp = gen_rtx_PLUS (Pmode, destexp, destreg);
11739       emit_insn (gen_rep_stos (destreg, countreg2, dst, zeroreg, destexp));
11740
11741       if (label)
11742         {
11743           emit_label (label);
11744           LABEL_NUSES (label) = 1;
11745         }
11746
11747       if (TARGET_64BIT && align > 4 && count != 0 && (count & 4))
11748         emit_insn (gen_strset (destreg, dst,
11749                                gen_rtx_SUBREG (SImode, zeroreg, 0)));
11750       if (TARGET_64BIT && (align <= 4 || count == 0))
11751         {
11752           rtx label = ix86_expand_aligntest (countreg, 4);
11753           emit_insn (gen_strset (destreg, dst,
11754                                  gen_rtx_SUBREG (SImode, zeroreg, 0)));
11755           emit_label (label);
11756           LABEL_NUSES (label) = 1;
11757         }
11758       if (align > 2 && count != 0 && (count & 2))
11759         emit_insn (gen_strset (destreg, dst,
11760                                gen_rtx_SUBREG (HImode, zeroreg, 0)));
11761       if (align <= 2 || count == 0)
11762         {
11763           rtx label = ix86_expand_aligntest (countreg, 2);
11764           emit_insn (gen_strset (destreg, dst,
11765                                  gen_rtx_SUBREG (HImode, zeroreg, 0)));
11766           emit_label (label);
11767           LABEL_NUSES (label) = 1;
11768         }
11769       if (align > 1 && count != 0 && (count & 1))
11770         emit_insn (gen_strset (destreg, dst,
11771                                gen_rtx_SUBREG (QImode, zeroreg, 0)));
11772       if (align <= 1 || count == 0)
11773         {
11774           rtx label = ix86_expand_aligntest (countreg, 1);
11775           emit_insn (gen_strset (destreg, dst,
11776                                  gen_rtx_SUBREG (QImode, zeroreg, 0)));
11777           emit_label (label);
11778           LABEL_NUSES (label) = 1;
11779         }
11780     }
11781   return 1;
11782 }
11783
11784 /* Expand strlen.  */
11785 int
11786 ix86_expand_strlen (rtx out, rtx src, rtx eoschar, rtx align)
11787 {
11788   rtx addr, scratch1, scratch2, scratch3, scratch4;
11789
11790   /* The generic case of strlen expander is long.  Avoid it's
11791      expanding unless TARGET_INLINE_ALL_STRINGOPS.  */
11792
11793   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11794       && !TARGET_INLINE_ALL_STRINGOPS
11795       && !optimize_size
11796       && (GET_CODE (align) != CONST_INT || INTVAL (align) < 4))
11797     return 0;
11798
11799   addr = force_reg (Pmode, XEXP (src, 0));
11800   scratch1 = gen_reg_rtx (Pmode);
11801
11802   if (TARGET_UNROLL_STRLEN && eoschar == const0_rtx && optimize > 1
11803       && !optimize_size)
11804     {
11805       /* Well it seems that some optimizer does not combine a call like
11806          foo(strlen(bar), strlen(bar));
11807          when the move and the subtraction is done here.  It does calculate
11808          the length just once when these instructions are done inside of
11809          output_strlen_unroll().  But I think since &bar[strlen(bar)] is
11810          often used and I use one fewer register for the lifetime of
11811          output_strlen_unroll() this is better.  */
11812
11813       emit_move_insn (out, addr);
11814
11815       ix86_expand_strlensi_unroll_1 (out, src, align);
11816
11817       /* strlensi_unroll_1 returns the address of the zero at the end of
11818          the string, like memchr(), so compute the length by subtracting
11819          the start address.  */
11820       if (TARGET_64BIT)
11821         emit_insn (gen_subdi3 (out, out, addr));
11822       else
11823         emit_insn (gen_subsi3 (out, out, addr));
11824     }
11825   else
11826     {
11827       rtx unspec;
11828       scratch2 = gen_reg_rtx (Pmode);
11829       scratch3 = gen_reg_rtx (Pmode);
11830       scratch4 = force_reg (Pmode, constm1_rtx);
11831
11832       emit_move_insn (scratch3, addr);
11833       eoschar = force_reg (QImode, eoschar);
11834
11835       emit_insn (gen_cld ());
11836       src = replace_equiv_address_nv (src, scratch3);
11837
11838       /* If .md starts supporting :P, this can be done in .md.  */
11839       unspec = gen_rtx_UNSPEC (Pmode, gen_rtvec (4, src, eoschar, align,
11840                                                  scratch4), UNSPEC_SCAS);
11841       emit_insn (gen_strlenqi_1 (scratch1, scratch3, unspec));
11842       if (TARGET_64BIT)
11843         {
11844           emit_insn (gen_one_cmpldi2 (scratch2, scratch1));
11845           emit_insn (gen_adddi3 (out, scratch2, constm1_rtx));
11846         }
11847       else
11848         {
11849           emit_insn (gen_one_cmplsi2 (scratch2, scratch1));
11850           emit_insn (gen_addsi3 (out, scratch2, constm1_rtx));
11851         }
11852     }
11853   return 1;
11854 }
11855
11856 /* Expand the appropriate insns for doing strlen if not just doing
11857    repnz; scasb
11858
11859    out = result, initialized with the start address
11860    align_rtx = alignment of the address.
11861    scratch = scratch register, initialized with the startaddress when
11862         not aligned, otherwise undefined
11863
11864    This is just the body. It needs the initializations mentioned above and
11865    some address computing at the end.  These things are done in i386.md.  */
11866
11867 static void
11868 ix86_expand_strlensi_unroll_1 (rtx out, rtx src, rtx align_rtx)
11869 {
11870   int align;
11871   rtx tmp;
11872   rtx align_2_label = NULL_RTX;
11873   rtx align_3_label = NULL_RTX;
11874   rtx align_4_label = gen_label_rtx ();
11875   rtx end_0_label = gen_label_rtx ();
11876   rtx mem;
11877   rtx tmpreg = gen_reg_rtx (SImode);
11878   rtx scratch = gen_reg_rtx (SImode);
11879   rtx cmp;
11880
11881   align = 0;
11882   if (GET_CODE (align_rtx) == CONST_INT)
11883     align = INTVAL (align_rtx);
11884
11885   /* Loop to check 1..3 bytes for null to get an aligned pointer.  */
11886
11887   /* Is there a known alignment and is it less than 4?  */
11888   if (align < 4)
11889     {
11890       rtx scratch1 = gen_reg_rtx (Pmode);
11891       emit_move_insn (scratch1, out);
11892       /* Is there a known alignment and is it not 2? */
11893       if (align != 2)
11894         {
11895           align_3_label = gen_label_rtx (); /* Label when aligned to 3-byte */
11896           align_2_label = gen_label_rtx (); /* Label when aligned to 2-byte */
11897
11898           /* Leave just the 3 lower bits.  */
11899           align_rtx = expand_binop (Pmode, and_optab, scratch1, GEN_INT (3),
11900                                     NULL_RTX, 0, OPTAB_WIDEN);
11901
11902           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11903                                    Pmode, 1, align_4_label);
11904           emit_cmp_and_jump_insns (align_rtx, const2_rtx, EQ, NULL,
11905                                    Pmode, 1, align_2_label);
11906           emit_cmp_and_jump_insns (align_rtx, const2_rtx, GTU, NULL,
11907                                    Pmode, 1, align_3_label);
11908         }
11909       else
11910         {
11911           /* Since the alignment is 2, we have to check 2 or 0 bytes;
11912              check if is aligned to 4 - byte.  */
11913
11914           align_rtx = expand_binop (Pmode, and_optab, scratch1, const2_rtx,
11915                                     NULL_RTX, 0, OPTAB_WIDEN);
11916
11917           emit_cmp_and_jump_insns (align_rtx, const0_rtx, EQ, NULL,
11918                                    Pmode, 1, align_4_label);
11919         }
11920
11921       mem = change_address (src, QImode, out);
11922
11923       /* Now compare the bytes.  */
11924
11925       /* Compare the first n unaligned byte on a byte per byte basis.  */
11926       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL,
11927                                QImode, 1, end_0_label);
11928
11929       /* Increment the address.  */
11930       if (TARGET_64BIT)
11931         emit_insn (gen_adddi3 (out, out, const1_rtx));
11932       else
11933         emit_insn (gen_addsi3 (out, out, const1_rtx));
11934
11935       /* Not needed with an alignment of 2 */
11936       if (align != 2)
11937         {
11938           emit_label (align_2_label);
11939
11940           emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11941                                    end_0_label);
11942
11943           if (TARGET_64BIT)
11944             emit_insn (gen_adddi3 (out, out, const1_rtx));
11945           else
11946             emit_insn (gen_addsi3 (out, out, const1_rtx));
11947
11948           emit_label (align_3_label);
11949         }
11950
11951       emit_cmp_and_jump_insns (mem, const0_rtx, EQ, NULL, QImode, 1,
11952                                end_0_label);
11953
11954       if (TARGET_64BIT)
11955         emit_insn (gen_adddi3 (out, out, const1_rtx));
11956       else
11957         emit_insn (gen_addsi3 (out, out, const1_rtx));
11958     }
11959
11960   /* Generate loop to check 4 bytes at a time.  It is not a good idea to
11961      align this loop.  It gives only huge programs, but does not help to
11962      speed up.  */
11963   emit_label (align_4_label);
11964
11965   mem = change_address (src, SImode, out);
11966   emit_move_insn (scratch, mem);
11967   if (TARGET_64BIT)
11968     emit_insn (gen_adddi3 (out, out, GEN_INT (4)));
11969   else
11970     emit_insn (gen_addsi3 (out, out, GEN_INT (4)));
11971
11972   /* This formula yields a nonzero result iff one of the bytes is zero.
11973      This saves three branches inside loop and many cycles.  */
11974
11975   emit_insn (gen_addsi3 (tmpreg, scratch, GEN_INT (-0x01010101)));
11976   emit_insn (gen_one_cmplsi2 (scratch, scratch));
11977   emit_insn (gen_andsi3 (tmpreg, tmpreg, scratch));
11978   emit_insn (gen_andsi3 (tmpreg, tmpreg,
11979                          gen_int_mode (0x80808080, SImode)));
11980   emit_cmp_and_jump_insns (tmpreg, const0_rtx, EQ, 0, SImode, 1,
11981                            align_4_label);
11982
11983   if (TARGET_CMOVE)
11984     {
11985        rtx reg = gen_reg_rtx (SImode);
11986        rtx reg2 = gen_reg_rtx (Pmode);
11987        emit_move_insn (reg, tmpreg);
11988        emit_insn (gen_lshrsi3 (reg, reg, GEN_INT (16)));
11989
11990        /* If zero is not in the first two bytes, move two bytes forward.  */
11991        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
11992        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
11993        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
11994        emit_insn (gen_rtx_SET (VOIDmode, tmpreg,
11995                                gen_rtx_IF_THEN_ELSE (SImode, tmp,
11996                                                      reg,
11997                                                      tmpreg)));
11998        /* Emit lea manually to avoid clobbering of flags.  */
11999        emit_insn (gen_rtx_SET (SImode, reg2,
12000                                gen_rtx_PLUS (Pmode, out, const2_rtx)));
12001
12002        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
12003        tmp = gen_rtx_EQ (VOIDmode, tmp, const0_rtx);
12004        emit_insn (gen_rtx_SET (VOIDmode, out,
12005                                gen_rtx_IF_THEN_ELSE (Pmode, tmp,
12006                                                      reg2,
12007                                                      out)));
12008
12009     }
12010   else
12011     {
12012        rtx end_2_label = gen_label_rtx ();
12013        /* Is zero in the first two bytes? */
12014
12015        emit_insn (gen_testsi_ccno_1 (tmpreg, GEN_INT (0x8080)));
12016        tmp = gen_rtx_REG (CCNOmode, FLAGS_REG);
12017        tmp = gen_rtx_NE (VOIDmode, tmp, const0_rtx);
12018        tmp = gen_rtx_IF_THEN_ELSE (VOIDmode, tmp,
12019                             gen_rtx_LABEL_REF (VOIDmode, end_2_label),
12020                             pc_rtx);
12021        tmp = emit_jump_insn (gen_rtx_SET (VOIDmode, pc_rtx, tmp));
12022        JUMP_LABEL (tmp) = end_2_label;
12023
12024        /* Not in the first two.  Move two bytes forward.  */
12025        emit_insn (gen_lshrsi3 (tmpreg, tmpreg, GEN_INT (16)));
12026        if (TARGET_64BIT)
12027          emit_insn (gen_adddi3 (out, out, const2_rtx));
12028        else
12029          emit_insn (gen_addsi3 (out, out, const2_rtx));
12030
12031        emit_label (end_2_label);
12032
12033     }
12034
12035   /* Avoid branch in fixing the byte.  */
12036   tmpreg = gen_lowpart (QImode, tmpreg);
12037   emit_insn (gen_addqi3_cc (tmpreg, tmpreg, tmpreg));
12038   cmp = gen_rtx_LTU (Pmode, gen_rtx_REG (CCmode, 17), const0_rtx);
12039   if (TARGET_64BIT)
12040     emit_insn (gen_subdi3_carry_rex64 (out, out, GEN_INT (3), cmp));
12041   else
12042     emit_insn (gen_subsi3_carry (out, out, GEN_INT (3), cmp));
12043
12044   emit_label (end_0_label);
12045 }
12046
12047 void
12048 ix86_expand_call (rtx retval, rtx fnaddr, rtx callarg1,
12049                   rtx callarg2 ATTRIBUTE_UNUSED,
12050                   rtx pop, int sibcall)
12051 {
12052   rtx use = NULL, call;
12053
12054   if (pop == const0_rtx)
12055     pop = NULL;
12056   gcc_assert (!TARGET_64BIT || !pop);
12057
12058 #if TARGET_MACHO
12059   if (flag_pic && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF)
12060     fnaddr = machopic_indirect_call_target (fnaddr);
12061 #else
12062   /* Static functions and indirect calls don't need the pic register.  */
12063   if (! TARGET_64BIT && flag_pic
12064       && GET_CODE (XEXP (fnaddr, 0)) == SYMBOL_REF
12065       && ! SYMBOL_REF_LOCAL_P (XEXP (fnaddr, 0)))
12066     use_reg (&use, pic_offset_table_rtx);
12067
12068   if (TARGET_64BIT && INTVAL (callarg2) >= 0)
12069     {
12070       rtx al = gen_rtx_REG (QImode, 0);
12071       emit_move_insn (al, callarg2);
12072       use_reg (&use, al);
12073     }
12074 #endif /* TARGET_MACHO */
12075
12076   if (! call_insn_operand (XEXP (fnaddr, 0), Pmode))
12077     {
12078       fnaddr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
12079       fnaddr = gen_rtx_MEM (QImode, fnaddr);
12080     }
12081   if (sibcall && TARGET_64BIT
12082       && !constant_call_address_operand (XEXP (fnaddr, 0), Pmode))
12083     {
12084       rtx addr;
12085       addr = copy_to_mode_reg (Pmode, XEXP (fnaddr, 0));
12086       fnaddr = gen_rtx_REG (Pmode, FIRST_REX_INT_REG + 3 /* R11 */);
12087       emit_move_insn (fnaddr, addr);
12088       fnaddr = gen_rtx_MEM (QImode, fnaddr);
12089     }
12090
12091   call = gen_rtx_CALL (VOIDmode, fnaddr, callarg1);
12092   if (retval)
12093     call = gen_rtx_SET (VOIDmode, retval, call);
12094   if (pop)
12095     {
12096       pop = gen_rtx_PLUS (Pmode, stack_pointer_rtx, pop);
12097       pop = gen_rtx_SET (VOIDmode, stack_pointer_rtx, pop);
12098       call = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, call, pop));
12099     }
12100
12101   call = emit_call_insn (call);
12102   if (use)
12103     CALL_INSN_FUNCTION_USAGE (call) = use;
12104 }
12105
12106 \f
12107 /* Clear stack slot assignments remembered from previous functions.
12108    This is called from INIT_EXPANDERS once before RTL is emitted for each
12109    function.  */
12110
12111 static struct machine_function *
12112 ix86_init_machine_status (void)
12113 {
12114   struct machine_function *f;
12115
12116   f = ggc_alloc_cleared (sizeof (struct machine_function));
12117   f->use_fast_prologue_epilogue_nregs = -1;
12118
12119   return f;
12120 }
12121
12122 /* Return a MEM corresponding to a stack slot with mode MODE.
12123    Allocate a new slot if necessary.
12124
12125    The RTL for a function can have several slots available: N is
12126    which slot to use.  */
12127
12128 rtx
12129 assign_386_stack_local (enum machine_mode mode, int n)
12130 {
12131   struct stack_local_entry *s;
12132
12133   gcc_assert (n >= 0 && n < MAX_386_STACK_LOCALS);
12134
12135   for (s = ix86_stack_locals; s; s = s->next)
12136     if (s->mode == mode && s->n == n)
12137       return s->rtl;
12138
12139   s = (struct stack_local_entry *)
12140     ggc_alloc (sizeof (struct stack_local_entry));
12141   s->n = n;
12142   s->mode = mode;
12143   s->rtl = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
12144
12145   s->next = ix86_stack_locals;
12146   ix86_stack_locals = s;
12147   return s->rtl;
12148 }
12149
12150 /* Construct the SYMBOL_REF for the tls_get_addr function.  */
12151
12152 static GTY(()) rtx ix86_tls_symbol;
12153 rtx
12154 ix86_tls_get_addr (void)
12155 {
12156
12157   if (!ix86_tls_symbol)
12158     {
12159       ix86_tls_symbol = gen_rtx_SYMBOL_REF (Pmode,
12160                                             (TARGET_GNU_TLS && !TARGET_64BIT)
12161                                             ? "___tls_get_addr"
12162                                             : "__tls_get_addr");
12163     }
12164
12165   return ix86_tls_symbol;
12166 }
12167 \f
12168 /* Calculate the length of the memory address in the instruction
12169    encoding.  Does not include the one-byte modrm, opcode, or prefix.  */
12170
12171 int
12172 memory_address_length (rtx addr)
12173 {
12174   struct ix86_address parts;
12175   rtx base, index, disp;
12176   int len;
12177   int ok;
12178
12179   if (GET_CODE (addr) == PRE_DEC
12180       || GET_CODE (addr) == POST_INC
12181       || GET_CODE (addr) == PRE_MODIFY
12182       || GET_CODE (addr) == POST_MODIFY)
12183     return 0;
12184
12185   ok = ix86_decompose_address (addr, &parts);
12186   gcc_assert (ok);
12187
12188   if (parts.base && GET_CODE (parts.base) == SUBREG)
12189     parts.base = SUBREG_REG (parts.base);
12190   if (parts.index && GET_CODE (parts.index) == SUBREG)
12191     parts.index = SUBREG_REG (parts.index);
12192
12193   base = parts.base;
12194   index = parts.index;
12195   disp = parts.disp;
12196   len = 0;
12197
12198   /* Rule of thumb:
12199        - esp as the base always wants an index,
12200        - ebp as the base always wants a displacement.  */
12201
12202   /* Register Indirect.  */
12203   if (base && !index && !disp)
12204     {
12205       /* esp (for its index) and ebp (for its displacement) need
12206          the two-byte modrm form.  */
12207       if (addr == stack_pointer_rtx
12208           || addr == arg_pointer_rtx
12209           || addr == frame_pointer_rtx
12210           || addr == hard_frame_pointer_rtx)
12211         len = 1;
12212     }
12213
12214   /* Direct Addressing.  */
12215   else if (disp && !base && !index)
12216     len = 4;
12217
12218   else
12219     {
12220       /* Find the length of the displacement constant.  */
12221       if (disp)
12222         {
12223           if (GET_CODE (disp) == CONST_INT
12224               && CONST_OK_FOR_LETTER_P (INTVAL (disp), 'K')
12225               && base)
12226             len = 1;
12227           else
12228             len = 4;
12229         }
12230       /* ebp always wants a displacement.  */
12231       else if (base == hard_frame_pointer_rtx)
12232         len = 1;
12233
12234       /* An index requires the two-byte modrm form....  */
12235       if (index
12236           /* ...like esp, which always wants an index.  */
12237           || base == stack_pointer_rtx
12238           || base == arg_pointer_rtx
12239           || base == frame_pointer_rtx)
12240         len += 1;
12241     }
12242
12243   return len;
12244 }
12245
12246 /* Compute default value for "length_immediate" attribute.  When SHORTFORM
12247    is set, expect that insn have 8bit immediate alternative.  */
12248 int
12249 ix86_attr_length_immediate_default (rtx insn, int shortform)
12250 {
12251   int len = 0;
12252   int i;
12253   extract_insn_cached (insn);
12254   for (i = recog_data.n_operands - 1; i >= 0; --i)
12255     if (CONSTANT_P (recog_data.operand[i]))
12256       {
12257         gcc_assert (!len);
12258         if (shortform
12259             && GET_CODE (recog_data.operand[i]) == CONST_INT
12260             && CONST_OK_FOR_LETTER_P (INTVAL (recog_data.operand[i]), 'K'))
12261           len = 1;
12262         else
12263           {
12264             switch (get_attr_mode (insn))
12265               {
12266                 case MODE_QI:
12267                   len+=1;
12268                   break;
12269                 case MODE_HI:
12270                   len+=2;
12271                   break;
12272                 case MODE_SI:
12273                   len+=4;
12274                   break;
12275                 /* Immediates for DImode instructions are encoded as 32bit sign extended values.  */
12276                 case MODE_DI:
12277                   len+=4;
12278                   break;
12279                 default:
12280                   fatal_insn ("unknown insn mode", insn);
12281               }
12282           }
12283       }
12284   return len;
12285 }
12286 /* Compute default value for "length_address" attribute.  */
12287 int
12288 ix86_attr_length_address_default (rtx insn)
12289 {
12290   int i;
12291
12292   if (get_attr_type (insn) == TYPE_LEA)
12293     {
12294       rtx set = PATTERN (insn);
12295
12296       if (GET_CODE (set) == PARALLEL)
12297         set = XVECEXP (set, 0, 0);
12298
12299       gcc_assert (GET_CODE (set) == SET);
12300
12301       return memory_address_length (SET_SRC (set));
12302     }
12303
12304   extract_insn_cached (insn);
12305   for (i = recog_data.n_operands - 1; i >= 0; --i)
12306     if (GET_CODE (recog_data.operand[i]) == MEM)
12307       {
12308         return memory_address_length (XEXP (recog_data.operand[i], 0));
12309         break;
12310       }
12311   return 0;
12312 }
12313 \f
12314 /* Return the maximum number of instructions a cpu can issue.  */
12315
12316 static int
12317 ix86_issue_rate (void)
12318 {
12319   switch (ix86_tune)
12320     {
12321     case PROCESSOR_PENTIUM:
12322     case PROCESSOR_K6:
12323       return 2;
12324
12325     case PROCESSOR_PENTIUMPRO:
12326     case PROCESSOR_PENTIUM4:
12327     case PROCESSOR_ATHLON:
12328     case PROCESSOR_K8:
12329     case PROCESSOR_NOCONA:
12330       return 3;
12331
12332     default:
12333       return 1;
12334     }
12335 }
12336
12337 /* A subroutine of ix86_adjust_cost -- return true iff INSN reads flags set
12338    by DEP_INSN and nothing set by DEP_INSN.  */
12339
12340 static int
12341 ix86_flags_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
12342 {
12343   rtx set, set2;
12344
12345   /* Simplify the test for uninteresting insns.  */
12346   if (insn_type != TYPE_SETCC
12347       && insn_type != TYPE_ICMOV
12348       && insn_type != TYPE_FCMOV
12349       && insn_type != TYPE_IBR)
12350     return 0;
12351
12352   if ((set = single_set (dep_insn)) != 0)
12353     {
12354       set = SET_DEST (set);
12355       set2 = NULL_RTX;
12356     }
12357   else if (GET_CODE (PATTERN (dep_insn)) == PARALLEL
12358            && XVECLEN (PATTERN (dep_insn), 0) == 2
12359            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 0)) == SET
12360            && GET_CODE (XVECEXP (PATTERN (dep_insn), 0, 1)) == SET)
12361     {
12362       set = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
12363       set2 = SET_DEST (XVECEXP (PATTERN (dep_insn), 0, 0));
12364     }
12365   else
12366     return 0;
12367
12368   if (GET_CODE (set) != REG || REGNO (set) != FLAGS_REG)
12369     return 0;
12370
12371   /* This test is true if the dependent insn reads the flags but
12372      not any other potentially set register.  */
12373   if (!reg_overlap_mentioned_p (set, PATTERN (insn)))
12374     return 0;
12375
12376   if (set2 && reg_overlap_mentioned_p (set2, PATTERN (insn)))
12377     return 0;
12378
12379   return 1;
12380 }
12381
12382 /* A subroutine of ix86_adjust_cost -- return true iff INSN has a memory
12383    address with operands set by DEP_INSN.  */
12384
12385 static int
12386 ix86_agi_dependant (rtx insn, rtx dep_insn, enum attr_type insn_type)
12387 {
12388   rtx addr;
12389
12390   if (insn_type == TYPE_LEA
12391       && TARGET_PENTIUM)
12392     {
12393       addr = PATTERN (insn);
12394
12395       if (GET_CODE (addr) == PARALLEL)
12396         addr = XVECEXP (addr, 0, 0);
12397       
12398       gcc_assert (GET_CODE (addr) == SET);
12399       
12400       addr = SET_SRC (addr);
12401     }
12402   else
12403     {
12404       int i;
12405       extract_insn_cached (insn);
12406       for (i = recog_data.n_operands - 1; i >= 0; --i)
12407         if (GET_CODE (recog_data.operand[i]) == MEM)
12408           {
12409             addr = XEXP (recog_data.operand[i], 0);
12410             goto found;
12411           }
12412       return 0;
12413     found:;
12414     }
12415
12416   return modified_in_p (addr, dep_insn);
12417 }
12418
12419 static int
12420 ix86_adjust_cost (rtx insn, rtx link, rtx dep_insn, int cost)
12421 {
12422   enum attr_type insn_type, dep_insn_type;
12423   enum attr_memory memory;
12424   rtx set, set2;
12425   int dep_insn_code_number;
12426
12427   /* Anti and output dependencies have zero cost on all CPUs.  */
12428   if (REG_NOTE_KIND (link) != 0)
12429     return 0;
12430
12431   dep_insn_code_number = recog_memoized (dep_insn);
12432
12433   /* If we can't recognize the insns, we can't really do anything.  */
12434   if (dep_insn_code_number < 0 || recog_memoized (insn) < 0)
12435     return cost;
12436
12437   insn_type = get_attr_type (insn);
12438   dep_insn_type = get_attr_type (dep_insn);
12439
12440   switch (ix86_tune)
12441     {
12442     case PROCESSOR_PENTIUM:
12443       /* Address Generation Interlock adds a cycle of latency.  */
12444       if (ix86_agi_dependant (insn, dep_insn, insn_type))
12445         cost += 1;
12446
12447       /* ??? Compares pair with jump/setcc.  */
12448       if (ix86_flags_dependant (insn, dep_insn, insn_type))
12449         cost = 0;
12450
12451       /* Floating point stores require value to be ready one cycle earlier.  */
12452       if (insn_type == TYPE_FMOV
12453           && get_attr_memory (insn) == MEMORY_STORE
12454           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12455         cost += 1;
12456       break;
12457
12458     case PROCESSOR_PENTIUMPRO:
12459       memory = get_attr_memory (insn);
12460
12461       /* INT->FP conversion is expensive.  */
12462       if (get_attr_fp_int_src (dep_insn))
12463         cost += 5;
12464
12465       /* There is one cycle extra latency between an FP op and a store.  */
12466       if (insn_type == TYPE_FMOV
12467           && (set = single_set (dep_insn)) != NULL_RTX
12468           && (set2 = single_set (insn)) != NULL_RTX
12469           && rtx_equal_p (SET_DEST (set), SET_SRC (set2))
12470           && GET_CODE (SET_DEST (set2)) == MEM)
12471         cost += 1;
12472
12473       /* Show ability of reorder buffer to hide latency of load by executing
12474          in parallel with previous instruction in case
12475          previous instruction is not needed to compute the address.  */
12476       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12477           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12478         {
12479           /* Claim moves to take one cycle, as core can issue one load
12480              at time and the next load can start cycle later.  */
12481           if (dep_insn_type == TYPE_IMOV
12482               || dep_insn_type == TYPE_FMOV)
12483             cost = 1;
12484           else if (cost > 1)
12485             cost--;
12486         }
12487       break;
12488
12489     case PROCESSOR_K6:
12490       memory = get_attr_memory (insn);
12491
12492       /* The esp dependency is resolved before the instruction is really
12493          finished.  */
12494       if ((insn_type == TYPE_PUSH || insn_type == TYPE_POP)
12495           && (dep_insn_type == TYPE_PUSH || dep_insn_type == TYPE_POP))
12496         return 1;
12497
12498       /* INT->FP conversion is expensive.  */
12499       if (get_attr_fp_int_src (dep_insn))
12500         cost += 5;
12501
12502       /* Show ability of reorder buffer to hide latency of load by executing
12503          in parallel with previous instruction in case
12504          previous instruction is not needed to compute the address.  */
12505       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12506           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12507         {
12508           /* Claim moves to take one cycle, as core can issue one load
12509              at time and the next load can start cycle later.  */
12510           if (dep_insn_type == TYPE_IMOV
12511               || dep_insn_type == TYPE_FMOV)
12512             cost = 1;
12513           else if (cost > 2)
12514             cost -= 2;
12515           else
12516             cost = 1;
12517         }
12518       break;
12519
12520     case PROCESSOR_ATHLON:
12521     case PROCESSOR_K8:
12522       memory = get_attr_memory (insn);
12523
12524       /* Show ability of reorder buffer to hide latency of load by executing
12525          in parallel with previous instruction in case
12526          previous instruction is not needed to compute the address.  */
12527       if ((memory == MEMORY_LOAD || memory == MEMORY_BOTH)
12528           && !ix86_agi_dependant (insn, dep_insn, insn_type))
12529         {
12530           enum attr_unit unit = get_attr_unit (insn);
12531           int loadcost = 3;
12532
12533           /* Because of the difference between the length of integer and
12534              floating unit pipeline preparation stages, the memory operands
12535              for floating point are cheaper.
12536
12537              ??? For Athlon it the difference is most probably 2.  */
12538           if (unit == UNIT_INTEGER || unit == UNIT_UNKNOWN)
12539             loadcost = 3;
12540           else
12541             loadcost = TARGET_ATHLON ? 2 : 0;
12542
12543           if (cost >= loadcost)
12544             cost -= loadcost;
12545           else
12546             cost = 0;
12547         }
12548
12549     default:
12550       break;
12551     }
12552
12553   return cost;
12554 }
12555
12556 /* How many alternative schedules to try.  This should be as wide as the
12557    scheduling freedom in the DFA, but no wider.  Making this value too
12558    large results extra work for the scheduler.  */
12559
12560 static int
12561 ia32_multipass_dfa_lookahead (void)
12562 {
12563   if (ix86_tune == PROCESSOR_PENTIUM)
12564     return 2;
12565
12566   if (ix86_tune == PROCESSOR_PENTIUMPRO
12567       || ix86_tune == PROCESSOR_K6)
12568     return 1;
12569
12570   else
12571     return 0;
12572 }
12573
12574 \f
12575 /* Compute the alignment given to a constant that is being placed in memory.
12576    EXP is the constant and ALIGN is the alignment that the object would
12577    ordinarily have.
12578    The value of this function is used instead of that alignment to align
12579    the object.  */
12580
12581 int
12582 ix86_constant_alignment (tree exp, int align)
12583 {
12584   if (TREE_CODE (exp) == REAL_CST)
12585     {
12586       if (TYPE_MODE (TREE_TYPE (exp)) == DFmode && align < 64)
12587         return 64;
12588       else if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (exp))) && align < 128)
12589         return 128;
12590     }
12591   else if (!optimize_size && TREE_CODE (exp) == STRING_CST
12592            && TREE_STRING_LENGTH (exp) >= 31 && align < BITS_PER_WORD)
12593     return BITS_PER_WORD;
12594
12595   return align;
12596 }
12597
12598 /* Compute the alignment for a static variable.
12599    TYPE is the data type, and ALIGN is the alignment that
12600    the object would ordinarily have.  The value of this function is used
12601    instead of that alignment to align the object.  */
12602
12603 int
12604 ix86_data_alignment (tree type, int align)
12605 {
12606   if (AGGREGATE_TYPE_P (type)
12607        && TYPE_SIZE (type)
12608        && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12609        && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 256
12610            || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 256)
12611     return 256;
12612
12613   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12614      to 16byte boundary.  */
12615   if (TARGET_64BIT)
12616     {
12617       if (AGGREGATE_TYPE_P (type)
12618            && TYPE_SIZE (type)
12619            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12620            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 128
12621                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12622         return 128;
12623     }
12624
12625   if (TREE_CODE (type) == ARRAY_TYPE)
12626     {
12627       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12628         return 64;
12629       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12630         return 128;
12631     }
12632   else if (TREE_CODE (type) == COMPLEX_TYPE)
12633     {
12634
12635       if (TYPE_MODE (type) == DCmode && align < 64)
12636         return 64;
12637       if (TYPE_MODE (type) == XCmode && align < 128)
12638         return 128;
12639     }
12640   else if ((TREE_CODE (type) == RECORD_TYPE
12641             || TREE_CODE (type) == UNION_TYPE
12642             || TREE_CODE (type) == QUAL_UNION_TYPE)
12643            && TYPE_FIELDS (type))
12644     {
12645       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12646         return 64;
12647       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12648         return 128;
12649     }
12650   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12651            || TREE_CODE (type) == INTEGER_TYPE)
12652     {
12653       if (TYPE_MODE (type) == DFmode && align < 64)
12654         return 64;
12655       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12656         return 128;
12657     }
12658
12659   return align;
12660 }
12661
12662 /* Compute the alignment for a local variable.
12663    TYPE is the data type, and ALIGN is the alignment that
12664    the object would ordinarily have.  The value of this macro is used
12665    instead of that alignment to align the object.  */
12666
12667 int
12668 ix86_local_alignment (tree type, int align)
12669 {
12670   /* x86-64 ABI requires arrays greater than 16 bytes to be aligned
12671      to 16byte boundary.  */
12672   if (TARGET_64BIT)
12673     {
12674       if (AGGREGATE_TYPE_P (type)
12675            && TYPE_SIZE (type)
12676            && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
12677            && (TREE_INT_CST_LOW (TYPE_SIZE (type)) >= 16
12678                || TREE_INT_CST_HIGH (TYPE_SIZE (type))) && align < 128)
12679         return 128;
12680     }
12681   if (TREE_CODE (type) == ARRAY_TYPE)
12682     {
12683       if (TYPE_MODE (TREE_TYPE (type)) == DFmode && align < 64)
12684         return 64;
12685       if (ALIGN_MODE_128 (TYPE_MODE (TREE_TYPE (type))) && align < 128)
12686         return 128;
12687     }
12688   else if (TREE_CODE (type) == COMPLEX_TYPE)
12689     {
12690       if (TYPE_MODE (type) == DCmode && align < 64)
12691         return 64;
12692       if (TYPE_MODE (type) == XCmode && align < 128)
12693         return 128;
12694     }
12695   else if ((TREE_CODE (type) == RECORD_TYPE
12696             || TREE_CODE (type) == UNION_TYPE
12697             || TREE_CODE (type) == QUAL_UNION_TYPE)
12698            && TYPE_FIELDS (type))
12699     {
12700       if (DECL_MODE (TYPE_FIELDS (type)) == DFmode && align < 64)
12701         return 64;
12702       if (ALIGN_MODE_128 (DECL_MODE (TYPE_FIELDS (type))) && align < 128)
12703         return 128;
12704     }
12705   else if (TREE_CODE (type) == REAL_TYPE || TREE_CODE (type) == VECTOR_TYPE
12706            || TREE_CODE (type) == INTEGER_TYPE)
12707     {
12708
12709       if (TYPE_MODE (type) == DFmode && align < 64)
12710         return 64;
12711       if (ALIGN_MODE_128 (TYPE_MODE (type)) && align < 128)
12712         return 128;
12713     }
12714   return align;
12715 }
12716 \f
12717 /* Emit RTL insns to initialize the variable parts of a trampoline.
12718    FNADDR is an RTX for the address of the function's pure code.
12719    CXT is an RTX for the static chain value for the function.  */
12720 void
12721 x86_initialize_trampoline (rtx tramp, rtx fnaddr, rtx cxt)
12722 {
12723   if (!TARGET_64BIT)
12724     {
12725       /* Compute offset from the end of the jmp to the target function.  */
12726       rtx disp = expand_binop (SImode, sub_optab, fnaddr,
12727                                plus_constant (tramp, 10),
12728                                NULL_RTX, 1, OPTAB_DIRECT);
12729       emit_move_insn (gen_rtx_MEM (QImode, tramp),
12730                       gen_int_mode (0xb9, QImode));
12731       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 1)), cxt);
12732       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, 5)),
12733                       gen_int_mode (0xe9, QImode));
12734       emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, 6)), disp);
12735     }
12736   else
12737     {
12738       int offset = 0;
12739       /* Try to load address using shorter movl instead of movabs.
12740          We may want to support movq for kernel mode, but kernel does not use
12741          trampolines at the moment.  */
12742       if (x86_64_zext_immediate_operand (fnaddr, VOIDmode))
12743         {
12744           fnaddr = copy_to_mode_reg (DImode, fnaddr);
12745           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12746                           gen_int_mode (0xbb41, HImode));
12747           emit_move_insn (gen_rtx_MEM (SImode, plus_constant (tramp, offset + 2)),
12748                           gen_lowpart (SImode, fnaddr));
12749           offset += 6;
12750         }
12751       else
12752         {
12753           emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12754                           gen_int_mode (0xbb49, HImode));
12755           emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12756                           fnaddr);
12757           offset += 10;
12758         }
12759       /* Load static chain using movabs to r10.  */
12760       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12761                       gen_int_mode (0xba49, HImode));
12762       emit_move_insn (gen_rtx_MEM (DImode, plus_constant (tramp, offset + 2)),
12763                       cxt);
12764       offset += 10;
12765       /* Jump to the r11 */
12766       emit_move_insn (gen_rtx_MEM (HImode, plus_constant (tramp, offset)),
12767                       gen_int_mode (0xff49, HImode));
12768       emit_move_insn (gen_rtx_MEM (QImode, plus_constant (tramp, offset+2)),
12769                       gen_int_mode (0xe3, QImode));
12770       offset += 3;
12771       gcc_assert (offset <= TRAMPOLINE_SIZE);
12772     }
12773
12774 #ifdef ENABLE_EXECUTE_STACK
12775   emit_library_call (gen_rtx_SYMBOL_REF (Pmode, "__enable_execute_stack"),
12776                      LCT_NORMAL, VOIDmode, 1, tramp, Pmode);
12777 #endif
12778 }
12779 \f
12780 /* Codes for all the SSE/MMX builtins.  */
12781 enum ix86_builtins
12782 {
12783   IX86_BUILTIN_ADDPS,
12784   IX86_BUILTIN_ADDSS,
12785   IX86_BUILTIN_DIVPS,
12786   IX86_BUILTIN_DIVSS,
12787   IX86_BUILTIN_MULPS,
12788   IX86_BUILTIN_MULSS,
12789   IX86_BUILTIN_SUBPS,
12790   IX86_BUILTIN_SUBSS,
12791
12792   IX86_BUILTIN_CMPEQPS,
12793   IX86_BUILTIN_CMPLTPS,
12794   IX86_BUILTIN_CMPLEPS,
12795   IX86_BUILTIN_CMPGTPS,
12796   IX86_BUILTIN_CMPGEPS,
12797   IX86_BUILTIN_CMPNEQPS,
12798   IX86_BUILTIN_CMPNLTPS,
12799   IX86_BUILTIN_CMPNLEPS,
12800   IX86_BUILTIN_CMPNGTPS,
12801   IX86_BUILTIN_CMPNGEPS,
12802   IX86_BUILTIN_CMPORDPS,
12803   IX86_BUILTIN_CMPUNORDPS,
12804   IX86_BUILTIN_CMPNEPS,
12805   IX86_BUILTIN_CMPEQSS,
12806   IX86_BUILTIN_CMPLTSS,
12807   IX86_BUILTIN_CMPLESS,
12808   IX86_BUILTIN_CMPNEQSS,
12809   IX86_BUILTIN_CMPNLTSS,
12810   IX86_BUILTIN_CMPNLESS,
12811   IX86_BUILTIN_CMPNGTSS,
12812   IX86_BUILTIN_CMPNGESS,
12813   IX86_BUILTIN_CMPORDSS,
12814   IX86_BUILTIN_CMPUNORDSS,
12815   IX86_BUILTIN_CMPNESS,
12816
12817   IX86_BUILTIN_COMIEQSS,
12818   IX86_BUILTIN_COMILTSS,
12819   IX86_BUILTIN_COMILESS,
12820   IX86_BUILTIN_COMIGTSS,
12821   IX86_BUILTIN_COMIGESS,
12822   IX86_BUILTIN_COMINEQSS,
12823   IX86_BUILTIN_UCOMIEQSS,
12824   IX86_BUILTIN_UCOMILTSS,
12825   IX86_BUILTIN_UCOMILESS,
12826   IX86_BUILTIN_UCOMIGTSS,
12827   IX86_BUILTIN_UCOMIGESS,
12828   IX86_BUILTIN_UCOMINEQSS,
12829
12830   IX86_BUILTIN_CVTPI2PS,
12831   IX86_BUILTIN_CVTPS2PI,
12832   IX86_BUILTIN_CVTSI2SS,
12833   IX86_BUILTIN_CVTSI642SS,
12834   IX86_BUILTIN_CVTSS2SI,
12835   IX86_BUILTIN_CVTSS2SI64,
12836   IX86_BUILTIN_CVTTPS2PI,
12837   IX86_BUILTIN_CVTTSS2SI,
12838   IX86_BUILTIN_CVTTSS2SI64,
12839
12840   IX86_BUILTIN_MAXPS,
12841   IX86_BUILTIN_MAXSS,
12842   IX86_BUILTIN_MINPS,
12843   IX86_BUILTIN_MINSS,
12844
12845   IX86_BUILTIN_LOADUPS,
12846   IX86_BUILTIN_STOREUPS,
12847   IX86_BUILTIN_MOVSS,
12848
12849   IX86_BUILTIN_MOVHLPS,
12850   IX86_BUILTIN_MOVLHPS,
12851   IX86_BUILTIN_LOADHPS,
12852   IX86_BUILTIN_LOADLPS,
12853   IX86_BUILTIN_STOREHPS,
12854   IX86_BUILTIN_STORELPS,
12855
12856   IX86_BUILTIN_MASKMOVQ,
12857   IX86_BUILTIN_MOVMSKPS,
12858   IX86_BUILTIN_PMOVMSKB,
12859
12860   IX86_BUILTIN_MOVNTPS,
12861   IX86_BUILTIN_MOVNTQ,
12862
12863   IX86_BUILTIN_LOADDQU,
12864   IX86_BUILTIN_STOREDQU,
12865
12866   IX86_BUILTIN_PACKSSWB,
12867   IX86_BUILTIN_PACKSSDW,
12868   IX86_BUILTIN_PACKUSWB,
12869
12870   IX86_BUILTIN_PADDB,
12871   IX86_BUILTIN_PADDW,
12872   IX86_BUILTIN_PADDD,
12873   IX86_BUILTIN_PADDQ,
12874   IX86_BUILTIN_PADDSB,
12875   IX86_BUILTIN_PADDSW,
12876   IX86_BUILTIN_PADDUSB,
12877   IX86_BUILTIN_PADDUSW,
12878   IX86_BUILTIN_PSUBB,
12879   IX86_BUILTIN_PSUBW,
12880   IX86_BUILTIN_PSUBD,
12881   IX86_BUILTIN_PSUBQ,
12882   IX86_BUILTIN_PSUBSB,
12883   IX86_BUILTIN_PSUBSW,
12884   IX86_BUILTIN_PSUBUSB,
12885   IX86_BUILTIN_PSUBUSW,
12886
12887   IX86_BUILTIN_PAND,
12888   IX86_BUILTIN_PANDN,
12889   IX86_BUILTIN_POR,
12890   IX86_BUILTIN_PXOR,
12891
12892   IX86_BUILTIN_PAVGB,
12893   IX86_BUILTIN_PAVGW,
12894
12895   IX86_BUILTIN_PCMPEQB,
12896   IX86_BUILTIN_PCMPEQW,
12897   IX86_BUILTIN_PCMPEQD,
12898   IX86_BUILTIN_PCMPGTB,
12899   IX86_BUILTIN_PCMPGTW,
12900   IX86_BUILTIN_PCMPGTD,
12901
12902   IX86_BUILTIN_PMADDWD,
12903
12904   IX86_BUILTIN_PMAXSW,
12905   IX86_BUILTIN_PMAXUB,
12906   IX86_BUILTIN_PMINSW,
12907   IX86_BUILTIN_PMINUB,
12908
12909   IX86_BUILTIN_PMULHUW,
12910   IX86_BUILTIN_PMULHW,
12911   IX86_BUILTIN_PMULLW,
12912
12913   IX86_BUILTIN_PSADBW,
12914   IX86_BUILTIN_PSHUFW,
12915
12916   IX86_BUILTIN_PSLLW,
12917   IX86_BUILTIN_PSLLD,
12918   IX86_BUILTIN_PSLLQ,
12919   IX86_BUILTIN_PSRAW,
12920   IX86_BUILTIN_PSRAD,
12921   IX86_BUILTIN_PSRLW,
12922   IX86_BUILTIN_PSRLD,
12923   IX86_BUILTIN_PSRLQ,
12924   IX86_BUILTIN_PSLLWI,
12925   IX86_BUILTIN_PSLLDI,
12926   IX86_BUILTIN_PSLLQI,
12927   IX86_BUILTIN_PSRAWI,
12928   IX86_BUILTIN_PSRADI,
12929   IX86_BUILTIN_PSRLWI,
12930   IX86_BUILTIN_PSRLDI,
12931   IX86_BUILTIN_PSRLQI,
12932
12933   IX86_BUILTIN_PUNPCKHBW,
12934   IX86_BUILTIN_PUNPCKHWD,
12935   IX86_BUILTIN_PUNPCKHDQ,
12936   IX86_BUILTIN_PUNPCKLBW,
12937   IX86_BUILTIN_PUNPCKLWD,
12938   IX86_BUILTIN_PUNPCKLDQ,
12939
12940   IX86_BUILTIN_SHUFPS,
12941
12942   IX86_BUILTIN_RCPPS,
12943   IX86_BUILTIN_RCPSS,
12944   IX86_BUILTIN_RSQRTPS,
12945   IX86_BUILTIN_RSQRTSS,
12946   IX86_BUILTIN_SQRTPS,
12947   IX86_BUILTIN_SQRTSS,
12948
12949   IX86_BUILTIN_UNPCKHPS,
12950   IX86_BUILTIN_UNPCKLPS,
12951
12952   IX86_BUILTIN_ANDPS,
12953   IX86_BUILTIN_ANDNPS,
12954   IX86_BUILTIN_ORPS,
12955   IX86_BUILTIN_XORPS,
12956
12957   IX86_BUILTIN_EMMS,
12958   IX86_BUILTIN_LDMXCSR,
12959   IX86_BUILTIN_STMXCSR,
12960   IX86_BUILTIN_SFENCE,
12961
12962   /* 3DNow! Original */
12963   IX86_BUILTIN_FEMMS,
12964   IX86_BUILTIN_PAVGUSB,
12965   IX86_BUILTIN_PF2ID,
12966   IX86_BUILTIN_PFACC,
12967   IX86_BUILTIN_PFADD,
12968   IX86_BUILTIN_PFCMPEQ,
12969   IX86_BUILTIN_PFCMPGE,
12970   IX86_BUILTIN_PFCMPGT,
12971   IX86_BUILTIN_PFMAX,
12972   IX86_BUILTIN_PFMIN,
12973   IX86_BUILTIN_PFMUL,
12974   IX86_BUILTIN_PFRCP,
12975   IX86_BUILTIN_PFRCPIT1,
12976   IX86_BUILTIN_PFRCPIT2,
12977   IX86_BUILTIN_PFRSQIT1,
12978   IX86_BUILTIN_PFRSQRT,
12979   IX86_BUILTIN_PFSUB,
12980   IX86_BUILTIN_PFSUBR,
12981   IX86_BUILTIN_PI2FD,
12982   IX86_BUILTIN_PMULHRW,
12983
12984   /* 3DNow! Athlon Extensions */
12985   IX86_BUILTIN_PF2IW,
12986   IX86_BUILTIN_PFNACC,
12987   IX86_BUILTIN_PFPNACC,
12988   IX86_BUILTIN_PI2FW,
12989   IX86_BUILTIN_PSWAPDSI,
12990   IX86_BUILTIN_PSWAPDSF,
12991
12992   /* SSE2 */
12993   IX86_BUILTIN_ADDPD,
12994   IX86_BUILTIN_ADDSD,
12995   IX86_BUILTIN_DIVPD,
12996   IX86_BUILTIN_DIVSD,
12997   IX86_BUILTIN_MULPD,
12998   IX86_BUILTIN_MULSD,
12999   IX86_BUILTIN_SUBPD,
13000   IX86_BUILTIN_SUBSD,
13001
13002   IX86_BUILTIN_CMPEQPD,
13003   IX86_BUILTIN_CMPLTPD,
13004   IX86_BUILTIN_CMPLEPD,
13005   IX86_BUILTIN_CMPGTPD,
13006   IX86_BUILTIN_CMPGEPD,
13007   IX86_BUILTIN_CMPNEQPD,
13008   IX86_BUILTIN_CMPNLTPD,
13009   IX86_BUILTIN_CMPNLEPD,
13010   IX86_BUILTIN_CMPNGTPD,
13011   IX86_BUILTIN_CMPNGEPD,
13012   IX86_BUILTIN_CMPORDPD,
13013   IX86_BUILTIN_CMPUNORDPD,
13014   IX86_BUILTIN_CMPNEPD,
13015   IX86_BUILTIN_CMPEQSD,
13016   IX86_BUILTIN_CMPLTSD,
13017   IX86_BUILTIN_CMPLESD,
13018   IX86_BUILTIN_CMPNEQSD,
13019   IX86_BUILTIN_CMPNLTSD,
13020   IX86_BUILTIN_CMPNLESD,
13021   IX86_BUILTIN_CMPORDSD,
13022   IX86_BUILTIN_CMPUNORDSD,
13023   IX86_BUILTIN_CMPNESD,
13024
13025   IX86_BUILTIN_COMIEQSD,
13026   IX86_BUILTIN_COMILTSD,
13027   IX86_BUILTIN_COMILESD,
13028   IX86_BUILTIN_COMIGTSD,
13029   IX86_BUILTIN_COMIGESD,
13030   IX86_BUILTIN_COMINEQSD,
13031   IX86_BUILTIN_UCOMIEQSD,
13032   IX86_BUILTIN_UCOMILTSD,
13033   IX86_BUILTIN_UCOMILESD,
13034   IX86_BUILTIN_UCOMIGTSD,
13035   IX86_BUILTIN_UCOMIGESD,
13036   IX86_BUILTIN_UCOMINEQSD,
13037
13038   IX86_BUILTIN_MAXPD,
13039   IX86_BUILTIN_MAXSD,
13040   IX86_BUILTIN_MINPD,
13041   IX86_BUILTIN_MINSD,
13042
13043   IX86_BUILTIN_ANDPD,
13044   IX86_BUILTIN_ANDNPD,
13045   IX86_BUILTIN_ORPD,
13046   IX86_BUILTIN_XORPD,
13047
13048   IX86_BUILTIN_SQRTPD,
13049   IX86_BUILTIN_SQRTSD,
13050
13051   IX86_BUILTIN_UNPCKHPD,
13052   IX86_BUILTIN_UNPCKLPD,
13053
13054   IX86_BUILTIN_SHUFPD,
13055
13056   IX86_BUILTIN_LOADUPD,
13057   IX86_BUILTIN_STOREUPD,
13058   IX86_BUILTIN_MOVSD,
13059
13060   IX86_BUILTIN_LOADHPD,
13061   IX86_BUILTIN_LOADLPD,
13062
13063   IX86_BUILTIN_CVTDQ2PD,
13064   IX86_BUILTIN_CVTDQ2PS,
13065
13066   IX86_BUILTIN_CVTPD2DQ,
13067   IX86_BUILTIN_CVTPD2PI,
13068   IX86_BUILTIN_CVTPD2PS,
13069   IX86_BUILTIN_CVTTPD2DQ,
13070   IX86_BUILTIN_CVTTPD2PI,
13071
13072   IX86_BUILTIN_CVTPI2PD,
13073   IX86_BUILTIN_CVTSI2SD,
13074   IX86_BUILTIN_CVTSI642SD,
13075
13076   IX86_BUILTIN_CVTSD2SI,
13077   IX86_BUILTIN_CVTSD2SI64,
13078   IX86_BUILTIN_CVTSD2SS,
13079   IX86_BUILTIN_CVTSS2SD,
13080   IX86_BUILTIN_CVTTSD2SI,
13081   IX86_BUILTIN_CVTTSD2SI64,
13082
13083   IX86_BUILTIN_CVTPS2DQ,
13084   IX86_BUILTIN_CVTPS2PD,
13085   IX86_BUILTIN_CVTTPS2DQ,
13086
13087   IX86_BUILTIN_MOVNTI,
13088   IX86_BUILTIN_MOVNTPD,
13089   IX86_BUILTIN_MOVNTDQ,
13090
13091   /* SSE2 MMX */
13092   IX86_BUILTIN_MASKMOVDQU,
13093   IX86_BUILTIN_MOVMSKPD,
13094   IX86_BUILTIN_PMOVMSKB128,
13095
13096   IX86_BUILTIN_PACKSSWB128,
13097   IX86_BUILTIN_PACKSSDW128,
13098   IX86_BUILTIN_PACKUSWB128,
13099
13100   IX86_BUILTIN_PADDB128,
13101   IX86_BUILTIN_PADDW128,
13102   IX86_BUILTIN_PADDD128,
13103   IX86_BUILTIN_PADDQ128,
13104   IX86_BUILTIN_PADDSB128,
13105   IX86_BUILTIN_PADDSW128,
13106   IX86_BUILTIN_PADDUSB128,
13107   IX86_BUILTIN_PADDUSW128,
13108   IX86_BUILTIN_PSUBB128,
13109   IX86_BUILTIN_PSUBW128,
13110   IX86_BUILTIN_PSUBD128,
13111   IX86_BUILTIN_PSUBQ128,
13112   IX86_BUILTIN_PSUBSB128,
13113   IX86_BUILTIN_PSUBSW128,
13114   IX86_BUILTIN_PSUBUSB128,
13115   IX86_BUILTIN_PSUBUSW128,
13116
13117   IX86_BUILTIN_PAND128,
13118   IX86_BUILTIN_PANDN128,
13119   IX86_BUILTIN_POR128,
13120   IX86_BUILTIN_PXOR128,
13121
13122   IX86_BUILTIN_PAVGB128,
13123   IX86_BUILTIN_PAVGW128,
13124
13125   IX86_BUILTIN_PCMPEQB128,
13126   IX86_BUILTIN_PCMPEQW128,
13127   IX86_BUILTIN_PCMPEQD128,
13128   IX86_BUILTIN_PCMPGTB128,
13129   IX86_BUILTIN_PCMPGTW128,
13130   IX86_BUILTIN_PCMPGTD128,
13131
13132   IX86_BUILTIN_PMADDWD128,
13133
13134   IX86_BUILTIN_PMAXSW128,
13135   IX86_BUILTIN_PMAXUB128,
13136   IX86_BUILTIN_PMINSW128,
13137   IX86_BUILTIN_PMINUB128,
13138
13139   IX86_BUILTIN_PMULUDQ,
13140   IX86_BUILTIN_PMULUDQ128,
13141   IX86_BUILTIN_PMULHUW128,
13142   IX86_BUILTIN_PMULHW128,
13143   IX86_BUILTIN_PMULLW128,
13144
13145   IX86_BUILTIN_PSADBW128,
13146   IX86_BUILTIN_PSHUFHW,
13147   IX86_BUILTIN_PSHUFLW,
13148   IX86_BUILTIN_PSHUFD,
13149
13150   IX86_BUILTIN_PSLLW128,
13151   IX86_BUILTIN_PSLLD128,
13152   IX86_BUILTIN_PSLLQ128,
13153   IX86_BUILTIN_PSRAW128,
13154   IX86_BUILTIN_PSRAD128,
13155   IX86_BUILTIN_PSRLW128,
13156   IX86_BUILTIN_PSRLD128,
13157   IX86_BUILTIN_PSRLQ128,
13158   IX86_BUILTIN_PSLLDQI128,
13159   IX86_BUILTIN_PSLLWI128,
13160   IX86_BUILTIN_PSLLDI128,
13161   IX86_BUILTIN_PSLLQI128,
13162   IX86_BUILTIN_PSRAWI128,
13163   IX86_BUILTIN_PSRADI128,
13164   IX86_BUILTIN_PSRLDQI128,
13165   IX86_BUILTIN_PSRLWI128,
13166   IX86_BUILTIN_PSRLDI128,
13167   IX86_BUILTIN_PSRLQI128,
13168
13169   IX86_BUILTIN_PUNPCKHBW128,
13170   IX86_BUILTIN_PUNPCKHWD128,
13171   IX86_BUILTIN_PUNPCKHDQ128,
13172   IX86_BUILTIN_PUNPCKHQDQ128,
13173   IX86_BUILTIN_PUNPCKLBW128,
13174   IX86_BUILTIN_PUNPCKLWD128,
13175   IX86_BUILTIN_PUNPCKLDQ128,
13176   IX86_BUILTIN_PUNPCKLQDQ128,
13177
13178   IX86_BUILTIN_CLFLUSH,
13179   IX86_BUILTIN_MFENCE,
13180   IX86_BUILTIN_LFENCE,
13181
13182   /* Prescott New Instructions.  */
13183   IX86_BUILTIN_ADDSUBPS,
13184   IX86_BUILTIN_HADDPS,
13185   IX86_BUILTIN_HSUBPS,
13186   IX86_BUILTIN_MOVSHDUP,
13187   IX86_BUILTIN_MOVSLDUP,
13188   IX86_BUILTIN_ADDSUBPD,
13189   IX86_BUILTIN_HADDPD,
13190   IX86_BUILTIN_HSUBPD,
13191   IX86_BUILTIN_LDDQU,
13192
13193   IX86_BUILTIN_MONITOR,
13194   IX86_BUILTIN_MWAIT,
13195
13196   IX86_BUILTIN_VEC_INIT_V2SI,
13197   IX86_BUILTIN_VEC_INIT_V4HI,
13198   IX86_BUILTIN_VEC_INIT_V8QI,
13199   IX86_BUILTIN_VEC_EXT_V2DF,
13200   IX86_BUILTIN_VEC_EXT_V2DI,
13201   IX86_BUILTIN_VEC_EXT_V4SF,
13202   IX86_BUILTIN_VEC_EXT_V4SI,
13203   IX86_BUILTIN_VEC_EXT_V8HI,
13204   IX86_BUILTIN_VEC_EXT_V2SI,
13205   IX86_BUILTIN_VEC_EXT_V4HI,
13206   IX86_BUILTIN_VEC_SET_V8HI,
13207   IX86_BUILTIN_VEC_SET_V4HI,
13208
13209   IX86_BUILTIN_MAX
13210 };
13211
13212 #define def_builtin(MASK, NAME, TYPE, CODE)                             \
13213 do {                                                                    \
13214   if ((MASK) & target_flags                                             \
13215       && (!((MASK) & MASK_64BIT) || TARGET_64BIT))                      \
13216     lang_hooks.builtin_function ((NAME), (TYPE), (CODE), BUILT_IN_MD,   \
13217                                  NULL, NULL_TREE);                      \
13218 } while (0)
13219
13220 /* Bits for builtin_description.flag.  */
13221
13222 /* Set when we don't support the comparison natively, and should
13223    swap_comparison in order to support it.  */
13224 #define BUILTIN_DESC_SWAP_OPERANDS      1
13225
13226 struct builtin_description
13227 {
13228   const unsigned int mask;
13229   const enum insn_code icode;
13230   const char *const name;
13231   const enum ix86_builtins code;
13232   const enum rtx_code comparison;
13233   const unsigned int flag;
13234 };
13235
13236 static const struct builtin_description bdesc_comi[] =
13237 {
13238   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comieq", IX86_BUILTIN_COMIEQSS, UNEQ, 0 },
13239   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comilt", IX86_BUILTIN_COMILTSS, UNLT, 0 },
13240   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comile", IX86_BUILTIN_COMILESS, UNLE, 0 },
13241   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comigt", IX86_BUILTIN_COMIGTSS, GT, 0 },
13242   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comige", IX86_BUILTIN_COMIGESS, GE, 0 },
13243   { MASK_SSE, CODE_FOR_sse_comi, "__builtin_ia32_comineq", IX86_BUILTIN_COMINEQSS, LTGT, 0 },
13244   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomieq", IX86_BUILTIN_UCOMIEQSS, UNEQ, 0 },
13245   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomilt", IX86_BUILTIN_UCOMILTSS, UNLT, 0 },
13246   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomile", IX86_BUILTIN_UCOMILESS, UNLE, 0 },
13247   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomigt", IX86_BUILTIN_UCOMIGTSS, GT, 0 },
13248   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomige", IX86_BUILTIN_UCOMIGESS, GE, 0 },
13249   { MASK_SSE, CODE_FOR_sse_ucomi, "__builtin_ia32_ucomineq", IX86_BUILTIN_UCOMINEQSS, LTGT, 0 },
13250   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdeq", IX86_BUILTIN_COMIEQSD, UNEQ, 0 },
13251   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdlt", IX86_BUILTIN_COMILTSD, UNLT, 0 },
13252   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdle", IX86_BUILTIN_COMILESD, UNLE, 0 },
13253   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdgt", IX86_BUILTIN_COMIGTSD, GT, 0 },
13254   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdge", IX86_BUILTIN_COMIGESD, GE, 0 },
13255   { MASK_SSE2, CODE_FOR_sse2_comi, "__builtin_ia32_comisdneq", IX86_BUILTIN_COMINEQSD, LTGT, 0 },
13256   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdeq", IX86_BUILTIN_UCOMIEQSD, UNEQ, 0 },
13257   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdlt", IX86_BUILTIN_UCOMILTSD, UNLT, 0 },
13258   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdle", IX86_BUILTIN_UCOMILESD, UNLE, 0 },
13259   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdgt", IX86_BUILTIN_UCOMIGTSD, GT, 0 },
13260   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdge", IX86_BUILTIN_UCOMIGESD, GE, 0 },
13261   { MASK_SSE2, CODE_FOR_sse2_ucomi, "__builtin_ia32_ucomisdneq", IX86_BUILTIN_UCOMINEQSD, LTGT, 0 },
13262 };
13263
13264 static const struct builtin_description bdesc_2arg[] =
13265 {
13266   /* SSE */
13267   { MASK_SSE, CODE_FOR_addv4sf3, "__builtin_ia32_addps", IX86_BUILTIN_ADDPS, 0, 0 },
13268   { MASK_SSE, CODE_FOR_subv4sf3, "__builtin_ia32_subps", IX86_BUILTIN_SUBPS, 0, 0 },
13269   { MASK_SSE, CODE_FOR_mulv4sf3, "__builtin_ia32_mulps", IX86_BUILTIN_MULPS, 0, 0 },
13270   { MASK_SSE, CODE_FOR_divv4sf3, "__builtin_ia32_divps", IX86_BUILTIN_DIVPS, 0, 0 },
13271   { MASK_SSE, CODE_FOR_sse_vmaddv4sf3,  "__builtin_ia32_addss", IX86_BUILTIN_ADDSS, 0, 0 },
13272   { MASK_SSE, CODE_FOR_sse_vmsubv4sf3,  "__builtin_ia32_subss", IX86_BUILTIN_SUBSS, 0, 0 },
13273   { MASK_SSE, CODE_FOR_sse_vmmulv4sf3,  "__builtin_ia32_mulss", IX86_BUILTIN_MULSS, 0, 0 },
13274   { MASK_SSE, CODE_FOR_sse_vmdivv4sf3,  "__builtin_ia32_divss", IX86_BUILTIN_DIVSS, 0, 0 },
13275
13276   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpeqps", IX86_BUILTIN_CMPEQPS, EQ, 0 },
13277   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpltps", IX86_BUILTIN_CMPLTPS, LT, 0 },
13278   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpleps", IX86_BUILTIN_CMPLEPS, LE, 0 },
13279   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgtps", IX86_BUILTIN_CMPGTPS, LT,
13280     BUILTIN_DESC_SWAP_OPERANDS },
13281   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpgeps", IX86_BUILTIN_CMPGEPS, LE,
13282     BUILTIN_DESC_SWAP_OPERANDS },
13283   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpunordps", IX86_BUILTIN_CMPUNORDPS, UNORDERED, 0 },
13284   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpneqps", IX86_BUILTIN_CMPNEQPS, NE, 0 },
13285   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnltps", IX86_BUILTIN_CMPNLTPS, UNGE, 0 },
13286   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpnleps", IX86_BUILTIN_CMPNLEPS, UNGT, 0 },
13287   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngtps", IX86_BUILTIN_CMPNGTPS, UNGE,
13288     BUILTIN_DESC_SWAP_OPERANDS },
13289   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpngeps", IX86_BUILTIN_CMPNGEPS, UNGT,
13290     BUILTIN_DESC_SWAP_OPERANDS },
13291   { MASK_SSE, CODE_FOR_sse_maskcmpv4sf3, "__builtin_ia32_cmpordps", IX86_BUILTIN_CMPORDPS, ORDERED, 0 },
13292   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpeqss", IX86_BUILTIN_CMPEQSS, EQ, 0 },
13293   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpltss", IX86_BUILTIN_CMPLTSS, LT, 0 },
13294   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpless", IX86_BUILTIN_CMPLESS, LE, 0 },
13295   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpunordss", IX86_BUILTIN_CMPUNORDSS, UNORDERED, 0 },
13296   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpneqss", IX86_BUILTIN_CMPNEQSS, NE, 0 },
13297   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnltss", IX86_BUILTIN_CMPNLTSS, UNGE, 0 },
13298   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpnless", IX86_BUILTIN_CMPNLESS, UNGT, 0 },
13299   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngtss", IX86_BUILTIN_CMPNGTSS, UNGE,
13300     BUILTIN_DESC_SWAP_OPERANDS },
13301   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpngess", IX86_BUILTIN_CMPNGESS, UNGT,
13302     BUILTIN_DESC_SWAP_OPERANDS },
13303   { MASK_SSE, CODE_FOR_sse_vmmaskcmpv4sf3, "__builtin_ia32_cmpordss", IX86_BUILTIN_CMPORDSS, UNORDERED, 0 },
13304
13305   { MASK_SSE, CODE_FOR_sminv4sf3, "__builtin_ia32_minps", IX86_BUILTIN_MINPS, 0, 0 },
13306   { MASK_SSE, CODE_FOR_smaxv4sf3, "__builtin_ia32_maxps", IX86_BUILTIN_MAXPS, 0, 0 },
13307   { MASK_SSE, CODE_FOR_sse_vmsminv4sf3, "__builtin_ia32_minss", IX86_BUILTIN_MINSS, 0, 0 },
13308   { MASK_SSE, CODE_FOR_sse_vmsmaxv4sf3, "__builtin_ia32_maxss", IX86_BUILTIN_MAXSS, 0, 0 },
13309
13310   { MASK_SSE, CODE_FOR_andv4sf3, "__builtin_ia32_andps", IX86_BUILTIN_ANDPS, 0, 0 },
13311   { MASK_SSE, CODE_FOR_sse_nandv4sf3,  "__builtin_ia32_andnps", IX86_BUILTIN_ANDNPS, 0, 0 },
13312   { MASK_SSE, CODE_FOR_iorv4sf3, "__builtin_ia32_orps", IX86_BUILTIN_ORPS, 0, 0 },
13313   { MASK_SSE, CODE_FOR_xorv4sf3,  "__builtin_ia32_xorps", IX86_BUILTIN_XORPS, 0, 0 },
13314
13315   { MASK_SSE, CODE_FOR_sse_movss,  "__builtin_ia32_movss", IX86_BUILTIN_MOVSS, 0, 0 },
13316   { MASK_SSE, CODE_FOR_sse_movhlps,  "__builtin_ia32_movhlps", IX86_BUILTIN_MOVHLPS, 0, 0 },
13317   { MASK_SSE, CODE_FOR_sse_movlhps,  "__builtin_ia32_movlhps", IX86_BUILTIN_MOVLHPS, 0, 0 },
13318   { MASK_SSE, CODE_FOR_sse_unpckhps, "__builtin_ia32_unpckhps", IX86_BUILTIN_UNPCKHPS, 0, 0 },
13319   { MASK_SSE, CODE_FOR_sse_unpcklps, "__builtin_ia32_unpcklps", IX86_BUILTIN_UNPCKLPS, 0, 0 },
13320
13321   /* MMX */
13322   { MASK_MMX, CODE_FOR_mmx_addv8qi3, "__builtin_ia32_paddb", IX86_BUILTIN_PADDB, 0, 0 },
13323   { MASK_MMX, CODE_FOR_mmx_addv4hi3, "__builtin_ia32_paddw", IX86_BUILTIN_PADDW, 0, 0 },
13324   { MASK_MMX, CODE_FOR_mmx_addv2si3, "__builtin_ia32_paddd", IX86_BUILTIN_PADDD, 0, 0 },
13325   { MASK_MMX, CODE_FOR_mmx_adddi3, "__builtin_ia32_paddq", IX86_BUILTIN_PADDQ, 0, 0 },
13326   { MASK_MMX, CODE_FOR_mmx_subv8qi3, "__builtin_ia32_psubb", IX86_BUILTIN_PSUBB, 0, 0 },
13327   { MASK_MMX, CODE_FOR_mmx_subv4hi3, "__builtin_ia32_psubw", IX86_BUILTIN_PSUBW, 0, 0 },
13328   { MASK_MMX, CODE_FOR_mmx_subv2si3, "__builtin_ia32_psubd", IX86_BUILTIN_PSUBD, 0, 0 },
13329   { MASK_MMX, CODE_FOR_mmx_subdi3, "__builtin_ia32_psubq", IX86_BUILTIN_PSUBQ, 0, 0 },
13330
13331   { MASK_MMX, CODE_FOR_mmx_ssaddv8qi3, "__builtin_ia32_paddsb", IX86_BUILTIN_PADDSB, 0, 0 },
13332   { MASK_MMX, CODE_FOR_mmx_ssaddv4hi3, "__builtin_ia32_paddsw", IX86_BUILTIN_PADDSW, 0, 0 },
13333   { MASK_MMX, CODE_FOR_mmx_sssubv8qi3, "__builtin_ia32_psubsb", IX86_BUILTIN_PSUBSB, 0, 0 },
13334   { MASK_MMX, CODE_FOR_mmx_sssubv4hi3, "__builtin_ia32_psubsw", IX86_BUILTIN_PSUBSW, 0, 0 },
13335   { MASK_MMX, CODE_FOR_mmx_usaddv8qi3, "__builtin_ia32_paddusb", IX86_BUILTIN_PADDUSB, 0, 0 },
13336   { MASK_MMX, CODE_FOR_mmx_usaddv4hi3, "__builtin_ia32_paddusw", IX86_BUILTIN_PADDUSW, 0, 0 },
13337   { MASK_MMX, CODE_FOR_mmx_ussubv8qi3, "__builtin_ia32_psubusb", IX86_BUILTIN_PSUBUSB, 0, 0 },
13338   { MASK_MMX, CODE_FOR_mmx_ussubv4hi3, "__builtin_ia32_psubusw", IX86_BUILTIN_PSUBUSW, 0, 0 },
13339
13340   { MASK_MMX, CODE_FOR_mmx_mulv4hi3, "__builtin_ia32_pmullw", IX86_BUILTIN_PMULLW, 0, 0 },
13341   { MASK_MMX, CODE_FOR_mmx_smulv4hi3_highpart, "__builtin_ia32_pmulhw", IX86_BUILTIN_PMULHW, 0, 0 },
13342   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umulv4hi3_highpart, "__builtin_ia32_pmulhuw", IX86_BUILTIN_PMULHUW, 0, 0 },
13343
13344   { MASK_MMX, CODE_FOR_mmx_andv2si3, "__builtin_ia32_pand", IX86_BUILTIN_PAND, 0, 0 },
13345   { MASK_MMX, CODE_FOR_mmx_nandv2si3, "__builtin_ia32_pandn", IX86_BUILTIN_PANDN, 0, 0 },
13346   { MASK_MMX, CODE_FOR_mmx_iorv2si3, "__builtin_ia32_por", IX86_BUILTIN_POR, 0, 0 },
13347   { MASK_MMX, CODE_FOR_mmx_xorv2si3, "__builtin_ia32_pxor", IX86_BUILTIN_PXOR, 0, 0 },
13348
13349   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv8qi3, "__builtin_ia32_pavgb", IX86_BUILTIN_PAVGB, 0, 0 },
13350   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uavgv4hi3, "__builtin_ia32_pavgw", IX86_BUILTIN_PAVGW, 0, 0 },
13351
13352   { MASK_MMX, CODE_FOR_mmx_eqv8qi3, "__builtin_ia32_pcmpeqb", IX86_BUILTIN_PCMPEQB, 0, 0 },
13353   { MASK_MMX, CODE_FOR_mmx_eqv4hi3, "__builtin_ia32_pcmpeqw", IX86_BUILTIN_PCMPEQW, 0, 0 },
13354   { MASK_MMX, CODE_FOR_mmx_eqv2si3, "__builtin_ia32_pcmpeqd", IX86_BUILTIN_PCMPEQD, 0, 0 },
13355   { MASK_MMX, CODE_FOR_mmx_gtv8qi3, "__builtin_ia32_pcmpgtb", IX86_BUILTIN_PCMPGTB, 0, 0 },
13356   { MASK_MMX, CODE_FOR_mmx_gtv4hi3, "__builtin_ia32_pcmpgtw", IX86_BUILTIN_PCMPGTW, 0, 0 },
13357   { MASK_MMX, CODE_FOR_mmx_gtv2si3, "__builtin_ia32_pcmpgtd", IX86_BUILTIN_PCMPGTD, 0, 0 },
13358
13359   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_umaxv8qi3, "__builtin_ia32_pmaxub", IX86_BUILTIN_PMAXUB, 0, 0 },
13360   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_smaxv4hi3, "__builtin_ia32_pmaxsw", IX86_BUILTIN_PMAXSW, 0, 0 },
13361   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_uminv8qi3, "__builtin_ia32_pminub", IX86_BUILTIN_PMINUB, 0, 0 },
13362   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_sminv4hi3, "__builtin_ia32_pminsw", IX86_BUILTIN_PMINSW, 0, 0 },
13363
13364   { MASK_MMX, CODE_FOR_mmx_punpckhbw, "__builtin_ia32_punpckhbw", IX86_BUILTIN_PUNPCKHBW, 0, 0 },
13365   { MASK_MMX, CODE_FOR_mmx_punpckhwd, "__builtin_ia32_punpckhwd", IX86_BUILTIN_PUNPCKHWD, 0, 0 },
13366   { MASK_MMX, CODE_FOR_mmx_punpckhdq, "__builtin_ia32_punpckhdq", IX86_BUILTIN_PUNPCKHDQ, 0, 0 },
13367   { MASK_MMX, CODE_FOR_mmx_punpcklbw, "__builtin_ia32_punpcklbw", IX86_BUILTIN_PUNPCKLBW, 0, 0 },
13368   { MASK_MMX, CODE_FOR_mmx_punpcklwd, "__builtin_ia32_punpcklwd", IX86_BUILTIN_PUNPCKLWD, 0, 0 },
13369   { MASK_MMX, CODE_FOR_mmx_punpckldq, "__builtin_ia32_punpckldq", IX86_BUILTIN_PUNPCKLDQ, 0, 0 },
13370
13371   /* Special.  */
13372   { MASK_MMX, CODE_FOR_mmx_packsswb, 0, IX86_BUILTIN_PACKSSWB, 0, 0 },
13373   { MASK_MMX, CODE_FOR_mmx_packssdw, 0, IX86_BUILTIN_PACKSSDW, 0, 0 },
13374   { MASK_MMX, CODE_FOR_mmx_packuswb, 0, IX86_BUILTIN_PACKUSWB, 0, 0 },
13375
13376   { MASK_SSE, CODE_FOR_sse_cvtpi2ps, 0, IX86_BUILTIN_CVTPI2PS, 0, 0 },
13377   { MASK_SSE, CODE_FOR_sse_cvtsi2ss, 0, IX86_BUILTIN_CVTSI2SS, 0, 0 },
13378   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtsi2ssq, 0, IX86_BUILTIN_CVTSI642SS, 0, 0 },
13379
13380   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLW, 0, 0 },
13381   { MASK_MMX, CODE_FOR_mmx_ashlv4hi3, 0, IX86_BUILTIN_PSLLWI, 0, 0 },
13382   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLD, 0, 0 },
13383   { MASK_MMX, CODE_FOR_mmx_ashlv2si3, 0, IX86_BUILTIN_PSLLDI, 0, 0 },
13384   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQ, 0, 0 },
13385   { MASK_MMX, CODE_FOR_mmx_ashldi3, 0, IX86_BUILTIN_PSLLQI, 0, 0 },
13386
13387   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLW, 0, 0 },
13388   { MASK_MMX, CODE_FOR_mmx_lshrv4hi3, 0, IX86_BUILTIN_PSRLWI, 0, 0 },
13389   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLD, 0, 0 },
13390   { MASK_MMX, CODE_FOR_mmx_lshrv2si3, 0, IX86_BUILTIN_PSRLDI, 0, 0 },
13391   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQ, 0, 0 },
13392   { MASK_MMX, CODE_FOR_mmx_lshrdi3, 0, IX86_BUILTIN_PSRLQI, 0, 0 },
13393
13394   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAW, 0, 0 },
13395   { MASK_MMX, CODE_FOR_mmx_ashrv4hi3, 0, IX86_BUILTIN_PSRAWI, 0, 0 },
13396   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRAD, 0, 0 },
13397   { MASK_MMX, CODE_FOR_mmx_ashrv2si3, 0, IX86_BUILTIN_PSRADI, 0, 0 },
13398
13399   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_psadbw, 0, IX86_BUILTIN_PSADBW, 0, 0 },
13400   { MASK_MMX, CODE_FOR_mmx_pmaddwd, 0, IX86_BUILTIN_PMADDWD, 0, 0 },
13401
13402   /* SSE2 */
13403   { MASK_SSE2, CODE_FOR_addv2df3, "__builtin_ia32_addpd", IX86_BUILTIN_ADDPD, 0, 0 },
13404   { MASK_SSE2, CODE_FOR_subv2df3, "__builtin_ia32_subpd", IX86_BUILTIN_SUBPD, 0, 0 },
13405   { MASK_SSE2, CODE_FOR_mulv2df3, "__builtin_ia32_mulpd", IX86_BUILTIN_MULPD, 0, 0 },
13406   { MASK_SSE2, CODE_FOR_divv2df3, "__builtin_ia32_divpd", IX86_BUILTIN_DIVPD, 0, 0 },
13407   { MASK_SSE2, CODE_FOR_sse2_vmaddv2df3,  "__builtin_ia32_addsd", IX86_BUILTIN_ADDSD, 0, 0 },
13408   { MASK_SSE2, CODE_FOR_sse2_vmsubv2df3,  "__builtin_ia32_subsd", IX86_BUILTIN_SUBSD, 0, 0 },
13409   { MASK_SSE2, CODE_FOR_sse2_vmmulv2df3,  "__builtin_ia32_mulsd", IX86_BUILTIN_MULSD, 0, 0 },
13410   { MASK_SSE2, CODE_FOR_sse2_vmdivv2df3,  "__builtin_ia32_divsd", IX86_BUILTIN_DIVSD, 0, 0 },
13411
13412   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpeqpd", IX86_BUILTIN_CMPEQPD, EQ, 0 },
13413   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpltpd", IX86_BUILTIN_CMPLTPD, LT, 0 },
13414   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmplepd", IX86_BUILTIN_CMPLEPD, LE, 0 },
13415   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgtpd", IX86_BUILTIN_CMPGTPD, LT,
13416     BUILTIN_DESC_SWAP_OPERANDS },
13417   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpgepd", IX86_BUILTIN_CMPGEPD, LE,
13418     BUILTIN_DESC_SWAP_OPERANDS },
13419   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpunordpd", IX86_BUILTIN_CMPUNORDPD, UNORDERED, 0 },
13420   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpneqpd", IX86_BUILTIN_CMPNEQPD, NE, 0 },
13421   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnltpd", IX86_BUILTIN_CMPNLTPD, UNGE, 0 },
13422   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpnlepd", IX86_BUILTIN_CMPNLEPD, UNGT, 0 },
13423   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngtpd", IX86_BUILTIN_CMPNGTPD, UNGE,
13424     BUILTIN_DESC_SWAP_OPERANDS },
13425   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpngepd", IX86_BUILTIN_CMPNGEPD, UNGT,
13426     BUILTIN_DESC_SWAP_OPERANDS },
13427   { MASK_SSE2, CODE_FOR_sse2_maskcmpv2df3, "__builtin_ia32_cmpordpd", IX86_BUILTIN_CMPORDPD, ORDERED, 0 },
13428   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpeqsd", IX86_BUILTIN_CMPEQSD, EQ, 0 },
13429   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpltsd", IX86_BUILTIN_CMPLTSD, LT, 0 },
13430   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmplesd", IX86_BUILTIN_CMPLESD, LE, 0 },
13431   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpunordsd", IX86_BUILTIN_CMPUNORDSD, UNORDERED, 0 },
13432   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpneqsd", IX86_BUILTIN_CMPNEQSD, NE, 0 },
13433   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnltsd", IX86_BUILTIN_CMPNLTSD, UNGE, 0 },
13434   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpnlesd", IX86_BUILTIN_CMPNLESD, UNGT, 0 },
13435   { MASK_SSE2, CODE_FOR_sse2_vmmaskcmpv2df3, "__builtin_ia32_cmpordsd", IX86_BUILTIN_CMPORDSD, ORDERED, 0 },
13436
13437   { MASK_SSE2, CODE_FOR_sminv2df3, "__builtin_ia32_minpd", IX86_BUILTIN_MINPD, 0, 0 },
13438   { MASK_SSE2, CODE_FOR_smaxv2df3, "__builtin_ia32_maxpd", IX86_BUILTIN_MAXPD, 0, 0 },
13439   { MASK_SSE2, CODE_FOR_sse2_vmsminv2df3, "__builtin_ia32_minsd", IX86_BUILTIN_MINSD, 0, 0 },
13440   { MASK_SSE2, CODE_FOR_sse2_vmsmaxv2df3, "__builtin_ia32_maxsd", IX86_BUILTIN_MAXSD, 0, 0 },
13441
13442   { MASK_SSE2, CODE_FOR_andv2df3, "__builtin_ia32_andpd", IX86_BUILTIN_ANDPD, 0, 0 },
13443   { MASK_SSE2, CODE_FOR_sse2_nandv2df3,  "__builtin_ia32_andnpd", IX86_BUILTIN_ANDNPD, 0, 0 },
13444   { MASK_SSE2, CODE_FOR_iorv2df3, "__builtin_ia32_orpd", IX86_BUILTIN_ORPD, 0, 0 },
13445   { MASK_SSE2, CODE_FOR_xorv2df3,  "__builtin_ia32_xorpd", IX86_BUILTIN_XORPD, 0, 0 },
13446
13447   { MASK_SSE2, CODE_FOR_sse2_movsd,  "__builtin_ia32_movsd", IX86_BUILTIN_MOVSD, 0, 0 },
13448   { MASK_SSE2, CODE_FOR_sse2_unpckhpd, "__builtin_ia32_unpckhpd", IX86_BUILTIN_UNPCKHPD, 0, 0 },
13449   { MASK_SSE2, CODE_FOR_sse2_unpcklpd, "__builtin_ia32_unpcklpd", IX86_BUILTIN_UNPCKLPD, 0, 0 },
13450
13451   /* SSE2 MMX */
13452   { MASK_SSE2, CODE_FOR_addv16qi3, "__builtin_ia32_paddb128", IX86_BUILTIN_PADDB128, 0, 0 },
13453   { MASK_SSE2, CODE_FOR_addv8hi3, "__builtin_ia32_paddw128", IX86_BUILTIN_PADDW128, 0, 0 },
13454   { MASK_SSE2, CODE_FOR_addv4si3, "__builtin_ia32_paddd128", IX86_BUILTIN_PADDD128, 0, 0 },
13455   { MASK_SSE2, CODE_FOR_addv2di3, "__builtin_ia32_paddq128", IX86_BUILTIN_PADDQ128, 0, 0 },
13456   { MASK_SSE2, CODE_FOR_subv16qi3, "__builtin_ia32_psubb128", IX86_BUILTIN_PSUBB128, 0, 0 },
13457   { MASK_SSE2, CODE_FOR_subv8hi3, "__builtin_ia32_psubw128", IX86_BUILTIN_PSUBW128, 0, 0 },
13458   { MASK_SSE2, CODE_FOR_subv4si3, "__builtin_ia32_psubd128", IX86_BUILTIN_PSUBD128, 0, 0 },
13459   { MASK_SSE2, CODE_FOR_subv2di3, "__builtin_ia32_psubq128", IX86_BUILTIN_PSUBQ128, 0, 0 },
13460
13461   { MASK_MMX, CODE_FOR_sse2_ssaddv16qi3, "__builtin_ia32_paddsb128", IX86_BUILTIN_PADDSB128, 0, 0 },
13462   { MASK_MMX, CODE_FOR_sse2_ssaddv8hi3, "__builtin_ia32_paddsw128", IX86_BUILTIN_PADDSW128, 0, 0 },
13463   { MASK_MMX, CODE_FOR_sse2_sssubv16qi3, "__builtin_ia32_psubsb128", IX86_BUILTIN_PSUBSB128, 0, 0 },
13464   { MASK_MMX, CODE_FOR_sse2_sssubv8hi3, "__builtin_ia32_psubsw128", IX86_BUILTIN_PSUBSW128, 0, 0 },
13465   { MASK_MMX, CODE_FOR_sse2_usaddv16qi3, "__builtin_ia32_paddusb128", IX86_BUILTIN_PADDUSB128, 0, 0 },
13466   { MASK_MMX, CODE_FOR_sse2_usaddv8hi3, "__builtin_ia32_paddusw128", IX86_BUILTIN_PADDUSW128, 0, 0 },
13467   { MASK_MMX, CODE_FOR_sse2_ussubv16qi3, "__builtin_ia32_psubusb128", IX86_BUILTIN_PSUBUSB128, 0, 0 },
13468   { MASK_MMX, CODE_FOR_sse2_ussubv8hi3, "__builtin_ia32_psubusw128", IX86_BUILTIN_PSUBUSW128, 0, 0 },
13469
13470   { MASK_SSE2, CODE_FOR_mulv8hi3, "__builtin_ia32_pmullw128", IX86_BUILTIN_PMULLW128, 0, 0 },
13471   { MASK_SSE2, CODE_FOR_sse2_smulv8hi3_highpart, "__builtin_ia32_pmulhw128", IX86_BUILTIN_PMULHW128, 0, 0 },
13472
13473   { MASK_SSE2, CODE_FOR_andv2di3, "__builtin_ia32_pand128", IX86_BUILTIN_PAND128, 0, 0 },
13474   { MASK_SSE2, CODE_FOR_sse2_nandv2di3, "__builtin_ia32_pandn128", IX86_BUILTIN_PANDN128, 0, 0 },
13475   { MASK_SSE2, CODE_FOR_iorv2di3, "__builtin_ia32_por128", IX86_BUILTIN_POR128, 0, 0 },
13476   { MASK_SSE2, CODE_FOR_xorv2di3, "__builtin_ia32_pxor128", IX86_BUILTIN_PXOR128, 0, 0 },
13477
13478   { MASK_SSE2, CODE_FOR_sse2_uavgv16qi3, "__builtin_ia32_pavgb128", IX86_BUILTIN_PAVGB128, 0, 0 },
13479   { MASK_SSE2, CODE_FOR_sse2_uavgv8hi3, "__builtin_ia32_pavgw128", IX86_BUILTIN_PAVGW128, 0, 0 },
13480
13481   { MASK_SSE2, CODE_FOR_sse2_eqv16qi3, "__builtin_ia32_pcmpeqb128", IX86_BUILTIN_PCMPEQB128, 0, 0 },
13482   { MASK_SSE2, CODE_FOR_sse2_eqv8hi3, "__builtin_ia32_pcmpeqw128", IX86_BUILTIN_PCMPEQW128, 0, 0 },
13483   { MASK_SSE2, CODE_FOR_sse2_eqv4si3, "__builtin_ia32_pcmpeqd128", IX86_BUILTIN_PCMPEQD128, 0, 0 },
13484   { MASK_SSE2, CODE_FOR_sse2_gtv16qi3, "__builtin_ia32_pcmpgtb128", IX86_BUILTIN_PCMPGTB128, 0, 0 },
13485   { MASK_SSE2, CODE_FOR_sse2_gtv8hi3, "__builtin_ia32_pcmpgtw128", IX86_BUILTIN_PCMPGTW128, 0, 0 },
13486   { MASK_SSE2, CODE_FOR_sse2_gtv4si3, "__builtin_ia32_pcmpgtd128", IX86_BUILTIN_PCMPGTD128, 0, 0 },
13487
13488   { MASK_SSE2, CODE_FOR_umaxv16qi3, "__builtin_ia32_pmaxub128", IX86_BUILTIN_PMAXUB128, 0, 0 },
13489   { MASK_SSE2, CODE_FOR_smaxv8hi3, "__builtin_ia32_pmaxsw128", IX86_BUILTIN_PMAXSW128, 0, 0 },
13490   { MASK_SSE2, CODE_FOR_uminv16qi3, "__builtin_ia32_pminub128", IX86_BUILTIN_PMINUB128, 0, 0 },
13491   { MASK_SSE2, CODE_FOR_sminv8hi3, "__builtin_ia32_pminsw128", IX86_BUILTIN_PMINSW128, 0, 0 },
13492
13493   { MASK_SSE2, CODE_FOR_sse2_punpckhbw, "__builtin_ia32_punpckhbw128", IX86_BUILTIN_PUNPCKHBW128, 0, 0 },
13494   { MASK_SSE2, CODE_FOR_sse2_punpckhwd, "__builtin_ia32_punpckhwd128", IX86_BUILTIN_PUNPCKHWD128, 0, 0 },
13495   { MASK_SSE2, CODE_FOR_sse2_punpckhdq, "__builtin_ia32_punpckhdq128", IX86_BUILTIN_PUNPCKHDQ128, 0, 0 },
13496   { MASK_SSE2, CODE_FOR_sse2_punpckhqdq, "__builtin_ia32_punpckhqdq128", IX86_BUILTIN_PUNPCKHQDQ128, 0, 0 },
13497   { MASK_SSE2, CODE_FOR_sse2_punpcklbw, "__builtin_ia32_punpcklbw128", IX86_BUILTIN_PUNPCKLBW128, 0, 0 },
13498   { MASK_SSE2, CODE_FOR_sse2_punpcklwd, "__builtin_ia32_punpcklwd128", IX86_BUILTIN_PUNPCKLWD128, 0, 0 },
13499   { MASK_SSE2, CODE_FOR_sse2_punpckldq, "__builtin_ia32_punpckldq128", IX86_BUILTIN_PUNPCKLDQ128, 0, 0 },
13500   { MASK_SSE2, CODE_FOR_sse2_punpcklqdq, "__builtin_ia32_punpcklqdq128", IX86_BUILTIN_PUNPCKLQDQ128, 0, 0 },
13501
13502   { MASK_SSE2, CODE_FOR_sse2_packsswb, "__builtin_ia32_packsswb128", IX86_BUILTIN_PACKSSWB128, 0, 0 },
13503   { MASK_SSE2, CODE_FOR_sse2_packssdw, "__builtin_ia32_packssdw128", IX86_BUILTIN_PACKSSDW128, 0, 0 },
13504   { MASK_SSE2, CODE_FOR_sse2_packuswb, "__builtin_ia32_packuswb128", IX86_BUILTIN_PACKUSWB128, 0, 0 },
13505
13506   { MASK_SSE2, CODE_FOR_sse2_umulv8hi3_highpart, "__builtin_ia32_pmulhuw128", IX86_BUILTIN_PMULHUW128, 0, 0 },
13507   { MASK_SSE2, CODE_FOR_sse2_psadbw, 0, IX86_BUILTIN_PSADBW128, 0, 0 },
13508
13509   { MASK_SSE2, CODE_FOR_sse2_umulsidi3, 0, IX86_BUILTIN_PMULUDQ, 0, 0 },
13510   { MASK_SSE2, CODE_FOR_sse2_umulv2siv2di3, 0, IX86_BUILTIN_PMULUDQ128, 0, 0 },
13511
13512   { MASK_SSE2, CODE_FOR_ashlv8hi3, 0, IX86_BUILTIN_PSLLWI128, 0, 0 },
13513   { MASK_SSE2, CODE_FOR_ashlv4si3, 0, IX86_BUILTIN_PSLLDI128, 0, 0 },
13514   { MASK_SSE2, CODE_FOR_ashlv2di3, 0, IX86_BUILTIN_PSLLQI128, 0, 0 },
13515
13516   { MASK_SSE2, CODE_FOR_lshrv8hi3, 0, IX86_BUILTIN_PSRLWI128, 0, 0 },
13517   { MASK_SSE2, CODE_FOR_lshrv4si3, 0, IX86_BUILTIN_PSRLDI128, 0, 0 },
13518   { MASK_SSE2, CODE_FOR_lshrv2di3, 0, IX86_BUILTIN_PSRLQI128, 0, 0 },
13519
13520   { MASK_SSE2, CODE_FOR_ashrv8hi3, 0, IX86_BUILTIN_PSRAWI128, 0, 0 },
13521   { MASK_SSE2, CODE_FOR_ashrv4si3, 0, IX86_BUILTIN_PSRADI128, 0, 0 },
13522
13523   { MASK_SSE2, CODE_FOR_sse2_pmaddwd, 0, IX86_BUILTIN_PMADDWD128, 0, 0 },
13524
13525   { MASK_SSE2, CODE_FOR_sse2_cvtsi2sd, 0, IX86_BUILTIN_CVTSI2SD, 0, 0 },
13526   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsi2sdq, 0, IX86_BUILTIN_CVTSI642SD, 0, 0 },
13527   { MASK_SSE2, CODE_FOR_sse2_cvtsd2ss, 0, IX86_BUILTIN_CVTSD2SS, 0, 0 },
13528   { MASK_SSE2, CODE_FOR_sse2_cvtss2sd, 0, IX86_BUILTIN_CVTSS2SD, 0, 0 },
13529
13530   /* SSE3 MMX */
13531   { MASK_SSE3, CODE_FOR_sse3_addsubv4sf3, "__builtin_ia32_addsubps", IX86_BUILTIN_ADDSUBPS, 0, 0 },
13532   { MASK_SSE3, CODE_FOR_sse3_addsubv2df3, "__builtin_ia32_addsubpd", IX86_BUILTIN_ADDSUBPD, 0, 0 },
13533   { MASK_SSE3, CODE_FOR_sse3_haddv4sf3, "__builtin_ia32_haddps", IX86_BUILTIN_HADDPS, 0, 0 },
13534   { MASK_SSE3, CODE_FOR_sse3_haddv2df3, "__builtin_ia32_haddpd", IX86_BUILTIN_HADDPD, 0, 0 },
13535   { MASK_SSE3, CODE_FOR_sse3_hsubv4sf3, "__builtin_ia32_hsubps", IX86_BUILTIN_HSUBPS, 0, 0 },
13536   { MASK_SSE3, CODE_FOR_sse3_hsubv2df3, "__builtin_ia32_hsubpd", IX86_BUILTIN_HSUBPD, 0, 0 }
13537 };
13538
13539 static const struct builtin_description bdesc_1arg[] =
13540 {
13541   { MASK_SSE | MASK_3DNOW_A, CODE_FOR_mmx_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB, 0, 0 },
13542   { MASK_SSE, CODE_FOR_sse_movmskps, 0, IX86_BUILTIN_MOVMSKPS, 0, 0 },
13543
13544   { MASK_SSE, CODE_FOR_sqrtv4sf2, 0, IX86_BUILTIN_SQRTPS, 0, 0 },
13545   { MASK_SSE, CODE_FOR_sse_rsqrtv4sf2, 0, IX86_BUILTIN_RSQRTPS, 0, 0 },
13546   { MASK_SSE, CODE_FOR_sse_rcpv4sf2, 0, IX86_BUILTIN_RCPPS, 0, 0 },
13547
13548   { MASK_SSE, CODE_FOR_sse_cvtps2pi, 0, IX86_BUILTIN_CVTPS2PI, 0, 0 },
13549   { MASK_SSE, CODE_FOR_sse_cvtss2si, 0, IX86_BUILTIN_CVTSS2SI, 0, 0 },
13550   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvtss2siq, 0, IX86_BUILTIN_CVTSS2SI64, 0, 0 },
13551   { MASK_SSE, CODE_FOR_sse_cvttps2pi, 0, IX86_BUILTIN_CVTTPS2PI, 0, 0 },
13552   { MASK_SSE, CODE_FOR_sse_cvttss2si, 0, IX86_BUILTIN_CVTTSS2SI, 0, 0 },
13553   { MASK_SSE | MASK_64BIT, CODE_FOR_sse_cvttss2siq, 0, IX86_BUILTIN_CVTTSS2SI64, 0, 0 },
13554
13555   { MASK_SSE2, CODE_FOR_sse2_pmovmskb, 0, IX86_BUILTIN_PMOVMSKB128, 0, 0 },
13556   { MASK_SSE2, CODE_FOR_sse2_movmskpd, 0, IX86_BUILTIN_MOVMSKPD, 0, 0 },
13557
13558   { MASK_SSE2, CODE_FOR_sqrtv2df2, 0, IX86_BUILTIN_SQRTPD, 0, 0 },
13559
13560   { MASK_SSE2, CODE_FOR_sse2_cvtdq2pd, 0, IX86_BUILTIN_CVTDQ2PD, 0, 0 },
13561   { MASK_SSE2, CODE_FOR_sse2_cvtdq2ps, 0, IX86_BUILTIN_CVTDQ2PS, 0, 0 },
13562
13563   { MASK_SSE2, CODE_FOR_sse2_cvtpd2dq, 0, IX86_BUILTIN_CVTPD2DQ, 0, 0 },
13564   { MASK_SSE2, CODE_FOR_sse2_cvtpd2pi, 0, IX86_BUILTIN_CVTPD2PI, 0, 0 },
13565   { MASK_SSE2, CODE_FOR_sse2_cvtpd2ps, 0, IX86_BUILTIN_CVTPD2PS, 0, 0 },
13566   { MASK_SSE2, CODE_FOR_sse2_cvttpd2dq, 0, IX86_BUILTIN_CVTTPD2DQ, 0, 0 },
13567   { MASK_SSE2, CODE_FOR_sse2_cvttpd2pi, 0, IX86_BUILTIN_CVTTPD2PI, 0, 0 },
13568
13569   { MASK_SSE2, CODE_FOR_sse2_cvtpi2pd, 0, IX86_BUILTIN_CVTPI2PD, 0, 0 },
13570
13571   { MASK_SSE2, CODE_FOR_sse2_cvtsd2si, 0, IX86_BUILTIN_CVTSD2SI, 0, 0 },
13572   { MASK_SSE2, CODE_FOR_sse2_cvttsd2si, 0, IX86_BUILTIN_CVTTSD2SI, 0, 0 },
13573   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvtsd2siq, 0, IX86_BUILTIN_CVTSD2SI64, 0, 0 },
13574   { MASK_SSE2 | MASK_64BIT, CODE_FOR_sse2_cvttsd2siq, 0, IX86_BUILTIN_CVTTSD2SI64, 0, 0 },
13575
13576   { MASK_SSE2, CODE_FOR_sse2_cvtps2dq, 0, IX86_BUILTIN_CVTPS2DQ, 0, 0 },
13577   { MASK_SSE2, CODE_FOR_sse2_cvtps2pd, 0, IX86_BUILTIN_CVTPS2PD, 0, 0 },
13578   { MASK_SSE2, CODE_FOR_sse2_cvttps2dq, 0, IX86_BUILTIN_CVTTPS2DQ, 0, 0 },
13579
13580   /* SSE3 */
13581   { MASK_SSE3, CODE_FOR_sse3_movshdup, 0, IX86_BUILTIN_MOVSHDUP, 0, 0 },
13582   { MASK_SSE3, CODE_FOR_sse3_movsldup, 0, IX86_BUILTIN_MOVSLDUP, 0, 0 },
13583 };
13584
13585 static void
13586 ix86_init_builtins (void)
13587 {
13588   if (TARGET_MMX)
13589     ix86_init_mmx_sse_builtins ();
13590 }
13591
13592 /* Set up all the MMX/SSE builtins.  This is not called if TARGET_MMX
13593    is zero.  Otherwise, if TARGET_SSE is not set, only expand the MMX
13594    builtins.  */
13595 static void
13596 ix86_init_mmx_sse_builtins (void)
13597 {
13598   const struct builtin_description * d;
13599   size_t i;
13600
13601   tree V16QI_type_node = build_vector_type_for_mode (intQI_type_node, V16QImode);
13602   tree V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
13603   tree V2SF_type_node = build_vector_type_for_mode (float_type_node, V2SFmode);
13604   tree V2DI_type_node
13605     = build_vector_type_for_mode (long_long_integer_type_node, V2DImode);
13606   tree V2DF_type_node = build_vector_type_for_mode (double_type_node, V2DFmode);
13607   tree V4SF_type_node = build_vector_type_for_mode (float_type_node, V4SFmode);
13608   tree V4SI_type_node = build_vector_type_for_mode (intSI_type_node, V4SImode);
13609   tree V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
13610   tree V8QI_type_node = build_vector_type_for_mode (intQI_type_node, V8QImode);
13611   tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node, V8HImode);
13612
13613   tree pchar_type_node = build_pointer_type (char_type_node);
13614   tree pcchar_type_node = build_pointer_type (
13615                              build_type_variant (char_type_node, 1, 0));
13616   tree pfloat_type_node = build_pointer_type (float_type_node);
13617   tree pcfloat_type_node = build_pointer_type (
13618                              build_type_variant (float_type_node, 1, 0));
13619   tree pv2si_type_node = build_pointer_type (V2SI_type_node);
13620   tree pv2di_type_node = build_pointer_type (V2DI_type_node);
13621   tree pdi_type_node = build_pointer_type (long_long_unsigned_type_node);
13622
13623   /* Comparisons.  */
13624   tree int_ftype_v4sf_v4sf
13625     = build_function_type_list (integer_type_node,
13626                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13627   tree v4si_ftype_v4sf_v4sf
13628     = build_function_type_list (V4SI_type_node,
13629                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13630   /* MMX/SSE/integer conversions.  */
13631   tree int_ftype_v4sf
13632     = build_function_type_list (integer_type_node,
13633                                 V4SF_type_node, NULL_TREE);
13634   tree int64_ftype_v4sf
13635     = build_function_type_list (long_long_integer_type_node,
13636                                 V4SF_type_node, NULL_TREE);
13637   tree int_ftype_v8qi
13638     = build_function_type_list (integer_type_node, V8QI_type_node, NULL_TREE);
13639   tree v4sf_ftype_v4sf_int
13640     = build_function_type_list (V4SF_type_node,
13641                                 V4SF_type_node, integer_type_node, NULL_TREE);
13642   tree v4sf_ftype_v4sf_int64
13643     = build_function_type_list (V4SF_type_node,
13644                                 V4SF_type_node, long_long_integer_type_node,
13645                                 NULL_TREE);
13646   tree v4sf_ftype_v4sf_v2si
13647     = build_function_type_list (V4SF_type_node,
13648                                 V4SF_type_node, V2SI_type_node, NULL_TREE);
13649
13650   /* Miscellaneous.  */
13651   tree v8qi_ftype_v4hi_v4hi
13652     = build_function_type_list (V8QI_type_node,
13653                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13654   tree v4hi_ftype_v2si_v2si
13655     = build_function_type_list (V4HI_type_node,
13656                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
13657   tree v4sf_ftype_v4sf_v4sf_int
13658     = build_function_type_list (V4SF_type_node,
13659                                 V4SF_type_node, V4SF_type_node,
13660                                 integer_type_node, NULL_TREE);
13661   tree v2si_ftype_v4hi_v4hi
13662     = build_function_type_list (V2SI_type_node,
13663                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13664   tree v4hi_ftype_v4hi_int
13665     = build_function_type_list (V4HI_type_node,
13666                                 V4HI_type_node, integer_type_node, NULL_TREE);
13667   tree v4hi_ftype_v4hi_di
13668     = build_function_type_list (V4HI_type_node,
13669                                 V4HI_type_node, long_long_unsigned_type_node,
13670                                 NULL_TREE);
13671   tree v2si_ftype_v2si_di
13672     = build_function_type_list (V2SI_type_node,
13673                                 V2SI_type_node, long_long_unsigned_type_node,
13674                                 NULL_TREE);
13675   tree void_ftype_void
13676     = build_function_type (void_type_node, void_list_node);
13677   tree void_ftype_unsigned
13678     = build_function_type_list (void_type_node, unsigned_type_node, NULL_TREE);
13679   tree void_ftype_unsigned_unsigned
13680     = build_function_type_list (void_type_node, unsigned_type_node,
13681                                 unsigned_type_node, NULL_TREE);
13682   tree void_ftype_pcvoid_unsigned_unsigned
13683     = build_function_type_list (void_type_node, const_ptr_type_node,
13684                                 unsigned_type_node, unsigned_type_node,
13685                                 NULL_TREE);
13686   tree unsigned_ftype_void
13687     = build_function_type (unsigned_type_node, void_list_node);
13688   tree v2si_ftype_v4sf
13689     = build_function_type_list (V2SI_type_node, V4SF_type_node, NULL_TREE);
13690   /* Loads/stores.  */
13691   tree void_ftype_v8qi_v8qi_pchar
13692     = build_function_type_list (void_type_node,
13693                                 V8QI_type_node, V8QI_type_node,
13694                                 pchar_type_node, NULL_TREE);
13695   tree v4sf_ftype_pcfloat
13696     = build_function_type_list (V4SF_type_node, pcfloat_type_node, NULL_TREE);
13697   /* @@@ the type is bogus */
13698   tree v4sf_ftype_v4sf_pv2si
13699     = build_function_type_list (V4SF_type_node,
13700                                 V4SF_type_node, pv2si_type_node, NULL_TREE);
13701   tree void_ftype_pv2si_v4sf
13702     = build_function_type_list (void_type_node,
13703                                 pv2si_type_node, V4SF_type_node, NULL_TREE);
13704   tree void_ftype_pfloat_v4sf
13705     = build_function_type_list (void_type_node,
13706                                 pfloat_type_node, V4SF_type_node, NULL_TREE);
13707   tree void_ftype_pdi_di
13708     = build_function_type_list (void_type_node,
13709                                 pdi_type_node, long_long_unsigned_type_node,
13710                                 NULL_TREE);
13711   tree void_ftype_pv2di_v2di
13712     = build_function_type_list (void_type_node,
13713                                 pv2di_type_node, V2DI_type_node, NULL_TREE);
13714   /* Normal vector unops.  */
13715   tree v4sf_ftype_v4sf
13716     = build_function_type_list (V4SF_type_node, V4SF_type_node, NULL_TREE);
13717
13718   /* Normal vector binops.  */
13719   tree v4sf_ftype_v4sf_v4sf
13720     = build_function_type_list (V4SF_type_node,
13721                                 V4SF_type_node, V4SF_type_node, NULL_TREE);
13722   tree v8qi_ftype_v8qi_v8qi
13723     = build_function_type_list (V8QI_type_node,
13724                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
13725   tree v4hi_ftype_v4hi_v4hi
13726     = build_function_type_list (V4HI_type_node,
13727                                 V4HI_type_node, V4HI_type_node, NULL_TREE);
13728   tree v2si_ftype_v2si_v2si
13729     = build_function_type_list (V2SI_type_node,
13730                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
13731   tree di_ftype_di_di
13732     = build_function_type_list (long_long_unsigned_type_node,
13733                                 long_long_unsigned_type_node,
13734                                 long_long_unsigned_type_node, NULL_TREE);
13735
13736   tree v2si_ftype_v2sf
13737     = build_function_type_list (V2SI_type_node, V2SF_type_node, NULL_TREE);
13738   tree v2sf_ftype_v2si
13739     = build_function_type_list (V2SF_type_node, V2SI_type_node, NULL_TREE);
13740   tree v2si_ftype_v2si
13741     = build_function_type_list (V2SI_type_node, V2SI_type_node, NULL_TREE);
13742   tree v2sf_ftype_v2sf
13743     = build_function_type_list (V2SF_type_node, V2SF_type_node, NULL_TREE);
13744   tree v2sf_ftype_v2sf_v2sf
13745     = build_function_type_list (V2SF_type_node,
13746                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
13747   tree v2si_ftype_v2sf_v2sf
13748     = build_function_type_list (V2SI_type_node,
13749                                 V2SF_type_node, V2SF_type_node, NULL_TREE);
13750   tree pint_type_node    = build_pointer_type (integer_type_node);
13751   tree pdouble_type_node = build_pointer_type (double_type_node);
13752   tree pcdouble_type_node = build_pointer_type (
13753                                 build_type_variant (double_type_node, 1, 0));
13754   tree int_ftype_v2df_v2df
13755     = build_function_type_list (integer_type_node,
13756                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13757
13758   tree ti_ftype_ti_ti
13759     = build_function_type_list (intTI_type_node,
13760                                 intTI_type_node, intTI_type_node, NULL_TREE);
13761   tree void_ftype_pcvoid
13762     = build_function_type_list (void_type_node, const_ptr_type_node, NULL_TREE);
13763   tree v4sf_ftype_v4si
13764     = build_function_type_list (V4SF_type_node, V4SI_type_node, NULL_TREE);
13765   tree v4si_ftype_v4sf
13766     = build_function_type_list (V4SI_type_node, V4SF_type_node, NULL_TREE);
13767   tree v2df_ftype_v4si
13768     = build_function_type_list (V2DF_type_node, V4SI_type_node, NULL_TREE);
13769   tree v4si_ftype_v2df
13770     = build_function_type_list (V4SI_type_node, V2DF_type_node, NULL_TREE);
13771   tree v2si_ftype_v2df
13772     = build_function_type_list (V2SI_type_node, V2DF_type_node, NULL_TREE);
13773   tree v4sf_ftype_v2df
13774     = build_function_type_list (V4SF_type_node, V2DF_type_node, NULL_TREE);
13775   tree v2df_ftype_v2si
13776     = build_function_type_list (V2DF_type_node, V2SI_type_node, NULL_TREE);
13777   tree v2df_ftype_v4sf
13778     = build_function_type_list (V2DF_type_node, V4SF_type_node, NULL_TREE);
13779   tree int_ftype_v2df
13780     = build_function_type_list (integer_type_node, V2DF_type_node, NULL_TREE);
13781   tree int64_ftype_v2df
13782     = build_function_type_list (long_long_integer_type_node,
13783                                 V2DF_type_node, NULL_TREE);
13784   tree v2df_ftype_v2df_int
13785     = build_function_type_list (V2DF_type_node,
13786                                 V2DF_type_node, integer_type_node, NULL_TREE);
13787   tree v2df_ftype_v2df_int64
13788     = build_function_type_list (V2DF_type_node,
13789                                 V2DF_type_node, long_long_integer_type_node,
13790                                 NULL_TREE);
13791   tree v4sf_ftype_v4sf_v2df
13792     = build_function_type_list (V4SF_type_node,
13793                                 V4SF_type_node, V2DF_type_node, NULL_TREE);
13794   tree v2df_ftype_v2df_v4sf
13795     = build_function_type_list (V2DF_type_node,
13796                                 V2DF_type_node, V4SF_type_node, NULL_TREE);
13797   tree v2df_ftype_v2df_v2df_int
13798     = build_function_type_list (V2DF_type_node,
13799                                 V2DF_type_node, V2DF_type_node,
13800                                 integer_type_node,
13801                                 NULL_TREE);
13802   tree v2df_ftype_v2df_pcdouble
13803     = build_function_type_list (V2DF_type_node,
13804                                 V2DF_type_node, pcdouble_type_node, NULL_TREE);
13805   tree void_ftype_pdouble_v2df
13806     = build_function_type_list (void_type_node,
13807                                 pdouble_type_node, V2DF_type_node, NULL_TREE);
13808   tree void_ftype_pint_int
13809     = build_function_type_list (void_type_node,
13810                                 pint_type_node, integer_type_node, NULL_TREE);
13811   tree void_ftype_v16qi_v16qi_pchar
13812     = build_function_type_list (void_type_node,
13813                                 V16QI_type_node, V16QI_type_node,
13814                                 pchar_type_node, NULL_TREE);
13815   tree v2df_ftype_pcdouble
13816     = build_function_type_list (V2DF_type_node, pcdouble_type_node, NULL_TREE);
13817   tree v2df_ftype_v2df_v2df
13818     = build_function_type_list (V2DF_type_node,
13819                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13820   tree v16qi_ftype_v16qi_v16qi
13821     = build_function_type_list (V16QI_type_node,
13822                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
13823   tree v8hi_ftype_v8hi_v8hi
13824     = build_function_type_list (V8HI_type_node,
13825                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
13826   tree v4si_ftype_v4si_v4si
13827     = build_function_type_list (V4SI_type_node,
13828                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
13829   tree v2di_ftype_v2di_v2di
13830     = build_function_type_list (V2DI_type_node,
13831                                 V2DI_type_node, V2DI_type_node, NULL_TREE);
13832   tree v2di_ftype_v2df_v2df
13833     = build_function_type_list (V2DI_type_node,
13834                                 V2DF_type_node, V2DF_type_node, NULL_TREE);
13835   tree v2df_ftype_v2df
13836     = build_function_type_list (V2DF_type_node, V2DF_type_node, NULL_TREE);
13837   tree v2di_ftype_v2di_int
13838     = build_function_type_list (V2DI_type_node,
13839                                 V2DI_type_node, integer_type_node, NULL_TREE);
13840   tree v4si_ftype_v4si_int
13841     = build_function_type_list (V4SI_type_node,
13842                                 V4SI_type_node, integer_type_node, NULL_TREE);
13843   tree v8hi_ftype_v8hi_int
13844     = build_function_type_list (V8HI_type_node,
13845                                 V8HI_type_node, integer_type_node, NULL_TREE);
13846   tree v8hi_ftype_v8hi_v2di
13847     = build_function_type_list (V8HI_type_node,
13848                                 V8HI_type_node, V2DI_type_node, NULL_TREE);
13849   tree v4si_ftype_v4si_v2di
13850     = build_function_type_list (V4SI_type_node,
13851                                 V4SI_type_node, V2DI_type_node, NULL_TREE);
13852   tree v4si_ftype_v8hi_v8hi
13853     = build_function_type_list (V4SI_type_node,
13854                                 V8HI_type_node, V8HI_type_node, NULL_TREE);
13855   tree di_ftype_v8qi_v8qi
13856     = build_function_type_list (long_long_unsigned_type_node,
13857                                 V8QI_type_node, V8QI_type_node, NULL_TREE);
13858   tree di_ftype_v2si_v2si
13859     = build_function_type_list (long_long_unsigned_type_node,
13860                                 V2SI_type_node, V2SI_type_node, NULL_TREE);
13861   tree v2di_ftype_v16qi_v16qi
13862     = build_function_type_list (V2DI_type_node,
13863                                 V16QI_type_node, V16QI_type_node, NULL_TREE);
13864   tree v2di_ftype_v4si_v4si
13865     = build_function_type_list (V2DI_type_node,
13866                                 V4SI_type_node, V4SI_type_node, NULL_TREE);
13867   tree int_ftype_v16qi
13868     = build_function_type_list (integer_type_node, V16QI_type_node, NULL_TREE);
13869   tree v16qi_ftype_pcchar
13870     = build_function_type_list (V16QI_type_node, pcchar_type_node, NULL_TREE);
13871   tree void_ftype_pchar_v16qi
13872     = build_function_type_list (void_type_node,
13873                                 pchar_type_node, V16QI_type_node, NULL_TREE);
13874
13875   tree float80_type;
13876   tree float128_type;
13877   tree ftype;
13878
13879   /* The __float80 type.  */
13880   if (TYPE_MODE (long_double_type_node) == XFmode)
13881     (*lang_hooks.types.register_builtin_type) (long_double_type_node,
13882                                                "__float80");
13883   else
13884     {
13885       /* The __float80 type.  */
13886       float80_type = make_node (REAL_TYPE);
13887       TYPE_PRECISION (float80_type) = 80;
13888       layout_type (float80_type);
13889       (*lang_hooks.types.register_builtin_type) (float80_type, "__float80");
13890     }
13891
13892   float128_type = make_node (REAL_TYPE);
13893   TYPE_PRECISION (float128_type) = 128;
13894   layout_type (float128_type);
13895   (*lang_hooks.types.register_builtin_type) (float128_type, "__float128");
13896
13897   /* Add all builtins that are more or less simple operations on two
13898      operands.  */
13899   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
13900     {
13901       /* Use one of the operands; the target can have a different mode for
13902          mask-generating compares.  */
13903       enum machine_mode mode;
13904       tree type;
13905
13906       if (d->name == 0)
13907         continue;
13908       mode = insn_data[d->icode].operand[1].mode;
13909
13910       switch (mode)
13911         {
13912         case V16QImode:
13913           type = v16qi_ftype_v16qi_v16qi;
13914           break;
13915         case V8HImode:
13916           type = v8hi_ftype_v8hi_v8hi;
13917           break;
13918         case V4SImode:
13919           type = v4si_ftype_v4si_v4si;
13920           break;
13921         case V2DImode:
13922           type = v2di_ftype_v2di_v2di;
13923           break;
13924         case V2DFmode:
13925           type = v2df_ftype_v2df_v2df;
13926           break;
13927         case TImode:
13928           type = ti_ftype_ti_ti;
13929           break;
13930         case V4SFmode:
13931           type = v4sf_ftype_v4sf_v4sf;
13932           break;
13933         case V8QImode:
13934           type = v8qi_ftype_v8qi_v8qi;
13935           break;
13936         case V4HImode:
13937           type = v4hi_ftype_v4hi_v4hi;
13938           break;
13939         case V2SImode:
13940           type = v2si_ftype_v2si_v2si;
13941           break;
13942         case DImode:
13943           type = di_ftype_di_di;
13944           break;
13945
13946         default:
13947           gcc_unreachable ();
13948         }
13949
13950       /* Override for comparisons.  */
13951       if (d->icode == CODE_FOR_sse_maskcmpv4sf3
13952           || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3)
13953         type = v4si_ftype_v4sf_v4sf;
13954
13955       if (d->icode == CODE_FOR_sse2_maskcmpv2df3
13956           || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
13957         type = v2di_ftype_v2df_v2df;
13958
13959       def_builtin (d->mask, d->name, type, d->code);
13960     }
13961
13962   /* Add the remaining MMX insns with somewhat more complicated types.  */
13963   def_builtin (MASK_MMX, "__builtin_ia32_emms", void_ftype_void, IX86_BUILTIN_EMMS);
13964   def_builtin (MASK_MMX, "__builtin_ia32_psllw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSLLW);
13965   def_builtin (MASK_MMX, "__builtin_ia32_pslld", v2si_ftype_v2si_di, IX86_BUILTIN_PSLLD);
13966   def_builtin (MASK_MMX, "__builtin_ia32_psllq", di_ftype_di_di, IX86_BUILTIN_PSLLQ);
13967
13968   def_builtin (MASK_MMX, "__builtin_ia32_psrlw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRLW);
13969   def_builtin (MASK_MMX, "__builtin_ia32_psrld", v2si_ftype_v2si_di, IX86_BUILTIN_PSRLD);
13970   def_builtin (MASK_MMX, "__builtin_ia32_psrlq", di_ftype_di_di, IX86_BUILTIN_PSRLQ);
13971
13972   def_builtin (MASK_MMX, "__builtin_ia32_psraw", v4hi_ftype_v4hi_di, IX86_BUILTIN_PSRAW);
13973   def_builtin (MASK_MMX, "__builtin_ia32_psrad", v2si_ftype_v2si_di, IX86_BUILTIN_PSRAD);
13974
13975   def_builtin (MASK_MMX, "__builtin_ia32_pshufw", v4hi_ftype_v4hi_int, IX86_BUILTIN_PSHUFW);
13976   def_builtin (MASK_MMX, "__builtin_ia32_pmaddwd", v2si_ftype_v4hi_v4hi, IX86_BUILTIN_PMADDWD);
13977
13978   /* comi/ucomi insns.  */
13979   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
13980     if (d->mask == MASK_SSE2)
13981       def_builtin (d->mask, d->name, int_ftype_v2df_v2df, d->code);
13982     else
13983       def_builtin (d->mask, d->name, int_ftype_v4sf_v4sf, d->code);
13984
13985   def_builtin (MASK_MMX, "__builtin_ia32_packsswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKSSWB);
13986   def_builtin (MASK_MMX, "__builtin_ia32_packssdw", v4hi_ftype_v2si_v2si, IX86_BUILTIN_PACKSSDW);
13987   def_builtin (MASK_MMX, "__builtin_ia32_packuswb", v8qi_ftype_v4hi_v4hi, IX86_BUILTIN_PACKUSWB);
13988
13989   def_builtin (MASK_SSE, "__builtin_ia32_ldmxcsr", void_ftype_unsigned, IX86_BUILTIN_LDMXCSR);
13990   def_builtin (MASK_SSE, "__builtin_ia32_stmxcsr", unsigned_ftype_void, IX86_BUILTIN_STMXCSR);
13991   def_builtin (MASK_SSE, "__builtin_ia32_cvtpi2ps", v4sf_ftype_v4sf_v2si, IX86_BUILTIN_CVTPI2PS);
13992   def_builtin (MASK_SSE, "__builtin_ia32_cvtps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTPS2PI);
13993   def_builtin (MASK_SSE, "__builtin_ia32_cvtsi2ss", v4sf_ftype_v4sf_int, IX86_BUILTIN_CVTSI2SS);
13994   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtsi642ss", v4sf_ftype_v4sf_int64, IX86_BUILTIN_CVTSI642SS);
13995   def_builtin (MASK_SSE, "__builtin_ia32_cvtss2si", int_ftype_v4sf, IX86_BUILTIN_CVTSS2SI);
13996   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvtss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTSS2SI64);
13997   def_builtin (MASK_SSE, "__builtin_ia32_cvttps2pi", v2si_ftype_v4sf, IX86_BUILTIN_CVTTPS2PI);
13998   def_builtin (MASK_SSE, "__builtin_ia32_cvttss2si", int_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI);
13999   def_builtin (MASK_SSE | MASK_64BIT, "__builtin_ia32_cvttss2si64", int64_ftype_v4sf, IX86_BUILTIN_CVTTSS2SI64);
14000
14001   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_maskmovq", void_ftype_v8qi_v8qi_pchar, IX86_BUILTIN_MASKMOVQ);
14002
14003   def_builtin (MASK_SSE, "__builtin_ia32_loadups", v4sf_ftype_pcfloat, IX86_BUILTIN_LOADUPS);
14004   def_builtin (MASK_SSE, "__builtin_ia32_storeups", void_ftype_pfloat_v4sf, IX86_BUILTIN_STOREUPS);
14005
14006   def_builtin (MASK_SSE, "__builtin_ia32_loadhps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADHPS);
14007   def_builtin (MASK_SSE, "__builtin_ia32_loadlps", v4sf_ftype_v4sf_pv2si, IX86_BUILTIN_LOADLPS);
14008   def_builtin (MASK_SSE, "__builtin_ia32_storehps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STOREHPS);
14009   def_builtin (MASK_SSE, "__builtin_ia32_storelps", void_ftype_pv2si_v4sf, IX86_BUILTIN_STORELPS);
14010
14011   def_builtin (MASK_SSE, "__builtin_ia32_movmskps", int_ftype_v4sf, IX86_BUILTIN_MOVMSKPS);
14012   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_pmovmskb", int_ftype_v8qi, IX86_BUILTIN_PMOVMSKB);
14013   def_builtin (MASK_SSE, "__builtin_ia32_movntps", void_ftype_pfloat_v4sf, IX86_BUILTIN_MOVNTPS);
14014   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_movntq", void_ftype_pdi_di, IX86_BUILTIN_MOVNTQ);
14015
14016   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_sfence", void_ftype_void, IX86_BUILTIN_SFENCE);
14017
14018   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_psadbw", di_ftype_v8qi_v8qi, IX86_BUILTIN_PSADBW);
14019
14020   def_builtin (MASK_SSE, "__builtin_ia32_rcpps", v4sf_ftype_v4sf, IX86_BUILTIN_RCPPS);
14021   def_builtin (MASK_SSE, "__builtin_ia32_rcpss", v4sf_ftype_v4sf, IX86_BUILTIN_RCPSS);
14022   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTPS);
14023   def_builtin (MASK_SSE, "__builtin_ia32_rsqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_RSQRTSS);
14024   def_builtin (MASK_SSE, "__builtin_ia32_sqrtps", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTPS);
14025   def_builtin (MASK_SSE, "__builtin_ia32_sqrtss", v4sf_ftype_v4sf, IX86_BUILTIN_SQRTSS);
14026
14027   def_builtin (MASK_SSE, "__builtin_ia32_shufps", v4sf_ftype_v4sf_v4sf_int, IX86_BUILTIN_SHUFPS);
14028
14029   /* Original 3DNow!  */
14030   def_builtin (MASK_3DNOW, "__builtin_ia32_femms", void_ftype_void, IX86_BUILTIN_FEMMS);
14031   def_builtin (MASK_3DNOW, "__builtin_ia32_pavgusb", v8qi_ftype_v8qi_v8qi, IX86_BUILTIN_PAVGUSB);
14032   def_builtin (MASK_3DNOW, "__builtin_ia32_pf2id", v2si_ftype_v2sf, IX86_BUILTIN_PF2ID);
14033   def_builtin (MASK_3DNOW, "__builtin_ia32_pfacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFACC);
14034   def_builtin (MASK_3DNOW, "__builtin_ia32_pfadd", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFADD);
14035   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpeq", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPEQ);
14036   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpge", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGE);
14037   def_builtin (MASK_3DNOW, "__builtin_ia32_pfcmpgt", v2si_ftype_v2sf_v2sf, IX86_BUILTIN_PFCMPGT);
14038   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmax", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMAX);
14039   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmin", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMIN);
14040   def_builtin (MASK_3DNOW, "__builtin_ia32_pfmul", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFMUL);
14041   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcp", v2sf_ftype_v2sf, IX86_BUILTIN_PFRCP);
14042   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT1);
14043   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrcpit2", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRCPIT2);
14044   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqrt", v2sf_ftype_v2sf, IX86_BUILTIN_PFRSQRT);
14045   def_builtin (MASK_3DNOW, "__builtin_ia32_pfrsqit1", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFRSQIT1);
14046   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsub", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUB);
14047   def_builtin (MASK_3DNOW, "__builtin_ia32_pfsubr", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFSUBR);
14048   def_builtin (MASK_3DNOW, "__builtin_ia32_pi2fd", v2sf_ftype_v2si, IX86_BUILTIN_PI2FD);
14049   def_builtin (MASK_3DNOW, "__builtin_ia32_pmulhrw", v4hi_ftype_v4hi_v4hi, IX86_BUILTIN_PMULHRW);
14050
14051   /* 3DNow! extension as used in the Athlon CPU.  */
14052   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pf2iw", v2si_ftype_v2sf, IX86_BUILTIN_PF2IW);
14053   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFNACC);
14054   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pfpnacc", v2sf_ftype_v2sf_v2sf, IX86_BUILTIN_PFPNACC);
14055   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pi2fw", v2sf_ftype_v2si, IX86_BUILTIN_PI2FW);
14056   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsf", v2sf_ftype_v2sf, IX86_BUILTIN_PSWAPDSF);
14057   def_builtin (MASK_3DNOW_A, "__builtin_ia32_pswapdsi", v2si_ftype_v2si, IX86_BUILTIN_PSWAPDSI);
14058
14059   /* SSE2 */
14060   def_builtin (MASK_SSE2, "__builtin_ia32_maskmovdqu", void_ftype_v16qi_v16qi_pchar, IX86_BUILTIN_MASKMOVDQU);
14061
14062   def_builtin (MASK_SSE2, "__builtin_ia32_loadupd", v2df_ftype_pcdouble, IX86_BUILTIN_LOADUPD);
14063   def_builtin (MASK_SSE2, "__builtin_ia32_storeupd", void_ftype_pdouble_v2df, IX86_BUILTIN_STOREUPD);
14064
14065   def_builtin (MASK_SSE2, "__builtin_ia32_loadhpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADHPD);
14066   def_builtin (MASK_SSE2, "__builtin_ia32_loadlpd", v2df_ftype_v2df_pcdouble, IX86_BUILTIN_LOADLPD);
14067
14068   def_builtin (MASK_SSE2, "__builtin_ia32_movmskpd", int_ftype_v2df, IX86_BUILTIN_MOVMSKPD);
14069   def_builtin (MASK_SSE2, "__builtin_ia32_pmovmskb128", int_ftype_v16qi, IX86_BUILTIN_PMOVMSKB128);
14070   def_builtin (MASK_SSE2, "__builtin_ia32_movnti", void_ftype_pint_int, IX86_BUILTIN_MOVNTI);
14071   def_builtin (MASK_SSE2, "__builtin_ia32_movntpd", void_ftype_pdouble_v2df, IX86_BUILTIN_MOVNTPD);
14072   def_builtin (MASK_SSE2, "__builtin_ia32_movntdq", void_ftype_pv2di_v2di, IX86_BUILTIN_MOVNTDQ);
14073
14074   def_builtin (MASK_SSE2, "__builtin_ia32_pshufd", v4si_ftype_v4si_int, IX86_BUILTIN_PSHUFD);
14075   def_builtin (MASK_SSE2, "__builtin_ia32_pshuflw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFLW);
14076   def_builtin (MASK_SSE2, "__builtin_ia32_pshufhw", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSHUFHW);
14077   def_builtin (MASK_SSE2, "__builtin_ia32_psadbw128", v2di_ftype_v16qi_v16qi, IX86_BUILTIN_PSADBW128);
14078
14079   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtpd", v2df_ftype_v2df, IX86_BUILTIN_SQRTPD);
14080   def_builtin (MASK_SSE2, "__builtin_ia32_sqrtsd", v2df_ftype_v2df, IX86_BUILTIN_SQRTSD);
14081
14082   def_builtin (MASK_SSE2, "__builtin_ia32_shufpd", v2df_ftype_v2df_v2df_int, IX86_BUILTIN_SHUFPD);
14083
14084   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2pd", v2df_ftype_v4si, IX86_BUILTIN_CVTDQ2PD);
14085   def_builtin (MASK_SSE2, "__builtin_ia32_cvtdq2ps", v4sf_ftype_v4si, IX86_BUILTIN_CVTDQ2PS);
14086
14087   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTPD2DQ);
14088   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTPD2PI);
14089   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpd2ps", v4sf_ftype_v2df, IX86_BUILTIN_CVTPD2PS);
14090   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2dq", v4si_ftype_v2df, IX86_BUILTIN_CVTTPD2DQ);
14091   def_builtin (MASK_SSE2, "__builtin_ia32_cvttpd2pi", v2si_ftype_v2df, IX86_BUILTIN_CVTTPD2PI);
14092
14093   def_builtin (MASK_SSE2, "__builtin_ia32_cvtpi2pd", v2df_ftype_v2si, IX86_BUILTIN_CVTPI2PD);
14094
14095   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2si", int_ftype_v2df, IX86_BUILTIN_CVTSD2SI);
14096   def_builtin (MASK_SSE2, "__builtin_ia32_cvttsd2si", int_ftype_v2df, IX86_BUILTIN_CVTTSD2SI);
14097   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTSD2SI64);
14098   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvttsd2si64", int64_ftype_v2df, IX86_BUILTIN_CVTTSD2SI64);
14099
14100   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTPS2DQ);
14101   def_builtin (MASK_SSE2, "__builtin_ia32_cvtps2pd", v2df_ftype_v4sf, IX86_BUILTIN_CVTPS2PD);
14102   def_builtin (MASK_SSE2, "__builtin_ia32_cvttps2dq", v4si_ftype_v4sf, IX86_BUILTIN_CVTTPS2DQ);
14103
14104   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsi2sd", v2df_ftype_v2df_int, IX86_BUILTIN_CVTSI2SD);
14105   def_builtin (MASK_SSE2 | MASK_64BIT, "__builtin_ia32_cvtsi642sd", v2df_ftype_v2df_int64, IX86_BUILTIN_CVTSI642SD);
14106   def_builtin (MASK_SSE2, "__builtin_ia32_cvtsd2ss", v4sf_ftype_v4sf_v2df, IX86_BUILTIN_CVTSD2SS);
14107   def_builtin (MASK_SSE2, "__builtin_ia32_cvtss2sd", v2df_ftype_v2df_v4sf, IX86_BUILTIN_CVTSS2SD);
14108
14109   def_builtin (MASK_SSE2, "__builtin_ia32_clflush", void_ftype_pcvoid, IX86_BUILTIN_CLFLUSH);
14110   def_builtin (MASK_SSE2, "__builtin_ia32_lfence", void_ftype_void, IX86_BUILTIN_LFENCE);
14111   def_builtin (MASK_SSE2, "__builtin_ia32_mfence", void_ftype_void, IX86_BUILTIN_MFENCE);
14112
14113   def_builtin (MASK_SSE2, "__builtin_ia32_loaddqu", v16qi_ftype_pcchar, IX86_BUILTIN_LOADDQU);
14114   def_builtin (MASK_SSE2, "__builtin_ia32_storedqu", void_ftype_pchar_v16qi, IX86_BUILTIN_STOREDQU);
14115
14116   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq", di_ftype_v2si_v2si, IX86_BUILTIN_PMULUDQ);
14117   def_builtin (MASK_SSE2, "__builtin_ia32_pmuludq128", v2di_ftype_v4si_v4si, IX86_BUILTIN_PMULUDQ128);
14118
14119   def_builtin (MASK_SSE2, "__builtin_ia32_psllw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSLLW128);
14120   def_builtin (MASK_SSE2, "__builtin_ia32_pslld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSLLD128);
14121   def_builtin (MASK_SSE2, "__builtin_ia32_psllq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSLLQ128);
14122
14123   def_builtin (MASK_SSE2, "__builtin_ia32_psrlw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRLW128);
14124   def_builtin (MASK_SSE2, "__builtin_ia32_psrld128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRLD128);
14125   def_builtin (MASK_SSE2, "__builtin_ia32_psrlq128", v2di_ftype_v2di_v2di, IX86_BUILTIN_PSRLQ128);
14126
14127   def_builtin (MASK_SSE2, "__builtin_ia32_psraw128", v8hi_ftype_v8hi_v2di, IX86_BUILTIN_PSRAW128);
14128   def_builtin (MASK_SSE2, "__builtin_ia32_psrad128", v4si_ftype_v4si_v2di, IX86_BUILTIN_PSRAD128);
14129
14130   def_builtin (MASK_SSE2, "__builtin_ia32_pslldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLDQI128);
14131   def_builtin (MASK_SSE2, "__builtin_ia32_psllwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSLLWI128);
14132   def_builtin (MASK_SSE2, "__builtin_ia32_pslldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSLLDI128);
14133   def_builtin (MASK_SSE2, "__builtin_ia32_psllqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSLLQI128);
14134
14135   def_builtin (MASK_SSE2, "__builtin_ia32_psrldqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLDQI128);
14136   def_builtin (MASK_SSE2, "__builtin_ia32_psrlwi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRLWI128);
14137   def_builtin (MASK_SSE2, "__builtin_ia32_psrldi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRLDI128);
14138   def_builtin (MASK_SSE2, "__builtin_ia32_psrlqi128", v2di_ftype_v2di_int, IX86_BUILTIN_PSRLQI128);
14139
14140   def_builtin (MASK_SSE2, "__builtin_ia32_psrawi128", v8hi_ftype_v8hi_int, IX86_BUILTIN_PSRAWI128);
14141   def_builtin (MASK_SSE2, "__builtin_ia32_psradi128", v4si_ftype_v4si_int, IX86_BUILTIN_PSRADI128);
14142
14143   def_builtin (MASK_SSE2, "__builtin_ia32_pmaddwd128", v4si_ftype_v8hi_v8hi, IX86_BUILTIN_PMADDWD128);
14144
14145   /* Prescott New Instructions.  */
14146   def_builtin (MASK_SSE3, "__builtin_ia32_monitor",
14147                void_ftype_pcvoid_unsigned_unsigned,
14148                IX86_BUILTIN_MONITOR);
14149   def_builtin (MASK_SSE3, "__builtin_ia32_mwait",
14150                void_ftype_unsigned_unsigned,
14151                IX86_BUILTIN_MWAIT);
14152   def_builtin (MASK_SSE3, "__builtin_ia32_movshdup",
14153                v4sf_ftype_v4sf,
14154                IX86_BUILTIN_MOVSHDUP);
14155   def_builtin (MASK_SSE3, "__builtin_ia32_movsldup",
14156                v4sf_ftype_v4sf,
14157                IX86_BUILTIN_MOVSLDUP);
14158   def_builtin (MASK_SSE3, "__builtin_ia32_lddqu",
14159                v16qi_ftype_pcchar, IX86_BUILTIN_LDDQU);
14160
14161   /* Access to the vec_init patterns.  */
14162   ftype = build_function_type_list (V2SI_type_node, integer_type_node,
14163                                     integer_type_node, NULL_TREE);
14164   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v2si",
14165                ftype, IX86_BUILTIN_VEC_INIT_V2SI);
14166
14167   ftype = build_function_type_list (V4HI_type_node, short_integer_type_node,
14168                                     short_integer_type_node,
14169                                     short_integer_type_node,
14170                                     short_integer_type_node, NULL_TREE);
14171   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v4hi",
14172                ftype, IX86_BUILTIN_VEC_INIT_V4HI);
14173
14174   ftype = build_function_type_list (V8QI_type_node, char_type_node,
14175                                     char_type_node, char_type_node,
14176                                     char_type_node, char_type_node,
14177                                     char_type_node, char_type_node,
14178                                     char_type_node, NULL_TREE);
14179   def_builtin (MASK_MMX, "__builtin_ia32_vec_init_v8qi",
14180                ftype, IX86_BUILTIN_VEC_INIT_V8QI);
14181
14182   /* Access to the vec_extract patterns.  */
14183   ftype = build_function_type_list (double_type_node, V2DF_type_node,
14184                                     integer_type_node, NULL_TREE);
14185   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2df",
14186                ftype, IX86_BUILTIN_VEC_EXT_V2DF);
14187
14188   ftype = build_function_type_list (long_long_integer_type_node,
14189                                     V2DI_type_node, integer_type_node,
14190                                     NULL_TREE);
14191   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v2di",
14192                ftype, IX86_BUILTIN_VEC_EXT_V2DI);
14193
14194   ftype = build_function_type_list (float_type_node, V4SF_type_node,
14195                                     integer_type_node, NULL_TREE);
14196   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4sf",
14197                ftype, IX86_BUILTIN_VEC_EXT_V4SF);
14198
14199   ftype = build_function_type_list (intSI_type_node, V4SI_type_node,
14200                                     integer_type_node, NULL_TREE);
14201   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v4si",
14202                ftype, IX86_BUILTIN_VEC_EXT_V4SI);
14203
14204   ftype = build_function_type_list (intHI_type_node, V8HI_type_node,
14205                                     integer_type_node, NULL_TREE);
14206   def_builtin (MASK_SSE, "__builtin_ia32_vec_ext_v8hi",
14207                ftype, IX86_BUILTIN_VEC_EXT_V8HI);
14208
14209   ftype = build_function_type_list (intHI_type_node, V4HI_type_node,
14210                                     integer_type_node, NULL_TREE);
14211   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_ext_v4hi",
14212                ftype, IX86_BUILTIN_VEC_EXT_V4HI);
14213
14214   ftype = build_function_type_list (intSI_type_node, V2SI_type_node,
14215                                     integer_type_node, NULL_TREE);
14216   def_builtin (MASK_MMX, "__builtin_ia32_vec_ext_v2si",
14217                ftype, IX86_BUILTIN_VEC_EXT_V2SI);
14218
14219   /* Access to the vec_set patterns.  */
14220   ftype = build_function_type_list (V8HI_type_node, V8HI_type_node,
14221                                     intHI_type_node,
14222                                     integer_type_node, NULL_TREE);
14223   def_builtin (MASK_SSE, "__builtin_ia32_vec_set_v8hi",
14224                ftype, IX86_BUILTIN_VEC_SET_V8HI);
14225   
14226   ftype = build_function_type_list (V4HI_type_node, V4HI_type_node,
14227                                     intHI_type_node,
14228                                     integer_type_node, NULL_TREE);
14229   def_builtin (MASK_SSE | MASK_3DNOW_A, "__builtin_ia32_vec_set_v4hi",
14230                ftype, IX86_BUILTIN_VEC_SET_V4HI);
14231 }
14232
14233 /* Errors in the source file can cause expand_expr to return const0_rtx
14234    where we expect a vector.  To avoid crashing, use one of the vector
14235    clear instructions.  */
14236 static rtx
14237 safe_vector_operand (rtx x, enum machine_mode mode)
14238 {
14239   if (x == const0_rtx)
14240     x = CONST0_RTX (mode);
14241   return x;
14242 }
14243
14244 /* Subroutine of ix86_expand_builtin to take care of binop insns.  */
14245
14246 static rtx
14247 ix86_expand_binop_builtin (enum insn_code icode, tree arglist, rtx target)
14248 {
14249   rtx pat, xops[3];
14250   tree arg0 = TREE_VALUE (arglist);
14251   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14252   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14253   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14254   enum machine_mode tmode = insn_data[icode].operand[0].mode;
14255   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
14256   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
14257
14258   if (VECTOR_MODE_P (mode0))
14259     op0 = safe_vector_operand (op0, mode0);
14260   if (VECTOR_MODE_P (mode1))
14261     op1 = safe_vector_operand (op1, mode1);
14262
14263   if (optimize || !target
14264       || GET_MODE (target) != tmode
14265       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14266     target = gen_reg_rtx (tmode);
14267
14268   if (GET_MODE (op1) == SImode && mode1 == TImode)
14269     {
14270       rtx x = gen_reg_rtx (V4SImode);
14271       emit_insn (gen_sse2_loadd (x, op1));
14272       op1 = gen_lowpart (TImode, x);
14273     }
14274
14275   /* The insn must want input operands in the same modes as the
14276      result.  */
14277   gcc_assert ((GET_MODE (op0) == mode0 || GET_MODE (op0) == VOIDmode)
14278               && (GET_MODE (op1) == mode1 || GET_MODE (op1) == VOIDmode));
14279
14280   if (!(*insn_data[icode].operand[1].predicate) (op0, mode0))
14281     op0 = copy_to_mode_reg (mode0, op0);
14282   if (!(*insn_data[icode].operand[2].predicate) (op1, mode1))
14283     op1 = copy_to_mode_reg (mode1, op1);
14284
14285   /* ??? Using ix86_fixup_binary_operands is problematic when
14286      we've got mismatched modes.  Fake it.  */
14287
14288   xops[0] = target;
14289   xops[1] = op0;
14290   xops[2] = op1;
14291
14292   if (tmode == mode0 && tmode == mode1)
14293     {
14294       target = ix86_fixup_binary_operands (UNKNOWN, tmode, xops);
14295       op0 = xops[1];
14296       op1 = xops[2];
14297     }
14298   else if (optimize || !ix86_binary_operator_ok (UNKNOWN, tmode, xops))
14299     {
14300       op0 = force_reg (mode0, op0);
14301       op1 = force_reg (mode1, op1);
14302       target = gen_reg_rtx (tmode);
14303     }
14304
14305   pat = GEN_FCN (icode) (target, op0, op1);
14306   if (! pat)
14307     return 0;
14308   emit_insn (pat);
14309   return target;
14310 }
14311
14312 /* Subroutine of ix86_expand_builtin to take care of stores.  */
14313
14314 static rtx
14315 ix86_expand_store_builtin (enum insn_code icode, tree arglist)
14316 {
14317   rtx pat;
14318   tree arg0 = TREE_VALUE (arglist);
14319   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14320   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14321   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14322   enum machine_mode mode0 = insn_data[icode].operand[0].mode;
14323   enum machine_mode mode1 = insn_data[icode].operand[1].mode;
14324
14325   if (VECTOR_MODE_P (mode1))
14326     op1 = safe_vector_operand (op1, mode1);
14327
14328   op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
14329   op1 = copy_to_mode_reg (mode1, op1);
14330
14331   pat = GEN_FCN (icode) (op0, op1);
14332   if (pat)
14333     emit_insn (pat);
14334   return 0;
14335 }
14336
14337 /* Subroutine of ix86_expand_builtin to take care of unop insns.  */
14338
14339 static rtx
14340 ix86_expand_unop_builtin (enum insn_code icode, tree arglist,
14341                           rtx target, int do_load)
14342 {
14343   rtx pat;
14344   tree arg0 = TREE_VALUE (arglist);
14345   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14346   enum machine_mode tmode = insn_data[icode].operand[0].mode;
14347   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
14348
14349   if (optimize || !target
14350       || GET_MODE (target) != tmode
14351       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14352     target = gen_reg_rtx (tmode);
14353   if (do_load)
14354     op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
14355   else
14356     {
14357       if (VECTOR_MODE_P (mode0))
14358         op0 = safe_vector_operand (op0, mode0);
14359
14360       if ((optimize && !register_operand (op0, mode0))
14361           || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14362         op0 = copy_to_mode_reg (mode0, op0);
14363     }
14364
14365   pat = GEN_FCN (icode) (target, op0);
14366   if (! pat)
14367     return 0;
14368   emit_insn (pat);
14369   return target;
14370 }
14371
14372 /* Subroutine of ix86_expand_builtin to take care of three special unop insns:
14373    sqrtss, rsqrtss, rcpss.  */
14374
14375 static rtx
14376 ix86_expand_unop1_builtin (enum insn_code icode, tree arglist, rtx target)
14377 {
14378   rtx pat;
14379   tree arg0 = TREE_VALUE (arglist);
14380   rtx op1, op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14381   enum machine_mode tmode = insn_data[icode].operand[0].mode;
14382   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
14383
14384   if (optimize || !target
14385       || GET_MODE (target) != tmode
14386       || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14387     target = gen_reg_rtx (tmode);
14388
14389   if (VECTOR_MODE_P (mode0))
14390     op0 = safe_vector_operand (op0, mode0);
14391
14392   if ((optimize && !register_operand (op0, mode0))
14393       || ! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14394     op0 = copy_to_mode_reg (mode0, op0);
14395
14396   op1 = op0;
14397   if (! (*insn_data[icode].operand[2].predicate) (op1, mode0))
14398     op1 = copy_to_mode_reg (mode0, op1);
14399
14400   pat = GEN_FCN (icode) (target, op0, op1);
14401   if (! pat)
14402     return 0;
14403   emit_insn (pat);
14404   return target;
14405 }
14406
14407 /* Subroutine of ix86_expand_builtin to take care of comparison insns.  */
14408
14409 static rtx
14410 ix86_expand_sse_compare (const struct builtin_description *d, tree arglist,
14411                          rtx target)
14412 {
14413   rtx pat;
14414   tree arg0 = TREE_VALUE (arglist);
14415   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14416   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14417   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14418   rtx op2;
14419   enum machine_mode tmode = insn_data[d->icode].operand[0].mode;
14420   enum machine_mode mode0 = insn_data[d->icode].operand[1].mode;
14421   enum machine_mode mode1 = insn_data[d->icode].operand[2].mode;
14422   enum rtx_code comparison = d->comparison;
14423
14424   if (VECTOR_MODE_P (mode0))
14425     op0 = safe_vector_operand (op0, mode0);
14426   if (VECTOR_MODE_P (mode1))
14427     op1 = safe_vector_operand (op1, mode1);
14428
14429   /* Swap operands if we have a comparison that isn't available in
14430      hardware.  */
14431   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
14432     {
14433       rtx tmp = gen_reg_rtx (mode1);
14434       emit_move_insn (tmp, op1);
14435       op1 = op0;
14436       op0 = tmp;
14437     }
14438
14439   if (optimize || !target
14440       || GET_MODE (target) != tmode
14441       || ! (*insn_data[d->icode].operand[0].predicate) (target, tmode))
14442     target = gen_reg_rtx (tmode);
14443
14444   if ((optimize && !register_operand (op0, mode0))
14445       || ! (*insn_data[d->icode].operand[1].predicate) (op0, mode0))
14446     op0 = copy_to_mode_reg (mode0, op0);
14447   if ((optimize && !register_operand (op1, mode1))
14448       || ! (*insn_data[d->icode].operand[2].predicate) (op1, mode1))
14449     op1 = copy_to_mode_reg (mode1, op1);
14450
14451   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
14452   pat = GEN_FCN (d->icode) (target, op0, op1, op2);
14453   if (! pat)
14454     return 0;
14455   emit_insn (pat);
14456   return target;
14457 }
14458
14459 /* Subroutine of ix86_expand_builtin to take care of comi insns.  */
14460
14461 static rtx
14462 ix86_expand_sse_comi (const struct builtin_description *d, tree arglist,
14463                       rtx target)
14464 {
14465   rtx pat;
14466   tree arg0 = TREE_VALUE (arglist);
14467   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14468   rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14469   rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14470   rtx op2;
14471   enum machine_mode mode0 = insn_data[d->icode].operand[0].mode;
14472   enum machine_mode mode1 = insn_data[d->icode].operand[1].mode;
14473   enum rtx_code comparison = d->comparison;
14474
14475   if (VECTOR_MODE_P (mode0))
14476     op0 = safe_vector_operand (op0, mode0);
14477   if (VECTOR_MODE_P (mode1))
14478     op1 = safe_vector_operand (op1, mode1);
14479
14480   /* Swap operands if we have a comparison that isn't available in
14481      hardware.  */
14482   if (d->flag & BUILTIN_DESC_SWAP_OPERANDS)
14483     {
14484       rtx tmp = op1;
14485       op1 = op0;
14486       op0 = tmp;
14487     }
14488
14489   target = gen_reg_rtx (SImode);
14490   emit_move_insn (target, const0_rtx);
14491   target = gen_rtx_SUBREG (QImode, target, 0);
14492
14493   if ((optimize && !register_operand (op0, mode0))
14494       || !(*insn_data[d->icode].operand[0].predicate) (op0, mode0))
14495     op0 = copy_to_mode_reg (mode0, op0);
14496   if ((optimize && !register_operand (op1, mode1))
14497       || !(*insn_data[d->icode].operand[1].predicate) (op1, mode1))
14498     op1 = copy_to_mode_reg (mode1, op1);
14499
14500   op2 = gen_rtx_fmt_ee (comparison, mode0, op0, op1);
14501   pat = GEN_FCN (d->icode) (op0, op1);
14502   if (! pat)
14503     return 0;
14504   emit_insn (pat);
14505   emit_insn (gen_rtx_SET (VOIDmode,
14506                           gen_rtx_STRICT_LOW_PART (VOIDmode, target),
14507                           gen_rtx_fmt_ee (comparison, QImode,
14508                                           SET_DEST (pat),
14509                                           const0_rtx)));
14510
14511   return SUBREG_REG (target);
14512 }
14513
14514 /* Return the integer constant in ARG.  Constrain it to be in the range
14515    of the subparts of VEC_TYPE; issue an error if not.  */
14516
14517 static int
14518 get_element_number (tree vec_type, tree arg)
14519 {
14520   unsigned HOST_WIDE_INT elt, max = TYPE_VECTOR_SUBPARTS (vec_type) - 1;
14521
14522   if (!host_integerp (arg, 1)
14523       || (elt = tree_low_cst (arg, 1), elt > max))
14524     {
14525       error ("selector must be an integer constant in the range 0..%i", max);
14526       return 0;
14527     }
14528
14529   return elt;
14530 }
14531
14532 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
14533    ix86_expand_vector_init.  We DO have language-level syntax for this, in
14534    the form of  (type){ init-list }.  Except that since we can't place emms
14535    instructions from inside the compiler, we can't allow the use of MMX
14536    registers unless the user explicitly asks for it.  So we do *not* define
14537    vec_set/vec_extract/vec_init patterns for MMX modes in mmx.md.  Instead
14538    we have builtins invoked by mmintrin.h that gives us license to emit 
14539    these sorts of instructions.  */
14540
14541 static rtx
14542 ix86_expand_vec_init_builtin (tree type, tree arglist, rtx target)
14543 {
14544   enum machine_mode tmode = TYPE_MODE (type);
14545   enum machine_mode inner_mode = GET_MODE_INNER (tmode);
14546   int i, n_elt = GET_MODE_NUNITS (tmode);
14547   rtvec v = rtvec_alloc (n_elt);
14548
14549   gcc_assert (VECTOR_MODE_P (tmode));
14550
14551   for (i = 0; i < n_elt; ++i, arglist = TREE_CHAIN (arglist))
14552     {
14553       rtx x = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
14554       RTVEC_ELT (v, i) = gen_lowpart (inner_mode, x);
14555     }
14556
14557   gcc_assert (arglist == NULL);
14558
14559   if (!target || !register_operand (target, tmode))
14560     target = gen_reg_rtx (tmode);
14561
14562   ix86_expand_vector_init (true, target, gen_rtx_PARALLEL (tmode, v));
14563   return target;
14564 }
14565
14566 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
14567    ix86_expand_vector_extract.  They would be redundant (for non-MMX) if we
14568    had a language-level syntax for referencing vector elements.  */
14569
14570 static rtx
14571 ix86_expand_vec_ext_builtin (tree arglist, rtx target)
14572 {
14573   enum machine_mode tmode, mode0;
14574   tree arg0, arg1;
14575   int elt;
14576   rtx op0;
14577
14578   arg0 = TREE_VALUE (arglist);
14579   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14580
14581   op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14582   elt = get_element_number (TREE_TYPE (arg0), arg1);
14583
14584   tmode = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
14585   mode0 = TYPE_MODE (TREE_TYPE (arg0));
14586   gcc_assert (VECTOR_MODE_P (mode0));
14587
14588   op0 = force_reg (mode0, op0);
14589
14590   if (optimize || !target || !register_operand (target, tmode))
14591     target = gen_reg_rtx (tmode);
14592
14593   ix86_expand_vector_extract (true, target, op0, elt);
14594
14595   return target;
14596 }
14597
14598 /* A subroutine of ix86_expand_builtin.  These builtins are a wrapper around
14599    ix86_expand_vector_set.  They would be redundant (for non-MMX) if we had
14600    a language-level syntax for referencing vector elements.  */
14601
14602 static rtx
14603 ix86_expand_vec_set_builtin (tree arglist)
14604 {
14605   enum machine_mode tmode, mode1;
14606   tree arg0, arg1, arg2;
14607   int elt;
14608   rtx op0, op1;
14609
14610   arg0 = TREE_VALUE (arglist);
14611   arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14612   arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14613
14614   tmode = TYPE_MODE (TREE_TYPE (arg0));
14615   mode1 = TYPE_MODE (TREE_TYPE (TREE_TYPE (arg0)));
14616   gcc_assert (VECTOR_MODE_P (tmode));
14617
14618   op0 = expand_expr (arg0, NULL_RTX, tmode, 0);
14619   op1 = expand_expr (arg1, NULL_RTX, mode1, 0);
14620   elt = get_element_number (TREE_TYPE (arg0), arg2);
14621
14622   if (GET_MODE (op1) != mode1 && GET_MODE (op1) != VOIDmode)
14623     op1 = convert_modes (mode1, GET_MODE (op1), op1, true);
14624
14625   op0 = force_reg (tmode, op0);
14626   op1 = force_reg (mode1, op1);
14627
14628   ix86_expand_vector_set (true, op0, op1, elt);
14629
14630   return op0;
14631 }
14632
14633 /* Expand an expression EXP that calls a built-in function,
14634    with result going to TARGET if that's convenient
14635    (and in mode MODE if that's convenient).
14636    SUBTARGET may be used as the target for computing one of EXP's operands.
14637    IGNORE is nonzero if the value is to be ignored.  */
14638
14639 static rtx
14640 ix86_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED,
14641                      enum machine_mode mode ATTRIBUTE_UNUSED,
14642                      int ignore ATTRIBUTE_UNUSED)
14643 {
14644   const struct builtin_description *d;
14645   size_t i;
14646   enum insn_code icode;
14647   tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
14648   tree arglist = TREE_OPERAND (exp, 1);
14649   tree arg0, arg1, arg2;
14650   rtx op0, op1, op2, pat;
14651   enum machine_mode tmode, mode0, mode1, mode2;
14652   unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
14653
14654   switch (fcode)
14655     {
14656     case IX86_BUILTIN_EMMS:
14657       emit_insn (gen_mmx_emms ());
14658       return 0;
14659
14660     case IX86_BUILTIN_SFENCE:
14661       emit_insn (gen_sse_sfence ());
14662       return 0;
14663
14664     case IX86_BUILTIN_MASKMOVQ:
14665     case IX86_BUILTIN_MASKMOVDQU:
14666       icode = (fcode == IX86_BUILTIN_MASKMOVQ
14667                ? CODE_FOR_mmx_maskmovq
14668                : CODE_FOR_sse2_maskmovdqu);
14669       /* Note the arg order is different from the operand order.  */
14670       arg1 = TREE_VALUE (arglist);
14671       arg2 = TREE_VALUE (TREE_CHAIN (arglist));
14672       arg0 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14673       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14674       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14675       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14676       mode0 = insn_data[icode].operand[0].mode;
14677       mode1 = insn_data[icode].operand[1].mode;
14678       mode2 = insn_data[icode].operand[2].mode;
14679
14680       op0 = force_reg (Pmode, op0);
14681       op0 = gen_rtx_MEM (mode1, op0);
14682
14683       if (! (*insn_data[icode].operand[0].predicate) (op0, mode0))
14684         op0 = copy_to_mode_reg (mode0, op0);
14685       if (! (*insn_data[icode].operand[1].predicate) (op1, mode1))
14686         op1 = copy_to_mode_reg (mode1, op1);
14687       if (! (*insn_data[icode].operand[2].predicate) (op2, mode2))
14688         op2 = copy_to_mode_reg (mode2, op2);
14689       pat = GEN_FCN (icode) (op0, op1, op2);
14690       if (! pat)
14691         return 0;
14692       emit_insn (pat);
14693       return 0;
14694
14695     case IX86_BUILTIN_SQRTSS:
14696       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmsqrtv4sf2, arglist, target);
14697     case IX86_BUILTIN_RSQRTSS:
14698       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrsqrtv4sf2, arglist, target);
14699     case IX86_BUILTIN_RCPSS:
14700       return ix86_expand_unop1_builtin (CODE_FOR_sse_vmrcpv4sf2, arglist, target);
14701
14702     case IX86_BUILTIN_LOADUPS:
14703       return ix86_expand_unop_builtin (CODE_FOR_sse_movups, arglist, target, 1);
14704
14705     case IX86_BUILTIN_STOREUPS:
14706       return ix86_expand_store_builtin (CODE_FOR_sse_movups, arglist);
14707
14708     case IX86_BUILTIN_LOADHPS:
14709     case IX86_BUILTIN_LOADLPS:
14710     case IX86_BUILTIN_LOADHPD:
14711     case IX86_BUILTIN_LOADLPD:
14712       icode = (fcode == IX86_BUILTIN_LOADHPS ? CODE_FOR_sse_loadhps
14713                : fcode == IX86_BUILTIN_LOADLPS ? CODE_FOR_sse_loadlps
14714                : fcode == IX86_BUILTIN_LOADHPD ? CODE_FOR_sse2_loadhpd
14715                : CODE_FOR_sse2_loadlpd);
14716       arg0 = TREE_VALUE (arglist);
14717       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14718       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14719       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14720       tmode = insn_data[icode].operand[0].mode;
14721       mode0 = insn_data[icode].operand[1].mode;
14722       mode1 = insn_data[icode].operand[2].mode;
14723
14724       op0 = force_reg (mode0, op0);
14725       op1 = gen_rtx_MEM (mode1, copy_to_mode_reg (Pmode, op1));
14726       if (optimize || target == 0
14727           || GET_MODE (target) != tmode
14728           || !register_operand (target, tmode))
14729         target = gen_reg_rtx (tmode);
14730       pat = GEN_FCN (icode) (target, op0, op1);
14731       if (! pat)
14732         return 0;
14733       emit_insn (pat);
14734       return target;
14735
14736     case IX86_BUILTIN_STOREHPS:
14737     case IX86_BUILTIN_STORELPS:
14738       icode = (fcode == IX86_BUILTIN_STOREHPS ? CODE_FOR_sse_storehps
14739                : CODE_FOR_sse_storelps);
14740       arg0 = TREE_VALUE (arglist);
14741       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14742       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14743       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14744       mode0 = insn_data[icode].operand[0].mode;
14745       mode1 = insn_data[icode].operand[1].mode;
14746
14747       op0 = gen_rtx_MEM (mode0, copy_to_mode_reg (Pmode, op0));
14748       op1 = force_reg (mode1, op1);
14749
14750       pat = GEN_FCN (icode) (op0, op1);
14751       if (! pat)
14752         return 0;
14753       emit_insn (pat);
14754       return const0_rtx;
14755
14756     case IX86_BUILTIN_MOVNTPS:
14757       return ix86_expand_store_builtin (CODE_FOR_sse_movntv4sf, arglist);
14758     case IX86_BUILTIN_MOVNTQ:
14759       return ix86_expand_store_builtin (CODE_FOR_sse_movntdi, arglist);
14760
14761     case IX86_BUILTIN_LDMXCSR:
14762       op0 = expand_expr (TREE_VALUE (arglist), NULL_RTX, VOIDmode, 0);
14763       target = assign_386_stack_local (SImode, 0);
14764       emit_move_insn (target, op0);
14765       emit_insn (gen_sse_ldmxcsr (target));
14766       return 0;
14767
14768     case IX86_BUILTIN_STMXCSR:
14769       target = assign_386_stack_local (SImode, 0);
14770       emit_insn (gen_sse_stmxcsr (target));
14771       return copy_to_mode_reg (SImode, target);
14772
14773     case IX86_BUILTIN_SHUFPS:
14774     case IX86_BUILTIN_SHUFPD:
14775       icode = (fcode == IX86_BUILTIN_SHUFPS
14776                ? CODE_FOR_sse_shufps
14777                : CODE_FOR_sse2_shufpd);
14778       arg0 = TREE_VALUE (arglist);
14779       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14780       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14781       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14782       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14783       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14784       tmode = insn_data[icode].operand[0].mode;
14785       mode0 = insn_data[icode].operand[1].mode;
14786       mode1 = insn_data[icode].operand[2].mode;
14787       mode2 = insn_data[icode].operand[3].mode;
14788
14789       if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
14790         op0 = copy_to_mode_reg (mode0, op0);
14791       if ((optimize && !register_operand (op1, mode1))
14792           || !(*insn_data[icode].operand[2].predicate) (op1, mode1))
14793         op1 = copy_to_mode_reg (mode1, op1);
14794       if (! (*insn_data[icode].operand[3].predicate) (op2, mode2))
14795         {
14796           /* @@@ better error message */
14797           error ("mask must be an immediate");
14798           return gen_reg_rtx (tmode);
14799         }
14800       if (optimize || target == 0
14801           || GET_MODE (target) != tmode
14802           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14803         target = gen_reg_rtx (tmode);
14804       pat = GEN_FCN (icode) (target, op0, op1, op2);
14805       if (! pat)
14806         return 0;
14807       emit_insn (pat);
14808       return target;
14809
14810     case IX86_BUILTIN_PSHUFW:
14811     case IX86_BUILTIN_PSHUFD:
14812     case IX86_BUILTIN_PSHUFHW:
14813     case IX86_BUILTIN_PSHUFLW:
14814       icode = (  fcode == IX86_BUILTIN_PSHUFHW ? CODE_FOR_sse2_pshufhw
14815                : fcode == IX86_BUILTIN_PSHUFLW ? CODE_FOR_sse2_pshuflw
14816                : fcode == IX86_BUILTIN_PSHUFD ? CODE_FOR_sse2_pshufd
14817                : CODE_FOR_mmx_pshufw);
14818       arg0 = TREE_VALUE (arglist);
14819       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14820       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14821       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14822       tmode = insn_data[icode].operand[0].mode;
14823       mode1 = insn_data[icode].operand[1].mode;
14824       mode2 = insn_data[icode].operand[2].mode;
14825
14826       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
14827         op0 = copy_to_mode_reg (mode1, op0);
14828       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
14829         {
14830           /* @@@ better error message */
14831           error ("mask must be an immediate");
14832           return const0_rtx;
14833         }
14834       if (target == 0
14835           || GET_MODE (target) != tmode
14836           || ! (*insn_data[icode].operand[0].predicate) (target, tmode))
14837         target = gen_reg_rtx (tmode);
14838       pat = GEN_FCN (icode) (target, op0, op1);
14839       if (! pat)
14840         return 0;
14841       emit_insn (pat);
14842       return target;
14843
14844     case IX86_BUILTIN_PSLLDQI128:
14845     case IX86_BUILTIN_PSRLDQI128:
14846       icode = (  fcode == IX86_BUILTIN_PSLLDQI128 ? CODE_FOR_sse2_ashlti3
14847                : CODE_FOR_sse2_lshrti3);
14848       arg0 = TREE_VALUE (arglist);
14849       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14850       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14851       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14852       tmode = insn_data[icode].operand[0].mode;
14853       mode1 = insn_data[icode].operand[1].mode;
14854       mode2 = insn_data[icode].operand[2].mode;
14855
14856       if (! (*insn_data[icode].operand[1].predicate) (op0, mode1))
14857         {
14858           op0 = copy_to_reg (op0);
14859           op0 = simplify_gen_subreg (mode1, op0, GET_MODE (op0), 0);
14860         }
14861       if (! (*insn_data[icode].operand[2].predicate) (op1, mode2))
14862         {
14863           error ("shift must be an immediate");
14864           return const0_rtx;
14865         }
14866       target = gen_reg_rtx (V2DImode);
14867       pat = GEN_FCN (icode) (simplify_gen_subreg (tmode, target, V2DImode, 0), op0, op1);
14868       if (! pat)
14869         return 0;
14870       emit_insn (pat);
14871       return target;
14872
14873     case IX86_BUILTIN_FEMMS:
14874       emit_insn (gen_mmx_femms ());
14875       return NULL_RTX;
14876
14877     case IX86_BUILTIN_PAVGUSB:
14878       return ix86_expand_binop_builtin (CODE_FOR_mmx_uavgv8qi3, arglist, target);
14879
14880     case IX86_BUILTIN_PF2ID:
14881       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2id, arglist, target, 0);
14882
14883     case IX86_BUILTIN_PFACC:
14884       return ix86_expand_binop_builtin (CODE_FOR_mmx_haddv2sf3, arglist, target);
14885
14886     case IX86_BUILTIN_PFADD:
14887      return ix86_expand_binop_builtin (CODE_FOR_mmx_addv2sf3, arglist, target);
14888
14889     case IX86_BUILTIN_PFCMPEQ:
14890       return ix86_expand_binop_builtin (CODE_FOR_mmx_eqv2sf3, arglist, target);
14891
14892     case IX86_BUILTIN_PFCMPGE:
14893       return ix86_expand_binop_builtin (CODE_FOR_mmx_gev2sf3, arglist, target);
14894
14895     case IX86_BUILTIN_PFCMPGT:
14896       return ix86_expand_binop_builtin (CODE_FOR_mmx_gtv2sf3, arglist, target);
14897
14898     case IX86_BUILTIN_PFMAX:
14899       return ix86_expand_binop_builtin (CODE_FOR_mmx_smaxv2sf3, arglist, target);
14900
14901     case IX86_BUILTIN_PFMIN:
14902       return ix86_expand_binop_builtin (CODE_FOR_mmx_sminv2sf3, arglist, target);
14903
14904     case IX86_BUILTIN_PFMUL:
14905       return ix86_expand_binop_builtin (CODE_FOR_mmx_mulv2sf3, arglist, target);
14906
14907     case IX86_BUILTIN_PFRCP:
14908       return ix86_expand_unop_builtin (CODE_FOR_mmx_rcpv2sf2, arglist, target, 0);
14909
14910     case IX86_BUILTIN_PFRCPIT1:
14911       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit1v2sf3, arglist, target);
14912
14913     case IX86_BUILTIN_PFRCPIT2:
14914       return ix86_expand_binop_builtin (CODE_FOR_mmx_rcpit2v2sf3, arglist, target);
14915
14916     case IX86_BUILTIN_PFRSQIT1:
14917       return ix86_expand_binop_builtin (CODE_FOR_mmx_rsqit1v2sf3, arglist, target);
14918
14919     case IX86_BUILTIN_PFRSQRT:
14920       return ix86_expand_unop_builtin (CODE_FOR_mmx_rsqrtv2sf2, arglist, target, 0);
14921
14922     case IX86_BUILTIN_PFSUB:
14923       return ix86_expand_binop_builtin (CODE_FOR_mmx_subv2sf3, arglist, target);
14924
14925     case IX86_BUILTIN_PFSUBR:
14926       return ix86_expand_binop_builtin (CODE_FOR_mmx_subrv2sf3, arglist, target);
14927
14928     case IX86_BUILTIN_PI2FD:
14929       return ix86_expand_unop_builtin (CODE_FOR_mmx_floatv2si2, arglist, target, 0);
14930
14931     case IX86_BUILTIN_PMULHRW:
14932       return ix86_expand_binop_builtin (CODE_FOR_mmx_pmulhrwv4hi3, arglist, target);
14933
14934     case IX86_BUILTIN_PF2IW:
14935       return ix86_expand_unop_builtin (CODE_FOR_mmx_pf2iw, arglist, target, 0);
14936
14937     case IX86_BUILTIN_PFNACC:
14938       return ix86_expand_binop_builtin (CODE_FOR_mmx_hsubv2sf3, arglist, target);
14939
14940     case IX86_BUILTIN_PFPNACC:
14941       return ix86_expand_binop_builtin (CODE_FOR_mmx_addsubv2sf3, arglist, target);
14942
14943     case IX86_BUILTIN_PI2FW:
14944       return ix86_expand_unop_builtin (CODE_FOR_mmx_pi2fw, arglist, target, 0);
14945
14946     case IX86_BUILTIN_PSWAPDSI:
14947       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2si2, arglist, target, 0);
14948
14949     case IX86_BUILTIN_PSWAPDSF:
14950       return ix86_expand_unop_builtin (CODE_FOR_mmx_pswapdv2sf2, arglist, target, 0);
14951
14952     case IX86_BUILTIN_SQRTSD:
14953       return ix86_expand_unop1_builtin (CODE_FOR_sse2_vmsqrtv2df2, arglist, target);
14954     case IX86_BUILTIN_LOADUPD:
14955       return ix86_expand_unop_builtin (CODE_FOR_sse2_movupd, arglist, target, 1);
14956     case IX86_BUILTIN_STOREUPD:
14957       return ix86_expand_store_builtin (CODE_FOR_sse2_movupd, arglist);
14958
14959     case IX86_BUILTIN_MFENCE:
14960         emit_insn (gen_sse2_mfence ());
14961         return 0;
14962     case IX86_BUILTIN_LFENCE:
14963         emit_insn (gen_sse2_lfence ());
14964         return 0;
14965
14966     case IX86_BUILTIN_CLFLUSH:
14967         arg0 = TREE_VALUE (arglist);
14968         op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14969         icode = CODE_FOR_sse2_clflush;
14970         if (! (*insn_data[icode].operand[0].predicate) (op0, Pmode))
14971             op0 = copy_to_mode_reg (Pmode, op0);
14972
14973         emit_insn (gen_sse2_clflush (op0));
14974         return 0;
14975
14976     case IX86_BUILTIN_MOVNTPD:
14977       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2df, arglist);
14978     case IX86_BUILTIN_MOVNTDQ:
14979       return ix86_expand_store_builtin (CODE_FOR_sse2_movntv2di, arglist);
14980     case IX86_BUILTIN_MOVNTI:
14981       return ix86_expand_store_builtin (CODE_FOR_sse2_movntsi, arglist);
14982
14983     case IX86_BUILTIN_LOADDQU:
14984       return ix86_expand_unop_builtin (CODE_FOR_sse2_movdqu, arglist, target, 1);
14985     case IX86_BUILTIN_STOREDQU:
14986       return ix86_expand_store_builtin (CODE_FOR_sse2_movdqu, arglist);
14987
14988     case IX86_BUILTIN_MONITOR:
14989       arg0 = TREE_VALUE (arglist);
14990       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
14991       arg2 = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist)));
14992       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
14993       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
14994       op2 = expand_expr (arg2, NULL_RTX, VOIDmode, 0);
14995       if (!REG_P (op0))
14996         op0 = copy_to_mode_reg (SImode, op0);
14997       if (!REG_P (op1))
14998         op1 = copy_to_mode_reg (SImode, op1);
14999       if (!REG_P (op2))
15000         op2 = copy_to_mode_reg (SImode, op2);
15001       emit_insn (gen_sse3_monitor (op0, op1, op2));
15002       return 0;
15003
15004     case IX86_BUILTIN_MWAIT:
15005       arg0 = TREE_VALUE (arglist);
15006       arg1 = TREE_VALUE (TREE_CHAIN (arglist));
15007       op0 = expand_expr (arg0, NULL_RTX, VOIDmode, 0);
15008       op1 = expand_expr (arg1, NULL_RTX, VOIDmode, 0);
15009       if (!REG_P (op0))
15010         op0 = copy_to_mode_reg (SImode, op0);
15011       if (!REG_P (op1))
15012         op1 = copy_to_mode_reg (SImode, op1);
15013       emit_insn (gen_sse3_mwait (op0, op1));
15014       return 0;
15015
15016     case IX86_BUILTIN_LDDQU:
15017       return ix86_expand_unop_builtin (CODE_FOR_sse3_lddqu, arglist,
15018                                        target, 1);
15019
15020     case IX86_BUILTIN_VEC_INIT_V2SI:
15021     case IX86_BUILTIN_VEC_INIT_V4HI:
15022     case IX86_BUILTIN_VEC_INIT_V8QI:
15023       return ix86_expand_vec_init_builtin (TREE_TYPE (exp), arglist, target);
15024
15025     case IX86_BUILTIN_VEC_EXT_V2DF:
15026     case IX86_BUILTIN_VEC_EXT_V2DI:
15027     case IX86_BUILTIN_VEC_EXT_V4SF:
15028     case IX86_BUILTIN_VEC_EXT_V4SI:
15029     case IX86_BUILTIN_VEC_EXT_V8HI:
15030     case IX86_BUILTIN_VEC_EXT_V2SI:
15031     case IX86_BUILTIN_VEC_EXT_V4HI:
15032       return ix86_expand_vec_ext_builtin (arglist, target);
15033
15034     case IX86_BUILTIN_VEC_SET_V8HI:
15035     case IX86_BUILTIN_VEC_SET_V4HI:
15036       return ix86_expand_vec_set_builtin (arglist);
15037
15038     default:
15039       break;
15040     }
15041
15042   for (i = 0, d = bdesc_2arg; i < ARRAY_SIZE (bdesc_2arg); i++, d++)
15043     if (d->code == fcode)
15044       {
15045         /* Compares are treated specially.  */
15046         if (d->icode == CODE_FOR_sse_maskcmpv4sf3
15047             || d->icode == CODE_FOR_sse_vmmaskcmpv4sf3
15048             || d->icode == CODE_FOR_sse2_maskcmpv2df3
15049             || d->icode == CODE_FOR_sse2_vmmaskcmpv2df3)
15050           return ix86_expand_sse_compare (d, arglist, target);
15051
15052         return ix86_expand_binop_builtin (d->icode, arglist, target);
15053       }
15054
15055   for (i = 0, d = bdesc_1arg; i < ARRAY_SIZE (bdesc_1arg); i++, d++)
15056     if (d->code == fcode)
15057       return ix86_expand_unop_builtin (d->icode, arglist, target, 0);
15058
15059   for (i = 0, d = bdesc_comi; i < ARRAY_SIZE (bdesc_comi); i++, d++)
15060     if (d->code == fcode)
15061       return ix86_expand_sse_comi (d, arglist, target);
15062
15063   gcc_unreachable ();
15064 }
15065
15066 /* Store OPERAND to the memory after reload is completed.  This means
15067    that we can't easily use assign_stack_local.  */
15068 rtx
15069 ix86_force_to_memory (enum machine_mode mode, rtx operand)
15070 {
15071   rtx result;
15072   
15073   gcc_assert (reload_completed);
15074   if (TARGET_RED_ZONE)
15075     {
15076       result = gen_rtx_MEM (mode,
15077                             gen_rtx_PLUS (Pmode,
15078                                           stack_pointer_rtx,
15079                                           GEN_INT (-RED_ZONE_SIZE)));
15080       emit_move_insn (result, operand);
15081     }
15082   else if (!TARGET_RED_ZONE && TARGET_64BIT)
15083     {
15084       switch (mode)
15085         {
15086         case HImode:
15087         case SImode:
15088           operand = gen_lowpart (DImode, operand);
15089           /* FALLTHRU */
15090         case DImode:
15091           emit_insn (
15092                       gen_rtx_SET (VOIDmode,
15093                                    gen_rtx_MEM (DImode,
15094                                                 gen_rtx_PRE_DEC (DImode,
15095                                                         stack_pointer_rtx)),
15096                                    operand));
15097           break;
15098         default:
15099           gcc_unreachable ();
15100         }
15101       result = gen_rtx_MEM (mode, stack_pointer_rtx);
15102     }
15103   else
15104     {
15105       switch (mode)
15106         {
15107         case DImode:
15108           {
15109             rtx operands[2];
15110             split_di (&operand, 1, operands, operands + 1);
15111             emit_insn (
15112                         gen_rtx_SET (VOIDmode,
15113                                      gen_rtx_MEM (SImode,
15114                                                   gen_rtx_PRE_DEC (Pmode,
15115                                                         stack_pointer_rtx)),
15116                                      operands[1]));
15117             emit_insn (
15118                         gen_rtx_SET (VOIDmode,
15119                                      gen_rtx_MEM (SImode,
15120                                                   gen_rtx_PRE_DEC (Pmode,
15121                                                         stack_pointer_rtx)),
15122                                      operands[0]));
15123           }
15124           break;
15125         case HImode:
15126           /* It is better to store HImodes as SImodes.  */
15127           if (!TARGET_PARTIAL_REG_STALL)
15128             operand = gen_lowpart (SImode, operand);
15129           /* FALLTHRU */
15130         case SImode:
15131           emit_insn (
15132                       gen_rtx_SET (VOIDmode,
15133                                    gen_rtx_MEM (GET_MODE (operand),
15134                                                 gen_rtx_PRE_DEC (SImode,
15135                                                         stack_pointer_rtx)),
15136                                    operand));
15137           break;
15138         default:
15139           gcc_unreachable ();
15140         }
15141       result = gen_rtx_MEM (mode, stack_pointer_rtx);
15142     }
15143   return result;
15144 }
15145
15146 /* Free operand from the memory.  */
15147 void
15148 ix86_free_from_memory (enum machine_mode mode)
15149 {
15150   if (!TARGET_RED_ZONE)
15151     {
15152       int size;
15153
15154       if (mode == DImode || TARGET_64BIT)
15155         size = 8;
15156       else if (mode == HImode && TARGET_PARTIAL_REG_STALL)
15157         size = 2;
15158       else
15159         size = 4;
15160       /* Use LEA to deallocate stack space.  In peephole2 it will be converted
15161          to pop or add instruction if registers are available.  */
15162       emit_insn (gen_rtx_SET (VOIDmode, stack_pointer_rtx,
15163                               gen_rtx_PLUS (Pmode, stack_pointer_rtx,
15164                                             GEN_INT (size))));
15165     }
15166 }
15167
15168 /* Put float CONST_DOUBLE in the constant pool instead of fp regs.
15169    QImode must go into class Q_REGS.
15170    Narrow ALL_REGS to GENERAL_REGS.  This supports allowing movsf and
15171    movdf to do mem-to-mem moves through integer regs.  */
15172 enum reg_class
15173 ix86_preferred_reload_class (rtx x, enum reg_class class)
15174 {
15175   /* We're only allowed to return a subclass of CLASS.  Many of the 
15176      following checks fail for NO_REGS, so eliminate that early.  */
15177   if (class == NO_REGS)
15178     return NO_REGS;
15179
15180   /* All classes can load zeros.  */
15181   if (x == CONST0_RTX (GET_MODE (x)))
15182     return class;
15183
15184   /* Floating-point constants need more complex checks.  */
15185   if (GET_CODE (x) == CONST_DOUBLE && GET_MODE (x) != VOIDmode)
15186     {
15187       /* General regs can load everything.  */
15188       if (reg_class_subset_p (class, GENERAL_REGS))
15189         return class;
15190
15191       /* Floats can load 0 and 1 plus some others.  Note that we eliminated
15192          zero above.  We only want to wind up preferring 80387 registers if
15193          we plan on doing computation with them.  */
15194       if (TARGET_80387
15195           && (TARGET_MIX_SSE_I387 
15196               || !(TARGET_SSE_MATH && SSE_FLOAT_MODE_P (GET_MODE (x))))
15197           && standard_80387_constant_p (x))
15198         {
15199           /* Limit class to non-sse.  */
15200           if (class == FLOAT_SSE_REGS)
15201             return FLOAT_REGS;
15202           if (class == FP_TOP_SSE_REGS)
15203             return FP_TOP_REG;
15204           if (class == FP_SECOND_SSE_REGS)
15205             return FP_SECOND_REG;
15206           if (class == FLOAT_INT_REGS || class == FLOAT_REGS)
15207             return class;
15208         }
15209
15210       return NO_REGS;
15211     }
15212   if (MAYBE_MMX_CLASS_P (class) && CONSTANT_P (x))
15213     return NO_REGS;
15214   if (MAYBE_SSE_CLASS_P (class) && CONSTANT_P (x))
15215     return NO_REGS;
15216
15217   /* Generally when we see PLUS here, it's the function invariant
15218      (plus soft-fp const_int).  Which can only be computed into general
15219      regs.  */
15220   if (GET_CODE (x) == PLUS)
15221     return reg_class_subset_p (class, GENERAL_REGS) ? class : NO_REGS;
15222
15223   /* QImode constants are easy to load, but non-constant QImode data
15224      must go into Q_REGS.  */
15225   if (GET_MODE (x) == QImode && !CONSTANT_P (x))
15226     {
15227       if (reg_class_subset_p (class, Q_REGS))
15228         return class;
15229       if (reg_class_subset_p (Q_REGS, class))
15230         return Q_REGS;
15231       return NO_REGS;
15232     }
15233
15234   return class;
15235 }
15236
15237 /* If we are copying between general and FP registers, we need a memory
15238    location. The same is true for SSE and MMX registers.
15239
15240    The macro can't work reliably when one of the CLASSES is class containing
15241    registers from multiple units (SSE, MMX, integer).  We avoid this by never
15242    combining those units in single alternative in the machine description.
15243    Ensure that this constraint holds to avoid unexpected surprises.
15244
15245    When STRICT is false, we are being called from REGISTER_MOVE_COST, so do not
15246    enforce these sanity checks.  */
15247
15248 int
15249 ix86_secondary_memory_needed (enum reg_class class1, enum reg_class class2,
15250                               enum machine_mode mode, int strict)
15251 {
15252   if (MAYBE_FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class1)
15253       || MAYBE_FLOAT_CLASS_P (class2) != FLOAT_CLASS_P (class2)
15254       || MAYBE_SSE_CLASS_P (class1) != SSE_CLASS_P (class1)
15255       || MAYBE_SSE_CLASS_P (class2) != SSE_CLASS_P (class2)
15256       || MAYBE_MMX_CLASS_P (class1) != MMX_CLASS_P (class1)
15257       || MAYBE_MMX_CLASS_P (class2) != MMX_CLASS_P (class2))
15258     {
15259       gcc_assert (!strict);
15260       return true;
15261     }
15262
15263   if (FLOAT_CLASS_P (class1) != FLOAT_CLASS_P (class2))
15264     return true;
15265
15266   /* ??? This is a lie.  We do have moves between mmx/general, and for
15267      mmx/sse2.  But by saying we need secondary memory we discourage the
15268      register allocator from using the mmx registers unless needed.  */
15269   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2))
15270     return true;
15271
15272   if (SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
15273     {
15274       /* SSE1 doesn't have any direct moves from other classes.  */
15275       if (!TARGET_SSE2)
15276         return true;
15277
15278       /* If the target says that inter-unit moves are more expensive 
15279          than moving through memory, then don't generate them.  */
15280       if (!TARGET_INTER_UNIT_MOVES && !optimize_size)
15281         return true;
15282
15283       /* Between SSE and general, we have moves no larger than word size.  */
15284       if (GET_MODE_SIZE (mode) > UNITS_PER_WORD)
15285         return true;
15286
15287       /* ??? For the cost of one register reformat penalty, we could use
15288          the same instructions to move SFmode and DFmode data, but the 
15289          relevant move patterns don't support those alternatives.  */
15290       if (mode == SFmode || mode == DFmode)
15291         return true;
15292     }
15293
15294   return false;
15295 }
15296
15297 /* Return the cost of moving data from a register in class CLASS1 to
15298    one in class CLASS2.
15299
15300    It is not required that the cost always equal 2 when FROM is the same as TO;
15301    on some machines it is expensive to move between registers if they are not
15302    general registers.  */
15303
15304 int
15305 ix86_register_move_cost (enum machine_mode mode, enum reg_class class1,
15306                          enum reg_class class2)
15307 {
15308   /* In case we require secondary memory, compute cost of the store followed
15309      by load.  In order to avoid bad register allocation choices, we need
15310      for this to be *at least* as high as the symmetric MEMORY_MOVE_COST.  */
15311
15312   if (ix86_secondary_memory_needed (class1, class2, mode, 0))
15313     {
15314       int cost = 1;
15315
15316       cost += MAX (MEMORY_MOVE_COST (mode, class1, 0),
15317                    MEMORY_MOVE_COST (mode, class1, 1));
15318       cost += MAX (MEMORY_MOVE_COST (mode, class2, 0),
15319                    MEMORY_MOVE_COST (mode, class2, 1));
15320
15321       /* In case of copying from general_purpose_register we may emit multiple
15322          stores followed by single load causing memory size mismatch stall.
15323          Count this as arbitrarily high cost of 20.  */
15324       if (CLASS_MAX_NREGS (class1, mode) > CLASS_MAX_NREGS (class2, mode))
15325         cost += 20;
15326
15327       /* In the case of FP/MMX moves, the registers actually overlap, and we
15328          have to switch modes in order to treat them differently.  */
15329       if ((MMX_CLASS_P (class1) && MAYBE_FLOAT_CLASS_P (class2))
15330           || (MMX_CLASS_P (class2) && MAYBE_FLOAT_CLASS_P (class1)))
15331         cost += 20;
15332
15333       return cost;
15334     }
15335
15336   /* Moves between SSE/MMX and integer unit are expensive.  */
15337   if (MMX_CLASS_P (class1) != MMX_CLASS_P (class2)
15338       || SSE_CLASS_P (class1) != SSE_CLASS_P (class2))
15339     return ix86_cost->mmxsse_to_integer;
15340   if (MAYBE_FLOAT_CLASS_P (class1))
15341     return ix86_cost->fp_move;
15342   if (MAYBE_SSE_CLASS_P (class1))
15343     return ix86_cost->sse_move;
15344   if (MAYBE_MMX_CLASS_P (class1))
15345     return ix86_cost->mmx_move;
15346   return 2;
15347 }
15348
15349 /* Return 1 if hard register REGNO can hold a value of machine-mode MODE.  */
15350
15351 bool
15352 ix86_hard_regno_mode_ok (int regno, enum machine_mode mode)
15353 {
15354   /* Flags and only flags can only hold CCmode values.  */
15355   if (CC_REGNO_P (regno))
15356     return GET_MODE_CLASS (mode) == MODE_CC;
15357   if (GET_MODE_CLASS (mode) == MODE_CC
15358       || GET_MODE_CLASS (mode) == MODE_RANDOM
15359       || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
15360     return 0;
15361   if (FP_REGNO_P (regno))
15362     return VALID_FP_MODE_P (mode);
15363   if (SSE_REGNO_P (regno))
15364     {
15365       /* We implement the move patterns for all vector modes into and
15366          out of SSE registers, even when no operation instructions
15367          are available.  */
15368       return (VALID_SSE_REG_MODE (mode)
15369               || VALID_SSE2_REG_MODE (mode)
15370               || VALID_MMX_REG_MODE (mode)
15371               || VALID_MMX_REG_MODE_3DNOW (mode));
15372     }
15373   if (MMX_REGNO_P (regno))
15374     {
15375       /* We implement the move patterns for 3DNOW modes even in MMX mode,
15376          so if the register is available at all, then we can move data of
15377          the given mode into or out of it.  */
15378       return (VALID_MMX_REG_MODE (mode)
15379               || VALID_MMX_REG_MODE_3DNOW (mode));
15380     }
15381
15382   if (mode == QImode)
15383     {
15384       /* Take care for QImode values - they can be in non-QI regs,
15385          but then they do cause partial register stalls.  */
15386       if (regno < 4 || TARGET_64BIT)
15387         return 1;
15388       if (!TARGET_PARTIAL_REG_STALL)
15389         return 1;
15390       return reload_in_progress || reload_completed;
15391     }
15392   /* We handle both integer and floats in the general purpose registers.  */
15393   else if (VALID_INT_MODE_P (mode))
15394     return 1;
15395   else if (VALID_FP_MODE_P (mode))
15396     return 1;
15397   /* Lots of MMX code casts 8 byte vector modes to DImode.  If we then go
15398      on to use that value in smaller contexts, this can easily force a 
15399      pseudo to be allocated to GENERAL_REGS.  Since this is no worse than
15400      supporting DImode, allow it.  */
15401   else if (VALID_MMX_REG_MODE_3DNOW (mode) || VALID_MMX_REG_MODE (mode))
15402     return 1;
15403
15404   return 0;
15405 }
15406
15407 /* A subroutine of ix86_modes_tieable_p.  Return true if MODE is a 
15408    tieable integer mode.  */
15409
15410 static bool
15411 ix86_tieable_integer_mode_p (enum machine_mode mode)
15412 {
15413   switch (mode)
15414     {
15415     case HImode:
15416     case SImode:
15417       return true;
15418
15419     case QImode:
15420       return TARGET_64BIT || !TARGET_PARTIAL_REG_STALL;
15421
15422     case DImode:
15423       return TARGET_64BIT;
15424
15425     default:
15426       return false;
15427     }
15428 }
15429
15430 /* Return true if MODE1 is accessible in a register that can hold MODE2
15431    without copying.  That is, all register classes that can hold MODE2
15432    can also hold MODE1.  */
15433
15434 bool
15435 ix86_modes_tieable_p (enum machine_mode mode1, enum machine_mode mode2)
15436 {
15437   if (mode1 == mode2)
15438     return true;
15439
15440   if (ix86_tieable_integer_mode_p (mode1)
15441       && ix86_tieable_integer_mode_p (mode2))
15442     return true;
15443
15444   /* MODE2 being XFmode implies fp stack or general regs, which means we
15445      can tie any smaller floating point modes to it.  Note that we do not
15446      tie this with TFmode.  */
15447   if (mode2 == XFmode)
15448     return mode1 == SFmode || mode1 == DFmode;
15449
15450   /* MODE2 being DFmode implies fp stack, general or sse regs, which means
15451      that we can tie it with SFmode.  */
15452   if (mode2 == DFmode)
15453     return mode1 == SFmode;
15454
15455   /* If MODE2 is only appropriate for an SSE register, then tie with 
15456      any other mode acceptable to SSE registers.  */
15457   if (GET_MODE_SIZE (mode2) >= 8
15458       && ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode2))
15459     return ix86_hard_regno_mode_ok (FIRST_SSE_REG, mode1);
15460
15461   /* If MODE2 is appropriate for an MMX (or SSE) register, then tie
15462      with any other mode acceptable to MMX registers.  */
15463   if (GET_MODE_SIZE (mode2) == 8
15464       && ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode2))
15465     return ix86_hard_regno_mode_ok (FIRST_MMX_REG, mode1);
15466
15467   return false;
15468 }
15469
15470 /* Return the cost of moving data of mode M between a
15471    register and memory.  A value of 2 is the default; this cost is
15472    relative to those in `REGISTER_MOVE_COST'.
15473
15474    If moving between registers and memory is more expensive than
15475    between two registers, you should define this macro to express the
15476    relative cost.
15477
15478    Model also increased moving costs of QImode registers in non
15479    Q_REGS classes.
15480  */
15481 int
15482 ix86_memory_move_cost (enum machine_mode mode, enum reg_class class, int in)
15483 {
15484   if (FLOAT_CLASS_P (class))
15485     {
15486       int index;
15487       switch (mode)
15488         {
15489           case SFmode:
15490             index = 0;
15491             break;
15492           case DFmode:
15493             index = 1;
15494             break;
15495           case XFmode:
15496             index = 2;
15497             break;
15498           default:
15499             return 100;
15500         }
15501       return in ? ix86_cost->fp_load [index] : ix86_cost->fp_store [index];
15502     }
15503   if (SSE_CLASS_P (class))
15504     {
15505       int index;
15506       switch (GET_MODE_SIZE (mode))
15507         {
15508           case 4:
15509             index = 0;
15510             break;
15511           case 8:
15512             index = 1;
15513             break;
15514           case 16:
15515             index = 2;
15516             break;
15517           default:
15518             return 100;
15519         }
15520       return in ? ix86_cost->sse_load [index] : ix86_cost->sse_store [index];
15521     }
15522   if (MMX_CLASS_P (class))
15523     {
15524       int index;
15525       switch (GET_MODE_SIZE (mode))
15526         {
15527           case 4:
15528             index = 0;
15529             break;
15530           case 8:
15531             index = 1;
15532             break;
15533           default:
15534             return 100;
15535         }
15536       return in ? ix86_cost->mmx_load [index] : ix86_cost->mmx_store [index];
15537     }
15538   switch (GET_MODE_SIZE (mode))
15539     {
15540       case 1:
15541         if (in)
15542           return (Q_CLASS_P (class) ? ix86_cost->int_load[0]
15543                   : ix86_cost->movzbl_load);
15544         else
15545           return (Q_CLASS_P (class) ? ix86_cost->int_store[0]
15546                   : ix86_cost->int_store[0] + 4);
15547         break;
15548       case 2:
15549         return in ? ix86_cost->int_load[1] : ix86_cost->int_store[1];
15550       default:
15551         /* Compute number of 32bit moves needed.  TFmode is moved as XFmode.  */
15552         if (mode == TFmode)
15553           mode = XFmode;
15554         return ((in ? ix86_cost->int_load[2] : ix86_cost->int_store[2])
15555                 * (((int) GET_MODE_SIZE (mode)
15556                     + UNITS_PER_WORD - 1) / UNITS_PER_WORD));
15557     }
15558 }
15559
15560 /* Compute a (partial) cost for rtx X.  Return true if the complete
15561    cost has been computed, and false if subexpressions should be
15562    scanned.  In either case, *TOTAL contains the cost result.  */
15563
15564 static bool
15565 ix86_rtx_costs (rtx x, int code, int outer_code, int *total)
15566 {
15567   enum machine_mode mode = GET_MODE (x);
15568
15569   switch (code)
15570     {
15571     case CONST_INT:
15572     case CONST:
15573     case LABEL_REF:
15574     case SYMBOL_REF:
15575       if (TARGET_64BIT && !x86_64_immediate_operand (x, VOIDmode))
15576         *total = 3;
15577       else if (TARGET_64BIT && !x86_64_zext_immediate_operand (x, VOIDmode))
15578         *total = 2;
15579       else if (flag_pic && SYMBOLIC_CONST (x)
15580                && (!TARGET_64BIT
15581                    || (!GET_CODE (x) != LABEL_REF
15582                        && (GET_CODE (x) != SYMBOL_REF
15583                            || !SYMBOL_REF_LOCAL_P (x)))))
15584         *total = 1;
15585       else
15586         *total = 0;
15587       return true;
15588
15589     case CONST_DOUBLE:
15590       if (mode == VOIDmode)
15591         *total = 0;
15592       else
15593         switch (standard_80387_constant_p (x))
15594           {
15595           case 1: /* 0.0 */
15596             *total = 1;
15597             break;
15598           default: /* Other constants */
15599             *total = 2;
15600             break;
15601           case 0:
15602           case -1:
15603             /* Start with (MEM (SYMBOL_REF)), since that's where
15604                it'll probably end up.  Add a penalty for size.  */
15605             *total = (COSTS_N_INSNS (1)
15606                       + (flag_pic != 0 && !TARGET_64BIT)
15607                       + (mode == SFmode ? 0 : mode == DFmode ? 1 : 2));
15608             break;
15609           }
15610       return true;
15611
15612     case ZERO_EXTEND:
15613       /* The zero extensions is often completely free on x86_64, so make
15614          it as cheap as possible.  */
15615       if (TARGET_64BIT && mode == DImode
15616           && GET_MODE (XEXP (x, 0)) == SImode)
15617         *total = 1;
15618       else if (TARGET_ZERO_EXTEND_WITH_AND)
15619         *total = COSTS_N_INSNS (ix86_cost->add);
15620       else
15621         *total = COSTS_N_INSNS (ix86_cost->movzx);
15622       return false;
15623
15624     case SIGN_EXTEND:
15625       *total = COSTS_N_INSNS (ix86_cost->movsx);
15626       return false;
15627
15628     case ASHIFT:
15629       if (GET_CODE (XEXP (x, 1)) == CONST_INT
15630           && (GET_MODE (XEXP (x, 0)) != DImode || TARGET_64BIT))
15631         {
15632           HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
15633           if (value == 1)
15634             {
15635               *total = COSTS_N_INSNS (ix86_cost->add);
15636               return false;
15637             }
15638           if ((value == 2 || value == 3)
15639               && ix86_cost->lea <= ix86_cost->shift_const)
15640             {
15641               *total = COSTS_N_INSNS (ix86_cost->lea);
15642               return false;
15643             }
15644         }
15645       /* FALLTHRU */
15646
15647     case ROTATE:
15648     case ASHIFTRT:
15649     case LSHIFTRT:
15650     case ROTATERT:
15651       if (!TARGET_64BIT && GET_MODE (XEXP (x, 0)) == DImode)
15652         {
15653           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
15654             {
15655               if (INTVAL (XEXP (x, 1)) > 32)
15656                 *total = COSTS_N_INSNS(ix86_cost->shift_const + 2);
15657               else
15658                 *total = COSTS_N_INSNS(ix86_cost->shift_const * 2);
15659             }
15660           else
15661             {
15662               if (GET_CODE (XEXP (x, 1)) == AND)
15663                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 2);
15664               else
15665                 *total = COSTS_N_INSNS(ix86_cost->shift_var * 6 + 2);
15666             }
15667         }
15668       else
15669         {
15670           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
15671             *total = COSTS_N_INSNS (ix86_cost->shift_const);
15672           else
15673             *total = COSTS_N_INSNS (ix86_cost->shift_var);
15674         }
15675       return false;
15676
15677     case MULT:
15678       if (FLOAT_MODE_P (mode))
15679         {
15680           *total = COSTS_N_INSNS (ix86_cost->fmul);
15681           return false;
15682         }
15683       else
15684         {
15685           rtx op0 = XEXP (x, 0);
15686           rtx op1 = XEXP (x, 1);
15687           int nbits;
15688           if (GET_CODE (XEXP (x, 1)) == CONST_INT)
15689             {
15690               unsigned HOST_WIDE_INT value = INTVAL (XEXP (x, 1));
15691               for (nbits = 0; value != 0; value &= value - 1)
15692                 nbits++;
15693             }
15694           else
15695             /* This is arbitrary.  */
15696             nbits = 7;
15697
15698           /* Compute costs correctly for widening multiplication.  */
15699           if ((GET_CODE (op0) == SIGN_EXTEND || GET_CODE (op1) == ZERO_EXTEND)
15700               && GET_MODE_SIZE (GET_MODE (XEXP (op0, 0))) * 2
15701                  == GET_MODE_SIZE (mode))
15702             {
15703               int is_mulwiden = 0;
15704               enum machine_mode inner_mode = GET_MODE (op0);
15705
15706               if (GET_CODE (op0) == GET_CODE (op1))
15707                 is_mulwiden = 1, op1 = XEXP (op1, 0);
15708               else if (GET_CODE (op1) == CONST_INT)
15709                 {
15710                   if (GET_CODE (op0) == SIGN_EXTEND)
15711                     is_mulwiden = trunc_int_for_mode (INTVAL (op1), inner_mode)
15712                                   == INTVAL (op1);
15713                   else
15714                     is_mulwiden = !(INTVAL (op1) & ~GET_MODE_MASK (inner_mode));
15715                 }
15716
15717               if (is_mulwiden)
15718                 op0 = XEXP (op0, 0), mode = GET_MODE (op0);
15719             }
15720
15721           *total = COSTS_N_INSNS (ix86_cost->mult_init[MODE_INDEX (mode)]
15722                                   + nbits * ix86_cost->mult_bit)
15723                    + rtx_cost (op0, outer_code) + rtx_cost (op1, outer_code);
15724
15725           return true;
15726         }
15727
15728     case DIV:
15729     case UDIV:
15730     case MOD:
15731     case UMOD:
15732       if (FLOAT_MODE_P (mode))
15733         *total = COSTS_N_INSNS (ix86_cost->fdiv);
15734       else
15735         *total = COSTS_N_INSNS (ix86_cost->divide[MODE_INDEX (mode)]);
15736       return false;
15737
15738     case PLUS:
15739       if (FLOAT_MODE_P (mode))
15740         *total = COSTS_N_INSNS (ix86_cost->fadd);
15741       else if (GET_MODE_CLASS (mode) == MODE_INT
15742                && GET_MODE_BITSIZE (mode) <= GET_MODE_BITSIZE (Pmode))
15743         {
15744           if (GET_CODE (XEXP (x, 0)) == PLUS
15745               && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
15746               && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
15747               && CONSTANT_P (XEXP (x, 1)))
15748             {
15749               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1));
15750               if (val == 2 || val == 4 || val == 8)
15751                 {
15752                   *total = COSTS_N_INSNS (ix86_cost->lea);
15753                   *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
15754                   *total += rtx_cost (XEXP (XEXP (XEXP (x, 0), 0), 0),
15755                                       outer_code);
15756                   *total += rtx_cost (XEXP (x, 1), outer_code);
15757                   return true;
15758                 }
15759             }
15760           else if (GET_CODE (XEXP (x, 0)) == MULT
15761                    && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
15762             {
15763               HOST_WIDE_INT val = INTVAL (XEXP (XEXP (x, 0), 1));
15764               if (val == 2 || val == 4 || val == 8)
15765                 {
15766                   *total = COSTS_N_INSNS (ix86_cost->lea);
15767                   *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
15768                   *total += rtx_cost (XEXP (x, 1), outer_code);
15769                   return true;
15770                 }
15771             }
15772           else if (GET_CODE (XEXP (x, 0)) == PLUS)
15773             {
15774               *total = COSTS_N_INSNS (ix86_cost->lea);
15775               *total += rtx_cost (XEXP (XEXP (x, 0), 0), outer_code);
15776               *total += rtx_cost (XEXP (XEXP (x, 0), 1), outer_code);
15777               *total += rtx_cost (XEXP (x, 1), outer_code);
15778               return true;
15779             }
15780         }
15781       /* FALLTHRU */
15782
15783     case MINUS:
15784       if (FLOAT_MODE_P (mode))
15785         {
15786           *total = COSTS_N_INSNS (ix86_cost->fadd);
15787           return false;
15788         }
15789       /* FALLTHRU */
15790
15791     case AND:
15792     case IOR:
15793     case XOR:
15794       if (!TARGET_64BIT && mode == DImode)
15795         {
15796           *total = (COSTS_N_INSNS (ix86_cost->add) * 2
15797                     + (rtx_cost (XEXP (x, 0), outer_code)
15798                        << (GET_MODE (XEXP (x, 0)) != DImode))
15799                     + (rtx_cost (XEXP (x, 1), outer_code)
15800                        << (GET_MODE (XEXP (x, 1)) != DImode)));
15801           return true;
15802         }
15803       /* FALLTHRU */
15804
15805     case NEG:
15806       if (FLOAT_MODE_P (mode))
15807         {
15808           *total = COSTS_N_INSNS (ix86_cost->fchs);
15809           return false;
15810         }
15811       /* FALLTHRU */
15812
15813     case NOT:
15814       if (!TARGET_64BIT && mode == DImode)
15815         *total = COSTS_N_INSNS (ix86_cost->add * 2);
15816       else
15817         *total = COSTS_N_INSNS (ix86_cost->add);
15818       return false;
15819
15820     case COMPARE:
15821       if (GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
15822           && XEXP (XEXP (x, 0), 1) == const1_rtx
15823           && GET_CODE (XEXP (XEXP (x, 0), 2)) == CONST_INT
15824           && XEXP (x, 1) == const0_rtx)
15825         {
15826           /* This kind of construct is implemented using test[bwl].
15827              Treat it as if we had an AND.  */
15828           *total = (COSTS_N_INSNS (ix86_cost->add)
15829                     + rtx_cost (XEXP (XEXP (x, 0), 0), outer_code)
15830                     + rtx_cost (const1_rtx, outer_code));
15831           return true;
15832         }
15833       return false;
15834
15835     case FLOAT_EXTEND:
15836       if (!TARGET_SSE_MATH
15837           || mode == XFmode
15838           || (mode == DFmode && !TARGET_SSE2))
15839         *total = 0;
15840       return false;
15841
15842     case ABS:
15843       if (FLOAT_MODE_P (mode))
15844         *total = COSTS_N_INSNS (ix86_cost->fabs);
15845       return false;
15846
15847     case SQRT:
15848       if (FLOAT_MODE_P (mode))
15849         *total = COSTS_N_INSNS (ix86_cost->fsqrt);
15850       return false;
15851
15852     case UNSPEC:
15853       if (XINT (x, 1) == UNSPEC_TP)
15854         *total = 0;
15855       return false;
15856
15857     default:
15858       return false;
15859     }
15860 }
15861
15862 #if TARGET_MACHO
15863
15864 static int current_machopic_label_num;
15865
15866 /* Given a symbol name and its associated stub, write out the
15867    definition of the stub.  */
15868
15869 void
15870 machopic_output_stub (FILE *file, const char *symb, const char *stub)
15871 {
15872   unsigned int length;
15873   char *binder_name, *symbol_name, lazy_ptr_name[32];
15874   int label = ++current_machopic_label_num;
15875
15876   /* Lose our funky encoding stuff so it doesn't contaminate the stub.  */
15877   symb = (*targetm.strip_name_encoding) (symb);
15878
15879   length = strlen (stub);
15880   binder_name = alloca (length + 32);
15881   GEN_BINDER_NAME_FOR_STUB (binder_name, stub, length);
15882
15883   length = strlen (symb);
15884   symbol_name = alloca (length + 32);
15885   GEN_SYMBOL_NAME_FOR_SYMBOL (symbol_name, symb, length);
15886
15887   sprintf (lazy_ptr_name, "L%d$lz", label);
15888
15889   if (MACHOPIC_PURE)
15890     machopic_picsymbol_stub_section ();
15891   else
15892     machopic_symbol_stub_section ();
15893
15894   fprintf (file, "%s:\n", stub);
15895   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15896
15897   if (MACHOPIC_PURE)
15898     {
15899       fprintf (file, "\tcall LPC$%d\nLPC$%d:\tpopl %%eax\n", label, label);
15900       fprintf (file, "\tmovl %s-LPC$%d(%%eax),%%edx\n", lazy_ptr_name, label);
15901       fprintf (file, "\tjmp %%edx\n");
15902     }
15903   else
15904     fprintf (file, "\tjmp *%s\n", lazy_ptr_name);
15905
15906   fprintf (file, "%s:\n", binder_name);
15907
15908   if (MACHOPIC_PURE)
15909     {
15910       fprintf (file, "\tlea %s-LPC$%d(%%eax),%%eax\n", lazy_ptr_name, label);
15911       fprintf (file, "\tpushl %%eax\n");
15912     }
15913   else
15914     fprintf (file, "\t pushl $%s\n", lazy_ptr_name);
15915
15916   fprintf (file, "\tjmp dyld_stub_binding_helper\n");
15917
15918   machopic_lazy_symbol_ptr_section ();
15919   fprintf (file, "%s:\n", lazy_ptr_name);
15920   fprintf (file, "\t.indirect_symbol %s\n", symbol_name);
15921   fprintf (file, "\t.long %s\n", binder_name);
15922 }
15923 #endif /* TARGET_MACHO */
15924
15925 /* Order the registers for register allocator.  */
15926
15927 void
15928 x86_order_regs_for_local_alloc (void)
15929 {
15930    int pos = 0;
15931    int i;
15932
15933    /* First allocate the local general purpose registers.  */
15934    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15935      if (GENERAL_REGNO_P (i) && call_used_regs[i])
15936         reg_alloc_order [pos++] = i;
15937
15938    /* Global general purpose registers.  */
15939    for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
15940      if (GENERAL_REGNO_P (i) && !call_used_regs[i])
15941         reg_alloc_order [pos++] = i;
15942
15943    /* x87 registers come first in case we are doing FP math
15944       using them.  */
15945    if (!TARGET_SSE_MATH)
15946      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
15947        reg_alloc_order [pos++] = i;
15948
15949    /* SSE registers.  */
15950    for (i = FIRST_SSE_REG; i <= LAST_SSE_REG; i++)
15951      reg_alloc_order [pos++] = i;
15952    for (i = FIRST_REX_SSE_REG; i <= LAST_REX_SSE_REG; i++)
15953      reg_alloc_order [pos++] = i;
15954
15955    /* x87 registers.  */
15956    if (TARGET_SSE_MATH)
15957      for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
15958        reg_alloc_order [pos++] = i;
15959
15960    for (i = FIRST_MMX_REG; i <= LAST_MMX_REG; i++)
15961      reg_alloc_order [pos++] = i;
15962
15963    /* Initialize the rest of array as we do not allocate some registers
15964       at all.  */
15965    while (pos < FIRST_PSEUDO_REGISTER)
15966      reg_alloc_order [pos++] = 0;
15967 }
15968
15969 /* Handle a "ms_struct" or "gcc_struct" attribute; arguments as in
15970    struct attribute_spec.handler.  */
15971 static tree
15972 ix86_handle_struct_attribute (tree *node, tree name,
15973                               tree args ATTRIBUTE_UNUSED,
15974                               int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
15975 {
15976   tree *type = NULL;
15977   if (DECL_P (*node))
15978     {
15979       if (TREE_CODE (*node) == TYPE_DECL)
15980         type = &TREE_TYPE (*node);
15981     }
15982   else
15983     type = node;
15984
15985   if (!(type && (TREE_CODE (*type) == RECORD_TYPE
15986                  || TREE_CODE (*type) == UNION_TYPE)))
15987     {
15988       warning ("%qs attribute ignored", IDENTIFIER_POINTER (name));
15989       *no_add_attrs = true;
15990     }
15991
15992   else if ((is_attribute_p ("ms_struct", name)
15993             && lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (*type)))
15994            || ((is_attribute_p ("gcc_struct", name)
15995                 && lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (*type)))))
15996     {
15997       warning ("%qs incompatible attribute ignored",
15998                IDENTIFIER_POINTER (name));
15999       *no_add_attrs = true;
16000     }
16001
16002   return NULL_TREE;
16003 }
16004
16005 static bool
16006 ix86_ms_bitfield_layout_p (tree record_type)
16007 {
16008   return (TARGET_MS_BITFIELD_LAYOUT &&
16009           !lookup_attribute ("gcc_struct", TYPE_ATTRIBUTES (record_type)))
16010     || lookup_attribute ("ms_struct", TYPE_ATTRIBUTES (record_type));
16011 }
16012
16013 /* Returns an expression indicating where the this parameter is
16014    located on entry to the FUNCTION.  */
16015
16016 static rtx
16017 x86_this_parameter (tree function)
16018 {
16019   tree type = TREE_TYPE (function);
16020
16021   if (TARGET_64BIT)
16022     {
16023       int n = aggregate_value_p (TREE_TYPE (type), type) != 0;
16024       return gen_rtx_REG (DImode, x86_64_int_parameter_registers[n]);
16025     }
16026
16027   if (ix86_function_regparm (type, function) > 0)
16028     {
16029       tree parm;
16030
16031       parm = TYPE_ARG_TYPES (type);
16032       /* Figure out whether or not the function has a variable number of
16033          arguments.  */
16034       for (; parm; parm = TREE_CHAIN (parm))
16035         if (TREE_VALUE (parm) == void_type_node)
16036           break;
16037       /* If not, the this parameter is in the first argument.  */
16038       if (parm)
16039         {
16040           int regno = 0;
16041           if (lookup_attribute ("fastcall", TYPE_ATTRIBUTES (type)))
16042             regno = 2;
16043           return gen_rtx_REG (SImode, regno);
16044         }
16045     }
16046
16047   if (aggregate_value_p (TREE_TYPE (type), type))
16048     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 8));
16049   else
16050     return gen_rtx_MEM (SImode, plus_constant (stack_pointer_rtx, 4));
16051 }
16052
16053 /* Determine whether x86_output_mi_thunk can succeed.  */
16054
16055 static bool
16056 x86_can_output_mi_thunk (tree thunk ATTRIBUTE_UNUSED,
16057                          HOST_WIDE_INT delta ATTRIBUTE_UNUSED,
16058                          HOST_WIDE_INT vcall_offset, tree function)
16059 {
16060   /* 64-bit can handle anything.  */
16061   if (TARGET_64BIT)
16062     return true;
16063
16064   /* For 32-bit, everything's fine if we have one free register.  */
16065   if (ix86_function_regparm (TREE_TYPE (function), function) < 3)
16066     return true;
16067
16068   /* Need a free register for vcall_offset.  */
16069   if (vcall_offset)
16070     return false;
16071
16072   /* Need a free register for GOT references.  */
16073   if (flag_pic && !(*targetm.binds_local_p) (function))
16074     return false;
16075
16076   /* Otherwise ok.  */
16077   return true;
16078 }
16079
16080 /* Output the assembler code for a thunk function.  THUNK_DECL is the
16081    declaration for the thunk function itself, FUNCTION is the decl for
16082    the target function.  DELTA is an immediate constant offset to be
16083    added to THIS.  If VCALL_OFFSET is nonzero, the word at
16084    *(*this + vcall_offset) should be added to THIS.  */
16085
16086 static void
16087 x86_output_mi_thunk (FILE *file ATTRIBUTE_UNUSED,
16088                      tree thunk ATTRIBUTE_UNUSED, HOST_WIDE_INT delta,
16089                      HOST_WIDE_INT vcall_offset, tree function)
16090 {
16091   rtx xops[3];
16092   rtx this = x86_this_parameter (function);
16093   rtx this_reg, tmp;
16094
16095   /* If VCALL_OFFSET, we'll need THIS in a register.  Might as well
16096      pull it in now and let DELTA benefit.  */
16097   if (REG_P (this))
16098     this_reg = this;
16099   else if (vcall_offset)
16100     {
16101       /* Put the this parameter into %eax.  */
16102       xops[0] = this;
16103       xops[1] = this_reg = gen_rtx_REG (Pmode, 0);
16104       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16105     }
16106   else
16107     this_reg = NULL_RTX;
16108
16109   /* Adjust the this parameter by a fixed constant.  */
16110   if (delta)
16111     {
16112       xops[0] = GEN_INT (delta);
16113       xops[1] = this_reg ? this_reg : this;
16114       if (TARGET_64BIT)
16115         {
16116           if (!x86_64_general_operand (xops[0], DImode))
16117             {
16118               tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
16119               xops[1] = tmp;
16120               output_asm_insn ("mov{q}\t{%1, %0|%0, %1}", xops);
16121               xops[0] = tmp;
16122               xops[1] = this;
16123             }
16124           output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
16125         }
16126       else
16127         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
16128     }
16129
16130   /* Adjust the this parameter by a value stored in the vtable.  */
16131   if (vcall_offset)
16132     {
16133       if (TARGET_64BIT)
16134         tmp = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 2 /* R10 */);
16135       else
16136         {
16137           int tmp_regno = 2 /* ECX */;
16138           if (lookup_attribute ("fastcall",
16139               TYPE_ATTRIBUTES (TREE_TYPE (function))))
16140             tmp_regno = 0 /* EAX */;
16141           tmp = gen_rtx_REG (SImode, tmp_regno);
16142         }
16143
16144       xops[0] = gen_rtx_MEM (Pmode, this_reg);
16145       xops[1] = tmp;
16146       if (TARGET_64BIT)
16147         output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
16148       else
16149         output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16150
16151       /* Adjust the this parameter.  */
16152       xops[0] = gen_rtx_MEM (Pmode, plus_constant (tmp, vcall_offset));
16153       if (TARGET_64BIT && !memory_operand (xops[0], Pmode))
16154         {
16155           rtx tmp2 = gen_rtx_REG (DImode, FIRST_REX_INT_REG + 3 /* R11 */);
16156           xops[0] = GEN_INT (vcall_offset);
16157           xops[1] = tmp2;
16158           output_asm_insn ("mov{q}\t{%0, %1|%1, %0}", xops);
16159           xops[0] = gen_rtx_MEM (Pmode, gen_rtx_PLUS (Pmode, tmp, tmp2));
16160         }
16161       xops[1] = this_reg;
16162       if (TARGET_64BIT)
16163         output_asm_insn ("add{q}\t{%0, %1|%1, %0}", xops);
16164       else
16165         output_asm_insn ("add{l}\t{%0, %1|%1, %0}", xops);
16166     }
16167
16168   /* If necessary, drop THIS back to its stack slot.  */
16169   if (this_reg && this_reg != this)
16170     {
16171       xops[0] = this_reg;
16172       xops[1] = this;
16173       output_asm_insn ("mov{l}\t{%0, %1|%1, %0}", xops);
16174     }
16175
16176   xops[0] = XEXP (DECL_RTL (function), 0);
16177   if (TARGET_64BIT)
16178     {
16179       if (!flag_pic || (*targetm.binds_local_p) (function))
16180         output_asm_insn ("jmp\t%P0", xops);
16181       else
16182         {
16183           tmp = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, xops[0]), UNSPEC_GOTPCREL);
16184           tmp = gen_rtx_CONST (Pmode, tmp);
16185           tmp = gen_rtx_MEM (QImode, tmp);
16186           xops[0] = tmp;
16187           output_asm_insn ("jmp\t%A0", xops);
16188         }
16189     }
16190   else
16191     {
16192       if (!flag_pic || (*targetm.binds_local_p) (function))
16193         output_asm_insn ("jmp\t%P0", xops);
16194       else
16195 #if TARGET_MACHO
16196         if (TARGET_MACHO)
16197           {
16198             rtx sym_ref = XEXP (DECL_RTL (function), 0);
16199             tmp = (gen_rtx_SYMBOL_REF
16200                    (Pmode,
16201                     machopic_indirection_name (sym_ref, /*stub_p=*/true)));
16202             tmp = gen_rtx_MEM (QImode, tmp);
16203             xops[0] = tmp;
16204             output_asm_insn ("jmp\t%0", xops);
16205           }
16206         else
16207 #endif /* TARGET_MACHO */
16208         {
16209           tmp = gen_rtx_REG (SImode, 2 /* ECX */);
16210           output_set_got (tmp);
16211
16212           xops[1] = tmp;
16213           output_asm_insn ("mov{l}\t{%0@GOT(%1), %1|%1, %0@GOT[%1]}", xops);
16214           output_asm_insn ("jmp\t{*}%1", xops);
16215         }
16216     }
16217 }
16218
16219 static void
16220 x86_file_start (void)
16221 {
16222   default_file_start ();
16223   if (X86_FILE_START_VERSION_DIRECTIVE)
16224     fputs ("\t.version\t\"01.01\"\n", asm_out_file);
16225   if (X86_FILE_START_FLTUSED)
16226     fputs ("\t.global\t__fltused\n", asm_out_file);
16227   if (ix86_asm_dialect == ASM_INTEL)
16228     fputs ("\t.intel_syntax\n", asm_out_file);
16229 }
16230
16231 int
16232 x86_field_alignment (tree field, int computed)
16233 {
16234   enum machine_mode mode;
16235   tree type = TREE_TYPE (field);
16236
16237   if (TARGET_64BIT || TARGET_ALIGN_DOUBLE)
16238     return computed;
16239   mode = TYPE_MODE (TREE_CODE (type) == ARRAY_TYPE
16240                     ? get_inner_array_type (type) : type);
16241   if (mode == DFmode || mode == DCmode
16242       || GET_MODE_CLASS (mode) == MODE_INT
16243       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
16244     return MIN (32, computed);
16245   return computed;
16246 }
16247
16248 /* Output assembler code to FILE to increment profiler label # LABELNO
16249    for profiling a function entry.  */
16250 void
16251 x86_function_profiler (FILE *file, int labelno ATTRIBUTE_UNUSED)
16252 {
16253   if (TARGET_64BIT)
16254     if (flag_pic)
16255       {
16256 #ifndef NO_PROFILE_COUNTERS
16257         fprintf (file, "\tleaq\t%sP%d@(%%rip),%%r11\n", LPREFIX, labelno);
16258 #endif
16259         fprintf (file, "\tcall\t*%s@GOTPCREL(%%rip)\n", MCOUNT_NAME);
16260       }
16261     else
16262       {
16263 #ifndef NO_PROFILE_COUNTERS
16264         fprintf (file, "\tmovq\t$%sP%d,%%r11\n", LPREFIX, labelno);
16265 #endif
16266         fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
16267       }
16268   else if (flag_pic)
16269     {
16270 #ifndef NO_PROFILE_COUNTERS
16271       fprintf (file, "\tleal\t%sP%d@GOTOFF(%%ebx),%%%s\n",
16272                LPREFIX, labelno, PROFILE_COUNT_REGISTER);
16273 #endif
16274       fprintf (file, "\tcall\t*%s@GOT(%%ebx)\n", MCOUNT_NAME);
16275     }
16276   else
16277     {
16278 #ifndef NO_PROFILE_COUNTERS
16279       fprintf (file, "\tmovl\t$%sP%d,%%%s\n", LPREFIX, labelno,
16280                PROFILE_COUNT_REGISTER);
16281 #endif
16282       fprintf (file, "\tcall\t%s\n", MCOUNT_NAME);
16283     }
16284 }
16285
16286 /* We don't have exact information about the insn sizes, but we may assume
16287    quite safely that we are informed about all 1 byte insns and memory
16288    address sizes.  This is enough to eliminate unnecessary padding in
16289    99% of cases.  */
16290
16291 static int
16292 min_insn_size (rtx insn)
16293 {
16294   int l = 0;
16295
16296   if (!INSN_P (insn) || !active_insn_p (insn))
16297     return 0;
16298
16299   /* Discard alignments we've emit and jump instructions.  */
16300   if (GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE
16301       && XINT (PATTERN (insn), 1) == UNSPECV_ALIGN)
16302     return 0;
16303   if (GET_CODE (insn) == JUMP_INSN
16304       && (GET_CODE (PATTERN (insn)) == ADDR_VEC
16305           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC))
16306     return 0;
16307
16308   /* Important case - calls are always 5 bytes.
16309      It is common to have many calls in the row.  */
16310   if (GET_CODE (insn) == CALL_INSN
16311       && symbolic_reference_mentioned_p (PATTERN (insn))
16312       && !SIBLING_CALL_P (insn))
16313     return 5;
16314   if (get_attr_length (insn) <= 1)
16315     return 1;
16316
16317   /* For normal instructions we may rely on the sizes of addresses
16318      and the presence of symbol to require 4 bytes of encoding.
16319      This is not the case for jumps where references are PC relative.  */
16320   if (GET_CODE (insn) != JUMP_INSN)
16321     {
16322       l = get_attr_length_address (insn);
16323       if (l < 4 && symbolic_reference_mentioned_p (PATTERN (insn)))
16324         l = 4;
16325     }
16326   if (l)
16327     return 1+l;
16328   else
16329     return 2;
16330 }
16331
16332 /* AMD K8 core mispredicts jumps when there are more than 3 jumps in 16 byte
16333    window.  */
16334
16335 static void
16336 ix86_avoid_jump_misspredicts (void)
16337 {
16338   rtx insn, start = get_insns ();
16339   int nbytes = 0, njumps = 0;
16340   int isjump = 0;
16341
16342   /* Look for all minimal intervals of instructions containing 4 jumps.
16343      The intervals are bounded by START and INSN.  NBYTES is the total
16344      size of instructions in the interval including INSN and not including
16345      START.  When the NBYTES is smaller than 16 bytes, it is possible
16346      that the end of START and INSN ends up in the same 16byte page.
16347
16348      The smallest offset in the page INSN can start is the case where START
16349      ends on the offset 0.  Offset of INSN is then NBYTES - sizeof (INSN).
16350      We add p2align to 16byte window with maxskip 17 - NBYTES + sizeof (INSN).
16351      */
16352   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
16353     {
16354
16355       nbytes += min_insn_size (insn);
16356       if (dump_file)
16357         fprintf(dump_file, "Insn %i estimated to %i bytes\n",
16358                 INSN_UID (insn), min_insn_size (insn));
16359       if ((GET_CODE (insn) == JUMP_INSN
16360            && GET_CODE (PATTERN (insn)) != ADDR_VEC
16361            && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC)
16362           || GET_CODE (insn) == CALL_INSN)
16363         njumps++;
16364       else
16365         continue;
16366
16367       while (njumps > 3)
16368         {
16369           start = NEXT_INSN (start);
16370           if ((GET_CODE (start) == JUMP_INSN
16371                && GET_CODE (PATTERN (start)) != ADDR_VEC
16372                && GET_CODE (PATTERN (start)) != ADDR_DIFF_VEC)
16373               || GET_CODE (start) == CALL_INSN)
16374             njumps--, isjump = 1;
16375           else
16376             isjump = 0;
16377           nbytes -= min_insn_size (start);
16378         }
16379       gcc_assert (njumps >= 0);
16380       if (dump_file)
16381         fprintf (dump_file, "Interval %i to %i has %i bytes\n",
16382                 INSN_UID (start), INSN_UID (insn), nbytes);
16383
16384       if (njumps == 3 && isjump && nbytes < 16)
16385         {
16386           int padsize = 15 - nbytes + min_insn_size (insn);
16387
16388           if (dump_file)
16389             fprintf (dump_file, "Padding insn %i by %i bytes!\n",
16390                      INSN_UID (insn), padsize);
16391           emit_insn_before (gen_align (GEN_INT (padsize)), insn);
16392         }
16393     }
16394 }
16395
16396 /* AMD Athlon works faster
16397    when RET is not destination of conditional jump or directly preceded
16398    by other jump instruction.  We avoid the penalty by inserting NOP just
16399    before the RET instructions in such cases.  */
16400 static void
16401 ix86_pad_returns (void)
16402 {
16403   edge e;
16404   edge_iterator ei;
16405
16406   FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR->preds)
16407     {
16408       basic_block bb = e->src;
16409       rtx ret = BB_END (bb);
16410       rtx prev;
16411       bool replace = false;
16412
16413       if (GET_CODE (ret) != JUMP_INSN || GET_CODE (PATTERN (ret)) != RETURN
16414           || !maybe_hot_bb_p (bb))
16415         continue;
16416       for (prev = PREV_INSN (ret); prev; prev = PREV_INSN (prev))
16417         if (active_insn_p (prev) || GET_CODE (prev) == CODE_LABEL)
16418           break;
16419       if (prev && GET_CODE (prev) == CODE_LABEL)
16420         {
16421           edge e;
16422           edge_iterator ei;
16423
16424           FOR_EACH_EDGE (e, ei, bb->preds)
16425             if (EDGE_FREQUENCY (e) && e->src->index >= 0
16426                 && !(e->flags & EDGE_FALLTHRU))
16427               replace = true;
16428         }
16429       if (!replace)
16430         {
16431           prev = prev_active_insn (ret);
16432           if (prev
16433               && ((GET_CODE (prev) == JUMP_INSN && any_condjump_p (prev))
16434                   || GET_CODE (prev) == CALL_INSN))
16435             replace = true;
16436           /* Empty functions get branch mispredict even when the jump destination
16437              is not visible to us.  */
16438           if (!prev && cfun->function_frequency > FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
16439             replace = true;
16440         }
16441       if (replace)
16442         {
16443           emit_insn_before (gen_return_internal_long (), ret);
16444           delete_insn (ret);
16445         }
16446     }
16447 }
16448
16449 /* Implement machine specific optimizations.  We implement padding of returns
16450    for K8 CPUs and pass to avoid 4 jumps in the single 16 byte window.  */
16451 static void
16452 ix86_reorg (void)
16453 {
16454   if (TARGET_ATHLON_K8 && optimize && !optimize_size)
16455     ix86_pad_returns ();
16456   if (TARGET_FOUR_JUMP_LIMIT && optimize && !optimize_size)
16457     ix86_avoid_jump_misspredicts ();
16458 }
16459
16460 /* Return nonzero when QImode register that must be represented via REX prefix
16461    is used.  */
16462 bool
16463 x86_extended_QIreg_mentioned_p (rtx insn)
16464 {
16465   int i;
16466   extract_insn_cached (insn);
16467   for (i = 0; i < recog_data.n_operands; i++)
16468     if (REG_P (recog_data.operand[i])
16469         && REGNO (recog_data.operand[i]) >= 4)
16470        return true;
16471   return false;
16472 }
16473
16474 /* Return nonzero when P points to register encoded via REX prefix.
16475    Called via for_each_rtx.  */
16476 static int
16477 extended_reg_mentioned_1 (rtx *p, void *data ATTRIBUTE_UNUSED)
16478 {
16479    unsigned int regno;
16480    if (!REG_P (*p))
16481      return 0;
16482    regno = REGNO (*p);
16483    return REX_INT_REGNO_P (regno) || REX_SSE_REGNO_P (regno);
16484 }
16485
16486 /* Return true when INSN mentions register that must be encoded using REX
16487    prefix.  */
16488 bool
16489 x86_extended_reg_mentioned_p (rtx insn)
16490 {
16491   return for_each_rtx (&PATTERN (insn), extended_reg_mentioned_1, NULL);
16492 }
16493
16494 /* Generate an unsigned DImode/SImode to FP conversion.  This is the same code
16495    optabs would emit if we didn't have TFmode patterns.  */
16496
16497 void
16498 x86_emit_floatuns (rtx operands[2])
16499 {
16500   rtx neglab, donelab, i0, i1, f0, in, out;
16501   enum machine_mode mode, inmode;
16502
16503   inmode = GET_MODE (operands[1]);
16504   gcc_assert (inmode == SImode || inmode == DImode);
16505
16506   out = operands[0];
16507   in = force_reg (inmode, operands[1]);
16508   mode = GET_MODE (out);
16509   neglab = gen_label_rtx ();
16510   donelab = gen_label_rtx ();
16511   i1 = gen_reg_rtx (Pmode);
16512   f0 = gen_reg_rtx (mode);
16513
16514   emit_cmp_and_jump_insns (in, const0_rtx, LT, const0_rtx, Pmode, 0, neglab);
16515
16516   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_FLOAT (mode, in)));
16517   emit_jump_insn (gen_jump (donelab));
16518   emit_barrier ();
16519
16520   emit_label (neglab);
16521
16522   i0 = expand_simple_binop (Pmode, LSHIFTRT, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
16523   i1 = expand_simple_binop (Pmode, AND, in, const1_rtx, NULL, 1, OPTAB_DIRECT);
16524   i0 = expand_simple_binop (Pmode, IOR, i0, i1, i0, 1, OPTAB_DIRECT);
16525   expand_float (f0, i0, 0);
16526   emit_insn (gen_rtx_SET (VOIDmode, out, gen_rtx_PLUS (mode, f0, f0)));
16527
16528   emit_label (donelab);
16529 }
16530 \f
16531 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
16532    with all elements equal to VAR.  Return true if successful.  */
16533
16534 static bool
16535 ix86_expand_vector_init_duplicate (bool mmx_ok, enum machine_mode mode,
16536                                    rtx target, rtx val)
16537 {
16538   enum machine_mode smode, wsmode, wvmode;
16539   rtx x;
16540
16541   switch (mode)
16542     {
16543     case V2SImode:
16544     case V2SFmode:
16545       if (!mmx_ok && !TARGET_SSE)
16546         return false;
16547       /* FALLTHRU */
16548
16549     case V2DFmode:
16550     case V2DImode:
16551     case V4SFmode:
16552     case V4SImode:
16553       val = force_reg (GET_MODE_INNER (mode), val);
16554       x = gen_rtx_VEC_DUPLICATE (mode, val);
16555       emit_insn (gen_rtx_SET (VOIDmode, target, x));
16556       return true;
16557
16558     case V4HImode:
16559       if (!mmx_ok)
16560         return false;
16561       if (TARGET_SSE || TARGET_3DNOW_A)
16562         {
16563           val = gen_lowpart (SImode, val);
16564           x = gen_rtx_TRUNCATE (HImode, val);
16565           x = gen_rtx_VEC_DUPLICATE (mode, x);
16566           emit_insn (gen_rtx_SET (VOIDmode, target, x));
16567           return true;
16568         }
16569       else
16570         {
16571           smode = HImode;
16572           wsmode = SImode;
16573           wvmode = V2SImode;
16574           goto widen;
16575         }
16576
16577     case V8QImode:
16578       if (!mmx_ok)
16579         return false;
16580       smode = QImode;
16581       wsmode = HImode;
16582       wvmode = V4HImode;
16583       goto widen;
16584     case V8HImode:
16585       smode = HImode;
16586       wsmode = SImode;
16587       wvmode = V4SImode;
16588       goto widen;
16589     case V16QImode:
16590       smode = QImode;
16591       wsmode = HImode;
16592       wvmode = V8HImode;
16593       goto widen;
16594     widen:
16595       /* Replicate the value once into the next wider mode and recurse.  */
16596       val = convert_modes (wsmode, smode, val, true);
16597       x = expand_simple_binop (wsmode, ASHIFT, val,
16598                                GEN_INT (GET_MODE_BITSIZE (smode)),
16599                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
16600       val = expand_simple_binop (wsmode, IOR, val, x, x, 1, OPTAB_LIB_WIDEN);
16601
16602       x = gen_reg_rtx (wvmode);
16603       if (!ix86_expand_vector_init_duplicate (mmx_ok, wvmode, x, val))
16604         gcc_unreachable ();
16605       emit_move_insn (target, gen_lowpart (mode, x));
16606       return true;
16607
16608     default:
16609       return false;
16610     }
16611 }
16612
16613 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
16614    whose low element is VAR, and other elements are zero.  Return true
16615    if successful.  */
16616
16617 static bool
16618 ix86_expand_vector_init_low_nonzero (bool mmx_ok, enum machine_mode mode,
16619                                      rtx target, rtx var)
16620 {
16621   enum machine_mode vsimode;
16622   rtx x;
16623
16624   switch (mode)
16625     {
16626     case V2SFmode:
16627     case V2SImode:
16628       if (!mmx_ok && !TARGET_SSE)
16629         return false;
16630       /* FALLTHRU */
16631
16632     case V2DFmode:
16633     case V2DImode:
16634       var = force_reg (GET_MODE_INNER (mode), var);
16635       x = gen_rtx_VEC_CONCAT (mode, var, CONST0_RTX (GET_MODE_INNER (mode)));
16636       emit_insn (gen_rtx_SET (VOIDmode, target, x));
16637       return true;
16638
16639     case V4SFmode:
16640     case V4SImode:
16641       var = force_reg (GET_MODE_INNER (mode), var);
16642       x = gen_rtx_VEC_DUPLICATE (mode, var);
16643       x = gen_rtx_VEC_MERGE (mode, x, CONST0_RTX (mode), const1_rtx);
16644       emit_insn (gen_rtx_SET (VOIDmode, target, x));
16645       return true;
16646
16647     case V8HImode:
16648     case V16QImode:
16649       vsimode = V4SImode;
16650       goto widen;
16651     case V4HImode:
16652     case V8QImode:
16653       if (!mmx_ok)
16654         return false;
16655       vsimode = V2SImode;
16656       goto widen;
16657     widen:
16658       /* Zero extend the variable element to SImode and recurse.  */
16659       var = convert_modes (SImode, GET_MODE_INNER (mode), var, true);
16660
16661       x = gen_reg_rtx (vsimode);
16662       if (!ix86_expand_vector_init_low_nonzero (mmx_ok, vsimode, x, var))
16663         gcc_unreachable ();
16664
16665       emit_move_insn (target, gen_lowpart (mode, x));
16666       return true;
16667
16668     default:
16669       return false;
16670     }
16671 }
16672
16673 /* A subroutine of ix86_expand_vector_init.  Store into TARGET a vector
16674    consisting of the values in VALS.  It is known that all elements
16675    except ONE_VAR are constants.  Return true if successful.  */
16676
16677 static bool
16678 ix86_expand_vector_init_one_var (bool mmx_ok, enum machine_mode mode,
16679                                  rtx target, rtx vals, int one_var)
16680 {
16681   rtx var = XVECEXP (vals, 0, one_var);
16682   enum machine_mode wmode;
16683   rtx const_vec, x;
16684
16685   XVECEXP (vals, 0, one_var) = CONST0_RTX (GET_MODE_INNER (mode));
16686   const_vec = gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)); 
16687
16688   switch (mode)
16689     {
16690     case V2DFmode:
16691     case V2DImode:
16692     case V2SFmode:
16693     case V2SImode:
16694       /* For the two element vectors, it's just as easy to use
16695          the general case.  */
16696       return false;
16697
16698     case V4SFmode:
16699     case V4SImode:
16700     case V8HImode:
16701     case V4HImode:
16702       break;
16703
16704     case V16QImode:
16705       wmode = V8HImode;
16706       goto widen;
16707     case V8QImode:
16708       wmode = V4HImode;
16709       goto widen;
16710     widen:
16711       /* There's no way to set one QImode entry easily.  Combine
16712          the variable value with its adjacent constant value, and
16713          promote to an HImode set.  */
16714       x = XVECEXP (vals, 0, one_var ^ 1);
16715       if (one_var & 1)
16716         {
16717           var = convert_modes (HImode, QImode, var, true);
16718           var = expand_simple_binop (HImode, ASHIFT, var, GEN_INT (8),
16719                                      NULL_RTX, 1, OPTAB_LIB_WIDEN);
16720           x = GEN_INT (INTVAL (x) & 0xff);
16721         }
16722       else
16723         {
16724           var = convert_modes (HImode, QImode, var, true);
16725           x = gen_int_mode (INTVAL (x) << 8, HImode);
16726         }
16727       if (x != const0_rtx)
16728         var = expand_simple_binop (HImode, IOR, var, x, var,
16729                                    1, OPTAB_LIB_WIDEN);
16730
16731       x = gen_reg_rtx (wmode);
16732       emit_move_insn (x, gen_lowpart (wmode, const_vec));
16733       ix86_expand_vector_set (mmx_ok, x, var, one_var >> 1);
16734
16735       emit_move_insn (target, gen_lowpart (mode, x));
16736       return true;
16737
16738     default:
16739       return false;
16740     }
16741
16742   emit_move_insn (target, const_vec);
16743   ix86_expand_vector_set (mmx_ok, target, var, one_var);
16744   return true;
16745 }
16746
16747 /* A subroutine of ix86_expand_vector_init.  Handle the most general case:
16748    all values variable, and none identical.  */
16749
16750 static void
16751 ix86_expand_vector_init_general (bool mmx_ok, enum machine_mode mode,
16752                                  rtx target, rtx vals)
16753 {
16754   enum machine_mode half_mode = GET_MODE_INNER (mode);
16755   rtx op0 = NULL, op1 = NULL;
16756   bool use_vec_concat = false;
16757
16758   switch (mode)
16759     {
16760     case V2SFmode:
16761     case V2SImode:
16762       if (!mmx_ok && !TARGET_SSE)
16763         break;
16764       /* FALLTHRU */
16765
16766     case V2DFmode:
16767     case V2DImode:
16768       /* For the two element vectors, we always implement VEC_CONCAT.  */
16769       op0 = XVECEXP (vals, 0, 0);
16770       op1 = XVECEXP (vals, 0, 1);
16771       use_vec_concat = true;
16772       break;
16773
16774     case V4SFmode:
16775       half_mode = V2SFmode;
16776       goto half;
16777     case V4SImode:
16778       half_mode = V2SImode;
16779       goto half;
16780     half:
16781       {
16782         rtvec v;
16783
16784         /* For V4SF and V4SI, we implement a concat of two V2 vectors.
16785            Recurse to load the two halves.  */
16786
16787         op0 = gen_reg_rtx (half_mode);
16788         v = gen_rtvec (2, XVECEXP (vals, 0, 0), XVECEXP (vals, 0, 1));
16789         ix86_expand_vector_init (false, op0, gen_rtx_PARALLEL (half_mode, v));
16790
16791         op1 = gen_reg_rtx (half_mode);
16792         v = gen_rtvec (2, XVECEXP (vals, 0, 2), XVECEXP (vals, 0, 3));
16793         ix86_expand_vector_init (false, op1, gen_rtx_PARALLEL (half_mode, v));
16794
16795         use_vec_concat = true;
16796       }
16797       break;
16798
16799     case V8HImode:
16800     case V16QImode:
16801     case V4HImode:
16802     case V8QImode:
16803       break;
16804
16805     default:
16806       gcc_unreachable ();
16807     }
16808
16809   if (use_vec_concat)
16810     {
16811       if (!register_operand (op0, half_mode))
16812         op0 = force_reg (half_mode, op0);
16813       if (!register_operand (op1, half_mode))
16814         op1 = force_reg (half_mode, op1);
16815
16816       emit_insn (gen_rtx_SET (VOIDmode, target, 
16817                               gen_rtx_VEC_CONCAT (mode, op0, op1)));
16818     }
16819   else
16820     {
16821       int i, j, n_elts, n_words, n_elt_per_word;
16822       enum machine_mode inner_mode;
16823       rtx words[4], shift;
16824
16825       inner_mode = GET_MODE_INNER (mode);
16826       n_elts = GET_MODE_NUNITS (mode);
16827       n_words = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
16828       n_elt_per_word = n_elts / n_words;
16829       shift = GEN_INT (GET_MODE_BITSIZE (inner_mode));
16830
16831       for (i = 0; i < n_words; ++i)
16832         {
16833           rtx word = NULL_RTX;
16834
16835           for (j = 0; j < n_elt_per_word; ++j)
16836             {
16837               rtx elt = XVECEXP (vals, 0, (i+1)*n_elt_per_word - j - 1);
16838               elt = convert_modes (word_mode, inner_mode, elt, true);
16839
16840               if (j == 0)
16841                 word = elt;
16842               else
16843                 {
16844                   word = expand_simple_binop (word_mode, ASHIFT, word, shift,
16845                                               word, 1, OPTAB_LIB_WIDEN);
16846                   word = expand_simple_binop (word_mode, IOR, word, elt,
16847                                               word, 1, OPTAB_LIB_WIDEN);
16848                 }
16849             }
16850
16851           words[i] = word;
16852         }
16853
16854       if (n_words == 1)
16855         emit_move_insn (target, gen_lowpart (mode, words[0]));
16856       else if (n_words == 2)
16857         {
16858           rtx tmp = gen_reg_rtx (mode);
16859           emit_insn (gen_rtx_CLOBBER (VOIDmode, tmp));
16860           emit_move_insn (gen_lowpart (word_mode, tmp), words[0]);
16861           emit_move_insn (gen_highpart (word_mode, tmp), words[1]);
16862           emit_move_insn (target, tmp);
16863         }
16864       else if (n_words == 4)
16865         {
16866           rtx tmp = gen_reg_rtx (V4SImode);
16867           vals = gen_rtx_PARALLEL (V4SImode, gen_rtvec_v (4, words));
16868           ix86_expand_vector_init_general (false, V4SImode, tmp, vals);
16869           emit_move_insn (target, gen_lowpart (mode, tmp));
16870         }
16871       else
16872         gcc_unreachable ();
16873     }
16874 }
16875
16876 /* Initialize vector TARGET via VALS.  Suppress the use of MMX 
16877    instructions unless MMX_OK is true.  */
16878
16879 void
16880 ix86_expand_vector_init (bool mmx_ok, rtx target, rtx vals)
16881 {
16882   enum machine_mode mode = GET_MODE (target);
16883   enum machine_mode inner_mode = GET_MODE_INNER (mode);
16884   int n_elts = GET_MODE_NUNITS (mode);
16885   int n_var = 0, one_var = -1;
16886   bool all_same = true, all_const_zero = true;
16887   int i;
16888   rtx x;
16889
16890   for (i = 0; i < n_elts; ++i)
16891     {
16892       x = XVECEXP (vals, 0, i);
16893       if (!CONSTANT_P (x))
16894         n_var++, one_var = i;
16895       else if (x != CONST0_RTX (inner_mode))
16896         all_const_zero = false;
16897       if (i > 0 && !rtx_equal_p (x, XVECEXP (vals, 0, 0)))
16898         all_same = false;
16899     }
16900
16901   /* Constants are best loaded from the constant pool.  */
16902   if (n_var == 0)
16903     {
16904       emit_move_insn (target, gen_rtx_CONST_VECTOR (mode, XVEC (vals, 0)));
16905       return;
16906     }
16907
16908   /* If all values are identical, broadcast the value.  */
16909   if (all_same
16910       && ix86_expand_vector_init_duplicate (mmx_ok, mode, target,
16911                                             XVECEXP (vals, 0, 0)))
16912     return;
16913
16914   /* Values where only one field is non-constant are best loaded from
16915      the pool and overwritten via move later.  */
16916   if (n_var == 1)
16917     {
16918       if (all_const_zero && one_var == 0
16919           && ix86_expand_vector_init_low_nonzero (mmx_ok, mode, target,
16920                                                   XVECEXP (vals, 0, 0)))
16921         return;
16922
16923       if (ix86_expand_vector_init_one_var (mmx_ok, mode, target, vals, one_var))
16924         return;
16925     }
16926
16927   ix86_expand_vector_init_general (mmx_ok, mode, target, vals);
16928 }
16929
16930 void
16931 ix86_expand_vector_set (bool mmx_ok, rtx target, rtx val, int elt)
16932 {
16933   enum machine_mode mode = GET_MODE (target);
16934   enum machine_mode inner_mode = GET_MODE_INNER (mode);
16935   bool use_vec_merge = false;
16936   rtx tmp;
16937
16938   switch (mode)
16939     {
16940     case V2SFmode:
16941     case V2SImode:
16942       if (mmx_ok)
16943         {
16944           tmp = gen_reg_rtx (GET_MODE_INNER (mode));
16945           ix86_expand_vector_extract (true, tmp, target, 1 - elt);
16946           if (elt == 0)
16947             tmp = gen_rtx_VEC_CONCAT (mode, tmp, val);
16948           else
16949             tmp = gen_rtx_VEC_CONCAT (mode, val, tmp);
16950           emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
16951           return;
16952         }
16953       break;
16954
16955     case V2DFmode:
16956     case V2DImode:
16957       {
16958         rtx op0, op1;
16959
16960         /* For the two element vectors, we implement a VEC_CONCAT with
16961            the extraction of the other element.  */
16962
16963         tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (1 - elt)));
16964         tmp = gen_rtx_VEC_SELECT (inner_mode, target, tmp);
16965
16966         if (elt == 0)
16967           op0 = val, op1 = tmp;
16968         else
16969           op0 = tmp, op1 = val;
16970
16971         tmp = gen_rtx_VEC_CONCAT (mode, op0, op1);
16972         emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
16973       }
16974       return;
16975
16976     case V4SFmode:
16977       switch (elt)
16978         {
16979         case 0:
16980           use_vec_merge = true;
16981           break;
16982
16983         case 1:
16984           /* tmp = op0 = A B C D */
16985           tmp = copy_to_reg (target);
16986
16987           /* op0 = C C D D */
16988           emit_insn (gen_sse_unpcklps (target, target, target));
16989
16990           /* op0 = C C D X */
16991           ix86_expand_vector_set (false, target, val, 0);
16992
16993           /* op0 = A B X D  */
16994           emit_insn (gen_sse_shufps_1 (target, target, tmp,
16995                                        GEN_INT (1), GEN_INT (0),
16996                                        GEN_INT (2+4), GEN_INT (3+4)));
16997           return;
16998
16999         case 2:
17000           tmp = copy_to_reg (target);
17001           ix86_expand_vector_set (false, target, val, 0);
17002           emit_insn (gen_sse_shufps_1 (target, target, tmp,
17003                                        GEN_INT (0), GEN_INT (1),
17004                                        GEN_INT (0+4), GEN_INT (3+4)));
17005           return;
17006
17007         case 3:
17008           tmp = copy_to_reg (target);
17009           ix86_expand_vector_set (false, target, val, 0);
17010           emit_insn (gen_sse_shufps_1 (target, target, tmp,
17011                                        GEN_INT (0), GEN_INT (1),
17012                                        GEN_INT (2+4), GEN_INT (0+4)));
17013           return;
17014
17015         default:
17016           gcc_unreachable ();
17017         }
17018       break;
17019
17020     case V4SImode:
17021       /* Element 0 handled by vec_merge below.  */
17022       if (elt == 0)
17023         {
17024           use_vec_merge = true;
17025           break;
17026         }
17027
17028       if (TARGET_SSE2)
17029         {
17030           /* With SSE2, use integer shuffles to swap element 0 and ELT,
17031              store into element 0, then shuffle them back.  */
17032
17033           rtx order[4];
17034
17035           order[0] = GEN_INT (elt);
17036           order[1] = const1_rtx;
17037           order[2] = const2_rtx;
17038           order[3] = GEN_INT (3);
17039           order[elt] = const0_rtx;
17040
17041           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
17042                                         order[1], order[2], order[3]));
17043
17044           ix86_expand_vector_set (false, target, val, 0);
17045
17046           emit_insn (gen_sse2_pshufd_1 (target, target, order[0],
17047                                         order[1], order[2], order[3]));
17048         }
17049       else
17050         {
17051           /* For SSE1, we have to reuse the V4SF code.  */
17052           ix86_expand_vector_set (false, gen_lowpart (V4SFmode, target),
17053                                   gen_lowpart (SFmode, val), elt);
17054         }
17055       return;
17056
17057     case V8HImode:
17058       use_vec_merge = TARGET_SSE2;
17059       break;
17060     case V4HImode:
17061       use_vec_merge = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
17062       break;
17063
17064     case V16QImode:
17065     case V8QImode:
17066     default:
17067       break;
17068     }
17069
17070   if (use_vec_merge)
17071     {
17072       tmp = gen_rtx_VEC_DUPLICATE (mode, val);
17073       tmp = gen_rtx_VEC_MERGE (mode, tmp, target, GEN_INT (1 << elt));
17074       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17075     }
17076   else
17077     {
17078       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
17079
17080       emit_move_insn (mem, target);
17081
17082       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
17083       emit_move_insn (tmp, val);
17084
17085       emit_move_insn (target, mem);
17086     }
17087 }
17088
17089 void
17090 ix86_expand_vector_extract (bool mmx_ok, rtx target, rtx vec, int elt)
17091 {
17092   enum machine_mode mode = GET_MODE (vec);
17093   enum machine_mode inner_mode = GET_MODE_INNER (mode);
17094   bool use_vec_extr = false;
17095   rtx tmp;
17096
17097   switch (mode)
17098     {
17099     case V2SImode:
17100     case V2SFmode:
17101       if (!mmx_ok)
17102         break;
17103       /* FALLTHRU */
17104
17105     case V2DFmode:
17106     case V2DImode:
17107       use_vec_extr = true;
17108       break;
17109
17110     case V4SFmode:
17111       switch (elt)
17112         {
17113         case 0:
17114           tmp = vec;
17115           break;
17116
17117         case 1:
17118         case 3:
17119           tmp = gen_reg_rtx (mode);
17120           emit_insn (gen_sse_shufps_1 (tmp, vec, vec,
17121                                        GEN_INT (elt), GEN_INT (elt),
17122                                        GEN_INT (elt+4), GEN_INT (elt+4)));
17123           break;
17124
17125         case 2:
17126           tmp = gen_reg_rtx (mode);
17127           emit_insn (gen_sse_unpckhps (tmp, vec, vec));
17128           break;
17129
17130         default:
17131           gcc_unreachable ();
17132         }
17133       vec = tmp;
17134       use_vec_extr = true;
17135       elt = 0;
17136       break;
17137
17138     case V4SImode:
17139       if (TARGET_SSE2)
17140         {
17141           switch (elt)
17142             {
17143             case 0:
17144               tmp = vec;
17145               break;
17146
17147             case 1:
17148             case 3:
17149               tmp = gen_reg_rtx (mode);
17150               emit_insn (gen_sse2_pshufd_1 (tmp, vec,
17151                                             GEN_INT (elt), GEN_INT (elt),
17152                                             GEN_INT (elt), GEN_INT (elt)));
17153               break;
17154
17155             case 2:
17156               tmp = gen_reg_rtx (mode);
17157               emit_insn (gen_sse2_punpckhdq (tmp, vec, vec));
17158               break;
17159
17160             default:
17161               gcc_unreachable ();
17162             }
17163           vec = tmp;
17164           use_vec_extr = true;
17165           elt = 0;
17166         }
17167       else
17168         {
17169           /* For SSE1, we have to reuse the V4SF code.  */
17170           ix86_expand_vector_extract (false, gen_lowpart (SFmode, target),
17171                                       gen_lowpart (V4SFmode, vec), elt);
17172           return;
17173         }
17174       break;
17175
17176     case V8HImode:
17177       use_vec_extr = TARGET_SSE2;
17178       break;
17179     case V4HImode:
17180       use_vec_extr = mmx_ok && (TARGET_SSE || TARGET_3DNOW_A);
17181       break;
17182
17183     case V16QImode:
17184     case V8QImode:
17185       /* ??? Could extract the appropriate HImode element and shift.  */
17186     default:
17187       break;
17188     }
17189
17190   if (use_vec_extr)
17191     {
17192       tmp = gen_rtx_PARALLEL (VOIDmode, gen_rtvec (1, GEN_INT (elt)));
17193       tmp = gen_rtx_VEC_SELECT (inner_mode, vec, tmp);
17194
17195       /* Let the rtl optimizers know about the zero extension performed.  */
17196       if (inner_mode == HImode)
17197         {
17198           tmp = gen_rtx_ZERO_EXTEND (SImode, tmp);
17199           target = gen_lowpart (SImode, target);
17200         }
17201
17202       emit_insn (gen_rtx_SET (VOIDmode, target, tmp));
17203     }
17204   else
17205     {
17206       rtx mem = assign_stack_temp (mode, GET_MODE_SIZE (mode), false);
17207
17208       emit_move_insn (mem, vec);
17209
17210       tmp = adjust_address (mem, inner_mode, elt*GET_MODE_SIZE (inner_mode));
17211       emit_move_insn (target, tmp);
17212     }
17213 }
17214 \f
17215 /* Implements target hook vector_mode_supported_p.  */
17216 static bool
17217 ix86_vector_mode_supported_p (enum machine_mode mode)
17218 {
17219   if (TARGET_SSE && VALID_SSE_REG_MODE (mode))
17220     return true;
17221   if (TARGET_SSE2 && VALID_SSE2_REG_MODE (mode))
17222     return true;
17223   if (TARGET_MMX && VALID_MMX_REG_MODE (mode))
17224     return true;
17225   if (TARGET_3DNOW && VALID_MMX_REG_MODE_3DNOW (mode))
17226     return true;
17227   return false;
17228 }
17229
17230 /* Worker function for TARGET_MD_ASM_CLOBBERS.
17231
17232    We do this in the new i386 backend to maintain source compatibility
17233    with the old cc0-based compiler.  */
17234
17235 static tree
17236 ix86_md_asm_clobbers (tree outputs ATTRIBUTE_UNUSED,
17237                       tree inputs ATTRIBUTE_UNUSED,
17238                       tree clobbers)
17239 {
17240   clobbers = tree_cons (NULL_TREE, build_string (5, "flags"),
17241                         clobbers);
17242   clobbers = tree_cons (NULL_TREE, build_string (4, "fpsr"),
17243                         clobbers);
17244   clobbers = tree_cons (NULL_TREE, build_string (7, "dirflag"),
17245                         clobbers);
17246   return clobbers;
17247 }
17248
17249 /* Worker function for REVERSE_CONDITION.  */
17250
17251 enum rtx_code
17252 ix86_reverse_condition (enum rtx_code code, enum machine_mode mode)
17253 {
17254   return (mode != CCFPmode && mode != CCFPUmode
17255           ? reverse_condition (code)
17256           : reverse_condition_maybe_unordered (code));
17257 }
17258
17259 /* Output code to perform an x87 FP register move, from OPERANDS[1]
17260    to OPERANDS[0].  */
17261
17262 const char *
17263 output_387_reg_move (rtx insn, rtx *operands)
17264 {
17265   if (REG_P (operands[1])
17266       && find_regno_note (insn, REG_DEAD, REGNO (operands[1])))
17267     {
17268       if (REGNO (operands[0]) == FIRST_STACK_REG
17269           && TARGET_USE_FFREEP)
17270         return "ffreep\t%y0";
17271       return "fstp\t%y0";
17272     }
17273   if (STACK_TOP_P (operands[0]))
17274     return "fld%z1\t%y1";
17275   return "fst\t%y0";
17276 }
17277
17278 /* Output code to perform a conditional jump to LABEL, if C2 flag in
17279    FP status register is set.  */
17280
17281 void
17282 ix86_emit_fp_unordered_jump (rtx label)
17283 {
17284   rtx reg = gen_reg_rtx (HImode);
17285   rtx temp;
17286
17287   emit_insn (gen_x86_fnstsw_1 (reg));
17288
17289   if (TARGET_USE_SAHF)
17290     {
17291       emit_insn (gen_x86_sahf_1 (reg));
17292
17293       temp = gen_rtx_REG (CCmode, FLAGS_REG);
17294       temp = gen_rtx_UNORDERED (VOIDmode, temp, const0_rtx);
17295     }
17296   else
17297     {
17298       emit_insn (gen_testqi_ext_ccno_0 (reg, GEN_INT (0x04)));
17299
17300       temp = gen_rtx_REG (CCNOmode, FLAGS_REG);
17301       temp = gen_rtx_NE (VOIDmode, temp, const0_rtx);
17302     }
17303
17304   temp = gen_rtx_IF_THEN_ELSE (VOIDmode, temp,
17305                               gen_rtx_LABEL_REF (VOIDmode, label),
17306                               pc_rtx);
17307   temp = gen_rtx_SET (VOIDmode, pc_rtx, temp);
17308   emit_jump_insn (temp);
17309 }
17310
17311 /* Output code to perform a log1p XFmode calculation.  */
17312
17313 void ix86_emit_i387_log1p (rtx op0, rtx op1)
17314 {
17315   rtx label1 = gen_label_rtx ();
17316   rtx label2 = gen_label_rtx ();
17317
17318   rtx tmp = gen_reg_rtx (XFmode);
17319   rtx tmp2 = gen_reg_rtx (XFmode);
17320
17321   emit_insn (gen_absxf2 (tmp, op1));
17322   emit_insn (gen_cmpxf (tmp,
17323     CONST_DOUBLE_FROM_REAL_VALUE (
17324        REAL_VALUE_ATOF ("0.29289321881345247561810596348408353", XFmode),
17325        XFmode)));
17326   emit_jump_insn (gen_bge (label1));
17327
17328   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
17329   emit_insn (gen_fyl2xp1_xf3 (op0, tmp2, op1));
17330   emit_jump (label2);
17331
17332   emit_label (label1);
17333   emit_move_insn (tmp, CONST1_RTX (XFmode));
17334   emit_insn (gen_addxf3 (tmp, op1, tmp));
17335   emit_move_insn (tmp2, standard_80387_constant_rtx (4)); /* fldln2 */
17336   emit_insn (gen_fyl2x_xf3 (op0, tmp2, tmp));
17337
17338   emit_label (label2);
17339 }
17340
17341 /* Solaris named-section hook.  Parameters are as for
17342    named_section_real.  */
17343
17344 static void
17345 i386_solaris_elf_named_section (const char *name, unsigned int flags,
17346                                 tree decl)
17347 {
17348   /* With Binutils 2.15, the "@unwind" marker must be specified on
17349      every occurrence of the ".eh_frame" section, not just the first
17350      one.  */
17351   if (TARGET_64BIT
17352       && strcmp (name, ".eh_frame") == 0)
17353     {
17354       fprintf (asm_out_file, "\t.section\t%s,\"%s\",@unwind\n", name,
17355                flags & SECTION_WRITE ? "aw" : "a");
17356       return;
17357     }
17358   default_elf_asm_named_section (name, flags, decl);
17359 }
17360
17361 #include "gt-i386.h"